xref: /openbmc/linux/drivers/tty/serial/8250/8250_core.c (revision 5c73cc4b6c83e88863a5de869cc5df3b913aef4a)
1 /*
2  *  Driver for 8250/16550-type serial ports
3  *
4  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5  *
6  *  Copyright (C) 2001 Russell King.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * A note about mapbase / membase
14  *
15  *  mapbase is the physical address of the IO port.
16  *  membase is an 'ioremapped' cookie.
17  */
18 
19 #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
20 #define SUPPORT_SYSRQ
21 #endif
22 
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/ioport.h>
26 #include <linux/init.h>
27 #include <linux/console.h>
28 #include <linux/sysrq.h>
29 #include <linux/delay.h>
30 #include <linux/platform_device.h>
31 #include <linux/tty.h>
32 #include <linux/ratelimit.h>
33 #include <linux/tty_flip.h>
34 #include <linux/serial.h>
35 #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 #ifdef CONFIG_SPARC
42 #include <linux/sunserialcore.h>
43 #endif
44 
45 #include <asm/io.h>
46 #include <asm/irq.h>
47 
48 #include "8250.h"
49 
50 /*
51  * Configuration:
52  *   share_irqs - whether we pass IRQF_SHARED to request_irq().  This option
53  *                is unsafe when used on edge-triggered interrupts.
54  */
55 static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
56 
57 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
58 
59 static struct uart_driver serial8250_reg;
60 
61 static int serial_index(struct uart_port *port)
62 {
63 	return port->minor - 64;
64 }
65 
66 static unsigned int skip_txen_test; /* force skip of txen test at init time */
67 
68 /*
69  * Debugging.
70  */
71 #if 0
72 #define DEBUG_AUTOCONF(fmt...)	printk(fmt)
73 #else
74 #define DEBUG_AUTOCONF(fmt...)	do { } while (0)
75 #endif
76 
77 #if 0
78 #define DEBUG_INTR(fmt...)	printk(fmt)
79 #else
80 #define DEBUG_INTR(fmt...)	do { } while (0)
81 #endif
82 
83 #define PASS_LIMIT	512
84 
85 #define BOTH_EMPTY 	(UART_LSR_TEMT | UART_LSR_THRE)
86 
87 
88 #ifdef CONFIG_SERIAL_8250_DETECT_IRQ
89 #define CONFIG_SERIAL_DETECT_IRQ 1
90 #endif
91 #ifdef CONFIG_SERIAL_8250_MANY_PORTS
92 #define CONFIG_SERIAL_MANY_PORTS 1
93 #endif
94 
95 /*
96  * HUB6 is always on.  This will be removed once the header
97  * files have been cleaned.
98  */
99 #define CONFIG_HUB6 1
100 
101 #include <asm/serial.h>
102 /*
103  * SERIAL_PORT_DFNS tells us about built-in ports that have no
104  * standard enumeration mechanism.   Platforms that can find all
105  * serial ports via mechanisms like ACPI or PCI need not supply it.
106  */
107 #ifndef SERIAL_PORT_DFNS
108 #define SERIAL_PORT_DFNS
109 #endif
110 
111 static const struct old_serial_port old_serial_port[] = {
112 	SERIAL_PORT_DFNS /* defined in asm/serial.h */
113 };
114 
115 #define UART_NR	CONFIG_SERIAL_8250_NR_UARTS
116 
117 #ifdef CONFIG_SERIAL_8250_RSA
118 
119 #define PORT_RSA_MAX 4
120 static unsigned long probe_rsa[PORT_RSA_MAX];
121 static unsigned int probe_rsa_count;
122 #endif /* CONFIG_SERIAL_8250_RSA  */
123 
124 struct irq_info {
125 	struct			hlist_node node;
126 	int			irq;
127 	spinlock_t		lock;	/* Protects list not the hash */
128 	struct list_head	*head;
129 };
130 
131 #define NR_IRQ_HASH		32	/* Can be adjusted later */
132 static struct hlist_head irq_lists[NR_IRQ_HASH];
133 static DEFINE_MUTEX(hash_mutex);	/* Used to walk the hash */
134 
135 /*
136  * Here we define the default xmit fifo size used for each type of UART.
137  */
138 static const struct serial8250_config uart_config[] = {
139 	[PORT_UNKNOWN] = {
140 		.name		= "unknown",
141 		.fifo_size	= 1,
142 		.tx_loadsz	= 1,
143 	},
144 	[PORT_8250] = {
145 		.name		= "8250",
146 		.fifo_size	= 1,
147 		.tx_loadsz	= 1,
148 	},
149 	[PORT_16450] = {
150 		.name		= "16450",
151 		.fifo_size	= 1,
152 		.tx_loadsz	= 1,
153 	},
154 	[PORT_16550] = {
155 		.name		= "16550",
156 		.fifo_size	= 1,
157 		.tx_loadsz	= 1,
158 	},
159 	[PORT_16550A] = {
160 		.name		= "16550A",
161 		.fifo_size	= 16,
162 		.tx_loadsz	= 16,
163 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
164 		.rxtrig_bytes	= {1, 4, 8, 14},
165 		.flags		= UART_CAP_FIFO,
166 	},
167 	[PORT_CIRRUS] = {
168 		.name		= "Cirrus",
169 		.fifo_size	= 1,
170 		.tx_loadsz	= 1,
171 	},
172 	[PORT_16650] = {
173 		.name		= "ST16650",
174 		.fifo_size	= 1,
175 		.tx_loadsz	= 1,
176 		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
177 	},
178 	[PORT_16650V2] = {
179 		.name		= "ST16650V2",
180 		.fifo_size	= 32,
181 		.tx_loadsz	= 16,
182 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
183 				  UART_FCR_T_TRIG_00,
184 		.rxtrig_bytes	= {8, 16, 24, 28},
185 		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
186 	},
187 	[PORT_16750] = {
188 		.name		= "TI16750",
189 		.fifo_size	= 64,
190 		.tx_loadsz	= 64,
191 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
192 				  UART_FCR7_64BYTE,
193 		.rxtrig_bytes	= {1, 16, 32, 56},
194 		.flags		= UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
195 	},
196 	[PORT_STARTECH] = {
197 		.name		= "Startech",
198 		.fifo_size	= 1,
199 		.tx_loadsz	= 1,
200 	},
201 	[PORT_16C950] = {
202 		.name		= "16C950/954",
203 		.fifo_size	= 128,
204 		.tx_loadsz	= 128,
205 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
206 		/* UART_CAP_EFR breaks billionon CF bluetooth card. */
207 		.flags		= UART_CAP_FIFO | UART_CAP_SLEEP,
208 	},
209 	[PORT_16654] = {
210 		.name		= "ST16654",
211 		.fifo_size	= 64,
212 		.tx_loadsz	= 32,
213 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
214 				  UART_FCR_T_TRIG_10,
215 		.rxtrig_bytes	= {8, 16, 56, 60},
216 		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
217 	},
218 	[PORT_16850] = {
219 		.name		= "XR16850",
220 		.fifo_size	= 128,
221 		.tx_loadsz	= 128,
222 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
223 		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
224 	},
225 	[PORT_RSA] = {
226 		.name		= "RSA",
227 		.fifo_size	= 2048,
228 		.tx_loadsz	= 2048,
229 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
230 		.flags		= UART_CAP_FIFO,
231 	},
232 	[PORT_NS16550A] = {
233 		.name		= "NS16550A",
234 		.fifo_size	= 16,
235 		.tx_loadsz	= 16,
236 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
237 		.flags		= UART_CAP_FIFO | UART_NATSEMI,
238 	},
239 	[PORT_XSCALE] = {
240 		.name		= "XScale",
241 		.fifo_size	= 32,
242 		.tx_loadsz	= 32,
243 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
244 		.flags		= UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
245 	},
246 	[PORT_OCTEON] = {
247 		.name		= "OCTEON",
248 		.fifo_size	= 64,
249 		.tx_loadsz	= 64,
250 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
251 		.flags		= UART_CAP_FIFO,
252 	},
253 	[PORT_AR7] = {
254 		.name		= "AR7",
255 		.fifo_size	= 16,
256 		.tx_loadsz	= 16,
257 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
258 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
259 	},
260 	[PORT_U6_16550A] = {
261 		.name		= "U6_16550A",
262 		.fifo_size	= 64,
263 		.tx_loadsz	= 64,
264 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
265 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
266 	},
267 	[PORT_TEGRA] = {
268 		.name		= "Tegra",
269 		.fifo_size	= 32,
270 		.tx_loadsz	= 8,
271 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
272 				  UART_FCR_T_TRIG_01,
273 		.rxtrig_bytes	= {1, 4, 8, 14},
274 		.flags		= UART_CAP_FIFO | UART_CAP_RTOIE,
275 	},
276 	[PORT_XR17D15X] = {
277 		.name		= "XR17D15X",
278 		.fifo_size	= 64,
279 		.tx_loadsz	= 64,
280 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
281 		.flags		= UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
282 				  UART_CAP_SLEEP,
283 	},
284 	[PORT_XR17V35X] = {
285 		.name		= "XR17V35X",
286 		.fifo_size	= 256,
287 		.tx_loadsz	= 256,
288 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11 |
289 				  UART_FCR_T_TRIG_11,
290 		.flags		= UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
291 				  UART_CAP_SLEEP,
292 	},
293 	[PORT_LPC3220] = {
294 		.name		= "LPC3220",
295 		.fifo_size	= 64,
296 		.tx_loadsz	= 32,
297 		.fcr		= UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
298 				  UART_FCR_R_TRIG_00 | UART_FCR_T_TRIG_00,
299 		.flags		= UART_CAP_FIFO,
300 	},
301 	[PORT_BRCM_TRUMANAGE] = {
302 		.name		= "TruManage",
303 		.fifo_size	= 1,
304 		.tx_loadsz	= 1024,
305 		.flags		= UART_CAP_HFIFO,
306 	},
307 	[PORT_8250_CIR] = {
308 		.name		= "CIR port"
309 	},
310 	[PORT_ALTR_16550_F32] = {
311 		.name		= "Altera 16550 FIFO32",
312 		.fifo_size	= 32,
313 		.tx_loadsz	= 32,
314 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
315 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
316 	},
317 	[PORT_ALTR_16550_F64] = {
318 		.name		= "Altera 16550 FIFO64",
319 		.fifo_size	= 64,
320 		.tx_loadsz	= 64,
321 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
322 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
323 	},
324 	[PORT_ALTR_16550_F128] = {
325 		.name		= "Altera 16550 FIFO128",
326 		.fifo_size	= 128,
327 		.tx_loadsz	= 128,
328 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
329 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
330 	},
331 /* tx_loadsz is set to 63-bytes instead of 64-bytes to implement
332 workaround of errata A-008006 which states that tx_loadsz should  be
333 configured less than Maximum supported fifo bytes */
334 	[PORT_16550A_FSL64] = {
335 		.name		= "16550A_FSL64",
336 		.fifo_size	= 64,
337 		.tx_loadsz	= 63,
338 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
339 				  UART_FCR7_64BYTE,
340 		.flags		= UART_CAP_FIFO,
341 	},
342 };
343 
344 /* Uart divisor latch read */
345 static int default_serial_dl_read(struct uart_8250_port *up)
346 {
347 	return serial_in(up, UART_DLL) | serial_in(up, UART_DLM) << 8;
348 }
349 
350 /* Uart divisor latch write */
351 static void default_serial_dl_write(struct uart_8250_port *up, int value)
352 {
353 	serial_out(up, UART_DLL, value & 0xff);
354 	serial_out(up, UART_DLM, value >> 8 & 0xff);
355 }
356 
357 #if defined(CONFIG_MIPS_ALCHEMY) || defined(CONFIG_SERIAL_8250_RT288X)
358 
359 /* Au1x00/RT288x UART hardware has a weird register layout */
360 static const s8 au_io_in_map[8] = {
361 	 0,	/* UART_RX  */
362 	 2,	/* UART_IER */
363 	 3,	/* UART_IIR */
364 	 5,	/* UART_LCR */
365 	 6,	/* UART_MCR */
366 	 7,	/* UART_LSR */
367 	 8,	/* UART_MSR */
368 	-1,	/* UART_SCR (unmapped) */
369 };
370 
371 static const s8 au_io_out_map[8] = {
372 	 1,	/* UART_TX  */
373 	 2,	/* UART_IER */
374 	 4,	/* UART_FCR */
375 	 5,	/* UART_LCR */
376 	 6,	/* UART_MCR */
377 	-1,	/* UART_LSR (unmapped) */
378 	-1,	/* UART_MSR (unmapped) */
379 	-1,	/* UART_SCR (unmapped) */
380 };
381 
382 static unsigned int au_serial_in(struct uart_port *p, int offset)
383 {
384 	if (offset >= ARRAY_SIZE(au_io_in_map))
385 		return UINT_MAX;
386 	offset = au_io_in_map[offset];
387 	if (offset < 0)
388 		return UINT_MAX;
389 	return __raw_readl(p->membase + (offset << p->regshift));
390 }
391 
392 static void au_serial_out(struct uart_port *p, int offset, int value)
393 {
394 	if (offset >= ARRAY_SIZE(au_io_out_map))
395 		return;
396 	offset = au_io_out_map[offset];
397 	if (offset < 0)
398 		return;
399 	__raw_writel(value, p->membase + (offset << p->regshift));
400 }
401 
402 /* Au1x00 haven't got a standard divisor latch */
403 static int au_serial_dl_read(struct uart_8250_port *up)
404 {
405 	return __raw_readl(up->port.membase + 0x28);
406 }
407 
408 static void au_serial_dl_write(struct uart_8250_port *up, int value)
409 {
410 	__raw_writel(value, up->port.membase + 0x28);
411 }
412 
413 #endif
414 
415 static unsigned int hub6_serial_in(struct uart_port *p, int offset)
416 {
417 	offset = offset << p->regshift;
418 	outb(p->hub6 - 1 + offset, p->iobase);
419 	return inb(p->iobase + 1);
420 }
421 
422 static void hub6_serial_out(struct uart_port *p, int offset, int value)
423 {
424 	offset = offset << p->regshift;
425 	outb(p->hub6 - 1 + offset, p->iobase);
426 	outb(value, p->iobase + 1);
427 }
428 
429 static unsigned int mem_serial_in(struct uart_port *p, int offset)
430 {
431 	offset = offset << p->regshift;
432 	return readb(p->membase + offset);
433 }
434 
435 static void mem_serial_out(struct uart_port *p, int offset, int value)
436 {
437 	offset = offset << p->regshift;
438 	writeb(value, p->membase + offset);
439 }
440 
441 static void mem32_serial_out(struct uart_port *p, int offset, int value)
442 {
443 	offset = offset << p->regshift;
444 	writel(value, p->membase + offset);
445 }
446 
447 static unsigned int mem32_serial_in(struct uart_port *p, int offset)
448 {
449 	offset = offset << p->regshift;
450 	return readl(p->membase + offset);
451 }
452 
453 static unsigned int io_serial_in(struct uart_port *p, int offset)
454 {
455 	offset = offset << p->regshift;
456 	return inb(p->iobase + offset);
457 }
458 
459 static void io_serial_out(struct uart_port *p, int offset, int value)
460 {
461 	offset = offset << p->regshift;
462 	outb(value, p->iobase + offset);
463 }
464 
465 static int serial8250_default_handle_irq(struct uart_port *port);
466 static int exar_handle_irq(struct uart_port *port);
467 
468 static void set_io_from_upio(struct uart_port *p)
469 {
470 	struct uart_8250_port *up = up_to_u8250p(p);
471 
472 	up->dl_read = default_serial_dl_read;
473 	up->dl_write = default_serial_dl_write;
474 
475 	switch (p->iotype) {
476 	case UPIO_HUB6:
477 		p->serial_in = hub6_serial_in;
478 		p->serial_out = hub6_serial_out;
479 		break;
480 
481 	case UPIO_MEM:
482 		p->serial_in = mem_serial_in;
483 		p->serial_out = mem_serial_out;
484 		break;
485 
486 	case UPIO_MEM32:
487 		p->serial_in = mem32_serial_in;
488 		p->serial_out = mem32_serial_out;
489 		break;
490 
491 #if defined(CONFIG_MIPS_ALCHEMY) || defined(CONFIG_SERIAL_8250_RT288X)
492 	case UPIO_AU:
493 		p->serial_in = au_serial_in;
494 		p->serial_out = au_serial_out;
495 		up->dl_read = au_serial_dl_read;
496 		up->dl_write = au_serial_dl_write;
497 		break;
498 #endif
499 
500 	default:
501 		p->serial_in = io_serial_in;
502 		p->serial_out = io_serial_out;
503 		break;
504 	}
505 	/* Remember loaded iotype */
506 	up->cur_iotype = p->iotype;
507 	p->handle_irq = serial8250_default_handle_irq;
508 }
509 
510 static void
511 serial_port_out_sync(struct uart_port *p, int offset, int value)
512 {
513 	switch (p->iotype) {
514 	case UPIO_MEM:
515 	case UPIO_MEM32:
516 	case UPIO_AU:
517 		p->serial_out(p, offset, value);
518 		p->serial_in(p, UART_LCR);	/* safe, no side-effects */
519 		break;
520 	default:
521 		p->serial_out(p, offset, value);
522 	}
523 }
524 
525 /*
526  * For the 16C950
527  */
528 static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
529 {
530 	serial_out(up, UART_SCR, offset);
531 	serial_out(up, UART_ICR, value);
532 }
533 
534 static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
535 {
536 	unsigned int value;
537 
538 	serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
539 	serial_out(up, UART_SCR, offset);
540 	value = serial_in(up, UART_ICR);
541 	serial_icr_write(up, UART_ACR, up->acr);
542 
543 	return value;
544 }
545 
546 /*
547  * FIFO support.
548  */
549 static void serial8250_clear_fifos(struct uart_8250_port *p)
550 {
551 	if (p->capabilities & UART_CAP_FIFO) {
552 		serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
553 		serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
554 			       UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
555 		serial_out(p, UART_FCR, 0);
556 	}
557 }
558 
559 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
560 {
561 	serial8250_clear_fifos(p);
562 	serial_out(p, UART_FCR, p->fcr);
563 }
564 EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);
565 
566 void serial8250_rpm_get(struct uart_8250_port *p)
567 {
568 	if (!(p->capabilities & UART_CAP_RPM))
569 		return;
570 	pm_runtime_get_sync(p->port.dev);
571 }
572 EXPORT_SYMBOL_GPL(serial8250_rpm_get);
573 
574 void serial8250_rpm_put(struct uart_8250_port *p)
575 {
576 	if (!(p->capabilities & UART_CAP_RPM))
577 		return;
578 	pm_runtime_mark_last_busy(p->port.dev);
579 	pm_runtime_put_autosuspend(p->port.dev);
580 }
581 EXPORT_SYMBOL_GPL(serial8250_rpm_put);
582 
583 /*
584  * These two wrappers ensure that enable_runtime_pm_tx() can be called more than
585  * once and disable_runtime_pm_tx() will still disable RPM because the fifo is
586  * empty and the HW can idle again.
587  */
588 static void serial8250_rpm_get_tx(struct uart_8250_port *p)
589 {
590 	unsigned char rpm_active;
591 
592 	if (!(p->capabilities & UART_CAP_RPM))
593 		return;
594 
595 	rpm_active = xchg(&p->rpm_tx_active, 1);
596 	if (rpm_active)
597 		return;
598 	pm_runtime_get_sync(p->port.dev);
599 }
600 
601 static void serial8250_rpm_put_tx(struct uart_8250_port *p)
602 {
603 	unsigned char rpm_active;
604 
605 	if (!(p->capabilities & UART_CAP_RPM))
606 		return;
607 
608 	rpm_active = xchg(&p->rpm_tx_active, 0);
609 	if (!rpm_active)
610 		return;
611 	pm_runtime_mark_last_busy(p->port.dev);
612 	pm_runtime_put_autosuspend(p->port.dev);
613 }
614 
615 /*
616  * IER sleep support.  UARTs which have EFRs need the "extended
617  * capability" bit enabled.  Note that on XR16C850s, we need to
618  * reset LCR to write to IER.
619  */
620 static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
621 {
622 	unsigned char lcr = 0, efr = 0;
623 	/*
624 	 * Exar UARTs have a SLEEP register that enables or disables
625 	 * each UART to enter sleep mode separately.  On the XR17V35x the
626 	 * register is accessible to each UART at the UART_EXAR_SLEEP
627 	 * offset but the UART channel may only write to the corresponding
628 	 * bit.
629 	 */
630 	serial8250_rpm_get(p);
631 	if ((p->port.type == PORT_XR17V35X) ||
632 	   (p->port.type == PORT_XR17D15X)) {
633 		serial_out(p, UART_EXAR_SLEEP, sleep ? 0xff : 0);
634 		goto out;
635 	}
636 
637 	if (p->capabilities & UART_CAP_SLEEP) {
638 		if (p->capabilities & UART_CAP_EFR) {
639 			lcr = serial_in(p, UART_LCR);
640 			efr = serial_in(p, UART_EFR);
641 			serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
642 			serial_out(p, UART_EFR, UART_EFR_ECB);
643 			serial_out(p, UART_LCR, 0);
644 		}
645 		serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
646 		if (p->capabilities & UART_CAP_EFR) {
647 			serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
648 			serial_out(p, UART_EFR, efr);
649 			serial_out(p, UART_LCR, lcr);
650 		}
651 	}
652 out:
653 	serial8250_rpm_put(p);
654 }
655 
656 #ifdef CONFIG_SERIAL_8250_RSA
657 /*
658  * Attempts to turn on the RSA FIFO.  Returns zero on failure.
659  * We set the port uart clock rate if we succeed.
660  */
661 static int __enable_rsa(struct uart_8250_port *up)
662 {
663 	unsigned char mode;
664 	int result;
665 
666 	mode = serial_in(up, UART_RSA_MSR);
667 	result = mode & UART_RSA_MSR_FIFO;
668 
669 	if (!result) {
670 		serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
671 		mode = serial_in(up, UART_RSA_MSR);
672 		result = mode & UART_RSA_MSR_FIFO;
673 	}
674 
675 	if (result)
676 		up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
677 
678 	return result;
679 }
680 
681 static void enable_rsa(struct uart_8250_port *up)
682 {
683 	if (up->port.type == PORT_RSA) {
684 		if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
685 			spin_lock_irq(&up->port.lock);
686 			__enable_rsa(up);
687 			spin_unlock_irq(&up->port.lock);
688 		}
689 		if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
690 			serial_out(up, UART_RSA_FRR, 0);
691 	}
692 }
693 
694 /*
695  * Attempts to turn off the RSA FIFO.  Returns zero on failure.
696  * It is unknown why interrupts were disabled in here.  However,
697  * the caller is expected to preserve this behaviour by grabbing
698  * the spinlock before calling this function.
699  */
700 static void disable_rsa(struct uart_8250_port *up)
701 {
702 	unsigned char mode;
703 	int result;
704 
705 	if (up->port.type == PORT_RSA &&
706 	    up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
707 		spin_lock_irq(&up->port.lock);
708 
709 		mode = serial_in(up, UART_RSA_MSR);
710 		result = !(mode & UART_RSA_MSR_FIFO);
711 
712 		if (!result) {
713 			serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
714 			mode = serial_in(up, UART_RSA_MSR);
715 			result = !(mode & UART_RSA_MSR_FIFO);
716 		}
717 
718 		if (result)
719 			up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
720 		spin_unlock_irq(&up->port.lock);
721 	}
722 }
723 #endif /* CONFIG_SERIAL_8250_RSA */
724 
725 /*
726  * This is a quickie test to see how big the FIFO is.
727  * It doesn't work at all the time, more's the pity.
728  */
729 static int size_fifo(struct uart_8250_port *up)
730 {
731 	unsigned char old_fcr, old_mcr, old_lcr;
732 	unsigned short old_dl;
733 	int count;
734 
735 	old_lcr = serial_in(up, UART_LCR);
736 	serial_out(up, UART_LCR, 0);
737 	old_fcr = serial_in(up, UART_FCR);
738 	old_mcr = serial_in(up, UART_MCR);
739 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
740 		    UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
741 	serial_out(up, UART_MCR, UART_MCR_LOOP);
742 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
743 	old_dl = serial_dl_read(up);
744 	serial_dl_write(up, 0x0001);
745 	serial_out(up, UART_LCR, 0x03);
746 	for (count = 0; count < 256; count++)
747 		serial_out(up, UART_TX, count);
748 	mdelay(20);/* FIXME - schedule_timeout */
749 	for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
750 	     (count < 256); count++)
751 		serial_in(up, UART_RX);
752 	serial_out(up, UART_FCR, old_fcr);
753 	serial_out(up, UART_MCR, old_mcr);
754 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
755 	serial_dl_write(up, old_dl);
756 	serial_out(up, UART_LCR, old_lcr);
757 
758 	return count;
759 }
760 
761 /*
762  * Read UART ID using the divisor method - set DLL and DLM to zero
763  * and the revision will be in DLL and device type in DLM.  We
764  * preserve the device state across this.
765  */
766 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
767 {
768 	unsigned char old_dll, old_dlm, old_lcr;
769 	unsigned int id;
770 
771 	old_lcr = serial_in(p, UART_LCR);
772 	serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
773 
774 	old_dll = serial_in(p, UART_DLL);
775 	old_dlm = serial_in(p, UART_DLM);
776 
777 	serial_out(p, UART_DLL, 0);
778 	serial_out(p, UART_DLM, 0);
779 
780 	id = serial_in(p, UART_DLL) | serial_in(p, UART_DLM) << 8;
781 
782 	serial_out(p, UART_DLL, old_dll);
783 	serial_out(p, UART_DLM, old_dlm);
784 	serial_out(p, UART_LCR, old_lcr);
785 
786 	return id;
787 }
788 
789 /*
790  * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
791  * When this function is called we know it is at least a StarTech
792  * 16650 V2, but it might be one of several StarTech UARTs, or one of
793  * its clones.  (We treat the broken original StarTech 16650 V1 as a
794  * 16550, and why not?  Startech doesn't seem to even acknowledge its
795  * existence.)
796  *
797  * What evil have men's minds wrought...
798  */
799 static void autoconfig_has_efr(struct uart_8250_port *up)
800 {
801 	unsigned int id1, id2, id3, rev;
802 
803 	/*
804 	 * Everything with an EFR has SLEEP
805 	 */
806 	up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
807 
808 	/*
809 	 * First we check to see if it's an Oxford Semiconductor UART.
810 	 *
811 	 * If we have to do this here because some non-National
812 	 * Semiconductor clone chips lock up if you try writing to the
813 	 * LSR register (which serial_icr_read does)
814 	 */
815 
816 	/*
817 	 * Check for Oxford Semiconductor 16C950.
818 	 *
819 	 * EFR [4] must be set else this test fails.
820 	 *
821 	 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
822 	 * claims that it's needed for 952 dual UART's (which are not
823 	 * recommended for new designs).
824 	 */
825 	up->acr = 0;
826 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
827 	serial_out(up, UART_EFR, UART_EFR_ECB);
828 	serial_out(up, UART_LCR, 0x00);
829 	id1 = serial_icr_read(up, UART_ID1);
830 	id2 = serial_icr_read(up, UART_ID2);
831 	id3 = serial_icr_read(up, UART_ID3);
832 	rev = serial_icr_read(up, UART_REV);
833 
834 	DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
835 
836 	if (id1 == 0x16 && id2 == 0xC9 &&
837 	    (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
838 		up->port.type = PORT_16C950;
839 
840 		/*
841 		 * Enable work around for the Oxford Semiconductor 952 rev B
842 		 * chip which causes it to seriously miscalculate baud rates
843 		 * when DLL is 0.
844 		 */
845 		if (id3 == 0x52 && rev == 0x01)
846 			up->bugs |= UART_BUG_QUOT;
847 		return;
848 	}
849 
850 	/*
851 	 * We check for a XR16C850 by setting DLL and DLM to 0, and then
852 	 * reading back DLL and DLM.  The chip type depends on the DLM
853 	 * value read back:
854 	 *  0x10 - XR16C850 and the DLL contains the chip revision.
855 	 *  0x12 - XR16C2850.
856 	 *  0x14 - XR16C854.
857 	 */
858 	id1 = autoconfig_read_divisor_id(up);
859 	DEBUG_AUTOCONF("850id=%04x ", id1);
860 
861 	id2 = id1 >> 8;
862 	if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
863 		up->port.type = PORT_16850;
864 		return;
865 	}
866 
867 	/*
868 	 * It wasn't an XR16C850.
869 	 *
870 	 * We distinguish between the '654 and the '650 by counting
871 	 * how many bytes are in the FIFO.  I'm using this for now,
872 	 * since that's the technique that was sent to me in the
873 	 * serial driver update, but I'm not convinced this works.
874 	 * I've had problems doing this in the past.  -TYT
875 	 */
876 	if (size_fifo(up) == 64)
877 		up->port.type = PORT_16654;
878 	else
879 		up->port.type = PORT_16650V2;
880 }
881 
882 /*
883  * We detected a chip without a FIFO.  Only two fall into
884  * this category - the original 8250 and the 16450.  The
885  * 16450 has a scratch register (accessible with LCR=0)
886  */
887 static void autoconfig_8250(struct uart_8250_port *up)
888 {
889 	unsigned char scratch, status1, status2;
890 
891 	up->port.type = PORT_8250;
892 
893 	scratch = serial_in(up, UART_SCR);
894 	serial_out(up, UART_SCR, 0xa5);
895 	status1 = serial_in(up, UART_SCR);
896 	serial_out(up, UART_SCR, 0x5a);
897 	status2 = serial_in(up, UART_SCR);
898 	serial_out(up, UART_SCR, scratch);
899 
900 	if (status1 == 0xa5 && status2 == 0x5a)
901 		up->port.type = PORT_16450;
902 }
903 
904 static int broken_efr(struct uart_8250_port *up)
905 {
906 	/*
907 	 * Exar ST16C2550 "A2" devices incorrectly detect as
908 	 * having an EFR, and report an ID of 0x0201.  See
909 	 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
910 	 */
911 	if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
912 		return 1;
913 
914 	return 0;
915 }
916 
917 /*
918  * We know that the chip has FIFOs.  Does it have an EFR?  The
919  * EFR is located in the same register position as the IIR and
920  * we know the top two bits of the IIR are currently set.  The
921  * EFR should contain zero.  Try to read the EFR.
922  */
923 static void autoconfig_16550a(struct uart_8250_port *up)
924 {
925 	unsigned char status1, status2;
926 	unsigned int iersave;
927 
928 	up->port.type = PORT_16550A;
929 	up->capabilities |= UART_CAP_FIFO;
930 
931 	/*
932 	 * XR17V35x UARTs have an extra divisor register, DLD
933 	 * that gets enabled with when DLAB is set which will
934 	 * cause the device to incorrectly match and assign
935 	 * port type to PORT_16650.  The EFR for this UART is
936 	 * found at offset 0x09. Instead check the Deice ID (DVID)
937 	 * register for a 2, 4 or 8 port UART.
938 	 */
939 	if (up->port.flags & UPF_EXAR_EFR) {
940 		status1 = serial_in(up, UART_EXAR_DVID);
941 		if (status1 == 0x82 || status1 == 0x84 || status1 == 0x88) {
942 			DEBUG_AUTOCONF("Exar XR17V35x ");
943 			up->port.type = PORT_XR17V35X;
944 			up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
945 						UART_CAP_SLEEP;
946 
947 			return;
948 		}
949 
950 	}
951 
952 	/*
953 	 * Check for presence of the EFR when DLAB is set.
954 	 * Only ST16C650V1 UARTs pass this test.
955 	 */
956 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
957 	if (serial_in(up, UART_EFR) == 0) {
958 		serial_out(up, UART_EFR, 0xA8);
959 		if (serial_in(up, UART_EFR) != 0) {
960 			DEBUG_AUTOCONF("EFRv1 ");
961 			up->port.type = PORT_16650;
962 			up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
963 		} else {
964 			serial_out(up, UART_LCR, 0);
965 			serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
966 				   UART_FCR7_64BYTE);
967 			status1 = serial_in(up, UART_IIR) >> 5;
968 			serial_out(up, UART_FCR, 0);
969 			serial_out(up, UART_LCR, 0);
970 
971 			if (status1 == 7)
972 				up->port.type = PORT_16550A_FSL64;
973 			else
974 				DEBUG_AUTOCONF("Motorola 8xxx DUART ");
975 		}
976 		serial_out(up, UART_EFR, 0);
977 		return;
978 	}
979 
980 	/*
981 	 * Maybe it requires 0xbf to be written to the LCR.
982 	 * (other ST16C650V2 UARTs, TI16C752A, etc)
983 	 */
984 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
985 	if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
986 		DEBUG_AUTOCONF("EFRv2 ");
987 		autoconfig_has_efr(up);
988 		return;
989 	}
990 
991 	/*
992 	 * Check for a National Semiconductor SuperIO chip.
993 	 * Attempt to switch to bank 2, read the value of the LOOP bit
994 	 * from EXCR1. Switch back to bank 0, change it in MCR. Then
995 	 * switch back to bank 2, read it from EXCR1 again and check
996 	 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
997 	 */
998 	serial_out(up, UART_LCR, 0);
999 	status1 = serial_in(up, UART_MCR);
1000 	serial_out(up, UART_LCR, 0xE0);
1001 	status2 = serial_in(up, 0x02); /* EXCR1 */
1002 
1003 	if (!((status2 ^ status1) & UART_MCR_LOOP)) {
1004 		serial_out(up, UART_LCR, 0);
1005 		serial_out(up, UART_MCR, status1 ^ UART_MCR_LOOP);
1006 		serial_out(up, UART_LCR, 0xE0);
1007 		status2 = serial_in(up, 0x02); /* EXCR1 */
1008 		serial_out(up, UART_LCR, 0);
1009 		serial_out(up, UART_MCR, status1);
1010 
1011 		if ((status2 ^ status1) & UART_MCR_LOOP) {
1012 			unsigned short quot;
1013 
1014 			serial_out(up, UART_LCR, 0xE0);
1015 
1016 			quot = serial_dl_read(up);
1017 			quot <<= 3;
1018 
1019 			if (ns16550a_goto_highspeed(up))
1020 				serial_dl_write(up, quot);
1021 
1022 			serial_out(up, UART_LCR, 0);
1023 
1024 			up->port.uartclk = 921600*16;
1025 			up->port.type = PORT_NS16550A;
1026 			up->capabilities |= UART_NATSEMI;
1027 			return;
1028 		}
1029 	}
1030 
1031 	/*
1032 	 * No EFR.  Try to detect a TI16750, which only sets bit 5 of
1033 	 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1034 	 * Try setting it with and without DLAB set.  Cheap clones
1035 	 * set bit 5 without DLAB set.
1036 	 */
1037 	serial_out(up, UART_LCR, 0);
1038 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1039 	status1 = serial_in(up, UART_IIR) >> 5;
1040 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1041 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1042 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1043 	status2 = serial_in(up, UART_IIR) >> 5;
1044 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1045 	serial_out(up, UART_LCR, 0);
1046 
1047 	DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1048 
1049 	if (status1 == 6 && status2 == 7) {
1050 		up->port.type = PORT_16750;
1051 		up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1052 		return;
1053 	}
1054 
1055 	/*
1056 	 * Try writing and reading the UART_IER_UUE bit (b6).
1057 	 * If it works, this is probably one of the Xscale platform's
1058 	 * internal UARTs.
1059 	 * We're going to explicitly set the UUE bit to 0 before
1060 	 * trying to write and read a 1 just to make sure it's not
1061 	 * already a 1 and maybe locked there before we even start start.
1062 	 */
1063 	iersave = serial_in(up, UART_IER);
1064 	serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
1065 	if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1066 		/*
1067 		 * OK it's in a known zero state, try writing and reading
1068 		 * without disturbing the current state of the other bits.
1069 		 */
1070 		serial_out(up, UART_IER, iersave | UART_IER_UUE);
1071 		if (serial_in(up, UART_IER) & UART_IER_UUE) {
1072 			/*
1073 			 * It's an Xscale.
1074 			 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1075 			 */
1076 			DEBUG_AUTOCONF("Xscale ");
1077 			up->port.type = PORT_XSCALE;
1078 			up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
1079 			return;
1080 		}
1081 	} else {
1082 		/*
1083 		 * If we got here we couldn't force the IER_UUE bit to 0.
1084 		 * Log it and continue.
1085 		 */
1086 		DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1087 	}
1088 	serial_out(up, UART_IER, iersave);
1089 
1090 	/*
1091 	 * Exar uarts have EFR in a weird location
1092 	 */
1093 	if (up->port.flags & UPF_EXAR_EFR) {
1094 		DEBUG_AUTOCONF("Exar XR17D15x ");
1095 		up->port.type = PORT_XR17D15X;
1096 		up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
1097 				    UART_CAP_SLEEP;
1098 
1099 		return;
1100 	}
1101 
1102 	/*
1103 	 * We distinguish between 16550A and U6 16550A by counting
1104 	 * how many bytes are in the FIFO.
1105 	 */
1106 	if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1107 		up->port.type = PORT_U6_16550A;
1108 		up->capabilities |= UART_CAP_AFE;
1109 	}
1110 }
1111 
1112 /*
1113  * This routine is called by rs_init() to initialize a specific serial
1114  * port.  It determines what type of UART chip this serial port is
1115  * using: 8250, 16450, 16550, 16550A.  The important question is
1116  * whether or not this UART is a 16550A or not, since this will
1117  * determine whether or not we can use its FIFO features or not.
1118  */
1119 static void autoconfig(struct uart_8250_port *up)
1120 {
1121 	unsigned char status1, scratch, scratch2, scratch3;
1122 	unsigned char save_lcr, save_mcr;
1123 	struct uart_port *port = &up->port;
1124 	unsigned long flags;
1125 	unsigned int old_capabilities;
1126 
1127 	if (!port->iobase && !port->mapbase && !port->membase)
1128 		return;
1129 
1130 	DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
1131 		       serial_index(port), port->iobase, port->membase);
1132 
1133 	/*
1134 	 * We really do need global IRQs disabled here - we're going to
1135 	 * be frobbing the chips IRQ enable register to see if it exists.
1136 	 */
1137 	spin_lock_irqsave(&port->lock, flags);
1138 
1139 	up->capabilities = 0;
1140 	up->bugs = 0;
1141 
1142 	if (!(port->flags & UPF_BUGGY_UART)) {
1143 		/*
1144 		 * Do a simple existence test first; if we fail this,
1145 		 * there's no point trying anything else.
1146 		 *
1147 		 * 0x80 is used as a nonsense port to prevent against
1148 		 * false positives due to ISA bus float.  The
1149 		 * assumption is that 0x80 is a non-existent port;
1150 		 * which should be safe since include/asm/io.h also
1151 		 * makes this assumption.
1152 		 *
1153 		 * Note: this is safe as long as MCR bit 4 is clear
1154 		 * and the device is in "PC" mode.
1155 		 */
1156 		scratch = serial_in(up, UART_IER);
1157 		serial_out(up, UART_IER, 0);
1158 #ifdef __i386__
1159 		outb(0xff, 0x080);
1160 #endif
1161 		/*
1162 		 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1163 		 * 16C754B) allow only to modify them if an EFR bit is set.
1164 		 */
1165 		scratch2 = serial_in(up, UART_IER) & 0x0f;
1166 		serial_out(up, UART_IER, 0x0F);
1167 #ifdef __i386__
1168 		outb(0, 0x080);
1169 #endif
1170 		scratch3 = serial_in(up, UART_IER) & 0x0f;
1171 		serial_out(up, UART_IER, scratch);
1172 		if (scratch2 != 0 || scratch3 != 0x0F) {
1173 			/*
1174 			 * We failed; there's nothing here
1175 			 */
1176 			spin_unlock_irqrestore(&port->lock, flags);
1177 			DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1178 				       scratch2, scratch3);
1179 			goto out;
1180 		}
1181 	}
1182 
1183 	save_mcr = serial_in(up, UART_MCR);
1184 	save_lcr = serial_in(up, UART_LCR);
1185 
1186 	/*
1187 	 * Check to see if a UART is really there.  Certain broken
1188 	 * internal modems based on the Rockwell chipset fail this
1189 	 * test, because they apparently don't implement the loopback
1190 	 * test mode.  So this test is skipped on the COM 1 through
1191 	 * COM 4 ports.  This *should* be safe, since no board
1192 	 * manufacturer would be stupid enough to design a board
1193 	 * that conflicts with COM 1-4 --- we hope!
1194 	 */
1195 	if (!(port->flags & UPF_SKIP_TEST)) {
1196 		serial_out(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1197 		status1 = serial_in(up, UART_MSR) & 0xF0;
1198 		serial_out(up, UART_MCR, save_mcr);
1199 		if (status1 != 0x90) {
1200 			spin_unlock_irqrestore(&port->lock, flags);
1201 			DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1202 				       status1);
1203 			goto out;
1204 		}
1205 	}
1206 
1207 	/*
1208 	 * We're pretty sure there's a port here.  Lets find out what
1209 	 * type of port it is.  The IIR top two bits allows us to find
1210 	 * out if it's 8250 or 16450, 16550, 16550A or later.  This
1211 	 * determines what we test for next.
1212 	 *
1213 	 * We also initialise the EFR (if any) to zero for later.  The
1214 	 * EFR occupies the same register location as the FCR and IIR.
1215 	 */
1216 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1217 	serial_out(up, UART_EFR, 0);
1218 	serial_out(up, UART_LCR, 0);
1219 
1220 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1221 	scratch = serial_in(up, UART_IIR) >> 6;
1222 
1223 	switch (scratch) {
1224 	case 0:
1225 		autoconfig_8250(up);
1226 		break;
1227 	case 1:
1228 		port->type = PORT_UNKNOWN;
1229 		break;
1230 	case 2:
1231 		port->type = PORT_16550;
1232 		break;
1233 	case 3:
1234 		autoconfig_16550a(up);
1235 		break;
1236 	}
1237 
1238 #ifdef CONFIG_SERIAL_8250_RSA
1239 	/*
1240 	 * Only probe for RSA ports if we got the region.
1241 	 */
1242 	if (port->type == PORT_16550A && up->probe & UART_PROBE_RSA &&
1243 	    __enable_rsa(up))
1244 		port->type = PORT_RSA;
1245 #endif
1246 
1247 	serial_out(up, UART_LCR, save_lcr);
1248 
1249 	port->fifosize = uart_config[up->port.type].fifo_size;
1250 	old_capabilities = up->capabilities;
1251 	up->capabilities = uart_config[port->type].flags;
1252 	up->tx_loadsz = uart_config[port->type].tx_loadsz;
1253 
1254 	if (port->type == PORT_UNKNOWN)
1255 		goto out_lock;
1256 
1257 	/*
1258 	 * Reset the UART.
1259 	 */
1260 #ifdef CONFIG_SERIAL_8250_RSA
1261 	if (port->type == PORT_RSA)
1262 		serial_out(up, UART_RSA_FRR, 0);
1263 #endif
1264 	serial_out(up, UART_MCR, save_mcr);
1265 	serial8250_clear_fifos(up);
1266 	serial_in(up, UART_RX);
1267 	if (up->capabilities & UART_CAP_UUE)
1268 		serial_out(up, UART_IER, UART_IER_UUE);
1269 	else
1270 		serial_out(up, UART_IER, 0);
1271 
1272 out_lock:
1273 	spin_unlock_irqrestore(&port->lock, flags);
1274 	if (up->capabilities != old_capabilities) {
1275 		printk(KERN_WARNING
1276 		       "ttyS%d: detected caps %08x should be %08x\n",
1277 		       serial_index(port), old_capabilities,
1278 		       up->capabilities);
1279 	}
1280 out:
1281 	DEBUG_AUTOCONF("iir=%d ", scratch);
1282 	DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
1283 }
1284 
1285 static void autoconfig_irq(struct uart_8250_port *up)
1286 {
1287 	struct uart_port *port = &up->port;
1288 	unsigned char save_mcr, save_ier;
1289 	unsigned char save_ICP = 0;
1290 	unsigned int ICP = 0;
1291 	unsigned long irqs;
1292 	int irq;
1293 
1294 	if (port->flags & UPF_FOURPORT) {
1295 		ICP = (port->iobase & 0xfe0) | 0x1f;
1296 		save_ICP = inb_p(ICP);
1297 		outb_p(0x80, ICP);
1298 		inb_p(ICP);
1299 	}
1300 
1301 	/* forget possible initially masked and pending IRQ */
1302 	probe_irq_off(probe_irq_on());
1303 	save_mcr = serial_in(up, UART_MCR);
1304 	save_ier = serial_in(up, UART_IER);
1305 	serial_out(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
1306 
1307 	irqs = probe_irq_on();
1308 	serial_out(up, UART_MCR, 0);
1309 	udelay(10);
1310 	if (port->flags & UPF_FOURPORT) {
1311 		serial_out(up, UART_MCR,
1312 			    UART_MCR_DTR | UART_MCR_RTS);
1313 	} else {
1314 		serial_out(up, UART_MCR,
1315 			    UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1316 	}
1317 	serial_out(up, UART_IER, 0x0f);	/* enable all intrs */
1318 	serial_in(up, UART_LSR);
1319 	serial_in(up, UART_RX);
1320 	serial_in(up, UART_IIR);
1321 	serial_in(up, UART_MSR);
1322 	serial_out(up, UART_TX, 0xFF);
1323 	udelay(20);
1324 	irq = probe_irq_off(irqs);
1325 
1326 	serial_out(up, UART_MCR, save_mcr);
1327 	serial_out(up, UART_IER, save_ier);
1328 
1329 	if (port->flags & UPF_FOURPORT)
1330 		outb_p(save_ICP, ICP);
1331 
1332 	port->irq = (irq > 0) ? irq : 0;
1333 }
1334 
1335 static inline void __stop_tx(struct uart_8250_port *p)
1336 {
1337 	if (p->ier & UART_IER_THRI) {
1338 		p->ier &= ~UART_IER_THRI;
1339 		serial_out(p, UART_IER, p->ier);
1340 		serial8250_rpm_put_tx(p);
1341 	}
1342 }
1343 
1344 static void serial8250_stop_tx(struct uart_port *port)
1345 {
1346 	struct uart_8250_port *up = up_to_u8250p(port);
1347 
1348 	serial8250_rpm_get(up);
1349 	__stop_tx(up);
1350 
1351 	/*
1352 	 * We really want to stop the transmitter from sending.
1353 	 */
1354 	if (port->type == PORT_16C950) {
1355 		up->acr |= UART_ACR_TXDIS;
1356 		serial_icr_write(up, UART_ACR, up->acr);
1357 	}
1358 	serial8250_rpm_put(up);
1359 }
1360 
1361 static void serial8250_start_tx(struct uart_port *port)
1362 {
1363 	struct uart_8250_port *up = up_to_u8250p(port);
1364 
1365 	serial8250_rpm_get_tx(up);
1366 
1367 	if (up->dma && !up->dma->tx_dma(up))
1368 		return;
1369 
1370 	if (!(up->ier & UART_IER_THRI)) {
1371 		up->ier |= UART_IER_THRI;
1372 		serial_port_out(port, UART_IER, up->ier);
1373 
1374 		if (up->bugs & UART_BUG_TXEN) {
1375 			unsigned char lsr;
1376 			lsr = serial_in(up, UART_LSR);
1377 			up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1378 			if (lsr & UART_LSR_THRE)
1379 				serial8250_tx_chars(up);
1380 		}
1381 	}
1382 
1383 	/*
1384 	 * Re-enable the transmitter if we disabled it.
1385 	 */
1386 	if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1387 		up->acr &= ~UART_ACR_TXDIS;
1388 		serial_icr_write(up, UART_ACR, up->acr);
1389 	}
1390 }
1391 
1392 static void serial8250_throttle(struct uart_port *port)
1393 {
1394 	port->throttle(port);
1395 }
1396 
1397 static void serial8250_unthrottle(struct uart_port *port)
1398 {
1399 	port->unthrottle(port);
1400 }
1401 
1402 static void serial8250_stop_rx(struct uart_port *port)
1403 {
1404 	struct uart_8250_port *up = up_to_u8250p(port);
1405 
1406 	serial8250_rpm_get(up);
1407 
1408 	up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1409 	up->port.read_status_mask &= ~UART_LSR_DR;
1410 	serial_port_out(port, UART_IER, up->ier);
1411 
1412 	serial8250_rpm_put(up);
1413 }
1414 
1415 static void serial8250_disable_ms(struct uart_port *port)
1416 {
1417 	struct uart_8250_port *up =
1418 		container_of(port, struct uart_8250_port, port);
1419 
1420 	/* no MSR capabilities */
1421 	if (up->bugs & UART_BUG_NOMSR)
1422 		return;
1423 
1424 	up->ier &= ~UART_IER_MSI;
1425 	serial_port_out(port, UART_IER, up->ier);
1426 }
1427 
1428 static void serial8250_enable_ms(struct uart_port *port)
1429 {
1430 	struct uart_8250_port *up = up_to_u8250p(port);
1431 
1432 	/* no MSR capabilities */
1433 	if (up->bugs & UART_BUG_NOMSR)
1434 		return;
1435 
1436 	up->ier |= UART_IER_MSI;
1437 
1438 	serial8250_rpm_get(up);
1439 	serial_port_out(port, UART_IER, up->ier);
1440 	serial8250_rpm_put(up);
1441 }
1442 
1443 /*
1444  * serial8250_rx_chars: processes according to the passed in LSR
1445  * value, and returns the remaining LSR bits not handled
1446  * by this Rx routine.
1447  */
1448 unsigned char
1449 serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
1450 {
1451 	struct uart_port *port = &up->port;
1452 	unsigned char ch;
1453 	int max_count = 256;
1454 	char flag;
1455 
1456 	do {
1457 		if (likely(lsr & UART_LSR_DR))
1458 			ch = serial_in(up, UART_RX);
1459 		else
1460 			/*
1461 			 * Intel 82571 has a Serial Over Lan device that will
1462 			 * set UART_LSR_BI without setting UART_LSR_DR when
1463 			 * it receives a break. To avoid reading from the
1464 			 * receive buffer without UART_LSR_DR bit set, we
1465 			 * just force the read character to be 0
1466 			 */
1467 			ch = 0;
1468 
1469 		flag = TTY_NORMAL;
1470 		port->icount.rx++;
1471 
1472 		lsr |= up->lsr_saved_flags;
1473 		up->lsr_saved_flags = 0;
1474 
1475 		if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1476 			if (lsr & UART_LSR_BI) {
1477 				lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1478 				port->icount.brk++;
1479 				/*
1480 				 * We do the SysRQ and SAK checking
1481 				 * here because otherwise the break
1482 				 * may get masked by ignore_status_mask
1483 				 * or read_status_mask.
1484 				 */
1485 				if (uart_handle_break(port))
1486 					goto ignore_char;
1487 			} else if (lsr & UART_LSR_PE)
1488 				port->icount.parity++;
1489 			else if (lsr & UART_LSR_FE)
1490 				port->icount.frame++;
1491 			if (lsr & UART_LSR_OE)
1492 				port->icount.overrun++;
1493 
1494 			/*
1495 			 * Mask off conditions which should be ignored.
1496 			 */
1497 			lsr &= port->read_status_mask;
1498 
1499 			if (lsr & UART_LSR_BI) {
1500 				DEBUG_INTR("handling break....");
1501 				flag = TTY_BREAK;
1502 			} else if (lsr & UART_LSR_PE)
1503 				flag = TTY_PARITY;
1504 			else if (lsr & UART_LSR_FE)
1505 				flag = TTY_FRAME;
1506 		}
1507 		if (uart_handle_sysrq_char(port, ch))
1508 			goto ignore_char;
1509 
1510 		uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
1511 
1512 ignore_char:
1513 		lsr = serial_in(up, UART_LSR);
1514 	} while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (--max_count > 0));
1515 	spin_unlock(&port->lock);
1516 	tty_flip_buffer_push(&port->state->port);
1517 	spin_lock(&port->lock);
1518 	return lsr;
1519 }
1520 EXPORT_SYMBOL_GPL(serial8250_rx_chars);
1521 
1522 void serial8250_tx_chars(struct uart_8250_port *up)
1523 {
1524 	struct uart_port *port = &up->port;
1525 	struct circ_buf *xmit = &port->state->xmit;
1526 	int count;
1527 
1528 	if (port->x_char) {
1529 		serial_out(up, UART_TX, port->x_char);
1530 		port->icount.tx++;
1531 		port->x_char = 0;
1532 		return;
1533 	}
1534 	if (uart_tx_stopped(port)) {
1535 		serial8250_stop_tx(port);
1536 		return;
1537 	}
1538 	if (uart_circ_empty(xmit)) {
1539 		__stop_tx(up);
1540 		return;
1541 	}
1542 
1543 	count = up->tx_loadsz;
1544 	do {
1545 		serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1546 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1547 		port->icount.tx++;
1548 		if (uart_circ_empty(xmit))
1549 			break;
1550 		if (up->capabilities & UART_CAP_HFIFO) {
1551 			if ((serial_port_in(port, UART_LSR) & BOTH_EMPTY) !=
1552 			    BOTH_EMPTY)
1553 				break;
1554 		}
1555 	} while (--count > 0);
1556 
1557 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1558 		uart_write_wakeup(port);
1559 
1560 	DEBUG_INTR("THRE...");
1561 
1562 	/*
1563 	 * With RPM enabled, we have to wait until the FIFO is empty before the
1564 	 * HW can go idle. So we get here once again with empty FIFO and disable
1565 	 * the interrupt and RPM in __stop_tx()
1566 	 */
1567 	if (uart_circ_empty(xmit) && !(up->capabilities & UART_CAP_RPM))
1568 		__stop_tx(up);
1569 }
1570 EXPORT_SYMBOL_GPL(serial8250_tx_chars);
1571 
1572 /* Caller holds uart port lock */
1573 unsigned int serial8250_modem_status(struct uart_8250_port *up)
1574 {
1575 	struct uart_port *port = &up->port;
1576 	unsigned int status = serial_in(up, UART_MSR);
1577 
1578 	status |= up->msr_saved_flags;
1579 	up->msr_saved_flags = 0;
1580 	if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1581 	    port->state != NULL) {
1582 		if (status & UART_MSR_TERI)
1583 			port->icount.rng++;
1584 		if (status & UART_MSR_DDSR)
1585 			port->icount.dsr++;
1586 		if (status & UART_MSR_DDCD)
1587 			uart_handle_dcd_change(port, status & UART_MSR_DCD);
1588 		if (status & UART_MSR_DCTS)
1589 			uart_handle_cts_change(port, status & UART_MSR_CTS);
1590 
1591 		wake_up_interruptible(&port->state->port.delta_msr_wait);
1592 	}
1593 
1594 	return status;
1595 }
1596 EXPORT_SYMBOL_GPL(serial8250_modem_status);
1597 
1598 /*
1599  * This handles the interrupt from one port.
1600  */
1601 int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
1602 {
1603 	unsigned char status;
1604 	unsigned long flags;
1605 	struct uart_8250_port *up = up_to_u8250p(port);
1606 	int dma_err = 0;
1607 
1608 	if (iir & UART_IIR_NO_INT)
1609 		return 0;
1610 
1611 	spin_lock_irqsave(&port->lock, flags);
1612 
1613 	status = serial_port_in(port, UART_LSR);
1614 
1615 	DEBUG_INTR("status = %x...", status);
1616 
1617 	if (status & (UART_LSR_DR | UART_LSR_BI)) {
1618 		if (up->dma)
1619 			dma_err = up->dma->rx_dma(up, iir);
1620 
1621 		if (!up->dma || dma_err)
1622 			status = serial8250_rx_chars(up, status);
1623 	}
1624 	serial8250_modem_status(up);
1625 	if ((!up->dma || (up->dma && up->dma->tx_err)) &&
1626 	    (status & UART_LSR_THRE))
1627 		serial8250_tx_chars(up);
1628 
1629 	spin_unlock_irqrestore(&port->lock, flags);
1630 	return 1;
1631 }
1632 EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1633 
1634 static int serial8250_default_handle_irq(struct uart_port *port)
1635 {
1636 	struct uart_8250_port *up = up_to_u8250p(port);
1637 	unsigned int iir;
1638 	int ret;
1639 
1640 	serial8250_rpm_get(up);
1641 
1642 	iir = serial_port_in(port, UART_IIR);
1643 	ret = serial8250_handle_irq(port, iir);
1644 
1645 	serial8250_rpm_put(up);
1646 	return ret;
1647 }
1648 
1649 /*
1650  * These Exar UARTs have an extra interrupt indicator that could
1651  * fire for a few unimplemented interrupts.  One of which is a
1652  * wakeup event when coming out of sleep.  Put this here just
1653  * to be on the safe side that these interrupts don't go unhandled.
1654  */
1655 static int exar_handle_irq(struct uart_port *port)
1656 {
1657 	unsigned char int0, int1, int2, int3;
1658 	unsigned int iir = serial_port_in(port, UART_IIR);
1659 	int ret;
1660 
1661 	ret = serial8250_handle_irq(port, iir);
1662 
1663 	if ((port->type == PORT_XR17V35X) ||
1664 	   (port->type == PORT_XR17D15X)) {
1665 		int0 = serial_port_in(port, 0x80);
1666 		int1 = serial_port_in(port, 0x81);
1667 		int2 = serial_port_in(port, 0x82);
1668 		int3 = serial_port_in(port, 0x83);
1669 	}
1670 
1671 	return ret;
1672 }
1673 
1674 /*
1675  * This is the serial driver's interrupt routine.
1676  *
1677  * Arjan thinks the old way was overly complex, so it got simplified.
1678  * Alan disagrees, saying that need the complexity to handle the weird
1679  * nature of ISA shared interrupts.  (This is a special exception.)
1680  *
1681  * In order to handle ISA shared interrupts properly, we need to check
1682  * that all ports have been serviced, and therefore the ISA interrupt
1683  * line has been de-asserted.
1684  *
1685  * This means we need to loop through all ports. checking that they
1686  * don't have an interrupt pending.
1687  */
1688 static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
1689 {
1690 	struct irq_info *i = dev_id;
1691 	struct list_head *l, *end = NULL;
1692 	int pass_counter = 0, handled = 0;
1693 
1694 	DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1695 
1696 	spin_lock(&i->lock);
1697 
1698 	l = i->head;
1699 	do {
1700 		struct uart_8250_port *up;
1701 		struct uart_port *port;
1702 
1703 		up = list_entry(l, struct uart_8250_port, list);
1704 		port = &up->port;
1705 
1706 		if (port->handle_irq(port)) {
1707 			handled = 1;
1708 			end = NULL;
1709 		} else if (end == NULL)
1710 			end = l;
1711 
1712 		l = l->next;
1713 
1714 		if (l == i->head && pass_counter++ > PASS_LIMIT) {
1715 			/* If we hit this, we're dead. */
1716 			printk_ratelimited(KERN_ERR
1717 				"serial8250: too much work for irq%d\n", irq);
1718 			break;
1719 		}
1720 	} while (l != end);
1721 
1722 	spin_unlock(&i->lock);
1723 
1724 	DEBUG_INTR("end.\n");
1725 
1726 	return IRQ_RETVAL(handled);
1727 }
1728 
1729 /*
1730  * To support ISA shared interrupts, we need to have one interrupt
1731  * handler that ensures that the IRQ line has been deasserted
1732  * before returning.  Failing to do this will result in the IRQ
1733  * line being stuck active, and, since ISA irqs are edge triggered,
1734  * no more IRQs will be seen.
1735  */
1736 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1737 {
1738 	spin_lock_irq(&i->lock);
1739 
1740 	if (!list_empty(i->head)) {
1741 		if (i->head == &up->list)
1742 			i->head = i->head->next;
1743 		list_del(&up->list);
1744 	} else {
1745 		BUG_ON(i->head != &up->list);
1746 		i->head = NULL;
1747 	}
1748 	spin_unlock_irq(&i->lock);
1749 	/* List empty so throw away the hash node */
1750 	if (i->head == NULL) {
1751 		hlist_del(&i->node);
1752 		kfree(i);
1753 	}
1754 }
1755 
1756 static int serial_link_irq_chain(struct uart_8250_port *up)
1757 {
1758 	struct hlist_head *h;
1759 	struct hlist_node *n;
1760 	struct irq_info *i;
1761 	int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
1762 
1763 	mutex_lock(&hash_mutex);
1764 
1765 	h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1766 
1767 	hlist_for_each(n, h) {
1768 		i = hlist_entry(n, struct irq_info, node);
1769 		if (i->irq == up->port.irq)
1770 			break;
1771 	}
1772 
1773 	if (n == NULL) {
1774 		i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
1775 		if (i == NULL) {
1776 			mutex_unlock(&hash_mutex);
1777 			return -ENOMEM;
1778 		}
1779 		spin_lock_init(&i->lock);
1780 		i->irq = up->port.irq;
1781 		hlist_add_head(&i->node, h);
1782 	}
1783 	mutex_unlock(&hash_mutex);
1784 
1785 	spin_lock_irq(&i->lock);
1786 
1787 	if (i->head) {
1788 		list_add(&up->list, i->head);
1789 		spin_unlock_irq(&i->lock);
1790 
1791 		ret = 0;
1792 	} else {
1793 		INIT_LIST_HEAD(&up->list);
1794 		i->head = &up->list;
1795 		spin_unlock_irq(&i->lock);
1796 		irq_flags |= up->port.irqflags;
1797 		ret = request_irq(up->port.irq, serial8250_interrupt,
1798 				  irq_flags, "serial", i);
1799 		if (ret < 0)
1800 			serial_do_unlink(i, up);
1801 	}
1802 
1803 	return ret;
1804 }
1805 
1806 static void serial_unlink_irq_chain(struct uart_8250_port *up)
1807 {
1808 	/*
1809 	 * yes, some broken gcc emit "warning: 'i' may be used uninitialized"
1810 	 * but no, we are not going to take a patch that assigns NULL below.
1811 	 */
1812 	struct irq_info *i;
1813 	struct hlist_node *n;
1814 	struct hlist_head *h;
1815 
1816 	mutex_lock(&hash_mutex);
1817 
1818 	h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1819 
1820 	hlist_for_each(n, h) {
1821 		i = hlist_entry(n, struct irq_info, node);
1822 		if (i->irq == up->port.irq)
1823 			break;
1824 	}
1825 
1826 	BUG_ON(n == NULL);
1827 	BUG_ON(i->head == NULL);
1828 
1829 	if (list_empty(i->head))
1830 		free_irq(up->port.irq, i);
1831 
1832 	serial_do_unlink(i, up);
1833 	mutex_unlock(&hash_mutex);
1834 }
1835 
1836 /*
1837  * This function is used to handle ports that do not have an
1838  * interrupt.  This doesn't work very well for 16450's, but gives
1839  * barely passable results for a 16550A.  (Although at the expense
1840  * of much CPU overhead).
1841  */
1842 static void serial8250_timeout(unsigned long data)
1843 {
1844 	struct uart_8250_port *up = (struct uart_8250_port *)data;
1845 
1846 	up->port.handle_irq(&up->port);
1847 	mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
1848 }
1849 
1850 static void serial8250_backup_timeout(unsigned long data)
1851 {
1852 	struct uart_8250_port *up = (struct uart_8250_port *)data;
1853 	unsigned int iir, ier = 0, lsr;
1854 	unsigned long flags;
1855 
1856 	spin_lock_irqsave(&up->port.lock, flags);
1857 
1858 	/*
1859 	 * Must disable interrupts or else we risk racing with the interrupt
1860 	 * based handler.
1861 	 */
1862 	if (up->port.irq) {
1863 		ier = serial_in(up, UART_IER);
1864 		serial_out(up, UART_IER, 0);
1865 	}
1866 
1867 	iir = serial_in(up, UART_IIR);
1868 
1869 	/*
1870 	 * This should be a safe test for anyone who doesn't trust the
1871 	 * IIR bits on their UART, but it's specifically designed for
1872 	 * the "Diva" UART used on the management processor on many HP
1873 	 * ia64 and parisc boxes.
1874 	 */
1875 	lsr = serial_in(up, UART_LSR);
1876 	up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1877 	if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
1878 	    (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
1879 	    (lsr & UART_LSR_THRE)) {
1880 		iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1881 		iir |= UART_IIR_THRI;
1882 	}
1883 
1884 	if (!(iir & UART_IIR_NO_INT))
1885 		serial8250_tx_chars(up);
1886 
1887 	if (up->port.irq)
1888 		serial_out(up, UART_IER, ier);
1889 
1890 	spin_unlock_irqrestore(&up->port.lock, flags);
1891 
1892 	/* Standard timer interval plus 0.2s to keep the port running */
1893 	mod_timer(&up->timer,
1894 		jiffies + uart_poll_timeout(&up->port) + HZ / 5);
1895 }
1896 
1897 static int univ8250_setup_irq(struct uart_8250_port *up)
1898 {
1899 	struct uart_port *port = &up->port;
1900 	int retval = 0;
1901 
1902 	/*
1903 	 * The above check will only give an accurate result the first time
1904 	 * the port is opened so this value needs to be preserved.
1905 	 */
1906 	if (up->bugs & UART_BUG_THRE) {
1907 		pr_debug("ttyS%d - using backup timer\n", serial_index(port));
1908 
1909 		up->timer.function = serial8250_backup_timeout;
1910 		up->timer.data = (unsigned long)up;
1911 		mod_timer(&up->timer, jiffies +
1912 			  uart_poll_timeout(port) + HZ / 5);
1913 	}
1914 
1915 	/*
1916 	 * If the "interrupt" for this port doesn't correspond with any
1917 	 * hardware interrupt, we use a timer-based system.  The original
1918 	 * driver used to do this with IRQ0.
1919 	 */
1920 	if (!port->irq) {
1921 		up->timer.data = (unsigned long)up;
1922 		mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
1923 	} else
1924 		retval = serial_link_irq_chain(up);
1925 
1926 	return retval;
1927 }
1928 
1929 static void univ8250_release_irq(struct uart_8250_port *up)
1930 {
1931 	struct uart_port *port = &up->port;
1932 
1933 	del_timer_sync(&up->timer);
1934 	up->timer.function = serial8250_timeout;
1935 	if (port->irq)
1936 		serial_unlink_irq_chain(up);
1937 }
1938 
1939 static unsigned int serial8250_tx_empty(struct uart_port *port)
1940 {
1941 	struct uart_8250_port *up = up_to_u8250p(port);
1942 	unsigned long flags;
1943 	unsigned int lsr;
1944 
1945 	serial8250_rpm_get(up);
1946 
1947 	spin_lock_irqsave(&port->lock, flags);
1948 	lsr = serial_port_in(port, UART_LSR);
1949 	up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1950 	spin_unlock_irqrestore(&port->lock, flags);
1951 
1952 	serial8250_rpm_put(up);
1953 
1954 	return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
1955 }
1956 
1957 static unsigned int serial8250_get_mctrl(struct uart_port *port)
1958 {
1959 	struct uart_8250_port *up = up_to_u8250p(port);
1960 	unsigned int status;
1961 	unsigned int ret;
1962 
1963 	serial8250_rpm_get(up);
1964 	status = serial8250_modem_status(up);
1965 	serial8250_rpm_put(up);
1966 
1967 	ret = 0;
1968 	if (status & UART_MSR_DCD)
1969 		ret |= TIOCM_CAR;
1970 	if (status & UART_MSR_RI)
1971 		ret |= TIOCM_RNG;
1972 	if (status & UART_MSR_DSR)
1973 		ret |= TIOCM_DSR;
1974 	if (status & UART_MSR_CTS)
1975 		ret |= TIOCM_CTS;
1976 	return ret;
1977 }
1978 
1979 void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl)
1980 {
1981 	struct uart_8250_port *up = up_to_u8250p(port);
1982 	unsigned char mcr = 0;
1983 
1984 	if (mctrl & TIOCM_RTS)
1985 		mcr |= UART_MCR_RTS;
1986 	if (mctrl & TIOCM_DTR)
1987 		mcr |= UART_MCR_DTR;
1988 	if (mctrl & TIOCM_OUT1)
1989 		mcr |= UART_MCR_OUT1;
1990 	if (mctrl & TIOCM_OUT2)
1991 		mcr |= UART_MCR_OUT2;
1992 	if (mctrl & TIOCM_LOOP)
1993 		mcr |= UART_MCR_LOOP;
1994 
1995 	mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1996 
1997 	serial_port_out(port, UART_MCR, mcr);
1998 }
1999 EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);
2000 
2001 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
2002 {
2003 	if (port->set_mctrl)
2004 		return port->set_mctrl(port, mctrl);
2005 	return serial8250_do_set_mctrl(port, mctrl);
2006 }
2007 
2008 static void serial8250_break_ctl(struct uart_port *port, int break_state)
2009 {
2010 	struct uart_8250_port *up = up_to_u8250p(port);
2011 	unsigned long flags;
2012 
2013 	serial8250_rpm_get(up);
2014 	spin_lock_irqsave(&port->lock, flags);
2015 	if (break_state == -1)
2016 		up->lcr |= UART_LCR_SBC;
2017 	else
2018 		up->lcr &= ~UART_LCR_SBC;
2019 	serial_port_out(port, UART_LCR, up->lcr);
2020 	spin_unlock_irqrestore(&port->lock, flags);
2021 	serial8250_rpm_put(up);
2022 }
2023 
2024 /*
2025  *	Wait for transmitter & holding register to empty
2026  */
2027 static void wait_for_xmitr(struct uart_8250_port *up, int bits)
2028 {
2029 	unsigned int status, tmout = 10000;
2030 
2031 	/* Wait up to 10ms for the character(s) to be sent. */
2032 	for (;;) {
2033 		status = serial_in(up, UART_LSR);
2034 
2035 		up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
2036 
2037 		if ((status & bits) == bits)
2038 			break;
2039 		if (--tmout == 0)
2040 			break;
2041 		udelay(1);
2042 	}
2043 
2044 	/* Wait up to 1s for flow control if necessary */
2045 	if (up->port.flags & UPF_CONS_FLOW) {
2046 		unsigned int tmout;
2047 		for (tmout = 1000000; tmout; tmout--) {
2048 			unsigned int msr = serial_in(up, UART_MSR);
2049 			up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
2050 			if (msr & UART_MSR_CTS)
2051 				break;
2052 			udelay(1);
2053 			touch_nmi_watchdog();
2054 		}
2055 	}
2056 }
2057 
2058 #ifdef CONFIG_CONSOLE_POLL
2059 /*
2060  * Console polling routines for writing and reading from the uart while
2061  * in an interrupt or debug context.
2062  */
2063 
2064 static int serial8250_get_poll_char(struct uart_port *port)
2065 {
2066 	struct uart_8250_port *up = up_to_u8250p(port);
2067 	unsigned char lsr;
2068 	int status;
2069 
2070 	serial8250_rpm_get(up);
2071 
2072 	lsr = serial_port_in(port, UART_LSR);
2073 
2074 	if (!(lsr & UART_LSR_DR)) {
2075 		status = NO_POLL_CHAR;
2076 		goto out;
2077 	}
2078 
2079 	status = serial_port_in(port, UART_RX);
2080 out:
2081 	serial8250_rpm_put(up);
2082 	return status;
2083 }
2084 
2085 
2086 static void serial8250_put_poll_char(struct uart_port *port,
2087 			 unsigned char c)
2088 {
2089 	unsigned int ier;
2090 	struct uart_8250_port *up = up_to_u8250p(port);
2091 
2092 	serial8250_rpm_get(up);
2093 	/*
2094 	 *	First save the IER then disable the interrupts
2095 	 */
2096 	ier = serial_port_in(port, UART_IER);
2097 	if (up->capabilities & UART_CAP_UUE)
2098 		serial_port_out(port, UART_IER, UART_IER_UUE);
2099 	else
2100 		serial_port_out(port, UART_IER, 0);
2101 
2102 	wait_for_xmitr(up, BOTH_EMPTY);
2103 	/*
2104 	 *	Send the character out.
2105 	 */
2106 	serial_port_out(port, UART_TX, c);
2107 
2108 	/*
2109 	 *	Finally, wait for transmitter to become empty
2110 	 *	and restore the IER
2111 	 */
2112 	wait_for_xmitr(up, BOTH_EMPTY);
2113 	serial_port_out(port, UART_IER, ier);
2114 	serial8250_rpm_put(up);
2115 }
2116 
2117 #endif /* CONFIG_CONSOLE_POLL */
2118 
2119 int serial8250_do_startup(struct uart_port *port)
2120 {
2121 	struct uart_8250_port *up = up_to_u8250p(port);
2122 	unsigned long flags;
2123 	unsigned char lsr, iir;
2124 	int retval;
2125 
2126 	if (port->type == PORT_8250_CIR)
2127 		return -ENODEV;
2128 
2129 	if (!port->fifosize)
2130 		port->fifosize = uart_config[port->type].fifo_size;
2131 	if (!up->tx_loadsz)
2132 		up->tx_loadsz = uart_config[port->type].tx_loadsz;
2133 	if (!up->capabilities)
2134 		up->capabilities = uart_config[port->type].flags;
2135 	up->mcr = 0;
2136 
2137 	if (port->iotype != up->cur_iotype)
2138 		set_io_from_upio(port);
2139 
2140 	serial8250_rpm_get(up);
2141 	if (port->type == PORT_16C950) {
2142 		/* Wake up and initialize UART */
2143 		up->acr = 0;
2144 		serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2145 		serial_port_out(port, UART_EFR, UART_EFR_ECB);
2146 		serial_port_out(port, UART_IER, 0);
2147 		serial_port_out(port, UART_LCR, 0);
2148 		serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2149 		serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2150 		serial_port_out(port, UART_EFR, UART_EFR_ECB);
2151 		serial_port_out(port, UART_LCR, 0);
2152 	}
2153 
2154 #ifdef CONFIG_SERIAL_8250_RSA
2155 	/*
2156 	 * If this is an RSA port, see if we can kick it up to the
2157 	 * higher speed clock.
2158 	 */
2159 	enable_rsa(up);
2160 #endif
2161 	/*
2162 	 * Clear the FIFO buffers and disable them.
2163 	 * (they will be reenabled in set_termios())
2164 	 */
2165 	serial8250_clear_fifos(up);
2166 
2167 	/*
2168 	 * Clear the interrupt registers.
2169 	 */
2170 	serial_port_in(port, UART_LSR);
2171 	serial_port_in(port, UART_RX);
2172 	serial_port_in(port, UART_IIR);
2173 	serial_port_in(port, UART_MSR);
2174 
2175 	/*
2176 	 * At this point, there's no way the LSR could still be 0xff;
2177 	 * if it is, then bail out, because there's likely no UART
2178 	 * here.
2179 	 */
2180 	if (!(port->flags & UPF_BUGGY_UART) &&
2181 	    (serial_port_in(port, UART_LSR) == 0xff)) {
2182 		printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2183 				   serial_index(port));
2184 		retval = -ENODEV;
2185 		goto out;
2186 	}
2187 
2188 	/*
2189 	 * For a XR16C850, we need to set the trigger levels
2190 	 */
2191 	if (port->type == PORT_16850) {
2192 		unsigned char fctr;
2193 
2194 		serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
2195 
2196 		fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2197 		serial_port_out(port, UART_FCTR,
2198 				fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2199 		serial_port_out(port, UART_TRG, UART_TRG_96);
2200 		serial_port_out(port, UART_FCTR,
2201 				fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2202 		serial_port_out(port, UART_TRG, UART_TRG_96);
2203 
2204 		serial_port_out(port, UART_LCR, 0);
2205 	}
2206 
2207 	if (port->irq) {
2208 		unsigned char iir1;
2209 		/*
2210 		 * Test for UARTs that do not reassert THRE when the
2211 		 * transmitter is idle and the interrupt has already
2212 		 * been cleared.  Real 16550s should always reassert
2213 		 * this interrupt whenever the transmitter is idle and
2214 		 * the interrupt is enabled.  Delays are necessary to
2215 		 * allow register changes to become visible.
2216 		 */
2217 		spin_lock_irqsave(&port->lock, flags);
2218 		if (up->port.irqflags & IRQF_SHARED)
2219 			disable_irq_nosync(port->irq);
2220 
2221 		wait_for_xmitr(up, UART_LSR_THRE);
2222 		serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2223 		udelay(1); /* allow THRE to set */
2224 		iir1 = serial_port_in(port, UART_IIR);
2225 		serial_port_out(port, UART_IER, 0);
2226 		serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2227 		udelay(1); /* allow a working UART time to re-assert THRE */
2228 		iir = serial_port_in(port, UART_IIR);
2229 		serial_port_out(port, UART_IER, 0);
2230 
2231 		if (port->irqflags & IRQF_SHARED)
2232 			enable_irq(port->irq);
2233 		spin_unlock_irqrestore(&port->lock, flags);
2234 
2235 		/*
2236 		 * If the interrupt is not reasserted, or we otherwise
2237 		 * don't trust the iir, setup a timer to kick the UART
2238 		 * on a regular basis.
2239 		 */
2240 		if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
2241 		    up->port.flags & UPF_BUG_THRE) {
2242 			up->bugs |= UART_BUG_THRE;
2243 		}
2244 	}
2245 
2246 	retval = up->ops->setup_irq(up);
2247 	if (retval)
2248 		goto out;
2249 
2250 	/*
2251 	 * Now, initialize the UART
2252 	 */
2253 	serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
2254 
2255 	spin_lock_irqsave(&port->lock, flags);
2256 	if (up->port.flags & UPF_FOURPORT) {
2257 		if (!up->port.irq)
2258 			up->port.mctrl |= TIOCM_OUT1;
2259 	} else
2260 		/*
2261 		 * Most PC uarts need OUT2 raised to enable interrupts.
2262 		 */
2263 		if (port->irq)
2264 			up->port.mctrl |= TIOCM_OUT2;
2265 
2266 	serial8250_set_mctrl(port, port->mctrl);
2267 
2268 	/* Serial over Lan (SoL) hack:
2269 	   Intel 8257x Gigabit ethernet chips have a
2270 	   16550 emulation, to be used for Serial Over Lan.
2271 	   Those chips take a longer time than a normal
2272 	   serial device to signalize that a transmission
2273 	   data was queued. Due to that, the above test generally
2274 	   fails. One solution would be to delay the reading of
2275 	   iir. However, this is not reliable, since the timeout
2276 	   is variable. So, let's just don't test if we receive
2277 	   TX irq. This way, we'll never enable UART_BUG_TXEN.
2278 	 */
2279 	if (up->port.flags & UPF_NO_TXEN_TEST)
2280 		goto dont_test_tx_en;
2281 
2282 	/*
2283 	 * Do a quick test to see if we receive an
2284 	 * interrupt when we enable the TX irq.
2285 	 */
2286 	serial_port_out(port, UART_IER, UART_IER_THRI);
2287 	lsr = serial_port_in(port, UART_LSR);
2288 	iir = serial_port_in(port, UART_IIR);
2289 	serial_port_out(port, UART_IER, 0);
2290 
2291 	if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2292 		if (!(up->bugs & UART_BUG_TXEN)) {
2293 			up->bugs |= UART_BUG_TXEN;
2294 			pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2295 				 serial_index(port));
2296 		}
2297 	} else {
2298 		up->bugs &= ~UART_BUG_TXEN;
2299 	}
2300 
2301 dont_test_tx_en:
2302 	spin_unlock_irqrestore(&port->lock, flags);
2303 
2304 	/*
2305 	 * Clear the interrupt registers again for luck, and clear the
2306 	 * saved flags to avoid getting false values from polling
2307 	 * routines or the previous session.
2308 	 */
2309 	serial_port_in(port, UART_LSR);
2310 	serial_port_in(port, UART_RX);
2311 	serial_port_in(port, UART_IIR);
2312 	serial_port_in(port, UART_MSR);
2313 	up->lsr_saved_flags = 0;
2314 	up->msr_saved_flags = 0;
2315 
2316 	/*
2317 	 * Request DMA channels for both RX and TX.
2318 	 */
2319 	if (up->dma) {
2320 		retval = serial8250_request_dma(up);
2321 		if (retval) {
2322 			pr_warn_ratelimited("ttyS%d - failed to request DMA\n",
2323 					    serial_index(port));
2324 			up->dma = NULL;
2325 		}
2326 	}
2327 
2328 	/*
2329 	 * Finally, enable interrupts.  Note: Modem status interrupts
2330 	 * are set via set_termios(), which will be occurring imminently
2331 	 * anyway, so we don't enable them here.
2332 	 */
2333 	up->ier = UART_IER_RLSI | UART_IER_RDI;
2334 	serial_port_out(port, UART_IER, up->ier);
2335 
2336 	if (port->flags & UPF_FOURPORT) {
2337 		unsigned int icp;
2338 		/*
2339 		 * Enable interrupts on the AST Fourport board
2340 		 */
2341 		icp = (port->iobase & 0xfe0) | 0x01f;
2342 		outb_p(0x80, icp);
2343 		inb_p(icp);
2344 	}
2345 	retval = 0;
2346 out:
2347 	serial8250_rpm_put(up);
2348 	return retval;
2349 }
2350 EXPORT_SYMBOL_GPL(serial8250_do_startup);
2351 
2352 static int serial8250_startup(struct uart_port *port)
2353 {
2354 	if (port->startup)
2355 		return port->startup(port);
2356 	return serial8250_do_startup(port);
2357 }
2358 
2359 void serial8250_do_shutdown(struct uart_port *port)
2360 {
2361 	struct uart_8250_port *up = up_to_u8250p(port);
2362 	unsigned long flags;
2363 
2364 	serial8250_rpm_get(up);
2365 	/*
2366 	 * Disable interrupts from this port
2367 	 */
2368 	up->ier = 0;
2369 	serial_port_out(port, UART_IER, 0);
2370 
2371 	if (up->dma)
2372 		serial8250_release_dma(up);
2373 
2374 	spin_lock_irqsave(&port->lock, flags);
2375 	if (port->flags & UPF_FOURPORT) {
2376 		/* reset interrupts on the AST Fourport board */
2377 		inb((port->iobase & 0xfe0) | 0x1f);
2378 		port->mctrl |= TIOCM_OUT1;
2379 	} else
2380 		port->mctrl &= ~TIOCM_OUT2;
2381 
2382 	serial8250_set_mctrl(port, port->mctrl);
2383 	spin_unlock_irqrestore(&port->lock, flags);
2384 
2385 	/*
2386 	 * Disable break condition and FIFOs
2387 	 */
2388 	serial_port_out(port, UART_LCR,
2389 			serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
2390 	serial8250_clear_fifos(up);
2391 
2392 #ifdef CONFIG_SERIAL_8250_RSA
2393 	/*
2394 	 * Reset the RSA board back to 115kbps compat mode.
2395 	 */
2396 	disable_rsa(up);
2397 #endif
2398 
2399 	/*
2400 	 * Read data port to reset things, and then unlink from
2401 	 * the IRQ chain.
2402 	 */
2403 	serial_port_in(port, UART_RX);
2404 	serial8250_rpm_put(up);
2405 
2406 	up->ops->release_irq(up);
2407 }
2408 EXPORT_SYMBOL_GPL(serial8250_do_shutdown);
2409 
2410 static void serial8250_shutdown(struct uart_port *port)
2411 {
2412 	if (port->shutdown)
2413 		port->shutdown(port);
2414 	else
2415 		serial8250_do_shutdown(port);
2416 }
2417 
2418 /*
2419  * XR17V35x UARTs have an extra fractional divisor register (DLD)
2420  * Calculate divisor with extra 4-bit fractional portion
2421  */
2422 static unsigned int xr17v35x_get_divisor(struct uart_8250_port *up,
2423 					 unsigned int baud,
2424 					 unsigned int *frac)
2425 {
2426 	struct uart_port *port = &up->port;
2427 	unsigned int quot_16;
2428 
2429 	quot_16 = DIV_ROUND_CLOSEST(port->uartclk, baud);
2430 	*frac = quot_16 & 0x0f;
2431 
2432 	return quot_16 >> 4;
2433 }
2434 
2435 static unsigned int serial8250_get_divisor(struct uart_8250_port *up,
2436 					   unsigned int baud,
2437 					   unsigned int *frac)
2438 {
2439 	struct uart_port *port = &up->port;
2440 	unsigned int quot;
2441 
2442 	/*
2443 	 * Handle magic divisors for baud rates above baud_base on
2444 	 * SMSC SuperIO chips.
2445 	 *
2446 	 */
2447 	if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2448 	    baud == (port->uartclk/4))
2449 		quot = 0x8001;
2450 	else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2451 		 baud == (port->uartclk/8))
2452 		quot = 0x8002;
2453 	else if (up->port.type == PORT_XR17V35X)
2454 		quot = xr17v35x_get_divisor(up, baud, frac);
2455 	else
2456 		quot = uart_get_divisor(port, baud);
2457 
2458 	/*
2459 	 * Oxford Semi 952 rev B workaround
2460 	 */
2461 	if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2462 		quot++;
2463 
2464 	return quot;
2465 }
2466 
2467 static unsigned char serial8250_compute_lcr(struct uart_8250_port *up,
2468 					    tcflag_t c_cflag)
2469 {
2470 	unsigned char cval;
2471 
2472 	switch (c_cflag & CSIZE) {
2473 	case CS5:
2474 		cval = UART_LCR_WLEN5;
2475 		break;
2476 	case CS6:
2477 		cval = UART_LCR_WLEN6;
2478 		break;
2479 	case CS7:
2480 		cval = UART_LCR_WLEN7;
2481 		break;
2482 	default:
2483 	case CS8:
2484 		cval = UART_LCR_WLEN8;
2485 		break;
2486 	}
2487 
2488 	if (c_cflag & CSTOPB)
2489 		cval |= UART_LCR_STOP;
2490 	if (c_cflag & PARENB) {
2491 		cval |= UART_LCR_PARITY;
2492 		if (up->bugs & UART_BUG_PARITY)
2493 			up->fifo_bug = true;
2494 	}
2495 	if (!(c_cflag & PARODD))
2496 		cval |= UART_LCR_EPAR;
2497 #ifdef CMSPAR
2498 	if (c_cflag & CMSPAR)
2499 		cval |= UART_LCR_SPAR;
2500 #endif
2501 
2502 	return cval;
2503 }
2504 
2505 static void serial8250_set_divisor(struct uart_port *port, unsigned int baud,
2506 			    unsigned int quot, unsigned int quot_frac)
2507 {
2508 	struct uart_8250_port *up = up_to_u8250p(port);
2509 
2510 	/* Workaround to enable 115200 baud on OMAP1510 internal ports */
2511 	if (is_omap1510_8250(up)) {
2512 		if (baud == 115200) {
2513 			quot = 1;
2514 			serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
2515 		} else
2516 			serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
2517 	}
2518 
2519 	/*
2520 	 * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
2521 	 * otherwise just set DLAB
2522 	 */
2523 	if (up->capabilities & UART_NATSEMI)
2524 		serial_port_out(port, UART_LCR, 0xe0);
2525 	else
2526 		serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
2527 
2528 	serial_dl_write(up, quot);
2529 
2530 	/* XR17V35x UARTs have an extra fractional divisor register (DLD) */
2531 	if (up->port.type == PORT_XR17V35X)
2532 		serial_port_out(port, 0x2, quot_frac);
2533 }
2534 
2535 void
2536 serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2537 		          struct ktermios *old)
2538 {
2539 	struct uart_8250_port *up = up_to_u8250p(port);
2540 	unsigned char cval;
2541 	unsigned long flags;
2542 	unsigned int baud, quot, frac = 0;
2543 
2544 	cval = serial8250_compute_lcr(up, termios->c_cflag);
2545 
2546 	/*
2547 	 * Ask the core to calculate the divisor for us.
2548 	 */
2549 	baud = uart_get_baud_rate(port, termios, old,
2550 				  port->uartclk / 16 / 0xffff,
2551 				  port->uartclk / 16);
2552 	quot = serial8250_get_divisor(up, baud, &frac);
2553 
2554 	/*
2555 	 * Ok, we're now changing the port state.  Do it with
2556 	 * interrupts disabled.
2557 	 */
2558 	serial8250_rpm_get(up);
2559 	spin_lock_irqsave(&port->lock, flags);
2560 
2561 	up->lcr = cval;					/* Save computed LCR */
2562 
2563 	if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
2564 		/* NOTE: If fifo_bug is not set, a user can set RX_trigger. */
2565 		if ((baud < 2400 && !up->dma) || up->fifo_bug) {
2566 			up->fcr &= ~UART_FCR_TRIGGER_MASK;
2567 			up->fcr |= UART_FCR_TRIGGER_1;
2568 		}
2569 	}
2570 
2571 	/*
2572 	 * MCR-based auto flow control.  When AFE is enabled, RTS will be
2573 	 * deasserted when the receive FIFO contains more characters than
2574 	 * the trigger, or the MCR RTS bit is cleared.  In the case where
2575 	 * the remote UART is not using CTS auto flow control, we must
2576 	 * have sufficient FIFO entries for the latency of the remote
2577 	 * UART to respond.  IOW, at least 32 bytes of FIFO.
2578 	 */
2579 	if (up->capabilities & UART_CAP_AFE && port->fifosize >= 32) {
2580 		up->mcr &= ~UART_MCR_AFE;
2581 		if (termios->c_cflag & CRTSCTS)
2582 			up->mcr |= UART_MCR_AFE;
2583 	}
2584 
2585 	/*
2586 	 * Update the per-port timeout.
2587 	 */
2588 	uart_update_timeout(port, termios->c_cflag, baud);
2589 
2590 	port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2591 	if (termios->c_iflag & INPCK)
2592 		port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2593 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2594 		port->read_status_mask |= UART_LSR_BI;
2595 
2596 	/*
2597 	 * Characteres to ignore
2598 	 */
2599 	port->ignore_status_mask = 0;
2600 	if (termios->c_iflag & IGNPAR)
2601 		port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2602 	if (termios->c_iflag & IGNBRK) {
2603 		port->ignore_status_mask |= UART_LSR_BI;
2604 		/*
2605 		 * If we're ignoring parity and break indicators,
2606 		 * ignore overruns too (for real raw support).
2607 		 */
2608 		if (termios->c_iflag & IGNPAR)
2609 			port->ignore_status_mask |= UART_LSR_OE;
2610 	}
2611 
2612 	/*
2613 	 * ignore all characters if CREAD is not set
2614 	 */
2615 	if ((termios->c_cflag & CREAD) == 0)
2616 		port->ignore_status_mask |= UART_LSR_DR;
2617 
2618 	/*
2619 	 * CTS flow control flag and modem status interrupts
2620 	 */
2621 	up->ier &= ~UART_IER_MSI;
2622 	if (!(up->bugs & UART_BUG_NOMSR) &&
2623 			UART_ENABLE_MS(&up->port, termios->c_cflag))
2624 		up->ier |= UART_IER_MSI;
2625 	if (up->capabilities & UART_CAP_UUE)
2626 		up->ier |= UART_IER_UUE;
2627 	if (up->capabilities & UART_CAP_RTOIE)
2628 		up->ier |= UART_IER_RTOIE;
2629 
2630 	serial_port_out(port, UART_IER, up->ier);
2631 
2632 	if (up->capabilities & UART_CAP_EFR) {
2633 		unsigned char efr = 0;
2634 		/*
2635 		 * TI16C752/Startech hardware flow control.  FIXME:
2636 		 * - TI16C752 requires control thresholds to be set.
2637 		 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2638 		 */
2639 		if (termios->c_cflag & CRTSCTS)
2640 			efr |= UART_EFR_CTS;
2641 
2642 		serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2643 		if (port->flags & UPF_EXAR_EFR)
2644 			serial_port_out(port, UART_XR_EFR, efr);
2645 		else
2646 			serial_port_out(port, UART_EFR, efr);
2647 	}
2648 
2649 	serial8250_set_divisor(port, baud, quot, frac);
2650 
2651 	/*
2652 	 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2653 	 * is written without DLAB set, this mode will be disabled.
2654 	 */
2655 	if (port->type == PORT_16750)
2656 		serial_port_out(port, UART_FCR, up->fcr);
2657 
2658 	serial_port_out(port, UART_LCR, up->lcr);	/* reset DLAB */
2659 	if (port->type != PORT_16750) {
2660 		/* emulated UARTs (Lucent Venus 167x) need two steps */
2661 		if (up->fcr & UART_FCR_ENABLE_FIFO)
2662 			serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
2663 		serial_port_out(port, UART_FCR, up->fcr);	/* set fcr */
2664 	}
2665 	serial8250_set_mctrl(port, port->mctrl);
2666 	spin_unlock_irqrestore(&port->lock, flags);
2667 	serial8250_rpm_put(up);
2668 
2669 	/* Don't rewrite B0 */
2670 	if (tty_termios_baud_rate(termios))
2671 		tty_termios_encode_baud_rate(termios, baud, baud);
2672 }
2673 EXPORT_SYMBOL(serial8250_do_set_termios);
2674 
2675 static void
2676 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2677 		       struct ktermios *old)
2678 {
2679 	if (port->set_termios)
2680 		port->set_termios(port, termios, old);
2681 	else
2682 		serial8250_do_set_termios(port, termios, old);
2683 }
2684 
2685 static void
2686 serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios)
2687 {
2688 	if (termios->c_line == N_PPS) {
2689 		port->flags |= UPF_HARDPPS_CD;
2690 		spin_lock_irq(&port->lock);
2691 		serial8250_enable_ms(port);
2692 		spin_unlock_irq(&port->lock);
2693 	} else {
2694 		port->flags &= ~UPF_HARDPPS_CD;
2695 		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2696 			spin_lock_irq(&port->lock);
2697 			serial8250_disable_ms(port);
2698 			spin_unlock_irq(&port->lock);
2699 		}
2700 	}
2701 }
2702 
2703 
2704 void serial8250_do_pm(struct uart_port *port, unsigned int state,
2705 		      unsigned int oldstate)
2706 {
2707 	struct uart_8250_port *p = up_to_u8250p(port);
2708 
2709 	serial8250_set_sleep(p, state != 0);
2710 }
2711 EXPORT_SYMBOL(serial8250_do_pm);
2712 
2713 static void
2714 serial8250_pm(struct uart_port *port, unsigned int state,
2715 	      unsigned int oldstate)
2716 {
2717 	if (port->pm)
2718 		port->pm(port, state, oldstate);
2719 	else
2720 		serial8250_do_pm(port, state, oldstate);
2721 }
2722 
2723 static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2724 {
2725 	if (pt->port.mapsize)
2726 		return pt->port.mapsize;
2727 	if (pt->port.iotype == UPIO_AU) {
2728 		if (pt->port.type == PORT_RT2880)
2729 			return 0x100;
2730 		return 0x1000;
2731 	}
2732 	if (is_omap1_8250(pt))
2733 		return 0x16 << pt->port.regshift;
2734 
2735 	return 8 << pt->port.regshift;
2736 }
2737 
2738 /*
2739  * Resource handling.
2740  */
2741 static int serial8250_request_std_resource(struct uart_8250_port *up)
2742 {
2743 	unsigned int size = serial8250_port_size(up);
2744 	struct uart_port *port = &up->port;
2745 	int ret = 0;
2746 
2747 	switch (port->iotype) {
2748 	case UPIO_AU:
2749 	case UPIO_TSI:
2750 	case UPIO_MEM32:
2751 	case UPIO_MEM:
2752 		if (!port->mapbase)
2753 			break;
2754 
2755 		if (!request_mem_region(port->mapbase, size, "serial")) {
2756 			ret = -EBUSY;
2757 			break;
2758 		}
2759 
2760 		if (port->flags & UPF_IOREMAP) {
2761 			port->membase = ioremap_nocache(port->mapbase, size);
2762 			if (!port->membase) {
2763 				release_mem_region(port->mapbase, size);
2764 				ret = -ENOMEM;
2765 			}
2766 		}
2767 		break;
2768 
2769 	case UPIO_HUB6:
2770 	case UPIO_PORT:
2771 		if (!request_region(port->iobase, size, "serial"))
2772 			ret = -EBUSY;
2773 		break;
2774 	}
2775 	return ret;
2776 }
2777 
2778 static void serial8250_release_std_resource(struct uart_8250_port *up)
2779 {
2780 	unsigned int size = serial8250_port_size(up);
2781 	struct uart_port *port = &up->port;
2782 
2783 	switch (port->iotype) {
2784 	case UPIO_AU:
2785 	case UPIO_TSI:
2786 	case UPIO_MEM32:
2787 	case UPIO_MEM:
2788 		if (!port->mapbase)
2789 			break;
2790 
2791 		if (port->flags & UPF_IOREMAP) {
2792 			iounmap(port->membase);
2793 			port->membase = NULL;
2794 		}
2795 
2796 		release_mem_region(port->mapbase, size);
2797 		break;
2798 
2799 	case UPIO_HUB6:
2800 	case UPIO_PORT:
2801 		release_region(port->iobase, size);
2802 		break;
2803 	}
2804 }
2805 
2806 #ifdef CONFIG_SERIAL_8250_RSA
2807 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2808 {
2809 	unsigned long start = UART_RSA_BASE << up->port.regshift;
2810 	unsigned int size = 8 << up->port.regshift;
2811 	struct uart_port *port = &up->port;
2812 	int ret = -EINVAL;
2813 
2814 	switch (port->iotype) {
2815 	case UPIO_HUB6:
2816 	case UPIO_PORT:
2817 		start += port->iobase;
2818 		if (request_region(start, size, "serial-rsa"))
2819 			ret = 0;
2820 		else
2821 			ret = -EBUSY;
2822 		break;
2823 	}
2824 
2825 	return ret;
2826 }
2827 
2828 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2829 {
2830 	unsigned long offset = UART_RSA_BASE << up->port.regshift;
2831 	unsigned int size = 8 << up->port.regshift;
2832 	struct uart_port *port = &up->port;
2833 
2834 	switch (port->iotype) {
2835 	case UPIO_HUB6:
2836 	case UPIO_PORT:
2837 		release_region(port->iobase + offset, size);
2838 		break;
2839 	}
2840 }
2841 #endif
2842 
2843 static void serial8250_release_port(struct uart_port *port)
2844 {
2845 	struct uart_8250_port *up = up_to_u8250p(port);
2846 
2847 	serial8250_release_std_resource(up);
2848 }
2849 
2850 static int serial8250_request_port(struct uart_port *port)
2851 {
2852 	struct uart_8250_port *up = up_to_u8250p(port);
2853 	int ret;
2854 
2855 	if (port->type == PORT_8250_CIR)
2856 		return -ENODEV;
2857 
2858 	ret = serial8250_request_std_resource(up);
2859 
2860 	return ret;
2861 }
2862 
2863 static int fcr_get_rxtrig_bytes(struct uart_8250_port *up)
2864 {
2865 	const struct serial8250_config *conf_type = &uart_config[up->port.type];
2866 	unsigned char bytes;
2867 
2868 	bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)];
2869 
2870 	return bytes ? bytes : -EOPNOTSUPP;
2871 }
2872 
2873 static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes)
2874 {
2875 	const struct serial8250_config *conf_type = &uart_config[up->port.type];
2876 	int i;
2877 
2878 	if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)])
2879 		return -EOPNOTSUPP;
2880 
2881 	for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) {
2882 		if (bytes < conf_type->rxtrig_bytes[i])
2883 			/* Use the nearest lower value */
2884 			return (--i) << UART_FCR_R_TRIG_SHIFT;
2885 	}
2886 
2887 	return UART_FCR_R_TRIG_11;
2888 }
2889 
2890 static int do_get_rxtrig(struct tty_port *port)
2891 {
2892 	struct uart_state *state = container_of(port, struct uart_state, port);
2893 	struct uart_port *uport = state->uart_port;
2894 	struct uart_8250_port *up =
2895 		container_of(uport, struct uart_8250_port, port);
2896 
2897 	if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
2898 		return -EINVAL;
2899 
2900 	return fcr_get_rxtrig_bytes(up);
2901 }
2902 
2903 static int do_serial8250_get_rxtrig(struct tty_port *port)
2904 {
2905 	int rxtrig_bytes;
2906 
2907 	mutex_lock(&port->mutex);
2908 	rxtrig_bytes = do_get_rxtrig(port);
2909 	mutex_unlock(&port->mutex);
2910 
2911 	return rxtrig_bytes;
2912 }
2913 
2914 static ssize_t serial8250_get_attr_rx_trig_bytes(struct device *dev,
2915 	struct device_attribute *attr, char *buf)
2916 {
2917 	struct tty_port *port = dev_get_drvdata(dev);
2918 	int rxtrig_bytes;
2919 
2920 	rxtrig_bytes = do_serial8250_get_rxtrig(port);
2921 	if (rxtrig_bytes < 0)
2922 		return rxtrig_bytes;
2923 
2924 	return snprintf(buf, PAGE_SIZE, "%d\n", rxtrig_bytes);
2925 }
2926 
2927 static int do_set_rxtrig(struct tty_port *port, unsigned char bytes)
2928 {
2929 	struct uart_state *state = container_of(port, struct uart_state, port);
2930 	struct uart_port *uport = state->uart_port;
2931 	struct uart_8250_port *up =
2932 		container_of(uport, struct uart_8250_port, port);
2933 	int rxtrig;
2934 
2935 	if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1 ||
2936 	    up->fifo_bug)
2937 		return -EINVAL;
2938 
2939 	rxtrig = bytes_to_fcr_rxtrig(up, bytes);
2940 	if (rxtrig < 0)
2941 		return rxtrig;
2942 
2943 	serial8250_clear_fifos(up);
2944 	up->fcr &= ~UART_FCR_TRIGGER_MASK;
2945 	up->fcr |= (unsigned char)rxtrig;
2946 	serial_out(up, UART_FCR, up->fcr);
2947 	return 0;
2948 }
2949 
2950 static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes)
2951 {
2952 	int ret;
2953 
2954 	mutex_lock(&port->mutex);
2955 	ret = do_set_rxtrig(port, bytes);
2956 	mutex_unlock(&port->mutex);
2957 
2958 	return ret;
2959 }
2960 
2961 static ssize_t serial8250_set_attr_rx_trig_bytes(struct device *dev,
2962 	struct device_attribute *attr, const char *buf, size_t count)
2963 {
2964 	struct tty_port *port = dev_get_drvdata(dev);
2965 	unsigned char bytes;
2966 	int ret;
2967 
2968 	if (!count)
2969 		return -EINVAL;
2970 
2971 	ret = kstrtou8(buf, 10, &bytes);
2972 	if (ret < 0)
2973 		return ret;
2974 
2975 	ret = do_serial8250_set_rxtrig(port, bytes);
2976 	if (ret < 0)
2977 		return ret;
2978 
2979 	return count;
2980 }
2981 
2982 static DEVICE_ATTR(rx_trig_bytes, S_IRUSR | S_IWUSR | S_IRGRP,
2983 		   serial8250_get_attr_rx_trig_bytes,
2984 		   serial8250_set_attr_rx_trig_bytes);
2985 
2986 static struct attribute *serial8250_dev_attrs[] = {
2987 	&dev_attr_rx_trig_bytes.attr,
2988 	NULL,
2989 	};
2990 
2991 static struct attribute_group serial8250_dev_attr_group = {
2992 	.attrs = serial8250_dev_attrs,
2993 	};
2994 
2995 static void register_dev_spec_attr_grp(struct uart_8250_port *up)
2996 {
2997 	const struct serial8250_config *conf_type = &uart_config[up->port.type];
2998 
2999 	if (conf_type->rxtrig_bytes[0])
3000 		up->port.attr_group = &serial8250_dev_attr_group;
3001 }
3002 
3003 static void serial8250_config_port(struct uart_port *port, int flags)
3004 {
3005 	struct uart_8250_port *up = up_to_u8250p(port);
3006 	int ret;
3007 
3008 	if (port->type == PORT_8250_CIR)
3009 		return;
3010 
3011 	/*
3012 	 * Find the region that we can probe for.  This in turn
3013 	 * tells us whether we can probe for the type of port.
3014 	 */
3015 	ret = serial8250_request_std_resource(up);
3016 	if (ret < 0)
3017 		return;
3018 
3019 	if (port->iotype != up->cur_iotype)
3020 		set_io_from_upio(port);
3021 
3022 	if (flags & UART_CONFIG_TYPE)
3023 		autoconfig(up);
3024 
3025 	/* if access method is AU, it is a 16550 with a quirk */
3026 	if (port->type == PORT_16550A && port->iotype == UPIO_AU)
3027 		up->bugs |= UART_BUG_NOMSR;
3028 
3029 	/* HW bugs may trigger IRQ while IIR == NO_INT */
3030 	if (port->type == PORT_TEGRA)
3031 		up->bugs |= UART_BUG_NOMSR;
3032 
3033 	if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
3034 		autoconfig_irq(up);
3035 
3036 	if (port->type == PORT_UNKNOWN)
3037 		serial8250_release_std_resource(up);
3038 
3039 	/* Fixme: probably not the best place for this */
3040 	if ((port->type == PORT_XR17V35X) ||
3041 	   (port->type == PORT_XR17D15X))
3042 		port->handle_irq = exar_handle_irq;
3043 
3044 	register_dev_spec_attr_grp(up);
3045 	up->fcr = uart_config[up->port.type].fcr;
3046 }
3047 
3048 static int
3049 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
3050 {
3051 	if (ser->irq >= nr_irqs || ser->irq < 0 ||
3052 	    ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
3053 	    ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
3054 	    ser->type == PORT_STARTECH)
3055 		return -EINVAL;
3056 	return 0;
3057 }
3058 
3059 static const char *
3060 serial8250_type(struct uart_port *port)
3061 {
3062 	int type = port->type;
3063 
3064 	if (type >= ARRAY_SIZE(uart_config))
3065 		type = 0;
3066 	return uart_config[type].name;
3067 }
3068 
3069 static const struct uart_ops serial8250_pops = {
3070 	.tx_empty	= serial8250_tx_empty,
3071 	.set_mctrl	= serial8250_set_mctrl,
3072 	.get_mctrl	= serial8250_get_mctrl,
3073 	.stop_tx	= serial8250_stop_tx,
3074 	.start_tx	= serial8250_start_tx,
3075 	.throttle	= serial8250_throttle,
3076 	.unthrottle	= serial8250_unthrottle,
3077 	.stop_rx	= serial8250_stop_rx,
3078 	.enable_ms	= serial8250_enable_ms,
3079 	.break_ctl	= serial8250_break_ctl,
3080 	.startup	= serial8250_startup,
3081 	.shutdown	= serial8250_shutdown,
3082 	.set_termios	= serial8250_set_termios,
3083 	.set_ldisc	= serial8250_set_ldisc,
3084 	.pm		= serial8250_pm,
3085 	.type		= serial8250_type,
3086 	.release_port	= serial8250_release_port,
3087 	.request_port	= serial8250_request_port,
3088 	.config_port	= serial8250_config_port,
3089 	.verify_port	= serial8250_verify_port,
3090 #ifdef CONFIG_CONSOLE_POLL
3091 	.poll_get_char = serial8250_get_poll_char,
3092 	.poll_put_char = serial8250_put_poll_char,
3093 #endif
3094 };
3095 
3096 static const struct uart_ops *base_ops;
3097 static struct uart_ops univ8250_port_ops;
3098 
3099 static const struct uart_8250_ops univ8250_driver_ops = {
3100 	.setup_irq	= univ8250_setup_irq,
3101 	.release_irq	= univ8250_release_irq,
3102 };
3103 
3104 static struct uart_8250_port serial8250_ports[UART_NR];
3105 
3106 /**
3107  * serial8250_get_port - retrieve struct uart_8250_port
3108  * @line: serial line number
3109  *
3110  * This function retrieves struct uart_8250_port for the specific line.
3111  * This struct *must* *not* be used to perform a 8250 or serial core operation
3112  * which is not accessible otherwise. Its only purpose is to make the struct
3113  * accessible to the runtime-pm callbacks for context suspend/restore.
3114  * The lock assumption made here is none because runtime-pm suspend/resume
3115  * callbacks should not be invoked if there is any operation performed on the
3116  * port.
3117  */
3118 struct uart_8250_port *serial8250_get_port(int line)
3119 {
3120 	return &serial8250_ports[line];
3121 }
3122 EXPORT_SYMBOL_GPL(serial8250_get_port);
3123 
3124 static void (*serial8250_isa_config)(int port, struct uart_port *up,
3125 	unsigned short *capabilities);
3126 
3127 void serial8250_set_isa_configurator(
3128 	void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
3129 {
3130 	serial8250_isa_config = v;
3131 }
3132 EXPORT_SYMBOL(serial8250_set_isa_configurator);
3133 
3134 static void serial8250_init_port(struct uart_8250_port *up)
3135 {
3136 	struct uart_port *port = &up->port;
3137 
3138 	spin_lock_init(&port->lock);
3139 	port->ops = &serial8250_pops;
3140 
3141 	up->cur_iotype = 0xFF;
3142 }
3143 
3144 static void serial8250_set_defaults(struct uart_8250_port *up)
3145 {
3146 	struct uart_port *port = &up->port;
3147 
3148 	if (up->port.flags & UPF_FIXED_TYPE) {
3149 		unsigned int type = up->port.type;
3150 
3151 		if (!up->port.fifosize)
3152 			up->port.fifosize = uart_config[type].fifo_size;
3153 		if (!up->tx_loadsz)
3154 			up->tx_loadsz = uart_config[type].tx_loadsz;
3155 		if (!up->capabilities)
3156 			up->capabilities = uart_config[type].flags;
3157 	}
3158 
3159 	set_io_from_upio(port);
3160 
3161 	/* default dma handlers */
3162 	if (up->dma) {
3163 		if (!up->dma->tx_dma)
3164 			up->dma->tx_dma = serial8250_tx_dma;
3165 		if (!up->dma->rx_dma)
3166 			up->dma->rx_dma = serial8250_rx_dma;
3167 	}
3168 }
3169 
3170 #ifdef CONFIG_SERIAL_8250_RSA
3171 
3172 static void univ8250_config_port(struct uart_port *port, int flags)
3173 {
3174 	struct uart_8250_port *up = up_to_u8250p(port);
3175 
3176 	up->probe &= ~UART_PROBE_RSA;
3177 	if (port->type == PORT_RSA) {
3178 		if (serial8250_request_rsa_resource(up) == 0)
3179 			up->probe |= UART_PROBE_RSA;
3180 	} else if (flags & UART_CONFIG_TYPE) {
3181 		int i;
3182 
3183 		for (i = 0; i < probe_rsa_count; i++) {
3184 			if (probe_rsa[i] == up->port.iobase) {
3185 				if (serial8250_request_rsa_resource(up) == 0)
3186 					up->probe |= UART_PROBE_RSA;
3187 				break;
3188 			}
3189 		}
3190 	}
3191 
3192 	base_ops->config_port(port, flags);
3193 
3194 	if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
3195 		serial8250_release_rsa_resource(up);
3196 }
3197 
3198 static int univ8250_request_port(struct uart_port *port)
3199 {
3200 	struct uart_8250_port *up = up_to_u8250p(port);
3201 	int ret;
3202 
3203 	ret = base_ops->request_port(port);
3204 	if (ret == 0 && port->type == PORT_RSA) {
3205 		ret = serial8250_request_rsa_resource(up);
3206 		if (ret < 0)
3207 			base_ops->release_port(port);
3208 	}
3209 
3210 	return ret;
3211 }
3212 
3213 static void univ8250_release_port(struct uart_port *port)
3214 {
3215 	struct uart_8250_port *up = up_to_u8250p(port);
3216 
3217 	if (port->type == PORT_RSA)
3218 		serial8250_release_rsa_resource(up);
3219 	base_ops->release_port(port);
3220 }
3221 
3222 static void univ8250_rsa_support(struct uart_ops *ops)
3223 {
3224 	ops->config_port  = univ8250_config_port;
3225 	ops->request_port = univ8250_request_port;
3226 	ops->release_port = univ8250_release_port;
3227 }
3228 
3229 #else
3230 #define univ8250_rsa_support(x)		do { } while (0)
3231 #endif /* CONFIG_SERIAL_8250_RSA */
3232 
3233 static void __init serial8250_isa_init_ports(void)
3234 {
3235 	struct uart_8250_port *up;
3236 	static int first = 1;
3237 	int i, irqflag = 0;
3238 
3239 	if (!first)
3240 		return;
3241 	first = 0;
3242 
3243 	if (nr_uarts > UART_NR)
3244 		nr_uarts = UART_NR;
3245 
3246 	for (i = 0; i < nr_uarts; i++) {
3247 		struct uart_8250_port *up = &serial8250_ports[i];
3248 		struct uart_port *port = &up->port;
3249 
3250 		port->line = i;
3251 		serial8250_init_port(up);
3252 		if (!base_ops)
3253 			base_ops = port->ops;
3254 		port->ops = &univ8250_port_ops;
3255 
3256 		init_timer(&up->timer);
3257 		up->timer.function = serial8250_timeout;
3258 
3259 		up->ops = &univ8250_driver_ops;
3260 
3261 		/*
3262 		 * ALPHA_KLUDGE_MCR needs to be killed.
3263 		 */
3264 		up->mcr_mask = ~ALPHA_KLUDGE_MCR;
3265 		up->mcr_force = ALPHA_KLUDGE_MCR;
3266 	}
3267 
3268 	/* chain base port ops to support Remote Supervisor Adapter */
3269 	univ8250_port_ops = *base_ops;
3270 	univ8250_rsa_support(&univ8250_port_ops);
3271 
3272 	if (share_irqs)
3273 		irqflag = IRQF_SHARED;
3274 
3275 	for (i = 0, up = serial8250_ports;
3276 	     i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
3277 	     i++, up++) {
3278 		struct uart_port *port = &up->port;
3279 
3280 		port->iobase   = old_serial_port[i].port;
3281 		port->irq      = irq_canonicalize(old_serial_port[i].irq);
3282 		port->irqflags = old_serial_port[i].irqflags;
3283 		port->uartclk  = old_serial_port[i].baud_base * 16;
3284 		port->flags    = old_serial_port[i].flags;
3285 		port->hub6     = old_serial_port[i].hub6;
3286 		port->membase  = old_serial_port[i].iomem_base;
3287 		port->iotype   = old_serial_port[i].io_type;
3288 		port->regshift = old_serial_port[i].iomem_reg_shift;
3289 		serial8250_set_defaults(up);
3290 
3291 		port->irqflags |= irqflag;
3292 		if (serial8250_isa_config != NULL)
3293 			serial8250_isa_config(i, &up->port, &up->capabilities);
3294 	}
3295 }
3296 
3297 static void __init
3298 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
3299 {
3300 	int i;
3301 
3302 	for (i = 0; i < nr_uarts; i++) {
3303 		struct uart_8250_port *up = &serial8250_ports[i];
3304 
3305 		if (up->port.dev)
3306 			continue;
3307 
3308 		up->port.dev = dev;
3309 
3310 		if (skip_txen_test)
3311 			up->port.flags |= UPF_NO_TXEN_TEST;
3312 
3313 		uart_add_one_port(drv, &up->port);
3314 	}
3315 }
3316 
3317 #ifdef CONFIG_SERIAL_8250_CONSOLE
3318 
3319 static void serial8250_console_putchar(struct uart_port *port, int ch)
3320 {
3321 	struct uart_8250_port *up = up_to_u8250p(port);
3322 
3323 	wait_for_xmitr(up, UART_LSR_THRE);
3324 	serial_port_out(port, UART_TX, ch);
3325 }
3326 
3327 /*
3328  *	Print a string to the serial port trying not to disturb
3329  *	any possible real use of the port...
3330  *
3331  *	The console_lock must be held when we get here.
3332  */
3333 static void serial8250_console_write(struct uart_8250_port *up, const char *s,
3334 				     unsigned int count)
3335 {
3336 	struct uart_port *port = &up->port;
3337 	unsigned long flags;
3338 	unsigned int ier;
3339 	int locked = 1;
3340 
3341 	touch_nmi_watchdog();
3342 
3343 	serial8250_rpm_get(up);
3344 
3345 	if (port->sysrq)
3346 		locked = 0;
3347 	else if (oops_in_progress)
3348 		locked = spin_trylock_irqsave(&port->lock, flags);
3349 	else
3350 		spin_lock_irqsave(&port->lock, flags);
3351 
3352 	/*
3353 	 *	First save the IER then disable the interrupts
3354 	 */
3355 	ier = serial_port_in(port, UART_IER);
3356 
3357 	if (up->capabilities & UART_CAP_UUE)
3358 		serial_port_out(port, UART_IER, UART_IER_UUE);
3359 	else
3360 		serial_port_out(port, UART_IER, 0);
3361 
3362 	/* check scratch reg to see if port powered off during system sleep */
3363 	if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) {
3364 		struct ktermios termios;
3365 		unsigned int baud, quot, frac = 0;
3366 
3367 		termios.c_cflag = port->cons->cflag;
3368 		if (port->state->port.tty && termios.c_cflag == 0)
3369 			termios.c_cflag = port->state->port.tty->termios.c_cflag;
3370 
3371 		baud = uart_get_baud_rate(port, &termios, NULL,
3372 					  port->uartclk / 16 / 0xffff,
3373 					  port->uartclk / 16);
3374 		quot = serial8250_get_divisor(up, baud, &frac);
3375 
3376 		serial8250_set_divisor(port, baud, quot, frac);
3377 		serial_port_out(port, UART_LCR, up->lcr);
3378 		serial_port_out(port, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
3379 
3380 		up->canary = 0;
3381 	}
3382 
3383 	uart_console_write(port, s, count, serial8250_console_putchar);
3384 
3385 	/*
3386 	 *	Finally, wait for transmitter to become empty
3387 	 *	and restore the IER
3388 	 */
3389 	wait_for_xmitr(up, BOTH_EMPTY);
3390 	serial_port_out(port, UART_IER, ier);
3391 
3392 	/*
3393 	 *	The receive handling will happen properly because the
3394 	 *	receive ready bit will still be set; it is not cleared
3395 	 *	on read.  However, modem control will not, we must
3396 	 *	call it if we have saved something in the saved flags
3397 	 *	while processing with interrupts off.
3398 	 */
3399 	if (up->msr_saved_flags)
3400 		serial8250_modem_status(up);
3401 
3402 	if (locked)
3403 		spin_unlock_irqrestore(&port->lock, flags);
3404 	serial8250_rpm_put(up);
3405 }
3406 
3407 static void univ8250_console_write(struct console *co, const char *s,
3408 				   unsigned int count)
3409 {
3410 	struct uart_8250_port *up = &serial8250_ports[co->index];
3411 
3412 	serial8250_console_write(up, s, count);
3413 }
3414 
3415 static unsigned int probe_baud(struct uart_port *port)
3416 {
3417 	unsigned char lcr, dll, dlm;
3418 	unsigned int quot;
3419 
3420 	lcr = serial_port_in(port, UART_LCR);
3421 	serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB);
3422 	dll = serial_port_in(port, UART_DLL);
3423 	dlm = serial_port_in(port, UART_DLM);
3424 	serial_port_out(port, UART_LCR, lcr);
3425 
3426 	quot = (dlm << 8) | dll;
3427 	return (port->uartclk / 16) / quot;
3428 }
3429 
3430 static int serial8250_console_setup(struct uart_port *port, char *options, bool probe)
3431 {
3432 	int baud = 9600;
3433 	int bits = 8;
3434 	int parity = 'n';
3435 	int flow = 'n';
3436 
3437 	if (!port->iobase && !port->membase)
3438 		return -ENODEV;
3439 
3440 	if (options)
3441 		uart_parse_options(options, &baud, &parity, &bits, &flow);
3442 	else if (probe)
3443 		baud = probe_baud(port);
3444 
3445 	return uart_set_options(port, port->cons, baud, parity, bits, flow);
3446 }
3447 
3448 static int univ8250_console_setup(struct console *co, char *options)
3449 {
3450 	struct uart_port *port;
3451 
3452 	/*
3453 	 * Check whether an invalid uart number has been specified, and
3454 	 * if so, search for the first available port that does have
3455 	 * console support.
3456 	 */
3457 	if (co->index >= nr_uarts)
3458 		co->index = 0;
3459 	port = &serial8250_ports[co->index].port;
3460 	/* link port to console */
3461 	port->cons = co;
3462 
3463 	return serial8250_console_setup(port, options, false);
3464 }
3465 
3466 /**
3467  *	univ8250_console_match - non-standard console matching
3468  *	@co:	  registering console
3469  *	@name:	  name from console command line
3470  *	@idx:	  index from console command line
3471  *	@options: ptr to option string from console command line
3472  *
3473  *	Only attempts to match console command lines of the form:
3474  *	    console=uart[8250],io|mmio|mmio32,<addr>[,<options>]
3475  *	    console=uart[8250],0x<addr>[,<options>]
3476  *	This form is used to register an initial earlycon boot console and
3477  *	replace it with the serial8250_console at 8250 driver init.
3478  *
3479  *	Performs console setup for a match (as required by interface)
3480  *	If no <options> are specified, then assume the h/w is already setup.
3481  *
3482  *	Returns 0 if console matches; otherwise non-zero to use default matching
3483  */
3484 static int univ8250_console_match(struct console *co, char *name, int idx,
3485 				  char *options)
3486 {
3487 	char match[] = "uart";	/* 8250-specific earlycon name */
3488 	unsigned char iotype;
3489 	unsigned long addr;
3490 	int i;
3491 
3492 	if (strncmp(name, match, 4) != 0)
3493 		return -ENODEV;
3494 
3495 	if (uart_parse_earlycon(options, &iotype, &addr, &options))
3496 		return -ENODEV;
3497 
3498 	/* try to match the port specified on the command line */
3499 	for (i = 0; i < nr_uarts; i++) {
3500 		struct uart_port *port = &serial8250_ports[i].port;
3501 
3502 		if (port->iotype != iotype)
3503 			continue;
3504 		if ((iotype == UPIO_MEM || iotype == UPIO_MEM32) &&
3505 		    (port->mapbase != addr))
3506 			continue;
3507 		if (iotype == UPIO_PORT && port->iobase != addr)
3508 			continue;
3509 
3510 		co->index = i;
3511 		port->cons = co;
3512 		return serial8250_console_setup(port, options, true);
3513 	}
3514 
3515 	return -ENODEV;
3516 }
3517 
3518 static struct console univ8250_console = {
3519 	.name		= "ttyS",
3520 	.write		= univ8250_console_write,
3521 	.device		= uart_console_device,
3522 	.setup		= univ8250_console_setup,
3523 	.match		= univ8250_console_match,
3524 	.flags		= CON_PRINTBUFFER | CON_ANYTIME,
3525 	.index		= -1,
3526 	.data		= &serial8250_reg,
3527 };
3528 
3529 static int __init univ8250_console_init(void)
3530 {
3531 	serial8250_isa_init_ports();
3532 	register_console(&univ8250_console);
3533 	return 0;
3534 }
3535 console_initcall(univ8250_console_init);
3536 
3537 #define SERIAL8250_CONSOLE	&univ8250_console
3538 #else
3539 #define SERIAL8250_CONSOLE	NULL
3540 #endif
3541 
3542 static struct uart_driver serial8250_reg = {
3543 	.owner			= THIS_MODULE,
3544 	.driver_name		= "serial",
3545 	.dev_name		= "ttyS",
3546 	.major			= TTY_MAJOR,
3547 	.minor			= 64,
3548 	.cons			= SERIAL8250_CONSOLE,
3549 };
3550 
3551 /*
3552  * early_serial_setup - early registration for 8250 ports
3553  *
3554  * Setup an 8250 port structure prior to console initialisation.  Use
3555  * after console initialisation will cause undefined behaviour.
3556  */
3557 int __init early_serial_setup(struct uart_port *port)
3558 {
3559 	struct uart_port *p;
3560 
3561 	if (port->line >= ARRAY_SIZE(serial8250_ports))
3562 		return -ENODEV;
3563 
3564 	serial8250_isa_init_ports();
3565 	p = &serial8250_ports[port->line].port;
3566 	p->iobase       = port->iobase;
3567 	p->membase      = port->membase;
3568 	p->irq          = port->irq;
3569 	p->irqflags     = port->irqflags;
3570 	p->uartclk      = port->uartclk;
3571 	p->fifosize     = port->fifosize;
3572 	p->regshift     = port->regshift;
3573 	p->iotype       = port->iotype;
3574 	p->flags        = port->flags;
3575 	p->mapbase      = port->mapbase;
3576 	p->mapsize      = port->mapsize;
3577 	p->private_data = port->private_data;
3578 	p->type		= port->type;
3579 	p->line		= port->line;
3580 
3581 	serial8250_set_defaults(up_to_u8250p(p));
3582 
3583 	if (port->serial_in)
3584 		p->serial_in = port->serial_in;
3585 	if (port->serial_out)
3586 		p->serial_out = port->serial_out;
3587 	if (port->handle_irq)
3588 		p->handle_irq = port->handle_irq;
3589 
3590 	return 0;
3591 }
3592 
3593 /**
3594  *	serial8250_suspend_port - suspend one serial port
3595  *	@line:  serial line number
3596  *
3597  *	Suspend one serial port.
3598  */
3599 void serial8250_suspend_port(int line)
3600 {
3601 	struct uart_8250_port *up = &serial8250_ports[line];
3602 	struct uart_port *port = &up->port;
3603 
3604 	if (!console_suspend_enabled && uart_console(port) &&
3605 	    port->type != PORT_8250) {
3606 		unsigned char canary = 0xa5;
3607 		serial_out(up, UART_SCR, canary);
3608 		if (serial_in(up, UART_SCR) == canary)
3609 			up->canary = canary;
3610 	}
3611 
3612 	uart_suspend_port(&serial8250_reg, port);
3613 }
3614 
3615 /**
3616  *	serial8250_resume_port - resume one serial port
3617  *	@line:  serial line number
3618  *
3619  *	Resume one serial port.
3620  */
3621 void serial8250_resume_port(int line)
3622 {
3623 	struct uart_8250_port *up = &serial8250_ports[line];
3624 	struct uart_port *port = &up->port;
3625 
3626 	up->canary = 0;
3627 
3628 	if (up->capabilities & UART_NATSEMI) {
3629 		/* Ensure it's still in high speed mode */
3630 		serial_port_out(port, UART_LCR, 0xE0);
3631 
3632 		ns16550a_goto_highspeed(up);
3633 
3634 		serial_port_out(port, UART_LCR, 0);
3635 		port->uartclk = 921600*16;
3636 	}
3637 	uart_resume_port(&serial8250_reg, port);
3638 }
3639 
3640 /*
3641  * Register a set of serial devices attached to a platform device.  The
3642  * list is terminated with a zero flags entry, which means we expect
3643  * all entries to have at least UPF_BOOT_AUTOCONF set.
3644  */
3645 static int serial8250_probe(struct platform_device *dev)
3646 {
3647 	struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
3648 	struct uart_8250_port uart;
3649 	int ret, i, irqflag = 0;
3650 
3651 	memset(&uart, 0, sizeof(uart));
3652 
3653 	if (share_irqs)
3654 		irqflag = IRQF_SHARED;
3655 
3656 	for (i = 0; p && p->flags != 0; p++, i++) {
3657 		uart.port.iobase	= p->iobase;
3658 		uart.port.membase	= p->membase;
3659 		uart.port.irq		= p->irq;
3660 		uart.port.irqflags	= p->irqflags;
3661 		uart.port.uartclk	= p->uartclk;
3662 		uart.port.regshift	= p->regshift;
3663 		uart.port.iotype	= p->iotype;
3664 		uart.port.flags		= p->flags;
3665 		uart.port.mapbase	= p->mapbase;
3666 		uart.port.hub6		= p->hub6;
3667 		uart.port.private_data	= p->private_data;
3668 		uart.port.type		= p->type;
3669 		uart.port.serial_in	= p->serial_in;
3670 		uart.port.serial_out	= p->serial_out;
3671 		uart.port.handle_irq	= p->handle_irq;
3672 		uart.port.handle_break	= p->handle_break;
3673 		uart.port.set_termios	= p->set_termios;
3674 		uart.port.pm		= p->pm;
3675 		uart.port.dev		= &dev->dev;
3676 		uart.port.irqflags	|= irqflag;
3677 		ret = serial8250_register_8250_port(&uart);
3678 		if (ret < 0) {
3679 			dev_err(&dev->dev, "unable to register port at index %d "
3680 				"(IO%lx MEM%llx IRQ%d): %d\n", i,
3681 				p->iobase, (unsigned long long)p->mapbase,
3682 				p->irq, ret);
3683 		}
3684 	}
3685 	return 0;
3686 }
3687 
3688 /*
3689  * Remove serial ports registered against a platform device.
3690  */
3691 static int serial8250_remove(struct platform_device *dev)
3692 {
3693 	int i;
3694 
3695 	for (i = 0; i < nr_uarts; i++) {
3696 		struct uart_8250_port *up = &serial8250_ports[i];
3697 
3698 		if (up->port.dev == &dev->dev)
3699 			serial8250_unregister_port(i);
3700 	}
3701 	return 0;
3702 }
3703 
3704 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
3705 {
3706 	int i;
3707 
3708 	for (i = 0; i < UART_NR; i++) {
3709 		struct uart_8250_port *up = &serial8250_ports[i];
3710 
3711 		if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
3712 			uart_suspend_port(&serial8250_reg, &up->port);
3713 	}
3714 
3715 	return 0;
3716 }
3717 
3718 static int serial8250_resume(struct platform_device *dev)
3719 {
3720 	int i;
3721 
3722 	for (i = 0; i < UART_NR; i++) {
3723 		struct uart_8250_port *up = &serial8250_ports[i];
3724 
3725 		if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
3726 			serial8250_resume_port(i);
3727 	}
3728 
3729 	return 0;
3730 }
3731 
3732 static struct platform_driver serial8250_isa_driver = {
3733 	.probe		= serial8250_probe,
3734 	.remove		= serial8250_remove,
3735 	.suspend	= serial8250_suspend,
3736 	.resume		= serial8250_resume,
3737 	.driver		= {
3738 		.name	= "serial8250",
3739 	},
3740 };
3741 
3742 /*
3743  * This "device" covers _all_ ISA 8250-compatible serial devices listed
3744  * in the table in include/asm/serial.h
3745  */
3746 static struct platform_device *serial8250_isa_devs;
3747 
3748 /*
3749  * serial8250_register_8250_port and serial8250_unregister_port allows for
3750  * 16x50 serial ports to be configured at run-time, to support PCMCIA
3751  * modems and PCI multiport cards.
3752  */
3753 static DEFINE_MUTEX(serial_mutex);
3754 
3755 static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
3756 {
3757 	int i;
3758 
3759 	/*
3760 	 * First, find a port entry which matches.
3761 	 */
3762 	for (i = 0; i < nr_uarts; i++)
3763 		if (uart_match_port(&serial8250_ports[i].port, port))
3764 			return &serial8250_ports[i];
3765 
3766 	/* try line number first if still available */
3767 	i = port->line;
3768 	if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
3769 			serial8250_ports[i].port.iobase == 0)
3770 		return &serial8250_ports[i];
3771 	/*
3772 	 * We didn't find a matching entry, so look for the first
3773 	 * free entry.  We look for one which hasn't been previously
3774 	 * used (indicated by zero iobase).
3775 	 */
3776 	for (i = 0; i < nr_uarts; i++)
3777 		if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
3778 		    serial8250_ports[i].port.iobase == 0)
3779 			return &serial8250_ports[i];
3780 
3781 	/*
3782 	 * That also failed.  Last resort is to find any entry which
3783 	 * doesn't have a real port associated with it.
3784 	 */
3785 	for (i = 0; i < nr_uarts; i++)
3786 		if (serial8250_ports[i].port.type == PORT_UNKNOWN)
3787 			return &serial8250_ports[i];
3788 
3789 	return NULL;
3790 }
3791 
3792 /**
3793  *	serial8250_register_8250_port - register a serial port
3794  *	@up: serial port template
3795  *
3796  *	Configure the serial port specified by the request. If the
3797  *	port exists and is in use, it is hung up and unregistered
3798  *	first.
3799  *
3800  *	The port is then probed and if necessary the IRQ is autodetected
3801  *	If this fails an error is returned.
3802  *
3803  *	On success the port is ready to use and the line number is returned.
3804  */
3805 int serial8250_register_8250_port(struct uart_8250_port *up)
3806 {
3807 	struct uart_8250_port *uart;
3808 	int ret = -ENOSPC;
3809 
3810 	if (up->port.uartclk == 0)
3811 		return -EINVAL;
3812 
3813 	mutex_lock(&serial_mutex);
3814 
3815 	uart = serial8250_find_match_or_unused(&up->port);
3816 	if (uart && uart->port.type != PORT_8250_CIR) {
3817 		if (uart->port.dev)
3818 			uart_remove_one_port(&serial8250_reg, &uart->port);
3819 
3820 		uart->port.iobase       = up->port.iobase;
3821 		uart->port.membase      = up->port.membase;
3822 		uart->port.irq          = up->port.irq;
3823 		uart->port.irqflags     = up->port.irqflags;
3824 		uart->port.uartclk      = up->port.uartclk;
3825 		uart->port.fifosize     = up->port.fifosize;
3826 		uart->port.regshift     = up->port.regshift;
3827 		uart->port.iotype       = up->port.iotype;
3828 		uart->port.flags        = up->port.flags | UPF_BOOT_AUTOCONF;
3829 		uart->bugs		= up->bugs;
3830 		uart->port.mapbase      = up->port.mapbase;
3831 		uart->port.mapsize      = up->port.mapsize;
3832 		uart->port.private_data = up->port.private_data;
3833 		uart->port.fifosize	= up->port.fifosize;
3834 		uart->tx_loadsz		= up->tx_loadsz;
3835 		uart->capabilities	= up->capabilities;
3836 		uart->port.throttle	= up->port.throttle;
3837 		uart->port.unthrottle	= up->port.unthrottle;
3838 		uart->port.rs485_config	= up->port.rs485_config;
3839 		uart->port.rs485	= up->port.rs485;
3840 		uart->dma		= up->dma;
3841 
3842 		/* Take tx_loadsz from fifosize if it wasn't set separately */
3843 		if (uart->port.fifosize && !uart->tx_loadsz)
3844 			uart->tx_loadsz = uart->port.fifosize;
3845 
3846 		if (up->port.dev)
3847 			uart->port.dev = up->port.dev;
3848 
3849 		if (skip_txen_test)
3850 			uart->port.flags |= UPF_NO_TXEN_TEST;
3851 
3852 		if (up->port.flags & UPF_FIXED_TYPE)
3853 			uart->port.type = up->port.type;
3854 
3855 		serial8250_set_defaults(uart);
3856 
3857 		/* Possibly override default I/O functions.  */
3858 		if (up->port.serial_in)
3859 			uart->port.serial_in = up->port.serial_in;
3860 		if (up->port.serial_out)
3861 			uart->port.serial_out = up->port.serial_out;
3862 		if (up->port.handle_irq)
3863 			uart->port.handle_irq = up->port.handle_irq;
3864 		/*  Possibly override set_termios call */
3865 		if (up->port.set_termios)
3866 			uart->port.set_termios = up->port.set_termios;
3867 		if (up->port.set_mctrl)
3868 			uart->port.set_mctrl = up->port.set_mctrl;
3869 		if (up->port.startup)
3870 			uart->port.startup = up->port.startup;
3871 		if (up->port.shutdown)
3872 			uart->port.shutdown = up->port.shutdown;
3873 		if (up->port.pm)
3874 			uart->port.pm = up->port.pm;
3875 		if (up->port.handle_break)
3876 			uart->port.handle_break = up->port.handle_break;
3877 		if (up->dl_read)
3878 			uart->dl_read = up->dl_read;
3879 		if (up->dl_write)
3880 			uart->dl_write = up->dl_write;
3881 
3882 		if (serial8250_isa_config != NULL)
3883 			serial8250_isa_config(0, &uart->port,
3884 					&uart->capabilities);
3885 
3886 		ret = uart_add_one_port(&serial8250_reg, &uart->port);
3887 		if (ret == 0)
3888 			ret = uart->port.line;
3889 	}
3890 	mutex_unlock(&serial_mutex);
3891 
3892 	return ret;
3893 }
3894 EXPORT_SYMBOL(serial8250_register_8250_port);
3895 
3896 /**
3897  *	serial8250_unregister_port - remove a 16x50 serial port at runtime
3898  *	@line: serial line number
3899  *
3900  *	Remove one serial port.  This may not be called from interrupt
3901  *	context.  We hand the port back to the our control.
3902  */
3903 void serial8250_unregister_port(int line)
3904 {
3905 	struct uart_8250_port *uart = &serial8250_ports[line];
3906 
3907 	mutex_lock(&serial_mutex);
3908 	uart_remove_one_port(&serial8250_reg, &uart->port);
3909 	if (serial8250_isa_devs) {
3910 		uart->port.flags &= ~UPF_BOOT_AUTOCONF;
3911 		if (skip_txen_test)
3912 			uart->port.flags |= UPF_NO_TXEN_TEST;
3913 		uart->port.type = PORT_UNKNOWN;
3914 		uart->port.dev = &serial8250_isa_devs->dev;
3915 		uart->capabilities = 0;
3916 		uart_add_one_port(&serial8250_reg, &uart->port);
3917 	} else {
3918 		uart->port.dev = NULL;
3919 	}
3920 	mutex_unlock(&serial_mutex);
3921 }
3922 EXPORT_SYMBOL(serial8250_unregister_port);
3923 
3924 static int __init serial8250_init(void)
3925 {
3926 	int ret;
3927 
3928 	serial8250_isa_init_ports();
3929 
3930 	printk(KERN_INFO "Serial: 8250/16550 driver, "
3931 		"%d ports, IRQ sharing %sabled\n", nr_uarts,
3932 		share_irqs ? "en" : "dis");
3933 
3934 #ifdef CONFIG_SPARC
3935 	ret = sunserial_register_minors(&serial8250_reg, UART_NR);
3936 #else
3937 	serial8250_reg.nr = UART_NR;
3938 	ret = uart_register_driver(&serial8250_reg);
3939 #endif
3940 	if (ret)
3941 		goto out;
3942 
3943 	ret = serial8250_pnp_init();
3944 	if (ret)
3945 		goto unreg_uart_drv;
3946 
3947 	serial8250_isa_devs = platform_device_alloc("serial8250",
3948 						    PLAT8250_DEV_LEGACY);
3949 	if (!serial8250_isa_devs) {
3950 		ret = -ENOMEM;
3951 		goto unreg_pnp;
3952 	}
3953 
3954 	ret = platform_device_add(serial8250_isa_devs);
3955 	if (ret)
3956 		goto put_dev;
3957 
3958 	serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
3959 
3960 	ret = platform_driver_register(&serial8250_isa_driver);
3961 	if (ret == 0)
3962 		goto out;
3963 
3964 	platform_device_del(serial8250_isa_devs);
3965 put_dev:
3966 	platform_device_put(serial8250_isa_devs);
3967 unreg_pnp:
3968 	serial8250_pnp_exit();
3969 unreg_uart_drv:
3970 #ifdef CONFIG_SPARC
3971 	sunserial_unregister_minors(&serial8250_reg, UART_NR);
3972 #else
3973 	uart_unregister_driver(&serial8250_reg);
3974 #endif
3975 out:
3976 	return ret;
3977 }
3978 
3979 static void __exit serial8250_exit(void)
3980 {
3981 	struct platform_device *isa_dev = serial8250_isa_devs;
3982 
3983 	/*
3984 	 * This tells serial8250_unregister_port() not to re-register
3985 	 * the ports (thereby making serial8250_isa_driver permanently
3986 	 * in use.)
3987 	 */
3988 	serial8250_isa_devs = NULL;
3989 
3990 	platform_driver_unregister(&serial8250_isa_driver);
3991 	platform_device_unregister(isa_dev);
3992 
3993 	serial8250_pnp_exit();
3994 
3995 #ifdef CONFIG_SPARC
3996 	sunserial_unregister_minors(&serial8250_reg, UART_NR);
3997 #else
3998 	uart_unregister_driver(&serial8250_reg);
3999 #endif
4000 }
4001 
4002 module_init(serial8250_init);
4003 module_exit(serial8250_exit);
4004 
4005 EXPORT_SYMBOL(serial8250_suspend_port);
4006 EXPORT_SYMBOL(serial8250_resume_port);
4007 
4008 MODULE_LICENSE("GPL");
4009 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
4010 
4011 module_param(share_irqs, uint, 0644);
4012 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
4013 	" (unsafe)");
4014 
4015 module_param(nr_uarts, uint, 0644);
4016 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
4017 
4018 module_param(skip_txen_test, uint, 0644);
4019 MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
4020 
4021 #ifdef CONFIG_SERIAL_8250_RSA
4022 module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
4023 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
4024 #endif
4025 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
4026 
4027 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
4028 #ifndef MODULE
4029 /* This module was renamed to 8250_core in 3.7.  Keep the old "8250" name
4030  * working as well for the module options so we don't break people.  We
4031  * need to keep the names identical and the convenient macros will happily
4032  * refuse to let us do that by failing the build with redefinition errors
4033  * of global variables.  So we stick them inside a dummy function to avoid
4034  * those conflicts.  The options still get parsed, and the redefined
4035  * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
4036  *
4037  * This is hacky.  I'm sorry.
4038  */
4039 static void __used s8250_options(void)
4040 {
4041 #undef MODULE_PARAM_PREFIX
4042 #define MODULE_PARAM_PREFIX "8250_core."
4043 
4044 	module_param_cb(share_irqs, &param_ops_uint, &share_irqs, 0644);
4045 	module_param_cb(nr_uarts, &param_ops_uint, &nr_uarts, 0644);
4046 	module_param_cb(skip_txen_test, &param_ops_uint, &skip_txen_test, 0644);
4047 #ifdef CONFIG_SERIAL_8250_RSA
4048 	__module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
4049 		&param_array_ops, .arr = &__param_arr_probe_rsa,
4050 		0444, -1, 0);
4051 #endif
4052 }
4053 #else
4054 MODULE_ALIAS("8250_core");
4055 #endif
4056 #endif
4057