8250_core.c 101.7 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
#include <linux/tty_flip.h>
#include <linux/serial.h>
#include <linux/serial_8250.h>
36
#include <linux/nmi.h>
37
#include <linux/mutex.h>
38
#include <linux/slab.h>
39
#include <linux/uaccess.h>
40
#include <linux/pm_runtime.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
static struct uart_driver serial8250_reg;

static int serial_index(struct uart_port *port)
{
63
	return port->minor - 64;
64 65
}

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)


88
#include <asm/serial.h>
L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96 97
/*
 * 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

98
static const struct old_serial_port old_serial_port[] = {
L
Linus Torvalds 已提交
99 100 101
	SERIAL_PORT_DFNS /* defined in asm/serial.h */
};

102
#define UART_NR	CONFIG_SERIAL_8250_NR_UARTS
L
Linus Torvalds 已提交
103 104 105 106 107 108 109 110 111

#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 已提交
112 113 114
	struct			hlist_node node;
	int			irq;
	spinlock_t		lock;	/* Protects list not the hash */
L
Linus Torvalds 已提交
115 116 117
	struct list_head	*head;
};

A
Alan Cox 已提交
118 119 120
#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 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

/*
 * 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,
151
		.rxtrig_bytes	= {1, 4, 8, 14},
L
Linus Torvalds 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
		.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,
171
		.rxtrig_bytes	= {8, 16, 24, 28},
L
Linus Torvalds 已提交
172 173 174 175 176 177 178 179
		.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,
180
		.rxtrig_bytes	= {1, 16, 32, 56},
L
Linus Torvalds 已提交
181 182 183 184 185 186 187 188 189 190 191 192
		.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,
193 194
		/* UART_CAP_EFR breaks billionon CF bluetooth card. */
		.flags		= UART_CAP_FIFO | UART_CAP_SLEEP,
L
Linus Torvalds 已提交
195 196 197 198 199 200 201
	},
	[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,
202
		.rxtrig_bytes	= {8, 16, 56, 60},
L
Linus Torvalds 已提交
203 204 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
		.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,
231
		.flags		= UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
L
Linus Torvalds 已提交
232
	},
233 234 235 236 237
	[PORT_OCTEON] = {
		.name		= "OCTEON",
		.fifo_size	= 64,
		.tx_loadsz	= 64,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
T
Thomas Koeller 已提交
238 239
		.flags		= UART_CAP_FIFO,
	},
240 241 242 243 244 245 246
	[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,
	},
247 248 249 250 251 252 253
	[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,
	},
254 255 256 257 258 259
	[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,
260
		.rxtrig_bytes	= {1, 4, 8, 14},
261 262
		.flags		= UART_CAP_FIFO | UART_CAP_RTOIE,
	},
263 264 265 266 267
	[PORT_XR17D15X] = {
		.name		= "XR17D15X",
		.fifo_size	= 64,
		.tx_loadsz	= 64,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
268 269
		.flags		= UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
				  UART_CAP_SLEEP,
270
	},
271 272 273 274 275 276 277 278 279
	[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,
	},
280 281 282 283 284 285 286 287
	[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,
	},
288 289 290 291 292 293
	[PORT_BRCM_TRUMANAGE] = {
		.name		= "TruManage",
		.fifo_size	= 1,
		.tx_loadsz	= 1024,
		.flags		= UART_CAP_HFIFO,
	},
S
Sean Young 已提交
294 295
	[PORT_8250_CIR] = {
		.name		= "CIR port"
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	},
	[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,
	},
318 319 320 321 322 323 324 325 326 327 328
/* tx_loadsz is set to 63-bytes instead of 64-bytes to implement
workaround of errata A-008006 which states that tx_loadsz should  be
configured less than Maximum supported fifo bytes */
	[PORT_16550A_FSL64] = {
		.name		= "16550A_FSL64",
		.fifo_size	= 64,
		.tx_loadsz	= 63,
		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
				  UART_FCR7_64BYTE,
		.flags		= UART_CAP_FIFO,
	},
L
Linus Torvalds 已提交
329 330
};

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

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

344
#if defined(CONFIG_MIPS_ALCHEMY) || defined(CONFIG_SERIAL_8250_RT288X)
345

346
/* Au1x00/RT288x UART hardware has a weird register layout */
347 348 349 350 351 352 353 354 355
static const s8 au_io_in_map[8] = {
	 0,	/* UART_RX  */
	 2,	/* UART_IER */
	 3,	/* UART_IIR */
	 5,	/* UART_LCR */
	 6,	/* UART_MCR */
	 7,	/* UART_LSR */
	 8,	/* UART_MSR */
	-1,	/* UART_SCR (unmapped) */
356 357
};

358 359 360 361 362 363 364 365 366
static const s8 au_io_out_map[8] = {
	 1,	/* UART_TX  */
	 2,	/* UART_IER */
	 4,	/* UART_FCR */
	 5,	/* UART_LCR */
	 6,	/* UART_MCR */
	-1,	/* UART_LSR (unmapped) */
	-1,	/* UART_MSR (unmapped) */
	-1,	/* UART_SCR (unmapped) */
367 368
};

369
static unsigned int au_serial_in(struct uart_port *p, int offset)
370
{
371 372 373 374 375 376
	if (offset >= ARRAY_SIZE(au_io_in_map))
		return UINT_MAX;
	offset = au_io_in_map[offset];
	if (offset < 0)
		return UINT_MAX;
	return __raw_readl(p->membase + (offset << p->regshift));
377 378
}

379
static void au_serial_out(struct uart_port *p, int offset, int value)
380
{
381 382 383 384 385 386
	if (offset >= ARRAY_SIZE(au_io_out_map))
		return;
	offset = au_io_out_map[offset];
	if (offset < 0)
		return;
	__raw_writel(value, p->membase + (offset << p->regshift));
387 388 389 390 391 392 393 394 395 396 397
}

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

400 401
#endif

402
static unsigned int hub6_serial_in(struct uart_port *p, int offset)
L
Linus Torvalds 已提交
403
{
404
	offset = offset << p->regshift;
405 406 407
	outb(p->hub6 - 1 + offset, p->iobase);
	return inb(p->iobase + 1);
}
L
Linus Torvalds 已提交
408

409 410
static void hub6_serial_out(struct uart_port *p, int offset, int value)
{
411
	offset = offset << p->regshift;
412 413 414
	outb(p->hub6 - 1 + offset, p->iobase);
	outb(value, p->iobase + 1);
}
L
Linus Torvalds 已提交
415

416 417
static unsigned int mem_serial_in(struct uart_port *p, int offset)
{
418
	offset = offset << p->regshift;
419 420
	return readb(p->membase + offset);
}
L
Linus Torvalds 已提交
421

422 423
static void mem_serial_out(struct uart_port *p, int offset, int value)
{
424
	offset = offset << p->regshift;
425 426 427 428 429
	writeb(value, p->membase + offset);
}

static void mem32_serial_out(struct uart_port *p, int offset, int value)
{
430
	offset = offset << p->regshift;
431 432 433 434 435
	writel(value, p->membase + offset);
}

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

440 441 442 443 444 445 446 447 448 449 450 451
static void mem32be_serial_out(struct uart_port *p, int offset, int value)
{
	offset = offset << p->regshift;
	iowrite32be(value, p->membase + offset);
}

static unsigned int mem32be_serial_in(struct uart_port *p, int offset)
{
	offset = offset << p->regshift;
	return ioread32be(p->membase + offset);
}

452 453
static unsigned int io_serial_in(struct uart_port *p, int offset)
{
454
	offset = offset << p->regshift;
455 456 457 458 459
	return inb(p->iobase + offset);
}

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

464
static int serial8250_default_handle_irq(struct uart_port *port);
465
static int exar_handle_irq(struct uart_port *port);
466

467 468
static void set_io_from_upio(struct uart_port *p)
{
469
	struct uart_8250_port *up = up_to_u8250p(p);
470

471 472
	up->dl_read = default_serial_dl_read;
	up->dl_write = default_serial_dl_write;
473

474
	switch (p->iotype) {
L
Linus Torvalds 已提交
475
	case UPIO_HUB6:
476 477
		p->serial_in = hub6_serial_in;
		p->serial_out = hub6_serial_out;
L
Linus Torvalds 已提交
478 479 480
		break;

	case UPIO_MEM:
481 482
		p->serial_in = mem_serial_in;
		p->serial_out = mem_serial_out;
L
Linus Torvalds 已提交
483 484 485
		break;

	case UPIO_MEM32:
486 487
		p->serial_in = mem32_serial_in;
		p->serial_out = mem32_serial_out;
L
Linus Torvalds 已提交
488 489
		break;

490 491 492 493 494
	case UPIO_MEM32BE:
		p->serial_in = mem32be_serial_in;
		p->serial_out = mem32be_serial_out;
		break;

495
#if defined(CONFIG_MIPS_ALCHEMY) || defined(CONFIG_SERIAL_8250_RT288X)
496
	case UPIO_AU:
497 498
		p->serial_in = au_serial_in;
		p->serial_out = au_serial_out;
499 500
		up->dl_read = au_serial_dl_read;
		up->dl_write = au_serial_dl_write;
501
		break;
502
#endif
503

L
Linus Torvalds 已提交
504
	default:
505 506 507
		p->serial_in = io_serial_in;
		p->serial_out = io_serial_out;
		break;
L
Linus Torvalds 已提交
508
	}
A
Alan Cox 已提交
509 510
	/* Remember loaded iotype */
	up->cur_iotype = p->iotype;
511
	p->handle_irq = serial8250_default_handle_irq;
L
Linus Torvalds 已提交
512 513
}

A
Alex Williamson 已提交
514
static void
515
serial_port_out_sync(struct uart_port *p, int offset, int value)
A
Alex Williamson 已提交
516
{
517
	switch (p->iotype) {
A
Alex Williamson 已提交
518 519
	case UPIO_MEM:
	case UPIO_MEM32:
520
	case UPIO_MEM32BE:
A
Alex Williamson 已提交
521
	case UPIO_AU:
522 523
		p->serial_out(p, offset, value);
		p->serial_in(p, UART_LCR);	/* safe, no side-effects */
A
Alex Williamson 已提交
524 525
		break;
	default:
526
		p->serial_out(p, offset, value);
A
Alex Williamson 已提交
527 528 529
	}
}

L
Linus Torvalds 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
/*
 * 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.
 */
554
static void serial8250_clear_fifos(struct uart_8250_port *p)
L
Linus Torvalds 已提交
555 556
{
	if (p->capabilities & UART_CAP_FIFO) {
557 558
		serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
		serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
L
Linus Torvalds 已提交
559
			       UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
560
		serial_out(p, UART_FCR, 0);
L
Linus Torvalds 已提交
561 562 563
	}
}

564 565 566
void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
{
	serial8250_clear_fifos(p);
567
	serial_out(p, UART_FCR, p->fcr);
568 569 570
}
EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);

571
void serial8250_rpm_get(struct uart_8250_port *p)
572 573 574 575 576
{
	if (!(p->capabilities & UART_CAP_RPM))
		return;
	pm_runtime_get_sync(p->port.dev);
}
577
EXPORT_SYMBOL_GPL(serial8250_rpm_get);
578

579
void serial8250_rpm_put(struct uart_8250_port *p)
580 581 582 583 584 585
{
	if (!(p->capabilities & UART_CAP_RPM))
		return;
	pm_runtime_mark_last_busy(p->port.dev);
	pm_runtime_put_autosuspend(p->port.dev);
}
586
EXPORT_SYMBOL_GPL(serial8250_rpm_put);
587 588

/*
589
 * These two wrappers ensure that enable_runtime_pm_tx() can be called more than
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
 * once and disable_runtime_pm_tx() will still disable RPM because the fifo is
 * empty and the HW can idle again.
 */
static void serial8250_rpm_get_tx(struct uart_8250_port *p)
{
	unsigned char rpm_active;

	if (!(p->capabilities & UART_CAP_RPM))
		return;

	rpm_active = xchg(&p->rpm_tx_active, 1);
	if (rpm_active)
		return;
	pm_runtime_get_sync(p->port.dev);
}

static void serial8250_rpm_put_tx(struct uart_8250_port *p)
{
	unsigned char rpm_active;

	if (!(p->capabilities & UART_CAP_RPM))
		return;

	rpm_active = xchg(&p->rpm_tx_active, 0);
	if (!rpm_active)
		return;
	pm_runtime_mark_last_busy(p->port.dev);
	pm_runtime_put_autosuspend(p->port.dev);
}

L
Linus Torvalds 已提交
620 621 622 623 624
/*
 * 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.
 */
625
static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
L
Linus Torvalds 已提交
626
{
627
	unsigned char lcr = 0, efr = 0;
628 629 630 631 632 633 634
	/*
	 * 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.
	 */
635
	serial8250_rpm_get(p);
636 637
	if ((p->port.type == PORT_XR17V35X) ||
	   (p->port.type == PORT_XR17D15X)) {
638
		serial_out(p, UART_EXAR_SLEEP, sleep ? 0xff : 0);
639
		goto out;
640 641
	}

L
Linus Torvalds 已提交
642 643
	if (p->capabilities & UART_CAP_SLEEP) {
		if (p->capabilities & UART_CAP_EFR) {
644 645
			lcr = serial_in(p, UART_LCR);
			efr = serial_in(p, UART_EFR);
646 647 648
			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 已提交
649
		}
650
		serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
L
Linus Torvalds 已提交
651
		if (p->capabilities & UART_CAP_EFR) {
652
			serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
653 654
			serial_out(p, UART_EFR, efr);
			serial_out(p, UART_LCR, lcr);
L
Linus Torvalds 已提交
655 656
		}
	}
657 658
out:
	serial8250_rpm_put(p);
L
Linus Torvalds 已提交
659 660 661 662 663 664 665 666 667 668 669 670
}

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

671
	mode = serial_in(up, UART_RSA_MSR);
