8250.c 83.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 *  Driver for 8250/16550-type serial ports
 *
 *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
 *
 *  Copyright (C) 2001 Russell King.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * A note about mapbase / membase
 *
 *  mapbase is the physical address of the IO port.
 *  membase is an 'ioremapped' cookie.
 */

#if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
#define SUPPORT_SYSRQ
#endif

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/console.h>
#include <linux/sysrq.h>
#include <linux/delay.h>
30
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
31
#include <linux/tty.h>
32
#include <linux/ratelimit.h>
L
Linus Torvalds 已提交
33 34 35 36 37
#include <linux/tty_flip.h>
#include <linux/serial_reg.h>
#include <linux/serial_core.h>
#include <linux/serial.h>
#include <linux/serial_8250.h>
38
#include <linux/nmi.h>
39
#include <linux/mutex.h>
40
#include <linux/slab.h>
41 42 43
#ifdef CONFIG_SPARC
#include <linux/sunserialcore.h>
#endif
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51

#include <asm/io.h>
#include <asm/irq.h>

#include "8250.h"

/*
 * Configuration:
52
 *   share_irqs - whether we pass IRQF_SHARED to request_irq().  This option
L
Linus Torvalds 已提交
53 54
 *                is unsafe when used on edge-triggered interrupts.
 */
A
Adrian Bunk 已提交
55
static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
L
Linus Torvalds 已提交
56

57 58
static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;

59 60 61 62 63 64 65
static struct uart_driver serial8250_reg;

static int serial_index(struct uart_port *port)
{
	return (serial8250_reg.minor - 64) + port->line;
}

66 67
static unsigned int skip_txen_test; /* force skip of txen test at init time */

L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
/*
 * Debugging.
 */
#if 0
#define DEBUG_AUTOCONF(fmt...)	printk(fmt)
#else
#define DEBUG_AUTOCONF(fmt...)	do { } while (0)
#endif

#if 0
#define DEBUG_INTR(fmt...)	printk(fmt)
#else
#define DEBUG_INTR(fmt...)	do { } while (0)
#endif

J
Jiri Slaby 已提交
83
#define PASS_LIMIT	512
L
Linus Torvalds 已提交
84

85 86 87
#define BOTH_EMPTY 	(UART_LSR_TEMT | UART_LSR_THRE)


L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100
#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
#define CONFIG_SERIAL_DETECT_IRQ 1
#endif
#ifdef CONFIG_SERIAL_8250_MANY_PORTS
#define CONFIG_SERIAL_MANY_PORTS 1
#endif

/*
 * HUB6 is always on.  This will be removed once the header
 * files have been cleaned.
 */
#define CONFIG_HUB6 1

101
#include <asm/serial.h>
L
Linus Torvalds 已提交
102 103 104 105 106 107 108 109 110
/*
 * SERIAL_PORT_DFNS tells us about built-in ports that have no
 * standard enumeration mechanism.   Platforms that can find all
 * serial ports via mechanisms like ACPI or PCI need not supply it.
 */
#ifndef SERIAL_PORT_DFNS
#define SERIAL_PORT_DFNS
#endif

111
static const struct old_serial_port old_serial_port[] = {
L
Linus Torvalds 已提交
112 113 114
	SERIAL_PORT_DFNS /* defined in asm/serial.h */
};

115
#define UART_NR	CONFIG_SERIAL_8250_NR_UARTS
L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124

#ifdef CONFIG_SERIAL_8250_RSA

#define PORT_RSA_MAX 4
static unsigned long probe_rsa[PORT_RSA_MAX];
static unsigned int probe_rsa_count;
#endif /* CONFIG_SERIAL_8250_RSA  */

struct irq_info {
A
Alan Cox 已提交
125 126 127
	struct			hlist_node node;
	int			irq;
	spinlock_t		lock;	/* Protects list not the hash */
L
Linus Torvalds 已提交
128 129 130
	struct list_head	*head;
};

A
Alan Cox 已提交
131 132 133
#define NR_IRQ_HASH		32	/* Can be adjusted later */
static struct hlist_head irq_lists[NR_IRQ_HASH];
static DEFINE_MUTEX(hash_mutex);	/* Used to walk the hash */
L
Linus Torvalds 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

/*
 * Here we define the default xmit fifo size used for each type of UART.
 */
static const struct serial8250_config uart_config[] = {
	[PORT_UNKNOWN] = {
		.name		= "unknown",
		.fifo_size	= 1,
		.tx_loadsz	= 1,
	},
	[PORT_8250] = {
		.name		= "8250",
		.fifo_size	= 1,
		.tx_loadsz	= 1,
	},
	[PORT_16450] = {
		.name		= "16450",
		.fifo_size	= 1,
		.tx_loadsz	= 1,
	},
	[PORT_16550] = {
		.name		= "16550",
		.fifo_size	= 1,
		.tx_loadsz	= 1,
	},
	[PORT_16550A] = {
		.name		= "16550A",
		.fifo_size	= 16,
		.tx_loadsz	= 16,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
		.flags		= UART_CAP_FIFO,
	},
	[PORT_CIRRUS] = {
		.name		= "Cirrus",
		.fifo_size	= 1,
		.tx_loadsz	= 1,
	},
	[PORT_16650] = {
		.name		= "ST16650",
		.fifo_size	= 1,
		.tx_loadsz	= 1,
		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
	},
	[PORT_16650V2] = {
		.name		= "ST16650V2",
		.fifo_size	= 32,
		.tx_loadsz	= 16,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
				  UART_FCR_T_TRIG_00,
		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
	},
	[PORT_16750] = {
		.name		= "TI16750",
		.fifo_size	= 64,
		.tx_loadsz	= 64,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
				  UART_FCR7_64BYTE,
		.flags		= UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
	},
	[PORT_STARTECH] = {
		.name		= "Startech",
		.fifo_size	= 1,
		.tx_loadsz	= 1,
	},
	[PORT_16C950] = {
		.name		= "16C950/954",
		.fifo_size	= 128,
		.tx_loadsz	= 128,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
203 204
		/* UART_CAP_EFR breaks billionon CF bluetooth card. */
		.flags		= UART_CAP_FIFO | UART_CAP_SLEEP,
L
Linus Torvalds 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	},
	[PORT_16654] = {
		.name		= "ST16654",
		.fifo_size	= 64,
		.tx_loadsz	= 32,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
				  UART_FCR_T_TRIG_10,
		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
	},
	[PORT_16850] = {
		.name		= "XR16850",
		.fifo_size	= 128,
		.tx_loadsz	= 128,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
	},
	[PORT_RSA] = {
		.name		= "RSA",
		.fifo_size	= 2048,
		.tx_loadsz	= 2048,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
		.flags		= UART_CAP_FIFO,
	},
	[PORT_NS16550A] = {
		.name		= "NS16550A",
		.fifo_size	= 16,
		.tx_loadsz	= 16,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
		.flags		= UART_CAP_FIFO | UART_NATSEMI,
	},
	[PORT_XSCALE] = {
		.name		= "XScale",
		.fifo_size	= 32,
		.tx_loadsz	= 32,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
240
		.flags		= UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
L
Linus Torvalds 已提交
241
	},
T
Thomas Koeller 已提交
242 243 244 245 246
	[PORT_RM9000] = {
		.name		= "RM9000",
		.fifo_size	= 16,
		.tx_loadsz	= 16,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
247 248 249 250 251 252 253
		.flags		= UART_CAP_FIFO,
	},
	[PORT_OCTEON] = {
		.name		= "OCTEON",
		.fifo_size	= 64,
		.tx_loadsz	= 64,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
T
Thomas Koeller 已提交
254 255
		.flags		= UART_CAP_FIFO,
	},
256 257 258 259 260 261 262
	[PORT_AR7] = {
		.name		= "AR7",
		.fifo_size	= 16,
		.tx_loadsz	= 16,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
	},
263 264 265 266 267 268 269
	[PORT_U6_16550A] = {
		.name		= "U6_16550A",
		.fifo_size	= 64,
		.tx_loadsz	= 64,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
	},
270 271 272 273 274 275 276 277
	[PORT_TEGRA] = {
		.name		= "Tegra",
		.fifo_size	= 32,
		.tx_loadsz	= 8,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
				  UART_FCR_T_TRIG_01,
		.flags		= UART_CAP_FIFO | UART_CAP_RTOIE,
	},
278 279 280 281 282 283 284
	[PORT_XR17D15X] = {
		.name		= "XR17D15X",
		.fifo_size	= 64,
		.tx_loadsz	= 64,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
		.flags		= UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR,
	},
L
Linus Torvalds 已提交
285 286
};

287
#if defined(CONFIG_MIPS_ALCHEMY)
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

/* Au1x00 UART hardware has a weird register layout */
static const u8 au_io_in_map[] = {
	[UART_RX]  = 0,
	[UART_IER] = 2,
	[UART_IIR] = 3,
	[UART_LCR] = 5,
	[UART_MCR] = 6,
	[UART_LSR] = 7,
	[UART_MSR] = 8,
};

static const u8 au_io_out_map[] = {
	[UART_TX]  = 1,
	[UART_IER] = 2,
	[UART_FCR] = 4,
	[UART_LCR] = 5,
	[UART_MCR] = 6,
};

/* sane hardware needs no mapping */
309
static inline int map_8250_in_reg(struct uart_port *p, int offset)
310
{
311
	if (p->iotype != UPIO_AU)
312 313 314 315
		return offset;
	return au_io_in_map[offset];
}

316
static inline int map_8250_out_reg(struct uart_port *p, int offset)
317
{
318
	if (p->iotype != UPIO_AU)
319 320 321 322
		return offset;
	return au_io_out_map[offset];
}

A
Alan Cox 已提交
323
#elif defined(CONFIG_SERIAL_8250_RM9K)
T
Thomas Koeller 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

static const u8
	regmap_in[8] = {
		[UART_RX]	= 0x00,
		[UART_IER]	= 0x0c,
		[UART_IIR]	= 0x14,
		[UART_LCR]	= 0x1c,
		[UART_MCR]	= 0x20,
		[UART_LSR]	= 0x24,
		[UART_MSR]	= 0x28,
		[UART_SCR]	= 0x2c
	},
	regmap_out[8] = {
		[UART_TX] 	= 0x04,
		[UART_IER]	= 0x0c,
		[UART_FCR]	= 0x18,
		[UART_LCR]	= 0x1c,
		[UART_MCR]	= 0x20,
		[UART_LSR]	= 0x24,
		[UART_MSR]	= 0x28,
		[UART_SCR]	= 0x2c
	};

347
static inline int map_8250_in_reg(struct uart_port *p, int offset)
T
Thomas Koeller 已提交
348
{
349
	if (p->iotype != UPIO_RM9000)
T
Thomas Koeller 已提交
350 351 352 353
		return offset;
	return regmap_in[offset];
}

354
static inline int map_8250_out_reg(struct uart_port *p, int offset)
T
Thomas Koeller 已提交
355
{
356
	if (p->iotype != UPIO_RM9000)
T
Thomas Koeller 已提交
357 358 359 360
		return offset;
	return regmap_out[offset];
}

361 362 363 364 365 366 367 368
#else

/* sane hardware needs no mapping */
#define map_8250_in_reg(up, offset) (offset)
#define map_8250_out_reg(up, offset) (offset)

#endif

369
static unsigned int hub6_serial_in(struct uart_port *p, int offset)
L
Linus Torvalds 已提交
370
{
371 372 373 374
	offset = map_8250_in_reg(p, offset) << p->regshift;
	outb(p->hub6 - 1 + offset, p->iobase);
	return inb(p->iobase + 1);
}
L
Linus Torvalds 已提交
375

376 377 378 379 380 381
static void hub6_serial_out(struct uart_port *p, int offset, int value)
{
	offset = map_8250_out_reg(p, offset) << p->regshift;
	outb(p->hub6 - 1 + offset, p->iobase);
	outb(value, p->iobase + 1);
}
L
Linus Torvalds 已提交
382

383 384 385 386 387
static unsigned int mem_serial_in(struct uart_port *p, int offset)
{
	offset = map_8250_in_reg(p, offset) << p->regshift;
	return readb(p->membase + offset);
}
L
Linus Torvalds 已提交
388

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
static void mem_serial_out(struct uart_port *p, int offset, int value)
{
	offset = map_8250_out_reg(p, offset) << p->regshift;
	writeb(value, p->membase + offset);
}

static void mem32_serial_out(struct uart_port *p, int offset, int value)
{
	offset = map_8250_out_reg(p, offset) << p->regshift;
	writel(value, p->membase + offset);
}

static unsigned int mem32_serial_in(struct uart_port *p, int offset)
{
	offset = map_8250_in_reg(p, offset) << p->regshift;
	return readl(p->membase + offset);
}
L
Linus Torvalds 已提交
406

407 408 409 410 411 412 413 414 415 416 417
static unsigned int au_serial_in(struct uart_port *p, int offset)
{
	offset = map_8250_in_reg(p, offset) << p->regshift;
	return __raw_readl(p->membase + offset);
}

static void au_serial_out(struct uart_port *p, int offset, int value)
{
	offset = map_8250_out_reg(p, offset) << p->regshift;
	__raw_writel(value, p->membase + offset);
}
418

419 420 421 422 423 424 425 426 427 428 429 430
static unsigned int io_serial_in(struct uart_port *p, int offset)
{
	offset = map_8250_in_reg(p, offset) << p->regshift;
	return inb(p->iobase + offset);
}

static void io_serial_out(struct uart_port *p, int offset, int value)
{
	offset = map_8250_out_reg(p, offset) << p->regshift;
	outb(value, p->iobase + offset);
}

431 432
static int serial8250_default_handle_irq(struct uart_port *port);

433 434
static void set_io_from_upio(struct uart_port *p)
{
435 436
	struct uart_8250_port *up =
		container_of(p, struct uart_8250_port, port);
437
	switch (p->iotype) {
L
Linus Torvalds 已提交
438
	case UPIO_HUB6:
439 440
		p->serial_in = hub6_serial_in;
		p->serial_out = hub6_serial_out;
L
Linus Torvalds 已提交
441 442 443
		break;

	case UPIO_MEM:
444 445
		p->serial_in = mem_serial_in;
		p->serial_out = mem_serial_out;
L
Linus Torvalds 已提交
446 447
		break;

T
Thomas Koeller 已提交
448
	case UPIO_RM9000:
L
Linus Torvalds 已提交
449
	case UPIO_MEM32:
450 451
		p->serial_in = mem32_serial_in;
		p->serial_out = mem32_serial_out;
L
Linus Torvalds 已提交
452 453
		break;

454
	case UPIO_AU:
455 456
		p->serial_in = au_serial_in;
		p->serial_out = au_serial_out;
457
		break;
458

L
Linus Torvalds 已提交
459
	default:
460 461 462
		p->serial_in = io_serial_in;
		p->serial_out = io_serial_out;
		break;
L
Linus Torvalds 已提交
463
	}
A
Alan Cox 已提交
464 465
	/* Remember loaded iotype */
	up->cur_iotype = p->iotype;
466
	p->handle_irq = serial8250_default_handle_irq;
L
Linus Torvalds 已提交
467 468
}

