8250.c 86.2 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
	},
242 243 244 245 246
	[PORT_OCTEON] = {
		.name		= "OCTEON",
		.fifo_size	= 64,
		.tx_loadsz	= 64,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
T
Thomas Koeller 已提交
247 248
		.flags		= UART_CAP_FIFO,
	},
249 250 251 252 253 254 255
	[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,
	},
256 257 258 259 260 261 262
	[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,
	},
263 264 265 266 267 268 269 270
	[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,
	},
271 272 273 274 275
	[PORT_XR17D15X] = {
		.name		= "XR17D15X",
		.fifo_size	= 64,
		.tx_loadsz	= 64,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
276 277
		.flags		= UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
				  UART_CAP_SLEEP,
278
	},
279 280 281 282 283 284 285 286 287
	[PORT_XR17V35X] = {
		.name		= "XR17V35X",
		.fifo_size	= 256,
		.tx_loadsz	= 256,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11 |
				  UART_FCR_T_TRIG_11,
		.flags		= UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
				  UART_CAP_SLEEP,
	},
288 289 290 291 292 293 294 295
	[PORT_LPC3220] = {
		.name		= "LPC3220",
		.fifo_size	= 64,
		.tx_loadsz	= 32,
		.fcr		= UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
				  UART_FCR_R_TRIG_00 | UART_FCR_T_TRIG_00,
		.flags		= UART_CAP_FIFO,
	},
296 297 298 299 300 301
	[PORT_BRCM_TRUMANAGE] = {
		.name		= "TruManage",
		.fifo_size	= 1,
		.tx_loadsz	= 1024,
		.flags		= UART_CAP_HFIFO,
	},
S
Sean Young 已提交
302 303
	[PORT_8250_CIR] = {
		.name		= "CIR port"
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
	},
	[PORT_ALTR_16550_F32] = {
		.name		= "Altera 16550 FIFO32",
		.fifo_size	= 32,
		.tx_loadsz	= 32,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
	},
	[PORT_ALTR_16550_F64] = {
		.name		= "Altera 16550 FIFO64",
		.fifo_size	= 64,
		.tx_loadsz	= 64,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
	},
	[PORT_ALTR_16550_F128] = {
		.name		= "Altera 16550 FIFO128",
		.fifo_size	= 128,
		.tx_loadsz	= 128,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
	},
L
Linus Torvalds 已提交
326 327
};

328
/* Uart divisor latch read */
329
static int default_serial_dl_read(struct uart_8250_port *up)
330 331 332 333 334
{
	return serial_in(up, UART_DLL) | serial_in(up, UART_DLM) << 8;
}

/* Uart divisor latch write */
335
static void default_serial_dl_write(struct uart_8250_port *up, int value)
336 337 338 339 340
{
	serial_out(up, UART_DLL, value & 0xff);
	serial_out(up, UART_DLM, value >> 8 & 0xff);
}

341
#if defined(CONFIG_MIPS_ALCHEMY) || defined(CONFIG_SERIAL_8250_RT288X)
342

343
/* Au1x00/RT288x UART hardware has a weird register layout */
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
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,
};

362
static unsigned int au_serial_in(struct uart_port *p, int offset)
363
{
364 365
	offset = au_io_in_map[offset] << p->regshift;
	return __raw_readl(p->membase + offset);
366 367
}

368
static void au_serial_out(struct uart_port *p, int offset, int value)
369
{
370 371 372 373 374 375 376 377 378 379 380 381 382
	offset = au_io_out_map[offset] << p->regshift;
	__raw_writel(value, p->membase + offset);
}

/* Au1x00 haven't got a standard divisor latch */
static int au_serial_dl_read(struct uart_8250_port *up)
{
	return __raw_readl(up->port.membase + 0x28);
}

static void au_serial_dl_write(struct uart_8250_port *up, int value)
{
	__raw_writel(value, up->port.membase + 0x28);
383 384
}

385 386
#endif

387
static unsigned int hub6_serial_in(struct uart_port *p, int offset)
L
Linus Torvalds 已提交
388
{
389
	offset = offset << p->regshift;
390 391 392
	outb(p->hub6 - 1 + offset, p->iobase);
	return inb(p->iobase + 1);
}
L
Linus Torvalds 已提交
393

394 395
static void hub6_serial_out(struct uart_port *p, int offset, int value)
{
396
	offset = offset << p->regshift;
397 398 399
	outb(p->hub6 - 1 + offset, p->iobase);
	outb(value, p->iobase + 1);
}
L
Linus Torvalds 已提交
400

401 402
static unsigned int mem_serial_in(struct uart_port *p, int offset)
{
403
	offset = offset << p->regshift;
404 405
	return readb(p->membase + offset);
}
L
Linus Torvalds 已提交
406

407 408
static void mem_serial_out(struct uart_port *p, int offset, int value)
{
409
	offset = offset << p->regshift;
410 411 412 413 414
	writeb(value, p->membase + offset);
}

static void mem32_serial_out(struct uart_port *p, int offset, int value)
{
415
	offset = offset << p->regshift;
416 417 418 419 420
	writel(value, p->membase + offset);
}

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

425 426
static unsigned int io_serial_in(struct uart_port *p, int offset)
{
427
	offset = offset << p->regshift;
428 429 430 431 432
	return inb(p->iobase + offset);
}

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

437
static int serial8250_default_handle_irq(struct uart_port *port);
438
static int exar_handle_irq(struct uart_port *port);
439

440 441
static void set_io_from_upio(struct uart_port *p)
{
442 443
	struct uart_8250_port *up =
		container_of(p, struct uart_8250_port, port);
444

445 446
	up->dl_read = default_serial_dl_read;
	up->dl_write = default_serial_dl_write;
447

448
	switch (p->iotype) {
L
Linus Torvalds 已提交
449
	case UPIO_HUB6:
450 451
		p->serial_in = hub6_serial_in;
		p->serial_out = hub6_serial_out;
L
Linus Torvalds 已提交
452 453 454
		break;

	case UPIO_MEM:
455 456
		p->serial_in = mem_serial_in;
		p->serial_out = mem_serial_out;
L
Linus Torvalds 已提交
457 458 459
		break;

	case UPIO_MEM32:
460 461
		p->serial_in = mem32_serial_in;
		p->serial_out = mem32_serial_out;
L
Linus Torvalds 已提交
462 463
		break;

464
#if defined(CONFIG_MIPS_ALCHEMY) || defined(CONFIG_SERIAL_8250_RT288X)
465
	case UPIO_AU:
466 467
		p->serial_in = au_serial_in;
		p->serial_out = au_serial_out;
468 469
		up->dl_read = au_serial_dl_read;
		up->dl_write = au_serial_dl_write;
470
		break;
471
#endif
472

L
Linus Torvalds 已提交
473
	default:
474 475 476
		p->serial_in = io_serial_in;
		p->serial_out = io_serial_out;
		break;
L
Linus Torvalds 已提交
477
	}
A
Alan Cox 已提交
478 479
	/* Remember loaded iotype */
	up->cur_iotype = p->iotype;
480
	p->handle_irq = serial8250_default_handle_irq;
L
Linus Torvalds 已提交
481 482
}

A
Alex Williamson 已提交
483
static void
484
serial_port_out_sync(struct uart_port *p, int offset, int value)
A
Alex Williamson 已提交
485
{
486
	switch (p->iotype) {
A
Alex Williamson 已提交
487 488 489
	case UPIO_MEM:
	case UPIO_MEM32:
	case UPIO_AU:
490 491
		p->serial_out(p, offset, value);
		p->serial_in(p, UART_LCR);	/* safe, no side-effects */
A
Alex Williamson 已提交
492 493
		break;
	default:
494
		p->serial_out(p, offset, value);
A
Alex Williamson 已提交
495 496 497
	}
}

L
Linus Torvalds 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
/*
 * 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.
 */
522
static void serial8250_clear_fifos(struct uart_8250_port *p)
L
Linus Torvalds 已提交
523 524
{
	if (p->capabilities & UART_CAP_FIFO) {
525 526
		serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
		serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
L
Linus Torvalds 已提交
527
			       UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
528
		serial_out(p, UART_FCR, 0);
L
Linus Torvalds 已提交
529 530 531
	}
}

532 533 534 535 536 537 538 539 540 541
void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
{
	unsigned char fcr;

	serial8250_clear_fifos(p);
	fcr = uart_config[p->port.type].fcr;
	serial_out(p, UART_FCR, fcr);
}
EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);

L
Linus Torvalds 已提交
542 543 544 545 546
/*
 * 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.
 */
547
static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
L
Linus Torvalds 已提交
548
{
549 550 551 552 553 554 555
	/*
	 * Exar UARTs have a SLEEP register that enables or disables
	 * each UART to enter sleep mode separately.  On the XR17V35x the
	 * register is accessible to each UART at the UART_EXAR_SLEEP
	 * offset but the UART channel may only write to the corresponding
	 * bit.
	 */
556 557
	if ((p->port.type == PORT_XR17V35X) ||
	   (p->port.type == PORT_XR17D15X)) {
558 559 560 561
		serial_out(p, UART_EXAR_SLEEP, 0xff);
		return;
	}

L
Linus Torvalds 已提交
562 563
	if (p->capabilities & UART_CAP_SLEEP) {
		if (p->capabilities & UART_CAP_EFR) {
564 565 566
			serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
			serial_out(p, UART_EFR, UART_EFR_ECB);
			serial_out(p, UART_LCR, 0);
L
Linus Torvalds 已提交
567
		}
568
		serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
L
Linus Torvalds 已提交
569
		if (p->capabilities & UART_CAP_EFR) {
570 571 572
			serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
			serial_out(p, UART_EFR, 0);
			serial_out(p, UART_LCR, 0);
L
Linus Torvalds 已提交
573 574 575 576 577 578 579 580 581 582 583 584 585 586
		}
	}
}

#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;

587
	mode = serial_in(up, UART_RSA_MSR);
L
Linus Torvalds 已提交
588 589 590
	result = mode & UART_RSA_MSR_FIFO;

	if (!result) {
591 592
		serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
		mode = serial_in(up, UART_RSA_MSR);
L
Linus Torvalds 已提交
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
		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)
611
			serial_out(up, UART_RSA_FRR, 0);
L
Linus Torvalds 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
	}
}

