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