A
Alex Williamson 已提交
469 470 471
static void
serial_out_sync(struct uart_8250_port *up, int offset, int value)
{
472 473
	struct uart_port *p = &up->port;
	switch (p->iotype) {
A
Alex Williamson 已提交
474 475 476
	case UPIO_MEM:
	case UPIO_MEM32:
	case UPIO_AU:
477 478
		p->serial_out(p, offset, value);
		p->serial_in(p, UART_LCR);	/* safe, no side-effects */
A
Alex Williamson 已提交
479 480
		break;
	default:
481
		p->serial_out(p, offset, value);
A
Alex Williamson 已提交
482 483 484
	}
}

485 486 487 488
#define serial_in(up, offset)		\
	(up->port.serial_in(&(up)->port, (offset)))
#define serial_out(up, offset, value)	\
	(up->port.serial_out(&(up)->port, (offset), (value)))
L
Linus Torvalds 已提交
489 490 491 492 493 494 495 496 497
/*
 * We used to support using pause I/O for certain machines.  We
 * haven't supported this for a while, but just in case it's badly
 * needed for certain old 386 machines, I've left these #define's
 * in....
 */
#define serial_inp(up, offset)		serial_in(up, offset)
#define serial_outp(up, offset, value)	serial_out(up, offset, value)

498 499 500 501 502 503 504 505 506 507 508 509 510
/* Uart divisor latch read */
static inline int _serial_dl_read(struct uart_8250_port *up)
{
	return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
}

/* Uart divisor latch write */
static inline void _serial_dl_write(struct uart_8250_port *up, int value)
{
	serial_outp(up, UART_DLL, value & 0xff);
	serial_outp(up, UART_DLM, value >> 8 & 0xff);
}

511
#if defined(CONFIG_MIPS_ALCHEMY)
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
/* Au1x00 haven't got a standard divisor latch */
static int serial_dl_read(struct uart_8250_port *up)
{
	if (up->port.iotype == UPIO_AU)
		return __raw_readl(up->port.membase + 0x28);
	else
		return _serial_dl_read(up);
}

static void serial_dl_write(struct uart_8250_port *up, int value)
{
	if (up->port.iotype == UPIO_AU)
		__raw_writel(value, up->port.membase + 0x28);
	else
		_serial_dl_write(up, value);
}
A
Alan Cox 已提交
528
#elif defined(CONFIG_SERIAL_8250_RM9K)
T
Thomas Koeller 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
static int serial_dl_read(struct uart_8250_port *up)
{
	return	(up->port.iotype == UPIO_RM9000) ?
		(((__raw_readl(up->port.membase + 0x10) << 8) |
		(__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
		_serial_dl_read(up);
}

static void serial_dl_write(struct uart_8250_port *up, int value)
{
	if (up->port.iotype == UPIO_RM9000) {
		__raw_writel(value, up->port.membase + 0x08);
		__raw_writel(value >> 8, up->port.membase + 0x10);
	} else {
		_serial_dl_write(up, value);
	}
}
546 547 548 549
#else
#define serial_dl_read(up) _serial_dl_read(up)
#define serial_dl_write(up, value) _serial_dl_write(up, value)
#endif
L
Linus Torvalds 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

/*
 * For the 16C950
 */
static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
{
	serial_out(up, UART_SCR, offset);
	serial_out(up, UART_ICR, value);
}

static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
{
	unsigned int value;

	serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
	serial_out(up, UART_SCR, offset);
	value = serial_in(up, UART_ICR);
	serial_icr_write(up, UART_ACR, up->acr);

	return value;
}

/*
 * FIFO support.
 */
575
static void serial8250_clear_fifos(struct uart_8250_port *p)
L
Linus Torvalds 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589
{
	if (p->capabilities & UART_CAP_FIFO) {
		serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
		serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
			       UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
		serial_outp(p, UART_FCR, 0);
	}
}

/*
 * IER sleep support.  UARTs which have EFRs need the "extended
 * capability" bit enabled.  Note that on XR16C850s, we need to
 * reset LCR to write to IER.
 */
590
static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
L
Linus Torvalds 已提交
591 592 593
{
	if (p->capabilities & UART_CAP_SLEEP) {
		if (p->capabilities & UART_CAP_EFR) {
594
			serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_B);
L
Linus Torvalds 已提交
595 596 597 598 599
			serial_outp(p, UART_EFR, UART_EFR_ECB);
			serial_outp(p, UART_LCR, 0);
		}
		serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
		if (p->capabilities & UART_CAP_EFR) {
600
			serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_B);
L
Linus Torvalds 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
			serial_outp(p, UART_EFR, 0);
			serial_outp(p, UART_LCR, 0);
		}
	}
}

#ifdef CONFIG_SERIAL_8250_RSA
/*
 * Attempts to turn on the RSA FIFO.  Returns zero on failure.
 * We set the port uart clock rate if we succeed.
 */
static int __enable_rsa(struct uart_8250_port *up)
{
	unsigned char mode;
	int result;

	mode = serial_inp(up, UART_RSA_MSR);
	result = mode & UART_RSA_MSR_FIFO;

	if (!result) {
		serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
		mode = serial_inp(up, UART_RSA_MSR);
		result = mode & UART_RSA_MSR_FIFO;
	}

	if (result)
		up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;

	return result;
}

static void enable_rsa(struct uart_8250_port *up)
{
	if (up->port.type == PORT_RSA) {
		if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
			spin_lock_irq(&up->port.lock);
			__enable_rsa(up);
			spin_unlock_irq(&up->port.lock);
		}
		if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
			serial_outp(up, UART_RSA_FRR, 0);
	}
}

/*
 * Attempts to turn off the RSA FIFO.  Returns zero on failure.
 * It is unknown why interrupts were disabled in here.  However,
 * the caller is expected to preserve this behaviour by grabbing
 * the spinlock before calling this function.
 */
static void disable_rsa(struct uart_8250_port *up)
{
	unsigned char mode;
	int result;

	if (up->port.type == PORT_RSA &&
	    up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
		spin_lock_irq(&up->port.lock);

		mode = serial_inp(up, UART_RSA_MSR);
		result = !(mode & UART_RSA_MSR_FIFO);

		if (!result) {
			serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
			mode = serial_inp(up, UART_RSA_MSR);
			result = !(mode & UART_RSA_MSR_FIFO);
		}

		if (result)
			up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
		spin_unlock_irq(&up->port.lock);
	}
}
#endif /* CONFIG_SERIAL_8250_RSA */

/*
 * This is a quickie test to see how big the FIFO is.
 * It doesn't work at all the time, more's the pity.
 */
static int size_fifo(struct uart_8250_port *up)
{
682 683
	unsigned char old_fcr, old_mcr, old_lcr;
	unsigned short old_dl;
L
Linus Torvalds 已提交
684 685 686 687 688 689 690 691 692
	int count;

	old_lcr = serial_inp(up, UART_LCR);
	serial_outp(up, UART_LCR, 0);
	old_fcr = serial_inp(up, UART_FCR);
	old_mcr = serial_inp(up, UART_MCR);
	serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
		    UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
	serial_outp(up, UART_MCR, UART_MCR_LOOP);
693
	serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
694 695
	old_dl = serial_dl_read(up);
	serial_dl_write(up, 0x0001);
L
Linus Torvalds 已提交
696 697 698 699 700 701 702 703 704
	serial_outp(up, UART_LCR, 0x03);
	for (count = 0; count < 256; count++)
		serial_outp(up, UART_TX, count);
	mdelay(20);/* FIXME - schedule_timeout */
	for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
	     (count < 256); count++)
		serial_inp(up, UART_RX);
	serial_outp(up, UART_FCR, old_fcr);
	serial_outp(up, UART_MCR, old_mcr);
705
	serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
706
	serial_dl_write(up, old_dl);
L
Linus Torvalds 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
	serial_outp(up, UART_LCR, old_lcr);

	return count;
}

/*
 * Read UART ID using the divisor method - set DLL and DLM to zero
 * and the revision will be in DLL and device type in DLM.  We
 * preserve the device state across this.
 */
static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
{
	unsigned char old_dll, old_dlm, old_lcr;
	unsigned int id;

	old_lcr = serial_inp(p, UART_LCR);
723
	serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_A);
L
Linus Torvalds 已提交
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

	old_dll = serial_inp(p, UART_DLL);
	old_dlm = serial_inp(p, UART_DLM);

	serial_outp(p, UART_DLL, 0);
	serial_outp(p, UART_DLM, 0);

	id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;

	serial_outp(p, UART_DLL, old_dll);
	serial_outp(p, UART_DLM, old_dlm);
	serial_outp(p, UART_LCR, old_lcr);

	return id;
}

/*
 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
 * When this function is called we know it is at least a StarTech
 * 16650 V2, but it might be one of several StarTech UARTs, or one of
 * its clones.  (We treat the broken original StarTech 16650 V1 as a
 * 16550, and why not?  Startech doesn't seem to even acknowledge its
 * existence.)
T
Thomas Koeller 已提交
747
 *
L
Linus Torvalds 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
 * What evil have men's minds wrought...
 */
static void autoconfig_has_efr(struct uart_8250_port *up)
{
	unsigned int id1, id2, id3, rev;

	/*
	 * Everything with an EFR has SLEEP
	 */
	up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;

	/*
	 * First we check to see if it's an Oxford Semiconductor UART.
	 *
	 * If we have to do this here because some non-National
	 * Semiconductor clone chips lock up if you try writing to the
	 * LSR register (which serial_icr_read does)
	 */

	/*
	 * Check for Oxford Semiconductor 16C950.
	 *
	 * EFR [4] must be set else this test fails.
	 *
	 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
	 * claims that it's needed for 952 dual UART's (which are not
	 * recommended for new designs).
	 */
	up->acr = 0;
777
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
L
Linus Torvalds 已提交
778 779 780 781 782 783 784 785 786 787 788 789
	serial_out(up, UART_EFR, UART_EFR_ECB);
	serial_out(up, UART_LCR, 0x00);
	id1 = serial_icr_read(up, UART_ID1);
	id2 = serial_icr_read(up, UART_ID2);
	id3 = serial_icr_read(up, UART_ID3);
	rev = serial_icr_read(up, UART_REV);

	DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);

	if (id1 == 0x16 && id2 == 0xC9 &&
	    (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
		up->port.type = PORT_16C950;
790 791 792 793 794 795 796 797

		/*
		 * Enable work around for the Oxford Semiconductor 952 rev B
		 * chip which causes it to seriously miscalculate baud rates
		 * when DLL is 0.
		 */
		if (id3 == 0x52 && rev == 0x01)
			up->bugs |= UART_BUG_QUOT;
L
Linus Torvalds 已提交
798 799
		return;
	}
T
Thomas Koeller 已提交
800

L
Linus Torvalds 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
	/*
	 * We check for a XR16C850 by setting DLL and DLM to 0, and then
	 * reading back DLL and DLM.  The chip type depends on the DLM
	 * value read back:
	 *  0x10 - XR16C850 and the DLL contains the chip revision.
	 *  0x12 - XR16C2850.
	 *  0x14 - XR16C854.
	 */
	id1 = autoconfig_read_divisor_id(up);
	DEBUG_AUTOCONF("850id=%04x ", id1);

	id2 = id1 >> 8;
	if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
		up->port.type = PORT_16850;
		return;
	}

	/*
	 * It wasn't an XR16C850.
	 *
	 * We distinguish between the '654 and the '650 by counting
	 * how many bytes are in the FIFO.  I'm using this for now,
	 * since that's the technique that was sent to me in the
	 * serial driver update, but I'm not convinced this works.
	 * I've had problems doing this in the past.  -TYT
	 */
	if (size_fifo(up) == 64)
		up->port.type = PORT_16654;
	else
		up->port.type = PORT_16650V2;
}

/*
 * We detected a chip without a FIFO.  Only two fall into
 * this category - the original 8250 and the 16450.  The
 * 16450 has a scratch register (accessible with LCR=0)
 */
static void autoconfig_8250(struct uart_8250_port *up)
{
	unsigned char scratch, status1, status2;

	up->port.type = PORT_8250;

	scratch = serial_in(up, UART_SCR);
	serial_outp(up, UART_SCR, 0xa5);
	status1 = serial_in(up, UART_SCR);
	serial_outp(up, UART_SCR, 0x5a);
	status2 = serial_in(up, UART_SCR);
	serial_outp(up, UART_SCR, scratch);

	if (status1 == 0xa5 && status2 == 0x5a)
		up->port.type = PORT_16450;
}

static int broken_efr(struct uart_8250_port *up)
{
	/*
	 * Exar ST16C2550 "A2" devices incorrectly detect as
	 * having an EFR, and report an ID of 0x0201.  See
860
	 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html 
L
Linus Torvalds 已提交
861 862 863 864 865 866 867
	 */
	if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
		return 1;

	return 0;
}

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
{
	unsigned char status;

	status = serial_in(up, 0x04); /* EXCR2 */
#define PRESL(x) ((x) & 0x30)
	if (PRESL(status) == 0x10) {
		/* already in high speed mode */
		return 0;
	} else {
		status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
		status |= 0x10;  /* 1.625 divisor for baud_base --> 921600 */
		serial_outp(up, 0x04, status);
	}
	return 1;
}

L
Linus Torvalds 已提交
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
/*
 * We know that the chip has FIFOs.  Does it have an EFR?  The
 * EFR is located in the same register position as the IIR and
 * we know the top two bits of the IIR are currently set.  The
 * EFR should contain zero.  Try to read the EFR.
 */
