ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / serial / au1x00_uart.c
1 /*
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
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
93         }
94 };
95
96 #define UART_NR ARRAY_SIZE(old_serial_port)
97
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;
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
117 struct irq_info {
118         spinlock_t              lock;
119         struct list_head        *head;
120 };
121
122 static 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  */
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 },
134 };
135
136 static _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
141 static _INLINE_ void
142 serial_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  */
157 static 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
203 static void serial8250_stop_tx(struct uart_port *port, unsigned int tty_stop)
204 {
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
213 static void serial8250_start_tx(struct uart_port *port, unsigned int tty_start)
214 {
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
223 static 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
232 static 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
240 static _INLINE_ void
241 receive_chars(struct uart_8250_port *up, int *status, struct pt_regs *regs)
242 {
243         struct tty_struct *tty = up->port.info->tty;
244         unsigned char ch;
245         int max_count = 256;
246
247         do {
248                 if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
249                         tty->flip.work.func((void *)tty);
250                         if (tty->flip.count >= TTY_FLIPBUF_SIZE)
251                                 return; // if TTY_DONT_FLIP is set
252                 }
253                 ch = serial_inp(up, UART_RX);
254                 *tty->flip.char_buf_ptr = ch;
255                 *tty->flip.flag_buf_ptr = TTY_NORMAL;
256                 up->port.icount.rx++;
257
258                 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
259                                        UART_LSR_FE | UART_LSR_OE))) {
260                         /*
261                          * For statistics only
262                          */
263                         if (*status & UART_LSR_BI) {
264                                 *status &= ~(UART_LSR_FE | UART_LSR_PE);
265                                 up->port.icount.brk++;
266                                 /*
267                                  * We do the SysRQ and SAK checking
268                                  * here because otherwise the break
269                                  * may get masked by ignore_status_mask
270                                  * or read_status_mask.
271                                  */
272                                 if (uart_handle_break(&up->port))
273                                         goto ignore_char;
274                         } else if (*status & UART_LSR_PE)
275                                 up->port.icount.parity++;
276                         else if (*status & UART_LSR_FE)
277                                 up->port.icount.frame++;
278                         if (*status & UART_LSR_OE)
279                                 up->port.icount.overrun++;
280
281                         /*
282                          * Mask off conditions which should be ingored.
283                          */
284                         *status &= up->port.read_status_mask;
285
286 #ifdef CONFIG_SERIAL_AU1X00_CONSOLE
287                         if (up->port.line == up->port.cons->index) {
288                                 /* Recover the break flag from console xmit */
289                                 *status |= up->lsr_break_flag;
290                                 up->lsr_break_flag = 0;
291                         }
292 #endif
293                         if (*status & UART_LSR_BI) {
294                                 DEBUG_INTR("handling break....");
295                                 *tty->flip.flag_buf_ptr = TTY_BREAK;
296                         } else if (*status & UART_LSR_PE)
297                                 *tty->flip.flag_buf_ptr = TTY_PARITY;
298                         else if (*status & UART_LSR_FE)
299                                 *tty->flip.flag_buf_ptr = TTY_FRAME;
300                 }
301                 if (uart_handle_sysrq_char(&up->port, ch, regs))
302                         goto ignore_char;
303                 if ((*status & up->port.ignore_status_mask) == 0) {
304                         tty->flip.flag_buf_ptr++;
305                         tty->flip.char_buf_ptr++;
306                         tty->flip.count++;
307                 }
308                 if ((*status & UART_LSR_OE) &&
309                     tty->flip.count < TTY_FLIPBUF_SIZE) {
310                         /*
311                          * Overrun is special, since it's reported
312                          * immediately, and doesn't affect the current
313                          * character.
314                          */
315                         *tty->flip.flag_buf_ptr = TTY_OVERRUN;
316                         tty->flip.flag_buf_ptr++;
317                         tty->flip.char_buf_ptr++;
318                         tty->flip.count++;
319                 }
320         ignore_char:
321                 *status = serial_inp(up, UART_LSR);
322         } while ((*status & UART_LSR_DR) && (max_count-- > 0));
323         tty_flip_buffer_push(tty);
324 }
325
326 static _INLINE_ void transmit_chars(struct uart_8250_port *up)
327 {
328         struct circ_buf *xmit = &up->port.info->xmit;
329         int count;
330
331         if (up->port.x_char) {
332                 serial_outp(up, UART_TX, up->port.x_char);
333                 up->port.icount.tx++;
334                 up->port.x_char = 0;
335                 return;
336         }
337         if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
338                 serial8250_stop_tx(&up->port, 0);
339                 return;
340         }
341
342         count = up->port.fifosize;
343         do {
344                 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
345                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
346                 up->port.icount.tx++;
347                 if (uart_circ_empty(xmit))
348                         break;
349         } while (--count > 0);
350
351         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
352                 uart_write_wakeup(&up->port);
353
354         DEBUG_INTR("THRE...");
355
356         if (uart_circ_empty(xmit))
357                 serial8250_stop_tx(&up->port, 0);
358 }
359
360 static _INLINE_ void check_modem_status(struct uart_8250_port *up)
361 {
362         int status;
363
364         status = serial_in(up, UART_MSR);
365
366         if ((status & UART_MSR_ANY_DELTA) == 0)
367                 return;
368
369         if (status & UART_MSR_TERI)
370                 up->port.icount.rng++;
371         if (status & UART_MSR_DDSR)
372                 up->port.icount.dsr++;
373         if (status & UART_MSR_DDCD)
374                 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
375         if (status & UART_MSR_DCTS)
376                 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
377
378         wake_up_interruptible(&up->port.info->delta_msr_wait);
379 }
380
381 /*
382  * This handles the interrupt from one port.
383  */
384 static inline void
385 serial8250_handle_port(struct uart_8250_port *up, struct pt_regs *regs)
386 {
387         unsigned int status = serial_inp(up, UART_LSR);
388
389         DEBUG_INTR("status = %x...", status);
390
391         if (status & UART_LSR_DR)
392                 receive_chars(up, &status, regs);
393         check_modem_status(up);
394         if (status & UART_LSR_THRE)
395                 transmit_chars(up);
396 }
397
398 /*
399  * This is the serial driver's interrupt routine.
400  *
401  * Arjan thinks the old way was overly complex, so it got simplified.
402  * Alan disagrees, saying that need the complexity to handle the weird
403  * nature of ISA shared interrupts.  (This is a special exception.)
404  *
405  * In order to handle ISA shared interrupts properly, we need to check
406  * that all ports have been serviced, and therefore the ISA interrupt
407  * line has been de-asserted.
408  *
409  * This means we need to loop through all ports. checking that they
410  * don't have an interrupt pending.
411  */
412 static irqreturn_t serial8250_interrupt(int irq, void *dev_id, struct pt_regs *regs)
413 {
414         struct irq_info *i = dev_id;
415         struct list_head *l, *end = NULL;
416         int pass_counter = 0;
417
418         DEBUG_INTR("serial8250_interrupt(%d)...", irq);
419
420         spin_lock(&i->lock);
421
422         l = i->head;
423         do {
424                 struct uart_8250_port *up;
425                 unsigned int iir;
426
427                 up = list_entry(l, struct uart_8250_port, list);
428
429                 iir = serial_in(up, UART_IIR);
430                 if (!(iir & UART_IIR_NO_INT)) {
431                         spin_lock(&up->port.lock);
432                         serial8250_handle_port(up, regs);
433                         spin_unlock(&up->port.lock);
434
435                         end = NULL;
436                 } else if (end == NULL)
437                         end = l;
438
439                 l = l->next;
440
441                 if (l == i->head && pass_counter++ > PASS_LIMIT) {
442                         /* If we hit this, we're dead. */
443                         printk(KERN_ERR "serial8250: too much work for "
444                                 "irq%d\n", irq);
445                         break;
446                 }
447         } while (l != end);
448
449         spin_unlock(&i->lock);
450
451         DEBUG_INTR("end.\n");
452         /* FIXME! Was it really ours? */
453         return IRQ_HANDLED;
454 }
455
456 /*
457  * To support ISA shared interrupts, we need to have one interrupt
458  * handler that ensures that the IRQ line has been deasserted
459  * before returning.  Failing to do this will result in the IRQ
460  * line being stuck active, and, since ISA irqs are edge triggered,
461  * no more IRQs will be seen.
462  */
463 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
464 {
465         spin_lock_irq(&i->lock);
466
467         if (!list_empty(i->head)) {
468                 if (i->head == &up->list)
469                         i->head = i->head->next;
470                 list_del(&up->list);
471         } else {
472                 BUG_ON(i->head != &up->list);
473                 i->head = NULL;
474         }
475
476         spin_unlock_irq(&i->lock);
477 }
478
479 static int serial_link_irq_chain(struct uart_8250_port *up)
480 {
481         struct irq_info *i = irq_lists + up->port.irq;
482         int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? SA_SHIRQ : 0;
483
484         spin_lock_irq(&i->lock);
485
486         if (i->head) {
487                 list_add(&up->list, i->head);
488                 spin_unlock_irq(&i->lock);
489
490                 ret = 0;
491         } else {
492                 INIT_LIST_HEAD(&up->list);
493                 i->head = &up->list;
494                 spin_unlock_irq(&i->lock);
495
496                 ret = request_irq(up->port.irq, serial8250_interrupt,
497                                   irq_flags, "serial", i);
498                 if (ret < 0)
499                         serial_do_unlink(i, up);
500         }
501
502         return ret;
503 }
504
505 static void serial_unlink_irq_chain(struct uart_8250_port *up)
506 {
507         struct irq_info *i = irq_lists + up->port.irq;
508
509         BUG_ON(i->head == NULL);
510
511         if (list_empty(i->head))
512                 free_irq(up->port.irq, i);
513
514         serial_do_unlink(i, up);
515 }
516
517 /*
518  * This function is used to handle ports that do not have an
519  * interrupt.  This doesn't work very well for 16450's, but gives
520  * barely passable results for a 16550A.  (Although at the expense
521  * of much CPU overhead).
522  */
523 static void serial8250_timeout(unsigned long data)
524 {
525         struct uart_8250_port *up = (struct uart_8250_port *)data;
526         unsigned int timeout;
527         unsigned int iir;
528
529         iir = serial_in(up, UART_IIR);
530         if (!(iir & UART_IIR_NO_INT)) {
531                 spin_lock(&up->port.lock);
532                 serial8250_handle_port(up, NULL);
533                 spin_unlock(&up->port.lock);
534         }
535
536         timeout = up->port.timeout;
537         timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
538         mod_timer(&up->timer, jiffies + timeout);
539 }
540
541 static unsigned int serial8250_tx_empty(struct uart_port *port)
542 {
543         struct uart_8250_port *up = (struct uart_8250_port *)port;
544         unsigned long flags;
545         unsigned int ret;
546
547         spin_lock_irqsave(&up->port.lock, flags);
548         ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
549         spin_unlock_irqrestore(&up->port.lock, flags);
550
551         return ret;
552 }
553
554 static unsigned int serial8250_get_mctrl(struct uart_port *port)
555 {
556         struct uart_8250_port *up = (struct uart_8250_port *)port;
557         unsigned long flags;
558         unsigned char status;
559         unsigned int ret;
560
561         spin_lock_irqsave(&up->port.lock, flags);
562         status = serial_in(up, UART_MSR);
563         spin_unlock_irqrestore(&up->port.lock, flags);
564
565         ret = 0;
566         if (status & UART_MSR_DCD)
567                 ret |= TIOCM_CAR;
568         if (status & UART_MSR_RI)
569                 ret |= TIOCM_RNG;
570         if (status & UART_MSR_DSR)
571                 ret |= TIOCM_DSR;
572         if (status & UART_MSR_CTS)
573                 ret |= TIOCM_CTS;
574         return ret;
575 }
576
577 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
578 {
579         struct uart_8250_port *up = (struct uart_8250_port *)port;
580         unsigned char mcr = 0;
581
582         if (mctrl & TIOCM_RTS)
583                 mcr |= UART_MCR_RTS;
584         if (mctrl & TIOCM_DTR)
585                 mcr |= UART_MCR_DTR;
586         if (mctrl & TIOCM_OUT1)
587                 mcr |= UART_MCR_OUT1;
588         if (mctrl & TIOCM_OUT2)
589                 mcr |= UART_MCR_OUT2;
590         if (mctrl & TIOCM_LOOP)
591                 mcr |= UART_MCR_LOOP;
592
593         mcr = (mcr & up->mcr_mask) | up->mcr_force;
594
595         serial_out(up, UART_MCR, mcr);
596 }
597
598 static void serial8250_break_ctl(struct uart_port *port, int break_state)
599 {
600         struct uart_8250_port *up = (struct uart_8250_port *)port;
601         unsigned long flags;
602
603         spin_lock_irqsave(&up->port.lock, flags);
604         if (break_state == -1)
605                 up->lcr |= UART_LCR_SBC;
606         else
607                 up->lcr &= ~UART_LCR_SBC;
608         serial_out(up, UART_LCR, up->lcr);
609         spin_unlock_irqrestore(&up->port.lock, flags);
610 }
611
612 static int serial8250_startup(struct uart_port *port)
613 {
614         struct uart_8250_port *up = (struct uart_8250_port *)port;
615         unsigned long flags;
616         int retval;
617
618         /*
619          * Clear the FIFO buffers and disable them.
620          * (they will be reeanbled in set_termios())
621          */
622         if (uart_config[up->port.type].flags & UART_CLEAR_FIFO) {
623                 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
624                 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
625                                 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
626                 serial_outp(up, UART_FCR, 0);
627         }
628
629         /*
630          * Clear the interrupt registers.
631          */
632         (void) serial_inp(up, UART_LSR);
633         (void) serial_inp(up, UART_RX);
634         (void) serial_inp(up, UART_IIR);
635         (void) serial_inp(up, UART_MSR);
636
637         /*
638          * At this point, there's no way the LSR could still be 0xff;
639          * if it is, then bail out, because there's likely no UART
640          * here.
641          */
642         if (!(up->port.flags & UPF_BUGGY_UART) &&
643             (serial_inp(up, UART_LSR) == 0xff)) {
644                 printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
645                 return -ENODEV;
646         }
647
648         retval = serial_link_irq_chain(up);
649                 if (retval)
650                         return retval;
651
652         /*
653          * Now, initialize the UART
654          */
655         serial_outp(up, UART_LCR, UART_LCR_WLEN8);
656
657         spin_lock_irqsave(&up->port.lock, flags);
658         if (up->port.flags & UPF_FOURPORT) {
659                 if (!is_real_interrupt(up->port.irq))
660                         up->port.mctrl |= TIOCM_OUT1;
661         } else
662                 /*
663                  * Most PC uarts need OUT2 raised to enable interrupts.
664                  */
665                 if (is_real_interrupt(up->port.irq))
666                         up->port.mctrl |= TIOCM_OUT2;
667
668         serial8250_set_mctrl(&up->port, up->port.mctrl);
669         spin_unlock_irqrestore(&up->port.lock, flags);
670
671         /*
672          * Finally, enable interrupts.  Note: Modem status interrupts
673          * are set via set_termios(), which will be occurring imminently
674          * anyway, so we don't enable them here.
675          */
676         up->ier = UART_IER_RLSI | UART_IER_RDI;
677         serial_outp(up, UART_IER, up->ier);
678
679         if (up->port.flags & UPF_FOURPORT) {
680                 unsigned int icp;
681                 /*
682                  * Enable interrupts on the AST Fourport board
683                  */
684                 icp = (up->port.iobase & 0xfe0) | 0x01f;
685                 outb_p(0x80, icp);
686                 (void) inb_p(icp);
687         }
688
689         /*
690          * And clear the interrupt registers again for luck.
691          */
692         (void) serial_inp(up, UART_LSR);
693         (void) serial_inp(up, UART_RX);
694         (void) serial_inp(up, UART_IIR);
695         (void) serial_inp(up, UART_MSR);
696
697         return 0;
698 }
699
700 static void serial8250_shutdown(struct uart_port *port)
701 {
702         struct uart_8250_port *up = (struct uart_8250_port *)port;
703         unsigned long flags;
704
705         /*
706          * Disable interrupts from this port
707          */
708         up->ier = 0;
709         serial_outp(up, UART_IER, 0);
710
711         spin_lock_irqsave(&up->port.lock, flags);
712         if (up->port.flags & UPF_FOURPORT) {
713                 /* reset interrupts on the AST Fourport board */
714                 inb((up->port.iobase & 0xfe0) | 0x1f);
715                 up->port.mctrl |= TIOCM_OUT1;
716         } else
717                 up->port.mctrl &= ~TIOCM_OUT2;
718
719         serial8250_set_mctrl(&up->port, up->port.mctrl);
720         spin_unlock_irqrestore(&up->port.lock, flags);
721
722         /*
723          * Disable break condition and FIFOs
724          */
725         serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
726         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
727                                   UART_FCR_CLEAR_RCVR |
728                                   UART_FCR_CLEAR_XMIT);
729         serial_outp(up, UART_FCR, 0);
730
731         /*
732          * Read data port to reset things, and then unlink from
733          * the IRQ chain.
734          */
735         (void) serial_in(up, UART_RX);
736
737         if (!is_real_interrupt(up->port.irq))
738                 del_timer_sync(&up->timer);
739         else
740                 serial_unlink_irq_chain(up);
741 }
742
743 static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
744 {
745         unsigned int quot;
746
747         /*
748          * Handle magic divisors for baud rates above baud_base on
749          * SMSC SuperIO chips.
750          */
751         if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
752             baud == (port->uartclk/4))
753                 quot = 0x8001;
754         else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
755                  baud == (port->uartclk/8))
756                 quot = 0x8002;
757         else
758                 quot = uart_get_divisor(port, baud);
759
760         return quot;
761 }
762
763 static void
764 serial8250_set_termios(struct uart_port *port, struct termios *termios,
765                        struct termios *old)
766 {
767         struct uart_8250_port *up = (struct uart_8250_port *)port;
768         unsigned char cval, fcr = 0;
769         unsigned long flags;
770         unsigned int baud, quot;
771
772         switch (termios->c_cflag & CSIZE) {
773         case CS5:
774                 cval = 0x00;
775                 break;
776         case CS6:
777                 cval = 0x01;
778                 break;
779         case CS7:
780                 cval = 0x02;
781                 break;
782         default:
783         case CS8:
784                 cval = 0x03;
785                 break;
786         }
787
788         if (termios->c_cflag & CSTOPB)
789                 cval |= 0x04;
790         if (termios->c_cflag & PARENB)
791                 cval |= UART_LCR_PARITY;
792         if (!(termios->c_cflag & PARODD))
793                 cval |= UART_LCR_EPAR;
794 #ifdef CMSPAR
795         if (termios->c_cflag & CMSPAR)
796                 cval |= UART_LCR_SPAR;
797 #endif
798
799         /*
800          * Ask the core to calculate the divisor for us.
801          */
802         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16); 
803         quot = serial8250_get_divisor(port, baud);
804         quot = 0x35; /* FIXME */
805
806         /*
807          * Work around a bug in the Oxford Semiconductor 952 rev B
808          * chip which causes it to seriously miscalculate baud rates
809          * when DLL is 0.
810          */
811         if ((quot & 0xff) == 0 && up->port.type == PORT_16C950 &&
812             up->rev == 0x5201)
813                 quot ++;
814
815         if (uart_config[up->port.type].flags & UART_USE_FIFO) {
816                 if (baud < 2400)
817                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_1;
818                 else
819                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_8;
820         }
821
822         /*
823          * Ok, we're now changing the port state.  Do it with
824          * interrupts disabled.
825          */
826         spin_lock_irqsave(&up->port.lock, flags);
827
828         /*
829          * Update the per-port timeout.
830          */
831         uart_update_timeout(port, termios->c_cflag, baud);
832
833         up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
834         if (termios->c_iflag & INPCK)
835                 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
836         if (termios->c_iflag & (BRKINT | PARMRK))
837                 up->port.read_status_mask |= UART_LSR_BI;
838
839         /*
840          * Characteres to ignore
841          */
842         up->port.ignore_status_mask = 0;
843         if (termios->c_iflag & IGNPAR)
844                 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
845         if (termios->c_iflag & IGNBRK) {
846                 up->port.ignore_status_mask |= UART_LSR_BI;
847                 /*
848                  * If we're ignoring parity and break indicators,
849                  * ignore overruns too (for real raw support).
850                  */
851                 if (termios->c_iflag & IGNPAR)
852                         up->port.ignore_status_mask |= UART_LSR_OE;
853         }
854
855         /*
856          * ignore all characters if CREAD is not set
857          */
858         if ((termios->c_cflag & CREAD) == 0)
859                 up->port.ignore_status_mask |= UART_LSR_DR;
860
861         /*
862          * CTS flow control flag and modem status interrupts
863          */
864         up->ier &= ~UART_IER_MSI;
865         if (UART_ENABLE_MS(&up->port, termios->c_cflag))
866                 up->ier |= UART_IER_MSI;
867
868         serial_out(up, UART_IER, up->ier);
869         serial_outp(up, 0x28, quot & 0xffff);
870         up->lcr = cval;                                 /* Save LCR */
871         if (up->port.type != PORT_16750) {
872                 if (fcr & UART_FCR_ENABLE_FIFO) {
873                         /* emulated UARTs (Lucent Venus 167x) need two steps */
874                         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
875                 }
876                 serial_outp(up, UART_FCR, fcr);         /* set fcr */
877         }
878         spin_unlock_irqrestore(&up->port.lock, flags);
879 }
880
881 static void
882 serial8250_pm(struct uart_port *port, unsigned int state,
883               unsigned int oldstate)
884 {
885         struct uart_8250_port *up = (struct uart_8250_port *)port;
886         if (state) {
887                 /* sleep */
888                 if (up->pm)
889                         up->pm(port, state, oldstate);
890         } else {
891                 /* wake */
892                 if (up->pm)
893                         up->pm(port, state, oldstate);
894         }
895 }
896
897 /*
898  * Resource handling.  This is complicated by the fact that resources
899  * depend on the port type.  Maybe we should be claiming the standard
900  * 8250 ports, and then trying to get other resources as necessary?
901  */
902 static int
903 serial8250_request_std_resource(struct uart_8250_port *up, struct resource **res)
904 {
905         unsigned int size = 8 << up->port.regshift;
906         int ret = 0;
907
908         switch (up->port.iotype) {
909         case SERIAL_IO_MEM:
910                 if (up->port.mapbase) {
911                         *res = request_mem_region(up->port.mapbase, size, "serial");
912                         if (!*res)
913                                 ret = -EBUSY;
914                 }
915                 break;
916
917         case SERIAL_IO_HUB6:
918         case SERIAL_IO_PORT:
919                 *res = request_region(up->port.iobase, size, "serial");
920                 if (!*res)
921                         ret = -EBUSY;
922                 break;
923         }
924         return ret;
925 }
926
927
928 static void serial8250_release_port(struct uart_port *port)
929 {
930         struct uart_8250_port *up = (struct uart_8250_port *)port;
931         unsigned long start, offset = 0, size = 0;
932
933         size <<= up->port.regshift;
934
935         switch (up->port.iotype) {
936         case SERIAL_IO_MEM:
937                 if (up->port.mapbase) {
938                         /*
939                          * Unmap the area.
940                          */
941                         iounmap(up->port.membase);
942                         up->port.membase = NULL;
943
944                         start = up->port.mapbase;
945
946                         if (size)
947                                 release_mem_region(start + offset, size);
948                         release_mem_region(start, 8 << up->port.regshift);
949                 }
950                 break;
951
952         case SERIAL_IO_HUB6:
953         case SERIAL_IO_PORT:
954                 start = up->port.iobase;
955
956                 if (size)
957                         release_region(start + offset, size);
958                 release_region(start + offset, 8 << up->port.regshift);
959                 break;
960
961         default:
962                 break;
963         }
964 }
965
966 static int serial8250_request_port(struct uart_port *port)
967 {
968         struct uart_8250_port *up = (struct uart_8250_port *)port;
969         struct resource *res = NULL, *res_rsa = NULL;
970         int ret = 0;
971
972         if (up->port.flags & UPF_RESOURCES) {
973                 ret = serial8250_request_std_resource(up, &res);
974         }
975
976         /*
977          * If we have a mapbase, then request that as well.
978          */
979         if (ret == 0 && up->port.flags & UPF_IOREMAP) {
980                 int size = res->end - res->start + 1;
981
982                 up->port.membase = ioremap(up->port.mapbase, size);
983                 if (!up->port.membase)
984                         ret = -ENOMEM;
985         }
986
987         if (ret < 0) {
988                 if (res_rsa)
989                         release_resource(res_rsa);
990                 if (res)
991                         release_resource(res);
992         }
993         return ret;
994 }
995
996 static void serial8250_config_port(struct uart_port *port, int flags)
997 {
998         struct uart_8250_port *up = (struct uart_8250_port *)port;
999         struct resource *res_std = NULL, *res_rsa = NULL;
1000         int probeflags = PROBE_ANY;
1001
1002         probeflags &= ~PROBE_RSA;
1003
1004         if (flags & UART_CONFIG_TYPE)
1005                 autoconfig(up, probeflags);
1006
1007         /*
1008          * If the port wasn't an RSA port, release the resource.
1009          */
1010         if (up->port.type != PORT_RSA && res_rsa)
1011                 release_resource(res_rsa);
1012
1013         if (up->port.type == PORT_UNKNOWN && res_std)
1014                 release_resource(res_std);
1015 }
1016
1017 static int
1018 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
1019 {
1020         if (ser->irq >= NR_IRQS || ser->irq < 0 ||
1021             ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
1022             ser->type > PORT_MAX_8250 || ser->type == PORT_CIRRUS ||
1023             ser->type == PORT_STARTECH)
1024                 return -EINVAL;
1025         return 0;
1026 }
1027
1028 static const char *
1029 serial8250_type(struct uart_port *port)
1030 {
1031         int type = port->type;
1032
1033         if (type >= ARRAY_SIZE(uart_config))
1034                 type = 0;
1035         return uart_config[type].name;
1036 }
1037
1038 static struct uart_ops serial8250_pops = {
1039         .tx_empty       = serial8250_tx_empty,
1040         .set_mctrl      = serial8250_set_mctrl,
1041         .get_mctrl      = serial8250_get_mctrl,
1042         .stop_tx        = serial8250_stop_tx,
1043         .start_tx       = serial8250_start_tx,
1044         .stop_rx        = serial8250_stop_rx,
1045         .enable_ms      = serial8250_enable_ms,
1046         .break_ctl      = serial8250_break_ctl,
1047         .startup        = serial8250_startup,
1048         .shutdown       = serial8250_shutdown,
1049         .set_termios    = serial8250_set_termios,
1050         .pm             = serial8250_pm,
1051         .type           = serial8250_type,
1052         .release_port   = serial8250_release_port,
1053         .request_port   = serial8250_request_port,
1054         .config_port    = serial8250_config_port,
1055         .verify_port    = serial8250_verify_port,
1056 };
1057
1058 static struct uart_8250_port serial8250_ports[UART_NR];
1059
1060 static void __init serial8250_isa_init_ports(void)
1061 {
1062         struct uart_8250_port *up;
1063         static int first = 1;
1064         int i;
1065
1066         if (!first)
1067                 return;
1068         first = 0;
1069
1070         for (i = 0, up = serial8250_ports; i < ARRAY_SIZE(old_serial_port);
1071              i++, up++) {
1072                 up->port.iobase   = old_serial_port[i].port;
1073                 up->port.irq      = old_serial_port[i].irq;
1074                 up->port.uartclk  = get_au1x00_uart_baud_base();
1075                 up->port.flags    = old_serial_port[i].flags |
1076                                     UPF_RESOURCES;
1077                 up->port.hub6     = old_serial_port[i].hub6;
1078                 up->port.membase  = old_serial_port[i].iomem_base;
1079                 up->port.iotype   = old_serial_port[i].io_type;
1080                 up->port.regshift = old_serial_port[i].iomem_reg_shift;
1081                 up->port.ops      = &serial8250_pops;
1082         }
1083 }
1084
1085 static void __init serial8250_register_ports(struct uart_driver *drv)
1086 {
1087         int i;
1088
1089         serial8250_isa_init_ports();
1090
1091         for (i = 0; i < UART_NR; i++) {
1092                 struct uart_8250_port *up = &serial8250_ports[i];
1093
1094                 up->port.line = i;
1095                 up->port.ops = &serial8250_pops;
1096                 init_timer(&up->timer);
1097                 up->timer.function = serial8250_timeout;
1098
1099                 /*
1100                  * ALPHA_KLUDGE_MCR needs to be killed.
1101                  */
1102                 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
1103                 up->mcr_force = ALPHA_KLUDGE_MCR;
1104
1105                 uart_add_one_port(drv, &up->port);
1106         }
1107 }
1108
1109 #ifdef CONFIG_SERIAL_AU1X00_CONSOLE
1110
1111 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1112
1113 /*
1114  *      Wait for transmitter & holding register to empty
1115  */
1116 static inline void wait_for_xmitr(struct uart_8250_port *up)
1117 {
1118         unsigned int status, tmout = 10000;
1119
1120         /* Wait up to 10ms for the character(s) to be sent. */
1121         do {
1122                 status = serial_in(up, UART_LSR);
1123
1124                 if (status & UART_LSR_BI)
1125                         up->lsr_break_flag = UART_LSR_BI;
1126
1127                 if (--tmout == 0)
1128                         break;
1129                 udelay(1);
1130         } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
1131
1132         /* Wait up to 1s for flow control if necessary */
1133         if (up->port.flags & UPF_CONS_FLOW) {
1134                 tmout = 1000000;
1135                 while (--tmout &&
1136                        ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
1137                         udelay(1);
1138         }
1139 }
1140
1141 /*
1142  *      Print a string to the serial port trying not to disturb
1143  *      any possible real use of the port...
1144  *
1145  *      The console_lock must be held when we get here.
1146  */
1147 static void
1148 serial8250_console_write(struct console *co, const char *s, unsigned int count)
1149 {
1150         struct uart_8250_port *up = &serial8250_ports[co->index];
1151         unsigned int ier;
1152         int i;
1153
1154         /*
1155          *      First save the UER then disable the interrupts
1156          */
1157         ier = serial_in(up, UART_IER);
1158         serial_out(up, UART_IER, 0);
1159
1160         /*
1161          *      Now, do each character
1162          */
1163         for (i = 0; i < count; i++, s++) {
1164                 wait_for_xmitr(up);
1165
1166                 /*
1167                  *      Send the character out.
1168                  *      If a LF, also do CR...
1169                  */
1170                 serial_out(up, UART_TX, *s);
1171                 if (*s == 10) {
1172                         wait_for_xmitr(up);
1173                         serial_out(up, UART_TX, 13);
1174                 }
1175         }
1176
1177         /*
1178          *      Finally, wait for transmitter to become empty
1179          *      and restore the IER
1180          */
1181         wait_for_xmitr(up);
1182         serial_out(up, UART_IER, ier);
1183 }
1184
1185 static int __init serial8250_console_setup(struct console *co, char *options)
1186 {
1187         struct uart_port *port;
1188         int baud = 9600;
1189         int bits = 8;
1190         int parity = 'n';
1191         int flow = 'n';
1192
1193         /*
1194          * Check whether an invalid uart number has been specified, and
1195          * if so, search for the first available port that does have
1196          * console support.
1197          */
1198         if (co->index >= UART_NR)
1199                 co->index = 0;
1200         port = &serial8250_ports[co->index].port;
1201
1202         /*
1203          * Temporary fix.
1204          */
1205         spin_lock_init(&port->lock);
1206
1207         if (options)
1208                 uart_parse_options(options, &baud, &parity, &bits, &flow);
1209
1210         return uart_set_options(port, co, baud, parity, bits, flow);
1211 }
1212
1213 extern struct uart_driver serial8250_reg;
1214 static struct console serial8250_console = {
1215         .name           = "ttyS",
1216         .write          = serial8250_console_write,
1217         .device         = uart_console_device,
1218         .setup          = serial8250_console_setup,
1219         .flags          = CON_PRINTBUFFER,
1220         .index          = -1,
1221         .data           = &serial8250_reg,
1222 };
1223
1224 static int __init serial8250_console_init(void)
1225 {
1226         serial8250_isa_init_ports();
1227         register_console(&serial8250_console);
1228         return 0;
1229 }
1230 console_initcall(serial8250_console_init);
1231
1232 #define SERIAL8250_CONSOLE      &serial8250_console
1233 #else
1234 #define SERIAL8250_CONSOLE      NULL
1235 #endif
1236
1237 static struct uart_driver serial8250_reg = {
1238         .owner                  = THIS_MODULE,
1239         .driver_name            = "serial",
1240         .devfs_name             = "tts/",
1241         .dev_name               = "ttyS",
1242         .major                  = TTY_MAJOR,
1243         .minor                  = 64,
1244         .nr                     = UART_NR,
1245         .cons                   = SERIAL8250_CONSOLE,
1246 };
1247
1248 /*
1249  * register_serial and unregister_serial allows for 16x50 serial ports to be
1250  * configured at run-time, to support PCMCIA modems.
1251  */
1252
1253 static int __register_serial(struct serial_struct *req, int line)
1254 {
1255         struct uart_port port;
1256
1257         port.iobase   = req->port;
1258         port.membase  = req->iomem_base;
1259         port.irq      = req->irq;
1260         port.uartclk  = req->baud_base * 16;
1261         port.fifosize = req->xmit_fifo_size;
1262         port.regshift = req->iomem_reg_shift;
1263         port.iotype   = req->io_type;
1264         port.flags    = req->flags | UPF_BOOT_AUTOCONF;
1265         port.mapbase  = req->iomap_base;
1266         port.line     = line;
1267
1268         if (HIGH_BITS_OFFSET)
1269                 port.iobase |= (long) req->port_high << HIGH_BITS_OFFSET;
1270
1271         /*
1272          * If a clock rate wasn't specified by the low level
1273          * driver, then default to the standard clock rate.
1274          */
1275         if (port.uartclk == 0)
1276                 port.uartclk = BASE_BAUD * 16;
1277
1278         return uart_register_port(&serial8250_reg, &port);
1279 }
1280
1281 /**
1282  *      register_serial - configure a 16x50 serial port at runtime
1283  *      @req: request structure
1284  *
1285  *      Configure the serial port specified by the request. If the
1286  *      port exists and is in use an error is returned. If the port
1287  *      is not currently in the table it is added.
1288  *
1289  *      The port is then probed and if necessary the IRQ is autodetected
1290  *      If this fails an error is returned.
1291  *
1292  *      On success the port is ready to use and the line number is returned.
1293  */
1294 int register_serial(struct serial_struct *req)
1295 {
1296         return __register_serial(req, -1);
1297 }
1298
1299 int __init early_serial_setup(struct uart_port *port)
1300 {
1301         serial8250_isa_init_ports();
1302         serial8250_ports[port->line].port       = *port;
1303         serial8250_ports[port->line].port.ops   = &serial8250_pops;
1304         return 0;
1305 }
1306
1307 /**
1308  *      unregister_serial - remove a 16x50 serial port at runtime
1309  *      @line: serial line number
1310  *
1311  *      Remove one serial port.  This may be called from interrupt
1312  *      context.
1313  */
1314 void unregister_serial(int line)
1315 {
1316         uart_unregister_port(&serial8250_reg, line);
1317 }
1318
1319 /*
1320  * This is for ISAPNP only.
1321  */
1322 void serial8250_get_irq_map(unsigned int *map)
1323 {
1324         int i;
1325
1326         for (i = 0; i < UART_NR; i++) {
1327                 if (serial8250_ports[i].port.type != PORT_UNKNOWN &&
1328                     serial8250_ports[i].port.irq < 16)
1329                         *map |= 1 << serial8250_ports[i].port.irq;
1330         }
1331 }
1332
1333 /**
1334  *      serial8250_suspend_port - suspend one serial port
1335  *      @line:  serial line number
1336  *      @level: the level of port suspension, as per uart_suspend_port
1337  *
1338  *      Suspend one serial port.
1339  */
1340 void serial8250_suspend_port(int line)
1341 {
1342         uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
1343 }
1344
1345 /**
1346  *      serial8250_resume_port - resume one serial port
1347  *      @line:  serial line number
1348  *      @level: the level of port resumption, as per uart_resume_port
1349  *
1350  *      Resume one serial port.
1351  */
1352 void serial8250_resume_port(int line)
1353 {
1354         uart_resume_port(&serial8250_reg, &serial8250_ports[line].port);
1355 }
1356
1357 static int __init serial8250_init(void)
1358 {
1359         int ret, i;
1360
1361         printk(KERN_INFO "Serial: Au1x00 driver\n");
1362
1363         for (i = 0; i < NR_IRQS; i++)
1364                 spin_lock_init(&irq_lists[i].lock);
1365
1366         ret = uart_register_driver(&serial8250_reg);
1367         if (ret >= 0)
1368                 serial8250_register_ports(&serial8250_reg);
1369
1370         return ret;
1371 }
1372
1373 static void __exit serial8250_exit(void)
1374 {
1375         int i;
1376
1377         for (i = 0; i < UART_NR; i++)
1378                 uart_remove_one_port(&serial8250_reg, &serial8250_ports[i].port);
1379
1380         uart_unregister_driver(&serial8250_reg);
1381 }
1382
1383 module_init(serial8250_init);
1384 module_exit(serial8250_exit);
1385
1386 EXPORT_SYMBOL(register_serial);
1387 EXPORT_SYMBOL(unregister_serial);
1388 EXPORT_SYMBOL(serial8250_get_irq_map);
1389 EXPORT_SYMBOL(serial8250_suspend_port);
1390 EXPORT_SYMBOL(serial8250_resume_port);
1391
1392 MODULE_LICENSE("GPL");
1393 MODULE_DESCRIPTION("Au1x00 serial driver\n");