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 
rsa8250_request_resource(struct uart_8250_port * up)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 
rsa8250_release_resource(struct uart_8250_port * up)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 	}
47 }
48 
univ8250_config_port(struct uart_port * port,int flags)49 static void univ8250_config_port(struct uart_port *port, int flags)
50 {
51 	struct uart_8250_port *up = up_to_u8250p(port);
52 	unsigned int i;
53 
54 	up->probe &= ~UART_PROBE_RSA;
55 	if (port->type == PORT_RSA) {
56 		if (rsa8250_request_resource(up) == 0)
57 			up->probe |= UART_PROBE_RSA;
58 	} else if (flags & UART_CONFIG_TYPE) {
59 		for (i = 0; i < probe_rsa_count; i++) {
60 			if (probe_rsa[i] == up->port.iobase) {
61 				if (rsa8250_request_resource(up) == 0)
62 					up->probe |= UART_PROBE_RSA;
63 				break;
64 			}
65 		}
66 	}
67 
68 	univ8250_port_base_ops->config_port(port, flags);
69 
70 	if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
71 		rsa8250_release_resource(up);
72 }
73 
univ8250_request_port(struct uart_port * port)74 static int univ8250_request_port(struct uart_port *port)
75 {
76 	struct uart_8250_port *up = up_to_u8250p(port);
77 	int ret;
78 
79 	ret = univ8250_port_base_ops->request_port(port);
80 	if (ret == 0 && port->type == PORT_RSA) {
81 		ret = rsa8250_request_resource(up);
82 		if (ret < 0)
83 			univ8250_port_base_ops->release_port(port);
84 	}
85 
86 	return ret;
87 }
88 
univ8250_release_port(struct uart_port * port)89 static void univ8250_release_port(struct uart_port *port)
90 {
91 	struct uart_8250_port *up = up_to_u8250p(port);
92 
93 	if (port->type == PORT_RSA)
94 		rsa8250_release_resource(up);
95 	univ8250_port_base_ops->release_port(port);
96 }
97 
univ8250_rsa_support(struct uart_ops * ops)98 void univ8250_rsa_support(struct uart_ops *ops)
99 {
100 	ops->config_port  = univ8250_config_port;
101 	ops->request_port = univ8250_request_port;
102 	ops->release_port = univ8250_release_port;
103 }
104 
105 module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444);
106 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
107 
108 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
109 #ifndef MODULE
110 /*
111  * Keep the old "8250" name working as well for the module options so we don't
112  * break people. We need to keep the names identical and the convenient macros
113  * will happily refuse to let us do that by failing the build with redefinition
114  * errors of global variables.  So we stick them inside a dummy function to
115  * avoid those conflicts.  The options still get parsed, and the redefined
116  * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
117  *
118  * This is hacky. I'm sorry.
119  */
rsa8250_options(void)120 static void __used rsa8250_options(void)
121 {
122 #undef MODULE_PARAM_PREFIX
123 #define MODULE_PARAM_PREFIX "8250_core."
124 
125 	__module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
126 		&param_array_ops, .arr = &__param_arr_probe_rsa,
127 		0444, -1, 0);
128 }
129 #endif
130 #endif
131