blob: db2cdc103c884ef70889039740d602e785e0fbe4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/8250.c
3 *
4 * Driver for 8250/16550-type serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright (C) 2001 Russell King.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * A note about mapbase / membase
16 *
17 * mapbase is the physical address of the IO port.
18 * membase is an 'ioremapped' cookie.
19 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
22#define SUPPORT_SYSRQ
23#endif
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/ioport.h>
28#include <linux/init.h>
29#include <linux/console.h>
30#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/delay.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010032#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/serial_reg.h>
36#include <linux/serial_core.h>
37#include <linux/serial.h>
38#include <linux/serial_8250.h>
Andrew Morton78512ec2005-11-07 00:59:13 -080039#include <linux/nmi.h>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000040#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/io.h>
43#include <asm/irq.h>
44
45#include "8250.h"
46
David Millerb70ac772008-10-13 10:36:31 +010047#ifdef CONFIG_SPARC
48#include "suncore.h"
49#endif
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * Configuration:
Thomas Gleixner40663cc2006-07-01 19:29:43 -070053 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * is unsafe when used on edge-triggered interrupts.
55 */
Adrian Bunk408b6642005-05-01 08:59:29 -070056static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Dave Jonesa61c2d72006-01-07 23:18:19 +000058static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
61 * Debugging.
62 */
63#if 0
64#define DEBUG_AUTOCONF(fmt...) printk(fmt)
65#else
66#define DEBUG_AUTOCONF(fmt...) do { } while (0)
67#endif
68
69#if 0
70#define DEBUG_INTR(fmt...) printk(fmt)
71#else
72#define DEBUG_INTR(fmt...) do { } while (0)
73#endif
74
75#define PASS_LIMIT 256
76
77/*
78 * We default to IRQ0 for the "no irq" hack. Some
79 * machine types want others as well - they're free
80 * to redefine this in their header file.
81 */
82#define is_real_interrupt(irq) ((irq) != 0)
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
85#define CONFIG_SERIAL_DETECT_IRQ 1
86#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#ifdef CONFIG_SERIAL_8250_MANY_PORTS
88#define CONFIG_SERIAL_MANY_PORTS 1
89#endif
90
91/*
92 * HUB6 is always on. This will be removed once the header
93 * files have been cleaned.
94 */
95#define CONFIG_HUB6 1
96
Bryan Wua4ed1e42008-05-31 16:10:04 +080097#include <asm/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
99 * SERIAL_PORT_DFNS tells us about built-in ports that have no
100 * standard enumeration mechanism. Platforms that can find all
101 * serial ports via mechanisms like ACPI or PCI need not supply it.
102 */
103#ifndef SERIAL_PORT_DFNS
104#define SERIAL_PORT_DFNS
105#endif
106
Arjan van de Vencb3592b2005-11-28 21:04:11 +0000107static const struct old_serial_port old_serial_port[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 SERIAL_PORT_DFNS /* defined in asm/serial.h */
109};
110
Russell King026d02a2005-06-29 18:45:19 +0100111#define UART_NR CONFIG_SERIAL_8250_NR_UARTS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113#ifdef CONFIG_SERIAL_8250_RSA
114
115#define PORT_RSA_MAX 4
116static unsigned long probe_rsa[PORT_RSA_MAX];
117static unsigned int probe_rsa_count;
118#endif /* CONFIG_SERIAL_8250_RSA */
119
120struct uart_8250_port {
121 struct uart_port port;
122 struct timer_list timer; /* "no irq" timer */
123 struct list_head list; /* ports on this IRQ */
Russell King4ba5e352005-06-23 10:43:04 +0100124 unsigned short capabilities; /* port capabilities */
125 unsigned short bugs; /* port bugs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned int tx_loadsz; /* transmit fifo load size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 unsigned char acr;
128 unsigned char ier;
129 unsigned char lcr;
130 unsigned char mcr;
131 unsigned char mcr_mask; /* mask of user bits */
132 unsigned char mcr_force; /* mask of forced bits */
Corey Minyardad4c2aa2007-08-22 14:01:18 -0700133
134 /*
135 * Some bits in registers are cleared on a read, so they must
136 * be saved whenever the register is read but the bits will not
137 * be immediately processed.
138 */
139#define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITS
140 unsigned char lsr_saved_flags;
141#define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
142 unsigned char msr_saved_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /*
145 * We provide a per-port pm hook.
146 */
147 void (*pm)(struct uart_port *port,
148 unsigned int state, unsigned int old);
149};
150
151struct irq_info {
152 spinlock_t lock;
153 struct list_head *head;
154};
155
156static struct irq_info irq_lists[NR_IRQS];
157
158/*
159 * Here we define the default xmit fifo size used for each type of UART.
160 */
161static const struct serial8250_config uart_config[] = {
162 [PORT_UNKNOWN] = {
163 .name = "unknown",
164 .fifo_size = 1,
165 .tx_loadsz = 1,
166 },
167 [PORT_8250] = {
168 .name = "8250",
169 .fifo_size = 1,
170 .tx_loadsz = 1,
171 },
172 [PORT_16450] = {
173 .name = "16450",
174 .fifo_size = 1,
175 .tx_loadsz = 1,
176 },
177 [PORT_16550] = {
178 .name = "16550",
179 .fifo_size = 1,
180 .tx_loadsz = 1,
181 },
182 [PORT_16550A] = {
183 .name = "16550A",
184 .fifo_size = 16,
185 .tx_loadsz = 16,
186 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
187 .flags = UART_CAP_FIFO,
188 },
189 [PORT_CIRRUS] = {
190 .name = "Cirrus",
191 .fifo_size = 1,
192 .tx_loadsz = 1,
193 },
194 [PORT_16650] = {
195 .name = "ST16650",
196 .fifo_size = 1,
197 .tx_loadsz = 1,
198 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
199 },
200 [PORT_16650V2] = {
201 .name = "ST16650V2",
202 .fifo_size = 32,
203 .tx_loadsz = 16,
204 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
205 UART_FCR_T_TRIG_00,
206 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
207 },
208 [PORT_16750] = {
209 .name = "TI16750",
210 .fifo_size = 64,
211 .tx_loadsz = 64,
212 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
213 UART_FCR7_64BYTE,
214 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
215 },
216 [PORT_STARTECH] = {
217 .name = "Startech",
218 .fifo_size = 1,
219 .tx_loadsz = 1,
220 },
221 [PORT_16C950] = {
222 .name = "16C950/954",
223 .fifo_size = 128,
224 .tx_loadsz = 128,
225 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
226 .flags = UART_CAP_FIFO,
227 },
228 [PORT_16654] = {
229 .name = "ST16654",
230 .fifo_size = 64,
231 .tx_loadsz = 32,
232 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
233 UART_FCR_T_TRIG_10,
234 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
235 },
236 [PORT_16850] = {
237 .name = "XR16850",
238 .fifo_size = 128,
239 .tx_loadsz = 128,
240 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
241 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
242 },
243 [PORT_RSA] = {
244 .name = "RSA",
245 .fifo_size = 2048,
246 .tx_loadsz = 2048,
247 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
248 .flags = UART_CAP_FIFO,
249 },
250 [PORT_NS16550A] = {
251 .name = "NS16550A",
252 .fifo_size = 16,
253 .tx_loadsz = 16,
254 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
255 .flags = UART_CAP_FIFO | UART_NATSEMI,
256 },
257 [PORT_XSCALE] = {
258 .name = "XScale",
259 .fifo_size = 32,
260 .tx_loadsz = 32,
261 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
262 .flags = UART_CAP_FIFO | UART_CAP_UUE,
263 },
Thomas Koellerbd71c182007-05-06 14:48:47 -0700264 [PORT_RM9000] = {
265 .name = "RM9000",
266 .fifo_size = 16,
267 .tx_loadsz = 16,
268 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
269 .flags = UART_CAP_FIFO,
270 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
272
Thomas Koellerbd71c182007-05-06 14:48:47 -0700273#if defined (CONFIG_SERIAL_8250_AU1X00)
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000274
275/* Au1x00 UART hardware has a weird register layout */
276static const u8 au_io_in_map[] = {
277 [UART_RX] = 0,
278 [UART_IER] = 2,
279 [UART_IIR] = 3,
280 [UART_LCR] = 5,
281 [UART_MCR] = 6,
282 [UART_LSR] = 7,
283 [UART_MSR] = 8,
284};
285
286static const u8 au_io_out_map[] = {
287 [UART_TX] = 1,
288 [UART_IER] = 2,
289 [UART_FCR] = 4,
290 [UART_LCR] = 5,
291 [UART_MCR] = 6,
292};
293
294/* sane hardware needs no mapping */
295static inline int map_8250_in_reg(struct uart_8250_port *up, int offset)
296{
297 if (up->port.iotype != UPIO_AU)
298 return offset;
299 return au_io_in_map[offset];
300}
301
302static inline int map_8250_out_reg(struct uart_8250_port *up, int offset)
303{
304 if (up->port.iotype != UPIO_AU)
305 return offset;
306 return au_io_out_map[offset];
307}
308
Alan Cox6f803cd2008-02-08 04:18:52 -0800309#elif defined(CONFIG_SERIAL_8250_RM9K)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700310
311static const u8
312 regmap_in[8] = {
313 [UART_RX] = 0x00,
314 [UART_IER] = 0x0c,
315 [UART_IIR] = 0x14,
316 [UART_LCR] = 0x1c,
317 [UART_MCR] = 0x20,
318 [UART_LSR] = 0x24,
319 [UART_MSR] = 0x28,
320 [UART_SCR] = 0x2c
321 },
322 regmap_out[8] = {
323 [UART_TX] = 0x04,
324 [UART_IER] = 0x0c,
325 [UART_FCR] = 0x18,
326 [UART_LCR] = 0x1c,
327 [UART_MCR] = 0x20,
328 [UART_LSR] = 0x24,
329 [UART_MSR] = 0x28,
330 [UART_SCR] = 0x2c
331 };
332
333static inline int map_8250_in_reg(struct uart_8250_port *up, int offset)
334{
335 if (up->port.iotype != UPIO_RM9000)
336 return offset;
337 return regmap_in[offset];
338}
339
340static inline int map_8250_out_reg(struct uart_8250_port *up, int offset)
341{
342 if (up->port.iotype != UPIO_RM9000)
343 return offset;
344 return regmap_out[offset];
345}
346
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000347#else
348
349/* sane hardware needs no mapping */
350#define map_8250_in_reg(up, offset) (offset)
351#define map_8250_out_reg(up, offset) (offset)
352
353#endif
354
Russell Kingea8874d2006-01-04 19:43:24 +0000355static unsigned int serial_in(struct uart_8250_port *up, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Zang Roy-r619113be91ec2006-06-30 02:29:58 -0700357 unsigned int tmp;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000358 offset = map_8250_in_reg(up, offset) << up->port.regshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 switch (up->port.iotype) {
361 case UPIO_HUB6:
362 outb(up->port.hub6 - 1 + offset, up->port.iobase);
363 return inb(up->port.iobase + 1);
364
365 case UPIO_MEM:
Marc St-Jeanbeab6972007-05-06 14:48:45 -0700366 case UPIO_DWAPB:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return readb(up->port.membase + offset);
368
Thomas Koellerbd71c182007-05-06 14:48:47 -0700369 case UPIO_RM9000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 case UPIO_MEM32:
371 return readl(up->port.membase + offset);
372
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000373#ifdef CONFIG_SERIAL_8250_AU1X00
374 case UPIO_AU:
375 return __raw_readl(up->port.membase + offset);
376#endif
377
Zang Roy-r619113be91ec2006-06-30 02:29:58 -0700378 case UPIO_TSI:
379 if (offset == UART_IIR) {
Al Viro9e84b602006-09-23 01:39:45 +0100380 tmp = readl(up->port.membase + (UART_IIR & ~3));
381 return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
Zang Roy-r619113be91ec2006-06-30 02:29:58 -0700382 } else
383 return readb(up->port.membase + offset);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 default:
386 return inb(up->port.iobase + offset);
387 }
388}
389
Russell Kingea8874d2006-01-04 19:43:24 +0000390static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391serial_out(struct uart_8250_port *up, int offset, int value)
392{
Marc St-Jeanbeab6972007-05-06 14:48:45 -0700393 /* Save the offset before it's remapped */
394 int save_offset = offset;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000395 offset = map_8250_out_reg(up, offset) << up->port.regshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 switch (up->port.iotype) {
398 case UPIO_HUB6:
399 outb(up->port.hub6 - 1 + offset, up->port.iobase);
400 outb(value, up->port.iobase + 1);
401 break;
402
403 case UPIO_MEM:
404 writeb(value, up->port.membase + offset);
405 break;
406
Thomas Koellerbd71c182007-05-06 14:48:47 -0700407 case UPIO_RM9000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 case UPIO_MEM32:
409 writel(value, up->port.membase + offset);
410 break;
411
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000412#ifdef CONFIG_SERIAL_8250_AU1X00
413 case UPIO_AU:
414 __raw_writel(value, up->port.membase + offset);
415 break;
416#endif
Zang Roy-r619113be91ec2006-06-30 02:29:58 -0700417 case UPIO_TSI:
418 if (!((offset == UART_IER) && (value & UART_IER_UUE)))
419 writeb(value, up->port.membase + offset);
420 break;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +0000421
Marc St-Jeanbeab6972007-05-06 14:48:45 -0700422 case UPIO_DWAPB:
423 /* Save the LCR value so it can be re-written when a
424 * Busy Detect interrupt occurs. */
425 if (save_offset == UART_LCR)
426 up->lcr = value;
427 writeb(value, up->port.membase + offset);
428 /* Read the IER to ensure any interrupt is cleared before
429 * returning from ISR. */
430 if (save_offset == UART_TX || save_offset == UART_IER)
431 value = serial_in(up, UART_IER);
432 break;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 default:
435 outb(value, up->port.iobase + offset);
436 }
437}
438
Alex Williamson40b36da2007-02-14 00:33:04 -0800439static void
440serial_out_sync(struct uart_8250_port *up, int offset, int value)
441{
442 switch (up->port.iotype) {
443 case UPIO_MEM:
444 case UPIO_MEM32:
445#ifdef CONFIG_SERIAL_8250_AU1X00
446 case UPIO_AU:
447#endif
Marc St-Jeanbeab6972007-05-06 14:48:45 -0700448 case UPIO_DWAPB:
Alex Williamson40b36da2007-02-14 00:33:04 -0800449 serial_out(up, offset, value);
450 serial_in(up, UART_LCR); /* safe, no side-effects */
451 break;
452 default:
453 serial_out(up, offset, value);
454 }
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
458 * We used to support using pause I/O for certain machines. We
459 * haven't supported this for a while, but just in case it's badly
460 * needed for certain old 386 machines, I've left these #define's
461 * in....
462 */
463#define serial_inp(up, offset) serial_in(up, offset)
464#define serial_outp(up, offset, value) serial_out(up, offset, value)
465
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100466/* Uart divisor latch read */
467static inline int _serial_dl_read(struct uart_8250_port *up)
468{
469 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
470}
471
472/* Uart divisor latch write */
473static inline void _serial_dl_write(struct uart_8250_port *up, int value)
474{
475 serial_outp(up, UART_DLL, value & 0xff);
476 serial_outp(up, UART_DLM, value >> 8 & 0xff);
477}
478
Alan Cox6f803cd2008-02-08 04:18:52 -0800479#if defined(CONFIG_SERIAL_8250_AU1X00)
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100480/* Au1x00 haven't got a standard divisor latch */
481static int serial_dl_read(struct uart_8250_port *up)
482{
483 if (up->port.iotype == UPIO_AU)
484 return __raw_readl(up->port.membase + 0x28);
485 else
486 return _serial_dl_read(up);
487}
488
489static void serial_dl_write(struct uart_8250_port *up, int value)
490{
491 if (up->port.iotype == UPIO_AU)
492 __raw_writel(value, up->port.membase + 0x28);
493 else
494 _serial_dl_write(up, value);
495}
Alan Cox6f803cd2008-02-08 04:18:52 -0800496#elif defined(CONFIG_SERIAL_8250_RM9K)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700497static int serial_dl_read(struct uart_8250_port *up)
498{
499 return (up->port.iotype == UPIO_RM9000) ?
500 (((__raw_readl(up->port.membase + 0x10) << 8) |
501 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
502 _serial_dl_read(up);
503}
504
505static void serial_dl_write(struct uart_8250_port *up, int value)
506{
507 if (up->port.iotype == UPIO_RM9000) {
508 __raw_writel(value, up->port.membase + 0x08);
509 __raw_writel(value >> 8, up->port.membase + 0x10);
510 } else {
511 _serial_dl_write(up, value);
512 }
513}
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100514#else
515#define serial_dl_read(up) _serial_dl_read(up)
516#define serial_dl_write(up, value) _serial_dl_write(up, value)
517#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519/*
520 * For the 16C950
521 */
522static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
523{
524 serial_out(up, UART_SCR, offset);
525 serial_out(up, UART_ICR, value);
526}
527
528static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
529{
530 unsigned int value;
531
532 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
533 serial_out(up, UART_SCR, offset);
534 value = serial_in(up, UART_ICR);
535 serial_icr_write(up, UART_ACR, up->acr);
536
537 return value;
538}
539
540/*
541 * FIFO support.
542 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100543static void serial8250_clear_fifos(struct uart_8250_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 if (p->capabilities & UART_CAP_FIFO) {
546 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
547 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
548 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
549 serial_outp(p, UART_FCR, 0);
550 }
551}
552
553/*
554 * IER sleep support. UARTs which have EFRs need the "extended
555 * capability" bit enabled. Note that on XR16C850s, we need to
556 * reset LCR to write to IER.
557 */
Will Newtonb5d674a2008-10-13 10:36:21 +0100558static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 if (p->capabilities & UART_CAP_SLEEP) {
561 if (p->capabilities & UART_CAP_EFR) {
562 serial_outp(p, UART_LCR, 0xBF);
563 serial_outp(p, UART_EFR, UART_EFR_ECB);
564 serial_outp(p, UART_LCR, 0);
565 }
566 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
567 if (p->capabilities & UART_CAP_EFR) {
568 serial_outp(p, UART_LCR, 0xBF);
569 serial_outp(p, UART_EFR, 0);
570 serial_outp(p, UART_LCR, 0);
571 }
572 }
573}
574
575#ifdef CONFIG_SERIAL_8250_RSA
576/*
577 * Attempts to turn on the RSA FIFO. Returns zero on failure.
578 * We set the port uart clock rate if we succeed.
579 */
580static int __enable_rsa(struct uart_8250_port *up)
581{
582 unsigned char mode;
583 int result;
584
585 mode = serial_inp(up, UART_RSA_MSR);
586 result = mode & UART_RSA_MSR_FIFO;
587
588 if (!result) {
589 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
590 mode = serial_inp(up, UART_RSA_MSR);
591 result = mode & UART_RSA_MSR_FIFO;
592 }
593
594 if (result)
595 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
596
597 return result;
598}
599
600static void enable_rsa(struct uart_8250_port *up)
601{
602 if (up->port.type == PORT_RSA) {
603 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
604 spin_lock_irq(&up->port.lock);
605 __enable_rsa(up);
606 spin_unlock_irq(&up->port.lock);
607 }
608 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
609 serial_outp(up, UART_RSA_FRR, 0);
610 }
611}
612
613/*
614 * Attempts to turn off the RSA FIFO. Returns zero on failure.
615 * It is unknown why interrupts were disabled in here. However,
616 * the caller is expected to preserve this behaviour by grabbing
617 * the spinlock before calling this function.
618 */
619static void disable_rsa(struct uart_8250_port *up)
620{
621 unsigned char mode;
622 int result;
623
624 if (up->port.type == PORT_RSA &&
625 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
626 spin_lock_irq(&up->port.lock);
627
628 mode = serial_inp(up, UART_RSA_MSR);
629 result = !(mode & UART_RSA_MSR_FIFO);
630
631 if (!result) {
632 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
633 mode = serial_inp(up, UART_RSA_MSR);
634 result = !(mode & UART_RSA_MSR_FIFO);
635 }
636
637 if (result)
638 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
639 spin_unlock_irq(&up->port.lock);
640 }
641}
642#endif /* CONFIG_SERIAL_8250_RSA */
643
644/*
645 * This is a quickie test to see how big the FIFO is.
646 * It doesn't work at all the time, more's the pity.
647 */
648static int size_fifo(struct uart_8250_port *up)
649{
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100650 unsigned char old_fcr, old_mcr, old_lcr;
651 unsigned short old_dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 int count;
653
654 old_lcr = serial_inp(up, UART_LCR);
655 serial_outp(up, UART_LCR, 0);
656 old_fcr = serial_inp(up, UART_FCR);
657 old_mcr = serial_inp(up, UART_MCR);
658 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
659 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
660 serial_outp(up, UART_MCR, UART_MCR_LOOP);
661 serial_outp(up, UART_LCR, UART_LCR_DLAB);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100662 old_dl = serial_dl_read(up);
663 serial_dl_write(up, 0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 serial_outp(up, UART_LCR, 0x03);
665 for (count = 0; count < 256; count++)
666 serial_outp(up, UART_TX, count);
667 mdelay(20);/* FIXME - schedule_timeout */
668 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
669 (count < 256); count++)
670 serial_inp(up, UART_RX);
671 serial_outp(up, UART_FCR, old_fcr);
672 serial_outp(up, UART_MCR, old_mcr);
673 serial_outp(up, UART_LCR, UART_LCR_DLAB);
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100674 serial_dl_write(up, old_dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 serial_outp(up, UART_LCR, old_lcr);
676
677 return count;
678}
679
680/*
681 * Read UART ID using the divisor method - set DLL and DLM to zero
682 * and the revision will be in DLL and device type in DLM. We
683 * preserve the device state across this.
684 */
685static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
686{
687 unsigned char old_dll, old_dlm, old_lcr;
688 unsigned int id;
689
690 old_lcr = serial_inp(p, UART_LCR);
691 serial_outp(p, UART_LCR, UART_LCR_DLAB);
692
693 old_dll = serial_inp(p, UART_DLL);
694 old_dlm = serial_inp(p, UART_DLM);
695
696 serial_outp(p, UART_DLL, 0);
697 serial_outp(p, UART_DLM, 0);
698
699 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
700
701 serial_outp(p, UART_DLL, old_dll);
702 serial_outp(p, UART_DLM, old_dlm);
703 serial_outp(p, UART_LCR, old_lcr);
704
705 return id;
706}
707
708/*
709 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
710 * When this function is called we know it is at least a StarTech
711 * 16650 V2, but it might be one of several StarTech UARTs, or one of
712 * its clones. (We treat the broken original StarTech 16650 V1 as a
713 * 16550, and why not? Startech doesn't seem to even acknowledge its
714 * existence.)
Thomas Koellerbd71c182007-05-06 14:48:47 -0700715 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 * What evil have men's minds wrought...
717 */
718static void autoconfig_has_efr(struct uart_8250_port *up)
719{
720 unsigned int id1, id2, id3, rev;
721
722 /*
723 * Everything with an EFR has SLEEP
724 */
725 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
726
727 /*
728 * First we check to see if it's an Oxford Semiconductor UART.
729 *
730 * If we have to do this here because some non-National
731 * Semiconductor clone chips lock up if you try writing to the
732 * LSR register (which serial_icr_read does)
733 */
734
735 /*
736 * Check for Oxford Semiconductor 16C950.
737 *
738 * EFR [4] must be set else this test fails.
739 *
740 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
741 * claims that it's needed for 952 dual UART's (which are not
742 * recommended for new designs).
743 */
744 up->acr = 0;
745 serial_out(up, UART_LCR, 0xBF);
746 serial_out(up, UART_EFR, UART_EFR_ECB);
747 serial_out(up, UART_LCR, 0x00);
748 id1 = serial_icr_read(up, UART_ID1);
749 id2 = serial_icr_read(up, UART_ID2);
750 id3 = serial_icr_read(up, UART_ID3);
751 rev = serial_icr_read(up, UART_REV);
752
753 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
754
755 if (id1 == 0x16 && id2 == 0xC9 &&
756 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
757 up->port.type = PORT_16C950;
Russell King4ba5e352005-06-23 10:43:04 +0100758
759 /*
760 * Enable work around for the Oxford Semiconductor 952 rev B
761 * chip which causes it to seriously miscalculate baud rates
762 * when DLL is 0.
763 */
764 if (id3 == 0x52 && rev == 0x01)
765 up->bugs |= UART_BUG_QUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return;
767 }
Thomas Koellerbd71c182007-05-06 14:48:47 -0700768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /*
770 * We check for a XR16C850 by setting DLL and DLM to 0, and then
771 * reading back DLL and DLM. The chip type depends on the DLM
772 * value read back:
773 * 0x10 - XR16C850 and the DLL contains the chip revision.
774 * 0x12 - XR16C2850.
775 * 0x14 - XR16C854.
776 */
777 id1 = autoconfig_read_divisor_id(up);
778 DEBUG_AUTOCONF("850id=%04x ", id1);
779
780 id2 = id1 >> 8;
781 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 up->port.type = PORT_16850;
783 return;
784 }
785
786 /*
787 * It wasn't an XR16C850.
788 *
789 * We distinguish between the '654 and the '650 by counting
790 * how many bytes are in the FIFO. I'm using this for now,
791 * since that's the technique that was sent to me in the
792 * serial driver update, but I'm not convinced this works.
793 * I've had problems doing this in the past. -TYT
794 */
795 if (size_fifo(up) == 64)
796 up->port.type = PORT_16654;
797 else
798 up->port.type = PORT_16650V2;
799}
800
801/*
802 * We detected a chip without a FIFO. Only two fall into
803 * this category - the original 8250 and the 16450. The
804 * 16450 has a scratch register (accessible with LCR=0)
805 */
806static void autoconfig_8250(struct uart_8250_port *up)
807{
808 unsigned char scratch, status1, status2;
809
810 up->port.type = PORT_8250;
811
812 scratch = serial_in(up, UART_SCR);
813 serial_outp(up, UART_SCR, 0xa5);
814 status1 = serial_in(up, UART_SCR);
815 serial_outp(up, UART_SCR, 0x5a);
816 status2 = serial_in(up, UART_SCR);
817 serial_outp(up, UART_SCR, scratch);
818
819 if (status1 == 0xa5 && status2 == 0x5a)
820 up->port.type = PORT_16450;
821}
822
823static int broken_efr(struct uart_8250_port *up)
824{
825 /*
826 * Exar ST16C2550 "A2" devices incorrectly detect as
827 * having an EFR, and report an ID of 0x0201. See
828 * http://www.exar.com/info.php?pdf=dan180_oct2004.pdf
829 */
830 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
831 return 1;
832
833 return 0;
834}
835
836/*
837 * We know that the chip has FIFOs. Does it have an EFR? The
838 * EFR is located in the same register position as the IIR and
839 * we know the top two bits of the IIR are currently set. The
840 * EFR should contain zero. Try to read the EFR.
841 */
842static void autoconfig_16550a(struct uart_8250_port *up)
843{
844 unsigned char status1, status2;
845 unsigned int iersave;
846
847 up->port.type = PORT_16550A;
848 up->capabilities |= UART_CAP_FIFO;
849
850 /*
851 * Check for presence of the EFR when DLAB is set.
852 * Only ST16C650V1 UARTs pass this test.
853 */
854 serial_outp(up, UART_LCR, UART_LCR_DLAB);
855 if (serial_in(up, UART_EFR) == 0) {
856 serial_outp(up, UART_EFR, 0xA8);
857 if (serial_in(up, UART_EFR) != 0) {
858 DEBUG_AUTOCONF("EFRv1 ");
859 up->port.type = PORT_16650;
860 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
861 } else {
862 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
863 }
864 serial_outp(up, UART_EFR, 0);
865 return;
866 }
867
868 /*
869 * Maybe it requires 0xbf to be written to the LCR.
870 * (other ST16C650V2 UARTs, TI16C752A, etc)
871 */
872 serial_outp(up, UART_LCR, 0xBF);
873 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
874 DEBUG_AUTOCONF("EFRv2 ");
875 autoconfig_has_efr(up);
876 return;
877 }
878
879 /*
880 * Check for a National Semiconductor SuperIO chip.
881 * Attempt to switch to bank 2, read the value of the LOOP bit
882 * from EXCR1. Switch back to bank 0, change it in MCR. Then
883 * switch back to bank 2, read it from EXCR1 again and check
884 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 */
886 serial_outp(up, UART_LCR, 0);
887 status1 = serial_in(up, UART_MCR);
888 serial_outp(up, UART_LCR, 0xE0);
889 status2 = serial_in(up, 0x02); /* EXCR1 */
890
891 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
892 serial_outp(up, UART_LCR, 0);
893 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
894 serial_outp(up, UART_LCR, 0xE0);
895 status2 = serial_in(up, 0x02); /* EXCR1 */
896 serial_outp(up, UART_LCR, 0);
897 serial_outp(up, UART_MCR, status1);
898
899 if ((status2 ^ status1) & UART_MCR_LOOP) {
David Woodhouse857dde22005-05-21 15:52:23 +0100900 unsigned short quot;
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 serial_outp(up, UART_LCR, 0xE0);
David Woodhouse857dde22005-05-21 15:52:23 +0100903
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100904 quot = serial_dl_read(up);
David Woodhouse857dde22005-05-21 15:52:23 +0100905 quot <<= 3;
906
David Woodhouseb5b82df2007-05-17 14:27:39 +0800907 status1 = serial_in(up, 0x04); /* EXCR2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
909 status1 |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
910 serial_outp(up, 0x04, status1);
Thomas Koellerbd71c182007-05-06 14:48:47 -0700911
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +0100912 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
David Woodhouse857dde22005-05-21 15:52:23 +0100914 serial_outp(up, UART_LCR, 0);
915
916 up->port.uartclk = 921600*16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 up->port.type = PORT_NS16550A;
918 up->capabilities |= UART_NATSEMI;
919 return;
920 }
921 }
922
923 /*
924 * No EFR. Try to detect a TI16750, which only sets bit 5 of
925 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
926 * Try setting it with and without DLAB set. Cheap clones
927 * set bit 5 without DLAB set.
928 */
929 serial_outp(up, UART_LCR, 0);
930 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
931 status1 = serial_in(up, UART_IIR) >> 5;
932 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
933 serial_outp(up, UART_LCR, UART_LCR_DLAB);
934 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
935 status2 = serial_in(up, UART_IIR) >> 5;
936 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
937 serial_outp(up, UART_LCR, 0);
938
939 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
940
941 if (status1 == 6 && status2 == 7) {
942 up->port.type = PORT_16750;
943 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
944 return;
945 }
946
947 /*
948 * Try writing and reading the UART_IER_UUE bit (b6).
949 * If it works, this is probably one of the Xscale platform's
950 * internal UARTs.
951 * We're going to explicitly set the UUE bit to 0 before
952 * trying to write and read a 1 just to make sure it's not
953 * already a 1 and maybe locked there before we even start start.
954 */
955 iersave = serial_in(up, UART_IER);
956 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
957 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
958 /*
959 * OK it's in a known zero state, try writing and reading
960 * without disturbing the current state of the other bits.
961 */
962 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
963 if (serial_in(up, UART_IER) & UART_IER_UUE) {
964 /*
965 * It's an Xscale.
966 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
967 */
968 DEBUG_AUTOCONF("Xscale ");
969 up->port.type = PORT_XSCALE;
970 up->capabilities |= UART_CAP_UUE;
971 return;
972 }
973 } else {
974 /*
975 * If we got here we couldn't force the IER_UUE bit to 0.
976 * Log it and continue.
977 */
978 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
979 }
980 serial_outp(up, UART_IER, iersave);
981}
982
983/*
984 * This routine is called by rs_init() to initialize a specific serial
985 * port. It determines what type of UART chip this serial port is
986 * using: 8250, 16450, 16550, 16550A. The important question is
987 * whether or not this UART is a 16550A or not, since this will
988 * determine whether or not we can use its FIFO features or not.
989 */
990static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
991{
992 unsigned char status1, scratch, scratch2, scratch3;
993 unsigned char save_lcr, save_mcr;
994 unsigned long flags;
995
996 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
997 return;
998
999 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%p): ",
1000 up->port.line, up->port.iobase, up->port.membase);
1001
1002 /*
1003 * We really do need global IRQs disabled here - we're going to
1004 * be frobbing the chips IRQ enable register to see if it exists.
1005 */
1006 spin_lock_irqsave(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 up->capabilities = 0;
Russell King4ba5e352005-06-23 10:43:04 +01001009 up->bugs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 if (!(up->port.flags & UPF_BUGGY_UART)) {
1012 /*
1013 * Do a simple existence test first; if we fail this,
1014 * there's no point trying anything else.
Thomas Koellerbd71c182007-05-06 14:48:47 -07001015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 * 0x80 is used as a nonsense port to prevent against
1017 * false positives due to ISA bus float. The
1018 * assumption is that 0x80 is a non-existent port;
1019 * which should be safe since include/asm/io.h also
1020 * makes this assumption.
1021 *
1022 * Note: this is safe as long as MCR bit 4 is clear
1023 * and the device is in "PC" mode.
1024 */
1025 scratch = serial_inp(up, UART_IER);
1026 serial_outp(up, UART_IER, 0);
1027#ifdef __i386__
1028 outb(0xff, 0x080);
1029#endif
Thomas Hoehn48212002007-02-10 01:46:05 -08001030 /*
1031 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1032 * 16C754B) allow only to modify them if an EFR bit is set.
1033 */
1034 scratch2 = serial_inp(up, UART_IER) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 serial_outp(up, UART_IER, 0x0F);
1036#ifdef __i386__
1037 outb(0, 0x080);
1038#endif
Thomas Hoehn48212002007-02-10 01:46:05 -08001039 scratch3 = serial_inp(up, UART_IER) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 serial_outp(up, UART_IER, scratch);
1041 if (scratch2 != 0 || scratch3 != 0x0F) {
1042 /*
1043 * We failed; there's nothing here
1044 */
1045 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1046 scratch2, scratch3);
1047 goto out;
1048 }
1049 }
1050
1051 save_mcr = serial_in(up, UART_MCR);
1052 save_lcr = serial_in(up, UART_LCR);
1053
Thomas Koellerbd71c182007-05-06 14:48:47 -07001054 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 * Check to see if a UART is really there. Certain broken
1056 * internal modems based on the Rockwell chipset fail this
1057 * test, because they apparently don't implement the loopback
1058 * test mode. So this test is skipped on the COM 1 through
1059 * COM 4 ports. This *should* be safe, since no board
1060 * manufacturer would be stupid enough to design a board
1061 * that conflicts with COM 1-4 --- we hope!
1062 */
1063 if (!(up->port.flags & UPF_SKIP_TEST)) {
1064 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1065 status1 = serial_inp(up, UART_MSR) & 0xF0;
1066 serial_outp(up, UART_MCR, save_mcr);
1067 if (status1 != 0x90) {
1068 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1069 status1);
1070 goto out;
1071 }
1072 }
1073
1074 /*
1075 * We're pretty sure there's a port here. Lets find out what
1076 * type of port it is. The IIR top two bits allows us to find
Russell King6f0d6182005-09-09 16:17:58 +01001077 * out if it's 8250 or 16450, 16550, 16550A or later. This
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 * determines what we test for next.
1079 *
1080 * We also initialise the EFR (if any) to zero for later. The
1081 * EFR occupies the same register location as the FCR and IIR.
1082 */
1083 serial_outp(up, UART_LCR, 0xBF);
1084 serial_outp(up, UART_EFR, 0);
1085 serial_outp(up, UART_LCR, 0);
1086
1087 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1088 scratch = serial_in(up, UART_IIR) >> 6;
1089
1090 DEBUG_AUTOCONF("iir=%d ", scratch);
1091
1092 switch (scratch) {
1093 case 0:
1094 autoconfig_8250(up);
1095 break;
1096 case 1:
1097 up->port.type = PORT_UNKNOWN;
1098 break;
1099 case 2:
1100 up->port.type = PORT_16550;
1101 break;
1102 case 3:
1103 autoconfig_16550a(up);
1104 break;
1105 }
1106
1107#ifdef CONFIG_SERIAL_8250_RSA
1108 /*
1109 * Only probe for RSA ports if we got the region.
1110 */
1111 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1112 int i;
1113
1114 for (i = 0 ; i < probe_rsa_count; ++i) {
1115 if (probe_rsa[i] == up->port.iobase &&
1116 __enable_rsa(up)) {
1117 up->port.type = PORT_RSA;
1118 break;
1119 }
1120 }
1121 }
1122#endif
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001123
1124#ifdef CONFIG_SERIAL_8250_AU1X00
1125 /* if access method is AU, it is a 16550 with a quirk */
1126 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
1127 up->bugs |= UART_BUG_NOMSR;
1128#endif
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 serial_outp(up, UART_LCR, save_lcr);
1131
1132 if (up->capabilities != uart_config[up->port.type].flags) {
1133 printk(KERN_WARNING
1134 "ttyS%d: detected caps %08x should be %08x\n",
1135 up->port.line, up->capabilities,
1136 uart_config[up->port.type].flags);
1137 }
1138
1139 up->port.fifosize = uart_config[up->port.type].fifo_size;
1140 up->capabilities = uart_config[up->port.type].flags;
1141 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1142
1143 if (up->port.type == PORT_UNKNOWN)
1144 goto out;
1145
1146 /*
1147 * Reset the UART.
1148 */
1149#ifdef CONFIG_SERIAL_8250_RSA
1150 if (up->port.type == PORT_RSA)
1151 serial_outp(up, UART_RSA_FRR, 0);
1152#endif
1153 serial_outp(up, UART_MCR, save_mcr);
1154 serial8250_clear_fifos(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001155 serial_in(up, UART_RX);
Lennert Buytenhek5c8c7552005-11-12 21:58:05 +00001156 if (up->capabilities & UART_CAP_UUE)
1157 serial_outp(up, UART_IER, UART_IER_UUE);
1158 else
1159 serial_outp(up, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Thomas Koellerbd71c182007-05-06 14:48:47 -07001161 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 spin_unlock_irqrestore(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1164}
1165
1166static void autoconfig_irq(struct uart_8250_port *up)
1167{
1168 unsigned char save_mcr, save_ier;
1169 unsigned char save_ICP = 0;
1170 unsigned int ICP = 0;
1171 unsigned long irqs;
1172 int irq;
1173
1174 if (up->port.flags & UPF_FOURPORT) {
1175 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1176 save_ICP = inb_p(ICP);
1177 outb_p(0x80, ICP);
1178 (void) inb_p(ICP);
1179 }
1180
1181 /* forget possible initially masked and pending IRQ */
1182 probe_irq_off(probe_irq_on());
1183 save_mcr = serial_inp(up, UART_MCR);
1184 save_ier = serial_inp(up, UART_IER);
1185 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
Thomas Koellerbd71c182007-05-06 14:48:47 -07001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 irqs = probe_irq_on();
1188 serial_outp(up, UART_MCR, 0);
Alan Cox6f803cd2008-02-08 04:18:52 -08001189 udelay(10);
1190 if (up->port.flags & UPF_FOURPORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 serial_outp(up, UART_MCR,
1192 UART_MCR_DTR | UART_MCR_RTS);
1193 } else {
1194 serial_outp(up, UART_MCR,
1195 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1196 }
1197 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1198 (void)serial_inp(up, UART_LSR);
1199 (void)serial_inp(up, UART_RX);
1200 (void)serial_inp(up, UART_IIR);
1201 (void)serial_inp(up, UART_MSR);
1202 serial_outp(up, UART_TX, 0xFF);
Alan Cox6f803cd2008-02-08 04:18:52 -08001203 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 irq = probe_irq_off(irqs);
1205
1206 serial_outp(up, UART_MCR, save_mcr);
1207 serial_outp(up, UART_IER, save_ier);
1208
1209 if (up->port.flags & UPF_FOURPORT)
1210 outb_p(save_ICP, ICP);
1211
1212 up->port.irq = (irq > 0) ? irq : 0;
1213}
1214
Russell Kinge763b902005-06-29 18:41:51 +01001215static inline void __stop_tx(struct uart_8250_port *p)
1216{
1217 if (p->ier & UART_IER_THRI) {
1218 p->ier &= ~UART_IER_THRI;
1219 serial_out(p, UART_IER, p->ier);
1220 }
1221}
1222
Russell Kingb129a8c2005-08-31 10:12:14 +01001223static void serial8250_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 struct uart_8250_port *up = (struct uart_8250_port *)port;
1226
Russell Kinge763b902005-06-29 18:41:51 +01001227 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 /*
Russell Kinge763b902005-06-29 18:41:51 +01001230 * We really want to stop the transmitter from sending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
Russell Kinge763b902005-06-29 18:41:51 +01001232 if (up->port.type == PORT_16C950) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 up->acr |= UART_ACR_TXDIS;
1234 serial_icr_write(up, UART_ACR, up->acr);
1235 }
1236}
1237
Russell King55d3b282005-06-23 15:05:41 +01001238static void transmit_chars(struct uart_8250_port *up);
1239
Russell Kingb129a8c2005-08-31 10:12:14 +01001240static void serial8250_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 struct uart_8250_port *up = (struct uart_8250_port *)port;
1243
1244 if (!(up->ier & UART_IER_THRI)) {
1245 up->ier |= UART_IER_THRI;
1246 serial_out(up, UART_IER, up->ier);
Russell King55d3b282005-06-23 15:05:41 +01001247
Russell King67f76542005-06-23 22:26:43 +01001248 if (up->bugs & UART_BUG_TXEN) {
Russell King55d3b282005-06-23 15:05:41 +01001249 unsigned char lsr, iir;
1250 lsr = serial_in(up, UART_LSR);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001251 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Thomas Koellerbd71c182007-05-06 14:48:47 -07001252 iir = serial_in(up, UART_IIR) & 0x0f;
1253 if ((up->port.type == PORT_RM9000) ?
1254 (lsr & UART_LSR_THRE &&
1255 (iir == UART_IIR_NO_INT || iir == UART_IIR_THRI)) :
1256 (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT))
Russell King55d3b282005-06-23 15:05:41 +01001257 transmit_chars(up);
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Russell Kinge763b902005-06-29 18:41:51 +01001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 /*
Russell Kinge763b902005-06-29 18:41:51 +01001262 * Re-enable the transmitter if we disabled it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 */
Russell Kinge763b902005-06-29 18:41:51 +01001264 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 up->acr &= ~UART_ACR_TXDIS;
1266 serial_icr_write(up, UART_ACR, up->acr);
1267 }
1268}
1269
1270static void serial8250_stop_rx(struct uart_port *port)
1271{
1272 struct uart_8250_port *up = (struct uart_8250_port *)port;
1273
1274 up->ier &= ~UART_IER_RLSI;
1275 up->port.read_status_mask &= ~UART_LSR_DR;
1276 serial_out(up, UART_IER, up->ier);
1277}
1278
1279static void serial8250_enable_ms(struct uart_port *port)
1280{
1281 struct uart_8250_port *up = (struct uart_8250_port *)port;
1282
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00001283 /* no MSR capabilities */
1284 if (up->bugs & UART_BUG_NOMSR)
1285 return;
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 up->ier |= UART_IER_MSI;
1288 serial_out(up, UART_IER, up->ier);
1289}
1290
Russell Kingea8874d2006-01-04 19:43:24 +00001291static void
Thomas Koellercc79aa92007-02-20 13:58:05 -08001292receive_chars(struct uart_8250_port *up, unsigned int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001294 struct tty_struct *tty = up->port.info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 unsigned char ch, lsr = *status;
1296 int max_count = 256;
1297 char flag;
1298
1299 do {
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001300 if (likely(lsr & UART_LSR_DR))
1301 ch = serial_inp(up, UART_RX);
1302 else
1303 /*
1304 * Intel 82571 has a Serial Over Lan device that will
1305 * set UART_LSR_BI without setting UART_LSR_DR when
1306 * it receives a break. To avoid reading from the
1307 * receive buffer without UART_LSR_DR bit set, we
1308 * just force the read character to be 0
1309 */
1310 ch = 0;
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 flag = TTY_NORMAL;
1313 up->port.icount.rx++;
1314
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001315 lsr |= up->lsr_saved_flags;
1316 up->lsr_saved_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001318 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /*
1320 * For statistics only
1321 */
1322 if (lsr & UART_LSR_BI) {
1323 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1324 up->port.icount.brk++;
1325 /*
1326 * We do the SysRQ and SAK checking
1327 * here because otherwise the break
1328 * may get masked by ignore_status_mask
1329 * or read_status_mask.
1330 */
1331 if (uart_handle_break(&up->port))
1332 goto ignore_char;
1333 } else if (lsr & UART_LSR_PE)
1334 up->port.icount.parity++;
1335 else if (lsr & UART_LSR_FE)
1336 up->port.icount.frame++;
1337 if (lsr & UART_LSR_OE)
1338 up->port.icount.overrun++;
1339
1340 /*
Russell King23907eb2005-04-16 15:26:39 -07001341 * Mask off conditions which should be ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 */
1343 lsr &= up->port.read_status_mask;
1344
1345 if (lsr & UART_LSR_BI) {
1346 DEBUG_INTR("handling break....");
1347 flag = TTY_BREAK;
1348 } else if (lsr & UART_LSR_PE)
1349 flag = TTY_PARITY;
1350 else if (lsr & UART_LSR_FE)
1351 flag = TTY_FRAME;
1352 }
David Howells7d12e782006-10-05 14:55:46 +01001353 if (uart_handle_sysrq_char(&up->port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 goto ignore_char;
Russell King05ab3012005-05-09 23:21:59 +01001355
1356 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1357
Alan Cox6f803cd2008-02-08 04:18:52 -08001358ignore_char:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 lsr = serial_inp(up, UART_LSR);
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001360 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 spin_unlock(&up->port.lock);
1362 tty_flip_buffer_push(tty);
1363 spin_lock(&up->port.lock);
1364 *status = lsr;
1365}
1366
Russell Kingea8874d2006-01-04 19:43:24 +00001367static void transmit_chars(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 struct circ_buf *xmit = &up->port.info->xmit;
1370 int count;
1371
1372 if (up->port.x_char) {
1373 serial_outp(up, UART_TX, up->port.x_char);
1374 up->port.icount.tx++;
1375 up->port.x_char = 0;
1376 return;
1377 }
Russell Kingb129a8c2005-08-31 10:12:14 +01001378 if (uart_tx_stopped(&up->port)) {
1379 serial8250_stop_tx(&up->port);
1380 return;
1381 }
1382 if (uart_circ_empty(xmit)) {
Russell Kinge763b902005-06-29 18:41:51 +01001383 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return;
1385 }
1386
1387 count = up->tx_loadsz;
1388 do {
1389 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1390 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1391 up->port.icount.tx++;
1392 if (uart_circ_empty(xmit))
1393 break;
1394 } while (--count > 0);
1395
1396 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1397 uart_write_wakeup(&up->port);
1398
1399 DEBUG_INTR("THRE...");
1400
1401 if (uart_circ_empty(xmit))
Russell Kinge763b902005-06-29 18:41:51 +01001402 __stop_tx(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
Russell King2af7cd62006-01-04 16:55:09 +00001405static unsigned int check_modem_status(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Russell King2af7cd62006-01-04 16:55:09 +00001407 unsigned int status = serial_in(up, UART_MSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001409 status |= up->msr_saved_flags;
1410 up->msr_saved_flags = 0;
Taku Izumifdc30b32007-04-23 14:41:00 -07001411 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1412 up->port.info != NULL) {
Russell King2af7cd62006-01-04 16:55:09 +00001413 if (status & UART_MSR_TERI)
1414 up->port.icount.rng++;
1415 if (status & UART_MSR_DDSR)
1416 up->port.icount.dsr++;
1417 if (status & UART_MSR_DDCD)
1418 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1419 if (status & UART_MSR_DCTS)
1420 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Russell King2af7cd62006-01-04 16:55:09 +00001422 wake_up_interruptible(&up->port.info->delta_msr_wait);
1423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Russell King2af7cd62006-01-04 16:55:09 +00001425 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
1428/*
1429 * This handles the interrupt from one port.
1430 */
Will Newtonb5d674a2008-10-13 10:36:21 +01001431static void serial8250_handle_port(struct uart_8250_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Russell King45e24602006-01-04 19:19:06 +00001433 unsigned int status;
Jiri Kosina4bf36312007-04-23 14:41:21 -07001434 unsigned long flags;
Russell King45e24602006-01-04 19:19:06 +00001435
Jiri Kosina4bf36312007-04-23 14:41:21 -07001436 spin_lock_irqsave(&up->port.lock, flags);
Russell King45e24602006-01-04 19:19:06 +00001437
1438 status = serial_inp(up, UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 DEBUG_INTR("status = %x...", status);
1441
Aristeu Rozanski7500b1f2008-07-23 21:29:45 -07001442 if (status & (UART_LSR_DR | UART_LSR_BI))
David Howells7d12e782006-10-05 14:55:46 +01001443 receive_chars(up, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 check_modem_status(up);
1445 if (status & UART_LSR_THRE)
1446 transmit_chars(up);
Russell King45e24602006-01-04 19:19:06 +00001447
Jiri Kosina4bf36312007-04-23 14:41:21 -07001448 spin_unlock_irqrestore(&up->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
1451/*
1452 * This is the serial driver's interrupt routine.
1453 *
1454 * Arjan thinks the old way was overly complex, so it got simplified.
1455 * Alan disagrees, saying that need the complexity to handle the weird
1456 * nature of ISA shared interrupts. (This is a special exception.)
1457 *
1458 * In order to handle ISA shared interrupts properly, we need to check
1459 * that all ports have been serviced, and therefore the ISA interrupt
1460 * line has been de-asserted.
1461 *
1462 * This means we need to loop through all ports. checking that they
1463 * don't have an interrupt pending.
1464 */
David Howells7d12e782006-10-05 14:55:46 +01001465static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 struct irq_info *i = dev_id;
1468 struct list_head *l, *end = NULL;
1469 int pass_counter = 0, handled = 0;
1470
1471 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1472
1473 spin_lock(&i->lock);
1474
1475 l = i->head;
1476 do {
1477 struct uart_8250_port *up;
1478 unsigned int iir;
1479
1480 up = list_entry(l, struct uart_8250_port, list);
1481
1482 iir = serial_in(up, UART_IIR);
1483 if (!(iir & UART_IIR_NO_INT)) {
David Howells7d12e782006-10-05 14:55:46 +01001484 serial8250_handle_port(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 handled = 1;
1487
1488 end = NULL;
Marc St-Jeanbeab6972007-05-06 14:48:45 -07001489 } else if (up->port.iotype == UPIO_DWAPB &&
1490 (iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
1491 /* The DesignWare APB UART has an Busy Detect (0x07)
1492 * interrupt meaning an LCR write attempt occured while the
1493 * UART was busy. The interrupt must be cleared by reading
1494 * the UART status register (USR) and the LCR re-written. */
1495 unsigned int status;
1496 status = *(volatile u32 *)up->port.private_data;
1497 serial_out(up, UART_LCR, up->lcr);
1498
1499 handled = 1;
1500
1501 end = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 } else if (end == NULL)
1503 end = l;
1504
1505 l = l->next;
1506
1507 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1508 /* If we hit this, we're dead. */
1509 printk(KERN_ERR "serial8250: too much work for "
1510 "irq%d\n", irq);
1511 break;
1512 }
1513 } while (l != end);
1514
1515 spin_unlock(&i->lock);
1516
1517 DEBUG_INTR("end.\n");
1518
1519 return IRQ_RETVAL(handled);
1520}
1521
1522/*
1523 * To support ISA shared interrupts, we need to have one interrupt
1524 * handler that ensures that the IRQ line has been deasserted
1525 * before returning. Failing to do this will result in the IRQ
1526 * line being stuck active, and, since ISA irqs are edge triggered,
1527 * no more IRQs will be seen.
1528 */
1529static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1530{
1531 spin_lock_irq(&i->lock);
1532
1533 if (!list_empty(i->head)) {
1534 if (i->head == &up->list)
1535 i->head = i->head->next;
1536 list_del(&up->list);
1537 } else {
1538 BUG_ON(i->head != &up->list);
1539 i->head = NULL;
1540 }
1541
1542 spin_unlock_irq(&i->lock);
1543}
1544
1545static int serial_link_irq_chain(struct uart_8250_port *up)
1546{
1547 struct irq_info *i = irq_lists + up->port.irq;
Thomas Gleixner40663cc2006-07-01 19:29:43 -07001548 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 spin_lock_irq(&i->lock);
1551
1552 if (i->head) {
1553 list_add(&up->list, i->head);
1554 spin_unlock_irq(&i->lock);
1555
1556 ret = 0;
1557 } else {
1558 INIT_LIST_HEAD(&up->list);
1559 i->head = &up->list;
1560 spin_unlock_irq(&i->lock);
1561
1562 ret = request_irq(up->port.irq, serial8250_interrupt,
1563 irq_flags, "serial", i);
1564 if (ret < 0)
1565 serial_do_unlink(i, up);
1566 }
1567
1568 return ret;
1569}
1570
1571static void serial_unlink_irq_chain(struct uart_8250_port *up)
1572{
1573 struct irq_info *i = irq_lists + up->port.irq;
1574
1575 BUG_ON(i->head == NULL);
1576
1577 if (list_empty(i->head))
1578 free_irq(up->port.irq, i);
1579
1580 serial_do_unlink(i, up);
1581}
1582
Alex Williamson40b36da2007-02-14 00:33:04 -08001583/* Base timer interval for polling */
1584static inline int poll_timeout(int timeout)
1585{
1586 return timeout > 6 ? (timeout / 2 - 2) : 1;
1587}
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589/*
1590 * This function is used to handle ports that do not have an
1591 * interrupt. This doesn't work very well for 16450's, but gives
1592 * barely passable results for a 16550A. (Although at the expense
1593 * of much CPU overhead).
1594 */
1595static void serial8250_timeout(unsigned long data)
1596{
1597 struct uart_8250_port *up = (struct uart_8250_port *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 unsigned int iir;
1599
1600 iir = serial_in(up, UART_IIR);
Russell King45e24602006-01-04 19:19:06 +00001601 if (!(iir & UART_IIR_NO_INT))
David Howells7d12e782006-10-05 14:55:46 +01001602 serial8250_handle_port(up);
Alex Williamson40b36da2007-02-14 00:33:04 -08001603 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
1604}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Alex Williamson40b36da2007-02-14 00:33:04 -08001606static void serial8250_backup_timeout(unsigned long data)
1607{
1608 struct uart_8250_port *up = (struct uart_8250_port *)data;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001609 unsigned int iir, ier = 0, lsr;
1610 unsigned long flags;
Alex Williamson40b36da2007-02-14 00:33:04 -08001611
1612 /*
1613 * Must disable interrupts or else we risk racing with the interrupt
1614 * based handler.
1615 */
1616 if (is_real_interrupt(up->port.irq)) {
1617 ier = serial_in(up, UART_IER);
1618 serial_out(up, UART_IER, 0);
1619 }
1620
1621 iir = serial_in(up, UART_IIR);
1622
1623 /*
1624 * This should be a safe test for anyone who doesn't trust the
1625 * IIR bits on their UART, but it's specifically designed for
1626 * the "Diva" UART used on the management processor on many HP
1627 * ia64 and parisc boxes.
1628 */
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001629 spin_lock_irqsave(&up->port.lock, flags);
1630 lsr = serial_in(up, UART_LSR);
1631 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1632 spin_unlock_irqrestore(&up->port.lock, flags);
Alex Williamson40b36da2007-02-14 00:33:04 -08001633 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
1634 (!uart_circ_empty(&up->port.info->xmit) || up->port.x_char) &&
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001635 (lsr & UART_LSR_THRE)) {
Alex Williamson40b36da2007-02-14 00:33:04 -08001636 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1637 iir |= UART_IIR_THRI;
1638 }
1639
1640 if (!(iir & UART_IIR_NO_INT))
1641 serial8250_handle_port(up);
1642
1643 if (is_real_interrupt(up->port.irq))
1644 serial_out(up, UART_IER, ier);
1645
1646 /* Standard timer interval plus 0.2s to keep the port running */
Alan Cox6f803cd2008-02-08 04:18:52 -08001647 mod_timer(&up->timer,
1648 jiffies + poll_timeout(up->port.timeout) + HZ / 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
1651static unsigned int serial8250_tx_empty(struct uart_port *port)
1652{
1653 struct uart_8250_port *up = (struct uart_8250_port *)port;
1654 unsigned long flags;
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001655 unsigned int lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 spin_lock_irqsave(&up->port.lock, flags);
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001658 lsr = serial_in(up, UART_LSR);
1659 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 spin_unlock_irqrestore(&up->port.lock, flags);
1661
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001662 return lsr & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
1665static unsigned int serial8250_get_mctrl(struct uart_port *port)
1666{
1667 struct uart_8250_port *up = (struct uart_8250_port *)port;
Russell King2af7cd62006-01-04 16:55:09 +00001668 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 unsigned int ret;
1670
Russell King2af7cd62006-01-04 16:55:09 +00001671 status = check_modem_status(up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 ret = 0;
1674 if (status & UART_MSR_DCD)
1675 ret |= TIOCM_CAR;
1676 if (status & UART_MSR_RI)
1677 ret |= TIOCM_RNG;
1678 if (status & UART_MSR_DSR)
1679 ret |= TIOCM_DSR;
1680 if (status & UART_MSR_CTS)
1681 ret |= TIOCM_CTS;
1682 return ret;
1683}
1684
1685static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1686{
1687 struct uart_8250_port *up = (struct uart_8250_port *)port;
1688 unsigned char mcr = 0;
1689
1690 if (mctrl & TIOCM_RTS)
1691 mcr |= UART_MCR_RTS;
1692 if (mctrl & TIOCM_DTR)
1693 mcr |= UART_MCR_DTR;
1694 if (mctrl & TIOCM_OUT1)
1695 mcr |= UART_MCR_OUT1;
1696 if (mctrl & TIOCM_OUT2)
1697 mcr |= UART_MCR_OUT2;
1698 if (mctrl & TIOCM_LOOP)
1699 mcr |= UART_MCR_LOOP;
1700
1701 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1702
1703 serial_out(up, UART_MCR, mcr);
1704}
1705
1706static void serial8250_break_ctl(struct uart_port *port, int break_state)
1707{
1708 struct uart_8250_port *up = (struct uart_8250_port *)port;
1709 unsigned long flags;
1710
1711 spin_lock_irqsave(&up->port.lock, flags);
1712 if (break_state == -1)
1713 up->lcr |= UART_LCR_SBC;
1714 else
1715 up->lcr &= ~UART_LCR_SBC;
1716 serial_out(up, UART_LCR, up->lcr);
1717 spin_unlock_irqrestore(&up->port.lock, flags);
1718}
1719
Alex Williamson40b36da2007-02-14 00:33:04 -08001720#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1721
1722/*
1723 * Wait for transmitter & holding register to empty
1724 */
Will Newtonb5d674a2008-10-13 10:36:21 +01001725static void wait_for_xmitr(struct uart_8250_port *up, int bits)
Alex Williamson40b36da2007-02-14 00:33:04 -08001726{
1727 unsigned int status, tmout = 10000;
1728
1729 /* Wait up to 10ms for the character(s) to be sent. */
1730 do {
1731 status = serial_in(up, UART_LSR);
1732
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001733 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -08001734
1735 if (--tmout == 0)
1736 break;
1737 udelay(1);
1738 } while ((status & bits) != bits);
1739
1740 /* Wait up to 1s for flow control if necessary */
1741 if (up->port.flags & UPF_CONS_FLOW) {
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001742 unsigned int tmout;
1743 for (tmout = 1000000; tmout; tmout--) {
1744 unsigned int msr = serial_in(up, UART_MSR);
1745 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1746 if (msr & UART_MSR_CTS)
1747 break;
Alex Williamson40b36da2007-02-14 00:33:04 -08001748 udelay(1);
1749 touch_nmi_watchdog();
1750 }
1751 }
1752}
1753
Jason Wesself2d937f2008-04-17 20:05:37 +02001754#ifdef CONFIG_CONSOLE_POLL
1755/*
1756 * Console polling routines for writing and reading from the uart while
1757 * in an interrupt or debug context.
1758 */
1759
1760static int serial8250_get_poll_char(struct uart_port *port)
1761{
1762 struct uart_8250_port *up = (struct uart_8250_port *)port;
1763 unsigned char lsr = serial_inp(up, UART_LSR);
1764
1765 while (!(lsr & UART_LSR_DR))
1766 lsr = serial_inp(up, UART_LSR);
1767
1768 return serial_inp(up, UART_RX);
1769}
1770
1771
1772static void serial8250_put_poll_char(struct uart_port *port,
1773 unsigned char c)
1774{
1775 unsigned int ier;
1776 struct uart_8250_port *up = (struct uart_8250_port *)port;
1777
1778 /*
1779 * First save the IER then disable the interrupts
1780 */
1781 ier = serial_in(up, UART_IER);
1782 if (up->capabilities & UART_CAP_UUE)
1783 serial_out(up, UART_IER, UART_IER_UUE);
1784 else
1785 serial_out(up, UART_IER, 0);
1786
1787 wait_for_xmitr(up, BOTH_EMPTY);
1788 /*
1789 * Send the character out.
1790 * If a LF, also do CR...
1791 */
1792 serial_out(up, UART_TX, c);
1793 if (c == 10) {
1794 wait_for_xmitr(up, BOTH_EMPTY);
1795 serial_out(up, UART_TX, 13);
1796 }
1797
1798 /*
1799 * Finally, wait for transmitter to become empty
1800 * and restore the IER
1801 */
1802 wait_for_xmitr(up, BOTH_EMPTY);
1803 serial_out(up, UART_IER, ier);
1804}
1805
1806#endif /* CONFIG_CONSOLE_POLL */
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808static int serial8250_startup(struct uart_port *port)
1809{
1810 struct uart_8250_port *up = (struct uart_8250_port *)port;
1811 unsigned long flags;
Russell King55d3b282005-06-23 15:05:41 +01001812 unsigned char lsr, iir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 int retval;
1814
1815 up->capabilities = uart_config[up->port.type].flags;
1816 up->mcr = 0;
1817
1818 if (up->port.type == PORT_16C950) {
1819 /* Wake up and initialize UART */
1820 up->acr = 0;
1821 serial_outp(up, UART_LCR, 0xBF);
1822 serial_outp(up, UART_EFR, UART_EFR_ECB);
1823 serial_outp(up, UART_IER, 0);
1824 serial_outp(up, UART_LCR, 0);
1825 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
1826 serial_outp(up, UART_LCR, 0xBF);
1827 serial_outp(up, UART_EFR, UART_EFR_ECB);
1828 serial_outp(up, UART_LCR, 0);
1829 }
1830
1831#ifdef CONFIG_SERIAL_8250_RSA
1832 /*
1833 * If this is an RSA port, see if we can kick it up to the
1834 * higher speed clock.
1835 */
1836 enable_rsa(up);
1837#endif
1838
1839 /*
1840 * Clear the FIFO buffers and disable them.
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08001841 * (they will be reenabled in set_termios())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 */
1843 serial8250_clear_fifos(up);
1844
1845 /*
1846 * Clear the interrupt registers.
1847 */
1848 (void) serial_inp(up, UART_LSR);
1849 (void) serial_inp(up, UART_RX);
1850 (void) serial_inp(up, UART_IIR);
1851 (void) serial_inp(up, UART_MSR);
1852
1853 /*
1854 * At this point, there's no way the LSR could still be 0xff;
1855 * if it is, then bail out, because there's likely no UART
1856 * here.
1857 */
1858 if (!(up->port.flags & UPF_BUGGY_UART) &&
1859 (serial_inp(up, UART_LSR) == 0xff)) {
1860 printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
1861 return -ENODEV;
1862 }
1863
1864 /*
1865 * For a XR16C850, we need to set the trigger levels
1866 */
1867 if (up->port.type == PORT_16850) {
1868 unsigned char fctr;
1869
1870 serial_outp(up, UART_LCR, 0xbf);
1871
1872 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
1873 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
1874 serial_outp(up, UART_TRG, UART_TRG_96);
1875 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
1876 serial_outp(up, UART_TRG, UART_TRG_96);
1877
1878 serial_outp(up, UART_LCR, 0);
1879 }
1880
Alex Williamson40b36da2007-02-14 00:33:04 -08001881 if (is_real_interrupt(up->port.irq)) {
Alex Williamson01c194d2008-04-28 02:14:09 -07001882 unsigned char iir1;
Alex Williamson40b36da2007-02-14 00:33:04 -08001883 /*
1884 * Test for UARTs that do not reassert THRE when the
1885 * transmitter is idle and the interrupt has already
1886 * been cleared. Real 16550s should always reassert
1887 * this interrupt whenever the transmitter is idle and
1888 * the interrupt is enabled. Delays are necessary to
1889 * allow register changes to become visible.
1890 */
Borislav Petkovc389d272008-07-29 22:33:32 -07001891 spin_lock_irqsave(&up->port.lock, flags);
Anton Vorontsov768aec02008-07-22 11:21:07 +01001892 if (up->port.flags & UPF_SHARE_IRQ)
1893 disable_irq_nosync(up->port.irq);
Alex Williamson40b36da2007-02-14 00:33:04 -08001894
1895 wait_for_xmitr(up, UART_LSR_THRE);
1896 serial_out_sync(up, UART_IER, UART_IER_THRI);
1897 udelay(1); /* allow THRE to set */
Alex Williamson01c194d2008-04-28 02:14:09 -07001898 iir1 = serial_in(up, UART_IIR);
Alex Williamson40b36da2007-02-14 00:33:04 -08001899 serial_out(up, UART_IER, 0);
1900 serial_out_sync(up, UART_IER, UART_IER_THRI);
1901 udelay(1); /* allow a working UART time to re-assert THRE */
1902 iir = serial_in(up, UART_IIR);
1903 serial_out(up, UART_IER, 0);
1904
Anton Vorontsov768aec02008-07-22 11:21:07 +01001905 if (up->port.flags & UPF_SHARE_IRQ)
1906 enable_irq(up->port.irq);
Borislav Petkovc389d272008-07-29 22:33:32 -07001907 spin_unlock_irqrestore(&up->port.lock, flags);
Alex Williamson40b36da2007-02-14 00:33:04 -08001908
1909 /*
1910 * If the interrupt is not reasserted, setup a timer to
1911 * kick the UART on a regular basis.
1912 */
Alex Williamson01c194d2008-04-28 02:14:09 -07001913 if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
Will Newton363f66f2008-09-02 14:35:44 -07001914 up->bugs |= UART_BUG_THRE;
Alex Williamson40b36da2007-02-14 00:33:04 -08001915 pr_debug("ttyS%d - using backup timer\n", port->line);
Alex Williamson40b36da2007-02-14 00:33:04 -08001916 }
1917 }
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 /*
Will Newton363f66f2008-09-02 14:35:44 -07001920 * The above check will only give an accurate result the first time
1921 * the port is opened so this value needs to be preserved.
1922 */
1923 if (up->bugs & UART_BUG_THRE) {
1924 up->timer.function = serial8250_backup_timeout;
1925 up->timer.data = (unsigned long)up;
1926 mod_timer(&up->timer, jiffies +
1927 poll_timeout(up->port.timeout) + HZ / 5);
1928 }
1929
1930 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 * If the "interrupt" for this port doesn't correspond with any
1932 * hardware interrupt, we use a timer-based system. The original
1933 * driver used to do this with IRQ0.
1934 */
1935 if (!is_real_interrupt(up->port.irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 up->timer.data = (unsigned long)up;
Alex Williamson40b36da2007-02-14 00:33:04 -08001937 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 } else {
1939 retval = serial_link_irq_chain(up);
1940 if (retval)
1941 return retval;
1942 }
1943
1944 /*
1945 * Now, initialize the UART
1946 */
1947 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
1948
1949 spin_lock_irqsave(&up->port.lock, flags);
1950 if (up->port.flags & UPF_FOURPORT) {
1951 if (!is_real_interrupt(up->port.irq))
1952 up->port.mctrl |= TIOCM_OUT1;
1953 } else
1954 /*
1955 * Most PC uarts need OUT2 raised to enable interrupts.
1956 */
1957 if (is_real_interrupt(up->port.irq))
1958 up->port.mctrl |= TIOCM_OUT2;
1959
1960 serial8250_set_mctrl(&up->port, up->port.mctrl);
Russell King55d3b282005-06-23 15:05:41 +01001961
1962 /*
1963 * Do a quick test to see if we receive an
1964 * interrupt when we enable the TX irq.
1965 */
1966 serial_outp(up, UART_IER, UART_IER_THRI);
1967 lsr = serial_in(up, UART_LSR);
1968 iir = serial_in(up, UART_IIR);
1969 serial_outp(up, UART_IER, 0);
1970
1971 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
Russell King67f76542005-06-23 22:26:43 +01001972 if (!(up->bugs & UART_BUG_TXEN)) {
1973 up->bugs |= UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01001974 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
1975 port->line);
1976 }
1977 } else {
Russell King67f76542005-06-23 22:26:43 +01001978 up->bugs &= ~UART_BUG_TXEN;
Russell King55d3b282005-06-23 15:05:41 +01001979 }
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 spin_unlock_irqrestore(&up->port.lock, flags);
1982
1983 /*
Corey Minyardad4c2aa2007-08-22 14:01:18 -07001984 * Clear the interrupt registers again for luck, and clear the
1985 * saved flags to avoid getting false values from polling
1986 * routines or the previous session.
1987 */
1988 serial_inp(up, UART_LSR);
1989 serial_inp(up, UART_RX);
1990 serial_inp(up, UART_IIR);
1991 serial_inp(up, UART_MSR);
1992 up->lsr_saved_flags = 0;
1993 up->msr_saved_flags = 0;
1994
1995 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 * Finally, enable interrupts. Note: Modem status interrupts
1997 * are set via set_termios(), which will be occurring imminently
1998 * anyway, so we don't enable them here.
1999 */
2000 up->ier = UART_IER_RLSI | UART_IER_RDI;
2001 serial_outp(up, UART_IER, up->ier);
2002
2003 if (up->port.flags & UPF_FOURPORT) {
2004 unsigned int icp;
2005 /*
2006 * Enable interrupts on the AST Fourport board
2007 */
2008 icp = (up->port.iobase & 0xfe0) | 0x01f;
2009 outb_p(0x80, icp);
2010 (void) inb_p(icp);
2011 }
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return 0;
2014}
2015
2016static void serial8250_shutdown(struct uart_port *port)
2017{
2018 struct uart_8250_port *up = (struct uart_8250_port *)port;
2019 unsigned long flags;
2020
2021 /*
2022 * Disable interrupts from this port
2023 */
2024 up->ier = 0;
2025 serial_outp(up, UART_IER, 0);
2026
2027 spin_lock_irqsave(&up->port.lock, flags);
2028 if (up->port.flags & UPF_FOURPORT) {
2029 /* reset interrupts on the AST Fourport board */
2030 inb((up->port.iobase & 0xfe0) | 0x1f);
2031 up->port.mctrl |= TIOCM_OUT1;
2032 } else
2033 up->port.mctrl &= ~TIOCM_OUT2;
2034
2035 serial8250_set_mctrl(&up->port, up->port.mctrl);
2036 spin_unlock_irqrestore(&up->port.lock, flags);
2037
2038 /*
2039 * Disable break condition and FIFOs
2040 */
2041 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
2042 serial8250_clear_fifos(up);
2043
2044#ifdef CONFIG_SERIAL_8250_RSA
2045 /*
2046 * Reset the RSA board back to 115kbps compat mode.
2047 */
2048 disable_rsa(up);
2049#endif
2050
2051 /*
2052 * Read data port to reset things, and then unlink from
2053 * the IRQ chain.
2054 */
2055 (void) serial_in(up, UART_RX);
2056
Alex Williamson40b36da2007-02-14 00:33:04 -08002057 del_timer_sync(&up->timer);
2058 up->timer.function = serial8250_timeout;
2059 if (is_real_interrupt(up->port.irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 serial_unlink_irq_chain(up);
2061}
2062
2063static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
2064{
2065 unsigned int quot;
2066
2067 /*
2068 * Handle magic divisors for baud rates above baud_base on
2069 * SMSC SuperIO chips.
2070 */
2071 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2072 baud == (port->uartclk/4))
2073 quot = 0x8001;
2074 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2075 baud == (port->uartclk/8))
2076 quot = 0x8002;
2077 else
2078 quot = uart_get_divisor(port, baud);
2079
2080 return quot;
2081}
2082
2083static void
Alan Cox606d0992006-12-08 02:38:45 -08002084serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2085 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
2087 struct uart_8250_port *up = (struct uart_8250_port *)port;
2088 unsigned char cval, fcr = 0;
2089 unsigned long flags;
2090 unsigned int baud, quot;
2091
2092 switch (termios->c_cflag & CSIZE) {
2093 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +01002094 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 break;
2096 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +01002097 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 break;
2099 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +01002100 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 break;
2102 default:
2103 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +01002104 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 break;
2106 }
2107
2108 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +01002109 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (termios->c_cflag & PARENB)
2111 cval |= UART_LCR_PARITY;
2112 if (!(termios->c_cflag & PARODD))
2113 cval |= UART_LCR_EPAR;
2114#ifdef CMSPAR
2115 if (termios->c_cflag & CMSPAR)
2116 cval |= UART_LCR_SPAR;
2117#endif
2118
2119 /*
2120 * Ask the core to calculate the divisor for us.
2121 */
Thomas Koellerbd71c182007-05-06 14:48:47 -07002122 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 quot = serial8250_get_divisor(port, baud);
2124
2125 /*
Russell King4ba5e352005-06-23 10:43:04 +01002126 * Oxford Semi 952 rev B workaround
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 */
Russell King4ba5e352005-06-23 10:43:04 +01002128 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
Alan Cox3e8d4e22008-02-04 22:27:53 -08002129 quot++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
2132 if (baud < 2400)
2133 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2134 else
2135 fcr = uart_config[up->port.type].fcr;
2136 }
2137
2138 /*
2139 * MCR-based auto flow control. When AFE is enabled, RTS will be
2140 * deasserted when the receive FIFO contains more characters than
2141 * the trigger, or the MCR RTS bit is cleared. In the case where
2142 * the remote UART is not using CTS auto flow control, we must
2143 * have sufficient FIFO entries for the latency of the remote
2144 * UART to respond. IOW, at least 32 bytes of FIFO.
2145 */
2146 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
2147 up->mcr &= ~UART_MCR_AFE;
2148 if (termios->c_cflag & CRTSCTS)
2149 up->mcr |= UART_MCR_AFE;
2150 }
2151
2152 /*
2153 * Ok, we're now changing the port state. Do it with
2154 * interrupts disabled.
2155 */
2156 spin_lock_irqsave(&up->port.lock, flags);
2157
2158 /*
2159 * Update the per-port timeout.
2160 */
2161 uart_update_timeout(port, termios->c_cflag, baud);
2162
2163 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2164 if (termios->c_iflag & INPCK)
2165 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2166 if (termios->c_iflag & (BRKINT | PARMRK))
2167 up->port.read_status_mask |= UART_LSR_BI;
2168
2169 /*
2170 * Characteres to ignore
2171 */
2172 up->port.ignore_status_mask = 0;
2173 if (termios->c_iflag & IGNPAR)
2174 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2175 if (termios->c_iflag & IGNBRK) {
2176 up->port.ignore_status_mask |= UART_LSR_BI;
2177 /*
2178 * If we're ignoring parity and break indicators,
2179 * ignore overruns too (for real raw support).
2180 */
2181 if (termios->c_iflag & IGNPAR)
2182 up->port.ignore_status_mask |= UART_LSR_OE;
2183 }
2184
2185 /*
2186 * ignore all characters if CREAD is not set
2187 */
2188 if ((termios->c_cflag & CREAD) == 0)
2189 up->port.ignore_status_mask |= UART_LSR_DR;
2190
2191 /*
2192 * CTS flow control flag and modem status interrupts
2193 */
2194 up->ier &= ~UART_IER_MSI;
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002195 if (!(up->bugs & UART_BUG_NOMSR) &&
2196 UART_ENABLE_MS(&up->port, termios->c_cflag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 up->ier |= UART_IER_MSI;
2198 if (up->capabilities & UART_CAP_UUE)
2199 up->ier |= UART_IER_UUE | UART_IER_RTOIE;
2200
2201 serial_out(up, UART_IER, up->ier);
2202
2203 if (up->capabilities & UART_CAP_EFR) {
2204 unsigned char efr = 0;
2205 /*
2206 * TI16C752/Startech hardware flow control. FIXME:
2207 * - TI16C752 requires control thresholds to be set.
2208 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2209 */
2210 if (termios->c_cflag & CRTSCTS)
2211 efr |= UART_EFR_CTS;
2212
2213 serial_outp(up, UART_LCR, 0xBF);
2214 serial_outp(up, UART_EFR, efr);
2215 }
2216
Jonathan McDowell255341c2006-08-14 23:05:32 -07002217#ifdef CONFIG_ARCH_OMAP15XX
2218 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2219 if (cpu_is_omap1510() && is_omap_port((unsigned int)up->port.membase)) {
2220 if (baud == 115200) {
2221 quot = 1;
2222 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2223 } else
2224 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2225 }
2226#endif
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 if (up->capabilities & UART_NATSEMI) {
2229 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
2230 serial_outp(up, UART_LCR, 0xe0);
2231 } else {
2232 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2233 }
2234
Jon Anders Haugumb32b19b2006-04-30 11:20:56 +01002235 serial_dl_write(up, quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 /*
2238 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2239 * is written without DLAB set, this mode will be disabled.
2240 */
2241 if (up->port.type == PORT_16750)
2242 serial_outp(up, UART_FCR, fcr);
2243
2244 serial_outp(up, UART_LCR, cval); /* reset DLAB */
2245 up->lcr = cval; /* Save LCR */
2246 if (up->port.type != PORT_16750) {
2247 if (fcr & UART_FCR_ENABLE_FIFO) {
2248 /* emulated UARTs (Lucent Venus 167x) need two steps */
2249 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
2250 }
2251 serial_outp(up, UART_FCR, fcr); /* set fcr */
2252 }
2253 serial8250_set_mctrl(&up->port, up->port.mctrl);
2254 spin_unlock_irqrestore(&up->port.lock, flags);
Alan Coxe991a2b2008-04-28 02:14:06 -07002255 /* Don't rewrite B0 */
2256 if (tty_termios_baud_rate(termios))
2257 tty_termios_encode_baud_rate(termios, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
2260static void
2261serial8250_pm(struct uart_port *port, unsigned int state,
2262 unsigned int oldstate)
2263{
2264 struct uart_8250_port *p = (struct uart_8250_port *)port;
2265
2266 serial8250_set_sleep(p, state != 0);
2267
2268 if (p->pm)
2269 p->pm(port, state, oldstate);
2270}
2271
2272/*
2273 * Resource handling.
2274 */
2275static int serial8250_request_std_resource(struct uart_8250_port *up)
2276{
2277 unsigned int size = 8 << up->port.regshift;
2278 int ret = 0;
2279
2280 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002281 case UPIO_AU:
2282 size = 0x100000;
2283 /* fall thru */
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002284 case UPIO_TSI:
2285 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 case UPIO_MEM:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002287 case UPIO_DWAPB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (!up->port.mapbase)
2289 break;
2290
2291 if (!request_mem_region(up->port.mapbase, size, "serial")) {
2292 ret = -EBUSY;
2293 break;
2294 }
2295
2296 if (up->port.flags & UPF_IOREMAP) {
Alan Cox6f441fe2008-05-01 04:34:59 -07002297 up->port.membase = ioremap_nocache(up->port.mapbase,
2298 size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (!up->port.membase) {
2300 release_mem_region(up->port.mapbase, size);
2301 ret = -ENOMEM;
2302 }
2303 }
2304 break;
2305
2306 case UPIO_HUB6:
2307 case UPIO_PORT:
2308 if (!request_region(up->port.iobase, size, "serial"))
2309 ret = -EBUSY;
2310 break;
2311 }
2312 return ret;
2313}
2314
2315static void serial8250_release_std_resource(struct uart_8250_port *up)
2316{
2317 unsigned int size = 8 << up->port.regshift;
2318
2319 switch (up->port.iotype) {
Sergei Shtylyov85835f42006-04-30 11:15:58 +01002320 case UPIO_AU:
2321 size = 0x100000;
2322 /* fall thru */
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002323 case UPIO_TSI:
2324 case UPIO_MEM32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 case UPIO_MEM:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002326 case UPIO_DWAPB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if (!up->port.mapbase)
2328 break;
2329
2330 if (up->port.flags & UPF_IOREMAP) {
2331 iounmap(up->port.membase);
2332 up->port.membase = NULL;
2333 }
2334
2335 release_mem_region(up->port.mapbase, size);
2336 break;
2337
2338 case UPIO_HUB6:
2339 case UPIO_PORT:
2340 release_region(up->port.iobase, size);
2341 break;
2342 }
2343}
2344
2345static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2346{
2347 unsigned long start = UART_RSA_BASE << up->port.regshift;
2348 unsigned int size = 8 << up->port.regshift;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002349 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 case UPIO_HUB6:
2353 case UPIO_PORT:
2354 start += up->port.iobase;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +04002355 if (request_region(start, size, "serial-rsa"))
2356 ret = 0;
2357 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 ret = -EBUSY;
2359 break;
2360 }
2361
2362 return ret;
2363}
2364
2365static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2366{
2367 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2368 unsigned int size = 8 << up->port.regshift;
2369
2370 switch (up->port.iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 case UPIO_HUB6:
2372 case UPIO_PORT:
2373 release_region(up->port.iobase + offset, size);
2374 break;
2375 }
2376}
2377
2378static void serial8250_release_port(struct uart_port *port)
2379{
2380 struct uart_8250_port *up = (struct uart_8250_port *)port;
2381
2382 serial8250_release_std_resource(up);
2383 if (up->port.type == PORT_RSA)
2384 serial8250_release_rsa_resource(up);
2385}
2386
2387static int serial8250_request_port(struct uart_port *port)
2388{
2389 struct uart_8250_port *up = (struct uart_8250_port *)port;
2390 int ret = 0;
2391
2392 ret = serial8250_request_std_resource(up);
2393 if (ret == 0 && up->port.type == PORT_RSA) {
2394 ret = serial8250_request_rsa_resource(up);
2395 if (ret < 0)
2396 serial8250_release_std_resource(up);
2397 }
2398
2399 return ret;
2400}
2401
2402static void serial8250_config_port(struct uart_port *port, int flags)
2403{
2404 struct uart_8250_port *up = (struct uart_8250_port *)port;
2405 int probeflags = PROBE_ANY;
2406 int ret;
2407
2408 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 * Find the region that we can probe for. This in turn
2410 * tells us whether we can probe for the type of port.
2411 */
2412 ret = serial8250_request_std_resource(up);
2413 if (ret < 0)
2414 return;
2415
2416 ret = serial8250_request_rsa_resource(up);
2417 if (ret < 0)
2418 probeflags &= ~PROBE_RSA;
2419
2420 if (flags & UART_CONFIG_TYPE)
2421 autoconfig(up, probeflags);
2422 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2423 autoconfig_irq(up);
2424
2425 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2426 serial8250_release_rsa_resource(up);
2427 if (up->port.type == PORT_UNKNOWN)
2428 serial8250_release_std_resource(up);
2429}
2430
2431static int
2432serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2433{
2434 if (ser->irq >= NR_IRQS || ser->irq < 0 ||
2435 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2436 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2437 ser->type == PORT_STARTECH)
2438 return -EINVAL;
2439 return 0;
2440}
2441
2442static const char *
2443serial8250_type(struct uart_port *port)
2444{
2445 int type = port->type;
2446
2447 if (type >= ARRAY_SIZE(uart_config))
2448 type = 0;
2449 return uart_config[type].name;
2450}
2451
2452static struct uart_ops serial8250_pops = {
2453 .tx_empty = serial8250_tx_empty,
2454 .set_mctrl = serial8250_set_mctrl,
2455 .get_mctrl = serial8250_get_mctrl,
2456 .stop_tx = serial8250_stop_tx,
2457 .start_tx = serial8250_start_tx,
2458 .stop_rx = serial8250_stop_rx,
2459 .enable_ms = serial8250_enable_ms,
2460 .break_ctl = serial8250_break_ctl,
2461 .startup = serial8250_startup,
2462 .shutdown = serial8250_shutdown,
2463 .set_termios = serial8250_set_termios,
2464 .pm = serial8250_pm,
2465 .type = serial8250_type,
2466 .release_port = serial8250_release_port,
2467 .request_port = serial8250_request_port,
2468 .config_port = serial8250_config_port,
2469 .verify_port = serial8250_verify_port,
Jason Wesself2d937f2008-04-17 20:05:37 +02002470#ifdef CONFIG_CONSOLE_POLL
2471 .poll_get_char = serial8250_get_poll_char,
2472 .poll_put_char = serial8250_put_poll_char,
2473#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474};
2475
2476static struct uart_8250_port serial8250_ports[UART_NR];
2477
2478static void __init serial8250_isa_init_ports(void)
2479{
2480 struct uart_8250_port *up;
2481 static int first = 1;
2482 int i;
2483
2484 if (!first)
2485 return;
2486 first = 0;
2487
Dave Jonesa61c2d72006-01-07 23:18:19 +00002488 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 struct uart_8250_port *up = &serial8250_ports[i];
2490
2491 up->port.line = i;
2492 spin_lock_init(&up->port.lock);
2493
2494 init_timer(&up->timer);
2495 up->timer.function = serial8250_timeout;
2496
2497 /*
2498 * ALPHA_KLUDGE_MCR needs to be killed.
2499 */
2500 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2501 up->mcr_force = ALPHA_KLUDGE_MCR;
2502
2503 up->port.ops = &serial8250_pops;
2504 }
2505
Russell King44454bc2005-06-30 22:41:22 +01002506 for (i = 0, up = serial8250_ports;
Dave Jonesa61c2d72006-01-07 23:18:19 +00002507 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 i++, up++) {
2509 up->port.iobase = old_serial_port[i].port;
2510 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
2511 up->port.uartclk = old_serial_port[i].baud_base * 16;
2512 up->port.flags = old_serial_port[i].flags;
2513 up->port.hub6 = old_serial_port[i].hub6;
2514 up->port.membase = old_serial_port[i].iomem_base;
2515 up->port.iotype = old_serial_port[i].io_type;
2516 up->port.regshift = old_serial_port[i].iomem_reg_shift;
2517 if (share_irqs)
2518 up->port.flags |= UPF_SHARE_IRQ;
2519 }
2520}
2521
2522static void __init
2523serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2524{
2525 int i;
2526
2527 serial8250_isa_init_ports();
2528
Dave Jonesa61c2d72006-01-07 23:18:19 +00002529 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 struct uart_8250_port *up = &serial8250_ports[i];
2531
2532 up->port.dev = dev;
2533 uart_add_one_port(drv, &up->port);
2534 }
2535}
2536
2537#ifdef CONFIG_SERIAL_8250_CONSOLE
2538
Russell Kingd3587882006-03-20 20:00:09 +00002539static void serial8250_console_putchar(struct uart_port *port, int ch)
2540{
2541 struct uart_8250_port *up = (struct uart_8250_port *)port;
2542
2543 wait_for_xmitr(up, UART_LSR_THRE);
2544 serial_out(up, UART_TX, ch);
2545}
2546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547/*
2548 * Print a string to the serial port trying not to disturb
2549 * any possible real use of the port...
2550 *
2551 * The console_lock must be held when we get here.
2552 */
2553static void
2554serial8250_console_write(struct console *co, const char *s, unsigned int count)
2555{
2556 struct uart_8250_port *up = &serial8250_ports[co->index];
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002557 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 unsigned int ier;
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002559 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Andrew Morton78512ec2005-11-07 00:59:13 -08002561 touch_nmi_watchdog();
2562
Andrew Morton68aa2c02006-06-30 02:29:59 -07002563 local_irq_save(flags);
2564 if (up->port.sysrq) {
2565 /* serial8250_handle_port() already took the lock */
2566 locked = 0;
2567 } else if (oops_in_progress) {
2568 locked = spin_trylock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002569 } else
Andrew Morton68aa2c02006-06-30 02:29:59 -07002570 spin_lock(&up->port.lock);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 /*
Ralf Baechledc7bf132006-02-15 09:59:47 +00002573 * First save the IER then disable the interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 */
2575 ier = serial_in(up, UART_IER);
2576
2577 if (up->capabilities & UART_CAP_UUE)
2578 serial_out(up, UART_IER, UART_IER_UUE);
2579 else
2580 serial_out(up, UART_IER, 0);
2581
Russell Kingd3587882006-03-20 20:00:09 +00002582 uart_console_write(&up->port, s, count, serial8250_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 /*
2585 * Finally, wait for transmitter to become empty
2586 * and restore the IER
2587 */
Alan Coxf91a3712006-01-21 14:59:12 +00002588 wait_for_xmitr(up, BOTH_EMPTY);
Russell Kinga88d75b2006-04-30 11:30:15 +01002589 serial_out(up, UART_IER, ier);
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002590
Corey Minyardad4c2aa2007-08-22 14:01:18 -07002591 /*
2592 * The receive handling will happen properly because the
2593 * receive ready bit will still be set; it is not cleared
2594 * on read. However, modem control will not, we must
2595 * call it if we have saved something in the saved flags
2596 * while processing with interrupts off.
2597 */
2598 if (up->msr_saved_flags)
2599 check_modem_status(up);
2600
Russell Kingd8a5a8d2006-05-02 16:04:29 +01002601 if (locked)
Andrew Morton68aa2c02006-06-30 02:29:59 -07002602 spin_unlock(&up->port.lock);
2603 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
Vivek Goyal118c0ac2007-01-11 01:52:44 +01002606static int __init serial8250_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
2608 struct uart_port *port;
2609 int baud = 9600;
2610 int bits = 8;
2611 int parity = 'n';
2612 int flow = 'n';
2613
2614 /*
2615 * Check whether an invalid uart number has been specified, and
2616 * if so, search for the first available port that does have
2617 * console support.
2618 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002619 if (co->index >= nr_uarts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 co->index = 0;
2621 port = &serial8250_ports[co->index].port;
2622 if (!port->iobase && !port->membase)
2623 return -ENODEV;
2624
2625 if (options)
2626 uart_parse_options(options, &baud, &parity, &bits, &flow);
2627
2628 return uart_set_options(port, co, baud, parity, bits, flow);
2629}
2630
Daniel Ritzb6b1d872007-08-03 16:07:43 +02002631static int serial8250_console_early_setup(void)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002632{
2633 return serial8250_find_port_for_earlycon();
2634}
2635
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636static struct uart_driver serial8250_reg;
2637static struct console serial8250_console = {
2638 .name = "ttyS",
2639 .write = serial8250_console_write,
2640 .device = uart_console_device,
2641 .setup = serial8250_console_setup,
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002642 .early_setup = serial8250_console_early_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 .flags = CON_PRINTBUFFER,
2644 .index = -1,
2645 .data = &serial8250_reg,
2646};
2647
2648static int __init serial8250_console_init(void)
2649{
Eric W. Biederman05d81d22008-07-12 13:47:53 -07002650 if (nr_uarts > UART_NR)
2651 nr_uarts = UART_NR;
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 serial8250_isa_init_ports();
2654 register_console(&serial8250_console);
2655 return 0;
2656}
2657console_initcall(serial8250_console_init);
2658
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002659int serial8250_find_port(struct uart_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
2661 int line;
2662 struct uart_port *port;
2663
Dave Jonesa61c2d72006-01-07 23:18:19 +00002664 for (line = 0; line < nr_uarts; line++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 port = &serial8250_ports[line].port;
Russell King50aec3b52006-01-04 18:13:03 +00002666 if (uart_match_port(p, port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 return line;
2668 }
2669 return -ENODEV;
2670}
2671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672#define SERIAL8250_CONSOLE &serial8250_console
2673#else
2674#define SERIAL8250_CONSOLE NULL
2675#endif
2676
2677static struct uart_driver serial8250_reg = {
2678 .owner = THIS_MODULE,
2679 .driver_name = "serial",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 .dev_name = "ttyS",
2681 .major = TTY_MAJOR,
2682 .minor = 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 .cons = SERIAL8250_CONSOLE,
2684};
2685
Russell Kingd856c662006-02-23 10:22:13 +00002686/*
2687 * early_serial_setup - early registration for 8250 ports
2688 *
2689 * Setup an 8250 port structure prior to console initialisation. Use
2690 * after console initialisation will cause undefined behaviour.
2691 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692int __init early_serial_setup(struct uart_port *port)
2693{
2694 if (port->line >= ARRAY_SIZE(serial8250_ports))
2695 return -ENODEV;
2696
2697 serial8250_isa_init_ports();
2698 serial8250_ports[port->line].port = *port;
2699 serial8250_ports[port->line].port.ops = &serial8250_pops;
2700 return 0;
2701}
2702
2703/**
2704 * serial8250_suspend_port - suspend one serial port
2705 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 *
2707 * Suspend one serial port.
2708 */
2709void serial8250_suspend_port(int line)
2710{
2711 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2712}
2713
2714/**
2715 * serial8250_resume_port - resume one serial port
2716 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 *
2718 * Resume one serial port.
2719 */
2720void serial8250_resume_port(int line)
2721{
David Woodhouseb5b82df2007-05-17 14:27:39 +08002722 struct uart_8250_port *up = &serial8250_ports[line];
2723
2724 if (up->capabilities & UART_NATSEMI) {
2725 unsigned char tmp;
2726
2727 /* Ensure it's still in high speed mode */
2728 serial_outp(up, UART_LCR, 0xE0);
2729
2730 tmp = serial_in(up, 0x04); /* EXCR2 */
2731 tmp &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
2732 tmp |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
2733 serial_outp(up, 0x04, tmp);
2734
2735 serial_outp(up, UART_LCR, 0);
2736 }
2737 uart_resume_port(&serial8250_reg, &up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
2740/*
2741 * Register a set of serial devices attached to a platform device. The
2742 * list is terminated with a zero flags entry, which means we expect
2743 * all entries to have at least UPF_BOOT_AUTOCONF set.
2744 */
Russell King3ae5eae2005-11-09 22:32:44 +00002745static int __devinit serial8250_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
Russell King3ae5eae2005-11-09 22:32:44 +00002747 struct plat_serial8250_port *p = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 struct uart_port port;
Russell Kingec9f47c2005-06-27 11:12:54 +01002749 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751 memset(&port, 0, sizeof(struct uart_port));
2752
Russell Kingec9f47c2005-06-27 11:12:54 +01002753 for (i = 0; p && p->flags != 0; p++, i++) {
Will Newton74a197412008-02-04 22:27:50 -08002754 port.iobase = p->iobase;
2755 port.membase = p->membase;
2756 port.irq = p->irq;
2757 port.uartclk = p->uartclk;
2758 port.regshift = p->regshift;
2759 port.iotype = p->iotype;
2760 port.flags = p->flags;
2761 port.mapbase = p->mapbase;
2762 port.hub6 = p->hub6;
2763 port.private_data = p->private_data;
2764 port.dev = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (share_irqs)
2766 port.flags |= UPF_SHARE_IRQ;
Russell Kingec9f47c2005-06-27 11:12:54 +01002767 ret = serial8250_register_port(&port);
2768 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00002769 dev_err(&dev->dev, "unable to register port at index %d "
Josh Boyer4f640ef2007-07-23 18:43:44 -07002770 "(IO%lx MEM%llx IRQ%d): %d\n", i,
2771 p->iobase, (unsigned long long)p->mapbase,
2772 p->irq, ret);
Russell Kingec9f47c2005-06-27 11:12:54 +01002773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 }
2775 return 0;
2776}
2777
2778/*
2779 * Remove serial ports registered against a platform device.
2780 */
Russell King3ae5eae2005-11-09 22:32:44 +00002781static int __devexit serial8250_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782{
2783 int i;
2784
Dave Jonesa61c2d72006-01-07 23:18:19 +00002785 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 struct uart_8250_port *up = &serial8250_ports[i];
2787
Russell King3ae5eae2005-11-09 22:32:44 +00002788 if (up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 serial8250_unregister_port(i);
2790 }
2791 return 0;
2792}
2793
Russell King3ae5eae2005-11-09 22:32:44 +00002794static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
2796 int i;
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 for (i = 0; i < UART_NR; i++) {
2799 struct uart_8250_port *up = &serial8250_ports[i];
2800
Russell King3ae5eae2005-11-09 22:32:44 +00002801 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 uart_suspend_port(&serial8250_reg, &up->port);
2803 }
2804
2805 return 0;
2806}
2807
Russell King3ae5eae2005-11-09 22:32:44 +00002808static int serial8250_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
2810 int i;
2811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 for (i = 0; i < UART_NR; i++) {
2813 struct uart_8250_port *up = &serial8250_ports[i];
2814
Russell King3ae5eae2005-11-09 22:32:44 +00002815 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
David Woodhouseb5b82df2007-05-17 14:27:39 +08002816 serial8250_resume_port(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 }
2818
2819 return 0;
2820}
2821
Russell King3ae5eae2005-11-09 22:32:44 +00002822static struct platform_driver serial8250_isa_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 .probe = serial8250_probe,
2824 .remove = __devexit_p(serial8250_remove),
2825 .suspend = serial8250_suspend,
2826 .resume = serial8250_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002827 .driver = {
2828 .name = "serial8250",
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002829 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002830 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831};
2832
2833/*
2834 * This "device" covers _all_ ISA 8250-compatible serial devices listed
2835 * in the table in include/asm/serial.h
2836 */
2837static struct platform_device *serial8250_isa_devs;
2838
2839/*
2840 * serial8250_register_port and serial8250_unregister_port allows for
2841 * 16x50 serial ports to be configured at run-time, to support PCMCIA
2842 * modems and PCI multiport cards.
2843 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002844static DEFINE_MUTEX(serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
2847{
2848 int i;
2849
2850 /*
2851 * First, find a port entry which matches.
2852 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002853 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 if (uart_match_port(&serial8250_ports[i].port, port))
2855 return &serial8250_ports[i];
2856
2857 /*
2858 * We didn't find a matching entry, so look for the first
2859 * free entry. We look for one which hasn't been previously
2860 * used (indicated by zero iobase).
2861 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002862 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
2864 serial8250_ports[i].port.iobase == 0)
2865 return &serial8250_ports[i];
2866
2867 /*
2868 * That also failed. Last resort is to find any entry which
2869 * doesn't have a real port associated with it.
2870 */
Dave Jonesa61c2d72006-01-07 23:18:19 +00002871 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
2873 return &serial8250_ports[i];
2874
2875 return NULL;
2876}
2877
2878/**
2879 * serial8250_register_port - register a serial port
2880 * @port: serial port template
2881 *
2882 * Configure the serial port specified by the request. If the
2883 * port exists and is in use, it is hung up and unregistered
2884 * first.
2885 *
2886 * The port is then probed and if necessary the IRQ is autodetected
2887 * If this fails an error is returned.
2888 *
2889 * On success the port is ready to use and the line number is returned.
2890 */
2891int serial8250_register_port(struct uart_port *port)
2892{
2893 struct uart_8250_port *uart;
2894 int ret = -ENOSPC;
2895
2896 if (port->uartclk == 0)
2897 return -EINVAL;
2898
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002899 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901 uart = serial8250_find_match_or_unused(port);
2902 if (uart) {
2903 uart_remove_one_port(&serial8250_reg, &uart->port);
2904
Will Newton74a197412008-02-04 22:27:50 -08002905 uart->port.iobase = port->iobase;
2906 uart->port.membase = port->membase;
2907 uart->port.irq = port->irq;
2908 uart->port.uartclk = port->uartclk;
2909 uart->port.fifosize = port->fifosize;
2910 uart->port.regshift = port->regshift;
2911 uart->port.iotype = port->iotype;
2912 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
2913 uart->port.mapbase = port->mapbase;
2914 uart->port.private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 if (port->dev)
2916 uart->port.dev = port->dev;
2917
2918 ret = uart_add_one_port(&serial8250_reg, &uart->port);
2919 if (ret == 0)
2920 ret = uart->port.line;
2921 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002922 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
2924 return ret;
2925}
2926EXPORT_SYMBOL(serial8250_register_port);
2927
2928/**
2929 * serial8250_unregister_port - remove a 16x50 serial port at runtime
2930 * @line: serial line number
2931 *
2932 * Remove one serial port. This may not be called from interrupt
2933 * context. We hand the port back to the our control.
2934 */
2935void serial8250_unregister_port(int line)
2936{
2937 struct uart_8250_port *uart = &serial8250_ports[line];
2938
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002939 mutex_lock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 uart_remove_one_port(&serial8250_reg, &uart->port);
2941 if (serial8250_isa_devs) {
2942 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
2943 uart->port.type = PORT_UNKNOWN;
2944 uart->port.dev = &serial8250_isa_devs->dev;
2945 uart_add_one_port(&serial8250_reg, &uart->port);
2946 } else {
2947 uart->port.dev = NULL;
2948 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002949 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950}
2951EXPORT_SYMBOL(serial8250_unregister_port);
2952
2953static int __init serial8250_init(void)
2954{
2955 int ret, i;
2956
Dave Jonesa61c2d72006-01-07 23:18:19 +00002957 if (nr_uarts > UART_NR)
2958 nr_uarts = UART_NR;
2959
Adrian Bunkd87a6d92008-07-16 21:53:31 +01002960 printk(KERN_INFO "Serial: 8250/16550 driver"
Dave Jonesa61c2d72006-01-07 23:18:19 +00002961 "%d ports, IRQ sharing %sabled\n", nr_uarts,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 share_irqs ? "en" : "dis");
2963
David Millerb70ac772008-10-13 10:36:31 +01002964#ifdef CONFIG_SPARC
2965 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
2966#else
2967 serial8250_reg.nr = UART_NR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 ret = uart_register_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01002969#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (ret)
2971 goto out;
2972
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002973 serial8250_isa_devs = platform_device_alloc("serial8250",
2974 PLAT8250_DEV_LEGACY);
2975 if (!serial8250_isa_devs) {
2976 ret = -ENOMEM;
Russell Kingbc965a72006-01-18 09:54:29 +00002977 goto unreg_uart_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 }
2979
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002980 ret = platform_device_add(serial8250_isa_devs);
2981 if (ret)
2982 goto put_dev;
2983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
2985
Russell Kingbc965a72006-01-18 09:54:29 +00002986 ret = platform_driver_register(&serial8250_isa_driver);
2987 if (ret == 0)
2988 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Russell Kingbc965a72006-01-18 09:54:29 +00002990 platform_device_del(serial8250_isa_devs);
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002991 put_dev:
2992 platform_device_put(serial8250_isa_devs);
Dmitry Torokhov7493a312006-01-13 22:06:43 +00002993 unreg_uart_drv:
David Millerb70ac772008-10-13 10:36:31 +01002994#ifdef CONFIG_SPARC
2995 sunserial_unregister_minors(&serial8250_reg, UART_NR);
2996#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01002998#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 out:
3000 return ret;
3001}
3002
3003static void __exit serial8250_exit(void)
3004{
3005 struct platform_device *isa_dev = serial8250_isa_devs;
3006
3007 /*
3008 * This tells serial8250_unregister_port() not to re-register
3009 * the ports (thereby making serial8250_isa_driver permanently
3010 * in use.)
3011 */
3012 serial8250_isa_devs = NULL;
3013
Russell King3ae5eae2005-11-09 22:32:44 +00003014 platform_driver_unregister(&serial8250_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 platform_device_unregister(isa_dev);
3016
David Millerb70ac772008-10-13 10:36:31 +01003017#ifdef CONFIG_SPARC
3018 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3019#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01003021#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
3024module_init(serial8250_init);
3025module_exit(serial8250_exit);
3026
3027EXPORT_SYMBOL(serial8250_suspend_port);
3028EXPORT_SYMBOL(serial8250_resume_port);
3029
3030MODULE_LICENSE("GPL");
Adrian Bunkd87a6d92008-07-16 21:53:31 +01003031MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
3033module_param(share_irqs, uint, 0644);
3034MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
3035 " (unsafe)");
3036
Dave Jonesa61c2d72006-01-07 23:18:19 +00003037module_param(nr_uarts, uint, 0644);
3038MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040#ifdef CONFIG_SERIAL_8250_RSA
3041module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
3042MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
3043#endif
3044MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);