static void autoconfig_16550a(struct uart_8250_port *up)
{
	unsigned char status1, status2;
	unsigned int iersave;

	up->port.type = PORT_16550A;
	up->capabilities |= UART_CAP_FIFO;

	/*
	 * Check for presence of the EFR when DLAB is set.
	 * Only ST16C650V1 UARTs pass this test.
	 */
903
	serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
L
Linus Torvalds 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
	if (serial_in(up, UART_EFR) == 0) {
		serial_outp(up, UART_EFR, 0xA8);
		if (serial_in(up, UART_EFR) != 0) {
			DEBUG_AUTOCONF("EFRv1 ");
			up->port.type = PORT_16650;
			up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
		} else {
			DEBUG_AUTOCONF("Motorola 8xxx DUART ");
		}
		serial_outp(up, UART_EFR, 0);
		return;
	}

	/*
	 * Maybe it requires 0xbf to be written to the LCR.
	 * (other ST16C650V2 UARTs, TI16C752A, etc)
	 */
921
	serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
L
Linus Torvalds 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
	if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
		DEBUG_AUTOCONF("EFRv2 ");
		autoconfig_has_efr(up);
		return;
	}

	/*
	 * Check for a National Semiconductor SuperIO chip.
	 * Attempt to switch to bank 2, read the value of the LOOP bit
	 * from EXCR1. Switch back to bank 0, change it in MCR. Then
	 * switch back to bank 2, read it from EXCR1 again and check
	 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
	 */
	serial_outp(up, UART_LCR, 0);
	status1 = serial_in(up, UART_MCR);
	serial_outp(up, UART_LCR, 0xE0);
	status2 = serial_in(up, 0x02); /* EXCR1 */

	if (!((status2 ^ status1) & UART_MCR_LOOP)) {
		serial_outp(up, UART_LCR, 0);
		serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
		serial_outp(up, UART_LCR, 0xE0);
		status2 = serial_in(up, 0x02); /* EXCR1 */
		serial_outp(up, UART_LCR, 0);
		serial_outp(up, UART_MCR, status1);

		if ((status2 ^ status1) & UART_MCR_LOOP) {
949 950
			unsigned short quot;

L
Linus Torvalds 已提交
951
			serial_outp(up, UART_LCR, 0xE0);
952

953
			quot = serial_dl_read(up);
954 955
			quot <<= 3;

956 957
			if (ns16550a_goto_highspeed(up))
				serial_dl_write(up, quot);
958

L
Linus Torvalds 已提交
959 960
			serial_outp(up, UART_LCR, 0);

961
			up->port.uartclk = 921600*16;
L
Linus Torvalds 已提交
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
			up->port.type = PORT_NS16550A;
			up->capabilities |= UART_NATSEMI;
			return;
		}
	}

	/*
	 * No EFR.  Try to detect a TI16750, which only sets bit 5 of
	 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
	 * Try setting it with and without DLAB set.  Cheap clones
	 * set bit 5 without DLAB set.
	 */
	serial_outp(up, UART_LCR, 0);
	serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
	status1 = serial_in(up, UART_IIR) >> 5;
	serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
978
	serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
L
Linus Torvalds 已提交
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
	serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
	status2 = serial_in(up, UART_IIR) >> 5;
	serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
	serial_outp(up, UART_LCR, 0);

	DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);

	if (status1 == 6 && status2 == 7) {
		up->port.type = PORT_16750;
		up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
		return;
	}

	/*
	 * Try writing and reading the UART_IER_UUE bit (b6).
	 * If it works, this is probably one of the Xscale platform's
	 * internal UARTs.
	 * We're going to explicitly set the UUE bit to 0 before
	 * trying to write and read a 1 just to make sure it's not
	 * already a 1 and maybe locked there before we even start start.
	 */
	iersave = serial_in(up, UART_IER);
	serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
	if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
		/*
		 * OK it's in a known zero state, try writing and reading
		 * without disturbing the current state of the other bits.
		 */
		serial_outp(up, UART_IER, iersave | UART_IER_UUE);
		if (serial_in(up, UART_IER) & UART_IER_UUE) {
			/*
			 * It's an Xscale.
			 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
			 */
			DEBUG_AUTOCONF("Xscale ");
			up->port.type = PORT_XSCALE;
1015
			up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
L
Linus Torvalds 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
			return;
		}
	} else {
		/*
		 * If we got here we couldn't force the IER_UUE bit to 0.
		 * Log it and continue.
		 */
		DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
	}
	serial_outp(up, UART_IER, iersave);
1026

1027 1028 1029 1030 1031 1032 1033 1034
	/*
	 * Exar uarts have EFR in a weird location
	 */
	if (up->port.flags & UPF_EXAR_EFR) {
		up->port.type = PORT_XR17D15X;
		up->capabilities |= UART_CAP_AFE | UART_CAP_EFR;
	}

1035 1036 1037 1038 1039 1040 1041 1042
	/*
	 * We distinguish between 16550A and U6 16550A by counting
	 * how many bytes are in the FIFO.
	 */
	if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
		up->port.type = PORT_U6_16550A;
		up->capabilities |= UART_CAP_AFE;
	}
L
Linus Torvalds 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
}

/*
 * This routine is called by rs_init() to initialize a specific serial
 * port.  It determines what type of UART chip this serial port is
 * using: 8250, 16450, 16550, 16550A.  The important question is
 * whether or not this UART is a 16550A or not, since this will
 * determine whether or not we can use its FIFO features or not.
 */
static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
{
	unsigned char status1, scratch, scratch2, scratch3;
	unsigned char save_lcr, save_mcr;
	unsigned long flags;

	if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
		return;

1061
	DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
1062
		       serial_index(&up->port), up->port.iobase, up->port.membase);
L
Linus Torvalds 已提交
1063 1064 1065 1066 1067 1068 1069 1070

	/*
	 * We really do need global IRQs disabled here - we're going to
	 * be frobbing the chips IRQ enable register to see if it exists.
	 */
	spin_lock_irqsave(&up->port.lock, flags);

	up->capabilities = 0;
1071
	up->bugs = 0;
L
Linus Torvalds 已提交
1072 1073 1074 1075 1076

	if (!(up->port.flags & UPF_BUGGY_UART)) {
		/*
		 * Do a simple existence test first; if we fail this,
		 * there's no point trying anything else.
T
Thomas Koeller 已提交
1077
		 *
L
Linus Torvalds 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
		 * 0x80 is used as a nonsense port to prevent against
		 * false positives due to ISA bus float.  The
		 * assumption is that 0x80 is a non-existent port;
		 * which should be safe since include/asm/io.h also
		 * makes this assumption.
		 *
		 * Note: this is safe as long as MCR bit 4 is clear
		 * and the device is in "PC" mode.
		 */
		scratch = serial_inp(up, UART_IER);
		serial_outp(up, UART_IER, 0);
#ifdef __i386__
		outb(0xff, 0x080);
#endif
1092 1093 1094 1095 1096
		/*
		 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
		 * 16C754B) allow only to modify them if an EFR bit is set.
		 */
		scratch2 = serial_inp(up, UART_IER) & 0x0f;
L
Linus Torvalds 已提交
1097 1098 1099 1100
		serial_outp(up, UART_IER, 0x0F);
#ifdef __i386__
		outb(0, 0x080);
#endif
1101
		scratch3 = serial_inp(up, UART_IER) & 0x0f;
L
Linus Torvalds 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
		serial_outp(up, UART_IER, scratch);
		if (scratch2 != 0 || scratch3 != 0x0F) {
			/*
			 * We failed; there's nothing here
			 */
			DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
				       scratch2, scratch3);
			goto out;
		}
	}

	save_mcr = serial_in(up, UART_MCR);
	save_lcr = serial_in(up, UART_LCR);

T
Thomas Koeller 已提交
1116
	/*
L
Linus Torvalds 已提交
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
	 * Check to see if a UART is really there.  Certain broken
	 * internal modems based on the Rockwell chipset fail this
	 * test, because they apparently don't implement the loopback
	 * test mode.  So this test is skipped on the COM 1 through
	 * COM 4 ports.  This *should* be safe, since no board
	 * manufacturer would be stupid enough to design a board
	 * that conflicts with COM 1-4 --- we hope!
	 */
	if (!(up->port.flags & UPF_SKIP_TEST)) {
		serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
		status1 = serial_inp(up, UART_MSR) & 0xF0;
		serial_outp(up, UART_MCR, save_mcr);
		if (status1 != 0x90) {
			DEBUG_AUTOCONF("LOOP test failed (%02x) ",
				       status1);
			goto out;
		}
	}

	/*
	 * We're pretty sure there's a port here.  Lets find out what
	 * type of port it is.  The IIR top two bits allows us to find
R
Russell King 已提交
1139
	 * out if it's 8250 or 16450, 16550, 16550A or later.  This
L
Linus Torvalds 已提交
1140 1141 1142 1143 1144
	 * determines what we test for next.
	 *
	 * We also initialise the EFR (if any) to zero for later.  The
	 * EFR occupies the same register location as the FCR and IIR.
	 */
1145
	serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
L
Linus Torvalds 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
	serial_outp(up, UART_EFR, 0);
	serial_outp(up, UART_LCR, 0);

	serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
	scratch = serial_in(up, UART_IIR) >> 6;

	DEBUG_AUTOCONF("iir=%d ", scratch);

	switch (scratch) {
	case 0:
		autoconfig_8250(up);
		break;
	case 1:
		up->port.type = PORT_UNKNOWN;
		break;
	case 2:
		up->port.type = PORT_16550;
		break;
	case 3:
		autoconfig_16550a(up);
		break;
	}

#ifdef CONFIG_SERIAL_8250_RSA
	/*
	 * Only probe for RSA ports if we got the region.
	 */
	if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
		int i;

		for (i = 0 ; i < probe_rsa_count; ++i) {
			if (probe_rsa[i] == up->port.iobase &&
			    __enable_rsa(up)) {
				up->port.type = PORT_RSA;
				break;
			}
		}
	}
#endif
1185

L
Linus Torvalds 已提交
1186 1187 1188 1189 1190
	serial_outp(up, UART_LCR, save_lcr);

	if (up->capabilities != uart_config[up->port.type].flags) {
		printk(KERN_WARNING
		       "ttyS%d: detected caps %08x should be %08x\n",
1191 1192
		       serial_index(&up->port), up->capabilities,
		       uart_config[up->port.type].flags);
L
Linus Torvalds 已提交
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
	}

	up->port.fifosize = uart_config[up->port.type].fifo_size;
	up->capabilities = uart_config[up->port.type].flags;
	up->tx_loadsz = uart_config[up->port.type].tx_loadsz;

	if (up->port.type == PORT_UNKNOWN)
		goto out;

	/*
	 * Reset the UART.
	 */
#ifdef CONFIG_SERIAL_8250_RSA
	if (up->port.type == PORT_RSA)
		serial_outp(up, UART_RSA_FRR, 0);
#endif
	serial_outp(up, UART_MCR, save_mcr);
	serial8250_clear_fifos(up);
A
Alex Williamson 已提交
1211
	serial_in(up, UART_RX);
1212 1213 1214 1215
	if (up->capabilities & UART_CAP_UUE)
		serial_outp(up, UART_IER, UART_IER_UUE);
	else
		serial_outp(up, UART_IER, 0);
L
Linus Torvalds 已提交
1216

T
Thomas Koeller 已提交
1217
 out:
L
Linus Torvalds 已提交
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	spin_unlock_irqrestore(&up->port.lock, flags);
	DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
}

static void autoconfig_irq(struct uart_8250_port *up)
{
	unsigned char save_mcr, save_ier;
	unsigned char save_ICP = 0;
	unsigned int ICP = 0;
	unsigned long irqs;
	int irq;

	if (up->port.flags & UPF_FOURPORT) {
		ICP = (up->port.iobase & 0xfe0) | 0x1f;
		save_ICP = inb_p(ICP);
		outb_p(0x80, ICP);
		(void) inb_p(ICP);
	}

	/* forget possible initially masked and pending IRQ */
	probe_irq_off(probe_irq_on());
	save_mcr = serial_inp(up, UART_MCR);
	save_ier = serial_inp(up, UART_IER);
	serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
T
Thomas Koeller 已提交
1242

L
Linus Torvalds 已提交
1243 1244
	irqs = probe_irq_on();
	serial_outp(up, UART_MCR, 0);
A
Alan Cox 已提交
1245 1246
	udelay(10);
	if (up->port.flags & UPF_FOURPORT) {
L
Linus Torvalds 已提交
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
		serial_outp(up, UART_MCR,
			    UART_MCR_DTR | UART_MCR_RTS);
	} else {
		serial_outp(up, UART_MCR,
			    UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
	}
	serial_outp(up, UART_IER, 0x0f);	/* enable all intrs */
	(void)serial_inp(up, UART_LSR);
	(void)serial_inp(up, UART_RX);
	(void)serial_inp(up, UART_IIR);
	(void)serial_inp(up, UART_MSR);
	serial_outp(up, UART_TX, 0xFF);
A
Alan Cox 已提交
1259
	udelay(20);
L
Linus Torvalds 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
	irq = probe_irq_off(irqs);

	serial_outp(up, UART_MCR, save_mcr);
	serial_outp(up, UART_IER, save_ier);

	if (up->port.flags & UPF_FOURPORT)
		outb_p(save_ICP, ICP);

	up->port.irq = (irq > 0) ? irq : 0;
}

1271 1272 1273 1274 1275 1276 1277 1278
static inline void __stop_tx(struct uart_8250_port *p)
{
	if (p->ier & UART_IER_THRI) {
		p->ier &= ~UART_IER_THRI;
		serial_out(p, UART_IER, p->ier);
	}
}

1279
static void serial8250_stop_tx(struct uart_port *port)
L
Linus Torvalds 已提交
1280
{
1281 1282
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
1283

1284
	__stop_tx(up);
L
Linus Torvalds 已提交
1285 1286

	/*
1287
	 * We really want to stop the transmitter from sending.
L
Linus Torvalds 已提交
1288
	 */
1289
	if (up->port.type == PORT_16C950) {
L
Linus Torvalds 已提交
1290 1291 1292 1293 1294
		up->acr |= UART_ACR_TXDIS;
		serial_icr_write(up, UART_ACR, up->acr);
	}
}

1295
static void serial8250_start_tx(struct uart_port *port)
L
Linus Torvalds 已提交
1296
{
1297 1298
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
1299 1300 1301 1302

	if (!(up->ier & UART_IER_THRI)) {
		up->ier |= UART_IER_THRI;
		serial_out(up, UART_IER, up->ier);
1303

1304
		if (up->bugs & UART_BUG_TXEN) {
1305
			unsigned char lsr;
1306
			lsr = serial_in(up, UART_LSR);
1307
			up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
T
Thomas Koeller 已提交
1308
			if ((up->port.type == PORT_RM9000) ?
1309 1310
				(lsr & UART_LSR_THRE) :
				(lsr & UART_LSR_TEMT))
1311
				serial8250_tx_chars(up);
1312
		}
L
Linus Torvalds 已提交
1313
	}
1314

L
Linus Torvalds 已提交
1315
	/*
1316
	 * Re-enable the transmitter if we disabled it.
L
Linus Torvalds 已提交
1317
	 */
1318
	if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
L
Linus Torvalds 已提交
1319 1320 1321 1322 1323 1324 1325
		up->acr &= ~UART_ACR_TXDIS;
		serial_icr_write(up, UART_ACR, up->acr);
	}
}

