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 | |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/init.h> |
Andrew Victor | 2af29b7 | 2009-02-11 21:23:10 +0100 | [diff] [blame] | 21 | #include <linux/io.h> |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 22 | #include <linux/kernel.h> |
| 23 | #include <linux/miscdevice.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/moduleparam.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/watchdog.h> |
| 29 | #include <linux/jiffies.h> |
| 30 | #include <linux/timer.h> |
| 31 | #include <linux/bitops.h> |
| 32 | #include <linux/uaccess.h> |
| 33 | |
Andrew Victor | c1dfda3 | 2008-11-06 12:53:42 -0800 | [diff] [blame] | 34 | #include <mach/at91_wdt.h> |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 35 | |
| 36 | #define DRV_NAME "AT91SAM9 Watchdog" |
| 37 | |
| 38 | /* AT91SAM9 watchdog runs a 12bit counter @ 256Hz, |
| 39 | * use this to convert a watchdog |
| 40 | * value from/to milliseconds. |
| 41 | */ |
| 42 | #define ms_to_ticks(t) (((t << 8) / 1000) - 1) |
| 43 | #define ticks_to_ms(t) (((t + 1) * 1000) >> 8) |
| 44 | |
| 45 | /* Hardware timeout in seconds */ |
| 46 | #define WDT_HW_TIMEOUT 2 |
| 47 | |
| 48 | /* Timer heartbeat (500ms) */ |
| 49 | #define WDT_TIMEOUT (HZ/2) |
| 50 | |
| 51 | /* User land timeout */ |
| 52 | #define WDT_HEARTBEAT 15 |
| 53 | static int heartbeat = WDT_HEARTBEAT; |
| 54 | module_param(heartbeat, int, 0); |
| 55 | MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. " |
| 56 | "(default = " __MODULE_STRING(WDT_HEARTBEAT) ")"); |
| 57 | |
| 58 | static int nowayout = WATCHDOG_NOWAYOUT; |
| 59 | module_param(nowayout, int, 0); |
| 60 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started " |
| 61 | "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 62 | |
| 63 | static void at91_ping(unsigned long data); |
| 64 | |
| 65 | static struct { |
| 66 | unsigned long next_heartbeat; /* the next_heartbeat for the timer */ |
| 67 | unsigned long open; |
| 68 | char expect_close; |
| 69 | struct timer_list timer; /* The timer that pings the watchdog */ |
| 70 | } at91wdt_private; |
| 71 | |
| 72 | /* ......................................................................... */ |
| 73 | |
| 74 | |
| 75 | /* |
| 76 | * Reload the watchdog timer. (ie, pat the watchdog) |
| 77 | */ |
| 78 | static inline void at91_wdt_reset(void) |
| 79 | { |
| 80 | at91_sys_write(AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Timer tick |
| 85 | */ |
| 86 | static void at91_ping(unsigned long data) |
| 87 | { |
| 88 | if (time_before(jiffies, at91wdt_private.next_heartbeat) || |
| 89 | (!nowayout && !at91wdt_private.open)) { |
| 90 | at91_wdt_reset(); |
| 91 | mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT); |
| 92 | } else |
| 93 | printk(KERN_CRIT DRV_NAME": I will reset your machine !\n"); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Watchdog device is opened, and watchdog starts running. |
| 98 | */ |
| 99 | static int at91_wdt_open(struct inode *inode, struct file *file) |
| 100 | { |
| 101 | if (test_and_set_bit(0, &at91wdt_private.open)) |
| 102 | return -EBUSY; |
| 103 | |
| 104 | at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ; |
| 105 | mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT); |
| 106 | |
| 107 | return nonseekable_open(inode, file); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Close the watchdog device. |
| 112 | */ |
| 113 | static int at91_wdt_close(struct inode *inode, struct file *file) |
| 114 | { |
| 115 | clear_bit(0, &at91wdt_private.open); |
| 116 | |
| 117 | /* stop internal ping */ |
| 118 | if (!at91wdt_private.expect_close) |
| 119 | del_timer(&at91wdt_private.timer); |
| 120 | |
| 121 | at91wdt_private.expect_close = 0; |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Set the watchdog time interval in 1/256Hz (write-once) |
| 127 | * Counter is 12 bit. |
| 128 | */ |
| 129 | static int at91_wdt_settimeout(unsigned int timeout) |
| 130 | { |
| 131 | unsigned int reg; |
| 132 | unsigned int mr; |
| 133 | |
| 134 | /* Check if disabled */ |
| 135 | mr = at91_sys_read(AT91_WDT_MR); |
| 136 | if (mr & AT91_WDT_WDDIS) { |
| 137 | printk(KERN_ERR DRV_NAME": sorry, watchdog is disabled\n"); |
| 138 | return -EIO; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * All counting occurs at SLOW_CLOCK / 128 = 256 Hz |
| 143 | * |
| 144 | * Since WDV is a 12-bit counter, the maximum period is |
| 145 | * 4096 / 256 = 16 seconds. |
| 146 | */ |
| 147 | reg = AT91_WDT_WDRSTEN /* causes watchdog reset */ |
| 148 | /* | AT91_WDT_WDRPROC causes processor reset only */ |
| 149 | | AT91_WDT_WDDBGHLT /* disabled in debug mode */ |
| 150 | | AT91_WDT_WDD /* restart at any time */ |
| 151 | | (timeout & AT91_WDT_WDV); /* timer value */ |
| 152 | at91_sys_write(AT91_WDT_MR, reg); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static const struct watchdog_info at91_wdt_info = { |
| 158 | .identity = DRV_NAME, |
| 159 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, |
| 160 | }; |
| 161 | |
| 162 | /* |
| 163 | * Handle commands from user-space. |
| 164 | */ |
| 165 | static long at91_wdt_ioctl(struct file *file, |
| 166 | unsigned int cmd, unsigned long arg) |
| 167 | { |
| 168 | void __user *argp = (void __user *)arg; |
| 169 | int __user *p = argp; |
| 170 | int new_value; |
| 171 | |
| 172 | switch (cmd) { |
| 173 | case WDIOC_GETSUPPORT: |
| 174 | return copy_to_user(argp, &at91_wdt_info, |
| 175 | sizeof(at91_wdt_info)) ? -EFAULT : 0; |
| 176 | |
| 177 | case WDIOC_GETSTATUS: |
| 178 | case WDIOC_GETBOOTSTATUS: |
| 179 | return put_user(0, p); |
| 180 | |
| 181 | case WDIOC_KEEPALIVE: |
| 182 | at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ; |
| 183 | return 0; |
| 184 | |
| 185 | case WDIOC_SETTIMEOUT: |
| 186 | if (get_user(new_value, p)) |
| 187 | return -EFAULT; |
| 188 | |
| 189 | heartbeat = new_value; |
| 190 | at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ; |
| 191 | |
| 192 | return put_user(new_value, p); /* return current value */ |
| 193 | |
| 194 | case WDIOC_GETTIMEOUT: |
| 195 | return put_user(heartbeat, p); |
| 196 | } |
| 197 | return -ENOTTY; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Pat the watchdog whenever device is written to. |
| 202 | */ |
| 203 | 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] | 204 | loff_t *ppos) |
Renaud CERRATO | e6bb42e | 2008-06-23 17:05:49 +0200 | [diff] [blame] | 205 | { |
| 206 | if (!len) |
| 207 | return 0; |
| 208 | |
| 209 | /* Scan for magic character */ |
| 210 | if (!nowayout) { |
| 211 | size_t i; |
| 212 | |
| 213 | at91wdt_private.expect_close = 0; |
| 214 | |
| 215 | for (i = 0; i < len; i++) { |
| 216 | char c; |
| 217 | if (get_user(c, data + i)) |
| 218 | return -EFAULT; |
| 219 | if (c == 'V') { |
| 220 | at91wdt_private.expect_close = 42; |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ; |
| 227 | |
| 228 | return len; |
| 229 | } |
| 230 | |
| 231 | /* ......................................................................... */ |
| 232 | |
| 233 | static const struct file_operations at91wdt_fops = { |
| 234 | .owner = THIS_MODULE, |
| 235 | .llseek = no_llseek, |
| 236 | .unlocked_ioctl = at91_wdt_ioctl, |
| 237 | .open = at91_wdt_open, |
| 238 | .release = at91_wdt_close, |
| 239 | .write = at91_wdt_write, |
| 240 | }; |
| 241 | |
| 242 | static struct miscdevice at91wdt_miscdev = { |
| 243 | .minor = WATCHDOG_MINOR, |
| 244 | .name = "watchdog", |
| 245 | .fops = &at91wdt_fops, |
| 246 | }; |
| 247 | |
| 248 | static int __init at91wdt_probe(struct platform_device *pdev) |
| 249 | { |
| 250 | int res; |
| 251 | |
| 252 | if (at91wdt_miscdev.parent) |
| 253 | return -EBUSY; |
| 254 | at91wdt_miscdev.parent = &pdev->dev; |
| 255 | |
| 256 | /* Set watchdog */ |
| 257 | res = at91_wdt_settimeout(ms_to_ticks(WDT_HW_TIMEOUT * 1000)); |
| 258 | if (res) |
| 259 | return res; |
| 260 | |
| 261 | res = misc_register(&at91wdt_miscdev); |
| 262 | if (res) |
| 263 | return res; |
| 264 | |
| 265 | at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ; |
| 266 | setup_timer(&at91wdt_private.timer, at91_ping, 0); |
| 267 | mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT); |
| 268 | |
| 269 | printk(KERN_INFO DRV_NAME " enabled (heartbeat=%d sec, nowayout=%d)\n", |
| 270 | heartbeat, nowayout); |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static int __exit at91wdt_remove(struct platform_device *pdev) |
| 276 | { |
| 277 | int res; |
| 278 | |
| 279 | res = misc_deregister(&at91wdt_miscdev); |
| 280 | if (!res) |
| 281 | at91wdt_miscdev.parent = NULL; |
| 282 | |
| 283 | return res; |
| 284 | } |
| 285 | |
| 286 | #ifdef CONFIG_PM |
| 287 | |
| 288 | static int at91wdt_suspend(struct platform_device *pdev, pm_message_t message) |
| 289 | { |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | static int at91wdt_resume(struct platform_device *pdev) |
| 294 | { |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | #else |
| 299 | #define at91wdt_suspend NULL |
| 300 | #define at91wdt_resume NULL |
| 301 | #endif |
| 302 | |
| 303 | static struct platform_driver at91wdt_driver = { |
| 304 | .remove = __exit_p(at91wdt_remove), |
| 305 | .suspend = at91wdt_suspend, |
| 306 | .resume = at91wdt_resume, |
| 307 | .driver = { |
| 308 | .name = "at91_wdt", |
| 309 | .owner = THIS_MODULE, |
| 310 | }, |
| 311 | }; |
| 312 | |
| 313 | static int __init at91sam_wdt_init(void) |
| 314 | { |
| 315 | return platform_driver_probe(&at91wdt_driver, at91wdt_probe); |
| 316 | } |
| 317 | |
| 318 | static void __exit at91sam_wdt_exit(void) |
| 319 | { |
| 320 | platform_driver_unregister(&at91wdt_driver); |
| 321 | } |
| 322 | |
| 323 | module_init(at91sam_wdt_init); |
| 324 | module_exit(at91sam_wdt_exit); |
| 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); |