blob: c0c472ac531196e9df1c8576386e0a1613d73813 [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) {
Al Viro9e84b602006-09-23 01:39:45 +0100323 tmp = readl(up->port.membase + (UART_IIR & ~3));
324 return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
Zang Roy-r619113be91ec2006-06-30 02:29:58 -0700325 } 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
Alex Williamson40b36da2007-02-14 00:33:04 -0800367static void
368serial_out_sync(struct uart_8250_port *up, int offset, int value)
369{
370 switch (up->port.iotype) {
371 case UPIO_MEM:
372 case UPIO_MEM32:
373#ifdef CONFIG_SERIAL_8250_AU1X00
374 case UPIO_AU:
375#endif
376 serial_out(up, offset, value);
377 serial_in(up, UART_LCR); /* safe, no side-effects */
378 break;
379 default:
380 serial_out(up, offset, value);
381 }
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/*
385 * We used to support using pause I/O for certain machines. We
386 * haven't supported this for a while, but just in case it's badly
387 * needed for certain old 386 machines, I've left these #define's
388 * in....
389 */
390#define serial_inp(up, offset) serial_in(up, offset)
391#define serial_outp(up, offset, value) serial_out(up, offset, value)
392
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100393/* Uart divisor latch read */
394static inline int _serial_dl_read(struct uart_8250_port *up)
395{
396 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
397}
398
399/* Uart divisor latch write */
400static inline void _serial_dl_write(struct uart_8250_port *up, int value)
401{
402 serial_outp(up, UART_DLL, value & 0xff);
403 serial_outp(up, UART_DLM, value >> 8 & 0xff);
404}
405
406#ifdef CONFIG_SERIAL_8250_AU1X00
407/* Au1x00 haven't got a standard divisor latch */
408static int serial_dl_read(struct uart_8250_port *up)
409{
410 if (up->port.iotype == UPIO_AU)
411 return __raw_readl(up->port.membase + 0x28);
412 else
413 return _serial_dl_read(up);
414}
415
416static void serial_dl_write(struct uart_8250_port *up, int value)
417{
418 if (up->port.iotype == UPIO_AU)
419 __raw_writel(value, up->port.membase + 0x28);
420 else
421 _serial_dl_write(up, value);
422}
423#else
424#define serial_dl_read(up) _serial_dl_read(up)
425#define serial_dl_write(up, value) _serial_dl_write(up, value)
426#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428/*
429 * For the 16C950
430 */
431static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
432{
433 serial_out(up, UART_SCR, offset);
434 serial_out(up, UART_ICR, value);
435}
436
437static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
438{
439 unsigned int value;
440
441 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
442 serial_out(up, UART_SCR, offset);
443 value = serial_in(up, UART_ICR);
444 serial_icr_write(up, UART_ACR, up->acr);
445
446 return value;
447}
448
449/*
450 * FIFO support.
451 */
452static inline void serial8250_clear_fifos(struct uart_8250_port *p)
453{
454 if (p->capabilities & UART_CAP_FIFO) {
455 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
456 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
457 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
458 serial_outp(p, UART_FCR, 0);
459 }
460}
461
462/*
463 * IER sleep support. UARTs which have EFRs need the "extended
464 * capability" bit enabled. Note that on XR16C850s, we need to
465 * reset LCR to write to IER.
466 */
467static inline void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
468{
469 if (p->capabilities & UART_CAP_SLEEP) {
470 if (p->capabilities & UART_CAP_EFR) {
471 serial_outp(p, UART_LCR, 0xBF);
472 serial_outp(p, UART_EFR, UART_EFR_ECB);
473 serial_outp(p, UART_LCR, 0);
474 }
475 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
476 if (p->capabilities & UART_CAP_EFR) {
477 serial_outp(p, UART_LCR, 0xBF);
478 serial_outp(p, UART_EFR, 0);
479 serial_outp(p, UART_LCR, 0);
480 }
481 }
482}
483
484#ifdef CONFIG_SERIAL_8250_RSA
485/*
486 * Attempts to turn on the RSA FIFO. Returns zero on failure.
487 * We set the port uart clock rate if we succeed.
488 */
489static int __enable_rsa(struct uart_8250_port *up)
490{
491 unsigned char mode;
492 int result;
493
494 mode = serial_inp(up, UART_RSA_MSR);
495 result = mode & UART_RSA_MSR_FIFO;
496
497 if (!result) {
498 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
499 mode = serial_inp(up, UART_RSA_MSR);
500 result = mode & UART_RSA_MSR_FIFO;
501 }
502
503 if (result)
504 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
505
506 return result;
507}
508
509static void enable_rsa(struct uart_8250_port *up)
510{
511 if (up->port.type == PORT_RSA) {
512 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
513 spin_lock_irq(&up->port.lock);
514 __enable_rsa(up);
515 spin_unlock_irq(&up->port.lock);
516 }
517 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
518 serial_outp(up, UART_RSA_FRR, 0);
519 }
520}
521
522/*
523 * Attempts to turn off the RSA FIFO. Returns zero on failure.
524 * It is unknown why interrupts were disabled in here. However,
525 * the caller is expected to preserve this behaviour by grabbing
526 * the spinlock before calling this function.
527 */
528static void disable_rsa(struct uart_8250_port *up)
529{
530 unsigned char mode;
531 int result;
532
533 if (up->port.type == PORT_RSA &&
534 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
535 spin_lock_irq(&up->port.lock);
536
537 mode = serial_inp(up, UART_RSA_MSR);
538 result = !(mode & UART_RSA_MSR_FIFO);
539
540 if (!result) {
541 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
542 mode = serial_inp(up, UART_RSA_MSR);
543 result = !(mode & UART_RSA_MSR_FIFO);
544 }
545
546 if (result)
547 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
548 spin_unlock_irq(&up->port.lock);
549 }
550}
551#endif /* CONFIG_SERIAL_8250_RSA */
552
553/*
554 * This is a quickie test to see how big the FIFO is.
555 * It doesn't work at all the time, more's the pity.
556 */
557static int size_fifo(struct uart_8250_port *up)
558{
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100559 unsigned char old_fcr, old_mcr, old_lcr;
560 unsigned short old_dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int count;
562
563 old_lcr = serial_inp(up, UART_LCR);
564 serial_outp(up, UART_LCR, 0);
565 old_fcr = serial_inp(up, UART_FCR);
566 old_mcr = serial_inp(up, UART_MCR);
567 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
568 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
569 serial_outp(up, UART_MCR, UART_MCR_LOOP);
570 serial_outp(up, UART_LCR, UART_LCR_DLAB);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100571 old_dl = serial_dl_read(up);
572 serial_dl_write(up, 0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 serial_outp(up, UART_LCR, 0x03);
574 for (count = 0; count < 256; count++)
575 serial_outp(up, UART_TX, count);
576 mdelay(20);/* FIXME - schedule_timeout */
577 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
578 (count < 256); count++)
579 serial_inp(up, UART_RX);
580 serial_outp(up, UART_FCR, old_fcr);
581 serial_outp(up, UART_MCR, old_mcr);
582 serial_outp(up, UART_LCR, UART_LCR_DLAB);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100583 serial_dl_write(up, old_dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 serial_outp(up, UART_LCR, old_lcr);
585
586 return count;
587}
588
589/*
590 * Read UART ID using the divisor method - set DLL and DLM to zero
591 * and the revision will be in DLL and device type in DLM. We
592 * preserve the device state across this.
593 */
594static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
595{
596 unsigned char old_dll, old_dlm, old_lcr;
597 unsigned int id;
598
599 old_lcr = serial_inp(p, UART_LCR);
600 serial_outp(p, UART_LCR, UART_LCR_DLAB);
601
602 old_dll = serial_inp(p, UART_DLL);
603 old_dlm = serial_inp(p, UART_DLM);
604
605 serial_outp(p, UART_DLL, 0);
606 serial_outp(p, UART_DLM, 0);
607
608 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
609
610 serial_outp(p, UART_DLL, old_dll);
611 serial_outp(p, UART_DLM, old_dlm);
612 serial_outp(p, UART_LCR, old_lcr);
613
614 return id;
615}
616
617/*
618 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
619 * When this function is called we know it is at least a StarTech
620 * 16650 V2, but it might be one of several StarTech UARTs, or one of
621 * its clones. (We treat the broken original StarTech 16650 V1 as a
622 * 16550, and why not? Startech doesn't seem to even acknowledge its
623 * existence.)
624 *
625 * What evil have men's minds wrought...
626 */
627static void autoconfig_has_efr(struct uart_8250_port *up)
628{
629 unsigned int id1, id2, id3, rev;
630
631 /*
632 * Everything with an EFR has SLEEP
633 */
634 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
635
636 /*
637 * First we check to see if it's an Oxford Semiconductor UART.
638 *
639 * If we have to do this here because some non-National
640 * Semiconductor clone chips lock up if you try writing to the
641 * LSR register (which serial_icr_read does)
642 */
643
644 /*
645 * Check for Oxford Semiconductor 16C950.
646 *
647 * EFR [4] must be set else this test fails.
648 *
649 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
650 * claims that it's needed for 952 dual UART's (which are not
651 * recommended for new designs).
652 */
653 up->acr = 0;
654 serial_out(up, UART_LCR, 0xBF);
655 serial_out(up, UART_EFR, UART_EFR_ECB);
656 serial_out(up, UART_LCR, 0x00);
657 id1 = serial_icr_read(up, UART_ID1);
658 id2 = serial_icr_read(up, UART_ID2);
659 id3 = serial_icr_read(up, UART_ID3);
660 rev = serial_icr_read(up, UART_REV);
661
662 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
663
664 if (id1 == 0x16 && id2 == 0xC9 &&
665 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
666 up->port.type = PORT_16C950;
Russell King4ba5e352005-06-23 10:43:04 +0100667
668 /*
669 * Enable work around for the Oxford Semiconductor 952 rev B
670 * chip which causes it to seriously miscalculate baud rates
671 * when DLL is 0.
672 */
673 if (id3 == 0x52 && rev == 0x01)
674 up->bugs |= UART_BUG_QUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return;
676 }
677
678 /*
679 * We check for a XR16C850 by setting DLL and DLM to 0, and then
680 * reading back DLL and DLM. The chip type depends on the DLM
681 * value read back:
682 * 0x10 - XR16C850 and the DLL contains the chip revision.
683 * 0x12 - XR16C2850.
684 * 0x14 - XR16C854.
685 */
686 id1 = autoconfig_read_divisor_id(up);
687 DEBUG_AUTOCONF("850id=%04x ", id1);
688
689 id2 = id1 >> 8;
690 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 up->port.type = PORT_16850;
692 return;
693 }
694
695 /*
696 * It wasn't an XR16C850.
697 *
698 * We distinguish between the '654 and the '650 by counting
699 * how many bytes are in the FIFO. I'm using this for now,
700 * since that's the technique that was sent to me in the
701 * serial driver update, but I'm not convinced this works.
702 * I've had problems doing this in the past. -TYT
703 */
704 if (size_fifo(up) == 64)
705 up->port.type = PORT_16654;
706 else
707 up->port.type = PORT_16650V2;
708}
709
710/*
711 * We detected a chip without a FIFO. Only two fall into
712 * this category - the original 8250 and the 16450. The
713 * 16450 has a scratch register (accessible with LCR=0)
714 */
715static void autoconfig_8250(struct uart_8250_port *up)
716{
717 unsigned char scratch, status1, status2;
718
719 up->port.type = PORT_8250;
720
721 scratch = serial_in(up, UART_SCR);
722 serial_outp(up, UART_SCR, 0xa5);
723 status1 = serial_in(up, UART_SCR);
724 serial_outp(up, UART_SCR, 0x5a);
725 status2 = serial_in(up, UART_SCR);
726 serial_outp(up, UART_SCR, scratch);
727
728 if (status1 == 0xa5 && status2 == 0x5a)
729 up->port.type = PORT_16450;
730}
731
732static int broken_efr(struct uart_8250_port *up)
733{
734 /*
735 * Exar ST16C2550 "A2" devices incorrectly detect as
736 * having an EFR, and report an ID of 0x0201. See
737 * http://www.exar.com/info.php?pdf=dan180_oct2004.pdf
738 */
739 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
740 return 1;
741
742 return 0;
743}
744
745/*
746 * We know that the chip has FIFOs. Does it have an EFR? The
747 * EFR is located in the same register position as the IIR and
748 * we know the top two bits of the IIR are currently set. The
749 * EFR should contain zero. Try to read the EFR.
750 */
751static void autoconfig_16550a(struct uart_8250_port *up)
752{
753 unsigned char status1, status2;
754 unsigned int iersave;
755
756 up->port.type = PORT_16550A;
757 up->capabilities |= UART_CAP_FIFO;
758
759 /*
760 * Check for presence of the EFR when DLAB is set.
761 * Only ST16C650V1 UARTs pass this test.
762 */
763 serial_outp(up, UART_LCR, UART_LCR_DLAB);
764 if (serial_in(up, UART_EFR) == 0) {
765 serial_outp(up, UART_EFR, 0xA8);
766 if (serial_in(up, UART_EFR) != 0) {
767 DEBUG_AUTOCONF("EFRv1 ");
768 up->port.type = PORT_16650;
769 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
770 } else {
771 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
772 }
773 serial_outp(up, UART_EFR, 0);
774 return;
775 }
776
777 /*
778 * Maybe it requires 0xbf to be written to the LCR.
779 * (other ST16C650V2 UARTs, TI16C752A, etc)
780 */
781 serial_outp(up, UART_LCR, 0xBF);
782 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
783 DEBUG_AUTOCONF("EFRv2 ");
784 autoconfig_has_efr(up);
785 return;
786 }
787
788 /*
789 * Check for a National Semiconductor SuperIO chip.
790 * Attempt to switch to bank 2, read the value of the LOOP bit
791 * from EXCR1. Switch back to bank 0, change it in MCR. Then
792 * switch back to bank 2, read it from EXCR1 again and check
793 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 */
795 serial_outp(up, UART_LCR, 0);
796 status1 = serial_in(up, UART_MCR);
797 serial_outp(up, UART_LCR, 0xE0);
798 status2 = serial_in(up, 0x02); /* EXCR1 */
799
800 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
801 serial_outp(up, UART_LCR, 0);
802 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
803 serial_outp(up, UART_LCR, 0xE0);
804 status2 = serial_in(up, 0x02); /* EXCR1 */
805 serial_outp(up, UART_LCR, 0);
806 serial_outp(up, UART_MCR, status1);
807
808 if ((status2 ^ status1) & UART_MCR_LOOP) {
David Woodhouse857dde22005-05-21 15:52:23 +0100809 unsigned short quot;
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 serial_outp(up, UART_LCR, 0xE0);
David Woodhouse857dde22005-05-21 15:52:23 +0100812
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100813 quot = serial_dl_read(up);
David Woodhouse857dde22005-05-21 15:52:23 +0100814 quot <<= 3;
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 status1 = serial_in(up, 0x04); /* EXCR1 */
817 status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
818 status1 |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
819 serial_outp(up, 0x04, status1);
David Woodhouse857dde22005-05-21 15:52:23 +0100820
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100821 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
David Woodhouse857dde22005-05-21 15:52:23 +0100823 serial_outp(up, UART_LCR, 0);
824
825 up->port.uartclk = 921600*16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 up->port.type = PORT_NS16550A;
827 up->capabilities |= UART_NATSEMI;
828 return;
829 }
830 }
831
832 /*
833 * No EFR. Try to detect a TI16750, which only sets bit 5 of
834 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
835 * Try setting it with and without DLAB set. Cheap clones
836 * set bit 5 without DLAB set.
837 */
838 serial_outp(up, UART_LCR, 0);
839 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
840 status1 = serial_in(up, UART_IIR) >> 5;
841 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
842 serial_outp(up, UART_LCR, UART_LCR_DLAB);
843 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
844 status2 = serial_in(up, UART_IIR) >> 5;
845 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
846 serial_outp(up, UART_LCR, 0);
847
848 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
849
850 if (status1 == 6 && status2 == 7) {
851 up->port.type = PORT_16750;
852 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
853 return;
854 }
855
856 /*
857 * Try writing and reading the UART_IER_UUE bit (b6).
858 * If it works, this is probably one of the Xscale platform's
859 * internal UARTs.
860 * We're going to explicitly set the UUE bit to 0 before
861 * trying to write and read a 1 just to make sure it's not
862 * already a 1 and maybe locked there before we even start start.
863 */
864 iersave = serial_in(up, UART_IER);
865 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
866 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
867 /*
868 * OK it's in a known zero state, try writing and reading
869 * without disturbing the current state of the other bits.
870 */
871 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
872 if (serial_in(up, UART_IER) & UART_IER_UUE) {
873 /*
874 * It's an Xscale.
875 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
876 */
877 DEBUG_AUTOCONF("Xscale ");
878 up->port.type = PORT_XSCALE;
879 up->capabilities |= UART_CAP_UUE;
880 return;
881 }
882 } else {
883 /*
884 * If we got here we couldn't force the IER_UUE bit to 0.
885 * Log it and continue.
886 */
887 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
888 }
889 serial_outp(up, UART_IER, iersave);
890}
891
892/*
893 * This routine is called by rs_init() to initialize a specific serial
894 * port. It determines what type of UART chip this serial port is
895 * using: 8250, 16450, 16550, 16550A. The important question is
896 * whether or not this UART is a 16550A or not, since this will
897 * determine whether or not we can use its FIFO features or not.
898 */
899static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
900{
901 unsigned char status1, scratch, scratch2, scratch3;
902 unsigned char save_lcr, save_mcr;
903 unsigned long flags;
904
905 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
906 return;
907
908 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%p): ",
909 up->port.line, up->port.iobase, up->port.membase);
910
911 /*
912 * We really do need global IRQs disabled here - we're going to
913 * be frobbing the chips IRQ enable register to see if it exists.
914 */
915 spin_lock_irqsave(&up->port.lock, flags);
916// save_flags(flags); cli();
917
918 up->capabilities = 0;
Russell King4ba5e352005-06-23 10:43:04 +0100919 up->bugs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (!(up->port.flags & UPF_BUGGY_UART)) {
922 /*
923 * Do a simple existence test first; if we fail this,
924 * there's no point trying anything else.
925 *
926 * 0x80 is used as a nonsense port to prevent against
927 * false positives due to ISA bus float. The
928 * assumption is that 0x80 is a non-existent port;
929 * which should be safe since include/asm/io.h also
930 * makes this assumption.
931 *
932 * Note: this is safe as long as MCR bit 4 is clear
933 * and the device is in "PC" mode.
934 */
935 scratch = serial_inp(up, UART_IER);
936 serial_outp(up, UART_IER, 0);
937#ifdef __i386__
938 outb(0xff, 0x080);
939#endif
Thomas Hoehn48212002007-02-10 01:46:05 -0800940 /*
941 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
942 * 16C754B) allow only to modify them if an EFR bit is set.
943 */
944 scratch2 = serial_inp(up, UART_IER) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 serial_outp(up, UART_IER, 0x0F);
946#ifdef __i386__
947 outb(0, 0x080);
948#endif
Thomas Hoehn48212002007-02-10 01:46:05 -0800949 scratch3 = serial_inp(up, UART_IER) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 serial_outp(up, UART_IER, scratch);
951 if (scratch2 != 0 || scratch3 != 0x0F) {
952 /*
953 * We failed; there's nothing here
954 */
955 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
956 scratch2, scratch3);
957 goto out;
958 }
959 }
960
961 save_mcr = serial_in(up, UART_MCR);
962 save_lcr = serial_in(up, UART_LCR);
963
964 /*
965 * Check to see if a UART is really there. Certain broken
966 * internal modems based on the Rockwell chipset fail this
967 * test, because they apparently don't implement the loopback
968 * test mode. So this test is skipped on the COM 1 through
969 * COM 4 ports. This *should* be safe, since no board
970 * manufacturer would be stupid enough to design a board
971 * that conflicts with COM 1-4 --- we hope!
972 */
973 if (!(up->port.flags & UPF_SKIP_TEST)) {
974 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
975 status1 = serial_inp(up, UART_MSR) & 0xF0;
976 serial_outp(up, UART_MCR, save_mcr);
977 if (status1 != 0x90) {
978 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
979 status1);
980 goto out;
981 }
982 }
983
984 /*
985 * We're pretty sure there's a port here. Lets find out what
986 * type of port it is. The IIR top two bits allows us to find
Russell King6f0d6182005-09-09 16:17:58 +0100987 * out if it's 8250 or 16450, 16550, 16550A or later. This
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 * determines what we test for next.
989 *
990 * We also initialise the EFR (if any) to zero for later. The
991 * EFR occupies the same register location as the FCR and IIR.
992 */
993 serial_outp(up, UART_LCR, 0xBF);
994 serial_outp(up, UART_EFR, 0);
995 serial_outp(up, UART_LCR, 0);
996
997 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
998 scratch = serial_in(up, UART_IIR) >> 6;
999
1000 DEBUG_AUTOCONF("iir=%d ", scratch);
1001
1002 switch (scratch) {
1003 case 0:
1004 autoconfig_8250(up);
1005 break;
1006 case 1:
1007 up->port.type = PORT_UNKNOWN;
1008 break;
1009 case 2:
1010 up->port.type = PORT_16550;
1011 break;
1012 case 3:
1013 autoconfig_16550a(up);
1014 break;
1015 }
1016
1017#ifdef CONFIG_SERIAL_8250_RSA
1018 /*
1019 * Only probe for RSA ports if we got the region.
1020 */
1021 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1022 int i;
1023
1024 for (i = 0 ; i < probe_rsa_count; ++i) {
1025 if (probe_rsa[i] == up->port.iobase &&
1026 __enable_rsa(up)) {
1027 up->port.type = PORT_RSA;
1028 break;
1029 }
1030 }
1031 }
1032#endif
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001033
1034#ifdef CONFIG_SERIAL_8250_AU1X00
1035 /* if access method is AU, it is a 16550 with a quirk */
1036 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
1037 up->bugs |= UART_BUG_NOMSR;
1038#endif
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 serial_outp(up, UART_LCR, save_lcr);
1041
1042 if (up->capabilities != uart_config[up->port.type].flags) {
1043 printk(KERN_WARNING
1044 "ttyS%d: detected caps %08x should be %08x\n",
1045 up->port.line, up->capabilities,
1046 uart_config[up->port.type].flags);
1047 }
1048
1049 up->port.fifosize = uart_config[up->port.type].fifo_size;
1050 up->capabilities = uart_config[up->port.type].flags;
1051 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1052
1053 if (up->port.type == PORT_UNKNOWN)
1054 goto out;
1055
1056 /*
1057 * Reset the UART.
1058 */
1059#ifdef CONFIG_SERIAL_8250_RSA
1060 if (up->port.type == PORT_RSA)
1061 serial_outp(up, UART_RSA_FRR, 0);
1062#endif
1063 serial_outp(up, UART_MCR, save_mcr);
1064 serial8250_clear_fifos(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001065 serial_in(up, UART_RX);
Lennert Buytenhek5c8c7552005-11-12 21:58:05 +00001066 if (up->capabilities & UART_CAP_UUE)
1067 serial_outp(up, UART_IER, UART_IER_UUE);
1068 else
1069 serial_outp(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 out:
1072 spin_unlock_irqrestore(&up->port.lock, flags);
1073// restore_flags(flags);
1074 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1075}
1076
1077static void autoconfig_irq(struct uart_8250_port *up)
1078{
1079 unsigned char save_mcr, save_ier;
1080 unsigned char save_ICP = 0;
1081 unsigned int ICP = 0;
1082 unsigned long irqs;
1083 int irq;
1084
1085 if (up->port.flags & UPF_FOURPORT) {
1086 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1087 save_ICP = inb_p(ICP);
1088 outb_p(0x80, ICP);
1089 (void) inb_p(ICP);
1090 }
1091
1092 /* forget possible initially masked and pending IRQ */
1093 probe_irq_off(probe_irq_on());
1094 save_mcr = serial_inp(up, UART_MCR);
1095 save_ier = serial_inp(up, UART_IER);
1096 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
1097
1098 irqs = probe_irq_on();
1099 serial_outp(up, UART_MCR, 0);
1100 udelay (10);
1101 if (up->port.flags & UPF_FOURPORT) {
1102 serial_outp(up, UART_MCR,
1103 UART_MCR_DTR | UART_MCR_RTS);
1104 } else {
1105 serial_outp(up, UART_MCR,
1106 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1107 }
1108 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1109 (void)serial_inp(up, UART_LSR);
1110 (void)serial_inp(up, UART_RX);
1111 (void)serial_inp(up, UART_IIR);
1112 (void)serial_inp(up, UART_MSR);
1113 serial_outp(up, UART_TX, 0xFF);
1114 udelay (20);
1115 irq = probe_irq_off(irqs);
1116
1117 serial_outp(up, UART_MCR, save_mcr);
1118 serial_outp(up, UART_IER, save_ier);
1119
1120 if (up->port.flags & UPF_FOURPORT)
1121 outb_p(save_ICP, ICP);
1122
1123 up->port.irq = (irq > 0) ? irq : 0;
1124}
1125
Russell Kinge763b902005-06-29 18:41:51 +01001126static inline void __stop_tx(struct uart_8250_port *p)
1127{
1128 if (p->ier & UART_IER_THRI) {
1129 p->ier &= ~UART_IER_THRI;
1130 serial_out(p, UART_IER, p->ier);
1131 }
1132}
1133
Russell Kingb129a8c2005-08-31 10:12:14 +01001134static void serial8250_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 struct uart_8250_port *up = (struct uart_8250_port *)port;
1137
Russell Kinge763b902005-06-29 18:41:51 +01001138 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 /*
Russell Kinge763b902005-06-29 18:41:51 +01001141 * We really want to stop the transmitter from sending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 */
Russell Kinge763b902005-06-29 18:41:51 +01001143 if (up->port.type == PORT_16C950) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 up->acr |= UART_ACR_TXDIS;
1145 serial_icr_write(up, UART_ACR, up->acr);
1146 }
1147}
1148
Russell King55d3b282005-06-23 15:05:41 +01001149static void transmit_chars(struct uart_8250_port *up);
1150
Russell Kingb129a8c2005-08-31 10:12:14 +01001151static void serial8250_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 struct uart_8250_port *up = (struct uart_8250_port *)port;
1154
1155 if (!(up->ier & UART_IER_THRI)) {
1156 up->ier |= UART_IER_THRI;
1157 serial_out(up, UART_IER, up->ier);
Russell King55d3b282005-06-23 15:05:41 +01001158
Russell King67f76542005-06-23 22:26:43 +01001159 if (up->bugs & UART_BUG_TXEN) {
Russell King55d3b282005-06-23 15:05:41 +01001160 unsigned char lsr, iir;
1161 lsr = serial_in(up, UART_LSR);
1162 iir = serial_in(up, UART_IIR);
1163 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT)
1164 transmit_chars(up);
1165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
Russell Kinge763b902005-06-29 18:41:51 +01001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /*
Russell Kinge763b902005-06-29 18:41:51 +01001169 * Re-enable the transmitter if we disabled it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 */
Russell Kinge763b902005-06-29 18:41:51 +01001171 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 up->acr &= ~UART_ACR_TXDIS;
1173 serial_icr_write(up, UART_ACR, up->acr);
1174 }
1175}
1176
1177static void serial8250_stop_rx(struct uart_port *port)
1178{
1179 struct uart_8250_port *up = (struct uart_8250_port *)port;
1180
1181 up->ier &= ~UART_IER_RLSI;
1182 up->port.read_status_mask &= ~UART_LSR_DR;
1183 serial_out(up, UART_IER, up->ier);
1184}
1185
1186static void serial8250_enable_ms(struct uart_port *port)
1187{
1188 struct uart_8250_port *up = (struct uart_8250_port *)port;
1189
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001190 /* no MSR capabilities */
1191 if (up->bugs & UART_BUG_NOMSR)
1192 return;
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 up->ier |= UART_IER_MSI;
1195 serial_out(up, UART_IER, up->ier);
1196}
1197
Russell Kingea8874d2006-01-04 19:43:24 +00001198static void
Thomas Koellercc79aa92007-02-20 13:58:05 -08001199receive_chars(struct uart_8250_port *up, unsigned int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 struct tty_struct *tty = up->port.info->tty;
1202 unsigned char ch, lsr = *status;
1203 int max_count = 256;
1204 char flag;
1205
1206 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 ch = serial_inp(up, UART_RX);
1208 flag = TTY_NORMAL;
1209 up->port.icount.rx++;
1210
1211#ifdef CONFIG_SERIAL_8250_CONSOLE
1212 /*
1213 * Recover the break flag from console xmit
1214 */
1215 if (up->port.line == up->port.cons->index) {
1216 lsr |= up->lsr_break_flag;
1217 up->lsr_break_flag = 0;
1218 }
1219#endif
1220
1221 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
1222 UART_LSR_FE | UART_LSR_OE))) {
1223 /*
1224 * For statistics only
1225 */
1226 if (lsr & UART_LSR_BI) {
1227 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1228 up->port.icount.brk++;
1229 /*
1230 * We do the SysRQ and SAK checking
1231 * here because otherwise the break
1232 * may get masked by ignore_status_mask
1233 * or read_status_mask.
1234 */
1235 if (uart_handle_break(&up->port))
1236 goto ignore_char;
1237 } else if (lsr & UART_LSR_PE)
1238 up->port.icount.parity++;
1239 else if (lsr & UART_LSR_FE)
1240 up->port.icount.frame++;
1241 if (lsr & UART_LSR_OE)
1242 up->port.icount.overrun++;
1243
1244 /*
Russell King23907eb2005-04-16 15:26:39 -07001245 * Mask off conditions which should be ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 */
1247 lsr &= up->port.read_status_mask;
1248
1249 if (lsr & UART_LSR_BI) {
1250 DEBUG_INTR("handling break....");
1251 flag = TTY_BREAK;
1252 } else if (lsr & UART_LSR_PE)
1253 flag = TTY_PARITY;
1254 else if (lsr & UART_LSR_FE)
1255 flag = TTY_FRAME;
1256 }
David Howells7d12e782006-10-05 14:55:46 +01001257 if (uart_handle_sysrq_char(&up->port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 goto ignore_char;
Russell King05ab3012005-05-09 23:21:59 +01001259
1260 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 ignore_char:
1263 lsr = serial_inp(up, UART_LSR);
1264 } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
1265 spin_unlock(&up->port.lock);
1266 tty_flip_buffer_push(tty);
1267 spin_lock(&up->port.lock);
1268 *status = lsr;
1269}
1270
Russell Kingea8874d2006-01-04 19:43:24 +00001271static void transmit_chars(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 struct circ_buf *xmit = &up->port.info->xmit;
1274 int count;
1275
1276 if (up->port.x_char) {
1277 serial_outp(up, UART_TX, up->port.x_char);
1278 up->port.icount.tx++;
1279 up->port.x_char = 0;
1280 return;
1281 }
Russell Kingb129a8c2005-08-31 10:12:14 +01001282 if (uart_tx_stopped(&up->port)) {
1283 serial8250_stop_tx(&up->port);
1284 return;
1285 }
1286 if (uart_circ_empty(xmit)) {
Russell Kinge763b902005-06-29 18:41:51 +01001287 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return;
1289 }
1290
1291 count = up->tx_loadsz;
1292 do {
1293 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1294 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1295 up->port.icount.tx++;
1296 if (uart_circ_empty(xmit))
1297 break;
1298 } while (--count > 0);
1299
1300 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1301 uart_write_wakeup(&up->port);
1302
1303 DEBUG_INTR("THRE...");
1304
1305 if (uart_circ_empty(xmit))
Russell Kinge763b902005-06-29 18:41:51 +01001306 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
Russell King2af7cd62006-01-04 16:55:09 +00001309static unsigned int check_modem_status(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Russell King2af7cd62006-01-04 16:55:09 +00001311 unsigned int status = serial_in(up, UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Taku Izumifdc30b32007-04-23 14:41:00 -07001313 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1314 up->port.info != NULL) {
Russell King2af7cd62006-01-04 16:55:09 +00001315 if (status & UART_MSR_TERI)
1316 up->port.icount.rng++;
1317 if (status & UART_MSR_DDSR)
1318 up->port.icount.dsr++;
1319 if (status & UART_MSR_DDCD)
1320 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1321 if (status & UART_MSR_DCTS)
1322 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Russell King2af7cd62006-01-04 16:55:09 +00001324 wake_up_interruptible(&up->port.info->delta_msr_wait);
1325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Russell King2af7cd62006-01-04 16:55:09 +00001327 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
1330/*
1331 * This handles the interrupt from one port.
1332 */
1333static inline void
David Howells7d12e782006-10-05 14:55:46 +01001334serial8250_handle_port(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Russell King45e24602006-01-04 19:19:06 +00001336 unsigned int status;
1337
1338 spin_lock(&up->port.lock);
1339
1340 status = serial_inp(up, UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 DEBUG_INTR("status = %x...", status);
1343
1344 if (status & UART_LSR_DR)
David Howells7d12e782006-10-05 14:55:46 +01001345 receive_chars(up, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 check_modem_status(up);
1347 if (status & UART_LSR_THRE)
1348 transmit_chars(up);
Russell King45e24602006-01-04 19:19:06 +00001349
1350 spin_unlock(&up->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
1353/*
1354 * This is the serial driver's interrupt routine.
1355 *
1356 * Arjan thinks the old way was overly complex, so it got simplified.
1357 * Alan disagrees, saying that need the complexity to handle the weird
1358 * nature of ISA shared interrupts. (This is a special exception.)
1359 *
1360 * In order to handle ISA shared interrupts properly, we need to check
1361 * that all ports have been serviced, and therefore the ISA interrupt
1362 * line has been de-asserted.
1363 *
1364 * This means we need to loop through all ports. checking that they
1365 * don't have an interrupt pending.
1366 */
David Howells7d12e782006-10-05 14:55:46 +01001367static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 struct irq_info *i = dev_id;
1370 struct list_head *l, *end = NULL;
1371 int pass_counter = 0, handled = 0;
1372
1373 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1374
1375 spin_lock(&i->lock);
1376
1377 l = i->head;
1378 do {
1379 struct uart_8250_port *up;
1380 unsigned int iir;
1381
1382 up = list_entry(l, struct uart_8250_port, list);
1383
1384 iir = serial_in(up, UART_IIR);
1385 if (!(iir & UART_IIR_NO_INT)) {
David Howells7d12e782006-10-05 14:55:46 +01001386 serial8250_handle_port(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 handled = 1;
1389
1390 end = NULL;
1391 } else if (end == NULL)
1392 end = l;
1393
1394 l = l->next;
1395
1396 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1397 /* If we hit this, we're dead. */
1398 printk(KERN_ERR "serial8250: too much work for "
1399 "irq%d\n", irq);
1400 break;
1401 }
1402 } while (l != end);
1403
1404 spin_unlock(&i->lock);
1405
1406 DEBUG_INTR("end.\n");
1407
1408 return IRQ_RETVAL(handled);
1409}
1410
1411/*
1412 * To support ISA shared interrupts, we need to have one interrupt
1413 * handler that ensures that the IRQ line has been deasserted
1414 * before returning. Failing to do this will result in the IRQ
1415 * line being stuck active, and, since ISA irqs are edge triggered,
1416 * no more IRQs will be seen.
1417 */
1418static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1419{
1420 spin_lock_irq(&i->lock);
1421
1422 if (!list_empty(i->head)) {
1423 if (i->head == &up->list)
1424 i->head = i->head->next;
1425 list_del(&up->list);
1426 } else {
1427 BUG_ON(i->head != &up->list);
1428 i->head = NULL;
1429 }
1430
1431 spin_unlock_irq(&i->lock);
1432}
1433
1434static int serial_link_irq_chain(struct uart_8250_port *up)
1435{
1436 struct irq_info *i = irq_lists + up->port.irq;
Thomas Gleixner40663cc2006-07-01 19:29:43 -07001437 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 spin_lock_irq(&i->lock);
1440
1441 if (i->head) {
1442 list_add(&up->list, i->head);
1443 spin_unlock_irq(&i->lock);
1444
1445 ret = 0;
1446 } else {
1447 INIT_LIST_HEAD(&up->list);
1448 i->head = &up->list;
1449 spin_unlock_irq(&i->lock);
1450
1451 ret = request_irq(up->port.irq, serial8250_interrupt,
1452 irq_flags, "serial", i);
1453 if (ret < 0)
1454 serial_do_unlink(i, up);
1455 }
1456
1457 return ret;
1458}
1459
1460static void serial_unlink_irq_chain(struct uart_8250_port *up)
1461{
1462 struct irq_info *i = irq_lists + up->port.irq;
1463
1464 BUG_ON(i->head == NULL);
1465
1466 if (list_empty(i->head))
1467 free_irq(up->port.irq, i);
1468
1469 serial_do_unlink(i, up);
1470}
1471
Alex Williamson40b36da2007-02-14 00:33:04 -08001472/* Base timer interval for polling */
1473static inline int poll_timeout(int timeout)
1474{
1475 return timeout > 6 ? (timeout / 2 - 2) : 1;
1476}
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478/*
1479 * This function is used to handle ports that do not have an
1480 * interrupt. This doesn't work very well for 16450's, but gives
1481 * barely passable results for a 16550A. (Although at the expense
1482 * of much CPU overhead).
1483 */
1484static void serial8250_timeout(unsigned long data)
1485{
1486 struct uart_8250_port *up = (struct uart_8250_port *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 unsigned int iir;
1488
1489 iir = serial_in(up, UART_IIR);
Russell King45e24602006-01-04 19:19:06 +00001490 if (!(iir & UART_IIR_NO_INT))
David Howells7d12e782006-10-05 14:55:46 +01001491 serial8250_handle_port(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001492 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
1493}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Alex Williamson40b36da2007-02-14 00:33:04 -08001495static void serial8250_backup_timeout(unsigned long data)
1496{
1497 struct uart_8250_port *up = (struct uart_8250_port *)data;
1498 unsigned int iir, ier = 0;
1499
1500 /*
1501 * Must disable interrupts or else we risk racing with the interrupt
1502 * based handler.
1503 */
1504 if (is_real_interrupt(up->port.irq)) {
1505 ier = serial_in(up, UART_IER);
1506 serial_out(up, UART_IER, 0);
1507 }
1508
1509 iir = serial_in(up, UART_IIR);
1510
1511 /*
1512 * This should be a safe test for anyone who doesn't trust the
1513 * IIR bits on their UART, but it's specifically designed for
1514 * the "Diva" UART used on the management processor on many HP
1515 * ia64 and parisc boxes.
1516 */
1517 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
1518 (!uart_circ_empty(&up->port.info->xmit) || up->port.x_char) &&
1519 (serial_in(up, UART_LSR) & UART_LSR_THRE)) {
1520 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1521 iir |= UART_IIR_THRI;
1522 }
1523
1524 if (!(iir & UART_IIR_NO_INT))
1525 serial8250_handle_port(up);
1526
1527 if (is_real_interrupt(up->port.irq))
1528 serial_out(up, UART_IER, ier);
1529
1530 /* Standard timer interval plus 0.2s to keep the port running */
1531 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout) + HZ/5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
1534static unsigned int serial8250_tx_empty(struct uart_port *port)
1535{
1536 struct uart_8250_port *up = (struct uart_8250_port *)port;
1537 unsigned long flags;
1538 unsigned int ret;
1539
1540 spin_lock_irqsave(&up->port.lock, flags);
1541 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
1542 spin_unlock_irqrestore(&up->port.lock, flags);
1543
1544 return ret;
1545}
1546
1547static unsigned int serial8250_get_mctrl(struct uart_port *port)
1548{
1549 struct uart_8250_port *up = (struct uart_8250_port *)port;
Russell King2af7cd62006-01-04 16:55:09 +00001550 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 unsigned int ret;
1552
Russell King2af7cd62006-01-04 16:55:09 +00001553 status = check_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 ret = 0;
1556 if (status & UART_MSR_DCD)
1557 ret |= TIOCM_CAR;
1558 if (status & UART_MSR_RI)
1559 ret |= TIOCM_RNG;
1560 if (status & UART_MSR_DSR)
1561 ret |= TIOCM_DSR;
1562 if (status & UART_MSR_CTS)
1563 ret |= TIOCM_CTS;
1564 return ret;
1565}
1566
1567static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1568{
1569 struct uart_8250_port *up = (struct uart_8250_port *)port;
1570 unsigned char mcr = 0;
1571
1572 if (mctrl & TIOCM_RTS)
1573 mcr |= UART_MCR_RTS;
1574 if (mctrl & TIOCM_DTR)
1575 mcr |= UART_MCR_DTR;
1576 if (mctrl & TIOCM_OUT1)
1577 mcr |= UART_MCR_OUT1;
1578 if (mctrl & TIOCM_OUT2)
1579 mcr |= UART_MCR_OUT2;
1580 if (mctrl & TIOCM_LOOP)
1581 mcr |= UART_MCR_LOOP;
1582
1583 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1584
1585 serial_out(up, UART_MCR, mcr);
1586}
1587
1588static void serial8250_break_ctl(struct uart_port *port, int break_state)
1589{
1590 struct uart_8250_port *up = (struct uart_8250_port *)port;
1591 unsigned long flags;
1592
1593 spin_lock_irqsave(&up->port.lock, flags);
1594 if (break_state == -1)
1595 up->lcr |= UART_LCR_SBC;
1596 else
1597 up->lcr &= ~UART_LCR_SBC;
1598 serial_out(up, UART_LCR, up->lcr);
1599 spin_unlock_irqrestore(&up->port.lock, flags);
1600}
1601
Alex Williamson40b36da2007-02-14 00:33:04 -08001602#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1603
1604/*
1605 * Wait for transmitter & holding register to empty
1606 */
1607static inline void wait_for_xmitr(struct uart_8250_port *up, int bits)
1608{
1609 unsigned int status, tmout = 10000;
1610
1611 /* Wait up to 10ms for the character(s) to be sent. */
1612 do {
1613 status = serial_in(up, UART_LSR);
1614
1615 if (status & UART_LSR_BI)
1616 up->lsr_break_flag = UART_LSR_BI;
1617
1618 if (--tmout == 0)
1619 break;
1620 udelay(1);
1621 } while ((status & bits) != bits);
1622
1623 /* Wait up to 1s for flow control if necessary */
1624 if (up->port.flags & UPF_CONS_FLOW) {
1625 tmout = 1000000;
1626 while (!(serial_in(up, UART_MSR) & UART_MSR_CTS) && --tmout) {
1627 udelay(1);
1628 touch_nmi_watchdog();
1629 }
1630 }
1631}
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633static int serial8250_startup(struct uart_port *port)
1634{
1635 struct uart_8250_port *up = (struct uart_8250_port *)port;
1636 unsigned long flags;
Russell King55d3b282005-06-23 15:05:41 +01001637 unsigned char lsr, iir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 int retval;
1639
1640 up->capabilities = uart_config[up->port.type].flags;
1641 up->mcr = 0;
1642
1643 if (up->port.type == PORT_16C950) {
1644 /* Wake up and initialize UART */
1645 up->acr = 0;
1646 serial_outp(up, UART_LCR, 0xBF);
1647 serial_outp(up, UART_EFR, UART_EFR_ECB);
1648 serial_outp(up, UART_IER, 0);
1649 serial_outp(up, UART_LCR, 0);
1650 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
1651 serial_outp(up, UART_LCR, 0xBF);
1652 serial_outp(up, UART_EFR, UART_EFR_ECB);
1653 serial_outp(up, UART_LCR, 0);
1654 }
1655
1656#ifdef CONFIG_SERIAL_8250_RSA
1657 /*
1658 * If this is an RSA port, see if we can kick it up to the
1659 * higher speed clock.
1660 */
1661 enable_rsa(up);
1662#endif
1663
1664 /*
1665 * Clear the FIFO buffers and disable them.
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08001666 * (they will be reenabled in set_termios())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 */
1668 serial8250_clear_fifos(up);
1669
1670 /*
1671 * Clear the interrupt registers.
1672 */
1673 (void) serial_inp(up, UART_LSR);
1674 (void) serial_inp(up, UART_RX);
1675 (void) serial_inp(up, UART_IIR);
1676 (void) serial_inp(up, UART_MSR);
1677
1678 /*
1679 * At this point, there's no way the LSR could still be 0xff;
1680 * if it is, then bail out, because there's likely no UART
1681 * here.
1682 */
1683 if (!(up->port.flags & UPF_BUGGY_UART) &&
1684 (serial_inp(up, UART_LSR) == 0xff)) {
1685 printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
1686 return -ENODEV;
1687 }
1688
1689 /*
1690 * For a XR16C850, we need to set the trigger levels
1691 */
1692 if (up->port.type == PORT_16850) {
1693 unsigned char fctr;
1694
1695 serial_outp(up, UART_LCR, 0xbf);
1696
1697 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
1698 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
1699 serial_outp(up, UART_TRG, UART_TRG_96);
1700 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
1701 serial_outp(up, UART_TRG, UART_TRG_96);
1702
1703 serial_outp(up, UART_LCR, 0);
1704 }
1705
Alex Williamson40b36da2007-02-14 00:33:04 -08001706 if (is_real_interrupt(up->port.irq)) {
1707 /*
1708 * Test for UARTs that do not reassert THRE when the
1709 * transmitter is idle and the interrupt has already
1710 * been cleared. Real 16550s should always reassert
1711 * this interrupt whenever the transmitter is idle and
1712 * the interrupt is enabled. Delays are necessary to
1713 * allow register changes to become visible.
1714 */
1715 spin_lock_irqsave(&up->port.lock, flags);
1716
1717 wait_for_xmitr(up, UART_LSR_THRE);
1718 serial_out_sync(up, UART_IER, UART_IER_THRI);
1719 udelay(1); /* allow THRE to set */
1720 serial_in(up, UART_IIR);
1721 serial_out(up, UART_IER, 0);
1722 serial_out_sync(up, UART_IER, UART_IER_THRI);
1723 udelay(1); /* allow a working UART time to re-assert THRE */
1724 iir = serial_in(up, UART_IIR);
1725 serial_out(up, UART_IER, 0);
1726
1727 spin_unlock_irqrestore(&up->port.lock, flags);
1728
1729 /*
1730 * If the interrupt is not reasserted, setup a timer to
1731 * kick the UART on a regular basis.
1732 */
1733 if (iir & UART_IIR_NO_INT) {
1734 pr_debug("ttyS%d - using backup timer\n", port->line);
1735 up->timer.function = serial8250_backup_timeout;
1736 up->timer.data = (unsigned long)up;
1737 mod_timer(&up->timer, jiffies +
1738 poll_timeout(up->port.timeout) + HZ/5);
1739 }
1740 }
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /*
1743 * If the "interrupt" for this port doesn't correspond with any
1744 * hardware interrupt, we use a timer-based system. The original
1745 * driver used to do this with IRQ0.
1746 */
1747 if (!is_real_interrupt(up->port.irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 up->timer.data = (unsigned long)up;
Alex Williamson40b36da2007-02-14 00:33:04 -08001749 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 } else {
1751 retval = serial_link_irq_chain(up);
1752 if (retval)
1753 return retval;
1754 }
1755
1756 /*
1757 * Now, initialize the UART
1758 */
1759 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
1760
1761 spin_lock_irqsave(&up->port.lock, flags);
1762 if (up->port.flags & UPF_FOURPORT) {
1763 if (!is_real_interrupt(up->port.irq))
1764 up->port.mctrl |= TIOCM_OUT1;
1765 } else
1766 /*
1767 * Most PC uarts need OUT2 raised to enable interrupts.
1768 */
1769 if (is_real_interrupt(up->port.irq))
1770 up->port.mctrl |= TIOCM_OUT2;
1771
1772 serial8250_set_mctrl(&up->port, up->port.mctrl);
Russell King55d3b282005-06-23 15:05:41 +01001773
1774 /*
1775 * Do a quick test to see if we receive an
1776 * interrupt when we enable the TX irq.
1777 */
1778 serial_outp(up, UART_IER, UART_IER_THRI);
1779 lsr = serial_in(up, UART_LSR);
1780 iir = serial_in(up, UART_IIR);
1781 serial_outp(up, UART_IER, 0);
1782
1783 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
Russell King67f76542005-06-23 22:26:43 +01001784 if (!(up->bugs & UART_BUG_TXEN)) {
1785 up->bugs |= UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01001786 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
1787 port->line);
1788 }
1789 } else {
Russell King67f76542005-06-23 22:26:43 +01001790 up->bugs &= ~UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01001791 }
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 spin_unlock_irqrestore(&up->port.lock, flags);
1794
1795 /*
1796 * Finally, enable interrupts. Note: Modem status interrupts
1797 * are set via set_termios(), which will be occurring imminently
1798 * anyway, so we don't enable them here.
1799 */
1800 up->ier = UART_IER_RLSI | UART_IER_RDI;
1801 serial_outp(up, UART_IER, up->ier);
1802
1803 if (up->port.flags & UPF_FOURPORT) {
1804 unsigned int icp;
1805 /*
1806 * Enable interrupts on the AST Fourport board
1807 */
1808 icp = (up->port.iobase & 0xfe0) | 0x01f;
1809 outb_p(0x80, icp);
1810 (void) inb_p(icp);
1811 }
1812
1813 /*
1814 * And clear the interrupt registers again for luck.
1815 */
1816 (void) serial_inp(up, UART_LSR);
1817 (void) serial_inp(up, UART_RX);
1818 (void) serial_inp(up, UART_IIR);
1819 (void) serial_inp(up, UART_MSR);
1820
1821 return 0;
1822}
1823
1824static void serial8250_shutdown(struct uart_port *port)
1825{
1826 struct uart_8250_port *up = (struct uart_8250_port *)port;
1827 unsigned long flags;
1828
1829 /*
1830 * Disable interrupts from this port
1831 */
1832 up->ier = 0;
1833 serial_outp(up, UART_IER, 0);
1834
1835 spin_lock_irqsave(&up->port.lock, flags);
1836 if (up->port.flags & UPF_FOURPORT) {
1837 /* reset interrupts on the AST Fourport board */
1838 inb((up->port.iobase & 0xfe0) | 0x1f);
1839 up->port.mctrl |= TIOCM_OUT1;
1840 } else
1841 up->port.mctrl &= ~TIOCM_OUT2;
1842
1843 serial8250_set_mctrl(&up->port, up->port.mctrl);
1844 spin_unlock_irqrestore(&up->port.lock, flags);
1845
1846 /*
1847 * Disable break condition and FIFOs
1848 */
1849 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
1850 serial8250_clear_fifos(up);
1851
1852#ifdef CONFIG_SERIAL_8250_RSA
1853 /*
1854 * Reset the RSA board back to 115kbps compat mode.
1855 */
1856 disable_rsa(up);
1857#endif
1858
1859 /*
1860 * Read data port to reset things, and then unlink from
1861 * the IRQ chain.
1862 */
1863 (void) serial_in(up, UART_RX);
1864
Alex Williamson40b36da2007-02-14 00:33:04 -08001865 del_timer_sync(&up->timer);
1866 up->timer.function = serial8250_timeout;
1867 if (is_real_interrupt(up->port.irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 serial_unlink_irq_chain(up);
1869}
1870
1871static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
1872{
1873 unsigned int quot;
1874
1875 /*
1876 * Handle magic divisors for baud rates above baud_base on
1877 * SMSC SuperIO chips.
1878 */
1879 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
1880 baud == (port->uartclk/4))
1881 quot = 0x8001;
1882 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
1883 baud == (port->uartclk/8))
1884 quot = 0x8002;
1885 else
1886 quot = uart_get_divisor(port, baud);
1887
1888 return quot;
1889}
1890
1891static void
Alan Cox606d0992006-12-08 02:38:45 -08001892serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
1893 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 struct uart_8250_port *up = (struct uart_8250_port *)port;
1896 unsigned char cval, fcr = 0;
1897 unsigned long flags;
1898 unsigned int baud, quot;
1899
1900 switch (termios->c_cflag & CSIZE) {
1901 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +01001902 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 break;
1904 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +01001905 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 break;
1907 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +01001908 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 break;
1910 default:
1911 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +01001912 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 break;
1914 }
1915
1916 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +01001917 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (termios->c_cflag & PARENB)
1919 cval |= UART_LCR_PARITY;
1920 if (!(termios->c_cflag & PARODD))
1921 cval |= UART_LCR_EPAR;
1922#ifdef CMSPAR
1923 if (termios->c_cflag & CMSPAR)
1924 cval |= UART_LCR_SPAR;
1925#endif
1926
1927 /*
1928 * Ask the core to calculate the divisor for us.
1929 */
1930 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
1931 quot = serial8250_get_divisor(port, baud);
1932
1933 /*
Russell King4ba5e352005-06-23 10:43:04 +01001934 * Oxford Semi 952 rev B workaround
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 */
Russell King4ba5e352005-06-23 10:43:04 +01001936 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 quot ++;
1938
1939 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
1940 if (baud < 2400)
1941 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1942 else
1943 fcr = uart_config[up->port.type].fcr;
1944 }
1945
1946 /*
1947 * MCR-based auto flow control. When AFE is enabled, RTS will be
1948 * deasserted when the receive FIFO contains more characters than
1949 * the trigger, or the MCR RTS bit is cleared. In the case where
1950 * the remote UART is not using CTS auto flow control, we must
1951 * have sufficient FIFO entries for the latency of the remote
1952 * UART to respond. IOW, at least 32 bytes of FIFO.
1953 */
1954 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
1955 up->mcr &= ~UART_MCR_AFE;
1956 if (termios->c_cflag & CRTSCTS)
1957 up->mcr |= UART_MCR_AFE;
1958 }
1959
1960 /*
1961 * Ok, we're now changing the port state. Do it with
1962 * interrupts disabled.
1963 */
1964 spin_lock_irqsave(&up->port.lock, flags);
1965
1966 /*
1967 * Update the per-port timeout.
1968 */
1969 uart_update_timeout(port, termios->c_cflag, baud);
1970
1971 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1972 if (termios->c_iflag & INPCK)
1973 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1974 if (termios->c_iflag & (BRKINT | PARMRK))
1975 up->port.read_status_mask |= UART_LSR_BI;
1976
1977 /*
1978 * Characteres to ignore
1979 */
1980 up->port.ignore_status_mask = 0;
1981 if (termios->c_iflag & IGNPAR)
1982 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1983 if (termios->c_iflag & IGNBRK) {
1984 up->port.ignore_status_mask |= UART_LSR_BI;
1985 /*
1986 * If we're ignoring parity and break indicators,
1987 * ignore overruns too (for real raw support).
1988 */
1989 if (termios->c_iflag & IGNPAR)
1990 up->port.ignore_status_mask |= UART_LSR_OE;
1991 }
1992
1993 /*
1994 * ignore all characters if CREAD is not set
1995 */
1996 if ((termios->c_cflag & CREAD) == 0)
1997 up->port.ignore_status_mask |= UART_LSR_DR;
1998
1999 /*
2000 * CTS flow control flag and modem status interrupts
2001 */
2002 up->ier &= ~UART_IER_MSI;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002003 if (!(up->bugs & UART_BUG_NOMSR) &&
2004 UART_ENABLE_MS(&up->port, termios->c_cflag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 up->ier |= UART_IER_MSI;
2006 if (up->capabilities & UART_CAP_UUE)
2007 up->ier |= UART_IER_UUE | UART_IER_RTOIE;
2008
2009 serial_out(up, UART_IER, up->ier);
2010
2011 if (up->capabilities & UART_CAP_EFR) {
2012 unsigned char efr = 0;
2013 /*
2014 * TI16C752/Startech hardware flow control. FIXME:
2015 * - TI16C752 requires control thresholds to be set.
2016 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2017 */
2018 if (termios->c_cflag & CRTSCTS)
2019 efr |= UART_EFR_CTS;
2020
2021 serial_outp(up, UART_LCR, 0xBF);
2022 serial_outp(up, UART_EFR, efr);
2023 }
2024
Jonathan McDowell255341c2006-08-14 23:05:32 -07002025#ifdef CONFIG_ARCH_OMAP15XX
2026 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2027 if (cpu_is_omap1510() && is_omap_port((unsigned int)up->port.membase)) {
2028 if (baud == 115200) {
2029 quot = 1;
2030 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2031 } else
2032 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2033 }
2034#endif
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (up->capabilities & UART_NATSEMI) {
2037 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
2038 serial_outp(up, UART_LCR, 0xe0);
2039 } else {
2040 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2041 }
2042
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +01002043 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 /*
2046 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2047 * is written without DLAB set, this mode will be disabled.
2048 */
2049 if (up->port.type == PORT_16750)
2050 serial_outp(up, UART_FCR, fcr);
2051
2052 serial_outp(up, UART_LCR, cval); /* reset DLAB */
2053 up->lcr = cval; /* Save LCR */
2054 if (up->port.type != PORT_16750) {
2055 if (fcr & UART_FCR_ENABLE_FIFO) {
2056 /* emulated UARTs (Lucent Venus 167x) need two steps */
2057 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
2058 }
2059 serial_outp(up, UART_FCR, fcr); /* set fcr */
2060 }
2061 serial8250_set_mctrl(&up->port, up->port.mctrl);
2062 spin_unlock_irqrestore(&up->port.lock, flags);
2063}
2064
2065static void
2066serial8250_pm(struct uart_port *port, unsigned int state,
2067 unsigned int oldstate)
2068{
2069 struct uart_8250_port *p = (struct uart_8250_port *)port;
2070
2071 serial8250_set_sleep(p, state != 0);
2072
2073 if (p->pm)
2074 p->pm(port, state, oldstate);
2075}
2076
2077/*
2078 * Resource handling.
2079 */
2080static int serial8250_request_std_resource(struct uart_8250_port *up)
2081{
2082 unsigned int size = 8 << up->port.regshift;
2083 int ret = 0;
2084
2085 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002086 case UPIO_AU:
2087 size = 0x100000;
2088 /* fall thru */
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002089 case UPIO_TSI:
2090 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 case UPIO_MEM:
2092 if (!up->port.mapbase)
2093 break;
2094
2095 if (!request_mem_region(up->port.mapbase, size, "serial")) {
2096 ret = -EBUSY;
2097 break;
2098 }
2099
2100 if (up->port.flags & UPF_IOREMAP) {
2101 up->port.membase = ioremap(up->port.mapbase, size);
2102 if (!up->port.membase) {
2103 release_mem_region(up->port.mapbase, size);
2104 ret = -ENOMEM;
2105 }
2106 }
2107 break;
2108
2109 case UPIO_HUB6:
2110 case UPIO_PORT:
2111 if (!request_region(up->port.iobase, size, "serial"))
2112 ret = -EBUSY;
2113 break;
2114 }
2115 return ret;
2116}
2117
2118static void serial8250_release_std_resource(struct uart_8250_port *up)
2119{
2120 unsigned int size = 8 << up->port.regshift;
2121
2122 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002123 case UPIO_AU:
2124 size = 0x100000;
2125 /* fall thru */
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002126 case UPIO_TSI:
2127 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 case UPIO_MEM:
2129 if (!up->port.mapbase)
2130 break;
2131
2132 if (up->port.flags & UPF_IOREMAP) {
2133 iounmap(up->port.membase);
2134 up->port.membase = NULL;
2135 }
2136
2137 release_mem_region(up->port.mapbase, size);
2138 break;
2139
2140 case UPIO_HUB6:
2141 case UPIO_PORT:
2142 release_region(up->port.iobase, size);
2143 break;
2144 }
2145}
2146
2147static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2148{
2149 unsigned long start = UART_RSA_BASE << up->port.regshift;
2150 unsigned int size = 8 << up->port.regshift;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002151 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 case UPIO_HUB6:
2155 case UPIO_PORT:
2156 start += up->port.iobase;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002157 if (request_region(start, size, "serial-rsa"))
2158 ret = 0;
2159 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 ret = -EBUSY;
2161 break;
2162 }
2163
2164 return ret;
2165}
2166
2167static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2168{
2169 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2170 unsigned int size = 8 << up->port.regshift;
2171
2172 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 case UPIO_HUB6:
2174 case UPIO_PORT:
2175 release_region(up->port.iobase + offset, size);
2176 break;
2177 }
2178}
2179
2180static void serial8250_release_port(struct uart_port *port)
2181{
2182 struct uart_8250_port *up = (struct uart_8250_port *)port;
2183
2184 serial8250_release_std_resource(up);
2185 if (up->port.type == PORT_RSA)
2186 serial8250_release_rsa_resource(up);
2187}
2188
2189static int serial8250_request_port(struct uart_port *port)
2190{
2191 struct uart_8250_port *up = (struct uart_8250_port *)port;
2192 int ret = 0;
2193
2194 ret = serial8250_request_std_resource(up);
2195 if (ret == 0 && up->port.type == PORT_RSA) {
2196 ret = serial8250_request_rsa_resource(up);
2197 if (ret < 0)
2198 serial8250_release_std_resource(up);
2199 }
2200
2201 return ret;
2202}
2203
2204static void serial8250_config_port(struct uart_port *port, int flags)
2205{
2206 struct uart_8250_port *up = (struct uart_8250_port *)port;
2207 int probeflags = PROBE_ANY;
2208 int ret;
2209
2210 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 * Find the region that we can probe for. This in turn
2212 * tells us whether we can probe for the type of port.
2213 */
2214 ret = serial8250_request_std_resource(up);
2215 if (ret < 0)
2216 return;
2217
2218 ret = serial8250_request_rsa_resource(up);
2219 if (ret < 0)
2220 probeflags &= ~PROBE_RSA;
2221
2222 if (flags & UART_CONFIG_TYPE)
2223 autoconfig(up, probeflags);
2224 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2225 autoconfig_irq(up);
2226
2227 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2228 serial8250_release_rsa_resource(up);
2229 if (up->port.type == PORT_UNKNOWN)
2230 serial8250_release_std_resource(up);
2231}
2232
2233static int
2234serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2235{
2236 if (ser->irq >= NR_IRQS || ser->irq < 0 ||
2237 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2238 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2239 ser->type == PORT_STARTECH)
2240 return -EINVAL;
2241 return 0;
2242}
2243
2244static const char *
2245serial8250_type(struct uart_port *port)
2246{
2247 int type = port->type;
2248
2249 if (type >= ARRAY_SIZE(uart_config))
2250 type = 0;
2251 return uart_config[type].name;
2252}
2253
2254static struct uart_ops serial8250_pops = {
2255 .tx_empty = serial8250_tx_empty,
2256 .set_mctrl = serial8250_set_mctrl,
2257 .get_mctrl = serial8250_get_mctrl,
2258 .stop_tx = serial8250_stop_tx,
2259 .start_tx = serial8250_start_tx,
2260 .stop_rx = serial8250_stop_rx,
2261 .enable_ms = serial8250_enable_ms,
2262 .break_ctl = serial8250_break_ctl,
2263 .startup = serial8250_startup,
2264 .shutdown = serial8250_shutdown,
2265 .set_termios = serial8250_set_termios,
2266 .pm = serial8250_pm,
2267 .type = serial8250_type,
2268 .release_port = serial8250_release_port,
2269 .request_port = serial8250_request_port,
2270 .config_port = serial8250_config_port,
2271 .verify_port = serial8250_verify_port,
2272};
2273
2274static struct uart_8250_port serial8250_ports[UART_NR];
2275
2276static void __init serial8250_isa_init_ports(void)
2277{
2278 struct uart_8250_port *up;
2279 static int first = 1;
2280 int i;
2281
2282 if (!first)
2283 return;
2284 first = 0;
2285
Dave Jonesa61c2d72006-01-07 23:18:19 +00002286 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 struct uart_8250_port *up = &serial8250_ports[i];
2288
2289 up->port.line = i;
2290 spin_lock_init(&up->port.lock);
2291
2292 init_timer(&up->timer);
2293 up->timer.function = serial8250_timeout;
2294
2295 /*
2296 * ALPHA_KLUDGE_MCR needs to be killed.
2297 */
2298 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2299 up->mcr_force = ALPHA_KLUDGE_MCR;
2300
2301 up->port.ops = &serial8250_pops;
2302 }
2303
Russell King44454bc2005-06-30 22:41:22 +01002304 for (i = 0, up = serial8250_ports;
Dave Jonesa61c2d72006-01-07 23:18:19 +00002305 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 i++, up++) {
2307 up->port.iobase = old_serial_port[i].port;
2308 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
2309 up->port.uartclk = old_serial_port[i].baud_base * 16;
2310 up->port.flags = old_serial_port[i].flags;
2311 up->port.hub6 = old_serial_port[i].hub6;
2312 up->port.membase = old_serial_port[i].iomem_base;
2313 up->port.iotype = old_serial_port[i].io_type;
2314 up->port.regshift = old_serial_port[i].iomem_reg_shift;
2315 if (share_irqs)
2316 up->port.flags |= UPF_SHARE_IRQ;
2317 }
2318}
2319
2320static void __init
2321serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2322{
2323 int i;
2324
2325 serial8250_isa_init_ports();
2326
Dave Jonesa61c2d72006-01-07 23:18:19 +00002327 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 struct uart_8250_port *up = &serial8250_ports[i];
2329
2330 up->port.dev = dev;
2331 uart_add_one_port(drv, &up->port);
2332 }
2333}
2334
2335#ifdef CONFIG_SERIAL_8250_CONSOLE
2336
Russell Kingd3587882006-03-20 20:00:09 +00002337static void serial8250_console_putchar(struct uart_port *port, int ch)
2338{
2339 struct uart_8250_port *up = (struct uart_8250_port *)port;
2340
2341 wait_for_xmitr(up, UART_LSR_THRE);
2342 serial_out(up, UART_TX, ch);
2343}
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345/*
2346 * Print a string to the serial port trying not to disturb
2347 * any possible real use of the port...
2348 *
2349 * The console_lock must be held when we get here.
2350 */
2351static void
2352serial8250_console_write(struct console *co, const char *s, unsigned int count)
2353{
2354 struct uart_8250_port *up = &serial8250_ports[co->index];
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002355 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 unsigned int ier;
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002357 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Andrew Morton78512ec2005-11-07 00:59:13 -08002359 touch_nmi_watchdog();
2360
Andrew Morton68aa2c02006-06-30 02:29:59 -07002361 local_irq_save(flags);
2362 if (up->port.sysrq) {
2363 /* serial8250_handle_port() already took the lock */
2364 locked = 0;
2365 } else if (oops_in_progress) {
2366 locked = spin_trylock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002367 } else
Andrew Morton68aa2c02006-06-30 02:29:59 -07002368 spin_lock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002369
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 /*
Ralf Baechledc7bf132006-02-15 09:59:47 +00002371 * First save the IER then disable the interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 */
2373 ier = serial_in(up, UART_IER);
2374
2375 if (up->capabilities & UART_CAP_UUE)
2376 serial_out(up, UART_IER, UART_IER_UUE);
2377 else
2378 serial_out(up, UART_IER, 0);
2379
Russell Kingd3587882006-03-20 20:00:09 +00002380 uart_console_write(&up->port, s, count, serial8250_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 /*
2383 * Finally, wait for transmitter to become empty
2384 * and restore the IER
2385 */
Alan Coxf91a3712006-01-21 14:59:12 +00002386 wait_for_xmitr(up, BOTH_EMPTY);
Russell Kinga88d75b2006-04-30 11:30:15 +01002387 serial_out(up, UART_IER, ier);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002388
2389 if (locked)
Andrew Morton68aa2c02006-06-30 02:29:59 -07002390 spin_unlock(&up->port.lock);
2391 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393
Vivek Goyal118c0ac2007-01-11 01:52:44 +01002394static int __init serial8250_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
2396 struct uart_port *port;
2397 int baud = 9600;
2398 int bits = 8;
2399 int parity = 'n';
2400 int flow = 'n';
2401
2402 /*
2403 * Check whether an invalid uart number has been specified, and
2404 * if so, search for the first available port that does have
2405 * console support.
2406 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002407 if (co->index >= nr_uarts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 co->index = 0;
2409 port = &serial8250_ports[co->index].port;
2410 if (!port->iobase && !port->membase)
2411 return -ENODEV;
2412
2413 if (options)
2414 uart_parse_options(options, &baud, &parity, &bits, &flow);
2415
2416 return uart_set_options(port, co, baud, parity, bits, flow);
2417}
2418
2419static struct uart_driver serial8250_reg;
2420static struct console serial8250_console = {
2421 .name = "ttyS",
2422 .write = serial8250_console_write,
2423 .device = uart_console_device,
2424 .setup = serial8250_console_setup,
2425 .flags = CON_PRINTBUFFER,
2426 .index = -1,
2427 .data = &serial8250_reg,
2428};
2429
2430static int __init serial8250_console_init(void)
2431{
2432 serial8250_isa_init_ports();
2433 register_console(&serial8250_console);
2434 return 0;
2435}
2436console_initcall(serial8250_console_init);
2437
2438static int __init find_port(struct uart_port *p)
2439{
2440 int line;
2441 struct uart_port *port;
2442
Dave Jonesa61c2d72006-01-07 23:18:19 +00002443 for (line = 0; line < nr_uarts; line++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 port = &serial8250_ports[line].port;
Russell King50aec3b2006-01-04 18:13:03 +00002445 if (uart_match_port(p, port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return line;
2447 }
2448 return -ENODEV;
2449}
2450
2451int __init serial8250_start_console(struct uart_port *port, char *options)
2452{
2453 int line;
2454
2455 line = find_port(port);
2456 if (line < 0)
2457 return -ENODEV;
2458
2459 add_preferred_console("ttyS", line, options);
2460 printk("Adding console on ttyS%d at %s 0x%lx (options '%s')\n",
2461 line, port->iotype == UPIO_MEM ? "MMIO" : "I/O port",
2462 port->iotype == UPIO_MEM ? (unsigned long) port->mapbase :
2463 (unsigned long) port->iobase, options);
2464 if (!(serial8250_console.flags & CON_ENABLED)) {
2465 serial8250_console.flags &= ~CON_PRINTBUFFER;
2466 register_console(&serial8250_console);
2467 }
2468 return line;
2469}
2470
2471#define SERIAL8250_CONSOLE &serial8250_console
2472#else
2473#define SERIAL8250_CONSOLE NULL
2474#endif
2475
2476static struct uart_driver serial8250_reg = {
2477 .owner = THIS_MODULE,
2478 .driver_name = "serial",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 .dev_name = "ttyS",
2480 .major = TTY_MAJOR,
2481 .minor = 64,
2482 .nr = UART_NR,
2483 .cons = SERIAL8250_CONSOLE,
2484};
2485
Russell Kingd856c662006-02-23 10:22:13 +00002486/*
2487 * early_serial_setup - early registration for 8250 ports
2488 *
2489 * Setup an 8250 port structure prior to console initialisation. Use
2490 * after console initialisation will cause undefined behaviour.
2491 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492int __init early_serial_setup(struct uart_port *port)
2493{
2494 if (port->line >= ARRAY_SIZE(serial8250_ports))
2495 return -ENODEV;
2496
2497 serial8250_isa_init_ports();
2498 serial8250_ports[port->line].port = *port;
2499 serial8250_ports[port->line].port.ops = &serial8250_pops;
2500 return 0;
2501}
2502
2503/**
2504 * serial8250_suspend_port - suspend one serial port
2505 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 *
2507 * Suspend one serial port.
2508 */
2509void serial8250_suspend_port(int line)
2510{
2511 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2512}
2513
2514/**
2515 * serial8250_resume_port - resume one serial port
2516 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 *
2518 * Resume one serial port.
2519 */
2520void serial8250_resume_port(int line)
2521{
2522 uart_resume_port(&serial8250_reg, &serial8250_ports[line].port);
2523}
2524
2525/*
2526 * Register a set of serial devices attached to a platform device. The
2527 * list is terminated with a zero flags entry, which means we expect
2528 * all entries to have at least UPF_BOOT_AUTOCONF set.
2529 */
Russell King3ae5eae2005-11-09 22:32:44 +00002530static int __devinit serial8250_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
Russell King3ae5eae2005-11-09 22:32:44 +00002532 struct plat_serial8250_port *p = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 struct uart_port port;
Russell Kingec9f47c2005-06-27 11:12:54 +01002534 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 memset(&port, 0, sizeof(struct uart_port));
2537
Russell Kingec9f47c2005-06-27 11:12:54 +01002538 for (i = 0; p && p->flags != 0; p++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 port.iobase = p->iobase;
2540 port.membase = p->membase;
2541 port.irq = p->irq;
2542 port.uartclk = p->uartclk;
2543 port.regshift = p->regshift;
2544 port.iotype = p->iotype;
2545 port.flags = p->flags;
2546 port.mapbase = p->mapbase;
Russell Kingec9f47c2005-06-27 11:12:54 +01002547 port.hub6 = p->hub6;
Russell King3ae5eae2005-11-09 22:32:44 +00002548 port.dev = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 if (share_irqs)
2550 port.flags |= UPF_SHARE_IRQ;
Russell Kingec9f47c2005-06-27 11:12:54 +01002551 ret = serial8250_register_port(&port);
2552 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00002553 dev_err(&dev->dev, "unable to register port at index %d "
Russell Kingec9f47c2005-06-27 11:12:54 +01002554 "(IO%lx MEM%lx IRQ%d): %d\n", i,
2555 p->iobase, p->mapbase, p->irq, ret);
2556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 }
2558 return 0;
2559}
2560
2561/*
2562 * Remove serial ports registered against a platform device.
2563 */
Russell King3ae5eae2005-11-09 22:32:44 +00002564static int __devexit serial8250_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
2566 int i;
2567
Dave Jonesa61c2d72006-01-07 23:18:19 +00002568 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 struct uart_8250_port *up = &serial8250_ports[i];
2570
Russell King3ae5eae2005-11-09 22:32:44 +00002571 if (up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 serial8250_unregister_port(i);
2573 }
2574 return 0;
2575}
2576
Russell King3ae5eae2005-11-09 22:32:44 +00002577static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
2579 int i;
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 for (i = 0; i < UART_NR; i++) {
2582 struct uart_8250_port *up = &serial8250_ports[i];
2583
Russell King3ae5eae2005-11-09 22:32:44 +00002584 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 uart_suspend_port(&serial8250_reg, &up->port);
2586 }
2587
2588 return 0;
2589}
2590
Russell King3ae5eae2005-11-09 22:32:44 +00002591static int serial8250_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
2593 int i;
2594
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 for (i = 0; i < UART_NR; i++) {
2596 struct uart_8250_port *up = &serial8250_ports[i];
2597
Russell King3ae5eae2005-11-09 22:32:44 +00002598 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 uart_resume_port(&serial8250_reg, &up->port);
2600 }
2601
2602 return 0;
2603}
2604
Russell King3ae5eae2005-11-09 22:32:44 +00002605static struct platform_driver serial8250_isa_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 .probe = serial8250_probe,
2607 .remove = __devexit_p(serial8250_remove),
2608 .suspend = serial8250_suspend,
2609 .resume = serial8250_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002610 .driver = {
2611 .name = "serial8250",
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002612 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002613 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614};
2615
2616/*
2617 * This "device" covers _all_ ISA 8250-compatible serial devices listed
2618 * in the table in include/asm/serial.h
2619 */
2620static struct platform_device *serial8250_isa_devs;
2621
2622/*
2623 * serial8250_register_port and serial8250_unregister_port allows for
2624 * 16x50 serial ports to be configured at run-time, to support PCMCIA
2625 * modems and PCI multiport cards.
2626 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002627static DEFINE_MUTEX(serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
2630{
2631 int i;
2632
2633 /*
2634 * First, find a port entry which matches.
2635 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002636 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 if (uart_match_port(&serial8250_ports[i].port, port))
2638 return &serial8250_ports[i];
2639
2640 /*
2641 * We didn't find a matching entry, so look for the first
2642 * free entry. We look for one which hasn't been previously
2643 * used (indicated by zero iobase).
2644 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002645 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
2647 serial8250_ports[i].port.iobase == 0)
2648 return &serial8250_ports[i];
2649
2650 /*
2651 * That also failed. Last resort is to find any entry which
2652 * doesn't have a real port associated with it.
2653 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002654 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
2656 return &serial8250_ports[i];
2657
2658 return NULL;
2659}
2660
2661/**
2662 * serial8250_register_port - register a serial port
2663 * @port: serial port template
2664 *
2665 * Configure the serial port specified by the request. If the
2666 * port exists and is in use, it is hung up and unregistered
2667 * first.
2668 *
2669 * The port is then probed and if necessary the IRQ is autodetected
2670 * If this fails an error is returned.
2671 *
2672 * On success the port is ready to use and the line number is returned.
2673 */
2674int serial8250_register_port(struct uart_port *port)
2675{
2676 struct uart_8250_port *uart;
2677 int ret = -ENOSPC;
2678
2679 if (port->uartclk == 0)
2680 return -EINVAL;
2681
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002682 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 uart = serial8250_find_match_or_unused(port);
2685 if (uart) {
2686 uart_remove_one_port(&serial8250_reg, &uart->port);
2687
2688 uart->port.iobase = port->iobase;
2689 uart->port.membase = port->membase;
2690 uart->port.irq = port->irq;
2691 uart->port.uartclk = port->uartclk;
2692 uart->port.fifosize = port->fifosize;
2693 uart->port.regshift = port->regshift;
2694 uart->port.iotype = port->iotype;
2695 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
2696 uart->port.mapbase = port->mapbase;
2697 if (port->dev)
2698 uart->port.dev = port->dev;
2699
2700 ret = uart_add_one_port(&serial8250_reg, &uart->port);
2701 if (ret == 0)
2702 ret = uart->port.line;
2703 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002704 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706 return ret;
2707}
2708EXPORT_SYMBOL(serial8250_register_port);
2709
2710/**
2711 * serial8250_unregister_port - remove a 16x50 serial port at runtime
2712 * @line: serial line number
2713 *
2714 * Remove one serial port. This may not be called from interrupt
2715 * context. We hand the port back to the our control.
2716 */
2717void serial8250_unregister_port(int line)
2718{
2719 struct uart_8250_port *uart = &serial8250_ports[line];
2720
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002721 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 uart_remove_one_port(&serial8250_reg, &uart->port);
2723 if (serial8250_isa_devs) {
2724 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
2725 uart->port.type = PORT_UNKNOWN;
2726 uart->port.dev = &serial8250_isa_devs->dev;
2727 uart_add_one_port(&serial8250_reg, &uart->port);
2728 } else {
2729 uart->port.dev = NULL;
2730 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002731 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733EXPORT_SYMBOL(serial8250_unregister_port);
2734
2735static int __init serial8250_init(void)
2736{
2737 int ret, i;
2738
Dave Jonesa61c2d72006-01-07 23:18:19 +00002739 if (nr_uarts > UART_NR)
2740 nr_uarts = UART_NR;
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 printk(KERN_INFO "Serial: 8250/16550 driver $Revision: 1.90 $ "
Dave Jonesa61c2d72006-01-07 23:18:19 +00002743 "%d ports, IRQ sharing %sabled\n", nr_uarts,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 share_irqs ? "en" : "dis");
2745
2746 for (i = 0; i < NR_IRQS; i++)
2747 spin_lock_init(&irq_lists[i].lock);
2748
2749 ret = uart_register_driver(&serial8250_reg);
2750 if (ret)
2751 goto out;
2752
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002753 serial8250_isa_devs = platform_device_alloc("serial8250",
2754 PLAT8250_DEV_LEGACY);
2755 if (!serial8250_isa_devs) {
2756 ret = -ENOMEM;
Russell Kingbc965a72006-01-18 09:54:29 +00002757 goto unreg_uart_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
2759
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002760 ret = platform_device_add(serial8250_isa_devs);
2761 if (ret)
2762 goto put_dev;
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
2765
Russell Kingbc965a72006-01-18 09:54:29 +00002766 ret = platform_driver_register(&serial8250_isa_driver);
2767 if (ret == 0)
2768 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Russell Kingbc965a72006-01-18 09:54:29 +00002770 platform_device_del(serial8250_isa_devs);
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002771 put_dev:
2772 platform_device_put(serial8250_isa_devs);
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002773 unreg_uart_drv:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 uart_unregister_driver(&serial8250_reg);
2775 out:
2776 return ret;
2777}
2778
2779static void __exit serial8250_exit(void)
2780{
2781 struct platform_device *isa_dev = serial8250_isa_devs;
2782
2783 /*
2784 * This tells serial8250_unregister_port() not to re-register
2785 * the ports (thereby making serial8250_isa_driver permanently
2786 * in use.)
2787 */
2788 serial8250_isa_devs = NULL;
2789
Russell King3ae5eae2005-11-09 22:32:44 +00002790 platform_driver_unregister(&serial8250_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 platform_device_unregister(isa_dev);
2792
2793 uart_unregister_driver(&serial8250_reg);
2794}
2795
2796module_init(serial8250_init);
2797module_exit(serial8250_exit);
2798
2799EXPORT_SYMBOL(serial8250_suspend_port);
2800EXPORT_SYMBOL(serial8250_resume_port);
2801
2802MODULE_LICENSE("GPL");
2803MODULE_DESCRIPTION("Generic 8250/16x50 serial driver $Revision: 1.90 $");
2804
2805module_param(share_irqs, uint, 0644);
2806MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
2807 " (unsafe)");
2808
Dave Jonesa61c2d72006-01-07 23:18:19 +00002809module_param(nr_uarts, uint, 0644);
2810MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
2811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812#ifdef CONFIG_SERIAL_8250_RSA
2813module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
2814MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
2815#endif
2816MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);