static void serial8250_stop_rx(struct uart_port *port)
{
1326 1327
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
1328 1329 1330 1331 1332 1333 1334 1335

	up->ier &= ~UART_IER_RLSI;
	up->port.read_status_mask &= ~UART_LSR_DR;
	serial_out(up, UART_IER, up->ier);
}

static void serial8250_enable_ms(struct uart_port *port)
{
1336 1337
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
1338

1339 1340 1341 1342
	/* no MSR capabilities */
	if (up->bugs & UART_BUG_NOMSR)
		return;

L
Linus Torvalds 已提交
1343 1344 1345 1346
	up->ier |= UART_IER_MSI;
	serial_out(up, UART_IER, up->ier);
}

1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
/*
 * Clear the Tegra rx fifo after a break
 *
 * FIXME: This needs to become a port specific callback once we have a
 * framework for this
 */
static void clear_rx_fifo(struct uart_8250_port *up)
{
	unsigned int status, tmout = 10000;
	do {
		status = serial_in(up, UART_LSR);
		if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
			status = serial_in(up, UART_RX);
		else
			break;
		if (--tmout == 0)
			break;
		udelay(1);
	} while (1);
}

1368
/*
1369
 * serial8250_rx_chars: processes according to the passed in LSR
1370 1371 1372
 * value, and returns the remaining LSR bits not handled
 * by this Rx routine.
 */
1373 1374
unsigned char
serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
L
Linus Torvalds 已提交
1375
{
A
Alan Cox 已提交
1376
	struct tty_struct *tty = up->port.state->port.tty;
1377
	unsigned char ch;
L
Linus Torvalds 已提交
1378 1379 1380 1381
	int max_count = 256;
	char flag;

	do {
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
		if (likely(lsr & UART_LSR_DR))
			ch = serial_inp(up, UART_RX);
		else
			/*
			 * Intel 82571 has a Serial Over Lan device that will
			 * set UART_LSR_BI without setting UART_LSR_DR when
			 * it receives a break. To avoid reading from the
			 * receive buffer without UART_LSR_DR bit set, we
			 * just force the read character to be 0
			 */
			ch = 0;

L
Linus Torvalds 已提交
1394 1395 1396
		flag = TTY_NORMAL;
		up->port.icount.rx++;

1397 1398
		lsr |= up->lsr_saved_flags;
		up->lsr_saved_flags = 0;
L
Linus Torvalds 已提交
1399

1400
		if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
L
Linus Torvalds 已提交
1401 1402 1403 1404 1405 1406
			/*
			 * For statistics only
			 */
			if (lsr & UART_LSR_BI) {
				lsr &= ~(UART_LSR_FE | UART_LSR_PE);
				up->port.icount.brk++;
1407 1408 1409 1410 1411 1412 1413
				/*
				 * If tegra port then clear the rx fifo to
				 * accept another break/character.
				 */
				if (up->port.type == PORT_TEGRA)
					clear_rx_fifo(up);

L
Linus Torvalds 已提交
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
				/*
				 * We do the SysRQ and SAK checking
				 * here because otherwise the break
				 * may get masked by ignore_status_mask
				 * or read_status_mask.
				 */
				if (uart_handle_break(&up->port))
					goto ignore_char;
			} else if (lsr & UART_LSR_PE)
				up->port.icount.parity++;
			else if (lsr & UART_LSR_FE)
				up->port.icount.frame++;
			if (lsr & UART_LSR_OE)
				up->port.icount.overrun++;

			/*
1430
			 * Mask off conditions which should be ignored.
L
Linus Torvalds 已提交
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
			 */
			lsr &= up->port.read_status_mask;

			if (lsr & UART_LSR_BI) {
				DEBUG_INTR("handling break....");
				flag = TTY_BREAK;
			} else if (lsr & UART_LSR_PE)
				flag = TTY_PARITY;
			else if (lsr & UART_LSR_FE)
				flag = TTY_FRAME;
		}
1442
		if (uart_handle_sysrq_char(&up->port, ch))
L
Linus Torvalds 已提交
1443
			goto ignore_char;
1444 1445 1446

		uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);

A
Alan Cox 已提交
1447
ignore_char:
L
Linus Torvalds 已提交
1448
		lsr = serial_inp(up, UART_LSR);
1449
	} while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
L
Linus Torvalds 已提交
1450 1451 1452
	spin_unlock(&up->port.lock);
	tty_flip_buffer_push(tty);
	spin_lock(&up->port.lock);
1453
	return lsr;
L
Linus Torvalds 已提交
1454
}
1455
EXPORT_SYMBOL_GPL(serial8250_rx_chars);
L
Linus Torvalds 已提交
1456

1457
void serial8250_tx_chars(struct uart_8250_port *up)
L
Linus Torvalds 已提交
1458
{
A
Alan Cox 已提交
1459
	struct circ_buf *xmit = &up->port.state->xmit;
L
Linus Torvalds 已提交
1460 1461 1462 1463 1464 1465 1466 1467
	int count;

	if (up->port.x_char) {
		serial_outp(up, UART_TX, up->port.x_char);
		up->port.icount.tx++;
		up->port.x_char = 0;
		return;
	}
1468 1469 1470 1471 1472
	if (uart_tx_stopped(&up->port)) {
		serial8250_stop_tx(&up->port);
		return;
	}
	if (uart_circ_empty(xmit)) {
1473
		__stop_tx(up);
L
Linus Torvalds 已提交
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
		return;
	}

	count = up->tx_loadsz;
	do {
		serial_out(up, UART_TX, xmit->buf[xmit->tail]);
		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
		up->port.icount.tx++;
		if (uart_circ_empty(xmit))
			break;
	} while (--count > 0);

	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
		uart_write_wakeup(&up->port);

	DEBUG_INTR("THRE...");

	if (uart_circ_empty(xmit))
1492
		__stop_tx(up);
L
Linus Torvalds 已提交
1493
}
1494
EXPORT_SYMBOL_GPL(serial8250_tx_chars);
L
Linus Torvalds 已提交
1495

1496
unsigned int serial8250_modem_status(struct uart_8250_port *up)
L
Linus Torvalds 已提交
1497
{
1498 1499
	unsigned int status = serial_in(up, UART_MSR);

1500 1501
	status |= up->msr_saved_flags;
	up->msr_saved_flags = 0;
1502
	if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
A
Alan Cox 已提交
1503
	    up->port.state != NULL) {
1504 1505 1506 1507 1508 1509 1510 1511 1512
		if (status & UART_MSR_TERI)
			up->port.icount.rng++;
		if (status & UART_MSR_DDSR)
			up->port.icount.dsr++;
		if (status & UART_MSR_DDCD)
			uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
		if (status & UART_MSR_DCTS)
			uart_handle_cts_change(&up->port, status & UART_MSR_CTS);

1513
		wake_up_interruptible(&up->port.state->port.delta_msr_wait);
1514
	}
L
Linus Torvalds 已提交
1515

1516
	return status;
L
Linus Torvalds 已提交
1517
}
1518
EXPORT_SYMBOL_GPL(serial8250_modem_status);
L
Linus Torvalds 已提交
1519 1520 1521 1522

/*
 * This handles the interrupt from one port.
 */
1523
int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
L
Linus Torvalds 已提交
1524
{
1525
	unsigned char status;
1526
	unsigned long flags;
1527 1528 1529 1530 1531
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);

	if (iir & UART_IIR_NO_INT)
		return 0;
1532

1533
	spin_lock_irqsave(&up->port.lock, flags);
1534 1535

	status = serial_inp(up, UART_LSR);
L
Linus Torvalds 已提交
1536 1537 1538

	DEBUG_INTR("status = %x...", status);

1539
	if (status & (UART_LSR_DR | UART_LSR_BI))
1540 1541
		status = serial8250_rx_chars(up, status);
	serial8250_modem_status(up);
L
Linus Torvalds 已提交
1542
	if (status & UART_LSR_THRE)
1543
		serial8250_tx_chars(up);
1544

1545
	spin_unlock_irqrestore(&up->port.lock, flags);
1546
	return 1;
1547
}
1548
EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558

static int serial8250_default_handle_irq(struct uart_port *port)
{
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
	unsigned int iir = serial_in(up, UART_IIR);

	return serial8250_handle_irq(port, iir);
}

L
Linus Torvalds 已提交
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
/*
 * This is the serial driver's interrupt routine.
 *
 * Arjan thinks the old way was overly complex, so it got simplified.
 * Alan disagrees, saying that need the complexity to handle the weird
 * nature of ISA shared interrupts.  (This is a special exception.)
 *
 * In order to handle ISA shared interrupts properly, we need to check
 * that all ports have been serviced, and therefore the ISA interrupt
 * line has been de-asserted.
 *
 * This means we need to loop through all ports. checking that they
 * don't have an interrupt pending.
 */
1573
static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
{
	struct irq_info *i = dev_id;
	struct list_head *l, *end = NULL;
	int pass_counter = 0, handled = 0;

	DEBUG_INTR("serial8250_interrupt(%d)...", irq);

	spin_lock(&i->lock);

	l = i->head;
	do {
		struct uart_8250_port *up;
1586
		struct uart_port *port;
1587
		bool skip;
L
Linus Torvalds 已提交
1588 1589

		up = list_entry(l, struct uart_8250_port, list);
1590
		port = &up->port;
1591
		skip = pass_counter && up->port.flags & UPF_IIR_ONCE;
L
Linus Torvalds 已提交
1592

1593
		if (!skip && port->handle_irq(port)) {
L
Linus Torvalds 已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602
			handled = 1;
			end = NULL;
		} else if (end == NULL)
			end = l;

		l = l->next;

		if (l == i->head && pass_counter++ > PASS_LIMIT) {
			/* If we hit this, we're dead. */
1603 1604
			printk_ratelimited(KERN_ERR
				"serial8250: too much work for irq%d\n", irq);
L
Linus Torvalds 已提交
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
			break;
		}
	} while (l != end);

	spin_unlock(&i->lock);

	DEBUG_INTR("end.\n");

	return IRQ_RETVAL(handled);
}

/*
 * To support ISA shared interrupts, we need to have one interrupt
 * handler that ensures that the IRQ line has been deasserted
 * before returning.  Failing to do this will result in the IRQ
 * line being stuck active, and, since ISA irqs are edge triggered,
 * no more IRQs will be seen.
 */
static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
{
	spin_lock_irq(&i->lock);

	if (!list_empty(i->head)) {
		if (i->head == &up->list)
			i->head = i->head->next;
		list_del(&up->list);
	} else {
		BUG_ON(i->head != &up->list);
		i->head = NULL;
	}
	spin_unlock_irq(&i->lock);
A
Alan Cox 已提交
1636 1637 1638 1639 1640
	/* List empty so throw away the hash node */
	if (i->head == NULL) {
		hlist_del(&i->node);
		kfree(i);
	}
L
Linus Torvalds 已提交
1641 1642 1643 1644
}

static int serial_link_irq_chain(struct uart_8250_port *up)
{
A
Alan Cox 已提交
1645 1646 1647
	struct hlist_head *h;
	struct hlist_node *n;
	struct irq_info *i;
1648
	int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
L
Linus Torvalds 已提交
1649

A
Alan Cox 已提交
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
	mutex_lock(&hash_mutex);

	h = &irq_lists[up->port.irq % NR_IRQ_HASH];

	hlist_for_each(n, h) {
		i = hlist_entry(n, struct irq_info, node);
		if (i->irq == up->port.irq)
			break;
	}

	if (n == NULL) {
		i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
		if (i == NULL) {
			mutex_unlock(&hash_mutex);
			return -ENOMEM;
		}
		spin_lock_init(&i->lock);
		i->irq = up->port.irq;
		hlist_add_head(&i->node, h);
	}
	mutex_unlock(&hash_mutex);

L
Linus Torvalds 已提交
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
	spin_lock_irq(&i->lock);

	if (i->head) {
		list_add(&up->list, i->head);
		spin_unlock_irq(&i->lock);

		ret = 0;
	} else {
		INIT_LIST_HEAD(&up->list);
		i->head = &up->list;
		spin_unlock_irq(&i->lock);
1683
		irq_flags |= up->port.irqflags;
L
Linus Torvalds 已提交
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
		ret = request_irq(up->port.irq, serial8250_interrupt,
				  irq_flags, "serial", i);
		if (ret < 0)
			serial_do_unlink(i, up);
	}

	return ret;
}

static void serial_unlink_irq_chain(struct uart_8250_port *up)
{
A
Alan Cox 已提交
1695 1696 1697
	struct irq_info *i;
	struct hlist_node *n;
	struct hlist_head *h;
L
Linus Torvalds 已提交
1698

A
Alan Cox 已提交
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
	mutex_lock(&hash_mutex);

	h = &irq_lists[up->port.irq % NR_IRQ_HASH];

	hlist_for_each(n, h) {
		i = hlist_entry(n, struct irq_info, node);
		if (i->irq == up->port.irq)
			break;
	}

	BUG_ON(n == NULL);
L
Linus Torvalds 已提交
1710 1711 1712 1713 1714 1715
	BUG_ON(i->head == NULL);

	if (list_empty(i->head))
		free_irq(up->port.irq, i);

	serial_do_unlink(i, up);
A
Alan Cox 已提交
1716
	mutex_unlock(&hash_mutex);
L
Linus Torvalds 已提交
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
}

/*
 * This function is used to handle ports that do not have an
 * interrupt.  This doesn't work very well for 16450's, but gives
 * barely passable results for a 16550A.  (Although at the expense
 * of much CPU overhead).
 */
static void serial8250_timeout(unsigned long data)
{
	struct uart_8250_port *up = (struct uart_8250_port *)data;

1729
	up->port.handle_irq(&up->port);
1730
	mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
A
Alex Williamson 已提交
1731 1732 1733 1734 1735
}

