wdt285.c 4.7 KB

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