imx2_wdt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Watchdog driver for IMX2 and later processors
  3. *
  4. * Copyright (C) 2010 Wolfram Sang, Pengutronix e.K. <w.sang@pengutronix.de>
  5. * Copyright (C) 2014 Freescale Semiconductor, Inc.
  6. *
  7. * some parts adapted by similar drivers from Darius Augulis and Vladimir
  8. * Zapolskiy, additional improvements by Wim Van Sebroeck.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published by
  12. * the Free Software Foundation.
  13. *
  14. * NOTE: MX1 has a slightly different Watchdog than MX2 and later:
  15. *
  16. * MX1: MX2+:
  17. * ---- -----
  18. * Registers: 32-bit 16-bit
  19. * Stopable timer: Yes No
  20. * Need to enable clk: No Yes
  21. * Halt on suspend: Manual Can be automatic
  22. */
  23. #include <linux/clk.h>
  24. #include <linux/delay.h>
  25. #include <linux/init.h>
  26. #include <linux/io.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/notifier.h>
  32. #include <linux/of_address.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/reboot.h>
  35. #include <linux/regmap.h>
  36. #include <linux/timer.h>
  37. #include <linux/watchdog.h>
  38. #define DRIVER_NAME "imx2-wdt"
  39. #define IMX2_WDT_WCR 0x00 /* Control Register */
  40. #define IMX2_WDT_WCR_WT (0xFF << 8) /* -> Watchdog Timeout Field */
  41. #define IMX2_WDT_WCR_WRE (1 << 3) /* -> WDOG Reset Enable */
  42. #define IMX2_WDT_WCR_WDE (1 << 2) /* -> Watchdog Enable */
  43. #define IMX2_WDT_WCR_WDZST (1 << 0) /* -> Watchdog timer Suspend */
  44. #define IMX2_WDT_WSR 0x02 /* Service Register */
  45. #define IMX2_WDT_SEQ1 0x5555 /* -> service sequence 1 */
  46. #define IMX2_WDT_SEQ2 0xAAAA /* -> service sequence 2 */
  47. #define IMX2_WDT_WRSR 0x04 /* Reset Status Register */
  48. #define IMX2_WDT_WRSR_TOUT (1 << 1) /* -> Reset due to Timeout */
  49. #define IMX2_WDT_WMCR 0x08 /* Misc Register */
  50. #define IMX2_WDT_MAX_TIME 128
  51. #define IMX2_WDT_DEFAULT_TIME 60 /* in seconds */
  52. #define WDOG_SEC_TO_COUNT(s) ((s * 2 - 1) << 8)
  53. struct imx2_wdt_device {
  54. struct clk *clk;
  55. struct regmap *regmap;
  56. struct timer_list timer; /* Pings the watchdog when closed */
  57. struct watchdog_device wdog;
  58. struct notifier_block restart_handler;
  59. };
  60. static bool nowayout = WATCHDOG_NOWAYOUT;
  61. module_param(nowayout, bool, 0);
  62. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  63. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  64. static unsigned timeout = IMX2_WDT_DEFAULT_TIME;
  65. module_param(timeout, uint, 0);
  66. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
  67. __MODULE_STRING(IMX2_WDT_DEFAULT_TIME) ")");
  68. static const struct watchdog_info imx2_wdt_info = {
  69. .identity = "imx2+ watchdog",
  70. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  71. };
  72. static int imx2_restart_handler(struct notifier_block *this, unsigned long mode,
  73. void *cmd)
  74. {
  75. unsigned int wcr_enable = IMX2_WDT_WCR_WDE;
  76. struct imx2_wdt_device *wdev = container_of(this,
  77. struct imx2_wdt_device,
  78. restart_handler);
  79. /* Assert SRS signal */
  80. regmap_write(wdev->regmap, 0, wcr_enable);
  81. /*
  82. * Due to imx6q errata ERR004346 (WDOG: WDOG SRS bit requires to be
  83. * written twice), we add another two writes to ensure there must be at
  84. * least two writes happen in the same one 32kHz clock period. We save
  85. * the target check here, since the writes shouldn't be a huge burden
  86. * for other platforms.
  87. */
  88. regmap_write(wdev->regmap, 0, wcr_enable);
  89. regmap_write(wdev->regmap, 0, wcr_enable);
  90. /* wait for reset to assert... */
  91. mdelay(500);
  92. return NOTIFY_DONE;
  93. }
  94. static inline void imx2_wdt_setup(struct watchdog_device *wdog)
  95. {
  96. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  97. u32 val;
  98. regmap_read(wdev->regmap, IMX2_WDT_WCR, &val);
  99. /* Suspend timer in low power mode, write once-only */
  100. val |= IMX2_WDT_WCR_WDZST;
  101. /* Strip the old watchdog Time-Out value */
  102. val &= ~IMX2_WDT_WCR_WT;
  103. /* Generate reset if WDOG times out */
  104. val &= ~IMX2_WDT_WCR_WRE;
  105. /* Keep Watchdog Disabled */
  106. val &= ~IMX2_WDT_WCR_WDE;
  107. /* Set the watchdog's Time-Out value */
  108. val |= WDOG_SEC_TO_COUNT(wdog->timeout);
  109. regmap_write(wdev->regmap, IMX2_WDT_WCR, val);
  110. /* enable the watchdog */
  111. val |= IMX2_WDT_WCR_WDE;
  112. regmap_write(wdev->regmap, IMX2_WDT_WCR, val);
  113. }
  114. static inline bool imx2_wdt_is_running(struct imx2_wdt_device *wdev)
  115. {
  116. u32 val;
  117. regmap_read(wdev->regmap, IMX2_WDT_WCR, &val);
  118. return val & IMX2_WDT_WCR_WDE;
  119. }
  120. static int imx2_wdt_ping(struct watchdog_device *wdog)
  121. {
  122. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  123. regmap_write(wdev->regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ1);
  124. regmap_write(wdev->regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ2);
  125. return 0;
  126. }
  127. static void imx2_wdt_timer_ping(unsigned long arg)
  128. {
  129. struct watchdog_device *wdog = (struct watchdog_device *)arg;
  130. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  131. /* ping it every wdog->timeout / 2 seconds to prevent reboot */
  132. imx2_wdt_ping(wdog);
  133. mod_timer(&wdev->timer, jiffies + wdog->timeout * HZ / 2);
  134. }
  135. static int imx2_wdt_set_timeout(struct watchdog_device *wdog,
  136. unsigned int new_timeout)
  137. {
  138. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  139. regmap_update_bits(wdev->regmap, IMX2_WDT_WCR, IMX2_WDT_WCR_WT,
  140. WDOG_SEC_TO_COUNT(new_timeout));
  141. return 0;
  142. }
  143. static int imx2_wdt_start(struct watchdog_device *wdog)
  144. {
  145. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  146. if (imx2_wdt_is_running(wdev)) {
  147. /* delete the timer that pings the watchdog after close */
  148. del_timer_sync(&wdev->timer);
  149. imx2_wdt_set_timeout(wdog, wdog->timeout);
  150. } else
  151. imx2_wdt_setup(wdog);
  152. return imx2_wdt_ping(wdog);
  153. }
  154. static int imx2_wdt_stop(struct watchdog_device *wdog)
  155. {
  156. /*
  157. * We don't need a clk_disable, it cannot be disabled once started.
  158. * We use a timer to ping the watchdog while /dev/watchdog is closed
  159. */
  160. imx2_wdt_timer_ping((unsigned long)wdog);
  161. return 0;
  162. }
  163. static inline void imx2_wdt_ping_if_active(struct watchdog_device *wdog)
  164. {
  165. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  166. if (imx2_wdt_is_running(wdev)) {
  167. imx2_wdt_set_timeout(wdog, wdog->timeout);
  168. imx2_wdt_timer_ping((unsigned long)wdog);
  169. }
  170. }
  171. static const struct watchdog_ops imx2_wdt_ops = {
  172. .owner = THIS_MODULE,
  173. .start = imx2_wdt_start,
  174. .stop = imx2_wdt_stop,
  175. .ping = imx2_wdt_ping,
  176. .set_timeout = imx2_wdt_set_timeout,
  177. };
  178. static const struct regmap_config imx2_wdt_regmap_config = {
  179. .reg_bits = 16,
  180. .reg_stride = 2,
  181. .val_bits = 16,
  182. .max_register = 0x8,
  183. };
  184. static int __init imx2_wdt_probe(struct platform_device *pdev)
  185. {
  186. struct imx2_wdt_device *wdev;
  187. struct watchdog_device *wdog;
  188. struct resource *res;
  189. void __iomem *base;
  190. int ret;
  191. u32 val;
  192. wdev = devm_kzalloc(&pdev->dev, sizeof(*wdev), GFP_KERNEL);
  193. if (!wdev)
  194. return -ENOMEM;
  195. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  196. base = devm_ioremap_resource(&pdev->dev, res);
  197. if (IS_ERR(base))
  198. return PTR_ERR(base);
  199. wdev->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
  200. &imx2_wdt_regmap_config);
  201. if (IS_ERR(wdev->regmap)) {
  202. dev_err(&pdev->dev, "regmap init failed\n");
  203. return PTR_ERR(wdev->regmap);
  204. }
  205. wdev->clk = devm_clk_get(&pdev->dev, NULL);
  206. if (IS_ERR(wdev->clk)) {
  207. dev_err(&pdev->dev, "can't get Watchdog clock\n");
  208. return PTR_ERR(wdev->clk);
  209. }
  210. wdog = &wdev->wdog;
  211. wdog->info = &imx2_wdt_info;
  212. wdog->ops = &imx2_wdt_ops;
  213. wdog->min_timeout = 1;
  214. wdog->max_timeout = IMX2_WDT_MAX_TIME;
  215. clk_prepare_enable(wdev->clk);
  216. regmap_read(wdev->regmap, IMX2_WDT_WRSR, &val);
  217. wdog->bootstatus = val & IMX2_WDT_WRSR_TOUT ? WDIOF_CARDRESET : 0;
  218. wdog->timeout = clamp_t(unsigned, timeout, 1, IMX2_WDT_MAX_TIME);
  219. if (wdog->timeout != timeout)
  220. dev_warn(&pdev->dev, "Initial timeout out of range! Clamped from %u to %u\n",
  221. timeout, wdog->timeout);
  222. platform_set_drvdata(pdev, wdog);
  223. watchdog_set_drvdata(wdog, wdev);
  224. watchdog_set_nowayout(wdog, nowayout);
  225. watchdog_init_timeout(wdog, timeout, &pdev->dev);
  226. setup_timer(&wdev->timer, imx2_wdt_timer_ping, (unsigned long)wdog);
  227. imx2_wdt_ping_if_active(wdog);
  228. /*
  229. * Disable the watchdog power down counter at boot. Otherwise the power
  230. * down counter will pull down the #WDOG interrupt line for one clock
  231. * cycle.
  232. */
  233. regmap_write(wdev->regmap, IMX2_WDT_WMCR, 0);
  234. ret = watchdog_register_device(wdog);
  235. if (ret) {
  236. dev_err(&pdev->dev, "cannot register watchdog device\n");
  237. return ret;
  238. }
  239. wdev->restart_handler.notifier_call = imx2_restart_handler;
  240. wdev->restart_handler.priority = 128;
  241. ret = register_restart_handler(&wdev->restart_handler);
  242. if (ret)
  243. dev_err(&pdev->dev, "cannot register restart handler\n");
  244. dev_info(&pdev->dev, "timeout %d sec (nowayout=%d)\n",
  245. wdog->timeout, nowayout);
  246. return 0;
  247. }
  248. static int __exit imx2_wdt_remove(struct platform_device *pdev)
  249. {
  250. struct watchdog_device *wdog = platform_get_drvdata(pdev);
  251. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  252. unregister_restart_handler(&wdev->restart_handler);
  253. watchdog_unregister_device(wdog);
  254. if (imx2_wdt_is_running(wdev)) {
  255. del_timer_sync(&wdev->timer);
  256. imx2_wdt_ping(wdog);
  257. dev_crit(&pdev->dev, "Device removed: Expect reboot!\n");
  258. }
  259. return 0;
  260. }
  261. static void imx2_wdt_shutdown(struct platform_device *pdev)
  262. {
  263. struct watchdog_device *wdog = platform_get_drvdata(pdev);
  264. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  265. if (imx2_wdt_is_running(wdev)) {
  266. /*
  267. * We are running, we need to delete the timer but will
  268. * give max timeout before reboot will take place
  269. */
  270. del_timer_sync(&wdev->timer);
  271. imx2_wdt_set_timeout(wdog, IMX2_WDT_MAX_TIME);
  272. imx2_wdt_ping(wdog);
  273. dev_crit(&pdev->dev, "Device shutdown: Expect reboot!\n");
  274. }
  275. }
  276. #ifdef CONFIG_PM_SLEEP
  277. /* Disable watchdog if it is active or non-active but still running */
  278. static int imx2_wdt_suspend(struct device *dev)
  279. {
  280. struct watchdog_device *wdog = dev_get_drvdata(dev);
  281. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  282. /* The watchdog IP block is running */
  283. if (imx2_wdt_is_running(wdev)) {
  284. imx2_wdt_set_timeout(wdog, IMX2_WDT_MAX_TIME);
  285. imx2_wdt_ping(wdog);
  286. /* The watchdog is not active */
  287. if (!watchdog_active(wdog))
  288. del_timer_sync(&wdev->timer);
  289. }
  290. clk_disable_unprepare(wdev->clk);
  291. return 0;
  292. }
  293. /* Enable watchdog and configure it if necessary */
  294. static int imx2_wdt_resume(struct device *dev)
  295. {
  296. struct watchdog_device *wdog = dev_get_drvdata(dev);
  297. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  298. clk_prepare_enable(wdev->clk);
  299. if (watchdog_active(wdog) && !imx2_wdt_is_running(wdev)) {
  300. /*
  301. * If the watchdog is still active and resumes
  302. * from deep sleep state, need to restart the
  303. * watchdog again.
  304. */
  305. imx2_wdt_setup(wdog);
  306. imx2_wdt_set_timeout(wdog, wdog->timeout);
  307. imx2_wdt_ping(wdog);
  308. } else if (imx2_wdt_is_running(wdev)) {
  309. /* Resuming from non-deep sleep state. */
  310. imx2_wdt_set_timeout(wdog, wdog->timeout);
  311. imx2_wdt_ping(wdog);
  312. /*
  313. * But the watchdog is not active, then start
  314. * the timer again.
  315. */
  316. if (!watchdog_active(wdog))
  317. mod_timer(&wdev->timer,
  318. jiffies + wdog->timeout * HZ / 2);
  319. }
  320. return 0;
  321. }
  322. #endif
  323. static SIMPLE_DEV_PM_OPS(imx2_wdt_pm_ops, imx2_wdt_suspend,
  324. imx2_wdt_resume);
  325. static const struct of_device_id imx2_wdt_dt_ids[] = {
  326. { .compatible = "fsl,imx21-wdt", },
  327. { /* sentinel */ }
  328. };
  329. MODULE_DEVICE_TABLE(of, imx2_wdt_dt_ids);
  330. static struct platform_driver imx2_wdt_driver = {
  331. .remove = __exit_p(imx2_wdt_remove),
  332. .shutdown = imx2_wdt_shutdown,
  333. .driver = {
  334. .name = DRIVER_NAME,
  335. .pm = &imx2_wdt_pm_ops,
  336. .of_match_table = imx2_wdt_dt_ids,
  337. },
  338. };
  339. module_platform_driver_probe(imx2_wdt_driver, imx2_wdt_probe);
  340. MODULE_AUTHOR("Wolfram Sang");
  341. MODULE_DESCRIPTION("Watchdog driver for IMX2 and later");
  342. MODULE_LICENSE("GPL v2");
  343. MODULE_ALIAS("platform:" DRIVER_NAME);