2 * Driver for 8250/16550-type serial ports
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 * Copyright (C) 2001 Russell King.
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 * A note about mapbase / membase
15 * mapbase is the physical address of the IO port. Currently, we don't
16 * support this very well, and it may well be dropped from this driver
17 * in future. As such, mapbase should be NULL.
19 * membase is an 'ioremapped' cookie. This is compatible with the old
20 * serial.c driver, and is currently the preferred form.
22 #include <linux/config.h>
23 #include <linux/module.h>
24 #include <linux/tty.h>
25 #include <linux/ioport.h>
26 #include <linux/init.h>
27 #include <linux/console.h>
28 #include <linux/sysrq.h>
29 #include <linux/serial.h>
30 #include <linux/serialP.h>
31 #include <linux/delay.h>
33 #include <asm/serial.h>
36 #include <asm/mach-au1x00/au1000.h>
38 #if defined(CONFIG_SERIAL_AU1X00_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
42 #include <linux/serial_core.h>
49 #define DEBUG_AUTOCONF(fmt...) printk(fmt)
51 #define DEBUG_AUTOCONF(fmt...) do { } while (0)
55 #define DEBUG_INTR(fmt...) printk(fmt)
57 #define DEBUG_INTR(fmt...) do { } while (0)
60 #define PASS_LIMIT 256
63 * We default to IRQ0 for the "no irq" hack. Some
64 * machine types want others as well - they're free
65 * to redefine this in their header file.
67 #define is_real_interrupt(irq) ((irq) != 0)
69 static struct old_serial_port old_serial_port
[] = {
71 .iomem_base
= (u8
*)UART0_ADDR
,
72 .irq
= AU1000_UART0_INT
,
73 .flags
= STD_COM_FLAGS
,
77 .iomem_base
= (u8
*)UART1_ADDR
,
78 .irq
= AU1000_UART1_INT
,
79 .flags
= STD_COM_FLAGS
,
83 .iomem_base
= (u8
*)UART2_ADDR
,
84 .irq
= AU1000_UART2_INT
,
85 .flags
= STD_COM_FLAGS
,
89 .iomem_base
= (u8
*)UART3_ADDR
,
90 .irq
= AU1000_UART3_INT
,
91 .flags
= STD_COM_FLAGS
,
96 #define UART_NR ARRAY_SIZE(old_serial_port)
98 struct uart_8250_port
{
99 struct uart_port port
;
100 struct timer_list timer
; /* "no irq" timer */
101 struct list_head list
; /* ports on this IRQ */
106 unsigned char mcr_mask
; /* mask of user bits */
107 unsigned char mcr_force
; /* mask of forced bits */
108 unsigned char lsr_break_flag
;
111 * We provide a per-port pm hook.
113 void (*pm
)(struct uart_port
*port
,
114 unsigned int state
, unsigned int old
);
119 struct list_head
*head
;
122 static struct irq_info irq_lists
[NR_IRQS
];
125 * Here we define the default xmit fifo size used for each type of UART.
127 static const struct serial_uart_config uart_config
[PORT_MAX_8250
+1] = {
133 { "AU1X00_UART",16, UART_CLEAR_FIFO
| UART_USE_FIFO
},
136 static _INLINE_
unsigned int serial_in(struct uart_8250_port
*up
, int offset
)
138 return au_readl((unsigned long)up
->port
.membase
+ offset
);
142 serial_out(struct uart_8250_port
*up
, int offset
, int value
)
144 au_writel(value
, (unsigned long)up
->port
.membase
+ offset
);
147 #define serial_inp(up, offset) serial_in(up, offset)
148 #define serial_outp(up, offset, value) serial_out(up, offset, value)
151 * This routine is called by rs_init() to initialize a specific serial
152 * port. It determines what type of UART chip this serial port is
153 * using: 8250, 16450, 16550, 16550A. The important question is
154 * whether or not this UART is a 16550A or not, since this will
155 * determine whether or not we can use its FIFO features or not.
157 static void autoconfig(struct uart_8250_port
*up
, unsigned int probeflags
)
159 unsigned char save_lcr
, save_mcr
;
162 if (!up
->port
.iobase
&& !up
->port
.mapbase
&& !up
->port
.membase
)
165 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%08lx): ",
166 up
->port
.line
, up
->port
.iobase
, up
->port
.membase
);
169 * We really do need global IRQs disabled here - we're going to
170 * be frobbing the chips IRQ enable register to see if it exists.
172 spin_lock_irqsave(&up
->port
.lock
, flags
);
173 // save_flags(flags); cli();
175 save_mcr
= serial_in(up
, UART_MCR
);
176 save_lcr
= serial_in(up
, UART_LCR
);
178 up
->port
.type
= PORT_16550A
;
179 serial_outp(up
, UART_LCR
, save_lcr
);
181 up
->port
.fifosize
= uart_config
[up
->port
.type
].dfl_xmit_fifo_size
;
183 if (up
->port
.type
== PORT_UNKNOWN
)
189 serial_outp(up
, UART_MCR
, save_mcr
);
190 serial_outp(up
, UART_FCR
, (UART_FCR_ENABLE_FIFO
|
191 UART_FCR_CLEAR_RCVR
|
192 UART_FCR_CLEAR_XMIT
));
193 serial_outp(up
, UART_FCR
, 0);
194 (void)serial_in(up
, UART_RX
);
195 serial_outp(up
, UART_IER
, 0);
198 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
199 // restore_flags(flags);
200 DEBUG_AUTOCONF("type=%s\n", uart_config
[up
->port
.type
].name
);
203 static void serial8250_stop_tx(struct uart_port
*port
)
205 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
207 if (up
->ier
& UART_IER_THRI
) {
208 up
->ier
&= ~UART_IER_THRI
;
209 serial_out(up
, UART_IER
, up
->ier
);
213 static void serial8250_start_tx(struct uart_port
*port
)
215 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
217 if (!(up
->ier
& UART_IER_THRI
)) {
218 up
->ier
|= UART_IER_THRI
;
219 serial_out(up
, UART_IER
, up
->ier
);
223 static void serial8250_stop_rx(struct uart_port
*port
)
225 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
227 up
->ier
&= ~UART_IER_RLSI
;
228 up
->port
.read_status_mask
&= ~UART_LSR_DR
;
229 serial_out(up
, UART_IER
, up
->ier
);
232 static void serial8250_enable_ms(struct uart_port
*port
)
234 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
236 up
->ier
|= UART_IER_MSI
;
237 serial_out(up
, UART_IER
, up
->ier
);
241 receive_chars(struct uart_8250_port
*up
, int *status
, struct pt_regs
*regs
)
243 struct tty_struct
*tty
= up
->port
.info
->tty
;
244 unsigned char ch
, flag
;
248 ch
= serial_inp(up
, UART_RX
);
250 up
->port
.icount
.rx
++;
252 if (unlikely(*status
& (UART_LSR_BI
| UART_LSR_PE
|
253 UART_LSR_FE
| UART_LSR_OE
))) {
255 * For statistics only
257 if (*status
& UART_LSR_BI
) {
258 *status
&= ~(UART_LSR_FE
| UART_LSR_PE
);
259 up
->port
.icount
.brk
++;
261 * We do the SysRQ and SAK checking
262 * here because otherwise the break
263 * may get masked by ignore_status_mask
264 * or read_status_mask.
266 if (uart_handle_break(&up
->port
))
268 } else if (*status
& UART_LSR_PE
)
269 up
->port
.icount
.parity
++;
270 else if (*status
& UART_LSR_FE
)
271 up
->port
.icount
.frame
++;
272 if (*status
& UART_LSR_OE
)
273 up
->port
.icount
.overrun
++;
276 * Mask off conditions which should be ingored.
278 *status
&= up
->port
.read_status_mask
;
280 #ifdef CONFIG_SERIAL_AU1X00_CONSOLE
281 if (up
->port
.line
== up
->port
.cons
->index
) {
282 /* Recover the break flag from console xmit */
283 *status
|= up
->lsr_break_flag
;
284 up
->lsr_break_flag
= 0;
287 if (*status
& UART_LSR_BI
) {
288 DEBUG_INTR("handling break....");
290 } else if (*status
& UART_LSR_PE
)
292 else if (*status
& UART_LSR_FE
)
295 if (uart_handle_sysrq_char(&up
->port
, ch
, regs
))
297 if ((*status
& up
->port
.ignore_status_mask
) == 0)
298 tty_insert_flip_char(tty
, ch
, flag
);
299 if (*status
& UART_LSR_OE
)
301 * Overrun is special, since it's reported
302 * immediately, and doesn't affect the current
305 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
308 *status
= serial_inp(up
, UART_LSR
);
309 } while ((*status
& UART_LSR_DR
) && (max_count
-- > 0));
310 spin_unlock(&up
->port
.lock
);
311 tty_flip_buffer_push(tty
);
312 spin_lock(&up
->port
.lock
);
315 static _INLINE_
void transmit_chars(struct uart_8250_port
*up
)
317 struct circ_buf
*xmit
= &up
->port
.info
->xmit
;
320 if (up
->port
.x_char
) {
321 serial_outp(up
, UART_TX
, up
->port
.x_char
);
322 up
->port
.icount
.tx
++;
326 if (uart_circ_empty(xmit
) || uart_tx_stopped(&up
->port
)) {
327 serial8250_stop_tx(&up
->port
);
331 count
= up
->port
.fifosize
;
333 serial_out(up
, UART_TX
, xmit
->buf
[xmit
->tail
]);
334 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
335 up
->port
.icount
.tx
++;
336 if (uart_circ_empty(xmit
))
338 } while (--count
> 0);
340 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
341 uart_write_wakeup(&up
->port
);
343 DEBUG_INTR("THRE...");
345 if (uart_circ_empty(xmit
))
346 serial8250_stop_tx(&up
->port
);
349 static _INLINE_
void check_modem_status(struct uart_8250_port
*up
)
353 status
= serial_in(up
, UART_MSR
);
355 if ((status
& UART_MSR_ANY_DELTA
) == 0)
358 if (status
& UART_MSR_TERI
)
359 up
->port
.icount
.rng
++;
360 if (status
& UART_MSR_DDSR
)
361 up
->port
.icount
.dsr
++;
362 if (status
& UART_MSR_DDCD
)
363 uart_handle_dcd_change(&up
->port
, status
& UART_MSR_DCD
);
364 if (status
& UART_MSR_DCTS
)
365 uart_handle_cts_change(&up
->port
, status
& UART_MSR_CTS
);
367 wake_up_interruptible(&up
->port
.info
->delta_msr_wait
);
371 * This handles the interrupt from one port.
374 serial8250_handle_port(struct uart_8250_port
*up
, struct pt_regs
*regs
)
376 unsigned int status
= serial_inp(up
, UART_LSR
);
378 DEBUG_INTR("status = %x...", status
);
380 if (status
& UART_LSR_DR
)
381 receive_chars(up
, &status
, regs
);
382 check_modem_status(up
);
383 if (status
& UART_LSR_THRE
)
388 * This is the serial driver's interrupt routine.
390 * Arjan thinks the old way was overly complex, so it got simplified.
391 * Alan disagrees, saying that need the complexity to handle the weird
392 * nature of ISA shared interrupts. (This is a special exception.)
394 * In order to handle ISA shared interrupts properly, we need to check
395 * that all ports have been serviced, and therefore the ISA interrupt
396 * line has been de-asserted.
398 * This means we need to loop through all ports. checking that they
399 * don't have an interrupt pending.
401 static irqreturn_t
serial8250_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
403 struct irq_info
*i
= dev_id
;
404 struct list_head
*l
, *end
= NULL
;
405 int pass_counter
= 0;
407 DEBUG_INTR("serial8250_interrupt(%d)...", irq
);
413 struct uart_8250_port
*up
;
416 up
= list_entry(l
, struct uart_8250_port
, list
);
418 iir
= serial_in(up
, UART_IIR
);
419 if (!(iir
& UART_IIR_NO_INT
)) {
420 spin_lock(&up
->port
.lock
);
421 serial8250_handle_port(up
, regs
);
422 spin_unlock(&up
->port
.lock
);
425 } else if (end
== NULL
)
430 if (l
== i
->head
&& pass_counter
++ > PASS_LIMIT
) {
431 /* If we hit this, we're dead. */
432 printk(KERN_ERR
"serial8250: too much work for "
438 spin_unlock(&i
->lock
);
440 DEBUG_INTR("end.\n");
441 /* FIXME! Was it really ours? */
446 * To support ISA shared interrupts, we need to have one interrupt
447 * handler that ensures that the IRQ line has been deasserted
448 * before returning. Failing to do this will result in the IRQ
449 * line being stuck active, and, since ISA irqs are edge triggered,
450 * no more IRQs will be seen.
452 static void serial_do_unlink(struct irq_info
*i
, struct uart_8250_port
*up
)
454 spin_lock_irq(&i
->lock
);
456 if (!list_empty(i
->head
)) {
457 if (i
->head
== &up
->list
)
458 i
->head
= i
->head
->next
;
461 BUG_ON(i
->head
!= &up
->list
);
465 spin_unlock_irq(&i
->lock
);
468 static int serial_link_irq_chain(struct uart_8250_port
*up
)
470 struct irq_info
*i
= irq_lists
+ up
->port
.irq
;
471 int ret
, irq_flags
= up
->port
.flags
& UPF_SHARE_IRQ
? SA_SHIRQ
: 0;
473 spin_lock_irq(&i
->lock
);
476 list_add(&up
->list
, i
->head
);
477 spin_unlock_irq(&i
->lock
);
481 INIT_LIST_HEAD(&up
->list
);
483 spin_unlock_irq(&i
->lock
);
485 ret
= request_irq(up
->port
.irq
, serial8250_interrupt
,
486 irq_flags
, "serial", i
);
488 serial_do_unlink(i
, up
);
494 static void serial_unlink_irq_chain(struct uart_8250_port
*up
)
496 struct irq_info
*i
= irq_lists
+ up
->port
.irq
;
498 BUG_ON(i
->head
== NULL
);
500 if (list_empty(i
->head
))
501 free_irq(up
->port
.irq
, i
);
503 serial_do_unlink(i
, up
);
507 * This function is used to handle ports that do not have an
508 * interrupt. This doesn't work very well for 16450's, but gives
509 * barely passable results for a 16550A. (Although at the expense
510 * of much CPU overhead).
512 static void serial8250_timeout(unsigned long data
)
514 struct uart_8250_port
*up
= (struct uart_8250_port
*)data
;
515 unsigned int timeout
;
518 iir
= serial_in(up
, UART_IIR
);
519 if (!(iir
& UART_IIR_NO_INT
)) {
520 spin_lock(&up
->port
.lock
);
521 serial8250_handle_port(up
, NULL
);
522 spin_unlock(&up
->port
.lock
);
525 timeout
= up
->port
.timeout
;
526 timeout
= timeout
> 6 ? (timeout
/ 2 - 2) : 1;
527 mod_timer(&up
->timer
, jiffies
+ timeout
);
530 static unsigned int serial8250_tx_empty(struct uart_port
*port
)
532 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
536 spin_lock_irqsave(&up
->port
.lock
, flags
);
537 ret
= serial_in(up
, UART_LSR
) & UART_LSR_TEMT
? TIOCSER_TEMT
: 0;
538 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
543 static unsigned int serial8250_get_mctrl(struct uart_port
*port
)
545 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
546 unsigned char status
;
549 status
= serial_in(up
, UART_MSR
);
552 if (status
& UART_MSR_DCD
)
554 if (status
& UART_MSR_RI
)
556 if (status
& UART_MSR_DSR
)
558 if (status
& UART_MSR_CTS
)
563 static void serial8250_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
565 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
566 unsigned char mcr
= 0;
568 if (mctrl
& TIOCM_RTS
)
570 if (mctrl
& TIOCM_DTR
)
572 if (mctrl
& TIOCM_OUT1
)
573 mcr
|= UART_MCR_OUT1
;
574 if (mctrl
& TIOCM_OUT2
)
575 mcr
|= UART_MCR_OUT2
;
576 if (mctrl
& TIOCM_LOOP
)
577 mcr
|= UART_MCR_LOOP
;
579 mcr
= (mcr
& up
->mcr_mask
) | up
->mcr_force
;
581 serial_out(up
, UART_MCR
, mcr
);
584 static void serial8250_break_ctl(struct uart_port
*port
, int break_state
)
586 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
589 spin_lock_irqsave(&up
->port
.lock
, flags
);
590 if (break_state
== -1)
591 up
->lcr
|= UART_LCR_SBC
;
593 up
->lcr
&= ~UART_LCR_SBC
;
594 serial_out(up
, UART_LCR
, up
->lcr
);
595 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
598 static int serial8250_startup(struct uart_port
*port
)
600 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
605 * Clear the FIFO buffers and disable them.
606 * (they will be reeanbled in set_termios())
608 if (uart_config
[up
->port
.type
].flags
& UART_CLEAR_FIFO
) {
609 serial_outp(up
, UART_FCR
, UART_FCR_ENABLE_FIFO
);
610 serial_outp(up
, UART_FCR
, UART_FCR_ENABLE_FIFO
|
611 UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
);
612 serial_outp(up
, UART_FCR
, 0);
616 * Clear the interrupt registers.
618 (void) serial_inp(up
, UART_LSR
);
619 (void) serial_inp(up
, UART_RX
);
620 (void) serial_inp(up
, UART_IIR
);
621 (void) serial_inp(up
, UART_MSR
);
624 * At this point, there's no way the LSR could still be 0xff;
625 * if it is, then bail out, because there's likely no UART
628 if (!(up
->port
.flags
& UPF_BUGGY_UART
) &&
629 (serial_inp(up
, UART_LSR
) == 0xff)) {
630 printk("ttyS%d: LSR safety check engaged!\n", up
->port
.line
);
634 retval
= serial_link_irq_chain(up
);
639 * Now, initialize the UART
641 serial_outp(up
, UART_LCR
, UART_LCR_WLEN8
);
643 spin_lock_irqsave(&up
->port
.lock
, flags
);
644 if (up
->port
.flags
& UPF_FOURPORT
) {
645 if (!is_real_interrupt(up
->port
.irq
))
646 up
->port
.mctrl
|= TIOCM_OUT1
;
649 * Most PC uarts need OUT2 raised to enable interrupts.
651 if (is_real_interrupt(up
->port
.irq
))
652 up
->port
.mctrl
|= TIOCM_OUT2
;
654 serial8250_set_mctrl(&up
->port
, up
->port
.mctrl
);
655 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
658 * Finally, enable interrupts. Note: Modem status interrupts
659 * are set via set_termios(), which will be occurring imminently
660 * anyway, so we don't enable them here.
662 up
->ier
= UART_IER_RLSI
| UART_IER_RDI
;
663 serial_outp(up
, UART_IER
, up
->ier
);
665 if (up
->port
.flags
& UPF_FOURPORT
) {
668 * Enable interrupts on the AST Fourport board
670 icp
= (up
->port
.iobase
& 0xfe0) | 0x01f;
676 * And clear the interrupt registers again for luck.
678 (void) serial_inp(up
, UART_LSR
);
679 (void) serial_inp(up
, UART_RX
);
680 (void) serial_inp(up
, UART_IIR
);
681 (void) serial_inp(up
, UART_MSR
);
686 static void serial8250_shutdown(struct uart_port
*port
)
688 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
692 * Disable interrupts from this port
695 serial_outp(up
, UART_IER
, 0);
697 spin_lock_irqsave(&up
->port
.lock
, flags
);
698 if (up
->port
.flags
& UPF_FOURPORT
) {
699 /* reset interrupts on the AST Fourport board */
700 inb((up
->port
.iobase
& 0xfe0) | 0x1f);
701 up
->port
.mctrl
|= TIOCM_OUT1
;
703 up
->port
.mctrl
&= ~TIOCM_OUT2
;
705 serial8250_set_mctrl(&up
->port
, up
->port
.mctrl
);
706 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
709 * Disable break condition and FIFOs
711 serial_out(up
, UART_LCR
, serial_inp(up
, UART_LCR
) & ~UART_LCR_SBC
);
712 serial_outp(up
, UART_FCR
, UART_FCR_ENABLE_FIFO
|
713 UART_FCR_CLEAR_RCVR
|
714 UART_FCR_CLEAR_XMIT
);
715 serial_outp(up
, UART_FCR
, 0);
718 * Read data port to reset things, and then unlink from
721 (void) serial_in(up
, UART_RX
);
723 if (!is_real_interrupt(up
->port
.irq
))
724 del_timer_sync(&up
->timer
);
726 serial_unlink_irq_chain(up
);
729 static unsigned int serial8250_get_divisor(struct uart_port
*port
, unsigned int baud
)
734 * Handle magic divisors for baud rates above baud_base on
735 * SMSC SuperIO chips.
737 if ((port
->flags
& UPF_MAGIC_MULTIPLIER
) &&
738 baud
== (port
->uartclk
/4))
740 else if ((port
->flags
& UPF_MAGIC_MULTIPLIER
) &&
741 baud
== (port
->uartclk
/8))
744 quot
= uart_get_divisor(port
, baud
);
750 serial8250_set_termios(struct uart_port
*port
, struct termios
*termios
,
753 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
754 unsigned char cval
, fcr
= 0;
756 unsigned int baud
, quot
;
758 switch (termios
->c_cflag
& CSIZE
) {
760 cval
= UART_LCR_WLEN5
;
763 cval
= UART_LCR_WLEN6
;
766 cval
= UART_LCR_WLEN7
;
770 cval
= UART_LCR_WLEN8
;
774 if (termios
->c_cflag
& CSTOPB
)
775 cval
|= UART_LCR_STOP
;
776 if (termios
->c_cflag
& PARENB
)
777 cval
|= UART_LCR_PARITY
;
778 if (!(termios
->c_cflag
& PARODD
))
779 cval
|= UART_LCR_EPAR
;
781 if (termios
->c_cflag
& CMSPAR
)
782 cval
|= UART_LCR_SPAR
;
786 * Ask the core to calculate the divisor for us.
788 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/16);
789 quot
= serial8250_get_divisor(port
, baud
);
790 quot
= 0x35; /* FIXME */
793 * Work around a bug in the Oxford Semiconductor 952 rev B
794 * chip which causes it to seriously miscalculate baud rates
797 if ((quot
& 0xff) == 0 && up
->port
.type
== PORT_16C950
&&
801 if (uart_config
[up
->port
.type
].flags
& UART_USE_FIFO
) {
803 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_R_TRIGGER_1
;
805 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_R_TRIGGER_8
;
809 * Ok, we're now changing the port state. Do it with
810 * interrupts disabled.
812 spin_lock_irqsave(&up
->port
.lock
, flags
);
815 * Update the per-port timeout.
817 uart_update_timeout(port
, termios
->c_cflag
, baud
);
819 up
->port
.read_status_mask
= UART_LSR_OE
| UART_LSR_THRE
| UART_LSR_DR
;
820 if (termios
->c_iflag
& INPCK
)
821 up
->port
.read_status_mask
|= UART_LSR_FE
| UART_LSR_PE
;
822 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
823 up
->port
.read_status_mask
|= UART_LSR_BI
;
826 * Characteres to ignore
828 up
->port
.ignore_status_mask
= 0;
829 if (termios
->c_iflag
& IGNPAR
)
830 up
->port
.ignore_status_mask
|= UART_LSR_PE
| UART_LSR_FE
;
831 if (termios
->c_iflag
& IGNBRK
) {
832 up
->port
.ignore_status_mask
|= UART_LSR_BI
;
834 * If we're ignoring parity and break indicators,
835 * ignore overruns too (for real raw support).
837 if (termios
->c_iflag
& IGNPAR
)
838 up
->port
.ignore_status_mask
|= UART_LSR_OE
;
842 * ignore all characters if CREAD is not set
844 if ((termios
->c_cflag
& CREAD
) == 0)
845 up
->port
.ignore_status_mask
|= UART_LSR_DR
;
848 * CTS flow control flag and modem status interrupts
850 up
->ier
&= ~UART_IER_MSI
;
851 if (UART_ENABLE_MS(&up
->port
, termios
->c_cflag
))
852 up
->ier
|= UART_IER_MSI
;
854 serial_out(up
, UART_IER
, up
->ier
);
855 serial_outp(up
, 0x28, quot
& 0xffff);
856 up
->lcr
= cval
; /* Save LCR */
857 if (up
->port
.type
!= PORT_16750
) {
858 if (fcr
& UART_FCR_ENABLE_FIFO
) {
859 /* emulated UARTs (Lucent Venus 167x) need two steps */
860 serial_outp(up
, UART_FCR
, UART_FCR_ENABLE_FIFO
);
862 serial_outp(up
, UART_FCR
, fcr
); /* set fcr */
864 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
868 serial8250_pm(struct uart_port
*port
, unsigned int state
,
869 unsigned int oldstate
)
871 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
875 up
->pm(port
, state
, oldstate
);
879 up
->pm(port
, state
, oldstate
);
884 * Resource handling. This is complicated by the fact that resources
885 * depend on the port type. Maybe we should be claiming the standard
886 * 8250 ports, and then trying to get other resources as necessary?
889 serial8250_request_std_resource(struct uart_8250_port
*up
, struct resource
**res
)
891 unsigned int size
= 8 << up
->port
.regshift
;
894 switch (up
->port
.iotype
) {
896 if (up
->port
.mapbase
) {
897 *res
= request_mem_region(up
->port
.mapbase
, size
, "serial");
905 *res
= request_region(up
->port
.iobase
, size
, "serial");
914 static void serial8250_release_port(struct uart_port
*port
)
916 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
917 unsigned long start
, offset
= 0, size
= 0;
919 size
<<= up
->port
.regshift
;
921 switch (up
->port
.iotype
) {
923 if (up
->port
.mapbase
) {
927 iounmap(up
->port
.membase
);
928 up
->port
.membase
= NULL
;
930 start
= up
->port
.mapbase
;
933 release_mem_region(start
+ offset
, size
);
934 release_mem_region(start
, 8 << up
->port
.regshift
);
940 start
= up
->port
.iobase
;
943 release_region(start
+ offset
, size
);
944 release_region(start
+ offset
, 8 << up
->port
.regshift
);
952 static int serial8250_request_port(struct uart_port
*port
)
954 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
955 struct resource
*res
= NULL
, *res_rsa
= NULL
;
958 ret
= serial8250_request_std_resource(up
, &res
);
961 * If we have a mapbase, then request that as well.
963 if (ret
== 0 && up
->port
.flags
& UPF_IOREMAP
) {
964 int size
= res
->end
- res
->start
+ 1;
966 up
->port
.membase
= ioremap(up
->port
.mapbase
, size
);
967 if (!up
->port
.membase
)
973 release_resource(res_rsa
);
975 release_resource(res
);
980 static void serial8250_config_port(struct uart_port
*port
, int flags
)
982 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
983 struct resource
*res_std
= NULL
, *res_rsa
= NULL
;
984 int probeflags
= PROBE_ANY
;
986 probeflags
&= ~PROBE_RSA
;
988 if (flags
& UART_CONFIG_TYPE
)
989 autoconfig(up
, probeflags
);
992 * If the port wasn't an RSA port, release the resource.
994 if (up
->port
.type
!= PORT_RSA
&& res_rsa
)
995 release_resource(res_rsa
);
997 if (up
->port
.type
== PORT_UNKNOWN
&& res_std
)
998 release_resource(res_std
);
1002 serial8250_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
1004 if (ser
->irq
>= NR_IRQS
|| ser
->irq
< 0 ||
1005 ser
->baud_base
< 9600 || ser
->type
< PORT_UNKNOWN
||
1006 ser
->type
> PORT_MAX_8250
|| ser
->type
== PORT_CIRRUS
||
1007 ser
->type
== PORT_STARTECH
)
1013 serial8250_type(struct uart_port
*port
)
1015 int type
= port
->type
;
1017 if (type
>= ARRAY_SIZE(uart_config
))
1019 return uart_config
[type
].name
;
1022 static struct uart_ops serial8250_pops
= {
1023 .tx_empty
= serial8250_tx_empty
,
1024 .set_mctrl
= serial8250_set_mctrl
,
1025 .get_mctrl
= serial8250_get_mctrl
,
1026 .stop_tx
= serial8250_stop_tx
,
1027 .start_tx
= serial8250_start_tx
,
1028 .stop_rx
= serial8250_stop_rx
,
1029 .enable_ms
= serial8250_enable_ms
,
1030 .break_ctl
= serial8250_break_ctl
,
1031 .startup
= serial8250_startup
,
1032 .shutdown
= serial8250_shutdown
,
1033 .set_termios
= serial8250_set_termios
,
1034 .pm
= serial8250_pm
,
1035 .type
= serial8250_type
,
1036 .release_port
= serial8250_release_port
,
1037 .request_port
= serial8250_request_port
,
1038 .config_port
= serial8250_config_port
,
1039 .verify_port
= serial8250_verify_port
,
1042 static struct uart_8250_port serial8250_ports
[UART_NR
];
1044 static void __init
serial8250_isa_init_ports(void)
1046 struct uart_8250_port
*up
;
1047 static int first
= 1;
1054 for (i
= 0, up
= serial8250_ports
; i
< ARRAY_SIZE(old_serial_port
);
1056 up
->port
.iobase
= old_serial_port
[i
].port
;
1057 up
->port
.irq
= old_serial_port
[i
].irq
;
1058 up
->port
.uartclk
= get_au1x00_uart_baud_base();
1059 up
->port
.flags
= old_serial_port
[i
].flags
;
1060 up
->port
.hub6
= old_serial_port
[i
].hub6
;
1061 up
->port
.membase
= old_serial_port
[i
].iomem_base
;
1062 up
->port
.iotype
= old_serial_port
[i
].io_type
;
1063 up
->port
.regshift
= old_serial_port
[i
].iomem_reg_shift
;
1064 up
->port
.ops
= &serial8250_pops
;
1068 static void __init
serial8250_register_ports(struct uart_driver
*drv
)
1072 serial8250_isa_init_ports();
1074 for (i
= 0; i
< UART_NR
; i
++) {
1075 struct uart_8250_port
*up
= &serial8250_ports
[i
];
1078 up
->port
.ops
= &serial8250_pops
;
1079 init_timer(&up
->timer
);
1080 up
->timer
.function
= serial8250_timeout
;
1083 * ALPHA_KLUDGE_MCR needs to be killed.
1085 up
->mcr_mask
= ~ALPHA_KLUDGE_MCR
;
1086 up
->mcr_force
= ALPHA_KLUDGE_MCR
;
1088 uart_add_one_port(drv
, &up
->port
);
1092 #ifdef CONFIG_SERIAL_AU1X00_CONSOLE
1094 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1097 * Wait for transmitter & holding register to empty
1099 static inline void wait_for_xmitr(struct uart_8250_port
*up
)
1101 unsigned int status
, tmout
= 10000;
1103 /* Wait up to 10ms for the character(s) to be sent. */
1105 status
= serial_in(up
, UART_LSR
);
1107 if (status
& UART_LSR_BI
)
1108 up
->lsr_break_flag
= UART_LSR_BI
;
1113 } while ((status
& BOTH_EMPTY
) != BOTH_EMPTY
);
1115 /* Wait up to 1s for flow control if necessary */
1116 if (up
->port
.flags
& UPF_CONS_FLOW
) {
1119 ((serial_in(up
, UART_MSR
) & UART_MSR_CTS
) == 0))
1125 * Print a string to the serial port trying not to disturb
1126 * any possible real use of the port...
1128 * The console_lock must be held when we get here.
1131 serial8250_console_write(struct console
*co
, const char *s
, unsigned int count
)
1133 struct uart_8250_port
*up
= &serial8250_ports
[co
->index
];
1138 * First save the UER then disable the interrupts
1140 ier
= serial_in(up
, UART_IER
);
1141 serial_out(up
, UART_IER
, 0);
1144 * Now, do each character
1146 for (i
= 0; i
< count
; i
++, s
++) {
1150 * Send the character out.
1151 * If a LF, also do CR...
1153 serial_out(up
, UART_TX
, *s
);
1156 serial_out(up
, UART_TX
, 13);
1161 * Finally, wait for transmitter to become empty
1162 * and restore the IER
1165 serial_out(up
, UART_IER
, ier
);
1168 static int __init
serial8250_console_setup(struct console
*co
, char *options
)
1170 struct uart_port
*port
;
1177 * Check whether an invalid uart number has been specified, and
1178 * if so, search for the first available port that does have
1181 if (co
->index
>= UART_NR
)
1183 port
= &serial8250_ports
[co
->index
].port
;
1188 spin_lock_init(&port
->lock
);
1191 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
1193 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
1196 extern struct uart_driver serial8250_reg
;
1197 static struct console serial8250_console
= {
1199 .write
= serial8250_console_write
,
1200 .device
= uart_console_device
,
1201 .setup
= serial8250_console_setup
,
1202 .flags
= CON_PRINTBUFFER
,
1204 .data
= &serial8250_reg
,
1207 static int __init
serial8250_console_init(void)
1209 serial8250_isa_init_ports();
1210 register_console(&serial8250_console
);
1213 console_initcall(serial8250_console_init
);
1215 #define SERIAL8250_CONSOLE &serial8250_console
1217 #define SERIAL8250_CONSOLE NULL
1220 static struct uart_driver serial8250_reg
= {
1221 .owner
= THIS_MODULE
,
1222 .driver_name
= "serial",
1223 .devfs_name
= "tts/",
1228 .cons
= SERIAL8250_CONSOLE
,
1231 int __init
early_serial_setup(struct uart_port
*port
)
1233 serial8250_isa_init_ports();
1234 serial8250_ports
[port
->line
].port
= *port
;
1235 serial8250_ports
[port
->line
].port
.ops
= &serial8250_pops
;
1240 * serial8250_suspend_port - suspend one serial port
1241 * @line: serial line number
1242 * @level: the level of port suspension, as per uart_suspend_port
1244 * Suspend one serial port.
1246 void serial8250_suspend_port(int line
)
1248 uart_suspend_port(&serial8250_reg
, &serial8250_ports
[line
].port
);
1252 * serial8250_resume_port - resume one serial port
1253 * @line: serial line number
1254 * @level: the level of port resumption, as per uart_resume_port
1256 * Resume one serial port.
1258 void serial8250_resume_port(int line
)
1260 uart_resume_port(&serial8250_reg
, &serial8250_ports
[line
].port
);
1263 static int __init
serial8250_init(void)
1267 printk(KERN_INFO
"Serial: Au1x00 driver\n");
1269 for (i
= 0; i
< NR_IRQS
; i
++)
1270 spin_lock_init(&irq_lists
[i
].lock
);
1272 ret
= uart_register_driver(&serial8250_reg
);
1274 serial8250_register_ports(&serial8250_reg
);
1279 static void __exit
serial8250_exit(void)
1283 for (i
= 0; i
< UART_NR
; i
++)
1284 uart_remove_one_port(&serial8250_reg
, &serial8250_ports
[i
].port
);
1286 uart_unregister_driver(&serial8250_reg
);
1289 module_init(serial8250_init
);
1290 module_exit(serial8250_exit
);
1292 EXPORT_SYMBOL(serial8250_suspend_port
);
1293 EXPORT_SYMBOL(serial8250_resume_port
);
1295 MODULE_LICENSE("GPL");
1296 MODULE_DESCRIPTION("Au1x00 serial driver\n");