ixp4xx_wdt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/fs.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/watchdog.h>
  23. #include <linux/init.h>
  24. #include <linux/bitops.h>
  25. #include <linux/uaccess.h>
  26. #include <mach/hardware.h>
  27. static bool nowayout = WATCHDOG_NOWAYOUT;
  28. static int heartbeat = 60; /* (secs) Default is 1 minute */
  29. static unsigned long wdt_status;
  30. static unsigned long boot_status;
  31. static DEFINE_SPINLOCK(wdt_lock);
  32. #define WDT_TICK_RATE (IXP4XX_PERIPHERAL_BUS_CLOCK * 1000000UL)
  33. #define WDT_IN_USE 0
  34. #define WDT_OK_TO_CLOSE 1
  35. static void wdt_enable(void)
  36. {
  37. spin_lock(&wdt_lock);
  38. *IXP4XX_OSWK = IXP4XX_WDT_KEY;
  39. *IXP4XX_OSWE = 0;
  40. *IXP4XX_OSWT = WDT_TICK_RATE * heartbeat;
  41. *IXP4XX_OSWE = IXP4XX_WDT_COUNT_ENABLE | IXP4XX_WDT_RESET_ENABLE;
  42. *IXP4XX_OSWK = 0;
  43. spin_unlock(&wdt_lock);
  44. }
  45. static void wdt_disable(void)
  46. {
  47. spin_lock(&wdt_lock);
  48. *IXP4XX_OSWK = IXP4XX_WDT_KEY;
  49. *IXP4XX_OSWE = 0;
  50. *IXP4XX_OSWK = 0;
  51. spin_unlock(&wdt_lock);
  52. }
  53. static int ixp4xx_wdt_open(struct inode *inode, struct file *file)
  54. {
  55. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  56. return -EBUSY;
  57. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  58. wdt_enable();
  59. return nonseekable_open(inode, file);
  60. }
  61. static ssize_t
  62. ixp4xx_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
  63. {
  64. if (len) {
  65. if (!nowayout) {
  66. size_t i;
  67. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  68. for (i = 0; i != len; i++) {
  69. char c;
  70. if (get_user(c, data + i))
  71. return -EFAULT;
  72. if (c == 'V')
  73. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  74. }
  75. }
  76. wdt_enable();
  77. }
  78. return len;
  79. }
  80. static const struct watchdog_info ident = {
  81. .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE |
  82. WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  83. .identity = "IXP4xx Watchdog",
  84. };
  85. static long ixp4xx_wdt_ioctl(struct file *file, unsigned int cmd,
  86. unsigned long arg)
  87. {
  88. int ret = -ENOTTY;
  89. int time;
  90. switch (cmd) {
  91. case WDIOC_GETSUPPORT:
  92. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  93. sizeof(ident)) ? -EFAULT : 0;
  94. break;
  95. case WDIOC_GETSTATUS:
  96. ret = put_user(0, (int *)arg);
  97. break;
  98. case WDIOC_GETBOOTSTATUS:
  99. ret = put_user(boot_status, (int *)arg);
  100. break;
  101. case WDIOC_KEEPALIVE:
  102. wdt_enable();
  103. ret = 0;
  104. break;
  105. case WDIOC_SETTIMEOUT:
  106. ret = get_user(time, (int *)arg);
  107. if (ret)
  108. break;
  109. if (time <= 0 || time > 60) {
  110. ret = -EINVAL;
  111. break;
  112. }
  113. heartbeat = time;
  114. wdt_enable();
  115. /* Fall through */
  116. case WDIOC_GETTIMEOUT:
  117. ret = put_user(heartbeat, (int *)arg);
  118. break;
  119. }
  120. return ret;
  121. }
  122. static int ixp4xx_wdt_release(struct inode *inode, struct file *file)
  123. {
  124. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  125. wdt_disable();
  126. else
  127. pr_crit("Device closed unexpectedly - 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. pr_err("Rev. A0 IXP42x CPU detected - watchdog disabled\n");
  150. return -ENODEV;
  151. }
  152. boot_status = (*IXP4XX_OSST & IXP4XX_OSST_TIMER_WARM_RESET) ?
  153. WDIOF_CARDRESET : 0;
  154. ret = misc_register(&ixp4xx_wdt_miscdev);
  155. if (ret == 0)
  156. pr_info("timer heartbeat %d sec\n", heartbeat);
  157. return ret;
  158. }
  159. static void __exit ixp4xx_wdt_exit(void)
  160. {
  161. misc_deregister(&ixp4xx_wdt_miscdev);
  162. }
  163. module_init(ixp4xx_wdt_init);
  164. module_exit(ixp4xx_wdt_exit);
  165. MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
  166. MODULE_DESCRIPTION("IXP4xx Network Processor Watchdog");
  167. module_param(heartbeat, int, 0);
  168. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 60s)");
  169. module_param(nowayout, bool, 0);
  170. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  171. MODULE_LICENSE("GPL");