treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / tty / serial / mux.c
blob47ab280f553b19c78f259baefd5a2d3a63ad0dfc
1 // SPDX-License-Identifier: GPL-2.0+
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 **
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.
15 #include <linux/module.h>
16 #include <linux/ioport.h>
17 #include <linux/init.h>
18 #include <linux/serial.h>
19 #include <linux/tty.h>
20 #include <linux/tty_flip.h>
21 #include <linux/console.h>
22 #include <linux/delay.h> /* for udelay */
23 #include <linux/device.h>
24 #include <asm/io.h>
25 #include <asm/irq.h>
26 #include <asm/parisc-device.h>
28 #include <linux/sysrq.h>
29 #include <linux/serial_core.h>
31 #define MUX_OFFSET 0x800
32 #define MUX_LINE_OFFSET 0x80
34 #define MUX_FIFO_SIZE 255
35 #define MUX_POLL_DELAY (30 * HZ / 1000)
37 #define IO_DATA_REG_OFFSET 0x3c
38 #define IO_DCOUNT_REG_OFFSET 0x40
40 #define MUX_EOFIFO(status) ((status & 0xF000) == 0xF000)
41 #define MUX_STATUS(status) ((status & 0xF000) == 0x8000)
42 #define MUX_BREAK(status) ((status & 0xF000) == 0x2000)
44 #define MUX_NR 256
45 static unsigned int port_cnt __read_mostly;
46 struct mux_port {
47 struct uart_port port;
48 int enabled;
50 static struct mux_port mux_ports[MUX_NR];
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,
61 static struct timer_list mux_timer;
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)
66 /**
67 * get_mux_port_count - Get the number of available ports on the Mux.
68 * @dev: The parisc device.
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.
76 static int __init get_mux_port_count(struct parisc_device *dev)
78 int status;
79 u8 iodc_data[32];
80 unsigned long bytecnt;
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.
86 if(dev->id.hversion == 0x15)
87 return 1;
89 status = pdc_iodc_read(&bytecnt, dev->hpa.start, 0, iodc_data, 32);
90 BUG_ON(status != PDC_OK);
92 /* Return the number of ports specified in the iodc data. */
93 return ((((iodc_data)[4] & 0xf0) >> 4) * 8) + 8;
96 /**
97 * mux_tx_empty - Check if the transmitter fifo is empty.
98 * @port: Ptr to the uart_port.
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.
104 static unsigned int mux_tx_empty(struct uart_port *port)
106 return UART_GET_FIFO_CNT(port) ? 0 : TIOCSER_TEMT;
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.
114 * The Serial MUX does not support CTS, DCD or DSR so this function
115 * is ignored.
117 static void mux_set_mctrl(struct uart_port *port, unsigned int mctrl)
122 * mux_get_mctrl - Returns the current state of modem control inputs.
123 * @port: Ptr to the uart_port.
125 * The Serial MUX does not support CTS, DCD or DSR so these lines are
126 * treated as permanently active.
128 static unsigned int mux_get_mctrl(struct uart_port *port)
130 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
134 * mux_stop_tx - Stop transmitting characters.
135 * @port: Ptr to the uart_port.
137 * The Serial MUX does not support this function.
139 static void mux_stop_tx(struct uart_port *port)
144 * mux_start_tx - Start transmitting characters.
145 * @port: Ptr to the uart_port.
147 * The Serial Mux does not support this function.
149 static void mux_start_tx(struct uart_port *port)
154 * mux_stop_rx - Stop receiving characters.
155 * @port: Ptr to the uart_port.
157 * The Serial Mux does not support this function.
159 static void mux_stop_rx(struct uart_port *port)
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.
168 * The Serial Mux does not support this function.
170 static void mux_break_ctl(struct uart_port *port, int break_state)
175 * mux_write - Write chars to the mux fifo.
176 * @port: Ptr to the uart_port.
178 * This function writes all the data from the uart buffer to
179 * the mux fifo.
181 static void mux_write(struct uart_port *port)
183 int count;
184 struct circ_buf *xmit = &port->state->xmit;
186 if(port->x_char) {
187 UART_PUT_CHAR(port, port->x_char);
188 port->icount.tx++;
189 port->x_char = 0;
190 return;
193 if(uart_circ_empty(xmit) || uart_tx_stopped(port)) {
194 mux_stop_tx(port);
195 return;
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;
206 } while(--count > 0);
208 while(UART_GET_FIFO_CNT(port))
209 udelay(1);
211 if(uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
212 uart_write_wakeup(port);
214 if (uart_circ_empty(xmit))
215 mux_stop_tx(port);
219 * mux_read - Read chars from the mux fifo.
220 * @port: Ptr to the uart_port.
222 * This reads all available data from the mux's fifo and pushes
223 * the data to the tty layer.
225 static void mux_read(struct uart_port *port)
227 struct tty_port *tport = &port->state->port;
228 int data;
229 __u32 start_count = port->icount.rx;
231 while(1) {
232 data = __raw_readl(port->membase + IO_DATA_REG_OFFSET);
234 if (MUX_STATUS(data))
235 continue;
237 if (MUX_EOFIFO(data))
238 break;
240 port->icount.rx++;
242 if (MUX_BREAK(data)) {
243 port->icount.brk++;
244 if(uart_handle_break(port))
245 continue;
248 if (uart_handle_sysrq_char(port, data & 0xffu))
249 continue;
251 tty_insert_flip_char(tport, data & 0xFF, TTY_NORMAL);
254 if (start_count != port->icount.rx)
255 tty_flip_buffer_push(tport);
259 * mux_startup - Initialize the port.
260 * @port: Ptr to the uart_port.
262 * Grab any resources needed for this port and start the
263 * mux timer.
265 static int mux_startup(struct uart_port *port)
267 mux_ports[port->line].enabled = 1;
268 return 0;
272 * mux_shutdown - Disable the port.
273 * @port: Ptr to the uart_port.
275 * Release any resources needed for the port.
277 static void mux_shutdown(struct uart_port *port)
279 mux_ports[port->line].enabled = 0;
283 * mux_set_termios - Chane port parameters.
284 * @port: Ptr to the uart_port.
285 * @termios: new termios settings.
286 * @old: old termios settings.
288 * The Serial Mux does not support this function.
290 static void
291 mux_set_termios(struct uart_port *port, struct ktermios *termios,
292 struct ktermios *old)
297 * mux_type - Describe the port.
298 * @port: Ptr to the uart_port.
300 * Return a pointer to a string constant describing the
301 * specified port.
303 static const char *mux_type(struct uart_port *port)
305 return "Mux";
309 * mux_release_port - Release memory and IO regions.
310 * @port: Ptr to the uart_port.
312 * Release any memory and IO region resources currently in use by
313 * the port.
315 static void mux_release_port(struct uart_port *port)
320 * mux_request_port - Request memory and IO regions.
321 * @port: Ptr to the uart_port.
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.
327 static int mux_request_port(struct uart_port *port)
329 return 0;
333 * mux_config_port - Perform port autoconfiguration.
334 * @port: Ptr to the uart_port.
335 * @type: Bitmask of required configurations.
337 * Perform any autoconfiguration steps for the port. This function is
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.]
343 static void mux_config_port(struct uart_port *port, int type)
345 port->type = PORT_MUX;
349 * mux_verify_port - Verify the port information.
350 * @port: Ptr to the uart_port.
351 * @ser: Ptr to the serial information.
353 * Verify the new serial port information contained within serinfo is
354 * suitable for this port type.
356 static int mux_verify_port(struct uart_port *port, struct serial_struct *ser)
358 if(port->membase == NULL)
359 return -EINVAL;
361 return 0;
365 * mux_drv_poll - Mux poll function.
366 * @unused: Unused variable
368 * This function periodically polls the Serial MUX to check for new data.
370 static void mux_poll(struct timer_list *unused)
372 int i;
374 for(i = 0; i < port_cnt; ++i) {
375 if(!mux_ports[i].enabled)
376 continue;
378 mux_read(&mux_ports[i].port);
379 mux_write(&mux_ports[i].port);
382 mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
386 #ifdef CONFIG_SERIAL_MUX_CONSOLE
387 static void mux_console_write(struct console *co, const char *s, unsigned count)
389 /* Wait until the FIFO drains. */
390 while(UART_GET_FIFO_CNT(&mux_ports[0].port))
391 udelay(1);
393 while(count--) {
394 if(*s == '\n') {
395 UART_PUT_CHAR(&mux_ports[0].port, '\r');
397 UART_PUT_CHAR(&mux_ports[0].port, *s++);
402 static int mux_console_setup(struct console *co, char *options)
404 return 0;
407 static struct console mux_console = {
408 .name = "ttyB",
409 .write = mux_console_write,
410 .device = uart_console_device,
411 .setup = mux_console_setup,
412 .flags = CON_ENABLED | CON_PRINTBUFFER,
413 .index = 0,
414 .data = &mux_driver,
417 #define MUX_CONSOLE &mux_console
418 #else
419 #define MUX_CONSOLE NULL
420 #endif
422 static const struct uart_ops mux_pops = {
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,
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,
441 * mux_probe - Determine if the Serial Mux should claim this device.
442 * @dev: The parisc device.
444 * Deterimine if the Serial Mux should claim this chip (return 0)
445 * or not (return 1).
447 static int __init mux_probe(struct parisc_device *dev)
449 int i, status;
451 int port_count = get_mux_port_count(dev);
452 printk(KERN_INFO "Serial mux driver (%d ports) Revision: 0.6\n", port_count);
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");
458 if(!port_cnt) {
459 mux_driver.cons = MUX_CONSOLE;
461 status = uart_register_driver(&mux_driver);
462 if(status) {
463 printk(KERN_ERR "Serial mux: Unable to register driver.\n");
464 return 1;
468 for(i = 0; i < port_count; ++i, ++port_cnt) {
469 struct uart_port *port = &mux_ports[port_cnt].port;
470 port->iobase = 0;
471 port->mapbase = dev->hpa.start + MUX_OFFSET +
472 (i * MUX_LINE_OFFSET);
473 port->membase = ioremap(port->mapbase, MUX_LINE_OFFSET);
474 port->iotype = UPIO_MEM;
475 port->type = PORT_MUX;
476 port->irq = 0;
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;
482 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_MUX_CONSOLE);
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.
489 port->timeout = HZ / 50;
490 spin_lock_init(&port->lock);
492 status = uart_add_one_port(&mux_driver, port);
493 BUG_ON(status);
496 return 0;
499 static int __exit mux_remove(struct parisc_device *dev)
501 int i, j;
502 int port_count = (long)dev_get_drvdata(&dev->dev);
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;
509 BUG_ON(i + port_count > port_cnt);
511 /* Release the resources associated with each port on the device. */
512 for(j = 0; j < port_count; ++j, ++i) {
513 struct uart_port *port = &mux_ports[i].port;
515 uart_remove_one_port(&mux_driver, port);
516 if(port->membase)
517 iounmap(port->membase);
520 release_mem_region(dev->hpa.start + MUX_OFFSET, port_count * MUX_LINE_OFFSET);
521 return 0;
524 /* Hack. This idea was taken from the 8250_gsc.c on how to properly order
525 * the serial port detection in the proper order. The idea is we always
526 * want the builtin mux to be detected before addin mux cards, so we
527 * specifically probe for the builtin mux cards first.
529 * This table only contains the parisc_device_id of known builtin mux
530 * devices. All other mux cards will be detected by the generic mux_tbl.
532 static const struct parisc_device_id builtin_mux_tbl[] __initconst = {
533 { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, 0x15, 0x0000D }, /* All K-class */
534 { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, 0x44, 0x0000D }, /* E35, E45, and E55 */
535 { 0, }
538 static const struct parisc_device_id mux_tbl[] __initconst = {
539 { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0000D },
540 { 0, }
543 MODULE_DEVICE_TABLE(parisc, builtin_mux_tbl);
544 MODULE_DEVICE_TABLE(parisc, mux_tbl);
546 static struct parisc_driver builtin_serial_mux_driver __refdata = {
547 .name = "builtin_serial_mux",
548 .id_table = builtin_mux_tbl,
549 .probe = mux_probe,
550 .remove = __exit_p(mux_remove),
553 static struct parisc_driver serial_mux_driver __refdata = {
554 .name = "serial_mux",
555 .id_table = mux_tbl,
556 .probe = mux_probe,
557 .remove = __exit_p(mux_remove),
561 * mux_init - Serial MUX initialization procedure.
563 * Register the Serial MUX driver.
565 static int __init mux_init(void)
567 register_parisc_driver(&builtin_serial_mux_driver);
568 register_parisc_driver(&serial_mux_driver);
570 if(port_cnt > 0) {
571 /* Start the Mux timer */
572 timer_setup(&mux_timer, mux_poll, 0);
573 mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
575 #ifdef CONFIG_SERIAL_MUX_CONSOLE
576 register_console(&mux_console);
577 #endif
580 return 0;
584 * mux_exit - Serial MUX cleanup procedure.
586 * Unregister the Serial MUX driver from the tty layer.
588 static void __exit mux_exit(void)
590 /* Delete the Mux timer. */
591 if(port_cnt > 0) {
592 del_timer_sync(&mux_timer);
593 #ifdef CONFIG_SERIAL_MUX_CONSOLE
594 unregister_console(&mux_console);
595 #endif
598 unregister_parisc_driver(&builtin_serial_mux_driver);
599 unregister_parisc_driver(&serial_mux_driver);
600 uart_unregister_driver(&mux_driver);
603 module_init(mux_init);
604 module_exit(mux_exit);
606 MODULE_AUTHOR("Ryan Bradetich");
607 MODULE_DESCRIPTION("Serial MUX driver");
608 MODULE_LICENSE("GPL");
609 MODULE_ALIAS_CHARDEV_MAJOR(MUX_MAJOR);