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