/*
 * 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);

630
		mode = serial_in(up, UART_RSA_MSR);
L
Linus Torvalds 已提交
631 632 633
		result = !(mode & UART_RSA_MSR_FIFO);

		if (!result) {
634 635
			serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
			mode = serial_in(up, UART_RSA_MSR);
L
Linus Torvalds 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
			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)
{
652 653
	unsigned char old_fcr, old_mcr, old_lcr;
	unsigned short old_dl;
L
Linus Torvalds 已提交
654 655
	int count;

656 657 658 659 660
	old_lcr = serial_in(up, UART_LCR);
	serial_out(up, UART_LCR, 0);
	old_fcr = serial_in(up, UART_FCR);
	old_mcr = serial_in(up, UART_MCR);
	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
L
Linus Torvalds 已提交
661
		    UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
662 663
	serial_out(up, UART_MCR, UART_MCR_LOOP);
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
664 665
	old_dl = serial_dl_read(up);
	serial_dl_write(up, 0x0001);
666
	serial_out(up, UART_LCR, 0x03);
L
Linus Torvalds 已提交
667
	for (count = 0; count < 256; count++)
668
		serial_out(up, UART_TX, count);
L
Linus Torvalds 已提交
669
	mdelay(20);/* FIXME - schedule_timeout */
670
	for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
L
Linus Torvalds 已提交
671
	     (count < 256); count++)
672 673 674 675
		serial_in(up, UART_RX);
	serial_out(up, UART_FCR, old_fcr);
	serial_out(up, UART_MCR, old_mcr);
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
676
	serial_dl_write(up, old_dl);
677
	serial_out(up, UART_LCR, old_lcr);
L
Linus Torvalds 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690 691

	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;

692 693
	old_lcr = serial_in(p, UART_LCR);
	serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
L
Linus Torvalds 已提交
694

695 696
	old_dll = serial_in(p, UART_DLL);
	old_dlm = serial_in(p, UART_DLM);
L
Linus Torvalds 已提交
697

698 699
	serial_out(p, UART_DLL, 0);
	serial_out(p, UART_DLM, 0);
L
Linus Torvalds 已提交
700

701
	id = serial_in(p, UART_DLL) | serial_in(p, UART_DLM) << 8;
L
Linus Torvalds 已提交
702

703 704 705
	serial_out(p, UART_DLL, old_dll);
	serial_out(p, UART_DLM, old_dlm);
	serial_out(p, UART_LCR, old_lcr);
L
Linus Torvalds 已提交
706 707 708 709 710 711 712 713 714 715 716

	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 已提交
717
 *
L
Linus Torvalds 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
 * 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;
747
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
L
Linus Torvalds 已提交
748 749 750 751 752 753 754 755 756 757 758 759
	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;
760 761 762 763 764 765 766 767

		/*
		 * 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 已提交
768 769
		return;
	}
T
Thomas Koeller 已提交
770

L
Linus Torvalds 已提交
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
	/*
	 * 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);
815
	serial_out(up, UART_SCR, 0xa5);
L
Linus Torvalds 已提交
816
	status1 = serial_in(up, UART_SCR);
817
	serial_out(up, UART_SCR, 0x5a);
L
Linus Torvalds 已提交
818
	status2 = serial_in(up, UART_SCR);
819
	serial_out(up, UART_SCR, scratch);
L
Linus Torvalds 已提交
820 821 822 823 824 825 826 827 828 829

	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
830
	 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html 
L
Linus Torvalds 已提交
831 832 833 834 835 836 837
	 */
	if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
		return 1;

	return 0;
}

838 839 840 841 842 843 844 845 846 847 848 849
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 */
850
		serial_out(up, 0x04, status);
851 852 853 854
	}
	return 1;
}

L
Linus Torvalds 已提交
855 856 857 858 859 860 861 862 863 864 865 866 867 868
/*
 * 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;

869 870 871 872 873 874 875 876
	/*
	 * XR17V35x UARTs have an extra divisor register, DLD
	 * that gets enabled with when DLAB is set which will
	 * cause the device to incorrectly match and assign
	 * port type to PORT_16650.  The EFR for this UART is
	 * found at offset 0x09. Instead check the Deice ID (DVID)
	 * register for a 2, 4 or 8 port UART.
	 */
877 878 879
	if (up->port.flags & UPF_EXAR_EFR) {
		status1 = serial_in(up, UART_EXAR_DVID);
		if (status1 == 0x82 || status1 == 0x84 || status1 == 0x88) {
880 881 882 883 884 885 886 887 888 889
			DEBUG_AUTOCONF("Exar XR17V35x ");
			up->port.type = PORT_XR17V35X;
			up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
						UART_CAP_SLEEP;

			return;
		}

	}

L
Linus Torvalds 已提交
890 891 892 893
	/*
	 * Check for presence of the EFR when DLAB is set.
	 * Only ST16C650V1 UARTs pass this test.
	 */
894
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
L
Linus Torvalds 已提交
895
	if (serial_in(up, UART_EFR) == 0) {
896
		serial_out(up, UART_EFR, 0xA8);
L
Linus Torvalds 已提交
897 898 899 900 901 902 903
		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 ");
		}
904
		serial_out(up, UART_EFR, 0);
L
Linus Torvalds 已提交
905 906 907 908 909 910 911
		return;
	}

	/*
	 * Maybe it requires 0xbf to be written to the LCR.
	 * (other ST16C650V2 UARTs, TI16C752A, etc)
	 */
912
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
L
Linus Torvalds 已提交
913 914 915 916 917 918 919 920 921 922 923 924 925
	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
	 */
926
	serial_out(up, UART_LCR, 0);
L
Linus Torvalds 已提交
927
	status1 = serial_in(up, UART_MCR);
928
	serial_out(up, UART_LCR, 0xE0);
L
Linus Torvalds 已提交
929 930 931
	status2 = serial_in(up, 0x02); /* EXCR1 */

	if (!((status2 ^ status1) & UART_MCR_LOOP)) {
932 933 934
		serial_out(up, UART_LCR, 0);
		serial_out(up, UART_MCR, status1 ^ UART_MCR_LOOP);
		serial_out(up, UART_LCR, 0xE0);
L
Linus Torvalds 已提交
935
		status2 = serial_in(up, 0x02); /* EXCR1 */
936 937
		serial_out(up, UART_LCR, 0);
		serial_out(up, UART_MCR, status1);
L
Linus Torvalds 已提交
938 939

		if ((status2 ^ status1) & UART_MCR_LOOP) {
940 941
			unsigned short quot;

942
			serial_out(up, UART_LCR, 0xE0);
943

944
			quot = serial_dl_read(up);
945 946
			quot <<= 3;

947 948
			if (ns16550a_goto_highspeed(up))
				serial_dl_write(up, quot);
949

950
			serial_out(up, UART_LCR, 0);
L
Linus Torvalds 已提交
951

952
			up->port.uartclk = 921600*16;
L
Linus Torvalds 已提交
953 954 955 956 957 958 959 960 961 962 963 964
			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.
	 */
965 966
	serial_out(up, UART_LCR, 0);
	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
L
Linus Torvalds 已提交
967
	status1 = serial_in(up, UART_IIR) >> 5;
968 969 970
	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
L
Linus Torvalds 已提交
971
	status2 = serial_in(up, UART_IIR) >> 5;
972 973
	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
	serial_out(up, UART_LCR, 0);
L
Linus Torvalds 已提交
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991

	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);
992
	serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
L
Linus Torvalds 已提交
993 994 995 996 997
	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.
		 */
998
		serial_out(up, UART_IER, iersave | UART_IER_UUE);
L
Linus Torvalds 已提交
999 1000 1001 1002 1003 1004 1005
		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;
1006
			up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
L
Linus Torvalds 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015
			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 ");
	}
1016
	serial_out(up, UART_IER, iersave);
1017

1018 1019 1020 1021
	/*
	 * Exar uarts have EFR in a weird location
	 */
	if (up->port.flags & UPF_EXAR_EFR) {
1022
		DEBUG_AUTOCONF("Exar XR17D15x ");
1023
		up->port.type = PORT_XR17D15X;
1024 1025 1026 1027
		up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
				    UART_CAP_SLEEP;

		return;
1028 1029
	}

1030 1031 1032 1033 1034 1035 1036 1037
	/*
	 * 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 已提交
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
}

/*
 * 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;
1051
	struct uart_port *port = &up->port;
L
Linus Torvalds 已提交
1052
	unsigned long flags;
1053
	unsigned int old_capabilities;
L
Linus Torvalds 已提交
1054

1055
	if (!port->iobase && !port->mapbase && !port->membase)
L
Linus Torvalds 已提交
1056 1057
		return;

1058
	DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
1059
		       serial_index(port), port->iobase, port->membase);
L
Linus Torvalds 已提交
1060 1061 1062 1063 1064

	/*
	 * We really do need global IRQs disabled here - we're going to
	 * be frobbing the chips IRQ enable register to see if it exists.
	 */
1065
	spin_lock_irqsave(&port->lock, flags);
L
Linus Torvalds 已提交
1066 1067

	up->capabilities = 0;
1068
	up->bugs = 0;
L
Linus Torvalds 已提交
1069

