riowd.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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/miscdevice.h>
  12. #include <linux/watchdog.h>
  13. #include <linux/of.h>
  14. #include <linux/of_device.h>
  15. #include <linux/io.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/slab.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. nonseekable_open(inode, filp);
  67. return 0;
  68. }
  69. static int riowd_release(struct inode *inode, struct file *filp)
  70. {
  71. return 0;
  72. }
  73. static long riowd_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  74. {
  75. static const struct watchdog_info info = {
  76. .options = WDIOF_SETTIMEOUT,
  77. .firmware_version = 1,
  78. .identity = DRIVER_NAME,
  79. };
  80. void __user *argp = (void __user *)arg;
  81. struct riowd *p = riowd_device;
  82. unsigned int options;
  83. int new_margin;
  84. switch (cmd) {
  85. case WDIOC_GETSUPPORT:
  86. if (copy_to_user(argp, &info, sizeof(info)))
  87. return -EFAULT;
  88. break;
  89. case WDIOC_GETSTATUS:
  90. case WDIOC_GETBOOTSTATUS:
  91. if (put_user(0, (int __user *)argp))
  92. return -EFAULT;
  93. break;
  94. case WDIOC_KEEPALIVE:
  95. riowd_writereg(p, riowd_timeout, WDTO_INDEX);
  96. break;
  97. case WDIOC_SETOPTIONS:
  98. if (copy_from_user(&options, argp, sizeof(options)))
  99. return -EFAULT;
  100. if (options & WDIOS_DISABLECARD)
  101. riowd_writereg(p, 0, WDTO_INDEX);
  102. else if (options & WDIOS_ENABLECARD)
  103. riowd_writereg(p, riowd_timeout, WDTO_INDEX);
  104. else
  105. return -EINVAL;
  106. break;
  107. case WDIOC_SETTIMEOUT:
  108. if (get_user(new_margin, (int __user *)argp))
  109. return -EFAULT;
  110. if ((new_margin < 60) || (new_margin > (255 * 60)))
  111. return -EINVAL;
  112. riowd_timeout = (new_margin + 59) / 60;
  113. riowd_writereg(p, riowd_timeout, WDTO_INDEX);
  114. /* Fall */
  115. case WDIOC_GETTIMEOUT:
  116. return put_user(riowd_timeout * 60, (int __user *)argp);
  117. default:
  118. return -EINVAL;
  119. };
  120. return 0;
  121. }
  122. static ssize_t riowd_write(struct file *file, const char __user *buf,
  123. 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 riowd_probe(struct platform_device *op)
  146. {
  147. struct riowd *p;
  148. int err = -EINVAL;
  149. if (riowd_device)
  150. goto out;
  151. err = -ENOMEM;
  152. p = devm_kzalloc(&op->dev, sizeof(*p), GFP_KERNEL);
  153. if (!p)
  154. goto out;
  155. spin_lock_init(&p->lock);
  156. p->regs = of_ioremap(&op->resource[0], 0, 2, DRIVER_NAME);
  157. if (!p->regs) {
  158. pr_err("Cannot map registers\n");
  159. goto out;
  160. }
  161. /* Make miscdev useable right away */
  162. riowd_device = p;
  163. err = misc_register(&riowd_miscdev);
  164. if (err) {
  165. pr_err("Cannot register watchdog misc device\n");
  166. goto out_iounmap;
  167. }
  168. pr_info("Hardware watchdog [%i minutes], regs at %p\n",
  169. riowd_timeout, p->regs);
  170. platform_set_drvdata(op, p);
  171. return 0;
  172. out_iounmap:
  173. riowd_device = NULL;
  174. of_iounmap(&op->resource[0], p->regs, 2);
  175. out:
  176. return err;
  177. }
  178. static int riowd_remove(struct platform_device *op)
  179. {
  180. struct riowd *p = platform_get_drvdata(op);
  181. misc_deregister(&riowd_miscdev);
  182. of_iounmap(&op->resource[0], p->regs, 2);
  183. return 0;
  184. }
  185. static const struct of_device_id riowd_match[] = {
  186. {
  187. .name = "pmc",
  188. },
  189. {},
  190. };
  191. MODULE_DEVICE_TABLE(of, riowd_match);
  192. static struct platform_driver riowd_driver = {
  193. .driver = {
  194. .name = DRIVER_NAME,
  195. .of_match_table = riowd_match,
  196. },
  197. .probe = riowd_probe,
  198. .remove = riowd_remove,
  199. };
  200. module_platform_driver(riowd_driver);