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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/types.h>
  22. #include <linux/timer.h>
  23. #include <linux/kernel.h>
  24. #include <linux/fs.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/watchdog.h>
  27. #include <linux/init.h>
  28. #include <linux/bitops.h>
  29. #include <linux/uaccess.h>
  30. #include <mach/hardware.h>
  31. static bool nowayout = WATCHDOG_NOWAYOUT;
  32. static unsigned int heartbeat = 60; /* (secs) Default is 1 minute */
  33. static unsigned long wdt_status;
  34. static DEFINE_SPINLOCK(wdt_lock);
  35. #define WDT_IN_USE 0
  36. #define WDT_OK_TO_CLOSE 1
  37. static unsigned long wdt_tick_rate;
  38. static void wdt_enable(void)
  39. {
  40. spin_lock(&wdt_lock);
  41. ixp2000_reg_write(IXP2000_RESET0, *(IXP2000_RESET0) | WDT_RESET_ENABLE);
  42. ixp2000_reg_write(IXP2000_TWDE, WDT_ENABLE);
  43. ixp2000_reg_write(IXP2000_T4_CLD, heartbeat * wdt_tick_rate);
  44. ixp2000_reg_write(IXP2000_T4_CTL, TIMER_DIVIDER_256 | TIMER_ENABLE);
  45. spin_unlock(&wdt_lock);
  46. }
  47. static void wdt_disable(void)
  48. {
  49. spin_lock(&wdt_lock);
  50. ixp2000_reg_write(IXP2000_T4_CTL, 0);
  51. spin_unlock(&wdt_lock);
  52. }
  53. static void wdt_keepalive(void)
  54. {
  55. spin_lock(&wdt_lock);
  56. ixp2000_reg_write(IXP2000_T4_CLD, heartbeat * wdt_tick_rate);
  57. spin_unlock(&wdt_lock);
  58. }
  59. static int ixp2000_wdt_open(struct inode *inode, struct file *file)
  60. {
  61. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  62. return -EBUSY;
  63. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  64. wdt_enable();
  65. return nonseekable_open(inode, file);
  66. }
  67. static ssize_t ixp2000_wdt_write(struct file *file, const char *data,
  68. size_t len, loff_t *ppos)
  69. {
  70. if (len) {
  71. if (!nowayout) {
  72. size_t i;
  73. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  74. for (i = 0; i != len; i++) {
  75. char c;
  76. if (get_user(c, data + i))
  77. return -EFAULT;
  78. if (c == 'V')
  79. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  80. }
  81. }
  82. wdt_keepalive();
  83. }
  84. return len;
  85. }
  86. static const struct watchdog_info ident = {
  87. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
  88. WDIOF_KEEPALIVEPING,
  89. .identity = "IXP2000 Watchdog",
  90. };
  91. static long ixp2000_wdt_ioctl(struct file *file, unsigned int cmd,
  92. unsigned long arg)
  93. {
  94. int ret = -ENOTTY;
  95. int time;
  96. switch (cmd) {
  97. case WDIOC_GETSUPPORT:
  98. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  99. sizeof(ident)) ? -EFAULT : 0;
  100. break;
  101. case WDIOC_GETSTATUS:
  102. ret = put_user(0, (int *)arg);
  103. break;
  104. case WDIOC_GETBOOTSTATUS:
  105. ret = put_user(0, (int *)arg);
  106. break;
  107. case WDIOC_KEEPALIVE:
  108. wdt_enable();
  109. ret = 0;
  110. break;
  111. case WDIOC_SETTIMEOUT:
  112. ret = get_user(time, (int *)arg);
  113. if (ret)
  114. break;
  115. if (time <= 0 || time > 60) {
  116. ret = -EINVAL;
  117. break;
  118. }
  119. heartbeat = time;
  120. wdt_keepalive();
  121. /* Fall through */
  122. case WDIOC_GETTIMEOUT:
  123. ret = put_user(heartbeat, (int *)arg);
  124. break;
  125. }
  126. return ret;
  127. }
  128. static int ixp2000_wdt_release(struct inode *inode, struct file *file)
  129. {
  130. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  131. wdt_disable();
  132. else
  133. pr_crit("Device closed unexpectedly - 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. pr_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. 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, bool, 0);
  171. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  172. MODULE_LICENSE("GPL");
  173. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);