imx2_wdt.c 9.3 KB

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