1 /*
2  *  Driver for 8250/16550-type serial ports
3  *
4  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5  *
6  *  Copyright (C) 2001 Russell King.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * A note about mapbase / membase
14  *
15  *  mapbase is the physical address of the IO port.
16  *  membase is an 'ioremapped' cookie.
17  */
18 
19 #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
20 #define SUPPORT_SYSRQ
21 #endif
22 
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/ioport.h>
26 #include <linux/init.h>
27 #include <linux/console.h>
28 #include <linux/sysrq.h>
29 #include <linux/delay.h>
30 #include <linux/platform_device.h>
31 #include <linux/tty.h>
32 #include <linux/ratelimit.h>
33 #include <linux/tty_flip.h>
34 #include <linux/serial.h>
35 #include <linux/serial_8250.h>
36 #include <linux/nmi.h>
37 #include <linux/mutex.h>
38 #include <linux/slab.h>
39 #include <linux/uaccess.h>
40 #include <linux/pm_runtime.h>
41 #ifdef CONFIG_SPARC
42 #include <linux/sunserialcore.h>
43 #endif
44 
45 #include <asm/io.h>
46 #include <asm/irq.h>
47 
48 #include "8250.h"
49 
50 /*
51  * Configuration:
52  *   share_irqs - whether we pass IRQF_SHARED to request_irq().  This option
53  *                is unsafe when used on edge-triggered interrupts.
54  */
55 static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
56 
57 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
58 
59 static struct uart_driver serial8250_reg;
60 
serial_index(struct uart_port * port)61 static int serial_index(struct uart_port *port)
62 {
63 	return port->minor - 64;
64 }
65 
66 static unsigned int skip_txen_test; /* force skip of txen test at init time */
67 
68 /*
69  * Debugging.
70  */
71 #if 0
72 #define DEBUG_AUTOCONF(fmt...)	printk(fmt)
73 #else
74 #define DEBUG_AUTOCONF(fmt...)	do { } while (0)
75 #endif
76 
77 #if 0
78 #define DEBUG_INTR(fmt...)	printk(fmt)
79 #else
80 #define DEBUG_INTR(fmt...)	do { } while (0)
81 #endif
82 
83 #define PASS_LIMIT	512
84 
85 #define BOTH_EMPTY 	(UART_LSR_TEMT | UART_LSR_THRE)
86 
87 
88 #ifdef CONFIG_SERIAL_8250_DETECT_IRQ
89 #define CONFIG_SERIAL_DETECT_IRQ 1
90 #endif
91 #ifdef CONFIG_SERIAL_8250_MANY_PORTS
92 #define CONFIG_SERIAL_MANY_PORTS 1
93 #endif
94 
95 /*
96  * HUB6 is always on.  This will be removed once the header
97  * files have been cleaned.
98  */
99 #define CONFIG_HUB6 1
100 
101 #include <asm/serial.h>
102 /*
103  * SERIAL_PORT_DFNS tells us about built-in ports that have no
104  * standard enumeration mechanism.   Platforms that can find all
105  * serial ports via mechanisms like ACPI or PCI need not supply it.
106  */
107 #ifndef SERIAL_PORT_DFNS
108 #define SERIAL_PORT_DFNS
109 #endif
110 
111 static const struct old_serial_port old_serial_port[] = {
112 	SERIAL_PORT_DFNS /* defined in asm/serial.h */
113 };
114 
115 #define UART_NR	CONFIG_SERIAL_8250_NR_UARTS
116 
117 #ifdef CONFIG_SERIAL_8250_RSA
118 
119 #define PORT_RSA_MAX 4
120 static unsigned long probe_rsa[PORT_RSA_MAX];
121 static unsigned int probe_rsa_count;
122 #endif /* CONFIG_SERIAL_8250_RSA  */
123 
124 struct irq_info {
125 	struct			hlist_node node;
126 	int			irq;
127 	spinlock_t		lock;	/* Protects list not the hash */
128 	struct list_head	*head;
129 };
130 
131 #define NR_IRQ_HASH		32	/* Can be adjusted later */
132 static struct hlist_head irq_lists[NR_IRQ_HASH];
133 static DEFINE_MUTEX(hash_mutex);	/* Used to walk the hash */
134 
135 /*
136  * Here we define the default xmit fifo size used for each type of UART.
137  */
138 static const struct serial8250_config uart_config[] = {
139 	[PORT_UNKNOWN] = {
140 		.name		= "unknown",
141 		.fifo_size	= 1,
142 		.tx_loadsz	= 1,
143 	},
144 	[PORT_8250] = {
145 		.name		= "8250",
146 		.fifo_size	= 1,
147 		.tx_loadsz	= 1,
148 	},
149 	[PORT_16450] = {
150 		.name		= "16450",
151 		.fifo_size	= 1,
152 		.tx_loadsz	= 1,
153 	},
154 	[PORT_16550] = {
155 		.name		= "16550",
156 		.fifo_size	= 1,
157 		.tx_loadsz	= 1,
158 	},
159 	[PORT_16550A] = {
160 		.name		= "16550A",
161 		.fifo_size	= 16,
162 		.tx_loadsz	= 16,
163 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
164 		.rxtrig_bytes	= {1, 4, 8, 14},
165 		.flags		= UART_CAP_FIFO,
166 	},
167 	[PORT_CIRRUS] = {
168 		.name		= "Cirrus",
169 		.fifo_size	= 1,
170 		.tx_loadsz	= 1,
171 	},
172 	[PORT_16650] = {
173 		.name		= "ST16650",
174 		.fifo_size	= 1,
175 		.tx_loadsz	= 1,
176 		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
177 	},
178 	[PORT_16650V2] = {
179 		.name		= "ST16650V2",
180 		.fifo_size	= 32,
181 		.tx_loadsz	= 16,
182 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
183 				  UART_FCR_T_TRIG_00,
184 		.rxtrig_bytes	= {8, 16, 24, 28},
185 		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
186 	},
187 	[PORT_16750] = {
188 		.name		= "TI16750",
189 		.fifo_size	= 64,
190 		.tx_loadsz	= 64,
191 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
192 				  UART_FCR7_64BYTE,
193 		.rxtrig_bytes	= {1, 16, 32, 56},
194 		.flags		= UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
195 	},
196 	[PORT_STARTECH] = {
197 		.name		= "Startech",
198 		.fifo_size	= 1,
199 		.tx_loadsz	= 1,
200 	},
201 	[PORT_16C950] = {
202 		.name		= "16C950/954",
203 		.fifo_size	= 128,
204 		.tx_loadsz	= 128,
205 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
206 		/* UART_CAP_EFR breaks billionon CF bluetooth card. */
207 		.flags		= UART_CAP_FIFO | UART_CAP_SLEEP,
208 	},
209 	[PORT_16654] = {
210 		.name		= "ST16654",
211 		.fifo_size	= 64,
212 		.tx_loadsz	= 32,
213 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
214 				  UART_FCR_T_TRIG_10,
215 		.rxtrig_bytes	= {8, 16, 56, 60},
216 		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
217 	},
218 	[PORT_16850] = {
219 		.name		= "XR16850",
220 		.fifo_size	= 128,
221 		.tx_loadsz	= 128,
222 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
223 		.flags		= UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
224 	},
225 	[PORT_RSA] = {
226 		.name		= "RSA",
227 		.fifo_size	= 2048,
228 		.tx_loadsz	= 2048,
229 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
230 		.flags		= UART_CAP_FIFO,
231 	},
232 	[PORT_NS16550A] = {
233 		.name		= "NS16550A",
234 		.fifo_size	= 16,
235 		.tx_loadsz	= 16,
236 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
237 		.flags		= UART_CAP_FIFO | UART_NATSEMI,
238 	},
239 	[PORT_XSCALE] = {
240 		.name		= "XScale",
241 		.fifo_size	= 32,
242 		.tx_loadsz	= 32,
243 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
244 		.flags		= UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
245 	},
246 	[PORT_OCTEON] = {
247 		.name		= "OCTEON",
248 		.fifo_size	= 64,
249 		.tx_loadsz	= 64,
250 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
251 		.flags		= UART_CAP_FIFO,
252 	},
253 	[PORT_AR7] = {
254 		.name		= "AR7",
255 		.fifo_size	= 16,
256 		.tx_loadsz	= 16,
257 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
258 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
259 	},
260 	[PORT_U6_16550A] = {
261 		.name		= "U6_16550A",
262 		.fifo_size	= 64,
263 		.tx_loadsz	= 64,
264 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
265 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
266 	},
267 	[PORT_TEGRA] = {
268 		.name		= "Tegra",
269 		.fifo_size	= 32,
270 		.tx_loadsz	= 8,
271 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
272 				  UART_FCR_T_TRIG_01,
273 		.rxtrig_bytes	= {1, 4, 8, 14},
274 		.flags		= UART_CAP_FIFO | UART_CAP_RTOIE,
275 	},
276 	[PORT_XR17D15X] = {
277 		.name		= "XR17D15X",
278 		.fifo_size	= 64,
279 		.tx_loadsz	= 64,
280 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
281 		.flags		= UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
282 				  UART_CAP_SLEEP,
283 	},
284 	[PORT_XR17V35X] = {
285 		.name		= "XR17V35X",
286 		.fifo_size	= 256,
287 		.tx_loadsz	= 256,
288 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11 |
289 				  UART_FCR_T_TRIG_11,
290 		.flags		= UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
291 				  UART_CAP_SLEEP,
292 	},
293 	[PORT_LPC3220] = {
294 		.name		= "LPC3220",
295 		.fifo_size	= 64,
296 		.tx_loadsz	= 32,
297 		.fcr		= UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
298 				  UART_FCR_R_TRIG_00 | UART_FCR_T_TRIG_00,
299 		.flags		= UART_CAP_FIFO,
300 	},
301 	[PORT_BRCM_TRUMANAGE] = {
302 		.name		= "TruManage",
303 		.fifo_size	= 1,
304 		.tx_loadsz	= 1024,
305 		.flags		= UART_CAP_HFIFO,
306 	},
307 	[PORT_8250_CIR] = {
308 		.name		= "CIR port"
309 	},
310 	[PORT_ALTR_16550_F32] = {
311 		.name		= "Altera 16550 FIFO32",
312 		.fifo_size	= 32,
313 		.tx_loadsz	= 32,
314 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
315 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
316 	},
317 	[PORT_ALTR_16550_F64] = {
318 		.name		= "Altera 16550 FIFO64",
319 		.fifo_size	= 64,
320 		.tx_loadsz	= 64,
321 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
322 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
323 	},
324 	[PORT_ALTR_16550_F128] = {
325 		.name		= "Altera 16550 FIFO128",
326 		.fifo_size	= 128,
327 		.tx_loadsz	= 128,
328 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
329 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
330 	},
331 /* tx_loadsz is set to 63-bytes instead of 64-bytes to implement
332 workaround of errata A-008006 which states that tx_loadsz should  be
333 configured less than Maximum supported fifo bytes */
334 	[PORT_16550A_FSL64] = {
335 		.name		= "16550A_FSL64",
336 		.fifo_size	= 64,
337 		.tx_loadsz	= 63,
338 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
339 				  UART_FCR7_64BYTE,
340 		.flags		= UART_CAP_FIFO,
341 	},
342 	[PORT_RT2880] = {
343 		.name		= "Palmchip BK-3103",
344 		.fifo_size	= 16,
345 		.tx_loadsz	= 16,
346 		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
347 		.rxtrig_bytes	= {1, 4, 8, 14},
348 		.flags		= UART_CAP_FIFO,
349 	},
350 };
351 
352 /* Uart divisor latch read */
default_serial_dl_read(struct uart_8250_port * up)353 static int default_serial_dl_read(struct uart_8250_port *up)
354 {
355 	return serial_in(up, UART_DLL) | serial_in(up, UART_DLM) << 8;
356 }
357 
358 /* Uart divisor latch write */
default_serial_dl_write(struct uart_8250_port * up,int value)359 static void default_serial_dl_write(struct uart_8250_port *up, int value)
360 {
361 	serial_out(up, UART_DLL, value & 0xff);
362 	serial_out(up, UART_DLM, value >> 8 & 0xff);
363 }
364 
365 #if defined(CONFIG_MIPS_ALCHEMY) || defined(CONFIG_SERIAL_8250_RT288X)
366 
367 /* Au1x00/RT288x UART hardware has a weird register layout */
368 static const s8 au_io_in_map[8] = {
369 	 0,	/* UART_RX  */
370 	 2,	/* UART_IER */
371 	 3,	/* UART_IIR */
372 	 5,	/* UART_LCR */
373 	 6,	/* UART_MCR */
374 	 7,	/* UART_LSR */
375 	 8,	/* UART_MSR */
376 	-1,	/* UART_SCR (unmapped) */
377 };
378 
379 static const s8 au_io_out_map[8] = {
380 	 1,	/* UART_TX  */
381 	 2,	/* UART_IER */
382 	 4,	/* UART_FCR */
383 	 5,	/* UART_LCR */
384 	 6,	/* UART_MCR */
385 	-1,	/* UART_LSR (unmapped) */
386 	-1,	/* UART_MSR (unmapped) */
387 	-1,	/* UART_SCR (unmapped) */
388 };
389 
au_serial_in(struct uart_port * p,int offset)390 static unsigned int au_serial_in(struct uart_port *p, int offset)
391 {
392 	if (offset >= ARRAY_SIZE(au_io_in_map))
393 		return UINT_MAX;
394 	offset = au_io_in_map[offset];
395 	if (offset < 0)
396 		return UINT_MAX;
397 	return __raw_readl(p->membase + (offset << p->regshift));
398 }
399 
au_serial_out(struct uart_port * p,int offset,int value)400 static void au_serial_out(struct uart_port *p, int offset, int value)
401 {
402 	if (offset >= ARRAY_SIZE(au_io_out_map))
403 		return;
404 	offset = au_io_out_map[offset];
405 	if (offset < 0)
406 		return;
407 	__raw_writel(value, p->membase + (offset << p->regshift));
408 }
409 
410 /* Au1x00 haven't got a standard divisor latch */
au_serial_dl_read(struct uart_8250_port * up)411 static int au_serial_dl_read(struct uart_8250_port *up)
412 {
413 	return __raw_readl(up->port.membase + 0x28);
414 }
415 
au_serial_dl_write(struct uart_8250_port * up,int value)416 static void au_serial_dl_write(struct uart_8250_port *up, int value)
417 {
418 	__raw_writel(value, up->port.membase + 0x28);
419 }
420 
421 #endif
422 
hub6_serial_in(struct uart_port * p,int offset)423 static unsigned int hub6_serial_in(struct uart_port *p, int offset)
424 {
425 	offset = offset << p->regshift;
426 	outb(p->hub6 - 1 + offset, p->iobase);
427 	return inb(p->iobase + 1);
428 }
429 
hub6_serial_out(struct uart_port * p,int offset,int value)430 static void hub6_serial_out(struct uart_port *p, int offset, int value)
431 {
432 	offset = offset << p->regshift;
433 	outb(p->hub6 - 1 + offset, p->iobase);
434 	outb(value, p->iobase + 1);
435 }
436 
mem_serial_in(struct uart_port * p,int offset)437 static unsigned int mem_serial_in(struct uart_port *p, int offset)
438 {
439 	offset = offset << p->regshift;
440 	return readb(p->membase + offset);
441 }
442 
mem_serial_out(struct uart_port * p,int offset,int value)443 static void mem_serial_out(struct uart_port *p, int offset, int value)
444 {
445 	offset = offset << p->regshift;
446 	writeb(value, p->membase + offset);
447 }
448 
mem32_serial_out(struct uart_port * p,int offset,int value)449 static void mem32_serial_out(struct uart_port *p, int offset, int value)
450 {
451 	offset = offset << p->regshift;
452 	writel(value, p->membase + offset);
453 }
454 
mem32_serial_in(struct uart_port * p,int offset)455 static unsigned int mem32_serial_in(struct uart_port *p, int offset)
456 {
457 	offset = offset << p->regshift;
458 	return readl(p->membase + offset);
459 }
460 
mem32be_serial_out(struct uart_port * p,int offset,int value)461 static void mem32be_serial_out(struct uart_port *p, int offset, int value)
462 {
463 	offset = offset << p->regshift;
464 	iowrite32be(value, p->membase + offset);
465 }
466 
mem32be_serial_in(struct uart_port * p,int offset)467 static unsigned int mem32be_serial_in(struct uart_port *p, int offset)
468 {
469 	offset = offset << p->regshift;
470 	return ioread32be(p->membase + offset);
471 }
472 
io_serial_in(struct uart_port * p,int offset)473 static unsigned int io_serial_in(struct uart_port *p, int offset)
474 {
475 	offset = offset << p->regshift;
476 	return inb(p->iobase + offset);
477 }
478 
io_serial_out(struct uart_port * p,int offset,int value)479 static void io_serial_out(struct uart_port *p, int offset, int value)
480 {
481 	offset = offset << p->regshift;
482 	outb(value, p->iobase + offset);
483 }
484 
485 static int serial8250_default_handle_irq(struct uart_port *port);
486 static int exar_handle_irq(struct uart_port *port);
487 
set_io_from_upio(struct uart_port * p)488 static void set_io_from_upio(struct uart_port *p)
489 {
490 	struct uart_8250_port *up = up_to_u8250p(p);
491 
492 	up->dl_read = default_serial_dl_read;
493 	up->dl_write = default_serial_dl_write;
494 
495 	switch (p->iotype) {
496 	case UPIO_HUB6:
497 		p->serial_in = hub6_serial_in;
498 		p->serial_out = hub6_serial_out;
499 		break;
500 
501 	case UPIO_MEM:
502 		p->serial_in = mem_serial_in;
503 		p->serial_out = mem_serial_out;
504 		break;
505 
506 	case UPIO_MEM32:
507 		p->serial_in = mem32_serial_in;
508 		p->serial_out = mem32_serial_out;
509 		break;
510 
511 	case UPIO_MEM32BE:
512 		p->serial_in = mem32be_serial_in;
513 		p->serial_out = mem32be_serial_out;
514 		break;
515 
516 #if defined(CONFIG_MIPS_ALCHEMY) || defined(CONFIG_SERIAL_8250_RT288X)
517 	case UPIO_AU:
518 		p->serial_in = au_serial_in;
519 		p->serial_out = au_serial_out;
520 		up->dl_read = au_serial_dl_read;
521 		up->dl_write = au_serial_dl_write;
522 		break;
523 #endif
524 
525 	default:
526 		p->serial_in = io_serial_in;
527 		p->serial_out = io_serial_out;
528 		break;
529 	}
530 	/* Remember loaded iotype */
531 	up->cur_iotype = p->iotype;
532 	p->handle_irq = serial8250_default_handle_irq;
533 }
534 
535 static void
serial_port_out_sync(struct uart_port * p,int offset,int value)536 serial_port_out_sync(struct uart_port *p, int offset, int value)
537 {
538 	switch (p->iotype) {
539 	case UPIO_MEM:
540 	case UPIO_MEM32:
541 	case UPIO_MEM32BE:
542 	case UPIO_AU:
543 		p->serial_out(p, offset, value);
544 		p->serial_in(p, UART_LCR);	/* safe, no side-effects */
545 		break;
546 	default:
547 		p->serial_out(p, offset, value);
548 	}
549 }
550 
551 /*
552  * For the 16C950
553  */
serial_icr_write(struct uart_8250_port * up,int offset,int value)554 static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
555 {
556 	serial_out(up, UART_SCR, offset);
557 	serial_out(up, UART_ICR, value);
558 }
559 
serial_icr_read(struct uart_8250_port * up,int offset)560 static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
561 {
562 	unsigned int value;
563 
564 	serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
565 	serial_out(up, UART_SCR, offset);
566 	value = serial_in(up, UART_ICR);
567 	serial_icr_write(up, UART_ACR, up->acr);
568 
569 	return value;
570 }
571 
572 /*
573  * FIFO support.
574  */
serial8250_clear_fifos(struct uart_8250_port * p)575 static void serial8250_clear_fifos(struct uart_8250_port *p)
576 {
577 	if (p->capabilities & UART_CAP_FIFO) {
578 		serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
579 		serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
580 			       UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
581 		serial_out(p, UART_FCR, 0);
582 	}
583 }
584 
serial8250_clear_and_reinit_fifos(struct uart_8250_port * p)585 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
586 {
587 	serial8250_clear_fifos(p);
588 	serial_out(p, UART_FCR, p->fcr);
589 }
590 EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);
591 
serial8250_rpm_get(struct uart_8250_port * p)592 void serial8250_rpm_get(struct uart_8250_port *p)
593 {
594 	if (!(p->capabilities & UART_CAP_RPM))
595 		return;
596 	pm_runtime_get_sync(p->port.dev);
597 }
598 EXPORT_SYMBOL_GPL(serial8250_rpm_get);
599 
serial8250_rpm_put(struct uart_8250_port * p)600 void serial8250_rpm_put(struct uart_8250_port *p)
601 {
602 	if (!(p->capabilities & UART_CAP_RPM))
603 		return;
604 	pm_runtime_mark_last_busy(p->port.dev);
605 	pm_runtime_put_autosuspend(p->port.dev);
606 }
607 EXPORT_SYMBOL_GPL(serial8250_rpm_put);
608 
609 /*
610  * These two wrappers ensure that enable_runtime_pm_tx() can be called more than
611  * once and disable_runtime_pm_tx() will still disable RPM because the fifo is
612  * empty and the HW can idle again.
613  */
serial8250_rpm_get_tx(struct uart_8250_port * p)614 static void serial8250_rpm_get_tx(struct uart_8250_port *p)
615 {
616 	unsigned char rpm_active;
617 
618 	if (!(p->capabilities & UART_CAP_RPM))
619 		return;
620 
621 	rpm_active = xchg(&p->rpm_tx_active, 1);
622 	if (rpm_active)
623 		return;
624 	pm_runtime_get_sync(p->port.dev);
625 }
626 
serial8250_rpm_put_tx(struct uart_8250_port * p)627 static void serial8250_rpm_put_tx(struct uart_8250_port *p)
628 {
629 	unsigned char rpm_active;
630 
631 	if (!(p->capabilities & UART_CAP_RPM))
632 		return;
633 
634 	rpm_active = xchg(&p->rpm_tx_active, 0);
635 	if (!rpm_active)
636 		return;
637 	pm_runtime_mark_last_busy(p->port.dev);
638 	pm_runtime_put_autosuspend(p->port.dev);
639 }
640 
641 /*
642  * IER sleep support.  UARTs which have EFRs need the "extended
643  * capability" bit enabled.  Note that on XR16C850s, we need to
644  * reset LCR to write to IER.
645  */
serial8250_set_sleep(struct uart_8250_port * p,int sleep)646 static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
647 {
648 	unsigned char lcr = 0, efr = 0;
649 	/*
650 	 * Exar UARTs have a SLEEP register that enables or disables
651 	 * each UART to enter sleep mode separately.  On the XR17V35x the
652 	 * register is accessible to each UART at the UART_EXAR_SLEEP
653 	 * offset but the UART channel may only write to the corresponding
654 	 * bit.
655 	 */
656 	serial8250_rpm_get(p);
657 	if ((p->port.type == PORT_XR17V35X) ||
658 	   (p->port.type == PORT_XR17D15X)) {
659 		serial_out(p, UART_EXAR_SLEEP, sleep ? 0xff : 0);
660 		goto out;
661 	}
662 
663 	if (p->capabilities & UART_CAP_SLEEP) {
664 		if (p->capabilities & UART_CAP_EFR) {
665 			lcr = serial_in(p, UART_LCR);
666 			efr = serial_in(p, UART_EFR);
667 			serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
668 			serial_out(p, UART_EFR, UART_EFR_ECB);
669 			serial_out(p, UART_LCR, 0);
670 		}
671 		serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
672 		if (p->capabilities & UART_CAP_EFR) {
673 			serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
674 			serial_out(p, UART_EFR, efr);
675 			serial_out(p, UART_LCR, lcr);
676 		}
677 	}
678 out:
679 	serial8250_rpm_put(p);
680 }
681 
682 #ifdef CONFIG_SERIAL_8250_RSA
683 /*
684  * Attempts to turn on the RSA FIFO.  Returns zero on failure.
685  * We set the port uart clock rate if we succeed.
686  */
__enable_rsa(struct uart_8250_port * up)687 static int __enable_rsa(struct uart_8250_port *up)
688 {
689 	unsigned char mode;
690 	int result;
691 
692 	mode = serial_in(up, UART_RSA_MSR);
693 	result = mode & UART_RSA_MSR_FIFO;
694 
695 	if (!result) {
696 		serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
697 		mode = serial_in(up, UART_RSA_MSR);
698 		result = mode & UART_RSA_MSR_FIFO;
699 	}
700 
701 	if (result)
702 		up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
703 
704 	return result;
705 }
706 
enable_rsa(struct uart_8250_port * up)707 static void enable_rsa(struct uart_8250_port *up)
708 {
709 	if (up->port.type == PORT_RSA) {
710 		if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
711 			spin_lock_irq(&up->port.lock);
712 			__enable_rsa(up);
713 			spin_unlock_irq(&up->port.lock);
714 		}
715 		if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
716 			serial_out(up, UART_RSA_FRR, 0);
717 	}
718 }
719 
720 /*
721  * Attempts to turn off the RSA FIFO.  Returns zero on failure.
722  * It is unknown why interrupts were disabled in here.  However,
723  * the caller is expected to preserve this behaviour by grabbing
724  * the spinlock before calling this function.
725  */
disable_rsa(struct uart_8250_port * up)726 static void disable_rsa(struct uart_8250_port *up)
727 {
728 	unsigned char mode;
729 	int result;
730 
731 	if (up->port.type == PORT_RSA &&
732 	    up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
733 		spin_lock_irq(&up->port.lock);
734 
735 		mode = serial_in(up, UART_RSA_MSR);
736 		result = !(mode & UART_RSA_MSR_FIFO);
737 
738 		if (!result) {
739 			serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
740 			mode = serial_in(up, UART_RSA_MSR);
741 			result = !(mode & UART_RSA_MSR_FIFO);
742 		}
743 
744 		if (result)
745 			up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
746 		spin_unlock_irq(&up->port.lock);
747 	}
748 }
749 #endif /* CONFIG_SERIAL_8250_RSA */
750 
751 /*
752  * This is a quickie test to see how big the FIFO is.
753  * It doesn't work at all the time, more's the pity.
754  */
size_fifo(struct uart_8250_port * up)755 static int size_fifo(struct uart_8250_port *up)
756 {
757 	unsigned char old_fcr, old_mcr, old_lcr;
758 	unsigned short old_dl;
759 	int count;
760 
761 	old_lcr = serial_in(up, UART_LCR);
762 	serial_out(up, UART_LCR, 0);
763 	old_fcr = serial_in(up, UART_FCR);
764 	old_mcr = serial_in(up, UART_MCR);
765 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
766 		    UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
767 	serial_out(up, UART_MCR, UART_MCR_LOOP);
768 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
769 	old_dl = serial_dl_read(up);
770 	serial_dl_write(up, 0x0001);
771 	serial_out(up, UART_LCR, 0x03);
772 	for (count = 0; count < 256; count++)
773 		serial_out(up, UART_TX, count);
774 	mdelay(20);/* FIXME - schedule_timeout */
775 	for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
776 	     (count < 256); count++)
777 		serial_in(up, UART_RX);
778 	serial_out(up, UART_FCR, old_fcr);
779 	serial_out(up, UART_MCR, old_mcr);
780 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
781 	serial_dl_write(up, old_dl);
782 	serial_out(up, UART_LCR, old_lcr);
783 
784 	return count;
785 }
786 
787 /*
788  * Read UART ID using the divisor method - set DLL and DLM to zero
789  * and the revision will be in DLL and device type in DLM.  We
790  * preserve the device state across this.
791  */
autoconfig_read_divisor_id(struct uart_8250_port * p)792 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
793 {
794 	unsigned char old_dll, old_dlm, old_lcr;
795 	unsigned int id;
796 
797 	old_lcr = serial_in(p, UART_LCR);
798 	serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
799 
800 	old_dll = serial_in(p, UART_DLL);
801 	old_dlm = serial_in(p, UART_DLM);
802 
803 	serial_out(p, UART_DLL, 0);
804 	serial_out(p, UART_DLM, 0);
805 
806 	id = serial_in(p, UART_DLL) | serial_in(p, UART_DLM) << 8;
807 
808 	serial_out(p, UART_DLL, old_dll);
809 	serial_out(p, UART_DLM, old_dlm);
810 	serial_out(p, UART_LCR, old_lcr);
811 
812 	return id;
813 }
814 
815 /*
816  * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
817  * When this function is called we know it is at least a StarTech
818  * 16650 V2, but it might be one of several StarTech UARTs, or one of
819  * its clones.  (We treat the broken original StarTech 16650 V1 as a
820  * 16550, and why not?  Startech doesn't seem to even acknowledge its
821  * existence.)
822  *
823  * What evil have men's minds wrought...
824  */
autoconfig_has_efr(struct uart_8250_port * up)825 static void autoconfig_has_efr(struct uart_8250_port *up)
826 {
827 	unsigned int id1, id2, id3, rev;
828 
829 	/*
830 	 * Everything with an EFR has SLEEP
831 	 */
832 	up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
833 
834 	/*
835 	 * First we check to see if it's an Oxford Semiconductor UART.
836 	 *
837 	 * If we have to do this here because some non-National
838 	 * Semiconductor clone chips lock up if you try writing to the
839 	 * LSR register (which serial_icr_read does)
840 	 */
841 
842 	/*
843 	 * Check for Oxford Semiconductor 16C950.
844 	 *
845 	 * EFR [4] must be set else this test fails.
846 	 *
847 	 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
848 	 * claims that it's needed for 952 dual UART's (which are not
849 	 * recommended for new designs).
850 	 */
851 	up->acr = 0;
852 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
853 	serial_out(up, UART_EFR, UART_EFR_ECB);
854 	serial_out(up, UART_LCR, 0x00);
855 	id1 = serial_icr_read(up, UART_ID1);
856 	id2 = serial_icr_read(up, UART_ID2);
857 	id3 = serial_icr_read(up, UART_ID3);
858 	rev = serial_icr_read(up, UART_REV);
859 
860 	DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
861 
862 	if (id1 == 0x16 && id2 == 0xC9 &&
863 	    (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
864 		up->port.type = PORT_16C950;
865 
866 		/*
867 		 * Enable work around for the Oxford Semiconductor 952 rev B
868 		 * chip which causes it to seriously miscalculate baud rates
869 		 * when DLL is 0.
870 		 */
871 		if (id3 == 0x52 && rev == 0x01)
872 			up->bugs |= UART_BUG_QUOT;
873 		return;
874 	}
875 
876 	/*
877 	 * We check for a XR16C850 by setting DLL and DLM to 0, and then
878 	 * reading back DLL and DLM.  The chip type depends on the DLM
879 	 * value read back:
880 	 *  0x10 - XR16C850 and the DLL contains the chip revision.
881 	 *  0x12 - XR16C2850.
882 	 *  0x14 - XR16C854.
883 	 */
884 	id1 = autoconfig_read_divisor_id(up);
885 	DEBUG_AUTOCONF("850id=%04x ", id1);
886 
887 	id2 = id1 >> 8;
888 	if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
889 		up->port.type = PORT_16850;
890 		return;
891 	}
892 
893 	/*
894 	 * It wasn't an XR16C850.
895 	 *
896 	 * We distinguish between the '654 and the '650 by counting
897 	 * how many bytes are in the FIFO.  I'm using this for now,
898 	 * since that's the technique that was sent to me in the
899 	 * serial driver update, but I'm not convinced this works.
900 	 * I've had problems doing this in the past.  -TYT
901 	 */
902 	if (size_fifo(up) == 64)
903 		up->port.type = PORT_16654;
904 	else
905 		up->port.type = PORT_16650V2;
906 }
907 
908 /*
909  * We detected a chip without a FIFO.  Only two fall into
910  * this category - the original 8250 and the 16450.  The
911  * 16450 has a scratch register (accessible with LCR=0)
912  */
autoconfig_8250(struct uart_8250_port * up)913 static void autoconfig_8250(struct uart_8250_port *up)
914 {
915 	unsigned char scratch, status1, status2;
916 
917 	up->port.type = PORT_8250;
918 
919 	scratch = serial_in(up, UART_SCR);
920 	serial_out(up, UART_SCR, 0xa5);
921 	status1 = serial_in(up, UART_SCR);
922 	serial_out(up, UART_SCR, 0x5a);
923 	status2 = serial_in(up, UART_SCR);
924 	serial_out(up, UART_SCR, scratch);
925 
926 	if (status1 == 0xa5 && status2 == 0x5a)
927 		up->port.type = PORT_16450;
928 }
929 
broken_efr(struct uart_8250_port * up)930 static int broken_efr(struct uart_8250_port *up)
931 {
932 	/*
933 	 * Exar ST16C2550 "A2" devices incorrectly detect as
934 	 * having an EFR, and report an ID of 0x0201.  See
935 	 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
936 	 */
937 	if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
938 		return 1;
939 
940 	return 0;
941 }
942 
943 /*
944  * We know that the chip has FIFOs.  Does it have an EFR?  The
945  * EFR is located in the same register position as the IIR and
946  * we know the top two bits of the IIR are currently set.  The
947  * EFR should contain zero.  Try to read the EFR.
948  */
autoconfig_16550a(struct uart_8250_port * up)949 static void autoconfig_16550a(struct uart_8250_port *up)
950 {
951 	unsigned char status1, status2;
952 	unsigned int iersave;
953 
954 	up->port.type = PORT_16550A;
955 	up->capabilities |= UART_CAP_FIFO;
956 
957 	/*
958 	 * XR17V35x UARTs have an extra divisor register, DLD
959 	 * that gets enabled with when DLAB is set which will
960 	 * cause the device to incorrectly match and assign
961 	 * port type to PORT_16650.  The EFR for this UART is
962 	 * found at offset 0x09. Instead check the Deice ID (DVID)
963 	 * register for a 2, 4 or 8 port UART.
964 	 */
965 	if (up->port.flags & UPF_EXAR_EFR) {
966 		status1 = serial_in(up, UART_EXAR_DVID);
967 		if (status1 == 0x82 || status1 == 0x84 || status1 == 0x88) {
968 			DEBUG_AUTOCONF("Exar XR17V35x ");
969 			up->port.type = PORT_XR17V35X;
970 			up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
971 						UART_CAP_SLEEP;
972 
973 			return;
974 		}
975 
976 	}
977 
978 	/*
979 	 * Check for presence of the EFR when DLAB is set.
980 	 * Only ST16C650V1 UARTs pass this test.
981 	 */
982 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
983 	if (serial_in(up, UART_EFR) == 0) {
984 		serial_out(up, UART_EFR, 0xA8);
985 		if (serial_in(up, UART_EFR) != 0) {
986 			DEBUG_AUTOCONF("EFRv1 ");
987 			up->port.type = PORT_16650;
988 			up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
989 		} else {
990 			serial_out(up, UART_LCR, 0);
991 			serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
992 				   UART_FCR7_64BYTE);
993 			status1 = serial_in(up, UART_IIR) >> 5;
994 			serial_out(up, UART_FCR, 0);
995 			serial_out(up, UART_LCR, 0);
996 
997 			if (status1 == 7)
998 				up->port.type = PORT_16550A_FSL64;
999 			else
1000 				DEBUG_AUTOCONF("Motorola 8xxx DUART ");
1001 		}
1002 		serial_out(up, UART_EFR, 0);
1003 		return;
1004 	}
1005 
1006 	/*
1007 	 * Maybe it requires 0xbf to be written to the LCR.
1008 	 * (other ST16C650V2 UARTs, TI16C752A, etc)
1009 	 */
1010 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1011 	if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
1012 		DEBUG_AUTOCONF("EFRv2 ");
1013 		autoconfig_has_efr(up);
1014 		return;
1015 	}
1016 
1017 	/*
1018 	 * Check for a National Semiconductor SuperIO chip.
1019 	 * Attempt to switch to bank 2, read the value of the LOOP bit
1020 	 * from EXCR1. Switch back to bank 0, change it in MCR. Then
1021 	 * switch back to bank 2, read it from EXCR1 again and check
1022 	 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
1023 	 */
1024 	serial_out(up, UART_LCR, 0);
1025 	status1 = serial_in(up, UART_MCR);
1026 	serial_out(up, UART_LCR, 0xE0);
1027 	status2 = serial_in(up, 0x02); /* EXCR1 */
1028 
1029 	if (!((status2 ^ status1) & UART_MCR_LOOP)) {
1030 		serial_out(up, UART_LCR, 0);
1031 		serial_out(up, UART_MCR, status1 ^ UART_MCR_LOOP);
1032 		serial_out(up, UART_LCR, 0xE0);
1033 		status2 = serial_in(up, 0x02); /* EXCR1 */
1034 		serial_out(up, UART_LCR, 0);
1035 		serial_out(up, UART_MCR, status1);
1036 
1037 		if ((status2 ^ status1) & UART_MCR_LOOP) {
1038 			unsigned short quot;
1039 
1040 			serial_out(up, UART_LCR, 0xE0);
1041 
1042 			quot = serial_dl_read(up);
1043 			quot <<= 3;
1044 
1045 			if (ns16550a_goto_highspeed(up))
1046 				serial_dl_write(up, quot);
1047 
1048 			serial_out(up, UART_LCR, 0);
1049 
1050 			up->port.uartclk = 921600*16;
1051 			up->port.type = PORT_NS16550A;
1052 			up->capabilities |= UART_NATSEMI;
1053 			return;
1054 		}
1055 	}
1056 
1057 	/*
1058 	 * No EFR.  Try to detect a TI16750, which only sets bit 5 of
1059 	 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1060 	 * Try setting it with and without DLAB set.  Cheap clones
1061 	 * set bit 5 without DLAB set.
1062 	 */
1063 	serial_out(up, UART_LCR, 0);
1064 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1065 	status1 = serial_in(up, UART_IIR) >> 5;
1066 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1067 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1068 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1069 	status2 = serial_in(up, UART_IIR) >> 5;
1070 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1071 	serial_out(up, UART_LCR, 0);
1072 
1073 	DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1074 
1075 	if (status1 == 6 && status2 == 7) {
1076 		up->port.type = PORT_16750;
1077 		up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1078 		return;
1079 	}
1080 
1081 	/*
1082 	 * Try writing and reading the UART_IER_UUE bit (b6).
1083 	 * If it works, this is probably one of the Xscale platform's
1084 	 * internal UARTs.
1085 	 * We're going to explicitly set the UUE bit to 0 before
1086 	 * trying to write and read a 1 just to make sure it's not
1087 	 * already a 1 and maybe locked there before we even start start.
1088 	 */
1089 	iersave = serial_in(up, UART_IER);
1090 	serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
1091 	if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1092 		/*
1093 		 * OK it's in a known zero state, try writing and reading
1094 		 * without disturbing the current state of the other bits.
1095 		 */
1096 		serial_out(up, UART_IER, iersave | UART_IER_UUE);
1097 		if (serial_in(up, UART_IER) & UART_IER_UUE) {
1098 			/*
1099 			 * It's an Xscale.
1100 			 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1101 			 */
1102 			DEBUG_AUTOCONF("Xscale ");
1103 			up->port.type = PORT_XSCALE;
1104 			up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
1105 			return;
1106 		}
1107 	} else {
1108 		/*
1109 		 * If we got here we couldn't force the IER_UUE bit to 0.
1110 		 * Log it and continue.
1111 		 */
1112 		DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1113 	}
1114 	serial_out(up, UART_IER, iersave);
1115 
1116 	/*
1117 	 * Exar uarts have EFR in a weird location
1118 	 */
1119 	if (up->port.flags & UPF_EXAR_EFR) {
1120 		DEBUG_AUTOCONF("Exar XR17D15x ");
1121 		up->port.type = PORT_XR17D15X;
1122 		up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
1123 				    UART_CAP_SLEEP;
1124 
1125 		return;
1126 	}
1127 
1128 	/*
1129 	 * We distinguish between 16550A and U6 16550A by counting
1130 	 * how many bytes are in the FIFO.
1131 	 */
1132 	if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1133 		up->port.type = PORT_U6_16550A;
1134 		up->capabilities |= UART_CAP_AFE;
1135 	}
1136 }
1137 
1138 /*
1139  * This routine is called by rs_init() to initialize a specific serial
1140  * port.  It determines what type of UART chip this serial port is
1141  * using: 8250, 16450, 16550, 16550A.  The important question is
1142  * whether or not this UART is a 16550A or not, since this will
1143  * determine whether or not we can use its FIFO features or not.
1144  */
autoconfig(struct uart_8250_port * up)1145 static void autoconfig(struct uart_8250_port *up)
1146 {
1147 	unsigned char status1, scratch, scratch2, scratch3;
1148 	unsigned char save_lcr, save_mcr;
1149 	struct uart_port *port = &up->port;
1150 	unsigned long flags;
1151 	unsigned int old_capabilities;
1152 
1153 	if (!port->iobase && !port->mapbase && !port->membase)
1154 		return;
1155 
1156 	DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
1157 		       serial_index(port), port->iobase, port->membase);
1158 
1159 	/*
1160 	 * We really do need global IRQs disabled here - we're going to
1161 	 * be frobbing the chips IRQ enable register to see if it exists.
1162 	 */
1163 	spin_lock_irqsave(&port->lock, flags);
1164 
1165 	up->capabilities = 0;
1166 	up->bugs = 0;
1167 
1168 	if (!(port->flags & UPF_BUGGY_UART)) {
1169 		/*
1170 		 * Do a simple existence test first; if we fail this,
1171 		 * there's no point trying anything else.
1172 		 *
1173 		 * 0x80 is used as a nonsense port to prevent against
1174 		 * false positives due to ISA bus float.  The
1175 		 * assumption is that 0x80 is a non-existent port;
1176 		 * which should be safe since include/asm/io.h also
1177 		 * makes this assumption.
1178 		 *
1179 		 * Note: this is safe as long as MCR bit 4 is clear
1180 		 * and the device is in "PC" mode.
1181 		 */
1182 		scratch = serial_in(up, UART_IER);
1183 		serial_out(up, UART_IER, 0);
1184 #ifdef __i386__
1185 		outb(0xff, 0x080);
1186 #endif
1187 		/*
1188 		 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1189 		 * 16C754B) allow only to modify them if an EFR bit is set.
1190 		 */
1191 		scratch2 = serial_in(up, UART_IER) & 0x0f;
1192 		serial_out(up, UART_IER, 0x0F);
1193 #ifdef __i386__
1194 		outb(0, 0x080);
1195 #endif
1196 		scratch3 = serial_in(up, UART_IER) & 0x0f;
1197 		serial_out(up, UART_IER, scratch);
1198 		if (scratch2 != 0 || scratch3 != 0x0F) {
1199 			/*
1200 			 * We failed; there's nothing here
1201 			 */
1202 			spin_unlock_irqrestore(&port->lock, flags);
1203 			DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1204 				       scratch2, scratch3);
1205 			goto out;
1206 		}
1207 	}
1208 
1209 	save_mcr = serial_in(up, UART_MCR);
1210 	save_lcr = serial_in(up, UART_LCR);
1211 
1212 	/*
1213 	 * Check to see if a UART is really there.  Certain broken
1214 	 * internal modems based on the Rockwell chipset fail this
1215 	 * test, because they apparently don't implement the loopback
1216 	 * test mode.  So this test is skipped on the COM 1 through
1217 	 * COM 4 ports.  This *should* be safe, since no board
1218 	 * manufacturer would be stupid enough to design a board
1219 	 * that conflicts with COM 1-4 --- we hope!
1220 	 */
1221 	if (!(port->flags & UPF_SKIP_TEST)) {
1222 		serial_out(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1223 		status1 = serial_in(up, UART_MSR) & 0xF0;
1224 		serial_out(up, UART_MCR, save_mcr);
1225 		if (status1 != 0x90) {
1226 			spin_unlock_irqrestore(&port->lock, flags);
1227 			DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1228 				       status1);
1229 			goto out;
1230 		}
1231 	}
1232 
1233 	/*
1234 	 * We're pretty sure there's a port here.  Lets find out what
1235 	 * type of port it is.  The IIR top two bits allows us to find
1236 	 * out if it's 8250 or 16450, 16550, 16550A or later.  This
1237 	 * determines what we test for next.
1238 	 *
1239 	 * We also initialise the EFR (if any) to zero for later.  The
1240 	 * EFR occupies the same register location as the FCR and IIR.
1241 	 */
1242 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1243 	serial_out(up, UART_EFR, 0);
1244 	serial_out(up, UART_LCR, 0);
1245 
1246 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1247 	scratch = serial_in(up, UART_IIR) >> 6;
1248 
1249 	switch (scratch) {
1250 	case 0:
1251 		autoconfig_8250(up);
1252 		break;
1253 	case 1:
1254 		port->type = PORT_UNKNOWN;
1255 		break;
1256 	case 2:
1257 		port->type = PORT_16550;
1258 		break;
1259 	case 3:
1260 		autoconfig_16550a(up);
1261 		break;
1262 	}
1263 
1264 #ifdef CONFIG_SERIAL_8250_RSA
1265 	/*
1266 	 * Only probe for RSA ports if we got the region.
1267 	 */
1268 	if (port->type == PORT_16550A && up->probe & UART_PROBE_RSA &&
1269 	    __enable_rsa(up))
1270 		port->type = PORT_RSA;
1271 #endif
1272 
1273 	serial_out(up, UART_LCR, save_lcr);
1274 
1275 	port->fifosize = uart_config[up->port.type].fifo_size;
1276 	old_capabilities = up->capabilities;
1277 	up->capabilities = uart_config[port->type].flags;
1278 	up->tx_loadsz = uart_config[port->type].tx_loadsz;
1279 
1280 	if (port->type == PORT_UNKNOWN)
1281 		goto out_lock;
1282 
1283 	/*
1284 	 * Reset the UART.
1285 	 */
1286 #ifdef CONFIG_SERIAL_8250_RSA
1287 	if (port->type == PORT_RSA)
1288 		serial_out(up, UART_RSA_FRR, 0);
1289 #endif
1290 	serial_out(up, UART_MCR, save_mcr);
1291 	serial8250_clear_fifos(up);
1292 	serial_in(up, UART_RX);
1293 	if (up->capabilities & UART_CAP_UUE)
1294 		serial_out(up, UART_IER, UART_IER_UUE);
1295 	else
1296 		serial_out(up, UART_IER, 0);
1297 
1298 out_lock:
1299 	spin_unlock_irqrestore(&port->lock, flags);
1300 	if (up->capabilities != old_capabilities) {
1301 		printk(KERN_WARNING
1302 		       "ttyS%d: detected caps %08x should be %08x\n",
1303 		       serial_index(port), old_capabilities,
1304 		       up->capabilities);
1305 	}
1306 out:
1307 	DEBUG_AUTOCONF("iir=%d ", scratch);
1308 	DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
1309 }
1310 
autoconfig_irq(struct uart_8250_port * up)1311 static void autoconfig_irq(struct uart_8250_port *up)
1312 {
1313 	struct uart_port *port = &up->port;
1314 	unsigned char save_mcr, save_ier;
1315 	unsigned char save_ICP = 0;
1316 	unsigned int ICP = 0;
1317 	unsigned long irqs;
1318 	int irq;
1319 
1320 	if (port->flags & UPF_FOURPORT) {
1321 		ICP = (port->iobase & 0xfe0) | 0x1f;
1322 		save_ICP = inb_p(ICP);
1323 		outb_p(0x80, ICP);
1324 		inb_p(ICP);
1325 	}
1326 
1327 	/* forget possible initially masked and pending IRQ */
1328 	probe_irq_off(probe_irq_on());
1329 	save_mcr = serial_in(up, UART_MCR);
1330 	save_ier = serial_in(up, UART_IER);
1331 	serial_out(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
1332 
1333 	irqs = probe_irq_on();
1334 	serial_out(up, UART_MCR, 0);
1335 	udelay(10);
1336 	if (port->flags & UPF_FOURPORT) {
1337 		serial_out(up, UART_MCR,
1338 			    UART_MCR_DTR | UART_MCR_RTS);
1339 	} else {
1340 		serial_out(up, UART_MCR,
1341 			    UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1342 	}
1343 	serial_out(up, UART_IER, 0x0f);	/* enable all intrs */
1344 	serial_in(up, UART_LSR);
1345 	serial_in(up, UART_RX);
1346 	serial_in(up, UART_IIR);
1347 	serial_in(up, UART_MSR);
1348 	serial_out(up, UART_TX, 0xFF);
1349 	udelay(20);
1350 	irq = probe_irq_off(irqs);
1351 
1352 	serial_out(up, UART_MCR, save_mcr);
1353 	serial_out(up, UART_IER, save_ier);
1354 
1355 	if (port->flags & UPF_FOURPORT)
1356 		outb_p(save_ICP, ICP);
1357 
1358 	port->irq = (irq > 0) ? irq : 0;
1359 }
1360 
__stop_tx(struct uart_8250_port * p)1361 static inline void __stop_tx(struct uart_8250_port *p)
1362 {
1363 	if (p->ier & UART_IER_THRI) {
1364 		p->ier &= ~UART_IER_THRI;
1365 		serial_out(p, UART_IER, p->ier);
1366 		serial8250_rpm_put_tx(p);
1367 	}
1368 }
1369 
serial8250_stop_tx(struct uart_port * port)1370 static void serial8250_stop_tx(struct uart_port *port)
1371 {
1372 	struct uart_8250_port *up = up_to_u8250p(port);
1373 
1374 	serial8250_rpm_get(up);
1375 	__stop_tx(up);
1376 
1377 	/*
1378 	 * We really want to stop the transmitter from sending.
1379 	 */
1380 	if (port->type == PORT_16C950) {
1381 		up->acr |= UART_ACR_TXDIS;
1382 		serial_icr_write(up, UART_ACR, up->acr);
1383 	}
1384 	serial8250_rpm_put(up);
1385 }
1386 
serial8250_start_tx(struct uart_port * port)1387 static void serial8250_start_tx(struct uart_port *port)
1388 {
1389 	struct uart_8250_port *up = up_to_u8250p(port);
1390 
1391 	serial8250_rpm_get_tx(up);
1392 
1393 	if (up->dma && !up->dma->tx_dma(up))
1394 		return;
1395 
1396 	if (!(up->ier & UART_IER_THRI)) {
1397 		up->ier |= UART_IER_THRI;
1398 		serial_port_out(port, UART_IER, up->ier);
1399 
1400 		if (up->bugs & UART_BUG_TXEN) {
1401 			unsigned char lsr;
1402 			lsr = serial_in(up, UART_LSR);
1403 			up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1404 			if (lsr & UART_LSR_THRE)
1405 				serial8250_tx_chars(up);
1406 		}
1407 	}
1408 
1409 	/*
1410 	 * Re-enable the transmitter if we disabled it.
1411 	 */
1412 	if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1413 		up->acr &= ~UART_ACR_TXDIS;
1414 		serial_icr_write(up, UART_ACR, up->acr);
1415 	}
1416 }
1417 
serial8250_throttle(struct uart_port * port)1418 static void serial8250_throttle(struct uart_port *port)
1419 {
1420 	port->throttle(port);
1421 }
1422 
serial8250_unthrottle(struct uart_port * port)1423 static void serial8250_unthrottle(struct uart_port *port)
1424 {
1425 	port->unthrottle(port);
1426 }
1427 
serial8250_stop_rx(struct uart_port * port)1428 static void serial8250_stop_rx(struct uart_port *port)
1429 {
1430 	struct uart_8250_port *up = up_to_u8250p(port);
1431 
1432 	serial8250_rpm_get(up);
1433 
1434 	up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1435 	up->port.read_status_mask &= ~UART_LSR_DR;
1436 	serial_port_out(port, UART_IER, up->ier);
1437 
1438 	serial8250_rpm_put(up);
1439 }
1440 
serial8250_disable_ms(struct uart_port * port)1441 static void serial8250_disable_ms(struct uart_port *port)
1442 {
1443 	struct uart_8250_port *up =
1444 		container_of(port, struct uart_8250_port, port);
1445 
1446 	/* no MSR capabilities */
1447 	if (up->bugs & UART_BUG_NOMSR)
1448 		return;
1449 
1450 	up->ier &= ~UART_IER_MSI;
1451 	serial_port_out(port, UART_IER, up->ier);
1452 }
1453 
serial8250_enable_ms(struct uart_port * port)1454 static void serial8250_enable_ms(struct uart_port *port)
1455 {
1456 	struct uart_8250_port *up = up_to_u8250p(port);
1457 
1458 	/* no MSR capabilities */
1459 	if (up->bugs & UART_BUG_NOMSR)
1460 		return;
1461 
1462 	up->ier |= UART_IER_MSI;
1463 
1464 	serial8250_rpm_get(up);
1465 	serial_port_out(port, UART_IER, up->ier);
1466 	serial8250_rpm_put(up);
1467 }
1468 
1469 /*
1470  * serial8250_rx_chars: processes according to the passed in LSR
1471  * value, and returns the remaining LSR bits not handled
1472  * by this Rx routine.
1473  */
1474 unsigned char
serial8250_rx_chars(struct uart_8250_port * up,unsigned char lsr)1475 serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
1476 {
1477 	struct uart_port *port = &up->port;
1478 	unsigned char ch;
1479 	int max_count = 256;
1480 	char flag;
1481 
1482 	do {
1483 		if (likely(lsr & UART_LSR_DR))
1484 			ch = serial_in(up, UART_RX);
1485 		else
1486 			/*
1487 			 * Intel 82571 has a Serial Over Lan device that will
1488 			 * set UART_LSR_BI without setting UART_LSR_DR when
1489 			 * it receives a break. To avoid reading from the
1490 			 * receive buffer without UART_LSR_DR bit set, we
1491 			 * just force the read character to be 0
1492 			 */
1493 			ch = 0;
1494 
1495 		flag = TTY_NORMAL;
1496 		port->icount.rx++;
1497 
1498 		lsr |= up->lsr_saved_flags;
1499 		up->lsr_saved_flags = 0;
1500 
1501 		if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1502 			if (lsr & UART_LSR_BI) {
1503 				lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1504 				port->icount.brk++;
1505 				/*
1506 				 * We do the SysRQ and SAK checking
1507 				 * here because otherwise the break
1508 				 * may get masked by ignore_status_mask
1509 				 * or read_status_mask.
1510 				 */
1511 				if (uart_handle_break(port))
1512 					goto ignore_char;
1513 			} else if (lsr & UART_LSR_PE)
1514 				port->icount.parity++;
1515 			else if (lsr & UART_LSR_FE)
1516 				port->icount.frame++;
1517 			if (lsr & UART_LSR_OE)
1518 				port->icount.overrun++;
1519 
1520 			/*
1521 			 * Mask off conditions which should be ignored.
1522 			 */
1523 			lsr &= port->read_status_mask;
1524 
1525 			if (lsr & UART_LSR_BI) {
1526 				DEBUG_INTR("handling break....");
1527 				flag = TTY_BREAK;
1528 			} else if (lsr & UART_LSR_PE)
1529 				flag = TTY_PARITY;
1530 			else if (lsr & UART_LSR_FE)
1531 				flag = TTY_FRAME;
1532 		}
1533 		if (uart_handle_sysrq_char(port, ch))
1534 			goto ignore_char;
1535 
1536 		uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
1537 
1538 ignore_char:
1539 		lsr = serial_in(up, UART_LSR);
1540 	} while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (--max_count > 0));
1541 	spin_unlock(&port->lock);
1542 	tty_flip_buffer_push(&port->state->port);
1543 	spin_lock(&port->lock);
1544 	return lsr;
1545 }
1546 EXPORT_SYMBOL_GPL(serial8250_rx_chars);
1547 
serial8250_tx_chars(struct uart_8250_port * up)1548 void serial8250_tx_chars(struct uart_8250_port *up)
1549 {
1550 	struct uart_port *port = &up->port;
1551 	struct circ_buf *xmit = &port->state->xmit;
1552 	int count;
1553 
1554 	if (port->x_char) {
1555 		serial_out(up, UART_TX, port->x_char);
1556 		port->icount.tx++;
1557 		port->x_char = 0;
1558 		return;
1559 	}
1560 	if (uart_tx_stopped(port)) {
1561 		serial8250_stop_tx(port);
1562 		return;
1563 	}
1564 	if (uart_circ_empty(xmit)) {
1565 		__stop_tx(up);
1566 		return;
1567 	}
1568 
1569 	count = up->tx_loadsz;
1570 	do {
1571 		serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1572 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1573 		port->icount.tx++;
1574 		if (uart_circ_empty(xmit))
1575 			break;
1576 		if (up->capabilities & UART_CAP_HFIFO) {
1577 			if ((serial_port_in(port, UART_LSR) & BOTH_EMPTY) !=
1578 			    BOTH_EMPTY)
1579 				break;
1580 		}
1581 	} while (--count > 0);
1582 
1583 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1584 		uart_write_wakeup(port);
1585 
1586 	DEBUG_INTR("THRE...");
1587 
1588 	/*
1589 	 * With RPM enabled, we have to wait until the FIFO is empty before the
1590 	 * HW can go idle. So we get here once again with empty FIFO and disable
1591 	 * the interrupt and RPM in __stop_tx()
1592 	 */
1593 	if (uart_circ_empty(xmit) && !(up->capabilities & UART_CAP_RPM))
1594 		__stop_tx(up);
1595 }
1596 EXPORT_SYMBOL_GPL(serial8250_tx_chars);
1597 
1598 /* Caller holds uart port lock */
serial8250_modem_status(struct uart_8250_port * up)1599 unsigned int serial8250_modem_status(struct uart_8250_port *up)
1600 {
1601 	struct uart_port *port = &up->port;
1602 	unsigned int status = serial_in(up, UART_MSR);
1603 
1604 	status |= up->msr_saved_flags;
1605 	up->msr_saved_flags = 0;
1606 	if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1607 	    port->state != NULL) {
1608 		if (status & UART_MSR_TERI)
1609 			port->icount.rng++;
1610 		if (status & UART_MSR_DDSR)
1611 			port->icount.dsr++;
1612 		if (status & UART_MSR_DDCD)
1613 			uart_handle_dcd_change(port, status & UART_MSR_DCD);
1614 		if (status & UART_MSR_DCTS)
1615 			uart_handle_cts_change(port, status & UART_MSR_CTS);
1616 
1617 		wake_up_interruptible(&port->state->port.delta_msr_wait);
1618 	}
1619 
1620 	return status;
1621 }
1622 EXPORT_SYMBOL_GPL(serial8250_modem_status);
1623 
1624 /*
1625  * This handles the interrupt from one port.
1626  */
serial8250_handle_irq(struct uart_port * port,unsigned int iir)1627 int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
1628 {
1629 	unsigned char status;
1630 	unsigned long flags;
1631 	struct uart_8250_port *up = up_to_u8250p(port);
1632 	int dma_err = 0;
1633 
1634 	if (iir & UART_IIR_NO_INT)
1635 		return 0;
1636 
1637 	spin_lock_irqsave(&port->lock, flags);
1638 
1639 	status = serial_port_in(port, UART_LSR);
1640 
1641 	DEBUG_INTR("status = %x...", status);
1642 
1643 	if (status & (UART_LSR_DR | UART_LSR_BI)) {
1644 		if (up->dma)
1645 			dma_err = up->dma->rx_dma(up, iir);
1646 
1647 		if (!up->dma || dma_err)
1648 			status = serial8250_rx_chars(up, status);
1649 	}
1650 	serial8250_modem_status(up);
1651 	if ((!up->dma || (up->dma && up->dma->tx_err)) &&
1652 	    (status & UART_LSR_THRE))
1653 		serial8250_tx_chars(up);
1654 
1655 	spin_unlock_irqrestore(&port->lock, flags);
1656 	return 1;
1657 }
1658 EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1659 
serial8250_default_handle_irq(struct uart_port * port)1660 static int serial8250_default_handle_irq(struct uart_port *port)
1661 {
1662 	struct uart_8250_port *up = up_to_u8250p(port);
1663 	unsigned int iir;
1664 	int ret;
1665 
1666 	serial8250_rpm_get(up);
1667 
1668 	iir = serial_port_in(port, UART_IIR);
1669 	ret = serial8250_handle_irq(port, iir);
1670 
1671 	serial8250_rpm_put(up);
1672 	return ret;
1673 }
1674 
1675 /*
1676  * These Exar UARTs have an extra interrupt indicator that could
1677  * fire for a few unimplemented interrupts.  One of which is a
1678  * wakeup event when coming out of sleep.  Put this here just
1679  * to be on the safe side that these interrupts don't go unhandled.
1680  */
exar_handle_irq(struct uart_port * port)1681 static int exar_handle_irq(struct uart_port *port)
1682 {
1683 	unsigned char int0, int1, int2, int3;
1684 	unsigned int iir = serial_port_in(port, UART_IIR);
1685 	int ret;
1686 
1687 	ret = serial8250_handle_irq(port, iir);
1688 
1689 	if ((port->type == PORT_XR17V35X) ||
1690 	   (port->type == PORT_XR17D15X)) {
1691 		int0 = serial_port_in(port, 0x80);
1692 		int1 = serial_port_in(port, 0x81);
1693 		int2 = serial_port_in(port, 0x82);
1694 		int3 = serial_port_in(port, 0x83);
1695 	}
1696 
1697 	return ret;
1698 }
1699 
1700 /*
1701  * This is the serial driver's interrupt routine.
1702  *
1703  * Arjan thinks the old way was overly complex, so it got simplified.
1704  * Alan disagrees, saying that need the complexity to handle the weird
1705  * nature of ISA shared interrupts.  (This is a special exception.)
1706  *
1707  * In order to handle ISA shared interrupts properly, we need to check
1708  * that all ports have been serviced, and therefore the ISA interrupt
1709  * line has been de-asserted.
1710  *
1711  * This means we need to loop through all ports. checking that they
1712  * don't have an interrupt pending.
1713  */
serial8250_interrupt(int irq,void * dev_id)1714 static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
1715 {
1716 	struct irq_info *i = dev_id;
1717 	struct list_head *l, *end = NULL;
1718 	int pass_counter = 0, handled = 0;
1719 
1720 	DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1721 
1722 	spin_lock(&i->lock);
1723 
1724 	l = i->head;
1725 	do {
1726 		struct uart_8250_port *up;
1727 		struct uart_port *port;
1728 
1729 		up = list_entry(l, struct uart_8250_port, list);
1730 		port = &up->port;
1731 
1732 		if (port->handle_irq(port)) {
1733 			handled = 1;
1734 			end = NULL;
1735 		} else if (end == NULL)
1736 			end = l;
1737 
1738 		l = l->next;
1739 
1740 		if (l == i->head && pass_counter++ > PASS_LIMIT) {
1741 			/* If we hit this, we're dead. */
1742 			printk_ratelimited(KERN_ERR
1743 				"serial8250: too much work for irq%d\n", irq);
1744 			break;
1745 		}
1746 	} while (l != end);
1747 
1748 	spin_unlock(&i->lock);
1749 
1750 	DEBUG_INTR("end.\n");
1751 
1752 	return IRQ_RETVAL(handled);
1753 }
1754 
1755 /*
1756  * To support ISA shared interrupts, we need to have one interrupt
1757  * handler that ensures that the IRQ line has been deasserted
1758  * before returning.  Failing to do this will result in the IRQ
1759  * line being stuck active, and, since ISA irqs are edge triggered,
1760  * no more IRQs will be seen.
1761  */
serial_do_unlink(struct irq_info * i,struct uart_8250_port * up)1762 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1763 {
1764 	spin_lock_irq(&i->lock);
1765 
1766 	if (!list_empty(i->head)) {
1767 		if (i->head == &up->list)
1768 			i->head = i->head->next;
1769 		list_del(&up->list);
1770 	} else {
1771 		BUG_ON(i->head != &up->list);
1772 		i->head = NULL;
1773 	}
1774 	spin_unlock_irq(&i->lock);
1775 	/* List empty so throw away the hash node */
1776 	if (i->head == NULL) {
1777 		hlist_del(&i->node);
1778 		kfree(i);
1779 	}
1780 }
1781 
serial_link_irq_chain(struct uart_8250_port * up)1782 static int serial_link_irq_chain(struct uart_8250_port *up)
1783 {
1784 	struct hlist_head *h;
1785 	struct hlist_node *n;
1786 	struct irq_info *i;
1787 	int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
1788 
1789 	mutex_lock(&hash_mutex);
1790 
1791 	h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1792 
1793 	hlist_for_each(n, h) {
1794 		i = hlist_entry(n, struct irq_info, node);
1795 		if (i->irq == up->port.irq)
1796 			break;
1797 	}
1798 
1799 	if (n == NULL) {
1800 		i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
1801 		if (i == NULL) {
1802 			mutex_unlock(&hash_mutex);
1803 			return -ENOMEM;
1804 		}
1805 		spin_lock_init(&i->lock);
1806 		i->irq = up->port.irq;
1807 		hlist_add_head(&i->node, h);
1808 	}
1809 	mutex_unlock(&hash_mutex);
1810 
1811 	spin_lock_irq(&i->lock);
1812 
1813 	if (i->head) {
1814 		list_add(&up->list, i->head);
1815 		spin_unlock_irq(&i->lock);
1816 
1817 		ret = 0;
1818 	} else {
1819 		INIT_LIST_HEAD(&up->list);
1820 		i->head = &up->list;
1821 		spin_unlock_irq(&i->lock);
1822 		irq_flags |= up->port.irqflags;
1823 		ret = request_irq(up->port.irq, serial8250_interrupt,
1824 				  irq_flags, "serial", i);
1825 		if (ret < 0)
1826 			serial_do_unlink(i, up);
1827 	}
1828 
1829 	return ret;
1830 }
1831 
serial_unlink_irq_chain(struct uart_8250_port * up)1832 static void serial_unlink_irq_chain(struct uart_8250_port *up)
1833 {
1834 	/*
1835 	 * yes, some broken gcc emit "warning: 'i' may be used uninitialized"
1836 	 * but no, we are not going to take a patch that assigns NULL below.
1837 	 */
1838 	struct irq_info *i;
1839 	struct hlist_node *n;
1840 	struct hlist_head *h;
1841 
1842 	mutex_lock(&hash_mutex);
1843 
1844 	h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1845 
1846 	hlist_for_each(n, h) {
1847 		i = hlist_entry(n, struct irq_info, node);
1848 		if (i->irq == up->port.irq)
1849 			break;
1850 	}
1851 
1852 	BUG_ON(n == NULL);
1853 	BUG_ON(i->head == NULL);
1854 
1855 	if (list_empty(i->head))
1856 		free_irq(up->port.irq, i);
1857 
1858 	serial_do_unlink(i, up);
1859 	mutex_unlock(&hash_mutex);
1860 }
1861 
1862 /*
1863  * This function is used to handle ports that do not have an
1864  * interrupt.  This doesn't work very well for 16450's, but gives
1865  * barely passable results for a 16550A.  (Although at the expense
1866  * of much CPU overhead).
1867  */
serial8250_timeout(unsigned long data)1868 static void serial8250_timeout(unsigned long data)
1869 {
1870 	struct uart_8250_port *up = (struct uart_8250_port *)data;
1871 
1872 	up->port.handle_irq(&up->port);
1873 	mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
1874 }
1875 
serial8250_backup_timeout(unsigned long data)1876 static void serial8250_backup_timeout(unsigned long data)
1877 {
1878 	struct uart_8250_port *up = (struct uart_8250_port *)data;
1879 	unsigned int iir, ier = 0, lsr;
1880 	unsigned long flags;
1881 
1882 	spin_lock_irqsave(&up->port.lock, flags);
1883 
1884 	/*
1885 	 * Must disable interrupts or else we risk racing with the interrupt
1886 	 * based handler.
1887 	 */
1888 	if (up->port.irq) {
1889 		ier = serial_in(up, UART_IER);
1890 		serial_out(up, UART_IER, 0);
1891 	}
1892 
1893 	iir = serial_in(up, UART_IIR);
1894 
1895 	/*
1896 	 * This should be a safe test for anyone who doesn't trust the
1897 	 * IIR bits on their UART, but it's specifically designed for
1898 	 * the "Diva" UART used on the management processor on many HP
1899 	 * ia64 and parisc boxes.
1900 	 */
1901 	lsr = serial_in(up, UART_LSR);
1902 	up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1903 	if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
1904 	    (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
1905 	    (lsr & UART_LSR_THRE)) {
1906 		iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1907 		iir |= UART_IIR_THRI;
1908 	}
1909 
1910 	if (!(iir & UART_IIR_NO_INT))
1911 		serial8250_tx_chars(up);
1912 
1913 	if (up->port.irq)
1914 		serial_out(up, UART_IER, ier);
1915 
1916 	spin_unlock_irqrestore(&up->port.lock, flags);
1917 
1918 	/* Standard timer interval plus 0.2s to keep the port running */
1919 	mod_timer(&up->timer,
1920 		jiffies + uart_poll_timeout(&up->port) + HZ / 5);
1921 }
1922 
univ8250_setup_irq(struct uart_8250_port * up)1923 static int univ8250_setup_irq(struct uart_8250_port *up)
1924 {
1925 	struct uart_port *port = &up->port;
1926 	int retval = 0;
1927 
1928 	/*
1929 	 * The above check will only give an accurate result the first time
1930 	 * the port is opened so this value needs to be preserved.
1931 	 */
1932 	if (up->bugs & UART_BUG_THRE) {
1933 		pr_debug("ttyS%d - using backup timer\n", serial_index(port));
1934 
1935 		up->timer.function = serial8250_backup_timeout;
1936 		up->timer.data = (unsigned long)up;
1937 		mod_timer(&up->timer, jiffies +
1938 			  uart_poll_timeout(port) + HZ / 5);
1939 	}
1940 
1941 	/*
1942 	 * If the "interrupt" for this port doesn't correspond with any
1943 	 * hardware interrupt, we use a timer-based system.  The original
1944 	 * driver used to do this with IRQ0.
1945 	 */
1946 	if (!port->irq) {
1947 		up->timer.data = (unsigned long)up;
1948 		mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
1949 	} else
1950 		retval = serial_link_irq_chain(up);
1951 
1952 	return retval;
1953 }
1954 
univ8250_release_irq(struct uart_8250_port * up)1955 static void univ8250_release_irq(struct uart_8250_port *up)
1956 {
1957 	struct uart_port *port = &up->port;
1958 
1959 	del_timer_sync(&up->timer);
1960 	up->timer.function = serial8250_timeout;
1961 	if (port->irq)
1962 		serial_unlink_irq_chain(up);
1963 }
1964 
serial8250_tx_empty(struct uart_port * port)1965 static unsigned int serial8250_tx_empty(struct uart_port *port)
1966 {
1967 	struct uart_8250_port *up = up_to_u8250p(port);
1968 	unsigned long flags;
1969 	unsigned int lsr;
1970 
1971 	serial8250_rpm_get(up);
1972 
1973 	spin_lock_irqsave(&port->lock, flags);
1974 	lsr = serial_port_in(port, UART_LSR);
1975 	up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1976 	spin_unlock_irqrestore(&port->lock, flags);
1977 
1978 	serial8250_rpm_put(up);
1979 
1980 	return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
1981 }
1982 
serial8250_get_mctrl(struct uart_port * port)1983 static unsigned int serial8250_get_mctrl(struct uart_port *port)
1984 {
1985 	struct uart_8250_port *up = up_to_u8250p(port);
1986 	unsigned int status;
1987 	unsigned int ret;
1988 
1989 	serial8250_rpm_get(up);
1990 	status = serial8250_modem_status(up);
1991 	serial8250_rpm_put(up);
1992 
1993 	ret = 0;
1994 	if (status & UART_MSR_DCD)
1995 		ret |= TIOCM_CAR;
1996 	if (status & UART_MSR_RI)
1997 		ret |= TIOCM_RNG;
1998 	if (status & UART_MSR_DSR)
1999 		ret |= TIOCM_DSR;
2000 	if (status & UART_MSR_CTS)
2001 		ret |= TIOCM_CTS;
2002 	return ret;
2003 }
2004 
serial8250_do_set_mctrl(struct uart_port * port,unsigned int mctrl)2005 void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl)
2006 {
2007 	struct uart_8250_port *up = up_to_u8250p(port);
2008 	unsigned char mcr = 0;
2009 
2010 	if (mctrl & TIOCM_RTS)
2011 		mcr |= UART_MCR_RTS;
2012 	if (mctrl & TIOCM_DTR)
2013 		mcr |= UART_MCR_DTR;
2014 	if (mctrl & TIOCM_OUT1)
2015 		mcr |= UART_MCR_OUT1;
2016 	if (mctrl & TIOCM_OUT2)
2017 		mcr |= UART_MCR_OUT2;
2018 	if (mctrl & TIOCM_LOOP)
2019 		mcr |= UART_MCR_LOOP;
2020 
2021 	mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
2022 
2023 	serial_port_out(port, UART_MCR, mcr);
2024 }
2025 EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);
2026 
serial8250_set_mctrl(struct uart_port * port,unsigned int mctrl)2027 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
2028 {
2029 	if (port->set_mctrl)
2030 		return port->set_mctrl(port, mctrl);
2031 	return serial8250_do_set_mctrl(port, mctrl);
2032 }
2033 
serial8250_break_ctl(struct uart_port * port,int break_state)2034 static void serial8250_break_ctl(struct uart_port *port, int break_state)
2035 {
2036 	struct uart_8250_port *up = up_to_u8250p(port);
2037 	unsigned long flags;
2038 
2039 	serial8250_rpm_get(up);
2040 	spin_lock_irqsave(&port->lock, flags);
2041 	if (break_state == -1)
2042 		up->lcr |= UART_LCR_SBC;
2043 	else
2044 		up->lcr &= ~UART_LCR_SBC;
2045 	serial_port_out(port, UART_LCR, up->lcr);
2046 	spin_unlock_irqrestore(&port->lock, flags);
2047 	serial8250_rpm_put(up);
2048 }
2049 
2050 /*
2051  *	Wait for transmitter & holding register to empty
2052  */
wait_for_xmitr(struct uart_8250_port * up,int bits)2053 static void wait_for_xmitr(struct uart_8250_port *up, int bits)
2054 {
2055 	unsigned int status, tmout = 10000;
2056 
2057 	/* Wait up to 10ms for the character(s) to be sent. */
2058 	for (;;) {
2059 		status = serial_in(up, UART_LSR);
2060 
2061 		up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
2062 
2063 		if ((status & bits) == bits)
2064 			break;
2065 		if (--tmout == 0)
2066 			break;
2067 		udelay(1);
2068 	}
2069 
2070 	/* Wait up to 1s for flow control if necessary */
2071 	if (up->port.flags & UPF_CONS_FLOW) {
2072 		unsigned int tmout;
2073 		for (tmout = 1000000; tmout; tmout--) {
2074 			unsigned int msr = serial_in(up, UART_MSR);
2075 			up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
2076 			if (msr & UART_MSR_CTS)
2077 				break;
2078 			udelay(1);
2079 			touch_nmi_watchdog();
2080 		}
2081 	}
2082 }
2083 
2084 #ifdef CONFIG_CONSOLE_POLL
2085 /*
2086  * Console polling routines for writing and reading from the uart while
2087  * in an interrupt or debug context.
2088  */
2089 
serial8250_get_poll_char(struct uart_port * port)2090 static int serial8250_get_poll_char(struct uart_port *port)
2091 {
2092 	struct uart_8250_port *up = up_to_u8250p(port);
2093 	unsigned char lsr;
2094 	int status;
2095 
2096 	serial8250_rpm_get(up);
2097 
2098 	lsr = serial_port_in(port, UART_LSR);
2099 
2100 	if (!(lsr & UART_LSR_DR)) {
2101 		status = NO_POLL_CHAR;
2102 		goto out;
2103 	}
2104 
2105 	status = serial_port_in(port, UART_RX);
2106 out:
2107 	serial8250_rpm_put(up);
2108 	return status;
2109 }
2110 
2111 
serial8250_put_poll_char(struct uart_port * port,unsigned char c)2112 static void serial8250_put_poll_char(struct uart_port *port,
2113 			 unsigned char c)
2114 {
2115 	unsigned int ier;
2116 	struct uart_8250_port *up = up_to_u8250p(port);
2117 
2118 	serial8250_rpm_get(up);
2119 	/*
2120 	 *	First save the IER then disable the interrupts
2121 	 */
2122 	ier = serial_port_in(port, UART_IER);
2123 	if (up->capabilities & UART_CAP_UUE)
2124 		serial_port_out(port, UART_IER, UART_IER_UUE);
2125 	else
2126 		serial_port_out(port, UART_IER, 0);
2127 
2128 	wait_for_xmitr(up, BOTH_EMPTY);
2129 	/*
2130 	 *	Send the character out.
2131 	 */
2132 	serial_port_out(port, UART_TX, c);
2133 
2134 	/*
2135 	 *	Finally, wait for transmitter to become empty
2136 	 *	and restore the IER
2137 	 */
2138 	wait_for_xmitr(up, BOTH_EMPTY);
2139 	serial_port_out(port, UART_IER, ier);
2140 	serial8250_rpm_put(up);
2141 }
2142 
2143 #endif /* CONFIG_CONSOLE_POLL */
2144 
serial8250_do_startup(struct uart_port * port)2145 int serial8250_do_startup(struct uart_port *port)
2146 {
2147 	struct uart_8250_port *up = up_to_u8250p(port);
2148 	unsigned long flags;
2149 	unsigned char lsr, iir;
2150 	int retval;
2151 
2152 	if (port->type == PORT_8250_CIR)
2153 		return -ENODEV;
2154 
2155 	if (!port->fifosize)
2156 		port->fifosize = uart_config[port->type].fifo_size;
2157 	if (!up->tx_loadsz)
2158 		up->tx_loadsz = uart_config[port->type].tx_loadsz;
2159 	if (!up->capabilities)
2160 		up->capabilities = uart_config[port->type].flags;
2161 	up->mcr = 0;
2162 
2163 	if (port->iotype != up->cur_iotype)
2164 		set_io_from_upio(port);
2165 
2166 	serial8250_rpm_get(up);
2167 	if (port->type == PORT_16C950) {
2168 		/* Wake up and initialize UART */
2169 		up->acr = 0;
2170 		serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2171 		serial_port_out(port, UART_EFR, UART_EFR_ECB);
2172 		serial_port_out(port, UART_IER, 0);
2173 		serial_port_out(port, UART_LCR, 0);
2174 		serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2175 		serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2176 		serial_port_out(port, UART_EFR, UART_EFR_ECB);
2177 		serial_port_out(port, UART_LCR, 0);
2178 	}
2179 
2180 #ifdef CONFIG_SERIAL_8250_RSA
2181 	/*
2182 	 * If this is an RSA port, see if we can kick it up to the
2183 	 * higher speed clock.
2184 	 */
2185 	enable_rsa(up);
2186 #endif
2187 	/*
2188 	 * Clear the FIFO buffers and disable them.
2189 	 * (they will be reenabled in set_termios())
2190 	 */
2191 	serial8250_clear_fifos(up);
2192 
2193 	/*
2194 	 * Clear the interrupt registers.
2195 	 */
2196 	serial_port_in(port, UART_LSR);
2197 	serial_port_in(port, UART_RX);
2198 	serial_port_in(port, UART_IIR);
2199 	serial_port_in(port, UART_MSR);
2200 
2201 	/*
2202 	 * At this point, there's no way the LSR could still be 0xff;
2203 	 * if it is, then bail out, because there's likely no UART
2204 	 * here.
2205 	 */
2206 	if (!(port->flags & UPF_BUGGY_UART) &&
2207 	    (serial_port_in(port, UART_LSR) == 0xff)) {
2208 		printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2209 				   serial_index(port));
2210 		retval = -ENODEV;
2211 		goto out;
2212 	}
2213 
2214 	/*
2215 	 * For a XR16C850, we need to set the trigger levels
2216 	 */
2217 	if (port->type == PORT_16850) {
2218 		unsigned char fctr;
2219 
2220 		serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
2221 
2222 		fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2223 		serial_port_out(port, UART_FCTR,
2224 				fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2225 		serial_port_out(port, UART_TRG, UART_TRG_96);
2226 		serial_port_out(port, UART_FCTR,
2227 				fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2228 		serial_port_out(port, UART_TRG, UART_TRG_96);
2229 
2230 		serial_port_out(port, UART_LCR, 0);
2231 	}
2232 
2233 	if (port->irq) {
2234 		unsigned char iir1;
2235 		/*
2236 		 * Test for UARTs that do not reassert THRE when the
2237 		 * transmitter is idle and the interrupt has already
2238 		 * been cleared.  Real 16550s should always reassert
2239 		 * this interrupt whenever the transmitter is idle and
2240 		 * the interrupt is enabled.  Delays are necessary to
2241 		 * allow register changes to become visible.
2242 		 */
2243 		spin_lock_irqsave(&port->lock, flags);
2244 		if (up->port.irqflags & IRQF_SHARED)
2245 			disable_irq_nosync(port->irq);
2246 
2247 		wait_for_xmitr(up, UART_LSR_THRE);
2248 		serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2249 		udelay(1); /* allow THRE to set */
2250 		iir1 = serial_port_in(port, UART_IIR);
2251 		serial_port_out(port, UART_IER, 0);
2252 		serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2253 		udelay(1); /* allow a working UART time to re-assert THRE */
2254 		iir = serial_port_in(port, UART_IIR);
2255 		serial_port_out(port, UART_IER, 0);
2256 
2257 		if (port->irqflags & IRQF_SHARED)
2258 			enable_irq(port->irq);
2259 		spin_unlock_irqrestore(&port->lock, flags);
2260 
2261 		/*
2262 		 * If the interrupt is not reasserted, or we otherwise
2263 		 * don't trust the iir, setup a timer to kick the UART
2264 		 * on a regular basis.
2265 		 */
2266 		if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
2267 		    up->port.flags & UPF_BUG_THRE) {
2268 			up->bugs |= UART_BUG_THRE;
2269 		}
2270 	}
2271 
2272 	retval = up->ops->setup_irq(up);
2273 	if (retval)
2274 		goto out;
2275 
2276 	/*
2277 	 * Now, initialize the UART
2278 	 */
2279 	serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
2280 
2281 	spin_lock_irqsave(&port->lock, flags);
2282 	if (up->port.flags & UPF_FOURPORT) {
2283 		if (!up->port.irq)
2284 			up->port.mctrl |= TIOCM_OUT1;
2285 	} else
2286 		/*
2287 		 * Most PC uarts need OUT2 raised to enable interrupts.
2288 		 */
2289 		if (port->irq)
2290 			up->port.mctrl |= TIOCM_OUT2;
2291 
2292 	serial8250_set_mctrl(port, port->mctrl);
2293 
2294 	/* Serial over Lan (SoL) hack:
2295 	   Intel 8257x Gigabit ethernet chips have a
2296 	   16550 emulation, to be used for Serial Over Lan.
2297 	   Those chips take a longer time than a normal
2298 	   serial device to signalize that a transmission
2299 	   data was queued. Due to that, the above test generally
2300 	   fails. One solution would be to delay the reading of
2301 	   iir. However, this is not reliable, since the timeout
2302 	   is variable. So, let's just don't test if we receive
2303 	   TX irq. This way, we'll never enable UART_BUG_TXEN.
2304 	 */
2305 	if (up->port.flags & UPF_NO_TXEN_TEST)
2306 		goto dont_test_tx_en;
2307 
2308 	/*
2309 	 * Do a quick test to see if we receive an
2310 	 * interrupt when we enable the TX irq.
2311 	 */
2312 	serial_port_out(port, UART_IER, UART_IER_THRI);
2313 	lsr = serial_port_in(port, UART_LSR);
2314 	iir = serial_port_in(port, UART_IIR);
2315 	serial_port_out(port, UART_IER, 0);
2316 
2317 	if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2318 		if (!(up->bugs & UART_BUG_TXEN)) {
2319 			up->bugs |= UART_BUG_TXEN;
2320 			pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2321 				 serial_index(port));
2322 		}
2323 	} else {
2324 		up->bugs &= ~UART_BUG_TXEN;
2325 	}
2326 
2327 dont_test_tx_en:
2328 	spin_unlock_irqrestore(&port->lock, flags);
2329 
2330 	/*
2331 	 * Clear the interrupt registers again for luck, and clear the
2332 	 * saved flags to avoid getting false values from polling
2333 	 * routines or the previous session.
2334 	 */
2335 	serial_port_in(port, UART_LSR);
2336 	serial_port_in(port, UART_RX);
2337 	serial_port_in(port, UART_IIR);
2338 	serial_port_in(port, UART_MSR);
2339 	up->lsr_saved_flags = 0;
2340 	up->msr_saved_flags = 0;
2341 
2342 	/*
2343 	 * Request DMA channels for both RX and TX.
2344 	 */
2345 	if (up->dma) {
2346 		retval = serial8250_request_dma(up);
2347 		if (retval) {
2348 			pr_warn_ratelimited("ttyS%d - failed to request DMA\n",
2349 					    serial_index(port));
2350 			up->dma = NULL;
2351 		}
2352 	}
2353 
2354 	/*
2355 	 * Finally, enable interrupts.  Note: Modem status interrupts
2356 	 * are set via set_termios(), which will be occurring imminently
2357 	 * anyway, so we don't enable them here.
2358 	 */
2359 	up->ier = UART_IER_RLSI | UART_IER_RDI;
2360 	serial_port_out(port, UART_IER, up->ier);
2361 
2362 	if (port->flags & UPF_FOURPORT) {
2363 		unsigned int icp;
2364 		/*
2365 		 * Enable interrupts on the AST Fourport board
2366 		 */
2367 		icp = (port->iobase & 0xfe0) | 0x01f;
2368 		outb_p(0x80, icp);
2369 		inb_p(icp);
2370 	}
2371 	retval = 0;
2372 out:
2373 	serial8250_rpm_put(up);
2374 	return retval;
2375 }
2376 EXPORT_SYMBOL_GPL(serial8250_do_startup);
2377 
serial8250_startup(struct uart_port * port)2378 static int serial8250_startup(struct uart_port *port)
2379 {
2380 	if (port->startup)
2381 		return port->startup(port);
2382 	return serial8250_do_startup(port);
2383 }
2384 
serial8250_do_shutdown(struct uart_port * port)2385 void serial8250_do_shutdown(struct uart_port *port)
2386 {
2387 	struct uart_8250_port *up = up_to_u8250p(port);
2388 	unsigned long flags;
2389 
2390 	serial8250_rpm_get(up);
2391 	/*
2392 	 * Disable interrupts from this port
2393 	 */
2394 	up->ier = 0;
2395 	serial_port_out(port, UART_IER, 0);
2396 
2397 	if (up->dma)
2398 		serial8250_release_dma(up);
2399 
2400 	spin_lock_irqsave(&port->lock, flags);
2401 	if (port->flags & UPF_FOURPORT) {
2402 		/* reset interrupts on the AST Fourport board */
2403 		inb((port->iobase & 0xfe0) | 0x1f);
2404 		port->mctrl |= TIOCM_OUT1;
2405 	} else
2406 		port->mctrl &= ~TIOCM_OUT2;
2407 
2408 	serial8250_set_mctrl(port, port->mctrl);
2409 	spin_unlock_irqrestore(&port->lock, flags);
2410 
2411 	/*
2412 	 * Disable break condition and FIFOs
2413 	 */
2414 	serial_port_out(port, UART_LCR,
2415 			serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
2416 	serial8250_clear_fifos(up);
2417 
2418 #ifdef CONFIG_SERIAL_8250_RSA
2419 	/*
2420 	 * Reset the RSA board back to 115kbps compat mode.
2421 	 */
2422 	disable_rsa(up);
2423 #endif
2424 
2425 	/*
2426 	 * Read data port to reset things, and then unlink from
2427 	 * the IRQ chain.
2428 	 */
2429 	serial_port_in(port, UART_RX);
2430 	serial8250_rpm_put(up);
2431 
2432 	up->ops->release_irq(up);
2433 }
2434 EXPORT_SYMBOL_GPL(serial8250_do_shutdown);
2435 
serial8250_shutdown(struct uart_port * port)2436 static void serial8250_shutdown(struct uart_port *port)
2437 {
2438 	if (port->shutdown)
2439 		port->shutdown(port);
2440 	else
2441 		serial8250_do_shutdown(port);
2442 }
2443 
2444 /*
2445  * XR17V35x UARTs have an extra fractional divisor register (DLD)
2446  * Calculate divisor with extra 4-bit fractional portion
2447  */
xr17v35x_get_divisor(struct uart_8250_port * up,unsigned int baud,unsigned int * frac)2448 static unsigned int xr17v35x_get_divisor(struct uart_8250_port *up,
2449 					 unsigned int baud,
2450 					 unsigned int *frac)
2451 {
2452 	struct uart_port *port = &up->port;
2453 	unsigned int quot_16;
2454 
2455 	quot_16 = DIV_ROUND_CLOSEST(port->uartclk, baud);
2456 	*frac = quot_16 & 0x0f;
2457 
2458 	return quot_16 >> 4;
2459 }
2460 
serial8250_get_divisor(struct uart_8250_port * up,unsigned int baud,unsigned int * frac)2461 static unsigned int serial8250_get_divisor(struct uart_8250_port *up,
2462 					   unsigned int baud,
2463 					   unsigned int *frac)
2464 {
2465 	struct uart_port *port = &up->port;
2466 	unsigned int quot;
2467 
2468 	/*
2469 	 * Handle magic divisors for baud rates above baud_base on
2470 	 * SMSC SuperIO chips.
2471 	 *
2472 	 */
2473 	if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2474 	    baud == (port->uartclk/4))
2475 		quot = 0x8001;
2476 	else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2477 		 baud == (port->uartclk/8))
2478 		quot = 0x8002;
2479 	else if (up->port.type == PORT_XR17V35X)
2480 		quot = xr17v35x_get_divisor(up, baud, frac);
2481 	else
2482 		quot = uart_get_divisor(port, baud);
2483 
2484 	/*
2485 	 * Oxford Semi 952 rev B workaround
2486 	 */
2487 	if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2488 		quot++;
2489 
2490 	return quot;
2491 }
2492 
serial8250_compute_lcr(struct uart_8250_port * up,tcflag_t c_cflag)2493 static unsigned char serial8250_compute_lcr(struct uart_8250_port *up,
2494 					    tcflag_t c_cflag)
2495 {
2496 	unsigned char cval;
2497 
2498 	switch (c_cflag & CSIZE) {
2499 	case CS5:
2500 		cval = UART_LCR_WLEN5;
2501 		break;
2502 	case CS6:
2503 		cval = UART_LCR_WLEN6;
2504 		break;
2505 	case CS7:
2506 		cval = UART_LCR_WLEN7;
2507 		break;
2508 	default:
2509 	case CS8:
2510 		cval = UART_LCR_WLEN8;
2511 		break;
2512 	}
2513 
2514 	if (c_cflag & CSTOPB)
2515 		cval |= UART_LCR_STOP;
2516 	if (c_cflag & PARENB) {
2517 		cval |= UART_LCR_PARITY;
2518 		if (up->bugs & UART_BUG_PARITY)
2519 			up->fifo_bug = true;
2520 	}
2521 	if (!(c_cflag & PARODD))
2522 		cval |= UART_LCR_EPAR;
2523 #ifdef CMSPAR
2524 	if (c_cflag & CMSPAR)
2525 		cval |= UART_LCR_SPAR;
2526 #endif
2527 
2528 	return cval;
2529 }
2530 
serial8250_set_divisor(struct uart_port * port,unsigned int baud,unsigned int quot,unsigned int quot_frac)2531 static void serial8250_set_divisor(struct uart_port *port, unsigned int baud,
2532 			    unsigned int quot, unsigned int quot_frac)
2533 {
2534 	struct uart_8250_port *up = up_to_u8250p(port);
2535 
2536 	/* Workaround to enable 115200 baud on OMAP1510 internal ports */
2537 	if (is_omap1510_8250(up)) {
2538 		if (baud == 115200) {
2539 			quot = 1;
2540 			serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
2541 		} else
2542 			serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
2543 	}
2544 
2545 	/*
2546 	 * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
2547 	 * otherwise just set DLAB
2548 	 */
2549 	if (up->capabilities & UART_NATSEMI)
2550 		serial_port_out(port, UART_LCR, 0xe0);
2551 	else
2552 		serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
2553 
2554 	serial_dl_write(up, quot);
2555 
2556 	/* XR17V35x UARTs have an extra fractional divisor register (DLD) */
2557 	if (up->port.type == PORT_XR17V35X)
2558 		serial_port_out(port, 0x2, quot_frac);
2559 }
2560 
2561 void
serial8250_do_set_termios(struct uart_port * port,struct ktermios * termios,struct ktermios * old)2562 serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2563 		          struct ktermios *old)
2564 {
2565 	struct uart_8250_port *up = up_to_u8250p(port);
2566 	unsigned char cval;
2567 	unsigned long flags;
2568 	unsigned int baud, quot, frac = 0;
2569 
2570 	cval = serial8250_compute_lcr(up, termios->c_cflag);
2571 
2572 	/*
2573 	 * Ask the core to calculate the divisor for us.
2574 	 */
2575 	baud = uart_get_baud_rate(port, termios, old,
2576 				  port->uartclk / 16 / 0xffff,
2577 				  port->uartclk / 16);
2578 	quot = serial8250_get_divisor(up, baud, &frac);
2579 
2580 	/*
2581 	 * Ok, we're now changing the port state.  Do it with
2582 	 * interrupts disabled.
2583 	 */
2584 	serial8250_rpm_get(up);
2585 	spin_lock_irqsave(&port->lock, flags);
2586 
2587 	up->lcr = cval;					/* Save computed LCR */
2588 
2589 	if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
2590 		/* NOTE: If fifo_bug is not set, a user can set RX_trigger. */
2591 		if ((baud < 2400 && !up->dma) || up->fifo_bug) {
2592 			up->fcr &= ~UART_FCR_TRIGGER_MASK;
2593 			up->fcr |= UART_FCR_TRIGGER_1;
2594 		}
2595 	}
2596 
2597 	/*
2598 	 * MCR-based auto flow control.  When AFE is enabled, RTS will be
2599 	 * deasserted when the receive FIFO contains more characters than
2600 	 * the trigger, or the MCR RTS bit is cleared.  In the case where
2601 	 * the remote UART is not using CTS auto flow control, we must
2602 	 * have sufficient FIFO entries for the latency of the remote
2603 	 * UART to respond.  IOW, at least 32 bytes of FIFO.
2604 	 */
2605 	if (up->capabilities & UART_CAP_AFE && port->fifosize >= 32) {
2606 		up->mcr &= ~UART_MCR_AFE;
2607 		if (termios->c_cflag & CRTSCTS)
2608 			up->mcr |= UART_MCR_AFE;
2609 	}
2610 
2611 	/*
2612 	 * Update the per-port timeout.
2613 	 */
2614 	uart_update_timeout(port, termios->c_cflag, baud);
2615 
2616 	port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2617 	if (termios->c_iflag & INPCK)
2618 		port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2619 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2620 		port->read_status_mask |= UART_LSR_BI;
2621 
2622 	/*
2623 	 * Characteres to ignore
2624 	 */
2625 	port->ignore_status_mask = 0;
2626 	if (termios->c_iflag & IGNPAR)
2627 		port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2628 	if (termios->c_iflag & IGNBRK) {
2629 		port->ignore_status_mask |= UART_LSR_BI;
2630 		/*
2631 		 * If we're ignoring parity and break indicators,
2632 		 * ignore overruns too (for real raw support).
2633 		 */
2634 		if (termios->c_iflag & IGNPAR)
2635 			port->ignore_status_mask |= UART_LSR_OE;
2636 	}
2637 
2638 	/*
2639 	 * ignore all characters if CREAD is not set
2640 	 */
2641 	if ((termios->c_cflag & CREAD) == 0)
2642 		port->ignore_status_mask |= UART_LSR_DR;
2643 
2644 	/*
2645 	 * CTS flow control flag and modem status interrupts
2646 	 */
2647 	up->ier &= ~UART_IER_MSI;
2648 	if (!(up->bugs & UART_BUG_NOMSR) &&
2649 			UART_ENABLE_MS(&up->port, termios->c_cflag))
2650 		up->ier |= UART_IER_MSI;
2651 	if (up->capabilities & UART_CAP_UUE)
2652 		up->ier |= UART_IER_UUE;
2653 	if (up->capabilities & UART_CAP_RTOIE)
2654 		up->ier |= UART_IER_RTOIE;
2655 
2656 	serial_port_out(port, UART_IER, up->ier);
2657 
2658 	if (up->capabilities & UART_CAP_EFR) {
2659 		unsigned char efr = 0;
2660 		/*
2661 		 * TI16C752/Startech hardware flow control.  FIXME:
2662 		 * - TI16C752 requires control thresholds to be set.
2663 		 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2664 		 */
2665 		if (termios->c_cflag & CRTSCTS)
2666 			efr |= UART_EFR_CTS;
2667 
2668 		serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2669 		if (port->flags & UPF_EXAR_EFR)
2670 			serial_port_out(port, UART_XR_EFR, efr);
2671 		else
2672 			serial_port_out(port, UART_EFR, efr);
2673 	}
2674 
2675 	serial8250_set_divisor(port, baud, quot, frac);
2676 
2677 	/*
2678 	 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2679 	 * is written without DLAB set, this mode will be disabled.
2680 	 */
2681 	if (port->type == PORT_16750)
2682 		serial_port_out(port, UART_FCR, up->fcr);
2683 
2684 	serial_port_out(port, UART_LCR, up->lcr);	/* reset DLAB */
2685 	if (port->type != PORT_16750) {
2686 		/* emulated UARTs (Lucent Venus 167x) need two steps */
2687 		if (up->fcr & UART_FCR_ENABLE_FIFO)
2688 			serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
2689 		serial_port_out(port, UART_FCR, up->fcr);	/* set fcr */
2690 	}
2691 	serial8250_set_mctrl(port, port->mctrl);
2692 	spin_unlock_irqrestore(&port->lock, flags);
2693 	serial8250_rpm_put(up);
2694 
2695 	/* Don't rewrite B0 */
2696 	if (tty_termios_baud_rate(termios))
2697 		tty_termios_encode_baud_rate(termios, baud, baud);
2698 }
2699 EXPORT_SYMBOL(serial8250_do_set_termios);
2700 
2701 static void
serial8250_set_termios(struct uart_port * port,struct ktermios * termios,struct ktermios * old)2702 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2703 		       struct ktermios *old)
2704 {
2705 	if (port->set_termios)
2706 		port->set_termios(port, termios, old);
2707 	else
2708 		serial8250_do_set_termios(port, termios, old);
2709 }
2710 
2711 static void
serial8250_set_ldisc(struct uart_port * port,struct ktermios * termios)2712 serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios)
2713 {
2714 	if (termios->c_line == N_PPS) {
2715 		port->flags |= UPF_HARDPPS_CD;
2716 		spin_lock_irq(&port->lock);
2717 		serial8250_enable_ms(port);
2718 		spin_unlock_irq(&port->lock);
2719 	} else {
2720 		port->flags &= ~UPF_HARDPPS_CD;
2721 		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2722 			spin_lock_irq(&port->lock);
2723 			serial8250_disable_ms(port);
2724 			spin_unlock_irq(&port->lock);
2725 		}
2726 	}
2727 }
2728 
2729 
serial8250_do_pm(struct uart_port * port,unsigned int state,unsigned int oldstate)2730 void serial8250_do_pm(struct uart_port *port, unsigned int state,
2731 		      unsigned int oldstate)
2732 {
2733 	struct uart_8250_port *p = up_to_u8250p(port);
2734 
2735 	serial8250_set_sleep(p, state != 0);
2736 }
2737 EXPORT_SYMBOL(serial8250_do_pm);
2738 
2739 static void
serial8250_pm(struct uart_port * port,unsigned int state,unsigned int oldstate)2740 serial8250_pm(struct uart_port *port, unsigned int state,
2741 	      unsigned int oldstate)
2742 {
2743 	if (port->pm)
2744 		port->pm(port, state, oldstate);
2745 	else
2746 		serial8250_do_pm(port, state, oldstate);
2747 }
2748 
serial8250_port_size(struct uart_8250_port * pt)2749 static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2750 {
2751 	if (pt->port.mapsize)
2752 		return pt->port.mapsize;
2753 	if (pt->port.iotype == UPIO_AU) {
2754 		if (pt->port.type == PORT_RT2880)
2755 			return 0x100;
2756 		return 0x1000;
2757 	}
2758 	if (is_omap1_8250(pt))
2759 		return 0x16 << pt->port.regshift;
2760 
2761 	return 8 << pt->port.regshift;
2762 }
2763 
2764 /*
2765  * Resource handling.
2766  */
serial8250_request_std_resource(struct uart_8250_port * up)2767 static int serial8250_request_std_resource(struct uart_8250_port *up)
2768 {
2769 	unsigned int size = serial8250_port_size(up);
2770 	struct uart_port *port = &up->port;
2771 	int ret = 0;
2772 
2773 	switch (port->iotype) {
2774 	case UPIO_AU:
2775 	case UPIO_TSI:
2776 	case UPIO_MEM32:
2777 	case UPIO_MEM32BE:
2778 	case UPIO_MEM:
2779 		if (!port->mapbase)
2780 			break;
2781 
2782 		if (!request_mem_region(port->mapbase, size, "serial")) {
2783 			ret = -EBUSY;
2784 			break;
2785 		}
2786 
2787 		if (port->flags & UPF_IOREMAP) {
2788 			port->membase = ioremap_nocache(port->mapbase, size);
2789 			if (!port->membase) {
2790 				release_mem_region(port->mapbase, size);
2791 				ret = -ENOMEM;
2792 			}
2793 		}
2794 		break;
2795 
2796 	case UPIO_HUB6:
2797 	case UPIO_PORT:
2798 		if (!request_region(port->iobase, size, "serial"))
2799 			ret = -EBUSY;
2800 		break;
2801 	}
2802 	return ret;
2803 }
2804 
serial8250_release_std_resource(struct uart_8250_port * up)2805 static void serial8250_release_std_resource(struct uart_8250_port *up)
2806 {
2807 	unsigned int size = serial8250_port_size(up);
2808 	struct uart_port *port = &up->port;
2809 
2810 	switch (port->iotype) {
2811 	case UPIO_AU:
2812 	case UPIO_TSI:
2813 	case UPIO_MEM32:
2814 	case UPIO_MEM32BE:
2815 	case UPIO_MEM:
2816 		if (!port->mapbase)
2817 			break;
2818 
2819 		if (port->flags & UPF_IOREMAP) {
2820 			iounmap(port->membase);
2821 			port->membase = NULL;
2822 		}
2823 
2824 		release_mem_region(port->mapbase, size);
2825 		break;
2826 
2827 	case UPIO_HUB6:
2828 	case UPIO_PORT:
2829 		release_region(port->iobase, size);
2830 		break;
2831 	}
2832 }
2833 
2834 #ifdef CONFIG_SERIAL_8250_RSA
serial8250_request_rsa_resource(struct uart_8250_port * up)2835 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2836 {
2837 	unsigned long start = UART_RSA_BASE << up->port.regshift;
2838 	unsigned int size = 8 << up->port.regshift;
2839 	struct uart_port *port = &up->port;
2840 	int ret = -EINVAL;
2841 
2842 	switch (port->iotype) {
2843 	case UPIO_HUB6:
2844 	case UPIO_PORT:
2845 		start += port->iobase;
2846 		if (request_region(start, size, "serial-rsa"))
2847 			ret = 0;
2848 		else
2849 			ret = -EBUSY;
2850 		break;
2851 	}
2852 
2853 	return ret;
2854 }
2855 
serial8250_release_rsa_resource(struct uart_8250_port * up)2856 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2857 {
2858 	unsigned long offset = UART_RSA_BASE << up->port.regshift;
2859 	unsigned int size = 8 << up->port.regshift;
2860 	struct uart_port *port = &up->port;
2861 
2862 	switch (port->iotype) {
2863 	case UPIO_HUB6:
2864 	case UPIO_PORT:
2865 		release_region(port->iobase + offset, size);
2866 		break;
2867 	}
2868 }
2869 #endif
2870 
serial8250_release_port(struct uart_port * port)2871 static void serial8250_release_port(struct uart_port *port)
2872 {
2873 	struct uart_8250_port *up = up_to_u8250p(port);
2874 
2875 	serial8250_release_std_resource(up);
2876 }
2877 
serial8250_request_port(struct uart_port * port)2878 static int serial8250_request_port(struct uart_port *port)
2879 {
2880 	struct uart_8250_port *up = up_to_u8250p(port);
2881 	int ret;
2882 
2883 	if (port->type == PORT_8250_CIR)
2884 		return -ENODEV;
2885 
2886 	ret = serial8250_request_std_resource(up);
2887 
2888 	return ret;
2889 }
2890 
fcr_get_rxtrig_bytes(struct uart_8250_port * up)2891 static int fcr_get_rxtrig_bytes(struct uart_8250_port *up)
2892 {
2893 	const struct serial8250_config *conf_type = &uart_config[up->port.type];
2894 	unsigned char bytes;
2895 
2896 	bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)];
2897 
2898 	return bytes ? bytes : -EOPNOTSUPP;
2899 }
2900 
bytes_to_fcr_rxtrig(struct uart_8250_port * up,unsigned char bytes)2901 static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes)
2902 {
2903 	const struct serial8250_config *conf_type = &uart_config[up->port.type];
2904 	int i;
2905 
2906 	if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)])
2907 		return -EOPNOTSUPP;
2908 
2909 	for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) {
2910 		if (bytes < conf_type->rxtrig_bytes[i])
2911 			/* Use the nearest lower value */
2912 			return (--i) << UART_FCR_R_TRIG_SHIFT;
2913 	}
2914 
2915 	return UART_FCR_R_TRIG_11;
2916 }
2917 
do_get_rxtrig(struct tty_port * port)2918 static int do_get_rxtrig(struct tty_port *port)
2919 {
2920 	struct uart_state *state = container_of(port, struct uart_state, port);
2921 	struct uart_port *uport = state->uart_port;
2922 	struct uart_8250_port *up =
2923 		container_of(uport, struct uart_8250_port, port);
2924 
2925 	if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
2926 		return -EINVAL;
2927 
2928 	return fcr_get_rxtrig_bytes(up);
2929 }
2930 
do_serial8250_get_rxtrig(struct tty_port * port)2931 static int do_serial8250_get_rxtrig(struct tty_port *port)
2932 {
2933 	int rxtrig_bytes;
2934 
2935 	mutex_lock(&port->mutex);
2936 	rxtrig_bytes = do_get_rxtrig(port);
2937 	mutex_unlock(&port->mutex);
2938 
2939 	return rxtrig_bytes;
2940 }
2941 
serial8250_get_attr_rx_trig_bytes(struct device * dev,struct device_attribute * attr,char * buf)2942 static ssize_t serial8250_get_attr_rx_trig_bytes(struct device *dev,
2943 	struct device_attribute *attr, char *buf)
2944 {
2945 	struct tty_port *port = dev_get_drvdata(dev);
2946 	int rxtrig_bytes;
2947 
2948 	rxtrig_bytes = do_serial8250_get_rxtrig(port);
2949 	if (rxtrig_bytes < 0)
2950 		return rxtrig_bytes;
2951 
2952 	return snprintf(buf, PAGE_SIZE, "%d\n", rxtrig_bytes);
2953 }
2954 
do_set_rxtrig(struct tty_port * port,unsigned char bytes)2955 static int do_set_rxtrig(struct tty_port *port, unsigned char bytes)
2956 {
2957 	struct uart_state *state = container_of(port, struct uart_state, port);
2958 	struct uart_port *uport = state->uart_port;
2959 	struct uart_8250_port *up =
2960 		container_of(uport, struct uart_8250_port, port);
2961 	int rxtrig;
2962 
2963 	if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1 ||
2964 	    up->fifo_bug)
2965 		return -EINVAL;
2966 
2967 	rxtrig = bytes_to_fcr_rxtrig(up, bytes);
2968 	if (rxtrig < 0)
2969 		return rxtrig;
2970 
2971 	serial8250_clear_fifos(up);
2972 	up->fcr &= ~UART_FCR_TRIGGER_MASK;
2973 	up->fcr |= (unsigned char)rxtrig;
2974 	serial_out(up, UART_FCR, up->fcr);
2975 	return 0;
2976 }
2977 
do_serial8250_set_rxtrig(struct tty_port * port,unsigned char bytes)2978 static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes)
2979 {
2980 	int ret;
2981 
2982 	mutex_lock(&port->mutex);
2983 	ret = do_set_rxtrig(port, bytes);
2984 	mutex_unlock(&port->mutex);
2985 
2986 	return ret;
2987 }
2988 
serial8250_set_attr_rx_trig_bytes(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2989 static ssize_t serial8250_set_attr_rx_trig_bytes(struct device *dev,
2990 	struct device_attribute *attr, const char *buf, size_t count)
2991 {
2992 	struct tty_port *port = dev_get_drvdata(dev);
2993 	unsigned char bytes;
2994 	int ret;
2995 
2996 	if (!count)
2997 		return -EINVAL;
2998 
2999 	ret = kstrtou8(buf, 10, &bytes);
3000 	if (ret < 0)
3001 		return ret;
3002 
3003 	ret = do_serial8250_set_rxtrig(port, bytes);
3004 	if (ret < 0)
3005 		return ret;
3006 
3007 	return count;
3008 }
3009 
3010 static DEVICE_ATTR(rx_trig_bytes, S_IRUSR | S_IWUSR | S_IRGRP,
3011 		   serial8250_get_attr_rx_trig_bytes,
3012 		   serial8250_set_attr_rx_trig_bytes);
3013 
3014 static struct attribute *serial8250_dev_attrs[] = {
3015 	&dev_attr_rx_trig_bytes.attr,
3016 	NULL,
3017 	};
3018 
3019 static struct attribute_group serial8250_dev_attr_group = {
3020 	.attrs = serial8250_dev_attrs,
3021 	};
3022 
register_dev_spec_attr_grp(struct uart_8250_port * up)3023 static void register_dev_spec_attr_grp(struct uart_8250_port *up)
3024 {
3025 	const struct serial8250_config *conf_type = &uart_config[up->port.type];
3026 
3027 	if (conf_type->rxtrig_bytes[0])
3028 		up->port.attr_group = &serial8250_dev_attr_group;
3029 }
3030 
serial8250_config_port(struct uart_port * port,int flags)3031 static void serial8250_config_port(struct uart_port *port, int flags)
3032 {
3033 	struct uart_8250_port *up = up_to_u8250p(port);
3034 	int ret;
3035 
3036 	if (port->type == PORT_8250_CIR)
3037 		return;
3038 
3039 	/*
3040 	 * Find the region that we can probe for.  This in turn
3041 	 * tells us whether we can probe for the type of port.
3042 	 */
3043 	ret = serial8250_request_std_resource(up);
3044 	if (ret < 0)
3045 		return;
3046 
3047 	if (port->iotype != up->cur_iotype)
3048 		set_io_from_upio(port);
3049 
3050 	if (flags & UART_CONFIG_TYPE)
3051 		autoconfig(up);
3052 
3053 	/* if access method is AU, it is a 16550 with a quirk */
3054 	if (port->type == PORT_16550A && port->iotype == UPIO_AU)
3055 		up->bugs |= UART_BUG_NOMSR;
3056 
3057 	/* HW bugs may trigger IRQ while IIR == NO_INT */
3058 	if (port->type == PORT_TEGRA)
3059 		up->bugs |= UART_BUG_NOMSR;
3060 
3061 	if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
3062 		autoconfig_irq(up);
3063 
3064 	if (port->type == PORT_UNKNOWN)
3065 		serial8250_release_std_resource(up);
3066 
3067 	/* Fixme: probably not the best place for this */
3068 	if ((port->type == PORT_XR17V35X) ||
3069 	   (port->type == PORT_XR17D15X))
3070 		port->handle_irq = exar_handle_irq;
3071 
3072 	register_dev_spec_attr_grp(up);
3073 	up->fcr = uart_config[up->port.type].fcr;
3074 }
3075 
3076 static int
serial8250_verify_port(struct uart_port * port,struct serial_struct * ser)3077 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
3078 {
3079 	if (ser->irq >= nr_irqs || ser->irq < 0 ||
3080 	    ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
3081 	    ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
3082 	    ser->type == PORT_STARTECH)
3083 		return -EINVAL;
3084 	return 0;
3085 }
3086 
3087 static const char *
serial8250_type(struct uart_port * port)3088 serial8250_type(struct uart_port *port)
3089 {
3090 	int type = port->type;
3091 
3092 	if (type >= ARRAY_SIZE(uart_config))
3093 		type = 0;
3094 	return uart_config[type].name;
3095 }
3096 
3097 static const struct uart_ops serial8250_pops = {
3098 	.tx_empty	= serial8250_tx_empty,
3099 	.set_mctrl	= serial8250_set_mctrl,
3100 	.get_mctrl	= serial8250_get_mctrl,
3101 	.stop_tx	= serial8250_stop_tx,
3102 	.start_tx	= serial8250_start_tx,
3103 	.throttle	= serial8250_throttle,
3104 	.unthrottle	= serial8250_unthrottle,
3105 	.stop_rx	= serial8250_stop_rx,
3106 	.enable_ms	= serial8250_enable_ms,
3107 	.break_ctl	= serial8250_break_ctl,
3108 	.startup	= serial8250_startup,
3109 	.shutdown	= serial8250_shutdown,
3110 	.set_termios	= serial8250_set_termios,
3111 	.set_ldisc	= serial8250_set_ldisc,
3112 	.pm		= serial8250_pm,
3113 	.type		= serial8250_type,
3114 	.release_port	= serial8250_release_port,
3115 	.request_port	= serial8250_request_port,
3116 	.config_port	= serial8250_config_port,
3117 	.verify_port	= serial8250_verify_port,
3118 #ifdef CONFIG_CONSOLE_POLL
3119 	.poll_get_char = serial8250_get_poll_char,
3120 	.poll_put_char = serial8250_put_poll_char,
3121 #endif
3122 };
3123 
3124 static const struct uart_ops *base_ops;
3125 static struct uart_ops univ8250_port_ops;
3126 
3127 static const struct uart_8250_ops univ8250_driver_ops = {
3128 	.setup_irq	= univ8250_setup_irq,
3129 	.release_irq	= univ8250_release_irq,
3130 };
3131 
3132 static struct uart_8250_port serial8250_ports[UART_NR];
3133 
3134 /**
3135  * serial8250_get_port - retrieve struct uart_8250_port
3136  * @line: serial line number
3137  *
3138  * This function retrieves struct uart_8250_port for the specific line.
3139  * This struct *must* *not* be used to perform a 8250 or serial core operation
3140  * which is not accessible otherwise. Its only purpose is to make the struct
3141  * accessible to the runtime-pm callbacks for context suspend/restore.
3142  * The lock assumption made here is none because runtime-pm suspend/resume
3143  * callbacks should not be invoked if there is any operation performed on the
3144  * port.
3145  */
serial8250_get_port(int line)3146 struct uart_8250_port *serial8250_get_port(int line)
3147 {
3148 	return &serial8250_ports[line];
3149 }
3150 EXPORT_SYMBOL_GPL(serial8250_get_port);
3151 
3152 static void (*serial8250_isa_config)(int port, struct uart_port *up,
3153 	unsigned short *capabilities);
3154 
serial8250_set_isa_configurator(void (* v)(int port,struct uart_port * up,unsigned short * capabilities))3155 void serial8250_set_isa_configurator(
3156 	void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
3157 {
3158 	serial8250_isa_config = v;
3159 }
3160 EXPORT_SYMBOL(serial8250_set_isa_configurator);
3161 
serial8250_init_port(struct uart_8250_port * up)3162 static void serial8250_init_port(struct uart_8250_port *up)
3163 {
3164 	struct uart_port *port = &up->port;
3165 
3166 	spin_lock_init(&port->lock);
3167 	port->ops = &serial8250_pops;
3168 
3169 	up->cur_iotype = 0xFF;
3170 }
3171 
serial8250_set_defaults(struct uart_8250_port * up)3172 static void serial8250_set_defaults(struct uart_8250_port *up)
3173 {
3174 	struct uart_port *port = &up->port;
3175 
3176 	if (up->port.flags & UPF_FIXED_TYPE) {
3177 		unsigned int type = up->port.type;
3178 
3179 		if (!up->port.fifosize)
3180 			up->port.fifosize = uart_config[type].fifo_size;
3181 		if (!up->tx_loadsz)
3182 			up->tx_loadsz = uart_config[type].tx_loadsz;
3183 		if (!up->capabilities)
3184 			up->capabilities = uart_config[type].flags;
3185 	}
3186 
3187 	set_io_from_upio(port);
3188 
3189 	/* default dma handlers */
3190 	if (up->dma) {
3191 		if (!up->dma->tx_dma)
3192 			up->dma->tx_dma = serial8250_tx_dma;
3193 		if (!up->dma->rx_dma)
3194 			up->dma->rx_dma = serial8250_rx_dma;
3195 	}
3196 }
3197 
3198 #ifdef CONFIG_SERIAL_8250_RSA
3199 
univ8250_config_port(struct uart_port * port,int flags)3200 static void univ8250_config_port(struct uart_port *port, int flags)
3201 {
3202 	struct uart_8250_port *up = up_to_u8250p(port);
3203 
3204 	up->probe &= ~UART_PROBE_RSA;
3205 	if (port->type == PORT_RSA) {
3206 		if (serial8250_request_rsa_resource(up) == 0)
3207 			up->probe |= UART_PROBE_RSA;
3208 	} else if (flags & UART_CONFIG_TYPE) {
3209 		int i;
3210 
3211 		for (i = 0; i < probe_rsa_count; i++) {
3212 			if (probe_rsa[i] == up->port.iobase) {
3213 				if (serial8250_request_rsa_resource(up) == 0)
3214 					up->probe |= UART_PROBE_RSA;
3215 				break;
3216 			}
3217 		}
3218 	}
3219 
3220 	base_ops->config_port(port, flags);
3221 
3222 	if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
3223 		serial8250_release_rsa_resource(up);
3224 }
3225 
univ8250_request_port(struct uart_port * port)3226 static int univ8250_request_port(struct uart_port *port)
3227 {
3228 	struct uart_8250_port *up = up_to_u8250p(port);
3229 	int ret;
3230 
3231 	ret = base_ops->request_port(port);
3232 	if (ret == 0 && port->type == PORT_RSA) {
3233 		ret = serial8250_request_rsa_resource(up);
3234 		if (ret < 0)
3235 			base_ops->release_port(port);
3236 	}
3237 
3238 	return ret;
3239 }
3240 
univ8250_release_port(struct uart_port * port)3241 static void univ8250_release_port(struct uart_port *port)
3242 {
3243 	struct uart_8250_port *up = up_to_u8250p(port);
3244 
3245 	if (port->type == PORT_RSA)
3246 		serial8250_release_rsa_resource(up);
3247 	base_ops->release_port(port);
3248 }
3249 
univ8250_rsa_support(struct uart_ops * ops)3250 static void univ8250_rsa_support(struct uart_ops *ops)
3251 {
3252 	ops->config_port  = univ8250_config_port;
3253 	ops->request_port = univ8250_request_port;
3254 	ops->release_port = univ8250_release_port;
3255 }
3256 
3257 #else
3258 #define univ8250_rsa_support(x)		do { } while (0)
3259 #endif /* CONFIG_SERIAL_8250_RSA */
3260 
serial8250_isa_init_ports(void)3261 static void __init serial8250_isa_init_ports(void)
3262 {
3263 	struct uart_8250_port *up;
3264 	static int first = 1;
3265 	int i, irqflag = 0;
3266 
3267 	if (!first)
3268 		return;
3269 	first = 0;
3270 
3271 	if (nr_uarts > UART_NR)
3272 		nr_uarts = UART_NR;
3273 
3274 	for (i = 0; i < nr_uarts; i++) {
3275 		struct uart_8250_port *up = &serial8250_ports[i];
3276 		struct uart_port *port = &up->port;
3277 
3278 		port->line = i;
3279 		serial8250_init_port(up);
3280 		if (!base_ops)
3281 			base_ops = port->ops;
3282 		port->ops = &univ8250_port_ops;
3283 
3284 		init_timer(&up->timer);
3285 		up->timer.function = serial8250_timeout;
3286 
3287 		up->ops = &univ8250_driver_ops;
3288 
3289 		/*
3290 		 * ALPHA_KLUDGE_MCR needs to be killed.
3291 		 */
3292 		up->mcr_mask = ~ALPHA_KLUDGE_MCR;
3293 		up->mcr_force = ALPHA_KLUDGE_MCR;
3294 	}
3295 
3296 	/* chain base port ops to support Remote Supervisor Adapter */
3297 	univ8250_port_ops = *base_ops;
3298 	univ8250_rsa_support(&univ8250_port_ops);
3299 
3300 	if (share_irqs)
3301 		irqflag = IRQF_SHARED;
3302 
3303 	for (i = 0, up = serial8250_ports;
3304 	     i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
3305 	     i++, up++) {
3306 		struct uart_port *port = &up->port;
3307 
3308 		port->iobase   = old_serial_port[i].port;
3309 		port->irq      = irq_canonicalize(old_serial_port[i].irq);
3310 		port->irqflags = old_serial_port[i].irqflags;
3311 		port->uartclk  = old_serial_port[i].baud_base * 16;
3312 		port->flags    = old_serial_port[i].flags;
3313 		port->hub6     = old_serial_port[i].hub6;
3314 		port->membase  = old_serial_port[i].iomem_base;
3315 		port->iotype   = old_serial_port[i].io_type;
3316 		port->regshift = old_serial_port[i].iomem_reg_shift;
3317 		serial8250_set_defaults(up);
3318 
3319 		port->irqflags |= irqflag;
3320 		if (serial8250_isa_config != NULL)
3321 			serial8250_isa_config(i, &up->port, &up->capabilities);
3322 	}
3323 }
3324 
3325 static void __init
serial8250_register_ports(struct uart_driver * drv,struct device * dev)3326 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
3327 {
3328 	int i;
3329 
3330 	for (i = 0; i < nr_uarts; i++) {
3331 		struct uart_8250_port *up = &serial8250_ports[i];
3332 
3333 		if (up->port.dev)
3334 			continue;
3335 
3336 		up->port.dev = dev;
3337 
3338 		if (skip_txen_test)
3339 			up->port.flags |= UPF_NO_TXEN_TEST;
3340 
3341 		uart_add_one_port(drv, &up->port);
3342 	}
3343 }
3344 
3345 #ifdef CONFIG_SERIAL_8250_CONSOLE
3346 
serial8250_console_putchar(struct uart_port * port,int ch)3347 static void serial8250_console_putchar(struct uart_port *port, int ch)
3348 {
3349 	struct uart_8250_port *up = up_to_u8250p(port);
3350 
3351 	wait_for_xmitr(up, UART_LSR_THRE);
3352 	serial_port_out(port, UART_TX, ch);
3353 }
3354 
3355 /*
3356  *	Print a string to the serial port trying not to disturb
3357  *	any possible real use of the port...
3358  *
3359  *	The console_lock must be held when we get here.
3360  */
serial8250_console_write(struct uart_8250_port * up,const char * s,unsigned int count)3361 static void serial8250_console_write(struct uart_8250_port *up, const char *s,
3362 				     unsigned int count)
3363 {
3364 	struct uart_port *port = &up->port;
3365 	unsigned long flags;
3366 	unsigned int ier;
3367 	int locked = 1;
3368 
3369 	touch_nmi_watchdog();
3370 
3371 	serial8250_rpm_get(up);
3372 
3373 	if (port->sysrq)
3374 		locked = 0;
3375 	else if (oops_in_progress)
3376 		locked = spin_trylock_irqsave(&port->lock, flags);
3377 	else
3378 		spin_lock_irqsave(&port->lock, flags);
3379 
3380 	/*
3381 	 *	First save the IER then disable the interrupts
3382 	 */
3383 	ier = serial_port_in(port, UART_IER);
3384 
3385 	if (up->capabilities & UART_CAP_UUE)
3386 		serial_port_out(port, UART_IER, UART_IER_UUE);
3387 	else
3388 		serial_port_out(port, UART_IER, 0);
3389 
3390 	/* check scratch reg to see if port powered off during system sleep */
3391 	if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) {
3392 		struct ktermios termios;
3393 		unsigned int baud, quot, frac = 0;
3394 
3395 		termios.c_cflag = port->cons->cflag;
3396 		if (port->state->port.tty && termios.c_cflag == 0)
3397 			termios.c_cflag = port->state->port.tty->termios.c_cflag;
3398 
3399 		baud = uart_get_baud_rate(port, &termios, NULL,
3400 					  port->uartclk / 16 / 0xffff,
3401 					  port->uartclk / 16);
3402 		quot = serial8250_get_divisor(up, baud, &frac);
3403 
3404 		serial8250_set_divisor(port, baud, quot, frac);
3405 		serial_port_out(port, UART_LCR, up->lcr);
3406 		serial_port_out(port, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
3407 
3408 		up->canary = 0;
3409 	}
3410 
3411 	uart_console_write(port, s, count, serial8250_console_putchar);
3412 
3413 	/*
3414 	 *	Finally, wait for transmitter to become empty
3415 	 *	and restore the IER
3416 	 */
3417 	wait_for_xmitr(up, BOTH_EMPTY);
3418 	serial_port_out(port, UART_IER, ier);
3419 
3420 	/*
3421 	 *	The receive handling will happen properly because the
3422 	 *	receive ready bit will still be set; it is not cleared
3423 	 *	on read.  However, modem control will not, we must
3424 	 *	call it if we have saved something in the saved flags
3425 	 *	while processing with interrupts off.
3426 	 */
3427 	if (up->msr_saved_flags)
3428 		serial8250_modem_status(up);
3429 
3430 	if (locked)
3431 		spin_unlock_irqrestore(&port->lock, flags);
3432 	serial8250_rpm_put(up);
3433 }
3434 
univ8250_console_write(struct console * co,const char * s,unsigned int count)3435 static void univ8250_console_write(struct console *co, const char *s,
3436 				   unsigned int count)
3437 {
3438 	struct uart_8250_port *up = &serial8250_ports[co->index];
3439 
3440 	serial8250_console_write(up, s, count);
3441 }
3442 
probe_baud(struct uart_port * port)3443 static unsigned int probe_baud(struct uart_port *port)
3444 {
3445 	unsigned char lcr, dll, dlm;
3446 	unsigned int quot;
3447 
3448 	lcr = serial_port_in(port, UART_LCR);
3449 	serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB);
3450 	dll = serial_port_in(port, UART_DLL);
3451 	dlm = serial_port_in(port, UART_DLM);
3452 	serial_port_out(port, UART_LCR, lcr);
3453 
3454 	quot = (dlm << 8) | dll;
3455 	return (port->uartclk / 16) / quot;
3456 }
3457 
serial8250_console_setup(struct uart_port * port,char * options,bool probe)3458 static int serial8250_console_setup(struct uart_port *port, char *options, bool probe)
3459 {
3460 	int baud = 9600;
3461 	int bits = 8;
3462 	int parity = 'n';
3463 	int flow = 'n';
3464 
3465 	if (!port->iobase && !port->membase)
3466 		return -ENODEV;
3467 
3468 	if (options)
3469 		uart_parse_options(options, &baud, &parity, &bits, &flow);
3470 	else if (probe)
3471 		baud = probe_baud(port);
3472 
3473 	return uart_set_options(port, port->cons, baud, parity, bits, flow);
3474 }
3475 
univ8250_console_setup(struct console * co,char * options)3476 static int univ8250_console_setup(struct console *co, char *options)
3477 {
3478 	struct uart_port *port;
3479 
3480 	/*
3481 	 * Check whether an invalid uart number has been specified, and
3482 	 * if so, search for the first available port that does have
3483 	 * console support.
3484 	 */
3485 	if (co->index >= nr_uarts)
3486 		co->index = 0;
3487 	port = &serial8250_ports[co->index].port;
3488 	/* link port to console */
3489 	port->cons = co;
3490 
3491 	return serial8250_console_setup(port, options, false);
3492 }
3493 
3494 /**
3495  *	univ8250_console_match - non-standard console matching
3496  *	@co:	  registering console
3497  *	@name:	  name from console command line
3498  *	@idx:	  index from console command line
3499  *	@options: ptr to option string from console command line
3500  *
3501  *	Only attempts to match console command lines of the form:
3502  *	    console=uart[8250],io|mmio|mmio32,<addr>[,<options>]
3503  *	    console=uart[8250],0x<addr>[,<options>]
3504  *	This form is used to register an initial earlycon boot console and
3505  *	replace it with the serial8250_console at 8250 driver init.
3506  *
3507  *	Performs console setup for a match (as required by interface)
3508  *	If no <options> are specified, then assume the h/w is already setup.
3509  *
3510  *	Returns 0 if console matches; otherwise non-zero to use default matching
3511  */
univ8250_console_match(struct console * co,char * name,int idx,char * options)3512 static int univ8250_console_match(struct console *co, char *name, int idx,
3513 				  char *options)
3514 {
3515 	char match[] = "uart";	/* 8250-specific earlycon name */
3516 	unsigned char iotype;
3517 	unsigned long addr;
3518 	int i;
3519 
3520 	if (strncmp(name, match, 4) != 0)
3521 		return -ENODEV;
3522 
3523 	if (uart_parse_earlycon(options, &iotype, &addr, &options))
3524 		return -ENODEV;
3525 
3526 	/* try to match the port specified on the command line */
3527 	for (i = 0; i < nr_uarts; i++) {
3528 		struct uart_port *port = &serial8250_ports[i].port;
3529 
3530 		if (port->iotype != iotype)
3531 			continue;
3532 		if ((iotype == UPIO_MEM || iotype == UPIO_MEM32) &&
3533 		    (port->mapbase != addr))
3534 			continue;
3535 		if (iotype == UPIO_PORT && port->iobase != addr)
3536 			continue;
3537 
3538 		co->index = i;
3539 		port->cons = co;
3540 		return serial8250_console_setup(port, options, true);
3541 	}
3542 
3543 	return -ENODEV;
3544 }
3545 
3546 static struct console univ8250_console = {
3547 	.name		= "ttyS",
3548 	.write		= univ8250_console_write,
3549 	.device		= uart_console_device,
3550 	.setup		= univ8250_console_setup,
3551 	.match		= univ8250_console_match,
3552 	.flags		= CON_PRINTBUFFER | CON_ANYTIME,
3553 	.index		= -1,
3554 	.data		= &serial8250_reg,
3555 };
3556 
univ8250_console_init(void)3557 static int __init univ8250_console_init(void)
3558 {
3559 	serial8250_isa_init_ports();
3560 	register_console(&univ8250_console);
3561 	return 0;
3562 }
3563 console_initcall(univ8250_console_init);
3564 
3565 #define SERIAL8250_CONSOLE	&univ8250_console
3566 #else
3567 #define SERIAL8250_CONSOLE	NULL
3568 #endif
3569 
3570 static struct uart_driver serial8250_reg = {
3571 	.owner			= THIS_MODULE,
3572 	.driver_name		= "serial",
3573 	.dev_name		= "ttyS",
3574 	.major			= TTY_MAJOR,
3575 	.minor			= 64,
3576 	.cons			= SERIAL8250_CONSOLE,
3577 };
3578 
3579 /*
3580  * early_serial_setup - early registration for 8250 ports
3581  *
3582  * Setup an 8250 port structure prior to console initialisation.  Use
3583  * after console initialisation will cause undefined behaviour.
3584  */
early_serial_setup(struct uart_port * port)3585 int __init early_serial_setup(struct uart_port *port)
3586 {
3587 	struct uart_port *p;
3588 
3589 	if (port->line >= ARRAY_SIZE(serial8250_ports))
3590 		return -ENODEV;
3591 
3592 	serial8250_isa_init_ports();
3593 	p = &serial8250_ports[port->line].port;
3594 	p->iobase       = port->iobase;
3595 	p->membase      = port->membase;
3596 	p->irq          = port->irq;
3597 	p->irqflags     = port->irqflags;
3598 	p->uartclk      = port->uartclk;
3599 	p->fifosize     = port->fifosize;
3600 	p->regshift     = port->regshift;
3601 	p->iotype       = port->iotype;
3602 	p->flags        = port->flags;
3603 	p->mapbase      = port->mapbase;
3604 	p->mapsize      = port->mapsize;
3605 	p->private_data = port->private_data;
3606 	p->type		= port->type;
3607 	p->line		= port->line;
3608 
3609 	serial8250_set_defaults(up_to_u8250p(p));
3610 
3611 	if (port->serial_in)
3612 		p->serial_in = port->serial_in;
3613 	if (port->serial_out)
3614 		p->serial_out = port->serial_out;
3615 	if (port->handle_irq)
3616 		p->handle_irq = port->handle_irq;
3617 
3618 	return 0;
3619 }
3620 
3621 /**
3622  *	serial8250_suspend_port - suspend one serial port
3623  *	@line:  serial line number
3624  *
3625  *	Suspend one serial port.
3626  */
serial8250_suspend_port(int line)3627 void serial8250_suspend_port(int line)
3628 {
3629 	struct uart_8250_port *up = &serial8250_ports[line];
3630 	struct uart_port *port = &up->port;
3631 
3632 	if (!console_suspend_enabled && uart_console(port) &&
3633 	    port->type != PORT_8250) {
3634 		unsigned char canary = 0xa5;
3635 		serial_out(up, UART_SCR, canary);
3636 		if (serial_in(up, UART_SCR) == canary)
3637 			up->canary = canary;
3638 	}
3639 
3640 	uart_suspend_port(&serial8250_reg, port);
3641 }
3642 
3643 /**
3644  *	serial8250_resume_port - resume one serial port
3645  *	@line:  serial line number
3646  *
3647  *	Resume one serial port.
3648  */
serial8250_resume_port(int line)3649 void serial8250_resume_port(int line)
3650 {
3651 	struct uart_8250_port *up = &serial8250_ports[line];
3652 	struct uart_port *port = &up->port;
3653 
3654 	up->canary = 0;
3655 
3656 	if (up->capabilities & UART_NATSEMI) {
3657 		/* Ensure it's still in high speed mode */
3658 		serial_port_out(port, UART_LCR, 0xE0);
3659 
3660 		ns16550a_goto_highspeed(up);
3661 
3662 		serial_port_out(port, UART_LCR, 0);
3663 		port->uartclk = 921600*16;
3664 	}
3665 	uart_resume_port(&serial8250_reg, port);
3666 }
3667 
3668 /*
3669  * Register a set of serial devices attached to a platform device.  The
3670  * list is terminated with a zero flags entry, which means we expect
3671  * all entries to have at least UPF_BOOT_AUTOCONF set.
3672  */
serial8250_probe(struct platform_device * dev)3673 static int serial8250_probe(struct platform_device *dev)
3674 {
3675 	struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
3676 	struct uart_8250_port uart;
3677 	int ret, i, irqflag = 0;
3678 
3679 	memset(&uart, 0, sizeof(uart));
3680 
3681 	if (share_irqs)
3682 		irqflag = IRQF_SHARED;
3683 
3684 	for (i = 0; p && p->flags != 0; p++, i++) {
3685 		uart.port.iobase	= p->iobase;
3686 		uart.port.membase	= p->membase;
3687 		uart.port.irq		= p->irq;
3688 		uart.port.irqflags	= p->irqflags;
3689 		uart.port.uartclk	= p->uartclk;
3690 		uart.port.regshift	= p->regshift;
3691 		uart.port.iotype	= p->iotype;
3692 		uart.port.flags		= p->flags;
3693 		uart.port.mapbase	= p->mapbase;
3694 		uart.port.hub6		= p->hub6;
3695 		uart.port.private_data	= p->private_data;
3696 		uart.port.type		= p->type;
3697 		uart.port.serial_in	= p->serial_in;
3698 		uart.port.serial_out	= p->serial_out;
3699 		uart.port.handle_irq	= p->handle_irq;
3700 		uart.port.handle_break	= p->handle_break;
3701 		uart.port.set_termios	= p->set_termios;
3702 		uart.port.pm		= p->pm;
3703 		uart.port.dev		= &dev->dev;
3704 		uart.port.irqflags	|= irqflag;
3705 		ret = serial8250_register_8250_port(&uart);
3706 		if (ret < 0) {
3707 			dev_err(&dev->dev, "unable to register port at index %d "
3708 				"(IO%lx MEM%llx IRQ%d): %d\n", i,
3709 				p->iobase, (unsigned long long)p->mapbase,
3710 				p->irq, ret);
3711 		}
3712 	}
3713 	return 0;
3714 }
3715 
3716 /*
3717  * Remove serial ports registered against a platform device.
3718  */
serial8250_remove(struct platform_device * dev)3719 static int serial8250_remove(struct platform_device *dev)
3720 {
3721 	int i;
3722 
3723 	for (i = 0; i < nr_uarts; i++) {
3724 		struct uart_8250_port *up = &serial8250_ports[i];
3725 
3726 		if (up->port.dev == &dev->dev)
3727 			serial8250_unregister_port(i);
3728 	}
3729 	return 0;
3730 }
3731 
serial8250_suspend(struct platform_device * dev,pm_message_t state)3732 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
3733 {
3734 	int i;
3735 
3736 	for (i = 0; i < UART_NR; i++) {
3737 		struct uart_8250_port *up = &serial8250_ports[i];
3738 
3739 		if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
3740 			uart_suspend_port(&serial8250_reg, &up->port);
3741 	}
3742 
3743 	return 0;
3744 }
3745 
serial8250_resume(struct platform_device * dev)3746 static int serial8250_resume(struct platform_device *dev)
3747 {
3748 	int i;
3749 
3750 	for (i = 0; i < UART_NR; i++) {
3751 		struct uart_8250_port *up = &serial8250_ports[i];
3752 
3753 		if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
3754 			serial8250_resume_port(i);
3755 	}
3756 
3757 	return 0;
3758 }
3759 
3760 static struct platform_driver serial8250_isa_driver = {
3761 	.probe		= serial8250_probe,
3762 	.remove		= serial8250_remove,
3763 	.suspend	= serial8250_suspend,
3764 	.resume		= serial8250_resume,
3765 	.driver		= {
3766 		.name	= "serial8250",
3767 	},
3768 };
3769 
3770 /*
3771  * This "device" covers _all_ ISA 8250-compatible serial devices listed
3772  * in the table in include/asm/serial.h
3773  */
3774 static struct platform_device *serial8250_isa_devs;
3775 
3776 /*
3777  * serial8250_register_8250_port and serial8250_unregister_port allows for
3778  * 16x50 serial ports to be configured at run-time, to support PCMCIA
3779  * modems and PCI multiport cards.
3780  */
3781 static DEFINE_MUTEX(serial_mutex);
3782 
serial8250_find_match_or_unused(struct uart_port * port)3783 static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
3784 {
3785 	int i;
3786 
3787 	/*
3788 	 * First, find a port entry which matches.
3789 	 */
3790 	for (i = 0; i < nr_uarts; i++)
3791 		if (uart_match_port(&serial8250_ports[i].port, port))
3792 			return &serial8250_ports[i];
3793 
3794 	/* try line number first if still available */
3795 	i = port->line;
3796 	if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
3797 			serial8250_ports[i].port.iobase == 0)
3798 		return &serial8250_ports[i];
3799 	/*
3800 	 * We didn't find a matching entry, so look for the first
3801 	 * free entry.  We look for one which hasn't been previously
3802 	 * used (indicated by zero iobase).
3803 	 */
3804 	for (i = 0; i < nr_uarts; i++)
3805 		if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
3806 		    serial8250_ports[i].port.iobase == 0)
3807 			return &serial8250_ports[i];
3808 
3809 	/*
3810 	 * That also failed.  Last resort is to find any entry which
3811 	 * doesn't have a real port associated with it.
3812 	 */
3813 	for (i = 0; i < nr_uarts; i++)
3814 		if (serial8250_ports[i].port.type == PORT_UNKNOWN)
3815 			return &serial8250_ports[i];
3816 
3817 	return NULL;
3818 }
3819 
3820 /**
3821  *	serial8250_register_8250_port - register a serial port
3822  *	@up: serial port template
3823  *
3824  *	Configure the serial port specified by the request. If the
3825  *	port exists and is in use, it is hung up and unregistered
3826  *	first.
3827  *
3828  *	The port is then probed and if necessary the IRQ is autodetected
3829  *	If this fails an error is returned.
3830  *
3831  *	On success the port is ready to use and the line number is returned.
3832  */
serial8250_register_8250_port(struct uart_8250_port * up)3833 int serial8250_register_8250_port(struct uart_8250_port *up)
3834 {
3835 	struct uart_8250_port *uart;
3836 	int ret = -ENOSPC;
3837 
3838 	if (up->port.uartclk == 0)
3839 		return -EINVAL;
3840 
3841 	mutex_lock(&serial_mutex);
3842 
3843 	uart = serial8250_find_match_or_unused(&up->port);
3844 	if (uart && uart->port.type != PORT_8250_CIR) {
3845 		if (uart->port.dev)
3846 			uart_remove_one_port(&serial8250_reg, &uart->port);
3847 
3848 		uart->port.iobase       = up->port.iobase;
3849 		uart->port.membase      = up->port.membase;
3850 		uart->port.irq          = up->port.irq;
3851 		uart->port.irqflags     = up->port.irqflags;
3852 		uart->port.uartclk      = up->port.uartclk;
3853 		uart->port.fifosize     = up->port.fifosize;
3854 		uart->port.regshift     = up->port.regshift;
3855 		uart->port.iotype       = up->port.iotype;
3856 		uart->port.flags        = up->port.flags | UPF_BOOT_AUTOCONF;
3857 		uart->bugs		= up->bugs;
3858 		uart->port.mapbase      = up->port.mapbase;
3859 		uart->port.mapsize      = up->port.mapsize;
3860 		uart->port.private_data = up->port.private_data;
3861 		uart->port.fifosize	= up->port.fifosize;
3862 		uart->tx_loadsz		= up->tx_loadsz;
3863 		uart->capabilities	= up->capabilities;
3864 		uart->port.throttle	= up->port.throttle;
3865 		uart->port.unthrottle	= up->port.unthrottle;
3866 		uart->port.rs485_config	= up->port.rs485_config;
3867 		uart->port.rs485	= up->port.rs485;
3868 		uart->dma		= up->dma;
3869 
3870 		/* Take tx_loadsz from fifosize if it wasn't set separately */
3871 		if (uart->port.fifosize && !uart->tx_loadsz)
3872 			uart->tx_loadsz = uart->port.fifosize;
3873 
3874 		if (up->port.dev)
3875 			uart->port.dev = up->port.dev;
3876 
3877 		if (skip_txen_test)
3878 			uart->port.flags |= UPF_NO_TXEN_TEST;
3879 
3880 		if (up->port.flags & UPF_FIXED_TYPE)
3881 			uart->port.type = up->port.type;
3882 
3883 		serial8250_set_defaults(uart);
3884 
3885 		/* Possibly override default I/O functions.  */
3886 		if (up->port.serial_in)
3887 			uart->port.serial_in = up->port.serial_in;
3888 		if (up->port.serial_out)
3889 			uart->port.serial_out = up->port.serial_out;
3890 		if (up->port.handle_irq)
3891 			uart->port.handle_irq = up->port.handle_irq;
3892 		/*  Possibly override set_termios call */
3893 		if (up->port.set_termios)
3894 			uart->port.set_termios = up->port.set_termios;
3895 		if (up->port.set_mctrl)
3896 			uart->port.set_mctrl = up->port.set_mctrl;
3897 		if (up->port.startup)
3898 			uart->port.startup = up->port.startup;
3899 		if (up->port.shutdown)
3900 			uart->port.shutdown = up->port.shutdown;
3901 		if (up->port.pm)
3902 			uart->port.pm = up->port.pm;
3903 		if (up->port.handle_break)
3904 			uart->port.handle_break = up->port.handle_break;
3905 		if (up->dl_read)
3906 			uart->dl_read = up->dl_read;
3907 		if (up->dl_write)
3908 			uart->dl_write = up->dl_write;
3909 
3910 		if (serial8250_isa_config != NULL)
3911 			serial8250_isa_config(0, &uart->port,
3912 					&uart->capabilities);
3913 
3914 		ret = uart_add_one_port(&serial8250_reg, &uart->port);
3915 		if (ret == 0)
3916 			ret = uart->port.line;
3917 	}
3918 	mutex_unlock(&serial_mutex);
3919 
3920 	return ret;
3921 }
3922 EXPORT_SYMBOL(serial8250_register_8250_port);
3923 
3924 /**
3925  *	serial8250_unregister_port - remove a 16x50 serial port at runtime
3926  *	@line: serial line number
3927  *
3928  *	Remove one serial port.  This may not be called from interrupt
3929  *	context.  We hand the port back to the our control.
3930  */
serial8250_unregister_port(int line)3931 void serial8250_unregister_port(int line)
3932 {
3933 	struct uart_8250_port *uart = &serial8250_ports[line];
3934 
3935 	mutex_lock(&serial_mutex);
3936 	uart_remove_one_port(&serial8250_reg, &uart->port);
3937 	if (serial8250_isa_devs) {
3938 		uart->port.flags &= ~UPF_BOOT_AUTOCONF;
3939 		if (skip_txen_test)
3940 			uart->port.flags |= UPF_NO_TXEN_TEST;
3941 		uart->port.type = PORT_UNKNOWN;
3942 		uart->port.dev = &serial8250_isa_devs->dev;
3943 		uart->capabilities = 0;
3944 		uart_add_one_port(&serial8250_reg, &uart->port);
3945 	} else {
3946 		uart->port.dev = NULL;
3947 	}
3948 	mutex_unlock(&serial_mutex);
3949 }
3950 EXPORT_SYMBOL(serial8250_unregister_port);
3951 
serial8250_init(void)3952 static int __init serial8250_init(void)
3953 {
3954 	int ret;
3955 
3956 	serial8250_isa_init_ports();
3957 
3958 	printk(KERN_INFO "Serial: 8250/16550 driver, "
3959 		"%d ports, IRQ sharing %sabled\n", nr_uarts,
3960 		share_irqs ? "en" : "dis");
3961 
3962 #ifdef CONFIG_SPARC
3963 	ret = sunserial_register_minors(&serial8250_reg, UART_NR);
3964 #else
3965 	serial8250_reg.nr = UART_NR;
3966 	ret = uart_register_driver(&serial8250_reg);
3967 #endif
3968 	if (ret)
3969 		goto out;
3970 
3971 	ret = serial8250_pnp_init();
3972 	if (ret)
3973 		goto unreg_uart_drv;
3974 
3975 	serial8250_isa_devs = platform_device_alloc("serial8250",
3976 						    PLAT8250_DEV_LEGACY);
3977 	if (!serial8250_isa_devs) {
3978 		ret = -ENOMEM;
3979 		goto unreg_pnp;
3980 	}
3981 
3982 	ret = platform_device_add(serial8250_isa_devs);
3983 	if (ret)
3984 		goto put_dev;
3985 
3986 	serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
3987 
3988 	ret = platform_driver_register(&serial8250_isa_driver);
3989 	if (ret == 0)
3990 		goto out;
3991 
3992 	platform_device_del(serial8250_isa_devs);
3993 put_dev:
3994 	platform_device_put(serial8250_isa_devs);
3995 unreg_pnp:
3996 	serial8250_pnp_exit();
3997 unreg_uart_drv:
3998 #ifdef CONFIG_SPARC
3999 	sunserial_unregister_minors(&serial8250_reg, UART_NR);
4000 #else
4001 	uart_unregister_driver(&serial8250_reg);
4002 #endif
4003 out:
4004 	return ret;
4005 }
4006 
serial8250_exit(void)4007 static void __exit serial8250_exit(void)
4008 {
4009 	struct platform_device *isa_dev = serial8250_isa_devs;
4010 
4011 	/*
4012 	 * This tells serial8250_unregister_port() not to re-register
4013 	 * the ports (thereby making serial8250_isa_driver permanently
4014 	 * in use.)
4015 	 */
4016 	serial8250_isa_devs = NULL;
4017 
4018 	platform_driver_unregister(&serial8250_isa_driver);
4019 	platform_device_unregister(isa_dev);
4020 
4021 	serial8250_pnp_exit();
4022 
4023 #ifdef CONFIG_SPARC
4024 	sunserial_unregister_minors(&serial8250_reg, UART_NR);
4025 #else
4026 	uart_unregister_driver(&serial8250_reg);
4027 #endif
4028 }
4029 
4030 module_init(serial8250_init);
4031 module_exit(serial8250_exit);
4032 
4033 EXPORT_SYMBOL(serial8250_suspend_port);
4034 EXPORT_SYMBOL(serial8250_resume_port);
4035 
4036 MODULE_LICENSE("GPL");
4037 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
4038 
4039 module_param(share_irqs, uint, 0644);
4040 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
4041 	" (unsafe)");
4042 
4043 module_param(nr_uarts, uint, 0644);
4044 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
4045 
4046 module_param(skip_txen_test, uint, 0644);
4047 MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
4048 
4049 #ifdef CONFIG_SERIAL_8250_RSA
4050 module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
4051 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
4052 #endif
4053 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
4054 
4055 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
4056 #ifndef MODULE
4057 /* This module was renamed to 8250_core in 3.7.  Keep the old "8250" name
4058  * working as well for the module options so we don't break people.  We
4059  * need to keep the names identical and the convenient macros will happily
4060  * refuse to let us do that by failing the build with redefinition errors
4061  * of global variables.  So we stick them inside a dummy function to avoid
4062  * those conflicts.  The options still get parsed, and the redefined
4063  * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
4064  *
4065  * This is hacky.  I'm sorry.
4066  */
s8250_options(void)4067 static void __used s8250_options(void)
4068 {
4069 #undef MODULE_PARAM_PREFIX
4070 #define MODULE_PARAM_PREFIX "8250_core."
4071 
4072 	module_param_cb(share_irqs, &param_ops_uint, &share_irqs, 0644);
4073 	module_param_cb(nr_uarts, &param_ops_uint, &nr_uarts, 0644);
4074 	module_param_cb(skip_txen_test, &param_ops_uint, &skip_txen_test, 0644);
4075 #ifdef CONFIG_SERIAL_8250_RSA
4076 	__module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
4077 		&param_array_ops, .arr = &__param_arr_probe_rsa,
4078 		0444, -1, 0);
4079 #endif
4080 }
4081 #else
4082 MODULE_ALIAS("8250_core");
4083 #endif
4084 #endif
4085