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