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