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 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS 111 #ifndef MODULE 112 /* 113 * Keep the old "8250" name working as well for the module options so we don't 114 * break people. We need to keep the names identical and the convenient macros 115 * will happily refuse to let us do that by failing the build with redefinition 116 * errors of global variables. So we stick them inside a dummy function to 117 * avoid those conflicts. The options still get parsed, and the redefined 118 * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive. 119 * 120 * This is hacky. I'm sorry. 121 */ 122 static void __used rsa8250_options(void) 123 { 124 #undef MODULE_PARAM_PREFIX 125 #define MODULE_PARAM_PREFIX "8250_core." 126 127 __module_param_call(MODULE_PARAM_PREFIX, probe_rsa, 128 ¶m_array_ops, .arr = &__param_arr_probe_rsa, 129 0444, -1, 0); 130 } 131 #endif 132 #endif 133