sb_wdog.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Watchdog driver for SiByte SB1 SoCs
  3. *
  4. * Copyright (C) 2007 OnStor, Inc. * Andrew Sharp <andy.sharp@onstor.com>
  5. *
  6. * This driver is intended to make the second of two hardware watchdogs
  7. * on the Sibyte 12XX and 11XX SoCs available to the user. There are two
  8. * such devices available on the SoC, but it seems that there isn't an
  9. * enumeration class for watchdogs in Linux like there is for RTCs.
  10. * The second is used rather than the first because it uses IRQ 1,
  11. * thereby avoiding all that IRQ 0 problematic nonsense.
  12. *
  13. * I have not tried this driver on a 1480 processor; it might work
  14. * just well enough to really screw things up.
  15. *
  16. * It is a simple timer, and there is an interrupt that is raised the
  17. * first time the timer expires. The second time it expires, the chip
  18. * is reset and there is no way to redirect that NMI. Which could
  19. * be problematic in some cases where this chip is sitting on the HT
  20. * bus and has just taken responsibility for providing a cache block.
  21. * Since the reset can't be redirected to the external reset pin, it is
  22. * possible that other HT connected processors might hang and not reset.
  23. * For Linux, a soft reset would probably be even worse than a hard reset.
  24. * There you have it.
  25. *
  26. * The timer takes 23 bits of a 64 bit register (?) as a count value,
  27. * and decrements the count every microsecond, for a max value of
  28. * 0x7fffff usec or about 8.3ish seconds.
  29. *
  30. * This watchdog borrows some user semantics from the softdog driver,
  31. * in that if you close the fd, it leaves the watchdog running, unless
  32. * you previously wrote a 'V' to the fd, in which case it disables
  33. * the watchdog when you close the fd like some other drivers.
  34. *
  35. * Based on various other watchdog drivers, which are probably all
  36. * loosely based on something Alan Cox wrote years ago.
  37. *
  38. * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
  39. * http://www.redhat.com
  40. *
  41. * This program is free software; you can redistribute it and/or
  42. * modify it under the terms of the GNU General Public License
  43. * version 1 or 2 as published by the Free Software Foundation.
  44. *
  45. */
  46. #include <linux/module.h>
  47. #include <linux/io.h>
  48. #include <linux/uaccess.h>
  49. #include <linux/fs.h>
  50. #include <linux/reboot.h>
  51. #include <linux/miscdevice.h>
  52. #include <linux/watchdog.h>
  53. #include <linux/interrupt.h>
  54. #include <asm/sibyte/sb1250.h>
  55. #include <asm/sibyte/sb1250_regs.h>
  56. #include <asm/sibyte/sb1250_int.h>
  57. #include <asm/sibyte/sb1250_scd.h>
  58. /*
  59. * set the initial count value of a timer
  60. *
  61. * wdog is the iomem address of the cfg register
  62. */
  63. void sbwdog_set(char __iomem *wdog, unsigned long t)
  64. {
  65. __raw_writeb(0, wdog - 0x10);
  66. __raw_writeq(t & 0x7fffffUL, wdog);
  67. }
  68. /*
  69. * cause the timer to [re]load it's initial count and start counting
  70. * all over again
  71. *
  72. * wdog is the iomem address of the cfg register
  73. */
  74. void sbwdog_pet(char __iomem *wdog)
  75. {
  76. __raw_writeb(__raw_readb(wdog) | 1, wdog);
  77. }
  78. static unsigned long sbwdog_gate; /* keeps it to one thread only */
  79. static char __iomem *kern_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_0));
  80. static char __iomem *user_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_1));
  81. static unsigned long timeout = 0x7fffffUL; /* useconds: 8.3ish secs. */
  82. static int expect_close;
  83. static struct watchdog_info ident = {
  84. .options = WDIOF_CARDRESET | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  85. .identity = "SiByte Watchdog",
  86. };
  87. /*
  88. * Allow only a single thread to walk the dog
  89. */
  90. static int sbwdog_open(struct inode *inode, struct file *file)
  91. {
  92. nonseekable_open(inode, file);
  93. if (test_and_set_bit(0, &sbwdog_gate)) {
  94. return -EBUSY;
  95. }
  96. __module_get(THIS_MODULE);
  97. /*
  98. * Activate the timer
  99. */
  100. sbwdog_set(user_dog, timeout);
  101. __raw_writeb(1, user_dog);
  102. return 0;
  103. }
  104. /*
  105. * Put the dog back in the kennel.
  106. */
  107. static int sbwdog_release(struct inode *inode, struct file *file)
  108. {
  109. if (expect_close == 42) {
  110. __raw_writeb(0, user_dog);
  111. module_put(THIS_MODULE);
  112. } else {
  113. printk(KERN_CRIT "%s: Unexpected close, not stopping watchdog!\n",
  114. ident.identity);
  115. sbwdog_pet(user_dog);
  116. }
  117. clear_bit(0, &sbwdog_gate);
  118. expect_close = 0;
  119. return 0;
  120. }
  121. /*
  122. * 42 - the answer
  123. */
  124. static ssize_t sbwdog_write(struct file *file, const char __user *data,
  125. size_t len, loff_t *ppos)
  126. {
  127. int i;
  128. if (len) {
  129. /*
  130. * restart the timer
  131. */
  132. expect_close = 0;
  133. for (i = 0; i != len; i++) {
  134. char c;
  135. if (get_user(c, data + i)) {
  136. return -EFAULT;
  137. }
  138. if (c == 'V') {
  139. expect_close = 42;
  140. }
  141. }
  142. sbwdog_pet(user_dog);
  143. }
  144. return len;
  145. }
  146. static int sbwdog_ioctl(struct inode *inode, struct file *file,
  147. unsigned int cmd, unsigned long arg)
  148. {
  149. int ret = -ENOTTY;
  150. unsigned long time;
  151. void __user *argp = (void __user *)arg;
  152. int __user *p = argp;
  153. switch (cmd) {
  154. case WDIOC_GETSUPPORT:
  155. ret = copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  156. break;
  157. case WDIOC_GETSTATUS:
  158. case WDIOC_GETBOOTSTATUS:
  159. ret = put_user(0, p);
  160. break;
  161. case WDIOC_SETTIMEOUT:
  162. ret = get_user(time, p);
  163. if (ret) {
  164. break;
  165. }
  166. time *= 1000000;
  167. if (time > 0x7fffffUL) {
  168. ret = -EINVAL;
  169. break;
  170. }
  171. timeout = time;
  172. sbwdog_set(user_dog, timeout);
  173. sbwdog_pet(user_dog);
  174. case WDIOC_GETTIMEOUT:
  175. /*
  176. * get the remaining count from the ... count register
  177. * which is 1*8 before the config register
  178. */
  179. ret = put_user(__raw_readq(user_dog - 8) / 1000000, p);
  180. break;
  181. case WDIOC_KEEPALIVE:
  182. sbwdog_pet(user_dog);
  183. ret = 0;
  184. break;
  185. }
  186. return ret;
  187. }
  188. /*
  189. * Notifier for system down
  190. */
  191. static int
  192. sbwdog_notify_sys(struct notifier_block *this, unsigned long code, void *erf)
  193. {
  194. if (code == SYS_DOWN || code == SYS_HALT) {
  195. /*
  196. * sit and sit
  197. */
  198. __raw_writeb(0, user_dog);
  199. __raw_writeb(0, kern_dog);
  200. }
  201. return NOTIFY_DONE;
  202. }
  203. static const struct file_operations sbwdog_fops =
  204. {
  205. .owner = THIS_MODULE,
  206. .llseek = no_llseek,
  207. .write = sbwdog_write,
  208. .ioctl = sbwdog_ioctl,
  209. .open = sbwdog_open,
  210. .release = sbwdog_release,
  211. };
  212. static struct miscdevice sbwdog_miscdev =
  213. {
  214. .minor = WATCHDOG_MINOR,
  215. .name = "watchdog",
  216. .fops = &sbwdog_fops,
  217. };
  218. static struct notifier_block sbwdog_notifier = {
  219. .notifier_call = sbwdog_notify_sys,
  220. };
  221. /*
  222. * interrupt handler
  223. *
  224. * doesn't do a whole lot for user, but oh so cleverly written so kernel
  225. * code can use it to re-up the watchdog, thereby saving the kernel from
  226. * having to create and maintain a timer, just to tickle another timer,
  227. * which is just so wrong.
  228. */
  229. irqreturn_t sbwdog_interrupt(int irq, void *addr)
  230. {
  231. unsigned long wd_init;
  232. char *wd_cfg_reg = (char *)addr;
  233. u8 cfg;
  234. cfg = __raw_readb(wd_cfg_reg);
  235. wd_init = __raw_readq(wd_cfg_reg - 8) & 0x7fffff;
  236. /*
  237. * if it's the second watchdog timer, it's for those users
  238. */
  239. if (wd_cfg_reg == user_dog) {
  240. printk(KERN_CRIT
  241. "%s in danger of initiating system reset in %ld.%01ld seconds\n",
  242. ident.identity, wd_init / 1000000, (wd_init / 100000) % 10);
  243. } else {
  244. cfg |= 1;
  245. }
  246. __raw_writeb(cfg, wd_cfg_reg);
  247. return IRQ_HANDLED;
  248. }
  249. static int __init sbwdog_init(void)
  250. {
  251. int ret;
  252. /*
  253. * register a reboot notifier
  254. */
  255. ret = register_reboot_notifier(&sbwdog_notifier);
  256. if (ret) {
  257. printk (KERN_ERR "%s: cannot register reboot notifier (err=%d)\n",
  258. ident.identity, ret);
  259. return ret;
  260. }
  261. /*
  262. * get the resources
  263. */
  264. ret = misc_register(&sbwdog_miscdev);
  265. if (ret == 0) {
  266. printk(KERN_INFO "%s: timeout is %ld.%ld secs\n", ident.identity,
  267. timeout / 1000000, (timeout / 100000) % 10);
  268. }
  269. ret = request_irq(1, sbwdog_interrupt, IRQF_DISABLED | IRQF_SHARED,
  270. ident.identity, (void *)user_dog);
  271. if (ret) {
  272. printk(KERN_ERR "%s: failed to request irq 1 - %d\n", ident.identity,
  273. ret);
  274. misc_deregister(&sbwdog_miscdev);
  275. }
  276. return ret;
  277. }
  278. static void __exit sbwdog_exit(void)
  279. {
  280. misc_deregister(&sbwdog_miscdev);
  281. }
  282. module_init(sbwdog_init);
  283. module_exit(sbwdog_exit);
  284. MODULE_AUTHOR("Andrew Sharp <andy.sharp@onstor.com>");
  285. MODULE_DESCRIPTION("SiByte Watchdog");
  286. module_param(timeout, ulong, 0);
  287. MODULE_PARM_DESC(timeout,
  288. "Watchdog timeout in microseconds (max/default 8388607 or 8.3ish secs)");
  289. MODULE_LICENSE("GPL");
  290. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  291. /*
  292. * example code that can be put in a platform code area to utilize the
  293. * first watchdog timer for the kernels own purpose.
  294. void
  295. platform_wd_setup(void)
  296. {
  297. int ret;
  298. ret = request_irq(0, sbwdog_interrupt, IRQF_DISABLED | IRQF_SHARED,
  299. "Kernel Watchdog", IOADDR(A_SCD_WDOG_CFG_0));
  300. if (ret) {
  301. printk(KERN_CRIT "Watchdog IRQ zero(0) failed to be requested - %d\n",
  302. ret);
  303. }
  304. }
  305. */