ixp4xx_wdt.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * drivers/char/watchdog/ixp4xx_wdt.c
  3. *
  4. * Watchdog driver for Intel IXP4xx network processors
  5. *
  6. * Author: Deepak Saxena <dsaxena@plexity.net>
  7. *
  8. * Copyright 2004 (c) MontaVista, Software, Inc.
  9. * Based on sa1100 driver, Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/fs.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/watchdog.h>
  22. #include <linux/init.h>
  23. #include <linux/bitops.h>
  24. #include <linux/uaccess.h>
  25. #include <mach/hardware.h>
  26. static int nowayout = WATCHDOG_NOWAYOUT;
  27. static int heartbeat = 60; /* (secs) Default is 1 minute */
  28. static unsigned long wdt_status;
  29. static unsigned long boot_status;
  30. static DEFINE_SPINLOCK(wdt_lock);
  31. #define WDT_TICK_RATE (IXP4XX_PERIPHERAL_BUS_CLOCK * 1000000UL)
  32. #define WDT_IN_USE 0
  33. #define WDT_OK_TO_CLOSE 1
  34. static void wdt_enable(void)
  35. {
  36. spin_lock(&wdt_lock);
  37. *IXP4XX_OSWK = IXP4XX_WDT_KEY;
  38. *IXP4XX_OSWE = 0;
  39. *IXP4XX_OSWT = WDT_TICK_RATE * heartbeat;
  40. *IXP4XX_OSWE = IXP4XX_WDT_COUNT_ENABLE | IXP4XX_WDT_RESET_ENABLE;
  41. *IXP4XX_OSWK = 0;
  42. spin_unlock(&wdt_lock);
  43. }
  44. static void wdt_disable(void)
  45. {
  46. spin_lock(&wdt_lock);
  47. *IXP4XX_OSWK = IXP4XX_WDT_KEY;
  48. *IXP4XX_OSWE = 0;
  49. *IXP4XX_OSWK = 0;
  50. spin_unlock(&wdt_lock);
  51. }
  52. static int ixp4xx_wdt_open(struct inode *inode, struct file *file)
  53. {
  54. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  55. return -EBUSY;
  56. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  57. wdt_enable();
  58. return nonseekable_open(inode, file);
  59. }
  60. static ssize_t
  61. ixp4xx_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
  62. {
  63. if (len) {
  64. if (!nowayout) {
  65. size_t i;
  66. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  67. for (i = 0; i != len; i++) {
  68. char c;
  69. if (get_user(c, data + i))
  70. return -EFAULT;
  71. if (c == 'V')
  72. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  73. }
  74. }
  75. wdt_enable();
  76. }
  77. return len;
  78. }
  79. static const struct watchdog_info ident = {
  80. .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE |
  81. WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  82. .identity = "IXP4xx Watchdog",
  83. };
  84. static long ixp4xx_wdt_ioctl(struct file *file, unsigned int cmd,
  85. unsigned long arg)
  86. {
  87. int ret = -ENOTTY;
  88. int time;
  89. switch (cmd) {
  90. case WDIOC_GETSUPPORT:
  91. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  92. sizeof(ident)) ? -EFAULT : 0;
  93. break;
  94. case WDIOC_GETSTATUS:
  95. ret = put_user(0, (int *)arg);
  96. break;
  97. case WDIOC_GETBOOTSTATUS:
  98. ret = put_user(boot_status, (int *)arg);
  99. break;
  100. case WDIOC_KEEPALIVE:
  101. wdt_enable();
  102. ret = 0;
  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_enable();
  114. /* Fall through */
  115. case WDIOC_GETTIMEOUT:
  116. ret = put_user(heartbeat, (int *)arg);
  117. break;
  118. }
  119. return ret;
  120. }
  121. static int ixp4xx_wdt_release(struct inode *inode, struct file *file)
  122. {
  123. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  124. wdt_disable();
  125. else
  126. printk(KERN_CRIT "WATCHDOG: Device closed unexpectedly - "
  127. "timer will not stop\n");
  128. clear_bit(WDT_IN_USE, &wdt_status);
  129. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  130. return 0;
  131. }
  132. static const struct file_operations ixp4xx_wdt_fops = {
  133. .owner = THIS_MODULE,
  134. .llseek = no_llseek,
  135. .write = ixp4xx_wdt_write,
  136. .unlocked_ioctl = ixp4xx_wdt_ioctl,
  137. .open = ixp4xx_wdt_open,
  138. .release = ixp4xx_wdt_release,
  139. };
  140. static struct miscdevice ixp4xx_wdt_miscdev = {
  141. .minor = WATCHDOG_MINOR,
  142. .name = "watchdog",
  143. .fops = &ixp4xx_wdt_fops,
  144. };
  145. static int __init ixp4xx_wdt_init(void)
  146. {
  147. int ret;
  148. if (!(read_cpuid_id() & 0xf) && !cpu_is_ixp46x()) {
  149. printk(KERN_ERR "IXP4XXX Watchdog: Rev. A0 IXP42x CPU detected"
  150. " - watchdog disabled\n");
  151. return -ENODEV;
  152. }
  153. spin_lock_init(&wdt_lock);
  154. boot_status = (*IXP4XX_OSST & IXP4XX_OSST_TIMER_WARM_RESET) ?
  155. WDIOF_CARDRESET : 0;
  156. ret = misc_register(&ixp4xx_wdt_miscdev);
  157. if (ret == 0)
  158. printk(KERN_INFO "IXP4xx Watchdog Timer: heartbeat %d sec\n",
  159. heartbeat);
  160. return ret;
  161. }
  162. static void __exit ixp4xx_wdt_exit(void)
  163. {
  164. misc_deregister(&ixp4xx_wdt_miscdev);
  165. }
  166. module_init(ixp4xx_wdt_init);
  167. module_exit(ixp4xx_wdt_exit);
  168. MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
  169. MODULE_DESCRIPTION("IXP4xx 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);