at91sam9_wdt.c 7.6 KB

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