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