lpc18xx_wdt.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * NXP LPC18xx Watchdog Timer (WDT)
  3. *
  4. * Copyright (c) 2015 Ariel D'Alessandro <ariel@vanguardiasur.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * Notes
  11. * -----
  12. * The Watchdog consists of a fixed divide-by-4 clock pre-scaler and a 24-bit
  13. * counter which decrements on every clock cycle.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/watchdog.h>
  21. /* Registers */
  22. #define LPC18XX_WDT_MOD 0x00
  23. #define LPC18XX_WDT_MOD_WDEN BIT(0)
  24. #define LPC18XX_WDT_MOD_WDRESET BIT(1)
  25. #define LPC18XX_WDT_TC 0x04
  26. #define LPC18XX_WDT_TC_MIN 0xff
  27. #define LPC18XX_WDT_TC_MAX 0xffffff
  28. #define LPC18XX_WDT_FEED 0x08
  29. #define LPC18XX_WDT_FEED_MAGIC1 0xaa
  30. #define LPC18XX_WDT_FEED_MAGIC2 0x55
  31. #define LPC18XX_WDT_TV 0x0c
  32. /* Clock pre-scaler */
  33. #define LPC18XX_WDT_CLK_DIV 4
  34. /* Timeout values in seconds */
  35. #define LPC18XX_WDT_DEF_TIMEOUT 30U
  36. static int heartbeat;
  37. module_param(heartbeat, int, 0);
  38. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds (default="
  39. __MODULE_STRING(LPC18XX_WDT_DEF_TIMEOUT) ")");
  40. static bool nowayout = WATCHDOG_NOWAYOUT;
  41. module_param(nowayout, bool, 0);
  42. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  43. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  44. struct lpc18xx_wdt_dev {
  45. struct watchdog_device wdt_dev;
  46. struct clk *reg_clk;
  47. struct clk *wdt_clk;
  48. unsigned long clk_rate;
  49. void __iomem *base;
  50. struct timer_list timer;
  51. spinlock_t lock;
  52. };
  53. static int lpc18xx_wdt_feed(struct watchdog_device *wdt_dev)
  54. {
  55. struct lpc18xx_wdt_dev *lpc18xx_wdt = watchdog_get_drvdata(wdt_dev);
  56. unsigned long flags;
  57. /*
  58. * An abort condition will occur if an interrupt happens during the feed
  59. * sequence.
  60. */
  61. spin_lock_irqsave(&lpc18xx_wdt->lock, flags);
  62. writel(LPC18XX_WDT_FEED_MAGIC1, lpc18xx_wdt->base + LPC18XX_WDT_FEED);
  63. writel(LPC18XX_WDT_FEED_MAGIC2, lpc18xx_wdt->base + LPC18XX_WDT_FEED);
  64. spin_unlock_irqrestore(&lpc18xx_wdt->lock, flags);
  65. return 0;
  66. }
  67. static void lpc18xx_wdt_timer_feed(struct timer_list *t)
  68. {
  69. struct lpc18xx_wdt_dev *lpc18xx_wdt = from_timer(lpc18xx_wdt, t, timer);
  70. struct watchdog_device *wdt_dev = &lpc18xx_wdt->wdt_dev;
  71. lpc18xx_wdt_feed(wdt_dev);
  72. /* Use safe value (1/2 of real timeout) */
  73. mod_timer(&lpc18xx_wdt->timer, jiffies +
  74. msecs_to_jiffies((wdt_dev->timeout * MSEC_PER_SEC) / 2));
  75. }
  76. /*
  77. * Since LPC18xx Watchdog cannot be disabled in hardware, we must keep feeding
  78. * it with a timer until userspace watchdog software takes over.
  79. */
  80. static int lpc18xx_wdt_stop(struct watchdog_device *wdt_dev)
  81. {
  82. struct lpc18xx_wdt_dev *lpc18xx_wdt = watchdog_get_drvdata(wdt_dev);
  83. lpc18xx_wdt_timer_feed(&lpc18xx_wdt->timer);
  84. return 0;
  85. }
  86. static void __lpc18xx_wdt_set_timeout(struct lpc18xx_wdt_dev *lpc18xx_wdt)
  87. {
  88. unsigned int val;
  89. val = DIV_ROUND_UP(lpc18xx_wdt->wdt_dev.timeout * lpc18xx_wdt->clk_rate,
  90. LPC18XX_WDT_CLK_DIV);
  91. writel(val, lpc18xx_wdt->base + LPC18XX_WDT_TC);
  92. }
  93. static int lpc18xx_wdt_set_timeout(struct watchdog_device *wdt_dev,
  94. unsigned int new_timeout)
  95. {
  96. struct lpc18xx_wdt_dev *lpc18xx_wdt = watchdog_get_drvdata(wdt_dev);
  97. lpc18xx_wdt->wdt_dev.timeout = new_timeout;
  98. __lpc18xx_wdt_set_timeout(lpc18xx_wdt);
  99. return 0;
  100. }
  101. static unsigned int lpc18xx_wdt_get_timeleft(struct watchdog_device *wdt_dev)
  102. {
  103. struct lpc18xx_wdt_dev *lpc18xx_wdt = watchdog_get_drvdata(wdt_dev);
  104. unsigned int val;
  105. val = readl(lpc18xx_wdt->base + LPC18XX_WDT_TV);
  106. return (val * LPC18XX_WDT_CLK_DIV) / lpc18xx_wdt->clk_rate;
  107. }
  108. static int lpc18xx_wdt_start(struct watchdog_device *wdt_dev)
  109. {
  110. struct lpc18xx_wdt_dev *lpc18xx_wdt = watchdog_get_drvdata(wdt_dev);
  111. unsigned int val;
  112. if (timer_pending(&lpc18xx_wdt->timer))
  113. del_timer(&lpc18xx_wdt->timer);
  114. val = readl(lpc18xx_wdt->base + LPC18XX_WDT_MOD);
  115. val |= LPC18XX_WDT_MOD_WDEN;
  116. val |= LPC18XX_WDT_MOD_WDRESET;
  117. writel(val, lpc18xx_wdt->base + LPC18XX_WDT_MOD);
  118. /*
  119. * Setting the WDEN bit in the WDMOD register is not sufficient to
  120. * enable the Watchdog. A valid feed sequence must be completed after
  121. * setting WDEN before the Watchdog is capable of generating a reset.
  122. */
  123. lpc18xx_wdt_feed(wdt_dev);
  124. return 0;
  125. }
  126. static int lpc18xx_wdt_restart(struct watchdog_device *wdt_dev,
  127. unsigned long action, void *data)
  128. {
  129. struct lpc18xx_wdt_dev *lpc18xx_wdt = watchdog_get_drvdata(wdt_dev);
  130. unsigned long flags;
  131. int val;
  132. /*
  133. * Incorrect feed sequence causes immediate watchdog reset if enabled.
  134. */
  135. spin_lock_irqsave(&lpc18xx_wdt->lock, flags);
  136. val = readl(lpc18xx_wdt->base + LPC18XX_WDT_MOD);
  137. val |= LPC18XX_WDT_MOD_WDEN;
  138. val |= LPC18XX_WDT_MOD_WDRESET;
  139. writel(val, lpc18xx_wdt->base + LPC18XX_WDT_MOD);
  140. writel(LPC18XX_WDT_FEED_MAGIC1, lpc18xx_wdt->base + LPC18XX_WDT_FEED);
  141. writel(LPC18XX_WDT_FEED_MAGIC2, lpc18xx_wdt->base + LPC18XX_WDT_FEED);
  142. writel(LPC18XX_WDT_FEED_MAGIC1, lpc18xx_wdt->base + LPC18XX_WDT_FEED);
  143. writel(LPC18XX_WDT_FEED_MAGIC1, lpc18xx_wdt->base + LPC18XX_WDT_FEED);
  144. spin_unlock_irqrestore(&lpc18xx_wdt->lock, flags);
  145. return 0;
  146. }
  147. static const struct watchdog_info lpc18xx_wdt_info = {
  148. .identity = "NXP LPC18xx Watchdog",
  149. .options = WDIOF_SETTIMEOUT |
  150. WDIOF_KEEPALIVEPING |
  151. WDIOF_MAGICCLOSE,
  152. };
  153. static const struct watchdog_ops lpc18xx_wdt_ops = {
  154. .owner = THIS_MODULE,
  155. .start = lpc18xx_wdt_start,
  156. .stop = lpc18xx_wdt_stop,
  157. .ping = lpc18xx_wdt_feed,
  158. .set_timeout = lpc18xx_wdt_set_timeout,
  159. .get_timeleft = lpc18xx_wdt_get_timeleft,
  160. .restart = lpc18xx_wdt_restart,
  161. };
  162. static int lpc18xx_wdt_probe(struct platform_device *pdev)
  163. {
  164. struct lpc18xx_wdt_dev *lpc18xx_wdt;
  165. struct device *dev = &pdev->dev;
  166. struct resource *res;
  167. int ret;
  168. lpc18xx_wdt = devm_kzalloc(dev, sizeof(*lpc18xx_wdt), GFP_KERNEL);
  169. if (!lpc18xx_wdt)
  170. return -ENOMEM;
  171. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  172. lpc18xx_wdt->base = devm_ioremap_resource(dev, res);
  173. if (IS_ERR(lpc18xx_wdt->base))
  174. return PTR_ERR(lpc18xx_wdt->base);
  175. lpc18xx_wdt->reg_clk = devm_clk_get(dev, "reg");
  176. if (IS_ERR(lpc18xx_wdt->reg_clk)) {
  177. dev_err(dev, "failed to get the reg clock\n");
  178. return PTR_ERR(lpc18xx_wdt->reg_clk);
  179. }
  180. lpc18xx_wdt->wdt_clk = devm_clk_get(dev, "wdtclk");
  181. if (IS_ERR(lpc18xx_wdt->wdt_clk)) {
  182. dev_err(dev, "failed to get the wdt clock\n");
  183. return PTR_ERR(lpc18xx_wdt->wdt_clk);
  184. }
  185. ret = clk_prepare_enable(lpc18xx_wdt->reg_clk);
  186. if (ret) {
  187. dev_err(dev, "could not prepare or enable sys clock\n");
  188. return ret;
  189. }
  190. ret = clk_prepare_enable(lpc18xx_wdt->wdt_clk);
  191. if (ret) {
  192. dev_err(dev, "could not prepare or enable wdt clock\n");
  193. goto disable_reg_clk;
  194. }
  195. /* We use the clock rate to calculate timeouts */
  196. lpc18xx_wdt->clk_rate = clk_get_rate(lpc18xx_wdt->wdt_clk);
  197. if (lpc18xx_wdt->clk_rate == 0) {
  198. dev_err(dev, "failed to get clock rate\n");
  199. ret = -EINVAL;
  200. goto disable_wdt_clk;
  201. }
  202. lpc18xx_wdt->wdt_dev.info = &lpc18xx_wdt_info;
  203. lpc18xx_wdt->wdt_dev.ops = &lpc18xx_wdt_ops;
  204. lpc18xx_wdt->wdt_dev.min_timeout = DIV_ROUND_UP(LPC18XX_WDT_TC_MIN *
  205. LPC18XX_WDT_CLK_DIV, lpc18xx_wdt->clk_rate);
  206. lpc18xx_wdt->wdt_dev.max_timeout = (LPC18XX_WDT_TC_MAX *
  207. LPC18XX_WDT_CLK_DIV) / lpc18xx_wdt->clk_rate;
  208. lpc18xx_wdt->wdt_dev.timeout = min(lpc18xx_wdt->wdt_dev.max_timeout,
  209. LPC18XX_WDT_DEF_TIMEOUT);
  210. spin_lock_init(&lpc18xx_wdt->lock);
  211. lpc18xx_wdt->wdt_dev.parent = dev;
  212. watchdog_set_drvdata(&lpc18xx_wdt->wdt_dev, lpc18xx_wdt);
  213. watchdog_init_timeout(&lpc18xx_wdt->wdt_dev, heartbeat, dev);
  214. __lpc18xx_wdt_set_timeout(lpc18xx_wdt);
  215. timer_setup(&lpc18xx_wdt->timer, lpc18xx_wdt_timer_feed, 0);
  216. watchdog_set_nowayout(&lpc18xx_wdt->wdt_dev, nowayout);
  217. watchdog_set_restart_priority(&lpc18xx_wdt->wdt_dev, 128);
  218. platform_set_drvdata(pdev, lpc18xx_wdt);
  219. ret = watchdog_register_device(&lpc18xx_wdt->wdt_dev);
  220. if (ret)
  221. goto disable_wdt_clk;
  222. return 0;
  223. disable_wdt_clk:
  224. clk_disable_unprepare(lpc18xx_wdt->wdt_clk);
  225. disable_reg_clk:
  226. clk_disable_unprepare(lpc18xx_wdt->reg_clk);
  227. return ret;
  228. }
  229. static void lpc18xx_wdt_shutdown(struct platform_device *pdev)
  230. {
  231. struct lpc18xx_wdt_dev *lpc18xx_wdt = platform_get_drvdata(pdev);
  232. lpc18xx_wdt_stop(&lpc18xx_wdt->wdt_dev);
  233. }
  234. static int lpc18xx_wdt_remove(struct platform_device *pdev)
  235. {
  236. struct lpc18xx_wdt_dev *lpc18xx_wdt = platform_get_drvdata(pdev);
  237. dev_warn(&pdev->dev, "I quit now, hardware will probably reboot!\n");
  238. del_timer(&lpc18xx_wdt->timer);
  239. watchdog_unregister_device(&lpc18xx_wdt->wdt_dev);
  240. clk_disable_unprepare(lpc18xx_wdt->wdt_clk);
  241. clk_disable_unprepare(lpc18xx_wdt->reg_clk);
  242. return 0;
  243. }
  244. static const struct of_device_id lpc18xx_wdt_match[] = {
  245. { .compatible = "nxp,lpc1850-wwdt" },
  246. {}
  247. };
  248. MODULE_DEVICE_TABLE(of, lpc18xx_wdt_match);
  249. static struct platform_driver lpc18xx_wdt_driver = {
  250. .driver = {
  251. .name = "lpc18xx-wdt",
  252. .of_match_table = lpc18xx_wdt_match,
  253. },
  254. .probe = lpc18xx_wdt_probe,
  255. .remove = lpc18xx_wdt_remove,
  256. .shutdown = lpc18xx_wdt_shutdown,
  257. };
  258. module_platform_driver(lpc18xx_wdt_driver);
  259. MODULE_AUTHOR("Ariel D'Alessandro <ariel@vanguardiasur.com.ar>");
  260. MODULE_DESCRIPTION("NXP LPC18xx Watchdog Timer Driver");
  261. MODULE_LICENSE("GPL v2");