IB/srp: Add multichannel support
[linux/fpc-iii.git] / drivers / tty / serial / pnx8xxx_uart.c
blob2ba24a45c97fb86cbf2c0fd7cd8c8a38dd6436d9
1 /*
2 * UART driver for PNX8XXX SoCs
4 * Author: Per Hallsmark per.hallsmark@mvista.com
5 * Ported to 2.6 kernel by EmbeddedAlley
6 * Reworked by Vitaly Wool <vitalywool@gmail.com>
8 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
9 * Copyright (C) 2000 Deep Blue Solutions Ltd.
11 * This file is licensed under the terms of the GNU General Public License
12 * version 2. This program is licensed "as is" without any warranty of
13 * any kind, whether express or implied.
17 #if defined(CONFIG_SERIAL_PNX8XXX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
18 #define SUPPORT_SYSRQ
19 #endif
21 #include <linux/module.h>
22 #include <linux/ioport.h>
23 #include <linux/init.h>
24 #include <linux/console.h>
25 #include <linux/sysrq.h>
26 #include <linux/device.h>
27 #include <linux/platform_device.h>
28 #include <linux/tty.h>
29 #include <linux/tty_flip.h>
30 #include <linux/serial_core.h>
31 #include <linux/serial.h>
32 #include <linux/serial_pnx8xxx.h>
34 #include <asm/io.h>
35 #include <asm/irq.h>
37 /* We'll be using StrongARM sa1100 serial port major/minor */
38 #define SERIAL_PNX8XXX_MAJOR 204
39 #define MINOR_START 5
41 #define NR_PORTS 2
43 #define PNX8XXX_ISR_PASS_LIMIT 256
46 * Convert from ignore_status_mask or read_status_mask to FIFO
47 * and interrupt status bits
49 #define SM_TO_FIFO(x) ((x) >> 10)
50 #define SM_TO_ISTAT(x) ((x) & 0x000001ff)
51 #define FIFO_TO_SM(x) ((x) << 10)
52 #define ISTAT_TO_SM(x) ((x) & 0x000001ff)
55 * This is the size of our serial port register set.
57 #define UART_PORT_SIZE 0x1000
60 * This determines how often we check the modem status signals
61 * for any change. They generally aren't connected to an IRQ
62 * so we have to poll them. We also check immediately before
63 * filling the TX fifo incase CTS has been dropped.
65 #define MCTRL_TIMEOUT (250*HZ/1000)
67 extern struct pnx8xxx_port pnx8xxx_ports[];
69 static inline int serial_in(struct pnx8xxx_port *sport, int offset)
71 return (__raw_readl(sport->port.membase + offset));
74 static inline void serial_out(struct pnx8xxx_port *sport, int offset, int value)
76 __raw_writel(value, sport->port.membase + offset);
80 * Handle any change of modem status signal since we were last called.
82 static void pnx8xxx_mctrl_check(struct pnx8xxx_port *sport)
84 unsigned int status, changed;
86 status = sport->port.ops->get_mctrl(&sport->port);
87 changed = status ^ sport->old_status;
89 if (changed == 0)
90 return;
92 sport->old_status = status;
94 if (changed & TIOCM_RI)
95 sport->port.icount.rng++;
96 if (changed & TIOCM_DSR)
97 sport->port.icount.dsr++;
98 if (changed & TIOCM_CAR)
99 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
100 if (changed & TIOCM_CTS)
101 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
103 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
107 * This is our per-port timeout handler, for checking the
108 * modem status signals.
110 static void pnx8xxx_timeout(unsigned long data)
112 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)data;
113 unsigned long flags;
115 if (sport->port.state) {
116 spin_lock_irqsave(&sport->port.lock, flags);
117 pnx8xxx_mctrl_check(sport);
118 spin_unlock_irqrestore(&sport->port.lock, flags);
120 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
125 * interrupts disabled on entry
127 static void pnx8xxx_stop_tx(struct uart_port *port)
129 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
130 u32 ien;
132 /* Disable TX intr */
133 ien = serial_in(sport, PNX8XXX_IEN);
134 serial_out(sport, PNX8XXX_IEN, ien & ~PNX8XXX_UART_INT_ALLTX);
136 /* Clear all pending TX intr */
137 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLTX);
141 * interrupts may not be disabled on entry
143 static void pnx8xxx_start_tx(struct uart_port *port)
145 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
146 u32 ien;
148 /* Clear all pending TX intr */
149 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLTX);
151 /* Enable TX intr */
152 ien = serial_in(sport, PNX8XXX_IEN);
153 serial_out(sport, PNX8XXX_IEN, ien | PNX8XXX_UART_INT_ALLTX);
157 * Interrupts enabled
159 static void pnx8xxx_stop_rx(struct uart_port *port)
161 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
162 u32 ien;
164 /* Disable RX intr */
165 ien = serial_in(sport, PNX8XXX_IEN);
166 serial_out(sport, PNX8XXX_IEN, ien & ~PNX8XXX_UART_INT_ALLRX);
168 /* Clear all pending RX intr */
169 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX);
173 * Set the modem control timer to fire immediately.
175 static void pnx8xxx_enable_ms(struct uart_port *port)
177 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
179 mod_timer(&sport->timer, jiffies);
182 static void pnx8xxx_rx_chars(struct pnx8xxx_port *sport)
184 unsigned int status, ch, flg;
186 status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) |
187 ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT));
188 while (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFIFO)) {
189 ch = serial_in(sport, PNX8XXX_FIFO) & 0xff;
191 sport->port.icount.rx++;
193 flg = TTY_NORMAL;
196 * note that the error handling code is
197 * out of the main execution path
199 if (status & (FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE |
200 PNX8XXX_UART_FIFO_RXPAR |
201 PNX8XXX_UART_FIFO_RXBRK) |
202 ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN))) {
203 if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXBRK)) {
204 status &= ~(FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
205 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR));
206 sport->port.icount.brk++;
207 if (uart_handle_break(&sport->port))
208 goto ignore_char;
209 } else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR))
210 sport->port.icount.parity++;
211 else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE))
212 sport->port.icount.frame++;
213 if (status & ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN))
214 sport->port.icount.overrun++;
216 status &= sport->port.read_status_mask;
218 if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR))
219 flg = TTY_PARITY;
220 else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE))
221 flg = TTY_FRAME;
223 #ifdef SUPPORT_SYSRQ
224 sport->port.sysrq = 0;
225 #endif
228 if (uart_handle_sysrq_char(&sport->port, ch))
229 goto ignore_char;
231 uart_insert_char(&sport->port, status,
232 ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN), ch, flg);
234 ignore_char:
235 serial_out(sport, PNX8XXX_LCR, serial_in(sport, PNX8XXX_LCR) |
236 PNX8XXX_UART_LCR_RX_NEXT);
237 status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) |
238 ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT));
241 spin_unlock(&sport->port.lock);
242 tty_flip_buffer_push(&sport->port.state->port);
243 spin_lock(&sport->port.lock);
246 static void pnx8xxx_tx_chars(struct pnx8xxx_port *sport)
248 struct circ_buf *xmit = &sport->port.state->xmit;
250 if (sport->port.x_char) {
251 serial_out(sport, PNX8XXX_FIFO, sport->port.x_char);
252 sport->port.icount.tx++;
253 sport->port.x_char = 0;
254 return;
258 * Check the modem control lines before
259 * transmitting anything.
261 pnx8xxx_mctrl_check(sport);
263 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
264 pnx8xxx_stop_tx(&sport->port);
265 return;
269 * TX while bytes available
271 while (((serial_in(sport, PNX8XXX_FIFO) &
272 PNX8XXX_UART_FIFO_TXFIFO) >> 16) < 16) {
273 serial_out(sport, PNX8XXX_FIFO, xmit->buf[xmit->tail]);
274 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
275 sport->port.icount.tx++;
276 if (uart_circ_empty(xmit))
277 break;
280 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
281 uart_write_wakeup(&sport->port);
283 if (uart_circ_empty(xmit))
284 pnx8xxx_stop_tx(&sport->port);
287 static irqreturn_t pnx8xxx_int(int irq, void *dev_id)
289 struct pnx8xxx_port *sport = dev_id;
290 unsigned int status;
292 spin_lock(&sport->port.lock);
293 /* Get the interrupts */
294 status = serial_in(sport, PNX8XXX_ISTAT) & serial_in(sport, PNX8XXX_IEN);
296 /* Byte or break signal received */
297 if (status & (PNX8XXX_UART_INT_RX | PNX8XXX_UART_INT_BREAK))
298 pnx8xxx_rx_chars(sport);
300 /* TX holding register empty - transmit a byte */
301 if (status & PNX8XXX_UART_INT_TX)
302 pnx8xxx_tx_chars(sport);
304 /* Clear the ISTAT register */
305 serial_out(sport, PNX8XXX_ICLR, status);
307 spin_unlock(&sport->port.lock);
308 return IRQ_HANDLED;
312 * Return TIOCSER_TEMT when transmitter is not busy.
314 static unsigned int pnx8xxx_tx_empty(struct uart_port *port)
316 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
318 return serial_in(sport, PNX8XXX_FIFO) & PNX8XXX_UART_FIFO_TXFIFO_STA ? 0 : TIOCSER_TEMT;
321 static unsigned int pnx8xxx_get_mctrl(struct uart_port *port)
323 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
324 unsigned int mctrl = TIOCM_DSR;
325 unsigned int msr;
327 /* REVISIT */
329 msr = serial_in(sport, PNX8XXX_MCR);
331 mctrl |= msr & PNX8XXX_UART_MCR_CTS ? TIOCM_CTS : 0;
332 mctrl |= msr & PNX8XXX_UART_MCR_DCD ? TIOCM_CAR : 0;
334 return mctrl;
337 static void pnx8xxx_set_mctrl(struct uart_port *port, unsigned int mctrl)
339 #if 0 /* FIXME */
340 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
341 unsigned int msr;
342 #endif
346 * Interrupts always disabled.
348 static void pnx8xxx_break_ctl(struct uart_port *port, int break_state)
350 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
351 unsigned long flags;
352 unsigned int lcr;
354 spin_lock_irqsave(&sport->port.lock, flags);
355 lcr = serial_in(sport, PNX8XXX_LCR);
356 if (break_state == -1)
357 lcr |= PNX8XXX_UART_LCR_TXBREAK;
358 else
359 lcr &= ~PNX8XXX_UART_LCR_TXBREAK;
360 serial_out(sport, PNX8XXX_LCR, lcr);
361 spin_unlock_irqrestore(&sport->port.lock, flags);
364 static int pnx8xxx_startup(struct uart_port *port)
366 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
367 int retval;
370 * Allocate the IRQ
372 retval = request_irq(sport->port.irq, pnx8xxx_int, 0,
373 "pnx8xxx-uart", sport);
374 if (retval)
375 return retval;
378 * Finally, clear and enable interrupts
381 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX |
382 PNX8XXX_UART_INT_ALLTX);
384 serial_out(sport, PNX8XXX_IEN, serial_in(sport, PNX8XXX_IEN) |
385 PNX8XXX_UART_INT_ALLRX |
386 PNX8XXX_UART_INT_ALLTX);
389 * Enable modem status interrupts
391 spin_lock_irq(&sport->port.lock);
392 pnx8xxx_enable_ms(&sport->port);
393 spin_unlock_irq(&sport->port.lock);
395 return 0;
398 static void pnx8xxx_shutdown(struct uart_port *port)
400 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
401 int lcr;
404 * Stop our timer.
406 del_timer_sync(&sport->timer);
409 * Disable all interrupts
411 serial_out(sport, PNX8XXX_IEN, 0);
414 * Reset the Tx and Rx FIFOS, disable the break condition
416 lcr = serial_in(sport, PNX8XXX_LCR);
417 lcr &= ~PNX8XXX_UART_LCR_TXBREAK;
418 lcr |= PNX8XXX_UART_LCR_TX_RST | PNX8XXX_UART_LCR_RX_RST;
419 serial_out(sport, PNX8XXX_LCR, lcr);
422 * Clear all interrupts
424 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX |
425 PNX8XXX_UART_INT_ALLTX);
428 * Free the interrupt
430 free_irq(sport->port.irq, sport);
433 static void
434 pnx8xxx_set_termios(struct uart_port *port, struct ktermios *termios,
435 struct ktermios *old)
437 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
438 unsigned long flags;
439 unsigned int lcr_fcr, old_ien, baud, quot;
440 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
443 * We only support CS7 and CS8.
445 while ((termios->c_cflag & CSIZE) != CS7 &&
446 (termios->c_cflag & CSIZE) != CS8) {
447 termios->c_cflag &= ~CSIZE;
448 termios->c_cflag |= old_csize;
449 old_csize = CS8;
452 if ((termios->c_cflag & CSIZE) == CS8)
453 lcr_fcr = PNX8XXX_UART_LCR_8BIT;
454 else
455 lcr_fcr = 0;
457 if (termios->c_cflag & CSTOPB)
458 lcr_fcr |= PNX8XXX_UART_LCR_2STOPB;
459 if (termios->c_cflag & PARENB) {
460 lcr_fcr |= PNX8XXX_UART_LCR_PAREN;
461 if (!(termios->c_cflag & PARODD))
462 lcr_fcr |= PNX8XXX_UART_LCR_PAREVN;
466 * Ask the core to calculate the divisor for us.
468 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
469 quot = uart_get_divisor(port, baud);
471 spin_lock_irqsave(&sport->port.lock, flags);
473 sport->port.read_status_mask = ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN) |
474 ISTAT_TO_SM(PNX8XXX_UART_INT_EMPTY) |
475 ISTAT_TO_SM(PNX8XXX_UART_INT_RX);
476 if (termios->c_iflag & INPCK)
477 sport->port.read_status_mask |=
478 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
479 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR);
480 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
481 sport->port.read_status_mask |=
482 ISTAT_TO_SM(PNX8XXX_UART_INT_BREAK);
485 * Characters to ignore
487 sport->port.ignore_status_mask = 0;
488 if (termios->c_iflag & IGNPAR)
489 sport->port.ignore_status_mask |=
490 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
491 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR);
492 if (termios->c_iflag & IGNBRK) {
493 sport->port.ignore_status_mask |=
494 ISTAT_TO_SM(PNX8XXX_UART_INT_BREAK);
496 * If we're ignoring parity and break indicators,
497 * ignore overruns too (for real raw support).
499 if (termios->c_iflag & IGNPAR)
500 sport->port.ignore_status_mask |=
501 ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN);
505 * ignore all characters if CREAD is not set
507 if ((termios->c_cflag & CREAD) == 0)
508 sport->port.ignore_status_mask |=
509 ISTAT_TO_SM(PNX8XXX_UART_INT_RX);
511 del_timer_sync(&sport->timer);
514 * Update the per-port timeout.
516 uart_update_timeout(port, termios->c_cflag, baud);
519 * disable interrupts and drain transmitter
521 old_ien = serial_in(sport, PNX8XXX_IEN);
522 serial_out(sport, PNX8XXX_IEN, old_ien & ~(PNX8XXX_UART_INT_ALLTX |
523 PNX8XXX_UART_INT_ALLRX));
525 while (serial_in(sport, PNX8XXX_FIFO) & PNX8XXX_UART_FIFO_TXFIFO_STA)
526 barrier();
528 /* then, disable everything */
529 serial_out(sport, PNX8XXX_IEN, 0);
531 /* Reset the Rx and Tx FIFOs too */
532 lcr_fcr |= PNX8XXX_UART_LCR_TX_RST;
533 lcr_fcr |= PNX8XXX_UART_LCR_RX_RST;
535 /* set the parity, stop bits and data size */
536 serial_out(sport, PNX8XXX_LCR, lcr_fcr);
538 /* set the baud rate */
539 quot -= 1;
540 serial_out(sport, PNX8XXX_BAUD, quot);
542 serial_out(sport, PNX8XXX_ICLR, -1);
544 serial_out(sport, PNX8XXX_IEN, old_ien);
546 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
547 pnx8xxx_enable_ms(&sport->port);
549 spin_unlock_irqrestore(&sport->port.lock, flags);
552 static const char *pnx8xxx_type(struct uart_port *port)
554 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
556 return sport->port.type == PORT_PNX8XXX ? "PNX8XXX" : NULL;
560 * Release the memory region(s) being used by 'port'.
562 static void pnx8xxx_release_port(struct uart_port *port)
564 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
566 release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
570 * Request the memory region(s) being used by 'port'.
572 static int pnx8xxx_request_port(struct uart_port *port)
574 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
575 return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
576 "pnx8xxx-uart") != NULL ? 0 : -EBUSY;
580 * Configure/autoconfigure the port.
582 static void pnx8xxx_config_port(struct uart_port *port, int flags)
584 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
586 if (flags & UART_CONFIG_TYPE &&
587 pnx8xxx_request_port(&sport->port) == 0)
588 sport->port.type = PORT_PNX8XXX;
592 * Verify the new serial_struct (for TIOCSSERIAL).
593 * The only change we allow are to the flags and type, and
594 * even then only between PORT_PNX8XXX and PORT_UNKNOWN
596 static int
597 pnx8xxx_verify_port(struct uart_port *port, struct serial_struct *ser)
599 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
600 int ret = 0;
602 if (ser->type != PORT_UNKNOWN && ser->type != PORT_PNX8XXX)
603 ret = -EINVAL;
604 if (sport->port.irq != ser->irq)
605 ret = -EINVAL;
606 if (ser->io_type != SERIAL_IO_MEM)
607 ret = -EINVAL;
608 if (sport->port.uartclk / 16 != ser->baud_base)
609 ret = -EINVAL;
610 if ((void *)sport->port.mapbase != ser->iomem_base)
611 ret = -EINVAL;
612 if (sport->port.iobase != ser->port)
613 ret = -EINVAL;
614 if (ser->hub6 != 0)
615 ret = -EINVAL;
616 return ret;
619 static struct uart_ops pnx8xxx_pops = {
620 .tx_empty = pnx8xxx_tx_empty,
621 .set_mctrl = pnx8xxx_set_mctrl,
622 .get_mctrl = pnx8xxx_get_mctrl,
623 .stop_tx = pnx8xxx_stop_tx,
624 .start_tx = pnx8xxx_start_tx,
625 .stop_rx = pnx8xxx_stop_rx,
626 .enable_ms = pnx8xxx_enable_ms,
627 .break_ctl = pnx8xxx_break_ctl,
628 .startup = pnx8xxx_startup,
629 .shutdown = pnx8xxx_shutdown,
630 .set_termios = pnx8xxx_set_termios,
631 .type = pnx8xxx_type,
632 .release_port = pnx8xxx_release_port,
633 .request_port = pnx8xxx_request_port,
634 .config_port = pnx8xxx_config_port,
635 .verify_port = pnx8xxx_verify_port,
640 * Setup the PNX8XXX serial ports.
642 * Note also that we support "console=ttySx" where "x" is either 0 or 1.
644 static void __init pnx8xxx_init_ports(void)
646 static int first = 1;
647 int i;
649 if (!first)
650 return;
651 first = 0;
653 for (i = 0; i < NR_PORTS; i++) {
654 init_timer(&pnx8xxx_ports[i].timer);
655 pnx8xxx_ports[i].timer.function = pnx8xxx_timeout;
656 pnx8xxx_ports[i].timer.data = (unsigned long)&pnx8xxx_ports[i];
657 pnx8xxx_ports[i].port.ops = &pnx8xxx_pops;
661 #ifdef CONFIG_SERIAL_PNX8XXX_CONSOLE
663 static void pnx8xxx_console_putchar(struct uart_port *port, int ch)
665 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
666 int status;
668 do {
669 /* Wait for UART_TX register to empty */
670 status = serial_in(sport, PNX8XXX_FIFO);
671 } while (status & PNX8XXX_UART_FIFO_TXFIFO);
672 serial_out(sport, PNX8XXX_FIFO, ch);
676 * Interrupts are disabled on entering
677 */static void
678 pnx8xxx_console_write(struct console *co, const char *s, unsigned int count)
680 struct pnx8xxx_port *sport = &pnx8xxx_ports[co->index];
681 unsigned int old_ien, status;
684 * First, save IEN and then disable interrupts
686 old_ien = serial_in(sport, PNX8XXX_IEN);
687 serial_out(sport, PNX8XXX_IEN, old_ien & ~(PNX8XXX_UART_INT_ALLTX |
688 PNX8XXX_UART_INT_ALLRX));
690 uart_console_write(&sport->port, s, count, pnx8xxx_console_putchar);
693 * Finally, wait for transmitter to become empty
694 * and restore IEN
696 do {
697 /* Wait for UART_TX register to empty */
698 status = serial_in(sport, PNX8XXX_FIFO);
699 } while (status & PNX8XXX_UART_FIFO_TXFIFO);
701 /* Clear TX and EMPTY interrupt */
702 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_TX |
703 PNX8XXX_UART_INT_EMPTY);
705 serial_out(sport, PNX8XXX_IEN, old_ien);
708 static int __init
709 pnx8xxx_console_setup(struct console *co, char *options)
711 struct pnx8xxx_port *sport;
712 int baud = 38400;
713 int bits = 8;
714 int parity = 'n';
715 int flow = 'n';
718 * Check whether an invalid uart number has been specified, and
719 * if so, search for the first available port that does have
720 * console support.
722 if (co->index == -1 || co->index >= NR_PORTS)
723 co->index = 0;
724 sport = &pnx8xxx_ports[co->index];
726 if (options)
727 uart_parse_options(options, &baud, &parity, &bits, &flow);
729 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
732 static struct uart_driver pnx8xxx_reg;
733 static struct console pnx8xxx_console = {
734 .name = "ttyS",
735 .write = pnx8xxx_console_write,
736 .device = uart_console_device,
737 .setup = pnx8xxx_console_setup,
738 .flags = CON_PRINTBUFFER,
739 .index = -1,
740 .data = &pnx8xxx_reg,
743 static int __init pnx8xxx_rs_console_init(void)
745 pnx8xxx_init_ports();
746 register_console(&pnx8xxx_console);
747 return 0;
749 console_initcall(pnx8xxx_rs_console_init);
751 #define PNX8XXX_CONSOLE &pnx8xxx_console
752 #else
753 #define PNX8XXX_CONSOLE NULL
754 #endif
756 static struct uart_driver pnx8xxx_reg = {
757 .owner = THIS_MODULE,
758 .driver_name = "ttyS",
759 .dev_name = "ttyS",
760 .major = SERIAL_PNX8XXX_MAJOR,
761 .minor = MINOR_START,
762 .nr = NR_PORTS,
763 .cons = PNX8XXX_CONSOLE,
766 static int pnx8xxx_serial_suspend(struct platform_device *pdev, pm_message_t state)
768 struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
770 return uart_suspend_port(&pnx8xxx_reg, &sport->port);
773 static int pnx8xxx_serial_resume(struct platform_device *pdev)
775 struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
777 return uart_resume_port(&pnx8xxx_reg, &sport->port);
780 static int pnx8xxx_serial_probe(struct platform_device *pdev)
782 struct resource *res = pdev->resource;
783 int i;
785 for (i = 0; i < pdev->num_resources; i++, res++) {
786 if (!(res->flags & IORESOURCE_MEM))
787 continue;
789 for (i = 0; i < NR_PORTS; i++) {
790 if (pnx8xxx_ports[i].port.mapbase != res->start)
791 continue;
793 pnx8xxx_ports[i].port.dev = &pdev->dev;
794 uart_add_one_port(&pnx8xxx_reg, &pnx8xxx_ports[i].port);
795 platform_set_drvdata(pdev, &pnx8xxx_ports[i]);
796 break;
800 return 0;
803 static int pnx8xxx_serial_remove(struct platform_device *pdev)
805 struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
807 if (sport)
808 uart_remove_one_port(&pnx8xxx_reg, &sport->port);
810 return 0;
813 static struct platform_driver pnx8xxx_serial_driver = {
814 .driver = {
815 .name = "pnx8xxx-uart",
816 .owner = THIS_MODULE,
818 .probe = pnx8xxx_serial_probe,
819 .remove = pnx8xxx_serial_remove,
820 .suspend = pnx8xxx_serial_suspend,
821 .resume = pnx8xxx_serial_resume,
824 static int __init pnx8xxx_serial_init(void)
826 int ret;
828 printk(KERN_INFO "Serial: PNX8XXX driver\n");
830 pnx8xxx_init_ports();
832 ret = uart_register_driver(&pnx8xxx_reg);
833 if (ret == 0) {
834 ret = platform_driver_register(&pnx8xxx_serial_driver);
835 if (ret)
836 uart_unregister_driver(&pnx8xxx_reg);
838 return ret;
841 static void __exit pnx8xxx_serial_exit(void)
843 platform_driver_unregister(&pnx8xxx_serial_driver);
844 uart_unregister_driver(&pnx8xxx_reg);
847 module_init(pnx8xxx_serial_init);
848 module_exit(pnx8xxx_serial_exit);
850 MODULE_AUTHOR("Embedded Alley Solutions, Inc.");
851 MODULE_DESCRIPTION("PNX8XXX SoCs serial port driver");
852 MODULE_LICENSE("GPL");
853 MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_PNX8XXX_MAJOR);
854 MODULE_ALIAS("platform:pnx8xxx-uart");