imx2_wdt.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/watchdog.h>
  30. #include <linux/clk.h>
  31. #include <linux/fs.h>
  32. #include <linux/io.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/timer.h>
  35. #include <linux/jiffies.h>
  36. #define DRIVER_NAME "imx2-wdt"
  37. #define IMX2_WDT_WCR 0x00 /* Control Register */
  38. #define IMX2_WDT_WCR_WT (0xFF << 8) /* -> Watchdog Timeout Field */
  39. #define IMX2_WDT_WCR_WRE (1 << 3) /* -> WDOG Reset Enable */
  40. #define IMX2_WDT_WCR_WDE (1 << 2) /* -> Watchdog Enable */
  41. #define IMX2_WDT_WCR_WDZST (1 << 0) /* -> Watchdog timer Suspend */
  42. #define IMX2_WDT_WSR 0x02 /* Service Register */
  43. #define IMX2_WDT_SEQ1 0x5555 /* -> service sequence 1 */
  44. #define IMX2_WDT_SEQ2 0xAAAA /* -> service sequence 2 */
  45. #define IMX2_WDT_WRSR 0x04 /* Reset Status Register */
  46. #define IMX2_WDT_WRSR_TOUT (1 << 1) /* -> Reset due to Timeout */
  47. #define IMX2_WDT_MAX_TIME 128
  48. #define IMX2_WDT_DEFAULT_TIME 60 /* in seconds */
  49. #define WDOG_SEC_TO_COUNT(s) ((s * 2 - 1) << 8)
  50. #define IMX2_WDT_STATUS_OPEN 0
  51. #define IMX2_WDT_STATUS_STARTED 1
  52. #define IMX2_WDT_EXPECT_CLOSE 2
  53. static struct {
  54. struct clk *clk;
  55. void __iomem *base;
  56. unsigned timeout;
  57. unsigned long status;
  58. struct timer_list timer; /* Pings the watchdog when closed */
  59. } imx2_wdt;
  60. static struct miscdevice imx2_wdt_miscdev;
  61. static bool nowayout = WATCHDOG_NOWAYOUT;
  62. module_param(nowayout, bool, 0);
  63. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  64. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  65. static unsigned timeout = IMX2_WDT_DEFAULT_TIME;
  66. module_param(timeout, uint, 0);
  67. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
  68. __MODULE_STRING(IMX2_WDT_DEFAULT_TIME) ")");
  69. static const struct watchdog_info imx2_wdt_info = {
  70. .identity = "imx2+ watchdog",
  71. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  72. };
  73. static inline void imx2_wdt_setup(void)
  74. {
  75. u16 val = __raw_readw(imx2_wdt.base + IMX2_WDT_WCR);
  76. /* Suspend timer in low power mode, write once-only */
  77. val |= IMX2_WDT_WCR_WDZST;
  78. /* Strip the old watchdog Time-Out value */
  79. val &= ~IMX2_WDT_WCR_WT;
  80. /* Generate reset if WDOG times out */
  81. val &= ~IMX2_WDT_WCR_WRE;
  82. /* Keep Watchdog Disabled */
  83. val &= ~IMX2_WDT_WCR_WDE;
  84. /* Set the watchdog's Time-Out value */
  85. val |= WDOG_SEC_TO_COUNT(imx2_wdt.timeout);
  86. __raw_writew(val, imx2_wdt.base + IMX2_WDT_WCR);
  87. /* enable the watchdog */
  88. val |= IMX2_WDT_WCR_WDE;
  89. __raw_writew(val, imx2_wdt.base + IMX2_WDT_WCR);
  90. }
  91. static inline void imx2_wdt_ping(void)
  92. {
  93. __raw_writew(IMX2_WDT_SEQ1, imx2_wdt.base + IMX2_WDT_WSR);
  94. __raw_writew(IMX2_WDT_SEQ2, imx2_wdt.base + IMX2_WDT_WSR);
  95. }
  96. static void imx2_wdt_timer_ping(unsigned long arg)
  97. {
  98. /* ping it every imx2_wdt.timeout / 2 seconds to prevent reboot */
  99. imx2_wdt_ping();
  100. mod_timer(&imx2_wdt.timer, jiffies + imx2_wdt.timeout * HZ / 2);
  101. }
  102. static void imx2_wdt_start(void)
  103. {
  104. if (!test_and_set_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) {
  105. /* at our first start we enable clock and do initialisations */
  106. clk_prepare_enable(imx2_wdt.clk);
  107. imx2_wdt_setup();
  108. } else /* delete the timer that pings the watchdog after close */
  109. del_timer_sync(&imx2_wdt.timer);
  110. /* Watchdog is enabled - time to reload the timeout value */
  111. imx2_wdt_ping();
  112. }
  113. static void imx2_wdt_stop(void)
  114. {
  115. /* we don't need a clk_disable, it cannot be disabled once started.
  116. * We use a timer to ping the watchdog while /dev/watchdog is closed */
  117. imx2_wdt_timer_ping(0);
  118. }
  119. static void imx2_wdt_set_timeout(int new_timeout)
  120. {
  121. u16 val = __raw_readw(imx2_wdt.base + IMX2_WDT_WCR);
  122. /* set the new timeout value in the WSR */
  123. val &= ~IMX2_WDT_WCR_WT;
  124. val |= WDOG_SEC_TO_COUNT(new_timeout);
  125. __raw_writew(val, imx2_wdt.base + IMX2_WDT_WCR);
  126. }
  127. static int imx2_wdt_open(struct inode *inode, struct file *file)
  128. {
  129. if (test_and_set_bit(IMX2_WDT_STATUS_OPEN, &imx2_wdt.status))
  130. return -EBUSY;
  131. imx2_wdt_start();
  132. return nonseekable_open(inode, file);
  133. }
  134. static int imx2_wdt_close(struct inode *inode, struct file *file)
  135. {
  136. if (test_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status) && !nowayout)
  137. imx2_wdt_stop();
  138. else {
  139. dev_crit(imx2_wdt_miscdev.parent,
  140. "Unexpected close: Expect reboot!\n");
  141. imx2_wdt_ping();
  142. }
  143. clear_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status);
  144. clear_bit(IMX2_WDT_STATUS_OPEN, &imx2_wdt.status);
  145. return 0;
  146. }
  147. static long imx2_wdt_ioctl(struct file *file, unsigned int cmd,
  148. unsigned long arg)
  149. {
  150. void __user *argp = (void __user *)arg;
  151. int __user *p = argp;
  152. int new_value;
  153. u16 val;
  154. switch (cmd) {
  155. case WDIOC_GETSUPPORT:
  156. return copy_to_user(argp, &imx2_wdt_info,
  157. sizeof(struct watchdog_info)) ? -EFAULT : 0;
  158. case WDIOC_GETSTATUS:
  159. return put_user(0, p);
  160. case WDIOC_GETBOOTSTATUS:
  161. val = __raw_readw(imx2_wdt.base + IMX2_WDT_WRSR);
  162. new_value = val & IMX2_WDT_WRSR_TOUT ? WDIOF_CARDRESET : 0;
  163. return put_user(new_value, p);
  164. case WDIOC_KEEPALIVE:
  165. imx2_wdt_ping();
  166. return 0;
  167. case WDIOC_SETTIMEOUT:
  168. if (get_user(new_value, p))
  169. return -EFAULT;
  170. if ((new_value < 1) || (new_value > IMX2_WDT_MAX_TIME))
  171. return -EINVAL;
  172. imx2_wdt_set_timeout(new_value);
  173. imx2_wdt.timeout = new_value;
  174. imx2_wdt_ping();
  175. /* Fallthrough to return current value */
  176. case WDIOC_GETTIMEOUT:
  177. return put_user(imx2_wdt.timeout, p);
  178. default:
  179. return -ENOTTY;
  180. }
  181. }
  182. static ssize_t imx2_wdt_write(struct file *file, const char __user *data,
  183. size_t len, loff_t *ppos)
  184. {
  185. size_t i;
  186. char c;
  187. if (len == 0) /* Can we see this even ? */
  188. return 0;
  189. clear_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status);
  190. /* scan to see whether or not we got the magic character */
  191. for (i = 0; i != len; i++) {
  192. if (get_user(c, data + i))
  193. return -EFAULT;
  194. if (c == 'V')
  195. set_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status);
  196. }
  197. imx2_wdt_ping();
  198. return len;
  199. }
  200. static const struct file_operations imx2_wdt_fops = {
  201. .owner = THIS_MODULE,
  202. .llseek = no_llseek,
  203. .unlocked_ioctl = imx2_wdt_ioctl,
  204. .open = imx2_wdt_open,
  205. .release = imx2_wdt_close,
  206. .write = imx2_wdt_write,
  207. };
  208. static struct miscdevice imx2_wdt_miscdev = {
  209. .minor = WATCHDOG_MINOR,
  210. .name = "watchdog",
  211. .fops = &imx2_wdt_fops,
  212. };
  213. static int __init imx2_wdt_probe(struct platform_device *pdev)
  214. {
  215. int ret;
  216. struct resource *res;
  217. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  218. imx2_wdt.base = devm_ioremap_resource(&pdev->dev, res);
  219. if (IS_ERR(imx2_wdt.base))
  220. return PTR_ERR(imx2_wdt.base);
  221. imx2_wdt.clk = devm_clk_get(&pdev->dev, NULL);
  222. if (IS_ERR(imx2_wdt.clk)) {
  223. dev_err(&pdev->dev, "can't get Watchdog clock\n");
  224. return PTR_ERR(imx2_wdt.clk);
  225. }
  226. imx2_wdt.timeout = clamp_t(unsigned, timeout, 1, IMX2_WDT_MAX_TIME);
  227. if (imx2_wdt.timeout != timeout)
  228. dev_warn(&pdev->dev, "Initial timeout out of range! "
  229. "Clamped from %u to %u\n", timeout, imx2_wdt.timeout);
  230. setup_timer(&imx2_wdt.timer, imx2_wdt_timer_ping, 0);
  231. imx2_wdt_miscdev.parent = &pdev->dev;
  232. ret = misc_register(&imx2_wdt_miscdev);
  233. if (ret)
  234. goto fail;
  235. dev_info(&pdev->dev,
  236. "IMX2+ Watchdog Timer enabled. timeout=%ds (nowayout=%d)\n",
  237. imx2_wdt.timeout, nowayout);
  238. return 0;
  239. fail:
  240. imx2_wdt_miscdev.parent = NULL;
  241. return ret;
  242. }
  243. static int __exit imx2_wdt_remove(struct platform_device *pdev)
  244. {
  245. misc_deregister(&imx2_wdt_miscdev);
  246. if (test_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) {
  247. del_timer_sync(&imx2_wdt.timer);
  248. dev_crit(imx2_wdt_miscdev.parent,
  249. "Device removed: Expect reboot!\n");
  250. }
  251. imx2_wdt_miscdev.parent = NULL;
  252. return 0;
  253. }
  254. static void imx2_wdt_shutdown(struct platform_device *pdev)
  255. {
  256. if (test_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) {
  257. /* we are running, we need to delete the timer but will give
  258. * max timeout before reboot will take place */
  259. del_timer_sync(&imx2_wdt.timer);
  260. imx2_wdt_set_timeout(IMX2_WDT_MAX_TIME);
  261. imx2_wdt_ping();
  262. dev_crit(imx2_wdt_miscdev.parent,
  263. "Device shutdown: Expect reboot!\n");
  264. }
  265. }
  266. static const struct of_device_id imx2_wdt_dt_ids[] = {
  267. { .compatible = "fsl,imx21-wdt", },
  268. { /* sentinel */ }
  269. };
  270. MODULE_DEVICE_TABLE(of, imx2_wdt_dt_ids);
  271. static struct platform_driver imx2_wdt_driver = {
  272. .remove = __exit_p(imx2_wdt_remove),
  273. .shutdown = imx2_wdt_shutdown,
  274. .driver = {
  275. .name = DRIVER_NAME,
  276. .owner = THIS_MODULE,
  277. .of_match_table = imx2_wdt_dt_ids,
  278. },
  279. };
  280. module_platform_driver_probe(imx2_wdt_driver, imx2_wdt_probe);
  281. MODULE_AUTHOR("Wolfram Sang");
  282. MODULE_DESCRIPTION("Watchdog driver for IMX2 and later");
  283. MODULE_LICENSE("GPL v2");
  284. MODULE_ALIAS("platform:" DRIVER_NAME);