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