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