ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / serial / 8250.c
1 /*
2  *  linux/drivers/char/8250.c
3  *
4  *  Driver for 8250/16550-type serial ports
5  *
6  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7  *
8  *  Copyright (C) 2001 Russell King.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  *  $Id: 8250.c,v 1.90 2002/07/28 10:03:27 rmk Exp $
16  *
17  * A note about mapbase / membase
18  *
19  *  mapbase is the physical address of the IO port.
20  *  membase is an 'ioremapped' cookie.
21  */
22 #include <linux/config.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/tty.h>
26 #include <linux/ioport.h>
27 #include <linux/init.h>
28 #include <linux/console.h>
29 #include <linux/sysrq.h>
30 #include <linux/serial_reg.h>
31 #include <linux/serial.h>
32 #include <linux/serialP.h>
33 #include <linux/delay.h>
34 #include <linux/device.h>
35
36 #include <asm/io.h>
37 #include <asm/irq.h>
38
39 #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
40 #define SUPPORT_SYSRQ
41 #endif
42
43 #include <linux/serial_core.h>
44 #include "8250.h"
45
46 /*
47  * Configuration:
48  *   share_irqs - whether we pass SA_SHIRQ to request_irq().  This option
49  *                is unsafe when used on edge-triggered interrupts.
50  */
51 unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
52
53 /*
54  * Debugging.
55  */
56 #if 0
57 #define DEBUG_AUTOCONF(fmt...)  printk(fmt)
58 #else
59 #define DEBUG_AUTOCONF(fmt...)  do { } while (0)
60 #endif
61
62 #if 0
63 #define DEBUG_INTR(fmt...)      printk(fmt)
64 #else
65 #define DEBUG_INTR(fmt...)      do { } while (0)
66 #endif
67
68 #define PASS_LIMIT      256
69
70 /*
71  * We default to IRQ0 for the "no irq" hack.   Some
72  * machine types want others as well - they're free
73  * to redefine this in their header file.
74  */
75 #define is_real_interrupt(irq)  ((irq) != 0)
76
77 /*
78  * This converts from our new CONFIG_ symbols to the symbols
79  * that asm/serial.h expects.  You _NEED_ to comment out the
80  * linux/config.h include contained inside asm/serial.h for
81  * this to work.
82  */
83 #undef CONFIG_SERIAL_MANY_PORTS
84 #undef CONFIG_SERIAL_DETECT_IRQ
85 #undef CONFIG_SERIAL_MULTIPORT
86 #undef CONFIG_HUB6
87
88 #ifdef CONFIG_SERIAL_8250_DETECT_IRQ
89 #define CONFIG_SERIAL_DETECT_IRQ 1
90 #endif
91 #ifdef CONFIG_SERIAL_8250_MULTIPORT
92 #define CONFIG_SERIAL_MULTIPORT 1
93 #endif
94 #ifdef CONFIG_SERIAL_8250_MANY_PORTS
95 #define CONFIG_SERIAL_MANY_PORTS 1
96 #endif
97
98 /*
99  * HUB6 is always on.  This will be removed once the header
100  * files have been cleaned.
101  */
102 #define CONFIG_HUB6 1
103
104 #include <asm/serial.h>
105
106 /*
107  * SERIAL_PORT_DFNS tells us about built-in ports that have no
108  * standard enumeration mechanism.   Platforms that can find all
109  * serial ports via mechanisms like ACPI or PCI need not supply it.
110  */
111 #ifndef SERIAL_PORT_DFNS
112 #define SERIAL_PORT_DFNS
113 #endif
114
115 static struct old_serial_port old_serial_port[] = {
116         SERIAL_PORT_DFNS /* defined in asm/serial.h */
117 };
118
119 #define UART_NR (ARRAY_SIZE(old_serial_port) + CONFIG_SERIAL_8250_NR_UARTS)
120
121 #ifdef CONFIG_SERIAL_8250_RSA
122
123 #define PORT_RSA_MAX 4
124 static unsigned long probe_rsa[PORT_RSA_MAX];
125 static unsigned int probe_rsa_count;
126 #endif /* CONFIG_SERIAL_8250_RSA  */
127
128 struct uart_8250_port {
129         struct uart_port        port;
130         struct timer_list       timer;          /* "no irq" timer */
131         struct list_head        list;           /* ports on this IRQ */
132         unsigned int            capabilities;   /* port capabilities */
133         unsigned short          rev;
134         unsigned char           acr;
135         unsigned char           ier;
136         unsigned char           lcr;
137         unsigned char           mcr;
138         unsigned char           mcr_mask;       /* mask of user bits */
139         unsigned char           mcr_force;      /* mask of forced bits */
140         unsigned char           lsr_break_flag;
141
142         /*
143          * We provide a per-port pm hook.
144          */
145         void                    (*pm)(struct uart_port *port,
146                                       unsigned int state, unsigned int old);
147 };
148
149 struct irq_info {
150         spinlock_t              lock;
151         struct list_head        *head;
152 };
153
154 static struct irq_info irq_lists[NR_IRQS];
155
156 /*
157  * Here we define the default xmit fifo size used for each type of UART.
158  */
159 static const struct serial_uart_config uart_config[PORT_MAX_8250+1] = {
160         { "unknown",    1,      0 },
161         { "8250",       1,      0 },
162         { "16450",      1,      0 },
163         { "16550",      1,      0 },
164         { "16550A",     16,     UART_CLEAR_FIFO | UART_USE_FIFO },
165         { "Cirrus",     1,      0 },
166         { "ST16650",    1,      UART_CLEAR_FIFO | UART_STARTECH },
167         { "ST16650V2",  32,     UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
168         { "TI16750",    64,     UART_CLEAR_FIFO | UART_USE_FIFO },
169         { "Startech",   1,      0 },
170         { "16C950/954", 128,    UART_CLEAR_FIFO | UART_USE_FIFO },
171         { "ST16654",    64,     UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
172         { "XR16850",    128,    UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
173         { "RSA",        2048,   UART_CLEAR_FIFO | UART_USE_FIFO },
174         { "NS16550A",   16,     UART_CLEAR_FIFO | UART_USE_FIFO | UART_NATSEMI },
175         { "XScale",     32,     UART_CLEAR_FIFO | UART_USE_FIFO  },
176 };
177
178 static _INLINE_ unsigned int serial_in(struct uart_8250_port *up, int offset)
179 {
180         offset <<= up->port.regshift;
181
182         switch (up->port.iotype) {
183         case UPIO_HUB6:
184                 outb(up->port.hub6 - 1 + offset, up->port.iobase);
185                 return inb(up->port.iobase + 1);
186
187         case UPIO_MEM:
188                 return readb(up->port.membase + offset);
189
190         default:
191                 return inb(up->port.iobase + offset);
192         }
193 }
194
195 static _INLINE_ void
196 serial_out(struct uart_8250_port *up, int offset, int value)
197 {
198         offset <<= up->port.regshift;
199
200         switch (up->port.iotype) {
201         case UPIO_HUB6:
202                 outb(up->port.hub6 - 1 + offset, up->port.iobase);
203                 outb(value, up->port.iobase + 1);
204                 break;
205
206         case UPIO_MEM:
207                 writeb(value, up->port.membase + offset);
208                 break;
209
210         default:
211                 outb(value, up->port.iobase + offset);
212         }
213 }
214
215 /*
216  * We used to support using pause I/O for certain machines.  We
217  * haven't supported this for a while, but just in case it's badly
218  * needed for certain old 386 machines, I've left these #define's
219  * in....
220  */
221 #define serial_inp(up, offset)          serial_in(up, offset)
222 #define serial_outp(up, offset, value)  serial_out(up, offset, value)
223
224
225 /*
226  * For the 16C950
227  */
228 static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
229 {
230         serial_out(up, UART_SCR, offset);
231         serial_out(up, UART_ICR, value);
232 }
233
234 static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
235 {
236         unsigned int value;
237
238         serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
239         serial_out(up, UART_SCR, offset);
240         value = serial_in(up, UART_ICR);
241         serial_icr_write(up, UART_ACR, up->acr);
242
243         return value;
244 }
245
246 #ifdef CONFIG_SERIAL_8250_RSA
247 /*
248  * Attempts to turn on the RSA FIFO.  Returns zero on failure.
249  * We set the port uart clock rate if we succeed.
250  */
251 static int __enable_rsa(struct uart_8250_port *up)
252 {
253         unsigned char mode;
254         int result;
255
256         mode = serial_inp(up, UART_RSA_MSR);
257         result = mode & UART_RSA_MSR_FIFO;
258
259         if (!result) {
260                 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
261                 mode = serial_inp(up, UART_RSA_MSR);
262                 result = mode & UART_RSA_MSR_FIFO;
263         }
264
265         if (result)
266                 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
267
268         return result;
269 }
270
271 static void enable_rsa(struct uart_8250_port *up)
272 {
273         if (up->port.type == PORT_RSA) {
274                 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
275                         spin_lock_irq(&up->port.lock);
276                         __enable_rsa(up);
277                         spin_unlock_irq(&up->port.lock);
278                 }
279                 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
280                         serial_outp(up, UART_RSA_FRR, 0);
281         }
282 }
283
284 /*
285  * Attempts to turn off the RSA FIFO.  Returns zero on failure.
286  * It is unknown why interrupts were disabled in here.  However,
287  * the caller is expected to preserve this behaviour by grabbing
288  * the spinlock before calling this function.
289  */
290 static void disable_rsa(struct uart_8250_port *up)
291 {
292         unsigned char mode;
293         int result;
294
295         if (up->port.type == PORT_RSA &&
296             up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
297                 spin_lock_irq(&up->port.lock);
298
299                 mode = serial_inp(up, UART_RSA_MSR);
300                 result = !(mode & UART_RSA_MSR_FIFO);
301
302                 if (!result) {
303                         serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
304                         mode = serial_inp(up, UART_RSA_MSR);
305                         result = !(mode & UART_RSA_MSR_FIFO);
306                 }
307
308                 if (result)
309                         up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
310                 spin_unlock_irq(&up->port.lock);
311         }
312 }
313 #endif /* CONFIG_SERIAL_8250_RSA */
314
315 /*
316  * This is a quickie test to see how big the FIFO is.
317  * It doesn't work at all the time, more's the pity.
318  */
319 static int size_fifo(struct uart_8250_port *up)
320 {
321         unsigned char old_fcr, old_mcr, old_dll, old_dlm;
322         int count;
323
324         old_fcr = serial_inp(up, UART_FCR);
325         old_mcr = serial_inp(up, UART_MCR);
326         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
327                     UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
328         serial_outp(up, UART_MCR, UART_MCR_LOOP);
329         serial_outp(up, UART_LCR, UART_LCR_DLAB);
330         old_dll = serial_inp(up, UART_DLL);
331         old_dlm = serial_inp(up, UART_DLM);
332         serial_outp(up, UART_DLL, 0x01);
333         serial_outp(up, UART_DLM, 0x00);
334         serial_outp(up, UART_LCR, 0x03);
335         for (count = 0; count < 256; count++)
336                 serial_outp(up, UART_TX, count);
337         mdelay(20);/* FIXME - schedule_timeout */
338         for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
339              (count < 256); count++)
340                 serial_inp(up, UART_RX);
341         serial_outp(up, UART_FCR, old_fcr);
342         serial_outp(up, UART_MCR, old_mcr);
343         serial_outp(up, UART_LCR, UART_LCR_DLAB);
344         serial_outp(up, UART_DLL, old_dll);
345         serial_outp(up, UART_DLM, old_dlm);
346
347         return count;
348 }
349
350 /*
351  * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
352  * When this function is called we know it is at least a StarTech
353  * 16650 V2, but it might be one of several StarTech UARTs, or one of
354  * its clones.  (We treat the broken original StarTech 16650 V1 as a
355  * 16550, and why not?  Startech doesn't seem to even acknowledge its
356  * existence.)
357  * 
358  * What evil have men's minds wrought...
359  */
360 static void autoconfig_has_efr(struct uart_8250_port *up)
361 {
362         unsigned char id1, id2, id3, rev, saved_dll, saved_dlm;
363
364         /*
365          * First we check to see if it's an Oxford Semiconductor UART.
366          *
367          * If we have to do this here because some non-National
368          * Semiconductor clone chips lock up if you try writing to the
369          * LSR register (which serial_icr_read does)
370          */
371
372         /*
373          * Check for Oxford Semiconductor 16C950.
374          *
375          * EFR [4] must be set else this test fails.
376          *
377          * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
378          * claims that it's needed for 952 dual UART's (which are not
379          * recommended for new designs).
380          */
381         up->acr = 0;
382         serial_out(up, UART_LCR, 0xBF);
383         serial_out(up, UART_EFR, UART_EFR_ECB);
384         serial_out(up, UART_LCR, 0x00);
385         id1 = serial_icr_read(up, UART_ID1);
386         id2 = serial_icr_read(up, UART_ID2);
387         id3 = serial_icr_read(up, UART_ID3);
388         rev = serial_icr_read(up, UART_REV);
389
390         DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
391
392         if (id1 == 0x16 && id2 == 0xC9 &&
393             (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
394                 up->port.type = PORT_16C950;
395                 up->rev = rev | (id3 << 8);
396                 return;
397         }
398         
399         /*
400          * We check for a XR16C850 by setting DLL and DLM to 0, and then
401          * reading back DLL and DLM.  The chip type depends on the DLM
402          * value read back:
403          *  0x10 - XR16C850 and the DLL contains the chip revision.
404          *  0x12 - XR16C2850.
405          *  0x14 - XR16C854.
406          */
407         serial_outp(up, UART_LCR, UART_LCR_DLAB);
408         saved_dll = serial_inp(up, UART_DLL);
409         saved_dlm = serial_inp(up, UART_DLM);
410         serial_outp(up, UART_DLL, 0);
411         serial_outp(up, UART_DLM, 0);
412         id2 = serial_inp(up, UART_DLL);
413         id1 = serial_inp(up, UART_DLM);
414         serial_outp(up, UART_DLL, saved_dll);
415         serial_outp(up, UART_DLM, saved_dlm);
416
417         DEBUG_AUTOCONF("850id=%02x:%02x ", id1, id2);
418
419         if (id1 == 0x10 || id1 == 0x12 || id1 == 0x14) {
420                 if (id1 == 0x10)
421                         up->rev = id2;
422                 up->port.type = PORT_16850;
423                 return;
424         }
425
426         /*
427          * It wasn't an XR16C850.
428          *
429          * We distinguish between the '654 and the '650 by counting
430          * how many bytes are in the FIFO.  I'm using this for now,
431          * since that's the technique that was sent to me in the
432          * serial driver update, but I'm not convinced this works.
433          * I've had problems doing this in the past.  -TYT
434          */
435         if (size_fifo(up) == 64)
436                 up->port.type = PORT_16654;
437         else
438                 up->port.type = PORT_16650V2;
439 }
440
441 /*
442  * We detected a chip without a FIFO.  Only two fall into
443  * this category - the original 8250 and the 16450.  The
444  * 16450 has a scratch register (accessible with LCR=0)
445  */
446 static void autoconfig_8250(struct uart_8250_port *up)
447 {
448         unsigned char scratch, status1, status2;
449
450         up->port.type = PORT_8250;
451
452         scratch = serial_in(up, UART_SCR);
453         serial_outp(up, UART_SCR, 0xa5);
454         status1 = serial_in(up, UART_SCR);
455         serial_outp(up, UART_SCR, 0x5a);
456         status2 = serial_in(up, UART_SCR);
457         serial_outp(up, UART_SCR, scratch);
458
459         if (status1 == 0xa5 && status2 == 0x5a)
460                 up->port.type = PORT_16450;
461 }
462
463 /*
464  * We know that the chip has FIFOs.  Does it have an EFR?  The
465  * EFR is located in the same register position as the IIR and
466  * we know the top two bits of the IIR are currently set.  The
467  * EFR should contain zero.  Try to read the EFR.
468  */
469 static void autoconfig_16550a(struct uart_8250_port *up)
470 {
471         unsigned char status1, status2;
472
473         up->port.type = PORT_16550A;
474
475         /*
476          * Check for presence of the EFR when DLAB is set.
477          * Only ST16C650V1 UARTs pass this test.
478          */
479         serial_outp(up, UART_LCR, UART_LCR_DLAB);
480         if (serial_in(up, UART_EFR) == 0) {
481                 serial_outp(up, UART_EFR, 0xA8);
482                 if (serial_in(up, UART_EFR) != 0) {
483                         DEBUG_AUTOCONF("EFRv1 ");
484                         up->port.type = PORT_16650;
485                 } else {
486                         DEBUG_AUTOCONF("Motorola 8xxx DUART ");
487                 }
488                 serial_outp(up, UART_EFR, 0);
489                 return;
490         }
491
492         /*
493          * Maybe it requires 0xbf to be written to the LCR.
494          * (other ST16C650V2 UARTs, TI16C752A, etc)
495          */
496         serial_outp(up, UART_LCR, 0xBF);
497         if (serial_in(up, UART_EFR) == 0) {
498                 DEBUG_AUTOCONF("EFRv2 ");
499                 autoconfig_has_efr(up);
500                 return;
501         }
502
503         /*
504          * Check for a National Semiconductor SuperIO chip.
505          * Attempt to switch to bank 2, read the value of the LOOP bit
506          * from EXCR1. Switch back to bank 0, change it in MCR. Then
507          * switch back to bank 2, read it from EXCR1 again and check
508          * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
509          * On PowerPC we don't want to change baud_base, as we have
510          * a number of different divisors.  -- Tom Rini
511          */
512         serial_outp(up, UART_LCR, 0);
513         status1 = serial_in(up, UART_MCR);
514         serial_outp(up, UART_LCR, 0xE0);
515         status2 = serial_in(up, 0x02); /* EXCR1 */
516
517         if (!((status2 ^ status1) & UART_MCR_LOOP)) {
518                 serial_outp(up, UART_LCR, 0);
519                 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
520                 serial_outp(up, UART_LCR, 0xE0);
521                 status2 = serial_in(up, 0x02); /* EXCR1 */
522                 serial_outp(up, UART_LCR, 0);
523                 serial_outp(up, UART_MCR, status1);
524
525                 if ((status2 ^ status1) & UART_MCR_LOOP) {
526 #ifndef CONFIG_PPC
527                         serial_outp(up, UART_LCR, 0xE0);
528                         status1 = serial_in(up, 0x04); /* EXCR1 */
529                         status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
530                         status1 |= 0x10;  /* 1.625 divisor for baud_base --> 921600 */
531                         serial_outp(up, 0x04, status1);
532                         serial_outp(up, UART_LCR, 0);
533                         up->port.uartclk = 921600*16;
534 #endif
535
536                         up->port.type = PORT_NS16550A;
537                         return;
538                 }
539         }
540
541         /*
542          * No EFR.  Try to detect a TI16750, which only sets bit 5 of
543          * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
544          * Try setting it with and without DLAB set.  Cheap clones
545          * set bit 5 without DLAB set.
546          */
547         serial_outp(up, UART_LCR, 0);
548         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
549         status1 = serial_in(up, UART_IIR) >> 5;
550         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
551         serial_outp(up, UART_LCR, UART_LCR_DLAB);
552         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
553         status2 = serial_in(up, UART_IIR) >> 5;
554         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
555
556         DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
557
558         if (status1 == 6 && status2 == 7) {
559                 up->port.type = PORT_16750;
560                 return;
561         }
562 }
563
564 /*
565  * This routine is called by rs_init() to initialize a specific serial
566  * port.  It determines what type of UART chip this serial port is
567  * using: 8250, 16450, 16550, 16550A.  The important question is
568  * whether or not this UART is a 16550A or not, since this will
569  * determine whether or not we can use its FIFO features or not.
570  */
571 static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
572 {
573         unsigned char status1, scratch, scratch2, scratch3;
574         unsigned char save_lcr, save_mcr;
575         unsigned long flags;
576
577         if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
578                 return;
579
580         DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%p): ",
581                         up->port.line, up->port.iobase, up->port.membase);
582
583         /*
584          * We really do need global IRQs disabled here - we're going to
585          * be frobbing the chips IRQ enable register to see if it exists.
586          */
587         spin_lock_irqsave(&up->port.lock, flags);
588 //      save_flags(flags); cli();
589
590         if (!(up->port.flags & UPF_BUGGY_UART)) {
591                 /*
592                  * Do a simple existence test first; if we fail this,
593                  * there's no point trying anything else.
594                  * 
595                  * 0x80 is used as a nonsense port to prevent against
596                  * false positives due to ISA bus float.  The
597                  * assumption is that 0x80 is a non-existent port;
598                  * which should be safe since include/asm/io.h also
599                  * makes this assumption.
600                  *
601                  * Note: this is safe as long as MCR bit 4 is clear
602                  * and the device is in "PC" mode.
603                  */
604                 scratch = serial_inp(up, UART_IER);
605                 serial_outp(up, UART_IER, 0);
606 #ifdef __i386__
607                 outb(0xff, 0x080);
608 #endif
609                 scratch2 = serial_inp(up, UART_IER);
610                 serial_outp(up, UART_IER, 0x0F);
611 #ifdef __i386__
612                 outb(0, 0x080);
613 #endif
614                 scratch3 = serial_inp(up, UART_IER);
615                 serial_outp(up, UART_IER, scratch);
616                 if (scratch2 != 0 || scratch3 != 0x0F) {
617                         /*
618                          * We failed; there's nothing here
619                          */
620                         DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
621                                        scratch2, scratch3);
622                         goto out;
623                 }
624         }
625
626         save_mcr = serial_in(up, UART_MCR);
627         save_lcr = serial_in(up, UART_LCR);
628
629         /* 
630          * Check to see if a UART is really there.  Certain broken
631          * internal modems based on the Rockwell chipset fail this
632          * test, because they apparently don't implement the loopback
633          * test mode.  So this test is skipped on the COM 1 through
634          * COM 4 ports.  This *should* be safe, since no board
635          * manufacturer would be stupid enough to design a board
636          * that conflicts with COM 1-4 --- we hope!
637          */
638         if (!(up->port.flags & UPF_SKIP_TEST)) {
639                 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
640                 status1 = serial_inp(up, UART_MSR) & 0xF0;
641                 serial_outp(up, UART_MCR, save_mcr);
642                 if (status1 != 0x90) {
643                         DEBUG_AUTOCONF("LOOP test failed (%02x) ",
644                                        status1);
645                         goto out;
646                 }
647         }
648
649         /*
650          * We're pretty sure there's a port here.  Lets find out what
651          * type of port it is.  The IIR top two bits allows us to find
652          * out if its 8250 or 16450, 16550, 16550A or later.  This
653          * determines what we test for next.
654          *
655          * We also initialise the EFR (if any) to zero for later.  The
656          * EFR occupies the same register location as the FCR and IIR.
657          */
658         serial_outp(up, UART_LCR, 0xBF);
659         serial_outp(up, UART_EFR, 0);
660         serial_outp(up, UART_LCR, 0);
661
662         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
663         scratch = serial_in(up, UART_IIR) >> 6;
664
665         DEBUG_AUTOCONF("iir=%d ", scratch);
666
667         switch (scratch) {
668         case 0:
669                 autoconfig_8250(up);
670                 break;
671         case 1:
672                 up->port.type = PORT_UNKNOWN;
673                 break;
674         case 2:
675                 up->port.type = PORT_16550;
676                 break;
677         case 3:
678                 autoconfig_16550a(up);
679                 break;
680         }
681
682 #ifdef CONFIG_SERIAL_8250_RSA
683         /*
684          * Only probe for RSA ports if we got the region.
685          */
686         if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
687                 int i;
688
689                 for (i = 0 ; i < probe_rsa_count; ++i) {
690                         if (probe_rsa[i] == up->port.iobase &&
691                             __enable_rsa(up)) {
692                                 up->port.type = PORT_RSA;
693                                 break;
694                         }
695                 }
696         }
697 #endif
698         serial_outp(up, UART_LCR, save_lcr);
699
700         up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;
701         up->capabilities = uart_config[up->port.type].flags;
702
703         if (up->port.type == PORT_UNKNOWN)
704                 goto out;
705
706         /*
707          * Reset the UART.
708          */
709 #ifdef CONFIG_SERIAL_8250_RSA
710         if (up->port.type == PORT_RSA)
711                 serial_outp(up, UART_RSA_FRR, 0);
712 #endif
713         serial_outp(up, UART_MCR, save_mcr);
714         serial_outp(up, UART_FCR, (UART_FCR_ENABLE_FIFO |
715                                      UART_FCR_CLEAR_RCVR |
716                                      UART_FCR_CLEAR_XMIT));
717         serial_outp(up, UART_FCR, 0);
718         (void)serial_in(up, UART_RX);
719         serial_outp(up, UART_IER, 0);
720
721  out:   
722         spin_unlock_irqrestore(&up->port.lock, flags);
723 //      restore_flags(flags);
724         DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
725 }
726
727 static void autoconfig_irq(struct uart_8250_port *up)
728 {
729         unsigned char save_mcr, save_ier;
730         unsigned char save_ICP = 0;
731         unsigned int ICP = 0;
732         unsigned long irqs;
733         int irq;
734
735         if (up->port.flags & UPF_FOURPORT) {
736                 ICP = (up->port.iobase & 0xfe0) | 0x1f;
737                 save_ICP = inb_p(ICP);
738                 outb_p(0x80, ICP);
739                 (void) inb_p(ICP);
740         }
741
742         /* forget possible initially masked and pending IRQ */
743         probe_irq_off(probe_irq_on());
744         save_mcr = serial_inp(up, UART_MCR);
745         save_ier = serial_inp(up, UART_IER);
746         serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
747         
748         irqs = probe_irq_on();
749         serial_outp(up, UART_MCR, 0);
750         udelay (10);
751         if (up->port.flags & UPF_FOURPORT)  {
752                 serial_outp(up, UART_MCR,
753                             UART_MCR_DTR | UART_MCR_RTS);
754         } else {
755                 serial_outp(up, UART_MCR,
756                             UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
757         }
758         serial_outp(up, UART_IER, 0x0f);        /* enable all intrs */
759         (void)serial_inp(up, UART_LSR);
760         (void)serial_inp(up, UART_RX);
761         (void)serial_inp(up, UART_IIR);
762         (void)serial_inp(up, UART_MSR);
763         serial_outp(up, UART_TX, 0xFF);
764         udelay (20);
765         irq = probe_irq_off(irqs);
766
767         serial_outp(up, UART_MCR, save_mcr);
768         serial_outp(up, UART_IER, save_ier);
769
770         if (up->port.flags & UPF_FOURPORT)
771                 outb_p(save_ICP, ICP);
772
773         up->port.irq = (irq > 0) ? irq : 0;
774 }
775
776 static void serial8250_stop_tx(struct uart_port *port, unsigned int tty_stop)
777 {
778         struct uart_8250_port *up = (struct uart_8250_port *)port;
779
780         if (up->ier & UART_IER_THRI) {
781                 up->ier &= ~UART_IER_THRI;
782                 serial_out(up, UART_IER, up->ier);
783         }
784         if (up->port.type == PORT_16C950 && tty_stop) {
785                 up->acr |= UART_ACR_TXDIS;
786                 serial_icr_write(up, UART_ACR, up->acr);
787         }
788 }
789
790 static void serial8250_start_tx(struct uart_port *port, unsigned int tty_start)
791 {
792         struct uart_8250_port *up = (struct uart_8250_port *)port;
793
794         if (!(up->ier & UART_IER_THRI)) {
795                 up->ier |= UART_IER_THRI;
796                 serial_out(up, UART_IER, up->ier);
797         }
798         /*
799          * We only do this from uart_start
800          */
801         if (tty_start && up->port.type == PORT_16C950) {
802                 up->acr &= ~UART_ACR_TXDIS;
803                 serial_icr_write(up, UART_ACR, up->acr);
804         }
805 }
806
807 static void serial8250_stop_rx(struct uart_port *port)
808 {
809         struct uart_8250_port *up = (struct uart_8250_port *)port;
810
811         up->ier &= ~UART_IER_RLSI;
812         up->port.read_status_mask &= ~UART_LSR_DR;
813         serial_out(up, UART_IER, up->ier);
814 }
815
816 static void serial8250_enable_ms(struct uart_port *port)
817 {
818         struct uart_8250_port *up = (struct uart_8250_port *)port;
819
820         up->ier |= UART_IER_MSI;
821         serial_out(up, UART_IER, up->ier);
822 }
823
824 static _INLINE_ void
825 receive_chars(struct uart_8250_port *up, int *status, struct pt_regs *regs)
826 {
827         struct tty_struct *tty = up->port.info->tty;
828         unsigned char ch;
829         int max_count = 256;
830
831         do {
832                 if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
833                         tty->flip.work.func((void *)tty);
834                         if (tty->flip.count >= TTY_FLIPBUF_SIZE)
835                                 return; // if TTY_DONT_FLIP is set
836                 }
837                 ch = serial_inp(up, UART_RX);
838                 *tty->flip.char_buf_ptr = ch;
839                 *tty->flip.flag_buf_ptr = TTY_NORMAL;
840                 up->port.icount.rx++;
841
842                 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
843                                        UART_LSR_FE | UART_LSR_OE))) {
844                         /*
845                          * For statistics only
846                          */
847                         if (*status & UART_LSR_BI) {
848                                 *status &= ~(UART_LSR_FE | UART_LSR_PE);
849                                 up->port.icount.brk++;
850                                 /*
851                                  * We do the SysRQ and SAK checking
852                                  * here because otherwise the break
853                                  * may get masked by ignore_status_mask
854                                  * or read_status_mask.
855                                  */
856                                 if (uart_handle_break(&up->port))
857                                         goto ignore_char;
858                         } else if (*status & UART_LSR_PE)
859                                 up->port.icount.parity++;
860                         else if (*status & UART_LSR_FE)
861                                 up->port.icount.frame++;
862                         if (*status & UART_LSR_OE)
863                                 up->port.icount.overrun++;
864
865                         /*
866                          * Mask off conditions which should be ingored.
867                          */
868                         *status &= up->port.read_status_mask;
869
870 #ifdef CONFIG_SERIAL_8250_CONSOLE
871                         if (up->port.line == up->port.cons->index) {
872                                 /* Recover the break flag from console xmit */
873                                 *status |= up->lsr_break_flag;
874                                 up->lsr_break_flag = 0;
875                         }
876 #endif
877                         if (*status & UART_LSR_BI) {
878                                 DEBUG_INTR("handling break....");
879                                 *tty->flip.flag_buf_ptr = TTY_BREAK;
880                         } else if (*status & UART_LSR_PE)
881                                 *tty->flip.flag_buf_ptr = TTY_PARITY;
882                         else if (*status & UART_LSR_FE)
883                                 *tty->flip.flag_buf_ptr = TTY_FRAME;
884                 }
885                 if (uart_handle_sysrq_char(&up->port, ch, regs))
886                         goto ignore_char;
887                 if ((*status & up->port.ignore_status_mask) == 0) {
888                         tty->flip.flag_buf_ptr++;
889                         tty->flip.char_buf_ptr++;
890                         tty->flip.count++;
891                 }
892                 if ((*status & UART_LSR_OE) &&
893                     tty->flip.count < TTY_FLIPBUF_SIZE) {
894                         /*
895                          * Overrun is special, since it's reported
896                          * immediately, and doesn't affect the current
897                          * character.
898                          */
899                         *tty->flip.flag_buf_ptr = TTY_OVERRUN;
900                         tty->flip.flag_buf_ptr++;
901                         tty->flip.char_buf_ptr++;
902                         tty->flip.count++;
903                 }
904         ignore_char:
905                 *status = serial_inp(up, UART_LSR);
906         } while ((*status & UART_LSR_DR) && (max_count-- > 0));
907         tty_flip_buffer_push(tty);
908 }
909
910 static _INLINE_ void transmit_chars(struct uart_8250_port *up)
911 {
912         struct circ_buf *xmit = &up->port.info->xmit;
913         int count;
914
915         if (up->port.x_char) {
916                 serial_outp(up, UART_TX, up->port.x_char);
917                 up->port.icount.tx++;
918                 up->port.x_char = 0;
919                 return;
920         }
921         if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
922                 serial8250_stop_tx(&up->port, 0);
923                 return;
924         }
925
926         count = up->port.fifosize;
927         do {
928                 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
929                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
930                 up->port.icount.tx++;
931                 if (uart_circ_empty(xmit))
932                         break;
933         } while (--count > 0);
934
935         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
936                 uart_write_wakeup(&up->port);
937
938         DEBUG_INTR("THRE...");
939
940         if (uart_circ_empty(xmit))
941                 serial8250_stop_tx(&up->port, 0);
942 }
943
944 static _INLINE_ void check_modem_status(struct uart_8250_port *up)
945 {
946         int status;
947
948         status = serial_in(up, UART_MSR);
949
950         if ((status & UART_MSR_ANY_DELTA) == 0)
951                 return;
952
953         if (status & UART_MSR_TERI)
954                 up->port.icount.rng++;
955         if (status & UART_MSR_DDSR)
956                 up->port.icount.dsr++;
957         if (status & UART_MSR_DDCD)
958                 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
959         if (status & UART_MSR_DCTS)
960                 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
961
962         wake_up_interruptible(&up->port.info->delta_msr_wait);
963 }
964
965 /*
966  * This handles the interrupt from one port.
967  */
968 static inline void
969 serial8250_handle_port(struct uart_8250_port *up, struct pt_regs *regs)
970 {
971         unsigned int status = serial_inp(up, UART_LSR);
972
973         DEBUG_INTR("status = %x...", status);
974
975         if (status & UART_LSR_DR)
976                 receive_chars(up, &status, regs);
977         check_modem_status(up);
978         if (status & UART_LSR_THRE)
979                 transmit_chars(up);
980 }
981
982 /*
983  * This is the serial driver's interrupt routine.
984  *
985  * Arjan thinks the old way was overly complex, so it got simplified.
986  * Alan disagrees, saying that need the complexity to handle the weird
987  * nature of ISA shared interrupts.  (This is a special exception.)
988  *
989  * In order to handle ISA shared interrupts properly, we need to check
990  * that all ports have been serviced, and therefore the ISA interrupt
991  * line has been de-asserted.
992  *
993  * This means we need to loop through all ports. checking that they
994  * don't have an interrupt pending.
995  */
996 static irqreturn_t serial8250_interrupt(int irq, void *dev_id, struct pt_regs *regs)
997 {
998         struct irq_info *i = dev_id;
999         struct list_head *l, *end = NULL;
1000         int pass_counter = 0;
1001
1002         DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1003
1004         spin_lock(&i->lock);
1005
1006         l = i->head;
1007         do {
1008                 struct uart_8250_port *up;
1009                 unsigned int iir;
1010
1011                 up = list_entry(l, struct uart_8250_port, list);
1012
1013                 iir = serial_in(up, UART_IIR);
1014                 if (!(iir & UART_IIR_NO_INT)) {
1015                         spin_lock(&up->port.lock);
1016                         serial8250_handle_port(up, regs);
1017                         spin_unlock(&up->port.lock);
1018
1019                         end = NULL;
1020                 } else if (end == NULL)
1021                         end = l;
1022
1023                 l = l->next;
1024
1025                 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1026                         /* If we hit this, we're dead. */
1027                         printk(KERN_ERR "serial8250: too much work for "
1028                                 "irq%d\n", irq);
1029                         break;
1030                 }
1031         } while (l != end);
1032
1033         spin_unlock(&i->lock);
1034
1035         DEBUG_INTR("end.\n");
1036         /* FIXME! Was it really ours? */
1037         return IRQ_HANDLED;
1038 }
1039
1040 /*
1041  * To support ISA shared interrupts, we need to have one interrupt
1042  * handler that ensures that the IRQ line has been deasserted
1043  * before returning.  Failing to do this will result in the IRQ
1044  * line being stuck active, and, since ISA irqs are edge triggered,
1045  * no more IRQs will be seen.
1046  */
1047 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1048 {
1049         spin_lock_irq(&i->lock);
1050
1051         if (!list_empty(i->head)) {
1052                 if (i->head == &up->list)
1053                         i->head = i->head->next;
1054                 list_del(&up->list);
1055         } else {
1056                 BUG_ON(i->head != &up->list);
1057                 i->head = NULL;
1058         }
1059
1060         spin_unlock_irq(&i->lock);
1061 }
1062
1063 static int serial_link_irq_chain(struct uart_8250_port *up)
1064 {
1065         struct irq_info *i = irq_lists + up->port.irq;
1066         int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? SA_SHIRQ : 0;
1067
1068         spin_lock_irq(&i->lock);
1069
1070         if (i->head) {
1071                 list_add(&up->list, i->head);
1072                 spin_unlock_irq(&i->lock);
1073
1074                 ret = 0;
1075         } else {
1076                 INIT_LIST_HEAD(&up->list);
1077                 i->head = &up->list;
1078                 spin_unlock_irq(&i->lock);
1079
1080                 ret = request_irq(up->port.irq, serial8250_interrupt,
1081                                   irq_flags, "serial", i);
1082                 if (ret < 0)
1083                         serial_do_unlink(i, up);
1084         }
1085
1086         return ret;
1087 }
1088
1089 static void serial_unlink_irq_chain(struct uart_8250_port *up)
1090 {
1091         struct irq_info *i = irq_lists + up->port.irq;
1092
1093         BUG_ON(i->head == NULL);
1094
1095         if (list_empty(i->head))
1096                 free_irq(up->port.irq, i);
1097
1098         serial_do_unlink(i, up);
1099 }
1100
1101 /*
1102  * This function is used to handle ports that do not have an
1103  * interrupt.  This doesn't work very well for 16450's, but gives
1104  * barely passable results for a 16550A.  (Although at the expense
1105  * of much CPU overhead).
1106  */
1107 static void serial8250_timeout(unsigned long data)
1108 {
1109         struct uart_8250_port *up = (struct uart_8250_port *)data;
1110         unsigned int timeout;
1111         unsigned int iir;
1112
1113         iir = serial_in(up, UART_IIR);
1114         if (!(iir & UART_IIR_NO_INT)) {
1115                 spin_lock(&up->port.lock);
1116                 serial8250_handle_port(up, NULL);
1117                 spin_unlock(&up->port.lock);
1118         }
1119
1120         timeout = up->port.timeout;
1121         timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
1122         mod_timer(&up->timer, jiffies + timeout);
1123 }
1124
1125 static unsigned int serial8250_tx_empty(struct uart_port *port)
1126 {
1127         struct uart_8250_port *up = (struct uart_8250_port *)port;
1128         unsigned long flags;
1129         unsigned int ret;
1130
1131         spin_lock_irqsave(&up->port.lock, flags);
1132         ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
1133         spin_unlock_irqrestore(&up->port.lock, flags);
1134
1135         return ret;
1136 }
1137
1138 static unsigned int serial8250_get_mctrl(struct uart_port *port)
1139 {
1140         struct uart_8250_port *up = (struct uart_8250_port *)port;
1141         unsigned long flags;
1142         unsigned char status;
1143         unsigned int ret;
1144
1145         spin_lock_irqsave(&up->port.lock, flags);
1146         status = serial_in(up, UART_MSR);
1147         spin_unlock_irqrestore(&up->port.lock, flags);
1148
1149         ret = 0;
1150         if (status & UART_MSR_DCD)
1151                 ret |= TIOCM_CAR;
1152         if (status & UART_MSR_RI)
1153                 ret |= TIOCM_RNG;
1154         if (status & UART_MSR_DSR)
1155                 ret |= TIOCM_DSR;
1156         if (status & UART_MSR_CTS)
1157                 ret |= TIOCM_CTS;
1158         return ret;
1159 }
1160
1161 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1162 {
1163         struct uart_8250_port *up = (struct uart_8250_port *)port;
1164         unsigned char mcr = 0;
1165
1166         if (mctrl & TIOCM_RTS)
1167                 mcr |= UART_MCR_RTS;
1168         if (mctrl & TIOCM_DTR)
1169                 mcr |= UART_MCR_DTR;
1170         if (mctrl & TIOCM_OUT1)
1171                 mcr |= UART_MCR_OUT1;
1172         if (mctrl & TIOCM_OUT2)
1173                 mcr |= UART_MCR_OUT2;
1174         if (mctrl & TIOCM_LOOP)
1175                 mcr |= UART_MCR_LOOP;
1176
1177         mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1178
1179         serial_out(up, UART_MCR, mcr);
1180 }
1181
1182 static void serial8250_break_ctl(struct uart_port *port, int break_state)
1183 {
1184         struct uart_8250_port *up = (struct uart_8250_port *)port;
1185         unsigned long flags;
1186
1187         spin_lock_irqsave(&up->port.lock, flags);
1188         if (break_state == -1)
1189                 up->lcr |= UART_LCR_SBC;
1190         else
1191                 up->lcr &= ~UART_LCR_SBC;
1192         serial_out(up, UART_LCR, up->lcr);
1193         spin_unlock_irqrestore(&up->port.lock, flags);
1194 }
1195
1196 static int serial8250_startup(struct uart_port *port)
1197 {
1198         struct uart_8250_port *up = (struct uart_8250_port *)port;
1199         unsigned long flags;
1200         int retval;
1201
1202         up->capabilities = uart_config[up->port.type].flags;
1203         up->mcr = 0;
1204
1205         if (up->port.type == PORT_16C950) {
1206                 /* Wake up and initialize UART */
1207                 up->acr = 0;
1208                 serial_outp(up, UART_LCR, 0xBF);
1209                 serial_outp(up, UART_EFR, UART_EFR_ECB);
1210                 serial_outp(up, UART_IER, 0);
1211                 serial_outp(up, UART_LCR, 0);
1212                 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
1213                 serial_outp(up, UART_LCR, 0xBF);
1214                 serial_outp(up, UART_EFR, UART_EFR_ECB);
1215                 serial_outp(up, UART_LCR, 0);
1216         }
1217
1218 #ifdef CONFIG_SERIAL_8250_RSA
1219         /*
1220          * If this is an RSA port, see if we can kick it up to the
1221          * higher speed clock.
1222          */
1223         enable_rsa(up);
1224 #endif
1225
1226         /*
1227          * Clear the FIFO buffers and disable them.
1228          * (they will be reeanbled in set_termios())
1229          */
1230         if (up->capabilities & UART_CLEAR_FIFO) {
1231                 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1232                 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
1233                                 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
1234                 serial_outp(up, UART_FCR, 0);
1235         }
1236
1237         /*
1238          * Clear the interrupt registers.
1239          */
1240         (void) serial_inp(up, UART_LSR);
1241         (void) serial_inp(up, UART_RX);
1242         (void) serial_inp(up, UART_IIR);
1243         (void) serial_inp(up, UART_MSR);
1244
1245         /*
1246          * At this point, there's no way the LSR could still be 0xff;
1247          * if it is, then bail out, because there's likely no UART
1248          * here.
1249          */
1250         if (!(up->port.flags & UPF_BUGGY_UART) &&
1251             (serial_inp(up, UART_LSR) == 0xff)) {
1252                 printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
1253                 return -ENODEV;
1254         }
1255
1256         /*
1257          * If the "interrupt" for this port doesn't correspond with any
1258          * hardware interrupt, we use a timer-based system.  The original
1259          * driver used to do this with IRQ0.
1260          */
1261         if (!is_real_interrupt(up->port.irq)) {
1262                 unsigned int timeout = up->port.timeout;
1263
1264                 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
1265
1266                 up->timer.data = (unsigned long)up;
1267                 mod_timer(&up->timer, jiffies + timeout);
1268         } else {
1269                 retval = serial_link_irq_chain(up);
1270                 if (retval)
1271                         return retval;
1272         }
1273
1274         /*
1275          * Now, initialize the UART
1276          */
1277         serial_outp(up, UART_LCR, UART_LCR_WLEN8);
1278
1279         spin_lock_irqsave(&up->port.lock, flags);
1280         if (up->port.flags & UPF_FOURPORT) {
1281                 if (!is_real_interrupt(up->port.irq))
1282                         up->port.mctrl |= TIOCM_OUT1;
1283         } else
1284                 /*
1285                  * Most PC uarts need OUT2 raised to enable interrupts.
1286                  */
1287                 if (is_real_interrupt(up->port.irq))
1288                         up->port.mctrl |= TIOCM_OUT2;
1289
1290         serial8250_set_mctrl(&up->port, up->port.mctrl);
1291         spin_unlock_irqrestore(&up->port.lock, flags);
1292
1293         /*
1294          * Finally, enable interrupts.  Note: Modem status interrupts
1295          * are set via set_termios(), which will be occurring imminently
1296          * anyway, so we don't enable them here.
1297          */
1298         up->ier = UART_IER_RLSI | UART_IER_RDI;
1299         serial_outp(up, UART_IER, up->ier);
1300
1301         if (up->port.flags & UPF_FOURPORT) {
1302                 unsigned int icp;
1303                 /*
1304                  * Enable interrupts on the AST Fourport board
1305                  */
1306                 icp = (up->port.iobase & 0xfe0) | 0x01f;
1307                 outb_p(0x80, icp);
1308                 (void) inb_p(icp);
1309         }
1310
1311         /*
1312          * And clear the interrupt registers again for luck.
1313          */
1314         (void) serial_inp(up, UART_LSR);
1315         (void) serial_inp(up, UART_RX);
1316         (void) serial_inp(up, UART_IIR);
1317         (void) serial_inp(up, UART_MSR);
1318
1319         return 0;
1320 }
1321
1322 static void serial8250_shutdown(struct uart_port *port)
1323 {
1324         struct uart_8250_port *up = (struct uart_8250_port *)port;
1325         unsigned long flags;
1326
1327         /*
1328          * Disable interrupts from this port
1329          */
1330         up->ier = 0;
1331         serial_outp(up, UART_IER, 0);
1332
1333         spin_lock_irqsave(&up->port.lock, flags);
1334         if (up->port.flags & UPF_FOURPORT) {
1335                 /* reset interrupts on the AST Fourport board */
1336                 inb((up->port.iobase & 0xfe0) | 0x1f);
1337                 up->port.mctrl |= TIOCM_OUT1;
1338         } else
1339                 up->port.mctrl &= ~TIOCM_OUT2;
1340
1341         serial8250_set_mctrl(&up->port, up->port.mctrl);
1342         spin_unlock_irqrestore(&up->port.lock, flags);
1343
1344         /*
1345          * Disable break condition and FIFOs
1346          */
1347         serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
1348         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
1349                                   UART_FCR_CLEAR_RCVR |
1350                                   UART_FCR_CLEAR_XMIT);
1351         serial_outp(up, UART_FCR, 0);
1352
1353 #ifdef CONFIG_SERIAL_8250_RSA
1354         /*
1355          * Reset the RSA board back to 115kbps compat mode.
1356          */
1357         disable_rsa(up);
1358 #endif
1359
1360         /*
1361          * Read data port to reset things, and then unlink from
1362          * the IRQ chain.
1363          */
1364         (void) serial_in(up, UART_RX);
1365
1366         if (!is_real_interrupt(up->port.irq))
1367                 del_timer_sync(&up->timer);
1368         else
1369                 serial_unlink_irq_chain(up);
1370 }
1371
1372 static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
1373 {
1374         unsigned int quot;
1375
1376         /*
1377          * Handle magic divisors for baud rates above baud_base on
1378          * SMSC SuperIO chips.
1379          */
1380         if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
1381             baud == (port->uartclk/4))
1382                 quot = 0x8001;
1383         else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
1384                  baud == (port->uartclk/8))
1385                 quot = 0x8002;
1386         else
1387                 quot = uart_get_divisor(port, baud);
1388
1389         return quot;
1390 }
1391
1392 static void
1393 serial8250_set_termios(struct uart_port *port, struct termios *termios,
1394                        struct termios *old)
1395 {
1396         struct uart_8250_port *up = (struct uart_8250_port *)port;
1397         unsigned char cval, fcr = 0;
1398         unsigned long flags;
1399         unsigned int baud, quot;
1400
1401         switch (termios->c_cflag & CSIZE) {
1402         case CS5:
1403                 cval = 0x00;
1404                 break;
1405         case CS6:
1406                 cval = 0x01;
1407                 break;
1408         case CS7:
1409                 cval = 0x02;
1410                 break;
1411         default:
1412         case CS8:
1413                 cval = 0x03;
1414                 break;
1415         }
1416
1417         if (termios->c_cflag & CSTOPB)
1418                 cval |= 0x04;
1419         if (termios->c_cflag & PARENB)
1420                 cval |= UART_LCR_PARITY;
1421         if (!(termios->c_cflag & PARODD))
1422                 cval |= UART_LCR_EPAR;
1423 #ifdef CMSPAR
1424         if (termios->c_cflag & CMSPAR)
1425                 cval |= UART_LCR_SPAR;
1426 #endif
1427
1428         /*
1429          * Ask the core to calculate the divisor for us.
1430          */
1431         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16); 
1432         quot = serial8250_get_divisor(port, baud);
1433
1434         /*
1435          * Work around a bug in the Oxford Semiconductor 952 rev B
1436          * chip which causes it to seriously miscalculate baud rates
1437          * when DLL is 0.
1438          */
1439         if ((quot & 0xff) == 0 && up->port.type == PORT_16C950 &&
1440             up->rev == 0x5201)
1441                 quot ++;
1442
1443         if (up->capabilities & UART_USE_FIFO) {
1444                 if (baud < 2400)
1445                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1446 #ifdef CONFIG_SERIAL_8250_RSA
1447                 else if (up->port.type == PORT_RSA)
1448                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_14;
1449 #endif
1450                 else
1451                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
1452         }
1453
1454         /*
1455          * TI16C750: hardware flow control and 64 byte FIFOs. When AFE is
1456          * enabled, RTS will be deasserted when the receive FIFO contains
1457          * more characters than the trigger, or the MCR RTS bit is cleared.
1458          */
1459         if (up->port.type == PORT_16750) {
1460                 up->mcr &= ~UART_MCR_AFE;
1461                 if (termios->c_cflag & CRTSCTS)
1462                         up->mcr |= UART_MCR_AFE;
1463
1464                 fcr |= UART_FCR7_64BYTE;
1465         }
1466
1467         /*
1468          * Ok, we're now changing the port state.  Do it with
1469          * interrupts disabled.
1470          */
1471         spin_lock_irqsave(&up->port.lock, flags);
1472
1473         /*
1474          * Update the per-port timeout.
1475          */
1476         uart_update_timeout(port, termios->c_cflag, baud);
1477
1478         up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1479         if (termios->c_iflag & INPCK)
1480                 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1481         if (termios->c_iflag & (BRKINT | PARMRK))
1482                 up->port.read_status_mask |= UART_LSR_BI;
1483
1484         /*
1485          * Characteres to ignore
1486          */
1487         up->port.ignore_status_mask = 0;
1488         if (termios->c_iflag & IGNPAR)
1489                 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1490         if (termios->c_iflag & IGNBRK) {
1491                 up->port.ignore_status_mask |= UART_LSR_BI;
1492                 /*
1493                  * If we're ignoring parity and break indicators,
1494                  * ignore overruns too (for real raw support).
1495                  */
1496                 if (termios->c_iflag & IGNPAR)
1497                         up->port.ignore_status_mask |= UART_LSR_OE;
1498         }
1499
1500         /*
1501          * ignore all characters if CREAD is not set
1502          */
1503         if ((termios->c_cflag & CREAD) == 0)
1504                 up->port.ignore_status_mask |= UART_LSR_DR;
1505
1506         /*
1507          * CTS flow control flag and modem status interrupts
1508          */
1509         up->ier &= ~UART_IER_MSI;
1510         if (UART_ENABLE_MS(&up->port, termios->c_cflag))
1511                 up->ier |= UART_IER_MSI;
1512         if (up->port.type == PORT_XSCALE)
1513                 up->ier |= UART_IER_UUE | UART_IER_RTOIE;
1514
1515         serial_out(up, UART_IER, up->ier);
1516
1517         if (up->capabilities & UART_STARTECH) {
1518                 serial_outp(up, UART_LCR, 0xBF);
1519                 serial_outp(up, UART_EFR,
1520                             termios->c_cflag & CRTSCTS ? UART_EFR_CTS :0);
1521         }
1522
1523         if (up->capabilities & UART_NATSEMI) {
1524                 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
1525                 serial_outp(up, UART_LCR, 0xe0);
1526         } else {
1527                 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
1528         }
1529
1530         serial_outp(up, UART_DLL, quot & 0xff);         /* LS of divisor */
1531         serial_outp(up, UART_DLM, quot >> 8);           /* MS of divisor */
1532
1533         /*
1534          * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
1535          * is written without DLAB set, this mode will be disabled.
1536          */
1537         if (up->port.type == PORT_16750)
1538                 serial_outp(up, UART_FCR, fcr);
1539
1540         serial_outp(up, UART_LCR, cval);                /* reset DLAB */
1541         up->lcr = cval;                                 /* Save LCR */
1542         if (up->port.type != PORT_16750) {
1543                 if (fcr & UART_FCR_ENABLE_FIFO) {
1544                         /* emulated UARTs (Lucent Venus 167x) need two steps */
1545                         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1546                 }
1547                 serial_outp(up, UART_FCR, fcr);         /* set fcr */
1548         }
1549         serial8250_set_mctrl(&up->port, up->port.mctrl);
1550         spin_unlock_irqrestore(&up->port.lock, flags);
1551 }
1552
1553 static void
1554 serial8250_pm(struct uart_port *port, unsigned int state,
1555               unsigned int oldstate)
1556 {
1557         struct uart_8250_port *up = (struct uart_8250_port *)port;
1558         if (state) {
1559                 /* sleep */
1560                 if (up->capabilities & UART_STARTECH) {
1561                         /* Arrange to enter sleep mode */
1562                         serial_outp(up, UART_LCR, 0xBF);
1563                         serial_outp(up, UART_EFR, UART_EFR_ECB);
1564                         serial_outp(up, UART_LCR, 0);
1565                         serial_outp(up, UART_IER, UART_IERX_SLEEP);
1566                         serial_outp(up, UART_LCR, 0xBF);
1567                         serial_outp(up, UART_EFR, 0);
1568                         serial_outp(up, UART_LCR, 0);
1569                 }
1570                 if (up->port.type == PORT_16750) {
1571                         /* Arrange to enter sleep mode */
1572                         serial_outp(up, UART_IER, UART_IERX_SLEEP);
1573                 }
1574
1575                 if (up->pm)
1576                         up->pm(port, state, oldstate);
1577         } else {
1578                 /* wake */
1579                 if (up->capabilities & UART_STARTECH) {
1580                         /* Wake up UART */
1581                         serial_outp(up, UART_LCR, 0xBF);
1582                         serial_outp(up, UART_EFR, UART_EFR_ECB);
1583                         /*
1584                          * Turn off LCR == 0xBF so we actually set the IER
1585                          * register on the XR16C850
1586                          */
1587                         serial_outp(up, UART_LCR, 0);
1588                         serial_outp(up, UART_IER, 0);
1589                         /*
1590                          * Now reset LCR so we can turn off the ECB bit
1591                          */
1592                         serial_outp(up, UART_LCR, 0xBF);
1593                         serial_outp(up, UART_EFR, 0);
1594                         /*
1595                          * For a XR16C850, we need to set the trigger levels
1596                          */
1597                         if (up->port.type == PORT_16850) {
1598                                 unsigned char fctr;
1599
1600                                 fctr = serial_inp(up, UART_FCTR) &
1601                                          ~(UART_FCTR_RX | UART_FCTR_TX);
1602                                 serial_outp(up, UART_FCTR, fctr |
1603                                                 UART_FCTR_TRGD |
1604                                                 UART_FCTR_RX);
1605                                 serial_outp(up, UART_TRG, UART_TRG_96);
1606                                 serial_outp(up, UART_FCTR, fctr |
1607                                                 UART_FCTR_TRGD |
1608                                                 UART_FCTR_TX);
1609                                 serial_outp(up, UART_TRG, UART_TRG_96);
1610                         }
1611                         serial_outp(up, UART_LCR, 0);
1612                 }
1613
1614                 if (up->port.type == PORT_16750) {
1615                         /* Wake up UART */
1616                         serial_outp(up, UART_IER, 0);
1617                 }
1618
1619                 if (up->pm)
1620                         up->pm(port, state, oldstate);
1621         }
1622 }
1623
1624 /*
1625  * Resource handling.  This is complicated by the fact that resources
1626  * depend on the port type.  Maybe we should be claiming the standard
1627  * 8250 ports, and then trying to get other resources as necessary?
1628  */
1629 static int
1630 serial8250_request_std_resource(struct uart_8250_port *up, struct resource **res)
1631 {
1632         unsigned int size = 8 << up->port.regshift;
1633         int ret = 0;
1634
1635         switch (up->port.iotype) {
1636         case UPIO_MEM:
1637                 if (up->port.mapbase) {
1638                         *res = request_mem_region(up->port.mapbase, size, "serial");
1639                         if (!*res)
1640                                 ret = -EBUSY;
1641                 }
1642                 break;
1643
1644         case UPIO_HUB6:
1645         case UPIO_PORT:
1646                 *res = request_region(up->port.iobase, size, "serial");
1647                 if (!*res)
1648                         ret = -EBUSY;
1649                 break;
1650         }
1651         return ret;
1652 }
1653
1654 static int
1655 serial8250_request_rsa_resource(struct uart_8250_port *up, struct resource **res)
1656 {
1657         unsigned int size = 8 << up->port.regshift;
1658         unsigned long start;
1659         int ret = 0;
1660
1661         switch (up->port.iotype) {
1662         case UPIO_MEM:
1663                 if (up->port.mapbase) {
1664                         start = up->port.mapbase;
1665                         start += UART_RSA_BASE << up->port.regshift;
1666                         *res = request_mem_region(start, size, "serial-rsa");
1667                         if (!*res)
1668                                 ret = -EBUSY;
1669                 }
1670                 break;
1671
1672         case UPIO_HUB6:
1673         case UPIO_PORT:
1674                 start = up->port.iobase;
1675                 start += UART_RSA_BASE << up->port.regshift;
1676                 *res = request_region(start, size, "serial-rsa");
1677                 if (!*res)
1678                         ret = -EBUSY;
1679                 break;
1680         }
1681
1682         return ret;
1683 }
1684
1685 static void serial8250_release_port(struct uart_port *port)
1686 {
1687         struct uart_8250_port *up = (struct uart_8250_port *)port;
1688         unsigned long start, offset = 0, size = 0;
1689
1690         if (!(up->port.flags & UPF_RESOURCES))
1691                 return;
1692         if (up->port.type == PORT_RSA) {
1693                 offset = UART_RSA_BASE << up->port.regshift;
1694                 size = 8;
1695         }
1696
1697         size <<= up->port.regshift;
1698
1699         switch (up->port.iotype) {
1700         case UPIO_MEM:
1701                 if (up->port.mapbase) {
1702                         /*
1703                          * Unmap the area.
1704                          */
1705                         iounmap(up->port.membase);
1706                         up->port.membase = NULL;
1707
1708                         start = up->port.mapbase;
1709
1710                         if (size)
1711                                 release_mem_region(start + offset, size);
1712                         release_mem_region(start, 8 << up->port.regshift);
1713                 }
1714                 break;
1715
1716         case UPIO_HUB6:
1717         case UPIO_PORT:
1718                 start = up->port.iobase;
1719
1720                 if (size)
1721                         release_region(start + offset, size);
1722                 release_region(start + offset, 8 << up->port.regshift);
1723                 break;
1724
1725         default:
1726                 break;
1727         }
1728 }
1729
1730 static int serial8250_request_port(struct uart_port *port)
1731 {
1732         struct uart_8250_port *up = (struct uart_8250_port *)port;
1733         struct resource *res = NULL, *res_rsa = NULL;
1734         int ret = 0;
1735
1736         if (up->port.flags & UPF_RESOURCES) {
1737                 if (up->port.type == PORT_RSA) {
1738                         ret = serial8250_request_rsa_resource(up, &res_rsa);
1739                         if (ret < 0)
1740                                 return ret;
1741                 }
1742
1743                 ret = serial8250_request_std_resource(up, &res);
1744         }
1745
1746         /*
1747          * If we have a mapbase, then request that as well.
1748          */
1749         if (ret == 0 && up->port.flags & UPF_IOREMAP) {
1750                 int size = res->end - res->start + 1;
1751
1752                 up->port.membase = ioremap(up->port.mapbase, size);
1753                 if (!up->port.membase)
1754                         ret = -ENOMEM;
1755         }
1756
1757         if (ret < 0) {
1758                 if (res_rsa)
1759                         release_resource(res_rsa);
1760                 if (res)
1761                         release_resource(res);
1762         }
1763         return ret;
1764 }
1765
1766 static void serial8250_config_port(struct uart_port *port, int flags)
1767 {
1768         struct uart_8250_port *up = (struct uart_8250_port *)port;
1769         struct resource *res_std = NULL, *res_rsa = NULL;
1770         int probeflags = PROBE_ANY;
1771         int ret;
1772
1773 #ifdef CONFIG_MCA
1774         /*
1775          * Don't probe for MCA ports on non-MCA machines.
1776          */
1777         if (up->port.flags & UPF_BOOT_ONLYMCA && !MCA_bus)
1778                 return;
1779 #endif
1780
1781         /*
1782          * Find the region that we can probe for.  This in turn
1783          * tells us whether we can probe for the type of port.
1784          */
1785         if (up->port.flags & UPF_RESOURCES) {
1786                 ret = serial8250_request_std_resource(up, &res_std);
1787                 if (ret < 0)
1788                         return;
1789
1790                 ret = serial8250_request_rsa_resource(up, &res_rsa);
1791                 if (ret < 0)
1792                         probeflags &= ~PROBE_RSA;
1793         } else {
1794                 probeflags &= ~PROBE_RSA;
1795         }
1796
1797         if (flags & UART_CONFIG_TYPE)
1798                 autoconfig(up, probeflags);
1799         if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
1800                 autoconfig_irq(up);
1801
1802         /*
1803          * If the port wasn't an RSA port, release the resource.
1804          */
1805         if (up->port.type != PORT_RSA && res_rsa)
1806                 release_resource(res_rsa);
1807
1808         if (up->port.type == PORT_UNKNOWN && res_std)
1809                 release_resource(res_std);
1810 }
1811
1812 static int
1813 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
1814 {
1815         if (ser->irq >= NR_IRQS || ser->irq < 0 ||
1816             ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
1817             ser->type > PORT_MAX_8250 || ser->type == PORT_CIRRUS ||
1818             ser->type == PORT_STARTECH)
1819                 return -EINVAL;
1820         return 0;
1821 }
1822
1823 static const char *
1824 serial8250_type(struct uart_port *port)
1825 {
1826         int type = port->type;
1827
1828         if (type >= ARRAY_SIZE(uart_config))
1829                 type = 0;
1830         return uart_config[type].name;
1831 }
1832
1833 static struct uart_ops serial8250_pops = {
1834         .tx_empty       = serial8250_tx_empty,
1835         .set_mctrl      = serial8250_set_mctrl,
1836         .get_mctrl      = serial8250_get_mctrl,
1837         .stop_tx        = serial8250_stop_tx,
1838         .start_tx       = serial8250_start_tx,
1839         .stop_rx        = serial8250_stop_rx,
1840         .enable_ms      = serial8250_enable_ms,
1841         .break_ctl      = serial8250_break_ctl,
1842         .startup        = serial8250_startup,
1843         .shutdown       = serial8250_shutdown,
1844         .set_termios    = serial8250_set_termios,
1845         .pm             = serial8250_pm,
1846         .type           = serial8250_type,
1847         .release_port   = serial8250_release_port,
1848         .request_port   = serial8250_request_port,
1849         .config_port    = serial8250_config_port,
1850         .verify_port    = serial8250_verify_port,
1851 };
1852
1853 static struct uart_8250_port serial8250_ports[UART_NR];
1854
1855 static void __init serial8250_isa_init_ports(void)
1856 {
1857         struct uart_8250_port *up;
1858         static int first = 1;
1859         int i;
1860
1861         if (!first)
1862                 return;
1863         first = 0;
1864
1865         for (i = 0, up = serial8250_ports; i < ARRAY_SIZE(old_serial_port);
1866              i++, up++) {
1867                 up->port.iobase   = old_serial_port[i].port;
1868                 up->port.irq      = irq_canonicalize(old_serial_port[i].irq);
1869                 up->port.uartclk  = old_serial_port[i].baud_base * 16;
1870                 up->port.flags    = old_serial_port[i].flags |
1871                                     UPF_RESOURCES;
1872                 up->port.hub6     = old_serial_port[i].hub6;
1873                 up->port.membase  = old_serial_port[i].iomem_base;
1874                 up->port.iotype   = old_serial_port[i].io_type;
1875                 up->port.regshift = old_serial_port[i].iomem_reg_shift;
1876                 up->port.ops      = &serial8250_pops;
1877                 if (share_irqs)
1878                         up->port.flags |= UPF_SHARE_IRQ;
1879         }
1880 }
1881
1882 static void __init serial8250_register_ports(struct uart_driver *drv)
1883 {
1884         int i;
1885
1886         serial8250_isa_init_ports();
1887
1888         for (i = 0; i < UART_NR; i++) {
1889                 struct uart_8250_port *up = &serial8250_ports[i];
1890
1891                 up->port.line = i;
1892                 up->port.ops = &serial8250_pops;
1893                 init_timer(&up->timer);
1894                 up->timer.function = serial8250_timeout;
1895
1896                 /*
1897                  * ALPHA_KLUDGE_MCR needs to be killed.
1898                  */
1899                 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
1900                 up->mcr_force = ALPHA_KLUDGE_MCR;
1901
1902                 uart_add_one_port(drv, &up->port);
1903         }
1904 }
1905
1906 #ifdef CONFIG_SERIAL_8250_CONSOLE
1907
1908 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1909
1910 /*
1911  *      Wait for transmitter & holding register to empty
1912  */
1913 static inline void wait_for_xmitr(struct uart_8250_port *up)
1914 {
1915         unsigned int status, tmout = 10000;
1916
1917         /* Wait up to 10ms for the character(s) to be sent. */
1918         do {
1919                 status = serial_in(up, UART_LSR);
1920
1921                 if (status & UART_LSR_BI)
1922                         up->lsr_break_flag = UART_LSR_BI;
1923
1924                 if (--tmout == 0)
1925                         break;
1926                 udelay(1);
1927         } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
1928
1929         /* Wait up to 1s for flow control if necessary */
1930         if (up->port.flags & UPF_CONS_FLOW) {
1931                 tmout = 1000000;
1932                 while (--tmout &&
1933                        ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
1934                         udelay(1);
1935         }
1936 }
1937
1938 /*
1939  *      Print a string to the serial port trying not to disturb
1940  *      any possible real use of the port...
1941  *
1942  *      The console_lock must be held when we get here.
1943  */
1944 static void
1945 serial8250_console_write(struct console *co, const char *s, unsigned int count)
1946 {
1947         struct uart_8250_port *up = &serial8250_ports[co->index];
1948         unsigned int ier;
1949         int i;
1950
1951         /*
1952          *      First save the UER then disable the interrupts
1953          */
1954         ier = serial_in(up, UART_IER);
1955
1956         if (up->port.type == PORT_XSCALE)
1957                 serial_out(up, UART_IER, UART_IER_UUE);
1958         else
1959                 serial_out(up, UART_IER, 0);
1960
1961         /*
1962          *      Now, do each character
1963          */
1964         for (i = 0; i < count; i++, s++) {
1965                 wait_for_xmitr(up);
1966
1967                 /*
1968                  *      Send the character out.
1969                  *      If a LF, also do CR...
1970                  */
1971                 serial_out(up, UART_TX, *s);
1972                 if (*s == 10) {
1973                         wait_for_xmitr(up);
1974                         serial_out(up, UART_TX, 13);
1975                 }
1976         }
1977
1978         /*
1979          *      Finally, wait for transmitter to become empty
1980          *      and restore the IER
1981          */
1982         wait_for_xmitr(up);
1983         serial_out(up, UART_IER, ier);
1984 }
1985
1986 static int __init serial8250_console_setup(struct console *co, char *options)
1987 {
1988         struct uart_port *port;
1989         int baud = 9600;
1990         int bits = 8;
1991         int parity = 'n';
1992         int flow = 'n';
1993
1994         /*
1995          * Check whether an invalid uart number has been specified, and
1996          * if so, search for the first available port that does have
1997          * console support.
1998          */
1999         if (co->index >= UART_NR)
2000                 co->index = 0;
2001         port = &serial8250_ports[co->index].port;
2002         if (!port->ops)
2003                 return -ENODEV;
2004
2005         /*
2006          * Temporary fix.
2007          */
2008         spin_lock_init(&port->lock);
2009
2010         if (options)
2011                 uart_parse_options(options, &baud, &parity, &bits, &flow);
2012
2013         return uart_set_options(port, co, baud, parity, bits, flow);
2014 }
2015
2016 extern struct uart_driver serial8250_reg;
2017 static struct console serial8250_console = {
2018         .name           = "ttyS",
2019         .write          = serial8250_console_write,
2020         .device         = uart_console_device,
2021         .setup          = serial8250_console_setup,
2022         .flags          = CON_PRINTBUFFER,
2023         .index          = -1,
2024         .data           = &serial8250_reg,
2025 };
2026
2027 static int __init serial8250_console_init(void)
2028 {
2029         serial8250_isa_init_ports();
2030         register_console(&serial8250_console);
2031         return 0;
2032 }
2033 console_initcall(serial8250_console_init);
2034
2035 static int __init serial8250_late_console_init(void)
2036 {
2037         if (!(serial8250_console.flags & CON_ENABLED))
2038                 register_console(&serial8250_console);
2039         return 0;
2040 }
2041 late_initcall(serial8250_late_console_init);
2042
2043 #define SERIAL8250_CONSOLE      &serial8250_console
2044 #else
2045 #define SERIAL8250_CONSOLE      NULL
2046 #endif
2047
2048 static struct uart_driver serial8250_reg = {
2049         .owner                  = THIS_MODULE,
2050         .driver_name            = "serial",
2051         .devfs_name             = "tts/",
2052         .dev_name               = "ttyS",
2053         .major                  = TTY_MAJOR,
2054         .minor                  = 64,
2055         .nr                     = UART_NR,
2056         .cons                   = SERIAL8250_CONSOLE,
2057 };
2058
2059 /*
2060  * register_serial and unregister_serial allows for 16x50 serial ports to be
2061  * configured at run-time, to support PCMCIA modems.
2062  */
2063
2064 static int __register_serial(struct serial_struct *req, int line)
2065 {
2066         struct uart_port port;
2067
2068         port.iobase   = req->port;
2069         port.membase  = req->iomem_base;
2070         port.irq      = req->irq;
2071         port.uartclk  = req->baud_base * 16;
2072         port.fifosize = req->xmit_fifo_size;
2073         port.regshift = req->iomem_reg_shift;
2074         port.iotype   = req->io_type;
2075         port.flags    = req->flags | UPF_BOOT_AUTOCONF;
2076         port.mapbase  = req->iomap_base;
2077         port.line     = line;
2078
2079         if (share_irqs)
2080                 port.flags |= UPF_SHARE_IRQ;
2081
2082         if (HIGH_BITS_OFFSET)
2083                 port.iobase |= (long) req->port_high << HIGH_BITS_OFFSET;
2084
2085         /*
2086          * If a clock rate wasn't specified by the low level
2087          * driver, then default to the standard clock rate.
2088          */
2089         if (port.uartclk == 0)
2090                 port.uartclk = BASE_BAUD * 16;
2091
2092         return uart_register_port(&serial8250_reg, &port);
2093 }
2094
2095 /**
2096  *      register_serial - configure a 16x50 serial port at runtime
2097  *      @req: request structure
2098  *
2099  *      Configure the serial port specified by the request. If the
2100  *      port exists and is in use an error is returned. If the port
2101  *      is not currently in the table it is added.
2102  *
2103  *      The port is then probed and if necessary the IRQ is autodetected
2104  *      If this fails an error is returned.
2105  *
2106  *      On success the port is ready to use and the line number is returned.
2107  */
2108 int register_serial(struct serial_struct *req)
2109 {
2110         return __register_serial(req, -1);
2111 }
2112
2113 int __init early_serial_setup(struct uart_port *port)
2114 {
2115         if (port->line >= ARRAY_SIZE(serial8250_ports))
2116                 return -ENODEV;
2117
2118         serial8250_isa_init_ports();
2119         serial8250_ports[port->line].port       = *port;
2120         serial8250_ports[port->line].port.ops   = &serial8250_pops;
2121         return 0;
2122 }
2123
2124 /**
2125  *      unregister_serial - remove a 16x50 serial port at runtime
2126  *      @line: serial line number
2127  *
2128  *      Remove one serial port.  This may be called from interrupt
2129  *      context.
2130  */
2131 void unregister_serial(int line)
2132 {
2133         uart_unregister_port(&serial8250_reg, line);
2134 }
2135
2136 /*
2137  * This is for ISAPNP only.
2138  */
2139 void serial8250_get_irq_map(unsigned int *map)
2140 {
2141         int i;
2142
2143         for (i = 0; i < UART_NR; i++) {
2144                 if (serial8250_ports[i].port.type != PORT_UNKNOWN &&
2145                     serial8250_ports[i].port.irq < 16)
2146                         *map |= 1 << serial8250_ports[i].port.irq;
2147         }
2148 }
2149
2150 /**
2151  *      serial8250_suspend_port - suspend one serial port
2152  *      @line:  serial line number
2153  *      @level: the level of port suspension, as per uart_suspend_port
2154  *
2155  *      Suspend one serial port.
2156  */
2157 void serial8250_suspend_port(int line)
2158 {
2159         uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2160 }
2161
2162 /**
2163  *      serial8250_resume_port - resume one serial port
2164  *      @line:  serial line number
2165  *      @level: the level of port resumption, as per uart_resume_port
2166  *
2167  *      Resume one serial port.
2168  */
2169 void serial8250_resume_port(int line)
2170 {
2171         uart_resume_port(&serial8250_reg, &serial8250_ports[line].port);
2172 }
2173
2174 static int __init serial8250_init(void)
2175 {
2176         int ret, i;
2177
2178         printk(KERN_INFO "Serial: 8250/16550 driver $Revision: 1.90 $ "
2179                 "%d ports, IRQ sharing %sabled\n", (int) UART_NR,
2180                 share_irqs ? "en" : "dis");
2181
2182         for (i = 0; i < NR_IRQS; i++)
2183                 spin_lock_init(&irq_lists[i].lock);
2184
2185         ret = uart_register_driver(&serial8250_reg);
2186         if (ret >= 0)
2187                 serial8250_register_ports(&serial8250_reg);
2188
2189         return ret;
2190 }
2191
2192 static void __exit serial8250_exit(void)
2193 {
2194         int i;
2195
2196         for (i = 0; i < UART_NR; i++)
2197                 uart_remove_one_port(&serial8250_reg, &serial8250_ports[i].port);
2198
2199         uart_unregister_driver(&serial8250_reg);
2200 }
2201
2202 module_init(serial8250_init);
2203 module_exit(serial8250_exit);
2204
2205 EXPORT_SYMBOL(register_serial);
2206 EXPORT_SYMBOL(unregister_serial);
2207 EXPORT_SYMBOL(serial8250_get_irq_map);
2208 EXPORT_SYMBOL(serial8250_suspend_port);
2209 EXPORT_SYMBOL(serial8250_resume_port);
2210
2211 MODULE_LICENSE("GPL");
2212 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver $Revision: 1.90 $");
2213
2214 module_param(share_irqs, uint, 0644);
2215 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
2216         " (unsafe)");
2217
2218 #ifdef CONFIG_SERIAL_8250_RSA
2219 module_param_array(probe_rsa, ulong, probe_rsa_count, 0444);
2220 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
2221 #endif
2222 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);