blob: 2d62c89195e9a4740611122137822855c61b7832 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver for 8250/16550-type serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2001 Russell King.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * A note about mapbase / membase
14 *
15 * mapbase is the physical address of the IO port.
16 * membase is an 'ioremapped' cookie.
17 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
20#define SUPPORT_SYSRQ
21#endif
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/ioport.h>
26#include <linux/init.h>
27#include <linux/console.h>
28#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/delay.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010030#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/tty.h>
Daniel Drakecd3ecad2010-10-20 16:00:48 -070032#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/tty_flip.h>
34#include <linux/serial_reg.h>
35#include <linux/serial_core.h>
36#include <linux/serial.h>
37#include <linux/serial_8250.h>
Andrew Morton78512ec2005-11-07 00:59:13 -080038#include <linux/nmi.h>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000039#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Paul Gortmaker68163832012-02-09 18:48:19 -050041#ifdef CONFIG_SPARC
42#include <linux/sunserialcore.h>
43#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/io.h>
46#include <asm/irq.h>
47
48#include "8250.h"
49
50/*
51 * Configuration:
Thomas Gleixner40663cc2006-07-01 19:29:43 -070052 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * is unsafe when used on edge-triggered interrupts.
54 */
Adrian Bunk408b6642005-05-01 08:59:29 -070055static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Dave Jonesa61c2d72006-01-07 23:18:19 +000057static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
58
David S. Miller84408382008-10-13 10:45:26 +010059static struct uart_driver serial8250_reg;
60
61static int serial_index(struct uart_port *port)
62{
63 return (serial8250_reg.minor - 64) + port->line;
64}
65
Chuck Ebbertd41a4b52009-10-01 15:44:26 -070066static unsigned int skip_txen_test; /* force skip of txen test at init time */
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
69 * Debugging.
70 */
71#if 0
72#define DEBUG_AUTOCONF(fmt...) printk(fmt)
73#else
74#define DEBUG_AUTOCONF(fmt...) do { } while (0)
75#endif
76
77#if 0
78#define DEBUG_INTR(fmt...) printk(fmt)
79#else
80#define DEBUG_INTR(fmt...) do { } while (0)
81#endif
82
Jiri Slabye7328ae2011-06-05 22:51:49 +020083#define PASS_LIMIT 512
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Dick Hollenbeckbca47612009-12-09 12:31:34 -080085#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
86
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
89#define CONFIG_SERIAL_DETECT_IRQ 1
90#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#ifdef CONFIG_SERIAL_8250_MANY_PORTS
92#define CONFIG_SERIAL_MANY_PORTS 1
93#endif
94
95/*
96 * HUB6 is always on. This will be removed once the header
97 * files have been cleaned.
98 */
99#define CONFIG_HUB6 1
100
Bryan Wua4ed1e42008-05-31 16:10:04 +0800101#include <asm/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/*
103 * SERIAL_PORT_DFNS tells us about built-in ports that have no
104 * standard enumeration mechanism. Platforms that can find all
105 * serial ports via mechanisms like ACPI or PCI need not supply it.
106 */
107#ifndef SERIAL_PORT_DFNS
108#define SERIAL_PORT_DFNS
109#endif
110
Arjan van de Vencb3592b2005-11-28 21:04:11 +0000111static const struct old_serial_port old_serial_port[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 SERIAL_PORT_DFNS /* defined in asm/serial.h */
113};
114
Russell King026d02a2005-06-29 18:45:19 +0100115#define UART_NR CONFIG_SERIAL_8250_NR_UARTS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117#ifdef CONFIG_SERIAL_8250_RSA
118
119#define PORT_RSA_MAX 4
120static unsigned long probe_rsa[PORT_RSA_MAX];
121static unsigned int probe_rsa_count;
122#endif /* CONFIG_SERIAL_8250_RSA */
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124struct irq_info {
Alan Cox25db8ad2008-08-19 20:49:40 -0700125 struct hlist_node node;
126 int irq;
127 spinlock_t lock; /* Protects list not the hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 struct list_head *head;
129};
130
Alan Cox25db8ad2008-08-19 20:49:40 -0700131#define NR_IRQ_HASH 32 /* Can be adjusted later */
132static struct hlist_head irq_lists[NR_IRQ_HASH];
133static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/*
136 * Here we define the default xmit fifo size used for each type of UART.
137 */
138static const struct serial8250_config uart_config[] = {
139 [PORT_UNKNOWN] = {
140 .name = "unknown",
141 .fifo_size = 1,
142 .tx_loadsz = 1,
143 },
144 [PORT_8250] = {
145 .name = "8250",
146 .fifo_size = 1,
147 .tx_loadsz = 1,
148 },
149 [PORT_16450] = {
150 .name = "16450",
151 .fifo_size = 1,
152 .tx_loadsz = 1,
153 },
154 [PORT_16550] = {
155 .name = "16550",
156 .fifo_size = 1,
157 .tx_loadsz = 1,
158 },
159 [PORT_16550A] = {
160 .name = "16550A",
161 .fifo_size = 16,
162 .tx_loadsz = 16,
163 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
164 .flags = UART_CAP_FIFO,
165 },
166 [PORT_CIRRUS] = {
167 .name = "Cirrus",
168 .fifo_size = 1,
169 .tx_loadsz = 1,
170 },
171 [PORT_16650] = {
172 .name = "ST16650",
173 .fifo_size = 1,
174 .tx_loadsz = 1,
175 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
176 },
177 [PORT_16650V2] = {
178 .name = "ST16650V2",
179 .fifo_size = 32,
180 .tx_loadsz = 16,
181 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
182 UART_FCR_T_TRIG_00,
183 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
184 },
185 [PORT_16750] = {
186 .name = "TI16750",
187 .fifo_size = 64,
188 .tx_loadsz = 64,
189 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
190 UART_FCR7_64BYTE,
191 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
192 },
193 [PORT_STARTECH] = {
194 .name = "Startech",
195 .fifo_size = 1,
196 .tx_loadsz = 1,
197 },
198 [PORT_16C950] = {
199 .name = "16C950/954",
200 .fifo_size = 128,
201 .tx_loadsz = 128,
202 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
Pavel Machekd0694e22011-01-09 08:38:48 +0100203 /* UART_CAP_EFR breaks billionon CF bluetooth card. */
204 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 },
206 [PORT_16654] = {
207 .name = "ST16654",
208 .fifo_size = 64,
209 .tx_loadsz = 32,
210 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
211 UART_FCR_T_TRIG_10,
212 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
213 },
214 [PORT_16850] = {
215 .name = "XR16850",
216 .fifo_size = 128,
217 .tx_loadsz = 128,
218 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
219 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
220 },
221 [PORT_RSA] = {
222 .name = "RSA",
223 .fifo_size = 2048,
224 .tx_loadsz = 2048,
225 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
226 .flags = UART_CAP_FIFO,
227 },
228 [PORT_NS16550A] = {
229 .name = "NS16550A",
230 .fifo_size = 16,
231 .tx_loadsz = 16,
232 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
233 .flags = UART_CAP_FIFO | UART_NATSEMI,
234 },
235 [PORT_XSCALE] = {
236 .name = "XScale",
237 .fifo_size = 32,
238 .tx_loadsz = 32,
239 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
Stephen Warren4539c242011-05-17 16:12:36 -0600240 .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 },
Thomas Koellerbd71c182007-05-06 14:48:47 -0700242 [PORT_RM9000] = {
243 .name = "RM9000",
244 .fifo_size = 16,
245 .tx_loadsz = 16,
246 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
247 .flags = UART_CAP_FIFO,
248 },
David Daney6b06f192009-01-02 13:50:00 +0000249 [PORT_OCTEON] = {
250 .name = "OCTEON",
251 .fifo_size = 64,
252 .tx_loadsz = 64,
253 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
254 .flags = UART_CAP_FIFO,
255 },
Florian Fainelli08e09922009-06-11 13:21:24 +0100256 [PORT_AR7] = {
257 .name = "AR7",
258 .fifo_size = 16,
259 .tx_loadsz = 16,
260 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
261 .flags = UART_CAP_FIFO | UART_CAP_AFE,
262 },
Philippe Langlais235dae52010-07-29 17:13:57 +0200263 [PORT_U6_16550A] = {
264 .name = "U6_16550A",
265 .fifo_size = 64,
266 .tx_loadsz = 64,
267 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
268 .flags = UART_CAP_FIFO | UART_CAP_AFE,
269 },
Stephen Warren4539c242011-05-17 16:12:36 -0600270 [PORT_TEGRA] = {
271 .name = "Tegra",
272 .fifo_size = 32,
273 .tx_loadsz = 8,
274 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
275 UART_FCR_T_TRIG_01,
276 .flags = UART_CAP_FIFO | UART_CAP_RTOIE,
277 },
Søren Holm06315342011-09-02 22:55:37 +0200278 [PORT_XR17D15X] = {
279 .name = "XR17D15X",
280 .fifo_size = 64,
281 .tx_loadsz = 64,
282 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
283 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR,
284 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285};
286
Magnus Dammcc419fa02012-05-02 21:46:51 +0900287/* Uart divisor latch read */
288static int default_dl_read(struct uart_8250_port *up)
289{
290 return serial_in(up, UART_DLL) | serial_in(up, UART_DLM) << 8;
291}
292
293/* Uart divisor latch write */
294static void default_dl_write(struct uart_8250_port *up, int value)
295{
296 serial_out(up, UART_DLL, value & 0xff);
297 serial_out(up, UART_DLM, value >> 8 & 0xff);
298}
299
Magnus Damm6b416032012-05-02 21:47:00 +0900300#if defined(CONFIG_SERIAL_8250_RM9K)
Magnus Dammcc419fa02012-05-02 21:46:51 +0900301static int _serial_dl_read(struct uart_8250_port *up)
302{
303 return (up->port.iotype == UPIO_RM9000) ?
304 (((__raw_readl(up->port.membase + 0x10) << 8) |
305 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
306 default_dl_read(up);
307}
308
309static void _serial_dl_write(struct uart_8250_port *up, int value)
310{
311 if (up->port.iotype == UPIO_RM9000) {
312 __raw_writel(value, up->port.membase + 0x08);
313 __raw_writel(value >> 8, up->port.membase + 0x10);
314 } else {
315 default_dl_write(up, value);
316 }
317}
318#else
319static int _serial_dl_read(struct uart_8250_port *up)
320{
321 return default_dl_read(up);
322}
323
324static void _serial_dl_write(struct uart_8250_port *up, int value)
325{
326 default_dl_write(up, value);
327}
328#endif
329
Magnus Damm6b416032012-05-02 21:47:00 +0900330#ifdef CONFIG_MIPS_ALCHEMY
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000331
332/* Au1x00 UART hardware has a weird register layout */
333static const u8 au_io_in_map[] = {
334 [UART_RX] = 0,
335 [UART_IER] = 2,
336 [UART_IIR] = 3,
337 [UART_LCR] = 5,
338 [UART_MCR] = 6,
339 [UART_LSR] = 7,
340 [UART_MSR] = 8,
341};
342
343static const u8 au_io_out_map[] = {
344 [UART_TX] = 1,
345 [UART_IER] = 2,
346 [UART_FCR] = 4,
347 [UART_LCR] = 5,
348 [UART_MCR] = 6,
349};
350
Magnus Damm6b416032012-05-02 21:47:00 +0900351static unsigned int au_serial_in(struct uart_port *p, int offset)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000352{
Magnus Damm6b416032012-05-02 21:47:00 +0900353 offset = au_io_in_map[offset] << p->regshift;
354 return __raw_readl(p->membase + offset);
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000355}
356
Magnus Damm6b416032012-05-02 21:47:00 +0900357static void au_serial_out(struct uart_port *p, int offset, int value)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000358{
Magnus Damm6b416032012-05-02 21:47:00 +0900359 offset = au_io_out_map[offset] << p->regshift;
360 __raw_writel(value, p->membase + offset);
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000361}
362
Magnus Damm6b416032012-05-02 21:47:00 +0900363/* Au1x00 haven't got a standard divisor latch */
364static int au_serial_dl_read(struct uart_8250_port *up)
365{
366 return __raw_readl(up->port.membase + 0x28);
367}
368
369static void au_serial_dl_write(struct uart_8250_port *up, int value)
370{
371 __raw_writel(value, up->port.membase + 0x28);
372}
373
374#endif
375
376#if defined(CONFIG_SERIAL_8250_RM9K)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700377
378static const u8
379 regmap_in[8] = {
380 [UART_RX] = 0x00,
381 [UART_IER] = 0x0c,
382 [UART_IIR] = 0x14,
383 [UART_LCR] = 0x1c,
384 [UART_MCR] = 0x20,
385 [UART_LSR] = 0x24,
386 [UART_MSR] = 0x28,
387 [UART_SCR] = 0x2c
388 },
389 regmap_out[8] = {
390 [UART_TX] = 0x04,
391 [UART_IER] = 0x0c,
392 [UART_FCR] = 0x18,
393 [UART_LCR] = 0x1c,
394 [UART_MCR] = 0x20,
395 [UART_LSR] = 0x24,
396 [UART_MSR] = 0x28,
397 [UART_SCR] = 0x2c
398 };
399
David Daney7d6a07d2009-01-02 13:49:47 +0000400static inline int map_8250_in_reg(struct uart_port *p, int offset)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700401{
David Daney7d6a07d2009-01-02 13:49:47 +0000402 if (p->iotype != UPIO_RM9000)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700403 return offset;
404 return regmap_in[offset];
405}
406
David Daney7d6a07d2009-01-02 13:49:47 +0000407static inline int map_8250_out_reg(struct uart_port *p, int offset)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700408{
David Daney7d6a07d2009-01-02 13:49:47 +0000409 if (p->iotype != UPIO_RM9000)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700410 return offset;
411 return regmap_out[offset];
412}
413
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000414#else
415
416/* sane hardware needs no mapping */
417#define map_8250_in_reg(up, offset) (offset)
418#define map_8250_out_reg(up, offset) (offset)
419
420#endif
421
David Daney7d6a07d2009-01-02 13:49:47 +0000422static unsigned int hub6_serial_in(struct uart_port *p, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
David Daney7d6a07d2009-01-02 13:49:47 +0000424 offset = map_8250_in_reg(p, offset) << p->regshift;
425 outb(p->hub6 - 1 + offset, p->iobase);
426 return inb(p->iobase + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
David Daney7d6a07d2009-01-02 13:49:47 +0000429static void hub6_serial_out(struct uart_port *p, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
David Daney7d6a07d2009-01-02 13:49:47 +0000431 offset = map_8250_out_reg(p, offset) << p->regshift;
432 outb(p->hub6 - 1 + offset, p->iobase);
433 outb(value, p->iobase + 1);
434}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
David Daney7d6a07d2009-01-02 13:49:47 +0000436static unsigned int mem_serial_in(struct uart_port *p, int offset)
437{
438 offset = map_8250_in_reg(p, offset) << p->regshift;
439 return readb(p->membase + offset);
440}
441
442static void mem_serial_out(struct uart_port *p, int offset, int value)
443{
444 offset = map_8250_out_reg(p, offset) << p->regshift;
445 writeb(value, p->membase + offset);
446}
447
448static void mem32_serial_out(struct uart_port *p, int offset, int value)
449{
450 offset = map_8250_out_reg(p, offset) << p->regshift;
451 writel(value, p->membase + offset);
452}
453
454static unsigned int mem32_serial_in(struct uart_port *p, int offset)
455{
456 offset = map_8250_in_reg(p, offset) << p->regshift;
457 return readl(p->membase + offset);
458}
459
David Daney7d6a07d2009-01-02 13:49:47 +0000460static unsigned int io_serial_in(struct uart_port *p, int offset)
461{
462 offset = map_8250_in_reg(p, offset) << p->regshift;
463 return inb(p->iobase + offset);
464}
465
466static void io_serial_out(struct uart_port *p, int offset, int value)
467{
468 offset = map_8250_out_reg(p, offset) << p->regshift;
469 outb(value, p->iobase + offset);
470}
471
Jamie Iles583d28e2011-08-15 10:17:52 +0100472static int serial8250_default_handle_irq(struct uart_port *port);
473
David Daney7d6a07d2009-01-02 13:49:47 +0000474static void set_io_from_upio(struct uart_port *p)
475{
Jamie Iles49d57412010-12-01 23:39:35 +0000476 struct uart_8250_port *up =
477 container_of(p, struct uart_8250_port, port);
Magnus Dammcc419fa02012-05-02 21:46:51 +0900478
479 up->dl_read = _serial_dl_read;
480 up->dl_write = _serial_dl_write;
481
David Daney7d6a07d2009-01-02 13:49:47 +0000482 switch (p->iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 case UPIO_HUB6:
David Daney7d6a07d2009-01-02 13:49:47 +0000484 p->serial_in = hub6_serial_in;
485 p->serial_out = hub6_serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487
488 case UPIO_MEM:
David Daney7d6a07d2009-01-02 13:49:47 +0000489 p->serial_in = mem_serial_in;
490 p->serial_out = mem_serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 break;
492
Thomas Koellerbd71c182007-05-06 14:48:47 -0700493 case UPIO_RM9000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 case UPIO_MEM32:
David Daney7d6a07d2009-01-02 13:49:47 +0000495 p->serial_in = mem32_serial_in;
496 p->serial_out = mem32_serial_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 break;
498
Magnus Damm6b416032012-05-02 21:47:00 +0900499#ifdef CONFIG_MIPS_ALCHEMY
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000500 case UPIO_AU:
David Daney7d6a07d2009-01-02 13:49:47 +0000501 p->serial_in = au_serial_in;
502 p->serial_out = au_serial_out;
Magnus Damm6b416032012-05-02 21:47:00 +0900503 up->dl_read = au_serial_dl_read;
504 up->dl_write = au_serial_dl_write;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000505 break;
Magnus Damm6b416032012-05-02 21:47:00 +0900506#endif
Manuel Lauss12bf3f22010-07-15 21:45:05 +0200507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 default:
David Daney7d6a07d2009-01-02 13:49:47 +0000509 p->serial_in = io_serial_in;
510 p->serial_out = io_serial_out;
511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Alan Coxb8e7e402009-05-28 14:01:35 +0100513 /* Remember loaded iotype */
514 up->cur_iotype = p->iotype;
Jamie Iles583d28e2011-08-15 10:17:52 +0100515 p->handle_irq = serial8250_default_handle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
Alex Williamson40b36da2007-02-14 00:33:04 -0800518static void
Paul Gortmaker55e40162012-03-08 19:12:14 -0500519serial_port_out_sync(struct uart_port *p, int offset, int value)
Alex Williamson40b36da2007-02-14 00:33:04 -0800520{
David Daney7d6a07d2009-01-02 13:49:47 +0000521 switch (p->iotype) {
Alex Williamson40b36da2007-02-14 00:33:04 -0800522 case UPIO_MEM:
523 case UPIO_MEM32:
Alex Williamson40b36da2007-02-14 00:33:04 -0800524 case UPIO_AU:
David Daney7d6a07d2009-01-02 13:49:47 +0000525 p->serial_out(p, offset, value);
526 p->serial_in(p, UART_LCR); /* safe, no side-effects */
Alex Williamson40b36da2007-02-14 00:33:04 -0800527 break;
528 default:
David Daney7d6a07d2009-01-02 13:49:47 +0000529 p->serial_out(p, offset, value);
Alex Williamson40b36da2007-02-14 00:33:04 -0800530 }
531}
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/*
534 * For the 16C950
535 */
536static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
537{
538 serial_out(up, UART_SCR, offset);
539 serial_out(up, UART_ICR, value);
540}
541
542static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
543{
544 unsigned int value;
545
546 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
547 serial_out(up, UART_SCR, offset);
548 value = serial_in(up, UART_ICR);
549 serial_icr_write(up, UART_ACR, up->acr);
550
551 return value;
552}
553
554/*
555 * FIFO support.
556 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100557static void serial8250_clear_fifos(struct uart_8250_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 if (p->capabilities & UART_CAP_FIFO) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500560 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
561 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500563 serial_out(p, UART_FCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565}
566
Sudhakar Mamillapalli0ad372b2012-04-10 14:10:58 -0700567void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
568{
569 unsigned char fcr;
570
571 serial8250_clear_fifos(p);
572 fcr = uart_config[p->port.type].fcr;
573 serial_out(p, UART_FCR, fcr);
574}
575EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577/*
578 * IER sleep support. UARTs which have EFRs need the "extended
579 * capability" bit enabled. Note that on XR16C850s, we need to
580 * reset LCR to write to IER.
581 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100582static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 if (p->capabilities & UART_CAP_SLEEP) {
585 if (p->capabilities & UART_CAP_EFR) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500586 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
587 serial_out(p, UART_EFR, UART_EFR_ECB);
588 serial_out(p, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500590 serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (p->capabilities & UART_CAP_EFR) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500592 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
593 serial_out(p, UART_EFR, 0);
594 serial_out(p, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596 }
597}
598
599#ifdef CONFIG_SERIAL_8250_RSA
600/*
601 * Attempts to turn on the RSA FIFO. Returns zero on failure.
602 * We set the port uart clock rate if we succeed.
603 */
604static int __enable_rsa(struct uart_8250_port *up)
605{
606 unsigned char mode;
607 int result;
608
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500609 mode = serial_in(up, UART_RSA_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 result = mode & UART_RSA_MSR_FIFO;
611
612 if (!result) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500613 serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
614 mode = serial_in(up, UART_RSA_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 result = mode & UART_RSA_MSR_FIFO;
616 }
617
618 if (result)
619 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
620
621 return result;
622}
623
624static void enable_rsa(struct uart_8250_port *up)
625{
626 if (up->port.type == PORT_RSA) {
627 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
628 spin_lock_irq(&up->port.lock);
629 __enable_rsa(up);
630 spin_unlock_irq(&up->port.lock);
631 }
632 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500633 serial_out(up, UART_RSA_FRR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635}
636
637/*
638 * Attempts to turn off the RSA FIFO. Returns zero on failure.
639 * It is unknown why interrupts were disabled in here. However,
640 * the caller is expected to preserve this behaviour by grabbing
641 * the spinlock before calling this function.
642 */
643static void disable_rsa(struct uart_8250_port *up)
644{
645 unsigned char mode;
646 int result;
647
648 if (up->port.type == PORT_RSA &&
649 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
650 spin_lock_irq(&up->port.lock);
651
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500652 mode = serial_in(up, UART_RSA_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 result = !(mode & UART_RSA_MSR_FIFO);
654
655 if (!result) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500656 serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
657 mode = serial_in(up, UART_RSA_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 result = !(mode & UART_RSA_MSR_FIFO);
659 }
660
661 if (result)
662 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
663 spin_unlock_irq(&up->port.lock);
664 }
665}
666#endif /* CONFIG_SERIAL_8250_RSA */
667
668/*
669 * This is a quickie test to see how big the FIFO is.
670 * It doesn't work at all the time, more's the pity.
671 */
672static int size_fifo(struct uart_8250_port *up)
673{
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100674 unsigned char old_fcr, old_mcr, old_lcr;
675 unsigned short old_dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int count;
677
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500678 old_lcr = serial_in(up, UART_LCR);
679 serial_out(up, UART_LCR, 0);
680 old_fcr = serial_in(up, UART_FCR);
681 old_mcr = serial_in(up, UART_MCR);
682 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500684 serial_out(up, UART_MCR, UART_MCR_LOOP);
685 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100686 old_dl = serial_dl_read(up);
687 serial_dl_write(up, 0x0001);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500688 serial_out(up, UART_LCR, 0x03);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 for (count = 0; count < 256; count++)
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500690 serial_out(up, UART_TX, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 mdelay(20);/* FIXME - schedule_timeout */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500692 for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 (count < 256); count++)
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500694 serial_in(up, UART_RX);
695 serial_out(up, UART_FCR, old_fcr);
696 serial_out(up, UART_MCR, old_mcr);
697 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100698 serial_dl_write(up, old_dl);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500699 serial_out(up, UART_LCR, old_lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 return count;
702}
703
704/*
705 * Read UART ID using the divisor method - set DLL and DLM to zero
706 * and the revision will be in DLL and device type in DLM. We
707 * preserve the device state across this.
708 */
709static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
710{
711 unsigned char old_dll, old_dlm, old_lcr;
712 unsigned int id;
713
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500714 old_lcr = serial_in(p, UART_LCR);
715 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500717 old_dll = serial_in(p, UART_DLL);
718 old_dlm = serial_in(p, UART_DLM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500720 serial_out(p, UART_DLL, 0);
721 serial_out(p, UART_DLM, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500723 id = serial_in(p, UART_DLL) | serial_in(p, UART_DLM) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500725 serial_out(p, UART_DLL, old_dll);
726 serial_out(p, UART_DLM, old_dlm);
727 serial_out(p, UART_LCR, old_lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 return id;
730}
731
732/*
733 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
734 * When this function is called we know it is at least a StarTech
735 * 16650 V2, but it might be one of several StarTech UARTs, or one of
736 * its clones. (We treat the broken original StarTech 16650 V1 as a
737 * 16550, and why not? Startech doesn't seem to even acknowledge its
738 * existence.)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700739 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * What evil have men's minds wrought...
741 */
742static void autoconfig_has_efr(struct uart_8250_port *up)
743{
744 unsigned int id1, id2, id3, rev;
745
746 /*
747 * Everything with an EFR has SLEEP
748 */
749 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
750
751 /*
752 * First we check to see if it's an Oxford Semiconductor UART.
753 *
754 * If we have to do this here because some non-National
755 * Semiconductor clone chips lock up if you try writing to the
756 * LSR register (which serial_icr_read does)
757 */
758
759 /*
760 * Check for Oxford Semiconductor 16C950.
761 *
762 * EFR [4] must be set else this test fails.
763 *
764 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
765 * claims that it's needed for 952 dual UART's (which are not
766 * recommended for new designs).
767 */
768 up->acr = 0;
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800769 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 serial_out(up, UART_EFR, UART_EFR_ECB);
771 serial_out(up, UART_LCR, 0x00);
772 id1 = serial_icr_read(up, UART_ID1);
773 id2 = serial_icr_read(up, UART_ID2);
774 id3 = serial_icr_read(up, UART_ID3);
775 rev = serial_icr_read(up, UART_REV);
776
777 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
778
779 if (id1 == 0x16 && id2 == 0xC9 &&
780 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
781 up->port.type = PORT_16C950;
Russell King4ba5e352005-06-23 10:43:04 +0100782
783 /*
784 * Enable work around for the Oxford Semiconductor 952 rev B
785 * chip which causes it to seriously miscalculate baud rates
786 * when DLL is 0.
787 */
788 if (id3 == 0x52 && rev == 0x01)
789 up->bugs |= UART_BUG_QUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return;
791 }
Thomas Koellerbd71c182007-05-06 14:48:47 -0700792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /*
794 * We check for a XR16C850 by setting DLL and DLM to 0, and then
795 * reading back DLL and DLM. The chip type depends on the DLM
796 * value read back:
797 * 0x10 - XR16C850 and the DLL contains the chip revision.
798 * 0x12 - XR16C2850.
799 * 0x14 - XR16C854.
800 */
801 id1 = autoconfig_read_divisor_id(up);
802 DEBUG_AUTOCONF("850id=%04x ", id1);
803
804 id2 = id1 >> 8;
805 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 up->port.type = PORT_16850;
807 return;
808 }
809
810 /*
811 * It wasn't an XR16C850.
812 *
813 * We distinguish between the '654 and the '650 by counting
814 * how many bytes are in the FIFO. I'm using this for now,
815 * since that's the technique that was sent to me in the
816 * serial driver update, but I'm not convinced this works.
817 * I've had problems doing this in the past. -TYT
818 */
819 if (size_fifo(up) == 64)
820 up->port.type = PORT_16654;
821 else
822 up->port.type = PORT_16650V2;
823}
824
825/*
826 * We detected a chip without a FIFO. Only two fall into
827 * this category - the original 8250 and the 16450. The
828 * 16450 has a scratch register (accessible with LCR=0)
829 */
830static void autoconfig_8250(struct uart_8250_port *up)
831{
832 unsigned char scratch, status1, status2;
833
834 up->port.type = PORT_8250;
835
836 scratch = serial_in(up, UART_SCR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500837 serial_out(up, UART_SCR, 0xa5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 status1 = serial_in(up, UART_SCR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500839 serial_out(up, UART_SCR, 0x5a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 status2 = serial_in(up, UART_SCR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500841 serial_out(up, UART_SCR, scratch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 if (status1 == 0xa5 && status2 == 0x5a)
844 up->port.type = PORT_16450;
845}
846
847static int broken_efr(struct uart_8250_port *up)
848{
849 /*
850 * Exar ST16C2550 "A2" devices incorrectly detect as
851 * having an EFR, and report an ID of 0x0201. See
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200852 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 */
854 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
855 return 1;
856
857 return 0;
858}
859
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800860static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
861{
862 unsigned char status;
863
864 status = serial_in(up, 0x04); /* EXCR2 */
865#define PRESL(x) ((x) & 0x30)
866 if (PRESL(status) == 0x10) {
867 /* already in high speed mode */
868 return 0;
869 } else {
870 status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
871 status |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500872 serial_out(up, 0x04, status);
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800873 }
874 return 1;
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877/*
878 * We know that the chip has FIFOs. Does it have an EFR? The
879 * EFR is located in the same register position as the IIR and
880 * we know the top two bits of the IIR are currently set. The
881 * EFR should contain zero. Try to read the EFR.
882 */
883static void autoconfig_16550a(struct uart_8250_port *up)
884{
885 unsigned char status1, status2;
886 unsigned int iersave;
887
888 up->port.type = PORT_16550A;
889 up->capabilities |= UART_CAP_FIFO;
890
891 /*
892 * Check for presence of the EFR when DLAB is set.
893 * Only ST16C650V1 UARTs pass this test.
894 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500895 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (serial_in(up, UART_EFR) == 0) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500897 serial_out(up, UART_EFR, 0xA8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (serial_in(up, UART_EFR) != 0) {
899 DEBUG_AUTOCONF("EFRv1 ");
900 up->port.type = PORT_16650;
901 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
902 } else {
903 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
904 }
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500905 serial_out(up, UART_EFR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return;
907 }
908
909 /*
910 * Maybe it requires 0xbf to be written to the LCR.
911 * (other ST16C650V2 UARTs, TI16C752A, etc)
912 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500913 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
915 DEBUG_AUTOCONF("EFRv2 ");
916 autoconfig_has_efr(up);
917 return;
918 }
919
920 /*
921 * Check for a National Semiconductor SuperIO chip.
922 * Attempt to switch to bank 2, read the value of the LOOP bit
923 * from EXCR1. Switch back to bank 0, change it in MCR. Then
924 * switch back to bank 2, read it from EXCR1 again and check
925 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500927 serial_out(up, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 status1 = serial_in(up, UART_MCR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500929 serial_out(up, UART_LCR, 0xE0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 status2 = serial_in(up, 0x02); /* EXCR1 */
931
932 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500933 serial_out(up, UART_LCR, 0);
934 serial_out(up, UART_MCR, status1 ^ UART_MCR_LOOP);
935 serial_out(up, UART_LCR, 0xE0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 status2 = serial_in(up, 0x02); /* EXCR1 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500937 serial_out(up, UART_LCR, 0);
938 serial_out(up, UART_MCR, status1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if ((status2 ^ status1) & UART_MCR_LOOP) {
David Woodhouse857dde22005-05-21 15:52:23 +0100941 unsigned short quot;
942
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500943 serial_out(up, UART_LCR, 0xE0);
David Woodhouse857dde22005-05-21 15:52:23 +0100944
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100945 quot = serial_dl_read(up);
David Woodhouse857dde22005-05-21 15:52:23 +0100946 quot <<= 3;
947
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800948 if (ns16550a_goto_highspeed(up))
949 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500951 serial_out(up, UART_LCR, 0);
David Woodhouse857dde22005-05-21 15:52:23 +0100952
953 up->port.uartclk = 921600*16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 up->port.type = PORT_NS16550A;
955 up->capabilities |= UART_NATSEMI;
956 return;
957 }
958 }
959
960 /*
961 * No EFR. Try to detect a TI16750, which only sets bit 5 of
962 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
963 * Try setting it with and without DLAB set. Cheap clones
964 * set bit 5 without DLAB set.
965 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500966 serial_out(up, UART_LCR, 0);
967 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 status1 = serial_in(up, UART_IIR) >> 5;
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500969 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
970 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
971 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 status2 = serial_in(up, UART_IIR) >> 5;
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500973 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
974 serial_out(up, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
977
978 if (status1 == 6 && status2 == 7) {
979 up->port.type = PORT_16750;
980 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
981 return;
982 }
983
984 /*
985 * Try writing and reading the UART_IER_UUE bit (b6).
986 * If it works, this is probably one of the Xscale platform's
987 * internal UARTs.
988 * We're going to explicitly set the UUE bit to 0 before
989 * trying to write and read a 1 just to make sure it's not
990 * already a 1 and maybe locked there before we even start start.
991 */
992 iersave = serial_in(up, UART_IER);
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500993 serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
995 /*
996 * OK it's in a known zero state, try writing and reading
997 * without disturbing the current state of the other bits.
998 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -0500999 serial_out(up, UART_IER, iersave | UART_IER_UUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1001 /*
1002 * It's an Xscale.
1003 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1004 */
1005 DEBUG_AUTOCONF("Xscale ");
1006 up->port.type = PORT_XSCALE;
Stephen Warren55681812011-06-17 09:45:07 -06001007 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return;
1009 }
1010 } else {
1011 /*
1012 * If we got here we couldn't force the IER_UUE bit to 0.
1013 * Log it and continue.
1014 */
1015 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1016 }
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001017 serial_out(up, UART_IER, iersave);
Philippe Langlais235dae52010-07-29 17:13:57 +02001018
1019 /*
Søren Holm06315342011-09-02 22:55:37 +02001020 * Exar uarts have EFR in a weird location
1021 */
1022 if (up->port.flags & UPF_EXAR_EFR) {
1023 up->port.type = PORT_XR17D15X;
1024 up->capabilities |= UART_CAP_AFE | UART_CAP_EFR;
1025 }
1026
1027 /*
Philippe Langlais235dae52010-07-29 17:13:57 +02001028 * We distinguish between 16550A and U6 16550A by counting
1029 * how many bytes are in the FIFO.
1030 */
1031 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1032 up->port.type = PORT_U6_16550A;
1033 up->capabilities |= UART_CAP_AFE;
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037/*
1038 * This routine is called by rs_init() to initialize a specific serial
1039 * port. It determines what type of UART chip this serial port is
1040 * using: 8250, 16450, 16550, 16550A. The important question is
1041 * whether or not this UART is a 16550A or not, since this will
1042 * determine whether or not we can use its FIFO features or not.
1043 */
1044static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
1045{
1046 unsigned char status1, scratch, scratch2, scratch3;
1047 unsigned char save_lcr, save_mcr;
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001048 struct uart_port *port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 unsigned long flags;
1050
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001051 if (!port->iobase && !port->mapbase && !port->membase)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return;
1053
Lennert Buytenhek80647b92009-11-11 14:26:41 -08001054 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001055 serial_index(port), port->iobase, port->membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 /*
1058 * We really do need global IRQs disabled here - we're going to
1059 * be frobbing the chips IRQ enable register to see if it exists.
1060 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001061 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 up->capabilities = 0;
Russell King4ba5e352005-06-23 10:43:04 +01001064 up->bugs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001066 if (!(port->flags & UPF_BUGGY_UART)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /*
1068 * Do a simple existence test first; if we fail this,
1069 * there's no point trying anything else.
Thomas Koellerbd71c182007-05-06 14:48:47 -07001070 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 * 0x80 is used as a nonsense port to prevent against
1072 * false positives due to ISA bus float. The
1073 * assumption is that 0x80 is a non-existent port;
1074 * which should be safe since include/asm/io.h also
1075 * makes this assumption.
1076 *
1077 * Note: this is safe as long as MCR bit 4 is clear
1078 * and the device is in "PC" mode.
1079 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001080 scratch = serial_in(up, UART_IER);
1081 serial_out(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082#ifdef __i386__
1083 outb(0xff, 0x080);
1084#endif
Thomas Hoehn48212002007-02-10 01:46:05 -08001085 /*
1086 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1087 * 16C754B) allow only to modify them if an EFR bit is set.
1088 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001089 scratch2 = serial_in(up, UART_IER) & 0x0f;
1090 serial_out(up, UART_IER, 0x0F);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091#ifdef __i386__
1092 outb(0, 0x080);
1093#endif
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001094 scratch3 = serial_in(up, UART_IER) & 0x0f;
1095 serial_out(up, UART_IER, scratch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (scratch2 != 0 || scratch3 != 0x0F) {
1097 /*
1098 * We failed; there's nothing here
1099 */
1100 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1101 scratch2, scratch3);
1102 goto out;
1103 }
1104 }
1105
1106 save_mcr = serial_in(up, UART_MCR);
1107 save_lcr = serial_in(up, UART_LCR);
1108
Thomas Koellerbd71c182007-05-06 14:48:47 -07001109 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 * Check to see if a UART is really there. Certain broken
1111 * internal modems based on the Rockwell chipset fail this
1112 * test, because they apparently don't implement the loopback
1113 * test mode. So this test is skipped on the COM 1 through
1114 * COM 4 ports. This *should* be safe, since no board
1115 * manufacturer would be stupid enough to design a board
1116 * that conflicts with COM 1-4 --- we hope!
1117 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001118 if (!(port->flags & UPF_SKIP_TEST)) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001119 serial_out(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1120 status1 = serial_in(up, UART_MSR) & 0xF0;
1121 serial_out(up, UART_MCR, save_mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (status1 != 0x90) {
1123 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1124 status1);
1125 goto out;
1126 }
1127 }
1128
1129 /*
1130 * We're pretty sure there's a port here. Lets find out what
1131 * type of port it is. The IIR top two bits allows us to find
Russell King6f0d6182005-09-09 16:17:58 +01001132 * out if it's 8250 or 16450, 16550, 16550A or later. This
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 * determines what we test for next.
1134 *
1135 * We also initialise the EFR (if any) to zero for later. The
1136 * EFR occupies the same register location as the FCR and IIR.
1137 */
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001138 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1139 serial_out(up, UART_EFR, 0);
1140 serial_out(up, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001142 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 scratch = serial_in(up, UART_IIR) >> 6;
1144
1145 DEBUG_AUTOCONF("iir=%d ", scratch);
1146
1147 switch (scratch) {
1148 case 0:
1149 autoconfig_8250(up);
1150 break;
1151 case 1:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001152 port->type = PORT_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 break;
1154 case 2:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001155 port->type = PORT_16550;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 break;
1157 case 3:
1158 autoconfig_16550a(up);
1159 break;
1160 }
1161
1162#ifdef CONFIG_SERIAL_8250_RSA
1163 /*
1164 * Only probe for RSA ports if we got the region.
1165 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001166 if (port->type == PORT_16550A && probeflags & PROBE_RSA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 int i;
1168
1169 for (i = 0 ; i < probe_rsa_count; ++i) {
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001170 if (probe_rsa[i] == port->iobase && __enable_rsa(up)) {
1171 port->type = PORT_RSA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 break;
1173 }
1174 }
1175 }
1176#endif
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001177
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001178 serial_out(up, UART_LCR, save_lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001180 if (up->capabilities != uart_config[port->type].flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 printk(KERN_WARNING
1182 "ttyS%d: detected caps %08x should be %08x\n",
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001183 serial_index(port), up->capabilities,
1184 uart_config[port->type].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001187 port->fifosize = uart_config[up->port.type].fifo_size;
1188 up->capabilities = uart_config[port->type].flags;
1189 up->tx_loadsz = uart_config[port->type].tx_loadsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001191 if (port->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 goto out;
1193
1194 /*
1195 * Reset the UART.
1196 */
1197#ifdef CONFIG_SERIAL_8250_RSA
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001198 if (port->type == PORT_RSA)
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001199 serial_out(up, UART_RSA_FRR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200#endif
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001201 serial_out(up, UART_MCR, save_mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 serial8250_clear_fifos(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001203 serial_in(up, UART_RX);
Lennert Buytenhek5c8c7552005-11-12 21:58:05 +00001204 if (up->capabilities & UART_CAP_UUE)
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001205 serial_out(up, UART_IER, UART_IER_UUE);
Lennert Buytenhek5c8c7552005-11-12 21:58:05 +00001206 else
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001207 serial_out(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Thomas Koellerbd71c182007-05-06 14:48:47 -07001209 out:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001210 spin_unlock_irqrestore(&port->lock, flags);
1211 DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214static void autoconfig_irq(struct uart_8250_port *up)
1215{
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001216 struct uart_port *port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 unsigned char save_mcr, save_ier;
1218 unsigned char save_ICP = 0;
1219 unsigned int ICP = 0;
1220 unsigned long irqs;
1221 int irq;
1222
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001223 if (port->flags & UPF_FOURPORT) {
1224 ICP = (port->iobase & 0xfe0) | 0x1f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 save_ICP = inb_p(ICP);
1226 outb_p(0x80, ICP);
Paul Gortmaker0d263a22012-03-08 19:12:10 -05001227 inb_p(ICP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229
1230 /* forget possible initially masked and pending IRQ */
1231 probe_irq_off(probe_irq_on());
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001232 save_mcr = serial_in(up, UART_MCR);
1233 save_ier = serial_in(up, UART_IER);
1234 serial_out(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
Thomas Koellerbd71c182007-05-06 14:48:47 -07001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 irqs = probe_irq_on();
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001237 serial_out(up, UART_MCR, 0);
Alan Cox6f803cd2008-02-08 04:18:52 -08001238 udelay(10);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001239 if (port->flags & UPF_FOURPORT) {
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001240 serial_out(up, UART_MCR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 UART_MCR_DTR | UART_MCR_RTS);
1242 } else {
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001243 serial_out(up, UART_MCR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1245 }
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001246 serial_out(up, UART_IER, 0x0f); /* enable all intrs */
Paul Gortmaker0d263a22012-03-08 19:12:10 -05001247 serial_in(up, UART_LSR);
1248 serial_in(up, UART_RX);
1249 serial_in(up, UART_IIR);
1250 serial_in(up, UART_MSR);
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001251 serial_out(up, UART_TX, 0xFF);
Alan Cox6f803cd2008-02-08 04:18:52 -08001252 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 irq = probe_irq_off(irqs);
1254
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001255 serial_out(up, UART_MCR, save_mcr);
1256 serial_out(up, UART_IER, save_ier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001258 if (port->flags & UPF_FOURPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 outb_p(save_ICP, ICP);
1260
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001261 port->irq = (irq > 0) ? irq : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Russell Kinge763b902005-06-29 18:41:51 +01001264static inline void __stop_tx(struct uart_8250_port *p)
1265{
1266 if (p->ier & UART_IER_THRI) {
1267 p->ier &= ~UART_IER_THRI;
1268 serial_out(p, UART_IER, p->ier);
1269 }
1270}
1271
Russell Kingb129a8c2005-08-31 10:12:14 +01001272static void serial8250_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Jamie Iles49d57412010-12-01 23:39:35 +00001274 struct uart_8250_port *up =
1275 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Russell Kinge763b902005-06-29 18:41:51 +01001277 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 /*
Russell Kinge763b902005-06-29 18:41:51 +01001280 * We really want to stop the transmitter from sending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001282 if (port->type == PORT_16C950) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 up->acr |= UART_ACR_TXDIS;
1284 serial_icr_write(up, UART_ACR, up->acr);
1285 }
1286}
1287
Russell Kingb129a8c2005-08-31 10:12:14 +01001288static void serial8250_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Jamie Iles49d57412010-12-01 23:39:35 +00001290 struct uart_8250_port *up =
1291 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 if (!(up->ier & UART_IER_THRI)) {
1294 up->ier |= UART_IER_THRI;
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001295 serial_port_out(port, UART_IER, up->ier);
Russell King55d3b282005-06-23 15:05:41 +01001296
Russell King67f76542005-06-23 22:26:43 +01001297 if (up->bugs & UART_BUG_TXEN) {
Ian Jackson68cb4f82009-11-18 11:08:11 +01001298 unsigned char lsr;
Russell King55d3b282005-06-23 15:05:41 +01001299 lsr = serial_in(up, UART_LSR);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001300 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001301 if ((port->type == PORT_RM9000) ?
Ian Jackson68cb4f82009-11-18 11:08:11 +01001302 (lsr & UART_LSR_THRE) :
1303 (lsr & UART_LSR_TEMT))
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001304 serial8250_tx_chars(up);
Russell King55d3b282005-06-23 15:05:41 +01001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
Russell Kinge763b902005-06-29 18:41:51 +01001307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /*
Russell Kinge763b902005-06-29 18:41:51 +01001309 * Re-enable the transmitter if we disabled it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001311 if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 up->acr &= ~UART_ACR_TXDIS;
1313 serial_icr_write(up, UART_ACR, up->acr);
1314 }
1315}
1316
1317static void serial8250_stop_rx(struct uart_port *port)
1318{
Jamie Iles49d57412010-12-01 23:39:35 +00001319 struct uart_8250_port *up =
1320 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 up->ier &= ~UART_IER_RLSI;
1323 up->port.read_status_mask &= ~UART_LSR_DR;
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001324 serial_port_out(port, UART_IER, up->ier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
1327static void serial8250_enable_ms(struct uart_port *port)
1328{
Jamie Iles49d57412010-12-01 23:39:35 +00001329 struct uart_8250_port *up =
1330 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001332 /* no MSR capabilities */
1333 if (up->bugs & UART_BUG_NOMSR)
1334 return;
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 up->ier |= UART_IER_MSI;
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001337 serial_port_out(port, UART_IER, up->ier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Stephen Warren5f873ba2011-05-17 16:12:37 -06001340/*
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001341 * serial8250_rx_chars: processes according to the passed in LSR
Paul Gortmaker0690f412011-12-04 18:42:19 -05001342 * value, and returns the remaining LSR bits not handled
1343 * by this Rx routine.
1344 */
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001345unsigned char
1346serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001348 struct uart_port *port = &up->port;
1349 struct tty_struct *tty = port->state->port.tty;
Paul Gortmaker0690f412011-12-04 18:42:19 -05001350 unsigned char ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 int max_count = 256;
1352 char flag;
1353
1354 do {
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001355 if (likely(lsr & UART_LSR_DR))
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001356 ch = serial_in(up, UART_RX);
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001357 else
1358 /*
1359 * Intel 82571 has a Serial Over Lan device that will
1360 * set UART_LSR_BI without setting UART_LSR_DR when
1361 * it receives a break. To avoid reading from the
1362 * receive buffer without UART_LSR_DR bit set, we
1363 * just force the read character to be 0
1364 */
1365 ch = 0;
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 flag = TTY_NORMAL;
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001368 port->icount.rx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001370 lsr |= up->lsr_saved_flags;
1371 up->lsr_saved_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001373 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (lsr & UART_LSR_BI) {
1375 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001376 port->icount.brk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 /*
1378 * We do the SysRQ and SAK checking
1379 * here because otherwise the break
1380 * may get masked by ignore_status_mask
1381 * or read_status_mask.
1382 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001383 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 goto ignore_char;
1385 } else if (lsr & UART_LSR_PE)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001386 port->icount.parity++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 else if (lsr & UART_LSR_FE)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001388 port->icount.frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (lsr & UART_LSR_OE)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001390 port->icount.overrun++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /*
Russell King23907eb2005-04-16 15:26:39 -07001393 * Mask off conditions which should be ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001395 lsr &= port->read_status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 if (lsr & UART_LSR_BI) {
1398 DEBUG_INTR("handling break....");
1399 flag = TTY_BREAK;
1400 } else if (lsr & UART_LSR_PE)
1401 flag = TTY_PARITY;
1402 else if (lsr & UART_LSR_FE)
1403 flag = TTY_FRAME;
1404 }
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001405 if (uart_handle_sysrq_char(port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 goto ignore_char;
Russell King05ab3012005-05-09 23:21:59 +01001407
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001408 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
Russell King05ab3012005-05-09 23:21:59 +01001409
Alan Cox6f803cd2008-02-08 04:18:52 -08001410ignore_char:
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001411 lsr = serial_in(up, UART_LSR);
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001412 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001413 spin_unlock(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 tty_flip_buffer_push(tty);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001415 spin_lock(&port->lock);
Paul Gortmaker0690f412011-12-04 18:42:19 -05001416 return lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001418EXPORT_SYMBOL_GPL(serial8250_rx_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001420void serial8250_tx_chars(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001422 struct uart_port *port = &up->port;
1423 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 int count;
1425
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001426 if (port->x_char) {
1427 serial_out(up, UART_TX, port->x_char);
1428 port->icount.tx++;
1429 port->x_char = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return;
1431 }
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001432 if (uart_tx_stopped(port)) {
1433 serial8250_stop_tx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001434 return;
1435 }
1436 if (uart_circ_empty(xmit)) {
Russell Kinge763b902005-06-29 18:41:51 +01001437 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return;
1439 }
1440
1441 count = up->tx_loadsz;
1442 do {
1443 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1444 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001445 port->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (uart_circ_empty(xmit))
1447 break;
1448 } while (--count > 0);
1449
1450 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001451 uart_write_wakeup(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 DEBUG_INTR("THRE...");
1454
1455 if (uart_circ_empty(xmit))
Russell Kinge763b902005-06-29 18:41:51 +01001456 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001458EXPORT_SYMBOL_GPL(serial8250_tx_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001460unsigned int serial8250_modem_status(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001462 struct uart_port *port = &up->port;
Russell King2af7cd62006-01-04 16:55:09 +00001463 unsigned int status = serial_in(up, UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001465 status |= up->msr_saved_flags;
1466 up->msr_saved_flags = 0;
Taku Izumifdc30b32007-04-23 14:41:00 -07001467 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001468 port->state != NULL) {
Russell King2af7cd62006-01-04 16:55:09 +00001469 if (status & UART_MSR_TERI)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001470 port->icount.rng++;
Russell King2af7cd62006-01-04 16:55:09 +00001471 if (status & UART_MSR_DDSR)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001472 port->icount.dsr++;
Russell King2af7cd62006-01-04 16:55:09 +00001473 if (status & UART_MSR_DDCD)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001474 uart_handle_dcd_change(port, status & UART_MSR_DCD);
Russell King2af7cd62006-01-04 16:55:09 +00001475 if (status & UART_MSR_DCTS)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001476 uart_handle_cts_change(port, status & UART_MSR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001478 wake_up_interruptible(&port->state->port.delta_msr_wait);
Russell King2af7cd62006-01-04 16:55:09 +00001479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Russell King2af7cd62006-01-04 16:55:09 +00001481 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001483EXPORT_SYMBOL_GPL(serial8250_modem_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485/*
1486 * This handles the interrupt from one port.
1487 */
Paul Gortmaker86b21192011-12-04 18:42:22 -05001488int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Paul Gortmaker0690f412011-12-04 18:42:19 -05001490 unsigned char status;
Jiri Kosina4bf36312007-04-23 14:41:21 -07001491 unsigned long flags;
Paul Gortmaker86b21192011-12-04 18:42:22 -05001492 struct uart_8250_port *up =
1493 container_of(port, struct uart_8250_port, port);
1494
1495 if (iir & UART_IIR_NO_INT)
1496 return 0;
Russell King45e24602006-01-04 19:19:06 +00001497
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001498 spin_lock_irqsave(&port->lock, flags);
Russell King45e24602006-01-04 19:19:06 +00001499
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001500 status = serial_port_in(port, UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 DEBUG_INTR("status = %x...", status);
1503
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001504 if (status & (UART_LSR_DR | UART_LSR_BI))
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001505 status = serial8250_rx_chars(up, status);
1506 serial8250_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (status & UART_LSR_THRE)
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001508 serial8250_tx_chars(up);
Russell King45e24602006-01-04 19:19:06 +00001509
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001510 spin_unlock_irqrestore(&port->lock, flags);
Paul Gortmaker86b21192011-12-04 18:42:22 -05001511 return 1;
Jamie Iles583d28e2011-08-15 10:17:52 +01001512}
Jamie Ilesc7a1bdc2011-08-26 19:04:49 +01001513EXPORT_SYMBOL_GPL(serial8250_handle_irq);
Jamie Iles583d28e2011-08-15 10:17:52 +01001514
1515static int serial8250_default_handle_irq(struct uart_port *port)
1516{
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001517 unsigned int iir = serial_port_in(port, UART_IIR);
Jamie Iles583d28e2011-08-15 10:17:52 +01001518
1519 return serial8250_handle_irq(port, iir);
1520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522/*
1523 * This is the serial driver's interrupt routine.
1524 *
1525 * Arjan thinks the old way was overly complex, so it got simplified.
1526 * Alan disagrees, saying that need the complexity to handle the weird
1527 * nature of ISA shared interrupts. (This is a special exception.)
1528 *
1529 * In order to handle ISA shared interrupts properly, we need to check
1530 * that all ports have been serviced, and therefore the ISA interrupt
1531 * line has been de-asserted.
1532 *
1533 * This means we need to loop through all ports. checking that they
1534 * don't have an interrupt pending.
1535 */
David Howells7d12e782006-10-05 14:55:46 +01001536static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 struct irq_info *i = dev_id;
1539 struct list_head *l, *end = NULL;
1540 int pass_counter = 0, handled = 0;
1541
1542 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1543
1544 spin_lock(&i->lock);
1545
1546 l = i->head;
1547 do {
1548 struct uart_8250_port *up;
Jamie Iles583d28e2011-08-15 10:17:52 +01001549 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 up = list_entry(l, struct uart_8250_port, list);
Jamie Iles583d28e2011-08-15 10:17:52 +01001552 port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Dan Williams49b532f2012-04-06 11:49:44 -07001554 if (port->handle_irq(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 handled = 1;
Marc St-Jeanbeab6972007-05-06 14:48:45 -07001556 end = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 } else if (end == NULL)
1558 end = l;
1559
1560 l = l->next;
1561
1562 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1563 /* If we hit this, we're dead. */
Daniel Drakecd3ecad2010-10-20 16:00:48 -07001564 printk_ratelimited(KERN_ERR
1565 "serial8250: too much work for irq%d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 break;
1567 }
1568 } while (l != end);
1569
1570 spin_unlock(&i->lock);
1571
1572 DEBUG_INTR("end.\n");
1573
1574 return IRQ_RETVAL(handled);
1575}
1576
1577/*
1578 * To support ISA shared interrupts, we need to have one interrupt
1579 * handler that ensures that the IRQ line has been deasserted
1580 * before returning. Failing to do this will result in the IRQ
1581 * line being stuck active, and, since ISA irqs are edge triggered,
1582 * no more IRQs will be seen.
1583 */
1584static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1585{
1586 spin_lock_irq(&i->lock);
1587
1588 if (!list_empty(i->head)) {
1589 if (i->head == &up->list)
1590 i->head = i->head->next;
1591 list_del(&up->list);
1592 } else {
1593 BUG_ON(i->head != &up->list);
1594 i->head = NULL;
1595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 spin_unlock_irq(&i->lock);
Alan Cox25db8ad2008-08-19 20:49:40 -07001597 /* List empty so throw away the hash node */
1598 if (i->head == NULL) {
1599 hlist_del(&i->node);
1600 kfree(i);
1601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
1603
1604static int serial_link_irq_chain(struct uart_8250_port *up)
1605{
Alan Cox25db8ad2008-08-19 20:49:40 -07001606 struct hlist_head *h;
1607 struct hlist_node *n;
1608 struct irq_info *i;
Thomas Gleixner40663cc2006-07-01 19:29:43 -07001609 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Alan Cox25db8ad2008-08-19 20:49:40 -07001611 mutex_lock(&hash_mutex);
1612
1613 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1614
1615 hlist_for_each(n, h) {
1616 i = hlist_entry(n, struct irq_info, node);
1617 if (i->irq == up->port.irq)
1618 break;
1619 }
1620
1621 if (n == NULL) {
1622 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
1623 if (i == NULL) {
1624 mutex_unlock(&hash_mutex);
1625 return -ENOMEM;
1626 }
1627 spin_lock_init(&i->lock);
1628 i->irq = up->port.irq;
1629 hlist_add_head(&i->node, h);
1630 }
1631 mutex_unlock(&hash_mutex);
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 spin_lock_irq(&i->lock);
1634
1635 if (i->head) {
1636 list_add(&up->list, i->head);
1637 spin_unlock_irq(&i->lock);
1638
1639 ret = 0;
1640 } else {
1641 INIT_LIST_HEAD(&up->list);
1642 i->head = &up->list;
1643 spin_unlock_irq(&i->lock);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07001644 irq_flags |= up->port.irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 ret = request_irq(up->port.irq, serial8250_interrupt,
1646 irq_flags, "serial", i);
1647 if (ret < 0)
1648 serial_do_unlink(i, up);
1649 }
1650
1651 return ret;
1652}
1653
1654static void serial_unlink_irq_chain(struct uart_8250_port *up)
1655{
Alan Cox25db8ad2008-08-19 20:49:40 -07001656 struct irq_info *i;
1657 struct hlist_node *n;
1658 struct hlist_head *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Alan Cox25db8ad2008-08-19 20:49:40 -07001660 mutex_lock(&hash_mutex);
1661
1662 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1663
1664 hlist_for_each(n, h) {
1665 i = hlist_entry(n, struct irq_info, node);
1666 if (i->irq == up->port.irq)
1667 break;
1668 }
1669
1670 BUG_ON(n == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 BUG_ON(i->head == NULL);
1672
1673 if (list_empty(i->head))
1674 free_irq(up->port.irq, i);
1675
1676 serial_do_unlink(i, up);
Alan Cox25db8ad2008-08-19 20:49:40 -07001677 mutex_unlock(&hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
1680/*
1681 * This function is used to handle ports that do not have an
1682 * interrupt. This doesn't work very well for 16450's, but gives
1683 * barely passable results for a 16550A. (Although at the expense
1684 * of much CPU overhead).
1685 */
1686static void serial8250_timeout(unsigned long data)
1687{
1688 struct uart_8250_port *up = (struct uart_8250_port *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Paul Gortmakera0431472011-12-04 18:42:21 -05001690 up->port.handle_irq(&up->port);
Anton Vorontsov54381062010-10-01 17:21:25 +04001691 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
Alex Williamson40b36da2007-02-14 00:33:04 -08001692}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Alex Williamson40b36da2007-02-14 00:33:04 -08001694static void serial8250_backup_timeout(unsigned long data)
1695{
1696 struct uart_8250_port *up = (struct uart_8250_port *)data;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001697 unsigned int iir, ier = 0, lsr;
1698 unsigned long flags;
Alex Williamson40b36da2007-02-14 00:33:04 -08001699
Al Cooperdbb3b1c2011-07-25 16:19:52 -04001700 spin_lock_irqsave(&up->port.lock, flags);
1701
Alex Williamson40b36da2007-02-14 00:33:04 -08001702 /*
1703 * Must disable interrupts or else we risk racing with the interrupt
1704 * based handler.
1705 */
Alan Coxd4e33fa2012-01-26 17:44:09 +00001706 if (up->port.irq) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001707 ier = serial_in(up, UART_IER);
1708 serial_out(up, UART_IER, 0);
1709 }
1710
1711 iir = serial_in(up, UART_IIR);
1712
1713 /*
1714 * This should be a safe test for anyone who doesn't trust the
1715 * IIR bits on their UART, but it's specifically designed for
1716 * the "Diva" UART used on the management processor on many HP
1717 * ia64 and parisc boxes.
1718 */
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001719 lsr = serial_in(up, UART_LSR);
1720 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -08001721 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
Alan Coxebd2c8f2009-09-19 13:13:28 -07001722 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001723 (lsr & UART_LSR_THRE)) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001724 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1725 iir |= UART_IIR_THRI;
1726 }
1727
1728 if (!(iir & UART_IIR_NO_INT))
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001729 serial8250_tx_chars(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001730
Alan Coxd4e33fa2012-01-26 17:44:09 +00001731 if (up->port.irq)
Alex Williamson40b36da2007-02-14 00:33:04 -08001732 serial_out(up, UART_IER, ier);
1733
Al Cooperdbb3b1c2011-07-25 16:19:52 -04001734 spin_unlock_irqrestore(&up->port.lock, flags);
1735
Alex Williamson40b36da2007-02-14 00:33:04 -08001736 /* Standard timer interval plus 0.2s to keep the port running */
Alan Cox6f803cd2008-02-08 04:18:52 -08001737 mod_timer(&up->timer,
Anton Vorontsov54381062010-10-01 17:21:25 +04001738 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
1741static unsigned int serial8250_tx_empty(struct uart_port *port)
1742{
Jamie Iles49d57412010-12-01 23:39:35 +00001743 struct uart_8250_port *up =
1744 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 unsigned long flags;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001746 unsigned int lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001748 spin_lock_irqsave(&port->lock, flags);
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001749 lsr = serial_port_in(port, UART_LSR);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001750 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001751 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Dick Hollenbeckbca47612009-12-09 12:31:34 -08001753 return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
1756static unsigned int serial8250_get_mctrl(struct uart_port *port)
1757{
Jamie Iles49d57412010-12-01 23:39:35 +00001758 struct uart_8250_port *up =
1759 container_of(port, struct uart_8250_port, port);
Russell King2af7cd62006-01-04 16:55:09 +00001760 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 unsigned int ret;
1762
Paul Gortmaker3986fb22011-12-04 18:42:20 -05001763 status = serial8250_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 ret = 0;
1766 if (status & UART_MSR_DCD)
1767 ret |= TIOCM_CAR;
1768 if (status & UART_MSR_RI)
1769 ret |= TIOCM_RNG;
1770 if (status & UART_MSR_DSR)
1771 ret |= TIOCM_DSR;
1772 if (status & UART_MSR_CTS)
1773 ret |= TIOCM_CTS;
1774 return ret;
1775}
1776
1777static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1778{
Jamie Iles49d57412010-12-01 23:39:35 +00001779 struct uart_8250_port *up =
1780 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 unsigned char mcr = 0;
1782
1783 if (mctrl & TIOCM_RTS)
1784 mcr |= UART_MCR_RTS;
1785 if (mctrl & TIOCM_DTR)
1786 mcr |= UART_MCR_DTR;
1787 if (mctrl & TIOCM_OUT1)
1788 mcr |= UART_MCR_OUT1;
1789 if (mctrl & TIOCM_OUT2)
1790 mcr |= UART_MCR_OUT2;
1791 if (mctrl & TIOCM_LOOP)
1792 mcr |= UART_MCR_LOOP;
1793
1794 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1795
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001796 serial_port_out(port, UART_MCR, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
1799static void serial8250_break_ctl(struct uart_port *port, int break_state)
1800{
Jamie Iles49d57412010-12-01 23:39:35 +00001801 struct uart_8250_port *up =
1802 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 unsigned long flags;
1804
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001805 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (break_state == -1)
1807 up->lcr |= UART_LCR_SBC;
1808 else
1809 up->lcr &= ~UART_LCR_SBC;
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001810 serial_port_out(port, UART_LCR, up->lcr);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001811 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
Alex Williamson40b36da2007-02-14 00:33:04 -08001814/*
1815 * Wait for transmitter & holding register to empty
1816 */
Will Newtonb5d674a2008-10-13 10:36:21 +01001817static void wait_for_xmitr(struct uart_8250_port *up, int bits)
Alex Williamson40b36da2007-02-14 00:33:04 -08001818{
1819 unsigned int status, tmout = 10000;
1820
1821 /* Wait up to 10ms for the character(s) to be sent. */
David Daney97d303b2010-10-05 11:40:07 -07001822 for (;;) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001823 status = serial_in(up, UART_LSR);
1824
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001825 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -08001826
David Daney97d303b2010-10-05 11:40:07 -07001827 if ((status & bits) == bits)
1828 break;
Alex Williamson40b36da2007-02-14 00:33:04 -08001829 if (--tmout == 0)
1830 break;
1831 udelay(1);
David Daney97d303b2010-10-05 11:40:07 -07001832 }
Alex Williamson40b36da2007-02-14 00:33:04 -08001833
1834 /* Wait up to 1s for flow control if necessary */
1835 if (up->port.flags & UPF_CONS_FLOW) {
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001836 unsigned int tmout;
1837 for (tmout = 1000000; tmout; tmout--) {
1838 unsigned int msr = serial_in(up, UART_MSR);
1839 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1840 if (msr & UART_MSR_CTS)
1841 break;
Alex Williamson40b36da2007-02-14 00:33:04 -08001842 udelay(1);
1843 touch_nmi_watchdog();
1844 }
1845 }
1846}
1847
Jason Wesself2d937f2008-04-17 20:05:37 +02001848#ifdef CONFIG_CONSOLE_POLL
1849/*
1850 * Console polling routines for writing and reading from the uart while
1851 * in an interrupt or debug context.
1852 */
1853
1854static int serial8250_get_poll_char(struct uart_port *port)
1855{
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001856 unsigned char lsr = serial_port_in(port, UART_LSR);
Jason Wesself2d937f2008-04-17 20:05:37 +02001857
Jason Wesself5316b42010-05-20 21:04:22 -05001858 if (!(lsr & UART_LSR_DR))
1859 return NO_POLL_CHAR;
Jason Wesself2d937f2008-04-17 20:05:37 +02001860
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001861 return serial_port_in(port, UART_RX);
Jason Wesself2d937f2008-04-17 20:05:37 +02001862}
1863
1864
1865static void serial8250_put_poll_char(struct uart_port *port,
1866 unsigned char c)
1867{
1868 unsigned int ier;
Jamie Iles49d57412010-12-01 23:39:35 +00001869 struct uart_8250_port *up =
1870 container_of(port, struct uart_8250_port, port);
Jason Wesself2d937f2008-04-17 20:05:37 +02001871
1872 /*
1873 * First save the IER then disable the interrupts
1874 */
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001875 ier = serial_port_in(port, UART_IER);
Jason Wesself2d937f2008-04-17 20:05:37 +02001876 if (up->capabilities & UART_CAP_UUE)
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001877 serial_port_out(port, UART_IER, UART_IER_UUE);
Jason Wesself2d937f2008-04-17 20:05:37 +02001878 else
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001879 serial_port_out(port, UART_IER, 0);
Jason Wesself2d937f2008-04-17 20:05:37 +02001880
1881 wait_for_xmitr(up, BOTH_EMPTY);
1882 /*
1883 * Send the character out.
1884 * If a LF, also do CR...
1885 */
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001886 serial_port_out(port, UART_TX, c);
Jason Wesself2d937f2008-04-17 20:05:37 +02001887 if (c == 10) {
1888 wait_for_xmitr(up, BOTH_EMPTY);
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001889 serial_port_out(port, UART_TX, 13);
Jason Wesself2d937f2008-04-17 20:05:37 +02001890 }
1891
1892 /*
1893 * Finally, wait for transmitter to become empty
1894 * and restore the IER
1895 */
1896 wait_for_xmitr(up, BOTH_EMPTY);
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001897 serial_port_out(port, UART_IER, ier);
Jason Wesself2d937f2008-04-17 20:05:37 +02001898}
1899
1900#endif /* CONFIG_CONSOLE_POLL */
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902static int serial8250_startup(struct uart_port *port)
1903{
Jamie Iles49d57412010-12-01 23:39:35 +00001904 struct uart_8250_port *up =
1905 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 unsigned long flags;
Russell King55d3b282005-06-23 15:05:41 +01001907 unsigned char lsr, iir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 int retval;
1909
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001910 port->fifosize = uart_config[up->port.type].fifo_size;
Ondrej Puzmane4f05af2010-12-04 21:17:38 +01001911 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 up->capabilities = uart_config[up->port.type].flags;
1913 up->mcr = 0;
1914
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001915 if (port->iotype != up->cur_iotype)
Alan Coxb8e7e402009-05-28 14:01:35 +01001916 set_io_from_upio(port);
1917
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001918 if (port->type == PORT_16C950) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 /* Wake up and initialize UART */
1920 up->acr = 0;
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001921 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
1922 serial_port_out(port, UART_EFR, UART_EFR_ECB);
1923 serial_port_out(port, UART_IER, 0);
1924 serial_port_out(port, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001926 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
1927 serial_port_out(port, UART_EFR, UART_EFR_ECB);
1928 serial_port_out(port, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930
1931#ifdef CONFIG_SERIAL_8250_RSA
1932 /*
1933 * If this is an RSA port, see if we can kick it up to the
1934 * higher speed clock.
1935 */
1936 enable_rsa(up);
1937#endif
1938
1939 /*
1940 * Clear the FIFO buffers and disable them.
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08001941 * (they will be reenabled in set_termios())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 */
1943 serial8250_clear_fifos(up);
1944
1945 /*
1946 * Clear the interrupt registers.
1947 */
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001948 serial_port_in(port, UART_LSR);
1949 serial_port_in(port, UART_RX);
1950 serial_port_in(port, UART_IIR);
1951 serial_port_in(port, UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 /*
1954 * At this point, there's no way the LSR could still be 0xff;
1955 * if it is, then bail out, because there's likely no UART
1956 * here.
1957 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001958 if (!(port->flags & UPF_BUGGY_UART) &&
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001959 (serial_port_in(port, UART_LSR) == 0xff)) {
Konrad Rzeszutek Wilk7808a4c2011-09-26 09:14:34 -04001960 printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001961 serial_index(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return -ENODEV;
1963 }
1964
1965 /*
1966 * For a XR16C850, we need to set the trigger levels
1967 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001968 if (port->type == PORT_16850) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 unsigned char fctr;
1970
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001971 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Paul Gortmaker0acf5192012-03-08 19:12:08 -05001973 fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001974 serial_port_out(port, UART_FCTR,
1975 fctr | UART_FCTR_TRGD | UART_FCTR_RX);
1976 serial_port_out(port, UART_TRG, UART_TRG_96);
1977 serial_port_out(port, UART_FCTR,
1978 fctr | UART_FCTR_TRGD | UART_FCTR_TX);
1979 serial_port_out(port, UART_TRG, UART_TRG_96);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05001981 serial_port_out(port, UART_LCR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001984 if (port->irq) {
Alex Williamson01c194d2008-04-28 02:14:09 -07001985 unsigned char iir1;
Alex Williamson40b36da2007-02-14 00:33:04 -08001986 /*
1987 * Test for UARTs that do not reassert THRE when the
1988 * transmitter is idle and the interrupt has already
1989 * been cleared. Real 16550s should always reassert
1990 * this interrupt whenever the transmitter is idle and
1991 * the interrupt is enabled. Delays are necessary to
1992 * allow register changes to become visible.
1993 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001994 spin_lock_irqsave(&port->lock, flags);
Vikram Pandita1c2f0492009-09-19 13:13:19 -07001995 if (up->port.irqflags & IRQF_SHARED)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05001996 disable_irq_nosync(port->irq);
Alex Williamson40b36da2007-02-14 00:33:04 -08001997
1998 wait_for_xmitr(up, UART_LSR_THRE);
Paul Gortmaker55e40162012-03-08 19:12:14 -05001999 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
Alex Williamson40b36da2007-02-14 00:33:04 -08002000 udelay(1); /* allow THRE to set */
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002001 iir1 = serial_port_in(port, UART_IIR);
2002 serial_port_out(port, UART_IER, 0);
Paul Gortmaker55e40162012-03-08 19:12:14 -05002003 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
Alex Williamson40b36da2007-02-14 00:33:04 -08002004 udelay(1); /* allow a working UART time to re-assert THRE */
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002005 iir = serial_port_in(port, UART_IIR);
2006 serial_port_out(port, UART_IER, 0);
Alex Williamson40b36da2007-02-14 00:33:04 -08002007
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002008 if (port->irqflags & IRQF_SHARED)
2009 enable_irq(port->irq);
2010 spin_unlock_irqrestore(&port->lock, flags);
Alex Williamson40b36da2007-02-14 00:33:04 -08002011
2012 /*
Dan Williamsbc02d152012-04-06 11:49:50 -07002013 * If the interrupt is not reasserted, or we otherwise
2014 * don't trust the iir, setup a timer to kick the UART
2015 * on a regular basis.
Alex Williamson40b36da2007-02-14 00:33:04 -08002016 */
Dan Williamsbc02d152012-04-06 11:49:50 -07002017 if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
2018 up->port.flags & UPF_BUG_THRE) {
Will Newton363f66f2008-09-02 14:35:44 -07002019 up->bugs |= UART_BUG_THRE;
David S. Miller84408382008-10-13 10:45:26 +01002020 pr_debug("ttyS%d - using backup timer\n",
2021 serial_index(port));
Alex Williamson40b36da2007-02-14 00:33:04 -08002022 }
2023 }
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 /*
Will Newton363f66f2008-09-02 14:35:44 -07002026 * The above check will only give an accurate result the first time
2027 * the port is opened so this value needs to be preserved.
2028 */
2029 if (up->bugs & UART_BUG_THRE) {
2030 up->timer.function = serial8250_backup_timeout;
2031 up->timer.data = (unsigned long)up;
2032 mod_timer(&up->timer, jiffies +
Anton Vorontsov54381062010-10-01 17:21:25 +04002033 uart_poll_timeout(port) + HZ / 5);
Will Newton363f66f2008-09-02 14:35:44 -07002034 }
2035
2036 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 * If the "interrupt" for this port doesn't correspond with any
2038 * hardware interrupt, we use a timer-based system. The original
2039 * driver used to do this with IRQ0.
2040 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002041 if (!port->irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 up->timer.data = (unsigned long)up;
Anton Vorontsov54381062010-10-01 17:21:25 +04002043 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 } else {
2045 retval = serial_link_irq_chain(up);
2046 if (retval)
2047 return retval;
2048 }
2049
2050 /*
2051 * Now, initialize the UART
2052 */
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002053 serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002055 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (up->port.flags & UPF_FOURPORT) {
Alan Coxd4e33fa2012-01-26 17:44:09 +00002057 if (!up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 up->port.mctrl |= TIOCM_OUT1;
2059 } else
2060 /*
2061 * Most PC uarts need OUT2 raised to enable interrupts.
2062 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002063 if (port->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 up->port.mctrl |= TIOCM_OUT2;
2065
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002066 serial8250_set_mctrl(port, port->mctrl);
Russell King55d3b282005-06-23 15:05:41 +01002067
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002068 /* Serial over Lan (SoL) hack:
2069 Intel 8257x Gigabit ethernet chips have a
2070 16550 emulation, to be used for Serial Over Lan.
2071 Those chips take a longer time than a normal
2072 serial device to signalize that a transmission
2073 data was queued. Due to that, the above test generally
2074 fails. One solution would be to delay the reading of
2075 iir. However, this is not reliable, since the timeout
2076 is variable. So, let's just don't test if we receive
2077 TX irq. This way, we'll never enable UART_BUG_TXEN.
2078 */
Chuck Ebbertd41a4b52009-10-01 15:44:26 -07002079 if (skip_txen_test || up->port.flags & UPF_NO_TXEN_TEST)
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002080 goto dont_test_tx_en;
2081
Russell King55d3b282005-06-23 15:05:41 +01002082 /*
2083 * Do a quick test to see if we receive an
2084 * interrupt when we enable the TX irq.
2085 */
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002086 serial_port_out(port, UART_IER, UART_IER_THRI);
2087 lsr = serial_port_in(port, UART_LSR);
2088 iir = serial_port_in(port, UART_IIR);
2089 serial_port_out(port, UART_IER, 0);
Russell King55d3b282005-06-23 15:05:41 +01002090
2091 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
Russell King67f76542005-06-23 22:26:43 +01002092 if (!(up->bugs & UART_BUG_TXEN)) {
2093 up->bugs |= UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002094 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
David S. Miller84408382008-10-13 10:45:26 +01002095 serial_index(port));
Russell King55d3b282005-06-23 15:05:41 +01002096 }
2097 } else {
Russell King67f76542005-06-23 22:26:43 +01002098 up->bugs &= ~UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002099 }
2100
Mauro Carvalho Chehabb6adea32009-02-20 15:38:52 -08002101dont_test_tx_en:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002102 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 /*
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002105 * Clear the interrupt registers again for luck, and clear the
2106 * saved flags to avoid getting false values from polling
2107 * routines or the previous session.
2108 */
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002109 serial_port_in(port, UART_LSR);
2110 serial_port_in(port, UART_RX);
2111 serial_port_in(port, UART_IIR);
2112 serial_port_in(port, UART_MSR);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002113 up->lsr_saved_flags = 0;
2114 up->msr_saved_flags = 0;
2115
2116 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 * Finally, enable interrupts. Note: Modem status interrupts
2118 * are set via set_termios(), which will be occurring imminently
2119 * anyway, so we don't enable them here.
2120 */
2121 up->ier = UART_IER_RLSI | UART_IER_RDI;
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002122 serial_port_out(port, UART_IER, up->ier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002124 if (port->flags & UPF_FOURPORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 unsigned int icp;
2126 /*
2127 * Enable interrupts on the AST Fourport board
2128 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002129 icp = (port->iobase & 0xfe0) | 0x01f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 outb_p(0x80, icp);
Paul Gortmaker0d263a22012-03-08 19:12:10 -05002131 inb_p(icp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return 0;
2135}
2136
2137static void serial8250_shutdown(struct uart_port *port)
2138{
Jamie Iles49d57412010-12-01 23:39:35 +00002139 struct uart_8250_port *up =
2140 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 unsigned long flags;
2142
2143 /*
2144 * Disable interrupts from this port
2145 */
2146 up->ier = 0;
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002147 serial_port_out(port, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002149 spin_lock_irqsave(&port->lock, flags);
2150 if (port->flags & UPF_FOURPORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 /* reset interrupts on the AST Fourport board */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002152 inb((port->iobase & 0xfe0) | 0x1f);
2153 port->mctrl |= TIOCM_OUT1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 } else
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002155 port->mctrl &= ~TIOCM_OUT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002157 serial8250_set_mctrl(port, port->mctrl);
2158 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 /*
2161 * Disable break condition and FIFOs
2162 */
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002163 serial_port_out(port, UART_LCR,
2164 serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 serial8250_clear_fifos(up);
2166
2167#ifdef CONFIG_SERIAL_8250_RSA
2168 /*
2169 * Reset the RSA board back to 115kbps compat mode.
2170 */
2171 disable_rsa(up);
2172#endif
2173
2174 /*
2175 * Read data port to reset things, and then unlink from
2176 * the IRQ chain.
2177 */
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002178 serial_port_in(port, UART_RX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Alex Williamson40b36da2007-02-14 00:33:04 -08002180 del_timer_sync(&up->timer);
2181 up->timer.function = serial8250_timeout;
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002182 if (port->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 serial_unlink_irq_chain(up);
2184}
2185
2186static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
2187{
2188 unsigned int quot;
2189
2190 /*
2191 * Handle magic divisors for baud rates above baud_base on
2192 * SMSC SuperIO chips.
2193 */
2194 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2195 baud == (port->uartclk/4))
2196 quot = 0x8001;
2197 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2198 baud == (port->uartclk/8))
2199 quot = 0x8002;
2200 else
2201 quot = uart_get_divisor(port, baud);
2202
2203 return quot;
2204}
2205
Philippe Langlais235dae52010-07-29 17:13:57 +02002206void
2207serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2208 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Jamie Iles49d57412010-12-01 23:39:35 +00002210 struct uart_8250_port *up =
2211 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 unsigned char cval, fcr = 0;
2213 unsigned long flags;
2214 unsigned int baud, quot;
2215
2216 switch (termios->c_cflag & CSIZE) {
2217 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +01002218 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 break;
2220 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +01002221 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 break;
2223 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +01002224 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 break;
2226 default:
2227 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +01002228 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 break;
2230 }
2231
2232 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +01002233 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (termios->c_cflag & PARENB)
2235 cval |= UART_LCR_PARITY;
2236 if (!(termios->c_cflag & PARODD))
2237 cval |= UART_LCR_EPAR;
2238#ifdef CMSPAR
2239 if (termios->c_cflag & CMSPAR)
2240 cval |= UART_LCR_SPAR;
2241#endif
2242
2243 /*
2244 * Ask the core to calculate the divisor for us.
2245 */
Anton Vorontsov24d481e2009-09-19 13:13:20 -07002246 baud = uart_get_baud_rate(port, termios, old,
2247 port->uartclk / 16 / 0xffff,
2248 port->uartclk / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 quot = serial8250_get_divisor(port, baud);
2250
2251 /*
Russell King4ba5e352005-06-23 10:43:04 +01002252 * Oxford Semi 952 rev B workaround
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 */
Russell King4ba5e352005-06-23 10:43:04 +01002254 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
Alan Cox3e8d4e22008-02-04 22:27:53 -08002255 quot++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002257 if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
Christian Melkif9a91112012-04-30 11:21:26 +02002258 fcr = uart_config[port->type].fcr;
2259 if (baud < 2400) {
2260 fcr &= ~UART_FCR_TRIGGER_MASK;
2261 fcr |= UART_FCR_TRIGGER_1;
2262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
2264
2265 /*
2266 * MCR-based auto flow control. When AFE is enabled, RTS will be
2267 * deasserted when the receive FIFO contains more characters than
2268 * the trigger, or the MCR RTS bit is cleared. In the case where
2269 * the remote UART is not using CTS auto flow control, we must
2270 * have sufficient FIFO entries for the latency of the remote
2271 * UART to respond. IOW, at least 32 bytes of FIFO.
2272 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002273 if (up->capabilities & UART_CAP_AFE && port->fifosize >= 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 up->mcr &= ~UART_MCR_AFE;
2275 if (termios->c_cflag & CRTSCTS)
2276 up->mcr |= UART_MCR_AFE;
2277 }
2278
2279 /*
2280 * Ok, we're now changing the port state. Do it with
2281 * interrupts disabled.
2282 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002283 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 /*
2286 * Update the per-port timeout.
2287 */
2288 uart_update_timeout(port, termios->c_cflag, baud);
2289
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002290 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (termios->c_iflag & INPCK)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002292 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (termios->c_iflag & (BRKINT | PARMRK))
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002294 port->read_status_mask |= UART_LSR_BI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 /*
2297 * Characteres to ignore
2298 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002299 port->ignore_status_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (termios->c_iflag & IGNPAR)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002301 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (termios->c_iflag & IGNBRK) {
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002303 port->ignore_status_mask |= UART_LSR_BI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 /*
2305 * If we're ignoring parity and break indicators,
2306 * ignore overruns too (for real raw support).
2307 */
2308 if (termios->c_iflag & IGNPAR)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002309 port->ignore_status_mask |= UART_LSR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
2311
2312 /*
2313 * ignore all characters if CREAD is not set
2314 */
2315 if ((termios->c_cflag & CREAD) == 0)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002316 port->ignore_status_mask |= UART_LSR_DR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 /*
2319 * CTS flow control flag and modem status interrupts
2320 */
Ingo Molnarf8b372a2010-11-13 16:21:58 +01002321 up->ier &= ~UART_IER_MSI;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002322 if (!(up->bugs & UART_BUG_NOMSR) &&
2323 UART_ENABLE_MS(&up->port, termios->c_cflag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 up->ier |= UART_IER_MSI;
2325 if (up->capabilities & UART_CAP_UUE)
Stephen Warren4539c242011-05-17 16:12:36 -06002326 up->ier |= UART_IER_UUE;
2327 if (up->capabilities & UART_CAP_RTOIE)
2328 up->ier |= UART_IER_RTOIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002330 serial_port_out(port, UART_IER, up->ier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 if (up->capabilities & UART_CAP_EFR) {
2333 unsigned char efr = 0;
2334 /*
2335 * TI16C752/Startech hardware flow control. FIXME:
2336 * - TI16C752 requires control thresholds to be set.
2337 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2338 */
2339 if (termios->c_cflag & CRTSCTS)
2340 efr |= UART_EFR_CTS;
2341
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002342 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002343 if (port->flags & UPF_EXAR_EFR)
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002344 serial_port_out(port, UART_XR_EFR, efr);
Søren Holm06315342011-09-02 22:55:37 +02002345 else
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002346 serial_port_out(port, UART_EFR, efr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
2348
Russell Kingf2eda272008-09-01 21:47:59 +01002349#ifdef CONFIG_ARCH_OMAP
Jonathan McDowell255341c2006-08-14 23:05:32 -07002350 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
Russell King56685452008-09-01 21:25:33 +01002351 if (cpu_is_omap1510() && is_omap_port(up)) {
Jonathan McDowell255341c2006-08-14 23:05:32 -07002352 if (baud == 115200) {
2353 quot = 1;
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002354 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
Jonathan McDowell255341c2006-08-14 23:05:32 -07002355 } else
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002356 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
Jonathan McDowell255341c2006-08-14 23:05:32 -07002357 }
2358#endif
2359
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002360 /*
2361 * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
2362 * otherwise just set DLAB
2363 */
2364 if (up->capabilities & UART_NATSEMI)
2365 serial_port_out(port, UART_LCR, 0xe0);
2366 else
2367 serial_port_out(port, UART_LCR, cval | UART_LCR_DLAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +01002369 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 /*
2372 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2373 * is written without DLAB set, this mode will be disabled.
2374 */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002375 if (port->type == PORT_16750)
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002376 serial_port_out(port, UART_FCR, fcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002378 serial_port_out(port, UART_LCR, cval); /* reset DLAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 up->lcr = cval; /* Save LCR */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002380 if (port->type != PORT_16750) {
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002381 /* emulated UARTs (Lucent Venus 167x) need two steps */
2382 if (fcr & UART_FCR_ENABLE_FIFO)
2383 serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
2384 serial_port_out(port, UART_FCR, fcr); /* set fcr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002386 serial8250_set_mctrl(port, port->mctrl);
2387 spin_unlock_irqrestore(&port->lock, flags);
Alan Coxe991a2b2008-04-28 02:14:06 -07002388 /* Don't rewrite B0 */
2389 if (tty_termios_baud_rate(termios))
2390 tty_termios_encode_baud_rate(termios, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
Philippe Langlais235dae52010-07-29 17:13:57 +02002392EXPORT_SYMBOL(serial8250_do_set_termios);
2393
2394static void
2395serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2396 struct ktermios *old)
2397{
2398 if (port->set_termios)
2399 port->set_termios(port, termios, old);
2400 else
2401 serial8250_do_set_termios(port, termios, old);
2402}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404static void
Arnd Bergmanna0821df2010-06-01 22:53:11 +02002405serial8250_set_ldisc(struct uart_port *port, int new)
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002406{
Arnd Bergmanna0821df2010-06-01 22:53:11 +02002407 if (new == N_PPS) {
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002408 port->flags |= UPF_HARDPPS_CD;
2409 serial8250_enable_ms(port);
2410 } else
2411 port->flags &= ~UPF_HARDPPS_CD;
2412}
2413
Manuel Laussc161afe2010-09-25 15:13:45 +02002414
2415void serial8250_do_pm(struct uart_port *port, unsigned int state,
2416 unsigned int oldstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
Jamie Iles49d57412010-12-01 23:39:35 +00002418 struct uart_8250_port *p =
2419 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 serial8250_set_sleep(p, state != 0);
Manuel Laussc161afe2010-09-25 15:13:45 +02002422}
2423EXPORT_SYMBOL(serial8250_do_pm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Manuel Laussc161afe2010-09-25 15:13:45 +02002425static void
2426serial8250_pm(struct uart_port *port, unsigned int state,
2427 unsigned int oldstate)
2428{
2429 if (port->pm)
2430 port->pm(port, state, oldstate);
2431 else
2432 serial8250_do_pm(port, state, oldstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
2434
Russell Kingf2eda272008-09-01 21:47:59 +01002435static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2436{
2437 if (pt->port.iotype == UPIO_AU)
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002438 return 0x1000;
Russell Kingf2eda272008-09-01 21:47:59 +01002439#ifdef CONFIG_ARCH_OMAP
2440 if (is_omap_port(pt))
2441 return 0x16 << pt->port.regshift;
2442#endif
2443 return 8 << pt->port.regshift;
2444}
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446/*
2447 * Resource handling.
2448 */
2449static int serial8250_request_std_resource(struct uart_8250_port *up)
2450{
Russell Kingf2eda272008-09-01 21:47:59 +01002451 unsigned int size = serial8250_port_size(up);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002452 struct uart_port *port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 int ret = 0;
2454
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002455 switch (port->iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002456 case UPIO_AU:
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002457 case UPIO_TSI:
2458 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 case UPIO_MEM:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002460 if (!port->mapbase)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 break;
2462
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002463 if (!request_mem_region(port->mapbase, size, "serial")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 ret = -EBUSY;
2465 break;
2466 }
2467
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002468 if (port->flags & UPF_IOREMAP) {
2469 port->membase = ioremap_nocache(port->mapbase, size);
2470 if (!port->membase) {
2471 release_mem_region(port->mapbase, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 ret = -ENOMEM;
2473 }
2474 }
2475 break;
2476
2477 case UPIO_HUB6:
2478 case UPIO_PORT:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002479 if (!request_region(port->iobase, size, "serial"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 ret = -EBUSY;
2481 break;
2482 }
2483 return ret;
2484}
2485
2486static void serial8250_release_std_resource(struct uart_8250_port *up)
2487{
Russell Kingf2eda272008-09-01 21:47:59 +01002488 unsigned int size = serial8250_port_size(up);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002489 struct uart_port *port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002491 switch (port->iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002492 case UPIO_AU:
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002493 case UPIO_TSI:
2494 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 case UPIO_MEM:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002496 if (!port->mapbase)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 break;
2498
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002499 if (port->flags & UPF_IOREMAP) {
2500 iounmap(port->membase);
2501 port->membase = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 }
2503
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002504 release_mem_region(port->mapbase, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 break;
2506
2507 case UPIO_HUB6:
2508 case UPIO_PORT:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002509 release_region(port->iobase, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 break;
2511 }
2512}
2513
2514static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2515{
2516 unsigned long start = UART_RSA_BASE << up->port.regshift;
2517 unsigned int size = 8 << up->port.regshift;
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002518 struct uart_port *port = &up->port;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002519 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002521 switch (port->iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 case UPIO_HUB6:
2523 case UPIO_PORT:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002524 start += port->iobase;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002525 if (request_region(start, size, "serial-rsa"))
2526 ret = 0;
2527 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 ret = -EBUSY;
2529 break;
2530 }
2531
2532 return ret;
2533}
2534
2535static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2536{
2537 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2538 unsigned int size = 8 << up->port.regshift;
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002539 struct uart_port *port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002541 switch (port->iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 case UPIO_HUB6:
2543 case UPIO_PORT:
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002544 release_region(port->iobase + offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 break;
2546 }
2547}
2548
2549static void serial8250_release_port(struct uart_port *port)
2550{
Jamie Iles49d57412010-12-01 23:39:35 +00002551 struct uart_8250_port *up =
2552 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 serial8250_release_std_resource(up);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002555 if (port->type == PORT_RSA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 serial8250_release_rsa_resource(up);
2557}
2558
2559static int serial8250_request_port(struct uart_port *port)
2560{
Jamie Iles49d57412010-12-01 23:39:35 +00002561 struct uart_8250_port *up =
2562 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 int ret = 0;
2564
2565 ret = serial8250_request_std_resource(up);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002566 if (ret == 0 && port->type == PORT_RSA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 ret = serial8250_request_rsa_resource(up);
2568 if (ret < 0)
2569 serial8250_release_std_resource(up);
2570 }
2571
2572 return ret;
2573}
2574
2575static void serial8250_config_port(struct uart_port *port, int flags)
2576{
Jamie Iles49d57412010-12-01 23:39:35 +00002577 struct uart_8250_port *up =
2578 container_of(port, struct uart_8250_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 int probeflags = PROBE_ANY;
2580 int ret;
2581
2582 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 * Find the region that we can probe for. This in turn
2584 * tells us whether we can probe for the type of port.
2585 */
2586 ret = serial8250_request_std_resource(up);
2587 if (ret < 0)
2588 return;
2589
2590 ret = serial8250_request_rsa_resource(up);
2591 if (ret < 0)
2592 probeflags &= ~PROBE_RSA;
2593
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002594 if (port->iotype != up->cur_iotype)
Alan Coxb8e7e402009-05-28 14:01:35 +01002595 set_io_from_upio(port);
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 if (flags & UART_CONFIG_TYPE)
2598 autoconfig(up, probeflags);
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002599
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002600 /* if access method is AU, it is a 16550 with a quirk */
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002601 if (port->type == PORT_16550A && port->iotype == UPIO_AU)
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002602 up->bugs |= UART_BUG_NOMSR;
Manuel Laussb2b13cd2009-10-28 21:37:28 +01002603
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002604 if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 autoconfig_irq(up);
2606
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002607 if (port->type != PORT_RSA && probeflags & PROBE_RSA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 serial8250_release_rsa_resource(up);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002609 if (port->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 serial8250_release_std_resource(up);
2611}
2612
2613static int
2614serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2615{
Yinghai Lua62c4132008-08-19 20:49:55 -07002616 if (ser->irq >= nr_irqs || ser->irq < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2618 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2619 ser->type == PORT_STARTECH)
2620 return -EINVAL;
2621 return 0;
2622}
2623
2624static const char *
2625serial8250_type(struct uart_port *port)
2626{
2627 int type = port->type;
2628
2629 if (type >= ARRAY_SIZE(uart_config))
2630 type = 0;
2631 return uart_config[type].name;
2632}
2633
2634static struct uart_ops serial8250_pops = {
2635 .tx_empty = serial8250_tx_empty,
2636 .set_mctrl = serial8250_set_mctrl,
2637 .get_mctrl = serial8250_get_mctrl,
2638 .stop_tx = serial8250_stop_tx,
2639 .start_tx = serial8250_start_tx,
2640 .stop_rx = serial8250_stop_rx,
2641 .enable_ms = serial8250_enable_ms,
2642 .break_ctl = serial8250_break_ctl,
2643 .startup = serial8250_startup,
2644 .shutdown = serial8250_shutdown,
2645 .set_termios = serial8250_set_termios,
Rodolfo Giomettidc77f162010-03-10 15:23:48 -08002646 .set_ldisc = serial8250_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 .pm = serial8250_pm,
2648 .type = serial8250_type,
2649 .release_port = serial8250_release_port,
2650 .request_port = serial8250_request_port,
2651 .config_port = serial8250_config_port,
2652 .verify_port = serial8250_verify_port,
Jason Wesself2d937f2008-04-17 20:05:37 +02002653#ifdef CONFIG_CONSOLE_POLL
2654 .poll_get_char = serial8250_get_poll_char,
2655 .poll_put_char = serial8250_put_poll_char,
2656#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657};
2658
2659static struct uart_8250_port serial8250_ports[UART_NR];
2660
Alan Coxaf7f3742010-10-18 11:38:02 -07002661static void (*serial8250_isa_config)(int port, struct uart_port *up,
2662 unsigned short *capabilities);
2663
2664void serial8250_set_isa_configurator(
2665 void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
2666{
2667 serial8250_isa_config = v;
2668}
2669EXPORT_SYMBOL(serial8250_set_isa_configurator);
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671static void __init serial8250_isa_init_ports(void)
2672{
2673 struct uart_8250_port *up;
2674 static int first = 1;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002675 int i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
2677 if (!first)
2678 return;
2679 first = 0;
2680
Dave Jonesa61c2d72006-01-07 23:18:19 +00002681 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 struct uart_8250_port *up = &serial8250_ports[i];
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002683 struct uart_port *port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002685 port->line = i;
2686 spin_lock_init(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688 init_timer(&up->timer);
2689 up->timer.function = serial8250_timeout;
2690
2691 /*
2692 * ALPHA_KLUDGE_MCR needs to be killed.
2693 */
2694 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2695 up->mcr_force = ALPHA_KLUDGE_MCR;
2696
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002697 port->ops = &serial8250_pops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
2699
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002700 if (share_irqs)
2701 irqflag = IRQF_SHARED;
2702
Russell King44454bc2005-06-30 22:41:22 +01002703 for (i = 0, up = serial8250_ports;
Dave Jonesa61c2d72006-01-07 23:18:19 +00002704 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 i++, up++) {
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002706 struct uart_port *port = &up->port;
2707
2708 port->iobase = old_serial_port[i].port;
2709 port->irq = irq_canonicalize(old_serial_port[i].irq);
2710 port->irqflags = old_serial_port[i].irqflags;
2711 port->uartclk = old_serial_port[i].baud_base * 16;
2712 port->flags = old_serial_port[i].flags;
2713 port->hub6 = old_serial_port[i].hub6;
2714 port->membase = old_serial_port[i].iomem_base;
2715 port->iotype = old_serial_port[i].io_type;
2716 port->regshift = old_serial_port[i].iomem_reg_shift;
2717 set_io_from_upio(port);
2718 port->irqflags |= irqflag;
Alan Coxaf7f3742010-10-18 11:38:02 -07002719 if (serial8250_isa_config != NULL)
2720 serial8250_isa_config(i, &up->port, &up->capabilities);
2721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
2723}
2724
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08002725static void
2726serial8250_init_fixed_type_port(struct uart_8250_port *up, unsigned int type)
2727{
2728 up->port.type = type;
2729 up->port.fifosize = uart_config[type].fifo_size;
2730 up->capabilities = uart_config[type].flags;
2731 up->tx_loadsz = uart_config[type].tx_loadsz;
2732}
2733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734static void __init
2735serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2736{
2737 int i;
2738
Alan Coxb8e7e402009-05-28 14:01:35 +01002739 for (i = 0; i < nr_uarts; i++) {
2740 struct uart_8250_port *up = &serial8250_ports[i];
2741 up->cur_iotype = 0xFF;
2742 }
2743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 serial8250_isa_init_ports();
2745
Dave Jonesa61c2d72006-01-07 23:18:19 +00002746 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 struct uart_8250_port *up = &serial8250_ports[i];
2748
2749 up->port.dev = dev;
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08002750
2751 if (up->port.flags & UPF_FIXED_TYPE)
2752 serial8250_init_fixed_type_port(up, up->port.type);
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 uart_add_one_port(drv, &up->port);
2755 }
2756}
2757
2758#ifdef CONFIG_SERIAL_8250_CONSOLE
2759
Russell Kingd3587882006-03-20 20:00:09 +00002760static void serial8250_console_putchar(struct uart_port *port, int ch)
2761{
Jamie Iles49d57412010-12-01 23:39:35 +00002762 struct uart_8250_port *up =
2763 container_of(port, struct uart_8250_port, port);
Russell Kingd3587882006-03-20 20:00:09 +00002764
2765 wait_for_xmitr(up, UART_LSR_THRE);
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002766 serial_port_out(port, UART_TX, ch);
Russell Kingd3587882006-03-20 20:00:09 +00002767}
2768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769/*
2770 * Print a string to the serial port trying not to disturb
2771 * any possible real use of the port...
2772 *
2773 * The console_lock must be held when we get here.
2774 */
2775static void
2776serial8250_console_write(struct console *co, const char *s, unsigned int count)
2777{
2778 struct uart_8250_port *up = &serial8250_ports[co->index];
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002779 struct uart_port *port = &up->port;
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002780 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 unsigned int ier;
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002782 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Andrew Morton78512ec2005-11-07 00:59:13 -08002784 touch_nmi_watchdog();
2785
Andrew Morton68aa2c02006-06-30 02:29:59 -07002786 local_irq_save(flags);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002787 if (port->sysrq) {
Paul Gortmaker86b21192011-12-04 18:42:22 -05002788 /* serial8250_handle_irq() already took the lock */
Andrew Morton68aa2c02006-06-30 02:29:59 -07002789 locked = 0;
2790 } else if (oops_in_progress) {
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002791 locked = spin_trylock(&port->lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002792 } else
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002793 spin_lock(&port->lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 /*
Ralf Baechledc7bf132006-02-15 09:59:47 +00002796 * First save the IER then disable the interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 */
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002798 ier = serial_port_in(port, UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 if (up->capabilities & UART_CAP_UUE)
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002801 serial_port_out(port, UART_IER, UART_IER_UUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 else
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002803 serial_port_out(port, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002805 uart_console_write(port, s, count, serial8250_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 /*
2808 * Finally, wait for transmitter to become empty
2809 * and restore the IER
2810 */
Alan Coxf91a3712006-01-21 14:59:12 +00002811 wait_for_xmitr(up, BOTH_EMPTY);
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002812 serial_port_out(port, UART_IER, ier);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002813
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002814 /*
2815 * The receive handling will happen properly because the
2816 * receive ready bit will still be set; it is not cleared
2817 * on read. However, modem control will not, we must
2818 * call it if we have saved something in the saved flags
2819 * while processing with interrupts off.
2820 */
2821 if (up->msr_saved_flags)
Paul Gortmaker3986fb22011-12-04 18:42:20 -05002822 serial8250_modem_status(up);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002823
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002824 if (locked)
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002825 spin_unlock(&port->lock);
Andrew Morton68aa2c02006-06-30 02:29:59 -07002826 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827}
2828
Vivek Goyal118c0ac2007-01-11 01:52:44 +01002829static int __init serial8250_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
2831 struct uart_port *port;
2832 int baud = 9600;
2833 int bits = 8;
2834 int parity = 'n';
2835 int flow = 'n';
2836
2837 /*
2838 * Check whether an invalid uart number has been specified, and
2839 * if so, search for the first available port that does have
2840 * console support.
2841 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002842 if (co->index >= nr_uarts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 co->index = 0;
2844 port = &serial8250_ports[co->index].port;
2845 if (!port->iobase && !port->membase)
2846 return -ENODEV;
2847
2848 if (options)
2849 uart_parse_options(options, &baud, &parity, &bits, &flow);
2850
2851 return uart_set_options(port, co, baud, parity, bits, flow);
2852}
2853
Daniel Ritzb6b1d872007-08-03 16:07:43 +02002854static int serial8250_console_early_setup(void)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002855{
2856 return serial8250_find_port_for_earlycon();
2857}
2858
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859static struct console serial8250_console = {
2860 .name = "ttyS",
2861 .write = serial8250_console_write,
2862 .device = uart_console_device,
2863 .setup = serial8250_console_setup,
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002864 .early_setup = serial8250_console_early_setup,
Peter Zijlstraa80c49d2010-11-15 21:11:12 +01002865 .flags = CON_PRINTBUFFER | CON_ANYTIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 .index = -1,
2867 .data = &serial8250_reg,
2868};
2869
2870static int __init serial8250_console_init(void)
2871{
Eric W. Biederman05d81d22008-07-12 13:47:53 -07002872 if (nr_uarts > UART_NR)
2873 nr_uarts = UART_NR;
2874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 serial8250_isa_init_ports();
2876 register_console(&serial8250_console);
2877 return 0;
2878}
2879console_initcall(serial8250_console_init);
2880
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002881int serial8250_find_port(struct uart_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
2883 int line;
2884 struct uart_port *port;
2885
Dave Jonesa61c2d72006-01-07 23:18:19 +00002886 for (line = 0; line < nr_uarts; line++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 port = &serial8250_ports[line].port;
Russell King50aec3b52006-01-04 18:13:03 +00002888 if (uart_match_port(p, port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return line;
2890 }
2891 return -ENODEV;
2892}
2893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894#define SERIAL8250_CONSOLE &serial8250_console
2895#else
2896#define SERIAL8250_CONSOLE NULL
2897#endif
2898
2899static struct uart_driver serial8250_reg = {
2900 .owner = THIS_MODULE,
2901 .driver_name = "serial",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 .dev_name = "ttyS",
2903 .major = TTY_MAJOR,
2904 .minor = 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 .cons = SERIAL8250_CONSOLE,
2906};
2907
Russell Kingd856c662006-02-23 10:22:13 +00002908/*
2909 * early_serial_setup - early registration for 8250 ports
2910 *
2911 * Setup an 8250 port structure prior to console initialisation. Use
2912 * after console initialisation will cause undefined behaviour.
2913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914int __init early_serial_setup(struct uart_port *port)
2915{
David Daneyb4304282009-01-02 13:49:41 +00002916 struct uart_port *p;
2917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 if (port->line >= ARRAY_SIZE(serial8250_ports))
2919 return -ENODEV;
2920
2921 serial8250_isa_init_ports();
David Daneyb4304282009-01-02 13:49:41 +00002922 p = &serial8250_ports[port->line].port;
2923 p->iobase = port->iobase;
2924 p->membase = port->membase;
2925 p->irq = port->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07002926 p->irqflags = port->irqflags;
David Daneyb4304282009-01-02 13:49:41 +00002927 p->uartclk = port->uartclk;
2928 p->fifosize = port->fifosize;
2929 p->regshift = port->regshift;
2930 p->iotype = port->iotype;
2931 p->flags = port->flags;
2932 p->mapbase = port->mapbase;
2933 p->private_data = port->private_data;
Helge Deller125c97d2009-01-13 22:51:07 +01002934 p->type = port->type;
2935 p->line = port->line;
David Daney7d6a07d2009-01-02 13:49:47 +00002936
2937 set_io_from_upio(p);
2938 if (port->serial_in)
2939 p->serial_in = port->serial_in;
2940 if (port->serial_out)
2941 p->serial_out = port->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01002942 if (port->handle_irq)
2943 p->handle_irq = port->handle_irq;
2944 else
2945 p->handle_irq = serial8250_default_handle_irq;
David Daney7d6a07d2009-01-02 13:49:47 +00002946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 return 0;
2948}
2949
2950/**
2951 * serial8250_suspend_port - suspend one serial port
2952 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 *
2954 * Suspend one serial port.
2955 */
2956void serial8250_suspend_port(int line)
2957{
2958 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2959}
2960
2961/**
2962 * serial8250_resume_port - resume one serial port
2963 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 *
2965 * Resume one serial port.
2966 */
2967void serial8250_resume_port(int line)
2968{
David Woodhouseb5b82df2007-05-17 14:27:39 +08002969 struct uart_8250_port *up = &serial8250_ports[line];
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002970 struct uart_port *port = &up->port;
David Woodhouseb5b82df2007-05-17 14:27:39 +08002971
2972 if (up->capabilities & UART_NATSEMI) {
David Woodhouseb5b82df2007-05-17 14:27:39 +08002973 /* Ensure it's still in high speed mode */
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002974 serial_port_out(port, UART_LCR, 0xE0);
David Woodhouseb5b82df2007-05-17 14:27:39 +08002975
Yin Kangkai0d0389e2011-02-09 11:35:18 +08002976 ns16550a_goto_highspeed(up);
David Woodhouseb5b82df2007-05-17 14:27:39 +08002977
Paul Gortmaker4fd996a2012-03-08 19:12:13 -05002978 serial_port_out(port, UART_LCR, 0);
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002979 port->uartclk = 921600*16;
David Woodhouseb5b82df2007-05-17 14:27:39 +08002980 }
Paul Gortmakerdfe42442012-03-08 19:12:11 -05002981 uart_resume_port(&serial8250_reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
2984/*
2985 * Register a set of serial devices attached to a platform device. The
2986 * list is terminated with a zero flags entry, which means we expect
2987 * all entries to have at least UPF_BOOT_AUTOCONF set.
2988 */
Russell King3ae5eae2005-11-09 22:32:44 +00002989static int __devinit serial8250_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Russell King3ae5eae2005-11-09 22:32:44 +00002991 struct plat_serial8250_port *p = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 struct uart_port port;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002993 int ret, i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
2995 memset(&port, 0, sizeof(struct uart_port));
2996
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02002997 if (share_irqs)
2998 irqflag = IRQF_SHARED;
2999
Russell Kingec9f47c2005-06-27 11:12:54 +01003000 for (i = 0; p && p->flags != 0; p++, i++) {
Will Newton74a197412008-02-04 22:27:50 -08003001 port.iobase = p->iobase;
3002 port.membase = p->membase;
3003 port.irq = p->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07003004 port.irqflags = p->irqflags;
Will Newton74a197412008-02-04 22:27:50 -08003005 port.uartclk = p->uartclk;
3006 port.regshift = p->regshift;
3007 port.iotype = p->iotype;
3008 port.flags = p->flags;
3009 port.mapbase = p->mapbase;
3010 port.hub6 = p->hub6;
3011 port.private_data = p->private_data;
David Daney8e23fcc2009-01-02 13:49:54 +00003012 port.type = p->type;
David Daney7d6a07d2009-01-02 13:49:47 +00003013 port.serial_in = p->serial_in;
3014 port.serial_out = p->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01003015 port.handle_irq = p->handle_irq;
Dan Williamsbf03f652012-04-10 14:10:53 -07003016 port.handle_break = p->handle_break;
Philippe Langlais235dae52010-07-29 17:13:57 +02003017 port.set_termios = p->set_termios;
Manuel Laussc161afe2010-09-25 15:13:45 +02003018 port.pm = p->pm;
Will Newton74a197412008-02-04 22:27:50 -08003019 port.dev = &dev->dev;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -02003020 port.irqflags |= irqflag;
Russell Kingec9f47c2005-06-27 11:12:54 +01003021 ret = serial8250_register_port(&port);
3022 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00003023 dev_err(&dev->dev, "unable to register port at index %d "
Josh Boyer4f640ef2007-07-23 18:43:44 -07003024 "(IO%lx MEM%llx IRQ%d): %d\n", i,
3025 p->iobase, (unsigned long long)p->mapbase,
3026 p->irq, ret);
Russell Kingec9f47c2005-06-27 11:12:54 +01003027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
3029 return 0;
3030}
3031
3032/*
3033 * Remove serial ports registered against a platform device.
3034 */
Russell King3ae5eae2005-11-09 22:32:44 +00003035static int __devexit serial8250_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
3037 int i;
3038
Dave Jonesa61c2d72006-01-07 23:18:19 +00003039 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 struct uart_8250_port *up = &serial8250_ports[i];
3041
Russell King3ae5eae2005-11-09 22:32:44 +00003042 if (up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 serial8250_unregister_port(i);
3044 }
3045 return 0;
3046}
3047
Russell King3ae5eae2005-11-09 22:32:44 +00003048static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
3050 int i;
3051
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 for (i = 0; i < UART_NR; i++) {
3053 struct uart_8250_port *up = &serial8250_ports[i];
3054
Russell King3ae5eae2005-11-09 22:32:44 +00003055 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 uart_suspend_port(&serial8250_reg, &up->port);
3057 }
3058
3059 return 0;
3060}
3061
Russell King3ae5eae2005-11-09 22:32:44 +00003062static int serial8250_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
3064 int i;
3065
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 for (i = 0; i < UART_NR; i++) {
3067 struct uart_8250_port *up = &serial8250_ports[i];
3068
Russell King3ae5eae2005-11-09 22:32:44 +00003069 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
David Woodhouseb5b82df2007-05-17 14:27:39 +08003070 serial8250_resume_port(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 }
3072
3073 return 0;
3074}
3075
Russell King3ae5eae2005-11-09 22:32:44 +00003076static struct platform_driver serial8250_isa_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 .probe = serial8250_probe,
3078 .remove = __devexit_p(serial8250_remove),
3079 .suspend = serial8250_suspend,
3080 .resume = serial8250_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00003081 .driver = {
3082 .name = "serial8250",
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003083 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00003084 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085};
3086
3087/*
3088 * This "device" covers _all_ ISA 8250-compatible serial devices listed
3089 * in the table in include/asm/serial.h
3090 */
3091static struct platform_device *serial8250_isa_devs;
3092
3093/*
3094 * serial8250_register_port and serial8250_unregister_port allows for
3095 * 16x50 serial ports to be configured at run-time, to support PCMCIA
3096 * modems and PCI multiport cards.
3097 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003098static DEFINE_MUTEX(serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
3100static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
3101{
3102 int i;
3103
3104 /*
3105 * First, find a port entry which matches.
3106 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003107 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 if (uart_match_port(&serial8250_ports[i].port, port))
3109 return &serial8250_ports[i];
3110
3111 /*
3112 * We didn't find a matching entry, so look for the first
3113 * free entry. We look for one which hasn't been previously
3114 * used (indicated by zero iobase).
3115 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003116 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
3118 serial8250_ports[i].port.iobase == 0)
3119 return &serial8250_ports[i];
3120
3121 /*
3122 * That also failed. Last resort is to find any entry which
3123 * doesn't have a real port associated with it.
3124 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00003125 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
3127 return &serial8250_ports[i];
3128
3129 return NULL;
3130}
3131
3132/**
3133 * serial8250_register_port - register a serial port
3134 * @port: serial port template
3135 *
3136 * Configure the serial port specified by the request. If the
3137 * port exists and is in use, it is hung up and unregistered
3138 * first.
3139 *
3140 * The port is then probed and if necessary the IRQ is autodetected
3141 * If this fails an error is returned.
3142 *
3143 * On success the port is ready to use and the line number is returned.
3144 */
3145int serial8250_register_port(struct uart_port *port)
3146{
3147 struct uart_8250_port *uart;
3148 int ret = -ENOSPC;
3149
3150 if (port->uartclk == 0)
3151 return -EINVAL;
3152
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003153 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155 uart = serial8250_find_match_or_unused(port);
3156 if (uart) {
3157 uart_remove_one_port(&serial8250_reg, &uart->port);
3158
Will Newton74a197412008-02-04 22:27:50 -08003159 uart->port.iobase = port->iobase;
3160 uart->port.membase = port->membase;
3161 uart->port.irq = port->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -07003162 uart->port.irqflags = port->irqflags;
Will Newton74a197412008-02-04 22:27:50 -08003163 uart->port.uartclk = port->uartclk;
3164 uart->port.fifosize = port->fifosize;
3165 uart->port.regshift = port->regshift;
3166 uart->port.iotype = port->iotype;
3167 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
3168 uart->port.mapbase = port->mapbase;
3169 uart->port.private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 if (port->dev)
3171 uart->port.dev = port->dev;
David Daney8e23fcc2009-01-02 13:49:54 +00003172
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -08003173 if (port->flags & UPF_FIXED_TYPE)
3174 serial8250_init_fixed_type_port(uart, port->type);
David Daney8e23fcc2009-01-02 13:49:54 +00003175
David Daney7d6a07d2009-01-02 13:49:47 +00003176 set_io_from_upio(&uart->port);
3177 /* Possibly override default I/O functions. */
3178 if (port->serial_in)
3179 uart->port.serial_in = port->serial_in;
3180 if (port->serial_out)
3181 uart->port.serial_out = port->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +01003182 if (port->handle_irq)
3183 uart->port.handle_irq = port->handle_irq;
Philippe Langlais235dae52010-07-29 17:13:57 +02003184 /* Possibly override set_termios call */
3185 if (port->set_termios)
3186 uart->port.set_termios = port->set_termios;
Manuel Laussc161afe2010-09-25 15:13:45 +02003187 if (port->pm)
3188 uart->port.pm = port->pm;
Dan Williamsbf03f652012-04-10 14:10:53 -07003189 if (port->handle_break)
3190 uart->port.handle_break = port->handle_break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Alan Coxaf7f3742010-10-18 11:38:02 -07003192 if (serial8250_isa_config != NULL)
3193 serial8250_isa_config(0, &uart->port,
3194 &uart->capabilities);
3195
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 ret = uart_add_one_port(&serial8250_reg, &uart->port);
3197 if (ret == 0)
3198 ret = uart->port.line;
3199 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003200 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
3202 return ret;
3203}
3204EXPORT_SYMBOL(serial8250_register_port);
3205
3206/**
3207 * serial8250_unregister_port - remove a 16x50 serial port at runtime
3208 * @line: serial line number
3209 *
3210 * Remove one serial port. This may not be called from interrupt
3211 * context. We hand the port back to the our control.
3212 */
3213void serial8250_unregister_port(int line)
3214{
3215 struct uart_8250_port *uart = &serial8250_ports[line];
3216
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003217 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 uart_remove_one_port(&serial8250_reg, &uart->port);
3219 if (serial8250_isa_devs) {
3220 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
3221 uart->port.type = PORT_UNKNOWN;
3222 uart->port.dev = &serial8250_isa_devs->dev;
leitao@linux.vnet.ibm.comcb01ece2011-05-26 11:18:39 -03003223 uart->capabilities = uart_config[uart->port.type].flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 uart_add_one_port(&serial8250_reg, &uart->port);
3225 } else {
3226 uart->port.dev = NULL;
3227 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003228 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
3230EXPORT_SYMBOL(serial8250_unregister_port);
3231
3232static int __init serial8250_init(void)
3233{
Alan Cox25db8ad2008-08-19 20:49:40 -07003234 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Dave Jonesa61c2d72006-01-07 23:18:19 +00003236 if (nr_uarts > UART_NR)
3237 nr_uarts = UART_NR;
3238
Paul Bollef1fb9bb2008-12-30 14:06:43 +01003239 printk(KERN_INFO "Serial: 8250/16550 driver, "
Dave Jonesa61c2d72006-01-07 23:18:19 +00003240 "%d ports, IRQ sharing %sabled\n", nr_uarts,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 share_irqs ? "en" : "dis");
3242
David Millerb70ac772008-10-13 10:36:31 +01003243#ifdef CONFIG_SPARC
3244 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
3245#else
3246 serial8250_reg.nr = UART_NR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 ret = uart_register_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003248#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 if (ret)
3250 goto out;
3251
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003252 serial8250_isa_devs = platform_device_alloc("serial8250",
3253 PLAT8250_DEV_LEGACY);
3254 if (!serial8250_isa_devs) {
3255 ret = -ENOMEM;
Russell Kingbc965a72006-01-18 09:54:29 +00003256 goto unreg_uart_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
3258
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003259 ret = platform_device_add(serial8250_isa_devs);
3260 if (ret)
3261 goto put_dev;
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
3264
Russell Kingbc965a72006-01-18 09:54:29 +00003265 ret = platform_driver_register(&serial8250_isa_driver);
3266 if (ret == 0)
3267 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Russell Kingbc965a72006-01-18 09:54:29 +00003269 platform_device_del(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003270put_dev:
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003271 platform_device_put(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003272unreg_uart_drv:
David Millerb70ac772008-10-13 10:36:31 +01003273#ifdef CONFIG_SPARC
3274 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3275#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003277#endif
Alan Cox25db8ad2008-08-19 20:49:40 -07003278out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return ret;
3280}
3281
3282static void __exit serial8250_exit(void)
3283{
3284 struct platform_device *isa_dev = serial8250_isa_devs;
3285
3286 /*
3287 * This tells serial8250_unregister_port() not to re-register
3288 * the ports (thereby making serial8250_isa_driver permanently
3289 * in use.)
3290 */
3291 serial8250_isa_devs = NULL;
3292
Russell King3ae5eae2005-11-09 22:32:44 +00003293 platform_driver_unregister(&serial8250_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 platform_device_unregister(isa_dev);
3295
David Millerb70ac772008-10-13 10:36:31 +01003296#ifdef CONFIG_SPARC
3297 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3298#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003300#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
3303module_init(serial8250_init);
3304module_exit(serial8250_exit);
3305
3306EXPORT_SYMBOL(serial8250_suspend_port);
3307EXPORT_SYMBOL(serial8250_resume_port);
3308
3309MODULE_LICENSE("GPL");
Adrian Bunkd87a6d92008-07-16 21:53:31 +01003310MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
3312module_param(share_irqs, uint, 0644);
3313MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
3314 " (unsafe)");
3315
Dave Jonesa61c2d72006-01-07 23:18:19 +00003316module_param(nr_uarts, uint, 0644);
3317MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
3318
Chuck Ebbertd41a4b52009-10-01 15:44:26 -07003319module_param(skip_txen_test, uint, 0644);
3320MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
3321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322#ifdef CONFIG_SERIAL_8250_RSA
3323module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
3324MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
3325#endif
3326MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);