blob: b0eb961393245d63009ed115c663f5ea06bfee68 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver for 8250/16550-type serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2001 Russell King.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * A note about mapbase / membase
14 *
15 * mapbase is the physical address of the IO port.
16 * membase is an 'ioremapped' cookie.
17 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
20#define SUPPORT_SYSRQ
21#endif
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/ioport.h>
26#include <linux/init.h>
27#include <linux/console.h>
28#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/delay.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010030#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/tty.h>
Daniel Drakecd3ecad2010-10-20 16:00:48 -070032#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/tty_flip.h>
34#include <linux/serial_reg.h>
35#include <linux/serial_core.h>
36#include <linux/serial.h>
37#include <linux/serial_8250.h>
Andrew Morton78512ec2005-11-07 00:59:13 -080038#include <linux/nmi.h>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000039#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/io.h>
43#include <asm/irq.h>
44
45#include "8250.h"
46
David Millerb70ac772008-10-13 10:36:31 +010047#ifdef CONFIG_SPARC
48#include "suncore.h"
49#endif
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * Configuration:
Thomas Gleixner40663cc2006-07-01 19:29:43 -070053 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * is unsafe when used on edge-triggered interrupts.
55 */
Adrian Bunk408b6642005-05-01 08:59:29 -070056static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Dave Jonesa61c2d72006-01-07 23:18:19 +000058static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
59
David S. Miller84408382008-10-13 10:45:26 +010060static struct uart_driver serial8250_reg;
61
62static int serial_index(struct uart_port *port)
63{
64 return (serial8250_reg.minor - 64) + port->line;
65}
66
Chuck Ebbertd41a4b52009-10-01 15:44:26 -070067static unsigned int skip_txen_test; /* force skip of txen test at init time */
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
70 * Debugging.
71 */
72#if 0
73#define DEBUG_AUTOCONF(fmt...) printk(fmt)
74#else
75#define DEBUG_AUTOCONF(fmt...) do { } while (0)
76#endif
77
78#if 0
79#define DEBUG_INTR(fmt...) printk(fmt)
80#else
81#define DEBUG_INTR(fmt...) do { } while (0)
82#endif
83
Jiri Slabye7328ae2011-06-05 22:51:49 +020084#define PASS_LIMIT 512
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Dick Hollenbeckbca47612009-12-09 12:31:34 -080086#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
87
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
90#define CONFIG_SERIAL_DETECT_IRQ 1
91#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#ifdef CONFIG_SERIAL_8250_MANY_PORTS
93#define CONFIG_SERIAL_MANY_PORTS 1
94#endif
95
96/*
97 * HUB6 is always on. This will be removed once the header
98 * files have been cleaned.
99 */
100#define CONFIG_HUB6 1
101
Bryan Wua4ed1e42008-05-31 16:10:04 +0800102#include <asm/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
104 * SERIAL_PORT_DFNS tells us about built-in ports that have no
105 * standard enumeration mechanism. Platforms that can find all
106 * serial ports via mechanisms like ACPI or PCI need not supply it.
107 */
108#ifndef SERIAL_PORT_DFNS
109#define SERIAL_PORT_DFNS
110#endif
111
Arjan van de Vencb3592b2005-11-28 21:04:11 +0000112static const struct old_serial_port old_serial_port[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 SERIAL_PORT_DFNS /* defined in asm/serial.h */
114};
115
Russell King026d02a2005-06-29 18:45:19 +0100116#define UART_NR CONFIG_SERIAL_8250_NR_UARTS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118#ifdef CONFIG_SERIAL_8250_RSA
119
120#define PORT_RSA_MAX 4
121static unsigned long probe_rsa[PORT_RSA_MAX];
122static unsigned int probe_rsa_count;
123#endif /* CONFIG_SERIAL_8250_RSA */
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125struct irq_info {
Alan Cox25db8ad2008-08-19 20:49:40 -0700126 struct hlist_node node;
127 int irq;
128 spinlock_t lock; /* Protects list not the hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct list_head *head;
130};
131
Alan Cox25db8ad2008-08-19 20:49:40 -0700132#define NR_IRQ_HASH 32 /* Can be adjusted later */
133static struct hlist_head irq_lists[NR_IRQ_HASH];
134static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136/*
137 * Here we define the default xmit fifo size used for each type of UART.
138 */
139static const struct serial8250_config uart_config[] = {
140 [PORT_UNKNOWN] = {
141 .name = "unknown",
142 .fifo_size = 1,
143 .tx_loadsz = 1,
144 },
145 [PORT_8250] = {
146 .name = "8250",
147 .fifo_size = 1,
148 .tx_loadsz = 1,
149 },
150 [PORT_16450] = {
151 .name = "16450",
152 .fifo_size = 1,
153 .tx_loadsz = 1,
154 },
155 [PORT_16550] = {
156 .name = "16550",
157 .fifo_size = 1,
158 .tx_loadsz = 1,
159 },
160 [PORT_16550A] = {
161 .name = "16550A",
162 .fifo_size = 16,
163 .tx_loadsz = 16,
164 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
165 .flags = UART_CAP_FIFO,
166 },
167 [PORT_CIRRUS] = {
168 .name = "Cirrus",
169 .fifo_size = 1,
170 .tx_loadsz = 1,
171 },
172 [PORT_16650] = {
173 .name = "ST16650",
174 .fifo_size = 1,
175 .tx_loadsz = 1,
176 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
177 },
178 [PORT_16650V2] = {
179 .name = "ST16650V2",
180 .fifo_size = 32,
181 .tx_loadsz = 16,
182 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
183 UART_FCR_T_TRIG_00,
184 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
185 },
186 [PORT_16750] = {
187 .name = "TI16750",
188 .fifo_size = 64,
189 .tx_loadsz = 64,
190 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
191 UART_FCR7_64BYTE,
192 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
193 },
194 [PORT_STARTECH] = {
195 .name = "Startech",
196 .fifo_size = 1,
197 .tx_loadsz = 1,
198 },
199 [PORT_16C950] = {
200 .name = "16C950/954",
201 .fifo_size = 128,
202 .tx_loadsz = 128,
203 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
Pavel Machekd0694e22011-01-09 08:38:48 +0100204 /* UART_CAP_EFR breaks billionon CF bluetooth card. */
205 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 },
207 [PORT_16654] = {
208 .name = "ST16654",
209 .fifo_size = 64,
210 .tx_loadsz = 32,
211 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
212 UART_FCR_T_TRIG_10,
213 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
214 },
215 [PORT_16850] = {
216 .name = "XR16850",
217 .fifo_size = 128,
218 .tx_loadsz = 128,
219 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
220 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
221 },
222 [PORT_RSA] = {
223 .name = "RSA",
224 .fifo_size = 2048,
225 .tx_loadsz = 2048,
226 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
227 .flags = UART_CAP_FIFO,
228 },
229 [PORT_NS16550A] = {
230 .name = "NS16550A",
231 .fifo_size = 16,
232 .tx_loadsz = 16,
233 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
234 .flags = UART_CAP_FIFO | UART_NATSEMI,
235 },
236 [PORT_XSCALE] = {
237 .name = "XScale",
238 .fifo_size = 32,
239 .tx_loadsz = 32,
240 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
Stephen Warren4539c242011-05-17 16:12:36 -0600241 .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 },
Thomas Koellerbd71c182007-05-06 14:48:47 -0700243 [PORT_RM9000] = {
244 .name = "RM9000",
245 .fifo_size = 16,
246 .tx_loadsz = 16,
247 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
248 .flags = UART_CAP_FIFO,
249 },
David Daney6b06f192009-01-02 13:50:00 +0000250 [PORT_OCTEON] = {
251 .name = "OCTEON",
252 .fifo_size = 64,
253 .tx_loadsz = 64,
254 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
255 .flags = UART_CAP_FIFO,
256 },
Florian Fainelli08e09922009-06-11 13:21:24 +0100257 [PORT_AR7] = {
258 .name = "AR7",
259 .fifo_size = 16,
260 .tx_loadsz = 16,
261 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
262 .flags = UART_CAP_FIFO | UART_CAP_AFE,
263 },
Philippe Langlais235dae52010-07-29 17:13:57 +0200264 [PORT_U6_16550A] = {
265 .name = "U6_16550A",
266 .fifo_size = 64,
267 .tx_loadsz = 64,
268 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
269 .flags = UART_CAP_FIFO | UART_CAP_AFE,
270 },
Stephen Warren4539c242011-05-17 16:12:36 -0600271 [PORT_TEGRA] = {
272 .name = "Tegra",
273 .fifo_size = 32,
274 .tx_loadsz = 8,
275 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
276 UART_FCR_T_TRIG_01,
277 .flags = UART_CAP_FIFO | UART_CAP_RTOIE,
278 },
Søren Holm06315342011-09-02 22:55:37 +0200279 [PORT_XR17D15X] = {
280 .name = "XR17D15X",
281 .fifo_size = 64,
282 .tx_loadsz = 64,
283 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
284 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR,
285 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286};
287
Manuel Lauss12bf3f22010-07-15 21:45:05 +0200288#if defined(CONFIG_MIPS_ALCHEMY)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000289
290/* Au1x00 UART hardware has a weird register layout */
291static const u8 au_io_in_map[] = {
292 [UART_RX] = 0,
293 [UART_IER] = 2,
294 [UART_IIR] = 3,
295 [UART_LCR] = 5,
296 [UART_MCR] = 6,
297 [UART_LSR] = 7,
298 [UART_MSR] = 8,
299};
300
301static const u8 au_io_out_map[] = {
302 [UART_TX] = 1,
303 [UART_IER] = 2,
304 [UART_FCR] = 4,
305 [UART_LCR] = 5,
306 [UART_MCR] = 6,
307};
308
309/* sane hardware needs no mapping */
David Daney7d6a07d2009-01-02 13:49:47 +0000310static inline int map_8250_in_reg(struct uart_port *p, int offset)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000311{
David Daney7d6a07d2009-01-02 13:49:47 +0000312 if (p->iotype != UPIO_AU)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000313 return offset;
314 return au_io_in_map[offset];
315}
316
David Daney7d6a07d2009-01-02 13:49:47 +0000317static inline int map_8250_out_reg(struct uart_port *p, int offset)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000318{
David Daney7d6a07d2009-01-02 13:49:47 +0000319 if (p->iotype != UPIO_AU)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000320 return offset;
321 return au_io_out_map[offset];
322}
323
Alan Cox6f803cd2008-02-08 04:18:52 -0800324#elif defined(CONFIG_SERIAL_8250_RM9K)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700325
326static const u8
327 regmap_in[8] = {
328 [UART_RX] = 0x00,
329 [UART_IER] = 0x0c,
330 [UART_IIR] = 0x14,
331 [UART_LCR] = 0x1c,
332 [UART_MCR] = 0x20,
333 [UART_LSR] = 0x24,
334 [UART_MSR] = 0x28,
335 [UART_SCR] = 0x2c
336 },
337 regmap_out[8] = {
338 [UART_TX] = 0x04,
339 [UART_IER] = 0x0c,
340 [UART_FCR] = 0x18,
341 [UART_LCR] = 0x1c,
342 [UART_MCR] = 0x20,
343 [UART_LSR] = 0x24,
344 [UART_MSR] = 0x28,
345 [UART_SCR] = 0x2c
346 };
347
David Daney7d6a07d2009-01-02 13:49:47 +0000348static inline int map_8250_in_reg(struct uart_port *p, int offset)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700349{
David Daney7d6a07d2009-01-02 13:49:47 +0000350 if (p->iotype != UPIO_RM9000)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700351 return offset;
352 return regmap_in[offset];
353}
354
David Daney7d6a07d2009-01-02 13:49:47 +0000355static inline int map_8250_out_reg(struct uart_port *p, int offset)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700356{
David Daney7d6a07d2009-01-02 13:49:47 +0000357 if (p->iotype != UPIO_RM9000)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700358 return offset;
359 return regmap_out[offset];
360}
361
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000362#else
363
364/* sane hardware needs no mapping */
365#define map_8250_in_reg(up, offset) (offset)
366#define map_8250_out_reg(up, offset) (offset)
367
368#endif
369
David Daney7d6a07d2009-01-02 13:49:47 +0000370static unsigned int hub6_serial_in(struct uart_port *p, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
David Daney7d6a07d2009-01-02 13:49:47 +0000372 offset = map_8250_in_reg(p, offset) << p->regshift;
373 outb(p->hub6 - 1 + offset, p->iobase);
374 return inb(p->iobase + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
David Daney7d6a07d2009-01-02 13:49:47 +0000377static void hub6_serial_out(struct uart_port *p, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
David Daney7d6a07d2009-01-02 13:49:47 +0000379 offset = map_8250_out_reg(p, offset) << p->regshift;
380 outb(p->hub6 - 1 + offset, p->iobase);
381 outb(value, p->iobase + 1);
382}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
David Daney7d6a07d2009-01-02 13:49:47 +0000384static unsigned int mem_serial_in(struct uart_port *p, int offset)
385{
386 offset = map_8250_in_reg(p, offset) << p->regshift;
387 return readb(p->membase + offset);
388}
389
390static void mem_serial_out(struct uart_port *p, int offset, int value)
391{
392 offset = map_8250_out_reg(p, offset) << p->regshift;
393 writeb(value, p->membase + offset);
394}
395
396static void mem32_serial_out(struct uart_port *p, int offset, int value)
397{
398 offset = map_8250_out_reg(p, offset) << p->regshift;
399 writel(value, p->membase + offset);
400}
401
402static unsigned int mem32_serial_in(struct uart_port *p, int offset)
403{
404 offset = map_8250_in_reg(p, offset) << p->regshift;
405 return readl(p->membase + offset);
406}
407
David Daney7d6a07d2009-01-02 13:49:47 +0000408static unsigned int au_serial_in(struct uart_port *p, int offset)
409{
410 offset = map_8250_in_reg(p, offset) << p->regshift;
411 return __raw_readl(p->membase + offset);
412}
413
414static void au_serial_out(struct uart_port *p, int offset, int value)
415{
416 offset = map_8250_out_reg(p, offset) << p->regshift;
417 __raw_writel(value, p->membase + offset);
418}
David Daney7d6a07d2009-01-02 13:49:47 +0000419
David Daney7d6a07d2009-01-02 13:49:47 +0000420static unsigned int io_serial_in(struct uart_port *p, int offset)
421{
422 offset = map_8250_in_reg(p, offset) << p->regshift;
423 return inb(p->iobase + offset);
424}
425
426static void io_serial_out(struct uart_port *p, int offset, int value)
427{
428 offset = map_8250_out_reg(p, offset) << p->regshift;
429 outb(value, p->iobase + offset);
430}
431
Jamie Iles583d28e2011-08-15 10:17:52 +0100432static int serial8250_default_handle_irq(struct uart_port *port);
433
David Daney7d6a07d2009-01-02 13:49:47 +0000434static void set_io_from_upio(struct uart_port *p)
435{
Jamie Iles49d57412010-12-01 23:39:35 +0000436 struct uart_8250_port *up =
437 container_of(p, struct uart_8250_port, port);
David Daney7d6a07d2009-01-02 13:49:47 +0000438 switch (p->iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 case UPIO_HUB6:
David Daney7d6a07d2009-01-02 13:49:47 +0000440 p->serial_in = hub6_serial_in;
441 p->serial_out = hub6_serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 break;
443
444 case UPIO_MEM:
David Daney7d6a07d2009-01-02 13:49:47 +0000445 p->serial_in = mem_serial_in;
446 p->serial_out = mem_serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
448
Thomas Koellerbd71c182007-05-06 14:48:47 -0700449 case UPIO_RM9000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 case UPIO_MEM32:
David Daney7d6a07d2009-01-02 13:49:47 +0000451 p->serial_in = mem32_serial_in;
452 p->serial_out = mem32_serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
454
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000455 case UPIO_AU:
David Daney7d6a07d2009-01-02 13:49:47 +0000456 p->serial_in = au_serial_in;
457 p->serial_out = au_serial_out;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000458 break;
Manuel Lauss12bf3f22010-07-15 21:45:05 +0200459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 default:
David Daney7d6a07d2009-01-02 13:49:47 +0000461 p->serial_in = io_serial_in;
462 p->serial_out = io_serial_out;
463 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Alan Coxb8e7e402009-05-28 14:01:35 +0100465 /* Remember loaded iotype */
466 up->cur_iotype = p->iotype;
Jamie Iles583d28e2011-08-15 10:17:52 +0100467 p->handle_irq = serial8250_default_handle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Alex Williamson40b36da2007-02-14 00:33:04 -0800470static void
471serial_out_sync(struct uart_8250_port *up, int offset, int value)
472{
David Daney7d6a07d2009-01-02 13:49:47 +0000473 struct uart_port *p = &up->port;
474 switch (p->iotype) {
Alex Williamson40b36da2007-02-14 00:33:04 -0800475 case UPIO_MEM:
476 case UPIO_MEM32:
Alex Williamson40b36da2007-02-14 00:33:04 -0800477 case UPIO_AU:
David Daney7d6a07d2009-01-02 13:49:47 +0000478 p->serial_out(p, offset, value);
479 p->serial_in(p, UART_LCR); /* safe, no side-effects */
Alex Williamson40b36da2007-02-14 00:33:04 -0800480 break;
481 default:
David Daney7d6a07d2009-01-02 13:49:47 +0000482 p->serial_out(p, offset, value);
Alex Williamson40b36da2007-02-14 00:33:04 -0800483 }
484}
485
David Daney7d6a07d2009-01-02 13:49:47 +0000486#define serial_in(up, offset) \
487 (up->port.serial_in(&(up)->port, (offset)))
488#define serial_out(up, offset, value) \
489 (up->port.serial_out(&(up)->port, (offset), (value)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/*
491 * We used to support using pause I/O for certain machines. We
492 * haven't supported this for a while, but just in case it's badly
493 * needed for certain old 386 machines, I've left these #define's
494 * in....
495 */
496#define serial_inp(up, offset) serial_in(up, offset)
497#define serial_outp(up, offset, value) serial_out(up, offset, value)
498
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100499/* Uart divisor latch read */
500static inline int _serial_dl_read(struct uart_8250_port *up)
501{
502 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
503}
504
505/* Uart divisor latch write */
506static inline void _serial_dl_write(struct uart_8250_port *up, int value)
507{
508 serial_outp(up, UART_DLL, value & 0xff);
509 serial_outp(up, UART_DLM, value >> 8 & 0xff);
510}
511
Manuel Lauss12bf3f22010-07-15 21:45:05 +0200512#if defined(CONFIG_MIPS_ALCHEMY)
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100513/* Au1x00 haven't got a standard divisor latch */
514static int serial_dl_read(struct uart_8250_port *up)
515{
516 if (up->port.iotype == UPIO_AU)
517 return __raw_readl(up->port.membase + 0x28);
518 else
519 return _serial_dl_read(up);
520}
521
522static void serial_dl_write(struct uart_8250_port *up, int value)
523{
524 if (up->port.iotype == UPIO_AU)
525 __raw_writel(value, up->port.membase + 0x28);
526 else
527 _serial_dl_write(up, value);
528}
Alan Cox6f803cd2008-02-08 04:18:52 -0800529#elif defined(CONFIG_SERIAL_8250_RM9K)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700530static int serial_dl_read(struct uart_8250_port *up)
531{
532 return (up->port.iotype == UPIO_RM9000) ?
533 (((__raw_readl(up->port.membase + 0x10) << 8) |
534 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
535 _serial_dl_read(up);
536}
537
538static void serial_dl_write(struct uart_8250_port *up, int value)
539{
540 if (up->port.iotype == UPIO_RM9000) {
541 __raw_writel(value, up->port.membase + 0x08);
542 __raw_writel(value >> 8, up->port.membase + 0x10);
543 } else {
544 _serial_dl_write(up, value);
545 }
546}
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100547#else
548#define serial_dl_read(up) _serial_dl_read(up)
549#define serial_dl_write(up, value) _serial_dl_write(up, value)
550#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552/*
553 * For the 16C950
554 */
555static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
556{
557 serial_out(up, UART_SCR, offset);
558 serial_out(up, UART_ICR, value);
559}
560
561static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
562{
563 unsigned int value;
564
565 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
566 serial_out(up, UART_SCR, offset);
567 value = serial_in(up, UART_ICR);
568 serial_icr_write(up, UART_ACR, up->acr);
569
570 return value;
571}
572
573/*
574 * FIFO support.
575 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100576static void serial8250_clear_fifos(struct uart_8250_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 if (p->capabilities & UART_CAP_FIFO) {
579 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
580 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
581 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
582 serial_outp(p, UART_FCR, 0);
583 }
584}
585
586/*
587 * IER sleep support. UARTs which have EFRs need the "extended
588 * capability" bit enabled. Note that on XR16C850s, we need to
589 * reset LCR to write to IER.
590 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100591static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 if (p->capabilities & UART_CAP_SLEEP) {
594 if (p->capabilities & UART_CAP_EFR) {
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800595 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 serial_outp(p, UART_EFR, UART_EFR_ECB);
597 serial_outp(p, UART_LCR, 0);
598 }
599 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
600 if (p->capabilities & UART_CAP_EFR) {
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800601 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 serial_outp(p, UART_EFR, 0);
603 serial_outp(p, UART_LCR, 0);
604 }
605 }
606}
607
608#ifdef CONFIG_SERIAL_8250_RSA
609/*
610 * Attempts to turn on the RSA FIFO. Returns zero on failure.
611 * We set the port uart clock rate if we succeed.
612 */
613static int __enable_rsa(struct uart_8250_port *up)
614{
615 unsigned char mode;
616 int result;
617
618 mode = serial_inp(up, UART_RSA_MSR);
619 result = mode & UART_RSA_MSR_FIFO;
620
621 if (!result) {
622 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
623 mode = serial_inp(up, UART_RSA_MSR);
624 result = mode & UART_RSA_MSR_FIFO;
625 }
626
627 if (result)
628 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
629
630 return result;
631}
632
633static void enable_rsa(struct uart_8250_port *up)
634{
635 if (up->port.type == PORT_RSA) {
636 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
637 spin_lock_irq(&up->port.lock);
638 __enable_rsa(up);
639 spin_unlock_irq(&up->port.lock);
640 }
641 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
642 serial_outp(up, UART_RSA_FRR, 0);
643 }
644}
645
646/*
647 * Attempts to turn off the RSA FIFO. Returns zero on failure.
648 * It is unknown why interrupts were disabled in here. However,
649 * the caller is expected to preserve this behaviour by grabbing
650 * the spinlock before calling this function.
651 */
652static void disable_rsa(struct uart_8250_port *up)
653{
654 unsigned char mode;
655 int result;
656
657 if (up->port.type == PORT_RSA &&
658 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
659 spin_lock_irq(&up->port.lock);
660
661 mode = serial_inp(up, UART_RSA_MSR);
662 result = !(mode & UART_RSA_MSR_FIFO);
663
664 if (!result) {
665 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
666 mode = serial_inp(up, UART_RSA_MSR);
667 result = !(mode & UART_RSA_MSR_FIFO);
668 }
669
670 if (result)
671 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
672 spin_unlock_irq(&up->port.lock);
673 }
674}
675#endif /* CONFIG_SERIAL_8250_RSA */
676
677/*
678 * This is a quickie test to see how big the FIFO is.
679 * It doesn't work at all the time, more's the pity.
680 */
681static int size_fifo(struct uart_8250_port *up)
682{
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100683 unsigned char old_fcr, old_mcr, old_lcr;
684 unsigned short old_dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int count;
686
687 old_lcr = serial_inp(up, UART_LCR);
688 serial_outp(up, UART_LCR, 0);
689 old_fcr = serial_inp(up, UART_FCR);
690 old_mcr = serial_inp(up, UART_MCR);
691 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
692 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
693 serial_outp(up, UART_MCR, UART_MCR_LOOP);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800694 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100695 old_dl = serial_dl_read(up);
696 serial_dl_write(up, 0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 serial_outp(up, UART_LCR, 0x03);
698 for (count = 0; count < 256; count++)
699 serial_outp(up, UART_TX, count);
700 mdelay(20);/* FIXME - schedule_timeout */
701 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
702 (count < 256); count++)
703 serial_inp(up, UART_RX);
704 serial_outp(up, UART_FCR, old_fcr);
705 serial_outp(up, UART_MCR, old_mcr);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800706 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100707 serial_dl_write(up, old_dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 serial_outp(up, UART_LCR, old_lcr);
709
710 return count;
711}
712
713/*
714 * Read UART ID using the divisor method - set DLL and DLM to zero
715 * and the revision will be in DLL and device type in DLM. We
716 * preserve the device state across this.
717 */
718static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
719{
720 unsigned char old_dll, old_dlm, old_lcr;
721 unsigned int id;
722
723 old_lcr = serial_inp(p, UART_LCR);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800724 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 old_dll = serial_inp(p, UART_DLL);
727 old_dlm = serial_inp(p, UART_DLM);
728
729 serial_outp(p, UART_DLL, 0);
730 serial_outp(p, UART_DLM, 0);
731
732 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
733
734 serial_outp(p, UART_DLL, old_dll);
735 serial_outp(p, UART_DLM, old_dlm);
736 serial_outp(p, UART_LCR, old_lcr);
737
738 return id;
739}
740
741/*
742 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
743 * When this function is called we know it is at least a StarTech
744 * 16650 V2, but it might be one of several StarTech UARTs, or one of
745 * its clones. (We treat the broken original StarTech 16650 V1 as a
746 * 16550, and why not? Startech doesn't seem to even acknowledge its
747 * existence.)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700748 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * What evil have men's minds wrought...
750 */
751static void autoconfig_has_efr(struct uart_8250_port *up)
752{
753 unsigned int id1, id2, id3, rev;
754
755 /*
756 * Everything with an EFR has SLEEP
757 */
758 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
759
760 /*
761 * First we check to see if it's an Oxford Semiconductor UART.
762 *
763 * If we have to do this here because some non-National
764 * Semiconductor clone chips lock up if you try writing to the
765 * LSR register (which serial_icr_read does)
766 */
767
768 /*
769 * Check for Oxford Semiconductor 16C950.
770 *
771 * EFR [4] must be set else this test fails.
772 *
773 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
774 * claims that it's needed for 952 dual UART's (which are not
775 * recommended for new designs).
776 */
777 up->acr = 0;
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800778 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 serial_out(up, UART_EFR, UART_EFR_ECB);
780 serial_out(up, UART_LCR, 0x00);
781 id1 = serial_icr_read(up, UART_ID1);
782 id2 = serial_icr_read(up, UART_ID2);
783 id3 = serial_icr_read(up, UART_ID3);
784 rev = serial_icr_read(up, UART_REV);
785
786 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
787
788 if (id1 == 0x16 && id2 == 0xC9 &&
789 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
790 up->port.type = PORT_16C950;
Russell King4ba5e352005-06-23 10:43:04 +0100791
792 /*
793 * Enable work around for the Oxford Semiconductor 952 rev B
794 * chip which causes it to seriously miscalculate baud rates
795 * when DLL is 0.
796 */
797 if (id3 == 0x52 && rev == 0x01)
798 up->bugs |= UART_BUG_QUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return;
800 }
Thomas Koellerbd71c182007-05-06 14:48:47 -0700801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /*
803 * We check for a XR16C850 by setting DLL and DLM to 0, and then
804 * reading back DLL and DLM. The chip type depends on the DLM
805 * value read back:
806 * 0x10 - XR16C850 and the DLL contains the chip revision.
807 * 0x12 - XR16C2850.
808 * 0x14 - XR16C854.
809 */
810 id1 = autoconfig_read_divisor_id(up);
811 DEBUG_AUTOCONF("850id=%04x ", id1);
812
813 id2 = id1 >> 8;
814 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 up->port.type = PORT_16850;
816 return;
817 }
818
819 /*
820 * It wasn't an XR16C850.
821 *
822 * We distinguish between the '654 and the '650 by counting
823 * how many bytes are in the FIFO. I'm using this for now,
824 * since that's the technique that was sent to me in the
825 * serial driver update, but I'm not convinced this works.
826 * I've had problems doing this in the past. -TYT
827 */
828 if (size_fifo(up) == 64)
829 up->port.type = PORT_16654;
830 else
831 up->port.type = PORT_16650V2;
832}
833
834/*
835 * We detected a chip without a FIFO. Only two fall into
836 * this category - the original 8250 and the 16450. The
837 * 16450 has a scratch register (accessible with LCR=0)
838 */
839static void autoconfig_8250(struct uart_8250_port *up)
840{
841 unsigned char scratch, status1, status2;
842
843 up->port.type = PORT_8250;
844
845 scratch = serial_in(up, UART_SCR);
846 serial_outp(up, UART_SCR, 0xa5);
847 status1 = serial_in(up, UART_SCR);
848 serial_outp(up, UART_SCR, 0x5a);
849 status2 = serial_in(up, UART_SCR);
850 serial_outp(up, UART_SCR, scratch);
851
852 if (status1 == 0xa5 && status2 == 0x5a)
853 up->port.type = PORT_16450;
854}
855
856static int broken_efr(struct uart_8250_port *up)
857{
858 /*
859 * Exar ST16C2550 "A2" devices incorrectly detect as
860 * having an EFR, and report an ID of 0x0201. See
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200861 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 */
863 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
864 return 1;
865
866 return 0;
867}
868
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800869static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
870{
871 unsigned char status;
872
873 status = serial_in(up, 0x04); /* EXCR2 */
874#define PRESL(x) ((x) & 0x30)
875 if (PRESL(status) == 0x10) {
876 /* already in high speed mode */
877 return 0;
878 } else {
879 status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
880 status |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
881 serial_outp(up, 0x04, status);
882 }
883 return 1;
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/*
887 * We know that the chip has FIFOs. Does it have an EFR? The
888 * EFR is located in the same register position as the IIR and
889 * we know the top two bits of the IIR are currently set. The
890 * EFR should contain zero. Try to read the EFR.
891 */
892static void autoconfig_16550a(struct uart_8250_port *up)
893{
894 unsigned char status1, status2;
895 unsigned int iersave;
896
897 up->port.type = PORT_16550A;
898 up->capabilities |= UART_CAP_FIFO;
899
900 /*
901 * Check for presence of the EFR when DLAB is set.
902 * Only ST16C650V1 UARTs pass this test.
903 */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800904 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (serial_in(up, UART_EFR) == 0) {
906 serial_outp(up, UART_EFR, 0xA8);
907 if (serial_in(up, UART_EFR) != 0) {
908 DEBUG_AUTOCONF("EFRv1 ");
909 up->port.type = PORT_16650;
910 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
911 } else {
912 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
913 }
914 serial_outp(up, UART_EFR, 0);
915 return;
916 }
917
918 /*
919 * Maybe it requires 0xbf to be written to the LCR.
920 * (other ST16C650V2 UARTs, TI16C752A, etc)
921 */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800922 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
924 DEBUG_AUTOCONF("EFRv2 ");
925 autoconfig_has_efr(up);
926 return;
927 }
928
929 /*
930 * Check for a National Semiconductor SuperIO chip.
931 * Attempt to switch to bank 2, read the value of the LOOP bit
932 * from EXCR1. Switch back to bank 0, change it in MCR. Then
933 * switch back to bank 2, read it from EXCR1 again and check
934 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 */
936 serial_outp(up, UART_LCR, 0);
937 status1 = serial_in(up, UART_MCR);
938 serial_outp(up, UART_LCR, 0xE0);
939 status2 = serial_in(up, 0x02); /* EXCR1 */
940
941 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
942 serial_outp(up, UART_LCR, 0);
943 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
944 serial_outp(up, UART_LCR, 0xE0);
945 status2 = serial_in(up, 0x02); /* EXCR1 */
946 serial_outp(up, UART_LCR, 0);
947 serial_outp(up, UART_MCR, status1);
948
949 if ((status2 ^ status1) & UART_MCR_LOOP) {
David Woodhouse857dde22005-05-21 15:52:23 +0100950 unsigned short quot;
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 serial_outp(up, UART_LCR, 0xE0);
David Woodhouse857dde22005-05-21 15:52:23 +0100953
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100954 quot = serial_dl_read(up);
David Woodhouse857dde22005-05-21 15:52:23 +0100955 quot <<= 3;
956
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800957 if (ns16550a_goto_highspeed(up))
958 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
David Woodhouse857dde22005-05-21 15:52:23 +0100960 serial_outp(up, UART_LCR, 0);
961
962 up->port.uartclk = 921600*16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 up->port.type = PORT_NS16550A;
964 up->capabilities |= UART_NATSEMI;
965 return;
966 }
967 }
968
969 /*
970 * No EFR. Try to detect a TI16750, which only sets bit 5 of
971 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
972 * Try setting it with and without DLAB set. Cheap clones
973 * set bit 5 without DLAB set.
974 */
975 serial_outp(up, UART_LCR, 0);
976 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
977 status1 = serial_in(up, UART_IIR) >> 5;
978 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800979 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
981 status2 = serial_in(up, UART_IIR) >> 5;
982 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
983 serial_outp(up, UART_LCR, 0);
984
985 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
986
987 if (status1 == 6 && status2 == 7) {
988 up->port.type = PORT_16750;
989 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
990 return;
991 }
992
993 /*
994 * Try writing and reading the UART_IER_UUE bit (b6).
995 * If it works, this is probably one of the Xscale platform's
996 * internal UARTs.
997 * We're going to explicitly set the UUE bit to 0 before
998 * trying to write and read a 1 just to make sure it's not
999 * already a 1 and maybe locked there before we even start start.
1000 */
1001 iersave = serial_in(up, UART_IER);
1002 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
1003 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1004 /*
1005 * OK it's in a known zero state, try writing and reading
1006 * without disturbing the current state of the other bits.
1007 */
1008 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
1009 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1010 /*
1011 * It's an Xscale.
1012 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1013 */
1014 DEBUG_AUTOCONF("Xscale ");
1015 up->port.type = PORT_XSCALE;
Stephen Warren55681812011-06-17 09:45:07 -06001016 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return;
1018 }
1019 } else {
1020 /*
1021 * If we got here we couldn't force the IER_UUE bit to 0.
1022 * Log it and continue.
1023 */
1024 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1025 }
1026 serial_outp(up, UART_IER, iersave);
Philippe Langlais235dae52010-07-29 17:13:57 +02001027
1028 /*
Søren Holm06315342011-09-02 22:55:37 +02001029 * Exar uarts have EFR in a weird location
1030 */
1031 if (up->port.flags & UPF_EXAR_EFR) {
1032 up->port.type = PORT_XR17D15X;
1033 up->capabilities |= UART_CAP_AFE | UART_CAP_EFR;
1034 }
1035
1036 /*
Philippe Langlais235dae52010-07-29 17:13:57 +02001037 * We distinguish between 16550A and U6 16550A by counting
1038 * how many bytes are in the FIFO.
1039 */
1040 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1041 up->port.type = PORT_U6_16550A;
1042 up->capabilities |= UART_CAP_AFE;
1043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046/*
1047 * This routine is called by rs_init() to initialize a specific serial
1048 * port. It determines what type of UART chip this serial port is
1049 * using: 8250, 16450, 16550, 16550A. The important question is
1050 * whether or not this UART is a 16550A or not, since this will
1051 * determine whether or not we can use its FIFO features or not.
1052 */
1053static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
1054{
1055 unsigned char status1, scratch, scratch2, scratch3;
1056 unsigned char save_lcr, save_mcr;
1057 unsigned long flags;
1058
1059 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
1060 return;
1061
Lennert Buytenhek80647b92009-11-11 14:26:41 -08001062 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
David S. Miller84408382008-10-13 10:45:26 +01001063 serial_index(&up->port), up->port.iobase, up->port.membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 /*
1066 * We really do need global IRQs disabled here - we're going to
1067 * be frobbing the chips IRQ enable register to see if it exists.
1068 */
1069 spin_lock_irqsave(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 up->capabilities = 0;
Russell King4ba5e352005-06-23 10:43:04 +01001072 up->bugs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 if (!(up->port.flags & UPF_BUGGY_UART)) {
1075 /*
1076 * Do a simple existence test first; if we fail this,
1077 * there's no point trying anything else.
Thomas Koellerbd71c182007-05-06 14:48:47 -07001078 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 * 0x80 is used as a nonsense port to prevent against
1080 * false positives due to ISA bus float. The
1081 * assumption is that 0x80 is a non-existent port;
1082 * which should be safe since include/asm/io.h also
1083 * makes this assumption.
1084 *
1085 * Note: this is safe as long as MCR bit 4 is clear
1086 * and the device is in "PC" mode.
1087 */
1088 scratch = serial_inp(up, UART_IER);
1089 serial_outp(up, UART_IER, 0);
1090#ifdef __i386__
1091 outb(0xff, 0x080);
1092#endif
Thomas Hoehn48212002007-02-10 01:46:05 -08001093 /*
1094 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1095 * 16C754B) allow only to modify them if an EFR bit is set.
1096 */
1097 scratch2 = serial_inp(up, UART_IER) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 serial_outp(up, UART_IER, 0x0F);
1099#ifdef __i386__
1100 outb(0, 0x080);
1101#endif
Thomas Hoehn48212002007-02-10 01:46:05 -08001102 scratch3 = serial_inp(up, UART_IER) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 serial_outp(up, UART_IER, scratch);
1104 if (scratch2 != 0 || scratch3 != 0x0F) {
1105 /*
1106 * We failed; there's nothing here
1107 */
1108 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1109 scratch2, scratch3);
1110 goto out;
1111 }
1112 }
1113
1114 save_mcr = serial_in(up, UART_MCR);
1115 save_lcr = serial_in(up, UART_LCR);
1116
Thomas Koellerbd71c182007-05-06 14:48:47 -07001117 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 * Check to see if a UART is really there. Certain broken
1119 * internal modems based on the Rockwell chipset fail this
1120 * test, because they apparently don't implement the loopback
1121 * test mode. So this test is skipped on the COM 1 through
1122 * COM 4 ports. This *should* be safe, since no board
1123 * manufacturer would be stupid enough to design a board
1124 * that conflicts with COM 1-4 --- we hope!
1125 */
1126 if (!(up->port.flags & UPF_SKIP_TEST)) {
1127 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1128 status1 = serial_inp(up, UART_MSR) & 0xF0;
1129 serial_outp(up, UART_MCR, save_mcr);
1130 if (status1 != 0x90) {
1131 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1132 status1);
1133 goto out;
1134 }
1135 }
1136
1137 /*
1138 * We're pretty sure there's a port here. Lets find out what
1139 * type of port it is. The IIR top two bits allows us to find
Russell King6f0d6182005-09-09 16:17:58 +01001140 * out if it's 8250 or 16450, 16550, 16550A or later. This
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 * determines what we test for next.
1142 *
1143 * We also initialise the EFR (if any) to zero for later. The
1144 * EFR occupies the same register location as the FCR and IIR.
1145 */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001146 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 serial_outp(up, UART_EFR, 0);
1148 serial_outp(up, UART_LCR, 0);
1149
1150 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1151 scratch = serial_in(up, UART_IIR) >> 6;
1152
1153 DEBUG_AUTOCONF("iir=%d ", scratch);
1154
1155 switch (scratch) {
1156 case 0:
1157 autoconfig_8250(up);
1158 break;
1159 case 1:
1160 up->port.type = PORT_UNKNOWN;
1161 break;
1162 case 2:
1163 up->port.type = PORT_16550;
1164 break;
1165 case 3:
1166 autoconfig_16550a(up);
1167 break;
1168 }
1169
1170#ifdef CONFIG_SERIAL_8250_RSA
1171 /*
1172 * Only probe for RSA ports if we got the region.
1173 */
1174 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1175 int i;
1176
1177 for (i = 0 ; i < probe_rsa_count; ++i) {
1178 if (probe_rsa[i] == up->port.iobase &&
1179 __enable_rsa(up)) {
1180 up->port.type = PORT_RSA;
1181 break;
1182 }
1183 }
1184 }
1185#endif
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 serial_outp(up, UART_LCR, save_lcr);
1188
1189 if (up->capabilities != uart_config[up->port.type].flags) {
1190 printk(KERN_WARNING
1191 "ttyS%d: detected caps %08x should be %08x\n",
David S. Miller84408382008-10-13 10:45:26 +01001192 serial_index(&up->port), up->capabilities,
1193 uart_config[up->port.type].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195
1196 up->port.fifosize = uart_config[up->port.type].fifo_size;
1197 up->capabilities = uart_config[up->port.type].flags;
1198 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1199
1200 if (up->port.type == PORT_UNKNOWN)
1201 goto out;
1202
1203 /*
1204 * Reset the UART.
1205 */
1206#ifdef CONFIG_SERIAL_8250_RSA
1207 if (up->port.type == PORT_RSA)
1208 serial_outp(up, UART_RSA_FRR, 0);
1209#endif
1210 serial_outp(up, UART_MCR, save_mcr);
1211 serial8250_clear_fifos(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001212 serial_in(up, UART_RX);
Lennert Buytenhek5c8c7552005-11-12 21:58:05 +00001213 if (up->capabilities & UART_CAP_UUE)
1214 serial_outp(up, UART_IER, UART_IER_UUE);
1215 else
1216 serial_outp(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Thomas Koellerbd71c182007-05-06 14:48:47 -07001218 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 spin_unlock_irqrestore(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1221}
1222
1223static void autoconfig_irq(struct uart_8250_port *up)
1224{
1225 unsigned char save_mcr, save_ier;
1226 unsigned char save_ICP = 0;
1227 unsigned int ICP = 0;
1228 unsigned long irqs;
1229 int irq;
1230
1231 if (up->port.flags & UPF_FOURPORT) {
1232 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1233 save_ICP = inb_p(ICP);
1234 outb_p(0x80, ICP);
1235 (void) inb_p(ICP);
1236 }
1237
1238 /* forget possible initially masked and pending IRQ */
1239 probe_irq_off(probe_irq_on());
1240 save_mcr = serial_inp(up, UART_MCR);
1241 save_ier = serial_inp(up, UART_IER);
1242 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
Thomas Koellerbd71c182007-05-06 14:48:47 -07001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 irqs = probe_irq_on();
1245 serial_outp(up, UART_MCR, 0);
Alan Cox6f803cd2008-02-08 04:18:52 -08001246 udelay(10);
1247 if (up->port.flags & UPF_FOURPORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 serial_outp(up, UART_MCR,
1249 UART_MCR_DTR | UART_MCR_RTS);
1250 } else {
1251 serial_outp(up, UART_MCR,
1252 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1253 }
1254 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1255 (void)serial_inp(up, UART_LSR);
1256 (void)serial_inp(up, UART_RX);
1257 (void)serial_inp(up, UART_IIR);
1258 (void)serial_inp(up, UART_MSR);
1259 serial_outp(up, UART_TX, 0xFF);
Alan Cox6f803cd2008-02-08 04:18:52 -08001260 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 irq = probe_irq_off(irqs);
1262
1263 serial_outp(up, UART_MCR, save_mcr);
1264 serial_outp(up, UART_IER, save_ier);
1265
1266 if (up->port.flags & UPF_FOURPORT)
1267 outb_p(save_ICP, ICP);
1268
1269 up->port.irq = (irq > 0) ? irq : 0;
1270}
1271
Russell Kinge763b902005-06-29 18:41:51 +01001272static inline void __stop_tx(struct uart_8250_port *p)
1273{
1274 if (p->ier & UART_IER_THRI) {
1275 p->ier &= ~UART_IER_THRI;
1276 serial_out(p, UART_IER, p->ier);
1277 }
1278}
1279
Russell Kingb129a8c2005-08-31 10:12:14 +01001280static void serial8250_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Jamie Iles49d57412010-12-01 23:39:35 +00001282 struct uart_8250_port *up =
1283 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Russell Kinge763b902005-06-29 18:41:51 +01001285 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 /*
Russell Kinge763b902005-06-29 18:41:51 +01001288 * We really want to stop the transmitter from sending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 */
Russell Kinge763b902005-06-29 18:41:51 +01001290 if (up->port.type == PORT_16C950) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 up->acr |= UART_ACR_TXDIS;
1292 serial_icr_write(up, UART_ACR, up->acr);
1293 }
1294}
1295
Russell Kingb129a8c2005-08-31 10:12:14 +01001296static void serial8250_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Jamie Iles49d57412010-12-01 23:39:35 +00001298 struct uart_8250_port *up =
1299 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 if (!(up->ier & UART_IER_THRI)) {
1302 up->ier |= UART_IER_THRI;
1303 serial_out(up, UART_IER, up->ier);
Russell King55d3b282005-06-23 15:05:41 +01001304
Russell King67f76542005-06-23 22:26:43 +01001305 if (up->bugs & UART_BUG_TXEN) {
Ian Jackson68cb4f82009-11-18 11:08:11 +01001306 unsigned char lsr;
Russell King55d3b282005-06-23 15:05:41 +01001307 lsr = serial_in(up, UART_LSR);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001308 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Thomas Koellerbd71c182007-05-06 14:48:47 -07001309 if ((up->port.type == PORT_RM9000) ?
Ian Jackson68cb4f82009-11-18 11:08:11 +01001310 (lsr & UART_LSR_THRE) :
1311 (lsr & UART_LSR_TEMT))
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001312 serial8250_tx_chars(up);
Russell King55d3b282005-06-23 15:05:41 +01001313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
Russell Kinge763b902005-06-29 18:41:51 +01001315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 /*
Russell Kinge763b902005-06-29 18:41:51 +01001317 * Re-enable the transmitter if we disabled it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 */
Russell Kinge763b902005-06-29 18:41:51 +01001319 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 up->acr &= ~UART_ACR_TXDIS;
1321 serial_icr_write(up, UART_ACR, up->acr);
1322 }
1323}
1324
1325static void serial8250_stop_rx(struct uart_port *port)
1326{
Jamie Iles49d57412010-12-01 23:39:35 +00001327 struct uart_8250_port *up =
1328 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 up->ier &= ~UART_IER_RLSI;
1331 up->port.read_status_mask &= ~UART_LSR_DR;
1332 serial_out(up, UART_IER, up->ier);
1333}
1334
1335static void serial8250_enable_ms(struct uart_port *port)
1336{
Jamie Iles49d57412010-12-01 23:39:35 +00001337 struct uart_8250_port *up =
1338 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001340 /* no MSR capabilities */
1341 if (up->bugs & UART_BUG_NOMSR)
1342 return;
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 up->ier |= UART_IER_MSI;
1345 serial_out(up, UART_IER, up->ier);
1346}
1347
Stephen Warren5f873ba2011-05-17 16:12:37 -06001348/*
1349 * Clear the Tegra rx fifo after a break
1350 *
1351 * FIXME: This needs to become a port specific callback once we have a
1352 * framework for this
1353 */
1354static void clear_rx_fifo(struct uart_8250_port *up)
1355{
1356 unsigned int status, tmout = 10000;
1357 do {
1358 status = serial_in(up, UART_LSR);
1359 if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
1360 status = serial_in(up, UART_RX);
1361 else
1362 break;
1363 if (--tmout == 0)
1364 break;
1365 udelay(1);
1366 } while (1);
1367}
1368
Paul Gortmaker0690f412011-12-04 18:42:19 -05001369/*
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001370 * serial8250_rx_chars: processes according to the passed in LSR
Paul Gortmaker0690f412011-12-04 18:42:19 -05001371 * value, and returns the remaining LSR bits not handled
1372 * by this Rx routine.
1373 */
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001374unsigned char
1375serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001377 struct tty_struct *tty = up->port.state->port.tty;
Paul Gortmaker0690f412011-12-04 18:42:19 -05001378 unsigned char ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 int max_count = 256;
1380 char flag;
1381
1382 do {
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001383 if (likely(lsr & UART_LSR_DR))
1384 ch = serial_inp(up, UART_RX);
1385 else
1386 /*
1387 * Intel 82571 has a Serial Over Lan device that will
1388 * set UART_LSR_BI without setting UART_LSR_DR when
1389 * it receives a break. To avoid reading from the
1390 * receive buffer without UART_LSR_DR bit set, we
1391 * just force the read character to be 0
1392 */
1393 ch = 0;
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 flag = TTY_NORMAL;
1396 up->port.icount.rx++;
1397
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001398 lsr |= up->lsr_saved_flags;
1399 up->lsr_saved_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001401 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /*
1403 * For statistics only
1404 */
1405 if (lsr & UART_LSR_BI) {
1406 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1407 up->port.icount.brk++;
1408 /*
Stephen Warren5f873ba2011-05-17 16:12:37 -06001409 * If tegra port then clear the rx fifo to
1410 * accept another break/character.
1411 */
1412 if (up->port.type == PORT_TEGRA)
1413 clear_rx_fifo(up);
1414
1415 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * We do the SysRQ and SAK checking
1417 * here because otherwise the break
1418 * may get masked by ignore_status_mask
1419 * or read_status_mask.
1420 */
1421 if (uart_handle_break(&up->port))
1422 goto ignore_char;
1423 } else if (lsr & UART_LSR_PE)
1424 up->port.icount.parity++;
1425 else if (lsr & UART_LSR_FE)
1426 up->port.icount.frame++;
1427 if (lsr & UART_LSR_OE)
1428 up->port.icount.overrun++;
1429
1430 /*
Russell King23907eb2005-04-16 15:26:39 -07001431 * Mask off conditions which should be ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
1433 lsr &= up->port.read_status_mask;
1434
1435 if (lsr & UART_LSR_BI) {
1436 DEBUG_INTR("handling break....");
1437 flag = TTY_BREAK;
1438 } else if (lsr & UART_LSR_PE)
1439 flag = TTY_PARITY;
1440 else if (lsr & UART_LSR_FE)
1441 flag = TTY_FRAME;
1442 }
David Howells7d12e782006-10-05 14:55:46 +01001443 if (uart_handle_sysrq_char(&up->port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 goto ignore_char;
Russell King05ab3012005-05-09 23:21:59 +01001445
1446 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1447
Alan Cox6f803cd2008-02-08 04:18:52 -08001448ignore_char:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 lsr = serial_inp(up, UART_LSR);
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001450 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 spin_unlock(&up->port.lock);
1452 tty_flip_buffer_push(tty);
1453 spin_lock(&up->port.lock);
Paul Gortmaker0690f412011-12-04 18:42:19 -05001454 return lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001456EXPORT_SYMBOL_GPL(serial8250_rx_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001458void serial8250_tx_chars(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001460 struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 int count;
1462
1463 if (up->port.x_char) {
1464 serial_outp(up, UART_TX, up->port.x_char);
1465 up->port.icount.tx++;
1466 up->port.x_char = 0;
1467 return;
1468 }
Russell Kingb129a8c2005-08-31 10:12:14 +01001469 if (uart_tx_stopped(&up->port)) {
1470 serial8250_stop_tx(&up->port);
1471 return;
1472 }
1473 if (uart_circ_empty(xmit)) {
Russell Kinge763b902005-06-29 18:41:51 +01001474 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return;
1476 }
1477
1478 count = up->tx_loadsz;
1479 do {
1480 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1481 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1482 up->port.icount.tx++;
1483 if (uart_circ_empty(xmit))
1484 break;
1485 } while (--count > 0);
1486
1487 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1488 uart_write_wakeup(&up->port);
1489
1490 DEBUG_INTR("THRE...");
1491
1492 if (uart_circ_empty(xmit))
Russell Kinge763b902005-06-29 18:41:51 +01001493 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001495EXPORT_SYMBOL_GPL(serial8250_tx_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001497unsigned int serial8250_modem_status(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Russell King2af7cd62006-01-04 16:55:09 +00001499 unsigned int status = serial_in(up, UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001501 status |= up->msr_saved_flags;
1502 up->msr_saved_flags = 0;
Taku Izumifdc30b32007-04-23 14:41:00 -07001503 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
Alan Coxebd2c8f2009-09-19 13:13:28 -07001504 up->port.state != NULL) {
Russell King2af7cd62006-01-04 16:55:09 +00001505 if (status & UART_MSR_TERI)
1506 up->port.icount.rng++;
1507 if (status & UART_MSR_DDSR)
1508 up->port.icount.dsr++;
1509 if (status & UART_MSR_DDCD)
1510 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1511 if (status & UART_MSR_DCTS)
1512 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Alan Coxbdc04e32009-09-19 13:13:31 -07001514 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
Russell King2af7cd62006-01-04 16:55:09 +00001515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Russell King2af7cd62006-01-04 16:55:09 +00001517 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001519EXPORT_SYMBOL_GPL(serial8250_modem_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521/*
1522 * This handles the interrupt from one port.
1523 */
Paul Gortmaker86b21192011-12-04 18:42:22 -05001524int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Paul Gortmaker0690f412011-12-04 18:42:19 -05001526 unsigned char status;
Jiri Kosina4bf36312007-04-23 14:41:21 -07001527 unsigned long flags;
Paul Gortmaker86b21192011-12-04 18:42:22 -05001528 struct uart_8250_port *up =
1529 container_of(port, struct uart_8250_port, port);
1530
1531 if (iir & UART_IIR_NO_INT)
1532 return 0;
Russell King45e24602006-01-04 19:19:06 +00001533
Jiri Kosina4bf36312007-04-23 14:41:21 -07001534 spin_lock_irqsave(&up->port.lock, flags);
Russell King45e24602006-01-04 19:19:06 +00001535
1536 status = serial_inp(up, UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 DEBUG_INTR("status = %x...", status);
1539
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001540 if (status & (UART_LSR_DR | UART_LSR_BI))
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001541 status = serial8250_rx_chars(up, status);
1542 serial8250_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (status & UART_LSR_THRE)
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001544 serial8250_tx_chars(up);
Russell King45e24602006-01-04 19:19:06 +00001545
Jiri Kosina4bf36312007-04-23 14:41:21 -07001546 spin_unlock_irqrestore(&up->port.lock, flags);
Paul Gortmaker86b21192011-12-04 18:42:22 -05001547 return 1;
Jamie Iles583d28e2011-08-15 10:17:52 +01001548}
Jamie Ilesc7a1bdc2011-08-26 19:04:49 +01001549EXPORT_SYMBOL_GPL(serial8250_handle_irq);
Jamie Iles583d28e2011-08-15 10:17:52 +01001550
1551static int serial8250_default_handle_irq(struct uart_port *port)
1552{
1553 struct uart_8250_port *up =
1554 container_of(port, struct uart_8250_port, port);
1555 unsigned int iir = serial_in(up, UART_IIR);
1556
1557 return serial8250_handle_irq(port, iir);
1558}
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560/*
1561 * This is the serial driver's interrupt routine.
1562 *
1563 * Arjan thinks the old way was overly complex, so it got simplified.
1564 * Alan disagrees, saying that need the complexity to handle the weird
1565 * nature of ISA shared interrupts. (This is a special exception.)
1566 *
1567 * In order to handle ISA shared interrupts properly, we need to check
1568 * that all ports have been serviced, and therefore the ISA interrupt
1569 * line has been de-asserted.
1570 *
1571 * This means we need to loop through all ports. checking that they
1572 * don't have an interrupt pending.
1573 */
David Howells7d12e782006-10-05 14:55:46 +01001574static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 struct irq_info *i = dev_id;
1577 struct list_head *l, *end = NULL;
1578 int pass_counter = 0, handled = 0;
1579
1580 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1581
1582 spin_lock(&i->lock);
1583
1584 l = i->head;
1585 do {
1586 struct uart_8250_port *up;
Jamie Iles583d28e2011-08-15 10:17:52 +01001587 struct uart_port *port;
Dan Williams448ac152011-11-22 13:41:24 -08001588 bool skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 up = list_entry(l, struct uart_8250_port, list);
Jamie Iles583d28e2011-08-15 10:17:52 +01001591 port = &up->port;
Dan Williams448ac152011-11-22 13:41:24 -08001592 skip = pass_counter && up->port.flags & UPF_IIR_ONCE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Dan Williams448ac152011-11-22 13:41:24 -08001594 if (!skip && port->handle_irq(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 handled = 1;
Marc St-Jeanbeab6972007-05-06 14:48:45 -07001596 end = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 } else if (end == NULL)
1598 end = l;
1599
1600 l = l->next;
1601
1602 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1603 /* If we hit this, we're dead. */
Daniel Drakecd3ecad2010-10-20 16:00:48 -07001604 printk_ratelimited(KERN_ERR
1605 "serial8250: too much work for irq%d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 break;
1607 }
1608 } while (l != end);
1609
1610 spin_unlock(&i->lock);
1611
1612 DEBUG_INTR("end.\n");
1613
1614 return IRQ_RETVAL(handled);
1615}
1616
1617/*
1618 * To support ISA shared interrupts, we need to have one interrupt
1619 * handler that ensures that the IRQ line has been deasserted
1620 * before returning. Failing to do this will result in the IRQ
1621 * line being stuck active, and, since ISA irqs are edge triggered,
1622 * no more IRQs will be seen.
1623 */
1624static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1625{
1626 spin_lock_irq(&i->lock);
1627
1628 if (!list_empty(i->head)) {
1629 if (i->head == &up->list)
1630 i->head = i->head->next;
1631 list_del(&up->list);
1632 } else {
1633 BUG_ON(i->head != &up->list);
1634 i->head = NULL;
1635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 spin_unlock_irq(&i->lock);
Alan Cox25db8ad2008-08-19 20:49:40 -07001637 /* List empty so throw away the hash node */
1638 if (i->head == NULL) {
1639 hlist_del(&i->node);
1640 kfree(i);
1641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
1644static int serial_link_irq_chain(struct uart_8250_port *up)
1645{
Alan Cox25db8ad2008-08-19 20:49:40 -07001646 struct hlist_head *h;
1647 struct hlist_node *n;
1648 struct irq_info *i;
Thomas Gleixner40663cc2006-07-01 19:29:43 -07001649 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Alan Cox25db8ad2008-08-19 20:49:40 -07001651 mutex_lock(&hash_mutex);
1652
1653 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1654
1655 hlist_for_each(n, h) {
1656 i = hlist_entry(n, struct irq_info, node);
1657 if (i->irq == up->port.irq)
1658 break;
1659 }
1660
1661 if (n == NULL) {
1662 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
1663 if (i == NULL) {
1664 mutex_unlock(&hash_mutex);
1665 return -ENOMEM;
1666 }
1667 spin_lock_init(&i->lock);
1668 i->irq = up->port.irq;
1669 hlist_add_head(&i->node, h);
1670 }
1671 mutex_unlock(&hash_mutex);
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 spin_lock_irq(&i->lock);
1674
1675 if (i->head) {
1676 list_add(&up->list, i->head);
1677 spin_unlock_irq(&i->lock);
1678
1679 ret = 0;
1680 } else {
1681 INIT_LIST_HEAD(&up->list);
1682 i->head = &up->list;
1683 spin_unlock_irq(&i->lock);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07001684 irq_flags |= up->port.irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 ret = request_irq(up->port.irq, serial8250_interrupt,
1686 irq_flags, "serial", i);
1687 if (ret < 0)
1688 serial_do_unlink(i, up);
1689 }
1690
1691 return ret;
1692}
1693
1694static void serial_unlink_irq_chain(struct uart_8250_port *up)
1695{
Alan Cox25db8ad2008-08-19 20:49:40 -07001696 struct irq_info *i;
1697 struct hlist_node *n;
1698 struct hlist_head *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Alan Cox25db8ad2008-08-19 20:49:40 -07001700 mutex_lock(&hash_mutex);
1701
1702 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1703
1704 hlist_for_each(n, h) {
1705 i = hlist_entry(n, struct irq_info, node);
1706 if (i->irq == up->port.irq)
1707 break;
1708 }
1709
1710 BUG_ON(n == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 BUG_ON(i->head == NULL);
1712
1713 if (list_empty(i->head))
1714 free_irq(up->port.irq, i);
1715
1716 serial_do_unlink(i, up);
Alan Cox25db8ad2008-08-19 20:49:40 -07001717 mutex_unlock(&hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
1719
1720/*
1721 * This function is used to handle ports that do not have an
1722 * interrupt. This doesn't work very well for 16450's, but gives
1723 * barely passable results for a 16550A. (Although at the expense
1724 * of much CPU overhead).
1725 */
1726static void serial8250_timeout(unsigned long data)
1727{
1728 struct uart_8250_port *up = (struct uart_8250_port *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Paul Gortmakera0431472011-12-04 18:42:21 -05001730 up->port.handle_irq(&up->port);
Anton Vorontsov54381062010-10-01 17:21:25 +04001731 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
Alex Williamson40b36da2007-02-14 00:33:04 -08001732}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Alex Williamson40b36da2007-02-14 00:33:04 -08001734static void serial8250_backup_timeout(unsigned long data)
1735{
1736 struct uart_8250_port *up = (struct uart_8250_port *)data;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001737 unsigned int iir, ier = 0, lsr;
1738 unsigned long flags;
Alex Williamson40b36da2007-02-14 00:33:04 -08001739
Al Cooperdbb3b1c2011-07-25 16:19:52 -04001740 spin_lock_irqsave(&up->port.lock, flags);
1741
Alex Williamson40b36da2007-02-14 00:33:04 -08001742 /*
1743 * Must disable interrupts or else we risk racing with the interrupt
1744 * based handler.
1745 */
Alan Coxd4e33fa2012-01-26 17:44:09 +00001746 if (up->port.irq) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001747 ier = serial_in(up, UART_IER);
1748 serial_out(up, UART_IER, 0);
1749 }
1750
1751 iir = serial_in(up, UART_IIR);
1752
1753 /*
1754 * This should be a safe test for anyone who doesn't trust the
1755 * IIR bits on their UART, but it's specifically designed for
1756 * the "Diva" UART used on the management processor on many HP
1757 * ia64 and parisc boxes.
1758 */
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001759 lsr = serial_in(up, UART_LSR);
1760 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -08001761 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
Alan Coxebd2c8f2009-09-19 13:13:28 -07001762 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001763 (lsr & UART_LSR_THRE)) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001764 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1765 iir |= UART_IIR_THRI;
1766 }
1767
1768 if (!(iir & UART_IIR_NO_INT))
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001769 serial8250_tx_chars(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001770
Alan Coxd4e33fa2012-01-26 17:44:09 +00001771 if (up->port.irq)
Alex Williamson40b36da2007-02-14 00:33:04 -08001772 serial_out(up, UART_IER, ier);
1773
Al Cooperdbb3b1c2011-07-25 16:19:52 -04001774 spin_unlock_irqrestore(&up->port.lock, flags);
1775
Alex Williamson40b36da2007-02-14 00:33:04 -08001776 /* Standard timer interval plus 0.2s to keep the port running */
Alan Cox6f803cd2008-02-08 04:18:52 -08001777 mod_timer(&up->timer,
Anton Vorontsov54381062010-10-01 17:21:25 +04001778 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
1781static unsigned int serial8250_tx_empty(struct uart_port *port)
1782{
Jamie Iles49d57412010-12-01 23:39:35 +00001783 struct uart_8250_port *up =
1784 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 unsigned long flags;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001786 unsigned int lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 spin_lock_irqsave(&up->port.lock, flags);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001789 lsr = serial_in(up, UART_LSR);
1790 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 spin_unlock_irqrestore(&up->port.lock, flags);
1792
Dick Hollenbeckbca47612009-12-09 12:31:34 -08001793 return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
1796static unsigned int serial8250_get_mctrl(struct uart_port *port)
1797{
Jamie Iles49d57412010-12-01 23:39:35 +00001798 struct uart_8250_port *up =
1799 container_of(port, struct uart_8250_port, port);
Russell King2af7cd62006-01-04 16:55:09 +00001800 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 unsigned int ret;
1802
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001803 status = serial8250_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 ret = 0;
1806 if (status & UART_MSR_DCD)
1807 ret |= TIOCM_CAR;
1808 if (status & UART_MSR_RI)
1809 ret |= TIOCM_RNG;
1810 if (status & UART_MSR_DSR)
1811 ret |= TIOCM_DSR;
1812 if (status & UART_MSR_CTS)
1813 ret |= TIOCM_CTS;
1814 return ret;
1815}
1816
1817static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1818{
Jamie Iles49d57412010-12-01 23:39:35 +00001819 struct uart_8250_port *up =
1820 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 unsigned char mcr = 0;
1822
1823 if (mctrl & TIOCM_RTS)
1824 mcr |= UART_MCR_RTS;
1825 if (mctrl & TIOCM_DTR)
1826 mcr |= UART_MCR_DTR;
1827 if (mctrl & TIOCM_OUT1)
1828 mcr |= UART_MCR_OUT1;
1829 if (mctrl & TIOCM_OUT2)
1830 mcr |= UART_MCR_OUT2;
1831 if (mctrl & TIOCM_LOOP)
1832 mcr |= UART_MCR_LOOP;
1833
1834 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1835
1836 serial_out(up, UART_MCR, mcr);
1837}
1838
1839static void serial8250_break_ctl(struct uart_port *port, int break_state)
1840{
Jamie Iles49d57412010-12-01 23:39:35 +00001841 struct uart_8250_port *up =
1842 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 unsigned long flags;
1844
1845 spin_lock_irqsave(&up->port.lock, flags);
1846 if (break_state == -1)
1847 up->lcr |= UART_LCR_SBC;
1848 else
1849 up->lcr &= ~UART_LCR_SBC;
1850 serial_out(up, UART_LCR, up->lcr);
1851 spin_unlock_irqrestore(&up->port.lock, flags);
1852}
1853
Alex Williamson40b36da2007-02-14 00:33:04 -08001854/*
1855 * Wait for transmitter & holding register to empty
1856 */
Will Newtonb5d674a2008-10-13 10:36:21 +01001857static void wait_for_xmitr(struct uart_8250_port *up, int bits)
Alex Williamson40b36da2007-02-14 00:33:04 -08001858{
1859 unsigned int status, tmout = 10000;
1860
1861 /* Wait up to 10ms for the character(s) to be sent. */
David Daney97d303b2010-10-05 11:40:07 -07001862 for (;;) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001863 status = serial_in(up, UART_LSR);
1864
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001865 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -08001866
David Daney97d303b2010-10-05 11:40:07 -07001867 if ((status & bits) == bits)
1868 break;
Alex Williamson40b36da2007-02-14 00:33:04 -08001869 if (--tmout == 0)
1870 break;
1871 udelay(1);
David Daney97d303b2010-10-05 11:40:07 -07001872 }
Alex Williamson40b36da2007-02-14 00:33:04 -08001873
1874 /* Wait up to 1s for flow control if necessary */
1875 if (up->port.flags & UPF_CONS_FLOW) {
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001876 unsigned int tmout;
1877 for (tmout = 1000000; tmout; tmout--) {
1878 unsigned int msr = serial_in(up, UART_MSR);
1879 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1880 if (msr & UART_MSR_CTS)
1881 break;
Alex Williamson40b36da2007-02-14 00:33:04 -08001882 udelay(1);
1883 touch_nmi_watchdog();
1884 }
1885 }
1886}
1887
Jason Wesself2d937f2008-04-17 20:05:37 +02001888#ifdef CONFIG_CONSOLE_POLL
1889/*
1890 * Console polling routines for writing and reading from the uart while
1891 * in an interrupt or debug context.
1892 */
1893
1894static int serial8250_get_poll_char(struct uart_port *port)
1895{
Jamie Iles49d57412010-12-01 23:39:35 +00001896 struct uart_8250_port *up =
1897 container_of(port, struct uart_8250_port, port);
Jason Wesself2d937f2008-04-17 20:05:37 +02001898 unsigned char lsr = serial_inp(up, UART_LSR);
1899
Jason Wesself5316b42010-05-20 21:04:22 -05001900 if (!(lsr & UART_LSR_DR))
1901 return NO_POLL_CHAR;
Jason Wesself2d937f2008-04-17 20:05:37 +02001902
1903 return serial_inp(up, UART_RX);
1904}
1905
1906
1907static void serial8250_put_poll_char(struct uart_port *port,
1908 unsigned char c)
1909{
1910 unsigned int ier;
Jamie Iles49d57412010-12-01 23:39:35 +00001911 struct uart_8250_port *up =
1912 container_of(port, struct uart_8250_port, port);
Jason Wesself2d937f2008-04-17 20:05:37 +02001913
1914 /*
1915 * First save the IER then disable the interrupts
1916 */
1917 ier = serial_in(up, UART_IER);
1918 if (up->capabilities & UART_CAP_UUE)
1919 serial_out(up, UART_IER, UART_IER_UUE);
1920 else
1921 serial_out(up, UART_IER, 0);
1922
1923 wait_for_xmitr(up, BOTH_EMPTY);
1924 /*
1925 * Send the character out.
1926 * If a LF, also do CR...
1927 */
1928 serial_out(up, UART_TX, c);
1929 if (c == 10) {
1930 wait_for_xmitr(up, BOTH_EMPTY);
1931 serial_out(up, UART_TX, 13);
1932 }
1933
1934 /*
1935 * Finally, wait for transmitter to become empty
1936 * and restore the IER
1937 */
1938 wait_for_xmitr(up, BOTH_EMPTY);
1939 serial_out(up, UART_IER, ier);
1940}
1941
1942#endif /* CONFIG_CONSOLE_POLL */
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944static int serial8250_startup(struct uart_port *port)
1945{
Jamie Iles49d57412010-12-01 23:39:35 +00001946 struct uart_8250_port *up =
1947 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 unsigned long flags;
Russell King55d3b282005-06-23 15:05:41 +01001949 unsigned char lsr, iir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 int retval;
1951
Ondrej Puzmane4f05af2010-12-04 21:17:38 +01001952 up->port.fifosize = uart_config[up->port.type].fifo_size;
1953 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 up->capabilities = uart_config[up->port.type].flags;
1955 up->mcr = 0;
1956
Alan Coxb8e7e402009-05-28 14:01:35 +01001957 if (up->port.iotype != up->cur_iotype)
1958 set_io_from_upio(port);
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (up->port.type == PORT_16C950) {
1961 /* Wake up and initialize UART */
1962 up->acr = 0;
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001963 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 serial_outp(up, UART_EFR, UART_EFR_ECB);
1965 serial_outp(up, UART_IER, 0);
1966 serial_outp(up, UART_LCR, 0);
1967 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
Wolfram Sang7d73aaf2011-12-09 18:07:13 +01001968 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 serial_outp(up, UART_EFR, UART_EFR_ECB);
1970 serial_outp(up, UART_LCR, 0);
1971 }
1972
1973#ifdef CONFIG_SERIAL_8250_RSA
1974 /*
1975 * If this is an RSA port, see if we can kick it up to the
1976 * higher speed clock.
1977 */
1978 enable_rsa(up);
1979#endif
1980
1981 /*
1982 * Clear the FIFO buffers and disable them.
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08001983 * (they will be reenabled in set_termios())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 */
1985 serial8250_clear_fifos(up);
1986
1987 /*
1988 * Clear the interrupt registers.
1989 */
1990 (void) serial_inp(up, UART_LSR);
1991 (void) serial_inp(up, UART_RX);
1992 (void) serial_inp(up, UART_IIR);
1993 (void) serial_inp(up, UART_MSR);
1994
1995 /*
1996 * At this point, there's no way the LSR could still be 0xff;
1997 * if it is, then bail out, because there's likely no UART
1998 * here.
1999 */
2000 if (!(up->port.flags & UPF_BUGGY_UART) &&
2001 (serial_inp(up, UART_LSR) == 0xff)) {
Konrad Rzeszutek Wilk7808a4c2011-09-26 09:14:34 -04002002 printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2003 serial_index(&up->port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return -ENODEV;
2005 }
2006
2007 /*
2008 * For a XR16C850, we need to set the trigger levels
2009 */
2010 if (up->port.type == PORT_16850) {
2011 unsigned char fctr;
2012
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08002013 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2016 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2017 serial_outp(up, UART_TRG, UART_TRG_96);
2018 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2019 serial_outp(up, UART_TRG, UART_TRG_96);
2020
2021 serial_outp(up, UART_LCR, 0);
2022 }
2023
Alan Coxd4e33fa2012-01-26 17:44:09 +00002024 if (up->port.irq) {
Alex Williamson01c194d2008-04-28 02:14:09 -07002025 unsigned char iir1;
Alex Williamson40b36da2007-02-14 00:33:04 -08002026 /*
2027 * Test for UARTs that do not reassert THRE when the
2028 * transmitter is idle and the interrupt has already
2029 * been cleared. Real 16550s should always reassert
2030 * this interrupt whenever the transmitter is idle and
2031 * the interrupt is enabled. Delays are necessary to
2032 * allow register changes to become visible.
2033 */
Borislav Petkovc389d272008-07-29 22:33:32 -07002034 spin_lock_irqsave(&up->port.lock, flags);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002035 if (up->port.irqflags & IRQF_SHARED)
Anton Vorontsov768aec02008-07-22 11:21:07 +01002036 disable_irq_nosync(up->port.irq);
Alex Williamson40b36da2007-02-14 00:33:04 -08002037
2038 wait_for_xmitr(up, UART_LSR_THRE);
2039 serial_out_sync(up, UART_IER, UART_IER_THRI);
2040 udelay(1); /* allow THRE to set */
Alex Williamson01c194d2008-04-28 02:14:09 -07002041 iir1 = serial_in(up, UART_IIR);
Alex Williamson40b36da2007-02-14 00:33:04 -08002042 serial_out(up, UART_IER, 0);
2043 serial_out_sync(up, UART_IER, UART_IER_THRI);
2044 udelay(1); /* allow a working UART time to re-assert THRE */
2045 iir = serial_in(up, UART_IIR);
2046 serial_out(up, UART_IER, 0);
2047
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002048 if (up->port.irqflags & IRQF_SHARED)
Anton Vorontsov768aec02008-07-22 11:21:07 +01002049 enable_irq(up->port.irq);
Borislav Petkovc389d272008-07-29 22:33:32 -07002050 spin_unlock_irqrestore(&up->port.lock, flags);
Alex Williamson40b36da2007-02-14 00:33:04 -08002051
2052 /*
2053 * If the interrupt is not reasserted, setup a timer to
2054 * kick the UART on a regular basis.
2055 */
Alex Williamson01c194d2008-04-28 02:14:09 -07002056 if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
Will Newton363f66f2008-09-02 14:35:44 -07002057 up->bugs |= UART_BUG_THRE;
David S. Miller84408382008-10-13 10:45:26 +01002058 pr_debug("ttyS%d - using backup timer\n",
2059 serial_index(port));
Alex Williamson40b36da2007-02-14 00:33:04 -08002060 }
2061 }
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 /*
Will Newton363f66f2008-09-02 14:35:44 -07002064 * The above check will only give an accurate result the first time
2065 * the port is opened so this value needs to be preserved.
2066 */
2067 if (up->bugs & UART_BUG_THRE) {
2068 up->timer.function = serial8250_backup_timeout;
2069 up->timer.data = (unsigned long)up;
2070 mod_timer(&up->timer, jiffies +
Anton Vorontsov54381062010-10-01 17:21:25 +04002071 uart_poll_timeout(port) + HZ / 5);
Will Newton363f66f2008-09-02 14:35:44 -07002072 }
2073
2074 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 * If the "interrupt" for this port doesn't correspond with any
2076 * hardware interrupt, we use a timer-based system. The original
2077 * driver used to do this with IRQ0.
2078 */
Alan Coxd4e33fa2012-01-26 17:44:09 +00002079 if (!up->port.irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 up->timer.data = (unsigned long)up;
Anton Vorontsov54381062010-10-01 17:21:25 +04002081 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 } else {
2083 retval = serial_link_irq_chain(up);
2084 if (retval)
2085 return retval;
2086 }
2087
2088 /*
2089 * Now, initialize the UART
2090 */
2091 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
2092
2093 spin_lock_irqsave(&up->port.lock, flags);
2094 if (up->port.flags & UPF_FOURPORT) {
Alan Coxd4e33fa2012-01-26 17:44:09 +00002095 if (!up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 up->port.mctrl |= TIOCM_OUT1;
2097 } else
2098 /*
2099 * Most PC uarts need OUT2 raised to enable interrupts.
2100 */
Alan Coxd4e33fa2012-01-26 17:44:09 +00002101 if (up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 up->port.mctrl |= TIOCM_OUT2;
2103
2104 serial8250_set_mctrl(&up->port, up->port.mctrl);
Russell King55d3b282005-06-23 15:05:41 +01002105
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002106 /* Serial over Lan (SoL) hack:
2107 Intel 8257x Gigabit ethernet chips have a
2108 16550 emulation, to be used for Serial Over Lan.
2109 Those chips take a longer time than a normal
2110 serial device to signalize that a transmission
2111 data was queued. Due to that, the above test generally
2112 fails. One solution would be to delay the reading of
2113 iir. However, this is not reliable, since the timeout
2114 is variable. So, let's just don't test if we receive
2115 TX irq. This way, we'll never enable UART_BUG_TXEN.
2116 */
Chuck Ebbertd41a4b52009-10-01 15:44:26 -07002117 if (skip_txen_test || up->port.flags & UPF_NO_TXEN_TEST)
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002118 goto dont_test_tx_en;
2119
Russell King55d3b282005-06-23 15:05:41 +01002120 /*
2121 * Do a quick test to see if we receive an
2122 * interrupt when we enable the TX irq.
2123 */
2124 serial_outp(up, UART_IER, UART_IER_THRI);
2125 lsr = serial_in(up, UART_LSR);
2126 iir = serial_in(up, UART_IIR);
2127 serial_outp(up, UART_IER, 0);
2128
2129 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
Russell King67f76542005-06-23 22:26:43 +01002130 if (!(up->bugs & UART_BUG_TXEN)) {
2131 up->bugs |= UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002132 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
David S. Miller84408382008-10-13 10:45:26 +01002133 serial_index(port));
Russell King55d3b282005-06-23 15:05:41 +01002134 }
2135 } else {
Russell King67f76542005-06-23 22:26:43 +01002136 up->bugs &= ~UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002137 }
2138
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002139dont_test_tx_en:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 spin_unlock_irqrestore(&up->port.lock, flags);
2141
2142 /*
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002143 * Clear the interrupt registers again for luck, and clear the
2144 * saved flags to avoid getting false values from polling
2145 * routines or the previous session.
2146 */
2147 serial_inp(up, UART_LSR);
2148 serial_inp(up, UART_RX);
2149 serial_inp(up, UART_IIR);
2150 serial_inp(up, UART_MSR);
2151 up->lsr_saved_flags = 0;
2152 up->msr_saved_flags = 0;
2153
2154 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 * Finally, enable interrupts. Note: Modem status interrupts
2156 * are set via set_termios(), which will be occurring imminently
2157 * anyway, so we don't enable them here.
2158 */
2159 up->ier = UART_IER_RLSI | UART_IER_RDI;
2160 serial_outp(up, UART_IER, up->ier);
2161
2162 if (up->port.flags & UPF_FOURPORT) {
2163 unsigned int icp;
2164 /*
2165 * Enable interrupts on the AST Fourport board
2166 */
2167 icp = (up->port.iobase & 0xfe0) | 0x01f;
2168 outb_p(0x80, icp);
2169 (void) inb_p(icp);
2170 }
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 return 0;
2173}
2174
2175static void serial8250_shutdown(struct uart_port *port)
2176{
Jamie Iles49d57412010-12-01 23:39:35 +00002177 struct uart_8250_port *up =
2178 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 unsigned long flags;
2180
2181 /*
2182 * Disable interrupts from this port
2183 */
2184 up->ier = 0;
2185 serial_outp(up, UART_IER, 0);
2186
2187 spin_lock_irqsave(&up->port.lock, flags);
2188 if (up->port.flags & UPF_FOURPORT) {
2189 /* reset interrupts on the AST Fourport board */
2190 inb((up->port.iobase & 0xfe0) | 0x1f);
2191 up->port.mctrl |= TIOCM_OUT1;
2192 } else
2193 up->port.mctrl &= ~TIOCM_OUT2;
2194
2195 serial8250_set_mctrl(&up->port, up->port.mctrl);
2196 spin_unlock_irqrestore(&up->port.lock, flags);
2197
2198 /*
2199 * Disable break condition and FIFOs
2200 */
2201 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
2202 serial8250_clear_fifos(up);
2203
2204#ifdef CONFIG_SERIAL_8250_RSA
2205 /*
2206 * Reset the RSA board back to 115kbps compat mode.
2207 */
2208 disable_rsa(up);
2209#endif
2210
2211 /*
2212 * Read data port to reset things, and then unlink from
2213 * the IRQ chain.
2214 */
2215 (void) serial_in(up, UART_RX);
2216
Alex Williamson40b36da2007-02-14 00:33:04 -08002217 del_timer_sync(&up->timer);
2218 up->timer.function = serial8250_timeout;
Alan Coxd4e33fa2012-01-26 17:44:09 +00002219 if (up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 serial_unlink_irq_chain(up);
2221}
2222
2223static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
2224{
2225 unsigned int quot;
2226
2227 /*
2228 * Handle magic divisors for baud rates above baud_base on
2229 * SMSC SuperIO chips.
2230 */
2231 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2232 baud == (port->uartclk/4))
2233 quot = 0x8001;
2234 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2235 baud == (port->uartclk/8))
2236 quot = 0x8002;
2237 else
2238 quot = uart_get_divisor(port, baud);
2239
2240 return quot;
2241}
2242
Philippe Langlais235dae52010-07-29 17:13:57 +02002243void
2244serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2245 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Jamie Iles49d57412010-12-01 23:39:35 +00002247 struct uart_8250_port *up =
2248 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 unsigned char cval, fcr = 0;
2250 unsigned long flags;
2251 unsigned int baud, quot;
2252
2253 switch (termios->c_cflag & CSIZE) {
2254 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +01002255 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 break;
2257 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +01002258 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 break;
2260 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +01002261 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 break;
2263 default:
2264 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +01002265 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 break;
2267 }
2268
2269 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +01002270 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (termios->c_cflag & PARENB)
2272 cval |= UART_LCR_PARITY;
2273 if (!(termios->c_cflag & PARODD))
2274 cval |= UART_LCR_EPAR;
2275#ifdef CMSPAR
2276 if (termios->c_cflag & CMSPAR)
2277 cval |= UART_LCR_SPAR;
2278#endif
2279
2280 /*
2281 * Ask the core to calculate the divisor for us.
2282 */
Anton Vorontsov24d481e2009-09-19 13:13:20 -07002283 baud = uart_get_baud_rate(port, termios, old,
2284 port->uartclk / 16 / 0xffff,
2285 port->uartclk / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 quot = serial8250_get_divisor(port, baud);
2287
2288 /*
Russell King4ba5e352005-06-23 10:43:04 +01002289 * Oxford Semi 952 rev B workaround
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 */
Russell King4ba5e352005-06-23 10:43:04 +01002291 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
Alan Cox3e8d4e22008-02-04 22:27:53 -08002292 quot++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
2295 if (baud < 2400)
2296 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2297 else
2298 fcr = uart_config[up->port.type].fcr;
2299 }
2300
2301 /*
2302 * MCR-based auto flow control. When AFE is enabled, RTS will be
2303 * deasserted when the receive FIFO contains more characters than
2304 * the trigger, or the MCR RTS bit is cleared. In the case where
2305 * the remote UART is not using CTS auto flow control, we must
2306 * have sufficient FIFO entries for the latency of the remote
2307 * UART to respond. IOW, at least 32 bytes of FIFO.
2308 */
2309 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
2310 up->mcr &= ~UART_MCR_AFE;
2311 if (termios->c_cflag & CRTSCTS)
2312 up->mcr |= UART_MCR_AFE;
2313 }
2314
2315 /*
2316 * Ok, we're now changing the port state. Do it with
2317 * interrupts disabled.
2318 */
2319 spin_lock_irqsave(&up->port.lock, flags);
2320
2321 /*
2322 * Update the per-port timeout.
2323 */
2324 uart_update_timeout(port, termios->c_cflag, baud);
2325
2326 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2327 if (termios->c_iflag & INPCK)
2328 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2329 if (termios->c_iflag & (BRKINT | PARMRK))
2330 up->port.read_status_mask |= UART_LSR_BI;
2331
2332 /*
2333 * Characteres to ignore
2334 */
2335 up->port.ignore_status_mask = 0;
2336 if (termios->c_iflag & IGNPAR)
2337 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2338 if (termios->c_iflag & IGNBRK) {
2339 up->port.ignore_status_mask |= UART_LSR_BI;
2340 /*
2341 * If we're ignoring parity and break indicators,
2342 * ignore overruns too (for real raw support).
2343 */
2344 if (termios->c_iflag & IGNPAR)
2345 up->port.ignore_status_mask |= UART_LSR_OE;
2346 }
2347
2348 /*
2349 * ignore all characters if CREAD is not set
2350 */
2351 if ((termios->c_cflag & CREAD) == 0)
2352 up->port.ignore_status_mask |= UART_LSR_DR;
2353
2354 /*
2355 * CTS flow control flag and modem status interrupts
2356 */
Ingo Molnarf8b372a2010-11-13 16:21:58 +01002357 up->ier &= ~UART_IER_MSI;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002358 if (!(up->bugs & UART_BUG_NOMSR) &&
2359 UART_ENABLE_MS(&up->port, termios->c_cflag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 up->ier |= UART_IER_MSI;
2361 if (up->capabilities & UART_CAP_UUE)
Stephen Warren4539c242011-05-17 16:12:36 -06002362 up->ier |= UART_IER_UUE;
2363 if (up->capabilities & UART_CAP_RTOIE)
2364 up->ier |= UART_IER_RTOIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 serial_out(up, UART_IER, up->ier);
2367
2368 if (up->capabilities & UART_CAP_EFR) {
2369 unsigned char efr = 0;
2370 /*
2371 * TI16C752/Startech hardware flow control. FIXME:
2372 * - TI16C752 requires control thresholds to be set.
2373 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2374 */
2375 if (termios->c_cflag & CRTSCTS)
2376 efr |= UART_EFR_CTS;
2377
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08002378 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
Søren Holm06315342011-09-02 22:55:37 +02002379 if (up->port.flags & UPF_EXAR_EFR)
2380 serial_outp(up, UART_XR_EFR, efr);
2381 else
2382 serial_outp(up, UART_EFR, efr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
2384
Russell Kingf2eda272008-09-01 21:47:59 +01002385#ifdef CONFIG_ARCH_OMAP
Jonathan McDowell255341c2006-08-14 23:05:32 -07002386 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
Russell King56685452008-09-01 21:25:33 +01002387 if (cpu_is_omap1510() && is_omap_port(up)) {
Jonathan McDowell255341c2006-08-14 23:05:32 -07002388 if (baud == 115200) {
2389 quot = 1;
2390 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2391 } else
2392 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2393 }
2394#endif
2395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 if (up->capabilities & UART_NATSEMI) {
2397 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
2398 serial_outp(up, UART_LCR, 0xe0);
2399 } else {
2400 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2401 }
2402
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +01002403 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 /*
2406 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2407 * is written without DLAB set, this mode will be disabled.
2408 */
2409 if (up->port.type == PORT_16750)
2410 serial_outp(up, UART_FCR, fcr);
2411
2412 serial_outp(up, UART_LCR, cval); /* reset DLAB */
2413 up->lcr = cval; /* Save LCR */
2414 if (up->port.type != PORT_16750) {
2415 if (fcr & UART_FCR_ENABLE_FIFO) {
2416 /* emulated UARTs (Lucent Venus 167x) need two steps */
2417 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
2418 }
2419 serial_outp(up, UART_FCR, fcr); /* set fcr */
2420 }
2421 serial8250_set_mctrl(&up->port, up->port.mctrl);
2422 spin_unlock_irqrestore(&up->port.lock, flags);
Alan Coxe991a2b2008-04-28 02:14:06 -07002423 /* Don't rewrite B0 */
2424 if (tty_termios_baud_rate(termios))
2425 tty_termios_encode_baud_rate(termios, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
Philippe Langlais235dae52010-07-29 17:13:57 +02002427EXPORT_SYMBOL(serial8250_do_set_termios);
2428
2429static void
2430serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2431 struct ktermios *old)
2432{
2433 if (port->set_termios)
2434 port->set_termios(port, termios, old);
2435 else
2436 serial8250_do_set_termios(port, termios, old);
2437}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439static void
Arnd Bergmanna0821df2010-06-01 22:53:11 +02002440serial8250_set_ldisc(struct uart_port *port, int new)
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002441{
Arnd Bergmanna0821df2010-06-01 22:53:11 +02002442 if (new == N_PPS) {
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002443 port->flags |= UPF_HARDPPS_CD;
2444 serial8250_enable_ms(port);
2445 } else
2446 port->flags &= ~UPF_HARDPPS_CD;
2447}
2448
Manuel Laussc161afe2010-09-25 15:13:45 +02002449
2450void serial8250_do_pm(struct uart_port *port, unsigned int state,
2451 unsigned int oldstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452{
Jamie Iles49d57412010-12-01 23:39:35 +00002453 struct uart_8250_port *p =
2454 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 serial8250_set_sleep(p, state != 0);
Manuel Laussc161afe2010-09-25 15:13:45 +02002457}
2458EXPORT_SYMBOL(serial8250_do_pm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Manuel Laussc161afe2010-09-25 15:13:45 +02002460static void
2461serial8250_pm(struct uart_port *port, unsigned int state,
2462 unsigned int oldstate)
2463{
2464 if (port->pm)
2465 port->pm(port, state, oldstate);
2466 else
2467 serial8250_do_pm(port, state, oldstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
Russell Kingf2eda272008-09-01 21:47:59 +01002470static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2471{
2472 if (pt->port.iotype == UPIO_AU)
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002473 return 0x1000;
Russell Kingf2eda272008-09-01 21:47:59 +01002474#ifdef CONFIG_ARCH_OMAP
2475 if (is_omap_port(pt))
2476 return 0x16 << pt->port.regshift;
2477#endif
2478 return 8 << pt->port.regshift;
2479}
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481/*
2482 * Resource handling.
2483 */
2484static int serial8250_request_std_resource(struct uart_8250_port *up)
2485{
Russell Kingf2eda272008-09-01 21:47:59 +01002486 unsigned int size = serial8250_port_size(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 int ret = 0;
2488
2489 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002490 case UPIO_AU:
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002491 case UPIO_TSI:
2492 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 case UPIO_MEM:
2494 if (!up->port.mapbase)
2495 break;
2496
2497 if (!request_mem_region(up->port.mapbase, size, "serial")) {
2498 ret = -EBUSY;
2499 break;
2500 }
2501
2502 if (up->port.flags & UPF_IOREMAP) {
Alan Cox6f441fe2008-05-01 04:34:59 -07002503 up->port.membase = ioremap_nocache(up->port.mapbase,
2504 size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (!up->port.membase) {
2506 release_mem_region(up->port.mapbase, size);
2507 ret = -ENOMEM;
2508 }
2509 }
2510 break;
2511
2512 case UPIO_HUB6:
2513 case UPIO_PORT:
2514 if (!request_region(up->port.iobase, size, "serial"))
2515 ret = -EBUSY;
2516 break;
2517 }
2518 return ret;
2519}
2520
2521static void serial8250_release_std_resource(struct uart_8250_port *up)
2522{
Russell Kingf2eda272008-09-01 21:47:59 +01002523 unsigned int size = serial8250_port_size(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002526 case UPIO_AU:
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002527 case UPIO_TSI:
2528 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 case UPIO_MEM:
2530 if (!up->port.mapbase)
2531 break;
2532
2533 if (up->port.flags & UPF_IOREMAP) {
2534 iounmap(up->port.membase);
2535 up->port.membase = NULL;
2536 }
2537
2538 release_mem_region(up->port.mapbase, size);
2539 break;
2540
2541 case UPIO_HUB6:
2542 case UPIO_PORT:
2543 release_region(up->port.iobase, size);
2544 break;
2545 }
2546}
2547
2548static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2549{
2550 unsigned long start = UART_RSA_BASE << up->port.regshift;
2551 unsigned int size = 8 << up->port.regshift;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002552 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 case UPIO_HUB6:
2556 case UPIO_PORT:
2557 start += up->port.iobase;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002558 if (request_region(start, size, "serial-rsa"))
2559 ret = 0;
2560 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 ret = -EBUSY;
2562 break;
2563 }
2564
2565 return ret;
2566}
2567
2568static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2569{
2570 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2571 unsigned int size = 8 << up->port.regshift;
2572
2573 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 case UPIO_HUB6:
2575 case UPIO_PORT:
2576 release_region(up->port.iobase + offset, size);
2577 break;
2578 }
2579}
2580
2581static void serial8250_release_port(struct uart_port *port)
2582{
Jamie Iles49d57412010-12-01 23:39:35 +00002583 struct uart_8250_port *up =
2584 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
2586 serial8250_release_std_resource(up);
2587 if (up->port.type == PORT_RSA)
2588 serial8250_release_rsa_resource(up);
2589}
2590
2591static int serial8250_request_port(struct uart_port *port)
2592{
Jamie Iles49d57412010-12-01 23:39:35 +00002593 struct uart_8250_port *up =
2594 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 int ret = 0;
2596
2597 ret = serial8250_request_std_resource(up);
2598 if (ret == 0 && up->port.type == PORT_RSA) {
2599 ret = serial8250_request_rsa_resource(up);
2600 if (ret < 0)
2601 serial8250_release_std_resource(up);
2602 }
2603
2604 return ret;
2605}
2606
2607static void serial8250_config_port(struct uart_port *port, int flags)
2608{
Jamie Iles49d57412010-12-01 23:39:35 +00002609 struct uart_8250_port *up =
2610 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 int probeflags = PROBE_ANY;
2612 int ret;
2613
2614 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 * Find the region that we can probe for. This in turn
2616 * tells us whether we can probe for the type of port.
2617 */
2618 ret = serial8250_request_std_resource(up);
2619 if (ret < 0)
2620 return;
2621
2622 ret = serial8250_request_rsa_resource(up);
2623 if (ret < 0)
2624 probeflags &= ~PROBE_RSA;
2625
Alan Coxb8e7e402009-05-28 14:01:35 +01002626 if (up->port.iotype != up->cur_iotype)
2627 set_io_from_upio(port);
2628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 if (flags & UART_CONFIG_TYPE)
2630 autoconfig(up, probeflags);
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002631
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002632 /* if access method is AU, it is a 16550 with a quirk */
2633 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
2634 up->bugs |= UART_BUG_NOMSR;
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002635
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2637 autoconfig_irq(up);
2638
2639 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2640 serial8250_release_rsa_resource(up);
2641 if (up->port.type == PORT_UNKNOWN)
2642 serial8250_release_std_resource(up);
2643}
2644
2645static int
2646serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2647{
Yinghai Lua62c4132008-08-19 20:49:55 -07002648 if (ser->irq >= nr_irqs || ser->irq < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2650 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2651 ser->type == PORT_STARTECH)
2652 return -EINVAL;
2653 return 0;
2654}
2655
2656static const char *
2657serial8250_type(struct uart_port *port)
2658{
2659 int type = port->type;
2660
2661 if (type >= ARRAY_SIZE(uart_config))
2662 type = 0;
2663 return uart_config[type].name;
2664}
2665
2666static struct uart_ops serial8250_pops = {
2667 .tx_empty = serial8250_tx_empty,
2668 .set_mctrl = serial8250_set_mctrl,
2669 .get_mctrl = serial8250_get_mctrl,
2670 .stop_tx = serial8250_stop_tx,
2671 .start_tx = serial8250_start_tx,
2672 .stop_rx = serial8250_stop_rx,
2673 .enable_ms = serial8250_enable_ms,
2674 .break_ctl = serial8250_break_ctl,
2675 .startup = serial8250_startup,
2676 .shutdown = serial8250_shutdown,
2677 .set_termios = serial8250_set_termios,
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002678 .set_ldisc = serial8250_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 .pm = serial8250_pm,
2680 .type = serial8250_type,
2681 .release_port = serial8250_release_port,
2682 .request_port = serial8250_request_port,
2683 .config_port = serial8250_config_port,
2684 .verify_port = serial8250_verify_port,
Jason Wesself2d937f2008-04-17 20:05:37 +02002685#ifdef CONFIG_CONSOLE_POLL
2686 .poll_get_char = serial8250_get_poll_char,
2687 .poll_put_char = serial8250_put_poll_char,
2688#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689};
2690
2691static struct uart_8250_port serial8250_ports[UART_NR];
2692
Alan Coxaf7f3742010-10-18 11:38:02 -07002693static void (*serial8250_isa_config)(int port, struct uart_port *up,
2694 unsigned short *capabilities);
2695
2696void serial8250_set_isa_configurator(
2697 void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
2698{
2699 serial8250_isa_config = v;
2700}
2701EXPORT_SYMBOL(serial8250_set_isa_configurator);
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703static void __init serial8250_isa_init_ports(void)
2704{
2705 struct uart_8250_port *up;
2706 static int first = 1;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002707 int i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
2709 if (!first)
2710 return;
2711 first = 0;
2712
Dave Jonesa61c2d72006-01-07 23:18:19 +00002713 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 struct uart_8250_port *up = &serial8250_ports[i];
2715
2716 up->port.line = i;
2717 spin_lock_init(&up->port.lock);
2718
2719 init_timer(&up->timer);
2720 up->timer.function = serial8250_timeout;
2721
2722 /*
2723 * ALPHA_KLUDGE_MCR needs to be killed.
2724 */
2725 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2726 up->mcr_force = ALPHA_KLUDGE_MCR;
2727
2728 up->port.ops = &serial8250_pops;
2729 }
2730
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002731 if (share_irqs)
2732 irqflag = IRQF_SHARED;
2733
Russell King44454bc2005-06-30 22:41:22 +01002734 for (i = 0, up = serial8250_ports;
Dave Jonesa61c2d72006-01-07 23:18:19 +00002735 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 i++, up++) {
2737 up->port.iobase = old_serial_port[i].port;
2738 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002739 up->port.irqflags = old_serial_port[i].irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 up->port.uartclk = old_serial_port[i].baud_base * 16;
2741 up->port.flags = old_serial_port[i].flags;
2742 up->port.hub6 = old_serial_port[i].hub6;
2743 up->port.membase = old_serial_port[i].iomem_base;
2744 up->port.iotype = old_serial_port[i].io_type;
2745 up->port.regshift = old_serial_port[i].iomem_reg_shift;
David Daney7d6a07d2009-01-02 13:49:47 +00002746 set_io_from_upio(&up->port);
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002747 up->port.irqflags |= irqflag;
Alan Coxaf7f3742010-10-18 11:38:02 -07002748 if (serial8250_isa_config != NULL)
2749 serial8250_isa_config(i, &up->port, &up->capabilities);
2750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
2752}
2753
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08002754static void
2755serial8250_init_fixed_type_port(struct uart_8250_port *up, unsigned int type)
2756{
2757 up->port.type = type;
2758 up->port.fifosize = uart_config[type].fifo_size;
2759 up->capabilities = uart_config[type].flags;
2760 up->tx_loadsz = uart_config[type].tx_loadsz;
2761}
2762
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763static void __init
2764serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2765{
2766 int i;
2767
Alan Coxb8e7e402009-05-28 14:01:35 +01002768 for (i = 0; i < nr_uarts; i++) {
2769 struct uart_8250_port *up = &serial8250_ports[i];
2770 up->cur_iotype = 0xFF;
2771 }
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 serial8250_isa_init_ports();
2774
Dave Jonesa61c2d72006-01-07 23:18:19 +00002775 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 struct uart_8250_port *up = &serial8250_ports[i];
2777
2778 up->port.dev = dev;
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08002779
2780 if (up->port.flags & UPF_FIXED_TYPE)
2781 serial8250_init_fixed_type_port(up, up->port.type);
2782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 uart_add_one_port(drv, &up->port);
2784 }
2785}
2786
2787#ifdef CONFIG_SERIAL_8250_CONSOLE
2788
Russell Kingd3587882006-03-20 20:00:09 +00002789static void serial8250_console_putchar(struct uart_port *port, int ch)
2790{
Jamie Iles49d57412010-12-01 23:39:35 +00002791 struct uart_8250_port *up =
2792 container_of(port, struct uart_8250_port, port);
Russell Kingd3587882006-03-20 20:00:09 +00002793
2794 wait_for_xmitr(up, UART_LSR_THRE);
2795 serial_out(up, UART_TX, ch);
2796}
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798/*
2799 * Print a string to the serial port trying not to disturb
2800 * any possible real use of the port...
2801 *
2802 * The console_lock must be held when we get here.
2803 */
2804static void
2805serial8250_console_write(struct console *co, const char *s, unsigned int count)
2806{
2807 struct uart_8250_port *up = &serial8250_ports[co->index];
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002808 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 unsigned int ier;
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002810 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Andrew Morton78512ec2005-11-07 00:59:13 -08002812 touch_nmi_watchdog();
2813
Andrew Morton68aa2c02006-06-30 02:29:59 -07002814 local_irq_save(flags);
2815 if (up->port.sysrq) {
Paul Gortmaker86b21192011-12-04 18:42:22 -05002816 /* serial8250_handle_irq() already took the lock */
Andrew Morton68aa2c02006-06-30 02:29:59 -07002817 locked = 0;
2818 } else if (oops_in_progress) {
2819 locked = spin_trylock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002820 } else
Andrew Morton68aa2c02006-06-30 02:29:59 -07002821 spin_lock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 /*
Ralf Baechledc7bf132006-02-15 09:59:47 +00002824 * First save the IER then disable the interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 */
2826 ier = serial_in(up, UART_IER);
2827
2828 if (up->capabilities & UART_CAP_UUE)
2829 serial_out(up, UART_IER, UART_IER_UUE);
2830 else
2831 serial_out(up, UART_IER, 0);
2832
Russell Kingd3587882006-03-20 20:00:09 +00002833 uart_console_write(&up->port, s, count, serial8250_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835 /*
2836 * Finally, wait for transmitter to become empty
2837 * and restore the IER
2838 */
Alan Coxf91a3712006-01-21 14:59:12 +00002839 wait_for_xmitr(up, BOTH_EMPTY);
Russell Kinga88d75b2006-04-30 11:30:15 +01002840 serial_out(up, UART_IER, ier);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002841
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002842 /*
2843 * The receive handling will happen properly because the
2844 * receive ready bit will still be set; it is not cleared
2845 * on read. However, modem control will not, we must
2846 * call it if we have saved something in the saved flags
2847 * while processing with interrupts off.
2848 */
2849 if (up->msr_saved_flags)
Paul Gortmaker3986fb22011-12-04 18:42:20 -05002850 serial8250_modem_status(up);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002851
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002852 if (locked)
Andrew Morton68aa2c02006-06-30 02:29:59 -07002853 spin_unlock(&up->port.lock);
2854 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855}
2856
Vivek Goyal118c0ac2007-01-11 01:52:44 +01002857static int __init serial8250_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
2859 struct uart_port *port;
2860 int baud = 9600;
2861 int bits = 8;
2862 int parity = 'n';
2863 int flow = 'n';
2864
2865 /*
2866 * Check whether an invalid uart number has been specified, and
2867 * if so, search for the first available port that does have
2868 * console support.
2869 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002870 if (co->index >= nr_uarts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 co->index = 0;
2872 port = &serial8250_ports[co->index].port;
2873 if (!port->iobase && !port->membase)
2874 return -ENODEV;
2875
2876 if (options)
2877 uart_parse_options(options, &baud, &parity, &bits, &flow);
2878
2879 return uart_set_options(port, co, baud, parity, bits, flow);
2880}
2881
Daniel Ritzb6b1d872007-08-03 16:07:43 +02002882static int serial8250_console_early_setup(void)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002883{
2884 return serial8250_find_port_for_earlycon();
2885}
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887static struct console serial8250_console = {
2888 .name = "ttyS",
2889 .write = serial8250_console_write,
2890 .device = uart_console_device,
2891 .setup = serial8250_console_setup,
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002892 .early_setup = serial8250_console_early_setup,
Peter Zijlstraa80c49d2010-11-15 21:11:12 +01002893 .flags = CON_PRINTBUFFER | CON_ANYTIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 .index = -1,
2895 .data = &serial8250_reg,
2896};
2897
2898static int __init serial8250_console_init(void)
2899{
Eric W. Biederman05d81d22008-07-12 13:47:53 -07002900 if (nr_uarts > UART_NR)
2901 nr_uarts = UART_NR;
2902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 serial8250_isa_init_ports();
2904 register_console(&serial8250_console);
2905 return 0;
2906}
2907console_initcall(serial8250_console_init);
2908
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002909int serial8250_find_port(struct uart_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
2911 int line;
2912 struct uart_port *port;
2913
Dave Jonesa61c2d72006-01-07 23:18:19 +00002914 for (line = 0; line < nr_uarts; line++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 port = &serial8250_ports[line].port;
Russell King50aec3b52006-01-04 18:13:03 +00002916 if (uart_match_port(p, port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 return line;
2918 }
2919 return -ENODEV;
2920}
2921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922#define SERIAL8250_CONSOLE &serial8250_console
2923#else
2924#define SERIAL8250_CONSOLE NULL
2925#endif
2926
2927static struct uart_driver serial8250_reg = {
2928 .owner = THIS_MODULE,
2929 .driver_name = "serial",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 .dev_name = "ttyS",
2931 .major = TTY_MAJOR,
2932 .minor = 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 .cons = SERIAL8250_CONSOLE,
2934};
2935
Russell Kingd856c662006-02-23 10:22:13 +00002936/*
2937 * early_serial_setup - early registration for 8250 ports
2938 *
2939 * Setup an 8250 port structure prior to console initialisation. Use
2940 * after console initialisation will cause undefined behaviour.
2941 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942int __init early_serial_setup(struct uart_port *port)
2943{
David Daneyb4304282009-01-02 13:49:41 +00002944 struct uart_port *p;
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 if (port->line >= ARRAY_SIZE(serial8250_ports))
2947 return -ENODEV;
2948
2949 serial8250_isa_init_ports();
David Daneyb4304282009-01-02 13:49:41 +00002950 p = &serial8250_ports[port->line].port;
2951 p->iobase = port->iobase;
2952 p->membase = port->membase;
2953 p->irq = port->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002954 p->irqflags = port->irqflags;
David Daneyb4304282009-01-02 13:49:41 +00002955 p->uartclk = port->uartclk;
2956 p->fifosize = port->fifosize;
2957 p->regshift = port->regshift;
2958 p->iotype = port->iotype;
2959 p->flags = port->flags;
2960 p->mapbase = port->mapbase;
2961 p->private_data = port->private_data;
Helge Deller125c97d2009-01-13 22:51:07 +01002962 p->type = port->type;
2963 p->line = port->line;
David Daney7d6a07d2009-01-02 13:49:47 +00002964
2965 set_io_from_upio(p);
2966 if (port->serial_in)
2967 p->serial_in = port->serial_in;
2968 if (port->serial_out)
2969 p->serial_out = port->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01002970 if (port->handle_irq)
2971 p->handle_irq = port->handle_irq;
2972 else
2973 p->handle_irq = serial8250_default_handle_irq;
David Daney7d6a07d2009-01-02 13:49:47 +00002974
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 return 0;
2976}
2977
2978/**
2979 * serial8250_suspend_port - suspend one serial port
2980 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 *
2982 * Suspend one serial port.
2983 */
2984void serial8250_suspend_port(int line)
2985{
2986 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2987}
2988
2989/**
2990 * serial8250_resume_port - resume one serial port
2991 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 *
2993 * Resume one serial port.
2994 */
2995void serial8250_resume_port(int line)
2996{
David Woodhouseb5b82df2007-05-17 14:27:39 +08002997 struct uart_8250_port *up = &serial8250_ports[line];
2998
2999 if (up->capabilities & UART_NATSEMI) {
David Woodhouseb5b82df2007-05-17 14:27:39 +08003000 /* Ensure it's still in high speed mode */
3001 serial_outp(up, UART_LCR, 0xE0);
3002
Yin Kangkai0d0389e2011-02-09 11:35:18 +08003003 ns16550a_goto_highspeed(up);
David Woodhouseb5b82df2007-05-17 14:27:39 +08003004
3005 serial_outp(up, UART_LCR, 0);
Yin Kangkai95926d22011-02-09 11:34:20 +08003006 up->port.uartclk = 921600*16;
David Woodhouseb5b82df2007-05-17 14:27:39 +08003007 }
3008 uart_resume_port(&serial8250_reg, &up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009}
3010
3011/*
3012 * Register a set of serial devices attached to a platform device. The
3013 * list is terminated with a zero flags entry, which means we expect
3014 * all entries to have at least UPF_BOOT_AUTOCONF set.
3015 */
Russell King3ae5eae2005-11-09 22:32:44 +00003016static int __devinit serial8250_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
Russell King3ae5eae2005-11-09 22:32:44 +00003018 struct plat_serial8250_port *p = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 struct uart_port port;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003020 int ret, i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
3022 memset(&port, 0, sizeof(struct uart_port));
3023
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003024 if (share_irqs)
3025 irqflag = IRQF_SHARED;
3026
Russell Kingec9f47c2005-06-27 11:12:54 +01003027 for (i = 0; p && p->flags != 0; p++, i++) {
Will Newton74a197412008-02-04 22:27:50 -08003028 port.iobase = p->iobase;
3029 port.membase = p->membase;
3030 port.irq = p->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07003031 port.irqflags = p->irqflags;
Will Newton74a197412008-02-04 22:27:50 -08003032 port.uartclk = p->uartclk;
3033 port.regshift = p->regshift;
3034 port.iotype = p->iotype;
3035 port.flags = p->flags;
3036 port.mapbase = p->mapbase;
3037 port.hub6 = p->hub6;
3038 port.private_data = p->private_data;
David Daney8e23fcc2009-01-02 13:49:54 +00003039 port.type = p->type;
David Daney7d6a07d2009-01-02 13:49:47 +00003040 port.serial_in = p->serial_in;
3041 port.serial_out = p->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01003042 port.handle_irq = p->handle_irq;
Philippe Langlais235dae52010-07-29 17:13:57 +02003043 port.set_termios = p->set_termios;
Manuel Laussc161afe2010-09-25 15:13:45 +02003044 port.pm = p->pm;
Will Newton74a197412008-02-04 22:27:50 -08003045 port.dev = &dev->dev;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003046 port.irqflags |= irqflag;
Russell Kingec9f47c2005-06-27 11:12:54 +01003047 ret = serial8250_register_port(&port);
3048 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00003049 dev_err(&dev->dev, "unable to register port at index %d "
Josh Boyer4f640ef2007-07-23 18:43:44 -07003050 "(IO%lx MEM%llx IRQ%d): %d\n", i,
3051 p->iobase, (unsigned long long)p->mapbase,
3052 p->irq, ret);
Russell Kingec9f47c2005-06-27 11:12:54 +01003053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 }
3055 return 0;
3056}
3057
3058/*
3059 * Remove serial ports registered against a platform device.
3060 */
Russell King3ae5eae2005-11-09 22:32:44 +00003061static int __devexit serial8250_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
3063 int i;
3064
Dave Jonesa61c2d72006-01-07 23:18:19 +00003065 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 struct uart_8250_port *up = &serial8250_ports[i];
3067
Russell King3ae5eae2005-11-09 22:32:44 +00003068 if (up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 serial8250_unregister_port(i);
3070 }
3071 return 0;
3072}
3073
Russell King3ae5eae2005-11-09 22:32:44 +00003074static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
3076 int i;
3077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 for (i = 0; i < UART_NR; i++) {
3079 struct uart_8250_port *up = &serial8250_ports[i];
3080
Russell King3ae5eae2005-11-09 22:32:44 +00003081 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 uart_suspend_port(&serial8250_reg, &up->port);
3083 }
3084
3085 return 0;
3086}
3087
Russell King3ae5eae2005-11-09 22:32:44 +00003088static int serial8250_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
3090 int i;
3091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 for (i = 0; i < UART_NR; i++) {
3093 struct uart_8250_port *up = &serial8250_ports[i];
3094
Russell King3ae5eae2005-11-09 22:32:44 +00003095 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
David Woodhouseb5b82df2007-05-17 14:27:39 +08003096 serial8250_resume_port(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
3098
3099 return 0;
3100}
3101
Russell King3ae5eae2005-11-09 22:32:44 +00003102static struct platform_driver serial8250_isa_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 .probe = serial8250_probe,
3104 .remove = __devexit_p(serial8250_remove),
3105 .suspend = serial8250_suspend,
3106 .resume = serial8250_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00003107 .driver = {
3108 .name = "serial8250",
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003109 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00003110 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111};
3112
3113/*
3114 * This "device" covers _all_ ISA 8250-compatible serial devices listed
3115 * in the table in include/asm/serial.h
3116 */
3117static struct platform_device *serial8250_isa_devs;
3118
3119/*
3120 * serial8250_register_port and serial8250_unregister_port allows for
3121 * 16x50 serial ports to be configured at run-time, to support PCMCIA
3122 * modems and PCI multiport cards.
3123 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003124static DEFINE_MUTEX(serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
3126static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
3127{
3128 int i;
3129
3130 /*
3131 * First, find a port entry which matches.
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 (uart_match_port(&serial8250_ports[i].port, port))
3135 return &serial8250_ports[i];
3136
3137 /*
3138 * We didn't find a matching entry, so look for the first
3139 * free entry. We look for one which hasn't been previously
3140 * used (indicated by zero iobase).
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 serial8250_ports[i].port.iobase == 0)
3145 return &serial8250_ports[i];
3146
3147 /*
3148 * That also failed. Last resort is to find any entry which
3149 * doesn't have a real port associated with it.
3150 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003151 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
3153 return &serial8250_ports[i];
3154
3155 return NULL;
3156}
3157
3158/**
3159 * serial8250_register_port - register a serial port
3160 * @port: serial port template
3161 *
3162 * Configure the serial port specified by the request. If the
3163 * port exists and is in use, it is hung up and unregistered
3164 * first.
3165 *
3166 * The port is then probed and if necessary the IRQ is autodetected
3167 * If this fails an error is returned.
3168 *
3169 * On success the port is ready to use and the line number is returned.
3170 */
3171int serial8250_register_port(struct uart_port *port)
3172{
3173 struct uart_8250_port *uart;
3174 int ret = -ENOSPC;
3175
3176 if (port->uartclk == 0)
3177 return -EINVAL;
3178
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003179 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
3181 uart = serial8250_find_match_or_unused(port);
3182 if (uart) {
3183 uart_remove_one_port(&serial8250_reg, &uart->port);
3184
Will Newton74a197412008-02-04 22:27:50 -08003185 uart->port.iobase = port->iobase;
3186 uart->port.membase = port->membase;
3187 uart->port.irq = port->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07003188 uart->port.irqflags = port->irqflags;
Will Newton74a197412008-02-04 22:27:50 -08003189 uart->port.uartclk = port->uartclk;
3190 uart->port.fifosize = port->fifosize;
3191 uart->port.regshift = port->regshift;
3192 uart->port.iotype = port->iotype;
3193 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
3194 uart->port.mapbase = port->mapbase;
3195 uart->port.private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 if (port->dev)
3197 uart->port.dev = port->dev;
David Daney8e23fcc2009-01-02 13:49:54 +00003198
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08003199 if (port->flags & UPF_FIXED_TYPE)
3200 serial8250_init_fixed_type_port(uart, port->type);
David Daney8e23fcc2009-01-02 13:49:54 +00003201
David Daney7d6a07d2009-01-02 13:49:47 +00003202 set_io_from_upio(&uart->port);
3203 /* Possibly override default I/O functions. */
3204 if (port->serial_in)
3205 uart->port.serial_in = port->serial_in;
3206 if (port->serial_out)
3207 uart->port.serial_out = port->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01003208 if (port->handle_irq)
3209 uart->port.handle_irq = port->handle_irq;
Philippe Langlais235dae52010-07-29 17:13:57 +02003210 /* Possibly override set_termios call */
3211 if (port->set_termios)
3212 uart->port.set_termios = port->set_termios;
Manuel Laussc161afe2010-09-25 15:13:45 +02003213 if (port->pm)
3214 uart->port.pm = port->pm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
Alan Coxaf7f3742010-10-18 11:38:02 -07003216 if (serial8250_isa_config != NULL)
3217 serial8250_isa_config(0, &uart->port,
3218 &uart->capabilities);
3219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 ret = uart_add_one_port(&serial8250_reg, &uart->port);
3221 if (ret == 0)
3222 ret = uart->port.line;
3223 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003224 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
3226 return ret;
3227}
3228EXPORT_SYMBOL(serial8250_register_port);
3229
3230/**
3231 * serial8250_unregister_port - remove a 16x50 serial port at runtime
3232 * @line: serial line number
3233 *
3234 * Remove one serial port. This may not be called from interrupt
3235 * context. We hand the port back to the our control.
3236 */
3237void serial8250_unregister_port(int line)
3238{
3239 struct uart_8250_port *uart = &serial8250_ports[line];
3240
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003241 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 uart_remove_one_port(&serial8250_reg, &uart->port);
3243 if (serial8250_isa_devs) {
3244 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
3245 uart->port.type = PORT_UNKNOWN;
3246 uart->port.dev = &serial8250_isa_devs->dev;
leitao@linux.vnet.ibm.comcb01ece2011-05-26 11:18:39 -03003247 uart->capabilities = uart_config[uart->port.type].flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 uart_add_one_port(&serial8250_reg, &uart->port);
3249 } else {
3250 uart->port.dev = NULL;
3251 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003252 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253}
3254EXPORT_SYMBOL(serial8250_unregister_port);
3255
3256static int __init serial8250_init(void)
3257{
Alan Cox25db8ad2008-08-19 20:49:40 -07003258 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Dave Jonesa61c2d72006-01-07 23:18:19 +00003260 if (nr_uarts > UART_NR)
3261 nr_uarts = UART_NR;
3262
Paul Bollef1fb9bb2008-12-30 14:06:43 +01003263 printk(KERN_INFO "Serial: 8250/16550 driver, "
Dave Jonesa61c2d72006-01-07 23:18:19 +00003264 "%d ports, IRQ sharing %sabled\n", nr_uarts,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 share_irqs ? "en" : "dis");
3266
David Millerb70ac772008-10-13 10:36:31 +01003267#ifdef CONFIG_SPARC
3268 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
3269#else
3270 serial8250_reg.nr = UART_NR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 ret = uart_register_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003272#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (ret)
3274 goto out;
3275
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003276 serial8250_isa_devs = platform_device_alloc("serial8250",
3277 PLAT8250_DEV_LEGACY);
3278 if (!serial8250_isa_devs) {
3279 ret = -ENOMEM;
Russell Kingbc965a72006-01-18 09:54:29 +00003280 goto unreg_uart_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 }
3282
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003283 ret = platform_device_add(serial8250_isa_devs);
3284 if (ret)
3285 goto put_dev;
3286
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
3288
Russell Kingbc965a72006-01-18 09:54:29 +00003289 ret = platform_driver_register(&serial8250_isa_driver);
3290 if (ret == 0)
3291 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Russell Kingbc965a72006-01-18 09:54:29 +00003293 platform_device_del(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003294put_dev:
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003295 platform_device_put(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003296unreg_uart_drv:
David Millerb70ac772008-10-13 10:36:31 +01003297#ifdef CONFIG_SPARC
3298 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3299#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003301#endif
Alan Cox25db8ad2008-08-19 20:49:40 -07003302out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 return ret;
3304}
3305
3306static void __exit serial8250_exit(void)
3307{
3308 struct platform_device *isa_dev = serial8250_isa_devs;
3309
3310 /*
3311 * This tells serial8250_unregister_port() not to re-register
3312 * the ports (thereby making serial8250_isa_driver permanently
3313 * in use.)
3314 */
3315 serial8250_isa_devs = NULL;
3316
Russell King3ae5eae2005-11-09 22:32:44 +00003317 platform_driver_unregister(&serial8250_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 platform_device_unregister(isa_dev);
3319
David Millerb70ac772008-10-13 10:36:31 +01003320#ifdef CONFIG_SPARC
3321 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3322#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003324#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325}
3326
3327module_init(serial8250_init);
3328module_exit(serial8250_exit);
3329
3330EXPORT_SYMBOL(serial8250_suspend_port);
3331EXPORT_SYMBOL(serial8250_resume_port);
3332
3333MODULE_LICENSE("GPL");
Adrian Bunkd87a6d92008-07-16 21:53:31 +01003334MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
3336module_param(share_irqs, uint, 0644);
3337MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
3338 " (unsafe)");
3339
Dave Jonesa61c2d72006-01-07 23:18:19 +00003340module_param(nr_uarts, uint, 0644);
3341MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
3342
Chuck Ebbertd41a4b52009-10-01 15:44:26 -07003343module_param(skip_txen_test, uint, 0644);
3344MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346#ifdef CONFIG_SERIAL_8250_RSA
3347module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
3348MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
3349#endif
3350MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);