[brcm63xx] fix spi chip select configuration
[openwrt.git] / target / linux / brcm63xx / patches-2.6.33 / 020-watchdog.patch
1 --- a/drivers/watchdog/Makefile
2 +++ b/drivers/watchdog/Makefile
3 @@ -112,6 +112,7 @@ obj-$(CONFIG_PNX833X_WDT) += pnx833x_wdt
4  obj-$(CONFIG_SIBYTE_WDOG) += sb_wdog.o
5  obj-$(CONFIG_AR7_WDT) += ar7_wdt.o
6  obj-$(CONFIG_TXX9_WDT) += txx9wdt.o
7 +obj-$(CONFIG_BCM63XX_WDT) += bcm63xx_wdt.o
8  
9  # PARISC Architecture
10  
11 --- a/drivers/watchdog/Kconfig
12 +++ b/drivers/watchdog/Kconfig
13 @@ -840,6 +840,16 @@ config TXX9_WDT
14         help
15           Hardware driver for the built-in watchdog timer on TXx9 MIPS SoCs.
16  
17 +config BCM63XX_WDT
18 +       tristate "Broadcom BCM63xx hardware watchdog"
19 +       depends on BCM63XX
20 +       help
21 +         Watchdog driver for the built in watchdog hardware in Broadcom
22 +         BCM63xx SoC.
23 +
24 +         To compile thi driver as a loadable module, choose M here.
25 +         The module will be called bcm63xx_wdt.
26 +
27  # PARISC Architecture
28  
29  # POWERPC Architecture
30 --- /dev/null
31 +++ b/drivers/watchdog/bcm63xx_wdt.c
32 @@ -0,0 +1,334 @@
33 +/*
34 + *  Broadcom BCM63xx SoC watchdog driver
35 + *
36 + *  Copyright (C) 2007, Miguel Gaio <miguel.gaio@efixo.com>
37 + *  Copyright (C) 2008, Florian Fainelli <florian@openwrt.org>
38 + *
39 + *  This program is free software; you can redistribute it and/or
40 + *  modify it under the terms of the GNU General Public License
41 + *  as published by the Free Software Foundation; either version
42 + *  2 of the License, or (at your option) any later version.
43 + */
44 +
45 +#include <linux/bitops.h>
46 +#include <linux/errno.h>
47 +#include <linux/fs.h>
48 +#include <linux/init.h>
49 +#include <linux/kernel.h>
50 +#include <linux/miscdevice.h>
51 +#include <linux/module.h>
52 +#include <linux/moduleparam.h>
53 +#include <linux/reboot.h>
54 +#include <linux/types.h>
55 +#include <linux/uaccess.h>
56 +#include <linux/watchdog.h>
57 +#include <linux/timer.h>
58 +#include <linux/jiffies.h>
59 +#include <linux/resource.h>
60 +#include <linux/platform_device.h>
61 +
62 +#include <bcm63xx_cpu.h>
63 +#include <bcm63xx_io.h>
64 +#include <bcm63xx_regs.h>
65 +
66 +#define PFX KBUILD_MODNAME
67 +
68 +#define WDT_HZ         50000000 /* Fclk */
69 +#define WDT_DEFAULT_TIME       30      /* seconds */
70 +#define WDT_MAX_TIME           256     /* seconds */
71 +
72 +static struct {
73 +       void __iomem *regs;
74 +       struct timer_list timer;
75 +       int default_ticks;
76 +       unsigned long inuse;
77 +       atomic_t ticks;
78 +} bcm63xx_wdt_device;
79 +
80 +static int expect_close;
81 +static int timeout;
82 +
83 +static int wdt_time = WDT_DEFAULT_TIME;
84 +static int nowayout = WATCHDOG_NOWAYOUT;
85 +module_param(nowayout, int, 0);
86 +MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
87 +       __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
88 +
89 +/* HW functions */
90 +static void bcm63xx_wdt_hw_start(void)
91 +{
92 +       bcm_writel(0xfffffffe, bcm63xx_wdt_device.regs + WDT_DEFVAL_REG);
93 +       bcm_writel(WDT_START_1, bcm63xx_wdt_device.regs + WDT_CTL_REG);
94 +       bcm_writel(WDT_START_2, bcm63xx_wdt_device.regs + WDT_CTL_REG);
95 +}
96 +
97 +static void bcm63xx_wdt_hw_stop(void)
98 +{
99 +       bcm_writel(WDT_STOP_1, bcm63xx_wdt_device.regs + WDT_CTL_REG);
100 +       bcm_writel(WDT_STOP_2, bcm63xx_wdt_device.regs + WDT_CTL_REG);
101 +}
102 +
103 +static void bcm63xx_timer_tick(unsigned long unused)
104 +{
105 +       if (!atomic_dec_and_test(&bcm63xx_wdt_device.ticks)) {
106 +               bcm63xx_wdt_hw_start();
107 +               mod_timer(&bcm63xx_wdt_device.timer, jiffies + HZ);
108 +       } else
109 +               printk(KERN_CRIT PFX ": watchdog will restart system\n");
110 +}
111 +
112 +static void bcm63xx_wdt_pet(void)
113 +{
114 +       atomic_set(&bcm63xx_wdt_device.ticks, wdt_time);
115 +}
116 +
117 +static void bcm63xx_wdt_start(void)
118 +{
119 +       bcm63xx_wdt_pet();
120 +       bcm63xx_timer_tick(0);
121 +}
122 +
123 +static void bcm63xx_wdt_pause(void)
124 +{
125 +       del_timer_sync(&bcm63xx_wdt_device.timer);
126 +       bcm63xx_wdt_hw_stop();
127 +}
128 +
129 +static int bcm63xx_wdt_settimeout(int new_time)
130 +{
131 +       if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
132 +               return -EINVAL;
133 +
134 +       wdt_time = new_time;
135 +
136 +       return 0;
137 +}
138 +
139 +static int bcm63xx_wdt_open(struct inode *inode, struct file *file)
140 +{
141 +       if (test_and_set_bit(0, &bcm63xx_wdt_device.inuse))
142 +               return -EBUSY;
143 +
144 +       bcm63xx_wdt_start();
145 +       return nonseekable_open(inode, file);
146 +}
147 +
148 +static int bcm63xx_wdt_release(struct inode *inode, struct file *file)
149 +{
150 +       if (expect_close == 42)
151 +               bcm63xx_wdt_pause();
152 +       else {
153 +               printk(KERN_CRIT PFX
154 +                       ": Unexpected close, not stopping watchdog!\n");
155 +               bcm63xx_wdt_start();
156 +       }
157 +       clear_bit(0, &bcm63xx_wdt_device.inuse);
158 +       expect_close = 0;
159 +       return 0;
160 +}
161 +
162 +static ssize_t bcm63xx_wdt_write(struct file *file, const char *data,
163 +                               size_t len, loff_t *ppos)
164 +{
165 +       if (len) {
166 +               if (!nowayout) {
167 +                       size_t i;
168 +
169 +                       /* In case it was set long ago */
170 +                       expect_close = 0;
171 +
172 +                       for (i = 0; i != len; i++) {
173 +                               char c;
174 +                               if (get_user(c, data + i))
175 +                                       return -EFAULT;
176 +                               if (c == 'V')
177 +                                       expect_close = 42;
178 +                       }
179 +               }
180 +               bcm63xx_wdt_pet();
181 +       }
182 +       return len;
183 +}
184 +
185 +static struct watchdog_info bcm63xx_wdt_info = {
186 +       .identity       = PFX,
187 +       .options        = WDIOF_SETTIMEOUT |
188 +                               WDIOF_KEEPALIVEPING |
189 +                               WDIOF_MAGICCLOSE,
190 +};
191 +
192 +
193 +static long bcm63xx_wdt_ioctl(struct file *file, unsigned int cmd,
194 +                               unsigned long arg)
195 +{
196 +       void __user *argp = (void __user *)arg;
197 +       int __user *p = argp;
198 +       int new_value, retval = -EINVAL;
199 +
200 +       switch (cmd) {
201 +       case WDIOC_GETSUPPORT:
202 +               return copy_to_user(argp, &bcm63xx_wdt_info,
203 +                       sizeof(bcm63xx_wdt_info)) ? -EFAULT : 0;
204 +
205 +       case WDIOC_GETSTATUS:
206 +       case WDIOC_GETBOOTSTATUS:
207 +               return put_user(0, p);
208 +
209 +       case WDIOC_SETOPTIONS:
210 +               if (get_user(new_value, p))
211 +                       return -EFAULT;
212 +
213 +               if (new_value & WDIOS_DISABLECARD) {
214 +                       bcm63xx_wdt_pause();
215 +                       retval = 0;
216 +               }
217 +               if (new_value & WDIOS_ENABLECARD) {
218 +                       bcm63xx_wdt_start();
219 +                       retval = 0;
220 +               }
221 +
222 +               return retval;
223 +
224 +       case WDIOC_KEEPALIVE:
225 +               bcm63xx_wdt_pet();
226 +               return 0;
227 +
228 +       case WDIOC_SETTIMEOUT:
229 +               if (get_user(new_value, p))
230 +                       return -EFAULT;
231 +
232 +               if (bcm63xx_wdt_settimeout(new_value))
233 +                       return -EINVAL;
234 +
235 +               bcm63xx_wdt_pet();
236 +
237 +       case WDIOC_GETTIMEOUT:
238 +               return put_user(wdt_time, p);
239 +
240 +       default:
241 +               return -ENOTTY;
242 +
243 +       }
244 +}
245 +
246 +static int bcm63xx_wdt_notify_sys(struct notifier_block *this,
247 +                               unsigned long code, void *unused)
248 +{
249 +       if (code == SYS_DOWN || code == SYS_HALT)
250 +               bcm63xx_wdt_pause();
251 +       return NOTIFY_DONE;
252 +}
253 +
254 +static const struct file_operations bcm63xx_wdt_fops = {
255 +       .owner          = THIS_MODULE,
256 +       .llseek         = no_llseek,
257 +       .write          = bcm63xx_wdt_write,
258 +       .unlocked_ioctl = bcm63xx_wdt_ioctl,
259 +       .open           = bcm63xx_wdt_open,
260 +       .release        = bcm63xx_wdt_release,
261 +};
262 +
263 +static struct miscdevice bcm63xx_wdt_miscdev = {
264 +       .minor  = WATCHDOG_MINOR,
265 +       .name   = "watchdog",
266 +       .fops   = &bcm63xx_wdt_fops,
267 +};
268 +
269 +static struct notifier_block bcm63xx_wdt_notifier = {
270 +       .notifier_call = bcm63xx_wdt_notify_sys,
271 +};
272 +
273 +
274 +static int bcm63xx_wdt_probe(struct platform_device *pdev)
275 +{
276 +       int ret;
277 +       struct resource *r;
278 +
279 +       setup_timer(&bcm63xx_wdt_device.timer, bcm63xx_timer_tick, 0L);
280 +
281 +       r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
282 +       if (!r) {
283 +               printk(KERN_ERR PFX
284 +                       "failed to retrieve resources\n");
285 +               return -ENODEV;
286 +       }
287 +
288 +       bcm63xx_wdt_device.regs = ioremap_nocache(r->start, r->end - r->start);
289 +       if (!bcm63xx_wdt_device.regs) {
290 +               printk(KERN_ERR PFX
291 +                       "failed to remap I/O resources\n");
292 +               return -ENXIO;
293 +       }
294 +
295 +       if (bcm63xx_wdt_settimeout(wdt_time)) {
296 +               bcm63xx_wdt_settimeout(WDT_DEFAULT_TIME);
297 +               printk(KERN_INFO PFX
298 +                       ": wdt_time value must be 1 <= wdt_time <= 256, using %d\n",
299 +                       wdt_time);
300 +       }
301 +
302 +       ret = register_reboot_notifier(&bcm63xx_wdt_notifier);
303 +       if (ret) {
304 +               printk(KERN_ERR PFX
305 +                       "failed to register reboot_notifier\n");
306 +               return ret;
307 +       }
308 +
309 +       ret = misc_register(&bcm63xx_wdt_miscdev);
310 +       if (ret < 0) {
311 +               printk(KERN_ERR PFX
312 +                       "failed to register watchdog device\n");
313 +               goto unmap;
314 +       }
315 +
316 +       printk(KERN_INFO PFX " started, timer margin: %d sec\n", WDT_DEFAULT_TIME);
317 +
318 +       return 0;
319 +
320 +unmap:
321 +       unregister_reboot_notifier(&bcm63xx_wdt_notifier);
322 +       iounmap(bcm63xx_wdt_device.regs);
323 +       return ret;
324 +}
325 +
326 +static int bcm63xx_wdt_remove(struct platform_device *pdev)
327 +{
328 +       if (!nowayout)
329 +               bcm63xx_wdt_pause();
330 +
331 +       misc_deregister(&bcm63xx_wdt_miscdev);
332 +
333 +       iounmap(bcm63xx_wdt_device.regs);
334 +
335 +       unregister_reboot_notifier(&bcm63xx_wdt_notifier);
336 +
337 +       return 0;
338 +}
339 +
340 +static struct platform_driver bcm63xx_wdt = {
341 +       .probe  = bcm63xx_wdt_probe,
342 +       .remove = bcm63xx_wdt_remove,
343 +       .driver = {
344 +               .name = "bcm63xx-wdt",
345 +       }
346 +};
347 +
348 +static int __init bcm63xx_wdt_init(void)
349 +{
350 +       return platform_driver_register(&bcm63xx_wdt);
351 +}
352 +
353 +static void __exit bcm63xx_wdt_exit(void)
354 +{
355 +       platform_driver_unregister(&bcm63xx_wdt);
356 +}
357 +
358 +module_init(bcm63xx_wdt_init);
359 +module_exit(bcm63xx_wdt_exit);
360 +
361 +MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
362 +MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
363 +MODULE_DESCRIPTION("Driver for the Broadcom BCM63xx SoC watchdog");
364 +MODULE_LICENSE("GPL");
365 +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
366 +MODULE_ALIAS("platform:bcm63xx-wdt");