mpcore_wdt.c 10 KB

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