imx2_wdt.c 12 KB

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