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