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