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