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. <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. struct resource *res;
  204. void __iomem *base;
  205. int ret;
  206. u32 val;
  207. wdev = devm_kzalloc(&pdev->dev, sizeof(*wdev), GFP_KERNEL);
  208. if (!wdev)
  209. return -ENOMEM;
  210. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  211. base = devm_ioremap_resource(&pdev->dev, res);
  212. if (IS_ERR(base))
  213. return PTR_ERR(base);
  214. wdev->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
  215. &imx2_wdt_regmap_config);
  216. if (IS_ERR(wdev->regmap)) {
  217. dev_err(&pdev->dev, "regmap init failed\n");
  218. return PTR_ERR(wdev->regmap);
  219. }
  220. wdev->clk = devm_clk_get(&pdev->dev, NULL);
  221. if (IS_ERR(wdev->clk)) {
  222. dev_err(&pdev->dev, "can't get Watchdog clock\n");
  223. return PTR_ERR(wdev->clk);
  224. }
  225. wdog = &wdev->wdog;
  226. wdog->info = &imx2_wdt_info;
  227. wdog->ops = &imx2_wdt_ops;
  228. wdog->min_timeout = 1;
  229. wdog->timeout = IMX2_WDT_DEFAULT_TIME;
  230. wdog->max_hw_heartbeat_ms = IMX2_WDT_MAX_TIME * 1000;
  231. wdog->parent = &pdev->dev;
  232. ret = platform_get_irq(pdev, 0);
  233. if (ret > 0)
  234. if (!devm_request_irq(&pdev->dev, ret, imx2_wdt_isr, 0,
  235. dev_name(&pdev->dev), wdog))
  236. wdog->info = &imx2_wdt_pretimeout_info;
  237. ret = clk_prepare_enable(wdev->clk);
  238. if (ret)
  239. return ret;
  240. regmap_read(wdev->regmap, IMX2_WDT_WRSR, &val);
  241. wdog->bootstatus = val & IMX2_WDT_WRSR_TOUT ? WDIOF_CARDRESET : 0;
  242. wdev->ext_reset = of_property_read_bool(pdev->dev.of_node,
  243. "fsl,ext-reset-output");
  244. platform_set_drvdata(pdev, wdog);
  245. watchdog_set_drvdata(wdog, wdev);
  246. watchdog_set_nowayout(wdog, nowayout);
  247. watchdog_set_restart_priority(wdog, 128);
  248. watchdog_init_timeout(wdog, timeout, &pdev->dev);
  249. if (imx2_wdt_is_running(wdev)) {
  250. imx2_wdt_set_timeout(wdog, wdog->timeout);
  251. set_bit(WDOG_HW_RUNNING, &wdog->status);
  252. }
  253. /*
  254. * Disable the watchdog power down counter at boot. Otherwise the power
  255. * down counter will pull down the #WDOG interrupt line for one clock
  256. * cycle.
  257. */
  258. regmap_write(wdev->regmap, IMX2_WDT_WMCR, 0);
  259. ret = watchdog_register_device(wdog);
  260. if (ret) {
  261. dev_err(&pdev->dev, "cannot register watchdog device\n");
  262. goto disable_clk;
  263. }
  264. dev_info(&pdev->dev, "timeout %d sec (nowayout=%d)\n",
  265. wdog->timeout, nowayout);
  266. return 0;
  267. disable_clk:
  268. clk_disable_unprepare(wdev->clk);
  269. return ret;
  270. }
  271. static int __exit imx2_wdt_remove(struct platform_device *pdev)
  272. {
  273. struct watchdog_device *wdog = platform_get_drvdata(pdev);
  274. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  275. watchdog_unregister_device(wdog);
  276. if (imx2_wdt_is_running(wdev)) {
  277. imx2_wdt_ping(wdog);
  278. dev_crit(&pdev->dev, "Device removed: Expect reboot!\n");
  279. }
  280. return 0;
  281. }
  282. static void imx2_wdt_shutdown(struct platform_device *pdev)
  283. {
  284. struct watchdog_device *wdog = platform_get_drvdata(pdev);
  285. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  286. if (imx2_wdt_is_running(wdev)) {
  287. /*
  288. * We are running, configure max timeout before reboot
  289. * will take place.
  290. */
  291. imx2_wdt_set_timeout(wdog, IMX2_WDT_MAX_TIME);
  292. imx2_wdt_ping(wdog);
  293. dev_crit(&pdev->dev, "Device shutdown: Expect reboot!\n");
  294. }
  295. }
  296. #ifdef CONFIG_PM_SLEEP
  297. /* Disable watchdog if it is active or non-active but still running */
  298. static int imx2_wdt_suspend(struct device *dev)
  299. {
  300. struct watchdog_device *wdog = dev_get_drvdata(dev);
  301. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  302. /* The watchdog IP block is running */
  303. if (imx2_wdt_is_running(wdev)) {
  304. /*
  305. * Don't update wdog->timeout, we'll restore the current value
  306. * during resume.
  307. */
  308. __imx2_wdt_set_timeout(wdog, IMX2_WDT_MAX_TIME);
  309. imx2_wdt_ping(wdog);
  310. }
  311. clk_disable_unprepare(wdev->clk);
  312. return 0;
  313. }
  314. /* Enable watchdog and configure it if necessary */
  315. static int imx2_wdt_resume(struct device *dev)
  316. {
  317. struct watchdog_device *wdog = dev_get_drvdata(dev);
  318. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  319. int ret;
  320. ret = clk_prepare_enable(wdev->clk);
  321. if (ret)
  322. return ret;
  323. if (watchdog_active(wdog) && !imx2_wdt_is_running(wdev)) {
  324. /*
  325. * If the watchdog is still active and resumes
  326. * from deep sleep state, need to restart the
  327. * watchdog again.
  328. */
  329. imx2_wdt_setup(wdog);
  330. }
  331. if (imx2_wdt_is_running(wdev)) {
  332. imx2_wdt_set_timeout(wdog, wdog->timeout);
  333. imx2_wdt_ping(wdog);
  334. }
  335. return 0;
  336. }
  337. #endif
  338. static SIMPLE_DEV_PM_OPS(imx2_wdt_pm_ops, imx2_wdt_suspend,
  339. imx2_wdt_resume);
  340. static const struct of_device_id imx2_wdt_dt_ids[] = {
  341. { .compatible = "fsl,imx21-wdt", },
  342. { /* sentinel */ }
  343. };
  344. MODULE_DEVICE_TABLE(of, imx2_wdt_dt_ids);
  345. static struct platform_driver imx2_wdt_driver = {
  346. .remove = __exit_p(imx2_wdt_remove),
  347. .shutdown = imx2_wdt_shutdown,
  348. .driver = {
  349. .name = DRIVER_NAME,
  350. .pm = &imx2_wdt_pm_ops,
  351. .of_match_table = imx2_wdt_dt_ids,
  352. },
  353. };
  354. module_platform_driver_probe(imx2_wdt_driver, imx2_wdt_probe);
  355. MODULE_AUTHOR("Wolfram Sang");
  356. MODULE_DESCRIPTION("Watchdog driver for IMX2 and later");
  357. MODULE_LICENSE("GPL v2");
  358. MODULE_ALIAS("platform:" DRIVER_NAME);