dw_wdt.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright 2010-2011 Picochip Ltd., Jamie Iles
  3. * http://www.picochip.com
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. *
  10. * This file implements a driver for the Synopsys DesignWare watchdog device
  11. * in the many subsystems. The watchdog has 16 different timeout periods
  12. * and these are a function of the input clock frequency.
  13. *
  14. * The DesignWare watchdog cannot be stopped once it has been started so we
  15. * use a software timer to implement a ping that will keep the watchdog alive.
  16. * If we receive an expected close for the watchdog then we keep the timer
  17. * running, otherwise the timer is stopped and the watchdog will expire.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/bitops.h>
  21. #include <linux/clk.h>
  22. #include <linux/delay.h>
  23. #include <linux/device.h>
  24. #include <linux/err.h>
  25. #include <linux/fs.h>
  26. #include <linux/io.h>
  27. #include <linux/kernel.h>
  28. #include <linux/miscdevice.h>
  29. #include <linux/module.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/notifier.h>
  32. #include <linux/of.h>
  33. #include <linux/pm.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/reboot.h>
  36. #include <linux/timer.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/watchdog.h>
  39. #define WDOG_CONTROL_REG_OFFSET 0x00
  40. #define WDOG_CONTROL_REG_WDT_EN_MASK 0x01
  41. #define WDOG_TIMEOUT_RANGE_REG_OFFSET 0x04
  42. #define WDOG_TIMEOUT_RANGE_TOPINIT_SHIFT 4
  43. #define WDOG_CURRENT_COUNT_REG_OFFSET 0x08
  44. #define WDOG_COUNTER_RESTART_REG_OFFSET 0x0c
  45. #define WDOG_COUNTER_RESTART_KICK_VALUE 0x76
  46. /* The maximum TOP (timeout period) value that can be set in the watchdog. */
  47. #define DW_WDT_MAX_TOP 15
  48. #define DW_WDT_DEFAULT_SECONDS 30
  49. static bool nowayout = WATCHDOG_NOWAYOUT;
  50. module_param(nowayout, bool, 0);
  51. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  52. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  53. #define WDT_TIMEOUT (HZ / 2)
  54. static struct {
  55. void __iomem *regs;
  56. struct clk *clk;
  57. unsigned long in_use;
  58. unsigned long next_heartbeat;
  59. struct timer_list timer;
  60. int expect_close;
  61. struct notifier_block restart_handler;
  62. } dw_wdt;
  63. static inline int dw_wdt_is_enabled(void)
  64. {
  65. return readl(dw_wdt.regs + WDOG_CONTROL_REG_OFFSET) &
  66. WDOG_CONTROL_REG_WDT_EN_MASK;
  67. }
  68. static inline int dw_wdt_top_in_seconds(unsigned top)
  69. {
  70. /*
  71. * There are 16 possible timeout values in 0..15 where the number of
  72. * cycles is 2 ^ (16 + i) and the watchdog counts down.
  73. */
  74. return (1 << (16 + top)) / clk_get_rate(dw_wdt.clk);
  75. }
  76. static int dw_wdt_get_top(void)
  77. {
  78. int top = readl(dw_wdt.regs + WDOG_TIMEOUT_RANGE_REG_OFFSET) & 0xF;
  79. return dw_wdt_top_in_seconds(top);
  80. }
  81. static inline void dw_wdt_set_next_heartbeat(void)
  82. {
  83. dw_wdt.next_heartbeat = jiffies + dw_wdt_get_top() * HZ;
  84. }
  85. static void dw_wdt_keepalive(void)
  86. {
  87. writel(WDOG_COUNTER_RESTART_KICK_VALUE, dw_wdt.regs +
  88. WDOG_COUNTER_RESTART_REG_OFFSET);
  89. }
  90. static int dw_wdt_set_top(unsigned top_s)
  91. {
  92. int i, top_val = DW_WDT_MAX_TOP;
  93. /*
  94. * Iterate over the timeout values until we find the closest match. We
  95. * always look for >=.
  96. */
  97. for (i = 0; i <= DW_WDT_MAX_TOP; ++i)
  98. if (dw_wdt_top_in_seconds(i) >= top_s) {
  99. top_val = i;
  100. break;
  101. }
  102. /*
  103. * Set the new value in the watchdog. Some versions of dw_wdt
  104. * have have TOPINIT in the TIMEOUT_RANGE register (as per
  105. * CP_WDT_DUAL_TOP in WDT_COMP_PARAMS_1). On those we
  106. * effectively get a pat of the watchdog right here.
  107. */
  108. writel(top_val | top_val << WDOG_TIMEOUT_RANGE_TOPINIT_SHIFT,
  109. dw_wdt.regs + WDOG_TIMEOUT_RANGE_REG_OFFSET);
  110. /*
  111. * Add an explicit pat to handle versions of the watchdog that
  112. * don't have TOPINIT. This won't hurt on versions that have
  113. * it.
  114. */
  115. dw_wdt_keepalive();
  116. dw_wdt_set_next_heartbeat();
  117. return dw_wdt_top_in_seconds(top_val);
  118. }
  119. static int dw_wdt_restart_handle(struct notifier_block *this,
  120. unsigned long mode, void *cmd)
  121. {
  122. u32 val;
  123. writel(0, dw_wdt.regs + WDOG_TIMEOUT_RANGE_REG_OFFSET);
  124. val = readl(dw_wdt.regs + WDOG_CONTROL_REG_OFFSET);
  125. if (val & WDOG_CONTROL_REG_WDT_EN_MASK)
  126. writel(WDOG_COUNTER_RESTART_KICK_VALUE, dw_wdt.regs +
  127. WDOG_COUNTER_RESTART_REG_OFFSET);
  128. else
  129. writel(WDOG_CONTROL_REG_WDT_EN_MASK,
  130. dw_wdt.regs + WDOG_CONTROL_REG_OFFSET);
  131. /* wait for reset to assert... */
  132. mdelay(500);
  133. return NOTIFY_DONE;
  134. }
  135. static void dw_wdt_ping(unsigned long data)
  136. {
  137. if (time_before(jiffies, dw_wdt.next_heartbeat) ||
  138. (!nowayout && !dw_wdt.in_use)) {
  139. dw_wdt_keepalive();
  140. mod_timer(&dw_wdt.timer, jiffies + WDT_TIMEOUT);
  141. } else
  142. pr_crit("keepalive missed, machine will reset\n");
  143. }
  144. static int dw_wdt_open(struct inode *inode, struct file *filp)
  145. {
  146. if (test_and_set_bit(0, &dw_wdt.in_use))
  147. return -EBUSY;
  148. /* Make sure we don't get unloaded. */
  149. __module_get(THIS_MODULE);
  150. if (!dw_wdt_is_enabled()) {
  151. /*
  152. * The watchdog is not currently enabled. Set the timeout to
  153. * something reasonable and then start it.
  154. */
  155. dw_wdt_set_top(DW_WDT_DEFAULT_SECONDS);
  156. writel(WDOG_CONTROL_REG_WDT_EN_MASK,
  157. dw_wdt.regs + WDOG_CONTROL_REG_OFFSET);
  158. }
  159. dw_wdt_set_next_heartbeat();
  160. return nonseekable_open(inode, filp);
  161. }
  162. static ssize_t dw_wdt_write(struct file *filp, const char __user *buf,
  163. size_t len, loff_t *offset)
  164. {
  165. if (!len)
  166. return 0;
  167. if (!nowayout) {
  168. size_t i;
  169. dw_wdt.expect_close = 0;
  170. for (i = 0; i < len; ++i) {
  171. char c;
  172. if (get_user(c, buf + i))
  173. return -EFAULT;
  174. if (c == 'V') {
  175. dw_wdt.expect_close = 1;
  176. break;
  177. }
  178. }
  179. }
  180. dw_wdt_set_next_heartbeat();
  181. dw_wdt_keepalive();
  182. mod_timer(&dw_wdt.timer, jiffies + WDT_TIMEOUT);
  183. return len;
  184. }
  185. static u32 dw_wdt_time_left(void)
  186. {
  187. return readl(dw_wdt.regs + WDOG_CURRENT_COUNT_REG_OFFSET) /
  188. clk_get_rate(dw_wdt.clk);
  189. }
  190. static const struct watchdog_info dw_wdt_ident = {
  191. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  192. WDIOF_MAGICCLOSE,
  193. .identity = "Synopsys DesignWare Watchdog",
  194. };
  195. static long dw_wdt_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  196. {
  197. unsigned long val;
  198. int timeout;
  199. switch (cmd) {
  200. case WDIOC_GETSUPPORT:
  201. return copy_to_user((void __user *)arg, &dw_wdt_ident,
  202. sizeof(dw_wdt_ident)) ? -EFAULT : 0;
  203. case WDIOC_GETSTATUS:
  204. case WDIOC_GETBOOTSTATUS:
  205. return put_user(0, (int __user *)arg);
  206. case WDIOC_KEEPALIVE:
  207. dw_wdt_set_next_heartbeat();
  208. return 0;
  209. case WDIOC_SETTIMEOUT:
  210. if (get_user(val, (int __user *)arg))
  211. return -EFAULT;
  212. timeout = dw_wdt_set_top(val);
  213. return put_user(timeout , (int __user *)arg);
  214. case WDIOC_GETTIMEOUT:
  215. return put_user(dw_wdt_get_top(), (int __user *)arg);
  216. case WDIOC_GETTIMELEFT:
  217. /* Get the time left until expiry. */
  218. if (get_user(val, (int __user *)arg))
  219. return -EFAULT;
  220. return put_user(dw_wdt_time_left(), (int __user *)arg);
  221. default:
  222. return -ENOTTY;
  223. }
  224. }
  225. static int dw_wdt_release(struct inode *inode, struct file *filp)
  226. {
  227. clear_bit(0, &dw_wdt.in_use);
  228. if (!dw_wdt.expect_close) {
  229. del_timer(&dw_wdt.timer);
  230. if (!nowayout)
  231. pr_crit("unexpected close, system will reboot soon\n");
  232. else
  233. pr_crit("watchdog cannot be disabled, system will reboot soon\n");
  234. }
  235. dw_wdt.expect_close = 0;
  236. return 0;
  237. }
  238. #ifdef CONFIG_PM_SLEEP
  239. static int dw_wdt_suspend(struct device *dev)
  240. {
  241. clk_disable_unprepare(dw_wdt.clk);
  242. return 0;
  243. }
  244. static int dw_wdt_resume(struct device *dev)
  245. {
  246. int err = clk_prepare_enable(dw_wdt.clk);
  247. if (err)
  248. return err;
  249. dw_wdt_keepalive();
  250. return 0;
  251. }
  252. #endif /* CONFIG_PM_SLEEP */
  253. static SIMPLE_DEV_PM_OPS(dw_wdt_pm_ops, dw_wdt_suspend, dw_wdt_resume);
  254. static const struct file_operations wdt_fops = {
  255. .owner = THIS_MODULE,
  256. .llseek = no_llseek,
  257. .open = dw_wdt_open,
  258. .write = dw_wdt_write,
  259. .unlocked_ioctl = dw_wdt_ioctl,
  260. .release = dw_wdt_release
  261. };
  262. static struct miscdevice dw_wdt_miscdev = {
  263. .fops = &wdt_fops,
  264. .name = "watchdog",
  265. .minor = WATCHDOG_MINOR,
  266. };
  267. static int dw_wdt_drv_probe(struct platform_device *pdev)
  268. {
  269. int ret;
  270. struct resource *mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  271. dw_wdt.regs = devm_ioremap_resource(&pdev->dev, mem);
  272. if (IS_ERR(dw_wdt.regs))
  273. return PTR_ERR(dw_wdt.regs);
  274. dw_wdt.clk = devm_clk_get(&pdev->dev, NULL);
  275. if (IS_ERR(dw_wdt.clk))
  276. return PTR_ERR(dw_wdt.clk);
  277. ret = clk_prepare_enable(dw_wdt.clk);
  278. if (ret)
  279. return ret;
  280. ret = misc_register(&dw_wdt_miscdev);
  281. if (ret)
  282. goto out_disable_clk;
  283. dw_wdt.restart_handler.notifier_call = dw_wdt_restart_handle;
  284. dw_wdt.restart_handler.priority = 128;
  285. ret = register_restart_handler(&dw_wdt.restart_handler);
  286. if (ret)
  287. pr_warn("cannot register restart handler\n");
  288. dw_wdt_set_next_heartbeat();
  289. setup_timer(&dw_wdt.timer, dw_wdt_ping, 0);
  290. mod_timer(&dw_wdt.timer, jiffies + WDT_TIMEOUT);
  291. return 0;
  292. out_disable_clk:
  293. clk_disable_unprepare(dw_wdt.clk);
  294. return ret;
  295. }
  296. static int dw_wdt_drv_remove(struct platform_device *pdev)
  297. {
  298. unregister_restart_handler(&dw_wdt.restart_handler);
  299. misc_deregister(&dw_wdt_miscdev);
  300. clk_disable_unprepare(dw_wdt.clk);
  301. return 0;
  302. }
  303. #ifdef CONFIG_OF
  304. static const struct of_device_id dw_wdt_of_match[] = {
  305. { .compatible = "snps,dw-wdt", },
  306. { /* sentinel */ }
  307. };
  308. MODULE_DEVICE_TABLE(of, dw_wdt_of_match);
  309. #endif
  310. static struct platform_driver dw_wdt_driver = {
  311. .probe = dw_wdt_drv_probe,
  312. .remove = dw_wdt_drv_remove,
  313. .driver = {
  314. .name = "dw_wdt",
  315. .of_match_table = of_match_ptr(dw_wdt_of_match),
  316. .pm = &dw_wdt_pm_ops,
  317. },
  318. };
  319. module_platform_driver(dw_wdt_driver);
  320. MODULE_AUTHOR("Jamie Iles");
  321. MODULE_DESCRIPTION("Synopsys DesignWare Watchdog Driver");
  322. MODULE_LICENSE("GPL");