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