lpc18xx_wdt.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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(unsigned long data)
  68. {
  69. struct watchdog_device *wdt_dev = (struct watchdog_device *)data;
  70. struct lpc18xx_wdt_dev *lpc18xx_wdt = watchdog_get_drvdata(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. lpc18xx_wdt_timer_feed((unsigned long)wdt_dev);
  83. return 0;
  84. }
  85. static void __lpc18xx_wdt_set_timeout(struct lpc18xx_wdt_dev *lpc18xx_wdt)
  86. {
  87. unsigned int val;
  88. val = DIV_ROUND_UP(lpc18xx_wdt->wdt_dev.timeout * lpc18xx_wdt->clk_rate,
  89. LPC18XX_WDT_CLK_DIV);
  90. writel(val, lpc18xx_wdt->base + LPC18XX_WDT_TC);
  91. }
  92. static int lpc18xx_wdt_set_timeout(struct watchdog_device *wdt_dev,
  93. unsigned int new_timeout)
  94. {
  95. struct lpc18xx_wdt_dev *lpc18xx_wdt = watchdog_get_drvdata(wdt_dev);
  96. lpc18xx_wdt->wdt_dev.timeout = new_timeout;
  97. __lpc18xx_wdt_set_timeout(lpc18xx_wdt);
  98. return 0;
  99. }
  100. static unsigned int lpc18xx_wdt_get_timeleft(struct watchdog_device *wdt_dev)
  101. {
  102. struct lpc18xx_wdt_dev *lpc18xx_wdt = watchdog_get_drvdata(wdt_dev);
  103. unsigned int val;
  104. val = readl(lpc18xx_wdt->base + LPC18XX_WDT_TV);
  105. return (val * LPC18XX_WDT_CLK_DIV) / lpc18xx_wdt->clk_rate;
  106. }
  107. static int lpc18xx_wdt_start(struct watchdog_device *wdt_dev)
  108. {
  109. struct lpc18xx_wdt_dev *lpc18xx_wdt = watchdog_get_drvdata(wdt_dev);
  110. unsigned int val;
  111. if (timer_pending(&lpc18xx_wdt->timer))
  112. del_timer(&lpc18xx_wdt->timer);
  113. val = readl(lpc18xx_wdt->base + LPC18XX_WDT_MOD);
  114. val |= LPC18XX_WDT_MOD_WDEN;
  115. val |= LPC18XX_WDT_MOD_WDRESET;
  116. writel(val, lpc18xx_wdt->base + LPC18XX_WDT_MOD);
  117. /*
  118. * Setting the WDEN bit in the WDMOD register is not sufficient to
  119. * enable the Watchdog. A valid feed sequence must be completed after
  120. * setting WDEN before the Watchdog is capable of generating a reset.
  121. */
  122. lpc18xx_wdt_feed(wdt_dev);
  123. return 0;
  124. }
  125. static int lpc18xx_wdt_restart(struct watchdog_device *wdt_dev,
  126. unsigned long action, void *data)
  127. {
  128. struct lpc18xx_wdt_dev *lpc18xx_wdt = watchdog_get_drvdata(wdt_dev);
  129. unsigned long flags;
  130. int val;
  131. /*
  132. * Incorrect feed sequence causes immediate watchdog reset if enabled.
  133. */
  134. spin_lock_irqsave(&lpc18xx_wdt->lock, flags);
  135. val = readl(lpc18xx_wdt->base + LPC18XX_WDT_MOD);
  136. val |= LPC18XX_WDT_MOD_WDEN;
  137. val |= LPC18XX_WDT_MOD_WDRESET;
  138. writel(val, lpc18xx_wdt->base + LPC18XX_WDT_MOD);
  139. writel(LPC18XX_WDT_FEED_MAGIC1, lpc18xx_wdt->base + LPC18XX_WDT_FEED);
  140. writel(LPC18XX_WDT_FEED_MAGIC2, lpc18xx_wdt->base + LPC18XX_WDT_FEED);
  141. writel(LPC18XX_WDT_FEED_MAGIC1, lpc18xx_wdt->base + LPC18XX_WDT_FEED);
  142. writel(LPC18XX_WDT_FEED_MAGIC1, lpc18xx_wdt->base + LPC18XX_WDT_FEED);
  143. spin_unlock_irqrestore(&lpc18xx_wdt->lock, flags);
  144. return 0;
  145. }
  146. static struct watchdog_info lpc18xx_wdt_info = {
  147. .identity = "NXP LPC18xx Watchdog",
  148. .options = WDIOF_SETTIMEOUT |
  149. WDIOF_KEEPALIVEPING |
  150. WDIOF_MAGICCLOSE,
  151. };
  152. static const struct watchdog_ops lpc18xx_wdt_ops = {
  153. .owner = THIS_MODULE,
  154. .start = lpc18xx_wdt_start,
  155. .stop = lpc18xx_wdt_stop,
  156. .ping = lpc18xx_wdt_feed,
  157. .set_timeout = lpc18xx_wdt_set_timeout,
  158. .get_timeleft = lpc18xx_wdt_get_timeleft,
  159. .restart = lpc18xx_wdt_restart,
  160. };
  161. static int lpc18xx_wdt_probe(struct platform_device *pdev)
  162. {
  163. struct lpc18xx_wdt_dev *lpc18xx_wdt;
  164. struct device *dev = &pdev->dev;
  165. struct resource *res;
  166. int ret;
  167. lpc18xx_wdt = devm_kzalloc(dev, sizeof(*lpc18xx_wdt), GFP_KERNEL);
  168. if (!lpc18xx_wdt)
  169. return -ENOMEM;
  170. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  171. lpc18xx_wdt->base = devm_ioremap_resource(dev, res);
  172. if (IS_ERR(lpc18xx_wdt->base))
  173. return PTR_ERR(lpc18xx_wdt->base);
  174. lpc18xx_wdt->reg_clk = devm_clk_get(dev, "reg");
  175. if (IS_ERR(lpc18xx_wdt->reg_clk)) {
  176. dev_err(dev, "failed to get the reg clock\n");
  177. return PTR_ERR(lpc18xx_wdt->reg_clk);
  178. }
  179. lpc18xx_wdt->wdt_clk = devm_clk_get(dev, "wdtclk");
  180. if (IS_ERR(lpc18xx_wdt->wdt_clk)) {
  181. dev_err(dev, "failed to get the wdt clock\n");
  182. return PTR_ERR(lpc18xx_wdt->wdt_clk);
  183. }
  184. ret = clk_prepare_enable(lpc18xx_wdt->reg_clk);
  185. if (ret) {
  186. dev_err(dev, "could not prepare or enable sys clock\n");
  187. return ret;
  188. }
  189. ret = clk_prepare_enable(lpc18xx_wdt->wdt_clk);
  190. if (ret) {
  191. dev_err(dev, "could not prepare or enable wdt clock\n");
  192. goto disable_reg_clk;
  193. }
  194. /* We use the clock rate to calculate timeouts */
  195. lpc18xx_wdt->clk_rate = clk_get_rate(lpc18xx_wdt->wdt_clk);
  196. if (lpc18xx_wdt->clk_rate == 0) {
  197. dev_err(dev, "failed to get clock rate\n");
  198. ret = -EINVAL;
  199. goto disable_wdt_clk;
  200. }
  201. lpc18xx_wdt->wdt_dev.info = &lpc18xx_wdt_info;
  202. lpc18xx_wdt->wdt_dev.ops = &lpc18xx_wdt_ops;
  203. lpc18xx_wdt->wdt_dev.min_timeout = DIV_ROUND_UP(LPC18XX_WDT_TC_MIN *
  204. LPC18XX_WDT_CLK_DIV, lpc18xx_wdt->clk_rate);
  205. lpc18xx_wdt->wdt_dev.max_timeout = (LPC18XX_WDT_TC_MAX *
  206. LPC18XX_WDT_CLK_DIV) / lpc18xx_wdt->clk_rate;
  207. lpc18xx_wdt->wdt_dev.timeout = min(lpc18xx_wdt->wdt_dev.max_timeout,
  208. LPC18XX_WDT_DEF_TIMEOUT);
  209. spin_lock_init(&lpc18xx_wdt->lock);
  210. lpc18xx_wdt->wdt_dev.parent = dev;
  211. watchdog_set_drvdata(&lpc18xx_wdt->wdt_dev, lpc18xx_wdt);
  212. ret = watchdog_init_timeout(&lpc18xx_wdt->wdt_dev, heartbeat, dev);
  213. __lpc18xx_wdt_set_timeout(lpc18xx_wdt);
  214. setup_timer(&lpc18xx_wdt->timer, lpc18xx_wdt_timer_feed,
  215. (unsigned long)&lpc18xx_wdt->wdt_dev);
  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");