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