1070
	if (!(port->flags & UPF_BUGGY_UART)) {
L
Linus Torvalds 已提交
1071 1072 1073
		/*
		 * Do a simple existence test first; if we fail this,
		 * there's no point trying anything else.
T
Thomas Koeller 已提交
1074
		 *
L
Linus Torvalds 已提交
1075 1076 1077 1078 1079 1080 1081 1082 1083
		 * 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.
		 */
1084 1085
		scratch = serial_in(up, UART_IER);
		serial_out(up, UART_IER, 0);
L
Linus Torvalds 已提交
1086 1087 1088
#ifdef __i386__
		outb(0xff, 0x080);
#endif
1089 1090 1091 1092
		/*
		 * 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.
		 */
1093 1094
		scratch2 = serial_in(up, UART_IER) & 0x0f;
		serial_out(up, UART_IER, 0x0F);
L
Linus Torvalds 已提交
1095 1096 1097
#ifdef __i386__
		outb(0, 0x080);
#endif
1098 1099
		scratch3 = serial_in(up, UART_IER) & 0x0f;
		serial_out(up, UART_IER, scratch);
L
Linus Torvalds 已提交
1100 1101 1102 1103
		if (scratch2 != 0 || scratch3 != 0x0F) {
			/*
			 * We failed; there's nothing here
			 */
1104
			spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113
			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 已提交
1114
	/*
L
Linus Torvalds 已提交
1115 1116 1117 1118 1119 1120 1121 1122
	 * 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!
	 */
1123
	if (!(port->flags & UPF_SKIP_TEST)) {
1124 1125 1126
		serial_out(up, UART_MCR, UART_MCR_LOOP | 0x0A);
		status1 = serial_in(up, UART_MSR) & 0xF0;
		serial_out(up, UART_MCR, save_mcr);
L
Linus Torvalds 已提交
1127
		if (status1 != 0x90) {
1128
			spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
1129 1130 1131 1132 1133 1134 1135 1136 1137
			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 已提交
1138
	 * out if it's 8250 or 16450, 16550, 16550A or later.  This
L
Linus Torvalds 已提交
1139 1140 1141 1142 1143
	 * 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.
	 */
1144 1145 1146
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
	serial_out(up, UART_EFR, 0);
	serial_out(up, UART_LCR, 0);
L
Linus Torvalds 已提交
1147

1148
	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
L
Linus Torvalds 已提交
1149 1150 1151 1152 1153 1154 1155
	scratch = serial_in(up, UART_IIR) >> 6;

	switch (scratch) {
	case 0:
		autoconfig_8250(up);
		break;
	case 1:
1156
		port->type = PORT_UNKNOWN;
L
Linus Torvalds 已提交
1157 1158
		break;
	case 2:
1159
		port->type = PORT_16550;
L
Linus Torvalds 已提交
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
		break;
	case 3:
		autoconfig_16550a(up);
		break;
	}

#ifdef CONFIG_SERIAL_8250_RSA
	/*
	 * Only probe for RSA ports if we got the region.
	 */
1170
	if (port->type == PORT_16550A && probeflags & PROBE_RSA) {
L
Linus Torvalds 已提交
1171 1172 1173
		int i;

		for (i = 0 ; i < probe_rsa_count; ++i) {
1174 1175
			if (probe_rsa[i] == port->iobase && __enable_rsa(up)) {
				port->type = PORT_RSA;
L
Linus Torvalds 已提交
1176 1177 1178 1179 1180
				break;
			}
		}
	}
#endif
1181

1182
	serial_out(up, UART_LCR, save_lcr);
L
Linus Torvalds 已提交
1183

1184
	port->fifosize = uart_config[up->port.type].fifo_size;
1185
	old_capabilities = up->capabilities; 
1186 1187
	up->capabilities = uart_config[port->type].flags;
	up->tx_loadsz = uart_config[port->type].tx_loadsz;
L
Linus Torvalds 已提交
1188

1189
	if (port->type == PORT_UNKNOWN)
1190
		goto out_lock;
L
Linus Torvalds 已提交
1191 1192 1193 1194 1195

	/*
	 * Reset the UART.
	 */
#ifdef CONFIG_SERIAL_8250_RSA
1196
	if (port->type == PORT_RSA)
1197
		serial_out(up, UART_RSA_FRR, 0);
L
Linus Torvalds 已提交
1198
#endif
1199
	serial_out(up, UART_MCR, save_mcr);
L
Linus Torvalds 已提交
1200
	serial8250_clear_fifos(up);
A
Alex Williamson 已提交
1201
	serial_in(up, UART_RX);
1202
	if (up->capabilities & UART_CAP_UUE)
1203
		serial_out(up, UART_IER, UART_IER_UUE);
1204
	else
1205
		serial_out(up, UART_IER, 0);
L
Linus Torvalds 已提交
1206

1207
out_lock:
1208
	spin_unlock_irqrestore(&port->lock, flags);
1209 1210 1211 1212 1213 1214 1215 1216
	if (up->capabilities != old_capabilities) {
		printk(KERN_WARNING
		       "ttyS%d: detected caps %08x should be %08x\n",
		       serial_index(port), old_capabilities,
		       up->capabilities);
	}
out:
	DEBUG_AUTOCONF("iir=%d ", scratch);
1217
	DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
L
Linus Torvalds 已提交
1218 1219 1220 1221
}

static void autoconfig_irq(struct uart_8250_port *up)
{
1222
	struct uart_port *port = &up->port;
L
Linus Torvalds 已提交
1223 1224 1225 1226 1227 1228
	unsigned char save_mcr, save_ier;
	unsigned char save_ICP = 0;
	unsigned int ICP = 0;
	unsigned long irqs;
	int irq;

1229 1230
	if (port->flags & UPF_FOURPORT) {
		ICP = (port->iobase & 0xfe0) | 0x1f;
L
Linus Torvalds 已提交
1231 1232
		save_ICP = inb_p(ICP);
		outb_p(0x80, ICP);
1233
		inb_p(ICP);
L
Linus Torvalds 已提交
1234 1235 1236 1237
	}

	/* forget possible initially masked and pending IRQ */
	probe_irq_off(probe_irq_on());
1238 1239 1240
	save_mcr = serial_in(up, UART_MCR);
	save_ier = serial_in(up, UART_IER);
	serial_out(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
T
Thomas Koeller 已提交
1241

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

1261 1262
	serial_out(up, UART_MCR, save_mcr);
	serial_out(up, UART_IER, save_ier);
L
Linus Torvalds 已提交
1263

1264
	if (port->flags & UPF_FOURPORT)
L
Linus Torvalds 已提交
1265 1266
		outb_p(save_ICP, ICP);

1267
	port->irq = (irq > 0) ? irq : 0;
L
Linus Torvalds 已提交
1268 1269
}

1270 1271 1272 1273 1274 1275 1276 1277
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);
	}
}

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

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

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

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

1299 1300 1301
	if (up->dma && !serial8250_tx_dma(up)) {
		return;
	} else if (!(up->ier & UART_IER_THRI)) {
L
Linus Torvalds 已提交
1302
		up->ier |= UART_IER_THRI;
1303
		serial_port_out(port, UART_IER, up->ier);
1304

1305
		if (up->bugs & UART_BUG_TXEN) {
1306
			unsigned char lsr;
1307
			lsr = serial_in(up, UART_LSR);
1308
			up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1309
			if (lsr & UART_LSR_TEMT)
1310
				serial8250_tx_chars(up);
1311
		}
L
Linus Torvalds 已提交
1312
	}
1313

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

static void serial8250_stop_rx(struct uart_port *port)
{
1325 1326
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
1327 1328 1329

	up->ier &= ~UART_IER_RLSI;
	up->port.read_status_mask &= ~UART_LSR_DR;
1330
	serial_port_out(port, UART_IER, up->ier);
L
Linus Torvalds 已提交
1331 1332 1333 1334
}

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

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

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

1346
/*
1347
 * serial8250_rx_chars: processes according to the passed in LSR
1348 1349 1350
 * value, and returns the remaining LSR bits not handled
 * by this Rx routine.
 */
1351 1352
unsigned char
serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
L
Linus Torvalds 已提交
1353
{
1354
	struct uart_port *port = &up->port;
1355
	unsigned char ch;
L
Linus Torvalds 已提交
1356 1357 1358 1359
	int max_count = 256;
	char flag;

	do {
1360
		if (likely(lsr & UART_LSR_DR))
1361
			ch = serial_in(up, UART_RX);
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
		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 已提交
1372
		flag = TTY_NORMAL;
1373
		port->icount.rx++;
L
Linus Torvalds 已提交
1374

1375 1376
		lsr |= up->lsr_saved_flags;
		up->lsr_saved_flags = 0;
L
Linus Torvalds 已提交
1377

1378
		if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
L
Linus Torvalds 已提交
1379 1380
			if (lsr & UART_LSR_BI) {
				lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1381
				port->icount.brk++;
L
Linus Torvalds 已提交
1382 1383 1384 1385 1386 1387
				/*
				 * We do the SysRQ and SAK checking
				 * here because otherwise the break
				 * may get masked by ignore_status_mask
				 * or read_status_mask.
				 */
1388
				if (uart_handle_break(port))
L
Linus Torvalds 已提交
1389 1390
					goto ignore_char;
			} else if (lsr & UART_LSR_PE)
1391
				port->icount.parity++;
L
Linus Torvalds 已提交
1392
			else if (lsr & UART_LSR_FE)
1393
				port->icount.frame++;
L
Linus Torvalds 已提交
1394
			if (lsr & UART_LSR_OE)
1395
				port->icount.overrun++;
L
Linus Torvalds 已提交
1396 1397

			/*
1398
			 * Mask off conditions which should be ignored.
L
Linus Torvalds 已提交
1399
			 */
1400
			lsr &= port->read_status_mask;
L
Linus Torvalds 已提交
1401 1402 1403 1404 1405 1406 1407 1408 1409

			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;
		}
1410
		if (uart_handle_sysrq_char(port, ch))
L
Linus Torvalds 已提交
1411
			goto ignore_char;
1412

1413
		uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
1414

A
Alan Cox 已提交
1415
ignore_char:
1416
		lsr = serial_in(up, UART_LSR);
1417
	} while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
1418
	spin_unlock(&port->lock);
J
Jiri Slaby 已提交
1419
	tty_flip_buffer_push(&port->state->port);
1420
	spin_lock(&port->lock);
1421
	return lsr;
L
Linus Torvalds 已提交
1422
}
1423
EXPORT_SYMBOL_GPL(serial8250_rx_chars);
L
Linus Torvalds 已提交
1424

1425
void serial8250_tx_chars(struct uart_8250_port *up)
L
Linus Torvalds 已提交
1426
{
1427 1428
	struct uart_port *port = &up->port;
	struct circ_buf *xmit = &port->state->xmit;
L
Linus Torvalds 已提交
1429 1430
	int count;

1431 1432 1433 1434
	if (port->x_char) {
		serial_out(up, UART_TX, port->x_char);
		port->icount.tx++;
		port->x_char = 0;
L
Linus Torvalds 已提交
1435 1436
		return;
	}
1437 1438
	if (uart_tx_stopped(port)) {
		serial8250_stop_tx(port);
1439 1440 1441
		return;
	}
	if (uart_circ_empty(xmit)) {
1442
		__stop_tx(up);
L
Linus Torvalds 已提交
1443 1444 1445 1446 1447 1448 1449
		return;
	}

	count = up->tx_loadsz;
	do {
		serial_out(up, UART_TX, xmit->buf[xmit->tail]);
		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1450
		port->icount.tx++;
L
Linus Torvalds 已提交
1451 1452
		if (uart_circ_empty(xmit))
			break;
1453 1454 1455 1456 1457
		if (up->capabilities & UART_CAP_HFIFO) {
			if ((serial_port_in(port, UART_LSR) & BOTH_EMPTY) !=
			    BOTH_EMPTY)
				break;
		}
L
Linus Torvalds 已提交
1458 1459 1460
	} while (--count > 0);

	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1461
		uart_write_wakeup(port);
L
Linus Torvalds 已提交
1462 1463 1464 1465

	DEBUG_INTR("THRE...");

	if (uart_circ_empty(xmit))
1466
		__stop_tx(up);
L
Linus Torvalds 已提交
1467
}
1468
EXPORT_SYMBOL_GPL(serial8250_tx_chars);
L
Linus Torvalds 已提交
1469

1470
unsigned int serial8250_modem_status(struct uart_8250_port *up)
L
Linus Torvalds 已提交
1471
{
1472
	struct uart_port *port = &up->port;
1473 1474
	unsigned int status = serial_in(up, UART_MSR);

1475 1476
	status |= up->msr_saved_flags;
	up->msr_saved_flags = 0;
1477
	if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1478
	    port->state != NULL) {
1479
		if (status & UART_MSR_TERI)
1480
			port->icount.rng++;
1481
		if (status & UART_MSR_DDSR)
1482
			port->icount.dsr++;
1483
		if (status & UART_MSR_DDCD)
1484
			uart_handle_dcd_change(port, status & UART_MSR_DCD);
1485
		if (status & UART_MSR_DCTS)
1486
			uart_handle_cts_change(port, status & UART_MSR_CTS);
1487

1488
		wake_up_interruptible(&port->state->port.delta_msr_wait);
1489
	}
