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