L
Linus Torvalds 已提交
672 673 674
	result = mode & UART_RSA_MSR_FIFO;

	if (!result) {
675 676
		serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
		mode = serial_in(up, UART_RSA_MSR);
L
Linus Torvalds 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
		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)
695
			serial_out(up, UART_RSA_FRR, 0);
L
Linus Torvalds 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
	}
}

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

714
		mode = serial_in(up, UART_RSA_MSR);
L
Linus Torvalds 已提交
715 716 717
		result = !(mode & UART_RSA_MSR_FIFO);

		if (!result) {
718 719
			serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
			mode = serial_in(up, UART_RSA_MSR);
L
Linus Torvalds 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
			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)
{
736 737
	unsigned char old_fcr, old_mcr, old_lcr;
	unsigned short old_dl;
L
Linus Torvalds 已提交
738 739
	int count;

740 741 742 743 744
	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 已提交
745
		    UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
746 747
	serial_out(up, UART_MCR, UART_MCR_LOOP);
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
748 749
	old_dl = serial_dl_read(up);
	serial_dl_write(up, 0x0001);
750
	serial_out(up, UART_LCR, 0x03);
L
Linus Torvalds 已提交
751
	for (count = 0; count < 256; count++)
752
		serial_out(up, UART_TX, count);
L
Linus Torvalds 已提交
753
	mdelay(20);/* FIXME - schedule_timeout */
754
	for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
L
Linus Torvalds 已提交
755
	     (count < 256); count++)
756 757 758 759
		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);
760
	serial_dl_write(up, old_dl);
761
	serial_out(up, UART_LCR, old_lcr);
L
Linus Torvalds 已提交
762 763 764 765 766 767 768 769 770 771 772 773 774 775

	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;

776 777
	old_lcr = serial_in(p, UART_LCR);
	serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
L
Linus Torvalds 已提交
778

779 780
	old_dll = serial_in(p, UART_DLL);
	old_dlm = serial_in(p, UART_DLM);
L
Linus Torvalds 已提交
781

782 783
	serial_out(p, UART_DLL, 0);
	serial_out(p, UART_DLM, 0);
L
Linus Torvalds 已提交
784

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

787 788 789
	serial_out(p, UART_DLL, old_dll);
	serial_out(p, UART_DLM, old_dlm);
	serial_out(p, UART_LCR, old_lcr);
L
Linus Torvalds 已提交
790 791 792 793 794 795 796 797 798 799 800

	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 已提交
801
 *
L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
 * 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;
831
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
L
Linus Torvalds 已提交
832 833 834 835 836 837 838 839 840 841 842 843
	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;
844 845 846 847 848 849 850 851

		/*
		 * 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 已提交
852 853
		return;
	}
T
Thomas Koeller 已提交
854

L
Linus Torvalds 已提交
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
	/*
	 * 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);
899
	serial_out(up, UART_SCR, 0xa5);
L
Linus Torvalds 已提交
900
	status1 = serial_in(up, UART_SCR);
901
	serial_out(up, UART_SCR, 0x5a);
L
Linus Torvalds 已提交
902
	status2 = serial_in(up, UART_SCR);
903
	serial_out(up, UART_SCR, scratch);
L
Linus Torvalds 已提交
904 905 906 907 908 909 910 911 912 913

	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
914
	 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
L
Linus Torvalds 已提交
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
	 */
	if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
		return 1;

	return 0;
}

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

936 937 938 939 940 941 942 943
	/*
	 * 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.
	 */
944 945 946
	if (up->port.flags & UPF_EXAR_EFR) {
		status1 = serial_in(up, UART_EXAR_DVID);
		if (status1 == 0x82 || status1 == 0x84 || status1 == 0x88) {
947 948 949 950 951 952 953 954 955 956
			DEBUG_AUTOCONF("Exar XR17V35x ");
			up->port.type = PORT_XR17V35X;
			up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
						UART_CAP_SLEEP;

			return;
		}

	}

L
Linus Torvalds 已提交
957 958 959 960
	/*
	 * Check for presence of the EFR when DLAB is set.
	 * Only ST16C650V1 UARTs pass this test.
	 */
961
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
L
Linus Torvalds 已提交
962
	if (serial_in(up, UART_EFR) == 0) {
963
		serial_out(up, UART_EFR, 0xA8);
L
Linus Torvalds 已提交
964 965 966 967 968
		if (serial_in(up, UART_EFR) != 0) {
			DEBUG_AUTOCONF("EFRv1 ");
			up->port.type = PORT_16650;
			up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
		} else {
969 970 971 972 973 974 975 976 977 978 979
			serial_out(up, UART_LCR, 0);
			serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
				   UART_FCR7_64BYTE);
			status1 = serial_in(up, UART_IIR) >> 5;
			serial_out(up, UART_FCR, 0);
			serial_out(up, UART_LCR, 0);

			if (status1 == 7)
				up->port.type = PORT_16550A_FSL64;
			else
				DEBUG_AUTOCONF("Motorola 8xxx DUART ");
L
Linus Torvalds 已提交
980
		}
981
		serial_out(up, UART_EFR, 0);
L
Linus Torvalds 已提交
982 983 984 985 986 987 988
		return;
	}

	/*
	 * Maybe it requires 0xbf to be written to the LCR.
	 * (other ST16C650V2 UARTs, TI16C752A, etc)
	 */
989
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
L
Linus Torvalds 已提交
990 991 992 993 994 995 996 997 998 999 1000 1001 1002
	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
	 */
1003
	serial_out(up, UART_LCR, 0);
L
Linus Torvalds 已提交
1004
	status1 = serial_in(up, UART_MCR);
1005
	serial_out(up, UART_LCR, 0xE0);
L
Linus Torvalds 已提交
1006 1007 1008
	status2 = serial_in(up, 0x02); /* EXCR1 */

	if (!((status2 ^ status1) & UART_MCR_LOOP)) {
1009 1010 1011
		serial_out(up, UART_LCR, 0);
		serial_out(up, UART_MCR, status1 ^ UART_MCR_LOOP);
		serial_out(up, UART_LCR, 0xE0);
L
Linus Torvalds 已提交
1012
		status2 = serial_in(up, 0x02); /* EXCR1 */
1013 1014
		serial_out(up, UART_LCR, 0);
		serial_out(up, UART_MCR, status1);
L
Linus Torvalds 已提交
1015 1016

		if ((status2 ^ status1) & UART_MCR_LOOP) {
1017 1018
			unsigned short quot;

1019
			serial_out(up, UART_LCR, 0xE0);
1020

1021
			quot = serial_dl_read(up);
1022 1023
			quot <<= 3;

1024 1025
			if (ns16550a_goto_highspeed(up))
				serial_dl_write(up, quot);
1026

1027
			serial_out(up, UART_LCR, 0);
L
Linus Torvalds 已提交
1028

1029
			up->port.uartclk = 921600*16;
L
Linus Torvalds 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
			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.
	 */
1042 1043
	serial_out(up, UART_LCR, 0);
	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
L
Linus Torvalds 已提交
1044
	status1 = serial_in(up, UART_IIR) >> 5;
1045 1046 1047
	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 已提交
1048
	status2 = serial_in(up, UART_IIR) >> 5;
1049 1050
	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
	serial_out(up, UART_LCR, 0);
L
Linus Torvalds 已提交
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068

	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);
1069
	serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
L
Linus Torvalds 已提交
1070 1071 1072 1073 1074
	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.
		 */
1075
		serial_out(up, UART_IER, iersave | UART_IER_UUE);
L
Linus Torvalds 已提交
1076 1077 1078 1079 1080 1081 1082
		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;
1083
			up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
L
Linus Torvalds 已提交
1084 1085 1086 1087 1088 1089 1090 1091 1092
			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 ");
	}
1093
	serial_out(up, UART_IER, iersave);
1094

1095 1096 1097 1098
	/*
	 * Exar uarts have EFR in a weird location
	 */
	if (up->port.flags & UPF_EXAR_EFR) {
1099
		DEBUG_AUTOCONF("Exar XR17D15x ");
1100
		up->port.type = PORT_XR17D15X;
1101 1102 1103 1104
		up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
				    UART_CAP_SLEEP;

		return;
1105 1106
	}

1107 1108 1109 1110 1111 1112 1113 1114
	/*
	 * 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 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123
}

/*
 * 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.
 */
P
Peter Hurley 已提交
1124
static void autoconfig(struct uart_8250_port *up)
L
Linus Torvalds 已提交
1125 1126 1127
{
	unsigned char status1, scratch, scratch2, scratch3;
	unsigned char save_lcr, save_mcr;
1128
	struct uart_port *port = &up->port;
L
Linus Torvalds 已提交
1129
	unsigned long flags;
1130
	unsigned int old_capabilities;
L
Linus Torvalds 已提交
1131

1132
	if (!port->iobase && !port->mapbase && !port->membase)
L
Linus Torvalds 已提交
1133 1134
		return;

1135
	DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
1136
		       serial_index(port), port->iobase, port->membase);
L
Linus Torvalds 已提交
1137 1138 1139 1140 1141

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

	up->capabilities = 0;
1145
	up->bugs = 0;
L
Linus Torvalds 已提交
1146

1147
	if (!(port->flags & UPF_BUGGY_UART)) {
L
Linus Torvalds 已提交
1148 1149 1150
		/*
		 * Do a simple existence test first; if we fail this,
		 * there's no point trying anything else.
T
Thomas Koeller 已提交
1151
		 *
L
Linus Torvalds 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160
		 * 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.
		 */
1161 1162
		scratch = serial_in(up, UART_IER);
		serial_out(up, UART_IER, 0);
L
Linus Torvalds 已提交
1163 1164 1165
#ifdef __i386__
		outb(0xff, 0x080);
#endif
1166 1167 1168 1169
		/*
		 * 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.
		 */
1170 1171
		scratch2 = serial_in(up, UART_IER) & 0x0f;
		serial_out(up, UART_IER, 0x0F);
L
Linus Torvalds 已提交
1172 1173 1174
#ifdef __i386__
		outb(0, 0x080);
#endif
1175 1176
		scratch3 = serial_in(up, UART_IER) & 0x0f;
		serial_out(up, UART_IER, scratch);
L
Linus Torvalds 已提交
1177 1178 1179 1180
		if (scratch2 != 0 || scratch3 != 0x0F) {
			/*
			 * We failed; there's nothing here
			 */
1181
			spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
1182 1183 1184 1185 1186 1187 1188 1189 1190
			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 已提交
1191
	/*
L
Linus Torvalds 已提交
1192 1193 1194 1195 1196 1197 1198 1199
	 * 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!
	 */
1200
	if (!(port->flags & UPF_SKIP_TEST)) {
1201 1202 1203
		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 已提交
1204
		if (status1 != 0x90) {
1205
			spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
1206 1207 1208 1209 1210 1211 1212 1213 1214
			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 已提交
1215
	 * out if it's 8250 or 16450, 16550, 16550A or later.  This
L
Linus Torvalds 已提交
1216 1217 1218 1219 1220
	 * 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.
	 */
1221 1222 1223
	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 已提交
1224

1225
	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
L
Linus Torvalds 已提交
1226 1227 1228 1229 1230 1231 1232
	scratch = serial_in(up, UART_IIR) >> 6;

	switch (scratch) {
	case 0:
		autoconfig_8250(up);
		break;
	case 1:
1233
		port->type = PORT_UNKNOWN;
L
Linus Torvalds 已提交
1234 1235
		break;
	case 2:
1236
		port->type = PORT_16550;
L
Linus Torvalds 已提交
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
		break;
	case 3:
		autoconfig_16550a(up);
		break;
	}

#ifdef CONFIG_SERIAL_8250_RSA
	/*
	 * Only probe for RSA ports if we got the region.
	 */
P
Peter Hurley 已提交
1247
	if (port->type == PORT_16550A && up->probe & UART_PROBE_RSA &&
1248 1249
	    __enable_rsa(up))
		port->type = PORT_RSA;
L
Linus Torvalds 已提交
1250
#endif
1251

1252
	serial_out(up, UART_LCR, save_lcr);
L
Linus Torvalds 已提交
1253

1254
	port->fifosize = uart_config[up->port.type].fifo_size;
1255
	old_capabilities = up->capabilities;
1256 1257
	up->capabilities = uart_config[port->type].flags;
	up->tx_loadsz = uart_config[port->type].tx_loadsz;
L
Linus Torvalds 已提交
1258

1259
	if (port->type == PORT_UNKNOWN)
1260
		goto out_lock;
L
Linus Torvalds 已提交
1261 1262 1263 1264 1265

	/*
	 * Reset the UART.
	 */
#ifdef CONFIG_SERIAL_8250_RSA
1266
	if (port->type == PORT_RSA)
1267
		serial_out(up, UART_RSA_FRR, 0);
L
Linus Torvalds 已提交
1268
#endif
1269
	serial_out(up, UART_MCR, save_mcr);
L
Linus Torvalds 已提交
1270
	serial8250_clear_fifos(up);
A
Alex Williamson 已提交
1271
	serial_in(up, UART_RX);
1272
	if (up->capabilities & UART_CAP_UUE)
1273
		serial_out(up, UART_IER, UART_IER_UUE);
1274
	else
1275
		serial_out(up, UART_IER, 0);
L
Linus Torvalds 已提交
1276

1277
out_lock:
1278
	spin_unlock_irqrestore(&port->lock, flags);
1279 1280 1281 1282 1283 1284 1285 1286
	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);
1287
	DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
L
Linus Torvalds 已提交
1288 1289 1290 1291
}

static void autoconfig_irq(struct uart_8250_port *up)
{
1292
	struct uart_port *port = &up->port;
L
Linus Torvalds 已提交
1293 1294 1295 1296 1297 1298
	unsigned char save_mcr, save_ier;
	unsigned char save_ICP = 0;
	unsigned int ICP = 0;
	unsigned long irqs;
	int irq;

1299 1300
	if (port->flags & UPF_FOURPORT) {
		ICP = (port->iobase & 0xfe0) | 0x1f;
L
Linus Torvalds 已提交
1301 1302
		save_ICP = inb_p(ICP);
		outb_p(0x80, ICP);
1303
		inb_p(ICP);
L
Linus Torvalds 已提交
1304 1305 1306 1307
	}

	/* forget possible initially masked and pending IRQ */
	probe_irq_off(probe_irq_on());
1308 1309 1310
	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 已提交
1311

L
Linus Torvalds 已提交
1312
	irqs = probe_irq_on();
1313
	serial_out(up, UART_MCR, 0);
A
Alan Cox 已提交
1314
	udelay(10);
1315
	if (port->flags & UPF_FOURPORT) {
1316
		serial_out(up, UART_MCR,
L
Linus Torvalds 已提交
1317 1318
			    UART_MCR_DTR | UART_MCR_RTS);
	} else {
1319
		serial_out(up, UART_MCR,
L
Linus Torvalds 已提交
1320 1321
			    UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
	}
1322
	serial_out(up, UART_IER, 0x0f);	/* enable all intrs */
1323 1324 1325 1326
	serial_in(up, UART_LSR);
	serial_in(up, UART_RX);
	serial_in(up, UART_IIR);
	serial_in(up, UART_MSR);
1327
	serial_out(up, UART_TX, 0xFF);
A
Alan Cox 已提交
1328
	udelay(20);
L
Linus Torvalds 已提交
1329 1330
	irq = probe_irq_off(irqs);

1331 1332
	serial_out(up, UART_MCR, save_mcr);
	serial_out(up, UART_IER, save_ier);
L
Linus Torvalds 已提交
1333

1334
	if (port->flags & UPF_FOURPORT)
L
Linus Torvalds 已提交
1335 1336
		outb_p(save_ICP, ICP);

1337
	port->irq = (irq > 0) ? irq : 0;
L
Linus Torvalds 已提交
1338 1339
}

1340 1341 1342 1343 1344
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);
1345
		serial8250_rpm_put_tx(p);
1346 1347 1348
	}
}

