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