at91sam9_wdt.c 7.7 KB

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