at91rm9200_wdt.c 6.5 KB

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