L
Linus Torvalds 已提交
1490

1491
	return status;
L
Linus Torvalds 已提交
1492
}
1493
EXPORT_SYMBOL_GPL(serial8250_modem_status);
L
Linus Torvalds 已提交
1494 1495 1496 1497

/*
 * This handles the interrupt from one port.
 */
1498
int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
L
Linus Torvalds 已提交
1499
{
1500
	unsigned char status;
1501
	unsigned long flags;
1502 1503
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
1504
	int dma_err = 0;
1505 1506 1507

	if (iir & UART_IIR_NO_INT)
		return 0;
1508

1509
	spin_lock_irqsave(&port->lock, flags);
1510

1511
	status = serial_port_in(port, UART_LSR);
L
Linus Torvalds 已提交
1512 1513 1514

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

1515 1516 1517 1518 1519 1520 1521
	if (status & (UART_LSR_DR | UART_LSR_BI)) {
		if (up->dma)
			dma_err = serial8250_rx_dma(up, iir);

		if (!up->dma || dma_err)
			status = serial8250_rx_chars(up, status);
	}
1522
	serial8250_modem_status(up);
L
Linus Torvalds 已提交
1523
	if (status & UART_LSR_THRE)
1524
		serial8250_tx_chars(up);
1525

1526
	spin_unlock_irqrestore(&port->lock, flags);
1527
	return 1;
1528
}
1529
EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1530 1531 1532

static int serial8250_default_handle_irq(struct uart_port *port)
{
1533
	unsigned int iir = serial_port_in(port, UART_IIR);
1534 1535 1536 1537

	return serial8250_handle_irq(port, iir);
}

1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
/*
 * These Exar UARTs have an extra interrupt indicator that could
 * fire for a few unimplemented interrupts.  One of which is a
 * wakeup event when coming out of sleep.  Put this here just
 * to be on the safe side that these interrupts don't go unhandled.
 */
static int exar_handle_irq(struct uart_port *port)
{
	unsigned char int0, int1, int2, int3;
	unsigned int iir = serial_port_in(port, UART_IIR);
	int ret;

	ret = serial8250_handle_irq(port, iir);

1552 1553
	if ((port->type == PORT_XR17V35X) ||
	   (port->type == PORT_XR17D15X)) {
1554 1555 1556 1557 1558 1559 1560 1561 1562
		int0 = serial_port_in(port, 0x80);
		int1 = serial_port_in(port, 0x81);
		int2 = serial_port_in(port, 0x82);
		int3 = serial_port_in(port, 0x83);
	}

	return ret;
}

L
Linus Torvalds 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
/*
 * 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.
 */
1577
static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
{
	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;
1590
		struct uart_port *port;
L
Linus Torvalds 已提交
1591 1592

		up = list_entry(l, struct uart_8250_port, list);
1593
		port = &up->port;
L
Linus Torvalds 已提交
1594

1595
		if (port->handle_irq(port)) {
L
Linus Torvalds 已提交
1596 1597 1598 1599 1600 1601 1602 1603 1604
			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. */
1605 1606
			printk_ratelimited(KERN_ERR
				"serial8250: too much work for irq%d\n", irq);
L
Linus Torvalds 已提交
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 1636 1637
			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 已提交
1638 1639 1640 1641 1642
	/* List empty so throw away the hash node */
	if (i->head == NULL) {
		hlist_del(&i->node);
		kfree(i);
	}
L
Linus Torvalds 已提交
1643 1644 1645 1646
}

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

A
Alan Cox 已提交
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
	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 已提交
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
	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);
1685
		irq_flags |= up->port.irqflags;
L
Linus Torvalds 已提交
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
		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 已提交
1697 1698 1699
	struct irq_info *i;
	struct hlist_node *n;
	struct hlist_head *h;
L
Linus Torvalds 已提交
1700

A
Alan Cox 已提交
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
	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 已提交
1712 1713 1714 1715 1716 1717
	BUG_ON(i->head == NULL);

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

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

/*
 * 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;

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

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

1741 1742
	spin_lock_irqsave(&up->port.lock, flags);

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

A
Alex Williamson 已提交
1752 1753 1754 1755 1756 1757 1758 1759
	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.
	 */
1760 1761
	lsr = serial_in(up, UART_LSR);
	up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
A
Alex Williamson 已提交
1762
	if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
A
Alan Cox 已提交
1763
	    (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
1764
	    (lsr & UART_LSR_THRE)) {
A
Alex Williamson 已提交
1765 1766 1767 1768 1769
		iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
		iir |= UART_IIR_THRI;
	}

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

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

1775 1776
	spin_unlock_irqrestore(&up->port.lock, flags);

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

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

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

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

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

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

	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)
{
1820 1821
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
	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;

1837
	serial_port_out(port, UART_MCR, mcr);
L
Linus Torvalds 已提交
1838 1839 1840 1841
}

static void serial8250_break_ctl(struct uart_port *port, int break_state)
{
1842 1843
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
1844 1845
	unsigned long flags;

1846
	spin_lock_irqsave(&port->lock, flags);
L
Linus Torvalds 已提交
1847 1848 1849 1850
	if (break_state == -1)
		up->lcr |= UART_LCR_SBC;
	else
		up->lcr &= ~UART_LCR_SBC;
1851
	serial_port_out(port, UART_LCR, up->lcr);
1852
	spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
1853 1854
}

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

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

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

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

	/* Wait up to 1s for flow control if necessary */
	if (up->port.flags & UPF_CONS_FLOW) {
1877 1878 1879 1880 1881 1882
		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 已提交
1883 1884 1885 1886 1887 1888
			udelay(1);
			touch_nmi_watchdog();
		}
	}
}

J
Jason Wessel 已提交
1889 1890 1891 1892 1893 1894 1895 1896
#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)
{
1897
	unsigned char lsr = serial_port_in(port, UART_LSR);
J
Jason Wessel 已提交
1898

1899 1900
	if (!(lsr & UART_LSR_DR))
		return NO_POLL_CHAR;
J
Jason Wessel 已提交
1901

1902
	return serial_port_in(port, UART_RX);
J
Jason Wessel 已提交
1903 1904 1905 1906 1907 1908 1909
}


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

	/*
	 *	First save the IER then disable the interrupts
	 */
1916
	ier = serial_port_in(port, UART_IER);
J
Jason Wessel 已提交
1917
	if (up->capabilities & UART_CAP_UUE)
1918
		serial_port_out(port, UART_IER, UART_IER_UUE);
J
Jason Wessel 已提交
1919
	else
1920
		serial_port_out(port, UART_IER, 0);
J
Jason Wessel 已提交
1921 1922 1923 1924 1925 1926

	wait_for_xmitr(up, BOTH_EMPTY);
	/*
	 *	Send the character out.
	 *	If a LF, also do CR...
	 */
1927
	serial_port_out(port, UART_TX, c);
J
Jason Wessel 已提交
1928 1929
	if (c == 10) {
		wait_for_xmitr(up, BOTH_EMPTY);
1930
		serial_port_out(port, UART_TX, 13);
J
Jason Wessel 已提交
1931 1932 1933 1934 1935 1936 1937
	}

	/*
	 *	Finally, wait for transmitter to become empty
	 *	and restore the IER
	 */
	wait_for_xmitr(up, BOTH_EMPTY);
1938
	serial_port_out(port, UART_IER, ier);
J
Jason Wessel 已提交
1939 1940 1941 1942
}

#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;

S
Sean Young 已提交
1951 1952 1953
	if (port->type == PORT_8250_CIR)
		return -ENODEV;

1954 1955 1956 1957 1958 1959
	if (!port->fifosize)
		port->fifosize = uart_config[port->type].fifo_size;
	if (!up->tx_loadsz)
		up->tx_loadsz = uart_config[port->type].tx_loadsz;
	if (!up->capabilities)
		up->capabilities = uart_config[port->type].flags;
L
Linus Torvalds 已提交
1960 1961
	up->mcr = 0;

1962
	if (port->iotype != up->cur_iotype)
A
Alan Cox 已提交
1963 1964
		set_io_from_upio(port);

1965
	if (port->type == PORT_16C950) {
L
Linus Torvalds 已提交
1966 1967
		/* Wake up and initialize UART */
		up->acr = 0;
1968 1969 1970 1971
		serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
		serial_port_out(port, UART_EFR, UART_EFR_ECB);
		serial_port_out(port, UART_IER, 0);
		serial_port_out(port, UART_LCR, 0);
L
Linus Torvalds 已提交
1972
		serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
1973 1974 1975
		serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
		serial_port_out(port, UART_EFR, UART_EFR_ECB);
		serial_port_out(port, UART_LCR, 0);
L
Linus Torvalds 已提交
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
	}

#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 已提交
1988
	 * (they will be reenabled in set_termios())
L
Linus Torvalds 已提交
1989 1990 1991 1992 1993 1994
	 */
	serial8250_clear_fifos(up);

	/*
	 * Clear the interrupt registers.
	 */
1995 1996 1997 1998
	serial_port_in(port, UART_LSR);
	serial_port_in(port, UART_RX);
	serial_port_in(port, UART_IIR);
	serial_port_in(port, UART_MSR);
L
Linus Torvalds 已提交
1999 2000 2001 2002 2003 2004

	/*
	 * 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.
	 */
2005
	if (!(port->flags & UPF_BUGGY_UART) &&
2006
	    (serial_port_in(port, UART_LSR) == 0xff)) {
2007
		printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2008
				   serial_index(port));
L
Linus Torvalds 已提交
2009 2010 2011 2012 2013 2014
		return -ENODEV;
	}

	/*
	 * For a XR16C850, we need to set the trigger levels
	 */
2015
	if (port->type == PORT_16850) {
L
Linus Torvalds 已提交
2016 2017
		unsigned char fctr;

2018
		serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
L
Linus Torvalds 已提交
2019

2020
		fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2021 2022 2023 2024 2025 2026
		serial_port_out(port, UART_FCTR,
				fctr | UART_FCTR_TRGD | UART_FCTR_RX);
		serial_port_out(port, UART_TRG, UART_TRG_96);
		serial_port_out(port, UART_FCTR,
				fctr | UART_FCTR_TRGD | UART_FCTR_TX);
		serial_port_out(port, UART_TRG, UART_TRG_96);
L
Linus Torvalds 已提交
2027

2028
		serial_port_out(port, UART_LCR, 0);
L
Linus Torvalds 已提交
2029 2030
	}

