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