imx2_wdt.c 12 KB

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