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