ixp4xx_wdt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/of.h>
  23. #include <linux/watchdog.h>
  24. #include <linux/init.h>
  25. #include <linux/bitops.h>
  26. #include <linux/uaccess.h>
  27. #include <mach/hardware.h>
  28. static bool nowayout = WATCHDOG_NOWAYOUT;
  29. static int heartbeat = 60; /* (secs) Default is 1 minute */
  30. static unsigned long wdt_status;
  31. static unsigned long boot_status;
  32. static DEFINE_SPINLOCK(wdt_lock);
  33. #define WDT_TICK_RATE (IXP4XX_PERIPHERAL_BUS_CLOCK * 1000000UL)
  34. #define WDT_IN_USE 0
  35. #define WDT_OK_TO_CLOSE 1
  36. static void wdt_enable(void)
  37. {
  38. spin_lock(&wdt_lock);
  39. *IXP4XX_OSWK = IXP4XX_WDT_KEY;
  40. *IXP4XX_OSWE = 0;
  41. *IXP4XX_OSWT = WDT_TICK_RATE * heartbeat;
  42. *IXP4XX_OSWE = IXP4XX_WDT_COUNT_ENABLE | IXP4XX_WDT_RESET_ENABLE;
  43. *IXP4XX_OSWK = 0;
  44. spin_unlock(&wdt_lock);
  45. }
  46. static void wdt_disable(void)
  47. {
  48. spin_lock(&wdt_lock);
  49. *IXP4XX_OSWK = IXP4XX_WDT_KEY;
  50. *IXP4XX_OSWE = 0;
  51. *IXP4XX_OSWK = 0;
  52. spin_unlock(&wdt_lock);
  53. }
  54. static int ixp4xx_wdt_open(struct inode *inode, struct file *file)
  55. {
  56. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  57. return -EBUSY;
  58. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  59. wdt_enable();
  60. return stream_open(inode, file);
  61. }
  62. static ssize_t
  63. ixp4xx_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
  64. {
  65. if (len) {
  66. if (!nowayout) {
  67. size_t i;
  68. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  69. for (i = 0; i != len; i++) {
  70. char c;
  71. if (get_user(c, data + i))
  72. return -EFAULT;
  73. if (c == 'V')
  74. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  75. }
  76. }
  77. wdt_enable();
  78. }
  79. return len;
  80. }
  81. static const struct watchdog_info ident = {
  82. .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE |
  83. WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  84. .identity = "IXP4xx Watchdog",
  85. };
  86. static long ixp4xx_wdt_ioctl(struct file *file, unsigned int cmd,
  87. unsigned long arg)
  88. {
  89. int ret = -ENOTTY;
  90. int time;
  91. switch (cmd) {
  92. case WDIOC_GETSUPPORT:
  93. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  94. sizeof(ident)) ? -EFAULT : 0;
  95. break;
  96. case WDIOC_GETSTATUS:
  97. ret = put_user(0, (int *)arg);
  98. break;
  99. case WDIOC_GETBOOTSTATUS:
  100. ret = put_user(boot_status, (int *)arg);
  101. break;
  102. case WDIOC_KEEPALIVE:
  103. wdt_enable();
  104. ret = 0;
  105. break;
  106. case WDIOC_SETTIMEOUT:
  107. ret = get_user(time, (int *)arg);
  108. if (ret)
  109. break;
  110. if (time <= 0 || time > 60) {
  111. ret = -EINVAL;
  112. break;
  113. }
  114. heartbeat = time;
  115. wdt_enable();
  116. fallthrough;
  117. case WDIOC_GETTIMEOUT:
  118. ret = put_user(heartbeat, (int *)arg);
  119. break;
  120. }
  121. return ret;
  122. }
  123. static int ixp4xx_wdt_release(struct inode *inode, struct file *file)
  124. {
  125. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  126. wdt_disable();
  127. else
  128. pr_crit("Device closed unexpectedly - timer will not stop\n");
  129. clear_bit(WDT_IN_USE, &wdt_status);
  130. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  131. return 0;
  132. }
  133. static const struct file_operations ixp4xx_wdt_fops = {
  134. .owner = THIS_MODULE,
  135. .llseek = no_llseek,
  136. .write = ixp4xx_wdt_write,
  137. .unlocked_ioctl = ixp4xx_wdt_ioctl,
  138. .compat_ioctl = compat_ptr_ioctl,
  139. .open = ixp4xx_wdt_open,
  140. .release = ixp4xx_wdt_release,
  141. };
  142. static struct miscdevice ixp4xx_wdt_miscdev = {
  143. .minor = WATCHDOG_MINOR,
  144. .name = "watchdog",
  145. .fops = &ixp4xx_wdt_fops,
  146. };
  147. static int __init ixp4xx_wdt_init(void)
  148. {
  149. int ret;
  150. /*
  151. * FIXME: we bail out on device tree boot but this really needs
  152. * to be fixed in a nicer way: this registers the MDIO bus before
  153. * even matching the driver infrastructure, we should only probe
  154. * detected hardware.
  155. */
  156. if (of_have_populated_dt())
  157. return -ENODEV;
  158. if (!(read_cpuid_id() & 0xf) && !cpu_is_ixp46x()) {
  159. pr_err("Rev. A0 IXP42x CPU detected - watchdog disabled\n");
  160. return -ENODEV;
  161. }
  162. boot_status = (*IXP4XX_OSST & IXP4XX_OSST_TIMER_WARM_RESET) ?
  163. WDIOF_CARDRESET : 0;
  164. ret = misc_register(&ixp4xx_wdt_miscdev);
  165. if (ret == 0)
  166. pr_info("timer heartbeat %d sec\n", heartbeat);
  167. return ret;
  168. }
  169. static void __exit ixp4xx_wdt_exit(void)
  170. {
  171. misc_deregister(&ixp4xx_wdt_miscdev);
  172. }
  173. module_init(ixp4xx_wdt_init);
  174. module_exit(ixp4xx_wdt_exit);
  175. MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
  176. MODULE_DESCRIPTION("IXP4xx Network Processor Watchdog");
  177. module_param(heartbeat, int, 0);
  178. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 60s)");
  179. module_param(nowayout, bool, 0);
  180. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  181. MODULE_LICENSE("GPL");