kernel: refresh patches
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-3.14 / 0004-bcm2708-watchdog-driver.patch
1 From 8711c9547fe7196dcd3adf59265968bc1fc16aad Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Wed, 1 May 2013 19:54:32 +0100
4 Subject: [PATCH 04/54] bcm2708 watchdog driver
5
6 Signed-off-by: popcornmix <popcornmix@gmail.com>
7 ---
8 drivers/watchdog/Kconfig | 6 +
9 drivers/watchdog/Makefile | 1 +
10 drivers/watchdog/bcm2708_wdog.c | 384 ++++++++++++++++++++++++++++++++++++++++
11 3 files changed, 391 insertions(+)
12 create mode 100644 drivers/watchdog/bcm2708_wdog.c
13
14 --- a/drivers/watchdog/Kconfig
15 +++ b/drivers/watchdog/Kconfig
16 @@ -402,6 +402,12 @@ config RETU_WATCHDOG
17 To compile this driver as a module, choose M here: the
18 module will be called retu_wdt.
19
20 +config BCM2708_WDT
21 + tristate "BCM2708 Watchdog"
22 + depends on ARCH_BCM2708
23 + help
24 + Enables BCM2708 watchdog support.
25 +
26 config MOXART_WDT
27 tristate "MOXART watchdog"
28 depends on ARCH_MOXART
29 --- a/drivers/watchdog/Makefile
30 +++ b/drivers/watchdog/Makefile
31 @@ -54,6 +54,7 @@ obj-$(CONFIG_TS72XX_WATCHDOG) += ts72xx_
32 obj-$(CONFIG_IMX2_WDT) += imx2_wdt.o
33 obj-$(CONFIG_UX500_WATCHDOG) += ux500_wdt.o
34 obj-$(CONFIG_RETU_WATCHDOG) += retu_wdt.o
35 +obj-$(CONFIG_BCM2708_WDT) += bcm2708_wdog.o
36 obj-$(CONFIG_BCM2835_WDT) += bcm2835_wdt.o
37 obj-$(CONFIG_MOXART_WDT) += moxart_wdt.o
38 obj-$(CONFIG_SIRFSOC_WATCHDOG) += sirfsoc_wdt.o
39 --- /dev/null
40 +++ b/drivers/watchdog/bcm2708_wdog.c
41 @@ -0,0 +1,384 @@
42 +/*
43 + * Broadcom BCM2708 watchdog driver.
44 + *
45 + * (c) Copyright 2010 Broadcom Europe Ltd
46 + *
47 + * This program is free software; you can redistribute it and/or
48 + * modify it under the terms of the GNU General Public License
49 + * as published by the Free Software Foundation; either version
50 + * 2 of the License, or (at your option) any later version.
51 + *
52 + * BCM2708 watchdog driver. Loosely based on wdt driver.
53 + */
54 +
55 +#include <linux/interrupt.h>
56 +#include <linux/module.h>
57 +#include <linux/moduleparam.h>
58 +#include <linux/types.h>
59 +#include <linux/miscdevice.h>
60 +#include <linux/watchdog.h>
61 +#include <linux/fs.h>
62 +#include <linux/ioport.h>
63 +#include <linux/notifier.h>
64 +#include <linux/reboot.h>
65 +#include <linux/init.h>
66 +#include <linux/io.h>
67 +#include <linux/uaccess.h>
68 +#include <mach/platform.h>
69 +
70 +#include <asm/system.h>
71 +
72 +#define SECS_TO_WDOG_TICKS(x) ((x) << 16)
73 +#define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
74 +
75 +static unsigned long wdog_is_open;
76 +static uint32_t wdog_ticks; /* Ticks to load into wdog timer */
77 +static char expect_close;
78 +
79 +/*
80 + * Module parameters
81 + */
82 +
83 +#define WD_TIMO 10 /* Default heartbeat = 60 seconds */
84 +static int heartbeat = WD_TIMO; /* Heartbeat in seconds */
85 +
86 +module_param(heartbeat, int, 0);
87 +MODULE_PARM_DESC(heartbeat,
88 + "Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default="
89 + __MODULE_STRING(WD_TIMO) ")");
90 +
91 +static int nowayout = WATCHDOG_NOWAYOUT;
92 +module_param(nowayout, int, 0);
93 +MODULE_PARM_DESC(nowayout,
94 + "Watchdog cannot be stopped once started (default="
95 + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
96 +
97 +static DEFINE_SPINLOCK(wdog_lock);
98 +
99 +/**
100 + * Start the watchdog driver.
101 + */
102 +
103 +static int wdog_start(unsigned long timeout)
104 +{
105 + uint32_t cur;
106 + unsigned long flags;
107 + spin_lock_irqsave(&wdog_lock, flags);
108 +
109 + /* enable the watchdog */
110 + iowrite32(PM_PASSWORD | (timeout & PM_WDOG_TIME_SET),
111 + __io_address(PM_WDOG));
112 + cur = ioread32(__io_address(PM_RSTC));
113 + iowrite32(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
114 + PM_RSTC_WRCFG_FULL_RESET, __io_address(PM_RSTC));
115 +
116 + spin_unlock_irqrestore(&wdog_lock, flags);
117 + return 0;
118 +}
119 +
120 +/**
121 + * Stop the watchdog driver.
122 + */
123 +
124 +static int wdog_stop(void)
125 +{
126 + iowrite32(PM_PASSWORD | PM_RSTC_RESET, __io_address(PM_RSTC));
127 + printk(KERN_INFO "watchdog stopped\n");
128 + return 0;
129 +}
130 +
131 +/**
132 + * Reload counter one with the watchdog heartbeat. We don't bother
133 + * reloading the cascade counter.
134 + */
135 +
136 +static void wdog_ping(void)
137 +{
138 + wdog_start(wdog_ticks);
139 +}
140 +
141 +/**
142 + * @t: the new heartbeat value that needs to be set.
143 + *
144 + * Set a new heartbeat value for the watchdog device. If the heartbeat
145 + * value is incorrect we keep the old value and return -EINVAL. If
146 + * successful we return 0.
147 + */
148 +
149 +static int wdog_set_heartbeat(int t)
150 +{
151 + if (t < 1 || t > WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET))
152 + return -EINVAL;
153 +
154 + heartbeat = t;
155 + wdog_ticks = SECS_TO_WDOG_TICKS(t);
156 + return 0;
157 +}
158 +
159 +/**
160 + * @file: file handle to the watchdog
161 + * @buf: buffer to write (unused as data does not matter here
162 + * @count: count of bytes
163 + * @ppos: pointer to the position to write. No seeks allowed
164 + *
165 + * A write to a watchdog device is defined as a keepalive signal.
166 + *
167 + * if 'nowayout' is set then normally a close() is ignored. But
168 + * if you write 'V' first then the close() will stop the timer.
169 + */
170 +
171 +static ssize_t wdog_write(struct file *file, const char __user *buf,
172 + size_t count, loff_t *ppos)
173 +{
174 + if (count) {
175 + if (!nowayout) {
176 + size_t i;
177 +
178 + /* In case it was set long ago */
179 + expect_close = 0;
180 +
181 + for (i = 0; i != count; i++) {
182 + char c;
183 + if (get_user(c, buf + i))
184 + return -EFAULT;
185 + if (c == 'V')
186 + expect_close = 42;
187 + }
188 + }
189 + wdog_ping();
190 + }
191 + return count;
192 +}
193 +
194 +static int wdog_get_status(void)
195 +{
196 + unsigned long flags;
197 + int status = 0;
198 + spin_lock_irqsave(&wdog_lock, flags);
199 + /* FIXME: readback reset reason */
200 + spin_unlock_irqrestore(&wdog_lock, flags);
201 + return status;
202 +}
203 +
204 +static uint32_t wdog_get_remaining(void)
205 +{
206 + uint32_t ret = ioread32(__io_address(PM_WDOG));
207 + return ret & PM_WDOG_TIME_SET;
208 +}
209 +
210 +/**
211 + * @file: file handle to the device
212 + * @cmd: watchdog command
213 + * @arg: argument pointer
214 + *
215 + * The watchdog API defines a common set of functions for all watchdogs
216 + * according to their available features. We only actually usefully support
217 + * querying capabilities and current status.
218 + */
219 +
220 +static long wdog_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
221 +{
222 + void __user *argp = (void __user *)arg;
223 + int __user *p = argp;
224 + int new_heartbeat;
225 + int status;
226 + int options;
227 + uint32_t remaining;
228 +
229 + struct watchdog_info ident = {
230 + .options = WDIOF_SETTIMEOUT|
231 + WDIOF_MAGICCLOSE|
232 + WDIOF_KEEPALIVEPING,
233 + .firmware_version = 1,
234 + .identity = "BCM2708",
235 + };
236 +
237 + switch (cmd) {
238 + case WDIOC_GETSUPPORT:
239 + return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
240 + case WDIOC_GETSTATUS:
241 + status = wdog_get_status();
242 + return put_user(status, p);
243 + case WDIOC_GETBOOTSTATUS:
244 + return put_user(0, p);
245 + case WDIOC_KEEPALIVE:
246 + wdog_ping();
247 + return 0;
248 + case WDIOC_SETTIMEOUT:
249 + if (get_user(new_heartbeat, p))
250 + return -EFAULT;
251 + if (wdog_set_heartbeat(new_heartbeat))
252 + return -EINVAL;
253 + wdog_ping();
254 + /* Fall */
255 + case WDIOC_GETTIMEOUT:
256 + return put_user(heartbeat, p);
257 + case WDIOC_GETTIMELEFT:
258 + remaining = WDOG_TICKS_TO_SECS(wdog_get_remaining());
259 + return put_user(remaining, p);
260 + case WDIOC_SETOPTIONS:
261 + if (get_user(options, p))
262 + return -EFAULT;
263 + if (options & WDIOS_DISABLECARD)
264 + wdog_stop();
265 + if (options & WDIOS_ENABLECARD)
266 + wdog_start(wdog_ticks);
267 + return 0;
268 + default:
269 + return -ENOTTY;
270 + }
271 +}
272 +
273 +/**
274 + * @inode: inode of device
275 + * @file: file handle to device
276 + *
277 + * The watchdog device has been opened. The watchdog device is single
278 + * open and on opening we load the counters.
279 + */
280 +
281 +static int wdog_open(struct inode *inode, struct file *file)
282 +{
283 + if (test_and_set_bit(0, &wdog_is_open))
284 + return -EBUSY;
285 + /*
286 + * Activate
287 + */
288 + wdog_start(wdog_ticks);
289 + return nonseekable_open(inode, file);
290 +}
291 +
292 +/**
293 + * @inode: inode to board
294 + * @file: file handle to board
295 + *
296 + * The watchdog has a configurable API. There is a religious dispute
297 + * between people who want their watchdog to be able to shut down and
298 + * those who want to be sure if the watchdog manager dies the machine
299 + * reboots. In the former case we disable the counters, in the latter
300 + * case you have to open it again very soon.
301 + */
302 +
303 +static int wdog_release(struct inode *inode, struct file *file)
304 +{
305 + if (expect_close == 42) {
306 + wdog_stop();
307 + } else {
308 + printk(KERN_CRIT
309 + "wdt: WDT device closed unexpectedly. WDT will not stop!\n");
310 + wdog_ping();
311 + }
312 + clear_bit(0, &wdog_is_open);
313 + expect_close = 0;
314 + return 0;
315 +}
316 +
317 +/**
318 + * @this: our notifier block
319 + * @code: the event being reported
320 + * @unused: unused
321 + *
322 + * Our notifier is called on system shutdowns. Turn the watchdog
323 + * off so that it does not fire during the next reboot.
324 + */
325 +
326 +static int wdog_notify_sys(struct notifier_block *this, unsigned long code,
327 + void *unused)
328 +{
329 + if (code == SYS_DOWN || code == SYS_HALT)
330 + wdog_stop();
331 + return NOTIFY_DONE;
332 +}
333 +
334 +/*
335 + * Kernel Interfaces
336 + */
337 +
338 +
339 +static const struct file_operations wdog_fops = {
340 + .owner = THIS_MODULE,
341 + .llseek = no_llseek,
342 + .write = wdog_write,
343 + .unlocked_ioctl = wdog_ioctl,
344 + .open = wdog_open,
345 + .release = wdog_release,
346 +};
347 +
348 +static struct miscdevice wdog_miscdev = {
349 + .minor = WATCHDOG_MINOR,
350 + .name = "watchdog",
351 + .fops = &wdog_fops,
352 +};
353 +
354 +/*
355 + * The WDT card needs to learn about soft shutdowns in order to
356 + * turn the timebomb registers off.
357 + */
358 +
359 +static struct notifier_block wdog_notifier = {
360 + .notifier_call = wdog_notify_sys,
361 +};
362 +
363 +/**
364 + * cleanup_module:
365 + *
366 + * Unload the watchdog. You cannot do this with any file handles open.
367 + * If your watchdog is set to continue ticking on close and you unload
368 + * it, well it keeps ticking. We won't get the interrupt but the board
369 + * will not touch PC memory so all is fine. You just have to load a new
370 + * module in 60 seconds or reboot.
371 + */
372 +
373 +static void __exit wdog_exit(void)
374 +{
375 + misc_deregister(&wdog_miscdev);
376 + unregister_reboot_notifier(&wdog_notifier);
377 +}
378 +
379 +static int __init wdog_init(void)
380 +{
381 + int ret;
382 +
383 + /* Check that the heartbeat value is within it's range;
384 + if not reset to the default */
385 + if (wdog_set_heartbeat(heartbeat)) {
386 + wdog_set_heartbeat(WD_TIMO);
387 + printk(KERN_INFO "bcm2708_wdog: heartbeat value must be "
388 + "0 < heartbeat < %d, using %d\n",
389 + WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
390 + WD_TIMO);
391 + }
392 +
393 + ret = register_reboot_notifier(&wdog_notifier);
394 + if (ret) {
395 + printk(KERN_ERR
396 + "wdt: cannot register reboot notifier (err=%d)\n", ret);
397 + goto out_reboot;
398 + }
399 +
400 + ret = misc_register(&wdog_miscdev);
401 + if (ret) {
402 + printk(KERN_ERR
403 + "wdt: cannot register miscdev on minor=%d (err=%d)\n",
404 + WATCHDOG_MINOR, ret);
405 + goto out_misc;
406 + }
407 +
408 + printk(KERN_INFO "bcm2708 watchdog, heartbeat=%d sec (nowayout=%d)\n",
409 + heartbeat, nowayout);
410 + return 0;
411 +
412 +out_misc:
413 + unregister_reboot_notifier(&wdog_notifier);
414 +out_reboot:
415 + return ret;
416 +}
417 +
418 +module_init(wdog_init);
419 +module_exit(wdog_exit);
420 +
421 +MODULE_AUTHOR("Luke Diamand");
422 +MODULE_DESCRIPTION("Driver for BCM2708 watchdog");
423 +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
424 +MODULE_ALIAS_MISCDEV(TEMP_MINOR);
425 +MODULE_LICENSE("GPL");