blob: 9674eac5535c695af1429e58108d57bf58c2f831 [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
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100490/* Uart divisor latch read */
491static inline int _serial_dl_read(struct uart_8250_port *up)
492{
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500493 return serial_in(up, UART_DLL) | serial_in(up, UART_DLM) << 8;
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100494}
495
496/* Uart divisor latch write */
497static inline void _serial_dl_write(struct uart_8250_port *up, int value)
498{
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500499 serial_out(up, UART_DLL, value & 0xff);
500 serial_out(up, UART_DLM, value >> 8 & 0xff);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100501}
502
Manuel Lauss12bf3f22010-07-15 21:45:05 +0200503#if defined(CONFIG_MIPS_ALCHEMY)
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100504/* Au1x00 haven't got a standard divisor latch */
505static int serial_dl_read(struct uart_8250_port *up)
506{
507 if (up->port.iotype == UPIO_AU)
508 return __raw_readl(up->port.membase + 0x28);
509 else
510 return _serial_dl_read(up);
511}
512
513static void serial_dl_write(struct uart_8250_port *up, int value)
514{
515 if (up->port.iotype == UPIO_AU)
516 __raw_writel(value, up->port.membase + 0x28);
517 else
518 _serial_dl_write(up, value);
519}
Alan Cox6f803cd2008-02-08 04:18:52 -0800520#elif defined(CONFIG_SERIAL_8250_RM9K)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700521static int serial_dl_read(struct uart_8250_port *up)
522{
523 return (up->port.iotype == UPIO_RM9000) ?
524 (((__raw_readl(up->port.membase + 0x10) << 8) |
525 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
526 _serial_dl_read(up);
527}
528
529static void serial_dl_write(struct uart_8250_port *up, int value)
530{
531 if (up->port.iotype == UPIO_RM9000) {
532 __raw_writel(value, up->port.membase + 0x08);
533 __raw_writel(value >> 8, up->port.membase + 0x10);
534 } else {
535 _serial_dl_write(up, value);
536 }
537}
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100538#else
539#define serial_dl_read(up) _serial_dl_read(up)
540#define serial_dl_write(up, value) _serial_dl_write(up, value)
541#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543/*
544 * For the 16C950
545 */
546static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
547{
548 serial_out(up, UART_SCR, offset);
549 serial_out(up, UART_ICR, value);
550}
551
552static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
553{
554 unsigned int value;
555
556 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
557 serial_out(up, UART_SCR, offset);
558 value = serial_in(up, UART_ICR);
559 serial_icr_write(up, UART_ACR, up->acr);
560
561 return value;
562}
563
564/*
565 * FIFO support.
566 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100567static void serial8250_clear_fifos(struct uart_8250_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 if (p->capabilities & UART_CAP_FIFO) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500570 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
571 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500573 serial_out(p, UART_FCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575}
576
577/*
578 * IER sleep support. UARTs which have EFRs need the "extended
579 * capability" bit enabled. Note that on XR16C850s, we need to
580 * reset LCR to write to IER.
581 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100582static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 if (p->capabilities & UART_CAP_SLEEP) {
585 if (p->capabilities & UART_CAP_EFR) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500586 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
587 serial_out(p, UART_EFR, UART_EFR_ECB);
588 serial_out(p, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500590 serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (p->capabilities & UART_CAP_EFR) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500592 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
593 serial_out(p, UART_EFR, 0);
594 serial_out(p, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596 }
597}
598
599#ifdef CONFIG_SERIAL_8250_RSA
600/*
601 * Attempts to turn on the RSA FIFO. Returns zero on failure.
602 * We set the port uart clock rate if we succeed.
603 */
604static int __enable_rsa(struct uart_8250_port *up)
605{
606 unsigned char mode;
607 int result;
608
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500609 mode = serial_in(up, UART_RSA_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 result = mode & UART_RSA_MSR_FIFO;
611
612 if (!result) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500613 serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
614 mode = serial_in(up, UART_RSA_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 result = mode & UART_RSA_MSR_FIFO;
616 }
617
618 if (result)
619 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
620
621 return result;
622}
623
624static void enable_rsa(struct uart_8250_port *up)
625{
626 if (up->port.type == PORT_RSA) {
627 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
628 spin_lock_irq(&up->port.lock);
629 __enable_rsa(up);
630 spin_unlock_irq(&up->port.lock);
631 }
632 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500633 serial_out(up, UART_RSA_FRR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635}
636
637/*
638 * Attempts to turn off the RSA FIFO. Returns zero on failure.
639 * It is unknown why interrupts were disabled in here. However,
640 * the caller is expected to preserve this behaviour by grabbing
641 * the spinlock before calling this function.
642 */
643static void disable_rsa(struct uart_8250_port *up)
644{
645 unsigned char mode;
646 int result;
647
648 if (up->port.type == PORT_RSA &&
649 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
650 spin_lock_irq(&up->port.lock);
651
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500652 mode = serial_in(up, UART_RSA_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 result = !(mode & UART_RSA_MSR_FIFO);
654
655 if (!result) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500656 serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
657 mode = serial_in(up, UART_RSA_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 result = !(mode & UART_RSA_MSR_FIFO);
659 }
660
661 if (result)
662 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
663 spin_unlock_irq(&up->port.lock);
664 }
665}
666#endif /* CONFIG_SERIAL_8250_RSA */
667
668/*
669 * This is a quickie test to see how big the FIFO is.
670 * It doesn't work at all the time, more's the pity.
671 */
672static int size_fifo(struct uart_8250_port *up)
673{
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100674 unsigned char old_fcr, old_mcr, old_lcr;
675 unsigned short old_dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int count;
677
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500678 old_lcr = serial_in(up, UART_LCR);
679 serial_out(up, UART_LCR, 0);
680 old_fcr = serial_in(up, UART_FCR);
681 old_mcr = serial_in(up, UART_MCR);
682 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500684 serial_out(up, UART_MCR, UART_MCR_LOOP);
685 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100686 old_dl = serial_dl_read(up);
687 serial_dl_write(up, 0x0001);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500688 serial_out(up, UART_LCR, 0x03);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 for (count = 0; count < 256; count++)
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500690 serial_out(up, UART_TX, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 mdelay(20);/* FIXME - schedule_timeout */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500692 for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 (count < 256); count++)
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500694 serial_in(up, UART_RX);
695 serial_out(up, UART_FCR, old_fcr);
696 serial_out(up, UART_MCR, old_mcr);
697 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100698 serial_dl_write(up, old_dl);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500699 serial_out(up, UART_LCR, old_lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 return count;
702}
703
704/*
705 * Read UART ID using the divisor method - set DLL and DLM to zero
706 * and the revision will be in DLL and device type in DLM. We
707 * preserve the device state across this.
708 */
709static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
710{
711 unsigned char old_dll, old_dlm, old_lcr;
712 unsigned int id;
713
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500714 old_lcr = serial_in(p, UART_LCR);
715 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500717 old_dll = serial_in(p, UART_DLL);
718 old_dlm = serial_in(p, UART_DLM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500720 serial_out(p, UART_DLL, 0);
721 serial_out(p, UART_DLM, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500723 id = serial_in(p, UART_DLL) | serial_in(p, UART_DLM) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500725 serial_out(p, UART_DLL, old_dll);
726 serial_out(p, UART_DLM, old_dlm);
727 serial_out(p, UART_LCR, old_lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 return id;
730}
731
732/*
733 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
734 * When this function is called we know it is at least a StarTech
735 * 16650 V2, but it might be one of several StarTech UARTs, or one of
736 * its clones. (We treat the broken original StarTech 16650 V1 as a
737 * 16550, and why not? Startech doesn't seem to even acknowledge its
738 * existence.)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700739 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * What evil have men's minds wrought...
741 */
742static void autoconfig_has_efr(struct uart_8250_port *up)
743{
744 unsigned int id1, id2, id3, rev;
745
746 /*
747 * Everything with an EFR has SLEEP
748 */
749 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
750
751 /*
752 * First we check to see if it's an Oxford Semiconductor UART.
753 *
754 * If we have to do this here because some non-National
755 * Semiconductor clone chips lock up if you try writing to the
756 * LSR register (which serial_icr_read does)
757 */
758
759 /*
760 * Check for Oxford Semiconductor 16C950.
761 *
762 * EFR [4] must be set else this test fails.
763 *
764 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
765 * claims that it's needed for 952 dual UART's (which are not
766 * recommended for new designs).
767 */
768 up->acr = 0;
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800769 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 serial_out(up, UART_EFR, UART_EFR_ECB);
771 serial_out(up, UART_LCR, 0x00);
772 id1 = serial_icr_read(up, UART_ID1);
773 id2 = serial_icr_read(up, UART_ID2);
774 id3 = serial_icr_read(up, UART_ID3);
775 rev = serial_icr_read(up, UART_REV);
776
777 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
778
779 if (id1 == 0x16 && id2 == 0xC9 &&
780 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
781 up->port.type = PORT_16C950;
Russell King4ba5e352005-06-23 10:43:04 +0100782
783 /*
784 * Enable work around for the Oxford Semiconductor 952 rev B
785 * chip which causes it to seriously miscalculate baud rates
786 * when DLL is 0.
787 */
788 if (id3 == 0x52 && rev == 0x01)
789 up->bugs |= UART_BUG_QUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return;
791 }
Thomas Koellerbd71c182007-05-06 14:48:47 -0700792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /*
794 * We check for a XR16C850 by setting DLL and DLM to 0, and then
795 * reading back DLL and DLM. The chip type depends on the DLM
796 * value read back:
797 * 0x10 - XR16C850 and the DLL contains the chip revision.
798 * 0x12 - XR16C2850.
799 * 0x14 - XR16C854.
800 */
801 id1 = autoconfig_read_divisor_id(up);
802 DEBUG_AUTOCONF("850id=%04x ", id1);
803
804 id2 = id1 >> 8;
805 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 up->port.type = PORT_16850;
807 return;
808 }
809
810 /*
811 * It wasn't an XR16C850.
812 *
813 * We distinguish between the '654 and the '650 by counting
814 * how many bytes are in the FIFO. I'm using this for now,
815 * since that's the technique that was sent to me in the
816 * serial driver update, but I'm not convinced this works.
817 * I've had problems doing this in the past. -TYT
818 */
819 if (size_fifo(up) == 64)
820 up->port.type = PORT_16654;
821 else
822 up->port.type = PORT_16650V2;
823}
824
825/*
826 * We detected a chip without a FIFO. Only two fall into
827 * this category - the original 8250 and the 16450. The
828 * 16450 has a scratch register (accessible with LCR=0)
829 */
830static void autoconfig_8250(struct uart_8250_port *up)
831{
832 unsigned char scratch, status1, status2;
833
834 up->port.type = PORT_8250;
835
836 scratch = serial_in(up, UART_SCR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500837 serial_out(up, UART_SCR, 0xa5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 status1 = serial_in(up, UART_SCR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500839 serial_out(up, UART_SCR, 0x5a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 status2 = serial_in(up, UART_SCR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500841 serial_out(up, UART_SCR, scratch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 if (status1 == 0xa5 && status2 == 0x5a)
844 up->port.type = PORT_16450;
845}
846
847static int broken_efr(struct uart_8250_port *up)
848{
849 /*
850 * Exar ST16C2550 "A2" devices incorrectly detect as
851 * having an EFR, and report an ID of 0x0201. See
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200852 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 */
854 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
855 return 1;
856
857 return 0;
858}
859
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800860static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
861{
862 unsigned char status;
863
864 status = serial_in(up, 0x04); /* EXCR2 */
865#define PRESL(x) ((x) & 0x30)
866 if (PRESL(status) == 0x10) {
867 /* already in high speed mode */
868 return 0;
869 } else {
870 status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
871 status |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500872 serial_out(up, 0x04, status);
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800873 }
874 return 1;
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877/*
878 * We know that the chip has FIFOs. Does it have an EFR? The
879 * EFR is located in the same register position as the IIR and
880 * we know the top two bits of the IIR are currently set. The
881 * EFR should contain zero. Try to read the EFR.
882 */
883static void autoconfig_16550a(struct uart_8250_port *up)
884{
885 unsigned char status1, status2;
886 unsigned int iersave;
887
888 up->port.type = PORT_16550A;
889 up->capabilities |= UART_CAP_FIFO;
890
891 /*
892 * Check for presence of the EFR when DLAB is set.
893 * Only ST16C650V1 UARTs pass this test.
894 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500895 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (serial_in(up, UART_EFR) == 0) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500897 serial_out(up, UART_EFR, 0xA8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (serial_in(up, UART_EFR) != 0) {
899 DEBUG_AUTOCONF("EFRv1 ");
900 up->port.type = PORT_16650;
901 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
902 } else {
903 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
904 }
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500905 serial_out(up, UART_EFR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return;
907 }
908
909 /*
910 * Maybe it requires 0xbf to be written to the LCR.
911 * (other ST16C650V2 UARTs, TI16C752A, etc)
912 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500913 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
915 DEBUG_AUTOCONF("EFRv2 ");
916 autoconfig_has_efr(up);
917 return;
918 }
919
920 /*
921 * Check for a National Semiconductor SuperIO chip.
922 * Attempt to switch to bank 2, read the value of the LOOP bit
923 * from EXCR1. Switch back to bank 0, change it in MCR. Then
924 * switch back to bank 2, read it from EXCR1 again and check
925 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500927 serial_out(up, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 status1 = serial_in(up, UART_MCR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500929 serial_out(up, UART_LCR, 0xE0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 status2 = serial_in(up, 0x02); /* EXCR1 */
931
932 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500933 serial_out(up, UART_LCR, 0);
934 serial_out(up, UART_MCR, status1 ^ UART_MCR_LOOP);
935 serial_out(up, UART_LCR, 0xE0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 status2 = serial_in(up, 0x02); /* EXCR1 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500937 serial_out(up, UART_LCR, 0);
938 serial_out(up, UART_MCR, status1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if ((status2 ^ status1) & UART_MCR_LOOP) {
David Woodhouse857dde22005-05-21 15:52:23 +0100941 unsigned short quot;
942
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500943 serial_out(up, UART_LCR, 0xE0);
David Woodhouse857dde22005-05-21 15:52:23 +0100944
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100945 quot = serial_dl_read(up);
David Woodhouse857dde22005-05-21 15:52:23 +0100946 quot <<= 3;
947
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800948 if (ns16550a_goto_highspeed(up))
949 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500951 serial_out(up, UART_LCR, 0);
David Woodhouse857dde22005-05-21 15:52:23 +0100952
953 up->port.uartclk = 921600*16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 up->port.type = PORT_NS16550A;
955 up->capabilities |= UART_NATSEMI;
956 return;
957 }
958 }
959
960 /*
961 * No EFR. Try to detect a TI16750, which only sets bit 5 of
962 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
963 * Try setting it with and without DLAB set. Cheap clones
964 * set bit 5 without DLAB set.
965 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500966 serial_out(up, UART_LCR, 0);
967 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 status1 = serial_in(up, UART_IIR) >> 5;
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500969 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
970 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
971 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 status2 = serial_in(up, UART_IIR) >> 5;
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500973 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
974 serial_out(up, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
977
978 if (status1 == 6 && status2 == 7) {
979 up->port.type = PORT_16750;
980 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
981 return;
982 }
983
984 /*
985 * Try writing and reading the UART_IER_UUE bit (b6).
986 * If it works, this is probably one of the Xscale platform's
987 * internal UARTs.
988 * We're going to explicitly set the UUE bit to 0 before
989 * trying to write and read a 1 just to make sure it's not
990 * already a 1 and maybe locked there before we even start start.
991 */
992 iersave = serial_in(up, UART_IER);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500993 serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
995 /*
996 * OK it's in a known zero state, try writing and reading
997 * without disturbing the current state of the other bits.
998 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500999 serial_out(up, UART_IER, iersave | UART_IER_UUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1001 /*
1002 * It's an Xscale.
1003 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1004 */
1005 DEBUG_AUTOCONF("Xscale ");
1006 up->port.type = PORT_XSCALE;
Stephen Warren55681812011-06-17 09:45:07 -06001007 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return;
1009 }
1010 } else {
1011 /*
1012 * If we got here we couldn't force the IER_UUE bit to 0.
1013 * Log it and continue.
1014 */
1015 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1016 }
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001017 serial_out(up, UART_IER, iersave);
Philippe Langlais235dae52010-07-29 17:13:57 +02001018
1019 /*
Søren Holm06315342011-09-02 22:55:37 +02001020 * Exar uarts have EFR in a weird location
1021 */
1022 if (up->port.flags & UPF_EXAR_EFR) {
1023 up->port.type = PORT_XR17D15X;
1024 up->capabilities |= UART_CAP_AFE | UART_CAP_EFR;
1025 }
1026
1027 /*
Philippe Langlais235dae52010-07-29 17:13:57 +02001028 * We distinguish between 16550A and U6 16550A by counting
1029 * how many bytes are in the FIFO.
1030 */
1031 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1032 up->port.type = PORT_U6_16550A;
1033 up->capabilities |= UART_CAP_AFE;
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037/*
1038 * This routine is called by rs_init() to initialize a specific serial
1039 * port. It determines what type of UART chip this serial port is
1040 * using: 8250, 16450, 16550, 16550A. The important question is
1041 * whether or not this UART is a 16550A or not, since this will
1042 * determine whether or not we can use its FIFO features or not.
1043 */
1044static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
1045{
1046 unsigned char status1, scratch, scratch2, scratch3;
1047 unsigned char save_lcr, save_mcr;
1048 unsigned long flags;
1049
1050 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
1051 return;
1052
Lennert Buytenhek80647b92009-11-11 14:26:41 -08001053 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
David S. Miller84408382008-10-13 10:45:26 +01001054 serial_index(&up->port), up->port.iobase, up->port.membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 /*
1057 * We really do need global IRQs disabled here - we're going to
1058 * be frobbing the chips IRQ enable register to see if it exists.
1059 */
1060 spin_lock_irqsave(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 up->capabilities = 0;
Russell King4ba5e352005-06-23 10:43:04 +01001063 up->bugs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 if (!(up->port.flags & UPF_BUGGY_UART)) {
1066 /*
1067 * Do a simple existence test first; if we fail this,
1068 * there's no point trying anything else.
Thomas Koellerbd71c182007-05-06 14:48:47 -07001069 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 * 0x80 is used as a nonsense port to prevent against
1071 * false positives due to ISA bus float. The
1072 * assumption is that 0x80 is a non-existent port;
1073 * which should be safe since include/asm/io.h also
1074 * makes this assumption.
1075 *
1076 * Note: this is safe as long as MCR bit 4 is clear
1077 * and the device is in "PC" mode.
1078 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001079 scratch = serial_in(up, UART_IER);
1080 serial_out(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081#ifdef __i386__
1082 outb(0xff, 0x080);
1083#endif
Thomas Hoehn48212002007-02-10 01:46:05 -08001084 /*
1085 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1086 * 16C754B) allow only to modify them if an EFR bit is set.
1087 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001088 scratch2 = serial_in(up, UART_IER) & 0x0f;
1089 serial_out(up, UART_IER, 0x0F);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090#ifdef __i386__
1091 outb(0, 0x080);
1092#endif
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001093 scratch3 = serial_in(up, UART_IER) & 0x0f;
1094 serial_out(up, UART_IER, scratch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (scratch2 != 0 || scratch3 != 0x0F) {
1096 /*
1097 * We failed; there's nothing here
1098 */
1099 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1100 scratch2, scratch3);
1101 goto out;
1102 }
1103 }
1104
1105 save_mcr = serial_in(up, UART_MCR);
1106 save_lcr = serial_in(up, UART_LCR);
1107
Thomas Koellerbd71c182007-05-06 14:48:47 -07001108 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 * Check to see if a UART is really there. Certain broken
1110 * internal modems based on the Rockwell chipset fail this
1111 * test, because they apparently don't implement the loopback
1112 * test mode. So this test is skipped on the COM 1 through
1113 * COM 4 ports. This *should* be safe, since no board
1114 * manufacturer would be stupid enough to design a board
1115 * that conflicts with COM 1-4 --- we hope!
1116 */
1117 if (!(up->port.flags & UPF_SKIP_TEST)) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001118 serial_out(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1119 status1 = serial_in(up, UART_MSR) & 0xF0;
1120 serial_out(up, UART_MCR, save_mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (status1 != 0x90) {
1122 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1123 status1);
1124 goto out;
1125 }
1126 }
1127
1128 /*
1129 * We're pretty sure there's a port here. Lets find out what
1130 * type of port it is. The IIR top two bits allows us to find
Russell King6f0d6182005-09-09 16:17:58 +01001131 * out if it's 8250 or 16450, 16550, 16550A or later. This
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 * determines what we test for next.
1133 *
1134 * We also initialise the EFR (if any) to zero for later. The
1135 * EFR occupies the same register location as the FCR and IIR.
1136 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001137 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1138 serial_out(up, UART_EFR, 0);
1139 serial_out(up, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001141 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 scratch = serial_in(up, UART_IIR) >> 6;
1143
1144 DEBUG_AUTOCONF("iir=%d ", scratch);
1145
1146 switch (scratch) {
1147 case 0:
1148 autoconfig_8250(up);
1149 break;
1150 case 1:
1151 up->port.type = PORT_UNKNOWN;
1152 break;
1153 case 2:
1154 up->port.type = PORT_16550;
1155 break;
1156 case 3:
1157 autoconfig_16550a(up);
1158 break;
1159 }
1160
1161#ifdef CONFIG_SERIAL_8250_RSA
1162 /*
1163 * Only probe for RSA ports if we got the region.
1164 */
1165 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1166 int i;
1167
1168 for (i = 0 ; i < probe_rsa_count; ++i) {
1169 if (probe_rsa[i] == up->port.iobase &&
1170 __enable_rsa(up)) {
1171 up->port.type = PORT_RSA;
1172 break;
1173 }
1174 }
1175 }
1176#endif
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001177
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001178 serial_out(up, UART_LCR, save_lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 if (up->capabilities != uart_config[up->port.type].flags) {
1181 printk(KERN_WARNING
1182 "ttyS%d: detected caps %08x should be %08x\n",
David S. Miller84408382008-10-13 10:45:26 +01001183 serial_index(&up->port), up->capabilities,
1184 uart_config[up->port.type].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186
1187 up->port.fifosize = uart_config[up->port.type].fifo_size;
1188 up->capabilities = uart_config[up->port.type].flags;
1189 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1190
1191 if (up->port.type == PORT_UNKNOWN)
1192 goto out;
1193
1194 /*
1195 * Reset the UART.
1196 */
1197#ifdef CONFIG_SERIAL_8250_RSA
1198 if (up->port.type == PORT_RSA)
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001199 serial_out(up, UART_RSA_FRR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200#endif
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001201 serial_out(up, UART_MCR, save_mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 serial8250_clear_fifos(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001203 serial_in(up, UART_RX);
Lennert Buytenhek5c8c7552005-11-12 21:58:05 +00001204 if (up->capabilities & UART_CAP_UUE)
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001205 serial_out(up, UART_IER, UART_IER_UUE);
Lennert Buytenhek5c8c7552005-11-12 21:58:05 +00001206 else
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001207 serial_out(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Thomas Koellerbd71c182007-05-06 14:48:47 -07001209 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 spin_unlock_irqrestore(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1212}
1213
1214static void autoconfig_irq(struct uart_8250_port *up)
1215{
1216 unsigned char save_mcr, save_ier;
1217 unsigned char save_ICP = 0;
1218 unsigned int ICP = 0;
1219 unsigned long irqs;
1220 int irq;
1221
1222 if (up->port.flags & UPF_FOURPORT) {
1223 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1224 save_ICP = inb_p(ICP);
1225 outb_p(0x80, ICP);
1226 (void) inb_p(ICP);
1227 }
1228
1229 /* forget possible initially masked and pending IRQ */
1230 probe_irq_off(probe_irq_on());
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001231 save_mcr = serial_in(up, UART_MCR);
1232 save_ier = serial_in(up, UART_IER);
1233 serial_out(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
Thomas Koellerbd71c182007-05-06 14:48:47 -07001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 irqs = probe_irq_on();
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001236 serial_out(up, UART_MCR, 0);
Alan Cox6f803cd2008-02-08 04:18:52 -08001237 udelay(10);
1238 if (up->port.flags & UPF_FOURPORT) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001239 serial_out(up, UART_MCR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 UART_MCR_DTR | UART_MCR_RTS);
1241 } else {
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001242 serial_out(up, UART_MCR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1244 }
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001245 serial_out(up, UART_IER, 0x0f); /* enable all intrs */
1246 (void)serial_in(up, UART_LSR);
1247 (void)serial_in(up, UART_RX);
1248 (void)serial_in(up, UART_IIR);
1249 (void)serial_in(up, UART_MSR);
1250 serial_out(up, UART_TX, 0xFF);
Alan Cox6f803cd2008-02-08 04:18:52 -08001251 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 irq = probe_irq_off(irqs);
1253
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001254 serial_out(up, UART_MCR, save_mcr);
1255 serial_out(up, UART_IER, save_ier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 if (up->port.flags & UPF_FOURPORT)
1258 outb_p(save_ICP, ICP);
1259
1260 up->port.irq = (irq > 0) ? irq : 0;
1261}
1262
Russell Kinge763b902005-06-29 18:41:51 +01001263static inline void __stop_tx(struct uart_8250_port *p)
1264{
1265 if (p->ier & UART_IER_THRI) {
1266 p->ier &= ~UART_IER_THRI;
1267 serial_out(p, UART_IER, p->ier);
1268 }
1269}
1270
Russell Kingb129a8c2005-08-31 10:12:14 +01001271static void serial8250_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Jamie Iles49d57412010-12-01 23:39:35 +00001273 struct uart_8250_port *up =
1274 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Russell Kinge763b902005-06-29 18:41:51 +01001276 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 /*
Russell Kinge763b902005-06-29 18:41:51 +01001279 * We really want to stop the transmitter from sending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 */
Russell Kinge763b902005-06-29 18:41:51 +01001281 if (up->port.type == PORT_16C950) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 up->acr |= UART_ACR_TXDIS;
1283 serial_icr_write(up, UART_ACR, up->acr);
1284 }
1285}
1286
Russell Kingb129a8c2005-08-31 10:12:14 +01001287static void serial8250_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Jamie Iles49d57412010-12-01 23:39:35 +00001289 struct uart_8250_port *up =
1290 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 if (!(up->ier & UART_IER_THRI)) {
1293 up->ier |= UART_IER_THRI;
1294 serial_out(up, UART_IER, up->ier);
Russell King55d3b282005-06-23 15:05:41 +01001295
Russell King67f76542005-06-23 22:26:43 +01001296 if (up->bugs & UART_BUG_TXEN) {
Ian Jackson68cb4f82009-11-18 11:08:11 +01001297 unsigned char lsr;
Russell King55d3b282005-06-23 15:05:41 +01001298 lsr = serial_in(up, UART_LSR);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001299 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Thomas Koellerbd71c182007-05-06 14:48:47 -07001300 if ((up->port.type == PORT_RM9000) ?
Ian Jackson68cb4f82009-11-18 11:08:11 +01001301 (lsr & UART_LSR_THRE) :
1302 (lsr & UART_LSR_TEMT))
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001303 serial8250_tx_chars(up);
Russell King55d3b282005-06-23 15:05:41 +01001304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
Russell Kinge763b902005-06-29 18:41:51 +01001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 /*
Russell Kinge763b902005-06-29 18:41:51 +01001308 * Re-enable the transmitter if we disabled it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 */
Russell Kinge763b902005-06-29 18:41:51 +01001310 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 up->acr &= ~UART_ACR_TXDIS;
1312 serial_icr_write(up, UART_ACR, up->acr);
1313 }
1314}
1315
1316static void serial8250_stop_rx(struct uart_port *port)
1317{
Jamie Iles49d57412010-12-01 23:39:35 +00001318 struct uart_8250_port *up =
1319 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 up->ier &= ~UART_IER_RLSI;
1322 up->port.read_status_mask &= ~UART_LSR_DR;
1323 serial_out(up, UART_IER, up->ier);
1324}
1325
1326static void serial8250_enable_ms(struct uart_port *port)
1327{
Jamie Iles49d57412010-12-01 23:39:35 +00001328 struct uart_8250_port *up =
1329 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001331 /* no MSR capabilities */
1332 if (up->bugs & UART_BUG_NOMSR)
1333 return;
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 up->ier |= UART_IER_MSI;
1336 serial_out(up, UART_IER, up->ier);
1337}
1338
Stephen Warren5f873ba2011-05-17 16:12:37 -06001339/*
1340 * Clear the Tegra rx fifo after a break
1341 *
1342 * FIXME: This needs to become a port specific callback once we have a
1343 * framework for this
1344 */
1345static void clear_rx_fifo(struct uart_8250_port *up)
1346{
1347 unsigned int status, tmout = 10000;
1348 do {
1349 status = serial_in(up, UART_LSR);
1350 if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
1351 status = serial_in(up, UART_RX);
1352 else
1353 break;
1354 if (--tmout == 0)
1355 break;
1356 udelay(1);
1357 } while (1);
1358}
1359
Paul Gortmaker0690f412011-12-04 18:42:19 -05001360/*
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001361 * serial8250_rx_chars: processes according to the passed in LSR
Paul Gortmaker0690f412011-12-04 18:42:19 -05001362 * value, and returns the remaining LSR bits not handled
1363 * by this Rx routine.
1364 */
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001365unsigned char
1366serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001368 struct tty_struct *tty = up->port.state->port.tty;
Paul Gortmaker0690f412011-12-04 18:42:19 -05001369 unsigned char ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 int max_count = 256;
1371 char flag;
1372
1373 do {
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001374 if (likely(lsr & UART_LSR_DR))
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001375 ch = serial_in(up, UART_RX);
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001376 else
1377 /*
1378 * Intel 82571 has a Serial Over Lan device that will
1379 * set UART_LSR_BI without setting UART_LSR_DR when
1380 * it receives a break. To avoid reading from the
1381 * receive buffer without UART_LSR_DR bit set, we
1382 * just force the read character to be 0
1383 */
1384 ch = 0;
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 flag = TTY_NORMAL;
1387 up->port.icount.rx++;
1388
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001389 lsr |= up->lsr_saved_flags;
1390 up->lsr_saved_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001392 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 /*
1394 * For statistics only
1395 */
1396 if (lsr & UART_LSR_BI) {
1397 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1398 up->port.icount.brk++;
1399 /*
Stephen Warren5f873ba2011-05-17 16:12:37 -06001400 * If tegra port then clear the rx fifo to
1401 * accept another break/character.
1402 */
1403 if (up->port.type == PORT_TEGRA)
1404 clear_rx_fifo(up);
1405
1406 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 * We do the SysRQ and SAK checking
1408 * here because otherwise the break
1409 * may get masked by ignore_status_mask
1410 * or read_status_mask.
1411 */
1412 if (uart_handle_break(&up->port))
1413 goto ignore_char;
1414 } else if (lsr & UART_LSR_PE)
1415 up->port.icount.parity++;
1416 else if (lsr & UART_LSR_FE)
1417 up->port.icount.frame++;
1418 if (lsr & UART_LSR_OE)
1419 up->port.icount.overrun++;
1420
1421 /*
Russell King23907eb2005-04-16 15:26:39 -07001422 * Mask off conditions which should be ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 */
1424 lsr &= up->port.read_status_mask;
1425
1426 if (lsr & UART_LSR_BI) {
1427 DEBUG_INTR("handling break....");
1428 flag = TTY_BREAK;
1429 } else if (lsr & UART_LSR_PE)
1430 flag = TTY_PARITY;
1431 else if (lsr & UART_LSR_FE)
1432 flag = TTY_FRAME;
1433 }
David Howells7d12e782006-10-05 14:55:46 +01001434 if (uart_handle_sysrq_char(&up->port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 goto ignore_char;
Russell King05ab3012005-05-09 23:21:59 +01001436
1437 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1438
Alan Cox6f803cd2008-02-08 04:18:52 -08001439ignore_char:
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001440 lsr = serial_in(up, UART_LSR);
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001441 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 spin_unlock(&up->port.lock);
1443 tty_flip_buffer_push(tty);
1444 spin_lock(&up->port.lock);
Paul Gortmaker0690f412011-12-04 18:42:19 -05001445 return lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001447EXPORT_SYMBOL_GPL(serial8250_rx_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001449void serial8250_tx_chars(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001451 struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 int count;
1453
1454 if (up->port.x_char) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001455 serial_out(up, UART_TX, up->port.x_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 up->port.icount.tx++;
1457 up->port.x_char = 0;
1458 return;
1459 }
Russell Kingb129a8c2005-08-31 10:12:14 +01001460 if (uart_tx_stopped(&up->port)) {
1461 serial8250_stop_tx(&up->port);
1462 return;
1463 }
1464 if (uart_circ_empty(xmit)) {
Russell Kinge763b902005-06-29 18:41:51 +01001465 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return;
1467 }
1468
1469 count = up->tx_loadsz;
1470 do {
1471 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1472 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1473 up->port.icount.tx++;
1474 if (uart_circ_empty(xmit))
1475 break;
1476 } while (--count > 0);
1477
1478 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1479 uart_write_wakeup(&up->port);
1480
1481 DEBUG_INTR("THRE...");
1482
1483 if (uart_circ_empty(xmit))
Russell Kinge763b902005-06-29 18:41:51 +01001484 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001486EXPORT_SYMBOL_GPL(serial8250_tx_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001488unsigned int serial8250_modem_status(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Russell King2af7cd62006-01-04 16:55:09 +00001490 unsigned int status = serial_in(up, UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001492 status |= up->msr_saved_flags;
1493 up->msr_saved_flags = 0;
Taku Izumifdc30b32007-04-23 14:41:00 -07001494 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
Alan Coxebd2c8f2009-09-19 13:13:28 -07001495 up->port.state != NULL) {
Russell King2af7cd62006-01-04 16:55:09 +00001496 if (status & UART_MSR_TERI)
1497 up->port.icount.rng++;
1498 if (status & UART_MSR_DDSR)
1499 up->port.icount.dsr++;
1500 if (status & UART_MSR_DDCD)
1501 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1502 if (status & UART_MSR_DCTS)
1503 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Alan Coxbdc04e32009-09-19 13:13:31 -07001505 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
Russell King2af7cd62006-01-04 16:55:09 +00001506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Russell King2af7cd62006-01-04 16:55:09 +00001508 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001510EXPORT_SYMBOL_GPL(serial8250_modem_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512/*
1513 * This handles the interrupt from one port.
1514 */
Paul Gortmaker86b21192011-12-04 18:42:22 -05001515int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Paul Gortmaker0690f412011-12-04 18:42:19 -05001517 unsigned char status;
Jiri Kosina4bf36312007-04-23 14:41:21 -07001518 unsigned long flags;
Paul Gortmaker86b21192011-12-04 18:42:22 -05001519 struct uart_8250_port *up =
1520 container_of(port, struct uart_8250_port, port);
1521
1522 if (iir & UART_IIR_NO_INT)
1523 return 0;
Russell King45e24602006-01-04 19:19:06 +00001524
Jiri Kosina4bf36312007-04-23 14:41:21 -07001525 spin_lock_irqsave(&up->port.lock, flags);
Russell King45e24602006-01-04 19:19:06 +00001526
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001527 status = serial_in(up, UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 DEBUG_INTR("status = %x...", status);
1530
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001531 if (status & (UART_LSR_DR | UART_LSR_BI))
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001532 status = serial8250_rx_chars(up, status);
1533 serial8250_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (status & UART_LSR_THRE)
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001535 serial8250_tx_chars(up);
Russell King45e24602006-01-04 19:19:06 +00001536
Jiri Kosina4bf36312007-04-23 14:41:21 -07001537 spin_unlock_irqrestore(&up->port.lock, flags);
Paul Gortmaker86b21192011-12-04 18:42:22 -05001538 return 1;
Jamie Iles583d28e2011-08-15 10:17:52 +01001539}
Jamie Ilesc7a1bdc2011-08-26 19:04:49 +01001540EXPORT_SYMBOL_GPL(serial8250_handle_irq);
Jamie Iles583d28e2011-08-15 10:17:52 +01001541
1542static int serial8250_default_handle_irq(struct uart_port *port)
1543{
1544 struct uart_8250_port *up =
1545 container_of(port, struct uart_8250_port, port);
1546 unsigned int iir = serial_in(up, UART_IIR);
1547
1548 return serial8250_handle_irq(port, iir);
1549}
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551/*
1552 * This is the serial driver's interrupt routine.
1553 *
1554 * Arjan thinks the old way was overly complex, so it got simplified.
1555 * Alan disagrees, saying that need the complexity to handle the weird
1556 * nature of ISA shared interrupts. (This is a special exception.)
1557 *
1558 * In order to handle ISA shared interrupts properly, we need to check
1559 * that all ports have been serviced, and therefore the ISA interrupt
1560 * line has been de-asserted.
1561 *
1562 * This means we need to loop through all ports. checking that they
1563 * don't have an interrupt pending.
1564 */
David Howells7d12e782006-10-05 14:55:46 +01001565static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
1567 struct irq_info *i = dev_id;
1568 struct list_head *l, *end = NULL;
1569 int pass_counter = 0, handled = 0;
1570
1571 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1572
1573 spin_lock(&i->lock);
1574
1575 l = i->head;
1576 do {
1577 struct uart_8250_port *up;
Jamie Iles583d28e2011-08-15 10:17:52 +01001578 struct uart_port *port;
Dan Williams448ac152011-11-22 13:41:24 -08001579 bool skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 up = list_entry(l, struct uart_8250_port, list);
Jamie Iles583d28e2011-08-15 10:17:52 +01001582 port = &up->port;
Dan Williams448ac152011-11-22 13:41:24 -08001583 skip = pass_counter && up->port.flags & UPF_IIR_ONCE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Dan Williams448ac152011-11-22 13:41:24 -08001585 if (!skip && port->handle_irq(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 handled = 1;
Marc St-Jeanbeab6972007-05-06 14:48:45 -07001587 end = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 } else if (end == NULL)
1589 end = l;
1590
1591 l = l->next;
1592
1593 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1594 /* If we hit this, we're dead. */
Daniel Drakecd3ecad2010-10-20 16:00:48 -07001595 printk_ratelimited(KERN_ERR
1596 "serial8250: too much work for irq%d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 break;
1598 }
1599 } while (l != end);
1600
1601 spin_unlock(&i->lock);
1602
1603 DEBUG_INTR("end.\n");
1604
1605 return IRQ_RETVAL(handled);
1606}
1607
1608/*
1609 * To support ISA shared interrupts, we need to have one interrupt
1610 * handler that ensures that the IRQ line has been deasserted
1611 * before returning. Failing to do this will result in the IRQ
1612 * line being stuck active, and, since ISA irqs are edge triggered,
1613 * no more IRQs will be seen.
1614 */
1615static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1616{
1617 spin_lock_irq(&i->lock);
1618
1619 if (!list_empty(i->head)) {
1620 if (i->head == &up->list)
1621 i->head = i->head->next;
1622 list_del(&up->list);
1623 } else {
1624 BUG_ON(i->head != &up->list);
1625 i->head = NULL;
1626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 spin_unlock_irq(&i->lock);
Alan Cox25db8ad2008-08-19 20:49:40 -07001628 /* List empty so throw away the hash node */
1629 if (i->head == NULL) {
1630 hlist_del(&i->node);
1631 kfree(i);
1632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
1635static int serial_link_irq_chain(struct uart_8250_port *up)
1636{
Alan Cox25db8ad2008-08-19 20:49:40 -07001637 struct hlist_head *h;
1638 struct hlist_node *n;
1639 struct irq_info *i;
Thomas Gleixner40663cc2006-07-01 19:29:43 -07001640 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Alan Cox25db8ad2008-08-19 20:49:40 -07001642 mutex_lock(&hash_mutex);
1643
1644 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1645
1646 hlist_for_each(n, h) {
1647 i = hlist_entry(n, struct irq_info, node);
1648 if (i->irq == up->port.irq)
1649 break;
1650 }
1651
1652 if (n == NULL) {
1653 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
1654 if (i == NULL) {
1655 mutex_unlock(&hash_mutex);
1656 return -ENOMEM;
1657 }
1658 spin_lock_init(&i->lock);
1659 i->irq = up->port.irq;
1660 hlist_add_head(&i->node, h);
1661 }
1662 mutex_unlock(&hash_mutex);
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 spin_lock_irq(&i->lock);
1665
1666 if (i->head) {
1667 list_add(&up->list, i->head);
1668 spin_unlock_irq(&i->lock);
1669
1670 ret = 0;
1671 } else {
1672 INIT_LIST_HEAD(&up->list);
1673 i->head = &up->list;
1674 spin_unlock_irq(&i->lock);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07001675 irq_flags |= up->port.irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 ret = request_irq(up->port.irq, serial8250_interrupt,
1677 irq_flags, "serial", i);
1678 if (ret < 0)
1679 serial_do_unlink(i, up);
1680 }
1681
1682 return ret;
1683}
1684
1685static void serial_unlink_irq_chain(struct uart_8250_port *up)
1686{
Alan Cox25db8ad2008-08-19 20:49:40 -07001687 struct irq_info *i;
1688 struct hlist_node *n;
1689 struct hlist_head *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Alan Cox25db8ad2008-08-19 20:49:40 -07001691 mutex_lock(&hash_mutex);
1692
1693 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1694
1695 hlist_for_each(n, h) {
1696 i = hlist_entry(n, struct irq_info, node);
1697 if (i->irq == up->port.irq)
1698 break;
1699 }
1700
1701 BUG_ON(n == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 BUG_ON(i->head == NULL);
1703
1704 if (list_empty(i->head))
1705 free_irq(up->port.irq, i);
1706
1707 serial_do_unlink(i, up);
Alan Cox25db8ad2008-08-19 20:49:40 -07001708 mutex_unlock(&hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
1711/*
1712 * This function is used to handle ports that do not have an
1713 * interrupt. This doesn't work very well for 16450's, but gives
1714 * barely passable results for a 16550A. (Although at the expense
1715 * of much CPU overhead).
1716 */
1717static void serial8250_timeout(unsigned long data)
1718{
1719 struct uart_8250_port *up = (struct uart_8250_port *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Paul Gortmakera0431472011-12-04 18:42:21 -05001721 up->port.handle_irq(&up->port);
Anton Vorontsov54381062010-10-01 17:21:25 +04001722 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
Alex Williamson40b36da2007-02-14 00:33:04 -08001723}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Alex Williamson40b36da2007-02-14 00:33:04 -08001725static void serial8250_backup_timeout(unsigned long data)
1726{
1727 struct uart_8250_port *up = (struct uart_8250_port *)data;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001728 unsigned int iir, ier = 0, lsr;
1729 unsigned long flags;
Alex Williamson40b36da2007-02-14 00:33:04 -08001730
Al Cooperdbb3b1c2011-07-25 16:19:52 -04001731 spin_lock_irqsave(&up->port.lock, flags);
1732
Alex Williamson40b36da2007-02-14 00:33:04 -08001733 /*
1734 * Must disable interrupts or else we risk racing with the interrupt
1735 * based handler.
1736 */
Alan Coxd4e33fa2012-01-26 17:44:09 +00001737 if (up->port.irq) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001738 ier = serial_in(up, UART_IER);
1739 serial_out(up, UART_IER, 0);
1740 }
1741
1742 iir = serial_in(up, UART_IIR);
1743
1744 /*
1745 * This should be a safe test for anyone who doesn't trust the
1746 * IIR bits on their UART, but it's specifically designed for
1747 * the "Diva" UART used on the management processor on many HP
1748 * ia64 and parisc boxes.
1749 */
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001750 lsr = serial_in(up, UART_LSR);
1751 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -08001752 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
Alan Coxebd2c8f2009-09-19 13:13:28 -07001753 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001754 (lsr & UART_LSR_THRE)) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001755 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1756 iir |= UART_IIR_THRI;
1757 }
1758
1759 if (!(iir & UART_IIR_NO_INT))
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001760 serial8250_tx_chars(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001761
Alan Coxd4e33fa2012-01-26 17:44:09 +00001762 if (up->port.irq)
Alex Williamson40b36da2007-02-14 00:33:04 -08001763 serial_out(up, UART_IER, ier);
1764
Al Cooperdbb3b1c2011-07-25 16:19:52 -04001765 spin_unlock_irqrestore(&up->port.lock, flags);
1766
Alex Williamson40b36da2007-02-14 00:33:04 -08001767 /* Standard timer interval plus 0.2s to keep the port running */
Alan Cox6f803cd2008-02-08 04:18:52 -08001768 mod_timer(&up->timer,
Anton Vorontsov54381062010-10-01 17:21:25 +04001769 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
1772static unsigned int serial8250_tx_empty(struct uart_port *port)
1773{
Jamie Iles49d57412010-12-01 23:39:35 +00001774 struct uart_8250_port *up =
1775 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 unsigned long flags;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001777 unsigned int lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 spin_lock_irqsave(&up->port.lock, flags);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001780 lsr = serial_in(up, UART_LSR);
1781 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 spin_unlock_irqrestore(&up->port.lock, flags);
1783
Dick Hollenbeckbca47612009-12-09 12:31:34 -08001784 return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
1787static unsigned int serial8250_get_mctrl(struct uart_port *port)
1788{
Jamie Iles49d57412010-12-01 23:39:35 +00001789 struct uart_8250_port *up =
1790 container_of(port, struct uart_8250_port, port);
Russell King2af7cd62006-01-04 16:55:09 +00001791 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 unsigned int ret;
1793
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001794 status = serial8250_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 ret = 0;
1797 if (status & UART_MSR_DCD)
1798 ret |= TIOCM_CAR;
1799 if (status & UART_MSR_RI)
1800 ret |= TIOCM_RNG;
1801 if (status & UART_MSR_DSR)
1802 ret |= TIOCM_DSR;
1803 if (status & UART_MSR_CTS)
1804 ret |= TIOCM_CTS;
1805 return ret;
1806}
1807
1808static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1809{
Jamie Iles49d57412010-12-01 23:39:35 +00001810 struct uart_8250_port *up =
1811 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 unsigned char mcr = 0;
1813
1814 if (mctrl & TIOCM_RTS)
1815 mcr |= UART_MCR_RTS;
1816 if (mctrl & TIOCM_DTR)
1817 mcr |= UART_MCR_DTR;
1818 if (mctrl & TIOCM_OUT1)
1819 mcr |= UART_MCR_OUT1;
1820 if (mctrl & TIOCM_OUT2)
1821 mcr |= UART_MCR_OUT2;
1822 if (mctrl & TIOCM_LOOP)
1823 mcr |= UART_MCR_LOOP;
1824
1825 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1826
1827 serial_out(up, UART_MCR, mcr);
1828}
1829
1830static void serial8250_break_ctl(struct uart_port *port, int break_state)
1831{
Jamie Iles49d57412010-12-01 23:39:35 +00001832 struct uart_8250_port *up =
1833 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 unsigned long flags;
1835
1836 spin_lock_irqsave(&up->port.lock, flags);
1837 if (break_state == -1)
1838 up->lcr |= UART_LCR_SBC;
1839 else
1840 up->lcr &= ~UART_LCR_SBC;
1841 serial_out(up, UART_LCR, up->lcr);
1842 spin_unlock_irqrestore(&up->port.lock, flags);
1843}
1844
Alex Williamson40b36da2007-02-14 00:33:04 -08001845/*
1846 * Wait for transmitter & holding register to empty
1847 */
Will Newtonb5d674a2008-10-13 10:36:21 +01001848static void wait_for_xmitr(struct uart_8250_port *up, int bits)
Alex Williamson40b36da2007-02-14 00:33:04 -08001849{
1850 unsigned int status, tmout = 10000;
1851
1852 /* Wait up to 10ms for the character(s) to be sent. */
David Daney97d303b2010-10-05 11:40:07 -07001853 for (;;) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001854 status = serial_in(up, UART_LSR);
1855
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001856 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -08001857
David Daney97d303b2010-10-05 11:40:07 -07001858 if ((status & bits) == bits)
1859 break;
Alex Williamson40b36da2007-02-14 00:33:04 -08001860 if (--tmout == 0)
1861 break;
1862 udelay(1);
David Daney97d303b2010-10-05 11:40:07 -07001863 }
Alex Williamson40b36da2007-02-14 00:33:04 -08001864
1865 /* Wait up to 1s for flow control if necessary */
1866 if (up->port.flags & UPF_CONS_FLOW) {
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001867 unsigned int tmout;
1868 for (tmout = 1000000; tmout; tmout--) {
1869 unsigned int msr = serial_in(up, UART_MSR);
1870 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1871 if (msr & UART_MSR_CTS)
1872 break;
Alex Williamson40b36da2007-02-14 00:33:04 -08001873 udelay(1);
1874 touch_nmi_watchdog();
1875 }
1876 }
1877}
1878
Jason Wesself2d937f2008-04-17 20:05:37 +02001879#ifdef CONFIG_CONSOLE_POLL
1880/*
1881 * Console polling routines for writing and reading from the uart while
1882 * in an interrupt or debug context.
1883 */
1884
1885static int serial8250_get_poll_char(struct uart_port *port)
1886{
Jamie Iles49d57412010-12-01 23:39:35 +00001887 struct uart_8250_port *up =
1888 container_of(port, struct uart_8250_port, port);
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001889 unsigned char lsr = serial_in(up, UART_LSR);
Jason Wesself2d937f2008-04-17 20:05:37 +02001890
Jason Wesself5316b42010-05-20 21:04:22 -05001891 if (!(lsr & UART_LSR_DR))
1892 return NO_POLL_CHAR;
Jason Wesself2d937f2008-04-17 20:05:37 +02001893
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001894 return serial_in(up, UART_RX);
Jason Wesself2d937f2008-04-17 20:05:37 +02001895}
1896
1897
1898static void serial8250_put_poll_char(struct uart_port *port,
1899 unsigned char c)
1900{
1901 unsigned int ier;
Jamie Iles49d57412010-12-01 23:39:35 +00001902 struct uart_8250_port *up =
1903 container_of(port, struct uart_8250_port, port);
Jason Wesself2d937f2008-04-17 20:05:37 +02001904
1905 /*
1906 * First save the IER then disable the interrupts
1907 */
1908 ier = serial_in(up, UART_IER);
1909 if (up->capabilities & UART_CAP_UUE)
1910 serial_out(up, UART_IER, UART_IER_UUE);
1911 else
1912 serial_out(up, UART_IER, 0);
1913
1914 wait_for_xmitr(up, BOTH_EMPTY);
1915 /*
1916 * Send the character out.
1917 * If a LF, also do CR...
1918 */
1919 serial_out(up, UART_TX, c);
1920 if (c == 10) {
1921 wait_for_xmitr(up, BOTH_EMPTY);
1922 serial_out(up, UART_TX, 13);
1923 }
1924
1925 /*
1926 * Finally, wait for transmitter to become empty
1927 * and restore the IER
1928 */
1929 wait_for_xmitr(up, BOTH_EMPTY);
1930 serial_out(up, UART_IER, ier);
1931}
1932
1933#endif /* CONFIG_CONSOLE_POLL */
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935static int serial8250_startup(struct uart_port *port)
1936{
Jamie Iles49d57412010-12-01 23:39:35 +00001937 struct uart_8250_port *up =
1938 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 unsigned long flags;
Russell King55d3b282005-06-23 15:05:41 +01001940 unsigned char lsr, iir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 int retval;
1942
Ondrej Puzmane4f05af2010-12-04 21:17:38 +01001943 up->port.fifosize = uart_config[up->port.type].fifo_size;
1944 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 up->capabilities = uart_config[up->port.type].flags;
1946 up->mcr = 0;
1947
Alan Coxb8e7e402009-05-28 14:01:35 +01001948 if (up->port.iotype != up->cur_iotype)
1949 set_io_from_upio(port);
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (up->port.type == PORT_16C950) {
1952 /* Wake up and initialize UART */
1953 up->acr = 0;
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001954 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1955 serial_out(up, UART_EFR, UART_EFR_ECB);
1956 serial_out(up, UART_IER, 0);
1957 serial_out(up, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001959 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1960 serial_out(up, UART_EFR, UART_EFR_ECB);
1961 serial_out(up, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
1963
1964#ifdef CONFIG_SERIAL_8250_RSA
1965 /*
1966 * If this is an RSA port, see if we can kick it up to the
1967 * higher speed clock.
1968 */
1969 enable_rsa(up);
1970#endif
1971
1972 /*
1973 * Clear the FIFO buffers and disable them.
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08001974 * (they will be reenabled in set_termios())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 */
1976 serial8250_clear_fifos(up);
1977
1978 /*
1979 * Clear the interrupt registers.
1980 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001981 (void) serial_in(up, UART_LSR);
1982 (void) serial_in(up, UART_RX);
1983 (void) serial_in(up, UART_IIR);
1984 (void) serial_in(up, UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 /*
1987 * At this point, there's no way the LSR could still be 0xff;
1988 * if it is, then bail out, because there's likely no UART
1989 * here.
1990 */
1991 if (!(up->port.flags & UPF_BUGGY_UART) &&
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001992 (serial_in(up, UART_LSR) == 0xff)) {
Konrad Rzeszutek Wilk7808a4c2011-09-26 09:14:34 -04001993 printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
1994 serial_index(&up->port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return -ENODEV;
1996 }
1997
1998 /*
1999 * For a XR16C850, we need to set the trigger levels
2000 */
2001 if (up->port.type == PORT_16850) {
2002 unsigned char fctr;
2003
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002004 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002006 fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2007 serial_out(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2008 serial_out(up, UART_TRG, UART_TRG_96);
2009 serial_out(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2010 serial_out(up, UART_TRG, UART_TRG_96);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002012 serial_out(up, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
2014
Alan Coxd4e33fa2012-01-26 17:44:09 +00002015 if (up->port.irq) {
Alex Williamson01c194d2008-04-28 02:14:09 -07002016 unsigned char iir1;
Alex Williamson40b36da2007-02-14 00:33:04 -08002017 /*
2018 * Test for UARTs that do not reassert THRE when the
2019 * transmitter is idle and the interrupt has already
2020 * been cleared. Real 16550s should always reassert
2021 * this interrupt whenever the transmitter is idle and
2022 * the interrupt is enabled. Delays are necessary to
2023 * allow register changes to become visible.
2024 */
Borislav Petkovc389d272008-07-29 22:33:32 -07002025 spin_lock_irqsave(&up->port.lock, flags);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002026 if (up->port.irqflags & IRQF_SHARED)
Anton Vorontsov768aec02008-07-22 11:21:07 +01002027 disable_irq_nosync(up->port.irq);
Alex Williamson40b36da2007-02-14 00:33:04 -08002028
2029 wait_for_xmitr(up, UART_LSR_THRE);
2030 serial_out_sync(up, UART_IER, UART_IER_THRI);
2031 udelay(1); /* allow THRE to set */
Alex Williamson01c194d2008-04-28 02:14:09 -07002032 iir1 = serial_in(up, UART_IIR);
Alex Williamson40b36da2007-02-14 00:33:04 -08002033 serial_out(up, UART_IER, 0);
2034 serial_out_sync(up, UART_IER, UART_IER_THRI);
2035 udelay(1); /* allow a working UART time to re-assert THRE */
2036 iir = serial_in(up, UART_IIR);
2037 serial_out(up, UART_IER, 0);
2038
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002039 if (up->port.irqflags & IRQF_SHARED)
Anton Vorontsov768aec02008-07-22 11:21:07 +01002040 enable_irq(up->port.irq);
Borislav Petkovc389d272008-07-29 22:33:32 -07002041 spin_unlock_irqrestore(&up->port.lock, flags);
Alex Williamson40b36da2007-02-14 00:33:04 -08002042
2043 /*
2044 * If the interrupt is not reasserted, setup a timer to
2045 * kick the UART on a regular basis.
2046 */
Alex Williamson01c194d2008-04-28 02:14:09 -07002047 if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
Will Newton363f66f2008-09-02 14:35:44 -07002048 up->bugs |= UART_BUG_THRE;
David S. Miller84408382008-10-13 10:45:26 +01002049 pr_debug("ttyS%d - using backup timer\n",
2050 serial_index(port));
Alex Williamson40b36da2007-02-14 00:33:04 -08002051 }
2052 }
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 /*
Will Newton363f66f2008-09-02 14:35:44 -07002055 * The above check will only give an accurate result the first time
2056 * the port is opened so this value needs to be preserved.
2057 */
2058 if (up->bugs & UART_BUG_THRE) {
2059 up->timer.function = serial8250_backup_timeout;
2060 up->timer.data = (unsigned long)up;
2061 mod_timer(&up->timer, jiffies +
Anton Vorontsov54381062010-10-01 17:21:25 +04002062 uart_poll_timeout(port) + HZ / 5);
Will Newton363f66f2008-09-02 14:35:44 -07002063 }
2064
2065 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 * If the "interrupt" for this port doesn't correspond with any
2067 * hardware interrupt, we use a timer-based system. The original
2068 * driver used to do this with IRQ0.
2069 */
Alan Coxd4e33fa2012-01-26 17:44:09 +00002070 if (!up->port.irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 up->timer.data = (unsigned long)up;
Anton Vorontsov54381062010-10-01 17:21:25 +04002072 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 } else {
2074 retval = serial_link_irq_chain(up);
2075 if (retval)
2076 return retval;
2077 }
2078
2079 /*
2080 * Now, initialize the UART
2081 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002082 serial_out(up, UART_LCR, UART_LCR_WLEN8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 spin_lock_irqsave(&up->port.lock, flags);
2085 if (up->port.flags & UPF_FOURPORT) {
Alan Coxd4e33fa2012-01-26 17:44:09 +00002086 if (!up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 up->port.mctrl |= TIOCM_OUT1;
2088 } else
2089 /*
2090 * Most PC uarts need OUT2 raised to enable interrupts.
2091 */
Alan Coxd4e33fa2012-01-26 17:44:09 +00002092 if (up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 up->port.mctrl |= TIOCM_OUT2;
2094
2095 serial8250_set_mctrl(&up->port, up->port.mctrl);
Russell King55d3b282005-06-23 15:05:41 +01002096
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002097 /* Serial over Lan (SoL) hack:
2098 Intel 8257x Gigabit ethernet chips have a
2099 16550 emulation, to be used for Serial Over Lan.
2100 Those chips take a longer time than a normal
2101 serial device to signalize that a transmission
2102 data was queued. Due to that, the above test generally
2103 fails. One solution would be to delay the reading of
2104 iir. However, this is not reliable, since the timeout
2105 is variable. So, let's just don't test if we receive
2106 TX irq. This way, we'll never enable UART_BUG_TXEN.
2107 */
Chuck Ebbertd41a4b52009-10-01 15:44:26 -07002108 if (skip_txen_test || up->port.flags & UPF_NO_TXEN_TEST)
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002109 goto dont_test_tx_en;
2110
Russell King55d3b282005-06-23 15:05:41 +01002111 /*
2112 * Do a quick test to see if we receive an
2113 * interrupt when we enable the TX irq.
2114 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002115 serial_out(up, UART_IER, UART_IER_THRI);
Russell King55d3b282005-06-23 15:05:41 +01002116 lsr = serial_in(up, UART_LSR);
2117 iir = serial_in(up, UART_IIR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002118 serial_out(up, UART_IER, 0);
Russell King55d3b282005-06-23 15:05:41 +01002119
2120 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
Russell King67f76542005-06-23 22:26:43 +01002121 if (!(up->bugs & UART_BUG_TXEN)) {
2122 up->bugs |= UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002123 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
David S. Miller84408382008-10-13 10:45:26 +01002124 serial_index(port));
Russell King55d3b282005-06-23 15:05:41 +01002125 }
2126 } else {
Russell King67f76542005-06-23 22:26:43 +01002127 up->bugs &= ~UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002128 }
2129
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002130dont_test_tx_en:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 spin_unlock_irqrestore(&up->port.lock, flags);
2132
2133 /*
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002134 * Clear the interrupt registers again for luck, and clear the
2135 * saved flags to avoid getting false values from polling
2136 * routines or the previous session.
2137 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002138 serial_in(up, UART_LSR);
2139 serial_in(up, UART_RX);
2140 serial_in(up, UART_IIR);
2141 serial_in(up, UART_MSR);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002142 up->lsr_saved_flags = 0;
2143 up->msr_saved_flags = 0;
2144
2145 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 * Finally, enable interrupts. Note: Modem status interrupts
2147 * are set via set_termios(), which will be occurring imminently
2148 * anyway, so we don't enable them here.
2149 */
2150 up->ier = UART_IER_RLSI | UART_IER_RDI;
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002151 serial_out(up, UART_IER, up->ier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 if (up->port.flags & UPF_FOURPORT) {
2154 unsigned int icp;
2155 /*
2156 * Enable interrupts on the AST Fourport board
2157 */
2158 icp = (up->port.iobase & 0xfe0) | 0x01f;
2159 outb_p(0x80, icp);
2160 (void) inb_p(icp);
2161 }
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return 0;
2164}
2165
2166static void serial8250_shutdown(struct uart_port *port)
2167{
Jamie Iles49d57412010-12-01 23:39:35 +00002168 struct uart_8250_port *up =
2169 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 unsigned long flags;
2171
2172 /*
2173 * Disable interrupts from this port
2174 */
2175 up->ier = 0;
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002176 serial_out(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 spin_lock_irqsave(&up->port.lock, flags);
2179 if (up->port.flags & UPF_FOURPORT) {
2180 /* reset interrupts on the AST Fourport board */
2181 inb((up->port.iobase & 0xfe0) | 0x1f);
2182 up->port.mctrl |= TIOCM_OUT1;
2183 } else
2184 up->port.mctrl &= ~TIOCM_OUT2;
2185
2186 serial8250_set_mctrl(&up->port, up->port.mctrl);
2187 spin_unlock_irqrestore(&up->port.lock, flags);
2188
2189 /*
2190 * Disable break condition and FIFOs
2191 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002192 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 serial8250_clear_fifos(up);
2194
2195#ifdef CONFIG_SERIAL_8250_RSA
2196 /*
2197 * Reset the RSA board back to 115kbps compat mode.
2198 */
2199 disable_rsa(up);
2200#endif
2201
2202 /*
2203 * Read data port to reset things, and then unlink from
2204 * the IRQ chain.
2205 */
2206 (void) serial_in(up, UART_RX);
2207
Alex Williamson40b36da2007-02-14 00:33:04 -08002208 del_timer_sync(&up->timer);
2209 up->timer.function = serial8250_timeout;
Alan Coxd4e33fa2012-01-26 17:44:09 +00002210 if (up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 serial_unlink_irq_chain(up);
2212}
2213
2214static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
2215{
2216 unsigned int quot;
2217
2218 /*
2219 * Handle magic divisors for baud rates above baud_base on
2220 * SMSC SuperIO chips.
2221 */
2222 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2223 baud == (port->uartclk/4))
2224 quot = 0x8001;
2225 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2226 baud == (port->uartclk/8))
2227 quot = 0x8002;
2228 else
2229 quot = uart_get_divisor(port, baud);
2230
2231 return quot;
2232}
2233
Philippe Langlais235dae52010-07-29 17:13:57 +02002234void
2235serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2236 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
Jamie Iles49d57412010-12-01 23:39:35 +00002238 struct uart_8250_port *up =
2239 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 unsigned char cval, fcr = 0;
2241 unsigned long flags;
2242 unsigned int baud, quot;
2243
2244 switch (termios->c_cflag & CSIZE) {
2245 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +01002246 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 break;
2248 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +01002249 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 break;
2251 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +01002252 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 break;
2254 default:
2255 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +01002256 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 break;
2258 }
2259
2260 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +01002261 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (termios->c_cflag & PARENB)
2263 cval |= UART_LCR_PARITY;
2264 if (!(termios->c_cflag & PARODD))
2265 cval |= UART_LCR_EPAR;
2266#ifdef CMSPAR
2267 if (termios->c_cflag & CMSPAR)
2268 cval |= UART_LCR_SPAR;
2269#endif
2270
2271 /*
2272 * Ask the core to calculate the divisor for us.
2273 */
Anton Vorontsov24d481e2009-09-19 13:13:20 -07002274 baud = uart_get_baud_rate(port, termios, old,
2275 port->uartclk / 16 / 0xffff,
2276 port->uartclk / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 quot = serial8250_get_divisor(port, baud);
2278
2279 /*
Russell King4ba5e352005-06-23 10:43:04 +01002280 * Oxford Semi 952 rev B workaround
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 */
Russell King4ba5e352005-06-23 10:43:04 +01002282 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
Alan Cox3e8d4e22008-02-04 22:27:53 -08002283 quot++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
2286 if (baud < 2400)
2287 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2288 else
2289 fcr = uart_config[up->port.type].fcr;
2290 }
2291
2292 /*
2293 * MCR-based auto flow control. When AFE is enabled, RTS will be
2294 * deasserted when the receive FIFO contains more characters than
2295 * the trigger, or the MCR RTS bit is cleared. In the case where
2296 * the remote UART is not using CTS auto flow control, we must
2297 * have sufficient FIFO entries for the latency of the remote
2298 * UART to respond. IOW, at least 32 bytes of FIFO.
2299 */
2300 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
2301 up->mcr &= ~UART_MCR_AFE;
2302 if (termios->c_cflag & CRTSCTS)
2303 up->mcr |= UART_MCR_AFE;
2304 }
2305
2306 /*
2307 * Ok, we're now changing the port state. Do it with
2308 * interrupts disabled.
2309 */
2310 spin_lock_irqsave(&up->port.lock, flags);
2311
2312 /*
2313 * Update the per-port timeout.
2314 */
2315 uart_update_timeout(port, termios->c_cflag, baud);
2316
2317 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2318 if (termios->c_iflag & INPCK)
2319 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2320 if (termios->c_iflag & (BRKINT | PARMRK))
2321 up->port.read_status_mask |= UART_LSR_BI;
2322
2323 /*
2324 * Characteres to ignore
2325 */
2326 up->port.ignore_status_mask = 0;
2327 if (termios->c_iflag & IGNPAR)
2328 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2329 if (termios->c_iflag & IGNBRK) {
2330 up->port.ignore_status_mask |= UART_LSR_BI;
2331 /*
2332 * If we're ignoring parity and break indicators,
2333 * ignore overruns too (for real raw support).
2334 */
2335 if (termios->c_iflag & IGNPAR)
2336 up->port.ignore_status_mask |= UART_LSR_OE;
2337 }
2338
2339 /*
2340 * ignore all characters if CREAD is not set
2341 */
2342 if ((termios->c_cflag & CREAD) == 0)
2343 up->port.ignore_status_mask |= UART_LSR_DR;
2344
2345 /*
2346 * CTS flow control flag and modem status interrupts
2347 */
Ingo Molnarf8b372a2010-11-13 16:21:58 +01002348 up->ier &= ~UART_IER_MSI;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002349 if (!(up->bugs & UART_BUG_NOMSR) &&
2350 UART_ENABLE_MS(&up->port, termios->c_cflag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 up->ier |= UART_IER_MSI;
2352 if (up->capabilities & UART_CAP_UUE)
Stephen Warren4539c242011-05-17 16:12:36 -06002353 up->ier |= UART_IER_UUE;
2354 if (up->capabilities & UART_CAP_RTOIE)
2355 up->ier |= UART_IER_RTOIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
2357 serial_out(up, UART_IER, up->ier);
2358
2359 if (up->capabilities & UART_CAP_EFR) {
2360 unsigned char efr = 0;
2361 /*
2362 * TI16C752/Startech hardware flow control. FIXME:
2363 * - TI16C752 requires control thresholds to be set.
2364 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2365 */
2366 if (termios->c_cflag & CRTSCTS)
2367 efr |= UART_EFR_CTS;
2368
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002369 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Søren Holm06315342011-09-02 22:55:37 +02002370 if (up->port.flags & UPF_EXAR_EFR)
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002371 serial_out(up, UART_XR_EFR, efr);
Søren Holm06315342011-09-02 22:55:37 +02002372 else
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002373 serial_out(up, UART_EFR, efr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375
Russell Kingf2eda272008-09-01 21:47:59 +01002376#ifdef CONFIG_ARCH_OMAP
Jonathan McDowell255341c2006-08-14 23:05:32 -07002377 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
Russell King56685452008-09-01 21:25:33 +01002378 if (cpu_is_omap1510() && is_omap_port(up)) {
Jonathan McDowell255341c2006-08-14 23:05:32 -07002379 if (baud == 115200) {
2380 quot = 1;
2381 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2382 } else
2383 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2384 }
2385#endif
2386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (up->capabilities & UART_NATSEMI) {
2388 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002389 serial_out(up, UART_LCR, 0xe0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 } else {
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002391 serial_out(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +01002394 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 /*
2397 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2398 * is written without DLAB set, this mode will be disabled.
2399 */
2400 if (up->port.type == PORT_16750)
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002401 serial_out(up, UART_FCR, fcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002403 serial_out(up, UART_LCR, cval); /* reset DLAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 up->lcr = cval; /* Save LCR */
2405 if (up->port.type != PORT_16750) {
2406 if (fcr & UART_FCR_ENABLE_FIFO) {
2407 /* emulated UARTs (Lucent Venus 167x) need two steps */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002408 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002410 serial_out(up, UART_FCR, fcr); /* set fcr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
2412 serial8250_set_mctrl(&up->port, up->port.mctrl);
2413 spin_unlock_irqrestore(&up->port.lock, flags);
Alan Coxe991a2b2008-04-28 02:14:06 -07002414 /* Don't rewrite B0 */
2415 if (tty_termios_baud_rate(termios))
2416 tty_termios_encode_baud_rate(termios, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417}
Philippe Langlais235dae52010-07-29 17:13:57 +02002418EXPORT_SYMBOL(serial8250_do_set_termios);
2419
2420static void
2421serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2422 struct ktermios *old)
2423{
2424 if (port->set_termios)
2425 port->set_termios(port, termios, old);
2426 else
2427 serial8250_do_set_termios(port, termios, old);
2428}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430static void
Arnd Bergmanna0821df2010-06-01 22:53:11 +02002431serial8250_set_ldisc(struct uart_port *port, int new)
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002432{
Arnd Bergmanna0821df2010-06-01 22:53:11 +02002433 if (new == N_PPS) {
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002434 port->flags |= UPF_HARDPPS_CD;
2435 serial8250_enable_ms(port);
2436 } else
2437 port->flags &= ~UPF_HARDPPS_CD;
2438}
2439
Manuel Laussc161afe2010-09-25 15:13:45 +02002440
2441void serial8250_do_pm(struct uart_port *port, unsigned int state,
2442 unsigned int oldstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
Jamie Iles49d57412010-12-01 23:39:35 +00002444 struct uart_8250_port *p =
2445 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447 serial8250_set_sleep(p, state != 0);
Manuel Laussc161afe2010-09-25 15:13:45 +02002448}
2449EXPORT_SYMBOL(serial8250_do_pm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Manuel Laussc161afe2010-09-25 15:13:45 +02002451static void
2452serial8250_pm(struct uart_port *port, unsigned int state,
2453 unsigned int oldstate)
2454{
2455 if (port->pm)
2456 port->pm(port, state, oldstate);
2457 else
2458 serial8250_do_pm(port, state, oldstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
Russell Kingf2eda272008-09-01 21:47:59 +01002461static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2462{
2463 if (pt->port.iotype == UPIO_AU)
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002464 return 0x1000;
Russell Kingf2eda272008-09-01 21:47:59 +01002465#ifdef CONFIG_ARCH_OMAP
2466 if (is_omap_port(pt))
2467 return 0x16 << pt->port.regshift;
2468#endif
2469 return 8 << pt->port.regshift;
2470}
2471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472/*
2473 * Resource handling.
2474 */
2475static int serial8250_request_std_resource(struct uart_8250_port *up)
2476{
Russell Kingf2eda272008-09-01 21:47:59 +01002477 unsigned int size = serial8250_port_size(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 int ret = 0;
2479
2480 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002481 case UPIO_AU:
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002482 case UPIO_TSI:
2483 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 case UPIO_MEM:
2485 if (!up->port.mapbase)
2486 break;
2487
2488 if (!request_mem_region(up->port.mapbase, size, "serial")) {
2489 ret = -EBUSY;
2490 break;
2491 }
2492
2493 if (up->port.flags & UPF_IOREMAP) {
Alan Cox6f441fe2008-05-01 04:34:59 -07002494 up->port.membase = ioremap_nocache(up->port.mapbase,
2495 size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 if (!up->port.membase) {
2497 release_mem_region(up->port.mapbase, size);
2498 ret = -ENOMEM;
2499 }
2500 }
2501 break;
2502
2503 case UPIO_HUB6:
2504 case UPIO_PORT:
2505 if (!request_region(up->port.iobase, size, "serial"))
2506 ret = -EBUSY;
2507 break;
2508 }
2509 return ret;
2510}
2511
2512static void serial8250_release_std_resource(struct uart_8250_port *up)
2513{
Russell Kingf2eda272008-09-01 21:47:59 +01002514 unsigned int size = serial8250_port_size(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002517 case UPIO_AU:
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002518 case UPIO_TSI:
2519 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 case UPIO_MEM:
2521 if (!up->port.mapbase)
2522 break;
2523
2524 if (up->port.flags & UPF_IOREMAP) {
2525 iounmap(up->port.membase);
2526 up->port.membase = NULL;
2527 }
2528
2529 release_mem_region(up->port.mapbase, size);
2530 break;
2531
2532 case UPIO_HUB6:
2533 case UPIO_PORT:
2534 release_region(up->port.iobase, size);
2535 break;
2536 }
2537}
2538
2539static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2540{
2541 unsigned long start = UART_RSA_BASE << up->port.regshift;
2542 unsigned int size = 8 << up->port.regshift;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002543 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
2545 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 case UPIO_HUB6:
2547 case UPIO_PORT:
2548 start += up->port.iobase;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002549 if (request_region(start, size, "serial-rsa"))
2550 ret = 0;
2551 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 ret = -EBUSY;
2553 break;
2554 }
2555
2556 return ret;
2557}
2558
2559static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2560{
2561 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2562 unsigned int size = 8 << up->port.regshift;
2563
2564 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 case UPIO_HUB6:
2566 case UPIO_PORT:
2567 release_region(up->port.iobase + offset, size);
2568 break;
2569 }
2570}
2571
2572static void serial8250_release_port(struct uart_port *port)
2573{
Jamie Iles49d57412010-12-01 23:39:35 +00002574 struct uart_8250_port *up =
2575 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 serial8250_release_std_resource(up);
2578 if (up->port.type == PORT_RSA)
2579 serial8250_release_rsa_resource(up);
2580}
2581
2582static int serial8250_request_port(struct uart_port *port)
2583{
Jamie Iles49d57412010-12-01 23:39:35 +00002584 struct uart_8250_port *up =
2585 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 int ret = 0;
2587
2588 ret = serial8250_request_std_resource(up);
2589 if (ret == 0 && up->port.type == PORT_RSA) {
2590 ret = serial8250_request_rsa_resource(up);
2591 if (ret < 0)
2592 serial8250_release_std_resource(up);
2593 }
2594
2595 return ret;
2596}
2597
2598static void serial8250_config_port(struct uart_port *port, int flags)
2599{
Jamie Iles49d57412010-12-01 23:39:35 +00002600 struct uart_8250_port *up =
2601 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 int probeflags = PROBE_ANY;
2603 int ret;
2604
2605 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 * Find the region that we can probe for. This in turn
2607 * tells us whether we can probe for the type of port.
2608 */
2609 ret = serial8250_request_std_resource(up);
2610 if (ret < 0)
2611 return;
2612
2613 ret = serial8250_request_rsa_resource(up);
2614 if (ret < 0)
2615 probeflags &= ~PROBE_RSA;
2616
Alan Coxb8e7e402009-05-28 14:01:35 +01002617 if (up->port.iotype != up->cur_iotype)
2618 set_io_from_upio(port);
2619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 if (flags & UART_CONFIG_TYPE)
2621 autoconfig(up, probeflags);
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002622
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002623 /* if access method is AU, it is a 16550 with a quirk */
2624 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
2625 up->bugs |= UART_BUG_NOMSR;
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2628 autoconfig_irq(up);
2629
2630 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2631 serial8250_release_rsa_resource(up);
2632 if (up->port.type == PORT_UNKNOWN)
2633 serial8250_release_std_resource(up);
2634}
2635
2636static int
2637serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2638{
Yinghai Lua62c4132008-08-19 20:49:55 -07002639 if (ser->irq >= nr_irqs || ser->irq < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2641 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2642 ser->type == PORT_STARTECH)
2643 return -EINVAL;
2644 return 0;
2645}
2646
2647static const char *
2648serial8250_type(struct uart_port *port)
2649{
2650 int type = port->type;
2651
2652 if (type >= ARRAY_SIZE(uart_config))
2653 type = 0;
2654 return uart_config[type].name;
2655}
2656
2657static struct uart_ops serial8250_pops = {
2658 .tx_empty = serial8250_tx_empty,
2659 .set_mctrl = serial8250_set_mctrl,
2660 .get_mctrl = serial8250_get_mctrl,
2661 .stop_tx = serial8250_stop_tx,
2662 .start_tx = serial8250_start_tx,
2663 .stop_rx = serial8250_stop_rx,
2664 .enable_ms = serial8250_enable_ms,
2665 .break_ctl = serial8250_break_ctl,
2666 .startup = serial8250_startup,
2667 .shutdown = serial8250_shutdown,
2668 .set_termios = serial8250_set_termios,
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002669 .set_ldisc = serial8250_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 .pm = serial8250_pm,
2671 .type = serial8250_type,
2672 .release_port = serial8250_release_port,
2673 .request_port = serial8250_request_port,
2674 .config_port = serial8250_config_port,
2675 .verify_port = serial8250_verify_port,
Jason Wesself2d937f2008-04-17 20:05:37 +02002676#ifdef CONFIG_CONSOLE_POLL
2677 .poll_get_char = serial8250_get_poll_char,
2678 .poll_put_char = serial8250_put_poll_char,
2679#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680};
2681
2682static struct uart_8250_port serial8250_ports[UART_NR];
2683
Alan Coxaf7f3742010-10-18 11:38:02 -07002684static void (*serial8250_isa_config)(int port, struct uart_port *up,
2685 unsigned short *capabilities);
2686
2687void serial8250_set_isa_configurator(
2688 void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
2689{
2690 serial8250_isa_config = v;
2691}
2692EXPORT_SYMBOL(serial8250_set_isa_configurator);
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694static void __init serial8250_isa_init_ports(void)
2695{
2696 struct uart_8250_port *up;
2697 static int first = 1;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002698 int i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700 if (!first)
2701 return;
2702 first = 0;
2703
Dave Jonesa61c2d72006-01-07 23:18:19 +00002704 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 struct uart_8250_port *up = &serial8250_ports[i];
2706
2707 up->port.line = i;
2708 spin_lock_init(&up->port.lock);
2709
2710 init_timer(&up->timer);
2711 up->timer.function = serial8250_timeout;
2712
2713 /*
2714 * ALPHA_KLUDGE_MCR needs to be killed.
2715 */
2716 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2717 up->mcr_force = ALPHA_KLUDGE_MCR;
2718
2719 up->port.ops = &serial8250_pops;
2720 }
2721
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002722 if (share_irqs)
2723 irqflag = IRQF_SHARED;
2724
Russell King44454bc2005-06-30 22:41:22 +01002725 for (i = 0, up = serial8250_ports;
Dave Jonesa61c2d72006-01-07 23:18:19 +00002726 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 i++, up++) {
2728 up->port.iobase = old_serial_port[i].port;
2729 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002730 up->port.irqflags = old_serial_port[i].irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 up->port.uartclk = old_serial_port[i].baud_base * 16;
2732 up->port.flags = old_serial_port[i].flags;
2733 up->port.hub6 = old_serial_port[i].hub6;
2734 up->port.membase = old_serial_port[i].iomem_base;
2735 up->port.iotype = old_serial_port[i].io_type;
2736 up->port.regshift = old_serial_port[i].iomem_reg_shift;
David Daney7d6a07d2009-01-02 13:49:47 +00002737 set_io_from_upio(&up->port);
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002738 up->port.irqflags |= irqflag;
Alan Coxaf7f3742010-10-18 11:38:02 -07002739 if (serial8250_isa_config != NULL)
2740 serial8250_isa_config(i, &up->port, &up->capabilities);
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 }
2743}
2744
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08002745static void
2746serial8250_init_fixed_type_port(struct uart_8250_port *up, unsigned int type)
2747{
2748 up->port.type = type;
2749 up->port.fifosize = uart_config[type].fifo_size;
2750 up->capabilities = uart_config[type].flags;
2751 up->tx_loadsz = uart_config[type].tx_loadsz;
2752}
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754static void __init
2755serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2756{
2757 int i;
2758
Alan Coxb8e7e402009-05-28 14:01:35 +01002759 for (i = 0; i < nr_uarts; i++) {
2760 struct uart_8250_port *up = &serial8250_ports[i];
2761 up->cur_iotype = 0xFF;
2762 }
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 serial8250_isa_init_ports();
2765
Dave Jonesa61c2d72006-01-07 23:18:19 +00002766 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 struct uart_8250_port *up = &serial8250_ports[i];
2768
2769 up->port.dev = dev;
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08002770
2771 if (up->port.flags & UPF_FIXED_TYPE)
2772 serial8250_init_fixed_type_port(up, up->port.type);
2773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 uart_add_one_port(drv, &up->port);
2775 }
2776}
2777
2778#ifdef CONFIG_SERIAL_8250_CONSOLE
2779
Russell Kingd3587882006-03-20 20:00:09 +00002780static void serial8250_console_putchar(struct uart_port *port, int ch)
2781{
Jamie Iles49d57412010-12-01 23:39:35 +00002782 struct uart_8250_port *up =
2783 container_of(port, struct uart_8250_port, port);
Russell Kingd3587882006-03-20 20:00:09 +00002784
2785 wait_for_xmitr(up, UART_LSR_THRE);
2786 serial_out(up, UART_TX, ch);
2787}
2788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789/*
2790 * Print a string to the serial port trying not to disturb
2791 * any possible real use of the port...
2792 *
2793 * The console_lock must be held when we get here.
2794 */
2795static void
2796serial8250_console_write(struct console *co, const char *s, unsigned int count)
2797{
2798 struct uart_8250_port *up = &serial8250_ports[co->index];
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002799 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 unsigned int ier;
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002801 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Andrew Morton78512ec2005-11-07 00:59:13 -08002803 touch_nmi_watchdog();
2804
Andrew Morton68aa2c02006-06-30 02:29:59 -07002805 local_irq_save(flags);
2806 if (up->port.sysrq) {
Paul Gortmaker86b21192011-12-04 18:42:22 -05002807 /* serial8250_handle_irq() already took the lock */
Andrew Morton68aa2c02006-06-30 02:29:59 -07002808 locked = 0;
2809 } else if (oops_in_progress) {
2810 locked = spin_trylock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002811 } else
Andrew Morton68aa2c02006-06-30 02:29:59 -07002812 spin_lock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 /*
Ralf Baechledc7bf132006-02-15 09:59:47 +00002815 * First save the IER then disable the interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 */
2817 ier = serial_in(up, UART_IER);
2818
2819 if (up->capabilities & UART_CAP_UUE)
2820 serial_out(up, UART_IER, UART_IER_UUE);
2821 else
2822 serial_out(up, UART_IER, 0);
2823
Russell Kingd3587882006-03-20 20:00:09 +00002824 uart_console_write(&up->port, s, count, serial8250_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826 /*
2827 * Finally, wait for transmitter to become empty
2828 * and restore the IER
2829 */
Alan Coxf91a3712006-01-21 14:59:12 +00002830 wait_for_xmitr(up, BOTH_EMPTY);
Russell Kinga88d75b2006-04-30 11:30:15 +01002831 serial_out(up, UART_IER, ier);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002832
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002833 /*
2834 * The receive handling will happen properly because the
2835 * receive ready bit will still be set; it is not cleared
2836 * on read. However, modem control will not, we must
2837 * call it if we have saved something in the saved flags
2838 * while processing with interrupts off.
2839 */
2840 if (up->msr_saved_flags)
Paul Gortmaker3986fb22011-12-04 18:42:20 -05002841 serial8250_modem_status(up);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002842
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002843 if (locked)
Andrew Morton68aa2c02006-06-30 02:29:59 -07002844 spin_unlock(&up->port.lock);
2845 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846}
2847
Vivek Goyal118c0ac2007-01-11 01:52:44 +01002848static int __init serial8250_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
2850 struct uart_port *port;
2851 int baud = 9600;
2852 int bits = 8;
2853 int parity = 'n';
2854 int flow = 'n';
2855
2856 /*
2857 * Check whether an invalid uart number has been specified, and
2858 * if so, search for the first available port that does have
2859 * console support.
2860 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002861 if (co->index >= nr_uarts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 co->index = 0;
2863 port = &serial8250_ports[co->index].port;
2864 if (!port->iobase && !port->membase)
2865 return -ENODEV;
2866
2867 if (options)
2868 uart_parse_options(options, &baud, &parity, &bits, &flow);
2869
2870 return uart_set_options(port, co, baud, parity, bits, flow);
2871}
2872
Daniel Ritzb6b1d872007-08-03 16:07:43 +02002873static int serial8250_console_early_setup(void)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002874{
2875 return serial8250_find_port_for_earlycon();
2876}
2877
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878static struct console serial8250_console = {
2879 .name = "ttyS",
2880 .write = serial8250_console_write,
2881 .device = uart_console_device,
2882 .setup = serial8250_console_setup,
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002883 .early_setup = serial8250_console_early_setup,
Peter Zijlstraa80c49d2010-11-15 21:11:12 +01002884 .flags = CON_PRINTBUFFER | CON_ANYTIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 .index = -1,
2886 .data = &serial8250_reg,
2887};
2888
2889static int __init serial8250_console_init(void)
2890{
Eric W. Biederman05d81d22008-07-12 13:47:53 -07002891 if (nr_uarts > UART_NR)
2892 nr_uarts = UART_NR;
2893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 serial8250_isa_init_ports();
2895 register_console(&serial8250_console);
2896 return 0;
2897}
2898console_initcall(serial8250_console_init);
2899
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002900int serial8250_find_port(struct uart_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
2902 int line;
2903 struct uart_port *port;
2904
Dave Jonesa61c2d72006-01-07 23:18:19 +00002905 for (line = 0; line < nr_uarts; line++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 port = &serial8250_ports[line].port;
Russell King50aec3b52006-01-04 18:13:03 +00002907 if (uart_match_port(p, port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 return line;
2909 }
2910 return -ENODEV;
2911}
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913#define SERIAL8250_CONSOLE &serial8250_console
2914#else
2915#define SERIAL8250_CONSOLE NULL
2916#endif
2917
2918static struct uart_driver serial8250_reg = {
2919 .owner = THIS_MODULE,
2920 .driver_name = "serial",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 .dev_name = "ttyS",
2922 .major = TTY_MAJOR,
2923 .minor = 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 .cons = SERIAL8250_CONSOLE,
2925};
2926
Russell Kingd856c662006-02-23 10:22:13 +00002927/*
2928 * early_serial_setup - early registration for 8250 ports
2929 *
2930 * Setup an 8250 port structure prior to console initialisation. Use
2931 * after console initialisation will cause undefined behaviour.
2932 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933int __init early_serial_setup(struct uart_port *port)
2934{
David Daneyb4304282009-01-02 13:49:41 +00002935 struct uart_port *p;
2936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (port->line >= ARRAY_SIZE(serial8250_ports))
2938 return -ENODEV;
2939
2940 serial8250_isa_init_ports();
David Daneyb4304282009-01-02 13:49:41 +00002941 p = &serial8250_ports[port->line].port;
2942 p->iobase = port->iobase;
2943 p->membase = port->membase;
2944 p->irq = port->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002945 p->irqflags = port->irqflags;
David Daneyb4304282009-01-02 13:49:41 +00002946 p->uartclk = port->uartclk;
2947 p->fifosize = port->fifosize;
2948 p->regshift = port->regshift;
2949 p->iotype = port->iotype;
2950 p->flags = port->flags;
2951 p->mapbase = port->mapbase;
2952 p->private_data = port->private_data;
Helge Deller125c97d2009-01-13 22:51:07 +01002953 p->type = port->type;
2954 p->line = port->line;
David Daney7d6a07d2009-01-02 13:49:47 +00002955
2956 set_io_from_upio(p);
2957 if (port->serial_in)
2958 p->serial_in = port->serial_in;
2959 if (port->serial_out)
2960 p->serial_out = port->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01002961 if (port->handle_irq)
2962 p->handle_irq = port->handle_irq;
2963 else
2964 p->handle_irq = serial8250_default_handle_irq;
David Daney7d6a07d2009-01-02 13:49:47 +00002965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 return 0;
2967}
2968
2969/**
2970 * serial8250_suspend_port - suspend one serial port
2971 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 *
2973 * Suspend one serial port.
2974 */
2975void serial8250_suspend_port(int line)
2976{
2977 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2978}
2979
2980/**
2981 * serial8250_resume_port - resume one serial port
2982 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 *
2984 * Resume one serial port.
2985 */
2986void serial8250_resume_port(int line)
2987{
David Woodhouseb5b82df2007-05-17 14:27:39 +08002988 struct uart_8250_port *up = &serial8250_ports[line];
2989
2990 if (up->capabilities & UART_NATSEMI) {
David Woodhouseb5b82df2007-05-17 14:27:39 +08002991 /* Ensure it's still in high speed mode */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002992 serial_out(up, UART_LCR, 0xE0);
David Woodhouseb5b82df2007-05-17 14:27:39 +08002993
Yin Kangkai0d0389e2011-02-09 11:35:18 +08002994 ns16550a_goto_highspeed(up);
David Woodhouseb5b82df2007-05-17 14:27:39 +08002995
Paul Gortmaker0acf5192012-03-08 19:12:08 -05002996 serial_out(up, UART_LCR, 0);
Yin Kangkai95926d22011-02-09 11:34:20 +08002997 up->port.uartclk = 921600*16;
David Woodhouseb5b82df2007-05-17 14:27:39 +08002998 }
2999 uart_resume_port(&serial8250_reg, &up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000}
3001
3002/*
3003 * Register a set of serial devices attached to a platform device. The
3004 * list is terminated with a zero flags entry, which means we expect
3005 * all entries to have at least UPF_BOOT_AUTOCONF set.
3006 */
Russell King3ae5eae2005-11-09 22:32:44 +00003007static int __devinit serial8250_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
Russell King3ae5eae2005-11-09 22:32:44 +00003009 struct plat_serial8250_port *p = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 struct uart_port port;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003011 int ret, i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
3013 memset(&port, 0, sizeof(struct uart_port));
3014
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003015 if (share_irqs)
3016 irqflag = IRQF_SHARED;
3017
Russell Kingec9f47c2005-06-27 11:12:54 +01003018 for (i = 0; p && p->flags != 0; p++, i++) {
Will Newton74a197412008-02-04 22:27:50 -08003019 port.iobase = p->iobase;
3020 port.membase = p->membase;
3021 port.irq = p->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07003022 port.irqflags = p->irqflags;
Will Newton74a197412008-02-04 22:27:50 -08003023 port.uartclk = p->uartclk;
3024 port.regshift = p->regshift;
3025 port.iotype = p->iotype;
3026 port.flags = p->flags;
3027 port.mapbase = p->mapbase;
3028 port.hub6 = p->hub6;
3029 port.private_data = p->private_data;
David Daney8e23fcc2009-01-02 13:49:54 +00003030 port.type = p->type;
David Daney7d6a07d2009-01-02 13:49:47 +00003031 port.serial_in = p->serial_in;
3032 port.serial_out = p->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01003033 port.handle_irq = p->handle_irq;
Philippe Langlais235dae52010-07-29 17:13:57 +02003034 port.set_termios = p->set_termios;
Manuel Laussc161afe2010-09-25 15:13:45 +02003035 port.pm = p->pm;
Will Newton74a197412008-02-04 22:27:50 -08003036 port.dev = &dev->dev;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003037 port.irqflags |= irqflag;
Russell Kingec9f47c2005-06-27 11:12:54 +01003038 ret = serial8250_register_port(&port);
3039 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00003040 dev_err(&dev->dev, "unable to register port at index %d "
Josh Boyer4f640ef2007-07-23 18:43:44 -07003041 "(IO%lx MEM%llx IRQ%d): %d\n", i,
3042 p->iobase, (unsigned long long)p->mapbase,
3043 p->irq, ret);
Russell Kingec9f47c2005-06-27 11:12:54 +01003044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 }
3046 return 0;
3047}
3048
3049/*
3050 * Remove serial ports registered against a platform device.
3051 */
Russell King3ae5eae2005-11-09 22:32:44 +00003052static int __devexit serial8250_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
3054 int i;
3055
Dave Jonesa61c2d72006-01-07 23:18:19 +00003056 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 struct uart_8250_port *up = &serial8250_ports[i];
3058
Russell King3ae5eae2005-11-09 22:32:44 +00003059 if (up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 serial8250_unregister_port(i);
3061 }
3062 return 0;
3063}
3064
Russell King3ae5eae2005-11-09 22:32:44 +00003065static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
3067 int i;
3068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 for (i = 0; i < UART_NR; i++) {
3070 struct uart_8250_port *up = &serial8250_ports[i];
3071
Russell King3ae5eae2005-11-09 22:32:44 +00003072 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 uart_suspend_port(&serial8250_reg, &up->port);
3074 }
3075
3076 return 0;
3077}
3078
Russell King3ae5eae2005-11-09 22:32:44 +00003079static int serial8250_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
3081 int i;
3082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 for (i = 0; i < UART_NR; i++) {
3084 struct uart_8250_port *up = &serial8250_ports[i];
3085
Russell King3ae5eae2005-11-09 22:32:44 +00003086 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
David Woodhouseb5b82df2007-05-17 14:27:39 +08003087 serial8250_resume_port(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 }
3089
3090 return 0;
3091}
3092
Russell King3ae5eae2005-11-09 22:32:44 +00003093static struct platform_driver serial8250_isa_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 .probe = serial8250_probe,
3095 .remove = __devexit_p(serial8250_remove),
3096 .suspend = serial8250_suspend,
3097 .resume = serial8250_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00003098 .driver = {
3099 .name = "serial8250",
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003100 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00003101 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102};
3103
3104/*
3105 * This "device" covers _all_ ISA 8250-compatible serial devices listed
3106 * in the table in include/asm/serial.h
3107 */
3108static struct platform_device *serial8250_isa_devs;
3109
3110/*
3111 * serial8250_register_port and serial8250_unregister_port allows for
3112 * 16x50 serial ports to be configured at run-time, to support PCMCIA
3113 * modems and PCI multiport cards.
3114 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003115static DEFINE_MUTEX(serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
3118{
3119 int i;
3120
3121 /*
3122 * First, find a port entry which matches.
3123 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003124 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 if (uart_match_port(&serial8250_ports[i].port, port))
3126 return &serial8250_ports[i];
3127
3128 /*
3129 * We didn't find a matching entry, so look for the first
3130 * free entry. We look for one which hasn't been previously
3131 * used (indicated by zero iobase).
3132 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003133 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
3135 serial8250_ports[i].port.iobase == 0)
3136 return &serial8250_ports[i];
3137
3138 /*
3139 * That also failed. Last resort is to find any entry which
3140 * doesn't have a real port associated with it.
3141 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003142 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
3144 return &serial8250_ports[i];
3145
3146 return NULL;
3147}
3148
3149/**
3150 * serial8250_register_port - register a serial port
3151 * @port: serial port template
3152 *
3153 * Configure the serial port specified by the request. If the
3154 * port exists and is in use, it is hung up and unregistered
3155 * first.
3156 *
3157 * The port is then probed and if necessary the IRQ is autodetected
3158 * If this fails an error is returned.
3159 *
3160 * On success the port is ready to use and the line number is returned.
3161 */
3162int serial8250_register_port(struct uart_port *port)
3163{
3164 struct uart_8250_port *uart;
3165 int ret = -ENOSPC;
3166
3167 if (port->uartclk == 0)
3168 return -EINVAL;
3169
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003170 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
3172 uart = serial8250_find_match_or_unused(port);
3173 if (uart) {
3174 uart_remove_one_port(&serial8250_reg, &uart->port);
3175
Will Newton74a197412008-02-04 22:27:50 -08003176 uart->port.iobase = port->iobase;
3177 uart->port.membase = port->membase;
3178 uart->port.irq = port->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07003179 uart->port.irqflags = port->irqflags;
Will Newton74a197412008-02-04 22:27:50 -08003180 uart->port.uartclk = port->uartclk;
3181 uart->port.fifosize = port->fifosize;
3182 uart->port.regshift = port->regshift;
3183 uart->port.iotype = port->iotype;
3184 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
3185 uart->port.mapbase = port->mapbase;
3186 uart->port.private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 if (port->dev)
3188 uart->port.dev = port->dev;
David Daney8e23fcc2009-01-02 13:49:54 +00003189
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08003190 if (port->flags & UPF_FIXED_TYPE)
3191 serial8250_init_fixed_type_port(uart, port->type);
David Daney8e23fcc2009-01-02 13:49:54 +00003192
David Daney7d6a07d2009-01-02 13:49:47 +00003193 set_io_from_upio(&uart->port);
3194 /* Possibly override default I/O functions. */
3195 if (port->serial_in)
3196 uart->port.serial_in = port->serial_in;
3197 if (port->serial_out)
3198 uart->port.serial_out = port->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01003199 if (port->handle_irq)
3200 uart->port.handle_irq = port->handle_irq;
Philippe Langlais235dae52010-07-29 17:13:57 +02003201 /* Possibly override set_termios call */
3202 if (port->set_termios)
3203 uart->port.set_termios = port->set_termios;
Manuel Laussc161afe2010-09-25 15:13:45 +02003204 if (port->pm)
3205 uart->port.pm = port->pm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Alan Coxaf7f3742010-10-18 11:38:02 -07003207 if (serial8250_isa_config != NULL)
3208 serial8250_isa_config(0, &uart->port,
3209 &uart->capabilities);
3210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 ret = uart_add_one_port(&serial8250_reg, &uart->port);
3212 if (ret == 0)
3213 ret = uart->port.line;
3214 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003215 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217 return ret;
3218}
3219EXPORT_SYMBOL(serial8250_register_port);
3220
3221/**
3222 * serial8250_unregister_port - remove a 16x50 serial port at runtime
3223 * @line: serial line number
3224 *
3225 * Remove one serial port. This may not be called from interrupt
3226 * context. We hand the port back to the our control.
3227 */
3228void serial8250_unregister_port(int line)
3229{
3230 struct uart_8250_port *uart = &serial8250_ports[line];
3231
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003232 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 uart_remove_one_port(&serial8250_reg, &uart->port);
3234 if (serial8250_isa_devs) {
3235 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
3236 uart->port.type = PORT_UNKNOWN;
3237 uart->port.dev = &serial8250_isa_devs->dev;
leitao@linux.vnet.ibm.comcb01ece2011-05-26 11:18:39 -03003238 uart->capabilities = uart_config[uart->port.type].flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 uart_add_one_port(&serial8250_reg, &uart->port);
3240 } else {
3241 uart->port.dev = NULL;
3242 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003243 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244}
3245EXPORT_SYMBOL(serial8250_unregister_port);
3246
3247static int __init serial8250_init(void)
3248{
Alan Cox25db8ad2008-08-19 20:49:40 -07003249 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Dave Jonesa61c2d72006-01-07 23:18:19 +00003251 if (nr_uarts > UART_NR)
3252 nr_uarts = UART_NR;
3253
Paul Bollef1fb9bb2008-12-30 14:06:43 +01003254 printk(KERN_INFO "Serial: 8250/16550 driver, "
Dave Jonesa61c2d72006-01-07 23:18:19 +00003255 "%d ports, IRQ sharing %sabled\n", nr_uarts,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 share_irqs ? "en" : "dis");
3257
David Millerb70ac772008-10-13 10:36:31 +01003258#ifdef CONFIG_SPARC
3259 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
3260#else
3261 serial8250_reg.nr = UART_NR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 ret = uart_register_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003263#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 if (ret)
3265 goto out;
3266
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003267 serial8250_isa_devs = platform_device_alloc("serial8250",
3268 PLAT8250_DEV_LEGACY);
3269 if (!serial8250_isa_devs) {
3270 ret = -ENOMEM;
Russell Kingbc965a72006-01-18 09:54:29 +00003271 goto unreg_uart_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 }
3273
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003274 ret = platform_device_add(serial8250_isa_devs);
3275 if (ret)
3276 goto put_dev;
3277
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
3279
Russell Kingbc965a72006-01-18 09:54:29 +00003280 ret = platform_driver_register(&serial8250_isa_driver);
3281 if (ret == 0)
3282 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Russell Kingbc965a72006-01-18 09:54:29 +00003284 platform_device_del(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003285put_dev:
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003286 platform_device_put(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003287unreg_uart_drv:
David Millerb70ac772008-10-13 10:36:31 +01003288#ifdef CONFIG_SPARC
3289 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3290#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003292#endif
Alan Cox25db8ad2008-08-19 20:49:40 -07003293out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 return ret;
3295}
3296
3297static void __exit serial8250_exit(void)
3298{
3299 struct platform_device *isa_dev = serial8250_isa_devs;
3300
3301 /*
3302 * This tells serial8250_unregister_port() not to re-register
3303 * the ports (thereby making serial8250_isa_driver permanently
3304 * in use.)
3305 */
3306 serial8250_isa_devs = NULL;
3307
Russell King3ae5eae2005-11-09 22:32:44 +00003308 platform_driver_unregister(&serial8250_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 platform_device_unregister(isa_dev);
3310
David Millerb70ac772008-10-13 10:36:31 +01003311#ifdef CONFIG_SPARC
3312 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3313#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316}
3317
3318module_init(serial8250_init);
3319module_exit(serial8250_exit);
3320
3321EXPORT_SYMBOL(serial8250_suspend_port);
3322EXPORT_SYMBOL(serial8250_resume_port);
3323
3324MODULE_LICENSE("GPL");
Adrian Bunkd87a6d92008-07-16 21:53:31 +01003325MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
3327module_param(share_irqs, uint, 0644);
3328MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
3329 " (unsafe)");
3330
Dave Jonesa61c2d72006-01-07 23:18:19 +00003331module_param(nr_uarts, uint, 0644);
3332MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
3333
Chuck Ebbertd41a4b52009-10-01 15:44:26 -07003334module_param(skip_txen_test, uint, 0644);
3335MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
3336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337#ifdef CONFIG_SERIAL_8250_RSA
3338module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
3339MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
3340#endif
3341MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);