imx2_wdt.c 9.9 KB

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