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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  47. #include <linux/module.h>
  48. #include <linux/io.h>
  49. #include <linux/uaccess.h>
  50. #include <linux/fs.h>
  51. #include <linux/reboot.h>
  52. #include <linux/miscdevice.h>
  53. #include <linux/watchdog.h>
  54. #include <linux/interrupt.h>
  55. #include <asm/sibyte/sb1250.h>
  56. #include <asm/sibyte/sb1250_regs.h>
  57. #include <asm/sibyte/sb1250_int.h>
  58. #include <asm/sibyte/sb1250_scd.h>
  59. static DEFINE_SPINLOCK(sbwd_lock);
  60. /*
  61. * set the initial count value of a timer
  62. *
  63. * wdog is the iomem address of the cfg register
  64. */
  65. static void sbwdog_set(char __iomem *wdog, unsigned long t)
  66. {
  67. spin_lock(&sbwd_lock);
  68. __raw_writeb(0, wdog);
  69. __raw_writeq(t & 0x7fffffUL, wdog - 0x10);
  70. spin_unlock(&sbwd_lock);
  71. }
  72. /*
  73. * cause the timer to [re]load it's initial count and start counting
  74. * all over again
  75. *
  76. * wdog is the iomem address of the cfg register
  77. */
  78. static void sbwdog_pet(char __iomem *wdog)
  79. {
  80. spin_lock(&sbwd_lock);
  81. __raw_writeb(__raw_readb(wdog) | 1, wdog);
  82. spin_unlock(&sbwd_lock);
  83. }
  84. static unsigned long sbwdog_gate; /* keeps it to one thread only */
  85. static char __iomem *kern_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_0));
  86. static char __iomem *user_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_1));
  87. static unsigned long timeout = 0x7fffffUL; /* useconds: 8.3ish secs. */
  88. static int expect_close;
  89. static const struct watchdog_info ident = {
  90. .options = WDIOF_CARDRESET | WDIOF_SETTIMEOUT |
  91. WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  92. .identity = "SiByte Watchdog",
  93. };
  94. /*
  95. * Allow only a single thread to walk the dog
  96. */
  97. static int sbwdog_open(struct inode *inode, struct file *file)
  98. {
  99. stream_open(inode, file);
  100. if (test_and_set_bit(0, &sbwdog_gate))
  101. return -EBUSY;
  102. __module_get(THIS_MODULE);
  103. /*
  104. * Activate the timer
  105. */
  106. sbwdog_set(user_dog, timeout);
  107. __raw_writeb(1, user_dog);
  108. return 0;
  109. }
  110. /*
  111. * Put the dog back in the kennel.
  112. */
  113. static int sbwdog_release(struct inode *inode, struct file *file)
  114. {
  115. if (expect_close == 42) {
  116. __raw_writeb(0, user_dog);
  117. module_put(THIS_MODULE);
  118. } else {
  119. pr_crit("%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. /* Fall through */
  182. case WDIOC_GETTIMEOUT:
  183. /*
  184. * get the remaining count from the ... count register
  185. * which is 1*8 before the config register
  186. */
  187. ret = put_user((u32)__raw_readq(user_dog - 8) / 1000000, p);
  188. break;
  189. }
  190. return ret;
  191. }
  192. /*
  193. * Notifier for system down
  194. */
  195. static int sbwdog_notify_sys(struct notifier_block *this, unsigned long code,
  196. void *erf)
  197. {
  198. if (code == SYS_DOWN || code == SYS_HALT) {
  199. /*
  200. * sit and sit
  201. */
  202. __raw_writeb(0, user_dog);
  203. __raw_writeb(0, kern_dog);
  204. }
  205. return NOTIFY_DONE;
  206. }
  207. static const struct file_operations sbwdog_fops = {
  208. .owner = THIS_MODULE,
  209. .llseek = no_llseek,
  210. .write = sbwdog_write,
  211. .unlocked_ioctl = sbwdog_ioctl,
  212. .open = sbwdog_open,
  213. .release = sbwdog_release,
  214. };
  215. static struct miscdevice sbwdog_miscdev = {
  216. .minor = WATCHDOG_MINOR,
  217. .name = "watchdog",
  218. .fops = &sbwdog_fops,
  219. };
  220. static struct notifier_block sbwdog_notifier = {
  221. .notifier_call = sbwdog_notify_sys,
  222. };
  223. /*
  224. * interrupt handler
  225. *
  226. * doesn't do a whole lot for user, but oh so cleverly written so kernel
  227. * code can use it to re-up the watchdog, thereby saving the kernel from
  228. * having to create and maintain a timer, just to tickle another timer,
  229. * which is just so wrong.
  230. */
  231. irqreturn_t sbwdog_interrupt(int irq, void *addr)
  232. {
  233. unsigned long wd_init;
  234. char *wd_cfg_reg = (char *)addr;
  235. u8 cfg;
  236. cfg = __raw_readb(wd_cfg_reg);
  237. wd_init = __raw_readq(wd_cfg_reg - 8) & 0x7fffff;
  238. /*
  239. * if it's the second watchdog timer, it's for those users
  240. */
  241. if (wd_cfg_reg == user_dog)
  242. pr_crit("%s in danger of initiating system reset "
  243. "in %ld.%01ld seconds\n",
  244. ident.identity,
  245. wd_init / 1000000, (wd_init / 100000) % 10);
  246. else
  247. cfg |= 1;
  248. __raw_writeb(cfg, wd_cfg_reg);
  249. return IRQ_HANDLED;
  250. }
  251. static int __init sbwdog_init(void)
  252. {
  253. int ret;
  254. /*
  255. * register a reboot notifier
  256. */
  257. ret = register_reboot_notifier(&sbwdog_notifier);
  258. if (ret) {
  259. pr_err("%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_SHARED,
  267. ident.identity, (void *)user_dog);
  268. if (ret) {
  269. pr_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. pr_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. /*
  300. * example code that can be put in a platform code area to utilize the
  301. * first watchdog timer for the kernels own purpose.
  302. void platform_wd_setup(void)
  303. {
  304. int ret;
  305. ret = request_irq(1, sbwdog_interrupt, IRQF_SHARED,
  306. "Kernel Watchdog", IOADDR(A_SCD_WDOG_CFG_0));
  307. if (ret) {
  308. pr_crit("Watchdog IRQ zero(0) failed to be requested - %d\n", ret);
  309. }
  310. }
  311. */