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