at91rm9200_wdt.c 6.5 KB

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