mpcore_wdt.c 9.9 KB

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