at91rm9200_wdt.c 7.9 KB

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