riowd.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* riowd.c - driver for hw watchdog inside Super I/O of RIO
  3. *
  4. * Copyright (C) 2001, 2008 David S. Miller (davem@davemloft.net)
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/types.h>
  10. #include <linux/fs.h>
  11. #include <linux/errno.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/watchdog.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/io.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/slab.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@davemloft.net>");
  43. MODULE_DESCRIPTION("Hardware watchdog driver for Sun RIO");
  44. MODULE_SUPPORTED_DEVICE("watchdog");
  45. MODULE_LICENSE("GPL");
  46. #define DRIVER_NAME "riowd"
  47. #define PFX DRIVER_NAME ": "
  48. struct riowd {
  49. void __iomem *regs;
  50. spinlock_t lock;
  51. };
  52. static struct riowd *riowd_device;
  53. #define WDTO_INDEX 0x05
  54. static int riowd_timeout = 1; /* in minutes */
  55. module_param(riowd_timeout, int, 0);
  56. MODULE_PARM_DESC(riowd_timeout, "Watchdog timeout in minutes");
  57. static void riowd_writereg(struct riowd *p, u8 val, int index)
  58. {
  59. unsigned long flags;
  60. spin_lock_irqsave(&p->lock, flags);
  61. writeb(index, p->regs + 0);
  62. writeb(val, p->regs + 1);
  63. spin_unlock_irqrestore(&p->lock, flags);
  64. }
  65. static int riowd_open(struct inode *inode, struct file *filp)
  66. {
  67. stream_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 const 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. fallthrough;
  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,
  124. 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. .unlocked_ioctl = riowd_ioctl,
  137. .compat_ioctl = compat_ptr_ioctl,
  138. .open = riowd_open,
  139. .write = riowd_write,
  140. .release = riowd_release,
  141. };
  142. static struct miscdevice riowd_miscdev = {
  143. .minor = WATCHDOG_MINOR,
  144. .name = "watchdog",
  145. .fops = &riowd_fops
  146. };
  147. static int riowd_probe(struct platform_device *op)
  148. {
  149. struct riowd *p;
  150. int err = -EINVAL;
  151. if (riowd_device)
  152. goto out;
  153. err = -ENOMEM;
  154. p = devm_kzalloc(&op->dev, 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. pr_err("Cannot map registers\n");
  161. goto out;
  162. }
  163. /* Make miscdev useable right away */
  164. riowd_device = p;
  165. err = misc_register(&riowd_miscdev);
  166. if (err) {
  167. pr_err("Cannot register watchdog misc device\n");
  168. goto out_iounmap;
  169. }
  170. pr_info("Hardware watchdog [%i minutes], regs at %p\n",
  171. riowd_timeout, p->regs);
  172. platform_set_drvdata(op, p);
  173. return 0;
  174. out_iounmap:
  175. riowd_device = NULL;
  176. of_iounmap(&op->resource[0], p->regs, 2);
  177. out:
  178. return err;
  179. }
  180. static int riowd_remove(struct platform_device *op)
  181. {
  182. struct riowd *p = platform_get_drvdata(op);
  183. misc_deregister(&riowd_miscdev);
  184. of_iounmap(&op->resource[0], p->regs, 2);
  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 platform_driver riowd_driver = {
  195. .driver = {
  196. .name = DRIVER_NAME,
  197. .of_match_table = riowd_match,
  198. },
  199. .probe = riowd_probe,
  200. .remove = riowd_remove,
  201. };
  202. module_platform_driver(riowd_driver);