1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Base port operations for 8250/16550-type serial ports
4  *
5  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6  *  Split from 8250_core.c, Copyright (C) 2001 Russell King.
7  *
8  * A note about mapbase / membase
9  *
10  *  mapbase is the physical address of the IO port.
11  *  membase is an 'ioremapped' cookie.
12  */
13 
14 #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
15 #define SUPPORT_SYSRQ
16 #endif
17 
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/ioport.h>
21 #include <linux/init.h>
22 #include <linux/console.h>
23 #include <linux/sysrq.h>
24 #include <linux/delay.h>
25 #include <linux/platform_device.h>
26 #include <linux/tty.h>
27 #include <linux/ratelimit.h>
28 #include <linux/tty_flip.h>
29 #include <linux/serial.h>
30 #include <linux/serial_8250.h>
31 #include <linux/nmi.h>
32 #include <linux/mutex.h>
33 #include <linux/slab.h>
34 #include <linux/uaccess.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/ktime.h>
37 
38 #include <asm/io.h>
39 #include <asm/irq.h>
40 
41 #include "8250.h"
42 
43 /*
44  * These are definitions for the Exar XR17V35X and XR17(C|D)15X
45  */
46 #define UART_EXAR_INT0		0x80
47 #define UART_EXAR_SLEEP		0x8b	/* Sleep mode */
48 #define UART_EXAR_DVID		0x8d	/* Device identification */
49 
50 /*
51  * Debugging.
52  */
53 #if 0
54 #define DEBUG_AUTOCONF(fmt...)	printk(fmt)
55 #else
56 #define DEBUG_AUTOCONF(fmt...)	do { } while (0)
57 #endif
58 
59 #define BOTH_EMPTY	(UART_LSR_TEMT | UART_LSR_THRE)
60 
61 /*
62  * Here we define the default xmit fifo size used for each type of UART.
63  */
64 static const struct serial8250_config uart_config[] = {
65 	[PORT_UNKNOWN] = {
66 		.name		= "unknown",
67 		.fifo_size	= 1,
68 		.tx_loadsz	= 1,
69 	},
70 	[PORT_8250] = {
71 		.name		= "8250",
72 		.fifo_size	= 1,
73 		.tx_loadsz	= 1,
74 	},
75 	[PORT_16450] = {
76 		.name		= "16450",
77 		.fifo_size	= 1,
78 		.tx_loadsz	= 1,
79 	},
80 	[PORT_16550] = {
81 		.name		= "16550",
82 		.fifo_size	= 1,
83 		.tx_loadsz	= 1,
84 	},
85 	[PORT_16550A] = {
86 		.name		= "16550A",
87 		.fifo_size	= 16,
88 		.tx_loadsz	= 16,
89 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
90 				  UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
91 		.rxtrig_bytes	= {1, 4, 8, 14},
92 		.flags		= UART_CAP_FIFO,
93 	},
94 	[PORT_CIRRUS] = {
95 		.name		= "Cirrus",
96 		.fifo_size	= 1,
97 		.tx_loadsz	= 1,
98 	},
99 	[PORT_16650] = {
100 		.name		= "ST16650",
101 		.fifo_size	= 1,
102 		.tx_loadsz	= 1,
103 		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
104 	},
105 	[PORT_16650V2] = {
106 		.name		= "ST16650V2",
107 		.fifo_size	= 32,
108 		.tx_loadsz	= 16,
109 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
110 				  UART_FCR_T_TRIG_00,
111 		.rxtrig_bytes	= {8, 16, 24, 28},
112 		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
113 	},
114 	[PORT_16750] = {
115 		.name		= "TI16750",
116 		.fifo_size	= 64,
117 		.tx_loadsz	= 64,
118 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
119 				  UART_FCR7_64BYTE,
120 		.rxtrig_bytes	= {1, 16, 32, 56},
121 		.flags		= UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
122 	},
123 	[PORT_STARTECH] = {
124 		.name		= "Startech",
125 		.fifo_size	= 1,
126 		.tx_loadsz	= 1,
127 	},
128 	[PORT_16C950] = {
129 		.name		= "16C950/954",
130 		.fifo_size	= 128,
131 		.tx_loadsz	= 128,
132 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
133 		/* UART_CAP_EFR breaks billionon CF bluetooth card. */
134 		.flags		= UART_CAP_FIFO | UART_CAP_SLEEP,
135 	},
136 	[PORT_16654] = {
137 		.name		= "ST16654",
138 		.fifo_size	= 64,
139 		.tx_loadsz	= 32,
140 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
141 				  UART_FCR_T_TRIG_10,
142 		.rxtrig_bytes	= {8, 16, 56, 60},
143 		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
144 	},
145 	[PORT_16850] = {
146 		.name		= "XR16850",
147 		.fifo_size	= 128,
148 		.tx_loadsz	= 128,
149 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
150 		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
151 	},
152 	[PORT_RSA] = {
153 		.name		= "RSA",
154 		.fifo_size	= 2048,
155 		.tx_loadsz	= 2048,
156 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
157 		.flags		= UART_CAP_FIFO,
158 	},
159 	[PORT_NS16550A] = {
160 		.name		= "NS16550A",
161 		.fifo_size	= 16,
162 		.tx_loadsz	= 16,
163 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
164 		.flags		= UART_CAP_FIFO | UART_NATSEMI,
165 	},
166 	[PORT_XSCALE] = {
167 		.name		= "XScale",
168 		.fifo_size	= 32,
169 		.tx_loadsz	= 32,
170 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
171 		.flags		= UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
172 	},
173 	[PORT_OCTEON] = {
174 		.name		= "OCTEON",
175 		.fifo_size	= 64,
176 		.tx_loadsz	= 64,
177 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
178 		.flags		= UART_CAP_FIFO,
179 	},
180 	[PORT_AR7] = {
181 		.name		= "AR7",
182 		.fifo_size	= 16,
183 		.tx_loadsz	= 16,
184 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
185 		.flags		= UART_CAP_FIFO /* | UART_CAP_AFE */,
186 	},
187 	[PORT_U6_16550A] = {
188 		.name		= "U6_16550A",
189 		.fifo_size	= 64,
190 		.tx_loadsz	= 64,
191 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
192 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
193 	},
194 	[PORT_TEGRA] = {
195 		.name		= "Tegra",
196 		.fifo_size	= 32,
197 		.tx_loadsz	= 8,
198 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
199 				  UART_FCR_T_TRIG_01,
200 		.rxtrig_bytes	= {1, 4, 8, 14},
201 		.flags		= UART_CAP_FIFO | UART_CAP_RTOIE,
202 	},
203 	[PORT_XR17D15X] = {
204 		.name		= "XR17D15X",
205 		.fifo_size	= 64,
206 		.tx_loadsz	= 64,
207 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
208 		.flags		= UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
209 				  UART_CAP_SLEEP,
210 	},
211 	[PORT_XR17V35X] = {
212 		.name		= "XR17V35X",
213 		.fifo_size	= 256,
214 		.tx_loadsz	= 256,
215 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11 |
216 				  UART_FCR_T_TRIG_11,
217 		.flags		= UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
218 				  UART_CAP_SLEEP,
219 	},
220 	[PORT_LPC3220] = {
221 		.name		= "LPC3220",
222 		.fifo_size	= 64,
223 		.tx_loadsz	= 32,
224 		.fcr		= UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
225 				  UART_FCR_R_TRIG_00 | UART_FCR_T_TRIG_00,
226 		.flags		= UART_CAP_FIFO,
227 	},
228 	[PORT_BRCM_TRUMANAGE] = {
229 		.name		= "TruManage",
230 		.fifo_size	= 1,
231 		.tx_loadsz	= 1024,
232 		.flags		= UART_CAP_HFIFO,
233 	},
234 	[PORT_8250_CIR] = {
235 		.name		= "CIR port"
236 	},
237 	[PORT_ALTR_16550_F32] = {
238 		.name		= "Altera 16550 FIFO32",
239 		.fifo_size	= 32,
240 		.tx_loadsz	= 32,
241 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
242 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
243 	},
244 	[PORT_ALTR_16550_F64] = {
245 		.name		= "Altera 16550 FIFO64",
246 		.fifo_size	= 64,
247 		.tx_loadsz	= 64,
248 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
249 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
250 	},
251 	[PORT_ALTR_16550_F128] = {
252 		.name		= "Altera 16550 FIFO128",
253 		.fifo_size	= 128,
254 		.tx_loadsz	= 128,
255 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
256 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
257 	},
258 	/*
259 	 * tx_loadsz is set to 63-bytes instead of 64-bytes to implement
260 	 * workaround of errata A-008006 which states that tx_loadsz should
261 	 * be configured less than Maximum supported fifo bytes.
262 	 */
263 	[PORT_16550A_FSL64] = {
264 		.name		= "16550A_FSL64",
265 		.fifo_size	= 64,
266 		.tx_loadsz	= 63,
267 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
268 				  UART_FCR7_64BYTE,
269 		.flags		= UART_CAP_FIFO,
270 	},
271 	[PORT_RT2880] = {
272 		.name		= "Palmchip BK-3103",
273 		.fifo_size	= 16,
274 		.tx_loadsz	= 16,
275 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
276 		.rxtrig_bytes	= {1, 4, 8, 14},
277 		.flags		= UART_CAP_FIFO,
278 	},
279 	[PORT_DA830] = {
280 		.name		= "TI DA8xx/66AK2x",
281 		.fifo_size	= 16,
282 		.tx_loadsz	= 16,
283 		.fcr		= UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
284 				  UART_FCR_R_TRIG_10,
285 		.rxtrig_bytes	= {1, 4, 8, 14},
286 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
287 	},
288 	[PORT_MTK_BTIF] = {
289 		.name		= "MediaTek BTIF",
290 		.fifo_size	= 16,
291 		.tx_loadsz	= 16,
292 		.fcr		= UART_FCR_ENABLE_FIFO |
293 				  UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
294 		.flags		= UART_CAP_FIFO,
295 	},
296 };
297 
298 /* Uart divisor latch read */
299 static int default_serial_dl_read(struct uart_8250_port *up)
300 {
301 	return serial_in(up, UART_DLL) | serial_in(up, UART_DLM) << 8;
302 }
303 
304 /* Uart divisor latch write */
305 static void default_serial_dl_write(struct uart_8250_port *up, int value)
306 {
307 	serial_out(up, UART_DLL, value & 0xff);
308 	serial_out(up, UART_DLM, value >> 8 & 0xff);
309 }
310 
311 #ifdef CONFIG_SERIAL_8250_RT288X
312 
313 /* Au1x00/RT288x UART hardware has a weird register layout */
314 static const s8 au_io_in_map[8] = {
315 	 0,	/* UART_RX  */
316 	 2,	/* UART_IER */
317 	 3,	/* UART_IIR */
318 	 5,	/* UART_LCR */
319 	 6,	/* UART_MCR */
320 	 7,	/* UART_LSR */
321 	 8,	/* UART_MSR */
322 	-1,	/* UART_SCR (unmapped) */
323 };
324 
325 static const s8 au_io_out_map[8] = {
326 	 1,	/* UART_TX  */
327 	 2,	/* UART_IER */
328 	 4,	/* UART_FCR */
329 	 5,	/* UART_LCR */
330 	 6,	/* UART_MCR */
331 	-1,	/* UART_LSR (unmapped) */
332 	-1,	/* UART_MSR (unmapped) */
333 	-1,	/* UART_SCR (unmapped) */
334 };
335 
336 unsigned int au_serial_in(struct uart_port *p, int offset)
337 {
338 	if (offset >= ARRAY_SIZE(au_io_in_map))
339 		return UINT_MAX;
340 	offset = au_io_in_map[offset];
341 	if (offset < 0)
342 		return UINT_MAX;
343 	return __raw_readl(p->membase + (offset << p->regshift));
344 }
345 
346 void au_serial_out(struct uart_port *p, int offset, int value)
347 {
348 	if (offset >= ARRAY_SIZE(au_io_out_map))
349 		return;
350 	offset = au_io_out_map[offset];
351 	if (offset < 0)
352 		return;
353 	__raw_writel(value, p->membase + (offset << p->regshift));
354 }
355 
356 /* Au1x00 haven't got a standard divisor latch */
357 static int au_serial_dl_read(struct uart_8250_port *up)
358 {
359 	return __raw_readl(up->port.membase + 0x28);
360 }
361 
362 static void au_serial_dl_write(struct uart_8250_port *up, int value)
363 {
364 	__raw_writel(value, up->port.membase + 0x28);
365 }
366 
367 #endif
368 
369 static unsigned int hub6_serial_in(struct uart_port *p, int offset)
370 {
371 	offset = offset << p->regshift;
372 	outb(p->hub6 - 1 + offset, p->iobase);
373 	return inb(p->iobase + 1);
374 }
375 
376 static void hub6_serial_out(struct uart_port *p, int offset, int value)
377 {
378 	offset = offset << p->regshift;
379 	outb(p->hub6 - 1 + offset, p->iobase);
380 	outb(value, p->iobase + 1);
381 }
382 
383 static unsigned int mem_serial_in(struct uart_port *p, int offset)
384 {
385 	offset = offset << p->regshift;
386 	return readb(p->membase + offset);
387 }
388 
389 static void mem_serial_out(struct uart_port *p, int offset, int value)
390 {
391 	offset = offset << p->regshift;
392 	writeb(value, p->membase + offset);
393 }
394 
395 static void mem16_serial_out(struct uart_port *p, int offset, int value)
396 {
397 	offset = offset << p->regshift;
398 	writew(value, p->membase + offset);
399 }
400 
401 static unsigned int mem16_serial_in(struct uart_port *p, int offset)
402 {
403 	offset = offset << p->regshift;
404 	return readw(p->membase + offset);
405 }
406 
407 static void mem32_serial_out(struct uart_port *p, int offset, int value)
408 {
409 	offset = offset << p->regshift;
410 	writel(value, p->membase + offset);
411 }
412 
413 static unsigned int mem32_serial_in(struct uart_port *p, int offset)
414 {
415 	offset = offset << p->regshift;
416 	return readl(p->membase + offset);
417 }
418 
419 static void mem32be_serial_out(struct uart_port *p, int offset, int value)
420 {
421 	offset = offset << p->regshift;
422 	iowrite32be(value, p->membase + offset);
423 }
424 
425 static unsigned int mem32be_serial_in(struct uart_port *p, int offset)
426 {
427 	offset = offset << p->regshift;
428 	return ioread32be(p->membase + offset);
429 }
430 
431 static unsigned int io_serial_in(struct uart_port *p, int offset)
432 {
433 	offset = offset << p->regshift;
434 	return inb(p->iobase + offset);
435 }
436 
437 static void io_serial_out(struct uart_port *p, int offset, int value)
438 {
439 	offset = offset << p->regshift;
440 	outb(value, p->iobase + offset);
441 }
442 
443 static int serial8250_default_handle_irq(struct uart_port *port);
444 static int exar_handle_irq(struct uart_port *port);
445 
446 static void set_io_from_upio(struct uart_port *p)
447 {
448 	struct uart_8250_port *up = up_to_u8250p(p);
449 
450 	up->dl_read = default_serial_dl_read;
451 	up->dl_write = default_serial_dl_write;
452 
453 	switch (p->iotype) {
454 	case UPIO_HUB6:
455 		p->serial_in = hub6_serial_in;
456 		p->serial_out = hub6_serial_out;
457 		break;
458 
459 	case UPIO_MEM:
460 		p->serial_in = mem_serial_in;
461 		p->serial_out = mem_serial_out;
462 		break;
463 
464 	case UPIO_MEM16:
465 		p->serial_in = mem16_serial_in;
466 		p->serial_out = mem16_serial_out;
467 		break;
468 
469 	case UPIO_MEM32:
470 		p->serial_in = mem32_serial_in;
471 		p->serial_out = mem32_serial_out;
472 		break;
473 
474 	case UPIO_MEM32BE:
475 		p->serial_in = mem32be_serial_in;
476 		p->serial_out = mem32be_serial_out;
477 		break;
478 
479 #ifdef CONFIG_SERIAL_8250_RT288X
480 	case UPIO_AU:
481 		p->serial_in = au_serial_in;
482 		p->serial_out = au_serial_out;
483 		up->dl_read = au_serial_dl_read;
484 		up->dl_write = au_serial_dl_write;
485 		break;
486 #endif
487 
488 	default:
489 		p->serial_in = io_serial_in;
490 		p->serial_out = io_serial_out;
491 		break;
492 	}
493 	/* Remember loaded iotype */
494 	up->cur_iotype = p->iotype;
495 	p->handle_irq = serial8250_default_handle_irq;
496 }
497 
498 static void
499 serial_port_out_sync(struct uart_port *p, int offset, int value)
500 {
501 	switch (p->iotype) {
502 	case UPIO_MEM:
503 	case UPIO_MEM16:
504 	case UPIO_MEM32:
505 	case UPIO_MEM32BE:
506 	case UPIO_AU:
507 		p->serial_out(p, offset, value);
508 		p->serial_in(p, UART_LCR);	/* safe, no side-effects */
509 		break;
510 	default:
511 		p->serial_out(p, offset, value);
512 	}
513 }
514 
515 /*
516  * For the 16C950
517  */
518 static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
519 {
520 	serial_out(up, UART_SCR, offset);
521 	serial_out(up, UART_ICR, value);
522 }
523 
524 static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
525 {
526 	unsigned int value;
527 
528 	serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
529 	serial_out(up, UART_SCR, offset);
530 	value = serial_in(up, UART_ICR);
531 	serial_icr_write(up, UART_ACR, up->acr);
532 
533 	return value;
534 }
535 
536 /*
537  * FIFO support.
538  */
539 static void serial8250_clear_fifos(struct uart_8250_port *p)
540 {
541 	if (p->capabilities & UART_CAP_FIFO) {
542 		serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
543 		serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
544 			       UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
545 		serial_out(p, UART_FCR, 0);
546 	}
547 }
548 
549 static inline void serial8250_em485_rts_after_send(struct uart_8250_port *p)
550 {
551 	unsigned char mcr = serial8250_in_MCR(p);
552 
553 	if (p->port.rs485.flags & SER_RS485_RTS_AFTER_SEND)
554 		mcr |= UART_MCR_RTS;
555 	else
556 		mcr &= ~UART_MCR_RTS;
557 	serial8250_out_MCR(p, mcr);
558 }
559 
560 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t);
561 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t);
562 
563 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
564 {
565 	serial8250_clear_fifos(p);
566 	serial_out(p, UART_FCR, p->fcr);
567 }
568 EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);
569 
570 void serial8250_rpm_get(struct uart_8250_port *p)
571 {
572 	if (!(p->capabilities & UART_CAP_RPM))
573 		return;
574 	pm_runtime_get_sync(p->port.dev);
575 }
576 EXPORT_SYMBOL_GPL(serial8250_rpm_get);
577 
578 void serial8250_rpm_put(struct uart_8250_port *p)
579 {
580 	if (!(p->capabilities & UART_CAP_RPM))
581 		return;
582 	pm_runtime_mark_last_busy(p->port.dev);
583 	pm_runtime_put_autosuspend(p->port.dev);
584 }
585 EXPORT_SYMBOL_GPL(serial8250_rpm_put);
586 
587 /**
588  *	serial8250_em485_init() - put uart_8250_port into rs485 emulating
589  *	@p:	uart_8250_port port instance
590  *
591  *	The function is used to start rs485 software emulating on the
592  *	&struct uart_8250_port* @p. Namely, RTS is switched before/after
593  *	transmission. The function is idempotent, so it is safe to call it
594  *	multiple times.
595  *
596  *	The caller MUST enable interrupt on empty shift register before
597  *	calling serial8250_em485_init(). This interrupt is not a part of
598  *	8250 standard, but implementation defined.
599  *
600  *	The function is supposed to be called from .rs485_config callback
601  *	or from any other callback protected with p->port.lock spinlock.
602  *
603  *	See also serial8250_em485_destroy()
604  *
605  *	Return 0 - success, -errno - otherwise
606  */
607 int serial8250_em485_init(struct uart_8250_port *p)
608 {
609 	if (p->em485)
610 		return 0;
611 
612 	p->em485 = kmalloc(sizeof(struct uart_8250_em485), GFP_ATOMIC);
613 	if (!p->em485)
614 		return -ENOMEM;
615 
616 	hrtimer_init(&p->em485->stop_tx_timer, CLOCK_MONOTONIC,
617 		     HRTIMER_MODE_REL);
618 	hrtimer_init(&p->em485->start_tx_timer, CLOCK_MONOTONIC,
619 		     HRTIMER_MODE_REL);
620 	p->em485->stop_tx_timer.function = &serial8250_em485_handle_stop_tx;
621 	p->em485->start_tx_timer.function = &serial8250_em485_handle_start_tx;
622 	p->em485->port = p;
623 	p->em485->active_timer = NULL;
624 	serial8250_em485_rts_after_send(p);
625 
626 	return 0;
627 }
628 EXPORT_SYMBOL_GPL(serial8250_em485_init);
629 
630 /**
631  *	serial8250_em485_destroy() - put uart_8250_port into normal state
632  *	@p:	uart_8250_port port instance
633  *
634  *	The function is used to stop rs485 software emulating on the
635  *	&struct uart_8250_port* @p. The function is idempotent, so it is safe to
636  *	call it multiple times.
637  *
638  *	The function is supposed to be called from .rs485_config callback
639  *	or from any other callback protected with p->port.lock spinlock.
640  *
641  *	See also serial8250_em485_init()
642  */
643 void serial8250_em485_destroy(struct uart_8250_port *p)
644 {
645 	if (!p->em485)
646 		return;
647 
648 	hrtimer_cancel(&p->em485->start_tx_timer);
649 	hrtimer_cancel(&p->em485->stop_tx_timer);
650 
651 	kfree(p->em485);
652 	p->em485 = NULL;
653 }
654 EXPORT_SYMBOL_GPL(serial8250_em485_destroy);
655 
656 /*
657  * These two wrappers ensure that enable_runtime_pm_tx() can be called more than
658  * once and disable_runtime_pm_tx() will still disable RPM because the fifo is
659  * empty and the HW can idle again.
660  */
661 void serial8250_rpm_get_tx(struct uart_8250_port *p)
662 {
663 	unsigned char rpm_active;
664 
665 	if (!(p->capabilities & UART_CAP_RPM))
666 		return;
667 
668 	rpm_active = xchg(&p->rpm_tx_active, 1);
669 	if (rpm_active)
670 		return;
671 	pm_runtime_get_sync(p->port.dev);
672 }
673 EXPORT_SYMBOL_GPL(serial8250_rpm_get_tx);
674 
675 void serial8250_rpm_put_tx(struct uart_8250_port *p)
676 {
677 	unsigned char rpm_active;
678 
679 	if (!(p->capabilities & UART_CAP_RPM))
680 		return;
681 
682 	rpm_active = xchg(&p->rpm_tx_active, 0);
683 	if (!rpm_active)
684 		return;
685 	pm_runtime_mark_last_busy(p->port.dev);
686 	pm_runtime_put_autosuspend(p->port.dev);
687 }
688 EXPORT_SYMBOL_GPL(serial8250_rpm_put_tx);
689 
690 /*
691  * IER sleep support.  UARTs which have EFRs need the "extended
692  * capability" bit enabled.  Note that on XR16C850s, we need to
693  * reset LCR to write to IER.
694  */
695 static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
696 {
697 	unsigned char lcr = 0, efr = 0;
698 	/*
699 	 * Exar UARTs have a SLEEP register that enables or disables
700 	 * each UART to enter sleep mode separately.  On the XR17V35x the
701 	 * register is accessible to each UART at the UART_EXAR_SLEEP
702 	 * offset but the UART channel may only write to the corresponding
703 	 * bit.
704 	 */
705 	serial8250_rpm_get(p);
706 	if ((p->port.type == PORT_XR17V35X) ||
707 	   (p->port.type == PORT_XR17D15X)) {
708 		serial_out(p, UART_EXAR_SLEEP, sleep ? 0xff : 0);
709 		goto out;
710 	}
711 
712 	if (p->capabilities & UART_CAP_SLEEP) {
713 		if (p->capabilities & UART_CAP_EFR) {
714 			lcr = serial_in(p, UART_LCR);
715 			efr = serial_in(p, UART_EFR);
716 			serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
717 			serial_out(p, UART_EFR, UART_EFR_ECB);
718 			serial_out(p, UART_LCR, 0);
719 		}
720 		serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
721 		if (p->capabilities & UART_CAP_EFR) {
722 			serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
723 			serial_out(p, UART_EFR, efr);
724 			serial_out(p, UART_LCR, lcr);
725 		}
726 	}
727 out:
728 	serial8250_rpm_put(p);
729 }
730 
731 #ifdef CONFIG_SERIAL_8250_RSA
732 /*
733  * Attempts to turn on the RSA FIFO.  Returns zero on failure.
734  * We set the port uart clock rate if we succeed.
735  */
736 static int __enable_rsa(struct uart_8250_port *up)
737 {
738 	unsigned char mode;
739 	int result;
740 
741 	mode = serial_in(up, UART_RSA_MSR);
742 	result = mode & UART_RSA_MSR_FIFO;
743 
744 	if (!result) {
745 		serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
746 		mode = serial_in(up, UART_RSA_MSR);
747 		result = mode & UART_RSA_MSR_FIFO;
748 	}
749 
750 	if (result)
751 		up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
752 
753 	return result;
754 }
755 
756 static void enable_rsa(struct uart_8250_port *up)
757 {
758 	if (up->port.type == PORT_RSA) {
759 		if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
760 			spin_lock_irq(&up->port.lock);
761 			__enable_rsa(up);
762 			spin_unlock_irq(&up->port.lock);
763 		}
764 		if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
765 			serial_out(up, UART_RSA_FRR, 0);
766 	}
767 }
768 
769 /*
770  * Attempts to turn off the RSA FIFO.  Returns zero on failure.
771  * It is unknown why interrupts were disabled in here.  However,
772  * the caller is expected to preserve this behaviour by grabbing
773  * the spinlock before calling this function.
774  */
775 static void disable_rsa(struct uart_8250_port *up)
776 {
777 	unsigned char mode;
778 	int result;
779 
780 	if (up->port.type == PORT_RSA &&
781 	    up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
782 		spin_lock_irq(&up->port.lock);
783 
784 		mode = serial_in(up, UART_RSA_MSR);
785 		result = !(mode & UART_RSA_MSR_FIFO);
786 
787 		if (!result) {
788 			serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
789 			mode = serial_in(up, UART_RSA_MSR);
790 			result = !(mode & UART_RSA_MSR_FIFO);
791 		}
792 
793 		if (result)
794 			up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
795 		spin_unlock_irq(&up->port.lock);
796 	}
797 }
798 #endif /* CONFIG_SERIAL_8250_RSA */
799 
800 /*
801  * This is a quickie test to see how big the FIFO is.
802  * It doesn't work at all the time, more's the pity.
803  */
804 static int size_fifo(struct uart_8250_port *up)
805 {
806 	unsigned char old_fcr, old_mcr, old_lcr;
807 	unsigned short old_dl;
808 	int count;
809 
810 	old_lcr = serial_in(up, UART_LCR);
811 	serial_out(up, UART_LCR, 0);
812 	old_fcr = serial_in(up, UART_FCR);
813 	old_mcr = serial8250_in_MCR(up);
814 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
815 		    UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
816 	serial8250_out_MCR(up, UART_MCR_LOOP);
817 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
818 	old_dl = serial_dl_read(up);
819 	serial_dl_write(up, 0x0001);
820 	serial_out(up, UART_LCR, 0x03);
821 	for (count = 0; count < 256; count++)
822 		serial_out(up, UART_TX, count);
823 	mdelay(20);/* FIXME - schedule_timeout */
824 	for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
825 	     (count < 256); count++)
826 		serial_in(up, UART_RX);
827 	serial_out(up, UART_FCR, old_fcr);
828 	serial8250_out_MCR(up, old_mcr);
829 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
830 	serial_dl_write(up, old_dl);
831 	serial_out(up, UART_LCR, old_lcr);
832 
833 	return count;
834 }
835 
836 /*
837  * Read UART ID using the divisor method - set DLL and DLM to zero
838  * and the revision will be in DLL and device type in DLM.  We
839  * preserve the device state across this.
840  */
841 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
842 {
843 	unsigned char old_lcr;
844 	unsigned int id, old_dl;
845 
846 	old_lcr = serial_in(p, UART_LCR);
847 	serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
848 	old_dl = serial_dl_read(p);
849 	serial_dl_write(p, 0);
850 	id = serial_dl_read(p);
851 	serial_dl_write(p, old_dl);
852 
853 	serial_out(p, UART_LCR, old_lcr);
854 
855 	return id;
856 }
857 
858 /*
859  * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
860  * When this function is called we know it is at least a StarTech
861  * 16650 V2, but it might be one of several StarTech UARTs, or one of
862  * its clones.  (We treat the broken original StarTech 16650 V1 as a
863  * 16550, and why not?  Startech doesn't seem to even acknowledge its
864  * existence.)
865  *
866  * What evil have men's minds wrought...
867  */
868 static void autoconfig_has_efr(struct uart_8250_port *up)
869 {
870 	unsigned int id1, id2, id3, rev;
871 
872 	/*
873 	 * Everything with an EFR has SLEEP
874 	 */
875 	up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
876 
877 	/*
878 	 * First we check to see if it's an Oxford Semiconductor UART.
879 	 *
880 	 * If we have to do this here because some non-National
881 	 * Semiconductor clone chips lock up if you try writing to the
882 	 * LSR register (which serial_icr_read does)
883 	 */
884 
885 	/*
886 	 * Check for Oxford Semiconductor 16C950.
887 	 *
888 	 * EFR [4] must be set else this test fails.
889 	 *
890 	 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
891 	 * claims that it's needed for 952 dual UART's (which are not
892 	 * recommended for new designs).
893 	 */
894 	up->acr = 0;
895 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
896 	serial_out(up, UART_EFR, UART_EFR_ECB);
897 	serial_out(up, UART_LCR, 0x00);
898 	id1 = serial_icr_read(up, UART_ID1);
899 	id2 = serial_icr_read(up, UART_ID2);
900 	id3 = serial_icr_read(up, UART_ID3);
901 	rev = serial_icr_read(up, UART_REV);
902 
903 	DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
904 
905 	if (id1 == 0x16 && id2 == 0xC9 &&
906 	    (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
907 		up->port.type = PORT_16C950;
908 
909 		/*
910 		 * Enable work around for the Oxford Semiconductor 952 rev B
911 		 * chip which causes it to seriously miscalculate baud rates
912 		 * when DLL is 0.
913 		 */
914 		if (id3 == 0x52 && rev == 0x01)
915 			up->bugs |= UART_BUG_QUOT;
916 		return;
917 	}
918 
919 	/*
920 	 * We check for a XR16C850 by setting DLL and DLM to 0, and then
921 	 * reading back DLL and DLM.  The chip type depends on the DLM
922 	 * value read back:
923 	 *  0x10 - XR16C850 and the DLL contains the chip revision.
924 	 *  0x12 - XR16C2850.
925 	 *  0x14 - XR16C854.
926 	 */
927 	id1 = autoconfig_read_divisor_id(up);
928 	DEBUG_AUTOCONF("850id=%04x ", id1);
929 
930 	id2 = id1 >> 8;
931 	if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
932 		up->port.type = PORT_16850;
933 		return;
934 	}
935 
936 	/*
937 	 * It wasn't an XR16C850.
938 	 *
939 	 * We distinguish between the '654 and the '650 by counting
940 	 * how many bytes are in the FIFO.  I'm using this for now,
941 	 * since that's the technique that was sent to me in the
942 	 * serial driver update, but I'm not convinced this works.
943 	 * I've had problems doing this in the past.  -TYT
944 	 */
945 	if (size_fifo(up) == 64)
946 		up->port.type = PORT_16654;
947 	else
948 		up->port.type = PORT_16650V2;
949 }
950 
951 /*
952  * We detected a chip without a FIFO.  Only two fall into
953  * this category - the original 8250 and the 16450.  The
954  * 16450 has a scratch register (accessible with LCR=0)
955  */
956 static void autoconfig_8250(struct uart_8250_port *up)
957 {
958 	unsigned char scratch, status1, status2;
959 
960 	up->port.type = PORT_8250;
961 
962 	scratch = serial_in(up, UART_SCR);
963 	serial_out(up, UART_SCR, 0xa5);
964 	status1 = serial_in(up, UART_SCR);
965 	serial_out(up, UART_SCR, 0x5a);
966 	status2 = serial_in(up, UART_SCR);
967 	serial_out(up, UART_SCR, scratch);
968 
969 	if (status1 == 0xa5 && status2 == 0x5a)
970 		up->port.type = PORT_16450;
971 }
972 
973 static int broken_efr(struct uart_8250_port *up)
974 {
975 	/*
976 	 * Exar ST16C2550 "A2" devices incorrectly detect as
977 	 * having an EFR, and report an ID of 0x0201.  See
978 	 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
979 	 */
980 	if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
981 		return 1;
982 
983 	return 0;
984 }
985 
986 /*
987  * We know that the chip has FIFOs.  Does it have an EFR?  The
988  * EFR is located in the same register position as the IIR and
989  * we know the top two bits of the IIR are currently set.  The
990  * EFR should contain zero.  Try to read the EFR.
991  */
992 static void autoconfig_16550a(struct uart_8250_port *up)
993 {
994 	unsigned char status1, status2;
995 	unsigned int iersave;
996 
997 	up->port.type = PORT_16550A;
998 	up->capabilities |= UART_CAP_FIFO;
999 
1000 	/*
1001 	 * XR17V35x UARTs have an extra divisor register, DLD
1002 	 * that gets enabled with when DLAB is set which will
1003 	 * cause the device to incorrectly match and assign
1004 	 * port type to PORT_16650.  The EFR for this UART is
1005 	 * found at offset 0x09. Instead check the Deice ID (DVID)
1006 	 * register for a 2, 4 or 8 port UART.
1007 	 */
1008 	if (up->port.flags & UPF_EXAR_EFR) {
1009 		status1 = serial_in(up, UART_EXAR_DVID);
1010 		if (status1 == 0x82 || status1 == 0x84 || status1 == 0x88) {
1011 			DEBUG_AUTOCONF("Exar XR17V35x ");
1012 			up->port.type = PORT_XR17V35X;
1013 			up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
1014 						UART_CAP_SLEEP;
1015 
1016 			return;
1017 		}
1018 
1019 	}
1020 
1021 	/*
1022 	 * Check for presence of the EFR when DLAB is set.
1023 	 * Only ST16C650V1 UARTs pass this test.
1024 	 */
1025 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1026 	if (serial_in(up, UART_EFR) == 0) {
1027 		serial_out(up, UART_EFR, 0xA8);
1028 		if (serial_in(up, UART_EFR) != 0) {
1029 			DEBUG_AUTOCONF("EFRv1 ");
1030 			up->port.type = PORT_16650;
1031 			up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
1032 		} else {
1033 			serial_out(up, UART_LCR, 0);
1034 			serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
1035 				   UART_FCR7_64BYTE);
1036 			status1 = serial_in(up, UART_IIR) >> 5;
1037 			serial_out(up, UART_FCR, 0);
1038 			serial_out(up, UART_LCR, 0);
1039 
1040 			if (status1 == 7)
1041 				up->port.type = PORT_16550A_FSL64;
1042 			else
1043 				DEBUG_AUTOCONF("Motorola 8xxx DUART ");
1044 		}
1045 		serial_out(up, UART_EFR, 0);
1046 		return;
1047 	}
1048 
1049 	/*
1050 	 * Maybe it requires 0xbf to be written to the LCR.
1051 	 * (other ST16C650V2 UARTs, TI16C752A, etc)
1052 	 */
1053 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1054 	if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
1055 		DEBUG_AUTOCONF("EFRv2 ");
1056 		autoconfig_has_efr(up);
1057 		return;
1058 	}
1059 
1060 	/*
1061 	 * Check for a National Semiconductor SuperIO chip.
1062 	 * Attempt to switch to bank 2, read the value of the LOOP bit
1063 	 * from EXCR1. Switch back to bank 0, change it in MCR. Then
1064 	 * switch back to bank 2, read it from EXCR1 again and check
1065 	 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
1066 	 */
1067 	serial_out(up, UART_LCR, 0);
1068 	status1 = serial8250_in_MCR(up);
1069 	serial_out(up, UART_LCR, 0xE0);
1070 	status2 = serial_in(up, 0x02); /* EXCR1 */
1071 
1072 	if (!((status2 ^ status1) & UART_MCR_LOOP)) {
1073 		serial_out(up, UART_LCR, 0);
1074 		serial8250_out_MCR(up, status1 ^ UART_MCR_LOOP);
1075 		serial_out(up, UART_LCR, 0xE0);
1076 		status2 = serial_in(up, 0x02); /* EXCR1 */
1077 		serial_out(up, UART_LCR, 0);
1078 		serial8250_out_MCR(up, status1);
1079 
1080 		if ((status2 ^ status1) & UART_MCR_LOOP) {
1081 			unsigned short quot;
1082 
1083 			serial_out(up, UART_LCR, 0xE0);
1084 
1085 			quot = serial_dl_read(up);
1086 			quot <<= 3;
1087 
1088 			if (ns16550a_goto_highspeed(up))
1089 				serial_dl_write(up, quot);
1090 
1091 			serial_out(up, UART_LCR, 0);
1092 
1093 			up->port.uartclk = 921600*16;
1094 			up->port.type = PORT_NS16550A;
1095 			up->capabilities |= UART_NATSEMI;
1096 			return;
1097 		}
1098 	}
1099 
1100 	/*
1101 	 * No EFR.  Try to detect a TI16750, which only sets bit 5 of
1102 	 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1103 	 * Try setting it with and without DLAB set.  Cheap clones
1104 	 * set bit 5 without DLAB set.
1105 	 */
1106 	serial_out(up, UART_LCR, 0);
1107 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1108 	status1 = serial_in(up, UART_IIR) >> 5;
1109 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1110 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1111 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1112 	status2 = serial_in(up, UART_IIR) >> 5;
1113 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1114 	serial_out(up, UART_LCR, 0);
1115 
1116 	DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1117 
1118 	if (status1 == 6 && status2 == 7) {
1119 		up->port.type = PORT_16750;
1120 		up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1121 		return;
1122 	}
1123 
1124 	/*
1125 	 * Try writing and reading the UART_IER_UUE bit (b6).
1126 	 * If it works, this is probably one of the Xscale platform's
1127 	 * internal UARTs.
1128 	 * We're going to explicitly set the UUE bit to 0 before
1129 	 * trying to write and read a 1 just to make sure it's not
1130 	 * already a 1 and maybe locked there before we even start start.
1131 	 */
1132 	iersave = serial_in(up, UART_IER);
1133 	serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
1134 	if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1135 		/*
1136 		 * OK it's in a known zero state, try writing and reading
1137 		 * without disturbing the current state of the other bits.
1138 		 */
1139 		serial_out(up, UART_IER, iersave | UART_IER_UUE);
1140 		if (serial_in(up, UART_IER) & UART_IER_UUE) {
1141 			/*
1142 			 * It's an Xscale.
1143 			 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1144 			 */
1145 			DEBUG_AUTOCONF("Xscale ");
1146 			up->port.type = PORT_XSCALE;
1147 			up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
1148 			return;
1149 		}
1150 	} else {
1151 		/*
1152 		 * If we got here we couldn't force the IER_UUE bit to 0.
1153 		 * Log it and continue.
1154 		 */
1155 		DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1156 	}
1157 	serial_out(up, UART_IER, iersave);
1158 
1159 	/*
1160 	 * Exar uarts have EFR in a weird location
1161 	 */
1162 	if (up->port.flags & UPF_EXAR_EFR) {
1163 		DEBUG_AUTOCONF("Exar XR17D15x ");
1164 		up->port.type = PORT_XR17D15X;
1165 		up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
1166 				    UART_CAP_SLEEP;
1167 
1168 		return;
1169 	}
1170 
1171 	/*
1172 	 * We distinguish between 16550A and U6 16550A by counting
1173 	 * how many bytes are in the FIFO.
1174 	 */
1175 	if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1176 		up->port.type = PORT_U6_16550A;
1177 		up->capabilities |= UART_CAP_AFE;
1178 	}
1179 }
1180 
1181 /*
1182  * This routine is called by rs_init() to initialize a specific serial
1183  * port.  It determines what type of UART chip this serial port is
1184  * using: 8250, 16450, 16550, 16550A.  The important question is
1185  * whether or not this UART is a 16550A or not, since this will
1186  * determine whether or not we can use its FIFO features or not.
1187  */
1188 static void autoconfig(struct uart_8250_port *up)
1189 {
1190 	unsigned char status1, scratch, scratch2, scratch3;
1191 	unsigned char save_lcr, save_mcr;
1192 	struct uart_port *port = &up->port;
1193 	unsigned long flags;
1194 	unsigned int old_capabilities;
1195 
1196 	if (!port->iobase && !port->mapbase && !port->membase)
1197 		return;
1198 
1199 	DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
1200 		       serial_index(port), port->iobase, port->membase);
1201 
1202 	/*
1203 	 * We really do need global IRQs disabled here - we're going to
1204 	 * be frobbing the chips IRQ enable register to see if it exists.
1205 	 */
1206 	spin_lock_irqsave(&port->lock, flags);
1207 
1208 	up->capabilities = 0;
1209 	up->bugs = 0;
1210 
1211 	if (!(port->flags & UPF_BUGGY_UART)) {
1212 		/*
1213 		 * Do a simple existence test first; if we fail this,
1214 		 * there's no point trying anything else.
1215 		 *
1216 		 * 0x80 is used as a nonsense port to prevent against
1217 		 * false positives due to ISA bus float.  The
1218 		 * assumption is that 0x80 is a non-existent port;
1219 		 * which should be safe since include/asm/io.h also
1220 		 * makes this assumption.
1221 		 *
1222 		 * Note: this is safe as long as MCR bit 4 is clear
1223 		 * and the device is in "PC" mode.
1224 		 */
1225 		scratch = serial_in(up, UART_IER);
1226 		serial_out(up, UART_IER, 0);
1227 #ifdef __i386__
1228 		outb(0xff, 0x080);
1229 #endif
1230 		/*
1231 		 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1232 		 * 16C754B) allow only to modify them if an EFR bit is set.
1233 		 */
1234 		scratch2 = serial_in(up, UART_IER) & 0x0f;
1235 		serial_out(up, UART_IER, 0x0F);
1236 #ifdef __i386__
1237 		outb(0, 0x080);
1238 #endif
1239 		scratch3 = serial_in(up, UART_IER) & 0x0f;
1240 		serial_out(up, UART_IER, scratch);
1241 		if (scratch2 != 0 || scratch3 != 0x0F) {
1242 			/*
1243 			 * We failed; there's nothing here
1244 			 */
1245 			spin_unlock_irqrestore(&port->lock, flags);
1246 			DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1247 				       scratch2, scratch3);
1248 			goto out;
1249 		}
1250 	}
1251 
1252 	save_mcr = serial8250_in_MCR(up);
1253 	save_lcr = serial_in(up, UART_LCR);
1254 
1255 	/*
1256 	 * Check to see if a UART is really there.  Certain broken
1257 	 * internal modems based on the Rockwell chipset fail this
1258 	 * test, because they apparently don't implement the loopback
1259 	 * test mode.  So this test is skipped on the COM 1 through
1260 	 * COM 4 ports.  This *should* be safe, since no board
1261 	 * manufacturer would be stupid enough to design a board
1262 	 * that conflicts with COM 1-4 --- we hope!
1263 	 */
1264 	if (!(port->flags & UPF_SKIP_TEST)) {
1265 		serial8250_out_MCR(up, UART_MCR_LOOP | 0x0A);
1266 		status1 = serial_in(up, UART_MSR) & 0xF0;
1267 		serial8250_out_MCR(up, save_mcr);
1268 		if (status1 != 0x90) {
1269 			spin_unlock_irqrestore(&port->lock, flags);
1270 			DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1271 				       status1);
1272 			goto out;
1273 		}
1274 	}
1275 
1276 	/*
1277 	 * We're pretty sure there's a port here.  Lets find out what
1278 	 * type of port it is.  The IIR top two bits allows us to find
1279 	 * out if it's 8250 or 16450, 16550, 16550A or later.  This
1280 	 * determines what we test for next.
1281 	 *
1282 	 * We also initialise the EFR (if any) to zero for later.  The
1283 	 * EFR occupies the same register location as the FCR and IIR.
1284 	 */
1285 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1286 	serial_out(up, UART_EFR, 0);
1287 	serial_out(up, UART_LCR, 0);
1288 
1289 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1290 	scratch = serial_in(up, UART_IIR) >> 6;
1291 
1292 	switch (scratch) {
1293 	case 0:
1294 		autoconfig_8250(up);
1295 		break;
1296 	case 1:
1297 		port->type = PORT_UNKNOWN;
1298 		break;
1299 	case 2:
1300 		port->type = PORT_16550;
1301 		break;
1302 	case 3:
1303 		autoconfig_16550a(up);
1304 		break;
1305 	}
1306 
1307 #ifdef CONFIG_SERIAL_8250_RSA
1308 	/*
1309 	 * Only probe for RSA ports if we got the region.
1310 	 */
1311 	if (port->type == PORT_16550A && up->probe & UART_PROBE_RSA &&
1312 	    __enable_rsa(up))
1313 		port->type = PORT_RSA;
1314 #endif
1315 
1316 	serial_out(up, UART_LCR, save_lcr);
1317 
1318 	port->fifosize = uart_config[up->port.type].fifo_size;
1319 	old_capabilities = up->capabilities;
1320 	up->capabilities = uart_config[port->type].flags;
1321 	up->tx_loadsz = uart_config[port->type].tx_loadsz;
1322 
1323 	if (port->type == PORT_UNKNOWN)
1324 		goto out_lock;
1325 
1326 	/*
1327 	 * Reset the UART.
1328 	 */
1329 #ifdef CONFIG_SERIAL_8250_RSA
1330 	if (port->type == PORT_RSA)
1331 		serial_out(up, UART_RSA_FRR, 0);
1332 #endif
1333 	serial8250_out_MCR(up, save_mcr);
1334 	serial8250_clear_fifos(up);
1335 	serial_in(up, UART_RX);
1336 	if (up->capabilities & UART_CAP_UUE)
1337 		serial_out(up, UART_IER, UART_IER_UUE);
1338 	else
1339 		serial_out(up, UART_IER, 0);
1340 
1341 out_lock:
1342 	spin_unlock_irqrestore(&port->lock, flags);
1343 
1344 	/*
1345 	 * Check if the device is a Fintek F81216A
1346 	 */
1347 	if (port->type == PORT_16550A && port->iotype == UPIO_PORT)
1348 		fintek_8250_probe(up);
1349 
1350 	if (up->capabilities != old_capabilities) {
1351 		pr_warn("ttyS%d: detected caps %08x should be %08x\n",
1352 		       serial_index(port), old_capabilities,
1353 		       up->capabilities);
1354 	}
1355 out:
1356 	DEBUG_AUTOCONF("iir=%d ", scratch);
1357 	DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
1358 }
1359 
1360 static void autoconfig_irq(struct uart_8250_port *up)
1361 {
1362 	struct uart_port *port = &up->port;
1363 	unsigned char save_mcr, save_ier;
1364 	unsigned char save_ICP = 0;
1365 	unsigned int ICP = 0;
1366 	unsigned long irqs;
1367 	int irq;
1368 
1369 	if (port->flags & UPF_FOURPORT) {
1370 		ICP = (port->iobase & 0xfe0) | 0x1f;
1371 		save_ICP = inb_p(ICP);
1372 		outb_p(0x80, ICP);
1373 		inb_p(ICP);
1374 	}
1375 
1376 	if (uart_console(port))
1377 		console_lock();
1378 
1379 	/* forget possible initially masked and pending IRQ */
1380 	probe_irq_off(probe_irq_on());
1381 	save_mcr = serial8250_in_MCR(up);
1382 	save_ier = serial_in(up, UART_IER);
1383 	serial8250_out_MCR(up, UART_MCR_OUT1 | UART_MCR_OUT2);
1384 
1385 	irqs = probe_irq_on();
1386 	serial8250_out_MCR(up, 0);
1387 	udelay(10);
1388 	if (port->flags & UPF_FOURPORT) {
1389 		serial8250_out_MCR(up, UART_MCR_DTR | UART_MCR_RTS);
1390 	} else {
1391 		serial8250_out_MCR(up,
1392 			UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1393 	}
1394 	serial_out(up, UART_IER, 0x0f);	/* enable all intrs */
1395 	serial_in(up, UART_LSR);
1396 	serial_in(up, UART_RX);
1397 	serial_in(up, UART_IIR);
1398 	serial_in(up, UART_MSR);
1399 	serial_out(up, UART_TX, 0xFF);
1400 	udelay(20);
1401 	irq = probe_irq_off(irqs);
1402 
1403 	serial8250_out_MCR(up, save_mcr);
1404 	serial_out(up, UART_IER, save_ier);
1405 
1406 	if (port->flags & UPF_FOURPORT)
1407 		outb_p(save_ICP, ICP);
1408 
1409 	if (uart_console(port))
1410 		console_unlock();
1411 
1412 	port->irq = (irq > 0) ? irq : 0;
1413 }
1414 
1415 static void serial8250_stop_rx(struct uart_port *port)
1416 {
1417 	struct uart_8250_port *up = up_to_u8250p(port);
1418 
1419 	serial8250_rpm_get(up);
1420 
1421 	up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1422 	up->port.read_status_mask &= ~UART_LSR_DR;
1423 	serial_port_out(port, UART_IER, up->ier);
1424 
1425 	serial8250_rpm_put(up);
1426 }
1427 
1428 static void __do_stop_tx_rs485(struct uart_8250_port *p)
1429 {
1430 	serial8250_em485_rts_after_send(p);
1431 
1432 	/*
1433 	 * Empty the RX FIFO, we are not interested in anything
1434 	 * received during the half-duplex transmission.
1435 	 * Enable previously disabled RX interrupts.
1436 	 */
1437 	if (!(p->port.rs485.flags & SER_RS485_RX_DURING_TX)) {
1438 		serial8250_clear_and_reinit_fifos(p);
1439 
1440 		p->ier |= UART_IER_RLSI | UART_IER_RDI;
1441 		serial_port_out(&p->port, UART_IER, p->ier);
1442 	}
1443 }
1444 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t)
1445 {
1446 	struct uart_8250_em485 *em485;
1447 	struct uart_8250_port *p;
1448 	unsigned long flags;
1449 
1450 	em485 = container_of(t, struct uart_8250_em485, stop_tx_timer);
1451 	p = em485->port;
1452 
1453 	serial8250_rpm_get(p);
1454 	spin_lock_irqsave(&p->port.lock, flags);
1455 	if (em485->active_timer == &em485->stop_tx_timer) {
1456 		__do_stop_tx_rs485(p);
1457 		em485->active_timer = NULL;
1458 	}
1459 	spin_unlock_irqrestore(&p->port.lock, flags);
1460 	serial8250_rpm_put(p);
1461 	return HRTIMER_NORESTART;
1462 }
1463 
1464 static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec)
1465 {
1466 	long sec = msec / 1000;
1467 	long nsec = (msec % 1000) * 1000000;
1468 	ktime_t t = ktime_set(sec, nsec);
1469 
1470 	hrtimer_start(hrt, t, HRTIMER_MODE_REL);
1471 }
1472 
1473 static void __stop_tx_rs485(struct uart_8250_port *p)
1474 {
1475 	struct uart_8250_em485 *em485 = p->em485;
1476 
1477 	/*
1478 	 * __do_stop_tx_rs485 is going to set RTS according to config
1479 	 * AND flush RX FIFO if required.
1480 	 */
1481 	if (p->port.rs485.delay_rts_after_send > 0) {
1482 		em485->active_timer = &em485->stop_tx_timer;
1483 		start_hrtimer_ms(&em485->stop_tx_timer,
1484 				   p->port.rs485.delay_rts_after_send);
1485 	} else {
1486 		__do_stop_tx_rs485(p);
1487 	}
1488 }
1489 
1490 static inline void __do_stop_tx(struct uart_8250_port *p)
1491 {
1492 	if (p->ier & UART_IER_THRI) {
1493 		p->ier &= ~UART_IER_THRI;
1494 		serial_out(p, UART_IER, p->ier);
1495 		serial8250_rpm_put_tx(p);
1496 	}
1497 }
1498 
1499 static inline void __stop_tx(struct uart_8250_port *p)
1500 {
1501 	struct uart_8250_em485 *em485 = p->em485;
1502 
1503 	if (em485) {
1504 		unsigned char lsr = serial_in(p, UART_LSR);
1505 		/*
1506 		 * To provide required timeing and allow FIFO transfer,
1507 		 * __stop_tx_rs485() must be called only when both FIFO and
1508 		 * shift register are empty. It is for device driver to enable
1509 		 * interrupt on TEMT.
1510 		 */
1511 		if ((lsr & BOTH_EMPTY) != BOTH_EMPTY)
1512 			return;
1513 
1514 		em485->active_timer = NULL;
1515 
1516 		__stop_tx_rs485(p);
1517 	}
1518 	__do_stop_tx(p);
1519 }
1520 
1521 static void serial8250_stop_tx(struct uart_port *port)
1522 {
1523 	struct uart_8250_port *up = up_to_u8250p(port);
1524 
1525 	serial8250_rpm_get(up);
1526 	__stop_tx(up);
1527 
1528 	/*
1529 	 * We really want to stop the transmitter from sending.
1530 	 */
1531 	if (port->type == PORT_16C950) {
1532 		up->acr |= UART_ACR_TXDIS;
1533 		serial_icr_write(up, UART_ACR, up->acr);
1534 	}
1535 	serial8250_rpm_put(up);
1536 }
1537 
1538 static inline void __start_tx(struct uart_port *port)
1539 {
1540 	struct uart_8250_port *up = up_to_u8250p(port);
1541 
1542 	if (up->dma && !up->dma->tx_dma(up))
1543 		return;
1544 
1545 	if (!(up->ier & UART_IER_THRI)) {
1546 		up->ier |= UART_IER_THRI;
1547 		serial_port_out(port, UART_IER, up->ier);
1548 
1549 		if (up->bugs & UART_BUG_TXEN) {
1550 			unsigned char lsr;
1551 
1552 			lsr = serial_in(up, UART_LSR);
1553 			up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1554 			if (lsr & UART_LSR_THRE)
1555 				serial8250_tx_chars(up);
1556 		}
1557 	}
1558 
1559 	/*
1560 	 * Re-enable the transmitter if we disabled it.
1561 	 */
1562 	if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1563 		up->acr &= ~UART_ACR_TXDIS;
1564 		serial_icr_write(up, UART_ACR, up->acr);
1565 	}
1566 }
1567 
1568 static inline void start_tx_rs485(struct uart_port *port)
1569 {
1570 	struct uart_8250_port *up = up_to_u8250p(port);
1571 	struct uart_8250_em485 *em485 = up->em485;
1572 	unsigned char mcr;
1573 
1574 	if (!(up->port.rs485.flags & SER_RS485_RX_DURING_TX))
1575 		serial8250_stop_rx(&up->port);
1576 
1577 	em485->active_timer = NULL;
1578 
1579 	mcr = serial8250_in_MCR(up);
1580 	if (!!(up->port.rs485.flags & SER_RS485_RTS_ON_SEND) !=
1581 	    !!(mcr & UART_MCR_RTS)) {
1582 		if (up->port.rs485.flags & SER_RS485_RTS_ON_SEND)
1583 			mcr |= UART_MCR_RTS;
1584 		else
1585 			mcr &= ~UART_MCR_RTS;
1586 		serial8250_out_MCR(up, mcr);
1587 
1588 		if (up->port.rs485.delay_rts_before_send > 0) {
1589 			em485->active_timer = &em485->start_tx_timer;
1590 			start_hrtimer_ms(&em485->start_tx_timer,
1591 					 up->port.rs485.delay_rts_before_send);
1592 			return;
1593 		}
1594 	}
1595 
1596 	__start_tx(port);
1597 }
1598 
1599 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t)
1600 {
1601 	struct uart_8250_em485 *em485;
1602 	struct uart_8250_port *p;
1603 	unsigned long flags;
1604 
1605 	em485 = container_of(t, struct uart_8250_em485, start_tx_timer);
1606 	p = em485->port;
1607 
1608 	spin_lock_irqsave(&p->port.lock, flags);
1609 	if (em485->active_timer == &em485->start_tx_timer) {
1610 		__start_tx(&p->port);
1611 		em485->active_timer = NULL;
1612 	}
1613 	spin_unlock_irqrestore(&p->port.lock, flags);
1614 	return HRTIMER_NORESTART;
1615 }
1616 
1617 static void serial8250_start_tx(struct uart_port *port)
1618 {
1619 	struct uart_8250_port *up = up_to_u8250p(port);
1620 	struct uart_8250_em485 *em485 = up->em485;
1621 
1622 	serial8250_rpm_get_tx(up);
1623 
1624 	if (em485 &&
1625 	    em485->active_timer == &em485->start_tx_timer)
1626 		return;
1627 
1628 	if (em485)
1629 		start_tx_rs485(port);
1630 	else
1631 		__start_tx(port);
1632 }
1633 
1634 static void serial8250_throttle(struct uart_port *port)
1635 {
1636 	port->throttle(port);
1637 }
1638 
1639 static void serial8250_unthrottle(struct uart_port *port)
1640 {
1641 	port->unthrottle(port);
1642 }
1643 
1644 static void serial8250_disable_ms(struct uart_port *port)
1645 {
1646 	struct uart_8250_port *up = up_to_u8250p(port);
1647 
1648 	/* no MSR capabilities */
1649 	if (up->bugs & UART_BUG_NOMSR)
1650 		return;
1651 
1652 	up->ier &= ~UART_IER_MSI;
1653 	serial_port_out(port, UART_IER, up->ier);
1654 }
1655 
1656 static void serial8250_enable_ms(struct uart_port *port)
1657 {
1658 	struct uart_8250_port *up = up_to_u8250p(port);
1659 
1660 	/* no MSR capabilities */
1661 	if (up->bugs & UART_BUG_NOMSR)
1662 		return;
1663 
1664 	up->ier |= UART_IER_MSI;
1665 
1666 	serial8250_rpm_get(up);
1667 	serial_port_out(port, UART_IER, up->ier);
1668 	serial8250_rpm_put(up);
1669 }
1670 
1671 static void serial8250_read_char(struct uart_8250_port *up, unsigned char lsr)
1672 {
1673 	struct uart_port *port = &up->port;
1674 	unsigned char ch;
1675 	char flag = TTY_NORMAL;
1676 
1677 	if (likely(lsr & UART_LSR_DR))
1678 		ch = serial_in(up, UART_RX);
1679 	else
1680 		/*
1681 		 * Intel 82571 has a Serial Over Lan device that will
1682 		 * set UART_LSR_BI without setting UART_LSR_DR when
1683 		 * it receives a break. To avoid reading from the
1684 		 * receive buffer without UART_LSR_DR bit set, we
1685 		 * just force the read character to be 0
1686 		 */
1687 		ch = 0;
1688 
1689 	port->icount.rx++;
1690 
1691 	lsr |= up->lsr_saved_flags;
1692 	up->lsr_saved_flags = 0;
1693 
1694 	if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1695 		if (lsr & UART_LSR_BI) {
1696 			lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1697 			port->icount.brk++;
1698 			/*
1699 			 * We do the SysRQ and SAK checking
1700 			 * here because otherwise the break
1701 			 * may get masked by ignore_status_mask
1702 			 * or read_status_mask.
1703 			 */
1704 			if (uart_handle_break(port))
1705 				return;
1706 		} else if (lsr & UART_LSR_PE)
1707 			port->icount.parity++;
1708 		else if (lsr & UART_LSR_FE)
1709 			port->icount.frame++;
1710 		if (lsr & UART_LSR_OE)
1711 			port->icount.overrun++;
1712 
1713 		/*
1714 		 * Mask off conditions which should be ignored.
1715 		 */
1716 		lsr &= port->read_status_mask;
1717 
1718 		if (lsr & UART_LSR_BI) {
1719 			pr_debug("%s: handling break\n", __func__);
1720 			flag = TTY_BREAK;
1721 		} else if (lsr & UART_LSR_PE)
1722 			flag = TTY_PARITY;
1723 		else if (lsr & UART_LSR_FE)
1724 			flag = TTY_FRAME;
1725 	}
1726 	if (uart_handle_sysrq_char(port, ch))
1727 		return;
1728 
1729 	uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
1730 }
1731 
1732 /*
1733  * serial8250_rx_chars: processes according to the passed in LSR
1734  * value, and returns the remaining LSR bits not handled
1735  * by this Rx routine.
1736  */
1737 unsigned char serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
1738 {
1739 	struct uart_port *port = &up->port;
1740 	int max_count = 256;
1741 
1742 	do {
1743 		serial8250_read_char(up, lsr);
1744 		if (--max_count == 0)
1745 			break;
1746 		lsr = serial_in(up, UART_LSR);
1747 	} while (lsr & (UART_LSR_DR | UART_LSR_BI));
1748 
1749 	tty_flip_buffer_push(&port->state->port);
1750 	return lsr;
1751 }
1752 EXPORT_SYMBOL_GPL(serial8250_rx_chars);
1753 
1754 void serial8250_tx_chars(struct uart_8250_port *up)
1755 {
1756 	struct uart_port *port = &up->port;
1757 	struct circ_buf *xmit = &port->state->xmit;
1758 	int count;
1759 
1760 	if (port->x_char) {
1761 		serial_out(up, UART_TX, port->x_char);
1762 		port->icount.tx++;
1763 		port->x_char = 0;
1764 		return;
1765 	}
1766 	if (uart_tx_stopped(port)) {
1767 		serial8250_stop_tx(port);
1768 		return;
1769 	}
1770 	if (uart_circ_empty(xmit)) {
1771 		__stop_tx(up);
1772 		return;
1773 	}
1774 
1775 	count = up->tx_loadsz;
1776 	do {
1777 		serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1778 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1779 		port->icount.tx++;
1780 		if (uart_circ_empty(xmit))
1781 			break;
1782 		if ((up->capabilities & UART_CAP_HFIFO) &&
1783 		    (serial_in(up, UART_LSR) & BOTH_EMPTY) != BOTH_EMPTY)
1784 			break;
1785 		/* The BCM2835 MINI UART THRE bit is really a not-full bit. */
1786 		if ((up->capabilities & UART_CAP_MINI) &&
1787 		    !(serial_in(up, UART_LSR) & UART_LSR_THRE))
1788 			break;
1789 	} while (--count > 0);
1790 
1791 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1792 		uart_write_wakeup(port);
1793 
1794 	/*
1795 	 * With RPM enabled, we have to wait until the FIFO is empty before the
1796 	 * HW can go idle. So we get here once again with empty FIFO and disable
1797 	 * the interrupt and RPM in __stop_tx()
1798 	 */
1799 	if (uart_circ_empty(xmit) && !(up->capabilities & UART_CAP_RPM))
1800 		__stop_tx(up);
1801 }
1802 EXPORT_SYMBOL_GPL(serial8250_tx_chars);
1803 
1804 /* Caller holds uart port lock */
1805 unsigned int serial8250_modem_status(struct uart_8250_port *up)
1806 {
1807 	struct uart_port *port = &up->port;
1808 	unsigned int status = serial_in(up, UART_MSR);
1809 
1810 	status |= up->msr_saved_flags;
1811 	up->msr_saved_flags = 0;
1812 	if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1813 	    port->state != NULL) {
1814 		if (status & UART_MSR_TERI)
1815 			port->icount.rng++;
1816 		if (status & UART_MSR_DDSR)
1817 			port->icount.dsr++;
1818 		if (status & UART_MSR_DDCD)
1819 			uart_handle_dcd_change(port, status & UART_MSR_DCD);
1820 		if (status & UART_MSR_DCTS)
1821 			uart_handle_cts_change(port, status & UART_MSR_CTS);
1822 
1823 		wake_up_interruptible(&port->state->port.delta_msr_wait);
1824 	}
1825 
1826 	return status;
1827 }
1828 EXPORT_SYMBOL_GPL(serial8250_modem_status);
1829 
1830 static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir)
1831 {
1832 	switch (iir & 0x3f) {
1833 	case UART_IIR_RX_TIMEOUT:
1834 		serial8250_rx_dma_flush(up);
1835 		/* fall-through */
1836 	case UART_IIR_RLSI:
1837 		return true;
1838 	}
1839 	return up->dma->rx_dma(up);
1840 }
1841 
1842 /*
1843  * This handles the interrupt from one port.
1844  */
1845 int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
1846 {
1847 	unsigned char status;
1848 	unsigned long flags;
1849 	struct uart_8250_port *up = up_to_u8250p(port);
1850 
1851 	if (iir & UART_IIR_NO_INT)
1852 		return 0;
1853 
1854 	spin_lock_irqsave(&port->lock, flags);
1855 
1856 	status = serial_port_in(port, UART_LSR);
1857 
1858 	if (status & (UART_LSR_DR | UART_LSR_BI)) {
1859 		if (!up->dma || handle_rx_dma(up, iir))
1860 			status = serial8250_rx_chars(up, status);
1861 	}
1862 	serial8250_modem_status(up);
1863 	if ((!up->dma || up->dma->tx_err) && (status & UART_LSR_THRE))
1864 		serial8250_tx_chars(up);
1865 
1866 	spin_unlock_irqrestore(&port->lock, flags);
1867 	return 1;
1868 }
1869 EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1870 
1871 static int serial8250_default_handle_irq(struct uart_port *port)
1872 {
1873 	struct uart_8250_port *up = up_to_u8250p(port);
1874 	unsigned int iir;
1875 	int ret;
1876 
1877 	serial8250_rpm_get(up);
1878 
1879 	iir = serial_port_in(port, UART_IIR);
1880 	ret = serial8250_handle_irq(port, iir);
1881 
1882 	serial8250_rpm_put(up);
1883 	return ret;
1884 }
1885 
1886 /*
1887  * These Exar UARTs have an extra interrupt indicator that could
1888  * fire for a few unimplemented interrupts.  One of which is a
1889  * wakeup event when coming out of sleep.  Put this here just
1890  * to be on the safe side that these interrupts don't go unhandled.
1891  */
1892 static int exar_handle_irq(struct uart_port *port)
1893 {
1894 	unsigned int iir = serial_port_in(port, UART_IIR);
1895 	int ret = 0;
1896 
1897 	if (((port->type == PORT_XR17V35X) || (port->type == PORT_XR17D15X)) &&
1898 	    serial_port_in(port, UART_EXAR_INT0) != 0)
1899 		ret = 1;
1900 
1901 	ret |= serial8250_handle_irq(port, iir);
1902 
1903 	return ret;
1904 }
1905 
1906 /*
1907  * Newer 16550 compatible parts such as the SC16C650 & Altera 16550 Soft IP
1908  * have a programmable TX threshold that triggers the THRE interrupt in
1909  * the IIR register. In this case, the THRE interrupt indicates the FIFO
1910  * has space available. Load it up with tx_loadsz bytes.
1911  */
1912 static int serial8250_tx_threshold_handle_irq(struct uart_port *port)
1913 {
1914 	unsigned long flags;
1915 	unsigned int iir = serial_port_in(port, UART_IIR);
1916 
1917 	/* TX Threshold IRQ triggered so load up FIFO */
1918 	if ((iir & UART_IIR_ID) == UART_IIR_THRI) {
1919 		struct uart_8250_port *up = up_to_u8250p(port);
1920 
1921 		spin_lock_irqsave(&port->lock, flags);
1922 		serial8250_tx_chars(up);
1923 		spin_unlock_irqrestore(&port->lock, flags);
1924 	}
1925 
1926 	iir = serial_port_in(port, UART_IIR);
1927 	return serial8250_handle_irq(port, iir);
1928 }
1929 
1930 static unsigned int serial8250_tx_empty(struct uart_port *port)
1931 {
1932 	struct uart_8250_port *up = up_to_u8250p(port);
1933 	unsigned long flags;
1934 	unsigned int lsr;
1935 
1936 	serial8250_rpm_get(up);
1937 
1938 	spin_lock_irqsave(&port->lock, flags);
1939 	lsr = serial_port_in(port, UART_LSR);
1940 	up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1941 	spin_unlock_irqrestore(&port->lock, flags);
1942 
1943 	serial8250_rpm_put(up);
1944 
1945 	return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
1946 }
1947 
1948 unsigned int serial8250_do_get_mctrl(struct uart_port *port)
1949 {
1950 	struct uart_8250_port *up = up_to_u8250p(port);
1951 	unsigned int status;
1952 	unsigned int ret;
1953 
1954 	serial8250_rpm_get(up);
1955 	status = serial8250_modem_status(up);
1956 	serial8250_rpm_put(up);
1957 
1958 	ret = 0;
1959 	if (status & UART_MSR_DCD)
1960 		ret |= TIOCM_CAR;
1961 	if (status & UART_MSR_RI)
1962 		ret |= TIOCM_RNG;
1963 	if (status & UART_MSR_DSR)
1964 		ret |= TIOCM_DSR;
1965 	if (status & UART_MSR_CTS)
1966 		ret |= TIOCM_CTS;
1967 	return ret;
1968 }
1969 EXPORT_SYMBOL_GPL(serial8250_do_get_mctrl);
1970 
1971 static unsigned int serial8250_get_mctrl(struct uart_port *port)
1972 {
1973 	if (port->get_mctrl)
1974 		return port->get_mctrl(port);
1975 	return serial8250_do_get_mctrl(port);
1976 }
1977 
1978 void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl)
1979 {
1980 	struct uart_8250_port *up = up_to_u8250p(port);
1981 	unsigned char mcr = 0;
1982 
1983 	if (mctrl & TIOCM_RTS)
1984 		mcr |= UART_MCR_RTS;
1985 	if (mctrl & TIOCM_DTR)
1986 		mcr |= UART_MCR_DTR;
1987 	if (mctrl & TIOCM_OUT1)
1988 		mcr |= UART_MCR_OUT1;
1989 	if (mctrl & TIOCM_OUT2)
1990 		mcr |= UART_MCR_OUT2;
1991 	if (mctrl & TIOCM_LOOP)
1992 		mcr |= UART_MCR_LOOP;
1993 
1994 	mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1995 
1996 	serial8250_out_MCR(up, mcr);
1997 }
1998 EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);
1999 
2000 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
2001 {
2002 	if (port->set_mctrl)
2003 		port->set_mctrl(port, mctrl);
2004 	else
2005 		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 		touch_nmi_watchdog();
2043 	}
2044 
2045 	/* Wait up to 1s for flow control if necessary */
2046 	if (up->port.flags & UPF_CONS_FLOW) {
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->fifosize)
2127 		port->fifosize = uart_config[port->type].fifo_size;
2128 	if (!up->tx_loadsz)
2129 		up->tx_loadsz = uart_config[port->type].tx_loadsz;
2130 	if (!up->capabilities)
2131 		up->capabilities = uart_config[port->type].flags;
2132 	up->mcr = 0;
2133 
2134 	if (port->iotype != up->cur_iotype)
2135 		set_io_from_upio(port);
2136 
2137 	serial8250_rpm_get(up);
2138 	if (port->type == PORT_16C950) {
2139 		/* Wake up and initialize UART */
2140 		up->acr = 0;
2141 		serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2142 		serial_port_out(port, UART_EFR, UART_EFR_ECB);
2143 		serial_port_out(port, UART_IER, 0);
2144 		serial_port_out(port, UART_LCR, 0);
2145 		serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2146 		serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2147 		serial_port_out(port, UART_EFR, UART_EFR_ECB);
2148 		serial_port_out(port, UART_LCR, 0);
2149 	}
2150 
2151 	if (port->type == PORT_DA830) {
2152 		/* Reset the port */
2153 		serial_port_out(port, UART_IER, 0);
2154 		serial_port_out(port, UART_DA830_PWREMU_MGMT, 0);
2155 		mdelay(10);
2156 
2157 		/* Enable Tx, Rx and free run mode */
2158 		serial_port_out(port, UART_DA830_PWREMU_MGMT,
2159 				UART_DA830_PWREMU_MGMT_UTRST |
2160 				UART_DA830_PWREMU_MGMT_URRST |
2161 				UART_DA830_PWREMU_MGMT_FREE);
2162 	}
2163 
2164 #ifdef CONFIG_SERIAL_8250_RSA
2165 	/*
2166 	 * If this is an RSA port, see if we can kick it up to the
2167 	 * higher speed clock.
2168 	 */
2169 	enable_rsa(up);
2170 #endif
2171 
2172 	if (port->type == PORT_XR17V35X) {
2173 		/*
2174 		 * First enable access to IER [7:5], ISR [5:4], FCR [5:4],
2175 		 * MCR [7:5] and MSR [7:0]
2176 		 */
2177 		serial_port_out(port, UART_XR_EFR, UART_EFR_ECB);
2178 
2179 		/*
2180 		 * Make sure all interrups are masked until initialization is
2181 		 * complete and the FIFOs are cleared
2182 		 */
2183 		serial_port_out(port, UART_IER, 0);
2184 	}
2185 
2186 	/*
2187 	 * Clear the FIFO buffers and disable them.
2188 	 * (they will be reenabled in set_termios())
2189 	 */
2190 	serial8250_clear_fifos(up);
2191 
2192 	/*
2193 	 * Clear the interrupt registers.
2194 	 */
2195 	serial_port_in(port, UART_LSR);
2196 	serial_port_in(port, UART_RX);
2197 	serial_port_in(port, UART_IIR);
2198 	serial_port_in(port, UART_MSR);
2199 	if ((port->type == PORT_XR17V35X) || (port->type == PORT_XR17D15X))
2200 		serial_port_in(port, UART_EXAR_INT0);
2201 
2202 	/*
2203 	 * At this point, there's no way the LSR could still be 0xff;
2204 	 * if it is, then bail out, because there's likely no UART
2205 	 * here.
2206 	 */
2207 	if (!(port->flags & UPF_BUGGY_UART) &&
2208 	    (serial_port_in(port, UART_LSR) == 0xff)) {
2209 		printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2210 				   serial_index(port));
2211 		retval = -ENODEV;
2212 		goto out;
2213 	}
2214 
2215 	/*
2216 	 * For a XR16C850, we need to set the trigger levels
2217 	 */
2218 	if (port->type == PORT_16850) {
2219 		unsigned char fctr;
2220 
2221 		serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
2222 
2223 		fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2224 		serial_port_out(port, UART_FCTR,
2225 				fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2226 		serial_port_out(port, UART_TRG, UART_TRG_96);
2227 		serial_port_out(port, UART_FCTR,
2228 				fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2229 		serial_port_out(port, UART_TRG, UART_TRG_96);
2230 
2231 		serial_port_out(port, UART_LCR, 0);
2232 	}
2233 
2234 	/*
2235 	 * For the Altera 16550 variants, set TX threshold trigger level.
2236 	 */
2237 	if (((port->type == PORT_ALTR_16550_F32) ||
2238 	     (port->type == PORT_ALTR_16550_F64) ||
2239 	     (port->type == PORT_ALTR_16550_F128)) && (port->fifosize > 1)) {
2240 		/* Bounds checking of TX threshold (valid 0 to fifosize-2) */
2241 		if ((up->tx_loadsz < 2) || (up->tx_loadsz > port->fifosize)) {
2242 			pr_err("ttyS%d TX FIFO Threshold errors, skipping\n",
2243 			       serial_index(port));
2244 		} else {
2245 			serial_port_out(port, UART_ALTR_AFR,
2246 					UART_ALTR_EN_TXFIFO_LW);
2247 			serial_port_out(port, UART_ALTR_TX_LOW,
2248 					port->fifosize - up->tx_loadsz);
2249 			port->handle_irq = serial8250_tx_threshold_handle_irq;
2250 		}
2251 	}
2252 
2253 	if (port->irq && !(up->port.flags & UPF_NO_THRE_TEST)) {
2254 		unsigned char iir1;
2255 		/*
2256 		 * Test for UARTs that do not reassert THRE when the
2257 		 * transmitter is idle and the interrupt has already
2258 		 * been cleared.  Real 16550s should always reassert
2259 		 * this interrupt whenever the transmitter is idle and
2260 		 * the interrupt is enabled.  Delays are necessary to
2261 		 * allow register changes to become visible.
2262 		 */
2263 		spin_lock_irqsave(&port->lock, flags);
2264 		if (up->port.irqflags & IRQF_SHARED)
2265 			disable_irq_nosync(port->irq);
2266 
2267 		wait_for_xmitr(up, UART_LSR_THRE);
2268 		serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2269 		udelay(1); /* allow THRE to set */
2270 		iir1 = serial_port_in(port, UART_IIR);
2271 		serial_port_out(port, UART_IER, 0);
2272 		serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2273 		udelay(1); /* allow a working UART time to re-assert THRE */
2274 		iir = serial_port_in(port, UART_IIR);
2275 		serial_port_out(port, UART_IER, 0);
2276 
2277 		if (port->irqflags & IRQF_SHARED)
2278 			enable_irq(port->irq);
2279 		spin_unlock_irqrestore(&port->lock, flags);
2280 
2281 		/*
2282 		 * If the interrupt is not reasserted, or we otherwise
2283 		 * don't trust the iir, setup a timer to kick the UART
2284 		 * on a regular basis.
2285 		 */
2286 		if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
2287 		    up->port.flags & UPF_BUG_THRE) {
2288 			up->bugs |= UART_BUG_THRE;
2289 		}
2290 	}
2291 
2292 	retval = up->ops->setup_irq(up);
2293 	if (retval)
2294 		goto out;
2295 
2296 	/*
2297 	 * Now, initialize the UART
2298 	 */
2299 	serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
2300 
2301 	spin_lock_irqsave(&port->lock, flags);
2302 	if (up->port.flags & UPF_FOURPORT) {
2303 		if (!up->port.irq)
2304 			up->port.mctrl |= TIOCM_OUT1;
2305 	} else
2306 		/*
2307 		 * Most PC uarts need OUT2 raised to enable interrupts.
2308 		 */
2309 		if (port->irq)
2310 			up->port.mctrl |= TIOCM_OUT2;
2311 
2312 	serial8250_set_mctrl(port, port->mctrl);
2313 
2314 	/*
2315 	 * Serial over Lan (SoL) hack:
2316 	 * Intel 8257x Gigabit ethernet chips have a 16550 emulation, to be
2317 	 * used for Serial Over Lan.  Those chips take a longer time than a
2318 	 * normal serial device to signalize that a transmission data was
2319 	 * queued. Due to that, the above test generally fails. One solution
2320 	 * would be to delay the reading of iir. However, this is not
2321 	 * reliable, since the timeout is variable. So, let's just don't
2322 	 * test if we receive TX irq.  This way, we'll never enable
2323 	 * UART_BUG_TXEN.
2324 	 */
2325 	if (up->port.quirks & UPQ_NO_TXEN_TEST)
2326 		goto dont_test_tx_en;
2327 
2328 	/*
2329 	 * Do a quick test to see if we receive an interrupt when we enable
2330 	 * the TX irq.
2331 	 */
2332 	serial_port_out(port, UART_IER, UART_IER_THRI);
2333 	lsr = serial_port_in(port, UART_LSR);
2334 	iir = serial_port_in(port, UART_IIR);
2335 	serial_port_out(port, UART_IER, 0);
2336 
2337 	if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2338 		if (!(up->bugs & UART_BUG_TXEN)) {
2339 			up->bugs |= UART_BUG_TXEN;
2340 			pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2341 				 serial_index(port));
2342 		}
2343 	} else {
2344 		up->bugs &= ~UART_BUG_TXEN;
2345 	}
2346 
2347 dont_test_tx_en:
2348 	spin_unlock_irqrestore(&port->lock, flags);
2349 
2350 	/*
2351 	 * Clear the interrupt registers again for luck, and clear the
2352 	 * saved flags to avoid getting false values from polling
2353 	 * routines or the previous session.
2354 	 */
2355 	serial_port_in(port, UART_LSR);
2356 	serial_port_in(port, UART_RX);
2357 	serial_port_in(port, UART_IIR);
2358 	serial_port_in(port, UART_MSR);
2359 	if ((port->type == PORT_XR17V35X) || (port->type == PORT_XR17D15X))
2360 		serial_port_in(port, UART_EXAR_INT0);
2361 	up->lsr_saved_flags = 0;
2362 	up->msr_saved_flags = 0;
2363 
2364 	/*
2365 	 * Request DMA channels for both RX and TX.
2366 	 */
2367 	if (up->dma) {
2368 		retval = serial8250_request_dma(up);
2369 		if (retval) {
2370 			pr_warn_ratelimited("ttyS%d - failed to request DMA\n",
2371 					    serial_index(port));
2372 			up->dma = NULL;
2373 		}
2374 	}
2375 
2376 	/*
2377 	 * Set the IER shadow for rx interrupts but defer actual interrupt
2378 	 * enable until after the FIFOs are enabled; otherwise, an already-
2379 	 * active sender can swamp the interrupt handler with "too much work".
2380 	 */
2381 	up->ier = UART_IER_RLSI | UART_IER_RDI;
2382 
2383 	if (port->flags & UPF_FOURPORT) {
2384 		unsigned int icp;
2385 		/*
2386 		 * Enable interrupts on the AST Fourport board
2387 		 */
2388 		icp = (port->iobase & 0xfe0) | 0x01f;
2389 		outb_p(0x80, icp);
2390 		inb_p(icp);
2391 	}
2392 	retval = 0;
2393 out:
2394 	serial8250_rpm_put(up);
2395 	return retval;
2396 }
2397 EXPORT_SYMBOL_GPL(serial8250_do_startup);
2398 
2399 static int serial8250_startup(struct uart_port *port)
2400 {
2401 	if (port->startup)
2402 		return port->startup(port);
2403 	return serial8250_do_startup(port);
2404 }
2405 
2406 void serial8250_do_shutdown(struct uart_port *port)
2407 {
2408 	struct uart_8250_port *up = up_to_u8250p(port);
2409 	unsigned long flags;
2410 
2411 	serial8250_rpm_get(up);
2412 	/*
2413 	 * Disable interrupts from this port
2414 	 */
2415 	spin_lock_irqsave(&port->lock, flags);
2416 	up->ier = 0;
2417 	serial_port_out(port, UART_IER, 0);
2418 	spin_unlock_irqrestore(&port->lock, flags);
2419 
2420 	synchronize_irq(port->irq);
2421 
2422 	if (up->dma)
2423 		serial8250_release_dma(up);
2424 
2425 	spin_lock_irqsave(&port->lock, flags);
2426 	if (port->flags & UPF_FOURPORT) {
2427 		/* reset interrupts on the AST Fourport board */
2428 		inb((port->iobase & 0xfe0) | 0x1f);
2429 		port->mctrl |= TIOCM_OUT1;
2430 	} else
2431 		port->mctrl &= ~TIOCM_OUT2;
2432 
2433 	serial8250_set_mctrl(port, port->mctrl);
2434 	spin_unlock_irqrestore(&port->lock, flags);
2435 
2436 	/*
2437 	 * Disable break condition and FIFOs
2438 	 */
2439 	serial_port_out(port, UART_LCR,
2440 			serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
2441 	serial8250_clear_fifos(up);
2442 
2443 #ifdef CONFIG_SERIAL_8250_RSA
2444 	/*
2445 	 * Reset the RSA board back to 115kbps compat mode.
2446 	 */
2447 	disable_rsa(up);
2448 #endif
2449 
2450 	/*
2451 	 * Read data port to reset things, and then unlink from
2452 	 * the IRQ chain.
2453 	 */
2454 	serial_port_in(port, UART_RX);
2455 	serial8250_rpm_put(up);
2456 
2457 	up->ops->release_irq(up);
2458 }
2459 EXPORT_SYMBOL_GPL(serial8250_do_shutdown);
2460 
2461 static void serial8250_shutdown(struct uart_port *port)
2462 {
2463 	if (port->shutdown)
2464 		port->shutdown(port);
2465 	else
2466 		serial8250_do_shutdown(port);
2467 }
2468 
2469 /*
2470  * XR17V35x UARTs have an extra fractional divisor register (DLD)
2471  * Calculate divisor with extra 4-bit fractional portion
2472  */
2473 static unsigned int xr17v35x_get_divisor(struct uart_8250_port *up,
2474 					 unsigned int baud,
2475 					 unsigned int *frac)
2476 {
2477 	struct uart_port *port = &up->port;
2478 	unsigned int quot_16;
2479 
2480 	quot_16 = DIV_ROUND_CLOSEST(port->uartclk, baud);
2481 	*frac = quot_16 & 0x0f;
2482 
2483 	return quot_16 >> 4;
2484 }
2485 
2486 static unsigned int serial8250_get_divisor(struct uart_8250_port *up,
2487 					   unsigned int baud,
2488 					   unsigned int *frac)
2489 {
2490 	struct uart_port *port = &up->port;
2491 	unsigned int quot;
2492 
2493 	/*
2494 	 * Handle magic divisors for baud rates above baud_base on
2495 	 * SMSC SuperIO chips.
2496 	 *
2497 	 */
2498 	if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2499 	    baud == (port->uartclk/4))
2500 		quot = 0x8001;
2501 	else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2502 		 baud == (port->uartclk/8))
2503 		quot = 0x8002;
2504 	else if (up->port.type == PORT_XR17V35X)
2505 		quot = xr17v35x_get_divisor(up, baud, frac);
2506 	else
2507 		quot = uart_get_divisor(port, baud);
2508 
2509 	/*
2510 	 * Oxford Semi 952 rev B workaround
2511 	 */
2512 	if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2513 		quot++;
2514 
2515 	return quot;
2516 }
2517 
2518 static unsigned char serial8250_compute_lcr(struct uart_8250_port *up,
2519 					    tcflag_t c_cflag)
2520 {
2521 	unsigned char cval;
2522 
2523 	switch (c_cflag & CSIZE) {
2524 	case CS5:
2525 		cval = UART_LCR_WLEN5;
2526 		break;
2527 	case CS6:
2528 		cval = UART_LCR_WLEN6;
2529 		break;
2530 	case CS7:
2531 		cval = UART_LCR_WLEN7;
2532 		break;
2533 	default:
2534 	case CS8:
2535 		cval = UART_LCR_WLEN8;
2536 		break;
2537 	}
2538 
2539 	if (c_cflag & CSTOPB)
2540 		cval |= UART_LCR_STOP;
2541 	if (c_cflag & PARENB) {
2542 		cval |= UART_LCR_PARITY;
2543 		if (up->bugs & UART_BUG_PARITY)
2544 			up->fifo_bug = true;
2545 	}
2546 	if (!(c_cflag & PARODD))
2547 		cval |= UART_LCR_EPAR;
2548 #ifdef CMSPAR
2549 	if (c_cflag & CMSPAR)
2550 		cval |= UART_LCR_SPAR;
2551 #endif
2552 
2553 	return cval;
2554 }
2555 
2556 static void serial8250_set_divisor(struct uart_port *port, unsigned int baud,
2557 			    unsigned int quot, unsigned int quot_frac)
2558 {
2559 	struct uart_8250_port *up = up_to_u8250p(port);
2560 
2561 	/* Workaround to enable 115200 baud on OMAP1510 internal ports */
2562 	if (is_omap1510_8250(up)) {
2563 		if (baud == 115200) {
2564 			quot = 1;
2565 			serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
2566 		} else
2567 			serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
2568 	}
2569 
2570 	/*
2571 	 * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
2572 	 * otherwise just set DLAB
2573 	 */
2574 	if (up->capabilities & UART_NATSEMI)
2575 		serial_port_out(port, UART_LCR, 0xe0);
2576 	else
2577 		serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
2578 
2579 	serial_dl_write(up, quot);
2580 
2581 	/* XR17V35x UARTs have an extra fractional divisor register (DLD) */
2582 	if (up->port.type == PORT_XR17V35X) {
2583 		/* Preserve bits not related to baudrate; DLD[7:4]. */
2584 		quot_frac |= serial_port_in(port, 0x2) & 0xf0;
2585 		serial_port_out(port, 0x2, quot_frac);
2586 	}
2587 }
2588 
2589 static unsigned int serial8250_get_baud_rate(struct uart_port *port,
2590 					     struct ktermios *termios,
2591 					     struct ktermios *old)
2592 {
2593 	/*
2594 	 * Ask the core to calculate the divisor for us.
2595 	 * Allow 1% tolerance at the upper limit so uart clks marginally
2596 	 * slower than nominal still match standard baud rates without
2597 	 * causing transmission errors.
2598 	 */
2599 	return uart_get_baud_rate(port, termios, old,
2600 				  port->uartclk / 16 / UART_DIV_MAX,
2601 				  port->uartclk);
2602 }
2603 
2604 void
2605 serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2606 			  struct ktermios *old)
2607 {
2608 	struct uart_8250_port *up = up_to_u8250p(port);
2609 	unsigned char cval;
2610 	unsigned long flags;
2611 	unsigned int baud, quot, frac = 0;
2612 
2613 	if (up->capabilities & UART_CAP_MINI) {
2614 		termios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CMSPAR);
2615 		if ((termios->c_cflag & CSIZE) == CS5 ||
2616 		    (termios->c_cflag & CSIZE) == CS6)
2617 			termios->c_cflag = (termios->c_cflag & ~CSIZE) | CS7;
2618 	}
2619 	cval = serial8250_compute_lcr(up, termios->c_cflag);
2620 
2621 	baud = serial8250_get_baud_rate(port, termios, old);
2622 	quot = serial8250_get_divisor(up, baud, &frac);
2623 
2624 	/*
2625 	 * Ok, we're now changing the port state.  Do it with
2626 	 * interrupts disabled.
2627 	 */
2628 	serial8250_rpm_get(up);
2629 	spin_lock_irqsave(&port->lock, flags);
2630 
2631 	up->lcr = cval;					/* Save computed LCR */
2632 
2633 	if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
2634 		/* NOTE: If fifo_bug is not set, a user can set RX_trigger. */
2635 		if ((baud < 2400 && !up->dma) || up->fifo_bug) {
2636 			up->fcr &= ~UART_FCR_TRIGGER_MASK;
2637 			up->fcr |= UART_FCR_TRIGGER_1;
2638 		}
2639 	}
2640 
2641 	/*
2642 	 * MCR-based auto flow control.  When AFE is enabled, RTS will be
2643 	 * deasserted when the receive FIFO contains more characters than
2644 	 * the trigger, or the MCR RTS bit is cleared.
2645 	 */
2646 	if (up->capabilities & UART_CAP_AFE) {
2647 		up->mcr &= ~UART_MCR_AFE;
2648 		if (termios->c_cflag & CRTSCTS)
2649 			up->mcr |= UART_MCR_AFE;
2650 	}
2651 
2652 	/*
2653 	 * Update the per-port timeout.
2654 	 */
2655 	uart_update_timeout(port, termios->c_cflag, baud);
2656 
2657 	port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2658 	if (termios->c_iflag & INPCK)
2659 		port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2660 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2661 		port->read_status_mask |= UART_LSR_BI;
2662 
2663 	/*
2664 	 * Characteres to ignore
2665 	 */
2666 	port->ignore_status_mask = 0;
2667 	if (termios->c_iflag & IGNPAR)
2668 		port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2669 	if (termios->c_iflag & IGNBRK) {
2670 		port->ignore_status_mask |= UART_LSR_BI;
2671 		/*
2672 		 * If we're ignoring parity and break indicators,
2673 		 * ignore overruns too (for real raw support).
2674 		 */
2675 		if (termios->c_iflag & IGNPAR)
2676 			port->ignore_status_mask |= UART_LSR_OE;
2677 	}
2678 
2679 	/*
2680 	 * ignore all characters if CREAD is not set
2681 	 */
2682 	if ((termios->c_cflag & CREAD) == 0)
2683 		port->ignore_status_mask |= UART_LSR_DR;
2684 
2685 	/*
2686 	 * CTS flow control flag and modem status interrupts
2687 	 */
2688 	up->ier &= ~UART_IER_MSI;
2689 	if (!(up->bugs & UART_BUG_NOMSR) &&
2690 			UART_ENABLE_MS(&up->port, termios->c_cflag))
2691 		up->ier |= UART_IER_MSI;
2692 	if (up->capabilities & UART_CAP_UUE)
2693 		up->ier |= UART_IER_UUE;
2694 	if (up->capabilities & UART_CAP_RTOIE)
2695 		up->ier |= UART_IER_RTOIE;
2696 
2697 	serial_port_out(port, UART_IER, up->ier);
2698 
2699 	if (up->capabilities & UART_CAP_EFR) {
2700 		unsigned char efr = 0;
2701 		/*
2702 		 * TI16C752/Startech hardware flow control.  FIXME:
2703 		 * - TI16C752 requires control thresholds to be set.
2704 		 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2705 		 */
2706 		if (termios->c_cflag & CRTSCTS)
2707 			efr |= UART_EFR_CTS;
2708 
2709 		serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2710 		if (port->flags & UPF_EXAR_EFR)
2711 			serial_port_out(port, UART_XR_EFR, efr);
2712 		else
2713 			serial_port_out(port, UART_EFR, efr);
2714 	}
2715 
2716 	serial8250_set_divisor(port, baud, quot, frac);
2717 
2718 	/*
2719 	 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2720 	 * is written without DLAB set, this mode will be disabled.
2721 	 */
2722 	if (port->type == PORT_16750)
2723 		serial_port_out(port, UART_FCR, up->fcr);
2724 
2725 	serial_port_out(port, UART_LCR, up->lcr);	/* reset DLAB */
2726 	if (port->type != PORT_16750) {
2727 		/* emulated UARTs (Lucent Venus 167x) need two steps */
2728 		if (up->fcr & UART_FCR_ENABLE_FIFO)
2729 			serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
2730 		serial_port_out(port, UART_FCR, up->fcr);	/* set fcr */
2731 	}
2732 	serial8250_set_mctrl(port, port->mctrl);
2733 	spin_unlock_irqrestore(&port->lock, flags);
2734 	serial8250_rpm_put(up);
2735 
2736 	/* Don't rewrite B0 */
2737 	if (tty_termios_baud_rate(termios))
2738 		tty_termios_encode_baud_rate(termios, baud, baud);
2739 }
2740 EXPORT_SYMBOL(serial8250_do_set_termios);
2741 
2742 static void
2743 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2744 		       struct ktermios *old)
2745 {
2746 	if (port->set_termios)
2747 		port->set_termios(port, termios, old);
2748 	else
2749 		serial8250_do_set_termios(port, termios, old);
2750 }
2751 
2752 void serial8250_do_set_ldisc(struct uart_port *port, struct ktermios *termios)
2753 {
2754 	if (termios->c_line == N_PPS) {
2755 		port->flags |= UPF_HARDPPS_CD;
2756 		spin_lock_irq(&port->lock);
2757 		serial8250_enable_ms(port);
2758 		spin_unlock_irq(&port->lock);
2759 	} else {
2760 		port->flags &= ~UPF_HARDPPS_CD;
2761 		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2762 			spin_lock_irq(&port->lock);
2763 			serial8250_disable_ms(port);
2764 			spin_unlock_irq(&port->lock);
2765 		}
2766 	}
2767 }
2768 EXPORT_SYMBOL_GPL(serial8250_do_set_ldisc);
2769 
2770 static void
2771 serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios)
2772 {
2773 	if (port->set_ldisc)
2774 		port->set_ldisc(port, termios);
2775 	else
2776 		serial8250_do_set_ldisc(port, termios);
2777 }
2778 
2779 void serial8250_do_pm(struct uart_port *port, unsigned int state,
2780 		      unsigned int oldstate)
2781 {
2782 	struct uart_8250_port *p = up_to_u8250p(port);
2783 
2784 	serial8250_set_sleep(p, state != 0);
2785 }
2786 EXPORT_SYMBOL(serial8250_do_pm);
2787 
2788 static void
2789 serial8250_pm(struct uart_port *port, unsigned int state,
2790 	      unsigned int oldstate)
2791 {
2792 	if (port->pm)
2793 		port->pm(port, state, oldstate);
2794 	else
2795 		serial8250_do_pm(port, state, oldstate);
2796 }
2797 
2798 static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2799 {
2800 	if (pt->port.mapsize)
2801 		return pt->port.mapsize;
2802 	if (pt->port.iotype == UPIO_AU) {
2803 		if (pt->port.type == PORT_RT2880)
2804 			return 0x100;
2805 		return 0x1000;
2806 	}
2807 	if (is_omap1_8250(pt))
2808 		return 0x16 << pt->port.regshift;
2809 
2810 	return 8 << pt->port.regshift;
2811 }
2812 
2813 /*
2814  * Resource handling.
2815  */
2816 static int serial8250_request_std_resource(struct uart_8250_port *up)
2817 {
2818 	unsigned int size = serial8250_port_size(up);
2819 	struct uart_port *port = &up->port;
2820 	int ret = 0;
2821 
2822 	switch (port->iotype) {
2823 	case UPIO_AU:
2824 	case UPIO_TSI:
2825 	case UPIO_MEM32:
2826 	case UPIO_MEM32BE:
2827 	case UPIO_MEM16:
2828 	case UPIO_MEM:
2829 		if (!port->mapbase)
2830 			break;
2831 
2832 		if (!request_mem_region(port->mapbase, size, "serial")) {
2833 			ret = -EBUSY;
2834 			break;
2835 		}
2836 
2837 		if (port->flags & UPF_IOREMAP) {
2838 			port->membase = ioremap_nocache(port->mapbase, size);
2839 			if (!port->membase) {
2840 				release_mem_region(port->mapbase, size);
2841 				ret = -ENOMEM;
2842 			}
2843 		}
2844 		break;
2845 
2846 	case UPIO_HUB6:
2847 	case UPIO_PORT:
2848 		if (!request_region(port->iobase, size, "serial"))
2849 			ret = -EBUSY;
2850 		break;
2851 	}
2852 	return ret;
2853 }
2854 
2855 static void serial8250_release_std_resource(struct uart_8250_port *up)
2856 {
2857 	unsigned int size = serial8250_port_size(up);
2858 	struct uart_port *port = &up->port;
2859 
2860 	switch (port->iotype) {
2861 	case UPIO_AU:
2862 	case UPIO_TSI:
2863 	case UPIO_MEM32:
2864 	case UPIO_MEM32BE:
2865 	case UPIO_MEM16:
2866 	case UPIO_MEM:
2867 		if (!port->mapbase)
2868 			break;
2869 
2870 		if (port->flags & UPF_IOREMAP) {
2871 			iounmap(port->membase);
2872 			port->membase = NULL;
2873 		}
2874 
2875 		release_mem_region(port->mapbase, size);
2876 		break;
2877 
2878 	case UPIO_HUB6:
2879 	case UPIO_PORT:
2880 		release_region(port->iobase, size);
2881 		break;
2882 	}
2883 }
2884 
2885 static void serial8250_release_port(struct uart_port *port)
2886 {
2887 	struct uart_8250_port *up = up_to_u8250p(port);
2888 
2889 	serial8250_release_std_resource(up);
2890 }
2891 
2892 static int serial8250_request_port(struct uart_port *port)
2893 {
2894 	struct uart_8250_port *up = up_to_u8250p(port);
2895 
2896 	return serial8250_request_std_resource(up);
2897 }
2898 
2899 static int fcr_get_rxtrig_bytes(struct uart_8250_port *up)
2900 {
2901 	const struct serial8250_config *conf_type = &uart_config[up->port.type];
2902 	unsigned char bytes;
2903 
2904 	bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)];
2905 
2906 	return bytes ? bytes : -EOPNOTSUPP;
2907 }
2908 
2909 static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes)
2910 {
2911 	const struct serial8250_config *conf_type = &uart_config[up->port.type];
2912 	int i;
2913 
2914 	if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)])
2915 		return -EOPNOTSUPP;
2916 
2917 	for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) {
2918 		if (bytes < conf_type->rxtrig_bytes[i])
2919 			/* Use the nearest lower value */
2920 			return (--i) << UART_FCR_R_TRIG_SHIFT;
2921 	}
2922 
2923 	return UART_FCR_R_TRIG_11;
2924 }
2925 
2926 static int do_get_rxtrig(struct tty_port *port)
2927 {
2928 	struct uart_state *state = container_of(port, struct uart_state, port);
2929 	struct uart_port *uport = state->uart_port;
2930 	struct uart_8250_port *up = up_to_u8250p(uport);
2931 
2932 	if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
2933 		return -EINVAL;
2934 
2935 	return fcr_get_rxtrig_bytes(up);
2936 }
2937 
2938 static int do_serial8250_get_rxtrig(struct tty_port *port)
2939 {
2940 	int rxtrig_bytes;
2941 
2942 	mutex_lock(&port->mutex);
2943 	rxtrig_bytes = do_get_rxtrig(port);
2944 	mutex_unlock(&port->mutex);
2945 
2946 	return rxtrig_bytes;
2947 }
2948 
2949 static ssize_t serial8250_get_attr_rx_trig_bytes(struct device *dev,
2950 	struct device_attribute *attr, char *buf)
2951 {
2952 	struct tty_port *port = dev_get_drvdata(dev);
2953 	int rxtrig_bytes;
2954 
2955 	rxtrig_bytes = do_serial8250_get_rxtrig(port);
2956 	if (rxtrig_bytes < 0)
2957 		return rxtrig_bytes;
2958 
2959 	return snprintf(buf, PAGE_SIZE, "%d\n", rxtrig_bytes);
2960 }
2961 
2962 static int do_set_rxtrig(struct tty_port *port, unsigned char bytes)
2963 {
2964 	struct uart_state *state = container_of(port, struct uart_state, port);
2965 	struct uart_port *uport = state->uart_port;
2966 	struct uart_8250_port *up = up_to_u8250p(uport);
2967 	int rxtrig;
2968 
2969 	if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1 ||
2970 	    up->fifo_bug)
2971 		return -EINVAL;
2972 
2973 	rxtrig = bytes_to_fcr_rxtrig(up, bytes);
2974 	if (rxtrig < 0)
2975 		return rxtrig;
2976 
2977 	serial8250_clear_fifos(up);
2978 	up->fcr &= ~UART_FCR_TRIGGER_MASK;
2979 	up->fcr |= (unsigned char)rxtrig;
2980 	serial_out(up, UART_FCR, up->fcr);
2981 	return 0;
2982 }
2983 
2984 static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes)
2985 {
2986 	int ret;
2987 
2988 	mutex_lock(&port->mutex);
2989 	ret = do_set_rxtrig(port, bytes);
2990 	mutex_unlock(&port->mutex);
2991 
2992 	return ret;
2993 }
2994 
2995 static ssize_t serial8250_set_attr_rx_trig_bytes(struct device *dev,
2996 	struct device_attribute *attr, const char *buf, size_t count)
2997 {
2998 	struct tty_port *port = dev_get_drvdata(dev);
2999 	unsigned char bytes;
3000 	int ret;
3001 
3002 	if (!count)
3003 		return -EINVAL;
3004 
3005 	ret = kstrtou8(buf, 10, &bytes);
3006 	if (ret < 0)
3007 		return ret;
3008 
3009 	ret = do_serial8250_set_rxtrig(port, bytes);
3010 	if (ret < 0)
3011 		return ret;
3012 
3013 	return count;
3014 }
3015 
3016 static DEVICE_ATTR(rx_trig_bytes, S_IRUSR | S_IWUSR | S_IRGRP,
3017 		   serial8250_get_attr_rx_trig_bytes,
3018 		   serial8250_set_attr_rx_trig_bytes);
3019 
3020 static struct attribute *serial8250_dev_attrs[] = {
3021 	&dev_attr_rx_trig_bytes.attr,
3022 	NULL,
3023 	};
3024 
3025 static struct attribute_group serial8250_dev_attr_group = {
3026 	.attrs = serial8250_dev_attrs,
3027 	};
3028 
3029 static void register_dev_spec_attr_grp(struct uart_8250_port *up)
3030 {
3031 	const struct serial8250_config *conf_type = &uart_config[up->port.type];
3032 
3033 	if (conf_type->rxtrig_bytes[0])
3034 		up->port.attr_group = &serial8250_dev_attr_group;
3035 }
3036 
3037 static void serial8250_config_port(struct uart_port *port, int flags)
3038 {
3039 	struct uart_8250_port *up = up_to_u8250p(port);
3040 	int ret;
3041 
3042 	/*
3043 	 * Find the region that we can probe for.  This in turn
3044 	 * tells us whether we can probe for the type of port.
3045 	 */
3046 	ret = serial8250_request_std_resource(up);
3047 	if (ret < 0)
3048 		return;
3049 
3050 	if (port->iotype != up->cur_iotype)
3051 		set_io_from_upio(port);
3052 
3053 	if (flags & UART_CONFIG_TYPE)
3054 		autoconfig(up);
3055 
3056 	/* if access method is AU, it is a 16550 with a quirk */
3057 	if (port->type == PORT_16550A && port->iotype == UPIO_AU)
3058 		up->bugs |= UART_BUG_NOMSR;
3059 
3060 	/* HW bugs may trigger IRQ while IIR == NO_INT */
3061 	if (port->type == PORT_TEGRA)
3062 		up->bugs |= UART_BUG_NOMSR;
3063 
3064 	if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
3065 		autoconfig_irq(up);
3066 
3067 	if (port->type == PORT_UNKNOWN)
3068 		serial8250_release_std_resource(up);
3069 
3070 	/* Fixme: probably not the best place for this */
3071 	if ((port->type == PORT_XR17V35X) ||
3072 	   (port->type == PORT_XR17D15X))
3073 		port->handle_irq = exar_handle_irq;
3074 
3075 	register_dev_spec_attr_grp(up);
3076 	up->fcr = uart_config[up->port.type].fcr;
3077 }
3078 
3079 static int
3080 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
3081 {
3082 	if (ser->irq >= nr_irqs || ser->irq < 0 ||
3083 	    ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
3084 	    ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
3085 	    ser->type == PORT_STARTECH)
3086 		return -EINVAL;
3087 	return 0;
3088 }
3089 
3090 static const char *serial8250_type(struct uart_port *port)
3091 {
3092 	int type = port->type;
3093 
3094 	if (type >= ARRAY_SIZE(uart_config))
3095 		type = 0;
3096 	return uart_config[type].name;
3097 }
3098 
3099 static const struct uart_ops serial8250_pops = {
3100 	.tx_empty	= serial8250_tx_empty,
3101 	.set_mctrl	= serial8250_set_mctrl,
3102 	.get_mctrl	= serial8250_get_mctrl,
3103 	.stop_tx	= serial8250_stop_tx,
3104 	.start_tx	= serial8250_start_tx,
3105 	.throttle	= serial8250_throttle,
3106 	.unthrottle	= serial8250_unthrottle,
3107 	.stop_rx	= serial8250_stop_rx,
3108 	.enable_ms	= serial8250_enable_ms,
3109 	.break_ctl	= serial8250_break_ctl,
3110 	.startup	= serial8250_startup,
3111 	.shutdown	= serial8250_shutdown,
3112 	.set_termios	= serial8250_set_termios,
3113 	.set_ldisc	= serial8250_set_ldisc,
3114 	.pm		= serial8250_pm,
3115 	.type		= serial8250_type,
3116 	.release_port	= serial8250_release_port,
3117 	.request_port	= serial8250_request_port,
3118 	.config_port	= serial8250_config_port,
3119 	.verify_port	= serial8250_verify_port,
3120 #ifdef CONFIG_CONSOLE_POLL
3121 	.poll_get_char = serial8250_get_poll_char,
3122 	.poll_put_char = serial8250_put_poll_char,
3123 #endif
3124 };
3125 
3126 void serial8250_init_port(struct uart_8250_port *up)
3127 {
3128 	struct uart_port *port = &up->port;
3129 
3130 	spin_lock_init(&port->lock);
3131 	port->ops = &serial8250_pops;
3132 
3133 	up->cur_iotype = 0xFF;
3134 }
3135 EXPORT_SYMBOL_GPL(serial8250_init_port);
3136 
3137 void serial8250_set_defaults(struct uart_8250_port *up)
3138 {
3139 	struct uart_port *port = &up->port;
3140 
3141 	if (up->port.flags & UPF_FIXED_TYPE) {
3142 		unsigned int type = up->port.type;
3143 
3144 		if (!up->port.fifosize)
3145 			up->port.fifosize = uart_config[type].fifo_size;
3146 		if (!up->tx_loadsz)
3147 			up->tx_loadsz = uart_config[type].tx_loadsz;
3148 		if (!up->capabilities)
3149 			up->capabilities = uart_config[type].flags;
3150 	}
3151 
3152 	set_io_from_upio(port);
3153 
3154 	/* default dma handlers */
3155 	if (up->dma) {
3156 		if (!up->dma->tx_dma)
3157 			up->dma->tx_dma = serial8250_tx_dma;
3158 		if (!up->dma->rx_dma)
3159 			up->dma->rx_dma = serial8250_rx_dma;
3160 	}
3161 }
3162 EXPORT_SYMBOL_GPL(serial8250_set_defaults);
3163 
3164 #ifdef CONFIG_SERIAL_8250_CONSOLE
3165 
3166 static void serial8250_console_putchar(struct uart_port *port, int ch)
3167 {
3168 	struct uart_8250_port *up = up_to_u8250p(port);
3169 
3170 	wait_for_xmitr(up, UART_LSR_THRE);
3171 	serial_port_out(port, UART_TX, ch);
3172 }
3173 
3174 /*
3175  *	Restore serial console when h/w power-off detected
3176  */
3177 static void serial8250_console_restore(struct uart_8250_port *up)
3178 {
3179 	struct uart_port *port = &up->port;
3180 	struct ktermios termios;
3181 	unsigned int baud, quot, frac = 0;
3182 
3183 	termios.c_cflag = port->cons->cflag;
3184 	if (port->state->port.tty && termios.c_cflag == 0)
3185 		termios.c_cflag = port->state->port.tty->termios.c_cflag;
3186 
3187 	baud = serial8250_get_baud_rate(port, &termios, NULL);
3188 	quot = serial8250_get_divisor(up, baud, &frac);
3189 
3190 	serial8250_set_divisor(port, baud, quot, frac);
3191 	serial_port_out(port, UART_LCR, up->lcr);
3192 	serial8250_out_MCR(up, UART_MCR_DTR | UART_MCR_RTS);
3193 }
3194 
3195 /*
3196  *	Print a string to the serial port trying not to disturb
3197  *	any possible real use of the port...
3198  *
3199  *	The console_lock must be held when we get here.
3200  */
3201 void serial8250_console_write(struct uart_8250_port *up, const char *s,
3202 			      unsigned int count)
3203 {
3204 	struct uart_port *port = &up->port;
3205 	unsigned long flags;
3206 	unsigned int ier;
3207 	int locked = 1;
3208 
3209 	touch_nmi_watchdog();
3210 
3211 	serial8250_rpm_get(up);
3212 
3213 	if (port->sysrq)
3214 		locked = 0;
3215 	else if (oops_in_progress)
3216 		locked = spin_trylock_irqsave(&port->lock, flags);
3217 	else
3218 		spin_lock_irqsave(&port->lock, flags);
3219 
3220 	/*
3221 	 *	First save the IER then disable the interrupts
3222 	 */
3223 	ier = serial_port_in(port, UART_IER);
3224 
3225 	if (up->capabilities & UART_CAP_UUE)
3226 		serial_port_out(port, UART_IER, UART_IER_UUE);
3227 	else
3228 		serial_port_out(port, UART_IER, 0);
3229 
3230 	/* check scratch reg to see if port powered off during system sleep */
3231 	if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) {
3232 		serial8250_console_restore(up);
3233 		up->canary = 0;
3234 	}
3235 
3236 	uart_console_write(port, s, count, serial8250_console_putchar);
3237 
3238 	/*
3239 	 *	Finally, wait for transmitter to become empty
3240 	 *	and restore the IER
3241 	 */
3242 	wait_for_xmitr(up, BOTH_EMPTY);
3243 	serial_port_out(port, UART_IER, ier);
3244 
3245 	/*
3246 	 *	The receive handling will happen properly because the
3247 	 *	receive ready bit will still be set; it is not cleared
3248 	 *	on read.  However, modem control will not, we must
3249 	 *	call it if we have saved something in the saved flags
3250 	 *	while processing with interrupts off.
3251 	 */
3252 	if (up->msr_saved_flags)
3253 		serial8250_modem_status(up);
3254 
3255 	if (locked)
3256 		spin_unlock_irqrestore(&port->lock, flags);
3257 	serial8250_rpm_put(up);
3258 }
3259 
3260 static unsigned int probe_baud(struct uart_port *port)
3261 {
3262 	unsigned char lcr, dll, dlm;
3263 	unsigned int quot;
3264 
3265 	lcr = serial_port_in(port, UART_LCR);
3266 	serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB);
3267 	dll = serial_port_in(port, UART_DLL);
3268 	dlm = serial_port_in(port, UART_DLM);
3269 	serial_port_out(port, UART_LCR, lcr);
3270 
3271 	quot = (dlm << 8) | dll;
3272 	return (port->uartclk / 16) / quot;
3273 }
3274 
3275 int serial8250_console_setup(struct uart_port *port, char *options, bool probe)
3276 {
3277 	int baud = 9600;
3278 	int bits = 8;
3279 	int parity = 'n';
3280 	int flow = 'n';
3281 
3282 	if (!port->iobase && !port->membase)
3283 		return -ENODEV;
3284 
3285 	if (options)
3286 		uart_parse_options(options, &baud, &parity, &bits, &flow);
3287 	else if (probe)
3288 		baud = probe_baud(port);
3289 
3290 	return uart_set_options(port, port->cons, baud, parity, bits, flow);
3291 }
3292 
3293 #endif /* CONFIG_SERIAL_8250_CONSOLE */
3294 
3295 MODULE_LICENSE("GPL");
3296