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 stream_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. .compat_ioctl = compat_ptr_ioctl,
  188. .open = at91_wdt_open,
  189. .release = at91_wdt_close,
  190. .write = at91_wdt_write,
  191. };
  192. static struct miscdevice at91wdt_miscdev = {
  193. .minor = WATCHDOG_MINOR,
  194. .name = "watchdog",
  195. .fops = &at91wdt_fops,
  196. };
  197. static int at91wdt_probe(struct platform_device *pdev)
  198. {
  199. struct device *dev = &pdev->dev;
  200. struct device *parent;
  201. int res;
  202. if (at91wdt_miscdev.parent)
  203. return -EBUSY;
  204. at91wdt_miscdev.parent = &pdev->dev;
  205. parent = dev->parent;
  206. if (!parent) {
  207. dev_err(dev, "no parent\n");
  208. return -ENODEV;
  209. }
  210. regmap_st = syscon_node_to_regmap(parent->of_node);
  211. if (IS_ERR(regmap_st))
  212. return -ENODEV;
  213. res = misc_register(&at91wdt_miscdev);
  214. if (res)
  215. return res;
  216. res = register_restart_handler(&at91rm9200_restart_nb);
  217. if (res)
  218. dev_warn(dev, "failed to register restart handler\n");
  219. pr_info("AT91 Watchdog Timer enabled (%d seconds%s)\n",
  220. wdt_time, nowayout ? ", nowayout" : "");
  221. return 0;
  222. }
  223. static int at91wdt_remove(struct platform_device *pdev)
  224. {
  225. struct device *dev = &pdev->dev;
  226. int res;
  227. res = unregister_restart_handler(&at91rm9200_restart_nb);
  228. if (res)
  229. dev_warn(dev, "failed to unregister restart handler\n");
  230. misc_deregister(&at91wdt_miscdev);
  231. at91wdt_miscdev.parent = NULL;
  232. return res;
  233. }
  234. static void at91wdt_shutdown(struct platform_device *pdev)
  235. {
  236. at91_wdt_stop();
  237. }
  238. #ifdef CONFIG_PM
  239. static int at91wdt_suspend(struct platform_device *pdev, pm_message_t message)
  240. {
  241. at91_wdt_stop();
  242. return 0;
  243. }
  244. static int at91wdt_resume(struct platform_device *pdev)
  245. {
  246. if (at91wdt_busy)
  247. at91_wdt_start();
  248. return 0;
  249. }
  250. #else
  251. #define at91wdt_suspend NULL
  252. #define at91wdt_resume NULL
  253. #endif
  254. static const struct of_device_id at91_wdt_dt_ids[] = {
  255. { .compatible = "atmel,at91rm9200-wdt" },
  256. { /* sentinel */ }
  257. };
  258. MODULE_DEVICE_TABLE(of, at91_wdt_dt_ids);
  259. static struct platform_driver at91wdt_driver = {
  260. .probe = at91wdt_probe,
  261. .remove = at91wdt_remove,
  262. .shutdown = at91wdt_shutdown,
  263. .suspend = at91wdt_suspend,
  264. .resume = at91wdt_resume,
  265. .driver = {
  266. .name = "atmel_st_watchdog",
  267. .of_match_table = at91_wdt_dt_ids,
  268. },
  269. };
  270. static int __init at91_wdt_init(void)
  271. {
  272. /* Check that the heartbeat value is within range;
  273. if not reset to the default */
  274. if (at91_wdt_settimeout(wdt_time)) {
  275. at91_wdt_settimeout(WDT_DEFAULT_TIME);
  276. pr_info("wdt_time value must be 1 <= wdt_time <= 256, using %d\n",
  277. wdt_time);
  278. }
  279. return platform_driver_register(&at91wdt_driver);
  280. }
  281. static void __exit at91_wdt_exit(void)
  282. {
  283. platform_driver_unregister(&at91wdt_driver);
  284. }
  285. module_init(at91_wdt_init);
  286. module_exit(at91_wdt_exit);
  287. MODULE_AUTHOR("Andrew Victor");
  288. MODULE_DESCRIPTION("Watchdog driver for Atmel AT91RM9200");
  289. MODULE_LICENSE("GPL");
  290. MODULE_ALIAS("platform:atmel_st_watchdog");