ixp2000_wdt.c 4.8 KB

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