at91rm9200_wdt.c 7.7 KB

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