static void serial8250_backup_timeout(unsigned long data)
{
	struct uart_8250_port *up = (struct uart_8250_port *)data;
1736 1737
	unsigned int iir, ier = 0, lsr;
	unsigned long flags;
A
Alex Williamson 已提交
1738

1739 1740
	spin_lock_irqsave(&up->port.lock, flags);

A
Alex Williamson 已提交
1741 1742 1743 1744
	/*
	 * Must disable interrupts or else we risk racing with the interrupt
	 * based handler.
	 */
A
Alan Cox 已提交
1745
	if (up->port.irq) {
A
Alex Williamson 已提交
1746 1747 1748
		ier = serial_in(up, UART_IER);
		serial_out(up, UART_IER, 0);
	}
L
Linus Torvalds 已提交
1749

A
Alex Williamson 已提交
1750 1751 1752 1753 1754 1755 1756 1757
	iir = serial_in(up, UART_IIR);

	/*
	 * This should be a safe test for anyone who doesn't trust the
	 * IIR bits on their UART, but it's specifically designed for
	 * the "Diva" UART used on the management processor on many HP
	 * ia64 and parisc boxes.
	 */
1758 1759
	lsr = serial_in(up, UART_LSR);
	up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
A
Alex Williamson 已提交
1760
	if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
A
Alan Cox 已提交
1761
	    (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
1762
	    (lsr & UART_LSR_THRE)) {
A
Alex Williamson 已提交
1763 1764 1765 1766 1767
		iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
		iir |= UART_IIR_THRI;
	}

	if (!(iir & UART_IIR_NO_INT))
1768
		serial8250_tx_chars(up);
A
Alex Williamson 已提交
1769

A
Alan Cox 已提交
1770
	if (up->port.irq)
A
Alex Williamson 已提交
1771 1772
		serial_out(up, UART_IER, ier);

1773 1774
	spin_unlock_irqrestore(&up->port.lock, flags);

A
Alex Williamson 已提交
1775
	/* Standard timer interval plus 0.2s to keep the port running */
A
Alan Cox 已提交
1776
	mod_timer(&up->timer,
1777
		jiffies + uart_poll_timeout(&up->port) + HZ / 5);
L
Linus Torvalds 已提交
1778 1779 1780 1781
}

static unsigned int serial8250_tx_empty(struct uart_port *port)
{
1782 1783
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
1784
	unsigned long flags;
1785
	unsigned int lsr;
L
Linus Torvalds 已提交
1786 1787

	spin_lock_irqsave(&up->port.lock, flags);
1788 1789
	lsr = serial_in(up, UART_LSR);
	up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
L
Linus Torvalds 已提交
1790 1791
	spin_unlock_irqrestore(&up->port.lock, flags);

1792
	return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
L
Linus Torvalds 已提交
1793 1794 1795 1796
}

static unsigned int serial8250_get_mctrl(struct uart_port *port)
{
1797 1798
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
1799
	unsigned int status;
L
Linus Torvalds 已提交
1800 1801
	unsigned int ret;

1802
	status = serial8250_modem_status(up);
L
Linus Torvalds 已提交
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817

	ret = 0;
	if (status & UART_MSR_DCD)
		ret |= TIOCM_CAR;
	if (status & UART_MSR_RI)
		ret |= TIOCM_RNG;
	if (status & UART_MSR_DSR)
		ret |= TIOCM_DSR;
	if (status & UART_MSR_CTS)
		ret |= TIOCM_CTS;
	return ret;
}

static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
1818 1819
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
	unsigned char mcr = 0;

	if (mctrl & TIOCM_RTS)
		mcr |= UART_MCR_RTS;
	if (mctrl & TIOCM_DTR)
		mcr |= UART_MCR_DTR;
	if (mctrl & TIOCM_OUT1)
		mcr |= UART_MCR_OUT1;
	if (mctrl & TIOCM_OUT2)
		mcr |= UART_MCR_OUT2;
	if (mctrl & TIOCM_LOOP)
		mcr |= UART_MCR_LOOP;

	mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;

	serial_out(up, UART_MCR, mcr);
}

static void serial8250_break_ctl(struct uart_port *port, int break_state)
{
1840 1841
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
	unsigned long flags;

	spin_lock_irqsave(&up->port.lock, flags);
	if (break_state == -1)
		up->lcr |= UART_LCR_SBC;
	else
		up->lcr &= ~UART_LCR_SBC;
	serial_out(up, UART_LCR, up->lcr);
	spin_unlock_irqrestore(&up->port.lock, flags);
}

A
Alex Williamson 已提交
1853 1854 1855
/*
 *	Wait for transmitter & holding register to empty
 */
1856
static void wait_for_xmitr(struct uart_8250_port *up, int bits)
A
Alex Williamson 已提交
1857 1858 1859 1860
{
	unsigned int status, tmout = 10000;

	/* Wait up to 10ms for the character(s) to be sent. */
1861
	for (;;) {
A
Alex Williamson 已提交
1862 1863
		status = serial_in(up, UART_LSR);

1864
		up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
A
Alex Williamson 已提交
1865

1866 1867
		if ((status & bits) == bits)
			break;
A
Alex Williamson 已提交
1868 1869 1870
		if (--tmout == 0)
			break;
		udelay(1);
1871
	}
A
Alex Williamson 已提交
1872 1873 1874

	/* Wait up to 1s for flow control if necessary */
	if (up->port.flags & UPF_CONS_FLOW) {
1875 1876 1877 1878 1879 1880
		unsigned int tmout;
		for (tmout = 1000000; tmout; tmout--) {
			unsigned int msr = serial_in(up, UART_MSR);
			up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
			if (msr & UART_MSR_CTS)
				break;
A
Alex Williamson 已提交
1881 1882 1883 1884 1885 1886
			udelay(1);
			touch_nmi_watchdog();
		}
	}
}

J
Jason Wessel 已提交
1887 1888 1889 1890 1891 1892 1893 1894
#ifdef CONFIG_CONSOLE_POLL
/*
 * Console polling routines for writing and reading from the uart while
 * in an interrupt or debug context.
 */

static int serial8250_get_poll_char(struct uart_port *port)
{
1895 1896
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
J
Jason Wessel 已提交
1897 1898
	unsigned char lsr = serial_inp(up, UART_LSR);

1899 1900
	if (!(lsr & UART_LSR_DR))
		return NO_POLL_CHAR;
J
Jason Wessel 已提交
1901 1902 1903 1904 1905 1906 1907 1908 1909

	return serial_inp(up, UART_RX);
}


static void serial8250_put_poll_char(struct uart_port *port,
			 unsigned char c)
{
	unsigned int ier;
1910 1911
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
J
Jason Wessel 已提交
1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942

	/*
	 *	First save the IER then disable the interrupts
	 */
	ier = serial_in(up, UART_IER);
	if (up->capabilities & UART_CAP_UUE)
		serial_out(up, UART_IER, UART_IER_UUE);
	else
		serial_out(up, UART_IER, 0);

	wait_for_xmitr(up, BOTH_EMPTY);
	/*
	 *	Send the character out.
	 *	If a LF, also do CR...
	 */
	serial_out(up, UART_TX, c);
	if (c == 10) {
		wait_for_xmitr(up, BOTH_EMPTY);
		serial_out(up, UART_TX, 13);
	}

	/*
	 *	Finally, wait for transmitter to become empty
	 *	and restore the IER
	 */
	wait_for_xmitr(up, BOTH_EMPTY);
	serial_out(up, UART_IER, ier);
}

#endif /* CONFIG_CONSOLE_POLL */

L
Linus Torvalds 已提交
1943 1944
static int serial8250_startup(struct uart_port *port)
{
1945 1946
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
1947
	unsigned long flags;
1948
	unsigned char lsr, iir;
L
Linus Torvalds 已提交
1949 1950
	int retval;

O
Ondrej Puzman 已提交
1951 1952
	up->port.fifosize = uart_config[up->port.type].fifo_size;
	up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
L
Linus Torvalds 已提交
1953 1954 1955
	up->capabilities = uart_config[up->port.type].flags;
	up->mcr = 0;

A
Alan Cox 已提交
1956 1957 1958
	if (up->port.iotype != up->cur_iotype)
		set_io_from_upio(port);

L
Linus Torvalds 已提交
1959 1960 1961
	if (up->port.type == PORT_16C950) {
		/* Wake up and initialize UART */
		up->acr = 0;
1962
		serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
L
Linus Torvalds 已提交
1963 1964 1965 1966
		serial_outp(up, UART_EFR, UART_EFR_ECB);
		serial_outp(up, UART_IER, 0);
		serial_outp(up, UART_LCR, 0);
		serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
1967
		serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
L
Linus Torvalds 已提交
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
		serial_outp(up, UART_EFR, UART_EFR_ECB);
		serial_outp(up, UART_LCR, 0);
	}

#ifdef CONFIG_SERIAL_8250_RSA
	/*
	 * If this is an RSA port, see if we can kick it up to the
	 * higher speed clock.
	 */
	enable_rsa(up);
#endif

	/*
	 * Clear the FIFO buffers and disable them.
A
Alexey Dobriyan 已提交
1982
	 * (they will be reenabled in set_termios())
L
Linus Torvalds 已提交
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
	 */
	serial8250_clear_fifos(up);

	/*
	 * Clear the interrupt registers.
	 */
	(void) serial_inp(up, UART_LSR);
	(void) serial_inp(up, UART_RX);
	(void) serial_inp(up, UART_IIR);
	(void) serial_inp(up, UART_MSR);

	/*
	 * At this point, there's no way the LSR could still be 0xff;
	 * if it is, then bail out, because there's likely no UART
	 * here.
	 */
	if (!(up->port.flags & UPF_BUGGY_UART) &&
	    (serial_inp(up, UART_LSR) == 0xff)) {
2001 2002
		printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
				   serial_index(&up->port));
L
Linus Torvalds 已提交
2003 2004 2005 2006 2007 2008 2009 2010 2011
		return -ENODEV;
	}

	/*
	 * For a XR16C850, we need to set the trigger levels
	 */
	if (up->port.type == PORT_16850) {
		unsigned char fctr;

2012
		serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
L
Linus Torvalds 已提交
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022

		fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
		serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
		serial_outp(up, UART_TRG, UART_TRG_96);
		serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
		serial_outp(up, UART_TRG, UART_TRG_96);

		serial_outp(up, UART_LCR, 0);
	}

A
Alan Cox 已提交
2023
	if (up->port.irq) {
2024
		unsigned char iir1;
A
Alex Williamson 已提交
2025 2026 2027 2028 2029 2030 2031 2032
		/*
		 * Test for UARTs that do not reassert THRE when the
		 * transmitter is idle and the interrupt has already
		 * been cleared.  Real 16550s should always reassert
		 * this interrupt whenever the transmitter is idle and
		 * the interrupt is enabled.  Delays are necessary to
		 * allow register changes to become visible.
		 */
B
Borislav Petkov 已提交
2033
		spin_lock_irqsave(&up->port.lock, flags);
2034
		if (up->port.irqflags & IRQF_SHARED)
2035
			disable_irq_nosync(up->port.irq);
A
Alex Williamson 已提交
2036 2037 2038 2039

		wait_for_xmitr(up, UART_LSR_THRE);
		serial_out_sync(up, UART_IER, UART_IER_THRI);
		udelay(1); /* allow THRE to set */
2040
		iir1 = serial_in(up, UART_IIR);
A
Alex Williamson 已提交
2041 2042 2043 2044 2045 2046
		serial_out(up, UART_IER, 0);
		serial_out_sync(up, UART_IER, UART_IER_THRI);
		udelay(1); /* allow a working UART time to re-assert THRE */
		iir = serial_in(up, UART_IIR);
		serial_out(up, UART_IER, 0);

2047
		if (up->port.irqflags & IRQF_SHARED)
2048
			enable_irq(up->port.irq);
B
Borislav Petkov 已提交
2049
		spin_unlock_irqrestore(&up->port.lock, flags);
A
Alex Williamson 已提交
2050 2051 2052 2053 2054

		/*
		 * If the interrupt is not reasserted, setup a timer to
		 * kick the UART on a regular basis.
		 */
2055
		if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
2056
			up->bugs |= UART_BUG_THRE;
2057 2058
			pr_debug("ttyS%d - using backup timer\n",
				 serial_index(port));
A
Alex Williamson 已提交
2059 2060 2061
		}
	}

2062 2063 2064 2065 2066 2067 2068 2069
	/*
	 * The above check will only give an accurate result the first time
	 * the port is opened so this value needs to be preserved.
	 */
	if (up->bugs & UART_BUG_THRE) {
		up->timer.function = serial8250_backup_timeout;
		up->timer.data = (unsigned long)up;
		mod_timer(&up->timer, jiffies +
2070
			uart_poll_timeout(port) + HZ / 5);
2071 2072
	}

L
Linus Torvalds 已提交
2073 2074 2075 2076 2077
	/*
	 * If the "interrupt" for this port doesn't correspond with any
	 * hardware interrupt, we use a timer-based system.  The original
	 * driver used to do this with IRQ0.
	 */
A
Alan Cox 已提交
2078
	if (!up->port.irq) {
L
Linus Torvalds 已提交
2079
		up->timer.data = (unsigned long)up;
2080
		mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
L
Linus Torvalds 已提交
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
	} else {
		retval = serial_link_irq_chain(up);
		if (retval)
			return retval;
	}

	/*
	 * Now, initialize the UART
	 */
	serial_outp(up, UART_LCR, UART_LCR_WLEN8);

	spin_lock_irqsave(&up->port.lock, flags);
	if (up->port.flags & UPF_FOURPORT) {
A
Alan Cox 已提交
2094
		if (!up->port.irq)
L
Linus Torvalds 已提交
2095 2096 2097 2098 2099
			up->port.mctrl |= TIOCM_OUT1;
	} else
		/*
		 * Most PC uarts need OUT2 raised to enable interrupts.
		 */
A
Alan Cox 已提交
2100
		if (up->port.irq)
L
Linus Torvalds 已提交
2101 2102 2103
			up->port.mctrl |= TIOCM_OUT2;

	serial8250_set_mctrl(&up->port, up->port.mctrl);
2104

2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
	/* Serial over Lan (SoL) hack:
	   Intel 8257x Gigabit ethernet chips have a
	   16550 emulation, to be used for Serial Over Lan.
	   Those chips take a longer time than a normal
	   serial device to signalize that a transmission
	   data was queued. Due to that, the above test generally
	   fails. One solution would be to delay the reading of
	   iir. However, this is not reliable, since the timeout
	   is variable. So, let's just don't test if we receive
	   TX irq. This way, we'll never enable UART_BUG_TXEN.
	 */
2116
	if (skip_txen_test || up->port.flags & UPF_NO_TXEN_TEST)
2117 2118
		goto dont_test_tx_en;

