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.
8 + tristate "Faraday watchdog"
9 + depends on ARCH_GEMINI
11 + Say Y here if you want support for the built-in watchdog timer
12 + found in some Faraday FA526 based SoCs.
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
26 obj-$(CONFIG_AT32AP700X_WDT) += at32ap700x_wdt.o
28 +++ b/drivers/watchdog/fa_wdt.c
31 + * Watchdog driver for SoCs based on the Faraday FA526 core
33 + * Copyright (C) 2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
34 + * Copyright (C) 2010-2012 Gabor Juhos <juhosg@openwrt.org>
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.
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>
55 +#define FA_WDCOUNTER 0x0
56 +#define FA_WDLOAD 0x4
57 +#define FA_WDRESTART 0x8
59 +#define WDRESTART_MAGIC 0x5AB9
63 +#define WDCR_CLOCK_5MHZ (1 << 4)
64 +#define WDCR_SYS_RST (1 << 1)
65 +#define WDCR_ENABLE (1 << 0)
67 +#define WDT_DEFAULT_TIMEOUT 13
71 +#define WDT_OK_TO_CLOSE 1
73 +static unsigned int timeout = WDT_DEFAULT_TIMEOUT;
74 +static int nowayout = WATCHDOG_NOWAYOUT;
76 +static DEFINE_SPINLOCK(fa_wdt_lock);
78 +static struct platform_device *fa_wdt_dev;
80 +struct fa_wdt_struct {
81 + struct resource *res;
84 + unsigned long status;
86 + unsigned int max_timeout;
89 +static const struct watchdog_info fa_wdt_info = {
90 + .identity = "Faraday watchdog",
91 + .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING |
95 +/* Disable the watchdog. */
96 +static void fa_wdt_stop(struct fa_wdt_struct *fa_wdt)
98 + spin_lock(&fa_wdt_lock);
100 + __raw_writel(0, fa_wdt->base + FA_WDCR);
102 + clear_bit(WDT_ACTIVE, &fa_wdt->status);
104 + spin_unlock(&fa_wdt_lock);
107 +/* Service the watchdog */
108 +static void fa_wdt_service(struct fa_wdt_struct *fa_wdt)
110 + __raw_writel(WDRESTART_MAGIC, fa_wdt->base + FA_WDRESTART);
113 +/* Enable and reset the watchdog. */
114 +static void fa_wdt_start(struct fa_wdt_struct *fa_wdt)
116 + spin_lock(&fa_wdt_lock);
118 + __raw_writel(timeout * fa_wdt->clock,
119 + fa_wdt->base + FA_WDLOAD);
121 + fa_wdt_service(fa_wdt);
123 + /* set clock before enabling */
124 + __raw_writel(WDCR_CLOCK_5MHZ | WDCR_SYS_RST,
125 + fa_wdt->base + FA_WDCR);
127 + __raw_writel(WDCR_CLOCK_5MHZ | WDCR_SYS_RST | WDCR_ENABLE,
128 + fa_wdt->base + FA_WDCR);
130 + set_bit(WDT_ACTIVE, &fa_wdt->status);
132 + spin_unlock(&fa_wdt_lock);
135 +/* Watchdog device is opened, and watchdog starts running. */
136 +static int fa_wdt_open(struct inode *inode, struct file *file)
138 + struct fa_wdt_struct *fa_wdt = platform_get_drvdata(fa_wdt_dev);
140 + if (test_bit(WDT_ACTIVE, &fa_wdt->status))
143 + file->private_data = fa_wdt;
145 + fa_wdt_start(fa_wdt);
147 + return nonseekable_open(inode, file);
150 +/* Close the watchdog device. */
151 +static int fa_wdt_close(struct inode *inode, struct file *file)
153 + struct fa_wdt_struct *fa_wdt = file->private_data;
155 + /* Disable the watchdog if possible */
156 + if (test_bit(WDT_OK_TO_CLOSE, &fa_wdt->status))
157 + fa_wdt_stop(fa_wdt);
159 + dev_warn(fa_wdt->dev, "Device closed unexpectedly - timer will not stop\n");
164 +/* Handle commands from user-space. */
165 +static long fa_wdt_ioctl(struct file *file, unsigned int cmd,
168 + struct fa_wdt_struct *fa_wdt = file->private_data;
173 + case WDIOC_KEEPALIVE:
174 + fa_wdt_service(fa_wdt);
177 + case WDIOC_GETSUPPORT:
178 + return copy_to_user((struct watchdog_info *)arg, &fa_wdt_info,
179 + sizeof(fa_wdt_info)) ? -EFAULT : 0;
181 + case WDIOC_SETTIMEOUT:
182 + if (get_user(value, (int *)arg))
185 + if ((value < 1) || (value > fa_wdt->max_timeout))
190 + /* restart wdt to use new timeout */
191 + fa_wdt_stop(fa_wdt);
192 + fa_wdt_start(fa_wdt);
195 + case WDIOC_GETTIMEOUT:
196 + return put_user(timeout, (int *)arg);
198 + case WDIOC_GETTIMELEFT:
199 + value = __raw_readl(fa_wdt->base + FA_WDCOUNTER);
200 + return put_user(value / fa_wdt->clock, (int *)arg);
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)
211 + struct fa_wdt_struct *fa_wdt = file->private_data;
217 + clear_bit(WDT_OK_TO_CLOSE, &fa_wdt->status);
218 + for (i = 0; i != len; i++) {
221 + if (get_user(c, data + i))
224 + set_bit(WDT_OK_TO_CLOSE,
228 + fa_wdt_service(fa_wdt);
234 +static int fa_wdt_notify_sys(struct notifier_block *this,
235 + unsigned long code, void *unused)
237 + struct fa_wdt_struct *fa_wdt = platform_get_drvdata(fa_wdt_dev);
239 + if (code == SYS_DOWN || code == SYS_HALT)
240 + fa_wdt_stop(fa_wdt);
242 + return NOTIFY_DONE;
245 +static struct notifier_block fa_wdt_notifier = {
246 + .notifier_call = fa_wdt_notify_sys,
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,
258 +static struct miscdevice fa_wdt_miscdev = {
259 + .minor = WATCHDOG_MINOR,
260 + .name = "watchdog",
261 + .fops = &fa_wdt_fops,
264 +static void fa_wdt_shutdown(struct platform_device *pdev)
266 + struct fa_wdt_struct *fa_wdt = platform_get_drvdata(pdev);
268 + fa_wdt_stop(fa_wdt);
271 +static int __devinit fa_wdt_probe(struct platform_device *pdev)
275 + struct resource *res;
276 + void __iomem *base;
277 + struct fa_wdt_struct *fa_wdt;
278 + struct fa_wdt_platform_data *pdata;
280 + pdata = pdev->dev.platform_data;
282 + dev_err(&pdev->dev, "no platform data specified\n");
286 + if (!pdata->clock) {
287 + dev_err(&pdev->dev, "invalid clock value\n");
291 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
293 + dev_err(&pdev->dev, "can't get device resources\n");
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);
304 + base = ioremap(res->start, res_size);
306 + dev_err(&pdev->dev, "ioremap failed\n");
311 + fa_wdt = kzalloc(sizeof(struct fa_wdt_struct), GFP_KERNEL);
313 + dev_err(&pdev->dev, "can't allocate interface\n");
318 + /* Setup fa_wdt driver structure */
319 + fa_wdt->base = base;
321 + fa_wdt->dev = &pdev->dev;
322 + fa_wdt->clock = pdata->clock;
323 + fa_wdt->max_timeout = 0xffffffffU / pdata->clock;
325 + /* Set up platform driver data */
326 + platform_set_drvdata(pdev, fa_wdt);
329 + if (fa_wdt_miscdev.parent) {
334 + ret = register_reboot_notifier(&fa_wdt_notifier);
338 + fa_wdt_miscdev.parent = &pdev->dev;
340 + ret = misc_register(&fa_wdt_miscdev);
347 + unregister_reboot_notifier(&fa_wdt_notifier);
349 + platform_set_drvdata(pdev, NULL);
354 + release_mem_region(res->start, res_size);
359 +static int __devexit fa_wdt_remove(struct platform_device *pdev)
361 + struct fa_wdt_struct *fa_wdt = platform_get_drvdata(pdev);
363 + platform_set_drvdata(pdev, NULL);
364 + misc_deregister(&fa_wdt_miscdev);
365 + unregister_reboot_notifier(&fa_wdt_notifier);
367 + iounmap(fa_wdt->base);
368 + release_mem_region(fa_wdt->res->start, resource_size(fa_wdt->res));
376 +static int fa_wdt_suspend(struct platform_device *pdev, pm_message_t message)
378 + struct fa_wdt_struct *fa_wdt = platform_get_drvdata(pdev);
381 + reg = __raw_readw(fa_wdt->base + FA_WDCR);
382 + reg &= ~(WDCR_WDENABLE);
383 + __raw_writel(reg, fa_wdt->base + FA_WDCR);
388 +static int fa_wdt_resume(struct platform_device *pdev)
390 + struct fa_wdt_struct *fa_wdt = platform_get_drvdata(pdev);
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);
402 +#define fa_wdt_suspend NULL
403 +#define fa_wdt_resume NULL
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,
414 + .owner = THIS_MODULE,
418 +static int __init fa_wdt_init(void)
420 + return platform_driver_probe(&fa_wdt_driver, fa_wdt_probe);
423 +static void __exit fa_wdt_exit(void)
425 + platform_driver_unregister(&fa_wdt_driver);
428 +module_init(fa_wdt_init);
429 +module_exit(fa_wdt_exit);
431 +module_param(timeout, uint, 0);
432 +MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
434 +module_param(nowayout, int, 0);
435 +MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
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");
444 +++ b/include/linux/fa_wdt.h
447 + * Platform data definition for the Faraday watchdog driver
453 +struct fa_wdt_platform_data {
454 + unsigned int clock;
457 +#endif /* _FA_WDT_H */