imx2_wdt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 int 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. wdog->timeout = new_timeout;
  146. regmap_update_bits(wdev->regmap, IMX2_WDT_WCR, IMX2_WDT_WCR_WT,
  147. WDOG_SEC_TO_COUNT(new_timeout));
  148. return 0;
  149. }
  150. static int imx2_wdt_set_pretimeout(struct watchdog_device *wdog,
  151. unsigned int new_pretimeout)
  152. {
  153. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  154. if (new_pretimeout >= IMX2_WDT_MAX_TIME)
  155. return -EINVAL;
  156. wdog->pretimeout = new_pretimeout;
  157. regmap_update_bits(wdev->regmap, IMX2_WDT_WICR,
  158. IMX2_WDT_WICR_WIE | IMX2_WDT_WICR_WICT,
  159. IMX2_WDT_WICR_WIE | (new_pretimeout << 1));
  160. return 0;
  161. }
  162. static irqreturn_t imx2_wdt_isr(int irq, void *wdog_arg)
  163. {
  164. struct watchdog_device *wdog = wdog_arg;
  165. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  166. regmap_write_bits(wdev->regmap, IMX2_WDT_WICR,
  167. IMX2_WDT_WICR_WTIS, IMX2_WDT_WICR_WTIS);
  168. watchdog_notify_pretimeout(wdog);
  169. return IRQ_HANDLED;
  170. }
  171. static int imx2_wdt_start(struct watchdog_device *wdog)
  172. {
  173. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  174. if (imx2_wdt_is_running(wdev))
  175. imx2_wdt_set_timeout(wdog, wdog->timeout);
  176. else
  177. imx2_wdt_setup(wdog);
  178. set_bit(WDOG_HW_RUNNING, &wdog->status);
  179. return imx2_wdt_ping(wdog);
  180. }
  181. static const struct watchdog_ops imx2_wdt_ops = {
  182. .owner = THIS_MODULE,
  183. .start = imx2_wdt_start,
  184. .ping = imx2_wdt_ping,
  185. .set_timeout = imx2_wdt_set_timeout,
  186. .set_pretimeout = imx2_wdt_set_pretimeout,
  187. .restart = imx2_wdt_restart,
  188. };
  189. static const struct regmap_config imx2_wdt_regmap_config = {
  190. .reg_bits = 16,
  191. .reg_stride = 2,
  192. .val_bits = 16,
  193. .max_register = 0x8,
  194. };
  195. static int __init imx2_wdt_probe(struct platform_device *pdev)
  196. {
  197. struct imx2_wdt_device *wdev;
  198. struct watchdog_device *wdog;
  199. struct resource *res;
  200. void __iomem *base;
  201. int ret;
  202. u32 val;
  203. wdev = devm_kzalloc(&pdev->dev, sizeof(*wdev), GFP_KERNEL);
  204. if (!wdev)
  205. return -ENOMEM;
  206. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  207. base = devm_ioremap_resource(&pdev->dev, res);
  208. if (IS_ERR(base))
  209. return PTR_ERR(base);
  210. wdev->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
  211. &imx2_wdt_regmap_config);
  212. if (IS_ERR(wdev->regmap)) {
  213. dev_err(&pdev->dev, "regmap init failed\n");
  214. return PTR_ERR(wdev->regmap);
  215. }
  216. wdev->clk = devm_clk_get(&pdev->dev, NULL);
  217. if (IS_ERR(wdev->clk)) {
  218. dev_err(&pdev->dev, "can't get Watchdog clock\n");
  219. return PTR_ERR(wdev->clk);
  220. }
  221. wdog = &wdev->wdog;
  222. wdog->info = &imx2_wdt_info;
  223. wdog->ops = &imx2_wdt_ops;
  224. wdog->min_timeout = 1;
  225. wdog->max_hw_heartbeat_ms = IMX2_WDT_MAX_TIME * 1000;
  226. wdog->parent = &pdev->dev;
  227. ret = platform_get_irq(pdev, 0);
  228. if (ret > 0)
  229. if (!devm_request_irq(&pdev->dev, ret, imx2_wdt_isr, 0,
  230. dev_name(&pdev->dev), wdog))
  231. wdog->info = &imx2_wdt_pretimeout_info;
  232. ret = clk_prepare_enable(wdev->clk);
  233. if (ret)
  234. return ret;
  235. regmap_read(wdev->regmap, IMX2_WDT_WRSR, &val);
  236. wdog->bootstatus = val & IMX2_WDT_WRSR_TOUT ? WDIOF_CARDRESET : 0;
  237. wdev->ext_reset = of_property_read_bool(pdev->dev.of_node,
  238. "fsl,ext-reset-output");
  239. wdog->timeout = clamp_t(unsigned, timeout, 1, IMX2_WDT_MAX_TIME);
  240. if (wdog->timeout != timeout)
  241. dev_warn(&pdev->dev, "Initial timeout out of range! Clamped from %u to %u\n",
  242. timeout, wdog->timeout);
  243. platform_set_drvdata(pdev, wdog);
  244. watchdog_set_drvdata(wdog, wdev);
  245. watchdog_set_nowayout(wdog, nowayout);
  246. watchdog_set_restart_priority(wdog, 128);
  247. watchdog_init_timeout(wdog, timeout, &pdev->dev);
  248. if (imx2_wdt_is_running(wdev)) {
  249. imx2_wdt_set_timeout(wdog, wdog->timeout);
  250. set_bit(WDOG_HW_RUNNING, &wdog->status);
  251. }
  252. /*
  253. * Disable the watchdog power down counter at boot. Otherwise the power
  254. * down counter will pull down the #WDOG interrupt line for one clock
  255. * cycle.
  256. */
  257. regmap_write(wdev->regmap, IMX2_WDT_WMCR, 0);
  258. ret = watchdog_register_device(wdog);
  259. if (ret) {
  260. dev_err(&pdev->dev, "cannot register watchdog device\n");
  261. goto disable_clk;
  262. }
  263. dev_info(&pdev->dev, "timeout %d sec (nowayout=%d)\n",
  264. wdog->timeout, nowayout);
  265. return 0;
  266. disable_clk:
  267. clk_disable_unprepare(wdev->clk);
  268. return ret;
  269. }
  270. static int __exit imx2_wdt_remove(struct platform_device *pdev)
  271. {
  272. struct watchdog_device *wdog = platform_get_drvdata(pdev);
  273. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  274. watchdog_unregister_device(wdog);
  275. if (imx2_wdt_is_running(wdev)) {
  276. imx2_wdt_ping(wdog);
  277. dev_crit(&pdev->dev, "Device removed: Expect reboot!\n");
  278. }
  279. return 0;
  280. }
  281. static void imx2_wdt_shutdown(struct platform_device *pdev)
  282. {
  283. struct watchdog_device *wdog = platform_get_drvdata(pdev);
  284. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  285. if (imx2_wdt_is_running(wdev)) {
  286. /*
  287. * We are running, configure max timeout before reboot
  288. * will take place.
  289. */
  290. imx2_wdt_set_timeout(wdog, IMX2_WDT_MAX_TIME);
  291. imx2_wdt_ping(wdog);
  292. dev_crit(&pdev->dev, "Device shutdown: Expect reboot!\n");
  293. }
  294. }
  295. #ifdef CONFIG_PM_SLEEP
  296. /* Disable watchdog if it is active or non-active but still running */
  297. static int imx2_wdt_suspend(struct device *dev)
  298. {
  299. struct watchdog_device *wdog = dev_get_drvdata(dev);
  300. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  301. /* The watchdog IP block is running */
  302. if (imx2_wdt_is_running(wdev)) {
  303. imx2_wdt_set_timeout(wdog, IMX2_WDT_MAX_TIME);
  304. imx2_wdt_ping(wdog);
  305. }
  306. clk_disable_unprepare(wdev->clk);
  307. return 0;
  308. }
  309. /* Enable watchdog and configure it if necessary */
  310. static int imx2_wdt_resume(struct device *dev)
  311. {
  312. struct watchdog_device *wdog = dev_get_drvdata(dev);
  313. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  314. int ret;
  315. ret = clk_prepare_enable(wdev->clk);
  316. if (ret)
  317. return ret;
  318. if (watchdog_active(wdog) && !imx2_wdt_is_running(wdev)) {
  319. /*
  320. * If the watchdog is still active and resumes
  321. * from deep sleep state, need to restart the
  322. * watchdog again.
  323. */
  324. imx2_wdt_setup(wdog);
  325. }
  326. if (imx2_wdt_is_running(wdev)) {
  327. imx2_wdt_set_timeout(wdog, wdog->timeout);
  328. imx2_wdt_ping(wdog);
  329. }
  330. return 0;
  331. }
  332. #endif
  333. static SIMPLE_DEV_PM_OPS(imx2_wdt_pm_ops, imx2_wdt_suspend,
  334. imx2_wdt_resume);
  335. static const struct of_device_id imx2_wdt_dt_ids[] = {
  336. { .compatible = "fsl,imx21-wdt", },
  337. { /* sentinel */ }
  338. };
  339. MODULE_DEVICE_TABLE(of, imx2_wdt_dt_ids);
  340. static struct platform_driver imx2_wdt_driver = {
  341. .remove = __exit_p(imx2_wdt_remove),
  342. .shutdown = imx2_wdt_shutdown,
  343. .driver = {
  344. .name = DRIVER_NAME,
  345. .pm = &imx2_wdt_pm_ops,
  346. .of_match_table = imx2_wdt_dt_ids,
  347. },
  348. };
  349. module_platform_driver_probe(imx2_wdt_driver, imx2_wdt_probe);
  350. MODULE_AUTHOR("Wolfram Sang");
  351. MODULE_DESCRIPTION("Watchdog driver for IMX2 and later");
  352. MODULE_LICENSE("GPL v2");
  353. MODULE_ALIAS("platform:" DRIVER_NAME);