Merge with Linux 2.5.56.
[linux-2.6/linux-mips.git] / drivers / serial / mux.c
blob3ef15146632ee65354c612d09054a5fc4634b40d
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 <asm/io.h>
28 #ifdef CONFIG_MAGIC_SYSRQ
29 #include <linux/sysrq.h>
30 #define SUPPORT_SYSRQ
31 #endif
33 #include <linux/serial_core.h>
35 #define MUX_NR 1
37 #define MUX_OFFSET 0x800
38 #define MUX_LINE_OFFSET 0x80
40 #define MUX_FIFO_SIZE 255
41 #define MUX_MIN_FREE_SIZE 32
43 #define MUX_FIFO_DRAIN_DELAY 1
44 #define MUX_POLL_DELAY (30 * HZ / 1000)
46 #define IO_COMMAND_REG_OFFSET 0x30
47 #define IO_STATUS_REG_OFFSET 0x34
48 #define IO_DATA_REG_OFFSET 0x3c
49 #define IO_DCOUNT_REG_OFFSET 0x40
50 #define IO_UCOUNT_REG_OFFSET 0x44
51 #define IO_FIFOS_REG_OFFSET 0x48
53 #define MUX_EOFIFO(status) ((status & 0xF000) == 0xF000)
54 #define MUX_STATUS(status) ((status & 0xF000) == 0x8000)
55 #define MUX_BREAK(status) ((status & 0xF000) == 0x2000)
58 static struct uart_port mux_ports[MUX_NR];
59 static struct timer_list mux_timer;
61 #define UART_PUT_CHAR(p, c) __raw_writel((c), (unsigned long)(p)->membase + IO_DATA_REG_OFFSET)
62 #define UART_GET_FIFO_CNT(p) __raw_readl((unsigned long)(p)->membase + IO_DCOUNT_REG_OFFSET)
64 /**
65 * mux_tx_empty - Check if the transmitter fifo is empty.
66 * @port: Ptr to the uart_port.
68 * This function test if the transmitter fifo for the port
69 * described by 'port' is empty. If it is empty, this function
70 * should return TIOCSER_TEMT, otherwise return 0.
72 static unsigned int mux_tx_empty(struct uart_port *port)
74 unsigned int cnt = __raw_readl((unsigned long)port->membase
75 + IO_DCOUNT_REG_OFFSET);
77 return cnt ? 0 : TIOCSER_TEMT;
80 /**
81 * mux_set_mctrl - Set the current state of the modem control inputs.
82 * @ports: Ptr to the uart_port.
83 * @mctrl: Modem control bits.
85 * The Serial MUX does not support CTS, DCD or DSR so this function
86 * is ignored.
88 static void mux_set_mctrl(struct uart_port *port, unsigned int mctrl)
92 /**
93 * mux_get_mctrl - Returns the current state of modem control inputs.
94 * @port: Ptr to the uart_port.
96 * The Serial MUX does not support CTS, DCD or DSR so these lines are
97 * treated as permanently active.
99 static unsigned int mux_get_mctrl(struct uart_port *port)
101 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
105 * mux_stop_tx - Stop transmitting characters.
106 * @port: Ptr to the uart_port.
107 * @tty_stop: tty layer issue this command?
109 * The Serial MUX does not support this function.
111 static void mux_stop_tx(struct uart_port *port, unsigned int tty_stop)
116 * mux_start_tx - Start transmitting characters.
117 * @port: Ptr to the uart_port.
118 * @tty_start: tty layer issue this command?
120 * The Serial Mux does not support this function.
122 static void mux_start_tx(struct uart_port *port, unsigned int tty_start)
127 * mux_stop_rx - Stop receiving characters.
128 * @port: Ptr to the uart_port.
130 * The Serial Mux does not support this function.
132 static void mux_stop_rx(struct uart_port *port)
137 * mux_enable_ms - Enable modum status interrupts.
138 * @port: Ptr to the uart_port.
140 * The Serial Mux does not support this function.
142 static void mux_enable_ms(struct uart_port *port)
147 * mux_break_ctl - Control the transmitssion of a break signal.
148 * @port: Ptr to the uart_port.
149 * @break_state: Raise/Lower the break signal.
151 * The Serial Mux does not support this function.
153 static void mux_break_ctl(struct uart_port *port, int break_state)
158 * mux_write - Write chars to the mux fifo.
159 * @port: Ptr to the uart_port.
161 * This function writes all the data from the uart buffer to
162 * the mux fifo.
164 static void mux_write(struct uart_port *port)
166 int count;
167 struct circ_buf *xmit = &port->info->xmit;
169 if(port->x_char) {
170 UART_PUT_CHAR(port, port->x_char);
171 port->icount.tx++;
172 port->x_char = 0;
173 return;
176 if(uart_circ_empty(xmit) || uart_tx_stopped(port)) {
177 mux_stop_tx(port, 0);
178 return;
181 count = (port->fifosize >> 1) - UART_GET_FIFO_CNT(port);
182 do {
183 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
184 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
185 port->icount.tx++;
186 if(uart_circ_empty(xmit))
187 break;
189 } while(--count > 0);
191 if(uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
192 uart_write_wakeup(port);
194 if (uart_circ_empty(xmit))
195 mux_stop_tx(port, 0);
199 * mux_read - Read chars from the mux fifo.
200 * @port: Ptr to the uart_port.
202 * This reads all available data from the mux's fifo and pushes
203 * the data to the tty layer.
205 static void mux_read(struct uart_port *port)
207 int data;
208 __u32 start_count = port->icount.rx;
209 struct tty_struct *tty = port->info->tty;
211 while(1) {
212 data = __raw_readl((unsigned long)port->membase
213 + IO_DATA_REG_OFFSET);
215 if (MUX_STATUS(data))
216 continue;
218 if (MUX_EOFIFO(data))
219 break;
221 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
222 continue;
224 *tty->flip.char_buf_ptr = data & 0xffu;
225 *tty->flip.flag_buf_ptr = TTY_NORMAL;
226 port->icount.rx++;
228 if (MUX_BREAK(data)) {
229 port->icount.brk++;
230 if(uart_handle_break(port))
231 continue;
234 if (uart_handle_sysrq_char(port, data & 0xffu, NULL))
235 continue;
237 tty->flip.flag_buf_ptr++;
238 tty->flip.char_buf_ptr++;
239 tty->flip.count++;
242 if (start_count != port->icount.rx) {
243 tty_flip_buffer_push(tty);
248 * mux_startup - Initialize the port.
249 * @port: Ptr to the uart_port.
251 * Grab any resources needed for this port and start the
252 * mux timer.
254 static int mux_startup(struct uart_port *port)
256 mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
257 return 0;
261 * mux_shutdown - Disable the port.
262 * @port: Ptr to the uart_port.
264 * Release any resources needed for the port.
266 static void mux_shutdown(struct uart_port *port)
271 * mux_set_termios - Chane port parameters.
272 * @port: Ptr to the uart_port.
273 * @termios: new termios settings.
274 * @old: old termios settings.
276 * The Serial Mux does not support this function.
278 static void
279 mux_set_termios(struct uart_port *port, struct termios *termios,
280 struct termios *old)
285 * mux_type - Describe the port.
286 * @port: Ptr to the uart_port.
288 * Return a pointer to a string constant describing the
289 * specified port.
291 static const char *mux_type(struct uart_port *port)
293 return "Mux";
297 * release_port - Release memory and IO regions.
298 * @port: Ptr to the uart_port.
300 * Release any memory and IO region resources currently in use by
301 * the port.
303 static void mux_release_port(struct uart_port *port)
308 * mux_request_port - Request memory and IO regions.
309 * @port: Ptr to the uart_port.
311 * Request any memory and IO region resources required by the port.
312 * If any fail, no resources should be registered when this function
313 * returns, and it should return -EBUSY on failure.
315 static int mux_request_port(struct uart_port *port)
317 return 0;
321 * mux_config_port - Perform port autoconfiguration.
322 * @port: Ptr to the uart_port.
323 * @type: Bitmask of required configurations.
325 * Perform any autoconfiguration steps for the port. This functino is
326 * called if the UPF_BOOT_AUTOCONF flag is specified for the port.
327 * [Note: This is required for now because of a bug in the Serial core.
328 * rmk has already submitted a patch to linus, should be available for
329 * 2.5.47.]
331 static void mux_config_port(struct uart_port *port, int type)
333 port->type = PORT_MUX;
337 * mux_verify_port - Verify the port information.
338 * @port: Ptr to the uart_port.
339 * @ser: Ptr to the serial information.
341 * Verify the new serial port information contained within serinfo is
342 * suitable for this port type.
344 static int mux_verify_port(struct uart_port *port, struct serial_struct *ser)
346 if(port->membase == NULL)
347 return -EINVAL;
349 return 0;
353 * mux_drv_poll - Mux poll function.
354 * @unused: Unused variable
356 * This function periodically polls the Serial MUX to check for new data.
358 static void mux_poll(unsigned long unused)
360 struct uart_port *port = &mux_ports[0];
362 mux_read(port);
363 mux_write(port);
364 mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
368 #ifdef CONFIG_SERIAL_MUX_CONSOLE
369 static struct console mux_console = {
370 .name = "ttyB",
371 .flags = CON_PRINTBUFFER,
372 .index = 0,
374 #define MUX_CONSOLE &mux_console
375 #else
376 #define MUX_CONSOLE NULL
377 #endif
379 static struct uart_ops mux_pops = {
380 .tx_empty = mux_tx_empty,
381 .set_mctrl = mux_set_mctrl,
382 .get_mctrl = mux_get_mctrl,
383 .stop_tx = mux_stop_tx,
384 .start_tx = mux_start_tx,
385 .stop_rx = mux_stop_rx,
386 .enable_ms = mux_enable_ms,
387 .break_ctl = mux_break_ctl,
388 .startup = mux_startup,
389 .shutdown = mux_shutdown,
390 .set_termios = mux_set_termios,
391 .type = mux_type,
392 .release_port = mux_release_port,
393 .request_port = mux_request_port,
394 .config_port = mux_config_port,
395 .verify_port = mux_verify_port,
398 static struct uart_driver mux_reg = {
399 .owner = THIS_MODULE,
400 .driver_name = "ttyB",
401 #ifdef CONFIG_DEVFS_FS
402 .dev_name = "ttyB%d",
403 #else
404 .dev_name = "ttyB%d",
405 #endif
406 .major = MUX_MAJOR,
407 .minor = 0,
408 .nr = MUX_NR,
409 .cons = MUX_CONSOLE,
413 * mux_probe - Determine if the Serial Mux should claim this device.
414 * @dev: The parisc device.
416 * Deterimine if the Serial Mux should claim this chip (return 0)
417 * or not (return 1).
419 static int __init mux_probe(struct parisc_device *dev)
421 int i, ret;
422 struct uart_port *port = &mux_ports[0];
424 init_timer(&mux_timer);
425 mux_timer.function = mux_poll;
427 printk(KERN_INFO "Serial mux driver Revision: 0.1\n");
429 ret = uart_register_driver(&mux_reg);
430 if (ret)
431 return ret;
433 for (i = 0; i < MUX_NR; i++) {
434 port = &mux_ports[i];
436 port->iobase = 0;
437 port->mapbase = dev->hpa + MUX_OFFSET + (i * MUX_LINE_OFFSET);
438 port->membase = ioremap(port->mapbase, MUX_LINE_OFFSET);
439 port->iotype = SERIAL_IO_MEM;
440 port->type = PORT_MUX;
441 port->irq = SERIAL_IRQ_NONE;
442 port->uartclk = 0;
443 port->fifosize = MUX_FIFO_SIZE;
444 port->ops = &mux_pops;
445 port->flags = UPF_BOOT_AUTOCONF;
446 port->line = 0;
448 uart_add_one_port(&mux_reg, port);
451 return 0;
454 static struct parisc_device_id mux_tbl[] = {
455 { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0000D },
456 { 0, }
459 MODULE_DEVICE_TABLE(parisc, mux_tbl);
461 static struct parisc_driver mux_driver = {
462 .name = "Serial MUX driver",
463 .id_table = mux_tbl,
464 .probe = mux_probe,
468 * mux_init - Serial MUX initalization procedure.
470 * Register the Serial MUX driver.
472 static int __init mux_init(void)
474 return register_parisc_driver(&mux_driver);
478 * mux_exit - Serial MUX cleanup procedure.
480 * Unregister the Serial MUX driver from the tty layer.
482 static void __exit mux_exit(void)
484 int i;
486 for (i = 0; i < MUX_NR; i++) {
487 uart_remove_one_port(&mux_reg, &mux_ports[i]);
490 uart_unregister_driver(&mux_reg);
493 module_init(mux_init);
494 module_exit(mux_exit);
496 MODULE_AUTHOR("Ryan Bradetich");
497 MODULE_DESCRIPTION("Serial MUX driver");
498 MODULE_LICENSE("GPL");