ixp2000_wdt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/timer.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 <linux/uaccess.h>
  29. #include <mach/hardware.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. static spinlock_t wdt_lock;
  34. #define WDT_IN_USE 0
  35. #define WDT_OK_TO_CLOSE 1
  36. static unsigned long wdt_tick_rate;
  37. static void wdt_enable(void)
  38. {
  39. spin_lock(&wdt_lock);
  40. ixp2000_reg_write(IXP2000_RESET0, *(IXP2000_RESET0) | WDT_RESET_ENABLE);
  41. ixp2000_reg_write(IXP2000_TWDE, WDT_ENABLE);
  42. ixp2000_reg_write(IXP2000_T4_CLD, heartbeat * wdt_tick_rate);
  43. ixp2000_reg_write(IXP2000_T4_CTL, TIMER_DIVIDER_256 | TIMER_ENABLE);
  44. spin_unlock(&wdt_lock);
  45. }
  46. static void wdt_disable(void)
  47. {
  48. spin_lock(&wdt_lock);
  49. ixp2000_reg_write(IXP2000_T4_CTL, 0);
  50. spin_unlock(&wdt_lock);
  51. }
  52. static void wdt_keepalive(void)
  53. {
  54. spin_lock(&wdt_lock);
  55. ixp2000_reg_write(IXP2000_T4_CLD, heartbeat * wdt_tick_rate);
  56. spin_unlock(&wdt_lock);
  57. }
  58. static int ixp2000_wdt_open(struct inode *inode, struct file *file)
  59. {
  60. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  61. return -EBUSY;
  62. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  63. wdt_enable();
  64. return nonseekable_open(inode, file);
  65. }
  66. static ssize_t ixp2000_wdt_write(struct file *file, const char *data,
  67. size_t len, loff_t *ppos)
  68. {
  69. if (len) {
  70. if (!nowayout) {
  71. size_t i;
  72. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  73. for (i = 0; i != len; i++) {
  74. char c;
  75. if (get_user(c, data + i))
  76. return -EFAULT;
  77. if (c == 'V')
  78. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  79. }
  80. }
  81. wdt_keepalive();
  82. }
  83. return len;
  84. }
  85. static const struct watchdog_info ident = {
  86. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
  87. WDIOF_KEEPALIVEPING,
  88. .identity = "IXP2000 Watchdog",
  89. };
  90. static long ixp2000_wdt_ioctl(struct file *file, unsigned int cmd,
  91. unsigned long arg)
  92. {
  93. int ret = -ENOTTY;
  94. int time;
  95. switch (cmd) {
  96. case WDIOC_GETSUPPORT:
  97. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  98. sizeof(ident)) ? -EFAULT : 0;
  99. break;
  100. case WDIOC_GETSTATUS:
  101. ret = put_user(0, (int *)arg);
  102. break;
  103. case WDIOC_GETBOOTSTATUS:
  104. ret = put_user(0, (int *)arg);
  105. break;
  106. case WDIOC_KEEPALIVE:
  107. wdt_enable();
  108. ret = 0;
  109. break;
  110. case WDIOC_SETTIMEOUT:
  111. ret = get_user(time, (int *)arg);
  112. if (ret)
  113. break;
  114. if (time <= 0 || time > 60) {
  115. ret = -EINVAL;
  116. break;
  117. }
  118. heartbeat = time;
  119. wdt_keepalive();
  120. /* Fall through */
  121. case WDIOC_GETTIMEOUT:
  122. ret = put_user(heartbeat, (int *)arg);
  123. break;
  124. }
  125. return ret;
  126. }
  127. static int ixp2000_wdt_release(struct inode *inode, struct file *file)
  128. {
  129. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  130. wdt_disable();
  131. else
  132. printk(KERN_CRIT "WATCHDOG: Device closed unexpectedly - "
  133. "timer will not stop\n");
  134. clear_bit(WDT_IN_USE, &wdt_status);
  135. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  136. return 0;
  137. }
  138. static const struct file_operations ixp2000_wdt_fops = {
  139. .owner = THIS_MODULE,
  140. .llseek = no_llseek,
  141. .write = ixp2000_wdt_write,
  142. .unlocked_ioctl = ixp2000_wdt_ioctl,
  143. .open = ixp2000_wdt_open,
  144. .release = ixp2000_wdt_release,
  145. };
  146. static struct miscdevice ixp2000_wdt_miscdev = {
  147. .minor = WATCHDOG_MINOR,
  148. .name = "watchdog",
  149. .fops = &ixp2000_wdt_fops,
  150. };
  151. static int __init ixp2000_wdt_init(void)
  152. {
  153. if ((*IXP2000_PRODUCT_ID & 0x001ffef0) == 0x00000000) {
  154. printk(KERN_INFO "Unable to use IXP2000 watchdog due to IXP2800 erratum #25.\n");
  155. return -EIO;
  156. }
  157. wdt_tick_rate = (*IXP2000_T1_CLD * HZ) / 256;
  158. spin_lock_init(&wdt_lock);
  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);