2031
	if (port->irq) {
2032
		unsigned char iir1;
A
Alex Williamson 已提交
2033 2034 2035 2036 2037 2038 2039 2040
		/*
		 * 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.
		 */
2041
		spin_lock_irqsave(&port->lock, flags);
2042
		if (up->port.irqflags & IRQF_SHARED)
2043
			disable_irq_nosync(port->irq);
A
Alex Williamson 已提交
2044 2045

		wait_for_xmitr(up, UART_LSR_THRE);
2046
		serial_port_out_sync(port, UART_IER, UART_IER_THRI);
A
Alex Williamson 已提交
2047
		udelay(1); /* allow THRE to set */
2048 2049
		iir1 = serial_port_in(port, UART_IIR);
		serial_port_out(port, UART_IER, 0);
2050
		serial_port_out_sync(port, UART_IER, UART_IER_THRI);
A
Alex Williamson 已提交
2051
		udelay(1); /* allow a working UART time to re-assert THRE */
2052 2053
		iir = serial_port_in(port, UART_IIR);
		serial_port_out(port, UART_IER, 0);
A
Alex Williamson 已提交
2054

2055 2056 2057
		if (port->irqflags & IRQF_SHARED)
			enable_irq(port->irq);
		spin_unlock_irqrestore(&port->lock, flags);
A
Alex Williamson 已提交
2058 2059

		/*
2060 2061 2062
		 * If the interrupt is not reasserted, or we otherwise
		 * don't trust the iir, setup a timer to kick the UART
		 * on a regular basis.
A
Alex Williamson 已提交
2063
		 */
2064 2065
		if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
		    up->port.flags & UPF_BUG_THRE) {
2066
			up->bugs |= UART_BUG_THRE;
2067 2068
			pr_debug("ttyS%d - using backup timer\n",
				 serial_index(port));
A
Alex Williamson 已提交
2069 2070 2071
		}
	}

2072 2073 2074 2075 2076 2077 2078 2079
	/*
	 * 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 +
2080
			uart_poll_timeout(port) + HZ / 5);
2081 2082
	}

L
Linus Torvalds 已提交
2083 2084 2085 2086 2087
	/*
	 * 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.
	 */
2088
	if (!port->irq) {
L
Linus Torvalds 已提交
2089
		up->timer.data = (unsigned long)up;
2090
		mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
L
Linus Torvalds 已提交
2091 2092 2093 2094 2095 2096 2097 2098 2099
	} else {
		retval = serial_link_irq_chain(up);
		if (retval)
			return retval;
	}

	/*
	 * Now, initialize the UART
	 */
2100
	serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
L
Linus Torvalds 已提交
2101

2102
	spin_lock_irqsave(&port->lock, flags);
L
Linus Torvalds 已提交
2103
	if (up->port.flags & UPF_FOURPORT) {
A
Alan Cox 已提交
2104
		if (!up->port.irq)
L
Linus Torvalds 已提交
2105 2106 2107 2108 2109
			up->port.mctrl |= TIOCM_OUT1;
	} else
		/*
		 * Most PC uarts need OUT2 raised to enable interrupts.
		 */
2110
		if (port->irq)
L
Linus Torvalds 已提交
2111 2112
			up->port.mctrl |= TIOCM_OUT2;

2113
	serial8250_set_mctrl(port, port->mctrl);
2114

2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125
	/* 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.
	 */
2126
	if (skip_txen_test || up->port.flags & UPF_NO_TXEN_TEST)
2127 2128
		goto dont_test_tx_en;

2129 2130 2131 2132
	/*
	 * Do a quick test to see if we receive an
	 * interrupt when we enable the TX irq.
	 */
2133 2134 2135 2136
	serial_port_out(port, UART_IER, UART_IER_THRI);
	lsr = serial_port_in(port, UART_LSR);
	iir = serial_port_in(port, UART_IIR);
	serial_port_out(port, UART_IER, 0);
2137 2138

	if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2139 2140
		if (!(up->bugs & UART_BUG_TXEN)) {
			up->bugs |= UART_BUG_TXEN;
2141
			pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2142
				 serial_index(port));
2143 2144
		}
	} else {
2145
		up->bugs &= ~UART_BUG_TXEN;
2146 2147
	}

2148
dont_test_tx_en:
2149
	spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
2150

2151 2152 2153 2154 2155
	/*
	 * Clear the interrupt registers again for luck, and clear the
	 * saved flags to avoid getting false values from polling
	 * routines or the previous session.
	 */
2156 2157 2158 2159
	serial_port_in(port, UART_LSR);
	serial_port_in(port, UART_RX);
	serial_port_in(port, UART_IIR);
	serial_port_in(port, UART_MSR);
2160 2161 2162
	up->lsr_saved_flags = 0;
	up->msr_saved_flags = 0;

2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
	/*
	 * Request DMA channels for both RX and TX.
	 */
	if (up->dma) {
		retval = serial8250_request_dma(up);
		if (retval) {
			pr_warn_ratelimited("ttyS%d - failed to request DMA\n",
					    serial_index(port));
			up->dma = NULL;
		}
	}

L
Linus Torvalds 已提交
2175 2176 2177 2178 2179 2180
	/*
	 * 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;
2181
	serial_port_out(port, UART_IER, up->ier);
L
Linus Torvalds 已提交
2182

2183
	if (port->flags & UPF_FOURPORT) {
L
Linus Torvalds 已提交
2184 2185 2186 2187
		unsigned int icp;
		/*
		 * Enable interrupts on the AST Fourport board
		 */
2188
		icp = (port->iobase & 0xfe0) | 0x01f;
L
Linus Torvalds 已提交
2189
		outb_p(0x80, icp);
2190
		inb_p(icp);
L
Linus Torvalds 已提交
2191 2192 2193 2194 2195 2196 2197
	}

	return 0;
}

static void serial8250_shutdown(struct uart_port *port)
{
2198 2199
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
2200 2201 2202 2203 2204 2205
	unsigned long flags;

	/*
	 * Disable interrupts from this port
	 */
	up->ier = 0;
2206
	serial_port_out(port, UART_IER, 0);
L
Linus Torvalds 已提交
2207

2208 2209 2210
	if (up->dma)
		serial8250_release_dma(up);

2211 2212
	spin_lock_irqsave(&port->lock, flags);
	if (port->flags & UPF_FOURPORT) {
L
Linus Torvalds 已提交
2213
		/* reset interrupts on the AST Fourport board */
2214 2215
		inb((port->iobase & 0xfe0) | 0x1f);
		port->mctrl |= TIOCM_OUT1;
L
Linus Torvalds 已提交
2216
	} else
2217
		port->mctrl &= ~TIOCM_OUT2;
L
Linus Torvalds 已提交
2218

2219 2220
	serial8250_set_mctrl(port, port->mctrl);
	spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
2221 2222 2223 2224

	/*
	 * Disable break condition and FIFOs
	 */
2225 2226
	serial_port_out(port, UART_LCR,
			serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
L
Linus Torvalds 已提交
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
	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.
	 */
2240
	serial_port_in(port, UART_RX);
L
Linus Torvalds 已提交
2241

A
Alex Williamson 已提交
2242 2243
	del_timer_sync(&up->timer);
	up->timer.function = serial8250_timeout;
2244
	if (port->irq)
L
Linus Torvalds 已提交
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
		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;
}

2268 2269 2270
void
serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
		          struct ktermios *old)
L
Linus Torvalds 已提交
2271
{
2272 2273
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
2274 2275 2276
	unsigned char cval, fcr = 0;
	unsigned long flags;
	unsigned int baud, quot;
2277
	int fifo_bug = 0;
L
Linus Torvalds 已提交
2278 2279 2280

	switch (termios->c_cflag & CSIZE) {
	case CS5:
2281
		cval = UART_LCR_WLEN5;
L
Linus Torvalds 已提交
2282 2283
		break;
	case CS6:
2284
		cval = UART_LCR_WLEN6;
L
Linus Torvalds 已提交
2285 2286
		break;
	case CS7:
2287
		cval = UART_LCR_WLEN7;
L
Linus Torvalds 已提交
2288 2289 2290
		break;
	default:
	case CS8:
2291
		cval = UART_LCR_WLEN8;
L
Linus Torvalds 已提交
2292 2293 2294 2295
		break;
	}

	if (termios->c_cflag & CSTOPB)
2296
		cval |= UART_LCR_STOP;
2297
	if (termios->c_cflag & PARENB) {
L
Linus Torvalds 已提交
2298
		cval |= UART_LCR_PARITY;
2299 2300 2301
		if (up->bugs & UART_BUG_PARITY)
			fifo_bug = 1;
	}
L
Linus Torvalds 已提交
2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
	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.
	 */
2312 2313 2314
	baud = uart_get_baud_rate(port, termios, old,
				  port->uartclk / 16 / 0xffff,
				  port->uartclk / 16);
L
Linus Torvalds 已提交
2315 2316 2317
	quot = serial8250_get_divisor(port, baud);

	/*
2318
	 * Oxford Semi 952 rev B workaround
L
Linus Torvalds 已提交
2319
	 */
2320
	if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
A
Alan Cox 已提交
2321
		quot++;
L
Linus Torvalds 已提交
2322

2323
	if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
2324
		fcr = uart_config[port->type].fcr;
2325
		if (baud < 2400 || fifo_bug) {
2326 2327 2328
			fcr &= ~UART_FCR_TRIGGER_MASK;
			fcr |= UART_FCR_TRIGGER_1;
		}
L
Linus Torvalds 已提交
2329 2330 2331 2332 2333 2334 2335 2336 2337 2338
	}

	/*
	 * 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.
	 */
2339
	if (up->capabilities & UART_CAP_AFE && port->fifosize >= 32) {
L
Linus Torvalds 已提交
2340 2341 2342 2343 2344 2345 2346 2347 2348
		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.
	 */
2349
	spin_lock_irqsave(&port->lock, flags);
L
Linus Torvalds 已提交
2350 2351 2352 2353 2354 2355

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

2356
	port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
L
Linus Torvalds 已提交
2357
	if (termios->c_iflag & INPCK)
2358
		port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
L
Linus Torvalds 已提交
2359
	if (termios->c_iflag & (BRKINT | PARMRK))
2360
		port->read_status_mask |= UART_LSR_BI;
L
Linus Torvalds 已提交
2361 2362 2363 2364

	/*
	 * Characteres to ignore
	 */
2365
	port->ignore_status_mask = 0;
L
Linus Torvalds 已提交
2366
	if (termios->c_iflag & IGNPAR)
2367
		port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
L
Linus Torvalds 已提交
2368
	if (termios->c_iflag & IGNBRK) {
2369
		port->ignore_status_mask |= UART_LSR_BI;
L
Linus Torvalds 已提交
2370 2371 2372 2373 2374
		/*
		 * If we're ignoring parity and break indicators,
		 * ignore overruns too (for real raw support).
		 */
		if (termios->c_iflag & IGNPAR)
2375
			port->ignore_status_mask |= UART_LSR_OE;
L
Linus Torvalds 已提交
2376 2377 2378 2379 2380 2381
	}

	/*
	 * ignore all characters if CREAD is not set
	 */
	if ((termios->c_cflag & CREAD) == 0)
2382
		port->ignore_status_mask |= UART_LSR_DR;
L
Linus Torvalds 已提交
2383 2384 2385 2386

	/*
	 * CTS flow control flag and modem status interrupts
	 */
2387
	up->ier &= ~UART_IER_MSI;
2388 2389
	if (!(up->bugs & UART_BUG_NOMSR) &&
			UART_ENABLE_MS(&up->port, termios->c_cflag))
L
Linus Torvalds 已提交
2390 2391
		up->ier |= UART_IER_MSI;
	if (up->capabilities & UART_CAP_UUE)
2392 2393 2394
		up->ier |= UART_IER_UUE;
	if (up->capabilities & UART_CAP_RTOIE)
		up->ier |= UART_IER_RTOIE;
L
Linus Torvalds 已提交
2395

2396
	serial_port_out(port, UART_IER, up->ier);
L
Linus Torvalds 已提交
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407

	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;

2408
		serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2409
		if (port->flags & UPF_EXAR_EFR)
2410
			serial_port_out(port, UART_XR_EFR, efr);
2411
		else
2412
			serial_port_out(port, UART_EFR, efr);
L
Linus Torvalds 已提交
2413 2414
	}

