ixp2000_wdt.c 4.6 KB

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