at91rm9200_wdt.c 7.6 KB

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