riowatchdog.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* $Id: riowatchdog.c,v 1.3.2.2 2002/01/23 18:48:02 davem Exp $
  2. * riowatchdog.c - driver for hw watchdog inside Super I/O of RIO
  3. *
  4. * Copyright (C) 2001 David S. Miller (davem@redhat.com)
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/types.h>
  9. #include <linux/fs.h>
  10. #include <linux/errno.h>
  11. #include <linux/init.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/smp_lock.h>
  14. #include <asm/io.h>
  15. #include <asm/ebus.h>
  16. #include <asm/bbc.h>
  17. #include <asm/oplib.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/watchdog.h>
  20. /* RIO uses the NatSemi Super I/O power management logical device
  21. * as its' watchdog.
  22. *
  23. * When the watchdog triggers, it asserts a line to the BBC (Boot Bus
  24. * Controller) of the machine. The BBC can only be configured to
  25. * trigger a power-on reset when the signal is asserted. The BBC
  26. * can be configured to ignore the signal entirely as well.
  27. *
  28. * The only Super I/O device register we care about is at index
  29. * 0x05 (WDTO_INDEX) which is the watchdog time-out in minutes (1-255).
  30. * If set to zero, this disables the watchdog. When set, the system
  31. * must periodically (before watchdog expires) clear (set to zero) and
  32. * re-set the watchdog else it will trigger.
  33. *
  34. * There are two other indexed watchdog registers inside this Super I/O
  35. * logical device, but they are unused. The first, at index 0x06 is
  36. * the watchdog control and can be used to make the watchdog timer re-set
  37. * when the PS/2 mouse or serial lines show activity. The second, at
  38. * index 0x07 is merely a sampling of the line from the watchdog to the
  39. * BBC.
  40. *
  41. * The watchdog device generates no interrupts.
  42. */
  43. MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
  44. MODULE_DESCRIPTION("Hardware watchdog driver for Sun RIO");
  45. MODULE_SUPPORTED_DEVICE("watchdog");
  46. MODULE_LICENSE("GPL");
  47. #define RIOWD_NAME "pmc"
  48. #define RIOWD_MINOR 215
  49. static DEFINE_SPINLOCK(riowd_lock);
  50. static void __iomem *bbc_regs;
  51. static void __iomem *riowd_regs;
  52. #define WDTO_INDEX 0x05
  53. static int riowd_timeout = 1; /* in minutes */
  54. module_param(riowd_timeout, int, 0);
  55. MODULE_PARM_DESC(riowd_timeout, "Watchdog timeout in minutes");
  56. #if 0 /* Currently unused. */
  57. static u8 riowd_readreg(int index)
  58. {
  59. unsigned long flags;
  60. u8 ret;
  61. spin_lock_irqsave(&riowd_lock, flags);
  62. writeb(index, riowd_regs + 0);
  63. ret = readb(riowd_regs + 1);
  64. spin_unlock_irqrestore(&riowd_lock, flags);
  65. return ret;
  66. }
  67. #endif
  68. static void riowd_writereg(u8 val, int index)
  69. {
  70. unsigned long flags;
  71. spin_lock_irqsave(&riowd_lock, flags);
  72. writeb(index, riowd_regs + 0);
  73. writeb(val, riowd_regs + 1);
  74. spin_unlock_irqrestore(&riowd_lock, flags);
  75. }
  76. static void riowd_pingtimer(void)
  77. {
  78. riowd_writereg(riowd_timeout, WDTO_INDEX);
  79. }
  80. static void riowd_stoptimer(void)
  81. {
  82. u8 val;
  83. riowd_writereg(0, WDTO_INDEX);
  84. val = readb(bbc_regs + BBC_WDACTION);
  85. val &= ~BBC_WDACTION_RST;
  86. writeb(val, bbc_regs + BBC_WDACTION);
  87. }
  88. static void riowd_starttimer(void)
  89. {
  90. u8 val;
  91. riowd_writereg(riowd_timeout, WDTO_INDEX);
  92. val = readb(bbc_regs + BBC_WDACTION);
  93. val |= BBC_WDACTION_RST;
  94. writeb(val, bbc_regs + BBC_WDACTION);
  95. }
  96. static int riowd_open(struct inode *inode, struct file *filp)
  97. {
  98. cycle_kernel_lock();
  99. nonseekable_open(inode, filp);
  100. return 0;
  101. }
  102. static int riowd_release(struct inode *inode, struct file *filp)
  103. {
  104. return 0;
  105. }
  106. static int riowd_ioctl(struct inode *inode, struct file *filp,
  107. unsigned int cmd, unsigned long arg)
  108. {
  109. static struct watchdog_info info = {
  110. WDIOF_SETTIMEOUT, 0, "Natl. Semiconductor PC97317"
  111. };
  112. void __user *argp = (void __user *)arg;
  113. unsigned int options;
  114. int new_margin;
  115. switch (cmd) {
  116. case WDIOC_GETSUPPORT:
  117. if (copy_to_user(argp, &info, sizeof(info)))
  118. return -EFAULT;
  119. break;
  120. case WDIOC_GETSTATUS:
  121. case WDIOC_GETBOOTSTATUS:
  122. if (put_user(0, (int __user *)argp))
  123. return -EFAULT;
  124. break;
  125. case WDIOC_KEEPALIVE:
  126. riowd_pingtimer();
  127. break;
  128. case WDIOC_SETOPTIONS:
  129. if (copy_from_user(&options, argp, sizeof(options)))
  130. return -EFAULT;
  131. if (options & WDIOS_DISABLECARD)
  132. riowd_stoptimer();
  133. else if (options & WDIOS_ENABLECARD)
  134. riowd_starttimer();
  135. else
  136. return -EINVAL;
  137. break;
  138. case WDIOC_SETTIMEOUT:
  139. if (get_user(new_margin, (int __user *)argp))
  140. return -EFAULT;
  141. if ((new_margin < 60) || (new_margin > (255 * 60)))
  142. return -EINVAL;
  143. riowd_timeout = (new_margin + 59) / 60;
  144. riowd_pingtimer();
  145. /* Fall */
  146. case WDIOC_GETTIMEOUT:
  147. return put_user(riowd_timeout * 60, (int __user *)argp);
  148. default:
  149. return -EINVAL;
  150. };
  151. return 0;
  152. }
  153. static ssize_t riowd_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  154. {
  155. if (count) {
  156. riowd_pingtimer();
  157. return 1;
  158. }
  159. return 0;
  160. }
  161. static const struct file_operations riowd_fops = {
  162. .owner = THIS_MODULE,
  163. .ioctl = riowd_ioctl,
  164. .open = riowd_open,
  165. .write = riowd_write,
  166. .release = riowd_release,
  167. };
  168. static struct miscdevice riowd_miscdev = { RIOWD_MINOR, RIOWD_NAME, &riowd_fops };
  169. static int __init riowd_bbc_init(void)
  170. {
  171. struct linux_ebus *ebus = NULL;
  172. struct linux_ebus_device *edev = NULL;
  173. u8 val;
  174. for_each_ebus(ebus) {
  175. for_each_ebusdev(edev, ebus) {
  176. if (!strcmp(edev->ofdev.node->name, "bbc"))
  177. goto found_bbc;
  178. }
  179. }
  180. found_bbc:
  181. if (!edev)
  182. return -ENODEV;
  183. bbc_regs = ioremap(edev->resource[0].start, BBC_REGS_SIZE);
  184. if (!bbc_regs)
  185. return -ENODEV;
  186. /* Turn it off. */
  187. val = readb(bbc_regs + BBC_WDACTION);
  188. val &= ~BBC_WDACTION_RST;
  189. writeb(val, bbc_regs + BBC_WDACTION);
  190. return 0;
  191. }
  192. static int __init riowd_init(void)
  193. {
  194. struct linux_ebus *ebus = NULL;
  195. struct linux_ebus_device *edev = NULL;
  196. for_each_ebus(ebus) {
  197. for_each_ebusdev(edev, ebus) {
  198. if (!strcmp(edev->ofdev.node->name, RIOWD_NAME))
  199. goto ebus_done;
  200. }
  201. }
  202. ebus_done:
  203. if (!edev)
  204. goto fail;
  205. riowd_regs = ioremap(edev->resource[0].start, 2);
  206. if (riowd_regs == NULL) {
  207. printk(KERN_ERR "pmc: Cannot map registers.\n");
  208. return -ENODEV;
  209. }
  210. if (riowd_bbc_init()) {
  211. printk(KERN_ERR "pmc: Failure initializing BBC config.\n");
  212. goto fail;
  213. }
  214. if (misc_register(&riowd_miscdev)) {
  215. printk(KERN_ERR "pmc: Cannot register watchdog misc device.\n");
  216. goto fail;
  217. }
  218. printk(KERN_INFO "pmc: Hardware watchdog [%i minutes], "
  219. "regs at %p\n", riowd_timeout, riowd_regs);
  220. return 0;
  221. fail:
  222. if (riowd_regs) {
  223. iounmap(riowd_regs);
  224. riowd_regs = NULL;
  225. }
  226. if (bbc_regs) {
  227. iounmap(bbc_regs);
  228. bbc_regs = NULL;
  229. }
  230. return -ENODEV;
  231. }
  232. static void __exit riowd_cleanup(void)
  233. {
  234. misc_deregister(&riowd_miscdev);
  235. iounmap(riowd_regs);
  236. riowd_regs = NULL;
  237. iounmap(bbc_regs);
  238. bbc_regs = NULL;
  239. }
  240. module_init(riowd_init);
  241. module_exit(riowd_cleanup);