blob: a35abc19fc1892a771ee33cea941e7306fd6d6ef [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
Anton Wuerfel6d7c1572016-01-14 16:08:11 +010056#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
Peter Hurleyb6830f62015-06-27 09:19:00 -040057
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{
Sebastian Frias0b41ce92015-12-18 17:40:05 +0100816 unsigned char old_lcr;
817 unsigned int id, old_dl;
Peter Hurleyb6830f62015-06-27 09:19:00 -0400818
819 old_lcr = serial_in(p, UART_LCR);
820 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
Sebastian Frias0b41ce92015-12-18 17:40:05 +0100821 old_dl = serial_dl_read(p);
822 serial_dl_write(p, 0);
823 id = serial_dl_read(p);
824 serial_dl_write(p, old_dl);
Peter Hurleyb6830f62015-06-27 09:19:00 -0400825
Peter Hurleyb6830f62015-06-27 09:19:00 -0400826 serial_out(p, UART_LCR, old_lcr);
827
828 return id;
829}
830
831/*
832 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
833 * When this function is called we know it is at least a StarTech
834 * 16650 V2, but it might be one of several StarTech UARTs, or one of
835 * its clones. (We treat the broken original StarTech 16650 V1 as a
836 * 16550, and why not? Startech doesn't seem to even acknowledge its
837 * existence.)
838 *
839 * What evil have men's minds wrought...
840 */
841static void autoconfig_has_efr(struct uart_8250_port *up)
842{
843 unsigned int id1, id2, id3, rev;
844
845 /*
846 * Everything with an EFR has SLEEP
847 */
848 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
849
850 /*
851 * First we check to see if it's an Oxford Semiconductor UART.
852 *
853 * If we have to do this here because some non-National
854 * Semiconductor clone chips lock up if you try writing to the
855 * LSR register (which serial_icr_read does)
856 */
857
858 /*
859 * Check for Oxford Semiconductor 16C950.
860 *
861 * EFR [4] must be set else this test fails.
862 *
863 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
864 * claims that it's needed for 952 dual UART's (which are not
865 * recommended for new designs).
866 */
867 up->acr = 0;
868 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
869 serial_out(up, UART_EFR, UART_EFR_ECB);
870 serial_out(up, UART_LCR, 0x00);
871 id1 = serial_icr_read(up, UART_ID1);
872 id2 = serial_icr_read(up, UART_ID2);
873 id3 = serial_icr_read(up, UART_ID3);
874 rev = serial_icr_read(up, UART_REV);
875
876 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
877
878 if (id1 == 0x16 && id2 == 0xC9 &&
879 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
880 up->port.type = PORT_16C950;
881
882 /*
883 * Enable work around for the Oxford Semiconductor 952 rev B
884 * chip which causes it to seriously miscalculate baud rates
885 * when DLL is 0.
886 */
887 if (id3 == 0x52 && rev == 0x01)
888 up->bugs |= UART_BUG_QUOT;
889 return;
890 }
891
892 /*
893 * We check for a XR16C850 by setting DLL and DLM to 0, and then
894 * reading back DLL and DLM. The chip type depends on the DLM
895 * value read back:
896 * 0x10 - XR16C850 and the DLL contains the chip revision.
897 * 0x12 - XR16C2850.
898 * 0x14 - XR16C854.
899 */
900 id1 = autoconfig_read_divisor_id(up);
901 DEBUG_AUTOCONF("850id=%04x ", id1);
902
903 id2 = id1 >> 8;
904 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
905 up->port.type = PORT_16850;
906 return;
907 }
908
909 /*
910 * It wasn't an XR16C850.
911 *
912 * We distinguish between the '654 and the '650 by counting
913 * how many bytes are in the FIFO. I'm using this for now,
914 * since that's the technique that was sent to me in the
915 * serial driver update, but I'm not convinced this works.
916 * I've had problems doing this in the past. -TYT
917 */
918 if (size_fifo(up) == 64)
919 up->port.type = PORT_16654;
920 else
921 up->port.type = PORT_16650V2;
922}
923
924/*
925 * We detected a chip without a FIFO. Only two fall into
926 * this category - the original 8250 and the 16450. The
927 * 16450 has a scratch register (accessible with LCR=0)
928 */
929static void autoconfig_8250(struct uart_8250_port *up)
930{
931 unsigned char scratch, status1, status2;
932
933 up->port.type = PORT_8250;
934
935 scratch = serial_in(up, UART_SCR);
936 serial_out(up, UART_SCR, 0xa5);
937 status1 = serial_in(up, UART_SCR);
938 serial_out(up, UART_SCR, 0x5a);
939 status2 = serial_in(up, UART_SCR);
940 serial_out(up, UART_SCR, scratch);
941
942 if (status1 == 0xa5 && status2 == 0x5a)
943 up->port.type = PORT_16450;
944}
945
946static int broken_efr(struct uart_8250_port *up)
947{
948 /*
949 * Exar ST16C2550 "A2" devices incorrectly detect as
950 * having an EFR, and report an ID of 0x0201. See
951 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
952 */
953 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
954 return 1;
955
956 return 0;
957}
958
959/*
960 * We know that the chip has FIFOs. Does it have an EFR? The
961 * EFR is located in the same register position as the IIR and
962 * we know the top two bits of the IIR are currently set. The
963 * EFR should contain zero. Try to read the EFR.
964 */
965static void autoconfig_16550a(struct uart_8250_port *up)
966{
967 unsigned char status1, status2;
968 unsigned int iersave;
969
970 up->port.type = PORT_16550A;
971 up->capabilities |= UART_CAP_FIFO;
972
973 /*
974 * XR17V35x UARTs have an extra divisor register, DLD
975 * that gets enabled with when DLAB is set which will
976 * cause the device to incorrectly match and assign
977 * port type to PORT_16650. The EFR for this UART is
978 * found at offset 0x09. Instead check the Deice ID (DVID)
979 * register for a 2, 4 or 8 port UART.
980 */
981 if (up->port.flags & UPF_EXAR_EFR) {
982 status1 = serial_in(up, UART_EXAR_DVID);
983 if (status1 == 0x82 || status1 == 0x84 || status1 == 0x88) {
984 DEBUG_AUTOCONF("Exar XR17V35x ");
985 up->port.type = PORT_XR17V35X;
986 up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
987 UART_CAP_SLEEP;
988
989 return;
990 }
991
992 }
993
994 /*
995 * Check for presence of the EFR when DLAB is set.
996 * Only ST16C650V1 UARTs pass this test.
997 */
998 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
999 if (serial_in(up, UART_EFR) == 0) {
1000 serial_out(up, UART_EFR, 0xA8);
1001 if (serial_in(up, UART_EFR) != 0) {
1002 DEBUG_AUTOCONF("EFRv1 ");
1003 up->port.type = PORT_16650;
1004 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
1005 } else {
1006 serial_out(up, UART_LCR, 0);
1007 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
1008 UART_FCR7_64BYTE);
1009 status1 = serial_in(up, UART_IIR) >> 5;
1010 serial_out(up, UART_FCR, 0);
1011 serial_out(up, UART_LCR, 0);
1012
1013 if (status1 == 7)
1014 up->port.type = PORT_16550A_FSL64;
1015 else
1016 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
1017 }
1018 serial_out(up, UART_EFR, 0);
1019 return;
1020 }
1021
1022 /*
1023 * Maybe it requires 0xbf to be written to the LCR.
1024 * (other ST16C650V2 UARTs, TI16C752A, etc)
1025 */
1026 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1027 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
1028 DEBUG_AUTOCONF("EFRv2 ");
1029 autoconfig_has_efr(up);
1030 return;
1031 }
1032
1033 /*
1034 * Check for a National Semiconductor SuperIO chip.
1035 * Attempt to switch to bank 2, read the value of the LOOP bit
1036 * from EXCR1. Switch back to bank 0, change it in MCR. Then
1037 * switch back to bank 2, read it from EXCR1 again and check
1038 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
1039 */
1040 serial_out(up, UART_LCR, 0);
1041 status1 = serial_in(up, UART_MCR);
1042 serial_out(up, UART_LCR, 0xE0);
1043 status2 = serial_in(up, 0x02); /* EXCR1 */
1044
1045 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
1046 serial_out(up, UART_LCR, 0);
1047 serial_out(up, UART_MCR, status1 ^ UART_MCR_LOOP);
1048 serial_out(up, UART_LCR, 0xE0);
1049 status2 = serial_in(up, 0x02); /* EXCR1 */
1050 serial_out(up, UART_LCR, 0);
1051 serial_out(up, UART_MCR, status1);
1052
1053 if ((status2 ^ status1) & UART_MCR_LOOP) {
1054 unsigned short quot;
1055
1056 serial_out(up, UART_LCR, 0xE0);
1057
1058 quot = serial_dl_read(up);
1059 quot <<= 3;
1060
1061 if (ns16550a_goto_highspeed(up))
1062 serial_dl_write(up, quot);
1063
1064 serial_out(up, UART_LCR, 0);
1065
1066 up->port.uartclk = 921600*16;
1067 up->port.type = PORT_NS16550A;
1068 up->capabilities |= UART_NATSEMI;
1069 return;
1070 }
1071 }
1072
1073 /*
1074 * No EFR. Try to detect a TI16750, which only sets bit 5 of
1075 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1076 * Try setting it with and without DLAB set. Cheap clones
1077 * set bit 5 without DLAB set.
1078 */
1079 serial_out(up, UART_LCR, 0);
1080 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1081 status1 = serial_in(up, UART_IIR) >> 5;
1082 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1083 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1084 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1085 status2 = serial_in(up, UART_IIR) >> 5;
1086 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1087 serial_out(up, UART_LCR, 0);
1088
1089 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1090
1091 if (status1 == 6 && status2 == 7) {
1092 up->port.type = PORT_16750;
1093 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1094 return;
1095 }
1096
1097 /*
1098 * Try writing and reading the UART_IER_UUE bit (b6).
1099 * If it works, this is probably one of the Xscale platform's
1100 * internal UARTs.
1101 * We're going to explicitly set the UUE bit to 0 before
1102 * trying to write and read a 1 just to make sure it's not
1103 * already a 1 and maybe locked there before we even start start.
1104 */
1105 iersave = serial_in(up, UART_IER);
1106 serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
1107 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1108 /*
1109 * OK it's in a known zero state, try writing and reading
1110 * without disturbing the current state of the other bits.
1111 */
1112 serial_out(up, UART_IER, iersave | UART_IER_UUE);
1113 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1114 /*
1115 * It's an Xscale.
1116 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1117 */
1118 DEBUG_AUTOCONF("Xscale ");
1119 up->port.type = PORT_XSCALE;
1120 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
1121 return;
1122 }
1123 } else {
1124 /*
1125 * If we got here we couldn't force the IER_UUE bit to 0.
1126 * Log it and continue.
1127 */
1128 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1129 }
1130 serial_out(up, UART_IER, iersave);
1131
1132 /*
1133 * Exar uarts have EFR in a weird location
1134 */
1135 if (up->port.flags & UPF_EXAR_EFR) {
1136 DEBUG_AUTOCONF("Exar XR17D15x ");
1137 up->port.type = PORT_XR17D15X;
1138 up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
1139 UART_CAP_SLEEP;
1140
1141 return;
1142 }
1143
1144 /*
1145 * We distinguish between 16550A and U6 16550A by counting
1146 * how many bytes are in the FIFO.
1147 */
1148 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1149 up->port.type = PORT_U6_16550A;
1150 up->capabilities |= UART_CAP_AFE;
1151 }
1152}
1153
1154/*
1155 * This routine is called by rs_init() to initialize a specific serial
1156 * port. It determines what type of UART chip this serial port is
1157 * using: 8250, 16450, 16550, 16550A. The important question is
1158 * whether or not this UART is a 16550A or not, since this will
1159 * determine whether or not we can use its FIFO features or not.
1160 */
1161static void autoconfig(struct uart_8250_port *up)
1162{
1163 unsigned char status1, scratch, scratch2, scratch3;
1164 unsigned char save_lcr, save_mcr;
1165 struct uart_port *port = &up->port;
1166 unsigned long flags;
1167 unsigned int old_capabilities;
1168
1169 if (!port->iobase && !port->mapbase && !port->membase)
1170 return;
1171
1172 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
1173 serial_index(port), port->iobase, port->membase);
1174
1175 /*
1176 * We really do need global IRQs disabled here - we're going to
1177 * be frobbing the chips IRQ enable register to see if it exists.
1178 */
1179 spin_lock_irqsave(&port->lock, flags);
1180
1181 up->capabilities = 0;
1182 up->bugs = 0;
1183
1184 if (!(port->flags & UPF_BUGGY_UART)) {
1185 /*
1186 * Do a simple existence test first; if we fail this,
1187 * there's no point trying anything else.
1188 *
1189 * 0x80 is used as a nonsense port to prevent against
1190 * false positives due to ISA bus float. The
1191 * assumption is that 0x80 is a non-existent port;
1192 * which should be safe since include/asm/io.h also
1193 * makes this assumption.
1194 *
1195 * Note: this is safe as long as MCR bit 4 is clear
1196 * and the device is in "PC" mode.
1197 */
1198 scratch = serial_in(up, UART_IER);
1199 serial_out(up, UART_IER, 0);
1200#ifdef __i386__
1201 outb(0xff, 0x080);
1202#endif
1203 /*
1204 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1205 * 16C754B) allow only to modify them if an EFR bit is set.
1206 */
1207 scratch2 = serial_in(up, UART_IER) & 0x0f;
1208 serial_out(up, UART_IER, 0x0F);
1209#ifdef __i386__
1210 outb(0, 0x080);
1211#endif
1212 scratch3 = serial_in(up, UART_IER) & 0x0f;
1213 serial_out(up, UART_IER, scratch);
1214 if (scratch2 != 0 || scratch3 != 0x0F) {
1215 /*
1216 * We failed; there's nothing here
1217 */
1218 spin_unlock_irqrestore(&port->lock, flags);
1219 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1220 scratch2, scratch3);
1221 goto out;
1222 }
1223 }
1224
1225 save_mcr = serial_in(up, UART_MCR);
1226 save_lcr = serial_in(up, UART_LCR);
1227
1228 /*
1229 * Check to see if a UART is really there. Certain broken
1230 * internal modems based on the Rockwell chipset fail this
1231 * test, because they apparently don't implement the loopback
1232 * test mode. So this test is skipped on the COM 1 through
1233 * COM 4 ports. This *should* be safe, since no board
1234 * manufacturer would be stupid enough to design a board
1235 * that conflicts with COM 1-4 --- we hope!
1236 */
1237 if (!(port->flags & UPF_SKIP_TEST)) {
1238 serial_out(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1239 status1 = serial_in(up, UART_MSR) & 0xF0;
1240 serial_out(up, UART_MCR, save_mcr);
1241 if (status1 != 0x90) {
1242 spin_unlock_irqrestore(&port->lock, flags);
1243 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1244 status1);
1245 goto out;
1246 }
1247 }
1248
1249 /*
1250 * We're pretty sure there's a port here. Lets find out what
1251 * type of port it is. The IIR top two bits allows us to find
1252 * out if it's 8250 or 16450, 16550, 16550A or later. This
1253 * determines what we test for next.
1254 *
1255 * We also initialise the EFR (if any) to zero for later. The
1256 * EFR occupies the same register location as the FCR and IIR.
1257 */
1258 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1259 serial_out(up, UART_EFR, 0);
1260 serial_out(up, UART_LCR, 0);
1261
1262 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1263 scratch = serial_in(up, UART_IIR) >> 6;
1264
1265 switch (scratch) {
1266 case 0:
1267 autoconfig_8250(up);
1268 break;
1269 case 1:
1270 port->type = PORT_UNKNOWN;
1271 break;
1272 case 2:
1273 port->type = PORT_16550;
1274 break;
1275 case 3:
1276 autoconfig_16550a(up);
1277 break;
1278 }
1279
1280#ifdef CONFIG_SERIAL_8250_RSA
1281 /*
1282 * Only probe for RSA ports if we got the region.
1283 */
1284 if (port->type == PORT_16550A && up->probe & UART_PROBE_RSA &&
1285 __enable_rsa(up))
1286 port->type = PORT_RSA;
1287#endif
1288
1289 serial_out(up, UART_LCR, save_lcr);
1290
1291 port->fifosize = uart_config[up->port.type].fifo_size;
1292 old_capabilities = up->capabilities;
1293 up->capabilities = uart_config[port->type].flags;
1294 up->tx_loadsz = uart_config[port->type].tx_loadsz;
1295
1296 if (port->type == PORT_UNKNOWN)
1297 goto out_lock;
1298
1299 /*
1300 * Reset the UART.
1301 */
1302#ifdef CONFIG_SERIAL_8250_RSA
1303 if (port->type == PORT_RSA)
1304 serial_out(up, UART_RSA_FRR, 0);
1305#endif
1306 serial_out(up, UART_MCR, save_mcr);
1307 serial8250_clear_fifos(up);
1308 serial_in(up, UART_RX);
1309 if (up->capabilities & UART_CAP_UUE)
1310 serial_out(up, UART_IER, UART_IER_UUE);
1311 else
1312 serial_out(up, UART_IER, 0);
1313
1314out_lock:
1315 spin_unlock_irqrestore(&port->lock, flags);
1316 if (up->capabilities != old_capabilities) {
1317 printk(KERN_WARNING
1318 "ttyS%d: detected caps %08x should be %08x\n",
1319 serial_index(port), old_capabilities,
1320 up->capabilities);
1321 }
1322out:
1323 DEBUG_AUTOCONF("iir=%d ", scratch);
1324 DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
1325}
1326
1327static void autoconfig_irq(struct uart_8250_port *up)
1328{
1329 struct uart_port *port = &up->port;
1330 unsigned char save_mcr, save_ier;
1331 unsigned char save_ICP = 0;
1332 unsigned int ICP = 0;
1333 unsigned long irqs;
1334 int irq;
1335
1336 if (port->flags & UPF_FOURPORT) {
1337 ICP = (port->iobase & 0xfe0) | 0x1f;
1338 save_ICP = inb_p(ICP);
1339 outb_p(0x80, ICP);
1340 inb_p(ICP);
1341 }
1342
Taichi Kageyama9a23a1d2015-08-17 02:45:29 +00001343 if (uart_console(port))
1344 console_lock();
1345
Peter Hurleyb6830f62015-06-27 09:19:00 -04001346 /* forget possible initially masked and pending IRQ */
1347 probe_irq_off(probe_irq_on());
1348 save_mcr = serial_in(up, UART_MCR);
1349 save_ier = serial_in(up, UART_IER);
1350 serial_out(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
1351
1352 irqs = probe_irq_on();
1353 serial_out(up, UART_MCR, 0);
1354 udelay(10);
1355 if (port->flags & UPF_FOURPORT) {
1356 serial_out(up, UART_MCR,
1357 UART_MCR_DTR | UART_MCR_RTS);
1358 } else {
1359 serial_out(up, UART_MCR,
1360 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1361 }
1362 serial_out(up, UART_IER, 0x0f); /* enable all intrs */
1363 serial_in(up, UART_LSR);
1364 serial_in(up, UART_RX);
1365 serial_in(up, UART_IIR);
1366 serial_in(up, UART_MSR);
1367 serial_out(up, UART_TX, 0xFF);
1368 udelay(20);
1369 irq = probe_irq_off(irqs);
1370
1371 serial_out(up, UART_MCR, save_mcr);
1372 serial_out(up, UART_IER, save_ier);
1373
1374 if (port->flags & UPF_FOURPORT)
1375 outb_p(save_ICP, ICP);
1376
Taichi Kageyama9a23a1d2015-08-17 02:45:29 +00001377 if (uart_console(port))
1378 console_unlock();
1379
Peter Hurleyb6830f62015-06-27 09:19:00 -04001380 port->irq = (irq > 0) ? irq : 0;
1381}
1382
Matwey V. Kornilova07a70b2016-02-01 21:09:20 +03001383static void serial8250_stop_rx(struct uart_port *port)
1384{
1385 struct uart_8250_port *up = up_to_u8250p(port);
1386
1387 serial8250_rpm_get(up);
1388
1389 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1390 up->port.read_status_mask &= ~UART_LSR_DR;
1391 serial_port_out(port, UART_IER, up->ier);
1392
1393 serial8250_rpm_put(up);
1394}
1395
Matwey V. Kornilove490c912016-02-01 21:09:21 +03001396static void __do_stop_tx_rs485(struct uart_8250_port *p)
1397{
1398 if (!p->em485)
1399 return;
1400
1401 serial8250_em485_rts_after_send(p);
1402 /*
1403 * Empty the RX FIFO, we are not interested in anything
1404 * received during the half-duplex transmission.
1405 */
1406 if (!(p->port.rs485.flags & SER_RS485_RX_DURING_TX))
1407 serial8250_clear_fifos(p);
1408}
1409
1410static void serial8250_em485_handle_stop_tx(unsigned long arg)
1411{
1412 struct uart_8250_port *p = (struct uart_8250_port *)arg;
1413 struct uart_8250_em485 *em485 = p->em485;
1414 unsigned long flags;
1415
1416 spin_lock_irqsave(&p->port.lock, flags);
1417 if (em485 &&
1418 em485->active_timer == &em485->stop_tx_timer) {
1419 __do_stop_tx_rs485(p);
1420 em485->active_timer = NULL;
1421 }
1422 spin_unlock_irqrestore(&p->port.lock, flags);
1423}
1424
1425static void __stop_tx_rs485(struct uart_8250_port *p)
1426{
1427 struct uart_8250_em485 *em485 = p->em485;
1428
1429 if (!em485)
1430 return;
1431
1432 /*
1433 * __do_stop_tx_rs485 is going to set RTS according to config
1434 * AND flush RX FIFO if required.
1435 */
1436 if (p->port.rs485.delay_rts_after_send > 0) {
1437 em485->active_timer = &em485->stop_tx_timer;
1438 mod_timer(&em485->stop_tx_timer, jiffies +
1439 p->port.rs485.delay_rts_after_send * HZ / 1000);
1440 } else {
1441 __do_stop_tx_rs485(p);
1442 }
1443}
1444
1445static inline void __do_stop_tx(struct uart_8250_port *p)
Peter Hurleyb6830f62015-06-27 09:19:00 -04001446{
1447 if (p->ier & UART_IER_THRI) {
1448 p->ier &= ~UART_IER_THRI;
1449 serial_out(p, UART_IER, p->ier);
1450 serial8250_rpm_put_tx(p);
1451 }
1452}
1453
Matwey V. Kornilove490c912016-02-01 21:09:21 +03001454static inline void __stop_tx(struct uart_8250_port *p)
1455{
1456 struct uart_8250_em485 *em485 = p->em485;
1457
1458 if (em485) {
1459 unsigned char lsr = serial_in(p, UART_LSR);
1460 /*
1461 * To provide required timeing and allow FIFO transfer,
1462 * __stop_tx_rs485 must be called only when both FIFO and
1463 * shift register are empty. It is for device driver to enable
1464 * interrupt on TEMT.
1465 */
1466 if ((lsr & BOTH_EMPTY) != BOTH_EMPTY)
1467 return;
1468
1469 del_timer(&em485->start_tx_timer);
1470 em485->active_timer = NULL;
1471 }
1472 __do_stop_tx(p);
1473 __stop_tx_rs485(p);
1474}
1475
Peter Hurleyb6830f62015-06-27 09:19:00 -04001476static void serial8250_stop_tx(struct uart_port *port)
1477{
1478 struct uart_8250_port *up = up_to_u8250p(port);
1479
1480 serial8250_rpm_get(up);
1481 __stop_tx(up);
1482
1483 /*
1484 * We really want to stop the transmitter from sending.
1485 */
1486 if (port->type == PORT_16C950) {
1487 up->acr |= UART_ACR_TXDIS;
1488 serial_icr_write(up, UART_ACR, up->acr);
1489 }
1490 serial8250_rpm_put(up);
1491}
1492
Matwey V. Kornilove490c912016-02-01 21:09:21 +03001493static inline void __start_tx(struct uart_port *port)
Peter Hurleyb6830f62015-06-27 09:19:00 -04001494{
1495 struct uart_8250_port *up = up_to_u8250p(port);
1496
Peter Hurleyb6830f62015-06-27 09:19:00 -04001497 if (up->dma && !up->dma->tx_dma(up))
1498 return;
1499
1500 if (!(up->ier & UART_IER_THRI)) {
1501 up->ier |= UART_IER_THRI;
1502 serial_port_out(port, UART_IER, up->ier);
1503
1504 if (up->bugs & UART_BUG_TXEN) {
1505 unsigned char lsr;
Anton Wuerfel1a33e342016-01-14 16:08:10 +01001506
Peter Hurleyb6830f62015-06-27 09:19:00 -04001507 lsr = serial_in(up, UART_LSR);
1508 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1509 if (lsr & UART_LSR_THRE)
1510 serial8250_tx_chars(up);
1511 }
1512 }
1513
1514 /*
1515 * Re-enable the transmitter if we disabled it.
1516 */
1517 if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1518 up->acr &= ~UART_ACR_TXDIS;
1519 serial_icr_write(up, UART_ACR, up->acr);
1520 }
1521}
1522
Matwey V. Kornilove490c912016-02-01 21:09:21 +03001523static inline void start_tx_rs485(struct uart_port *port)
1524{
1525 struct uart_8250_port *up = up_to_u8250p(port);
1526 struct uart_8250_em485 *em485 = up->em485;
1527 unsigned char mcr;
1528
1529 if (!(up->port.rs485.flags & SER_RS485_RX_DURING_TX))
1530 serial8250_stop_rx(&up->port);
1531
1532 del_timer(&em485->stop_tx_timer);
1533 em485->active_timer = NULL;
1534
1535 mcr = serial_in(up, UART_MCR);
1536 if (!!(up->port.rs485.flags & SER_RS485_RTS_ON_SEND) !=
1537 !!(mcr & UART_MCR_RTS)) {
1538 if (up->port.rs485.flags & SER_RS485_RTS_ON_SEND)
1539 mcr |= UART_MCR_RTS;
1540 else
1541 mcr &= ~UART_MCR_RTS;
1542 serial_out(up, UART_MCR, mcr);
1543
1544 if (up->port.rs485.delay_rts_before_send > 0) {
1545 em485->active_timer = &em485->start_tx_timer;
1546 mod_timer(&em485->start_tx_timer, jiffies +
1547 up->port.rs485.delay_rts_before_send * HZ / 1000);
1548 return;
1549 }
1550 }
1551
1552 __start_tx(port);
1553}
1554
1555static void serial8250_em485_handle_start_tx(unsigned long arg)
1556{
1557 struct uart_8250_port *p = (struct uart_8250_port *)arg;
1558 struct uart_8250_em485 *em485 = p->em485;
1559 unsigned long flags;
1560
1561 spin_lock_irqsave(&p->port.lock, flags);
1562 if (em485 &&
1563 em485->active_timer == &em485->start_tx_timer) {
1564 __start_tx(&p->port);
1565 em485->active_timer = NULL;
1566 }
1567 spin_unlock_irqrestore(&p->port.lock, flags);
1568}
1569
1570static void serial8250_start_tx(struct uart_port *port)
1571{
1572 struct uart_8250_port *up = up_to_u8250p(port);
1573 struct uart_8250_em485 *em485 = up->em485;
1574
1575 serial8250_rpm_get_tx(up);
1576
1577 if (em485 &&
1578 em485->active_timer == &em485->start_tx_timer)
1579 return;
1580
1581 if (em485)
1582 start_tx_rs485(port);
1583 else
1584 __start_tx(port);
1585}
1586
Peter Hurleyb6830f62015-06-27 09:19:00 -04001587static void serial8250_throttle(struct uart_port *port)
1588{
1589 port->throttle(port);
1590}
1591
1592static void serial8250_unthrottle(struct uart_port *port)
1593{
1594 port->unthrottle(port);
1595}
1596
Peter Hurleyb6830f62015-06-27 09:19:00 -04001597static void serial8250_disable_ms(struct uart_port *port)
1598{
1599 struct uart_8250_port *up =
1600 container_of(port, struct uart_8250_port, port);
1601
1602 /* no MSR capabilities */
1603 if (up->bugs & UART_BUG_NOMSR)
1604 return;
1605
1606 up->ier &= ~UART_IER_MSI;
1607 serial_port_out(port, UART_IER, up->ier);
1608}
1609
1610static void serial8250_enable_ms(struct uart_port *port)
1611{
1612 struct uart_8250_port *up = up_to_u8250p(port);
1613
1614 /* no MSR capabilities */
1615 if (up->bugs & UART_BUG_NOMSR)
1616 return;
1617
1618 up->ier |= UART_IER_MSI;
1619
1620 serial8250_rpm_get(up);
1621 serial_port_out(port, UART_IER, up->ier);
1622 serial8250_rpm_put(up);
1623}
1624
Peter Hurley3f6b3ce2016-01-10 14:39:31 -08001625static void serial8250_read_char(struct uart_8250_port *up, unsigned char lsr)
1626{
1627 struct uart_port *port = &up->port;
1628 unsigned char ch;
1629 char flag = TTY_NORMAL;
1630
1631 if (likely(lsr & UART_LSR_DR))
1632 ch = serial_in(up, UART_RX);
1633 else
1634 /*
1635 * Intel 82571 has a Serial Over Lan device that will
1636 * set UART_LSR_BI without setting UART_LSR_DR when
1637 * it receives a break. To avoid reading from the
1638 * receive buffer without UART_LSR_DR bit set, we
1639 * just force the read character to be 0
1640 */
1641 ch = 0;
1642
1643 port->icount.rx++;
1644
1645 lsr |= up->lsr_saved_flags;
1646 up->lsr_saved_flags = 0;
1647
1648 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1649 if (lsr & UART_LSR_BI) {
1650 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1651 port->icount.brk++;
1652 /*
1653 * We do the SysRQ and SAK checking
1654 * here because otherwise the break
1655 * may get masked by ignore_status_mask
1656 * or read_status_mask.
1657 */
1658 if (uart_handle_break(port))
1659 return;
1660 } else if (lsr & UART_LSR_PE)
1661 port->icount.parity++;
1662 else if (lsr & UART_LSR_FE)
1663 port->icount.frame++;
1664 if (lsr & UART_LSR_OE)
1665 port->icount.overrun++;
1666
1667 /*
1668 * Mask off conditions which should be ignored.
1669 */
1670 lsr &= port->read_status_mask;
1671
1672 if (lsr & UART_LSR_BI) {
1673 DEBUG_INTR("handling break....");
1674 flag = TTY_BREAK;
1675 } else if (lsr & UART_LSR_PE)
1676 flag = TTY_PARITY;
1677 else if (lsr & UART_LSR_FE)
1678 flag = TTY_FRAME;
1679 }
1680 if (uart_handle_sysrq_char(port, ch))
1681 return;
1682
1683 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
1684}
1685
Peter Hurleyb6830f62015-06-27 09:19:00 -04001686/*
1687 * serial8250_rx_chars: processes according to the passed in LSR
1688 * value, and returns the remaining LSR bits not handled
1689 * by this Rx routine.
1690 */
Peter Hurleyd22f8f12016-01-10 14:39:32 -08001691unsigned char serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
Peter Hurleyb6830f62015-06-27 09:19:00 -04001692{
1693 struct uart_port *port = &up->port;
Peter Hurleyb6830f62015-06-27 09:19:00 -04001694 int max_count = 256;
Peter Hurleyb6830f62015-06-27 09:19:00 -04001695
1696 do {
Peter Hurley3f6b3ce2016-01-10 14:39:31 -08001697 serial8250_read_char(up, lsr);
Peter Hurleyd22f8f12016-01-10 14:39:32 -08001698 if (--max_count == 0)
1699 break;
Peter Hurleyb6830f62015-06-27 09:19:00 -04001700 lsr = serial_in(up, UART_LSR);
Peter Hurleyd22f8f12016-01-10 14:39:32 -08001701 } while (lsr & (UART_LSR_DR | UART_LSR_BI));
Peter Hurley6a597a32016-01-10 14:39:33 -08001702
Peter Hurleyb6830f62015-06-27 09:19:00 -04001703 tty_flip_buffer_push(&port->state->port);
Peter Hurleyb6830f62015-06-27 09:19:00 -04001704 return lsr;
1705}
1706EXPORT_SYMBOL_GPL(serial8250_rx_chars);
1707
1708void serial8250_tx_chars(struct uart_8250_port *up)
1709{
1710 struct uart_port *port = &up->port;
1711 struct circ_buf *xmit = &port->state->xmit;
1712 int count;
1713
1714 if (port->x_char) {
1715 serial_out(up, UART_TX, port->x_char);
1716 port->icount.tx++;
1717 port->x_char = 0;
1718 return;
1719 }
1720 if (uart_tx_stopped(port)) {
1721 serial8250_stop_tx(port);
1722 return;
1723 }
1724 if (uart_circ_empty(xmit)) {
1725 __stop_tx(up);
1726 return;
1727 }
1728
1729 count = up->tx_loadsz;
1730 do {
1731 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1732 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1733 port->icount.tx++;
1734 if (uart_circ_empty(xmit))
1735 break;
Peter Hurleycee10c82016-01-10 14:39:36 -08001736 if ((up->capabilities & UART_CAP_HFIFO) &&
1737 (serial_in(up, UART_LSR) & BOTH_EMPTY) != BOTH_EMPTY)
1738 break;
Peter Hurleyb6830f62015-06-27 09:19:00 -04001739 } while (--count > 0);
1740
1741 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1742 uart_write_wakeup(port);
1743
1744 DEBUG_INTR("THRE...");
1745
1746 /*
1747 * With RPM enabled, we have to wait until the FIFO is empty before the
1748 * HW can go idle. So we get here once again with empty FIFO and disable
1749 * the interrupt and RPM in __stop_tx()
1750 */
1751 if (uart_circ_empty(xmit) && !(up->capabilities & UART_CAP_RPM))
1752 __stop_tx(up);
1753}
1754EXPORT_SYMBOL_GPL(serial8250_tx_chars);
1755
1756/* Caller holds uart port lock */
1757unsigned int serial8250_modem_status(struct uart_8250_port *up)
1758{
1759 struct uart_port *port = &up->port;
1760 unsigned int status = serial_in(up, UART_MSR);
1761
1762 status |= up->msr_saved_flags;
1763 up->msr_saved_flags = 0;
1764 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1765 port->state != NULL) {
1766 if (status & UART_MSR_TERI)
1767 port->icount.rng++;
1768 if (status & UART_MSR_DDSR)
1769 port->icount.dsr++;
1770 if (status & UART_MSR_DDCD)
1771 uart_handle_dcd_change(port, status & UART_MSR_DCD);
1772 if (status & UART_MSR_DCTS)
1773 uart_handle_cts_change(port, status & UART_MSR_CTS);
1774
1775 wake_up_interruptible(&port->state->port.delta_msr_wait);
1776 }
1777
1778 return status;
1779}
1780EXPORT_SYMBOL_GPL(serial8250_modem_status);
1781
1782/*
1783 * This handles the interrupt from one port.
1784 */
1785int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
1786{
1787 unsigned char status;
1788 unsigned long flags;
1789 struct uart_8250_port *up = up_to_u8250p(port);
1790 int dma_err = 0;
1791
1792 if (iir & UART_IIR_NO_INT)
1793 return 0;
1794
1795 spin_lock_irqsave(&port->lock, flags);
1796
1797 status = serial_port_in(port, UART_LSR);
1798
1799 DEBUG_INTR("status = %x...", status);
1800
1801 if (status & (UART_LSR_DR | UART_LSR_BI)) {
1802 if (up->dma)
1803 dma_err = up->dma->rx_dma(up, iir);
1804
1805 if (!up->dma || dma_err)
1806 status = serial8250_rx_chars(up, status);
1807 }
1808 serial8250_modem_status(up);
1809 if ((!up->dma || (up->dma && up->dma->tx_err)) &&
1810 (status & UART_LSR_THRE))
1811 serial8250_tx_chars(up);
1812
1813 spin_unlock_irqrestore(&port->lock, flags);
1814 return 1;
1815}
1816EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1817
1818static int serial8250_default_handle_irq(struct uart_port *port)
1819{
1820 struct uart_8250_port *up = up_to_u8250p(port);
1821 unsigned int iir;
1822 int ret;
1823
1824 serial8250_rpm_get(up);
1825
1826 iir = serial_port_in(port, UART_IIR);
1827 ret = serial8250_handle_irq(port, iir);
1828
1829 serial8250_rpm_put(up);
1830 return ret;
1831}
1832
1833/*
1834 * These Exar UARTs have an extra interrupt indicator that could
1835 * fire for a few unimplemented interrupts. One of which is a
1836 * wakeup event when coming out of sleep. Put this here just
1837 * to be on the safe side that these interrupts don't go unhandled.
1838 */
1839static int exar_handle_irq(struct uart_port *port)
1840{
1841 unsigned char int0, int1, int2, int3;
1842 unsigned int iir = serial_port_in(port, UART_IIR);
1843 int ret;
1844
1845 ret = serial8250_handle_irq(port, iir);
1846
1847 if ((port->type == PORT_XR17V35X) ||
1848 (port->type == PORT_XR17D15X)) {
1849 int0 = serial_port_in(port, 0x80);
1850 int1 = serial_port_in(port, 0x81);
1851 int2 = serial_port_in(port, 0x82);
1852 int3 = serial_port_in(port, 0x83);
1853 }
1854
1855 return ret;
1856}
1857
1858static unsigned int serial8250_tx_empty(struct uart_port *port)
1859{
1860 struct uart_8250_port *up = up_to_u8250p(port);
1861 unsigned long flags;
1862 unsigned int lsr;
1863
1864 serial8250_rpm_get(up);
1865
1866 spin_lock_irqsave(&port->lock, flags);
1867 lsr = serial_port_in(port, UART_LSR);
1868 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1869 spin_unlock_irqrestore(&port->lock, flags);
1870
1871 serial8250_rpm_put(up);
1872
1873 return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
1874}
1875
1876static unsigned int serial8250_get_mctrl(struct uart_port *port)
1877{
1878 struct uart_8250_port *up = up_to_u8250p(port);
1879 unsigned int status;
1880 unsigned int ret;
1881
1882 serial8250_rpm_get(up);
1883 status = serial8250_modem_status(up);
1884 serial8250_rpm_put(up);
1885
1886 ret = 0;
1887 if (status & UART_MSR_DCD)
1888 ret |= TIOCM_CAR;
1889 if (status & UART_MSR_RI)
1890 ret |= TIOCM_RNG;
1891 if (status & UART_MSR_DSR)
1892 ret |= TIOCM_DSR;
1893 if (status & UART_MSR_CTS)
1894 ret |= TIOCM_CTS;
1895 return ret;
1896}
1897
1898void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl)
1899{
1900 struct uart_8250_port *up = up_to_u8250p(port);
1901 unsigned char mcr = 0;
1902
1903 if (mctrl & TIOCM_RTS)
1904 mcr |= UART_MCR_RTS;
1905 if (mctrl & TIOCM_DTR)
1906 mcr |= UART_MCR_DTR;
1907 if (mctrl & TIOCM_OUT1)
1908 mcr |= UART_MCR_OUT1;
1909 if (mctrl & TIOCM_OUT2)
1910 mcr |= UART_MCR_OUT2;
1911 if (mctrl & TIOCM_LOOP)
1912 mcr |= UART_MCR_LOOP;
1913
1914 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1915
1916 serial_port_out(port, UART_MCR, mcr);
1917}
1918EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);
1919
1920static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1921{
1922 if (port->set_mctrl)
1923 port->set_mctrl(port, mctrl);
1924 else
1925 serial8250_do_set_mctrl(port, mctrl);
1926}
1927
1928static void serial8250_break_ctl(struct uart_port *port, int break_state)
1929{
1930 struct uart_8250_port *up = up_to_u8250p(port);
1931 unsigned long flags;
1932
1933 serial8250_rpm_get(up);
1934 spin_lock_irqsave(&port->lock, flags);
1935 if (break_state == -1)
1936 up->lcr |= UART_LCR_SBC;
1937 else
1938 up->lcr &= ~UART_LCR_SBC;
1939 serial_port_out(port, UART_LCR, up->lcr);
1940 spin_unlock_irqrestore(&port->lock, flags);
1941 serial8250_rpm_put(up);
1942}
1943
1944/*
1945 * Wait for transmitter & holding register to empty
1946 */
1947static void wait_for_xmitr(struct uart_8250_port *up, int bits)
1948{
1949 unsigned int status, tmout = 10000;
1950
1951 /* Wait up to 10ms for the character(s) to be sent. */
1952 for (;;) {
1953 status = serial_in(up, UART_LSR);
1954
1955 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
1956
1957 if ((status & bits) == bits)
1958 break;
1959 if (--tmout == 0)
1960 break;
1961 udelay(1);
1962 }
1963
1964 /* Wait up to 1s for flow control if necessary */
1965 if (up->port.flags & UPF_CONS_FLOW) {
1966 unsigned int tmout;
Anton Wuerfel1a33e342016-01-14 16:08:10 +01001967
Peter Hurleyb6830f62015-06-27 09:19:00 -04001968 for (tmout = 1000000; tmout; tmout--) {
1969 unsigned int msr = serial_in(up, UART_MSR);
1970 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1971 if (msr & UART_MSR_CTS)
1972 break;
1973 udelay(1);
1974 touch_nmi_watchdog();
1975 }
1976 }
1977}
1978
1979#ifdef CONFIG_CONSOLE_POLL
1980/*
1981 * Console polling routines for writing and reading from the uart while
1982 * in an interrupt or debug context.
1983 */
1984
1985static int serial8250_get_poll_char(struct uart_port *port)
1986{
1987 struct uart_8250_port *up = up_to_u8250p(port);
1988 unsigned char lsr;
1989 int status;
1990
1991 serial8250_rpm_get(up);
1992
1993 lsr = serial_port_in(port, UART_LSR);
1994
1995 if (!(lsr & UART_LSR_DR)) {
1996 status = NO_POLL_CHAR;
1997 goto out;
1998 }
1999
2000 status = serial_port_in(port, UART_RX);
2001out:
2002 serial8250_rpm_put(up);
2003 return status;
2004}
2005
2006
2007static void serial8250_put_poll_char(struct uart_port *port,
2008 unsigned char c)
2009{
2010 unsigned int ier;
2011 struct uart_8250_port *up = up_to_u8250p(port);
2012
2013 serial8250_rpm_get(up);
2014 /*
2015 * First save the IER then disable the interrupts
2016 */
2017 ier = serial_port_in(port, UART_IER);
2018 if (up->capabilities & UART_CAP_UUE)
2019 serial_port_out(port, UART_IER, UART_IER_UUE);
2020 else
2021 serial_port_out(port, UART_IER, 0);
2022
2023 wait_for_xmitr(up, BOTH_EMPTY);
2024 /*
2025 * Send the character out.
2026 */
2027 serial_port_out(port, UART_TX, c);
2028
2029 /*
2030 * Finally, wait for transmitter to become empty
2031 * and restore the IER
2032 */
2033 wait_for_xmitr(up, BOTH_EMPTY);
2034 serial_port_out(port, UART_IER, ier);
2035 serial8250_rpm_put(up);
2036}
2037
2038#endif /* CONFIG_CONSOLE_POLL */
2039
2040int serial8250_do_startup(struct uart_port *port)
2041{
2042 struct uart_8250_port *up = up_to_u8250p(port);
2043 unsigned long flags;
2044 unsigned char lsr, iir;
2045 int retval;
2046
Peter Hurleyb6830f62015-06-27 09:19:00 -04002047 if (!port->fifosize)
2048 port->fifosize = uart_config[port->type].fifo_size;
2049 if (!up->tx_loadsz)
2050 up->tx_loadsz = uart_config[port->type].tx_loadsz;
2051 if (!up->capabilities)
2052 up->capabilities = uart_config[port->type].flags;
2053 up->mcr = 0;
2054
2055 if (port->iotype != up->cur_iotype)
2056 set_io_from_upio(port);
2057
2058 serial8250_rpm_get(up);
2059 if (port->type == PORT_16C950) {
2060 /* Wake up and initialize UART */
2061 up->acr = 0;
2062 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2063 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2064 serial_port_out(port, UART_IER, 0);
2065 serial_port_out(port, UART_LCR, 0);
2066 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2067 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2068 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2069 serial_port_out(port, UART_LCR, 0);
2070 }
2071
2072#ifdef CONFIG_SERIAL_8250_RSA
2073 /*
2074 * If this is an RSA port, see if we can kick it up to the
2075 * higher speed clock.
2076 */
2077 enable_rsa(up);
2078#endif
Joerg Roedelda891642015-07-16 10:29:13 +02002079
2080 if (port->type == PORT_XR17V35X) {
2081 /*
2082 * First enable access to IER [7:5], ISR [5:4], FCR [5:4],
2083 * MCR [7:5] and MSR [7:0]
2084 */
2085 serial_port_out(port, UART_XR_EFR, UART_EFR_ECB);
2086
2087 /*
2088 * Make sure all interrups are masked until initialization is
2089 * complete and the FIFOs are cleared
2090 */
2091 serial_port_out(port, UART_IER, 0);
2092 }
2093
Peter Hurleyb6830f62015-06-27 09:19:00 -04002094 /*
2095 * Clear the FIFO buffers and disable them.
2096 * (they will be reenabled in set_termios())
2097 */
2098 serial8250_clear_fifos(up);
2099
2100 /*
2101 * Clear the interrupt registers.
2102 */
2103 serial_port_in(port, UART_LSR);
2104 serial_port_in(port, UART_RX);
2105 serial_port_in(port, UART_IIR);
2106 serial_port_in(port, UART_MSR);
2107
2108 /*
2109 * At this point, there's no way the LSR could still be 0xff;
2110 * if it is, then bail out, because there's likely no UART
2111 * here.
2112 */
2113 if (!(port->flags & UPF_BUGGY_UART) &&
2114 (serial_port_in(port, UART_LSR) == 0xff)) {
2115 printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2116 serial_index(port));
2117 retval = -ENODEV;
2118 goto out;
2119 }
2120
2121 /*
2122 * For a XR16C850, we need to set the trigger levels
2123 */
2124 if (port->type == PORT_16850) {
2125 unsigned char fctr;
2126
2127 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
2128
2129 fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2130 serial_port_out(port, UART_FCTR,
2131 fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2132 serial_port_out(port, UART_TRG, UART_TRG_96);
2133 serial_port_out(port, UART_FCTR,
2134 fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2135 serial_port_out(port, UART_TRG, UART_TRG_96);
2136
2137 serial_port_out(port, UART_LCR, 0);
2138 }
2139
2140 if (port->irq) {
2141 unsigned char iir1;
2142 /*
2143 * Test for UARTs that do not reassert THRE when the
2144 * transmitter is idle and the interrupt has already
2145 * been cleared. Real 16550s should always reassert
2146 * this interrupt whenever the transmitter is idle and
2147 * the interrupt is enabled. Delays are necessary to
2148 * allow register changes to become visible.
2149 */
2150 spin_lock_irqsave(&port->lock, flags);
2151 if (up->port.irqflags & IRQF_SHARED)
2152 disable_irq_nosync(port->irq);
2153
2154 wait_for_xmitr(up, UART_LSR_THRE);
2155 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2156 udelay(1); /* allow THRE to set */
2157 iir1 = serial_port_in(port, UART_IIR);
2158 serial_port_out(port, UART_IER, 0);
2159 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2160 udelay(1); /* allow a working UART time to re-assert THRE */
2161 iir = serial_port_in(port, UART_IIR);
2162 serial_port_out(port, UART_IER, 0);
2163
2164 if (port->irqflags & IRQF_SHARED)
2165 enable_irq(port->irq);
2166 spin_unlock_irqrestore(&port->lock, flags);
2167
2168 /*
2169 * If the interrupt is not reasserted, or we otherwise
2170 * don't trust the iir, setup a timer to kick the UART
2171 * on a regular basis.
2172 */
2173 if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
2174 up->port.flags & UPF_BUG_THRE) {
2175 up->bugs |= UART_BUG_THRE;
2176 }
2177 }
2178
2179 retval = up->ops->setup_irq(up);
2180 if (retval)
2181 goto out;
2182
2183 /*
2184 * Now, initialize the UART
2185 */
2186 serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
2187
2188 spin_lock_irqsave(&port->lock, flags);
2189 if (up->port.flags & UPF_FOURPORT) {
2190 if (!up->port.irq)
2191 up->port.mctrl |= TIOCM_OUT1;
2192 } else
2193 /*
2194 * Most PC uarts need OUT2 raised to enable interrupts.
2195 */
2196 if (port->irq)
2197 up->port.mctrl |= TIOCM_OUT2;
2198
2199 serial8250_set_mctrl(port, port->mctrl);
2200
2201 /* Serial over Lan (SoL) hack:
2202 Intel 8257x Gigabit ethernet chips have a
2203 16550 emulation, to be used for Serial Over Lan.
2204 Those chips take a longer time than a normal
2205 serial device to signalize that a transmission
2206 data was queued. Due to that, the above test generally
2207 fails. One solution would be to delay the reading of
2208 iir. However, this is not reliable, since the timeout
2209 is variable. So, let's just don't test if we receive
2210 TX irq. This way, we'll never enable UART_BUG_TXEN.
2211 */
2212 if (up->port.flags & UPF_NO_TXEN_TEST)
2213 goto dont_test_tx_en;
2214
2215 /*
2216 * Do a quick test to see if we receive an
2217 * interrupt when we enable the TX irq.
2218 */
2219 serial_port_out(port, UART_IER, UART_IER_THRI);
2220 lsr = serial_port_in(port, UART_LSR);
2221 iir = serial_port_in(port, UART_IIR);
2222 serial_port_out(port, UART_IER, 0);
2223
2224 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2225 if (!(up->bugs & UART_BUG_TXEN)) {
2226 up->bugs |= UART_BUG_TXEN;
2227 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2228 serial_index(port));
2229 }
2230 } else {
2231 up->bugs &= ~UART_BUG_TXEN;
2232 }
2233
2234dont_test_tx_en:
2235 spin_unlock_irqrestore(&port->lock, flags);
2236
2237 /*
2238 * Clear the interrupt registers again for luck, and clear the
2239 * saved flags to avoid getting false values from polling
2240 * routines or the previous session.
2241 */
2242 serial_port_in(port, UART_LSR);
2243 serial_port_in(port, UART_RX);
2244 serial_port_in(port, UART_IIR);
2245 serial_port_in(port, UART_MSR);
2246 up->lsr_saved_flags = 0;
2247 up->msr_saved_flags = 0;
2248
2249 /*
2250 * Request DMA channels for both RX and TX.
2251 */
2252 if (up->dma) {
2253 retval = serial8250_request_dma(up);
2254 if (retval) {
2255 pr_warn_ratelimited("ttyS%d - failed to request DMA\n",
2256 serial_index(port));
2257 up->dma = NULL;
2258 }
2259 }
2260
2261 /*
Peter Hurleyee3ad902015-07-12 21:11:31 -04002262 * Set the IER shadow for rx interrupts but defer actual interrupt
2263 * enable until after the FIFOs are enabled; otherwise, an already-
2264 * active sender can swamp the interrupt handler with "too much work".
Peter Hurleyb6830f62015-06-27 09:19:00 -04002265 */
2266 up->ier = UART_IER_RLSI | UART_IER_RDI;
Peter Hurleyb6830f62015-06-27 09:19:00 -04002267
2268 if (port->flags & UPF_FOURPORT) {
2269 unsigned int icp;
2270 /*
2271 * Enable interrupts on the AST Fourport board
2272 */
2273 icp = (port->iobase & 0xfe0) | 0x01f;
2274 outb_p(0x80, icp);
2275 inb_p(icp);
2276 }
2277 retval = 0;
2278out:
2279 serial8250_rpm_put(up);
2280 return retval;
2281}
2282EXPORT_SYMBOL_GPL(serial8250_do_startup);
2283
2284static int serial8250_startup(struct uart_port *port)
2285{
2286 if (port->startup)
2287 return port->startup(port);
2288 return serial8250_do_startup(port);
2289}
2290
2291void serial8250_do_shutdown(struct uart_port *port)
2292{
2293 struct uart_8250_port *up = up_to_u8250p(port);
2294 unsigned long flags;
2295
2296 serial8250_rpm_get(up);
2297 /*
2298 * Disable interrupts from this port
2299 */
Peter Hurley611e0d82016-01-10 14:39:35 -08002300 spin_lock_irqsave(&port->lock, flags);
Peter Hurleyb6830f62015-06-27 09:19:00 -04002301 up->ier = 0;
2302 serial_port_out(port, UART_IER, 0);
Peter Hurley611e0d82016-01-10 14:39:35 -08002303 spin_unlock_irqrestore(&port->lock, flags);
2304
2305 synchronize_irq(port->irq);
Peter Hurleyb6830f62015-06-27 09:19:00 -04002306
2307 if (up->dma)
2308 serial8250_release_dma(up);
2309
2310 spin_lock_irqsave(&port->lock, flags);
2311 if (port->flags & UPF_FOURPORT) {
2312 /* reset interrupts on the AST Fourport board */
2313 inb((port->iobase & 0xfe0) | 0x1f);
2314 port->mctrl |= TIOCM_OUT1;
2315 } else
2316 port->mctrl &= ~TIOCM_OUT2;
2317
2318 serial8250_set_mctrl(port, port->mctrl);
2319 spin_unlock_irqrestore(&port->lock, flags);
2320
2321 /*
2322 * Disable break condition and FIFOs
2323 */
2324 serial_port_out(port, UART_LCR,
2325 serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
2326 serial8250_clear_fifos(up);
2327
2328#ifdef CONFIG_SERIAL_8250_RSA
2329 /*
2330 * Reset the RSA board back to 115kbps compat mode.
2331 */
2332 disable_rsa(up);
2333#endif
2334
2335 /*
2336 * Read data port to reset things, and then unlink from
2337 * the IRQ chain.
2338 */
2339 serial_port_in(port, UART_RX);
2340 serial8250_rpm_put(up);
2341
2342 up->ops->release_irq(up);
2343}
2344EXPORT_SYMBOL_GPL(serial8250_do_shutdown);
2345
2346static void serial8250_shutdown(struct uart_port *port)
2347{
2348 if (port->shutdown)
2349 port->shutdown(port);
2350 else
2351 serial8250_do_shutdown(port);
2352}
2353
2354/*
2355 * XR17V35x UARTs have an extra fractional divisor register (DLD)
2356 * Calculate divisor with extra 4-bit fractional portion
2357 */
2358static unsigned int xr17v35x_get_divisor(struct uart_8250_port *up,
2359 unsigned int baud,
2360 unsigned int *frac)
2361{
2362 struct uart_port *port = &up->port;
2363 unsigned int quot_16;
2364
2365 quot_16 = DIV_ROUND_CLOSEST(port->uartclk, baud);
2366 *frac = quot_16 & 0x0f;
2367
2368 return quot_16 >> 4;
2369}
2370
2371static unsigned int serial8250_get_divisor(struct uart_8250_port *up,
2372 unsigned int baud,
2373 unsigned int *frac)
2374{
2375 struct uart_port *port = &up->port;
2376 unsigned int quot;
2377
2378 /*
2379 * Handle magic divisors for baud rates above baud_base on
2380 * SMSC SuperIO chips.
2381 *
2382 */
2383 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2384 baud == (port->uartclk/4))
2385 quot = 0x8001;
2386 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2387 baud == (port->uartclk/8))
2388 quot = 0x8002;
2389 else if (up->port.type == PORT_XR17V35X)
2390 quot = xr17v35x_get_divisor(up, baud, frac);
2391 else
2392 quot = uart_get_divisor(port, baud);
2393
2394 /*
2395 * Oxford Semi 952 rev B workaround
2396 */
2397 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2398 quot++;
2399
2400 return quot;
2401}
2402
2403static unsigned char serial8250_compute_lcr(struct uart_8250_port *up,
2404 tcflag_t c_cflag)
2405{
2406 unsigned char cval;
2407
2408 switch (c_cflag & CSIZE) {
2409 case CS5:
2410 cval = UART_LCR_WLEN5;
2411 break;
2412 case CS6:
2413 cval = UART_LCR_WLEN6;
2414 break;
2415 case CS7:
2416 cval = UART_LCR_WLEN7;
2417 break;
2418 default:
2419 case CS8:
2420 cval = UART_LCR_WLEN8;
2421 break;
2422 }
2423
2424 if (c_cflag & CSTOPB)
2425 cval |= UART_LCR_STOP;
2426 if (c_cflag & PARENB) {
2427 cval |= UART_LCR_PARITY;
2428 if (up->bugs & UART_BUG_PARITY)
2429 up->fifo_bug = true;
2430 }
2431 if (!(c_cflag & PARODD))
2432 cval |= UART_LCR_EPAR;
2433#ifdef CMSPAR
2434 if (c_cflag & CMSPAR)
2435 cval |= UART_LCR_SPAR;
2436#endif
2437
2438 return cval;
2439}
2440
2441static void serial8250_set_divisor(struct uart_port *port, unsigned int baud,
2442 unsigned int quot, unsigned int quot_frac)
2443{
2444 struct uart_8250_port *up = up_to_u8250p(port);
2445
2446 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2447 if (is_omap1510_8250(up)) {
2448 if (baud == 115200) {
2449 quot = 1;
2450 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
2451 } else
2452 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
2453 }
2454
2455 /*
2456 * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
2457 * otherwise just set DLAB
2458 */
2459 if (up->capabilities & UART_NATSEMI)
2460 serial_port_out(port, UART_LCR, 0xe0);
2461 else
2462 serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
2463
2464 serial_dl_write(up, quot);
2465
2466 /* XR17V35x UARTs have an extra fractional divisor register (DLD) */
2467 if (up->port.type == PORT_XR17V35X)
2468 serial_port_out(port, 0x2, quot_frac);
2469}
2470
James Hogan4f56f3f2015-09-25 15:36:10 -04002471static unsigned int
2472serial8250_get_baud_rate(struct uart_port *port, struct ktermios *termios,
2473 struct ktermios *old)
2474{
2475 unsigned int tolerance = port->uartclk / 100;
2476
2477 /*
2478 * Ask the core to calculate the divisor for us.
2479 * Allow 1% tolerance at the upper limit so uart clks marginally
2480 * slower than nominal still match standard baud rates without
2481 * causing transmission errors.
2482 */
2483 return uart_get_baud_rate(port, termios, old,
2484 port->uartclk / 16 / 0xffff,
2485 (port->uartclk + tolerance) / 16);
2486}
2487
Peter Hurleyb6830f62015-06-27 09:19:00 -04002488void
2489serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
Anton Wuerfel6d7c1572016-01-14 16:08:11 +01002490 struct ktermios *old)
Peter Hurleyb6830f62015-06-27 09:19:00 -04002491{
2492 struct uart_8250_port *up = up_to_u8250p(port);
2493 unsigned char cval;
2494 unsigned long flags;
2495 unsigned int baud, quot, frac = 0;
2496
2497 cval = serial8250_compute_lcr(up, termios->c_cflag);
2498
James Hogan4f56f3f2015-09-25 15:36:10 -04002499 baud = serial8250_get_baud_rate(port, termios, old);
Peter Hurleyb6830f62015-06-27 09:19:00 -04002500 quot = serial8250_get_divisor(up, baud, &frac);
2501
2502 /*
2503 * Ok, we're now changing the port state. Do it with
2504 * interrupts disabled.
2505 */
2506 serial8250_rpm_get(up);
2507 spin_lock_irqsave(&port->lock, flags);
2508
2509 up->lcr = cval; /* Save computed LCR */
2510
2511 if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
2512 /* NOTE: If fifo_bug is not set, a user can set RX_trigger. */
2513 if ((baud < 2400 && !up->dma) || up->fifo_bug) {
2514 up->fcr &= ~UART_FCR_TRIGGER_MASK;
2515 up->fcr |= UART_FCR_TRIGGER_1;
2516 }
2517 }
2518
2519 /*
2520 * MCR-based auto flow control. When AFE is enabled, RTS will be
2521 * deasserted when the receive FIFO contains more characters than
2522 * the trigger, or the MCR RTS bit is cleared. In the case where
2523 * the remote UART is not using CTS auto flow control, we must
2524 * have sufficient FIFO entries for the latency of the remote
2525 * UART to respond. IOW, at least 32 bytes of FIFO.
2526 */
2527 if (up->capabilities & UART_CAP_AFE && port->fifosize >= 32) {
2528 up->mcr &= ~UART_MCR_AFE;
2529 if (termios->c_cflag & CRTSCTS)
2530 up->mcr |= UART_MCR_AFE;
2531 }
2532
2533 /*
2534 * Update the per-port timeout.
2535 */
2536 uart_update_timeout(port, termios->c_cflag, baud);
2537
2538 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2539 if (termios->c_iflag & INPCK)
2540 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2541 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2542 port->read_status_mask |= UART_LSR_BI;
2543
2544 /*
2545 * Characteres to ignore
2546 */
2547 port->ignore_status_mask = 0;
2548 if (termios->c_iflag & IGNPAR)
2549 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2550 if (termios->c_iflag & IGNBRK) {
2551 port->ignore_status_mask |= UART_LSR_BI;
2552 /*
2553 * If we're ignoring parity and break indicators,
2554 * ignore overruns too (for real raw support).
2555 */
2556 if (termios->c_iflag & IGNPAR)
2557 port->ignore_status_mask |= UART_LSR_OE;
2558 }
2559
2560 /*
2561 * ignore all characters if CREAD is not set
2562 */
2563 if ((termios->c_cflag & CREAD) == 0)
2564 port->ignore_status_mask |= UART_LSR_DR;
2565
2566 /*
2567 * CTS flow control flag and modem status interrupts
2568 */
2569 up->ier &= ~UART_IER_MSI;
2570 if (!(up->bugs & UART_BUG_NOMSR) &&
2571 UART_ENABLE_MS(&up->port, termios->c_cflag))
2572 up->ier |= UART_IER_MSI;
2573 if (up->capabilities & UART_CAP_UUE)
2574 up->ier |= UART_IER_UUE;
2575 if (up->capabilities & UART_CAP_RTOIE)
2576 up->ier |= UART_IER_RTOIE;
2577
2578 serial_port_out(port, UART_IER, up->ier);
2579
2580 if (up->capabilities & UART_CAP_EFR) {
2581 unsigned char efr = 0;
2582 /*
2583 * TI16C752/Startech hardware flow control. FIXME:
2584 * - TI16C752 requires control thresholds to be set.
2585 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2586 */
2587 if (termios->c_cflag & CRTSCTS)
2588 efr |= UART_EFR_CTS;
2589
2590 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2591 if (port->flags & UPF_EXAR_EFR)
2592 serial_port_out(port, UART_XR_EFR, efr);
2593 else
2594 serial_port_out(port, UART_EFR, efr);
2595 }
2596
2597 serial8250_set_divisor(port, baud, quot, frac);
2598
2599 /*
2600 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2601 * is written without DLAB set, this mode will be disabled.
2602 */
2603 if (port->type == PORT_16750)
2604 serial_port_out(port, UART_FCR, up->fcr);
2605
2606 serial_port_out(port, UART_LCR, up->lcr); /* reset DLAB */
2607 if (port->type != PORT_16750) {
2608 /* emulated UARTs (Lucent Venus 167x) need two steps */
2609 if (up->fcr & UART_FCR_ENABLE_FIFO)
2610 serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
2611 serial_port_out(port, UART_FCR, up->fcr); /* set fcr */
2612 }
2613 serial8250_set_mctrl(port, port->mctrl);
2614 spin_unlock_irqrestore(&port->lock, flags);
2615 serial8250_rpm_put(up);
2616
2617 /* Don't rewrite B0 */
2618 if (tty_termios_baud_rate(termios))
2619 tty_termios_encode_baud_rate(termios, baud, baud);
2620}
2621EXPORT_SYMBOL(serial8250_do_set_termios);
2622
2623static void
2624serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2625 struct ktermios *old)
2626{
2627 if (port->set_termios)
2628 port->set_termios(port, termios, old);
2629 else
2630 serial8250_do_set_termios(port, termios, old);
2631}
2632
2633static void
2634serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios)
2635{
2636 if (termios->c_line == N_PPS) {
2637 port->flags |= UPF_HARDPPS_CD;
2638 spin_lock_irq(&port->lock);
2639 serial8250_enable_ms(port);
2640 spin_unlock_irq(&port->lock);
2641 } else {
2642 port->flags &= ~UPF_HARDPPS_CD;
2643 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2644 spin_lock_irq(&port->lock);
2645 serial8250_disable_ms(port);
2646 spin_unlock_irq(&port->lock);
2647 }
2648 }
2649}
2650
2651
2652void serial8250_do_pm(struct uart_port *port, unsigned int state,
2653 unsigned int oldstate)
2654{
2655 struct uart_8250_port *p = up_to_u8250p(port);
2656
2657 serial8250_set_sleep(p, state != 0);
2658}
2659EXPORT_SYMBOL(serial8250_do_pm);
2660
2661static void
2662serial8250_pm(struct uart_port *port, unsigned int state,
2663 unsigned int oldstate)
2664{
2665 if (port->pm)
2666 port->pm(port, state, oldstate);
2667 else
2668 serial8250_do_pm(port, state, oldstate);
2669}
2670
2671static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2672{
2673 if (pt->port.mapsize)
2674 return pt->port.mapsize;
2675 if (pt->port.iotype == UPIO_AU) {
2676 if (pt->port.type == PORT_RT2880)
2677 return 0x100;
2678 return 0x1000;
2679 }
2680 if (is_omap1_8250(pt))
2681 return 0x16 << pt->port.regshift;
2682
2683 return 8 << pt->port.regshift;
2684}
2685
2686/*
2687 * Resource handling.
2688 */
2689static int serial8250_request_std_resource(struct uart_8250_port *up)
2690{
2691 unsigned int size = serial8250_port_size(up);
2692 struct uart_port *port = &up->port;
2693 int ret = 0;
2694
2695 switch (port->iotype) {
2696 case UPIO_AU:
2697 case UPIO_TSI:
2698 case UPIO_MEM32:
2699 case UPIO_MEM32BE:
Masahiro Yamadabd94c402015-10-28 12:46:05 +09002700 case UPIO_MEM16:
Peter Hurleyb6830f62015-06-27 09:19:00 -04002701 case UPIO_MEM:
2702 if (!port->mapbase)
2703 break;
2704
2705 if (!request_mem_region(port->mapbase, size, "serial")) {
2706 ret = -EBUSY;
2707 break;
2708 }
2709
2710 if (port->flags & UPF_IOREMAP) {
2711 port->membase = ioremap_nocache(port->mapbase, size);
2712 if (!port->membase) {
2713 release_mem_region(port->mapbase, size);
2714 ret = -ENOMEM;
2715 }
2716 }
2717 break;
2718
2719 case UPIO_HUB6:
2720 case UPIO_PORT:
2721 if (!request_region(port->iobase, size, "serial"))
2722 ret = -EBUSY;
2723 break;
2724 }
2725 return ret;
2726}
2727
2728static void serial8250_release_std_resource(struct uart_8250_port *up)
2729{
2730 unsigned int size = serial8250_port_size(up);
2731 struct uart_port *port = &up->port;
2732
2733 switch (port->iotype) {
2734 case UPIO_AU:
2735 case UPIO_TSI:
2736 case UPIO_MEM32:
2737 case UPIO_MEM32BE:
Masahiro Yamadabd94c402015-10-28 12:46:05 +09002738 case UPIO_MEM16:
Peter Hurleyb6830f62015-06-27 09:19:00 -04002739 case UPIO_MEM:
2740 if (!port->mapbase)
2741 break;
2742
2743 if (port->flags & UPF_IOREMAP) {
2744 iounmap(port->membase);
2745 port->membase = NULL;
2746 }
2747
2748 release_mem_region(port->mapbase, size);
2749 break;
2750
2751 case UPIO_HUB6:
2752 case UPIO_PORT:
2753 release_region(port->iobase, size);
2754 break;
2755 }
2756}
2757
2758static void serial8250_release_port(struct uart_port *port)
2759{
2760 struct uart_8250_port *up = up_to_u8250p(port);
2761
2762 serial8250_release_std_resource(up);
2763}
2764
2765static int serial8250_request_port(struct uart_port *port)
2766{
2767 struct uart_8250_port *up = up_to_u8250p(port);
Peter Hurleyb6830f62015-06-27 09:19:00 -04002768
Maciej S. Szmigieroe4fda3a2015-09-27 16:25:56 +02002769 return serial8250_request_std_resource(up);
Peter Hurleyb6830f62015-06-27 09:19:00 -04002770}
2771
2772static int fcr_get_rxtrig_bytes(struct uart_8250_port *up)
2773{
2774 const struct serial8250_config *conf_type = &uart_config[up->port.type];
2775 unsigned char bytes;
2776
2777 bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)];
2778
2779 return bytes ? bytes : -EOPNOTSUPP;
2780}
2781
2782static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes)
2783{
2784 const struct serial8250_config *conf_type = &uart_config[up->port.type];
2785 int i;
2786
2787 if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)])
2788 return -EOPNOTSUPP;
2789
2790 for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) {
2791 if (bytes < conf_type->rxtrig_bytes[i])
2792 /* Use the nearest lower value */
2793 return (--i) << UART_FCR_R_TRIG_SHIFT;
2794 }
2795
2796 return UART_FCR_R_TRIG_11;
2797}
2798
2799static int do_get_rxtrig(struct tty_port *port)
2800{
2801 struct uart_state *state = container_of(port, struct uart_state, port);
2802 struct uart_port *uport = state->uart_port;
2803 struct uart_8250_port *up =
2804 container_of(uport, struct uart_8250_port, port);
2805
2806 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
2807 return -EINVAL;
2808
2809 return fcr_get_rxtrig_bytes(up);
2810}
2811
2812static int do_serial8250_get_rxtrig(struct tty_port *port)
2813{
2814 int rxtrig_bytes;
2815
2816 mutex_lock(&port->mutex);
2817 rxtrig_bytes = do_get_rxtrig(port);
2818 mutex_unlock(&port->mutex);
2819
2820 return rxtrig_bytes;
2821}
2822
2823static ssize_t serial8250_get_attr_rx_trig_bytes(struct device *dev,
2824 struct device_attribute *attr, char *buf)
2825{
2826 struct tty_port *port = dev_get_drvdata(dev);
2827 int rxtrig_bytes;
2828
2829 rxtrig_bytes = do_serial8250_get_rxtrig(port);
2830 if (rxtrig_bytes < 0)
2831 return rxtrig_bytes;
2832
2833 return snprintf(buf, PAGE_SIZE, "%d\n", rxtrig_bytes);
2834}
2835
2836static int do_set_rxtrig(struct tty_port *port, unsigned char bytes)
2837{
2838 struct uart_state *state = container_of(port, struct uart_state, port);
2839 struct uart_port *uport = state->uart_port;
2840 struct uart_8250_port *up =
2841 container_of(uport, struct uart_8250_port, port);
2842 int rxtrig;
2843
2844 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1 ||
2845 up->fifo_bug)
2846 return -EINVAL;
2847
2848 rxtrig = bytes_to_fcr_rxtrig(up, bytes);
2849 if (rxtrig < 0)
2850 return rxtrig;
2851
2852 serial8250_clear_fifos(up);
2853 up->fcr &= ~UART_FCR_TRIGGER_MASK;
2854 up->fcr |= (unsigned char)rxtrig;
2855 serial_out(up, UART_FCR, up->fcr);
2856 return 0;
2857}
2858
2859static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes)
2860{
2861 int ret;
2862
2863 mutex_lock(&port->mutex);
2864 ret = do_set_rxtrig(port, bytes);
2865 mutex_unlock(&port->mutex);
2866
2867 return ret;
2868}
2869
2870static ssize_t serial8250_set_attr_rx_trig_bytes(struct device *dev,
2871 struct device_attribute *attr, const char *buf, size_t count)
2872{
2873 struct tty_port *port = dev_get_drvdata(dev);
2874 unsigned char bytes;
2875 int ret;
2876
2877 if (!count)
2878 return -EINVAL;
2879
2880 ret = kstrtou8(buf, 10, &bytes);
2881 if (ret < 0)
2882 return ret;
2883
2884 ret = do_serial8250_set_rxtrig(port, bytes);
2885 if (ret < 0)
2886 return ret;
2887
2888 return count;
2889}
2890
2891static DEVICE_ATTR(rx_trig_bytes, S_IRUSR | S_IWUSR | S_IRGRP,
2892 serial8250_get_attr_rx_trig_bytes,
2893 serial8250_set_attr_rx_trig_bytes);
2894
2895static struct attribute *serial8250_dev_attrs[] = {
2896 &dev_attr_rx_trig_bytes.attr,
2897 NULL,
2898 };
2899
2900static struct attribute_group serial8250_dev_attr_group = {
2901 .attrs = serial8250_dev_attrs,
2902 };
2903
2904static void register_dev_spec_attr_grp(struct uart_8250_port *up)
2905{
2906 const struct serial8250_config *conf_type = &uart_config[up->port.type];
2907
2908 if (conf_type->rxtrig_bytes[0])
2909 up->port.attr_group = &serial8250_dev_attr_group;
2910}
2911
2912static void serial8250_config_port(struct uart_port *port, int flags)
2913{
2914 struct uart_8250_port *up = up_to_u8250p(port);
2915 int ret;
2916
Peter Hurleyb6830f62015-06-27 09:19:00 -04002917 /*
2918 * Find the region that we can probe for. This in turn
2919 * tells us whether we can probe for the type of port.
2920 */
2921 ret = serial8250_request_std_resource(up);
2922 if (ret < 0)
2923 return;
2924
2925 if (port->iotype != up->cur_iotype)
2926 set_io_from_upio(port);
2927
2928 if (flags & UART_CONFIG_TYPE)
2929 autoconfig(up);
2930
2931 /* if access method is AU, it is a 16550 with a quirk */
2932 if (port->type == PORT_16550A && port->iotype == UPIO_AU)
2933 up->bugs |= UART_BUG_NOMSR;
2934
2935 /* HW bugs may trigger IRQ while IIR == NO_INT */
2936 if (port->type == PORT_TEGRA)
2937 up->bugs |= UART_BUG_NOMSR;
2938
2939 if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2940 autoconfig_irq(up);
2941
2942 if (port->type == PORT_UNKNOWN)
2943 serial8250_release_std_resource(up);
2944
2945 /* Fixme: probably not the best place for this */
2946 if ((port->type == PORT_XR17V35X) ||
2947 (port->type == PORT_XR17D15X))
2948 port->handle_irq = exar_handle_irq;
2949
2950 register_dev_spec_attr_grp(up);
2951 up->fcr = uart_config[up->port.type].fcr;
2952}
2953
2954static int
2955serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2956{
2957 if (ser->irq >= nr_irqs || ser->irq < 0 ||
2958 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2959 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2960 ser->type == PORT_STARTECH)
2961 return -EINVAL;
2962 return 0;
2963}
2964
Peter Hurleycee10c82016-01-10 14:39:36 -08002965static const char *serial8250_type(struct uart_port *port)
Peter Hurleyb6830f62015-06-27 09:19:00 -04002966{
2967 int type = port->type;
2968
2969 if (type >= ARRAY_SIZE(uart_config))
2970 type = 0;
2971 return uart_config[type].name;
2972}
2973
2974static const struct uart_ops serial8250_pops = {
2975 .tx_empty = serial8250_tx_empty,
2976 .set_mctrl = serial8250_set_mctrl,
2977 .get_mctrl = serial8250_get_mctrl,
2978 .stop_tx = serial8250_stop_tx,
2979 .start_tx = serial8250_start_tx,
2980 .throttle = serial8250_throttle,
2981 .unthrottle = serial8250_unthrottle,
2982 .stop_rx = serial8250_stop_rx,
2983 .enable_ms = serial8250_enable_ms,
2984 .break_ctl = serial8250_break_ctl,
2985 .startup = serial8250_startup,
2986 .shutdown = serial8250_shutdown,
2987 .set_termios = serial8250_set_termios,
2988 .set_ldisc = serial8250_set_ldisc,
2989 .pm = serial8250_pm,
2990 .type = serial8250_type,
2991 .release_port = serial8250_release_port,
2992 .request_port = serial8250_request_port,
2993 .config_port = serial8250_config_port,
2994 .verify_port = serial8250_verify_port,
2995#ifdef CONFIG_CONSOLE_POLL
2996 .poll_get_char = serial8250_get_poll_char,
2997 .poll_put_char = serial8250_put_poll_char,
2998#endif
2999};
3000
3001void serial8250_init_port(struct uart_8250_port *up)
3002{
3003 struct uart_port *port = &up->port;
3004
3005 spin_lock_init(&port->lock);
3006 port->ops = &serial8250_pops;
3007
3008 up->cur_iotype = 0xFF;
3009}
3010EXPORT_SYMBOL_GPL(serial8250_init_port);
3011
3012void serial8250_set_defaults(struct uart_8250_port *up)
3013{
3014 struct uart_port *port = &up->port;
3015
3016 if (up->port.flags & UPF_FIXED_TYPE) {
3017 unsigned int type = up->port.type;
3018
3019 if (!up->port.fifosize)
3020 up->port.fifosize = uart_config[type].fifo_size;
3021 if (!up->tx_loadsz)
3022 up->tx_loadsz = uart_config[type].tx_loadsz;
3023 if (!up->capabilities)
3024 up->capabilities = uart_config[type].flags;
3025 }
3026
3027 set_io_from_upio(port);
3028
3029 /* default dma handlers */
3030 if (up->dma) {
3031 if (!up->dma->tx_dma)
3032 up->dma->tx_dma = serial8250_tx_dma;
3033 if (!up->dma->rx_dma)
3034 up->dma->rx_dma = serial8250_rx_dma;
3035 }
3036}
3037EXPORT_SYMBOL_GPL(serial8250_set_defaults);
3038
3039#ifdef CONFIG_SERIAL_8250_CONSOLE
3040
3041static void serial8250_console_putchar(struct uart_port *port, int ch)
3042{
3043 struct uart_8250_port *up = up_to_u8250p(port);
3044
3045 wait_for_xmitr(up, UART_LSR_THRE);
3046 serial_port_out(port, UART_TX, ch);
3047}
3048
3049/*
Peter Hurley10791232015-09-25 15:36:11 -04003050 * Restore serial console when h/w power-off detected
3051 */
3052static void serial8250_console_restore(struct uart_8250_port *up)
3053{
3054 struct uart_port *port = &up->port;
3055 struct ktermios termios;
3056 unsigned int baud, quot, frac = 0;
3057
3058 termios.c_cflag = port->cons->cflag;
3059 if (port->state->port.tty && termios.c_cflag == 0)
3060 termios.c_cflag = port->state->port.tty->termios.c_cflag;
3061
3062 baud = serial8250_get_baud_rate(port, &termios, NULL);
3063 quot = serial8250_get_divisor(up, baud, &frac);
3064
3065 serial8250_set_divisor(port, baud, quot, frac);
3066 serial_port_out(port, UART_LCR, up->lcr);
3067 serial_port_out(port, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
3068}
3069
3070/*
Peter Hurleyb6830f62015-06-27 09:19:00 -04003071 * Print a string to the serial port trying not to disturb
3072 * any possible real use of the port...
3073 *
3074 * The console_lock must be held when we get here.
3075 */
3076void serial8250_console_write(struct uart_8250_port *up, const char *s,
3077 unsigned int count)
3078{
3079 struct uart_port *port = &up->port;
3080 unsigned long flags;
3081 unsigned int ier;
3082 int locked = 1;
3083
3084 touch_nmi_watchdog();
3085
3086 serial8250_rpm_get(up);
3087
3088 if (port->sysrq)
3089 locked = 0;
3090 else if (oops_in_progress)
3091 locked = spin_trylock_irqsave(&port->lock, flags);
3092 else
3093 spin_lock_irqsave(&port->lock, flags);
3094
3095 /*
3096 * First save the IER then disable the interrupts
3097 */
3098 ier = serial_port_in(port, UART_IER);
3099
3100 if (up->capabilities & UART_CAP_UUE)
3101 serial_port_out(port, UART_IER, UART_IER_UUE);
3102 else
3103 serial_port_out(port, UART_IER, 0);
3104
3105 /* check scratch reg to see if port powered off during system sleep */
3106 if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) {
Peter Hurley10791232015-09-25 15:36:11 -04003107 serial8250_console_restore(up);
Peter Hurleyb6830f62015-06-27 09:19:00 -04003108 up->canary = 0;
3109 }
3110
3111 uart_console_write(port, s, count, serial8250_console_putchar);
3112
3113 /*
3114 * Finally, wait for transmitter to become empty
3115 * and restore the IER
3116 */
3117 wait_for_xmitr(up, BOTH_EMPTY);
3118 serial_port_out(port, UART_IER, ier);
3119
3120 /*
3121 * The receive handling will happen properly because the
3122 * receive ready bit will still be set; it is not cleared
3123 * on read. However, modem control will not, we must
3124 * call it if we have saved something in the saved flags
3125 * while processing with interrupts off.
3126 */
3127 if (up->msr_saved_flags)
3128 serial8250_modem_status(up);
3129
3130 if (locked)
3131 spin_unlock_irqrestore(&port->lock, flags);
3132 serial8250_rpm_put(up);
3133}
3134
3135static unsigned int probe_baud(struct uart_port *port)
3136{
3137 unsigned char lcr, dll, dlm;
3138 unsigned int quot;
3139
3140 lcr = serial_port_in(port, UART_LCR);
3141 serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB);
3142 dll = serial_port_in(port, UART_DLL);
3143 dlm = serial_port_in(port, UART_DLM);
3144 serial_port_out(port, UART_LCR, lcr);
3145
3146 quot = (dlm << 8) | dll;
3147 return (port->uartclk / 16) / quot;
3148}
3149
3150int serial8250_console_setup(struct uart_port *port, char *options, bool probe)
3151{
3152 int baud = 9600;
3153 int bits = 8;
3154 int parity = 'n';
3155 int flow = 'n';
3156
3157 if (!port->iobase && !port->membase)
3158 return -ENODEV;
3159
3160 if (options)
3161 uart_parse_options(options, &baud, &parity, &bits, &flow);
3162 else if (probe)
3163 baud = probe_baud(port);
3164
3165 return uart_set_options(port, port->cons, baud, parity, bits, flow);
3166}
3167
3168#endif /* CONFIG_SERIAL_8250_CONSOLE */
Jonathan McDowellf7a76512015-09-21 21:23:47 +01003169
3170MODULE_LICENSE("GPL");