blob: a07442ffefbdff05080fbec613069bffcf4f5e49 [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 *
15 * $Id: 8250.c,v 1.90 2002/07/28 10:03:27 rmk Exp $
16 *
17 * A note about mapbase / membase
18 *
19 * mapbase is the physical address of the IO port.
20 * membase is an 'ioremapped' cookie.
21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
24#define SUPPORT_SYSRQ
25#endif
26
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/ioport.h>
30#include <linux/init.h>
31#include <linux/console.h>
32#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010034#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/tty.h>
36#include <linux/tty_flip.h>
37#include <linux/serial_reg.h>
38#include <linux/serial_core.h>
39#include <linux/serial.h>
40#include <linux/serial_8250.h>
Andrew Morton78512ec2005-11-07 00:59:13 -080041#include <linux/nmi.h>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000042#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <asm/io.h>
45#include <asm/irq.h>
46
47#include "8250.h"
48
49/*
50 * Configuration:
Thomas Gleixner40663cc2006-07-01 19:29:43 -070051 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * is unsafe when used on edge-triggered interrupts.
53 */
Adrian Bunk408b6642005-05-01 08:59:29 -070054static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Dave Jonesa61c2d72006-01-07 23:18:19 +000056static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
59 * Debugging.
60 */
61#if 0
62#define DEBUG_AUTOCONF(fmt...) printk(fmt)
63#else
64#define DEBUG_AUTOCONF(fmt...) do { } while (0)
65#endif
66
67#if 0
68#define DEBUG_INTR(fmt...) printk(fmt)
69#else
70#define DEBUG_INTR(fmt...) do { } while (0)
71#endif
72
73#define PASS_LIMIT 256
74
75/*
76 * We default to IRQ0 for the "no irq" hack. Some
77 * machine types want others as well - they're free
78 * to redefine this in their header file.
79 */
80#define is_real_interrupt(irq) ((irq) != 0)
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
83#define CONFIG_SERIAL_DETECT_IRQ 1
84#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#ifdef CONFIG_SERIAL_8250_MANY_PORTS
86#define CONFIG_SERIAL_MANY_PORTS 1
87#endif
88
89/*
90 * HUB6 is always on. This will be removed once the header
91 * files have been cleaned.
92 */
93#define CONFIG_HUB6 1
94
95#include <asm/serial.h>
96
97/*
98 * SERIAL_PORT_DFNS tells us about built-in ports that have no
99 * standard enumeration mechanism. Platforms that can find all
100 * serial ports via mechanisms like ACPI or PCI need not supply it.
101 */
102#ifndef SERIAL_PORT_DFNS
103#define SERIAL_PORT_DFNS
104#endif
105
Arjan van de Vencb3592b2005-11-28 21:04:11 +0000106static const struct old_serial_port old_serial_port[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 SERIAL_PORT_DFNS /* defined in asm/serial.h */
108};
109
Russell King026d02a2005-06-29 18:45:19 +0100110#define UART_NR CONFIG_SERIAL_8250_NR_UARTS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112#ifdef CONFIG_SERIAL_8250_RSA
113
114#define PORT_RSA_MAX 4
115static unsigned long probe_rsa[PORT_RSA_MAX];
116static unsigned int probe_rsa_count;
117#endif /* CONFIG_SERIAL_8250_RSA */
118
119struct uart_8250_port {
120 struct uart_port port;
121 struct timer_list timer; /* "no irq" timer */
122 struct list_head list; /* ports on this IRQ */
Russell King4ba5e352005-06-23 10:43:04 +0100123 unsigned short capabilities; /* port capabilities */
124 unsigned short bugs; /* port bugs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned int tx_loadsz; /* transmit fifo load size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned char acr;
127 unsigned char ier;
128 unsigned char lcr;
129 unsigned char mcr;
130 unsigned char mcr_mask; /* mask of user bits */
131 unsigned char mcr_force; /* mask of forced bits */
132 unsigned char lsr_break_flag;
133
134 /*
135 * We provide a per-port pm hook.
136 */
137 void (*pm)(struct uart_port *port,
138 unsigned int state, unsigned int old);
139};
140
141struct irq_info {
142 spinlock_t lock;
143 struct list_head *head;
144};
145
146static struct irq_info irq_lists[NR_IRQS];
147
148/*
149 * Here we define the default xmit fifo size used for each type of UART.
150 */
151static const struct serial8250_config uart_config[] = {
152 [PORT_UNKNOWN] = {
153 .name = "unknown",
154 .fifo_size = 1,
155 .tx_loadsz = 1,
156 },
157 [PORT_8250] = {
158 .name = "8250",
159 .fifo_size = 1,
160 .tx_loadsz = 1,
161 },
162 [PORT_16450] = {
163 .name = "16450",
164 .fifo_size = 1,
165 .tx_loadsz = 1,
166 },
167 [PORT_16550] = {
168 .name = "16550",
169 .fifo_size = 1,
170 .tx_loadsz = 1,
171 },
172 [PORT_16550A] = {
173 .name = "16550A",
174 .fifo_size = 16,
175 .tx_loadsz = 16,
176 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
177 .flags = UART_CAP_FIFO,
178 },
179 [PORT_CIRRUS] = {
180 .name = "Cirrus",
181 .fifo_size = 1,
182 .tx_loadsz = 1,
183 },
184 [PORT_16650] = {
185 .name = "ST16650",
186 .fifo_size = 1,
187 .tx_loadsz = 1,
188 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
189 },
190 [PORT_16650V2] = {
191 .name = "ST16650V2",
192 .fifo_size = 32,
193 .tx_loadsz = 16,
194 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
195 UART_FCR_T_TRIG_00,
196 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
197 },
198 [PORT_16750] = {
199 .name = "TI16750",
200 .fifo_size = 64,
201 .tx_loadsz = 64,
202 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
203 UART_FCR7_64BYTE,
204 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
205 },
206 [PORT_STARTECH] = {
207 .name = "Startech",
208 .fifo_size = 1,
209 .tx_loadsz = 1,
210 },
211 [PORT_16C950] = {
212 .name = "16C950/954",
213 .fifo_size = 128,
214 .tx_loadsz = 128,
215 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
216 .flags = UART_CAP_FIFO,
217 },
218 [PORT_16654] = {
219 .name = "ST16654",
220 .fifo_size = 64,
221 .tx_loadsz = 32,
222 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
223 UART_FCR_T_TRIG_10,
224 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
225 },
226 [PORT_16850] = {
227 .name = "XR16850",
228 .fifo_size = 128,
229 .tx_loadsz = 128,
230 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
231 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
232 },
233 [PORT_RSA] = {
234 .name = "RSA",
235 .fifo_size = 2048,
236 .tx_loadsz = 2048,
237 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
238 .flags = UART_CAP_FIFO,
239 },
240 [PORT_NS16550A] = {
241 .name = "NS16550A",
242 .fifo_size = 16,
243 .tx_loadsz = 16,
244 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
245 .flags = UART_CAP_FIFO | UART_NATSEMI,
246 },
247 [PORT_XSCALE] = {
248 .name = "XScale",
249 .fifo_size = 32,
250 .tx_loadsz = 32,
251 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
252 .flags = UART_CAP_FIFO | UART_CAP_UUE,
253 },
254};
255
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000256#ifdef CONFIG_SERIAL_8250_AU1X00
257
258/* Au1x00 UART hardware has a weird register layout */
259static const u8 au_io_in_map[] = {
260 [UART_RX] = 0,
261 [UART_IER] = 2,
262 [UART_IIR] = 3,
263 [UART_LCR] = 5,
264 [UART_MCR] = 6,
265 [UART_LSR] = 7,
266 [UART_MSR] = 8,
267};
268
269static const u8 au_io_out_map[] = {
270 [UART_TX] = 1,
271 [UART_IER] = 2,
272 [UART_FCR] = 4,
273 [UART_LCR] = 5,
274 [UART_MCR] = 6,
275};
276
277/* sane hardware needs no mapping */
278static inline int map_8250_in_reg(struct uart_8250_port *up, int offset)
279{
280 if (up->port.iotype != UPIO_AU)
281 return offset;
282 return au_io_in_map[offset];
283}
284
285static inline int map_8250_out_reg(struct uart_8250_port *up, int offset)
286{
287 if (up->port.iotype != UPIO_AU)
288 return offset;
289 return au_io_out_map[offset];
290}
291
292#else
293
294/* sane hardware needs no mapping */
295#define map_8250_in_reg(up, offset) (offset)
296#define map_8250_out_reg(up, offset) (offset)
297
298#endif
299
Russell Kingea8874d2006-01-04 19:43:24 +0000300static unsigned int serial_in(struct uart_8250_port *up, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Zang Roy-r619113be91ec2006-06-30 02:29:58 -0700302 unsigned int tmp;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000303 offset = map_8250_in_reg(up, offset) << up->port.regshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 switch (up->port.iotype) {
306 case UPIO_HUB6:
307 outb(up->port.hub6 - 1 + offset, up->port.iobase);
308 return inb(up->port.iobase + 1);
309
310 case UPIO_MEM:
311 return readb(up->port.membase + offset);
312
313 case UPIO_MEM32:
314 return readl(up->port.membase + offset);
315
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000316#ifdef CONFIG_SERIAL_8250_AU1X00
317 case UPIO_AU:
318 return __raw_readl(up->port.membase + offset);
319#endif
320
Zang Roy-r619113be91ec2006-06-30 02:29:58 -0700321 case UPIO_TSI:
322 if (offset == UART_IIR) {
323 tmp = readl((u32 *)(up->port.membase + UART_RX));
324 return (cpu_to_le32(tmp) >> 8) & 0xff;
325 } else
326 return readb(up->port.membase + offset);
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 default:
329 return inb(up->port.iobase + offset);
330 }
331}
332
Russell Kingea8874d2006-01-04 19:43:24 +0000333static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334serial_out(struct uart_8250_port *up, int offset, int value)
335{
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000336 offset = map_8250_out_reg(up, offset) << up->port.regshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 switch (up->port.iotype) {
339 case UPIO_HUB6:
340 outb(up->port.hub6 - 1 + offset, up->port.iobase);
341 outb(value, up->port.iobase + 1);
342 break;
343
344 case UPIO_MEM:
345 writeb(value, up->port.membase + offset);
346 break;
347
348 case UPIO_MEM32:
349 writel(value, up->port.membase + offset);
350 break;
351
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000352#ifdef CONFIG_SERIAL_8250_AU1X00
353 case UPIO_AU:
354 __raw_writel(value, up->port.membase + offset);
355 break;
356#endif
Zang Roy-r619113be91ec2006-06-30 02:29:58 -0700357 case UPIO_TSI:
358 if (!((offset == UART_IER) && (value & UART_IER_UUE)))
359 writeb(value, up->port.membase + offset);
360 break;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 default:
363 outb(value, up->port.iobase + offset);
364 }
365}
366
367/*
368 * We used to support using pause I/O for certain machines. We
369 * haven't supported this for a while, but just in case it's badly
370 * needed for certain old 386 machines, I've left these #define's
371 * in....
372 */
373#define serial_inp(up, offset) serial_in(up, offset)
374#define serial_outp(up, offset, value) serial_out(up, offset, value)
375
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100376/* Uart divisor latch read */
377static inline int _serial_dl_read(struct uart_8250_port *up)
378{
379 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
380}
381
382/* Uart divisor latch write */
383static inline void _serial_dl_write(struct uart_8250_port *up, int value)
384{
385 serial_outp(up, UART_DLL, value & 0xff);
386 serial_outp(up, UART_DLM, value >> 8 & 0xff);
387}
388
389#ifdef CONFIG_SERIAL_8250_AU1X00
390/* Au1x00 haven't got a standard divisor latch */
391static int serial_dl_read(struct uart_8250_port *up)
392{
393 if (up->port.iotype == UPIO_AU)
394 return __raw_readl(up->port.membase + 0x28);
395 else
396 return _serial_dl_read(up);
397}
398
399static void serial_dl_write(struct uart_8250_port *up, int value)
400{
401 if (up->port.iotype == UPIO_AU)
402 __raw_writel(value, up->port.membase + 0x28);
403 else
404 _serial_dl_write(up, value);
405}
406#else
407#define serial_dl_read(up) _serial_dl_read(up)
408#define serial_dl_write(up, value) _serial_dl_write(up, value)
409#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411/*
412 * For the 16C950
413 */
414static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
415{
416 serial_out(up, UART_SCR, offset);
417 serial_out(up, UART_ICR, value);
418}
419
420static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
421{
422 unsigned int value;
423
424 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
425 serial_out(up, UART_SCR, offset);
426 value = serial_in(up, UART_ICR);
427 serial_icr_write(up, UART_ACR, up->acr);
428
429 return value;
430}
431
432/*
433 * FIFO support.
434 */
435static inline void serial8250_clear_fifos(struct uart_8250_port *p)
436{
437 if (p->capabilities & UART_CAP_FIFO) {
438 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
439 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
440 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
441 serial_outp(p, UART_FCR, 0);
442 }
443}
444
445/*
446 * IER sleep support. UARTs which have EFRs need the "extended
447 * capability" bit enabled. Note that on XR16C850s, we need to
448 * reset LCR to write to IER.
449 */
450static inline void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
451{
452 if (p->capabilities & UART_CAP_SLEEP) {
453 if (p->capabilities & UART_CAP_EFR) {
454 serial_outp(p, UART_LCR, 0xBF);
455 serial_outp(p, UART_EFR, UART_EFR_ECB);
456 serial_outp(p, UART_LCR, 0);
457 }
458 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
459 if (p->capabilities & UART_CAP_EFR) {
460 serial_outp(p, UART_LCR, 0xBF);
461 serial_outp(p, UART_EFR, 0);
462 serial_outp(p, UART_LCR, 0);
463 }
464 }
465}
466
467#ifdef CONFIG_SERIAL_8250_RSA
468/*
469 * Attempts to turn on the RSA FIFO. Returns zero on failure.
470 * We set the port uart clock rate if we succeed.
471 */
472static int __enable_rsa(struct uart_8250_port *up)
473{
474 unsigned char mode;
475 int result;
476
477 mode = serial_inp(up, UART_RSA_MSR);
478 result = mode & UART_RSA_MSR_FIFO;
479
480 if (!result) {
481 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
482 mode = serial_inp(up, UART_RSA_MSR);
483 result = mode & UART_RSA_MSR_FIFO;
484 }
485
486 if (result)
487 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
488
489 return result;
490}
491
492static void enable_rsa(struct uart_8250_port *up)
493{
494 if (up->port.type == PORT_RSA) {
495 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
496 spin_lock_irq(&up->port.lock);
497 __enable_rsa(up);
498 spin_unlock_irq(&up->port.lock);
499 }
500 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
501 serial_outp(up, UART_RSA_FRR, 0);
502 }
503}
504
505/*
506 * Attempts to turn off the RSA FIFO. Returns zero on failure.
507 * It is unknown why interrupts were disabled in here. However,
508 * the caller is expected to preserve this behaviour by grabbing
509 * the spinlock before calling this function.
510 */
511static void disable_rsa(struct uart_8250_port *up)
512{
513 unsigned char mode;
514 int result;
515
516 if (up->port.type == PORT_RSA &&
517 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
518 spin_lock_irq(&up->port.lock);
519
520 mode = serial_inp(up, UART_RSA_MSR);
521 result = !(mode & UART_RSA_MSR_FIFO);
522
523 if (!result) {
524 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
525 mode = serial_inp(up, UART_RSA_MSR);
526 result = !(mode & UART_RSA_MSR_FIFO);
527 }
528
529 if (result)
530 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
531 spin_unlock_irq(&up->port.lock);
532 }
533}
534#endif /* CONFIG_SERIAL_8250_RSA */
535
536/*
537 * This is a quickie test to see how big the FIFO is.
538 * It doesn't work at all the time, more's the pity.
539 */
540static int size_fifo(struct uart_8250_port *up)
541{
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100542 unsigned char old_fcr, old_mcr, old_lcr;
543 unsigned short old_dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 int count;
545
546 old_lcr = serial_inp(up, UART_LCR);
547 serial_outp(up, UART_LCR, 0);
548 old_fcr = serial_inp(up, UART_FCR);
549 old_mcr = serial_inp(up, UART_MCR);
550 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
551 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
552 serial_outp(up, UART_MCR, UART_MCR_LOOP);
553 serial_outp(up, UART_LCR, UART_LCR_DLAB);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100554 old_dl = serial_dl_read(up);
555 serial_dl_write(up, 0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 serial_outp(up, UART_LCR, 0x03);
557 for (count = 0; count < 256; count++)
558 serial_outp(up, UART_TX, count);
559 mdelay(20);/* FIXME - schedule_timeout */
560 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
561 (count < 256); count++)
562 serial_inp(up, UART_RX);
563 serial_outp(up, UART_FCR, old_fcr);
564 serial_outp(up, UART_MCR, old_mcr);
565 serial_outp(up, UART_LCR, UART_LCR_DLAB);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100566 serial_dl_write(up, old_dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 serial_outp(up, UART_LCR, old_lcr);
568
569 return count;
570}
571
572/*
573 * Read UART ID using the divisor method - set DLL and DLM to zero
574 * and the revision will be in DLL and device type in DLM. We
575 * preserve the device state across this.
576 */
577static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
578{
579 unsigned char old_dll, old_dlm, old_lcr;
580 unsigned int id;
581
582 old_lcr = serial_inp(p, UART_LCR);
583 serial_outp(p, UART_LCR, UART_LCR_DLAB);
584
585 old_dll = serial_inp(p, UART_DLL);
586 old_dlm = serial_inp(p, UART_DLM);
587
588 serial_outp(p, UART_DLL, 0);
589 serial_outp(p, UART_DLM, 0);
590
591 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
592
593 serial_outp(p, UART_DLL, old_dll);
594 serial_outp(p, UART_DLM, old_dlm);
595 serial_outp(p, UART_LCR, old_lcr);
596
597 return id;
598}
599
600/*
601 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
602 * When this function is called we know it is at least a StarTech
603 * 16650 V2, but it might be one of several StarTech UARTs, or one of
604 * its clones. (We treat the broken original StarTech 16650 V1 as a
605 * 16550, and why not? Startech doesn't seem to even acknowledge its
606 * existence.)
607 *
608 * What evil have men's minds wrought...
609 */
610static void autoconfig_has_efr(struct uart_8250_port *up)
611{
612 unsigned int id1, id2, id3, rev;
613
614 /*
615 * Everything with an EFR has SLEEP
616 */
617 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
618
619 /*
620 * First we check to see if it's an Oxford Semiconductor UART.
621 *
622 * If we have to do this here because some non-National
623 * Semiconductor clone chips lock up if you try writing to the
624 * LSR register (which serial_icr_read does)
625 */
626
627 /*
628 * Check for Oxford Semiconductor 16C950.
629 *
630 * EFR [4] must be set else this test fails.
631 *
632 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
633 * claims that it's needed for 952 dual UART's (which are not
634 * recommended for new designs).
635 */
636 up->acr = 0;
637 serial_out(up, UART_LCR, 0xBF);
638 serial_out(up, UART_EFR, UART_EFR_ECB);
639 serial_out(up, UART_LCR, 0x00);
640 id1 = serial_icr_read(up, UART_ID1);
641 id2 = serial_icr_read(up, UART_ID2);
642 id3 = serial_icr_read(up, UART_ID3);
643 rev = serial_icr_read(up, UART_REV);
644
645 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
646
647 if (id1 == 0x16 && id2 == 0xC9 &&
648 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
649 up->port.type = PORT_16C950;
Russell King4ba5e352005-06-23 10:43:04 +0100650
651 /*
652 * Enable work around for the Oxford Semiconductor 952 rev B
653 * chip which causes it to seriously miscalculate baud rates
654 * when DLL is 0.
655 */
656 if (id3 == 0x52 && rev == 0x01)
657 up->bugs |= UART_BUG_QUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return;
659 }
660
661 /*
662 * We check for a XR16C850 by setting DLL and DLM to 0, and then
663 * reading back DLL and DLM. The chip type depends on the DLM
664 * value read back:
665 * 0x10 - XR16C850 and the DLL contains the chip revision.
666 * 0x12 - XR16C2850.
667 * 0x14 - XR16C854.
668 */
669 id1 = autoconfig_read_divisor_id(up);
670 DEBUG_AUTOCONF("850id=%04x ", id1);
671
672 id2 = id1 >> 8;
673 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 up->port.type = PORT_16850;
675 return;
676 }
677
678 /*
679 * It wasn't an XR16C850.
680 *
681 * We distinguish between the '654 and the '650 by counting
682 * how many bytes are in the FIFO. I'm using this for now,
683 * since that's the technique that was sent to me in the
684 * serial driver update, but I'm not convinced this works.
685 * I've had problems doing this in the past. -TYT
686 */
687 if (size_fifo(up) == 64)
688 up->port.type = PORT_16654;
689 else
690 up->port.type = PORT_16650V2;
691}
692
693/*
694 * We detected a chip without a FIFO. Only two fall into
695 * this category - the original 8250 and the 16450. The
696 * 16450 has a scratch register (accessible with LCR=0)
697 */
698static void autoconfig_8250(struct uart_8250_port *up)
699{
700 unsigned char scratch, status1, status2;
701
702 up->port.type = PORT_8250;
703
704 scratch = serial_in(up, UART_SCR);
705 serial_outp(up, UART_SCR, 0xa5);
706 status1 = serial_in(up, UART_SCR);
707 serial_outp(up, UART_SCR, 0x5a);
708 status2 = serial_in(up, UART_SCR);
709 serial_outp(up, UART_SCR, scratch);
710
711 if (status1 == 0xa5 && status2 == 0x5a)
712 up->port.type = PORT_16450;
713}
714
715static int broken_efr(struct uart_8250_port *up)
716{
717 /*
718 * Exar ST16C2550 "A2" devices incorrectly detect as
719 * having an EFR, and report an ID of 0x0201. See
720 * http://www.exar.com/info.php?pdf=dan180_oct2004.pdf
721 */
722 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
723 return 1;
724
725 return 0;
726}
727
728/*
729 * We know that the chip has FIFOs. Does it have an EFR? The
730 * EFR is located in the same register position as the IIR and
731 * we know the top two bits of the IIR are currently set. The
732 * EFR should contain zero. Try to read the EFR.
733 */
734static void autoconfig_16550a(struct uart_8250_port *up)
735{
736 unsigned char status1, status2;
737 unsigned int iersave;
738
739 up->port.type = PORT_16550A;
740 up->capabilities |= UART_CAP_FIFO;
741
742 /*
743 * Check for presence of the EFR when DLAB is set.
744 * Only ST16C650V1 UARTs pass this test.
745 */
746 serial_outp(up, UART_LCR, UART_LCR_DLAB);
747 if (serial_in(up, UART_EFR) == 0) {
748 serial_outp(up, UART_EFR, 0xA8);
749 if (serial_in(up, UART_EFR) != 0) {
750 DEBUG_AUTOCONF("EFRv1 ");
751 up->port.type = PORT_16650;
752 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
753 } else {
754 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
755 }
756 serial_outp(up, UART_EFR, 0);
757 return;
758 }
759
760 /*
761 * Maybe it requires 0xbf to be written to the LCR.
762 * (other ST16C650V2 UARTs, TI16C752A, etc)
763 */
764 serial_outp(up, UART_LCR, 0xBF);
765 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
766 DEBUG_AUTOCONF("EFRv2 ");
767 autoconfig_has_efr(up);
768 return;
769 }
770
771 /*
772 * Check for a National Semiconductor SuperIO chip.
773 * Attempt to switch to bank 2, read the value of the LOOP bit
774 * from EXCR1. Switch back to bank 0, change it in MCR. Then
775 * switch back to bank 2, read it from EXCR1 again and check
776 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
778 serial_outp(up, UART_LCR, 0);
779 status1 = serial_in(up, UART_MCR);
780 serial_outp(up, UART_LCR, 0xE0);
781 status2 = serial_in(up, 0x02); /* EXCR1 */
782
783 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
784 serial_outp(up, UART_LCR, 0);
785 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
786 serial_outp(up, UART_LCR, 0xE0);
787 status2 = serial_in(up, 0x02); /* EXCR1 */
788 serial_outp(up, UART_LCR, 0);
789 serial_outp(up, UART_MCR, status1);
790
791 if ((status2 ^ status1) & UART_MCR_LOOP) {
David Woodhouse857dde22005-05-21 15:52:23 +0100792 unsigned short quot;
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 serial_outp(up, UART_LCR, 0xE0);
David Woodhouse857dde22005-05-21 15:52:23 +0100795
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100796 quot = serial_dl_read(up);
David Woodhouse857dde22005-05-21 15:52:23 +0100797 quot <<= 3;
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 status1 = serial_in(up, 0x04); /* EXCR1 */
800 status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
801 status1 |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
802 serial_outp(up, 0x04, status1);
David Woodhouse857dde22005-05-21 15:52:23 +0100803
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100804 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
David Woodhouse857dde22005-05-21 15:52:23 +0100806 serial_outp(up, UART_LCR, 0);
807
808 up->port.uartclk = 921600*16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 up->port.type = PORT_NS16550A;
810 up->capabilities |= UART_NATSEMI;
811 return;
812 }
813 }
814
815 /*
816 * No EFR. Try to detect a TI16750, which only sets bit 5 of
817 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
818 * Try setting it with and without DLAB set. Cheap clones
819 * set bit 5 without DLAB set.
820 */
821 serial_outp(up, UART_LCR, 0);
822 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
823 status1 = serial_in(up, UART_IIR) >> 5;
824 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
825 serial_outp(up, UART_LCR, UART_LCR_DLAB);
826 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
827 status2 = serial_in(up, UART_IIR) >> 5;
828 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
829 serial_outp(up, UART_LCR, 0);
830
831 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
832
833 if (status1 == 6 && status2 == 7) {
834 up->port.type = PORT_16750;
835 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
836 return;
837 }
838
839 /*
840 * Try writing and reading the UART_IER_UUE bit (b6).
841 * If it works, this is probably one of the Xscale platform's
842 * internal UARTs.
843 * We're going to explicitly set the UUE bit to 0 before
844 * trying to write and read a 1 just to make sure it's not
845 * already a 1 and maybe locked there before we even start start.
846 */
847 iersave = serial_in(up, UART_IER);
848 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
849 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
850 /*
851 * OK it's in a known zero state, try writing and reading
852 * without disturbing the current state of the other bits.
853 */
854 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
855 if (serial_in(up, UART_IER) & UART_IER_UUE) {
856 /*
857 * It's an Xscale.
858 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
859 */
860 DEBUG_AUTOCONF("Xscale ");
861 up->port.type = PORT_XSCALE;
862 up->capabilities |= UART_CAP_UUE;
863 return;
864 }
865 } else {
866 /*
867 * If we got here we couldn't force the IER_UUE bit to 0.
868 * Log it and continue.
869 */
870 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
871 }
872 serial_outp(up, UART_IER, iersave);
873}
874
875/*
876 * This routine is called by rs_init() to initialize a specific serial
877 * port. It determines what type of UART chip this serial port is
878 * using: 8250, 16450, 16550, 16550A. The important question is
879 * whether or not this UART is a 16550A or not, since this will
880 * determine whether or not we can use its FIFO features or not.
881 */
882static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
883{
884 unsigned char status1, scratch, scratch2, scratch3;
885 unsigned char save_lcr, save_mcr;
886 unsigned long flags;
887
888 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
889 return;
890
891 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%p): ",
892 up->port.line, up->port.iobase, up->port.membase);
893
894 /*
895 * We really do need global IRQs disabled here - we're going to
896 * be frobbing the chips IRQ enable register to see if it exists.
897 */
898 spin_lock_irqsave(&up->port.lock, flags);
899// save_flags(flags); cli();
900
901 up->capabilities = 0;
Russell King4ba5e352005-06-23 10:43:04 +0100902 up->bugs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 if (!(up->port.flags & UPF_BUGGY_UART)) {
905 /*
906 * Do a simple existence test first; if we fail this,
907 * there's no point trying anything else.
908 *
909 * 0x80 is used as a nonsense port to prevent against
910 * false positives due to ISA bus float. The
911 * assumption is that 0x80 is a non-existent port;
912 * which should be safe since include/asm/io.h also
913 * makes this assumption.
914 *
915 * Note: this is safe as long as MCR bit 4 is clear
916 * and the device is in "PC" mode.
917 */
918 scratch = serial_inp(up, UART_IER);
919 serial_outp(up, UART_IER, 0);
920#ifdef __i386__
921 outb(0xff, 0x080);
922#endif
923 scratch2 = serial_inp(up, UART_IER);
924 serial_outp(up, UART_IER, 0x0F);
925#ifdef __i386__
926 outb(0, 0x080);
927#endif
928 scratch3 = serial_inp(up, UART_IER);
929 serial_outp(up, UART_IER, scratch);
930 if (scratch2 != 0 || scratch3 != 0x0F) {
931 /*
932 * We failed; there's nothing here
933 */
934 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
935 scratch2, scratch3);
936 goto out;
937 }
938 }
939
940 save_mcr = serial_in(up, UART_MCR);
941 save_lcr = serial_in(up, UART_LCR);
942
943 /*
944 * Check to see if a UART is really there. Certain broken
945 * internal modems based on the Rockwell chipset fail this
946 * test, because they apparently don't implement the loopback
947 * test mode. So this test is skipped on the COM 1 through
948 * COM 4 ports. This *should* be safe, since no board
949 * manufacturer would be stupid enough to design a board
950 * that conflicts with COM 1-4 --- we hope!
951 */
952 if (!(up->port.flags & UPF_SKIP_TEST)) {
953 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
954 status1 = serial_inp(up, UART_MSR) & 0xF0;
955 serial_outp(up, UART_MCR, save_mcr);
956 if (status1 != 0x90) {
957 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
958 status1);
959 goto out;
960 }
961 }
962
963 /*
964 * We're pretty sure there's a port here. Lets find out what
965 * type of port it is. The IIR top two bits allows us to find
Russell King6f0d6182005-09-09 16:17:58 +0100966 * out if it's 8250 or 16450, 16550, 16550A or later. This
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * determines what we test for next.
968 *
969 * We also initialise the EFR (if any) to zero for later. The
970 * EFR occupies the same register location as the FCR and IIR.
971 */
972 serial_outp(up, UART_LCR, 0xBF);
973 serial_outp(up, UART_EFR, 0);
974 serial_outp(up, UART_LCR, 0);
975
976 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
977 scratch = serial_in(up, UART_IIR) >> 6;
978
979 DEBUG_AUTOCONF("iir=%d ", scratch);
980
981 switch (scratch) {
982 case 0:
983 autoconfig_8250(up);
984 break;
985 case 1:
986 up->port.type = PORT_UNKNOWN;
987 break;
988 case 2:
989 up->port.type = PORT_16550;
990 break;
991 case 3:
992 autoconfig_16550a(up);
993 break;
994 }
995
996#ifdef CONFIG_SERIAL_8250_RSA
997 /*
998 * Only probe for RSA ports if we got the region.
999 */
1000 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1001 int i;
1002
1003 for (i = 0 ; i < probe_rsa_count; ++i) {
1004 if (probe_rsa[i] == up->port.iobase &&
1005 __enable_rsa(up)) {
1006 up->port.type = PORT_RSA;
1007 break;
1008 }
1009 }
1010 }
1011#endif
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001012
1013#ifdef CONFIG_SERIAL_8250_AU1X00
1014 /* if access method is AU, it is a 16550 with a quirk */
1015 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
1016 up->bugs |= UART_BUG_NOMSR;
1017#endif
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 serial_outp(up, UART_LCR, save_lcr);
1020
1021 if (up->capabilities != uart_config[up->port.type].flags) {
1022 printk(KERN_WARNING
1023 "ttyS%d: detected caps %08x should be %08x\n",
1024 up->port.line, up->capabilities,
1025 uart_config[up->port.type].flags);
1026 }
1027
1028 up->port.fifosize = uart_config[up->port.type].fifo_size;
1029 up->capabilities = uart_config[up->port.type].flags;
1030 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1031
1032 if (up->port.type == PORT_UNKNOWN)
1033 goto out;
1034
1035 /*
1036 * Reset the UART.
1037 */
1038#ifdef CONFIG_SERIAL_8250_RSA
1039 if (up->port.type == PORT_RSA)
1040 serial_outp(up, UART_RSA_FRR, 0);
1041#endif
1042 serial_outp(up, UART_MCR, save_mcr);
1043 serial8250_clear_fifos(up);
1044 (void)serial_in(up, UART_RX);
Lennert Buytenhek5c8c7552005-11-12 21:58:05 +00001045 if (up->capabilities & UART_CAP_UUE)
1046 serial_outp(up, UART_IER, UART_IER_UUE);
1047 else
1048 serial_outp(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 out:
1051 spin_unlock_irqrestore(&up->port.lock, flags);
1052// restore_flags(flags);
1053 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1054}
1055
1056static void autoconfig_irq(struct uart_8250_port *up)
1057{
1058 unsigned char save_mcr, save_ier;
1059 unsigned char save_ICP = 0;
1060 unsigned int ICP = 0;
1061 unsigned long irqs;
1062 int irq;
1063
1064 if (up->port.flags & UPF_FOURPORT) {
1065 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1066 save_ICP = inb_p(ICP);
1067 outb_p(0x80, ICP);
1068 (void) inb_p(ICP);
1069 }
1070
1071 /* forget possible initially masked and pending IRQ */
1072 probe_irq_off(probe_irq_on());
1073 save_mcr = serial_inp(up, UART_MCR);
1074 save_ier = serial_inp(up, UART_IER);
1075 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
1076
1077 irqs = probe_irq_on();
1078 serial_outp(up, UART_MCR, 0);
1079 udelay (10);
1080 if (up->port.flags & UPF_FOURPORT) {
1081 serial_outp(up, UART_MCR,
1082 UART_MCR_DTR | UART_MCR_RTS);
1083 } else {
1084 serial_outp(up, UART_MCR,
1085 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1086 }
1087 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1088 (void)serial_inp(up, UART_LSR);
1089 (void)serial_inp(up, UART_RX);
1090 (void)serial_inp(up, UART_IIR);
1091 (void)serial_inp(up, UART_MSR);
1092 serial_outp(up, UART_TX, 0xFF);
1093 udelay (20);
1094 irq = probe_irq_off(irqs);
1095
1096 serial_outp(up, UART_MCR, save_mcr);
1097 serial_outp(up, UART_IER, save_ier);
1098
1099 if (up->port.flags & UPF_FOURPORT)
1100 outb_p(save_ICP, ICP);
1101
1102 up->port.irq = (irq > 0) ? irq : 0;
1103}
1104
Russell Kinge763b902005-06-29 18:41:51 +01001105static inline void __stop_tx(struct uart_8250_port *p)
1106{
1107 if (p->ier & UART_IER_THRI) {
1108 p->ier &= ~UART_IER_THRI;
1109 serial_out(p, UART_IER, p->ier);
1110 }
1111}
1112
Russell Kingb129a8c2005-08-31 10:12:14 +01001113static void serial8250_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 struct uart_8250_port *up = (struct uart_8250_port *)port;
1116
Russell Kinge763b902005-06-29 18:41:51 +01001117 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 /*
Russell Kinge763b902005-06-29 18:41:51 +01001120 * We really want to stop the transmitter from sending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 */
Russell Kinge763b902005-06-29 18:41:51 +01001122 if (up->port.type == PORT_16C950) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 up->acr |= UART_ACR_TXDIS;
1124 serial_icr_write(up, UART_ACR, up->acr);
1125 }
1126}
1127
Russell King55d3b282005-06-23 15:05:41 +01001128static void transmit_chars(struct uart_8250_port *up);
1129
Russell Kingb129a8c2005-08-31 10:12:14 +01001130static void serial8250_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 struct uart_8250_port *up = (struct uart_8250_port *)port;
1133
1134 if (!(up->ier & UART_IER_THRI)) {
1135 up->ier |= UART_IER_THRI;
1136 serial_out(up, UART_IER, up->ier);
Russell King55d3b282005-06-23 15:05:41 +01001137
Russell King67f76542005-06-23 22:26:43 +01001138 if (up->bugs & UART_BUG_TXEN) {
Russell King55d3b282005-06-23 15:05:41 +01001139 unsigned char lsr, iir;
1140 lsr = serial_in(up, UART_LSR);
1141 iir = serial_in(up, UART_IIR);
1142 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT)
1143 transmit_chars(up);
1144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
Russell Kinge763b902005-06-29 18:41:51 +01001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /*
Russell Kinge763b902005-06-29 18:41:51 +01001148 * Re-enable the transmitter if we disabled it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 */
Russell Kinge763b902005-06-29 18:41:51 +01001150 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 up->acr &= ~UART_ACR_TXDIS;
1152 serial_icr_write(up, UART_ACR, up->acr);
1153 }
1154}
1155
1156static void serial8250_stop_rx(struct uart_port *port)
1157{
1158 struct uart_8250_port *up = (struct uart_8250_port *)port;
1159
1160 up->ier &= ~UART_IER_RLSI;
1161 up->port.read_status_mask &= ~UART_LSR_DR;
1162 serial_out(up, UART_IER, up->ier);
1163}
1164
1165static void serial8250_enable_ms(struct uart_port *port)
1166{
1167 struct uart_8250_port *up = (struct uart_8250_port *)port;
1168
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001169 /* no MSR capabilities */
1170 if (up->bugs & UART_BUG_NOMSR)
1171 return;
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 up->ier |= UART_IER_MSI;
1174 serial_out(up, UART_IER, up->ier);
1175}
1176
Russell Kingea8874d2006-01-04 19:43:24 +00001177static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178receive_chars(struct uart_8250_port *up, int *status, struct pt_regs *regs)
1179{
1180 struct tty_struct *tty = up->port.info->tty;
1181 unsigned char ch, lsr = *status;
1182 int max_count = 256;
1183 char flag;
1184
1185 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 ch = serial_inp(up, UART_RX);
1187 flag = TTY_NORMAL;
1188 up->port.icount.rx++;
1189
1190#ifdef CONFIG_SERIAL_8250_CONSOLE
1191 /*
1192 * Recover the break flag from console xmit
1193 */
1194 if (up->port.line == up->port.cons->index) {
1195 lsr |= up->lsr_break_flag;
1196 up->lsr_break_flag = 0;
1197 }
1198#endif
1199
1200 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
1201 UART_LSR_FE | UART_LSR_OE))) {
1202 /*
1203 * For statistics only
1204 */
1205 if (lsr & UART_LSR_BI) {
1206 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1207 up->port.icount.brk++;
1208 /*
1209 * We do the SysRQ and SAK checking
1210 * here because otherwise the break
1211 * may get masked by ignore_status_mask
1212 * or read_status_mask.
1213 */
1214 if (uart_handle_break(&up->port))
1215 goto ignore_char;
1216 } else if (lsr & UART_LSR_PE)
1217 up->port.icount.parity++;
1218 else if (lsr & UART_LSR_FE)
1219 up->port.icount.frame++;
1220 if (lsr & UART_LSR_OE)
1221 up->port.icount.overrun++;
1222
1223 /*
Russell King23907eb2005-04-16 15:26:39 -07001224 * Mask off conditions which should be ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 */
1226 lsr &= up->port.read_status_mask;
1227
1228 if (lsr & UART_LSR_BI) {
1229 DEBUG_INTR("handling break....");
1230 flag = TTY_BREAK;
1231 } else if (lsr & UART_LSR_PE)
1232 flag = TTY_PARITY;
1233 else if (lsr & UART_LSR_FE)
1234 flag = TTY_FRAME;
1235 }
1236 if (uart_handle_sysrq_char(&up->port, ch, regs))
1237 goto ignore_char;
Russell King05ab3012005-05-09 23:21:59 +01001238
1239 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 ignore_char:
1242 lsr = serial_inp(up, UART_LSR);
1243 } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
1244 spin_unlock(&up->port.lock);
1245 tty_flip_buffer_push(tty);
1246 spin_lock(&up->port.lock);
1247 *status = lsr;
1248}
1249
Russell Kingea8874d2006-01-04 19:43:24 +00001250static void transmit_chars(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 struct circ_buf *xmit = &up->port.info->xmit;
1253 int count;
1254
1255 if (up->port.x_char) {
1256 serial_outp(up, UART_TX, up->port.x_char);
1257 up->port.icount.tx++;
1258 up->port.x_char = 0;
1259 return;
1260 }
Russell Kingb129a8c2005-08-31 10:12:14 +01001261 if (uart_tx_stopped(&up->port)) {
1262 serial8250_stop_tx(&up->port);
1263 return;
1264 }
1265 if (uart_circ_empty(xmit)) {
Russell Kinge763b902005-06-29 18:41:51 +01001266 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return;
1268 }
1269
1270 count = up->tx_loadsz;
1271 do {
1272 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1273 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1274 up->port.icount.tx++;
1275 if (uart_circ_empty(xmit))
1276 break;
1277 } while (--count > 0);
1278
1279 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1280 uart_write_wakeup(&up->port);
1281
1282 DEBUG_INTR("THRE...");
1283
1284 if (uart_circ_empty(xmit))
Russell Kinge763b902005-06-29 18:41:51 +01001285 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Russell King2af7cd62006-01-04 16:55:09 +00001288static unsigned int check_modem_status(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Russell King2af7cd62006-01-04 16:55:09 +00001290 unsigned int status = serial_in(up, UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Russell King2af7cd62006-01-04 16:55:09 +00001292 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI) {
1293 if (status & UART_MSR_TERI)
1294 up->port.icount.rng++;
1295 if (status & UART_MSR_DDSR)
1296 up->port.icount.dsr++;
1297 if (status & UART_MSR_DDCD)
1298 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1299 if (status & UART_MSR_DCTS)
1300 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Russell King2af7cd62006-01-04 16:55:09 +00001302 wake_up_interruptible(&up->port.info->delta_msr_wait);
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Russell King2af7cd62006-01-04 16:55:09 +00001305 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308/*
1309 * This handles the interrupt from one port.
1310 */
1311static inline void
1312serial8250_handle_port(struct uart_8250_port *up, struct pt_regs *regs)
1313{
Russell King45e24602006-01-04 19:19:06 +00001314 unsigned int status;
1315
1316 spin_lock(&up->port.lock);
1317
1318 status = serial_inp(up, UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 DEBUG_INTR("status = %x...", status);
1321
1322 if (status & UART_LSR_DR)
1323 receive_chars(up, &status, regs);
1324 check_modem_status(up);
1325 if (status & UART_LSR_THRE)
1326 transmit_chars(up);
Russell King45e24602006-01-04 19:19:06 +00001327
1328 spin_unlock(&up->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
1331/*
1332 * This is the serial driver's interrupt routine.
1333 *
1334 * Arjan thinks the old way was overly complex, so it got simplified.
1335 * Alan disagrees, saying that need the complexity to handle the weird
1336 * nature of ISA shared interrupts. (This is a special exception.)
1337 *
1338 * In order to handle ISA shared interrupts properly, we need to check
1339 * that all ports have been serviced, and therefore the ISA interrupt
1340 * line has been de-asserted.
1341 *
1342 * This means we need to loop through all ports. checking that they
1343 * don't have an interrupt pending.
1344 */
1345static irqreturn_t serial8250_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1346{
1347 struct irq_info *i = dev_id;
1348 struct list_head *l, *end = NULL;
1349 int pass_counter = 0, handled = 0;
1350
1351 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1352
1353 spin_lock(&i->lock);
1354
1355 l = i->head;
1356 do {
1357 struct uart_8250_port *up;
1358 unsigned int iir;
1359
1360 up = list_entry(l, struct uart_8250_port, list);
1361
1362 iir = serial_in(up, UART_IIR);
1363 if (!(iir & UART_IIR_NO_INT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 serial8250_handle_port(up, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 handled = 1;
1367
1368 end = NULL;
1369 } else if (end == NULL)
1370 end = l;
1371
1372 l = l->next;
1373
1374 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1375 /* If we hit this, we're dead. */
1376 printk(KERN_ERR "serial8250: too much work for "
1377 "irq%d\n", irq);
1378 break;
1379 }
1380 } while (l != end);
1381
1382 spin_unlock(&i->lock);
1383
1384 DEBUG_INTR("end.\n");
1385
1386 return IRQ_RETVAL(handled);
1387}
1388
1389/*
1390 * To support ISA shared interrupts, we need to have one interrupt
1391 * handler that ensures that the IRQ line has been deasserted
1392 * before returning. Failing to do this will result in the IRQ
1393 * line being stuck active, and, since ISA irqs are edge triggered,
1394 * no more IRQs will be seen.
1395 */
1396static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1397{
1398 spin_lock_irq(&i->lock);
1399
1400 if (!list_empty(i->head)) {
1401 if (i->head == &up->list)
1402 i->head = i->head->next;
1403 list_del(&up->list);
1404 } else {
1405 BUG_ON(i->head != &up->list);
1406 i->head = NULL;
1407 }
1408
1409 spin_unlock_irq(&i->lock);
1410}
1411
1412static int serial_link_irq_chain(struct uart_8250_port *up)
1413{
1414 struct irq_info *i = irq_lists + up->port.irq;
Thomas Gleixner40663cc2006-07-01 19:29:43 -07001415 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 spin_lock_irq(&i->lock);
1418
1419 if (i->head) {
1420 list_add(&up->list, i->head);
1421 spin_unlock_irq(&i->lock);
1422
1423 ret = 0;
1424 } else {
1425 INIT_LIST_HEAD(&up->list);
1426 i->head = &up->list;
1427 spin_unlock_irq(&i->lock);
1428
1429 ret = request_irq(up->port.irq, serial8250_interrupt,
1430 irq_flags, "serial", i);
1431 if (ret < 0)
1432 serial_do_unlink(i, up);
1433 }
1434
1435 return ret;
1436}
1437
1438static void serial_unlink_irq_chain(struct uart_8250_port *up)
1439{
1440 struct irq_info *i = irq_lists + up->port.irq;
1441
1442 BUG_ON(i->head == NULL);
1443
1444 if (list_empty(i->head))
1445 free_irq(up->port.irq, i);
1446
1447 serial_do_unlink(i, up);
1448}
1449
1450/*
1451 * This function is used to handle ports that do not have an
1452 * interrupt. This doesn't work very well for 16450's, but gives
1453 * barely passable results for a 16550A. (Although at the expense
1454 * of much CPU overhead).
1455 */
1456static void serial8250_timeout(unsigned long data)
1457{
1458 struct uart_8250_port *up = (struct uart_8250_port *)data;
1459 unsigned int timeout;
1460 unsigned int iir;
1461
1462 iir = serial_in(up, UART_IIR);
Russell King45e24602006-01-04 19:19:06 +00001463 if (!(iir & UART_IIR_NO_INT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 serial8250_handle_port(up, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 timeout = up->port.timeout;
1467 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
1468 mod_timer(&up->timer, jiffies + timeout);
1469}
1470
1471static unsigned int serial8250_tx_empty(struct uart_port *port)
1472{
1473 struct uart_8250_port *up = (struct uart_8250_port *)port;
1474 unsigned long flags;
1475 unsigned int ret;
1476
1477 spin_lock_irqsave(&up->port.lock, flags);
1478 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
1479 spin_unlock_irqrestore(&up->port.lock, flags);
1480
1481 return ret;
1482}
1483
1484static unsigned int serial8250_get_mctrl(struct uart_port *port)
1485{
1486 struct uart_8250_port *up = (struct uart_8250_port *)port;
Russell King2af7cd62006-01-04 16:55:09 +00001487 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 unsigned int ret;
1489
Russell King2af7cd62006-01-04 16:55:09 +00001490 status = check_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 ret = 0;
1493 if (status & UART_MSR_DCD)
1494 ret |= TIOCM_CAR;
1495 if (status & UART_MSR_RI)
1496 ret |= TIOCM_RNG;
1497 if (status & UART_MSR_DSR)
1498 ret |= TIOCM_DSR;
1499 if (status & UART_MSR_CTS)
1500 ret |= TIOCM_CTS;
1501 return ret;
1502}
1503
1504static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1505{
1506 struct uart_8250_port *up = (struct uart_8250_port *)port;
1507 unsigned char mcr = 0;
1508
1509 if (mctrl & TIOCM_RTS)
1510 mcr |= UART_MCR_RTS;
1511 if (mctrl & TIOCM_DTR)
1512 mcr |= UART_MCR_DTR;
1513 if (mctrl & TIOCM_OUT1)
1514 mcr |= UART_MCR_OUT1;
1515 if (mctrl & TIOCM_OUT2)
1516 mcr |= UART_MCR_OUT2;
1517 if (mctrl & TIOCM_LOOP)
1518 mcr |= UART_MCR_LOOP;
1519
1520 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1521
1522 serial_out(up, UART_MCR, mcr);
1523}
1524
1525static void serial8250_break_ctl(struct uart_port *port, int break_state)
1526{
1527 struct uart_8250_port *up = (struct uart_8250_port *)port;
1528 unsigned long flags;
1529
1530 spin_lock_irqsave(&up->port.lock, flags);
1531 if (break_state == -1)
1532 up->lcr |= UART_LCR_SBC;
1533 else
1534 up->lcr &= ~UART_LCR_SBC;
1535 serial_out(up, UART_LCR, up->lcr);
1536 spin_unlock_irqrestore(&up->port.lock, flags);
1537}
1538
1539static int serial8250_startup(struct uart_port *port)
1540{
1541 struct uart_8250_port *up = (struct uart_8250_port *)port;
1542 unsigned long flags;
Russell King55d3b282005-06-23 15:05:41 +01001543 unsigned char lsr, iir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 int retval;
1545
1546 up->capabilities = uart_config[up->port.type].flags;
1547 up->mcr = 0;
1548
1549 if (up->port.type == PORT_16C950) {
1550 /* Wake up and initialize UART */
1551 up->acr = 0;
1552 serial_outp(up, UART_LCR, 0xBF);
1553 serial_outp(up, UART_EFR, UART_EFR_ECB);
1554 serial_outp(up, UART_IER, 0);
1555 serial_outp(up, UART_LCR, 0);
1556 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
1557 serial_outp(up, UART_LCR, 0xBF);
1558 serial_outp(up, UART_EFR, UART_EFR_ECB);
1559 serial_outp(up, UART_LCR, 0);
1560 }
1561
1562#ifdef CONFIG_SERIAL_8250_RSA
1563 /*
1564 * If this is an RSA port, see if we can kick it up to the
1565 * higher speed clock.
1566 */
1567 enable_rsa(up);
1568#endif
1569
1570 /*
1571 * Clear the FIFO buffers and disable them.
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08001572 * (they will be reenabled in set_termios())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 */
1574 serial8250_clear_fifos(up);
1575
1576 /*
1577 * Clear the interrupt registers.
1578 */
1579 (void) serial_inp(up, UART_LSR);
1580 (void) serial_inp(up, UART_RX);
1581 (void) serial_inp(up, UART_IIR);
1582 (void) serial_inp(up, UART_MSR);
1583
1584 /*
1585 * At this point, there's no way the LSR could still be 0xff;
1586 * if it is, then bail out, because there's likely no UART
1587 * here.
1588 */
1589 if (!(up->port.flags & UPF_BUGGY_UART) &&
1590 (serial_inp(up, UART_LSR) == 0xff)) {
1591 printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
1592 return -ENODEV;
1593 }
1594
1595 /*
1596 * For a XR16C850, we need to set the trigger levels
1597 */
1598 if (up->port.type == PORT_16850) {
1599 unsigned char fctr;
1600
1601 serial_outp(up, UART_LCR, 0xbf);
1602
1603 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
1604 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
1605 serial_outp(up, UART_TRG, UART_TRG_96);
1606 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
1607 serial_outp(up, UART_TRG, UART_TRG_96);
1608
1609 serial_outp(up, UART_LCR, 0);
1610 }
1611
1612 /*
1613 * If the "interrupt" for this port doesn't correspond with any
1614 * hardware interrupt, we use a timer-based system. The original
1615 * driver used to do this with IRQ0.
1616 */
1617 if (!is_real_interrupt(up->port.irq)) {
1618 unsigned int timeout = up->port.timeout;
1619
1620 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
1621
1622 up->timer.data = (unsigned long)up;
1623 mod_timer(&up->timer, jiffies + timeout);
1624 } else {
1625 retval = serial_link_irq_chain(up);
1626 if (retval)
1627 return retval;
1628 }
1629
1630 /*
1631 * Now, initialize the UART
1632 */
1633 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
1634
1635 spin_lock_irqsave(&up->port.lock, flags);
1636 if (up->port.flags & UPF_FOURPORT) {
1637 if (!is_real_interrupt(up->port.irq))
1638 up->port.mctrl |= TIOCM_OUT1;
1639 } else
1640 /*
1641 * Most PC uarts need OUT2 raised to enable interrupts.
1642 */
1643 if (is_real_interrupt(up->port.irq))
1644 up->port.mctrl |= TIOCM_OUT2;
1645
1646 serial8250_set_mctrl(&up->port, up->port.mctrl);
Russell King55d3b282005-06-23 15:05:41 +01001647
1648 /*
1649 * Do a quick test to see if we receive an
1650 * interrupt when we enable the TX irq.
1651 */
1652 serial_outp(up, UART_IER, UART_IER_THRI);
1653 lsr = serial_in(up, UART_LSR);
1654 iir = serial_in(up, UART_IIR);
1655 serial_outp(up, UART_IER, 0);
1656
1657 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
Russell King67f76542005-06-23 22:26:43 +01001658 if (!(up->bugs & UART_BUG_TXEN)) {
1659 up->bugs |= UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01001660 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
1661 port->line);
1662 }
1663 } else {
Russell King67f76542005-06-23 22:26:43 +01001664 up->bugs &= ~UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01001665 }
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 spin_unlock_irqrestore(&up->port.lock, flags);
1668
1669 /*
1670 * Finally, enable interrupts. Note: Modem status interrupts
1671 * are set via set_termios(), which will be occurring imminently
1672 * anyway, so we don't enable them here.
1673 */
1674 up->ier = UART_IER_RLSI | UART_IER_RDI;
1675 serial_outp(up, UART_IER, up->ier);
1676
1677 if (up->port.flags & UPF_FOURPORT) {
1678 unsigned int icp;
1679 /*
1680 * Enable interrupts on the AST Fourport board
1681 */
1682 icp = (up->port.iobase & 0xfe0) | 0x01f;
1683 outb_p(0x80, icp);
1684 (void) inb_p(icp);
1685 }
1686
1687 /*
1688 * And clear the interrupt registers again for luck.
1689 */
1690 (void) serial_inp(up, UART_LSR);
1691 (void) serial_inp(up, UART_RX);
1692 (void) serial_inp(up, UART_IIR);
1693 (void) serial_inp(up, UART_MSR);
1694
1695 return 0;
1696}
1697
1698static void serial8250_shutdown(struct uart_port *port)
1699{
1700 struct uart_8250_port *up = (struct uart_8250_port *)port;
1701 unsigned long flags;
1702
1703 /*
1704 * Disable interrupts from this port
1705 */
1706 up->ier = 0;
1707 serial_outp(up, UART_IER, 0);
1708
1709 spin_lock_irqsave(&up->port.lock, flags);
1710 if (up->port.flags & UPF_FOURPORT) {
1711 /* reset interrupts on the AST Fourport board */
1712 inb((up->port.iobase & 0xfe0) | 0x1f);
1713 up->port.mctrl |= TIOCM_OUT1;
1714 } else
1715 up->port.mctrl &= ~TIOCM_OUT2;
1716
1717 serial8250_set_mctrl(&up->port, up->port.mctrl);
1718 spin_unlock_irqrestore(&up->port.lock, flags);
1719
1720 /*
1721 * Disable break condition and FIFOs
1722 */
1723 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
1724 serial8250_clear_fifos(up);
1725
1726#ifdef CONFIG_SERIAL_8250_RSA
1727 /*
1728 * Reset the RSA board back to 115kbps compat mode.
1729 */
1730 disable_rsa(up);
1731#endif
1732
1733 /*
1734 * Read data port to reset things, and then unlink from
1735 * the IRQ chain.
1736 */
1737 (void) serial_in(up, UART_RX);
1738
1739 if (!is_real_interrupt(up->port.irq))
1740 del_timer_sync(&up->timer);
1741 else
1742 serial_unlink_irq_chain(up);
1743}
1744
1745static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
1746{
1747 unsigned int quot;
1748
1749 /*
1750 * Handle magic divisors for baud rates above baud_base on
1751 * SMSC SuperIO chips.
1752 */
1753 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
1754 baud == (port->uartclk/4))
1755 quot = 0x8001;
1756 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
1757 baud == (port->uartclk/8))
1758 quot = 0x8002;
1759 else
1760 quot = uart_get_divisor(port, baud);
1761
1762 return quot;
1763}
1764
1765static void
1766serial8250_set_termios(struct uart_port *port, struct termios *termios,
1767 struct termios *old)
1768{
1769 struct uart_8250_port *up = (struct uart_8250_port *)port;
1770 unsigned char cval, fcr = 0;
1771 unsigned long flags;
1772 unsigned int baud, quot;
1773
1774 switch (termios->c_cflag & CSIZE) {
1775 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +01001776 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 break;
1778 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +01001779 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 break;
1781 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +01001782 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 break;
1784 default:
1785 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +01001786 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 break;
1788 }
1789
1790 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +01001791 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (termios->c_cflag & PARENB)
1793 cval |= UART_LCR_PARITY;
1794 if (!(termios->c_cflag & PARODD))
1795 cval |= UART_LCR_EPAR;
1796#ifdef CMSPAR
1797 if (termios->c_cflag & CMSPAR)
1798 cval |= UART_LCR_SPAR;
1799#endif
1800
1801 /*
1802 * Ask the core to calculate the divisor for us.
1803 */
1804 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
1805 quot = serial8250_get_divisor(port, baud);
1806
1807 /*
Russell King4ba5e352005-06-23 10:43:04 +01001808 * Oxford Semi 952 rev B workaround
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 */
Russell King4ba5e352005-06-23 10:43:04 +01001810 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 quot ++;
1812
1813 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
1814 if (baud < 2400)
1815 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1816 else
1817 fcr = uart_config[up->port.type].fcr;
1818 }
1819
1820 /*
1821 * MCR-based auto flow control. When AFE is enabled, RTS will be
1822 * deasserted when the receive FIFO contains more characters than
1823 * the trigger, or the MCR RTS bit is cleared. In the case where
1824 * the remote UART is not using CTS auto flow control, we must
1825 * have sufficient FIFO entries for the latency of the remote
1826 * UART to respond. IOW, at least 32 bytes of FIFO.
1827 */
1828 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
1829 up->mcr &= ~UART_MCR_AFE;
1830 if (termios->c_cflag & CRTSCTS)
1831 up->mcr |= UART_MCR_AFE;
1832 }
1833
1834 /*
1835 * Ok, we're now changing the port state. Do it with
1836 * interrupts disabled.
1837 */
1838 spin_lock_irqsave(&up->port.lock, flags);
1839
1840 /*
1841 * Update the per-port timeout.
1842 */
1843 uart_update_timeout(port, termios->c_cflag, baud);
1844
1845 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1846 if (termios->c_iflag & INPCK)
1847 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1848 if (termios->c_iflag & (BRKINT | PARMRK))
1849 up->port.read_status_mask |= UART_LSR_BI;
1850
1851 /*
1852 * Characteres to ignore
1853 */
1854 up->port.ignore_status_mask = 0;
1855 if (termios->c_iflag & IGNPAR)
1856 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1857 if (termios->c_iflag & IGNBRK) {
1858 up->port.ignore_status_mask |= UART_LSR_BI;
1859 /*
1860 * If we're ignoring parity and break indicators,
1861 * ignore overruns too (for real raw support).
1862 */
1863 if (termios->c_iflag & IGNPAR)
1864 up->port.ignore_status_mask |= UART_LSR_OE;
1865 }
1866
1867 /*
1868 * ignore all characters if CREAD is not set
1869 */
1870 if ((termios->c_cflag & CREAD) == 0)
1871 up->port.ignore_status_mask |= UART_LSR_DR;
1872
1873 /*
1874 * CTS flow control flag and modem status interrupts
1875 */
1876 up->ier &= ~UART_IER_MSI;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001877 if (!(up->bugs & UART_BUG_NOMSR) &&
1878 UART_ENABLE_MS(&up->port, termios->c_cflag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 up->ier |= UART_IER_MSI;
1880 if (up->capabilities & UART_CAP_UUE)
1881 up->ier |= UART_IER_UUE | UART_IER_RTOIE;
1882
1883 serial_out(up, UART_IER, up->ier);
1884
1885 if (up->capabilities & UART_CAP_EFR) {
1886 unsigned char efr = 0;
1887 /*
1888 * TI16C752/Startech hardware flow control. FIXME:
1889 * - TI16C752 requires control thresholds to be set.
1890 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
1891 */
1892 if (termios->c_cflag & CRTSCTS)
1893 efr |= UART_EFR_CTS;
1894
1895 serial_outp(up, UART_LCR, 0xBF);
1896 serial_outp(up, UART_EFR, efr);
1897 }
1898
Jonathan McDowell255341c2006-08-14 23:05:32 -07001899#ifdef CONFIG_ARCH_OMAP15XX
1900 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
1901 if (cpu_is_omap1510() && is_omap_port((unsigned int)up->port.membase)) {
1902 if (baud == 115200) {
1903 quot = 1;
1904 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
1905 } else
1906 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
1907 }
1908#endif
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (up->capabilities & UART_NATSEMI) {
1911 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
1912 serial_outp(up, UART_LCR, 0xe0);
1913 } else {
1914 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
1915 }
1916
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +01001917 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 /*
1920 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
1921 * is written without DLAB set, this mode will be disabled.
1922 */
1923 if (up->port.type == PORT_16750)
1924 serial_outp(up, UART_FCR, fcr);
1925
1926 serial_outp(up, UART_LCR, cval); /* reset DLAB */
1927 up->lcr = cval; /* Save LCR */
1928 if (up->port.type != PORT_16750) {
1929 if (fcr & UART_FCR_ENABLE_FIFO) {
1930 /* emulated UARTs (Lucent Venus 167x) need two steps */
1931 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1932 }
1933 serial_outp(up, UART_FCR, fcr); /* set fcr */
1934 }
1935 serial8250_set_mctrl(&up->port, up->port.mctrl);
1936 spin_unlock_irqrestore(&up->port.lock, flags);
1937}
1938
1939static void
1940serial8250_pm(struct uart_port *port, unsigned int state,
1941 unsigned int oldstate)
1942{
1943 struct uart_8250_port *p = (struct uart_8250_port *)port;
1944
1945 serial8250_set_sleep(p, state != 0);
1946
1947 if (p->pm)
1948 p->pm(port, state, oldstate);
1949}
1950
1951/*
1952 * Resource handling.
1953 */
1954static int serial8250_request_std_resource(struct uart_8250_port *up)
1955{
1956 unsigned int size = 8 << up->port.regshift;
1957 int ret = 0;
1958
1959 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01001960 case UPIO_AU:
1961 size = 0x100000;
1962 /* fall thru */
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04001963 case UPIO_TSI:
1964 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 case UPIO_MEM:
1966 if (!up->port.mapbase)
1967 break;
1968
1969 if (!request_mem_region(up->port.mapbase, size, "serial")) {
1970 ret = -EBUSY;
1971 break;
1972 }
1973
1974 if (up->port.flags & UPF_IOREMAP) {
1975 up->port.membase = ioremap(up->port.mapbase, size);
1976 if (!up->port.membase) {
1977 release_mem_region(up->port.mapbase, size);
1978 ret = -ENOMEM;
1979 }
1980 }
1981 break;
1982
1983 case UPIO_HUB6:
1984 case UPIO_PORT:
1985 if (!request_region(up->port.iobase, size, "serial"))
1986 ret = -EBUSY;
1987 break;
1988 }
1989 return ret;
1990}
1991
1992static void serial8250_release_std_resource(struct uart_8250_port *up)
1993{
1994 unsigned int size = 8 << up->port.regshift;
1995
1996 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01001997 case UPIO_AU:
1998 size = 0x100000;
1999 /* fall thru */
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002000 case UPIO_TSI:
2001 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 case UPIO_MEM:
2003 if (!up->port.mapbase)
2004 break;
2005
2006 if (up->port.flags & UPF_IOREMAP) {
2007 iounmap(up->port.membase);
2008 up->port.membase = NULL;
2009 }
2010
2011 release_mem_region(up->port.mapbase, size);
2012 break;
2013
2014 case UPIO_HUB6:
2015 case UPIO_PORT:
2016 release_region(up->port.iobase, size);
2017 break;
2018 }
2019}
2020
2021static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2022{
2023 unsigned long start = UART_RSA_BASE << up->port.regshift;
2024 unsigned int size = 8 << up->port.regshift;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002025 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 case UPIO_HUB6:
2029 case UPIO_PORT:
2030 start += up->port.iobase;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002031 if (request_region(start, size, "serial-rsa"))
2032 ret = 0;
2033 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 ret = -EBUSY;
2035 break;
2036 }
2037
2038 return ret;
2039}
2040
2041static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2042{
2043 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2044 unsigned int size = 8 << up->port.regshift;
2045
2046 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 case UPIO_HUB6:
2048 case UPIO_PORT:
2049 release_region(up->port.iobase + offset, size);
2050 break;
2051 }
2052}
2053
2054static void serial8250_release_port(struct uart_port *port)
2055{
2056 struct uart_8250_port *up = (struct uart_8250_port *)port;
2057
2058 serial8250_release_std_resource(up);
2059 if (up->port.type == PORT_RSA)
2060 serial8250_release_rsa_resource(up);
2061}
2062
2063static int serial8250_request_port(struct uart_port *port)
2064{
2065 struct uart_8250_port *up = (struct uart_8250_port *)port;
2066 int ret = 0;
2067
2068 ret = serial8250_request_std_resource(up);
2069 if (ret == 0 && up->port.type == PORT_RSA) {
2070 ret = serial8250_request_rsa_resource(up);
2071 if (ret < 0)
2072 serial8250_release_std_resource(up);
2073 }
2074
2075 return ret;
2076}
2077
2078static void serial8250_config_port(struct uart_port *port, int flags)
2079{
2080 struct uart_8250_port *up = (struct uart_8250_port *)port;
2081 int probeflags = PROBE_ANY;
2082 int ret;
2083
2084 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 * Find the region that we can probe for. This in turn
2086 * tells us whether we can probe for the type of port.
2087 */
2088 ret = serial8250_request_std_resource(up);
2089 if (ret < 0)
2090 return;
2091
2092 ret = serial8250_request_rsa_resource(up);
2093 if (ret < 0)
2094 probeflags &= ~PROBE_RSA;
2095
2096 if (flags & UART_CONFIG_TYPE)
2097 autoconfig(up, probeflags);
2098 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2099 autoconfig_irq(up);
2100
2101 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2102 serial8250_release_rsa_resource(up);
2103 if (up->port.type == PORT_UNKNOWN)
2104 serial8250_release_std_resource(up);
2105}
2106
2107static int
2108serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2109{
2110 if (ser->irq >= NR_IRQS || ser->irq < 0 ||
2111 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2112 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2113 ser->type == PORT_STARTECH)
2114 return -EINVAL;
2115 return 0;
2116}
2117
2118static const char *
2119serial8250_type(struct uart_port *port)
2120{
2121 int type = port->type;
2122
2123 if (type >= ARRAY_SIZE(uart_config))
2124 type = 0;
2125 return uart_config[type].name;
2126}
2127
2128static struct uart_ops serial8250_pops = {
2129 .tx_empty = serial8250_tx_empty,
2130 .set_mctrl = serial8250_set_mctrl,
2131 .get_mctrl = serial8250_get_mctrl,
2132 .stop_tx = serial8250_stop_tx,
2133 .start_tx = serial8250_start_tx,
2134 .stop_rx = serial8250_stop_rx,
2135 .enable_ms = serial8250_enable_ms,
2136 .break_ctl = serial8250_break_ctl,
2137 .startup = serial8250_startup,
2138 .shutdown = serial8250_shutdown,
2139 .set_termios = serial8250_set_termios,
2140 .pm = serial8250_pm,
2141 .type = serial8250_type,
2142 .release_port = serial8250_release_port,
2143 .request_port = serial8250_request_port,
2144 .config_port = serial8250_config_port,
2145 .verify_port = serial8250_verify_port,
2146};
2147
2148static struct uart_8250_port serial8250_ports[UART_NR];
2149
2150static void __init serial8250_isa_init_ports(void)
2151{
2152 struct uart_8250_port *up;
2153 static int first = 1;
2154 int i;
2155
2156 if (!first)
2157 return;
2158 first = 0;
2159
Dave Jonesa61c2d72006-01-07 23:18:19 +00002160 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 struct uart_8250_port *up = &serial8250_ports[i];
2162
2163 up->port.line = i;
2164 spin_lock_init(&up->port.lock);
2165
2166 init_timer(&up->timer);
2167 up->timer.function = serial8250_timeout;
2168
2169 /*
2170 * ALPHA_KLUDGE_MCR needs to be killed.
2171 */
2172 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2173 up->mcr_force = ALPHA_KLUDGE_MCR;
2174
2175 up->port.ops = &serial8250_pops;
2176 }
2177
Russell King44454bc2005-06-30 22:41:22 +01002178 for (i = 0, up = serial8250_ports;
Dave Jonesa61c2d72006-01-07 23:18:19 +00002179 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 i++, up++) {
2181 up->port.iobase = old_serial_port[i].port;
2182 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
2183 up->port.uartclk = old_serial_port[i].baud_base * 16;
2184 up->port.flags = old_serial_port[i].flags;
2185 up->port.hub6 = old_serial_port[i].hub6;
2186 up->port.membase = old_serial_port[i].iomem_base;
2187 up->port.iotype = old_serial_port[i].io_type;
2188 up->port.regshift = old_serial_port[i].iomem_reg_shift;
2189 if (share_irqs)
2190 up->port.flags |= UPF_SHARE_IRQ;
2191 }
2192}
2193
2194static void __init
2195serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2196{
2197 int i;
2198
2199 serial8250_isa_init_ports();
2200
Dave Jonesa61c2d72006-01-07 23:18:19 +00002201 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 struct uart_8250_port *up = &serial8250_ports[i];
2203
2204 up->port.dev = dev;
2205 uart_add_one_port(drv, &up->port);
2206 }
2207}
2208
2209#ifdef CONFIG_SERIAL_8250_CONSOLE
2210
2211#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
2212
2213/*
2214 * Wait for transmitter & holding register to empty
2215 */
Alan Coxf91a3712006-01-21 14:59:12 +00002216static inline void wait_for_xmitr(struct uart_8250_port *up, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218 unsigned int status, tmout = 10000;
2219
2220 /* Wait up to 10ms for the character(s) to be sent. */
2221 do {
2222 status = serial_in(up, UART_LSR);
2223
2224 if (status & UART_LSR_BI)
2225 up->lsr_break_flag = UART_LSR_BI;
2226
2227 if (--tmout == 0)
2228 break;
2229 udelay(1);
Alan Coxf91a3712006-01-21 14:59:12 +00002230 } while ((status & bits) != bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 /* Wait up to 1s for flow control if necessary */
2233 if (up->port.flags & UPF_CONS_FLOW) {
2234 tmout = 1000000;
2235 while (--tmout &&
2236 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
2237 udelay(1);
2238 }
2239}
2240
Russell Kingd3587882006-03-20 20:00:09 +00002241static void serial8250_console_putchar(struct uart_port *port, int ch)
2242{
2243 struct uart_8250_port *up = (struct uart_8250_port *)port;
2244
2245 wait_for_xmitr(up, UART_LSR_THRE);
2246 serial_out(up, UART_TX, ch);
2247}
2248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249/*
2250 * Print a string to the serial port trying not to disturb
2251 * any possible real use of the port...
2252 *
2253 * The console_lock must be held when we get here.
2254 */
2255static void
2256serial8250_console_write(struct console *co, const char *s, unsigned int count)
2257{
2258 struct uart_8250_port *up = &serial8250_ports[co->index];
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002259 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 unsigned int ier;
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002261 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Andrew Morton78512ec2005-11-07 00:59:13 -08002263 touch_nmi_watchdog();
2264
Andrew Morton68aa2c02006-06-30 02:29:59 -07002265 local_irq_save(flags);
2266 if (up->port.sysrq) {
2267 /* serial8250_handle_port() already took the lock */
2268 locked = 0;
2269 } else if (oops_in_progress) {
2270 locked = spin_trylock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002271 } else
Andrew Morton68aa2c02006-06-30 02:29:59 -07002272 spin_lock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 /*
Ralf Baechledc7bf132006-02-15 09:59:47 +00002275 * First save the IER then disable the interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 */
2277 ier = serial_in(up, UART_IER);
2278
2279 if (up->capabilities & UART_CAP_UUE)
2280 serial_out(up, UART_IER, UART_IER_UUE);
2281 else
2282 serial_out(up, UART_IER, 0);
2283
Russell Kingd3587882006-03-20 20:00:09 +00002284 uart_console_write(&up->port, s, count, serial8250_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 /*
2287 * Finally, wait for transmitter to become empty
2288 * and restore the IER
2289 */
Alan Coxf91a3712006-01-21 14:59:12 +00002290 wait_for_xmitr(up, BOTH_EMPTY);
Russell Kinga88d75b2006-04-30 11:30:15 +01002291 serial_out(up, UART_IER, ier);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002292
2293 if (locked)
Andrew Morton68aa2c02006-06-30 02:29:59 -07002294 spin_unlock(&up->port.lock);
2295 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
2297
2298static int serial8250_console_setup(struct console *co, char *options)
2299{
2300 struct uart_port *port;
2301 int baud = 9600;
2302 int bits = 8;
2303 int parity = 'n';
2304 int flow = 'n';
2305
2306 /*
2307 * Check whether an invalid uart number has been specified, and
2308 * if so, search for the first available port that does have
2309 * console support.
2310 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002311 if (co->index >= nr_uarts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 co->index = 0;
2313 port = &serial8250_ports[co->index].port;
2314 if (!port->iobase && !port->membase)
2315 return -ENODEV;
2316
2317 if (options)
2318 uart_parse_options(options, &baud, &parity, &bits, &flow);
2319
2320 return uart_set_options(port, co, baud, parity, bits, flow);
2321}
2322
2323static struct uart_driver serial8250_reg;
2324static struct console serial8250_console = {
2325 .name = "ttyS",
2326 .write = serial8250_console_write,
2327 .device = uart_console_device,
2328 .setup = serial8250_console_setup,
2329 .flags = CON_PRINTBUFFER,
2330 .index = -1,
2331 .data = &serial8250_reg,
2332};
2333
2334static int __init serial8250_console_init(void)
2335{
2336 serial8250_isa_init_ports();
2337 register_console(&serial8250_console);
2338 return 0;
2339}
2340console_initcall(serial8250_console_init);
2341
2342static int __init find_port(struct uart_port *p)
2343{
2344 int line;
2345 struct uart_port *port;
2346
Dave Jonesa61c2d72006-01-07 23:18:19 +00002347 for (line = 0; line < nr_uarts; line++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 port = &serial8250_ports[line].port;
Russell King50aec3b52006-01-04 18:13:03 +00002349 if (uart_match_port(p, port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return line;
2351 }
2352 return -ENODEV;
2353}
2354
2355int __init serial8250_start_console(struct uart_port *port, char *options)
2356{
2357 int line;
2358
2359 line = find_port(port);
2360 if (line < 0)
2361 return -ENODEV;
2362
2363 add_preferred_console("ttyS", line, options);
2364 printk("Adding console on ttyS%d at %s 0x%lx (options '%s')\n",
2365 line, port->iotype == UPIO_MEM ? "MMIO" : "I/O port",
2366 port->iotype == UPIO_MEM ? (unsigned long) port->mapbase :
2367 (unsigned long) port->iobase, options);
2368 if (!(serial8250_console.flags & CON_ENABLED)) {
2369 serial8250_console.flags &= ~CON_PRINTBUFFER;
2370 register_console(&serial8250_console);
2371 }
2372 return line;
2373}
2374
2375#define SERIAL8250_CONSOLE &serial8250_console
2376#else
2377#define SERIAL8250_CONSOLE NULL
2378#endif
2379
2380static struct uart_driver serial8250_reg = {
2381 .owner = THIS_MODULE,
2382 .driver_name = "serial",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 .dev_name = "ttyS",
2384 .major = TTY_MAJOR,
2385 .minor = 64,
2386 .nr = UART_NR,
2387 .cons = SERIAL8250_CONSOLE,
2388};
2389
Russell Kingd856c662006-02-23 10:22:13 +00002390/*
2391 * early_serial_setup - early registration for 8250 ports
2392 *
2393 * Setup an 8250 port structure prior to console initialisation. Use
2394 * after console initialisation will cause undefined behaviour.
2395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396int __init early_serial_setup(struct uart_port *port)
2397{
2398 if (port->line >= ARRAY_SIZE(serial8250_ports))
2399 return -ENODEV;
2400
2401 serial8250_isa_init_ports();
2402 serial8250_ports[port->line].port = *port;
2403 serial8250_ports[port->line].port.ops = &serial8250_pops;
2404 return 0;
2405}
2406
2407/**
2408 * serial8250_suspend_port - suspend one serial port
2409 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 *
2411 * Suspend one serial port.
2412 */
2413void serial8250_suspend_port(int line)
2414{
2415 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2416}
2417
2418/**
2419 * serial8250_resume_port - resume one serial port
2420 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 *
2422 * Resume one serial port.
2423 */
2424void serial8250_resume_port(int line)
2425{
2426 uart_resume_port(&serial8250_reg, &serial8250_ports[line].port);
2427}
2428
2429/*
2430 * Register a set of serial devices attached to a platform device. The
2431 * list is terminated with a zero flags entry, which means we expect
2432 * all entries to have at least UPF_BOOT_AUTOCONF set.
2433 */
Russell King3ae5eae2005-11-09 22:32:44 +00002434static int __devinit serial8250_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Russell King3ae5eae2005-11-09 22:32:44 +00002436 struct plat_serial8250_port *p = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 struct uart_port port;
Russell Kingec9f47c2005-06-27 11:12:54 +01002438 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 memset(&port, 0, sizeof(struct uart_port));
2441
Russell Kingec9f47c2005-06-27 11:12:54 +01002442 for (i = 0; p && p->flags != 0; p++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 port.iobase = p->iobase;
2444 port.membase = p->membase;
2445 port.irq = p->irq;
2446 port.uartclk = p->uartclk;
2447 port.regshift = p->regshift;
2448 port.iotype = p->iotype;
2449 port.flags = p->flags;
2450 port.mapbase = p->mapbase;
Russell Kingec9f47c2005-06-27 11:12:54 +01002451 port.hub6 = p->hub6;
Russell King3ae5eae2005-11-09 22:32:44 +00002452 port.dev = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (share_irqs)
2454 port.flags |= UPF_SHARE_IRQ;
Russell Kingec9f47c2005-06-27 11:12:54 +01002455 ret = serial8250_register_port(&port);
2456 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00002457 dev_err(&dev->dev, "unable to register port at index %d "
Russell Kingec9f47c2005-06-27 11:12:54 +01002458 "(IO%lx MEM%lx IRQ%d): %d\n", i,
2459 p->iobase, p->mapbase, p->irq, ret);
2460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
2462 return 0;
2463}
2464
2465/*
2466 * Remove serial ports registered against a platform device.
2467 */
Russell King3ae5eae2005-11-09 22:32:44 +00002468static int __devexit serial8250_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 int i;
2471
Dave Jonesa61c2d72006-01-07 23:18:19 +00002472 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 struct uart_8250_port *up = &serial8250_ports[i];
2474
Russell King3ae5eae2005-11-09 22:32:44 +00002475 if (up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 serial8250_unregister_port(i);
2477 }
2478 return 0;
2479}
2480
Russell King3ae5eae2005-11-09 22:32:44 +00002481static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
2483 int i;
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 for (i = 0; i < UART_NR; i++) {
2486 struct uart_8250_port *up = &serial8250_ports[i];
2487
Russell King3ae5eae2005-11-09 22:32:44 +00002488 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 uart_suspend_port(&serial8250_reg, &up->port);
2490 }
2491
2492 return 0;
2493}
2494
Russell King3ae5eae2005-11-09 22:32:44 +00002495static int serial8250_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
2497 int i;
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 for (i = 0; i < UART_NR; i++) {
2500 struct uart_8250_port *up = &serial8250_ports[i];
2501
Russell King3ae5eae2005-11-09 22:32:44 +00002502 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 uart_resume_port(&serial8250_reg, &up->port);
2504 }
2505
2506 return 0;
2507}
2508
Russell King3ae5eae2005-11-09 22:32:44 +00002509static struct platform_driver serial8250_isa_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 .probe = serial8250_probe,
2511 .remove = __devexit_p(serial8250_remove),
2512 .suspend = serial8250_suspend,
2513 .resume = serial8250_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002514 .driver = {
2515 .name = "serial8250",
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002516 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002517 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518};
2519
2520/*
2521 * This "device" covers _all_ ISA 8250-compatible serial devices listed
2522 * in the table in include/asm/serial.h
2523 */
2524static struct platform_device *serial8250_isa_devs;
2525
2526/*
2527 * serial8250_register_port and serial8250_unregister_port allows for
2528 * 16x50 serial ports to be configured at run-time, to support PCMCIA
2529 * modems and PCI multiport cards.
2530 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002531static DEFINE_MUTEX(serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
2534{
2535 int i;
2536
2537 /*
2538 * First, find a port entry which matches.
2539 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002540 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 if (uart_match_port(&serial8250_ports[i].port, port))
2542 return &serial8250_ports[i];
2543
2544 /*
2545 * We didn't find a matching entry, so look for the first
2546 * free entry. We look for one which hasn't been previously
2547 * used (indicated by zero iobase).
2548 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002549 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
2551 serial8250_ports[i].port.iobase == 0)
2552 return &serial8250_ports[i];
2553
2554 /*
2555 * That also failed. Last resort is to find any entry which
2556 * doesn't have a real port associated with it.
2557 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002558 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
2560 return &serial8250_ports[i];
2561
2562 return NULL;
2563}
2564
2565/**
2566 * serial8250_register_port - register a serial port
2567 * @port: serial port template
2568 *
2569 * Configure the serial port specified by the request. If the
2570 * port exists and is in use, it is hung up and unregistered
2571 * first.
2572 *
2573 * The port is then probed and if necessary the IRQ is autodetected
2574 * If this fails an error is returned.
2575 *
2576 * On success the port is ready to use and the line number is returned.
2577 */
2578int serial8250_register_port(struct uart_port *port)
2579{
2580 struct uart_8250_port *uart;
2581 int ret = -ENOSPC;
2582
2583 if (port->uartclk == 0)
2584 return -EINVAL;
2585
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002586 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 uart = serial8250_find_match_or_unused(port);
2589 if (uart) {
2590 uart_remove_one_port(&serial8250_reg, &uart->port);
2591
2592 uart->port.iobase = port->iobase;
2593 uart->port.membase = port->membase;
2594 uart->port.irq = port->irq;
2595 uart->port.uartclk = port->uartclk;
2596 uart->port.fifosize = port->fifosize;
2597 uart->port.regshift = port->regshift;
2598 uart->port.iotype = port->iotype;
2599 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
2600 uart->port.mapbase = port->mapbase;
2601 if (port->dev)
2602 uart->port.dev = port->dev;
2603
2604 ret = uart_add_one_port(&serial8250_reg, &uart->port);
2605 if (ret == 0)
2606 ret = uart->port.line;
2607 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002608 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 return ret;
2611}
2612EXPORT_SYMBOL(serial8250_register_port);
2613
2614/**
2615 * serial8250_unregister_port - remove a 16x50 serial port at runtime
2616 * @line: serial line number
2617 *
2618 * Remove one serial port. This may not be called from interrupt
2619 * context. We hand the port back to the our control.
2620 */
2621void serial8250_unregister_port(int line)
2622{
2623 struct uart_8250_port *uart = &serial8250_ports[line];
2624
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002625 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 uart_remove_one_port(&serial8250_reg, &uart->port);
2627 if (serial8250_isa_devs) {
2628 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
2629 uart->port.type = PORT_UNKNOWN;
2630 uart->port.dev = &serial8250_isa_devs->dev;
2631 uart_add_one_port(&serial8250_reg, &uart->port);
2632 } else {
2633 uart->port.dev = NULL;
2634 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002635 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636}
2637EXPORT_SYMBOL(serial8250_unregister_port);
2638
2639static int __init serial8250_init(void)
2640{
2641 int ret, i;
2642
Dave Jonesa61c2d72006-01-07 23:18:19 +00002643 if (nr_uarts > UART_NR)
2644 nr_uarts = UART_NR;
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 printk(KERN_INFO "Serial: 8250/16550 driver $Revision: 1.90 $ "
Dave Jonesa61c2d72006-01-07 23:18:19 +00002647 "%d ports, IRQ sharing %sabled\n", nr_uarts,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 share_irqs ? "en" : "dis");
2649
2650 for (i = 0; i < NR_IRQS; i++)
2651 spin_lock_init(&irq_lists[i].lock);
2652
2653 ret = uart_register_driver(&serial8250_reg);
2654 if (ret)
2655 goto out;
2656
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002657 serial8250_isa_devs = platform_device_alloc("serial8250",
2658 PLAT8250_DEV_LEGACY);
2659 if (!serial8250_isa_devs) {
2660 ret = -ENOMEM;
Russell Kingbc965a72006-01-18 09:54:29 +00002661 goto unreg_uart_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
2663
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002664 ret = platform_device_add(serial8250_isa_devs);
2665 if (ret)
2666 goto put_dev;
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
2669
Russell Kingbc965a72006-01-18 09:54:29 +00002670 ret = platform_driver_register(&serial8250_isa_driver);
2671 if (ret == 0)
2672 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Russell Kingbc965a72006-01-18 09:54:29 +00002674 platform_device_del(serial8250_isa_devs);
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002675 put_dev:
2676 platform_device_put(serial8250_isa_devs);
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002677 unreg_uart_drv:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 uart_unregister_driver(&serial8250_reg);
2679 out:
2680 return ret;
2681}
2682
2683static void __exit serial8250_exit(void)
2684{
2685 struct platform_device *isa_dev = serial8250_isa_devs;
2686
2687 /*
2688 * This tells serial8250_unregister_port() not to re-register
2689 * the ports (thereby making serial8250_isa_driver permanently
2690 * in use.)
2691 */
2692 serial8250_isa_devs = NULL;
2693
Russell King3ae5eae2005-11-09 22:32:44 +00002694 platform_driver_unregister(&serial8250_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 platform_device_unregister(isa_dev);
2696
2697 uart_unregister_driver(&serial8250_reg);
2698}
2699
2700module_init(serial8250_init);
2701module_exit(serial8250_exit);
2702
2703EXPORT_SYMBOL(serial8250_suspend_port);
2704EXPORT_SYMBOL(serial8250_resume_port);
2705
2706MODULE_LICENSE("GPL");
2707MODULE_DESCRIPTION("Generic 8250/16x50 serial driver $Revision: 1.90 $");
2708
2709module_param(share_irqs, uint, 0644);
2710MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
2711 " (unsafe)");
2712
Dave Jonesa61c2d72006-01-07 23:18:19 +00002713module_param(nr_uarts, uint, 0644);
2714MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716#ifdef CONFIG_SERIAL_8250_RSA
2717module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
2718MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
2719#endif
2720MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);