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