wdt285.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Intel 21285 watchdog driver
  4. * Copyright (c) Phil Blundell <pb@nexus.co.uk>, 1998
  5. *
  6. * based on
  7. *
  8. * SoftDog 0.05: A Software Watchdog Device
  9. *
  10. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  11. * All Rights Reserved.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/fs.h>
  19. #include <linux/mm.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/watchdog.h>
  22. #include <linux/reboot.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/irq.h>
  27. #include <mach/hardware.h>
  28. #include <asm/mach-types.h>
  29. #include <asm/system_info.h>
  30. #include <asm/hardware/dec21285.h>
  31. /*
  32. * Define this to stop the watchdog actually rebooting the machine.
  33. */
  34. #undef ONLY_TESTING
  35. static unsigned int soft_margin = 60; /* in seconds */
  36. static unsigned int reload;
  37. static unsigned long timer_alive;
  38. #ifdef ONLY_TESTING
  39. /*
  40. * If the timer expires..
  41. */
  42. static void watchdog_fire(int irq, void *dev_id)
  43. {
  44. pr_crit("Would Reboot\n");
  45. *CSR_TIMER4_CNTL = 0;
  46. *CSR_TIMER4_CLR = 0;
  47. }
  48. #endif
  49. /*
  50. * Refresh the timer.
  51. */
  52. static void watchdog_ping(void)
  53. {
  54. *CSR_TIMER4_LOAD = reload;
  55. }
  56. /*
  57. * Allow only one person to hold it open
  58. */
  59. static int watchdog_open(struct inode *inode, struct file *file)
  60. {
  61. int ret;
  62. if (*CSR_SA110_CNTL & (1 << 13))
  63. return -EBUSY;
  64. if (test_and_set_bit(1, &timer_alive))
  65. return -EBUSY;
  66. reload = soft_margin * (mem_fclk_21285 / 256);
  67. *CSR_TIMER4_CLR = 0;
  68. watchdog_ping();
  69. *CSR_TIMER4_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_AUTORELOAD
  70. | TIMER_CNTL_DIV256;
  71. #ifdef ONLY_TESTING
  72. ret = request_irq(IRQ_TIMER4, watchdog_fire, 0, "watchdog", NULL);
  73. if (ret) {
  74. *CSR_TIMER4_CNTL = 0;
  75. clear_bit(1, &timer_alive);
  76. }
  77. #else
  78. /*
  79. * Setting this bit is irreversible; once enabled, there is
  80. * no way to disable the watchdog.
  81. */
  82. *CSR_SA110_CNTL |= 1 << 13;
  83. ret = 0;
  84. #endif
  85. stream_open(inode, file);
  86. return ret;
  87. }
  88. /*
  89. * Shut off the timer.
  90. * Note: if we really have enabled the watchdog, there
  91. * is no way to turn off.
  92. */
  93. static int watchdog_release(struct inode *inode, struct file *file)
  94. {
  95. #ifdef ONLY_TESTING
  96. free_irq(IRQ_TIMER4, NULL);
  97. clear_bit(1, &timer_alive);
  98. #endif
  99. return 0;
  100. }
  101. static ssize_t watchdog_write(struct file *file, const char __user *data,
  102. size_t len, loff_t *ppos)
  103. {
  104. /*
  105. * Refresh the timer.
  106. */
  107. if (len)
  108. watchdog_ping();
  109. return len;
  110. }
  111. static const struct watchdog_info ident = {
  112. .options = WDIOF_SETTIMEOUT,
  113. .identity = "Footbridge Watchdog",
  114. };
  115. static long watchdog_ioctl(struct file *file, unsigned int cmd,
  116. unsigned long arg)
  117. {
  118. int __user *int_arg = (int __user *)arg;
  119. int new_margin, ret = -ENOTTY;
  120. switch (cmd) {
  121. case WDIOC_GETSUPPORT:
  122. ret = 0;
  123. if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
  124. ret = -EFAULT;
  125. break;
  126. case WDIOC_GETSTATUS:
  127. case WDIOC_GETBOOTSTATUS:
  128. ret = put_user(0, int_arg);
  129. break;
  130. case WDIOC_KEEPALIVE:
  131. watchdog_ping();
  132. ret = 0;
  133. break;
  134. case WDIOC_SETTIMEOUT:
  135. ret = get_user(new_margin, int_arg);
  136. if (ret)
  137. break;
  138. /* Arbitrary, can't find the card's limits */
  139. if (new_margin < 0 || new_margin > 60) {
  140. ret = -EINVAL;
  141. break;
  142. }
  143. soft_margin = new_margin;
  144. reload = soft_margin * (mem_fclk_21285 / 256);
  145. watchdog_ping();
  146. fallthrough;
  147. case WDIOC_GETTIMEOUT:
  148. ret = put_user(soft_margin, int_arg);
  149. break;
  150. }
  151. return ret;
  152. }
  153. static const struct file_operations watchdog_fops = {
  154. .owner = THIS_MODULE,
  155. .llseek = no_llseek,
  156. .write = watchdog_write,
  157. .unlocked_ioctl = watchdog_ioctl,
  158. .compat_ioctl = compat_ptr_ioctl,
  159. .open = watchdog_open,
  160. .release = watchdog_release,
  161. };
  162. static struct miscdevice watchdog_miscdev = {
  163. .minor = WATCHDOG_MINOR,
  164. .name = "watchdog",
  165. .fops = &watchdog_fops,
  166. };
  167. static int __init footbridge_watchdog_init(void)
  168. {
  169. int retval;
  170. if (machine_is_netwinder())
  171. return -ENODEV;
  172. retval = misc_register(&watchdog_miscdev);
  173. if (retval < 0)
  174. return retval;
  175. pr_info("Footbridge Watchdog Timer: 0.01, timer margin: %d sec\n",
  176. soft_margin);
  177. if (machine_is_cats())
  178. pr_warn("Warning: Watchdog reset may not work on this machine\n");
  179. return 0;
  180. }
  181. static void __exit footbridge_watchdog_exit(void)
  182. {
  183. misc_deregister(&watchdog_miscdev);
  184. }
  185. MODULE_AUTHOR("Phil Blundell <pb@nexus.co.uk>");
  186. MODULE_DESCRIPTION("Footbridge watchdog driver");
  187. MODULE_LICENSE("GPL");
  188. module_param(soft_margin, int, 0);
  189. MODULE_PARM_DESC(soft_margin, "Watchdog timeout in seconds");
  190. module_init(footbridge_watchdog_init);
  191. module_exit(footbridge_watchdog_exit);