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