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