]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - drivers/watchdog/asm9260_wdt.c
Merge tag 'soc-fsl-fix-v5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/leo...
[mirror_ubuntu-focal-kernel.git] / drivers / watchdog / asm9260_wdt.c
1 /*
2 * Watchdog driver for Alphascale ASM9260.
3 *
4 * Copyright (c) 2014 Oleksij Rempel <linux@rempel-privat.de>
5 *
6 * Licensed under GPLv2 or later.
7 */
8
9 #include <linux/bitops.h>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 #include <linux/reset.h>
18 #include <linux/watchdog.h>
19
20 #define CLOCK_FREQ 1000000
21
22 /* Watchdog Mode register */
23 #define HW_WDMOD 0x00
24 /* Wake interrupt. Set by HW, can't be cleared. */
25 #define BM_MOD_WDINT BIT(3)
26 /* This bit set if timeout reached. Cleared by SW. */
27 #define BM_MOD_WDTOF BIT(2)
28 /* HW Reset on timeout */
29 #define BM_MOD_WDRESET BIT(1)
30 /* WD enable */
31 #define BM_MOD_WDEN BIT(0)
32
33 /*
34 * Watchdog Timer Constant register
35 * Minimal value is 0xff, the meaning of this value
36 * depends on used clock: T = WDCLK * (0xff + 1) * 4
37 */
38 #define HW_WDTC 0x04
39 #define BM_WDTC_MAX(freq) (0x7fffffff / (freq))
40
41 /* Watchdog Feed register */
42 #define HW_WDFEED 0x08
43
44 /* Watchdog Timer Value register */
45 #define HW_WDTV 0x0c
46
47 #define ASM9260_WDT_DEFAULT_TIMEOUT 30
48
49 enum asm9260_wdt_mode {
50 HW_RESET,
51 SW_RESET,
52 DEBUG,
53 };
54
55 struct asm9260_wdt_priv {
56 struct device *dev;
57 struct watchdog_device wdd;
58 struct clk *clk;
59 struct clk *clk_ahb;
60 struct reset_control *rst;
61
62 void __iomem *iobase;
63 int irq;
64 unsigned long wdt_freq;
65 enum asm9260_wdt_mode mode;
66 };
67
68 static int asm9260_wdt_feed(struct watchdog_device *wdd)
69 {
70 struct asm9260_wdt_priv *priv = watchdog_get_drvdata(wdd);
71
72 iowrite32(0xaa, priv->iobase + HW_WDFEED);
73 iowrite32(0x55, priv->iobase + HW_WDFEED);
74
75 return 0;
76 }
77
78 static unsigned int asm9260_wdt_gettimeleft(struct watchdog_device *wdd)
79 {
80 struct asm9260_wdt_priv *priv = watchdog_get_drvdata(wdd);
81 u32 counter;
82
83 counter = ioread32(priv->iobase + HW_WDTV);
84
85 return counter / priv->wdt_freq;
86 }
87
88 static int asm9260_wdt_updatetimeout(struct watchdog_device *wdd)
89 {
90 struct asm9260_wdt_priv *priv = watchdog_get_drvdata(wdd);
91 u32 counter;
92
93 counter = wdd->timeout * priv->wdt_freq;
94
95 iowrite32(counter, priv->iobase + HW_WDTC);
96
97 return 0;
98 }
99
100 static int asm9260_wdt_enable(struct watchdog_device *wdd)
101 {
102 struct asm9260_wdt_priv *priv = watchdog_get_drvdata(wdd);
103 u32 mode = 0;
104
105 if (priv->mode == HW_RESET)
106 mode = BM_MOD_WDRESET;
107
108 iowrite32(BM_MOD_WDEN | mode, priv->iobase + HW_WDMOD);
109
110 asm9260_wdt_updatetimeout(wdd);
111
112 asm9260_wdt_feed(wdd);
113
114 return 0;
115 }
116
117 static int asm9260_wdt_disable(struct watchdog_device *wdd)
118 {
119 struct asm9260_wdt_priv *priv = watchdog_get_drvdata(wdd);
120
121 /* The only way to disable WD is to reset it. */
122 reset_control_assert(priv->rst);
123 reset_control_deassert(priv->rst);
124
125 return 0;
126 }
127
128 static int asm9260_wdt_settimeout(struct watchdog_device *wdd, unsigned int to)
129 {
130 wdd->timeout = to;
131 asm9260_wdt_updatetimeout(wdd);
132
133 return 0;
134 }
135
136 static void asm9260_wdt_sys_reset(struct asm9260_wdt_priv *priv)
137 {
138 /* init WD if it was not started */
139
140 iowrite32(BM_MOD_WDEN | BM_MOD_WDRESET, priv->iobase + HW_WDMOD);
141
142 iowrite32(0xff, priv->iobase + HW_WDTC);
143 /* first pass correct sequence */
144 asm9260_wdt_feed(&priv->wdd);
145 /*
146 * Then write wrong pattern to the feed to trigger reset
147 * ASAP.
148 */
149 iowrite32(0xff, priv->iobase + HW_WDFEED);
150
151 mdelay(1000);
152 }
153
154 static irqreturn_t asm9260_wdt_irq(int irq, void *devid)
155 {
156 struct asm9260_wdt_priv *priv = devid;
157 u32 stat;
158
159 stat = ioread32(priv->iobase + HW_WDMOD);
160 if (!(stat & BM_MOD_WDINT))
161 return IRQ_NONE;
162
163 if (priv->mode == DEBUG) {
164 dev_info(priv->dev, "Watchdog Timeout. Do nothing.\n");
165 } else {
166 dev_info(priv->dev, "Watchdog Timeout. Doing SW Reset.\n");
167 asm9260_wdt_sys_reset(priv);
168 }
169
170 return IRQ_HANDLED;
171 }
172
173 static int asm9260_restart(struct watchdog_device *wdd, unsigned long action,
174 void *data)
175 {
176 struct asm9260_wdt_priv *priv = watchdog_get_drvdata(wdd);
177
178 asm9260_wdt_sys_reset(priv);
179
180 return 0;
181 }
182
183 static const struct watchdog_info asm9260_wdt_ident = {
184 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
185 | WDIOF_MAGICCLOSE,
186 .identity = "Alphascale asm9260 Watchdog",
187 };
188
189 static const struct watchdog_ops asm9260_wdt_ops = {
190 .owner = THIS_MODULE,
191 .start = asm9260_wdt_enable,
192 .stop = asm9260_wdt_disable,
193 .get_timeleft = asm9260_wdt_gettimeleft,
194 .ping = asm9260_wdt_feed,
195 .set_timeout = asm9260_wdt_settimeout,
196 .restart = asm9260_restart,
197 };
198
199 static void asm9260_clk_disable_unprepare(void *data)
200 {
201 clk_disable_unprepare(data);
202 }
203
204 static int asm9260_wdt_get_dt_clks(struct asm9260_wdt_priv *priv)
205 {
206 int err;
207 unsigned long clk;
208
209 priv->clk = devm_clk_get(priv->dev, "mod");
210 if (IS_ERR(priv->clk)) {
211 dev_err(priv->dev, "Failed to get \"mod\" clk\n");
212 return PTR_ERR(priv->clk);
213 }
214
215 /* configure AHB clock */
216 priv->clk_ahb = devm_clk_get(priv->dev, "ahb");
217 if (IS_ERR(priv->clk_ahb)) {
218 dev_err(priv->dev, "Failed to get \"ahb\" clk\n");
219 return PTR_ERR(priv->clk_ahb);
220 }
221
222 err = clk_prepare_enable(priv->clk_ahb);
223 if (err) {
224 dev_err(priv->dev, "Failed to enable ahb_clk!\n");
225 return err;
226 }
227 err = devm_add_action_or_reset(priv->dev,
228 asm9260_clk_disable_unprepare,
229 priv->clk_ahb);
230 if (err)
231 return err;
232
233 err = clk_set_rate(priv->clk, CLOCK_FREQ);
234 if (err) {
235 dev_err(priv->dev, "Failed to set rate!\n");
236 return err;
237 }
238
239 err = clk_prepare_enable(priv->clk);
240 if (err) {
241 dev_err(priv->dev, "Failed to enable clk!\n");
242 return err;
243 }
244 err = devm_add_action_or_reset(priv->dev,
245 asm9260_clk_disable_unprepare,
246 priv->clk);
247 if (err)
248 return err;
249
250 /* wdt has internal divider */
251 clk = clk_get_rate(priv->clk);
252 if (!clk) {
253 dev_err(priv->dev, "Failed, clk is 0!\n");
254 return -EINVAL;
255 }
256
257 priv->wdt_freq = clk / 2;
258
259 return 0;
260 }
261
262 static void asm9260_wdt_get_dt_mode(struct asm9260_wdt_priv *priv)
263 {
264 const char *tmp;
265 int ret;
266
267 /* default mode */
268 priv->mode = HW_RESET;
269
270 ret = of_property_read_string(priv->dev->of_node,
271 "alphascale,mode", &tmp);
272 if (ret < 0)
273 return;
274
275 if (!strcmp(tmp, "hw"))
276 priv->mode = HW_RESET;
277 else if (!strcmp(tmp, "sw"))
278 priv->mode = SW_RESET;
279 else if (!strcmp(tmp, "debug"))
280 priv->mode = DEBUG;
281 else
282 dev_warn(priv->dev, "unknown reset-type: %s. Using default \"hw\" mode.",
283 tmp);
284 }
285
286 static int asm9260_wdt_probe(struct platform_device *pdev)
287 {
288 struct device *dev = &pdev->dev;
289 struct asm9260_wdt_priv *priv;
290 struct watchdog_device *wdd;
291 int ret;
292 static const char * const mode_name[] = { "hw", "sw", "debug", };
293
294 priv = devm_kzalloc(dev, sizeof(struct asm9260_wdt_priv), GFP_KERNEL);
295 if (!priv)
296 return -ENOMEM;
297
298 priv->dev = dev;
299
300 priv->iobase = devm_platform_ioremap_resource(pdev, 0);
301 if (IS_ERR(priv->iobase))
302 return PTR_ERR(priv->iobase);
303
304 priv->rst = devm_reset_control_get_exclusive(dev, "wdt_rst");
305 if (IS_ERR(priv->rst))
306 return PTR_ERR(priv->rst);
307
308 ret = asm9260_wdt_get_dt_clks(priv);
309 if (ret)
310 return ret;
311
312 wdd = &priv->wdd;
313 wdd->info = &asm9260_wdt_ident;
314 wdd->ops = &asm9260_wdt_ops;
315 wdd->min_timeout = 1;
316 wdd->max_timeout = BM_WDTC_MAX(priv->wdt_freq);
317 wdd->parent = dev;
318
319 watchdog_set_drvdata(wdd, priv);
320
321 /*
322 * If 'timeout-sec' unspecified in devicetree, assume a 30 second
323 * default, unless the max timeout is less than 30 seconds, then use
324 * the max instead.
325 */
326 wdd->timeout = ASM9260_WDT_DEFAULT_TIMEOUT;
327 watchdog_init_timeout(wdd, 0, dev);
328
329 asm9260_wdt_get_dt_mode(priv);
330
331 if (priv->mode != HW_RESET)
332 priv->irq = platform_get_irq(pdev, 0);
333
334 if (priv->irq > 0) {
335 /*
336 * Not all supported platforms specify an interrupt for the
337 * watchdog, so let's make it optional.
338 */
339 ret = devm_request_irq(dev, priv->irq, asm9260_wdt_irq, 0,
340 pdev->name, priv);
341 if (ret < 0)
342 dev_warn(dev, "failed to request IRQ\n");
343 }
344
345 watchdog_set_restart_priority(wdd, 128);
346
347 watchdog_stop_on_reboot(wdd);
348 watchdog_stop_on_unregister(wdd);
349 ret = devm_watchdog_register_device(dev, wdd);
350 if (ret)
351 return ret;
352
353 platform_set_drvdata(pdev, priv);
354
355 dev_info(dev, "Watchdog enabled (timeout: %d sec, mode: %s)\n",
356 wdd->timeout, mode_name[priv->mode]);
357 return 0;
358 }
359
360 static const struct of_device_id asm9260_wdt_of_match[] = {
361 { .compatible = "alphascale,asm9260-wdt"},
362 {},
363 };
364 MODULE_DEVICE_TABLE(of, asm9260_wdt_of_match);
365
366 static struct platform_driver asm9260_wdt_driver = {
367 .driver = {
368 .name = "asm9260-wdt",
369 .of_match_table = asm9260_wdt_of_match,
370 },
371 .probe = asm9260_wdt_probe,
372 };
373 module_platform_driver(asm9260_wdt_driver);
374
375 MODULE_DESCRIPTION("asm9260 WatchDog Timer Driver");
376 MODULE_AUTHOR("Oleksij Rempel <linux@rempel-privat.de>");
377 MODULE_LICENSE("GPL");