lpc18xx_wdt.c 9.4 KB

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