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