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