at91rm9200_wdt.c 6.7 KB

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