ixp2000_wdt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * drivers/char/watchdog/ixp2000_wdt.c
  3. *
  4. * Watchdog driver for Intel IXP2000 network processors
  5. *
  6. * Adapted from the IXP4xx watchdog driver by Lennert Buytenhek.
  7. * The original version carries these notices:
  8. *
  9. * Author: Deepak Saxena <dsaxena@plexity.net>
  10. *
  11. * Copyright 2004 (c) MontaVista, Software, Inc.
  12. * Based on sa1100 driver, Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
  13. *
  14. * This file is licensed under the terms of the GNU General Public
  15. * License version 2. This program is licensed "as is" without any
  16. * warranty of any kind, whether express or implied.
  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/miscdevice.h>
  24. #include <linux/watchdog.h>
  25. #include <linux/init.h>
  26. #include <linux/bitops.h>
  27. #include <asm/hardware.h>
  28. #include <asm/uaccess.h>
  29. static int nowayout = WATCHDOG_NOWAYOUT;
  30. static unsigned int heartbeat = 60; /* (secs) Default is 1 minute */
  31. static unsigned long wdt_status;
  32. #define WDT_IN_USE 0
  33. #define WDT_OK_TO_CLOSE 1
  34. static unsigned long wdt_tick_rate;
  35. static void
  36. wdt_enable(void)
  37. {
  38. ixp2000_reg_write(IXP2000_RESET0, *(IXP2000_RESET0) | WDT_RESET_ENABLE);
  39. ixp2000_reg_write(IXP2000_TWDE, WDT_ENABLE);
  40. ixp2000_reg_write(IXP2000_T4_CLD, heartbeat * wdt_tick_rate);
  41. ixp2000_reg_write(IXP2000_T4_CTL, TIMER_DIVIDER_256 | TIMER_ENABLE);
  42. }
  43. static void
  44. wdt_disable(void)
  45. {
  46. ixp2000_reg_write(IXP2000_T4_CTL, 0);
  47. }
  48. static void
  49. wdt_keepalive(void)
  50. {
  51. ixp2000_reg_write(IXP2000_T4_CLD, heartbeat * wdt_tick_rate);
  52. }
  53. static int
  54. ixp2000_wdt_open(struct inode *inode, struct file *file)
  55. {
  56. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  57. return -EBUSY;
  58. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  59. wdt_enable();
  60. return nonseekable_open(inode, file);
  61. }
  62. static ssize_t
  63. ixp2000_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
  64. {
  65. if (len) {
  66. if (!nowayout) {
  67. size_t i;
  68. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  69. for (i = 0; i != len; i++) {
  70. char c;
  71. if (get_user(c, data + i))
  72. return -EFAULT;
  73. if (c == 'V')
  74. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  75. }
  76. }
  77. wdt_keepalive();
  78. }
  79. return len;
  80. }
  81. static struct watchdog_info ident = {
  82. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
  83. WDIOF_KEEPALIVEPING,
  84. .identity = "IXP2000 Watchdog",
  85. };
  86. static int
  87. ixp2000_wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  88. unsigned long arg)
  89. {
  90. int ret = -ENOTTY;
  91. int time;
  92. switch (cmd) {
  93. case WDIOC_GETSUPPORT:
  94. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  95. sizeof(ident)) ? -EFAULT : 0;
  96. break;
  97. case WDIOC_GETSTATUS:
  98. ret = put_user(0, (int *)arg);
  99. break;
  100. case WDIOC_GETBOOTSTATUS:
  101. ret = put_user(0, (int *)arg);
  102. break;
  103. case WDIOC_SETTIMEOUT:
  104. ret = get_user(time, (int *)arg);
  105. if (ret)
  106. break;
  107. if (time <= 0 || time > 60) {
  108. ret = -EINVAL;
  109. break;
  110. }
  111. heartbeat = time;
  112. wdt_keepalive();
  113. /* Fall through */
  114. case WDIOC_GETTIMEOUT:
  115. ret = put_user(heartbeat, (int *)arg);
  116. break;
  117. case WDIOC_KEEPALIVE:
  118. wdt_enable();
  119. ret = 0;
  120. break;
  121. }
  122. return ret;
  123. }
  124. static int
  125. ixp2000_wdt_release(struct inode *inode, struct file *file)
  126. {
  127. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status)) {
  128. wdt_disable();
  129. } else {
  130. printk(KERN_CRIT "WATCHDOG: Device closed unexpectedly - "
  131. "timer will not stop\n");
  132. }
  133. clear_bit(WDT_IN_USE, &wdt_status);
  134. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  135. return 0;
  136. }
  137. static const struct file_operations ixp2000_wdt_fops =
  138. {
  139. .owner = THIS_MODULE,
  140. .llseek = no_llseek,
  141. .write = ixp2000_wdt_write,
  142. .ioctl = ixp2000_wdt_ioctl,
  143. .open = ixp2000_wdt_open,
  144. .release = ixp2000_wdt_release,
  145. };
  146. static struct miscdevice ixp2000_wdt_miscdev =
  147. {
  148. .minor = WATCHDOG_MINOR,
  149. .name = "watchdog",
  150. .fops = &ixp2000_wdt_fops,
  151. };
  152. static int __init ixp2000_wdt_init(void)
  153. {
  154. if ((*IXP2000_PRODUCT_ID & 0x001ffef0) == 0x00000000) {
  155. printk(KERN_INFO "Unable to use IXP2000 watchdog due to IXP2800 erratum #25.\n");
  156. return -EIO;
  157. }
  158. wdt_tick_rate = (*IXP2000_T1_CLD * HZ) / 256;
  159. return misc_register(&ixp2000_wdt_miscdev);
  160. }
  161. static void __exit ixp2000_wdt_exit(void)
  162. {
  163. misc_deregister(&ixp2000_wdt_miscdev);
  164. }
  165. module_init(ixp2000_wdt_init);
  166. module_exit(ixp2000_wdt_exit);
  167. MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net">);
  168. MODULE_DESCRIPTION("IXP2000 Network Processor Watchdog");
  169. module_param(heartbeat, int, 0);
  170. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 60s)");
  171. module_param(nowayout, int, 0);
  172. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  173. MODULE_LICENSE("GPL");
  174. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);