2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
	/*
	 * Do a quick test to see if we receive an
	 * interrupt when we enable the TX irq.
	 */
	serial_outp(up, UART_IER, UART_IER_THRI);
	lsr = serial_in(up, UART_LSR);
	iir = serial_in(up, UART_IIR);
	serial_outp(up, UART_IER, 0);

	if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2129 2130
		if (!(up->bugs & UART_BUG_TXEN)) {
			up->bugs |= UART_BUG_TXEN;
2131
			pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2132
				 serial_index(port));
2133 2134
		}
	} else {
2135
		up->bugs &= ~UART_BUG_TXEN;
2136 2137
	}

2138
dont_test_tx_en:
L
Linus Torvalds 已提交
2139 2140
	spin_unlock_irqrestore(&up->port.lock, flags);

2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
	/*
	 * Clear the interrupt registers again for luck, and clear the
	 * saved flags to avoid getting false values from polling
	 * routines or the previous session.
	 */
	serial_inp(up, UART_LSR);
	serial_inp(up, UART_RX);
	serial_inp(up, UART_IIR);
	serial_inp(up, UART_MSR);
	up->lsr_saved_flags = 0;
	up->msr_saved_flags = 0;

L
Linus Torvalds 已提交
2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
	/*
	 * Finally, enable interrupts.  Note: Modem status interrupts
	 * are set via set_termios(), which will be occurring imminently
	 * anyway, so we don't enable them here.
	 */
	up->ier = UART_IER_RLSI | UART_IER_RDI;
	serial_outp(up, UART_IER, up->ier);

	if (up->port.flags & UPF_FOURPORT) {
		unsigned int icp;
		/*
		 * Enable interrupts on the AST Fourport board
		 */
		icp = (up->port.iobase & 0xfe0) | 0x01f;
		outb_p(0x80, icp);
		(void) inb_p(icp);
	}

	return 0;
}

static void serial8250_shutdown(struct uart_port *port)
{
2176 2177
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215
	unsigned long flags;

	/*
	 * Disable interrupts from this port
	 */
	up->ier = 0;
	serial_outp(up, UART_IER, 0);

	spin_lock_irqsave(&up->port.lock, flags);
	if (up->port.flags & UPF_FOURPORT) {
		/* reset interrupts on the AST Fourport board */
		inb((up->port.iobase & 0xfe0) | 0x1f);
		up->port.mctrl |= TIOCM_OUT1;
	} else
		up->port.mctrl &= ~TIOCM_OUT2;

	serial8250_set_mctrl(&up->port, up->port.mctrl);
	spin_unlock_irqrestore(&up->port.lock, flags);

	/*
	 * Disable break condition and FIFOs
	 */
	serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
	serial8250_clear_fifos(up);

#ifdef CONFIG_SERIAL_8250_RSA
	/*
	 * Reset the RSA board back to 115kbps compat mode.
	 */
	disable_rsa(up);
#endif

	/*
	 * Read data port to reset things, and then unlink from
	 * the IRQ chain.
	 */
	(void) serial_in(up, UART_RX);

A
Alex Williamson 已提交
2216 2217
	del_timer_sync(&up->timer);
	up->timer.function = serial8250_timeout;
A
Alan Cox 已提交
2218
	if (up->port.irq)
L
Linus Torvalds 已提交
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
		serial_unlink_irq_chain(up);
}

static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
{
	unsigned int quot;

	/*
	 * Handle magic divisors for baud rates above baud_base on
	 * SMSC SuperIO chips.
	 */
	if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
	    baud == (port->uartclk/4))
		quot = 0x8001;
	else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
		 baud == (port->uartclk/8))
		quot = 0x8002;
	else
		quot = uart_get_divisor(port, baud);

	return quot;
}

2242 2243 2244
void
serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
		          struct ktermios *old)
L
Linus Torvalds 已提交
2245
{
2246 2247
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
2248 2249 2250 2251 2252 2253
	unsigned char cval, fcr = 0;
	unsigned long flags;
	unsigned int baud, quot;

	switch (termios->c_cflag & CSIZE) {
	case CS5:
2254
		cval = UART_LCR_WLEN5;
L
Linus Torvalds 已提交
2255 2256
		break;
	case CS6:
2257
		cval = UART_LCR_WLEN6;
L
Linus Torvalds 已提交
2258 2259
		break;
	case CS7:
2260
		cval = UART_LCR_WLEN7;
L
Linus Torvalds 已提交
2261 2262 2263
		break;
	default:
	case CS8:
2264
		cval = UART_LCR_WLEN8;
L
Linus Torvalds 已提交
2265 2266 2267 2268
		break;
	}

	if (termios->c_cflag & CSTOPB)
2269
		cval |= UART_LCR_STOP;
L
Linus Torvalds 已提交
2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281
	if (termios->c_cflag & PARENB)
		cval |= UART_LCR_PARITY;
	if (!(termios->c_cflag & PARODD))
		cval |= UART_LCR_EPAR;
#ifdef CMSPAR
	if (termios->c_cflag & CMSPAR)
		cval |= UART_LCR_SPAR;
#endif

	/*
	 * Ask the core to calculate the divisor for us.
	 */
2282 2283 2284
	baud = uart_get_baud_rate(port, termios, old,
				  port->uartclk / 16 / 0xffff,
				  port->uartclk / 16);
L
Linus Torvalds 已提交
2285 2286 2287
	quot = serial8250_get_divisor(port, baud);

	/*
2288
	 * Oxford Semi 952 rev B workaround
L
Linus Torvalds 已提交
2289
	 */
2290
	if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
A
Alan Cox 已提交
2291
		quot++;
L
Linus Torvalds 已提交
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355

	if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
		if (baud < 2400)
			fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
		else
			fcr = uart_config[up->port.type].fcr;
	}

	/*
	 * MCR-based auto flow control.  When AFE is enabled, RTS will be
	 * deasserted when the receive FIFO contains more characters than
	 * the trigger, or the MCR RTS bit is cleared.  In the case where
	 * the remote UART is not using CTS auto flow control, we must
	 * have sufficient FIFO entries for the latency of the remote
	 * UART to respond.  IOW, at least 32 bytes of FIFO.
	 */
	if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
		up->mcr &= ~UART_MCR_AFE;
		if (termios->c_cflag & CRTSCTS)
			up->mcr |= UART_MCR_AFE;
	}

	/*
	 * Ok, we're now changing the port state.  Do it with
	 * interrupts disabled.
	 */
	spin_lock_irqsave(&up->port.lock, flags);

	/*
	 * Update the per-port timeout.
	 */
	uart_update_timeout(port, termios->c_cflag, baud);

	up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
	if (termios->c_iflag & INPCK)
		up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
	if (termios->c_iflag & (BRKINT | PARMRK))
		up->port.read_status_mask |= UART_LSR_BI;

	/*
	 * Characteres to ignore
	 */
	up->port.ignore_status_mask = 0;
	if (termios->c_iflag & IGNPAR)
		up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
	if (termios->c_iflag & IGNBRK) {
		up->port.ignore_status_mask |= UART_LSR_BI;
		/*
		 * If we're ignoring parity and break indicators,
		 * ignore overruns too (for real raw support).
		 */
		if (termios->c_iflag & IGNPAR)
			up->port.ignore_status_mask |= UART_LSR_OE;
	}

	/*
	 * ignore all characters if CREAD is not set
	 */
	if ((termios->c_cflag & CREAD) == 0)
		up->port.ignore_status_mask |= UART_LSR_DR;

	/*
	 * CTS flow control flag and modem status interrupts
	 */
2356
	up->ier &= ~UART_IER_MSI;
2357 2358
	if (!(up->bugs & UART_BUG_NOMSR) &&
			UART_ENABLE_MS(&up->port, termios->c_cflag))
L
Linus Torvalds 已提交
2359 2360
		up->ier |= UART_IER_MSI;
	if (up->capabilities & UART_CAP_UUE)
2361 2362 2363
		up->ier |= UART_IER_UUE;
	if (up->capabilities & UART_CAP_RTOIE)
		up->ier |= UART_IER_RTOIE;
L
Linus Torvalds 已提交
2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376

	serial_out(up, UART_IER, up->ier);

	if (up->capabilities & UART_CAP_EFR) {
		unsigned char efr = 0;
		/*
		 * TI16C752/Startech hardware flow control.  FIXME:
		 * - TI16C752 requires control thresholds to be set.
		 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
		 */
		if (termios->c_cflag & CRTSCTS)
			efr |= UART_EFR_CTS;

2377
		serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
2378 2379 2380 2381
		if (up->port.flags & UPF_EXAR_EFR)
			serial_outp(up, UART_XR_EFR, efr);
		else
			serial_outp(up, UART_EFR, efr);
L
Linus Torvalds 已提交
2382 2383
	}

2384
#ifdef CONFIG_ARCH_OMAP
2385
	/* Workaround to enable 115200 baud on OMAP1510 internal ports */
2386
	if (cpu_is_omap1510() && is_omap_port(up)) {
2387 2388 2389 2390 2391 2392 2393 2394
		if (baud == 115200) {
			quot = 1;
			serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
		} else
			serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
	}
#endif

L
Linus Torvalds 已提交
2395 2396 2397 2398 2399 2400 2401
	if (up->capabilities & UART_NATSEMI) {
		/* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
		serial_outp(up, UART_LCR, 0xe0);
	} else {
		serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
	}

2402
	serial_dl_write(up, quot);
L
Linus Torvalds 已提交
2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421

	/*
	 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
	 * is written without DLAB set, this mode will be disabled.
	 */
	if (up->port.type == PORT_16750)
		serial_outp(up, UART_FCR, fcr);

	serial_outp(up, UART_LCR, cval);		/* reset DLAB */
	up->lcr = cval;					/* Save LCR */
	if (up->port.type != PORT_16750) {
		if (fcr & UART_FCR_ENABLE_FIFO) {
			/* emulated UARTs (Lucent Venus 167x) need two steps */
			serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
		}
		serial_outp(up, UART_FCR, fcr);		/* set fcr */
	}
	serial8250_set_mctrl(&up->port, up->port.mctrl);
	spin_unlock_irqrestore(&up->port.lock, flags);
A
Alan Cox 已提交
2422 2423 2424
	/* Don't rewrite B0 */
	if (tty_termios_baud_rate(termios))
		tty_termios_encode_baud_rate(termios, baud, baud);
L
Linus Torvalds 已提交
2425
}
2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436
EXPORT_SYMBOL(serial8250_do_set_termios);

static void
serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
		       struct ktermios *old)
{
	if (port->set_termios)
		port->set_termios(port, termios, old);
	else
		serial8250_do_set_termios(port, termios, old);
}
L
Linus Torvalds 已提交
2437

2438
static void
A
Arnd Bergmann 已提交
2439
serial8250_set_ldisc(struct uart_port *port, int new)
2440
{
A
Arnd Bergmann 已提交
2441
	if (new == N_PPS) {
2442 2443 2444 2445 2446 2447
		port->flags |= UPF_HARDPPS_CD;
		serial8250_enable_ms(port);
	} else
		port->flags &= ~UPF_HARDPPS_CD;
}

2448 2449 2450

void serial8250_do_pm(struct uart_port *port, unsigned int state,
		      unsigned int oldstate)
L
Linus Torvalds 已提交
2451
{
2452 2453
	struct uart_8250_port *p =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
2454 2455

	serial8250_set_sleep(p, state != 0);
2456 2457
}
EXPORT_SYMBOL(serial8250_do_pm);
L
Linus Torvalds 已提交
2458

2459 2460 2461 2462 2463 2464 2465 2466
static void
serial8250_pm(struct uart_port *port, unsigned int state,
	      unsigned int oldstate)
{
	if (port->pm)
		port->pm(port, state, oldstate);
	else
		serial8250_do_pm(port, state, oldstate);
L
Linus Torvalds 已提交
2467 2468
}

2469 2470 2471
static unsigned int serial8250_port_size(struct uart_8250_port *pt)
{
	if (pt->port.iotype == UPIO_AU)
2472
		return 0x1000;
2473 2474 2475 2476 2477 2478 2479
#ifdef CONFIG_ARCH_OMAP
	if (is_omap_port(pt))
		return 0x16 << pt->port.regshift;
#endif
	return 8 << pt->port.regshift;
}

L
Linus Torvalds 已提交
2480 2481 2482 2483 2484
/*
 * Resource handling.
 */
static int serial8250_request_std_resource(struct uart_8250_port *up)
{
2485
	unsigned int size = serial8250_port_size(up);
L
Linus Torvalds 已提交
2486 2487 2488
	int ret = 0;

	switch (up->port.iotype) {
2489
	case UPIO_AU:
2490 2491
	case UPIO_TSI:
	case UPIO_MEM32:
L
Linus Torvalds 已提交
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
	case UPIO_MEM:
		if (!up->port.mapbase)
			break;

		if (!request_mem_region(up->port.mapbase, size, "serial")) {
			ret = -EBUSY;
			break;
		}

		if (up->port.flags & UPF_IOREMAP) {
2502 2503
			up->port.membase = ioremap_nocache(up->port.mapbase,
									size);
L
Linus Torvalds 已提交
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521
			if (!up->port.membase) {
				release_mem_region(up->port.mapbase, size);
				ret = -ENOMEM;
			}
		}
		break;

	case UPIO_HUB6:
	case UPIO_PORT:
		if (!request_region(up->port.iobase, size, "serial"))
			ret = -EBUSY;
		break;
	}
	return ret;
}

static void serial8250_release_std_resource(struct uart_8250_port *up)
{
2522
	unsigned int size = serial8250_port_size(up);
L
Linus Torvalds 已提交
2523 2524

	switch (up->port.iotype) {
2525
	case UPIO_AU:
2526 2527
	case UPIO_TSI:
	case UPIO_MEM32:
L
Linus Torvalds 已提交
2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550
	case UPIO_MEM:
		if (!up->port.mapbase)
			break;

		if (up->port.flags & UPF_IOREMAP) {
			iounmap(up->port.membase);
			up->port.membase = NULL;
		}

		release_mem_region(up->port.mapbase, size);
		break;

	case UPIO_HUB6:
	case UPIO_PORT:
		release_region(up->port.iobase, size);
		break;
	}
}

static int serial8250_request_rsa_resource(struct uart_8250_port *up)
{
	unsigned long start = UART_RSA_BASE << up->port.regshift;
	unsigned int size = 8 << up->port.regshift;
2551
	int ret = -EINVAL;
L
Linus Torvalds 已提交
2552 2553 2554 2555 2556

	switch (up->port.iotype) {
	case UPIO_HUB6:
	case UPIO_PORT:
		start += up->port.iobase;
2557 2558 2559
		if (request_region(start, size, "serial-rsa"))
			ret = 0;
		else
L
Linus Torvalds 已提交
2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581
			ret = -EBUSY;
		break;
	}

	return ret;
}

