blob: 09ef57034c9c75b217ac8a9c395512ddb5b72e1a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/8250.c
3 *
4 * Driver for 8250/16550-type serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright (C) 2001 Russell King.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * A note about mapbase / membase
16 *
17 * mapbase is the physical address of the IO port.
18 * membase is an 'ioremapped' cookie.
19 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
22#define SUPPORT_SYSRQ
23#endif
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/ioport.h>
28#include <linux/init.h>
29#include <linux/console.h>
30#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/delay.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010032#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/serial_reg.h>
36#include <linux/serial_core.h>
37#include <linux/serial.h>
38#include <linux/serial_8250.h>
Andrew Morton78512ec2005-11-07 00:59:13 -080039#include <linux/nmi.h>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000040#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/io.h>
44#include <asm/irq.h>
45
46#include "8250.h"
47
David Millerb70ac772008-10-13 10:36:31 +010048#ifdef CONFIG_SPARC
49#include "suncore.h"
50#endif
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * Configuration:
Thomas Gleixner40663cc2006-07-01 19:29:43 -070054 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * is unsafe when used on edge-triggered interrupts.
56 */
Adrian Bunk408b6642005-05-01 08:59:29 -070057static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Dave Jonesa61c2d72006-01-07 23:18:19 +000059static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
60
David S. Miller84408382008-10-13 10:45:26 +010061static struct uart_driver serial8250_reg;
62
63static int serial_index(struct uart_port *port)
64{
65 return (serial8250_reg.minor - 64) + port->line;
66}
67
Chuck Ebbertd41a4b52009-10-01 15:44:26 -070068static unsigned int skip_txen_test; /* force skip of txen test at init time */
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
71 * Debugging.
72 */
73#if 0
74#define DEBUG_AUTOCONF(fmt...) printk(fmt)
75#else
76#define DEBUG_AUTOCONF(fmt...) do { } while (0)
77#endif
78
79#if 0
80#define DEBUG_INTR(fmt...) printk(fmt)
81#else
82#define DEBUG_INTR(fmt...) do { } while (0)
83#endif
84
85#define PASS_LIMIT 256
86
Dick Hollenbeckbca47612009-12-09 12:31:34 -080087#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
88
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
91 * We default to IRQ0 for the "no irq" hack. Some
92 * machine types want others as well - they're free
93 * to redefine this in their header file.
94 */
95#define is_real_interrupt(irq) ((irq) != 0)
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
98#define CONFIG_SERIAL_DETECT_IRQ 1
99#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#ifdef CONFIG_SERIAL_8250_MANY_PORTS
101#define CONFIG_SERIAL_MANY_PORTS 1
102#endif
103
104/*
105 * HUB6 is always on. This will be removed once the header
106 * files have been cleaned.
107 */
108#define CONFIG_HUB6 1
109
Bryan Wua4ed1e42008-05-31 16:10:04 +0800110#include <asm/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/*
112 * SERIAL_PORT_DFNS tells us about built-in ports that have no
113 * standard enumeration mechanism. Platforms that can find all
114 * serial ports via mechanisms like ACPI or PCI need not supply it.
115 */
116#ifndef SERIAL_PORT_DFNS
117#define SERIAL_PORT_DFNS
118#endif
119
Arjan van de Vencb3592b2005-11-28 21:04:11 +0000120static const struct old_serial_port old_serial_port[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 SERIAL_PORT_DFNS /* defined in asm/serial.h */
122};
123
Russell King026d02a2005-06-29 18:45:19 +0100124#define UART_NR CONFIG_SERIAL_8250_NR_UARTS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126#ifdef CONFIG_SERIAL_8250_RSA
127
128#define PORT_RSA_MAX 4
129static unsigned long probe_rsa[PORT_RSA_MAX];
130static unsigned int probe_rsa_count;
131#endif /* CONFIG_SERIAL_8250_RSA */
132
133struct uart_8250_port {
134 struct uart_port port;
135 struct timer_list timer; /* "no irq" timer */
136 struct list_head list; /* ports on this IRQ */
Russell King4ba5e352005-06-23 10:43:04 +0100137 unsigned short capabilities; /* port capabilities */
138 unsigned short bugs; /* port bugs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 unsigned int tx_loadsz; /* transmit fifo load size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 unsigned char acr;
141 unsigned char ier;
142 unsigned char lcr;
143 unsigned char mcr;
144 unsigned char mcr_mask; /* mask of user bits */
145 unsigned char mcr_force; /* mask of forced bits */
Alan Coxb8e7e402009-05-28 14:01:35 +0100146 unsigned char cur_iotype; /* Running I/O type */
Corey Minyardad4c2aa2007-08-22 14:01:18 -0700147
148 /*
149 * Some bits in registers are cleared on a read, so they must
150 * be saved whenever the register is read but the bits will not
151 * be immediately processed.
152 */
153#define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITS
154 unsigned char lsr_saved_flags;
155#define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
156 unsigned char msr_saved_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 /*
159 * We provide a per-port pm hook.
160 */
161 void (*pm)(struct uart_port *port,
162 unsigned int state, unsigned int old);
163};
164
165struct irq_info {
Alan Cox25db8ad2008-08-19 20:49:40 -0700166 struct hlist_node node;
167 int irq;
168 spinlock_t lock; /* Protects list not the hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 struct list_head *head;
170};
171
Alan Cox25db8ad2008-08-19 20:49:40 -0700172#define NR_IRQ_HASH 32 /* Can be adjusted later */
173static struct hlist_head irq_lists[NR_IRQ_HASH];
174static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/*
177 * Here we define the default xmit fifo size used for each type of UART.
178 */
179static const struct serial8250_config uart_config[] = {
180 [PORT_UNKNOWN] = {
181 .name = "unknown",
182 .fifo_size = 1,
183 .tx_loadsz = 1,
184 },
185 [PORT_8250] = {
186 .name = "8250",
187 .fifo_size = 1,
188 .tx_loadsz = 1,
189 },
190 [PORT_16450] = {
191 .name = "16450",
192 .fifo_size = 1,
193 .tx_loadsz = 1,
194 },
195 [PORT_16550] = {
196 .name = "16550",
197 .fifo_size = 1,
198 .tx_loadsz = 1,
199 },
200 [PORT_16550A] = {
201 .name = "16550A",
202 .fifo_size = 16,
203 .tx_loadsz = 16,
204 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
205 .flags = UART_CAP_FIFO,
206 },
207 [PORT_CIRRUS] = {
208 .name = "Cirrus",
209 .fifo_size = 1,
210 .tx_loadsz = 1,
211 },
212 [PORT_16650] = {
213 .name = "ST16650",
214 .fifo_size = 1,
215 .tx_loadsz = 1,
216 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
217 },
218 [PORT_16650V2] = {
219 .name = "ST16650V2",
220 .fifo_size = 32,
221 .tx_loadsz = 16,
222 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
223 UART_FCR_T_TRIG_00,
224 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
225 },
226 [PORT_16750] = {
227 .name = "TI16750",
228 .fifo_size = 64,
229 .tx_loadsz = 64,
230 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
231 UART_FCR7_64BYTE,
232 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
233 },
234 [PORT_STARTECH] = {
235 .name = "Startech",
236 .fifo_size = 1,
237 .tx_loadsz = 1,
238 },
239 [PORT_16C950] = {
240 .name = "16C950/954",
241 .fifo_size = 128,
242 .tx_loadsz = 128,
243 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
244 .flags = UART_CAP_FIFO,
245 },
246 [PORT_16654] = {
247 .name = "ST16654",
248 .fifo_size = 64,
249 .tx_loadsz = 32,
250 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
251 UART_FCR_T_TRIG_10,
252 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
253 },
254 [PORT_16850] = {
255 .name = "XR16850",
256 .fifo_size = 128,
257 .tx_loadsz = 128,
258 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
259 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
260 },
261 [PORT_RSA] = {
262 .name = "RSA",
263 .fifo_size = 2048,
264 .tx_loadsz = 2048,
265 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
266 .flags = UART_CAP_FIFO,
267 },
268 [PORT_NS16550A] = {
269 .name = "NS16550A",
270 .fifo_size = 16,
271 .tx_loadsz = 16,
272 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
273 .flags = UART_CAP_FIFO | UART_NATSEMI,
274 },
275 [PORT_XSCALE] = {
276 .name = "XScale",
277 .fifo_size = 32,
278 .tx_loadsz = 32,
279 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
280 .flags = UART_CAP_FIFO | UART_CAP_UUE,
281 },
Thomas Koellerbd71c182007-05-06 14:48:47 -0700282 [PORT_RM9000] = {
283 .name = "RM9000",
284 .fifo_size = 16,
285 .tx_loadsz = 16,
286 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
287 .flags = UART_CAP_FIFO,
288 },
David Daney6b06f192009-01-02 13:50:00 +0000289 [PORT_OCTEON] = {
290 .name = "OCTEON",
291 .fifo_size = 64,
292 .tx_loadsz = 64,
293 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
294 .flags = UART_CAP_FIFO,
295 },
Florian Fainelli08e09922009-06-11 13:21:24 +0100296 [PORT_AR7] = {
297 .name = "AR7",
298 .fifo_size = 16,
299 .tx_loadsz = 16,
300 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
301 .flags = UART_CAP_FIFO | UART_CAP_AFE,
302 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303};
304
Manuel Lauss12bf3f22010-07-15 21:45:05 +0200305#if defined(CONFIG_MIPS_ALCHEMY)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000306
307/* Au1x00 UART hardware has a weird register layout */
308static const u8 au_io_in_map[] = {
309 [UART_RX] = 0,
310 [UART_IER] = 2,
311 [UART_IIR] = 3,
312 [UART_LCR] = 5,
313 [UART_MCR] = 6,
314 [UART_LSR] = 7,
315 [UART_MSR] = 8,
316};
317
318static const u8 au_io_out_map[] = {
319 [UART_TX] = 1,
320 [UART_IER] = 2,
321 [UART_FCR] = 4,
322 [UART_LCR] = 5,
323 [UART_MCR] = 6,
324};
325
326/* sane hardware needs no mapping */
David Daney7d6a07d2009-01-02 13:49:47 +0000327static inline int map_8250_in_reg(struct uart_port *p, int offset)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000328{
David Daney7d6a07d2009-01-02 13:49:47 +0000329 if (p->iotype != UPIO_AU)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000330 return offset;
331 return au_io_in_map[offset];
332}
333
David Daney7d6a07d2009-01-02 13:49:47 +0000334static inline int map_8250_out_reg(struct uart_port *p, int offset)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000335{
David Daney7d6a07d2009-01-02 13:49:47 +0000336 if (p->iotype != UPIO_AU)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000337 return offset;
338 return au_io_out_map[offset];
339}
340
Alan Cox6f803cd2008-02-08 04:18:52 -0800341#elif defined(CONFIG_SERIAL_8250_RM9K)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700342
343static const u8
344 regmap_in[8] = {
345 [UART_RX] = 0x00,
346 [UART_IER] = 0x0c,
347 [UART_IIR] = 0x14,
348 [UART_LCR] = 0x1c,
349 [UART_MCR] = 0x20,
350 [UART_LSR] = 0x24,
351 [UART_MSR] = 0x28,
352 [UART_SCR] = 0x2c
353 },
354 regmap_out[8] = {
355 [UART_TX] = 0x04,
356 [UART_IER] = 0x0c,
357 [UART_FCR] = 0x18,
358 [UART_LCR] = 0x1c,
359 [UART_MCR] = 0x20,
360 [UART_LSR] = 0x24,
361 [UART_MSR] = 0x28,
362 [UART_SCR] = 0x2c
363 };
364
David Daney7d6a07d2009-01-02 13:49:47 +0000365static inline int map_8250_in_reg(struct uart_port *p, int offset)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700366{
David Daney7d6a07d2009-01-02 13:49:47 +0000367 if (p->iotype != UPIO_RM9000)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700368 return offset;
369 return regmap_in[offset];
370}
371
David Daney7d6a07d2009-01-02 13:49:47 +0000372static inline int map_8250_out_reg(struct uart_port *p, int offset)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700373{
David Daney7d6a07d2009-01-02 13:49:47 +0000374 if (p->iotype != UPIO_RM9000)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700375 return offset;
376 return regmap_out[offset];
377}
378
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000379#else
380
381/* sane hardware needs no mapping */
382#define map_8250_in_reg(up, offset) (offset)
383#define map_8250_out_reg(up, offset) (offset)
384
385#endif
386
David Daney7d6a07d2009-01-02 13:49:47 +0000387static unsigned int hub6_serial_in(struct uart_port *p, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
David Daney7d6a07d2009-01-02 13:49:47 +0000389 offset = map_8250_in_reg(p, offset) << p->regshift;
390 outb(p->hub6 - 1 + offset, p->iobase);
391 return inb(p->iobase + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
David Daney7d6a07d2009-01-02 13:49:47 +0000394static void hub6_serial_out(struct uart_port *p, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
David Daney7d6a07d2009-01-02 13:49:47 +0000396 offset = map_8250_out_reg(p, offset) << p->regshift;
397 outb(p->hub6 - 1 + offset, p->iobase);
398 outb(value, p->iobase + 1);
399}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
David Daney7d6a07d2009-01-02 13:49:47 +0000401static unsigned int mem_serial_in(struct uart_port *p, int offset)
402{
403 offset = map_8250_in_reg(p, offset) << p->regshift;
404 return readb(p->membase + offset);
405}
406
407static void mem_serial_out(struct uart_port *p, int offset, int value)
408{
409 offset = map_8250_out_reg(p, offset) << p->regshift;
410 writeb(value, p->membase + offset);
411}
412
413static void mem32_serial_out(struct uart_port *p, int offset, int value)
414{
415 offset = map_8250_out_reg(p, offset) << p->regshift;
416 writel(value, p->membase + offset);
417}
418
419static unsigned int mem32_serial_in(struct uart_port *p, int offset)
420{
421 offset = map_8250_in_reg(p, offset) << p->regshift;
422 return readl(p->membase + offset);
423}
424
David Daney7d6a07d2009-01-02 13:49:47 +0000425static unsigned int au_serial_in(struct uart_port *p, int offset)
426{
427 offset = map_8250_in_reg(p, offset) << p->regshift;
428 return __raw_readl(p->membase + offset);
429}
430
431static void au_serial_out(struct uart_port *p, int offset, int value)
432{
433 offset = map_8250_out_reg(p, offset) << p->regshift;
434 __raw_writel(value, p->membase + offset);
435}
David Daney7d6a07d2009-01-02 13:49:47 +0000436
437static unsigned int tsi_serial_in(struct uart_port *p, int offset)
438{
439 unsigned int tmp;
440 offset = map_8250_in_reg(p, offset) << p->regshift;
441 if (offset == UART_IIR) {
442 tmp = readl(p->membase + (UART_IIR & ~3));
443 return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
444 } else
445 return readb(p->membase + offset);
446}
447
448static void tsi_serial_out(struct uart_port *p, int offset, int value)
449{
450 offset = map_8250_out_reg(p, offset) << p->regshift;
451 if (!((offset == UART_IER) && (value & UART_IER_UUE)))
452 writeb(value, p->membase + offset);
453}
454
455static void dwapb_serial_out(struct uart_port *p, int offset, int value)
456{
457 int save_offset = offset;
458 offset = map_8250_out_reg(p, offset) << p->regshift;
459 /* Save the LCR value so it can be re-written when a
460 * Busy Detect interrupt occurs. */
461 if (save_offset == UART_LCR) {
462 struct uart_8250_port *up = (struct uart_8250_port *)p;
463 up->lcr = value;
464 }
465 writeb(value, p->membase + offset);
466 /* Read the IER to ensure any interrupt is cleared before
467 * returning from ISR. */
468 if (save_offset == UART_TX || save_offset == UART_IER)
469 value = p->serial_in(p, UART_IER);
470}
471
472static unsigned int io_serial_in(struct uart_port *p, int offset)
473{
474 offset = map_8250_in_reg(p, offset) << p->regshift;
475 return inb(p->iobase + offset);
476}
477
478static void io_serial_out(struct uart_port *p, int offset, int value)
479{
480 offset = map_8250_out_reg(p, offset) << p->regshift;
481 outb(value, p->iobase + offset);
482}
483
484static void set_io_from_upio(struct uart_port *p)
485{
Alan Coxb8e7e402009-05-28 14:01:35 +0100486 struct uart_8250_port *up = (struct uart_8250_port *)p;
David Daney7d6a07d2009-01-02 13:49:47 +0000487 switch (p->iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 case UPIO_HUB6:
David Daney7d6a07d2009-01-02 13:49:47 +0000489 p->serial_in = hub6_serial_in;
490 p->serial_out = hub6_serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 break;
492
493 case UPIO_MEM:
David Daney7d6a07d2009-01-02 13:49:47 +0000494 p->serial_in = mem_serial_in;
495 p->serial_out = mem_serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 break;
497
Thomas Koellerbd71c182007-05-06 14:48:47 -0700498 case UPIO_RM9000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 case UPIO_MEM32:
David Daney7d6a07d2009-01-02 13:49:47 +0000500 p->serial_in = mem32_serial_in;
501 p->serial_out = mem32_serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 break;
503
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000504 case UPIO_AU:
David Daney7d6a07d2009-01-02 13:49:47 +0000505 p->serial_in = au_serial_in;
506 p->serial_out = au_serial_out;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000507 break;
Manuel Lauss12bf3f22010-07-15 21:45:05 +0200508
Zang Roy-r619113be91ec2006-06-30 02:29:58 -0700509 case UPIO_TSI:
David Daney7d6a07d2009-01-02 13:49:47 +0000510 p->serial_in = tsi_serial_in;
511 p->serial_out = tsi_serial_out;
Zang Roy-r619113be91ec2006-06-30 02:29:58 -0700512 break;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000513
Marc St-Jeanbeab6972007-05-06 14:48:45 -0700514 case UPIO_DWAPB:
David Daney7d6a07d2009-01-02 13:49:47 +0000515 p->serial_in = mem_serial_in;
516 p->serial_out = dwapb_serial_out;
Marc St-Jeanbeab6972007-05-06 14:48:45 -0700517 break;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 default:
David Daney7d6a07d2009-01-02 13:49:47 +0000520 p->serial_in = io_serial_in;
521 p->serial_out = io_serial_out;
522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Alan Coxb8e7e402009-05-28 14:01:35 +0100524 /* Remember loaded iotype */
525 up->cur_iotype = p->iotype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Alex Williamson40b36da2007-02-14 00:33:04 -0800528static void
529serial_out_sync(struct uart_8250_port *up, int offset, int value)
530{
David Daney7d6a07d2009-01-02 13:49:47 +0000531 struct uart_port *p = &up->port;
532 switch (p->iotype) {
Alex Williamson40b36da2007-02-14 00:33:04 -0800533 case UPIO_MEM:
534 case UPIO_MEM32:
Alex Williamson40b36da2007-02-14 00:33:04 -0800535 case UPIO_AU:
Marc St-Jeanbeab6972007-05-06 14:48:45 -0700536 case UPIO_DWAPB:
David Daney7d6a07d2009-01-02 13:49:47 +0000537 p->serial_out(p, offset, value);
538 p->serial_in(p, UART_LCR); /* safe, no side-effects */
Alex Williamson40b36da2007-02-14 00:33:04 -0800539 break;
540 default:
David Daney7d6a07d2009-01-02 13:49:47 +0000541 p->serial_out(p, offset, value);
Alex Williamson40b36da2007-02-14 00:33:04 -0800542 }
543}
544
David Daney7d6a07d2009-01-02 13:49:47 +0000545#define serial_in(up, offset) \
546 (up->port.serial_in(&(up)->port, (offset)))
547#define serial_out(up, offset, value) \
548 (up->port.serial_out(&(up)->port, (offset), (value)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/*
550 * We used to support using pause I/O for certain machines. We
551 * haven't supported this for a while, but just in case it's badly
552 * needed for certain old 386 machines, I've left these #define's
553 * in....
554 */
555#define serial_inp(up, offset) serial_in(up, offset)
556#define serial_outp(up, offset, value) serial_out(up, offset, value)
557
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100558/* Uart divisor latch read */
559static inline int _serial_dl_read(struct uart_8250_port *up)
560{
561 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
562}
563
564/* Uart divisor latch write */
565static inline void _serial_dl_write(struct uart_8250_port *up, int value)
566{
567 serial_outp(up, UART_DLL, value & 0xff);
568 serial_outp(up, UART_DLM, value >> 8 & 0xff);
569}
570
Manuel Lauss12bf3f22010-07-15 21:45:05 +0200571#if defined(CONFIG_MIPS_ALCHEMY)
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100572/* Au1x00 haven't got a standard divisor latch */
573static int serial_dl_read(struct uart_8250_port *up)
574{
575 if (up->port.iotype == UPIO_AU)
576 return __raw_readl(up->port.membase + 0x28);
577 else
578 return _serial_dl_read(up);
579}
580
581static void serial_dl_write(struct uart_8250_port *up, int value)
582{
583 if (up->port.iotype == UPIO_AU)
584 __raw_writel(value, up->port.membase + 0x28);
585 else
586 _serial_dl_write(up, value);
587}
Alan Cox6f803cd2008-02-08 04:18:52 -0800588#elif defined(CONFIG_SERIAL_8250_RM9K)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700589static int serial_dl_read(struct uart_8250_port *up)
590{
591 return (up->port.iotype == UPIO_RM9000) ?
592 (((__raw_readl(up->port.membase + 0x10) << 8) |
593 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
594 _serial_dl_read(up);
595}
596
597static void serial_dl_write(struct uart_8250_port *up, int value)
598{
599 if (up->port.iotype == UPIO_RM9000) {
600 __raw_writel(value, up->port.membase + 0x08);
601 __raw_writel(value >> 8, up->port.membase + 0x10);
602 } else {
603 _serial_dl_write(up, value);
604 }
605}
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100606#else
607#define serial_dl_read(up) _serial_dl_read(up)
608#define serial_dl_write(up, value) _serial_dl_write(up, value)
609#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611/*
612 * For the 16C950
613 */
614static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
615{
616 serial_out(up, UART_SCR, offset);
617 serial_out(up, UART_ICR, value);
618}
619
620static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
621{
622 unsigned int value;
623
624 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
625 serial_out(up, UART_SCR, offset);
626 value = serial_in(up, UART_ICR);
627 serial_icr_write(up, UART_ACR, up->acr);
628
629 return value;
630}
631
632/*
633 * FIFO support.
634 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100635static void serial8250_clear_fifos(struct uart_8250_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 if (p->capabilities & UART_CAP_FIFO) {
638 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
639 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
640 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
641 serial_outp(p, UART_FCR, 0);
642 }
643}
644
645/*
646 * IER sleep support. UARTs which have EFRs need the "extended
647 * capability" bit enabled. Note that on XR16C850s, we need to
648 * reset LCR to write to IER.
649 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100650static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 if (p->capabilities & UART_CAP_SLEEP) {
653 if (p->capabilities & UART_CAP_EFR) {
654 serial_outp(p, UART_LCR, 0xBF);
655 serial_outp(p, UART_EFR, UART_EFR_ECB);
656 serial_outp(p, UART_LCR, 0);
657 }
658 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
659 if (p->capabilities & UART_CAP_EFR) {
660 serial_outp(p, UART_LCR, 0xBF);
661 serial_outp(p, UART_EFR, 0);
662 serial_outp(p, UART_LCR, 0);
663 }
664 }
665}
666
667#ifdef CONFIG_SERIAL_8250_RSA
668/*
669 * Attempts to turn on the RSA FIFO. Returns zero on failure.
670 * We set the port uart clock rate if we succeed.
671 */
672static int __enable_rsa(struct uart_8250_port *up)
673{
674 unsigned char mode;
675 int result;
676
677 mode = serial_inp(up, UART_RSA_MSR);
678 result = mode & UART_RSA_MSR_FIFO;
679
680 if (!result) {
681 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
682 mode = serial_inp(up, UART_RSA_MSR);
683 result = mode & UART_RSA_MSR_FIFO;
684 }
685
686 if (result)
687 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
688
689 return result;
690}
691
692static void enable_rsa(struct uart_8250_port *up)
693{
694 if (up->port.type == PORT_RSA) {
695 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
696 spin_lock_irq(&up->port.lock);
697 __enable_rsa(up);
698 spin_unlock_irq(&up->port.lock);
699 }
700 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
701 serial_outp(up, UART_RSA_FRR, 0);
702 }
703}
704
705/*
706 * Attempts to turn off the RSA FIFO. Returns zero on failure.
707 * It is unknown why interrupts were disabled in here. However,
708 * the caller is expected to preserve this behaviour by grabbing
709 * the spinlock before calling this function.
710 */
711static void disable_rsa(struct uart_8250_port *up)
712{
713 unsigned char mode;
714 int result;
715
716 if (up->port.type == PORT_RSA &&
717 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
718 spin_lock_irq(&up->port.lock);
719
720 mode = serial_inp(up, UART_RSA_MSR);
721 result = !(mode & UART_RSA_MSR_FIFO);
722
723 if (!result) {
724 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
725 mode = serial_inp(up, UART_RSA_MSR);
726 result = !(mode & UART_RSA_MSR_FIFO);
727 }
728
729 if (result)
730 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
731 spin_unlock_irq(&up->port.lock);
732 }
733}
734#endif /* CONFIG_SERIAL_8250_RSA */
735
736/*
737 * This is a quickie test to see how big the FIFO is.
738 * It doesn't work at all the time, more's the pity.
739 */
740static int size_fifo(struct uart_8250_port *up)
741{
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100742 unsigned char old_fcr, old_mcr, old_lcr;
743 unsigned short old_dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 int count;
745
746 old_lcr = serial_inp(up, UART_LCR);
747 serial_outp(up, UART_LCR, 0);
748 old_fcr = serial_inp(up, UART_FCR);
749 old_mcr = serial_inp(up, UART_MCR);
750 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
751 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
752 serial_outp(up, UART_MCR, UART_MCR_LOOP);
753 serial_outp(up, UART_LCR, UART_LCR_DLAB);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100754 old_dl = serial_dl_read(up);
755 serial_dl_write(up, 0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 serial_outp(up, UART_LCR, 0x03);
757 for (count = 0; count < 256; count++)
758 serial_outp(up, UART_TX, count);
759 mdelay(20);/* FIXME - schedule_timeout */
760 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
761 (count < 256); count++)
762 serial_inp(up, UART_RX);
763 serial_outp(up, UART_FCR, old_fcr);
764 serial_outp(up, UART_MCR, old_mcr);
765 serial_outp(up, UART_LCR, UART_LCR_DLAB);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100766 serial_dl_write(up, old_dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 serial_outp(up, UART_LCR, old_lcr);
768
769 return count;
770}
771
772/*
773 * Read UART ID using the divisor method - set DLL and DLM to zero
774 * and the revision will be in DLL and device type in DLM. We
775 * preserve the device state across this.
776 */
777static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
778{
779 unsigned char old_dll, old_dlm, old_lcr;
780 unsigned int id;
781
782 old_lcr = serial_inp(p, UART_LCR);
783 serial_outp(p, UART_LCR, UART_LCR_DLAB);
784
785 old_dll = serial_inp(p, UART_DLL);
786 old_dlm = serial_inp(p, UART_DLM);
787
788 serial_outp(p, UART_DLL, 0);
789 serial_outp(p, UART_DLM, 0);
790
791 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
792
793 serial_outp(p, UART_DLL, old_dll);
794 serial_outp(p, UART_DLM, old_dlm);
795 serial_outp(p, UART_LCR, old_lcr);
796
797 return id;
798}
799
800/*
801 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
802 * When this function is called we know it is at least a StarTech
803 * 16650 V2, but it might be one of several StarTech UARTs, or one of
804 * its clones. (We treat the broken original StarTech 16650 V1 as a
805 * 16550, and why not? Startech doesn't seem to even acknowledge its
806 * existence.)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700807 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * What evil have men's minds wrought...
809 */
810static void autoconfig_has_efr(struct uart_8250_port *up)
811{
812 unsigned int id1, id2, id3, rev;
813
814 /*
815 * Everything with an EFR has SLEEP
816 */
817 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
818
819 /*
820 * First we check to see if it's an Oxford Semiconductor UART.
821 *
822 * If we have to do this here because some non-National
823 * Semiconductor clone chips lock up if you try writing to the
824 * LSR register (which serial_icr_read does)
825 */
826
827 /*
828 * Check for Oxford Semiconductor 16C950.
829 *
830 * EFR [4] must be set else this test fails.
831 *
832 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
833 * claims that it's needed for 952 dual UART's (which are not
834 * recommended for new designs).
835 */
836 up->acr = 0;
837 serial_out(up, UART_LCR, 0xBF);
838 serial_out(up, UART_EFR, UART_EFR_ECB);
839 serial_out(up, UART_LCR, 0x00);
840 id1 = serial_icr_read(up, UART_ID1);
841 id2 = serial_icr_read(up, UART_ID2);
842 id3 = serial_icr_read(up, UART_ID3);
843 rev = serial_icr_read(up, UART_REV);
844
845 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
846
847 if (id1 == 0x16 && id2 == 0xC9 &&
848 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
849 up->port.type = PORT_16C950;
Russell King4ba5e352005-06-23 10:43:04 +0100850
851 /*
852 * Enable work around for the Oxford Semiconductor 952 rev B
853 * chip which causes it to seriously miscalculate baud rates
854 * when DLL is 0.
855 */
856 if (id3 == 0x52 && rev == 0x01)
857 up->bugs |= UART_BUG_QUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return;
859 }
Thomas Koellerbd71c182007-05-06 14:48:47 -0700860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /*
862 * We check for a XR16C850 by setting DLL and DLM to 0, and then
863 * reading back DLL and DLM. The chip type depends on the DLM
864 * value read back:
865 * 0x10 - XR16C850 and the DLL contains the chip revision.
866 * 0x12 - XR16C2850.
867 * 0x14 - XR16C854.
868 */
869 id1 = autoconfig_read_divisor_id(up);
870 DEBUG_AUTOCONF("850id=%04x ", id1);
871
872 id2 = id1 >> 8;
873 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 up->port.type = PORT_16850;
875 return;
876 }
877
878 /*
879 * It wasn't an XR16C850.
880 *
881 * We distinguish between the '654 and the '650 by counting
882 * how many bytes are in the FIFO. I'm using this for now,
883 * since that's the technique that was sent to me in the
884 * serial driver update, but I'm not convinced this works.
885 * I've had problems doing this in the past. -TYT
886 */
887 if (size_fifo(up) == 64)
888 up->port.type = PORT_16654;
889 else
890 up->port.type = PORT_16650V2;
891}
892
893/*
894 * We detected a chip without a FIFO. Only two fall into
895 * this category - the original 8250 and the 16450. The
896 * 16450 has a scratch register (accessible with LCR=0)
897 */
898static void autoconfig_8250(struct uart_8250_port *up)
899{
900 unsigned char scratch, status1, status2;
901
902 up->port.type = PORT_8250;
903
904 scratch = serial_in(up, UART_SCR);
905 serial_outp(up, UART_SCR, 0xa5);
906 status1 = serial_in(up, UART_SCR);
907 serial_outp(up, UART_SCR, 0x5a);
908 status2 = serial_in(up, UART_SCR);
909 serial_outp(up, UART_SCR, scratch);
910
911 if (status1 == 0xa5 && status2 == 0x5a)
912 up->port.type = PORT_16450;
913}
914
915static int broken_efr(struct uart_8250_port *up)
916{
917 /*
918 * Exar ST16C2550 "A2" devices incorrectly detect as
919 * having an EFR, and report an ID of 0x0201. See
920 * http://www.exar.com/info.php?pdf=dan180_oct2004.pdf
921 */
922 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
923 return 1;
924
925 return 0;
926}
927
928/*
929 * We know that the chip has FIFOs. Does it have an EFR? The
930 * EFR is located in the same register position as the IIR and
931 * we know the top two bits of the IIR are currently set. The
932 * EFR should contain zero. Try to read the EFR.
933 */
934static void autoconfig_16550a(struct uart_8250_port *up)
935{
936 unsigned char status1, status2;
937 unsigned int iersave;
938
939 up->port.type = PORT_16550A;
940 up->capabilities |= UART_CAP_FIFO;
941
942 /*
943 * Check for presence of the EFR when DLAB is set.
944 * Only ST16C650V1 UARTs pass this test.
945 */
946 serial_outp(up, UART_LCR, UART_LCR_DLAB);
947 if (serial_in(up, UART_EFR) == 0) {
948 serial_outp(up, UART_EFR, 0xA8);
949 if (serial_in(up, UART_EFR) != 0) {
950 DEBUG_AUTOCONF("EFRv1 ");
951 up->port.type = PORT_16650;
952 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
953 } else {
954 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
955 }
956 serial_outp(up, UART_EFR, 0);
957 return;
958 }
959
960 /*
961 * Maybe it requires 0xbf to be written to the LCR.
962 * (other ST16C650V2 UARTs, TI16C752A, etc)
963 */
964 serial_outp(up, UART_LCR, 0xBF);
965 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
966 DEBUG_AUTOCONF("EFRv2 ");
967 autoconfig_has_efr(up);
968 return;
969 }
970
971 /*
972 * Check for a National Semiconductor SuperIO chip.
973 * Attempt to switch to bank 2, read the value of the LOOP bit
974 * from EXCR1. Switch back to bank 0, change it in MCR. Then
975 * switch back to bank 2, read it from EXCR1 again and check
976 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 */
978 serial_outp(up, UART_LCR, 0);
979 status1 = serial_in(up, UART_MCR);
980 serial_outp(up, UART_LCR, 0xE0);
981 status2 = serial_in(up, 0x02); /* EXCR1 */
982
983 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
984 serial_outp(up, UART_LCR, 0);
985 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
986 serial_outp(up, UART_LCR, 0xE0);
987 status2 = serial_in(up, 0x02); /* EXCR1 */
988 serial_outp(up, UART_LCR, 0);
989 serial_outp(up, UART_MCR, status1);
990
991 if ((status2 ^ status1) & UART_MCR_LOOP) {
David Woodhouse857dde22005-05-21 15:52:23 +0100992 unsigned short quot;
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 serial_outp(up, UART_LCR, 0xE0);
David Woodhouse857dde22005-05-21 15:52:23 +0100995
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100996 quot = serial_dl_read(up);
David Woodhouse857dde22005-05-21 15:52:23 +0100997 quot <<= 3;
998
David Woodhouseb5b82df2007-05-17 14:27:39 +0800999 status1 = serial_in(up, 0x04); /* EXCR2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
1001 status1 |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
1002 serial_outp(up, 0x04, status1);
Thomas Koellerbd71c182007-05-06 14:48:47 -07001003
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +01001004 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
David Woodhouse857dde22005-05-21 15:52:23 +01001006 serial_outp(up, UART_LCR, 0);
1007
1008 up->port.uartclk = 921600*16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 up->port.type = PORT_NS16550A;
1010 up->capabilities |= UART_NATSEMI;
1011 return;
1012 }
1013 }
1014
1015 /*
1016 * No EFR. Try to detect a TI16750, which only sets bit 5 of
1017 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1018 * Try setting it with and without DLAB set. Cheap clones
1019 * set bit 5 without DLAB set.
1020 */
1021 serial_outp(up, UART_LCR, 0);
1022 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1023 status1 = serial_in(up, UART_IIR) >> 5;
1024 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1025 serial_outp(up, UART_LCR, UART_LCR_DLAB);
1026 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1027 status2 = serial_in(up, UART_IIR) >> 5;
1028 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1029 serial_outp(up, UART_LCR, 0);
1030
1031 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1032
1033 if (status1 == 6 && status2 == 7) {
1034 up->port.type = PORT_16750;
1035 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1036 return;
1037 }
1038
1039 /*
1040 * Try writing and reading the UART_IER_UUE bit (b6).
1041 * If it works, this is probably one of the Xscale platform's
1042 * internal UARTs.
1043 * We're going to explicitly set the UUE bit to 0 before
1044 * trying to write and read a 1 just to make sure it's not
1045 * already a 1 and maybe locked there before we even start start.
1046 */
1047 iersave = serial_in(up, UART_IER);
1048 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
1049 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1050 /*
1051 * OK it's in a known zero state, try writing and reading
1052 * without disturbing the current state of the other bits.
1053 */
1054 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
1055 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1056 /*
1057 * It's an Xscale.
1058 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1059 */
1060 DEBUG_AUTOCONF("Xscale ");
1061 up->port.type = PORT_XSCALE;
1062 up->capabilities |= UART_CAP_UUE;
1063 return;
1064 }
1065 } else {
1066 /*
1067 * If we got here we couldn't force the IER_UUE bit to 0.
1068 * Log it and continue.
1069 */
1070 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1071 }
1072 serial_outp(up, UART_IER, iersave);
1073}
1074
1075/*
1076 * This routine is called by rs_init() to initialize a specific serial
1077 * port. It determines what type of UART chip this serial port is
1078 * using: 8250, 16450, 16550, 16550A. The important question is
1079 * whether or not this UART is a 16550A or not, since this will
1080 * determine whether or not we can use its FIFO features or not.
1081 */
1082static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
1083{
1084 unsigned char status1, scratch, scratch2, scratch3;
1085 unsigned char save_lcr, save_mcr;
1086 unsigned long flags;
1087
1088 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
1089 return;
1090
Lennert Buytenhek80647b92009-11-11 14:26:41 -08001091 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
David S. Miller84408382008-10-13 10:45:26 +01001092 serial_index(&up->port), up->port.iobase, up->port.membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 /*
1095 * We really do need global IRQs disabled here - we're going to
1096 * be frobbing the chips IRQ enable register to see if it exists.
1097 */
1098 spin_lock_irqsave(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 up->capabilities = 0;
Russell King4ba5e352005-06-23 10:43:04 +01001101 up->bugs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 if (!(up->port.flags & UPF_BUGGY_UART)) {
1104 /*
1105 * Do a simple existence test first; if we fail this,
1106 * there's no point trying anything else.
Thomas Koellerbd71c182007-05-06 14:48:47 -07001107 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 * 0x80 is used as a nonsense port to prevent against
1109 * false positives due to ISA bus float. The
1110 * assumption is that 0x80 is a non-existent port;
1111 * which should be safe since include/asm/io.h also
1112 * makes this assumption.
1113 *
1114 * Note: this is safe as long as MCR bit 4 is clear
1115 * and the device is in "PC" mode.
1116 */
1117 scratch = serial_inp(up, UART_IER);
1118 serial_outp(up, UART_IER, 0);
1119#ifdef __i386__
1120 outb(0xff, 0x080);
1121#endif
Thomas Hoehn48212002007-02-10 01:46:05 -08001122 /*
1123 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1124 * 16C754B) allow only to modify them if an EFR bit is set.
1125 */
1126 scratch2 = serial_inp(up, UART_IER) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 serial_outp(up, UART_IER, 0x0F);
1128#ifdef __i386__
1129 outb(0, 0x080);
1130#endif
Thomas Hoehn48212002007-02-10 01:46:05 -08001131 scratch3 = serial_inp(up, UART_IER) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 serial_outp(up, UART_IER, scratch);
1133 if (scratch2 != 0 || scratch3 != 0x0F) {
1134 /*
1135 * We failed; there's nothing here
1136 */
1137 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1138 scratch2, scratch3);
1139 goto out;
1140 }
1141 }
1142
1143 save_mcr = serial_in(up, UART_MCR);
1144 save_lcr = serial_in(up, UART_LCR);
1145
Thomas Koellerbd71c182007-05-06 14:48:47 -07001146 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 * Check to see if a UART is really there. Certain broken
1148 * internal modems based on the Rockwell chipset fail this
1149 * test, because they apparently don't implement the loopback
1150 * test mode. So this test is skipped on the COM 1 through
1151 * COM 4 ports. This *should* be safe, since no board
1152 * manufacturer would be stupid enough to design a board
1153 * that conflicts with COM 1-4 --- we hope!
1154 */
1155 if (!(up->port.flags & UPF_SKIP_TEST)) {
1156 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1157 status1 = serial_inp(up, UART_MSR) & 0xF0;
1158 serial_outp(up, UART_MCR, save_mcr);
1159 if (status1 != 0x90) {
1160 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1161 status1);
1162 goto out;
1163 }
1164 }
1165
1166 /*
1167 * We're pretty sure there's a port here. Lets find out what
1168 * type of port it is. The IIR top two bits allows us to find
Russell King6f0d6182005-09-09 16:17:58 +01001169 * out if it's 8250 or 16450, 16550, 16550A or later. This
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 * determines what we test for next.
1171 *
1172 * We also initialise the EFR (if any) to zero for later. The
1173 * EFR occupies the same register location as the FCR and IIR.
1174 */
1175 serial_outp(up, UART_LCR, 0xBF);
1176 serial_outp(up, UART_EFR, 0);
1177 serial_outp(up, UART_LCR, 0);
1178
1179 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1180 scratch = serial_in(up, UART_IIR) >> 6;
1181
1182 DEBUG_AUTOCONF("iir=%d ", scratch);
1183
1184 switch (scratch) {
1185 case 0:
1186 autoconfig_8250(up);
1187 break;
1188 case 1:
1189 up->port.type = PORT_UNKNOWN;
1190 break;
1191 case 2:
1192 up->port.type = PORT_16550;
1193 break;
1194 case 3:
1195 autoconfig_16550a(up);
1196 break;
1197 }
1198
1199#ifdef CONFIG_SERIAL_8250_RSA
1200 /*
1201 * Only probe for RSA ports if we got the region.
1202 */
1203 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1204 int i;
1205
1206 for (i = 0 ; i < probe_rsa_count; ++i) {
1207 if (probe_rsa[i] == up->port.iobase &&
1208 __enable_rsa(up)) {
1209 up->port.type = PORT_RSA;
1210 break;
1211 }
1212 }
1213 }
1214#endif
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 serial_outp(up, UART_LCR, save_lcr);
1217
1218 if (up->capabilities != uart_config[up->port.type].flags) {
1219 printk(KERN_WARNING
1220 "ttyS%d: detected caps %08x should be %08x\n",
David S. Miller84408382008-10-13 10:45:26 +01001221 serial_index(&up->port), up->capabilities,
1222 uart_config[up->port.type].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224
1225 up->port.fifosize = uart_config[up->port.type].fifo_size;
1226 up->capabilities = uart_config[up->port.type].flags;
1227 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1228
1229 if (up->port.type == PORT_UNKNOWN)
1230 goto out;
1231
1232 /*
1233 * Reset the UART.
1234 */
1235#ifdef CONFIG_SERIAL_8250_RSA
1236 if (up->port.type == PORT_RSA)
1237 serial_outp(up, UART_RSA_FRR, 0);
1238#endif
1239 serial_outp(up, UART_MCR, save_mcr);
1240 serial8250_clear_fifos(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001241 serial_in(up, UART_RX);
Lennert Buytenhek5c8c7552005-11-12 21:58:05 +00001242 if (up->capabilities & UART_CAP_UUE)
1243 serial_outp(up, UART_IER, UART_IER_UUE);
1244 else
1245 serial_outp(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Thomas Koellerbd71c182007-05-06 14:48:47 -07001247 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 spin_unlock_irqrestore(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1250}
1251
1252static void autoconfig_irq(struct uart_8250_port *up)
1253{
1254 unsigned char save_mcr, save_ier;
1255 unsigned char save_ICP = 0;
1256 unsigned int ICP = 0;
1257 unsigned long irqs;
1258 int irq;
1259
1260 if (up->port.flags & UPF_FOURPORT) {
1261 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1262 save_ICP = inb_p(ICP);
1263 outb_p(0x80, ICP);
1264 (void) inb_p(ICP);
1265 }
1266
1267 /* forget possible initially masked and pending IRQ */
1268 probe_irq_off(probe_irq_on());
1269 save_mcr = serial_inp(up, UART_MCR);
1270 save_ier = serial_inp(up, UART_IER);
1271 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
Thomas Koellerbd71c182007-05-06 14:48:47 -07001272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 irqs = probe_irq_on();
1274 serial_outp(up, UART_MCR, 0);
Alan Cox6f803cd2008-02-08 04:18:52 -08001275 udelay(10);
1276 if (up->port.flags & UPF_FOURPORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 serial_outp(up, UART_MCR,
1278 UART_MCR_DTR | UART_MCR_RTS);
1279 } else {
1280 serial_outp(up, UART_MCR,
1281 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1282 }
1283 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1284 (void)serial_inp(up, UART_LSR);
1285 (void)serial_inp(up, UART_RX);
1286 (void)serial_inp(up, UART_IIR);
1287 (void)serial_inp(up, UART_MSR);
1288 serial_outp(up, UART_TX, 0xFF);
Alan Cox6f803cd2008-02-08 04:18:52 -08001289 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 irq = probe_irq_off(irqs);
1291
1292 serial_outp(up, UART_MCR, save_mcr);
1293 serial_outp(up, UART_IER, save_ier);
1294
1295 if (up->port.flags & UPF_FOURPORT)
1296 outb_p(save_ICP, ICP);
1297
1298 up->port.irq = (irq > 0) ? irq : 0;
1299}
1300
Russell Kinge763b902005-06-29 18:41:51 +01001301static inline void __stop_tx(struct uart_8250_port *p)
1302{
1303 if (p->ier & UART_IER_THRI) {
1304 p->ier &= ~UART_IER_THRI;
1305 serial_out(p, UART_IER, p->ier);
1306 }
1307}
1308
Russell Kingb129a8c2005-08-31 10:12:14 +01001309static void serial8250_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 struct uart_8250_port *up = (struct uart_8250_port *)port;
1312
Russell Kinge763b902005-06-29 18:41:51 +01001313 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 /*
Russell Kinge763b902005-06-29 18:41:51 +01001316 * We really want to stop the transmitter from sending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 */
Russell Kinge763b902005-06-29 18:41:51 +01001318 if (up->port.type == PORT_16C950) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 up->acr |= UART_ACR_TXDIS;
1320 serial_icr_write(up, UART_ACR, up->acr);
1321 }
1322}
1323
Russell King55d3b282005-06-23 15:05:41 +01001324static void transmit_chars(struct uart_8250_port *up);
1325
Russell Kingb129a8c2005-08-31 10:12:14 +01001326static void serial8250_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 struct uart_8250_port *up = (struct uart_8250_port *)port;
1329
1330 if (!(up->ier & UART_IER_THRI)) {
1331 up->ier |= UART_IER_THRI;
1332 serial_out(up, UART_IER, up->ier);
Russell King55d3b282005-06-23 15:05:41 +01001333
Russell King67f76542005-06-23 22:26:43 +01001334 if (up->bugs & UART_BUG_TXEN) {
Ian Jackson68cb4f82009-11-18 11:08:11 +01001335 unsigned char lsr;
Russell King55d3b282005-06-23 15:05:41 +01001336 lsr = serial_in(up, UART_LSR);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001337 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Thomas Koellerbd71c182007-05-06 14:48:47 -07001338 if ((up->port.type == PORT_RM9000) ?
Ian Jackson68cb4f82009-11-18 11:08:11 +01001339 (lsr & UART_LSR_THRE) :
1340 (lsr & UART_LSR_TEMT))
Russell King55d3b282005-06-23 15:05:41 +01001341 transmit_chars(up);
1342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
Russell Kinge763b902005-06-29 18:41:51 +01001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /*
Russell Kinge763b902005-06-29 18:41:51 +01001346 * Re-enable the transmitter if we disabled it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 */
Russell Kinge763b902005-06-29 18:41:51 +01001348 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 up->acr &= ~UART_ACR_TXDIS;
1350 serial_icr_write(up, UART_ACR, up->acr);
1351 }
1352}
1353
1354static void serial8250_stop_rx(struct uart_port *port)
1355{
1356 struct uart_8250_port *up = (struct uart_8250_port *)port;
1357
1358 up->ier &= ~UART_IER_RLSI;
1359 up->port.read_status_mask &= ~UART_LSR_DR;
1360 serial_out(up, UART_IER, up->ier);
1361}
1362
1363static void serial8250_enable_ms(struct uart_port *port)
1364{
1365 struct uart_8250_port *up = (struct uart_8250_port *)port;
1366
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001367 /* no MSR capabilities */
1368 if (up->bugs & UART_BUG_NOMSR)
1369 return;
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 up->ier |= UART_IER_MSI;
1372 serial_out(up, UART_IER, up->ier);
1373}
1374
Russell Kingea8874d2006-01-04 19:43:24 +00001375static void
Thomas Koellercc79aa92007-02-20 13:58:05 -08001376receive_chars(struct uart_8250_port *up, unsigned int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001378 struct tty_struct *tty = up->port.state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 unsigned char ch, lsr = *status;
1380 int max_count = 256;
1381 char flag;
1382
1383 do {
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001384 if (likely(lsr & UART_LSR_DR))
1385 ch = serial_inp(up, UART_RX);
1386 else
1387 /*
1388 * Intel 82571 has a Serial Over Lan device that will
1389 * set UART_LSR_BI without setting UART_LSR_DR when
1390 * it receives a break. To avoid reading from the
1391 * receive buffer without UART_LSR_DR bit set, we
1392 * just force the read character to be 0
1393 */
1394 ch = 0;
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 flag = TTY_NORMAL;
1397 up->port.icount.rx++;
1398
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001399 lsr |= up->lsr_saved_flags;
1400 up->lsr_saved_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001402 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 /*
1404 * For statistics only
1405 */
1406 if (lsr & UART_LSR_BI) {
1407 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1408 up->port.icount.brk++;
1409 /*
1410 * We do the SysRQ and SAK checking
1411 * here because otherwise the break
1412 * may get masked by ignore_status_mask
1413 * or read_status_mask.
1414 */
1415 if (uart_handle_break(&up->port))
1416 goto ignore_char;
1417 } else if (lsr & UART_LSR_PE)
1418 up->port.icount.parity++;
1419 else if (lsr & UART_LSR_FE)
1420 up->port.icount.frame++;
1421 if (lsr & UART_LSR_OE)
1422 up->port.icount.overrun++;
1423
1424 /*
Russell King23907eb2005-04-16 15:26:39 -07001425 * Mask off conditions which should be ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 */
1427 lsr &= up->port.read_status_mask;
1428
1429 if (lsr & UART_LSR_BI) {
1430 DEBUG_INTR("handling break....");
1431 flag = TTY_BREAK;
1432 } else if (lsr & UART_LSR_PE)
1433 flag = TTY_PARITY;
1434 else if (lsr & UART_LSR_FE)
1435 flag = TTY_FRAME;
1436 }
David Howells7d12e782006-10-05 14:55:46 +01001437 if (uart_handle_sysrq_char(&up->port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 goto ignore_char;
Russell King05ab3012005-05-09 23:21:59 +01001439
1440 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1441
Alan Cox6f803cd2008-02-08 04:18:52 -08001442ignore_char:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 lsr = serial_inp(up, UART_LSR);
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001444 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 spin_unlock(&up->port.lock);
1446 tty_flip_buffer_push(tty);
1447 spin_lock(&up->port.lock);
1448 *status = lsr;
1449}
1450
Russell Kingea8874d2006-01-04 19:43:24 +00001451static void transmit_chars(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001453 struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 int count;
1455
1456 if (up->port.x_char) {
1457 serial_outp(up, UART_TX, up->port.x_char);
1458 up->port.icount.tx++;
1459 up->port.x_char = 0;
1460 return;
1461 }
Russell Kingb129a8c2005-08-31 10:12:14 +01001462 if (uart_tx_stopped(&up->port)) {
1463 serial8250_stop_tx(&up->port);
1464 return;
1465 }
1466 if (uart_circ_empty(xmit)) {
Russell Kinge763b902005-06-29 18:41:51 +01001467 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return;
1469 }
1470
1471 count = up->tx_loadsz;
1472 do {
1473 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1474 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1475 up->port.icount.tx++;
1476 if (uart_circ_empty(xmit))
1477 break;
1478 } while (--count > 0);
1479
1480 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1481 uart_write_wakeup(&up->port);
1482
1483 DEBUG_INTR("THRE...");
1484
1485 if (uart_circ_empty(xmit))
Russell Kinge763b902005-06-29 18:41:51 +01001486 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Russell King2af7cd62006-01-04 16:55:09 +00001489static unsigned int check_modem_status(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Russell King2af7cd62006-01-04 16:55:09 +00001491 unsigned int status = serial_in(up, UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001493 status |= up->msr_saved_flags;
1494 up->msr_saved_flags = 0;
Taku Izumifdc30b32007-04-23 14:41:00 -07001495 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
Alan Coxebd2c8f2009-09-19 13:13:28 -07001496 up->port.state != NULL) {
Russell King2af7cd62006-01-04 16:55:09 +00001497 if (status & UART_MSR_TERI)
1498 up->port.icount.rng++;
1499 if (status & UART_MSR_DDSR)
1500 up->port.icount.dsr++;
1501 if (status & UART_MSR_DDCD)
1502 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1503 if (status & UART_MSR_DCTS)
1504 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Alan Coxbdc04e32009-09-19 13:13:31 -07001506 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
Russell King2af7cd62006-01-04 16:55:09 +00001507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Russell King2af7cd62006-01-04 16:55:09 +00001509 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
1512/*
1513 * This handles the interrupt from one port.
1514 */
Will Newtonb5d674a2008-10-13 10:36:21 +01001515static void serial8250_handle_port(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Russell King45e24602006-01-04 19:19:06 +00001517 unsigned int status;
Jiri Kosina4bf36312007-04-23 14:41:21 -07001518 unsigned long flags;
Russell King45e24602006-01-04 19:19:06 +00001519
Jiri Kosina4bf36312007-04-23 14:41:21 -07001520 spin_lock_irqsave(&up->port.lock, flags);
Russell King45e24602006-01-04 19:19:06 +00001521
1522 status = serial_inp(up, UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 DEBUG_INTR("status = %x...", status);
1525
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001526 if (status & (UART_LSR_DR | UART_LSR_BI))
David Howells7d12e782006-10-05 14:55:46 +01001527 receive_chars(up, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 check_modem_status(up);
1529 if (status & UART_LSR_THRE)
1530 transmit_chars(up);
Russell King45e24602006-01-04 19:19:06 +00001531
Jiri Kosina4bf36312007-04-23 14:41:21 -07001532 spin_unlock_irqrestore(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
1535/*
1536 * This is the serial driver's interrupt routine.
1537 *
1538 * Arjan thinks the old way was overly complex, so it got simplified.
1539 * Alan disagrees, saying that need the complexity to handle the weird
1540 * nature of ISA shared interrupts. (This is a special exception.)
1541 *
1542 * In order to handle ISA shared interrupts properly, we need to check
1543 * that all ports have been serviced, and therefore the ISA interrupt
1544 * line has been de-asserted.
1545 *
1546 * This means we need to loop through all ports. checking that they
1547 * don't have an interrupt pending.
1548 */
David Howells7d12e782006-10-05 14:55:46 +01001549static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 struct irq_info *i = dev_id;
1552 struct list_head *l, *end = NULL;
1553 int pass_counter = 0, handled = 0;
1554
1555 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1556
1557 spin_lock(&i->lock);
1558
1559 l = i->head;
1560 do {
1561 struct uart_8250_port *up;
1562 unsigned int iir;
1563
1564 up = list_entry(l, struct uart_8250_port, list);
1565
1566 iir = serial_in(up, UART_IIR);
1567 if (!(iir & UART_IIR_NO_INT)) {
David Howells7d12e782006-10-05 14:55:46 +01001568 serial8250_handle_port(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 handled = 1;
1571
1572 end = NULL;
Marc St-Jeanbeab6972007-05-06 14:48:45 -07001573 } else if (up->port.iotype == UPIO_DWAPB &&
1574 (iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
1575 /* The DesignWare APB UART has an Busy Detect (0x07)
1576 * interrupt meaning an LCR write attempt occured while the
1577 * UART was busy. The interrupt must be cleared by reading
1578 * the UART status register (USR) and the LCR re-written. */
1579 unsigned int status;
1580 status = *(volatile u32 *)up->port.private_data;
1581 serial_out(up, UART_LCR, up->lcr);
1582
1583 handled = 1;
1584
1585 end = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 } else if (end == NULL)
1587 end = l;
1588
1589 l = l->next;
1590
1591 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1592 /* If we hit this, we're dead. */
1593 printk(KERN_ERR "serial8250: too much work for "
1594 "irq%d\n", irq);
1595 break;
1596 }
1597 } while (l != end);
1598
1599 spin_unlock(&i->lock);
1600
1601 DEBUG_INTR("end.\n");
1602
1603 return IRQ_RETVAL(handled);
1604}
1605
1606/*
1607 * To support ISA shared interrupts, we need to have one interrupt
1608 * handler that ensures that the IRQ line has been deasserted
1609 * before returning. Failing to do this will result in the IRQ
1610 * line being stuck active, and, since ISA irqs are edge triggered,
1611 * no more IRQs will be seen.
1612 */
1613static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1614{
1615 spin_lock_irq(&i->lock);
1616
1617 if (!list_empty(i->head)) {
1618 if (i->head == &up->list)
1619 i->head = i->head->next;
1620 list_del(&up->list);
1621 } else {
1622 BUG_ON(i->head != &up->list);
1623 i->head = NULL;
1624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 spin_unlock_irq(&i->lock);
Alan Cox25db8ad2008-08-19 20:49:40 -07001626 /* List empty so throw away the hash node */
1627 if (i->head == NULL) {
1628 hlist_del(&i->node);
1629 kfree(i);
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
1633static int serial_link_irq_chain(struct uart_8250_port *up)
1634{
Alan Cox25db8ad2008-08-19 20:49:40 -07001635 struct hlist_head *h;
1636 struct hlist_node *n;
1637 struct irq_info *i;
Thomas Gleixner40663cc2006-07-01 19:29:43 -07001638 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Alan Cox25db8ad2008-08-19 20:49:40 -07001640 mutex_lock(&hash_mutex);
1641
1642 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1643
1644 hlist_for_each(n, h) {
1645 i = hlist_entry(n, struct irq_info, node);
1646 if (i->irq == up->port.irq)
1647 break;
1648 }
1649
1650 if (n == NULL) {
1651 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
1652 if (i == NULL) {
1653 mutex_unlock(&hash_mutex);
1654 return -ENOMEM;
1655 }
1656 spin_lock_init(&i->lock);
1657 i->irq = up->port.irq;
1658 hlist_add_head(&i->node, h);
1659 }
1660 mutex_unlock(&hash_mutex);
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 spin_lock_irq(&i->lock);
1663
1664 if (i->head) {
1665 list_add(&up->list, i->head);
1666 spin_unlock_irq(&i->lock);
1667
1668 ret = 0;
1669 } else {
1670 INIT_LIST_HEAD(&up->list);
1671 i->head = &up->list;
1672 spin_unlock_irq(&i->lock);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07001673 irq_flags |= up->port.irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 ret = request_irq(up->port.irq, serial8250_interrupt,
1675 irq_flags, "serial", i);
1676 if (ret < 0)
1677 serial_do_unlink(i, up);
1678 }
1679
1680 return ret;
1681}
1682
1683static void serial_unlink_irq_chain(struct uart_8250_port *up)
1684{
Alan Cox25db8ad2008-08-19 20:49:40 -07001685 struct irq_info *i;
1686 struct hlist_node *n;
1687 struct hlist_head *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Alan Cox25db8ad2008-08-19 20:49:40 -07001689 mutex_lock(&hash_mutex);
1690
1691 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1692
1693 hlist_for_each(n, h) {
1694 i = hlist_entry(n, struct irq_info, node);
1695 if (i->irq == up->port.irq)
1696 break;
1697 }
1698
1699 BUG_ON(n == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 BUG_ON(i->head == NULL);
1701
1702 if (list_empty(i->head))
1703 free_irq(up->port.irq, i);
1704
1705 serial_do_unlink(i, up);
Alan Cox25db8ad2008-08-19 20:49:40 -07001706 mutex_unlock(&hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
Alex Williamson40b36da2007-02-14 00:33:04 -08001709/* Base timer interval for polling */
1710static inline int poll_timeout(int timeout)
1711{
1712 return timeout > 6 ? (timeout / 2 - 2) : 1;
1713}
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715/*
1716 * This function is used to handle ports that do not have an
1717 * interrupt. This doesn't work very well for 16450's, but gives
1718 * barely passable results for a 16550A. (Although at the expense
1719 * of much CPU overhead).
1720 */
1721static void serial8250_timeout(unsigned long data)
1722{
1723 struct uart_8250_port *up = (struct uart_8250_port *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 unsigned int iir;
1725
1726 iir = serial_in(up, UART_IIR);
Russell King45e24602006-01-04 19:19:06 +00001727 if (!(iir & UART_IIR_NO_INT))
David Howells7d12e782006-10-05 14:55:46 +01001728 serial8250_handle_port(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001729 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
1730}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Alex Williamson40b36da2007-02-14 00:33:04 -08001732static void serial8250_backup_timeout(unsigned long data)
1733{
1734 struct uart_8250_port *up = (struct uart_8250_port *)data;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001735 unsigned int iir, ier = 0, lsr;
1736 unsigned long flags;
Alex Williamson40b36da2007-02-14 00:33:04 -08001737
1738 /*
1739 * Must disable interrupts or else we risk racing with the interrupt
1740 * based handler.
1741 */
1742 if (is_real_interrupt(up->port.irq)) {
1743 ier = serial_in(up, UART_IER);
1744 serial_out(up, UART_IER, 0);
1745 }
1746
1747 iir = serial_in(up, UART_IIR);
1748
1749 /*
1750 * This should be a safe test for anyone who doesn't trust the
1751 * IIR bits on their UART, but it's specifically designed for
1752 * the "Diva" UART used on the management processor on many HP
1753 * ia64 and parisc boxes.
1754 */
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001755 spin_lock_irqsave(&up->port.lock, flags);
1756 lsr = serial_in(up, UART_LSR);
1757 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1758 spin_unlock_irqrestore(&up->port.lock, flags);
Alex Williamson40b36da2007-02-14 00:33:04 -08001759 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
Alan Coxebd2c8f2009-09-19 13:13:28 -07001760 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001761 (lsr & UART_LSR_THRE)) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001762 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1763 iir |= UART_IIR_THRI;
1764 }
1765
1766 if (!(iir & UART_IIR_NO_INT))
1767 serial8250_handle_port(up);
1768
1769 if (is_real_interrupt(up->port.irq))
1770 serial_out(up, UART_IER, ier);
1771
1772 /* Standard timer interval plus 0.2s to keep the port running */
Alan Cox6f803cd2008-02-08 04:18:52 -08001773 mod_timer(&up->timer,
1774 jiffies + poll_timeout(up->port.timeout) + HZ / 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
1777static unsigned int serial8250_tx_empty(struct uart_port *port)
1778{
1779 struct uart_8250_port *up = (struct uart_8250_port *)port;
1780 unsigned long flags;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001781 unsigned int lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 spin_lock_irqsave(&up->port.lock, flags);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001784 lsr = serial_in(up, UART_LSR);
1785 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 spin_unlock_irqrestore(&up->port.lock, flags);
1787
Dick Hollenbeckbca47612009-12-09 12:31:34 -08001788 return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
1791static unsigned int serial8250_get_mctrl(struct uart_port *port)
1792{
1793 struct uart_8250_port *up = (struct uart_8250_port *)port;
Russell King2af7cd62006-01-04 16:55:09 +00001794 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 unsigned int ret;
1796
Russell King2af7cd62006-01-04 16:55:09 +00001797 status = check_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 ret = 0;
1800 if (status & UART_MSR_DCD)
1801 ret |= TIOCM_CAR;
1802 if (status & UART_MSR_RI)
1803 ret |= TIOCM_RNG;
1804 if (status & UART_MSR_DSR)
1805 ret |= TIOCM_DSR;
1806 if (status & UART_MSR_CTS)
1807 ret |= TIOCM_CTS;
1808 return ret;
1809}
1810
1811static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1812{
1813 struct uart_8250_port *up = (struct uart_8250_port *)port;
1814 unsigned char mcr = 0;
1815
1816 if (mctrl & TIOCM_RTS)
1817 mcr |= UART_MCR_RTS;
1818 if (mctrl & TIOCM_DTR)
1819 mcr |= UART_MCR_DTR;
1820 if (mctrl & TIOCM_OUT1)
1821 mcr |= UART_MCR_OUT1;
1822 if (mctrl & TIOCM_OUT2)
1823 mcr |= UART_MCR_OUT2;
1824 if (mctrl & TIOCM_LOOP)
1825 mcr |= UART_MCR_LOOP;
1826
1827 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1828
1829 serial_out(up, UART_MCR, mcr);
1830}
1831
1832static void serial8250_break_ctl(struct uart_port *port, int break_state)
1833{
1834 struct uart_8250_port *up = (struct uart_8250_port *)port;
1835 unsigned long flags;
1836
1837 spin_lock_irqsave(&up->port.lock, flags);
1838 if (break_state == -1)
1839 up->lcr |= UART_LCR_SBC;
1840 else
1841 up->lcr &= ~UART_LCR_SBC;
1842 serial_out(up, UART_LCR, up->lcr);
1843 spin_unlock_irqrestore(&up->port.lock, flags);
1844}
1845
Alex Williamson40b36da2007-02-14 00:33:04 -08001846/*
1847 * Wait for transmitter & holding register to empty
1848 */
Will Newtonb5d674a2008-10-13 10:36:21 +01001849static void wait_for_xmitr(struct uart_8250_port *up, int bits)
Alex Williamson40b36da2007-02-14 00:33:04 -08001850{
1851 unsigned int status, tmout = 10000;
1852
1853 /* Wait up to 10ms for the character(s) to be sent. */
1854 do {
1855 status = serial_in(up, UART_LSR);
1856
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001857 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -08001858
1859 if (--tmout == 0)
1860 break;
1861 udelay(1);
1862 } while ((status & bits) != bits);
1863
1864 /* Wait up to 1s for flow control if necessary */
1865 if (up->port.flags & UPF_CONS_FLOW) {
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001866 unsigned int tmout;
1867 for (tmout = 1000000; tmout; tmout--) {
1868 unsigned int msr = serial_in(up, UART_MSR);
1869 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1870 if (msr & UART_MSR_CTS)
1871 break;
Alex Williamson40b36da2007-02-14 00:33:04 -08001872 udelay(1);
1873 touch_nmi_watchdog();
1874 }
1875 }
1876}
1877
Jason Wesself2d937f2008-04-17 20:05:37 +02001878#ifdef CONFIG_CONSOLE_POLL
1879/*
1880 * Console polling routines for writing and reading from the uart while
1881 * in an interrupt or debug context.
1882 */
1883
1884static int serial8250_get_poll_char(struct uart_port *port)
1885{
1886 struct uart_8250_port *up = (struct uart_8250_port *)port;
1887 unsigned char lsr = serial_inp(up, UART_LSR);
1888
Jason Wesself5316b42010-05-20 21:04:22 -05001889 if (!(lsr & UART_LSR_DR))
1890 return NO_POLL_CHAR;
Jason Wesself2d937f2008-04-17 20:05:37 +02001891
1892 return serial_inp(up, UART_RX);
1893}
1894
1895
1896static void serial8250_put_poll_char(struct uart_port *port,
1897 unsigned char c)
1898{
1899 unsigned int ier;
1900 struct uart_8250_port *up = (struct uart_8250_port *)port;
1901
1902 /*
1903 * First save the IER then disable the interrupts
1904 */
1905 ier = serial_in(up, UART_IER);
1906 if (up->capabilities & UART_CAP_UUE)
1907 serial_out(up, UART_IER, UART_IER_UUE);
1908 else
1909 serial_out(up, UART_IER, 0);
1910
1911 wait_for_xmitr(up, BOTH_EMPTY);
1912 /*
1913 * Send the character out.
1914 * If a LF, also do CR...
1915 */
1916 serial_out(up, UART_TX, c);
1917 if (c == 10) {
1918 wait_for_xmitr(up, BOTH_EMPTY);
1919 serial_out(up, UART_TX, 13);
1920 }
1921
1922 /*
1923 * Finally, wait for transmitter to become empty
1924 * and restore the IER
1925 */
1926 wait_for_xmitr(up, BOTH_EMPTY);
1927 serial_out(up, UART_IER, ier);
1928}
1929
1930#endif /* CONFIG_CONSOLE_POLL */
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932static int serial8250_startup(struct uart_port *port)
1933{
1934 struct uart_8250_port *up = (struct uart_8250_port *)port;
1935 unsigned long flags;
Russell King55d3b282005-06-23 15:05:41 +01001936 unsigned char lsr, iir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 int retval;
1938
1939 up->capabilities = uart_config[up->port.type].flags;
1940 up->mcr = 0;
1941
Alan Coxb8e7e402009-05-28 14:01:35 +01001942 if (up->port.iotype != up->cur_iotype)
1943 set_io_from_upio(port);
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (up->port.type == PORT_16C950) {
1946 /* Wake up and initialize UART */
1947 up->acr = 0;
1948 serial_outp(up, UART_LCR, 0xBF);
1949 serial_outp(up, UART_EFR, UART_EFR_ECB);
1950 serial_outp(up, UART_IER, 0);
1951 serial_outp(up, UART_LCR, 0);
1952 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
1953 serial_outp(up, UART_LCR, 0xBF);
1954 serial_outp(up, UART_EFR, UART_EFR_ECB);
1955 serial_outp(up, UART_LCR, 0);
1956 }
1957
1958#ifdef CONFIG_SERIAL_8250_RSA
1959 /*
1960 * If this is an RSA port, see if we can kick it up to the
1961 * higher speed clock.
1962 */
1963 enable_rsa(up);
1964#endif
1965
1966 /*
1967 * Clear the FIFO buffers and disable them.
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08001968 * (they will be reenabled in set_termios())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 */
1970 serial8250_clear_fifos(up);
1971
1972 /*
1973 * Clear the interrupt registers.
1974 */
1975 (void) serial_inp(up, UART_LSR);
1976 (void) serial_inp(up, UART_RX);
1977 (void) serial_inp(up, UART_IIR);
1978 (void) serial_inp(up, UART_MSR);
1979
1980 /*
1981 * At this point, there's no way the LSR could still be 0xff;
1982 * if it is, then bail out, because there's likely no UART
1983 * here.
1984 */
1985 if (!(up->port.flags & UPF_BUGGY_UART) &&
1986 (serial_inp(up, UART_LSR) == 0xff)) {
David S. Miller84408382008-10-13 10:45:26 +01001987 printk(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
1988 serial_index(&up->port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return -ENODEV;
1990 }
1991
1992 /*
1993 * For a XR16C850, we need to set the trigger levels
1994 */
1995 if (up->port.type == PORT_16850) {
1996 unsigned char fctr;
1997
1998 serial_outp(up, UART_LCR, 0xbf);
1999
2000 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2001 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2002 serial_outp(up, UART_TRG, UART_TRG_96);
2003 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2004 serial_outp(up, UART_TRG, UART_TRG_96);
2005
2006 serial_outp(up, UART_LCR, 0);
2007 }
2008
Alex Williamson40b36da2007-02-14 00:33:04 -08002009 if (is_real_interrupt(up->port.irq)) {
Alex Williamson01c194d2008-04-28 02:14:09 -07002010 unsigned char iir1;
Alex Williamson40b36da2007-02-14 00:33:04 -08002011 /*
2012 * Test for UARTs that do not reassert THRE when the
2013 * transmitter is idle and the interrupt has already
2014 * been cleared. Real 16550s should always reassert
2015 * this interrupt whenever the transmitter is idle and
2016 * the interrupt is enabled. Delays are necessary to
2017 * allow register changes to become visible.
2018 */
Borislav Petkovc389d272008-07-29 22:33:32 -07002019 spin_lock_irqsave(&up->port.lock, flags);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002020 if (up->port.irqflags & IRQF_SHARED)
Anton Vorontsov768aec02008-07-22 11:21:07 +01002021 disable_irq_nosync(up->port.irq);
Alex Williamson40b36da2007-02-14 00:33:04 -08002022
2023 wait_for_xmitr(up, UART_LSR_THRE);
2024 serial_out_sync(up, UART_IER, UART_IER_THRI);
2025 udelay(1); /* allow THRE to set */
Alex Williamson01c194d2008-04-28 02:14:09 -07002026 iir1 = serial_in(up, UART_IIR);
Alex Williamson40b36da2007-02-14 00:33:04 -08002027 serial_out(up, UART_IER, 0);
2028 serial_out_sync(up, UART_IER, UART_IER_THRI);
2029 udelay(1); /* allow a working UART time to re-assert THRE */
2030 iir = serial_in(up, UART_IIR);
2031 serial_out(up, UART_IER, 0);
2032
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002033 if (up->port.irqflags & IRQF_SHARED)
Anton Vorontsov768aec02008-07-22 11:21:07 +01002034 enable_irq(up->port.irq);
Borislav Petkovc389d272008-07-29 22:33:32 -07002035 spin_unlock_irqrestore(&up->port.lock, flags);
Alex Williamson40b36da2007-02-14 00:33:04 -08002036
2037 /*
2038 * If the interrupt is not reasserted, setup a timer to
2039 * kick the UART on a regular basis.
2040 */
Alex Williamson01c194d2008-04-28 02:14:09 -07002041 if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
Will Newton363f66f2008-09-02 14:35:44 -07002042 up->bugs |= UART_BUG_THRE;
David S. Miller84408382008-10-13 10:45:26 +01002043 pr_debug("ttyS%d - using backup timer\n",
2044 serial_index(port));
Alex Williamson40b36da2007-02-14 00:33:04 -08002045 }
2046 }
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 /*
Will Newton363f66f2008-09-02 14:35:44 -07002049 * The above check will only give an accurate result the first time
2050 * the port is opened so this value needs to be preserved.
2051 */
2052 if (up->bugs & UART_BUG_THRE) {
2053 up->timer.function = serial8250_backup_timeout;
2054 up->timer.data = (unsigned long)up;
2055 mod_timer(&up->timer, jiffies +
2056 poll_timeout(up->port.timeout) + HZ / 5);
2057 }
2058
2059 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 * If the "interrupt" for this port doesn't correspond with any
2061 * hardware interrupt, we use a timer-based system. The original
2062 * driver used to do this with IRQ0.
2063 */
2064 if (!is_real_interrupt(up->port.irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 up->timer.data = (unsigned long)up;
Alex Williamson40b36da2007-02-14 00:33:04 -08002066 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 } else {
2068 retval = serial_link_irq_chain(up);
2069 if (retval)
2070 return retval;
2071 }
2072
2073 /*
2074 * Now, initialize the UART
2075 */
2076 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
2077
2078 spin_lock_irqsave(&up->port.lock, flags);
2079 if (up->port.flags & UPF_FOURPORT) {
2080 if (!is_real_interrupt(up->port.irq))
2081 up->port.mctrl |= TIOCM_OUT1;
2082 } else
2083 /*
2084 * Most PC uarts need OUT2 raised to enable interrupts.
2085 */
2086 if (is_real_interrupt(up->port.irq))
2087 up->port.mctrl |= TIOCM_OUT2;
2088
2089 serial8250_set_mctrl(&up->port, up->port.mctrl);
Russell King55d3b282005-06-23 15:05:41 +01002090
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002091 /* Serial over Lan (SoL) hack:
2092 Intel 8257x Gigabit ethernet chips have a
2093 16550 emulation, to be used for Serial Over Lan.
2094 Those chips take a longer time than a normal
2095 serial device to signalize that a transmission
2096 data was queued. Due to that, the above test generally
2097 fails. One solution would be to delay the reading of
2098 iir. However, this is not reliable, since the timeout
2099 is variable. So, let's just don't test if we receive
2100 TX irq. This way, we'll never enable UART_BUG_TXEN.
2101 */
Chuck Ebbertd41a4b52009-10-01 15:44:26 -07002102 if (skip_txen_test || up->port.flags & UPF_NO_TXEN_TEST)
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002103 goto dont_test_tx_en;
2104
Russell King55d3b282005-06-23 15:05:41 +01002105 /*
2106 * Do a quick test to see if we receive an
2107 * interrupt when we enable the TX irq.
2108 */
2109 serial_outp(up, UART_IER, UART_IER_THRI);
2110 lsr = serial_in(up, UART_LSR);
2111 iir = serial_in(up, UART_IIR);
2112 serial_outp(up, UART_IER, 0);
2113
2114 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
Russell King67f76542005-06-23 22:26:43 +01002115 if (!(up->bugs & UART_BUG_TXEN)) {
2116 up->bugs |= UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002117 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
David S. Miller84408382008-10-13 10:45:26 +01002118 serial_index(port));
Russell King55d3b282005-06-23 15:05:41 +01002119 }
2120 } else {
Russell King67f76542005-06-23 22:26:43 +01002121 up->bugs &= ~UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002122 }
2123
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002124dont_test_tx_en:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 spin_unlock_irqrestore(&up->port.lock, flags);
2126
2127 /*
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002128 * Clear the interrupt registers again for luck, and clear the
2129 * saved flags to avoid getting false values from polling
2130 * routines or the previous session.
2131 */
2132 serial_inp(up, UART_LSR);
2133 serial_inp(up, UART_RX);
2134 serial_inp(up, UART_IIR);
2135 serial_inp(up, UART_MSR);
2136 up->lsr_saved_flags = 0;
2137 up->msr_saved_flags = 0;
2138
2139 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 * Finally, enable interrupts. Note: Modem status interrupts
2141 * are set via set_termios(), which will be occurring imminently
2142 * anyway, so we don't enable them here.
2143 */
2144 up->ier = UART_IER_RLSI | UART_IER_RDI;
2145 serial_outp(up, UART_IER, up->ier);
2146
2147 if (up->port.flags & UPF_FOURPORT) {
2148 unsigned int icp;
2149 /*
2150 * Enable interrupts on the AST Fourport board
2151 */
2152 icp = (up->port.iobase & 0xfe0) | 0x01f;
2153 outb_p(0x80, icp);
2154 (void) inb_p(icp);
2155 }
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return 0;
2158}
2159
2160static void serial8250_shutdown(struct uart_port *port)
2161{
2162 struct uart_8250_port *up = (struct uart_8250_port *)port;
2163 unsigned long flags;
2164
2165 /*
2166 * Disable interrupts from this port
2167 */
2168 up->ier = 0;
2169 serial_outp(up, UART_IER, 0);
2170
2171 spin_lock_irqsave(&up->port.lock, flags);
2172 if (up->port.flags & UPF_FOURPORT) {
2173 /* reset interrupts on the AST Fourport board */
2174 inb((up->port.iobase & 0xfe0) | 0x1f);
2175 up->port.mctrl |= TIOCM_OUT1;
2176 } else
2177 up->port.mctrl &= ~TIOCM_OUT2;
2178
2179 serial8250_set_mctrl(&up->port, up->port.mctrl);
2180 spin_unlock_irqrestore(&up->port.lock, flags);
2181
2182 /*
2183 * Disable break condition and FIFOs
2184 */
2185 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
2186 serial8250_clear_fifos(up);
2187
2188#ifdef CONFIG_SERIAL_8250_RSA
2189 /*
2190 * Reset the RSA board back to 115kbps compat mode.
2191 */
2192 disable_rsa(up);
2193#endif
2194
2195 /*
2196 * Read data port to reset things, and then unlink from
2197 * the IRQ chain.
2198 */
2199 (void) serial_in(up, UART_RX);
2200
Alex Williamson40b36da2007-02-14 00:33:04 -08002201 del_timer_sync(&up->timer);
2202 up->timer.function = serial8250_timeout;
2203 if (is_real_interrupt(up->port.irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 serial_unlink_irq_chain(up);
2205}
2206
2207static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
2208{
2209 unsigned int quot;
2210
2211 /*
2212 * Handle magic divisors for baud rates above baud_base on
2213 * SMSC SuperIO chips.
2214 */
2215 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2216 baud == (port->uartclk/4))
2217 quot = 0x8001;
2218 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2219 baud == (port->uartclk/8))
2220 quot = 0x8002;
2221 else
2222 quot = uart_get_divisor(port, baud);
2223
2224 return quot;
2225}
2226
2227static void
Alan Cox606d0992006-12-08 02:38:45 -08002228serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2229 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
2231 struct uart_8250_port *up = (struct uart_8250_port *)port;
2232 unsigned char cval, fcr = 0;
2233 unsigned long flags;
2234 unsigned int baud, quot;
2235
2236 switch (termios->c_cflag & CSIZE) {
2237 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +01002238 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 break;
2240 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +01002241 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 break;
2243 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +01002244 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 break;
2246 default:
2247 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +01002248 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 break;
2250 }
2251
2252 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +01002253 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (termios->c_cflag & PARENB)
2255 cval |= UART_LCR_PARITY;
2256 if (!(termios->c_cflag & PARODD))
2257 cval |= UART_LCR_EPAR;
2258#ifdef CMSPAR
2259 if (termios->c_cflag & CMSPAR)
2260 cval |= UART_LCR_SPAR;
2261#endif
2262
2263 /*
2264 * Ask the core to calculate the divisor for us.
2265 */
Anton Vorontsov24d481e2009-09-19 13:13:20 -07002266 baud = uart_get_baud_rate(port, termios, old,
2267 port->uartclk / 16 / 0xffff,
2268 port->uartclk / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 quot = serial8250_get_divisor(port, baud);
2270
2271 /*
Russell King4ba5e352005-06-23 10:43:04 +01002272 * Oxford Semi 952 rev B workaround
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 */
Russell King4ba5e352005-06-23 10:43:04 +01002274 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
Alan Cox3e8d4e22008-02-04 22:27:53 -08002275 quot++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
2278 if (baud < 2400)
2279 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2280 else
2281 fcr = uart_config[up->port.type].fcr;
2282 }
2283
2284 /*
2285 * MCR-based auto flow control. When AFE is enabled, RTS will be
2286 * deasserted when the receive FIFO contains more characters than
2287 * the trigger, or the MCR RTS bit is cleared. In the case where
2288 * the remote UART is not using CTS auto flow control, we must
2289 * have sufficient FIFO entries for the latency of the remote
2290 * UART to respond. IOW, at least 32 bytes of FIFO.
2291 */
2292 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
2293 up->mcr &= ~UART_MCR_AFE;
2294 if (termios->c_cflag & CRTSCTS)
2295 up->mcr |= UART_MCR_AFE;
2296 }
2297
2298 /*
2299 * Ok, we're now changing the port state. Do it with
2300 * interrupts disabled.
2301 */
2302 spin_lock_irqsave(&up->port.lock, flags);
2303
2304 /*
2305 * Update the per-port timeout.
2306 */
2307 uart_update_timeout(port, termios->c_cflag, baud);
2308
2309 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2310 if (termios->c_iflag & INPCK)
2311 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2312 if (termios->c_iflag & (BRKINT | PARMRK))
2313 up->port.read_status_mask |= UART_LSR_BI;
2314
2315 /*
2316 * Characteres to ignore
2317 */
2318 up->port.ignore_status_mask = 0;
2319 if (termios->c_iflag & IGNPAR)
2320 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2321 if (termios->c_iflag & IGNBRK) {
2322 up->port.ignore_status_mask |= UART_LSR_BI;
2323 /*
2324 * If we're ignoring parity and break indicators,
2325 * ignore overruns too (for real raw support).
2326 */
2327 if (termios->c_iflag & IGNPAR)
2328 up->port.ignore_status_mask |= UART_LSR_OE;
2329 }
2330
2331 /*
2332 * ignore all characters if CREAD is not set
2333 */
2334 if ((termios->c_cflag & CREAD) == 0)
2335 up->port.ignore_status_mask |= UART_LSR_DR;
2336
2337 /*
2338 * CTS flow control flag and modem status interrupts
2339 */
2340 up->ier &= ~UART_IER_MSI;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002341 if (!(up->bugs & UART_BUG_NOMSR) &&
2342 UART_ENABLE_MS(&up->port, termios->c_cflag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 up->ier |= UART_IER_MSI;
2344 if (up->capabilities & UART_CAP_UUE)
2345 up->ier |= UART_IER_UUE | UART_IER_RTOIE;
2346
2347 serial_out(up, UART_IER, up->ier);
2348
2349 if (up->capabilities & UART_CAP_EFR) {
2350 unsigned char efr = 0;
2351 /*
2352 * TI16C752/Startech hardware flow control. FIXME:
2353 * - TI16C752 requires control thresholds to be set.
2354 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2355 */
2356 if (termios->c_cflag & CRTSCTS)
2357 efr |= UART_EFR_CTS;
2358
2359 serial_outp(up, UART_LCR, 0xBF);
2360 serial_outp(up, UART_EFR, efr);
2361 }
2362
Russell Kingf2eda272008-09-01 21:47:59 +01002363#ifdef CONFIG_ARCH_OMAP
Jonathan McDowell255341c2006-08-14 23:05:32 -07002364 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
Russell King56685452008-09-01 21:25:33 +01002365 if (cpu_is_omap1510() && is_omap_port(up)) {
Jonathan McDowell255341c2006-08-14 23:05:32 -07002366 if (baud == 115200) {
2367 quot = 1;
2368 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2369 } else
2370 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2371 }
2372#endif
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (up->capabilities & UART_NATSEMI) {
2375 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
2376 serial_outp(up, UART_LCR, 0xe0);
2377 } else {
2378 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2379 }
2380
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +01002381 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 /*
2384 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2385 * is written without DLAB set, this mode will be disabled.
2386 */
2387 if (up->port.type == PORT_16750)
2388 serial_outp(up, UART_FCR, fcr);
2389
2390 serial_outp(up, UART_LCR, cval); /* reset DLAB */
2391 up->lcr = cval; /* Save LCR */
2392 if (up->port.type != PORT_16750) {
2393 if (fcr & UART_FCR_ENABLE_FIFO) {
2394 /* emulated UARTs (Lucent Venus 167x) need two steps */
2395 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
2396 }
2397 serial_outp(up, UART_FCR, fcr); /* set fcr */
2398 }
2399 serial8250_set_mctrl(&up->port, up->port.mctrl);
2400 spin_unlock_irqrestore(&up->port.lock, flags);
Alan Coxe991a2b2008-04-28 02:14:06 -07002401 /* Don't rewrite B0 */
2402 if (tty_termios_baud_rate(termios))
2403 tty_termios_encode_baud_rate(termios, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
2406static void
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002407serial8250_set_ldisc(struct uart_port *port)
2408{
2409 int line = port->line;
2410
2411 if (line >= port->state->port.tty->driver->num)
2412 return;
2413
2414 if (port->state->port.tty->ldisc->ops->num == N_PPS) {
2415 port->flags |= UPF_HARDPPS_CD;
2416 serial8250_enable_ms(port);
2417 } else
2418 port->flags &= ~UPF_HARDPPS_CD;
2419}
2420
2421static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422serial8250_pm(struct uart_port *port, unsigned int state,
2423 unsigned int oldstate)
2424{
2425 struct uart_8250_port *p = (struct uart_8250_port *)port;
2426
2427 serial8250_set_sleep(p, state != 0);
2428
2429 if (p->pm)
2430 p->pm(port, state, oldstate);
2431}
2432
Russell Kingf2eda272008-09-01 21:47:59 +01002433static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2434{
2435 if (pt->port.iotype == UPIO_AU)
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002436 return 0x1000;
Russell Kingf2eda272008-09-01 21:47:59 +01002437#ifdef CONFIG_ARCH_OMAP
2438 if (is_omap_port(pt))
2439 return 0x16 << pt->port.regshift;
2440#endif
2441 return 8 << pt->port.regshift;
2442}
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444/*
2445 * Resource handling.
2446 */
2447static int serial8250_request_std_resource(struct uart_8250_port *up)
2448{
Russell Kingf2eda272008-09-01 21:47:59 +01002449 unsigned int size = serial8250_port_size(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 int ret = 0;
2451
2452 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002453 case UPIO_AU:
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002454 case UPIO_TSI:
2455 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 case UPIO_MEM:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002457 case UPIO_DWAPB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (!up->port.mapbase)
2459 break;
2460
2461 if (!request_mem_region(up->port.mapbase, size, "serial")) {
2462 ret = -EBUSY;
2463 break;
2464 }
2465
2466 if (up->port.flags & UPF_IOREMAP) {
Alan Cox6f441fe2008-05-01 04:34:59 -07002467 up->port.membase = ioremap_nocache(up->port.mapbase,
2468 size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (!up->port.membase) {
2470 release_mem_region(up->port.mapbase, size);
2471 ret = -ENOMEM;
2472 }
2473 }
2474 break;
2475
2476 case UPIO_HUB6:
2477 case UPIO_PORT:
2478 if (!request_region(up->port.iobase, size, "serial"))
2479 ret = -EBUSY;
2480 break;
2481 }
2482 return ret;
2483}
2484
2485static void serial8250_release_std_resource(struct uart_8250_port *up)
2486{
Russell Kingf2eda272008-09-01 21:47:59 +01002487 unsigned int size = serial8250_port_size(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002490 case UPIO_AU:
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002491 case UPIO_TSI:
2492 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 case UPIO_MEM:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002494 case UPIO_DWAPB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (!up->port.mapbase)
2496 break;
2497
2498 if (up->port.flags & UPF_IOREMAP) {
2499 iounmap(up->port.membase);
2500 up->port.membase = NULL;
2501 }
2502
2503 release_mem_region(up->port.mapbase, size);
2504 break;
2505
2506 case UPIO_HUB6:
2507 case UPIO_PORT:
2508 release_region(up->port.iobase, size);
2509 break;
2510 }
2511}
2512
2513static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2514{
2515 unsigned long start = UART_RSA_BASE << up->port.regshift;
2516 unsigned int size = 8 << up->port.regshift;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002517 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 case UPIO_HUB6:
2521 case UPIO_PORT:
2522 start += up->port.iobase;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002523 if (request_region(start, size, "serial-rsa"))
2524 ret = 0;
2525 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 ret = -EBUSY;
2527 break;
2528 }
2529
2530 return ret;
2531}
2532
2533static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2534{
2535 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2536 unsigned int size = 8 << up->port.regshift;
2537
2538 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 case UPIO_HUB6:
2540 case UPIO_PORT:
2541 release_region(up->port.iobase + offset, size);
2542 break;
2543 }
2544}
2545
2546static void serial8250_release_port(struct uart_port *port)
2547{
2548 struct uart_8250_port *up = (struct uart_8250_port *)port;
2549
2550 serial8250_release_std_resource(up);
2551 if (up->port.type == PORT_RSA)
2552 serial8250_release_rsa_resource(up);
2553}
2554
2555static int serial8250_request_port(struct uart_port *port)
2556{
2557 struct uart_8250_port *up = (struct uart_8250_port *)port;
2558 int ret = 0;
2559
2560 ret = serial8250_request_std_resource(up);
2561 if (ret == 0 && up->port.type == PORT_RSA) {
2562 ret = serial8250_request_rsa_resource(up);
2563 if (ret < 0)
2564 serial8250_release_std_resource(up);
2565 }
2566
2567 return ret;
2568}
2569
2570static void serial8250_config_port(struct uart_port *port, int flags)
2571{
2572 struct uart_8250_port *up = (struct uart_8250_port *)port;
2573 int probeflags = PROBE_ANY;
2574 int ret;
2575
2576 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 * Find the region that we can probe for. This in turn
2578 * tells us whether we can probe for the type of port.
2579 */
2580 ret = serial8250_request_std_resource(up);
2581 if (ret < 0)
2582 return;
2583
2584 ret = serial8250_request_rsa_resource(up);
2585 if (ret < 0)
2586 probeflags &= ~PROBE_RSA;
2587
Alan Coxb8e7e402009-05-28 14:01:35 +01002588 if (up->port.iotype != up->cur_iotype)
2589 set_io_from_upio(port);
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (flags & UART_CONFIG_TYPE)
2592 autoconfig(up, probeflags);
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002593
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002594 /* if access method is AU, it is a 16550 with a quirk */
2595 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
2596 up->bugs |= UART_BUG_NOMSR;
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2599 autoconfig_irq(up);
2600
2601 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2602 serial8250_release_rsa_resource(up);
2603 if (up->port.type == PORT_UNKNOWN)
2604 serial8250_release_std_resource(up);
2605}
2606
2607static int
2608serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2609{
Yinghai Lua62c4132008-08-19 20:49:55 -07002610 if (ser->irq >= nr_irqs || ser->irq < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2612 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2613 ser->type == PORT_STARTECH)
2614 return -EINVAL;
2615 return 0;
2616}
2617
2618static const char *
2619serial8250_type(struct uart_port *port)
2620{
2621 int type = port->type;
2622
2623 if (type >= ARRAY_SIZE(uart_config))
2624 type = 0;
2625 return uart_config[type].name;
2626}
2627
2628static struct uart_ops serial8250_pops = {
2629 .tx_empty = serial8250_tx_empty,
2630 .set_mctrl = serial8250_set_mctrl,
2631 .get_mctrl = serial8250_get_mctrl,
2632 .stop_tx = serial8250_stop_tx,
2633 .start_tx = serial8250_start_tx,
2634 .stop_rx = serial8250_stop_rx,
2635 .enable_ms = serial8250_enable_ms,
2636 .break_ctl = serial8250_break_ctl,
2637 .startup = serial8250_startup,
2638 .shutdown = serial8250_shutdown,
2639 .set_termios = serial8250_set_termios,
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002640 .set_ldisc = serial8250_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 .pm = serial8250_pm,
2642 .type = serial8250_type,
2643 .release_port = serial8250_release_port,
2644 .request_port = serial8250_request_port,
2645 .config_port = serial8250_config_port,
2646 .verify_port = serial8250_verify_port,
Jason Wesself2d937f2008-04-17 20:05:37 +02002647#ifdef CONFIG_CONSOLE_POLL
2648 .poll_get_char = serial8250_get_poll_char,
2649 .poll_put_char = serial8250_put_poll_char,
2650#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651};
2652
2653static struct uart_8250_port serial8250_ports[UART_NR];
2654
2655static void __init serial8250_isa_init_ports(void)
2656{
2657 struct uart_8250_port *up;
2658 static int first = 1;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002659 int i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
2661 if (!first)
2662 return;
2663 first = 0;
2664
Dave Jonesa61c2d72006-01-07 23:18:19 +00002665 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 struct uart_8250_port *up = &serial8250_ports[i];
2667
2668 up->port.line = i;
2669 spin_lock_init(&up->port.lock);
2670
2671 init_timer(&up->timer);
2672 up->timer.function = serial8250_timeout;
2673
2674 /*
2675 * ALPHA_KLUDGE_MCR needs to be killed.
2676 */
2677 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2678 up->mcr_force = ALPHA_KLUDGE_MCR;
2679
2680 up->port.ops = &serial8250_pops;
2681 }
2682
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002683 if (share_irqs)
2684 irqflag = IRQF_SHARED;
2685
Russell King44454bc2005-06-30 22:41:22 +01002686 for (i = 0, up = serial8250_ports;
Dave Jonesa61c2d72006-01-07 23:18:19 +00002687 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 i++, up++) {
2689 up->port.iobase = old_serial_port[i].port;
2690 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002691 up->port.irqflags = old_serial_port[i].irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 up->port.uartclk = old_serial_port[i].baud_base * 16;
2693 up->port.flags = old_serial_port[i].flags;
2694 up->port.hub6 = old_serial_port[i].hub6;
2695 up->port.membase = old_serial_port[i].iomem_base;
2696 up->port.iotype = old_serial_port[i].io_type;
2697 up->port.regshift = old_serial_port[i].iomem_reg_shift;
David Daney7d6a07d2009-01-02 13:49:47 +00002698 set_io_from_upio(&up->port);
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002699 up->port.irqflags |= irqflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
2701}
2702
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08002703static void
2704serial8250_init_fixed_type_port(struct uart_8250_port *up, unsigned int type)
2705{
2706 up->port.type = type;
2707 up->port.fifosize = uart_config[type].fifo_size;
2708 up->capabilities = uart_config[type].flags;
2709 up->tx_loadsz = uart_config[type].tx_loadsz;
2710}
2711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712static void __init
2713serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2714{
2715 int i;
2716
Alan Coxb8e7e402009-05-28 14:01:35 +01002717 for (i = 0; i < nr_uarts; i++) {
2718 struct uart_8250_port *up = &serial8250_ports[i];
2719 up->cur_iotype = 0xFF;
2720 }
2721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 serial8250_isa_init_ports();
2723
Dave Jonesa61c2d72006-01-07 23:18:19 +00002724 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 struct uart_8250_port *up = &serial8250_ports[i];
2726
2727 up->port.dev = dev;
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08002728
2729 if (up->port.flags & UPF_FIXED_TYPE)
2730 serial8250_init_fixed_type_port(up, up->port.type);
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 uart_add_one_port(drv, &up->port);
2733 }
2734}
2735
2736#ifdef CONFIG_SERIAL_8250_CONSOLE
2737
Russell Kingd3587882006-03-20 20:00:09 +00002738static void serial8250_console_putchar(struct uart_port *port, int ch)
2739{
2740 struct uart_8250_port *up = (struct uart_8250_port *)port;
2741
2742 wait_for_xmitr(up, UART_LSR_THRE);
2743 serial_out(up, UART_TX, ch);
2744}
2745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746/*
2747 * Print a string to the serial port trying not to disturb
2748 * any possible real use of the port...
2749 *
2750 * The console_lock must be held when we get here.
2751 */
2752static void
2753serial8250_console_write(struct console *co, const char *s, unsigned int count)
2754{
2755 struct uart_8250_port *up = &serial8250_ports[co->index];
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002756 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 unsigned int ier;
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002758 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Andrew Morton78512ec2005-11-07 00:59:13 -08002760 touch_nmi_watchdog();
2761
Andrew Morton68aa2c02006-06-30 02:29:59 -07002762 local_irq_save(flags);
2763 if (up->port.sysrq) {
2764 /* serial8250_handle_port() already took the lock */
2765 locked = 0;
2766 } else if (oops_in_progress) {
2767 locked = spin_trylock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002768 } else
Andrew Morton68aa2c02006-06-30 02:29:59 -07002769 spin_lock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 /*
Ralf Baechledc7bf132006-02-15 09:59:47 +00002772 * First save the IER then disable the interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 */
2774 ier = serial_in(up, UART_IER);
2775
2776 if (up->capabilities & UART_CAP_UUE)
2777 serial_out(up, UART_IER, UART_IER_UUE);
2778 else
2779 serial_out(up, UART_IER, 0);
2780
Russell Kingd3587882006-03-20 20:00:09 +00002781 uart_console_write(&up->port, s, count, serial8250_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 /*
2784 * Finally, wait for transmitter to become empty
2785 * and restore the IER
2786 */
Alan Coxf91a3712006-01-21 14:59:12 +00002787 wait_for_xmitr(up, BOTH_EMPTY);
Russell Kinga88d75b2006-04-30 11:30:15 +01002788 serial_out(up, UART_IER, ier);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002789
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002790 /*
2791 * The receive handling will happen properly because the
2792 * receive ready bit will still be set; it is not cleared
2793 * on read. However, modem control will not, we must
2794 * call it if we have saved something in the saved flags
2795 * while processing with interrupts off.
2796 */
2797 if (up->msr_saved_flags)
2798 check_modem_status(up);
2799
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002800 if (locked)
Andrew Morton68aa2c02006-06-30 02:29:59 -07002801 spin_unlock(&up->port.lock);
2802 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803}
2804
Vivek Goyal118c0ac2007-01-11 01:52:44 +01002805static int __init serial8250_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
2807 struct uart_port *port;
2808 int baud = 9600;
2809 int bits = 8;
2810 int parity = 'n';
2811 int flow = 'n';
2812
2813 /*
2814 * Check whether an invalid uart number has been specified, and
2815 * if so, search for the first available port that does have
2816 * console support.
2817 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002818 if (co->index >= nr_uarts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 co->index = 0;
2820 port = &serial8250_ports[co->index].port;
2821 if (!port->iobase && !port->membase)
2822 return -ENODEV;
2823
2824 if (options)
2825 uart_parse_options(options, &baud, &parity, &bits, &flow);
2826
2827 return uart_set_options(port, co, baud, parity, bits, flow);
2828}
2829
Daniel Ritzb6b1d872007-08-03 16:07:43 +02002830static int serial8250_console_early_setup(void)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002831{
2832 return serial8250_find_port_for_earlycon();
2833}
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835static struct console serial8250_console = {
2836 .name = "ttyS",
2837 .write = serial8250_console_write,
2838 .device = uart_console_device,
2839 .setup = serial8250_console_setup,
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002840 .early_setup = serial8250_console_early_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 .flags = CON_PRINTBUFFER,
2842 .index = -1,
2843 .data = &serial8250_reg,
2844};
2845
2846static int __init serial8250_console_init(void)
2847{
Eric W. Biederman05d81d22008-07-12 13:47:53 -07002848 if (nr_uarts > UART_NR)
2849 nr_uarts = UART_NR;
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 serial8250_isa_init_ports();
2852 register_console(&serial8250_console);
2853 return 0;
2854}
2855console_initcall(serial8250_console_init);
2856
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002857int serial8250_find_port(struct uart_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
2859 int line;
2860 struct uart_port *port;
2861
Dave Jonesa61c2d72006-01-07 23:18:19 +00002862 for (line = 0; line < nr_uarts; line++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 port = &serial8250_ports[line].port;
Russell King50aec3b52006-01-04 18:13:03 +00002864 if (uart_match_port(p, port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 return line;
2866 }
2867 return -ENODEV;
2868}
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870#define SERIAL8250_CONSOLE &serial8250_console
2871#else
2872#define SERIAL8250_CONSOLE NULL
2873#endif
2874
2875static struct uart_driver serial8250_reg = {
2876 .owner = THIS_MODULE,
2877 .driver_name = "serial",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 .dev_name = "ttyS",
2879 .major = TTY_MAJOR,
2880 .minor = 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 .cons = SERIAL8250_CONSOLE,
2882};
2883
Russell Kingd856c662006-02-23 10:22:13 +00002884/*
2885 * early_serial_setup - early registration for 8250 ports
2886 *
2887 * Setup an 8250 port structure prior to console initialisation. Use
2888 * after console initialisation will cause undefined behaviour.
2889 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890int __init early_serial_setup(struct uart_port *port)
2891{
David Daneyb4304282009-01-02 13:49:41 +00002892 struct uart_port *p;
2893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (port->line >= ARRAY_SIZE(serial8250_ports))
2895 return -ENODEV;
2896
2897 serial8250_isa_init_ports();
David Daneyb4304282009-01-02 13:49:41 +00002898 p = &serial8250_ports[port->line].port;
2899 p->iobase = port->iobase;
2900 p->membase = port->membase;
2901 p->irq = port->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002902 p->irqflags = port->irqflags;
David Daneyb4304282009-01-02 13:49:41 +00002903 p->uartclk = port->uartclk;
2904 p->fifosize = port->fifosize;
2905 p->regshift = port->regshift;
2906 p->iotype = port->iotype;
2907 p->flags = port->flags;
2908 p->mapbase = port->mapbase;
2909 p->private_data = port->private_data;
Helge Deller125c97d2009-01-13 22:51:07 +01002910 p->type = port->type;
2911 p->line = port->line;
David Daney7d6a07d2009-01-02 13:49:47 +00002912
2913 set_io_from_upio(p);
2914 if (port->serial_in)
2915 p->serial_in = port->serial_in;
2916 if (port->serial_out)
2917 p->serial_out = port->serial_out;
2918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return 0;
2920}
2921
2922/**
2923 * serial8250_suspend_port - suspend one serial port
2924 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 *
2926 * Suspend one serial port.
2927 */
2928void serial8250_suspend_port(int line)
2929{
2930 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2931}
2932
2933/**
2934 * serial8250_resume_port - resume one serial port
2935 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 *
2937 * Resume one serial port.
2938 */
2939void serial8250_resume_port(int line)
2940{
David Woodhouseb5b82df2007-05-17 14:27:39 +08002941 struct uart_8250_port *up = &serial8250_ports[line];
2942
2943 if (up->capabilities & UART_NATSEMI) {
2944 unsigned char tmp;
2945
2946 /* Ensure it's still in high speed mode */
2947 serial_outp(up, UART_LCR, 0xE0);
2948
2949 tmp = serial_in(up, 0x04); /* EXCR2 */
2950 tmp &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
2951 tmp |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
2952 serial_outp(up, 0x04, tmp);
2953
2954 serial_outp(up, UART_LCR, 0);
2955 }
2956 uart_resume_port(&serial8250_reg, &up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957}
2958
2959/*
2960 * Register a set of serial devices attached to a platform device. The
2961 * list is terminated with a zero flags entry, which means we expect
2962 * all entries to have at least UPF_BOOT_AUTOCONF set.
2963 */
Russell King3ae5eae2005-11-09 22:32:44 +00002964static int __devinit serial8250_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
Russell King3ae5eae2005-11-09 22:32:44 +00002966 struct plat_serial8250_port *p = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 struct uart_port port;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002968 int ret, i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
2970 memset(&port, 0, sizeof(struct uart_port));
2971
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002972 if (share_irqs)
2973 irqflag = IRQF_SHARED;
2974
Russell Kingec9f47c2005-06-27 11:12:54 +01002975 for (i = 0; p && p->flags != 0; p++, i++) {
Will Newton74a197412008-02-04 22:27:50 -08002976 port.iobase = p->iobase;
2977 port.membase = p->membase;
2978 port.irq = p->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002979 port.irqflags = p->irqflags;
Will Newton74a197412008-02-04 22:27:50 -08002980 port.uartclk = p->uartclk;
2981 port.regshift = p->regshift;
2982 port.iotype = p->iotype;
2983 port.flags = p->flags;
2984 port.mapbase = p->mapbase;
2985 port.hub6 = p->hub6;
2986 port.private_data = p->private_data;
David Daney8e23fcc2009-01-02 13:49:54 +00002987 port.type = p->type;
David Daney7d6a07d2009-01-02 13:49:47 +00002988 port.serial_in = p->serial_in;
2989 port.serial_out = p->serial_out;
Will Newton74a197412008-02-04 22:27:50 -08002990 port.dev = &dev->dev;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002991 port.irqflags |= irqflag;
Russell Kingec9f47c2005-06-27 11:12:54 +01002992 ret = serial8250_register_port(&port);
2993 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00002994 dev_err(&dev->dev, "unable to register port at index %d "
Josh Boyer4f640ef2007-07-23 18:43:44 -07002995 "(IO%lx MEM%llx IRQ%d): %d\n", i,
2996 p->iobase, (unsigned long long)p->mapbase,
2997 p->irq, ret);
Russell Kingec9f47c2005-06-27 11:12:54 +01002998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 }
3000 return 0;
3001}
3002
3003/*
3004 * Remove serial ports registered against a platform device.
3005 */
Russell King3ae5eae2005-11-09 22:32:44 +00003006static int __devexit serial8250_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
3008 int i;
3009
Dave Jonesa61c2d72006-01-07 23:18:19 +00003010 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 struct uart_8250_port *up = &serial8250_ports[i];
3012
Russell King3ae5eae2005-11-09 22:32:44 +00003013 if (up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 serial8250_unregister_port(i);
3015 }
3016 return 0;
3017}
3018
Russell King3ae5eae2005-11-09 22:32:44 +00003019static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
3021 int i;
3022
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 for (i = 0; i < UART_NR; i++) {
3024 struct uart_8250_port *up = &serial8250_ports[i];
3025
Russell King3ae5eae2005-11-09 22:32:44 +00003026 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 uart_suspend_port(&serial8250_reg, &up->port);
3028 }
3029
3030 return 0;
3031}
3032
Russell King3ae5eae2005-11-09 22:32:44 +00003033static int serial8250_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
3035 int i;
3036
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 for (i = 0; i < UART_NR; i++) {
3038 struct uart_8250_port *up = &serial8250_ports[i];
3039
Russell King3ae5eae2005-11-09 22:32:44 +00003040 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
David Woodhouseb5b82df2007-05-17 14:27:39 +08003041 serial8250_resume_port(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
3043
3044 return 0;
3045}
3046
Russell King3ae5eae2005-11-09 22:32:44 +00003047static struct platform_driver serial8250_isa_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 .probe = serial8250_probe,
3049 .remove = __devexit_p(serial8250_remove),
3050 .suspend = serial8250_suspend,
3051 .resume = serial8250_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00003052 .driver = {
3053 .name = "serial8250",
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003054 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00003055 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056};
3057
3058/*
3059 * This "device" covers _all_ ISA 8250-compatible serial devices listed
3060 * in the table in include/asm/serial.h
3061 */
3062static struct platform_device *serial8250_isa_devs;
3063
3064/*
3065 * serial8250_register_port and serial8250_unregister_port allows for
3066 * 16x50 serial ports to be configured at run-time, to support PCMCIA
3067 * modems and PCI multiport cards.
3068 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003069static DEFINE_MUTEX(serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
3072{
3073 int i;
3074
3075 /*
3076 * First, find a port entry which matches.
3077 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003078 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 if (uart_match_port(&serial8250_ports[i].port, port))
3080 return &serial8250_ports[i];
3081
3082 /*
3083 * We didn't find a matching entry, so look for the first
3084 * free entry. We look for one which hasn't been previously
3085 * used (indicated by zero iobase).
3086 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003087 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
3089 serial8250_ports[i].port.iobase == 0)
3090 return &serial8250_ports[i];
3091
3092 /*
3093 * That also failed. Last resort is to find any entry which
3094 * doesn't have a real port associated with it.
3095 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003096 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
3098 return &serial8250_ports[i];
3099
3100 return NULL;
3101}
3102
3103/**
3104 * serial8250_register_port - register a serial port
3105 * @port: serial port template
3106 *
3107 * Configure the serial port specified by the request. If the
3108 * port exists and is in use, it is hung up and unregistered
3109 * first.
3110 *
3111 * The port is then probed and if necessary the IRQ is autodetected
3112 * If this fails an error is returned.
3113 *
3114 * On success the port is ready to use and the line number is returned.
3115 */
3116int serial8250_register_port(struct uart_port *port)
3117{
3118 struct uart_8250_port *uart;
3119 int ret = -ENOSPC;
3120
3121 if (port->uartclk == 0)
3122 return -EINVAL;
3123
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003124 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
3126 uart = serial8250_find_match_or_unused(port);
3127 if (uart) {
3128 uart_remove_one_port(&serial8250_reg, &uart->port);
3129
Will Newton74a197412008-02-04 22:27:50 -08003130 uart->port.iobase = port->iobase;
3131 uart->port.membase = port->membase;
3132 uart->port.irq = port->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07003133 uart->port.irqflags = port->irqflags;
Will Newton74a197412008-02-04 22:27:50 -08003134 uart->port.uartclk = port->uartclk;
3135 uart->port.fifosize = port->fifosize;
3136 uart->port.regshift = port->regshift;
3137 uart->port.iotype = port->iotype;
3138 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
3139 uart->port.mapbase = port->mapbase;
3140 uart->port.private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 if (port->dev)
3142 uart->port.dev = port->dev;
David Daney8e23fcc2009-01-02 13:49:54 +00003143
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08003144 if (port->flags & UPF_FIXED_TYPE)
3145 serial8250_init_fixed_type_port(uart, port->type);
David Daney8e23fcc2009-01-02 13:49:54 +00003146
David Daney7d6a07d2009-01-02 13:49:47 +00003147 set_io_from_upio(&uart->port);
3148 /* Possibly override default I/O functions. */
3149 if (port->serial_in)
3150 uart->port.serial_in = port->serial_in;
3151 if (port->serial_out)
3152 uart->port.serial_out = port->serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
3154 ret = uart_add_one_port(&serial8250_reg, &uart->port);
3155 if (ret == 0)
3156 ret = uart->port.line;
3157 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003158 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
3160 return ret;
3161}
3162EXPORT_SYMBOL(serial8250_register_port);
3163
3164/**
3165 * serial8250_unregister_port - remove a 16x50 serial port at runtime
3166 * @line: serial line number
3167 *
3168 * Remove one serial port. This may not be called from interrupt
3169 * context. We hand the port back to the our control.
3170 */
3171void serial8250_unregister_port(int line)
3172{
3173 struct uart_8250_port *uart = &serial8250_ports[line];
3174
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003175 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 uart_remove_one_port(&serial8250_reg, &uart->port);
3177 if (serial8250_isa_devs) {
3178 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
3179 uart->port.type = PORT_UNKNOWN;
3180 uart->port.dev = &serial8250_isa_devs->dev;
3181 uart_add_one_port(&serial8250_reg, &uart->port);
3182 } else {
3183 uart->port.dev = NULL;
3184 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003185 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186}
3187EXPORT_SYMBOL(serial8250_unregister_port);
3188
3189static int __init serial8250_init(void)
3190{
Alan Cox25db8ad2008-08-19 20:49:40 -07003191 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Dave Jonesa61c2d72006-01-07 23:18:19 +00003193 if (nr_uarts > UART_NR)
3194 nr_uarts = UART_NR;
3195
Paul Bollef1fb9bb2008-12-30 14:06:43 +01003196 printk(KERN_INFO "Serial: 8250/16550 driver, "
Dave Jonesa61c2d72006-01-07 23:18:19 +00003197 "%d ports, IRQ sharing %sabled\n", nr_uarts,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 share_irqs ? "en" : "dis");
3199
David Millerb70ac772008-10-13 10:36:31 +01003200#ifdef CONFIG_SPARC
3201 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
3202#else
3203 serial8250_reg.nr = UART_NR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 ret = uart_register_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003205#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if (ret)
3207 goto out;
3208
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003209 serial8250_isa_devs = platform_device_alloc("serial8250",
3210 PLAT8250_DEV_LEGACY);
3211 if (!serial8250_isa_devs) {
3212 ret = -ENOMEM;
Russell Kingbc965a72006-01-18 09:54:29 +00003213 goto unreg_uart_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 }
3215
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003216 ret = platform_device_add(serial8250_isa_devs);
3217 if (ret)
3218 goto put_dev;
3219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
3221
Russell Kingbc965a72006-01-18 09:54:29 +00003222 ret = platform_driver_register(&serial8250_isa_driver);
3223 if (ret == 0)
3224 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Russell Kingbc965a72006-01-18 09:54:29 +00003226 platform_device_del(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003227put_dev:
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003228 platform_device_put(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003229unreg_uart_drv:
David Millerb70ac772008-10-13 10:36:31 +01003230#ifdef CONFIG_SPARC
3231 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3232#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003234#endif
Alan Cox25db8ad2008-08-19 20:49:40 -07003235out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 return ret;
3237}
3238
3239static void __exit serial8250_exit(void)
3240{
3241 struct platform_device *isa_dev = serial8250_isa_devs;
3242
3243 /*
3244 * This tells serial8250_unregister_port() not to re-register
3245 * the ports (thereby making serial8250_isa_driver permanently
3246 * in use.)
3247 */
3248 serial8250_isa_devs = NULL;
3249
Russell King3ae5eae2005-11-09 22:32:44 +00003250 platform_driver_unregister(&serial8250_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 platform_device_unregister(isa_dev);
3252
David Millerb70ac772008-10-13 10:36:31 +01003253#ifdef CONFIG_SPARC
3254 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3255#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003257#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
3260module_init(serial8250_init);
3261module_exit(serial8250_exit);
3262
3263EXPORT_SYMBOL(serial8250_suspend_port);
3264EXPORT_SYMBOL(serial8250_resume_port);
3265
3266MODULE_LICENSE("GPL");
Adrian Bunkd87a6d92008-07-16 21:53:31 +01003267MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
3269module_param(share_irqs, uint, 0644);
3270MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
3271 " (unsafe)");
3272
Dave Jonesa61c2d72006-01-07 23:18:19 +00003273module_param(nr_uarts, uint, 0644);
3274MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
3275
Chuck Ebbertd41a4b52009-10-01 15:44:26 -07003276module_param(skip_txen_test, uint, 0644);
3277MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
3278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279#ifdef CONFIG_SERIAL_8250_RSA
3280module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
3281MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
3282#endif
3283MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);