mpcore_wdt.c 10 KB

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