mpcore_wdt.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Watchdog driver for the mpcore watchdog timer
  3. *
  4. * (c) Copyright 2004 ARM Limited
  5. *
  6. * Based on the SoftDog driver:
  7. * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
  8. * http://www.redhat.com
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. *
  15. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  16. * warranty for any of this software. This material is provided
  17. * "AS-IS" and at no charge.
  18. *
  19. * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/config.h>
  25. #include <linux/types.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/watchdog.h>
  28. #include <linux/fs.h>
  29. #include <linux/reboot.h>
  30. #include <linux/init.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/platform_device.h>
  33. #include <asm/hardware/arm_twd.h>
  34. #include <asm/uaccess.h>
  35. struct mpcore_wdt {
  36. unsigned long timer_alive;
  37. struct device *dev;
  38. void __iomem *base;
  39. int irq;
  40. unsigned int perturb;
  41. char expect_close;
  42. };
  43. static struct platform_device *mpcore_wdt_dev;
  44. extern unsigned int mpcore_timer_rate;
  45. #define TIMER_MARGIN 60
  46. static int mpcore_margin = TIMER_MARGIN;
  47. module_param(mpcore_margin, int, 0);
  48. MODULE_PARM_DESC(mpcore_margin, "MPcore timer margin in seconds. (0<mpcore_margin<65536, default=" __MODULE_STRING(TIMER_MARGIN) ")");
  49. static int nowayout = WATCHDOG_NOWAYOUT;
  50. module_param(nowayout, int, 0);
  51. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  52. #define ONLY_TESTING 0
  53. static int mpcore_noboot = ONLY_TESTING;
  54. module_param(mpcore_noboot, int, 0);
  55. MODULE_PARM_DESC(mpcore_noboot, "MPcore watchdog action, set to 1 to ignore reboots, 0 to reboot (default=" __MODULE_STRING(ONLY_TESTING) ")");
  56. /*
  57. * This is the interrupt handler. Note that we only use this
  58. * in testing mode, so don't actually do a reboot here.
  59. */
  60. static irqreturn_t mpcore_wdt_fire(int irq, void *arg, struct pt_regs *regs)
  61. {
  62. struct mpcore_wdt *wdt = arg;
  63. /* Check it really was our interrupt */
  64. if (readl(wdt->base + TWD_WDOG_INTSTAT)) {
  65. dev_printk(KERN_CRIT, wdt->dev, "Triggered - Reboot ignored.\n");
  66. /* Clear the interrupt on the watchdog */
  67. writel(1, wdt->base + TWD_WDOG_INTSTAT);
  68. return IRQ_HANDLED;
  69. }
  70. return IRQ_NONE;
  71. }
  72. /*
  73. * mpcore_wdt_keepalive - reload the timer
  74. *
  75. * Note that the spec says a DIFFERENT value must be written to the reload
  76. * register each time. The "perturb" variable deals with this by adding 1
  77. * to the count every other time the function is called.
  78. */
  79. static void mpcore_wdt_keepalive(struct mpcore_wdt *wdt)
  80. {
  81. unsigned int count;
  82. /* Assume prescale is set to 256 */
  83. count = (mpcore_timer_rate / 256) * mpcore_margin;
  84. /* Reload the counter */
  85. writel(count + wdt->perturb, wdt->base + TWD_WDOG_LOAD);
  86. wdt->perturb = wdt->perturb ? 0 : 1;
  87. }
  88. static void mpcore_wdt_stop(struct mpcore_wdt *wdt)
  89. {
  90. writel(0x12345678, wdt->base + TWD_WDOG_DISABLE);
  91. writel(0x87654321, wdt->base + TWD_WDOG_DISABLE);
  92. writel(0x0, wdt->base + TWD_WDOG_CONTROL);
  93. }
  94. static void mpcore_wdt_start(struct mpcore_wdt *wdt)
  95. {
  96. dev_printk(KERN_INFO, wdt->dev, "enabling watchdog.\n");
  97. /* This loads the count register but does NOT start the count yet */
  98. mpcore_wdt_keepalive(wdt);
  99. if (mpcore_noboot) {
  100. /* Enable watchdog - prescale=256, watchdog mode=0, enable=1 */
  101. writel(0x0000FF01, wdt->base + TWD_WDOG_CONTROL);
  102. } else {
  103. /* Enable watchdog - prescale=256, watchdog mode=1, enable=1 */
  104. writel(0x0000FF09, wdt->base + TWD_WDOG_CONTROL);
  105. }
  106. }
  107. static int mpcore_wdt_set_heartbeat(int t)
  108. {
  109. if (t < 0x0001 || t > 0xFFFF)
  110. return -EINVAL;
  111. mpcore_margin = t;
  112. return 0;
  113. }
  114. /*
  115. * /dev/watchdog handling
  116. */
  117. static int mpcore_wdt_open(struct inode *inode, struct file *file)
  118. {
  119. struct mpcore_wdt *wdt = platform_get_drvdata(mpcore_wdt_dev);
  120. if (test_and_set_bit(0, &wdt->timer_alive))
  121. return -EBUSY;
  122. if (nowayout)
  123. __module_get(THIS_MODULE);
  124. file->private_data = wdt;
  125. /*
  126. * Activate timer
  127. */
  128. mpcore_wdt_start(wdt);
  129. return nonseekable_open(inode, file);
  130. }
  131. static int mpcore_wdt_release(struct inode *inode, struct file *file)
  132. {
  133. struct mpcore_wdt *wdt = file->private_data;
  134. /*
  135. * Shut off the timer.
  136. * Lock it in if it's a module and we set nowayout
  137. */
  138. if (wdt->expect_close == 42) {
  139. mpcore_wdt_stop(wdt);
  140. } else {
  141. dev_printk(KERN_CRIT, wdt->dev, "unexpected close, not stopping watchdog!\n");
  142. mpcore_wdt_keepalive(wdt);
  143. }
  144. clear_bit(0, &wdt->timer_alive);
  145. wdt->expect_close = 0;
  146. return 0;
  147. }
  148. static ssize_t mpcore_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
  149. {
  150. struct mpcore_wdt *wdt = file->private_data;
  151. /*
  152. * Refresh the timer.
  153. */
  154. if (len) {
  155. if (!nowayout) {
  156. size_t i;
  157. /* In case it was set long ago */
  158. wdt->expect_close = 0;
  159. for (i = 0; i != len; i++) {
  160. char c;
  161. if (get_user(c, data + i))
  162. return -EFAULT;
  163. if (c == 'V')
  164. wdt->expect_close = 42;
  165. }
  166. }
  167. mpcore_wdt_keepalive(wdt);
  168. }
  169. return len;
  170. }
  171. static struct watchdog_info ident = {
  172. .options = WDIOF_SETTIMEOUT |
  173. WDIOF_KEEPALIVEPING |
  174. WDIOF_MAGICCLOSE,
  175. .identity = "MPcore Watchdog",
  176. };
  177. static int mpcore_wdt_ioctl(struct inode *inode, struct file *file,
  178. unsigned int cmd, unsigned long arg)
  179. {
  180. struct mpcore_wdt *wdt = file->private_data;
  181. int ret;
  182. union {
  183. struct watchdog_info ident;
  184. int i;
  185. } uarg;
  186. if (_IOC_DIR(cmd) && _IOC_SIZE(cmd) > sizeof(uarg))
  187. return -ENOIOCTLCMD;
  188. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  189. ret = copy_from_user(&uarg, (void __user *)arg, _IOC_SIZE(cmd));
  190. if (ret)
  191. return -EFAULT;
  192. }
  193. switch (cmd) {
  194. case WDIOC_GETSUPPORT:
  195. uarg.ident = ident;
  196. ret = 0;
  197. break;
  198. case WDIOC_SETOPTIONS:
  199. ret = -EINVAL;
  200. if (uarg.i & WDIOS_DISABLECARD) {
  201. mpcore_wdt_stop(wdt);
  202. ret = 0;
  203. }
  204. if (uarg.i & WDIOS_ENABLECARD) {
  205. mpcore_wdt_start(wdt);
  206. ret = 0;
  207. }
  208. break;
  209. case WDIOC_GETSTATUS:
  210. case WDIOC_GETBOOTSTATUS:
  211. uarg.i = 0;
  212. ret = 0;
  213. break;
  214. case WDIOC_KEEPALIVE:
  215. mpcore_wdt_keepalive(wdt);
  216. ret = 0;
  217. break;
  218. case WDIOC_SETTIMEOUT:
  219. ret = mpcore_wdt_set_heartbeat(uarg.i);
  220. if (ret)
  221. break;
  222. mpcore_wdt_keepalive(wdt);
  223. /* Fall */
  224. case WDIOC_GETTIMEOUT:
  225. uarg.i = mpcore_margin;
  226. ret = 0;
  227. break;
  228. default:
  229. return -ENOIOCTLCMD;
  230. }
  231. if (ret == 0 && _IOC_DIR(cmd) & _IOC_READ) {
  232. ret = copy_to_user((void __user *)arg, &uarg, _IOC_SIZE(cmd));
  233. if (ret)
  234. ret = -EFAULT;
  235. }
  236. return ret;
  237. }
  238. /*
  239. * System shutdown handler. Turn off the watchdog if we're
  240. * restarting or halting the system.
  241. */
  242. static void mpcore_wdt_shutdown(struct platform_device *dev)
  243. {
  244. struct mpcore_wdt *wdt = platform_get_drvdata(dev);
  245. if (system_state == SYSTEM_RESTART || system_state == SYSTEM_HALT)
  246. mpcore_wdt_stop(wdt);
  247. }
  248. /*
  249. * Kernel Interfaces
  250. */
  251. static struct file_operations mpcore_wdt_fops = {
  252. .owner = THIS_MODULE,
  253. .llseek = no_llseek,
  254. .write = mpcore_wdt_write,
  255. .ioctl = mpcore_wdt_ioctl,
  256. .open = mpcore_wdt_open,
  257. .release = mpcore_wdt_release,
  258. };
  259. static struct miscdevice mpcore_wdt_miscdev = {
  260. .minor = WATCHDOG_MINOR,
  261. .name = "watchdog",
  262. .fops = &mpcore_wdt_fops,
  263. };
  264. static int __devinit mpcore_wdt_probe(struct platform_device *dev)
  265. {
  266. struct mpcore_wdt *wdt;
  267. struct resource *res;
  268. int ret;
  269. /* We only accept one device, and it must have an id of -1 */
  270. if (dev->id != -1)
  271. return -ENODEV;
  272. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  273. if (!res) {
  274. ret = -ENODEV;
  275. goto err_out;
  276. }
  277. wdt = kmalloc(sizeof(struct mpcore_wdt), GFP_KERNEL);
  278. if (!wdt) {
  279. ret = -ENOMEM;
  280. goto err_out;
  281. }
  282. memset(wdt, 0, sizeof(struct mpcore_wdt));
  283. wdt->dev = &dev->dev;
  284. wdt->irq = platform_get_irq(dev, 0);
  285. wdt->base = ioremap(res->start, res->end - res->start + 1);
  286. if (!wdt->base) {
  287. ret = -ENOMEM;
  288. goto err_free;
  289. }
  290. mpcore_wdt_miscdev.dev = &dev->dev;
  291. ret = misc_register(&mpcore_wdt_miscdev);
  292. if (ret) {
  293. dev_printk(KERN_ERR, _dev, "cannot register miscdev on minor=%d (err=%d)\n",
  294. WATCHDOG_MINOR, ret);
  295. goto err_misc;
  296. }
  297. ret = request_irq(wdt->irq, mpcore_wdt_fire, SA_INTERRUPT, "mpcore_wdt", wdt);
  298. if (ret) {
  299. dev_printk(KERN_ERR, _dev, "cannot register IRQ%d for watchdog\n", wdt->irq);
  300. goto err_irq;
  301. }
  302. mpcore_wdt_stop(wdt);
  303. platform_set_drvdata(&dev->dev, wdt);
  304. mpcore_wdt_dev = dev;
  305. return 0;
  306. err_irq:
  307. misc_deregister(&mpcore_wdt_miscdev);
  308. err_misc:
  309. iounmap(wdt->base);
  310. err_free:
  311. kfree(wdt);
  312. err_out:
  313. return ret;
  314. }
  315. static int __devexit mpcore_wdt_remove(struct platform_device *dev)
  316. {
  317. struct mpcore_wdt *wdt = platform_get_drvdata(dev);
  318. platform_set_drvdata(dev, NULL);
  319. misc_deregister(&mpcore_wdt_miscdev);
  320. mpcore_wdt_dev = NULL;
  321. free_irq(wdt->irq, wdt);
  322. iounmap(wdt->base);
  323. kfree(wdt);
  324. return 0;
  325. }
  326. static struct platform_driver mpcore_wdt_driver = {
  327. .probe = mpcore_wdt_probe,
  328. .remove = __devexit_p(mpcore_wdt_remove),
  329. .shutdown = mpcore_wdt_shutdown,
  330. .driver = {
  331. .owner = THIS_MODULE,
  332. .name = "mpcore_wdt",
  333. },
  334. };
  335. static char banner[] __initdata = KERN_INFO "MPcore Watchdog Timer: 0.1. mpcore_noboot=%d mpcore_margin=%d sec (nowayout= %d)\n";
  336. static int __init mpcore_wdt_init(void)
  337. {
  338. /*
  339. * Check that the margin value is within it's range;
  340. * if not reset to the default
  341. */
  342. if (mpcore_wdt_set_heartbeat(mpcore_margin)) {
  343. mpcore_wdt_set_heartbeat(TIMER_MARGIN);
  344. printk(KERN_INFO "mpcore_margin value must be 0<mpcore_margin<65536, using %d\n",
  345. TIMER_MARGIN);
  346. }
  347. printk(banner, mpcore_noboot, mpcore_margin, nowayout);
  348. return platform_driver_register(&mpcore_wdt_driver);
  349. }
  350. static void __exit mpcore_wdt_exit(void)
  351. {
  352. platform_driver_unregister(&mpcore_wdt_driver);
  353. }
  354. module_init(mpcore_wdt_init);
  355. module_exit(mpcore_wdt_exit);
  356. MODULE_AUTHOR("ARM Limited");
  357. MODULE_DESCRIPTION("MPcore Watchdog Device Driver");
  358. MODULE_LICENSE("GPL");
  359. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);