Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0+
2 :
3 : #include <linux/errno.h>
4 : #include <linux/ioport.h>
5 : #include <linux/module.h>
6 : #include <linux/moduleparam.h>
7 :
8 : #include <linux/serial.h>
9 : #include <linux/serial_8250.h>
10 :
11 : #include "8250.h"
12 :
13 : #define PORT_RSA_MAX 4
14 : static unsigned long probe_rsa[PORT_RSA_MAX];
15 : static unsigned int probe_rsa_count;
16 :
17 0 : static int rsa8250_request_resource(struct uart_8250_port *up)
18 : {
19 0 : struct uart_port *port = &up->port;
20 0 : unsigned long start = UART_RSA_BASE << port->regshift;
21 0 : unsigned int size = 8 << port->regshift;
22 :
23 0 : switch (port->iotype) {
24 : case UPIO_HUB6:
25 : case UPIO_PORT:
26 0 : start += port->iobase;
27 0 : if (!request_region(start, size, "serial-rsa"))
28 0 : return -EBUSY;
29 0 : return 0;
30 : default:
31 0 : return -EINVAL;
32 : }
33 0 : }
34 :
35 0 : static void rsa8250_release_resource(struct uart_8250_port *up)
36 : {
37 0 : struct uart_port *port = &up->port;
38 0 : unsigned long offset = UART_RSA_BASE << port->regshift;
39 0 : unsigned int size = 8 << port->regshift;
40 :
41 0 : switch (port->iotype) {
42 : case UPIO_HUB6:
43 : case UPIO_PORT:
44 0 : release_region(port->iobase + offset, size);
45 0 : break;
46 : default:
47 0 : break;
48 : }
49 0 : }
50 :
51 0 : static void univ8250_config_port(struct uart_port *port, int flags)
52 : {
53 0 : struct uart_8250_port *up = up_to_u8250p(port);
54 0 : unsigned int i;
55 :
56 0 : up->probe &= ~UART_PROBE_RSA;
57 0 : if (port->type == PORT_RSA) {
58 0 : if (rsa8250_request_resource(up) == 0)
59 0 : up->probe |= UART_PROBE_RSA;
60 0 : } else if (flags & UART_CONFIG_TYPE) {
61 0 : for (i = 0; i < probe_rsa_count; i++) {
62 0 : if (probe_rsa[i] == up->port.iobase) {
63 0 : if (rsa8250_request_resource(up) == 0)
64 0 : up->probe |= UART_PROBE_RSA;
65 0 : break;
66 : }
67 0 : }
68 0 : }
69 :
70 0 : univ8250_port_base_ops->config_port(port, flags);
71 :
72 0 : if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
73 0 : rsa8250_release_resource(up);
74 0 : }
75 :
76 0 : static int univ8250_request_port(struct uart_port *port)
77 : {
78 0 : struct uart_8250_port *up = up_to_u8250p(port);
79 0 : int ret;
80 :
81 0 : ret = univ8250_port_base_ops->request_port(port);
82 0 : if (ret == 0 && port->type == PORT_RSA) {
83 0 : ret = rsa8250_request_resource(up);
84 0 : if (ret < 0)
85 0 : univ8250_port_base_ops->release_port(port);
86 0 : }
87 :
88 0 : return ret;
89 0 : }
90 :
91 0 : static void univ8250_release_port(struct uart_port *port)
92 : {
93 0 : struct uart_8250_port *up = up_to_u8250p(port);
94 :
95 0 : if (port->type == PORT_RSA)
96 0 : rsa8250_release_resource(up);
97 0 : univ8250_port_base_ops->release_port(port);
98 0 : }
99 :
100 0 : void univ8250_rsa_support(struct uart_ops *ops)
101 : {
102 0 : ops->config_port = univ8250_config_port;
103 0 : ops->request_port = univ8250_request_port;
104 0 : ops->release_port = univ8250_release_port;
105 0 : }
106 :
107 : module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444);
108 : MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
109 :
110 : /*
111 : * Attempts to turn on the RSA FIFO. Returns zero on failure.
112 : * We set the port uart clock rate if we succeed.
113 : */
114 0 : static int __rsa_enable(struct uart_8250_port *up)
115 : {
116 0 : unsigned char mode;
117 0 : int result;
118 :
119 0 : mode = serial_in(up, UART_RSA_MSR);
120 0 : result = mode & UART_RSA_MSR_FIFO;
121 :
122 0 : if (!result) {
123 0 : serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
124 0 : mode = serial_in(up, UART_RSA_MSR);
125 0 : result = mode & UART_RSA_MSR_FIFO;
126 0 : }
127 :
128 0 : if (result)
129 0 : up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
130 :
131 0 : return result;
132 0 : }
133 :
134 : /*
135 : * If this is an RSA port, see if we can kick it up to the higher speed clock.
136 : */
137 0 : void rsa_enable(struct uart_8250_port *up)
138 : {
139 0 : if (up->port.type != PORT_RSA)
140 0 : return;
141 :
142 0 : if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
143 0 : uart_port_lock_irq(&up->port);
144 0 : __rsa_enable(up);
145 0 : uart_port_unlock_irq(&up->port);
146 0 : }
147 0 : if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
148 0 : serial_out(up, UART_RSA_FRR, 0);
149 0 : }
150 : EXPORT_SYMBOL_FOR_MODULES(rsa_enable, "8250_base");
151 :
152 : /*
153 : * Attempts to turn off the RSA FIFO and resets the RSA board back to 115kbps compat mode. It is
154 : * unknown why interrupts were disabled in here. However, the caller is expected to preserve this
155 : * behaviour by grabbing the spinlock before calling this function.
156 : */
157 0 : void rsa_disable(struct uart_8250_port *up)
158 : {
159 0 : unsigned char mode;
160 0 : int result;
161 :
162 0 : if (up->port.type != PORT_RSA)
163 0 : return;
164 :
165 0 : if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16)
166 0 : return;
167 :
168 0 : uart_port_lock_irq(&up->port);
169 0 : mode = serial_in(up, UART_RSA_MSR);
170 0 : result = !(mode & UART_RSA_MSR_FIFO);
171 :
172 0 : if (!result) {
173 0 : serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
174 0 : mode = serial_in(up, UART_RSA_MSR);
175 0 : result = !(mode & UART_RSA_MSR_FIFO);
176 0 : }
177 :
178 0 : if (result)
179 0 : up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
180 0 : uart_port_unlock_irq(&up->port);
181 0 : }
182 : EXPORT_SYMBOL_FOR_MODULES(rsa_disable, "8250_base");
183 :
184 0 : void rsa_autoconfig(struct uart_8250_port *up)
185 : {
186 : /* Only probe for RSA ports if we got the region. */
187 0 : if (up->port.type != PORT_16550A)
188 0 : return;
189 0 : if (!(up->probe & UART_PROBE_RSA))
190 0 : return;
191 :
192 0 : if (__rsa_enable(up))
193 0 : up->port.type = PORT_RSA;
194 0 : }
195 : EXPORT_SYMBOL_FOR_MODULES(rsa_autoconfig, "8250_base");
196 :
197 0 : void rsa_reset(struct uart_8250_port *up)
198 : {
199 0 : if (up->port.type != PORT_RSA)
200 0 : return;
201 :
202 0 : serial_out(up, UART_RSA_FRR, 0);
203 0 : }
204 : EXPORT_SYMBOL_FOR_MODULES(rsa_reset, "8250_base");
205 :
206 : #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
207 : #ifndef MODULE
208 : /*
209 : * Keep the old "8250" name working as well for the module options so we don't
210 : * break people. We need to keep the names identical and the convenient macros
211 : * will happily refuse to let us do that by failing the build with redefinition
212 : * errors of global variables. So we stick them inside a dummy function to
213 : * avoid those conflicts. The options still get parsed, and the redefined
214 : * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
215 : *
216 : * This is hacky. I'm sorry.
217 : */
218 0 : static void __used rsa8250_options(void)
219 : {
220 : #undef MODULE_PARAM_PREFIX
221 : #define MODULE_PARAM_PREFIX "8250_core."
222 :
223 : __module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
224 : ¶m_array_ops, .arr = &__param_arr_probe_rsa,
225 : 0444, -1, 0);
226 0 : }
227 : #endif
228 : #endif
|