2 * drivers/watchdog/orion_wdt.c
4 * Watchdog driver for Orion/Kirkwood processors
6 * Author: Sylver Bruneau <sylver.bruneau@googlemail.com>
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
20 #include <linux/miscdevice.h>
21 #include <linux/platform_device.h>
22 #include <linux/watchdog.h>
23 #include <linux/init.h>
24 #include <linux/uaccess.h>
26 #include <linux/spinlock.h>
27 #include <linux/clk.h>
29 #include <mach/bridge-regs.h>
32 * Watchdog timer block registers.
34 #define TIMER_CTRL 0x0000
36 #define WDT_VAL 0x0024
38 #define WDT_MAX_CYCLE_COUNT 0xffffffff
40 #define WDT_OK_TO_CLOSE 1
42 static bool nowayout = WATCHDOG_NOWAYOUT;
43 static int heartbeat = -1; /* module parameter (seconds) */
44 static unsigned int wdt_max_duration; /* (seconds) */
45 static struct clk *clk;
46 static unsigned int wdt_tclk;
47 static void __iomem *wdt_reg;
48 static unsigned long wdt_status;
49 static DEFINE_SPINLOCK(wdt_lock);
51 static void orion_wdt_ping(void)
55 /* Reload watchdog duration */
56 writel(wdt_tclk * heartbeat, wdt_reg + WDT_VAL);
58 spin_unlock(&wdt_lock);
61 static void orion_wdt_enable(void)
67 /* Set watchdog duration */
68 writel(wdt_tclk * heartbeat, wdt_reg + WDT_VAL);
70 /* Clear watchdog timer interrupt */
71 reg = readl(BRIDGE_CAUSE);
73 writel(reg, BRIDGE_CAUSE);
75 /* Enable watchdog timer */
76 reg = readl(wdt_reg + TIMER_CTRL);
78 writel(reg, wdt_reg + TIMER_CTRL);
80 /* Enable reset on watchdog */
81 reg = readl(RSTOUTn_MASK);
82 reg |= WDT_RESET_OUT_EN;
83 writel(reg, RSTOUTn_MASK);
85 spin_unlock(&wdt_lock);
88 static void orion_wdt_disable(void)
94 /* Disable reset on watchdog */
95 reg = readl(RSTOUTn_MASK);
96 reg &= ~WDT_RESET_OUT_EN;
97 writel(reg, RSTOUTn_MASK);
99 /* Disable watchdog timer */
100 reg = readl(wdt_reg + TIMER_CTRL);
102 writel(reg, wdt_reg + TIMER_CTRL);
104 spin_unlock(&wdt_lock);
107 static int orion_wdt_get_timeleft(int *time_left)
109 spin_lock(&wdt_lock);
110 *time_left = readl(wdt_reg + WDT_VAL) / wdt_tclk;
111 spin_unlock(&wdt_lock);
115 static int orion_wdt_open(struct inode *inode, struct file *file)
117 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
119 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
121 return nonseekable_open(inode, file);
124 static ssize_t orion_wdt_write(struct file *file, const char *data,
125 size_t len, loff_t *ppos)
131 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
132 for (i = 0; i != len; i++) {
135 if (get_user(c, data + i))
138 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
146 static int orion_wdt_settimeout(int new_time)
148 if ((new_time <= 0) || (new_time > wdt_max_duration))
151 /* Set new watchdog time to be used when
152 * orion_wdt_enable() or orion_wdt_ping() is called. */
153 heartbeat = new_time;
157 static const struct watchdog_info ident = {
158 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
160 .identity = "Orion Watchdog",
163 static long orion_wdt_ioctl(struct file *file, unsigned int cmd,
170 case WDIOC_GETSUPPORT:
171 ret = copy_to_user((struct watchdog_info *)arg, &ident,
172 sizeof(ident)) ? -EFAULT : 0;
175 case WDIOC_GETSTATUS:
176 case WDIOC_GETBOOTSTATUS:
177 ret = put_user(0, (int *)arg);
180 case WDIOC_KEEPALIVE:
185 case WDIOC_SETTIMEOUT:
186 ret = get_user(time, (int *)arg);
190 if (orion_wdt_settimeout(time)) {
197 case WDIOC_GETTIMEOUT:
198 ret = put_user(heartbeat, (int *)arg);
201 case WDIOC_GETTIMELEFT:
202 if (orion_wdt_get_timeleft(&time)) {
206 ret = put_user(time, (int *)arg);
212 static int orion_wdt_release(struct inode *inode, struct file *file)
214 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
217 pr_crit("Device closed unexpectedly - timer will not stop\n");
218 clear_bit(WDT_IN_USE, &wdt_status);
219 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
225 static const struct file_operations orion_wdt_fops = {
226 .owner = THIS_MODULE,
228 .write = orion_wdt_write,
229 .unlocked_ioctl = orion_wdt_ioctl,
230 .open = orion_wdt_open,
231 .release = orion_wdt_release,
234 static struct miscdevice orion_wdt_miscdev = {
235 .minor = WATCHDOG_MINOR,
237 .fops = &orion_wdt_fops,
240 static int __devinit orion_wdt_probe(struct platform_device *pdev)
242 struct resource *res;
245 clk = clk_get(&pdev->dev, NULL);
247 printk(KERN_ERR "Orion Watchdog missing clock\n");
250 clk_prepare_enable(clk);
251 wdt_tclk = clk_get_rate(clk);
253 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
255 wdt_reg = ioremap(res->start, resource_size(res));
257 if (orion_wdt_miscdev.parent)
259 orion_wdt_miscdev.parent = &pdev->dev;
261 wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
262 if (orion_wdt_settimeout(heartbeat))
263 heartbeat = wdt_max_duration;
265 ret = misc_register(&orion_wdt_miscdev);
269 pr_info("Initial timeout %d sec%s\n",
270 heartbeat, nowayout ? ", nowayout" : "");
274 static int __devexit orion_wdt_remove(struct platform_device *pdev)
278 if (test_bit(WDT_IN_USE, &wdt_status)) {
280 clear_bit(WDT_IN_USE, &wdt_status);
283 ret = misc_deregister(&orion_wdt_miscdev);
285 orion_wdt_miscdev.parent = NULL;
287 clk_disable_unprepare(clk);
293 static void orion_wdt_shutdown(struct platform_device *pdev)
295 if (test_bit(WDT_IN_USE, &wdt_status))
299 static const struct of_device_id orion_wdt_of_match_table[] __devinitdata = {
300 { .compatible = "marvell,orion-wdt", },
303 MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
305 static struct platform_driver orion_wdt_driver = {
306 .probe = orion_wdt_probe,
307 .remove = __devexit_p(orion_wdt_remove),
308 .shutdown = orion_wdt_shutdown,
310 .owner = THIS_MODULE,
312 .of_match_table = of_match_ptr(orion_wdt_of_match_table),
316 module_platform_driver(orion_wdt_driver);
318 MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
319 MODULE_DESCRIPTION("Orion Processor Watchdog");
321 module_param(heartbeat, int, 0);
322 MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
324 module_param(nowayout, bool, 0);
325 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
326 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
328 MODULE_LICENSE("GPL");
329 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);