mpcore_wdt.c 10 KB

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