Greg Kroah-Hartman | e3b3d0f | 2017-11-06 18:11:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | ** mux.c: |
| 4 | ** serial driver for the Mux console found in some PA-RISC servers. |
| 5 | ** |
| 6 | ** (c) Copyright 2002 Ryan Bradetich |
| 7 | ** (c) Copyright 2002 Hewlett-Packard Company |
| 8 | ** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | ** This Driver currently only supports the console (port 0) on the MUX. |
| 10 | ** Additional work will be needed on this driver to enable the full |
| 11 | ** functionality of the MUX. |
| 12 | ** |
| 13 | */ |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/ioport.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/serial.h> |
Jiri Slaby | ee160a3 | 2011-09-01 16:20:57 +0200 | [diff] [blame] | 19 | #include <linux/tty.h> |
| 20 | #include <linux/tty_flip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/console.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/delay.h> /* for udelay */ |
| 23 | #include <linux/device.h> |
Zihao Tang | b1c92c1 | 2021-05-13 11:01:37 +0800 | [diff] [blame] | 24 | #include <linux/io.h> |
Matthew Wilcox | ae8c75c | 2005-10-21 22:58:03 -0400 | [diff] [blame] | 25 | #include <asm/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/parisc-device.h> |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/sysrq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/serial_core.h> |
| 30 | |
| 31 | #define MUX_OFFSET 0x800 |
| 32 | #define MUX_LINE_OFFSET 0x80 |
| 33 | |
| 34 | #define MUX_FIFO_SIZE 255 |
| 35 | #define MUX_POLL_DELAY (30 * HZ / 1000) |
| 36 | |
| 37 | #define IO_DATA_REG_OFFSET 0x3c |
| 38 | #define IO_DCOUNT_REG_OFFSET 0x40 |
| 39 | |
| 40 | #define MUX_EOFIFO(status) ((status & 0xF000) == 0xF000) |
| 41 | #define MUX_STATUS(status) ((status & 0xF000) == 0x8000) |
| 42 | #define MUX_BREAK(status) ((status & 0xF000) == 0x2000) |
| 43 | |
| 44 | #define MUX_NR 256 |
Helge Deller | 5076c15 | 2006-03-27 12:52:15 -0700 | [diff] [blame] | 45 | static unsigned int port_cnt __read_mostly; |
Ryan Bradetich | 4bd5d82 | 2006-11-03 05:38:39 +0000 | [diff] [blame] | 46 | struct mux_port { |
| 47 | struct uart_port port; |
| 48 | int enabled; |
| 49 | }; |
| 50 | static struct mux_port mux_ports[MUX_NR]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | static struct uart_driver mux_driver = { |
| 53 | .owner = THIS_MODULE, |
| 54 | .driver_name = "ttyB", |
| 55 | .dev_name = "ttyB", |
| 56 | .major = MUX_MAJOR, |
| 57 | .minor = 0, |
| 58 | .nr = MUX_NR, |
| 59 | }; |
| 60 | |
| 61 | static struct timer_list mux_timer; |
| 62 | |
Ryan Bradetich | 92495c0 | 2005-11-17 16:36:52 -0500 | [diff] [blame] | 63 | #define UART_PUT_CHAR(p, c) __raw_writel((c), (p)->membase + IO_DATA_REG_OFFSET) |
| 64 | #define UART_GET_FIFO_CNT(p) __raw_readl((p)->membase + IO_DCOUNT_REG_OFFSET) |
Ryan Bradetich | 6142544 | 2006-11-10 03:08:45 +0000 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * get_mux_port_count - Get the number of available ports on the Mux. |
| 68 | * @dev: The parisc device. |
| 69 | * |
| 70 | * This function is used to determine the number of ports the Mux |
| 71 | * supports. The IODC data reports the number of ports the Mux |
| 72 | * can support, but there are cases where not all the Mux ports |
| 73 | * are connected. This function can override the IODC and |
| 74 | * return the true port count. |
| 75 | */ |
| 76 | static int __init get_mux_port_count(struct parisc_device *dev) |
| 77 | { |
Ryan Bradetich | 514fb84 | 2006-11-10 04:06:14 +0000 | [diff] [blame] | 78 | int status; |
Ryan Bradetich | 6142544 | 2006-11-10 03:08:45 +0000 | [diff] [blame] | 79 | u8 iodc_data[32]; |
| 80 | unsigned long bytecnt; |
| 81 | |
Ryan Bradetich | 6142544 | 2006-11-10 03:08:45 +0000 | [diff] [blame] | 82 | /* If this is the built-in Mux for the K-Class (Eole CAP/MUX), |
| 83 | * we only need to allocate resources for 1 port since the |
| 84 | * other 7 ports are not connected. |
| 85 | */ |
Ryan Bradetich | 514fb84 | 2006-11-10 04:06:14 +0000 | [diff] [blame] | 86 | if(dev->id.hversion == 0x15) |
Ryan Bradetich | 6142544 | 2006-11-10 03:08:45 +0000 | [diff] [blame] | 87 | return 1; |
| 88 | |
Ryan Bradetich | 514fb84 | 2006-11-10 04:06:14 +0000 | [diff] [blame] | 89 | status = pdc_iodc_read(&bytecnt, dev->hpa.start, 0, iodc_data, 32); |
| 90 | BUG_ON(status != PDC_OK); |
| 91 | |
Ryan Bradetich | 6142544 | 2006-11-10 03:08:45 +0000 | [diff] [blame] | 92 | /* Return the number of ports specified in the iodc data. */ |
| 93 | return ((((iodc_data)[4] & 0xf0) >> 4) * 8) + 8; |
| 94 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * mux_tx_empty - Check if the transmitter fifo is empty. |
| 98 | * @port: Ptr to the uart_port. |
| 99 | * |
| 100 | * This function test if the transmitter fifo for the port |
| 101 | * described by 'port' is empty. If it is empty, this function |
| 102 | * should return TIOCSER_TEMT, otherwise return 0. |
| 103 | */ |
| 104 | static unsigned int mux_tx_empty(struct uart_port *port) |
| 105 | { |
Ryan Bradetich | 92495c0 | 2005-11-17 16:36:52 -0500 | [diff] [blame] | 106 | return UART_GET_FIFO_CNT(port) ? 0 : TIOCSER_TEMT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * mux_set_mctrl - Set the current state of the modem control inputs. |
| 111 | * @ports: Ptr to the uart_port. |
| 112 | * @mctrl: Modem control bits. |
| 113 | * |
| 114 | * The Serial MUX does not support CTS, DCD or DSR so this function |
| 115 | * is ignored. |
| 116 | */ |
| 117 | static void mux_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 118 | { |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * mux_get_mctrl - Returns the current state of modem control inputs. |
| 123 | * @port: Ptr to the uart_port. |
| 124 | * |
| 125 | * The Serial MUX does not support CTS, DCD or DSR so these lines are |
| 126 | * treated as permanently active. |
| 127 | */ |
| 128 | static unsigned int mux_get_mctrl(struct uart_port *port) |
| 129 | { |
| 130 | return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * mux_stop_tx - Stop transmitting characters. |
| 135 | * @port: Ptr to the uart_port. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | * |
| 137 | * The Serial MUX does not support this function. |
| 138 | */ |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 139 | static void mux_stop_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * mux_start_tx - Start transmitting characters. |
| 145 | * @port: Ptr to the uart_port. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | * |
| 147 | * The Serial Mux does not support this function. |
| 148 | */ |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 149 | static void mux_start_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * mux_stop_rx - Stop receiving characters. |
| 155 | * @port: Ptr to the uart_port. |
| 156 | * |
| 157 | * The Serial Mux does not support this function. |
| 158 | */ |
| 159 | static void mux_stop_rx(struct uart_port *port) |
| 160 | { |
| 161 | } |
| 162 | |
| 163 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | * mux_break_ctl - Control the transmitssion of a break signal. |
| 165 | * @port: Ptr to the uart_port. |
| 166 | * @break_state: Raise/Lower the break signal. |
| 167 | * |
| 168 | * The Serial Mux does not support this function. |
| 169 | */ |
| 170 | static void mux_break_ctl(struct uart_port *port, int break_state) |
| 171 | { |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * mux_write - Write chars to the mux fifo. |
| 176 | * @port: Ptr to the uart_port. |
| 177 | * |
| 178 | * This function writes all the data from the uart buffer to |
| 179 | * the mux fifo. |
| 180 | */ |
| 181 | static void mux_write(struct uart_port *port) |
| 182 | { |
| 183 | int count; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 184 | struct circ_buf *xmit = &port->state->xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | if(port->x_char) { |
| 187 | UART_PUT_CHAR(port, port->x_char); |
| 188 | port->icount.tx++; |
| 189 | port->x_char = 0; |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | if(uart_circ_empty(xmit) || uart_tx_stopped(port)) { |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 194 | mux_stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | |
| 198 | count = (port->fifosize) - UART_GET_FIFO_CNT(port); |
| 199 | do { |
| 200 | UART_PUT_CHAR(port, xmit->buf[xmit->tail]); |
| 201 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 202 | port->icount.tx++; |
| 203 | if(uart_circ_empty(xmit)) |
| 204 | break; |
| 205 | |
| 206 | } while(--count > 0); |
| 207 | |
| 208 | while(UART_GET_FIFO_CNT(port)) |
| 209 | udelay(1); |
| 210 | |
| 211 | if(uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 212 | uart_write_wakeup(port); |
| 213 | |
| 214 | if (uart_circ_empty(xmit)) |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 215 | mux_stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
| 219 | * mux_read - Read chars from the mux fifo. |
| 220 | * @port: Ptr to the uart_port. |
| 221 | * |
| 222 | * This reads all available data from the mux's fifo and pushes |
| 223 | * the data to the tty layer. |
| 224 | */ |
| 225 | static void mux_read(struct uart_port *port) |
| 226 | { |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 227 | struct tty_port *tport = &port->state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | int data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | __u32 start_count = port->icount.rx; |
| 230 | |
| 231 | while(1) { |
Ryan Bradetich | 92495c0 | 2005-11-17 16:36:52 -0500 | [diff] [blame] | 232 | data = __raw_readl(port->membase + IO_DATA_REG_OFFSET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
| 234 | if (MUX_STATUS(data)) |
| 235 | continue; |
| 236 | |
| 237 | if (MUX_EOFIFO(data)) |
| 238 | break; |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | port->icount.rx++; |
| 241 | |
| 242 | if (MUX_BREAK(data)) { |
| 243 | port->icount.brk++; |
| 244 | if(uart_handle_break(port)) |
| 245 | continue; |
| 246 | } |
| 247 | |
Matthew Wilcox | be577a5 | 2006-10-06 20:47:23 -0600 | [diff] [blame] | 248 | if (uart_handle_sysrq_char(port, data & 0xffu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | continue; |
| 250 | |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 251 | tty_insert_flip_char(tport, data & 0xFF, TTY_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 254 | if (start_count != port->icount.rx) |
| 255 | tty_flip_buffer_push(tport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
| 259 | * mux_startup - Initialize the port. |
| 260 | * @port: Ptr to the uart_port. |
| 261 | * |
| 262 | * Grab any resources needed for this port and start the |
| 263 | * mux timer. |
| 264 | */ |
| 265 | static int mux_startup(struct uart_port *port) |
| 266 | { |
Ryan Bradetich | 4bd5d82 | 2006-11-03 05:38:39 +0000 | [diff] [blame] | 267 | mux_ports[port->line].enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * mux_shutdown - Disable the port. |
| 273 | * @port: Ptr to the uart_port. |
| 274 | * |
| 275 | * Release any resources needed for the port. |
| 276 | */ |
| 277 | static void mux_shutdown(struct uart_port *port) |
| 278 | { |
Ryan Bradetich | 4bd5d82 | 2006-11-03 05:38:39 +0000 | [diff] [blame] | 279 | mux_ports[port->line].enabled = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /** |
| 283 | * mux_set_termios - Chane port parameters. |
| 284 | * @port: Ptr to the uart_port. |
| 285 | * @termios: new termios settings. |
| 286 | * @old: old termios settings. |
| 287 | * |
| 288 | * The Serial Mux does not support this function. |
| 289 | */ |
| 290 | static void |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 291 | mux_set_termios(struct uart_port *port, struct ktermios *termios, |
| 292 | struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * mux_type - Describe the port. |
| 298 | * @port: Ptr to the uart_port. |
| 299 | * |
| 300 | * Return a pointer to a string constant describing the |
| 301 | * specified port. |
| 302 | */ |
| 303 | static const char *mux_type(struct uart_port *port) |
| 304 | { |
| 305 | return "Mux"; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * mux_release_port - Release memory and IO regions. |
| 310 | * @port: Ptr to the uart_port. |
| 311 | * |
| 312 | * Release any memory and IO region resources currently in use by |
| 313 | * the port. |
| 314 | */ |
| 315 | static void mux_release_port(struct uart_port *port) |
| 316 | { |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * mux_request_port - Request memory and IO regions. |
| 321 | * @port: Ptr to the uart_port. |
| 322 | * |
| 323 | * Request any memory and IO region resources required by the port. |
| 324 | * If any fail, no resources should be registered when this function |
| 325 | * returns, and it should return -EBUSY on failure. |
| 326 | */ |
| 327 | static int mux_request_port(struct uart_port *port) |
| 328 | { |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * mux_config_port - Perform port autoconfiguration. |
| 334 | * @port: Ptr to the uart_port. |
| 335 | * @type: Bitmask of required configurations. |
| 336 | * |
Ryan Bradetich | 4bd5d82 | 2006-11-03 05:38:39 +0000 | [diff] [blame] | 337 | * Perform any autoconfiguration steps for the port. This function is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | * called if the UPF_BOOT_AUTOCONF flag is specified for the port. |
| 339 | * [Note: This is required for now because of a bug in the Serial core. |
| 340 | * rmk has already submitted a patch to linus, should be available for |
| 341 | * 2.5.47.] |
| 342 | */ |
| 343 | static void mux_config_port(struct uart_port *port, int type) |
| 344 | { |
| 345 | port->type = PORT_MUX; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * mux_verify_port - Verify the port information. |
| 350 | * @port: Ptr to the uart_port. |
| 351 | * @ser: Ptr to the serial information. |
| 352 | * |
| 353 | * Verify the new serial port information contained within serinfo is |
| 354 | * suitable for this port type. |
| 355 | */ |
| 356 | static int mux_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 357 | { |
| 358 | if(port->membase == NULL) |
| 359 | return -EINVAL; |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * mux_drv_poll - Mux poll function. |
| 366 | * @unused: Unused variable |
| 367 | * |
| 368 | * This function periodically polls the Serial MUX to check for new data. |
| 369 | */ |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 370 | static void mux_poll(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | { |
| 372 | int i; |
| 373 | |
| 374 | for(i = 0; i < port_cnt; ++i) { |
Ryan Bradetich | 4bd5d82 | 2006-11-03 05:38:39 +0000 | [diff] [blame] | 375 | if(!mux_ports[i].enabled) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | continue; |
| 377 | |
Ryan Bradetich | 4bd5d82 | 2006-11-03 05:38:39 +0000 | [diff] [blame] | 378 | mux_read(&mux_ports[i].port); |
| 379 | mux_write(&mux_ports[i].port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY); |
| 383 | } |
| 384 | |
| 385 | |
| 386 | #ifdef CONFIG_SERIAL_MUX_CONSOLE |
| 387 | static void mux_console_write(struct console *co, const char *s, unsigned count) |
| 388 | { |
Ryan Bradetich | 3de7b64 | 2006-11-03 05:52:41 +0000 | [diff] [blame] | 389 | /* Wait until the FIFO drains. */ |
| 390 | while(UART_GET_FIFO_CNT(&mux_ports[0].port)) |
| 391 | udelay(1); |
| 392 | |
| 393 | while(count--) { |
| 394 | if(*s == '\n') { |
| 395 | UART_PUT_CHAR(&mux_ports[0].port, '\r'); |
| 396 | } |
| 397 | UART_PUT_CHAR(&mux_ports[0].port, *s++); |
| 398 | } |
| 399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static int mux_console_setup(struct console *co, char *options) |
| 403 | { |
| 404 | return 0; |
| 405 | } |
| 406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | static struct console mux_console = { |
| 408 | .name = "ttyB", |
| 409 | .write = mux_console_write, |
Axel Lin | aa0bdd2 | 2015-09-19 11:43:07 +0800 | [diff] [blame] | 410 | .device = uart_console_device, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | .setup = mux_console_setup, |
| 412 | .flags = CON_ENABLED | CON_PRINTBUFFER, |
| 413 | .index = 0, |
Axel Lin | aa0bdd2 | 2015-09-19 11:43:07 +0800 | [diff] [blame] | 414 | .data = &mux_driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | }; |
| 416 | |
| 417 | #define MUX_CONSOLE &mux_console |
| 418 | #else |
| 419 | #define MUX_CONSOLE NULL |
| 420 | #endif |
| 421 | |
Julia Lawall | 454d237 | 2017-08-13 08:21:50 +0200 | [diff] [blame] | 422 | static const struct uart_ops mux_pops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | .tx_empty = mux_tx_empty, |
| 424 | .set_mctrl = mux_set_mctrl, |
| 425 | .get_mctrl = mux_get_mctrl, |
| 426 | .stop_tx = mux_stop_tx, |
| 427 | .start_tx = mux_start_tx, |
| 428 | .stop_rx = mux_stop_rx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | .break_ctl = mux_break_ctl, |
| 430 | .startup = mux_startup, |
| 431 | .shutdown = mux_shutdown, |
| 432 | .set_termios = mux_set_termios, |
| 433 | .type = mux_type, |
| 434 | .release_port = mux_release_port, |
| 435 | .request_port = mux_request_port, |
| 436 | .config_port = mux_config_port, |
| 437 | .verify_port = mux_verify_port, |
| 438 | }; |
| 439 | |
| 440 | /** |
| 441 | * mux_probe - Determine if the Serial Mux should claim this device. |
| 442 | * @dev: The parisc device. |
| 443 | * |
| 444 | * Deterimine if the Serial Mux should claim this chip (return 0) |
| 445 | * or not (return 1). |
| 446 | */ |
| 447 | static int __init mux_probe(struct parisc_device *dev) |
| 448 | { |
Ryan Bradetich | 6142544 | 2006-11-10 03:08:45 +0000 | [diff] [blame] | 449 | int i, status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
Ryan Bradetich | 6142544 | 2006-11-10 03:08:45 +0000 | [diff] [blame] | 451 | int port_count = get_mux_port_count(dev); |
Ryan Bradetich | 752b940 | 2006-11-09 04:45:08 +0000 | [diff] [blame] | 452 | printk(KERN_INFO "Serial mux driver (%d ports) Revision: 0.6\n", port_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
Ryan Bradetich | c380f05 | 2006-11-05 01:21:44 +0000 | [diff] [blame] | 454 | dev_set_drvdata(&dev->dev, (void *)(long)port_count); |
| 455 | request_mem_region(dev->hpa.start + MUX_OFFSET, |
| 456 | port_count * MUX_LINE_OFFSET, "Mux"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
| 458 | if(!port_cnt) { |
| 459 | mux_driver.cons = MUX_CONSOLE; |
| 460 | |
| 461 | status = uart_register_driver(&mux_driver); |
| 462 | if(status) { |
| 463 | printk(KERN_ERR "Serial mux: Unable to register driver.\n"); |
| 464 | return 1; |
| 465 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Ryan Bradetich | c380f05 | 2006-11-05 01:21:44 +0000 | [diff] [blame] | 468 | for(i = 0; i < port_count; ++i, ++port_cnt) { |
| 469 | struct uart_port *port = &mux_ports[port_cnt].port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | port->iobase = 0; |
Matthew Wilcox | ae8c75c | 2005-10-21 22:58:03 -0400 | [diff] [blame] | 471 | port->mapbase = dev->hpa.start + MUX_OFFSET + |
| 472 | (i * MUX_LINE_OFFSET); |
Christoph Hellwig | 4bdc0d6 | 2020-01-06 09:43:50 +0100 | [diff] [blame] | 473 | port->membase = ioremap(port->mapbase, MUX_LINE_OFFSET); |
Russell King | 9b4a161 | 2006-02-05 10:48:10 +0000 | [diff] [blame] | 474 | port->iotype = UPIO_MEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | port->type = PORT_MUX; |
Alan Cox | d4e33fa | 2012-01-26 17:44:09 +0000 | [diff] [blame] | 476 | port->irq = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | port->uartclk = 0; |
| 478 | port->fifosize = MUX_FIFO_SIZE; |
| 479 | port->ops = &mux_pops; |
| 480 | port->flags = UPF_BOOT_AUTOCONF; |
| 481 | port->line = port_cnt; |
Dmitry Safonov | b4088e8 | 2019-12-13 00:06:25 +0000 | [diff] [blame] | 482 | port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_MUX_CONSOLE); |
Ryan Bradetich | a137ce85 | 2005-11-17 16:38:28 -0500 | [diff] [blame] | 483 | |
| 484 | /* The port->timeout needs to match what is present in |
| 485 | * uart_wait_until_sent in serial_core.c. Otherwise |
| 486 | * the time spent in msleep_interruptable will be very |
| 487 | * long, causing the appearance of a console hang. |
| 488 | */ |
| 489 | port->timeout = HZ / 50; |
Matthew Wilcox | ae8c75c | 2005-10-21 22:58:03 -0400 | [diff] [blame] | 490 | spin_lock_init(&port->lock); |
Ryan Bradetich | 752b940 | 2006-11-09 04:45:08 +0000 | [diff] [blame] | 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | status = uart_add_one_port(&mux_driver, port); |
| 493 | BUG_ON(status); |
| 494 | } |
| 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
Uwe Kleine-König | 87875c1 | 2021-08-07 11:19:27 +0200 | [diff] [blame] | 499 | static void __exit mux_remove(struct parisc_device *dev) |
Ryan Bradetich | c380f05 | 2006-11-05 01:21:44 +0000 | [diff] [blame] | 500 | { |
Ryan Bradetich | 752b940 | 2006-11-09 04:45:08 +0000 | [diff] [blame] | 501 | int i, j; |
Ryan Bradetich | c380f05 | 2006-11-05 01:21:44 +0000 | [diff] [blame] | 502 | int port_count = (long)dev_get_drvdata(&dev->dev); |
| 503 | |
Ryan Bradetich | c380f05 | 2006-11-05 01:21:44 +0000 | [diff] [blame] | 504 | /* Find Port 0 for this card in the mux_ports list. */ |
| 505 | for(i = 0; i < port_cnt; ++i) { |
| 506 | if(mux_ports[i].port.mapbase == dev->hpa.start + MUX_OFFSET) |
| 507 | break; |
| 508 | } |
| 509 | BUG_ON(i + port_count > port_cnt); |
| 510 | |
| 511 | /* Release the resources associated with each port on the device. */ |
Ryan Bradetich | 752b940 | 2006-11-09 04:45:08 +0000 | [diff] [blame] | 512 | for(j = 0; j < port_count; ++j, ++i) { |
Ryan Bradetich | c380f05 | 2006-11-05 01:21:44 +0000 | [diff] [blame] | 513 | struct uart_port *port = &mux_ports[i].port; |
| 514 | |
| 515 | uart_remove_one_port(&mux_driver, port); |
| 516 | if(port->membase) |
| 517 | iounmap(port->membase); |
| 518 | } |
| 519 | |
| 520 | release_mem_region(dev->hpa.start + MUX_OFFSET, port_count * MUX_LINE_OFFSET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Ryan Bradetich | 752b940 | 2006-11-09 04:45:08 +0000 | [diff] [blame] | 523 | /* Hack. This idea was taken from the 8250_gsc.c on how to properly order |
| 524 | * the serial port detection in the proper order. The idea is we always |
| 525 | * want the builtin mux to be detected before addin mux cards, so we |
| 526 | * specifically probe for the builtin mux cards first. |
| 527 | * |
| 528 | * This table only contains the parisc_device_id of known builtin mux |
| 529 | * devices. All other mux cards will be detected by the generic mux_tbl. |
| 530 | */ |
Helge Deller | fc72b7a | 2017-08-21 21:55:11 +0200 | [diff] [blame] | 531 | static const struct parisc_device_id builtin_mux_tbl[] __initconst = { |
Ryan Bradetich | 752b940 | 2006-11-09 04:45:08 +0000 | [diff] [blame] | 532 | { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, 0x15, 0x0000D }, /* All K-class */ |
| 533 | { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, 0x44, 0x0000D }, /* E35, E45, and E55 */ |
| 534 | { 0, } |
| 535 | }; |
| 536 | |
Helge Deller | fc72b7a | 2017-08-21 21:55:11 +0200 | [diff] [blame] | 537 | static const struct parisc_device_id mux_tbl[] __initconst = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0000D }, |
| 539 | { 0, } |
| 540 | }; |
| 541 | |
Ryan Bradetich | 752b940 | 2006-11-09 04:45:08 +0000 | [diff] [blame] | 542 | MODULE_DEVICE_TABLE(parisc, builtin_mux_tbl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | MODULE_DEVICE_TABLE(parisc, mux_tbl); |
| 544 | |
Helge Deller | fc72b7a | 2017-08-21 21:55:11 +0200 | [diff] [blame] | 545 | static struct parisc_driver builtin_serial_mux_driver __refdata = { |
Ryan Bradetich | 752b940 | 2006-11-09 04:45:08 +0000 | [diff] [blame] | 546 | .name = "builtin_serial_mux", |
| 547 | .id_table = builtin_mux_tbl, |
| 548 | .probe = mux_probe, |
Helge Deller | fc72b7a | 2017-08-21 21:55:11 +0200 | [diff] [blame] | 549 | .remove = __exit_p(mux_remove), |
Ryan Bradetich | 752b940 | 2006-11-09 04:45:08 +0000 | [diff] [blame] | 550 | }; |
| 551 | |
Helge Deller | fc72b7a | 2017-08-21 21:55:11 +0200 | [diff] [blame] | 552 | static struct parisc_driver serial_mux_driver __refdata = { |
Matthew Wilcox | bdad1f8 | 2005-10-21 22:36:23 -0400 | [diff] [blame] | 553 | .name = "serial_mux", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | .id_table = mux_tbl, |
| 555 | .probe = mux_probe, |
Helge Deller | fc72b7a | 2017-08-21 21:55:11 +0200 | [diff] [blame] | 556 | .remove = __exit_p(mux_remove), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | }; |
| 558 | |
| 559 | /** |
Joe Perches | 8f4aafe | 2008-02-03 17:29:25 +0200 | [diff] [blame] | 560 | * mux_init - Serial MUX initialization procedure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | * |
| 562 | * Register the Serial MUX driver. |
| 563 | */ |
| 564 | static int __init mux_init(void) |
| 565 | { |
Ryan Bradetich | 752b940 | 2006-11-09 04:45:08 +0000 | [diff] [blame] | 566 | register_parisc_driver(&builtin_serial_mux_driver); |
| 567 | register_parisc_driver(&serial_mux_driver); |
Ryan Bradetich | c380f05 | 2006-11-05 01:21:44 +0000 | [diff] [blame] | 568 | |
| 569 | if(port_cnt > 0) { |
| 570 | /* Start the Mux timer */ |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 571 | timer_setup(&mux_timer, mux_poll, 0); |
Ryan Bradetich | c380f05 | 2006-11-05 01:21:44 +0000 | [diff] [blame] | 572 | mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY); |
| 573 | |
| 574 | #ifdef CONFIG_SERIAL_MUX_CONSOLE |
| 575 | register_console(&mux_console); |
| 576 | #endif |
| 577 | } |
| 578 | |
Ryan Bradetich | 752b940 | 2006-11-09 04:45:08 +0000 | [diff] [blame] | 579 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /** |
| 583 | * mux_exit - Serial MUX cleanup procedure. |
| 584 | * |
| 585 | * Unregister the Serial MUX driver from the tty layer. |
| 586 | */ |
| 587 | static void __exit mux_exit(void) |
| 588 | { |
Ryan Bradetich | c380f05 | 2006-11-05 01:21:44 +0000 | [diff] [blame] | 589 | /* Delete the Mux timer. */ |
| 590 | if(port_cnt > 0) { |
Julia Lawall | 0f1e126 | 2014-03-26 22:33:42 +0100 | [diff] [blame] | 591 | del_timer_sync(&mux_timer); |
Ryan Bradetich | c380f05 | 2006-11-05 01:21:44 +0000 | [diff] [blame] | 592 | #ifdef CONFIG_SERIAL_MUX_CONSOLE |
| 593 | unregister_console(&mux_console); |
| 594 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Ryan Bradetich | 752b940 | 2006-11-09 04:45:08 +0000 | [diff] [blame] | 597 | unregister_parisc_driver(&builtin_serial_mux_driver); |
Ryan Bradetich | c380f05 | 2006-11-05 01:21:44 +0000 | [diff] [blame] | 598 | unregister_parisc_driver(&serial_mux_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | uart_unregister_driver(&mux_driver); |
| 600 | } |
| 601 | |
| 602 | module_init(mux_init); |
| 603 | module_exit(mux_exit); |
| 604 | |
| 605 | MODULE_AUTHOR("Ryan Bradetich"); |
| 606 | MODULE_DESCRIPTION("Serial MUX driver"); |
| 607 | MODULE_LICENSE("GPL"); |
| 608 | MODULE_ALIAS_CHARDEV_MAJOR(MUX_MAJOR); |