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