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