iop_wdt.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/char/watchdog/iop_wdt.c
  4. *
  5. * WDT driver for Intel I/O Processors
  6. * Copyright (C) 2005, Intel Corporation.
  7. *
  8. * Based on ixp4xx driver, Copyright 2004 (c) MontaVista, Software, Inc.
  9. *
  10. * Curt E Bruns <curt.e.bruns@intel.com>
  11. * Peter Milne <peter.milne@d-tacq.com>
  12. * Dan Williams <dan.j.williams@intel.com>
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/fs.h>
  18. #include <linux/init.h>
  19. #include <linux/device.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/watchdog.h>
  22. #include <linux/uaccess.h>
  23. #include <mach/hardware.h>
  24. static bool nowayout = WATCHDOG_NOWAYOUT;
  25. static unsigned long wdt_status;
  26. static unsigned long boot_status;
  27. static DEFINE_SPINLOCK(wdt_lock);
  28. #define WDT_IN_USE 0
  29. #define WDT_OK_TO_CLOSE 1
  30. #define WDT_ENABLED 2
  31. static unsigned long iop_watchdog_timeout(void)
  32. {
  33. return (0xffffffffUL / get_iop_tick_rate());
  34. }
  35. /**
  36. * wdt_supports_disable - determine if we are accessing a iop13xx watchdog
  37. * or iop3xx by whether it has a disable command
  38. */
  39. static int wdt_supports_disable(void)
  40. {
  41. int can_disable;
  42. if (IOP_WDTCR_EN_ARM != IOP_WDTCR_DIS_ARM)
  43. can_disable = 1;
  44. else
  45. can_disable = 0;
  46. return can_disable;
  47. }
  48. static void wdt_enable(void)
  49. {
  50. /* Arm and enable the Timer to starting counting down from 0xFFFF.FFFF
  51. * Takes approx. 10.7s to timeout
  52. */
  53. spin_lock(&wdt_lock);
  54. write_wdtcr(IOP_WDTCR_EN_ARM);
  55. write_wdtcr(IOP_WDTCR_EN);
  56. spin_unlock(&wdt_lock);
  57. }
  58. /* returns 0 if the timer was successfully disabled */
  59. static int wdt_disable(void)
  60. {
  61. /* Stop Counting */
  62. if (wdt_supports_disable()) {
  63. spin_lock(&wdt_lock);
  64. write_wdtcr(IOP_WDTCR_DIS_ARM);
  65. write_wdtcr(IOP_WDTCR_DIS);
  66. clear_bit(WDT_ENABLED, &wdt_status);
  67. spin_unlock(&wdt_lock);
  68. pr_info("Disabled\n");
  69. return 0;
  70. } else
  71. return 1;
  72. }
  73. static int iop_wdt_open(struct inode *inode, struct file *file)
  74. {
  75. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  76. return -EBUSY;
  77. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  78. wdt_enable();
  79. set_bit(WDT_ENABLED, &wdt_status);
  80. return stream_open(inode, file);
  81. }
  82. static ssize_t iop_wdt_write(struct file *file, const char *data, size_t len,
  83. loff_t *ppos)
  84. {
  85. if (len) {
  86. if (!nowayout) {
  87. size_t i;
  88. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  89. for (i = 0; i != len; i++) {
  90. char c;
  91. if (get_user(c, data + i))
  92. return -EFAULT;
  93. if (c == 'V')
  94. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  95. }
  96. }
  97. wdt_enable();
  98. }
  99. return len;
  100. }
  101. static const struct watchdog_info ident = {
  102. .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  103. .identity = "iop watchdog",
  104. };
  105. static long iop_wdt_ioctl(struct file *file,
  106. unsigned int cmd, unsigned long arg)
  107. {
  108. int options;
  109. int ret = -ENOTTY;
  110. int __user *argp = (int __user *)arg;
  111. switch (cmd) {
  112. case WDIOC_GETSUPPORT:
  113. if (copy_to_user(argp, &ident, sizeof(ident)))
  114. ret = -EFAULT;
  115. else
  116. ret = 0;
  117. break;
  118. case WDIOC_GETSTATUS:
  119. ret = put_user(0, argp);
  120. break;
  121. case WDIOC_GETBOOTSTATUS:
  122. ret = put_user(boot_status, argp);
  123. break;
  124. case WDIOC_SETOPTIONS:
  125. if (get_user(options, (int *)arg))
  126. return -EFAULT;
  127. if (options & WDIOS_DISABLECARD) {
  128. if (!nowayout) {
  129. if (wdt_disable() == 0) {
  130. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  131. ret = 0;
  132. } else
  133. ret = -ENXIO;
  134. } else
  135. ret = 0;
  136. }
  137. if (options & WDIOS_ENABLECARD) {
  138. wdt_enable();
  139. ret = 0;
  140. }
  141. break;
  142. case WDIOC_KEEPALIVE:
  143. wdt_enable();
  144. ret = 0;
  145. break;
  146. case WDIOC_GETTIMEOUT:
  147. ret = put_user(iop_watchdog_timeout(), argp);
  148. break;
  149. }
  150. return ret;
  151. }
  152. static int iop_wdt_release(struct inode *inode, struct file *file)
  153. {
  154. int state = 1;
  155. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  156. if (test_bit(WDT_ENABLED, &wdt_status))
  157. state = wdt_disable();
  158. /* if the timer is not disabled reload and notify that we are still
  159. * going down
  160. */
  161. if (state != 0) {
  162. wdt_enable();
  163. pr_crit("Device closed unexpectedly - reset in %lu seconds\n",
  164. iop_watchdog_timeout());
  165. }
  166. clear_bit(WDT_IN_USE, &wdt_status);
  167. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  168. return 0;
  169. }
  170. static const struct file_operations iop_wdt_fops = {
  171. .owner = THIS_MODULE,
  172. .llseek = no_llseek,
  173. .write = iop_wdt_write,
  174. .unlocked_ioctl = iop_wdt_ioctl,
  175. .compat_ioctl = compat_ptr_ioctl,
  176. .open = iop_wdt_open,
  177. .release = iop_wdt_release,
  178. };
  179. static struct miscdevice iop_wdt_miscdev = {
  180. .minor = WATCHDOG_MINOR,
  181. .name = "watchdog",
  182. .fops = &iop_wdt_fops,
  183. };
  184. static int __init iop_wdt_init(void)
  185. {
  186. int ret;
  187. /* check if the reset was caused by the watchdog timer */
  188. boot_status = (read_rcsr() & IOP_RCSR_WDT) ? WDIOF_CARDRESET : 0;
  189. /* Configure Watchdog Timeout to cause an Internal Bus (IB) Reset
  190. * NOTE: An IB Reset will Reset both cores in the IOP342
  191. */
  192. write_wdtsr(IOP13XX_WDTCR_IB_RESET);
  193. /* Register after we have the device set up so we cannot race
  194. with an open */
  195. ret = misc_register(&iop_wdt_miscdev);
  196. if (ret == 0)
  197. pr_info("timeout %lu sec\n", iop_watchdog_timeout());
  198. return ret;
  199. }
  200. static void __exit iop_wdt_exit(void)
  201. {
  202. misc_deregister(&iop_wdt_miscdev);
  203. }
  204. module_init(iop_wdt_init);
  205. module_exit(iop_wdt_exit);
  206. module_param(nowayout, bool, 0);
  207. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  208. MODULE_AUTHOR("Curt E Bruns <curt.e.bruns@intel.com>");
  209. MODULE_DESCRIPTION("iop watchdog timer driver");
  210. MODULE_LICENSE("GPL");