riowatchdog.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:6k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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 spinlock_t riowd_lock = SPIN_LOCK_UNLOCKED;
  49. static void *bbc_regs;
  50. static void *riowd_regs;
  51. #define WDTO_INDEX 0x05
  52. static int riowd_timeout = 1; /* in minutes */
  53. MODULE_PARM(riowd_timeout,"i");
  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. return 0;
  98. }
  99. static int riowd_release(struct inode *inode, struct file *filp)
  100. {
  101. return 0;
  102. }
  103. static int riowd_ioctl(struct inode *inode, struct file *filp,
  104.        unsigned int cmd, unsigned long arg)
  105. {
  106. static struct watchdog_info info = {
  107.         WDIOF_SETTIMEOUT, 0, "Natl. Semiconductor PC97317"
  108. };
  109. unsigned int options;
  110. int new_margin;
  111. switch (cmd) {
  112. case WDIOC_GETSUPPORT:
  113. if (copy_to_user((struct watchdog_info *) arg, &info, sizeof(info)))
  114. return -EFAULT;
  115. break;
  116. case WDIOC_GETSTATUS:
  117. case WDIOC_GETBOOTSTATUS:
  118. if (put_user(0, (int *) arg))
  119. return -EFAULT;
  120. break;
  121. case WDIOC_KEEPALIVE:
  122. riowd_pingtimer();
  123. break;
  124. case WDIOC_SETOPTIONS:
  125. if (copy_from_user(&options, (void *) arg, sizeof(options)))
  126. return -EFAULT;
  127. if (options & WDIOS_DISABLECARD)
  128. riowd_stoptimer();
  129. else if (options & WDIOS_ENABLECARD)
  130. riowd_starttimer();
  131. else
  132. return -EINVAL;
  133. break;
  134. case WDIOC_SETTIMEOUT:
  135. if (get_user(new_margin, (int *)arg))
  136. return -EFAULT;
  137. if ((new_margin < 60) || (new_margin > (255 * 60)))
  138.     return -EINVAL;
  139. riowd_timeout = (new_margin + 59) / 60;
  140. riowd_pingtimer();
  141. /* Fall */
  142. case WDIOC_GETTIMEOUT:
  143. return put_user(riowd_timeout * 60, (int *)arg);
  144. default:
  145. return -EINVAL;
  146. };
  147. return 0;
  148. }
  149. static ssize_t riowd_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
  150. {
  151. if (ppos != &file->f_pos)
  152. return -ESPIPE;
  153. if (count) {
  154. riowd_pingtimer();
  155. return 1;
  156. }
  157. return 0;
  158. }
  159. static ssize_t riowd_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
  160. {
  161. return -EINVAL;
  162. }
  163. static struct file_operations riowd_fops = {
  164. owner: THIS_MODULE,
  165. ioctl: riowd_ioctl,
  166. open: riowd_open,
  167. write: riowd_write,
  168. read: riowd_read,
  169. release: riowd_release,
  170. };
  171. static struct miscdevice riowd_miscdev = { RIOWD_MINOR, RIOWD_NAME, &riowd_fops };
  172. static int __init riowd_bbc_init(void)
  173. {
  174. struct  linux_ebus *ebus = NULL;
  175. struct  linux_ebus_device *edev = NULL;
  176. u8 val;
  177. for_each_ebus(ebus) {
  178. for_each_ebusdev(edev, ebus) {
  179. if (!strcmp(edev->prom_name, "bbc"))
  180. goto found_bbc;
  181. }
  182. }
  183. found_bbc:
  184. if (!edev)
  185. return -ENODEV;
  186. bbc_regs = ioremap(edev->resource[0].start, BBC_REGS_SIZE);
  187. if (!bbc_regs)
  188. return -ENODEV;
  189. /* Turn it off. */
  190. val = readb(bbc_regs + BBC_WDACTION);
  191. val &= ~BBC_WDACTION_RST;
  192. writeb(val, bbc_regs + BBC_WDACTION);
  193. return 0;
  194. }
  195. static int __init riowd_init(void)
  196. {
  197. struct  linux_ebus *ebus = NULL;
  198. struct  linux_ebus_device *edev = NULL;
  199. for_each_ebus(ebus) {
  200. for_each_ebusdev(edev, ebus) {
  201. if (!strcmp(edev->prom_name, RIOWD_NAME))
  202. goto ebus_done;
  203. }
  204. }
  205. ebus_done:
  206. if (!edev)
  207. goto fail;
  208. riowd_regs = ioremap(edev->resource[0].start, 2);
  209. if (riowd_regs == NULL) {
  210. printk(KERN_ERR "pmc: Cannot map registers.n");
  211. return -ENODEV;
  212. }
  213. if (riowd_bbc_init()) {
  214. printk(KERN_ERR "pmc: Failure initializing BBC config.n");
  215. goto fail;
  216. }
  217. if (misc_register(&riowd_miscdev)) {
  218. printk(KERN_ERR "pmc: Cannot register watchdog misc device.n");
  219. goto fail;
  220. }
  221. printk(KERN_INFO "pmc: Hardware watchdog [%i minutes], "
  222.        "regs at %pn", riowd_timeout, riowd_regs);
  223. return 0;
  224. fail:
  225. if (riowd_regs) {
  226. iounmap(riowd_regs);
  227. riowd_regs = NULL;
  228. }
  229. if (bbc_regs) {
  230. iounmap(bbc_regs);
  231. bbc_regs = NULL;
  232. }
  233. return -ENODEV;
  234. }
  235. static void __exit riowd_cleanup(void)
  236. {
  237. misc_deregister(&riowd_miscdev);
  238. iounmap(riowd_regs);
  239. riowd_regs = NULL;
  240. iounmap(bbc_regs);
  241. bbc_regs = NULL;
  242. }
  243. module_init(riowd_init);
  244. module_exit(riowd_cleanup);