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