[CONNECTOR]: async connector mode.
[linux-2.6/verdex.git] / drivers / serial / mux.c
blob189064607709d9ae717cdb70646062b768ba2c0d
1 /*
2 ** mux.c:
3 ** serial driver for the Mux console found in some PA-RISC servers.
4 **
5 ** (c) Copyright 2002 Ryan Bradetich
6 ** (c) Copyright 2002 Hewlett-Packard Company
7 **
8 ** This program is free software; you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License as published by
10 ** the Free Software Foundation; either version 2 of the License, or
11 ** (at your option) any later version.
13 ** This Driver currently only supports the console (port 0) on the MUX.
14 ** Additional work will be needed on this driver to enable the full
15 ** functionality of the MUX.
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/tty.h>
22 #include <linux/ioport.h>
23 #include <linux/init.h>
24 #include <linux/serial.h>
25 #include <linux/console.h>
26 #include <linux/slab.h>
27 #include <linux/delay.h> /* for udelay */
28 #include <linux/device.h>
29 #include <asm/io.h>
30 #include <asm/parisc-device.h>
32 #ifdef CONFIG_MAGIC_SYSRQ
33 #include <linux/sysrq.h>
34 #define SUPPORT_SYSRQ
35 #endif
37 #include <linux/serial_core.h>
39 #define MUX_OFFSET 0x800
40 #define MUX_LINE_OFFSET 0x80
42 #define MUX_FIFO_SIZE 255
43 #define MUX_POLL_DELAY (30 * HZ / 1000)
45 #define IO_DATA_REG_OFFSET 0x3c
46 #define IO_DCOUNT_REG_OFFSET 0x40
48 #define MUX_EOFIFO(status) ((status & 0xF000) == 0xF000)
49 #define MUX_STATUS(status) ((status & 0xF000) == 0x8000)
50 #define MUX_BREAK(status) ((status & 0xF000) == 0x2000)
52 #define MUX_NR 256
53 static unsigned int port_cnt = 0;
54 static struct uart_port mux_ports[MUX_NR];
56 static struct uart_driver mux_driver = {
57 .owner = THIS_MODULE,
58 .driver_name = "ttyB",
59 .dev_name = "ttyB",
60 .major = MUX_MAJOR,
61 .minor = 0,
62 .nr = MUX_NR,
65 static struct timer_list mux_timer;
67 #define UART_PUT_CHAR(p, c) __raw_writel((c), (unsigned long)(p)->membase + IO_DATA_REG_OFFSET)
68 #define UART_GET_FIFO_CNT(p) __raw_readl((unsigned long)(p)->membase + IO_DCOUNT_REG_OFFSET)
69 #define GET_MUX_PORTS(iodc_data) ((((iodc_data)[4] & 0xf0) >> 4) * 8) + 8
71 /**
72 * mux_tx_empty - Check if the transmitter fifo is empty.
73 * @port: Ptr to the uart_port.
75 * This function test if the transmitter fifo for the port
76 * described by 'port' is empty. If it is empty, this function
77 * should return TIOCSER_TEMT, otherwise return 0.
79 static unsigned int mux_tx_empty(struct uart_port *port)
81 unsigned int cnt = __raw_readl((unsigned long)port->membase
82 + IO_DCOUNT_REG_OFFSET);
84 return cnt ? 0 : TIOCSER_TEMT;
87 /**
88 * mux_set_mctrl - Set the current state of the modem control inputs.
89 * @ports: Ptr to the uart_port.
90 * @mctrl: Modem control bits.
92 * The Serial MUX does not support CTS, DCD or DSR so this function
93 * is ignored.
95 static void mux_set_mctrl(struct uart_port *port, unsigned int mctrl)
99 /**
100 * mux_get_mctrl - Returns the current state of modem control inputs.
101 * @port: Ptr to the uart_port.
103 * The Serial MUX does not support CTS, DCD or DSR so these lines are
104 * treated as permanently active.
106 static unsigned int mux_get_mctrl(struct uart_port *port)
108 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
112 * mux_stop_tx - Stop transmitting characters.
113 * @port: Ptr to the uart_port.
115 * The Serial MUX does not support this function.
117 static void mux_stop_tx(struct uart_port *port)
122 * mux_start_tx - Start transmitting characters.
123 * @port: Ptr to the uart_port.
125 * The Serial Mux does not support this function.
127 static void mux_start_tx(struct uart_port *port)
132 * mux_stop_rx - Stop receiving characters.
133 * @port: Ptr to the uart_port.
135 * The Serial Mux does not support this function.
137 static void mux_stop_rx(struct uart_port *port)
142 * mux_enable_ms - Enable modum status interrupts.
143 * @port: Ptr to the uart_port.
145 * The Serial Mux does not support this function.
147 static void mux_enable_ms(struct uart_port *port)
152 * mux_break_ctl - Control the transmitssion of a break signal.
153 * @port: Ptr to the uart_port.
154 * @break_state: Raise/Lower the break signal.
156 * The Serial Mux does not support this function.
158 static void mux_break_ctl(struct uart_port *port, int break_state)
163 * mux_write - Write chars to the mux fifo.
164 * @port: Ptr to the uart_port.
166 * This function writes all the data from the uart buffer to
167 * the mux fifo.
169 static void mux_write(struct uart_port *port)
171 int count;
172 struct circ_buf *xmit = &port->info->xmit;
174 if(port->x_char) {
175 UART_PUT_CHAR(port, port->x_char);
176 port->icount.tx++;
177 port->x_char = 0;
178 return;
181 if(uart_circ_empty(xmit) || uart_tx_stopped(port)) {
182 mux_stop_tx(port);
183 return;
186 count = (port->fifosize) - UART_GET_FIFO_CNT(port);
187 do {
188 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
189 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
190 port->icount.tx++;
191 if(uart_circ_empty(xmit))
192 break;
194 } while(--count > 0);
196 while(UART_GET_FIFO_CNT(port))
197 udelay(1);
199 if(uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
200 uart_write_wakeup(port);
202 if (uart_circ_empty(xmit))
203 mux_stop_tx(port);
207 * mux_read - Read chars from the mux fifo.
208 * @port: Ptr to the uart_port.
210 * This reads all available data from the mux's fifo and pushes
211 * the data to the tty layer.
213 static void mux_read(struct uart_port *port)
215 int data;
216 struct tty_struct *tty = port->info->tty;
217 __u32 start_count = port->icount.rx;
219 while(1) {
220 data = __raw_readl((unsigned long)port->membase
221 + IO_DATA_REG_OFFSET);
223 if (MUX_STATUS(data))
224 continue;
226 if (MUX_EOFIFO(data))
227 break;
229 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
230 continue;
232 *tty->flip.char_buf_ptr = data & 0xffu;
233 *tty->flip.flag_buf_ptr = TTY_NORMAL;
234 port->icount.rx++;
236 if (MUX_BREAK(data)) {
237 port->icount.brk++;
238 if(uart_handle_break(port))
239 continue;
242 if (uart_handle_sysrq_char(port, data & 0xffu, NULL))
243 continue;
245 tty->flip.flag_buf_ptr++;
246 tty->flip.char_buf_ptr++;
247 tty->flip.count++;
250 if (start_count != port->icount.rx) {
251 tty_flip_buffer_push(tty);
256 * mux_startup - Initialize the port.
257 * @port: Ptr to the uart_port.
259 * Grab any resources needed for this port and start the
260 * mux timer.
262 static int mux_startup(struct uart_port *port)
264 mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
265 return 0;
269 * mux_shutdown - Disable the port.
270 * @port: Ptr to the uart_port.
272 * Release any resources needed for the port.
274 static void mux_shutdown(struct uart_port *port)
279 * mux_set_termios - Chane port parameters.
280 * @port: Ptr to the uart_port.
281 * @termios: new termios settings.
282 * @old: old termios settings.
284 * The Serial Mux does not support this function.
286 static void
287 mux_set_termios(struct uart_port *port, struct termios *termios,
288 struct termios *old)
293 * mux_type - Describe the port.
294 * @port: Ptr to the uart_port.
296 * Return a pointer to a string constant describing the
297 * specified port.
299 static const char *mux_type(struct uart_port *port)
301 return "Mux";
305 * mux_release_port - Release memory and IO regions.
306 * @port: Ptr to the uart_port.
308 * Release any memory and IO region resources currently in use by
309 * the port.
311 static void mux_release_port(struct uart_port *port)
316 * mux_request_port - Request memory and IO regions.
317 * @port: Ptr to the uart_port.
319 * Request any memory and IO region resources required by the port.
320 * If any fail, no resources should be registered when this function
321 * returns, and it should return -EBUSY on failure.
323 static int mux_request_port(struct uart_port *port)
325 return 0;
329 * mux_config_port - Perform port autoconfiguration.
330 * @port: Ptr to the uart_port.
331 * @type: Bitmask of required configurations.
333 * Perform any autoconfiguration steps for the port. This functino is
334 * called if the UPF_BOOT_AUTOCONF flag is specified for the port.
335 * [Note: This is required for now because of a bug in the Serial core.
336 * rmk has already submitted a patch to linus, should be available for
337 * 2.5.47.]
339 static void mux_config_port(struct uart_port *port, int type)
341 port->type = PORT_MUX;
345 * mux_verify_port - Verify the port information.
346 * @port: Ptr to the uart_port.
347 * @ser: Ptr to the serial information.
349 * Verify the new serial port information contained within serinfo is
350 * suitable for this port type.
352 static int mux_verify_port(struct uart_port *port, struct serial_struct *ser)
354 if(port->membase == NULL)
355 return -EINVAL;
357 return 0;
361 * mux_drv_poll - Mux poll function.
362 * @unused: Unused variable
364 * This function periodically polls the Serial MUX to check for new data.
366 static void mux_poll(unsigned long unused)
368 int i;
370 for(i = 0; i < port_cnt; ++i) {
371 if(!mux_ports[i].info)
372 continue;
374 mux_read(&mux_ports[i]);
375 mux_write(&mux_ports[i]);
378 mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
382 #ifdef CONFIG_SERIAL_MUX_CONSOLE
383 static void mux_console_write(struct console *co, const char *s, unsigned count)
385 while(count--)
386 pdc_iodc_putc(*s++);
389 static int mux_console_setup(struct console *co, char *options)
391 return 0;
394 struct tty_driver *mux_console_device(struct console *co, int *index)
396 *index = co->index;
397 return mux_driver.tty_driver;
400 static struct console mux_console = {
401 .name = "ttyB",
402 .write = mux_console_write,
403 .device = mux_console_device,
404 .setup = mux_console_setup,
405 .flags = CON_ENABLED | CON_PRINTBUFFER,
406 .index = 0,
409 #define MUX_CONSOLE &mux_console
410 #else
411 #define MUX_CONSOLE NULL
412 #endif
414 static struct uart_ops mux_pops = {
415 .tx_empty = mux_tx_empty,
416 .set_mctrl = mux_set_mctrl,
417 .get_mctrl = mux_get_mctrl,
418 .stop_tx = mux_stop_tx,
419 .start_tx = mux_start_tx,
420 .stop_rx = mux_stop_rx,
421 .enable_ms = mux_enable_ms,
422 .break_ctl = mux_break_ctl,
423 .startup = mux_startup,
424 .shutdown = mux_shutdown,
425 .set_termios = mux_set_termios,
426 .type = mux_type,
427 .release_port = mux_release_port,
428 .request_port = mux_request_port,
429 .config_port = mux_config_port,
430 .verify_port = mux_verify_port,
434 * mux_probe - Determine if the Serial Mux should claim this device.
435 * @dev: The parisc device.
437 * Deterimine if the Serial Mux should claim this chip (return 0)
438 * or not (return 1).
440 static int __init mux_probe(struct parisc_device *dev)
442 int i, status, ports;
443 u8 iodc_data[32];
444 unsigned long bytecnt;
445 struct uart_port *port;
447 status = pdc_iodc_read(&bytecnt, dev->hpa, 0, iodc_data, 32);
448 if(status != PDC_OK) {
449 printk(KERN_ERR "Serial mux: Unable to read IODC.\n");
450 return 1;
453 ports = GET_MUX_PORTS(iodc_data);
454 printk(KERN_INFO "Serial mux driver (%d ports) Revision: 0.3\n", ports);
456 if(!port_cnt) {
457 mux_driver.cons = MUX_CONSOLE;
459 status = uart_register_driver(&mux_driver);
460 if(status) {
461 printk(KERN_ERR "Serial mux: Unable to register driver.\n");
462 return 1;
465 init_timer(&mux_timer);
466 mux_timer.function = mux_poll;
469 for(i = 0; i < ports; ++i, ++port_cnt) {
470 port = &mux_ports[port_cnt];
471 port->iobase = 0;
472 port->mapbase = dev->hpa + MUX_OFFSET + (i * MUX_LINE_OFFSET);
473 port->membase = ioremap(port->mapbase, MUX_LINE_OFFSET);
474 port->iotype = SERIAL_IO_MEM;
475 port->type = PORT_MUX;
476 port->irq = SERIAL_IRQ_NONE;
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 status = uart_add_one_port(&mux_driver, port);
483 BUG_ON(status);
486 #ifdef CONFIG_SERIAL_MUX_CONSOLE
487 register_console(&mux_console);
488 #endif
489 return 0;
492 static struct parisc_device_id mux_tbl[] = {
493 { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0000D },
494 { 0, }
497 MODULE_DEVICE_TABLE(parisc, mux_tbl);
499 static struct parisc_driver serial_mux_driver = {
500 .name = "Serial MUX",
501 .id_table = mux_tbl,
502 .probe = mux_probe,
506 * mux_init - Serial MUX initalization procedure.
508 * Register the Serial MUX driver.
510 static int __init mux_init(void)
512 return register_parisc_driver(&serial_mux_driver);
516 * mux_exit - Serial MUX cleanup procedure.
518 * Unregister the Serial MUX driver from the tty layer.
520 static void __exit mux_exit(void)
522 int i;
524 for (i = 0; i < port_cnt; i++) {
525 uart_remove_one_port(&mux_driver, &mux_ports[i]);
528 uart_unregister_driver(&mux_driver);
531 module_init(mux_init);
532 module_exit(mux_exit);
534 MODULE_AUTHOR("Ryan Bradetich");
535 MODULE_DESCRIPTION("Serial MUX driver");
536 MODULE_LICENSE("GPL");
537 MODULE_ALIAS_CHARDEV_MAJOR(MUX_MAJOR);