Merge master.kernel.org:/home/rmk/linux-2.6-serial
[pandora-kernel.git] / drivers / serial / mux.c
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.
12 **
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.
16 **
17 */
18
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>
31
32 #ifdef CONFIG_MAGIC_SYSRQ
33 #include <linux/sysrq.h>
34 #define SUPPORT_SYSRQ
35 #endif
36
37 #include <linux/serial_core.h>
38
39 #define MUX_OFFSET 0x800
40 #define MUX_LINE_OFFSET 0x80
41
42 #define MUX_FIFO_SIZE 255
43 #define MUX_POLL_DELAY (30 * HZ / 1000)
44
45 #define IO_DATA_REG_OFFSET 0x3c
46 #define IO_DCOUNT_REG_OFFSET 0x40
47
48 #define MUX_EOFIFO(status) ((status & 0xF000) == 0xF000)
49 #define MUX_STATUS(status) ((status & 0xF000) == 0x8000)
50 #define MUX_BREAK(status) ((status & 0xF000) == 0x2000)
51
52 #define MUX_NR 256
53 static unsigned int port_cnt = 0;
54 static struct uart_port mux_ports[MUX_NR];
55
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,
63 };
64
65 static struct timer_list mux_timer;
66
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
70
71 /**
72  * mux_tx_empty - Check if the transmitter fifo is empty.
73  * @port: Ptr to the uart_port.
74  *
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.
78  */
79 static unsigned int mux_tx_empty(struct uart_port *port)
80 {
81         unsigned int cnt = __raw_readl((unsigned long)port->membase 
82                                 + IO_DCOUNT_REG_OFFSET);
83
84         return cnt ? 0 : TIOCSER_TEMT;
85
86
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.
91  *
92  * The Serial MUX does not support CTS, DCD or DSR so this function
93  * is ignored.
94  */
95 static void mux_set_mctrl(struct uart_port *port, unsigned int mctrl)
96 {
97 }
98
99 /**
100  * mux_get_mctrl - Returns the current state of modem control inputs.
101  * @port: Ptr to the uart_port.
102  *
103  * The Serial MUX does not support CTS, DCD or DSR so these lines are
104  * treated as permanently active.
105  */
106 static unsigned int mux_get_mctrl(struct uart_port *port)
107
108         return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
109 }
110
111 /**
112  * mux_stop_tx - Stop transmitting characters.
113  * @port: Ptr to the uart_port.
114  *
115  * The Serial MUX does not support this function.
116  */
117 static void mux_stop_tx(struct uart_port *port)
118 {
119 }
120
121 /**
122  * mux_start_tx - Start transmitting characters.
123  * @port: Ptr to the uart_port.
124  *
125  * The Serial Mux does not support this function.
126  */
127 static void mux_start_tx(struct uart_port *port)
128 {
129 }
130
131 /**
132  * mux_stop_rx - Stop receiving characters.
133  * @port: Ptr to the uart_port.
134  *
135  * The Serial Mux does not support this function.
136  */
137 static void mux_stop_rx(struct uart_port *port)
138 {
139 }
140
141 /**
142  * mux_enable_ms - Enable modum status interrupts.
143  * @port: Ptr to the uart_port.
144  *
145  * The Serial Mux does not support this function.
146  */
147 static void mux_enable_ms(struct uart_port *port)
148 {
149 }
150
151 /**
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.
155  *
156  * The Serial Mux does not support this function.
157  */
158 static void mux_break_ctl(struct uart_port *port, int break_state)
159 {
160 }
161
162 /**
163  * mux_write - Write chars to the mux fifo.
164  * @port: Ptr to the uart_port.
165  *
166  * This function writes all the data from the uart buffer to
167  * the mux fifo.
168  */
169 static void mux_write(struct uart_port *port)
170 {
171         int count;
172         struct circ_buf *xmit = &port->info->xmit;
173
174         if(port->x_char) {
175                 UART_PUT_CHAR(port, port->x_char);
176                 port->icount.tx++;
177                 port->x_char = 0;
178                 return;
179         }
180
181         if(uart_circ_empty(xmit) || uart_tx_stopped(port)) {
182                 mux_stop_tx(port);
183                 return;
184         }
185
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;
193
194         } while(--count > 0);
195
196         while(UART_GET_FIFO_CNT(port)) 
197                 udelay(1);
198
199         if(uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
200                 uart_write_wakeup(port);
201
202         if (uart_circ_empty(xmit))
203                 mux_stop_tx(port);
204 }
205
206 /**
207  * mux_read - Read chars from the mux fifo.
208  * @port: Ptr to the uart_port.
209  *
210  * This reads all available data from the mux's fifo and pushes
211  * the data to the tty layer.
212  */
213 static void mux_read(struct uart_port *port)
214 {
215         int data;
216         struct tty_struct *tty = port->info->tty;
217         __u32 start_count = port->icount.rx;
218
219         while(1) {
220                 data = __raw_readl((unsigned long)port->membase
221                                                 + IO_DATA_REG_OFFSET);
222
223                 if (MUX_STATUS(data))
224                         continue;
225
226                 if (MUX_EOFIFO(data))
227                         break;
228
229                 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
230                         continue;
231
232                 *tty->flip.char_buf_ptr = data & 0xffu;
233                 *tty->flip.flag_buf_ptr = TTY_NORMAL;
234                 port->icount.rx++;
235
236                 if (MUX_BREAK(data)) {
237                         port->icount.brk++;
238                         if(uart_handle_break(port))
239                                 continue;
240                 }
241
242                 if (uart_handle_sysrq_char(port, data & 0xffu, NULL))
243                         continue;
244
245                 tty->flip.flag_buf_ptr++;
246                 tty->flip.char_buf_ptr++;
247                 tty->flip.count++;
248         }
249         
250         if (start_count != port->icount.rx) {
251                 tty_flip_buffer_push(tty);
252         }
253 }
254
255 /**
256  * mux_startup - Initialize the port.
257  * @port: Ptr to the uart_port.
258  *
259  * Grab any resources needed for this port and start the
260  * mux timer.
261  */
262 static int mux_startup(struct uart_port *port)
263 {
264         mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
265         return 0;
266 }
267
268 /**
269  * mux_shutdown - Disable the port.
270  * @port: Ptr to the uart_port.
271  *
272  * Release any resources needed for the port.
273  */
274 static void mux_shutdown(struct uart_port *port)
275 {
276 }
277
278 /**
279  * mux_set_termios - Chane port parameters.
280  * @port: Ptr to the uart_port.
281  * @termios: new termios settings.
282  * @old: old termios settings.
283  *
284  * The Serial Mux does not support this function.
285  */
286 static void
287 mux_set_termios(struct uart_port *port, struct termios *termios,
288                 struct termios *old)
289 {
290 }
291
292 /**
293  * mux_type - Describe the port.
294  * @port: Ptr to the uart_port.
295  *
296  * Return a pointer to a string constant describing the
297  * specified port.
298  */
299 static const char *mux_type(struct uart_port *port)
300 {
301         return "Mux";
302 }
303
304 /**
305  * mux_release_port - Release memory and IO regions.
306  * @port: Ptr to the uart_port.
307  * 
308  * Release any memory and IO region resources currently in use by
309  * the port.
310  */
311 static void mux_release_port(struct uart_port *port)
312 {
313 }
314
315 /**
316  * mux_request_port - Request memory and IO regions.
317  * @port: Ptr to the uart_port.
318  *
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.
322  */
323 static int mux_request_port(struct uart_port *port)
324 {
325         return 0;
326 }
327
328 /**
329  * mux_config_port - Perform port autoconfiguration.
330  * @port: Ptr to the uart_port.
331  * @type: Bitmask of required configurations.
332  *
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.]
338  */
339 static void mux_config_port(struct uart_port *port, int type)
340 {
341         port->type = PORT_MUX;
342 }
343
344 /**
345  * mux_verify_port - Verify the port information.
346  * @port: Ptr to the uart_port.
347  * @ser: Ptr to the serial information.
348  *
349  * Verify the new serial port information contained within serinfo is
350  * suitable for this port type.
351  */
352 static int mux_verify_port(struct uart_port *port, struct serial_struct *ser)
353 {
354         if(port->membase == NULL)
355                 return -EINVAL;
356
357         return 0;
358 }
359
360 /**
361  * mux_drv_poll - Mux poll function.
362  * @unused: Unused variable
363  *
364  * This function periodically polls the Serial MUX to check for new data.
365  */
366 static void mux_poll(unsigned long unused)
367 {  
368         int i;
369
370         for(i = 0; i < port_cnt; ++i) {
371                 if(!mux_ports[i].info)
372                         continue;
373
374                 mux_read(&mux_ports[i]);
375                 mux_write(&mux_ports[i]);
376         }
377
378         mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
379 }
380
381
382 #ifdef CONFIG_SERIAL_MUX_CONSOLE
383 static void mux_console_write(struct console *co, const char *s, unsigned count)
384 {
385         while(count--)
386                 pdc_iodc_putc(*s++);
387 }
388
389 static int mux_console_setup(struct console *co, char *options)
390 {
391         return 0;
392 }
393
394 struct tty_driver *mux_console_device(struct console *co, int *index)
395 {
396         *index = co->index;
397         return mux_driver.tty_driver;
398 }
399
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,
407 };
408
409 #define MUX_CONSOLE     &mux_console
410 #else
411 #define MUX_CONSOLE     NULL
412 #endif
413
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,
431 };
432
433 /**
434  * mux_probe - Determine if the Serial Mux should claim this device.
435  * @dev: The parisc device.
436  *
437  * Deterimine if the Serial Mux should claim this chip (return 0)
438  * or not (return 1).
439  */
440 static int __init mux_probe(struct parisc_device *dev)
441 {
442         int i, status, ports;
443         u8 iodc_data[32];
444         unsigned long bytecnt;
445         struct uart_port *port;
446
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;
451         }
452
453         ports = GET_MUX_PORTS(iodc_data);
454         printk(KERN_INFO "Serial mux driver (%d ports) Revision: 0.3\n", ports);
455
456         if(!port_cnt) {
457                 mux_driver.cons = MUX_CONSOLE;
458
459                 status = uart_register_driver(&mux_driver);
460                 if(status) {
461                         printk(KERN_ERR "Serial mux: Unable to register driver.\n");
462                         return 1;
463                 }
464
465                 init_timer(&mux_timer);
466                 mux_timer.function = mux_poll;
467         }
468
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);
484         }
485
486 #ifdef CONFIG_SERIAL_MUX_CONSOLE
487         register_console(&mux_console);
488 #endif
489         return 0;
490 }
491
492 static struct parisc_device_id mux_tbl[] = {
493         { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0000D },
494         { 0, }
495 };
496
497 MODULE_DEVICE_TABLE(parisc, mux_tbl);
498
499 static struct parisc_driver serial_mux_driver = {
500         .name =         "Serial MUX",
501         .id_table =     mux_tbl,
502         .probe =        mux_probe,
503 };
504
505 /**
506  * mux_init - Serial MUX initalization procedure.
507  *
508  * Register the Serial MUX driver.
509  */
510 static int __init mux_init(void)
511 {
512         return register_parisc_driver(&serial_mux_driver);
513 }
514
515 /**
516  * mux_exit - Serial MUX cleanup procedure.
517  *
518  * Unregister the Serial MUX driver from the tty layer.
519  */
520 static void __exit mux_exit(void)
521 {
522         int i;
523
524         for (i = 0; i < port_cnt; i++) {
525                 uart_remove_one_port(&mux_driver, &mux_ports[i]);
526         }
527
528         uart_unregister_driver(&mux_driver);
529 }
530
531 module_init(mux_init);
532 module_exit(mux_exit);
533
534 MODULE_AUTHOR("Ryan Bradetich");
535 MODULE_DESCRIPTION("Serial MUX driver");
536 MODULE_LICENSE("GPL");
537 MODULE_ALIAS_CHARDEV_MAJOR(MUX_MAJOR);