blob: a0d114d8baefe0d090be89710a49cd9161f87978 [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>
Paul Gortmaker68163832012-02-09 18:48:19 -050041#ifdef CONFIG_SPARC
42#include <linux/sunserialcore.h>
43#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/io.h>
46#include <asm/irq.h>
47
48#include "8250.h"
49
50/*
51 * Configuration:
Thomas Gleixner40663cc2006-07-01 19:29:43 -070052 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * is unsafe when used on edge-triggered interrupts.
54 */
Adrian Bunk408b6642005-05-01 08:59:29 -070055static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Dave Jonesa61c2d72006-01-07 23:18:19 +000057static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
58
David S. Miller84408382008-10-13 10:45:26 +010059static struct uart_driver serial8250_reg;
60
61static int serial_index(struct uart_port *port)
62{
63 return (serial8250_reg.minor - 64) + port->line;
64}
65
Chuck Ebbertd41a4b52009-10-01 15:44:26 -070066static unsigned int skip_txen_test; /* force skip of txen test at init time */
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
69 * Debugging.
70 */
71#if 0
72#define DEBUG_AUTOCONF(fmt...) printk(fmt)
73#else
74#define DEBUG_AUTOCONF(fmt...) do { } while (0)
75#endif
76
77#if 0
78#define DEBUG_INTR(fmt...) printk(fmt)
79#else
80#define DEBUG_INTR(fmt...) do { } while (0)
81#endif
82
Jiri Slabye7328ae2011-06-05 22:51:49 +020083#define PASS_LIMIT 512
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Dick Hollenbeckbca47612009-12-09 12:31:34 -080085#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
86
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
89#define CONFIG_SERIAL_DETECT_IRQ 1
90#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#ifdef CONFIG_SERIAL_8250_MANY_PORTS
92#define CONFIG_SERIAL_MANY_PORTS 1
93#endif
94
95/*
96 * HUB6 is always on. This will be removed once the header
97 * files have been cleaned.
98 */
99#define CONFIG_HUB6 1
100
Bryan Wua4ed1e42008-05-31 16:10:04 +0800101#include <asm/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/*
103 * SERIAL_PORT_DFNS tells us about built-in ports that have no
104 * standard enumeration mechanism. Platforms that can find all
105 * serial ports via mechanisms like ACPI or PCI need not supply it.
106 */
107#ifndef SERIAL_PORT_DFNS
108#define SERIAL_PORT_DFNS
109#endif
110
Arjan van de Vencb3592b2005-11-28 21:04:11 +0000111static const struct old_serial_port old_serial_port[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 SERIAL_PORT_DFNS /* defined in asm/serial.h */
113};
114
Russell King026d02a2005-06-29 18:45:19 +0100115#define UART_NR CONFIG_SERIAL_8250_NR_UARTS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117#ifdef CONFIG_SERIAL_8250_RSA
118
119#define PORT_RSA_MAX 4
120static unsigned long probe_rsa[PORT_RSA_MAX];
121static unsigned int probe_rsa_count;
122#endif /* CONFIG_SERIAL_8250_RSA */
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124struct irq_info {
Alan Cox25db8ad2008-08-19 20:49:40 -0700125 struct hlist_node node;
126 int irq;
127 spinlock_t lock; /* Protects list not the hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 struct list_head *head;
129};
130
Alan Cox25db8ad2008-08-19 20:49:40 -0700131#define NR_IRQ_HASH 32 /* Can be adjusted later */
132static struct hlist_head irq_lists[NR_IRQ_HASH];
133static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/*
136 * Here we define the default xmit fifo size used for each type of UART.
137 */
138static const struct serial8250_config uart_config[] = {
139 [PORT_UNKNOWN] = {
140 .name = "unknown",
141 .fifo_size = 1,
142 .tx_loadsz = 1,
143 },
144 [PORT_8250] = {
145 .name = "8250",
146 .fifo_size = 1,
147 .tx_loadsz = 1,
148 },
149 [PORT_16450] = {
150 .name = "16450",
151 .fifo_size = 1,
152 .tx_loadsz = 1,
153 },
154 [PORT_16550] = {
155 .name = "16550",
156 .fifo_size = 1,
157 .tx_loadsz = 1,
158 },
159 [PORT_16550A] = {
160 .name = "16550A",
161 .fifo_size = 16,
162 .tx_loadsz = 16,
163 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
164 .flags = UART_CAP_FIFO,
165 },
166 [PORT_CIRRUS] = {
167 .name = "Cirrus",
168 .fifo_size = 1,
169 .tx_loadsz = 1,
170 },
171 [PORT_16650] = {
172 .name = "ST16650",
173 .fifo_size = 1,
174 .tx_loadsz = 1,
175 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
176 },
177 [PORT_16650V2] = {
178 .name = "ST16650V2",
179 .fifo_size = 32,
180 .tx_loadsz = 16,
181 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
182 UART_FCR_T_TRIG_00,
183 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
184 },
185 [PORT_16750] = {
186 .name = "TI16750",
187 .fifo_size = 64,
188 .tx_loadsz = 64,
189 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
190 UART_FCR7_64BYTE,
191 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
192 },
193 [PORT_STARTECH] = {
194 .name = "Startech",
195 .fifo_size = 1,
196 .tx_loadsz = 1,
197 },
198 [PORT_16C950] = {
199 .name = "16C950/954",
200 .fifo_size = 128,
201 .tx_loadsz = 128,
202 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
Pavel Machekd0694e22011-01-09 08:38:48 +0100203 /* UART_CAP_EFR breaks billionon CF bluetooth card. */
204 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 },
206 [PORT_16654] = {
207 .name = "ST16654",
208 .fifo_size = 64,
209 .tx_loadsz = 32,
210 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
211 UART_FCR_T_TRIG_10,
212 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
213 },
214 [PORT_16850] = {
215 .name = "XR16850",
216 .fifo_size = 128,
217 .tx_loadsz = 128,
218 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
219 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
220 },
221 [PORT_RSA] = {
222 .name = "RSA",
223 .fifo_size = 2048,
224 .tx_loadsz = 2048,
225 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
226 .flags = UART_CAP_FIFO,
227 },
228 [PORT_NS16550A] = {
229 .name = "NS16550A",
230 .fifo_size = 16,
231 .tx_loadsz = 16,
232 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
233 .flags = UART_CAP_FIFO | UART_NATSEMI,
234 },
235 [PORT_XSCALE] = {
236 .name = "XScale",
237 .fifo_size = 32,
238 .tx_loadsz = 32,
239 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
Stephen Warren4539c242011-05-17 16:12:36 -0600240 .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 },
Thomas Koellerbd71c182007-05-06 14:48:47 -0700242 [PORT_RM9000] = {
243 .name = "RM9000",
244 .fifo_size = 16,
245 .tx_loadsz = 16,
246 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
247 .flags = UART_CAP_FIFO,
248 },
David Daney6b06f192009-01-02 13:50:00 +0000249 [PORT_OCTEON] = {
250 .name = "OCTEON",
251 .fifo_size = 64,
252 .tx_loadsz = 64,
253 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
254 .flags = UART_CAP_FIFO,
255 },
Florian Fainelli08e09922009-06-11 13:21:24 +0100256 [PORT_AR7] = {
257 .name = "AR7",
258 .fifo_size = 16,
259 .tx_loadsz = 16,
260 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
261 .flags = UART_CAP_FIFO | UART_CAP_AFE,
262 },
Philippe Langlais235dae52010-07-29 17:13:57 +0200263 [PORT_U6_16550A] = {
264 .name = "U6_16550A",
265 .fifo_size = 64,
266 .tx_loadsz = 64,
267 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
268 .flags = UART_CAP_FIFO | UART_CAP_AFE,
269 },
Stephen Warren4539c242011-05-17 16:12:36 -0600270 [PORT_TEGRA] = {
271 .name = "Tegra",
272 .fifo_size = 32,
273 .tx_loadsz = 8,
274 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
275 UART_FCR_T_TRIG_01,
276 .flags = UART_CAP_FIFO | UART_CAP_RTOIE,
277 },
Søren Holm06315342011-09-02 22:55:37 +0200278 [PORT_XR17D15X] = {
279 .name = "XR17D15X",
280 .fifo_size = 64,
281 .tx_loadsz = 64,
282 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
283 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR,
284 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285};
286
Manuel Lauss12bf3f22010-07-15 21:45:05 +0200287#if defined(CONFIG_MIPS_ALCHEMY)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000288
289/* Au1x00 UART hardware has a weird register layout */
290static const u8 au_io_in_map[] = {
291 [UART_RX] = 0,
292 [UART_IER] = 2,
293 [UART_IIR] = 3,
294 [UART_LCR] = 5,
295 [UART_MCR] = 6,
296 [UART_LSR] = 7,
297 [UART_MSR] = 8,
298};
299
300static const u8 au_io_out_map[] = {
301 [UART_TX] = 1,
302 [UART_IER] = 2,
303 [UART_FCR] = 4,
304 [UART_LCR] = 5,
305 [UART_MCR] = 6,
306};
307
308/* sane hardware needs no mapping */
David Daney7d6a07d2009-01-02 13:49:47 +0000309static inline int map_8250_in_reg(struct uart_port *p, int offset)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000310{
David Daney7d6a07d2009-01-02 13:49:47 +0000311 if (p->iotype != UPIO_AU)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000312 return offset;
313 return au_io_in_map[offset];
314}
315
David Daney7d6a07d2009-01-02 13:49:47 +0000316static inline int map_8250_out_reg(struct uart_port *p, int offset)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000317{
David Daney7d6a07d2009-01-02 13:49:47 +0000318 if (p->iotype != UPIO_AU)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000319 return offset;
320 return au_io_out_map[offset];
321}
322
Alan Cox6f803cd2008-02-08 04:18:52 -0800323#elif defined(CONFIG_SERIAL_8250_RM9K)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700324
325static const u8
326 regmap_in[8] = {
327 [UART_RX] = 0x00,
328 [UART_IER] = 0x0c,
329 [UART_IIR] = 0x14,
330 [UART_LCR] = 0x1c,
331 [UART_MCR] = 0x20,
332 [UART_LSR] = 0x24,
333 [UART_MSR] = 0x28,
334 [UART_SCR] = 0x2c
335 },
336 regmap_out[8] = {
337 [UART_TX] = 0x04,
338 [UART_IER] = 0x0c,
339 [UART_FCR] = 0x18,
340 [UART_LCR] = 0x1c,
341 [UART_MCR] = 0x20,
342 [UART_LSR] = 0x24,
343 [UART_MSR] = 0x28,
344 [UART_SCR] = 0x2c
345 };
346
David Daney7d6a07d2009-01-02 13:49:47 +0000347static inline int map_8250_in_reg(struct uart_port *p, int offset)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700348{
David Daney7d6a07d2009-01-02 13:49:47 +0000349 if (p->iotype != UPIO_RM9000)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700350 return offset;
351 return regmap_in[offset];
352}
353
David Daney7d6a07d2009-01-02 13:49:47 +0000354static inline int map_8250_out_reg(struct uart_port *p, int offset)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700355{
David Daney7d6a07d2009-01-02 13:49:47 +0000356 if (p->iotype != UPIO_RM9000)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700357 return offset;
358 return regmap_out[offset];
359}
360
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000361#else
362
363/* sane hardware needs no mapping */
364#define map_8250_in_reg(up, offset) (offset)
365#define map_8250_out_reg(up, offset) (offset)
366
367#endif
368
David Daney7d6a07d2009-01-02 13:49:47 +0000369static unsigned int hub6_serial_in(struct uart_port *p, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
David Daney7d6a07d2009-01-02 13:49:47 +0000371 offset = map_8250_in_reg(p, offset) << p->regshift;
372 outb(p->hub6 - 1 + offset, p->iobase);
373 return inb(p->iobase + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
David Daney7d6a07d2009-01-02 13:49:47 +0000376static void hub6_serial_out(struct uart_port *p, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
David Daney7d6a07d2009-01-02 13:49:47 +0000378 offset = map_8250_out_reg(p, offset) << p->regshift;
379 outb(p->hub6 - 1 + offset, p->iobase);
380 outb(value, p->iobase + 1);
381}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
David Daney7d6a07d2009-01-02 13:49:47 +0000383static unsigned int mem_serial_in(struct uart_port *p, int offset)
384{
385 offset = map_8250_in_reg(p, offset) << p->regshift;
386 return readb(p->membase + offset);
387}
388
389static void mem_serial_out(struct uart_port *p, int offset, int value)
390{
391 offset = map_8250_out_reg(p, offset) << p->regshift;
392 writeb(value, p->membase + offset);
393}
394
395static void mem32_serial_out(struct uart_port *p, int offset, int value)
396{
397 offset = map_8250_out_reg(p, offset) << p->regshift;
398 writel(value, p->membase + offset);
399}
400
401static unsigned int mem32_serial_in(struct uart_port *p, int offset)
402{
403 offset = map_8250_in_reg(p, offset) << p->regshift;
404 return readl(p->membase + offset);
405}
406
David Daney7d6a07d2009-01-02 13:49:47 +0000407static unsigned int au_serial_in(struct uart_port *p, int offset)
408{
409 offset = map_8250_in_reg(p, offset) << p->regshift;
410 return __raw_readl(p->membase + offset);
411}
412
413static void au_serial_out(struct uart_port *p, int offset, int value)
414{
415 offset = map_8250_out_reg(p, offset) << p->regshift;
416 __raw_writel(value, p->membase + offset);
417}
David Daney7d6a07d2009-01-02 13:49:47 +0000418
David Daney7d6a07d2009-01-02 13:49:47 +0000419static unsigned int io_serial_in(struct uart_port *p, int offset)
420{
421 offset = map_8250_in_reg(p, offset) << p->regshift;
422 return inb(p->iobase + offset);
423}
424
425static void io_serial_out(struct uart_port *p, int offset, int value)
426{
427 offset = map_8250_out_reg(p, offset) << p->regshift;
428 outb(value, p->iobase + offset);
429}
430
Jamie Iles583d28e2011-08-15 10:17:52 +0100431static int serial8250_default_handle_irq(struct uart_port *port);
432
David Daney7d6a07d2009-01-02 13:49:47 +0000433static void set_io_from_upio(struct uart_port *p)
434{
Jamie Iles49d57412010-12-01 23:39:35 +0000435 struct uart_8250_port *up =
436 container_of(p, struct uart_8250_port, port);
David Daney7d6a07d2009-01-02 13:49:47 +0000437 switch (p->iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 case UPIO_HUB6:
David Daney7d6a07d2009-01-02 13:49:47 +0000439 p->serial_in = hub6_serial_in;
440 p->serial_out = hub6_serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 break;
442
443 case UPIO_MEM:
David Daney7d6a07d2009-01-02 13:49:47 +0000444 p->serial_in = mem_serial_in;
445 p->serial_out = mem_serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 break;
447
Thomas Koellerbd71c182007-05-06 14:48:47 -0700448 case UPIO_RM9000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 case UPIO_MEM32:
David Daney7d6a07d2009-01-02 13:49:47 +0000450 p->serial_in = mem32_serial_in;
451 p->serial_out = mem32_serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
453
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000454 case UPIO_AU:
David Daney7d6a07d2009-01-02 13:49:47 +0000455 p->serial_in = au_serial_in;
456 p->serial_out = au_serial_out;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000457 break;
Manuel Lauss12bf3f22010-07-15 21:45:05 +0200458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 default:
David Daney7d6a07d2009-01-02 13:49:47 +0000460 p->serial_in = io_serial_in;
461 p->serial_out = io_serial_out;
462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
Alan Coxb8e7e402009-05-28 14:01:35 +0100464 /* Remember loaded iotype */
465 up->cur_iotype = p->iotype;
Jamie Iles583d28e2011-08-15 10:17:52 +0100466 p->handle_irq = serial8250_default_handle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Alex Williamson40b36da2007-02-14 00:33:04 -0800469static void
470serial_out_sync(struct uart_8250_port *up, int offset, int value)
471{
David Daney7d6a07d2009-01-02 13:49:47 +0000472 struct uart_port *p = &up->port;
473 switch (p->iotype) {
Alex Williamson40b36da2007-02-14 00:33:04 -0800474 case UPIO_MEM:
475 case UPIO_MEM32:
Alex Williamson40b36da2007-02-14 00:33:04 -0800476 case UPIO_AU:
David Daney7d6a07d2009-01-02 13:49:47 +0000477 p->serial_out(p, offset, value);
478 p->serial_in(p, UART_LCR); /* safe, no side-effects */
Alex Williamson40b36da2007-02-14 00:33:04 -0800479 break;
480 default:
David Daney7d6a07d2009-01-02 13:49:47 +0000481 p->serial_out(p, offset, value);
Alex Williamson40b36da2007-02-14 00:33:04 -0800482 }
483}
484
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100485/* Uart divisor latch read */
486static inline int _serial_dl_read(struct uart_8250_port *up)
487{
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500488 return serial_in(up, UART_DLL) | serial_in(up, UART_DLM) << 8;
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100489}
490
491/* Uart divisor latch write */
492static inline void _serial_dl_write(struct uart_8250_port *up, int value)
493{
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500494 serial_out(up, UART_DLL, value & 0xff);
495 serial_out(up, UART_DLM, value >> 8 & 0xff);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100496}
497
Manuel Lauss12bf3f22010-07-15 21:45:05 +0200498#if defined(CONFIG_MIPS_ALCHEMY)
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100499/* Au1x00 haven't got a standard divisor latch */
500static int serial_dl_read(struct uart_8250_port *up)
501{
502 if (up->port.iotype == UPIO_AU)
503 return __raw_readl(up->port.membase + 0x28);
504 else
505 return _serial_dl_read(up);
506}
507
508static void serial_dl_write(struct uart_8250_port *up, int value)
509{
510 if (up->port.iotype == UPIO_AU)
511 __raw_writel(value, up->port.membase + 0x28);
512 else
513 _serial_dl_write(up, value);
514}
Alan Cox6f803cd2008-02-08 04:18:52 -0800515#elif defined(CONFIG_SERIAL_8250_RM9K)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700516static int serial_dl_read(struct uart_8250_port *up)
517{
518 return (up->port.iotype == UPIO_RM9000) ?
519 (((__raw_readl(up->port.membase + 0x10) << 8) |
520 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
521 _serial_dl_read(up);
522}
523
524static void serial_dl_write(struct uart_8250_port *up, int value)
525{
526 if (up->port.iotype == UPIO_RM9000) {
527 __raw_writel(value, up->port.membase + 0x08);
528 __raw_writel(value >> 8, up->port.membase + 0x10);
529 } else {
530 _serial_dl_write(up, value);
531 }
532}
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100533#else
534#define serial_dl_read(up) _serial_dl_read(up)
535#define serial_dl_write(up, value) _serial_dl_write(up, value)
536#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538/*
539 * For the 16C950
540 */
541static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
542{
543 serial_out(up, UART_SCR, offset);
544 serial_out(up, UART_ICR, value);
545}
546
547static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
548{
549 unsigned int value;
550
551 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
552 serial_out(up, UART_SCR, offset);
553 value = serial_in(up, UART_ICR);
554 serial_icr_write(up, UART_ACR, up->acr);
555
556 return value;
557}
558
559/*
560 * FIFO support.
561 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100562static void serial8250_clear_fifos(struct uart_8250_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 if (p->capabilities & UART_CAP_FIFO) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500565 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
566 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500568 serial_out(p, UART_FCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570}
571
572/*
573 * IER sleep support. UARTs which have EFRs need the "extended
574 * capability" bit enabled. Note that on XR16C850s, we need to
575 * reset LCR to write to IER.
576 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100577static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 if (p->capabilities & UART_CAP_SLEEP) {
580 if (p->capabilities & UART_CAP_EFR) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500581 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
582 serial_out(p, UART_EFR, UART_EFR_ECB);
583 serial_out(p, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500585 serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (p->capabilities & UART_CAP_EFR) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500587 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
588 serial_out(p, UART_EFR, 0);
589 serial_out(p, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591 }
592}
593
594#ifdef CONFIG_SERIAL_8250_RSA
595/*
596 * Attempts to turn on the RSA FIFO. Returns zero on failure.
597 * We set the port uart clock rate if we succeed.
598 */
599static int __enable_rsa(struct uart_8250_port *up)
600{
601 unsigned char mode;
602 int result;
603
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500604 mode = serial_in(up, UART_RSA_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 result = mode & UART_RSA_MSR_FIFO;
606
607 if (!result) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500608 serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
609 mode = serial_in(up, UART_RSA_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 result = mode & UART_RSA_MSR_FIFO;
611 }
612
613 if (result)
614 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
615
616 return result;
617}
618
619static void enable_rsa(struct uart_8250_port *up)
620{
621 if (up->port.type == PORT_RSA) {
622 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
623 spin_lock_irq(&up->port.lock);
624 __enable_rsa(up);
625 spin_unlock_irq(&up->port.lock);
626 }
627 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500628 serial_out(up, UART_RSA_FRR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630}
631
632/*
633 * Attempts to turn off the RSA FIFO. Returns zero on failure.
634 * It is unknown why interrupts were disabled in here. However,
635 * the caller is expected to preserve this behaviour by grabbing
636 * the spinlock before calling this function.
637 */
638static void disable_rsa(struct uart_8250_port *up)
639{
640 unsigned char mode;
641 int result;
642
643 if (up->port.type == PORT_RSA &&
644 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
645 spin_lock_irq(&up->port.lock);
646
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500647 mode = serial_in(up, UART_RSA_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 result = !(mode & UART_RSA_MSR_FIFO);
649
650 if (!result) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500651 serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
652 mode = serial_in(up, UART_RSA_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 result = !(mode & UART_RSA_MSR_FIFO);
654 }
655
656 if (result)
657 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
658 spin_unlock_irq(&up->port.lock);
659 }
660}
661#endif /* CONFIG_SERIAL_8250_RSA */
662
663/*
664 * This is a quickie test to see how big the FIFO is.
665 * It doesn't work at all the time, more's the pity.
666 */
667static int size_fifo(struct uart_8250_port *up)
668{
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100669 unsigned char old_fcr, old_mcr, old_lcr;
670 unsigned short old_dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 int count;
672
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500673 old_lcr = serial_in(up, UART_LCR);
674 serial_out(up, UART_LCR, 0);
675 old_fcr = serial_in(up, UART_FCR);
676 old_mcr = serial_in(up, UART_MCR);
677 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500679 serial_out(up, UART_MCR, UART_MCR_LOOP);
680 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100681 old_dl = serial_dl_read(up);
682 serial_dl_write(up, 0x0001);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500683 serial_out(up, UART_LCR, 0x03);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 for (count = 0; count < 256; count++)
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500685 serial_out(up, UART_TX, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 mdelay(20);/* FIXME - schedule_timeout */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500687 for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 (count < 256); count++)
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500689 serial_in(up, UART_RX);
690 serial_out(up, UART_FCR, old_fcr);
691 serial_out(up, UART_MCR, old_mcr);
692 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100693 serial_dl_write(up, old_dl);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500694 serial_out(up, UART_LCR, old_lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 return count;
697}
698
699/*
700 * Read UART ID using the divisor method - set DLL and DLM to zero
701 * and the revision will be in DLL and device type in DLM. We
702 * preserve the device state across this.
703 */
704static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
705{
706 unsigned char old_dll, old_dlm, old_lcr;
707 unsigned int id;
708
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500709 old_lcr = serial_in(p, UART_LCR);
710 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500712 old_dll = serial_in(p, UART_DLL);
713 old_dlm = serial_in(p, UART_DLM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500715 serial_out(p, UART_DLL, 0);
716 serial_out(p, UART_DLM, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500718 id = serial_in(p, UART_DLL) | serial_in(p, UART_DLM) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500720 serial_out(p, UART_DLL, old_dll);
721 serial_out(p, UART_DLM, old_dlm);
722 serial_out(p, UART_LCR, old_lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 return id;
725}
726
727/*
728 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
729 * When this function is called we know it is at least a StarTech
730 * 16650 V2, but it might be one of several StarTech UARTs, or one of
731 * its clones. (We treat the broken original StarTech 16650 V1 as a
732 * 16550, and why not? Startech doesn't seem to even acknowledge its
733 * existence.)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700734 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * What evil have men's minds wrought...
736 */
737static void autoconfig_has_efr(struct uart_8250_port *up)
738{
739 unsigned int id1, id2, id3, rev;
740
741 /*
742 * Everything with an EFR has SLEEP
743 */
744 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
745
746 /*
747 * First we check to see if it's an Oxford Semiconductor UART.
748 *
749 * If we have to do this here because some non-National
750 * Semiconductor clone chips lock up if you try writing to the
751 * LSR register (which serial_icr_read does)
752 */
753
754 /*
755 * Check for Oxford Semiconductor 16C950.
756 *
757 * EFR [4] must be set else this test fails.
758 *
759 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
760 * claims that it's needed for 952 dual UART's (which are not
761 * recommended for new designs).
762 */
763 up->acr = 0;
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800764 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 serial_out(up, UART_EFR, UART_EFR_ECB);
766 serial_out(up, UART_LCR, 0x00);
767 id1 = serial_icr_read(up, UART_ID1);
768 id2 = serial_icr_read(up, UART_ID2);
769 id3 = serial_icr_read(up, UART_ID3);
770 rev = serial_icr_read(up, UART_REV);
771
772 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
773
774 if (id1 == 0x16 && id2 == 0xC9 &&
775 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
776 up->port.type = PORT_16C950;
Russell King4ba5e352005-06-23 10:43:04 +0100777
778 /*
779 * Enable work around for the Oxford Semiconductor 952 rev B
780 * chip which causes it to seriously miscalculate baud rates
781 * when DLL is 0.
782 */
783 if (id3 == 0x52 && rev == 0x01)
784 up->bugs |= UART_BUG_QUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return;
786 }
Thomas Koellerbd71c182007-05-06 14:48:47 -0700787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 /*
789 * We check for a XR16C850 by setting DLL and DLM to 0, and then
790 * reading back DLL and DLM. The chip type depends on the DLM
791 * value read back:
792 * 0x10 - XR16C850 and the DLL contains the chip revision.
793 * 0x12 - XR16C2850.
794 * 0x14 - XR16C854.
795 */
796 id1 = autoconfig_read_divisor_id(up);
797 DEBUG_AUTOCONF("850id=%04x ", id1);
798
799 id2 = id1 >> 8;
800 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 up->port.type = PORT_16850;
802 return;
803 }
804
805 /*
806 * It wasn't an XR16C850.
807 *
808 * We distinguish between the '654 and the '650 by counting
809 * how many bytes are in the FIFO. I'm using this for now,
810 * since that's the technique that was sent to me in the
811 * serial driver update, but I'm not convinced this works.
812 * I've had problems doing this in the past. -TYT
813 */
814 if (size_fifo(up) == 64)
815 up->port.type = PORT_16654;
816 else
817 up->port.type = PORT_16650V2;
818}
819
820/*
821 * We detected a chip without a FIFO. Only two fall into
822 * this category - the original 8250 and the 16450. The
823 * 16450 has a scratch register (accessible with LCR=0)
824 */
825static void autoconfig_8250(struct uart_8250_port *up)
826{
827 unsigned char scratch, status1, status2;
828
829 up->port.type = PORT_8250;
830
831 scratch = serial_in(up, UART_SCR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500832 serial_out(up, UART_SCR, 0xa5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 status1 = serial_in(up, UART_SCR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500834 serial_out(up, UART_SCR, 0x5a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 status2 = serial_in(up, UART_SCR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500836 serial_out(up, UART_SCR, scratch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 if (status1 == 0xa5 && status2 == 0x5a)
839 up->port.type = PORT_16450;
840}
841
842static int broken_efr(struct uart_8250_port *up)
843{
844 /*
845 * Exar ST16C2550 "A2" devices incorrectly detect as
846 * having an EFR, and report an ID of 0x0201. See
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200847 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 */
849 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
850 return 1;
851
852 return 0;
853}
854
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800855static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
856{
857 unsigned char status;
858
859 status = serial_in(up, 0x04); /* EXCR2 */
860#define PRESL(x) ((x) & 0x30)
861 if (PRESL(status) == 0x10) {
862 /* already in high speed mode */
863 return 0;
864 } else {
865 status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
866 status |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500867 serial_out(up, 0x04, status);
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800868 }
869 return 1;
870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872/*
873 * We know that the chip has FIFOs. Does it have an EFR? The
874 * EFR is located in the same register position as the IIR and
875 * we know the top two bits of the IIR are currently set. The
876 * EFR should contain zero. Try to read the EFR.
877 */
878static void autoconfig_16550a(struct uart_8250_port *up)
879{
880 unsigned char status1, status2;
881 unsigned int iersave;
882
883 up->port.type = PORT_16550A;
884 up->capabilities |= UART_CAP_FIFO;
885
886 /*
887 * Check for presence of the EFR when DLAB is set.
888 * Only ST16C650V1 UARTs pass this test.
889 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500890 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (serial_in(up, UART_EFR) == 0) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500892 serial_out(up, UART_EFR, 0xA8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (serial_in(up, UART_EFR) != 0) {
894 DEBUG_AUTOCONF("EFRv1 ");
895 up->port.type = PORT_16650;
896 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
897 } else {
898 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
899 }
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500900 serial_out(up, UART_EFR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return;
902 }
903
904 /*
905 * Maybe it requires 0xbf to be written to the LCR.
906 * (other ST16C650V2 UARTs, TI16C752A, etc)
907 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500908 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
910 DEBUG_AUTOCONF("EFRv2 ");
911 autoconfig_has_efr(up);
912 return;
913 }
914
915 /*
916 * Check for a National Semiconductor SuperIO chip.
917 * Attempt to switch to bank 2, read the value of the LOOP bit
918 * from EXCR1. Switch back to bank 0, change it in MCR. Then
919 * switch back to bank 2, read it from EXCR1 again and check
920 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500922 serial_out(up, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 status1 = serial_in(up, UART_MCR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500924 serial_out(up, UART_LCR, 0xE0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 status2 = serial_in(up, 0x02); /* EXCR1 */
926
927 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500928 serial_out(up, UART_LCR, 0);
929 serial_out(up, UART_MCR, status1 ^ UART_MCR_LOOP);
930 serial_out(up, UART_LCR, 0xE0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 status2 = serial_in(up, 0x02); /* EXCR1 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500932 serial_out(up, UART_LCR, 0);
933 serial_out(up, UART_MCR, status1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 if ((status2 ^ status1) & UART_MCR_LOOP) {
David Woodhouse857dde22005-05-21 15:52:23 +0100936 unsigned short quot;
937
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500938 serial_out(up, UART_LCR, 0xE0);
David Woodhouse857dde22005-05-21 15:52:23 +0100939
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100940 quot = serial_dl_read(up);
David Woodhouse857dde22005-05-21 15:52:23 +0100941 quot <<= 3;
942
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800943 if (ns16550a_goto_highspeed(up))
944 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500946 serial_out(up, UART_LCR, 0);
David Woodhouse857dde22005-05-21 15:52:23 +0100947
948 up->port.uartclk = 921600*16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 up->port.type = PORT_NS16550A;
950 up->capabilities |= UART_NATSEMI;
951 return;
952 }
953 }
954
955 /*
956 * No EFR. Try to detect a TI16750, which only sets bit 5 of
957 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
958 * Try setting it with and without DLAB set. Cheap clones
959 * set bit 5 without DLAB set.
960 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500961 serial_out(up, UART_LCR, 0);
962 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 status1 = serial_in(up, UART_IIR) >> 5;
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500964 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
965 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
966 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 status2 = serial_in(up, UART_IIR) >> 5;
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500968 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
969 serial_out(up, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
972
973 if (status1 == 6 && status2 == 7) {
974 up->port.type = PORT_16750;
975 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
976 return;
977 }
978
979 /*
980 * Try writing and reading the UART_IER_UUE bit (b6).
981 * If it works, this is probably one of the Xscale platform's
982 * internal UARTs.
983 * We're going to explicitly set the UUE bit to 0 before
984 * trying to write and read a 1 just to make sure it's not
985 * already a 1 and maybe locked there before we even start start.
986 */
987 iersave = serial_in(up, UART_IER);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500988 serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
990 /*
991 * OK it's in a known zero state, try writing and reading
992 * without disturbing the current state of the other bits.
993 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500994 serial_out(up, UART_IER, iersave | UART_IER_UUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (serial_in(up, UART_IER) & UART_IER_UUE) {
996 /*
997 * It's an Xscale.
998 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
999 */
1000 DEBUG_AUTOCONF("Xscale ");
1001 up->port.type = PORT_XSCALE;
Stephen Warren55681812011-06-17 09:45:07 -06001002 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return;
1004 }
1005 } else {
1006 /*
1007 * If we got here we couldn't force the IER_UUE bit to 0.
1008 * Log it and continue.
1009 */
1010 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1011 }
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001012 serial_out(up, UART_IER, iersave);
Philippe Langlais235dae52010-07-29 17:13:57 +02001013
1014 /*
Søren Holm06315342011-09-02 22:55:37 +02001015 * Exar uarts have EFR in a weird location
1016 */
1017 if (up->port.flags & UPF_EXAR_EFR) {
1018 up->port.type = PORT_XR17D15X;
1019 up->capabilities |= UART_CAP_AFE | UART_CAP_EFR;
1020 }
1021
1022 /*
Philippe Langlais235dae52010-07-29 17:13:57 +02001023 * We distinguish between 16550A and U6 16550A by counting
1024 * how many bytes are in the FIFO.
1025 */
1026 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1027 up->port.type = PORT_U6_16550A;
1028 up->capabilities |= UART_CAP_AFE;
1029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032/*
1033 * This routine is called by rs_init() to initialize a specific serial
1034 * port. It determines what type of UART chip this serial port is
1035 * using: 8250, 16450, 16550, 16550A. The important question is
1036 * whether or not this UART is a 16550A or not, since this will
1037 * determine whether or not we can use its FIFO features or not.
1038 */
1039static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
1040{
1041 unsigned char status1, scratch, scratch2, scratch3;
1042 unsigned char save_lcr, save_mcr;
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001043 struct uart_port *port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 unsigned long flags;
1045
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001046 if (!port->iobase && !port->mapbase && !port->membase)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return;
1048
Lennert Buytenhek80647b92009-11-11 14:26:41 -08001049 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001050 serial_index(port), port->iobase, port->membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 /*
1053 * We really do need global IRQs disabled here - we're going to
1054 * be frobbing the chips IRQ enable register to see if it exists.
1055 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001056 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 up->capabilities = 0;
Russell King4ba5e352005-06-23 10:43:04 +01001059 up->bugs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001061 if (!(port->flags & UPF_BUGGY_UART)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /*
1063 * Do a simple existence test first; if we fail this,
1064 * there's no point trying anything else.
Thomas Koellerbd71c182007-05-06 14:48:47 -07001065 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 * 0x80 is used as a nonsense port to prevent against
1067 * false positives due to ISA bus float. The
1068 * assumption is that 0x80 is a non-existent port;
1069 * which should be safe since include/asm/io.h also
1070 * makes this assumption.
1071 *
1072 * Note: this is safe as long as MCR bit 4 is clear
1073 * and the device is in "PC" mode.
1074 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001075 scratch = serial_in(up, UART_IER);
1076 serial_out(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077#ifdef __i386__
1078 outb(0xff, 0x080);
1079#endif
Thomas Hoehn48212002007-02-10 01:46:05 -08001080 /*
1081 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1082 * 16C754B) allow only to modify them if an EFR bit is set.
1083 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001084 scratch2 = serial_in(up, UART_IER) & 0x0f;
1085 serial_out(up, UART_IER, 0x0F);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086#ifdef __i386__
1087 outb(0, 0x080);
1088#endif
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001089 scratch3 = serial_in(up, UART_IER) & 0x0f;
1090 serial_out(up, UART_IER, scratch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (scratch2 != 0 || scratch3 != 0x0F) {
1092 /*
1093 * We failed; there's nothing here
1094 */
1095 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1096 scratch2, scratch3);
1097 goto out;
1098 }
1099 }
1100
1101 save_mcr = serial_in(up, UART_MCR);
1102 save_lcr = serial_in(up, UART_LCR);
1103
Thomas Koellerbd71c182007-05-06 14:48:47 -07001104 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 * Check to see if a UART is really there. Certain broken
1106 * internal modems based on the Rockwell chipset fail this
1107 * test, because they apparently don't implement the loopback
1108 * test mode. So this test is skipped on the COM 1 through
1109 * COM 4 ports. This *should* be safe, since no board
1110 * manufacturer would be stupid enough to design a board
1111 * that conflicts with COM 1-4 --- we hope!
1112 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001113 if (!(port->flags & UPF_SKIP_TEST)) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001114 serial_out(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1115 status1 = serial_in(up, UART_MSR) & 0xF0;
1116 serial_out(up, UART_MCR, save_mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (status1 != 0x90) {
1118 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1119 status1);
1120 goto out;
1121 }
1122 }
1123
1124 /*
1125 * We're pretty sure there's a port here. Lets find out what
1126 * type of port it is. The IIR top two bits allows us to find
Russell King6f0d6182005-09-09 16:17:58 +01001127 * out if it's 8250 or 16450, 16550, 16550A or later. This
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 * determines what we test for next.
1129 *
1130 * We also initialise the EFR (if any) to zero for later. The
1131 * EFR occupies the same register location as the FCR and IIR.
1132 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001133 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1134 serial_out(up, UART_EFR, 0);
1135 serial_out(up, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001137 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 scratch = serial_in(up, UART_IIR) >> 6;
1139
1140 DEBUG_AUTOCONF("iir=%d ", scratch);
1141
1142 switch (scratch) {
1143 case 0:
1144 autoconfig_8250(up);
1145 break;
1146 case 1:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001147 port->type = PORT_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 break;
1149 case 2:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001150 port->type = PORT_16550;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 break;
1152 case 3:
1153 autoconfig_16550a(up);
1154 break;
1155 }
1156
1157#ifdef CONFIG_SERIAL_8250_RSA
1158 /*
1159 * Only probe for RSA ports if we got the region.
1160 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001161 if (port->type == PORT_16550A && probeflags & PROBE_RSA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 int i;
1163
1164 for (i = 0 ; i < probe_rsa_count; ++i) {
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001165 if (probe_rsa[i] == port->iobase && __enable_rsa(up)) {
1166 port->type = PORT_RSA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
1168 }
1169 }
1170 }
1171#endif
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001172
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001173 serial_out(up, UART_LCR, save_lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001175 if (up->capabilities != uart_config[port->type].flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 printk(KERN_WARNING
1177 "ttyS%d: detected caps %08x should be %08x\n",
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001178 serial_index(port), up->capabilities,
1179 uart_config[port->type].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001182 port->fifosize = uart_config[up->port.type].fifo_size;
1183 up->capabilities = uart_config[port->type].flags;
1184 up->tx_loadsz = uart_config[port->type].tx_loadsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001186 if (port->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 goto out;
1188
1189 /*
1190 * Reset the UART.
1191 */
1192#ifdef CONFIG_SERIAL_8250_RSA
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001193 if (port->type == PORT_RSA)
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001194 serial_out(up, UART_RSA_FRR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195#endif
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001196 serial_out(up, UART_MCR, save_mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 serial8250_clear_fifos(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001198 serial_in(up, UART_RX);
Lennert Buytenhek5c8c7552005-11-12 21:58:05 +00001199 if (up->capabilities & UART_CAP_UUE)
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001200 serial_out(up, UART_IER, UART_IER_UUE);
Lennert Buytenhek5c8c7552005-11-12 21:58:05 +00001201 else
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001202 serial_out(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Thomas Koellerbd71c182007-05-06 14:48:47 -07001204 out:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001205 spin_unlock_irqrestore(&port->lock, flags);
1206 DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209static void autoconfig_irq(struct uart_8250_port *up)
1210{
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001211 struct uart_port *port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 unsigned char save_mcr, save_ier;
1213 unsigned char save_ICP = 0;
1214 unsigned int ICP = 0;
1215 unsigned long irqs;
1216 int irq;
1217
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001218 if (port->flags & UPF_FOURPORT) {
1219 ICP = (port->iobase & 0xfe0) | 0x1f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 save_ICP = inb_p(ICP);
1221 outb_p(0x80, ICP);
Paul Gortmaker0d263a22012-03-08 19:12:10 -05001222 inb_p(ICP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224
1225 /* forget possible initially masked and pending IRQ */
1226 probe_irq_off(probe_irq_on());
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001227 save_mcr = serial_in(up, UART_MCR);
1228 save_ier = serial_in(up, UART_IER);
1229 serial_out(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
Thomas Koellerbd71c182007-05-06 14:48:47 -07001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 irqs = probe_irq_on();
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001232 serial_out(up, UART_MCR, 0);
Alan Cox6f803cd2008-02-08 04:18:52 -08001233 udelay(10);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001234 if (port->flags & UPF_FOURPORT) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001235 serial_out(up, UART_MCR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 UART_MCR_DTR | UART_MCR_RTS);
1237 } else {
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001238 serial_out(up, UART_MCR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1240 }
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001241 serial_out(up, UART_IER, 0x0f); /* enable all intrs */
Paul Gortmaker0d263a22012-03-08 19:12:10 -05001242 serial_in(up, UART_LSR);
1243 serial_in(up, UART_RX);
1244 serial_in(up, UART_IIR);
1245 serial_in(up, UART_MSR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001246 serial_out(up, UART_TX, 0xFF);
Alan Cox6f803cd2008-02-08 04:18:52 -08001247 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 irq = probe_irq_off(irqs);
1249
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001250 serial_out(up, UART_MCR, save_mcr);
1251 serial_out(up, UART_IER, save_ier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001253 if (port->flags & UPF_FOURPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 outb_p(save_ICP, ICP);
1255
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001256 port->irq = (irq > 0) ? irq : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
Russell Kinge763b902005-06-29 18:41:51 +01001259static inline void __stop_tx(struct uart_8250_port *p)
1260{
1261 if (p->ier & UART_IER_THRI) {
1262 p->ier &= ~UART_IER_THRI;
1263 serial_out(p, UART_IER, p->ier);
1264 }
1265}
1266
Russell Kingb129a8c2005-08-31 10:12:14 +01001267static void serial8250_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Jamie Iles49d57412010-12-01 23:39:35 +00001269 struct uart_8250_port *up =
1270 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Russell Kinge763b902005-06-29 18:41:51 +01001272 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 /*
Russell Kinge763b902005-06-29 18:41:51 +01001275 * We really want to stop the transmitter from sending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001277 if (port->type == PORT_16C950) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 up->acr |= UART_ACR_TXDIS;
1279 serial_icr_write(up, UART_ACR, up->acr);
1280 }
1281}
1282
Russell Kingb129a8c2005-08-31 10:12:14 +01001283static void serial8250_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Jamie Iles49d57412010-12-01 23:39:35 +00001285 struct uart_8250_port *up =
1286 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 if (!(up->ier & UART_IER_THRI)) {
1289 up->ier |= UART_IER_THRI;
1290 serial_out(up, UART_IER, up->ier);
Russell King55d3b282005-06-23 15:05:41 +01001291
Russell King67f76542005-06-23 22:26:43 +01001292 if (up->bugs & UART_BUG_TXEN) {
Ian Jackson68cb4f82009-11-18 11:08:11 +01001293 unsigned char lsr;
Russell King55d3b282005-06-23 15:05:41 +01001294 lsr = serial_in(up, UART_LSR);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001295 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001296 if ((port->type == PORT_RM9000) ?
Ian Jackson68cb4f82009-11-18 11:08:11 +01001297 (lsr & UART_LSR_THRE) :
1298 (lsr & UART_LSR_TEMT))
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001299 serial8250_tx_chars(up);
Russell King55d3b282005-06-23 15:05:41 +01001300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
Russell Kinge763b902005-06-29 18:41:51 +01001302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 /*
Russell Kinge763b902005-06-29 18:41:51 +01001304 * Re-enable the transmitter if we disabled it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001306 if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 up->acr &= ~UART_ACR_TXDIS;
1308 serial_icr_write(up, UART_ACR, up->acr);
1309 }
1310}
1311
1312static void serial8250_stop_rx(struct uart_port *port)
1313{
Jamie Iles49d57412010-12-01 23:39:35 +00001314 struct uart_8250_port *up =
1315 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 up->ier &= ~UART_IER_RLSI;
1318 up->port.read_status_mask &= ~UART_LSR_DR;
1319 serial_out(up, UART_IER, up->ier);
1320}
1321
1322static void serial8250_enable_ms(struct uart_port *port)
1323{
Jamie Iles49d57412010-12-01 23:39:35 +00001324 struct uart_8250_port *up =
1325 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001327 /* no MSR capabilities */
1328 if (up->bugs & UART_BUG_NOMSR)
1329 return;
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 up->ier |= UART_IER_MSI;
1332 serial_out(up, UART_IER, up->ier);
1333}
1334
Stephen Warren5f873ba2011-05-17 16:12:37 -06001335/*
1336 * Clear the Tegra rx fifo after a break
1337 *
1338 * FIXME: This needs to become a port specific callback once we have a
1339 * framework for this
1340 */
1341static void clear_rx_fifo(struct uart_8250_port *up)
1342{
1343 unsigned int status, tmout = 10000;
1344 do {
1345 status = serial_in(up, UART_LSR);
1346 if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
1347 status = serial_in(up, UART_RX);
1348 else
1349 break;
1350 if (--tmout == 0)
1351 break;
1352 udelay(1);
1353 } while (1);
1354}
1355
Paul Gortmaker0690f412011-12-04 18:42:19 -05001356/*
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001357 * serial8250_rx_chars: processes according to the passed in LSR
Paul Gortmaker0690f412011-12-04 18:42:19 -05001358 * value, and returns the remaining LSR bits not handled
1359 * by this Rx routine.
1360 */
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001361unsigned char
1362serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001364 struct uart_port *port = &up->port;
1365 struct tty_struct *tty = port->state->port.tty;
Paul Gortmaker0690f412011-12-04 18:42:19 -05001366 unsigned char ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 int max_count = 256;
1368 char flag;
1369
1370 do {
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001371 if (likely(lsr & UART_LSR_DR))
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001372 ch = serial_in(up, UART_RX);
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001373 else
1374 /*
1375 * Intel 82571 has a Serial Over Lan device that will
1376 * set UART_LSR_BI without setting UART_LSR_DR when
1377 * it receives a break. To avoid reading from the
1378 * receive buffer without UART_LSR_DR bit set, we
1379 * just force the read character to be 0
1380 */
1381 ch = 0;
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 flag = TTY_NORMAL;
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001384 port->icount.rx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001386 lsr |= up->lsr_saved_flags;
1387 up->lsr_saved_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001389 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /*
1391 * For statistics only
1392 */
1393 if (lsr & UART_LSR_BI) {
1394 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001395 port->icount.brk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 /*
Stephen Warren5f873ba2011-05-17 16:12:37 -06001397 * If tegra port then clear the rx fifo to
1398 * accept another break/character.
1399 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001400 if (port->type == PORT_TEGRA)
Stephen Warren5f873ba2011-05-17 16:12:37 -06001401 clear_rx_fifo(up);
1402
1403 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 * We do the SysRQ and SAK checking
1405 * here because otherwise the break
1406 * may get masked by ignore_status_mask
1407 * or read_status_mask.
1408 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001409 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 goto ignore_char;
1411 } else if (lsr & UART_LSR_PE)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001412 port->icount.parity++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 else if (lsr & UART_LSR_FE)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001414 port->icount.frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (lsr & UART_LSR_OE)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001416 port->icount.overrun++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 /*
Russell King23907eb2005-04-16 15:26:39 -07001419 * Mask off conditions which should be ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001421 lsr &= port->read_status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 if (lsr & UART_LSR_BI) {
1424 DEBUG_INTR("handling break....");
1425 flag = TTY_BREAK;
1426 } else if (lsr & UART_LSR_PE)
1427 flag = TTY_PARITY;
1428 else if (lsr & UART_LSR_FE)
1429 flag = TTY_FRAME;
1430 }
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001431 if (uart_handle_sysrq_char(port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 goto ignore_char;
Russell King05ab3012005-05-09 23:21:59 +01001433
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001434 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
Russell King05ab3012005-05-09 23:21:59 +01001435
Alan Cox6f803cd2008-02-08 04:18:52 -08001436ignore_char:
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001437 lsr = serial_in(up, UART_LSR);
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001438 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001439 spin_unlock(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 tty_flip_buffer_push(tty);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001441 spin_lock(&port->lock);
Paul Gortmaker0690f412011-12-04 18:42:19 -05001442 return lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001444EXPORT_SYMBOL_GPL(serial8250_rx_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001446void serial8250_tx_chars(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001448 struct uart_port *port = &up->port;
1449 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 int count;
1451
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001452 if (port->x_char) {
1453 serial_out(up, UART_TX, port->x_char);
1454 port->icount.tx++;
1455 port->x_char = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return;
1457 }
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001458 if (uart_tx_stopped(port)) {
1459 serial8250_stop_tx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001460 return;
1461 }
1462 if (uart_circ_empty(xmit)) {
Russell Kinge763b902005-06-29 18:41:51 +01001463 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return;
1465 }
1466
1467 count = up->tx_loadsz;
1468 do {
1469 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1470 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001471 port->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (uart_circ_empty(xmit))
1473 break;
1474 } while (--count > 0);
1475
1476 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001477 uart_write_wakeup(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 DEBUG_INTR("THRE...");
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}
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001484EXPORT_SYMBOL_GPL(serial8250_tx_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001486unsigned int serial8250_modem_status(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001488 struct uart_port *port = &up->port;
Russell King2af7cd62006-01-04 16:55:09 +00001489 unsigned int status = serial_in(up, UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001491 status |= up->msr_saved_flags;
1492 up->msr_saved_flags = 0;
Taku Izumifdc30b32007-04-23 14:41:00 -07001493 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001494 port->state != NULL) {
Russell King2af7cd62006-01-04 16:55:09 +00001495 if (status & UART_MSR_TERI)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001496 port->icount.rng++;
Russell King2af7cd62006-01-04 16:55:09 +00001497 if (status & UART_MSR_DDSR)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001498 port->icount.dsr++;
Russell King2af7cd62006-01-04 16:55:09 +00001499 if (status & UART_MSR_DDCD)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001500 uart_handle_dcd_change(port, status & UART_MSR_DCD);
Russell King2af7cd62006-01-04 16:55:09 +00001501 if (status & UART_MSR_DCTS)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001502 uart_handle_cts_change(port, status & UART_MSR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001504 wake_up_interruptible(&port->state->port.delta_msr_wait);
Russell King2af7cd62006-01-04 16:55:09 +00001505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Russell King2af7cd62006-01-04 16:55:09 +00001507 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001509EXPORT_SYMBOL_GPL(serial8250_modem_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511/*
1512 * This handles the interrupt from one port.
1513 */
Paul Gortmaker86b21192011-12-04 18:42:22 -05001514int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Paul Gortmaker0690f412011-12-04 18:42:19 -05001516 unsigned char status;
Jiri Kosina4bf36312007-04-23 14:41:21 -07001517 unsigned long flags;
Paul Gortmaker86b21192011-12-04 18:42:22 -05001518 struct uart_8250_port *up =
1519 container_of(port, struct uart_8250_port, port);
1520
1521 if (iir & UART_IIR_NO_INT)
1522 return 0;
Russell King45e24602006-01-04 19:19:06 +00001523
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001524 spin_lock_irqsave(&port->lock, flags);
Russell King45e24602006-01-04 19:19:06 +00001525
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001526 status = serial_in(up, UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 DEBUG_INTR("status = %x...", status);
1529
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001530 if (status & (UART_LSR_DR | UART_LSR_BI))
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001531 status = serial8250_rx_chars(up, status);
1532 serial8250_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (status & UART_LSR_THRE)
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001534 serial8250_tx_chars(up);
Russell King45e24602006-01-04 19:19:06 +00001535
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001536 spin_unlock_irqrestore(&port->lock, flags);
Paul Gortmaker86b21192011-12-04 18:42:22 -05001537 return 1;
Jamie Iles583d28e2011-08-15 10:17:52 +01001538}
Jamie Ilesc7a1bdc2011-08-26 19:04:49 +01001539EXPORT_SYMBOL_GPL(serial8250_handle_irq);
Jamie Iles583d28e2011-08-15 10:17:52 +01001540
1541static int serial8250_default_handle_irq(struct uart_port *port)
1542{
1543 struct uart_8250_port *up =
1544 container_of(port, struct uart_8250_port, port);
1545 unsigned int iir = serial_in(up, UART_IIR);
1546
1547 return serial8250_handle_irq(port, iir);
1548}
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550/*
1551 * This is the serial driver's interrupt routine.
1552 *
1553 * Arjan thinks the old way was overly complex, so it got simplified.
1554 * Alan disagrees, saying that need the complexity to handle the weird
1555 * nature of ISA shared interrupts. (This is a special exception.)
1556 *
1557 * In order to handle ISA shared interrupts properly, we need to check
1558 * that all ports have been serviced, and therefore the ISA interrupt
1559 * line has been de-asserted.
1560 *
1561 * This means we need to loop through all ports. checking that they
1562 * don't have an interrupt pending.
1563 */
David Howells7d12e782006-10-05 14:55:46 +01001564static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 struct irq_info *i = dev_id;
1567 struct list_head *l, *end = NULL;
1568 int pass_counter = 0, handled = 0;
1569
1570 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1571
1572 spin_lock(&i->lock);
1573
1574 l = i->head;
1575 do {
1576 struct uart_8250_port *up;
Jamie Iles583d28e2011-08-15 10:17:52 +01001577 struct uart_port *port;
Dan Williams448ac152011-11-22 13:41:24 -08001578 bool skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 up = list_entry(l, struct uart_8250_port, list);
Jamie Iles583d28e2011-08-15 10:17:52 +01001581 port = &up->port;
Dan Williams448ac152011-11-22 13:41:24 -08001582 skip = pass_counter && up->port.flags & UPF_IIR_ONCE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Dan Williams448ac152011-11-22 13:41:24 -08001584 if (!skip && port->handle_irq(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 handled = 1;
Marc St-Jeanbeab6972007-05-06 14:48:45 -07001586 end = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 } else if (end == NULL)
1588 end = l;
1589
1590 l = l->next;
1591
1592 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1593 /* If we hit this, we're dead. */
Daniel Drakecd3ecad2010-10-20 16:00:48 -07001594 printk_ratelimited(KERN_ERR
1595 "serial8250: too much work for irq%d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 break;
1597 }
1598 } while (l != end);
1599
1600 spin_unlock(&i->lock);
1601
1602 DEBUG_INTR("end.\n");
1603
1604 return IRQ_RETVAL(handled);
1605}
1606
1607/*
1608 * To support ISA shared interrupts, we need to have one interrupt
1609 * handler that ensures that the IRQ line has been deasserted
1610 * before returning. Failing to do this will result in the IRQ
1611 * line being stuck active, and, since ISA irqs are edge triggered,
1612 * no more IRQs will be seen.
1613 */
1614static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1615{
1616 spin_lock_irq(&i->lock);
1617
1618 if (!list_empty(i->head)) {
1619 if (i->head == &up->list)
1620 i->head = i->head->next;
1621 list_del(&up->list);
1622 } else {
1623 BUG_ON(i->head != &up->list);
1624 i->head = NULL;
1625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 spin_unlock_irq(&i->lock);
Alan Cox25db8ad2008-08-19 20:49:40 -07001627 /* List empty so throw away the hash node */
1628 if (i->head == NULL) {
1629 hlist_del(&i->node);
1630 kfree(i);
1631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
1634static int serial_link_irq_chain(struct uart_8250_port *up)
1635{
Alan Cox25db8ad2008-08-19 20:49:40 -07001636 struct hlist_head *h;
1637 struct hlist_node *n;
1638 struct irq_info *i;
Thomas Gleixner40663cc2006-07-01 19:29:43 -07001639 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Alan Cox25db8ad2008-08-19 20:49:40 -07001641 mutex_lock(&hash_mutex);
1642
1643 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1644
1645 hlist_for_each(n, h) {
1646 i = hlist_entry(n, struct irq_info, node);
1647 if (i->irq == up->port.irq)
1648 break;
1649 }
1650
1651 if (n == NULL) {
1652 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
1653 if (i == NULL) {
1654 mutex_unlock(&hash_mutex);
1655 return -ENOMEM;
1656 }
1657 spin_lock_init(&i->lock);
1658 i->irq = up->port.irq;
1659 hlist_add_head(&i->node, h);
1660 }
1661 mutex_unlock(&hash_mutex);
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 spin_lock_irq(&i->lock);
1664
1665 if (i->head) {
1666 list_add(&up->list, i->head);
1667 spin_unlock_irq(&i->lock);
1668
1669 ret = 0;
1670 } else {
1671 INIT_LIST_HEAD(&up->list);
1672 i->head = &up->list;
1673 spin_unlock_irq(&i->lock);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07001674 irq_flags |= up->port.irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 ret = request_irq(up->port.irq, serial8250_interrupt,
1676 irq_flags, "serial", i);
1677 if (ret < 0)
1678 serial_do_unlink(i, up);
1679 }
1680
1681 return ret;
1682}
1683
1684static void serial_unlink_irq_chain(struct uart_8250_port *up)
1685{
Alan Cox25db8ad2008-08-19 20:49:40 -07001686 struct irq_info *i;
1687 struct hlist_node *n;
1688 struct hlist_head *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Alan Cox25db8ad2008-08-19 20:49:40 -07001690 mutex_lock(&hash_mutex);
1691
1692 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1693
1694 hlist_for_each(n, h) {
1695 i = hlist_entry(n, struct irq_info, node);
1696 if (i->irq == up->port.irq)
1697 break;
1698 }
1699
1700 BUG_ON(n == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 BUG_ON(i->head == NULL);
1702
1703 if (list_empty(i->head))
1704 free_irq(up->port.irq, i);
1705
1706 serial_do_unlink(i, up);
Alan Cox25db8ad2008-08-19 20:49:40 -07001707 mutex_unlock(&hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
1710/*
1711 * This function is used to handle ports that do not have an
1712 * interrupt. This doesn't work very well for 16450's, but gives
1713 * barely passable results for a 16550A. (Although at the expense
1714 * of much CPU overhead).
1715 */
1716static void serial8250_timeout(unsigned long data)
1717{
1718 struct uart_8250_port *up = (struct uart_8250_port *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Paul Gortmakera0431472011-12-04 18:42:21 -05001720 up->port.handle_irq(&up->port);
Anton Vorontsov54381062010-10-01 17:21:25 +04001721 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
Alex Williamson40b36da2007-02-14 00:33:04 -08001722}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Alex Williamson40b36da2007-02-14 00:33:04 -08001724static void serial8250_backup_timeout(unsigned long data)
1725{
1726 struct uart_8250_port *up = (struct uart_8250_port *)data;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001727 unsigned int iir, ier = 0, lsr;
1728 unsigned long flags;
Alex Williamson40b36da2007-02-14 00:33:04 -08001729
Al Cooperdbb3b1c2011-07-25 16:19:52 -04001730 spin_lock_irqsave(&up->port.lock, flags);
1731
Alex Williamson40b36da2007-02-14 00:33:04 -08001732 /*
1733 * Must disable interrupts or else we risk racing with the interrupt
1734 * based handler.
1735 */
Alan Coxd4e33fa2012-01-26 17:44:09 +00001736 if (up->port.irq) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001737 ier = serial_in(up, UART_IER);
1738 serial_out(up, UART_IER, 0);
1739 }
1740
1741 iir = serial_in(up, UART_IIR);
1742
1743 /*
1744 * This should be a safe test for anyone who doesn't trust the
1745 * IIR bits on their UART, but it's specifically designed for
1746 * the "Diva" UART used on the management processor on many HP
1747 * ia64 and parisc boxes.
1748 */
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001749 lsr = serial_in(up, UART_LSR);
1750 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -08001751 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
Alan Coxebd2c8f2009-09-19 13:13:28 -07001752 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001753 (lsr & UART_LSR_THRE)) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001754 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1755 iir |= UART_IIR_THRI;
1756 }
1757
1758 if (!(iir & UART_IIR_NO_INT))
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001759 serial8250_tx_chars(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001760
Alan Coxd4e33fa2012-01-26 17:44:09 +00001761 if (up->port.irq)
Alex Williamson40b36da2007-02-14 00:33:04 -08001762 serial_out(up, UART_IER, ier);
1763
Al Cooperdbb3b1c2011-07-25 16:19:52 -04001764 spin_unlock_irqrestore(&up->port.lock, flags);
1765
Alex Williamson40b36da2007-02-14 00:33:04 -08001766 /* Standard timer interval plus 0.2s to keep the port running */
Alan Cox6f803cd2008-02-08 04:18:52 -08001767 mod_timer(&up->timer,
Anton Vorontsov54381062010-10-01 17:21:25 +04001768 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
1771static unsigned int serial8250_tx_empty(struct uart_port *port)
1772{
Jamie Iles49d57412010-12-01 23:39:35 +00001773 struct uart_8250_port *up =
1774 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 unsigned long flags;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001776 unsigned int lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001778 spin_lock_irqsave(&port->lock, flags);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001779 lsr = serial_in(up, UART_LSR);
1780 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001781 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Dick Hollenbeckbca47612009-12-09 12:31:34 -08001783 return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
1786static unsigned int serial8250_get_mctrl(struct uart_port *port)
1787{
Jamie Iles49d57412010-12-01 23:39:35 +00001788 struct uart_8250_port *up =
1789 container_of(port, struct uart_8250_port, port);
Russell King2af7cd62006-01-04 16:55:09 +00001790 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 unsigned int ret;
1792
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001793 status = serial8250_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 ret = 0;
1796 if (status & UART_MSR_DCD)
1797 ret |= TIOCM_CAR;
1798 if (status & UART_MSR_RI)
1799 ret |= TIOCM_RNG;
1800 if (status & UART_MSR_DSR)
1801 ret |= TIOCM_DSR;
1802 if (status & UART_MSR_CTS)
1803 ret |= TIOCM_CTS;
1804 return ret;
1805}
1806
1807static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1808{
Jamie Iles49d57412010-12-01 23:39:35 +00001809 struct uart_8250_port *up =
1810 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 unsigned char mcr = 0;
1812
1813 if (mctrl & TIOCM_RTS)
1814 mcr |= UART_MCR_RTS;
1815 if (mctrl & TIOCM_DTR)
1816 mcr |= UART_MCR_DTR;
1817 if (mctrl & TIOCM_OUT1)
1818 mcr |= UART_MCR_OUT1;
1819 if (mctrl & TIOCM_OUT2)
1820 mcr |= UART_MCR_OUT2;
1821 if (mctrl & TIOCM_LOOP)
1822 mcr |= UART_MCR_LOOP;
1823
1824 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1825
1826 serial_out(up, UART_MCR, mcr);
1827}
1828
1829static void serial8250_break_ctl(struct uart_port *port, int break_state)
1830{
Jamie Iles49d57412010-12-01 23:39:35 +00001831 struct uart_8250_port *up =
1832 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 unsigned long flags;
1834
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001835 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (break_state == -1)
1837 up->lcr |= UART_LCR_SBC;
1838 else
1839 up->lcr &= ~UART_LCR_SBC;
1840 serial_out(up, UART_LCR, up->lcr);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001841 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
Alex Williamson40b36da2007-02-14 00:33:04 -08001844/*
1845 * Wait for transmitter & holding register to empty
1846 */
Will Newtonb5d674a2008-10-13 10:36:21 +01001847static void wait_for_xmitr(struct uart_8250_port *up, int bits)
Alex Williamson40b36da2007-02-14 00:33:04 -08001848{
1849 unsigned int status, tmout = 10000;
1850
1851 /* Wait up to 10ms for the character(s) to be sent. */
David Daney97d303b2010-10-05 11:40:07 -07001852 for (;;) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001853 status = serial_in(up, UART_LSR);
1854
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001855 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -08001856
David Daney97d303b2010-10-05 11:40:07 -07001857 if ((status & bits) == bits)
1858 break;
Alex Williamson40b36da2007-02-14 00:33:04 -08001859 if (--tmout == 0)
1860 break;
1861 udelay(1);
David Daney97d303b2010-10-05 11:40:07 -07001862 }
Alex Williamson40b36da2007-02-14 00:33:04 -08001863
1864 /* Wait up to 1s for flow control if necessary */
1865 if (up->port.flags & UPF_CONS_FLOW) {
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001866 unsigned int tmout;
1867 for (tmout = 1000000; tmout; tmout--) {
1868 unsigned int msr = serial_in(up, UART_MSR);
1869 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1870 if (msr & UART_MSR_CTS)
1871 break;
Alex Williamson40b36da2007-02-14 00:33:04 -08001872 udelay(1);
1873 touch_nmi_watchdog();
1874 }
1875 }
1876}
1877
Jason Wesself2d937f2008-04-17 20:05:37 +02001878#ifdef CONFIG_CONSOLE_POLL
1879/*
1880 * Console polling routines for writing and reading from the uart while
1881 * in an interrupt or debug context.
1882 */
1883
1884static int serial8250_get_poll_char(struct uart_port *port)
1885{
Jamie Iles49d57412010-12-01 23:39:35 +00001886 struct uart_8250_port *up =
1887 container_of(port, struct uart_8250_port, port);
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001888 unsigned char lsr = serial_in(up, UART_LSR);
Jason Wesself2d937f2008-04-17 20:05:37 +02001889
Jason Wesself5316b42010-05-20 21:04:22 -05001890 if (!(lsr & UART_LSR_DR))
1891 return NO_POLL_CHAR;
Jason Wesself2d937f2008-04-17 20:05:37 +02001892
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001893 return serial_in(up, UART_RX);
Jason Wesself2d937f2008-04-17 20:05:37 +02001894}
1895
1896
1897static void serial8250_put_poll_char(struct uart_port *port,
1898 unsigned char c)
1899{
1900 unsigned int ier;
Jamie Iles49d57412010-12-01 23:39:35 +00001901 struct uart_8250_port *up =
1902 container_of(port, struct uart_8250_port, port);
Jason Wesself2d937f2008-04-17 20:05:37 +02001903
1904 /*
1905 * First save the IER then disable the interrupts
1906 */
1907 ier = serial_in(up, UART_IER);
1908 if (up->capabilities & UART_CAP_UUE)
1909 serial_out(up, UART_IER, UART_IER_UUE);
1910 else
1911 serial_out(up, UART_IER, 0);
1912
1913 wait_for_xmitr(up, BOTH_EMPTY);
1914 /*
1915 * Send the character out.
1916 * If a LF, also do CR...
1917 */
1918 serial_out(up, UART_TX, c);
1919 if (c == 10) {
1920 wait_for_xmitr(up, BOTH_EMPTY);
1921 serial_out(up, UART_TX, 13);
1922 }
1923
1924 /*
1925 * Finally, wait for transmitter to become empty
1926 * and restore the IER
1927 */
1928 wait_for_xmitr(up, BOTH_EMPTY);
1929 serial_out(up, UART_IER, ier);
1930}
1931
1932#endif /* CONFIG_CONSOLE_POLL */
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934static int serial8250_startup(struct uart_port *port)
1935{
Jamie Iles49d57412010-12-01 23:39:35 +00001936 struct uart_8250_port *up =
1937 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 unsigned long flags;
Russell King55d3b282005-06-23 15:05:41 +01001939 unsigned char lsr, iir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 int retval;
1941
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001942 port->fifosize = uart_config[up->port.type].fifo_size;
Ondrej Puzmane4f05af2010-12-04 21:17:38 +01001943 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 up->capabilities = uart_config[up->port.type].flags;
1945 up->mcr = 0;
1946
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001947 if (port->iotype != up->cur_iotype)
Alan Coxb8e7e402009-05-28 14:01:35 +01001948 set_io_from_upio(port);
1949
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001950 if (port->type == PORT_16C950) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /* Wake up and initialize UART */
1952 up->acr = 0;
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001953 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1954 serial_out(up, UART_EFR, UART_EFR_ECB);
1955 serial_out(up, UART_IER, 0);
1956 serial_out(up, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001958 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1959 serial_out(up, UART_EFR, UART_EFR_ECB);
1960 serial_out(up, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962
1963#ifdef CONFIG_SERIAL_8250_RSA
1964 /*
1965 * If this is an RSA port, see if we can kick it up to the
1966 * higher speed clock.
1967 */
1968 enable_rsa(up);
1969#endif
1970
1971 /*
1972 * Clear the FIFO buffers and disable them.
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08001973 * (they will be reenabled in set_termios())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 */
1975 serial8250_clear_fifos(up);
1976
1977 /*
1978 * Clear the interrupt registers.
1979 */
Paul Gortmaker0d263a22012-03-08 19:12:10 -05001980 serial_in(up, UART_LSR);
1981 serial_in(up, UART_RX);
1982 serial_in(up, UART_IIR);
1983 serial_in(up, UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 /*
1986 * At this point, there's no way the LSR could still be 0xff;
1987 * if it is, then bail out, because there's likely no UART
1988 * here.
1989 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001990 if (!(port->flags & UPF_BUGGY_UART) &&
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001991 (serial_in(up, UART_LSR) == 0xff)) {
Konrad Rzeszutek Wilk7808a4c2011-09-26 09:14:34 -04001992 printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001993 serial_index(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return -ENODEV;
1995 }
1996
1997 /*
1998 * For a XR16C850, we need to set the trigger levels
1999 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002000 if (port->type == PORT_16850) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 unsigned char fctr;
2002
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002003 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002005 fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2006 serial_out(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2007 serial_out(up, UART_TRG, UART_TRG_96);
2008 serial_out(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2009 serial_out(up, UART_TRG, UART_TRG_96);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002011 serial_out(up, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002014 if (port->irq) {
Alex Williamson01c194d2008-04-28 02:14:09 -07002015 unsigned char iir1;
Alex Williamson40b36da2007-02-14 00:33:04 -08002016 /*
2017 * Test for UARTs that do not reassert THRE when the
2018 * transmitter is idle and the interrupt has already
2019 * been cleared. Real 16550s should always reassert
2020 * this interrupt whenever the transmitter is idle and
2021 * the interrupt is enabled. Delays are necessary to
2022 * allow register changes to become visible.
2023 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002024 spin_lock_irqsave(&port->lock, flags);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002025 if (up->port.irqflags & IRQF_SHARED)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002026 disable_irq_nosync(port->irq);
Alex Williamson40b36da2007-02-14 00:33:04 -08002027
2028 wait_for_xmitr(up, UART_LSR_THRE);
2029 serial_out_sync(up, UART_IER, UART_IER_THRI);
2030 udelay(1); /* allow THRE to set */
Alex Williamson01c194d2008-04-28 02:14:09 -07002031 iir1 = serial_in(up, UART_IIR);
Alex Williamson40b36da2007-02-14 00:33:04 -08002032 serial_out(up, UART_IER, 0);
2033 serial_out_sync(up, UART_IER, UART_IER_THRI);
2034 udelay(1); /* allow a working UART time to re-assert THRE */
2035 iir = serial_in(up, UART_IIR);
2036 serial_out(up, UART_IER, 0);
2037
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002038 if (port->irqflags & IRQF_SHARED)
2039 enable_irq(port->irq);
2040 spin_unlock_irqrestore(&port->lock, flags);
Alex Williamson40b36da2007-02-14 00:33:04 -08002041
2042 /*
2043 * If the interrupt is not reasserted, setup a timer to
2044 * kick the UART on a regular basis.
2045 */
Alex Williamson01c194d2008-04-28 02:14:09 -07002046 if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
Will Newton363f66f2008-09-02 14:35:44 -07002047 up->bugs |= UART_BUG_THRE;
David S. Miller84408382008-10-13 10:45:26 +01002048 pr_debug("ttyS%d - using backup timer\n",
2049 serial_index(port));
Alex Williamson40b36da2007-02-14 00:33:04 -08002050 }
2051 }
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 /*
Will Newton363f66f2008-09-02 14:35:44 -07002054 * The above check will only give an accurate result the first time
2055 * the port is opened so this value needs to be preserved.
2056 */
2057 if (up->bugs & UART_BUG_THRE) {
2058 up->timer.function = serial8250_backup_timeout;
2059 up->timer.data = (unsigned long)up;
2060 mod_timer(&up->timer, jiffies +
Anton Vorontsov54381062010-10-01 17:21:25 +04002061 uart_poll_timeout(port) + HZ / 5);
Will Newton363f66f2008-09-02 14:35:44 -07002062 }
2063
2064 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 * If the "interrupt" for this port doesn't correspond with any
2066 * hardware interrupt, we use a timer-based system. The original
2067 * driver used to do this with IRQ0.
2068 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002069 if (!port->irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 up->timer.data = (unsigned long)up;
Anton Vorontsov54381062010-10-01 17:21:25 +04002071 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 } else {
2073 retval = serial_link_irq_chain(up);
2074 if (retval)
2075 return retval;
2076 }
2077
2078 /*
2079 * Now, initialize the UART
2080 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002081 serial_out(up, UART_LCR, UART_LCR_WLEN8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002083 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if (up->port.flags & UPF_FOURPORT) {
Alan Coxd4e33fa2012-01-26 17:44:09 +00002085 if (!up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 up->port.mctrl |= TIOCM_OUT1;
2087 } else
2088 /*
2089 * Most PC uarts need OUT2 raised to enable interrupts.
2090 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002091 if (port->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 up->port.mctrl |= TIOCM_OUT2;
2093
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002094 serial8250_set_mctrl(port, port->mctrl);
Russell King55d3b282005-06-23 15:05:41 +01002095
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002096 /* Serial over Lan (SoL) hack:
2097 Intel 8257x Gigabit ethernet chips have a
2098 16550 emulation, to be used for Serial Over Lan.
2099 Those chips take a longer time than a normal
2100 serial device to signalize that a transmission
2101 data was queued. Due to that, the above test generally
2102 fails. One solution would be to delay the reading of
2103 iir. However, this is not reliable, since the timeout
2104 is variable. So, let's just don't test if we receive
2105 TX irq. This way, we'll never enable UART_BUG_TXEN.
2106 */
Chuck Ebbertd41a4b52009-10-01 15:44:26 -07002107 if (skip_txen_test || up->port.flags & UPF_NO_TXEN_TEST)
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002108 goto dont_test_tx_en;
2109
Russell King55d3b282005-06-23 15:05:41 +01002110 /*
2111 * Do a quick test to see if we receive an
2112 * interrupt when we enable the TX irq.
2113 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002114 serial_out(up, UART_IER, UART_IER_THRI);
Russell King55d3b282005-06-23 15:05:41 +01002115 lsr = serial_in(up, UART_LSR);
2116 iir = serial_in(up, UART_IIR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002117 serial_out(up, UART_IER, 0);
Russell King55d3b282005-06-23 15:05:41 +01002118
2119 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
Russell King67f76542005-06-23 22:26:43 +01002120 if (!(up->bugs & UART_BUG_TXEN)) {
2121 up->bugs |= UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002122 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
David S. Miller84408382008-10-13 10:45:26 +01002123 serial_index(port));
Russell King55d3b282005-06-23 15:05:41 +01002124 }
2125 } else {
Russell King67f76542005-06-23 22:26:43 +01002126 up->bugs &= ~UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002127 }
2128
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002129dont_test_tx_en:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002130 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 /*
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002133 * Clear the interrupt registers again for luck, and clear the
2134 * saved flags to avoid getting false values from polling
2135 * routines or the previous session.
2136 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002137 serial_in(up, UART_LSR);
2138 serial_in(up, UART_RX);
2139 serial_in(up, UART_IIR);
2140 serial_in(up, UART_MSR);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002141 up->lsr_saved_flags = 0;
2142 up->msr_saved_flags = 0;
2143
2144 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 * Finally, enable interrupts. Note: Modem status interrupts
2146 * are set via set_termios(), which will be occurring imminently
2147 * anyway, so we don't enable them here.
2148 */
2149 up->ier = UART_IER_RLSI | UART_IER_RDI;
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002150 serial_out(up, UART_IER, up->ier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002152 if (port->flags & UPF_FOURPORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 unsigned int icp;
2154 /*
2155 * Enable interrupts on the AST Fourport board
2156 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002157 icp = (port->iobase & 0xfe0) | 0x01f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 outb_p(0x80, icp);
Paul Gortmaker0d263a22012-03-08 19:12:10 -05002159 inb_p(icp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return 0;
2163}
2164
2165static void serial8250_shutdown(struct uart_port *port)
2166{
Jamie Iles49d57412010-12-01 23:39:35 +00002167 struct uart_8250_port *up =
2168 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 unsigned long flags;
2170
2171 /*
2172 * Disable interrupts from this port
2173 */
2174 up->ier = 0;
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002175 serial_out(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002177 spin_lock_irqsave(&port->lock, flags);
2178 if (port->flags & UPF_FOURPORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /* reset interrupts on the AST Fourport board */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002180 inb((port->iobase & 0xfe0) | 0x1f);
2181 port->mctrl |= TIOCM_OUT1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 } else
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002183 port->mctrl &= ~TIOCM_OUT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002185 serial8250_set_mctrl(port, port->mctrl);
2186 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 /*
2189 * Disable break condition and FIFOs
2190 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002191 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 serial8250_clear_fifos(up);
2193
2194#ifdef CONFIG_SERIAL_8250_RSA
2195 /*
2196 * Reset the RSA board back to 115kbps compat mode.
2197 */
2198 disable_rsa(up);
2199#endif
2200
2201 /*
2202 * Read data port to reset things, and then unlink from
2203 * the IRQ chain.
2204 */
Paul Gortmaker0d263a22012-03-08 19:12:10 -05002205 serial_in(up, UART_RX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Alex Williamson40b36da2007-02-14 00:33:04 -08002207 del_timer_sync(&up->timer);
2208 up->timer.function = serial8250_timeout;
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002209 if (port->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 serial_unlink_irq_chain(up);
2211}
2212
2213static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
2214{
2215 unsigned int quot;
2216
2217 /*
2218 * Handle magic divisors for baud rates above baud_base on
2219 * SMSC SuperIO chips.
2220 */
2221 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2222 baud == (port->uartclk/4))
2223 quot = 0x8001;
2224 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2225 baud == (port->uartclk/8))
2226 quot = 0x8002;
2227 else
2228 quot = uart_get_divisor(port, baud);
2229
2230 return quot;
2231}
2232
Philippe Langlais235dae52010-07-29 17:13:57 +02002233void
2234serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2235 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Jamie Iles49d57412010-12-01 23:39:35 +00002237 struct uart_8250_port *up =
2238 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 unsigned char cval, fcr = 0;
2240 unsigned long flags;
2241 unsigned int baud, quot;
2242
2243 switch (termios->c_cflag & CSIZE) {
2244 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +01002245 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 break;
2247 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +01002248 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 break;
2250 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +01002251 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 break;
2253 default:
2254 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +01002255 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 break;
2257 }
2258
2259 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +01002260 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (termios->c_cflag & PARENB)
2262 cval |= UART_LCR_PARITY;
2263 if (!(termios->c_cflag & PARODD))
2264 cval |= UART_LCR_EPAR;
2265#ifdef CMSPAR
2266 if (termios->c_cflag & CMSPAR)
2267 cval |= UART_LCR_SPAR;
2268#endif
2269
2270 /*
2271 * Ask the core to calculate the divisor for us.
2272 */
Anton Vorontsov24d481e2009-09-19 13:13:20 -07002273 baud = uart_get_baud_rate(port, termios, old,
2274 port->uartclk / 16 / 0xffff,
2275 port->uartclk / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 quot = serial8250_get_divisor(port, baud);
2277
2278 /*
Russell King4ba5e352005-06-23 10:43:04 +01002279 * Oxford Semi 952 rev B workaround
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 */
Russell King4ba5e352005-06-23 10:43:04 +01002281 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
Alan Cox3e8d4e22008-02-04 22:27:53 -08002282 quot++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002284 if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (baud < 2400)
2286 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2287 else
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002288 fcr = uart_config[port->type].fcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
2290
2291 /*
2292 * MCR-based auto flow control. When AFE is enabled, RTS will be
2293 * deasserted when the receive FIFO contains more characters than
2294 * the trigger, or the MCR RTS bit is cleared. In the case where
2295 * the remote UART is not using CTS auto flow control, we must
2296 * have sufficient FIFO entries for the latency of the remote
2297 * UART to respond. IOW, at least 32 bytes of FIFO.
2298 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002299 if (up->capabilities & UART_CAP_AFE && port->fifosize >= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 up->mcr &= ~UART_MCR_AFE;
2301 if (termios->c_cflag & CRTSCTS)
2302 up->mcr |= UART_MCR_AFE;
2303 }
2304
2305 /*
2306 * Ok, we're now changing the port state. Do it with
2307 * interrupts disabled.
2308 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002309 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 /*
2312 * Update the per-port timeout.
2313 */
2314 uart_update_timeout(port, termios->c_cflag, baud);
2315
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002316 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 if (termios->c_iflag & INPCK)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002318 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (termios->c_iflag & (BRKINT | PARMRK))
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002320 port->read_status_mask |= UART_LSR_BI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 /*
2323 * Characteres to ignore
2324 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002325 port->ignore_status_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (termios->c_iflag & IGNPAR)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002327 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (termios->c_iflag & IGNBRK) {
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002329 port->ignore_status_mask |= UART_LSR_BI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 /*
2331 * If we're ignoring parity and break indicators,
2332 * ignore overruns too (for real raw support).
2333 */
2334 if (termios->c_iflag & IGNPAR)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002335 port->ignore_status_mask |= UART_LSR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
2337
2338 /*
2339 * ignore all characters if CREAD is not set
2340 */
2341 if ((termios->c_cflag & CREAD) == 0)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002342 port->ignore_status_mask |= UART_LSR_DR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 /*
2345 * CTS flow control flag and modem status interrupts
2346 */
Ingo Molnarf8b372a2010-11-13 16:21:58 +01002347 up->ier &= ~UART_IER_MSI;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002348 if (!(up->bugs & UART_BUG_NOMSR) &&
2349 UART_ENABLE_MS(&up->port, termios->c_cflag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 up->ier |= UART_IER_MSI;
2351 if (up->capabilities & UART_CAP_UUE)
Stephen Warren4539c242011-05-17 16:12:36 -06002352 up->ier |= UART_IER_UUE;
2353 if (up->capabilities & UART_CAP_RTOIE)
2354 up->ier |= UART_IER_RTOIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356 serial_out(up, UART_IER, up->ier);
2357
2358 if (up->capabilities & UART_CAP_EFR) {
2359 unsigned char efr = 0;
2360 /*
2361 * TI16C752/Startech hardware flow control. FIXME:
2362 * - TI16C752 requires control thresholds to be set.
2363 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2364 */
2365 if (termios->c_cflag & CRTSCTS)
2366 efr |= UART_EFR_CTS;
2367
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002368 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002369 if (port->flags & UPF_EXAR_EFR)
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002370 serial_out(up, UART_XR_EFR, efr);
Søren Holm06315342011-09-02 22:55:37 +02002371 else
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002372 serial_out(up, UART_EFR, efr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374
Russell Kingf2eda272008-09-01 21:47:59 +01002375#ifdef CONFIG_ARCH_OMAP
Jonathan McDowell255341c2006-08-14 23:05:32 -07002376 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
Russell King56685452008-09-01 21:25:33 +01002377 if (cpu_is_omap1510() && is_omap_port(up)) {
Jonathan McDowell255341c2006-08-14 23:05:32 -07002378 if (baud == 115200) {
2379 quot = 1;
2380 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2381 } else
2382 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2383 }
2384#endif
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (up->capabilities & UART_NATSEMI) {
2387 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002388 serial_out(up, UART_LCR, 0xe0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 } else {
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002390 serial_out(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
2392
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +01002393 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395 /*
2396 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2397 * is written without DLAB set, this mode will be disabled.
2398 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002399 if (port->type == PORT_16750)
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002400 serial_out(up, UART_FCR, fcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002402 serial_out(up, UART_LCR, cval); /* reset DLAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 up->lcr = cval; /* Save LCR */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002404 if (port->type != PORT_16750) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 if (fcr & UART_FCR_ENABLE_FIFO) {
2406 /* emulated UARTs (Lucent Venus 167x) need two steps */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002407 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 }
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002409 serial_out(up, UART_FCR, fcr); /* set fcr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002411 serial8250_set_mctrl(port, port->mctrl);
2412 spin_unlock_irqrestore(&port->lock, flags);
Alan Coxe991a2b2008-04-28 02:14:06 -07002413 /* Don't rewrite B0 */
2414 if (tty_termios_baud_rate(termios))
2415 tty_termios_encode_baud_rate(termios, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
Philippe Langlais235dae52010-07-29 17:13:57 +02002417EXPORT_SYMBOL(serial8250_do_set_termios);
2418
2419static void
2420serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2421 struct ktermios *old)
2422{
2423 if (port->set_termios)
2424 port->set_termios(port, termios, old);
2425 else
2426 serial8250_do_set_termios(port, termios, old);
2427}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
2429static void
Arnd Bergmanna0821df2010-06-01 22:53:11 +02002430serial8250_set_ldisc(struct uart_port *port, int new)
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002431{
Arnd Bergmanna0821df2010-06-01 22:53:11 +02002432 if (new == N_PPS) {
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002433 port->flags |= UPF_HARDPPS_CD;
2434 serial8250_enable_ms(port);
2435 } else
2436 port->flags &= ~UPF_HARDPPS_CD;
2437}
2438
Manuel Laussc161afe2010-09-25 15:13:45 +02002439
2440void serial8250_do_pm(struct uart_port *port, unsigned int state,
2441 unsigned int oldstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Jamie Iles49d57412010-12-01 23:39:35 +00002443 struct uart_8250_port *p =
2444 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 serial8250_set_sleep(p, state != 0);
Manuel Laussc161afe2010-09-25 15:13:45 +02002447}
2448EXPORT_SYMBOL(serial8250_do_pm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Manuel Laussc161afe2010-09-25 15:13:45 +02002450static void
2451serial8250_pm(struct uart_port *port, unsigned int state,
2452 unsigned int oldstate)
2453{
2454 if (port->pm)
2455 port->pm(port, state, oldstate);
2456 else
2457 serial8250_do_pm(port, state, oldstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
Russell Kingf2eda272008-09-01 21:47:59 +01002460static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2461{
2462 if (pt->port.iotype == UPIO_AU)
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002463 return 0x1000;
Russell Kingf2eda272008-09-01 21:47:59 +01002464#ifdef CONFIG_ARCH_OMAP
2465 if (is_omap_port(pt))
2466 return 0x16 << pt->port.regshift;
2467#endif
2468 return 8 << pt->port.regshift;
2469}
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471/*
2472 * Resource handling.
2473 */
2474static int serial8250_request_std_resource(struct uart_8250_port *up)
2475{
Russell Kingf2eda272008-09-01 21:47:59 +01002476 unsigned int size = serial8250_port_size(up);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002477 struct uart_port *port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 int ret = 0;
2479
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002480 switch (port->iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002481 case UPIO_AU:
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002482 case UPIO_TSI:
2483 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 case UPIO_MEM:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002485 if (!port->mapbase)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 break;
2487
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002488 if (!request_mem_region(port->mapbase, size, "serial")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 ret = -EBUSY;
2490 break;
2491 }
2492
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002493 if (port->flags & UPF_IOREMAP) {
2494 port->membase = ioremap_nocache(port->mapbase, size);
2495 if (!port->membase) {
2496 release_mem_region(port->mapbase, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 ret = -ENOMEM;
2498 }
2499 }
2500 break;
2501
2502 case UPIO_HUB6:
2503 case UPIO_PORT:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002504 if (!request_region(port->iobase, size, "serial"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 ret = -EBUSY;
2506 break;
2507 }
2508 return ret;
2509}
2510
2511static void serial8250_release_std_resource(struct uart_8250_port *up)
2512{
Russell Kingf2eda272008-09-01 21:47:59 +01002513 unsigned int size = serial8250_port_size(up);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002514 struct uart_port *port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002516 switch (port->iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002517 case UPIO_AU:
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002518 case UPIO_TSI:
2519 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 case UPIO_MEM:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002521 if (!port->mapbase)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 break;
2523
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002524 if (port->flags & UPF_IOREMAP) {
2525 iounmap(port->membase);
2526 port->membase = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
2528
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002529 release_mem_region(port->mapbase, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 break;
2531
2532 case UPIO_HUB6:
2533 case UPIO_PORT:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002534 release_region(port->iobase, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 break;
2536 }
2537}
2538
2539static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2540{
2541 unsigned long start = UART_RSA_BASE << up->port.regshift;
2542 unsigned int size = 8 << up->port.regshift;
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002543 struct uart_port *port = &up->port;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002544 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002546 switch (port->iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 case UPIO_HUB6:
2548 case UPIO_PORT:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002549 start += port->iobase;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002550 if (request_region(start, size, "serial-rsa"))
2551 ret = 0;
2552 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 ret = -EBUSY;
2554 break;
2555 }
2556
2557 return ret;
2558}
2559
2560static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2561{
2562 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2563 unsigned int size = 8 << up->port.regshift;
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002564 struct uart_port *port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002566 switch (port->iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 case UPIO_HUB6:
2568 case UPIO_PORT:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002569 release_region(port->iobase + offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 break;
2571 }
2572}
2573
2574static void serial8250_release_port(struct uart_port *port)
2575{
Jamie Iles49d57412010-12-01 23:39:35 +00002576 struct uart_8250_port *up =
2577 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
2579 serial8250_release_std_resource(up);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002580 if (port->type == PORT_RSA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 serial8250_release_rsa_resource(up);
2582}
2583
2584static int serial8250_request_port(struct uart_port *port)
2585{
Jamie Iles49d57412010-12-01 23:39:35 +00002586 struct uart_8250_port *up =
2587 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 int ret = 0;
2589
2590 ret = serial8250_request_std_resource(up);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002591 if (ret == 0 && port->type == PORT_RSA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 ret = serial8250_request_rsa_resource(up);
2593 if (ret < 0)
2594 serial8250_release_std_resource(up);
2595 }
2596
2597 return ret;
2598}
2599
2600static void serial8250_config_port(struct uart_port *port, int flags)
2601{
Jamie Iles49d57412010-12-01 23:39:35 +00002602 struct uart_8250_port *up =
2603 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 int probeflags = PROBE_ANY;
2605 int ret;
2606
2607 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 * Find the region that we can probe for. This in turn
2609 * tells us whether we can probe for the type of port.
2610 */
2611 ret = serial8250_request_std_resource(up);
2612 if (ret < 0)
2613 return;
2614
2615 ret = serial8250_request_rsa_resource(up);
2616 if (ret < 0)
2617 probeflags &= ~PROBE_RSA;
2618
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002619 if (port->iotype != up->cur_iotype)
Alan Coxb8e7e402009-05-28 14:01:35 +01002620 set_io_from_upio(port);
2621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 if (flags & UART_CONFIG_TYPE)
2623 autoconfig(up, probeflags);
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002624
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002625 /* if access method is AU, it is a 16550 with a quirk */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002626 if (port->type == PORT_16550A && port->iotype == UPIO_AU)
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002627 up->bugs |= UART_BUG_NOMSR;
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002628
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002629 if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 autoconfig_irq(up);
2631
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002632 if (port->type != PORT_RSA && probeflags & PROBE_RSA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 serial8250_release_rsa_resource(up);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002634 if (port->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 serial8250_release_std_resource(up);
2636}
2637
2638static int
2639serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2640{
Yinghai Lua62c4132008-08-19 20:49:55 -07002641 if (ser->irq >= nr_irqs || ser->irq < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2643 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2644 ser->type == PORT_STARTECH)
2645 return -EINVAL;
2646 return 0;
2647}
2648
2649static const char *
2650serial8250_type(struct uart_port *port)
2651{
2652 int type = port->type;
2653
2654 if (type >= ARRAY_SIZE(uart_config))
2655 type = 0;
2656 return uart_config[type].name;
2657}
2658
2659static struct uart_ops serial8250_pops = {
2660 .tx_empty = serial8250_tx_empty,
2661 .set_mctrl = serial8250_set_mctrl,
2662 .get_mctrl = serial8250_get_mctrl,
2663 .stop_tx = serial8250_stop_tx,
2664 .start_tx = serial8250_start_tx,
2665 .stop_rx = serial8250_stop_rx,
2666 .enable_ms = serial8250_enable_ms,
2667 .break_ctl = serial8250_break_ctl,
2668 .startup = serial8250_startup,
2669 .shutdown = serial8250_shutdown,
2670 .set_termios = serial8250_set_termios,
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002671 .set_ldisc = serial8250_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 .pm = serial8250_pm,
2673 .type = serial8250_type,
2674 .release_port = serial8250_release_port,
2675 .request_port = serial8250_request_port,
2676 .config_port = serial8250_config_port,
2677 .verify_port = serial8250_verify_port,
Jason Wesself2d937f2008-04-17 20:05:37 +02002678#ifdef CONFIG_CONSOLE_POLL
2679 .poll_get_char = serial8250_get_poll_char,
2680 .poll_put_char = serial8250_put_poll_char,
2681#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682};
2683
2684static struct uart_8250_port serial8250_ports[UART_NR];
2685
Alan Coxaf7f3742010-10-18 11:38:02 -07002686static void (*serial8250_isa_config)(int port, struct uart_port *up,
2687 unsigned short *capabilities);
2688
2689void serial8250_set_isa_configurator(
2690 void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
2691{
2692 serial8250_isa_config = v;
2693}
2694EXPORT_SYMBOL(serial8250_set_isa_configurator);
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696static void __init serial8250_isa_init_ports(void)
2697{
2698 struct uart_8250_port *up;
2699 static int first = 1;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002700 int i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 if (!first)
2703 return;
2704 first = 0;
2705
Dave Jonesa61c2d72006-01-07 23:18:19 +00002706 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 struct uart_8250_port *up = &serial8250_ports[i];
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002708 struct uart_port *port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002710 port->line = i;
2711 spin_lock_init(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
2713 init_timer(&up->timer);
2714 up->timer.function = serial8250_timeout;
2715
2716 /*
2717 * ALPHA_KLUDGE_MCR needs to be killed.
2718 */
2719 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2720 up->mcr_force = ALPHA_KLUDGE_MCR;
2721
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002722 port->ops = &serial8250_pops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
2724
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002725 if (share_irqs)
2726 irqflag = IRQF_SHARED;
2727
Russell King44454bc2005-06-30 22:41:22 +01002728 for (i = 0, up = serial8250_ports;
Dave Jonesa61c2d72006-01-07 23:18:19 +00002729 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 i++, up++) {
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002731 struct uart_port *port = &up->port;
2732
2733 port->iobase = old_serial_port[i].port;
2734 port->irq = irq_canonicalize(old_serial_port[i].irq);
2735 port->irqflags = old_serial_port[i].irqflags;
2736 port->uartclk = old_serial_port[i].baud_base * 16;
2737 port->flags = old_serial_port[i].flags;
2738 port->hub6 = old_serial_port[i].hub6;
2739 port->membase = old_serial_port[i].iomem_base;
2740 port->iotype = old_serial_port[i].io_type;
2741 port->regshift = old_serial_port[i].iomem_reg_shift;
2742 set_io_from_upio(port);
2743 port->irqflags |= irqflag;
Alan Coxaf7f3742010-10-18 11:38:02 -07002744 if (serial8250_isa_config != NULL)
2745 serial8250_isa_config(i, &up->port, &up->capabilities);
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
2748}
2749
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08002750static void
2751serial8250_init_fixed_type_port(struct uart_8250_port *up, unsigned int type)
2752{
2753 up->port.type = type;
2754 up->port.fifosize = uart_config[type].fifo_size;
2755 up->capabilities = uart_config[type].flags;
2756 up->tx_loadsz = uart_config[type].tx_loadsz;
2757}
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759static void __init
2760serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2761{
2762 int i;
2763
Alan Coxb8e7e402009-05-28 14:01:35 +01002764 for (i = 0; i < nr_uarts; i++) {
2765 struct uart_8250_port *up = &serial8250_ports[i];
2766 up->cur_iotype = 0xFF;
2767 }
2768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 serial8250_isa_init_ports();
2770
Dave Jonesa61c2d72006-01-07 23:18:19 +00002771 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 struct uart_8250_port *up = &serial8250_ports[i];
2773
2774 up->port.dev = dev;
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08002775
2776 if (up->port.flags & UPF_FIXED_TYPE)
2777 serial8250_init_fixed_type_port(up, up->port.type);
2778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 uart_add_one_port(drv, &up->port);
2780 }
2781}
2782
2783#ifdef CONFIG_SERIAL_8250_CONSOLE
2784
Russell Kingd3587882006-03-20 20:00:09 +00002785static void serial8250_console_putchar(struct uart_port *port, int ch)
2786{
Jamie Iles49d57412010-12-01 23:39:35 +00002787 struct uart_8250_port *up =
2788 container_of(port, struct uart_8250_port, port);
Russell Kingd3587882006-03-20 20:00:09 +00002789
2790 wait_for_xmitr(up, UART_LSR_THRE);
2791 serial_out(up, UART_TX, ch);
2792}
2793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794/*
2795 * Print a string to the serial port trying not to disturb
2796 * any possible real use of the port...
2797 *
2798 * The console_lock must be held when we get here.
2799 */
2800static void
2801serial8250_console_write(struct console *co, const char *s, unsigned int count)
2802{
2803 struct uart_8250_port *up = &serial8250_ports[co->index];
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002804 struct uart_port *port = &up->port;
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002805 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 unsigned int ier;
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002807 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Andrew Morton78512ec2005-11-07 00:59:13 -08002809 touch_nmi_watchdog();
2810
Andrew Morton68aa2c02006-06-30 02:29:59 -07002811 local_irq_save(flags);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002812 if (port->sysrq) {
Paul Gortmaker86b21192011-12-04 18:42:22 -05002813 /* serial8250_handle_irq() already took the lock */
Andrew Morton68aa2c02006-06-30 02:29:59 -07002814 locked = 0;
2815 } else if (oops_in_progress) {
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002816 locked = spin_trylock(&port->lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002817 } else
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002818 spin_lock(&port->lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 /*
Ralf Baechledc7bf132006-02-15 09:59:47 +00002821 * First save the IER then disable the interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 */
2823 ier = serial_in(up, UART_IER);
2824
2825 if (up->capabilities & UART_CAP_UUE)
2826 serial_out(up, UART_IER, UART_IER_UUE);
2827 else
2828 serial_out(up, UART_IER, 0);
2829
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002830 uart_console_write(port, s, count, serial8250_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
2832 /*
2833 * Finally, wait for transmitter to become empty
2834 * and restore the IER
2835 */
Alan Coxf91a3712006-01-21 14:59:12 +00002836 wait_for_xmitr(up, BOTH_EMPTY);
Russell Kinga88d75b2006-04-30 11:30:15 +01002837 serial_out(up, UART_IER, ier);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002838
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002839 /*
2840 * The receive handling will happen properly because the
2841 * receive ready bit will still be set; it is not cleared
2842 * on read. However, modem control will not, we must
2843 * call it if we have saved something in the saved flags
2844 * while processing with interrupts off.
2845 */
2846 if (up->msr_saved_flags)
Paul Gortmaker3986fb22011-12-04 18:42:20 -05002847 serial8250_modem_status(up);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002848
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002849 if (locked)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002850 spin_unlock(&port->lock);
Andrew Morton68aa2c02006-06-30 02:29:59 -07002851 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852}
2853
Vivek Goyal118c0ac2007-01-11 01:52:44 +01002854static int __init serial8250_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 struct uart_port *port;
2857 int baud = 9600;
2858 int bits = 8;
2859 int parity = 'n';
2860 int flow = 'n';
2861
2862 /*
2863 * Check whether an invalid uart number has been specified, and
2864 * if so, search for the first available port that does have
2865 * console support.
2866 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002867 if (co->index >= nr_uarts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 co->index = 0;
2869 port = &serial8250_ports[co->index].port;
2870 if (!port->iobase && !port->membase)
2871 return -ENODEV;
2872
2873 if (options)
2874 uart_parse_options(options, &baud, &parity, &bits, &flow);
2875
2876 return uart_set_options(port, co, baud, parity, bits, flow);
2877}
2878
Daniel Ritzb6b1d872007-08-03 16:07:43 +02002879static int serial8250_console_early_setup(void)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002880{
2881 return serial8250_find_port_for_earlycon();
2882}
2883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884static struct console serial8250_console = {
2885 .name = "ttyS",
2886 .write = serial8250_console_write,
2887 .device = uart_console_device,
2888 .setup = serial8250_console_setup,
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002889 .early_setup = serial8250_console_early_setup,
Peter Zijlstraa80c49d2010-11-15 21:11:12 +01002890 .flags = CON_PRINTBUFFER | CON_ANYTIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 .index = -1,
2892 .data = &serial8250_reg,
2893};
2894
2895static int __init serial8250_console_init(void)
2896{
Eric W. Biederman05d81d22008-07-12 13:47:53 -07002897 if (nr_uarts > UART_NR)
2898 nr_uarts = UART_NR;
2899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 serial8250_isa_init_ports();
2901 register_console(&serial8250_console);
2902 return 0;
2903}
2904console_initcall(serial8250_console_init);
2905
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002906int serial8250_find_port(struct uart_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
2908 int line;
2909 struct uart_port *port;
2910
Dave Jonesa61c2d72006-01-07 23:18:19 +00002911 for (line = 0; line < nr_uarts; line++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 port = &serial8250_ports[line].port;
Russell King50aec3b52006-01-04 18:13:03 +00002913 if (uart_match_port(p, port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 return line;
2915 }
2916 return -ENODEV;
2917}
2918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919#define SERIAL8250_CONSOLE &serial8250_console
2920#else
2921#define SERIAL8250_CONSOLE NULL
2922#endif
2923
2924static struct uart_driver serial8250_reg = {
2925 .owner = THIS_MODULE,
2926 .driver_name = "serial",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 .dev_name = "ttyS",
2928 .major = TTY_MAJOR,
2929 .minor = 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 .cons = SERIAL8250_CONSOLE,
2931};
2932
Russell Kingd856c662006-02-23 10:22:13 +00002933/*
2934 * early_serial_setup - early registration for 8250 ports
2935 *
2936 * Setup an 8250 port structure prior to console initialisation. Use
2937 * after console initialisation will cause undefined behaviour.
2938 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939int __init early_serial_setup(struct uart_port *port)
2940{
David Daneyb4304282009-01-02 13:49:41 +00002941 struct uart_port *p;
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 if (port->line >= ARRAY_SIZE(serial8250_ports))
2944 return -ENODEV;
2945
2946 serial8250_isa_init_ports();
David Daneyb4304282009-01-02 13:49:41 +00002947 p = &serial8250_ports[port->line].port;
2948 p->iobase = port->iobase;
2949 p->membase = port->membase;
2950 p->irq = port->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002951 p->irqflags = port->irqflags;
David Daneyb4304282009-01-02 13:49:41 +00002952 p->uartclk = port->uartclk;
2953 p->fifosize = port->fifosize;
2954 p->regshift = port->regshift;
2955 p->iotype = port->iotype;
2956 p->flags = port->flags;
2957 p->mapbase = port->mapbase;
2958 p->private_data = port->private_data;
Helge Deller125c97d2009-01-13 22:51:07 +01002959 p->type = port->type;
2960 p->line = port->line;
David Daney7d6a07d2009-01-02 13:49:47 +00002961
2962 set_io_from_upio(p);
2963 if (port->serial_in)
2964 p->serial_in = port->serial_in;
2965 if (port->serial_out)
2966 p->serial_out = port->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01002967 if (port->handle_irq)
2968 p->handle_irq = port->handle_irq;
2969 else
2970 p->handle_irq = serial8250_default_handle_irq;
David Daney7d6a07d2009-01-02 13:49:47 +00002971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return 0;
2973}
2974
2975/**
2976 * serial8250_suspend_port - suspend one serial port
2977 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 *
2979 * Suspend one serial port.
2980 */
2981void serial8250_suspend_port(int line)
2982{
2983 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2984}
2985
2986/**
2987 * serial8250_resume_port - resume one serial port
2988 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 *
2990 * Resume one serial port.
2991 */
2992void serial8250_resume_port(int line)
2993{
David Woodhouseb5b82df2007-05-17 14:27:39 +08002994 struct uart_8250_port *up = &serial8250_ports[line];
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002995 struct uart_port *port = &up->port;
David Woodhouseb5b82df2007-05-17 14:27:39 +08002996
2997 if (up->capabilities & UART_NATSEMI) {
David Woodhouseb5b82df2007-05-17 14:27:39 +08002998 /* Ensure it's still in high speed mode */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002999 serial_out(up, UART_LCR, 0xE0);
David Woodhouseb5b82df2007-05-17 14:27:39 +08003000
Yin Kangkai0d0389e2011-02-09 11:35:18 +08003001 ns16550a_goto_highspeed(up);
David Woodhouseb5b82df2007-05-17 14:27:39 +08003002
Paul Gortmaker0acf5192012-03-08 19:12:08 -05003003 serial_out(up, UART_LCR, 0);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05003004 port->uartclk = 921600*16;
David Woodhouseb5b82df2007-05-17 14:27:39 +08003005 }
Paul Gortmakerdfe42442012-03-08 19:12:11 -05003006 uart_resume_port(&serial8250_reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007}
3008
3009/*
3010 * Register a set of serial devices attached to a platform device. The
3011 * list is terminated with a zero flags entry, which means we expect
3012 * all entries to have at least UPF_BOOT_AUTOCONF set.
3013 */
Russell King3ae5eae2005-11-09 22:32:44 +00003014static int __devinit serial8250_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
Russell King3ae5eae2005-11-09 22:32:44 +00003016 struct plat_serial8250_port *p = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 struct uart_port port;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003018 int ret, i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
3020 memset(&port, 0, sizeof(struct uart_port));
3021
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003022 if (share_irqs)
3023 irqflag = IRQF_SHARED;
3024
Russell Kingec9f47c2005-06-27 11:12:54 +01003025 for (i = 0; p && p->flags != 0; p++, i++) {
Will Newton74a197412008-02-04 22:27:50 -08003026 port.iobase = p->iobase;
3027 port.membase = p->membase;
3028 port.irq = p->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07003029 port.irqflags = p->irqflags;
Will Newton74a197412008-02-04 22:27:50 -08003030 port.uartclk = p->uartclk;
3031 port.regshift = p->regshift;
3032 port.iotype = p->iotype;
3033 port.flags = p->flags;
3034 port.mapbase = p->mapbase;
3035 port.hub6 = p->hub6;
3036 port.private_data = p->private_data;
David Daney8e23fcc2009-01-02 13:49:54 +00003037 port.type = p->type;
David Daney7d6a07d2009-01-02 13:49:47 +00003038 port.serial_in = p->serial_in;
3039 port.serial_out = p->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01003040 port.handle_irq = p->handle_irq;
Philippe Langlais235dae52010-07-29 17:13:57 +02003041 port.set_termios = p->set_termios;
Manuel Laussc161afe2010-09-25 15:13:45 +02003042 port.pm = p->pm;
Will Newton74a197412008-02-04 22:27:50 -08003043 port.dev = &dev->dev;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003044 port.irqflags |= irqflag;
Russell Kingec9f47c2005-06-27 11:12:54 +01003045 ret = serial8250_register_port(&port);
3046 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00003047 dev_err(&dev->dev, "unable to register port at index %d "
Josh Boyer4f640ef2007-07-23 18:43:44 -07003048 "(IO%lx MEM%llx IRQ%d): %d\n", i,
3049 p->iobase, (unsigned long long)p->mapbase,
3050 p->irq, ret);
Russell Kingec9f47c2005-06-27 11:12:54 +01003051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 }
3053 return 0;
3054}
3055
3056/*
3057 * Remove serial ports registered against a platform device.
3058 */
Russell King3ae5eae2005-11-09 22:32:44 +00003059static int __devexit serial8250_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
3061 int i;
3062
Dave Jonesa61c2d72006-01-07 23:18:19 +00003063 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 struct uart_8250_port *up = &serial8250_ports[i];
3065
Russell King3ae5eae2005-11-09 22:32:44 +00003066 if (up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 serial8250_unregister_port(i);
3068 }
3069 return 0;
3070}
3071
Russell King3ae5eae2005-11-09 22:32:44 +00003072static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
3074 int i;
3075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 for (i = 0; i < UART_NR; i++) {
3077 struct uart_8250_port *up = &serial8250_ports[i];
3078
Russell King3ae5eae2005-11-09 22:32:44 +00003079 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 uart_suspend_port(&serial8250_reg, &up->port);
3081 }
3082
3083 return 0;
3084}
3085
Russell King3ae5eae2005-11-09 22:32:44 +00003086static int serial8250_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
3088 int i;
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 for (i = 0; i < UART_NR; i++) {
3091 struct uart_8250_port *up = &serial8250_ports[i];
3092
Russell King3ae5eae2005-11-09 22:32:44 +00003093 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
David Woodhouseb5b82df2007-05-17 14:27:39 +08003094 serial8250_resume_port(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 }
3096
3097 return 0;
3098}
3099
Russell King3ae5eae2005-11-09 22:32:44 +00003100static struct platform_driver serial8250_isa_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 .probe = serial8250_probe,
3102 .remove = __devexit_p(serial8250_remove),
3103 .suspend = serial8250_suspend,
3104 .resume = serial8250_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00003105 .driver = {
3106 .name = "serial8250",
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003107 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00003108 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109};
3110
3111/*
3112 * This "device" covers _all_ ISA 8250-compatible serial devices listed
3113 * in the table in include/asm/serial.h
3114 */
3115static struct platform_device *serial8250_isa_devs;
3116
3117/*
3118 * serial8250_register_port and serial8250_unregister_port allows for
3119 * 16x50 serial ports to be configured at run-time, to support PCMCIA
3120 * modems and PCI multiport cards.
3121 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003122static DEFINE_MUTEX(serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
3124static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
3125{
3126 int i;
3127
3128 /*
3129 * First, find a port entry which matches.
3130 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003131 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 if (uart_match_port(&serial8250_ports[i].port, port))
3133 return &serial8250_ports[i];
3134
3135 /*
3136 * We didn't find a matching entry, so look for the first
3137 * free entry. We look for one which hasn't been previously
3138 * used (indicated by zero iobase).
3139 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003140 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
3142 serial8250_ports[i].port.iobase == 0)
3143 return &serial8250_ports[i];
3144
3145 /*
3146 * That also failed. Last resort is to find any entry which
3147 * doesn't have a real port associated with it.
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 (serial8250_ports[i].port.type == PORT_UNKNOWN)
3151 return &serial8250_ports[i];
3152
3153 return NULL;
3154}
3155
3156/**
3157 * serial8250_register_port - register a serial port
3158 * @port: serial port template
3159 *
3160 * Configure the serial port specified by the request. If the
3161 * port exists and is in use, it is hung up and unregistered
3162 * first.
3163 *
3164 * The port is then probed and if necessary the IRQ is autodetected
3165 * If this fails an error is returned.
3166 *
3167 * On success the port is ready to use and the line number is returned.
3168 */
3169int serial8250_register_port(struct uart_port *port)
3170{
3171 struct uart_8250_port *uart;
3172 int ret = -ENOSPC;
3173
3174 if (port->uartclk == 0)
3175 return -EINVAL;
3176
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003177 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
3179 uart = serial8250_find_match_or_unused(port);
3180 if (uart) {
3181 uart_remove_one_port(&serial8250_reg, &uart->port);
3182
Will Newton74a197412008-02-04 22:27:50 -08003183 uart->port.iobase = port->iobase;
3184 uart->port.membase = port->membase;
3185 uart->port.irq = port->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07003186 uart->port.irqflags = port->irqflags;
Will Newton74a197412008-02-04 22:27:50 -08003187 uart->port.uartclk = port->uartclk;
3188 uart->port.fifosize = port->fifosize;
3189 uart->port.regshift = port->regshift;
3190 uart->port.iotype = port->iotype;
3191 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
3192 uart->port.mapbase = port->mapbase;
3193 uart->port.private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 if (port->dev)
3195 uart->port.dev = port->dev;
David Daney8e23fcc2009-01-02 13:49:54 +00003196
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08003197 if (port->flags & UPF_FIXED_TYPE)
3198 serial8250_init_fixed_type_port(uart, port->type);
David Daney8e23fcc2009-01-02 13:49:54 +00003199
David Daney7d6a07d2009-01-02 13:49:47 +00003200 set_io_from_upio(&uart->port);
3201 /* Possibly override default I/O functions. */
3202 if (port->serial_in)
3203 uart->port.serial_in = port->serial_in;
3204 if (port->serial_out)
3205 uart->port.serial_out = port->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01003206 if (port->handle_irq)
3207 uart->port.handle_irq = port->handle_irq;
Philippe Langlais235dae52010-07-29 17:13:57 +02003208 /* Possibly override set_termios call */
3209 if (port->set_termios)
3210 uart->port.set_termios = port->set_termios;
Manuel Laussc161afe2010-09-25 15:13:45 +02003211 if (port->pm)
3212 uart->port.pm = port->pm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Alan Coxaf7f3742010-10-18 11:38:02 -07003214 if (serial8250_isa_config != NULL)
3215 serial8250_isa_config(0, &uart->port,
3216 &uart->capabilities);
3217
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 ret = uart_add_one_port(&serial8250_reg, &uart->port);
3219 if (ret == 0)
3220 ret = uart->port.line;
3221 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003222 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
3224 return ret;
3225}
3226EXPORT_SYMBOL(serial8250_register_port);
3227
3228/**
3229 * serial8250_unregister_port - remove a 16x50 serial port at runtime
3230 * @line: serial line number
3231 *
3232 * Remove one serial port. This may not be called from interrupt
3233 * context. We hand the port back to the our control.
3234 */
3235void serial8250_unregister_port(int line)
3236{
3237 struct uart_8250_port *uart = &serial8250_ports[line];
3238
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003239 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 uart_remove_one_port(&serial8250_reg, &uart->port);
3241 if (serial8250_isa_devs) {
3242 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
3243 uart->port.type = PORT_UNKNOWN;
3244 uart->port.dev = &serial8250_isa_devs->dev;
leitao@linux.vnet.ibm.comcb01ece2011-05-26 11:18:39 -03003245 uart->capabilities = uart_config[uart->port.type].flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 uart_add_one_port(&serial8250_reg, &uart->port);
3247 } else {
3248 uart->port.dev = NULL;
3249 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003250 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251}
3252EXPORT_SYMBOL(serial8250_unregister_port);
3253
3254static int __init serial8250_init(void)
3255{
Alan Cox25db8ad2008-08-19 20:49:40 -07003256 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Dave Jonesa61c2d72006-01-07 23:18:19 +00003258 if (nr_uarts > UART_NR)
3259 nr_uarts = UART_NR;
3260
Paul Bollef1fb9bb2008-12-30 14:06:43 +01003261 printk(KERN_INFO "Serial: 8250/16550 driver, "
Dave Jonesa61c2d72006-01-07 23:18:19 +00003262 "%d ports, IRQ sharing %sabled\n", nr_uarts,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 share_irqs ? "en" : "dis");
3264
David Millerb70ac772008-10-13 10:36:31 +01003265#ifdef CONFIG_SPARC
3266 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
3267#else
3268 serial8250_reg.nr = UART_NR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 ret = uart_register_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003270#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 if (ret)
3272 goto out;
3273
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003274 serial8250_isa_devs = platform_device_alloc("serial8250",
3275 PLAT8250_DEV_LEGACY);
3276 if (!serial8250_isa_devs) {
3277 ret = -ENOMEM;
Russell Kingbc965a72006-01-18 09:54:29 +00003278 goto unreg_uart_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 }
3280
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003281 ret = platform_device_add(serial8250_isa_devs);
3282 if (ret)
3283 goto put_dev;
3284
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
3286
Russell Kingbc965a72006-01-18 09:54:29 +00003287 ret = platform_driver_register(&serial8250_isa_driver);
3288 if (ret == 0)
3289 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Russell Kingbc965a72006-01-18 09:54:29 +00003291 platform_device_del(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003292put_dev:
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003293 platform_device_put(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003294unreg_uart_drv:
David Millerb70ac772008-10-13 10:36:31 +01003295#ifdef CONFIG_SPARC
3296 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3297#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003299#endif
Alan Cox25db8ad2008-08-19 20:49:40 -07003300out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 return ret;
3302}
3303
3304static void __exit serial8250_exit(void)
3305{
3306 struct platform_device *isa_dev = serial8250_isa_devs;
3307
3308 /*
3309 * This tells serial8250_unregister_port() not to re-register
3310 * the ports (thereby making serial8250_isa_driver permanently
3311 * in use.)
3312 */
3313 serial8250_isa_devs = NULL;
3314
Russell King3ae5eae2005-11-09 22:32:44 +00003315 platform_driver_unregister(&serial8250_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 platform_device_unregister(isa_dev);
3317
David Millerb70ac772008-10-13 10:36:31 +01003318#ifdef CONFIG_SPARC
3319 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3320#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003322#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323}
3324
3325module_init(serial8250_init);
3326module_exit(serial8250_exit);
3327
3328EXPORT_SYMBOL(serial8250_suspend_port);
3329EXPORT_SYMBOL(serial8250_resume_port);
3330
3331MODULE_LICENSE("GPL");
Adrian Bunkd87a6d92008-07-16 21:53:31 +01003332MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
3334module_param(share_irqs, uint, 0644);
3335MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
3336 " (unsafe)");
3337
Dave Jonesa61c2d72006-01-07 23:18:19 +00003338module_param(nr_uarts, uint, 0644);
3339MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
3340
Chuck Ebbertd41a4b52009-10-01 15:44:26 -07003341module_param(skip_txen_test, uint, 0644);
3342MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344#ifdef CONFIG_SERIAL_8250_RSA
3345module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
3346MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
3347#endif
3348MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);