vserver 2.0 rc7
[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         spin_unlock(&up->port.lock);
324         tty_flip_buffer_push(tty);
325         spin_lock(&up->port.lock);
326 }
327
328 static _INLINE_ void transmit_chars(struct uart_8250_port *up)
329 {
330         struct circ_buf *xmit = &up->port.info->xmit;
331         int count;
332
333         if (up->port.x_char) {
334                 serial_outp(up, UART_TX, up->port.x_char);
335                 up->port.icount.tx++;
336                 up->port.x_char = 0;
337                 return;
338         }
339         if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
340                 serial8250_stop_tx(&up->port, 0);
341                 return;
342         }
343
344         count = up->port.fifosize;
345         do {
346                 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
347                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
348                 up->port.icount.tx++;
349                 if (uart_circ_empty(xmit))
350                         break;
351         } while (--count > 0);
352
353         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
354                 uart_write_wakeup(&up->port);
355
356         DEBUG_INTR("THRE...");
357
358         if (uart_circ_empty(xmit))
359                 serial8250_stop_tx(&up->port, 0);
360 }
361
362 static _INLINE_ void check_modem_status(struct uart_8250_port *up)
363 {
364         int status;
365
366         status = serial_in(up, UART_MSR);
367
368         if ((status & UART_MSR_ANY_DELTA) == 0)
369                 return;
370
371         if (status & UART_MSR_TERI)
372                 up->port.icount.rng++;
373         if (status & UART_MSR_DDSR)
374                 up->port.icount.dsr++;
375         if (status & UART_MSR_DDCD)
376                 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
377         if (status & UART_MSR_DCTS)
378                 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
379
380         wake_up_interruptible(&up->port.info->delta_msr_wait);
381 }
382
383 /*
384  * This handles the interrupt from one port.
385  */
386 static inline void
387 serial8250_handle_port(struct uart_8250_port *up, struct pt_regs *regs)
388 {
389         unsigned int status = serial_inp(up, UART_LSR);
390
391         DEBUG_INTR("status = %x...", status);
392
393         if (status & UART_LSR_DR)
394                 receive_chars(up, &status, regs);
395         check_modem_status(up);
396         if (status & UART_LSR_THRE)
397                 transmit_chars(up);
398 }
399
400 /*
401  * This is the serial driver's interrupt routine.
402  *
403  * Arjan thinks the old way was overly complex, so it got simplified.
404  * Alan disagrees, saying that need the complexity to handle the weird
405  * nature of ISA shared interrupts.  (This is a special exception.)
406  *
407  * In order to handle ISA shared interrupts properly, we need to check
408  * that all ports have been serviced, and therefore the ISA interrupt
409  * line has been de-asserted.
410  *
411  * This means we need to loop through all ports. checking that they
412  * don't have an interrupt pending.
413  */
414 static irqreturn_t serial8250_interrupt(int irq, void *dev_id, struct pt_regs *regs)
415 {
416         struct irq_info *i = dev_id;
417         struct list_head *l, *end = NULL;
418         int pass_counter = 0;
419
420         DEBUG_INTR("serial8250_interrupt(%d)...", irq);
421
422         spin_lock(&i->lock);
423
424         l = i->head;
425         do {
426                 struct uart_8250_port *up;
427                 unsigned int iir;
428
429                 up = list_entry(l, struct uart_8250_port, list);
430
431                 iir = serial_in(up, UART_IIR);
432                 if (!(iir & UART_IIR_NO_INT)) {
433                         spin_lock(&up->port.lock);
434                         serial8250_handle_port(up, regs);
435                         spin_unlock(&up->port.lock);
436
437                         end = NULL;
438                 } else if (end == NULL)
439                         end = l;
440
441                 l = l->next;
442
443                 if (l == i->head && pass_counter++ > PASS_LIMIT) {
444                         /* If we hit this, we're dead. */
445                         printk(KERN_ERR "serial8250: too much work for "
446                                 "irq%d\n", irq);
447                         break;
448                 }
449         } while (l != end);
450
451         spin_unlock(&i->lock);
452
453         DEBUG_INTR("end.\n");
454         /* FIXME! Was it really ours? */
455         return IRQ_HANDLED;
456 }
457
458 /*
459  * To support ISA shared interrupts, we need to have one interrupt
460  * handler that ensures that the IRQ line has been deasserted
461  * before returning.  Failing to do this will result in the IRQ
462  * line being stuck active, and, since ISA irqs are edge triggered,
463  * no more IRQs will be seen.
464  */
465 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
466 {
467         spin_lock_irq(&i->lock);
468
469         if (!list_empty(i->head)) {
470                 if (i->head == &up->list)
471                         i->head = i->head->next;
472                 list_del(&up->list);
473         } else {
474                 BUG_ON(i->head != &up->list);
475                 i->head = NULL;
476         }
477
478         spin_unlock_irq(&i->lock);
479 }
480
481 static int serial_link_irq_chain(struct uart_8250_port *up)
482 {
483         struct irq_info *i = irq_lists + up->port.irq;
484         int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? SA_SHIRQ : 0;
485
486         spin_lock_irq(&i->lock);
487
488         if (i->head) {
489                 list_add(&up->list, i->head);
490                 spin_unlock_irq(&i->lock);
491
492                 ret = 0;
493         } else {
494                 INIT_LIST_HEAD(&up->list);
495                 i->head = &up->list;
496                 spin_unlock_irq(&i->lock);
497
498                 ret = request_irq(up->port.irq, serial8250_interrupt,
499                                   irq_flags, "serial", i);
500                 if (ret < 0)
501                         serial_do_unlink(i, up);
502         }
503
504         return ret;
505 }
506
507 static void serial_unlink_irq_chain(struct uart_8250_port *up)
508 {
509         struct irq_info *i = irq_lists + up->port.irq;
510
511         BUG_ON(i->head == NULL);
512
513         if (list_empty(i->head))
514                 free_irq(up->port.irq, i);
515
516         serial_do_unlink(i, up);
517 }
518
519 /*
520  * This function is used to handle ports that do not have an
521  * interrupt.  This doesn't work very well for 16450's, but gives
522  * barely passable results for a 16550A.  (Although at the expense
523  * of much CPU overhead).
524  */
525 static void serial8250_timeout(unsigned long data)
526 {
527         struct uart_8250_port *up = (struct uart_8250_port *)data;
528         unsigned int timeout;
529         unsigned int iir;
530
531         iir = serial_in(up, UART_IIR);
532         if (!(iir & UART_IIR_NO_INT)) {
533                 spin_lock(&up->port.lock);
534                 serial8250_handle_port(up, NULL);
535                 spin_unlock(&up->port.lock);
536         }
537
538         timeout = up->port.timeout;
539         timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
540         mod_timer(&up->timer, jiffies + timeout);
541 }
542
543 static unsigned int serial8250_tx_empty(struct uart_port *port)
544 {
545         struct uart_8250_port *up = (struct uart_8250_port *)port;
546         unsigned long flags;
547         unsigned int ret;
548
549         spin_lock_irqsave(&up->port.lock, flags);
550         ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
551         spin_unlock_irqrestore(&up->port.lock, flags);
552
553         return ret;
554 }
555
556 static unsigned int serial8250_get_mctrl(struct uart_port *port)
557 {
558         struct uart_8250_port *up = (struct uart_8250_port *)port;
559         unsigned long flags;
560         unsigned char status;
561         unsigned int ret;
562
563         spin_lock_irqsave(&up->port.lock, flags);
564         status = serial_in(up, UART_MSR);
565         spin_unlock_irqrestore(&up->port.lock, flags);
566
567         ret = 0;
568         if (status & UART_MSR_DCD)
569                 ret |= TIOCM_CAR;
570         if (status & UART_MSR_RI)
571                 ret |= TIOCM_RNG;
572         if (status & UART_MSR_DSR)
573                 ret |= TIOCM_DSR;
574         if (status & UART_MSR_CTS)
575                 ret |= TIOCM_CTS;
576         return ret;
577 }
578
579 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
580 {
581         struct uart_8250_port *up = (struct uart_8250_port *)port;
582         unsigned char mcr = 0;
583
584         if (mctrl & TIOCM_RTS)
585                 mcr |= UART_MCR_RTS;
586         if (mctrl & TIOCM_DTR)
587                 mcr |= UART_MCR_DTR;
588         if (mctrl & TIOCM_OUT1)
589                 mcr |= UART_MCR_OUT1;
590         if (mctrl & TIOCM_OUT2)
591                 mcr |= UART_MCR_OUT2;
592         if (mctrl & TIOCM_LOOP)
593                 mcr |= UART_MCR_LOOP;
594
595         mcr = (mcr & up->mcr_mask) | up->mcr_force;
596
597         serial_out(up, UART_MCR, mcr);
598 }
599
600 static void serial8250_break_ctl(struct uart_port *port, int break_state)
601 {
602         struct uart_8250_port *up = (struct uart_8250_port *)port;
603         unsigned long flags;
604
605         spin_lock_irqsave(&up->port.lock, flags);
606         if (break_state == -1)
607                 up->lcr |= UART_LCR_SBC;
608         else
609                 up->lcr &= ~UART_LCR_SBC;
610         serial_out(up, UART_LCR, up->lcr);
611         spin_unlock_irqrestore(&up->port.lock, flags);
612 }
613
614 static int serial8250_startup(struct uart_port *port)
615 {
616         struct uart_8250_port *up = (struct uart_8250_port *)port;
617         unsigned long flags;
618         int retval;
619
620         /*
621          * Clear the FIFO buffers and disable them.
622          * (they will be reeanbled in set_termios())
623          */
624         if (uart_config[up->port.type].flags & UART_CLEAR_FIFO) {
625                 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
626                 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
627                                 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
628                 serial_outp(up, UART_FCR, 0);
629         }
630
631         /*
632          * Clear the interrupt registers.
633          */
634         (void) serial_inp(up, UART_LSR);
635         (void) serial_inp(up, UART_RX);
636         (void) serial_inp(up, UART_IIR);
637         (void) serial_inp(up, UART_MSR);
638
639         /*
640          * At this point, there's no way the LSR could still be 0xff;
641          * if it is, then bail out, because there's likely no UART
642          * here.
643          */
644         if (!(up->port.flags & UPF_BUGGY_UART) &&
645             (serial_inp(up, UART_LSR) == 0xff)) {
646                 printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
647                 return -ENODEV;
648         }
649
650         retval = serial_link_irq_chain(up);
651                 if (retval)
652                         return retval;
653
654         /*
655          * Now, initialize the UART
656          */
657         serial_outp(up, UART_LCR, UART_LCR_WLEN8);
658
659         spin_lock_irqsave(&up->port.lock, flags);
660         if (up->port.flags & UPF_FOURPORT) {
661                 if (!is_real_interrupt(up->port.irq))
662                         up->port.mctrl |= TIOCM_OUT1;
663         } else
664                 /*
665                  * Most PC uarts need OUT2 raised to enable interrupts.
666                  */
667                 if (is_real_interrupt(up->port.irq))
668                         up->port.mctrl |= TIOCM_OUT2;
669
670         serial8250_set_mctrl(&up->port, up->port.mctrl);
671         spin_unlock_irqrestore(&up->port.lock, flags);
672
673         /*
674          * Finally, enable interrupts.  Note: Modem status interrupts
675          * are set via set_termios(), which will be occurring imminently
676          * anyway, so we don't enable them here.
677          */
678         up->ier = UART_IER_RLSI | UART_IER_RDI;
679         serial_outp(up, UART_IER, up->ier);
680
681         if (up->port.flags & UPF_FOURPORT) {
682                 unsigned int icp;
683                 /*
684                  * Enable interrupts on the AST Fourport board
685                  */
686                 icp = (up->port.iobase & 0xfe0) | 0x01f;
687                 outb_p(0x80, icp);
688                 (void) inb_p(icp);
689         }
690
691         /*
692          * And clear the interrupt registers again for luck.
693          */
694         (void) serial_inp(up, UART_LSR);
695         (void) serial_inp(up, UART_RX);
696         (void) serial_inp(up, UART_IIR);
697         (void) serial_inp(up, UART_MSR);
698
699         return 0;
700 }
701
702 static void serial8250_shutdown(struct uart_port *port)
703 {
704         struct uart_8250_port *up = (struct uart_8250_port *)port;
705         unsigned long flags;
706
707         /*
708          * Disable interrupts from this port
709          */
710         up->ier = 0;
711         serial_outp(up, UART_IER, 0);
712
713         spin_lock_irqsave(&up->port.lock, flags);
714         if (up->port.flags & UPF_FOURPORT) {
715                 /* reset interrupts on the AST Fourport board */
716                 inb((up->port.iobase & 0xfe0) | 0x1f);
717                 up->port.mctrl |= TIOCM_OUT1;
718         } else
719                 up->port.mctrl &= ~TIOCM_OUT2;
720
721         serial8250_set_mctrl(&up->port, up->port.mctrl);
722         spin_unlock_irqrestore(&up->port.lock, flags);
723
724         /*
725          * Disable break condition and FIFOs
726          */
727         serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
728         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
729                                   UART_FCR_CLEAR_RCVR |
730                                   UART_FCR_CLEAR_XMIT);
731         serial_outp(up, UART_FCR, 0);
732
733         /*
734          * Read data port to reset things, and then unlink from
735          * the IRQ chain.
736          */
737         (void) serial_in(up, UART_RX);
738
739         if (!is_real_interrupt(up->port.irq))
740                 del_timer_sync(&up->timer);
741         else
742                 serial_unlink_irq_chain(up);
743 }
744
745 static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
746 {
747         unsigned int quot;
748
749         /*
750          * Handle magic divisors for baud rates above baud_base on
751          * SMSC SuperIO chips.
752          */
753         if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
754             baud == (port->uartclk/4))
755                 quot = 0x8001;
756         else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
757                  baud == (port->uartclk/8))
758                 quot = 0x8002;
759         else
760                 quot = uart_get_divisor(port, baud);
761
762         return quot;
763 }
764
765 static void
766 serial8250_set_termios(struct uart_port *port, struct termios *termios,
767                        struct termios *old)
768 {
769         struct uart_8250_port *up = (struct uart_8250_port *)port;
770         unsigned char cval, fcr = 0;
771         unsigned long flags;
772         unsigned int baud, quot;
773
774         switch (termios->c_cflag & CSIZE) {
775         case CS5:
776                 cval = 0x00;
777                 break;
778         case CS6:
779                 cval = 0x01;
780                 break;
781         case CS7:
782                 cval = 0x02;
783                 break;
784         default:
785         case CS8:
786                 cval = 0x03;
787                 break;
788         }
789
790         if (termios->c_cflag & CSTOPB)
791                 cval |= 0x04;
792         if (termios->c_cflag & PARENB)
793                 cval |= UART_LCR_PARITY;
794         if (!(termios->c_cflag & PARODD))
795                 cval |= UART_LCR_EPAR;
796 #ifdef CMSPAR
797         if (termios->c_cflag & CMSPAR)
798                 cval |= UART_LCR_SPAR;
799 #endif
800
801         /*
802          * Ask the core to calculate the divisor for us.
803          */
804         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16); 
805         quot = serial8250_get_divisor(port, baud);
806         quot = 0x35; /* FIXME */
807
808         /*
809          * Work around a bug in the Oxford Semiconductor 952 rev B
810          * chip which causes it to seriously miscalculate baud rates
811          * when DLL is 0.
812          */
813         if ((quot & 0xff) == 0 && up->port.type == PORT_16C950 &&
814             up->rev == 0x5201)
815                 quot ++;
816
817         if (uart_config[up->port.type].flags & UART_USE_FIFO) {
818                 if (baud < 2400)
819                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_1;
820                 else
821                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_8;
822         }
823
824         /*
825          * Ok, we're now changing the port state.  Do it with
826          * interrupts disabled.
827          */
828         spin_lock_irqsave(&up->port.lock, flags);
829
830         /*
831          * Update the per-port timeout.
832          */
833         uart_update_timeout(port, termios->c_cflag, baud);
834
835         up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
836         if (termios->c_iflag & INPCK)
837                 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
838         if (termios->c_iflag & (BRKINT | PARMRK))
839                 up->port.read_status_mask |= UART_LSR_BI;
840
841         /*
842          * Characteres to ignore
843          */
844         up->port.ignore_status_mask = 0;
845         if (termios->c_iflag & IGNPAR)
846                 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
847         if (termios->c_iflag & IGNBRK) {
848                 up->port.ignore_status_mask |= UART_LSR_BI;
849                 /*
850                  * If we're ignoring parity and break indicators,
851                  * ignore overruns too (for real raw support).
852                  */
853                 if (termios->c_iflag & IGNPAR)
854                         up->port.ignore_status_mask |= UART_LSR_OE;
855         }
856
857         /*
858          * ignore all characters if CREAD is not set
859          */
860         if ((termios->c_cflag & CREAD) == 0)
861                 up->port.ignore_status_mask |= UART_LSR_DR;
862
863         /*
864          * CTS flow control flag and modem status interrupts
865          */
866         up->ier &= ~UART_IER_MSI;
867         if (UART_ENABLE_MS(&up->port, termios->c_cflag))
868                 up->ier |= UART_IER_MSI;
869
870         serial_out(up, UART_IER, up->ier);
871         serial_outp(up, 0x28, quot & 0xffff);
872         up->lcr = cval;                                 /* Save LCR */
873         if (up->port.type != PORT_16750) {
874                 if (fcr & UART_FCR_ENABLE_FIFO) {
875                         /* emulated UARTs (Lucent Venus 167x) need two steps */
876                         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
877                 }
878                 serial_outp(up, UART_FCR, fcr);         /* set fcr */
879         }
880         spin_unlock_irqrestore(&up->port.lock, flags);
881 }
882
883 static void
884 serial8250_pm(struct uart_port *port, unsigned int state,
885               unsigned int oldstate)
886 {
887         struct uart_8250_port *up = (struct uart_8250_port *)port;
888         if (state) {
889                 /* sleep */
890                 if (up->pm)
891                         up->pm(port, state, oldstate);
892         } else {
893                 /* wake */
894                 if (up->pm)
895                         up->pm(port, state, oldstate);
896         }
897 }
898
899 /*
900  * Resource handling.  This is complicated by the fact that resources
901  * depend on the port type.  Maybe we should be claiming the standard
902  * 8250 ports, and then trying to get other resources as necessary?
903  */
904 static int
905 serial8250_request_std_resource(struct uart_8250_port *up, struct resource **res)
906 {
907         unsigned int size = 8 << up->port.regshift;
908         int ret = 0;
909
910         switch (up->port.iotype) {
911         case SERIAL_IO_MEM:
912                 if (up->port.mapbase) {
913                         *res = request_mem_region(up->port.mapbase, size, "serial");
914                         if (!*res)
915                                 ret = -EBUSY;
916                 }
917                 break;
918
919         case SERIAL_IO_HUB6:
920         case SERIAL_IO_PORT:
921                 *res = request_region(up->port.iobase, size, "serial");
922                 if (!*res)
923                         ret = -EBUSY;
924                 break;
925         }
926         return ret;
927 }
928
929
930 static void serial8250_release_port(struct uart_port *port)
931 {
932         struct uart_8250_port *up = (struct uart_8250_port *)port;
933         unsigned long start, offset = 0, size = 0;
934
935         size <<= up->port.regshift;
936
937         switch (up->port.iotype) {
938         case SERIAL_IO_MEM:
939                 if (up->port.mapbase) {
940                         /*
941                          * Unmap the area.
942                          */
943                         iounmap(up->port.membase);
944                         up->port.membase = NULL;
945
946                         start = up->port.mapbase;
947
948                         if (size)
949                                 release_mem_region(start + offset, size);
950                         release_mem_region(start, 8 << up->port.regshift);
951                 }
952                 break;
953
954         case SERIAL_IO_HUB6:
955         case SERIAL_IO_PORT:
956                 start = up->port.iobase;
957
958                 if (size)
959                         release_region(start + offset, size);
960                 release_region(start + offset, 8 << up->port.regshift);
961                 break;
962
963         default:
964                 break;
965         }
966 }
967
968 static int serial8250_request_port(struct uart_port *port)
969 {
970         struct uart_8250_port *up = (struct uart_8250_port *)port;
971         struct resource *res = NULL, *res_rsa = NULL;
972         int ret = 0;
973
974         ret = serial8250_request_std_resource(up, &res);
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                 up->port.hub6     = old_serial_port[i].hub6;
1077                 up->port.membase  = old_serial_port[i].iomem_base;
1078                 up->port.iotype   = old_serial_port[i].io_type;
1079                 up->port.regshift = old_serial_port[i].iomem_reg_shift;
1080                 up->port.ops      = &serial8250_pops;
1081         }
1082 }
1083
1084 static void __init serial8250_register_ports(struct uart_driver *drv)
1085 {
1086         int i;
1087
1088         serial8250_isa_init_ports();
1089
1090         for (i = 0; i < UART_NR; i++) {
1091                 struct uart_8250_port *up = &serial8250_ports[i];
1092
1093                 up->port.line = i;
1094                 up->port.ops = &serial8250_pops;
1095                 init_timer(&up->timer);
1096                 up->timer.function = serial8250_timeout;
1097
1098                 /*
1099                  * ALPHA_KLUDGE_MCR needs to be killed.
1100                  */
1101                 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
1102                 up->mcr_force = ALPHA_KLUDGE_MCR;
1103
1104                 uart_add_one_port(drv, &up->port);
1105         }
1106 }
1107
1108 #ifdef CONFIG_SERIAL_AU1X00_CONSOLE
1109
1110 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1111
1112 /*
1113  *      Wait for transmitter & holding register to empty
1114  */
1115 static inline void wait_for_xmitr(struct uart_8250_port *up)
1116 {
1117         unsigned int status, tmout = 10000;
1118
1119         /* Wait up to 10ms for the character(s) to be sent. */
1120         do {
1121                 status = serial_in(up, UART_LSR);
1122
1123                 if (status & UART_LSR_BI)
1124                         up->lsr_break_flag = UART_LSR_BI;
1125
1126                 if (--tmout == 0)
1127                         break;
1128                 udelay(1);
1129         } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
1130
1131         /* Wait up to 1s for flow control if necessary */
1132         if (up->port.flags & UPF_CONS_FLOW) {
1133                 tmout = 1000000;
1134                 while (--tmout &&
1135                        ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
1136                         udelay(1);
1137         }
1138 }
1139
1140 /*
1141  *      Print a string to the serial port trying not to disturb
1142  *      any possible real use of the port...
1143  *
1144  *      The console_lock must be held when we get here.
1145  */
1146 static void
1147 serial8250_console_write(struct console *co, const char *s, unsigned int count)
1148 {
1149         struct uart_8250_port *up = &serial8250_ports[co->index];
1150         unsigned int ier;
1151         int i;
1152
1153         /*
1154          *      First save the UER then disable the interrupts
1155          */
1156         ier = serial_in(up, UART_IER);
1157         serial_out(up, UART_IER, 0);
1158
1159         /*
1160          *      Now, do each character
1161          */
1162         for (i = 0; i < count; i++, s++) {
1163                 wait_for_xmitr(up);
1164
1165                 /*
1166                  *      Send the character out.
1167                  *      If a LF, also do CR...
1168                  */
1169                 serial_out(up, UART_TX, *s);
1170                 if (*s == 10) {
1171                         wait_for_xmitr(up);
1172                         serial_out(up, UART_TX, 13);
1173                 }
1174         }
1175
1176         /*
1177          *      Finally, wait for transmitter to become empty
1178          *      and restore the IER
1179          */
1180         wait_for_xmitr(up);
1181         serial_out(up, UART_IER, ier);
1182 }
1183
1184 static int __init serial8250_console_setup(struct console *co, char *options)
1185 {
1186         struct uart_port *port;
1187         int baud = 9600;
1188         int bits = 8;
1189         int parity = 'n';
1190         int flow = 'n';
1191
1192         /*
1193          * Check whether an invalid uart number has been specified, and
1194          * if so, search for the first available port that does have
1195          * console support.
1196          */
1197         if (co->index >= UART_NR)
1198                 co->index = 0;
1199         port = &serial8250_ports[co->index].port;
1200
1201         /*
1202          * Temporary fix.
1203          */
1204         spin_lock_init(&port->lock);
1205
1206         if (options)
1207                 uart_parse_options(options, &baud, &parity, &bits, &flow);
1208
1209         return uart_set_options(port, co, baud, parity, bits, flow);
1210 }
1211
1212 extern struct uart_driver serial8250_reg;
1213 static struct console serial8250_console = {
1214         .name           = "ttyS",
1215         .write          = serial8250_console_write,
1216         .device         = uart_console_device,
1217         .setup          = serial8250_console_setup,
1218         .flags          = CON_PRINTBUFFER,
1219         .index          = -1,
1220         .data           = &serial8250_reg,
1221 };
1222
1223 static int __init serial8250_console_init(void)
1224 {
1225         serial8250_isa_init_ports();
1226         register_console(&serial8250_console);
1227         return 0;
1228 }
1229 console_initcall(serial8250_console_init);
1230
1231 #define SERIAL8250_CONSOLE      &serial8250_console
1232 #else
1233 #define SERIAL8250_CONSOLE      NULL
1234 #endif
1235
1236 static struct uart_driver serial8250_reg = {
1237         .owner                  = THIS_MODULE,
1238         .driver_name            = "serial",
1239         .devfs_name             = "tts/",
1240         .dev_name               = "ttyS",
1241         .major                  = TTY_MAJOR,
1242         .minor                  = 64,
1243         .nr                     = UART_NR,
1244         .cons                   = SERIAL8250_CONSOLE,
1245 };
1246
1247 int __init early_serial_setup(struct uart_port *port)
1248 {
1249         serial8250_isa_init_ports();
1250         serial8250_ports[port->line].port       = *port;
1251         serial8250_ports[port->line].port.ops   = &serial8250_pops;
1252         return 0;
1253 }
1254
1255 /**
1256  *      serial8250_suspend_port - suspend one serial port
1257  *      @line:  serial line number
1258  *      @level: the level of port suspension, as per uart_suspend_port
1259  *
1260  *      Suspend one serial port.
1261  */
1262 void serial8250_suspend_port(int line)
1263 {
1264         uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
1265 }
1266
1267 /**
1268  *      serial8250_resume_port - resume one serial port
1269  *      @line:  serial line number
1270  *      @level: the level of port resumption, as per uart_resume_port
1271  *
1272  *      Resume one serial port.
1273  */
1274 void serial8250_resume_port(int line)
1275 {
1276         uart_resume_port(&serial8250_reg, &serial8250_ports[line].port);
1277 }
1278
1279 static int __init serial8250_init(void)
1280 {
1281         int ret, i;
1282
1283         printk(KERN_INFO "Serial: Au1x00 driver\n");
1284
1285         for (i = 0; i < NR_IRQS; i++)
1286                 spin_lock_init(&irq_lists[i].lock);
1287
1288         ret = uart_register_driver(&serial8250_reg);
1289         if (ret >= 0)
1290                 serial8250_register_ports(&serial8250_reg);
1291
1292         return ret;
1293 }
1294
1295 static void __exit serial8250_exit(void)
1296 {
1297         int i;
1298
1299         for (i = 0; i < UART_NR; i++)
1300                 uart_remove_one_port(&serial8250_reg, &serial8250_ports[i].port);
1301
1302         uart_unregister_driver(&serial8250_reg);
1303 }
1304
1305 module_init(serial8250_init);
1306 module_exit(serial8250_exit);
1307
1308 EXPORT_SYMBOL(serial8250_suspend_port);
1309 EXPORT_SYMBOL(serial8250_resume_port);
1310
1311 MODULE_LICENSE("GPL");
1312 MODULE_DESCRIPTION("Au1x00 serial driver\n");