cns21xx: add support for 3.2
[openwrt/svn-archive/archive.git] / target / linux / cns21xx / patches-3.2 / 006-arm-add-fa-watchdog-driver.patch
1 --- a/drivers/watchdog/Kconfig
2 +++ b/drivers/watchdog/Kconfig
3 @@ -343,6 +343,13 @@ config IMX2_WDT
4 To compile this driver as a module, choose M here: the
5 module will be called imx2_wdt.
6
7 +config FA_WATCHDOG
8 + tristate "Faraday watchdog"
9 + depends on ARCH_GEMINI
10 + help
11 + Say Y here if you want support for the built-in watchdog timer
12 + found in some Faraday FA526 based SoCs.
13 +
14 # AVR32 Architecture
15
16 config AT32AP700X_WDT
17 --- a/drivers/watchdog/Makefile
18 +++ b/drivers/watchdog/Makefile
19 @@ -53,6 +53,7 @@ obj-$(CONFIG_STMP3XXX_WATCHDOG) += stmp3
20 obj-$(CONFIG_NUC900_WATCHDOG) += nuc900_wdt.o
21 obj-$(CONFIG_TS72XX_WATCHDOG) += ts72xx_wdt.o
22 obj-$(CONFIG_IMX2_WDT) += imx2_wdt.o
23 +obj-$(CONFIG_FA_WATCHDOG) += fa_wdt.o
24
25 # AVR32 Architecture
26 obj-$(CONFIG_AT32AP700X_WDT) += at32ap700x_wdt.o
27 --- /dev/null
28 +++ b/drivers/watchdog/fa_wdt.c
29 @@ -0,0 +1,413 @@
30 +/*
31 + * Watchdog driver for SoCs based on the Faraday FA526 core
32 + *
33 + * Copyright (C) 2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
34 + * Copyright (C) 2010-2012 Gabor Juhos <juhosg@openwrt.org>
35 + *
36 + * This program is free software; you can redistribute it and/or modify
37 + * it under the terms of the GNU General Public License version 2 as
38 + * published by the Free Software Foundation.
39 + */
40 +
41 +#include <linux/kernel.h>
42 +#include <linux/module.h>
43 +#include <linux/init.h>
44 +#include <linux/io.h>
45 +#include <linux/fs.h>
46 +#include <linux/notifier.h>
47 +#include <linux/reboot.h>
48 +#include <linux/uaccess.h>
49 +#include <linux/miscdevice.h>
50 +#include <linux/platform_device.h>
51 +#include <linux/watchdog.h>
52 +#include <linux/slab.h>
53 +#include <linux/fa_wdt.h>
54 +
55 +#define FA_WDCOUNTER 0x0
56 +#define FA_WDLOAD 0x4
57 +#define FA_WDRESTART 0x8
58 +
59 +#define WDRESTART_MAGIC 0x5AB9
60 +
61 +#define FA_WDCR 0xC
62 +
63 +#define WDCR_CLOCK_5MHZ (1 << 4)
64 +#define WDCR_SYS_RST (1 << 1)
65 +#define WDCR_ENABLE (1 << 0)
66 +
67 +#define WDT_DEFAULT_TIMEOUT 13
68 +
69 +/* status bits */
70 +#define WDT_ACTIVE 0
71 +#define WDT_OK_TO_CLOSE 1
72 +
73 +static unsigned int timeout = WDT_DEFAULT_TIMEOUT;
74 +static int nowayout = WATCHDOG_NOWAYOUT;
75 +
76 +static DEFINE_SPINLOCK(fa_wdt_lock);
77 +
78 +static struct platform_device *fa_wdt_dev;
79 +
80 +struct fa_wdt_struct {
81 + struct resource *res;
82 + struct device *dev;
83 + void __iomem *base;
84 + unsigned long status;
85 + unsigned int clock;
86 + unsigned int max_timeout;
87 +};
88 +
89 +static const struct watchdog_info fa_wdt_info = {
90 + .identity = "Faraday watchdog",
91 + .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING |
92 + WDIOF_SETTIMEOUT,
93 +};
94 +
95 +/* Disable the watchdog. */
96 +static void fa_wdt_stop(struct fa_wdt_struct *fa_wdt)
97 +{
98 + spin_lock(&fa_wdt_lock);
99 +
100 + __raw_writel(0, fa_wdt->base + FA_WDCR);
101 +
102 + clear_bit(WDT_ACTIVE, &fa_wdt->status);
103 +
104 + spin_unlock(&fa_wdt_lock);
105 +}
106 +
107 +/* Service the watchdog */
108 +static void fa_wdt_service(struct fa_wdt_struct *fa_wdt)
109 +{
110 + __raw_writel(WDRESTART_MAGIC, fa_wdt->base + FA_WDRESTART);
111 +}
112 +
113 +/* Enable and reset the watchdog. */
114 +static void fa_wdt_start(struct fa_wdt_struct *fa_wdt)
115 +{
116 + spin_lock(&fa_wdt_lock);
117 +
118 + __raw_writel(timeout * fa_wdt->clock,
119 + fa_wdt->base + FA_WDLOAD);
120 +
121 + fa_wdt_service(fa_wdt);
122 +
123 + /* set clock before enabling */
124 + __raw_writel(WDCR_CLOCK_5MHZ | WDCR_SYS_RST,
125 + fa_wdt->base + FA_WDCR);
126 +
127 + __raw_writel(WDCR_CLOCK_5MHZ | WDCR_SYS_RST | WDCR_ENABLE,
128 + fa_wdt->base + FA_WDCR);
129 +
130 + set_bit(WDT_ACTIVE, &fa_wdt->status);
131 +
132 + spin_unlock(&fa_wdt_lock);
133 +}
134 +
135 +/* Watchdog device is opened, and watchdog starts running. */
136 +static int fa_wdt_open(struct inode *inode, struct file *file)
137 +{
138 + struct fa_wdt_struct *fa_wdt = platform_get_drvdata(fa_wdt_dev);
139 +
140 + if (test_bit(WDT_ACTIVE, &fa_wdt->status))
141 + return -EBUSY;
142 +
143 + file->private_data = fa_wdt;
144 +
145 + fa_wdt_start(fa_wdt);
146 +
147 + return nonseekable_open(inode, file);
148 +}
149 +
150 +/* Close the watchdog device. */
151 +static int fa_wdt_close(struct inode *inode, struct file *file)
152 +{
153 + struct fa_wdt_struct *fa_wdt = file->private_data;
154 +
155 + /* Disable the watchdog if possible */
156 + if (test_bit(WDT_OK_TO_CLOSE, &fa_wdt->status))
157 + fa_wdt_stop(fa_wdt);
158 + else
159 + dev_warn(fa_wdt->dev, "Device closed unexpectedly - timer will not stop\n");
160 +
161 + return 0;
162 +}
163 +
164 +/* Handle commands from user-space. */
165 +static long fa_wdt_ioctl(struct file *file, unsigned int cmd,
166 + unsigned long arg)
167 +{
168 + struct fa_wdt_struct *fa_wdt = file->private_data;
169 +
170 + int value;
171 +
172 + switch (cmd) {
173 + case WDIOC_KEEPALIVE:
174 + fa_wdt_service(fa_wdt);
175 + return 0;
176 +
177 + case WDIOC_GETSUPPORT:
178 + return copy_to_user((struct watchdog_info *)arg, &fa_wdt_info,
179 + sizeof(fa_wdt_info)) ? -EFAULT : 0;
180 +
181 + case WDIOC_SETTIMEOUT:
182 + if (get_user(value, (int *)arg))
183 + return -EFAULT;
184 +
185 + if ((value < 1) || (value > fa_wdt->max_timeout))
186 + return -EINVAL;
187 +
188 + timeout = value;
189 +
190 + /* restart wdt to use new timeout */
191 + fa_wdt_stop(fa_wdt);
192 + fa_wdt_start(fa_wdt);
193 +
194 + /* Fall through */
195 + case WDIOC_GETTIMEOUT:
196 + return put_user(timeout, (int *)arg);
197 +
198 + case WDIOC_GETTIMELEFT:
199 + value = __raw_readl(fa_wdt->base + FA_WDCOUNTER);
200 + return put_user(value / fa_wdt->clock, (int *)arg);
201 +
202 + default:
203 + return -ENOTTY;
204 + }
205 +}
206 +
207 +/* Refresh the watchdog whenever device is written to. */
208 +static ssize_t fa_wdt_write(struct file *file, const char *data,
209 + size_t len, loff_t *ppos)
210 +{
211 + struct fa_wdt_struct *fa_wdt = file->private_data;
212 +
213 + if (len) {
214 + if (!nowayout) {
215 + size_t i;
216 +
217 + clear_bit(WDT_OK_TO_CLOSE, &fa_wdt->status);
218 + for (i = 0; i != len; i++) {
219 + char c;
220 +
221 + if (get_user(c, data + i))
222 + return -EFAULT;
223 + if (c == 'V')
224 + set_bit(WDT_OK_TO_CLOSE,
225 + &fa_wdt->status);
226 + }
227 + }
228 + fa_wdt_service(fa_wdt);
229 + }
230 +
231 + return len;
232 +}
233 +
234 +static int fa_wdt_notify_sys(struct notifier_block *this,
235 + unsigned long code, void *unused)
236 +{
237 + struct fa_wdt_struct *fa_wdt = platform_get_drvdata(fa_wdt_dev);
238 +
239 + if (code == SYS_DOWN || code == SYS_HALT)
240 + fa_wdt_stop(fa_wdt);
241 +
242 + return NOTIFY_DONE;
243 +}
244 +
245 +static struct notifier_block fa_wdt_notifier = {
246 + .notifier_call = fa_wdt_notify_sys,
247 +};
248 +
249 +static const struct file_operations fa_wdt_fops = {
250 + .owner = THIS_MODULE,
251 + .llseek = no_llseek,
252 + .unlocked_ioctl = fa_wdt_ioctl,
253 + .open = fa_wdt_open,
254 + .release = fa_wdt_close,
255 + .write = fa_wdt_write,
256 +};
257 +
258 +static struct miscdevice fa_wdt_miscdev = {
259 + .minor = WATCHDOG_MINOR,
260 + .name = "watchdog",
261 + .fops = &fa_wdt_fops,
262 +};
263 +
264 +static void fa_wdt_shutdown(struct platform_device *pdev)
265 +{
266 + struct fa_wdt_struct *fa_wdt = platform_get_drvdata(pdev);
267 +
268 + fa_wdt_stop(fa_wdt);
269 +}
270 +
271 +static int __devinit fa_wdt_probe(struct platform_device *pdev)
272 +{
273 + int ret;
274 + int res_size;
275 + struct resource *res;
276 + void __iomem *base;
277 + struct fa_wdt_struct *fa_wdt;
278 + struct fa_wdt_platform_data *pdata;
279 +
280 + pdata = pdev->dev.platform_data;
281 + if (!pdata) {
282 + dev_err(&pdev->dev, "no platform data specified\n");
283 + return -EINVAL;
284 + }
285 +
286 + if (!pdata->clock) {
287 + dev_err(&pdev->dev, "invalid clock value\n");
288 + return -EINVAL;
289 + }
290 +
291 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
292 + if (!res) {
293 + dev_err(&pdev->dev, "can't get device resources\n");
294 + return -ENODEV;
295 + }
296 +
297 + res_size = resource_size(res);
298 + if (!request_mem_region(res->start, res_size, res->name)) {
299 + dev_err(&pdev->dev, "can't allocate %d bytes at %d address\n",
300 + res_size, res->start);
301 + return -ENOMEM;
302 + }
303 +
304 + base = ioremap(res->start, res_size);
305 + if (!base) {
306 + dev_err(&pdev->dev, "ioremap failed\n");
307 + ret = -EIO;
308 + goto fail0;
309 + }
310 +
311 + fa_wdt = kzalloc(sizeof(struct fa_wdt_struct), GFP_KERNEL);
312 + if (!fa_wdt) {
313 + dev_err(&pdev->dev, "can't allocate interface\n");
314 + ret = -ENOMEM;
315 + goto fail1;
316 + }
317 +
318 + /* Setup fa_wdt driver structure */
319 + fa_wdt->base = base;
320 + fa_wdt->res = res;
321 + fa_wdt->dev = &pdev->dev;
322 + fa_wdt->clock = pdata->clock;
323 + fa_wdt->max_timeout = 0xffffffffU / pdata->clock;
324 +
325 + /* Set up platform driver data */
326 + platform_set_drvdata(pdev, fa_wdt);
327 + fa_wdt_dev = pdev;
328 +
329 + if (fa_wdt_miscdev.parent) {
330 + ret = -EBUSY;
331 + goto fail2;
332 + }
333 +
334 + ret = register_reboot_notifier(&fa_wdt_notifier);
335 + if (ret)
336 + goto fail2;
337 +
338 + fa_wdt_miscdev.parent = &pdev->dev;
339 +
340 + ret = misc_register(&fa_wdt_miscdev);
341 + if (ret)
342 + goto fail3;
343 +
344 + return 0;
345 +
346 +fail3:
347 + unregister_reboot_notifier(&fa_wdt_notifier);
348 +fail2:
349 + platform_set_drvdata(pdev, NULL);
350 + kfree(fa_wdt);
351 +fail1:
352 + iounmap(base);
353 +fail0:
354 + release_mem_region(res->start, res_size);
355 +
356 + return ret;
357 +}
358 +
359 +static int __devexit fa_wdt_remove(struct platform_device *pdev)
360 +{
361 + struct fa_wdt_struct *fa_wdt = platform_get_drvdata(pdev);
362 +
363 + platform_set_drvdata(pdev, NULL);
364 + misc_deregister(&fa_wdt_miscdev);
365 + unregister_reboot_notifier(&fa_wdt_notifier);
366 + fa_wdt_dev = NULL;
367 + iounmap(fa_wdt->base);
368 + release_mem_region(fa_wdt->res->start, resource_size(fa_wdt->res));
369 +
370 + kfree(fa_wdt);
371 +
372 + return 0;
373 +}
374 +
375 +#ifdef CONFIG_PM
376 +static int fa_wdt_suspend(struct platform_device *pdev, pm_message_t message)
377 +{
378 + struct fa_wdt_struct *fa_wdt = platform_get_drvdata(pdev);
379 + unsigned int reg;
380 +
381 + reg = __raw_readw(fa_wdt->base + FA_WDCR);
382 + reg &= ~(WDCR_WDENABLE);
383 + __raw_writel(reg, fa_wdt->base + FA_WDCR);
384 +
385 + return 0;
386 +}
387 +
388 +static int fa_wdt_resume(struct platform_device *pdev)
389 +{
390 + struct fa_wdt_struct *fa_wdt = platform_get_drvdata(pdev);
391 + unsigned int reg;
392 +
393 + if (fa_wdt->status) {
394 + reg = __raw_readw(fa_wdt->base + FA_WDCR);
395 + reg |= WDCR_WDENABLE;
396 + __raw_writel(reg, fa_wdt->base + FA_WDCR);
397 + }
398 +
399 + return 0;
400 +}
401 +#else
402 +#define fa_wdt_suspend NULL
403 +#define fa_wdt_resume NULL
404 +#endif
405 +
406 +static struct platform_driver fa_wdt_driver = {
407 + .probe = fa_wdt_probe,
408 + .remove = __devexit_p(fa_wdt_remove),
409 + .shutdown = fa_wdt_shutdown,
410 + .suspend = fa_wdt_suspend,
411 + .resume = fa_wdt_resume,
412 + .driver = {
413 + .name = "fa-wdt",
414 + .owner = THIS_MODULE,
415 + },
416 +};
417 +
418 +static int __init fa_wdt_init(void)
419 +{
420 + return platform_driver_probe(&fa_wdt_driver, fa_wdt_probe);
421 +}
422 +
423 +static void __exit fa_wdt_exit(void)
424 +{
425 + platform_driver_unregister(&fa_wdt_driver);
426 +}
427 +
428 +module_init(fa_wdt_init);
429 +module_exit(fa_wdt_exit);
430 +
431 +module_param(timeout, uint, 0);
432 +MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
433 +
434 +module_param(nowayout, int, 0);
435 +MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
436 +
437 +MODULE_AUTHOR("Paulius Zaleckas");
438 +MODULE_AUTHOR("Gabor Juhos");
439 +MODULE_DESCRIPTION("Watchdog driver for Faraday FA526 based SoCs");
440 +MODULE_LICENSE("GPL v2");
441 +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
442 +MODULE_ALIAS("platform:fa-wdt");
443 --- /dev/null
444 +++ b/include/linux/fa_wdt.h
445 @@ -0,0 +1,13 @@
446 +/*
447 + * Platform data definition for the Faraday watchdog driver
448 + */
449 +
450 +#ifndef _FA_WDT_H
451 +#define _FA_WDT_H
452 +
453 +struct fa_wdt_platform_data {
454 + unsigned int clock;
455 +};
456 +
457 +#endif /* _FA_WDT_H */
458 +