Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / serial / pnx8xxx_uart.c
blobd0e5a79ea635982a79cff1b8877a4ee71b12ef5f
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.info->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.info) {
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 struct tty_struct *tty = sport->port.info->tty;
185 unsigned int status, ch, flg;
187 status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) |
188 ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT));
189 while (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFIFO)) {
190 ch = serial_in(sport, PNX8XXX_FIFO);
192 sport->port.icount.rx++;
194 flg = TTY_NORMAL;
197 * note that the error handling code is
198 * out of the main execution path
200 if (status & (FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE |
201 PNX8XXX_UART_FIFO_RXPAR) |
202 ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN))) {
203 if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR))
204 sport->port.icount.parity++;
205 else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE))
206 sport->port.icount.frame++;
207 if (status & ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN))
208 sport->port.icount.overrun++;
210 status &= sport->port.read_status_mask;
212 if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR))
213 flg = TTY_PARITY;
214 else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE))
215 flg = TTY_FRAME;
217 #ifdef SUPPORT_SYSRQ
218 sport->port.sysrq = 0;
219 #endif
222 if (uart_handle_sysrq_char(&sport->port, ch))
223 goto ignore_char;
225 uart_insert_char(&sport->port, status,
226 ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN), ch, flg);
228 ignore_char:
229 serial_out(sport, PNX8XXX_LCR, serial_in(sport, PNX8XXX_LCR) |
230 PNX8XXX_UART_LCR_RX_NEXT);
231 status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) |
232 ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT));
234 tty_flip_buffer_push(tty);
237 static void pnx8xxx_tx_chars(struct pnx8xxx_port *sport)
239 struct circ_buf *xmit = &sport->port.info->xmit;
241 if (sport->port.x_char) {
242 serial_out(sport, PNX8XXX_FIFO, sport->port.x_char);
243 sport->port.icount.tx++;
244 sport->port.x_char = 0;
245 return;
249 * Check the modem control lines before
250 * transmitting anything.
252 pnx8xxx_mctrl_check(sport);
254 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
255 pnx8xxx_stop_tx(&sport->port);
256 return;
260 * TX while bytes available
262 while (((serial_in(sport, PNX8XXX_FIFO) &
263 PNX8XXX_UART_FIFO_TXFIFO) >> 16) < 16) {
264 serial_out(sport, PNX8XXX_FIFO, xmit->buf[xmit->tail]);
265 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
266 sport->port.icount.tx++;
267 if (uart_circ_empty(xmit))
268 break;
271 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
272 uart_write_wakeup(&sport->port);
274 if (uart_circ_empty(xmit))
275 pnx8xxx_stop_tx(&sport->port);
278 static irqreturn_t pnx8xxx_int(int irq, void *dev_id)
280 struct pnx8xxx_port *sport = dev_id;
281 unsigned int status;
283 spin_lock(&sport->port.lock);
284 /* Get the interrupts */
285 status = serial_in(sport, PNX8XXX_ISTAT) & serial_in(sport, PNX8XXX_IEN);
287 /* Break signal received */
288 if (status & PNX8XXX_UART_INT_BREAK) {
289 sport->port.icount.brk++;
290 uart_handle_break(&sport->port);
293 /* Byte received */
294 if (status & PNX8XXX_UART_INT_RX)
295 pnx8xxx_rx_chars(sport);
297 /* TX holding register empty - transmit a byte */
298 if (status & PNX8XXX_UART_INT_TX)
299 pnx8xxx_tx_chars(sport);
301 /* Clear the ISTAT register */
302 serial_out(sport, PNX8XXX_ICLR, status);
304 spin_unlock(&sport->port.lock);
305 return IRQ_HANDLED;
309 * Return TIOCSER_TEMT when transmitter is not busy.
311 static unsigned int pnx8xxx_tx_empty(struct uart_port *port)
313 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
315 return serial_in(sport, PNX8XXX_FIFO) & PNX8XXX_UART_FIFO_TXFIFO_STA ? 0 : TIOCSER_TEMT;
318 static unsigned int pnx8xxx_get_mctrl(struct uart_port *port)
320 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
321 unsigned int mctrl = TIOCM_DSR;
322 unsigned int msr;
324 /* REVISIT */
326 msr = serial_in(sport, PNX8XXX_MCR);
328 mctrl |= msr & PNX8XXX_UART_MCR_CTS ? TIOCM_CTS : 0;
329 mctrl |= msr & PNX8XXX_UART_MCR_DCD ? TIOCM_CAR : 0;
331 return mctrl;
334 static void pnx8xxx_set_mctrl(struct uart_port *port, unsigned int mctrl)
336 #if 0 /* FIXME */
337 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
338 unsigned int msr;
339 #endif
343 * Interrupts always disabled.
345 static void pnx8xxx_break_ctl(struct uart_port *port, int break_state)
347 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
348 unsigned long flags;
349 unsigned int lcr;
351 spin_lock_irqsave(&sport->port.lock, flags);
352 lcr = serial_in(sport, PNX8XXX_LCR);
353 if (break_state == -1)
354 lcr |= PNX8XXX_UART_LCR_TXBREAK;
355 else
356 lcr &= ~PNX8XXX_UART_LCR_TXBREAK;
357 serial_out(sport, PNX8XXX_LCR, lcr);
358 spin_unlock_irqrestore(&sport->port.lock, flags);
361 static int pnx8xxx_startup(struct uart_port *port)
363 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
364 int retval;
367 * Allocate the IRQ
369 retval = request_irq(sport->port.irq, pnx8xxx_int, 0,
370 "pnx8xxx-uart", sport);
371 if (retval)
372 return retval;
375 * Finally, clear and enable interrupts
378 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX |
379 PNX8XXX_UART_INT_ALLTX);
381 serial_out(sport, PNX8XXX_IEN, serial_in(sport, PNX8XXX_IEN) |
382 PNX8XXX_UART_INT_ALLRX |
383 PNX8XXX_UART_INT_ALLTX);
386 * Enable modem status interrupts
388 spin_lock_irq(&sport->port.lock);
389 pnx8xxx_enable_ms(&sport->port);
390 spin_unlock_irq(&sport->port.lock);
392 return 0;
395 static void pnx8xxx_shutdown(struct uart_port *port)
397 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
398 int lcr;
401 * Stop our timer.
403 del_timer_sync(&sport->timer);
406 * Disable all interrupts
408 serial_out(sport, PNX8XXX_IEN, 0);
411 * Reset the Tx and Rx FIFOS, disable the break condition
413 lcr = serial_in(sport, PNX8XXX_LCR);
414 lcr &= ~PNX8XXX_UART_LCR_TXBREAK;
415 lcr |= PNX8XXX_UART_LCR_TX_RST | PNX8XXX_UART_LCR_RX_RST;
416 serial_out(sport, PNX8XXX_LCR, lcr);
419 * Clear all interrupts
421 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX |
422 PNX8XXX_UART_INT_ALLTX);
425 * Free the interrupt
427 free_irq(sport->port.irq, sport);
430 static void
431 pnx8xxx_set_termios(struct uart_port *port, struct ktermios *termios,
432 struct ktermios *old)
434 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
435 unsigned long flags;
436 unsigned int lcr_fcr, old_ien, baud, quot;
437 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
440 * We only support CS7 and CS8.
442 while ((termios->c_cflag & CSIZE) != CS7 &&
443 (termios->c_cflag & CSIZE) != CS8) {
444 termios->c_cflag &= ~CSIZE;
445 termios->c_cflag |= old_csize;
446 old_csize = CS8;
449 if ((termios->c_cflag & CSIZE) == CS8)
450 lcr_fcr = PNX8XXX_UART_LCR_8BIT;
451 else
452 lcr_fcr = 0;
454 if (termios->c_cflag & CSTOPB)
455 lcr_fcr |= PNX8XXX_UART_LCR_2STOPB;
456 if (termios->c_cflag & PARENB) {
457 lcr_fcr |= PNX8XXX_UART_LCR_PAREN;
458 if (!(termios->c_cflag & PARODD))
459 lcr_fcr |= PNX8XXX_UART_LCR_PAREVN;
463 * Ask the core to calculate the divisor for us.
465 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
466 quot = uart_get_divisor(port, baud);
468 spin_lock_irqsave(&sport->port.lock, flags);
470 sport->port.read_status_mask = ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN) |
471 ISTAT_TO_SM(PNX8XXX_UART_INT_EMPTY) |
472 ISTAT_TO_SM(PNX8XXX_UART_INT_RX);
473 if (termios->c_iflag & INPCK)
474 sport->port.read_status_mask |=
475 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
476 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR);
477 if (termios->c_iflag & (BRKINT | PARMRK))
478 sport->port.read_status_mask |=
479 ISTAT_TO_SM(PNX8XXX_UART_INT_BREAK);
482 * Characters to ignore
484 sport->port.ignore_status_mask = 0;
485 if (termios->c_iflag & IGNPAR)
486 sport->port.ignore_status_mask |=
487 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
488 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR);
489 if (termios->c_iflag & IGNBRK) {
490 sport->port.ignore_status_mask |=
491 ISTAT_TO_SM(PNX8XXX_UART_INT_BREAK);
493 * If we're ignoring parity and break indicators,
494 * ignore overruns too (for real raw support).
496 if (termios->c_iflag & IGNPAR)
497 sport->port.ignore_status_mask |=
498 ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN);
502 * ignore all characters if CREAD is not set
504 if ((termios->c_cflag & CREAD) == 0)
505 sport->port.ignore_status_mask |=
506 ISTAT_TO_SM(PNX8XXX_UART_INT_RX);
508 del_timer_sync(&sport->timer);
511 * Update the per-port timeout.
513 uart_update_timeout(port, termios->c_cflag, baud);
516 * disable interrupts and drain transmitter
518 old_ien = serial_in(sport, PNX8XXX_IEN);
519 serial_out(sport, PNX8XXX_IEN, old_ien & ~(PNX8XXX_UART_INT_ALLTX |
520 PNX8XXX_UART_INT_ALLRX));
522 while (serial_in(sport, PNX8XXX_FIFO) & PNX8XXX_UART_FIFO_TXFIFO_STA)
523 barrier();
525 /* then, disable everything */
526 serial_out(sport, PNX8XXX_IEN, 0);
528 /* Reset the Rx and Tx FIFOs too */
529 lcr_fcr |= PNX8XXX_UART_LCR_TX_RST;
530 lcr_fcr |= PNX8XXX_UART_LCR_RX_RST;
532 /* set the parity, stop bits and data size */
533 serial_out(sport, PNX8XXX_LCR, lcr_fcr);
535 /* set the baud rate */
536 quot -= 1;
537 serial_out(sport, PNX8XXX_BAUD, quot);
539 serial_out(sport, PNX8XXX_ICLR, -1);
541 serial_out(sport, PNX8XXX_IEN, old_ien);
543 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
544 pnx8xxx_enable_ms(&sport->port);
546 spin_unlock_irqrestore(&sport->port.lock, flags);
549 static const char *pnx8xxx_type(struct uart_port *port)
551 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
553 return sport->port.type == PORT_PNX8XXX ? "PNX8XXX" : NULL;
557 * Release the memory region(s) being used by 'port'.
559 static void pnx8xxx_release_port(struct uart_port *port)
561 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
563 release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
567 * Request the memory region(s) being used by 'port'.
569 static int pnx8xxx_request_port(struct uart_port *port)
571 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
572 return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
573 "pnx8xxx-uart") != NULL ? 0 : -EBUSY;
577 * Configure/autoconfigure the port.
579 static void pnx8xxx_config_port(struct uart_port *port, int flags)
581 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
583 if (flags & UART_CONFIG_TYPE &&
584 pnx8xxx_request_port(&sport->port) == 0)
585 sport->port.type = PORT_PNX8XXX;
589 * Verify the new serial_struct (for TIOCSSERIAL).
590 * The only change we allow are to the flags and type, and
591 * even then only between PORT_PNX8XXX and PORT_UNKNOWN
593 static int
594 pnx8xxx_verify_port(struct uart_port *port, struct serial_struct *ser)
596 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
597 int ret = 0;
599 if (ser->type != PORT_UNKNOWN && ser->type != PORT_PNX8XXX)
600 ret = -EINVAL;
601 if (sport->port.irq != ser->irq)
602 ret = -EINVAL;
603 if (ser->io_type != SERIAL_IO_MEM)
604 ret = -EINVAL;
605 if (sport->port.uartclk / 16 != ser->baud_base)
606 ret = -EINVAL;
607 if ((void *)sport->port.mapbase != ser->iomem_base)
608 ret = -EINVAL;
609 if (sport->port.iobase != ser->port)
610 ret = -EINVAL;
611 if (ser->hub6 != 0)
612 ret = -EINVAL;
613 return ret;
616 static struct uart_ops pnx8xxx_pops = {
617 .tx_empty = pnx8xxx_tx_empty,
618 .set_mctrl = pnx8xxx_set_mctrl,
619 .get_mctrl = pnx8xxx_get_mctrl,
620 .stop_tx = pnx8xxx_stop_tx,
621 .start_tx = pnx8xxx_start_tx,
622 .stop_rx = pnx8xxx_stop_rx,
623 .enable_ms = pnx8xxx_enable_ms,
624 .break_ctl = pnx8xxx_break_ctl,
625 .startup = pnx8xxx_startup,
626 .shutdown = pnx8xxx_shutdown,
627 .set_termios = pnx8xxx_set_termios,
628 .type = pnx8xxx_type,
629 .release_port = pnx8xxx_release_port,
630 .request_port = pnx8xxx_request_port,
631 .config_port = pnx8xxx_config_port,
632 .verify_port = pnx8xxx_verify_port,
637 * Setup the PNX8XXX serial ports.
639 * Note also that we support "console=ttySx" where "x" is either 0 or 1.
641 static void __init pnx8xxx_init_ports(void)
643 static int first = 1;
644 int i;
646 if (!first)
647 return;
648 first = 0;
650 for (i = 0; i < NR_PORTS; i++) {
651 init_timer(&pnx8xxx_ports[i].timer);
652 pnx8xxx_ports[i].timer.function = pnx8xxx_timeout;
653 pnx8xxx_ports[i].timer.data = (unsigned long)&pnx8xxx_ports[i];
654 pnx8xxx_ports[i].port.ops = &pnx8xxx_pops;
658 #ifdef CONFIG_SERIAL_PNX8XXX_CONSOLE
660 static void pnx8xxx_console_putchar(struct uart_port *port, int ch)
662 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
663 int status;
665 do {
666 /* Wait for UART_TX register to empty */
667 status = serial_in(sport, PNX8XXX_FIFO);
668 } while (status & PNX8XXX_UART_FIFO_TXFIFO);
669 serial_out(sport, PNX8XXX_FIFO, ch);
673 * Interrupts are disabled on entering
674 */static void
675 pnx8xxx_console_write(struct console *co, const char *s, unsigned int count)
677 struct pnx8xxx_port *sport = &pnx8xxx_ports[co->index];
678 unsigned int old_ien, status;
681 * First, save IEN and then disable interrupts
683 old_ien = serial_in(sport, PNX8XXX_IEN);
684 serial_out(sport, PNX8XXX_IEN, old_ien & ~(PNX8XXX_UART_INT_ALLTX |
685 PNX8XXX_UART_INT_ALLRX));
687 uart_console_write(&sport->port, s, count, pnx8xxx_console_putchar);
690 * Finally, wait for transmitter to become empty
691 * and restore IEN
693 do {
694 /* Wait for UART_TX register to empty */
695 status = serial_in(sport, PNX8XXX_FIFO);
696 } while (status & PNX8XXX_UART_FIFO_TXFIFO);
698 /* Clear TX and EMPTY interrupt */
699 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_TX |
700 PNX8XXX_UART_INT_EMPTY);
702 serial_out(sport, PNX8XXX_IEN, old_ien);
705 static int __init
706 pnx8xxx_console_setup(struct console *co, char *options)
708 struct pnx8xxx_port *sport;
709 int baud = 38400;
710 int bits = 8;
711 int parity = 'n';
712 int flow = 'n';
715 * Check whether an invalid uart number has been specified, and
716 * if so, search for the first available port that does have
717 * console support.
719 if (co->index == -1 || co->index >= NR_PORTS)
720 co->index = 0;
721 sport = &pnx8xxx_ports[co->index];
723 if (options)
724 uart_parse_options(options, &baud, &parity, &bits, &flow);
726 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
729 static struct uart_driver pnx8xxx_reg;
730 static struct console pnx8xxx_console = {
731 .name = "ttyS",
732 .write = pnx8xxx_console_write,
733 .device = uart_console_device,
734 .setup = pnx8xxx_console_setup,
735 .flags = CON_PRINTBUFFER,
736 .index = -1,
737 .data = &pnx8xxx_reg,
740 static int __init pnx8xxx_rs_console_init(void)
742 pnx8xxx_init_ports();
743 register_console(&pnx8xxx_console);
744 return 0;
746 console_initcall(pnx8xxx_rs_console_init);
748 #define PNX8XXX_CONSOLE &pnx8xxx_console
749 #else
750 #define PNX8XXX_CONSOLE NULL
751 #endif
753 static struct uart_driver pnx8xxx_reg = {
754 .owner = THIS_MODULE,
755 .driver_name = "ttyS",
756 .dev_name = "ttyS",
757 .major = SERIAL_PNX8XXX_MAJOR,
758 .minor = MINOR_START,
759 .nr = NR_PORTS,
760 .cons = PNX8XXX_CONSOLE,
763 static int pnx8xxx_serial_suspend(struct platform_device *pdev, pm_message_t state)
765 struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
767 return uart_suspend_port(&pnx8xxx_reg, &sport->port);
770 static int pnx8xxx_serial_resume(struct platform_device *pdev)
772 struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
774 return uart_resume_port(&pnx8xxx_reg, &sport->port);
777 static int pnx8xxx_serial_probe(struct platform_device *pdev)
779 struct resource *res = pdev->resource;
780 int i;
782 for (i = 0; i < pdev->num_resources; i++, res++) {
783 if (!(res->flags & IORESOURCE_MEM))
784 continue;
786 for (i = 0; i < NR_PORTS; i++) {
787 if (pnx8xxx_ports[i].port.mapbase != res->start)
788 continue;
790 pnx8xxx_ports[i].port.dev = &pdev->dev;
791 uart_add_one_port(&pnx8xxx_reg, &pnx8xxx_ports[i].port);
792 platform_set_drvdata(pdev, &pnx8xxx_ports[i]);
793 break;
797 return 0;
800 static int pnx8xxx_serial_remove(struct platform_device *pdev)
802 struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
804 platform_set_drvdata(pdev, NULL);
806 if (sport)
807 uart_remove_one_port(&pnx8xxx_reg, &sport->port);
809 return 0;
812 static struct platform_driver pnx8xxx_serial_driver = {
813 .driver = {
814 .name = "pnx8xxx-uart",
815 .owner = THIS_MODULE,
817 .probe = pnx8xxx_serial_probe,
818 .remove = pnx8xxx_serial_remove,
819 .suspend = pnx8xxx_serial_suspend,
820 .resume = pnx8xxx_serial_resume,
823 static int __init pnx8xxx_serial_init(void)
825 int ret;
827 printk(KERN_INFO "Serial: PNX8XXX driver $Revision: 1.2 $\n");
829 pnx8xxx_init_ports();
831 ret = uart_register_driver(&pnx8xxx_reg);
832 if (ret == 0) {
833 ret = platform_driver_register(&pnx8xxx_serial_driver);
834 if (ret)
835 uart_unregister_driver(&pnx8xxx_reg);
837 return ret;
840 static void __exit pnx8xxx_serial_exit(void)
842 platform_driver_unregister(&pnx8xxx_serial_driver);
843 uart_unregister_driver(&pnx8xxx_reg);
846 module_init(pnx8xxx_serial_init);
847 module_exit(pnx8xxx_serial_exit);
849 MODULE_AUTHOR("Embedded Alley Solutions, Inc.");
850 MODULE_DESCRIPTION("PNX8XXX SoCs serial port driver");
851 MODULE_LICENSE("GPL");
852 MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_PNX8XXX_MAJOR);
853 MODULE_ALIAS("platform:pnx8xxx-uart");