imx2_wdt.c 12 KB

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