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