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