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