imx2_wdt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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, IMX2_WDT_WCR, 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, IMX2_WDT_WCR, wcr_enable);
  89. regmap_write(wdev->regmap, IMX2_WDT_WCR, 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 void __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. }
  142. static int imx2_wdt_set_timeout(struct watchdog_device *wdog,
  143. unsigned int new_timeout)
  144. {
  145. __imx2_wdt_set_timeout(wdog, new_timeout);
  146. wdog->timeout = new_timeout;
  147. return 0;
  148. }
  149. static int imx2_wdt_start(struct watchdog_device *wdog)
  150. {
  151. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  152. if (imx2_wdt_is_running(wdev)) {
  153. /* delete the timer that pings the watchdog after close */
  154. del_timer_sync(&wdev->timer);
  155. imx2_wdt_set_timeout(wdog, wdog->timeout);
  156. } else
  157. imx2_wdt_setup(wdog);
  158. return imx2_wdt_ping(wdog);
  159. }
  160. static int imx2_wdt_stop(struct watchdog_device *wdog)
  161. {
  162. /*
  163. * We don't need a clk_disable, it cannot be disabled once started.
  164. * We use a timer to ping the watchdog while /dev/watchdog is closed
  165. */
  166. imx2_wdt_timer_ping((unsigned long)wdog);
  167. return 0;
  168. }
  169. static inline void imx2_wdt_ping_if_active(struct watchdog_device *wdog)
  170. {
  171. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  172. if (imx2_wdt_is_running(wdev)) {
  173. imx2_wdt_set_timeout(wdog, wdog->timeout);
  174. imx2_wdt_timer_ping((unsigned long)wdog);
  175. }
  176. }
  177. static const struct watchdog_ops imx2_wdt_ops = {
  178. .owner = THIS_MODULE,
  179. .start = imx2_wdt_start,
  180. .stop = imx2_wdt_stop,
  181. .ping = imx2_wdt_ping,
  182. .set_timeout = imx2_wdt_set_timeout,
  183. };
  184. static const struct regmap_config imx2_wdt_regmap_config = {
  185. .reg_bits = 16,
  186. .reg_stride = 2,
  187. .val_bits = 16,
  188. .max_register = 0x8,
  189. };
  190. static int __init imx2_wdt_probe(struct platform_device *pdev)
  191. {
  192. struct imx2_wdt_device *wdev;
  193. struct watchdog_device *wdog;
  194. struct resource *res;
  195. void __iomem *base;
  196. int ret;
  197. u32 val;
  198. wdev = devm_kzalloc(&pdev->dev, sizeof(*wdev), GFP_KERNEL);
  199. if (!wdev)
  200. return -ENOMEM;
  201. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  202. base = devm_ioremap_resource(&pdev->dev, res);
  203. if (IS_ERR(base))
  204. return PTR_ERR(base);
  205. wdev->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
  206. &imx2_wdt_regmap_config);
  207. if (IS_ERR(wdev->regmap)) {
  208. dev_err(&pdev->dev, "regmap init failed\n");
  209. return PTR_ERR(wdev->regmap);
  210. }
  211. wdev->clk = devm_clk_get(&pdev->dev, NULL);
  212. if (IS_ERR(wdev->clk)) {
  213. dev_err(&pdev->dev, "can't get Watchdog clock\n");
  214. return PTR_ERR(wdev->clk);
  215. }
  216. wdog = &wdev->wdog;
  217. wdog->info = &imx2_wdt_info;
  218. wdog->ops = &imx2_wdt_ops;
  219. wdog->min_timeout = 1;
  220. wdog->max_timeout = IMX2_WDT_MAX_TIME;
  221. wdog->parent = &pdev->dev;
  222. ret = clk_prepare_enable(wdev->clk);
  223. if (ret)
  224. return ret;
  225. regmap_read(wdev->regmap, IMX2_WDT_WRSR, &val);
  226. wdog->bootstatus = val & IMX2_WDT_WRSR_TOUT ? WDIOF_CARDRESET : 0;
  227. wdog->timeout = clamp_t(unsigned, timeout, 1, IMX2_WDT_MAX_TIME);
  228. if (wdog->timeout != timeout)
  229. dev_warn(&pdev->dev, "Initial timeout out of range! Clamped from %u to %u\n",
  230. timeout, wdog->timeout);
  231. platform_set_drvdata(pdev, wdog);
  232. watchdog_set_drvdata(wdog, wdev);
  233. watchdog_set_nowayout(wdog, nowayout);
  234. watchdog_init_timeout(wdog, timeout, &pdev->dev);
  235. setup_timer(&wdev->timer, imx2_wdt_timer_ping, (unsigned long)wdog);
  236. imx2_wdt_ping_if_active(wdog);
  237. /*
  238. * Disable the watchdog power down counter at boot. Otherwise the power
  239. * down counter will pull down the #WDOG interrupt line for one clock
  240. * cycle.
  241. */
  242. regmap_write(wdev->regmap, IMX2_WDT_WMCR, 0);
  243. ret = watchdog_register_device(wdog);
  244. if (ret) {
  245. dev_err(&pdev->dev, "cannot register watchdog device\n");
  246. goto disable_clk;
  247. }
  248. wdev->restart_handler.notifier_call = imx2_restart_handler;
  249. wdev->restart_handler.priority = 128;
  250. ret = register_restart_handler(&wdev->restart_handler);
  251. if (ret)
  252. dev_err(&pdev->dev, "cannot register restart handler\n");
  253. dev_info(&pdev->dev, "timeout %d sec (nowayout=%d)\n",
  254. wdog->timeout, nowayout);
  255. return 0;
  256. disable_clk:
  257. clk_disable_unprepare(wdev->clk);
  258. return ret;
  259. }
  260. static int __exit imx2_wdt_remove(struct platform_device *pdev)
  261. {
  262. struct watchdog_device *wdog = platform_get_drvdata(pdev);
  263. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  264. unregister_restart_handler(&wdev->restart_handler);
  265. watchdog_unregister_device(wdog);
  266. if (imx2_wdt_is_running(wdev)) {
  267. del_timer_sync(&wdev->timer);
  268. imx2_wdt_ping(wdog);
  269. dev_crit(&pdev->dev, "Device removed: Expect reboot!\n");
  270. }
  271. return 0;
  272. }
  273. static void imx2_wdt_shutdown(struct platform_device *pdev)
  274. {
  275. struct watchdog_device *wdog = platform_get_drvdata(pdev);
  276. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  277. if (imx2_wdt_is_running(wdev)) {
  278. /*
  279. * We are running, we need to delete the timer but will
  280. * give max timeout before reboot will take place
  281. */
  282. del_timer_sync(&wdev->timer);
  283. imx2_wdt_set_timeout(wdog, IMX2_WDT_MAX_TIME);
  284. imx2_wdt_ping(wdog);
  285. dev_crit(&pdev->dev, "Device shutdown: Expect reboot!\n");
  286. }
  287. }
  288. #ifdef CONFIG_PM_SLEEP
  289. /* Disable watchdog if it is active or non-active but still running */
  290. static int imx2_wdt_suspend(struct device *dev)
  291. {
  292. struct watchdog_device *wdog = dev_get_drvdata(dev);
  293. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  294. /* The watchdog IP block is running */
  295. if (imx2_wdt_is_running(wdev)) {
  296. /*
  297. * Don't update wdog->timeout, we'll restore the current value
  298. * during resume.
  299. */
  300. __imx2_wdt_set_timeout(wdog, IMX2_WDT_MAX_TIME);
  301. imx2_wdt_ping(wdog);
  302. /* The watchdog is not active */
  303. if (!watchdog_active(wdog))
  304. del_timer_sync(&wdev->timer);
  305. }
  306. clk_disable_unprepare(wdev->clk);
  307. return 0;
  308. }
  309. /* Enable watchdog and configure it if necessary */
  310. static int imx2_wdt_resume(struct device *dev)
  311. {
  312. struct watchdog_device *wdog = dev_get_drvdata(dev);
  313. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  314. int ret;
  315. ret = clk_prepare_enable(wdev->clk);
  316. if (ret)
  317. return ret;
  318. if (watchdog_active(wdog) && !imx2_wdt_is_running(wdev)) {
  319. /*
  320. * If the watchdog is still active and resumes
  321. * from deep sleep state, need to restart the
  322. * watchdog again.
  323. */
  324. imx2_wdt_setup(wdog);
  325. imx2_wdt_set_timeout(wdog, wdog->timeout);
  326. imx2_wdt_ping(wdog);
  327. } else if (imx2_wdt_is_running(wdev)) {
  328. /* Resuming from non-deep sleep state. */
  329. imx2_wdt_set_timeout(wdog, wdog->timeout);
  330. imx2_wdt_ping(wdog);
  331. /*
  332. * But the watchdog is not active, then start
  333. * the timer again.
  334. */
  335. if (!watchdog_active(wdog))
  336. mod_timer(&wdev->timer,
  337. jiffies + wdog->timeout * HZ / 2);
  338. }
  339. return 0;
  340. }
  341. #endif
  342. static SIMPLE_DEV_PM_OPS(imx2_wdt_pm_ops, imx2_wdt_suspend,
  343. imx2_wdt_resume);
  344. static const struct of_device_id imx2_wdt_dt_ids[] = {
  345. { .compatible = "fsl,imx21-wdt", },
  346. { /* sentinel */ }
  347. };
  348. MODULE_DEVICE_TABLE(of, imx2_wdt_dt_ids);
  349. static struct platform_driver imx2_wdt_driver = {
  350. .remove = __exit_p(imx2_wdt_remove),
  351. .shutdown = imx2_wdt_shutdown,
  352. .driver = {
  353. .name = DRIVER_NAME,
  354. .pm = &imx2_wdt_pm_ops,
  355. .of_match_table = imx2_wdt_dt_ids,
  356. },
  357. };
  358. module_platform_driver_probe(imx2_wdt_driver, imx2_wdt_probe);
  359. MODULE_AUTHOR("Wolfram Sang");
  360. MODULE_DESCRIPTION("Watchdog driver for IMX2 and later");
  361. MODULE_LICENSE("GPL v2");
  362. MODULE_ALIAS("platform:" DRIVER_NAME);