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