8250_rsa.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <linux/errno.h>
  3. #include <linux/ioport.h>
  4. #include <linux/module.h>
  5. #include <linux/moduleparam.h>
  6. #include <linux/serial.h>
  7. #include <linux/serial_8250.h>
  8. #include "8250.h"
  9. #define PORT_RSA_MAX 4
  10. static unsigned long probe_rsa[PORT_RSA_MAX];
  11. static unsigned int probe_rsa_count;
  12. static const struct uart_ops *core_port_base_ops;
  13. static int rsa8250_request_resource(struct uart_8250_port *up)
  14. {
  15. struct uart_port *port = &up->port;
  16. unsigned long start = UART_RSA_BASE << port->regshift;
  17. unsigned int size = 8 << port->regshift;
  18. switch (port->iotype) {
  19. case UPIO_HUB6:
  20. case UPIO_PORT:
  21. start += port->iobase;
  22. if (!request_region(start, size, "serial-rsa"))
  23. return -EBUSY;
  24. return 0;
  25. default:
  26. return -EINVAL;
  27. }
  28. }
  29. static void rsa8250_release_resource(struct uart_8250_port *up)
  30. {
  31. struct uart_port *port = &up->port;
  32. unsigned long offset = UART_RSA_BASE << port->regshift;
  33. unsigned int size = 8 << port->regshift;
  34. switch (port->iotype) {
  35. case UPIO_HUB6:
  36. case UPIO_PORT:
  37. release_region(port->iobase + offset, size);
  38. break;
  39. default:
  40. break;
  41. }
  42. }
  43. static void univ8250_config_port(struct uart_port *port, int flags)
  44. {
  45. struct uart_8250_port *up = up_to_u8250p(port);
  46. unsigned int i;
  47. up->probe &= ~UART_PROBE_RSA;
  48. if (port->type == PORT_RSA) {
  49. if (rsa8250_request_resource(up) == 0)
  50. up->probe |= UART_PROBE_RSA;
  51. } else if (flags & UART_CONFIG_TYPE) {
  52. for (i = 0; i < probe_rsa_count; i++) {
  53. if (probe_rsa[i] == up->port.iobase) {
  54. if (rsa8250_request_resource(up) == 0)
  55. up->probe |= UART_PROBE_RSA;
  56. break;
  57. }
  58. }
  59. }
  60. core_port_base_ops->config_port(port, flags);
  61. if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
  62. rsa8250_release_resource(up);
  63. }
  64. static int univ8250_request_port(struct uart_port *port)
  65. {
  66. struct uart_8250_port *up = up_to_u8250p(port);
  67. int ret;
  68. ret = core_port_base_ops->request_port(port);
  69. if (ret == 0 && port->type == PORT_RSA) {
  70. ret = rsa8250_request_resource(up);
  71. if (ret < 0)
  72. core_port_base_ops->release_port(port);
  73. }
  74. return ret;
  75. }
  76. static void univ8250_release_port(struct uart_port *port)
  77. {
  78. struct uart_8250_port *up = up_to_u8250p(port);
  79. if (port->type == PORT_RSA)
  80. rsa8250_release_resource(up);
  81. core_port_base_ops->release_port(port);
  82. }
  83. /*
  84. * It is not allowed to directly reference any symbols from 8250.ko here as
  85. * that would result in a dependency loop between the 8250.ko and
  86. * 8250_base.ko modules. This function is called from 8250.ko and is used to
  87. * break the symbolic dependency cycle. Anything that is needed from 8250.ko
  88. * has to be passed as pointers to this function which then can adjust those
  89. * variables on 8250.ko side or store them locally as needed.
  90. */
  91. void univ8250_rsa_support(struct uart_ops *ops, const struct uart_ops *core_ops)
  92. {
  93. core_port_base_ops = core_ops;
  94. ops->config_port = univ8250_config_port;
  95. ops->request_port = univ8250_request_port;
  96. ops->release_port = univ8250_release_port;
  97. }
  98. EXPORT_SYMBOL_FOR_MODULES(univ8250_rsa_support, "8250");
  99. module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444);
  100. MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
  101. /*
  102. * Attempts to turn on the RSA FIFO. Returns zero on failure.
  103. * We set the port uart clock rate if we succeed.
  104. */
  105. static int __rsa_enable(struct uart_8250_port *up)
  106. {
  107. unsigned char mode;
  108. int result;
  109. mode = serial_in(up, UART_RSA_MSR);
  110. result = mode & UART_RSA_MSR_FIFO;
  111. if (!result) {
  112. serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
  113. mode = serial_in(up, UART_RSA_MSR);
  114. result = mode & UART_RSA_MSR_FIFO;
  115. }
  116. if (result)
  117. up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
  118. return result;
  119. }
  120. /*
  121. * If this is an RSA port, see if we can kick it up to the higher speed clock.
  122. */
  123. void rsa_enable(struct uart_8250_port *up)
  124. {
  125. if (up->port.type != PORT_RSA)
  126. return;
  127. if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
  128. guard(uart_port_lock_irq)(&up->port);
  129. __rsa_enable(up);
  130. }
  131. if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
  132. serial_out(up, UART_RSA_FRR, 0);
  133. }
  134. /*
  135. * Attempts to turn off the RSA FIFO and resets the RSA board back to 115kbps compat mode. It is
  136. * unknown why interrupts were disabled in here. However, the caller is expected to preserve this
  137. * behaviour by grabbing the spinlock before calling this function.
  138. */
  139. void rsa_disable(struct uart_8250_port *up)
  140. {
  141. unsigned char mode;
  142. int result;
  143. if (up->port.type != PORT_RSA)
  144. return;
  145. if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16)
  146. return;
  147. guard(uart_port_lock_irq)(&up->port);
  148. mode = serial_in(up, UART_RSA_MSR);
  149. result = !(mode & UART_RSA_MSR_FIFO);
  150. if (!result) {
  151. serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
  152. mode = serial_in(up, UART_RSA_MSR);
  153. result = !(mode & UART_RSA_MSR_FIFO);
  154. }
  155. if (result)
  156. up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
  157. }
  158. void rsa_autoconfig(struct uart_8250_port *up)
  159. {
  160. /* Only probe for RSA ports if we got the region. */
  161. if (up->port.type != PORT_16550A)
  162. return;
  163. if (!(up->probe & UART_PROBE_RSA))
  164. return;
  165. if (__rsa_enable(up))
  166. up->port.type = PORT_RSA;
  167. }
  168. void rsa_reset(struct uart_8250_port *up)
  169. {
  170. if (up->port.type != PORT_RSA)
  171. return;
  172. serial_out(up, UART_RSA_FRR, 0);
  173. }