imx2_wdt.c 12 KB

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