riowd.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* riowd.c - driver for hw watchdog inside Super I/O of RIO
  2. *
  3. * Copyright (C) 2001, 2008 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/types.h>
  8. #include <linux/fs.h>
  9. #include <linux/errno.h>
  10. #include <linux/init.h>
  11. #include <linux/miscdevice.h>
  12. #include <linux/smp_lock.h>
  13. #include <linux/watchdog.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <asm/io.h>
  17. #include <asm/uaccess.h>
  18. /* RIO uses the NatSemi Super I/O power management logical device
  19. * as its' watchdog.
  20. *
  21. * When the watchdog triggers, it asserts a line to the BBC (Boot Bus
  22. * Controller) of the machine. The BBC can only be configured to
  23. * trigger a power-on reset when the signal is asserted. The BBC
  24. * can be configured to ignore the signal entirely as well.
  25. *
  26. * The only Super I/O device register we care about is at index
  27. * 0x05 (WDTO_INDEX) which is the watchdog time-out in minutes (1-255).
  28. * If set to zero, this disables the watchdog. When set, the system
  29. * must periodically (before watchdog expires) clear (set to zero) and
  30. * re-set the watchdog else it will trigger.
  31. *
  32. * There are two other indexed watchdog registers inside this Super I/O
  33. * logical device, but they are unused. The first, at index 0x06 is
  34. * the watchdog control and can be used to make the watchdog timer re-set
  35. * when the PS/2 mouse or serial lines show activity. The second, at
  36. * index 0x07 is merely a sampling of the line from the watchdog to the
  37. * BBC.
  38. *
  39. * The watchdog device generates no interrupts.
  40. */
  41. MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
  42. MODULE_DESCRIPTION("Hardware watchdog driver for Sun RIO");
  43. MODULE_SUPPORTED_DEVICE("watchdog");
  44. MODULE_LICENSE("GPL");
  45. #define DRIVER_NAME "riowd"
  46. #define PFX DRIVER_NAME ": "
  47. struct riowd {
  48. void __iomem *regs;
  49. spinlock_t lock;
  50. };
  51. static struct riowd *riowd_device;
  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. static void riowd_writereg(struct riowd *p, u8 val, int index)
  57. {
  58. unsigned long flags;
  59. spin_lock_irqsave(&p->lock, flags);
  60. writeb(index, p->regs + 0);
  61. writeb(val, p->regs + 1);
  62. spin_unlock_irqrestore(&p->lock, flags);
  63. }
  64. static int riowd_open(struct inode *inode, struct file *filp)
  65. {
  66. cycle_kernel_lock();
  67. nonseekable_open(inode, filp);
  68. return 0;
  69. }
  70. static int riowd_release(struct inode *inode, struct file *filp)
  71. {
  72. return 0;
  73. }
  74. static int riowd_ioctl(struct inode *inode, struct file *filp,
  75. unsigned int cmd, unsigned long arg)
  76. {
  77. static struct watchdog_info info = {
  78. .options = WDIOF_SETTIMEOUT,
  79. .firmware_version = 1,
  80. .identity = DRIVER_NAME,
  81. };
  82. void __user *argp = (void __user *)arg;
  83. struct riowd *p = riowd_device;
  84. unsigned int options;
  85. int new_margin;
  86. switch (cmd) {
  87. case WDIOC_GETSUPPORT:
  88. if (copy_to_user(argp, &info, sizeof(info)))
  89. return -EFAULT;
  90. break;
  91. case WDIOC_GETSTATUS:
  92. case WDIOC_GETBOOTSTATUS:
  93. if (put_user(0, (int __user *)argp))
  94. return -EFAULT;
  95. break;
  96. case WDIOC_KEEPALIVE:
  97. riowd_writereg(p, riowd_timeout, WDTO_INDEX);
  98. break;
  99. case WDIOC_SETOPTIONS:
  100. if (copy_from_user(&options, argp, sizeof(options)))
  101. return -EFAULT;
  102. if (options & WDIOS_DISABLECARD)
  103. riowd_writereg(p, 0, WDTO_INDEX);
  104. else if (options & WDIOS_ENABLECARD)
  105. riowd_writereg(p, riowd_timeout, WDTO_INDEX);
  106. else
  107. return -EINVAL;
  108. break;
  109. case WDIOC_SETTIMEOUT:
  110. if (get_user(new_margin, (int __user *)argp))
  111. return -EFAULT;
  112. if ((new_margin < 60) || (new_margin > (255 * 60)))
  113. return -EINVAL;
  114. riowd_timeout = (new_margin + 59) / 60;
  115. riowd_writereg(p, riowd_timeout, WDTO_INDEX);
  116. /* Fall */
  117. case WDIOC_GETTIMEOUT:
  118. return put_user(riowd_timeout * 60, (int __user *)argp);
  119. default:
  120. return -EINVAL;
  121. };
  122. return 0;
  123. }
  124. static ssize_t riowd_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  125. {
  126. struct riowd *p = riowd_device;
  127. if (count) {
  128. riowd_writereg(p, riowd_timeout, WDTO_INDEX);
  129. return 1;
  130. }
  131. return 0;
  132. }
  133. static const struct file_operations riowd_fops = {
  134. .owner = THIS_MODULE,
  135. .llseek = no_llseek,
  136. .ioctl = riowd_ioctl,
  137. .open = riowd_open,
  138. .write = riowd_write,
  139. .release = riowd_release,
  140. };
  141. static struct miscdevice riowd_miscdev = {
  142. .minor = WATCHDOG_MINOR,
  143. .name = "watchdog",
  144. .fops = &riowd_fops
  145. };
  146. static int __devinit riowd_probe(struct of_device *op,
  147. const struct of_device_id *match)
  148. {
  149. struct riowd *p;
  150. int err = -EINVAL;
  151. if (riowd_device)
  152. goto out;
  153. err = -ENOMEM;
  154. p = kzalloc(sizeof(*p), GFP_KERNEL);
  155. if (!p)
  156. goto out;
  157. spin_lock_init(&p->lock);
  158. p->regs = of_ioremap(&op->resource[0], 0, 2, DRIVER_NAME);
  159. if (!p->regs) {
  160. printk(KERN_ERR PFX "Cannot map registers.\n");
  161. goto out_free;
  162. }
  163. err = misc_register(&riowd_miscdev);
  164. if (err) {
  165. printk(KERN_ERR PFX "Cannot register watchdog misc device.\n");
  166. goto out_iounmap;
  167. }
  168. printk(KERN_INFO PFX "Hardware watchdog [%i minutes], "
  169. "regs at %p\n", riowd_timeout, p->regs);
  170. dev_set_drvdata(&op->dev, p);
  171. riowd_device = p;
  172. err = 0;
  173. out_iounmap:
  174. of_iounmap(&op->resource[0], p->regs, 2);
  175. out_free:
  176. kfree(p);
  177. out:
  178. return err;
  179. }
  180. static int __devexit riowd_remove(struct of_device *op)
  181. {
  182. struct riowd *p = dev_get_drvdata(&op->dev);
  183. misc_deregister(&riowd_miscdev);
  184. of_iounmap(&op->resource[0], p->regs, 2);
  185. kfree(p);
  186. return 0;
  187. }
  188. static const struct of_device_id riowd_match[] = {
  189. {
  190. .name = "pmc",
  191. },
  192. {},
  193. };
  194. MODULE_DEVICE_TABLE(of, riowd_match);
  195. static struct of_platform_driver riowd_driver = {
  196. .name = DRIVER_NAME,
  197. .match_table = riowd_match,
  198. .probe = riowd_probe,
  199. .remove = __devexit_p(riowd_remove),
  200. };
  201. static int __init riowd_init(void)
  202. {
  203. return of_register_driver(&riowd_driver, &of_bus_type);
  204. }
  205. static void __exit riowd_exit(void)
  206. {
  207. of_unregister_driver(&riowd_driver);
  208. }
  209. module_init(riowd_init);
  210. module_exit(riowd_exit);