1349
static void serial8250_stop_tx(struct uart_port *port)
L
Linus Torvalds 已提交
1350
{
1351
	struct uart_8250_port *up = up_to_u8250p(port);
L
Linus Torvalds 已提交
1352

1353
	serial8250_rpm_get(up);
1354
	__stop_tx(up);
L
Linus Torvalds 已提交
1355 1356

	/*
1357
	 * We really want to stop the transmitter from sending.
L
Linus Torvalds 已提交
1358
	 */
1359
	if (port->type == PORT_16C950) {
L
Linus Torvalds 已提交
1360 1361 1362
		up->acr |= UART_ACR_TXDIS;
		serial_icr_write(up, UART_ACR, up->acr);
	}
1363
	serial8250_rpm_put(up);
L
Linus Torvalds 已提交
1364 1365
}

1366
static void serial8250_start_tx(struct uart_port *port)
L
Linus Torvalds 已提交
1367
{
1368
	struct uart_8250_port *up = up_to_u8250p(port);
L
Linus Torvalds 已提交
1369

1370
	serial8250_rpm_get_tx(up);
1371 1372

	if (up->dma && !up->dma->tx_dma(up))
1373
		return;
1374 1375

	if (!(up->ier & UART_IER_THRI)) {
L
Linus Torvalds 已提交
1376
		up->ier |= UART_IER_THRI;
1377
		serial_port_out(port, UART_IER, up->ier);
1378

1379
		if (up->bugs & UART_BUG_TXEN) {
1380
			unsigned char lsr;
1381
			lsr = serial_in(up, UART_LSR);
1382
			up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1383
			if (lsr & UART_LSR_THRE)
1384
				serial8250_tx_chars(up);
1385
		}
L
Linus Torvalds 已提交
1386
	}
1387

L
Linus Torvalds 已提交
1388
	/*
1389
	 * Re-enable the transmitter if we disabled it.
L
Linus Torvalds 已提交
1390
	 */
1391
	if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
L
Linus Torvalds 已提交
1392 1393 1394 1395 1396
		up->acr &= ~UART_ACR_TXDIS;
		serial_icr_write(up, UART_ACR, up->acr);
	}
}

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
static void serial8250_throttle(struct uart_port *port)
{
	port->throttle(port);
}

static void serial8250_unthrottle(struct uart_port *port)
{
	port->unthrottle(port);
}

L
Linus Torvalds 已提交
1407 1408
static void serial8250_stop_rx(struct uart_port *port)
{
1409
	struct uart_8250_port *up = up_to_u8250p(port);
L
Linus Torvalds 已提交
1410

1411 1412
	serial8250_rpm_get(up);

1413
	up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
L
Linus Torvalds 已提交
1414
	up->port.read_status_mask &= ~UART_LSR_DR;
1415
	serial_port_out(port, UART_IER, up->ier);
1416 1417

	serial8250_rpm_put(up);
L
Linus Torvalds 已提交
1418 1419
}

1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
static void serial8250_disable_ms(struct uart_port *port)
{
	struct uart_8250_port *up =
		container_of(port, struct uart_8250_port, port);

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

	up->ier &= ~UART_IER_MSI;
	serial_port_out(port, UART_IER, up->ier);
}

L
Linus Torvalds 已提交
1433 1434
static void serial8250_enable_ms(struct uart_port *port)
{
1435
	struct uart_8250_port *up = up_to_u8250p(port);
L
Linus Torvalds 已提交
1436

1437 1438 1439 1440
	/* no MSR capabilities */
	if (up->bugs & UART_BUG_NOMSR)
		return;

L
Linus Torvalds 已提交
1441
	up->ier |= UART_IER_MSI;
1442 1443

	serial8250_rpm_get(up);
1444
	serial_port_out(port, UART_IER, up->ier);
1445
	serial8250_rpm_put(up);
L
Linus Torvalds 已提交
1446 1447
}

1448
/*
1449
 * serial8250_rx_chars: processes according to the passed in LSR
1450 1451 1452
 * value, and returns the remaining LSR bits not handled
 * by this Rx routine.
 */
1453 1454
unsigned char
serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
L
Linus Torvalds 已提交
1455
{
1456
	struct uart_port *port = &up->port;
1457
	unsigned char ch;
L
Linus Torvalds 已提交
1458 1459 1460 1461
	int max_count = 256;
	char flag;

	do {
1462
		if (likely(lsr & UART_LSR_DR))
1463
			ch = serial_in(up, UART_RX);
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
		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 已提交
1474
		flag = TTY_NORMAL;
1475
		port->icount.rx++;
L
Linus Torvalds 已提交
1476

1477 1478
		lsr |= up->lsr_saved_flags;
		up->lsr_saved_flags = 0;
L
Linus Torvalds 已提交
1479

1480
		if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
L
Linus Torvalds 已提交
1481 1482
			if (lsr & UART_LSR_BI) {
				lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1483
				port->icount.brk++;
L
Linus Torvalds 已提交
1484 1485 1486 1487 1488 1489
				/*
				 * We do the SysRQ and SAK checking
				 * here because otherwise the break
				 * may get masked by ignore_status_mask
				 * or read_status_mask.
				 */
1490
				if (uart_handle_break(port))
L
Linus Torvalds 已提交
1491 1492
					goto ignore_char;
			} else if (lsr & UART_LSR_PE)
1493
				port->icount.parity++;
L
Linus Torvalds 已提交
1494
			else if (lsr & UART_LSR_FE)
1495
				port->icount.frame++;
L
Linus Torvalds 已提交
1496
			if (lsr & UART_LSR_OE)
1497
				port->icount.overrun++;
L
Linus Torvalds 已提交
1498 1499

			/*
1500
			 * Mask off conditions which should be ignored.
L
Linus Torvalds 已提交
1501
			 */
1502
			lsr &= port->read_status_mask;
L
Linus Torvalds 已提交
1503 1504 1505 1506 1507 1508 1509 1510 1511

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

1515
		uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
1516

A
Alan Cox 已提交
1517
ignore_char:
1518
		lsr = serial_in(up, UART_LSR);
1519
	} while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (--max_count > 0));
1520
	spin_unlock(&port->lock);
J
Jiri Slaby 已提交
1521
	tty_flip_buffer_push(&port->state->port);
1522
	spin_lock(&port->lock);
1523
	return lsr;
L
Linus Torvalds 已提交
1524
}
1525
EXPORT_SYMBOL_GPL(serial8250_rx_chars);
L
Linus Torvalds 已提交
1526

1527
void serial8250_tx_chars(struct uart_8250_port *up)
L
Linus Torvalds 已提交
1528
{
1529 1530
	struct uart_port *port = &up->port;
	struct circ_buf *xmit = &port->state->xmit;
L
Linus Torvalds 已提交
1531 1532
	int count;

1533 1534 1535 1536
	if (port->x_char) {
		serial_out(up, UART_TX, port->x_char);
		port->icount.tx++;
		port->x_char = 0;
L
Linus Torvalds 已提交
1537 1538
		return;
	}
1539 1540
	if (uart_tx_stopped(port)) {
		serial8250_stop_tx(port);
1541 1542 1543
		return;
	}
	if (uart_circ_empty(xmit)) {
1544
		__stop_tx(up);
L
Linus Torvalds 已提交
1545 1546 1547 1548 1549 1550 1551
		return;
	}

	count = up->tx_loadsz;
	do {
		serial_out(up, UART_TX, xmit->buf[xmit->tail]);
		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1552
		port->icount.tx++;
L
Linus Torvalds 已提交
1553 1554
		if (uart_circ_empty(xmit))
			break;
1555 1556 1557 1558 1559
		if (up->capabilities & UART_CAP_HFIFO) {
			if ((serial_port_in(port, UART_LSR) & BOTH_EMPTY) !=
			    BOTH_EMPTY)
				break;
		}
L
Linus Torvalds 已提交
1560 1561 1562
	} while (--count > 0);

	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1563
		uart_write_wakeup(port);
L
Linus Torvalds 已提交
1564 1565 1566

	DEBUG_INTR("THRE...");

1567
	/*
1568
	 * With RPM enabled, we have to wait until the FIFO is empty before the
1569 1570 1571 1572
	 * HW can go idle. So we get here once again with empty FIFO and disable
	 * the interrupt and RPM in __stop_tx()
	 */
	if (uart_circ_empty(xmit) && !(up->capabilities & UART_CAP_RPM))
1573
		__stop_tx(up);
L
Linus Torvalds 已提交
1574
}
1575
EXPORT_SYMBOL_GPL(serial8250_tx_chars);
L
Linus Torvalds 已提交
1576

1577
/* Caller holds uart port lock */
1578
unsigned int serial8250_modem_status(struct uart_8250_port *up)
L
Linus Torvalds 已提交
1579
{
1580
	struct uart_port *port = &up->port;
1581 1582
	unsigned int status = serial_in(up, UART_MSR);

1583 1584
	status |= up->msr_saved_flags;
	up->msr_saved_flags = 0;
1585
	if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1586
	    port->state != NULL) {
1587
		if (status & UART_MSR_TERI)
1588
			port->icount.rng++;
1589
		if (status & UART_MSR_DDSR)
1590
			port->icount.dsr++;
1591
		if (status & UART_MSR_DDCD)
1592
			uart_handle_dcd_change(port, status & UART_MSR_DCD);
1593
		if (status & UART_MSR_DCTS)
1594
			uart_handle_cts_change(port, status & UART_MSR_CTS);
1595

1596
		wake_up_interruptible(&port->state->port.delta_msr_wait);
1597
	}
L
Linus Torvalds 已提交
1598

1599
	return status;
L
Linus Torvalds 已提交
1600
}
1601
EXPORT_SYMBOL_GPL(serial8250_modem_status);
L
Linus Torvalds 已提交
1602 1603 1604 1605

/*
 * This handles the interrupt from one port.
 */
1606
int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
L
Linus Torvalds 已提交
1607
{
1608
	unsigned char status;
1609
	unsigned long flags;
1610
	struct uart_8250_port *up = up_to_u8250p(port);
1611
	int dma_err = 0;
1612 1613 1614

	if (iir & UART_IIR_NO_INT)
		return 0;
1615

1616
	spin_lock_irqsave(&port->lock, flags);
1617

1618
	status = serial_port_in(port, UART_LSR);
L
Linus Torvalds 已提交
1619 1620 1621

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

1622 1623
	if (status & (UART_LSR_DR | UART_LSR_BI)) {
		if (up->dma)
1624
			dma_err = up->dma->rx_dma(up, iir);
1625 1626 1627 1628

		if (!up->dma || dma_err)
			status = serial8250_rx_chars(up, status);
	}
1629
	serial8250_modem_status(up);
1630 1631
	if ((!up->dma || (up->dma && up->dma->tx_err)) &&
	    (status & UART_LSR_THRE))
1632
		serial8250_tx_chars(up);
1633

1634
	spin_unlock_irqrestore(&port->lock, flags);
1635
	return 1;
1636
}
1637
EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1638 1639 1640

static int serial8250_default_handle_irq(struct uart_port *port)
{
1641 1642 1643 1644 1645
	struct uart_8250_port *up = up_to_u8250p(port);
	unsigned int iir;
	int ret;

	serial8250_rpm_get(up);
1646

1647 1648 1649 1650 1651
	iir = serial_port_in(port, UART_IIR);
	ret = serial8250_handle_irq(port, iir);

	serial8250_rpm_put(up);
	return ret;
1652 1653
}

1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
/*
 * 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);

1668 1669
	if ((port->type == PORT_XR17V35X) ||
	   (port->type == PORT_XR17D15X)) {
1670 1671 1672 1673 1674 1675 1676 1677 1678
		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 已提交
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
/*
 * 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.
 */
