blob: 5274228fa03ccccebb0eefe962f1ccb10aec0a47 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver for 8250/16550-type serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2001 Russell King.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * A note about mapbase / membase
14 *
15 * mapbase is the physical address of the IO port.
16 * membase is an 'ioremapped' cookie.
17 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
20#define SUPPORT_SYSRQ
21#endif
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/ioport.h>
26#include <linux/init.h>
27#include <linux/console.h>
28#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/delay.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010030#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/tty.h>
Daniel Drakecd3ecad2010-10-20 16:00:48 -070032#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/tty_flip.h>
34#include <linux/serial_reg.h>
35#include <linux/serial_core.h>
36#include <linux/serial.h>
37#include <linux/serial_8250.h>
Andrew Morton78512ec2005-11-07 00:59:13 -080038#include <linux/nmi.h>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000039#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/io.h>
43#include <asm/irq.h>
44
45#include "8250.h"
46
David Millerb70ac772008-10-13 10:36:31 +010047#ifdef CONFIG_SPARC
48#include "suncore.h"
49#endif
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * Configuration:
Thomas Gleixner40663cc2006-07-01 19:29:43 -070053 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * is unsafe when used on edge-triggered interrupts.
55 */
Adrian Bunk408b6642005-05-01 08:59:29 -070056static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Dave Jonesa61c2d72006-01-07 23:18:19 +000058static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
59
David S. Miller84408382008-10-13 10:45:26 +010060static struct uart_driver serial8250_reg;
61
62static int serial_index(struct uart_port *port)
63{
64 return (serial8250_reg.minor - 64) + port->line;
65}
66
Chuck Ebbertd41a4b52009-10-01 15:44:26 -070067static unsigned int skip_txen_test; /* force skip of txen test at init time */
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
70 * Debugging.
71 */
72#if 0
73#define DEBUG_AUTOCONF(fmt...) printk(fmt)
74#else
75#define DEBUG_AUTOCONF(fmt...) do { } while (0)
76#endif
77
78#if 0
79#define DEBUG_INTR(fmt...) printk(fmt)
80#else
81#define DEBUG_INTR(fmt...) do { } while (0)
82#endif
83
Jiri Slabye7328ae2011-06-05 22:51:49 +020084#define PASS_LIMIT 512
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Dick Hollenbeckbca47612009-12-09 12:31:34 -080086#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
87
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * We default to IRQ0 for the "no irq" hack. Some
91 * machine types want others as well - they're free
92 * to redefine this in their header file.
93 */
94#define is_real_interrupt(irq) ((irq) != 0)
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
97#define CONFIG_SERIAL_DETECT_IRQ 1
98#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#ifdef CONFIG_SERIAL_8250_MANY_PORTS
100#define CONFIG_SERIAL_MANY_PORTS 1
101#endif
102
103/*
104 * HUB6 is always on. This will be removed once the header
105 * files have been cleaned.
106 */
107#define CONFIG_HUB6 1
108
Bryan Wua4ed1e42008-05-31 16:10:04 +0800109#include <asm/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
111 * SERIAL_PORT_DFNS tells us about built-in ports that have no
112 * standard enumeration mechanism. Platforms that can find all
113 * serial ports via mechanisms like ACPI or PCI need not supply it.
114 */
115#ifndef SERIAL_PORT_DFNS
116#define SERIAL_PORT_DFNS
117#endif
118
Arjan van de Vencb3592b2005-11-28 21:04:11 +0000119static const struct old_serial_port old_serial_port[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 SERIAL_PORT_DFNS /* defined in asm/serial.h */
121};
122
Russell King026d02a2005-06-29 18:45:19 +0100123#define UART_NR CONFIG_SERIAL_8250_NR_UARTS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125#ifdef CONFIG_SERIAL_8250_RSA
126
127#define PORT_RSA_MAX 4
128static unsigned long probe_rsa[PORT_RSA_MAX];
129static unsigned int probe_rsa_count;
130#endif /* CONFIG_SERIAL_8250_RSA */
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132struct irq_info {
Alan Cox25db8ad2008-08-19 20:49:40 -0700133 struct hlist_node node;
134 int irq;
135 spinlock_t lock; /* Protects list not the hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 struct list_head *head;
137};
138
Alan Cox25db8ad2008-08-19 20:49:40 -0700139#define NR_IRQ_HASH 32 /* Can be adjusted later */
140static struct hlist_head irq_lists[NR_IRQ_HASH];
141static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143/*
144 * Here we define the default xmit fifo size used for each type of UART.
145 */
146static const struct serial8250_config uart_config[] = {
147 [PORT_UNKNOWN] = {
148 .name = "unknown",
149 .fifo_size = 1,
150 .tx_loadsz = 1,
151 },
152 [PORT_8250] = {
153 .name = "8250",
154 .fifo_size = 1,
155 .tx_loadsz = 1,
156 },
157 [PORT_16450] = {
158 .name = "16450",
159 .fifo_size = 1,
160 .tx_loadsz = 1,
161 },
162 [PORT_16550] = {
163 .name = "16550",
164 .fifo_size = 1,
165 .tx_loadsz = 1,
166 },
167 [PORT_16550A] = {
168 .name = "16550A",
169 .fifo_size = 16,
170 .tx_loadsz = 16,
171 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
172 .flags = UART_CAP_FIFO,
173 },
174 [PORT_CIRRUS] = {
175 .name = "Cirrus",
176 .fifo_size = 1,
177 .tx_loadsz = 1,
178 },
179 [PORT_16650] = {
180 .name = "ST16650",
181 .fifo_size = 1,
182 .tx_loadsz = 1,
183 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
184 },
185 [PORT_16650V2] = {
186 .name = "ST16650V2",
187 .fifo_size = 32,
188 .tx_loadsz = 16,
189 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
190 UART_FCR_T_TRIG_00,
191 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
192 },
193 [PORT_16750] = {
194 .name = "TI16750",
195 .fifo_size = 64,
196 .tx_loadsz = 64,
197 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
198 UART_FCR7_64BYTE,
199 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
200 },
201 [PORT_STARTECH] = {
202 .name = "Startech",
203 .fifo_size = 1,
204 .tx_loadsz = 1,
205 },
206 [PORT_16C950] = {
207 .name = "16C950/954",
208 .fifo_size = 128,
209 .tx_loadsz = 128,
210 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
Pavel Machekd0694e22011-01-09 08:38:48 +0100211 /* UART_CAP_EFR breaks billionon CF bluetooth card. */
212 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 },
214 [PORT_16654] = {
215 .name = "ST16654",
216 .fifo_size = 64,
217 .tx_loadsz = 32,
218 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
219 UART_FCR_T_TRIG_10,
220 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
221 },
222 [PORT_16850] = {
223 .name = "XR16850",
224 .fifo_size = 128,
225 .tx_loadsz = 128,
226 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
227 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
228 },
229 [PORT_RSA] = {
230 .name = "RSA",
231 .fifo_size = 2048,
232 .tx_loadsz = 2048,
233 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
234 .flags = UART_CAP_FIFO,
235 },
236 [PORT_NS16550A] = {
237 .name = "NS16550A",
238 .fifo_size = 16,
239 .tx_loadsz = 16,
240 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
241 .flags = UART_CAP_FIFO | UART_NATSEMI,
242 },
243 [PORT_XSCALE] = {
244 .name = "XScale",
245 .fifo_size = 32,
246 .tx_loadsz = 32,
247 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
Stephen Warren4539c242011-05-17 16:12:36 -0600248 .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 },
Thomas Koellerbd71c182007-05-06 14:48:47 -0700250 [PORT_RM9000] = {
251 .name = "RM9000",
252 .fifo_size = 16,
253 .tx_loadsz = 16,
254 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
255 .flags = UART_CAP_FIFO,
256 },
David Daney6b06f192009-01-02 13:50:00 +0000257 [PORT_OCTEON] = {
258 .name = "OCTEON",
259 .fifo_size = 64,
260 .tx_loadsz = 64,
261 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
262 .flags = UART_CAP_FIFO,
263 },
Florian Fainelli08e09922009-06-11 13:21:24 +0100264 [PORT_AR7] = {
265 .name = "AR7",
266 .fifo_size = 16,
267 .tx_loadsz = 16,
268 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
269 .flags = UART_CAP_FIFO | UART_CAP_AFE,
270 },
Philippe Langlais235dae52010-07-29 17:13:57 +0200271 [PORT_U6_16550A] = {
272 .name = "U6_16550A",
273 .fifo_size = 64,
274 .tx_loadsz = 64,
275 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
276 .flags = UART_CAP_FIFO | UART_CAP_AFE,
277 },
Stephen Warren4539c242011-05-17 16:12:36 -0600278 [PORT_TEGRA] = {
279 .name = "Tegra",
280 .fifo_size = 32,
281 .tx_loadsz = 8,
282 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
283 UART_FCR_T_TRIG_01,
284 .flags = UART_CAP_FIFO | UART_CAP_RTOIE,
285 },
Søren Holm06315342011-09-02 22:55:37 +0200286 [PORT_XR17D15X] = {
287 .name = "XR17D15X",
288 .fifo_size = 64,
289 .tx_loadsz = 64,
290 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
291 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR,
292 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
Manuel Lauss12bf3f22010-07-15 21:45:05 +0200295#if defined(CONFIG_MIPS_ALCHEMY)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000296
297/* Au1x00 UART hardware has a weird register layout */
298static const u8 au_io_in_map[] = {
299 [UART_RX] = 0,
300 [UART_IER] = 2,
301 [UART_IIR] = 3,
302 [UART_LCR] = 5,
303 [UART_MCR] = 6,
304 [UART_LSR] = 7,
305 [UART_MSR] = 8,
306};
307
308static const u8 au_io_out_map[] = {
309 [UART_TX] = 1,
310 [UART_IER] = 2,
311 [UART_FCR] = 4,
312 [UART_LCR] = 5,
313 [UART_MCR] = 6,
314};
315
316/* sane hardware needs no mapping */
David Daney7d6a07d2009-01-02 13:49:47 +0000317static inline int map_8250_in_reg(struct uart_port *p, int offset)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000318{
David Daney7d6a07d2009-01-02 13:49:47 +0000319 if (p->iotype != UPIO_AU)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000320 return offset;
321 return au_io_in_map[offset];
322}
323
David Daney7d6a07d2009-01-02 13:49:47 +0000324static inline int map_8250_out_reg(struct uart_port *p, int offset)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000325{
David Daney7d6a07d2009-01-02 13:49:47 +0000326 if (p->iotype != UPIO_AU)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000327 return offset;
328 return au_io_out_map[offset];
329}
330
Alan Cox6f803cd2008-02-08 04:18:52 -0800331#elif defined(CONFIG_SERIAL_8250_RM9K)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700332
333static const u8
334 regmap_in[8] = {
335 [UART_RX] = 0x00,
336 [UART_IER] = 0x0c,
337 [UART_IIR] = 0x14,
338 [UART_LCR] = 0x1c,
339 [UART_MCR] = 0x20,
340 [UART_LSR] = 0x24,
341 [UART_MSR] = 0x28,
342 [UART_SCR] = 0x2c
343 },
344 regmap_out[8] = {
345 [UART_TX] = 0x04,
346 [UART_IER] = 0x0c,
347 [UART_FCR] = 0x18,
348 [UART_LCR] = 0x1c,
349 [UART_MCR] = 0x20,
350 [UART_LSR] = 0x24,
351 [UART_MSR] = 0x28,
352 [UART_SCR] = 0x2c
353 };
354
David Daney7d6a07d2009-01-02 13:49:47 +0000355static inline int map_8250_in_reg(struct uart_port *p, int offset)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700356{
David Daney7d6a07d2009-01-02 13:49:47 +0000357 if (p->iotype != UPIO_RM9000)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700358 return offset;
359 return regmap_in[offset];
360}
361
David Daney7d6a07d2009-01-02 13:49:47 +0000362static inline int map_8250_out_reg(struct uart_port *p, int offset)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700363{
David Daney7d6a07d2009-01-02 13:49:47 +0000364 if (p->iotype != UPIO_RM9000)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700365 return offset;
366 return regmap_out[offset];
367}
368
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000369#else
370
371/* sane hardware needs no mapping */
372#define map_8250_in_reg(up, offset) (offset)
373#define map_8250_out_reg(up, offset) (offset)
374
375#endif
376
David Daney7d6a07d2009-01-02 13:49:47 +0000377static unsigned int hub6_serial_in(struct uart_port *p, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
David Daney7d6a07d2009-01-02 13:49:47 +0000379 offset = map_8250_in_reg(p, offset) << p->regshift;
380 outb(p->hub6 - 1 + offset, p->iobase);
381 return inb(p->iobase + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
David Daney7d6a07d2009-01-02 13:49:47 +0000384static void hub6_serial_out(struct uart_port *p, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
David Daney7d6a07d2009-01-02 13:49:47 +0000386 offset = map_8250_out_reg(p, offset) << p->regshift;
387 outb(p->hub6 - 1 + offset, p->iobase);
388 outb(value, p->iobase + 1);
389}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
David Daney7d6a07d2009-01-02 13:49:47 +0000391static unsigned int mem_serial_in(struct uart_port *p, int offset)
392{
393 offset = map_8250_in_reg(p, offset) << p->regshift;
394 return readb(p->membase + offset);
395}
396
397static void mem_serial_out(struct uart_port *p, int offset, int value)
398{
399 offset = map_8250_out_reg(p, offset) << p->regshift;
400 writeb(value, p->membase + offset);
401}
402
403static void mem32_serial_out(struct uart_port *p, int offset, int value)
404{
405 offset = map_8250_out_reg(p, offset) << p->regshift;
406 writel(value, p->membase + offset);
407}
408
409static unsigned int mem32_serial_in(struct uart_port *p, int offset)
410{
411 offset = map_8250_in_reg(p, offset) << p->regshift;
412 return readl(p->membase + offset);
413}
414
David Daney7d6a07d2009-01-02 13:49:47 +0000415static unsigned int au_serial_in(struct uart_port *p, int offset)
416{
417 offset = map_8250_in_reg(p, offset) << p->regshift;
418 return __raw_readl(p->membase + offset);
419}
420
421static void au_serial_out(struct uart_port *p, int offset, int value)
422{
423 offset = map_8250_out_reg(p, offset) << p->regshift;
424 __raw_writel(value, p->membase + offset);
425}
David Daney7d6a07d2009-01-02 13:49:47 +0000426
David Daney7d6a07d2009-01-02 13:49:47 +0000427static unsigned int io_serial_in(struct uart_port *p, int offset)
428{
429 offset = map_8250_in_reg(p, offset) << p->regshift;
430 return inb(p->iobase + offset);
431}
432
433static void io_serial_out(struct uart_port *p, int offset, int value)
434{
435 offset = map_8250_out_reg(p, offset) << p->regshift;
436 outb(value, p->iobase + offset);
437}
438
Jamie Iles583d28e2011-08-15 10:17:52 +0100439static int serial8250_default_handle_irq(struct uart_port *port);
440
David Daney7d6a07d2009-01-02 13:49:47 +0000441static void set_io_from_upio(struct uart_port *p)
442{
Jamie Iles49d57412010-12-01 23:39:35 +0000443 struct uart_8250_port *up =
444 container_of(p, struct uart_8250_port, port);
David Daney7d6a07d2009-01-02 13:49:47 +0000445 switch (p->iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 case UPIO_HUB6:
David Daney7d6a07d2009-01-02 13:49:47 +0000447 p->serial_in = hub6_serial_in;
448 p->serial_out = hub6_serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 break;
450
451 case UPIO_MEM:
David Daney7d6a07d2009-01-02 13:49:47 +0000452 p->serial_in = mem_serial_in;
453 p->serial_out = mem_serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
455
Thomas Koellerbd71c182007-05-06 14:48:47 -0700456 case UPIO_RM9000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 case UPIO_MEM32:
David Daney7d6a07d2009-01-02 13:49:47 +0000458 p->serial_in = mem32_serial_in;
459 p->serial_out = mem32_serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
461
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000462 case UPIO_AU:
David Daney7d6a07d2009-01-02 13:49:47 +0000463 p->serial_in = au_serial_in;
464 p->serial_out = au_serial_out;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000465 break;
Manuel Lauss12bf3f22010-07-15 21:45:05 +0200466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 default:
David Daney7d6a07d2009-01-02 13:49:47 +0000468 p->serial_in = io_serial_in;
469 p->serial_out = io_serial_out;
470 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Alan Coxb8e7e402009-05-28 14:01:35 +0100472 /* Remember loaded iotype */
473 up->cur_iotype = p->iotype;
Jamie Iles583d28e2011-08-15 10:17:52 +0100474 p->handle_irq = serial8250_default_handle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Alex Williamson40b36da2007-02-14 00:33:04 -0800477static void
478serial_out_sync(struct uart_8250_port *up, int offset, int value)
479{
David Daney7d6a07d2009-01-02 13:49:47 +0000480 struct uart_port *p = &up->port;
481 switch (p->iotype) {
Alex Williamson40b36da2007-02-14 00:33:04 -0800482 case UPIO_MEM:
483 case UPIO_MEM32:
Alex Williamson40b36da2007-02-14 00:33:04 -0800484 case UPIO_AU:
David Daney7d6a07d2009-01-02 13:49:47 +0000485 p->serial_out(p, offset, value);
486 p->serial_in(p, UART_LCR); /* safe, no side-effects */
Alex Williamson40b36da2007-02-14 00:33:04 -0800487 break;
488 default:
David Daney7d6a07d2009-01-02 13:49:47 +0000489 p->serial_out(p, offset, value);
Alex Williamson40b36da2007-02-14 00:33:04 -0800490 }
491}
492
David Daney7d6a07d2009-01-02 13:49:47 +0000493#define serial_in(up, offset) \
494 (up->port.serial_in(&(up)->port, (offset)))
495#define serial_out(up, offset, value) \
496 (up->port.serial_out(&(up)->port, (offset), (value)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497/*
498 * We used to support using pause I/O for certain machines. We
499 * haven't supported this for a while, but just in case it's badly
500 * needed for certain old 386 machines, I've left these #define's
501 * in....
502 */
503#define serial_inp(up, offset) serial_in(up, offset)
504#define serial_outp(up, offset, value) serial_out(up, offset, value)
505
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100506/* Uart divisor latch read */
507static inline int _serial_dl_read(struct uart_8250_port *up)
508{
509 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
510}
511
512/* Uart divisor latch write */
513static inline void _serial_dl_write(struct uart_8250_port *up, int value)
514{
515 serial_outp(up, UART_DLL, value & 0xff);
516 serial_outp(up, UART_DLM, value >> 8 & 0xff);
517}
518
Manuel Lauss12bf3f22010-07-15 21:45:05 +0200519#if defined(CONFIG_MIPS_ALCHEMY)
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100520/* Au1x00 haven't got a standard divisor latch */
521static int serial_dl_read(struct uart_8250_port *up)
522{
523 if (up->port.iotype == UPIO_AU)
524 return __raw_readl(up->port.membase + 0x28);
525 else
526 return _serial_dl_read(up);
527}
528
529static void serial_dl_write(struct uart_8250_port *up, int value)
530{
531 if (up->port.iotype == UPIO_AU)
532 __raw_writel(value, up->port.membase + 0x28);
533 else
534 _serial_dl_write(up, value);
535}
Alan Cox6f803cd2008-02-08 04:18:52 -0800536#elif defined(CONFIG_SERIAL_8250_RM9K)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700537static int serial_dl_read(struct uart_8250_port *up)
538{
539 return (up->port.iotype == UPIO_RM9000) ?
540 (((__raw_readl(up->port.membase + 0x10) << 8) |
541 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
542 _serial_dl_read(up);
543}
544
545static void serial_dl_write(struct uart_8250_port *up, int value)
546{
547 if (up->port.iotype == UPIO_RM9000) {
548 __raw_writel(value, up->port.membase + 0x08);
549 __raw_writel(value >> 8, up->port.membase + 0x10);
550 } else {
551 _serial_dl_write(up, value);
552 }
553}
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100554#else
555#define serial_dl_read(up) _serial_dl_read(up)
556#define serial_dl_write(up, value) _serial_dl_write(up, value)
557#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559/*
560 * For the 16C950
561 */
562static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
563{
564 serial_out(up, UART_SCR, offset);
565 serial_out(up, UART_ICR, value);
566}
567
568static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
569{
570 unsigned int value;
571
572 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
573 serial_out(up, UART_SCR, offset);
574 value = serial_in(up, UART_ICR);
575 serial_icr_write(up, UART_ACR, up->acr);
576
577 return value;
578}
579
580/*
581 * FIFO support.
582 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100583static void serial8250_clear_fifos(struct uart_8250_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 if (p->capabilities & UART_CAP_FIFO) {
586 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
587 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
588 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
589 serial_outp(p, UART_FCR, 0);
590 }
591}
592
593/*
594 * IER sleep support. UARTs which have EFRs need the "extended
595 * capability" bit enabled. Note that on XR16C850s, we need to
596 * reset LCR to write to IER.
597 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100598static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 if (p->capabilities & UART_CAP_SLEEP) {
601 if (p->capabilities & UART_CAP_EFR) {
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800602 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 serial_outp(p, UART_EFR, UART_EFR_ECB);
604 serial_outp(p, UART_LCR, 0);
605 }
606 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
607 if (p->capabilities & UART_CAP_EFR) {
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800608 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 serial_outp(p, UART_EFR, 0);
610 serial_outp(p, UART_LCR, 0);
611 }
612 }
613}
614
615#ifdef CONFIG_SERIAL_8250_RSA
616/*
617 * Attempts to turn on the RSA FIFO. Returns zero on failure.
618 * We set the port uart clock rate if we succeed.
619 */
620static int __enable_rsa(struct uart_8250_port *up)
621{
622 unsigned char mode;
623 int result;
624
625 mode = serial_inp(up, UART_RSA_MSR);
626 result = mode & UART_RSA_MSR_FIFO;
627
628 if (!result) {
629 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
630 mode = serial_inp(up, UART_RSA_MSR);
631 result = mode & UART_RSA_MSR_FIFO;
632 }
633
634 if (result)
635 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
636
637 return result;
638}
639
640static void enable_rsa(struct uart_8250_port *up)
641{
642 if (up->port.type == PORT_RSA) {
643 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
644 spin_lock_irq(&up->port.lock);
645 __enable_rsa(up);
646 spin_unlock_irq(&up->port.lock);
647 }
648 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
649 serial_outp(up, UART_RSA_FRR, 0);
650 }
651}
652
653/*
654 * Attempts to turn off the RSA FIFO. Returns zero on failure.
655 * It is unknown why interrupts were disabled in here. However,
656 * the caller is expected to preserve this behaviour by grabbing
657 * the spinlock before calling this function.
658 */
659static void disable_rsa(struct uart_8250_port *up)
660{
661 unsigned char mode;
662 int result;
663
664 if (up->port.type == PORT_RSA &&
665 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
666 spin_lock_irq(&up->port.lock);
667
668 mode = serial_inp(up, UART_RSA_MSR);
669 result = !(mode & UART_RSA_MSR_FIFO);
670
671 if (!result) {
672 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
673 mode = serial_inp(up, UART_RSA_MSR);
674 result = !(mode & UART_RSA_MSR_FIFO);
675 }
676
677 if (result)
678 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
679 spin_unlock_irq(&up->port.lock);
680 }
681}
682#endif /* CONFIG_SERIAL_8250_RSA */
683
684/*
685 * This is a quickie test to see how big the FIFO is.
686 * It doesn't work at all the time, more's the pity.
687 */
688static int size_fifo(struct uart_8250_port *up)
689{
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100690 unsigned char old_fcr, old_mcr, old_lcr;
691 unsigned short old_dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 int count;
693
694 old_lcr = serial_inp(up, UART_LCR);
695 serial_outp(up, UART_LCR, 0);
696 old_fcr = serial_inp(up, UART_FCR);
697 old_mcr = serial_inp(up, UART_MCR);
698 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
699 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
700 serial_outp(up, UART_MCR, UART_MCR_LOOP);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800701 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100702 old_dl = serial_dl_read(up);
703 serial_dl_write(up, 0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 serial_outp(up, UART_LCR, 0x03);
705 for (count = 0; count < 256; count++)
706 serial_outp(up, UART_TX, count);
707 mdelay(20);/* FIXME - schedule_timeout */
708 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
709 (count < 256); count++)
710 serial_inp(up, UART_RX);
711 serial_outp(up, UART_FCR, old_fcr);
712 serial_outp(up, UART_MCR, old_mcr);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800713 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100714 serial_dl_write(up, old_dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 serial_outp(up, UART_LCR, old_lcr);
716
717 return count;
718}
719
720/*
721 * Read UART ID using the divisor method - set DLL and DLM to zero
722 * and the revision will be in DLL and device type in DLM. We
723 * preserve the device state across this.
724 */
725static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
726{
727 unsigned char old_dll, old_dlm, old_lcr;
728 unsigned int id;
729
730 old_lcr = serial_inp(p, UART_LCR);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800731 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 old_dll = serial_inp(p, UART_DLL);
734 old_dlm = serial_inp(p, UART_DLM);
735
736 serial_outp(p, UART_DLL, 0);
737 serial_outp(p, UART_DLM, 0);
738
739 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
740
741 serial_outp(p, UART_DLL, old_dll);
742 serial_outp(p, UART_DLM, old_dlm);
743 serial_outp(p, UART_LCR, old_lcr);
744
745 return id;
746}
747
748/*
749 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
750 * When this function is called we know it is at least a StarTech
751 * 16650 V2, but it might be one of several StarTech UARTs, or one of
752 * its clones. (We treat the broken original StarTech 16650 V1 as a
753 * 16550, and why not? Startech doesn't seem to even acknowledge its
754 * existence.)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700755 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * What evil have men's minds wrought...
757 */
758static void autoconfig_has_efr(struct uart_8250_port *up)
759{
760 unsigned int id1, id2, id3, rev;
761
762 /*
763 * Everything with an EFR has SLEEP
764 */
765 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
766
767 /*
768 * First we check to see if it's an Oxford Semiconductor UART.
769 *
770 * If we have to do this here because some non-National
771 * Semiconductor clone chips lock up if you try writing to the
772 * LSR register (which serial_icr_read does)
773 */
774
775 /*
776 * Check for Oxford Semiconductor 16C950.
777 *
778 * EFR [4] must be set else this test fails.
779 *
780 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
781 * claims that it's needed for 952 dual UART's (which are not
782 * recommended for new designs).
783 */
784 up->acr = 0;
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800785 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 serial_out(up, UART_EFR, UART_EFR_ECB);
787 serial_out(up, UART_LCR, 0x00);
788 id1 = serial_icr_read(up, UART_ID1);
789 id2 = serial_icr_read(up, UART_ID2);
790 id3 = serial_icr_read(up, UART_ID3);
791 rev = serial_icr_read(up, UART_REV);
792
793 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
794
795 if (id1 == 0x16 && id2 == 0xC9 &&
796 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
797 up->port.type = PORT_16C950;
Russell King4ba5e352005-06-23 10:43:04 +0100798
799 /*
800 * Enable work around for the Oxford Semiconductor 952 rev B
801 * chip which causes it to seriously miscalculate baud rates
802 * when DLL is 0.
803 */
804 if (id3 == 0x52 && rev == 0x01)
805 up->bugs |= UART_BUG_QUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return;
807 }
Thomas Koellerbd71c182007-05-06 14:48:47 -0700808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 /*
810 * We check for a XR16C850 by setting DLL and DLM to 0, and then
811 * reading back DLL and DLM. The chip type depends on the DLM
812 * value read back:
813 * 0x10 - XR16C850 and the DLL contains the chip revision.
814 * 0x12 - XR16C2850.
815 * 0x14 - XR16C854.
816 */
817 id1 = autoconfig_read_divisor_id(up);
818 DEBUG_AUTOCONF("850id=%04x ", id1);
819
820 id2 = id1 >> 8;
821 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 up->port.type = PORT_16850;
823 return;
824 }
825
826 /*
827 * It wasn't an XR16C850.
828 *
829 * We distinguish between the '654 and the '650 by counting
830 * how many bytes are in the FIFO. I'm using this for now,
831 * since that's the technique that was sent to me in the
832 * serial driver update, but I'm not convinced this works.
833 * I've had problems doing this in the past. -TYT
834 */
835 if (size_fifo(up) == 64)
836 up->port.type = PORT_16654;
837 else
838 up->port.type = PORT_16650V2;
839}
840
841/*
842 * We detected a chip without a FIFO. Only two fall into
843 * this category - the original 8250 and the 16450. The
844 * 16450 has a scratch register (accessible with LCR=0)
845 */
846static void autoconfig_8250(struct uart_8250_port *up)
847{
848 unsigned char scratch, status1, status2;
849
850 up->port.type = PORT_8250;
851
852 scratch = serial_in(up, UART_SCR);
853 serial_outp(up, UART_SCR, 0xa5);
854 status1 = serial_in(up, UART_SCR);
855 serial_outp(up, UART_SCR, 0x5a);
856 status2 = serial_in(up, UART_SCR);
857 serial_outp(up, UART_SCR, scratch);
858
859 if (status1 == 0xa5 && status2 == 0x5a)
860 up->port.type = PORT_16450;
861}
862
863static int broken_efr(struct uart_8250_port *up)
864{
865 /*
866 * Exar ST16C2550 "A2" devices incorrectly detect as
867 * having an EFR, and report an ID of 0x0201. See
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200868 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
870 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
871 return 1;
872
873 return 0;
874}
875
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800876static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
877{
878 unsigned char status;
879
880 status = serial_in(up, 0x04); /* EXCR2 */
881#define PRESL(x) ((x) & 0x30)
882 if (PRESL(status) == 0x10) {
883 /* already in high speed mode */
884 return 0;
885 } else {
886 status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
887 status |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
888 serial_outp(up, 0x04, status);
889 }
890 return 1;
891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893/*
894 * We know that the chip has FIFOs. Does it have an EFR? The
895 * EFR is located in the same register position as the IIR and
896 * we know the top two bits of the IIR are currently set. The
897 * EFR should contain zero. Try to read the EFR.
898 */
899static void autoconfig_16550a(struct uart_8250_port *up)
900{
901 unsigned char status1, status2;
902 unsigned int iersave;
903
904 up->port.type = PORT_16550A;
905 up->capabilities |= UART_CAP_FIFO;
906
907 /*
908 * Check for presence of the EFR when DLAB is set.
909 * Only ST16C650V1 UARTs pass this test.
910 */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800911 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (serial_in(up, UART_EFR) == 0) {
913 serial_outp(up, UART_EFR, 0xA8);
914 if (serial_in(up, UART_EFR) != 0) {
915 DEBUG_AUTOCONF("EFRv1 ");
916 up->port.type = PORT_16650;
917 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
918 } else {
919 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
920 }
921 serial_outp(up, UART_EFR, 0);
922 return;
923 }
924
925 /*
926 * Maybe it requires 0xbf to be written to the LCR.
927 * (other ST16C650V2 UARTs, TI16C752A, etc)
928 */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800929 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
931 DEBUG_AUTOCONF("EFRv2 ");
932 autoconfig_has_efr(up);
933 return;
934 }
935
936 /*
937 * Check for a National Semiconductor SuperIO chip.
938 * Attempt to switch to bank 2, read the value of the LOOP bit
939 * from EXCR1. Switch back to bank 0, change it in MCR. Then
940 * switch back to bank 2, read it from EXCR1 again and check
941 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 */
943 serial_outp(up, UART_LCR, 0);
944 status1 = serial_in(up, UART_MCR);
945 serial_outp(up, UART_LCR, 0xE0);
946 status2 = serial_in(up, 0x02); /* EXCR1 */
947
948 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
949 serial_outp(up, UART_LCR, 0);
950 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
951 serial_outp(up, UART_LCR, 0xE0);
952 status2 = serial_in(up, 0x02); /* EXCR1 */
953 serial_outp(up, UART_LCR, 0);
954 serial_outp(up, UART_MCR, status1);
955
956 if ((status2 ^ status1) & UART_MCR_LOOP) {
David Woodhouse857dde22005-05-21 15:52:23 +0100957 unsigned short quot;
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 serial_outp(up, UART_LCR, 0xE0);
David Woodhouse857dde22005-05-21 15:52:23 +0100960
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100961 quot = serial_dl_read(up);
David Woodhouse857dde22005-05-21 15:52:23 +0100962 quot <<= 3;
963
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800964 if (ns16550a_goto_highspeed(up))
965 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
David Woodhouse857dde22005-05-21 15:52:23 +0100967 serial_outp(up, UART_LCR, 0);
968
969 up->port.uartclk = 921600*16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 up->port.type = PORT_NS16550A;
971 up->capabilities |= UART_NATSEMI;
972 return;
973 }
974 }
975
976 /*
977 * No EFR. Try to detect a TI16750, which only sets bit 5 of
978 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
979 * Try setting it with and without DLAB set. Cheap clones
980 * set bit 5 without DLAB set.
981 */
982 serial_outp(up, UART_LCR, 0);
983 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
984 status1 = serial_in(up, UART_IIR) >> 5;
985 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800986 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
988 status2 = serial_in(up, UART_IIR) >> 5;
989 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
990 serial_outp(up, UART_LCR, 0);
991
992 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
993
994 if (status1 == 6 && status2 == 7) {
995 up->port.type = PORT_16750;
996 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
997 return;
998 }
999
1000 /*
1001 * Try writing and reading the UART_IER_UUE bit (b6).
1002 * If it works, this is probably one of the Xscale platform's
1003 * internal UARTs.
1004 * We're going to explicitly set the UUE bit to 0 before
1005 * trying to write and read a 1 just to make sure it's not
1006 * already a 1 and maybe locked there before we even start start.
1007 */
1008 iersave = serial_in(up, UART_IER);
1009 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
1010 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1011 /*
1012 * OK it's in a known zero state, try writing and reading
1013 * without disturbing the current state of the other bits.
1014 */
1015 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
1016 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1017 /*
1018 * It's an Xscale.
1019 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1020 */
1021 DEBUG_AUTOCONF("Xscale ");
1022 up->port.type = PORT_XSCALE;
Stephen Warren55681812011-06-17 09:45:07 -06001023 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return;
1025 }
1026 } else {
1027 /*
1028 * If we got here we couldn't force the IER_UUE bit to 0.
1029 * Log it and continue.
1030 */
1031 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1032 }
1033 serial_outp(up, UART_IER, iersave);
Philippe Langlais235dae52010-07-29 17:13:57 +02001034
1035 /*
Søren Holm06315342011-09-02 22:55:37 +02001036 * Exar uarts have EFR in a weird location
1037 */
1038 if (up->port.flags & UPF_EXAR_EFR) {
1039 up->port.type = PORT_XR17D15X;
1040 up->capabilities |= UART_CAP_AFE | UART_CAP_EFR;
1041 }
1042
1043 /*
Philippe Langlais235dae52010-07-29 17:13:57 +02001044 * We distinguish between 16550A and U6 16550A by counting
1045 * how many bytes are in the FIFO.
1046 */
1047 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1048 up->port.type = PORT_U6_16550A;
1049 up->capabilities |= UART_CAP_AFE;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053/*
1054 * This routine is called by rs_init() to initialize a specific serial
1055 * port. It determines what type of UART chip this serial port is
1056 * using: 8250, 16450, 16550, 16550A. The important question is
1057 * whether or not this UART is a 16550A or not, since this will
1058 * determine whether or not we can use its FIFO features or not.
1059 */
1060static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
1061{
1062 unsigned char status1, scratch, scratch2, scratch3;
1063 unsigned char save_lcr, save_mcr;
1064 unsigned long flags;
1065
1066 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
1067 return;
1068
Lennert Buytenhek80647b92009-11-11 14:26:41 -08001069 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
David S. Miller84408382008-10-13 10:45:26 +01001070 serial_index(&up->port), up->port.iobase, up->port.membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /*
1073 * We really do need global IRQs disabled here - we're going to
1074 * be frobbing the chips IRQ enable register to see if it exists.
1075 */
1076 spin_lock_irqsave(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 up->capabilities = 0;
Russell King4ba5e352005-06-23 10:43:04 +01001079 up->bugs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 if (!(up->port.flags & UPF_BUGGY_UART)) {
1082 /*
1083 * Do a simple existence test first; if we fail this,
1084 * there's no point trying anything else.
Thomas Koellerbd71c182007-05-06 14:48:47 -07001085 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 * 0x80 is used as a nonsense port to prevent against
1087 * false positives due to ISA bus float. The
1088 * assumption is that 0x80 is a non-existent port;
1089 * which should be safe since include/asm/io.h also
1090 * makes this assumption.
1091 *
1092 * Note: this is safe as long as MCR bit 4 is clear
1093 * and the device is in "PC" mode.
1094 */
1095 scratch = serial_inp(up, UART_IER);
1096 serial_outp(up, UART_IER, 0);
1097#ifdef __i386__
1098 outb(0xff, 0x080);
1099#endif
Thomas Hoehn48212002007-02-10 01:46:05 -08001100 /*
1101 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1102 * 16C754B) allow only to modify them if an EFR bit is set.
1103 */
1104 scratch2 = serial_inp(up, UART_IER) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 serial_outp(up, UART_IER, 0x0F);
1106#ifdef __i386__
1107 outb(0, 0x080);
1108#endif
Thomas Hoehn48212002007-02-10 01:46:05 -08001109 scratch3 = serial_inp(up, UART_IER) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 serial_outp(up, UART_IER, scratch);
1111 if (scratch2 != 0 || scratch3 != 0x0F) {
1112 /*
1113 * We failed; there's nothing here
1114 */
1115 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1116 scratch2, scratch3);
1117 goto out;
1118 }
1119 }
1120
1121 save_mcr = serial_in(up, UART_MCR);
1122 save_lcr = serial_in(up, UART_LCR);
1123
Thomas Koellerbd71c182007-05-06 14:48:47 -07001124 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 * Check to see if a UART is really there. Certain broken
1126 * internal modems based on the Rockwell chipset fail this
1127 * test, because they apparently don't implement the loopback
1128 * test mode. So this test is skipped on the COM 1 through
1129 * COM 4 ports. This *should* be safe, since no board
1130 * manufacturer would be stupid enough to design a board
1131 * that conflicts with COM 1-4 --- we hope!
1132 */
1133 if (!(up->port.flags & UPF_SKIP_TEST)) {
1134 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1135 status1 = serial_inp(up, UART_MSR) & 0xF0;
1136 serial_outp(up, UART_MCR, save_mcr);
1137 if (status1 != 0x90) {
1138 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1139 status1);
1140 goto out;
1141 }
1142 }
1143
1144 /*
1145 * We're pretty sure there's a port here. Lets find out what
1146 * type of port it is. The IIR top two bits allows us to find
Russell King6f0d6182005-09-09 16:17:58 +01001147 * out if it's 8250 or 16450, 16550, 16550A or later. This
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 * determines what we test for next.
1149 *
1150 * We also initialise the EFR (if any) to zero for later. The
1151 * EFR occupies the same register location as the FCR and IIR.
1152 */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001153 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 serial_outp(up, UART_EFR, 0);
1155 serial_outp(up, UART_LCR, 0);
1156
1157 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1158 scratch = serial_in(up, UART_IIR) >> 6;
1159
1160 DEBUG_AUTOCONF("iir=%d ", scratch);
1161
1162 switch (scratch) {
1163 case 0:
1164 autoconfig_8250(up);
1165 break;
1166 case 1:
1167 up->port.type = PORT_UNKNOWN;
1168 break;
1169 case 2:
1170 up->port.type = PORT_16550;
1171 break;
1172 case 3:
1173 autoconfig_16550a(up);
1174 break;
1175 }
1176
1177#ifdef CONFIG_SERIAL_8250_RSA
1178 /*
1179 * Only probe for RSA ports if we got the region.
1180 */
1181 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1182 int i;
1183
1184 for (i = 0 ; i < probe_rsa_count; ++i) {
1185 if (probe_rsa[i] == up->port.iobase &&
1186 __enable_rsa(up)) {
1187 up->port.type = PORT_RSA;
1188 break;
1189 }
1190 }
1191 }
1192#endif
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 serial_outp(up, UART_LCR, save_lcr);
1195
1196 if (up->capabilities != uart_config[up->port.type].flags) {
1197 printk(KERN_WARNING
1198 "ttyS%d: detected caps %08x should be %08x\n",
David S. Miller84408382008-10-13 10:45:26 +01001199 serial_index(&up->port), up->capabilities,
1200 uart_config[up->port.type].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202
1203 up->port.fifosize = uart_config[up->port.type].fifo_size;
1204 up->capabilities = uart_config[up->port.type].flags;
1205 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1206
1207 if (up->port.type == PORT_UNKNOWN)
1208 goto out;
1209
1210 /*
1211 * Reset the UART.
1212 */
1213#ifdef CONFIG_SERIAL_8250_RSA
1214 if (up->port.type == PORT_RSA)
1215 serial_outp(up, UART_RSA_FRR, 0);
1216#endif
1217 serial_outp(up, UART_MCR, save_mcr);
1218 serial8250_clear_fifos(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001219 serial_in(up, UART_RX);
Lennert Buytenhek5c8c7552005-11-12 21:58:05 +00001220 if (up->capabilities & UART_CAP_UUE)
1221 serial_outp(up, UART_IER, UART_IER_UUE);
1222 else
1223 serial_outp(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Thomas Koellerbd71c182007-05-06 14:48:47 -07001225 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 spin_unlock_irqrestore(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1228}
1229
1230static void autoconfig_irq(struct uart_8250_port *up)
1231{
1232 unsigned char save_mcr, save_ier;
1233 unsigned char save_ICP = 0;
1234 unsigned int ICP = 0;
1235 unsigned long irqs;
1236 int irq;
1237
1238 if (up->port.flags & UPF_FOURPORT) {
1239 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1240 save_ICP = inb_p(ICP);
1241 outb_p(0x80, ICP);
1242 (void) inb_p(ICP);
1243 }
1244
1245 /* forget possible initially masked and pending IRQ */
1246 probe_irq_off(probe_irq_on());
1247 save_mcr = serial_inp(up, UART_MCR);
1248 save_ier = serial_inp(up, UART_IER);
1249 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
Thomas Koellerbd71c182007-05-06 14:48:47 -07001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 irqs = probe_irq_on();
1252 serial_outp(up, UART_MCR, 0);
Alan Cox6f803cd2008-02-08 04:18:52 -08001253 udelay(10);
1254 if (up->port.flags & UPF_FOURPORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 serial_outp(up, UART_MCR,
1256 UART_MCR_DTR | UART_MCR_RTS);
1257 } else {
1258 serial_outp(up, UART_MCR,
1259 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1260 }
1261 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1262 (void)serial_inp(up, UART_LSR);
1263 (void)serial_inp(up, UART_RX);
1264 (void)serial_inp(up, UART_IIR);
1265 (void)serial_inp(up, UART_MSR);
1266 serial_outp(up, UART_TX, 0xFF);
Alan Cox6f803cd2008-02-08 04:18:52 -08001267 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 irq = probe_irq_off(irqs);
1269
1270 serial_outp(up, UART_MCR, save_mcr);
1271 serial_outp(up, UART_IER, save_ier);
1272
1273 if (up->port.flags & UPF_FOURPORT)
1274 outb_p(save_ICP, ICP);
1275
1276 up->port.irq = (irq > 0) ? irq : 0;
1277}
1278
Russell Kinge763b902005-06-29 18:41:51 +01001279static inline void __stop_tx(struct uart_8250_port *p)
1280{
1281 if (p->ier & UART_IER_THRI) {
1282 p->ier &= ~UART_IER_THRI;
1283 serial_out(p, UART_IER, p->ier);
1284 }
1285}
1286
Russell Kingb129a8c2005-08-31 10:12:14 +01001287static void serial8250_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Jamie Iles49d57412010-12-01 23:39:35 +00001289 struct uart_8250_port *up =
1290 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Russell Kinge763b902005-06-29 18:41:51 +01001292 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 /*
Russell Kinge763b902005-06-29 18:41:51 +01001295 * We really want to stop the transmitter from sending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
Russell Kinge763b902005-06-29 18:41:51 +01001297 if (up->port.type == PORT_16C950) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 up->acr |= UART_ACR_TXDIS;
1299 serial_icr_write(up, UART_ACR, up->acr);
1300 }
1301}
1302
Russell King55d3b282005-06-23 15:05:41 +01001303static void transmit_chars(struct uart_8250_port *up);
1304
Russell Kingb129a8c2005-08-31 10:12:14 +01001305static void serial8250_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Jamie Iles49d57412010-12-01 23:39:35 +00001307 struct uart_8250_port *up =
1308 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 if (!(up->ier & UART_IER_THRI)) {
1311 up->ier |= UART_IER_THRI;
1312 serial_out(up, UART_IER, up->ier);
Russell King55d3b282005-06-23 15:05:41 +01001313
Russell King67f76542005-06-23 22:26:43 +01001314 if (up->bugs & UART_BUG_TXEN) {
Ian Jackson68cb4f82009-11-18 11:08:11 +01001315 unsigned char lsr;
Russell King55d3b282005-06-23 15:05:41 +01001316 lsr = serial_in(up, UART_LSR);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001317 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Thomas Koellerbd71c182007-05-06 14:48:47 -07001318 if ((up->port.type == PORT_RM9000) ?
Ian Jackson68cb4f82009-11-18 11:08:11 +01001319 (lsr & UART_LSR_THRE) :
1320 (lsr & UART_LSR_TEMT))
Russell King55d3b282005-06-23 15:05:41 +01001321 transmit_chars(up);
1322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
Russell Kinge763b902005-06-29 18:41:51 +01001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 /*
Russell Kinge763b902005-06-29 18:41:51 +01001326 * Re-enable the transmitter if we disabled it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 */
Russell Kinge763b902005-06-29 18:41:51 +01001328 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 up->acr &= ~UART_ACR_TXDIS;
1330 serial_icr_write(up, UART_ACR, up->acr);
1331 }
1332}
1333
1334static void serial8250_stop_rx(struct uart_port *port)
1335{
Jamie Iles49d57412010-12-01 23:39:35 +00001336 struct uart_8250_port *up =
1337 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 up->ier &= ~UART_IER_RLSI;
1340 up->port.read_status_mask &= ~UART_LSR_DR;
1341 serial_out(up, UART_IER, up->ier);
1342}
1343
1344static void serial8250_enable_ms(struct uart_port *port)
1345{
Jamie Iles49d57412010-12-01 23:39:35 +00001346 struct uart_8250_port *up =
1347 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001349 /* no MSR capabilities */
1350 if (up->bugs & UART_BUG_NOMSR)
1351 return;
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 up->ier |= UART_IER_MSI;
1354 serial_out(up, UART_IER, up->ier);
1355}
1356
Stephen Warren5f873ba2011-05-17 16:12:37 -06001357/*
1358 * Clear the Tegra rx fifo after a break
1359 *
1360 * FIXME: This needs to become a port specific callback once we have a
1361 * framework for this
1362 */
1363static void clear_rx_fifo(struct uart_8250_port *up)
1364{
1365 unsigned int status, tmout = 10000;
1366 do {
1367 status = serial_in(up, UART_LSR);
1368 if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
1369 status = serial_in(up, UART_RX);
1370 else
1371 break;
1372 if (--tmout == 0)
1373 break;
1374 udelay(1);
1375 } while (1);
1376}
1377
Paul Gortmaker0690f412011-12-04 18:42:19 -05001378/*
1379 * receive_chars: processes according to the passed in LSR
1380 * value, and returns the remaining LSR bits not handled
1381 * by this Rx routine.
1382 */
1383static unsigned char
1384receive_chars(struct uart_8250_port *up, unsigned char lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001386 struct tty_struct *tty = up->port.state->port.tty;
Paul Gortmaker0690f412011-12-04 18:42:19 -05001387 unsigned char ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 int max_count = 256;
1389 char flag;
1390
1391 do {
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001392 if (likely(lsr & UART_LSR_DR))
1393 ch = serial_inp(up, UART_RX);
1394 else
1395 /*
1396 * Intel 82571 has a Serial Over Lan device that will
1397 * set UART_LSR_BI without setting UART_LSR_DR when
1398 * it receives a break. To avoid reading from the
1399 * receive buffer without UART_LSR_DR bit set, we
1400 * just force the read character to be 0
1401 */
1402 ch = 0;
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 flag = TTY_NORMAL;
1405 up->port.icount.rx++;
1406
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001407 lsr |= up->lsr_saved_flags;
1408 up->lsr_saved_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001410 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 /*
1412 * For statistics only
1413 */
1414 if (lsr & UART_LSR_BI) {
1415 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1416 up->port.icount.brk++;
1417 /*
Stephen Warren5f873ba2011-05-17 16:12:37 -06001418 * If tegra port then clear the rx fifo to
1419 * accept another break/character.
1420 */
1421 if (up->port.type == PORT_TEGRA)
1422 clear_rx_fifo(up);
1423
1424 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 * We do the SysRQ and SAK checking
1426 * here because otherwise the break
1427 * may get masked by ignore_status_mask
1428 * or read_status_mask.
1429 */
1430 if (uart_handle_break(&up->port))
1431 goto ignore_char;
1432 } else if (lsr & UART_LSR_PE)
1433 up->port.icount.parity++;
1434 else if (lsr & UART_LSR_FE)
1435 up->port.icount.frame++;
1436 if (lsr & UART_LSR_OE)
1437 up->port.icount.overrun++;
1438
1439 /*
Russell King23907eb2005-04-16 15:26:39 -07001440 * Mask off conditions which should be ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 */
1442 lsr &= up->port.read_status_mask;
1443
1444 if (lsr & UART_LSR_BI) {
1445 DEBUG_INTR("handling break....");
1446 flag = TTY_BREAK;
1447 } else if (lsr & UART_LSR_PE)
1448 flag = TTY_PARITY;
1449 else if (lsr & UART_LSR_FE)
1450 flag = TTY_FRAME;
1451 }
David Howells7d12e782006-10-05 14:55:46 +01001452 if (uart_handle_sysrq_char(&up->port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 goto ignore_char;
Russell King05ab3012005-05-09 23:21:59 +01001454
1455 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1456
Alan Cox6f803cd2008-02-08 04:18:52 -08001457ignore_char:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 lsr = serial_inp(up, UART_LSR);
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001459 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 spin_unlock(&up->port.lock);
1461 tty_flip_buffer_push(tty);
1462 spin_lock(&up->port.lock);
Paul Gortmaker0690f412011-12-04 18:42:19 -05001463 return lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Russell Kingea8874d2006-01-04 19:43:24 +00001466static void transmit_chars(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001468 struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 int count;
1470
1471 if (up->port.x_char) {
1472 serial_outp(up, UART_TX, up->port.x_char);
1473 up->port.icount.tx++;
1474 up->port.x_char = 0;
1475 return;
1476 }
Russell Kingb129a8c2005-08-31 10:12:14 +01001477 if (uart_tx_stopped(&up->port)) {
1478 serial8250_stop_tx(&up->port);
1479 return;
1480 }
1481 if (uart_circ_empty(xmit)) {
Russell Kinge763b902005-06-29 18:41:51 +01001482 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return;
1484 }
1485
1486 count = up->tx_loadsz;
1487 do {
1488 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1489 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1490 up->port.icount.tx++;
1491 if (uart_circ_empty(xmit))
1492 break;
1493 } while (--count > 0);
1494
1495 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1496 uart_write_wakeup(&up->port);
1497
1498 DEBUG_INTR("THRE...");
1499
1500 if (uart_circ_empty(xmit))
Russell Kinge763b902005-06-29 18:41:51 +01001501 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Russell King2af7cd62006-01-04 16:55:09 +00001504static unsigned int check_modem_status(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Russell King2af7cd62006-01-04 16:55:09 +00001506 unsigned int status = serial_in(up, UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001508 status |= up->msr_saved_flags;
1509 up->msr_saved_flags = 0;
Taku Izumifdc30b32007-04-23 14:41:00 -07001510 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
Alan Coxebd2c8f2009-09-19 13:13:28 -07001511 up->port.state != NULL) {
Russell King2af7cd62006-01-04 16:55:09 +00001512 if (status & UART_MSR_TERI)
1513 up->port.icount.rng++;
1514 if (status & UART_MSR_DDSR)
1515 up->port.icount.dsr++;
1516 if (status & UART_MSR_DDCD)
1517 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1518 if (status & UART_MSR_DCTS)
1519 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Alan Coxbdc04e32009-09-19 13:13:31 -07001521 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
Russell King2af7cd62006-01-04 16:55:09 +00001522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Russell King2af7cd62006-01-04 16:55:09 +00001524 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
1527/*
1528 * This handles the interrupt from one port.
1529 */
Will Newtonb5d674a2008-10-13 10:36:21 +01001530static void serial8250_handle_port(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Paul Gortmaker0690f412011-12-04 18:42:19 -05001532 unsigned char status;
Jiri Kosina4bf36312007-04-23 14:41:21 -07001533 unsigned long flags;
Russell King45e24602006-01-04 19:19:06 +00001534
Jiri Kosina4bf36312007-04-23 14:41:21 -07001535 spin_lock_irqsave(&up->port.lock, flags);
Russell King45e24602006-01-04 19:19:06 +00001536
1537 status = serial_inp(up, UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 DEBUG_INTR("status = %x...", status);
1540
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001541 if (status & (UART_LSR_DR | UART_LSR_BI))
Paul Gortmaker0690f412011-12-04 18:42:19 -05001542 status = receive_chars(up, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 check_modem_status(up);
1544 if (status & UART_LSR_THRE)
1545 transmit_chars(up);
Russell King45e24602006-01-04 19:19:06 +00001546
Jiri Kosina4bf36312007-04-23 14:41:21 -07001547 spin_unlock_irqrestore(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Jamie Iles583d28e2011-08-15 10:17:52 +01001550int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
1551{
1552 struct uart_8250_port *up =
1553 container_of(port, struct uart_8250_port, port);
1554
1555 if (!(iir & UART_IIR_NO_INT)) {
1556 serial8250_handle_port(up);
1557 return 1;
1558 }
1559
1560 return 0;
1561}
Jamie Ilesc7a1bdc2011-08-26 19:04:49 +01001562EXPORT_SYMBOL_GPL(serial8250_handle_irq);
Jamie Iles583d28e2011-08-15 10:17:52 +01001563
1564static int serial8250_default_handle_irq(struct uart_port *port)
1565{
1566 struct uart_8250_port *up =
1567 container_of(port, struct uart_8250_port, port);
1568 unsigned int iir = serial_in(up, UART_IIR);
1569
1570 return serial8250_handle_irq(port, iir);
1571}
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573/*
1574 * This is the serial driver's interrupt routine.
1575 *
1576 * Arjan thinks the old way was overly complex, so it got simplified.
1577 * Alan disagrees, saying that need the complexity to handle the weird
1578 * nature of ISA shared interrupts. (This is a special exception.)
1579 *
1580 * In order to handle ISA shared interrupts properly, we need to check
1581 * that all ports have been serviced, and therefore the ISA interrupt
1582 * line has been de-asserted.
1583 *
1584 * This means we need to loop through all ports. checking that they
1585 * don't have an interrupt pending.
1586 */
David Howells7d12e782006-10-05 14:55:46 +01001587static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
1589 struct irq_info *i = dev_id;
1590 struct list_head *l, *end = NULL;
1591 int pass_counter = 0, handled = 0;
1592
1593 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1594
1595 spin_lock(&i->lock);
1596
1597 l = i->head;
1598 do {
1599 struct uart_8250_port *up;
Jamie Iles583d28e2011-08-15 10:17:52 +01001600 struct uart_port *port;
Dan Williams448ac152011-11-22 13:41:24 -08001601 bool skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 up = list_entry(l, struct uart_8250_port, list);
Jamie Iles583d28e2011-08-15 10:17:52 +01001604 port = &up->port;
Dan Williams448ac152011-11-22 13:41:24 -08001605 skip = pass_counter && up->port.flags & UPF_IIR_ONCE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Dan Williams448ac152011-11-22 13:41:24 -08001607 if (!skip && port->handle_irq(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 handled = 1;
Marc St-Jeanbeab6972007-05-06 14:48:45 -07001609 end = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 } else if (end == NULL)
1611 end = l;
1612
1613 l = l->next;
1614
1615 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1616 /* If we hit this, we're dead. */
Daniel Drakecd3ecad2010-10-20 16:00:48 -07001617 printk_ratelimited(KERN_ERR
1618 "serial8250: too much work for irq%d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 break;
1620 }
1621 } while (l != end);
1622
1623 spin_unlock(&i->lock);
1624
1625 DEBUG_INTR("end.\n");
1626
1627 return IRQ_RETVAL(handled);
1628}
1629
1630/*
1631 * To support ISA shared interrupts, we need to have one interrupt
1632 * handler that ensures that the IRQ line has been deasserted
1633 * before returning. Failing to do this will result in the IRQ
1634 * line being stuck active, and, since ISA irqs are edge triggered,
1635 * no more IRQs will be seen.
1636 */
1637static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1638{
1639 spin_lock_irq(&i->lock);
1640
1641 if (!list_empty(i->head)) {
1642 if (i->head == &up->list)
1643 i->head = i->head->next;
1644 list_del(&up->list);
1645 } else {
1646 BUG_ON(i->head != &up->list);
1647 i->head = NULL;
1648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 spin_unlock_irq(&i->lock);
Alan Cox25db8ad2008-08-19 20:49:40 -07001650 /* List empty so throw away the hash node */
1651 if (i->head == NULL) {
1652 hlist_del(&i->node);
1653 kfree(i);
1654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
1657static int serial_link_irq_chain(struct uart_8250_port *up)
1658{
Alan Cox25db8ad2008-08-19 20:49:40 -07001659 struct hlist_head *h;
1660 struct hlist_node *n;
1661 struct irq_info *i;
Thomas Gleixner40663cc2006-07-01 19:29:43 -07001662 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Alan Cox25db8ad2008-08-19 20:49:40 -07001664 mutex_lock(&hash_mutex);
1665
1666 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1667
1668 hlist_for_each(n, h) {
1669 i = hlist_entry(n, struct irq_info, node);
1670 if (i->irq == up->port.irq)
1671 break;
1672 }
1673
1674 if (n == NULL) {
1675 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
1676 if (i == NULL) {
1677 mutex_unlock(&hash_mutex);
1678 return -ENOMEM;
1679 }
1680 spin_lock_init(&i->lock);
1681 i->irq = up->port.irq;
1682 hlist_add_head(&i->node, h);
1683 }
1684 mutex_unlock(&hash_mutex);
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 spin_lock_irq(&i->lock);
1687
1688 if (i->head) {
1689 list_add(&up->list, i->head);
1690 spin_unlock_irq(&i->lock);
1691
1692 ret = 0;
1693 } else {
1694 INIT_LIST_HEAD(&up->list);
1695 i->head = &up->list;
1696 spin_unlock_irq(&i->lock);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07001697 irq_flags |= up->port.irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 ret = request_irq(up->port.irq, serial8250_interrupt,
1699 irq_flags, "serial", i);
1700 if (ret < 0)
1701 serial_do_unlink(i, up);
1702 }
1703
1704 return ret;
1705}
1706
1707static void serial_unlink_irq_chain(struct uart_8250_port *up)
1708{
Alan Cox25db8ad2008-08-19 20:49:40 -07001709 struct irq_info *i;
1710 struct hlist_node *n;
1711 struct hlist_head *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Alan Cox25db8ad2008-08-19 20:49:40 -07001713 mutex_lock(&hash_mutex);
1714
1715 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1716
1717 hlist_for_each(n, h) {
1718 i = hlist_entry(n, struct irq_info, node);
1719 if (i->irq == up->port.irq)
1720 break;
1721 }
1722
1723 BUG_ON(n == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 BUG_ON(i->head == NULL);
1725
1726 if (list_empty(i->head))
1727 free_irq(up->port.irq, i);
1728
1729 serial_do_unlink(i, up);
Alan Cox25db8ad2008-08-19 20:49:40 -07001730 mutex_unlock(&hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
1733/*
1734 * This function is used to handle ports that do not have an
1735 * interrupt. This doesn't work very well for 16450's, but gives
1736 * barely passable results for a 16550A. (Although at the expense
1737 * of much CPU overhead).
1738 */
1739static void serial8250_timeout(unsigned long data)
1740{
1741 struct uart_8250_port *up = (struct uart_8250_port *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 unsigned int iir;
1743
1744 iir = serial_in(up, UART_IIR);
Russell King45e24602006-01-04 19:19:06 +00001745 if (!(iir & UART_IIR_NO_INT))
David Howells7d12e782006-10-05 14:55:46 +01001746 serial8250_handle_port(up);
Anton Vorontsov54381062010-10-01 17:21:25 +04001747 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
Alex Williamson40b36da2007-02-14 00:33:04 -08001748}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Alex Williamson40b36da2007-02-14 00:33:04 -08001750static void serial8250_backup_timeout(unsigned long data)
1751{
1752 struct uart_8250_port *up = (struct uart_8250_port *)data;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001753 unsigned int iir, ier = 0, lsr;
1754 unsigned long flags;
Alex Williamson40b36da2007-02-14 00:33:04 -08001755
Al Cooperdbb3b1c2011-07-25 16:19:52 -04001756 spin_lock_irqsave(&up->port.lock, flags);
1757
Alex Williamson40b36da2007-02-14 00:33:04 -08001758 /*
1759 * Must disable interrupts or else we risk racing with the interrupt
1760 * based handler.
1761 */
1762 if (is_real_interrupt(up->port.irq)) {
1763 ier = serial_in(up, UART_IER);
1764 serial_out(up, UART_IER, 0);
1765 }
1766
1767 iir = serial_in(up, UART_IIR);
1768
1769 /*
1770 * This should be a safe test for anyone who doesn't trust the
1771 * IIR bits on their UART, but it's specifically designed for
1772 * the "Diva" UART used on the management processor on many HP
1773 * ia64 and parisc boxes.
1774 */
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001775 lsr = serial_in(up, UART_LSR);
1776 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -08001777 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
Alan Coxebd2c8f2009-09-19 13:13:28 -07001778 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001779 (lsr & UART_LSR_THRE)) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001780 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1781 iir |= UART_IIR_THRI;
1782 }
1783
1784 if (!(iir & UART_IIR_NO_INT))
Al Cooperdbb3b1c2011-07-25 16:19:52 -04001785 transmit_chars(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001786
1787 if (is_real_interrupt(up->port.irq))
1788 serial_out(up, UART_IER, ier);
1789
Al Cooperdbb3b1c2011-07-25 16:19:52 -04001790 spin_unlock_irqrestore(&up->port.lock, flags);
1791
Alex Williamson40b36da2007-02-14 00:33:04 -08001792 /* Standard timer interval plus 0.2s to keep the port running */
Alan Cox6f803cd2008-02-08 04:18:52 -08001793 mod_timer(&up->timer,
Anton Vorontsov54381062010-10-01 17:21:25 +04001794 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
1797static unsigned int serial8250_tx_empty(struct uart_port *port)
1798{
Jamie Iles49d57412010-12-01 23:39:35 +00001799 struct uart_8250_port *up =
1800 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 unsigned long flags;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001802 unsigned int lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 spin_lock_irqsave(&up->port.lock, flags);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001805 lsr = serial_in(up, UART_LSR);
1806 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 spin_unlock_irqrestore(&up->port.lock, flags);
1808
Dick Hollenbeckbca47612009-12-09 12:31:34 -08001809 return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
1812static unsigned int serial8250_get_mctrl(struct uart_port *port)
1813{
Jamie Iles49d57412010-12-01 23:39:35 +00001814 struct uart_8250_port *up =
1815 container_of(port, struct uart_8250_port, port);
Russell King2af7cd62006-01-04 16:55:09 +00001816 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 unsigned int ret;
1818
Russell King2af7cd62006-01-04 16:55:09 +00001819 status = check_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 ret = 0;
1822 if (status & UART_MSR_DCD)
1823 ret |= TIOCM_CAR;
1824 if (status & UART_MSR_RI)
1825 ret |= TIOCM_RNG;
1826 if (status & UART_MSR_DSR)
1827 ret |= TIOCM_DSR;
1828 if (status & UART_MSR_CTS)
1829 ret |= TIOCM_CTS;
1830 return ret;
1831}
1832
1833static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1834{
Jamie Iles49d57412010-12-01 23:39:35 +00001835 struct uart_8250_port *up =
1836 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 unsigned char mcr = 0;
1838
1839 if (mctrl & TIOCM_RTS)
1840 mcr |= UART_MCR_RTS;
1841 if (mctrl & TIOCM_DTR)
1842 mcr |= UART_MCR_DTR;
1843 if (mctrl & TIOCM_OUT1)
1844 mcr |= UART_MCR_OUT1;
1845 if (mctrl & TIOCM_OUT2)
1846 mcr |= UART_MCR_OUT2;
1847 if (mctrl & TIOCM_LOOP)
1848 mcr |= UART_MCR_LOOP;
1849
1850 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1851
1852 serial_out(up, UART_MCR, mcr);
1853}
1854
1855static void serial8250_break_ctl(struct uart_port *port, int break_state)
1856{
Jamie Iles49d57412010-12-01 23:39:35 +00001857 struct uart_8250_port *up =
1858 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 unsigned long flags;
1860
1861 spin_lock_irqsave(&up->port.lock, flags);
1862 if (break_state == -1)
1863 up->lcr |= UART_LCR_SBC;
1864 else
1865 up->lcr &= ~UART_LCR_SBC;
1866 serial_out(up, UART_LCR, up->lcr);
1867 spin_unlock_irqrestore(&up->port.lock, flags);
1868}
1869
Alex Williamson40b36da2007-02-14 00:33:04 -08001870/*
1871 * Wait for transmitter & holding register to empty
1872 */
Will Newtonb5d674a2008-10-13 10:36:21 +01001873static void wait_for_xmitr(struct uart_8250_port *up, int bits)
Alex Williamson40b36da2007-02-14 00:33:04 -08001874{
1875 unsigned int status, tmout = 10000;
1876
1877 /* Wait up to 10ms for the character(s) to be sent. */
David Daney97d303b2010-10-05 11:40:07 -07001878 for (;;) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001879 status = serial_in(up, UART_LSR);
1880
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001881 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -08001882
David Daney97d303b2010-10-05 11:40:07 -07001883 if ((status & bits) == bits)
1884 break;
Alex Williamson40b36da2007-02-14 00:33:04 -08001885 if (--tmout == 0)
1886 break;
1887 udelay(1);
David Daney97d303b2010-10-05 11:40:07 -07001888 }
Alex Williamson40b36da2007-02-14 00:33:04 -08001889
1890 /* Wait up to 1s for flow control if necessary */
1891 if (up->port.flags & UPF_CONS_FLOW) {
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001892 unsigned int tmout;
1893 for (tmout = 1000000; tmout; tmout--) {
1894 unsigned int msr = serial_in(up, UART_MSR);
1895 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1896 if (msr & UART_MSR_CTS)
1897 break;
Alex Williamson40b36da2007-02-14 00:33:04 -08001898 udelay(1);
1899 touch_nmi_watchdog();
1900 }
1901 }
1902}
1903
Jason Wesself2d937f2008-04-17 20:05:37 +02001904#ifdef CONFIG_CONSOLE_POLL
1905/*
1906 * Console polling routines for writing and reading from the uart while
1907 * in an interrupt or debug context.
1908 */
1909
1910static int serial8250_get_poll_char(struct uart_port *port)
1911{
Jamie Iles49d57412010-12-01 23:39:35 +00001912 struct uart_8250_port *up =
1913 container_of(port, struct uart_8250_port, port);
Jason Wesself2d937f2008-04-17 20:05:37 +02001914 unsigned char lsr = serial_inp(up, UART_LSR);
1915
Jason Wesself5316b42010-05-20 21:04:22 -05001916 if (!(lsr & UART_LSR_DR))
1917 return NO_POLL_CHAR;
Jason Wesself2d937f2008-04-17 20:05:37 +02001918
1919 return serial_inp(up, UART_RX);
1920}
1921
1922
1923static void serial8250_put_poll_char(struct uart_port *port,
1924 unsigned char c)
1925{
1926 unsigned int ier;
Jamie Iles49d57412010-12-01 23:39:35 +00001927 struct uart_8250_port *up =
1928 container_of(port, struct uart_8250_port, port);
Jason Wesself2d937f2008-04-17 20:05:37 +02001929
1930 /*
1931 * First save the IER then disable the interrupts
1932 */
1933 ier = serial_in(up, UART_IER);
1934 if (up->capabilities & UART_CAP_UUE)
1935 serial_out(up, UART_IER, UART_IER_UUE);
1936 else
1937 serial_out(up, UART_IER, 0);
1938
1939 wait_for_xmitr(up, BOTH_EMPTY);
1940 /*
1941 * Send the character out.
1942 * If a LF, also do CR...
1943 */
1944 serial_out(up, UART_TX, c);
1945 if (c == 10) {
1946 wait_for_xmitr(up, BOTH_EMPTY);
1947 serial_out(up, UART_TX, 13);
1948 }
1949
1950 /*
1951 * Finally, wait for transmitter to become empty
1952 * and restore the IER
1953 */
1954 wait_for_xmitr(up, BOTH_EMPTY);
1955 serial_out(up, UART_IER, ier);
1956}
1957
1958#endif /* CONFIG_CONSOLE_POLL */
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960static int serial8250_startup(struct uart_port *port)
1961{
Jamie Iles49d57412010-12-01 23:39:35 +00001962 struct uart_8250_port *up =
1963 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 unsigned long flags;
Russell King55d3b282005-06-23 15:05:41 +01001965 unsigned char lsr, iir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 int retval;
1967
Ondrej Puzmane4f05af2010-12-04 21:17:38 +01001968 up->port.fifosize = uart_config[up->port.type].fifo_size;
1969 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 up->capabilities = uart_config[up->port.type].flags;
1971 up->mcr = 0;
1972
Alan Coxb8e7e402009-05-28 14:01:35 +01001973 if (up->port.iotype != up->cur_iotype)
1974 set_io_from_upio(port);
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (up->port.type == PORT_16C950) {
1977 /* Wake up and initialize UART */
1978 up->acr = 0;
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001979 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 serial_outp(up, UART_EFR, UART_EFR_ECB);
1981 serial_outp(up, UART_IER, 0);
1982 serial_outp(up, UART_LCR, 0);
1983 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
Wolfram Sang7d73aaf2011-12-09 18:07:13 +01001984 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 serial_outp(up, UART_EFR, UART_EFR_ECB);
1986 serial_outp(up, UART_LCR, 0);
1987 }
1988
1989#ifdef CONFIG_SERIAL_8250_RSA
1990 /*
1991 * If this is an RSA port, see if we can kick it up to the
1992 * higher speed clock.
1993 */
1994 enable_rsa(up);
1995#endif
1996
1997 /*
1998 * Clear the FIFO buffers and disable them.
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08001999 * (they will be reenabled in set_termios())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 */
2001 serial8250_clear_fifos(up);
2002
2003 /*
2004 * Clear the interrupt registers.
2005 */
2006 (void) serial_inp(up, UART_LSR);
2007 (void) serial_inp(up, UART_RX);
2008 (void) serial_inp(up, UART_IIR);
2009 (void) serial_inp(up, UART_MSR);
2010
2011 /*
2012 * At this point, there's no way the LSR could still be 0xff;
2013 * if it is, then bail out, because there's likely no UART
2014 * here.
2015 */
2016 if (!(up->port.flags & UPF_BUGGY_UART) &&
2017 (serial_inp(up, UART_LSR) == 0xff)) {
Konrad Rzeszutek Wilk7808a4c2011-09-26 09:14:34 -04002018 printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2019 serial_index(&up->port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return -ENODEV;
2021 }
2022
2023 /*
2024 * For a XR16C850, we need to set the trigger levels
2025 */
2026 if (up->port.type == PORT_16850) {
2027 unsigned char fctr;
2028
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08002029 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2032 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2033 serial_outp(up, UART_TRG, UART_TRG_96);
2034 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2035 serial_outp(up, UART_TRG, UART_TRG_96);
2036
2037 serial_outp(up, UART_LCR, 0);
2038 }
2039
Alex Williamson40b36da2007-02-14 00:33:04 -08002040 if (is_real_interrupt(up->port.irq)) {
Alex Williamson01c194d2008-04-28 02:14:09 -07002041 unsigned char iir1;
Alex Williamson40b36da2007-02-14 00:33:04 -08002042 /*
2043 * Test for UARTs that do not reassert THRE when the
2044 * transmitter is idle and the interrupt has already
2045 * been cleared. Real 16550s should always reassert
2046 * this interrupt whenever the transmitter is idle and
2047 * the interrupt is enabled. Delays are necessary to
2048 * allow register changes to become visible.
2049 */
Borislav Petkovc389d272008-07-29 22:33:32 -07002050 spin_lock_irqsave(&up->port.lock, flags);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002051 if (up->port.irqflags & IRQF_SHARED)
Anton Vorontsov768aec02008-07-22 11:21:07 +01002052 disable_irq_nosync(up->port.irq);
Alex Williamson40b36da2007-02-14 00:33:04 -08002053
2054 wait_for_xmitr(up, UART_LSR_THRE);
2055 serial_out_sync(up, UART_IER, UART_IER_THRI);
2056 udelay(1); /* allow THRE to set */
Alex Williamson01c194d2008-04-28 02:14:09 -07002057 iir1 = serial_in(up, UART_IIR);
Alex Williamson40b36da2007-02-14 00:33:04 -08002058 serial_out(up, UART_IER, 0);
2059 serial_out_sync(up, UART_IER, UART_IER_THRI);
2060 udelay(1); /* allow a working UART time to re-assert THRE */
2061 iir = serial_in(up, UART_IIR);
2062 serial_out(up, UART_IER, 0);
2063
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002064 if (up->port.irqflags & IRQF_SHARED)
Anton Vorontsov768aec02008-07-22 11:21:07 +01002065 enable_irq(up->port.irq);
Borislav Petkovc389d272008-07-29 22:33:32 -07002066 spin_unlock_irqrestore(&up->port.lock, flags);
Alex Williamson40b36da2007-02-14 00:33:04 -08002067
2068 /*
2069 * If the interrupt is not reasserted, setup a timer to
2070 * kick the UART on a regular basis.
2071 */
Alex Williamson01c194d2008-04-28 02:14:09 -07002072 if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
Will Newton363f66f2008-09-02 14:35:44 -07002073 up->bugs |= UART_BUG_THRE;
David S. Miller84408382008-10-13 10:45:26 +01002074 pr_debug("ttyS%d - using backup timer\n",
2075 serial_index(port));
Alex Williamson40b36da2007-02-14 00:33:04 -08002076 }
2077 }
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 /*
Will Newton363f66f2008-09-02 14:35:44 -07002080 * The above check will only give an accurate result the first time
2081 * the port is opened so this value needs to be preserved.
2082 */
2083 if (up->bugs & UART_BUG_THRE) {
2084 up->timer.function = serial8250_backup_timeout;
2085 up->timer.data = (unsigned long)up;
2086 mod_timer(&up->timer, jiffies +
Anton Vorontsov54381062010-10-01 17:21:25 +04002087 uart_poll_timeout(port) + HZ / 5);
Will Newton363f66f2008-09-02 14:35:44 -07002088 }
2089
2090 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 * If the "interrupt" for this port doesn't correspond with any
2092 * hardware interrupt, we use a timer-based system. The original
2093 * driver used to do this with IRQ0.
2094 */
2095 if (!is_real_interrupt(up->port.irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 up->timer.data = (unsigned long)up;
Anton Vorontsov54381062010-10-01 17:21:25 +04002097 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 } else {
2099 retval = serial_link_irq_chain(up);
2100 if (retval)
2101 return retval;
2102 }
2103
2104 /*
2105 * Now, initialize the UART
2106 */
2107 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
2108
2109 spin_lock_irqsave(&up->port.lock, flags);
2110 if (up->port.flags & UPF_FOURPORT) {
2111 if (!is_real_interrupt(up->port.irq))
2112 up->port.mctrl |= TIOCM_OUT1;
2113 } else
2114 /*
2115 * Most PC uarts need OUT2 raised to enable interrupts.
2116 */
2117 if (is_real_interrupt(up->port.irq))
2118 up->port.mctrl |= TIOCM_OUT2;
2119
2120 serial8250_set_mctrl(&up->port, up->port.mctrl);
Russell King55d3b282005-06-23 15:05:41 +01002121
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002122 /* Serial over Lan (SoL) hack:
2123 Intel 8257x Gigabit ethernet chips have a
2124 16550 emulation, to be used for Serial Over Lan.
2125 Those chips take a longer time than a normal
2126 serial device to signalize that a transmission
2127 data was queued. Due to that, the above test generally
2128 fails. One solution would be to delay the reading of
2129 iir. However, this is not reliable, since the timeout
2130 is variable. So, let's just don't test if we receive
2131 TX irq. This way, we'll never enable UART_BUG_TXEN.
2132 */
Chuck Ebbertd41a4b52009-10-01 15:44:26 -07002133 if (skip_txen_test || up->port.flags & UPF_NO_TXEN_TEST)
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002134 goto dont_test_tx_en;
2135
Russell King55d3b282005-06-23 15:05:41 +01002136 /*
2137 * Do a quick test to see if we receive an
2138 * interrupt when we enable the TX irq.
2139 */
2140 serial_outp(up, UART_IER, UART_IER_THRI);
2141 lsr = serial_in(up, UART_LSR);
2142 iir = serial_in(up, UART_IIR);
2143 serial_outp(up, UART_IER, 0);
2144
2145 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
Russell King67f76542005-06-23 22:26:43 +01002146 if (!(up->bugs & UART_BUG_TXEN)) {
2147 up->bugs |= UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002148 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
David S. Miller84408382008-10-13 10:45:26 +01002149 serial_index(port));
Russell King55d3b282005-06-23 15:05:41 +01002150 }
2151 } else {
Russell King67f76542005-06-23 22:26:43 +01002152 up->bugs &= ~UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002153 }
2154
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002155dont_test_tx_en:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 spin_unlock_irqrestore(&up->port.lock, flags);
2157
2158 /*
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002159 * Clear the interrupt registers again for luck, and clear the
2160 * saved flags to avoid getting false values from polling
2161 * routines or the previous session.
2162 */
2163 serial_inp(up, UART_LSR);
2164 serial_inp(up, UART_RX);
2165 serial_inp(up, UART_IIR);
2166 serial_inp(up, UART_MSR);
2167 up->lsr_saved_flags = 0;
2168 up->msr_saved_flags = 0;
2169
2170 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 * Finally, enable interrupts. Note: Modem status interrupts
2172 * are set via set_termios(), which will be occurring imminently
2173 * anyway, so we don't enable them here.
2174 */
2175 up->ier = UART_IER_RLSI | UART_IER_RDI;
2176 serial_outp(up, UART_IER, up->ier);
2177
2178 if (up->port.flags & UPF_FOURPORT) {
2179 unsigned int icp;
2180 /*
2181 * Enable interrupts on the AST Fourport board
2182 */
2183 icp = (up->port.iobase & 0xfe0) | 0x01f;
2184 outb_p(0x80, icp);
2185 (void) inb_p(icp);
2186 }
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return 0;
2189}
2190
2191static void serial8250_shutdown(struct uart_port *port)
2192{
Jamie Iles49d57412010-12-01 23:39:35 +00002193 struct uart_8250_port *up =
2194 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 unsigned long flags;
2196
2197 /*
2198 * Disable interrupts from this port
2199 */
2200 up->ier = 0;
2201 serial_outp(up, UART_IER, 0);
2202
2203 spin_lock_irqsave(&up->port.lock, flags);
2204 if (up->port.flags & UPF_FOURPORT) {
2205 /* reset interrupts on the AST Fourport board */
2206 inb((up->port.iobase & 0xfe0) | 0x1f);
2207 up->port.mctrl |= TIOCM_OUT1;
2208 } else
2209 up->port.mctrl &= ~TIOCM_OUT2;
2210
2211 serial8250_set_mctrl(&up->port, up->port.mctrl);
2212 spin_unlock_irqrestore(&up->port.lock, flags);
2213
2214 /*
2215 * Disable break condition and FIFOs
2216 */
2217 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
2218 serial8250_clear_fifos(up);
2219
2220#ifdef CONFIG_SERIAL_8250_RSA
2221 /*
2222 * Reset the RSA board back to 115kbps compat mode.
2223 */
2224 disable_rsa(up);
2225#endif
2226
2227 /*
2228 * Read data port to reset things, and then unlink from
2229 * the IRQ chain.
2230 */
2231 (void) serial_in(up, UART_RX);
2232
Alex Williamson40b36da2007-02-14 00:33:04 -08002233 del_timer_sync(&up->timer);
2234 up->timer.function = serial8250_timeout;
2235 if (is_real_interrupt(up->port.irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 serial_unlink_irq_chain(up);
2237}
2238
2239static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
2240{
2241 unsigned int quot;
2242
2243 /*
2244 * Handle magic divisors for baud rates above baud_base on
2245 * SMSC SuperIO chips.
2246 */
2247 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2248 baud == (port->uartclk/4))
2249 quot = 0x8001;
2250 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2251 baud == (port->uartclk/8))
2252 quot = 0x8002;
2253 else
2254 quot = uart_get_divisor(port, baud);
2255
2256 return quot;
2257}
2258
Philippe Langlais235dae52010-07-29 17:13:57 +02002259void
2260serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2261 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
Jamie Iles49d57412010-12-01 23:39:35 +00002263 struct uart_8250_port *up =
2264 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 unsigned char cval, fcr = 0;
2266 unsigned long flags;
2267 unsigned int baud, quot;
2268
2269 switch (termios->c_cflag & CSIZE) {
2270 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +01002271 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 break;
2273 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +01002274 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 break;
2276 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +01002277 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 break;
2279 default:
2280 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +01002281 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 break;
2283 }
2284
2285 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +01002286 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 if (termios->c_cflag & PARENB)
2288 cval |= UART_LCR_PARITY;
2289 if (!(termios->c_cflag & PARODD))
2290 cval |= UART_LCR_EPAR;
2291#ifdef CMSPAR
2292 if (termios->c_cflag & CMSPAR)
2293 cval |= UART_LCR_SPAR;
2294#endif
2295
2296 /*
2297 * Ask the core to calculate the divisor for us.
2298 */
Anton Vorontsov24d481e2009-09-19 13:13:20 -07002299 baud = uart_get_baud_rate(port, termios, old,
2300 port->uartclk / 16 / 0xffff,
2301 port->uartclk / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 quot = serial8250_get_divisor(port, baud);
2303
2304 /*
Russell King4ba5e352005-06-23 10:43:04 +01002305 * Oxford Semi 952 rev B workaround
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 */
Russell King4ba5e352005-06-23 10:43:04 +01002307 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
Alan Cox3e8d4e22008-02-04 22:27:53 -08002308 quot++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
2311 if (baud < 2400)
2312 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2313 else
2314 fcr = uart_config[up->port.type].fcr;
2315 }
2316
2317 /*
2318 * MCR-based auto flow control. When AFE is enabled, RTS will be
2319 * deasserted when the receive FIFO contains more characters than
2320 * the trigger, or the MCR RTS bit is cleared. In the case where
2321 * the remote UART is not using CTS auto flow control, we must
2322 * have sufficient FIFO entries for the latency of the remote
2323 * UART to respond. IOW, at least 32 bytes of FIFO.
2324 */
2325 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
2326 up->mcr &= ~UART_MCR_AFE;
2327 if (termios->c_cflag & CRTSCTS)
2328 up->mcr |= UART_MCR_AFE;
2329 }
2330
2331 /*
2332 * Ok, we're now changing the port state. Do it with
2333 * interrupts disabled.
2334 */
2335 spin_lock_irqsave(&up->port.lock, flags);
2336
2337 /*
2338 * Update the per-port timeout.
2339 */
2340 uart_update_timeout(port, termios->c_cflag, baud);
2341
2342 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2343 if (termios->c_iflag & INPCK)
2344 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2345 if (termios->c_iflag & (BRKINT | PARMRK))
2346 up->port.read_status_mask |= UART_LSR_BI;
2347
2348 /*
2349 * Characteres to ignore
2350 */
2351 up->port.ignore_status_mask = 0;
2352 if (termios->c_iflag & IGNPAR)
2353 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2354 if (termios->c_iflag & IGNBRK) {
2355 up->port.ignore_status_mask |= UART_LSR_BI;
2356 /*
2357 * If we're ignoring parity and break indicators,
2358 * ignore overruns too (for real raw support).
2359 */
2360 if (termios->c_iflag & IGNPAR)
2361 up->port.ignore_status_mask |= UART_LSR_OE;
2362 }
2363
2364 /*
2365 * ignore all characters if CREAD is not set
2366 */
2367 if ((termios->c_cflag & CREAD) == 0)
2368 up->port.ignore_status_mask |= UART_LSR_DR;
2369
2370 /*
2371 * CTS flow control flag and modem status interrupts
2372 */
Ingo Molnarf8b372a2010-11-13 16:21:58 +01002373 up->ier &= ~UART_IER_MSI;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002374 if (!(up->bugs & UART_BUG_NOMSR) &&
2375 UART_ENABLE_MS(&up->port, termios->c_cflag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 up->ier |= UART_IER_MSI;
2377 if (up->capabilities & UART_CAP_UUE)
Stephen Warren4539c242011-05-17 16:12:36 -06002378 up->ier |= UART_IER_UUE;
2379 if (up->capabilities & UART_CAP_RTOIE)
2380 up->ier |= UART_IER_RTOIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 serial_out(up, UART_IER, up->ier);
2383
2384 if (up->capabilities & UART_CAP_EFR) {
2385 unsigned char efr = 0;
2386 /*
2387 * TI16C752/Startech hardware flow control. FIXME:
2388 * - TI16C752 requires control thresholds to be set.
2389 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2390 */
2391 if (termios->c_cflag & CRTSCTS)
2392 efr |= UART_EFR_CTS;
2393
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08002394 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Søren Holm06315342011-09-02 22:55:37 +02002395 if (up->port.flags & UPF_EXAR_EFR)
2396 serial_outp(up, UART_XR_EFR, efr);
2397 else
2398 serial_outp(up, UART_EFR, efr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
2400
Russell Kingf2eda272008-09-01 21:47:59 +01002401#ifdef CONFIG_ARCH_OMAP
Jonathan McDowell255341c2006-08-14 23:05:32 -07002402 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
Russell King56685452008-09-01 21:25:33 +01002403 if (cpu_is_omap1510() && is_omap_port(up)) {
Jonathan McDowell255341c2006-08-14 23:05:32 -07002404 if (baud == 115200) {
2405 quot = 1;
2406 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2407 } else
2408 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2409 }
2410#endif
2411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (up->capabilities & UART_NATSEMI) {
2413 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
2414 serial_outp(up, UART_LCR, 0xe0);
2415 } else {
2416 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2417 }
2418
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +01002419 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 /*
2422 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2423 * is written without DLAB set, this mode will be disabled.
2424 */
2425 if (up->port.type == PORT_16750)
2426 serial_outp(up, UART_FCR, fcr);
2427
2428 serial_outp(up, UART_LCR, cval); /* reset DLAB */
2429 up->lcr = cval; /* Save LCR */
2430 if (up->port.type != PORT_16750) {
2431 if (fcr & UART_FCR_ENABLE_FIFO) {
2432 /* emulated UARTs (Lucent Venus 167x) need two steps */
2433 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
2434 }
2435 serial_outp(up, UART_FCR, fcr); /* set fcr */
2436 }
2437 serial8250_set_mctrl(&up->port, up->port.mctrl);
2438 spin_unlock_irqrestore(&up->port.lock, flags);
Alan Coxe991a2b2008-04-28 02:14:06 -07002439 /* Don't rewrite B0 */
2440 if (tty_termios_baud_rate(termios))
2441 tty_termios_encode_baud_rate(termios, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
Philippe Langlais235dae52010-07-29 17:13:57 +02002443EXPORT_SYMBOL(serial8250_do_set_termios);
2444
2445static void
2446serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2447 struct ktermios *old)
2448{
2449 if (port->set_termios)
2450 port->set_termios(port, termios, old);
2451 else
2452 serial8250_do_set_termios(port, termios, old);
2453}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455static void
Arnd Bergmanna0821df2010-06-01 22:53:11 +02002456serial8250_set_ldisc(struct uart_port *port, int new)
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002457{
Arnd Bergmanna0821df2010-06-01 22:53:11 +02002458 if (new == N_PPS) {
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002459 port->flags |= UPF_HARDPPS_CD;
2460 serial8250_enable_ms(port);
2461 } else
2462 port->flags &= ~UPF_HARDPPS_CD;
2463}
2464
Manuel Laussc161afe2010-09-25 15:13:45 +02002465
2466void serial8250_do_pm(struct uart_port *port, unsigned int state,
2467 unsigned int oldstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Jamie Iles49d57412010-12-01 23:39:35 +00002469 struct uart_8250_port *p =
2470 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 serial8250_set_sleep(p, state != 0);
Manuel Laussc161afe2010-09-25 15:13:45 +02002473}
2474EXPORT_SYMBOL(serial8250_do_pm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Manuel Laussc161afe2010-09-25 15:13:45 +02002476static void
2477serial8250_pm(struct uart_port *port, unsigned int state,
2478 unsigned int oldstate)
2479{
2480 if (port->pm)
2481 port->pm(port, state, oldstate);
2482 else
2483 serial8250_do_pm(port, state, oldstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
2485
Russell Kingf2eda272008-09-01 21:47:59 +01002486static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2487{
2488 if (pt->port.iotype == UPIO_AU)
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002489 return 0x1000;
Russell Kingf2eda272008-09-01 21:47:59 +01002490#ifdef CONFIG_ARCH_OMAP
2491 if (is_omap_port(pt))
2492 return 0x16 << pt->port.regshift;
2493#endif
2494 return 8 << pt->port.regshift;
2495}
2496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497/*
2498 * Resource handling.
2499 */
2500static int serial8250_request_std_resource(struct uart_8250_port *up)
2501{
Russell Kingf2eda272008-09-01 21:47:59 +01002502 unsigned int size = serial8250_port_size(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 int ret = 0;
2504
2505 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002506 case UPIO_AU:
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002507 case UPIO_TSI:
2508 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 case UPIO_MEM:
2510 if (!up->port.mapbase)
2511 break;
2512
2513 if (!request_mem_region(up->port.mapbase, size, "serial")) {
2514 ret = -EBUSY;
2515 break;
2516 }
2517
2518 if (up->port.flags & UPF_IOREMAP) {
Alan Cox6f441fe2008-05-01 04:34:59 -07002519 up->port.membase = ioremap_nocache(up->port.mapbase,
2520 size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 if (!up->port.membase) {
2522 release_mem_region(up->port.mapbase, size);
2523 ret = -ENOMEM;
2524 }
2525 }
2526 break;
2527
2528 case UPIO_HUB6:
2529 case UPIO_PORT:
2530 if (!request_region(up->port.iobase, size, "serial"))
2531 ret = -EBUSY;
2532 break;
2533 }
2534 return ret;
2535}
2536
2537static void serial8250_release_std_resource(struct uart_8250_port *up)
2538{
Russell Kingf2eda272008-09-01 21:47:59 +01002539 unsigned int size = serial8250_port_size(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002542 case UPIO_AU:
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002543 case UPIO_TSI:
2544 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 case UPIO_MEM:
2546 if (!up->port.mapbase)
2547 break;
2548
2549 if (up->port.flags & UPF_IOREMAP) {
2550 iounmap(up->port.membase);
2551 up->port.membase = NULL;
2552 }
2553
2554 release_mem_region(up->port.mapbase, size);
2555 break;
2556
2557 case UPIO_HUB6:
2558 case UPIO_PORT:
2559 release_region(up->port.iobase, size);
2560 break;
2561 }
2562}
2563
2564static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2565{
2566 unsigned long start = UART_RSA_BASE << up->port.regshift;
2567 unsigned int size = 8 << up->port.regshift;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002568 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
2570 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 case UPIO_HUB6:
2572 case UPIO_PORT:
2573 start += up->port.iobase;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002574 if (request_region(start, size, "serial-rsa"))
2575 ret = 0;
2576 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 ret = -EBUSY;
2578 break;
2579 }
2580
2581 return ret;
2582}
2583
2584static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2585{
2586 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2587 unsigned int size = 8 << up->port.regshift;
2588
2589 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 case UPIO_HUB6:
2591 case UPIO_PORT:
2592 release_region(up->port.iobase + offset, size);
2593 break;
2594 }
2595}
2596
2597static void serial8250_release_port(struct uart_port *port)
2598{
Jamie Iles49d57412010-12-01 23:39:35 +00002599 struct uart_8250_port *up =
2600 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
2602 serial8250_release_std_resource(up);
2603 if (up->port.type == PORT_RSA)
2604 serial8250_release_rsa_resource(up);
2605}
2606
2607static int serial8250_request_port(struct uart_port *port)
2608{
Jamie Iles49d57412010-12-01 23:39:35 +00002609 struct uart_8250_port *up =
2610 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 int ret = 0;
2612
2613 ret = serial8250_request_std_resource(up);
2614 if (ret == 0 && up->port.type == PORT_RSA) {
2615 ret = serial8250_request_rsa_resource(up);
2616 if (ret < 0)
2617 serial8250_release_std_resource(up);
2618 }
2619
2620 return ret;
2621}
2622
2623static void serial8250_config_port(struct uart_port *port, int flags)
2624{
Jamie Iles49d57412010-12-01 23:39:35 +00002625 struct uart_8250_port *up =
2626 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 int probeflags = PROBE_ANY;
2628 int ret;
2629
2630 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 * Find the region that we can probe for. This in turn
2632 * tells us whether we can probe for the type of port.
2633 */
2634 ret = serial8250_request_std_resource(up);
2635 if (ret < 0)
2636 return;
2637
2638 ret = serial8250_request_rsa_resource(up);
2639 if (ret < 0)
2640 probeflags &= ~PROBE_RSA;
2641
Alan Coxb8e7e402009-05-28 14:01:35 +01002642 if (up->port.iotype != up->cur_iotype)
2643 set_io_from_upio(port);
2644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if (flags & UART_CONFIG_TYPE)
2646 autoconfig(up, probeflags);
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002647
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002648 /* if access method is AU, it is a 16550 with a quirk */
2649 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
2650 up->bugs |= UART_BUG_NOMSR;
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2653 autoconfig_irq(up);
2654
2655 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2656 serial8250_release_rsa_resource(up);
2657 if (up->port.type == PORT_UNKNOWN)
2658 serial8250_release_std_resource(up);
2659}
2660
2661static int
2662serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2663{
Yinghai Lua62c4132008-08-19 20:49:55 -07002664 if (ser->irq >= nr_irqs || ser->irq < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2666 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2667 ser->type == PORT_STARTECH)
2668 return -EINVAL;
2669 return 0;
2670}
2671
2672static const char *
2673serial8250_type(struct uart_port *port)
2674{
2675 int type = port->type;
2676
2677 if (type >= ARRAY_SIZE(uart_config))
2678 type = 0;
2679 return uart_config[type].name;
2680}
2681
2682static struct uart_ops serial8250_pops = {
2683 .tx_empty = serial8250_tx_empty,
2684 .set_mctrl = serial8250_set_mctrl,
2685 .get_mctrl = serial8250_get_mctrl,
2686 .stop_tx = serial8250_stop_tx,
2687 .start_tx = serial8250_start_tx,
2688 .stop_rx = serial8250_stop_rx,
2689 .enable_ms = serial8250_enable_ms,
2690 .break_ctl = serial8250_break_ctl,
2691 .startup = serial8250_startup,
2692 .shutdown = serial8250_shutdown,
2693 .set_termios = serial8250_set_termios,
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002694 .set_ldisc = serial8250_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 .pm = serial8250_pm,
2696 .type = serial8250_type,
2697 .release_port = serial8250_release_port,
2698 .request_port = serial8250_request_port,
2699 .config_port = serial8250_config_port,
2700 .verify_port = serial8250_verify_port,
Jason Wesself2d937f2008-04-17 20:05:37 +02002701#ifdef CONFIG_CONSOLE_POLL
2702 .poll_get_char = serial8250_get_poll_char,
2703 .poll_put_char = serial8250_put_poll_char,
2704#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705};
2706
2707static struct uart_8250_port serial8250_ports[UART_NR];
2708
Alan Coxaf7f3742010-10-18 11:38:02 -07002709static void (*serial8250_isa_config)(int port, struct uart_port *up,
2710 unsigned short *capabilities);
2711
2712void serial8250_set_isa_configurator(
2713 void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
2714{
2715 serial8250_isa_config = v;
2716}
2717EXPORT_SYMBOL(serial8250_set_isa_configurator);
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719static void __init serial8250_isa_init_ports(void)
2720{
2721 struct uart_8250_port *up;
2722 static int first = 1;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002723 int i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 if (!first)
2726 return;
2727 first = 0;
2728
Dave Jonesa61c2d72006-01-07 23:18:19 +00002729 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 struct uart_8250_port *up = &serial8250_ports[i];
2731
2732 up->port.line = i;
2733 spin_lock_init(&up->port.lock);
2734
2735 init_timer(&up->timer);
2736 up->timer.function = serial8250_timeout;
2737
2738 /*
2739 * ALPHA_KLUDGE_MCR needs to be killed.
2740 */
2741 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2742 up->mcr_force = ALPHA_KLUDGE_MCR;
2743
2744 up->port.ops = &serial8250_pops;
2745 }
2746
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002747 if (share_irqs)
2748 irqflag = IRQF_SHARED;
2749
Russell King44454bc2005-06-30 22:41:22 +01002750 for (i = 0, up = serial8250_ports;
Dave Jonesa61c2d72006-01-07 23:18:19 +00002751 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 i++, up++) {
2753 up->port.iobase = old_serial_port[i].port;
2754 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002755 up->port.irqflags = old_serial_port[i].irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 up->port.uartclk = old_serial_port[i].baud_base * 16;
2757 up->port.flags = old_serial_port[i].flags;
2758 up->port.hub6 = old_serial_port[i].hub6;
2759 up->port.membase = old_serial_port[i].iomem_base;
2760 up->port.iotype = old_serial_port[i].io_type;
2761 up->port.regshift = old_serial_port[i].iomem_reg_shift;
David Daney7d6a07d2009-01-02 13:49:47 +00002762 set_io_from_upio(&up->port);
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002763 up->port.irqflags |= irqflag;
Alan Coxaf7f3742010-10-18 11:38:02 -07002764 if (serial8250_isa_config != NULL)
2765 serial8250_isa_config(i, &up->port, &up->capabilities);
2766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
2768}
2769
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08002770static void
2771serial8250_init_fixed_type_port(struct uart_8250_port *up, unsigned int type)
2772{
2773 up->port.type = type;
2774 up->port.fifosize = uart_config[type].fifo_size;
2775 up->capabilities = uart_config[type].flags;
2776 up->tx_loadsz = uart_config[type].tx_loadsz;
2777}
2778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779static void __init
2780serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2781{
2782 int i;
2783
Alan Coxb8e7e402009-05-28 14:01:35 +01002784 for (i = 0; i < nr_uarts; i++) {
2785 struct uart_8250_port *up = &serial8250_ports[i];
2786 up->cur_iotype = 0xFF;
2787 }
2788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 serial8250_isa_init_ports();
2790
Dave Jonesa61c2d72006-01-07 23:18:19 +00002791 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 struct uart_8250_port *up = &serial8250_ports[i];
2793
2794 up->port.dev = dev;
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08002795
2796 if (up->port.flags & UPF_FIXED_TYPE)
2797 serial8250_init_fixed_type_port(up, up->port.type);
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 uart_add_one_port(drv, &up->port);
2800 }
2801}
2802
2803#ifdef CONFIG_SERIAL_8250_CONSOLE
2804
Russell Kingd3587882006-03-20 20:00:09 +00002805static void serial8250_console_putchar(struct uart_port *port, int ch)
2806{
Jamie Iles49d57412010-12-01 23:39:35 +00002807 struct uart_8250_port *up =
2808 container_of(port, struct uart_8250_port, port);
Russell Kingd3587882006-03-20 20:00:09 +00002809
2810 wait_for_xmitr(up, UART_LSR_THRE);
2811 serial_out(up, UART_TX, ch);
2812}
2813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814/*
2815 * Print a string to the serial port trying not to disturb
2816 * any possible real use of the port...
2817 *
2818 * The console_lock must be held when we get here.
2819 */
2820static void
2821serial8250_console_write(struct console *co, const char *s, unsigned int count)
2822{
2823 struct uart_8250_port *up = &serial8250_ports[co->index];
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002824 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 unsigned int ier;
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002826 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Andrew Morton78512ec2005-11-07 00:59:13 -08002828 touch_nmi_watchdog();
2829
Andrew Morton68aa2c02006-06-30 02:29:59 -07002830 local_irq_save(flags);
2831 if (up->port.sysrq) {
2832 /* serial8250_handle_port() already took the lock */
2833 locked = 0;
2834 } else if (oops_in_progress) {
2835 locked = spin_trylock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002836 } else
Andrew Morton68aa2c02006-06-30 02:29:59 -07002837 spin_lock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 /*
Ralf Baechledc7bf132006-02-15 09:59:47 +00002840 * First save the IER then disable the interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 */
2842 ier = serial_in(up, UART_IER);
2843
2844 if (up->capabilities & UART_CAP_UUE)
2845 serial_out(up, UART_IER, UART_IER_UUE);
2846 else
2847 serial_out(up, UART_IER, 0);
2848
Russell Kingd3587882006-03-20 20:00:09 +00002849 uart_console_write(&up->port, s, count, serial8250_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 /*
2852 * Finally, wait for transmitter to become empty
2853 * and restore the IER
2854 */
Alan Coxf91a3712006-01-21 14:59:12 +00002855 wait_for_xmitr(up, BOTH_EMPTY);
Russell Kinga88d75b2006-04-30 11:30:15 +01002856 serial_out(up, UART_IER, ier);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002857
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002858 /*
2859 * The receive handling will happen properly because the
2860 * receive ready bit will still be set; it is not cleared
2861 * on read. However, modem control will not, we must
2862 * call it if we have saved something in the saved flags
2863 * while processing with interrupts off.
2864 */
2865 if (up->msr_saved_flags)
2866 check_modem_status(up);
2867
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002868 if (locked)
Andrew Morton68aa2c02006-06-30 02:29:59 -07002869 spin_unlock(&up->port.lock);
2870 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}
2872
Vivek Goyal118c0ac2007-01-11 01:52:44 +01002873static int __init serial8250_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
2875 struct uart_port *port;
2876 int baud = 9600;
2877 int bits = 8;
2878 int parity = 'n';
2879 int flow = 'n';
2880
2881 /*
2882 * Check whether an invalid uart number has been specified, and
2883 * if so, search for the first available port that does have
2884 * console support.
2885 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002886 if (co->index >= nr_uarts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 co->index = 0;
2888 port = &serial8250_ports[co->index].port;
2889 if (!port->iobase && !port->membase)
2890 return -ENODEV;
2891
2892 if (options)
2893 uart_parse_options(options, &baud, &parity, &bits, &flow);
2894
2895 return uart_set_options(port, co, baud, parity, bits, flow);
2896}
2897
Daniel Ritzb6b1d872007-08-03 16:07:43 +02002898static int serial8250_console_early_setup(void)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002899{
2900 return serial8250_find_port_for_earlycon();
2901}
2902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903static struct console serial8250_console = {
2904 .name = "ttyS",
2905 .write = serial8250_console_write,
2906 .device = uart_console_device,
2907 .setup = serial8250_console_setup,
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002908 .early_setup = serial8250_console_early_setup,
Peter Zijlstraa80c49d2010-11-15 21:11:12 +01002909 .flags = CON_PRINTBUFFER | CON_ANYTIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 .index = -1,
2911 .data = &serial8250_reg,
2912};
2913
2914static int __init serial8250_console_init(void)
2915{
Eric W. Biederman05d81d22008-07-12 13:47:53 -07002916 if (nr_uarts > UART_NR)
2917 nr_uarts = UART_NR;
2918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 serial8250_isa_init_ports();
2920 register_console(&serial8250_console);
2921 return 0;
2922}
2923console_initcall(serial8250_console_init);
2924
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002925int serial8250_find_port(struct uart_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926{
2927 int line;
2928 struct uart_port *port;
2929
Dave Jonesa61c2d72006-01-07 23:18:19 +00002930 for (line = 0; line < nr_uarts; line++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 port = &serial8250_ports[line].port;
Russell King50aec3b2006-01-04 18:13:03 +00002932 if (uart_match_port(p, port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 return line;
2934 }
2935 return -ENODEV;
2936}
2937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938#define SERIAL8250_CONSOLE &serial8250_console
2939#else
2940#define SERIAL8250_CONSOLE NULL
2941#endif
2942
2943static struct uart_driver serial8250_reg = {
2944 .owner = THIS_MODULE,
2945 .driver_name = "serial",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 .dev_name = "ttyS",
2947 .major = TTY_MAJOR,
2948 .minor = 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 .cons = SERIAL8250_CONSOLE,
2950};
2951
Russell Kingd856c662006-02-23 10:22:13 +00002952/*
2953 * early_serial_setup - early registration for 8250 ports
2954 *
2955 * Setup an 8250 port structure prior to console initialisation. Use
2956 * after console initialisation will cause undefined behaviour.
2957 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958int __init early_serial_setup(struct uart_port *port)
2959{
David Daneyb4304282009-01-02 13:49:41 +00002960 struct uart_port *p;
2961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 if (port->line >= ARRAY_SIZE(serial8250_ports))
2963 return -ENODEV;
2964
2965 serial8250_isa_init_ports();
David Daneyb4304282009-01-02 13:49:41 +00002966 p = &serial8250_ports[port->line].port;
2967 p->iobase = port->iobase;
2968 p->membase = port->membase;
2969 p->irq = port->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002970 p->irqflags = port->irqflags;
David Daneyb4304282009-01-02 13:49:41 +00002971 p->uartclk = port->uartclk;
2972 p->fifosize = port->fifosize;
2973 p->regshift = port->regshift;
2974 p->iotype = port->iotype;
2975 p->flags = port->flags;
2976 p->mapbase = port->mapbase;
2977 p->private_data = port->private_data;
Helge Deller125c97d2009-01-13 22:51:07 +01002978 p->type = port->type;
2979 p->line = port->line;
David Daney7d6a07d2009-01-02 13:49:47 +00002980
2981 set_io_from_upio(p);
2982 if (port->serial_in)
2983 p->serial_in = port->serial_in;
2984 if (port->serial_out)
2985 p->serial_out = port->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01002986 if (port->handle_irq)
2987 p->handle_irq = port->handle_irq;
2988 else
2989 p->handle_irq = serial8250_default_handle_irq;
David Daney7d6a07d2009-01-02 13:49:47 +00002990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 return 0;
2992}
2993
2994/**
2995 * serial8250_suspend_port - suspend one serial port
2996 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 *
2998 * Suspend one serial port.
2999 */
3000void serial8250_suspend_port(int line)
3001{
3002 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
3003}
3004
3005/**
3006 * serial8250_resume_port - resume one serial port
3007 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 *
3009 * Resume one serial port.
3010 */
3011void serial8250_resume_port(int line)
3012{
David Woodhouseb5b82df2007-05-17 14:27:39 +08003013 struct uart_8250_port *up = &serial8250_ports[line];
3014
3015 if (up->capabilities & UART_NATSEMI) {
David Woodhouseb5b82df2007-05-17 14:27:39 +08003016 /* Ensure it's still in high speed mode */
3017 serial_outp(up, UART_LCR, 0xE0);
3018
Yin Kangkai0d0389e2011-02-09 11:35:18 +08003019 ns16550a_goto_highspeed(up);
David Woodhouseb5b82df2007-05-17 14:27:39 +08003020
3021 serial_outp(up, UART_LCR, 0);
Yin Kangkai95926d22011-02-09 11:34:20 +08003022 up->port.uartclk = 921600*16;
David Woodhouseb5b82df2007-05-17 14:27:39 +08003023 }
3024 uart_resume_port(&serial8250_reg, &up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
3027/*
3028 * Register a set of serial devices attached to a platform device. The
3029 * list is terminated with a zero flags entry, which means we expect
3030 * all entries to have at least UPF_BOOT_AUTOCONF set.
3031 */
Russell King3ae5eae2005-11-09 22:32:44 +00003032static int __devinit serial8250_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
Russell King3ae5eae2005-11-09 22:32:44 +00003034 struct plat_serial8250_port *p = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 struct uart_port port;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003036 int ret, i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038 memset(&port, 0, sizeof(struct uart_port));
3039
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003040 if (share_irqs)
3041 irqflag = IRQF_SHARED;
3042
Russell Kingec9f47c2005-06-27 11:12:54 +01003043 for (i = 0; p && p->flags != 0; p++, i++) {
Will Newton74a197412008-02-04 22:27:50 -08003044 port.iobase = p->iobase;
3045 port.membase = p->membase;
3046 port.irq = p->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07003047 port.irqflags = p->irqflags;
Will Newton74a197412008-02-04 22:27:50 -08003048 port.uartclk = p->uartclk;
3049 port.regshift = p->regshift;
3050 port.iotype = p->iotype;
3051 port.flags = p->flags;
3052 port.mapbase = p->mapbase;
3053 port.hub6 = p->hub6;
3054 port.private_data = p->private_data;
David Daney8e23fcc2009-01-02 13:49:54 +00003055 port.type = p->type;
David Daney7d6a07d2009-01-02 13:49:47 +00003056 port.serial_in = p->serial_in;
3057 port.serial_out = p->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01003058 port.handle_irq = p->handle_irq;
Philippe Langlais235dae52010-07-29 17:13:57 +02003059 port.set_termios = p->set_termios;
Manuel Laussc161afe2010-09-25 15:13:45 +02003060 port.pm = p->pm;
Will Newton74a197412008-02-04 22:27:50 -08003061 port.dev = &dev->dev;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003062 port.irqflags |= irqflag;
Russell Kingec9f47c2005-06-27 11:12:54 +01003063 ret = serial8250_register_port(&port);
3064 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00003065 dev_err(&dev->dev, "unable to register port at index %d "
Josh Boyer4f640ef2007-07-23 18:43:44 -07003066 "(IO%lx MEM%llx IRQ%d): %d\n", i,
3067 p->iobase, (unsigned long long)p->mapbase,
3068 p->irq, ret);
Russell Kingec9f47c2005-06-27 11:12:54 +01003069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
3071 return 0;
3072}
3073
3074/*
3075 * Remove serial ports registered against a platform device.
3076 */
Russell King3ae5eae2005-11-09 22:32:44 +00003077static int __devexit serial8250_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
3079 int i;
3080
Dave Jonesa61c2d72006-01-07 23:18:19 +00003081 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 struct uart_8250_port *up = &serial8250_ports[i];
3083
Russell King3ae5eae2005-11-09 22:32:44 +00003084 if (up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 serial8250_unregister_port(i);
3086 }
3087 return 0;
3088}
3089
Russell King3ae5eae2005-11-09 22:32:44 +00003090static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
3092 int i;
3093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 for (i = 0; i < UART_NR; i++) {
3095 struct uart_8250_port *up = &serial8250_ports[i];
3096
Russell King3ae5eae2005-11-09 22:32:44 +00003097 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 uart_suspend_port(&serial8250_reg, &up->port);
3099 }
3100
3101 return 0;
3102}
3103
Russell King3ae5eae2005-11-09 22:32:44 +00003104static int serial8250_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
3106 int i;
3107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 for (i = 0; i < UART_NR; i++) {
3109 struct uart_8250_port *up = &serial8250_ports[i];
3110
Russell King3ae5eae2005-11-09 22:32:44 +00003111 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
David Woodhouseb5b82df2007-05-17 14:27:39 +08003112 serial8250_resume_port(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 }
3114
3115 return 0;
3116}
3117
Russell King3ae5eae2005-11-09 22:32:44 +00003118static struct platform_driver serial8250_isa_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 .probe = serial8250_probe,
3120 .remove = __devexit_p(serial8250_remove),
3121 .suspend = serial8250_suspend,
3122 .resume = serial8250_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00003123 .driver = {
3124 .name = "serial8250",
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003125 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00003126 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127};
3128
3129/*
3130 * This "device" covers _all_ ISA 8250-compatible serial devices listed
3131 * in the table in include/asm/serial.h
3132 */
3133static struct platform_device *serial8250_isa_devs;
3134
3135/*
3136 * serial8250_register_port and serial8250_unregister_port allows for
3137 * 16x50 serial ports to be configured at run-time, to support PCMCIA
3138 * modems and PCI multiport cards.
3139 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003140static DEFINE_MUTEX(serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
3142static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
3143{
3144 int i;
3145
3146 /*
3147 * First, find a port entry which matches.
3148 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003149 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 if (uart_match_port(&serial8250_ports[i].port, port))
3151 return &serial8250_ports[i];
3152
3153 /*
3154 * We didn't find a matching entry, so look for the first
3155 * free entry. We look for one which hasn't been previously
3156 * used (indicated by zero iobase).
3157 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003158 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
3160 serial8250_ports[i].port.iobase == 0)
3161 return &serial8250_ports[i];
3162
3163 /*
3164 * That also failed. Last resort is to find any entry which
3165 * doesn't have a real port associated with it.
3166 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003167 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
3169 return &serial8250_ports[i];
3170
3171 return NULL;
3172}
3173
3174/**
3175 * serial8250_register_port - register a serial port
3176 * @port: serial port template
3177 *
3178 * Configure the serial port specified by the request. If the
3179 * port exists and is in use, it is hung up and unregistered
3180 * first.
3181 *
3182 * The port is then probed and if necessary the IRQ is autodetected
3183 * If this fails an error is returned.
3184 *
3185 * On success the port is ready to use and the line number is returned.
3186 */
3187int serial8250_register_port(struct uart_port *port)
3188{
3189 struct uart_8250_port *uart;
3190 int ret = -ENOSPC;
3191
3192 if (port->uartclk == 0)
3193 return -EINVAL;
3194
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003195 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
3197 uart = serial8250_find_match_or_unused(port);
3198 if (uart) {
3199 uart_remove_one_port(&serial8250_reg, &uart->port);
3200
Will Newton74a197412008-02-04 22:27:50 -08003201 uart->port.iobase = port->iobase;
3202 uart->port.membase = port->membase;
3203 uart->port.irq = port->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07003204 uart->port.irqflags = port->irqflags;
Will Newton74a197412008-02-04 22:27:50 -08003205 uart->port.uartclk = port->uartclk;
3206 uart->port.fifosize = port->fifosize;
3207 uart->port.regshift = port->regshift;
3208 uart->port.iotype = port->iotype;
3209 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
3210 uart->port.mapbase = port->mapbase;
3211 uart->port.private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 if (port->dev)
3213 uart->port.dev = port->dev;
David Daney8e23fcc2009-01-02 13:49:54 +00003214
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08003215 if (port->flags & UPF_FIXED_TYPE)
3216 serial8250_init_fixed_type_port(uart, port->type);
David Daney8e23fcc2009-01-02 13:49:54 +00003217
David Daney7d6a07d2009-01-02 13:49:47 +00003218 set_io_from_upio(&uart->port);
3219 /* Possibly override default I/O functions. */
3220 if (port->serial_in)
3221 uart->port.serial_in = port->serial_in;
3222 if (port->serial_out)
3223 uart->port.serial_out = port->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01003224 if (port->handle_irq)
3225 uart->port.handle_irq = port->handle_irq;
Philippe Langlais235dae52010-07-29 17:13:57 +02003226 /* Possibly override set_termios call */
3227 if (port->set_termios)
3228 uart->port.set_termios = port->set_termios;
Manuel Laussc161afe2010-09-25 15:13:45 +02003229 if (port->pm)
3230 uart->port.pm = port->pm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Alan Coxaf7f3742010-10-18 11:38:02 -07003232 if (serial8250_isa_config != NULL)
3233 serial8250_isa_config(0, &uart->port,
3234 &uart->capabilities);
3235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 ret = uart_add_one_port(&serial8250_reg, &uart->port);
3237 if (ret == 0)
3238 ret = uart->port.line;
3239 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003240 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
3242 return ret;
3243}
3244EXPORT_SYMBOL(serial8250_register_port);
3245
3246/**
3247 * serial8250_unregister_port - remove a 16x50 serial port at runtime
3248 * @line: serial line number
3249 *
3250 * Remove one serial port. This may not be called from interrupt
3251 * context. We hand the port back to the our control.
3252 */
3253void serial8250_unregister_port(int line)
3254{
3255 struct uart_8250_port *uart = &serial8250_ports[line];
3256
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003257 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 uart_remove_one_port(&serial8250_reg, &uart->port);
3259 if (serial8250_isa_devs) {
3260 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
3261 uart->port.type = PORT_UNKNOWN;
3262 uart->port.dev = &serial8250_isa_devs->dev;
leitao@linux.vnet.ibm.comcb01ece2011-05-26 11:18:39 -03003263 uart->capabilities = uart_config[uart->port.type].flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 uart_add_one_port(&serial8250_reg, &uart->port);
3265 } else {
3266 uart->port.dev = NULL;
3267 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003268 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269}
3270EXPORT_SYMBOL(serial8250_unregister_port);
3271
3272static int __init serial8250_init(void)
3273{
Alan Cox25db8ad2008-08-19 20:49:40 -07003274 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Dave Jonesa61c2d72006-01-07 23:18:19 +00003276 if (nr_uarts > UART_NR)
3277 nr_uarts = UART_NR;
3278
Paul Bollef1fb9bb2008-12-30 14:06:43 +01003279 printk(KERN_INFO "Serial: 8250/16550 driver, "
Dave Jonesa61c2d72006-01-07 23:18:19 +00003280 "%d ports, IRQ sharing %sabled\n", nr_uarts,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 share_irqs ? "en" : "dis");
3282
David Millerb70ac772008-10-13 10:36:31 +01003283#ifdef CONFIG_SPARC
3284 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
3285#else
3286 serial8250_reg.nr = UART_NR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 ret = uart_register_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003288#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 if (ret)
3290 goto out;
3291
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003292 serial8250_isa_devs = platform_device_alloc("serial8250",
3293 PLAT8250_DEV_LEGACY);
3294 if (!serial8250_isa_devs) {
3295 ret = -ENOMEM;
Russell Kingbc965a72006-01-18 09:54:29 +00003296 goto unreg_uart_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 }
3298
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003299 ret = platform_device_add(serial8250_isa_devs);
3300 if (ret)
3301 goto put_dev;
3302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
3304
Russell Kingbc965a72006-01-18 09:54:29 +00003305 ret = platform_driver_register(&serial8250_isa_driver);
3306 if (ret == 0)
3307 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
Russell Kingbc965a72006-01-18 09:54:29 +00003309 platform_device_del(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003310put_dev:
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003311 platform_device_put(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003312unreg_uart_drv:
David Millerb70ac772008-10-13 10:36:31 +01003313#ifdef CONFIG_SPARC
3314 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3315#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003317#endif
Alan Cox25db8ad2008-08-19 20:49:40 -07003318out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 return ret;
3320}
3321
3322static void __exit serial8250_exit(void)
3323{
3324 struct platform_device *isa_dev = serial8250_isa_devs;
3325
3326 /*
3327 * This tells serial8250_unregister_port() not to re-register
3328 * the ports (thereby making serial8250_isa_driver permanently
3329 * in use.)
3330 */
3331 serial8250_isa_devs = NULL;
3332
Russell King3ae5eae2005-11-09 22:32:44 +00003333 platform_driver_unregister(&serial8250_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 platform_device_unregister(isa_dev);
3335
David Millerb70ac772008-10-13 10:36:31 +01003336#ifdef CONFIG_SPARC
3337 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3338#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003340#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341}
3342
3343module_init(serial8250_init);
3344module_exit(serial8250_exit);
3345
3346EXPORT_SYMBOL(serial8250_suspend_port);
3347EXPORT_SYMBOL(serial8250_resume_port);
3348
3349MODULE_LICENSE("GPL");
Adrian Bunkd87a6d92008-07-16 21:53:31 +01003350MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
3352module_param(share_irqs, uint, 0644);
3353MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
3354 " (unsafe)");
3355
Dave Jonesa61c2d72006-01-07 23:18:19 +00003356module_param(nr_uarts, uint, 0644);
3357MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
3358
Chuck Ebbertd41a4b52009-10-01 15:44:26 -07003359module_param(skip_txen_test, uint, 0644);
3360MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
3361
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362#ifdef CONFIG_SERIAL_8250_RSA
3363module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
3364MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
3365#endif
3366MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);