imx2_wdt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Watchdog driver for IMX2 and later processors
  4. *
  5. * Copyright (C) 2010 Wolfram Sang, Pengutronix e.K. <kernel@pengutronix.de>
  6. * Copyright (C) 2014 Freescale Semiconductor, Inc.
  7. *
  8. * some parts adapted by similar drivers from Darius Augulis and Vladimir
  9. * Zapolskiy, additional improvements by Wim Van Sebroeck.
  10. *
  11. * NOTE: MX1 has a slightly different Watchdog than MX2 and later:
  12. *
  13. * MX1: MX2+:
  14. * ---- -----
  15. * Registers: 32-bit 16-bit
  16. * Stopable timer: Yes No
  17. * Need to enable clk: No Yes
  18. * Halt on suspend: Manual Can be automatic
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/of.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/regmap.h>
  31. #include <linux/watchdog.h>
  32. #define DRIVER_NAME "imx2-wdt"
  33. #define IMX2_WDT_WCR 0x00 /* Control Register */
  34. #define IMX2_WDT_WCR_WT (0xFF << 8) /* -> Watchdog Timeout Field */
  35. #define IMX2_WDT_WCR_WDW BIT(7) /* -> Watchdog disable for WAIT */
  36. #define IMX2_WDT_WCR_WDA BIT(5) /* -> External Reset WDOG_B */
  37. #define IMX2_WDT_WCR_SRS BIT(4) /* -> Software Reset Signal */
  38. #define IMX2_WDT_WCR_WRE BIT(3) /* -> WDOG Reset Enable */
  39. #define IMX2_WDT_WCR_WDE BIT(2) /* -> Watchdog Enable */
  40. #define IMX2_WDT_WCR_WDZST BIT(0) /* -> Watchdog timer Suspend */
  41. #define IMX2_WDT_WSR 0x02 /* Service Register */
  42. #define IMX2_WDT_SEQ1 0x5555 /* -> service sequence 1 */
  43. #define IMX2_WDT_SEQ2 0xAAAA /* -> service sequence 2 */
  44. #define IMX2_WDT_WRSR 0x04 /* Reset Status Register */
  45. #define IMX2_WDT_WRSR_TOUT BIT(1) /* -> Reset due to Timeout */
  46. #define IMX2_WDT_WICR 0x06 /* Interrupt Control Register */
  47. #define IMX2_WDT_WICR_WIE BIT(15) /* -> Interrupt Enable */
  48. #define IMX2_WDT_WICR_WTIS BIT(14) /* -> Interrupt Status */
  49. #define IMX2_WDT_WICR_WICT 0xFF /* -> Interrupt Count Timeout */
  50. #define IMX2_WDT_WMCR 0x08 /* Misc Register */
  51. #define IMX2_WDT_MAX_TIME 128U
  52. #define IMX2_WDT_DEFAULT_TIME 60 /* in seconds */
  53. #define WDOG_SEC_TO_COUNT(s) ((s * 2 - 1) << 8)
  54. struct imx2_wdt_data {
  55. bool wdw_supported;
  56. };
  57. struct imx2_wdt_device {
  58. struct clk *clk;
  59. struct regmap *regmap;
  60. struct watchdog_device wdog;
  61. const struct imx2_wdt_data *data;
  62. bool ext_reset;
  63. bool clk_is_on;
  64. bool no_ping;
  65. bool sleep_wait;
  66. };
  67. static bool nowayout = WATCHDOG_NOWAYOUT;
  68. module_param(nowayout, bool, 0);
  69. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  70. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  71. static unsigned timeout;
  72. module_param(timeout, uint, 0);
  73. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
  74. __MODULE_STRING(IMX2_WDT_DEFAULT_TIME) ")");
  75. static const struct watchdog_info imx2_wdt_info = {
  76. .identity = "imx2+ watchdog",
  77. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  78. };
  79. static const struct watchdog_info imx2_wdt_pretimeout_info = {
  80. .identity = "imx2+ watchdog",
  81. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
  82. WDIOF_PRETIMEOUT,
  83. };
  84. static int imx2_wdt_restart(struct watchdog_device *wdog, unsigned long action,
  85. void *data)
  86. {
  87. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  88. unsigned int wcr_enable = IMX2_WDT_WCR_WDE;
  89. /* Use internal reset or external - not both */
  90. if (wdev->ext_reset)
  91. wcr_enable |= IMX2_WDT_WCR_SRS; /* do not assert int reset */
  92. else
  93. wcr_enable |= IMX2_WDT_WCR_WDA; /* do not assert ext-reset */
  94. /* Assert SRS signal */
  95. regmap_write(wdev->regmap, IMX2_WDT_WCR, wcr_enable);
  96. /*
  97. * Due to imx6q errata ERR004346 (WDOG: WDOG SRS bit requires to be
  98. * written twice), we add another two writes to ensure there must be at
  99. * least two writes happen in the same one 32kHz clock period. We save
  100. * the target check here, since the writes shouldn't be a huge burden
  101. * for other platforms.
  102. */
  103. regmap_write(wdev->regmap, IMX2_WDT_WCR, wcr_enable);
  104. regmap_write(wdev->regmap, IMX2_WDT_WCR, wcr_enable);
  105. /* wait for reset to assert... */
  106. mdelay(500);
  107. return 0;
  108. }
  109. static inline void imx2_wdt_setup(struct watchdog_device *wdog)
  110. {
  111. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  112. u32 val;
  113. regmap_read(wdev->regmap, IMX2_WDT_WCR, &val);
  114. /* Suspend timer in low power mode, write once-only */
  115. val |= IMX2_WDT_WCR_WDZST;
  116. /* Suspend timer in low power WAIT mode, write once-only */
  117. if (wdev->sleep_wait)
  118. val |= IMX2_WDT_WCR_WDW;
  119. /* Strip the old watchdog Time-Out value */
  120. val &= ~IMX2_WDT_WCR_WT;
  121. /* Generate internal chip-level reset if WDOG times out */
  122. if (!wdev->ext_reset)
  123. val &= ~IMX2_WDT_WCR_WRE;
  124. /* Or if external-reset assert WDOG_B reset only on time-out */
  125. else
  126. val |= IMX2_WDT_WCR_WRE;
  127. /* Keep Watchdog Disabled */
  128. val &= ~IMX2_WDT_WCR_WDE;
  129. /* Set the watchdog's Time-Out value */
  130. val |= WDOG_SEC_TO_COUNT(wdog->timeout);
  131. regmap_write(wdev->regmap, IMX2_WDT_WCR, val);
  132. /* enable the watchdog */
  133. val |= IMX2_WDT_WCR_WDE;
  134. regmap_write(wdev->regmap, IMX2_WDT_WCR, val);
  135. }
  136. static inline bool imx2_wdt_is_running(struct imx2_wdt_device *wdev)
  137. {
  138. u32 val;
  139. regmap_read(wdev->regmap, IMX2_WDT_WCR, &val);
  140. return val & IMX2_WDT_WCR_WDE;
  141. }
  142. static int imx2_wdt_ping(struct watchdog_device *wdog)
  143. {
  144. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  145. if (!wdev->clk_is_on)
  146. return 0;
  147. regmap_write(wdev->regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ1);
  148. regmap_write(wdev->regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ2);
  149. return 0;
  150. }
  151. static void __imx2_wdt_set_timeout(struct watchdog_device *wdog,
  152. unsigned int new_timeout)
  153. {
  154. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  155. regmap_update_bits(wdev->regmap, IMX2_WDT_WCR, IMX2_WDT_WCR_WT,
  156. WDOG_SEC_TO_COUNT(new_timeout));
  157. }
  158. static int imx2_wdt_set_timeout(struct watchdog_device *wdog,
  159. unsigned int new_timeout)
  160. {
  161. unsigned int actual;
  162. actual = min(new_timeout, IMX2_WDT_MAX_TIME);
  163. __imx2_wdt_set_timeout(wdog, actual);
  164. wdog->timeout = new_timeout;
  165. return 0;
  166. }
  167. static int imx2_wdt_set_pretimeout(struct watchdog_device *wdog,
  168. unsigned int new_pretimeout)
  169. {
  170. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  171. if (new_pretimeout >= IMX2_WDT_MAX_TIME)
  172. return -EINVAL;
  173. wdog->pretimeout = new_pretimeout;
  174. regmap_update_bits(wdev->regmap, IMX2_WDT_WICR,
  175. IMX2_WDT_WICR_WIE | IMX2_WDT_WICR_WICT,
  176. IMX2_WDT_WICR_WIE | (new_pretimeout << 1));
  177. return 0;
  178. }
  179. static irqreturn_t imx2_wdt_isr(int irq, void *wdog_arg)
  180. {
  181. struct watchdog_device *wdog = wdog_arg;
  182. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  183. regmap_write_bits(wdev->regmap, IMX2_WDT_WICR,
  184. IMX2_WDT_WICR_WTIS, IMX2_WDT_WICR_WTIS);
  185. watchdog_notify_pretimeout(wdog);
  186. return IRQ_HANDLED;
  187. }
  188. static int imx2_wdt_start(struct watchdog_device *wdog)
  189. {
  190. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  191. if (imx2_wdt_is_running(wdev))
  192. imx2_wdt_set_timeout(wdog, wdog->timeout);
  193. else
  194. imx2_wdt_setup(wdog);
  195. set_bit(WDOG_HW_RUNNING, &wdog->status);
  196. return imx2_wdt_ping(wdog);
  197. }
  198. static const struct watchdog_ops imx2_wdt_ops = {
  199. .owner = THIS_MODULE,
  200. .start = imx2_wdt_start,
  201. .ping = imx2_wdt_ping,
  202. .set_timeout = imx2_wdt_set_timeout,
  203. .set_pretimeout = imx2_wdt_set_pretimeout,
  204. .restart = imx2_wdt_restart,
  205. };
  206. static const struct regmap_config imx2_wdt_regmap_config = {
  207. .reg_bits = 16,
  208. .reg_stride = 2,
  209. .val_bits = 16,
  210. .max_register = 0x8,
  211. };
  212. static void imx2_wdt_action(void *data)
  213. {
  214. clk_disable_unprepare(data);
  215. }
  216. static int __init imx2_wdt_probe(struct platform_device *pdev)
  217. {
  218. struct device *dev = &pdev->dev;
  219. struct imx2_wdt_device *wdev;
  220. struct watchdog_device *wdog;
  221. void __iomem *base;
  222. int ret;
  223. u32 val;
  224. wdev = devm_kzalloc(dev, sizeof(*wdev), GFP_KERNEL);
  225. if (!wdev)
  226. return -ENOMEM;
  227. base = devm_platform_ioremap_resource(pdev, 0);
  228. if (IS_ERR(base))
  229. return PTR_ERR(base);
  230. wdev->regmap = devm_regmap_init_mmio_clk(dev, NULL, base,
  231. &imx2_wdt_regmap_config);
  232. if (IS_ERR(wdev->regmap)) {
  233. dev_err(dev, "regmap init failed\n");
  234. return PTR_ERR(wdev->regmap);
  235. }
  236. wdev->clk = devm_clk_get(dev, NULL);
  237. if (IS_ERR(wdev->clk)) {
  238. dev_err(dev, "can't get Watchdog clock\n");
  239. return PTR_ERR(wdev->clk);
  240. }
  241. wdog = &wdev->wdog;
  242. wdog->info = &imx2_wdt_info;
  243. wdog->ops = &imx2_wdt_ops;
  244. wdog->min_timeout = 1;
  245. wdog->timeout = IMX2_WDT_DEFAULT_TIME;
  246. wdog->max_hw_heartbeat_ms = IMX2_WDT_MAX_TIME * 1000;
  247. wdog->parent = dev;
  248. wdev->data = of_device_get_match_data(dev);
  249. ret = platform_get_irq(pdev, 0);
  250. if (ret > 0)
  251. if (!devm_request_irq(dev, ret, imx2_wdt_isr, 0,
  252. dev_name(dev), wdog))
  253. wdog->info = &imx2_wdt_pretimeout_info;
  254. ret = clk_prepare_enable(wdev->clk);
  255. if (ret)
  256. return ret;
  257. ret = devm_add_action_or_reset(dev, imx2_wdt_action, wdev->clk);
  258. if (ret)
  259. return ret;
  260. wdev->clk_is_on = true;
  261. regmap_read(wdev->regmap, IMX2_WDT_WRSR, &val);
  262. wdog->bootstatus = val & IMX2_WDT_WRSR_TOUT ? WDIOF_CARDRESET : 0;
  263. wdev->ext_reset = of_property_read_bool(dev->of_node,
  264. "fsl,ext-reset-output");
  265. if (of_property_read_bool(dev->of_node, "fsl,suspend-in-wait")) {
  266. if (!wdev->data->wdw_supported) {
  267. dev_err(dev, "suspend-in-wait not supported\n");
  268. return -EINVAL;
  269. }
  270. wdev->sleep_wait = true;
  271. }
  272. /*
  273. * The i.MX7D doesn't support low power mode, so we need to ping the watchdog
  274. * during suspend. Interaction with "fsl,suspend-in-wait" is unknown!
  275. */
  276. wdev->no_ping = !of_device_is_compatible(dev->of_node, "fsl,imx7d-wdt");
  277. platform_set_drvdata(pdev, wdog);
  278. watchdog_set_drvdata(wdog, wdev);
  279. watchdog_set_nowayout(wdog, nowayout);
  280. watchdog_set_restart_priority(wdog, 128);
  281. watchdog_init_timeout(wdog, timeout, dev);
  282. if (wdev->no_ping)
  283. watchdog_stop_ping_on_suspend(wdog);
  284. if (imx2_wdt_is_running(wdev)) {
  285. imx2_wdt_set_timeout(wdog, wdog->timeout);
  286. set_bit(WDOG_HW_RUNNING, &wdog->status);
  287. }
  288. /*
  289. * Disable the watchdog power down counter at boot. Otherwise the power
  290. * down counter will pull down the #WDOG interrupt line for one clock
  291. * cycle.
  292. */
  293. regmap_write(wdev->regmap, IMX2_WDT_WMCR, 0);
  294. return devm_watchdog_register_device(dev, wdog);
  295. }
  296. static void imx2_wdt_shutdown(struct platform_device *pdev)
  297. {
  298. struct watchdog_device *wdog = platform_get_drvdata(pdev);
  299. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  300. if (imx2_wdt_is_running(wdev)) {
  301. /*
  302. * We are running, configure max timeout before reboot
  303. * will take place.
  304. */
  305. imx2_wdt_set_timeout(wdog, IMX2_WDT_MAX_TIME);
  306. imx2_wdt_ping(wdog);
  307. dev_crit(&pdev->dev, "Device shutdown.\n");
  308. }
  309. }
  310. /* Disable watchdog if it is active or non-active but still running */
  311. static int imx2_wdt_suspend(struct device *dev)
  312. {
  313. struct watchdog_device *wdog = dev_get_drvdata(dev);
  314. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  315. /* The watchdog IP block is running */
  316. if (imx2_wdt_is_running(wdev)) {
  317. /*
  318. * Don't update wdog->timeout, we'll restore the current value
  319. * during resume.
  320. */
  321. __imx2_wdt_set_timeout(wdog, IMX2_WDT_MAX_TIME);
  322. imx2_wdt_ping(wdog);
  323. }
  324. if (wdev->no_ping) {
  325. clk_disable_unprepare(wdev->clk);
  326. wdev->clk_is_on = false;
  327. }
  328. return 0;
  329. }
  330. /* Enable watchdog and configure it if necessary */
  331. static int imx2_wdt_resume(struct device *dev)
  332. {
  333. struct watchdog_device *wdog = dev_get_drvdata(dev);
  334. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  335. int ret;
  336. if (wdev->no_ping) {
  337. ret = clk_prepare_enable(wdev->clk);
  338. if (ret)
  339. return ret;
  340. wdev->clk_is_on = true;
  341. }
  342. if (watchdog_active(wdog) && !imx2_wdt_is_running(wdev)) {
  343. /*
  344. * If the watchdog is still active and resumes
  345. * from deep sleep state, need to restart the
  346. * watchdog again.
  347. */
  348. imx2_wdt_setup(wdog);
  349. }
  350. if (imx2_wdt_is_running(wdev)) {
  351. imx2_wdt_set_timeout(wdog, wdog->timeout);
  352. imx2_wdt_ping(wdog);
  353. }
  354. return 0;
  355. }
  356. static DEFINE_SIMPLE_DEV_PM_OPS(imx2_wdt_pm_ops, imx2_wdt_suspend,
  357. imx2_wdt_resume);
  358. static struct imx2_wdt_data imx_wdt = {
  359. .wdw_supported = true,
  360. };
  361. static struct imx2_wdt_data imx_wdt_legacy = {
  362. .wdw_supported = false,
  363. };
  364. static const struct of_device_id imx2_wdt_dt_ids[] = {
  365. { .compatible = "fsl,imx21-wdt", .data = &imx_wdt_legacy },
  366. { .compatible = "fsl,imx25-wdt", .data = &imx_wdt },
  367. { .compatible = "fsl,imx27-wdt", .data = &imx_wdt_legacy },
  368. { .compatible = "fsl,imx31-wdt", .data = &imx_wdt_legacy },
  369. { .compatible = "fsl,imx35-wdt", .data = &imx_wdt },
  370. { .compatible = "fsl,imx50-wdt", .data = &imx_wdt },
  371. { .compatible = "fsl,imx51-wdt", .data = &imx_wdt },
  372. { .compatible = "fsl,imx53-wdt", .data = &imx_wdt },
  373. { .compatible = "fsl,imx6q-wdt", .data = &imx_wdt },
  374. { .compatible = "fsl,imx6sl-wdt", .data = &imx_wdt },
  375. { .compatible = "fsl,imx6sll-wdt", .data = &imx_wdt },
  376. { .compatible = "fsl,imx6sx-wdt", .data = &imx_wdt },
  377. { .compatible = "fsl,imx6ul-wdt", .data = &imx_wdt },
  378. { .compatible = "fsl,imx7d-wdt", .data = &imx_wdt },
  379. { .compatible = "fsl,imx8mm-wdt", .data = &imx_wdt },
  380. { .compatible = "fsl,imx8mn-wdt", .data = &imx_wdt },
  381. { .compatible = "fsl,imx8mp-wdt", .data = &imx_wdt },
  382. { .compatible = "fsl,imx8mq-wdt", .data = &imx_wdt },
  383. { .compatible = "fsl,ls1012a-wdt", .data = &imx_wdt_legacy },
  384. { .compatible = "fsl,ls1043a-wdt", .data = &imx_wdt_legacy },
  385. { .compatible = "fsl,vf610-wdt", .data = &imx_wdt },
  386. { /* sentinel */ }
  387. };
  388. MODULE_DEVICE_TABLE(of, imx2_wdt_dt_ids);
  389. static struct platform_driver imx2_wdt_driver = {
  390. .shutdown = imx2_wdt_shutdown,
  391. .driver = {
  392. .name = DRIVER_NAME,
  393. .pm = pm_sleep_ptr(&imx2_wdt_pm_ops),
  394. .of_match_table = imx2_wdt_dt_ids,
  395. },
  396. };
  397. module_platform_driver_probe(imx2_wdt_driver, imx2_wdt_probe);
  398. MODULE_AUTHOR("Wolfram Sang");
  399. MODULE_DESCRIPTION("Watchdog driver for IMX2 and later");
  400. MODULE_LICENSE("GPL v2");
  401. MODULE_ALIAS("platform:" DRIVER_NAME);