x86/topology: Update the 'cpu cores' field in /proc/cpuinfo correctly across CPU...
[cris-mirror.git] / drivers / tty / serial / 8250 / 8250_port.c
blob1328c7e7010844ac50a38496df1c816067231afd
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Base port operations for 8250/16550-type serial ports
5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 * Split from 8250_core.c, Copyright (C) 2001 Russell King.
8 * A note about mapbase / membase
10 * mapbase is the physical address of the IO port.
11 * membase is an 'ioremapped' cookie.
14 #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
15 #define SUPPORT_SYSRQ
16 #endif
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/ioport.h>
21 #include <linux/init.h>
22 #include <linux/console.h>
23 #include <linux/sysrq.h>
24 #include <linux/delay.h>
25 #include <linux/platform_device.h>
26 #include <linux/tty.h>
27 #include <linux/ratelimit.h>
28 #include <linux/tty_flip.h>
29 #include <linux/serial.h>
30 #include <linux/serial_8250.h>
31 #include <linux/nmi.h>
32 #include <linux/mutex.h>
33 #include <linux/slab.h>
34 #include <linux/uaccess.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/ktime.h>
38 #include <asm/io.h>
39 #include <asm/irq.h>
41 #include "8250.h"
44 * These are definitions for the Exar XR17V35X and XR17(C|D)15X
46 #define UART_EXAR_INT0 0x80
47 #define UART_EXAR_SLEEP 0x8b /* Sleep mode */
48 #define UART_EXAR_DVID 0x8d /* Device identification */
51 * Debugging.
53 #if 0
54 #define DEBUG_AUTOCONF(fmt...) printk(fmt)
55 #else
56 #define DEBUG_AUTOCONF(fmt...) do { } while (0)
57 #endif
59 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
62 * Here we define the default xmit fifo size used for each type of UART.
64 static const struct serial8250_config uart_config[] = {
65 [PORT_UNKNOWN] = {
66 .name = "unknown",
67 .fifo_size = 1,
68 .tx_loadsz = 1,
70 [PORT_8250] = {
71 .name = "8250",
72 .fifo_size = 1,
73 .tx_loadsz = 1,
75 [PORT_16450] = {
76 .name = "16450",
77 .fifo_size = 1,
78 .tx_loadsz = 1,
80 [PORT_16550] = {
81 .name = "16550",
82 .fifo_size = 1,
83 .tx_loadsz = 1,
85 [PORT_16550A] = {
86 .name = "16550A",
87 .fifo_size = 16,
88 .tx_loadsz = 16,
89 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
90 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
91 .rxtrig_bytes = {1, 4, 8, 14},
92 .flags = UART_CAP_FIFO,
94 [PORT_CIRRUS] = {
95 .name = "Cirrus",
96 .fifo_size = 1,
97 .tx_loadsz = 1,
99 [PORT_16650] = {
100 .name = "ST16650",
101 .fifo_size = 1,
102 .tx_loadsz = 1,
103 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
105 [PORT_16650V2] = {
106 .name = "ST16650V2",
107 .fifo_size = 32,
108 .tx_loadsz = 16,
109 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
110 UART_FCR_T_TRIG_00,
111 .rxtrig_bytes = {8, 16, 24, 28},
112 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
114 [PORT_16750] = {
115 .name = "TI16750",
116 .fifo_size = 64,
117 .tx_loadsz = 64,
118 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
119 UART_FCR7_64BYTE,
120 .rxtrig_bytes = {1, 16, 32, 56},
121 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
123 [PORT_STARTECH] = {
124 .name = "Startech",
125 .fifo_size = 1,
126 .tx_loadsz = 1,
128 [PORT_16C950] = {
129 .name = "16C950/954",
130 .fifo_size = 128,
131 .tx_loadsz = 128,
132 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
133 /* UART_CAP_EFR breaks billionon CF bluetooth card. */
134 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
136 [PORT_16654] = {
137 .name = "ST16654",
138 .fifo_size = 64,
139 .tx_loadsz = 32,
140 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
141 UART_FCR_T_TRIG_10,
142 .rxtrig_bytes = {8, 16, 56, 60},
143 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
145 [PORT_16850] = {
146 .name = "XR16850",
147 .fifo_size = 128,
148 .tx_loadsz = 128,
149 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
150 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
152 [PORT_RSA] = {
153 .name = "RSA",
154 .fifo_size = 2048,
155 .tx_loadsz = 2048,
156 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
157 .flags = UART_CAP_FIFO,
159 [PORT_NS16550A] = {
160 .name = "NS16550A",
161 .fifo_size = 16,
162 .tx_loadsz = 16,
163 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
164 .flags = UART_CAP_FIFO | UART_NATSEMI,
166 [PORT_XSCALE] = {
167 .name = "XScale",
168 .fifo_size = 32,
169 .tx_loadsz = 32,
170 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
171 .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
173 [PORT_OCTEON] = {
174 .name = "OCTEON",
175 .fifo_size = 64,
176 .tx_loadsz = 64,
177 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
178 .flags = UART_CAP_FIFO,
180 [PORT_AR7] = {
181 .name = "AR7",
182 .fifo_size = 16,
183 .tx_loadsz = 16,
184 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
185 .flags = UART_CAP_FIFO /* | UART_CAP_AFE */,
187 [PORT_U6_16550A] = {
188 .name = "U6_16550A",
189 .fifo_size = 64,
190 .tx_loadsz = 64,
191 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
192 .flags = UART_CAP_FIFO | UART_CAP_AFE,
194 [PORT_TEGRA] = {
195 .name = "Tegra",
196 .fifo_size = 32,
197 .tx_loadsz = 8,
198 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
199 UART_FCR_T_TRIG_01,
200 .rxtrig_bytes = {1, 4, 8, 14},
201 .flags = UART_CAP_FIFO | UART_CAP_RTOIE,
203 [PORT_XR17D15X] = {
204 .name = "XR17D15X",
205 .fifo_size = 64,
206 .tx_loadsz = 64,
207 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
208 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
209 UART_CAP_SLEEP,
211 [PORT_XR17V35X] = {
212 .name = "XR17V35X",
213 .fifo_size = 256,
214 .tx_loadsz = 256,
215 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11 |
216 UART_FCR_T_TRIG_11,
217 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
218 UART_CAP_SLEEP,
220 [PORT_LPC3220] = {
221 .name = "LPC3220",
222 .fifo_size = 64,
223 .tx_loadsz = 32,
224 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
225 UART_FCR_R_TRIG_00 | UART_FCR_T_TRIG_00,
226 .flags = UART_CAP_FIFO,
228 [PORT_BRCM_TRUMANAGE] = {
229 .name = "TruManage",
230 .fifo_size = 1,
231 .tx_loadsz = 1024,
232 .flags = UART_CAP_HFIFO,
234 [PORT_8250_CIR] = {
235 .name = "CIR port"
237 [PORT_ALTR_16550_F32] = {
238 .name = "Altera 16550 FIFO32",
239 .fifo_size = 32,
240 .tx_loadsz = 32,
241 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
242 .flags = UART_CAP_FIFO | UART_CAP_AFE,
244 [PORT_ALTR_16550_F64] = {
245 .name = "Altera 16550 FIFO64",
246 .fifo_size = 64,
247 .tx_loadsz = 64,
248 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
249 .flags = UART_CAP_FIFO | UART_CAP_AFE,
251 [PORT_ALTR_16550_F128] = {
252 .name = "Altera 16550 FIFO128",
253 .fifo_size = 128,
254 .tx_loadsz = 128,
255 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
256 .flags = UART_CAP_FIFO | UART_CAP_AFE,
259 * tx_loadsz is set to 63-bytes instead of 64-bytes to implement
260 * workaround of errata A-008006 which states that tx_loadsz should
261 * be configured less than Maximum supported fifo bytes.
263 [PORT_16550A_FSL64] = {
264 .name = "16550A_FSL64",
265 .fifo_size = 64,
266 .tx_loadsz = 63,
267 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
268 UART_FCR7_64BYTE,
269 .flags = UART_CAP_FIFO,
271 [PORT_RT2880] = {
272 .name = "Palmchip BK-3103",
273 .fifo_size = 16,
274 .tx_loadsz = 16,
275 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
276 .rxtrig_bytes = {1, 4, 8, 14},
277 .flags = UART_CAP_FIFO,
279 [PORT_DA830] = {
280 .name = "TI DA8xx/66AK2x",
281 .fifo_size = 16,
282 .tx_loadsz = 16,
283 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
284 UART_FCR_R_TRIG_10,
285 .rxtrig_bytes = {1, 4, 8, 14},
286 .flags = UART_CAP_FIFO | UART_CAP_AFE,
288 [PORT_MTK_BTIF] = {
289 .name = "MediaTek BTIF",
290 .fifo_size = 16,
291 .tx_loadsz = 16,
292 .fcr = UART_FCR_ENABLE_FIFO |
293 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
294 .flags = UART_CAP_FIFO,
298 /* Uart divisor latch read */
299 static int default_serial_dl_read(struct uart_8250_port *up)
301 return serial_in(up, UART_DLL) | serial_in(up, UART_DLM) << 8;
304 /* Uart divisor latch write */
305 static void default_serial_dl_write(struct uart_8250_port *up, int value)
307 serial_out(up, UART_DLL, value & 0xff);
308 serial_out(up, UART_DLM, value >> 8 & 0xff);
311 #ifdef CONFIG_SERIAL_8250_RT288X
313 /* Au1x00/RT288x UART hardware has a weird register layout */
314 static const s8 au_io_in_map[8] = {
315 0, /* UART_RX */
316 2, /* UART_IER */
317 3, /* UART_IIR */
318 5, /* UART_LCR */
319 6, /* UART_MCR */
320 7, /* UART_LSR */
321 8, /* UART_MSR */
322 -1, /* UART_SCR (unmapped) */
325 static const s8 au_io_out_map[8] = {
326 1, /* UART_TX */
327 2, /* UART_IER */
328 4, /* UART_FCR */
329 5, /* UART_LCR */
330 6, /* UART_MCR */
331 -1, /* UART_LSR (unmapped) */
332 -1, /* UART_MSR (unmapped) */
333 -1, /* UART_SCR (unmapped) */
336 unsigned int au_serial_in(struct uart_port *p, int offset)
338 if (offset >= ARRAY_SIZE(au_io_in_map))
339 return UINT_MAX;
340 offset = au_io_in_map[offset];
341 if (offset < 0)
342 return UINT_MAX;
343 return __raw_readl(p->membase + (offset << p->regshift));
346 void au_serial_out(struct uart_port *p, int offset, int value)
348 if (offset >= ARRAY_SIZE(au_io_out_map))
349 return;
350 offset = au_io_out_map[offset];
351 if (offset < 0)
352 return;
353 __raw_writel(value, p->membase + (offset << p->regshift));
356 /* Au1x00 haven't got a standard divisor latch */
357 static int au_serial_dl_read(struct uart_8250_port *up)
359 return __raw_readl(up->port.membase + 0x28);
362 static void au_serial_dl_write(struct uart_8250_port *up, int value)
364 __raw_writel(value, up->port.membase + 0x28);
367 #endif
369 static unsigned int hub6_serial_in(struct uart_port *p, int offset)
371 offset = offset << p->regshift;
372 outb(p->hub6 - 1 + offset, p->iobase);
373 return inb(p->iobase + 1);
376 static void hub6_serial_out(struct uart_port *p, int offset, int value)
378 offset = offset << p->regshift;
379 outb(p->hub6 - 1 + offset, p->iobase);
380 outb(value, p->iobase + 1);
383 static unsigned int mem_serial_in(struct uart_port *p, int offset)
385 offset = offset << p->regshift;
386 return readb(p->membase + offset);
389 static void mem_serial_out(struct uart_port *p, int offset, int value)
391 offset = offset << p->regshift;
392 writeb(value, p->membase + offset);
395 static void mem16_serial_out(struct uart_port *p, int offset, int value)
397 offset = offset << p->regshift;
398 writew(value, p->membase + offset);
401 static unsigned int mem16_serial_in(struct uart_port *p, int offset)
403 offset = offset << p->regshift;
404 return readw(p->membase + offset);
407 static void mem32_serial_out(struct uart_port *p, int offset, int value)
409 offset = offset << p->regshift;
410 writel(value, p->membase + offset);
413 static unsigned int mem32_serial_in(struct uart_port *p, int offset)
415 offset = offset << p->regshift;
416 return readl(p->membase + offset);
419 static void mem32be_serial_out(struct uart_port *p, int offset, int value)
421 offset = offset << p->regshift;
422 iowrite32be(value, p->membase + offset);
425 static unsigned int mem32be_serial_in(struct uart_port *p, int offset)
427 offset = offset << p->regshift;
428 return ioread32be(p->membase + offset);
431 static unsigned int io_serial_in(struct uart_port *p, int offset)
433 offset = offset << p->regshift;
434 return inb(p->iobase + offset);
437 static void io_serial_out(struct uart_port *p, int offset, int value)
439 offset = offset << p->regshift;
440 outb(value, p->iobase + offset);
443 static int serial8250_default_handle_irq(struct uart_port *port);
445 static void set_io_from_upio(struct uart_port *p)
447 struct uart_8250_port *up = up_to_u8250p(p);
449 up->dl_read = default_serial_dl_read;
450 up->dl_write = default_serial_dl_write;
452 switch (p->iotype) {
453 case UPIO_HUB6:
454 p->serial_in = hub6_serial_in;
455 p->serial_out = hub6_serial_out;
456 break;
458 case UPIO_MEM:
459 p->serial_in = mem_serial_in;
460 p->serial_out = mem_serial_out;
461 break;
463 case UPIO_MEM16:
464 p->serial_in = mem16_serial_in;
465 p->serial_out = mem16_serial_out;
466 break;
468 case UPIO_MEM32:
469 p->serial_in = mem32_serial_in;
470 p->serial_out = mem32_serial_out;
471 break;
473 case UPIO_MEM32BE:
474 p->serial_in = mem32be_serial_in;
475 p->serial_out = mem32be_serial_out;
476 break;
478 #ifdef CONFIG_SERIAL_8250_RT288X
479 case UPIO_AU:
480 p->serial_in = au_serial_in;
481 p->serial_out = au_serial_out;
482 up->dl_read = au_serial_dl_read;
483 up->dl_write = au_serial_dl_write;
484 break;
485 #endif
487 default:
488 p->serial_in = io_serial_in;
489 p->serial_out = io_serial_out;
490 break;
492 /* Remember loaded iotype */
493 up->cur_iotype = p->iotype;
494 p->handle_irq = serial8250_default_handle_irq;
497 static void
498 serial_port_out_sync(struct uart_port *p, int offset, int value)
500 switch (p->iotype) {
501 case UPIO_MEM:
502 case UPIO_MEM16:
503 case UPIO_MEM32:
504 case UPIO_MEM32BE:
505 case UPIO_AU:
506 p->serial_out(p, offset, value);
507 p->serial_in(p, UART_LCR); /* safe, no side-effects */
508 break;
509 default:
510 p->serial_out(p, offset, value);
515 * For the 16C950
517 static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
519 serial_out(up, UART_SCR, offset);
520 serial_out(up, UART_ICR, value);
523 static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
525 unsigned int value;
527 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
528 serial_out(up, UART_SCR, offset);
529 value = serial_in(up, UART_ICR);
530 serial_icr_write(up, UART_ACR, up->acr);
532 return value;
536 * FIFO support.
538 static void serial8250_clear_fifos(struct uart_8250_port *p)
540 if (p->capabilities & UART_CAP_FIFO) {
541 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
542 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
543 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
544 serial_out(p, UART_FCR, 0);
548 static inline void serial8250_em485_rts_after_send(struct uart_8250_port *p)
550 unsigned char mcr = serial8250_in_MCR(p);
552 if (p->port.rs485.flags & SER_RS485_RTS_AFTER_SEND)
553 mcr |= UART_MCR_RTS;
554 else
555 mcr &= ~UART_MCR_RTS;
556 serial8250_out_MCR(p, mcr);
559 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t);
560 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t);
562 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
564 serial8250_clear_fifos(p);
565 serial_out(p, UART_FCR, p->fcr);
567 EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);
569 void serial8250_rpm_get(struct uart_8250_port *p)
571 if (!(p->capabilities & UART_CAP_RPM))
572 return;
573 pm_runtime_get_sync(p->port.dev);
575 EXPORT_SYMBOL_GPL(serial8250_rpm_get);
577 void serial8250_rpm_put(struct uart_8250_port *p)
579 if (!(p->capabilities & UART_CAP_RPM))
580 return;
581 pm_runtime_mark_last_busy(p->port.dev);
582 pm_runtime_put_autosuspend(p->port.dev);
584 EXPORT_SYMBOL_GPL(serial8250_rpm_put);
587 * serial8250_em485_init() - put uart_8250_port into rs485 emulating
588 * @p: uart_8250_port port instance
590 * The function is used to start rs485 software emulating on the
591 * &struct uart_8250_port* @p. Namely, RTS is switched before/after
592 * transmission. The function is idempotent, so it is safe to call it
593 * multiple times.
595 * The caller MUST enable interrupt on empty shift register before
596 * calling serial8250_em485_init(). This interrupt is not a part of
597 * 8250 standard, but implementation defined.
599 * The function is supposed to be called from .rs485_config callback
600 * or from any other callback protected with p->port.lock spinlock.
602 * See also serial8250_em485_destroy()
604 * Return 0 - success, -errno - otherwise
606 int serial8250_em485_init(struct uart_8250_port *p)
608 if (p->em485)
609 return 0;
611 p->em485 = kmalloc(sizeof(struct uart_8250_em485), GFP_ATOMIC);
612 if (!p->em485)
613 return -ENOMEM;
615 hrtimer_init(&p->em485->stop_tx_timer, CLOCK_MONOTONIC,
616 HRTIMER_MODE_REL);
617 hrtimer_init(&p->em485->start_tx_timer, CLOCK_MONOTONIC,
618 HRTIMER_MODE_REL);
619 p->em485->stop_tx_timer.function = &serial8250_em485_handle_stop_tx;
620 p->em485->start_tx_timer.function = &serial8250_em485_handle_start_tx;
621 p->em485->port = p;
622 p->em485->active_timer = NULL;
623 serial8250_em485_rts_after_send(p);
625 return 0;
627 EXPORT_SYMBOL_GPL(serial8250_em485_init);
630 * serial8250_em485_destroy() - put uart_8250_port into normal state
631 * @p: uart_8250_port port instance
633 * The function is used to stop rs485 software emulating on the
634 * &struct uart_8250_port* @p. The function is idempotent, so it is safe to
635 * call it multiple times.
637 * The function is supposed to be called from .rs485_config callback
638 * or from any other callback protected with p->port.lock spinlock.
640 * See also serial8250_em485_init()
642 void serial8250_em485_destroy(struct uart_8250_port *p)
644 if (!p->em485)
645 return;
647 hrtimer_cancel(&p->em485->start_tx_timer);
648 hrtimer_cancel(&p->em485->stop_tx_timer);
650 kfree(p->em485);
651 p->em485 = NULL;
653 EXPORT_SYMBOL_GPL(serial8250_em485_destroy);
656 * These two wrappers ensure that enable_runtime_pm_tx() can be called more than
657 * once and disable_runtime_pm_tx() will still disable RPM because the fifo is
658 * empty and the HW can idle again.
660 void serial8250_rpm_get_tx(struct uart_8250_port *p)
662 unsigned char rpm_active;
664 if (!(p->capabilities & UART_CAP_RPM))
665 return;
667 rpm_active = xchg(&p->rpm_tx_active, 1);
668 if (rpm_active)
669 return;
670 pm_runtime_get_sync(p->port.dev);
672 EXPORT_SYMBOL_GPL(serial8250_rpm_get_tx);
674 void serial8250_rpm_put_tx(struct uart_8250_port *p)
676 unsigned char rpm_active;
678 if (!(p->capabilities & UART_CAP_RPM))
679 return;
681 rpm_active = xchg(&p->rpm_tx_active, 0);
682 if (!rpm_active)
683 return;
684 pm_runtime_mark_last_busy(p->port.dev);
685 pm_runtime_put_autosuspend(p->port.dev);
687 EXPORT_SYMBOL_GPL(serial8250_rpm_put_tx);
690 * IER sleep support. UARTs which have EFRs need the "extended
691 * capability" bit enabled. Note that on XR16C850s, we need to
692 * reset LCR to write to IER.
694 static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
696 unsigned char lcr = 0, efr = 0;
698 * Exar UARTs have a SLEEP register that enables or disables
699 * each UART to enter sleep mode separately. On the XR17V35x the
700 * register is accessible to each UART at the UART_EXAR_SLEEP
701 * offset but the UART channel may only write to the corresponding
702 * bit.
704 serial8250_rpm_get(p);
705 if ((p->port.type == PORT_XR17V35X) ||
706 (p->port.type == PORT_XR17D15X)) {
707 serial_out(p, UART_EXAR_SLEEP, sleep ? 0xff : 0);
708 goto out;
711 if (p->capabilities & UART_CAP_SLEEP) {
712 if (p->capabilities & UART_CAP_EFR) {
713 lcr = serial_in(p, UART_LCR);
714 efr = serial_in(p, UART_EFR);
715 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
716 serial_out(p, UART_EFR, UART_EFR_ECB);
717 serial_out(p, UART_LCR, 0);
719 serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
720 if (p->capabilities & UART_CAP_EFR) {
721 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
722 serial_out(p, UART_EFR, efr);
723 serial_out(p, UART_LCR, lcr);
726 out:
727 serial8250_rpm_put(p);
730 #ifdef CONFIG_SERIAL_8250_RSA
732 * Attempts to turn on the RSA FIFO. Returns zero on failure.
733 * We set the port uart clock rate if we succeed.
735 static int __enable_rsa(struct uart_8250_port *up)
737 unsigned char mode;
738 int result;
740 mode = serial_in(up, UART_RSA_MSR);
741 result = mode & UART_RSA_MSR_FIFO;
743 if (!result) {
744 serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
745 mode = serial_in(up, UART_RSA_MSR);
746 result = mode & UART_RSA_MSR_FIFO;
749 if (result)
750 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
752 return result;
755 static void enable_rsa(struct uart_8250_port *up)
757 if (up->port.type == PORT_RSA) {
758 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
759 spin_lock_irq(&up->port.lock);
760 __enable_rsa(up);
761 spin_unlock_irq(&up->port.lock);
763 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
764 serial_out(up, UART_RSA_FRR, 0);
769 * Attempts to turn off the RSA FIFO. Returns zero on failure.
770 * It is unknown why interrupts were disabled in here. However,
771 * the caller is expected to preserve this behaviour by grabbing
772 * the spinlock before calling this function.
774 static void disable_rsa(struct uart_8250_port *up)
776 unsigned char mode;
777 int result;
779 if (up->port.type == PORT_RSA &&
780 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
781 spin_lock_irq(&up->port.lock);
783 mode = serial_in(up, UART_RSA_MSR);
784 result = !(mode & UART_RSA_MSR_FIFO);
786 if (!result) {
787 serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
788 mode = serial_in(up, UART_RSA_MSR);
789 result = !(mode & UART_RSA_MSR_FIFO);
792 if (result)
793 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
794 spin_unlock_irq(&up->port.lock);
797 #endif /* CONFIG_SERIAL_8250_RSA */
800 * This is a quickie test to see how big the FIFO is.
801 * It doesn't work at all the time, more's the pity.
803 static int size_fifo(struct uart_8250_port *up)
805 unsigned char old_fcr, old_mcr, old_lcr;
806 unsigned short old_dl;
807 int count;
809 old_lcr = serial_in(up, UART_LCR);
810 serial_out(up, UART_LCR, 0);
811 old_fcr = serial_in(up, UART_FCR);
812 old_mcr = serial8250_in_MCR(up);
813 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
814 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
815 serial8250_out_MCR(up, UART_MCR_LOOP);
816 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
817 old_dl = serial_dl_read(up);
818 serial_dl_write(up, 0x0001);
819 serial_out(up, UART_LCR, 0x03);
820 for (count = 0; count < 256; count++)
821 serial_out(up, UART_TX, count);
822 mdelay(20);/* FIXME - schedule_timeout */
823 for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
824 (count < 256); count++)
825 serial_in(up, UART_RX);
826 serial_out(up, UART_FCR, old_fcr);
827 serial8250_out_MCR(up, old_mcr);
828 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
829 serial_dl_write(up, old_dl);
830 serial_out(up, UART_LCR, old_lcr);
832 return count;
836 * Read UART ID using the divisor method - set DLL and DLM to zero
837 * and the revision will be in DLL and device type in DLM. We
838 * preserve the device state across this.
840 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
842 unsigned char old_lcr;
843 unsigned int id, old_dl;
845 old_lcr = serial_in(p, UART_LCR);
846 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
847 old_dl = serial_dl_read(p);
848 serial_dl_write(p, 0);
849 id = serial_dl_read(p);
850 serial_dl_write(p, old_dl);
852 serial_out(p, UART_LCR, old_lcr);
854 return id;
858 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
859 * When this function is called we know it is at least a StarTech
860 * 16650 V2, but it might be one of several StarTech UARTs, or one of
861 * its clones. (We treat the broken original StarTech 16650 V1 as a
862 * 16550, and why not? Startech doesn't seem to even acknowledge its
863 * existence.)
865 * What evil have men's minds wrought...
867 static void autoconfig_has_efr(struct uart_8250_port *up)
869 unsigned int id1, id2, id3, rev;
872 * Everything with an EFR has SLEEP
874 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
877 * First we check to see if it's an Oxford Semiconductor UART.
879 * If we have to do this here because some non-National
880 * Semiconductor clone chips lock up if you try writing to the
881 * LSR register (which serial_icr_read does)
885 * Check for Oxford Semiconductor 16C950.
887 * EFR [4] must be set else this test fails.
889 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
890 * claims that it's needed for 952 dual UART's (which are not
891 * recommended for new designs).
893 up->acr = 0;
894 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
895 serial_out(up, UART_EFR, UART_EFR_ECB);
896 serial_out(up, UART_LCR, 0x00);
897 id1 = serial_icr_read(up, UART_ID1);
898 id2 = serial_icr_read(up, UART_ID2);
899 id3 = serial_icr_read(up, UART_ID3);
900 rev = serial_icr_read(up, UART_REV);
902 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
904 if (id1 == 0x16 && id2 == 0xC9 &&
905 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
906 up->port.type = PORT_16C950;
909 * Enable work around for the Oxford Semiconductor 952 rev B
910 * chip which causes it to seriously miscalculate baud rates
911 * when DLL is 0.
913 if (id3 == 0x52 && rev == 0x01)
914 up->bugs |= UART_BUG_QUOT;
915 return;
919 * We check for a XR16C850 by setting DLL and DLM to 0, and then
920 * reading back DLL and DLM. The chip type depends on the DLM
921 * value read back:
922 * 0x10 - XR16C850 and the DLL contains the chip revision.
923 * 0x12 - XR16C2850.
924 * 0x14 - XR16C854.
926 id1 = autoconfig_read_divisor_id(up);
927 DEBUG_AUTOCONF("850id=%04x ", id1);
929 id2 = id1 >> 8;
930 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
931 up->port.type = PORT_16850;
932 return;
936 * It wasn't an XR16C850.
938 * We distinguish between the '654 and the '650 by counting
939 * how many bytes are in the FIFO. I'm using this for now,
940 * since that's the technique that was sent to me in the
941 * serial driver update, but I'm not convinced this works.
942 * I've had problems doing this in the past. -TYT
944 if (size_fifo(up) == 64)
945 up->port.type = PORT_16654;
946 else
947 up->port.type = PORT_16650V2;
951 * We detected a chip without a FIFO. Only two fall into
952 * this category - the original 8250 and the 16450. The
953 * 16450 has a scratch register (accessible with LCR=0)
955 static void autoconfig_8250(struct uart_8250_port *up)
957 unsigned char scratch, status1, status2;
959 up->port.type = PORT_8250;
961 scratch = serial_in(up, UART_SCR);
962 serial_out(up, UART_SCR, 0xa5);
963 status1 = serial_in(up, UART_SCR);
964 serial_out(up, UART_SCR, 0x5a);
965 status2 = serial_in(up, UART_SCR);
966 serial_out(up, UART_SCR, scratch);
968 if (status1 == 0xa5 && status2 == 0x5a)
969 up->port.type = PORT_16450;
972 static int broken_efr(struct uart_8250_port *up)
975 * Exar ST16C2550 "A2" devices incorrectly detect as
976 * having an EFR, and report an ID of 0x0201. See
977 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
979 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
980 return 1;
982 return 0;
986 * We know that the chip has FIFOs. Does it have an EFR? The
987 * EFR is located in the same register position as the IIR and
988 * we know the top two bits of the IIR are currently set. The
989 * EFR should contain zero. Try to read the EFR.
991 static void autoconfig_16550a(struct uart_8250_port *up)
993 unsigned char status1, status2;
994 unsigned int iersave;
996 up->port.type = PORT_16550A;
997 up->capabilities |= UART_CAP_FIFO;
1000 * XR17V35x UARTs have an extra divisor register, DLD
1001 * that gets enabled with when DLAB is set which will
1002 * cause the device to incorrectly match and assign
1003 * port type to PORT_16650. The EFR for this UART is
1004 * found at offset 0x09. Instead check the Deice ID (DVID)
1005 * register for a 2, 4 or 8 port UART.
1007 if (up->port.flags & UPF_EXAR_EFR) {
1008 status1 = serial_in(up, UART_EXAR_DVID);
1009 if (status1 == 0x82 || status1 == 0x84 || status1 == 0x88) {
1010 DEBUG_AUTOCONF("Exar XR17V35x ");
1011 up->port.type = PORT_XR17V35X;
1012 up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
1013 UART_CAP_SLEEP;
1015 return;
1021 * Check for presence of the EFR when DLAB is set.
1022 * Only ST16C650V1 UARTs pass this test.
1024 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1025 if (serial_in(up, UART_EFR) == 0) {
1026 serial_out(up, UART_EFR, 0xA8);
1027 if (serial_in(up, UART_EFR) != 0) {
1028 DEBUG_AUTOCONF("EFRv1 ");
1029 up->port.type = PORT_16650;
1030 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
1031 } else {
1032 serial_out(up, UART_LCR, 0);
1033 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
1034 UART_FCR7_64BYTE);
1035 status1 = serial_in(up, UART_IIR) >> 5;
1036 serial_out(up, UART_FCR, 0);
1037 serial_out(up, UART_LCR, 0);
1039 if (status1 == 7)
1040 up->port.type = PORT_16550A_FSL64;
1041 else
1042 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
1044 serial_out(up, UART_EFR, 0);
1045 return;
1049 * Maybe it requires 0xbf to be written to the LCR.
1050 * (other ST16C650V2 UARTs, TI16C752A, etc)
1052 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1053 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
1054 DEBUG_AUTOCONF("EFRv2 ");
1055 autoconfig_has_efr(up);
1056 return;
1060 * Check for a National Semiconductor SuperIO chip.
1061 * Attempt to switch to bank 2, read the value of the LOOP bit
1062 * from EXCR1. Switch back to bank 0, change it in MCR. Then
1063 * switch back to bank 2, read it from EXCR1 again and check
1064 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
1066 serial_out(up, UART_LCR, 0);
1067 status1 = serial8250_in_MCR(up);
1068 serial_out(up, UART_LCR, 0xE0);
1069 status2 = serial_in(up, 0x02); /* EXCR1 */
1071 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
1072 serial_out(up, UART_LCR, 0);
1073 serial8250_out_MCR(up, status1 ^ UART_MCR_LOOP);
1074 serial_out(up, UART_LCR, 0xE0);
1075 status2 = serial_in(up, 0x02); /* EXCR1 */
1076 serial_out(up, UART_LCR, 0);
1077 serial8250_out_MCR(up, status1);
1079 if ((status2 ^ status1) & UART_MCR_LOOP) {
1080 unsigned short quot;
1082 serial_out(up, UART_LCR, 0xE0);
1084 quot = serial_dl_read(up);
1085 quot <<= 3;
1087 if (ns16550a_goto_highspeed(up))
1088 serial_dl_write(up, quot);
1090 serial_out(up, UART_LCR, 0);
1092 up->port.uartclk = 921600*16;
1093 up->port.type = PORT_NS16550A;
1094 up->capabilities |= UART_NATSEMI;
1095 return;
1100 * No EFR. Try to detect a TI16750, which only sets bit 5 of
1101 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1102 * Try setting it with and without DLAB set. Cheap clones
1103 * set bit 5 without DLAB set.
1105 serial_out(up, UART_LCR, 0);
1106 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1107 status1 = serial_in(up, UART_IIR) >> 5;
1108 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1109 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1110 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1111 status2 = serial_in(up, UART_IIR) >> 5;
1112 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1113 serial_out(up, UART_LCR, 0);
1115 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1117 if (status1 == 6 && status2 == 7) {
1118 up->port.type = PORT_16750;
1119 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1120 return;
1124 * Try writing and reading the UART_IER_UUE bit (b6).
1125 * If it works, this is probably one of the Xscale platform's
1126 * internal UARTs.
1127 * We're going to explicitly set the UUE bit to 0 before
1128 * trying to write and read a 1 just to make sure it's not
1129 * already a 1 and maybe locked there before we even start start.
1131 iersave = serial_in(up, UART_IER);
1132 serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
1133 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1135 * OK it's in a known zero state, try writing and reading
1136 * without disturbing the current state of the other bits.
1138 serial_out(up, UART_IER, iersave | UART_IER_UUE);
1139 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1141 * It's an Xscale.
1142 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1144 DEBUG_AUTOCONF("Xscale ");
1145 up->port.type = PORT_XSCALE;
1146 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
1147 return;
1149 } else {
1151 * If we got here we couldn't force the IER_UUE bit to 0.
1152 * Log it and continue.
1154 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1156 serial_out(up, UART_IER, iersave);
1159 * Exar uarts have EFR in a weird location
1161 if (up->port.flags & UPF_EXAR_EFR) {
1162 DEBUG_AUTOCONF("Exar XR17D15x ");
1163 up->port.type = PORT_XR17D15X;
1164 up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
1165 UART_CAP_SLEEP;
1167 return;
1171 * We distinguish between 16550A and U6 16550A by counting
1172 * how many bytes are in the FIFO.
1174 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1175 up->port.type = PORT_U6_16550A;
1176 up->capabilities |= UART_CAP_AFE;
1181 * This routine is called by rs_init() to initialize a specific serial
1182 * port. It determines what type of UART chip this serial port is
1183 * using: 8250, 16450, 16550, 16550A. The important question is
1184 * whether or not this UART is a 16550A or not, since this will
1185 * determine whether or not we can use its FIFO features or not.
1187 static void autoconfig(struct uart_8250_port *up)
1189 unsigned char status1, scratch, scratch2, scratch3;
1190 unsigned char save_lcr, save_mcr;
1191 struct uart_port *port = &up->port;
1192 unsigned long flags;
1193 unsigned int old_capabilities;
1195 if (!port->iobase && !port->mapbase && !port->membase)
1196 return;
1198 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
1199 serial_index(port), port->iobase, port->membase);
1202 * We really do need global IRQs disabled here - we're going to
1203 * be frobbing the chips IRQ enable register to see if it exists.
1205 spin_lock_irqsave(&port->lock, flags);
1207 up->capabilities = 0;
1208 up->bugs = 0;
1210 if (!(port->flags & UPF_BUGGY_UART)) {
1212 * Do a simple existence test first; if we fail this,
1213 * there's no point trying anything else.
1215 * 0x80 is used as a nonsense port to prevent against
1216 * false positives due to ISA bus float. The
1217 * assumption is that 0x80 is a non-existent port;
1218 * which should be safe since include/asm/io.h also
1219 * makes this assumption.
1221 * Note: this is safe as long as MCR bit 4 is clear
1222 * and the device is in "PC" mode.
1224 scratch = serial_in(up, UART_IER);
1225 serial_out(up, UART_IER, 0);
1226 #ifdef __i386__
1227 outb(0xff, 0x080);
1228 #endif
1230 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1231 * 16C754B) allow only to modify them if an EFR bit is set.
1233 scratch2 = serial_in(up, UART_IER) & 0x0f;
1234 serial_out(up, UART_IER, 0x0F);
1235 #ifdef __i386__
1236 outb(0, 0x080);
1237 #endif
1238 scratch3 = serial_in(up, UART_IER) & 0x0f;
1239 serial_out(up, UART_IER, scratch);
1240 if (scratch2 != 0 || scratch3 != 0x0F) {
1242 * We failed; there's nothing here
1244 spin_unlock_irqrestore(&port->lock, flags);
1245 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1246 scratch2, scratch3);
1247 goto out;
1251 save_mcr = serial8250_in_MCR(up);
1252 save_lcr = serial_in(up, UART_LCR);
1255 * Check to see if a UART is really there. Certain broken
1256 * internal modems based on the Rockwell chipset fail this
1257 * test, because they apparently don't implement the loopback
1258 * test mode. So this test is skipped on the COM 1 through
1259 * COM 4 ports. This *should* be safe, since no board
1260 * manufacturer would be stupid enough to design a board
1261 * that conflicts with COM 1-4 --- we hope!
1263 if (!(port->flags & UPF_SKIP_TEST)) {
1264 serial8250_out_MCR(up, UART_MCR_LOOP | 0x0A);
1265 status1 = serial_in(up, UART_MSR) & 0xF0;
1266 serial8250_out_MCR(up, save_mcr);
1267 if (status1 != 0x90) {
1268 spin_unlock_irqrestore(&port->lock, flags);
1269 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1270 status1);
1271 goto out;
1276 * We're pretty sure there's a port here. Lets find out what
1277 * type of port it is. The IIR top two bits allows us to find
1278 * out if it's 8250 or 16450, 16550, 16550A or later. This
1279 * determines what we test for next.
1281 * We also initialise the EFR (if any) to zero for later. The
1282 * EFR occupies the same register location as the FCR and IIR.
1284 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1285 serial_out(up, UART_EFR, 0);
1286 serial_out(up, UART_LCR, 0);
1288 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1289 scratch = serial_in(up, UART_IIR) >> 6;
1291 switch (scratch) {
1292 case 0:
1293 autoconfig_8250(up);
1294 break;
1295 case 1:
1296 port->type = PORT_UNKNOWN;
1297 break;
1298 case 2:
1299 port->type = PORT_16550;
1300 break;
1301 case 3:
1302 autoconfig_16550a(up);
1303 break;
1306 #ifdef CONFIG_SERIAL_8250_RSA
1308 * Only probe for RSA ports if we got the region.
1310 if (port->type == PORT_16550A && up->probe & UART_PROBE_RSA &&
1311 __enable_rsa(up))
1312 port->type = PORT_RSA;
1313 #endif
1315 serial_out(up, UART_LCR, save_lcr);
1317 port->fifosize = uart_config[up->port.type].fifo_size;
1318 old_capabilities = up->capabilities;
1319 up->capabilities = uart_config[port->type].flags;
1320 up->tx_loadsz = uart_config[port->type].tx_loadsz;
1322 if (port->type == PORT_UNKNOWN)
1323 goto out_lock;
1326 * Reset the UART.
1328 #ifdef CONFIG_SERIAL_8250_RSA
1329 if (port->type == PORT_RSA)
1330 serial_out(up, UART_RSA_FRR, 0);
1331 #endif
1332 serial8250_out_MCR(up, save_mcr);
1333 serial8250_clear_fifos(up);
1334 serial_in(up, UART_RX);
1335 if (up->capabilities & UART_CAP_UUE)
1336 serial_out(up, UART_IER, UART_IER_UUE);
1337 else
1338 serial_out(up, UART_IER, 0);
1340 out_lock:
1341 spin_unlock_irqrestore(&port->lock, flags);
1344 * Check if the device is a Fintek F81216A
1346 if (port->type == PORT_16550A && port->iotype == UPIO_PORT)
1347 fintek_8250_probe(up);
1349 if (up->capabilities != old_capabilities) {
1350 pr_warn("ttyS%d: detected caps %08x should be %08x\n",
1351 serial_index(port), old_capabilities,
1352 up->capabilities);
1354 out:
1355 DEBUG_AUTOCONF("iir=%d ", scratch);
1356 DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
1359 static void autoconfig_irq(struct uart_8250_port *up)
1361 struct uart_port *port = &up->port;
1362 unsigned char save_mcr, save_ier;
1363 unsigned char save_ICP = 0;
1364 unsigned int ICP = 0;
1365 unsigned long irqs;
1366 int irq;
1368 if (port->flags & UPF_FOURPORT) {
1369 ICP = (port->iobase & 0xfe0) | 0x1f;
1370 save_ICP = inb_p(ICP);
1371 outb_p(0x80, ICP);
1372 inb_p(ICP);
1375 if (uart_console(port))
1376 console_lock();
1378 /* forget possible initially masked and pending IRQ */
1379 probe_irq_off(probe_irq_on());
1380 save_mcr = serial8250_in_MCR(up);
1381 save_ier = serial_in(up, UART_IER);
1382 serial8250_out_MCR(up, UART_MCR_OUT1 | UART_MCR_OUT2);
1384 irqs = probe_irq_on();
1385 serial8250_out_MCR(up, 0);
1386 udelay(10);
1387 if (port->flags & UPF_FOURPORT) {
1388 serial8250_out_MCR(up, UART_MCR_DTR | UART_MCR_RTS);
1389 } else {
1390 serial8250_out_MCR(up,
1391 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1393 serial_out(up, UART_IER, 0x0f); /* enable all intrs */
1394 serial_in(up, UART_LSR);
1395 serial_in(up, UART_RX);
1396 serial_in(up, UART_IIR);
1397 serial_in(up, UART_MSR);
1398 serial_out(up, UART_TX, 0xFF);
1399 udelay(20);
1400 irq = probe_irq_off(irqs);
1402 serial8250_out_MCR(up, save_mcr);
1403 serial_out(up, UART_IER, save_ier);
1405 if (port->flags & UPF_FOURPORT)
1406 outb_p(save_ICP, ICP);
1408 if (uart_console(port))
1409 console_unlock();
1411 port->irq = (irq > 0) ? irq : 0;
1414 static void serial8250_stop_rx(struct uart_port *port)
1416 struct uart_8250_port *up = up_to_u8250p(port);
1418 serial8250_rpm_get(up);
1420 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1421 up->port.read_status_mask &= ~UART_LSR_DR;
1422 serial_port_out(port, UART_IER, up->ier);
1424 serial8250_rpm_put(up);
1427 static void __do_stop_tx_rs485(struct uart_8250_port *p)
1429 serial8250_em485_rts_after_send(p);
1432 * Empty the RX FIFO, we are not interested in anything
1433 * received during the half-duplex transmission.
1434 * Enable previously disabled RX interrupts.
1436 if (!(p->port.rs485.flags & SER_RS485_RX_DURING_TX)) {
1437 serial8250_clear_and_reinit_fifos(p);
1439 p->ier |= UART_IER_RLSI | UART_IER_RDI;
1440 serial_port_out(&p->port, UART_IER, p->ier);
1443 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t)
1445 struct uart_8250_em485 *em485;
1446 struct uart_8250_port *p;
1447 unsigned long flags;
1449 em485 = container_of(t, struct uart_8250_em485, stop_tx_timer);
1450 p = em485->port;
1452 serial8250_rpm_get(p);
1453 spin_lock_irqsave(&p->port.lock, flags);
1454 if (em485->active_timer == &em485->stop_tx_timer) {
1455 __do_stop_tx_rs485(p);
1456 em485->active_timer = NULL;
1458 spin_unlock_irqrestore(&p->port.lock, flags);
1459 serial8250_rpm_put(p);
1460 return HRTIMER_NORESTART;
1463 static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec)
1465 long sec = msec / 1000;
1466 long nsec = (msec % 1000) * 1000000;
1467 ktime_t t = ktime_set(sec, nsec);
1469 hrtimer_start(hrt, t, HRTIMER_MODE_REL);
1472 static void __stop_tx_rs485(struct uart_8250_port *p)
1474 struct uart_8250_em485 *em485 = p->em485;
1477 * __do_stop_tx_rs485 is going to set RTS according to config
1478 * AND flush RX FIFO if required.
1480 if (p->port.rs485.delay_rts_after_send > 0) {
1481 em485->active_timer = &em485->stop_tx_timer;
1482 start_hrtimer_ms(&em485->stop_tx_timer,
1483 p->port.rs485.delay_rts_after_send);
1484 } else {
1485 __do_stop_tx_rs485(p);
1489 static inline void __do_stop_tx(struct uart_8250_port *p)
1491 if (p->ier & UART_IER_THRI) {
1492 p->ier &= ~UART_IER_THRI;
1493 serial_out(p, UART_IER, p->ier);
1494 serial8250_rpm_put_tx(p);
1498 static inline void __stop_tx(struct uart_8250_port *p)
1500 struct uart_8250_em485 *em485 = p->em485;
1502 if (em485) {
1503 unsigned char lsr = serial_in(p, UART_LSR);
1505 * To provide required timeing and allow FIFO transfer,
1506 * __stop_tx_rs485() must be called only when both FIFO and
1507 * shift register are empty. It is for device driver to enable
1508 * interrupt on TEMT.
1510 if ((lsr & BOTH_EMPTY) != BOTH_EMPTY)
1511 return;
1513 em485->active_timer = NULL;
1515 __stop_tx_rs485(p);
1517 __do_stop_tx(p);
1520 static void serial8250_stop_tx(struct uart_port *port)
1522 struct uart_8250_port *up = up_to_u8250p(port);
1524 serial8250_rpm_get(up);
1525 __stop_tx(up);
1528 * We really want to stop the transmitter from sending.
1530 if (port->type == PORT_16C950) {
1531 up->acr |= UART_ACR_TXDIS;
1532 serial_icr_write(up, UART_ACR, up->acr);
1534 serial8250_rpm_put(up);
1537 static inline void __start_tx(struct uart_port *port)
1539 struct uart_8250_port *up = up_to_u8250p(port);
1541 if (up->dma && !up->dma->tx_dma(up))
1542 return;
1544 if (!(up->ier & UART_IER_THRI)) {
1545 up->ier |= UART_IER_THRI;
1546 serial_port_out(port, UART_IER, up->ier);
1548 if (up->bugs & UART_BUG_TXEN) {
1549 unsigned char lsr;
1551 lsr = serial_in(up, UART_LSR);
1552 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1553 if (lsr & UART_LSR_THRE)
1554 serial8250_tx_chars(up);
1559 * Re-enable the transmitter if we disabled it.
1561 if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1562 up->acr &= ~UART_ACR_TXDIS;
1563 serial_icr_write(up, UART_ACR, up->acr);
1567 static inline void start_tx_rs485(struct uart_port *port)
1569 struct uart_8250_port *up = up_to_u8250p(port);
1570 struct uart_8250_em485 *em485 = up->em485;
1571 unsigned char mcr;
1573 if (!(up->port.rs485.flags & SER_RS485_RX_DURING_TX))
1574 serial8250_stop_rx(&up->port);
1576 em485->active_timer = NULL;
1578 mcr = serial8250_in_MCR(up);
1579 if (!!(up->port.rs485.flags & SER_RS485_RTS_ON_SEND) !=
1580 !!(mcr & UART_MCR_RTS)) {
1581 if (up->port.rs485.flags & SER_RS485_RTS_ON_SEND)
1582 mcr |= UART_MCR_RTS;
1583 else
1584 mcr &= ~UART_MCR_RTS;
1585 serial8250_out_MCR(up, mcr);
1587 if (up->port.rs485.delay_rts_before_send > 0) {
1588 em485->active_timer = &em485->start_tx_timer;
1589 start_hrtimer_ms(&em485->start_tx_timer,
1590 up->port.rs485.delay_rts_before_send);
1591 return;
1595 __start_tx(port);
1598 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t)
1600 struct uart_8250_em485 *em485;
1601 struct uart_8250_port *p;
1602 unsigned long flags;
1604 em485 = container_of(t, struct uart_8250_em485, start_tx_timer);
1605 p = em485->port;
1607 spin_lock_irqsave(&p->port.lock, flags);
1608 if (em485->active_timer == &em485->start_tx_timer) {
1609 __start_tx(&p->port);
1610 em485->active_timer = NULL;
1612 spin_unlock_irqrestore(&p->port.lock, flags);
1613 return HRTIMER_NORESTART;
1616 static void serial8250_start_tx(struct uart_port *port)
1618 struct uart_8250_port *up = up_to_u8250p(port);
1619 struct uart_8250_em485 *em485 = up->em485;
1621 serial8250_rpm_get_tx(up);
1623 if (em485 &&
1624 em485->active_timer == &em485->start_tx_timer)
1625 return;
1627 if (em485)
1628 start_tx_rs485(port);
1629 else
1630 __start_tx(port);
1633 static void serial8250_throttle(struct uart_port *port)
1635 port->throttle(port);
1638 static void serial8250_unthrottle(struct uart_port *port)
1640 port->unthrottle(port);
1643 static void serial8250_disable_ms(struct uart_port *port)
1645 struct uart_8250_port *up = up_to_u8250p(port);
1647 /* no MSR capabilities */
1648 if (up->bugs & UART_BUG_NOMSR)
1649 return;
1651 up->ier &= ~UART_IER_MSI;
1652 serial_port_out(port, UART_IER, up->ier);
1655 static void serial8250_enable_ms(struct uart_port *port)
1657 struct uart_8250_port *up = up_to_u8250p(port);
1659 /* no MSR capabilities */
1660 if (up->bugs & UART_BUG_NOMSR)
1661 return;
1663 up->ier |= UART_IER_MSI;
1665 serial8250_rpm_get(up);
1666 serial_port_out(port, UART_IER, up->ier);
1667 serial8250_rpm_put(up);
1670 static void serial8250_read_char(struct uart_8250_port *up, unsigned char lsr)
1672 struct uart_port *port = &up->port;
1673 unsigned char ch;
1674 char flag = TTY_NORMAL;
1676 if (likely(lsr & UART_LSR_DR))
1677 ch = serial_in(up, UART_RX);
1678 else
1680 * Intel 82571 has a Serial Over Lan device that will
1681 * set UART_LSR_BI without setting UART_LSR_DR when
1682 * it receives a break. To avoid reading from the
1683 * receive buffer without UART_LSR_DR bit set, we
1684 * just force the read character to be 0
1686 ch = 0;
1688 port->icount.rx++;
1690 lsr |= up->lsr_saved_flags;
1691 up->lsr_saved_flags = 0;
1693 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1694 if (lsr & UART_LSR_BI) {
1695 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1696 port->icount.brk++;
1698 * We do the SysRQ and SAK checking
1699 * here because otherwise the break
1700 * may get masked by ignore_status_mask
1701 * or read_status_mask.
1703 if (uart_handle_break(port))
1704 return;
1705 } else if (lsr & UART_LSR_PE)
1706 port->icount.parity++;
1707 else if (lsr & UART_LSR_FE)
1708 port->icount.frame++;
1709 if (lsr & UART_LSR_OE)
1710 port->icount.overrun++;
1713 * Mask off conditions which should be ignored.
1715 lsr &= port->read_status_mask;
1717 if (lsr & UART_LSR_BI) {
1718 pr_debug("%s: handling break\n", __func__);
1719 flag = TTY_BREAK;
1720 } else if (lsr & UART_LSR_PE)
1721 flag = TTY_PARITY;
1722 else if (lsr & UART_LSR_FE)
1723 flag = TTY_FRAME;
1725 if (uart_handle_sysrq_char(port, ch))
1726 return;
1728 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
1732 * serial8250_rx_chars: processes according to the passed in LSR
1733 * value, and returns the remaining LSR bits not handled
1734 * by this Rx routine.
1736 unsigned char serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
1738 struct uart_port *port = &up->port;
1739 int max_count = 256;
1741 do {
1742 serial8250_read_char(up, lsr);
1743 if (--max_count == 0)
1744 break;
1745 lsr = serial_in(up, UART_LSR);
1746 } while (lsr & (UART_LSR_DR | UART_LSR_BI));
1748 tty_flip_buffer_push(&port->state->port);
1749 return lsr;
1751 EXPORT_SYMBOL_GPL(serial8250_rx_chars);
1753 void serial8250_tx_chars(struct uart_8250_port *up)
1755 struct uart_port *port = &up->port;
1756 struct circ_buf *xmit = &port->state->xmit;
1757 int count;
1759 if (port->x_char) {
1760 serial_out(up, UART_TX, port->x_char);
1761 port->icount.tx++;
1762 port->x_char = 0;
1763 return;
1765 if (uart_tx_stopped(port)) {
1766 serial8250_stop_tx(port);
1767 return;
1769 if (uart_circ_empty(xmit)) {
1770 __stop_tx(up);
1771 return;
1774 count = up->tx_loadsz;
1775 do {
1776 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1777 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1778 port->icount.tx++;
1779 if (uart_circ_empty(xmit))
1780 break;
1781 if ((up->capabilities & UART_CAP_HFIFO) &&
1782 (serial_in(up, UART_LSR) & BOTH_EMPTY) != BOTH_EMPTY)
1783 break;
1784 /* The BCM2835 MINI UART THRE bit is really a not-full bit. */
1785 if ((up->capabilities & UART_CAP_MINI) &&
1786 !(serial_in(up, UART_LSR) & UART_LSR_THRE))
1787 break;
1788 } while (--count > 0);
1790 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1791 uart_write_wakeup(port);
1794 * With RPM enabled, we have to wait until the FIFO is empty before the
1795 * HW can go idle. So we get here once again with empty FIFO and disable
1796 * the interrupt and RPM in __stop_tx()
1798 if (uart_circ_empty(xmit) && !(up->capabilities & UART_CAP_RPM))
1799 __stop_tx(up);
1801 EXPORT_SYMBOL_GPL(serial8250_tx_chars);
1803 /* Caller holds uart port lock */
1804 unsigned int serial8250_modem_status(struct uart_8250_port *up)
1806 struct uart_port *port = &up->port;
1807 unsigned int status = serial_in(up, UART_MSR);
1809 status |= up->msr_saved_flags;
1810 up->msr_saved_flags = 0;
1811 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1812 port->state != NULL) {
1813 if (status & UART_MSR_TERI)
1814 port->icount.rng++;
1815 if (status & UART_MSR_DDSR)
1816 port->icount.dsr++;
1817 if (status & UART_MSR_DDCD)
1818 uart_handle_dcd_change(port, status & UART_MSR_DCD);
1819 if (status & UART_MSR_DCTS)
1820 uart_handle_cts_change(port, status & UART_MSR_CTS);
1822 wake_up_interruptible(&port->state->port.delta_msr_wait);
1825 return status;
1827 EXPORT_SYMBOL_GPL(serial8250_modem_status);
1829 static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir)
1831 switch (iir & 0x3f) {
1832 case UART_IIR_RX_TIMEOUT:
1833 serial8250_rx_dma_flush(up);
1834 /* fall-through */
1835 case UART_IIR_RLSI:
1836 return true;
1838 return up->dma->rx_dma(up);
1842 * This handles the interrupt from one port.
1844 int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
1846 unsigned char status;
1847 unsigned long flags;
1848 struct uart_8250_port *up = up_to_u8250p(port);
1850 if (iir & UART_IIR_NO_INT)
1851 return 0;
1853 spin_lock_irqsave(&port->lock, flags);
1855 status = serial_port_in(port, UART_LSR);
1857 if (status & (UART_LSR_DR | UART_LSR_BI)) {
1858 if (!up->dma || handle_rx_dma(up, iir))
1859 status = serial8250_rx_chars(up, status);
1861 serial8250_modem_status(up);
1862 if ((!up->dma || up->dma->tx_err) && (status & UART_LSR_THRE))
1863 serial8250_tx_chars(up);
1865 spin_unlock_irqrestore(&port->lock, flags);
1866 return 1;
1868 EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1870 static int serial8250_default_handle_irq(struct uart_port *port)
1872 struct uart_8250_port *up = up_to_u8250p(port);
1873 unsigned int iir;
1874 int ret;
1876 serial8250_rpm_get(up);
1878 iir = serial_port_in(port, UART_IIR);
1879 ret = serial8250_handle_irq(port, iir);
1881 serial8250_rpm_put(up);
1882 return ret;
1886 * Newer 16550 compatible parts such as the SC16C650 & Altera 16550 Soft IP
1887 * have a programmable TX threshold that triggers the THRE interrupt in
1888 * the IIR register. In this case, the THRE interrupt indicates the FIFO
1889 * has space available. Load it up with tx_loadsz bytes.
1891 static int serial8250_tx_threshold_handle_irq(struct uart_port *port)
1893 unsigned long flags;
1894 unsigned int iir = serial_port_in(port, UART_IIR);
1896 /* TX Threshold IRQ triggered so load up FIFO */
1897 if ((iir & UART_IIR_ID) == UART_IIR_THRI) {
1898 struct uart_8250_port *up = up_to_u8250p(port);
1900 spin_lock_irqsave(&port->lock, flags);
1901 serial8250_tx_chars(up);
1902 spin_unlock_irqrestore(&port->lock, flags);
1905 iir = serial_port_in(port, UART_IIR);
1906 return serial8250_handle_irq(port, iir);
1909 static unsigned int serial8250_tx_empty(struct uart_port *port)
1911 struct uart_8250_port *up = up_to_u8250p(port);
1912 unsigned long flags;
1913 unsigned int lsr;
1915 serial8250_rpm_get(up);
1917 spin_lock_irqsave(&port->lock, flags);
1918 lsr = serial_port_in(port, UART_LSR);
1919 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1920 spin_unlock_irqrestore(&port->lock, flags);
1922 serial8250_rpm_put(up);
1924 return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
1927 unsigned int serial8250_do_get_mctrl(struct uart_port *port)
1929 struct uart_8250_port *up = up_to_u8250p(port);
1930 unsigned int status;
1931 unsigned int ret;
1933 serial8250_rpm_get(up);
1934 status = serial8250_modem_status(up);
1935 serial8250_rpm_put(up);
1937 ret = 0;
1938 if (status & UART_MSR_DCD)
1939 ret |= TIOCM_CAR;
1940 if (status & UART_MSR_RI)
1941 ret |= TIOCM_RNG;
1942 if (status & UART_MSR_DSR)
1943 ret |= TIOCM_DSR;
1944 if (status & UART_MSR_CTS)
1945 ret |= TIOCM_CTS;
1946 return ret;
1948 EXPORT_SYMBOL_GPL(serial8250_do_get_mctrl);
1950 static unsigned int serial8250_get_mctrl(struct uart_port *port)
1952 if (port->get_mctrl)
1953 return port->get_mctrl(port);
1954 return serial8250_do_get_mctrl(port);
1957 void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl)
1959 struct uart_8250_port *up = up_to_u8250p(port);
1960 unsigned char mcr = 0;
1962 if (mctrl & TIOCM_RTS)
1963 mcr |= UART_MCR_RTS;
1964 if (mctrl & TIOCM_DTR)
1965 mcr |= UART_MCR_DTR;
1966 if (mctrl & TIOCM_OUT1)
1967 mcr |= UART_MCR_OUT1;
1968 if (mctrl & TIOCM_OUT2)
1969 mcr |= UART_MCR_OUT2;
1970 if (mctrl & TIOCM_LOOP)
1971 mcr |= UART_MCR_LOOP;
1973 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1975 serial8250_out_MCR(up, mcr);
1977 EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);
1979 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1981 if (port->set_mctrl)
1982 port->set_mctrl(port, mctrl);
1983 else
1984 serial8250_do_set_mctrl(port, mctrl);
1987 static void serial8250_break_ctl(struct uart_port *port, int break_state)
1989 struct uart_8250_port *up = up_to_u8250p(port);
1990 unsigned long flags;
1992 serial8250_rpm_get(up);
1993 spin_lock_irqsave(&port->lock, flags);
1994 if (break_state == -1)
1995 up->lcr |= UART_LCR_SBC;
1996 else
1997 up->lcr &= ~UART_LCR_SBC;
1998 serial_port_out(port, UART_LCR, up->lcr);
1999 spin_unlock_irqrestore(&port->lock, flags);
2000 serial8250_rpm_put(up);
2004 * Wait for transmitter & holding register to empty
2006 static void wait_for_xmitr(struct uart_8250_port *up, int bits)
2008 unsigned int status, tmout = 10000;
2010 /* Wait up to 10ms for the character(s) to be sent. */
2011 for (;;) {
2012 status = serial_in(up, UART_LSR);
2014 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
2016 if ((status & bits) == bits)
2017 break;
2018 if (--tmout == 0)
2019 break;
2020 udelay(1);
2021 touch_nmi_watchdog();
2024 /* Wait up to 1s for flow control if necessary */
2025 if (up->port.flags & UPF_CONS_FLOW) {
2026 for (tmout = 1000000; tmout; tmout--) {
2027 unsigned int msr = serial_in(up, UART_MSR);
2028 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
2029 if (msr & UART_MSR_CTS)
2030 break;
2031 udelay(1);
2032 touch_nmi_watchdog();
2037 #ifdef CONFIG_CONSOLE_POLL
2039 * Console polling routines for writing and reading from the uart while
2040 * in an interrupt or debug context.
2043 static int serial8250_get_poll_char(struct uart_port *port)
2045 struct uart_8250_port *up = up_to_u8250p(port);
2046 unsigned char lsr;
2047 int status;
2049 serial8250_rpm_get(up);
2051 lsr = serial_port_in(port, UART_LSR);
2053 if (!(lsr & UART_LSR_DR)) {
2054 status = NO_POLL_CHAR;
2055 goto out;
2058 status = serial_port_in(port, UART_RX);
2059 out:
2060 serial8250_rpm_put(up);
2061 return status;
2065 static void serial8250_put_poll_char(struct uart_port *port,
2066 unsigned char c)
2068 unsigned int ier;
2069 struct uart_8250_port *up = up_to_u8250p(port);
2071 serial8250_rpm_get(up);
2073 * First save the IER then disable the interrupts
2075 ier = serial_port_in(port, UART_IER);
2076 if (up->capabilities & UART_CAP_UUE)
2077 serial_port_out(port, UART_IER, UART_IER_UUE);
2078 else
2079 serial_port_out(port, UART_IER, 0);
2081 wait_for_xmitr(up, BOTH_EMPTY);
2083 * Send the character out.
2085 serial_port_out(port, UART_TX, c);
2088 * Finally, wait for transmitter to become empty
2089 * and restore the IER
2091 wait_for_xmitr(up, BOTH_EMPTY);
2092 serial_port_out(port, UART_IER, ier);
2093 serial8250_rpm_put(up);
2096 #endif /* CONFIG_CONSOLE_POLL */
2098 int serial8250_do_startup(struct uart_port *port)
2100 struct uart_8250_port *up = up_to_u8250p(port);
2101 unsigned long flags;
2102 unsigned char lsr, iir;
2103 int retval;
2105 if (!port->fifosize)
2106 port->fifosize = uart_config[port->type].fifo_size;
2107 if (!up->tx_loadsz)
2108 up->tx_loadsz = uart_config[port->type].tx_loadsz;
2109 if (!up->capabilities)
2110 up->capabilities = uart_config[port->type].flags;
2111 up->mcr = 0;
2113 if (port->iotype != up->cur_iotype)
2114 set_io_from_upio(port);
2116 serial8250_rpm_get(up);
2117 if (port->type == PORT_16C950) {
2118 /* Wake up and initialize UART */
2119 up->acr = 0;
2120 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2121 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2122 serial_port_out(port, UART_IER, 0);
2123 serial_port_out(port, UART_LCR, 0);
2124 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2125 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2126 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2127 serial_port_out(port, UART_LCR, 0);
2130 if (port->type == PORT_DA830) {
2131 /* Reset the port */
2132 serial_port_out(port, UART_IER, 0);
2133 serial_port_out(port, UART_DA830_PWREMU_MGMT, 0);
2134 mdelay(10);
2136 /* Enable Tx, Rx and free run mode */
2137 serial_port_out(port, UART_DA830_PWREMU_MGMT,
2138 UART_DA830_PWREMU_MGMT_UTRST |
2139 UART_DA830_PWREMU_MGMT_URRST |
2140 UART_DA830_PWREMU_MGMT_FREE);
2143 #ifdef CONFIG_SERIAL_8250_RSA
2145 * If this is an RSA port, see if we can kick it up to the
2146 * higher speed clock.
2148 enable_rsa(up);
2149 #endif
2151 if (port->type == PORT_XR17V35X) {
2153 * First enable access to IER [7:5], ISR [5:4], FCR [5:4],
2154 * MCR [7:5] and MSR [7:0]
2156 serial_port_out(port, UART_XR_EFR, UART_EFR_ECB);
2159 * Make sure all interrups are masked until initialization is
2160 * complete and the FIFOs are cleared
2162 serial_port_out(port, UART_IER, 0);
2166 * Clear the FIFO buffers and disable them.
2167 * (they will be reenabled in set_termios())
2169 serial8250_clear_fifos(up);
2172 * Clear the interrupt registers.
2174 serial_port_in(port, UART_LSR);
2175 serial_port_in(port, UART_RX);
2176 serial_port_in(port, UART_IIR);
2177 serial_port_in(port, UART_MSR);
2178 if ((port->type == PORT_XR17V35X) || (port->type == PORT_XR17D15X))
2179 serial_port_in(port, UART_EXAR_INT0);
2182 * At this point, there's no way the LSR could still be 0xff;
2183 * if it is, then bail out, because there's likely no UART
2184 * here.
2186 if (!(port->flags & UPF_BUGGY_UART) &&
2187 (serial_port_in(port, UART_LSR) == 0xff)) {
2188 printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2189 serial_index(port));
2190 retval = -ENODEV;
2191 goto out;
2195 * For a XR16C850, we need to set the trigger levels
2197 if (port->type == PORT_16850) {
2198 unsigned char fctr;
2200 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
2202 fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2203 serial_port_out(port, UART_FCTR,
2204 fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2205 serial_port_out(port, UART_TRG, UART_TRG_96);
2206 serial_port_out(port, UART_FCTR,
2207 fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2208 serial_port_out(port, UART_TRG, UART_TRG_96);
2210 serial_port_out(port, UART_LCR, 0);
2214 * For the Altera 16550 variants, set TX threshold trigger level.
2216 if (((port->type == PORT_ALTR_16550_F32) ||
2217 (port->type == PORT_ALTR_16550_F64) ||
2218 (port->type == PORT_ALTR_16550_F128)) && (port->fifosize > 1)) {
2219 /* Bounds checking of TX threshold (valid 0 to fifosize-2) */
2220 if ((up->tx_loadsz < 2) || (up->tx_loadsz > port->fifosize)) {
2221 pr_err("ttyS%d TX FIFO Threshold errors, skipping\n",
2222 serial_index(port));
2223 } else {
2224 serial_port_out(port, UART_ALTR_AFR,
2225 UART_ALTR_EN_TXFIFO_LW);
2226 serial_port_out(port, UART_ALTR_TX_LOW,
2227 port->fifosize - up->tx_loadsz);
2228 port->handle_irq = serial8250_tx_threshold_handle_irq;
2232 if (port->irq && !(up->port.flags & UPF_NO_THRE_TEST)) {
2233 unsigned char iir1;
2235 * Test for UARTs that do not reassert THRE when the
2236 * transmitter is idle and the interrupt has already
2237 * been cleared. Real 16550s should always reassert
2238 * this interrupt whenever the transmitter is idle and
2239 * the interrupt is enabled. Delays are necessary to
2240 * allow register changes to become visible.
2242 spin_lock_irqsave(&port->lock, flags);
2243 if (up->port.irqflags & IRQF_SHARED)
2244 disable_irq_nosync(port->irq);
2246 wait_for_xmitr(up, UART_LSR_THRE);
2247 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2248 udelay(1); /* allow THRE to set */
2249 iir1 = serial_port_in(port, UART_IIR);
2250 serial_port_out(port, UART_IER, 0);
2251 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2252 udelay(1); /* allow a working UART time to re-assert THRE */
2253 iir = serial_port_in(port, UART_IIR);
2254 serial_port_out(port, UART_IER, 0);
2256 if (port->irqflags & IRQF_SHARED)
2257 enable_irq(port->irq);
2258 spin_unlock_irqrestore(&port->lock, flags);
2261 * If the interrupt is not reasserted, or we otherwise
2262 * don't trust the iir, setup a timer to kick the UART
2263 * on a regular basis.
2265 if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
2266 up->port.flags & UPF_BUG_THRE) {
2267 up->bugs |= UART_BUG_THRE;
2271 retval = up->ops->setup_irq(up);
2272 if (retval)
2273 goto out;
2276 * Now, initialize the UART
2278 serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
2280 spin_lock_irqsave(&port->lock, flags);
2281 if (up->port.flags & UPF_FOURPORT) {
2282 if (!up->port.irq)
2283 up->port.mctrl |= TIOCM_OUT1;
2284 } else
2286 * Most PC uarts need OUT2 raised to enable interrupts.
2288 if (port->irq)
2289 up->port.mctrl |= TIOCM_OUT2;
2291 serial8250_set_mctrl(port, port->mctrl);
2294 * Serial over Lan (SoL) hack:
2295 * Intel 8257x Gigabit ethernet chips have a 16550 emulation, to be
2296 * used for Serial Over Lan. Those chips take a longer time than a
2297 * normal serial device to signalize that a transmission data was
2298 * queued. Due to that, the above test generally fails. One solution
2299 * would be to delay the reading of iir. However, this is not
2300 * reliable, since the timeout is variable. So, let's just don't
2301 * test if we receive TX irq. This way, we'll never enable
2302 * UART_BUG_TXEN.
2304 if (up->port.quirks & UPQ_NO_TXEN_TEST)
2305 goto dont_test_tx_en;
2308 * Do a quick test to see if we receive an interrupt when we enable
2309 * the TX irq.
2311 serial_port_out(port, UART_IER, UART_IER_THRI);
2312 lsr = serial_port_in(port, UART_LSR);
2313 iir = serial_port_in(port, UART_IIR);
2314 serial_port_out(port, UART_IER, 0);
2316 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2317 if (!(up->bugs & UART_BUG_TXEN)) {
2318 up->bugs |= UART_BUG_TXEN;
2319 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2320 serial_index(port));
2322 } else {
2323 up->bugs &= ~UART_BUG_TXEN;
2326 dont_test_tx_en:
2327 spin_unlock_irqrestore(&port->lock, flags);
2330 * Clear the interrupt registers again for luck, and clear the
2331 * saved flags to avoid getting false values from polling
2332 * routines or the previous session.
2334 serial_port_in(port, UART_LSR);
2335 serial_port_in(port, UART_RX);
2336 serial_port_in(port, UART_IIR);
2337 serial_port_in(port, UART_MSR);
2338 if ((port->type == PORT_XR17V35X) || (port->type == PORT_XR17D15X))
2339 serial_port_in(port, UART_EXAR_INT0);
2340 up->lsr_saved_flags = 0;
2341 up->msr_saved_flags = 0;
2344 * Request DMA channels for both RX and TX.
2346 if (up->dma) {
2347 retval = serial8250_request_dma(up);
2348 if (retval) {
2349 pr_warn_ratelimited("ttyS%d - failed to request DMA\n",
2350 serial_index(port));
2351 up->dma = NULL;
2356 * Set the IER shadow for rx interrupts but defer actual interrupt
2357 * enable until after the FIFOs are enabled; otherwise, an already-
2358 * active sender can swamp the interrupt handler with "too much work".
2360 up->ier = UART_IER_RLSI | UART_IER_RDI;
2362 if (port->flags & UPF_FOURPORT) {
2363 unsigned int icp;
2365 * Enable interrupts on the AST Fourport board
2367 icp = (port->iobase & 0xfe0) | 0x01f;
2368 outb_p(0x80, icp);
2369 inb_p(icp);
2371 retval = 0;
2372 out:
2373 serial8250_rpm_put(up);
2374 return retval;
2376 EXPORT_SYMBOL_GPL(serial8250_do_startup);
2378 static int serial8250_startup(struct uart_port *port)
2380 if (port->startup)
2381 return port->startup(port);
2382 return serial8250_do_startup(port);
2385 void serial8250_do_shutdown(struct uart_port *port)
2387 struct uart_8250_port *up = up_to_u8250p(port);
2388 unsigned long flags;
2390 serial8250_rpm_get(up);
2392 * Disable interrupts from this port
2394 spin_lock_irqsave(&port->lock, flags);
2395 up->ier = 0;
2396 serial_port_out(port, UART_IER, 0);
2397 spin_unlock_irqrestore(&port->lock, flags);
2399 synchronize_irq(port->irq);
2401 if (up->dma)
2402 serial8250_release_dma(up);
2404 spin_lock_irqsave(&port->lock, flags);
2405 if (port->flags & UPF_FOURPORT) {
2406 /* reset interrupts on the AST Fourport board */
2407 inb((port->iobase & 0xfe0) | 0x1f);
2408 port->mctrl |= TIOCM_OUT1;
2409 } else
2410 port->mctrl &= ~TIOCM_OUT2;
2412 serial8250_set_mctrl(port, port->mctrl);
2413 spin_unlock_irqrestore(&port->lock, flags);
2416 * Disable break condition and FIFOs
2418 serial_port_out(port, UART_LCR,
2419 serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
2420 serial8250_clear_fifos(up);
2422 #ifdef CONFIG_SERIAL_8250_RSA
2424 * Reset the RSA board back to 115kbps compat mode.
2426 disable_rsa(up);
2427 #endif
2430 * Read data port to reset things, and then unlink from
2431 * the IRQ chain.
2433 serial_port_in(port, UART_RX);
2434 serial8250_rpm_put(up);
2436 up->ops->release_irq(up);
2438 EXPORT_SYMBOL_GPL(serial8250_do_shutdown);
2440 static void serial8250_shutdown(struct uart_port *port)
2442 if (port->shutdown)
2443 port->shutdown(port);
2444 else
2445 serial8250_do_shutdown(port);
2449 * XR17V35x UARTs have an extra fractional divisor register (DLD)
2450 * Calculate divisor with extra 4-bit fractional portion
2452 static unsigned int xr17v35x_get_divisor(struct uart_8250_port *up,
2453 unsigned int baud,
2454 unsigned int *frac)
2456 struct uart_port *port = &up->port;
2457 unsigned int quot_16;
2459 quot_16 = DIV_ROUND_CLOSEST(port->uartclk, baud);
2460 *frac = quot_16 & 0x0f;
2462 return quot_16 >> 4;
2465 static unsigned int serial8250_get_divisor(struct uart_8250_port *up,
2466 unsigned int baud,
2467 unsigned int *frac)
2469 struct uart_port *port = &up->port;
2470 unsigned int quot;
2473 * Handle magic divisors for baud rates above baud_base on
2474 * SMSC SuperIO chips.
2477 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2478 baud == (port->uartclk/4))
2479 quot = 0x8001;
2480 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2481 baud == (port->uartclk/8))
2482 quot = 0x8002;
2483 else if (up->port.type == PORT_XR17V35X)
2484 quot = xr17v35x_get_divisor(up, baud, frac);
2485 else
2486 quot = uart_get_divisor(port, baud);
2489 * Oxford Semi 952 rev B workaround
2491 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2492 quot++;
2494 return quot;
2497 static unsigned char serial8250_compute_lcr(struct uart_8250_port *up,
2498 tcflag_t c_cflag)
2500 unsigned char cval;
2502 switch (c_cflag & CSIZE) {
2503 case CS5:
2504 cval = UART_LCR_WLEN5;
2505 break;
2506 case CS6:
2507 cval = UART_LCR_WLEN6;
2508 break;
2509 case CS7:
2510 cval = UART_LCR_WLEN7;
2511 break;
2512 default:
2513 case CS8:
2514 cval = UART_LCR_WLEN8;
2515 break;
2518 if (c_cflag & CSTOPB)
2519 cval |= UART_LCR_STOP;
2520 if (c_cflag & PARENB) {
2521 cval |= UART_LCR_PARITY;
2522 if (up->bugs & UART_BUG_PARITY)
2523 up->fifo_bug = true;
2525 if (!(c_cflag & PARODD))
2526 cval |= UART_LCR_EPAR;
2527 #ifdef CMSPAR
2528 if (c_cflag & CMSPAR)
2529 cval |= UART_LCR_SPAR;
2530 #endif
2532 return cval;
2535 static void serial8250_set_divisor(struct uart_port *port, unsigned int baud,
2536 unsigned int quot, unsigned int quot_frac)
2538 struct uart_8250_port *up = up_to_u8250p(port);
2540 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2541 if (is_omap1510_8250(up)) {
2542 if (baud == 115200) {
2543 quot = 1;
2544 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
2545 } else
2546 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
2550 * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
2551 * otherwise just set DLAB
2553 if (up->capabilities & UART_NATSEMI)
2554 serial_port_out(port, UART_LCR, 0xe0);
2555 else
2556 serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
2558 serial_dl_write(up, quot);
2560 /* XR17V35x UARTs have an extra fractional divisor register (DLD) */
2561 if (up->port.type == PORT_XR17V35X) {
2562 /* Preserve bits not related to baudrate; DLD[7:4]. */
2563 quot_frac |= serial_port_in(port, 0x2) & 0xf0;
2564 serial_port_out(port, 0x2, quot_frac);
2568 static unsigned int serial8250_get_baud_rate(struct uart_port *port,
2569 struct ktermios *termios,
2570 struct ktermios *old)
2573 * Ask the core to calculate the divisor for us.
2574 * Allow 1% tolerance at the upper limit so uart clks marginally
2575 * slower than nominal still match standard baud rates without
2576 * causing transmission errors.
2578 return uart_get_baud_rate(port, termios, old,
2579 port->uartclk / 16 / UART_DIV_MAX,
2580 port->uartclk);
2583 void
2584 serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2585 struct ktermios *old)
2587 struct uart_8250_port *up = up_to_u8250p(port);
2588 unsigned char cval;
2589 unsigned long flags;
2590 unsigned int baud, quot, frac = 0;
2592 if (up->capabilities & UART_CAP_MINI) {
2593 termios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CMSPAR);
2594 if ((termios->c_cflag & CSIZE) == CS5 ||
2595 (termios->c_cflag & CSIZE) == CS6)
2596 termios->c_cflag = (termios->c_cflag & ~CSIZE) | CS7;
2598 cval = serial8250_compute_lcr(up, termios->c_cflag);
2600 baud = serial8250_get_baud_rate(port, termios, old);
2601 quot = serial8250_get_divisor(up, baud, &frac);
2604 * Ok, we're now changing the port state. Do it with
2605 * interrupts disabled.
2607 serial8250_rpm_get(up);
2608 spin_lock_irqsave(&port->lock, flags);
2610 up->lcr = cval; /* Save computed LCR */
2612 if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
2613 /* NOTE: If fifo_bug is not set, a user can set RX_trigger. */
2614 if ((baud < 2400 && !up->dma) || up->fifo_bug) {
2615 up->fcr &= ~UART_FCR_TRIGGER_MASK;
2616 up->fcr |= UART_FCR_TRIGGER_1;
2621 * MCR-based auto flow control. When AFE is enabled, RTS will be
2622 * deasserted when the receive FIFO contains more characters than
2623 * the trigger, or the MCR RTS bit is cleared.
2625 if (up->capabilities & UART_CAP_AFE) {
2626 up->mcr &= ~UART_MCR_AFE;
2627 if (termios->c_cflag & CRTSCTS)
2628 up->mcr |= UART_MCR_AFE;
2632 * Update the per-port timeout.
2634 uart_update_timeout(port, termios->c_cflag, baud);
2636 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2637 if (termios->c_iflag & INPCK)
2638 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2639 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2640 port->read_status_mask |= UART_LSR_BI;
2643 * Characteres to ignore
2645 port->ignore_status_mask = 0;
2646 if (termios->c_iflag & IGNPAR)
2647 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2648 if (termios->c_iflag & IGNBRK) {
2649 port->ignore_status_mask |= UART_LSR_BI;
2651 * If we're ignoring parity and break indicators,
2652 * ignore overruns too (for real raw support).
2654 if (termios->c_iflag & IGNPAR)
2655 port->ignore_status_mask |= UART_LSR_OE;
2659 * ignore all characters if CREAD is not set
2661 if ((termios->c_cflag & CREAD) == 0)
2662 port->ignore_status_mask |= UART_LSR_DR;
2665 * CTS flow control flag and modem status interrupts
2667 up->ier &= ~UART_IER_MSI;
2668 if (!(up->bugs & UART_BUG_NOMSR) &&
2669 UART_ENABLE_MS(&up->port, termios->c_cflag))
2670 up->ier |= UART_IER_MSI;
2671 if (up->capabilities & UART_CAP_UUE)
2672 up->ier |= UART_IER_UUE;
2673 if (up->capabilities & UART_CAP_RTOIE)
2674 up->ier |= UART_IER_RTOIE;
2676 serial_port_out(port, UART_IER, up->ier);
2678 if (up->capabilities & UART_CAP_EFR) {
2679 unsigned char efr = 0;
2681 * TI16C752/Startech hardware flow control. FIXME:
2682 * - TI16C752 requires control thresholds to be set.
2683 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2685 if (termios->c_cflag & CRTSCTS)
2686 efr |= UART_EFR_CTS;
2688 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2689 if (port->flags & UPF_EXAR_EFR)
2690 serial_port_out(port, UART_XR_EFR, efr);
2691 else
2692 serial_port_out(port, UART_EFR, efr);
2695 serial8250_set_divisor(port, baud, quot, frac);
2698 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2699 * is written without DLAB set, this mode will be disabled.
2701 if (port->type == PORT_16750)
2702 serial_port_out(port, UART_FCR, up->fcr);
2704 serial_port_out(port, UART_LCR, up->lcr); /* reset DLAB */
2705 if (port->type != PORT_16750) {
2706 /* emulated UARTs (Lucent Venus 167x) need two steps */
2707 if (up->fcr & UART_FCR_ENABLE_FIFO)
2708 serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
2709 serial_port_out(port, UART_FCR, up->fcr); /* set fcr */
2711 serial8250_set_mctrl(port, port->mctrl);
2712 spin_unlock_irqrestore(&port->lock, flags);
2713 serial8250_rpm_put(up);
2715 /* Don't rewrite B0 */
2716 if (tty_termios_baud_rate(termios))
2717 tty_termios_encode_baud_rate(termios, baud, baud);
2719 EXPORT_SYMBOL(serial8250_do_set_termios);
2721 static void
2722 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2723 struct ktermios *old)
2725 if (port->set_termios)
2726 port->set_termios(port, termios, old);
2727 else
2728 serial8250_do_set_termios(port, termios, old);
2731 void serial8250_do_set_ldisc(struct uart_port *port, struct ktermios *termios)
2733 if (termios->c_line == N_PPS) {
2734 port->flags |= UPF_HARDPPS_CD;
2735 spin_lock_irq(&port->lock);
2736 serial8250_enable_ms(port);
2737 spin_unlock_irq(&port->lock);
2738 } else {
2739 port->flags &= ~UPF_HARDPPS_CD;
2740 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2741 spin_lock_irq(&port->lock);
2742 serial8250_disable_ms(port);
2743 spin_unlock_irq(&port->lock);
2747 EXPORT_SYMBOL_GPL(serial8250_do_set_ldisc);
2749 static void
2750 serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios)
2752 if (port->set_ldisc)
2753 port->set_ldisc(port, termios);
2754 else
2755 serial8250_do_set_ldisc(port, termios);
2758 void serial8250_do_pm(struct uart_port *port, unsigned int state,
2759 unsigned int oldstate)
2761 struct uart_8250_port *p = up_to_u8250p(port);
2763 serial8250_set_sleep(p, state != 0);
2765 EXPORT_SYMBOL(serial8250_do_pm);
2767 static void
2768 serial8250_pm(struct uart_port *port, unsigned int state,
2769 unsigned int oldstate)
2771 if (port->pm)
2772 port->pm(port, state, oldstate);
2773 else
2774 serial8250_do_pm(port, state, oldstate);
2777 static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2779 if (pt->port.mapsize)
2780 return pt->port.mapsize;
2781 if (pt->port.iotype == UPIO_AU) {
2782 if (pt->port.type == PORT_RT2880)
2783 return 0x100;
2784 return 0x1000;
2786 if (is_omap1_8250(pt))
2787 return 0x16 << pt->port.regshift;
2789 return 8 << pt->port.regshift;
2793 * Resource handling.
2795 static int serial8250_request_std_resource(struct uart_8250_port *up)
2797 unsigned int size = serial8250_port_size(up);
2798 struct uart_port *port = &up->port;
2799 int ret = 0;
2801 switch (port->iotype) {
2802 case UPIO_AU:
2803 case UPIO_TSI:
2804 case UPIO_MEM32:
2805 case UPIO_MEM32BE:
2806 case UPIO_MEM16:
2807 case UPIO_MEM:
2808 if (!port->mapbase)
2809 break;
2811 if (!request_mem_region(port->mapbase, size, "serial")) {
2812 ret = -EBUSY;
2813 break;
2816 if (port->flags & UPF_IOREMAP) {
2817 port->membase = ioremap_nocache(port->mapbase, size);
2818 if (!port->membase) {
2819 release_mem_region(port->mapbase, size);
2820 ret = -ENOMEM;
2823 break;
2825 case UPIO_HUB6:
2826 case UPIO_PORT:
2827 if (!request_region(port->iobase, size, "serial"))
2828 ret = -EBUSY;
2829 break;
2831 return ret;
2834 static void serial8250_release_std_resource(struct uart_8250_port *up)
2836 unsigned int size = serial8250_port_size(up);
2837 struct uart_port *port = &up->port;
2839 switch (port->iotype) {
2840 case UPIO_AU:
2841 case UPIO_TSI:
2842 case UPIO_MEM32:
2843 case UPIO_MEM32BE:
2844 case UPIO_MEM16:
2845 case UPIO_MEM:
2846 if (!port->mapbase)
2847 break;
2849 if (port->flags & UPF_IOREMAP) {
2850 iounmap(port->membase);
2851 port->membase = NULL;
2854 release_mem_region(port->mapbase, size);
2855 break;
2857 case UPIO_HUB6:
2858 case UPIO_PORT:
2859 release_region(port->iobase, size);
2860 break;
2864 static void serial8250_release_port(struct uart_port *port)
2866 struct uart_8250_port *up = up_to_u8250p(port);
2868 serial8250_release_std_resource(up);
2871 static int serial8250_request_port(struct uart_port *port)
2873 struct uart_8250_port *up = up_to_u8250p(port);
2875 return serial8250_request_std_resource(up);
2878 static int fcr_get_rxtrig_bytes(struct uart_8250_port *up)
2880 const struct serial8250_config *conf_type = &uart_config[up->port.type];
2881 unsigned char bytes;
2883 bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)];
2885 return bytes ? bytes : -EOPNOTSUPP;
2888 static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes)
2890 const struct serial8250_config *conf_type = &uart_config[up->port.type];
2891 int i;
2893 if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)])
2894 return -EOPNOTSUPP;
2896 for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) {
2897 if (bytes < conf_type->rxtrig_bytes[i])
2898 /* Use the nearest lower value */
2899 return (--i) << UART_FCR_R_TRIG_SHIFT;
2902 return UART_FCR_R_TRIG_11;
2905 static int do_get_rxtrig(struct tty_port *port)
2907 struct uart_state *state = container_of(port, struct uart_state, port);
2908 struct uart_port *uport = state->uart_port;
2909 struct uart_8250_port *up = up_to_u8250p(uport);
2911 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
2912 return -EINVAL;
2914 return fcr_get_rxtrig_bytes(up);
2917 static int do_serial8250_get_rxtrig(struct tty_port *port)
2919 int rxtrig_bytes;
2921 mutex_lock(&port->mutex);
2922 rxtrig_bytes = do_get_rxtrig(port);
2923 mutex_unlock(&port->mutex);
2925 return rxtrig_bytes;
2928 static ssize_t serial8250_get_attr_rx_trig_bytes(struct device *dev,
2929 struct device_attribute *attr, char *buf)
2931 struct tty_port *port = dev_get_drvdata(dev);
2932 int rxtrig_bytes;
2934 rxtrig_bytes = do_serial8250_get_rxtrig(port);
2935 if (rxtrig_bytes < 0)
2936 return rxtrig_bytes;
2938 return snprintf(buf, PAGE_SIZE, "%d\n", rxtrig_bytes);
2941 static int do_set_rxtrig(struct tty_port *port, unsigned char bytes)
2943 struct uart_state *state = container_of(port, struct uart_state, port);
2944 struct uart_port *uport = state->uart_port;
2945 struct uart_8250_port *up = up_to_u8250p(uport);
2946 int rxtrig;
2948 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1 ||
2949 up->fifo_bug)
2950 return -EINVAL;
2952 rxtrig = bytes_to_fcr_rxtrig(up, bytes);
2953 if (rxtrig < 0)
2954 return rxtrig;
2956 serial8250_clear_fifos(up);
2957 up->fcr &= ~UART_FCR_TRIGGER_MASK;
2958 up->fcr |= (unsigned char)rxtrig;
2959 serial_out(up, UART_FCR, up->fcr);
2960 return 0;
2963 static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes)
2965 int ret;
2967 mutex_lock(&port->mutex);
2968 ret = do_set_rxtrig(port, bytes);
2969 mutex_unlock(&port->mutex);
2971 return ret;
2974 static ssize_t serial8250_set_attr_rx_trig_bytes(struct device *dev,
2975 struct device_attribute *attr, const char *buf, size_t count)
2977 struct tty_port *port = dev_get_drvdata(dev);
2978 unsigned char bytes;
2979 int ret;
2981 if (!count)
2982 return -EINVAL;
2984 ret = kstrtou8(buf, 10, &bytes);
2985 if (ret < 0)
2986 return ret;
2988 ret = do_serial8250_set_rxtrig(port, bytes);
2989 if (ret < 0)
2990 return ret;
2992 return count;
2995 static DEVICE_ATTR(rx_trig_bytes, S_IRUSR | S_IWUSR | S_IRGRP,
2996 serial8250_get_attr_rx_trig_bytes,
2997 serial8250_set_attr_rx_trig_bytes);
2999 static struct attribute *serial8250_dev_attrs[] = {
3000 &dev_attr_rx_trig_bytes.attr,
3001 NULL,
3004 static struct attribute_group serial8250_dev_attr_group = {
3005 .attrs = serial8250_dev_attrs,
3008 static void register_dev_spec_attr_grp(struct uart_8250_port *up)
3010 const struct serial8250_config *conf_type = &uart_config[up->port.type];
3012 if (conf_type->rxtrig_bytes[0])
3013 up->port.attr_group = &serial8250_dev_attr_group;
3016 static void serial8250_config_port(struct uart_port *port, int flags)
3018 struct uart_8250_port *up = up_to_u8250p(port);
3019 int ret;
3022 * Find the region that we can probe for. This in turn
3023 * tells us whether we can probe for the type of port.
3025 ret = serial8250_request_std_resource(up);
3026 if (ret < 0)
3027 return;
3029 if (port->iotype != up->cur_iotype)
3030 set_io_from_upio(port);
3032 if (flags & UART_CONFIG_TYPE)
3033 autoconfig(up);
3035 /* if access method is AU, it is a 16550 with a quirk */
3036 if (port->type == PORT_16550A && port->iotype == UPIO_AU)
3037 up->bugs |= UART_BUG_NOMSR;
3039 /* HW bugs may trigger IRQ while IIR == NO_INT */
3040 if (port->type == PORT_TEGRA)
3041 up->bugs |= UART_BUG_NOMSR;
3043 if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
3044 autoconfig_irq(up);
3046 if (port->type == PORT_UNKNOWN)
3047 serial8250_release_std_resource(up);
3049 register_dev_spec_attr_grp(up);
3050 up->fcr = uart_config[up->port.type].fcr;
3053 static int
3054 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
3056 if (ser->irq >= nr_irqs || ser->irq < 0 ||
3057 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
3058 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
3059 ser->type == PORT_STARTECH)
3060 return -EINVAL;
3061 return 0;
3064 static const char *serial8250_type(struct uart_port *port)
3066 int type = port->type;
3068 if (type >= ARRAY_SIZE(uart_config))
3069 type = 0;
3070 return uart_config[type].name;
3073 static const struct uart_ops serial8250_pops = {
3074 .tx_empty = serial8250_tx_empty,
3075 .set_mctrl = serial8250_set_mctrl,
3076 .get_mctrl = serial8250_get_mctrl,
3077 .stop_tx = serial8250_stop_tx,
3078 .start_tx = serial8250_start_tx,
3079 .throttle = serial8250_throttle,
3080 .unthrottle = serial8250_unthrottle,
3081 .stop_rx = serial8250_stop_rx,
3082 .enable_ms = serial8250_enable_ms,
3083 .break_ctl = serial8250_break_ctl,
3084 .startup = serial8250_startup,
3085 .shutdown = serial8250_shutdown,
3086 .set_termios = serial8250_set_termios,
3087 .set_ldisc = serial8250_set_ldisc,
3088 .pm = serial8250_pm,
3089 .type = serial8250_type,
3090 .release_port = serial8250_release_port,
3091 .request_port = serial8250_request_port,
3092 .config_port = serial8250_config_port,
3093 .verify_port = serial8250_verify_port,
3094 #ifdef CONFIG_CONSOLE_POLL
3095 .poll_get_char = serial8250_get_poll_char,
3096 .poll_put_char = serial8250_put_poll_char,
3097 #endif
3100 void serial8250_init_port(struct uart_8250_port *up)
3102 struct uart_port *port = &up->port;
3104 spin_lock_init(&port->lock);
3105 port->ops = &serial8250_pops;
3107 up->cur_iotype = 0xFF;
3109 EXPORT_SYMBOL_GPL(serial8250_init_port);
3111 void serial8250_set_defaults(struct uart_8250_port *up)
3113 struct uart_port *port = &up->port;
3115 if (up->port.flags & UPF_FIXED_TYPE) {
3116 unsigned int type = up->port.type;
3118 if (!up->port.fifosize)
3119 up->port.fifosize = uart_config[type].fifo_size;
3120 if (!up->tx_loadsz)
3121 up->tx_loadsz = uart_config[type].tx_loadsz;
3122 if (!up->capabilities)
3123 up->capabilities = uart_config[type].flags;
3126 set_io_from_upio(port);
3128 /* default dma handlers */
3129 if (up->dma) {
3130 if (!up->dma->tx_dma)
3131 up->dma->tx_dma = serial8250_tx_dma;
3132 if (!up->dma->rx_dma)
3133 up->dma->rx_dma = serial8250_rx_dma;
3136 EXPORT_SYMBOL_GPL(serial8250_set_defaults);
3138 #ifdef CONFIG_SERIAL_8250_CONSOLE
3140 static void serial8250_console_putchar(struct uart_port *port, int ch)
3142 struct uart_8250_port *up = up_to_u8250p(port);
3144 wait_for_xmitr(up, UART_LSR_THRE);
3145 serial_port_out(port, UART_TX, ch);
3149 * Restore serial console when h/w power-off detected
3151 static void serial8250_console_restore(struct uart_8250_port *up)
3153 struct uart_port *port = &up->port;
3154 struct ktermios termios;
3155 unsigned int baud, quot, frac = 0;
3157 termios.c_cflag = port->cons->cflag;
3158 if (port->state->port.tty && termios.c_cflag == 0)
3159 termios.c_cflag = port->state->port.tty->termios.c_cflag;
3161 baud = serial8250_get_baud_rate(port, &termios, NULL);
3162 quot = serial8250_get_divisor(up, baud, &frac);
3164 serial8250_set_divisor(port, baud, quot, frac);
3165 serial_port_out(port, UART_LCR, up->lcr);
3166 serial8250_out_MCR(up, UART_MCR_DTR | UART_MCR_RTS);
3170 * Print a string to the serial port trying not to disturb
3171 * any possible real use of the port...
3173 * The console_lock must be held when we get here.
3175 void serial8250_console_write(struct uart_8250_port *up, const char *s,
3176 unsigned int count)
3178 struct uart_port *port = &up->port;
3179 unsigned long flags;
3180 unsigned int ier;
3181 int locked = 1;
3183 touch_nmi_watchdog();
3185 serial8250_rpm_get(up);
3187 if (port->sysrq)
3188 locked = 0;
3189 else if (oops_in_progress)
3190 locked = spin_trylock_irqsave(&port->lock, flags);
3191 else
3192 spin_lock_irqsave(&port->lock, flags);
3195 * First save the IER then disable the interrupts
3197 ier = serial_port_in(port, UART_IER);
3199 if (up->capabilities & UART_CAP_UUE)
3200 serial_port_out(port, UART_IER, UART_IER_UUE);
3201 else
3202 serial_port_out(port, UART_IER, 0);
3204 /* check scratch reg to see if port powered off during system sleep */
3205 if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) {
3206 serial8250_console_restore(up);
3207 up->canary = 0;
3210 uart_console_write(port, s, count, serial8250_console_putchar);
3213 * Finally, wait for transmitter to become empty
3214 * and restore the IER
3216 wait_for_xmitr(up, BOTH_EMPTY);
3217 serial_port_out(port, UART_IER, ier);
3220 * The receive handling will happen properly because the
3221 * receive ready bit will still be set; it is not cleared
3222 * on read. However, modem control will not, we must
3223 * call it if we have saved something in the saved flags
3224 * while processing with interrupts off.
3226 if (up->msr_saved_flags)
3227 serial8250_modem_status(up);
3229 if (locked)
3230 spin_unlock_irqrestore(&port->lock, flags);
3231 serial8250_rpm_put(up);
3234 static unsigned int probe_baud(struct uart_port *port)
3236 unsigned char lcr, dll, dlm;
3237 unsigned int quot;
3239 lcr = serial_port_in(port, UART_LCR);
3240 serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB);
3241 dll = serial_port_in(port, UART_DLL);
3242 dlm = serial_port_in(port, UART_DLM);
3243 serial_port_out(port, UART_LCR, lcr);
3245 quot = (dlm << 8) | dll;
3246 return (port->uartclk / 16) / quot;
3249 int serial8250_console_setup(struct uart_port *port, char *options, bool probe)
3251 int baud = 9600;
3252 int bits = 8;
3253 int parity = 'n';
3254 int flow = 'n';
3256 if (!port->iobase && !port->membase)
3257 return -ENODEV;
3259 if (options)
3260 uart_parse_options(options, &baud, &parity, &bits, &flow);
3261 else if (probe)
3262 baud = probe_baud(port);
3264 return uart_set_options(port, port->cons, baud, parity, bits, flow);
3267 #endif /* CONFIG_SERIAL_8250_CONSOLE */
3269 MODULE_LICENSE("GPL");