1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * drivers/char/watchdog/max63xx_wdt.c
4 *
5 * Driver for max63{69,70,71,72,73,74} watchdog timers
6 *
7 * Copyright (C) 2009 Marc Zyngier <maz@misterjones.org>
8 *
9 * This driver assumes the watchdog pins are memory mapped (as it is
10 * the case for the Arcom Zeus). Should it be connected over GPIOs or
11 * another interface, some abstraction will have to be introduced.
12 */
13
14 #include <linux/err.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/watchdog.h>
20 #include <linux/bitops.h>
21 #include <linux/platform_device.h>
22 #include <linux/spinlock.h>
23 #include <linux/io.h>
24 #include <linux/slab.h>
25 #include <linux/property.h>
26
27 #define DEFAULT_HEARTBEAT 60
28 #define MAX_HEARTBEAT 60
29
30 static unsigned int heartbeat = DEFAULT_HEARTBEAT;
31 static bool nowayout = WATCHDOG_NOWAYOUT;
32
33 /*
34 * Memory mapping: a single byte, 3 first lower bits to select bit 3
35 * to ping the watchdog.
36 */
37 #define MAX6369_WDSET (7 << 0)
38 #define MAX6369_WDI (1 << 3)
39
40 #define MAX6369_WDSET_DISABLED 3
41
42 static int nodelay;
43
44 struct max63xx_wdt {
45 struct watchdog_device wdd;
46 const struct max63xx_timeout *timeout;
47
48 /* memory mapping */
49 void __iomem *base;
50 spinlock_t lock;
51
52 /* WDI and WSET bits write access routines */
53 void (*ping)(struct max63xx_wdt *wdt);
54 void (*set)(struct max63xx_wdt *wdt, u8 set);
55 };
56
57 /*
58 * The timeout values used are actually the absolute minimum the chip
59 * offers. Typical values on my board are slightly over twice as long
60 * (10s setting ends up with a 25s timeout), and can be up to 3 times
61 * the nominal setting (according to the datasheet). So please take
62 * these values with a grain of salt. Same goes for the initial delay
63 * "feature". Only max6373/74 have a few settings without this initial
64 * delay (selected with the "nodelay" parameter).
65 *
66 * I also decided to remove from the tables any timeout smaller than a
67 * second, as it looked completly overkill...
68 */
69
70 /* Timeouts in second */
71 struct max63xx_timeout {
72 const u8 wdset;
73 const u8 tdelay;
74 const u8 twd;
75 };
76
77 static const struct max63xx_timeout max6369_table[] = {
78 { 5, 1, 1 },
79 { 6, 10, 10 },
80 { 7, 60, 60 },
81 { },
82 };
83
84 static const struct max63xx_timeout max6371_table[] = {
85 { 6, 60, 3 },
86 { 7, 60, 60 },
87 { },
88 };
89
90 static const struct max63xx_timeout max6373_table[] = {
91 { 2, 60, 1 },
92 { 5, 0, 1 },
93 { 1, 3, 3 },
94 { 7, 60, 10 },
95 { 6, 0, 10 },
96 { },
97 };
98
99 static const struct max63xx_timeout *
max63xx_select_timeout(const struct max63xx_timeout * table,int value)100 max63xx_select_timeout(const struct max63xx_timeout *table, int value)
101 {
102 while (table->twd) {
103 if (value <= table->twd) {
104 if (nodelay && table->tdelay == 0)
105 return table;
106
107 if (!nodelay)
108 return table;
109 }
110
111 table++;
112 }
113
114 return NULL;
115 }
116
max63xx_wdt_ping(struct watchdog_device * wdd)117 static int max63xx_wdt_ping(struct watchdog_device *wdd)
118 {
119 struct max63xx_wdt *wdt = watchdog_get_drvdata(wdd);
120
121 wdt->ping(wdt);
122 return 0;
123 }
124
max63xx_wdt_start(struct watchdog_device * wdd)125 static int max63xx_wdt_start(struct watchdog_device *wdd)
126 {
127 struct max63xx_wdt *wdt = watchdog_get_drvdata(wdd);
128
129 wdt->set(wdt, wdt->timeout->wdset);
130
131 /* check for a edge triggered startup */
132 if (wdt->timeout->tdelay == 0)
133 wdt->ping(wdt);
134 return 0;
135 }
136
max63xx_wdt_stop(struct watchdog_device * wdd)137 static int max63xx_wdt_stop(struct watchdog_device *wdd)
138 {
139 struct max63xx_wdt *wdt = watchdog_get_drvdata(wdd);
140
141 wdt->set(wdt, MAX6369_WDSET_DISABLED);
142 return 0;
143 }
144
145 static const struct watchdog_ops max63xx_wdt_ops = {
146 .owner = THIS_MODULE,
147 .start = max63xx_wdt_start,
148 .stop = max63xx_wdt_stop,
149 .ping = max63xx_wdt_ping,
150 };
151
152 static const struct watchdog_info max63xx_wdt_info = {
153 .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
154 .identity = "max63xx Watchdog",
155 };
156
max63xx_mmap_ping(struct max63xx_wdt * wdt)157 static void max63xx_mmap_ping(struct max63xx_wdt *wdt)
158 {
159 u8 val;
160
161 spin_lock(&wdt->lock);
162
163 val = __raw_readb(wdt->base);
164
165 __raw_writeb(val | MAX6369_WDI, wdt->base);
166 __raw_writeb(val & ~MAX6369_WDI, wdt->base);
167
168 spin_unlock(&wdt->lock);
169 }
170
max63xx_mmap_set(struct max63xx_wdt * wdt,u8 set)171 static void max63xx_mmap_set(struct max63xx_wdt *wdt, u8 set)
172 {
173 u8 val;
174
175 spin_lock(&wdt->lock);
176
177 val = __raw_readb(wdt->base);
178 val &= ~MAX6369_WDSET;
179 val |= set & MAX6369_WDSET;
180 __raw_writeb(val, wdt->base);
181
182 spin_unlock(&wdt->lock);
183 }
184
max63xx_mmap_init(struct platform_device * p,struct max63xx_wdt * wdt)185 static int max63xx_mmap_init(struct platform_device *p, struct max63xx_wdt *wdt)
186 {
187 wdt->base = devm_platform_ioremap_resource(p, 0);
188 if (IS_ERR(wdt->base))
189 return PTR_ERR(wdt->base);
190
191 spin_lock_init(&wdt->lock);
192
193 wdt->ping = max63xx_mmap_ping;
194 wdt->set = max63xx_mmap_set;
195 return 0;
196 }
197
max63xx_wdt_probe(struct platform_device * pdev)198 static int max63xx_wdt_probe(struct platform_device *pdev)
199 {
200 struct device *dev = &pdev->dev;
201 struct max63xx_wdt *wdt;
202 const struct max63xx_timeout *table;
203 int err;
204
205 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
206 if (!wdt)
207 return -ENOMEM;
208
209 /* Attempt to use fwnode first */
210 table = device_get_match_data(dev);
211 if (!table)
212 table = (struct max63xx_timeout *)pdev->id_entry->driver_data;
213
214 if (heartbeat < 1 || heartbeat > MAX_HEARTBEAT)
215 heartbeat = DEFAULT_HEARTBEAT;
216
217 wdt->timeout = max63xx_select_timeout(table, heartbeat);
218 if (!wdt->timeout) {
219 dev_err(dev, "unable to satisfy %ds heartbeat request\n",
220 heartbeat);
221 return -EINVAL;
222 }
223
224 err = max63xx_mmap_init(pdev, wdt);
225 if (err)
226 return err;
227
228 platform_set_drvdata(pdev, &wdt->wdd);
229 watchdog_set_drvdata(&wdt->wdd, wdt);
230
231 wdt->wdd.parent = dev;
232 wdt->wdd.timeout = wdt->timeout->twd;
233 wdt->wdd.info = &max63xx_wdt_info;
234 wdt->wdd.ops = &max63xx_wdt_ops;
235
236 watchdog_set_nowayout(&wdt->wdd, nowayout);
237
238 err = devm_watchdog_register_device(dev, &wdt->wdd);
239 if (err)
240 return err;
241
242 dev_info(dev, "using %ds heartbeat with %ds initial delay\n",
243 wdt->timeout->twd, wdt->timeout->tdelay);
244 return 0;
245 }
246
247 static const struct platform_device_id max63xx_id_table[] = {
248 { .name = "max6369_wdt", .driver_data = (kernel_ulong_t)max6369_table },
249 { .name = "max6370_wdt", .driver_data = (kernel_ulong_t)max6369_table },
250 { .name = "max6371_wdt", .driver_data = (kernel_ulong_t)max6371_table },
251 { .name = "max6372_wdt", .driver_data = (kernel_ulong_t)max6371_table },
252 { .name = "max6373_wdt", .driver_data = (kernel_ulong_t)max6373_table },
253 { .name = "max6374_wdt", .driver_data = (kernel_ulong_t)max6373_table },
254 { }
255 };
256 MODULE_DEVICE_TABLE(platform, max63xx_id_table);
257
258 static const struct of_device_id max63xx_dt_id_table[] = {
259 { .compatible = "maxim,max6369", .data = max6369_table, },
260 { .compatible = "maxim,max6370", .data = max6369_table, },
261 { .compatible = "maxim,max6371", .data = max6371_table, },
262 { .compatible = "maxim,max6372", .data = max6371_table, },
263 { .compatible = "maxim,max6373", .data = max6373_table, },
264 { .compatible = "maxim,max6374", .data = max6373_table, },
265 { }
266 };
267 MODULE_DEVICE_TABLE(of, max63xx_dt_id_table);
268
269 static struct platform_driver max63xx_wdt_driver = {
270 .probe = max63xx_wdt_probe,
271 .id_table = max63xx_id_table,
272 .driver = {
273 .name = "max63xx_wdt",
274 .of_match_table = max63xx_dt_id_table,
275 },
276 };
277
278 module_platform_driver(max63xx_wdt_driver);
279
280 MODULE_AUTHOR("Marc Zyngier <maz@misterjones.org>");
281 MODULE_DESCRIPTION("max63xx Watchdog Driver");
282
283 module_param(heartbeat, int, 0);
284 MODULE_PARM_DESC(heartbeat,
285 "Watchdog heartbeat period in seconds from 1 to "
286 __MODULE_STRING(MAX_HEARTBEAT) ", default "
287 __MODULE_STRING(DEFAULT_HEARTBEAT));
288
289 module_param(nowayout, bool, 0);
290 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
291 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
292
293 module_param(nodelay, int, 0);
294 MODULE_PARM_DESC(nodelay,
295 "Force selection of a timeout setting without initial delay "
296 "(max6373/74 only, default=0)");
297
298 MODULE_LICENSE("GPL v2");
299