1693
static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
{
	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;
1706
		struct uart_port *port;
L
Linus Torvalds 已提交
1707 1708

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

1711
		if (port->handle_irq(port)) {
L
Linus Torvalds 已提交
1712 1713 1714 1715 1716 1717 1718 1719 1720
			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. */
1721 1722
			printk_ratelimited(KERN_ERR
				"serial8250: too much work for irq%d\n", irq);
L
Linus Torvalds 已提交
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
			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 已提交
1754 1755 1756 1757 1758
	/* List empty so throw away the hash node */
	if (i->head == NULL) {
		hlist_del(&i->node);
		kfree(i);
	}
L
Linus Torvalds 已提交
1759 1760 1761 1762
}

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

A
Alan Cox 已提交
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
	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 已提交
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
	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);
1801
		irq_flags |= up->port.irqflags;
L
Linus Torvalds 已提交
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
		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)
{
1813 1814 1815 1816
	/*
	 * yes, some broken gcc emit "warning: 'i' may be used uninitialized"
	 * but no, we are not going to take a patch that assigns NULL below.
	 */
A
Alan Cox 已提交
1817 1818 1819
	struct irq_info *i;
	struct hlist_node *n;
	struct hlist_head *h;
L
Linus Torvalds 已提交
1820

A
Alan Cox 已提交
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
	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 已提交
1832 1833 1834 1835 1836 1837
	BUG_ON(i->head == NULL);

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

	serial_do_unlink(i, up);
A
Alan Cox 已提交
1838
	mutex_unlock(&hash_mutex);
L
Linus Torvalds 已提交
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
}

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

1851
	up->port.handle_irq(&up->port);
1852
	mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
A
Alex Williamson 已提交
1853 1854 1855 1856 1857
}

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

1861 1862
	spin_lock_irqsave(&up->port.lock, flags);

A
Alex Williamson 已提交
1863 1864 1865 1866
	/*
	 * Must disable interrupts or else we risk racing with the interrupt
	 * based handler.
	 */
A
Alan Cox 已提交
1867
	if (up->port.irq) {
A
Alex Williamson 已提交
1868 1869 1870
		ier = serial_in(up, UART_IER);
		serial_out(up, UART_IER, 0);
	}
L
Linus Torvalds 已提交
1871

A
Alex Williamson 已提交
1872 1873 1874 1875 1876 1877 1878 1879
	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.
	 */
1880 1881
	lsr = serial_in(up, UART_LSR);
	up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
A
Alex Williamson 已提交
1882
	if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
A
Alan Cox 已提交
1883
	    (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
1884
	    (lsr & UART_LSR_THRE)) {
A
Alex Williamson 已提交
1885 1886 1887 1888 1889
		iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
		iir |= UART_IIR_THRI;
	}

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

A
Alan Cox 已提交
1892
	if (up->port.irq)
A
Alex Williamson 已提交
1893 1894
		serial_out(up, UART_IER, ier);

1895 1896
	spin_unlock_irqrestore(&up->port.lock, flags);

A
Alex Williamson 已提交
1897
	/* Standard timer interval plus 0.2s to keep the port running */
A
Alan Cox 已提交
1898
	mod_timer(&up->timer,
1899
		jiffies + uart_poll_timeout(&up->port) + HZ / 5);
L
Linus Torvalds 已提交
1900 1901
}

1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
static int univ8250_setup_irq(struct uart_8250_port *up)
{
	struct uart_port *port = &up->port;
	int retval = 0;

	/*
	 * 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) {
		pr_debug("ttyS%d - using backup timer\n", serial_index(port));

		up->timer.function = serial8250_backup_timeout;
		up->timer.data = (unsigned long)up;
		mod_timer(&up->timer, jiffies +
			  uart_poll_timeout(port) + HZ / 5);
	}

	/*
	 * 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.
	 */
	if (!port->irq) {
		up->timer.data = (unsigned long)up;
		mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
	} else
		retval = serial_link_irq_chain(up);

	return retval;
}

static void univ8250_release_irq(struct uart_8250_port *up)
{
	struct uart_port *port = &up->port;

	del_timer_sync(&up->timer);
	up->timer.function = serial8250_timeout;
	if (port->irq)
		serial_unlink_irq_chain(up);
}

L
Linus Torvalds 已提交
1944 1945
static unsigned int serial8250_tx_empty(struct uart_port *port)
{
1946
	struct uart_8250_port *up = up_to_u8250p(port);
L
Linus Torvalds 已提交
1947
	unsigned long flags;
1948
	unsigned int lsr;
L
Linus Torvalds 已提交
1949

1950 1951
	serial8250_rpm_get(up);

1952
	spin_lock_irqsave(&port->lock, flags);
1953
	lsr = serial_port_in(port, UART_LSR);
1954
	up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1955
	spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
1956

1957 1958
	serial8250_rpm_put(up);

1959
	return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
L
Linus Torvalds 已提交
1960 1961 1962 1963
}

static unsigned int serial8250_get_mctrl(struct uart_port *port)
{
1964
	struct uart_8250_port *up = up_to_u8250p(port);
1965
	unsigned int status;
L
Linus Torvalds 已提交
1966 1967
	unsigned int ret;

1968
	serial8250_rpm_get(up);
1969
	status = serial8250_modem_status(up);
1970
	serial8250_rpm_put(up);
L
Linus Torvalds 已提交
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983

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

1984
void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl)
L
Linus Torvalds 已提交
1985
{
1986
	struct uart_8250_port *up = up_to_u8250p(port);
L
Linus Torvalds 已提交
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
	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;

2002
	serial_port_out(port, UART_MCR, mcr);
L
Linus Torvalds 已提交
2003
}
2004 2005 2006 2007 2008
EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);

static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
	if (port->set_mctrl)
2009 2010 2011
		port->set_mctrl(port, mctrl);
	else
		serial8250_do_set_mctrl(port, mctrl);
2012
}
L
Linus Torvalds 已提交
2013 2014 2015

static void serial8250_break_ctl(struct uart_port *port, int break_state)
{
2016
	struct uart_8250_port *up = up_to_u8250p(port);
L
Linus Torvalds 已提交
2017 2018
	unsigned long flags;

2019
	serial8250_rpm_get(up);
2020
	spin_lock_irqsave(&port->lock, flags);
L
Linus Torvalds 已提交
2021 2022 2023 2024
	if (break_state == -1)
		up->lcr |= UART_LCR_SBC;
	else
		up->lcr &= ~UART_LCR_SBC;
2025
	serial_port_out(port, UART_LCR, up->lcr);
2026
	spin_unlock_irqrestore(&port->lock, flags);
2027
	serial8250_rpm_put(up);
L
Linus Torvalds 已提交
2028 2029
}

A
Alex Williamson 已提交
2030 2031 2032
/*
 *	Wait for transmitter & holding register to empty
 */
2033
static void wait_for_xmitr(struct uart_8250_port *up, int bits)
A
Alex Williamson 已提交
2034 2035 2036 2037
{
	unsigned int status, tmout = 10000;

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

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

2043 2044
		if ((status & bits) == bits)
			break;
A
Alex Williamson 已提交
2045 2046 2047
		if (--tmout == 0)
			break;
		udelay(1);
2048
	}
A
Alex Williamson 已提交
2049 2050 2051

	/* Wait up to 1s for flow control if necessary */
	if (up->port.flags & UPF_CONS_FLOW) {
2052 2053 2054 2055 2056 2057
		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 已提交
2058 2059 2060 2061 2062 2063
			udelay(1);
			touch_nmi_watchdog();
		}
	}
}

J
Jason Wessel 已提交
2064 2065 2066 2067 2068 2069 2070 2071
#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)
{
2072 2073 2074
	struct uart_8250_port *up = up_to_u8250p(port);
	unsigned char lsr;
	int status;
J
Jason Wessel 已提交
2075

2076
	serial8250_rpm_get(up);
J
Jason Wessel 已提交
2077

2078
	lsr = serial_port_in(port, UART_LSR);
J
Jason Wessel 已提交
2079

2080 2081 2082 2083
	if (!(lsr & UART_LSR_DR)) {
		status = NO_POLL_CHAR;
		goto out;
	}
J
Jason Wessel 已提交
2084

2085 2086 2087 2088
	status = serial_port_in(port, UART_RX);
out:
	serial8250_rpm_put(up);
	return status;
J
Jason Wessel 已提交
2089 2090 2091 2092 2093 2094 2095
}


static void serial8250_put_poll_char(struct uart_port *port,
			 unsigned char c)
{
	unsigned int ier;
2096
	struct uart_8250_port *up = up_to_u8250p(port);
J
Jason Wessel 已提交
2097

2098
	serial8250_rpm_get(up);
J
Jason Wessel 已提交
2099 2100 2101
	/*
	 *	First save the IER then disable the interrupts
	 */
2102
	ier = serial_port_in(port, UART_IER);
J
Jason Wessel 已提交
2103
	if (up->capabilities & UART_CAP_UUE)
2104
		serial_port_out(port, UART_IER, UART_IER_UUE);
J
Jason Wessel 已提交
2105
	else
2106
		serial_port_out(port, UART_IER, 0);
J
Jason Wessel 已提交
2107 2108 2109 2110 2111

	wait_for_xmitr(up, BOTH_EMPTY);
	/*
	 *	Send the character out.
	 */
2112
	serial_port_out(port, UART_TX, c);
J
Jason Wessel 已提交
2113 2114 2115 2116 2117 2118

	/*
	 *	Finally, wait for transmitter to become empty
	 *	and restore the IER
	 */
	wait_for_xmitr(up, BOTH_EMPTY);
2119
	serial_port_out(port, UART_IER, ier);
2120
	serial8250_rpm_put(up);
J
Jason Wessel 已提交
2121 2122 2123 2124
}

#endif /* CONFIG_CONSOLE_POLL */

2125
int serial8250_do_startup(struct uart_port *port)
L
Linus Torvalds 已提交
2126
{
2127
	struct uart_8250_port *up = up_to_u8250p(port);
L
Linus Torvalds 已提交
2128
	unsigned long flags;
2129
	unsigned char lsr, iir;
L
Linus Torvalds 已提交
2130 2131
	int retval;

S
Sean Young 已提交
2132 2133 2134
	if (port->type == PORT_8250_CIR)
		return -ENODEV;

2135 2136 2137 2138 2139 2140
	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 已提交
2141 2142
	up->mcr = 0;

2143
	if (port->iotype != up->cur_iotype)
A
Alan Cox 已提交
2144 2145
		set_io_from_upio(port);

2146
	serial8250_rpm_get(up);
2147
	if (port->type == PORT_16C950) {
L
Linus Torvalds 已提交
2148 2149
		/* Wake up and initialize UART */
		up->acr = 0;
2150 2151 2152 2153
		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 已提交
2154
		serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2155 2156 2157
		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 已提交
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168
	}

#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 已提交
2169
	 * (they will be reenabled in set_termios())
L
Linus Torvalds 已提交
2170 2171 2172 2173 2174 2175
	 */
	serial8250_clear_fifos(up);

	/*
	 * Clear the interrupt registers.
	 */
2176 2177
	serial_port_in(port, UART_LSR);
	serial_port_in(port, UART_RX);
2178 2179
	serial_port_in(port, UART_IIR);
	serial_port_in(port, UART_MSR);
L
Linus Torvalds 已提交
2180 2181 2182 2183 2184 2185

	/*
	 * 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.
	 */
2186
	if (!(port->flags & UPF_BUGGY_UART) &&
2187
	    (serial_port_in(port, UART_LSR) == 0xff)) {
2188
		printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2189
				   serial_index(port));
2190 2191
		retval = -ENODEV;
		goto out;
L
Linus Torvalds 已提交
2192 2193 2194 2195 2196
	}

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

2200
		serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
L
Linus Torvalds 已提交
2201

2202
		fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2203 2204 2205 2206 2207 2208
		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 已提交
2209

2210
		serial_port_out(port, UART_LCR, 0);
L
Linus Torvalds 已提交
2211 2212
	}

2213
	if (port->irq) {
2214
		unsigned char iir1;
A
Alex Williamson 已提交
2215 2216 2217 2218 2219 2220 2221 2222
		/*
		 * 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.
		 */
2223
		spin_lock_irqsave(&port->lock, flags);
2224
		if (up->port.irqflags & IRQF_SHARED)
2225
			disable_irq_nosync(port->irq);
A
Alex Williamson 已提交
2226 2227

		wait_for_xmitr(up, UART_LSR_THRE);
2228
		serial_port_out_sync(port, UART_IER, UART_IER_THRI);
A
Alex Williamson 已提交
2229
		udelay(1); /* allow THRE to set */
2230 2231
		iir1 = serial_port_in(port, UART_IIR);
		serial_port_out(port, UART_IER, 0);
2232
		serial_port_out_sync(port, UART_IER, UART_IER_THRI);
A
Alex Williamson 已提交
2233
		udelay(1); /* allow a working UART time to re-assert THRE */
2234 2235
		iir = serial_port_in(port, UART_IIR);
		serial_port_out(port, UART_IER, 0);
A
Alex Williamson 已提交
2236

2237 2238 2239
		if (port->irqflags & IRQF_SHARED)
			enable_irq(port->irq);
		spin_unlock_irqrestore(&port->lock, flags);
A
Alex Williamson 已提交
2240 2241

		/*
2242 2243 2244
		 * 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 已提交
2245
		 */
2246 2247
		if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
		    up->port.flags & UPF_BUG_THRE) {
2248
			up->bugs |= UART_BUG_THRE;
A
Alex Williamson 已提交
2249 2250 2251
		}
	}

2252 2253 2254
	retval = up->ops->setup_irq(up);
	if (retval)
		goto out;
L
Linus Torvalds 已提交
2255 2256 2257 2258

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

2261
	spin_lock_irqsave(&port->lock, flags);
L
Linus Torvalds 已提交
2262
	if (up->port.flags & UPF_FOURPORT) {
A
Alan Cox 已提交
2263
		if (!up->port.irq)
L
Linus Torvalds 已提交
2264 2265 2266 2267 2268
			up->port.mctrl |= TIOCM_OUT1;
	} else
		/*
		 * Most PC uarts need OUT2 raised to enable interrupts.
		 */
2269
		if (port->irq)
