blob: 917ab8452746c9a3476530e62aefa2c2b5726cc2 [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
David Daney7d6a07d2009-01-02 13:49:47 +0000485#define serial_in(up, offset) \
486 (up->port.serial_in(&(up)->port, (offset)))
487#define serial_out(up, offset, value) \
488 (up->port.serial_out(&(up)->port, (offset), (value)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/*
490 * We used to support using pause I/O for certain machines. We
491 * haven't supported this for a while, but just in case it's badly
492 * needed for certain old 386 machines, I've left these #define's
493 * in....
494 */
495#define serial_inp(up, offset) serial_in(up, offset)
496#define serial_outp(up, offset, value) serial_out(up, offset, value)
497
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100498/* Uart divisor latch read */
499static inline int _serial_dl_read(struct uart_8250_port *up)
500{
501 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
502}
503
504/* Uart divisor latch write */
505static inline void _serial_dl_write(struct uart_8250_port *up, int value)
506{
507 serial_outp(up, UART_DLL, value & 0xff);
508 serial_outp(up, UART_DLM, value >> 8 & 0xff);
509}
510
Manuel Lauss12bf3f22010-07-15 21:45:05 +0200511#if defined(CONFIG_MIPS_ALCHEMY)
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100512/* Au1x00 haven't got a standard divisor latch */
513static int serial_dl_read(struct uart_8250_port *up)
514{
515 if (up->port.iotype == UPIO_AU)
516 return __raw_readl(up->port.membase + 0x28);
517 else
518 return _serial_dl_read(up);
519}
520
521static void serial_dl_write(struct uart_8250_port *up, int value)
522{
523 if (up->port.iotype == UPIO_AU)
524 __raw_writel(value, up->port.membase + 0x28);
525 else
526 _serial_dl_write(up, value);
527}
Alan Cox6f803cd2008-02-08 04:18:52 -0800528#elif defined(CONFIG_SERIAL_8250_RM9K)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700529static int serial_dl_read(struct uart_8250_port *up)
530{
531 return (up->port.iotype == UPIO_RM9000) ?
532 (((__raw_readl(up->port.membase + 0x10) << 8) |
533 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
534 _serial_dl_read(up);
535}
536
537static void serial_dl_write(struct uart_8250_port *up, int value)
538{
539 if (up->port.iotype == UPIO_RM9000) {
540 __raw_writel(value, up->port.membase + 0x08);
541 __raw_writel(value >> 8, up->port.membase + 0x10);
542 } else {
543 _serial_dl_write(up, value);
544 }
545}
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100546#else
547#define serial_dl_read(up) _serial_dl_read(up)
548#define serial_dl_write(up, value) _serial_dl_write(up, value)
549#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551/*
552 * For the 16C950
553 */
554static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
555{
556 serial_out(up, UART_SCR, offset);
557 serial_out(up, UART_ICR, value);
558}
559
560static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
561{
562 unsigned int value;
563
564 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
565 serial_out(up, UART_SCR, offset);
566 value = serial_in(up, UART_ICR);
567 serial_icr_write(up, UART_ACR, up->acr);
568
569 return value;
570}
571
572/*
573 * FIFO support.
574 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100575static void serial8250_clear_fifos(struct uart_8250_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 if (p->capabilities & UART_CAP_FIFO) {
578 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
579 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
580 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
581 serial_outp(p, UART_FCR, 0);
582 }
583}
584
585/*
586 * IER sleep support. UARTs which have EFRs need the "extended
587 * capability" bit enabled. Note that on XR16C850s, we need to
588 * reset LCR to write to IER.
589 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100590static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 if (p->capabilities & UART_CAP_SLEEP) {
593 if (p->capabilities & UART_CAP_EFR) {
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800594 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 serial_outp(p, UART_EFR, UART_EFR_ECB);
596 serial_outp(p, UART_LCR, 0);
597 }
598 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
599 if (p->capabilities & UART_CAP_EFR) {
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800600 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 serial_outp(p, UART_EFR, 0);
602 serial_outp(p, UART_LCR, 0);
603 }
604 }
605}
606
607#ifdef CONFIG_SERIAL_8250_RSA
608/*
609 * Attempts to turn on the RSA FIFO. Returns zero on failure.
610 * We set the port uart clock rate if we succeed.
611 */
612static int __enable_rsa(struct uart_8250_port *up)
613{
614 unsigned char mode;
615 int result;
616
617 mode = serial_inp(up, UART_RSA_MSR);
618 result = mode & UART_RSA_MSR_FIFO;
619
620 if (!result) {
621 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
622 mode = serial_inp(up, UART_RSA_MSR);
623 result = mode & UART_RSA_MSR_FIFO;
624 }
625
626 if (result)
627 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
628
629 return result;
630}
631
632static void enable_rsa(struct uart_8250_port *up)
633{
634 if (up->port.type == PORT_RSA) {
635 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
636 spin_lock_irq(&up->port.lock);
637 __enable_rsa(up);
638 spin_unlock_irq(&up->port.lock);
639 }
640 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
641 serial_outp(up, UART_RSA_FRR, 0);
642 }
643}
644
645/*
646 * Attempts to turn off the RSA FIFO. Returns zero on failure.
647 * It is unknown why interrupts were disabled in here. However,
648 * the caller is expected to preserve this behaviour by grabbing
649 * the spinlock before calling this function.
650 */
651static void disable_rsa(struct uart_8250_port *up)
652{
653 unsigned char mode;
654 int result;
655
656 if (up->port.type == PORT_RSA &&
657 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
658 spin_lock_irq(&up->port.lock);
659
660 mode = serial_inp(up, UART_RSA_MSR);
661 result = !(mode & UART_RSA_MSR_FIFO);
662
663 if (!result) {
664 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
665 mode = serial_inp(up, UART_RSA_MSR);
666 result = !(mode & UART_RSA_MSR_FIFO);
667 }
668
669 if (result)
670 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
671 spin_unlock_irq(&up->port.lock);
672 }
673}
674#endif /* CONFIG_SERIAL_8250_RSA */
675
676/*
677 * This is a quickie test to see how big the FIFO is.
678 * It doesn't work at all the time, more's the pity.
679 */
680static int size_fifo(struct uart_8250_port *up)
681{
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100682 unsigned char old_fcr, old_mcr, old_lcr;
683 unsigned short old_dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 int count;
685
686 old_lcr = serial_inp(up, UART_LCR);
687 serial_outp(up, UART_LCR, 0);
688 old_fcr = serial_inp(up, UART_FCR);
689 old_mcr = serial_inp(up, UART_MCR);
690 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
691 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
692 serial_outp(up, UART_MCR, UART_MCR_LOOP);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800693 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100694 old_dl = serial_dl_read(up);
695 serial_dl_write(up, 0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 serial_outp(up, UART_LCR, 0x03);
697 for (count = 0; count < 256; count++)
698 serial_outp(up, UART_TX, count);
699 mdelay(20);/* FIXME - schedule_timeout */
700 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
701 (count < 256); count++)
702 serial_inp(up, UART_RX);
703 serial_outp(up, UART_FCR, old_fcr);
704 serial_outp(up, UART_MCR, old_mcr);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800705 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100706 serial_dl_write(up, old_dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 serial_outp(up, UART_LCR, old_lcr);
708
709 return count;
710}
711
712/*
713 * Read UART ID using the divisor method - set DLL and DLM to zero
714 * and the revision will be in DLL and device type in DLM. We
715 * preserve the device state across this.
716 */
717static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
718{
719 unsigned char old_dll, old_dlm, old_lcr;
720 unsigned int id;
721
722 old_lcr = serial_inp(p, UART_LCR);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800723 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 old_dll = serial_inp(p, UART_DLL);
726 old_dlm = serial_inp(p, UART_DLM);
727
728 serial_outp(p, UART_DLL, 0);
729 serial_outp(p, UART_DLM, 0);
730
731 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
732
733 serial_outp(p, UART_DLL, old_dll);
734 serial_outp(p, UART_DLM, old_dlm);
735 serial_outp(p, UART_LCR, old_lcr);
736
737 return id;
738}
739
740/*
741 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
742 * When this function is called we know it is at least a StarTech
743 * 16650 V2, but it might be one of several StarTech UARTs, or one of
744 * its clones. (We treat the broken original StarTech 16650 V1 as a
745 * 16550, and why not? Startech doesn't seem to even acknowledge its
746 * existence.)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700747 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * What evil have men's minds wrought...
749 */
750static void autoconfig_has_efr(struct uart_8250_port *up)
751{
752 unsigned int id1, id2, id3, rev;
753
754 /*
755 * Everything with an EFR has SLEEP
756 */
757 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
758
759 /*
760 * First we check to see if it's an Oxford Semiconductor UART.
761 *
762 * If we have to do this here because some non-National
763 * Semiconductor clone chips lock up if you try writing to the
764 * LSR register (which serial_icr_read does)
765 */
766
767 /*
768 * Check for Oxford Semiconductor 16C950.
769 *
770 * EFR [4] must be set else this test fails.
771 *
772 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
773 * claims that it's needed for 952 dual UART's (which are not
774 * recommended for new designs).
775 */
776 up->acr = 0;
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800777 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 serial_out(up, UART_EFR, UART_EFR_ECB);
779 serial_out(up, UART_LCR, 0x00);
780 id1 = serial_icr_read(up, UART_ID1);
781 id2 = serial_icr_read(up, UART_ID2);
782 id3 = serial_icr_read(up, UART_ID3);
783 rev = serial_icr_read(up, UART_REV);
784
785 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
786
787 if (id1 == 0x16 && id2 == 0xC9 &&
788 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
789 up->port.type = PORT_16C950;
Russell King4ba5e352005-06-23 10:43:04 +0100790
791 /*
792 * Enable work around for the Oxford Semiconductor 952 rev B
793 * chip which causes it to seriously miscalculate baud rates
794 * when DLL is 0.
795 */
796 if (id3 == 0x52 && rev == 0x01)
797 up->bugs |= UART_BUG_QUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return;
799 }
Thomas Koellerbd71c182007-05-06 14:48:47 -0700800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /*
802 * We check for a XR16C850 by setting DLL and DLM to 0, and then
803 * reading back DLL and DLM. The chip type depends on the DLM
804 * value read back:
805 * 0x10 - XR16C850 and the DLL contains the chip revision.
806 * 0x12 - XR16C2850.
807 * 0x14 - XR16C854.
808 */
809 id1 = autoconfig_read_divisor_id(up);
810 DEBUG_AUTOCONF("850id=%04x ", id1);
811
812 id2 = id1 >> 8;
813 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 up->port.type = PORT_16850;
815 return;
816 }
817
818 /*
819 * It wasn't an XR16C850.
820 *
821 * We distinguish between the '654 and the '650 by counting
822 * how many bytes are in the FIFO. I'm using this for now,
823 * since that's the technique that was sent to me in the
824 * serial driver update, but I'm not convinced this works.
825 * I've had problems doing this in the past. -TYT
826 */
827 if (size_fifo(up) == 64)
828 up->port.type = PORT_16654;
829 else
830 up->port.type = PORT_16650V2;
831}
832
833/*
834 * We detected a chip without a FIFO. Only two fall into
835 * this category - the original 8250 and the 16450. The
836 * 16450 has a scratch register (accessible with LCR=0)
837 */
838static void autoconfig_8250(struct uart_8250_port *up)
839{
840 unsigned char scratch, status1, status2;
841
842 up->port.type = PORT_8250;
843
844 scratch = serial_in(up, UART_SCR);
845 serial_outp(up, UART_SCR, 0xa5);
846 status1 = serial_in(up, UART_SCR);
847 serial_outp(up, UART_SCR, 0x5a);
848 status2 = serial_in(up, UART_SCR);
849 serial_outp(up, UART_SCR, scratch);
850
851 if (status1 == 0xa5 && status2 == 0x5a)
852 up->port.type = PORT_16450;
853}
854
855static int broken_efr(struct uart_8250_port *up)
856{
857 /*
858 * Exar ST16C2550 "A2" devices incorrectly detect as
859 * having an EFR, and report an ID of 0x0201. See
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200860 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 */
862 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
863 return 1;
864
865 return 0;
866}
867
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800868static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
869{
870 unsigned char status;
871
872 status = serial_in(up, 0x04); /* EXCR2 */
873#define PRESL(x) ((x) & 0x30)
874 if (PRESL(status) == 0x10) {
875 /* already in high speed mode */
876 return 0;
877 } else {
878 status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
879 status |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
880 serial_outp(up, 0x04, status);
881 }
882 return 1;
883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885/*
886 * We know that the chip has FIFOs. Does it have an EFR? The
887 * EFR is located in the same register position as the IIR and
888 * we know the top two bits of the IIR are currently set. The
889 * EFR should contain zero. Try to read the EFR.
890 */
891static void autoconfig_16550a(struct uart_8250_port *up)
892{
893 unsigned char status1, status2;
894 unsigned int iersave;
895
896 up->port.type = PORT_16550A;
897 up->capabilities |= UART_CAP_FIFO;
898
899 /*
900 * Check for presence of the EFR when DLAB is set.
901 * Only ST16C650V1 UARTs pass this test.
902 */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800903 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (serial_in(up, UART_EFR) == 0) {
905 serial_outp(up, UART_EFR, 0xA8);
906 if (serial_in(up, UART_EFR) != 0) {
907 DEBUG_AUTOCONF("EFRv1 ");
908 up->port.type = PORT_16650;
909 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
910 } else {
911 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
912 }
913 serial_outp(up, UART_EFR, 0);
914 return;
915 }
916
917 /*
918 * Maybe it requires 0xbf to be written to the LCR.
919 * (other ST16C650V2 UARTs, TI16C752A, etc)
920 */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800921 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
923 DEBUG_AUTOCONF("EFRv2 ");
924 autoconfig_has_efr(up);
925 return;
926 }
927
928 /*
929 * Check for a National Semiconductor SuperIO chip.
930 * Attempt to switch to bank 2, read the value of the LOOP bit
931 * from EXCR1. Switch back to bank 0, change it in MCR. Then
932 * switch back to bank 2, read it from EXCR1 again and check
933 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 */
935 serial_outp(up, UART_LCR, 0);
936 status1 = serial_in(up, UART_MCR);
937 serial_outp(up, UART_LCR, 0xE0);
938 status2 = serial_in(up, 0x02); /* EXCR1 */
939
940 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
941 serial_outp(up, UART_LCR, 0);
942 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
943 serial_outp(up, UART_LCR, 0xE0);
944 status2 = serial_in(up, 0x02); /* EXCR1 */
945 serial_outp(up, UART_LCR, 0);
946 serial_outp(up, UART_MCR, status1);
947
948 if ((status2 ^ status1) & UART_MCR_LOOP) {
David Woodhouse857dde22005-05-21 15:52:23 +0100949 unsigned short quot;
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 serial_outp(up, UART_LCR, 0xE0);
David Woodhouse857dde22005-05-21 15:52:23 +0100952
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100953 quot = serial_dl_read(up);
David Woodhouse857dde22005-05-21 15:52:23 +0100954 quot <<= 3;
955
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800956 if (ns16550a_goto_highspeed(up))
957 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
David Woodhouse857dde22005-05-21 15:52:23 +0100959 serial_outp(up, UART_LCR, 0);
960
961 up->port.uartclk = 921600*16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 up->port.type = PORT_NS16550A;
963 up->capabilities |= UART_NATSEMI;
964 return;
965 }
966 }
967
968 /*
969 * No EFR. Try to detect a TI16750, which only sets bit 5 of
970 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
971 * Try setting it with and without DLAB set. Cheap clones
972 * set bit 5 without DLAB set.
973 */
974 serial_outp(up, UART_LCR, 0);
975 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
976 status1 = serial_in(up, UART_IIR) >> 5;
977 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800978 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
980 status2 = serial_in(up, UART_IIR) >> 5;
981 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
982 serial_outp(up, UART_LCR, 0);
983
984 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
985
986 if (status1 == 6 && status2 == 7) {
987 up->port.type = PORT_16750;
988 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
989 return;
990 }
991
992 /*
993 * Try writing and reading the UART_IER_UUE bit (b6).
994 * If it works, this is probably one of the Xscale platform's
995 * internal UARTs.
996 * We're going to explicitly set the UUE bit to 0 before
997 * trying to write and read a 1 just to make sure it's not
998 * already a 1 and maybe locked there before we even start start.
999 */
1000 iersave = serial_in(up, UART_IER);
1001 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
1002 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1003 /*
1004 * OK it's in a known zero state, try writing and reading
1005 * without disturbing the current state of the other bits.
1006 */
1007 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
1008 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1009 /*
1010 * It's an Xscale.
1011 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1012 */
1013 DEBUG_AUTOCONF("Xscale ");
1014 up->port.type = PORT_XSCALE;
Stephen Warren55681812011-06-17 09:45:07 -06001015 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return;
1017 }
1018 } else {
1019 /*
1020 * If we got here we couldn't force the IER_UUE bit to 0.
1021 * Log it and continue.
1022 */
1023 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1024 }
1025 serial_outp(up, UART_IER, iersave);
Philippe Langlais235dae52010-07-29 17:13:57 +02001026
1027 /*
Søren Holm06315342011-09-02 22:55:37 +02001028 * Exar uarts have EFR in a weird location
1029 */
1030 if (up->port.flags & UPF_EXAR_EFR) {
1031 up->port.type = PORT_XR17D15X;
1032 up->capabilities |= UART_CAP_AFE | UART_CAP_EFR;
1033 }
1034
1035 /*
Philippe Langlais235dae52010-07-29 17:13:57 +02001036 * We distinguish between 16550A and U6 16550A by counting
1037 * how many bytes are in the FIFO.
1038 */
1039 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1040 up->port.type = PORT_U6_16550A;
1041 up->capabilities |= UART_CAP_AFE;
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045/*
1046 * This routine is called by rs_init() to initialize a specific serial
1047 * port. It determines what type of UART chip this serial port is
1048 * using: 8250, 16450, 16550, 16550A. The important question is
1049 * whether or not this UART is a 16550A or not, since this will
1050 * determine whether or not we can use its FIFO features or not.
1051 */
1052static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
1053{
1054 unsigned char status1, scratch, scratch2, scratch3;
1055 unsigned char save_lcr, save_mcr;
1056 unsigned long flags;
1057
1058 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
1059 return;
1060
Lennert Buytenhek80647b92009-11-11 14:26:41 -08001061 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
David S. Miller84408382008-10-13 10:45:26 +01001062 serial_index(&up->port), up->port.iobase, up->port.membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 /*
1065 * We really do need global IRQs disabled here - we're going to
1066 * be frobbing the chips IRQ enable register to see if it exists.
1067 */
1068 spin_lock_irqsave(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 up->capabilities = 0;
Russell King4ba5e352005-06-23 10:43:04 +01001071 up->bugs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 if (!(up->port.flags & UPF_BUGGY_UART)) {
1074 /*
1075 * Do a simple existence test first; if we fail this,
1076 * there's no point trying anything else.
Thomas Koellerbd71c182007-05-06 14:48:47 -07001077 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 * 0x80 is used as a nonsense port to prevent against
1079 * false positives due to ISA bus float. The
1080 * assumption is that 0x80 is a non-existent port;
1081 * which should be safe since include/asm/io.h also
1082 * makes this assumption.
1083 *
1084 * Note: this is safe as long as MCR bit 4 is clear
1085 * and the device is in "PC" mode.
1086 */
1087 scratch = serial_inp(up, UART_IER);
1088 serial_outp(up, UART_IER, 0);
1089#ifdef __i386__
1090 outb(0xff, 0x080);
1091#endif
Thomas Hoehn48212002007-02-10 01:46:05 -08001092 /*
1093 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1094 * 16C754B) allow only to modify them if an EFR bit is set.
1095 */
1096 scratch2 = serial_inp(up, UART_IER) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 serial_outp(up, UART_IER, 0x0F);
1098#ifdef __i386__
1099 outb(0, 0x080);
1100#endif
Thomas Hoehn48212002007-02-10 01:46:05 -08001101 scratch3 = serial_inp(up, UART_IER) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 serial_outp(up, UART_IER, scratch);
1103 if (scratch2 != 0 || scratch3 != 0x0F) {
1104 /*
1105 * We failed; there's nothing here
1106 */
1107 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1108 scratch2, scratch3);
1109 goto out;
1110 }
1111 }
1112
1113 save_mcr = serial_in(up, UART_MCR);
1114 save_lcr = serial_in(up, UART_LCR);
1115
Thomas Koellerbd71c182007-05-06 14:48:47 -07001116 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 * Check to see if a UART is really there. Certain broken
1118 * internal modems based on the Rockwell chipset fail this
1119 * test, because they apparently don't implement the loopback
1120 * test mode. So this test is skipped on the COM 1 through
1121 * COM 4 ports. This *should* be safe, since no board
1122 * manufacturer would be stupid enough to design a board
1123 * that conflicts with COM 1-4 --- we hope!
1124 */
1125 if (!(up->port.flags & UPF_SKIP_TEST)) {
1126 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1127 status1 = serial_inp(up, UART_MSR) & 0xF0;
1128 serial_outp(up, UART_MCR, save_mcr);
1129 if (status1 != 0x90) {
1130 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1131 status1);
1132 goto out;
1133 }
1134 }
1135
1136 /*
1137 * We're pretty sure there's a port here. Lets find out what
1138 * type of port it is. The IIR top two bits allows us to find
Russell King6f0d6182005-09-09 16:17:58 +01001139 * out if it's 8250 or 16450, 16550, 16550A or later. This
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 * determines what we test for next.
1141 *
1142 * We also initialise the EFR (if any) to zero for later. The
1143 * EFR occupies the same register location as the FCR and IIR.
1144 */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001145 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 serial_outp(up, UART_EFR, 0);
1147 serial_outp(up, UART_LCR, 0);
1148
1149 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1150 scratch = serial_in(up, UART_IIR) >> 6;
1151
1152 DEBUG_AUTOCONF("iir=%d ", scratch);
1153
1154 switch (scratch) {
1155 case 0:
1156 autoconfig_8250(up);
1157 break;
1158 case 1:
1159 up->port.type = PORT_UNKNOWN;
1160 break;
1161 case 2:
1162 up->port.type = PORT_16550;
1163 break;
1164 case 3:
1165 autoconfig_16550a(up);
1166 break;
1167 }
1168
1169#ifdef CONFIG_SERIAL_8250_RSA
1170 /*
1171 * Only probe for RSA ports if we got the region.
1172 */
1173 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1174 int i;
1175
1176 for (i = 0 ; i < probe_rsa_count; ++i) {
1177 if (probe_rsa[i] == up->port.iobase &&
1178 __enable_rsa(up)) {
1179 up->port.type = PORT_RSA;
1180 break;
1181 }
1182 }
1183 }
1184#endif
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 serial_outp(up, UART_LCR, save_lcr);
1187
1188 if (up->capabilities != uart_config[up->port.type].flags) {
1189 printk(KERN_WARNING
1190 "ttyS%d: detected caps %08x should be %08x\n",
David S. Miller84408382008-10-13 10:45:26 +01001191 serial_index(&up->port), up->capabilities,
1192 uart_config[up->port.type].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194
1195 up->port.fifosize = uart_config[up->port.type].fifo_size;
1196 up->capabilities = uart_config[up->port.type].flags;
1197 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1198
1199 if (up->port.type == PORT_UNKNOWN)
1200 goto out;
1201
1202 /*
1203 * Reset the UART.
1204 */
1205#ifdef CONFIG_SERIAL_8250_RSA
1206 if (up->port.type == PORT_RSA)
1207 serial_outp(up, UART_RSA_FRR, 0);
1208#endif
1209 serial_outp(up, UART_MCR, save_mcr);
1210 serial8250_clear_fifos(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001211 serial_in(up, UART_RX);
Lennert Buytenhek5c8c7552005-11-12 21:58:05 +00001212 if (up->capabilities & UART_CAP_UUE)
1213 serial_outp(up, UART_IER, UART_IER_UUE);
1214 else
1215 serial_outp(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Thomas Koellerbd71c182007-05-06 14:48:47 -07001217 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 spin_unlock_irqrestore(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1220}
1221
1222static void autoconfig_irq(struct uart_8250_port *up)
1223{
1224 unsigned char save_mcr, save_ier;
1225 unsigned char save_ICP = 0;
1226 unsigned int ICP = 0;
1227 unsigned long irqs;
1228 int irq;
1229
1230 if (up->port.flags & UPF_FOURPORT) {
1231 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1232 save_ICP = inb_p(ICP);
1233 outb_p(0x80, ICP);
1234 (void) inb_p(ICP);
1235 }
1236
1237 /* forget possible initially masked and pending IRQ */
1238 probe_irq_off(probe_irq_on());
1239 save_mcr = serial_inp(up, UART_MCR);
1240 save_ier = serial_inp(up, UART_IER);
1241 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
Thomas Koellerbd71c182007-05-06 14:48:47 -07001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 irqs = probe_irq_on();
1244 serial_outp(up, UART_MCR, 0);
Alan Cox6f803cd2008-02-08 04:18:52 -08001245 udelay(10);
1246 if (up->port.flags & UPF_FOURPORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 serial_outp(up, UART_MCR,
1248 UART_MCR_DTR | UART_MCR_RTS);
1249 } else {
1250 serial_outp(up, UART_MCR,
1251 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1252 }
1253 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1254 (void)serial_inp(up, UART_LSR);
1255 (void)serial_inp(up, UART_RX);
1256 (void)serial_inp(up, UART_IIR);
1257 (void)serial_inp(up, UART_MSR);
1258 serial_outp(up, UART_TX, 0xFF);
Alan Cox6f803cd2008-02-08 04:18:52 -08001259 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 irq = probe_irq_off(irqs);
1261
1262 serial_outp(up, UART_MCR, save_mcr);
1263 serial_outp(up, UART_IER, save_ier);
1264
1265 if (up->port.flags & UPF_FOURPORT)
1266 outb_p(save_ICP, ICP);
1267
1268 up->port.irq = (irq > 0) ? irq : 0;
1269}
1270
Russell Kinge763b902005-06-29 18:41:51 +01001271static inline void __stop_tx(struct uart_8250_port *p)
1272{
1273 if (p->ier & UART_IER_THRI) {
1274 p->ier &= ~UART_IER_THRI;
1275 serial_out(p, UART_IER, p->ier);
1276 }
1277}
1278
Russell Kingb129a8c2005-08-31 10:12:14 +01001279static void serial8250_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Jamie Iles49d57412010-12-01 23:39:35 +00001281 struct uart_8250_port *up =
1282 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Russell Kinge763b902005-06-29 18:41:51 +01001284 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 /*
Russell Kinge763b902005-06-29 18:41:51 +01001287 * We really want to stop the transmitter from sending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 */
Russell Kinge763b902005-06-29 18:41:51 +01001289 if (up->port.type == PORT_16C950) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 up->acr |= UART_ACR_TXDIS;
1291 serial_icr_write(up, UART_ACR, up->acr);
1292 }
1293}
1294
Russell Kingb129a8c2005-08-31 10:12:14 +01001295static void serial8250_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Jamie Iles49d57412010-12-01 23:39:35 +00001297 struct uart_8250_port *up =
1298 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 if (!(up->ier & UART_IER_THRI)) {
1301 up->ier |= UART_IER_THRI;
1302 serial_out(up, UART_IER, up->ier);
Russell King55d3b282005-06-23 15:05:41 +01001303
Russell King67f76542005-06-23 22:26:43 +01001304 if (up->bugs & UART_BUG_TXEN) {
Ian Jackson68cb4f82009-11-18 11:08:11 +01001305 unsigned char lsr;
Russell King55d3b282005-06-23 15:05:41 +01001306 lsr = serial_in(up, UART_LSR);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001307 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Thomas Koellerbd71c182007-05-06 14:48:47 -07001308 if ((up->port.type == PORT_RM9000) ?
Ian Jackson68cb4f82009-11-18 11:08:11 +01001309 (lsr & UART_LSR_THRE) :
1310 (lsr & UART_LSR_TEMT))
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001311 serial8250_tx_chars(up);
Russell King55d3b282005-06-23 15:05:41 +01001312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
Russell Kinge763b902005-06-29 18:41:51 +01001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /*
Russell Kinge763b902005-06-29 18:41:51 +01001316 * Re-enable the transmitter if we disabled it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 */
Russell Kinge763b902005-06-29 18:41:51 +01001318 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 up->acr &= ~UART_ACR_TXDIS;
1320 serial_icr_write(up, UART_ACR, up->acr);
1321 }
1322}
1323
1324static void serial8250_stop_rx(struct uart_port *port)
1325{
Jamie Iles49d57412010-12-01 23:39:35 +00001326 struct uart_8250_port *up =
1327 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 up->ier &= ~UART_IER_RLSI;
1330 up->port.read_status_mask &= ~UART_LSR_DR;
1331 serial_out(up, UART_IER, up->ier);
1332}
1333
1334static void serial8250_enable_ms(struct uart_port *port)
1335{
Jamie Iles49d57412010-12-01 23:39:35 +00001336 struct uart_8250_port *up =
1337 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001339 /* no MSR capabilities */
1340 if (up->bugs & UART_BUG_NOMSR)
1341 return;
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 up->ier |= UART_IER_MSI;
1344 serial_out(up, UART_IER, up->ier);
1345}
1346
Stephen Warren5f873ba2011-05-17 16:12:37 -06001347/*
1348 * Clear the Tegra rx fifo after a break
1349 *
1350 * FIXME: This needs to become a port specific callback once we have a
1351 * framework for this
1352 */
1353static void clear_rx_fifo(struct uart_8250_port *up)
1354{
1355 unsigned int status, tmout = 10000;
1356 do {
1357 status = serial_in(up, UART_LSR);
1358 if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
1359 status = serial_in(up, UART_RX);
1360 else
1361 break;
1362 if (--tmout == 0)
1363 break;
1364 udelay(1);
1365 } while (1);
1366}
1367
Paul Gortmaker0690f412011-12-04 18:42:19 -05001368/*
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001369 * serial8250_rx_chars: processes according to the passed in LSR
Paul Gortmaker0690f412011-12-04 18:42:19 -05001370 * value, and returns the remaining LSR bits not handled
1371 * by this Rx routine.
1372 */
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001373unsigned char
1374serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001376 struct tty_struct *tty = up->port.state->port.tty;
Paul Gortmaker0690f412011-12-04 18:42:19 -05001377 unsigned char ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 int max_count = 256;
1379 char flag;
1380
1381 do {
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001382 if (likely(lsr & UART_LSR_DR))
1383 ch = serial_inp(up, UART_RX);
1384 else
1385 /*
1386 * Intel 82571 has a Serial Over Lan device that will
1387 * set UART_LSR_BI without setting UART_LSR_DR when
1388 * it receives a break. To avoid reading from the
1389 * receive buffer without UART_LSR_DR bit set, we
1390 * just force the read character to be 0
1391 */
1392 ch = 0;
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 flag = TTY_NORMAL;
1395 up->port.icount.rx++;
1396
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001397 lsr |= up->lsr_saved_flags;
1398 up->lsr_saved_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001400 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 /*
1402 * For statistics only
1403 */
1404 if (lsr & UART_LSR_BI) {
1405 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1406 up->port.icount.brk++;
1407 /*
Stephen Warren5f873ba2011-05-17 16:12:37 -06001408 * If tegra port then clear the rx fifo to
1409 * accept another break/character.
1410 */
1411 if (up->port.type == PORT_TEGRA)
1412 clear_rx_fifo(up);
1413
1414 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 * We do the SysRQ and SAK checking
1416 * here because otherwise the break
1417 * may get masked by ignore_status_mask
1418 * or read_status_mask.
1419 */
1420 if (uart_handle_break(&up->port))
1421 goto ignore_char;
1422 } else if (lsr & UART_LSR_PE)
1423 up->port.icount.parity++;
1424 else if (lsr & UART_LSR_FE)
1425 up->port.icount.frame++;
1426 if (lsr & UART_LSR_OE)
1427 up->port.icount.overrun++;
1428
1429 /*
Russell King23907eb2005-04-16 15:26:39 -07001430 * Mask off conditions which should be ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 */
1432 lsr &= up->port.read_status_mask;
1433
1434 if (lsr & UART_LSR_BI) {
1435 DEBUG_INTR("handling break....");
1436 flag = TTY_BREAK;
1437 } else if (lsr & UART_LSR_PE)
1438 flag = TTY_PARITY;
1439 else if (lsr & UART_LSR_FE)
1440 flag = TTY_FRAME;
1441 }
David Howells7d12e782006-10-05 14:55:46 +01001442 if (uart_handle_sysrq_char(&up->port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 goto ignore_char;
Russell King05ab3012005-05-09 23:21:59 +01001444
1445 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1446
Alan Cox6f803cd2008-02-08 04:18:52 -08001447ignore_char:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 lsr = serial_inp(up, UART_LSR);
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001449 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 spin_unlock(&up->port.lock);
1451 tty_flip_buffer_push(tty);
1452 spin_lock(&up->port.lock);
Paul Gortmaker0690f412011-12-04 18:42:19 -05001453 return lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001455EXPORT_SYMBOL_GPL(serial8250_rx_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001457void serial8250_tx_chars(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001459 struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 int count;
1461
1462 if (up->port.x_char) {
1463 serial_outp(up, UART_TX, up->port.x_char);
1464 up->port.icount.tx++;
1465 up->port.x_char = 0;
1466 return;
1467 }
Russell Kingb129a8c2005-08-31 10:12:14 +01001468 if (uart_tx_stopped(&up->port)) {
1469 serial8250_stop_tx(&up->port);
1470 return;
1471 }
1472 if (uart_circ_empty(xmit)) {
Russell Kinge763b902005-06-29 18:41:51 +01001473 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return;
1475 }
1476
1477 count = up->tx_loadsz;
1478 do {
1479 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1480 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1481 up->port.icount.tx++;
1482 if (uart_circ_empty(xmit))
1483 break;
1484 } while (--count > 0);
1485
1486 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1487 uart_write_wakeup(&up->port);
1488
1489 DEBUG_INTR("THRE...");
1490
1491 if (uart_circ_empty(xmit))
Russell Kinge763b902005-06-29 18:41:51 +01001492 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001494EXPORT_SYMBOL_GPL(serial8250_tx_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001496unsigned int serial8250_modem_status(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Russell King2af7cd62006-01-04 16:55:09 +00001498 unsigned int status = serial_in(up, UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001500 status |= up->msr_saved_flags;
1501 up->msr_saved_flags = 0;
Taku Izumifdc30b32007-04-23 14:41:00 -07001502 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
Alan Coxebd2c8f2009-09-19 13:13:28 -07001503 up->port.state != NULL) {
Russell King2af7cd62006-01-04 16:55:09 +00001504 if (status & UART_MSR_TERI)
1505 up->port.icount.rng++;
1506 if (status & UART_MSR_DDSR)
1507 up->port.icount.dsr++;
1508 if (status & UART_MSR_DDCD)
1509 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1510 if (status & UART_MSR_DCTS)
1511 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Alan Coxbdc04e32009-09-19 13:13:31 -07001513 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
Russell King2af7cd62006-01-04 16:55:09 +00001514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Russell King2af7cd62006-01-04 16:55:09 +00001516 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001518EXPORT_SYMBOL_GPL(serial8250_modem_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520/*
1521 * This handles the interrupt from one port.
1522 */
Paul Gortmaker86b21192011-12-04 18:42:22 -05001523int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Paul Gortmaker0690f412011-12-04 18:42:19 -05001525 unsigned char status;
Jiri Kosina4bf36312007-04-23 14:41:21 -07001526 unsigned long flags;
Paul Gortmaker86b21192011-12-04 18:42:22 -05001527 struct uart_8250_port *up =
1528 container_of(port, struct uart_8250_port, port);
1529
1530 if (iir & UART_IIR_NO_INT)
1531 return 0;
Russell King45e24602006-01-04 19:19:06 +00001532
Jiri Kosina4bf36312007-04-23 14:41:21 -07001533 spin_lock_irqsave(&up->port.lock, flags);
Russell King45e24602006-01-04 19:19:06 +00001534
1535 status = serial_inp(up, UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 DEBUG_INTR("status = %x...", status);
1538
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001539 if (status & (UART_LSR_DR | UART_LSR_BI))
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001540 status = serial8250_rx_chars(up, status);
1541 serial8250_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (status & UART_LSR_THRE)
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001543 serial8250_tx_chars(up);
Russell King45e24602006-01-04 19:19:06 +00001544
Jiri Kosina4bf36312007-04-23 14:41:21 -07001545 spin_unlock_irqrestore(&up->port.lock, flags);
Paul Gortmaker86b21192011-12-04 18:42:22 -05001546 return 1;
Jamie Iles583d28e2011-08-15 10:17:52 +01001547}
Jamie Ilesc7a1bdc2011-08-26 19:04:49 +01001548EXPORT_SYMBOL_GPL(serial8250_handle_irq);
Jamie Iles583d28e2011-08-15 10:17:52 +01001549
1550static int serial8250_default_handle_irq(struct uart_port *port)
1551{
1552 struct uart_8250_port *up =
1553 container_of(port, struct uart_8250_port, port);
1554 unsigned int iir = serial_in(up, UART_IIR);
1555
1556 return serial8250_handle_irq(port, iir);
1557}
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559/*
1560 * This is the serial driver's interrupt routine.
1561 *
1562 * Arjan thinks the old way was overly complex, so it got simplified.
1563 * Alan disagrees, saying that need the complexity to handle the weird
1564 * nature of ISA shared interrupts. (This is a special exception.)
1565 *
1566 * In order to handle ISA shared interrupts properly, we need to check
1567 * that all ports have been serviced, and therefore the ISA interrupt
1568 * line has been de-asserted.
1569 *
1570 * This means we need to loop through all ports. checking that they
1571 * don't have an interrupt pending.
1572 */
David Howells7d12e782006-10-05 14:55:46 +01001573static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 struct irq_info *i = dev_id;
1576 struct list_head *l, *end = NULL;
1577 int pass_counter = 0, handled = 0;
1578
1579 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1580
1581 spin_lock(&i->lock);
1582
1583 l = i->head;
1584 do {
1585 struct uart_8250_port *up;
Jamie Iles583d28e2011-08-15 10:17:52 +01001586 struct uart_port *port;
Dan Williams448ac152011-11-22 13:41:24 -08001587 bool skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 up = list_entry(l, struct uart_8250_port, list);
Jamie Iles583d28e2011-08-15 10:17:52 +01001590 port = &up->port;
Dan Williams448ac152011-11-22 13:41:24 -08001591 skip = pass_counter && up->port.flags & UPF_IIR_ONCE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Dan Williams448ac152011-11-22 13:41:24 -08001593 if (!skip && port->handle_irq(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 handled = 1;
Marc St-Jeanbeab6972007-05-06 14:48:45 -07001595 end = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 } else if (end == NULL)
1597 end = l;
1598
1599 l = l->next;
1600
1601 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1602 /* If we hit this, we're dead. */
Daniel Drakecd3ecad2010-10-20 16:00:48 -07001603 printk_ratelimited(KERN_ERR
1604 "serial8250: too much work for irq%d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 break;
1606 }
1607 } while (l != end);
1608
1609 spin_unlock(&i->lock);
1610
1611 DEBUG_INTR("end.\n");
1612
1613 return IRQ_RETVAL(handled);
1614}
1615
1616/*
1617 * To support ISA shared interrupts, we need to have one interrupt
1618 * handler that ensures that the IRQ line has been deasserted
1619 * before returning. Failing to do this will result in the IRQ
1620 * line being stuck active, and, since ISA irqs are edge triggered,
1621 * no more IRQs will be seen.
1622 */
1623static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1624{
1625 spin_lock_irq(&i->lock);
1626
1627 if (!list_empty(i->head)) {
1628 if (i->head == &up->list)
1629 i->head = i->head->next;
1630 list_del(&up->list);
1631 } else {
1632 BUG_ON(i->head != &up->list);
1633 i->head = NULL;
1634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 spin_unlock_irq(&i->lock);
Alan Cox25db8ad2008-08-19 20:49:40 -07001636 /* List empty so throw away the hash node */
1637 if (i->head == NULL) {
1638 hlist_del(&i->node);
1639 kfree(i);
1640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
1643static int serial_link_irq_chain(struct uart_8250_port *up)
1644{
Alan Cox25db8ad2008-08-19 20:49:40 -07001645 struct hlist_head *h;
1646 struct hlist_node *n;
1647 struct irq_info *i;
Thomas Gleixner40663cc2006-07-01 19:29:43 -07001648 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Alan Cox25db8ad2008-08-19 20:49:40 -07001650 mutex_lock(&hash_mutex);
1651
1652 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1653
1654 hlist_for_each(n, h) {
1655 i = hlist_entry(n, struct irq_info, node);
1656 if (i->irq == up->port.irq)
1657 break;
1658 }
1659
1660 if (n == NULL) {
1661 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
1662 if (i == NULL) {
1663 mutex_unlock(&hash_mutex);
1664 return -ENOMEM;
1665 }
1666 spin_lock_init(&i->lock);
1667 i->irq = up->port.irq;
1668 hlist_add_head(&i->node, h);
1669 }
1670 mutex_unlock(&hash_mutex);
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 spin_lock_irq(&i->lock);
1673
1674 if (i->head) {
1675 list_add(&up->list, i->head);
1676 spin_unlock_irq(&i->lock);
1677
1678 ret = 0;
1679 } else {
1680 INIT_LIST_HEAD(&up->list);
1681 i->head = &up->list;
1682 spin_unlock_irq(&i->lock);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07001683 irq_flags |= up->port.irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 ret = request_irq(up->port.irq, serial8250_interrupt,
1685 irq_flags, "serial", i);
1686 if (ret < 0)
1687 serial_do_unlink(i, up);
1688 }
1689
1690 return ret;
1691}
1692
1693static void serial_unlink_irq_chain(struct uart_8250_port *up)
1694{
Alan Cox25db8ad2008-08-19 20:49:40 -07001695 struct irq_info *i;
1696 struct hlist_node *n;
1697 struct hlist_head *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Alan Cox25db8ad2008-08-19 20:49:40 -07001699 mutex_lock(&hash_mutex);
1700
1701 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1702
1703 hlist_for_each(n, h) {
1704 i = hlist_entry(n, struct irq_info, node);
1705 if (i->irq == up->port.irq)
1706 break;
1707 }
1708
1709 BUG_ON(n == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 BUG_ON(i->head == NULL);
1711
1712 if (list_empty(i->head))
1713 free_irq(up->port.irq, i);
1714
1715 serial_do_unlink(i, up);
Alan Cox25db8ad2008-08-19 20:49:40 -07001716 mutex_unlock(&hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
1719/*
1720 * This function is used to handle ports that do not have an
1721 * interrupt. This doesn't work very well for 16450's, but gives
1722 * barely passable results for a 16550A. (Although at the expense
1723 * of much CPU overhead).
1724 */
1725static void serial8250_timeout(unsigned long data)
1726{
1727 struct uart_8250_port *up = (struct uart_8250_port *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Paul Gortmakera0431472011-12-04 18:42:21 -05001729 up->port.handle_irq(&up->port);
Anton Vorontsov54381062010-10-01 17:21:25 +04001730 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
Alex Williamson40b36da2007-02-14 00:33:04 -08001731}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Alex Williamson40b36da2007-02-14 00:33:04 -08001733static void serial8250_backup_timeout(unsigned long data)
1734{
1735 struct uart_8250_port *up = (struct uart_8250_port *)data;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001736 unsigned int iir, ier = 0, lsr;
1737 unsigned long flags;
Alex Williamson40b36da2007-02-14 00:33:04 -08001738
Al Cooperdbb3b1c2011-07-25 16:19:52 -04001739 spin_lock_irqsave(&up->port.lock, flags);
1740
Alex Williamson40b36da2007-02-14 00:33:04 -08001741 /*
1742 * Must disable interrupts or else we risk racing with the interrupt
1743 * based handler.
1744 */
Alan Coxd4e33fa2012-01-26 17:44:09 +00001745 if (up->port.irq) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001746 ier = serial_in(up, UART_IER);
1747 serial_out(up, UART_IER, 0);
1748 }
1749
1750 iir = serial_in(up, UART_IIR);
1751
1752 /*
1753 * This should be a safe test for anyone who doesn't trust the
1754 * IIR bits on their UART, but it's specifically designed for
1755 * the "Diva" UART used on the management processor on many HP
1756 * ia64 and parisc boxes.
1757 */
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001758 lsr = serial_in(up, UART_LSR);
1759 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -08001760 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
Alan Coxebd2c8f2009-09-19 13:13:28 -07001761 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001762 (lsr & UART_LSR_THRE)) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001763 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1764 iir |= UART_IIR_THRI;
1765 }
1766
1767 if (!(iir & UART_IIR_NO_INT))
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001768 serial8250_tx_chars(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001769
Alan Coxd4e33fa2012-01-26 17:44:09 +00001770 if (up->port.irq)
Alex Williamson40b36da2007-02-14 00:33:04 -08001771 serial_out(up, UART_IER, ier);
1772
Al Cooperdbb3b1c2011-07-25 16:19:52 -04001773 spin_unlock_irqrestore(&up->port.lock, flags);
1774
Alex Williamson40b36da2007-02-14 00:33:04 -08001775 /* Standard timer interval plus 0.2s to keep the port running */
Alan Cox6f803cd2008-02-08 04:18:52 -08001776 mod_timer(&up->timer,
Anton Vorontsov54381062010-10-01 17:21:25 +04001777 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
1780static unsigned int serial8250_tx_empty(struct uart_port *port)
1781{
Jamie Iles49d57412010-12-01 23:39:35 +00001782 struct uart_8250_port *up =
1783 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 unsigned long flags;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001785 unsigned int lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 spin_lock_irqsave(&up->port.lock, flags);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001788 lsr = serial_in(up, UART_LSR);
1789 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 spin_unlock_irqrestore(&up->port.lock, flags);
1791
Dick Hollenbeckbca47612009-12-09 12:31:34 -08001792 return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
1795static unsigned int serial8250_get_mctrl(struct uart_port *port)
1796{
Jamie Iles49d57412010-12-01 23:39:35 +00001797 struct uart_8250_port *up =
1798 container_of(port, struct uart_8250_port, port);
Russell King2af7cd62006-01-04 16:55:09 +00001799 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 unsigned int ret;
1801
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001802 status = serial8250_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 ret = 0;
1805 if (status & UART_MSR_DCD)
1806 ret |= TIOCM_CAR;
1807 if (status & UART_MSR_RI)
1808 ret |= TIOCM_RNG;
1809 if (status & UART_MSR_DSR)
1810 ret |= TIOCM_DSR;
1811 if (status & UART_MSR_CTS)
1812 ret |= TIOCM_CTS;
1813 return ret;
1814}
1815
1816static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1817{
Jamie Iles49d57412010-12-01 23:39:35 +00001818 struct uart_8250_port *up =
1819 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 unsigned char mcr = 0;
1821
1822 if (mctrl & TIOCM_RTS)
1823 mcr |= UART_MCR_RTS;
1824 if (mctrl & TIOCM_DTR)
1825 mcr |= UART_MCR_DTR;
1826 if (mctrl & TIOCM_OUT1)
1827 mcr |= UART_MCR_OUT1;
1828 if (mctrl & TIOCM_OUT2)
1829 mcr |= UART_MCR_OUT2;
1830 if (mctrl & TIOCM_LOOP)
1831 mcr |= UART_MCR_LOOP;
1832
1833 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1834
1835 serial_out(up, UART_MCR, mcr);
1836}
1837
1838static void serial8250_break_ctl(struct uart_port *port, int break_state)
1839{
Jamie Iles49d57412010-12-01 23:39:35 +00001840 struct uart_8250_port *up =
1841 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 unsigned long flags;
1843
1844 spin_lock_irqsave(&up->port.lock, flags);
1845 if (break_state == -1)
1846 up->lcr |= UART_LCR_SBC;
1847 else
1848 up->lcr &= ~UART_LCR_SBC;
1849 serial_out(up, UART_LCR, up->lcr);
1850 spin_unlock_irqrestore(&up->port.lock, flags);
1851}
1852
Alex Williamson40b36da2007-02-14 00:33:04 -08001853/*
1854 * Wait for transmitter & holding register to empty
1855 */
Will Newtonb5d674a2008-10-13 10:36:21 +01001856static void wait_for_xmitr(struct uart_8250_port *up, int bits)
Alex Williamson40b36da2007-02-14 00:33:04 -08001857{
1858 unsigned int status, tmout = 10000;
1859
1860 /* Wait up to 10ms for the character(s) to be sent. */
David Daney97d303b2010-10-05 11:40:07 -07001861 for (;;) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001862 status = serial_in(up, UART_LSR);
1863
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001864 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -08001865
David Daney97d303b2010-10-05 11:40:07 -07001866 if ((status & bits) == bits)
1867 break;
Alex Williamson40b36da2007-02-14 00:33:04 -08001868 if (--tmout == 0)
1869 break;
1870 udelay(1);
David Daney97d303b2010-10-05 11:40:07 -07001871 }
Alex Williamson40b36da2007-02-14 00:33:04 -08001872
1873 /* Wait up to 1s for flow control if necessary */
1874 if (up->port.flags & UPF_CONS_FLOW) {
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001875 unsigned int tmout;
1876 for (tmout = 1000000; tmout; tmout--) {
1877 unsigned int msr = serial_in(up, UART_MSR);
1878 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1879 if (msr & UART_MSR_CTS)
1880 break;
Alex Williamson40b36da2007-02-14 00:33:04 -08001881 udelay(1);
1882 touch_nmi_watchdog();
1883 }
1884 }
1885}
1886
Jason Wesself2d937f2008-04-17 20:05:37 +02001887#ifdef CONFIG_CONSOLE_POLL
1888/*
1889 * Console polling routines for writing and reading from the uart while
1890 * in an interrupt or debug context.
1891 */
1892
1893static int serial8250_get_poll_char(struct uart_port *port)
1894{
Jamie Iles49d57412010-12-01 23:39:35 +00001895 struct uart_8250_port *up =
1896 container_of(port, struct uart_8250_port, port);
Jason Wesself2d937f2008-04-17 20:05:37 +02001897 unsigned char lsr = serial_inp(up, UART_LSR);
1898
Jason Wesself5316b42010-05-20 21:04:22 -05001899 if (!(lsr & UART_LSR_DR))
1900 return NO_POLL_CHAR;
Jason Wesself2d937f2008-04-17 20:05:37 +02001901
1902 return serial_inp(up, UART_RX);
1903}
1904
1905
1906static void serial8250_put_poll_char(struct uart_port *port,
1907 unsigned char c)
1908{
1909 unsigned int ier;
Jamie Iles49d57412010-12-01 23:39:35 +00001910 struct uart_8250_port *up =
1911 container_of(port, struct uart_8250_port, port);
Jason Wesself2d937f2008-04-17 20:05:37 +02001912
1913 /*
1914 * First save the IER then disable the interrupts
1915 */
1916 ier = serial_in(up, UART_IER);
1917 if (up->capabilities & UART_CAP_UUE)
1918 serial_out(up, UART_IER, UART_IER_UUE);
1919 else
1920 serial_out(up, UART_IER, 0);
1921
1922 wait_for_xmitr(up, BOTH_EMPTY);
1923 /*
1924 * Send the character out.
1925 * If a LF, also do CR...
1926 */
1927 serial_out(up, UART_TX, c);
1928 if (c == 10) {
1929 wait_for_xmitr(up, BOTH_EMPTY);
1930 serial_out(up, UART_TX, 13);
1931 }
1932
1933 /*
1934 * Finally, wait for transmitter to become empty
1935 * and restore the IER
1936 */
1937 wait_for_xmitr(up, BOTH_EMPTY);
1938 serial_out(up, UART_IER, ier);
1939}
1940
1941#endif /* CONFIG_CONSOLE_POLL */
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943static int serial8250_startup(struct uart_port *port)
1944{
Jamie Iles49d57412010-12-01 23:39:35 +00001945 struct uart_8250_port *up =
1946 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 unsigned long flags;
Russell King55d3b282005-06-23 15:05:41 +01001948 unsigned char lsr, iir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 int retval;
1950
Ondrej Puzmane4f05af2010-12-04 21:17:38 +01001951 up->port.fifosize = uart_config[up->port.type].fifo_size;
1952 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 up->capabilities = uart_config[up->port.type].flags;
1954 up->mcr = 0;
1955
Alan Coxb8e7e402009-05-28 14:01:35 +01001956 if (up->port.iotype != up->cur_iotype)
1957 set_io_from_upio(port);
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (up->port.type == PORT_16C950) {
1960 /* Wake up and initialize UART */
1961 up->acr = 0;
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001962 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 serial_outp(up, UART_EFR, UART_EFR_ECB);
1964 serial_outp(up, UART_IER, 0);
1965 serial_outp(up, UART_LCR, 0);
1966 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
Wolfram Sang7d73aaf2011-12-09 18:07:13 +01001967 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 serial_outp(up, UART_EFR, UART_EFR_ECB);
1969 serial_outp(up, UART_LCR, 0);
1970 }
1971
1972#ifdef CONFIG_SERIAL_8250_RSA
1973 /*
1974 * If this is an RSA port, see if we can kick it up to the
1975 * higher speed clock.
1976 */
1977 enable_rsa(up);
1978#endif
1979
1980 /*
1981 * Clear the FIFO buffers and disable them.
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08001982 * (they will be reenabled in set_termios())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 */
1984 serial8250_clear_fifos(up);
1985
1986 /*
1987 * Clear the interrupt registers.
1988 */
1989 (void) serial_inp(up, UART_LSR);
1990 (void) serial_inp(up, UART_RX);
1991 (void) serial_inp(up, UART_IIR);
1992 (void) serial_inp(up, UART_MSR);
1993
1994 /*
1995 * At this point, there's no way the LSR could still be 0xff;
1996 * if it is, then bail out, because there's likely no UART
1997 * here.
1998 */
1999 if (!(up->port.flags & UPF_BUGGY_UART) &&
2000 (serial_inp(up, UART_LSR) == 0xff)) {
Konrad Rzeszutek Wilk7808a4c2011-09-26 09:14:34 -04002001 printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2002 serial_index(&up->port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return -ENODEV;
2004 }
2005
2006 /*
2007 * For a XR16C850, we need to set the trigger levels
2008 */
2009 if (up->port.type == PORT_16850) {
2010 unsigned char fctr;
2011
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08002012 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2015 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2016 serial_outp(up, UART_TRG, UART_TRG_96);
2017 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2018 serial_outp(up, UART_TRG, UART_TRG_96);
2019
2020 serial_outp(up, UART_LCR, 0);
2021 }
2022
Alan Coxd4e33fa2012-01-26 17:44:09 +00002023 if (up->port.irq) {
Alex Williamson01c194d2008-04-28 02:14:09 -07002024 unsigned char iir1;
Alex Williamson40b36da2007-02-14 00:33:04 -08002025 /*
2026 * Test for UARTs that do not reassert THRE when the
2027 * transmitter is idle and the interrupt has already
2028 * been cleared. Real 16550s should always reassert
2029 * this interrupt whenever the transmitter is idle and
2030 * the interrupt is enabled. Delays are necessary to
2031 * allow register changes to become visible.
2032 */
Borislav Petkovc389d272008-07-29 22:33:32 -07002033 spin_lock_irqsave(&up->port.lock, flags);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002034 if (up->port.irqflags & IRQF_SHARED)
Anton Vorontsov768aec02008-07-22 11:21:07 +01002035 disable_irq_nosync(up->port.irq);
Alex Williamson40b36da2007-02-14 00:33:04 -08002036
2037 wait_for_xmitr(up, UART_LSR_THRE);
2038 serial_out_sync(up, UART_IER, UART_IER_THRI);
2039 udelay(1); /* allow THRE to set */
Alex Williamson01c194d2008-04-28 02:14:09 -07002040 iir1 = serial_in(up, UART_IIR);
Alex Williamson40b36da2007-02-14 00:33:04 -08002041 serial_out(up, UART_IER, 0);
2042 serial_out_sync(up, UART_IER, UART_IER_THRI);
2043 udelay(1); /* allow a working UART time to re-assert THRE */
2044 iir = serial_in(up, UART_IIR);
2045 serial_out(up, UART_IER, 0);
2046
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002047 if (up->port.irqflags & IRQF_SHARED)
Anton Vorontsov768aec02008-07-22 11:21:07 +01002048 enable_irq(up->port.irq);
Borislav Petkovc389d272008-07-29 22:33:32 -07002049 spin_unlock_irqrestore(&up->port.lock, flags);
Alex Williamson40b36da2007-02-14 00:33:04 -08002050
2051 /*
2052 * If the interrupt is not reasserted, setup a timer to
2053 * kick the UART on a regular basis.
2054 */
Alex Williamson01c194d2008-04-28 02:14:09 -07002055 if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
Will Newton363f66f2008-09-02 14:35:44 -07002056 up->bugs |= UART_BUG_THRE;
David S. Miller84408382008-10-13 10:45:26 +01002057 pr_debug("ttyS%d - using backup timer\n",
2058 serial_index(port));
Alex Williamson40b36da2007-02-14 00:33:04 -08002059 }
2060 }
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 /*
Will Newton363f66f2008-09-02 14:35:44 -07002063 * The above check will only give an accurate result the first time
2064 * the port is opened so this value needs to be preserved.
2065 */
2066 if (up->bugs & UART_BUG_THRE) {
2067 up->timer.function = serial8250_backup_timeout;
2068 up->timer.data = (unsigned long)up;
2069 mod_timer(&up->timer, jiffies +
Anton Vorontsov54381062010-10-01 17:21:25 +04002070 uart_poll_timeout(port) + HZ / 5);
Will Newton363f66f2008-09-02 14:35:44 -07002071 }
2072
2073 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 * If the "interrupt" for this port doesn't correspond with any
2075 * hardware interrupt, we use a timer-based system. The original
2076 * driver used to do this with IRQ0.
2077 */
Alan Coxd4e33fa2012-01-26 17:44:09 +00002078 if (!up->port.irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 up->timer.data = (unsigned long)up;
Anton Vorontsov54381062010-10-01 17:21:25 +04002080 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 } else {
2082 retval = serial_link_irq_chain(up);
2083 if (retval)
2084 return retval;
2085 }
2086
2087 /*
2088 * Now, initialize the UART
2089 */
2090 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
2091
2092 spin_lock_irqsave(&up->port.lock, flags);
2093 if (up->port.flags & UPF_FOURPORT) {
Alan Coxd4e33fa2012-01-26 17:44:09 +00002094 if (!up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 up->port.mctrl |= TIOCM_OUT1;
2096 } else
2097 /*
2098 * Most PC uarts need OUT2 raised to enable interrupts.
2099 */
Alan Coxd4e33fa2012-01-26 17:44:09 +00002100 if (up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 up->port.mctrl |= TIOCM_OUT2;
2102
2103 serial8250_set_mctrl(&up->port, up->port.mctrl);
Russell King55d3b282005-06-23 15:05:41 +01002104
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002105 /* Serial over Lan (SoL) hack:
2106 Intel 8257x Gigabit ethernet chips have a
2107 16550 emulation, to be used for Serial Over Lan.
2108 Those chips take a longer time than a normal
2109 serial device to signalize that a transmission
2110 data was queued. Due to that, the above test generally
2111 fails. One solution would be to delay the reading of
2112 iir. However, this is not reliable, since the timeout
2113 is variable. So, let's just don't test if we receive
2114 TX irq. This way, we'll never enable UART_BUG_TXEN.
2115 */
Chuck Ebbertd41a4b52009-10-01 15:44:26 -07002116 if (skip_txen_test || up->port.flags & UPF_NO_TXEN_TEST)
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002117 goto dont_test_tx_en;
2118
Russell King55d3b282005-06-23 15:05:41 +01002119 /*
2120 * Do a quick test to see if we receive an
2121 * interrupt when we enable the TX irq.
2122 */
2123 serial_outp(up, UART_IER, UART_IER_THRI);
2124 lsr = serial_in(up, UART_LSR);
2125 iir = serial_in(up, UART_IIR);
2126 serial_outp(up, UART_IER, 0);
2127
2128 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
Russell King67f76542005-06-23 22:26:43 +01002129 if (!(up->bugs & UART_BUG_TXEN)) {
2130 up->bugs |= UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002131 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
David S. Miller84408382008-10-13 10:45:26 +01002132 serial_index(port));
Russell King55d3b282005-06-23 15:05:41 +01002133 }
2134 } else {
Russell King67f76542005-06-23 22:26:43 +01002135 up->bugs &= ~UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002136 }
2137
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002138dont_test_tx_en:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 spin_unlock_irqrestore(&up->port.lock, flags);
2140
2141 /*
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002142 * Clear the interrupt registers again for luck, and clear the
2143 * saved flags to avoid getting false values from polling
2144 * routines or the previous session.
2145 */
2146 serial_inp(up, UART_LSR);
2147 serial_inp(up, UART_RX);
2148 serial_inp(up, UART_IIR);
2149 serial_inp(up, UART_MSR);
2150 up->lsr_saved_flags = 0;
2151 up->msr_saved_flags = 0;
2152
2153 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 * Finally, enable interrupts. Note: Modem status interrupts
2155 * are set via set_termios(), which will be occurring imminently
2156 * anyway, so we don't enable them here.
2157 */
2158 up->ier = UART_IER_RLSI | UART_IER_RDI;
2159 serial_outp(up, UART_IER, up->ier);
2160
2161 if (up->port.flags & UPF_FOURPORT) {
2162 unsigned int icp;
2163 /*
2164 * Enable interrupts on the AST Fourport board
2165 */
2166 icp = (up->port.iobase & 0xfe0) | 0x01f;
2167 outb_p(0x80, icp);
2168 (void) inb_p(icp);
2169 }
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return 0;
2172}
2173
2174static void serial8250_shutdown(struct uart_port *port)
2175{
Jamie Iles49d57412010-12-01 23:39:35 +00002176 struct uart_8250_port *up =
2177 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 unsigned long flags;
2179
2180 /*
2181 * Disable interrupts from this port
2182 */
2183 up->ier = 0;
2184 serial_outp(up, UART_IER, 0);
2185
2186 spin_lock_irqsave(&up->port.lock, flags);
2187 if (up->port.flags & UPF_FOURPORT) {
2188 /* reset interrupts on the AST Fourport board */
2189 inb((up->port.iobase & 0xfe0) | 0x1f);
2190 up->port.mctrl |= TIOCM_OUT1;
2191 } else
2192 up->port.mctrl &= ~TIOCM_OUT2;
2193
2194 serial8250_set_mctrl(&up->port, up->port.mctrl);
2195 spin_unlock_irqrestore(&up->port.lock, flags);
2196
2197 /*
2198 * Disable break condition and FIFOs
2199 */
2200 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
2201 serial8250_clear_fifos(up);
2202
2203#ifdef CONFIG_SERIAL_8250_RSA
2204 /*
2205 * Reset the RSA board back to 115kbps compat mode.
2206 */
2207 disable_rsa(up);
2208#endif
2209
2210 /*
2211 * Read data port to reset things, and then unlink from
2212 * the IRQ chain.
2213 */
2214 (void) serial_in(up, UART_RX);
2215
Alex Williamson40b36da2007-02-14 00:33:04 -08002216 del_timer_sync(&up->timer);
2217 up->timer.function = serial8250_timeout;
Alan Coxd4e33fa2012-01-26 17:44:09 +00002218 if (up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 serial_unlink_irq_chain(up);
2220}
2221
2222static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
2223{
2224 unsigned int quot;
2225
2226 /*
2227 * Handle magic divisors for baud rates above baud_base on
2228 * SMSC SuperIO chips.
2229 */
2230 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2231 baud == (port->uartclk/4))
2232 quot = 0x8001;
2233 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2234 baud == (port->uartclk/8))
2235 quot = 0x8002;
2236 else
2237 quot = uart_get_divisor(port, baud);
2238
2239 return quot;
2240}
2241
Philippe Langlais235dae52010-07-29 17:13:57 +02002242void
2243serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2244 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Jamie Iles49d57412010-12-01 23:39:35 +00002246 struct uart_8250_port *up =
2247 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 unsigned char cval, fcr = 0;
2249 unsigned long flags;
2250 unsigned int baud, quot;
2251
2252 switch (termios->c_cflag & CSIZE) {
2253 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +01002254 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 break;
2256 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +01002257 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 break;
2259 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +01002260 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 break;
2262 default:
2263 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +01002264 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 break;
2266 }
2267
2268 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +01002269 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (termios->c_cflag & PARENB)
2271 cval |= UART_LCR_PARITY;
2272 if (!(termios->c_cflag & PARODD))
2273 cval |= UART_LCR_EPAR;
2274#ifdef CMSPAR
2275 if (termios->c_cflag & CMSPAR)
2276 cval |= UART_LCR_SPAR;
2277#endif
2278
2279 /*
2280 * Ask the core to calculate the divisor for us.
2281 */
Anton Vorontsov24d481e2009-09-19 13:13:20 -07002282 baud = uart_get_baud_rate(port, termios, old,
2283 port->uartclk / 16 / 0xffff,
2284 port->uartclk / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 quot = serial8250_get_divisor(port, baud);
2286
2287 /*
Russell King4ba5e352005-06-23 10:43:04 +01002288 * Oxford Semi 952 rev B workaround
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 */
Russell King4ba5e352005-06-23 10:43:04 +01002290 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
Alan Cox3e8d4e22008-02-04 22:27:53 -08002291 quot++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
2294 if (baud < 2400)
2295 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2296 else
2297 fcr = uart_config[up->port.type].fcr;
2298 }
2299
2300 /*
2301 * MCR-based auto flow control. When AFE is enabled, RTS will be
2302 * deasserted when the receive FIFO contains more characters than
2303 * the trigger, or the MCR RTS bit is cleared. In the case where
2304 * the remote UART is not using CTS auto flow control, we must
2305 * have sufficient FIFO entries for the latency of the remote
2306 * UART to respond. IOW, at least 32 bytes of FIFO.
2307 */
2308 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
2309 up->mcr &= ~UART_MCR_AFE;
2310 if (termios->c_cflag & CRTSCTS)
2311 up->mcr |= UART_MCR_AFE;
2312 }
2313
2314 /*
2315 * Ok, we're now changing the port state. Do it with
2316 * interrupts disabled.
2317 */
2318 spin_lock_irqsave(&up->port.lock, flags);
2319
2320 /*
2321 * Update the per-port timeout.
2322 */
2323 uart_update_timeout(port, termios->c_cflag, baud);
2324
2325 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2326 if (termios->c_iflag & INPCK)
2327 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2328 if (termios->c_iflag & (BRKINT | PARMRK))
2329 up->port.read_status_mask |= UART_LSR_BI;
2330
2331 /*
2332 * Characteres to ignore
2333 */
2334 up->port.ignore_status_mask = 0;
2335 if (termios->c_iflag & IGNPAR)
2336 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2337 if (termios->c_iflag & IGNBRK) {
2338 up->port.ignore_status_mask |= UART_LSR_BI;
2339 /*
2340 * If we're ignoring parity and break indicators,
2341 * ignore overruns too (for real raw support).
2342 */
2343 if (termios->c_iflag & IGNPAR)
2344 up->port.ignore_status_mask |= UART_LSR_OE;
2345 }
2346
2347 /*
2348 * ignore all characters if CREAD is not set
2349 */
2350 if ((termios->c_cflag & CREAD) == 0)
2351 up->port.ignore_status_mask |= UART_LSR_DR;
2352
2353 /*
2354 * CTS flow control flag and modem status interrupts
2355 */
Ingo Molnarf8b372a2010-11-13 16:21:58 +01002356 up->ier &= ~UART_IER_MSI;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002357 if (!(up->bugs & UART_BUG_NOMSR) &&
2358 UART_ENABLE_MS(&up->port, termios->c_cflag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 up->ier |= UART_IER_MSI;
2360 if (up->capabilities & UART_CAP_UUE)
Stephen Warren4539c242011-05-17 16:12:36 -06002361 up->ier |= UART_IER_UUE;
2362 if (up->capabilities & UART_CAP_RTOIE)
2363 up->ier |= UART_IER_RTOIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 serial_out(up, UART_IER, up->ier);
2366
2367 if (up->capabilities & UART_CAP_EFR) {
2368 unsigned char efr = 0;
2369 /*
2370 * TI16C752/Startech hardware flow control. FIXME:
2371 * - TI16C752 requires control thresholds to be set.
2372 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2373 */
2374 if (termios->c_cflag & CRTSCTS)
2375 efr |= UART_EFR_CTS;
2376
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08002377 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Søren Holm06315342011-09-02 22:55:37 +02002378 if (up->port.flags & UPF_EXAR_EFR)
2379 serial_outp(up, UART_XR_EFR, efr);
2380 else
2381 serial_outp(up, UART_EFR, efr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
2383
Russell Kingf2eda272008-09-01 21:47:59 +01002384#ifdef CONFIG_ARCH_OMAP
Jonathan McDowell255341c2006-08-14 23:05:32 -07002385 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
Russell King56685452008-09-01 21:25:33 +01002386 if (cpu_is_omap1510() && is_omap_port(up)) {
Jonathan McDowell255341c2006-08-14 23:05:32 -07002387 if (baud == 115200) {
2388 quot = 1;
2389 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2390 } else
2391 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2392 }
2393#endif
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 if (up->capabilities & UART_NATSEMI) {
2396 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
2397 serial_outp(up, UART_LCR, 0xe0);
2398 } else {
2399 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2400 }
2401
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +01002402 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 /*
2405 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2406 * is written without DLAB set, this mode will be disabled.
2407 */
2408 if (up->port.type == PORT_16750)
2409 serial_outp(up, UART_FCR, fcr);
2410
2411 serial_outp(up, UART_LCR, cval); /* reset DLAB */
2412 up->lcr = cval; /* Save LCR */
2413 if (up->port.type != PORT_16750) {
2414 if (fcr & UART_FCR_ENABLE_FIFO) {
2415 /* emulated UARTs (Lucent Venus 167x) need two steps */
2416 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
2417 }
2418 serial_outp(up, UART_FCR, fcr); /* set fcr */
2419 }
2420 serial8250_set_mctrl(&up->port, up->port.mctrl);
2421 spin_unlock_irqrestore(&up->port.lock, flags);
Alan Coxe991a2b2008-04-28 02:14:06 -07002422 /* Don't rewrite B0 */
2423 if (tty_termios_baud_rate(termios))
2424 tty_termios_encode_baud_rate(termios, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
Philippe Langlais235dae52010-07-29 17:13:57 +02002426EXPORT_SYMBOL(serial8250_do_set_termios);
2427
2428static void
2429serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2430 struct ktermios *old)
2431{
2432 if (port->set_termios)
2433 port->set_termios(port, termios, old);
2434 else
2435 serial8250_do_set_termios(port, termios, old);
2436}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438static void
Arnd Bergmanna0821df2010-06-01 22:53:11 +02002439serial8250_set_ldisc(struct uart_port *port, int new)
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002440{
Arnd Bergmanna0821df2010-06-01 22:53:11 +02002441 if (new == N_PPS) {
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002442 port->flags |= UPF_HARDPPS_CD;
2443 serial8250_enable_ms(port);
2444 } else
2445 port->flags &= ~UPF_HARDPPS_CD;
2446}
2447
Manuel Laussc161afe2010-09-25 15:13:45 +02002448
2449void serial8250_do_pm(struct uart_port *port, unsigned int state,
2450 unsigned int oldstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Jamie Iles49d57412010-12-01 23:39:35 +00002452 struct uart_8250_port *p =
2453 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 serial8250_set_sleep(p, state != 0);
Manuel Laussc161afe2010-09-25 15:13:45 +02002456}
2457EXPORT_SYMBOL(serial8250_do_pm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Manuel Laussc161afe2010-09-25 15:13:45 +02002459static void
2460serial8250_pm(struct uart_port *port, unsigned int state,
2461 unsigned int oldstate)
2462{
2463 if (port->pm)
2464 port->pm(port, state, oldstate);
2465 else
2466 serial8250_do_pm(port, state, oldstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
Russell Kingf2eda272008-09-01 21:47:59 +01002469static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2470{
2471 if (pt->port.iotype == UPIO_AU)
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002472 return 0x1000;
Russell Kingf2eda272008-09-01 21:47:59 +01002473#ifdef CONFIG_ARCH_OMAP
2474 if (is_omap_port(pt))
2475 return 0x16 << pt->port.regshift;
2476#endif
2477 return 8 << pt->port.regshift;
2478}
2479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480/*
2481 * Resource handling.
2482 */
2483static int serial8250_request_std_resource(struct uart_8250_port *up)
2484{
Russell Kingf2eda272008-09-01 21:47:59 +01002485 unsigned int size = serial8250_port_size(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 int ret = 0;
2487
2488 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002489 case UPIO_AU:
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002490 case UPIO_TSI:
2491 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 case UPIO_MEM:
2493 if (!up->port.mapbase)
2494 break;
2495
2496 if (!request_mem_region(up->port.mapbase, size, "serial")) {
2497 ret = -EBUSY;
2498 break;
2499 }
2500
2501 if (up->port.flags & UPF_IOREMAP) {
Alan Cox6f441fe2008-05-01 04:34:59 -07002502 up->port.membase = ioremap_nocache(up->port.mapbase,
2503 size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (!up->port.membase) {
2505 release_mem_region(up->port.mapbase, size);
2506 ret = -ENOMEM;
2507 }
2508 }
2509 break;
2510
2511 case UPIO_HUB6:
2512 case UPIO_PORT:
2513 if (!request_region(up->port.iobase, size, "serial"))
2514 ret = -EBUSY;
2515 break;
2516 }
2517 return ret;
2518}
2519
2520static void serial8250_release_std_resource(struct uart_8250_port *up)
2521{
Russell Kingf2eda272008-09-01 21:47:59 +01002522 unsigned int size = serial8250_port_size(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002525 case UPIO_AU:
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002526 case UPIO_TSI:
2527 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 case UPIO_MEM:
2529 if (!up->port.mapbase)
2530 break;
2531
2532 if (up->port.flags & UPF_IOREMAP) {
2533 iounmap(up->port.membase);
2534 up->port.membase = NULL;
2535 }
2536
2537 release_mem_region(up->port.mapbase, size);
2538 break;
2539
2540 case UPIO_HUB6:
2541 case UPIO_PORT:
2542 release_region(up->port.iobase, size);
2543 break;
2544 }
2545}
2546
2547static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2548{
2549 unsigned long start = UART_RSA_BASE << up->port.regshift;
2550 unsigned int size = 8 << up->port.regshift;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002551 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
2553 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 case UPIO_HUB6:
2555 case UPIO_PORT:
2556 start += up->port.iobase;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002557 if (request_region(start, size, "serial-rsa"))
2558 ret = 0;
2559 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 ret = -EBUSY;
2561 break;
2562 }
2563
2564 return ret;
2565}
2566
2567static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2568{
2569 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2570 unsigned int size = 8 << up->port.regshift;
2571
2572 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 case UPIO_HUB6:
2574 case UPIO_PORT:
2575 release_region(up->port.iobase + offset, size);
2576 break;
2577 }
2578}
2579
2580static void serial8250_release_port(struct uart_port *port)
2581{
Jamie Iles49d57412010-12-01 23:39:35 +00002582 struct uart_8250_port *up =
2583 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585 serial8250_release_std_resource(up);
2586 if (up->port.type == PORT_RSA)
2587 serial8250_release_rsa_resource(up);
2588}
2589
2590static int serial8250_request_port(struct uart_port *port)
2591{
Jamie Iles49d57412010-12-01 23:39:35 +00002592 struct uart_8250_port *up =
2593 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 int ret = 0;
2595
2596 ret = serial8250_request_std_resource(up);
2597 if (ret == 0 && up->port.type == PORT_RSA) {
2598 ret = serial8250_request_rsa_resource(up);
2599 if (ret < 0)
2600 serial8250_release_std_resource(up);
2601 }
2602
2603 return ret;
2604}
2605
2606static void serial8250_config_port(struct uart_port *port, int flags)
2607{
Jamie Iles49d57412010-12-01 23:39:35 +00002608 struct uart_8250_port *up =
2609 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 int probeflags = PROBE_ANY;
2611 int ret;
2612
2613 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 * Find the region that we can probe for. This in turn
2615 * tells us whether we can probe for the type of port.
2616 */
2617 ret = serial8250_request_std_resource(up);
2618 if (ret < 0)
2619 return;
2620
2621 ret = serial8250_request_rsa_resource(up);
2622 if (ret < 0)
2623 probeflags &= ~PROBE_RSA;
2624
Alan Coxb8e7e402009-05-28 14:01:35 +01002625 if (up->port.iotype != up->cur_iotype)
2626 set_io_from_upio(port);
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 if (flags & UART_CONFIG_TYPE)
2629 autoconfig(up, probeflags);
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002630
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002631 /* if access method is AU, it is a 16550 with a quirk */
2632 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
2633 up->bugs |= UART_BUG_NOMSR;
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2636 autoconfig_irq(up);
2637
2638 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2639 serial8250_release_rsa_resource(up);
2640 if (up->port.type == PORT_UNKNOWN)
2641 serial8250_release_std_resource(up);
2642}
2643
2644static int
2645serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2646{
Yinghai Lua62c4132008-08-19 20:49:55 -07002647 if (ser->irq >= nr_irqs || ser->irq < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2649 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2650 ser->type == PORT_STARTECH)
2651 return -EINVAL;
2652 return 0;
2653}
2654
2655static const char *
2656serial8250_type(struct uart_port *port)
2657{
2658 int type = port->type;
2659
2660 if (type >= ARRAY_SIZE(uart_config))
2661 type = 0;
2662 return uart_config[type].name;
2663}
2664
2665static struct uart_ops serial8250_pops = {
2666 .tx_empty = serial8250_tx_empty,
2667 .set_mctrl = serial8250_set_mctrl,
2668 .get_mctrl = serial8250_get_mctrl,
2669 .stop_tx = serial8250_stop_tx,
2670 .start_tx = serial8250_start_tx,
2671 .stop_rx = serial8250_stop_rx,
2672 .enable_ms = serial8250_enable_ms,
2673 .break_ctl = serial8250_break_ctl,
2674 .startup = serial8250_startup,
2675 .shutdown = serial8250_shutdown,
2676 .set_termios = serial8250_set_termios,
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002677 .set_ldisc = serial8250_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 .pm = serial8250_pm,
2679 .type = serial8250_type,
2680 .release_port = serial8250_release_port,
2681 .request_port = serial8250_request_port,
2682 .config_port = serial8250_config_port,
2683 .verify_port = serial8250_verify_port,
Jason Wesself2d937f2008-04-17 20:05:37 +02002684#ifdef CONFIG_CONSOLE_POLL
2685 .poll_get_char = serial8250_get_poll_char,
2686 .poll_put_char = serial8250_put_poll_char,
2687#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688};
2689
2690static struct uart_8250_port serial8250_ports[UART_NR];
2691
Alan Coxaf7f3742010-10-18 11:38:02 -07002692static void (*serial8250_isa_config)(int port, struct uart_port *up,
2693 unsigned short *capabilities);
2694
2695void serial8250_set_isa_configurator(
2696 void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
2697{
2698 serial8250_isa_config = v;
2699}
2700EXPORT_SYMBOL(serial8250_set_isa_configurator);
2701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702static void __init serial8250_isa_init_ports(void)
2703{
2704 struct uart_8250_port *up;
2705 static int first = 1;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002706 int i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 if (!first)
2709 return;
2710 first = 0;
2711
Dave Jonesa61c2d72006-01-07 23:18:19 +00002712 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 struct uart_8250_port *up = &serial8250_ports[i];
2714
2715 up->port.line = i;
2716 spin_lock_init(&up->port.lock);
2717
2718 init_timer(&up->timer);
2719 up->timer.function = serial8250_timeout;
2720
2721 /*
2722 * ALPHA_KLUDGE_MCR needs to be killed.
2723 */
2724 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2725 up->mcr_force = ALPHA_KLUDGE_MCR;
2726
2727 up->port.ops = &serial8250_pops;
2728 }
2729
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002730 if (share_irqs)
2731 irqflag = IRQF_SHARED;
2732
Russell King44454bc2005-06-30 22:41:22 +01002733 for (i = 0, up = serial8250_ports;
Dave Jonesa61c2d72006-01-07 23:18:19 +00002734 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 i++, up++) {
2736 up->port.iobase = old_serial_port[i].port;
2737 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002738 up->port.irqflags = old_serial_port[i].irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 up->port.uartclk = old_serial_port[i].baud_base * 16;
2740 up->port.flags = old_serial_port[i].flags;
2741 up->port.hub6 = old_serial_port[i].hub6;
2742 up->port.membase = old_serial_port[i].iomem_base;
2743 up->port.iotype = old_serial_port[i].io_type;
2744 up->port.regshift = old_serial_port[i].iomem_reg_shift;
David Daney7d6a07d2009-01-02 13:49:47 +00002745 set_io_from_upio(&up->port);
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002746 up->port.irqflags |= irqflag;
Alan Coxaf7f3742010-10-18 11:38:02 -07002747 if (serial8250_isa_config != NULL)
2748 serial8250_isa_config(i, &up->port, &up->capabilities);
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 }
2751}
2752
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08002753static void
2754serial8250_init_fixed_type_port(struct uart_8250_port *up, unsigned int type)
2755{
2756 up->port.type = type;
2757 up->port.fifosize = uart_config[type].fifo_size;
2758 up->capabilities = uart_config[type].flags;
2759 up->tx_loadsz = uart_config[type].tx_loadsz;
2760}
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762static void __init
2763serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2764{
2765 int i;
2766
Alan Coxb8e7e402009-05-28 14:01:35 +01002767 for (i = 0; i < nr_uarts; i++) {
2768 struct uart_8250_port *up = &serial8250_ports[i];
2769 up->cur_iotype = 0xFF;
2770 }
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 serial8250_isa_init_ports();
2773
Dave Jonesa61c2d72006-01-07 23:18:19 +00002774 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 struct uart_8250_port *up = &serial8250_ports[i];
2776
2777 up->port.dev = dev;
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08002778
2779 if (up->port.flags & UPF_FIXED_TYPE)
2780 serial8250_init_fixed_type_port(up, up->port.type);
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 uart_add_one_port(drv, &up->port);
2783 }
2784}
2785
2786#ifdef CONFIG_SERIAL_8250_CONSOLE
2787
Russell Kingd3587882006-03-20 20:00:09 +00002788static void serial8250_console_putchar(struct uart_port *port, int ch)
2789{
Jamie Iles49d57412010-12-01 23:39:35 +00002790 struct uart_8250_port *up =
2791 container_of(port, struct uart_8250_port, port);
Russell Kingd3587882006-03-20 20:00:09 +00002792
2793 wait_for_xmitr(up, UART_LSR_THRE);
2794 serial_out(up, UART_TX, ch);
2795}
2796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797/*
2798 * Print a string to the serial port trying not to disturb
2799 * any possible real use of the port...
2800 *
2801 * The console_lock must be held when we get here.
2802 */
2803static void
2804serial8250_console_write(struct console *co, const char *s, unsigned int count)
2805{
2806 struct uart_8250_port *up = &serial8250_ports[co->index];
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002807 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 unsigned int ier;
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002809 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Andrew Morton78512ec2005-11-07 00:59:13 -08002811 touch_nmi_watchdog();
2812
Andrew Morton68aa2c02006-06-30 02:29:59 -07002813 local_irq_save(flags);
2814 if (up->port.sysrq) {
Paul Gortmaker86b21192011-12-04 18:42:22 -05002815 /* serial8250_handle_irq() already took the lock */
Andrew Morton68aa2c02006-06-30 02:29:59 -07002816 locked = 0;
2817 } else if (oops_in_progress) {
2818 locked = spin_trylock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002819 } else
Andrew Morton68aa2c02006-06-30 02:29:59 -07002820 spin_lock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002821
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 /*
Ralf Baechledc7bf132006-02-15 09:59:47 +00002823 * First save the IER then disable the interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 */
2825 ier = serial_in(up, UART_IER);
2826
2827 if (up->capabilities & UART_CAP_UUE)
2828 serial_out(up, UART_IER, UART_IER_UUE);
2829 else
2830 serial_out(up, UART_IER, 0);
2831
Russell Kingd3587882006-03-20 20:00:09 +00002832 uart_console_write(&up->port, s, count, serial8250_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
2834 /*
2835 * Finally, wait for transmitter to become empty
2836 * and restore the IER
2837 */
Alan Coxf91a3712006-01-21 14:59:12 +00002838 wait_for_xmitr(up, BOTH_EMPTY);
Russell Kinga88d75b2006-04-30 11:30:15 +01002839 serial_out(up, UART_IER, ier);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002840
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002841 /*
2842 * The receive handling will happen properly because the
2843 * receive ready bit will still be set; it is not cleared
2844 * on read. However, modem control will not, we must
2845 * call it if we have saved something in the saved flags
2846 * while processing with interrupts off.
2847 */
2848 if (up->msr_saved_flags)
Paul Gortmaker3986fb22011-12-04 18:42:20 -05002849 serial8250_modem_status(up);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002850
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002851 if (locked)
Andrew Morton68aa2c02006-06-30 02:29:59 -07002852 spin_unlock(&up->port.lock);
2853 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854}
2855
Vivek Goyal118c0ac2007-01-11 01:52:44 +01002856static int __init serial8250_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
2858 struct uart_port *port;
2859 int baud = 9600;
2860 int bits = 8;
2861 int parity = 'n';
2862 int flow = 'n';
2863
2864 /*
2865 * Check whether an invalid uart number has been specified, and
2866 * if so, search for the first available port that does have
2867 * console support.
2868 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002869 if (co->index >= nr_uarts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 co->index = 0;
2871 port = &serial8250_ports[co->index].port;
2872 if (!port->iobase && !port->membase)
2873 return -ENODEV;
2874
2875 if (options)
2876 uart_parse_options(options, &baud, &parity, &bits, &flow);
2877
2878 return uart_set_options(port, co, baud, parity, bits, flow);
2879}
2880
Daniel Ritzb6b1d872007-08-03 16:07:43 +02002881static int serial8250_console_early_setup(void)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002882{
2883 return serial8250_find_port_for_earlycon();
2884}
2885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886static struct console serial8250_console = {
2887 .name = "ttyS",
2888 .write = serial8250_console_write,
2889 .device = uart_console_device,
2890 .setup = serial8250_console_setup,
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002891 .early_setup = serial8250_console_early_setup,
Peter Zijlstraa80c49d2010-11-15 21:11:12 +01002892 .flags = CON_PRINTBUFFER | CON_ANYTIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 .index = -1,
2894 .data = &serial8250_reg,
2895};
2896
2897static int __init serial8250_console_init(void)
2898{
Eric W. Biederman05d81d22008-07-12 13:47:53 -07002899 if (nr_uarts > UART_NR)
2900 nr_uarts = UART_NR;
2901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 serial8250_isa_init_ports();
2903 register_console(&serial8250_console);
2904 return 0;
2905}
2906console_initcall(serial8250_console_init);
2907
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002908int serial8250_find_port(struct uart_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
2910 int line;
2911 struct uart_port *port;
2912
Dave Jonesa61c2d72006-01-07 23:18:19 +00002913 for (line = 0; line < nr_uarts; line++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 port = &serial8250_ports[line].port;
Russell King50aec3b52006-01-04 18:13:03 +00002915 if (uart_match_port(p, port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return line;
2917 }
2918 return -ENODEV;
2919}
2920
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921#define SERIAL8250_CONSOLE &serial8250_console
2922#else
2923#define SERIAL8250_CONSOLE NULL
2924#endif
2925
2926static struct uart_driver serial8250_reg = {
2927 .owner = THIS_MODULE,
2928 .driver_name = "serial",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 .dev_name = "ttyS",
2930 .major = TTY_MAJOR,
2931 .minor = 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 .cons = SERIAL8250_CONSOLE,
2933};
2934
Russell Kingd856c662006-02-23 10:22:13 +00002935/*
2936 * early_serial_setup - early registration for 8250 ports
2937 *
2938 * Setup an 8250 port structure prior to console initialisation. Use
2939 * after console initialisation will cause undefined behaviour.
2940 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941int __init early_serial_setup(struct uart_port *port)
2942{
David Daneyb4304282009-01-02 13:49:41 +00002943 struct uart_port *p;
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 if (port->line >= ARRAY_SIZE(serial8250_ports))
2946 return -ENODEV;
2947
2948 serial8250_isa_init_ports();
David Daneyb4304282009-01-02 13:49:41 +00002949 p = &serial8250_ports[port->line].port;
2950 p->iobase = port->iobase;
2951 p->membase = port->membase;
2952 p->irq = port->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002953 p->irqflags = port->irqflags;
David Daneyb4304282009-01-02 13:49:41 +00002954 p->uartclk = port->uartclk;
2955 p->fifosize = port->fifosize;
2956 p->regshift = port->regshift;
2957 p->iotype = port->iotype;
2958 p->flags = port->flags;
2959 p->mapbase = port->mapbase;
2960 p->private_data = port->private_data;
Helge Deller125c97d2009-01-13 22:51:07 +01002961 p->type = port->type;
2962 p->line = port->line;
David Daney7d6a07d2009-01-02 13:49:47 +00002963
2964 set_io_from_upio(p);
2965 if (port->serial_in)
2966 p->serial_in = port->serial_in;
2967 if (port->serial_out)
2968 p->serial_out = port->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01002969 if (port->handle_irq)
2970 p->handle_irq = port->handle_irq;
2971 else
2972 p->handle_irq = serial8250_default_handle_irq;
David Daney7d6a07d2009-01-02 13:49:47 +00002973
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 return 0;
2975}
2976
2977/**
2978 * serial8250_suspend_port - suspend one serial port
2979 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 *
2981 * Suspend one serial port.
2982 */
2983void serial8250_suspend_port(int line)
2984{
2985 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2986}
2987
2988/**
2989 * serial8250_resume_port - resume one serial port
2990 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 *
2992 * Resume one serial port.
2993 */
2994void serial8250_resume_port(int line)
2995{
David Woodhouseb5b82df2007-05-17 14:27:39 +08002996 struct uart_8250_port *up = &serial8250_ports[line];
2997
2998 if (up->capabilities & UART_NATSEMI) {
David Woodhouseb5b82df2007-05-17 14:27:39 +08002999 /* Ensure it's still in high speed mode */
3000 serial_outp(up, UART_LCR, 0xE0);
3001
Yin Kangkai0d0389e2011-02-09 11:35:18 +08003002 ns16550a_goto_highspeed(up);
David Woodhouseb5b82df2007-05-17 14:27:39 +08003003
3004 serial_outp(up, UART_LCR, 0);
Yin Kangkai95926d22011-02-09 11:34:20 +08003005 up->port.uartclk = 921600*16;
David Woodhouseb5b82df2007-05-17 14:27:39 +08003006 }
3007 uart_resume_port(&serial8250_reg, &up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
3010/*
3011 * Register a set of serial devices attached to a platform device. The
3012 * list is terminated with a zero flags entry, which means we expect
3013 * all entries to have at least UPF_BOOT_AUTOCONF set.
3014 */
Russell King3ae5eae2005-11-09 22:32:44 +00003015static int __devinit serial8250_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
Russell King3ae5eae2005-11-09 22:32:44 +00003017 struct plat_serial8250_port *p = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 struct uart_port port;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003019 int ret, i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
3021 memset(&port, 0, sizeof(struct uart_port));
3022
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003023 if (share_irqs)
3024 irqflag = IRQF_SHARED;
3025
Russell Kingec9f47c2005-06-27 11:12:54 +01003026 for (i = 0; p && p->flags != 0; p++, i++) {
Will Newton74a197412008-02-04 22:27:50 -08003027 port.iobase = p->iobase;
3028 port.membase = p->membase;
3029 port.irq = p->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07003030 port.irqflags = p->irqflags;
Will Newton74a197412008-02-04 22:27:50 -08003031 port.uartclk = p->uartclk;
3032 port.regshift = p->regshift;
3033 port.iotype = p->iotype;
3034 port.flags = p->flags;
3035 port.mapbase = p->mapbase;
3036 port.hub6 = p->hub6;
3037 port.private_data = p->private_data;
David Daney8e23fcc2009-01-02 13:49:54 +00003038 port.type = p->type;
David Daney7d6a07d2009-01-02 13:49:47 +00003039 port.serial_in = p->serial_in;
3040 port.serial_out = p->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01003041 port.handle_irq = p->handle_irq;
Philippe Langlais235dae52010-07-29 17:13:57 +02003042 port.set_termios = p->set_termios;
Manuel Laussc161afe2010-09-25 15:13:45 +02003043 port.pm = p->pm;
Will Newton74a197412008-02-04 22:27:50 -08003044 port.dev = &dev->dev;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003045 port.irqflags |= irqflag;
Russell Kingec9f47c2005-06-27 11:12:54 +01003046 ret = serial8250_register_port(&port);
3047 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00003048 dev_err(&dev->dev, "unable to register port at index %d "
Josh Boyer4f640ef2007-07-23 18:43:44 -07003049 "(IO%lx MEM%llx IRQ%d): %d\n", i,
3050 p->iobase, (unsigned long long)p->mapbase,
3051 p->irq, ret);
Russell Kingec9f47c2005-06-27 11:12:54 +01003052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 }
3054 return 0;
3055}
3056
3057/*
3058 * Remove serial ports registered against a platform device.
3059 */
Russell King3ae5eae2005-11-09 22:32:44 +00003060static int __devexit serial8250_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
3062 int i;
3063
Dave Jonesa61c2d72006-01-07 23:18:19 +00003064 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 struct uart_8250_port *up = &serial8250_ports[i];
3066
Russell King3ae5eae2005-11-09 22:32:44 +00003067 if (up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 serial8250_unregister_port(i);
3069 }
3070 return 0;
3071}
3072
Russell King3ae5eae2005-11-09 22:32:44 +00003073static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
3075 int i;
3076
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 for (i = 0; i < UART_NR; i++) {
3078 struct uart_8250_port *up = &serial8250_ports[i];
3079
Russell King3ae5eae2005-11-09 22:32:44 +00003080 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 uart_suspend_port(&serial8250_reg, &up->port);
3082 }
3083
3084 return 0;
3085}
3086
Russell King3ae5eae2005-11-09 22:32:44 +00003087static int serial8250_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088{
3089 int i;
3090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 for (i = 0; i < UART_NR; i++) {
3092 struct uart_8250_port *up = &serial8250_ports[i];
3093
Russell King3ae5eae2005-11-09 22:32:44 +00003094 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
David Woodhouseb5b82df2007-05-17 14:27:39 +08003095 serial8250_resume_port(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 }
3097
3098 return 0;
3099}
3100
Russell King3ae5eae2005-11-09 22:32:44 +00003101static struct platform_driver serial8250_isa_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 .probe = serial8250_probe,
3103 .remove = __devexit_p(serial8250_remove),
3104 .suspend = serial8250_suspend,
3105 .resume = serial8250_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00003106 .driver = {
3107 .name = "serial8250",
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003108 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00003109 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110};
3111
3112/*
3113 * This "device" covers _all_ ISA 8250-compatible serial devices listed
3114 * in the table in include/asm/serial.h
3115 */
3116static struct platform_device *serial8250_isa_devs;
3117
3118/*
3119 * serial8250_register_port and serial8250_unregister_port allows for
3120 * 16x50 serial ports to be configured at run-time, to support PCMCIA
3121 * modems and PCI multiport cards.
3122 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003123static DEFINE_MUTEX(serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
3125static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
3126{
3127 int i;
3128
3129 /*
3130 * First, find a port entry which matches.
3131 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003132 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (uart_match_port(&serial8250_ports[i].port, port))
3134 return &serial8250_ports[i];
3135
3136 /*
3137 * We didn't find a matching entry, so look for the first
3138 * free entry. We look for one which hasn't been previously
3139 * used (indicated by zero iobase).
3140 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003141 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
3143 serial8250_ports[i].port.iobase == 0)
3144 return &serial8250_ports[i];
3145
3146 /*
3147 * That also failed. Last resort is to find any entry which
3148 * doesn't have a real port associated with it.
3149 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003150 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
3152 return &serial8250_ports[i];
3153
3154 return NULL;
3155}
3156
3157/**
3158 * serial8250_register_port - register a serial port
3159 * @port: serial port template
3160 *
3161 * Configure the serial port specified by the request. If the
3162 * port exists and is in use, it is hung up and unregistered
3163 * first.
3164 *
3165 * The port is then probed and if necessary the IRQ is autodetected
3166 * If this fails an error is returned.
3167 *
3168 * On success the port is ready to use and the line number is returned.
3169 */
3170int serial8250_register_port(struct uart_port *port)
3171{
3172 struct uart_8250_port *uart;
3173 int ret = -ENOSPC;
3174
3175 if (port->uartclk == 0)
3176 return -EINVAL;
3177
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003178 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
3180 uart = serial8250_find_match_or_unused(port);
3181 if (uart) {
3182 uart_remove_one_port(&serial8250_reg, &uart->port);
3183
Will Newton74a197412008-02-04 22:27:50 -08003184 uart->port.iobase = port->iobase;
3185 uart->port.membase = port->membase;
3186 uart->port.irq = port->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07003187 uart->port.irqflags = port->irqflags;
Will Newton74a197412008-02-04 22:27:50 -08003188 uart->port.uartclk = port->uartclk;
3189 uart->port.fifosize = port->fifosize;
3190 uart->port.regshift = port->regshift;
3191 uart->port.iotype = port->iotype;
3192 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
3193 uart->port.mapbase = port->mapbase;
3194 uart->port.private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 if (port->dev)
3196 uart->port.dev = port->dev;
David Daney8e23fcc2009-01-02 13:49:54 +00003197
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08003198 if (port->flags & UPF_FIXED_TYPE)
3199 serial8250_init_fixed_type_port(uart, port->type);
David Daney8e23fcc2009-01-02 13:49:54 +00003200
David Daney7d6a07d2009-01-02 13:49:47 +00003201 set_io_from_upio(&uart->port);
3202 /* Possibly override default I/O functions. */
3203 if (port->serial_in)
3204 uart->port.serial_in = port->serial_in;
3205 if (port->serial_out)
3206 uart->port.serial_out = port->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01003207 if (port->handle_irq)
3208 uart->port.handle_irq = port->handle_irq;
Philippe Langlais235dae52010-07-29 17:13:57 +02003209 /* Possibly override set_termios call */
3210 if (port->set_termios)
3211 uart->port.set_termios = port->set_termios;
Manuel Laussc161afe2010-09-25 15:13:45 +02003212 if (port->pm)
3213 uart->port.pm = port->pm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Alan Coxaf7f3742010-10-18 11:38:02 -07003215 if (serial8250_isa_config != NULL)
3216 serial8250_isa_config(0, &uart->port,
3217 &uart->capabilities);
3218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 ret = uart_add_one_port(&serial8250_reg, &uart->port);
3220 if (ret == 0)
3221 ret = uart->port.line;
3222 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003223 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
3225 return ret;
3226}
3227EXPORT_SYMBOL(serial8250_register_port);
3228
3229/**
3230 * serial8250_unregister_port - remove a 16x50 serial port at runtime
3231 * @line: serial line number
3232 *
3233 * Remove one serial port. This may not be called from interrupt
3234 * context. We hand the port back to the our control.
3235 */
3236void serial8250_unregister_port(int line)
3237{
3238 struct uart_8250_port *uart = &serial8250_ports[line];
3239
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003240 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 uart_remove_one_port(&serial8250_reg, &uart->port);
3242 if (serial8250_isa_devs) {
3243 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
3244 uart->port.type = PORT_UNKNOWN;
3245 uart->port.dev = &serial8250_isa_devs->dev;
leitao@linux.vnet.ibm.comcb01ece2011-05-26 11:18:39 -03003246 uart->capabilities = uart_config[uart->port.type].flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 uart_add_one_port(&serial8250_reg, &uart->port);
3248 } else {
3249 uart->port.dev = NULL;
3250 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003251 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253EXPORT_SYMBOL(serial8250_unregister_port);
3254
3255static int __init serial8250_init(void)
3256{
Alan Cox25db8ad2008-08-19 20:49:40 -07003257 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
Dave Jonesa61c2d72006-01-07 23:18:19 +00003259 if (nr_uarts > UART_NR)
3260 nr_uarts = UART_NR;
3261
Paul Bollef1fb9bb2008-12-30 14:06:43 +01003262 printk(KERN_INFO "Serial: 8250/16550 driver, "
Dave Jonesa61c2d72006-01-07 23:18:19 +00003263 "%d ports, IRQ sharing %sabled\n", nr_uarts,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 share_irqs ? "en" : "dis");
3265
David Millerb70ac772008-10-13 10:36:31 +01003266#ifdef CONFIG_SPARC
3267 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
3268#else
3269 serial8250_reg.nr = UART_NR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 ret = uart_register_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003271#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 if (ret)
3273 goto out;
3274
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003275 serial8250_isa_devs = platform_device_alloc("serial8250",
3276 PLAT8250_DEV_LEGACY);
3277 if (!serial8250_isa_devs) {
3278 ret = -ENOMEM;
Russell Kingbc965a72006-01-18 09:54:29 +00003279 goto unreg_uart_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
3281
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003282 ret = platform_device_add(serial8250_isa_devs);
3283 if (ret)
3284 goto put_dev;
3285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
3287
Russell Kingbc965a72006-01-18 09:54:29 +00003288 ret = platform_driver_register(&serial8250_isa_driver);
3289 if (ret == 0)
3290 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Russell Kingbc965a72006-01-18 09:54:29 +00003292 platform_device_del(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003293put_dev:
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003294 platform_device_put(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003295unreg_uart_drv:
David Millerb70ac772008-10-13 10:36:31 +01003296#ifdef CONFIG_SPARC
3297 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3298#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003300#endif
Alan Cox25db8ad2008-08-19 20:49:40 -07003301out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 return ret;
3303}
3304
3305static void __exit serial8250_exit(void)
3306{
3307 struct platform_device *isa_dev = serial8250_isa_devs;
3308
3309 /*
3310 * This tells serial8250_unregister_port() not to re-register
3311 * the ports (thereby making serial8250_isa_driver permanently
3312 * in use.)
3313 */
3314 serial8250_isa_devs = NULL;
3315
Russell King3ae5eae2005-11-09 22:32:44 +00003316 platform_driver_unregister(&serial8250_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 platform_device_unregister(isa_dev);
3318
David Millerb70ac772008-10-13 10:36:31 +01003319#ifdef CONFIG_SPARC
3320 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3321#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003323#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324}
3325
3326module_init(serial8250_init);
3327module_exit(serial8250_exit);
3328
3329EXPORT_SYMBOL(serial8250_suspend_port);
3330EXPORT_SYMBOL(serial8250_resume_port);
3331
3332MODULE_LICENSE("GPL");
Adrian Bunkd87a6d92008-07-16 21:53:31 +01003333MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
3335module_param(share_irqs, uint, 0644);
3336MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
3337 " (unsafe)");
3338
Dave Jonesa61c2d72006-01-07 23:18:19 +00003339module_param(nr_uarts, uint, 0644);
3340MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
3341
Chuck Ebbertd41a4b52009-10-01 15:44:26 -07003342module_param(skip_txen_test, uint, 0644);
3343MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
3344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345#ifdef CONFIG_SERIAL_8250_RSA
3346module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
3347MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
3348#endif
3349MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);