xref: /linux/drivers/tty/serial/8250/8250_rsa.c (revision 205a7309cccd34ad49c2b6b1b59b907c12395d6c)
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 		uart_port_lock_irq(&up->port);
144 		__rsa_enable(up);
145 		uart_port_unlock_irq(&up->port);
146 	}
147 	if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
148 		serial_out(up, UART_RSA_FRR, 0);
149 }
150 EXPORT_SYMBOL_GPL_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 void rsa_disable(struct uart_8250_port *up)
158 {
159 	unsigned char mode;
160 	int result;
161 
162 	if (up->port.type != PORT_RSA)
163 		return;
164 
165 	if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16)
166 		return;
167 
168 	uart_port_lock_irq(&up->port);
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 	uart_port_unlock_irq(&up->port);
181 }
182 EXPORT_SYMBOL_GPL_FOR_MODULES(rsa_disable, "8250_base");
183 
184 void rsa_autoconfig(struct uart_8250_port *up)
185 {
186 	/* Only probe for RSA ports if we got the region. */
187 	if (up->port.type != PORT_16550A)
188 		return;
189 	if (!(up->probe & UART_PROBE_RSA))
190 		return;
191 
192 	if (__rsa_enable(up))
193 		up->port.type = PORT_RSA;
194 }
195 EXPORT_SYMBOL_GPL_FOR_MODULES(rsa_autoconfig, "8250_base");
196 
197 void rsa_reset(struct uart_8250_port *up)
198 {
199 	if (up->port.type != PORT_RSA)
200 		return;
201 
202 	serial_out(up, UART_RSA_FRR, 0);
203 }
204 EXPORT_SYMBOL_GPL_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 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 		&param_array_ops, .arr = &__param_arr_probe_rsa,
225 		0444, -1, 0);
226 }
227 #endif
228 #endif
229