L
Linus Torvalds 已提交
2270 2271
			up->port.mctrl |= TIOCM_OUT2;

2272
	serial8250_set_mctrl(port, port->mctrl);
2273

2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
	/* 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.
	 */
2285
	if (up->port.flags & UPF_NO_TXEN_TEST)
2286 2287
		goto dont_test_tx_en;

2288 2289 2290 2291
	/*
	 * Do a quick test to see if we receive an
	 * interrupt when we enable the TX irq.
	 */
2292 2293 2294 2295
	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);
2296 2297

	if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2298 2299
		if (!(up->bugs & UART_BUG_TXEN)) {
			up->bugs |= UART_BUG_TXEN;
2300
			pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2301
				 serial_index(port));
2302 2303
		}
	} else {
2304
		up->bugs &= ~UART_BUG_TXEN;
2305 2306
	}

2307
dont_test_tx_en:
2308
	spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
2309

2310 2311 2312 2313 2314
	/*
	 * Clear the interrupt registers again for luck, and clear the
	 * saved flags to avoid getting false values from polling
	 * routines or the previous session.
	 */
2315 2316
	serial_port_in(port, UART_LSR);
	serial_port_in(port, UART_RX);
2317 2318
	serial_port_in(port, UART_IIR);
	serial_port_in(port, UART_MSR);
2319 2320 2321
	up->lsr_saved_flags = 0;
	up->msr_saved_flags = 0;

2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
	/*
	 * 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 已提交
2334 2335 2336 2337 2338 2339
	/*
	 * 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;
2340
	serial_port_out(port, UART_IER, up->ier);
L
Linus Torvalds 已提交
2341

2342
	if (port->flags & UPF_FOURPORT) {
L
Linus Torvalds 已提交
2343 2344 2345 2346
		unsigned int icp;
		/*
		 * Enable interrupts on the AST Fourport board
		 */
2347
		icp = (port->iobase & 0xfe0) | 0x01f;
L
Linus Torvalds 已提交
2348
		outb_p(0x80, icp);
2349
		inb_p(icp);
L
Linus Torvalds 已提交
2350
	}
2351 2352 2353 2354
	retval = 0;
out:
	serial8250_rpm_put(up);
	return retval;
L
Linus Torvalds 已提交
2355
}
2356
EXPORT_SYMBOL_GPL(serial8250_do_startup);
L
Linus Torvalds 已提交
2357

2358 2359 2360 2361 2362
static int serial8250_startup(struct uart_port *port)
{
	if (port->startup)
		return port->startup(port);
	return serial8250_do_startup(port);
L
Linus Torvalds 已提交
2363 2364
}

2365
void serial8250_do_shutdown(struct uart_port *port)
L
Linus Torvalds 已提交
2366
{
2367
	struct uart_8250_port *up = up_to_u8250p(port);
L
Linus Torvalds 已提交
2368 2369
	unsigned long flags;

2370
	serial8250_rpm_get(up);
L
Linus Torvalds 已提交
2371 2372 2373 2374
	/*
	 * Disable interrupts from this port
	 */
	up->ier = 0;
2375
	serial_port_out(port, UART_IER, 0);
L
Linus Torvalds 已提交
2376

2377 2378 2379
	if (up->dma)
		serial8250_release_dma(up);

2380 2381
	spin_lock_irqsave(&port->lock, flags);
	if (port->flags & UPF_FOURPORT) {
L
Linus Torvalds 已提交
2382
		/* reset interrupts on the AST Fourport board */
2383 2384
		inb((port->iobase & 0xfe0) | 0x1f);
		port->mctrl |= TIOCM_OUT1;
L
Linus Torvalds 已提交
2385
	} else
2386
		port->mctrl &= ~TIOCM_OUT2;
L
Linus Torvalds 已提交
2387

2388 2389
	serial8250_set_mctrl(port, port->mctrl);
	spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
2390 2391 2392 2393

	/*
	 * Disable break condition and FIFOs
	 */
2394 2395
	serial_port_out(port, UART_LCR,
			serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
L
Linus Torvalds 已提交
2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408
	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.
	 */
2409
	serial_port_in(port, UART_RX);
2410
	serial8250_rpm_put(up);
L
Linus Torvalds 已提交
2411

2412
	up->ops->release_irq(up);
L
Linus Torvalds 已提交
2413
}
2414 2415 2416 2417 2418 2419 2420 2421 2422
EXPORT_SYMBOL_GPL(serial8250_do_shutdown);

static void serial8250_shutdown(struct uart_port *port)
{
	if (port->shutdown)
		port->shutdown(port);
	else
		serial8250_do_shutdown(port);
}
L
Linus Torvalds 已提交
2423

2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443
/*
 * XR17V35x UARTs have an extra fractional divisor register (DLD)
 * Calculate divisor with extra 4-bit fractional portion
 */
static unsigned int xr17v35x_get_divisor(struct uart_8250_port *up,
					 unsigned int baud,
					 unsigned int *frac)
{
	struct uart_port *port = &up->port;
	unsigned int quot_16;

	quot_16 = DIV_ROUND_CLOSEST(port->uartclk, baud);
	*frac = quot_16 & 0x0f;

	return quot_16 >> 4;
}

static unsigned int serial8250_get_divisor(struct uart_8250_port *up,
					   unsigned int baud,
					   unsigned int *frac)
L
Linus Torvalds 已提交
2444
{
2445
	struct uart_port *port = &up->port;
L
Linus Torvalds 已提交
2446 2447 2448 2449 2450
	unsigned int quot;

	/*
	 * Handle magic divisors for baud rates above baud_base on
	 * SMSC SuperIO chips.
2451
	 *
L
Linus Torvalds 已提交
2452 2453 2454 2455 2456 2457 2458
	 */
	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;
2459 2460
	else if (up->port.type == PORT_XR17V35X)
		quot = xr17v35x_get_divisor(up, baud, frac);
L
Linus Torvalds 已提交
2461 2462 2463
	else
		quot = uart_get_divisor(port, baud);

2464 2465 2466 2467 2468 2469
	/*
	 * Oxford Semi 952 rev B workaround
	 */
	if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
		quot++;

L
Linus Torvalds 已提交
2470 2471 2472
	return quot;
}

2473 2474
static unsigned char serial8250_compute_lcr(struct uart_8250_port *up,
					    tcflag_t c_cflag)
L
Linus Torvalds 已提交
2475
{
2476
	unsigned char cval;
L
Linus Torvalds 已提交
2477

2478
	switch (c_cflag & CSIZE) {
L
Linus Torvalds 已提交
2479
	case CS5:
2480
		cval = UART_LCR_WLEN5;
L
Linus Torvalds 已提交
2481 2482
		break;
	case CS6:
2483
		cval = UART_LCR_WLEN6;
L
Linus Torvalds 已提交
2484 2485
		break;
	case CS7:
2486
		cval = UART_LCR_WLEN7;
L
Linus Torvalds 已提交
2487 2488 2489
		break;
	default:
	case CS8:
2490
		cval = UART_LCR_WLEN8;
L
Linus Torvalds 已提交
2491 2492 2493
		break;
	}

2494
	if (c_cflag & CSTOPB)
2495
		cval |= UART_LCR_STOP;
2496
	if (c_cflag & PARENB) {
L
Linus Torvalds 已提交
2497
		cval |= UART_LCR_PARITY;
2498
		if (up->bugs & UART_BUG_PARITY)
2499
			up->fifo_bug = true;
2500
	}
2501
	if (!(c_cflag & PARODD))
L
Linus Torvalds 已提交
2502 2503
		cval |= UART_LCR_EPAR;
#ifdef CMSPAR
2504
	if (c_cflag & CMSPAR)
L
Linus Torvalds 已提交
2505 2506 2507
		cval |= UART_LCR_SPAR;
#endif

2508 2509 2510
	return cval;
}

2511
static void serial8250_set_divisor(struct uart_port *port, unsigned int baud,
2512
			    unsigned int quot, unsigned int quot_frac)
2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535
{
	struct uart_8250_port *up = up_to_u8250p(port);

	/* Workaround to enable 115200 baud on OMAP1510 internal ports */
	if (is_omap1510_8250(up)) {
		if (baud == 115200) {
			quot = 1;
			serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
		} else
			serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
	}

	/*
	 * 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, up->lcr | UART_LCR_DLAB);

	serial_dl_write(up, quot);

2536 2537 2538
	/* XR17V35x UARTs have an extra fractional divisor register (DLD) */
	if (up->port.type == PORT_XR17V35X)
		serial_port_out(port, 0x2, quot_frac);
2539 2540
}

2541 2542 2543 2544 2545 2546 2547
void
serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
		          struct ktermios *old)
{
	struct uart_8250_port *up = up_to_u8250p(port);
	unsigned char cval;
	unsigned long flags;
2548
	unsigned int baud, quot, frac = 0;
2549 2550 2551

	cval = serial8250_compute_lcr(up, termios->c_cflag);

L
Linus Torvalds 已提交
2552 2553 2554
	/*
	 * Ask the core to calculate the divisor for us.
	 */
2555 2556 2557
	baud = uart_get_baud_rate(port, termios, old,
				  port->uartclk / 16 / 0xffff,
				  port->uartclk / 16);
2558
	quot = serial8250_get_divisor(up, baud, &frac);
L
Linus Torvalds 已提交
2559

2560 2561 2562 2563 2564 2565 2566 2567 2568
	/*
	 * Ok, we're now changing the port state.  Do it with
	 * interrupts disabled.
	 */
	serial8250_rpm_get(up);
	spin_lock_irqsave(&port->lock, flags);

	up->lcr = cval;					/* Save computed LCR */

2569
	if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
2570 2571 2572 2573
		/* NOTE: If fifo_bug is not set, a user can set RX_trigger. */
		if ((baud < 2400 && !up->dma) || up->fifo_bug) {
			up->fcr &= ~UART_FCR_TRIGGER_MASK;
			up->fcr |= UART_FCR_TRIGGER_1;
2574
		}
L
Linus Torvalds 已提交
2575 2576 2577 2578 2579 2580 2581 2582
	}

	/*
	 * 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
2583
	 * UART to respond.  IOW, at least 32 bytes of FIFO.
L
Linus Torvalds 已提交
2584
	 */
2585
	if (up->capabilities & UART_CAP_AFE && port->fifosize >= 32) {
L
Linus Torvalds 已提交
2586 2587 2588 2589 2590 2591 2592 2593 2594 2595
		up->mcr &= ~UART_MCR_AFE;
		if (termios->c_cflag & CRTSCTS)
			up->mcr |= UART_MCR_AFE;
	}

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

2596
	port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
L
Linus Torvalds 已提交
2597
	if (termios->c_iflag & INPCK)
2598
		port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
P
Peter Hurley 已提交
2599
	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2600
		port->read_status_mask |= UART_LSR_BI;
L
Linus Torvalds 已提交
2601 2602 2603 2604

	/*
	 * Characteres to ignore
	 */
2605
	port->ignore_status_mask = 0;
L
Linus Torvalds 已提交
2606
	if (termios->c_iflag & IGNPAR)
2607
		port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
L
Linus Torvalds 已提交
2608
	if (termios->c_iflag & IGNBRK) {
2609
		port->ignore_status_mask |= UART_LSR_BI;
L
Linus Torvalds 已提交
2610 2611 2612 2613 2614
		/*
		 * If we're ignoring parity and break indicators,
		 * ignore overruns too (for real raw support).
		 */
		if (termios->c_iflag & IGNPAR)
2615
			port->ignore_status_mask |= UART_LSR_OE;
L
Linus Torvalds 已提交
2616 2617 2618 2619 2620 2621
	}

	/*
	 * ignore all characters if CREAD is not set
	 */
	if ((termios->c_cflag & CREAD) == 0)
2622
		port->ignore_status_mask |= UART_LSR_DR;
L
Linus Torvalds 已提交
2623 2624 2625 2626

	/*
	 * CTS flow control flag and modem status interrupts
	 */
2627
	up->ier &= ~UART_IER_MSI;
2628 2629
	if (!(up->bugs & UART_BUG_NOMSR) &&
			UART_ENABLE_MS(&up->port, termios->c_cflag))
L
Linus Torvalds 已提交
2630 2631
		up->ier |= UART_IER_MSI;
	if (up->capabilities & UART_CAP_UUE)
2632 2633 2634
		up->ier |= UART_IER_UUE;
	if (up->capabilities & UART_CAP_RTOIE)
		up->ier |= UART_IER_RTOIE;
L
Linus Torvalds 已提交
2635

2636
	serial_port_out(port, UART_IER, up->ier);
L
Linus Torvalds 已提交
2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647

	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;

2648
		serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2649
		if (port->flags & UPF_EXAR_EFR)
2650
			serial_port_out(port, UART_XR_EFR, efr);
2651
		else
2652
			serial_port_out(port, UART_EFR, efr);
L
Linus Torvalds 已提交
2653 2654
	}

2655
	serial8250_set_divisor(port, baud, quot, frac);
2656

L
Linus Torvalds 已提交
2657 2658 2659 2660
	/*
	 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
	 * is written without DLAB set, this mode will be disabled.
	 */
2661
	if (port->type == PORT_16750)
2662
		serial_port_out(port, UART_FCR, up->fcr);
L
Linus Torvalds 已提交
2663

2664
	serial_port_out(port, UART_LCR, up->lcr);	/* reset DLAB */
2665
	if (port->type != PORT_16750) {
2666
		/* emulated UARTs (Lucent Venus 167x) need two steps */
2667
		if (up->fcr & UART_FCR_ENABLE_FIFO)
2668
			serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
2669
		serial_port_out(port, UART_FCR, up->fcr);	/* set fcr */
L
Linus Torvalds 已提交
2670
	}
2671 2672
	serial8250_set_mctrl(port, port->mctrl);
	spin_unlock_irqrestore(&port->lock, flags);
2673 2674
	serial8250_rpm_put(up);

A
Alan Cox 已提交
2675 2676 2677
	/* Don't rewrite B0 */
	if (tty_termios_baud_rate(termios))
		tty_termios_encode_baud_rate(termios, baud, baud);
