xref: /openbmc/linux/drivers/tty/serial/mux.c (revision 54618888)
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.
12 **
13 */
14 
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 <linux/io.h>
25 #include <asm/irq.h>
26 #include <asm/parisc-device.h>
27 
28 #include <linux/sysrq.h>
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
45 static unsigned int port_cnt __read_mostly;
46 struct mux_port {
47 	struct uart_port port;
48 	int enabled;
49 };
50 static struct mux_port mux_ports[MUX_NR];
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 
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)
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 {
78 	int status;
79 	u8 iodc_data[32];
80 	unsigned long bytecnt;
81 
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 	 */
86 	if(dev->id.hversion == 0x15)
87 		return 1;
88 
89 	status = pdc_iodc_read(&bytecnt, dev->hpa.start, 0, iodc_data, 32);
90 	BUG_ON(status != PDC_OK);
91 
92 	/* Return the number of ports specified in the iodc data. */
93 	return ((((iodc_data)[4] & 0xf0) >> 4) * 8) + 8;
94 }
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 {
106 	return UART_GET_FIFO_CNT(port) ? 0 : TIOCSER_TEMT;
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.
136  *
137  * The Serial MUX does not support this function.
138  */
139 static void mux_stop_tx(struct uart_port *port)
140 {
141 }
142 
143 /**
144  * mux_start_tx - Start transmitting characters.
145  * @port: Ptr to the uart_port.
146  *
147  * The Serial Mux does not support this function.
148  */
149 static void mux_start_tx(struct uart_port *port)
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 /**
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 static void mux_tx_done(struct uart_port *port)
175 {
176 	/* FIXME js: really needs to wait? */
177 	while (UART_GET_FIFO_CNT(port))
178 		udelay(1);
179 }
180 
181 /**
182  * mux_write - Write chars to the mux fifo.
183  * @port: Ptr to the uart_port.
184  *
185  * This function writes all the data from the uart buffer to
186  * the mux fifo.
187  */
188 static void mux_write(struct uart_port *port)
189 {
190 	u8 ch;
191 
192 	uart_port_tx_limited(port, ch,
193 		port->fifosize - UART_GET_FIFO_CNT(port),
194 		true,
195 		UART_PUT_CHAR(port, ch),
196 		mux_tx_done(port));
197 }
198 
199 /**
200  * mux_read - Read chars from the mux fifo.
201  * @port: Ptr to the uart_port.
202  *
203  * This reads all available data from the mux's fifo and pushes
204  * the data to the tty layer.
205  */
206 static void mux_read(struct uart_port *port)
207 {
208 	struct tty_port *tport = &port->state->port;
209 	int data;
210 	__u32 start_count = port->icount.rx;
211 
212 	while(1) {
213 		data = __raw_readl(port->membase + IO_DATA_REG_OFFSET);
214 
215 		if (MUX_STATUS(data))
216 			continue;
217 
218 		if (MUX_EOFIFO(data))
219 			break;
220 
221 		port->icount.rx++;
222 
223 		if (MUX_BREAK(data)) {
224 			port->icount.brk++;
225 			if(uart_handle_break(port))
226 				continue;
227 		}
228 
229 		if (uart_handle_sysrq_char(port, data & 0xffu))
230 			continue;
231 
232 		tty_insert_flip_char(tport, data & 0xFF, TTY_NORMAL);
233 	}
234 
235 	if (start_count != port->icount.rx)
236 		tty_flip_buffer_push(tport);
237 }
238 
239 /**
240  * mux_startup - Initialize the port.
241  * @port: Ptr to the uart_port.
242  *
243  * Grab any resources needed for this port and start the
244  * mux timer.
245  */
246 static int mux_startup(struct uart_port *port)
247 {
248 	mux_ports[port->line].enabled = 1;
249 	return 0;
250 }
251 
252 /**
253  * mux_shutdown - Disable the port.
254  * @port: Ptr to the uart_port.
255  *
256  * Release any resources needed for the port.
257  */
258 static void mux_shutdown(struct uart_port *port)
259 {
260 	mux_ports[port->line].enabled = 0;
261 }
262 
263 /**
264  * mux_set_termios - Chane port parameters.
265  * @port: Ptr to the uart_port.
266  * @termios: new termios settings.
267  * @old: old termios settings.
268  *
269  * The Serial Mux does not support this function.
270  */
271 static void
272 mux_set_termios(struct uart_port *port, struct ktermios *termios,
273 	        const struct ktermios *old)
274 {
275 }
276 
277 /**
278  * mux_type - Describe the port.
279  * @port: Ptr to the uart_port.
280  *
281  * Return a pointer to a string constant describing the
282  * specified port.
283  */
284 static const char *mux_type(struct uart_port *port)
285 {
286 	return "Mux";
287 }
288 
289 /**
290  * mux_release_port - Release memory and IO regions.
291  * @port: Ptr to the uart_port.
292  *
293  * Release any memory and IO region resources currently in use by
294  * the port.
295  */
296 static void mux_release_port(struct uart_port *port)
297 {
298 }
299 
300 /**
301  * mux_request_port - Request memory and IO regions.
302  * @port: Ptr to the uart_port.
303  *
304  * Request any memory and IO region resources required by the port.
305  * If any fail, no resources should be registered when this function
306  * returns, and it should return -EBUSY on failure.
307  */
308 static int mux_request_port(struct uart_port *port)
309 {
310 	return 0;
311 }
312 
313 /**
314  * mux_config_port - Perform port autoconfiguration.
315  * @port: Ptr to the uart_port.
316  * @type: Bitmask of required configurations.
317  *
318  * Perform any autoconfiguration steps for the port.  This function is
319  * called if the UPF_BOOT_AUTOCONF flag is specified for the port.
320  * [Note: This is required for now because of a bug in the Serial core.
321  *  rmk has already submitted a patch to linus, should be available for
322  *  2.5.47.]
323  */
324 static void mux_config_port(struct uart_port *port, int type)
325 {
326 	port->type = PORT_MUX;
327 }
328 
329 /**
330  * mux_verify_port - Verify the port information.
331  * @port: Ptr to the uart_port.
332  * @ser: Ptr to the serial information.
333  *
334  * Verify the new serial port information contained within serinfo is
335  * suitable for this port type.
336  */
337 static int mux_verify_port(struct uart_port *port, struct serial_struct *ser)
338 {
339 	if(port->membase == NULL)
340 		return -EINVAL;
341 
342 	return 0;
343 }
344 
345 /**
346  * mux_drv_poll - Mux poll function.
347  * @unused: Unused variable
348  *
349  * This function periodically polls the Serial MUX to check for new data.
350  */
351 static void mux_poll(struct timer_list *unused)
352 {
353 	int i;
354 
355 	for(i = 0; i < port_cnt; ++i) {
356 		if(!mux_ports[i].enabled)
357 			continue;
358 
359 		mux_read(&mux_ports[i].port);
360 		mux_write(&mux_ports[i].port);
361 	}
362 
363 	mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
364 }
365 
366 
367 #ifdef CONFIG_SERIAL_MUX_CONSOLE
368 static void mux_console_write(struct console *co, const char *s, unsigned count)
369 {
370 	/* Wait until the FIFO drains. */
371 	while(UART_GET_FIFO_CNT(&mux_ports[0].port))
372 		udelay(1);
373 
374 	while(count--) {
375 		if(*s == '\n') {
376 			UART_PUT_CHAR(&mux_ports[0].port, '\r');
377 		}
378 		UART_PUT_CHAR(&mux_ports[0].port, *s++);
379 	}
380 
381 }
382 
383 static int mux_console_setup(struct console *co, char *options)
384 {
385         return 0;
386 }
387 
388 static struct console mux_console = {
389 	.name =		"ttyB",
390 	.write =	mux_console_write,
391 	.device =	uart_console_device,
392 	.setup =	mux_console_setup,
393 	.flags =	CON_ENABLED | CON_PRINTBUFFER,
394 	.index =	0,
395 	.data =		&mux_driver,
396 };
397 
398 #define MUX_CONSOLE	&mux_console
399 #else
400 #define MUX_CONSOLE	NULL
401 #endif
402 
403 static const struct uart_ops mux_pops = {
404 	.tx_empty =		mux_tx_empty,
405 	.set_mctrl =		mux_set_mctrl,
406 	.get_mctrl =		mux_get_mctrl,
407 	.stop_tx =		mux_stop_tx,
408 	.start_tx =		mux_start_tx,
409 	.stop_rx =		mux_stop_rx,
410 	.break_ctl =		mux_break_ctl,
411 	.startup =		mux_startup,
412 	.shutdown =		mux_shutdown,
413 	.set_termios =		mux_set_termios,
414 	.type =			mux_type,
415 	.release_port =		mux_release_port,
416 	.request_port =		mux_request_port,
417 	.config_port =		mux_config_port,
418 	.verify_port =		mux_verify_port,
419 };
420 
421 /**
422  * mux_probe - Determine if the Serial Mux should claim this device.
423  * @dev: The parisc device.
424  *
425  * Deterimine if the Serial Mux should claim this chip (return 0)
426  * or not (return 1).
427  */
428 static int __init mux_probe(struct parisc_device *dev)
429 {
430 	int i, status;
431 
432 	int port_count = get_mux_port_count(dev);
433 	printk(KERN_INFO "Serial mux driver (%d ports) Revision: 0.6\n", port_count);
434 
435 	dev_set_drvdata(&dev->dev, (void *)(long)port_count);
436 	request_mem_region(dev->hpa.start + MUX_OFFSET,
437                            port_count * MUX_LINE_OFFSET, "Mux");
438 
439 	if(!port_cnt) {
440 		mux_driver.cons = MUX_CONSOLE;
441 
442 		status = uart_register_driver(&mux_driver);
443 		if(status) {
444 			printk(KERN_ERR "Serial mux: Unable to register driver.\n");
445 			return 1;
446 		}
447 	}
448 
449 	for(i = 0; i < port_count; ++i, ++port_cnt) {
450 		struct uart_port *port = &mux_ports[port_cnt].port;
451 		port->iobase	= 0;
452 		port->mapbase	= dev->hpa.start + MUX_OFFSET +
453 						(i * MUX_LINE_OFFSET);
454 		port->membase	= ioremap(port->mapbase, MUX_LINE_OFFSET);
455 		port->iotype	= UPIO_MEM;
456 		port->type	= PORT_MUX;
457 		port->irq	= 0;
458 		port->uartclk	= 0;
459 		port->fifosize	= MUX_FIFO_SIZE;
460 		port->ops	= &mux_pops;
461 		port->flags	= UPF_BOOT_AUTOCONF;
462 		port->line	= port_cnt;
463 		port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_MUX_CONSOLE);
464 
465 		spin_lock_init(&port->lock);
466 
467 		status = uart_add_one_port(&mux_driver, port);
468 		BUG_ON(status);
469 	}
470 
471 	return 0;
472 }
473 
474 static void __exit mux_remove(struct parisc_device *dev)
475 {
476 	int i, j;
477 	int port_count = (long)dev_get_drvdata(&dev->dev);
478 
479 	/* Find Port 0 for this card in the mux_ports list. */
480 	for(i = 0; i < port_cnt; ++i) {
481 		if(mux_ports[i].port.mapbase == dev->hpa.start + MUX_OFFSET)
482 			break;
483 	}
484 	BUG_ON(i + port_count > port_cnt);
485 
486 	/* Release the resources associated with each port on the device. */
487 	for(j = 0; j < port_count; ++j, ++i) {
488 		struct uart_port *port = &mux_ports[i].port;
489 
490 		uart_remove_one_port(&mux_driver, port);
491 		if(port->membase)
492 			iounmap(port->membase);
493 	}
494 
495 	release_mem_region(dev->hpa.start + MUX_OFFSET, port_count * MUX_LINE_OFFSET);
496 }
497 
498 /* Hack.  This idea was taken from the 8250_gsc.c on how to properly order
499  * the serial port detection in the proper order.   The idea is we always
500  * want the builtin mux to be detected before addin mux cards, so we
501  * specifically probe for the builtin mux cards first.
502  *
503  * This table only contains the parisc_device_id of known builtin mux
504  * devices.  All other mux cards will be detected by the generic mux_tbl.
505  */
506 static const struct parisc_device_id builtin_mux_tbl[] __initconst = {
507 	{ HPHW_A_DIRECT, HVERSION_REV_ANY_ID, 0x15, 0x0000D }, /* All K-class */
508 	{ HPHW_A_DIRECT, HVERSION_REV_ANY_ID, 0x44, 0x0000D }, /* E35, E45, and E55 */
509 	{ 0, }
510 };
511 
512 static const struct parisc_device_id mux_tbl[] __initconst = {
513 	{ HPHW_A_DIRECT, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0000D },
514 	{ 0, }
515 };
516 
517 MODULE_DEVICE_TABLE(parisc, builtin_mux_tbl);
518 MODULE_DEVICE_TABLE(parisc, mux_tbl);
519 
520 static struct parisc_driver builtin_serial_mux_driver __refdata = {
521 	.name =		"builtin_serial_mux",
522 	.id_table =	builtin_mux_tbl,
523 	.probe =	mux_probe,
524 	.remove =       __exit_p(mux_remove),
525 };
526 
527 static struct parisc_driver serial_mux_driver __refdata = {
528 	.name =		"serial_mux",
529 	.id_table =	mux_tbl,
530 	.probe =	mux_probe,
531 	.remove =       __exit_p(mux_remove),
532 };
533 
534 /**
535  * mux_init - Serial MUX initialization procedure.
536  *
537  * Register the Serial MUX driver.
538  */
539 static int __init mux_init(void)
540 {
541 	register_parisc_driver(&builtin_serial_mux_driver);
542 	register_parisc_driver(&serial_mux_driver);
543 
544 	if(port_cnt > 0) {
545 		/* Start the Mux timer */
546 		timer_setup(&mux_timer, mux_poll, 0);
547 		mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
548 
549 #ifdef CONFIG_SERIAL_MUX_CONSOLE
550 	        register_console(&mux_console);
551 #endif
552 	}
553 
554 	return 0;
555 }
556 
557 /**
558  * mux_exit - Serial MUX cleanup procedure.
559  *
560  * Unregister the Serial MUX driver from the tty layer.
561  */
562 static void __exit mux_exit(void)
563 {
564 	/* Delete the Mux timer. */
565 	if(port_cnt > 0) {
566 		del_timer_sync(&mux_timer);
567 #ifdef CONFIG_SERIAL_MUX_CONSOLE
568 		unregister_console(&mux_console);
569 #endif
570 	}
571 
572 	unregister_parisc_driver(&builtin_serial_mux_driver);
573 	unregister_parisc_driver(&serial_mux_driver);
574 	uart_unregister_driver(&mux_driver);
575 }
576 
577 module_init(mux_init);
578 module_exit(mux_exit);
579 
580 MODULE_AUTHOR("Ryan Bradetich");
581 MODULE_DESCRIPTION("Serial MUX driver");
582 MODULE_LICENSE("GPL");
583 MODULE_ALIAS_CHARDEV_MAJOR(MUX_MAJOR);
584