sb_wdog.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Watchdog driver for SiByte SB1 SoCs
  3. *
  4. * Copyright (C) 2007 OnStor, Inc. * Andrew Sharp <andy.sharp@lsi.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@lxorguk.ukuu.org.uk>,
  39. * All Rights Reserved.
  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. static DEFINE_SPINLOCK(sbwd_lock);
  59. /*
  60. * set the initial count value of a timer
  61. *
  62. * wdog is the iomem address of the cfg register
  63. */
  64. void sbwdog_set(char __iomem *wdog, unsigned long t)
  65. {
  66. spin_lock(&sbwd_lock);
  67. __raw_writeb(0, wdog);
  68. __raw_writeq(t & 0x7fffffUL, wdog - 0x10);
  69. spin_unlock(&sbwd_lock);
  70. }
  71. /*
  72. * cause the timer to [re]load it's initial count and start counting
  73. * all over again
  74. *
  75. * wdog is the iomem address of the cfg register
  76. */
  77. void sbwdog_pet(char __iomem *wdog)
  78. {
  79. spin_lock(&sbwd_lock);
  80. __raw_writeb(__raw_readb(wdog) | 1, wdog);
  81. spin_unlock(&sbwd_lock);
  82. }
  83. static unsigned long sbwdog_gate; /* keeps it to one thread only */
  84. static char __iomem *kern_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_0));
  85. static char __iomem *user_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_1));
  86. static unsigned long timeout = 0x7fffffUL; /* useconds: 8.3ish secs. */
  87. static int expect_close;
  88. static const struct watchdog_info ident = {
  89. .options = WDIOF_CARDRESET | WDIOF_SETTIMEOUT |
  90. WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  91. .identity = "SiByte Watchdog",
  92. };
  93. /*
  94. * Allow only a single thread to walk the dog
  95. */
  96. static int sbwdog_open(struct inode *inode, struct file *file)
  97. {
  98. nonseekable_open(inode, file);
  99. if (test_and_set_bit(0, &sbwdog_gate))
  100. return -EBUSY;
  101. __module_get(THIS_MODULE);
  102. /*
  103. * Activate the timer
  104. */
  105. sbwdog_set(user_dog, timeout);
  106. __raw_writeb(1, user_dog);
  107. return 0;
  108. }
  109. /*
  110. * Put the dog back in the kennel.
  111. */
  112. static int sbwdog_release(struct inode *inode, struct file *file)
  113. {
  114. if (expect_close == 42) {
  115. __raw_writeb(0, user_dog);
  116. module_put(THIS_MODULE);
  117. } else {
  118. printk(KERN_CRIT
  119. "%s: Unexpected close, not stopping watchdog!\n",
  120. ident.identity);
  121. sbwdog_pet(user_dog);
  122. }
  123. clear_bit(0, &sbwdog_gate);
  124. expect_close = 0;
  125. return 0;
  126. }
  127. /*
  128. * 42 - the answer
  129. */
  130. static ssize_t sbwdog_write(struct file *file, const char __user *data,
  131. size_t len, loff_t *ppos)
  132. {
  133. int i;
  134. if (len) {
  135. /*
  136. * restart the timer
  137. */
  138. expect_close = 0;
  139. for (i = 0; i != len; i++) {
  140. char c;
  141. if (get_user(c, data + i))
  142. return -EFAULT;
  143. if (c == 'V')
  144. expect_close = 42;
  145. }
  146. sbwdog_pet(user_dog);
  147. }
  148. return len;
  149. }
  150. static long sbwdog_ioctl(struct file *file, unsigned int cmd,
  151. unsigned long arg)
  152. {
  153. int ret = -ENOTTY;
  154. unsigned long time;
  155. void __user *argp = (void __user *)arg;
  156. int __user *p = argp;
  157. switch (cmd) {
  158. case WDIOC_GETSUPPORT:
  159. ret = copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  160. break;
  161. case WDIOC_GETSTATUS:
  162. case WDIOC_GETBOOTSTATUS:
  163. ret = put_user(0, p);
  164. break;
  165. case WDIOC_KEEPALIVE:
  166. sbwdog_pet(user_dog);
  167. ret = 0;
  168. break;
  169. case WDIOC_SETTIMEOUT:
  170. ret = get_user(time, p);
  171. if (ret)
  172. break;
  173. time *= 1000000;
  174. if (time > 0x7fffffUL) {
  175. ret = -EINVAL;
  176. break;
  177. }
  178. timeout = time;
  179. sbwdog_set(user_dog, timeout);
  180. sbwdog_pet(user_dog);
  181. case WDIOC_GETTIMEOUT:
  182. /*
  183. * get the remaining count from the ... count register
  184. * which is 1*8 before the config register
  185. */
  186. ret = put_user(__raw_readq(user_dog - 8) / 1000000, p);
  187. break;
  188. }
  189. return ret;
  190. }
  191. /*
  192. * Notifier for system down
  193. */
  194. static int sbwdog_notify_sys(struct notifier_block *this, unsigned long code,
  195. void *erf)
  196. {
  197. if (code == SYS_DOWN || code == SYS_HALT) {
  198. /*
  199. * sit and sit
  200. */
  201. __raw_writeb(0, user_dog);
  202. __raw_writeb(0, kern_dog);
  203. }
  204. return NOTIFY_DONE;
  205. }
  206. static const struct file_operations sbwdog_fops = {
  207. .owner = THIS_MODULE,
  208. .llseek = no_llseek,
  209. .write = sbwdog_write,
  210. .unlocked_ioctl = sbwdog_ioctl,
  211. .open = sbwdog_open,
  212. .release = sbwdog_release,
  213. };
  214. static struct miscdevice sbwdog_miscdev = {
  215. .minor = WATCHDOG_MINOR,
  216. .name = "watchdog",
  217. .fops = &sbwdog_fops,
  218. };
  219. static struct notifier_block sbwdog_notifier = {
  220. .notifier_call = sbwdog_notify_sys,
  221. };
  222. /*
  223. * interrupt handler
  224. *
  225. * doesn't do a whole lot for user, but oh so cleverly written so kernel
  226. * code can use it to re-up the watchdog, thereby saving the kernel from
  227. * having to create and maintain a timer, just to tickle another timer,
  228. * which is just so wrong.
  229. */
  230. irqreturn_t sbwdog_interrupt(int irq, void *addr)
  231. {
  232. unsigned long wd_init;
  233. char *wd_cfg_reg = (char *)addr;
  234. u8 cfg;
  235. cfg = __raw_readb(wd_cfg_reg);
  236. wd_init = __raw_readq(wd_cfg_reg - 8) & 0x7fffff;
  237. /*
  238. * if it's the second watchdog timer, it's for those users
  239. */
  240. if (wd_cfg_reg == user_dog)
  241. printk(KERN_CRIT "%s in danger of initiating system reset "
  242. "in %ld.%01ld seconds\n",
  243. ident.identity,
  244. wd_init / 1000000, (wd_init / 100000) % 10);
  245. else
  246. cfg |= 1;
  247. __raw_writeb(cfg, wd_cfg_reg);
  248. return IRQ_HANDLED;
  249. }
  250. static int __init sbwdog_init(void)
  251. {
  252. int ret;
  253. /*
  254. * register a reboot notifier
  255. */
  256. ret = register_reboot_notifier(&sbwdog_notifier);
  257. if (ret) {
  258. printk(KERN_ERR
  259. "%s: cannot register reboot notifier (err=%d)\n",
  260. ident.identity, ret);
  261. return ret;
  262. }
  263. /*
  264. * get the resources
  265. */
  266. ret = request_irq(1, sbwdog_interrupt, IRQF_DISABLED | IRQF_SHARED,
  267. ident.identity, (void *)user_dog);
  268. if (ret) {
  269. printk(KERN_ERR "%s: failed to request irq 1 - %d\n",
  270. ident.identity, ret);
  271. goto out;
  272. }
  273. ret = misc_register(&sbwdog_miscdev);
  274. if (ret == 0) {
  275. printk(KERN_INFO "%s: timeout is %ld.%ld secs\n",
  276. ident.identity,
  277. timeout / 1000000, (timeout / 100000) % 10);
  278. return 0;
  279. }
  280. free_irq(1, (void *)user_dog);
  281. out:
  282. unregister_reboot_notifier(&sbwdog_notifier);
  283. return ret;
  284. }
  285. static void __exit sbwdog_exit(void)
  286. {
  287. misc_deregister(&sbwdog_miscdev);
  288. free_irq(1, (void *)user_dog);
  289. unregister_reboot_notifier(&sbwdog_notifier);
  290. }
  291. module_init(sbwdog_init);
  292. module_exit(sbwdog_exit);
  293. MODULE_AUTHOR("Andrew Sharp <andy.sharp@lsi.com>");
  294. MODULE_DESCRIPTION("SiByte Watchdog");
  295. module_param(timeout, ulong, 0);
  296. MODULE_PARM_DESC(timeout,
  297. "Watchdog timeout in microseconds (max/default 8388607 or 8.3ish secs)");
  298. MODULE_LICENSE("GPL");
  299. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  300. /*
  301. * example code that can be put in a platform code area to utilize the
  302. * first watchdog timer for the kernels own purpose.
  303. void platform_wd_setup(void)
  304. {
  305. int ret;
  306. ret = request_irq(1, sbwdog_interrupt, IRQF_DISABLED | IRQF_SHARED,
  307. "Kernel Watchdog", IOADDR(A_SCD_WDOG_CFG_0));
  308. if (ret) {
  309. printk(KERN_CRIT
  310. "Watchdog IRQ zero(0) failed to be requested - %d\n", ret);
  311. }
  312. }
  313. */