imx2_wdt.c 11 KB

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