[CPUFREQ] Fix the p4-clockmod N60 errata workaround.
[linux-2.6/mini2440.git] / drivers / serial / au1x00_uart.c
blob344022fe53ef870522700ef2e92e57e6b25c1945
1 /*
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>
34 #include <asm/io.h>
35 #include <asm/irq.h>
36 #include <asm/mach-au1x00/au1000.h>
38 #if defined(CONFIG_SERIAL_AU1X00_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
39 #define SUPPORT_SYSRQ
40 #endif
42 #include <linux/serial_core.h>
43 #include "8250.h"
46 * Debugging.
48 #if 0
49 #define DEBUG_AUTOCONF(fmt...) printk(fmt)
50 #else
51 #define DEBUG_AUTOCONF(fmt...) do { } while (0)
52 #endif
54 #if 0
55 #define DEBUG_INTR(fmt...) printk(fmt)
56 #else
57 #define DEBUG_INTR(fmt...) do { } while (0)
58 #endif
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[] = {
70 { .baud_base = 0,
71 .iomem_base = (u8 *)UART0_ADDR,
72 .irq = AU1000_UART0_INT,
73 .flags = STD_COM_FLAGS,
74 .iomem_reg_shift = 2,
75 }, {
76 .baud_base = 0,
77 .iomem_base = (u8 *)UART1_ADDR,
78 .irq = AU1000_UART1_INT,
79 .flags = STD_COM_FLAGS,
80 .iomem_reg_shift = 2
81 }, {
82 .baud_base = 0,
83 .iomem_base = (u8 *)UART2_ADDR,
84 .irq = AU1000_UART2_INT,
85 .flags = STD_COM_FLAGS,
86 .iomem_reg_shift = 2
87 }, {
88 .baud_base = 0,
89 .iomem_base = (u8 *)UART3_ADDR,
90 .irq = AU1000_UART3_INT,
91 .flags = STD_COM_FLAGS,
92 .iomem_reg_shift = 2
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 */
102 unsigned short rev;
103 unsigned char acr;
104 unsigned char ier;
105 unsigned char lcr;
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);
117 struct irq_info {
118 spinlock_t lock;
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] = {
128 { "unknown", 1, 0 },
129 { "8250", 1, 0 },
130 { "16450", 1, 0 },
131 { "16550", 1, 0 },
132 /* PORT_16550A */
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);
141 static _INLINE_ void
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;
160 unsigned long flags;
162 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
163 return;
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)
184 goto out;
187 * Reset the UART.
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);
197 out:
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);
240 static _INLINE_ void
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;
245 int max_count = 256;
247 do {
248 ch = serial_inp(up, UART_RX);
249 flag = TTY_NORMAL;
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))
267 goto ignore_char;
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;
286 #endif
287 if (*status & UART_LSR_BI) {
288 DEBUG_INTR("handling break....");
289 flag = TTY_BREAK;
290 } else if (*status & UART_LSR_PE)
291 flag = TTY_PARITY;
292 else if (*status & UART_LSR_FE)
293 flag = TTY_FRAME;
295 if (uart_handle_sysrq_char(&up->port, ch, regs))
296 goto ignore_char;
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
303 * character.
305 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
307 ignore_char:
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;
318 int count;
320 if (up->port.x_char) {
321 serial_outp(up, UART_TX, up->port.x_char);
322 up->port.icount.tx++;
323 up->port.x_char = 0;
324 return;
326 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
327 serial8250_stop_tx(&up->port);
328 return;
331 count = up->port.fifosize;
332 do {
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))
337 break;
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)
351 int status;
353 status = serial_in(up, UART_MSR);
355 if ((status & UART_MSR_ANY_DELTA) == 0)
356 return;
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.
373 static inline void
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)
384 transmit_chars(up);
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);
409 spin_lock(&i->lock);
411 l = i->head;
412 do {
413 struct uart_8250_port *up;
414 unsigned int iir;
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);
424 end = NULL;
425 } else if (end == NULL)
426 end = l;
428 l = l->next;
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 "
433 "irq%d\n", irq);
434 break;
436 } while (l != end);
438 spin_unlock(&i->lock);
440 DEBUG_INTR("end.\n");
441 /* FIXME! Was it really ours? */
442 return IRQ_HANDLED;
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;
459 list_del(&up->list);
460 } else {
461 BUG_ON(i->head != &up->list);
462 i->head = NULL;
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);
475 if (i->head) {
476 list_add(&up->list, i->head);
477 spin_unlock_irq(&i->lock);
479 ret = 0;
480 } else {
481 INIT_LIST_HEAD(&up->list);
482 i->head = &up->list;
483 spin_unlock_irq(&i->lock);
485 ret = request_irq(up->port.irq, serial8250_interrupt,
486 irq_flags, "serial", i);
487 if (ret < 0)
488 serial_do_unlink(i, up);
491 return ret;
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;
516 unsigned int iir;
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;
533 unsigned long flags;
534 unsigned int ret;
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);
540 return ret;
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;
547 unsigned int ret;
549 status = serial_in(up, UART_MSR);
551 ret = 0;
552 if (status & UART_MSR_DCD)
553 ret |= TIOCM_CAR;
554 if (status & UART_MSR_RI)
555 ret |= TIOCM_RNG;
556 if (status & UART_MSR_DSR)
557 ret |= TIOCM_DSR;
558 if (status & UART_MSR_CTS)
559 ret |= TIOCM_CTS;
560 return ret;
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)
569 mcr |= UART_MCR_RTS;
570 if (mctrl & TIOCM_DTR)
571 mcr |= UART_MCR_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;
587 unsigned long flags;
589 spin_lock_irqsave(&up->port.lock, flags);
590 if (break_state == -1)
591 up->lcr |= UART_LCR_SBC;
592 else
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;
601 unsigned long flags;
602 int retval;
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
626 * here.
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);
631 return -ENODEV;
634 retval = serial_link_irq_chain(up);
635 if (retval)
636 return retval;
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;
647 } else
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) {
666 unsigned int icp;
668 * Enable interrupts on the AST Fourport board
670 icp = (up->port.iobase & 0xfe0) | 0x01f;
671 outb_p(0x80, icp);
672 (void) inb_p(icp);
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);
683 return 0;
686 static void serial8250_shutdown(struct uart_port *port)
688 struct uart_8250_port *up = (struct uart_8250_port *)port;
689 unsigned long flags;
692 * Disable interrupts from this port
694 up->ier = 0;
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;
702 } else
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
719 * the IRQ chain.
721 (void) serial_in(up, UART_RX);
723 if (!is_real_interrupt(up->port.irq))
724 del_timer_sync(&up->timer);
725 else
726 serial_unlink_irq_chain(up);
729 static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
731 unsigned int quot;
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))
739 quot = 0x8001;
740 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
741 baud == (port->uartclk/8))
742 quot = 0x8002;
743 else
744 quot = uart_get_divisor(port, baud);
746 return quot;
749 static void
750 serial8250_set_termios(struct uart_port *port, struct termios *termios,
751 struct termios *old)
753 struct uart_8250_port *up = (struct uart_8250_port *)port;
754 unsigned char cval, fcr = 0;
755 unsigned long flags;
756 unsigned int baud, quot;
758 switch (termios->c_cflag & CSIZE) {
759 case CS5:
760 cval = UART_LCR_WLEN5;
761 break;
762 case CS6:
763 cval = UART_LCR_WLEN6;
764 break;
765 case CS7:
766 cval = UART_LCR_WLEN7;
767 break;
768 default:
769 case CS8:
770 cval = UART_LCR_WLEN8;
771 break;
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;
780 #ifdef CMSPAR
781 if (termios->c_cflag & CMSPAR)
782 cval |= UART_LCR_SPAR;
783 #endif
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
795 * when DLL is 0.
797 if ((quot & 0xff) == 0 && up->port.type == PORT_16C950 &&
798 up->rev == 0x5201)
799 quot ++;
801 if (uart_config[up->port.type].flags & UART_USE_FIFO) {
802 if (baud < 2400)
803 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_1;
804 else
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);
867 static void
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;
872 if (state) {
873 /* sleep */
874 if (up->pm)
875 up->pm(port, state, oldstate);
876 } else {
877 /* wake */
878 if (up->pm)
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?
888 static int
889 serial8250_request_std_resource(struct uart_8250_port *up, struct resource **res)
891 unsigned int size = 8 << up->port.regshift;
892 int ret = 0;
894 switch (up->port.iotype) {
895 case UPIO_MEM:
896 if (up->port.mapbase) {
897 *res = request_mem_region(up->port.mapbase, size, "serial");
898 if (!*res)
899 ret = -EBUSY;
901 break;
903 case UPIO_HUB6:
904 case UPIO_PORT:
905 *res = request_region(up->port.iobase, size, "serial");
906 if (!*res)
907 ret = -EBUSY;
908 break;
910 return ret;
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) {
922 case UPIO_MEM:
923 if (up->port.mapbase) {
925 * Unmap the area.
927 iounmap(up->port.membase);
928 up->port.membase = NULL;
930 start = up->port.mapbase;
932 if (size)
933 release_mem_region(start + offset, size);
934 release_mem_region(start, 8 << up->port.regshift);
936 break;
938 case UPIO_HUB6:
939 case UPIO_PORT:
940 start = up->port.iobase;
942 if (size)
943 release_region(start + offset, size);
944 release_region(start + offset, 8 << up->port.regshift);
945 break;
947 default:
948 break;
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;
956 int ret = 0;
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)
968 ret = -ENOMEM;
971 if (ret < 0) {
972 if (res_rsa)
973 release_resource(res_rsa);
974 if (res)
975 release_resource(res);
977 return ret;
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);
1001 static int
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)
1008 return -EINVAL;
1009 return 0;
1012 static const char *
1013 serial8250_type(struct uart_port *port)
1015 int type = port->type;
1017 if (type >= ARRAY_SIZE(uart_config))
1018 type = 0;
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;
1048 int i;
1050 if (!first)
1051 return;
1052 first = 0;
1054 for (i = 0, up = serial8250_ports; i < ARRAY_SIZE(old_serial_port);
1055 i++, up++) {
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)
1070 int i;
1072 serial8250_isa_init_ports();
1074 for (i = 0; i < UART_NR; i++) {
1075 struct uart_8250_port *up = &serial8250_ports[i];
1077 up->port.line = 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. */
1104 do {
1105 status = serial_in(up, UART_LSR);
1107 if (status & UART_LSR_BI)
1108 up->lsr_break_flag = UART_LSR_BI;
1110 if (--tmout == 0)
1111 break;
1112 udelay(1);
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) {
1117 tmout = 1000000;
1118 while (--tmout &&
1119 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
1120 udelay(1);
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.
1130 static void
1131 serial8250_console_write(struct console *co, const char *s, unsigned int count)
1133 struct uart_8250_port *up = &serial8250_ports[co->index];
1134 unsigned int ier;
1135 int i;
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++) {
1147 wait_for_xmitr(up);
1150 * Send the character out.
1151 * If a LF, also do CR...
1153 serial_out(up, UART_TX, *s);
1154 if (*s == 10) {
1155 wait_for_xmitr(up);
1156 serial_out(up, UART_TX, 13);
1161 * Finally, wait for transmitter to become empty
1162 * and restore the IER
1164 wait_for_xmitr(up);
1165 serial_out(up, UART_IER, ier);
1168 static int __init serial8250_console_setup(struct console *co, char *options)
1170 struct uart_port *port;
1171 int baud = 9600;
1172 int bits = 8;
1173 int parity = 'n';
1174 int flow = 'n';
1177 * Check whether an invalid uart number has been specified, and
1178 * if so, search for the first available port that does have
1179 * console support.
1181 if (co->index >= UART_NR)
1182 co->index = 0;
1183 port = &serial8250_ports[co->index].port;
1186 * Temporary fix.
1188 spin_lock_init(&port->lock);
1190 if (options)
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 = {
1198 .name = "ttyS",
1199 .write = serial8250_console_write,
1200 .device = uart_console_device,
1201 .setup = serial8250_console_setup,
1202 .flags = CON_PRINTBUFFER,
1203 .index = -1,
1204 .data = &serial8250_reg,
1207 static int __init serial8250_console_init(void)
1209 serial8250_isa_init_ports();
1210 register_console(&serial8250_console);
1211 return 0;
1213 console_initcall(serial8250_console_init);
1215 #define SERIAL8250_CONSOLE &serial8250_console
1216 #else
1217 #define SERIAL8250_CONSOLE NULL
1218 #endif
1220 static struct uart_driver serial8250_reg = {
1221 .owner = THIS_MODULE,
1222 .driver_name = "serial",
1223 .devfs_name = "tts/",
1224 .dev_name = "ttyS",
1225 .major = TTY_MAJOR,
1226 .minor = 64,
1227 .nr = UART_NR,
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;
1236 return 0;
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)
1265 int ret, i;
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);
1273 if (ret >= 0)
1274 serial8250_register_ports(&serial8250_reg);
1276 return ret;
1279 static void __exit serial8250_exit(void)
1281 int i;
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");