blob: 01a85a7e7427c1cff65e754c05e8438399ee245e [file] [log] [blame]
Peter Hurleyb6830f62015-06-27 09:19:00 -04001/*
2 * Base port operations for 8250/16550-type serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 * Split from 8250_core.c, Copyright (C) 2001 Russell King.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * A note about mapbase / membase
13 *
14 * mapbase is the physical address of the IO port.
15 * membase is an 'ioremapped' cookie.
16 */
17
18#if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19#define SUPPORT_SYSRQ
20#endif
21
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/ioport.h>
25#include <linux/init.h>
26#include <linux/console.h>
27#include <linux/sysrq.h>
28#include <linux/delay.h>
29#include <linux/platform_device.h>
30#include <linux/tty.h>
31#include <linux/ratelimit.h>
32#include <linux/tty_flip.h>
33#include <linux/serial.h>
34#include <linux/serial_8250.h>
35#include <linux/nmi.h>
36#include <linux/mutex.h>
37#include <linux/slab.h>
38#include <linux/uaccess.h>
39#include <linux/pm_runtime.h>
Matwey V. Kornilove490c912016-02-01 21:09:21 +030040#include <linux/timer.h>
Peter Hurleyb6830f62015-06-27 09:19:00 -040041
42#include <asm/io.h>
43#include <asm/irq.h>
44
45#include "8250.h"
46
47/*
48 * Debugging.
49 */
50#if 0
51#define DEBUG_AUTOCONF(fmt...) printk(fmt)
52#else
53#define DEBUG_AUTOCONF(fmt...) do { } while (0)
54#endif
55
56#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
57
58/*
59 * Here we define the default xmit fifo size used for each type of UART.
60 */
61static const struct serial8250_config uart_config[] = {
62 [PORT_UNKNOWN] = {
63 .name = "unknown",
64 .fifo_size = 1,
65 .tx_loadsz = 1,
66 },
67 [PORT_8250] = {
68 .name = "8250",
69 .fifo_size = 1,
70 .tx_loadsz = 1,
71 },
72 [PORT_16450] = {
73 .name = "16450",
74 .fifo_size = 1,
75 .tx_loadsz = 1,
76 },
77 [PORT_16550] = {
78 .name = "16550",
79 .fifo_size = 1,
80 .tx_loadsz = 1,
81 },
82 [PORT_16550A] = {
83 .name = "16550A",
84 .fifo_size = 16,
85 .tx_loadsz = 16,
86 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
87 .rxtrig_bytes = {1, 4, 8, 14},
88 .flags = UART_CAP_FIFO,
89 },
90 [PORT_CIRRUS] = {
91 .name = "Cirrus",
92 .fifo_size = 1,
93 .tx_loadsz = 1,
94 },
95 [PORT_16650] = {
96 .name = "ST16650",
97 .fifo_size = 1,
98 .tx_loadsz = 1,
99 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
100 },
101 [PORT_16650V2] = {
102 .name = "ST16650V2",
103 .fifo_size = 32,
104 .tx_loadsz = 16,
105 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
106 UART_FCR_T_TRIG_00,
107 .rxtrig_bytes = {8, 16, 24, 28},
108 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
109 },
110 [PORT_16750] = {
111 .name = "TI16750",
112 .fifo_size = 64,
113 .tx_loadsz = 64,
114 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
115 UART_FCR7_64BYTE,
116 .rxtrig_bytes = {1, 16, 32, 56},
117 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
118 },
119 [PORT_STARTECH] = {
120 .name = "Startech",
121 .fifo_size = 1,
122 .tx_loadsz = 1,
123 },
124 [PORT_16C950] = {
125 .name = "16C950/954",
126 .fifo_size = 128,
127 .tx_loadsz = 128,
128 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
129 /* UART_CAP_EFR breaks billionon CF bluetooth card. */
130 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
131 },
132 [PORT_16654] = {
133 .name = "ST16654",
134 .fifo_size = 64,
135 .tx_loadsz = 32,
136 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
137 UART_FCR_T_TRIG_10,
138 .rxtrig_bytes = {8, 16, 56, 60},
139 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
140 },
141 [PORT_16850] = {
142 .name = "XR16850",
143 .fifo_size = 128,
144 .tx_loadsz = 128,
145 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
146 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
147 },
148 [PORT_RSA] = {
149 .name = "RSA",
150 .fifo_size = 2048,
151 .tx_loadsz = 2048,
152 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
153 .flags = UART_CAP_FIFO,
154 },
155 [PORT_NS16550A] = {
156 .name = "NS16550A",
157 .fifo_size = 16,
158 .tx_loadsz = 16,
159 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
160 .flags = UART_CAP_FIFO | UART_NATSEMI,
161 },
162 [PORT_XSCALE] = {
163 .name = "XScale",
164 .fifo_size = 32,
165 .tx_loadsz = 32,
166 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
167 .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
168 },
169 [PORT_OCTEON] = {
170 .name = "OCTEON",
171 .fifo_size = 64,
172 .tx_loadsz = 64,
173 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
174 .flags = UART_CAP_FIFO,
175 },
176 [PORT_AR7] = {
177 .name = "AR7",
178 .fifo_size = 16,
179 .tx_loadsz = 16,
180 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
181 .flags = UART_CAP_FIFO | UART_CAP_AFE,
182 },
183 [PORT_U6_16550A] = {
184 .name = "U6_16550A",
185 .fifo_size = 64,
186 .tx_loadsz = 64,
187 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
188 .flags = UART_CAP_FIFO | UART_CAP_AFE,
189 },
190 [PORT_TEGRA] = {
191 .name = "Tegra",
192 .fifo_size = 32,
193 .tx_loadsz = 8,
194 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
195 UART_FCR_T_TRIG_01,
196 .rxtrig_bytes = {1, 4, 8, 14},
197 .flags = UART_CAP_FIFO | UART_CAP_RTOIE,
198 },
199 [PORT_XR17D15X] = {
200 .name = "XR17D15X",
201 .fifo_size = 64,
202 .tx_loadsz = 64,
203 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
204 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
205 UART_CAP_SLEEP,
206 },
207 [PORT_XR17V35X] = {
208 .name = "XR17V35X",
209 .fifo_size = 256,
210 .tx_loadsz = 256,
211 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11 |
212 UART_FCR_T_TRIG_11,
213 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
214 UART_CAP_SLEEP,
215 },
216 [PORT_LPC3220] = {
217 .name = "LPC3220",
218 .fifo_size = 64,
219 .tx_loadsz = 32,
220 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
221 UART_FCR_R_TRIG_00 | UART_FCR_T_TRIG_00,
222 .flags = UART_CAP_FIFO,
223 },
224 [PORT_BRCM_TRUMANAGE] = {
225 .name = "TruManage",
226 .fifo_size = 1,
227 .tx_loadsz = 1024,
228 .flags = UART_CAP_HFIFO,
229 },
230 [PORT_8250_CIR] = {
231 .name = "CIR port"
232 },
233 [PORT_ALTR_16550_F32] = {
234 .name = "Altera 16550 FIFO32",
235 .fifo_size = 32,
236 .tx_loadsz = 32,
237 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
238 .flags = UART_CAP_FIFO | UART_CAP_AFE,
239 },
240 [PORT_ALTR_16550_F64] = {
241 .name = "Altera 16550 FIFO64",
242 .fifo_size = 64,
243 .tx_loadsz = 64,
244 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
245 .flags = UART_CAP_FIFO | UART_CAP_AFE,
246 },
247 [PORT_ALTR_16550_F128] = {
248 .name = "Altera 16550 FIFO128",
249 .fifo_size = 128,
250 .tx_loadsz = 128,
251 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
252 .flags = UART_CAP_FIFO | UART_CAP_AFE,
253 },
254/* tx_loadsz is set to 63-bytes instead of 64-bytes to implement
255workaround of errata A-008006 which states that tx_loadsz should be
256configured less than Maximum supported fifo bytes */
257 [PORT_16550A_FSL64] = {
258 .name = "16550A_FSL64",
259 .fifo_size = 64,
260 .tx_loadsz = 63,
261 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
262 UART_FCR7_64BYTE,
263 .flags = UART_CAP_FIFO,
264 },
Mans Rullgard3c5a0352015-10-02 17:50:31 +0100265 [PORT_RT2880] = {
266 .name = "Palmchip BK-3103",
267 .fifo_size = 16,
268 .tx_loadsz = 16,
269 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
270 .rxtrig_bytes = {1, 4, 8, 14},
271 .flags = UART_CAP_FIFO,
272 },
Peter Hurleyb6830f62015-06-27 09:19:00 -0400273};
274
275/* Uart divisor latch read */
276static int default_serial_dl_read(struct uart_8250_port *up)
277{
278 return serial_in(up, UART_DLL) | serial_in(up, UART_DLM) << 8;
279}
280
281/* Uart divisor latch write */
282static void default_serial_dl_write(struct uart_8250_port *up, int value)
283{
284 serial_out(up, UART_DLL, value & 0xff);
285 serial_out(up, UART_DLM, value >> 8 & 0xff);
286}
287
Mans Rullgard9b2256c2015-09-15 17:54:13 +0100288#ifdef CONFIG_SERIAL_8250_RT288X
Peter Hurleyb6830f62015-06-27 09:19:00 -0400289
290/* Au1x00/RT288x UART hardware has a weird register layout */
291static const s8 au_io_in_map[8] = {
292 0, /* UART_RX */
293 2, /* UART_IER */
294 3, /* UART_IIR */
295 5, /* UART_LCR */
296 6, /* UART_MCR */
297 7, /* UART_LSR */
298 8, /* UART_MSR */
299 -1, /* UART_SCR (unmapped) */
300};
301
302static const s8 au_io_out_map[8] = {
303 1, /* UART_TX */
304 2, /* UART_IER */
305 4, /* UART_FCR */
306 5, /* UART_LCR */
307 6, /* UART_MCR */
308 -1, /* UART_LSR (unmapped) */
309 -1, /* UART_MSR (unmapped) */
310 -1, /* UART_SCR (unmapped) */
311};
312
313static unsigned int au_serial_in(struct uart_port *p, int offset)
314{
315 if (offset >= ARRAY_SIZE(au_io_in_map))
316 return UINT_MAX;
317 offset = au_io_in_map[offset];
318 if (offset < 0)
319 return UINT_MAX;
320 return __raw_readl(p->membase + (offset << p->regshift));
321}
322
323static void au_serial_out(struct uart_port *p, int offset, int value)
324{
325 if (offset >= ARRAY_SIZE(au_io_out_map))
326 return;
327 offset = au_io_out_map[offset];
328 if (offset < 0)
329 return;
330 __raw_writel(value, p->membase + (offset << p->regshift));
331}
332
333/* Au1x00 haven't got a standard divisor latch */
334static int au_serial_dl_read(struct uart_8250_port *up)
335{
336 return __raw_readl(up->port.membase + 0x28);
337}
338
339static void au_serial_dl_write(struct uart_8250_port *up, int value)
340{
341 __raw_writel(value, up->port.membase + 0x28);
342}
343
344#endif
345
346static unsigned int hub6_serial_in(struct uart_port *p, int offset)
347{
348 offset = offset << p->regshift;
349 outb(p->hub6 - 1 + offset, p->iobase);
350 return inb(p->iobase + 1);
351}
352
353static void hub6_serial_out(struct uart_port *p, int offset, int value)
354{
355 offset = offset << p->regshift;
356 outb(p->hub6 - 1 + offset, p->iobase);
357 outb(value, p->iobase + 1);
358}
359
360static unsigned int mem_serial_in(struct uart_port *p, int offset)
361{
362 offset = offset << p->regshift;
363 return readb(p->membase + offset);
364}
365
366static void mem_serial_out(struct uart_port *p, int offset, int value)
367{
368 offset = offset << p->regshift;
369 writeb(value, p->membase + offset);
370}
371
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900372static void mem16_serial_out(struct uart_port *p, int offset, int value)
373{
374 offset = offset << p->regshift;
375 writew(value, p->membase + offset);
376}
377
378static unsigned int mem16_serial_in(struct uart_port *p, int offset)
379{
380 offset = offset << p->regshift;
381 return readw(p->membase + offset);
382}
383
Peter Hurleyb6830f62015-06-27 09:19:00 -0400384static void mem32_serial_out(struct uart_port *p, int offset, int value)
385{
386 offset = offset << p->regshift;
387 writel(value, p->membase + offset);
388}
389
390static unsigned int mem32_serial_in(struct uart_port *p, int offset)
391{
392 offset = offset << p->regshift;
393 return readl(p->membase + offset);
394}
395
396static void mem32be_serial_out(struct uart_port *p, int offset, int value)
397{
398 offset = offset << p->regshift;
399 iowrite32be(value, p->membase + offset);
400}
401
402static unsigned int mem32be_serial_in(struct uart_port *p, int offset)
403{
404 offset = offset << p->regshift;
405 return ioread32be(p->membase + offset);
406}
407
408static unsigned int io_serial_in(struct uart_port *p, int offset)
409{
410 offset = offset << p->regshift;
411 return inb(p->iobase + offset);
412}
413
414static void io_serial_out(struct uart_port *p, int offset, int value)
415{
416 offset = offset << p->regshift;
417 outb(value, p->iobase + offset);
418}
419
420static int serial8250_default_handle_irq(struct uart_port *port);
421static int exar_handle_irq(struct uart_port *port);
422
423static void set_io_from_upio(struct uart_port *p)
424{
425 struct uart_8250_port *up = up_to_u8250p(p);
426
427 up->dl_read = default_serial_dl_read;
428 up->dl_write = default_serial_dl_write;
429
430 switch (p->iotype) {
431 case UPIO_HUB6:
432 p->serial_in = hub6_serial_in;
433 p->serial_out = hub6_serial_out;
434 break;
435
436 case UPIO_MEM:
437 p->serial_in = mem_serial_in;
438 p->serial_out = mem_serial_out;
439 break;
440
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900441 case UPIO_MEM16:
442 p->serial_in = mem16_serial_in;
443 p->serial_out = mem16_serial_out;
444 break;
445
Peter Hurleyb6830f62015-06-27 09:19:00 -0400446 case UPIO_MEM32:
447 p->serial_in = mem32_serial_in;
448 p->serial_out = mem32_serial_out;
449 break;
450
451 case UPIO_MEM32BE:
452 p->serial_in = mem32be_serial_in;
453 p->serial_out = mem32be_serial_out;
454 break;
455
Mans Rullgard9b2256c2015-09-15 17:54:13 +0100456#ifdef CONFIG_SERIAL_8250_RT288X
Peter Hurleyb6830f62015-06-27 09:19:00 -0400457 case UPIO_AU:
458 p->serial_in = au_serial_in;
459 p->serial_out = au_serial_out;
460 up->dl_read = au_serial_dl_read;
461 up->dl_write = au_serial_dl_write;
462 break;
463#endif
464
465 default:
466 p->serial_in = io_serial_in;
467 p->serial_out = io_serial_out;
468 break;
469 }
470 /* Remember loaded iotype */
471 up->cur_iotype = p->iotype;
472 p->handle_irq = serial8250_default_handle_irq;
473}
474
475static void
476serial_port_out_sync(struct uart_port *p, int offset, int value)
477{
478 switch (p->iotype) {
479 case UPIO_MEM:
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900480 case UPIO_MEM16:
Peter Hurleyb6830f62015-06-27 09:19:00 -0400481 case UPIO_MEM32:
482 case UPIO_MEM32BE:
483 case UPIO_AU:
484 p->serial_out(p, offset, value);
485 p->serial_in(p, UART_LCR); /* safe, no side-effects */
486 break;
487 default:
488 p->serial_out(p, offset, value);
489 }
490}
491
492/*
493 * For the 16C950
494 */
495static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
496{
497 serial_out(up, UART_SCR, offset);
498 serial_out(up, UART_ICR, value);
499}
500
501static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
502{
503 unsigned int value;
504
505 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
506 serial_out(up, UART_SCR, offset);
507 value = serial_in(up, UART_ICR);
508 serial_icr_write(up, UART_ACR, up->acr);
509
510 return value;
511}
512
513/*
514 * FIFO support.
515 */
516static void serial8250_clear_fifos(struct uart_8250_port *p)
517{
518 if (p->capabilities & UART_CAP_FIFO) {
519 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
520 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
521 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
522 serial_out(p, UART_FCR, 0);
523 }
524}
525
Matwey V. Kornilove490c912016-02-01 21:09:21 +0300526static inline void serial8250_em485_rts_after_send(struct uart_8250_port *p)
527{
528 unsigned char mcr = serial_in(p, UART_MCR);
529
530 if (p->port.rs485.flags & SER_RS485_RTS_AFTER_SEND)
531 mcr |= UART_MCR_RTS;
532 else
533 mcr &= ~UART_MCR_RTS;
534 serial_out(p, UART_MCR, mcr);
535}
536
537static void serial8250_em485_handle_start_tx(unsigned long arg);
538static void serial8250_em485_handle_stop_tx(unsigned long arg);
539
Peter Hurleyb6830f62015-06-27 09:19:00 -0400540void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
541{
542 serial8250_clear_fifos(p);
543 serial_out(p, UART_FCR, p->fcr);
544}
545EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);
546
547void serial8250_rpm_get(struct uart_8250_port *p)
548{
549 if (!(p->capabilities & UART_CAP_RPM))
550 return;
551 pm_runtime_get_sync(p->port.dev);
552}
553EXPORT_SYMBOL_GPL(serial8250_rpm_get);
554
555void serial8250_rpm_put(struct uart_8250_port *p)
556{
557 if (!(p->capabilities & UART_CAP_RPM))
558 return;
559 pm_runtime_mark_last_busy(p->port.dev);
560 pm_runtime_put_autosuspend(p->port.dev);
561}
562EXPORT_SYMBOL_GPL(serial8250_rpm_put);
563
Matwey V. Kornilove490c912016-02-01 21:09:21 +0300564/**
565 * serial8250_em485_init() - put uart_8250_port into rs485 emulating
566 * @p: uart_8250_port port instance
567 *
568 * The function is used to start rs485 software emulating on the
569 * &struct uart_8250_port* @p. Namely, RTS is switched before/after
570 * transmission. The function is idempotent, so it is safe to call it
571 * multiple times.
572 *
573 * The caller MUST enable interrupt on empty shift register before
574 * calling serial8250_em485_init(). This interrupt is not a part of
575 * 8250 standard, but implementation defined.
576 *
577 * The function is supposed to be called from .rs485_config callback
578 * or from any other callback protected with p->port.lock spinlock.
579 *
580 * See also serial8250_em485_destroy()
581 *
582 * Return 0 - success, -errno - otherwise
583 */
584int serial8250_em485_init(struct uart_8250_port *p)
585{
586 if (p->em485 != NULL)
587 return 0;
588
589 p->em485 = kmalloc(sizeof(struct uart_8250_em485), GFP_KERNEL);
590 if (p->em485 == NULL)
591 return -ENOMEM;
592
593 setup_timer(&p->em485->stop_tx_timer,
594 serial8250_em485_handle_stop_tx, (unsigned long)p);
595 setup_timer(&p->em485->start_tx_timer,
596 serial8250_em485_handle_start_tx, (unsigned long)p);
597 p->em485->active_timer = NULL;
598
599 serial8250_em485_rts_after_send(p);
600
601 return 0;
602}
603EXPORT_SYMBOL_GPL(serial8250_em485_init);
604
605/**
606 * serial8250_em485_destroy() - put uart_8250_port into normal state
607 * @p: uart_8250_port port instance
608 *
609 * The function is used to stop rs485 software emulating on the
610 * &struct uart_8250_port* @p. The function is idempotent, so it is safe to
611 * call it multiple times.
612 *
613 * The function is supposed to be called from .rs485_config callback
614 * or from any other callback protected with p->port.lock spinlock.
615 *
616 * See also serial8250_em485_init()
617 */
618void serial8250_em485_destroy(struct uart_8250_port *p)
619{
620 if (p->em485 == NULL)
621 return;
622
623 del_timer(&p->em485->start_tx_timer);
624 del_timer(&p->em485->stop_tx_timer);
625
626 kfree(p->em485);
627 p->em485 = NULL;
628}
629EXPORT_SYMBOL_GPL(serial8250_em485_destroy);
630
Peter Hurleyb6830f62015-06-27 09:19:00 -0400631/*
632 * These two wrappers ensure that enable_runtime_pm_tx() can be called more than
633 * once and disable_runtime_pm_tx() will still disable RPM because the fifo is
634 * empty and the HW can idle again.
635 */
636static void serial8250_rpm_get_tx(struct uart_8250_port *p)
637{
638 unsigned char rpm_active;
639
640 if (!(p->capabilities & UART_CAP_RPM))
641 return;
642
643 rpm_active = xchg(&p->rpm_tx_active, 1);
644 if (rpm_active)
645 return;
646 pm_runtime_get_sync(p->port.dev);
647}
648
649static void serial8250_rpm_put_tx(struct uart_8250_port *p)
650{
651 unsigned char rpm_active;
652
653 if (!(p->capabilities & UART_CAP_RPM))
654 return;
655
656 rpm_active = xchg(&p->rpm_tx_active, 0);
657 if (!rpm_active)
658 return;
659 pm_runtime_mark_last_busy(p->port.dev);
660 pm_runtime_put_autosuspend(p->port.dev);
661}
662
663/*
664 * IER sleep support. UARTs which have EFRs need the "extended
665 * capability" bit enabled. Note that on XR16C850s, we need to
666 * reset LCR to write to IER.
667 */
668static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
669{
670 unsigned char lcr = 0, efr = 0;
671 /*
672 * Exar UARTs have a SLEEP register that enables or disables
673 * each UART to enter sleep mode separately. On the XR17V35x the
674 * register is accessible to each UART at the UART_EXAR_SLEEP
675 * offset but the UART channel may only write to the corresponding
676 * bit.
677 */
678 serial8250_rpm_get(p);
679 if ((p->port.type == PORT_XR17V35X) ||
680 (p->port.type == PORT_XR17D15X)) {
681 serial_out(p, UART_EXAR_SLEEP, sleep ? 0xff : 0);
682 goto out;
683 }
684
685 if (p->capabilities & UART_CAP_SLEEP) {
686 if (p->capabilities & UART_CAP_EFR) {
687 lcr = serial_in(p, UART_LCR);
688 efr = serial_in(p, UART_EFR);
689 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
690 serial_out(p, UART_EFR, UART_EFR_ECB);
691 serial_out(p, UART_LCR, 0);
692 }
693 serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
694 if (p->capabilities & UART_CAP_EFR) {
695 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
696 serial_out(p, UART_EFR, efr);
697 serial_out(p, UART_LCR, lcr);
698 }
699 }
700out:
701 serial8250_rpm_put(p);
702}
703
704#ifdef CONFIG_SERIAL_8250_RSA
705/*
706 * Attempts to turn on the RSA FIFO. Returns zero on failure.
707 * We set the port uart clock rate if we succeed.
708 */
709static int __enable_rsa(struct uart_8250_port *up)
710{
711 unsigned char mode;
712 int result;
713
714 mode = serial_in(up, UART_RSA_MSR);
715 result = mode & UART_RSA_MSR_FIFO;
716
717 if (!result) {
718 serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
719 mode = serial_in(up, UART_RSA_MSR);
720 result = mode & UART_RSA_MSR_FIFO;
721 }
722
723 if (result)
724 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
725
726 return result;
727}
728
729static void enable_rsa(struct uart_8250_port *up)
730{
731 if (up->port.type == PORT_RSA) {
732 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
733 spin_lock_irq(&up->port.lock);
734 __enable_rsa(up);
735 spin_unlock_irq(&up->port.lock);
736 }
737 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
738 serial_out(up, UART_RSA_FRR, 0);
739 }
740}
741
742/*
743 * Attempts to turn off the RSA FIFO. Returns zero on failure.
744 * It is unknown why interrupts were disabled in here. However,
745 * the caller is expected to preserve this behaviour by grabbing
746 * the spinlock before calling this function.
747 */
748static void disable_rsa(struct uart_8250_port *up)
749{
750 unsigned char mode;
751 int result;
752
753 if (up->port.type == PORT_RSA &&
754 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
755 spin_lock_irq(&up->port.lock);
756
757 mode = serial_in(up, UART_RSA_MSR);
758 result = !(mode & UART_RSA_MSR_FIFO);
759
760 if (!result) {
761 serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
762 mode = serial_in(up, UART_RSA_MSR);
763 result = !(mode & UART_RSA_MSR_FIFO);
764 }
765
766 if (result)
767 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
768 spin_unlock_irq(&up->port.lock);
769 }
770}
771#endif /* CONFIG_SERIAL_8250_RSA */
772
773/*
774 * This is a quickie test to see how big the FIFO is.
775 * It doesn't work at all the time, more's the pity.
776 */
777static int size_fifo(struct uart_8250_port *up)
778{
779 unsigned char old_fcr, old_mcr, old_lcr;
780 unsigned short old_dl;
781 int count;
782
783 old_lcr = serial_in(up, UART_LCR);
784 serial_out(up, UART_LCR, 0);
785 old_fcr = serial_in(up, UART_FCR);
786 old_mcr = serial_in(up, UART_MCR);
787 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
788 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
789 serial_out(up, UART_MCR, UART_MCR_LOOP);
790 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
791 old_dl = serial_dl_read(up);
792 serial_dl_write(up, 0x0001);
793 serial_out(up, UART_LCR, 0x03);
794 for (count = 0; count < 256; count++)
795 serial_out(up, UART_TX, count);
796 mdelay(20);/* FIXME - schedule_timeout */
797 for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
798 (count < 256); count++)
799 serial_in(up, UART_RX);
800 serial_out(up, UART_FCR, old_fcr);
801 serial_out(up, UART_MCR, old_mcr);
802 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
803 serial_dl_write(up, old_dl);
804 serial_out(up, UART_LCR, old_lcr);
805
806 return count;
807}
808
809/*
810 * Read UART ID using the divisor method - set DLL and DLM to zero
811 * and the revision will be in DLL and device type in DLM. We
812 * preserve the device state across this.
813 */
814static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
815{
816 unsigned char old_dll, old_dlm, old_lcr;
817 unsigned int id;
818
819 old_lcr = serial_in(p, UART_LCR);
820 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
821
822 old_dll = serial_in(p, UART_DLL);
823 old_dlm = serial_in(p, UART_DLM);
824
825 serial_out(p, UART_DLL, 0);
826 serial_out(p, UART_DLM, 0);
827
828 id = serial_in(p, UART_DLL) | serial_in(p, UART_DLM) << 8;
829
830 serial_out(p, UART_DLL, old_dll);
831 serial_out(p, UART_DLM, old_dlm);
832 serial_out(p, UART_LCR, old_lcr);
833
834 return id;
835}
836
837/*
838 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
839 * When this function is called we know it is at least a StarTech
840 * 16650 V2, but it might be one of several StarTech UARTs, or one of
841 * its clones. (We treat the broken original StarTech 16650 V1 as a
842 * 16550, and why not? Startech doesn't seem to even acknowledge its
843 * existence.)
844 *
845 * What evil have men's minds wrought...
846 */
847static void autoconfig_has_efr(struct uart_8250_port *up)
848{
849 unsigned int id1, id2, id3, rev;
850
851 /*
852 * Everything with an EFR has SLEEP
853 */
854 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
855
856 /*
857 * First we check to see if it's an Oxford Semiconductor UART.
858 *
859 * If we have to do this here because some non-National
860 * Semiconductor clone chips lock up if you try writing to the
861 * LSR register (which serial_icr_read does)
862 */
863
864 /*
865 * Check for Oxford Semiconductor 16C950.
866 *
867 * EFR [4] must be set else this test fails.
868 *
869 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
870 * claims that it's needed for 952 dual UART's (which are not
871 * recommended for new designs).
872 */
873 up->acr = 0;
874 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
875 serial_out(up, UART_EFR, UART_EFR_ECB);
876 serial_out(up, UART_LCR, 0x00);
877 id1 = serial_icr_read(up, UART_ID1);
878 id2 = serial_icr_read(up, UART_ID2);
879 id3 = serial_icr_read(up, UART_ID3);
880 rev = serial_icr_read(up, UART_REV);
881
882 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
883
884 if (id1 == 0x16 && id2 == 0xC9 &&
885 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
886 up->port.type = PORT_16C950;
887
888 /*
889 * Enable work around for the Oxford Semiconductor 952 rev B
890 * chip which causes it to seriously miscalculate baud rates
891 * when DLL is 0.
892 */
893 if (id3 == 0x52 && rev == 0x01)
894 up->bugs |= UART_BUG_QUOT;
895 return;
896 }
897
898 /*
899 * We check for a XR16C850 by setting DLL and DLM to 0, and then
900 * reading back DLL and DLM. The chip type depends on the DLM
901 * value read back:
902 * 0x10 - XR16C850 and the DLL contains the chip revision.
903 * 0x12 - XR16C2850.
904 * 0x14 - XR16C854.
905 */
906 id1 = autoconfig_read_divisor_id(up);
907 DEBUG_AUTOCONF("850id=%04x ", id1);
908
909 id2 = id1 >> 8;
910 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
911 up->port.type = PORT_16850;
912 return;
913 }
914
915 /*
916 * It wasn't an XR16C850.
917 *
918 * We distinguish between the '654 and the '650 by counting
919 * how many bytes are in the FIFO. I'm using this for now,
920 * since that's the technique that was sent to me in the
921 * serial driver update, but I'm not convinced this works.
922 * I've had problems doing this in the past. -TYT
923 */
924 if (size_fifo(up) == 64)
925 up->port.type = PORT_16654;
926 else
927 up->port.type = PORT_16650V2;
928}
929
930/*
931 * We detected a chip without a FIFO. Only two fall into
932 * this category - the original 8250 and the 16450. The
933 * 16450 has a scratch register (accessible with LCR=0)
934 */
935static void autoconfig_8250(struct uart_8250_port *up)
936{
937 unsigned char scratch, status1, status2;
938
939 up->port.type = PORT_8250;
940
941 scratch = serial_in(up, UART_SCR);
942 serial_out(up, UART_SCR, 0xa5);
943 status1 = serial_in(up, UART_SCR);
944 serial_out(up, UART_SCR, 0x5a);
945 status2 = serial_in(up, UART_SCR);
946 serial_out(up, UART_SCR, scratch);
947
948 if (status1 == 0xa5 && status2 == 0x5a)
949 up->port.type = PORT_16450;
950}
951
952static int broken_efr(struct uart_8250_port *up)
953{
954 /*
955 * Exar ST16C2550 "A2" devices incorrectly detect as
956 * having an EFR, and report an ID of 0x0201. See
957 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
958 */
959 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
960 return 1;
961
962 return 0;
963}
964
965/*
966 * We know that the chip has FIFOs. Does it have an EFR? The
967 * EFR is located in the same register position as the IIR and
968 * we know the top two bits of the IIR are currently set. The
969 * EFR should contain zero. Try to read the EFR.
970 */
971static void autoconfig_16550a(struct uart_8250_port *up)
972{
973 unsigned char status1, status2;
974 unsigned int iersave;
975
976 up->port.type = PORT_16550A;
977 up->capabilities |= UART_CAP_FIFO;
978
979 /*
980 * XR17V35x UARTs have an extra divisor register, DLD
981 * that gets enabled with when DLAB is set which will
982 * cause the device to incorrectly match and assign
983 * port type to PORT_16650. The EFR for this UART is
984 * found at offset 0x09. Instead check the Deice ID (DVID)
985 * register for a 2, 4 or 8 port UART.
986 */
987 if (up->port.flags & UPF_EXAR_EFR) {
988 status1 = serial_in(up, UART_EXAR_DVID);
989 if (status1 == 0x82 || status1 == 0x84 || status1 == 0x88) {
990 DEBUG_AUTOCONF("Exar XR17V35x ");
991 up->port.type = PORT_XR17V35X;
992 up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
993 UART_CAP_SLEEP;
994
995 return;
996 }
997
998 }
999
1000 /*
1001 * Check for presence of the EFR when DLAB is set.
1002 * Only ST16C650V1 UARTs pass this test.
1003 */
1004 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1005 if (serial_in(up, UART_EFR) == 0) {
1006 serial_out(up, UART_EFR, 0xA8);
1007 if (serial_in(up, UART_EFR) != 0) {
1008 DEBUG_AUTOCONF("EFRv1 ");
1009 up->port.type = PORT_16650;
1010 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
1011 } else {
1012 serial_out(up, UART_LCR, 0);
1013 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
1014 UART_FCR7_64BYTE);
1015 status1 = serial_in(up, UART_IIR) >> 5;
1016 serial_out(up, UART_FCR, 0);
1017 serial_out(up, UART_LCR, 0);
1018
1019 if (status1 == 7)
1020 up->port.type = PORT_16550A_FSL64;
1021 else
1022 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
1023 }
1024 serial_out(up, UART_EFR, 0);
1025 return;
1026 }
1027
1028 /*
1029 * Maybe it requires 0xbf to be written to the LCR.
1030 * (other ST16C650V2 UARTs, TI16C752A, etc)
1031 */
1032 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1033 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
1034 DEBUG_AUTOCONF("EFRv2 ");
1035 autoconfig_has_efr(up);
1036 return;
1037 }
1038
1039 /*
1040 * Check for a National Semiconductor SuperIO chip.
1041 * Attempt to switch to bank 2, read the value of the LOOP bit
1042 * from EXCR1. Switch back to bank 0, change it in MCR. Then
1043 * switch back to bank 2, read it from EXCR1 again and check
1044 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
1045 */
1046 serial_out(up, UART_LCR, 0);
1047 status1 = serial_in(up, UART_MCR);
1048 serial_out(up, UART_LCR, 0xE0);
1049 status2 = serial_in(up, 0x02); /* EXCR1 */
1050
1051 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
1052 serial_out(up, UART_LCR, 0);
1053 serial_out(up, UART_MCR, status1 ^ UART_MCR_LOOP);
1054 serial_out(up, UART_LCR, 0xE0);
1055 status2 = serial_in(up, 0x02); /* EXCR1 */
1056 serial_out(up, UART_LCR, 0);
1057 serial_out(up, UART_MCR, status1);
1058
1059 if ((status2 ^ status1) & UART_MCR_LOOP) {
1060 unsigned short quot;
1061
1062 serial_out(up, UART_LCR, 0xE0);
1063
1064 quot = serial_dl_read(up);
1065 quot <<= 3;
1066
1067 if (ns16550a_goto_highspeed(up))
1068 serial_dl_write(up, quot);
1069
1070 serial_out(up, UART_LCR, 0);
1071
1072 up->port.uartclk = 921600*16;
1073 up->port.type = PORT_NS16550A;
1074 up->capabilities |= UART_NATSEMI;
1075 return;
1076 }
1077 }
1078
1079 /*
1080 * No EFR. Try to detect a TI16750, which only sets bit 5 of
1081 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1082 * Try setting it with and without DLAB set. Cheap clones
1083 * set bit 5 without DLAB set.
1084 */
1085 serial_out(up, UART_LCR, 0);
1086 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1087 status1 = serial_in(up, UART_IIR) >> 5;
1088 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1089 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1090 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1091 status2 = serial_in(up, UART_IIR) >> 5;
1092 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1093 serial_out(up, UART_LCR, 0);
1094
1095 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1096
1097 if (status1 == 6 && status2 == 7) {
1098 up->port.type = PORT_16750;
1099 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1100 return;
1101 }
1102
1103 /*
1104 * Try writing and reading the UART_IER_UUE bit (b6).
1105 * If it works, this is probably one of the Xscale platform's
1106 * internal UARTs.
1107 * We're going to explicitly set the UUE bit to 0 before
1108 * trying to write and read a 1 just to make sure it's not
1109 * already a 1 and maybe locked there before we even start start.
1110 */
1111 iersave = serial_in(up, UART_IER);
1112 serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
1113 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1114 /*
1115 * OK it's in a known zero state, try writing and reading
1116 * without disturbing the current state of the other bits.
1117 */
1118 serial_out(up, UART_IER, iersave | UART_IER_UUE);
1119 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1120 /*
1121 * It's an Xscale.
1122 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1123 */
1124 DEBUG_AUTOCONF("Xscale ");
1125 up->port.type = PORT_XSCALE;
1126 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
1127 return;
1128 }
1129 } else {
1130 /*
1131 * If we got here we couldn't force the IER_UUE bit to 0.
1132 * Log it and continue.
1133 */
1134 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1135 }
1136 serial_out(up, UART_IER, iersave);
1137
1138 /*
1139 * Exar uarts have EFR in a weird location
1140 */
1141 if (up->port.flags & UPF_EXAR_EFR) {
1142 DEBUG_AUTOCONF("Exar XR17D15x ");
1143 up->port.type = PORT_XR17D15X;
1144 up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
1145 UART_CAP_SLEEP;
1146
1147 return;
1148 }
1149
1150 /*
1151 * We distinguish between 16550A and U6 16550A by counting
1152 * how many bytes are in the FIFO.
1153 */
1154 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1155 up->port.type = PORT_U6_16550A;
1156 up->capabilities |= UART_CAP_AFE;
1157 }
1158}
1159
1160/*
1161 * This routine is called by rs_init() to initialize a specific serial
1162 * port. It determines what type of UART chip this serial port is
1163 * using: 8250, 16450, 16550, 16550A. The important question is
1164 * whether or not this UART is a 16550A or not, since this will
1165 * determine whether or not we can use its FIFO features or not.
1166 */
1167static void autoconfig(struct uart_8250_port *up)
1168{
1169 unsigned char status1, scratch, scratch2, scratch3;
1170 unsigned char save_lcr, save_mcr;
1171 struct uart_port *port = &up->port;
1172 unsigned long flags;
1173 unsigned int old_capabilities;
1174
1175 if (!port->iobase && !port->mapbase && !port->membase)
1176 return;
1177
1178 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
1179 serial_index(port), port->iobase, port->membase);
1180
1181 /*
1182 * We really do need global IRQs disabled here - we're going to
1183 * be frobbing the chips IRQ enable register to see if it exists.
1184 */
1185 spin_lock_irqsave(&port->lock, flags);
1186
1187 up->capabilities = 0;
1188 up->bugs = 0;
1189
1190 if (!(port->flags & UPF_BUGGY_UART)) {
1191 /*
1192 * Do a simple existence test first; if we fail this,
1193 * there's no point trying anything else.
1194 *
1195 * 0x80 is used as a nonsense port to prevent against
1196 * false positives due to ISA bus float. The
1197 * assumption is that 0x80 is a non-existent port;
1198 * which should be safe since include/asm/io.h also
1199 * makes this assumption.
1200 *
1201 * Note: this is safe as long as MCR bit 4 is clear
1202 * and the device is in "PC" mode.
1203 */
1204 scratch = serial_in(up, UART_IER);
1205 serial_out(up, UART_IER, 0);
1206#ifdef __i386__
1207 outb(0xff, 0x080);
1208#endif
1209 /*
1210 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1211 * 16C754B) allow only to modify them if an EFR bit is set.
1212 */
1213 scratch2 = serial_in(up, UART_IER) & 0x0f;
1214 serial_out(up, UART_IER, 0x0F);
1215#ifdef __i386__
1216 outb(0, 0x080);
1217#endif
1218 scratch3 = serial_in(up, UART_IER) & 0x0f;
1219 serial_out(up, UART_IER, scratch);
1220 if (scratch2 != 0 || scratch3 != 0x0F) {
1221 /*
1222 * We failed; there's nothing here
1223 */
1224 spin_unlock_irqrestore(&port->lock, flags);
1225 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1226 scratch2, scratch3);
1227 goto out;
1228 }
1229 }
1230
1231 save_mcr = serial_in(up, UART_MCR);
1232 save_lcr = serial_in(up, UART_LCR);
1233
1234 /*
1235 * Check to see if a UART is really there. Certain broken
1236 * internal modems based on the Rockwell chipset fail this
1237 * test, because they apparently don't implement the loopback
1238 * test mode. So this test is skipped on the COM 1 through
1239 * COM 4 ports. This *should* be safe, since no board
1240 * manufacturer would be stupid enough to design a board
1241 * that conflicts with COM 1-4 --- we hope!
1242 */
1243 if (!(port->flags & UPF_SKIP_TEST)) {
1244 serial_out(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1245 status1 = serial_in(up, UART_MSR) & 0xF0;
1246 serial_out(up, UART_MCR, save_mcr);
1247 if (status1 != 0x90) {
1248 spin_unlock_irqrestore(&port->lock, flags);
1249 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1250 status1);
1251 goto out;
1252 }
1253 }
1254
1255 /*
1256 * We're pretty sure there's a port here. Lets find out what
1257 * type of port it is. The IIR top two bits allows us to find
1258 * out if it's 8250 or 16450, 16550, 16550A or later. This
1259 * determines what we test for next.
1260 *
1261 * We also initialise the EFR (if any) to zero for later. The
1262 * EFR occupies the same register location as the FCR and IIR.
1263 */
1264 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1265 serial_out(up, UART_EFR, 0);
1266 serial_out(up, UART_LCR, 0);
1267
1268 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1269 scratch = serial_in(up, UART_IIR) >> 6;
1270
1271 switch (scratch) {
1272 case 0:
1273 autoconfig_8250(up);
1274 break;
1275 case 1:
1276 port->type = PORT_UNKNOWN;
1277 break;
1278 case 2:
1279 port->type = PORT_16550;
1280 break;
1281 case 3:
1282 autoconfig_16550a(up);
1283 break;
1284 }
1285
1286#ifdef CONFIG_SERIAL_8250_RSA
1287 /*
1288 * Only probe for RSA ports if we got the region.
1289 */
1290 if (port->type == PORT_16550A && up->probe & UART_PROBE_RSA &&
1291 __enable_rsa(up))
1292 port->type = PORT_RSA;
1293#endif
1294
1295 serial_out(up, UART_LCR, save_lcr);
1296
1297 port->fifosize = uart_config[up->port.type].fifo_size;
1298 old_capabilities = up->capabilities;
1299 up->capabilities = uart_config[port->type].flags;
1300 up->tx_loadsz = uart_config[port->type].tx_loadsz;
1301
1302 if (port->type == PORT_UNKNOWN)
1303 goto out_lock;
1304
1305 /*
1306 * Reset the UART.
1307 */
1308#ifdef CONFIG_SERIAL_8250_RSA
1309 if (port->type == PORT_RSA)
1310 serial_out(up, UART_RSA_FRR, 0);
1311#endif
1312 serial_out(up, UART_MCR, save_mcr);
1313 serial8250_clear_fifos(up);
1314 serial_in(up, UART_RX);
1315 if (up->capabilities & UART_CAP_UUE)
1316 serial_out(up, UART_IER, UART_IER_UUE);
1317 else
1318 serial_out(up, UART_IER, 0);
1319
1320out_lock:
1321 spin_unlock_irqrestore(&port->lock, flags);
1322 if (up->capabilities != old_capabilities) {
1323 printk(KERN_WARNING
1324 "ttyS%d: detected caps %08x should be %08x\n",
1325 serial_index(port), old_capabilities,
1326 up->capabilities);
1327 }
1328out:
1329 DEBUG_AUTOCONF("iir=%d ", scratch);
1330 DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
1331}
1332
1333static void autoconfig_irq(struct uart_8250_port *up)
1334{
1335 struct uart_port *port = &up->port;
1336 unsigned char save_mcr, save_ier;
1337 unsigned char save_ICP = 0;
1338 unsigned int ICP = 0;
1339 unsigned long irqs;
1340 int irq;
1341
1342 if (port->flags & UPF_FOURPORT) {
1343 ICP = (port->iobase & 0xfe0) | 0x1f;
1344 save_ICP = inb_p(ICP);
1345 outb_p(0x80, ICP);
1346 inb_p(ICP);
1347 }
1348
Taichi Kageyama9a23a1d2015-08-17 02:45:29 +00001349 if (uart_console(port))
1350 console_lock();
1351
Peter Hurleyb6830f62015-06-27 09:19:00 -04001352 /* forget possible initially masked and pending IRQ */
1353 probe_irq_off(probe_irq_on());
1354 save_mcr = serial_in(up, UART_MCR);
1355 save_ier = serial_in(up, UART_IER);
1356 serial_out(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
1357
1358 irqs = probe_irq_on();
1359 serial_out(up, UART_MCR, 0);
1360 udelay(10);
1361 if (port->flags & UPF_FOURPORT) {
1362 serial_out(up, UART_MCR,
1363 UART_MCR_DTR | UART_MCR_RTS);
1364 } else {
1365 serial_out(up, UART_MCR,
1366 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1367 }
1368 serial_out(up, UART_IER, 0x0f); /* enable all intrs */
1369 serial_in(up, UART_LSR);
1370 serial_in(up, UART_RX);
1371 serial_in(up, UART_IIR);
1372 serial_in(up, UART_MSR);
1373 serial_out(up, UART_TX, 0xFF);
1374 udelay(20);
1375 irq = probe_irq_off(irqs);
1376
1377 serial_out(up, UART_MCR, save_mcr);
1378 serial_out(up, UART_IER, save_ier);
1379
1380 if (port->flags & UPF_FOURPORT)
1381 outb_p(save_ICP, ICP);
1382
Taichi Kageyama9a23a1d2015-08-17 02:45:29 +00001383 if (uart_console(port))
1384 console_unlock();
1385
Peter Hurleyb6830f62015-06-27 09:19:00 -04001386 port->irq = (irq > 0) ? irq : 0;
1387}
1388
Matwey V. Kornilova07a70b2016-02-01 21:09:20 +03001389static void serial8250_stop_rx(struct uart_port *port)
1390{
1391 struct uart_8250_port *up = up_to_u8250p(port);
1392
1393 serial8250_rpm_get(up);
1394
1395 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1396 up->port.read_status_mask &= ~UART_LSR_DR;
1397 serial_port_out(port, UART_IER, up->ier);
1398
1399 serial8250_rpm_put(up);
1400}
1401
Matwey V. Kornilove490c912016-02-01 21:09:21 +03001402static void __do_stop_tx_rs485(struct uart_8250_port *p)
1403{
1404 if (!p->em485)
1405 return;
1406
1407 serial8250_em485_rts_after_send(p);
1408 /*
1409 * Empty the RX FIFO, we are not interested in anything
1410 * received during the half-duplex transmission.
1411 */
1412 if (!(p->port.rs485.flags & SER_RS485_RX_DURING_TX))
1413 serial8250_clear_fifos(p);
1414}
1415
1416static void serial8250_em485_handle_stop_tx(unsigned long arg)
1417{
1418 struct uart_8250_port *p = (struct uart_8250_port *)arg;
1419 struct uart_8250_em485 *em485 = p->em485;
1420 unsigned long flags;
1421
1422 spin_lock_irqsave(&p->port.lock, flags);
1423 if (em485 &&
1424 em485->active_timer == &em485->stop_tx_timer) {
1425 __do_stop_tx_rs485(p);
1426 em485->active_timer = NULL;
1427 }
1428 spin_unlock_irqrestore(&p->port.lock, flags);
1429}
1430
1431static void __stop_tx_rs485(struct uart_8250_port *p)
1432{
1433 struct uart_8250_em485 *em485 = p->em485;
1434
1435 if (!em485)
1436 return;
1437
1438 /*
1439 * __do_stop_tx_rs485 is going to set RTS according to config
1440 * AND flush RX FIFO if required.
1441 */
1442 if (p->port.rs485.delay_rts_after_send > 0) {
1443 em485->active_timer = &em485->stop_tx_timer;
1444 mod_timer(&em485->stop_tx_timer, jiffies +
1445 p->port.rs485.delay_rts_after_send * HZ / 1000);
1446 } else {
1447 __do_stop_tx_rs485(p);
1448 }
1449}
1450
1451static inline void __do_stop_tx(struct uart_8250_port *p)
Peter Hurleyb6830f62015-06-27 09:19:00 -04001452{
1453 if (p->ier & UART_IER_THRI) {
1454 p->ier &= ~UART_IER_THRI;
1455 serial_out(p, UART_IER, p->ier);
1456 serial8250_rpm_put_tx(p);
1457 }
1458}
1459
Matwey V. Kornilove490c912016-02-01 21:09:21 +03001460static inline void __stop_tx(struct uart_8250_port *p)
1461{
1462 struct uart_8250_em485 *em485 = p->em485;
1463
1464 if (em485) {
1465 unsigned char lsr = serial_in(p, UART_LSR);
1466 /*
1467 * To provide required timeing and allow FIFO transfer,
1468 * __stop_tx_rs485 must be called only when both FIFO and
1469 * shift register are empty. It is for device driver to enable
1470 * interrupt on TEMT.
1471 */
1472 if ((lsr & BOTH_EMPTY) != BOTH_EMPTY)
1473 return;
1474
1475 del_timer(&em485->start_tx_timer);
1476 em485->active_timer = NULL;
1477 }
1478 __do_stop_tx(p);
1479 __stop_tx_rs485(p);
1480}
1481
Peter Hurleyb6830f62015-06-27 09:19:00 -04001482static void serial8250_stop_tx(struct uart_port *port)
1483{
1484 struct uart_8250_port *up = up_to_u8250p(port);
1485
1486 serial8250_rpm_get(up);
1487 __stop_tx(up);
1488
1489 /*
1490 * We really want to stop the transmitter from sending.
1491 */
1492 if (port->type == PORT_16C950) {
1493 up->acr |= UART_ACR_TXDIS;
1494 serial_icr_write(up, UART_ACR, up->acr);
1495 }
1496 serial8250_rpm_put(up);
1497}
1498
Matwey V. Kornilove490c912016-02-01 21:09:21 +03001499static inline void __start_tx(struct uart_port *port)
Peter Hurleyb6830f62015-06-27 09:19:00 -04001500{
1501 struct uart_8250_port *up = up_to_u8250p(port);
1502
Peter Hurleyb6830f62015-06-27 09:19:00 -04001503 if (up->dma && !up->dma->tx_dma(up))
1504 return;
1505
1506 if (!(up->ier & UART_IER_THRI)) {
1507 up->ier |= UART_IER_THRI;
1508 serial_port_out(port, UART_IER, up->ier);
1509
1510 if (up->bugs & UART_BUG_TXEN) {
1511 unsigned char lsr;
1512 lsr = serial_in(up, UART_LSR);
1513 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1514 if (lsr & UART_LSR_THRE)
1515 serial8250_tx_chars(up);
1516 }
1517 }
1518
1519 /*
1520 * Re-enable the transmitter if we disabled it.
1521 */
1522 if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1523 up->acr &= ~UART_ACR_TXDIS;
1524 serial_icr_write(up, UART_ACR, up->acr);
1525 }
1526}
1527
Matwey V. Kornilove490c912016-02-01 21:09:21 +03001528static inline void start_tx_rs485(struct uart_port *port)
1529{
1530 struct uart_8250_port *up = up_to_u8250p(port);
1531 struct uart_8250_em485 *em485 = up->em485;
1532 unsigned char mcr;
1533
1534 if (!(up->port.rs485.flags & SER_RS485_RX_DURING_TX))
1535 serial8250_stop_rx(&up->port);
1536
1537 del_timer(&em485->stop_tx_timer);
1538 em485->active_timer = NULL;
1539
1540 mcr = serial_in(up, UART_MCR);
1541 if (!!(up->port.rs485.flags & SER_RS485_RTS_ON_SEND) !=
1542 !!(mcr & UART_MCR_RTS)) {
1543 if (up->port.rs485.flags & SER_RS485_RTS_ON_SEND)
1544 mcr |= UART_MCR_RTS;
1545 else
1546 mcr &= ~UART_MCR_RTS;
1547 serial_out(up, UART_MCR, mcr);
1548
1549 if (up->port.rs485.delay_rts_before_send > 0) {
1550 em485->active_timer = &em485->start_tx_timer;
1551 mod_timer(&em485->start_tx_timer, jiffies +
1552 up->port.rs485.delay_rts_before_send * HZ / 1000);
1553 return;
1554 }
1555 }
1556
1557 __start_tx(port);
1558}
1559
1560static void serial8250_em485_handle_start_tx(unsigned long arg)
1561{
1562 struct uart_8250_port *p = (struct uart_8250_port *)arg;
1563 struct uart_8250_em485 *em485 = p->em485;
1564 unsigned long flags;
1565
1566 spin_lock_irqsave(&p->port.lock, flags);
1567 if (em485 &&
1568 em485->active_timer == &em485->start_tx_timer) {
1569 __start_tx(&p->port);
1570 em485->active_timer = NULL;
1571 }
1572 spin_unlock_irqrestore(&p->port.lock, flags);
1573}
1574
1575static void serial8250_start_tx(struct uart_port *port)
1576{
1577 struct uart_8250_port *up = up_to_u8250p(port);
1578 struct uart_8250_em485 *em485 = up->em485;
1579
1580 serial8250_rpm_get_tx(up);
1581
1582 if (em485 &&
1583 em485->active_timer == &em485->start_tx_timer)
1584 return;
1585
1586 if (em485)
1587 start_tx_rs485(port);
1588 else
1589 __start_tx(port);
1590}
1591
Peter Hurleyb6830f62015-06-27 09:19:00 -04001592static void serial8250_throttle(struct uart_port *port)
1593{
1594 port->throttle(port);
1595}
1596
1597static void serial8250_unthrottle(struct uart_port *port)
1598{
1599 port->unthrottle(port);
1600}
1601
Peter Hurleyb6830f62015-06-27 09:19:00 -04001602static void serial8250_disable_ms(struct uart_port *port)
1603{
1604 struct uart_8250_port *up =
1605 container_of(port, struct uart_8250_port, port);
1606
1607 /* no MSR capabilities */
1608 if (up->bugs & UART_BUG_NOMSR)
1609 return;
1610
1611 up->ier &= ~UART_IER_MSI;
1612 serial_port_out(port, UART_IER, up->ier);
1613}
1614
1615static void serial8250_enable_ms(struct uart_port *port)
1616{
1617 struct uart_8250_port *up = up_to_u8250p(port);
1618
1619 /* no MSR capabilities */
1620 if (up->bugs & UART_BUG_NOMSR)
1621 return;
1622
1623 up->ier |= UART_IER_MSI;
1624
1625 serial8250_rpm_get(up);
1626 serial_port_out(port, UART_IER, up->ier);
1627 serial8250_rpm_put(up);
1628}
1629
Peter Hurley3f6b3ce2016-01-10 14:39:31 -08001630static void serial8250_read_char(struct uart_8250_port *up, unsigned char lsr)
1631{
1632 struct uart_port *port = &up->port;
1633 unsigned char ch;
1634 char flag = TTY_NORMAL;
1635
1636 if (likely(lsr & UART_LSR_DR))
1637 ch = serial_in(up, UART_RX);
1638 else
1639 /*
1640 * Intel 82571 has a Serial Over Lan device that will
1641 * set UART_LSR_BI without setting UART_LSR_DR when
1642 * it receives a break. To avoid reading from the
1643 * receive buffer without UART_LSR_DR bit set, we
1644 * just force the read character to be 0
1645 */
1646 ch = 0;
1647
1648 port->icount.rx++;
1649
1650 lsr |= up->lsr_saved_flags;
1651 up->lsr_saved_flags = 0;
1652
1653 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1654 if (lsr & UART_LSR_BI) {
1655 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1656 port->icount.brk++;
1657 /*
1658 * We do the SysRQ and SAK checking
1659 * here because otherwise the break
1660 * may get masked by ignore_status_mask
1661 * or read_status_mask.
1662 */
1663 if (uart_handle_break(port))
1664 return;
1665 } else if (lsr & UART_LSR_PE)
1666 port->icount.parity++;
1667 else if (lsr & UART_LSR_FE)
1668 port->icount.frame++;
1669 if (lsr & UART_LSR_OE)
1670 port->icount.overrun++;
1671
1672 /*
1673 * Mask off conditions which should be ignored.
1674 */
1675 lsr &= port->read_status_mask;
1676
1677 if (lsr & UART_LSR_BI) {
1678 DEBUG_INTR("handling break....");
1679 flag = TTY_BREAK;
1680 } else if (lsr & UART_LSR_PE)
1681 flag = TTY_PARITY;
1682 else if (lsr & UART_LSR_FE)
1683 flag = TTY_FRAME;
1684 }
1685 if (uart_handle_sysrq_char(port, ch))
1686 return;
1687
1688 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
1689}
1690
Peter Hurleyb6830f62015-06-27 09:19:00 -04001691/*
1692 * serial8250_rx_chars: processes according to the passed in LSR
1693 * value, and returns the remaining LSR bits not handled
1694 * by this Rx routine.
1695 */
Peter Hurleyd22f8f12016-01-10 14:39:32 -08001696unsigned char serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
Peter Hurleyb6830f62015-06-27 09:19:00 -04001697{
1698 struct uart_port *port = &up->port;
Peter Hurleyb6830f62015-06-27 09:19:00 -04001699 int max_count = 256;
Peter Hurleyb6830f62015-06-27 09:19:00 -04001700
1701 do {
Peter Hurley3f6b3ce2016-01-10 14:39:31 -08001702 serial8250_read_char(up, lsr);
Peter Hurleyd22f8f12016-01-10 14:39:32 -08001703 if (--max_count == 0)
1704 break;
Peter Hurleyb6830f62015-06-27 09:19:00 -04001705 lsr = serial_in(up, UART_LSR);
Peter Hurleyd22f8f12016-01-10 14:39:32 -08001706 } while (lsr & (UART_LSR_DR | UART_LSR_BI));
Peter Hurley6a597a32016-01-10 14:39:33 -08001707
Peter Hurleyb6830f62015-06-27 09:19:00 -04001708 tty_flip_buffer_push(&port->state->port);
Peter Hurleyb6830f62015-06-27 09:19:00 -04001709 return lsr;
1710}
1711EXPORT_SYMBOL_GPL(serial8250_rx_chars);
1712
1713void serial8250_tx_chars(struct uart_8250_port *up)
1714{
1715 struct uart_port *port = &up->port;
1716 struct circ_buf *xmit = &port->state->xmit;
1717 int count;
1718
1719 if (port->x_char) {
1720 serial_out(up, UART_TX, port->x_char);
1721 port->icount.tx++;
1722 port->x_char = 0;
1723 return;
1724 }
1725 if (uart_tx_stopped(port)) {
1726 serial8250_stop_tx(port);
1727 return;
1728 }
1729 if (uart_circ_empty(xmit)) {
1730 __stop_tx(up);
1731 return;
1732 }
1733
1734 count = up->tx_loadsz;
1735 do {
1736 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1737 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1738 port->icount.tx++;
1739 if (uart_circ_empty(xmit))
1740 break;
Peter Hurleycee10c82016-01-10 14:39:36 -08001741 if ((up->capabilities & UART_CAP_HFIFO) &&
1742 (serial_in(up, UART_LSR) & BOTH_EMPTY) != BOTH_EMPTY)
1743 break;
Peter Hurleyb6830f62015-06-27 09:19:00 -04001744 } while (--count > 0);
1745
1746 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1747 uart_write_wakeup(port);
1748
1749 DEBUG_INTR("THRE...");
1750
1751 /*
1752 * With RPM enabled, we have to wait until the FIFO is empty before the
1753 * HW can go idle. So we get here once again with empty FIFO and disable
1754 * the interrupt and RPM in __stop_tx()
1755 */
1756 if (uart_circ_empty(xmit) && !(up->capabilities & UART_CAP_RPM))
1757 __stop_tx(up);
1758}
1759EXPORT_SYMBOL_GPL(serial8250_tx_chars);
1760
1761/* Caller holds uart port lock */
1762unsigned int serial8250_modem_status(struct uart_8250_port *up)
1763{
1764 struct uart_port *port = &up->port;
1765 unsigned int status = serial_in(up, UART_MSR);
1766
1767 status |= up->msr_saved_flags;
1768 up->msr_saved_flags = 0;
1769 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1770 port->state != NULL) {
1771 if (status & UART_MSR_TERI)
1772 port->icount.rng++;
1773 if (status & UART_MSR_DDSR)
1774 port->icount.dsr++;
1775 if (status & UART_MSR_DDCD)
1776 uart_handle_dcd_change(port, status & UART_MSR_DCD);
1777 if (status & UART_MSR_DCTS)
1778 uart_handle_cts_change(port, status & UART_MSR_CTS);
1779
1780 wake_up_interruptible(&port->state->port.delta_msr_wait);
1781 }
1782
1783 return status;
1784}
1785EXPORT_SYMBOL_GPL(serial8250_modem_status);
1786
1787/*
1788 * This handles the interrupt from one port.
1789 */
1790int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
1791{
1792 unsigned char status;
1793 unsigned long flags;
1794 struct uart_8250_port *up = up_to_u8250p(port);
1795 int dma_err = 0;
1796
1797 if (iir & UART_IIR_NO_INT)
1798 return 0;
1799
1800 spin_lock_irqsave(&port->lock, flags);
1801
1802 status = serial_port_in(port, UART_LSR);
1803
1804 DEBUG_INTR("status = %x...", status);
1805
1806 if (status & (UART_LSR_DR | UART_LSR_BI)) {
1807 if (up->dma)
1808 dma_err = up->dma->rx_dma(up, iir);
1809
1810 if (!up->dma || dma_err)
1811 status = serial8250_rx_chars(up, status);
1812 }
1813 serial8250_modem_status(up);
1814 if ((!up->dma || (up->dma && up->dma->tx_err)) &&
1815 (status & UART_LSR_THRE))
1816 serial8250_tx_chars(up);
1817
1818 spin_unlock_irqrestore(&port->lock, flags);
1819 return 1;
1820}
1821EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1822
1823static int serial8250_default_handle_irq(struct uart_port *port)
1824{
1825 struct uart_8250_port *up = up_to_u8250p(port);
1826 unsigned int iir;
1827 int ret;
1828
1829 serial8250_rpm_get(up);
1830
1831 iir = serial_port_in(port, UART_IIR);
1832 ret = serial8250_handle_irq(port, iir);
1833
1834 serial8250_rpm_put(up);
1835 return ret;
1836}
1837
1838/*
1839 * These Exar UARTs have an extra interrupt indicator that could
1840 * fire for a few unimplemented interrupts. One of which is a
1841 * wakeup event when coming out of sleep. Put this here just
1842 * to be on the safe side that these interrupts don't go unhandled.
1843 */
1844static int exar_handle_irq(struct uart_port *port)
1845{
1846 unsigned char int0, int1, int2, int3;
1847 unsigned int iir = serial_port_in(port, UART_IIR);
1848 int ret;
1849
1850 ret = serial8250_handle_irq(port, iir);
1851
1852 if ((port->type == PORT_XR17V35X) ||
1853 (port->type == PORT_XR17D15X)) {
1854 int0 = serial_port_in(port, 0x80);
1855 int1 = serial_port_in(port, 0x81);
1856 int2 = serial_port_in(port, 0x82);
1857 int3 = serial_port_in(port, 0x83);
1858 }
1859
1860 return ret;
1861}
1862
1863static unsigned int serial8250_tx_empty(struct uart_port *port)
1864{
1865 struct uart_8250_port *up = up_to_u8250p(port);
1866 unsigned long flags;
1867 unsigned int lsr;
1868
1869 serial8250_rpm_get(up);
1870
1871 spin_lock_irqsave(&port->lock, flags);
1872 lsr = serial_port_in(port, UART_LSR);
1873 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1874 spin_unlock_irqrestore(&port->lock, flags);
1875
1876 serial8250_rpm_put(up);
1877
1878 return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
1879}
1880
1881static unsigned int serial8250_get_mctrl(struct uart_port *port)
1882{
1883 struct uart_8250_port *up = up_to_u8250p(port);
1884 unsigned int status;
1885 unsigned int ret;
1886
1887 serial8250_rpm_get(up);
1888 status = serial8250_modem_status(up);
1889 serial8250_rpm_put(up);
1890
1891 ret = 0;
1892 if (status & UART_MSR_DCD)
1893 ret |= TIOCM_CAR;
1894 if (status & UART_MSR_RI)
1895 ret |= TIOCM_RNG;
1896 if (status & UART_MSR_DSR)
1897 ret |= TIOCM_DSR;
1898 if (status & UART_MSR_CTS)
1899 ret |= TIOCM_CTS;
1900 return ret;
1901}
1902
1903void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl)
1904{
1905 struct uart_8250_port *up = up_to_u8250p(port);
1906 unsigned char mcr = 0;
1907
1908 if (mctrl & TIOCM_RTS)
1909 mcr |= UART_MCR_RTS;
1910 if (mctrl & TIOCM_DTR)
1911 mcr |= UART_MCR_DTR;
1912 if (mctrl & TIOCM_OUT1)
1913 mcr |= UART_MCR_OUT1;
1914 if (mctrl & TIOCM_OUT2)
1915 mcr |= UART_MCR_OUT2;
1916 if (mctrl & TIOCM_LOOP)
1917 mcr |= UART_MCR_LOOP;
1918
1919 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1920
1921 serial_port_out(port, UART_MCR, mcr);
1922}
1923EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);
1924
1925static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1926{
1927 if (port->set_mctrl)
1928 port->set_mctrl(port, mctrl);
1929 else
1930 serial8250_do_set_mctrl(port, mctrl);
1931}
1932
1933static void serial8250_break_ctl(struct uart_port *port, int break_state)
1934{
1935 struct uart_8250_port *up = up_to_u8250p(port);
1936 unsigned long flags;
1937
1938 serial8250_rpm_get(up);
1939 spin_lock_irqsave(&port->lock, flags);
1940 if (break_state == -1)
1941 up->lcr |= UART_LCR_SBC;
1942 else
1943 up->lcr &= ~UART_LCR_SBC;
1944 serial_port_out(port, UART_LCR, up->lcr);
1945 spin_unlock_irqrestore(&port->lock, flags);
1946 serial8250_rpm_put(up);
1947}
1948
1949/*
1950 * Wait for transmitter & holding register to empty
1951 */
1952static void wait_for_xmitr(struct uart_8250_port *up, int bits)
1953{
1954 unsigned int status, tmout = 10000;
1955
1956 /* Wait up to 10ms for the character(s) to be sent. */
1957 for (;;) {
1958 status = serial_in(up, UART_LSR);
1959
1960 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
1961
1962 if ((status & bits) == bits)
1963 break;
1964 if (--tmout == 0)
1965 break;
1966 udelay(1);
1967 }
1968
1969 /* Wait up to 1s for flow control if necessary */
1970 if (up->port.flags & UPF_CONS_FLOW) {
1971 unsigned int tmout;
1972 for (tmout = 1000000; tmout; tmout--) {
1973 unsigned int msr = serial_in(up, UART_MSR);
1974 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1975 if (msr & UART_MSR_CTS)
1976 break;
1977 udelay(1);
1978 touch_nmi_watchdog();
1979 }
1980 }
1981}
1982
1983#ifdef CONFIG_CONSOLE_POLL
1984/*
1985 * Console polling routines for writing and reading from the uart while
1986 * in an interrupt or debug context.
1987 */
1988
1989static int serial8250_get_poll_char(struct uart_port *port)
1990{
1991 struct uart_8250_port *up = up_to_u8250p(port);
1992 unsigned char lsr;
1993 int status;
1994
1995 serial8250_rpm_get(up);
1996
1997 lsr = serial_port_in(port, UART_LSR);
1998
1999 if (!(lsr & UART_LSR_DR)) {
2000 status = NO_POLL_CHAR;
2001 goto out;
2002 }
2003
2004 status = serial_port_in(port, UART_RX);
2005out:
2006 serial8250_rpm_put(up);
2007 return status;
2008}
2009
2010
2011static void serial8250_put_poll_char(struct uart_port *port,
2012 unsigned char c)
2013{
2014 unsigned int ier;
2015 struct uart_8250_port *up = up_to_u8250p(port);
2016
2017 serial8250_rpm_get(up);
2018 /*
2019 * First save the IER then disable the interrupts
2020 */
2021 ier = serial_port_in(port, UART_IER);
2022 if (up->capabilities & UART_CAP_UUE)
2023 serial_port_out(port, UART_IER, UART_IER_UUE);
2024 else
2025 serial_port_out(port, UART_IER, 0);
2026
2027 wait_for_xmitr(up, BOTH_EMPTY);
2028 /*
2029 * Send the character out.
2030 */
2031 serial_port_out(port, UART_TX, c);
2032
2033 /*
2034 * Finally, wait for transmitter to become empty
2035 * and restore the IER
2036 */
2037 wait_for_xmitr(up, BOTH_EMPTY);
2038 serial_port_out(port, UART_IER, ier);
2039 serial8250_rpm_put(up);
2040}
2041
2042#endif /* CONFIG_CONSOLE_POLL */
2043
2044int serial8250_do_startup(struct uart_port *port)
2045{
2046 struct uart_8250_port *up = up_to_u8250p(port);
2047 unsigned long flags;
2048 unsigned char lsr, iir;
2049 int retval;
2050
Peter Hurleyb6830f62015-06-27 09:19:00 -04002051 if (!port->fifosize)
2052 port->fifosize = uart_config[port->type].fifo_size;
2053 if (!up->tx_loadsz)
2054 up->tx_loadsz = uart_config[port->type].tx_loadsz;
2055 if (!up->capabilities)
2056 up->capabilities = uart_config[port->type].flags;
2057 up->mcr = 0;
2058
2059 if (port->iotype != up->cur_iotype)
2060 set_io_from_upio(port);
2061
2062 serial8250_rpm_get(up);
2063 if (port->type == PORT_16C950) {
2064 /* Wake up and initialize UART */
2065 up->acr = 0;
2066 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2067 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2068 serial_port_out(port, UART_IER, 0);
2069 serial_port_out(port, UART_LCR, 0);
2070 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2071 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2072 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2073 serial_port_out(port, UART_LCR, 0);
2074 }
2075
2076#ifdef CONFIG_SERIAL_8250_RSA
2077 /*
2078 * If this is an RSA port, see if we can kick it up to the
2079 * higher speed clock.
2080 */
2081 enable_rsa(up);
2082#endif
Joerg Roedelda891642015-07-16 10:29:13 +02002083
2084 if (port->type == PORT_XR17V35X) {
2085 /*
2086 * First enable access to IER [7:5], ISR [5:4], FCR [5:4],
2087 * MCR [7:5] and MSR [7:0]
2088 */
2089 serial_port_out(port, UART_XR_EFR, UART_EFR_ECB);
2090
2091 /*
2092 * Make sure all interrups are masked until initialization is
2093 * complete and the FIFOs are cleared
2094 */
2095 serial_port_out(port, UART_IER, 0);
2096 }
2097
Peter Hurleyb6830f62015-06-27 09:19:00 -04002098 /*
2099 * Clear the FIFO buffers and disable them.
2100 * (they will be reenabled in set_termios())
2101 */
2102 serial8250_clear_fifos(up);
2103
2104 /*
2105 * Clear the interrupt registers.
2106 */
2107 serial_port_in(port, UART_LSR);
2108 serial_port_in(port, UART_RX);
2109 serial_port_in(port, UART_IIR);
2110 serial_port_in(port, UART_MSR);
2111
2112 /*
2113 * At this point, there's no way the LSR could still be 0xff;
2114 * if it is, then bail out, because there's likely no UART
2115 * here.
2116 */
2117 if (!(port->flags & UPF_BUGGY_UART) &&
2118 (serial_port_in(port, UART_LSR) == 0xff)) {
2119 printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2120 serial_index(port));
2121 retval = -ENODEV;
2122 goto out;
2123 }
2124
2125 /*
2126 * For a XR16C850, we need to set the trigger levels
2127 */
2128 if (port->type == PORT_16850) {
2129 unsigned char fctr;
2130
2131 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
2132
2133 fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2134 serial_port_out(port, UART_FCTR,
2135 fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2136 serial_port_out(port, UART_TRG, UART_TRG_96);
2137 serial_port_out(port, UART_FCTR,
2138 fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2139 serial_port_out(port, UART_TRG, UART_TRG_96);
2140
2141 serial_port_out(port, UART_LCR, 0);
2142 }
2143
2144 if (port->irq) {
2145 unsigned char iir1;
2146 /*
2147 * Test for UARTs that do not reassert THRE when the
2148 * transmitter is idle and the interrupt has already
2149 * been cleared. Real 16550s should always reassert
2150 * this interrupt whenever the transmitter is idle and
2151 * the interrupt is enabled. Delays are necessary to
2152 * allow register changes to become visible.
2153 */
2154 spin_lock_irqsave(&port->lock, flags);
2155 if (up->port.irqflags & IRQF_SHARED)
2156 disable_irq_nosync(port->irq);
2157
2158 wait_for_xmitr(up, UART_LSR_THRE);
2159 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2160 udelay(1); /* allow THRE to set */
2161 iir1 = serial_port_in(port, UART_IIR);
2162 serial_port_out(port, UART_IER, 0);
2163 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2164 udelay(1); /* allow a working UART time to re-assert THRE */
2165 iir = serial_port_in(port, UART_IIR);
2166 serial_port_out(port, UART_IER, 0);
2167
2168 if (port->irqflags & IRQF_SHARED)
2169 enable_irq(port->irq);
2170 spin_unlock_irqrestore(&port->lock, flags);
2171
2172 /*
2173 * If the interrupt is not reasserted, or we otherwise
2174 * don't trust the iir, setup a timer to kick the UART
2175 * on a regular basis.
2176 */
2177 if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
2178 up->port.flags & UPF_BUG_THRE) {
2179 up->bugs |= UART_BUG_THRE;
2180 }
2181 }
2182
2183 retval = up->ops->setup_irq(up);
2184 if (retval)
2185 goto out;
2186
2187 /*
2188 * Now, initialize the UART
2189 */
2190 serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
2191
2192 spin_lock_irqsave(&port->lock, flags);
2193 if (up->port.flags & UPF_FOURPORT) {
2194 if (!up->port.irq)
2195 up->port.mctrl |= TIOCM_OUT1;
2196 } else
2197 /*
2198 * Most PC uarts need OUT2 raised to enable interrupts.
2199 */
2200 if (port->irq)
2201 up->port.mctrl |= TIOCM_OUT2;
2202
2203 serial8250_set_mctrl(port, port->mctrl);
2204
2205 /* Serial over Lan (SoL) hack:
2206 Intel 8257x Gigabit ethernet chips have a
2207 16550 emulation, to be used for Serial Over Lan.
2208 Those chips take a longer time than a normal
2209 serial device to signalize that a transmission
2210 data was queued. Due to that, the above test generally
2211 fails. One solution would be to delay the reading of
2212 iir. However, this is not reliable, since the timeout
2213 is variable. So, let's just don't test if we receive
2214 TX irq. This way, we'll never enable UART_BUG_TXEN.
2215 */
2216 if (up->port.flags & UPF_NO_TXEN_TEST)
2217 goto dont_test_tx_en;
2218
2219 /*
2220 * Do a quick test to see if we receive an
2221 * interrupt when we enable the TX irq.
2222 */
2223 serial_port_out(port, UART_IER, UART_IER_THRI);
2224 lsr = serial_port_in(port, UART_LSR);
2225 iir = serial_port_in(port, UART_IIR);
2226 serial_port_out(port, UART_IER, 0);
2227
2228 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2229 if (!(up->bugs & UART_BUG_TXEN)) {
2230 up->bugs |= UART_BUG_TXEN;
2231 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2232 serial_index(port));
2233 }
2234 } else {
2235 up->bugs &= ~UART_BUG_TXEN;
2236 }
2237
2238dont_test_tx_en:
2239 spin_unlock_irqrestore(&port->lock, flags);
2240
2241 /*
2242 * Clear the interrupt registers again for luck, and clear the
2243 * saved flags to avoid getting false values from polling
2244 * routines or the previous session.
2245 */
2246 serial_port_in(port, UART_LSR);
2247 serial_port_in(port, UART_RX);
2248 serial_port_in(port, UART_IIR);
2249 serial_port_in(port, UART_MSR);
2250 up->lsr_saved_flags = 0;
2251 up->msr_saved_flags = 0;
2252
2253 /*
2254 * Request DMA channels for both RX and TX.
2255 */
2256 if (up->dma) {
2257 retval = serial8250_request_dma(up);
2258 if (retval) {
2259 pr_warn_ratelimited("ttyS%d - failed to request DMA\n",
2260 serial_index(port));
2261 up->dma = NULL;
2262 }
2263 }
2264
2265 /*
Peter Hurleyee3ad902015-07-12 21:11:31 -04002266 * Set the IER shadow for rx interrupts but defer actual interrupt
2267 * enable until after the FIFOs are enabled; otherwise, an already-
2268 * active sender can swamp the interrupt handler with "too much work".
Peter Hurleyb6830f62015-06-27 09:19:00 -04002269 */
2270 up->ier = UART_IER_RLSI | UART_IER_RDI;
Peter Hurleyb6830f62015-06-27 09:19:00 -04002271
2272 if (port->flags & UPF_FOURPORT) {
2273 unsigned int icp;
2274 /*
2275 * Enable interrupts on the AST Fourport board
2276 */
2277 icp = (port->iobase & 0xfe0) | 0x01f;
2278 outb_p(0x80, icp);
2279 inb_p(icp);
2280 }
2281 retval = 0;
2282out:
2283 serial8250_rpm_put(up);
2284 return retval;
2285}
2286EXPORT_SYMBOL_GPL(serial8250_do_startup);
2287
2288static int serial8250_startup(struct uart_port *port)
2289{
2290 if (port->startup)
2291 return port->startup(port);
2292 return serial8250_do_startup(port);
2293}
2294
2295void serial8250_do_shutdown(struct uart_port *port)
2296{
2297 struct uart_8250_port *up = up_to_u8250p(port);
2298 unsigned long flags;
2299
2300 serial8250_rpm_get(up);
2301 /*
2302 * Disable interrupts from this port
2303 */
Peter Hurley611e0d82016-01-10 14:39:35 -08002304 spin_lock_irqsave(&port->lock, flags);
Peter Hurleyb6830f62015-06-27 09:19:00 -04002305 up->ier = 0;
2306 serial_port_out(port, UART_IER, 0);
Peter Hurley611e0d82016-01-10 14:39:35 -08002307 spin_unlock_irqrestore(&port->lock, flags);
2308
2309 synchronize_irq(port->irq);
Peter Hurleyb6830f62015-06-27 09:19:00 -04002310
2311 if (up->dma)
2312 serial8250_release_dma(up);
2313
2314 spin_lock_irqsave(&port->lock, flags);
2315 if (port->flags & UPF_FOURPORT) {
2316 /* reset interrupts on the AST Fourport board */
2317 inb((port->iobase & 0xfe0) | 0x1f);
2318 port->mctrl |= TIOCM_OUT1;
2319 } else
2320 port->mctrl &= ~TIOCM_OUT2;
2321
2322 serial8250_set_mctrl(port, port->mctrl);
2323 spin_unlock_irqrestore(&port->lock, flags);
2324
2325 /*
2326 * Disable break condition and FIFOs
2327 */
2328 serial_port_out(port, UART_LCR,
2329 serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
2330 serial8250_clear_fifos(up);
2331
2332#ifdef CONFIG_SERIAL_8250_RSA
2333 /*
2334 * Reset the RSA board back to 115kbps compat mode.
2335 */
2336 disable_rsa(up);
2337#endif
2338
2339 /*
2340 * Read data port to reset things, and then unlink from
2341 * the IRQ chain.
2342 */
2343 serial_port_in(port, UART_RX);
2344 serial8250_rpm_put(up);
2345
2346 up->ops->release_irq(up);
2347}
2348EXPORT_SYMBOL_GPL(serial8250_do_shutdown);
2349
2350static void serial8250_shutdown(struct uart_port *port)
2351{
2352 if (port->shutdown)
2353 port->shutdown(port);
2354 else
2355 serial8250_do_shutdown(port);
2356}
2357
2358/*
2359 * XR17V35x UARTs have an extra fractional divisor register (DLD)
2360 * Calculate divisor with extra 4-bit fractional portion
2361 */
2362static unsigned int xr17v35x_get_divisor(struct uart_8250_port *up,
2363 unsigned int baud,
2364 unsigned int *frac)
2365{
2366 struct uart_port *port = &up->port;
2367 unsigned int quot_16;
2368
2369 quot_16 = DIV_ROUND_CLOSEST(port->uartclk, baud);
2370 *frac = quot_16 & 0x0f;
2371
2372 return quot_16 >> 4;
2373}
2374
2375static unsigned int serial8250_get_divisor(struct uart_8250_port *up,
2376 unsigned int baud,
2377 unsigned int *frac)
2378{
2379 struct uart_port *port = &up->port;
2380 unsigned int quot;
2381
2382 /*
2383 * Handle magic divisors for baud rates above baud_base on
2384 * SMSC SuperIO chips.
2385 *
2386 */
2387 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2388 baud == (port->uartclk/4))
2389 quot = 0x8001;
2390 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2391 baud == (port->uartclk/8))
2392 quot = 0x8002;
2393 else if (up->port.type == PORT_XR17V35X)
2394 quot = xr17v35x_get_divisor(up, baud, frac);
2395 else
2396 quot = uart_get_divisor(port, baud);
2397
2398 /*
2399 * Oxford Semi 952 rev B workaround
2400 */
2401 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2402 quot++;
2403
2404 return quot;
2405}
2406
2407static unsigned char serial8250_compute_lcr(struct uart_8250_port *up,
2408 tcflag_t c_cflag)
2409{
2410 unsigned char cval;
2411
2412 switch (c_cflag & CSIZE) {
2413 case CS5:
2414 cval = UART_LCR_WLEN5;
2415 break;
2416 case CS6:
2417 cval = UART_LCR_WLEN6;
2418 break;
2419 case CS7:
2420 cval = UART_LCR_WLEN7;
2421 break;
2422 default:
2423 case CS8:
2424 cval = UART_LCR_WLEN8;
2425 break;
2426 }
2427
2428 if (c_cflag & CSTOPB)
2429 cval |= UART_LCR_STOP;
2430 if (c_cflag & PARENB) {
2431 cval |= UART_LCR_PARITY;
2432 if (up->bugs & UART_BUG_PARITY)
2433 up->fifo_bug = true;
2434 }
2435 if (!(c_cflag & PARODD))
2436 cval |= UART_LCR_EPAR;
2437#ifdef CMSPAR
2438 if (c_cflag & CMSPAR)
2439 cval |= UART_LCR_SPAR;
2440#endif
2441
2442 return cval;
2443}
2444
2445static void serial8250_set_divisor(struct uart_port *port, unsigned int baud,
2446 unsigned int quot, unsigned int quot_frac)
2447{
2448 struct uart_8250_port *up = up_to_u8250p(port);
2449
2450 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2451 if (is_omap1510_8250(up)) {
2452 if (baud == 115200) {
2453 quot = 1;
2454 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
2455 } else
2456 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
2457 }
2458
2459 /*
2460 * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
2461 * otherwise just set DLAB
2462 */
2463 if (up->capabilities & UART_NATSEMI)
2464 serial_port_out(port, UART_LCR, 0xe0);
2465 else
2466 serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
2467
2468 serial_dl_write(up, quot);
2469
2470 /* XR17V35x UARTs have an extra fractional divisor register (DLD) */
2471 if (up->port.type == PORT_XR17V35X)
2472 serial_port_out(port, 0x2, quot_frac);
2473}
2474
James Hogan4f56f3f2015-09-25 15:36:10 -04002475static unsigned int
2476serial8250_get_baud_rate(struct uart_port *port, struct ktermios *termios,
2477 struct ktermios *old)
2478{
2479 unsigned int tolerance = port->uartclk / 100;
2480
2481 /*
2482 * Ask the core to calculate the divisor for us.
2483 * Allow 1% tolerance at the upper limit so uart clks marginally
2484 * slower than nominal still match standard baud rates without
2485 * causing transmission errors.
2486 */
2487 return uart_get_baud_rate(port, termios, old,
2488 port->uartclk / 16 / 0xffff,
2489 (port->uartclk + tolerance) / 16);
2490}
2491
Peter Hurleyb6830f62015-06-27 09:19:00 -04002492void
2493serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2494 struct ktermios *old)
2495{
2496 struct uart_8250_port *up = up_to_u8250p(port);
2497 unsigned char cval;
2498 unsigned long flags;
2499 unsigned int baud, quot, frac = 0;
2500
2501 cval = serial8250_compute_lcr(up, termios->c_cflag);
2502
James Hogan4f56f3f2015-09-25 15:36:10 -04002503 baud = serial8250_get_baud_rate(port, termios, old);
Peter Hurleyb6830f62015-06-27 09:19:00 -04002504 quot = serial8250_get_divisor(up, baud, &frac);
2505
2506 /*
2507 * Ok, we're now changing the port state. Do it with
2508 * interrupts disabled.
2509 */
2510 serial8250_rpm_get(up);
2511 spin_lock_irqsave(&port->lock, flags);
2512
2513 up->lcr = cval; /* Save computed LCR */
2514
2515 if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
2516 /* NOTE: If fifo_bug is not set, a user can set RX_trigger. */
2517 if ((baud < 2400 && !up->dma) || up->fifo_bug) {
2518 up->fcr &= ~UART_FCR_TRIGGER_MASK;
2519 up->fcr |= UART_FCR_TRIGGER_1;
2520 }
2521 }
2522
2523 /*
2524 * MCR-based auto flow control. When AFE is enabled, RTS will be
2525 * deasserted when the receive FIFO contains more characters than
2526 * the trigger, or the MCR RTS bit is cleared. In the case where
2527 * the remote UART is not using CTS auto flow control, we must
2528 * have sufficient FIFO entries for the latency of the remote
2529 * UART to respond. IOW, at least 32 bytes of FIFO.
2530 */
2531 if (up->capabilities & UART_CAP_AFE && port->fifosize >= 32) {
2532 up->mcr &= ~UART_MCR_AFE;
2533 if (termios->c_cflag & CRTSCTS)
2534 up->mcr |= UART_MCR_AFE;
2535 }
2536
2537 /*
2538 * Update the per-port timeout.
2539 */
2540 uart_update_timeout(port, termios->c_cflag, baud);
2541
2542 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2543 if (termios->c_iflag & INPCK)
2544 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2545 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2546 port->read_status_mask |= UART_LSR_BI;
2547
2548 /*
2549 * Characteres to ignore
2550 */
2551 port->ignore_status_mask = 0;
2552 if (termios->c_iflag & IGNPAR)
2553 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2554 if (termios->c_iflag & IGNBRK) {
2555 port->ignore_status_mask |= UART_LSR_BI;
2556 /*
2557 * If we're ignoring parity and break indicators,
2558 * ignore overruns too (for real raw support).
2559 */
2560 if (termios->c_iflag & IGNPAR)
2561 port->ignore_status_mask |= UART_LSR_OE;
2562 }
2563
2564 /*
2565 * ignore all characters if CREAD is not set
2566 */
2567 if ((termios->c_cflag & CREAD) == 0)
2568 port->ignore_status_mask |= UART_LSR_DR;
2569
2570 /*
2571 * CTS flow control flag and modem status interrupts
2572 */
2573 up->ier &= ~UART_IER_MSI;
2574 if (!(up->bugs & UART_BUG_NOMSR) &&
2575 UART_ENABLE_MS(&up->port, termios->c_cflag))
2576 up->ier |= UART_IER_MSI;
2577 if (up->capabilities & UART_CAP_UUE)
2578 up->ier |= UART_IER_UUE;
2579 if (up->capabilities & UART_CAP_RTOIE)
2580 up->ier |= UART_IER_RTOIE;
2581
2582 serial_port_out(port, UART_IER, up->ier);
2583
2584 if (up->capabilities & UART_CAP_EFR) {
2585 unsigned char efr = 0;
2586 /*
2587 * TI16C752/Startech hardware flow control. FIXME:
2588 * - TI16C752 requires control thresholds to be set.
2589 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2590 */
2591 if (termios->c_cflag & CRTSCTS)
2592 efr |= UART_EFR_CTS;
2593
2594 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2595 if (port->flags & UPF_EXAR_EFR)
2596 serial_port_out(port, UART_XR_EFR, efr);
2597 else
2598 serial_port_out(port, UART_EFR, efr);
2599 }
2600
2601 serial8250_set_divisor(port, baud, quot, frac);
2602
2603 /*
2604 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2605 * is written without DLAB set, this mode will be disabled.
2606 */
2607 if (port->type == PORT_16750)
2608 serial_port_out(port, UART_FCR, up->fcr);
2609
2610 serial_port_out(port, UART_LCR, up->lcr); /* reset DLAB */
2611 if (port->type != PORT_16750) {
2612 /* emulated UARTs (Lucent Venus 167x) need two steps */
2613 if (up->fcr & UART_FCR_ENABLE_FIFO)
2614 serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
2615 serial_port_out(port, UART_FCR, up->fcr); /* set fcr */
2616 }
2617 serial8250_set_mctrl(port, port->mctrl);
2618 spin_unlock_irqrestore(&port->lock, flags);
2619 serial8250_rpm_put(up);
2620
2621 /* Don't rewrite B0 */
2622 if (tty_termios_baud_rate(termios))
2623 tty_termios_encode_baud_rate(termios, baud, baud);
2624}
2625EXPORT_SYMBOL(serial8250_do_set_termios);
2626
2627static void
2628serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2629 struct ktermios *old)
2630{
2631 if (port->set_termios)
2632 port->set_termios(port, termios, old);
2633 else
2634 serial8250_do_set_termios(port, termios, old);
2635}
2636
2637static void
2638serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios)
2639{
2640 if (termios->c_line == N_PPS) {
2641 port->flags |= UPF_HARDPPS_CD;
2642 spin_lock_irq(&port->lock);
2643 serial8250_enable_ms(port);
2644 spin_unlock_irq(&port->lock);
2645 } else {
2646 port->flags &= ~UPF_HARDPPS_CD;
2647 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2648 spin_lock_irq(&port->lock);
2649 serial8250_disable_ms(port);
2650 spin_unlock_irq(&port->lock);
2651 }
2652 }
2653}
2654
2655
2656void serial8250_do_pm(struct uart_port *port, unsigned int state,
2657 unsigned int oldstate)
2658{
2659 struct uart_8250_port *p = up_to_u8250p(port);
2660
2661 serial8250_set_sleep(p, state != 0);
2662}
2663EXPORT_SYMBOL(serial8250_do_pm);
2664
2665static void
2666serial8250_pm(struct uart_port *port, unsigned int state,
2667 unsigned int oldstate)
2668{
2669 if (port->pm)
2670 port->pm(port, state, oldstate);
2671 else
2672 serial8250_do_pm(port, state, oldstate);
2673}
2674
2675static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2676{
2677 if (pt->port.mapsize)
2678 return pt->port.mapsize;
2679 if (pt->port.iotype == UPIO_AU) {
2680 if (pt->port.type == PORT_RT2880)
2681 return 0x100;
2682 return 0x1000;
2683 }
2684 if (is_omap1_8250(pt))
2685 return 0x16 << pt->port.regshift;
2686
2687 return 8 << pt->port.regshift;
2688}
2689
2690/*
2691 * Resource handling.
2692 */
2693static int serial8250_request_std_resource(struct uart_8250_port *up)
2694{
2695 unsigned int size = serial8250_port_size(up);
2696 struct uart_port *port = &up->port;
2697 int ret = 0;
2698
2699 switch (port->iotype) {
2700 case UPIO_AU:
2701 case UPIO_TSI:
2702 case UPIO_MEM32:
2703 case UPIO_MEM32BE:
Masahiro Yamadabd94c402015-10-28 12:46:05 +09002704 case UPIO_MEM16:
Peter Hurleyb6830f62015-06-27 09:19:00 -04002705 case UPIO_MEM:
2706 if (!port->mapbase)
2707 break;
2708
2709 if (!request_mem_region(port->mapbase, size, "serial")) {
2710 ret = -EBUSY;
2711 break;
2712 }
2713
2714 if (port->flags & UPF_IOREMAP) {
2715 port->membase = ioremap_nocache(port->mapbase, size);
2716 if (!port->membase) {
2717 release_mem_region(port->mapbase, size);
2718 ret = -ENOMEM;
2719 }
2720 }
2721 break;
2722
2723 case UPIO_HUB6:
2724 case UPIO_PORT:
2725 if (!request_region(port->iobase, size, "serial"))
2726 ret = -EBUSY;
2727 break;
2728 }
2729 return ret;
2730}
2731
2732static void serial8250_release_std_resource(struct uart_8250_port *up)
2733{
2734 unsigned int size = serial8250_port_size(up);
2735 struct uart_port *port = &up->port;
2736
2737 switch (port->iotype) {
2738 case UPIO_AU:
2739 case UPIO_TSI:
2740 case UPIO_MEM32:
2741 case UPIO_MEM32BE:
Masahiro Yamadabd94c402015-10-28 12:46:05 +09002742 case UPIO_MEM16:
Peter Hurleyb6830f62015-06-27 09:19:00 -04002743 case UPIO_MEM:
2744 if (!port->mapbase)
2745 break;
2746
2747 if (port->flags & UPF_IOREMAP) {
2748 iounmap(port->membase);
2749 port->membase = NULL;
2750 }
2751
2752 release_mem_region(port->mapbase, size);
2753 break;
2754
2755 case UPIO_HUB6:
2756 case UPIO_PORT:
2757 release_region(port->iobase, size);
2758 break;
2759 }
2760}
2761
2762static void serial8250_release_port(struct uart_port *port)
2763{
2764 struct uart_8250_port *up = up_to_u8250p(port);
2765
2766 serial8250_release_std_resource(up);
2767}
2768
2769static int serial8250_request_port(struct uart_port *port)
2770{
2771 struct uart_8250_port *up = up_to_u8250p(port);
Peter Hurleyb6830f62015-06-27 09:19:00 -04002772
Maciej S. Szmigieroe4fda3a2015-09-27 16:25:56 +02002773 return serial8250_request_std_resource(up);
Peter Hurleyb6830f62015-06-27 09:19:00 -04002774}
2775
2776static int fcr_get_rxtrig_bytes(struct uart_8250_port *up)
2777{
2778 const struct serial8250_config *conf_type = &uart_config[up->port.type];
2779 unsigned char bytes;
2780
2781 bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)];
2782
2783 return bytes ? bytes : -EOPNOTSUPP;
2784}
2785
2786static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes)
2787{
2788 const struct serial8250_config *conf_type = &uart_config[up->port.type];
2789 int i;
2790
2791 if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)])
2792 return -EOPNOTSUPP;
2793
2794 for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) {
2795 if (bytes < conf_type->rxtrig_bytes[i])
2796 /* Use the nearest lower value */
2797 return (--i) << UART_FCR_R_TRIG_SHIFT;
2798 }
2799
2800 return UART_FCR_R_TRIG_11;
2801}
2802
2803static int do_get_rxtrig(struct tty_port *port)
2804{
2805 struct uart_state *state = container_of(port, struct uart_state, port);
2806 struct uart_port *uport = state->uart_port;
2807 struct uart_8250_port *up =
2808 container_of(uport, struct uart_8250_port, port);
2809
2810 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
2811 return -EINVAL;
2812
2813 return fcr_get_rxtrig_bytes(up);
2814}
2815
2816static int do_serial8250_get_rxtrig(struct tty_port *port)
2817{
2818 int rxtrig_bytes;
2819
2820 mutex_lock(&port->mutex);
2821 rxtrig_bytes = do_get_rxtrig(port);
2822 mutex_unlock(&port->mutex);
2823
2824 return rxtrig_bytes;
2825}
2826
2827static ssize_t serial8250_get_attr_rx_trig_bytes(struct device *dev,
2828 struct device_attribute *attr, char *buf)
2829{
2830 struct tty_port *port = dev_get_drvdata(dev);
2831 int rxtrig_bytes;
2832
2833 rxtrig_bytes = do_serial8250_get_rxtrig(port);
2834 if (rxtrig_bytes < 0)
2835 return rxtrig_bytes;
2836
2837 return snprintf(buf, PAGE_SIZE, "%d\n", rxtrig_bytes);
2838}
2839
2840static int do_set_rxtrig(struct tty_port *port, unsigned char bytes)
2841{
2842 struct uart_state *state = container_of(port, struct uart_state, port);
2843 struct uart_port *uport = state->uart_port;
2844 struct uart_8250_port *up =
2845 container_of(uport, struct uart_8250_port, port);
2846 int rxtrig;
2847
2848 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1 ||
2849 up->fifo_bug)
2850 return -EINVAL;
2851
2852 rxtrig = bytes_to_fcr_rxtrig(up, bytes);
2853 if (rxtrig < 0)
2854 return rxtrig;
2855
2856 serial8250_clear_fifos(up);
2857 up->fcr &= ~UART_FCR_TRIGGER_MASK;
2858 up->fcr |= (unsigned char)rxtrig;
2859 serial_out(up, UART_FCR, up->fcr);
2860 return 0;
2861}
2862
2863static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes)
2864{
2865 int ret;
2866
2867 mutex_lock(&port->mutex);
2868 ret = do_set_rxtrig(port, bytes);
2869 mutex_unlock(&port->mutex);
2870
2871 return ret;
2872}
2873
2874static ssize_t serial8250_set_attr_rx_trig_bytes(struct device *dev,
2875 struct device_attribute *attr, const char *buf, size_t count)
2876{
2877 struct tty_port *port = dev_get_drvdata(dev);
2878 unsigned char bytes;
2879 int ret;
2880
2881 if (!count)
2882 return -EINVAL;
2883
2884 ret = kstrtou8(buf, 10, &bytes);
2885 if (ret < 0)
2886 return ret;
2887
2888 ret = do_serial8250_set_rxtrig(port, bytes);
2889 if (ret < 0)
2890 return ret;
2891
2892 return count;
2893}
2894
2895static DEVICE_ATTR(rx_trig_bytes, S_IRUSR | S_IWUSR | S_IRGRP,
2896 serial8250_get_attr_rx_trig_bytes,
2897 serial8250_set_attr_rx_trig_bytes);
2898
2899static struct attribute *serial8250_dev_attrs[] = {
2900 &dev_attr_rx_trig_bytes.attr,
2901 NULL,
2902 };
2903
2904static struct attribute_group serial8250_dev_attr_group = {
2905 .attrs = serial8250_dev_attrs,
2906 };
2907
2908static void register_dev_spec_attr_grp(struct uart_8250_port *up)
2909{
2910 const struct serial8250_config *conf_type = &uart_config[up->port.type];
2911
2912 if (conf_type->rxtrig_bytes[0])
2913 up->port.attr_group = &serial8250_dev_attr_group;
2914}
2915
2916static void serial8250_config_port(struct uart_port *port, int flags)
2917{
2918 struct uart_8250_port *up = up_to_u8250p(port);
2919 int ret;
2920
Peter Hurleyb6830f62015-06-27 09:19:00 -04002921 /*
2922 * Find the region that we can probe for. This in turn
2923 * tells us whether we can probe for the type of port.
2924 */
2925 ret = serial8250_request_std_resource(up);
2926 if (ret < 0)
2927 return;
2928
2929 if (port->iotype != up->cur_iotype)
2930 set_io_from_upio(port);
2931
2932 if (flags & UART_CONFIG_TYPE)
2933 autoconfig(up);
2934
2935 /* if access method is AU, it is a 16550 with a quirk */
2936 if (port->type == PORT_16550A && port->iotype == UPIO_AU)
2937 up->bugs |= UART_BUG_NOMSR;
2938
2939 /* HW bugs may trigger IRQ while IIR == NO_INT */
2940 if (port->type == PORT_TEGRA)
2941 up->bugs |= UART_BUG_NOMSR;
2942
2943 if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2944 autoconfig_irq(up);
2945
2946 if (port->type == PORT_UNKNOWN)
2947 serial8250_release_std_resource(up);
2948
2949 /* Fixme: probably not the best place for this */
2950 if ((port->type == PORT_XR17V35X) ||
2951 (port->type == PORT_XR17D15X))
2952 port->handle_irq = exar_handle_irq;
2953
2954 register_dev_spec_attr_grp(up);
2955 up->fcr = uart_config[up->port.type].fcr;
2956}
2957
2958static int
2959serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2960{
2961 if (ser->irq >= nr_irqs || ser->irq < 0 ||
2962 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2963 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2964 ser->type == PORT_STARTECH)
2965 return -EINVAL;
2966 return 0;
2967}
2968
Peter Hurleycee10c82016-01-10 14:39:36 -08002969static const char *serial8250_type(struct uart_port *port)
Peter Hurleyb6830f62015-06-27 09:19:00 -04002970{
2971 int type = port->type;
2972
2973 if (type >= ARRAY_SIZE(uart_config))
2974 type = 0;
2975 return uart_config[type].name;
2976}
2977
2978static const struct uart_ops serial8250_pops = {
2979 .tx_empty = serial8250_tx_empty,
2980 .set_mctrl = serial8250_set_mctrl,
2981 .get_mctrl = serial8250_get_mctrl,
2982 .stop_tx = serial8250_stop_tx,
2983 .start_tx = serial8250_start_tx,
2984 .throttle = serial8250_throttle,
2985 .unthrottle = serial8250_unthrottle,
2986 .stop_rx = serial8250_stop_rx,
2987 .enable_ms = serial8250_enable_ms,
2988 .break_ctl = serial8250_break_ctl,
2989 .startup = serial8250_startup,
2990 .shutdown = serial8250_shutdown,
2991 .set_termios = serial8250_set_termios,
2992 .set_ldisc = serial8250_set_ldisc,
2993 .pm = serial8250_pm,
2994 .type = serial8250_type,
2995 .release_port = serial8250_release_port,
2996 .request_port = serial8250_request_port,
2997 .config_port = serial8250_config_port,
2998 .verify_port = serial8250_verify_port,
2999#ifdef CONFIG_CONSOLE_POLL
3000 .poll_get_char = serial8250_get_poll_char,
3001 .poll_put_char = serial8250_put_poll_char,
3002#endif
3003};
3004
3005void serial8250_init_port(struct uart_8250_port *up)
3006{
3007 struct uart_port *port = &up->port;
3008
3009 spin_lock_init(&port->lock);
3010 port->ops = &serial8250_pops;
3011
3012 up->cur_iotype = 0xFF;
3013}
3014EXPORT_SYMBOL_GPL(serial8250_init_port);
3015
3016void serial8250_set_defaults(struct uart_8250_port *up)
3017{
3018 struct uart_port *port = &up->port;
3019
3020 if (up->port.flags & UPF_FIXED_TYPE) {
3021 unsigned int type = up->port.type;
3022
3023 if (!up->port.fifosize)
3024 up->port.fifosize = uart_config[type].fifo_size;
3025 if (!up->tx_loadsz)
3026 up->tx_loadsz = uart_config[type].tx_loadsz;
3027 if (!up->capabilities)
3028 up->capabilities = uart_config[type].flags;
3029 }
3030
3031 set_io_from_upio(port);
3032
3033 /* default dma handlers */
3034 if (up->dma) {
3035 if (!up->dma->tx_dma)
3036 up->dma->tx_dma = serial8250_tx_dma;
3037 if (!up->dma->rx_dma)
3038 up->dma->rx_dma = serial8250_rx_dma;
3039 }
3040}
3041EXPORT_SYMBOL_GPL(serial8250_set_defaults);
3042
3043#ifdef CONFIG_SERIAL_8250_CONSOLE
3044
3045static void serial8250_console_putchar(struct uart_port *port, int ch)
3046{
3047 struct uart_8250_port *up = up_to_u8250p(port);
3048
3049 wait_for_xmitr(up, UART_LSR_THRE);
3050 serial_port_out(port, UART_TX, ch);
3051}
3052
3053/*
Peter Hurley10791232015-09-25 15:36:11 -04003054 * Restore serial console when h/w power-off detected
3055 */
3056static void serial8250_console_restore(struct uart_8250_port *up)
3057{
3058 struct uart_port *port = &up->port;
3059 struct ktermios termios;
3060 unsigned int baud, quot, frac = 0;
3061
3062 termios.c_cflag = port->cons->cflag;
3063 if (port->state->port.tty && termios.c_cflag == 0)
3064 termios.c_cflag = port->state->port.tty->termios.c_cflag;
3065
3066 baud = serial8250_get_baud_rate(port, &termios, NULL);
3067 quot = serial8250_get_divisor(up, baud, &frac);
3068
3069 serial8250_set_divisor(port, baud, quot, frac);
3070 serial_port_out(port, UART_LCR, up->lcr);
3071 serial_port_out(port, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
3072}
3073
3074/*
Peter Hurleyb6830f62015-06-27 09:19:00 -04003075 * Print a string to the serial port trying not to disturb
3076 * any possible real use of the port...
3077 *
3078 * The console_lock must be held when we get here.
3079 */
3080void serial8250_console_write(struct uart_8250_port *up, const char *s,
3081 unsigned int count)
3082{
3083 struct uart_port *port = &up->port;
3084 unsigned long flags;
3085 unsigned int ier;
3086 int locked = 1;
3087
3088 touch_nmi_watchdog();
3089
3090 serial8250_rpm_get(up);
3091
3092 if (port->sysrq)
3093 locked = 0;
3094 else if (oops_in_progress)
3095 locked = spin_trylock_irqsave(&port->lock, flags);
3096 else
3097 spin_lock_irqsave(&port->lock, flags);
3098
3099 /*
3100 * First save the IER then disable the interrupts
3101 */
3102 ier = serial_port_in(port, UART_IER);
3103
3104 if (up->capabilities & UART_CAP_UUE)
3105 serial_port_out(port, UART_IER, UART_IER_UUE);
3106 else
3107 serial_port_out(port, UART_IER, 0);
3108
3109 /* check scratch reg to see if port powered off during system sleep */
3110 if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) {
Peter Hurley10791232015-09-25 15:36:11 -04003111 serial8250_console_restore(up);
Peter Hurleyb6830f62015-06-27 09:19:00 -04003112 up->canary = 0;
3113 }
3114
3115 uart_console_write(port, s, count, serial8250_console_putchar);
3116
3117 /*
3118 * Finally, wait for transmitter to become empty
3119 * and restore the IER
3120 */
3121 wait_for_xmitr(up, BOTH_EMPTY);
3122 serial_port_out(port, UART_IER, ier);
3123
3124 /*
3125 * The receive handling will happen properly because the
3126 * receive ready bit will still be set; it is not cleared
3127 * on read. However, modem control will not, we must
3128 * call it if we have saved something in the saved flags
3129 * while processing with interrupts off.
3130 */
3131 if (up->msr_saved_flags)
3132 serial8250_modem_status(up);
3133
3134 if (locked)
3135 spin_unlock_irqrestore(&port->lock, flags);
3136 serial8250_rpm_put(up);
3137}
3138
3139static unsigned int probe_baud(struct uart_port *port)
3140{
3141 unsigned char lcr, dll, dlm;
3142 unsigned int quot;
3143
3144 lcr = serial_port_in(port, UART_LCR);
3145 serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB);
3146 dll = serial_port_in(port, UART_DLL);
3147 dlm = serial_port_in(port, UART_DLM);
3148 serial_port_out(port, UART_LCR, lcr);
3149
3150 quot = (dlm << 8) | dll;
3151 return (port->uartclk / 16) / quot;
3152}
3153
3154int serial8250_console_setup(struct uart_port *port, char *options, bool probe)
3155{
3156 int baud = 9600;
3157 int bits = 8;
3158 int parity = 'n';
3159 int flow = 'n';
3160
3161 if (!port->iobase && !port->membase)
3162 return -ENODEV;
3163
3164 if (options)
3165 uart_parse_options(options, &baud, &parity, &bits, &flow);
3166 else if (probe)
3167 baud = probe_baud(port);
3168
3169 return uart_set_options(port, port->cons, baud, parity, bits, flow);
3170}
3171
3172#endif /* CONFIG_SERIAL_8250_CONSOLE */
Jonathan McDowellf7a76512015-09-21 21:23:47 +01003173
3174MODULE_LICENSE("GPL");