L
Linus Torvalds 已提交
2678
}
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
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 已提交
2690

2691
static void
2692
serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios)
2693
{
2694
	if (termios->c_line == N_PPS) {
2695
		port->flags |= UPF_HARDPPS_CD;
2696
		spin_lock_irq(&port->lock);
2697
		serial8250_enable_ms(port);
2698
		spin_unlock_irq(&port->lock);
2699
	} else {
2700
		port->flags &= ~UPF_HARDPPS_CD;
2701 2702 2703 2704 2705 2706
		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
			spin_lock_irq(&port->lock);
			serial8250_disable_ms(port);
			spin_unlock_irq(&port->lock);
		}
	}
2707 2708
}

2709 2710 2711

void serial8250_do_pm(struct uart_port *port, unsigned int state,
		      unsigned int oldstate)
L
Linus Torvalds 已提交
2712
{
2713
	struct uart_8250_port *p = up_to_u8250p(port);
L
Linus Torvalds 已提交
2714 2715

	serial8250_set_sleep(p, state != 0);
2716 2717
}
EXPORT_SYMBOL(serial8250_do_pm);
L
Linus Torvalds 已提交
2718

2719 2720 2721 2722 2723 2724 2725 2726
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 已提交
2727 2728
}

2729 2730
static unsigned int serial8250_port_size(struct uart_8250_port *pt)
{
2731 2732
	if (pt->port.mapsize)
		return pt->port.mapsize;
2733 2734 2735
	if (pt->port.iotype == UPIO_AU) {
		if (pt->port.type == PORT_RT2880)
			return 0x100;
2736
		return 0x1000;
2737
	}
2738
	if (is_omap1_8250(pt))
2739
		return 0x16 << pt->port.regshift;
2740

2741 2742 2743
	return 8 << pt->port.regshift;
}

L
Linus Torvalds 已提交
2744 2745 2746 2747 2748
/*
 * Resource handling.
 */
static int serial8250_request_std_resource(struct uart_8250_port *up)
{
2749
	unsigned int size = serial8250_port_size(up);
2750
	struct uart_port *port = &up->port;
L
Linus Torvalds 已提交
2751 2752
	int ret = 0;

2753
	switch (port->iotype) {
2754
	case UPIO_AU:
2755 2756
	case UPIO_TSI:
	case UPIO_MEM32:
2757
	case UPIO_MEM32BE:
L
Linus Torvalds 已提交
2758
	case UPIO_MEM:
2759
		if (!port->mapbase)
L
Linus Torvalds 已提交
2760 2761
			break;

2762
		if (!request_mem_region(port->mapbase, size, "serial")) {
L
Linus Torvalds 已提交
2763 2764 2765 2766
			ret = -EBUSY;
			break;
		}

2767 2768 2769 2770
		if (port->flags & UPF_IOREMAP) {
			port->membase = ioremap_nocache(port->mapbase, size);
			if (!port->membase) {
				release_mem_region(port->mapbase, size);
L
Linus Torvalds 已提交
2771 2772 2773 2774 2775 2776 2777
				ret = -ENOMEM;
			}
		}
		break;

	case UPIO_HUB6:
	case UPIO_PORT:
2778
		if (!request_region(port->iobase, size, "serial"))
L
Linus Torvalds 已提交
2779 2780 2781 2782 2783 2784 2785 2786
			ret = -EBUSY;
		break;
	}
	return ret;
}

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

2790
	switch (port->iotype) {
2791
	case UPIO_AU:
2792 2793
	case UPIO_TSI:
	case UPIO_MEM32:
2794
	case UPIO_MEM32BE:
L
Linus Torvalds 已提交
2795
	case UPIO_MEM:
2796
		if (!port->mapbase)
L
Linus Torvalds 已提交
2797 2798
			break;

2799 2800 2801
		if (port->flags & UPF_IOREMAP) {
			iounmap(port->membase);
			port->membase = NULL;
L
Linus Torvalds 已提交
2802 2803
		}

2804
		release_mem_region(port->mapbase, size);
L
Linus Torvalds 已提交
2805 2806 2807 2808
		break;

	case UPIO_HUB6:
	case UPIO_PORT:
2809
		release_region(port->iobase, size);
L
Linus Torvalds 已提交
2810 2811 2812 2813
		break;
	}
}

2814
#ifdef CONFIG_SERIAL_8250_RSA
L
Linus Torvalds 已提交
2815 2816 2817 2818
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;
2819
	struct uart_port *port = &up->port;
2820
	int ret = -EINVAL;
L
Linus Torvalds 已提交
2821

2822
	switch (port->iotype) {
L
Linus Torvalds 已提交
2823 2824
	case UPIO_HUB6:
	case UPIO_PORT:
2825
		start += port->iobase;
2826 2827 2828
		if (request_region(start, size, "serial-rsa"))
			ret = 0;
		else
L
Linus Torvalds 已提交
2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839
			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;
2840
	struct uart_port *port = &up->port;
L
Linus Torvalds 已提交
2841

2842
	switch (port->iotype) {
L
Linus Torvalds 已提交
2843 2844
	case UPIO_HUB6:
	case UPIO_PORT:
2845
		release_region(port->iobase + offset, size);
L
Linus Torvalds 已提交
2846 2847 2848
		break;
	}
}
2849
#endif
L
Linus Torvalds 已提交
2850 2851 2852

static void serial8250_release_port(struct uart_port *port)
{
2853
	struct uart_8250_port *up = up_to_u8250p(port);
L
Linus Torvalds 已提交
2854 2855 2856 2857 2858 2859

	serial8250_release_std_resource(up);
}

static int serial8250_request_port(struct uart_port *port)
{
2860
	struct uart_8250_port *up = up_to_u8250p(port);
S
Sean Young 已提交
2861 2862 2863 2864
	int ret;

	if (port->type == PORT_8250_CIR)
		return -ENODEV;
L
Linus Torvalds 已提交
2865 2866 2867 2868 2869 2870

	ret = serial8250_request_std_resource(up);

	return ret;
}

2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010
static int fcr_get_rxtrig_bytes(struct uart_8250_port *up)
{
	const struct serial8250_config *conf_type = &uart_config[up->port.type];
	unsigned char bytes;

	bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)];

	return bytes ? bytes : -EOPNOTSUPP;
}

static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes)
{
	const struct serial8250_config *conf_type = &uart_config[up->port.type];
	int i;

	if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)])
		return -EOPNOTSUPP;

	for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) {
		if (bytes < conf_type->rxtrig_bytes[i])
			/* Use the nearest lower value */
			return (--i) << UART_FCR_R_TRIG_SHIFT;
	}

	return UART_FCR_R_TRIG_11;
}

static int do_get_rxtrig(struct tty_port *port)
{
	struct uart_state *state = container_of(port, struct uart_state, port);
	struct uart_port *uport = state->uart_port;
	struct uart_8250_port *up =
		container_of(uport, struct uart_8250_port, port);

	if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
		return -EINVAL;

	return fcr_get_rxtrig_bytes(up);
}

static int do_serial8250_get_rxtrig(struct tty_port *port)
{
	int rxtrig_bytes;

	mutex_lock(&port->mutex);
	rxtrig_bytes = do_get_rxtrig(port);
	mutex_unlock(&port->mutex);

	return rxtrig_bytes;
}

static ssize_t serial8250_get_attr_rx_trig_bytes(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct tty_port *port = dev_get_drvdata(dev);
	int rxtrig_bytes;

	rxtrig_bytes = do_serial8250_get_rxtrig(port);
	if (rxtrig_bytes < 0)
		return rxtrig_bytes;

	return snprintf(buf, PAGE_SIZE, "%d\n", rxtrig_bytes);
}

static int do_set_rxtrig(struct tty_port *port, unsigned char bytes)
{
	struct uart_state *state = container_of(port, struct uart_state, port);
	struct uart_port *uport = state->uart_port;
	struct uart_8250_port *up =
		container_of(uport, struct uart_8250_port, port);
	int rxtrig;

	if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1 ||
	    up->fifo_bug)
		return -EINVAL;

	rxtrig = bytes_to_fcr_rxtrig(up, bytes);
	if (rxtrig < 0)
		return rxtrig;

	serial8250_clear_fifos(up);
	up->fcr &= ~UART_FCR_TRIGGER_MASK;
	up->fcr |= (unsigned char)rxtrig;
	serial_out(up, UART_FCR, up->fcr);
	return 0;
}

static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes)
{
	int ret;

	mutex_lock(&port->mutex);
	ret = do_set_rxtrig(port, bytes);
	mutex_unlock(&port->mutex);

	return ret;
}

