imx2_wdt.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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/init.h>
  25. #include <linux/io.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/regmap.h>
  32. #include <linux/timer.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_MAX_TIME 128
  46. #define IMX2_WDT_DEFAULT_TIME 60 /* in seconds */
  47. #define WDOG_SEC_TO_COUNT(s) ((s * 2 - 1) << 8)
  48. struct imx2_wdt_device {
  49. struct clk *clk;
  50. struct regmap *regmap;
  51. struct timer_list timer; /* Pings the watchdog when closed */
  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 inline void imx2_wdt_setup(struct watchdog_device *wdog)
  67. {
  68. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  69. u32 val;
  70. regmap_read(wdev->regmap, IMX2_WDT_WCR, &val);
  71. /* Suspend timer in low power mode, write once-only */
  72. val |= IMX2_WDT_WCR_WDZST;
  73. /* Strip the old watchdog Time-Out value */
  74. val &= ~IMX2_WDT_WCR_WT;
  75. /* Generate reset if WDOG times out */
  76. val &= ~IMX2_WDT_WCR_WRE;
  77. /* Keep Watchdog Disabled */
  78. val &= ~IMX2_WDT_WCR_WDE;
  79. /* Set the watchdog's Time-Out value */
  80. val |= WDOG_SEC_TO_COUNT(wdog->timeout);
  81. regmap_write(wdev->regmap, IMX2_WDT_WCR, val);
  82. /* enable the watchdog */
  83. val |= IMX2_WDT_WCR_WDE;
  84. regmap_write(wdev->regmap, IMX2_WDT_WCR, val);
  85. }
  86. static inline bool imx2_wdt_is_running(struct imx2_wdt_device *wdev)
  87. {
  88. u32 val;
  89. regmap_read(wdev->regmap, IMX2_WDT_WCR, &val);
  90. return val & IMX2_WDT_WCR_WDE;
  91. }
  92. static int imx2_wdt_ping(struct watchdog_device *wdog)
  93. {
  94. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  95. regmap_write(wdev->regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ1);
  96. regmap_write(wdev->regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ2);
  97. return 0;
  98. }
  99. static void imx2_wdt_timer_ping(unsigned long arg)
  100. {
  101. struct watchdog_device *wdog = (struct watchdog_device *)arg;
  102. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  103. /* ping it every wdog->timeout / 2 seconds to prevent reboot */
  104. imx2_wdt_ping(wdog);
  105. mod_timer(&wdev->timer, jiffies + wdog->timeout * HZ / 2);
  106. }
  107. static int imx2_wdt_set_timeout(struct watchdog_device *wdog,
  108. unsigned int new_timeout)
  109. {
  110. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  111. regmap_update_bits(wdev->regmap, IMX2_WDT_WCR, IMX2_WDT_WCR_WT,
  112. WDOG_SEC_TO_COUNT(new_timeout));
  113. return 0;
  114. }
  115. static int imx2_wdt_start(struct watchdog_device *wdog)
  116. {
  117. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  118. if (imx2_wdt_is_running(wdev)) {
  119. /* delete the timer that pings the watchdog after close */
  120. del_timer_sync(&wdev->timer);
  121. imx2_wdt_set_timeout(wdog, wdog->timeout);
  122. } else
  123. imx2_wdt_setup(wdog);
  124. return imx2_wdt_ping(wdog);
  125. }
  126. static int imx2_wdt_stop(struct watchdog_device *wdog)
  127. {
  128. /*
  129. * We don't need a clk_disable, it cannot be disabled once started.
  130. * We use a timer to ping the watchdog while /dev/watchdog is closed
  131. */
  132. imx2_wdt_timer_ping((unsigned long)wdog);
  133. return 0;
  134. }
  135. static inline void imx2_wdt_ping_if_active(struct watchdog_device *wdog)
  136. {
  137. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  138. if (imx2_wdt_is_running(wdev)) {
  139. imx2_wdt_set_timeout(wdog, wdog->timeout);
  140. imx2_wdt_timer_ping((unsigned long)wdog);
  141. }
  142. }
  143. static struct watchdog_ops imx2_wdt_ops = {
  144. .owner = THIS_MODULE,
  145. .start = imx2_wdt_start,
  146. .stop = imx2_wdt_stop,
  147. .ping = imx2_wdt_ping,
  148. .set_timeout = imx2_wdt_set_timeout,
  149. };
  150. static struct regmap_config imx2_wdt_regmap_config = {
  151. .reg_bits = 16,
  152. .reg_stride = 2,
  153. .val_bits = 16,
  154. .max_register = 0x8,
  155. };
  156. static int __init imx2_wdt_probe(struct platform_device *pdev)
  157. {
  158. struct imx2_wdt_device *wdev;
  159. struct watchdog_device *wdog;
  160. struct resource *res;
  161. void __iomem *base;
  162. int ret;
  163. u32 val;
  164. wdev = devm_kzalloc(&pdev->dev, sizeof(*wdev), GFP_KERNEL);
  165. if (!wdev)
  166. return -ENOMEM;
  167. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  168. base = devm_ioremap_resource(&pdev->dev, res);
  169. if (IS_ERR(base))
  170. return PTR_ERR(base);
  171. wdev->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
  172. &imx2_wdt_regmap_config);
  173. if (IS_ERR(wdev->regmap)) {
  174. dev_err(&pdev->dev, "regmap init failed\n");
  175. return PTR_ERR(wdev->regmap);
  176. }
  177. wdev->clk = devm_clk_get(&pdev->dev, NULL);
  178. if (IS_ERR(wdev->clk)) {
  179. dev_err(&pdev->dev, "can't get Watchdog clock\n");
  180. return PTR_ERR(wdev->clk);
  181. }
  182. wdog = &wdev->wdog;
  183. wdog->info = &imx2_wdt_info;
  184. wdog->ops = &imx2_wdt_ops;
  185. wdog->min_timeout = 1;
  186. wdog->max_timeout = IMX2_WDT_MAX_TIME;
  187. clk_prepare_enable(wdev->clk);
  188. regmap_read(wdev->regmap, IMX2_WDT_WRSR, &val);
  189. wdog->bootstatus = val & IMX2_WDT_WRSR_TOUT ? WDIOF_CARDRESET : 0;
  190. wdog->timeout = clamp_t(unsigned, timeout, 1, IMX2_WDT_MAX_TIME);
  191. if (wdog->timeout != timeout)
  192. dev_warn(&pdev->dev, "Initial timeout out of range! Clamped from %u to %u\n",
  193. timeout, wdog->timeout);
  194. platform_set_drvdata(pdev, wdog);
  195. watchdog_set_drvdata(wdog, wdev);
  196. watchdog_set_nowayout(wdog, nowayout);
  197. watchdog_init_timeout(wdog, timeout, &pdev->dev);
  198. setup_timer(&wdev->timer, imx2_wdt_timer_ping, (unsigned long)wdog);
  199. imx2_wdt_ping_if_active(wdog);
  200. ret = watchdog_register_device(wdog);
  201. if (ret) {
  202. dev_err(&pdev->dev, "cannot register watchdog device\n");
  203. return ret;
  204. }
  205. dev_info(&pdev->dev, "timeout %d sec (nowayout=%d)\n",
  206. wdog->timeout, nowayout);
  207. return 0;
  208. }
  209. static int __exit imx2_wdt_remove(struct platform_device *pdev)
  210. {
  211. struct watchdog_device *wdog = platform_get_drvdata(pdev);
  212. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  213. watchdog_unregister_device(wdog);
  214. if (imx2_wdt_is_running(wdev)) {
  215. del_timer_sync(&wdev->timer);
  216. imx2_wdt_ping(wdog);
  217. dev_crit(&pdev->dev, "Device removed: Expect reboot!\n");
  218. }
  219. return 0;
  220. }
  221. static void imx2_wdt_shutdown(struct platform_device *pdev)
  222. {
  223. struct watchdog_device *wdog = platform_get_drvdata(pdev);
  224. struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
  225. if (imx2_wdt_is_running(wdev)) {
  226. /*
  227. * We are running, we need to delete the timer but will
  228. * give max timeout before reboot will take place
  229. */
  230. del_timer_sync(&wdev->timer);
  231. imx2_wdt_set_timeout(wdog, IMX2_WDT_MAX_TIME);
  232. imx2_wdt_ping(wdog);
  233. dev_crit(&pdev->dev, "Device shutdown: Expect reboot!\n");
  234. }
  235. }
  236. static const struct of_device_id imx2_wdt_dt_ids[] = {
  237. { .compatible = "fsl,imx21-wdt", },
  238. { /* sentinel */ }
  239. };
  240. MODULE_DEVICE_TABLE(of, imx2_wdt_dt_ids);
  241. static struct platform_driver imx2_wdt_driver = {
  242. .remove = __exit_p(imx2_wdt_remove),
  243. .shutdown = imx2_wdt_shutdown,
  244. .driver = {
  245. .name = DRIVER_NAME,
  246. .owner = THIS_MODULE,
  247. .of_match_table = imx2_wdt_dt_ids,
  248. },
  249. };
  250. module_platform_driver_probe(imx2_wdt_driver, imx2_wdt_probe);
  251. MODULE_AUTHOR("Wolfram Sang");
  252. MODULE_DESCRIPTION("Watchdog driver for IMX2 and later");
  253. MODULE_LICENSE("GPL v2");
  254. MODULE_ALIAS("platform:" DRIVER_NAME);