riowd.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 long riowd_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  75. {
  76. static struct watchdog_info info = {
  77. .options = WDIOF_SETTIMEOUT,
  78. .firmware_version = 1,
  79. .identity = DRIVER_NAME,
  80. };
  81. void __user *argp = (void __user *)arg;
  82. struct riowd *p = riowd_device;
  83. unsigned int options;
  84. int new_margin;
  85. switch (cmd) {
  86. case WDIOC_GETSUPPORT:
  87. if (copy_to_user(argp, &info, sizeof(info)))
  88. return -EFAULT;
  89. break;
  90. case WDIOC_GETSTATUS:
  91. case WDIOC_GETBOOTSTATUS:
  92. if (put_user(0, (int __user *)argp))
  93. return -EFAULT;
  94. break;
  95. case WDIOC_KEEPALIVE:
  96. riowd_writereg(p, riowd_timeout, WDTO_INDEX);
  97. break;
  98. case WDIOC_SETOPTIONS:
  99. if (copy_from_user(&options, argp, sizeof(options)))
  100. return -EFAULT;
  101. if (options & WDIOS_DISABLECARD)
  102. riowd_writereg(p, 0, WDTO_INDEX);
  103. else if (options & WDIOS_ENABLECARD)
  104. riowd_writereg(p, riowd_timeout, WDTO_INDEX);
  105. else
  106. return -EINVAL;
  107. break;
  108. case WDIOC_SETTIMEOUT:
  109. if (get_user(new_margin, (int __user *)argp))
  110. return -EFAULT;
  111. if ((new_margin < 60) || (new_margin > (255 * 60)))
  112. return -EINVAL;
  113. riowd_timeout = (new_margin + 59) / 60;
  114. riowd_writereg(p, riowd_timeout, WDTO_INDEX);
  115. /* Fall */
  116. case WDIOC_GETTIMEOUT:
  117. return put_user(riowd_timeout * 60, (int __user *)argp);
  118. default:
  119. return -EINVAL;
  120. };
  121. return 0;
  122. }
  123. static ssize_t riowd_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  124. {
  125. struct riowd *p = riowd_device;
  126. if (count) {
  127. riowd_writereg(p, riowd_timeout, WDTO_INDEX);
  128. return 1;
  129. }
  130. return 0;
  131. }
  132. static const struct file_operations riowd_fops = {
  133. .owner = THIS_MODULE,
  134. .llseek = no_llseek,
  135. .unlocked_ioctl = riowd_ioctl,
  136. .open = riowd_open,
  137. .write = riowd_write,
  138. .release = riowd_release,
  139. };
  140. static struct miscdevice riowd_miscdev = {
  141. .minor = WATCHDOG_MINOR,
  142. .name = "watchdog",
  143. .fops = &riowd_fops
  144. };
  145. static int __devinit riowd_probe(struct of_device *op,
  146. const struct of_device_id *match)
  147. {
  148. struct riowd *p;
  149. int err = -EINVAL;
  150. if (riowd_device)
  151. goto out;
  152. err = -ENOMEM;
  153. p = kzalloc(sizeof(*p), GFP_KERNEL);
  154. if (!p)
  155. goto out;
  156. spin_lock_init(&p->lock);
  157. p->regs = of_ioremap(&op->resource[0], 0, 2, DRIVER_NAME);
  158. if (!p->regs) {
  159. printk(KERN_ERR PFX "Cannot map registers.\n");
  160. goto out_free;
  161. }
  162. err = misc_register(&riowd_miscdev);
  163. if (err) {
  164. printk(KERN_ERR PFX "Cannot register watchdog misc device.\n");
  165. goto out_iounmap;
  166. }
  167. printk(KERN_INFO PFX "Hardware watchdog [%i minutes], "
  168. "regs at %p\n", riowd_timeout, p->regs);
  169. dev_set_drvdata(&op->dev, p);
  170. riowd_device = p;
  171. err = 0;
  172. out_iounmap:
  173. of_iounmap(&op->resource[0], p->regs, 2);
  174. out_free:
  175. kfree(p);
  176. out:
  177. return err;
  178. }
  179. static int __devexit riowd_remove(struct of_device *op)
  180. {
  181. struct riowd *p = dev_get_drvdata(&op->dev);
  182. misc_deregister(&riowd_miscdev);
  183. of_iounmap(&op->resource[0], p->regs, 2);
  184. kfree(p);
  185. return 0;
  186. }
  187. static const struct of_device_id riowd_match[] = {
  188. {
  189. .name = "pmc",
  190. },
  191. {},
  192. };
  193. MODULE_DEVICE_TABLE(of, riowd_match);
  194. static struct of_platform_driver riowd_driver = {
  195. .name = DRIVER_NAME,
  196. .match_table = riowd_match,
  197. .probe = riowd_probe,
  198. .remove = __devexit_p(riowd_remove),
  199. };
  200. static int __init riowd_init(void)
  201. {
  202. return of_register_driver(&riowd_driver, &of_bus_type);
  203. }
  204. static void __exit riowd_exit(void)
  205. {
  206. of_unregister_driver(&riowd_driver);
  207. }
  208. module_init(riowd_init);
  209. module_exit(riowd_exit);