Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Watchdog driver for Atmel AT91SAM9x processors. |
| 3 | * |
| 4 | * Copyright (C) 2008 Renaud CERRATO r.cerrato@til-technologies.fr |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * The Watchdog Timer Mode Register can be only written to once. If the |
| 14 | * timeout need to be set from Linux, be sure that the bootstrap or the |
| 15 | * bootloader doesn't write to this register. |
| 16 | */ |
| 17 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 19 | |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 20 | #include <linux/errno.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/init.h> |
Andrew Victor | 2af29b7 | 2009-02-11 21:23:10 +0100 | [diff] [blame] | 23 | #include <linux/io.h> |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 24 | #include <linux/kernel.h> |
| 25 | #include <linux/miscdevice.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/moduleparam.h> |
| 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/watchdog.h> |
| 31 | #include <linux/jiffies.h> |
| 32 | #include <linux/timer.h> |
| 33 | #include <linux/bitops.h> |
| 34 | #include <linux/uaccess.h> |
Fabio Porcedda | be49bba | 2012-11-12 09:37:25 +0100 | [diff] [blame] | 35 | #include <linux/of.h> |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 36 | |
Jean-Christophe Plagniol-Villard | e7b3914 | 2011-07-15 01:52:05 +0200 | [diff] [blame] | 37 | #include "at91sam9_wdt.h" |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 38 | |
| 39 | #define DRV_NAME "AT91SAM9 Watchdog" |
| 40 | |
Jean-Christophe PLAGNIOL-VILLARD | c1c30a2 | 2011-11-02 01:43:31 +0800 | [diff] [blame] | 41 | #define wdt_read(field) \ |
| 42 | __raw_readl(at91wdt_private.base + field) |
| 43 | #define wdt_write(field, val) \ |
| 44 | __raw_writel((val), at91wdt_private.base + field) |
| 45 | |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 46 | /* AT91SAM9 watchdog runs a 12bit counter @ 256Hz, |
| 47 | * use this to convert a watchdog |
| 48 | * value from/to milliseconds. |
| 49 | */ |
| 50 | #define ms_to_ticks(t) (((t << 8) / 1000) - 1) |
| 51 | #define ticks_to_ms(t) (((t + 1) * 1000) >> 8) |
| 52 | |
| 53 | /* Hardware timeout in seconds */ |
| 54 | #define WDT_HW_TIMEOUT 2 |
| 55 | |
| 56 | /* Timer heartbeat (500ms) */ |
| 57 | #define WDT_TIMEOUT (HZ/2) |
| 58 | |
| 59 | /* User land timeout */ |
| 60 | #define WDT_HEARTBEAT 15 |
| 61 | static int heartbeat = WDT_HEARTBEAT; |
| 62 | module_param(heartbeat, int, 0); |
| 63 | MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. " |
| 64 | "(default = " __MODULE_STRING(WDT_HEARTBEAT) ")"); |
| 65 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 66 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 67 | module_param(nowayout, bool, 0); |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 68 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started " |
| 69 | "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 70 | |
| 71 | static void at91_ping(unsigned long data); |
| 72 | |
| 73 | static struct { |
Jean-Christophe PLAGNIOL-VILLARD | c1c30a2 | 2011-11-02 01:43:31 +0800 | [diff] [blame] | 74 | void __iomem *base; |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 75 | unsigned long next_heartbeat; /* the next_heartbeat for the timer */ |
| 76 | unsigned long open; |
| 77 | char expect_close; |
| 78 | struct timer_list timer; /* The timer that pings the watchdog */ |
| 79 | } at91wdt_private; |
| 80 | |
| 81 | /* ......................................................................... */ |
| 82 | |
| 83 | |
| 84 | /* |
| 85 | * Reload the watchdog timer. (ie, pat the watchdog) |
| 86 | */ |
| 87 | static inline void at91_wdt_reset(void) |
| 88 | { |
Jean-Christophe PLAGNIOL-VILLARD | c1c30a2 | 2011-11-02 01:43:31 +0800 | [diff] [blame] | 89 | wdt_write(AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT); |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Timer tick |
| 94 | */ |
| 95 | static void at91_ping(unsigned long data) |
| 96 | { |
| 97 | if (time_before(jiffies, at91wdt_private.next_heartbeat) || |
| 98 | (!nowayout && !at91wdt_private.open)) { |
| 99 | at91_wdt_reset(); |
| 100 | mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT); |
| 101 | } else |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 102 | pr_crit("I will reset your machine !\n"); |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Watchdog device is opened, and watchdog starts running. |
| 107 | */ |
| 108 | static int at91_wdt_open(struct inode *inode, struct file *file) |
| 109 | { |
| 110 | if (test_and_set_bit(0, &at91wdt_private.open)) |
| 111 | return -EBUSY; |
| 112 | |
| 113 | at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ; |
| 114 | mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT); |
| 115 | |
| 116 | return nonseekable_open(inode, file); |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Close the watchdog device. |
| 121 | */ |
| 122 | static int at91_wdt_close(struct inode *inode, struct file *file) |
| 123 | { |
| 124 | clear_bit(0, &at91wdt_private.open); |
| 125 | |
| 126 | /* stop internal ping */ |
| 127 | if (!at91wdt_private.expect_close) |
| 128 | del_timer(&at91wdt_private.timer); |
| 129 | |
| 130 | at91wdt_private.expect_close = 0; |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Set the watchdog time interval in 1/256Hz (write-once) |
| 136 | * Counter is 12 bit. |
| 137 | */ |
| 138 | static int at91_wdt_settimeout(unsigned int timeout) |
| 139 | { |
| 140 | unsigned int reg; |
| 141 | unsigned int mr; |
| 142 | |
| 143 | /* Check if disabled */ |
Jean-Christophe PLAGNIOL-VILLARD | c1c30a2 | 2011-11-02 01:43:31 +0800 | [diff] [blame] | 144 | mr = wdt_read(AT91_WDT_MR); |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 145 | if (mr & AT91_WDT_WDDIS) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 146 | pr_err("sorry, watchdog is disabled\n"); |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 147 | return -EIO; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * All counting occurs at SLOW_CLOCK / 128 = 256 Hz |
| 152 | * |
| 153 | * Since WDV is a 12-bit counter, the maximum period is |
| 154 | * 4096 / 256 = 16 seconds. |
| 155 | */ |
| 156 | reg = AT91_WDT_WDRSTEN /* causes watchdog reset */ |
| 157 | /* | AT91_WDT_WDRPROC causes processor reset only */ |
| 158 | | AT91_WDT_WDDBGHLT /* disabled in debug mode */ |
| 159 | | AT91_WDT_WDD /* restart at any time */ |
| 160 | | (timeout & AT91_WDT_WDV); /* timer value */ |
Jean-Christophe PLAGNIOL-VILLARD | c1c30a2 | 2011-11-02 01:43:31 +0800 | [diff] [blame] | 161 | wdt_write(AT91_WDT_MR, reg); |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static const struct watchdog_info at91_wdt_info = { |
| 167 | .identity = DRV_NAME, |
Wim Van Sebroeck | e73a780 | 2009-05-11 18:33:00 +0000 | [diff] [blame] | 168 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | |
| 169 | WDIOF_MAGICCLOSE, |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | /* |
| 173 | * Handle commands from user-space. |
| 174 | */ |
| 175 | static long at91_wdt_ioctl(struct file *file, |
| 176 | unsigned int cmd, unsigned long arg) |
| 177 | { |
| 178 | void __user *argp = (void __user *)arg; |
| 179 | int __user *p = argp; |
| 180 | int new_value; |
| 181 | |
| 182 | switch (cmd) { |
| 183 | case WDIOC_GETSUPPORT: |
| 184 | return copy_to_user(argp, &at91_wdt_info, |
| 185 | sizeof(at91_wdt_info)) ? -EFAULT : 0; |
| 186 | |
| 187 | case WDIOC_GETSTATUS: |
| 188 | case WDIOC_GETBOOTSTATUS: |
| 189 | return put_user(0, p); |
| 190 | |
| 191 | case WDIOC_KEEPALIVE: |
| 192 | at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ; |
| 193 | return 0; |
| 194 | |
| 195 | case WDIOC_SETTIMEOUT: |
| 196 | if (get_user(new_value, p)) |
| 197 | return -EFAULT; |
| 198 | |
| 199 | heartbeat = new_value; |
| 200 | at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ; |
| 201 | |
| 202 | return put_user(new_value, p); /* return current value */ |
| 203 | |
| 204 | case WDIOC_GETTIMEOUT: |
| 205 | return put_user(heartbeat, p); |
| 206 | } |
| 207 | return -ENOTTY; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Pat the watchdog whenever device is written to. |
| 212 | */ |
| 213 | static ssize_t at91_wdt_write(struct file *file, const char *data, size_t len, |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 214 | loff_t *ppos) |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 215 | { |
| 216 | if (!len) |
| 217 | return 0; |
| 218 | |
| 219 | /* Scan for magic character */ |
| 220 | if (!nowayout) { |
| 221 | size_t i; |
| 222 | |
| 223 | at91wdt_private.expect_close = 0; |
| 224 | |
| 225 | for (i = 0; i < len; i++) { |
| 226 | char c; |
| 227 | if (get_user(c, data + i)) |
| 228 | return -EFAULT; |
| 229 | if (c == 'V') { |
| 230 | at91wdt_private.expect_close = 42; |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ; |
| 237 | |
| 238 | return len; |
| 239 | } |
| 240 | |
| 241 | /* ......................................................................... */ |
| 242 | |
| 243 | static const struct file_operations at91wdt_fops = { |
| 244 | .owner = THIS_MODULE, |
| 245 | .llseek = no_llseek, |
| 246 | .unlocked_ioctl = at91_wdt_ioctl, |
| 247 | .open = at91_wdt_open, |
| 248 | .release = at91_wdt_close, |
| 249 | .write = at91_wdt_write, |
| 250 | }; |
| 251 | |
| 252 | static struct miscdevice at91wdt_miscdev = { |
| 253 | .minor = WATCHDOG_MINOR, |
| 254 | .name = "watchdog", |
| 255 | .fops = &at91wdt_fops, |
| 256 | }; |
| 257 | |
| 258 | static int __init at91wdt_probe(struct platform_device *pdev) |
| 259 | { |
Jean-Christophe PLAGNIOL-VILLARD | c1c30a2 | 2011-11-02 01:43:31 +0800 | [diff] [blame] | 260 | struct resource *r; |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 261 | int res; |
| 262 | |
| 263 | if (at91wdt_miscdev.parent) |
| 264 | return -EBUSY; |
| 265 | at91wdt_miscdev.parent = &pdev->dev; |
| 266 | |
Jean-Christophe PLAGNIOL-VILLARD | c1c30a2 | 2011-11-02 01:43:31 +0800 | [diff] [blame] | 267 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 268 | if (!r) |
| 269 | return -ENODEV; |
| 270 | at91wdt_private.base = ioremap(r->start, resource_size(r)); |
| 271 | if (!at91wdt_private.base) { |
| 272 | dev_err(&pdev->dev, "failed to map registers, aborting.\n"); |
| 273 | return -ENOMEM; |
| 274 | } |
| 275 | |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 276 | /* Set watchdog */ |
| 277 | res = at91_wdt_settimeout(ms_to_ticks(WDT_HW_TIMEOUT * 1000)); |
| 278 | if (res) |
| 279 | return res; |
| 280 | |
| 281 | res = misc_register(&at91wdt_miscdev); |
| 282 | if (res) |
| 283 | return res; |
| 284 | |
| 285 | at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ; |
| 286 | setup_timer(&at91wdt_private.timer, at91_ping, 0); |
| 287 | mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT); |
| 288 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 289 | pr_info("enabled (heartbeat=%d sec, nowayout=%d)\n", |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 290 | heartbeat, nowayout); |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static int __exit at91wdt_remove(struct platform_device *pdev) |
| 296 | { |
| 297 | int res; |
| 298 | |
| 299 | res = misc_deregister(&at91wdt_miscdev); |
| 300 | if (!res) |
| 301 | at91wdt_miscdev.parent = NULL; |
| 302 | |
| 303 | return res; |
| 304 | } |
| 305 | |
Fabio Porcedda | be49bba | 2012-11-12 09:37:25 +0100 | [diff] [blame] | 306 | #if defined(CONFIG_OF) |
| 307 | static const struct of_device_id at91_wdt_dt_ids[] __initconst = { |
| 308 | { .compatible = "atmel,at91sam9260-wdt" }, |
| 309 | { /* sentinel */ } |
| 310 | }; |
| 311 | |
| 312 | MODULE_DEVICE_TABLE(of, at91_wdt_dt_ids); |
| 313 | #endif |
| 314 | |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 315 | static struct platform_driver at91wdt_driver = { |
| 316 | .remove = __exit_p(at91wdt_remove), |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 317 | .driver = { |
| 318 | .name = "at91_wdt", |
| 319 | .owner = THIS_MODULE, |
Fabio Porcedda | be49bba | 2012-11-12 09:37:25 +0100 | [diff] [blame] | 320 | .of_match_table = of_match_ptr(at91_wdt_dt_ids), |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 321 | }, |
| 322 | }; |
| 323 | |
Fabio Porcedda | 1cb9204 | 2013-01-09 12:15:27 +0100 | [diff] [blame] | 324 | module_platform_driver_probe(at91wdt_driver, at91wdt_probe); |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 325 | |
| 326 | MODULE_AUTHOR("Renaud CERRATO <r.cerrato@til-technologies.fr>"); |
| 327 | MODULE_DESCRIPTION("Watchdog driver for Atmel AT91SAM9x processors"); |
| 328 | MODULE_LICENSE("GPL"); |
| 329 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |