at91_wdt.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Watchdog driver for Atmel AT91RM9200 (Thunder)
  3. *
  4. * Copyright (C) 2003 SAN People (Pty) Ltd
  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. #include <linux/errno.h>
  12. #include <linux/fs.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/miscdevice.h>
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/types.h>
  20. #include <linux/watchdog.h>
  21. #include <asm/bitops.h>
  22. #include <asm/uaccess.h>
  23. #define WDT_DEFAULT_TIME 5 /* seconds */
  24. #define WDT_MAX_TIME 256 /* seconds */
  25. static int wdt_time = WDT_DEFAULT_TIME;
  26. static int nowayout = WATCHDOG_NOWAYOUT;
  27. module_param(wdt_time, int, 0);
  28. MODULE_PARM_DESC(wdt_time, "Watchdog time in seconds. (default="__MODULE_STRING(WDT_DEFAULT_TIME) ")");
  29. #ifdef CONFIG_WATCHDOG_NOWAYOUT
  30. module_param(nowayout, int, 0);
  31. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  32. #endif
  33. static unsigned long at91wdt_busy;
  34. /* ......................................................................... */
  35. /*
  36. * Disable the watchdog.
  37. */
  38. static void inline at91_wdt_stop(void)
  39. {
  40. at91_sys_write(AT91_ST_WDMR, AT91_ST_EXTEN);
  41. }
  42. /*
  43. * Enable and reset the watchdog.
  44. */
  45. static void inline at91_wdt_start(void)
  46. {
  47. at91_sys_write(AT91_ST_WDMR, AT91_ST_EXTEN | AT91_ST_RSTEN | (((65536 * wdt_time) >> 8) & AT91_ST_WDV));
  48. at91_sys_write(AT91_ST_CR, AT91_ST_WDRST);
  49. }
  50. /*
  51. * Reload the watchdog timer. (ie, pat the watchdog)
  52. */
  53. static void inline at91_wdt_reload(void)
  54. {
  55. at91_sys_write(AT91_ST_CR, AT91_ST_WDRST);
  56. }
  57. /* ......................................................................... */
  58. /*
  59. * Watchdog device is opened, and watchdog starts running.
  60. */
  61. static int at91_wdt_open(struct inode *inode, struct file *file)
  62. {
  63. if (test_and_set_bit(0, &at91wdt_busy))
  64. return -EBUSY;
  65. at91_wdt_start();
  66. return nonseekable_open(inode, file);
  67. }
  68. /*
  69. * Close the watchdog device.
  70. * If CONFIG_WATCHDOG_NOWAYOUT is NOT defined then the watchdog is also
  71. * disabled.
  72. */
  73. static int at91_wdt_close(struct inode *inode, struct file *file)
  74. {
  75. if (!nowayout)
  76. at91_wdt_stop(); /* Disable the watchdog when file is closed */
  77. clear_bit(0, &at91wdt_busy);
  78. return 0;
  79. }
  80. /*
  81. * Change the watchdog time interval.
  82. */
  83. static int at91_wdt_settimeout(int new_time)
  84. {
  85. /*
  86. * All counting occurs at SLOW_CLOCK / 128 = 0.256 Hz
  87. *
  88. * Since WDV is a 16-bit counter, the maximum period is
  89. * 65536 / 0.256 = 256 seconds.
  90. */
  91. if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
  92. return -EINVAL;
  93. /* Set new watchdog time. It will be used when at91_wdt_start() is called. */
  94. wdt_time = new_time;
  95. return 0;
  96. }
  97. static struct watchdog_info at91_wdt_info = {
  98. .identity = "at91 watchdog",
  99. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  100. };
  101. /*
  102. * Handle commands from user-space.
  103. */
  104. static int at91_wdt_ioctl(struct inode *inode, struct file *file,
  105. unsigned int cmd, unsigned long arg)
  106. {
  107. void __user *argp = (void __user *)arg;
  108. int __user *p = argp;
  109. int new_value;
  110. switch(cmd) {
  111. case WDIOC_KEEPALIVE:
  112. at91_wdt_reload(); /* pat the watchdog */
  113. return 0;
  114. case WDIOC_GETSUPPORT:
  115. return copy_to_user(argp, &at91_wdt_info, sizeof(at91_wdt_info)) ? -EFAULT : 0;
  116. case WDIOC_SETTIMEOUT:
  117. if (get_user(new_value, p))
  118. return -EFAULT;
  119. if (at91_wdt_settimeout(new_value))
  120. return -EINVAL;
  121. /* Enable new time value */
  122. at91_wdt_start();
  123. /* Return current value */
  124. return put_user(wdt_time, p);
  125. case WDIOC_GETTIMEOUT:
  126. return put_user(wdt_time, p);
  127. case WDIOC_GETSTATUS:
  128. case WDIOC_GETBOOTSTATUS:
  129. return put_user(0, p);
  130. case WDIOC_SETOPTIONS:
  131. if (get_user(new_value, p))
  132. return -EFAULT;
  133. if (new_value & WDIOS_DISABLECARD)
  134. at91_wdt_stop();
  135. if (new_value & WDIOS_ENABLECARD)
  136. at91_wdt_start();
  137. return 0;
  138. default:
  139. return -ENOIOCTLCMD;
  140. }
  141. }
  142. /*
  143. * Pat the watchdog whenever device is written to.
  144. */
  145. static ssize_t at91_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
  146. {
  147. at91_wdt_reload(); /* pat the watchdog */
  148. return len;
  149. }
  150. /* ......................................................................... */
  151. static const struct file_operations at91wdt_fops = {
  152. .owner = THIS_MODULE,
  153. .llseek = no_llseek,
  154. .ioctl = at91_wdt_ioctl,
  155. .open = at91_wdt_open,
  156. .release = at91_wdt_close,
  157. .write = at91_wdt_write,
  158. };
  159. static struct miscdevice at91wdt_miscdev = {
  160. .minor = WATCHDOG_MINOR,
  161. .name = "watchdog",
  162. .fops = &at91wdt_fops,
  163. };
  164. static int __init at91wdt_probe(struct platform_device *pdev)
  165. {
  166. int res;
  167. if (at91wdt_miscdev.dev)
  168. return -EBUSY;
  169. at91wdt_miscdev.dev = &pdev->dev;
  170. res = misc_register(&at91wdt_miscdev);
  171. if (res)
  172. return res;
  173. printk("AT91 Watchdog Timer enabled (%d seconds%s)\n", wdt_time, nowayout ? ", nowayout" : "");
  174. return 0;
  175. }
  176. static int __exit at91wdt_remove(struct platform_device *pdev)
  177. {
  178. int res;
  179. res = misc_deregister(&at91wdt_miscdev);
  180. if (!res)
  181. at91wdt_miscdev.dev = NULL;
  182. return res;
  183. }
  184. static void at91wdt_shutdown(struct platform_device *pdev)
  185. {
  186. at91_wdt_stop();
  187. }
  188. #ifdef CONFIG_PM
  189. static int at91wdt_suspend(struct platform_device *pdev, pm_message_t message)
  190. {
  191. at91_wdt_stop();
  192. return 0;
  193. }
  194. static int at91wdt_resume(struct platform_device *pdev)
  195. {
  196. if (at91wdt_busy)
  197. at91_wdt_start();
  198. return 0;
  199. }
  200. #else
  201. #define at91wdt_suspend NULL
  202. #define at91wdt_resume NULL
  203. #endif
  204. static struct platform_driver at91wdt_driver = {
  205. .probe = at91wdt_probe,
  206. .remove = __exit_p(at91wdt_remove),
  207. .shutdown = at91wdt_shutdown,
  208. .suspend = at91wdt_suspend,
  209. .resume = at91wdt_resume,
  210. .driver = {
  211. .name = "at91_wdt",
  212. .owner = THIS_MODULE,
  213. },
  214. };
  215. static int __init at91_wdt_init(void)
  216. {
  217. /* Check that the heartbeat value is within range; if not reset to the default */
  218. if (at91_wdt_settimeout(wdt_time)) {
  219. at91_wdt_settimeout(WDT_DEFAULT_TIME);
  220. pr_info("at91_wdt: wdt_time value must be 1 <= wdt_time <= 256, using %d\n", wdt_time);
  221. }
  222. return platform_driver_register(&at91wdt_driver);
  223. }
  224. static void __exit at91_wdt_exit(void)
  225. {
  226. platform_driver_unregister(&at91wdt_driver);
  227. }
  228. module_init(at91_wdt_init);
  229. module_exit(at91_wdt_exit);
  230. MODULE_AUTHOR("Andrew Victor");
  231. MODULE_DESCRIPTION("Watchdog driver for Atmel AT91RM9200");
  232. MODULE_LICENSE("GPL");
  233. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);