1
2
3
4
5
6
7#include <linux/bitops.h>
8#include <linux/clk.h>
9#include <linux/delay.h>
10#include <linux/io.h>
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/platform_device.h>
15#include <linux/watchdog.h>
16
17#define DEFAULT_TIMEOUT 30
18
19static bool nowayout = WATCHDOG_NOWAYOUT;
20module_param(nowayout, bool, 0);
21MODULE_PARM_DESC(nowayout,
22 "Watchdog cannot be stopped once started (default="
23 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
24
25static unsigned int timeout;
26module_param(timeout, int, 0);
27MODULE_PARM_DESC(timeout, "Watchdog timeout");
28
29
30
31
32
33#define WD_COUNTER 0
34
35#define WD_CONFIG 4
36#define WD_CONFIG_XTAL_IN BIT(0)
37#define WD_CONFIG_DISABLE BIT(31)
38
39struct tangox_wdt_device {
40 struct watchdog_device wdt;
41 void __iomem *base;
42 unsigned long clk_rate;
43 struct clk *clk;
44};
45
46static int tangox_wdt_set_timeout(struct watchdog_device *wdt,
47 unsigned int new_timeout)
48{
49 wdt->timeout = new_timeout;
50
51 return 0;
52}
53
54static int tangox_wdt_start(struct watchdog_device *wdt)
55{
56 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
57 u32 ticks;
58
59 ticks = 1 + wdt->timeout * dev->clk_rate;
60 writel(ticks, dev->base + WD_COUNTER);
61
62 return 0;
63}
64
65static int tangox_wdt_stop(struct watchdog_device *wdt)
66{
67 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
68
69 writel(0, dev->base + WD_COUNTER);
70
71 return 0;
72}
73
74static unsigned int tangox_wdt_get_timeleft(struct watchdog_device *wdt)
75{
76 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
77 u32 count;
78
79 count = readl(dev->base + WD_COUNTER);
80
81 if (!count)
82 return 0;
83
84 return (count - 1) / dev->clk_rate;
85}
86
87static const struct watchdog_info tangox_wdt_info = {
88 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
89 .identity = "tangox watchdog",
90};
91
92static int tangox_wdt_restart(struct watchdog_device *wdt,
93 unsigned long action, void *data)
94{
95 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
96
97 writel(1, dev->base + WD_COUNTER);
98
99 return 0;
100}
101
102static const struct watchdog_ops tangox_wdt_ops = {
103 .start = tangox_wdt_start,
104 .stop = tangox_wdt_stop,
105 .set_timeout = tangox_wdt_set_timeout,
106 .get_timeleft = tangox_wdt_get_timeleft,
107 .restart = tangox_wdt_restart,
108};
109
110static int tangox_wdt_probe(struct platform_device *pdev)
111{
112 struct tangox_wdt_device *dev;
113 struct resource *res;
114 u32 config;
115 int err;
116
117 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
118 if (!dev)
119 return -ENOMEM;
120
121 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
122 dev->base = devm_ioremap_resource(&pdev->dev, res);
123 if (IS_ERR(dev->base))
124 return PTR_ERR(dev->base);
125
126 dev->clk = devm_clk_get(&pdev->dev, NULL);
127 if (IS_ERR(dev->clk))
128 return PTR_ERR(dev->clk);
129
130 err = clk_prepare_enable(dev->clk);
131 if (err)
132 return err;
133
134 dev->clk_rate = clk_get_rate(dev->clk);
135 if (!dev->clk_rate) {
136 err = -EINVAL;
137 goto err;
138 }
139
140 dev->wdt.parent = &pdev->dev;
141 dev->wdt.info = &tangox_wdt_info;
142 dev->wdt.ops = &tangox_wdt_ops;
143 dev->wdt.timeout = DEFAULT_TIMEOUT;
144 dev->wdt.min_timeout = 1;
145 dev->wdt.max_hw_heartbeat_ms = (U32_MAX - 1) / dev->clk_rate;
146
147 watchdog_init_timeout(&dev->wdt, timeout, &pdev->dev);
148 watchdog_set_nowayout(&dev->wdt, nowayout);
149 watchdog_set_drvdata(&dev->wdt, dev);
150
151
152
153
154
155 config = readl(dev->base + WD_CONFIG);
156 if (config & WD_CONFIG_DISABLE)
157 writel(0, dev->base + WD_COUNTER);
158
159 writel(WD_CONFIG_XTAL_IN, dev->base + WD_CONFIG);
160
161
162
163
164
165 if (readl(dev->base + WD_COUNTER)) {
166 set_bit(WDOG_HW_RUNNING, &dev->wdt.status);
167 tangox_wdt_start(&dev->wdt);
168 }
169
170 watchdog_set_restart_priority(&dev->wdt, 128);
171
172 err = watchdog_register_device(&dev->wdt);
173 if (err)
174 goto err;
175
176 platform_set_drvdata(pdev, dev);
177
178 dev_info(&pdev->dev, "SMP86xx/SMP87xx watchdog registered\n");
179
180 return 0;
181
182 err:
183 clk_disable_unprepare(dev->clk);
184 return err;
185}
186
187static int tangox_wdt_remove(struct platform_device *pdev)
188{
189 struct tangox_wdt_device *dev = platform_get_drvdata(pdev);
190
191 tangox_wdt_stop(&dev->wdt);
192 clk_disable_unprepare(dev->clk);
193
194 watchdog_unregister_device(&dev->wdt);
195
196 return 0;
197}
198
199static const struct of_device_id tangox_wdt_dt_ids[] = {
200 { .compatible = "sigma,smp8642-wdt" },
201 { .compatible = "sigma,smp8759-wdt" },
202 { }
203};
204MODULE_DEVICE_TABLE(of, tangox_wdt_dt_ids);
205
206static struct platform_driver tangox_wdt_driver = {
207 .probe = tangox_wdt_probe,
208 .remove = tangox_wdt_remove,
209 .driver = {
210 .name = "tangox-wdt",
211 .of_match_table = tangox_wdt_dt_ids,
212 },
213};
214
215module_platform_driver(tangox_wdt_driver);
216
217MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
218MODULE_DESCRIPTION("SMP86xx/SMP87xx Watchdog driver");
219MODULE_LICENSE("GPL");
220