blob: c66bb44c47477e4bf7dfe6b3be5a1d746ae45b28 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/8250.c
3 *
4 * Driver for 8250/16550-type serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright (C) 2001 Russell King.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * A note about mapbase / membase
16 *
17 * mapbase is the physical address of the IO port.
18 * membase is an 'ioremapped' cookie.
19 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
22#define SUPPORT_SYSRQ
23#endif
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/ioport.h>
28#include <linux/init.h>
29#include <linux/console.h>
30#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/delay.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010032#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/serial_reg.h>
36#include <linux/serial_core.h>
37#include <linux/serial.h>
38#include <linux/serial_8250.h>
Andrew Morton78512ec2005-11-07 00:59:13 -080039#include <linux/nmi.h>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000040#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/io.h>
43#include <asm/irq.h>
44
45#include "8250.h"
46
David Millerb70ac772008-10-13 10:36:31 +010047#ifdef CONFIG_SPARC
48#include "suncore.h"
49#endif
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * Configuration:
Thomas Gleixner40663cc2006-07-01 19:29:43 -070053 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * is unsafe when used on edge-triggered interrupts.
55 */
Adrian Bunk408b6642005-05-01 08:59:29 -070056static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Dave Jonesa61c2d72006-01-07 23:18:19 +000058static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
59
David S. Miller84408382008-10-13 10:45:26 +010060static struct uart_driver serial8250_reg;
61
62static int serial_index(struct uart_port *port)
63{
64 return (serial8250_reg.minor - 64) + port->line;
65}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * Debugging.
69 */
70#if 0
71#define DEBUG_AUTOCONF(fmt...) printk(fmt)
72#else
73#define DEBUG_AUTOCONF(fmt...) do { } while (0)
74#endif
75
76#if 0
77#define DEBUG_INTR(fmt...) printk(fmt)
78#else
79#define DEBUG_INTR(fmt...) do { } while (0)
80#endif
81
82#define PASS_LIMIT 256
83
84/*
85 * We default to IRQ0 for the "no irq" hack. Some
86 * machine types want others as well - they're free
87 * to redefine this in their header file.
88 */
89#define is_real_interrupt(irq) ((irq) != 0)
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
92#define CONFIG_SERIAL_DETECT_IRQ 1
93#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#ifdef CONFIG_SERIAL_8250_MANY_PORTS
95#define CONFIG_SERIAL_MANY_PORTS 1
96#endif
97
98/*
99 * HUB6 is always on. This will be removed once the header
100 * files have been cleaned.
101 */
102#define CONFIG_HUB6 1
103
Bryan Wua4ed1e42008-05-31 16:10:04 +0800104#include <asm/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * SERIAL_PORT_DFNS tells us about built-in ports that have no
107 * standard enumeration mechanism. Platforms that can find all
108 * serial ports via mechanisms like ACPI or PCI need not supply it.
109 */
110#ifndef SERIAL_PORT_DFNS
111#define SERIAL_PORT_DFNS
112#endif
113
Arjan van de Vencb3592b2005-11-28 21:04:11 +0000114static const struct old_serial_port old_serial_port[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 SERIAL_PORT_DFNS /* defined in asm/serial.h */
116};
117
Russell King026d02a2005-06-29 18:45:19 +0100118#define UART_NR CONFIG_SERIAL_8250_NR_UARTS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120#ifdef CONFIG_SERIAL_8250_RSA
121
122#define PORT_RSA_MAX 4
123static unsigned long probe_rsa[PORT_RSA_MAX];
124static unsigned int probe_rsa_count;
125#endif /* CONFIG_SERIAL_8250_RSA */
126
127struct uart_8250_port {
128 struct uart_port port;
129 struct timer_list timer; /* "no irq" timer */
130 struct list_head list; /* ports on this IRQ */
Russell King4ba5e352005-06-23 10:43:04 +0100131 unsigned short capabilities; /* port capabilities */
132 unsigned short bugs; /* port bugs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 unsigned int tx_loadsz; /* transmit fifo load size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 unsigned char acr;
135 unsigned char ier;
136 unsigned char lcr;
137 unsigned char mcr;
138 unsigned char mcr_mask; /* mask of user bits */
139 unsigned char mcr_force; /* mask of forced bits */
Corey Minyardad4c2aa2007-08-22 14:01:18 -0700140
141 /*
142 * Some bits in registers are cleared on a read, so they must
143 * be saved whenever the register is read but the bits will not
144 * be immediately processed.
145 */
146#define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITS
147 unsigned char lsr_saved_flags;
148#define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
149 unsigned char msr_saved_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /*
152 * We provide a per-port pm hook.
153 */
154 void (*pm)(struct uart_port *port,
155 unsigned int state, unsigned int old);
156};
157
158struct irq_info {
Alan Cox25db8ad2008-08-19 20:49:40 -0700159 struct hlist_node node;
160 int irq;
161 spinlock_t lock; /* Protects list not the hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct list_head *head;
163};
164
Alan Cox25db8ad2008-08-19 20:49:40 -0700165#define NR_IRQ_HASH 32 /* Can be adjusted later */
166static struct hlist_head irq_lists[NR_IRQ_HASH];
167static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169/*
170 * Here we define the default xmit fifo size used for each type of UART.
171 */
172static const struct serial8250_config uart_config[] = {
173 [PORT_UNKNOWN] = {
174 .name = "unknown",
175 .fifo_size = 1,
176 .tx_loadsz = 1,
177 },
178 [PORT_8250] = {
179 .name = "8250",
180 .fifo_size = 1,
181 .tx_loadsz = 1,
182 },
183 [PORT_16450] = {
184 .name = "16450",
185 .fifo_size = 1,
186 .tx_loadsz = 1,
187 },
188 [PORT_16550] = {
189 .name = "16550",
190 .fifo_size = 1,
191 .tx_loadsz = 1,
192 },
193 [PORT_16550A] = {
194 .name = "16550A",
195 .fifo_size = 16,
196 .tx_loadsz = 16,
197 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
198 .flags = UART_CAP_FIFO,
199 },
200 [PORT_CIRRUS] = {
201 .name = "Cirrus",
202 .fifo_size = 1,
203 .tx_loadsz = 1,
204 },
205 [PORT_16650] = {
206 .name = "ST16650",
207 .fifo_size = 1,
208 .tx_loadsz = 1,
209 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
210 },
211 [PORT_16650V2] = {
212 .name = "ST16650V2",
213 .fifo_size = 32,
214 .tx_loadsz = 16,
215 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
216 UART_FCR_T_TRIG_00,
217 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
218 },
219 [PORT_16750] = {
220 .name = "TI16750",
221 .fifo_size = 64,
222 .tx_loadsz = 64,
223 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
224 UART_FCR7_64BYTE,
225 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
226 },
227 [PORT_STARTECH] = {
228 .name = "Startech",
229 .fifo_size = 1,
230 .tx_loadsz = 1,
231 },
232 [PORT_16C950] = {
233 .name = "16C950/954",
234 .fifo_size = 128,
235 .tx_loadsz = 128,
236 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
237 .flags = UART_CAP_FIFO,
238 },
239 [PORT_16654] = {
240 .name = "ST16654",
241 .fifo_size = 64,
242 .tx_loadsz = 32,
243 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
244 UART_FCR_T_TRIG_10,
245 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
246 },
247 [PORT_16850] = {
248 .name = "XR16850",
249 .fifo_size = 128,
250 .tx_loadsz = 128,
251 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
252 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
253 },
254 [PORT_RSA] = {
255 .name = "RSA",
256 .fifo_size = 2048,
257 .tx_loadsz = 2048,
258 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
259 .flags = UART_CAP_FIFO,
260 },
261 [PORT_NS16550A] = {
262 .name = "NS16550A",
263 .fifo_size = 16,
264 .tx_loadsz = 16,
265 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
266 .flags = UART_CAP_FIFO | UART_NATSEMI,
267 },
268 [PORT_XSCALE] = {
269 .name = "XScale",
270 .fifo_size = 32,
271 .tx_loadsz = 32,
272 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
273 .flags = UART_CAP_FIFO | UART_CAP_UUE,
274 },
Thomas Koellerbd71c182007-05-06 14:48:47 -0700275 [PORT_RM9000] = {
276 .name = "RM9000",
277 .fifo_size = 16,
278 .tx_loadsz = 16,
279 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
280 .flags = UART_CAP_FIFO,
281 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282};
283
Thomas Koellerbd71c182007-05-06 14:48:47 -0700284#if defined (CONFIG_SERIAL_8250_AU1X00)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000285
286/* Au1x00 UART hardware has a weird register layout */
287static const u8 au_io_in_map[] = {
288 [UART_RX] = 0,
289 [UART_IER] = 2,
290 [UART_IIR] = 3,
291 [UART_LCR] = 5,
292 [UART_MCR] = 6,
293 [UART_LSR] = 7,
294 [UART_MSR] = 8,
295};
296
297static const u8 au_io_out_map[] = {
298 [UART_TX] = 1,
299 [UART_IER] = 2,
300 [UART_FCR] = 4,
301 [UART_LCR] = 5,
302 [UART_MCR] = 6,
303};
304
305/* sane hardware needs no mapping */
306static inline int map_8250_in_reg(struct uart_8250_port *up, int offset)
307{
308 if (up->port.iotype != UPIO_AU)
309 return offset;
310 return au_io_in_map[offset];
311}
312
313static inline int map_8250_out_reg(struct uart_8250_port *up, int offset)
314{
315 if (up->port.iotype != UPIO_AU)
316 return offset;
317 return au_io_out_map[offset];
318}
319
Alan Cox6f803cd2008-02-08 04:18:52 -0800320#elif defined(CONFIG_SERIAL_8250_RM9K)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700321
322static const u8
323 regmap_in[8] = {
324 [UART_RX] = 0x00,
325 [UART_IER] = 0x0c,
326 [UART_IIR] = 0x14,
327 [UART_LCR] = 0x1c,
328 [UART_MCR] = 0x20,
329 [UART_LSR] = 0x24,
330 [UART_MSR] = 0x28,
331 [UART_SCR] = 0x2c
332 },
333 regmap_out[8] = {
334 [UART_TX] = 0x04,
335 [UART_IER] = 0x0c,
336 [UART_FCR] = 0x18,
337 [UART_LCR] = 0x1c,
338 [UART_MCR] = 0x20,
339 [UART_LSR] = 0x24,
340 [UART_MSR] = 0x28,
341 [UART_SCR] = 0x2c
342 };
343
344static inline int map_8250_in_reg(struct uart_8250_port *up, int offset)
345{
346 if (up->port.iotype != UPIO_RM9000)
347 return offset;
348 return regmap_in[offset];
349}
350
351static inline int map_8250_out_reg(struct uart_8250_port *up, int offset)
352{
353 if (up->port.iotype != UPIO_RM9000)
354 return offset;
355 return regmap_out[offset];
356}
357
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000358#else
359
360/* sane hardware needs no mapping */
361#define map_8250_in_reg(up, offset) (offset)
362#define map_8250_out_reg(up, offset) (offset)
363
364#endif
365
Russell Kingea8874d2006-01-04 19:43:24 +0000366static unsigned int serial_in(struct uart_8250_port *up, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Zang Roy-r619113be91ec2006-06-30 02:29:58 -0700368 unsigned int tmp;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000369 offset = map_8250_in_reg(up, offset) << up->port.regshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 switch (up->port.iotype) {
372 case UPIO_HUB6:
373 outb(up->port.hub6 - 1 + offset, up->port.iobase);
374 return inb(up->port.iobase + 1);
375
376 case UPIO_MEM:
Marc St-Jeanbeab6972007-05-06 14:48:45 -0700377 case UPIO_DWAPB:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return readb(up->port.membase + offset);
379
Thomas Koellerbd71c182007-05-06 14:48:47 -0700380 case UPIO_RM9000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 case UPIO_MEM32:
382 return readl(up->port.membase + offset);
383
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000384#ifdef CONFIG_SERIAL_8250_AU1X00
385 case UPIO_AU:
386 return __raw_readl(up->port.membase + offset);
387#endif
388
Zang Roy-r619113be91ec2006-06-30 02:29:58 -0700389 case UPIO_TSI:
390 if (offset == UART_IIR) {
Al Viro9e84b602006-09-23 01:39:45 +0100391 tmp = readl(up->port.membase + (UART_IIR & ~3));
392 return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
Zang Roy-r619113be91ec2006-06-30 02:29:58 -0700393 } else
394 return readb(up->port.membase + offset);
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 default:
397 return inb(up->port.iobase + offset);
398 }
399}
400
Russell Kingea8874d2006-01-04 19:43:24 +0000401static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402serial_out(struct uart_8250_port *up, int offset, int value)
403{
Marc St-Jeanbeab6972007-05-06 14:48:45 -0700404 /* Save the offset before it's remapped */
405 int save_offset = offset;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000406 offset = map_8250_out_reg(up, offset) << up->port.regshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 switch (up->port.iotype) {
409 case UPIO_HUB6:
410 outb(up->port.hub6 - 1 + offset, up->port.iobase);
411 outb(value, up->port.iobase + 1);
412 break;
413
414 case UPIO_MEM:
415 writeb(value, up->port.membase + offset);
416 break;
417
Thomas Koellerbd71c182007-05-06 14:48:47 -0700418 case UPIO_RM9000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 case UPIO_MEM32:
420 writel(value, up->port.membase + offset);
421 break;
422
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000423#ifdef CONFIG_SERIAL_8250_AU1X00
424 case UPIO_AU:
425 __raw_writel(value, up->port.membase + offset);
426 break;
427#endif
Zang Roy-r619113be91ec2006-06-30 02:29:58 -0700428 case UPIO_TSI:
429 if (!((offset == UART_IER) && (value & UART_IER_UUE)))
430 writeb(value, up->port.membase + offset);
431 break;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000432
Marc St-Jeanbeab6972007-05-06 14:48:45 -0700433 case UPIO_DWAPB:
434 /* Save the LCR value so it can be re-written when a
435 * Busy Detect interrupt occurs. */
436 if (save_offset == UART_LCR)
437 up->lcr = value;
438 writeb(value, up->port.membase + offset);
439 /* Read the IER to ensure any interrupt is cleared before
440 * returning from ISR. */
441 if (save_offset == UART_TX || save_offset == UART_IER)
442 value = serial_in(up, UART_IER);
443 break;
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 default:
446 outb(value, up->port.iobase + offset);
447 }
448}
449
Alex Williamson40b36da2007-02-14 00:33:04 -0800450static void
451serial_out_sync(struct uart_8250_port *up, int offset, int value)
452{
453 switch (up->port.iotype) {
454 case UPIO_MEM:
455 case UPIO_MEM32:
456#ifdef CONFIG_SERIAL_8250_AU1X00
457 case UPIO_AU:
458#endif
Marc St-Jeanbeab6972007-05-06 14:48:45 -0700459 case UPIO_DWAPB:
Alex Williamson40b36da2007-02-14 00:33:04 -0800460 serial_out(up, offset, value);
461 serial_in(up, UART_LCR); /* safe, no side-effects */
462 break;
463 default:
464 serial_out(up, offset, value);
465 }
466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
469 * We used to support using pause I/O for certain machines. We
470 * haven't supported this for a while, but just in case it's badly
471 * needed for certain old 386 machines, I've left these #define's
472 * in....
473 */
474#define serial_inp(up, offset) serial_in(up, offset)
475#define serial_outp(up, offset, value) serial_out(up, offset, value)
476
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100477/* Uart divisor latch read */
478static inline int _serial_dl_read(struct uart_8250_port *up)
479{
480 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
481}
482
483/* Uart divisor latch write */
484static inline void _serial_dl_write(struct uart_8250_port *up, int value)
485{
486 serial_outp(up, UART_DLL, value & 0xff);
487 serial_outp(up, UART_DLM, value >> 8 & 0xff);
488}
489
Alan Cox6f803cd2008-02-08 04:18:52 -0800490#if defined(CONFIG_SERIAL_8250_AU1X00)
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100491/* Au1x00 haven't got a standard divisor latch */
492static int serial_dl_read(struct uart_8250_port *up)
493{
494 if (up->port.iotype == UPIO_AU)
495 return __raw_readl(up->port.membase + 0x28);
496 else
497 return _serial_dl_read(up);
498}
499
500static void serial_dl_write(struct uart_8250_port *up, int value)
501{
502 if (up->port.iotype == UPIO_AU)
503 __raw_writel(value, up->port.membase + 0x28);
504 else
505 _serial_dl_write(up, value);
506}
Alan Cox6f803cd2008-02-08 04:18:52 -0800507#elif defined(CONFIG_SERIAL_8250_RM9K)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700508static int serial_dl_read(struct uart_8250_port *up)
509{
510 return (up->port.iotype == UPIO_RM9000) ?
511 (((__raw_readl(up->port.membase + 0x10) << 8) |
512 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
513 _serial_dl_read(up);
514}
515
516static void serial_dl_write(struct uart_8250_port *up, int value)
517{
518 if (up->port.iotype == UPIO_RM9000) {
519 __raw_writel(value, up->port.membase + 0x08);
520 __raw_writel(value >> 8, up->port.membase + 0x10);
521 } else {
522 _serial_dl_write(up, value);
523 }
524}
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100525#else
526#define serial_dl_read(up) _serial_dl_read(up)
527#define serial_dl_write(up, value) _serial_dl_write(up, value)
528#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530/*
531 * For the 16C950
532 */
533static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
534{
535 serial_out(up, UART_SCR, offset);
536 serial_out(up, UART_ICR, value);
537}
538
539static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
540{
541 unsigned int value;
542
543 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
544 serial_out(up, UART_SCR, offset);
545 value = serial_in(up, UART_ICR);
546 serial_icr_write(up, UART_ACR, up->acr);
547
548 return value;
549}
550
551/*
552 * FIFO support.
553 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100554static void serial8250_clear_fifos(struct uart_8250_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 if (p->capabilities & UART_CAP_FIFO) {
557 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
558 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
559 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
560 serial_outp(p, UART_FCR, 0);
561 }
562}
563
564/*
565 * IER sleep support. UARTs which have EFRs need the "extended
566 * capability" bit enabled. Note that on XR16C850s, we need to
567 * reset LCR to write to IER.
568 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100569static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 if (p->capabilities & UART_CAP_SLEEP) {
572 if (p->capabilities & UART_CAP_EFR) {
573 serial_outp(p, UART_LCR, 0xBF);
574 serial_outp(p, UART_EFR, UART_EFR_ECB);
575 serial_outp(p, UART_LCR, 0);
576 }
577 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
578 if (p->capabilities & UART_CAP_EFR) {
579 serial_outp(p, UART_LCR, 0xBF);
580 serial_outp(p, UART_EFR, 0);
581 serial_outp(p, UART_LCR, 0);
582 }
583 }
584}
585
586#ifdef CONFIG_SERIAL_8250_RSA
587/*
588 * Attempts to turn on the RSA FIFO. Returns zero on failure.
589 * We set the port uart clock rate if we succeed.
590 */
591static int __enable_rsa(struct uart_8250_port *up)
592{
593 unsigned char mode;
594 int result;
595
596 mode = serial_inp(up, UART_RSA_MSR);
597 result = mode & UART_RSA_MSR_FIFO;
598
599 if (!result) {
600 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
601 mode = serial_inp(up, UART_RSA_MSR);
602 result = mode & UART_RSA_MSR_FIFO;
603 }
604
605 if (result)
606 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
607
608 return result;
609}
610
611static void enable_rsa(struct uart_8250_port *up)
612{
613 if (up->port.type == PORT_RSA) {
614 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
615 spin_lock_irq(&up->port.lock);
616 __enable_rsa(up);
617 spin_unlock_irq(&up->port.lock);
618 }
619 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
620 serial_outp(up, UART_RSA_FRR, 0);
621 }
622}
623
624/*
625 * Attempts to turn off the RSA FIFO. Returns zero on failure.
626 * It is unknown why interrupts were disabled in here. However,
627 * the caller is expected to preserve this behaviour by grabbing
628 * the spinlock before calling this function.
629 */
630static void disable_rsa(struct uart_8250_port *up)
631{
632 unsigned char mode;
633 int result;
634
635 if (up->port.type == PORT_RSA &&
636 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
637 spin_lock_irq(&up->port.lock);
638
639 mode = serial_inp(up, UART_RSA_MSR);
640 result = !(mode & UART_RSA_MSR_FIFO);
641
642 if (!result) {
643 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
644 mode = serial_inp(up, UART_RSA_MSR);
645 result = !(mode & UART_RSA_MSR_FIFO);
646 }
647
648 if (result)
649 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
650 spin_unlock_irq(&up->port.lock);
651 }
652}
653#endif /* CONFIG_SERIAL_8250_RSA */
654
655/*
656 * This is a quickie test to see how big the FIFO is.
657 * It doesn't work at all the time, more's the pity.
658 */
659static int size_fifo(struct uart_8250_port *up)
660{
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100661 unsigned char old_fcr, old_mcr, old_lcr;
662 unsigned short old_dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 int count;
664
665 old_lcr = serial_inp(up, UART_LCR);
666 serial_outp(up, UART_LCR, 0);
667 old_fcr = serial_inp(up, UART_FCR);
668 old_mcr = serial_inp(up, UART_MCR);
669 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
670 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
671 serial_outp(up, UART_MCR, UART_MCR_LOOP);
672 serial_outp(up, UART_LCR, UART_LCR_DLAB);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100673 old_dl = serial_dl_read(up);
674 serial_dl_write(up, 0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 serial_outp(up, UART_LCR, 0x03);
676 for (count = 0; count < 256; count++)
677 serial_outp(up, UART_TX, count);
678 mdelay(20);/* FIXME - schedule_timeout */
679 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
680 (count < 256); count++)
681 serial_inp(up, UART_RX);
682 serial_outp(up, UART_FCR, old_fcr);
683 serial_outp(up, UART_MCR, old_mcr);
684 serial_outp(up, UART_LCR, UART_LCR_DLAB);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100685 serial_dl_write(up, old_dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 serial_outp(up, UART_LCR, old_lcr);
687
688 return count;
689}
690
691/*
692 * Read UART ID using the divisor method - set DLL and DLM to zero
693 * and the revision will be in DLL and device type in DLM. We
694 * preserve the device state across this.
695 */
696static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
697{
698 unsigned char old_dll, old_dlm, old_lcr;
699 unsigned int id;
700
701 old_lcr = serial_inp(p, UART_LCR);
702 serial_outp(p, UART_LCR, UART_LCR_DLAB);
703
704 old_dll = serial_inp(p, UART_DLL);
705 old_dlm = serial_inp(p, UART_DLM);
706
707 serial_outp(p, UART_DLL, 0);
708 serial_outp(p, UART_DLM, 0);
709
710 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
711
712 serial_outp(p, UART_DLL, old_dll);
713 serial_outp(p, UART_DLM, old_dlm);
714 serial_outp(p, UART_LCR, old_lcr);
715
716 return id;
717}
718
719/*
720 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
721 * When this function is called we know it is at least a StarTech
722 * 16650 V2, but it might be one of several StarTech UARTs, or one of
723 * its clones. (We treat the broken original StarTech 16650 V1 as a
724 * 16550, and why not? Startech doesn't seem to even acknowledge its
725 * existence.)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700726 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * What evil have men's minds wrought...
728 */
729static void autoconfig_has_efr(struct uart_8250_port *up)
730{
731 unsigned int id1, id2, id3, rev;
732
733 /*
734 * Everything with an EFR has SLEEP
735 */
736 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
737
738 /*
739 * First we check to see if it's an Oxford Semiconductor UART.
740 *
741 * If we have to do this here because some non-National
742 * Semiconductor clone chips lock up if you try writing to the
743 * LSR register (which serial_icr_read does)
744 */
745
746 /*
747 * Check for Oxford Semiconductor 16C950.
748 *
749 * EFR [4] must be set else this test fails.
750 *
751 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
752 * claims that it's needed for 952 dual UART's (which are not
753 * recommended for new designs).
754 */
755 up->acr = 0;
756 serial_out(up, UART_LCR, 0xBF);
757 serial_out(up, UART_EFR, UART_EFR_ECB);
758 serial_out(up, UART_LCR, 0x00);
759 id1 = serial_icr_read(up, UART_ID1);
760 id2 = serial_icr_read(up, UART_ID2);
761 id3 = serial_icr_read(up, UART_ID3);
762 rev = serial_icr_read(up, UART_REV);
763
764 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
765
766 if (id1 == 0x16 && id2 == 0xC9 &&
767 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
768 up->port.type = PORT_16C950;
Russell King4ba5e352005-06-23 10:43:04 +0100769
770 /*
771 * Enable work around for the Oxford Semiconductor 952 rev B
772 * chip which causes it to seriously miscalculate baud rates
773 * when DLL is 0.
774 */
775 if (id3 == 0x52 && rev == 0x01)
776 up->bugs |= UART_BUG_QUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return;
778 }
Thomas Koellerbd71c182007-05-06 14:48:47 -0700779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /*
781 * We check for a XR16C850 by setting DLL and DLM to 0, and then
782 * reading back DLL and DLM. The chip type depends on the DLM
783 * value read back:
784 * 0x10 - XR16C850 and the DLL contains the chip revision.
785 * 0x12 - XR16C2850.
786 * 0x14 - XR16C854.
787 */
788 id1 = autoconfig_read_divisor_id(up);
789 DEBUG_AUTOCONF("850id=%04x ", id1);
790
791 id2 = id1 >> 8;
792 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 up->port.type = PORT_16850;
794 return;
795 }
796
797 /*
798 * It wasn't an XR16C850.
799 *
800 * We distinguish between the '654 and the '650 by counting
801 * how many bytes are in the FIFO. I'm using this for now,
802 * since that's the technique that was sent to me in the
803 * serial driver update, but I'm not convinced this works.
804 * I've had problems doing this in the past. -TYT
805 */
806 if (size_fifo(up) == 64)
807 up->port.type = PORT_16654;
808 else
809 up->port.type = PORT_16650V2;
810}
811
812/*
813 * We detected a chip without a FIFO. Only two fall into
814 * this category - the original 8250 and the 16450. The
815 * 16450 has a scratch register (accessible with LCR=0)
816 */
817static void autoconfig_8250(struct uart_8250_port *up)
818{
819 unsigned char scratch, status1, status2;
820
821 up->port.type = PORT_8250;
822
823 scratch = serial_in(up, UART_SCR);
824 serial_outp(up, UART_SCR, 0xa5);
825 status1 = serial_in(up, UART_SCR);
826 serial_outp(up, UART_SCR, 0x5a);
827 status2 = serial_in(up, UART_SCR);
828 serial_outp(up, UART_SCR, scratch);
829
830 if (status1 == 0xa5 && status2 == 0x5a)
831 up->port.type = PORT_16450;
832}
833
834static int broken_efr(struct uart_8250_port *up)
835{
836 /*
837 * Exar ST16C2550 "A2" devices incorrectly detect as
838 * having an EFR, and report an ID of 0x0201. See
839 * http://www.exar.com/info.php?pdf=dan180_oct2004.pdf
840 */
841 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
842 return 1;
843
844 return 0;
845}
846
847/*
848 * We know that the chip has FIFOs. Does it have an EFR? The
849 * EFR is located in the same register position as the IIR and
850 * we know the top two bits of the IIR are currently set. The
851 * EFR should contain zero. Try to read the EFR.
852 */
853static void autoconfig_16550a(struct uart_8250_port *up)
854{
855 unsigned char status1, status2;
856 unsigned int iersave;
857
858 up->port.type = PORT_16550A;
859 up->capabilities |= UART_CAP_FIFO;
860
861 /*
862 * Check for presence of the EFR when DLAB is set.
863 * Only ST16C650V1 UARTs pass this test.
864 */
865 serial_outp(up, UART_LCR, UART_LCR_DLAB);
866 if (serial_in(up, UART_EFR) == 0) {
867 serial_outp(up, UART_EFR, 0xA8);
868 if (serial_in(up, UART_EFR) != 0) {
869 DEBUG_AUTOCONF("EFRv1 ");
870 up->port.type = PORT_16650;
871 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
872 } else {
873 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
874 }
875 serial_outp(up, UART_EFR, 0);
876 return;
877 }
878
879 /*
880 * Maybe it requires 0xbf to be written to the LCR.
881 * (other ST16C650V2 UARTs, TI16C752A, etc)
882 */
883 serial_outp(up, UART_LCR, 0xBF);
884 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
885 DEBUG_AUTOCONF("EFRv2 ");
886 autoconfig_has_efr(up);
887 return;
888 }
889
890 /*
891 * Check for a National Semiconductor SuperIO chip.
892 * Attempt to switch to bank 2, read the value of the LOOP bit
893 * from EXCR1. Switch back to bank 0, change it in MCR. Then
894 * switch back to bank 2, read it from EXCR1 again and check
895 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 */
897 serial_outp(up, UART_LCR, 0);
898 status1 = serial_in(up, UART_MCR);
899 serial_outp(up, UART_LCR, 0xE0);
900 status2 = serial_in(up, 0x02); /* EXCR1 */
901
902 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
903 serial_outp(up, UART_LCR, 0);
904 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
905 serial_outp(up, UART_LCR, 0xE0);
906 status2 = serial_in(up, 0x02); /* EXCR1 */
907 serial_outp(up, UART_LCR, 0);
908 serial_outp(up, UART_MCR, status1);
909
910 if ((status2 ^ status1) & UART_MCR_LOOP) {
David Woodhouse857dde22005-05-21 15:52:23 +0100911 unsigned short quot;
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 serial_outp(up, UART_LCR, 0xE0);
David Woodhouse857dde22005-05-21 15:52:23 +0100914
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100915 quot = serial_dl_read(up);
David Woodhouse857dde22005-05-21 15:52:23 +0100916 quot <<= 3;
917
David Woodhouseb5b82df2007-05-17 14:27:39 +0800918 status1 = serial_in(up, 0x04); /* EXCR2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
920 status1 |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
921 serial_outp(up, 0x04, status1);
Thomas Koellerbd71c182007-05-06 14:48:47 -0700922
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100923 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
David Woodhouse857dde22005-05-21 15:52:23 +0100925 serial_outp(up, UART_LCR, 0);
926
927 up->port.uartclk = 921600*16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 up->port.type = PORT_NS16550A;
929 up->capabilities |= UART_NATSEMI;
930 return;
931 }
932 }
933
934 /*
935 * No EFR. Try to detect a TI16750, which only sets bit 5 of
936 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
937 * Try setting it with and without DLAB set. Cheap clones
938 * set bit 5 without DLAB set.
939 */
940 serial_outp(up, UART_LCR, 0);
941 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
942 status1 = serial_in(up, UART_IIR) >> 5;
943 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
944 serial_outp(up, UART_LCR, UART_LCR_DLAB);
945 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
946 status2 = serial_in(up, UART_IIR) >> 5;
947 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
948 serial_outp(up, UART_LCR, 0);
949
950 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
951
952 if (status1 == 6 && status2 == 7) {
953 up->port.type = PORT_16750;
954 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
955 return;
956 }
957
958 /*
959 * Try writing and reading the UART_IER_UUE bit (b6).
960 * If it works, this is probably one of the Xscale platform's
961 * internal UARTs.
962 * We're going to explicitly set the UUE bit to 0 before
963 * trying to write and read a 1 just to make sure it's not
964 * already a 1 and maybe locked there before we even start start.
965 */
966 iersave = serial_in(up, UART_IER);
967 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
968 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
969 /*
970 * OK it's in a known zero state, try writing and reading
971 * without disturbing the current state of the other bits.
972 */
973 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
974 if (serial_in(up, UART_IER) & UART_IER_UUE) {
975 /*
976 * It's an Xscale.
977 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
978 */
979 DEBUG_AUTOCONF("Xscale ");
980 up->port.type = PORT_XSCALE;
981 up->capabilities |= UART_CAP_UUE;
982 return;
983 }
984 } else {
985 /*
986 * If we got here we couldn't force the IER_UUE bit to 0.
987 * Log it and continue.
988 */
989 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
990 }
991 serial_outp(up, UART_IER, iersave);
992}
993
994/*
995 * This routine is called by rs_init() to initialize a specific serial
996 * port. It determines what type of UART chip this serial port is
997 * using: 8250, 16450, 16550, 16550A. The important question is
998 * whether or not this UART is a 16550A or not, since this will
999 * determine whether or not we can use its FIFO features or not.
1000 */
1001static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
1002{
1003 unsigned char status1, scratch, scratch2, scratch3;
1004 unsigned char save_lcr, save_mcr;
1005 unsigned long flags;
1006
1007 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
1008 return;
1009
1010 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%p): ",
David S. Miller84408382008-10-13 10:45:26 +01001011 serial_index(&up->port), up->port.iobase, up->port.membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 /*
1014 * We really do need global IRQs disabled here - we're going to
1015 * be frobbing the chips IRQ enable register to see if it exists.
1016 */
1017 spin_lock_irqsave(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 up->capabilities = 0;
Russell King4ba5e352005-06-23 10:43:04 +01001020 up->bugs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 if (!(up->port.flags & UPF_BUGGY_UART)) {
1023 /*
1024 * Do a simple existence test first; if we fail this,
1025 * there's no point trying anything else.
Thomas Koellerbd71c182007-05-06 14:48:47 -07001026 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * 0x80 is used as a nonsense port to prevent against
1028 * false positives due to ISA bus float. The
1029 * assumption is that 0x80 is a non-existent port;
1030 * which should be safe since include/asm/io.h also
1031 * makes this assumption.
1032 *
1033 * Note: this is safe as long as MCR bit 4 is clear
1034 * and the device is in "PC" mode.
1035 */
1036 scratch = serial_inp(up, UART_IER);
1037 serial_outp(up, UART_IER, 0);
1038#ifdef __i386__
1039 outb(0xff, 0x080);
1040#endif
Thomas Hoehn48212002007-02-10 01:46:05 -08001041 /*
1042 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1043 * 16C754B) allow only to modify them if an EFR bit is set.
1044 */
1045 scratch2 = serial_inp(up, UART_IER) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 serial_outp(up, UART_IER, 0x0F);
1047#ifdef __i386__
1048 outb(0, 0x080);
1049#endif
Thomas Hoehn48212002007-02-10 01:46:05 -08001050 scratch3 = serial_inp(up, UART_IER) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 serial_outp(up, UART_IER, scratch);
1052 if (scratch2 != 0 || scratch3 != 0x0F) {
1053 /*
1054 * We failed; there's nothing here
1055 */
1056 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1057 scratch2, scratch3);
1058 goto out;
1059 }
1060 }
1061
1062 save_mcr = serial_in(up, UART_MCR);
1063 save_lcr = serial_in(up, UART_LCR);
1064
Thomas Koellerbd71c182007-05-06 14:48:47 -07001065 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 * Check to see if a UART is really there. Certain broken
1067 * internal modems based on the Rockwell chipset fail this
1068 * test, because they apparently don't implement the loopback
1069 * test mode. So this test is skipped on the COM 1 through
1070 * COM 4 ports. This *should* be safe, since no board
1071 * manufacturer would be stupid enough to design a board
1072 * that conflicts with COM 1-4 --- we hope!
1073 */
1074 if (!(up->port.flags & UPF_SKIP_TEST)) {
1075 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1076 status1 = serial_inp(up, UART_MSR) & 0xF0;
1077 serial_outp(up, UART_MCR, save_mcr);
1078 if (status1 != 0x90) {
1079 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1080 status1);
1081 goto out;
1082 }
1083 }
1084
1085 /*
1086 * We're pretty sure there's a port here. Lets find out what
1087 * type of port it is. The IIR top two bits allows us to find
Russell King6f0d6182005-09-09 16:17:58 +01001088 * out if it's 8250 or 16450, 16550, 16550A or later. This
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 * determines what we test for next.
1090 *
1091 * We also initialise the EFR (if any) to zero for later. The
1092 * EFR occupies the same register location as the FCR and IIR.
1093 */
1094 serial_outp(up, UART_LCR, 0xBF);
1095 serial_outp(up, UART_EFR, 0);
1096 serial_outp(up, UART_LCR, 0);
1097
1098 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1099 scratch = serial_in(up, UART_IIR) >> 6;
1100
1101 DEBUG_AUTOCONF("iir=%d ", scratch);
1102
1103 switch (scratch) {
1104 case 0:
1105 autoconfig_8250(up);
1106 break;
1107 case 1:
1108 up->port.type = PORT_UNKNOWN;
1109 break;
1110 case 2:
1111 up->port.type = PORT_16550;
1112 break;
1113 case 3:
1114 autoconfig_16550a(up);
1115 break;
1116 }
1117
1118#ifdef CONFIG_SERIAL_8250_RSA
1119 /*
1120 * Only probe for RSA ports if we got the region.
1121 */
1122 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1123 int i;
1124
1125 for (i = 0 ; i < probe_rsa_count; ++i) {
1126 if (probe_rsa[i] == up->port.iobase &&
1127 __enable_rsa(up)) {
1128 up->port.type = PORT_RSA;
1129 break;
1130 }
1131 }
1132 }
1133#endif
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001134
1135#ifdef CONFIG_SERIAL_8250_AU1X00
1136 /* if access method is AU, it is a 16550 with a quirk */
1137 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
1138 up->bugs |= UART_BUG_NOMSR;
1139#endif
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 serial_outp(up, UART_LCR, save_lcr);
1142
1143 if (up->capabilities != uart_config[up->port.type].flags) {
1144 printk(KERN_WARNING
1145 "ttyS%d: detected caps %08x should be %08x\n",
David S. Miller84408382008-10-13 10:45:26 +01001146 serial_index(&up->port), up->capabilities,
1147 uart_config[up->port.type].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149
1150 up->port.fifosize = uart_config[up->port.type].fifo_size;
1151 up->capabilities = uart_config[up->port.type].flags;
1152 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1153
1154 if (up->port.type == PORT_UNKNOWN)
1155 goto out;
1156
1157 /*
1158 * Reset the UART.
1159 */
1160#ifdef CONFIG_SERIAL_8250_RSA
1161 if (up->port.type == PORT_RSA)
1162 serial_outp(up, UART_RSA_FRR, 0);
1163#endif
1164 serial_outp(up, UART_MCR, save_mcr);
1165 serial8250_clear_fifos(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001166 serial_in(up, UART_RX);
Lennert Buytenhek5c8c7552005-11-12 21:58:05 +00001167 if (up->capabilities & UART_CAP_UUE)
1168 serial_outp(up, UART_IER, UART_IER_UUE);
1169 else
1170 serial_outp(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Thomas Koellerbd71c182007-05-06 14:48:47 -07001172 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 spin_unlock_irqrestore(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1175}
1176
1177static void autoconfig_irq(struct uart_8250_port *up)
1178{
1179 unsigned char save_mcr, save_ier;
1180 unsigned char save_ICP = 0;
1181 unsigned int ICP = 0;
1182 unsigned long irqs;
1183 int irq;
1184
1185 if (up->port.flags & UPF_FOURPORT) {
1186 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1187 save_ICP = inb_p(ICP);
1188 outb_p(0x80, ICP);
1189 (void) inb_p(ICP);
1190 }
1191
1192 /* forget possible initially masked and pending IRQ */
1193 probe_irq_off(probe_irq_on());
1194 save_mcr = serial_inp(up, UART_MCR);
1195 save_ier = serial_inp(up, UART_IER);
1196 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
Thomas Koellerbd71c182007-05-06 14:48:47 -07001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 irqs = probe_irq_on();
1199 serial_outp(up, UART_MCR, 0);
Alan Cox6f803cd2008-02-08 04:18:52 -08001200 udelay(10);
1201 if (up->port.flags & UPF_FOURPORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 serial_outp(up, UART_MCR,
1203 UART_MCR_DTR | UART_MCR_RTS);
1204 } else {
1205 serial_outp(up, UART_MCR,
1206 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1207 }
1208 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1209 (void)serial_inp(up, UART_LSR);
1210 (void)serial_inp(up, UART_RX);
1211 (void)serial_inp(up, UART_IIR);
1212 (void)serial_inp(up, UART_MSR);
1213 serial_outp(up, UART_TX, 0xFF);
Alan Cox6f803cd2008-02-08 04:18:52 -08001214 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 irq = probe_irq_off(irqs);
1216
1217 serial_outp(up, UART_MCR, save_mcr);
1218 serial_outp(up, UART_IER, save_ier);
1219
1220 if (up->port.flags & UPF_FOURPORT)
1221 outb_p(save_ICP, ICP);
1222
1223 up->port.irq = (irq > 0) ? irq : 0;
1224}
1225
Russell Kinge763b902005-06-29 18:41:51 +01001226static inline void __stop_tx(struct uart_8250_port *p)
1227{
1228 if (p->ier & UART_IER_THRI) {
1229 p->ier &= ~UART_IER_THRI;
1230 serial_out(p, UART_IER, p->ier);
1231 }
1232}
1233
Russell Kingb129a8c2005-08-31 10:12:14 +01001234static void serial8250_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 struct uart_8250_port *up = (struct uart_8250_port *)port;
1237
Russell Kinge763b902005-06-29 18:41:51 +01001238 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /*
Russell Kinge763b902005-06-29 18:41:51 +01001241 * We really want to stop the transmitter from sending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
Russell Kinge763b902005-06-29 18:41:51 +01001243 if (up->port.type == PORT_16C950) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 up->acr |= UART_ACR_TXDIS;
1245 serial_icr_write(up, UART_ACR, up->acr);
1246 }
1247}
1248
Russell King55d3b282005-06-23 15:05:41 +01001249static void transmit_chars(struct uart_8250_port *up);
1250
Russell Kingb129a8c2005-08-31 10:12:14 +01001251static void serial8250_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 struct uart_8250_port *up = (struct uart_8250_port *)port;
1254
1255 if (!(up->ier & UART_IER_THRI)) {
1256 up->ier |= UART_IER_THRI;
1257 serial_out(up, UART_IER, up->ier);
Russell King55d3b282005-06-23 15:05:41 +01001258
Russell King67f76542005-06-23 22:26:43 +01001259 if (up->bugs & UART_BUG_TXEN) {
Russell King55d3b282005-06-23 15:05:41 +01001260 unsigned char lsr, iir;
1261 lsr = serial_in(up, UART_LSR);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001262 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Thomas Koellerbd71c182007-05-06 14:48:47 -07001263 iir = serial_in(up, UART_IIR) & 0x0f;
1264 if ((up->port.type == PORT_RM9000) ?
1265 (lsr & UART_LSR_THRE &&
1266 (iir == UART_IIR_NO_INT || iir == UART_IIR_THRI)) :
1267 (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT))
Russell King55d3b282005-06-23 15:05:41 +01001268 transmit_chars(up);
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
Russell Kinge763b902005-06-29 18:41:51 +01001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 /*
Russell Kinge763b902005-06-29 18:41:51 +01001273 * Re-enable the transmitter if we disabled it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 */
Russell Kinge763b902005-06-29 18:41:51 +01001275 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 up->acr &= ~UART_ACR_TXDIS;
1277 serial_icr_write(up, UART_ACR, up->acr);
1278 }
1279}
1280
1281static void serial8250_stop_rx(struct uart_port *port)
1282{
1283 struct uart_8250_port *up = (struct uart_8250_port *)port;
1284
1285 up->ier &= ~UART_IER_RLSI;
1286 up->port.read_status_mask &= ~UART_LSR_DR;
1287 serial_out(up, UART_IER, up->ier);
1288}
1289
1290static void serial8250_enable_ms(struct uart_port *port)
1291{
1292 struct uart_8250_port *up = (struct uart_8250_port *)port;
1293
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001294 /* no MSR capabilities */
1295 if (up->bugs & UART_BUG_NOMSR)
1296 return;
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 up->ier |= UART_IER_MSI;
1299 serial_out(up, UART_IER, up->ier);
1300}
1301
Russell Kingea8874d2006-01-04 19:43:24 +00001302static void
Thomas Koellercc79aa92007-02-20 13:58:05 -08001303receive_chars(struct uart_8250_port *up, unsigned int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001305 struct tty_struct *tty = up->port.info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 unsigned char ch, lsr = *status;
1307 int max_count = 256;
1308 char flag;
1309
1310 do {
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001311 if (likely(lsr & UART_LSR_DR))
1312 ch = serial_inp(up, UART_RX);
1313 else
1314 /*
1315 * Intel 82571 has a Serial Over Lan device that will
1316 * set UART_LSR_BI without setting UART_LSR_DR when
1317 * it receives a break. To avoid reading from the
1318 * receive buffer without UART_LSR_DR bit set, we
1319 * just force the read character to be 0
1320 */
1321 ch = 0;
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 flag = TTY_NORMAL;
1324 up->port.icount.rx++;
1325
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001326 lsr |= up->lsr_saved_flags;
1327 up->lsr_saved_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001329 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 /*
1331 * For statistics only
1332 */
1333 if (lsr & UART_LSR_BI) {
1334 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1335 up->port.icount.brk++;
1336 /*
1337 * We do the SysRQ and SAK checking
1338 * here because otherwise the break
1339 * may get masked by ignore_status_mask
1340 * or read_status_mask.
1341 */
1342 if (uart_handle_break(&up->port))
1343 goto ignore_char;
1344 } else if (lsr & UART_LSR_PE)
1345 up->port.icount.parity++;
1346 else if (lsr & UART_LSR_FE)
1347 up->port.icount.frame++;
1348 if (lsr & UART_LSR_OE)
1349 up->port.icount.overrun++;
1350
1351 /*
Russell King23907eb2005-04-16 15:26:39 -07001352 * Mask off conditions which should be ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 */
1354 lsr &= up->port.read_status_mask;
1355
1356 if (lsr & UART_LSR_BI) {
1357 DEBUG_INTR("handling break....");
1358 flag = TTY_BREAK;
1359 } else if (lsr & UART_LSR_PE)
1360 flag = TTY_PARITY;
1361 else if (lsr & UART_LSR_FE)
1362 flag = TTY_FRAME;
1363 }
David Howells7d12e782006-10-05 14:55:46 +01001364 if (uart_handle_sysrq_char(&up->port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 goto ignore_char;
Russell King05ab3012005-05-09 23:21:59 +01001366
1367 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1368
Alan Cox6f803cd2008-02-08 04:18:52 -08001369ignore_char:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 lsr = serial_inp(up, UART_LSR);
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001371 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 spin_unlock(&up->port.lock);
1373 tty_flip_buffer_push(tty);
1374 spin_lock(&up->port.lock);
1375 *status = lsr;
1376}
1377
Russell Kingea8874d2006-01-04 19:43:24 +00001378static void transmit_chars(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 struct circ_buf *xmit = &up->port.info->xmit;
1381 int count;
1382
1383 if (up->port.x_char) {
1384 serial_outp(up, UART_TX, up->port.x_char);
1385 up->port.icount.tx++;
1386 up->port.x_char = 0;
1387 return;
1388 }
Russell Kingb129a8c2005-08-31 10:12:14 +01001389 if (uart_tx_stopped(&up->port)) {
1390 serial8250_stop_tx(&up->port);
1391 return;
1392 }
1393 if (uart_circ_empty(xmit)) {
Russell Kinge763b902005-06-29 18:41:51 +01001394 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return;
1396 }
1397
1398 count = up->tx_loadsz;
1399 do {
1400 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1401 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1402 up->port.icount.tx++;
1403 if (uart_circ_empty(xmit))
1404 break;
1405 } while (--count > 0);
1406
1407 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1408 uart_write_wakeup(&up->port);
1409
1410 DEBUG_INTR("THRE...");
1411
1412 if (uart_circ_empty(xmit))
Russell Kinge763b902005-06-29 18:41:51 +01001413 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Russell King2af7cd62006-01-04 16:55:09 +00001416static unsigned int check_modem_status(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Russell King2af7cd62006-01-04 16:55:09 +00001418 unsigned int status = serial_in(up, UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001420 status |= up->msr_saved_flags;
1421 up->msr_saved_flags = 0;
Taku Izumifdc30b32007-04-23 14:41:00 -07001422 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1423 up->port.info != NULL) {
Russell King2af7cd62006-01-04 16:55:09 +00001424 if (status & UART_MSR_TERI)
1425 up->port.icount.rng++;
1426 if (status & UART_MSR_DDSR)
1427 up->port.icount.dsr++;
1428 if (status & UART_MSR_DDCD)
1429 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1430 if (status & UART_MSR_DCTS)
1431 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Russell King2af7cd62006-01-04 16:55:09 +00001433 wake_up_interruptible(&up->port.info->delta_msr_wait);
1434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Russell King2af7cd62006-01-04 16:55:09 +00001436 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
1439/*
1440 * This handles the interrupt from one port.
1441 */
Will Newtonb5d674a2008-10-13 10:36:21 +01001442static void serial8250_handle_port(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Russell King45e24602006-01-04 19:19:06 +00001444 unsigned int status;
Jiri Kosina4bf36312007-04-23 14:41:21 -07001445 unsigned long flags;
Russell King45e24602006-01-04 19:19:06 +00001446
Jiri Kosina4bf36312007-04-23 14:41:21 -07001447 spin_lock_irqsave(&up->port.lock, flags);
Russell King45e24602006-01-04 19:19:06 +00001448
1449 status = serial_inp(up, UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 DEBUG_INTR("status = %x...", status);
1452
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001453 if (status & (UART_LSR_DR | UART_LSR_BI))
David Howells7d12e782006-10-05 14:55:46 +01001454 receive_chars(up, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 check_modem_status(up);
1456 if (status & UART_LSR_THRE)
1457 transmit_chars(up);
Russell King45e24602006-01-04 19:19:06 +00001458
Jiri Kosina4bf36312007-04-23 14:41:21 -07001459 spin_unlock_irqrestore(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
1462/*
1463 * This is the serial driver's interrupt routine.
1464 *
1465 * Arjan thinks the old way was overly complex, so it got simplified.
1466 * Alan disagrees, saying that need the complexity to handle the weird
1467 * nature of ISA shared interrupts. (This is a special exception.)
1468 *
1469 * In order to handle ISA shared interrupts properly, we need to check
1470 * that all ports have been serviced, and therefore the ISA interrupt
1471 * line has been de-asserted.
1472 *
1473 * This means we need to loop through all ports. checking that they
1474 * don't have an interrupt pending.
1475 */
David Howells7d12e782006-10-05 14:55:46 +01001476static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct irq_info *i = dev_id;
1479 struct list_head *l, *end = NULL;
1480 int pass_counter = 0, handled = 0;
1481
1482 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1483
1484 spin_lock(&i->lock);
1485
1486 l = i->head;
1487 do {
1488 struct uart_8250_port *up;
1489 unsigned int iir;
1490
1491 up = list_entry(l, struct uart_8250_port, list);
1492
1493 iir = serial_in(up, UART_IIR);
1494 if (!(iir & UART_IIR_NO_INT)) {
David Howells7d12e782006-10-05 14:55:46 +01001495 serial8250_handle_port(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 handled = 1;
1498
1499 end = NULL;
Marc St-Jeanbeab6972007-05-06 14:48:45 -07001500 } else if (up->port.iotype == UPIO_DWAPB &&
1501 (iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
1502 /* The DesignWare APB UART has an Busy Detect (0x07)
1503 * interrupt meaning an LCR write attempt occured while the
1504 * UART was busy. The interrupt must be cleared by reading
1505 * the UART status register (USR) and the LCR re-written. */
1506 unsigned int status;
1507 status = *(volatile u32 *)up->port.private_data;
1508 serial_out(up, UART_LCR, up->lcr);
1509
1510 handled = 1;
1511
1512 end = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 } else if (end == NULL)
1514 end = l;
1515
1516 l = l->next;
1517
1518 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1519 /* If we hit this, we're dead. */
1520 printk(KERN_ERR "serial8250: too much work for "
1521 "irq%d\n", irq);
1522 break;
1523 }
1524 } while (l != end);
1525
1526 spin_unlock(&i->lock);
1527
1528 DEBUG_INTR("end.\n");
1529
1530 return IRQ_RETVAL(handled);
1531}
1532
1533/*
1534 * To support ISA shared interrupts, we need to have one interrupt
1535 * handler that ensures that the IRQ line has been deasserted
1536 * before returning. Failing to do this will result in the IRQ
1537 * line being stuck active, and, since ISA irqs are edge triggered,
1538 * no more IRQs will be seen.
1539 */
1540static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1541{
1542 spin_lock_irq(&i->lock);
1543
1544 if (!list_empty(i->head)) {
1545 if (i->head == &up->list)
1546 i->head = i->head->next;
1547 list_del(&up->list);
1548 } else {
1549 BUG_ON(i->head != &up->list);
1550 i->head = NULL;
1551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 spin_unlock_irq(&i->lock);
Alan Cox25db8ad2008-08-19 20:49:40 -07001553 /* List empty so throw away the hash node */
1554 if (i->head == NULL) {
1555 hlist_del(&i->node);
1556 kfree(i);
1557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
1560static int serial_link_irq_chain(struct uart_8250_port *up)
1561{
Alan Cox25db8ad2008-08-19 20:49:40 -07001562 struct hlist_head *h;
1563 struct hlist_node *n;
1564 struct irq_info *i;
Thomas Gleixner40663cc2006-07-01 19:29:43 -07001565 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Alan Cox25db8ad2008-08-19 20:49:40 -07001567 mutex_lock(&hash_mutex);
1568
1569 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1570
1571 hlist_for_each(n, h) {
1572 i = hlist_entry(n, struct irq_info, node);
1573 if (i->irq == up->port.irq)
1574 break;
1575 }
1576
1577 if (n == NULL) {
1578 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
1579 if (i == NULL) {
1580 mutex_unlock(&hash_mutex);
1581 return -ENOMEM;
1582 }
1583 spin_lock_init(&i->lock);
1584 i->irq = up->port.irq;
1585 hlist_add_head(&i->node, h);
1586 }
1587 mutex_unlock(&hash_mutex);
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 spin_lock_irq(&i->lock);
1590
1591 if (i->head) {
1592 list_add(&up->list, i->head);
1593 spin_unlock_irq(&i->lock);
1594
1595 ret = 0;
1596 } else {
1597 INIT_LIST_HEAD(&up->list);
1598 i->head = &up->list;
1599 spin_unlock_irq(&i->lock);
1600
1601 ret = request_irq(up->port.irq, serial8250_interrupt,
1602 irq_flags, "serial", i);
1603 if (ret < 0)
1604 serial_do_unlink(i, up);
1605 }
1606
1607 return ret;
1608}
1609
1610static void serial_unlink_irq_chain(struct uart_8250_port *up)
1611{
Alan Cox25db8ad2008-08-19 20:49:40 -07001612 struct irq_info *i;
1613 struct hlist_node *n;
1614 struct hlist_head *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Alan Cox25db8ad2008-08-19 20:49:40 -07001616 mutex_lock(&hash_mutex);
1617
1618 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1619
1620 hlist_for_each(n, h) {
1621 i = hlist_entry(n, struct irq_info, node);
1622 if (i->irq == up->port.irq)
1623 break;
1624 }
1625
1626 BUG_ON(n == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 BUG_ON(i->head == NULL);
1628
1629 if (list_empty(i->head))
1630 free_irq(up->port.irq, i);
1631
1632 serial_do_unlink(i, up);
Alan Cox25db8ad2008-08-19 20:49:40 -07001633 mutex_unlock(&hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
Alex Williamson40b36da2007-02-14 00:33:04 -08001636/* Base timer interval for polling */
1637static inline int poll_timeout(int timeout)
1638{
1639 return timeout > 6 ? (timeout / 2 - 2) : 1;
1640}
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642/*
1643 * This function is used to handle ports that do not have an
1644 * interrupt. This doesn't work very well for 16450's, but gives
1645 * barely passable results for a 16550A. (Although at the expense
1646 * of much CPU overhead).
1647 */
1648static void serial8250_timeout(unsigned long data)
1649{
1650 struct uart_8250_port *up = (struct uart_8250_port *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 unsigned int iir;
1652
1653 iir = serial_in(up, UART_IIR);
Russell King45e24602006-01-04 19:19:06 +00001654 if (!(iir & UART_IIR_NO_INT))
David Howells7d12e782006-10-05 14:55:46 +01001655 serial8250_handle_port(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001656 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
1657}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Alex Williamson40b36da2007-02-14 00:33:04 -08001659static void serial8250_backup_timeout(unsigned long data)
1660{
1661 struct uart_8250_port *up = (struct uart_8250_port *)data;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001662 unsigned int iir, ier = 0, lsr;
1663 unsigned long flags;
Alex Williamson40b36da2007-02-14 00:33:04 -08001664
1665 /*
1666 * Must disable interrupts or else we risk racing with the interrupt
1667 * based handler.
1668 */
1669 if (is_real_interrupt(up->port.irq)) {
1670 ier = serial_in(up, UART_IER);
1671 serial_out(up, UART_IER, 0);
1672 }
1673
1674 iir = serial_in(up, UART_IIR);
1675
1676 /*
1677 * This should be a safe test for anyone who doesn't trust the
1678 * IIR bits on their UART, but it's specifically designed for
1679 * the "Diva" UART used on the management processor on many HP
1680 * ia64 and parisc boxes.
1681 */
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001682 spin_lock_irqsave(&up->port.lock, flags);
1683 lsr = serial_in(up, UART_LSR);
1684 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1685 spin_unlock_irqrestore(&up->port.lock, flags);
Alex Williamson40b36da2007-02-14 00:33:04 -08001686 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
1687 (!uart_circ_empty(&up->port.info->xmit) || up->port.x_char) &&
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001688 (lsr & UART_LSR_THRE)) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001689 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1690 iir |= UART_IIR_THRI;
1691 }
1692
1693 if (!(iir & UART_IIR_NO_INT))
1694 serial8250_handle_port(up);
1695
1696 if (is_real_interrupt(up->port.irq))
1697 serial_out(up, UART_IER, ier);
1698
1699 /* Standard timer interval plus 0.2s to keep the port running */
Alan Cox6f803cd2008-02-08 04:18:52 -08001700 mod_timer(&up->timer,
1701 jiffies + poll_timeout(up->port.timeout) + HZ / 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
1704static unsigned int serial8250_tx_empty(struct uart_port *port)
1705{
1706 struct uart_8250_port *up = (struct uart_8250_port *)port;
1707 unsigned long flags;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001708 unsigned int lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 spin_lock_irqsave(&up->port.lock, flags);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001711 lsr = serial_in(up, UART_LSR);
1712 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 spin_unlock_irqrestore(&up->port.lock, flags);
1714
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001715 return lsr & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
1718static unsigned int serial8250_get_mctrl(struct uart_port *port)
1719{
1720 struct uart_8250_port *up = (struct uart_8250_port *)port;
Russell King2af7cd62006-01-04 16:55:09 +00001721 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 unsigned int ret;
1723
Russell King2af7cd62006-01-04 16:55:09 +00001724 status = check_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 ret = 0;
1727 if (status & UART_MSR_DCD)
1728 ret |= TIOCM_CAR;
1729 if (status & UART_MSR_RI)
1730 ret |= TIOCM_RNG;
1731 if (status & UART_MSR_DSR)
1732 ret |= TIOCM_DSR;
1733 if (status & UART_MSR_CTS)
1734 ret |= TIOCM_CTS;
1735 return ret;
1736}
1737
1738static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1739{
1740 struct uart_8250_port *up = (struct uart_8250_port *)port;
1741 unsigned char mcr = 0;
1742
1743 if (mctrl & TIOCM_RTS)
1744 mcr |= UART_MCR_RTS;
1745 if (mctrl & TIOCM_DTR)
1746 mcr |= UART_MCR_DTR;
1747 if (mctrl & TIOCM_OUT1)
1748 mcr |= UART_MCR_OUT1;
1749 if (mctrl & TIOCM_OUT2)
1750 mcr |= UART_MCR_OUT2;
1751 if (mctrl & TIOCM_LOOP)
1752 mcr |= UART_MCR_LOOP;
1753
1754 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1755
1756 serial_out(up, UART_MCR, mcr);
1757}
1758
1759static void serial8250_break_ctl(struct uart_port *port, int break_state)
1760{
1761 struct uart_8250_port *up = (struct uart_8250_port *)port;
1762 unsigned long flags;
1763
1764 spin_lock_irqsave(&up->port.lock, flags);
1765 if (break_state == -1)
1766 up->lcr |= UART_LCR_SBC;
1767 else
1768 up->lcr &= ~UART_LCR_SBC;
1769 serial_out(up, UART_LCR, up->lcr);
1770 spin_unlock_irqrestore(&up->port.lock, flags);
1771}
1772
Alex Williamson40b36da2007-02-14 00:33:04 -08001773#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1774
1775/*
1776 * Wait for transmitter & holding register to empty
1777 */
Will Newtonb5d674a2008-10-13 10:36:21 +01001778static void wait_for_xmitr(struct uart_8250_port *up, int bits)
Alex Williamson40b36da2007-02-14 00:33:04 -08001779{
1780 unsigned int status, tmout = 10000;
1781
1782 /* Wait up to 10ms for the character(s) to be sent. */
1783 do {
1784 status = serial_in(up, UART_LSR);
1785
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001786 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -08001787
1788 if (--tmout == 0)
1789 break;
1790 udelay(1);
1791 } while ((status & bits) != bits);
1792
1793 /* Wait up to 1s for flow control if necessary */
1794 if (up->port.flags & UPF_CONS_FLOW) {
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001795 unsigned int tmout;
1796 for (tmout = 1000000; tmout; tmout--) {
1797 unsigned int msr = serial_in(up, UART_MSR);
1798 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1799 if (msr & UART_MSR_CTS)
1800 break;
Alex Williamson40b36da2007-02-14 00:33:04 -08001801 udelay(1);
1802 touch_nmi_watchdog();
1803 }
1804 }
1805}
1806
Jason Wesself2d937f2008-04-17 20:05:37 +02001807#ifdef CONFIG_CONSOLE_POLL
1808/*
1809 * Console polling routines for writing and reading from the uart while
1810 * in an interrupt or debug context.
1811 */
1812
1813static int serial8250_get_poll_char(struct uart_port *port)
1814{
1815 struct uart_8250_port *up = (struct uart_8250_port *)port;
1816 unsigned char lsr = serial_inp(up, UART_LSR);
1817
1818 while (!(lsr & UART_LSR_DR))
1819 lsr = serial_inp(up, UART_LSR);
1820
1821 return serial_inp(up, UART_RX);
1822}
1823
1824
1825static void serial8250_put_poll_char(struct uart_port *port,
1826 unsigned char c)
1827{
1828 unsigned int ier;
1829 struct uart_8250_port *up = (struct uart_8250_port *)port;
1830
1831 /*
1832 * First save the IER then disable the interrupts
1833 */
1834 ier = serial_in(up, UART_IER);
1835 if (up->capabilities & UART_CAP_UUE)
1836 serial_out(up, UART_IER, UART_IER_UUE);
1837 else
1838 serial_out(up, UART_IER, 0);
1839
1840 wait_for_xmitr(up, BOTH_EMPTY);
1841 /*
1842 * Send the character out.
1843 * If a LF, also do CR...
1844 */
1845 serial_out(up, UART_TX, c);
1846 if (c == 10) {
1847 wait_for_xmitr(up, BOTH_EMPTY);
1848 serial_out(up, UART_TX, 13);
1849 }
1850
1851 /*
1852 * Finally, wait for transmitter to become empty
1853 * and restore the IER
1854 */
1855 wait_for_xmitr(up, BOTH_EMPTY);
1856 serial_out(up, UART_IER, ier);
1857}
1858
1859#endif /* CONFIG_CONSOLE_POLL */
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861static int serial8250_startup(struct uart_port *port)
1862{
1863 struct uart_8250_port *up = (struct uart_8250_port *)port;
1864 unsigned long flags;
Russell King55d3b282005-06-23 15:05:41 +01001865 unsigned char lsr, iir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 int retval;
1867
1868 up->capabilities = uart_config[up->port.type].flags;
1869 up->mcr = 0;
1870
1871 if (up->port.type == PORT_16C950) {
1872 /* Wake up and initialize UART */
1873 up->acr = 0;
1874 serial_outp(up, UART_LCR, 0xBF);
1875 serial_outp(up, UART_EFR, UART_EFR_ECB);
1876 serial_outp(up, UART_IER, 0);
1877 serial_outp(up, UART_LCR, 0);
1878 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
1879 serial_outp(up, UART_LCR, 0xBF);
1880 serial_outp(up, UART_EFR, UART_EFR_ECB);
1881 serial_outp(up, UART_LCR, 0);
1882 }
1883
1884#ifdef CONFIG_SERIAL_8250_RSA
1885 /*
1886 * If this is an RSA port, see if we can kick it up to the
1887 * higher speed clock.
1888 */
1889 enable_rsa(up);
1890#endif
1891
1892 /*
1893 * Clear the FIFO buffers and disable them.
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08001894 * (they will be reenabled in set_termios())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 */
1896 serial8250_clear_fifos(up);
1897
1898 /*
1899 * Clear the interrupt registers.
1900 */
1901 (void) serial_inp(up, UART_LSR);
1902 (void) serial_inp(up, UART_RX);
1903 (void) serial_inp(up, UART_IIR);
1904 (void) serial_inp(up, UART_MSR);
1905
1906 /*
1907 * At this point, there's no way the LSR could still be 0xff;
1908 * if it is, then bail out, because there's likely no UART
1909 * here.
1910 */
1911 if (!(up->port.flags & UPF_BUGGY_UART) &&
1912 (serial_inp(up, UART_LSR) == 0xff)) {
David S. Miller84408382008-10-13 10:45:26 +01001913 printk(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
1914 serial_index(&up->port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 return -ENODEV;
1916 }
1917
1918 /*
1919 * For a XR16C850, we need to set the trigger levels
1920 */
1921 if (up->port.type == PORT_16850) {
1922 unsigned char fctr;
1923
1924 serial_outp(up, UART_LCR, 0xbf);
1925
1926 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
1927 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
1928 serial_outp(up, UART_TRG, UART_TRG_96);
1929 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
1930 serial_outp(up, UART_TRG, UART_TRG_96);
1931
1932 serial_outp(up, UART_LCR, 0);
1933 }
1934
Alex Williamson40b36da2007-02-14 00:33:04 -08001935 if (is_real_interrupt(up->port.irq)) {
Alex Williamson01c194d2008-04-28 02:14:09 -07001936 unsigned char iir1;
Alex Williamson40b36da2007-02-14 00:33:04 -08001937 /*
1938 * Test for UARTs that do not reassert THRE when the
1939 * transmitter is idle and the interrupt has already
1940 * been cleared. Real 16550s should always reassert
1941 * this interrupt whenever the transmitter is idle and
1942 * the interrupt is enabled. Delays are necessary to
1943 * allow register changes to become visible.
1944 */
Borislav Petkovc389d272008-07-29 22:33:32 -07001945 spin_lock_irqsave(&up->port.lock, flags);
Anton Vorontsov768aec02008-07-22 11:21:07 +01001946 if (up->port.flags & UPF_SHARE_IRQ)
1947 disable_irq_nosync(up->port.irq);
Alex Williamson40b36da2007-02-14 00:33:04 -08001948
1949 wait_for_xmitr(up, UART_LSR_THRE);
1950 serial_out_sync(up, UART_IER, UART_IER_THRI);
1951 udelay(1); /* allow THRE to set */
Alex Williamson01c194d2008-04-28 02:14:09 -07001952 iir1 = serial_in(up, UART_IIR);
Alex Williamson40b36da2007-02-14 00:33:04 -08001953 serial_out(up, UART_IER, 0);
1954 serial_out_sync(up, UART_IER, UART_IER_THRI);
1955 udelay(1); /* allow a working UART time to re-assert THRE */
1956 iir = serial_in(up, UART_IIR);
1957 serial_out(up, UART_IER, 0);
1958
Anton Vorontsov768aec02008-07-22 11:21:07 +01001959 if (up->port.flags & UPF_SHARE_IRQ)
1960 enable_irq(up->port.irq);
Borislav Petkovc389d272008-07-29 22:33:32 -07001961 spin_unlock_irqrestore(&up->port.lock, flags);
Alex Williamson40b36da2007-02-14 00:33:04 -08001962
1963 /*
1964 * If the interrupt is not reasserted, setup a timer to
1965 * kick the UART on a regular basis.
1966 */
Alex Williamson01c194d2008-04-28 02:14:09 -07001967 if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
Will Newton363f66f2008-09-02 14:35:44 -07001968 up->bugs |= UART_BUG_THRE;
David S. Miller84408382008-10-13 10:45:26 +01001969 pr_debug("ttyS%d - using backup timer\n",
1970 serial_index(port));
Alex Williamson40b36da2007-02-14 00:33:04 -08001971 }
1972 }
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /*
Will Newton363f66f2008-09-02 14:35:44 -07001975 * The above check will only give an accurate result the first time
1976 * the port is opened so this value needs to be preserved.
1977 */
1978 if (up->bugs & UART_BUG_THRE) {
1979 up->timer.function = serial8250_backup_timeout;
1980 up->timer.data = (unsigned long)up;
1981 mod_timer(&up->timer, jiffies +
1982 poll_timeout(up->port.timeout) + HZ / 5);
1983 }
1984
1985 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 * If the "interrupt" for this port doesn't correspond with any
1987 * hardware interrupt, we use a timer-based system. The original
1988 * driver used to do this with IRQ0.
1989 */
1990 if (!is_real_interrupt(up->port.irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 up->timer.data = (unsigned long)up;
Alex Williamson40b36da2007-02-14 00:33:04 -08001992 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 } else {
1994 retval = serial_link_irq_chain(up);
1995 if (retval)
1996 return retval;
1997 }
1998
1999 /*
2000 * Now, initialize the UART
2001 */
2002 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
2003
2004 spin_lock_irqsave(&up->port.lock, flags);
2005 if (up->port.flags & UPF_FOURPORT) {
2006 if (!is_real_interrupt(up->port.irq))
2007 up->port.mctrl |= TIOCM_OUT1;
2008 } else
2009 /*
2010 * Most PC uarts need OUT2 raised to enable interrupts.
2011 */
2012 if (is_real_interrupt(up->port.irq))
2013 up->port.mctrl |= TIOCM_OUT2;
2014
2015 serial8250_set_mctrl(&up->port, up->port.mctrl);
Russell King55d3b282005-06-23 15:05:41 +01002016
2017 /*
2018 * Do a quick test to see if we receive an
2019 * interrupt when we enable the TX irq.
2020 */
2021 serial_outp(up, UART_IER, UART_IER_THRI);
2022 lsr = serial_in(up, UART_LSR);
2023 iir = serial_in(up, UART_IIR);
2024 serial_outp(up, UART_IER, 0);
2025
2026 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
Russell King67f76542005-06-23 22:26:43 +01002027 if (!(up->bugs & UART_BUG_TXEN)) {
2028 up->bugs |= UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002029 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
David S. Miller84408382008-10-13 10:45:26 +01002030 serial_index(port));
Russell King55d3b282005-06-23 15:05:41 +01002031 }
2032 } else {
Russell King67f76542005-06-23 22:26:43 +01002033 up->bugs &= ~UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01002034 }
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 spin_unlock_irqrestore(&up->port.lock, flags);
2037
2038 /*
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002039 * Clear the interrupt registers again for luck, and clear the
2040 * saved flags to avoid getting false values from polling
2041 * routines or the previous session.
2042 */
2043 serial_inp(up, UART_LSR);
2044 serial_inp(up, UART_RX);
2045 serial_inp(up, UART_IIR);
2046 serial_inp(up, UART_MSR);
2047 up->lsr_saved_flags = 0;
2048 up->msr_saved_flags = 0;
2049
2050 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 * Finally, enable interrupts. Note: Modem status interrupts
2052 * are set via set_termios(), which will be occurring imminently
2053 * anyway, so we don't enable them here.
2054 */
2055 up->ier = UART_IER_RLSI | UART_IER_RDI;
2056 serial_outp(up, UART_IER, up->ier);
2057
2058 if (up->port.flags & UPF_FOURPORT) {
2059 unsigned int icp;
2060 /*
2061 * Enable interrupts on the AST Fourport board
2062 */
2063 icp = (up->port.iobase & 0xfe0) | 0x01f;
2064 outb_p(0x80, icp);
2065 (void) inb_p(icp);
2066 }
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return 0;
2069}
2070
2071static void serial8250_shutdown(struct uart_port *port)
2072{
2073 struct uart_8250_port *up = (struct uart_8250_port *)port;
2074 unsigned long flags;
2075
2076 /*
2077 * Disable interrupts from this port
2078 */
2079 up->ier = 0;
2080 serial_outp(up, UART_IER, 0);
2081
2082 spin_lock_irqsave(&up->port.lock, flags);
2083 if (up->port.flags & UPF_FOURPORT) {
2084 /* reset interrupts on the AST Fourport board */
2085 inb((up->port.iobase & 0xfe0) | 0x1f);
2086 up->port.mctrl |= TIOCM_OUT1;
2087 } else
2088 up->port.mctrl &= ~TIOCM_OUT2;
2089
2090 serial8250_set_mctrl(&up->port, up->port.mctrl);
2091 spin_unlock_irqrestore(&up->port.lock, flags);
2092
2093 /*
2094 * Disable break condition and FIFOs
2095 */
2096 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
2097 serial8250_clear_fifos(up);
2098
2099#ifdef CONFIG_SERIAL_8250_RSA
2100 /*
2101 * Reset the RSA board back to 115kbps compat mode.
2102 */
2103 disable_rsa(up);
2104#endif
2105
2106 /*
2107 * Read data port to reset things, and then unlink from
2108 * the IRQ chain.
2109 */
2110 (void) serial_in(up, UART_RX);
2111
Alex Williamson40b36da2007-02-14 00:33:04 -08002112 del_timer_sync(&up->timer);
2113 up->timer.function = serial8250_timeout;
2114 if (is_real_interrupt(up->port.irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 serial_unlink_irq_chain(up);
2116}
2117
2118static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
2119{
2120 unsigned int quot;
2121
2122 /*
2123 * Handle magic divisors for baud rates above baud_base on
2124 * SMSC SuperIO chips.
2125 */
2126 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2127 baud == (port->uartclk/4))
2128 quot = 0x8001;
2129 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2130 baud == (port->uartclk/8))
2131 quot = 0x8002;
2132 else
2133 quot = uart_get_divisor(port, baud);
2134
2135 return quot;
2136}
2137
2138static void
Alan Cox606d0992006-12-08 02:38:45 -08002139serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2140 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 struct uart_8250_port *up = (struct uart_8250_port *)port;
2143 unsigned char cval, fcr = 0;
2144 unsigned long flags;
2145 unsigned int baud, quot;
2146
2147 switch (termios->c_cflag & CSIZE) {
2148 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +01002149 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 break;
2151 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +01002152 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 break;
2154 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +01002155 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 break;
2157 default:
2158 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +01002159 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 break;
2161 }
2162
2163 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +01002164 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (termios->c_cflag & PARENB)
2166 cval |= UART_LCR_PARITY;
2167 if (!(termios->c_cflag & PARODD))
2168 cval |= UART_LCR_EPAR;
2169#ifdef CMSPAR
2170 if (termios->c_cflag & CMSPAR)
2171 cval |= UART_LCR_SPAR;
2172#endif
2173
2174 /*
2175 * Ask the core to calculate the divisor for us.
2176 */
Thomas Koellerbd71c182007-05-06 14:48:47 -07002177 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 quot = serial8250_get_divisor(port, baud);
2179
2180 /*
Russell King4ba5e352005-06-23 10:43:04 +01002181 * Oxford Semi 952 rev B workaround
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 */
Russell King4ba5e352005-06-23 10:43:04 +01002183 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
Alan Cox3e8d4e22008-02-04 22:27:53 -08002184 quot++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
2187 if (baud < 2400)
2188 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2189 else
2190 fcr = uart_config[up->port.type].fcr;
2191 }
2192
2193 /*
2194 * MCR-based auto flow control. When AFE is enabled, RTS will be
2195 * deasserted when the receive FIFO contains more characters than
2196 * the trigger, or the MCR RTS bit is cleared. In the case where
2197 * the remote UART is not using CTS auto flow control, we must
2198 * have sufficient FIFO entries for the latency of the remote
2199 * UART to respond. IOW, at least 32 bytes of FIFO.
2200 */
2201 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
2202 up->mcr &= ~UART_MCR_AFE;
2203 if (termios->c_cflag & CRTSCTS)
2204 up->mcr |= UART_MCR_AFE;
2205 }
2206
2207 /*
2208 * Ok, we're now changing the port state. Do it with
2209 * interrupts disabled.
2210 */
2211 spin_lock_irqsave(&up->port.lock, flags);
2212
2213 /*
2214 * Update the per-port timeout.
2215 */
2216 uart_update_timeout(port, termios->c_cflag, baud);
2217
2218 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2219 if (termios->c_iflag & INPCK)
2220 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2221 if (termios->c_iflag & (BRKINT | PARMRK))
2222 up->port.read_status_mask |= UART_LSR_BI;
2223
2224 /*
2225 * Characteres to ignore
2226 */
2227 up->port.ignore_status_mask = 0;
2228 if (termios->c_iflag & IGNPAR)
2229 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2230 if (termios->c_iflag & IGNBRK) {
2231 up->port.ignore_status_mask |= UART_LSR_BI;
2232 /*
2233 * If we're ignoring parity and break indicators,
2234 * ignore overruns too (for real raw support).
2235 */
2236 if (termios->c_iflag & IGNPAR)
2237 up->port.ignore_status_mask |= UART_LSR_OE;
2238 }
2239
2240 /*
2241 * ignore all characters if CREAD is not set
2242 */
2243 if ((termios->c_cflag & CREAD) == 0)
2244 up->port.ignore_status_mask |= UART_LSR_DR;
2245
2246 /*
2247 * CTS flow control flag and modem status interrupts
2248 */
2249 up->ier &= ~UART_IER_MSI;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002250 if (!(up->bugs & UART_BUG_NOMSR) &&
2251 UART_ENABLE_MS(&up->port, termios->c_cflag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 up->ier |= UART_IER_MSI;
2253 if (up->capabilities & UART_CAP_UUE)
2254 up->ier |= UART_IER_UUE | UART_IER_RTOIE;
2255
2256 serial_out(up, UART_IER, up->ier);
2257
2258 if (up->capabilities & UART_CAP_EFR) {
2259 unsigned char efr = 0;
2260 /*
2261 * TI16C752/Startech hardware flow control. FIXME:
2262 * - TI16C752 requires control thresholds to be set.
2263 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2264 */
2265 if (termios->c_cflag & CRTSCTS)
2266 efr |= UART_EFR_CTS;
2267
2268 serial_outp(up, UART_LCR, 0xBF);
2269 serial_outp(up, UART_EFR, efr);
2270 }
2271
Jonathan McDowell255341c2006-08-14 23:05:32 -07002272#ifdef CONFIG_ARCH_OMAP15XX
2273 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2274 if (cpu_is_omap1510() && is_omap_port((unsigned int)up->port.membase)) {
2275 if (baud == 115200) {
2276 quot = 1;
2277 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2278 } else
2279 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2280 }
2281#endif
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (up->capabilities & UART_NATSEMI) {
2284 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
2285 serial_outp(up, UART_LCR, 0xe0);
2286 } else {
2287 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2288 }
2289
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +01002290 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 /*
2293 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2294 * is written without DLAB set, this mode will be disabled.
2295 */
2296 if (up->port.type == PORT_16750)
2297 serial_outp(up, UART_FCR, fcr);
2298
2299 serial_outp(up, UART_LCR, cval); /* reset DLAB */
2300 up->lcr = cval; /* Save LCR */
2301 if (up->port.type != PORT_16750) {
2302 if (fcr & UART_FCR_ENABLE_FIFO) {
2303 /* emulated UARTs (Lucent Venus 167x) need two steps */
2304 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
2305 }
2306 serial_outp(up, UART_FCR, fcr); /* set fcr */
2307 }
2308 serial8250_set_mctrl(&up->port, up->port.mctrl);
2309 spin_unlock_irqrestore(&up->port.lock, flags);
Alan Coxe991a2b2008-04-28 02:14:06 -07002310 /* Don't rewrite B0 */
2311 if (tty_termios_baud_rate(termios))
2312 tty_termios_encode_baud_rate(termios, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
2315static void
2316serial8250_pm(struct uart_port *port, unsigned int state,
2317 unsigned int oldstate)
2318{
2319 struct uart_8250_port *p = (struct uart_8250_port *)port;
2320
2321 serial8250_set_sleep(p, state != 0);
2322
2323 if (p->pm)
2324 p->pm(port, state, oldstate);
2325}
2326
2327/*
2328 * Resource handling.
2329 */
2330static int serial8250_request_std_resource(struct uart_8250_port *up)
2331{
2332 unsigned int size = 8 << up->port.regshift;
2333 int ret = 0;
2334
2335 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002336 case UPIO_AU:
2337 size = 0x100000;
2338 /* fall thru */
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002339 case UPIO_TSI:
2340 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 case UPIO_MEM:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002342 case UPIO_DWAPB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (!up->port.mapbase)
2344 break;
2345
2346 if (!request_mem_region(up->port.mapbase, size, "serial")) {
2347 ret = -EBUSY;
2348 break;
2349 }
2350
2351 if (up->port.flags & UPF_IOREMAP) {
Alan Cox6f441fe2008-05-01 04:34:59 -07002352 up->port.membase = ioremap_nocache(up->port.mapbase,
2353 size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (!up->port.membase) {
2355 release_mem_region(up->port.mapbase, size);
2356 ret = -ENOMEM;
2357 }
2358 }
2359 break;
2360
2361 case UPIO_HUB6:
2362 case UPIO_PORT:
2363 if (!request_region(up->port.iobase, size, "serial"))
2364 ret = -EBUSY;
2365 break;
2366 }
2367 return ret;
2368}
2369
2370static void serial8250_release_std_resource(struct uart_8250_port *up)
2371{
2372 unsigned int size = 8 << up->port.regshift;
2373
2374 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002375 case UPIO_AU:
2376 size = 0x100000;
2377 /* fall thru */
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002378 case UPIO_TSI:
2379 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 case UPIO_MEM:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002381 case UPIO_DWAPB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (!up->port.mapbase)
2383 break;
2384
2385 if (up->port.flags & UPF_IOREMAP) {
2386 iounmap(up->port.membase);
2387 up->port.membase = NULL;
2388 }
2389
2390 release_mem_region(up->port.mapbase, size);
2391 break;
2392
2393 case UPIO_HUB6:
2394 case UPIO_PORT:
2395 release_region(up->port.iobase, size);
2396 break;
2397 }
2398}
2399
2400static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2401{
2402 unsigned long start = UART_RSA_BASE << up->port.regshift;
2403 unsigned int size = 8 << up->port.regshift;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002404 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 case UPIO_HUB6:
2408 case UPIO_PORT:
2409 start += up->port.iobase;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002410 if (request_region(start, size, "serial-rsa"))
2411 ret = 0;
2412 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 ret = -EBUSY;
2414 break;
2415 }
2416
2417 return ret;
2418}
2419
2420static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2421{
2422 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2423 unsigned int size = 8 << up->port.regshift;
2424
2425 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 case UPIO_HUB6:
2427 case UPIO_PORT:
2428 release_region(up->port.iobase + offset, size);
2429 break;
2430 }
2431}
2432
2433static void serial8250_release_port(struct uart_port *port)
2434{
2435 struct uart_8250_port *up = (struct uart_8250_port *)port;
2436
2437 serial8250_release_std_resource(up);
2438 if (up->port.type == PORT_RSA)
2439 serial8250_release_rsa_resource(up);
2440}
2441
2442static int serial8250_request_port(struct uart_port *port)
2443{
2444 struct uart_8250_port *up = (struct uart_8250_port *)port;
2445 int ret = 0;
2446
2447 ret = serial8250_request_std_resource(up);
2448 if (ret == 0 && up->port.type == PORT_RSA) {
2449 ret = serial8250_request_rsa_resource(up);
2450 if (ret < 0)
2451 serial8250_release_std_resource(up);
2452 }
2453
2454 return ret;
2455}
2456
2457static void serial8250_config_port(struct uart_port *port, int flags)
2458{
2459 struct uart_8250_port *up = (struct uart_8250_port *)port;
2460 int probeflags = PROBE_ANY;
2461 int ret;
2462
2463 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 * Find the region that we can probe for. This in turn
2465 * tells us whether we can probe for the type of port.
2466 */
2467 ret = serial8250_request_std_resource(up);
2468 if (ret < 0)
2469 return;
2470
2471 ret = serial8250_request_rsa_resource(up);
2472 if (ret < 0)
2473 probeflags &= ~PROBE_RSA;
2474
2475 if (flags & UART_CONFIG_TYPE)
2476 autoconfig(up, probeflags);
2477 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2478 autoconfig_irq(up);
2479
2480 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2481 serial8250_release_rsa_resource(up);
2482 if (up->port.type == PORT_UNKNOWN)
2483 serial8250_release_std_resource(up);
2484}
2485
2486static int
2487serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2488{
2489 if (ser->irq >= NR_IRQS || ser->irq < 0 ||
2490 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2491 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2492 ser->type == PORT_STARTECH)
2493 return -EINVAL;
2494 return 0;
2495}
2496
2497static const char *
2498serial8250_type(struct uart_port *port)
2499{
2500 int type = port->type;
2501
2502 if (type >= ARRAY_SIZE(uart_config))
2503 type = 0;
2504 return uart_config[type].name;
2505}
2506
2507static struct uart_ops serial8250_pops = {
2508 .tx_empty = serial8250_tx_empty,
2509 .set_mctrl = serial8250_set_mctrl,
2510 .get_mctrl = serial8250_get_mctrl,
2511 .stop_tx = serial8250_stop_tx,
2512 .start_tx = serial8250_start_tx,
2513 .stop_rx = serial8250_stop_rx,
2514 .enable_ms = serial8250_enable_ms,
2515 .break_ctl = serial8250_break_ctl,
2516 .startup = serial8250_startup,
2517 .shutdown = serial8250_shutdown,
2518 .set_termios = serial8250_set_termios,
2519 .pm = serial8250_pm,
2520 .type = serial8250_type,
2521 .release_port = serial8250_release_port,
2522 .request_port = serial8250_request_port,
2523 .config_port = serial8250_config_port,
2524 .verify_port = serial8250_verify_port,
Jason Wesself2d937f2008-04-17 20:05:37 +02002525#ifdef CONFIG_CONSOLE_POLL
2526 .poll_get_char = serial8250_get_poll_char,
2527 .poll_put_char = serial8250_put_poll_char,
2528#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529};
2530
2531static struct uart_8250_port serial8250_ports[UART_NR];
2532
2533static void __init serial8250_isa_init_ports(void)
2534{
2535 struct uart_8250_port *up;
2536 static int first = 1;
2537 int i;
2538
2539 if (!first)
2540 return;
2541 first = 0;
2542
Dave Jonesa61c2d72006-01-07 23:18:19 +00002543 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 struct uart_8250_port *up = &serial8250_ports[i];
2545
2546 up->port.line = i;
2547 spin_lock_init(&up->port.lock);
2548
2549 init_timer(&up->timer);
2550 up->timer.function = serial8250_timeout;
2551
2552 /*
2553 * ALPHA_KLUDGE_MCR needs to be killed.
2554 */
2555 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2556 up->mcr_force = ALPHA_KLUDGE_MCR;
2557
2558 up->port.ops = &serial8250_pops;
2559 }
2560
Russell King44454bc2005-06-30 22:41:22 +01002561 for (i = 0, up = serial8250_ports;
Dave Jonesa61c2d72006-01-07 23:18:19 +00002562 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 i++, up++) {
2564 up->port.iobase = old_serial_port[i].port;
2565 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
2566 up->port.uartclk = old_serial_port[i].baud_base * 16;
2567 up->port.flags = old_serial_port[i].flags;
2568 up->port.hub6 = old_serial_port[i].hub6;
2569 up->port.membase = old_serial_port[i].iomem_base;
2570 up->port.iotype = old_serial_port[i].io_type;
2571 up->port.regshift = old_serial_port[i].iomem_reg_shift;
2572 if (share_irqs)
2573 up->port.flags |= UPF_SHARE_IRQ;
2574 }
2575}
2576
2577static void __init
2578serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2579{
2580 int i;
2581
2582 serial8250_isa_init_ports();
2583
Dave Jonesa61c2d72006-01-07 23:18:19 +00002584 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 struct uart_8250_port *up = &serial8250_ports[i];
2586
2587 up->port.dev = dev;
2588 uart_add_one_port(drv, &up->port);
2589 }
2590}
2591
2592#ifdef CONFIG_SERIAL_8250_CONSOLE
2593
Russell Kingd3587882006-03-20 20:00:09 +00002594static void serial8250_console_putchar(struct uart_port *port, int ch)
2595{
2596 struct uart_8250_port *up = (struct uart_8250_port *)port;
2597
2598 wait_for_xmitr(up, UART_LSR_THRE);
2599 serial_out(up, UART_TX, ch);
2600}
2601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602/*
2603 * Print a string to the serial port trying not to disturb
2604 * any possible real use of the port...
2605 *
2606 * The console_lock must be held when we get here.
2607 */
2608static void
2609serial8250_console_write(struct console *co, const char *s, unsigned int count)
2610{
2611 struct uart_8250_port *up = &serial8250_ports[co->index];
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002612 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 unsigned int ier;
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002614 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Andrew Morton78512ec2005-11-07 00:59:13 -08002616 touch_nmi_watchdog();
2617
Andrew Morton68aa2c02006-06-30 02:29:59 -07002618 local_irq_save(flags);
2619 if (up->port.sysrq) {
2620 /* serial8250_handle_port() already took the lock */
2621 locked = 0;
2622 } else if (oops_in_progress) {
2623 locked = spin_trylock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002624 } else
Andrew Morton68aa2c02006-06-30 02:29:59 -07002625 spin_lock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 /*
Ralf Baechledc7bf132006-02-15 09:59:47 +00002628 * First save the IER then disable the interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 */
2630 ier = serial_in(up, UART_IER);
2631
2632 if (up->capabilities & UART_CAP_UUE)
2633 serial_out(up, UART_IER, UART_IER_UUE);
2634 else
2635 serial_out(up, UART_IER, 0);
2636
Russell Kingd3587882006-03-20 20:00:09 +00002637 uart_console_write(&up->port, s, count, serial8250_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 /*
2640 * Finally, wait for transmitter to become empty
2641 * and restore the IER
2642 */
Alan Coxf91a3712006-01-21 14:59:12 +00002643 wait_for_xmitr(up, BOTH_EMPTY);
Russell Kinga88d75b2006-04-30 11:30:15 +01002644 serial_out(up, UART_IER, ier);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002645
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002646 /*
2647 * The receive handling will happen properly because the
2648 * receive ready bit will still be set; it is not cleared
2649 * on read. However, modem control will not, we must
2650 * call it if we have saved something in the saved flags
2651 * while processing with interrupts off.
2652 */
2653 if (up->msr_saved_flags)
2654 check_modem_status(up);
2655
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002656 if (locked)
Andrew Morton68aa2c02006-06-30 02:29:59 -07002657 spin_unlock(&up->port.lock);
2658 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
2660
Vivek Goyal118c0ac2007-01-11 01:52:44 +01002661static int __init serial8250_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
2663 struct uart_port *port;
2664 int baud = 9600;
2665 int bits = 8;
2666 int parity = 'n';
2667 int flow = 'n';
2668
2669 /*
2670 * Check whether an invalid uart number has been specified, and
2671 * if so, search for the first available port that does have
2672 * console support.
2673 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002674 if (co->index >= nr_uarts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 co->index = 0;
2676 port = &serial8250_ports[co->index].port;
2677 if (!port->iobase && !port->membase)
2678 return -ENODEV;
2679
2680 if (options)
2681 uart_parse_options(options, &baud, &parity, &bits, &flow);
2682
2683 return uart_set_options(port, co, baud, parity, bits, flow);
2684}
2685
Daniel Ritzb6b1d872007-08-03 16:07:43 +02002686static int serial8250_console_early_setup(void)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002687{
2688 return serial8250_find_port_for_earlycon();
2689}
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691static struct console serial8250_console = {
2692 .name = "ttyS",
2693 .write = serial8250_console_write,
2694 .device = uart_console_device,
2695 .setup = serial8250_console_setup,
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002696 .early_setup = serial8250_console_early_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 .flags = CON_PRINTBUFFER,
2698 .index = -1,
2699 .data = &serial8250_reg,
2700};
2701
2702static int __init serial8250_console_init(void)
2703{
Eric W. Biederman05d81d22008-07-12 13:47:53 -07002704 if (nr_uarts > UART_NR)
2705 nr_uarts = UART_NR;
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 serial8250_isa_init_ports();
2708 register_console(&serial8250_console);
2709 return 0;
2710}
2711console_initcall(serial8250_console_init);
2712
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002713int serial8250_find_port(struct uart_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
2715 int line;
2716 struct uart_port *port;
2717
Dave Jonesa61c2d72006-01-07 23:18:19 +00002718 for (line = 0; line < nr_uarts; line++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 port = &serial8250_ports[line].port;
Russell King50aec3b52006-01-04 18:13:03 +00002720 if (uart_match_port(p, port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 return line;
2722 }
2723 return -ENODEV;
2724}
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726#define SERIAL8250_CONSOLE &serial8250_console
2727#else
2728#define SERIAL8250_CONSOLE NULL
2729#endif
2730
2731static struct uart_driver serial8250_reg = {
2732 .owner = THIS_MODULE,
2733 .driver_name = "serial",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 .dev_name = "ttyS",
2735 .major = TTY_MAJOR,
2736 .minor = 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 .cons = SERIAL8250_CONSOLE,
2738};
2739
Russell Kingd856c662006-02-23 10:22:13 +00002740/*
2741 * early_serial_setup - early registration for 8250 ports
2742 *
2743 * Setup an 8250 port structure prior to console initialisation. Use
2744 * after console initialisation will cause undefined behaviour.
2745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746int __init early_serial_setup(struct uart_port *port)
2747{
2748 if (port->line >= ARRAY_SIZE(serial8250_ports))
2749 return -ENODEV;
2750
2751 serial8250_isa_init_ports();
2752 serial8250_ports[port->line].port = *port;
2753 serial8250_ports[port->line].port.ops = &serial8250_pops;
2754 return 0;
2755}
2756
2757/**
2758 * serial8250_suspend_port - suspend one serial port
2759 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 *
2761 * Suspend one serial port.
2762 */
2763void serial8250_suspend_port(int line)
2764{
2765 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2766}
2767
2768/**
2769 * serial8250_resume_port - resume one serial port
2770 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 *
2772 * Resume one serial port.
2773 */
2774void serial8250_resume_port(int line)
2775{
David Woodhouseb5b82df2007-05-17 14:27:39 +08002776 struct uart_8250_port *up = &serial8250_ports[line];
2777
2778 if (up->capabilities & UART_NATSEMI) {
2779 unsigned char tmp;
2780
2781 /* Ensure it's still in high speed mode */
2782 serial_outp(up, UART_LCR, 0xE0);
2783
2784 tmp = serial_in(up, 0x04); /* EXCR2 */
2785 tmp &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
2786 tmp |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
2787 serial_outp(up, 0x04, tmp);
2788
2789 serial_outp(up, UART_LCR, 0);
2790 }
2791 uart_resume_port(&serial8250_reg, &up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
2793
2794/*
2795 * Register a set of serial devices attached to a platform device. The
2796 * list is terminated with a zero flags entry, which means we expect
2797 * all entries to have at least UPF_BOOT_AUTOCONF set.
2798 */
Russell King3ae5eae2005-11-09 22:32:44 +00002799static int __devinit serial8250_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800{
Russell King3ae5eae2005-11-09 22:32:44 +00002801 struct plat_serial8250_port *p = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 struct uart_port port;
Russell Kingec9f47c2005-06-27 11:12:54 +01002803 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 memset(&port, 0, sizeof(struct uart_port));
2806
Russell Kingec9f47c2005-06-27 11:12:54 +01002807 for (i = 0; p && p->flags != 0; p++, i++) {
Will Newton74a197412008-02-04 22:27:50 -08002808 port.iobase = p->iobase;
2809 port.membase = p->membase;
2810 port.irq = p->irq;
2811 port.uartclk = p->uartclk;
2812 port.regshift = p->regshift;
2813 port.iotype = p->iotype;
2814 port.flags = p->flags;
2815 port.mapbase = p->mapbase;
2816 port.hub6 = p->hub6;
2817 port.private_data = p->private_data;
2818 port.dev = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 if (share_irqs)
2820 port.flags |= UPF_SHARE_IRQ;
Russell Kingec9f47c2005-06-27 11:12:54 +01002821 ret = serial8250_register_port(&port);
2822 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00002823 dev_err(&dev->dev, "unable to register port at index %d "
Josh Boyer4f640ef2007-07-23 18:43:44 -07002824 "(IO%lx MEM%llx IRQ%d): %d\n", i,
2825 p->iobase, (unsigned long long)p->mapbase,
2826 p->irq, ret);
Russell Kingec9f47c2005-06-27 11:12:54 +01002827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 }
2829 return 0;
2830}
2831
2832/*
2833 * Remove serial ports registered against a platform device.
2834 */
Russell King3ae5eae2005-11-09 22:32:44 +00002835static int __devexit serial8250_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
2837 int i;
2838
Dave Jonesa61c2d72006-01-07 23:18:19 +00002839 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 struct uart_8250_port *up = &serial8250_ports[i];
2841
Russell King3ae5eae2005-11-09 22:32:44 +00002842 if (up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 serial8250_unregister_port(i);
2844 }
2845 return 0;
2846}
2847
Russell King3ae5eae2005-11-09 22:32:44 +00002848static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
2850 int i;
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 for (i = 0; i < UART_NR; i++) {
2853 struct uart_8250_port *up = &serial8250_ports[i];
2854
Russell King3ae5eae2005-11-09 22:32:44 +00002855 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 uart_suspend_port(&serial8250_reg, &up->port);
2857 }
2858
2859 return 0;
2860}
2861
Russell King3ae5eae2005-11-09 22:32:44 +00002862static int serial8250_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
2864 int i;
2865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 for (i = 0; i < UART_NR; i++) {
2867 struct uart_8250_port *up = &serial8250_ports[i];
2868
Russell King3ae5eae2005-11-09 22:32:44 +00002869 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
David Woodhouseb5b82df2007-05-17 14:27:39 +08002870 serial8250_resume_port(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 }
2872
2873 return 0;
2874}
2875
Russell King3ae5eae2005-11-09 22:32:44 +00002876static struct platform_driver serial8250_isa_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 .probe = serial8250_probe,
2878 .remove = __devexit_p(serial8250_remove),
2879 .suspend = serial8250_suspend,
2880 .resume = serial8250_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002881 .driver = {
2882 .name = "serial8250",
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002883 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002884 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885};
2886
2887/*
2888 * This "device" covers _all_ ISA 8250-compatible serial devices listed
2889 * in the table in include/asm/serial.h
2890 */
2891static struct platform_device *serial8250_isa_devs;
2892
2893/*
2894 * serial8250_register_port and serial8250_unregister_port allows for
2895 * 16x50 serial ports to be configured at run-time, to support PCMCIA
2896 * modems and PCI multiport cards.
2897 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002898static DEFINE_MUTEX(serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
2900static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
2901{
2902 int i;
2903
2904 /*
2905 * First, find a port entry which matches.
2906 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002907 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (uart_match_port(&serial8250_ports[i].port, port))
2909 return &serial8250_ports[i];
2910
2911 /*
2912 * We didn't find a matching entry, so look for the first
2913 * free entry. We look for one which hasn't been previously
2914 * used (indicated by zero iobase).
2915 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002916 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
2918 serial8250_ports[i].port.iobase == 0)
2919 return &serial8250_ports[i];
2920
2921 /*
2922 * That also failed. Last resort is to find any entry which
2923 * doesn't have a real port associated with it.
2924 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002925 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
2927 return &serial8250_ports[i];
2928
2929 return NULL;
2930}
2931
2932/**
2933 * serial8250_register_port - register a serial port
2934 * @port: serial port template
2935 *
2936 * Configure the serial port specified by the request. If the
2937 * port exists and is in use, it is hung up and unregistered
2938 * first.
2939 *
2940 * The port is then probed and if necessary the IRQ is autodetected
2941 * If this fails an error is returned.
2942 *
2943 * On success the port is ready to use and the line number is returned.
2944 */
2945int serial8250_register_port(struct uart_port *port)
2946{
2947 struct uart_8250_port *uart;
2948 int ret = -ENOSPC;
2949
2950 if (port->uartclk == 0)
2951 return -EINVAL;
2952
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002953 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
2955 uart = serial8250_find_match_or_unused(port);
2956 if (uart) {
2957 uart_remove_one_port(&serial8250_reg, &uart->port);
2958
Will Newton74a197412008-02-04 22:27:50 -08002959 uart->port.iobase = port->iobase;
2960 uart->port.membase = port->membase;
2961 uart->port.irq = port->irq;
2962 uart->port.uartclk = port->uartclk;
2963 uart->port.fifosize = port->fifosize;
2964 uart->port.regshift = port->regshift;
2965 uart->port.iotype = port->iotype;
2966 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
2967 uart->port.mapbase = port->mapbase;
2968 uart->port.private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 if (port->dev)
2970 uart->port.dev = port->dev;
2971
2972 ret = uart_add_one_port(&serial8250_reg, &uart->port);
2973 if (ret == 0)
2974 ret = uart->port.line;
2975 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002976 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
2978 return ret;
2979}
2980EXPORT_SYMBOL(serial8250_register_port);
2981
2982/**
2983 * serial8250_unregister_port - remove a 16x50 serial port at runtime
2984 * @line: serial line number
2985 *
2986 * Remove one serial port. This may not be called from interrupt
2987 * context. We hand the port back to the our control.
2988 */
2989void serial8250_unregister_port(int line)
2990{
2991 struct uart_8250_port *uart = &serial8250_ports[line];
2992
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002993 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 uart_remove_one_port(&serial8250_reg, &uart->port);
2995 if (serial8250_isa_devs) {
2996 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
2997 uart->port.type = PORT_UNKNOWN;
2998 uart->port.dev = &serial8250_isa_devs->dev;
2999 uart_add_one_port(&serial8250_reg, &uart->port);
3000 } else {
3001 uart->port.dev = NULL;
3002 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003003 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004}
3005EXPORT_SYMBOL(serial8250_unregister_port);
3006
3007static int __init serial8250_init(void)
3008{
Alan Cox25db8ad2008-08-19 20:49:40 -07003009 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Dave Jonesa61c2d72006-01-07 23:18:19 +00003011 if (nr_uarts > UART_NR)
3012 nr_uarts = UART_NR;
3013
Adrian Bunkd87a6d92008-07-16 21:53:31 +01003014 printk(KERN_INFO "Serial: 8250/16550 driver"
Dave Jonesa61c2d72006-01-07 23:18:19 +00003015 "%d ports, IRQ sharing %sabled\n", nr_uarts,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 share_irqs ? "en" : "dis");
3017
David Millerb70ac772008-10-13 10:36:31 +01003018#ifdef CONFIG_SPARC
3019 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
3020#else
3021 serial8250_reg.nr = UART_NR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 ret = uart_register_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003023#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 if (ret)
3025 goto out;
3026
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003027 serial8250_isa_devs = platform_device_alloc("serial8250",
3028 PLAT8250_DEV_LEGACY);
3029 if (!serial8250_isa_devs) {
3030 ret = -ENOMEM;
Russell Kingbc965a72006-01-18 09:54:29 +00003031 goto unreg_uart_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 }
3033
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003034 ret = platform_device_add(serial8250_isa_devs);
3035 if (ret)
3036 goto put_dev;
3037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
3039
Russell Kingbc965a72006-01-18 09:54:29 +00003040 ret = platform_driver_register(&serial8250_isa_driver);
3041 if (ret == 0)
3042 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Russell Kingbc965a72006-01-18 09:54:29 +00003044 platform_device_del(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003045put_dev:
Dmitry Torokhov7493a312006-01-13 22:06:43 +00003046 platform_device_put(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07003047unreg_uart_drv:
David Millerb70ac772008-10-13 10:36:31 +01003048#ifdef CONFIG_SPARC
3049 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3050#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003052#endif
Alan Cox25db8ad2008-08-19 20:49:40 -07003053out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 return ret;
3055}
3056
3057static void __exit serial8250_exit(void)
3058{
3059 struct platform_device *isa_dev = serial8250_isa_devs;
3060
3061 /*
3062 * This tells serial8250_unregister_port() not to re-register
3063 * the ports (thereby making serial8250_isa_driver permanently
3064 * in use.)
3065 */
3066 serial8250_isa_devs = NULL;
3067
Russell King3ae5eae2005-11-09 22:32:44 +00003068 platform_driver_unregister(&serial8250_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 platform_device_unregister(isa_dev);
3070
David Millerb70ac772008-10-13 10:36:31 +01003071#ifdef CONFIG_SPARC
3072 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3073#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003075#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
3078module_init(serial8250_init);
3079module_exit(serial8250_exit);
3080
3081EXPORT_SYMBOL(serial8250_suspend_port);
3082EXPORT_SYMBOL(serial8250_resume_port);
3083
3084MODULE_LICENSE("GPL");
Adrian Bunkd87a6d92008-07-16 21:53:31 +01003085MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
3087module_param(share_irqs, uint, 0644);
3088MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
3089 " (unsafe)");
3090
Dave Jonesa61c2d72006-01-07 23:18:19 +00003091module_param(nr_uarts, uint, 0644);
3092MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
3093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094#ifdef CONFIG_SERIAL_8250_RSA
3095module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
3096MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
3097#endif
3098MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);