2415
	/* Workaround to enable 115200 baud on OMAP1510 internal ports */
2416
	if (is_omap1510_8250(up)) {
2417 2418
		if (baud == 115200) {
			quot = 1;
2419
			serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
2420
		} else
2421
			serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
2422 2423
	}

2424 2425 2426 2427 2428 2429 2430 2431
	/*
	 * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
	 * otherwise just set DLAB
	 */
	if (up->capabilities & UART_NATSEMI)
		serial_port_out(port, UART_LCR, 0xe0);
	else
		serial_port_out(port, UART_LCR, cval | UART_LCR_DLAB);
L
Linus Torvalds 已提交
2432

2433
	serial_dl_write(up, quot);
L
Linus Torvalds 已提交
2434 2435 2436 2437 2438

	/*
	 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
	 * is written without DLAB set, this mode will be disabled.
	 */
2439
	if (port->type == PORT_16750)
2440
		serial_port_out(port, UART_FCR, fcr);
L
Linus Torvalds 已提交
2441

2442
	serial_port_out(port, UART_LCR, cval);		/* reset DLAB */
L
Linus Torvalds 已提交
2443
	up->lcr = cval;					/* Save LCR */
2444
	if (port->type != PORT_16750) {
2445 2446 2447 2448
		/* emulated UARTs (Lucent Venus 167x) need two steps */
		if (fcr & UART_FCR_ENABLE_FIFO)
			serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
		serial_port_out(port, UART_FCR, fcr);		/* set fcr */
L
Linus Torvalds 已提交
2449
	}
2450 2451
	serial8250_set_mctrl(port, port->mctrl);
	spin_unlock_irqrestore(&port->lock, flags);
A
Alan Cox 已提交
2452 2453 2454
	/* Don't rewrite B0 */
	if (tty_termios_baud_rate(termios))
		tty_termios_encode_baud_rate(termios, baud, baud);
L
Linus Torvalds 已提交
2455
}
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466
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 已提交
2467

2468
static void
A
Arnd Bergmann 已提交
2469
serial8250_set_ldisc(struct uart_port *port, int new)
2470
{
A
Arnd Bergmann 已提交
2471
	if (new == N_PPS) {
2472 2473 2474 2475 2476 2477
		port->flags |= UPF_HARDPPS_CD;
		serial8250_enable_ms(port);
	} else
		port->flags &= ~UPF_HARDPPS_CD;
}

2478 2479 2480

void serial8250_do_pm(struct uart_port *port, unsigned int state,
		      unsigned int oldstate)
L
Linus Torvalds 已提交
2481
{
2482 2483
	struct uart_8250_port *p =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
2484 2485

	serial8250_set_sleep(p, state != 0);
2486 2487
}
EXPORT_SYMBOL(serial8250_do_pm);
L
Linus Torvalds 已提交
2488

2489 2490 2491 2492 2493 2494 2495 2496
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 已提交
2497 2498
}

2499 2500 2501
static unsigned int serial8250_port_size(struct uart_8250_port *pt)
{
	if (pt->port.iotype == UPIO_AU)
2502
		return 0x1000;
2503
	if (is_omap1_8250(pt))
2504
		return 0x16 << pt->port.regshift;
2505

2506 2507 2508
	return 8 << pt->port.regshift;
}

L
Linus Torvalds 已提交
2509 2510 2511 2512 2513
/*
 * Resource handling.
 */
static int serial8250_request_std_resource(struct uart_8250_port *up)
{
2514
	unsigned int size = serial8250_port_size(up);
2515
	struct uart_port *port = &up->port;
L
Linus Torvalds 已提交
2516 2517
	int ret = 0;

2518
	switch (port->iotype) {
2519
	case UPIO_AU:
2520 2521
	case UPIO_TSI:
	case UPIO_MEM32:
L
Linus Torvalds 已提交
2522
	case UPIO_MEM:
2523
		if (!port->mapbase)
L
Linus Torvalds 已提交
2524 2525
			break;

2526
		if (!request_mem_region(port->mapbase, size, "serial")) {
L
Linus Torvalds 已提交
2527 2528 2529 2530
			ret = -EBUSY;
			break;
		}

2531 2532 2533 2534
		if (port->flags & UPF_IOREMAP) {
			port->membase = ioremap_nocache(port->mapbase, size);
			if (!port->membase) {
				release_mem_region(port->mapbase, size);
L
Linus Torvalds 已提交
2535 2536 2537 2538 2539 2540 2541
				ret = -ENOMEM;
			}
		}
		break;

	case UPIO_HUB6:
	case UPIO_PORT:
2542
		if (!request_region(port->iobase, size, "serial"))
L
Linus Torvalds 已提交
2543 2544 2545 2546 2547 2548 2549 2550
			ret = -EBUSY;
		break;
	}
	return ret;
}

