wdt285.c 4.8 KB

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