sb_wdog.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. 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. 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. nonseekable_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. 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((u32)__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. pr_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. pr_err("%s: cannot register reboot notifier (err=%d)\n",
  259. ident.identity, ret);
  260. return ret;
  261. }
  262. /*
  263. * get the resources
  264. */
  265. ret = request_irq(1, sbwdog_interrupt, IRQF_SHARED,
  266. ident.identity, (void *)user_dog);
  267. if (ret) {
  268. pr_err("%s: failed to request irq 1 - %d\n",
  269. ident.identity, ret);
  270. goto out;
  271. }
  272. ret = misc_register(&sbwdog_miscdev);
  273. if (ret == 0) {
  274. pr_info("%s: timeout is %ld.%ld secs\n",
  275. ident.identity,
  276. timeout / 1000000, (timeout / 100000) % 10);
  277. return 0;
  278. }
  279. free_irq(1, (void *)user_dog);
  280. out:
  281. unregister_reboot_notifier(&sbwdog_notifier);
  282. return ret;
  283. }
  284. static void __exit sbwdog_exit(void)
  285. {
  286. misc_deregister(&sbwdog_miscdev);
  287. free_irq(1, (void *)user_dog);
  288. unregister_reboot_notifier(&sbwdog_notifier);
  289. }
  290. module_init(sbwdog_init);
  291. module_exit(sbwdog_exit);
  292. MODULE_AUTHOR("Andrew Sharp <andy.sharp@lsi.com>");
  293. MODULE_DESCRIPTION("SiByte Watchdog");
  294. module_param(timeout, ulong, 0);
  295. MODULE_PARM_DESC(timeout,
  296. "Watchdog timeout in microseconds (max/default 8388607 or 8.3ish secs)");
  297. MODULE_LICENSE("GPL");
  298. /*
  299. * example code that can be put in a platform code area to utilize the
  300. * first watchdog timer for the kernels own purpose.
  301. void platform_wd_setup(void)
  302. {
  303. int ret;
  304. ret = request_irq(1, sbwdog_interrupt, IRQF_SHARED,
  305. "Kernel Watchdog", IOADDR(A_SCD_WDOG_CFG_0));
  306. if (ret) {
  307. pr_crit("Watchdog IRQ zero(0) failed to be requested - %d\n", ret);
  308. }
  309. }
  310. */