static void serial8250_release_rsa_resource(struct uart_8250_port *up)
{
	unsigned long offset = UART_RSA_BASE << up->port.regshift;
	unsigned int size = 8 << up->port.regshift;

	switch (up->port.iotype) {
	case UPIO_HUB6:
	case UPIO_PORT:
		release_region(up->port.iobase + offset, size);
		break;
	}
}

static void serial8250_release_port(struct uart_port *port)
{
2582 2583
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
2584 2585 2586 2587 2588 2589 2590 2591

	serial8250_release_std_resource(up);
	if (up->port.type == PORT_RSA)
		serial8250_release_rsa_resource(up);
}

static int serial8250_request_port(struct uart_port *port)
{
2592 2593
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
	int ret = 0;

	ret = serial8250_request_std_resource(up);
	if (ret == 0 && up->port.type == PORT_RSA) {
		ret = serial8250_request_rsa_resource(up);
		if (ret < 0)
			serial8250_release_std_resource(up);
	}

	return ret;
}

static void serial8250_config_port(struct uart_port *port, int flags)
{
2608 2609
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624
	int probeflags = PROBE_ANY;
	int ret;

	/*
	 * Find the region that we can probe for.  This in turn
	 * tells us whether we can probe for the type of port.
	 */
	ret = serial8250_request_std_resource(up);
	if (ret < 0)
		return;

	ret = serial8250_request_rsa_resource(up);
	if (ret < 0)
		probeflags &= ~PROBE_RSA;

A
Alan Cox 已提交
2625 2626 2627
	if (up->port.iotype != up->cur_iotype)
		set_io_from_upio(port);

L
Linus Torvalds 已提交
2628 2629
	if (flags & UART_CONFIG_TYPE)
		autoconfig(up, probeflags);
2630 2631 2632 2633 2634

	/* if access method is AU, it is a 16550 with a quirk */
	if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
		up->bugs |= UART_BUG_NOMSR;

L
Linus Torvalds 已提交
2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646
	if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
		autoconfig_irq(up);

	if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
		serial8250_release_rsa_resource(up);
	if (up->port.type == PORT_UNKNOWN)
		serial8250_release_std_resource(up);
}

static int
serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
{
Y
Yinghai Lu 已提交
2647
	if (ser->irq >= nr_irqs || ser->irq < 0 ||
L
Linus Torvalds 已提交
2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676
	    ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
	    ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
	    ser->type == PORT_STARTECH)
		return -EINVAL;
	return 0;
}

static const char *
serial8250_type(struct uart_port *port)
{
	int type = port->type;

	if (type >= ARRAY_SIZE(uart_config))
		type = 0;
	return uart_config[type].name;
}

static struct uart_ops serial8250_pops = {
	.tx_empty	= serial8250_tx_empty,
	.set_mctrl	= serial8250_set_mctrl,
	.get_mctrl	= serial8250_get_mctrl,
	.stop_tx	= serial8250_stop_tx,
	.start_tx	= serial8250_start_tx,
	.stop_rx	= serial8250_stop_rx,
	.enable_ms	= serial8250_enable_ms,
	.break_ctl	= serial8250_break_ctl,
	.startup	= serial8250_startup,
	.shutdown	= serial8250_shutdown,
	.set_termios	= serial8250_set_termios,
2677
	.set_ldisc	= serial8250_set_ldisc,
L
Linus Torvalds 已提交
2678 2679 2680 2681 2682 2683
	.pm		= serial8250_pm,
	.type		= serial8250_type,
	.release_port	= serial8250_release_port,
	.request_port	= serial8250_request_port,
	.config_port	= serial8250_config_port,
	.verify_port	= serial8250_verify_port,
J
Jason Wessel 已提交
2684 2685 2686 2687
#ifdef CONFIG_CONSOLE_POLL
	.poll_get_char = serial8250_get_poll_char,
	.poll_put_char = serial8250_put_poll_char,
#endif
L
Linus Torvalds 已提交
2688 2689 2690 2691
};

static struct uart_8250_port serial8250_ports[UART_NR];

2692 2693 2694 2695 2696 2697 2698 2699 2700 2701
static void (*serial8250_isa_config)(int port, struct uart_port *up,
	unsigned short *capabilities);

void serial8250_set_isa_configurator(
	void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
{
	serial8250_isa_config = v;
}
EXPORT_SYMBOL(serial8250_set_isa_configurator);

L
Linus Torvalds 已提交
2702 2703 2704 2705
static void __init serial8250_isa_init_ports(void)
{
	struct uart_8250_port *up;
	static int first = 1;
2706
	int i, irqflag = 0;
L
Linus Torvalds 已提交
2707 2708 2709 2710 2711

	if (!first)
		return;
	first = 0;

2712
	for (i = 0; i < nr_uarts; i++) {
L
Linus Torvalds 已提交
2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729
		struct uart_8250_port *up = &serial8250_ports[i];

		up->port.line = i;
		spin_lock_init(&up->port.lock);

		init_timer(&up->timer);
		up->timer.function = serial8250_timeout;

		/*
		 * ALPHA_KLUDGE_MCR needs to be killed.
		 */
		up->mcr_mask = ~ALPHA_KLUDGE_MCR;
		up->mcr_force = ALPHA_KLUDGE_MCR;

		up->port.ops = &serial8250_pops;
	}

2730 2731 2732
	if (share_irqs)
		irqflag = IRQF_SHARED;

2733
	for (i = 0, up = serial8250_ports;
2734
	     i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
L
Linus Torvalds 已提交
2735 2736 2737
	     i++, up++) {
		up->port.iobase   = old_serial_port[i].port;
		up->port.irq      = irq_canonicalize(old_serial_port[i].irq);
2738
		up->port.irqflags = old_serial_port[i].irqflags;
L
Linus Torvalds 已提交
2739 2740 2741 2742 2743 2744
		up->port.uartclk  = old_serial_port[i].baud_base * 16;
		up->port.flags    = old_serial_port[i].flags;
		up->port.hub6     = old_serial_port[i].hub6;
		up->port.membase  = old_serial_port[i].iomem_base;
		up->port.iotype   = old_serial_port[i].io_type;
		up->port.regshift = old_serial_port[i].iomem_reg_shift;
2745
		set_io_from_upio(&up->port);
2746
		up->port.irqflags |= irqflag;
2747 2748 2749
		if (serial8250_isa_config != NULL)
			serial8250_isa_config(i, &up->port, &up->capabilities);

L
Linus Torvalds 已提交
2750 2751 2752
	}
}

2753 2754 2755 2756 2757 2758 2759 2760 2761
static void
serial8250_init_fixed_type_port(struct uart_8250_port *up, unsigned int type)
{
	up->port.type = type;
	up->port.fifosize = uart_config[type].fifo_size;
	up->capabilities = uart_config[type].flags;
	up->tx_loadsz = uart_config[type].tx_loadsz;
}

L
Linus Torvalds 已提交
2762 2763 2764 2765 2766
static void __init
serial8250_register_ports(struct uart_driver *drv, struct device *dev)
{
	int i;

A
Alan Cox 已提交
2767 2768 2769 2770 2771
	for (i = 0; i < nr_uarts; i++) {
		struct uart_8250_port *up = &serial8250_ports[i];
		up->cur_iotype = 0xFF;
	}

L
Linus Torvalds 已提交
2772 2773
	serial8250_isa_init_ports();

2774
	for (i = 0; i < nr_uarts; i++) {
L
Linus Torvalds 已提交
2775 2776 2777
		struct uart_8250_port *up = &serial8250_ports[i];

		up->port.dev = dev;
2778 2779 2780 2781

		if (up->port.flags & UPF_FIXED_TYPE)
			serial8250_init_fixed_type_port(up, up->port.type);

L
Linus Torvalds 已提交
2782 2783 2784 2785 2786 2787
		uart_add_one_port(drv, &up->port);
	}
}

#ifdef CONFIG_SERIAL_8250_CONSOLE

2788 2789
static void serial8250_console_putchar(struct uart_port *port, int ch)
{
2790 2791
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
2792 2793 2794 2795 2796

	wait_for_xmitr(up, UART_LSR_THRE);
	serial_out(up, UART_TX, ch);
}

L
Linus Torvalds 已提交
2797 2798 2799 2800 2801 2802 2803 2804 2805 2806
/*
 *	Print a string to the serial port trying not to disturb
 *	any possible real use of the port...
 *
 *	The console_lock must be held when we get here.
 */
static void
serial8250_console_write(struct console *co, const char *s, unsigned int count)
{
	struct uart_8250_port *up = &serial8250_ports[co->index];
2807
	unsigned long flags;
L
Linus Torvalds 已提交
2808
	unsigned int ier;
2809
	int locked = 1;
L
Linus Torvalds 已提交
2810

2811 2812
	touch_nmi_watchdog();

2813 2814
	local_irq_save(flags);
	if (up->port.sysrq) {
2815
		/* serial8250_handle_irq() already took the lock */
2816 2817 2818
		locked = 0;
	} else if (oops_in_progress) {
		locked = spin_trylock(&up->port.lock);
2819
	} else
2820
		spin_lock(&up->port.lock);
2821

L
Linus Torvalds 已提交
2822
	/*
R
Ralf Baechle 已提交
2823
	 *	First save the IER then disable the interrupts
L
Linus Torvalds 已提交
2824 2825 2826 2827 2828 2829 2830 2831
	 */
	ier = serial_in(up, UART_IER);

	if (up->capabilities & UART_CAP_UUE)
		serial_out(up, UART_IER, UART_IER_UUE);
	else
		serial_out(up, UART_IER, 0);

2832
	uart_console_write(&up->port, s, count, serial8250_console_putchar);
L
Linus Torvalds 已提交
2833 2834 2835 2836 2837

	/*
	 *	Finally, wait for transmitter to become empty
	 *	and restore the IER
	 */
A
Alan Cox 已提交
2838
	wait_for_xmitr(up, BOTH_EMPTY);
2839
	serial_out(up, UART_IER, ier);
2840

2841 2842 2843 2844 2845 2846 2847 2848
	/*
	 *	The receive handling will happen properly because the
	 *	receive ready bit will still be set; it is not cleared
	 *	on read.  However, modem control will not, we must
	 *	call it if we have saved something in the saved flags
	 *	while processing with interrupts off.
	 */
	if (up->msr_saved_flags)
2849
		serial8250_modem_status(up);
2850

2851
	if (locked)
2852 2853
		spin_unlock(&up->port.lock);
	local_irq_restore(flags);
L
Linus Torvalds 已提交
2854 2855
}

2856
static int __init serial8250_console_setup(struct console *co, char *options)
L
Linus Torvalds 已提交
2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868
{
	struct uart_port *port;
	int baud = 9600;
	int bits = 8;
	int parity = 'n';
	int flow = 'n';

	/*
	 * Check whether an invalid uart number has been specified, and
	 * if so, search for the first available port that does have
	 * console support.
	 */
2869
	if (co->index >= nr_uarts)
L
Linus Torvalds 已提交
2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880
		co->index = 0;
	port = &serial8250_ports[co->index].port;
	if (!port->iobase && !port->membase)
		return -ENODEV;

	if (options)
		uart_parse_options(options, &baud, &parity, &bits, &flow);

	return uart_set_options(port, co, baud, parity, bits, flow);
}

2881
static int serial8250_console_early_setup(void)
2882 2883 2884 2885
{
	return serial8250_find_port_for_earlycon();
}

L
Linus Torvalds 已提交
2886 2887 2888 2889 2890
static struct console serial8250_console = {
	.name		= "ttyS",
	.write		= serial8250_console_write,
	.device		= uart_console_device,
	.setup		= serial8250_console_setup,
2891
	.early_setup	= serial8250_console_early_setup,
2892
	.flags		= CON_PRINTBUFFER | CON_ANYTIME,
L
Linus Torvalds 已提交
2893 2894 2895 2896 2897 2898
	.index		= -1,
	.data		= &serial8250_reg,
};

static int __init serial8250_console_init(void)
{
2899 2900 2901
	if (nr_uarts > UART_NR)
		nr_uarts = UART_NR;

L
Linus Torvalds 已提交
2902 2903 2904 2905 2906 2907
	serial8250_isa_init_ports();
	register_console(&serial8250_console);
	return 0;
}
console_initcall(serial8250_console_init);

2908
int serial8250_find_port(struct uart_port *p)
L
Linus Torvalds 已提交
2909 2910 2911 2912
{
	int line;
	struct uart_port *port;

2913
	for (line = 0; line < nr_uarts; line++) {
L
Linus Torvalds 已提交
2914
		port = &serial8250_ports[line].port;
2915
		if (uart_match_port(p, port))
L
Linus Torvalds 已提交
2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934
			return line;
	}
	return -ENODEV;
}

#define SERIAL8250_CONSOLE	&serial8250_console
#else
#define SERIAL8250_CONSOLE	NULL
#endif

static struct uart_driver serial8250_reg = {
	.owner			= THIS_MODULE,
	.driver_name		= "serial",
	.dev_name		= "ttyS",
	.major			= TTY_MAJOR,
	.minor			= 64,
	.cons			= SERIAL8250_CONSOLE,
};

2935 2936 2937 2938 2939 2940
/*
 * early_serial_setup - early registration for 8250 ports
 *
 * Setup an 8250 port structure prior to console initialisation.  Use
 * after console initialisation will cause undefined behaviour.
 */
L
Linus Torvalds 已提交
2941 2942
int __init early_serial_setup(struct uart_port *port)
{
D
David Daney 已提交
2943 2944
	struct uart_port *p;

L
Linus Torvalds 已提交
2945 2946 2947 2948
	if (port->line >= ARRAY_SIZE(serial8250_ports))
		return -ENODEV;

	serial8250_isa_init_ports();
D
David Daney 已提交
2949 2950 2951 2952
	p = &serial8250_ports[port->line].port;
	p->iobase       = port->iobase;
	p->membase      = port->membase;
	p->irq          = port->irq;
2953
	p->irqflags     = port->irqflags;
D
David Daney 已提交
2954 2955 2956 2957 2958 2959 2960
	p->uartclk      = port->uartclk;
	p->fifosize     = port->fifosize;
	p->regshift     = port->regshift;
	p->iotype       = port->iotype;
	p->flags        = port->flags;
	p->mapbase      = port->mapbase;
	p->private_data = port->private_data;
2961 2962
	p->type		= port->type;
	p->line		= port->line;
2963 2964 2965 2966 2967 2968

	set_io_from_upio(p);
	if (port->serial_in)
		p->serial_in = port->serial_in;
	if (port->serial_out)
		p->serial_out = port->serial_out;
2969 2970 2971 2972
	if (port->handle_irq)
		p->handle_irq = port->handle_irq;
	else
		p->handle_irq = serial8250_default_handle_irq;
2973

L
Linus Torvalds 已提交
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995
	return 0;
}

/**
 *	serial8250_suspend_port - suspend one serial port
 *	@line:  serial line number
 *
 *	Suspend one serial port.
 */
void serial8250_suspend_port(int line)
{
	uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
}

/**
 *	serial8250_resume_port - resume one serial port
 *	@line:  serial line number
 *
 *	Resume one serial port.
 */
void serial8250_resume_port(int line)
{
2996 2997 2998 2999 3000 3001
	struct uart_8250_port *up = &serial8250_ports[line];

	if (up->capabilities & UART_NATSEMI) {
		/* Ensure it's still in high speed mode */
		serial_outp(up, UART_LCR, 0xE0);

3002
		ns16550a_goto_highspeed(up);
3003 3004

		serial_outp(up, UART_LCR, 0);
3005
		up->port.uartclk = 921600*16;
3006 3007
	}
	uart_resume_port(&serial8250_reg, &up->port);
L
Linus Torvalds 已提交
3008 3009 3010 3011 3012 3013 3014
}

/*
 * Register a set of serial devices attached to a platform device.  The
 * list is terminated with a zero flags entry, which means we expect
 * all entries to have at least UPF_BOOT_AUTOCONF set.
 */
3015
static int __devinit serial8250_probe(struct platform_device *dev)
L
Linus Torvalds 已提交
3016
{
3017
	struct plat_serial8250_port *p = dev->dev.platform_data;
L
Linus Torvalds 已提交
3018
	struct uart_port port;
3019
	int ret, i, irqflag = 0;
L
Linus Torvalds 已提交
3020 3021 3022

	memset(&port, 0, sizeof(struct uart_port));

3023 3024 3025
	if (share_irqs)
		irqflag = IRQF_SHARED;

3026
	for (i = 0; p && p->flags != 0; p++, i++) {
3027 3028 3029
		port.iobase		= p->iobase;
		port.membase		= p->membase;
		port.irq		= p->irq;
3030
		port.irqflags		= p->irqflags;
3031 3032 3033 3034 3035 3036 3037
		port.uartclk		= p->uartclk;
		port.regshift		= p->regshift;
		port.iotype		= p->iotype;
		port.flags		= p->flags;
		port.mapbase		= p->mapbase;
		port.hub6		= p->hub6;
		port.private_data	= p->private_data;
3038
		port.type		= p->type;
3039 3040
		port.serial_in		= p->serial_in;
		port.serial_out		= p->serial_out;
3041
		port.handle_irq		= p->handle_irq;
3042
		port.set_termios	= p->set_termios;
3043
		port.pm			= p->pm;
3044
		port.dev		= &dev->dev;
3045
		port.irqflags		|= irqflag;
3046 3047
		ret = serial8250_register_port(&port);
		if (ret < 0) {
3048
			dev_err(&dev->dev, "unable to register port at index %d "
3049 3050 3051
				"(IO%lx MEM%llx IRQ%d): %d\n", i,
				p->iobase, (unsigned long long)p->mapbase,
				p->irq, ret);
3052
		}
L
Linus Torvalds 已提交
3053 3054 3055 3056 3057 3058 3059
	}
	return 0;
}

/*
 * Remove serial ports registered against a platform device.
 */
3060
static int __devexit serial8250_remove(struct platform_device *dev)
L
Linus Torvalds 已提交
3061 3062 3063
{
	int i;

3064
	for (i = 0; i < nr_uarts; i++) {
L
Linus Torvalds 已提交
3065 3066
		struct uart_8250_port *up = &serial8250_ports[i];

3067
		if (up->port.dev == &dev->dev)
L
Linus Torvalds 已提交
3068 3069 3070 3071 3072
			serial8250_unregister_port(i);
	}
	return 0;
}

3073
static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
L
Linus Torvalds 已提交
3074 3075 3076 3077 3078 3079
{
	int i;

	for (i = 0; i < UART_NR; i++) {
		struct uart_8250_port *up = &serial8250_ports[i];

3080
		if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
L
Linus Torvalds 已提交
3081 3082 3083 3084 3085 3086
			uart_suspend_port(&serial8250_reg, &up->port);
	}

	return 0;
}

3087
static int serial8250_resume(struct platform_device *dev)
L
Linus Torvalds 已提交
3088 3089 3090 3091 3092 3093
{
	int i;

	for (i = 0; i < UART_NR; i++) {
		struct uart_8250_port *up = &serial8250_ports[i];

3094
		if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
3095
			serial8250_resume_port(i);
L
Linus Torvalds 已提交
3096 3097 3098 3099 3100
	}

	return 0;
}

3101
static struct platform_driver serial8250_isa_driver = {
L
Linus Torvalds 已提交
3102 3103 3104 3105
	.probe		= serial8250_probe,
	.remove		= __devexit_p(serial8250_remove),
	.suspend	= serial8250_suspend,
	.resume		= serial8250_resume,
3106 3107
	.driver		= {
		.name	= "serial8250",
3108
		.owner	= THIS_MODULE,
3109
	},
L
Linus Torvalds 已提交
3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122
};

/*
 * This "device" covers _all_ ISA 8250-compatible serial devices listed
 * in the table in include/asm/serial.h
 */
static struct platform_device *serial8250_isa_devs;

/*
 * serial8250_register_port and serial8250_unregister_port allows for
 * 16x50 serial ports to be configured at run-time, to support PCMCIA
 * modems and PCI multiport cards.
 */
3123
static DEFINE_MUTEX(serial_mutex);
L
Linus Torvalds 已提交
3124 3125 3126 3127 3128 3129 3130 3131

static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
{
	int i;

	/*
	 * First, find a port entry which matches.
	 */
3132
	for (i = 0; i < nr_uarts; i++)
L
Linus Torvalds 已提交
3133 3134 3135 3136 3137 3138 3139 3140
		if (uart_match_port(&serial8250_ports[i].port, port))
			return &serial8250_ports[i];

	/*
	 * We didn't find a matching entry, so look for the first
	 * free entry.  We look for one which hasn't been previously
	 * used (indicated by zero iobase).
	 */
3141
	for (i = 0; i < nr_uarts; i++)
L
Linus Torvalds 已提交
3142 3143 3144 3145 3146 3147 3148 3149
		if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
		    serial8250_ports[i].port.iobase == 0)
			return &serial8250_ports[i];

	/*
	 * That also failed.  Last resort is to find any entry which
	 * doesn't have a real port associated with it.
	 */
3150
	for (i = 0; i < nr_uarts; i++)
L
Linus Torvalds 已提交
3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177
		if (serial8250_ports[i].port.type == PORT_UNKNOWN)
			return &serial8250_ports[i];

	return NULL;
}

