mpcore_wdt.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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/types.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/watchdog.h>
  27. #include <linux/fs.h>
  28. #include <linux/reboot.h>
  29. #include <linux/init.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/platform_device.h>
  32. #include <asm/hardware/arm_twd.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)
  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 = platform_get_drvdata(mpcore_wdt_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. /*
  151. * Refresh the timer.
  152. */
  153. if (len) {
  154. if (!nowayout) {
  155. size_t i;
  156. /* In case it was set long ago */
  157. wdt->expect_close = 0;
  158. for (i = 0; i != len; i++) {
  159. char c;
  160. if (get_user(c, data + i))
  161. return -EFAULT;
  162. if (c == 'V')
  163. wdt->expect_close = 42;
  164. }
  165. }
  166. mpcore_wdt_keepalive(wdt);
  167. }
  168. return len;
  169. }
  170. static struct watchdog_info ident = {
  171. .options = WDIOF_SETTIMEOUT |
  172. WDIOF_KEEPALIVEPING |
  173. WDIOF_MAGICCLOSE,
  174. .identity = "MPcore Watchdog",
  175. };
  176. static int mpcore_wdt_ioctl(struct inode *inode, struct file *file,
  177. unsigned int cmd, unsigned long arg)
  178. {
  179. struct mpcore_wdt *wdt = file->private_data;
  180. int ret;
  181. union {
  182. struct watchdog_info ident;
  183. int i;
  184. } uarg;
  185. if (_IOC_DIR(cmd) && _IOC_SIZE(cmd) > sizeof(uarg))
  186. return -ENOTTY;
  187. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  188. ret = copy_from_user(&uarg, (void __user *)arg, _IOC_SIZE(cmd));
  189. if (ret)
  190. return -EFAULT;
  191. }
  192. switch (cmd) {
  193. case WDIOC_GETSUPPORT:
  194. uarg.ident = ident;
  195. ret = 0;
  196. break;
  197. case WDIOC_SETOPTIONS:
  198. ret = -EINVAL;
  199. if (uarg.i & WDIOS_DISABLECARD) {
  200. mpcore_wdt_stop(wdt);
  201. ret = 0;
  202. }
  203. if (uarg.i & WDIOS_ENABLECARD) {
  204. mpcore_wdt_start(wdt);
  205. ret = 0;
  206. }
  207. break;
  208. case WDIOC_GETSTATUS:
  209. case WDIOC_GETBOOTSTATUS:
  210. uarg.i = 0;
  211. ret = 0;
  212. break;
  213. case WDIOC_KEEPALIVE:
  214. mpcore_wdt_keepalive(wdt);
  215. ret = 0;
  216. break;
  217. case WDIOC_SETTIMEOUT:
  218. ret = mpcore_wdt_set_heartbeat(uarg.i);
  219. if (ret)
  220. break;
  221. mpcore_wdt_keepalive(wdt);
  222. /* Fall */
  223. case WDIOC_GETTIMEOUT:
  224. uarg.i = mpcore_margin;
  225. ret = 0;
  226. break;
  227. default:
  228. return -ENOTTY;
  229. }
  230. if (ret == 0 && _IOC_DIR(cmd) & _IOC_READ) {
  231. ret = copy_to_user((void __user *)arg, &uarg, _IOC_SIZE(cmd));
  232. if (ret)
  233. ret = -EFAULT;
  234. }
  235. return ret;
  236. }
  237. /*
  238. * System shutdown handler. Turn off the watchdog if we're
  239. * restarting or halting the system.
  240. */
  241. static void mpcore_wdt_shutdown(struct platform_device *dev)
  242. {
  243. struct mpcore_wdt *wdt = platform_get_drvdata(dev);
  244. if (system_state == SYSTEM_RESTART || system_state == SYSTEM_HALT)
  245. mpcore_wdt_stop(wdt);
  246. }
  247. /*
  248. * Kernel Interfaces
  249. */
  250. static const struct file_operations mpcore_wdt_fops = {
  251. .owner = THIS_MODULE,
  252. .llseek = no_llseek,
  253. .write = mpcore_wdt_write,
  254. .ioctl = mpcore_wdt_ioctl,
  255. .open = mpcore_wdt_open,
  256. .release = mpcore_wdt_release,
  257. };
  258. static struct miscdevice mpcore_wdt_miscdev = {
  259. .minor = WATCHDOG_MINOR,
  260. .name = "watchdog",
  261. .fops = &mpcore_wdt_fops,
  262. };
  263. static int __devinit mpcore_wdt_probe(struct platform_device *dev)
  264. {
  265. struct mpcore_wdt *wdt;
  266. struct resource *res;
  267. int ret;
  268. /* We only accept one device, and it must have an id of -1 */
  269. if (dev->id != -1)
  270. return -ENODEV;
  271. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  272. if (!res) {
  273. ret = -ENODEV;
  274. goto err_out;
  275. }
  276. wdt = kmalloc(sizeof(struct mpcore_wdt), GFP_KERNEL);
  277. if (!wdt) {
  278. ret = -ENOMEM;
  279. goto err_out;
  280. }
  281. memset(wdt, 0, sizeof(struct mpcore_wdt));
  282. wdt->dev = &dev->dev;
  283. wdt->irq = platform_get_irq(dev, 0);
  284. if (wdt->irq < 0) {
  285. ret = -ENXIO;
  286. goto err_free;
  287. }
  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, IRQF_DISABLED, "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. platform_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 platform_device *dev)
  319. {
  320. struct mpcore_wdt *wdt = platform_get_drvdata(dev);
  321. platform_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 platform_driver mpcore_wdt_driver = {
  330. .probe = mpcore_wdt_probe,
  331. .remove = __devexit_p(mpcore_wdt_remove),
  332. .shutdown = mpcore_wdt_shutdown,
  333. .driver = {
  334. .owner = THIS_MODULE,
  335. .name = "mpcore_wdt",
  336. },
  337. };
  338. static char banner[] __initdata = KERN_INFO "MPcore Watchdog Timer: 0.1. mpcore_noboot=%d mpcore_margin=%d sec (nowayout= %d)\n";
  339. static int __init mpcore_wdt_init(void)
  340. {
  341. /*
  342. * Check that the margin value is within it's range;
  343. * if not reset to the default
  344. */
  345. if (mpcore_wdt_set_heartbeat(mpcore_margin)) {
  346. mpcore_wdt_set_heartbeat(TIMER_MARGIN);
  347. printk(KERN_INFO "mpcore_margin value must be 0<mpcore_margin<65536, using %d\n",
  348. TIMER_MARGIN);
  349. }
  350. printk(banner, mpcore_noboot, mpcore_margin, nowayout);
  351. return platform_driver_register(&mpcore_wdt_driver);
  352. }
  353. static void __exit mpcore_wdt_exit(void)
  354. {
  355. platform_driver_unregister(&mpcore_wdt_driver);
  356. }
  357. module_init(mpcore_wdt_init);
  358. module_exit(mpcore_wdt_exit);
  359. MODULE_AUTHOR("ARM Limited");
  360. MODULE_DESCRIPTION("MPcore Watchdog Device Driver");
  361. MODULE_LICENSE("GPL");
  362. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);