blob: ceb5d7f37bbd26920b27813b29daf812ec33926e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for 8250/16550-type serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2001 Russell King.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * A note about mapbase / membase
14 *
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.
18 *
19 * membase is an 'ioremapped' cookie. This is compatible with the old
20 * serial.c driver, and is currently the preferred form.
21 */
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>
32
33#include <asm/serial.h>
34#include <asm/io.h>
35#include <asm/irq.h>
36#include <asm/mach-au1x00/au1000.h>
37
38#if defined(CONFIG_SERIAL_AU1X00_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
39#define SUPPORT_SYSRQ
40#endif
41
42#include <linux/serial_core.h>
43#include "8250.h"
44
45/*
46 * Debugging.
47 */
48#if 0
49#define DEBUG_AUTOCONF(fmt...) printk(fmt)
50#else
51#define DEBUG_AUTOCONF(fmt...) do { } while (0)
52#endif
53
54#if 0
55#define DEBUG_INTR(fmt...) printk(fmt)
56#else
57#define DEBUG_INTR(fmt...) do { } while (0)
58#endif
59
60#define PASS_LIMIT 256
61
62/*
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.
66 */
67#define is_real_interrupt(irq) ((irq) != 0)
68
69static 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
93 }
94};
95
96#define UART_NR ARRAY_SIZE(old_serial_port)
97
98struct 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;
109
110 /*
111 * We provide a per-port pm hook.
112 */
113 void (*pm)(struct uart_port *port,
114 unsigned int state, unsigned int old);
115};
116
117struct irq_info {
118 spinlock_t lock;
119 struct list_head *head;
120};
121
122static struct irq_info irq_lists[NR_IRQS];
123
124/*
125 * Here we define the default xmit fifo size used for each type of UART.
126 */
127static 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 },
134};
135
136static _INLINE_ unsigned int serial_in(struct uart_8250_port *up, int offset)
137{
138 return au_readl((unsigned long)up->port.membase + offset);
139}
140
141static _INLINE_ void
142serial_out(struct uart_8250_port *up, int offset, int value)
143{
144 au_writel(value, (unsigned long)up->port.membase + offset);
145}
146
147#define serial_inp(up, offset) serial_in(up, offset)
148#define serial_outp(up, offset, value) serial_out(up, offset, value)
149
150/*
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.
156 */
157static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
158{
159 unsigned char save_lcr, save_mcr;
160 unsigned long flags;
161
162 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
163 return;
164
165 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%08lx): ",
166 up->port.line, up->port.iobase, up->port.membase);
167
168 /*
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.
171 */
172 spin_lock_irqsave(&up->port.lock, flags);
173// save_flags(flags); cli();
174
175 save_mcr = serial_in(up, UART_MCR);
176 save_lcr = serial_in(up, UART_LCR);
177
178 up->port.type = PORT_16550A;
179 serial_outp(up, UART_LCR, save_lcr);
180
181 up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;
182
183 if (up->port.type == PORT_UNKNOWN)
184 goto out;
185
186 /*
187 * Reset the UART.
188 */
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);
196
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);
201}
202
Russell Kingb129a8c2005-08-31 10:12:14 +0100203static void serial8250_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 struct uart_8250_port *up = (struct uart_8250_port *)port;
206
207 if (up->ier & UART_IER_THRI) {
208 up->ier &= ~UART_IER_THRI;
209 serial_out(up, UART_IER, up->ier);
210 }
211}
212
Russell Kingb129a8c2005-08-31 10:12:14 +0100213static void serial8250_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 struct uart_8250_port *up = (struct uart_8250_port *)port;
216
217 if (!(up->ier & UART_IER_THRI)) {
218 up->ier |= UART_IER_THRI;
219 serial_out(up, UART_IER, up->ier);
220 }
221}
222
223static void serial8250_stop_rx(struct uart_port *port)
224{
225 struct uart_8250_port *up = (struct uart_8250_port *)port;
226
227 up->ier &= ~UART_IER_RLSI;
228 up->port.read_status_mask &= ~UART_LSR_DR;
229 serial_out(up, UART_IER, up->ier);
230}
231
232static void serial8250_enable_ms(struct uart_port *port)
233{
234 struct uart_8250_port *up = (struct uart_8250_port *)port;
235
236 up->ier |= UART_IER_MSI;
237 serial_out(up, UART_IER, up->ier);
238}
239
240static _INLINE_ void
241receive_chars(struct uart_8250_port *up, int *status, struct pt_regs *regs)
242{
243 struct tty_struct *tty = up->port.info->tty;
Alan Cox33f0f882006-01-09 20:54:13 -0800244 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 int max_count = 256;
246
247 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 ch = serial_inp(up, UART_RX);
Alan Cox33f0f882006-01-09 20:54:13 -0800249 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 up->port.icount.rx++;
251
252 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
253 UART_LSR_FE | UART_LSR_OE))) {
254 /*
255 * For statistics only
256 */
257 if (*status & UART_LSR_BI) {
258 *status &= ~(UART_LSR_FE | UART_LSR_PE);
259 up->port.icount.brk++;
260 /*
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.
265 */
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++;
274
275 /*
276 * Mask off conditions which should be ingored.
277 */
278 *status &= up->port.read_status_mask;
279
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;
285 }
286#endif
287 if (*status & UART_LSR_BI) {
288 DEBUG_INTR("handling break....");
Alan Cox33f0f882006-01-09 20:54:13 -0800289 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 } else if (*status & UART_LSR_PE)
Alan Cox33f0f882006-01-09 20:54:13 -0800291 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 else if (*status & UART_LSR_FE)
Alan Cox33f0f882006-01-09 20:54:13 -0800293 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295 if (uart_handle_sysrq_char(&up->port, ch, regs))
296 goto ignore_char;
Alan Cox33f0f882006-01-09 20:54:13 -0800297 if ((*status & up->port.ignore_status_mask) == 0)
298 tty_insert_flip_char(tty, ch, flag);
299 if (*status & UART_LSR_OE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /*
301 * Overrun is special, since it's reported
302 * immediately, and doesn't affect the current
303 * character.
304 */
Alan Cox33f0f882006-01-09 20:54:13 -0800305 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
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);
313}
314
315static _INLINE_ void transmit_chars(struct uart_8250_port *up)
316{
317 struct circ_buf *xmit = &up->port.info->xmit;
318 int count;
319
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;
325 }
326 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100327 serial8250_stop_tx(&up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return;
329 }
330
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);
339
340 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
341 uart_write_wakeup(&up->port);
342
343 DEBUG_INTR("THRE...");
344
345 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100346 serial8250_stop_tx(&up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349static _INLINE_ void check_modem_status(struct uart_8250_port *up)
350{
351 int status;
352
353 status = serial_in(up, UART_MSR);
354
355 if ((status & UART_MSR_ANY_DELTA) == 0)
356 return;
357
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);
366
367 wake_up_interruptible(&up->port.info->delta_msr_wait);
368}
369
370/*
371 * This handles the interrupt from one port.
372 */
373static inline void
374serial8250_handle_port(struct uart_8250_port *up, struct pt_regs *regs)
375{
376 unsigned int status = serial_inp(up, UART_LSR);
377
378 DEBUG_INTR("status = %x...", status);
379
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);
385}
386
387/*
388 * This is the serial driver's interrupt routine.
389 *
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.)
393 *
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.
397 *
398 * This means we need to loop through all ports. checking that they
399 * don't have an interrupt pending.
400 */
401static irqreturn_t serial8250_interrupt(int irq, void *dev_id, struct pt_regs *regs)
402{
403 struct irq_info *i = dev_id;
404 struct list_head *l, *end = NULL;
405 int pass_counter = 0;
406
407 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
408
409 spin_lock(&i->lock);
410
411 l = i->head;
412 do {
413 struct uart_8250_port *up;
414 unsigned int iir;
415
416 up = list_entry(l, struct uart_8250_port, list);
417
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);
423
424 end = NULL;
425 } else if (end == NULL)
426 end = l;
427
428 l = l->next;
429
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;
435 }
436 } while (l != end);
437
438 spin_unlock(&i->lock);
439
440 DEBUG_INTR("end.\n");
441 /* FIXME! Was it really ours? */
442 return IRQ_HANDLED;
443}
444
445/*
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.
451 */
452static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
453{
454 spin_lock_irq(&i->lock);
455
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;
463 }
464
465 spin_unlock_irq(&i->lock);
466}
467
468static int serial_link_irq_chain(struct uart_8250_port *up)
469{
470 struct irq_info *i = irq_lists + up->port.irq;
471 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? SA_SHIRQ : 0;
472
473 spin_lock_irq(&i->lock);
474
475 if (i->head) {
476 list_add(&up->list, i->head);
477 spin_unlock_irq(&i->lock);
478
479 ret = 0;
480 } else {
481 INIT_LIST_HEAD(&up->list);
482 i->head = &up->list;
483 spin_unlock_irq(&i->lock);
484
485 ret = request_irq(up->port.irq, serial8250_interrupt,
486 irq_flags, "serial", i);
487 if (ret < 0)
488 serial_do_unlink(i, up);
489 }
490
491 return ret;
492}
493
494static void serial_unlink_irq_chain(struct uart_8250_port *up)
495{
496 struct irq_info *i = irq_lists + up->port.irq;
497
498 BUG_ON(i->head == NULL);
499
500 if (list_empty(i->head))
501 free_irq(up->port.irq, i);
502
503 serial_do_unlink(i, up);
504}
505
506/*
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).
511 */
512static void serial8250_timeout(unsigned long data)
513{
514 struct uart_8250_port *up = (struct uart_8250_port *)data;
515 unsigned int timeout;
516 unsigned int iir;
517
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);
523 }
524
525 timeout = up->port.timeout;
526 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
527 mod_timer(&up->timer, jiffies + timeout);
528}
529
530static unsigned int serial8250_tx_empty(struct uart_port *port)
531{
532 struct uart_8250_port *up = (struct uart_8250_port *)port;
533 unsigned long flags;
534 unsigned int ret;
535
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);
539
540 return ret;
541}
542
543static unsigned int serial8250_get_mctrl(struct uart_port *port)
544{
545 struct uart_8250_port *up = (struct uart_8250_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 unsigned char status;
547 unsigned int ret;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 status = serial_in(up, UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
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;
561}
562
563static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
564{
565 struct uart_8250_port *up = (struct uart_8250_port *)port;
566 unsigned char mcr = 0;
567
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;
578
579 mcr = (mcr & up->mcr_mask) | up->mcr_force;
580
581 serial_out(up, UART_MCR, mcr);
582}
583
584static void serial8250_break_ctl(struct uart_port *port, int break_state)
585{
586 struct uart_8250_port *up = (struct uart_8250_port *)port;
587 unsigned long flags;
588
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);
596}
597
598static int serial8250_startup(struct uart_port *port)
599{
600 struct uart_8250_port *up = (struct uart_8250_port *)port;
601 unsigned long flags;
602 int retval;
603
604 /*
605 * Clear the FIFO buffers and disable them.
606 * (they will be reeanbled in set_termios())
607 */
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);
613 }
614
615 /*
616 * Clear the interrupt registers.
617 */
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);
622
623 /*
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.
627 */
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;
632 }
633
634 retval = serial_link_irq_chain(up);
635 if (retval)
636 return retval;
637
638 /*
639 * Now, initialize the UART
640 */
641 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
642
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
648 /*
649 * Most PC uarts need OUT2 raised to enable interrupts.
650 */
651 if (is_real_interrupt(up->port.irq))
652 up->port.mctrl |= TIOCM_OUT2;
653
654 serial8250_set_mctrl(&up->port, up->port.mctrl);
655 spin_unlock_irqrestore(&up->port.lock, flags);
656
657 /*
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.
661 */
662 up->ier = UART_IER_RLSI | UART_IER_RDI;
663 serial_outp(up, UART_IER, up->ier);
664
665 if (up->port.flags & UPF_FOURPORT) {
666 unsigned int icp;
667 /*
668 * Enable interrupts on the AST Fourport board
669 */
670 icp = (up->port.iobase & 0xfe0) | 0x01f;
671 outb_p(0x80, icp);
672 (void) inb_p(icp);
673 }
674
675 /*
676 * And clear the interrupt registers again for luck.
677 */
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);
682
683 return 0;
684}
685
686static void serial8250_shutdown(struct uart_port *port)
687{
688 struct uart_8250_port *up = (struct uart_8250_port *)port;
689 unsigned long flags;
690
691 /*
692 * Disable interrupts from this port
693 */
694 up->ier = 0;
695 serial_outp(up, UART_IER, 0);
696
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;
704
705 serial8250_set_mctrl(&up->port, up->port.mctrl);
706 spin_unlock_irqrestore(&up->port.lock, flags);
707
708 /*
709 * Disable break condition and FIFOs
710 */
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);
716
717 /*
718 * Read data port to reset things, and then unlink from
719 * the IRQ chain.
720 */
721 (void) serial_in(up, UART_RX);
722
723 if (!is_real_interrupt(up->port.irq))
724 del_timer_sync(&up->timer);
725 else
726 serial_unlink_irq_chain(up);
727}
728
729static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
730{
731 unsigned int quot;
732
733 /*
734 * Handle magic divisors for baud rates above baud_base on
735 * SMSC SuperIO chips.
736 */
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);
745
746 return quot;
747}
748
749static void
750serial8250_set_termios(struct uart_port *port, struct termios *termios,
751 struct termios *old)
752{
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;
757
758 switch (termios->c_cflag & CSIZE) {
759 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +0100760 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
762 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +0100763 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 break;
765 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +0100766 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 break;
768 default:
769 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +0100770 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 break;
772 }
773
774 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +0100775 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 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
784
785 /*
786 * Ask the core to calculate the divisor for us.
787 */
788 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
789 quot = serial8250_get_divisor(port, baud);
790 quot = 0x35; /* FIXME */
791
792 /*
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.
796 */
797 if ((quot & 0xff) == 0 && up->port.type == PORT_16C950 &&
798 up->rev == 0x5201)
799 quot ++;
800
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;
806 }
807
808 /*
809 * Ok, we're now changing the port state. Do it with
810 * interrupts disabled.
811 */
812 spin_lock_irqsave(&up->port.lock, flags);
813
814 /*
815 * Update the per-port timeout.
816 */
817 uart_update_timeout(port, termios->c_cflag, baud);
818
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;
824
825 /*
826 * Characteres to ignore
827 */
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;
833 /*
834 * If we're ignoring parity and break indicators,
835 * ignore overruns too (for real raw support).
836 */
837 if (termios->c_iflag & IGNPAR)
838 up->port.ignore_status_mask |= UART_LSR_OE;
839 }
840
841 /*
842 * ignore all characters if CREAD is not set
843 */
844 if ((termios->c_cflag & CREAD) == 0)
845 up->port.ignore_status_mask |= UART_LSR_DR;
846
847 /*
848 * CTS flow control flag and modem status interrupts
849 */
850 up->ier &= ~UART_IER_MSI;
851 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
852 up->ier |= UART_IER_MSI;
853
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);
861 }
862 serial_outp(up, UART_FCR, fcr); /* set fcr */
863 }
864 spin_unlock_irqrestore(&up->port.lock, flags);
865}
866
867static void
868serial8250_pm(struct uart_port *port, unsigned int state,
869 unsigned int oldstate)
870{
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);
880 }
881}
882
883/*
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?
887 */
888static int
889serial8250_request_std_resource(struct uart_8250_port *up, struct resource **res)
890{
891 unsigned int size = 8 << up->port.regshift;
892 int ret = 0;
893
894 switch (up->port.iotype) {
895 case SERIAL_IO_MEM:
896 if (up->port.mapbase) {
897 *res = request_mem_region(up->port.mapbase, size, "serial");
898 if (!*res)
899 ret = -EBUSY;
900 }
901 break;
902
903 case SERIAL_IO_HUB6:
904 case SERIAL_IO_PORT:
905 *res = request_region(up->port.iobase, size, "serial");
906 if (!*res)
907 ret = -EBUSY;
908 break;
909 }
910 return ret;
911}
912
913
914static void serial8250_release_port(struct uart_port *port)
915{
916 struct uart_8250_port *up = (struct uart_8250_port *)port;
917 unsigned long start, offset = 0, size = 0;
918
919 size <<= up->port.regshift;
920
921 switch (up->port.iotype) {
922 case SERIAL_IO_MEM:
923 if (up->port.mapbase) {
924 /*
925 * Unmap the area.
926 */
927 iounmap(up->port.membase);
928 up->port.membase = NULL;
929
930 start = up->port.mapbase;
931
932 if (size)
933 release_mem_region(start + offset, size);
934 release_mem_region(start, 8 << up->port.regshift);
935 }
936 break;
937
938 case SERIAL_IO_HUB6:
939 case SERIAL_IO_PORT:
940 start = up->port.iobase;
941
942 if (size)
943 release_region(start + offset, size);
944 release_region(start + offset, 8 << up->port.regshift);
945 break;
946
947 default:
948 break;
949 }
950}
951
952static int serial8250_request_port(struct uart_port *port)
953{
954 struct uart_8250_port *up = (struct uart_8250_port *)port;
955 struct resource *res = NULL, *res_rsa = NULL;
956 int ret = 0;
957
958 ret = serial8250_request_std_resource(up, &res);
959
960 /*
961 * If we have a mapbase, then request that as well.
962 */
963 if (ret == 0 && up->port.flags & UPF_IOREMAP) {
964 int size = res->end - res->start + 1;
965
966 up->port.membase = ioremap(up->port.mapbase, size);
967 if (!up->port.membase)
968 ret = -ENOMEM;
969 }
970
971 if (ret < 0) {
972 if (res_rsa)
973 release_resource(res_rsa);
974 if (res)
975 release_resource(res);
976 }
977 return ret;
978}
979
980static void serial8250_config_port(struct uart_port *port, int flags)
981{
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;
985
986 probeflags &= ~PROBE_RSA;
987
988 if (flags & UART_CONFIG_TYPE)
989 autoconfig(up, probeflags);
990
991 /*
992 * If the port wasn't an RSA port, release the resource.
993 */
994 if (up->port.type != PORT_RSA && res_rsa)
995 release_resource(res_rsa);
996
997 if (up->port.type == PORT_UNKNOWN && res_std)
998 release_resource(res_std);
999}
1000
1001static int
1002serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
1003{
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;
1010}
1011
1012static const char *
1013serial8250_type(struct uart_port *port)
1014{
1015 int type = port->type;
1016
1017 if (type >= ARRAY_SIZE(uart_config))
1018 type = 0;
1019 return uart_config[type].name;
1020}
1021
1022static 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,
1040};
1041
1042static struct uart_8250_port serial8250_ports[UART_NR];
1043
1044static void __init serial8250_isa_init_ports(void)
1045{
1046 struct uart_8250_port *up;
1047 static int first = 1;
1048 int i;
1049
1050 if (!first)
1051 return;
1052 first = 0;
1053
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;
1065 }
1066}
1067
1068static void __init serial8250_register_ports(struct uart_driver *drv)
1069{
1070 int i;
1071
1072 serial8250_isa_init_ports();
1073
1074 for (i = 0; i < UART_NR; i++) {
1075 struct uart_8250_port *up = &serial8250_ports[i];
1076
1077 up->port.line = i;
1078 up->port.ops = &serial8250_pops;
1079 init_timer(&up->timer);
1080 up->timer.function = serial8250_timeout;
1081
1082 /*
1083 * ALPHA_KLUDGE_MCR needs to be killed.
1084 */
1085 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
1086 up->mcr_force = ALPHA_KLUDGE_MCR;
1087
1088 uart_add_one_port(drv, &up->port);
1089 }
1090}
1091
1092#ifdef CONFIG_SERIAL_AU1X00_CONSOLE
1093
1094#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1095
1096/*
1097 * Wait for transmitter & holding register to empty
1098 */
1099static inline void wait_for_xmitr(struct uart_8250_port *up)
1100{
1101 unsigned int status, tmout = 10000;
1102
1103 /* Wait up to 10ms for the character(s) to be sent. */
1104 do {
1105 status = serial_in(up, UART_LSR);
1106
1107 if (status & UART_LSR_BI)
1108 up->lsr_break_flag = UART_LSR_BI;
1109
1110 if (--tmout == 0)
1111 break;
1112 udelay(1);
1113 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
1114
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);
1121 }
1122}
1123
1124/*
1125 * Print a string to the serial port trying not to disturb
1126 * any possible real use of the port...
1127 *
1128 * The console_lock must be held when we get here.
1129 */
1130static void
1131serial8250_console_write(struct console *co, const char *s, unsigned int count)
1132{
1133 struct uart_8250_port *up = &serial8250_ports[co->index];
1134 unsigned int ier;
1135 int i;
1136
1137 /*
1138 * First save the UER then disable the interrupts
1139 */
1140 ier = serial_in(up, UART_IER);
1141 serial_out(up, UART_IER, 0);
1142
1143 /*
1144 * Now, do each character
1145 */
1146 for (i = 0; i < count; i++, s++) {
1147 wait_for_xmitr(up);
1148
1149 /*
1150 * Send the character out.
1151 * If a LF, also do CR...
1152 */
1153 serial_out(up, UART_TX, *s);
1154 if (*s == 10) {
1155 wait_for_xmitr(up);
1156 serial_out(up, UART_TX, 13);
1157 }
1158 }
1159
1160 /*
1161 * Finally, wait for transmitter to become empty
1162 * and restore the IER
1163 */
1164 wait_for_xmitr(up);
1165 serial_out(up, UART_IER, ier);
1166}
1167
1168static int __init serial8250_console_setup(struct console *co, char *options)
1169{
1170 struct uart_port *port;
1171 int baud = 9600;
1172 int bits = 8;
1173 int parity = 'n';
1174 int flow = 'n';
1175
1176 /*
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.
1180 */
1181 if (co->index >= UART_NR)
1182 co->index = 0;
1183 port = &serial8250_ports[co->index].port;
1184
1185 /*
1186 * Temporary fix.
1187 */
1188 spin_lock_init(&port->lock);
1189
1190 if (options)
1191 uart_parse_options(options, &baud, &parity, &bits, &flow);
1192
1193 return uart_set_options(port, co, baud, parity, bits, flow);
1194}
1195
1196extern struct uart_driver serial8250_reg;
1197static 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,
1205};
1206
1207static int __init serial8250_console_init(void)
1208{
1209 serial8250_isa_init_ports();
1210 register_console(&serial8250_console);
1211 return 0;
1212}
1213console_initcall(serial8250_console_init);
1214
1215#define SERIAL8250_CONSOLE &serial8250_console
1216#else
1217#define SERIAL8250_CONSOLE NULL
1218#endif
1219
1220static 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,
1229};
1230
1231int __init early_serial_setup(struct uart_port *port)
1232{
1233 serial8250_isa_init_ports();
1234 serial8250_ports[port->line].port = *port;
1235 serial8250_ports[port->line].port.ops = &serial8250_pops;
1236 return 0;
1237}
1238
1239/**
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
1243 *
1244 * Suspend one serial port.
1245 */
1246void serial8250_suspend_port(int line)
1247{
1248 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
1249}
1250
1251/**
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
1255 *
1256 * Resume one serial port.
1257 */
1258void serial8250_resume_port(int line)
1259{
1260 uart_resume_port(&serial8250_reg, &serial8250_ports[line].port);
1261}
1262
1263static int __init serial8250_init(void)
1264{
1265 int ret, i;
1266
1267 printk(KERN_INFO "Serial: Au1x00 driver\n");
1268
1269 for (i = 0; i < NR_IRQS; i++)
1270 spin_lock_init(&irq_lists[i].lock);
1271
1272 ret = uart_register_driver(&serial8250_reg);
1273 if (ret >= 0)
1274 serial8250_register_ports(&serial8250_reg);
1275
1276 return ret;
1277}
1278
1279static void __exit serial8250_exit(void)
1280{
1281 int i;
1282
1283 for (i = 0; i < UART_NR; i++)
1284 uart_remove_one_port(&serial8250_reg, &serial8250_ports[i].port);
1285
1286 uart_unregister_driver(&serial8250_reg);
1287}
1288
1289module_init(serial8250_init);
1290module_exit(serial8250_exit);
1291
1292EXPORT_SYMBOL(serial8250_suspend_port);
1293EXPORT_SYMBOL(serial8250_resume_port);
1294
1295MODULE_LICENSE("GPL");
1296MODULE_DESCRIPTION("Au1x00 serial driver\n");