Merge tag 'rpmsg-v6.6' of git://git.kernel.org/pub/scm/linux/kernel/git/remoteproc...
[platform/kernel/linux-rpi.git] / drivers / watchdog / loongson1_wdt.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2016 Yang Ling <gnaygnil@gmail.com>
4  */
5
6 #include <linux/clk.h>
7 #include <linux/module.h>
8 #include <linux/of.h>
9 #include <linux/platform_device.h>
10 #include <linux/watchdog.h>
11
12 /* Loongson 1 Watchdog Register Definitions */
13 #define WDT_EN                  0x0
14 #define WDT_TIMER               0x4
15 #define WDT_SET                 0x8
16
17 #define DEFAULT_HEARTBEAT       30
18
19 static bool nowayout = WATCHDOG_NOWAYOUT;
20 module_param(nowayout, bool, 0444);
21
22 static unsigned int heartbeat;
23 module_param(heartbeat, uint, 0444);
24
25 struct ls1x_wdt_drvdata {
26         void __iomem *base;
27         struct clk *clk;
28         unsigned long clk_rate;
29         struct watchdog_device wdt;
30 };
31
32 static int ls1x_wdt_ping(struct watchdog_device *wdt_dev)
33 {
34         struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
35
36         writel(0x1, drvdata->base + WDT_SET);
37
38         return 0;
39 }
40
41 static int ls1x_wdt_set_timeout(struct watchdog_device *wdt_dev,
42                                 unsigned int timeout)
43 {
44         struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
45         unsigned int max_hw_heartbeat = wdt_dev->max_hw_heartbeat_ms / 1000;
46         unsigned int counts;
47
48         wdt_dev->timeout = timeout;
49
50         counts = drvdata->clk_rate * min(timeout, max_hw_heartbeat);
51         writel(counts, drvdata->base + WDT_TIMER);
52
53         return 0;
54 }
55
56 static int ls1x_wdt_start(struct watchdog_device *wdt_dev)
57 {
58         struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
59
60         writel(0x1, drvdata->base + WDT_EN);
61
62         return 0;
63 }
64
65 static int ls1x_wdt_stop(struct watchdog_device *wdt_dev)
66 {
67         struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
68
69         writel(0x0, drvdata->base + WDT_EN);
70
71         return 0;
72 }
73
74 static int ls1x_wdt_restart(struct watchdog_device *wdt_dev,
75                             unsigned long action, void *data)
76 {
77         struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
78
79         writel(0x1, drvdata->base + WDT_EN);
80         writel(0x1, drvdata->base + WDT_TIMER);
81         writel(0x1, drvdata->base + WDT_SET);
82
83         return 0;
84 }
85
86 static const struct watchdog_info ls1x_wdt_info = {
87         .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
88         .identity = "Loongson1 Watchdog",
89 };
90
91 static const struct watchdog_ops ls1x_wdt_ops = {
92         .owner = THIS_MODULE,
93         .start = ls1x_wdt_start,
94         .stop = ls1x_wdt_stop,
95         .ping = ls1x_wdt_ping,
96         .set_timeout = ls1x_wdt_set_timeout,
97         .restart = ls1x_wdt_restart,
98 };
99
100 static int ls1x_wdt_probe(struct platform_device *pdev)
101 {
102         struct device *dev = &pdev->dev;
103         struct ls1x_wdt_drvdata *drvdata;
104         struct watchdog_device *ls1x_wdt;
105         unsigned long clk_rate;
106         int err;
107
108         drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
109         if (!drvdata)
110                 return -ENOMEM;
111
112         drvdata->base = devm_platform_ioremap_resource(pdev, 0);
113         if (IS_ERR(drvdata->base))
114                 return PTR_ERR(drvdata->base);
115
116         drvdata->clk = devm_clk_get_enabled(dev, NULL);
117         if (IS_ERR(drvdata->clk))
118                 return PTR_ERR(drvdata->clk);
119
120         clk_rate = clk_get_rate(drvdata->clk);
121         if (!clk_rate)
122                 return -EINVAL;
123         drvdata->clk_rate = clk_rate;
124
125         ls1x_wdt = &drvdata->wdt;
126         ls1x_wdt->info = &ls1x_wdt_info;
127         ls1x_wdt->ops = &ls1x_wdt_ops;
128         ls1x_wdt->timeout = DEFAULT_HEARTBEAT;
129         ls1x_wdt->min_timeout = 1;
130         ls1x_wdt->max_hw_heartbeat_ms = U32_MAX / clk_rate * 1000;
131         ls1x_wdt->parent = dev;
132
133         watchdog_init_timeout(ls1x_wdt, heartbeat, dev);
134         watchdog_set_nowayout(ls1x_wdt, nowayout);
135         watchdog_set_drvdata(ls1x_wdt, drvdata);
136
137         err = devm_watchdog_register_device(dev, &drvdata->wdt);
138         if (err)
139                 return err;
140
141         platform_set_drvdata(pdev, drvdata);
142
143         dev_info(dev, "Loongson1 Watchdog driver registered\n");
144
145         return 0;
146 }
147
148 #ifdef CONFIG_OF
149 static const struct of_device_id ls1x_wdt_dt_ids[] = {
150         { .compatible = "loongson,ls1b-wdt", },
151         { .compatible = "loongson,ls1c-wdt", },
152         { /* sentinel */ }
153 };
154 MODULE_DEVICE_TABLE(of, ls1x_wdt_dt_ids);
155 #endif
156
157 static struct platform_driver ls1x_wdt_driver = {
158         .probe = ls1x_wdt_probe,
159         .driver = {
160                 .name = "ls1x-wdt",
161                 .of_match_table = of_match_ptr(ls1x_wdt_dt_ids),
162         },
163 };
164
165 module_platform_driver(ls1x_wdt_driver);
166
167 MODULE_AUTHOR("Yang Ling <gnaygnil@gmail.com>");
168 MODULE_DESCRIPTION("Loongson1 Watchdog Driver");
169 MODULE_LICENSE("GPL");