static void serial8250_release_std_resource(struct uart_8250_port *up)
{
2551
	unsigned int size = serial8250_port_size(up);
2552
	struct uart_port *port = &up->port;
L
Linus Torvalds 已提交
2553

2554
	switch (port->iotype) {
2555
	case UPIO_AU:
2556 2557
	case UPIO_TSI:
	case UPIO_MEM32:
L
Linus Torvalds 已提交
2558
	case UPIO_MEM:
2559
		if (!port->mapbase)
L
Linus Torvalds 已提交
2560 2561
			break;

2562 2563 2564
		if (port->flags & UPF_IOREMAP) {
			iounmap(port->membase);
			port->membase = NULL;
L
Linus Torvalds 已提交
2565 2566
		}

2567
		release_mem_region(port->mapbase, size);
L
Linus Torvalds 已提交
2568 2569 2570 2571
		break;

	case UPIO_HUB6:
	case UPIO_PORT:
2572
		release_region(port->iobase, size);
L
Linus Torvalds 已提交
2573 2574 2575 2576 2577 2578 2579 2580
		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;
2581
	struct uart_port *port = &up->port;
2582
	int ret = -EINVAL;
L
Linus Torvalds 已提交
2583

2584
	switch (port->iotype) {
L
Linus Torvalds 已提交
2585 2586
	case UPIO_HUB6:
	case UPIO_PORT:
2587
		start += port->iobase;
2588 2589 2590
		if (request_region(start, size, "serial-rsa"))
			ret = 0;
		else
L
Linus Torvalds 已提交
2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601
			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;
2602
	struct uart_port *port = &up->port;
L
Linus Torvalds 已提交
2603

2604
	switch (port->iotype) {
L
Linus Torvalds 已提交
2605 2606
	case UPIO_HUB6:
	case UPIO_PORT:
2607
		release_region(port->iobase + offset, size);
L
Linus Torvalds 已提交
2608 2609 2610 2611 2612 2613
		break;
	}
}

static void serial8250_release_port(struct uart_port *port)
{
2614 2615
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
2616 2617

	serial8250_release_std_resource(up);
2618
	if (port->type == PORT_RSA)
L
Linus Torvalds 已提交
2619 2620 2621 2622 2623
		serial8250_release_rsa_resource(up);
}

static int serial8250_request_port(struct uart_port *port)
{
2624 2625
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
S
Sean Young 已提交
2626 2627 2628 2629
	int ret;

	if (port->type == PORT_8250_CIR)
		return -ENODEV;
L
Linus Torvalds 已提交
2630 2631

	ret = serial8250_request_std_resource(up);
2632
	if (ret == 0 && port->type == PORT_RSA) {
L
Linus Torvalds 已提交
2633 2634 2635 2636 2637 2638 2639 2640 2641 2642
		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)
{
2643 2644
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
L
Linus Torvalds 已提交
2645 2646 2647
	int probeflags = PROBE_ANY;
	int ret;

S
Sean Young 已提交
2648 2649 2650
	if (port->type == PORT_8250_CIR)
		return;

L
Linus Torvalds 已提交
2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662
	/*
	 * 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;

2663
	if (port->iotype != up->cur_iotype)
A
Alan Cox 已提交
2664 2665
		set_io_from_upio(port);

L
Linus Torvalds 已提交
2666 2667
	if (flags & UART_CONFIG_TYPE)
		autoconfig(up, probeflags);
2668 2669

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

2673
	if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
L
Linus Torvalds 已提交
2674 2675
		autoconfig_irq(up);

2676
	if (port->type != PORT_RSA && probeflags & PROBE_RSA)
L
Linus Torvalds 已提交
2677
		serial8250_release_rsa_resource(up);
2678
	if (port->type == PORT_UNKNOWN)
L
Linus Torvalds 已提交
2679
		serial8250_release_std_resource(up);
2680 2681

	/* Fixme: probably not the best place for this */
2682 2683
	if ((port->type == PORT_XR17V35X) ||
	   (port->type == PORT_XR17D15X))
2684
		port->handle_irq = exar_handle_irq;
L
Linus Torvalds 已提交
2685 2686 2687 2688 2689
}

static int
serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
{
Y
Yinghai Lu 已提交
2690
	if (ser->irq >= nr_irqs || ser->irq < 0 ||
L
Linus Torvalds 已提交
2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719
	    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,
2720
	.set_ldisc	= serial8250_set_ldisc,
L
Linus Torvalds 已提交
2721 2722 2723 2724 2725 2726
	.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 已提交
2727 2728 2729 2730
#ifdef CONFIG_CONSOLE_POLL
	.poll_get_char = serial8250_get_poll_char,
	.poll_put_char = serial8250_put_poll_char,
#endif
L
Linus Torvalds 已提交
2731 2732 2733 2734
};

static struct uart_8250_port serial8250_ports[UART_NR];

2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
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 已提交
2745 2746 2747 2748
static void __init serial8250_isa_init_ports(void)
{
	struct uart_8250_port *up;
	static int first = 1;
2749
	int i, irqflag = 0;
L
Linus Torvalds 已提交
2750 2751 2752 2753 2754

	if (!first)
		return;
	first = 0;

2755 2756 2757
	if (nr_uarts > UART_NR)
		nr_uarts = UART_NR;

2758
	for (i = 0; i < nr_uarts; i++) {
L
Linus Torvalds 已提交
2759
		struct uart_8250_port *up = &serial8250_ports[i];
2760
		struct uart_port *port = &up->port;
L
Linus Torvalds 已提交
2761

2762 2763
		port->line = i;
		spin_lock_init(&port->lock);
L
Linus Torvalds 已提交
2764 2765 2766

		init_timer(&up->timer);
		up->timer.function = serial8250_timeout;
2767
		up->cur_iotype = 0xFF;
L
Linus Torvalds 已提交
2768 2769 2770 2771 2772 2773 2774

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

2775
		port->ops = &serial8250_pops;
L
Linus Torvalds 已提交
2776 2777
	}

2778 2779 2780
	if (share_irqs)
		irqflag = IRQF_SHARED;

2781
	for (i = 0, up = serial8250_ports;
2782
	     i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
L
Linus Torvalds 已提交
2783
	     i++, up++) {
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796
		struct uart_port *port = &up->port;

		port->iobase   = old_serial_port[i].port;
		port->irq      = irq_canonicalize(old_serial_port[i].irq);
		port->irqflags = old_serial_port[i].irqflags;
		port->uartclk  = old_serial_port[i].baud_base * 16;
		port->flags    = old_serial_port[i].flags;
		port->hub6     = old_serial_port[i].hub6;
		port->membase  = old_serial_port[i].iomem_base;
		port->iotype   = old_serial_port[i].io_type;
		port->regshift = old_serial_port[i].iomem_reg_shift;
		set_io_from_upio(port);
		port->irqflags |= irqflag;
2797 2798 2799
		if (serial8250_isa_config != NULL)
			serial8250_isa_config(i, &up->port, &up->capabilities);

L
Linus Torvalds 已提交
2800 2801 2802
	}
}

2803 2804 2805 2806
static void
serial8250_init_fixed_type_port(struct uart_8250_port *up, unsigned int type)
{
	up->port.type = type;
2807 2808 2809 2810 2811 2812
	if (!up->port.fifosize)
		up->port.fifosize = uart_config[type].fifo_size;
	if (!up->tx_loadsz)
		up->tx_loadsz = uart_config[type].tx_loadsz;
	if (!up->capabilities)
		up->capabilities = uart_config[type].flags;
2813 2814
}

L
Linus Torvalds 已提交
2815 2816 2817 2818 2819
static void __init
serial8250_register_ports(struct uart_driver *drv, struct device *dev)
{
	int i;

A
Alan Cox 已提交
2820 2821 2822
	for (i = 0; i < nr_uarts; i++) {
		struct uart_8250_port *up = &serial8250_ports[i];

2823 2824
		if (up->port.dev)
			continue;
L
Linus Torvalds 已提交
2825 2826

		up->port.dev = dev;
2827 2828 2829 2830

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

L
Linus Torvalds 已提交
2831 2832 2833 2834 2835 2836
		uart_add_one_port(drv, &up->port);
	}
}

#ifdef CONFIG_SERIAL_8250_CONSOLE

2837 2838
static void serial8250_console_putchar(struct uart_port *port, int ch)
{
2839 2840
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);
2841 2842

	wait_for_xmitr(up, UART_LSR_THRE);
2843
	serial_port_out(port, UART_TX, ch);
2844 2845
}

L
Linus Torvalds 已提交
2846 2847 2848 2849 2850 2851 2852 2853 2854 2855
/*
 *	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];
2856
	struct uart_port *port = &up->port;
2857
	unsigned long flags;
L
Linus Torvalds 已提交
2858
	unsigned int ier;
2859
	int locked = 1;
L
Linus Torvalds 已提交
2860

2861 2862
	touch_nmi_watchdog();

2863
	local_irq_save(flags);
2864
	if (port->sysrq) {
2865
		/* serial8250_handle_irq() already took the lock */
2866 2867
		locked = 0;
	} else if (oops_in_progress) {
2868
		locked = spin_trylock(&port->lock);
2869
	} else
2870
		spin_lock(&port->lock);
2871

L
Linus Torvalds 已提交
2872
	/*
R
Ralf Baechle 已提交
2873
	 *	First save the IER then disable the interrupts
L
Linus Torvalds 已提交
2874
	 */
2875
	ier = serial_port_in(port, UART_IER);
L
Linus Torvalds 已提交
2876 2877

	if (up->capabilities & UART_CAP_UUE)
2878
		serial_port_out(port, UART_IER, UART_IER_UUE);
L
Linus Torvalds 已提交
2879
	else
2880
		serial_port_out(port, UART_IER, 0);
L
Linus Torvalds 已提交
2881

2882
	uart_console_write(port, s, count, serial8250_console_putchar);
L
Linus Torvalds 已提交
2883 2884 2885 2886 2887

	/*
	 *	Finally, wait for transmitter to become empty
	 *	and restore the IER
	 */
A
Alan Cox 已提交
2888
	wait_for_xmitr(up, BOTH_EMPTY);
2889
	serial_port_out(port, UART_IER, ier);
2890

2891 2892 2893 2894 2895 2896 2897 2898
	/*
	 *	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)
2899
		serial8250_modem_status(up);
2900

2901
	if (locked)
2902
		spin_unlock(&port->lock);
2903
	local_irq_restore(flags);
L
Linus Torvalds 已提交
2904 2905
}

2906
static int __init serial8250_console_setup(struct console *co, char *options)
L
Linus Torvalds 已提交
2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918
{
	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.
	 */
2919
	if (co->index >= nr_uarts)
L
Linus Torvalds 已提交
2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930
		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);
}

2931
static int serial8250_console_early_setup(void)
2932 2933 2934 2935
{
	return serial8250_find_port_for_earlycon();
}

L
Linus Torvalds 已提交
2936 2937 2938 2939 2940
static struct console serial8250_console = {
	.name		= "ttyS",
	.write		= serial8250_console_write,
	.device		= uart_console_device,
	.setup		= serial8250_console_setup,
2941
	.early_setup	= serial8250_console_early_setup,
2942
	.flags		= CON_PRINTBUFFER | CON_ANYTIME,
L
Linus Torvalds 已提交
2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954
	.index		= -1,
	.data		= &serial8250_reg,
};

static int __init serial8250_console_init(void)
{
	serial8250_isa_init_ports();
	register_console(&serial8250_console);
	return 0;
}
console_initcall(serial8250_console_init);

2955
int serial8250_find_port(struct uart_port *p)
L
Linus Torvalds 已提交
2956 2957 2958 2959
{
	int line;
	struct uart_port *port;

2960
	for (line = 0; line < nr_uarts; line++) {
L
Linus Torvalds 已提交
2961
		port = &serial8250_ports[line].port;
2962
		if (uart_match_port(p, port))
L
Linus Torvalds 已提交
2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981
			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,
};

2982 2983 2984 2985 2986 2987
/*
 * 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 已提交
2988 2989
int __init early_serial_setup(struct uart_port *port)
{
D
David Daney 已提交
2990 2991
	struct uart_port *p;

L
Linus Torvalds 已提交
2992 2993 2994 2995
	if (port->line >= ARRAY_SIZE(serial8250_ports))
		return -ENODEV;

	serial8250_isa_init_ports();
D
David Daney 已提交
2996 2997 2998 2999
	p = &serial8250_ports[port->line].port;
	p->iobase       = port->iobase;
	p->membase      = port->membase;
	p->irq          = port->irq;
3000
	p->irqflags     = port->irqflags;
D
David Daney 已提交
3001 3002 3003 3004 3005 3006 3007
	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;
3008 3009
	p->type		= port->type;
	p->line		= port->line;
3010 3011 3012 3013 3014 3015

	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;
3016 3017 3018 3019
	if (port->handle_irq)
		p->handle_irq = port->handle_irq;
	else
		p->handle_irq = serial8250_default_handle_irq;
3020

L
Linus Torvalds 已提交
3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042
	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)
{
3043
	struct uart_8250_port *up = &serial8250_ports[line];
3044
	struct uart_port *port = &up->port;
3045 3046 3047

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

3050
		ns16550a_goto_highspeed(up);
3051

3052
		serial_port_out(port, UART_LCR, 0);
3053
		port->uartclk = 921600*16;
3054
	}
3055
	uart_resume_port(&serial8250_reg, port);
L
Linus Torvalds 已提交
3056 3057 3058 3059 3060 3061 3062
}

/*
 * 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.
 */
B
Bill Pemberton 已提交
3063
static int serial8250_probe(struct platform_device *dev)
L
Linus Torvalds 已提交
3064
{
3065
	struct plat_serial8250_port *p = dev->dev.platform_data;
3066
	struct uart_8250_port uart;
3067
	int ret, i, irqflag = 0;
L
Linus Torvalds 已提交
3068

3069
	memset(&uart, 0, sizeof(uart));
L
Linus Torvalds 已提交
3070

3071 3072 3073
	if (share_irqs)
		irqflag = IRQF_SHARED;

3074
	for (i = 0; p && p->flags != 0; p++, i++) {
3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095
		uart.port.iobase	= p->iobase;
		uart.port.membase	= p->membase;
		uart.port.irq		= p->irq;
		uart.port.irqflags	= p->irqflags;
		uart.port.uartclk	= p->uartclk;
		uart.port.regshift	= p->regshift;
		uart.port.iotype	= p->iotype;
		uart.port.flags		= p->flags;
		uart.port.mapbase	= p->mapbase;
		uart.port.hub6		= p->hub6;
		uart.port.private_data	= p->private_data;
		uart.port.type		= p->type;
		uart.port.serial_in	= p->serial_in;
		uart.port.serial_out	= p->serial_out;
		uart.port.handle_irq	= p->handle_irq;
		uart.port.handle_break	= p->handle_break;
		uart.port.set_termios	= p->set_termios;
		uart.port.pm		= p->pm;
		uart.port.dev		= &dev->dev;
		uart.port.irqflags	|= irqflag;
		ret = serial8250_register_8250_port(&uart);
3096
		if (ret < 0) {
3097
			dev_err(&dev->dev, "unable to register port at index %d "
3098 3099 3100
				"(IO%lx MEM%llx IRQ%d): %d\n", i,
				p->iobase, (unsigned long long)p->mapbase,
				p->irq, ret);
3101
		}
L
Linus Torvalds 已提交
3102 3103 3104 3105 3106 3107 3108
	}
	return 0;
}

/*
 * Remove serial ports registered against a platform device.
 */
B
Bill Pemberton 已提交
3109
static int serial8250_remove(struct platform_device *dev)
L
Linus Torvalds 已提交
3110 3111 3112
{
	int i;

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

3116
		if (up->port.dev == &dev->dev)
L
Linus Torvalds 已提交
3117 3118 3119 3120 3121
			serial8250_unregister_port(i);
	}
	return 0;
}