static ssize_t serial8250_set_attr_rx_trig_bytes(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
{
	struct tty_port *port = dev_get_drvdata(dev);
	unsigned char bytes;
	int ret;

	if (!count)
		return -EINVAL;

	ret = kstrtou8(buf, 10, &bytes);
	if (ret < 0)
		return ret;

	ret = do_serial8250_set_rxtrig(port, bytes);
	if (ret < 0)
		return ret;

	return count;
}

static DEVICE_ATTR(rx_trig_bytes, S_IRUSR | S_IWUSR | S_IRGRP,
		   serial8250_get_attr_rx_trig_bytes,
		   serial8250_set_attr_rx_trig_bytes);

static struct attribute *serial8250_dev_attrs[] = {
	&dev_attr_rx_trig_bytes.attr,
	NULL,
	};

static struct attribute_group serial8250_dev_attr_group = {
	.attrs = serial8250_dev_attrs,
	};

static void register_dev_spec_attr_grp(struct uart_8250_port *up)
{
	const struct serial8250_config *conf_type = &uart_config[up->port.type];

	if (conf_type->rxtrig_bytes[0])
		up->port.attr_group = &serial8250_dev_attr_group;
}

L
Linus Torvalds 已提交
3011 3012
static void serial8250_config_port(struct uart_port *port, int flags)
{
3013
	struct uart_8250_port *up = up_to_u8250p(port);
L
Linus Torvalds 已提交
3014 3015
	int ret;

S
Sean Young 已提交
3016 3017 3018
	if (port->type == PORT_8250_CIR)
		return;

L
Linus Torvalds 已提交
3019 3020 3021 3022 3023 3024 3025 3026
	/*
	 * 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;

3027
	if (port->iotype != up->cur_iotype)
A
Alan Cox 已提交
3028 3029
		set_io_from_upio(port);

L
Linus Torvalds 已提交
3030
	if (flags & UART_CONFIG_TYPE)
P
Peter Hurley 已提交
3031
		autoconfig(up);
3032 3033

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

3037 3038 3039 3040
	/* HW bugs may trigger IRQ while IIR == NO_INT */
	if (port->type == PORT_TEGRA)
		up->bugs |= UART_BUG_NOMSR;

3041
	if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
L
Linus Torvalds 已提交
3042 3043
		autoconfig_irq(up);

3044
	if (port->type == PORT_UNKNOWN)
L
Linus Torvalds 已提交
3045
		serial8250_release_std_resource(up);
3046 3047

	/* Fixme: probably not the best place for this */
3048 3049
	if ((port->type == PORT_XR17V35X) ||
	   (port->type == PORT_XR17D15X))
3050
		port->handle_irq = exar_handle_irq;
3051 3052 3053

	register_dev_spec_attr_grp(up);
	up->fcr = uart_config[up->port.type].fcr;
L
Linus Torvalds 已提交
3054 3055 3056 3057 3058
}

static int
serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
{
Y
Yinghai Lu 已提交
3059
	if (ser->irq >= nr_irqs || ser->irq < 0 ||
L
Linus Torvalds 已提交
3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076
	    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;
}

3077
static const struct uart_ops serial8250_pops = {
L
Linus Torvalds 已提交
3078 3079 3080 3081 3082
	.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,
3083 3084
	.throttle	= serial8250_throttle,
	.unthrottle	= serial8250_unthrottle,
L
Linus Torvalds 已提交
3085 3086 3087 3088 3089 3090
	.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,
3091
	.set_ldisc	= serial8250_set_ldisc,
L
Linus Torvalds 已提交
3092 3093 3094 3095 3096 3097
	.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 已提交
3098 3099 3100 3101
#ifdef CONFIG_CONSOLE_POLL
	.poll_get_char = serial8250_get_poll_char,
	.poll_put_char = serial8250_put_poll_char,
#endif
L
Linus Torvalds 已提交
3102 3103
};

P
Peter Hurley 已提交
3104 3105 3106
static const struct uart_ops *base_ops;
static struct uart_ops univ8250_port_ops;

3107 3108 3109 3110 3111
static const struct uart_8250_ops univ8250_driver_ops = {
	.setup_irq	= univ8250_setup_irq,
	.release_irq	= univ8250_release_irq,
};

L
Linus Torvalds 已提交
3112 3113
static struct uart_8250_port serial8250_ports[UART_NR];

3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131
/**
 * serial8250_get_port - retrieve struct uart_8250_port
 * @line: serial line number
 *
 * This function retrieves struct uart_8250_port for the specific line.
 * This struct *must* *not* be used to perform a 8250 or serial core operation
 * which is not accessible otherwise. Its only purpose is to make the struct
 * accessible to the runtime-pm callbacks for context suspend/restore.
 * The lock assumption made here is none because runtime-pm suspend/resume
 * callbacks should not be invoked if there is any operation performed on the
 * port.
 */
struct uart_8250_port *serial8250_get_port(int line)
{
	return &serial8250_ports[line];
}
EXPORT_SYMBOL_GPL(serial8250_get_port);

3132 3133 3134 3135 3136 3137 3138 3139 3140 3141
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);

3142 3143 3144 3145 3146 3147 3148 3149 3150 3151
static void serial8250_init_port(struct uart_8250_port *up)
{
	struct uart_port *port = &up->port;

	spin_lock_init(&port->lock);
	port->ops = &serial8250_pops;

	up->cur_iotype = 0xFF;
}

3152 3153 3154 3155
static void serial8250_set_defaults(struct uart_8250_port *up)
{
	struct uart_port *port = &up->port;

3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166
	if (up->port.flags & UPF_FIXED_TYPE) {
		unsigned int type = up->port.type;

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

3167
	set_io_from_upio(port);
3168 3169 3170 3171 3172 3173 3174 3175

	/* default dma handlers */
	if (up->dma) {
		if (!up->dma->tx_dma)
			up->dma->tx_dma = serial8250_tx_dma;
		if (!up->dma->rx_dma)
			up->dma->rx_dma = serial8250_rx_dma;
	}
3176 3177
}

P
Peter Hurley 已提交
3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240
#ifdef CONFIG_SERIAL_8250_RSA

static void univ8250_config_port(struct uart_port *port, int flags)
{
	struct uart_8250_port *up = up_to_u8250p(port);

	up->probe &= ~UART_PROBE_RSA;
	if (port->type == PORT_RSA) {
		if (serial8250_request_rsa_resource(up) == 0)
			up->probe |= UART_PROBE_RSA;
	} else if (flags & UART_CONFIG_TYPE) {
		int i;

		for (i = 0; i < probe_rsa_count; i++) {
			if (probe_rsa[i] == up->port.iobase) {
				if (serial8250_request_rsa_resource(up) == 0)
					up->probe |= UART_PROBE_RSA;
				break;
			}
		}
	}

	base_ops->config_port(port, flags);

	if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
		serial8250_release_rsa_resource(up);
}

static int univ8250_request_port(struct uart_port *port)
{
	struct uart_8250_port *up = up_to_u8250p(port);
	int ret;

	ret = base_ops->request_port(port);
	if (ret == 0 && port->type == PORT_RSA) {
		ret = serial8250_request_rsa_resource(up);
		if (ret < 0)
			base_ops->release_port(port);
	}

	return ret;
}

static void univ8250_release_port(struct uart_port *port)
{
	struct uart_8250_port *up = up_to_u8250p(port);

	if (port->type == PORT_RSA)
		serial8250_release_rsa_resource(up);
	base_ops->release_port(port);
}

static void univ8250_rsa_support(struct uart_ops *ops)
{
	ops->config_port  = univ8250_config_port;
	ops->request_port = univ8250_request_port;
	ops->release_port = univ8250_release_port;
}

#else
#define univ8250_rsa_support(x)		do { } while (0)
#endif /* CONFIG_SERIAL_8250_RSA */

L
Linus Torvalds 已提交
3241 3242 3243 3244
static void __init serial8250_isa_init_ports(void)
{
	struct uart_8250_port *up;
	static int first = 1;
3245
	int i, irqflag = 0;
L
Linus Torvalds 已提交
3246 3247 3248 3249 3250

	if (!first)
		return;
	first = 0;

3251 3252 3253
	if (nr_uarts > UART_NR)
		nr_uarts = UART_NR;

3254
	for (i = 0; i < nr_uarts; i++) {
L
Linus Torvalds 已提交
3255
		struct uart_8250_port *up = &serial8250_ports[i];
3256
		struct uart_port *port = &up->port;
L
Linus Torvalds 已提交
3257

3258
		port->line = i;
3259
		serial8250_init_port(up);
P
Peter Hurley 已提交
3260 3261 3262
		if (!base_ops)
			base_ops = port->ops;
		port->ops = &univ8250_port_ops;
L
Linus Torvalds 已提交
3263 3264 3265 3266

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

3267 3268
		up->ops = &univ8250_driver_ops;

L
Linus Torvalds 已提交
3269 3270 3271 3272 3273 3274 3275
		/*
		 * ALPHA_KLUDGE_MCR needs to be killed.
		 */
		up->mcr_mask = ~ALPHA_KLUDGE_MCR;
		up->mcr_force = ALPHA_KLUDGE_MCR;
	}

P
Peter Hurley 已提交
3276 3277 3278 3279
	/* chain base port ops to support Remote Supervisor Adapter */
	univ8250_port_ops = *base_ops;
	univ8250_rsa_support(&univ8250_port_ops);

3280 3281 3282
	if (share_irqs)
		irqflag = IRQF_SHARED;

3283
	for (i = 0, up = serial8250_ports;
3284
	     i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
L
Linus Torvalds 已提交
3285
	     i++, up++) {
3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296
		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;
3297 3298
		serial8250_set_defaults(up);

3299
		port->irqflags |= irqflag;
3300 3301
		if (serial8250_isa_config != NULL)
			serial8250_isa_config(i, &up->port, &up->capabilities);
L
Linus Torvalds 已提交
3302 3303 3304 3305 3306 3307 3308 3309
	}
}

static void __init
serial8250_register_ports(struct uart_driver *drv, struct device *dev)
{
	int i;

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

3313 3314
		if (up->port.dev)
			continue;
L
Linus Torvalds 已提交
3315 3316

		up->port.dev = dev;
3317

3318 3319 3320
		if (skip_txen_test)
			up->port.flags |= UPF_NO_TXEN_TEST;

L
Linus Torvalds 已提交
3321 3322 3323 3324 3325 3326
		uart_add_one_port(drv, &up->port);
	}
}

#ifdef CONFIG_SERIAL_8250_CONSOLE

3327 3328
static void serial8250_console_putchar(struct uart_port *port, int ch)
{
3329
	struct uart_8250_port *up = up_to_u8250p(port);
3330 3331

	wait_for_xmitr(up, UART_LSR_THRE);
3332
	serial_port_out(port, UART_TX, ch);
3333 3334
}

L
Linus Torvalds 已提交
3335 3336 3337 3338 3339 3340
/*
 *	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.
 */
3341 3342
static void serial8250_console_write(struct uart_8250_port *up, const char *s,
				     unsigned int count)
L
Linus Torvalds 已提交
3343
{
3344
	struct uart_port *port = &up->port;
3345
	unsigned long flags;
L
Linus Torvalds 已提交
3346
	unsigned int ier;
3347
	int locked = 1;
L
Linus Torvalds 已提交
3348

3349 3350
	touch_nmi_watchdog();

3351 3352
	serial8250_rpm_get(up);

3353 3354 3355
	if (port->sysrq)
		locked = 0;
	else if (oops_in_progress)
3356 3357 3358
		locked = spin_trylock_irqsave(&port->lock, flags);
	else
		spin_lock_irqsave(&port->lock, flags);
3359

L
Linus Torvalds 已提交
3360
	/*
R
Ralf Baechle 已提交
3361
	 *	First save the IER then disable the interrupts
L
Linus Torvalds 已提交
3362
	 */
3363
	ier = serial_port_in(port, UART_IER);
L
Linus Torvalds 已提交
3364 3365

	if (up->capabilities & UART_CAP_UUE)
3366
		serial_port_out(port, UART_IER, UART_IER_UUE);
L
Linus Torvalds 已提交
3367
	else
3368
		serial_port_out(port, UART_IER, 0);
L
Linus Torvalds 已提交
3369

3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390
	/* check scratch reg to see if port powered off during system sleep */
	if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) {
		struct ktermios termios;
		unsigned int baud, quot, frac = 0;

		termios.c_cflag = port->cons->cflag;
		if (port->state->port.tty && termios.c_cflag == 0)
			termios.c_cflag = port->state->port.tty->termios.c_cflag;

		baud = uart_get_baud_rate(port, &termios, NULL,
					  port->uartclk / 16 / 0xffff,
					  port->uartclk / 16);
		quot = serial8250_get_divisor(up, baud, &frac);

		serial8250_set_divisor(port, baud, quot, frac);
		serial_port_out(port, UART_LCR, up->lcr);
		serial_port_out(port, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);

		up->canary = 0;
	}

3391
	uart_console_write(port, s, count, serial8250_console_putchar);
L
Linus Torvalds 已提交
3392 3393 3394 3395 3396

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

3400 3401 3402 3403 3404 3405 3406 3407
	/*
	 *	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)
3408
		serial8250_modem_status(up);
3409

3410
	if (locked)
3411
		spin_unlock_irqrestore(&port->lock, flags);
3412
	serial8250_rpm_put(up);
L
Linus Torvalds 已提交
3413 3414
}

3415 3416 3417 3418 3419 3420 3421 3422
static void univ8250_console_write(struct console *co, const char *s,
				   unsigned int count)
{
	struct uart_8250_port *up = &serial8250_ports[co->index];

	serial8250_console_write(up, s, count);
}

3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438
static unsigned int probe_baud(struct uart_port *port)
{
	unsigned char lcr, dll, dlm;
	unsigned int quot;

	lcr = serial_port_in(port, UART_LCR);
	serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB);
	dll = serial_port_in(port, UART_DLL);
	dlm = serial_port_in(port, UART_DLM);
	serial_port_out(port, UART_LCR, lcr);

	quot = (dlm << 8) | dll;
	return (port->uartclk / 16) / quot;
}

static int serial8250_console_setup(struct uart_port *port, char *options, bool probe)
L
Linus Torvalds 已提交
3439 3440 3441 3442 3443 3444
{
	int baud = 9600;
	int bits = 8;
	int parity = 'n';
	int flow = 'n';

3445 3446 3447 3448 3449
	if (!port->iobase && !port->membase)
		return -ENODEV;

	if (options)
		uart_parse_options(options, &baud, &parity, &bits, &flow);
3450 3451
	else if (probe)
		baud = probe_baud(port);
3452 3453 3454 3455 3456 3457

	return uart_set_options(port, port->cons, baud, parity, bits, flow);
}

static int univ8250_console_setup(struct console *co, char *options)
{
3458
	struct uart_port *port;
3459

L
Linus Torvalds 已提交
3460 3461 3462 3463 3464
	/*
	 * Check whether an invalid uart number has been specified, and
	 * if so, search for the first available port that does have
	 * console support.
	 */
3465
	if (co->index >= nr_uarts)
L
Linus Torvalds 已提交
3466
		co->index = 0;
3467
	port = &serial8250_ports[co->index].port;
3468
	/* link port to console */
3469
	port->cons = co;
L
Linus Torvalds 已提交
3470

3471
	return serial8250_console_setup(port, options, false);
L
Linus Torvalds 已提交
3472 3473
}

3474
/**
3475
 *	univ8250_console_match - non-standard console matching
3476 3477 3478 3479 3480 3481
 *	@co:	  registering console
 *	@name:	  name from console command line
 *	@idx:	  index from console command line
 *	@options: ptr to option string from console command line
 *
 *	Only attempts to match console command lines of the form:
3482 3483
 *	    console=uart[8250],io|mmio|mmio32,<addr>[,<options>]
 *	    console=uart[8250],0x<addr>[,<options>]
3484 3485 3486 3487
 *	This form is used to register an initial earlycon boot console and
 *	replace it with the serial8250_console at 8250 driver init.
 *
 *	Performs console setup for a match (as required by interface)
3488
 *	If no <options> are specified, then assume the h/w is already setup.
3489 3490 3491
 *
 *	Returns 0 if console matches; otherwise non-zero to use default matching
 */
3492 3493
static int univ8250_console_match(struct console *co, char *name, int idx,
				  char *options)
3494
{
3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518
	char match[] = "uart";	/* 8250-specific earlycon name */
	unsigned char iotype;
	unsigned long addr;
	int i;

	if (strncmp(name, match, 4) != 0)
		return -ENODEV;

	if (uart_parse_earlycon(options, &iotype, &addr, &options))
		return -ENODEV;

	/* try to match the port specified on the command line */
	for (i = 0; i < nr_uarts; i++) {
		struct uart_port *port = &serial8250_ports[i].port;

		if (port->iotype != iotype)
			continue;
		if ((iotype == UPIO_MEM || iotype == UPIO_MEM32) &&
		    (port->mapbase != addr))
			continue;
		if (iotype == UPIO_PORT && port->iobase != addr)
			continue;

		co->index = i;
3519 3520
		port->cons = co;
		return serial8250_console_setup(port, options, true);
3521 3522 3523
	}

	return -ENODEV;
3524 3525
}

3526
static struct console univ8250_console = {
L
Linus Torvalds 已提交
3527
	.name		= "ttyS",
3528
	.write		= univ8250_console_write,
L
Linus Torvalds 已提交
3529
	.device		= uart_console_device,
3530 3531
	.setup		= univ8250_console_setup,
	.match		= univ8250_console_match,
3532
	.flags		= CON_PRINTBUFFER | CON_ANYTIME,
L
Linus Torvalds 已提交
3533 3534 3535 3536
	.index		= -1,
	.data		= &serial8250_reg,
};

3537
static int __init univ8250_console_init(void)
L
Linus Torvalds 已提交
3538
{
3539 3540 3541
	if (nr_uarts == 0)
		return -ENODEV;

L
Linus Torvalds 已提交
3542
	serial8250_isa_init_ports();
3543
	register_console(&univ8250_console);
L
Linus Torvalds 已提交
3544 3545
	return 0;
}
3546
console_initcall(univ8250_console_init);
L
Linus Torvalds 已提交
3547

3548
#define SERIAL8250_CONSOLE	&univ8250_console
L
Linus Torvalds 已提交
3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561
#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,
};

3562 3563 3564 3565 3566 3567
/*
 * 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 已提交
3568 3569
int __init early_serial_setup(struct uart_port *port)
{
D
David Daney 已提交
3570 3571
	struct uart_port *p;

3572
	if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
L
Linus Torvalds 已提交
3573 3574 3575
		return -ENODEV;

	serial8250_isa_init_ports();
D
David Daney 已提交
3576 3577 3578 3579
	p = &serial8250_ports[port->line].port;
	p->iobase       = port->iobase;
	p->membase      = port->membase;
	p->irq          = port->irq;
3580
	p->irqflags     = port->irqflags;
D
David Daney 已提交
3581 3582 3583 3584 3585 3586
	p->uartclk      = port->uartclk;
	p->fifosize     = port->fifosize;
	p->regshift     = port->regshift;
	p->iotype       = port->iotype;
	p->flags        = port->flags;
	p->mapbase      = port->mapbase;
3587
	p->mapsize      = port->mapsize;
D
David Daney 已提交
3588
	p->private_data = port->private_data;
3589 3590
	p->type		= port->type;
	p->line		= port->line;
3591

3592 3593
	serial8250_set_defaults(up_to_u8250p(p));

3594 3595 3596 3597
	if (port->serial_in)
		p->serial_in = port->serial_in;
	if (port->serial_out)
		p->serial_out = port->serial_out;
3598 3599
	if (port->handle_irq)
		p->handle_irq = port->handle_irq;
3600

L
Linus Torvalds 已提交
3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611
	return 0;
}

/**
 *	serial8250_suspend_port - suspend one serial port
 *	@line:  serial line number
 *
 *	Suspend one serial port.
 */