/**
 *	serial8250_register_port - register a serial port
 *	@port: serial port template
 *
 *	Configure the serial port specified by the request. If the
 *	port exists and is in use, it is hung up and unregistered
 *	first.
 *
 *	The port is then probed and if necessary the IRQ is autodetected
 *	If this fails an error is returned.
 *
 *	On success the port is ready to use and the line number is returned.
 */
int serial8250_register_port(struct uart_port *port)
{
	struct uart_8250_port *uart;
	int ret = -ENOSPC;

	if (port->uartclk == 0)
		return -EINVAL;

3178
	mutex_lock(&serial_mutex);
L
Linus Torvalds 已提交
3179 3180 3181 3182 3183

	uart = serial8250_find_match_or_unused(port);
	if (uart) {
		uart_remove_one_port(&serial8250_reg, &uart->port);

3184 3185 3186
		uart->port.iobase       = port->iobase;
		uart->port.membase      = port->membase;
		uart->port.irq          = port->irq;
3187
		uart->port.irqflags     = port->irqflags;
3188 3189 3190 3191 3192 3193 3194
		uart->port.uartclk      = port->uartclk;
		uart->port.fifosize     = port->fifosize;
		uart->port.regshift     = port->regshift;
		uart->port.iotype       = port->iotype;
		uart->port.flags        = port->flags | UPF_BOOT_AUTOCONF;
		uart->port.mapbase      = port->mapbase;
		uart->port.private_data = port->private_data;
L
Linus Torvalds 已提交
3195 3196
		if (port->dev)
			uart->port.dev = port->dev;
3197

3198 3199
		if (port->flags & UPF_FIXED_TYPE)
			serial8250_init_fixed_type_port(uart, port->type);
3200

3201 3202 3203 3204 3205 3206
		set_io_from_upio(&uart->port);
		/* Possibly override default I/O functions.  */
		if (port->serial_in)
			uart->port.serial_in = port->serial_in;
		if (port->serial_out)
			uart->port.serial_out = port->serial_out;
3207 3208
		if (port->handle_irq)
			uart->port.handle_irq = port->handle_irq;
3209 3210 3211
		/*  Possibly override set_termios call */
		if (port->set_termios)
			uart->port.set_termios = port->set_termios;
3212 3213
		if (port->pm)
			uart->port.pm = port->pm;
L
Linus Torvalds 已提交
3214

3215 3216 3217 3218
		if (serial8250_isa_config != NULL)
			serial8250_isa_config(0, &uart->port,
					&uart->capabilities);

L
Linus Torvalds 已提交
3219 3220 3221 3222
		ret = uart_add_one_port(&serial8250_reg, &uart->port);
		if (ret == 0)
			ret = uart->port.line;
	}
3223
	mutex_unlock(&serial_mutex);
L
Linus Torvalds 已提交
3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239

	return ret;
}
EXPORT_SYMBOL(serial8250_register_port);

/**
 *	serial8250_unregister_port - remove a 16x50 serial port at runtime
 *	@line: serial line number
 *
 *	Remove one serial port.  This may not be called from interrupt
 *	context.  We hand the port back to the our control.
 */
void serial8250_unregister_port(int line)
{
	struct uart_8250_port *uart = &serial8250_ports[line];

3240
	mutex_lock(&serial_mutex);
L
Linus Torvalds 已提交
3241 3242 3243 3244 3245
	uart_remove_one_port(&serial8250_reg, &uart->port);
	if (serial8250_isa_devs) {
		uart->port.flags &= ~UPF_BOOT_AUTOCONF;
		uart->port.type = PORT_UNKNOWN;
		uart->port.dev = &serial8250_isa_devs->dev;
3246
		uart->capabilities = uart_config[uart->port.type].flags;
L
Linus Torvalds 已提交
3247 3248 3249 3250
		uart_add_one_port(&serial8250_reg, &uart->port);
	} else {
		uart->port.dev = NULL;
	}
3251
	mutex_unlock(&serial_mutex);
L
Linus Torvalds 已提交
3252 3253 3254 3255 3256
}
EXPORT_SYMBOL(serial8250_unregister_port);

static int __init serial8250_init(void)
{
A
Alan Cox 已提交
3257
	int ret;
L
Linus Torvalds 已提交
3258

3259 3260 3261
	if (nr_uarts > UART_NR)
		nr_uarts = UART_NR;

3262
	printk(KERN_INFO "Serial: 8250/16550 driver, "
3263
		"%d ports, IRQ sharing %sabled\n", nr_uarts,
L
Linus Torvalds 已提交
3264 3265
		share_irqs ? "en" : "dis");

3266 3267 3268 3269
#ifdef CONFIG_SPARC
	ret = sunserial_register_minors(&serial8250_reg, UART_NR);
#else
	serial8250_reg.nr = UART_NR;
L
Linus Torvalds 已提交
3270
	ret = uart_register_driver(&serial8250_reg);
3271
#endif
L
Linus Torvalds 已提交
3272 3273 3274
	if (ret)
		goto out;

3275 3276 3277 3278
	serial8250_isa_devs = platform_device_alloc("serial8250",
						    PLAT8250_DEV_LEGACY);
	if (!serial8250_isa_devs) {
		ret = -ENOMEM;
3279
		goto unreg_uart_drv;
L
Linus Torvalds 已提交
3280 3281
	}

3282 3283 3284 3285
	ret = platform_device_add(serial8250_isa_devs);
	if (ret)
		goto put_dev;

L
Linus Torvalds 已提交
3286 3287
	serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);

3288 3289 3290
	ret = platform_driver_register(&serial8250_isa_driver);
	if (ret == 0)
		goto out;
L
Linus Torvalds 已提交
3291

3292
	platform_device_del(serial8250_isa_devs);
A
Alan Cox 已提交
3293
put_dev:
3294
	platform_device_put(serial8250_isa_devs);
A
Alan Cox 已提交
3295
unreg_uart_drv:
3296 3297 3298
#ifdef CONFIG_SPARC
	sunserial_unregister_minors(&serial8250_reg, UART_NR);
#else
L
Linus Torvalds 已提交
3299
	uart_unregister_driver(&serial8250_reg);
3300
#endif
A
Alan Cox 已提交
3301
out:
L
Linus Torvalds 已提交
3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315
	return ret;
}

static void __exit serial8250_exit(void)
{
	struct platform_device *isa_dev = serial8250_isa_devs;

	/*
	 * This tells serial8250_unregister_port() not to re-register
	 * the ports (thereby making serial8250_isa_driver permanently
	 * in use.)
	 */
	serial8250_isa_devs = NULL;

3316
	platform_driver_unregister(&serial8250_isa_driver);
L
Linus Torvalds 已提交
3317 3318
	platform_device_unregister(isa_dev);

3319 3320 3321
#ifdef CONFIG_SPARC
	sunserial_unregister_minors(&serial8250_reg, UART_NR);
#else
L
Linus Torvalds 已提交
3322
	uart_unregister_driver(&serial8250_reg);
3323
#endif
L
Linus Torvalds 已提交
3324 3325 3326 3327 3328 3329 3330 3331 3332
}

module_init(serial8250_init);
module_exit(serial8250_exit);

EXPORT_SYMBOL(serial8250_suspend_port);
EXPORT_SYMBOL(serial8250_resume_port);

MODULE_LICENSE("GPL");
3333
MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
L
Linus Torvalds 已提交
3334 3335 3336 3337 3338

module_param(share_irqs, uint, 0644);
MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
	" (unsafe)");

3339 3340 3341
module_param(nr_uarts, uint, 0644);
MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");

3342 3343 3344
module_param(skip_txen_test, uint, 0644);
MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");

L
Linus Torvalds 已提交
3345 3346 3347 3348 3349
#ifdef CONFIG_SERIAL_8250_RSA
module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
#endif
MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);