riowatchdog.c 6.4 KB

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