at91sam9_wdt.c 8.0 KB

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