imx2_wdt.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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_MAX_TIME 128
  45. #define IMX2_WDT_DEFAULT_TIME 60 /* in seconds */
  46. #define WDOG_SEC_TO_COUNT(s) ((s * 2 - 1) << 8)
  47. #define IMX2_WDT_STATUS_OPEN 0
  48. #define IMX2_WDT_STATUS_STARTED 1
  49. #define IMX2_WDT_EXPECT_CLOSE 2
  50. static struct {
  51. struct clk *clk;
  52. void __iomem *base;
  53. unsigned timeout;
  54. unsigned long status;
  55. struct timer_list timer; /* Pings the watchdog when closed */
  56. } imx2_wdt;
  57. static struct miscdevice imx2_wdt_miscdev;
  58. static int nowayout = WATCHDOG_NOWAYOUT;
  59. module_param(nowayout, int, 0);
  60. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  61. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  62. static unsigned timeout = IMX2_WDT_DEFAULT_TIME;
  63. module_param(timeout, uint, 0);
  64. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
  65. __MODULE_STRING(IMX2_WDT_DEFAULT_TIME) ")");
  66. static const struct watchdog_info imx2_wdt_info = {
  67. .identity = "imx2+ watchdog",
  68. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  69. };
  70. static inline void imx2_wdt_setup(void)
  71. {
  72. u16 val = __raw_readw(imx2_wdt.base + IMX2_WDT_WCR);
  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(imx2_wdt.timeout);
  81. __raw_writew(val, imx2_wdt.base + IMX2_WDT_WCR);
  82. /* enable the watchdog */
  83. val |= IMX2_WDT_WCR_WDE;
  84. __raw_writew(val, imx2_wdt.base + IMX2_WDT_WCR);
  85. }
  86. static inline void imx2_wdt_ping(void)
  87. {
  88. __raw_writew(IMX2_WDT_SEQ1, imx2_wdt.base + IMX2_WDT_WSR);
  89. __raw_writew(IMX2_WDT_SEQ2, imx2_wdt.base + IMX2_WDT_WSR);
  90. }
  91. static void imx2_wdt_timer_ping(unsigned long arg)
  92. {
  93. /* ping it every imx2_wdt.timeout / 2 seconds to prevent reboot */
  94. imx2_wdt_ping();
  95. mod_timer(&imx2_wdt.timer, jiffies + imx2_wdt.timeout * HZ / 2);
  96. }
  97. static void imx2_wdt_start(void)
  98. {
  99. if (!test_and_set_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) {
  100. /* at our first start we enable clock and do initialisations */
  101. clk_enable(imx2_wdt.clk);
  102. imx2_wdt_setup();
  103. } else /* delete the timer that pings the watchdog after close */
  104. del_timer_sync(&imx2_wdt.timer);
  105. /* Watchdog is enabled - time to reload the timeout value */
  106. imx2_wdt_ping();
  107. }
  108. static void imx2_wdt_stop(void)
  109. {
  110. /* we don't need a clk_disable, it cannot be disabled once started.
  111. * We use a timer to ping the watchdog while /dev/watchdog is closed */
  112. imx2_wdt_timer_ping(0);
  113. }
  114. static void imx2_wdt_set_timeout(int new_timeout)
  115. {
  116. u16 val = __raw_readw(imx2_wdt.base + IMX2_WDT_WCR);
  117. /* set the new timeout value in the WSR */
  118. val &= ~IMX2_WDT_WCR_WT;
  119. val |= WDOG_SEC_TO_COUNT(new_timeout);
  120. __raw_writew(val, imx2_wdt.base + IMX2_WDT_WCR);
  121. }
  122. static int imx2_wdt_open(struct inode *inode, struct file *file)
  123. {
  124. if (test_and_set_bit(IMX2_WDT_STATUS_OPEN, &imx2_wdt.status))
  125. return -EBUSY;
  126. imx2_wdt_start();
  127. return nonseekable_open(inode, file);
  128. }
  129. static int imx2_wdt_close(struct inode *inode, struct file *file)
  130. {
  131. if (test_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status) && !nowayout)
  132. imx2_wdt_stop();
  133. else {
  134. dev_crit(imx2_wdt_miscdev.parent,
  135. "Unexpected close: Expect reboot!\n");
  136. imx2_wdt_ping();
  137. }
  138. clear_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status);
  139. clear_bit(IMX2_WDT_STATUS_OPEN, &imx2_wdt.status);
  140. return 0;
  141. }
  142. static long imx2_wdt_ioctl(struct file *file, unsigned int cmd,
  143. unsigned long arg)
  144. {
  145. void __user *argp = (void __user *)arg;
  146. int __user *p = argp;
  147. int new_value;
  148. switch (cmd) {
  149. case WDIOC_GETSUPPORT:
  150. return copy_to_user(argp, &imx2_wdt_info,
  151. sizeof(struct watchdog_info)) ? -EFAULT : 0;
  152. case WDIOC_GETSTATUS:
  153. case WDIOC_GETBOOTSTATUS:
  154. return put_user(0, p);
  155. case WDIOC_KEEPALIVE:
  156. imx2_wdt_ping();
  157. return 0;
  158. case WDIOC_SETTIMEOUT:
  159. if (get_user(new_value, p))
  160. return -EFAULT;
  161. if ((new_value < 1) || (new_value > IMX2_WDT_MAX_TIME))
  162. return -EINVAL;
  163. imx2_wdt_set_timeout(new_value);
  164. imx2_wdt.timeout = new_value;
  165. imx2_wdt_ping();
  166. /* Fallthrough to return current value */
  167. case WDIOC_GETTIMEOUT:
  168. return put_user(imx2_wdt.timeout, p);
  169. default:
  170. return -ENOTTY;
  171. }
  172. }
  173. static ssize_t imx2_wdt_write(struct file *file, const char __user *data,
  174. size_t len, loff_t *ppos)
  175. {
  176. size_t i;
  177. char c;
  178. if (len == 0) /* Can we see this even ? */
  179. return 0;
  180. clear_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status);
  181. /* scan to see whether or not we got the magic character */
  182. for (i = 0; i != len; i++) {
  183. if (get_user(c, data + i))
  184. return -EFAULT;
  185. if (c == 'V')
  186. set_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status);
  187. }
  188. imx2_wdt_ping();
  189. return len;
  190. }
  191. static const struct file_operations imx2_wdt_fops = {
  192. .owner = THIS_MODULE,
  193. .llseek = no_llseek,
  194. .unlocked_ioctl = imx2_wdt_ioctl,
  195. .open = imx2_wdt_open,
  196. .release = imx2_wdt_close,
  197. .write = imx2_wdt_write,
  198. };
  199. static struct miscdevice imx2_wdt_miscdev = {
  200. .minor = WATCHDOG_MINOR,
  201. .name = "watchdog",
  202. .fops = &imx2_wdt_fops,
  203. };
  204. static int __init imx2_wdt_probe(struct platform_device *pdev)
  205. {
  206. int ret;
  207. int res_size;
  208. struct resource *res;
  209. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  210. if (!res) {
  211. dev_err(&pdev->dev, "can't get device resources\n");
  212. return -ENODEV;
  213. }
  214. res_size = resource_size(res);
  215. if (!devm_request_mem_region(&pdev->dev, res->start, res_size,
  216. res->name)) {
  217. dev_err(&pdev->dev, "can't allocate %d bytes at %d address\n",
  218. res_size, res->start);
  219. return -ENOMEM;
  220. }
  221. imx2_wdt.base = devm_ioremap_nocache(&pdev->dev, res->start, res_size);
  222. if (!imx2_wdt.base) {
  223. dev_err(&pdev->dev, "ioremap failed\n");
  224. return -ENOMEM;
  225. }
  226. imx2_wdt.clk = clk_get(&pdev->dev, NULL);
  227. if (IS_ERR(imx2_wdt.clk)) {
  228. dev_err(&pdev->dev, "can't get Watchdog clock\n");
  229. return PTR_ERR(imx2_wdt.clk);
  230. }
  231. imx2_wdt.timeout = clamp_t(unsigned, timeout, 1, IMX2_WDT_MAX_TIME);
  232. if (imx2_wdt.timeout != timeout)
  233. dev_warn(&pdev->dev, "Initial timeout out of range! "
  234. "Clamped from %u to %u\n", timeout, imx2_wdt.timeout);
  235. setup_timer(&imx2_wdt.timer, imx2_wdt_timer_ping, 0);
  236. imx2_wdt_miscdev.parent = &pdev->dev;
  237. ret = misc_register(&imx2_wdt_miscdev);
  238. if (ret)
  239. goto fail;
  240. dev_info(&pdev->dev,
  241. "IMX2+ Watchdog Timer enabled. timeout=%ds (nowayout=%d)\n",
  242. imx2_wdt.timeout, nowayout);
  243. return 0;
  244. fail:
  245. imx2_wdt_miscdev.parent = NULL;
  246. clk_put(imx2_wdt.clk);
  247. return ret;
  248. }
  249. static int __exit imx2_wdt_remove(struct platform_device *pdev)
  250. {
  251. misc_deregister(&imx2_wdt_miscdev);
  252. if (test_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) {
  253. del_timer_sync(&imx2_wdt.timer);
  254. dev_crit(imx2_wdt_miscdev.parent,
  255. "Device removed: Expect reboot!\n");
  256. } else
  257. clk_put(imx2_wdt.clk);
  258. imx2_wdt_miscdev.parent = NULL;
  259. return 0;
  260. }
  261. static void imx2_wdt_shutdown(struct platform_device *pdev)
  262. {
  263. if (test_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) {
  264. /* we are running, we need to delete the timer but will give
  265. * max timeout before reboot will take place */
  266. del_timer_sync(&imx2_wdt.timer);
  267. imx2_wdt_set_timeout(IMX2_WDT_MAX_TIME);
  268. imx2_wdt_ping();
  269. dev_crit(imx2_wdt_miscdev.parent,
  270. "Device shutdown: Expect reboot!\n");
  271. }
  272. }
  273. static struct platform_driver imx2_wdt_driver = {
  274. .remove = __exit_p(imx2_wdt_remove),
  275. .shutdown = imx2_wdt_shutdown,
  276. .driver = {
  277. .name = DRIVER_NAME,
  278. .owner = THIS_MODULE,
  279. },
  280. };
  281. static int __init imx2_wdt_init(void)
  282. {
  283. return platform_driver_probe(&imx2_wdt_driver, imx2_wdt_probe);
  284. }
  285. module_init(imx2_wdt_init);
  286. static void __exit imx2_wdt_exit(void)
  287. {
  288. platform_driver_unregister(&imx2_wdt_driver);
  289. }
  290. module_exit(imx2_wdt_exit);
  291. MODULE_AUTHOR("Wolfram Sang");
  292. MODULE_DESCRIPTION("Watchdog driver for IMX2 and later");
  293. MODULE_LICENSE("GPL v2");
  294. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  295. MODULE_ALIAS("platform:" DRIVER_NAME);