at91sam9_wdt.c 7.5 KB

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