fedora core 6 1.2949 + vserver 2.2.0
[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  *  2005/09/16: Enabled higher baud rates for 16C95x.
11  *              (Mathias Adam <a2@adamis.de>)
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  *  $Id: 8250.c,v 1.90 2002/07/28 10:03:27 rmk Exp $
19  *
20  * A note about mapbase / membase
21  *
22  *  mapbase is the physical address of the IO port.
23  *  membase is an 'ioremapped' cookie.
24  */
25
26 #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
27 #define SUPPORT_SYSRQ
28 #endif
29
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/ioport.h>
33 #include <linux/init.h>
34 #include <linux/console.h>
35 #include <linux/sysrq.h>
36 #include <linux/delay.h>
37 #include <linux/platform_device.h>
38 #include <linux/tty.h>
39 #include <linux/tty_flip.h>
40 #include <linux/serial_reg.h>
41 #include <linux/serial_core.h>
42 #include <linux/serial.h>
43 #include <linux/serial_8250.h>
44 #include <linux/nmi.h>
45 #include <linux/mutex.h>
46
47 #include <asm/io.h>
48 #include <asm/irq.h>
49
50 #include "8250.h"
51
52 /*
53  * Configuration:
54  *   share_irqs - whether we pass IRQF_SHARED to request_irq().  This option
55  *                is unsafe when used on edge-triggered interrupts.
56  */
57 static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
58
59 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
60
61 /*
62  * Debugging.
63  */
64 #if 0
65 #define DEBUG_AUTOCONF(fmt...)  printk(fmt)
66 #else
67 #define DEBUG_AUTOCONF(fmt...)  do { } while (0)
68 #endif
69
70 #if 0
71 #define DEBUG_INTR(fmt...)      printk(fmt)
72 #else
73 #define DEBUG_INTR(fmt...)      do { } while (0)
74 #endif
75
76 #define PASS_LIMIT      256
77
78 /*
79  * We default to IRQ0 for the "no irq" hack.   Some
80  * machine types want others as well - they're free
81  * to redefine this in their header file.
82  */
83 #define is_real_interrupt(irq)  ((irq) != 0)
84
85 #ifdef CONFIG_SERIAL_8250_DETECT_IRQ
86 #define CONFIG_SERIAL_DETECT_IRQ 1
87 #endif
88 #ifdef CONFIG_SERIAL_8250_MANY_PORTS
89 #define CONFIG_SERIAL_MANY_PORTS 1
90 #endif
91
92 /*
93  * HUB6 is always on.  This will be removed once the header
94  * files have been cleaned.
95  */
96 #define CONFIG_HUB6 1
97
98 #include <asm/serial.h>
99
100 /*
101  * SERIAL_PORT_DFNS tells us about built-in ports that have no
102  * standard enumeration mechanism.   Platforms that can find all
103  * serial ports via mechanisms like ACPI or PCI need not supply it.
104  */
105 #ifndef SERIAL_PORT_DFNS
106 #define SERIAL_PORT_DFNS
107 #endif
108
109 static const struct old_serial_port old_serial_port[] = {
110         SERIAL_PORT_DFNS /* defined in asm/serial.h */
111 };
112
113 #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
114
115 #ifdef CONFIG_SERIAL_8250_RSA
116
117 #define PORT_RSA_MAX 4
118 static unsigned long probe_rsa[PORT_RSA_MAX];
119 static unsigned int probe_rsa_count;
120 #endif /* CONFIG_SERIAL_8250_RSA  */
121
122 struct uart_8250_port {
123         struct uart_port        port;
124         struct timer_list       timer;          /* "no irq" timer */
125         struct list_head        list;           /* ports on this IRQ */
126         unsigned short          capabilities;   /* port capabilities */
127         unsigned short          bugs;           /* port bugs */
128         unsigned int            tx_loadsz;      /* transmit fifo load size */
129         unsigned char           acr;
130         unsigned char           ier;
131         unsigned char           lcr;
132         unsigned char           mcr;
133         unsigned char           mcr_mask;       /* mask of user bits */
134         unsigned char           mcr_force;      /* mask of forced bits */
135         unsigned char           lsr_break_flag;
136
137         /*
138          * We provide a per-port pm hook.
139          */
140         void                    (*pm)(struct uart_port *port,
141                                       unsigned int state, unsigned int old);
142 };
143
144 struct irq_info {
145         spinlock_t              lock;
146         struct list_head        *head;
147 };
148
149 static struct irq_info irq_lists[NR_IRQS];
150
151 /*
152  * Here we define the default xmit fifo size used for each type of UART.
153  */
154 static const struct serial8250_config uart_config[] = {
155         [PORT_UNKNOWN] = {
156                 .name           = "unknown",
157                 .fifo_size      = 1,
158                 .tx_loadsz      = 1,
159         },
160         [PORT_8250] = {
161                 .name           = "8250",
162                 .fifo_size      = 1,
163                 .tx_loadsz      = 1,
164         },
165         [PORT_16450] = {
166                 .name           = "16450",
167                 .fifo_size      = 1,
168                 .tx_loadsz      = 1,
169         },
170         [PORT_16550] = {
171                 .name           = "16550",
172                 .fifo_size      = 1,
173                 .tx_loadsz      = 1,
174         },
175         [PORT_16550A] = {
176                 .name           = "16550A",
177                 .fifo_size      = 16,
178                 .tx_loadsz      = 16,
179                 .fcr            = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
180                 .flags          = UART_CAP_FIFO,
181         },
182         [PORT_CIRRUS] = {
183                 .name           = "Cirrus",
184                 .fifo_size      = 1,
185                 .tx_loadsz      = 1,
186         },
187         [PORT_16650] = {
188                 .name           = "ST16650",
189                 .fifo_size      = 1,
190                 .tx_loadsz      = 1,
191                 .flags          = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
192         },
193         [PORT_16650V2] = {
194                 .name           = "ST16650V2",
195                 .fifo_size      = 32,
196                 .tx_loadsz      = 16,
197                 .fcr            = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
198                                   UART_FCR_T_TRIG_00,
199                 .flags          = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
200         },
201         [PORT_16750] = {
202                 .name           = "TI16750",
203                 .fifo_size      = 64,
204                 .tx_loadsz      = 64,
205                 .fcr            = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
206                                   UART_FCR7_64BYTE,
207                 .flags          = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
208         },
209         [PORT_STARTECH] = {
210                 .name           = "Startech",
211                 .fifo_size      = 1,
212                 .tx_loadsz      = 1,
213         },
214         [PORT_16C950] = {
215                 .name           = "16C950/954",
216                 .fifo_size      = 128,
217                 .tx_loadsz      = 128,
218                 .fcr            = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
219                 .flags          = UART_CAP_FIFO,
220         },
221         [PORT_16654] = {
222                 .name           = "ST16654",
223                 .fifo_size      = 64,
224                 .tx_loadsz      = 32,
225                 .fcr            = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
226                                   UART_FCR_T_TRIG_10,
227                 .flags          = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
228         },
229         [PORT_16850] = {
230                 .name           = "XR16850",
231                 .fifo_size      = 128,
232                 .tx_loadsz      = 128,
233                 .fcr            = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
234                 .flags          = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
235         },
236         [PORT_RSA] = {
237                 .name           = "RSA",
238                 .fifo_size      = 2048,
239                 .tx_loadsz      = 2048,
240                 .fcr            = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
241                 .flags          = UART_CAP_FIFO,
242         },
243         [PORT_NS16550A] = {
244                 .name           = "NS16550A",
245                 .fifo_size      = 16,
246                 .tx_loadsz      = 16,
247                 .fcr            = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
248                 .flags          = UART_CAP_FIFO | UART_NATSEMI,
249         },
250         [PORT_XSCALE] = {
251                 .name           = "XScale",
252                 .fifo_size      = 32,
253                 .tx_loadsz      = 32,
254                 .fcr            = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
255                 .flags          = UART_CAP_FIFO | UART_CAP_UUE,
256         },
257 };
258
259 #ifdef CONFIG_SERIAL_8250_AU1X00
260
261 /* Au1x00 UART hardware has a weird register layout */
262 static const u8 au_io_in_map[] = {
263         [UART_RX]  = 0,
264         [UART_IER] = 2,
265         [UART_IIR] = 3,
266         [UART_LCR] = 5,
267         [UART_MCR] = 6,
268         [UART_LSR] = 7,
269         [UART_MSR] = 8,
270 };
271
272 static const u8 au_io_out_map[] = {
273         [UART_TX]  = 1,
274         [UART_IER] = 2,
275         [UART_FCR] = 4,
276         [UART_LCR] = 5,
277         [UART_MCR] = 6,
278 };
279
280 /* sane hardware needs no mapping */
281 static inline int map_8250_in_reg(struct uart_8250_port *up, int offset)
282 {
283         if (up->port.iotype != UPIO_AU)
284                 return offset;
285         return au_io_in_map[offset];
286 }
287
288 static inline int map_8250_out_reg(struct uart_8250_port *up, int offset)
289 {
290         if (up->port.iotype != UPIO_AU)
291                 return offset;
292         return au_io_out_map[offset];
293 }
294
295 #else
296
297 /* sane hardware needs no mapping */
298 #define map_8250_in_reg(up, offset) (offset)
299 #define map_8250_out_reg(up, offset) (offset)
300
301 #endif
302
303 static unsigned int serial_in(struct uart_8250_port *up, int offset)
304 {
305         unsigned int tmp;
306         offset = map_8250_in_reg(up, offset) << up->port.regshift;
307
308         switch (up->port.iotype) {
309         case UPIO_HUB6:
310                 outb(up->port.hub6 - 1 + offset, up->port.iobase);
311                 return inb(up->port.iobase + 1);
312
313         case UPIO_MEM:
314                 return readb(up->port.membase + offset);
315
316         case UPIO_MEM32:
317                 return readl(up->port.membase + offset);
318
319 #ifdef CONFIG_SERIAL_8250_AU1X00
320         case UPIO_AU:
321                 return __raw_readl(up->port.membase + offset);
322 #endif
323
324         case UPIO_TSI:
325                 if (offset == UART_IIR) {
326                         tmp = readl(up->port.membase + (UART_IIR & ~3));
327                         return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
328                 } else
329                         return readb(up->port.membase + offset);
330
331         default:
332                 return inb(up->port.iobase + offset);
333         }
334 }
335
336 static void
337 serial_out(struct uart_8250_port *up, int offset, int value)
338 {
339         offset = map_8250_out_reg(up, offset) << up->port.regshift;
340
341         switch (up->port.iotype) {
342         case UPIO_HUB6:
343                 outb(up->port.hub6 - 1 + offset, up->port.iobase);
344                 outb(value, up->port.iobase + 1);
345                 break;
346
347         case UPIO_MEM:
348                 writeb(value, up->port.membase + offset);
349                 break;
350
351         case UPIO_MEM32:
352                 writel(value, up->port.membase + offset);
353                 break;
354
355 #ifdef CONFIG_SERIAL_8250_AU1X00
356         case UPIO_AU:
357                 __raw_writel(value, up->port.membase + offset);
358                 break;
359 #endif
360         case UPIO_TSI:
361                 if (!((offset == UART_IER) && (value & UART_IER_UUE)))
362                         writeb(value, up->port.membase + offset);
363                 break;
364
365         default:
366                 outb(value, up->port.iobase + offset);
367         }
368 }
369
370 /*
371  * We used to support using pause I/O for certain machines.  We
372  * haven't supported this for a while, but just in case it's badly
373  * needed for certain old 386 machines, I've left these #define's
374  * in....
375  */
376 #define serial_inp(up, offset)          serial_in(up, offset)
377 #define serial_outp(up, offset, value)  serial_out(up, offset, value)
378
379 /* Uart divisor latch read */
380 static inline int _serial_dl_read(struct uart_8250_port *up)
381 {
382         return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
383 }
384
385 /* Uart divisor latch write */
386 static inline void _serial_dl_write(struct uart_8250_port *up, int value)
387 {
388         serial_outp(up, UART_DLL, value & 0xff);
389         serial_outp(up, UART_DLM, value >> 8 & 0xff);
390 }
391
392 #ifdef CONFIG_SERIAL_8250_AU1X00
393 /* Au1x00 haven't got a standard divisor latch */
394 static int serial_dl_read(struct uart_8250_port *up)
395 {
396         if (up->port.iotype == UPIO_AU)
397                 return __raw_readl(up->port.membase + 0x28);
398         else
399                 return _serial_dl_read(up);
400 }
401
402 static void serial_dl_write(struct uart_8250_port *up, int value)
403 {
404         if (up->port.iotype == UPIO_AU)
405                 __raw_writel(value, up->port.membase + 0x28);
406         else
407                 _serial_dl_write(up, value);
408 }
409 #else
410 #define serial_dl_read(up) _serial_dl_read(up)
411 #define serial_dl_write(up, value) _serial_dl_write(up, value)
412 #endif
413
414 /*
415  * For the 16C950
416  */
417 static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
418 {
419         serial_out(up, UART_SCR, offset);
420         serial_out(up, UART_ICR, value);
421 }
422
423 static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
424 {
425         unsigned int value;
426
427         serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
428         serial_out(up, UART_SCR, offset);
429         value = serial_in(up, UART_ICR);
430         serial_icr_write(up, UART_ACR, up->acr);
431
432         return value;
433 }
434
435 /*
436  * FIFO support.
437  */
438 static inline void serial8250_clear_fifos(struct uart_8250_port *p)
439 {
440         if (p->capabilities & UART_CAP_FIFO) {
441                 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
442                 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
443                                UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
444                 serial_outp(p, UART_FCR, 0);
445         }
446 }
447
448 /*
449  * IER sleep support.  UARTs which have EFRs need the "extended
450  * capability" bit enabled.  Note that on XR16C850s, we need to
451  * reset LCR to write to IER.
452  */
453 static inline void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
454 {
455         if (p->capabilities & UART_CAP_SLEEP) {
456                 if (p->capabilities & UART_CAP_EFR) {
457                         serial_outp(p, UART_LCR, 0xBF);
458                         serial_outp(p, UART_EFR, UART_EFR_ECB);
459                         serial_outp(p, UART_LCR, 0);
460                 }
461                 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
462                 if (p->capabilities & UART_CAP_EFR) {
463                         serial_outp(p, UART_LCR, 0xBF);
464                         serial_outp(p, UART_EFR, 0);
465                         serial_outp(p, UART_LCR, 0);
466                 }
467         }
468 }
469
470 #ifdef CONFIG_SERIAL_8250_RSA
471 /*
472  * Attempts to turn on the RSA FIFO.  Returns zero on failure.
473  * We set the port uart clock rate if we succeed.
474  */
475 static int __enable_rsa(struct uart_8250_port *up)
476 {
477         unsigned char mode;
478         int result;
479
480         mode = serial_inp(up, UART_RSA_MSR);
481         result = mode & UART_RSA_MSR_FIFO;
482
483         if (!result) {
484                 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
485                 mode = serial_inp(up, UART_RSA_MSR);
486                 result = mode & UART_RSA_MSR_FIFO;
487         }
488
489         if (result)
490                 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
491
492         return result;
493 }
494
495 static void enable_rsa(struct uart_8250_port *up)
496 {
497         if (up->port.type == PORT_RSA) {
498                 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
499                         spin_lock_irq(&up->port.lock);
500                         __enable_rsa(up);
501                         spin_unlock_irq(&up->port.lock);
502                 }
503                 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
504                         serial_outp(up, UART_RSA_FRR, 0);
505         }
506 }
507
508 /*
509  * Attempts to turn off the RSA FIFO.  Returns zero on failure.
510  * It is unknown why interrupts were disabled in here.  However,
511  * the caller is expected to preserve this behaviour by grabbing
512  * the spinlock before calling this function.
513  */
514 static void disable_rsa(struct uart_8250_port *up)
515 {
516         unsigned char mode;
517         int result;
518
519         if (up->port.type == PORT_RSA &&
520             up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
521                 spin_lock_irq(&up->port.lock);
522
523                 mode = serial_inp(up, UART_RSA_MSR);
524                 result = !(mode & UART_RSA_MSR_FIFO);
525
526                 if (!result) {
527                         serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
528                         mode = serial_inp(up, UART_RSA_MSR);
529                         result = !(mode & UART_RSA_MSR_FIFO);
530                 }
531
532                 if (result)
533                         up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
534                 spin_unlock_irq(&up->port.lock);
535         }
536 }
537 #endif /* CONFIG_SERIAL_8250_RSA */
538
539 /*
540  * This is a quickie test to see how big the FIFO is.
541  * It doesn't work at all the time, more's the pity.
542  */
543 static int size_fifo(struct uart_8250_port *up)
544 {
545         unsigned char old_fcr, old_mcr, old_lcr;
546         unsigned short old_dl;
547         int count;
548
549         old_lcr = serial_inp(up, UART_LCR);
550         serial_outp(up, UART_LCR, 0);
551         old_fcr = serial_inp(up, UART_FCR);
552         old_mcr = serial_inp(up, UART_MCR);
553         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
554                     UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
555         serial_outp(up, UART_MCR, UART_MCR_LOOP);
556         serial_outp(up, UART_LCR, UART_LCR_DLAB);
557         old_dl = serial_dl_read(up);
558         serial_dl_write(up, 0x0001);
559         serial_outp(up, UART_LCR, 0x03);
560         for (count = 0; count < 256; count++)
561                 serial_outp(up, UART_TX, count);
562         mdelay(20);/* FIXME - schedule_timeout */
563         for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
564              (count < 256); count++)
565                 serial_inp(up, UART_RX);
566         serial_outp(up, UART_FCR, old_fcr);
567         serial_outp(up, UART_MCR, old_mcr);
568         serial_outp(up, UART_LCR, UART_LCR_DLAB);
569         serial_dl_write(up, old_dl);
570         serial_outp(up, UART_LCR, old_lcr);
571
572         return count;
573 }
574
575 /*
576  * Read UART ID using the divisor method - set DLL and DLM to zero
577  * and the revision will be in DLL and device type in DLM.  We
578  * preserve the device state across this.
579  */
580 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
581 {
582         unsigned char old_dll, old_dlm, old_lcr;
583         unsigned int id;
584
585         old_lcr = serial_inp(p, UART_LCR);
586         serial_outp(p, UART_LCR, UART_LCR_DLAB);
587
588         old_dll = serial_inp(p, UART_DLL);
589         old_dlm = serial_inp(p, UART_DLM);
590
591         serial_outp(p, UART_DLL, 0);
592         serial_outp(p, UART_DLM, 0);
593
594         id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
595
596         serial_outp(p, UART_DLL, old_dll);
597         serial_outp(p, UART_DLM, old_dlm);
598         serial_outp(p, UART_LCR, old_lcr);
599
600         return id;
601 }
602
603 /*
604  * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
605  * When this function is called we know it is at least a StarTech
606  * 16650 V2, but it might be one of several StarTech UARTs, or one of
607  * its clones.  (We treat the broken original StarTech 16650 V1 as a
608  * 16550, and why not?  Startech doesn't seem to even acknowledge its
609  * existence.)
610  * 
611  * What evil have men's minds wrought...
612  */
613 static void autoconfig_has_efr(struct uart_8250_port *up)
614 {
615         unsigned int id1, id2, id3, rev;
616
617         /*
618          * Everything with an EFR has SLEEP
619          */
620         up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
621
622         /*
623          * First we check to see if it's an Oxford Semiconductor UART.
624          *
625          * If we have to do this here because some non-National
626          * Semiconductor clone chips lock up if you try writing to the
627          * LSR register (which serial_icr_read does)
628          */
629
630         /*
631          * Check for Oxford Semiconductor 16C950.
632          *
633          * EFR [4] must be set else this test fails.
634          *
635          * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
636          * claims that it's needed for 952 dual UART's (which are not
637          * recommended for new designs).
638          */
639         up->acr = 0;
640         serial_out(up, UART_LCR, 0xBF);
641         serial_out(up, UART_EFR, UART_EFR_ECB);
642         serial_out(up, UART_LCR, 0x00);
643         id1 = serial_icr_read(up, UART_ID1);
644         id2 = serial_icr_read(up, UART_ID2);
645         id3 = serial_icr_read(up, UART_ID3);
646         rev = serial_icr_read(up, UART_REV);
647
648         DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
649
650         if (id1 == 0x16 && id2 == 0xC9 &&
651             (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
652                 up->port.type = PORT_16C950;
653
654                 /*
655                  * Enable work around for the Oxford Semiconductor 952 rev B
656                  * chip which causes it to seriously miscalculate baud rates
657                  * when DLL is 0.
658                  */
659                 if (id3 == 0x52 && rev == 0x01)
660                         up->bugs |= UART_BUG_QUOT;
661                 return;
662         }
663         
664         /*
665          * We check for a XR16C850 by setting DLL and DLM to 0, and then
666          * reading back DLL and DLM.  The chip type depends on the DLM
667          * value read back:
668          *  0x10 - XR16C850 and the DLL contains the chip revision.
669          *  0x12 - XR16C2850.
670          *  0x14 - XR16C854.
671          */
672         id1 = autoconfig_read_divisor_id(up);
673         DEBUG_AUTOCONF("850id=%04x ", id1);
674
675         id2 = id1 >> 8;
676         if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
677                 up->port.type = PORT_16850;
678                 return;
679         }
680
681         /*
682          * It wasn't an XR16C850.
683          *
684          * We distinguish between the '654 and the '650 by counting
685          * how many bytes are in the FIFO.  I'm using this for now,
686          * since that's the technique that was sent to me in the
687          * serial driver update, but I'm not convinced this works.
688          * I've had problems doing this in the past.  -TYT
689          */
690         if (size_fifo(up) == 64)
691                 up->port.type = PORT_16654;
692         else
693                 up->port.type = PORT_16650V2;
694 }
695
696 /*
697  * We detected a chip without a FIFO.  Only two fall into
698  * this category - the original 8250 and the 16450.  The
699  * 16450 has a scratch register (accessible with LCR=0)
700  */
701 static void autoconfig_8250(struct uart_8250_port *up)
702 {
703         unsigned char scratch, status1, status2;
704
705         up->port.type = PORT_8250;
706
707         scratch = serial_in(up, UART_SCR);
708         serial_outp(up, UART_SCR, 0xa5);
709         status1 = serial_in(up, UART_SCR);
710         serial_outp(up, UART_SCR, 0x5a);
711         status2 = serial_in(up, UART_SCR);
712         serial_outp(up, UART_SCR, scratch);
713
714         if (status1 == 0xa5 && status2 == 0x5a)
715                 up->port.type = PORT_16450;
716 }
717
718 static int broken_efr(struct uart_8250_port *up)
719 {
720         /*
721          * Exar ST16C2550 "A2" devices incorrectly detect as
722          * having an EFR, and report an ID of 0x0201.  See
723          * http://www.exar.com/info.php?pdf=dan180_oct2004.pdf
724          */
725         if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
726                 return 1;
727
728         return 0;
729 }
730
731 /*
732  * We know that the chip has FIFOs.  Does it have an EFR?  The
733  * EFR is located in the same register position as the IIR and
734  * we know the top two bits of the IIR are currently set.  The
735  * EFR should contain zero.  Try to read the EFR.
736  */
737 static void autoconfig_16550a(struct uart_8250_port *up)
738 {
739         unsigned char status1, status2;
740         unsigned int iersave;
741
742         up->port.type = PORT_16550A;
743         up->capabilities |= UART_CAP_FIFO;
744
745         /*
746          * Check for presence of the EFR when DLAB is set.
747          * Only ST16C650V1 UARTs pass this test.
748          */
749         serial_outp(up, UART_LCR, UART_LCR_DLAB);
750         if (serial_in(up, UART_EFR) == 0) {
751                 serial_outp(up, UART_EFR, 0xA8);
752                 if (serial_in(up, UART_EFR) != 0) {
753                         DEBUG_AUTOCONF("EFRv1 ");
754                         up->port.type = PORT_16650;
755                         up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
756                 } else {
757                         DEBUG_AUTOCONF("Motorola 8xxx DUART ");
758                 }
759                 serial_outp(up, UART_EFR, 0);
760                 return;
761         }
762
763         /*
764          * Maybe it requires 0xbf to be written to the LCR.
765          * (other ST16C650V2 UARTs, TI16C752A, etc)
766          */
767         serial_outp(up, UART_LCR, 0xBF);
768         if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
769                 DEBUG_AUTOCONF("EFRv2 ");
770                 autoconfig_has_efr(up);
771                 return;
772         }
773
774         /*
775          * Check for a National Semiconductor SuperIO chip.
776          * Attempt to switch to bank 2, read the value of the LOOP bit
777          * from EXCR1. Switch back to bank 0, change it in MCR. Then
778          * switch back to bank 2, read it from EXCR1 again and check
779          * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
780          */
781         serial_outp(up, UART_LCR, 0);
782         status1 = serial_in(up, UART_MCR);
783         serial_outp(up, UART_LCR, 0xE0);
784         status2 = serial_in(up, 0x02); /* EXCR1 */
785
786         if (!((status2 ^ status1) & UART_MCR_LOOP)) {
787                 serial_outp(up, UART_LCR, 0);
788                 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
789                 serial_outp(up, UART_LCR, 0xE0);
790                 status2 = serial_in(up, 0x02); /* EXCR1 */
791                 serial_outp(up, UART_LCR, 0);
792                 serial_outp(up, UART_MCR, status1);
793
794                 if ((status2 ^ status1) & UART_MCR_LOOP) {
795                         unsigned short quot;
796
797                         serial_outp(up, UART_LCR, 0xE0);
798
799                         quot = serial_dl_read(up);
800                         quot <<= 3;
801
802                         status1 = serial_in(up, 0x04); /* EXCR1 */
803                         status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
804                         status1 |= 0x10;  /* 1.625 divisor for baud_base --> 921600 */
805                         serial_outp(up, 0x04, status1);
806                         
807                         serial_dl_write(up, quot);
808
809                         serial_outp(up, UART_LCR, 0);
810
811                         up->port.uartclk = 921600*16;
812                         up->port.type = PORT_NS16550A;
813                         up->capabilities |= UART_NATSEMI;
814                         return;
815                 }
816         }
817
818         /*
819          * No EFR.  Try to detect a TI16750, which only sets bit 5 of
820          * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
821          * Try setting it with and without DLAB set.  Cheap clones
822          * set bit 5 without DLAB set.
823          */
824         serial_outp(up, UART_LCR, 0);
825         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
826         status1 = serial_in(up, UART_IIR) >> 5;
827         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
828         serial_outp(up, UART_LCR, UART_LCR_DLAB);
829         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
830         status2 = serial_in(up, UART_IIR) >> 5;
831         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
832         serial_outp(up, UART_LCR, 0);
833
834         DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
835
836         if (status1 == 6 && status2 == 7) {
837                 up->port.type = PORT_16750;
838                 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
839                 return;
840         }
841
842         /*
843          * Try writing and reading the UART_IER_UUE bit (b6).
844          * If it works, this is probably one of the Xscale platform's
845          * internal UARTs.
846          * We're going to explicitly set the UUE bit to 0 before
847          * trying to write and read a 1 just to make sure it's not
848          * already a 1 and maybe locked there before we even start start.
849          */
850         iersave = serial_in(up, UART_IER);
851         serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
852         if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
853                 /*
854                  * OK it's in a known zero state, try writing and reading
855                  * without disturbing the current state of the other bits.
856                  */
857                 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
858                 if (serial_in(up, UART_IER) & UART_IER_UUE) {
859                         /*
860                          * It's an Xscale.
861                          * We'll leave the UART_IER_UUE bit set to 1 (enabled).
862                          */
863                         DEBUG_AUTOCONF("Xscale ");
864                         up->port.type = PORT_XSCALE;
865                         up->capabilities |= UART_CAP_UUE;
866                         return;
867                 }
868         } else {
869                 /*
870                  * If we got here we couldn't force the IER_UUE bit to 0.
871                  * Log it and continue.
872                  */
873                 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
874         }
875         serial_outp(up, UART_IER, iersave);
876 }
877
878 /*
879  * This routine is called by rs_init() to initialize a specific serial
880  * port.  It determines what type of UART chip this serial port is
881  * using: 8250, 16450, 16550, 16550A.  The important question is
882  * whether or not this UART is a 16550A or not, since this will
883  * determine whether or not we can use its FIFO features or not.
884  */
885 static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
886 {
887         unsigned char status1, scratch, scratch2, scratch3;
888         unsigned char save_lcr, save_mcr;
889         unsigned long flags;
890
891         if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
892                 return;
893
894         DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%p): ",
895                         up->port.line, up->port.iobase, up->port.membase);
896
897         /*
898          * We really do need global IRQs disabled here - we're going to
899          * be frobbing the chips IRQ enable register to see if it exists.
900          */
901         spin_lock_irqsave(&up->port.lock, flags);
902 //      save_flags(flags); cli();
903
904         up->capabilities = 0;
905         up->bugs = 0;
906
907         if (!(up->port.flags & UPF_BUGGY_UART)) {
908                 /*
909                  * Do a simple existence test first; if we fail this,
910                  * there's no point trying anything else.
911                  * 
912                  * 0x80 is used as a nonsense port to prevent against
913                  * false positives due to ISA bus float.  The
914                  * assumption is that 0x80 is a non-existent port;
915                  * which should be safe since include/asm/io.h also
916                  * makes this assumption.
917                  *
918                  * Note: this is safe as long as MCR bit 4 is clear
919                  * and the device is in "PC" mode.
920                  */
921                 scratch = serial_inp(up, UART_IER);
922                 serial_outp(up, UART_IER, 0);
923 #ifdef __i386__
924                 outb(0xff, 0x080);
925 #endif
926                 scratch2 = serial_inp(up, UART_IER);
927                 serial_outp(up, UART_IER, 0x0F);
928 #ifdef __i386__
929                 outb(0, 0x080);
930 #endif
931                 scratch3 = serial_inp(up, UART_IER);
932                 serial_outp(up, UART_IER, scratch);
933                 if (scratch2 != 0 || scratch3 != 0x0F) {
934                         /*
935                          * We failed; there's nothing here
936                          */
937                         DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
938                                        scratch2, scratch3);
939                         goto out;
940                 }
941         }
942
943         save_mcr = serial_in(up, UART_MCR);
944         save_lcr = serial_in(up, UART_LCR);
945
946         /* 
947          * Check to see if a UART is really there.  Certain broken
948          * internal modems based on the Rockwell chipset fail this
949          * test, because they apparently don't implement the loopback
950          * test mode.  So this test is skipped on the COM 1 through
951          * COM 4 ports.  This *should* be safe, since no board
952          * manufacturer would be stupid enough to design a board
953          * that conflicts with COM 1-4 --- we hope!
954          */
955         if (!(up->port.flags & UPF_SKIP_TEST)) {
956                 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
957                 status1 = serial_inp(up, UART_MSR) & 0xF0;
958                 serial_outp(up, UART_MCR, save_mcr);
959                 if (status1 != 0x90) {
960                         DEBUG_AUTOCONF("LOOP test failed (%02x) ",
961                                        status1);
962                         goto out;
963                 }
964         }
965
966         /*
967          * We're pretty sure there's a port here.  Lets find out what
968          * type of port it is.  The IIR top two bits allows us to find
969          * out if it's 8250 or 16450, 16550, 16550A or later.  This
970          * determines what we test for next.
971          *
972          * We also initialise the EFR (if any) to zero for later.  The
973          * EFR occupies the same register location as the FCR and IIR.
974          */
975         serial_outp(up, UART_LCR, 0xBF);
976         serial_outp(up, UART_EFR, 0);
977         serial_outp(up, UART_LCR, 0);
978
979         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
980         scratch = serial_in(up, UART_IIR) >> 6;
981
982         DEBUG_AUTOCONF("iir=%d ", scratch);
983
984         switch (scratch) {
985         case 0:
986                 autoconfig_8250(up);
987                 break;
988         case 1:
989                 up->port.type = PORT_UNKNOWN;
990                 break;
991         case 2:
992                 up->port.type = PORT_16550;
993                 break;
994         case 3:
995                 autoconfig_16550a(up);
996                 break;
997         }
998
999 #ifdef CONFIG_SERIAL_8250_RSA
1000         /*
1001          * Only probe for RSA ports if we got the region.
1002          */
1003         if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1004                 int i;
1005
1006                 for (i = 0 ; i < probe_rsa_count; ++i) {
1007                         if (probe_rsa[i] == up->port.iobase &&
1008                             __enable_rsa(up)) {
1009                                 up->port.type = PORT_RSA;
1010                                 break;
1011                         }
1012                 }
1013         }
1014 #endif
1015
1016 #ifdef CONFIG_SERIAL_8250_AU1X00
1017         /* if access method is AU, it is a 16550 with a quirk */
1018         if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
1019                 up->bugs |= UART_BUG_NOMSR;
1020 #endif
1021
1022         serial_outp(up, UART_LCR, save_lcr);
1023
1024         if (up->capabilities != uart_config[up->port.type].flags) {
1025                 printk(KERN_WARNING
1026                        "ttyS%d: detected caps %08x should be %08x\n",
1027                         up->port.line, up->capabilities,
1028                         uart_config[up->port.type].flags);
1029         }
1030
1031         up->port.fifosize = uart_config[up->port.type].fifo_size;
1032         up->capabilities = uart_config[up->port.type].flags;
1033         up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1034
1035         if (up->port.type == PORT_UNKNOWN)
1036                 goto out;
1037
1038         /*
1039          * Reset the UART.
1040          */
1041 #ifdef CONFIG_SERIAL_8250_RSA
1042         if (up->port.type == PORT_RSA)
1043                 serial_outp(up, UART_RSA_FRR, 0);
1044 #endif
1045         serial_outp(up, UART_MCR, save_mcr);
1046         serial8250_clear_fifos(up);
1047         (void)serial_in(up, UART_RX);
1048         if (up->capabilities & UART_CAP_UUE)
1049                 serial_outp(up, UART_IER, UART_IER_UUE);
1050         else
1051                 serial_outp(up, UART_IER, 0);
1052
1053  out:   
1054         spin_unlock_irqrestore(&up->port.lock, flags);
1055 //      restore_flags(flags);
1056         DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1057 }
1058
1059 static void autoconfig_irq(struct uart_8250_port *up)
1060 {
1061         unsigned char save_mcr, save_ier;
1062         unsigned char save_ICP = 0;
1063         unsigned int ICP = 0;
1064         unsigned long irqs;
1065         int irq;
1066
1067         if (up->port.flags & UPF_FOURPORT) {
1068                 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1069                 save_ICP = inb_p(ICP);
1070                 outb_p(0x80, ICP);
1071                 (void) inb_p(ICP);
1072         }
1073
1074         /* forget possible initially masked and pending IRQ */
1075         probe_irq_off(probe_irq_on());
1076         save_mcr = serial_inp(up, UART_MCR);
1077         save_ier = serial_inp(up, UART_IER);
1078         serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
1079         
1080         irqs = probe_irq_on();
1081         serial_outp(up, UART_MCR, 0);
1082         udelay (10);
1083         if (up->port.flags & UPF_FOURPORT)  {
1084                 serial_outp(up, UART_MCR,
1085                             UART_MCR_DTR | UART_MCR_RTS);
1086         } else {
1087                 serial_outp(up, UART_MCR,
1088                             UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1089         }
1090         serial_outp(up, UART_IER, 0x0f);        /* enable all intrs */
1091         (void)serial_inp(up, UART_LSR);
1092         (void)serial_inp(up, UART_RX);
1093         (void)serial_inp(up, UART_IIR);
1094         (void)serial_inp(up, UART_MSR);
1095         serial_outp(up, UART_TX, 0xFF);
1096         udelay (20);
1097         irq = probe_irq_off(irqs);
1098
1099         serial_outp(up, UART_MCR, save_mcr);
1100         serial_outp(up, UART_IER, save_ier);
1101
1102         if (up->port.flags & UPF_FOURPORT)
1103                 outb_p(save_ICP, ICP);
1104
1105         up->port.irq = (irq > 0) ? irq : 0;
1106 }
1107
1108 static inline void __stop_tx(struct uart_8250_port *p)
1109 {
1110         if (p->ier & UART_IER_THRI) {
1111                 p->ier &= ~UART_IER_THRI;
1112                 serial_out(p, UART_IER, p->ier);
1113         }
1114 }
1115
1116 static void serial8250_stop_tx(struct uart_port *port)
1117 {
1118         struct uart_8250_port *up = (struct uart_8250_port *)port;
1119
1120         __stop_tx(up);
1121
1122         /*
1123          * We really want to stop the transmitter from sending.
1124          */
1125         if (up->port.type == PORT_16C950) {
1126                 up->acr |= UART_ACR_TXDIS;
1127                 serial_icr_write(up, UART_ACR, up->acr);
1128         }
1129 }
1130
1131 static void transmit_chars(struct uart_8250_port *up);
1132
1133 static void serial8250_start_tx(struct uart_port *port)
1134 {
1135         struct uart_8250_port *up = (struct uart_8250_port *)port;
1136
1137         if (!(up->ier & UART_IER_THRI)) {
1138                 up->ier |= UART_IER_THRI;
1139                 serial_out(up, UART_IER, up->ier);
1140
1141                 if (up->bugs & UART_BUG_TXEN) {
1142                         unsigned char lsr, iir;
1143                         lsr = serial_in(up, UART_LSR);
1144                         iir = serial_in(up, UART_IIR);
1145                         if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT)
1146                                 transmit_chars(up);
1147                 }
1148         }
1149
1150         /*
1151          * Re-enable the transmitter if we disabled it.
1152          */
1153         if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1154                 up->acr &= ~UART_ACR_TXDIS;
1155                 serial_icr_write(up, UART_ACR, up->acr);
1156         }
1157 }
1158
1159 static void serial8250_stop_rx(struct uart_port *port)
1160 {
1161         struct uart_8250_port *up = (struct uart_8250_port *)port;
1162
1163         up->ier &= ~UART_IER_RLSI;
1164         up->port.read_status_mask &= ~UART_LSR_DR;
1165         serial_out(up, UART_IER, up->ier);
1166 }
1167
1168 static void serial8250_enable_ms(struct uart_port *port)
1169 {
1170         struct uart_8250_port *up = (struct uart_8250_port *)port;
1171
1172         /* no MSR capabilities */
1173         if (up->bugs & UART_BUG_NOMSR)
1174                 return;
1175
1176         up->ier |= UART_IER_MSI;
1177         serial_out(up, UART_IER, up->ier);
1178 }
1179
1180 static void
1181 receive_chars(struct uart_8250_port *up, int *status)
1182 {
1183         struct tty_struct *tty = up->port.info->tty;
1184         unsigned char ch, lsr = *status;
1185         int max_count = 256;
1186         char flag;
1187
1188         do {
1189                 ch = serial_inp(up, UART_RX);
1190                 flag = TTY_NORMAL;
1191                 up->port.icount.rx++;
1192
1193 #ifdef CONFIG_SERIAL_8250_CONSOLE
1194                 /*
1195                  * Recover the break flag from console xmit
1196                  */
1197                 if (up->port.line == up->port.cons->index) {
1198                         lsr |= up->lsr_break_flag;
1199                         up->lsr_break_flag = 0;
1200                 }
1201 #endif
1202
1203                 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
1204                                     UART_LSR_FE | UART_LSR_OE))) {
1205                         /*
1206                          * For statistics only
1207                          */
1208                         if (lsr & UART_LSR_BI) {
1209                                 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1210                                 up->port.icount.brk++;
1211                                 /*
1212                                  * We do the SysRQ and SAK checking
1213                                  * here because otherwise the break
1214                                  * may get masked by ignore_status_mask
1215                                  * or read_status_mask.
1216                                  */
1217                                 if (uart_handle_break(&up->port))
1218                                         goto ignore_char;
1219                         } else if (lsr & UART_LSR_PE)
1220                                 up->port.icount.parity++;
1221                         else if (lsr & UART_LSR_FE)
1222                                 up->port.icount.frame++;
1223                         if (lsr & UART_LSR_OE)
1224                                 up->port.icount.overrun++;
1225
1226                         /*
1227                          * Mask off conditions which should be ignored.
1228                          */
1229                         lsr &= up->port.read_status_mask;
1230
1231                         if (lsr & UART_LSR_BI) {
1232                                 DEBUG_INTR("handling break....");
1233                                 flag = TTY_BREAK;
1234                         } else if (lsr & UART_LSR_PE)
1235                                 flag = TTY_PARITY;
1236                         else if (lsr & UART_LSR_FE)
1237                                 flag = TTY_FRAME;
1238                 }
1239                 if (uart_handle_sysrq_char(&up->port, ch))
1240                         goto ignore_char;
1241
1242                 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1243
1244         ignore_char:
1245                 lsr = serial_inp(up, UART_LSR);
1246         } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
1247         spin_unlock(&up->port.lock);
1248         tty_flip_buffer_push(tty);
1249         spin_lock(&up->port.lock);
1250         *status = lsr;
1251 }
1252
1253 static void transmit_chars(struct uart_8250_port *up)
1254 {
1255         struct circ_buf *xmit = &up->port.info->xmit;
1256         int count;
1257
1258         if (up->port.x_char) {
1259                 serial_outp(up, UART_TX, up->port.x_char);
1260                 up->port.icount.tx++;
1261                 up->port.x_char = 0;
1262                 return;
1263         }
1264         if (uart_tx_stopped(&up->port)) {
1265                 serial8250_stop_tx(&up->port);
1266                 return;
1267         }
1268         if (uart_circ_empty(xmit)) {
1269                 __stop_tx(up);
1270                 return;
1271         }
1272
1273         count = up->tx_loadsz;
1274         do {
1275                 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1276                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1277                 up->port.icount.tx++;
1278                 if (uart_circ_empty(xmit))
1279                         break;
1280         } while (--count > 0);
1281
1282         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1283                 uart_write_wakeup(&up->port);
1284
1285         DEBUG_INTR("THRE...");
1286
1287         if (uart_circ_empty(xmit))
1288                 __stop_tx(up);
1289 }
1290
1291 static unsigned int check_modem_status(struct uart_8250_port *up)
1292 {
1293         unsigned int status = serial_in(up, UART_MSR);
1294
1295         if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1296             up->port.info != NULL) {
1297                 if (status & UART_MSR_TERI)
1298                         up->port.icount.rng++;
1299                 if (status & UART_MSR_DDSR)
1300                         up->port.icount.dsr++;
1301                 if (status & UART_MSR_DDCD)
1302                         uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1303                 if (status & UART_MSR_DCTS)
1304                         uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
1305
1306                 wake_up_interruptible(&up->port.info->delta_msr_wait);
1307         }
1308
1309         return status;
1310 }
1311
1312 /*
1313  * This handles the interrupt from one port.
1314  */
1315 static inline void
1316 serial8250_handle_port(struct uart_8250_port *up)
1317 {
1318         unsigned int status;
1319
1320         spin_lock(&up->port.lock);
1321
1322         status = serial_inp(up, UART_LSR);
1323
1324         DEBUG_INTR("status = %x...", status);
1325
1326         if (status & UART_LSR_DR)
1327                 receive_chars(up, &status);
1328         check_modem_status(up);
1329         if (status & UART_LSR_THRE)
1330                 transmit_chars(up);
1331
1332         spin_unlock(&up->port.lock);
1333 }
1334
1335 /*
1336  * This is the serial driver's interrupt routine.
1337  *
1338  * Arjan thinks the old way was overly complex, so it got simplified.
1339  * Alan disagrees, saying that need the complexity to handle the weird
1340  * nature of ISA shared interrupts.  (This is a special exception.)
1341  *
1342  * In order to handle ISA shared interrupts properly, we need to check
1343  * that all ports have been serviced, and therefore the ISA interrupt
1344  * line has been de-asserted.
1345  *
1346  * This means we need to loop through all ports. checking that they
1347  * don't have an interrupt pending.
1348  */
1349 static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
1350 {
1351         struct irq_info *i = dev_id;
1352         struct list_head *l, *end = NULL;
1353         int pass_counter = 0, handled = 0;
1354
1355         DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1356
1357         spin_lock(&i->lock);
1358
1359         l = i->head;
1360         do {
1361                 struct uart_8250_port *up;
1362                 unsigned int iir;
1363
1364                 up = list_entry(l, struct uart_8250_port, list);
1365
1366                 iir = serial_in(up, UART_IIR);
1367                 if (!(iir & UART_IIR_NO_INT)) {
1368                         serial8250_handle_port(up);
1369
1370                         handled = 1;
1371
1372                         end = NULL;
1373                 } else if (end == NULL)
1374                         end = l;
1375
1376                 l = l->next;
1377
1378                 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1379                         /* If we hit this, we're dead. */
1380                         printk(KERN_ERR "serial8250: too much work for "
1381                                 "irq%d\n", irq);
1382                         break;
1383                 }
1384         } while (l != end);
1385
1386         spin_unlock(&i->lock);
1387
1388         DEBUG_INTR("end.\n");
1389
1390         return IRQ_RETVAL(handled);
1391 }
1392
1393 /*
1394  * To support ISA shared interrupts, we need to have one interrupt
1395  * handler that ensures that the IRQ line has been deasserted
1396  * before returning.  Failing to do this will result in the IRQ
1397  * line being stuck active, and, since ISA irqs are edge triggered,
1398  * no more IRQs will be seen.
1399  */
1400 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1401 {
1402         spin_lock_irq(&i->lock);
1403
1404         if (!list_empty(i->head)) {
1405                 if (i->head == &up->list)
1406                         i->head = i->head->next;
1407                 list_del(&up->list);
1408         } else {
1409                 BUG_ON(i->head != &up->list);
1410                 i->head = NULL;
1411         }
1412
1413         spin_unlock_irq(&i->lock);
1414 }
1415
1416 static int serial_link_irq_chain(struct uart_8250_port *up)
1417 {
1418         struct irq_info *i = irq_lists + up->port.irq;
1419         int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
1420
1421         spin_lock_irq(&i->lock);
1422
1423         if (i->head) {
1424                 list_add(&up->list, i->head);
1425                 spin_unlock_irq(&i->lock);
1426
1427                 ret = 0;
1428         } else {
1429                 INIT_LIST_HEAD(&up->list);
1430                 i->head = &up->list;
1431                 spin_unlock_irq(&i->lock);
1432
1433                 ret = request_irq(up->port.irq, serial8250_interrupt,
1434                                   irq_flags, "serial", i);
1435                 if (ret < 0)
1436                         serial_do_unlink(i, up);
1437         }
1438
1439         return ret;
1440 }
1441
1442 static void serial_unlink_irq_chain(struct uart_8250_port *up)
1443 {
1444         struct irq_info *i = irq_lists + up->port.irq;
1445
1446         BUG_ON(i->head == NULL);
1447
1448         if (list_empty(i->head))
1449                 free_irq(up->port.irq, i);
1450
1451         serial_do_unlink(i, up);
1452 }
1453
1454 /*
1455  * This function is used to handle ports that do not have an
1456  * interrupt.  This doesn't work very well for 16450's, but gives
1457  * barely passable results for a 16550A.  (Although at the expense
1458  * of much CPU overhead).
1459  */
1460 static void serial8250_timeout(unsigned long data)
1461 {
1462         struct uart_8250_port *up = (struct uart_8250_port *)data;
1463         unsigned int timeout;
1464         unsigned int iir;
1465
1466         iir = serial_in(up, UART_IIR);
1467         if (!(iir & UART_IIR_NO_INT))
1468                 serial8250_handle_port(up);
1469
1470         timeout = up->port.timeout;
1471         timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
1472         mod_timer(&up->timer, jiffies + timeout);
1473 }
1474
1475 static unsigned int serial8250_tx_empty(struct uart_port *port)
1476 {
1477         struct uart_8250_port *up = (struct uart_8250_port *)port;
1478         unsigned long flags;
1479         unsigned int ret;
1480
1481         spin_lock_irqsave(&up->port.lock, flags);
1482         ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
1483         spin_unlock_irqrestore(&up->port.lock, flags);
1484
1485         return ret;
1486 }
1487
1488 static unsigned int serial8250_get_mctrl(struct uart_port *port)
1489 {
1490         struct uart_8250_port *up = (struct uart_8250_port *)port;
1491         unsigned int status;
1492         unsigned int ret;
1493
1494         status = check_modem_status(up);
1495
1496         ret = 0;
1497         if (status & UART_MSR_DCD)
1498                 ret |= TIOCM_CAR;
1499         if (status & UART_MSR_RI)
1500                 ret |= TIOCM_RNG;
1501         if (status & UART_MSR_DSR)
1502                 ret |= TIOCM_DSR;
1503         if (status & UART_MSR_CTS)
1504                 ret |= TIOCM_CTS;
1505         return ret;
1506 }
1507
1508 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1509 {
1510         struct uart_8250_port *up = (struct uart_8250_port *)port;
1511         unsigned char mcr = 0;
1512
1513         if (mctrl & TIOCM_RTS)
1514                 mcr |= UART_MCR_RTS;
1515         if (mctrl & TIOCM_DTR)
1516                 mcr |= UART_MCR_DTR;
1517         if (mctrl & TIOCM_OUT1)
1518                 mcr |= UART_MCR_OUT1;
1519         if (mctrl & TIOCM_OUT2)
1520                 mcr |= UART_MCR_OUT2;
1521         if (mctrl & TIOCM_LOOP)
1522                 mcr |= UART_MCR_LOOP;
1523
1524         mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1525
1526         serial_out(up, UART_MCR, mcr);
1527 }
1528
1529 static void serial8250_break_ctl(struct uart_port *port, int break_state)
1530 {
1531         struct uart_8250_port *up = (struct uart_8250_port *)port;
1532         unsigned long flags;
1533
1534         spin_lock_irqsave(&up->port.lock, flags);
1535         if (break_state == -1)
1536                 up->lcr |= UART_LCR_SBC;
1537         else
1538                 up->lcr &= ~UART_LCR_SBC;
1539         serial_out(up, UART_LCR, up->lcr);
1540         spin_unlock_irqrestore(&up->port.lock, flags);
1541 }
1542
1543 static int serial8250_startup(struct uart_port *port)
1544 {
1545         struct uart_8250_port *up = (struct uart_8250_port *)port;
1546         unsigned long flags;
1547         unsigned char lsr, iir;
1548         int retval;
1549
1550         up->capabilities = uart_config[up->port.type].flags;
1551         up->mcr = 0;
1552
1553         if (up->port.type == PORT_16C950) {
1554                 /* Wake up and initialize UART */
1555                 up->acr = 0;
1556                 serial_outp(up, UART_LCR, 0xBF);
1557                 serial_outp(up, UART_EFR, UART_EFR_ECB);
1558                 serial_outp(up, UART_IER, 0);
1559                 serial_outp(up, UART_LCR, 0);
1560                 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
1561                 serial_outp(up, UART_LCR, 0xBF);
1562                 serial_outp(up, UART_EFR, UART_EFR_ECB);
1563                 serial_outp(up, UART_LCR, 0);
1564         }
1565
1566 #ifdef CONFIG_SERIAL_8250_RSA
1567         /*
1568          * If this is an RSA port, see if we can kick it up to the
1569          * higher speed clock.
1570          */
1571         enable_rsa(up);
1572 #endif
1573
1574         /*
1575          * Clear the FIFO buffers and disable them.
1576          * (they will be reenabled in set_termios())
1577          */
1578         serial8250_clear_fifos(up);
1579
1580         /*
1581          * Clear the interrupt registers.
1582          */
1583         (void) serial_inp(up, UART_LSR);
1584         (void) serial_inp(up, UART_RX);
1585         (void) serial_inp(up, UART_IIR);
1586         (void) serial_inp(up, UART_MSR);
1587
1588         /*
1589          * At this point, there's no way the LSR could still be 0xff;
1590          * if it is, then bail out, because there's likely no UART
1591          * here.
1592          */
1593         if (!(up->port.flags & UPF_BUGGY_UART) &&
1594             (serial_inp(up, UART_LSR) == 0xff)) {
1595                 printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
1596                 return -ENODEV;
1597         }
1598
1599         /*
1600          * For a XR16C850, we need to set the trigger levels
1601          */
1602         if (up->port.type == PORT_16850) {
1603                 unsigned char fctr;
1604
1605                 serial_outp(up, UART_LCR, 0xbf);
1606
1607                 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
1608                 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
1609                 serial_outp(up, UART_TRG, UART_TRG_96);
1610                 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
1611                 serial_outp(up, UART_TRG, UART_TRG_96);
1612
1613                 serial_outp(up, UART_LCR, 0);
1614         }
1615
1616         /*
1617          * If the "interrupt" for this port doesn't correspond with any
1618          * hardware interrupt, we use a timer-based system.  The original
1619          * driver used to do this with IRQ0.
1620          */
1621         if (!is_real_interrupt(up->port.irq)) {
1622                 unsigned int timeout = up->port.timeout;
1623
1624                 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
1625
1626                 up->timer.data = (unsigned long)up;
1627                 mod_timer(&up->timer, jiffies + timeout);
1628         } else {
1629                 retval = serial_link_irq_chain(up);
1630                 if (retval)
1631                         return retval;
1632         }
1633
1634         /*
1635          * Now, initialize the UART
1636          */
1637         serial_outp(up, UART_LCR, UART_LCR_WLEN8);
1638
1639         spin_lock_irqsave(&up->port.lock, flags);
1640         if (up->port.flags & UPF_FOURPORT) {
1641                 if (!is_real_interrupt(up->port.irq))
1642                         up->port.mctrl |= TIOCM_OUT1;
1643         } else
1644                 /*
1645                  * Most PC uarts need OUT2 raised to enable interrupts.
1646                  */
1647                 if (is_real_interrupt(up->port.irq))
1648                         up->port.mctrl |= TIOCM_OUT2;
1649
1650         serial8250_set_mctrl(&up->port, up->port.mctrl);
1651
1652         /*
1653          * Do a quick test to see if we receive an
1654          * interrupt when we enable the TX irq.
1655          */
1656         serial_outp(up, UART_IER, UART_IER_THRI);
1657         lsr = serial_in(up, UART_LSR);
1658         iir = serial_in(up, UART_IIR);
1659         serial_outp(up, UART_IER, 0);
1660
1661         if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
1662                 if (!(up->bugs & UART_BUG_TXEN)) {
1663                         up->bugs |= UART_BUG_TXEN;
1664                         pr_debug("ttyS%d - enabling bad tx status workarounds\n",
1665                                  port->line);
1666                 }
1667         } else {
1668                 up->bugs &= ~UART_BUG_TXEN;
1669         }
1670
1671         spin_unlock_irqrestore(&up->port.lock, flags);
1672
1673         /*
1674          * Finally, enable interrupts.  Note: Modem status interrupts
1675          * are set via set_termios(), which will be occurring imminently
1676          * anyway, so we don't enable them here.
1677          */
1678         up->ier = UART_IER_RLSI | UART_IER_RDI;
1679         serial_outp(up, UART_IER, up->ier);
1680
1681         if (up->port.flags & UPF_FOURPORT) {
1682                 unsigned int icp;
1683                 /*
1684                  * Enable interrupts on the AST Fourport board
1685                  */
1686                 icp = (up->port.iobase & 0xfe0) | 0x01f;
1687                 outb_p(0x80, icp);
1688                 (void) inb_p(icp);
1689         }
1690
1691         /*
1692          * And clear the interrupt registers again for luck.
1693          */
1694         (void) serial_inp(up, UART_LSR);
1695         (void) serial_inp(up, UART_RX);
1696         (void) serial_inp(up, UART_IIR);
1697         (void) serial_inp(up, UART_MSR);
1698
1699         return 0;
1700 }
1701
1702 static void serial8250_shutdown(struct uart_port *port)
1703 {
1704         struct uart_8250_port *up = (struct uart_8250_port *)port;
1705         unsigned long flags;
1706
1707         /*
1708          * Disable interrupts from this port
1709          */
1710         up->ier = 0;
1711         serial_outp(up, UART_IER, 0);
1712
1713         spin_lock_irqsave(&up->port.lock, flags);
1714         if (up->port.flags & UPF_FOURPORT) {
1715                 /* reset interrupts on the AST Fourport board */
1716                 inb((up->port.iobase & 0xfe0) | 0x1f);
1717                 up->port.mctrl |= TIOCM_OUT1;
1718         } else
1719                 up->port.mctrl &= ~TIOCM_OUT2;
1720
1721         serial8250_set_mctrl(&up->port, up->port.mctrl);
1722         spin_unlock_irqrestore(&up->port.lock, flags);
1723
1724         /*
1725          * Disable break condition and FIFOs
1726          */
1727         serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
1728         serial8250_clear_fifos(up);
1729
1730 #ifdef CONFIG_SERIAL_8250_RSA
1731         /*
1732          * Reset the RSA board back to 115kbps compat mode.
1733          */
1734         disable_rsa(up);
1735 #endif
1736
1737         /*
1738          * Read data port to reset things, and then unlink from
1739          * the IRQ chain.
1740          */
1741         (void) serial_in(up, UART_RX);
1742
1743         if (!is_real_interrupt(up->port.irq))
1744                 del_timer_sync(&up->timer);
1745         else
1746                 serial_unlink_irq_chain(up);
1747 }
1748
1749 static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
1750 {
1751         unsigned int quot;
1752
1753         /*
1754          * Handle magic divisors for baud rates above baud_base on
1755          * SMSC SuperIO chips.
1756          */
1757         if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
1758             baud == (port->uartclk/4))
1759                 quot = 0x8001;
1760         else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
1761                  baud == (port->uartclk/8))
1762                 quot = 0x8002;
1763         /*
1764          * For 16C950s UART_TCR is used in combination with divisor==1
1765          * to achieve baud rates up to baud_base*4.
1766          */
1767         else if ((port->type == PORT_16C950) &&
1768                  baud > (port->uartclk/16))
1769                 quot = 1;
1770
1771         else
1772                 quot = uart_get_divisor(port, baud);
1773
1774         return quot;
1775 }
1776
1777 static void
1778 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
1779                        struct ktermios *old)
1780 {
1781         struct uart_8250_port *up = (struct uart_8250_port *)port;
1782         unsigned char cval, fcr = 0;
1783         unsigned long flags;
1784         unsigned int baud, quot, max_baud;
1785
1786         switch (termios->c_cflag & CSIZE) {
1787         case CS5:
1788                 cval = UART_LCR_WLEN5;
1789                 break;
1790         case CS6:
1791                 cval = UART_LCR_WLEN6;
1792                 break;
1793         case CS7:
1794                 cval = UART_LCR_WLEN7;
1795                 break;
1796         default:
1797         case CS8:
1798                 cval = UART_LCR_WLEN8;
1799                 break;
1800         }
1801
1802         if (termios->c_cflag & CSTOPB)
1803                 cval |= UART_LCR_STOP;
1804         if (termios->c_cflag & PARENB)
1805                 cval |= UART_LCR_PARITY;
1806         if (!(termios->c_cflag & PARODD))
1807                 cval |= UART_LCR_EPAR;
1808 #ifdef CMSPAR
1809         if (termios->c_cflag & CMSPAR)
1810                 cval |= UART_LCR_SPAR;
1811 #endif
1812
1813         /*
1814          * Ask the core to calculate the divisor for us.
1815          */
1816         max_baud = (up->port.type == PORT_16C950 ? port->uartclk/4 : port->uartclk/16);
1817         baud = uart_get_baud_rate(port, termios, old, 0, max_baud); 
1818         quot = serial8250_get_divisor(port, baud);
1819
1820         /*
1821          * Oxford Semi 952 rev B workaround
1822          */
1823         if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
1824                 quot ++;
1825
1826         if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
1827                 if (baud < 2400)
1828                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1829                 else
1830                         fcr = uart_config[up->port.type].fcr;
1831         }
1832
1833         /*
1834          * MCR-based auto flow control.  When AFE is enabled, RTS will be
1835          * deasserted when the receive FIFO contains more characters than
1836          * the trigger, or the MCR RTS bit is cleared.  In the case where
1837          * the remote UART is not using CTS auto flow control, we must
1838          * have sufficient FIFO entries for the latency of the remote
1839          * UART to respond.  IOW, at least 32 bytes of FIFO.
1840          */
1841         if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
1842                 up->mcr &= ~UART_MCR_AFE;
1843                 if (termios->c_cflag & CRTSCTS)
1844                         up->mcr |= UART_MCR_AFE;
1845         }
1846
1847         /*
1848          * Ok, we're now changing the port state.  Do it with
1849          * interrupts disabled.
1850          */
1851         spin_lock_irqsave(&up->port.lock, flags);
1852
1853         /* 
1854          * 16C950 supports additional prescaler ratios between 1:16 and 1:4
1855          * thus increasing max baud rate to uartclk/4.
1856          */
1857         if (up->port.type == PORT_16C950) {
1858                 if (baud == port->uartclk/4)
1859                         serial_icr_write(up, UART_TCR, 0x4);
1860                 else if (baud == port->uartclk/8)
1861                         serial_icr_write(up, UART_TCR, 0x8);
1862                 else
1863                         serial_icr_write(up, UART_TCR, 0);
1864         }
1865         
1866         /*
1867          * Update the per-port timeout.
1868          */
1869         uart_update_timeout(port, termios->c_cflag, baud);
1870
1871         up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1872         if (termios->c_iflag & INPCK)
1873                 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1874         if (termios->c_iflag & (BRKINT | PARMRK))
1875                 up->port.read_status_mask |= UART_LSR_BI;
1876
1877         /*
1878          * Characteres to ignore
1879          */
1880         up->port.ignore_status_mask = 0;
1881         if (termios->c_iflag & IGNPAR)
1882                 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1883         if (termios->c_iflag & IGNBRK) {
1884                 up->port.ignore_status_mask |= UART_LSR_BI;
1885                 /*
1886                  * If we're ignoring parity and break indicators,
1887                  * ignore overruns too (for real raw support).
1888                  */
1889                 if (termios->c_iflag & IGNPAR)
1890                         up->port.ignore_status_mask |= UART_LSR_OE;
1891         }
1892
1893         /*
1894          * ignore all characters if CREAD is not set
1895          */
1896         if ((termios->c_cflag & CREAD) == 0)
1897                 up->port.ignore_status_mask |= UART_LSR_DR;
1898
1899         /*
1900          * CTS flow control flag and modem status interrupts
1901          */
1902         up->ier &= ~UART_IER_MSI;
1903         if (!(up->bugs & UART_BUG_NOMSR) &&
1904                         UART_ENABLE_MS(&up->port, termios->c_cflag))
1905                 up->ier |= UART_IER_MSI;
1906         if (up->capabilities & UART_CAP_UUE)
1907                 up->ier |= UART_IER_UUE | UART_IER_RTOIE;
1908
1909         serial_out(up, UART_IER, up->ier);
1910
1911         if (up->capabilities & UART_CAP_EFR) {
1912                 unsigned char efr = 0;
1913                 /*
1914                  * TI16C752/Startech hardware flow control.  FIXME:
1915                  * - TI16C752 requires control thresholds to be set.
1916                  * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
1917                  */
1918                 if (termios->c_cflag & CRTSCTS)
1919                         efr |= UART_EFR_CTS;
1920
1921                 serial_outp(up, UART_LCR, 0xBF);
1922                 serial_outp(up, UART_EFR, efr);
1923         }
1924
1925 #ifdef CONFIG_ARCH_OMAP15XX
1926         /* Workaround to enable 115200 baud on OMAP1510 internal ports */
1927         if (cpu_is_omap1510() && is_omap_port((unsigned int)up->port.membase)) {
1928                 if (baud == 115200) {
1929                         quot = 1;
1930                         serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
1931                 } else
1932                         serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
1933         }
1934 #endif
1935
1936         if (up->capabilities & UART_NATSEMI) {
1937                 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
1938                 serial_outp(up, UART_LCR, 0xe0);
1939         } else {
1940                 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
1941         }
1942
1943         serial_dl_write(up, quot);
1944
1945         /*
1946          * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
1947          * is written without DLAB set, this mode will be disabled.
1948          */
1949         if (up->port.type == PORT_16750)
1950                 serial_outp(up, UART_FCR, fcr);
1951
1952         serial_outp(up, UART_LCR, cval);                /* reset DLAB */
1953         up->lcr = cval;                                 /* Save LCR */
1954         if (up->port.type != PORT_16750) {
1955                 if (fcr & UART_FCR_ENABLE_FIFO) {
1956                         /* emulated UARTs (Lucent Venus 167x) need two steps */
1957                         serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1958                 }
1959                 serial_outp(up, UART_FCR, fcr);         /* set fcr */
1960         }
1961         serial8250_set_mctrl(&up->port, up->port.mctrl);
1962         spin_unlock_irqrestore(&up->port.lock, flags);
1963 }
1964
1965 static void
1966 serial8250_pm(struct uart_port *port, unsigned int state,
1967               unsigned int oldstate)
1968 {
1969         struct uart_8250_port *p = (struct uart_8250_port *)port;
1970
1971         serial8250_set_sleep(p, state != 0);
1972
1973         if (p->pm)
1974                 p->pm(port, state, oldstate);
1975 }
1976
1977 /*
1978  * Resource handling.
1979  */
1980 static int serial8250_request_std_resource(struct uart_8250_port *up)
1981 {
1982         unsigned int size = 8 << up->port.regshift;
1983         int ret = 0;
1984
1985         switch (up->port.iotype) {
1986         case UPIO_AU:
1987                 size = 0x100000;
1988                 /* fall thru */
1989         case UPIO_TSI:
1990         case UPIO_MEM32:
1991         case UPIO_MEM:
1992                 if (!up->port.mapbase)
1993                         break;
1994
1995                 if (!request_mem_region(up->port.mapbase, size, "serial")) {
1996                         ret = -EBUSY;
1997                         break;
1998                 }
1999
2000                 if (up->port.flags & UPF_IOREMAP) {
2001                         up->port.membase = ioremap(up->port.mapbase, size);
2002                         if (!up->port.membase) {
2003                                 release_mem_region(up->port.mapbase, size);
2004                                 ret = -ENOMEM;
2005                         }
2006                 }
2007                 break;
2008
2009         case UPIO_HUB6:
2010         case UPIO_PORT:
2011                 if (!request_region(up->port.iobase, size, "serial"))
2012                         ret = -EBUSY;
2013                 break;
2014         }
2015         return ret;
2016 }
2017
2018 static void serial8250_release_std_resource(struct uart_8250_port *up)
2019 {
2020         unsigned int size = 8 << up->port.regshift;
2021
2022         switch (up->port.iotype) {
2023         case UPIO_AU:
2024                 size = 0x100000;
2025                 /* fall thru */
2026         case UPIO_TSI:
2027         case UPIO_MEM32:
2028         case UPIO_MEM:
2029                 if (!up->port.mapbase)
2030                         break;
2031
2032                 if (up->port.flags & UPF_IOREMAP) {
2033                         iounmap(up->port.membase);
2034                         up->port.membase = NULL;
2035                 }
2036
2037                 release_mem_region(up->port.mapbase, size);
2038                 break;
2039
2040         case UPIO_HUB6:
2041         case UPIO_PORT:
2042                 release_region(up->port.iobase, size);
2043                 break;
2044         }
2045 }
2046
2047 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2048 {
2049         unsigned long start = UART_RSA_BASE << up->port.regshift;
2050         unsigned int size = 8 << up->port.regshift;
2051         int ret = -EINVAL;
2052
2053         switch (up->port.iotype) {
2054         case UPIO_HUB6:
2055         case UPIO_PORT:
2056                 start += up->port.iobase;
2057                 if (request_region(start, size, "serial-rsa"))
2058                         ret = 0;
2059                 else
2060                         ret = -EBUSY;
2061                 break;
2062         }
2063
2064         return ret;
2065 }
2066
2067 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2068 {
2069         unsigned long offset = UART_RSA_BASE << up->port.regshift;
2070         unsigned int size = 8 << up->port.regshift;
2071
2072         switch (up->port.iotype) {
2073         case UPIO_HUB6:
2074         case UPIO_PORT:
2075                 release_region(up->port.iobase + offset, size);
2076                 break;
2077         }
2078 }
2079
2080 static void serial8250_release_port(struct uart_port *port)
2081 {
2082         struct uart_8250_port *up = (struct uart_8250_port *)port;
2083
2084         serial8250_release_std_resource(up);
2085         if (up->port.type == PORT_RSA)
2086                 serial8250_release_rsa_resource(up);
2087 }
2088
2089 static int serial8250_request_port(struct uart_port *port)
2090 {
2091         struct uart_8250_port *up = (struct uart_8250_port *)port;
2092         int ret = 0;
2093
2094         ret = serial8250_request_std_resource(up);
2095         if (ret == 0 && up->port.type == PORT_RSA) {
2096                 ret = serial8250_request_rsa_resource(up);
2097                 if (ret < 0)
2098                         serial8250_release_std_resource(up);
2099         }
2100
2101         return ret;
2102 }
2103
2104 static void serial8250_config_port(struct uart_port *port, int flags)
2105 {
2106         struct uart_8250_port *up = (struct uart_8250_port *)port;
2107         int probeflags = PROBE_ANY;
2108         int ret;
2109
2110         /*
2111          * Find the region that we can probe for.  This in turn
2112          * tells us whether we can probe for the type of port.
2113          */
2114         ret = serial8250_request_std_resource(up);
2115         if (ret < 0)
2116                 return;
2117
2118         ret = serial8250_request_rsa_resource(up);
2119         if (ret < 0)
2120                 probeflags &= ~PROBE_RSA;
2121
2122         if (flags & UART_CONFIG_TYPE)
2123                 autoconfig(up, probeflags);
2124         if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2125                 autoconfig_irq(up);
2126
2127         if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2128                 serial8250_release_rsa_resource(up);
2129         if (up->port.type == PORT_UNKNOWN)
2130                 serial8250_release_std_resource(up);
2131 }
2132
2133 static int
2134 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2135 {
2136         if (ser->irq >= NR_IRQS || ser->irq < 0 ||
2137             ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2138             ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2139             ser->type == PORT_STARTECH)
2140                 return -EINVAL;
2141         return 0;
2142 }
2143
2144 static const char *
2145 serial8250_type(struct uart_port *port)
2146 {
2147         int type = port->type;
2148
2149         if (type >= ARRAY_SIZE(uart_config))
2150                 type = 0;
2151         return uart_config[type].name;
2152 }
2153
2154 static struct uart_ops serial8250_pops = {
2155         .tx_empty       = serial8250_tx_empty,
2156         .set_mctrl      = serial8250_set_mctrl,
2157         .get_mctrl      = serial8250_get_mctrl,
2158         .stop_tx        = serial8250_stop_tx,
2159         .start_tx       = serial8250_start_tx,
2160         .stop_rx        = serial8250_stop_rx,
2161         .enable_ms      = serial8250_enable_ms,
2162         .break_ctl      = serial8250_break_ctl,
2163         .startup        = serial8250_startup,
2164         .shutdown       = serial8250_shutdown,
2165         .set_termios    = serial8250_set_termios,
2166         .pm             = serial8250_pm,
2167         .type           = serial8250_type,
2168         .release_port   = serial8250_release_port,
2169         .request_port   = serial8250_request_port,
2170         .config_port    = serial8250_config_port,
2171         .verify_port    = serial8250_verify_port,
2172 };
2173
2174 static struct uart_8250_port serial8250_ports[UART_NR];
2175
2176 static void __init serial8250_isa_init_ports(void)
2177 {
2178         struct uart_8250_port *up;
2179         static int first = 1;
2180         int i;
2181
2182         if (!first)
2183                 return;
2184         first = 0;
2185
2186         for (i = 0; i < nr_uarts; i++) {
2187                 struct uart_8250_port *up = &serial8250_ports[i];
2188
2189                 up->port.line = i;
2190                 spin_lock_init(&up->port.lock);
2191
2192                 init_timer(&up->timer);
2193                 up->timer.function = serial8250_timeout;
2194
2195                 /*
2196                  * ALPHA_KLUDGE_MCR needs to be killed.
2197                  */
2198                 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2199                 up->mcr_force = ALPHA_KLUDGE_MCR;
2200
2201                 up->port.ops = &serial8250_pops;
2202         }
2203
2204         for (i = 0, up = serial8250_ports;
2205              i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
2206              i++, up++) {
2207                 up->port.iobase   = old_serial_port[i].port;
2208                 up->port.irq      = irq_canonicalize(old_serial_port[i].irq);
2209                 up->port.uartclk  = old_serial_port[i].baud_base * 16;
2210                 up->port.flags    = old_serial_port[i].flags;
2211                 up->port.hub6     = old_serial_port[i].hub6;
2212                 up->port.membase  = old_serial_port[i].iomem_base;
2213                 up->port.iotype   = old_serial_port[i].io_type;
2214                 up->port.regshift = old_serial_port[i].iomem_reg_shift;
2215                 if (share_irqs)
2216                         up->port.flags |= UPF_SHARE_IRQ;
2217         }
2218 }
2219
2220 static void __init
2221 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2222 {
2223         int i;
2224
2225         serial8250_isa_init_ports();
2226
2227         for (i = 0; i < nr_uarts; i++) {
2228                 struct uart_8250_port *up = &serial8250_ports[i];
2229
2230                 up->port.dev = dev;
2231                 uart_add_one_port(drv, &up->port);
2232         }
2233 }
2234
2235 #ifdef CONFIG_SERIAL_8250_CONSOLE
2236
2237 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
2238
2239 /*
2240  *      Wait for transmitter & holding register to empty
2241  */
2242 static inline void wait_for_xmitr(struct uart_8250_port *up, int bits)
2243 {
2244         unsigned int status, tmout = 10000;
2245
2246         /* Wait up to 10ms for the character(s) to be sent. */
2247         do {
2248                 status = serial_in(up, UART_LSR);
2249
2250                 if (status & UART_LSR_BI)
2251                         up->lsr_break_flag = UART_LSR_BI;
2252
2253                 if (--tmout == 0)
2254                         break;
2255                 udelay(1);
2256         } while ((status & bits) != bits);
2257
2258         /* Wait up to 1s for flow control if necessary */
2259         if (up->port.flags & UPF_CONS_FLOW) {
2260                 tmout = 1000000;
2261                 while (!(serial_in(up, UART_MSR) & UART_MSR_CTS) && --tmout) {
2262                         udelay(1);
2263                         touch_nmi_watchdog();
2264                 }
2265         }
2266 }
2267
2268 static void serial8250_console_putchar(struct uart_port *port, int ch)
2269 {
2270         struct uart_8250_port *up = (struct uart_8250_port *)port;
2271
2272         wait_for_xmitr(up, UART_LSR_THRE);
2273         serial_out(up, UART_TX, ch);
2274 }
2275
2276 /*
2277  *      Print a string to the serial port trying not to disturb
2278  *      any possible real use of the port...
2279  *
2280  *      The console_lock must be held when we get here.
2281  */
2282 static void
2283 serial8250_console_write(struct console *co, const char *s, unsigned int count)
2284 {
2285         struct uart_8250_port *up = &serial8250_ports[co->index];
2286         unsigned long flags;
2287         unsigned int ier;
2288         int locked = 1;
2289
2290         touch_nmi_watchdog();
2291
2292         local_irq_save(flags);
2293         if (up->port.sysrq) {
2294                 /* serial8250_handle_port() already took the lock */
2295                 locked = 0;
2296         } else if (oops_in_progress) {
2297                 locked = spin_trylock(&up->port.lock);
2298         } else
2299                 spin_lock(&up->port.lock);
2300
2301         /*
2302          *      First save the IER then disable the interrupts
2303          */
2304         ier = serial_in(up, UART_IER);
2305
2306         if (up->capabilities & UART_CAP_UUE)
2307                 serial_out(up, UART_IER, UART_IER_UUE);
2308         else
2309                 serial_out(up, UART_IER, 0);
2310
2311         uart_console_write(&up->port, s, count, serial8250_console_putchar);
2312
2313         /*
2314          *      Finally, wait for transmitter to become empty
2315          *      and restore the IER
2316          */
2317         wait_for_xmitr(up, BOTH_EMPTY);
2318         serial_out(up, UART_IER, ier);
2319
2320         if (locked)
2321                 spin_unlock(&up->port.lock);
2322         local_irq_restore(flags);
2323 }
2324
2325 static int __init serial8250_console_setup(struct console *co, char *options)
2326 {
2327         struct uart_port *port;
2328         int baud = 9600;
2329         int bits = 8;
2330         int parity = 'n';
2331         int flow = 'n';
2332
2333         /*
2334          * Check whether an invalid uart number has been specified, and
2335          * if so, search for the first available port that does have
2336          * console support.
2337          */
2338         if (co->index >= nr_uarts)
2339                 co->index = 0;
2340         port = &serial8250_ports[co->index].port;
2341         if (!port->iobase && !port->membase)
2342                 return -ENODEV;
2343
2344         if (options)
2345                 uart_parse_options(options, &baud, &parity, &bits, &flow);
2346
2347         return uart_set_options(port, co, baud, parity, bits, flow);
2348 }
2349
2350 static struct uart_driver serial8250_reg;
2351 static struct console serial8250_console = {
2352         .name           = "ttyS",
2353         .write          = serial8250_console_write,
2354         .device         = uart_console_device,
2355         .setup          = serial8250_console_setup,
2356         .flags          = CON_PRINTBUFFER,
2357         .index          = -1,
2358         .data           = &serial8250_reg,
2359 };
2360
2361 static int __init serial8250_console_init(void)
2362 {
2363         serial8250_isa_init_ports();
2364         register_console(&serial8250_console);
2365         return 0;
2366 }
2367 console_initcall(serial8250_console_init);
2368
2369 static int __init find_port(struct uart_port *p)
2370 {
2371         int line;
2372         struct uart_port *port;
2373
2374         for (line = 0; line < nr_uarts; line++) {
2375                 port = &serial8250_ports[line].port;
2376                 if (uart_match_port(p, port))
2377                         return line;
2378         }
2379         return -ENODEV;
2380 }
2381
2382 int __init serial8250_start_console(struct uart_port *port, char *options)
2383 {
2384         int line;
2385
2386         line = find_port(port);
2387         if (line < 0)
2388                 return -ENODEV;
2389
2390         add_preferred_console("ttyS", line, options);
2391         printk("Adding console on ttyS%d at %s 0x%lx (options '%s')\n",
2392                 line, port->iotype == UPIO_MEM ? "MMIO" : "I/O port",
2393                 port->iotype == UPIO_MEM ? (unsigned long) port->mapbase :
2394                     (unsigned long) port->iobase, options);
2395         if (!(serial8250_console.flags & CON_ENABLED)) {
2396                 serial8250_console.flags &= ~CON_PRINTBUFFER;
2397                 register_console(&serial8250_console);
2398         }
2399         return line;
2400 }
2401
2402 #define SERIAL8250_CONSOLE      &serial8250_console
2403 #else
2404 #define SERIAL8250_CONSOLE      NULL
2405 #endif
2406
2407 static struct uart_driver serial8250_reg = {
2408         .owner                  = THIS_MODULE,
2409         .driver_name            = "serial",
2410         .dev_name               = "ttyS",
2411         .major                  = TTY_MAJOR,
2412         .minor                  = 64,
2413         .nr                     = UART_NR,
2414         .cons                   = SERIAL8250_CONSOLE,
2415 };
2416
2417 /*
2418  * early_serial_setup - early registration for 8250 ports
2419  *
2420  * Setup an 8250 port structure prior to console initialisation.  Use
2421  * after console initialisation will cause undefined behaviour.
2422  */
2423 int __init early_serial_setup(struct uart_port *port)
2424 {
2425         if (port->line >= ARRAY_SIZE(serial8250_ports))
2426                 return -ENODEV;
2427
2428         serial8250_isa_init_ports();
2429         serial8250_ports[port->line].port       = *port;
2430         serial8250_ports[port->line].port.ops   = &serial8250_pops;
2431         return 0;
2432 }
2433
2434 /**
2435  *      serial8250_suspend_port - suspend one serial port
2436  *      @line:  serial line number
2437  *
2438  *      Suspend one serial port.
2439  */
2440 void serial8250_suspend_port(int line)
2441 {
2442         uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2443 }
2444
2445 /**
2446  *      serial8250_resume_port - resume one serial port
2447  *      @line:  serial line number
2448  *
2449  *      Resume one serial port.
2450  */
2451 void serial8250_resume_port(int line)
2452 {
2453         uart_resume_port(&serial8250_reg, &serial8250_ports[line].port);
2454 }
2455
2456 /*
2457  * Register a set of serial devices attached to a platform device.  The
2458  * list is terminated with a zero flags entry, which means we expect
2459  * all entries to have at least UPF_BOOT_AUTOCONF set.
2460  */
2461 static int __devinit serial8250_probe(struct platform_device *dev)
2462 {
2463         struct plat_serial8250_port *p = dev->dev.platform_data;
2464         struct uart_port port;
2465         int ret, i;
2466
2467         memset(&port, 0, sizeof(struct uart_port));
2468
2469         for (i = 0; p && p->flags != 0; p++, i++) {
2470                 port.iobase     = p->iobase;
2471                 port.membase    = p->membase;
2472                 port.irq        = p->irq;
2473                 port.uartclk    = p->uartclk;
2474                 port.regshift   = p->regshift;
2475                 port.iotype     = p->iotype;
2476                 port.flags      = p->flags;
2477                 port.mapbase    = p->mapbase;
2478                 port.hub6       = p->hub6;
2479                 port.dev        = &dev->dev;
2480                 if (share_irqs)
2481                         port.flags |= UPF_SHARE_IRQ;
2482                 ret = serial8250_register_port(&port);
2483                 if (ret < 0) {
2484                         dev_err(&dev->dev, "unable to register port at index %d "
2485                                 "(IO%lx MEM%lx IRQ%d): %d\n", i,
2486                                 p->iobase, p->mapbase, p->irq, ret);
2487                 }
2488         }
2489         return 0;
2490 }
2491
2492 /*
2493  * Remove serial ports registered against a platform device.
2494  */
2495 static int __devexit serial8250_remove(struct platform_device *dev)
2496 {
2497         int i;
2498
2499         for (i = 0; i < nr_uarts; i++) {
2500                 struct uart_8250_port *up = &serial8250_ports[i];
2501
2502                 if (up->port.dev == &dev->dev)
2503                         serial8250_unregister_port(i);
2504         }
2505         return 0;
2506 }
2507
2508 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
2509 {
2510         int i;
2511
2512         for (i = 0; i < UART_NR; i++) {
2513                 struct uart_8250_port *up = &serial8250_ports[i];
2514
2515                 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
2516                         uart_suspend_port(&serial8250_reg, &up->port);
2517         }
2518
2519         return 0;
2520 }
2521
2522 static int serial8250_resume(struct platform_device *dev)
2523 {
2524         int i;
2525
2526         for (i = 0; i < UART_NR; i++) {
2527                 struct uart_8250_port *up = &serial8250_ports[i];
2528
2529                 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
2530                         uart_resume_port(&serial8250_reg, &up->port);
2531         }
2532
2533         return 0;
2534 }
2535
2536 static struct platform_driver serial8250_isa_driver = {
2537         .probe          = serial8250_probe,
2538         .remove         = __devexit_p(serial8250_remove),
2539         .suspend        = serial8250_suspend,
2540         .resume         = serial8250_resume,
2541         .driver         = {
2542                 .name   = "serial8250",
2543                 .owner  = THIS_MODULE,
2544         },
2545 };
2546
2547 /*
2548  * This "device" covers _all_ ISA 8250-compatible serial devices listed
2549  * in the table in include/asm/serial.h
2550  */
2551 static struct platform_device *serial8250_isa_devs;
2552
2553 /*
2554  * serial8250_register_port and serial8250_unregister_port allows for
2555  * 16x50 serial ports to be configured at run-time, to support PCMCIA
2556  * modems and PCI multiport cards.
2557  */
2558 static DEFINE_MUTEX(serial_mutex);
2559
2560 static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
2561 {
2562         int i;
2563
2564         /*
2565          * First, find a port entry which matches.
2566          */
2567         for (i = 0; i < nr_uarts; i++)
2568                 if (uart_match_port(&serial8250_ports[i].port, port))
2569                         return &serial8250_ports[i];
2570
2571         /*
2572          * We didn't find a matching entry, so look for the first
2573          * free entry.  We look for one which hasn't been previously
2574          * used (indicated by zero iobase).
2575          */
2576         for (i = 0; i < nr_uarts; i++)
2577                 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
2578                     serial8250_ports[i].port.iobase == 0)
2579                         return &serial8250_ports[i];
2580
2581         /*
2582          * That also failed.  Last resort is to find any entry which
2583          * doesn't have a real port associated with it.
2584          */
2585         for (i = 0; i < nr_uarts; i++)
2586                 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
2587                         return &serial8250_ports[i];
2588
2589         return NULL;
2590 }
2591
2592 /**
2593  *      serial8250_register_port - register a serial port
2594  *      @port: serial port template
2595  *
2596  *      Configure the serial port specified by the request. If the
2597  *      port exists and is in use, it is hung up and unregistered
2598  *      first.
2599  *
2600  *      The port is then probed and if necessary the IRQ is autodetected
2601  *      If this fails an error is returned.
2602  *
2603  *      On success the port is ready to use and the line number is returned.
2604  */
2605 int serial8250_register_port(struct uart_port *port)
2606 {
2607         struct uart_8250_port *uart;
2608         int ret = -ENOSPC;
2609
2610         if (port->uartclk == 0)
2611                 return -EINVAL;
2612
2613         mutex_lock(&serial_mutex);
2614
2615         uart = serial8250_find_match_or_unused(port);
2616         if (uart) {
2617                 uart_remove_one_port(&serial8250_reg, &uart->port);
2618
2619                 uart->port.iobase   = port->iobase;
2620                 uart->port.membase  = port->membase;
2621                 uart->port.irq      = port->irq;
2622                 uart->port.uartclk  = port->uartclk;
2623                 uart->port.fifosize = port->fifosize;
2624                 uart->port.regshift = port->regshift;
2625                 uart->port.iotype   = port->iotype;
2626                 uart->port.flags    = port->flags | UPF_BOOT_AUTOCONF;
2627                 uart->port.mapbase  = port->mapbase;
2628                 if (port->dev)
2629                         uart->port.dev = port->dev;
2630
2631                 ret = uart_add_one_port(&serial8250_reg, &uart->port);
2632                 if (ret == 0)
2633                         ret = uart->port.line;
2634         }
2635         mutex_unlock(&serial_mutex);
2636
2637         return ret;
2638 }
2639 EXPORT_SYMBOL(serial8250_register_port);
2640
2641 /**
2642  *      serial8250_unregister_port - remove a 16x50 serial port at runtime
2643  *      @line: serial line number
2644  *
2645  *      Remove one serial port.  This may not be called from interrupt
2646  *      context.  We hand the port back to the our control.
2647  */
2648 void serial8250_unregister_port(int line)
2649 {
2650         struct uart_8250_port *uart = &serial8250_ports[line];
2651
2652         mutex_lock(&serial_mutex);
2653         uart_remove_one_port(&serial8250_reg, &uart->port);
2654         if (serial8250_isa_devs) {
2655                 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
2656                 uart->port.type = PORT_UNKNOWN;
2657                 uart->port.dev = &serial8250_isa_devs->dev;
2658                 uart_add_one_port(&serial8250_reg, &uart->port);
2659         } else {
2660                 uart->port.dev = NULL;
2661         }
2662         mutex_unlock(&serial_mutex);
2663 }
2664 EXPORT_SYMBOL(serial8250_unregister_port);
2665
2666 static int __init serial8250_init(void)
2667 {
2668         int ret, i;
2669
2670         if (nr_uarts > UART_NR)
2671                 nr_uarts = UART_NR;
2672
2673         printk(KERN_INFO "Serial: 8250/16550 driver $Revision: 1.90 $ "
2674                 "%d ports, IRQ sharing %sabled\n", nr_uarts,
2675                 share_irqs ? "en" : "dis");
2676
2677         for (i = 0; i < NR_IRQS; i++)
2678                 spin_lock_init(&irq_lists[i].lock);
2679
2680         ret = uart_register_driver(&serial8250_reg);
2681         if (ret)
2682                 goto out;
2683
2684         serial8250_isa_devs = platform_device_alloc("serial8250",
2685                                                     PLAT8250_DEV_LEGACY);
2686         if (!serial8250_isa_devs) {
2687                 ret = -ENOMEM;
2688                 goto unreg_uart_drv;
2689         }
2690
2691         ret = platform_device_add(serial8250_isa_devs);
2692         if (ret)
2693                 goto put_dev;
2694
2695         serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
2696
2697         ret = platform_driver_register(&serial8250_isa_driver);
2698         if (ret == 0)
2699                 goto out;
2700
2701         platform_device_del(serial8250_isa_devs);
2702  put_dev:
2703         platform_device_put(serial8250_isa_devs);
2704  unreg_uart_drv:
2705         uart_unregister_driver(&serial8250_reg);
2706  out:
2707         return ret;
2708 }
2709
2710 static void __exit serial8250_exit(void)
2711 {
2712         struct platform_device *isa_dev = serial8250_isa_devs;
2713
2714         /*
2715          * This tells serial8250_unregister_port() not to re-register
2716          * the ports (thereby making serial8250_isa_driver permanently
2717          * in use.)
2718          */
2719         serial8250_isa_devs = NULL;
2720
2721         platform_driver_unregister(&serial8250_isa_driver);
2722         platform_device_unregister(isa_dev);
2723
2724         uart_unregister_driver(&serial8250_reg);
2725 }
2726
2727 module_init(serial8250_init);
2728 module_exit(serial8250_exit);
2729
2730 EXPORT_SYMBOL(serial8250_suspend_port);
2731 EXPORT_SYMBOL(serial8250_resume_port);
2732
2733 MODULE_LICENSE("GPL");
2734 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver $Revision: 1.90 $");
2735
2736 module_param(share_irqs, uint, 0644);
2737 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
2738         " (unsafe)");
2739
2740 module_param(nr_uarts, uint, 0644);
2741 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
2742
2743 #ifdef CONFIG_SERIAL_8250_RSA
2744 module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
2745 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
2746 #endif
2747 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);