3122
static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
L
Linus Torvalds 已提交
3123 3124 3125 3126 3127 3128
{
	int i;

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

3129
		if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
L
Linus Torvalds 已提交
3130 3131 3132 3133 3134 3135
			uart_suspend_port(&serial8250_reg, &up->port);
	}

	return 0;
}

3136
static int serial8250_resume(struct platform_device *dev)
L
Linus Torvalds 已提交
3137 3138 3139 3140 3141 3142
{
	int i;

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

3143
		if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
3144
			serial8250_resume_port(i);
L
Linus Torvalds 已提交
3145 3146 3147 3148 3149
	}

	return 0;
}

3150
static struct platform_driver serial8250_isa_driver = {
L
Linus Torvalds 已提交
3151
	.probe		= serial8250_probe,
3152
	.remove		= serial8250_remove,
L
Linus Torvalds 已提交
3153 3154
	.suspend	= serial8250_suspend,
	.resume		= serial8250_resume,
3155 3156
	.driver		= {
		.name	= "serial8250",
3157
		.owner	= THIS_MODULE,
3158
	},
L
Linus Torvalds 已提交
3159 3160 3161 3162 3163 3164 3165 3166 3167
};

/*
 * 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;

/*
3168
 * serial8250_register_8250_port and serial8250_unregister_port allows for
L
Linus Torvalds 已提交
3169 3170 3171
 * 16x50 serial ports to be configured at run-time, to support PCMCIA
 * modems and PCI multiport cards.
 */
3172
static DEFINE_MUTEX(serial_mutex);
L
Linus Torvalds 已提交
3173 3174 3175 3176 3177 3178 3179 3180

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

	/*
	 * First, find a port entry which matches.
	 */
3181
	for (i = 0; i < nr_uarts; i++)
L
Linus Torvalds 已提交
3182 3183 3184 3185 3186 3187 3188 3189
		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).
	 */
3190
	for (i = 0; i < nr_uarts; i++)
L
Linus Torvalds 已提交
3191 3192 3193 3194 3195 3196 3197 3198
		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.
	 */
3199
	for (i = 0; i < nr_uarts; i++)
L
Linus Torvalds 已提交
3200 3201 3202 3203 3204 3205 3206
		if (serial8250_ports[i].port.type == PORT_UNKNOWN)
			return &serial8250_ports[i];

	return NULL;
}

/**
3207
 *	serial8250_register_8250_port - register a serial port
3208
 *	@up: serial port template
L
Linus Torvalds 已提交
3209 3210 3211 3212 3213 3214 3215 3216 3217 3218
 *
 *	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.
 */
3219
int serial8250_register_8250_port(struct uart_8250_port *up)
L
Linus Torvalds 已提交
3220 3221 3222 3223
{
	struct uart_8250_port *uart;
	int ret = -ENOSPC;

3224
	if (up->port.uartclk == 0)
L
Linus Torvalds 已提交
3225 3226
		return -EINVAL;

3227
	mutex_lock(&serial_mutex);
L
Linus Torvalds 已提交
3228

3229
	uart = serial8250_find_match_or_unused(&up->port);
S
Sean Young 已提交
3230
	if (uart && uart->port.type != PORT_8250_CIR) {
3231 3232
		if (uart->port.dev)
			uart_remove_one_port(&serial8250_reg, &uart->port);
L
Linus Torvalds 已提交
3233

3234 3235 3236 3237 3238 3239 3240 3241 3242
		uart->port.iobase       = up->port.iobase;
		uart->port.membase      = up->port.membase;
		uart->port.irq          = up->port.irq;
		uart->port.irqflags     = up->port.irqflags;
		uart->port.uartclk      = up->port.uartclk;
		uart->port.fifosize     = up->port.fifosize;
		uart->port.regshift     = up->port.regshift;
		uart->port.iotype       = up->port.iotype;
		uart->port.flags        = up->port.flags | UPF_BOOT_AUTOCONF;
A
Alan Cox 已提交
3243
		uart->bugs		= up->bugs;
3244 3245
		uart->port.mapbase      = up->port.mapbase;
		uart->port.private_data = up->port.private_data;
3246 3247 3248 3249
		uart->port.fifosize	= up->port.fifosize;
		uart->tx_loadsz		= up->tx_loadsz;
		uart->capabilities	= up->capabilities;

3250 3251 3252 3253 3254
		if (up->port.dev)
			uart->port.dev = up->port.dev;

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

3256 3257
		set_io_from_upio(&uart->port);
		/* Possibly override default I/O functions.  */
3258 3259 3260 3261 3262 3263
		if (up->port.serial_in)
			uart->port.serial_in = up->port.serial_in;
		if (up->port.serial_out)
			uart->port.serial_out = up->port.serial_out;
		if (up->port.handle_irq)
			uart->port.handle_irq = up->port.handle_irq;
3264
		/*  Possibly override set_termios call */
3265 3266 3267 3268 3269 3270 3271 3272 3273 3274
		if (up->port.set_termios)
			uart->port.set_termios = up->port.set_termios;
		if (up->port.pm)
			uart->port.pm = up->port.pm;
		if (up->port.handle_break)
			uart->port.handle_break = up->port.handle_break;
		if (up->dl_read)
			uart->dl_read = up->dl_read;
		if (up->dl_write)
			uart->dl_write = up->dl_write;
3275 3276
		if (up->dma)
			uart->dma = up->dma;
L
Linus Torvalds 已提交
3277

3278 3279 3280 3281
		if (serial8250_isa_config != NULL)
			serial8250_isa_config(0, &uart->port,
					&uart->capabilities);

L
Linus Torvalds 已提交
3282 3283 3284 3285
		ret = uart_add_one_port(&serial8250_reg, &uart->port);
		if (ret == 0)
			ret = uart->port.line;
	}
3286
	mutex_unlock(&serial_mutex);
L
Linus Torvalds 已提交
3287 3288 3289

	return ret;
}
3290 3291
EXPORT_SYMBOL(serial8250_register_8250_port);

L
Linus Torvalds 已提交
3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302
/**
 *	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];

3303
	mutex_lock(&serial_mutex);
L
Linus Torvalds 已提交
3304 3305 3306 3307 3308
	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;
3309
		uart->capabilities = uart_config[uart->port.type].flags;
L
Linus Torvalds 已提交
3310 3311 3312 3313
		uart_add_one_port(&serial8250_reg, &uart->port);
	} else {
		uart->port.dev = NULL;
	}
3314
	mutex_unlock(&serial_mutex);
L
Linus Torvalds 已提交
3315 3316 3317 3318 3319
}
EXPORT_SYMBOL(serial8250_unregister_port);

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

3322
	serial8250_isa_init_ports();
3323

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

3328 3329 3330 3331
#ifdef CONFIG_SPARC
	ret = sunserial_register_minors(&serial8250_reg, UART_NR);
#else
	serial8250_reg.nr = UART_NR;
L
Linus Torvalds 已提交
3332
	ret = uart_register_driver(&serial8250_reg);
3333
#endif
L
Linus Torvalds 已提交
3334 3335 3336
	if (ret)
		goto out;

3337 3338 3339 3340
	ret = serial8250_pnp_init();
	if (ret)
		goto unreg_uart_drv;

3341 3342 3343 3344
	serial8250_isa_devs = platform_device_alloc("serial8250",
						    PLAT8250_DEV_LEGACY);
	if (!serial8250_isa_devs) {
		ret = -ENOMEM;
3345
		goto unreg_pnp;
L
Linus Torvalds 已提交
3346 3347
	}

3348 3349 3350 3351
	ret = platform_device_add(serial8250_isa_devs);
	if (ret)
		goto put_dev;

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

3354 3355 3356
	ret = platform_driver_register(&serial8250_isa_driver);
	if (ret == 0)
		goto out;
L
Linus Torvalds 已提交
3357

3358
	platform_device_del(serial8250_isa_devs);
A
Alan Cox 已提交
3359
put_dev:
3360
	platform_device_put(serial8250_isa_devs);
3361 3362
unreg_pnp:
	serial8250_pnp_exit();
A
Alan Cox 已提交
3363
unreg_uart_drv:
3364 3365 3366
#ifdef CONFIG_SPARC
	sunserial_unregister_minors(&serial8250_reg, UART_NR);
#else
L
Linus Torvalds 已提交
3367
	uart_unregister_driver(&serial8250_reg);
3368
#endif
A
Alan Cox 已提交
3369
out:
L
Linus Torvalds 已提交
3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383
	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;

3384
	platform_driver_unregister(&serial8250_isa_driver);
L
Linus Torvalds 已提交
3385 3386
	platform_device_unregister(isa_dev);

3387 3388
	serial8250_pnp_exit();

3389 3390 3391
#ifdef CONFIG_SPARC
	sunserial_unregister_minors(&serial8250_reg, UART_NR);
#else
L
Linus Torvalds 已提交
3392
	uart_unregister_driver(&serial8250_reg);
3393
#endif
L
Linus Torvalds 已提交
3394 3395 3396 3397 3398 3399 3400 3401 3402
}

module_init(serial8250_init);
module_exit(serial8250_exit);

EXPORT_SYMBOL(serial8250_suspend_port);
EXPORT_SYMBOL(serial8250_resume_port);

MODULE_LICENSE("GPL");
3403
MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
L
Linus Torvalds 已提交
3404 3405 3406 3407 3408

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

3409 3410 3411
module_param(nr_uarts, uint, 0644);
MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");

3412 3413 3414
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 已提交
3415 3416 3417 3418 3419
#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);
3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448

#ifndef MODULE
/* This module was renamed to 8250_core in 3.7.  Keep the old "8250" name
 * working as well for the module options so we don't break people.  We
 * need to keep the names identical and the convenient macros will happily
 * refuse to let us do that by failing the build with redefinition errors
 * of global variables.  So we stick them inside a dummy function to avoid
 * those conflicts.  The options still get parsed, and the redefined
 * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
 *
 * This is hacky.  I'm sorry.
 */
static void __used s8250_options(void)
{
#undef MODULE_PARAM_PREFIX
#define MODULE_PARAM_PREFIX "8250."

	module_param_cb(share_irqs, &param_ops_uint, &share_irqs, 0644);
	module_param_cb(nr_uarts, &param_ops_uint, &nr_uarts, 0644);
	module_param_cb(skip_txen_test, &param_ops_uint, &skip_txen_test, 0644);
#ifdef CONFIG_SERIAL_8250_RSA
	__module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
		&param_array_ops, .arr = &__param_arr_probe_rsa,
		0444, -1);
#endif
}
#else
MODULE_ALIAS("8250");
#endif