mpcore_wdt.c 10 KB

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