void serial8250_suspend_port(int line)
{
3612 3613 3614 3615 3616 3617 3618
	struct uart_8250_port *up = &serial8250_ports[line];
	struct uart_port *port = &up->port;

	if (!console_suspend_enabled && uart_console(port) &&
	    port->type != PORT_8250) {
		unsigned char canary = 0xa5;
		serial_out(up, UART_SCR, canary);
3619 3620
		if (serial_in(up, UART_SCR) == canary)
			up->canary = canary;
3621 3622 3623
	}

	uart_suspend_port(&serial8250_reg, port);
L
Linus Torvalds 已提交
3624 3625 3626 3627 3628 3629 3630 3631 3632 3633
}

/**
 *	serial8250_resume_port - resume one serial port
 *	@line:  serial line number
 *
 *	Resume one serial port.
 */
void serial8250_resume_port(int line)
{
3634
	struct uart_8250_port *up = &serial8250_ports[line];
3635
	struct uart_port *port = &up->port;
3636

3637 3638
	up->canary = 0;

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

3643
		ns16550a_goto_highspeed(up);
3644

3645
		serial_port_out(port, UART_LCR, 0);
3646
		port->uartclk = 921600*16;
3647
	}
3648
	uart_resume_port(&serial8250_reg, port);
L
Linus Torvalds 已提交
3649 3650 3651 3652 3653 3654 3655
}

/*
 * 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 已提交
3656
static int serial8250_probe(struct platform_device *dev)
L
Linus Torvalds 已提交
3657
{
J
Jingoo Han 已提交
3658
	struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
3659
	struct uart_8250_port uart;
3660
	int ret, i, irqflag = 0;
L
Linus Torvalds 已提交
3661

3662
	memset(&uart, 0, sizeof(uart));
L
Linus Torvalds 已提交
3663

3664 3665 3666
	if (share_irqs)
		irqflag = IRQF_SHARED;

3667
	for (i = 0; p && p->flags != 0; p++, i++) {
3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688
		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);
3689
		if (ret < 0) {
3690
			dev_err(&dev->dev, "unable to register port at index %d "
3691 3692 3693
				"(IO%lx MEM%llx IRQ%d): %d\n", i,
				p->iobase, (unsigned long long)p->mapbase,
				p->irq, ret);
3694
		}
L
Linus Torvalds 已提交
3695 3696 3697 3698 3699 3700 3701
	}
	return 0;
}

/*
 * Remove serial ports registered against a platform device.
 */
B
Bill Pemberton 已提交
3702
static int serial8250_remove(struct platform_device *dev)
L
Linus Torvalds 已提交
3703 3704 3705
{
	int i;

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

3709
		if (up->port.dev == &dev->dev)
L
Linus Torvalds 已提交
3710 3711 3712 3713 3714
			serial8250_unregister_port(i);
	}
	return 0;
}

3715
static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
L
Linus Torvalds 已提交
3716 3717 3718 3719 3720 3721
{
	int i;

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

3722
		if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
L
Linus Torvalds 已提交
3723 3724 3725 3726 3727 3728
			uart_suspend_port(&serial8250_reg, &up->port);
	}

	return 0;
}

3729
static int serial8250_resume(struct platform_device *dev)
L
Linus Torvalds 已提交
3730 3731 3732 3733 3734 3735
{
	int i;

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

3736
		if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
3737
			serial8250_resume_port(i);
L
Linus Torvalds 已提交
3738 3739 3740 3741 3742
	}

	return 0;
}

3743
static struct platform_driver serial8250_isa_driver = {
L
Linus Torvalds 已提交
3744
	.probe		= serial8250_probe,
3745
	.remove		= serial8250_remove,
L
Linus Torvalds 已提交
3746 3747
	.suspend	= serial8250_suspend,
	.resume		= serial8250_resume,
3748 3749 3750
	.driver		= {
		.name	= "serial8250",
	},
L
Linus Torvalds 已提交
3751 3752 3753 3754 3755 3756 3757 3758 3759
};

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

/*
3760
 * serial8250_register_8250_port and serial8250_unregister_port allows for
L
Linus Torvalds 已提交
3761 3762 3763
 * 16x50 serial ports to be configured at run-time, to support PCMCIA
 * modems and PCI multiport cards.
 */
3764
static DEFINE_MUTEX(serial_mutex);
L
Linus Torvalds 已提交
3765 3766 3767 3768 3769 3770 3771 3772

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

	/*
	 * First, find a port entry which matches.
	 */
3773
	for (i = 0; i < nr_uarts; i++)
L
Linus Torvalds 已提交
3774 3775 3776
		if (uart_match_port(&serial8250_ports[i].port, port))
			return &serial8250_ports[i];

3777 3778 3779 3780 3781
	/* try line number first if still available */
	i = port->line;
	if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
			serial8250_ports[i].port.iobase == 0)
		return &serial8250_ports[i];
L
Linus Torvalds 已提交
3782 3783 3784 3785 3786
	/*
	 * 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).
	 */
3787
	for (i = 0; i < nr_uarts; i++)
L
Linus Torvalds 已提交
3788 3789 3790 3791 3792 3793 3794 3795
		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.
	 */
3796
	for (i = 0; i < nr_uarts; i++)
L
Linus Torvalds 已提交
3797 3798 3799 3800 3801 3802 3803
		if (serial8250_ports[i].port.type == PORT_UNKNOWN)
			return &serial8250_ports[i];

	return NULL;
}

/**
3804
 *	serial8250_register_8250_port - register a serial port
3805
 *	@up: serial port template
L
Linus Torvalds 已提交
3806 3807 3808 3809 3810 3811 3812 3813 3814 3815
 *
 *	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.
 */
3816
int serial8250_register_8250_port(struct uart_8250_port *up)
L
Linus Torvalds 已提交
3817 3818 3819 3820
{
	struct uart_8250_port *uart;
	int ret = -ENOSPC;

3821
	if (up->port.uartclk == 0)
L
Linus Torvalds 已提交
3822 3823
		return -EINVAL;

3824
	mutex_lock(&serial_mutex);
L
Linus Torvalds 已提交
3825

3826
	uart = serial8250_find_match_or_unused(&up->port);
S
Sean Young 已提交
3827
	if (uart && uart->port.type != PORT_8250_CIR) {
3828 3829
		if (uart->port.dev)
			uart_remove_one_port(&serial8250_reg, &uart->port);
L
Linus Torvalds 已提交
3830

3831 3832 3833 3834 3835 3836 3837 3838 3839
		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 已提交
3840
		uart->bugs		= up->bugs;
3841
		uart->port.mapbase      = up->port.mapbase;
3842
		uart->port.mapsize      = up->port.mapsize;
3843
		uart->port.private_data = up->port.private_data;
3844 3845
		uart->tx_loadsz		= up->tx_loadsz;
		uart->capabilities	= up->capabilities;
3846 3847
		uart->port.throttle	= up->port.throttle;
		uart->port.unthrottle	= up->port.unthrottle;
3848 3849
		uart->port.rs485_config	= up->port.rs485_config;
		uart->port.rs485	= up->port.rs485;
3850
		uart->dma		= up->dma;
3851

3852 3853 3854 3855
		/* Take tx_loadsz from fifosize if it wasn't set separately */
		if (uart->port.fifosize && !uart->tx_loadsz)
			uart->tx_loadsz = uart->port.fifosize;

3856 3857 3858
		if (up->port.dev)
			uart->port.dev = up->port.dev;

3859 3860 3861
		if (skip_txen_test)
			uart->port.flags |= UPF_NO_TXEN_TEST;

3862
		if (up->port.flags & UPF_FIXED_TYPE)
3863
			uart->port.type = up->port.type;
3864

3865 3866
		serial8250_set_defaults(uart);

3867
		/* Possibly override default I/O functions.  */
3868 3869 3870 3871 3872 3873
		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;
3874
		/*  Possibly override set_termios call */
3875 3876
		if (up->port.set_termios)
			uart->port.set_termios = up->port.set_termios;
3877 3878
		if (up->port.set_mctrl)
			uart->port.set_mctrl = up->port.set_mctrl;
3879 3880 3881 3882
		if (up->port.startup)
			uart->port.startup = up->port.startup;
		if (up->port.shutdown)
			uart->port.shutdown = up->port.shutdown;
3883 3884 3885 3886 3887 3888 3889 3890
		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;
L
Linus Torvalds 已提交
3891

3892 3893 3894 3895
		if (serial8250_isa_config != NULL)
			serial8250_isa_config(0, &uart->port,
					&uart->capabilities);

L
Linus Torvalds 已提交
3896 3897 3898 3899
		ret = uart_add_one_port(&serial8250_reg, &uart->port);
		if (ret == 0)
			ret = uart->port.line;
	}
3900
	mutex_unlock(&serial_mutex);
L
Linus Torvalds 已提交
3901 3902 3903

	return ret;
}
3904 3905
EXPORT_SYMBOL(serial8250_register_8250_port);

L
Linus Torvalds 已提交
3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916
/**
 *	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];

3917
	mutex_lock(&serial_mutex);
L
Linus Torvalds 已提交
3918 3919 3920
	uart_remove_one_port(&serial8250_reg, &uart->port);
	if (serial8250_isa_devs) {
		uart->port.flags &= ~UPF_BOOT_AUTOCONF;
3921 3922
		if (skip_txen_test)
			uart->port.flags |= UPF_NO_TXEN_TEST;
L
Linus Torvalds 已提交
3923 3924
		uart->port.type = PORT_UNKNOWN;
		uart->port.dev = &serial8250_isa_devs->dev;
3925
		uart->capabilities = 0;
L
Linus Torvalds 已提交
3926 3927 3928 3929
		uart_add_one_port(&serial8250_reg, &uart->port);
	} else {
		uart->port.dev = NULL;
	}
3930
	mutex_unlock(&serial_mutex);
L
Linus Torvalds 已提交
3931 3932 3933 3934 3935
}
EXPORT_SYMBOL(serial8250_unregister_port);

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

3938 3939 3940
	if (nr_uarts == 0)
		return -ENODEV;

3941
	serial8250_isa_init_ports();
3942

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

3947 3948 3949 3950
#ifdef CONFIG_SPARC
	ret = sunserial_register_minors(&serial8250_reg, UART_NR);
#else
	serial8250_reg.nr = UART_NR;
L
Linus Torvalds 已提交
3951
	ret = uart_register_driver(&serial8250_reg);
3952
#endif
L
Linus Torvalds 已提交
3953 3954 3955
	if (ret)
		goto out;

3956 3957 3958 3959
	ret = serial8250_pnp_init();
	if (ret)
		goto unreg_uart_drv;

3960 3961 3962 3963
	serial8250_isa_devs = platform_device_alloc("serial8250",
						    PLAT8250_DEV_LEGACY);
	if (!serial8250_isa_devs) {
		ret = -ENOMEM;
3964
		goto unreg_pnp;
L
Linus Torvalds 已提交
3965 3966
	}

3967 3968 3969 3970
	ret = platform_device_add(serial8250_isa_devs);
	if (ret)
		goto put_dev;

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

3973 3974 3975
	ret = platform_driver_register(&serial8250_isa_driver);
	if (ret == 0)
		goto out;
L
Linus Torvalds 已提交
3976

3977
	platform_device_del(serial8250_isa_devs);
A
Alan Cox 已提交
3978
put_dev:
3979
	platform_device_put(serial8250_isa_devs);
3980 3981
unreg_pnp:
	serial8250_pnp_exit();
A
Alan Cox 已提交
3982
unreg_uart_drv:
3983 3984 3985
#ifdef CONFIG_SPARC
	sunserial_unregister_minors(&serial8250_reg, UART_NR);
#else
L
Linus Torvalds 已提交
3986
	uart_unregister_driver(&serial8250_reg);
3987
#endif
A
Alan Cox 已提交
3988
out:
L
Linus Torvalds 已提交
3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002
	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;

4003
	platform_driver_unregister(&serial8250_isa_driver);
L
Linus Torvalds 已提交
4004 4005
	platform_device_unregister(isa_dev);

4006 4007
	serial8250_pnp_exit();

4008 4009 4010
#ifdef CONFIG_SPARC
	sunserial_unregister_minors(&serial8250_reg, UART_NR);
#else
L
Linus Torvalds 已提交
4011
	uart_unregister_driver(&serial8250_reg);
4012
#endif
L
Linus Torvalds 已提交
4013 4014 4015 4016 4017 4018 4019 4020 4021
}

module_init(serial8250_init);
module_exit(serial8250_exit);

EXPORT_SYMBOL(serial8250_suspend_port);
EXPORT_SYMBOL(serial8250_resume_port);

MODULE_LICENSE("GPL");
4022
MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
L
Linus Torvalds 已提交
4023 4024 4025 4026 4027

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

4028 4029 4030
module_param(nr_uarts, uint, 0644);
MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");

4031 4032 4033
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 已提交
4034 4035 4036 4037 4038
#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);
4039

4040
#ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054
#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
4055
#define MODULE_PARAM_PREFIX "8250_core."
4056 4057 4058 4059 4060 4061 4062

	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,
4063
		0444, -1, 0);
4064 4065 4066
#endif
}
#else
4067
MODULE_ALIAS("8250_core");
4068 4069
#endif
#endif