1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Base port operations for 8250/16550-type serial ports
4 *
5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 * Split from 8250_core.c, Copyright (C) 2001 Russell King.
7 *
8 * A note about mapbase / membase
9 *
10 * mapbase is the physical address of the IO port.
11 * membase is an 'ioremapped' cookie.
12 */
13
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/ioport.h>
17 #include <linux/init.h>
18 #include <linux/irq.h>
19 #include <linux/console.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/lockdep.h>
22 #include <linux/sysrq.h>
23 #include <linux/delay.h>
24 #include <linux/platform_device.h>
25 #include <linux/tty.h>
26 #include <linux/ratelimit.h>
27 #include <linux/tty_flip.h>
28 #include <linux/serial.h>
29 #include <linux/serial_8250.h>
30 #include <linux/nmi.h>
31 #include <linux/mutex.h>
32 #include <linux/slab.h>
33 #include <linux/uaccess.h>
34 #include <linux/pm_runtime.h>
35 #include <linux/ktime.h>
36
37 #include <asm/io.h>
38 #include <asm/irq.h>
39
40 #include "8250.h"
41
42 /*
43 * Here we define the default xmit fifo size used for each type of UART.
44 */
45 static const struct serial8250_config uart_config[] = {
46 [PORT_UNKNOWN] = {
47 .name = "unknown",
48 .fifo_size = 1,
49 .tx_loadsz = 1,
50 },
51 [PORT_8250] = {
52 .name = "8250",
53 .fifo_size = 1,
54 .tx_loadsz = 1,
55 },
56 [PORT_16450] = {
57 .name = "16450",
58 .fifo_size = 1,
59 .tx_loadsz = 1,
60 },
61 [PORT_16550] = {
62 .name = "16550",
63 .fifo_size = 1,
64 .tx_loadsz = 1,
65 },
66 [PORT_16550A] = {
67 .name = "16550A",
68 .fifo_size = 16,
69 .tx_loadsz = 16,
70 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
71 .rxtrig_bytes = {1, 4, 8, 14},
72 .flags = UART_CAP_FIFO,
73 },
74 [PORT_CIRRUS] = {
75 .name = "Cirrus",
76 .fifo_size = 1,
77 .tx_loadsz = 1,
78 },
79 [PORT_16650] = {
80 .name = "ST16650",
81 .fifo_size = 1,
82 .tx_loadsz = 1,
83 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
84 },
85 [PORT_16650V2] = {
86 .name = "ST16650V2",
87 .fifo_size = 32,
88 .tx_loadsz = 16,
89 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
90 UART_FCR_T_TRIG_00,
91 .rxtrig_bytes = {8, 16, 24, 28},
92 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
93 },
94 [PORT_16750] = {
95 .name = "TI16750",
96 .fifo_size = 64,
97 .tx_loadsz = 64,
98 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
99 UART_FCR7_64BYTE,
100 .rxtrig_bytes = {1, 16, 32, 56},
101 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
102 },
103 [PORT_STARTECH] = {
104 .name = "Startech",
105 .fifo_size = 1,
106 .tx_loadsz = 1,
107 },
108 [PORT_16C950] = {
109 .name = "16C950/954",
110 .fifo_size = 128,
111 .tx_loadsz = 128,
112 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01,
113 .rxtrig_bytes = {16, 32, 112, 120},
114 /* UART_CAP_EFR breaks billionon CF bluetooth card. */
115 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
116 },
117 [PORT_16654] = {
118 .name = "ST16654",
119 .fifo_size = 64,
120 .tx_loadsz = 32,
121 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
122 UART_FCR_T_TRIG_10,
123 .rxtrig_bytes = {8, 16, 56, 60},
124 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
125 },
126 [PORT_16850] = {
127 .name = "XR16850",
128 .fifo_size = 128,
129 .tx_loadsz = 128,
130 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
131 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
132 },
133 [PORT_RSA] = {
134 .name = "RSA",
135 .fifo_size = 2048,
136 .tx_loadsz = 2048,
137 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
138 .flags = UART_CAP_FIFO,
139 },
140 [PORT_NS16550A] = {
141 .name = "NS16550A",
142 .fifo_size = 16,
143 .tx_loadsz = 16,
144 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
145 .flags = UART_CAP_FIFO | UART_NATSEMI,
146 },
147 [PORT_XSCALE] = {
148 .name = "XScale",
149 .fifo_size = 32,
150 .tx_loadsz = 32,
151 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
152 .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
153 },
154 [PORT_OCTEON] = {
155 .name = "OCTEON",
156 .fifo_size = 64,
157 .tx_loadsz = 64,
158 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
159 .flags = UART_CAP_FIFO,
160 },
161 [PORT_U6_16550A] = {
162 .name = "U6_16550A",
163 .fifo_size = 64,
164 .tx_loadsz = 64,
165 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
166 .flags = UART_CAP_FIFO | UART_CAP_AFE,
167 },
168 [PORT_TEGRA] = {
169 .name = "Tegra",
170 .fifo_size = 32,
171 .tx_loadsz = 8,
172 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
173 UART_FCR_T_TRIG_01,
174 .rxtrig_bytes = {1, 4, 8, 14},
175 .flags = UART_CAP_FIFO | UART_CAP_RTOIE,
176 },
177 [PORT_XR17D15X] = {
178 .name = "XR17D15X",
179 .fifo_size = 64,
180 .tx_loadsz = 64,
181 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
182 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
183 UART_CAP_SLEEP,
184 },
185 [PORT_XR17V35X] = {
186 .name = "XR17V35X",
187 .fifo_size = 256,
188 .tx_loadsz = 256,
189 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11 |
190 UART_FCR_T_TRIG_11,
191 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
192 UART_CAP_SLEEP,
193 },
194 [PORT_LPC3220] = {
195 .name = "LPC3220",
196 .fifo_size = 64,
197 .tx_loadsz = 32,
198 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
199 UART_FCR_R_TRIG_00 | UART_FCR_T_TRIG_00,
200 .flags = UART_CAP_FIFO,
201 },
202 [PORT_BRCM_TRUMANAGE] = {
203 .name = "TruManage",
204 .fifo_size = 1,
205 .tx_loadsz = 1024,
206 .flags = UART_CAP_HFIFO,
207 },
208 [PORT_8250_CIR] = {
209 .name = "CIR port"
210 },
211 [PORT_ALTR_16550_F32] = {
212 .name = "Altera 16550 FIFO32",
213 .fifo_size = 32,
214 .tx_loadsz = 32,
215 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
216 .rxtrig_bytes = {1, 8, 16, 30},
217 .flags = UART_CAP_FIFO | UART_CAP_AFE,
218 },
219 [PORT_ALTR_16550_F64] = {
220 .name = "Altera 16550 FIFO64",
221 .fifo_size = 64,
222 .tx_loadsz = 64,
223 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
224 .rxtrig_bytes = {1, 16, 32, 62},
225 .flags = UART_CAP_FIFO | UART_CAP_AFE,
226 },
227 [PORT_ALTR_16550_F128] = {
228 .name = "Altera 16550 FIFO128",
229 .fifo_size = 128,
230 .tx_loadsz = 128,
231 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
232 .rxtrig_bytes = {1, 32, 64, 126},
233 .flags = UART_CAP_FIFO | UART_CAP_AFE,
234 },
235 /*
236 * tx_loadsz is set to 63-bytes instead of 64-bytes to implement
237 * workaround of errata A-008006 which states that tx_loadsz should
238 * be configured less than Maximum supported fifo bytes.
239 */
240 [PORT_16550A_FSL64] = {
241 .name = "16550A_FSL64",
242 .fifo_size = 64,
243 .tx_loadsz = 63,
244 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
245 UART_FCR7_64BYTE,
246 .flags = UART_CAP_FIFO | UART_CAP_NOTEMT,
247 },
248 [PORT_RT2880] = {
249 .name = "Palmchip BK-3103",
250 .fifo_size = 16,
251 .tx_loadsz = 16,
252 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
253 .rxtrig_bytes = {1, 4, 8, 14},
254 .flags = UART_CAP_FIFO,
255 },
256 [PORT_DA830] = {
257 .name = "TI DA8xx/66AK2x",
258 .fifo_size = 16,
259 .tx_loadsz = 16,
260 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
261 UART_FCR_R_TRIG_10,
262 .rxtrig_bytes = {1, 4, 8, 14},
263 .flags = UART_CAP_FIFO | UART_CAP_AFE,
264 },
265 [PORT_MTK_BTIF] = {
266 .name = "MediaTek BTIF",
267 .fifo_size = 16,
268 .tx_loadsz = 16,
269 .fcr = UART_FCR_ENABLE_FIFO |
270 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
271 .flags = UART_CAP_FIFO,
272 },
273 [PORT_NPCM] = {
274 .name = "Nuvoton 16550",
275 .fifo_size = 16,
276 .tx_loadsz = 16,
277 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
278 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
279 .rxtrig_bytes = {1, 4, 8, 14},
280 .flags = UART_CAP_FIFO,
281 },
282 [PORT_SUNIX] = {
283 .name = "Sunix",
284 .fifo_size = 128,
285 .tx_loadsz = 128,
286 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
287 .rxtrig_bytes = {1, 32, 64, 112},
288 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
289 },
290 [PORT_ASPEED_VUART] = {
291 .name = "ASPEED VUART",
292 .fifo_size = 16,
293 .tx_loadsz = 16,
294 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
295 .rxtrig_bytes = {1, 4, 8, 14},
296 .flags = UART_CAP_FIFO,
297 },
298 [PORT_MCHP16550A] = {
299 .name = "MCHP16550A",
300 .fifo_size = 256,
301 .tx_loadsz = 256,
302 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01,
303 .rxtrig_bytes = {2, 66, 130, 194},
304 .flags = UART_CAP_FIFO,
305 },
306 [PORT_BCM7271] = {
307 .name = "Broadcom BCM7271 UART",
308 .fifo_size = 32,
309 .tx_loadsz = 32,
310 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01,
311 .rxtrig_bytes = {1, 8, 16, 30},
312 .flags = UART_CAP_FIFO | UART_CAP_AFE,
313 },
314 [PORT_MUEX50] = {
315 .name = "Moxa MUEx50 UART",
316 .fifo_size = 128,
317 .tx_loadsz = 128,
318 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
319 .rxtrig_bytes = {15, 31, 63, 111},
320 .flags = UART_CAP_FIFO,
321 },
322 };
323
324 /* Uart divisor latch read */
default_serial_dl_read(struct uart_8250_port * up)325 static u32 default_serial_dl_read(struct uart_8250_port *up)
326 {
327 /* Assign these in pieces to truncate any bits above 7. */
328 unsigned char dll = serial_in(up, UART_DLL);
329 unsigned char dlm = serial_in(up, UART_DLM);
330
331 return dll | dlm << 8;
332 }
333
334 /* Uart divisor latch write */
default_serial_dl_write(struct uart_8250_port * up,u32 value)335 static void default_serial_dl_write(struct uart_8250_port *up, u32 value)
336 {
337 serial_out(up, UART_DLL, value & 0xff);
338 serial_out(up, UART_DLM, value >> 8 & 0xff);
339 }
340
341 #ifdef CONFIG_HAS_IOPORT
hub6_serial_in(struct uart_port * p,unsigned int offset)342 static u32 hub6_serial_in(struct uart_port *p, unsigned int offset)
343 {
344 offset = offset << p->regshift;
345 outb(p->hub6 - 1 + offset, p->iobase);
346 return inb(p->iobase + 1);
347 }
348
hub6_serial_out(struct uart_port * p,unsigned int offset,u32 value)349 static void hub6_serial_out(struct uart_port *p, unsigned int offset, u32 value)
350 {
351 offset = offset << p->regshift;
352 outb(p->hub6 - 1 + offset, p->iobase);
353 outb(value, p->iobase + 1);
354 }
355 #endif /* CONFIG_HAS_IOPORT */
356
mem_serial_in(struct uart_port * p,unsigned int offset)357 static u32 mem_serial_in(struct uart_port *p, unsigned int offset)
358 {
359 offset = offset << p->regshift;
360 return readb(p->membase + offset);
361 }
362
mem_serial_out(struct uart_port * p,unsigned int offset,u32 value)363 static void mem_serial_out(struct uart_port *p, unsigned int offset, u32 value)
364 {
365 offset = offset << p->regshift;
366 writeb(value, p->membase + offset);
367 }
368
mem16_serial_out(struct uart_port * p,unsigned int offset,u32 value)369 static void mem16_serial_out(struct uart_port *p, unsigned int offset, u32 value)
370 {
371 offset = offset << p->regshift;
372 writew(value, p->membase + offset);
373 }
374
mem16_serial_in(struct uart_port * p,unsigned int offset)375 static u32 mem16_serial_in(struct uart_port *p, unsigned int offset)
376 {
377 offset = offset << p->regshift;
378 return readw(p->membase + offset);
379 }
380
mem32_serial_out(struct uart_port * p,unsigned int offset,u32 value)381 static void mem32_serial_out(struct uart_port *p, unsigned int offset, u32 value)
382 {
383 offset = offset << p->regshift;
384 writel(value, p->membase + offset);
385 }
386
mem32_serial_in(struct uart_port * p,unsigned int offset)387 static u32 mem32_serial_in(struct uart_port *p, unsigned int offset)
388 {
389 offset = offset << p->regshift;
390 return readl(p->membase + offset);
391 }
392
mem32be_serial_out(struct uart_port * p,unsigned int offset,u32 value)393 static void mem32be_serial_out(struct uart_port *p, unsigned int offset, u32 value)
394 {
395 offset = offset << p->regshift;
396 iowrite32be(value, p->membase + offset);
397 }
398
mem32be_serial_in(struct uart_port * p,unsigned int offset)399 static u32 mem32be_serial_in(struct uart_port *p, unsigned int offset)
400 {
401 offset = offset << p->regshift;
402 return ioread32be(p->membase + offset);
403 }
404
405 #ifdef CONFIG_HAS_IOPORT
io_serial_in(struct uart_port * p,unsigned int offset)406 static u32 io_serial_in(struct uart_port *p, unsigned int offset)
407 {
408 offset = offset << p->regshift;
409 return inb(p->iobase + offset);
410 }
411
io_serial_out(struct uart_port * p,unsigned int offset,u32 value)412 static void io_serial_out(struct uart_port *p, unsigned int offset, u32 value)
413 {
414 offset = offset << p->regshift;
415 outb(value, p->iobase + offset);
416 }
417 #endif
no_serial_in(struct uart_port * p,unsigned int offset)418 static u32 no_serial_in(struct uart_port *p, unsigned int offset)
419 {
420 return ~0U;
421 }
422
no_serial_out(struct uart_port * p,unsigned int offset,u32 value)423 static void no_serial_out(struct uart_port *p, unsigned int offset, u32 value)
424 {
425 }
426
427 static int serial8250_default_handle_irq(struct uart_port *port);
428
set_io_from_upio(struct uart_port * p)429 static void set_io_from_upio(struct uart_port *p)
430 {
431 struct uart_8250_port *up = up_to_u8250p(p);
432
433 up->dl_read = default_serial_dl_read;
434 up->dl_write = default_serial_dl_write;
435
436 switch (p->iotype) {
437 #ifdef CONFIG_HAS_IOPORT
438 case UPIO_HUB6:
439 p->serial_in = hub6_serial_in;
440 p->serial_out = hub6_serial_out;
441 break;
442 #endif
443
444 case UPIO_MEM:
445 p->serial_in = mem_serial_in;
446 p->serial_out = mem_serial_out;
447 break;
448
449 case UPIO_MEM16:
450 p->serial_in = mem16_serial_in;
451 p->serial_out = mem16_serial_out;
452 break;
453
454 case UPIO_MEM32:
455 p->serial_in = mem32_serial_in;
456 p->serial_out = mem32_serial_out;
457 break;
458
459 case UPIO_MEM32BE:
460 p->serial_in = mem32be_serial_in;
461 p->serial_out = mem32be_serial_out;
462 break;
463 #ifdef CONFIG_HAS_IOPORT
464 case UPIO_PORT:
465 p->serial_in = io_serial_in;
466 p->serial_out = io_serial_out;
467 break;
468 #endif
469 case UPIO_AU:
470 break;
471 default:
472 WARN(p->iotype != UPIO_PORT || p->iobase,
473 "Unsupported UART type %x\n", p->iotype);
474 p->serial_in = no_serial_in;
475 p->serial_out = no_serial_out;
476 }
477 /* Remember loaded iotype */
478 up->cur_iotype = p->iotype;
479 p->handle_irq = serial8250_default_handle_irq;
480 }
481
482 static void
serial_port_out_sync(struct uart_port * p,int offset,int value)483 serial_port_out_sync(struct uart_port *p, int offset, int value)
484 {
485 if (uart_iotype_mmio(p->iotype)) {
486 p->serial_out(p, offset, value);
487 p->serial_in(p, UART_LCR); /* safe, no side-effects */
488 } else {
489 p->serial_out(p, offset, value);
490 }
491 }
492
493 /*
494 * FIFO support.
495 */
serial8250_clear_fifos(struct uart_8250_port * p)496 void serial8250_clear_fifos(struct uart_8250_port *p)
497 {
498 if (p->capabilities & UART_CAP_FIFO) {
499 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
500 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
501 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
502 serial_out(p, UART_FCR, 0);
503 }
504 }
505 EXPORT_SYMBOL_NS_GPL(serial8250_clear_fifos, "SERIAL_8250");
506
507 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t);
508 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t);
509
serial8250_clear_and_reinit_fifos(struct uart_8250_port * p)510 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
511 {
512 serial8250_clear_fifos(p);
513 serial_out(p, UART_FCR, p->fcr);
514 }
515 EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);
516
serial8250_rpm_get(struct uart_8250_port * p)517 void serial8250_rpm_get(struct uart_8250_port *p)
518 {
519 if (!(p->capabilities & UART_CAP_RPM))
520 return;
521 pm_runtime_get_sync(p->port.dev);
522 }
523 EXPORT_SYMBOL_GPL(serial8250_rpm_get);
524
serial8250_rpm_put(struct uart_8250_port * p)525 void serial8250_rpm_put(struct uart_8250_port *p)
526 {
527 if (!(p->capabilities & UART_CAP_RPM))
528 return;
529 pm_runtime_mark_last_busy(p->port.dev);
530 pm_runtime_put_autosuspend(p->port.dev);
531 }
532 EXPORT_SYMBOL_GPL(serial8250_rpm_put);
533
534 /**
535 * serial8250_em485_init() - put uart_8250_port into rs485 emulating
536 * @p: uart_8250_port port instance
537 *
538 * The function is used to start rs485 software emulating on the
539 * &struct uart_8250_port* @p. Namely, RTS is switched before/after
540 * transmission. The function is idempotent, so it is safe to call it
541 * multiple times.
542 *
543 * The caller MUST enable interrupt on empty shift register before
544 * calling serial8250_em485_init(). This interrupt is not a part of
545 * 8250 standard, but implementation defined.
546 *
547 * The function is supposed to be called from .rs485_config callback
548 * or from any other callback protected with p->port.lock spinlock.
549 *
550 * See also serial8250_em485_destroy()
551 *
552 * Return 0 - success, -errno - otherwise
553 */
serial8250_em485_init(struct uart_8250_port * p)554 static int serial8250_em485_init(struct uart_8250_port *p)
555 {
556 /* Port locked to synchronize UART_IER access against the console. */
557 lockdep_assert_held_once(&p->port.lock);
558
559 if (p->em485)
560 goto deassert_rts;
561
562 p->em485 = kmalloc_obj(struct uart_8250_em485, GFP_ATOMIC);
563 if (!p->em485)
564 return -ENOMEM;
565
566 hrtimer_setup(&p->em485->stop_tx_timer, &serial8250_em485_handle_stop_tx, CLOCK_MONOTONIC,
567 HRTIMER_MODE_REL);
568 hrtimer_setup(&p->em485->start_tx_timer, &serial8250_em485_handle_start_tx, CLOCK_MONOTONIC,
569 HRTIMER_MODE_REL);
570 p->em485->port = p;
571 p->em485->active_timer = NULL;
572 p->em485->tx_stopped = true;
573
574 deassert_rts:
575 if (p->em485->tx_stopped)
576 p->rs485_stop_tx(p, true);
577
578 return 0;
579 }
580
581 /**
582 * serial8250_em485_destroy() - put uart_8250_port into normal state
583 * @p: uart_8250_port port instance
584 *
585 * The function is used to stop rs485 software emulating on the
586 * &struct uart_8250_port* @p. The function is idempotent, so it is safe to
587 * call it multiple times.
588 *
589 * The function is supposed to be called from .rs485_config callback
590 * or from any other callback protected with p->port.lock spinlock.
591 *
592 * See also serial8250_em485_init()
593 */
serial8250_em485_destroy(struct uart_8250_port * p)594 void serial8250_em485_destroy(struct uart_8250_port *p)
595 {
596 if (!p->em485)
597 return;
598
599 hrtimer_cancel(&p->em485->start_tx_timer);
600 hrtimer_cancel(&p->em485->stop_tx_timer);
601
602 kfree(p->em485);
603 p->em485 = NULL;
604 }
605 EXPORT_SYMBOL_GPL(serial8250_em485_destroy);
606
607 struct serial_rs485 serial8250_em485_supported = {
608 .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
609 SER_RS485_TERMINATE_BUS | SER_RS485_RX_DURING_TX,
610 .delay_rts_before_send = 1,
611 .delay_rts_after_send = 1,
612 };
613 EXPORT_SYMBOL_GPL(serial8250_em485_supported);
614
615 /**
616 * serial8250_em485_config() - generic ->rs485_config() callback
617 * @port: uart port
618 * @termios: termios structure
619 * @rs485: rs485 settings
620 *
621 * Generic callback usable by 8250 uart drivers to activate rs485 settings
622 * if the uart is incapable of driving RTS as a Transmit Enable signal in
623 * hardware, relying on software emulation instead.
624 */
serial8250_em485_config(struct uart_port * port,struct ktermios * termios,struct serial_rs485 * rs485)625 int serial8250_em485_config(struct uart_port *port, struct ktermios *termios,
626 struct serial_rs485 *rs485)
627 {
628 struct uart_8250_port *up = up_to_u8250p(port);
629
630 /*
631 * Both serial8250_em485_init() and serial8250_em485_destroy()
632 * are idempotent.
633 */
634 if (rs485->flags & SER_RS485_ENABLED)
635 return serial8250_em485_init(up);
636
637 serial8250_em485_destroy(up);
638 return 0;
639 }
640 EXPORT_SYMBOL_GPL(serial8250_em485_config);
641
642 /*
643 * These two wrappers ensure that enable_runtime_pm_tx() can be called more than
644 * once and disable_runtime_pm_tx() will still disable RPM because the fifo is
645 * empty and the HW can idle again.
646 */
serial8250_rpm_get_tx(struct uart_8250_port * p)647 static void serial8250_rpm_get_tx(struct uart_8250_port *p)
648 {
649 unsigned char rpm_active;
650
651 if (!(p->capabilities & UART_CAP_RPM))
652 return;
653
654 rpm_active = xchg(&p->rpm_tx_active, 1);
655 if (rpm_active)
656 return;
657 pm_runtime_get_sync(p->port.dev);
658 }
659
serial8250_rpm_put_tx(struct uart_8250_port * p)660 static void serial8250_rpm_put_tx(struct uart_8250_port *p)
661 {
662 unsigned char rpm_active;
663
664 if (!(p->capabilities & UART_CAP_RPM))
665 return;
666
667 rpm_active = xchg(&p->rpm_tx_active, 0);
668 if (!rpm_active)
669 return;
670 pm_runtime_mark_last_busy(p->port.dev);
671 pm_runtime_put_autosuspend(p->port.dev);
672 }
673
674 /*
675 * IER sleep support. UARTs which have EFRs need the "extended
676 * capability" bit enabled. Note that on XR16C850s, we need to
677 * reset LCR to write to IER.
678 */
serial8250_set_sleep(struct uart_8250_port * p,int sleep)679 static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
680 {
681 unsigned char lcr = 0, efr = 0;
682
683 guard(serial8250_rpm)(p);
684
685 if (!(p->capabilities & UART_CAP_SLEEP))
686 return;
687
688 /* Synchronize UART_IER access against the console. */
689 guard(uart_port_lock_irq)(&p->port);
690
691 if (p->capabilities & UART_CAP_EFR) {
692 lcr = serial_in(p, UART_LCR);
693 efr = serial_in(p, UART_EFR);
694 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
695 serial_out(p, UART_EFR, UART_EFR_ECB);
696 serial_out(p, UART_LCR, 0);
697 }
698 serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
699 if (p->capabilities & UART_CAP_EFR) {
700 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
701 serial_out(p, UART_EFR, efr);
702 serial_out(p, UART_LCR, lcr);
703 }
704 }
705
706 /* Clear the interrupt registers. */
serial8250_clear_interrupts(struct uart_port * port)707 static void serial8250_clear_interrupts(struct uart_port *port)
708 {
709 serial_port_in(port, UART_LSR);
710 serial_port_in(port, UART_RX);
711 serial_port_in(port, UART_IIR);
712 serial_port_in(port, UART_MSR);
713 }
714
715 /*
716 * Only to be directly used by serial8250_console_write() and
717 * serial8250_put_poll_char(), which do not require the port lock.
718 * Use serial8250_clear_IER() instead for all other cases.
719 */
__serial8250_clear_IER(struct uart_8250_port * up)720 static void __serial8250_clear_IER(struct uart_8250_port *up)
721 {
722 if (up->capabilities & UART_CAP_UUE)
723 serial_out(up, UART_IER, UART_IER_UUE);
724 else
725 serial_out(up, UART_IER, 0);
726 }
727
serial8250_clear_IER(struct uart_8250_port * up)728 static inline void serial8250_clear_IER(struct uart_8250_port *up)
729 {
730 /* Port locked to synchronize UART_IER access against the console */
731 lockdep_assert_held_once(&up->port.lock);
732
733 __serial8250_clear_IER(up);
734 }
735
736 /*
737 * This is a quickie test to see how big the FIFO is.
738 * It doesn't work at all the time, more's the pity.
739 */
size_fifo(struct uart_8250_port * up)740 static int size_fifo(struct uart_8250_port *up)
741 {
742 unsigned char old_fcr, old_mcr, old_lcr;
743 u32 old_dl;
744 int count;
745
746 old_lcr = serial_in(up, UART_LCR);
747 serial_out(up, UART_LCR, 0);
748 old_fcr = serial_in(up, UART_FCR);
749 old_mcr = serial8250_in_MCR(up);
750 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
751 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
752 serial8250_out_MCR(up, UART_MCR_LOOP);
753 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
754 old_dl = serial_dl_read(up);
755 serial_dl_write(up, 0x0001);
756 serial_out(up, UART_LCR, UART_LCR_WLEN8);
757 for (count = 0; count < 256; count++)
758 serial_out(up, UART_TX, count);
759 mdelay(20);/* FIXME - schedule_timeout */
760 for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
761 (count < 256); count++)
762 serial_in(up, UART_RX);
763 serial_out(up, UART_FCR, old_fcr);
764 serial8250_out_MCR(up, old_mcr);
765 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
766 serial_dl_write(up, old_dl);
767 serial_out(up, UART_LCR, old_lcr);
768
769 return count;
770 }
771
772 /*
773 * Read UART ID using the divisor method - set DLL and DLM to zero
774 * and the revision will be in DLL and device type in DLM. We
775 * preserve the device state across this.
776 */
autoconfig_read_divisor_id(struct uart_8250_port * p)777 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
778 {
779 unsigned char old_lcr;
780 unsigned int id, old_dl;
781
782 old_lcr = serial_in(p, UART_LCR);
783 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
784 old_dl = serial_dl_read(p);
785 serial_dl_write(p, 0);
786 id = serial_dl_read(p);
787 serial_dl_write(p, old_dl);
788
789 serial_out(p, UART_LCR, old_lcr);
790
791 return id;
792 }
793
794 /*
795 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
796 * When this function is called we know it is at least a StarTech
797 * 16650 V2, but it might be one of several StarTech UARTs, or one of
798 * its clones. (We treat the broken original StarTech 16650 V1 as a
799 * 16550, and why not? Startech doesn't seem to even acknowledge its
800 * existence.)
801 *
802 * What evil have men's minds wrought...
803 */
autoconfig_has_efr(struct uart_8250_port * up)804 static void autoconfig_has_efr(struct uart_8250_port *up)
805 {
806 unsigned int id1, id2, id3, rev;
807
808 /*
809 * Everything with an EFR has SLEEP
810 */
811 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
812
813 /*
814 * First we check to see if it's an Oxford Semiconductor UART.
815 *
816 * If we have to do this here because some non-National
817 * Semiconductor clone chips lock up if you try writing to the
818 * LSR register (which serial_icr_read does)
819 */
820
821 /*
822 * Check for Oxford Semiconductor 16C950.
823 *
824 * EFR [4] must be set else this test fails.
825 *
826 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
827 * claims that it's needed for 952 dual UART's (which are not
828 * recommended for new designs).
829 */
830 up->acr = 0;
831 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
832 serial_out(up, UART_EFR, UART_EFR_ECB);
833 serial_out(up, UART_LCR, 0x00);
834 id1 = serial_icr_read(up, UART_ID1);
835 id2 = serial_icr_read(up, UART_ID2);
836 id3 = serial_icr_read(up, UART_ID3);
837 rev = serial_icr_read(up, UART_REV);
838
839 if (id1 == 0x16 && id2 == 0xC9 &&
840 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
841 up->port.type = PORT_16C950;
842
843 /*
844 * Enable work around for the Oxford Semiconductor 952 rev B
845 * chip which causes it to seriously miscalculate baud rates
846 * when DLL is 0.
847 */
848 if (id3 == 0x52 && rev == 0x01)
849 up->bugs |= UART_BUG_QUOT;
850 return;
851 }
852
853 /*
854 * We check for a XR16C850 by setting DLL and DLM to 0, and then
855 * reading back DLL and DLM. The chip type depends on the DLM
856 * value read back:
857 * 0x10 - XR16C850 and the DLL contains the chip revision.
858 * 0x12 - XR16C2850.
859 * 0x14 - XR16C854.
860 */
861 id1 = autoconfig_read_divisor_id(up);
862
863 id2 = id1 >> 8;
864 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
865 up->port.type = PORT_16850;
866 return;
867 }
868
869 /*
870 * It wasn't an XR16C850.
871 *
872 * We distinguish between the '654 and the '650 by counting
873 * how many bytes are in the FIFO. I'm using this for now,
874 * since that's the technique that was sent to me in the
875 * serial driver update, but I'm not convinced this works.
876 * I've had problems doing this in the past. -TYT
877 */
878 if (size_fifo(up) == 64)
879 up->port.type = PORT_16654;
880 else
881 up->port.type = PORT_16650V2;
882 }
883
884 /*
885 * We detected a chip without a FIFO. Only two fall into
886 * this category - the original 8250 and the 16450. The
887 * 16450 has a scratch register (accessible with LCR=0)
888 */
autoconfig_8250(struct uart_8250_port * up)889 static void autoconfig_8250(struct uart_8250_port *up)
890 {
891 unsigned char scratch, status1, status2;
892
893 up->port.type = PORT_8250;
894
895 scratch = serial_in(up, UART_SCR);
896 serial_out(up, UART_SCR, 0xa5);
897 status1 = serial_in(up, UART_SCR);
898 serial_out(up, UART_SCR, 0x5a);
899 status2 = serial_in(up, UART_SCR);
900 serial_out(up, UART_SCR, scratch);
901
902 if (status1 == 0xa5 && status2 == 0x5a)
903 up->port.type = PORT_16450;
904 }
905
broken_efr(struct uart_8250_port * up)906 static int broken_efr(struct uart_8250_port *up)
907 {
908 /*
909 * Exar ST16C2550 "A2" devices incorrectly detect as
910 * having an EFR, and report an ID of 0x0201. See
911 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
912 */
913 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
914 return 1;
915
916 return 0;
917 }
918
919 /*
920 * We know that the chip has FIFOs. Does it have an EFR? The
921 * EFR is located in the same register position as the IIR and
922 * we know the top two bits of the IIR are currently set. The
923 * EFR should contain zero. Try to read the EFR.
924 */
autoconfig_16550a(struct uart_8250_port * up)925 static void autoconfig_16550a(struct uart_8250_port *up)
926 {
927 unsigned char status1, status2;
928 unsigned int iersave;
929
930 /* Port locked to synchronize UART_IER access against the console. */
931 lockdep_assert_held_once(&up->port.lock);
932
933 up->port.type = PORT_16550A;
934 up->capabilities |= UART_CAP_FIFO;
935
936 if (!IS_ENABLED(CONFIG_SERIAL_8250_16550A_VARIANTS) &&
937 !(up->port.flags & UPF_FULL_PROBE))
938 return;
939
940 /*
941 * Check for presence of the EFR when DLAB is set.
942 * Only ST16C650V1 UARTs pass this test.
943 */
944 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
945 if (serial_in(up, UART_EFR) == 0) {
946 serial_out(up, UART_EFR, 0xA8);
947 if (serial_in(up, UART_EFR) != 0) {
948 up->port.type = PORT_16650;
949 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
950 } else {
951 serial_out(up, UART_LCR, 0);
952 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
953 UART_FCR7_64BYTE);
954 status1 = serial_in(up, UART_IIR) & UART_IIR_FIFO_ENABLED_16750;
955 serial_out(up, UART_FCR, 0);
956 serial_out(up, UART_LCR, 0);
957
958 if (status1 == UART_IIR_FIFO_ENABLED_16750)
959 up->port.type = PORT_16550A_FSL64;
960 }
961 serial_out(up, UART_EFR, 0);
962 return;
963 }
964
965 /*
966 * Maybe it requires 0xbf to be written to the LCR.
967 * (other ST16C650V2 UARTs, TI16C752A, etc)
968 */
969 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
970 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
971 autoconfig_has_efr(up);
972 return;
973 }
974
975 /*
976 * Check for a National Semiconductor SuperIO chip.
977 * Attempt to switch to bank 2, read the value of the LOOP bit
978 * from EXCR1. Switch back to bank 0, change it in MCR. Then
979 * switch back to bank 2, read it from EXCR1 again and check
980 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
981 */
982 serial_out(up, UART_LCR, 0);
983 status1 = serial8250_in_MCR(up);
984 serial_out(up, UART_LCR, 0xE0);
985 status2 = serial_in(up, 0x02); /* EXCR1 */
986
987 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
988 serial_out(up, UART_LCR, 0);
989 serial8250_out_MCR(up, status1 ^ UART_MCR_LOOP);
990 serial_out(up, UART_LCR, 0xE0);
991 status2 = serial_in(up, 0x02); /* EXCR1 */
992 serial_out(up, UART_LCR, 0);
993 serial8250_out_MCR(up, status1);
994
995 if ((status2 ^ status1) & UART_MCR_LOOP) {
996 unsigned short quot;
997
998 serial_out(up, UART_LCR, 0xE0);
999
1000 quot = serial_dl_read(up);
1001 quot <<= 3;
1002
1003 if (ns16550a_goto_highspeed(up))
1004 serial_dl_write(up, quot);
1005
1006 serial_out(up, UART_LCR, 0);
1007
1008 up->port.uartclk = 921600*16;
1009 up->port.type = PORT_NS16550A;
1010 up->capabilities |= UART_NATSEMI;
1011 return;
1012 }
1013 }
1014
1015 /*
1016 * No EFR. Try to detect a TI16750, which only sets bit 5 of
1017 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1018 * Try setting it with and without DLAB set. Cheap clones
1019 * set bit 5 without DLAB set.
1020 */
1021 serial_out(up, UART_LCR, 0);
1022 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1023 status1 = serial_in(up, UART_IIR) & UART_IIR_FIFO_ENABLED_16750;
1024 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1025
1026 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1027 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1028 status2 = serial_in(up, UART_IIR) & UART_IIR_FIFO_ENABLED_16750;
1029 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1030
1031 serial_out(up, UART_LCR, 0);
1032
1033 if (status1 == UART_IIR_FIFO_ENABLED_16550A &&
1034 status2 == UART_IIR_FIFO_ENABLED_16750) {
1035 up->port.type = PORT_16750;
1036 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1037 return;
1038 }
1039
1040 /*
1041 * Try writing and reading the UART_IER_UUE bit (b6).
1042 * If it works, this is probably one of the Xscale platform's
1043 * internal UARTs.
1044 * We're going to explicitly set the UUE bit to 0 before
1045 * trying to write and read a 1 just to make sure it's not
1046 * already a 1 and maybe locked there before we even start.
1047 */
1048 iersave = serial_in(up, UART_IER);
1049 serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
1050 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1051 /*
1052 * OK it's in a known zero state, try writing and reading
1053 * without disturbing the current state of the other bits.
1054 */
1055 serial_out(up, UART_IER, iersave | UART_IER_UUE);
1056 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1057 /*
1058 * It's an Xscale.
1059 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1060 */
1061 up->port.type = PORT_XSCALE;
1062 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
1063 return;
1064 }
1065 }
1066 serial_out(up, UART_IER, iersave);
1067
1068 /*
1069 * We distinguish between 16550A and U6 16550A by counting
1070 * how many bytes are in the FIFO.
1071 */
1072 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1073 up->port.type = PORT_U6_16550A;
1074 up->capabilities |= UART_CAP_AFE;
1075 }
1076 }
1077
1078 /*
1079 * This routine is called by rs_init() to initialize a specific serial
1080 * port. It determines what type of UART chip this serial port is
1081 * using: 8250, 16450, 16550, 16550A. The important question is
1082 * whether or not this UART is a 16550A or not, since this will
1083 * determine whether or not we can use its FIFO features or not.
1084 */
autoconfig(struct uart_8250_port * up)1085 static void autoconfig(struct uart_8250_port *up)
1086 {
1087 unsigned char status1, scratch, scratch2, scratch3;
1088 unsigned char save_lcr, save_mcr;
1089 struct uart_port *port = &up->port;
1090 unsigned long flags;
1091 unsigned int old_capabilities;
1092
1093 if (!port->iobase && !port->mapbase && !port->membase)
1094 return;
1095
1096 /*
1097 * We really do need global IRQs disabled here - we're going to
1098 * be frobbing the chips IRQ enable register to see if it exists.
1099 *
1100 * Synchronize UART_IER access against the console.
1101 */
1102 uart_port_lock_irqsave(port, &flags);
1103
1104 up->capabilities = 0;
1105 up->bugs = 0;
1106
1107 if (!(port->flags & UPF_BUGGY_UART)) {
1108 /*
1109 * Do a simple existence test first; if we fail this,
1110 * there's no point trying anything else.
1111 *
1112 * 0x80 is used as a nonsense port to prevent against
1113 * false positives due to ISA bus float. The
1114 * assumption is that 0x80 is a non-existent port;
1115 * which should be safe since include/asm/io.h also
1116 * makes this assumption.
1117 *
1118 * Note: this is safe as long as MCR bit 4 is clear
1119 * and the device is in "PC" mode.
1120 */
1121 scratch = serial_in(up, UART_IER);
1122 serial_out(up, UART_IER, 0);
1123 #if defined(__i386__) && defined(CONFIG_HAS_IOPORT)
1124 outb(0xff, 0x080);
1125 #endif
1126 /*
1127 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1128 * 16C754B) allow only to modify them if an EFR bit is set.
1129 */
1130 scratch2 = serial_in(up, UART_IER) & UART_IER_ALL_INTR;
1131 serial_out(up, UART_IER, UART_IER_ALL_INTR);
1132 #if defined(__i386__) && defined(CONFIG_HAS_IOPORT)
1133 outb(0, 0x080);
1134 #endif
1135 scratch3 = serial_in(up, UART_IER) & UART_IER_ALL_INTR;
1136 serial_out(up, UART_IER, scratch);
1137 if (scratch2 != 0 || scratch3 != UART_IER_ALL_INTR) {
1138 /*
1139 * We failed; there's nothing here
1140 */
1141 uart_port_unlock_irqrestore(port, flags);
1142 return;
1143 }
1144 }
1145
1146 save_mcr = serial8250_in_MCR(up);
1147 save_lcr = serial_in(up, UART_LCR);
1148
1149 /*
1150 * Check to see if a UART is really there. Certain broken
1151 * internal modems based on the Rockwell chipset fail this
1152 * test, because they apparently don't implement the loopback
1153 * test mode. So this test is skipped on the COM 1 through
1154 * COM 4 ports. This *should* be safe, since no board
1155 * manufacturer would be stupid enough to design a board
1156 * that conflicts with COM 1-4 --- we hope!
1157 */
1158 if (!(port->flags & UPF_SKIP_TEST)) {
1159 serial8250_out_MCR(up, UART_MCR_LOOP | UART_MCR_OUT2 | UART_MCR_RTS);
1160 status1 = serial_in(up, UART_MSR) & UART_MSR_STATUS_BITS;
1161 serial8250_out_MCR(up, save_mcr);
1162 if (status1 != (UART_MSR_DCD | UART_MSR_CTS)) {
1163 uart_port_unlock_irqrestore(port, flags);
1164 return;
1165 }
1166 }
1167
1168 /*
1169 * We're pretty sure there's a port here. Lets find out what
1170 * type of port it is. The IIR top two bits allows us to find
1171 * out if it's 8250 or 16450, 16550, 16550A or later. This
1172 * determines what we test for next.
1173 *
1174 * We also initialise the EFR (if any) to zero for later. The
1175 * EFR occupies the same register location as the FCR and IIR.
1176 */
1177 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1178 serial_out(up, UART_EFR, 0);
1179 serial_out(up, UART_LCR, 0);
1180
1181 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1182
1183 switch (serial_in(up, UART_IIR) & UART_IIR_FIFO_ENABLED) {
1184 case UART_IIR_FIFO_ENABLED_8250:
1185 autoconfig_8250(up);
1186 break;
1187 case UART_IIR_FIFO_ENABLED_16550:
1188 port->type = PORT_16550;
1189 break;
1190 case UART_IIR_FIFO_ENABLED_16550A:
1191 autoconfig_16550a(up);
1192 break;
1193 default:
1194 port->type = PORT_UNKNOWN;
1195 break;
1196 }
1197
1198 rsa_autoconfig(up);
1199
1200 serial_out(up, UART_LCR, save_lcr);
1201
1202 port->fifosize = uart_config[up->port.type].fifo_size;
1203 old_capabilities = up->capabilities;
1204 up->capabilities = uart_config[port->type].flags;
1205 up->tx_loadsz = uart_config[port->type].tx_loadsz;
1206
1207 if (port->type != PORT_UNKNOWN) {
1208 /*
1209 * Reset the UART.
1210 */
1211 rsa_reset(up);
1212 serial8250_out_MCR(up, save_mcr);
1213 serial8250_clear_fifos(up);
1214 serial_in(up, UART_RX);
1215 serial8250_clear_IER(up);
1216 }
1217
1218 uart_port_unlock_irqrestore(port, flags);
1219
1220 /*
1221 * Check if the device is a Fintek F81216A
1222 */
1223 if (port->type == PORT_16550A && port->iotype == UPIO_PORT)
1224 fintek_8250_probe(up);
1225
1226 if (up->capabilities != old_capabilities) {
1227 dev_warn(port->dev, "detected caps %08x should be %08x\n",
1228 old_capabilities, up->capabilities);
1229 }
1230 }
1231
autoconfig_irq(struct uart_8250_port * up)1232 static void autoconfig_irq(struct uart_8250_port *up)
1233 {
1234 struct uart_port *port = &up->port;
1235 unsigned char save_mcr, save_ier;
1236 unsigned char save_ICP = 0;
1237 unsigned int ICP = 0;
1238 unsigned long irqs;
1239 int irq;
1240
1241 if (port->flags & UPF_FOURPORT) {
1242 ICP = (port->iobase & 0xfe0) | 0x1f;
1243 save_ICP = inb_p(ICP);
1244 outb_p(0x80, ICP);
1245 inb_p(ICP);
1246 }
1247
1248 /* forget possible initially masked and pending IRQ */
1249 probe_irq_off(probe_irq_on());
1250 save_mcr = serial8250_in_MCR(up);
1251 /* Synchronize UART_IER access against the console. */
1252 scoped_guard(uart_port_lock_irq, port)
1253 save_ier = serial_in(up, UART_IER);
1254 serial8250_out_MCR(up, UART_MCR_OUT1 | UART_MCR_OUT2);
1255
1256 irqs = probe_irq_on();
1257 serial8250_out_MCR(up, 0);
1258 udelay(10);
1259 if (port->flags & UPF_FOURPORT) {
1260 serial8250_out_MCR(up, UART_MCR_DTR | UART_MCR_RTS);
1261 } else {
1262 serial8250_out_MCR(up,
1263 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1264 }
1265 /* Synchronize UART_IER access against the console. */
1266 scoped_guard(uart_port_lock_irq, port)
1267 serial_out(up, UART_IER, UART_IER_ALL_INTR);
1268 serial8250_clear_interrupts(port);
1269 serial_out(up, UART_TX, 0xFF);
1270 udelay(20);
1271 irq = probe_irq_off(irqs);
1272
1273 serial8250_out_MCR(up, save_mcr);
1274 /* Synchronize UART_IER access against the console. */
1275 scoped_guard(uart_port_lock_irq, port)
1276 serial_out(up, UART_IER, save_ier);
1277
1278 if (port->flags & UPF_FOURPORT)
1279 outb_p(save_ICP, ICP);
1280
1281 port->irq = (irq > 0) ? irq : 0;
1282 }
1283
serial8250_stop_rx(struct uart_port * port)1284 static void serial8250_stop_rx(struct uart_port *port)
1285 {
1286 struct uart_8250_port *up = up_to_u8250p(port);
1287
1288 /* Port locked to synchronize UART_IER access against the console. */
1289 lockdep_assert_held_once(&port->lock);
1290
1291 guard(serial8250_rpm)(up);
1292
1293 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1294 serial_port_out(port, UART_IER, up->ier);
1295 }
1296
1297 /**
1298 * serial8250_em485_stop_tx() - generic ->rs485_stop_tx() callback
1299 * @p: uart 8250 port
1300 * @toggle_ier: true to allow enabling receive interrupts
1301 *
1302 * Generic callback usable by 8250 uart drivers to stop rs485 transmission.
1303 */
serial8250_em485_stop_tx(struct uart_8250_port * p,bool toggle_ier)1304 void serial8250_em485_stop_tx(struct uart_8250_port *p, bool toggle_ier)
1305 {
1306 unsigned char mcr = serial8250_in_MCR(p);
1307
1308 if (p->port.rs485.flags & SER_RS485_RTS_AFTER_SEND)
1309 mcr |= UART_MCR_RTS;
1310 else
1311 mcr &= ~UART_MCR_RTS;
1312 serial8250_out_MCR(p, mcr);
1313
1314 /*
1315 * Empty the RX FIFO, we are not interested in anything
1316 * received during the half-duplex transmission.
1317 * Enable previously disabled RX interrupts.
1318 */
1319 if (!(p->port.rs485.flags & SER_RS485_RX_DURING_TX)) {
1320 serial8250_clear_and_reinit_fifos(p);
1321
1322 if (toggle_ier) {
1323 /*
1324 * Port locked to synchronize UART_IER access against
1325 * the console. The lockdep_assert must be restricted
1326 * to this condition because only here is it
1327 * guaranteed that the port lock is held. The other
1328 * hardware access in this function is synchronized
1329 * by console ownership.
1330 */
1331 lockdep_assert_held_once(&p->port.lock);
1332
1333 p->ier |= UART_IER_RLSI | UART_IER_RDI;
1334 serial_port_out(&p->port, UART_IER, p->ier);
1335 }
1336 }
1337 }
1338 EXPORT_SYMBOL_GPL(serial8250_em485_stop_tx);
1339
serial8250_em485_handle_stop_tx(struct hrtimer * t)1340 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t)
1341 {
1342 struct uart_8250_em485 *em485 = container_of(t, struct uart_8250_em485,
1343 stop_tx_timer);
1344 struct uart_8250_port *p = em485->port;
1345
1346 guard(serial8250_rpm)(p);
1347 guard(uart_port_lock_irqsave)(&p->port);
1348
1349 if (em485->active_timer == &em485->stop_tx_timer) {
1350 p->rs485_stop_tx(p, true);
1351 em485->active_timer = NULL;
1352 em485->tx_stopped = true;
1353 }
1354
1355 return HRTIMER_NORESTART;
1356 }
1357
start_hrtimer_ms(struct hrtimer * hrt,unsigned long msec)1358 static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec)
1359 {
1360 hrtimer_start(hrt, ms_to_ktime(msec), HRTIMER_MODE_REL);
1361 }
1362
__stop_tx_rs485(struct uart_8250_port * p,u64 stop_delay)1363 static void __stop_tx_rs485(struct uart_8250_port *p, u64 stop_delay)
1364 {
1365 struct uart_8250_em485 *em485 = p->em485;
1366
1367 /* Port locked to synchronize UART_IER access against the console. */
1368 lockdep_assert_held_once(&p->port.lock);
1369
1370 stop_delay += (u64)p->port.rs485.delay_rts_after_send * NSEC_PER_MSEC;
1371
1372 /*
1373 * rs485_stop_tx() is going to set RTS according to config
1374 * AND flush RX FIFO if required.
1375 */
1376 if (stop_delay > 0) {
1377 em485->active_timer = &em485->stop_tx_timer;
1378 hrtimer_start(&em485->stop_tx_timer, ns_to_ktime(stop_delay), HRTIMER_MODE_REL);
1379 } else {
1380 p->rs485_stop_tx(p, true);
1381 em485->active_timer = NULL;
1382 em485->tx_stopped = true;
1383 }
1384 }
1385
__stop_tx(struct uart_8250_port * p)1386 static inline void __stop_tx(struct uart_8250_port *p)
1387 {
1388 struct uart_8250_em485 *em485 = p->em485;
1389
1390 if (em485) {
1391 u16 lsr = serial_lsr_in(p);
1392 u64 stop_delay = 0;
1393
1394 if (!(lsr & UART_LSR_THRE))
1395 return;
1396 /*
1397 * To provide required timing and allow FIFO transfer,
1398 * __stop_tx_rs485() must be called only when both FIFO and
1399 * shift register are empty. The device driver should either
1400 * enable interrupt on TEMT or set UART_CAP_NOTEMT that will
1401 * enlarge stop_tx_timer by the tx time of one frame to cover
1402 * for emptying of the shift register.
1403 */
1404 if (!(lsr & UART_LSR_TEMT)) {
1405 if (!(p->capabilities & UART_CAP_NOTEMT))
1406 return;
1407 /*
1408 * RTS might get deasserted too early with the normal
1409 * frame timing formula. It seems to suggest THRE might
1410 * get asserted already during tx of the stop bit
1411 * rather than after it is fully sent.
1412 * Roughly estimate 1 extra bit here with / 7.
1413 */
1414 stop_delay = p->port.frame_time + DIV_ROUND_UP(p->port.frame_time, 7);
1415 }
1416
1417 __stop_tx_rs485(p, stop_delay);
1418 }
1419
1420 if (serial8250_clear_THRI(p))
1421 serial8250_rpm_put_tx(p);
1422 }
1423
serial8250_stop_tx(struct uart_port * port)1424 static void serial8250_stop_tx(struct uart_port *port)
1425 {
1426 struct uart_8250_port *up = up_to_u8250p(port);
1427
1428 guard(serial8250_rpm)(up);
1429 __stop_tx(up);
1430
1431 /*
1432 * We really want to stop the transmitter from sending.
1433 */
1434 if (port->type == PORT_16C950) {
1435 up->acr |= UART_ACR_TXDIS;
1436 serial_icr_write(up, UART_ACR, up->acr);
1437 }
1438 }
1439
__start_tx(struct uart_port * port)1440 static inline void __start_tx(struct uart_port *port)
1441 {
1442 struct uart_8250_port *up = up_to_u8250p(port);
1443
1444 if (up->dma && !up->dma->tx_dma(up))
1445 return;
1446
1447 if (serial8250_set_THRI(up)) {
1448 if (up->bugs & UART_BUG_TXEN) {
1449 u16 lsr = serial_lsr_in(up);
1450
1451 if (lsr & UART_LSR_THRE)
1452 serial8250_tx_chars(up);
1453 }
1454 }
1455
1456 /*
1457 * Re-enable the transmitter if we disabled it.
1458 */
1459 if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1460 up->acr &= ~UART_ACR_TXDIS;
1461 serial_icr_write(up, UART_ACR, up->acr);
1462 }
1463 }
1464
1465 /**
1466 * serial8250_em485_start_tx() - generic ->rs485_start_tx() callback
1467 * @up: uart 8250 port
1468 * @toggle_ier: true to allow disabling receive interrupts
1469 *
1470 * Generic callback usable by 8250 uart drivers to start rs485 transmission.
1471 * Assumes that setting the RTS bit in the MCR register means RTS is high.
1472 * (Some chips use inverse semantics.) Further assumes that reception is
1473 * stoppable by disabling the UART_IER_RDI interrupt. (Some chips set the
1474 * UART_LSR_DR bit even when UART_IER_RDI is disabled, foiling this approach.)
1475 */
serial8250_em485_start_tx(struct uart_8250_port * up,bool toggle_ier)1476 void serial8250_em485_start_tx(struct uart_8250_port *up, bool toggle_ier)
1477 {
1478 unsigned char mcr = serial8250_in_MCR(up);
1479
1480 if (!(up->port.rs485.flags & SER_RS485_RX_DURING_TX) && toggle_ier)
1481 serial8250_stop_rx(&up->port);
1482
1483 if (up->port.rs485.flags & SER_RS485_RTS_ON_SEND)
1484 mcr |= UART_MCR_RTS;
1485 else
1486 mcr &= ~UART_MCR_RTS;
1487 serial8250_out_MCR(up, mcr);
1488 }
1489 EXPORT_SYMBOL_GPL(serial8250_em485_start_tx);
1490
1491 /* Returns false, if start_tx_timer was setup to defer TX start */
start_tx_rs485(struct uart_port * port)1492 static bool start_tx_rs485(struct uart_port *port)
1493 {
1494 struct uart_8250_port *up = up_to_u8250p(port);
1495 struct uart_8250_em485 *em485 = up->em485;
1496
1497 /*
1498 * While serial8250_em485_handle_stop_tx() is a noop if
1499 * em485->active_timer != &em485->stop_tx_timer, it might happen that
1500 * the timer is still armed and triggers only after the current bunch of
1501 * chars is send and em485->active_timer == &em485->stop_tx_timer again.
1502 * So cancel the timer. There is still a theoretical race condition if
1503 * the timer is already running and only comes around to check for
1504 * em485->active_timer when &em485->stop_tx_timer is armed again.
1505 */
1506 if (em485->active_timer == &em485->stop_tx_timer)
1507 hrtimer_try_to_cancel(&em485->stop_tx_timer);
1508
1509 em485->active_timer = NULL;
1510
1511 if (em485->tx_stopped) {
1512 em485->tx_stopped = false;
1513
1514 up->rs485_start_tx(up, true);
1515
1516 if (up->port.rs485.delay_rts_before_send > 0) {
1517 em485->active_timer = &em485->start_tx_timer;
1518 start_hrtimer_ms(&em485->start_tx_timer,
1519 up->port.rs485.delay_rts_before_send);
1520 return false;
1521 }
1522 }
1523
1524 return true;
1525 }
1526
serial8250_em485_handle_start_tx(struct hrtimer * t)1527 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t)
1528 {
1529 struct uart_8250_em485 *em485 = container_of(t, struct uart_8250_em485,
1530 start_tx_timer);
1531 struct uart_8250_port *p = em485->port;
1532
1533 guard(uart_port_lock_irqsave)(&p->port);
1534
1535 if (em485->active_timer == &em485->start_tx_timer) {
1536 __start_tx(&p->port);
1537 em485->active_timer = NULL;
1538 }
1539
1540 return HRTIMER_NORESTART;
1541 }
1542
serial8250_start_tx(struct uart_port * port)1543 static void serial8250_start_tx(struct uart_port *port)
1544 {
1545 struct uart_8250_port *up = up_to_u8250p(port);
1546 struct uart_8250_em485 *em485 = up->em485;
1547
1548 /* Port locked to synchronize UART_IER access against the console. */
1549 lockdep_assert_held_once(&port->lock);
1550
1551 if (!port->x_char && kfifo_is_empty(&port->state->port.xmit_fifo))
1552 return;
1553
1554 serial8250_rpm_get_tx(up);
1555
1556 if (em485) {
1557 if ((em485->active_timer == &em485->start_tx_timer) ||
1558 !start_tx_rs485(port))
1559 return;
1560 }
1561 __start_tx(port);
1562 }
1563
serial8250_throttle(struct uart_port * port)1564 static void serial8250_throttle(struct uart_port *port)
1565 {
1566 port->throttle(port);
1567 }
1568
serial8250_unthrottle(struct uart_port * port)1569 static void serial8250_unthrottle(struct uart_port *port)
1570 {
1571 port->unthrottle(port);
1572 }
1573
serial8250_disable_ms(struct uart_port * port)1574 static void serial8250_disable_ms(struct uart_port *port)
1575 {
1576 struct uart_8250_port *up = up_to_u8250p(port);
1577
1578 /* Port locked to synchronize UART_IER access against the console. */
1579 lockdep_assert_held_once(&port->lock);
1580
1581 /* no MSR capabilities */
1582 if (up->bugs & UART_BUG_NOMSR)
1583 return;
1584
1585 mctrl_gpio_disable_ms_no_sync(up->gpios);
1586
1587 up->ier &= ~UART_IER_MSI;
1588 serial_port_out(port, UART_IER, up->ier);
1589 }
1590
serial8250_enable_ms(struct uart_port * port)1591 static void serial8250_enable_ms(struct uart_port *port)
1592 {
1593 struct uart_8250_port *up = up_to_u8250p(port);
1594
1595 /* Port locked to synchronize UART_IER access against the console. */
1596 lockdep_assert_held_once(&port->lock);
1597
1598 /* no MSR capabilities */
1599 if (up->bugs & UART_BUG_NOMSR)
1600 return;
1601
1602 mctrl_gpio_enable_ms(up->gpios);
1603
1604 up->ier |= UART_IER_MSI;
1605
1606 guard(serial8250_rpm)(up);
1607 serial_port_out(port, UART_IER, up->ier);
1608 }
1609
serial8250_read_char(struct uart_8250_port * up,u16 lsr)1610 void serial8250_read_char(struct uart_8250_port *up, u16 lsr)
1611 {
1612 struct uart_port *port = &up->port;
1613 u8 ch, flag = TTY_NORMAL;
1614
1615 if (likely(lsr & UART_LSR_DR))
1616 ch = serial_in(up, UART_RX);
1617 else
1618 /*
1619 * Intel 82571 has a Serial Over Lan device that will
1620 * set UART_LSR_BI without setting UART_LSR_DR when
1621 * it receives a break. To avoid reading from the
1622 * receive buffer without UART_LSR_DR bit set, we
1623 * just force the read character to be 0
1624 */
1625 ch = 0;
1626
1627 port->icount.rx++;
1628
1629 lsr |= up->lsr_saved_flags;
1630 up->lsr_saved_flags = 0;
1631
1632 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1633 if (lsr & UART_LSR_BI) {
1634 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1635 port->icount.brk++;
1636 /*
1637 * We do the SysRQ and SAK checking
1638 * here because otherwise the break
1639 * may get masked by ignore_status_mask
1640 * or read_status_mask.
1641 */
1642 if (uart_handle_break(port))
1643 return;
1644 } else if (lsr & UART_LSR_PE)
1645 port->icount.parity++;
1646 else if (lsr & UART_LSR_FE)
1647 port->icount.frame++;
1648 if (lsr & UART_LSR_OE)
1649 port->icount.overrun++;
1650
1651 /*
1652 * Mask off conditions which should be ignored.
1653 */
1654 lsr &= port->read_status_mask;
1655
1656 if (lsr & UART_LSR_BI) {
1657 dev_dbg(port->dev, "handling break\n");
1658 flag = TTY_BREAK;
1659 } else if (lsr & UART_LSR_PE)
1660 flag = TTY_PARITY;
1661 else if (lsr & UART_LSR_FE)
1662 flag = TTY_FRAME;
1663 }
1664 if (uart_prepare_sysrq_char(port, ch))
1665 return;
1666
1667 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
1668 }
1669 EXPORT_SYMBOL_GPL(serial8250_read_char);
1670
1671 /*
1672 * serial8250_rx_chars - Read characters. The first LSR value must be passed in.
1673 *
1674 * Returns LSR bits. The caller should rely only on non-Rx related LSR bits
1675 * (such as THRE) because the LSR value might come from an already consumed
1676 * character.
1677 */
serial8250_rx_chars(struct uart_8250_port * up,u16 lsr)1678 u16 serial8250_rx_chars(struct uart_8250_port *up, u16 lsr)
1679 {
1680 struct uart_port *port = &up->port;
1681 int max_count = 256;
1682
1683 do {
1684 serial8250_read_char(up, lsr);
1685 if (--max_count == 0)
1686 break;
1687 lsr = serial_in(up, UART_LSR);
1688 } while (lsr & (UART_LSR_DR | UART_LSR_BI));
1689
1690 tty_flip_buffer_push(&port->state->port);
1691 return lsr;
1692 }
1693 EXPORT_SYMBOL_GPL(serial8250_rx_chars);
1694
serial8250_tx_chars(struct uart_8250_port * up)1695 void serial8250_tx_chars(struct uart_8250_port *up)
1696 {
1697 struct uart_port *port = &up->port;
1698 struct tty_port *tport = &port->state->port;
1699 int count;
1700
1701 if (port->x_char) {
1702 uart_xchar_out(port, UART_TX);
1703 return;
1704 }
1705 if (uart_tx_stopped(port)) {
1706 serial8250_stop_tx(port);
1707 return;
1708 }
1709 if (kfifo_is_empty(&tport->xmit_fifo)) {
1710 __stop_tx(up);
1711 return;
1712 }
1713
1714 count = up->tx_loadsz;
1715 do {
1716 unsigned char c;
1717
1718 if (!uart_fifo_get(port, &c))
1719 break;
1720
1721 serial_out(up, UART_TX, c);
1722 if (up->bugs & UART_BUG_TXRACE) {
1723 /*
1724 * The Aspeed BMC virtual UARTs have a bug where data
1725 * may get stuck in the BMC's Tx FIFO from bursts of
1726 * writes on the APB interface.
1727 *
1728 * Delay back-to-back writes by a read cycle to avoid
1729 * stalling the VUART. Read a register that won't have
1730 * side-effects and discard the result.
1731 */
1732 serial_in(up, UART_SCR);
1733 }
1734
1735 if ((up->capabilities & UART_CAP_HFIFO) &&
1736 !uart_lsr_tx_empty(serial_in(up, UART_LSR)))
1737 break;
1738 /* The BCM2835 MINI UART THRE bit is really a not-full bit. */
1739 if ((up->capabilities & UART_CAP_MINI) &&
1740 !(serial_in(up, UART_LSR) & UART_LSR_THRE))
1741 break;
1742 } while (--count > 0);
1743
1744 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
1745 uart_write_wakeup(port);
1746
1747 /*
1748 * With RPM enabled, we have to wait until the FIFO is empty before the
1749 * HW can go idle. So we get here once again with empty FIFO and disable
1750 * the interrupt and RPM in __stop_tx()
1751 */
1752 if (kfifo_is_empty(&tport->xmit_fifo) &&
1753 !(up->capabilities & UART_CAP_RPM))
1754 __stop_tx(up);
1755 }
1756 EXPORT_SYMBOL_GPL(serial8250_tx_chars);
1757
1758 /* Caller holds uart port lock */
serial8250_modem_status(struct uart_8250_port * up)1759 unsigned int serial8250_modem_status(struct uart_8250_port *up)
1760 {
1761 struct uart_port *port = &up->port;
1762 unsigned int status = serial_in(up, UART_MSR);
1763
1764 status |= up->msr_saved_flags;
1765 up->msr_saved_flags = 0;
1766 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1767 port->state != NULL) {
1768 if (status & UART_MSR_TERI)
1769 port->icount.rng++;
1770 if (status & UART_MSR_DDSR)
1771 port->icount.dsr++;
1772 if (status & UART_MSR_DDCD)
1773 uart_handle_dcd_change(port, status & UART_MSR_DCD);
1774 if (status & UART_MSR_DCTS)
1775 uart_handle_cts_change(port, status & UART_MSR_CTS);
1776
1777 wake_up_interruptible(&port->state->port.delta_msr_wait);
1778 }
1779
1780 return status;
1781 }
1782 EXPORT_SYMBOL_GPL(serial8250_modem_status);
1783
handle_rx_dma(struct uart_8250_port * up,unsigned int iir)1784 static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir)
1785 {
1786 switch (iir & 0x3f) {
1787 case UART_IIR_THRI:
1788 /*
1789 * Postpone DMA or not decision to IIR_RDI or IIR_RX_TIMEOUT
1790 * because it's impossible to do an informed decision about
1791 * that with IIR_THRI.
1792 *
1793 * This also fixes one known DMA Rx corruption issue where
1794 * DR is asserted but DMA Rx only gets a corrupted zero byte
1795 * (too early DR?).
1796 */
1797 return false;
1798 case UART_IIR_RDI:
1799 if (!up->dma->rx_running)
1800 break;
1801 fallthrough;
1802 case UART_IIR_RLSI:
1803 case UART_IIR_RX_TIMEOUT:
1804 serial8250_rx_dma_flush(up);
1805 return true;
1806 }
1807 return up->dma->rx_dma(up);
1808 }
1809
1810 /*
1811 * Context: port's lock must be held by the caller. The caller must
1812 * release it via guard(uart_port_lock_check_sysrq_irqsave) or
1813 * uart_unlock_and_check_sysrq_irqrestore(), which captures SysRq
1814 * character on unlock.
1815 */
serial8250_handle_irq_locked(struct uart_port * port,unsigned int iir)1816 void serial8250_handle_irq_locked(struct uart_port *port, unsigned int iir)
1817 {
1818 struct uart_8250_port *up = up_to_u8250p(port);
1819 struct tty_port *tport = &port->state->port;
1820 bool skip_rx = false;
1821 u16 status;
1822
1823 lockdep_assert_held_once(&port->lock);
1824
1825 status = serial_lsr_in(up);
1826
1827 /*
1828 * Recover from no-data-ready and FIFO error condition to avoid getting
1829 * stuck in the ISR.
1830 */
1831 if (!(status & UART_LSR_DR) && (status & UART_LSR_FIFOE))
1832 serial8250_clear_and_reinit_fifos(up);
1833
1834 /*
1835 * If port is stopped and there are no error conditions in the
1836 * FIFO, then don't drain the FIFO, as this may lead to TTY buffer
1837 * overflow. Not servicing, RX FIFO would trigger auto HW flow
1838 * control when FIFO occupancy reaches preset threshold, thus
1839 * halting RX. This only works when auto HW flow control is
1840 * available.
1841 */
1842 if (!(status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS)) &&
1843 (port->status & (UPSTAT_AUTOCTS | UPSTAT_AUTORTS)) &&
1844 !(up->ier & (UART_IER_RLSI | UART_IER_RDI)))
1845 skip_rx = true;
1846
1847 if (status & (UART_LSR_DR | UART_LSR_BI) && !skip_rx) {
1848 struct irq_data *d;
1849
1850 d = irq_get_irq_data(port->irq);
1851 if (d && irqd_is_wakeup_set(d))
1852 pm_wakeup_event(tport->tty->dev, 0);
1853 if (!up->dma || handle_rx_dma(up, iir))
1854 status = serial8250_rx_chars(up, status);
1855 }
1856 serial8250_modem_status(up);
1857 if ((status & UART_LSR_THRE) && (up->ier & UART_IER_THRI)) {
1858 if (!up->dma || up->dma->tx_err)
1859 serial8250_tx_chars(up);
1860 else if (!up->dma->tx_running)
1861 __stop_tx(up);
1862 }
1863 }
1864 EXPORT_SYMBOL_NS_GPL(serial8250_handle_irq_locked, "SERIAL_8250");
1865
1866 /*
1867 * This handles the interrupt from one port.
1868 */
serial8250_handle_irq(struct uart_port * port,unsigned int iir)1869 int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
1870 {
1871 if (iir & UART_IIR_NO_INT)
1872 return 0;
1873
1874 guard(uart_port_lock_check_sysrq_irqsave)(port);
1875 serial8250_handle_irq_locked(port, iir);
1876
1877 return 1;
1878 }
1879 EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1880
serial8250_default_handle_irq(struct uart_port * port)1881 static int serial8250_default_handle_irq(struct uart_port *port)
1882 {
1883 struct uart_8250_port *up = up_to_u8250p(port);
1884 unsigned int iir;
1885
1886 guard(serial8250_rpm)(up);
1887
1888 iir = serial_port_in(port, UART_IIR);
1889 return serial8250_handle_irq(port, iir);
1890 }
1891
1892 /*
1893 * Newer 16550 compatible parts such as the SC16C650 & Altera 16550 Soft IP
1894 * have a programmable TX threshold that triggers the THRE interrupt in
1895 * the IIR register. In this case, the THRE interrupt indicates the FIFO
1896 * has space available. Load it up with tx_loadsz bytes.
1897 */
serial8250_tx_threshold_handle_irq(struct uart_port * port)1898 static int serial8250_tx_threshold_handle_irq(struct uart_port *port)
1899 {
1900 unsigned int iir = serial_port_in(port, UART_IIR);
1901
1902 /* TX Threshold IRQ triggered so load up FIFO */
1903 if ((iir & UART_IIR_ID) == UART_IIR_THRI) {
1904 struct uart_8250_port *up = up_to_u8250p(port);
1905
1906 guard(uart_port_lock_irqsave)(port);
1907 serial8250_tx_chars(up);
1908 }
1909
1910 iir = serial_port_in(port, UART_IIR);
1911 return serial8250_handle_irq(port, iir);
1912 }
1913
serial8250_tx_empty(struct uart_port * port)1914 static unsigned int serial8250_tx_empty(struct uart_port *port)
1915 {
1916 struct uart_8250_port *up = up_to_u8250p(port);
1917
1918 guard(serial8250_rpm)(up);
1919 guard(uart_port_lock_irqsave)(port);
1920
1921 if (!serial8250_tx_dma_running(up) && uart_lsr_tx_empty(serial_lsr_in(up)))
1922 return TIOCSER_TEMT;
1923
1924 return 0;
1925 }
1926
serial8250_do_get_mctrl(struct uart_port * port)1927 unsigned int serial8250_do_get_mctrl(struct uart_port *port)
1928 {
1929 struct uart_8250_port *up = up_to_u8250p(port);
1930 unsigned int status;
1931 unsigned int val;
1932
1933 scoped_guard(serial8250_rpm, up)
1934 status = serial8250_modem_status(up);
1935
1936 val = serial8250_MSR_to_TIOCM(status);
1937 if (up->gpios)
1938 return mctrl_gpio_get(up->gpios, &val);
1939
1940 return val;
1941 }
1942 EXPORT_SYMBOL_GPL(serial8250_do_get_mctrl);
1943
serial8250_get_mctrl(struct uart_port * port)1944 static unsigned int serial8250_get_mctrl(struct uart_port *port)
1945 {
1946 if (port->get_mctrl)
1947 return port->get_mctrl(port);
1948 return serial8250_do_get_mctrl(port);
1949 }
1950
serial8250_do_set_mctrl(struct uart_port * port,unsigned int mctrl)1951 void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl)
1952 {
1953 struct uart_8250_port *up = up_to_u8250p(port);
1954 unsigned char mcr;
1955
1956 mcr = serial8250_TIOCM_to_MCR(mctrl);
1957
1958 mcr |= up->mcr;
1959
1960 serial8250_out_MCR(up, mcr);
1961 }
1962 EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);
1963
serial8250_set_mctrl(struct uart_port * port,unsigned int mctrl)1964 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1965 {
1966 if (port->rs485.flags & SER_RS485_ENABLED)
1967 return;
1968
1969 if (port->set_mctrl)
1970 port->set_mctrl(port, mctrl);
1971 else
1972 serial8250_do_set_mctrl(port, mctrl);
1973 }
1974
serial8250_do_break_ctl(struct uart_port * port,int break_state)1975 void serial8250_do_break_ctl(struct uart_port *port, int break_state)
1976 {
1977 struct uart_8250_port *up = up_to_u8250p(port);
1978
1979 guard(serial8250_rpm)(up);
1980 guard(uart_port_lock_irqsave)(port);
1981
1982 if (break_state == -1)
1983 up->lcr |= UART_LCR_SBC;
1984 else
1985 up->lcr &= ~UART_LCR_SBC;
1986 serial_port_out(port, UART_LCR, up->lcr);
1987 }
1988 EXPORT_SYMBOL_GPL(serial8250_do_break_ctl);
1989
serial8250_break_ctl(struct uart_port * port,int break_state)1990 static void serial8250_break_ctl(struct uart_port *port, int break_state)
1991 {
1992 if (port->break_ctl)
1993 port->break_ctl(port, break_state);
1994 else
1995 serial8250_do_break_ctl(port, break_state);
1996 }
1997
1998 /* Returns true if @bits were set, false on timeout */
wait_for_lsr(struct uart_8250_port * up,int bits)1999 static bool wait_for_lsr(struct uart_8250_port *up, int bits)
2000 {
2001 unsigned int status, tmout;
2002
2003 /*
2004 * Wait for a character to be sent. Fallback to a safe default
2005 * timeout value if @frame_time is not available.
2006 */
2007 if (up->port.frame_time)
2008 tmout = up->port.frame_time * 2 / NSEC_PER_USEC;
2009 else
2010 tmout = 10000;
2011
2012 for (;;) {
2013 status = serial_lsr_in(up);
2014
2015 if ((status & bits) == bits)
2016 break;
2017 if (--tmout == 0)
2018 break;
2019 udelay(1);
2020 touch_nmi_watchdog();
2021 }
2022
2023 return (tmout != 0);
2024 }
2025
2026 /* Wait for transmitter and holding register to empty with timeout */
wait_for_xmitr(struct uart_8250_port * up,int bits)2027 static void wait_for_xmitr(struct uart_8250_port *up, int bits)
2028 {
2029 unsigned int tmout;
2030 bool tx_ready;
2031
2032 tx_ready = wait_for_lsr(up, bits);
2033
2034 /*
2035 * Wait up to 1s for flow control if necessary.
2036 * When 'no_console_suspend' is active (in the window between
2037 * suspend() and resume()), flow control is temporarily ignored
2038 * because the canary workaround is not reliable in all situations,
2039 * leading to flow control timeouts for every character.
2040 */
2041 if (uart_console_hwflow_active(&up->port) && !up->canary) {
2042 for (tmout = 1000000; tmout; tmout--) {
2043 unsigned int msr = serial_in(up, UART_MSR);
2044 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
2045 if (msr & UART_MSR_CTS) {
2046 if (!tx_ready)
2047 wait_for_lsr(up, bits);
2048 break;
2049 }
2050 udelay(1);
2051 touch_nmi_watchdog();
2052 }
2053 }
2054 }
2055
2056 #ifdef CONFIG_CONSOLE_POLL
2057 /*
2058 * Console polling routines for writing and reading from the uart while
2059 * in an interrupt or debug context.
2060 */
2061
serial8250_get_poll_char(struct uart_port * port)2062 static int serial8250_get_poll_char(struct uart_port *port)
2063 {
2064 struct uart_8250_port *up = up_to_u8250p(port);
2065 u16 lsr;
2066
2067 guard(serial8250_rpm)(up);
2068
2069 lsr = serial_port_in(port, UART_LSR);
2070 if (!(lsr & UART_LSR_DR))
2071 return NO_POLL_CHAR;
2072
2073 return serial_port_in(port, UART_RX);
2074 }
2075
2076
serial8250_put_poll_char(struct uart_port * port,unsigned char c)2077 static void serial8250_put_poll_char(struct uart_port *port,
2078 unsigned char c)
2079 {
2080 unsigned int ier;
2081 struct uart_8250_port *up = up_to_u8250p(port);
2082
2083 /*
2084 * Normally the port is locked to synchronize UART_IER access
2085 * against the console. However, this function is only used by
2086 * KDB/KGDB, where it may not be possible to acquire the port
2087 * lock because all other CPUs are quiesced. The quiescence
2088 * should allow safe lockless usage here.
2089 */
2090
2091 guard(serial8250_rpm)(up);
2092 /*
2093 * First, save the IER, then disable the interrupts. The special
2094 * variant to clear the IER is used because KDB/KGDB printing
2095 * is synchronized via CPU quiescence without holding the port
2096 * lock.
2097 */
2098 ier = serial_port_in(port, UART_IER);
2099 __serial8250_clear_IER(up);
2100
2101 wait_for_xmitr(up, UART_LSR_BOTH_EMPTY);
2102 /*
2103 * Send the character out.
2104 */
2105 serial_port_out(port, UART_TX, c);
2106
2107 /*
2108 * Finally, wait for transmitter to become empty
2109 * and restore the IER
2110 */
2111 wait_for_xmitr(up, UART_LSR_BOTH_EMPTY);
2112 serial_port_out(port, UART_IER, ier);
2113 }
2114
2115 #endif /* CONFIG_CONSOLE_POLL */
2116
serial8250_startup_special(struct uart_port * port)2117 static void serial8250_startup_special(struct uart_port *port)
2118 {
2119 struct uart_8250_port *up = up_to_u8250p(port);
2120
2121 switch (port->type) {
2122 case PORT_16C950: {
2123 /*
2124 * Wake up and initialize UART
2125 *
2126 * Synchronize UART_IER access against the console.
2127 */
2128 guard(uart_port_lock_irqsave)(port);
2129 up->acr = 0;
2130 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2131 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2132 serial_port_out(port, UART_IER, 0);
2133 serial_port_out(port, UART_LCR, 0);
2134 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2135 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2136 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2137 serial_port_out(port, UART_LCR, 0);
2138 break;
2139 }
2140 case PORT_DA830:
2141 /*
2142 * Reset the port
2143 *
2144 * Synchronize UART_IER access against the console.
2145 */
2146 scoped_guard(uart_port_lock_irqsave, port) {
2147 serial_port_out(port, UART_IER, 0);
2148 serial_port_out(port, UART_DA830_PWREMU_MGMT, 0);
2149 }
2150 mdelay(10);
2151
2152 /* Enable Tx, Rx and free run mode */
2153 serial_port_out(port, UART_DA830_PWREMU_MGMT,
2154 UART_DA830_PWREMU_MGMT_UTRST |
2155 UART_DA830_PWREMU_MGMT_URRST |
2156 UART_DA830_PWREMU_MGMT_FREE);
2157 break;
2158 case PORT_RSA:
2159 rsa_enable(up);
2160 break;
2161 }
2162 }
2163
serial8250_set_TRG_levels(struct uart_port * port)2164 static void serial8250_set_TRG_levels(struct uart_port *port)
2165 {
2166 struct uart_8250_port *up = up_to_u8250p(port);
2167
2168 switch (port->type) {
2169 /* For a XR16C850, we need to set the trigger levels */
2170 case PORT_16850: {
2171 u8 fctr;
2172
2173 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
2174
2175 fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2176 fctr |= UART_FCTR_TRGD;
2177 serial_port_out(port, UART_FCTR, fctr | UART_FCTR_RX);
2178 serial_port_out(port, UART_TRG, UART_TRG_96);
2179 serial_port_out(port, UART_FCTR, fctr | UART_FCTR_TX);
2180 serial_port_out(port, UART_TRG, UART_TRG_96);
2181
2182 serial_port_out(port, UART_LCR, 0);
2183 break;
2184 }
2185 /* For the Altera 16550 variants, set TX threshold trigger level. */
2186 case PORT_ALTR_16550_F32:
2187 case PORT_ALTR_16550_F64:
2188 case PORT_ALTR_16550_F128:
2189 if (port->fifosize <= 1)
2190 return;
2191
2192 /* Bounds checking of TX threshold (valid 0 to fifosize-2) */
2193 if (up->tx_loadsz < 2 || up->tx_loadsz > port->fifosize) {
2194 dev_err(port->dev, "TX FIFO Threshold errors, skipping\n");
2195 return;
2196 }
2197 serial_port_out(port, UART_ALTR_AFR, UART_ALTR_EN_TXFIFO_LW);
2198 serial_port_out(port, UART_ALTR_TX_LOW, port->fifosize - up->tx_loadsz);
2199 port->handle_irq = serial8250_tx_threshold_handle_irq;
2200 break;
2201 }
2202 }
2203
serial8250_THRE_test(struct uart_port * port)2204 static void serial8250_THRE_test(struct uart_port *port)
2205 {
2206 struct uart_8250_port *up = up_to_u8250p(port);
2207 bool iir_noint1, iir_noint2;
2208
2209 if (!port->irq)
2210 return;
2211
2212 if (up->port.flags & UPF_NO_THRE_TEST)
2213 return;
2214
2215 disable_irq(port->irq);
2216
2217 /*
2218 * Test for UARTs that do not reassert THRE when the transmitter is idle and the interrupt
2219 * has already been cleared. Real 16550s should always reassert this interrupt whenever the
2220 * transmitter is idle and the interrupt is enabled. Delays are necessary to allow register
2221 * changes to become visible.
2222 *
2223 * Synchronize UART_IER access against the console.
2224 */
2225 scoped_guard(uart_port_lock_irqsave, port) {
2226 wait_for_xmitr(up, UART_LSR_THRE);
2227 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2228 udelay(1); /* allow THRE to set */
2229 iir_noint1 = serial_port_in(port, UART_IIR) & UART_IIR_NO_INT;
2230 serial_port_out(port, UART_IER, 0);
2231 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2232 udelay(1); /* allow a working UART time to re-assert THRE */
2233 iir_noint2 = serial_port_in(port, UART_IIR) & UART_IIR_NO_INT;
2234 serial_port_out(port, UART_IER, 0);
2235 }
2236
2237 enable_irq(port->irq);
2238
2239 /*
2240 * If the interrupt is not reasserted, or we otherwise don't trust the iir, setup a timer to
2241 * kick the UART on a regular basis.
2242 */
2243 if ((!iir_noint1 && iir_noint2) || up->port.flags & UPF_BUG_THRE)
2244 up->bugs |= UART_BUG_THRE;
2245 }
2246
serial8250_init_mctrl(struct uart_port * port)2247 static void serial8250_init_mctrl(struct uart_port *port)
2248 {
2249 if (port->flags & UPF_FOURPORT) {
2250 if (!port->irq)
2251 port->mctrl |= TIOCM_OUT1;
2252 } else {
2253 /* Most PC uarts need OUT2 raised to enable interrupts. */
2254 if (port->irq)
2255 port->mctrl |= TIOCM_OUT2;
2256 }
2257
2258 serial8250_set_mctrl(port, port->mctrl);
2259 }
2260
serial8250_iir_txen_test(struct uart_port * port)2261 static void serial8250_iir_txen_test(struct uart_port *port)
2262 {
2263 struct uart_8250_port *up = up_to_u8250p(port);
2264 bool lsr_temt, iir_noint;
2265
2266 if (port->quirks & UPQ_NO_TXEN_TEST)
2267 return;
2268
2269 /* Do a quick test to see if we receive an interrupt when we enable the TX irq. */
2270 serial_port_out(port, UART_IER, UART_IER_THRI);
2271 lsr_temt = serial_port_in(port, UART_LSR) & UART_LSR_TEMT;
2272 iir_noint = serial_port_in(port, UART_IIR) & UART_IIR_NO_INT;
2273 serial_port_out(port, UART_IER, 0);
2274
2275 /*
2276 * Serial over Lan (SoL) hack:
2277 * Intel 8257x Gigabit ethernet chips have a 16550 emulation, to be used for Serial Over
2278 * Lan. Those chips take a longer time than a normal serial device to signalize that a
2279 * transmission data was queued. Due to that, the above test generally fails. One solution
2280 * would be to delay the reading of iir. However, this is not reliable, since the timeout is
2281 * variable. So, in case of UPQ_NO_TXEN_TEST, let's just don't test if we receive TX irq.
2282 * This way, we'll never enable UART_BUG_TXEN.
2283 */
2284 if (lsr_temt && iir_noint) {
2285 if (!(up->bugs & UART_BUG_TXEN)) {
2286 up->bugs |= UART_BUG_TXEN;
2287 dev_dbg(port->dev, "enabling bad tx status workarounds\n");
2288 }
2289 return;
2290 }
2291
2292 /* FIXME: why is this needed? */
2293 up->bugs &= ~UART_BUG_TXEN;
2294 }
2295
serial8250_initialize(struct uart_port * port)2296 static void serial8250_initialize(struct uart_port *port)
2297 {
2298 guard(uart_port_lock_irqsave)(port);
2299 serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
2300
2301 serial8250_init_mctrl(port);
2302 serial8250_iir_txen_test(port);
2303 }
2304
serial8250_do_startup(struct uart_port * port)2305 int serial8250_do_startup(struct uart_port *port)
2306 {
2307 struct uart_8250_port *up = up_to_u8250p(port);
2308 int retval;
2309
2310 if (!port->fifosize)
2311 port->fifosize = uart_config[port->type].fifo_size;
2312 if (!up->tx_loadsz)
2313 up->tx_loadsz = uart_config[port->type].tx_loadsz;
2314 if (!up->capabilities)
2315 up->capabilities = uart_config[port->type].flags;
2316 up->mcr = 0;
2317
2318 if (port->iotype != up->cur_iotype)
2319 set_io_from_upio(port);
2320
2321 guard(serial8250_rpm)(up);
2322
2323 serial8250_startup_special(port);
2324
2325 /*
2326 * Clear the FIFO buffers and disable them.
2327 * (they will be reenabled in set_termios())
2328 */
2329 serial8250_clear_fifos(up);
2330
2331 serial8250_clear_interrupts(port);
2332
2333 /*
2334 * At this point, there's no way the LSR could still be 0xff;
2335 * if it is, then bail out, because there's likely no UART
2336 * here.
2337 */
2338 if (!(port->flags & UPF_BUGGY_UART) &&
2339 (serial_port_in(port, UART_LSR) == 0xff)) {
2340 dev_info_ratelimited(port->dev, "LSR safety check engaged!\n");
2341 return -ENODEV;
2342 }
2343
2344 serial8250_set_TRG_levels(port);
2345
2346 /* Check if we need to have shared IRQs */
2347 if (port->irq && (up->port.flags & UPF_SHARE_IRQ))
2348 up->port.irqflags |= IRQF_SHARED;
2349
2350 retval = up->ops->setup_irq(up);
2351 if (retval)
2352 return retval;
2353
2354 serial8250_THRE_test(port);
2355
2356 up->ops->setup_timer(up);
2357
2358 serial8250_initialize(port);
2359
2360 /*
2361 * Clear the interrupt registers again for luck, and clear the
2362 * saved flags to avoid getting false values from polling
2363 * routines or the previous session.
2364 */
2365 serial8250_clear_interrupts(port);
2366 up->lsr_saved_flags = 0;
2367 up->msr_saved_flags = 0;
2368
2369 /*
2370 * Request DMA channels for both RX and TX.
2371 */
2372 if (up->dma) {
2373 const char *msg = NULL;
2374
2375 if (uart_console(port))
2376 msg = "forbid DMA for kernel console";
2377 else if (serial8250_request_dma(up))
2378 msg = "failed to request DMA";
2379 if (msg) {
2380 dev_warn_ratelimited(port->dev, "%s\n", msg);
2381 up->dma = NULL;
2382 }
2383 }
2384
2385 /*
2386 * Set the IER shadow for rx interrupts but defer actual interrupt
2387 * enable until after the FIFOs are enabled; otherwise, an already-
2388 * active sender can swamp the interrupt handler with "too much work".
2389 */
2390 up->ier = UART_IER_RLSI | UART_IER_RDI;
2391
2392 if (port->flags & UPF_FOURPORT) {
2393 unsigned int icp;
2394 /*
2395 * Enable interrupts on the AST Fourport board
2396 */
2397 icp = (port->iobase & 0xfe0) | 0x01f;
2398 outb_p(0x80, icp);
2399 inb_p(icp);
2400 }
2401
2402 return 0;
2403 }
2404 EXPORT_SYMBOL_GPL(serial8250_do_startup);
2405
serial8250_startup(struct uart_port * port)2406 static int serial8250_startup(struct uart_port *port)
2407 {
2408 if (port->startup)
2409 return port->startup(port);
2410 return serial8250_do_startup(port);
2411 }
2412
serial8250_do_shutdown(struct uart_port * port)2413 void serial8250_do_shutdown(struct uart_port *port)
2414 {
2415 struct uart_8250_port *up = up_to_u8250p(port);
2416 u32 lcr;
2417
2418 serial8250_rpm_get(up);
2419 /*
2420 * Disable interrupts from this port
2421 *
2422 * Synchronize UART_IER access against the console.
2423 */
2424 scoped_guard(uart_port_lock_irqsave, port) {
2425 up->ier = 0;
2426 serial_port_out(port, UART_IER, 0);
2427 }
2428
2429 synchronize_irq(port->irq);
2430
2431 serial8250_release_dma(up);
2432 up->dma = NULL;
2433
2434 scoped_guard(uart_port_lock_irqsave, port) {
2435 if (port->flags & UPF_FOURPORT) {
2436 /* reset interrupts on the AST Fourport board */
2437 inb((port->iobase & 0xfe0) | 0x1f);
2438 port->mctrl |= TIOCM_OUT1;
2439 } else
2440 port->mctrl &= ~TIOCM_OUT2;
2441
2442 serial8250_set_mctrl(port, port->mctrl);
2443
2444 /* Disable break condition */
2445 lcr = serial_port_in(port, UART_LCR);
2446 lcr &= ~UART_LCR_SBC;
2447 serial_port_out(port, UART_LCR, lcr);
2448 }
2449
2450 serial8250_clear_fifos(up);
2451
2452 rsa_disable(up);
2453
2454 /*
2455 * Read data port to reset things, and then unlink from
2456 * the IRQ chain.
2457 */
2458 serial_port_in(port, UART_RX);
2459 /*
2460 * LCR writes on DW UART can trigger late (unmaskable) IRQs.
2461 * Handle them before releasing the handler.
2462 */
2463 synchronize_irq(port->irq);
2464
2465 serial8250_rpm_put(up);
2466
2467 up->ops->release_irq(up);
2468 }
2469 EXPORT_SYMBOL_GPL(serial8250_do_shutdown);
2470
serial8250_shutdown(struct uart_port * port)2471 static void serial8250_shutdown(struct uart_port *port)
2472 {
2473 if (port->shutdown)
2474 port->shutdown(port);
2475 else
2476 serial8250_do_shutdown(port);
2477 }
2478
serial8250_flush_buffer(struct uart_port * port)2479 static void serial8250_flush_buffer(struct uart_port *port)
2480 {
2481 struct uart_8250_port *up = up_to_u8250p(port);
2482
2483 if (up->dma)
2484 serial8250_tx_dma_flush(up);
2485 }
2486
serial8250_do_get_divisor(struct uart_port * port,unsigned int baud)2487 static unsigned int serial8250_do_get_divisor(struct uart_port *port, unsigned int baud)
2488 {
2489 upf_t magic_multiplier = port->flags & UPF_MAGIC_MULTIPLIER;
2490 struct uart_8250_port *up = up_to_u8250p(port);
2491 unsigned int quot;
2492
2493 /*
2494 * Handle magic divisors for baud rates above baud_base on SMSC
2495 * Super I/O chips. We clamp custom rates from clk/6 and clk/12
2496 * up to clk/4 (0x8001) and clk/8 (0x8002) respectively. These
2497 * magic divisors actually reprogram the baud rate generator's
2498 * reference clock derived from chips's 14.318MHz clock input.
2499 *
2500 * Documentation claims that with these magic divisors the base
2501 * frequencies of 7.3728MHz and 3.6864MHz are used respectively
2502 * for the extra baud rates of 460800bps and 230400bps rather
2503 * than the usual base frequency of 1.8462MHz. However empirical
2504 * evidence contradicts that.
2505 *
2506 * Instead bit 7 of the DLM register (bit 15 of the divisor) is
2507 * effectively used as a clock prescaler selection bit for the
2508 * base frequency of 7.3728MHz, always used. If set to 0, then
2509 * the base frequency is divided by 4 for use by the Baud Rate
2510 * Generator, for the usual arrangement where the value of 1 of
2511 * the divisor produces the baud rate of 115200bps. Conversely,
2512 * if set to 1 and high-speed operation has been enabled with the
2513 * Serial Port Mode Register in the Device Configuration Space,
2514 * then the base frequency is supplied directly to the Baud Rate
2515 * Generator, so for the divisor values of 0x8001, 0x8002, 0x8003,
2516 * 0x8004, etc. the respective baud rates produced are 460800bps,
2517 * 230400bps, 153600bps, 115200bps, etc.
2518 *
2519 * In all cases only low 15 bits of the divisor are used to divide
2520 * the baud base and therefore 32767 is the maximum divisor value
2521 * possible, even though documentation says that the programmable
2522 * Baud Rate Generator is capable of dividing the internal PLL
2523 * clock by any divisor from 1 to 65535.
2524 */
2525 if (magic_multiplier && baud >= port->uartclk / 6)
2526 quot = 0x8001;
2527 else if (magic_multiplier && baud >= port->uartclk / 12)
2528 quot = 0x8002;
2529 else
2530 quot = uart_get_divisor(port, baud);
2531
2532 /*
2533 * Oxford Semi 952 rev B workaround
2534 */
2535 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2536 quot++;
2537
2538 return quot;
2539 }
2540
serial8250_get_divisor(struct uart_port * port,unsigned int baud,unsigned int * frac)2541 static unsigned int serial8250_get_divisor(struct uart_port *port,
2542 unsigned int baud,
2543 unsigned int *frac)
2544 {
2545 if (port->get_divisor)
2546 return port->get_divisor(port, baud, frac);
2547
2548 return serial8250_do_get_divisor(port, baud);
2549 }
2550
serial8250_compute_lcr(struct uart_8250_port * up,tcflag_t c_cflag)2551 static unsigned char serial8250_compute_lcr(struct uart_8250_port *up, tcflag_t c_cflag)
2552 {
2553 u8 lcr = UART_LCR_WLEN(tty_get_char_size(c_cflag));
2554
2555 if (c_cflag & CSTOPB)
2556 lcr |= UART_LCR_STOP;
2557 if (c_cflag & PARENB)
2558 lcr |= UART_LCR_PARITY;
2559 if (!(c_cflag & PARODD))
2560 lcr |= UART_LCR_EPAR;
2561 if (c_cflag & CMSPAR)
2562 lcr |= UART_LCR_SPAR;
2563
2564 return lcr;
2565 }
2566
serial8250_do_set_divisor(struct uart_port * port,unsigned int baud,unsigned int quot)2567 void serial8250_do_set_divisor(struct uart_port *port, unsigned int baud,
2568 unsigned int quot)
2569 {
2570 struct uart_8250_port *up = up_to_u8250p(port);
2571
2572 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2573 if (is_omap1510_8250(up)) {
2574 if (baud == 115200) {
2575 quot = 1;
2576 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
2577 } else
2578 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
2579 }
2580
2581 /*
2582 * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
2583 * otherwise just set DLAB
2584 */
2585 if (up->capabilities & UART_NATSEMI)
2586 serial_port_out(port, UART_LCR, 0xe0);
2587 else
2588 serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
2589
2590 serial_dl_write(up, quot);
2591 }
2592 EXPORT_SYMBOL_GPL(serial8250_do_set_divisor);
2593
serial8250_set_divisor(struct uart_port * port,unsigned int baud,unsigned int quot,unsigned int quot_frac)2594 static void serial8250_set_divisor(struct uart_port *port, unsigned int baud,
2595 unsigned int quot, unsigned int quot_frac)
2596 {
2597 if (port->set_divisor)
2598 port->set_divisor(port, baud, quot, quot_frac);
2599 else
2600 serial8250_do_set_divisor(port, baud, quot);
2601 }
2602
serial8250_get_baud_rate(struct uart_port * port,struct ktermios * termios,const struct ktermios * old)2603 static unsigned int serial8250_get_baud_rate(struct uart_port *port,
2604 struct ktermios *termios,
2605 const struct ktermios *old)
2606 {
2607 unsigned int tolerance = port->uartclk / 100;
2608 unsigned int min;
2609 unsigned int max;
2610
2611 /*
2612 * Handle magic divisors for baud rates above baud_base on SMSC
2613 * Super I/O chips. Enable custom rates of clk/4 and clk/8, but
2614 * disable divisor values beyond 32767, which are unavailable.
2615 */
2616 if (port->flags & UPF_MAGIC_MULTIPLIER) {
2617 min = port->uartclk / 16 / UART_DIV_MAX >> 1;
2618 max = (port->uartclk + tolerance) / 4;
2619 } else {
2620 min = port->uartclk / 16 / UART_DIV_MAX;
2621 max = (port->uartclk + tolerance) / 16;
2622 }
2623
2624 /*
2625 * Ask the core to calculate the divisor for us.
2626 * Allow 1% tolerance at the upper limit so uart clks marginally
2627 * slower than nominal still match standard baud rates without
2628 * causing transmission errors.
2629 */
2630 return uart_get_baud_rate(port, termios, old, min, max);
2631 }
2632
2633 /*
2634 * Note in order to avoid the tty port mutex deadlock don't use the next method
2635 * within the uart port callbacks. Primarily it's supposed to be utilized to
2636 * handle a sudden reference clock rate change.
2637 */
serial8250_update_uartclk(struct uart_port * port,unsigned int uartclk)2638 void serial8250_update_uartclk(struct uart_port *port, unsigned int uartclk)
2639 {
2640 struct tty_port *tport = &port->state->port;
2641
2642 scoped_guard(tty_port_tty, tport) {
2643 struct tty_struct *tty = scoped_tty();
2644
2645 guard(rwsem_write)(&tty->termios_rwsem);
2646 guard(mutex)(&tport->mutex);
2647
2648 if (port->uartclk == uartclk)
2649 return;
2650
2651 port->uartclk = uartclk;
2652
2653 if (!tty_port_initialized(tport))
2654 return;
2655
2656 serial8250_do_set_termios(port, &tty->termios, NULL);
2657
2658 return;
2659 }
2660 guard(mutex)(&tport->mutex);
2661 port->uartclk = uartclk;
2662 }
2663 EXPORT_SYMBOL_GPL(serial8250_update_uartclk);
2664
serial8250_set_mini(struct uart_port * port,struct ktermios * termios)2665 static void serial8250_set_mini(struct uart_port *port, struct ktermios *termios)
2666 {
2667 struct uart_8250_port *up = up_to_u8250p(port);
2668
2669 if (!(up->capabilities & UART_CAP_MINI))
2670 return;
2671
2672 termios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CMSPAR);
2673
2674 tcflag_t csize = termios->c_cflag & CSIZE;
2675 if (csize == CS5 || csize == CS6) {
2676 termios->c_cflag &= ~CSIZE;
2677 termios->c_cflag |= CS7;
2678 }
2679 }
2680
serial8250_set_trigger_for_slow_speed(struct uart_port * port,struct ktermios * termios,unsigned int baud)2681 static void serial8250_set_trigger_for_slow_speed(struct uart_port *port, struct ktermios *termios,
2682 unsigned int baud)
2683 {
2684 struct uart_8250_port *up = up_to_u8250p(port);
2685
2686 if (!(up->capabilities & UART_CAP_FIFO))
2687 return;
2688 if (port->fifosize <= 1)
2689 return;
2690 if (baud >= 2400)
2691 return;
2692 if (up->dma)
2693 return;
2694
2695 up->fcr &= ~UART_FCR_TRIGGER_MASK;
2696 up->fcr |= UART_FCR_TRIGGER_1;
2697 }
2698
2699 /*
2700 * MCR-based auto flow control. When AFE is enabled, RTS will be deasserted when the receive FIFO
2701 * contains more characters than the trigger, or the MCR RTS bit is cleared.
2702 */
serial8250_set_afe(struct uart_port * port,struct ktermios * termios)2703 static void serial8250_set_afe(struct uart_port *port, struct ktermios *termios)
2704 {
2705 struct uart_8250_port *up = up_to_u8250p(port);
2706
2707 if (!(up->capabilities & UART_CAP_AFE))
2708 return;
2709
2710 up->mcr &= ~UART_MCR_AFE;
2711 if (termios->c_cflag & CRTSCTS)
2712 up->mcr |= UART_MCR_AFE;
2713 }
2714
serial8250_set_errors_and_ignores(struct uart_port * port,struct ktermios * termios)2715 static void serial8250_set_errors_and_ignores(struct uart_port *port, struct ktermios *termios)
2716 {
2717 /*
2718 * Specify which conditions may be considered for error handling and the ignoring of
2719 * characters. The actual ignoring of characters only occurs if the bit is set in
2720 * @ignore_status_mask as well.
2721 */
2722 port->read_status_mask = UART_LSR_OE | UART_LSR_DR;
2723 if (termios->c_iflag & INPCK)
2724 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2725 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2726 port->read_status_mask |= UART_LSR_BI;
2727
2728 /* Characters to ignore */
2729 port->ignore_status_mask = 0;
2730 if (termios->c_iflag & IGNPAR)
2731 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2732 if (termios->c_iflag & IGNBRK) {
2733 port->ignore_status_mask |= UART_LSR_BI;
2734 /*
2735 * If we're ignoring parity and break indicators, ignore overruns too (for real raw
2736 * support).
2737 */
2738 if (termios->c_iflag & IGNPAR)
2739 port->ignore_status_mask |= UART_LSR_OE;
2740 }
2741
2742 /* ignore all characters if CREAD is not set */
2743 if ((termios->c_cflag & CREAD) == 0)
2744 port->ignore_status_mask |= UART_LSR_DR;
2745 }
2746
serial8250_set_ier(struct uart_port * port,struct ktermios * termios)2747 static void serial8250_set_ier(struct uart_port *port, struct ktermios *termios)
2748 {
2749 struct uart_8250_port *up = up_to_u8250p(port);
2750
2751 /* CTS flow control flag and modem status interrupts */
2752 up->ier &= ~UART_IER_MSI;
2753 if (!(up->bugs & UART_BUG_NOMSR) && UART_ENABLE_MS(&up->port, termios->c_cflag))
2754 up->ier |= UART_IER_MSI;
2755 if (up->capabilities & UART_CAP_UUE)
2756 up->ier |= UART_IER_UUE;
2757 if (up->capabilities & UART_CAP_RTOIE)
2758 up->ier |= UART_IER_RTOIE;
2759
2760 serial_port_out(port, UART_IER, up->ier);
2761 }
2762
serial8250_set_efr(struct uart_port * port,struct ktermios * termios)2763 static void serial8250_set_efr(struct uart_port *port, struct ktermios *termios)
2764 {
2765 struct uart_8250_port *up = up_to_u8250p(port);
2766 u8 efr_reg = UART_EFR;
2767 u8 efr = 0;
2768
2769 if (!(up->capabilities & UART_CAP_EFR))
2770 return;
2771
2772 /*
2773 * TI16C752/Startech hardware flow control. FIXME:
2774 * - TI16C752 requires control thresholds to be set.
2775 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2776 */
2777 if (termios->c_cflag & CRTSCTS)
2778 efr |= UART_EFR_CTS;
2779
2780 if (port->flags & UPF_EXAR_EFR)
2781 efr_reg = UART_XR_EFR;
2782
2783 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2784 serial_port_out(port, efr_reg, efr);
2785 }
2786
serial8250_set_fcr(struct uart_port * port,struct ktermios * termios)2787 static void serial8250_set_fcr(struct uart_port *port, struct ktermios *termios)
2788 {
2789 struct uart_8250_port *up = up_to_u8250p(port);
2790 bool is_16750 = port->type == PORT_16750;
2791
2792 if (is_16750)
2793 serial_port_out(port, UART_FCR, up->fcr);
2794
2795 /*
2796 * LCR DLAB must be reset to enable 64-byte FIFO mode. If the FCR is written without DLAB
2797 * set, this mode will be disabled.
2798 */
2799 serial_port_out(port, UART_LCR, up->lcr);
2800
2801 if (is_16750)
2802 return;
2803
2804 /* emulated UARTs (Lucent Venus 167x) need two steps */
2805 if (up->fcr & UART_FCR_ENABLE_FIFO)
2806 serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
2807
2808 serial_port_out(port, UART_FCR, up->fcr);
2809 }
2810
2811 void
serial8250_do_set_termios(struct uart_port * port,struct ktermios * termios,const struct ktermios * old)2812 serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2813 const struct ktermios *old)
2814 {
2815 struct uart_8250_port *up = up_to_u8250p(port);
2816 unsigned int baud, quot, frac = 0;
2817 u8 lcr;
2818
2819 serial8250_set_mini(port, termios);
2820 lcr = serial8250_compute_lcr(up, termios->c_cflag);
2821 baud = serial8250_get_baud_rate(port, termios, old);
2822 quot = serial8250_get_divisor(port, baud, &frac);
2823
2824 /*
2825 * Ok, we're now changing the port state. Do it with interrupts disabled.
2826 *
2827 * Synchronize UART_IER access against the console.
2828 */
2829 scoped_guard(serial8250_rpm, up) {
2830 guard(uart_port_lock_irqsave)(port);
2831
2832 up->lcr = lcr;
2833 serial8250_set_trigger_for_slow_speed(port, termios, baud);
2834 serial8250_set_afe(port, termios);
2835 uart_update_timeout(port, termios->c_cflag, baud);
2836 serial8250_set_errors_and_ignores(port, termios);
2837 serial8250_set_ier(port, termios);
2838 serial8250_set_efr(port, termios);
2839 serial8250_set_divisor(port, baud, quot, frac);
2840 serial8250_set_fcr(port, termios);
2841 /* Consoles manually poll CTS for hardware flow control. */
2842 if (uart_console(port) &&
2843 !(port->rs485.flags & SER_RS485_ENABLED)
2844 && termios->c_cflag & CRTSCTS) {
2845 port->mctrl |= TIOCM_RTS;
2846 }
2847 serial8250_set_mctrl(port, port->mctrl);
2848 }
2849
2850 /* Don't rewrite B0 */
2851 if (tty_termios_baud_rate(termios))
2852 tty_termios_encode_baud_rate(termios, baud, baud);
2853 }
2854 EXPORT_SYMBOL(serial8250_do_set_termios);
2855
2856 static void
serial8250_set_termios(struct uart_port * port,struct ktermios * termios,const struct ktermios * old)2857 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2858 const struct ktermios *old)
2859 {
2860 if (port->set_termios)
2861 port->set_termios(port, termios, old);
2862 else
2863 serial8250_do_set_termios(port, termios, old);
2864 }
2865
serial8250_do_set_ldisc(struct uart_port * port,struct ktermios * termios)2866 void serial8250_do_set_ldisc(struct uart_port *port, struct ktermios *termios)
2867 {
2868 if (termios->c_line == N_PPS) {
2869 port->flags |= UPF_HARDPPS_CD;
2870 guard(uart_port_lock_irq)(port);
2871 serial8250_enable_ms(port);
2872 } else {
2873 port->flags &= ~UPF_HARDPPS_CD;
2874 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2875 guard(uart_port_lock_irq)(port);
2876 serial8250_disable_ms(port);
2877 }
2878 }
2879 }
2880 EXPORT_SYMBOL_GPL(serial8250_do_set_ldisc);
2881
2882 static void
serial8250_set_ldisc(struct uart_port * port,struct ktermios * termios)2883 serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios)
2884 {
2885 if (port->set_ldisc)
2886 port->set_ldisc(port, termios);
2887 else
2888 serial8250_do_set_ldisc(port, termios);
2889 }
2890
serial8250_do_pm(struct uart_port * port,unsigned int state,unsigned int oldstate)2891 void serial8250_do_pm(struct uart_port *port, unsigned int state,
2892 unsigned int oldstate)
2893 {
2894 struct uart_8250_port *p = up_to_u8250p(port);
2895
2896 serial8250_set_sleep(p, state != 0);
2897 }
2898 EXPORT_SYMBOL(serial8250_do_pm);
2899
2900 static void
serial8250_pm(struct uart_port * port,unsigned int state,unsigned int oldstate)2901 serial8250_pm(struct uart_port *port, unsigned int state,
2902 unsigned int oldstate)
2903 {
2904 if (port->pm)
2905 port->pm(port, state, oldstate);
2906 else
2907 serial8250_do_pm(port, state, oldstate);
2908 }
2909
serial8250_port_size(struct uart_8250_port * pt)2910 static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2911 {
2912 if (pt->port.mapsize)
2913 return pt->port.mapsize;
2914 if (is_omap1_8250(pt))
2915 return 0x16 << pt->port.regshift;
2916
2917 return 8 << pt->port.regshift;
2918 }
2919
2920 /*
2921 * Resource handling.
2922 */
serial8250_request_std_resource(struct uart_8250_port * up)2923 static int serial8250_request_std_resource(struct uart_8250_port *up)
2924 {
2925 unsigned int size = serial8250_port_size(up);
2926 struct uart_port *port = &up->port;
2927
2928 if (uart_iotype_mmio(port->iotype)) {
2929 if (!port->mapbase)
2930 return -EINVAL;
2931
2932 if (!request_mem_region(port->mapbase, size, "serial"))
2933 return -EBUSY;
2934
2935 if (port->flags & UPF_IOREMAP) {
2936 port->membase = ioremap(port->mapbase, size);
2937 if (!port->membase) {
2938 release_mem_region(port->mapbase, size);
2939 return -ENOMEM;
2940 }
2941 }
2942 } else if (uart_iotype_io(port->iotype)) {
2943 if (!request_region(port->iobase, size, "serial"))
2944 return -EBUSY;
2945 }
2946
2947 return 0;
2948 }
2949
serial8250_release_std_resource(struct uart_8250_port * up)2950 static void serial8250_release_std_resource(struct uart_8250_port *up)
2951 {
2952 unsigned int size = serial8250_port_size(up);
2953 struct uart_port *port = &up->port;
2954
2955 if (uart_iotype_mmio(port->iotype)) {
2956 if (!port->mapbase)
2957 return;
2958
2959 if (port->flags & UPF_IOREMAP) {
2960 iounmap(port->membase);
2961 port->membase = NULL;
2962 }
2963
2964 release_mem_region(port->mapbase, size);
2965 } else if (uart_iotype_io(port->iotype)) {
2966 release_region(port->iobase, size);
2967 }
2968 }
2969
serial8250_release_port(struct uart_port * port)2970 static void serial8250_release_port(struct uart_port *port)
2971 {
2972 struct uart_8250_port *up = up_to_u8250p(port);
2973
2974 serial8250_release_std_resource(up);
2975 }
2976
serial8250_request_port(struct uart_port * port)2977 static int serial8250_request_port(struct uart_port *port)
2978 {
2979 struct uart_8250_port *up = up_to_u8250p(port);
2980
2981 return serial8250_request_std_resource(up);
2982 }
2983
fcr_get_rxtrig_bytes(struct uart_8250_port * up)2984 static int fcr_get_rxtrig_bytes(struct uart_8250_port *up)
2985 {
2986 const struct serial8250_config *conf_type = &uart_config[up->port.type];
2987 unsigned char bytes;
2988
2989 bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)];
2990
2991 return bytes ? bytes : -EOPNOTSUPP;
2992 }
2993
bytes_to_fcr_rxtrig(struct uart_8250_port * up,unsigned char bytes)2994 static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes)
2995 {
2996 const struct serial8250_config *conf_type = &uart_config[up->port.type];
2997 int i;
2998
2999 if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)])
3000 return -EOPNOTSUPP;
3001
3002 for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) {
3003 if (bytes < conf_type->rxtrig_bytes[i])
3004 /* Use the nearest lower value */
3005 return (--i) << UART_FCR_R_TRIG_SHIFT;
3006 }
3007
3008 return UART_FCR_R_TRIG_11;
3009 }
3010
do_get_rxtrig(struct tty_port * port)3011 static int do_get_rxtrig(struct tty_port *port)
3012 {
3013 struct uart_state *state = container_of(port, struct uart_state, port);
3014 struct uart_port *uport = state->uart_port;
3015 struct uart_8250_port *up = up_to_u8250p(uport);
3016
3017 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
3018 return -EINVAL;
3019
3020 return fcr_get_rxtrig_bytes(up);
3021 }
3022
do_serial8250_get_rxtrig(struct tty_port * port)3023 static int do_serial8250_get_rxtrig(struct tty_port *port)
3024 {
3025 int rxtrig_bytes;
3026
3027 mutex_lock(&port->mutex);
3028 rxtrig_bytes = do_get_rxtrig(port);
3029 mutex_unlock(&port->mutex);
3030
3031 return rxtrig_bytes;
3032 }
3033
rx_trig_bytes_show(struct device * dev,struct device_attribute * attr,char * buf)3034 static ssize_t rx_trig_bytes_show(struct device *dev,
3035 struct device_attribute *attr, char *buf)
3036 {
3037 struct tty_port *port = dev_get_drvdata(dev);
3038 struct uart_state *state = container_of(port, struct uart_state, port);
3039 struct uart_port *uport = state->uart_port;
3040 int rxtrig_bytes;
3041
3042 if (uport->get_rxtrig)
3043 rxtrig_bytes = uport->get_rxtrig(uport);
3044 else
3045 rxtrig_bytes = do_serial8250_get_rxtrig(port);
3046 if (rxtrig_bytes < 0)
3047 return rxtrig_bytes;
3048
3049 return sysfs_emit(buf, "%d\n", rxtrig_bytes);
3050 }
3051
do_set_rxtrig(struct tty_port * port,unsigned char bytes)3052 static int do_set_rxtrig(struct tty_port *port, unsigned char bytes)
3053 {
3054 struct uart_state *state = container_of(port, struct uart_state, port);
3055 struct uart_port *uport = state->uart_port;
3056 struct uart_8250_port *up = up_to_u8250p(uport);
3057 int rxtrig;
3058
3059 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
3060 return -EINVAL;
3061
3062 rxtrig = bytes_to_fcr_rxtrig(up, bytes);
3063 if (rxtrig < 0)
3064 return rxtrig;
3065
3066 serial8250_clear_fifos(up);
3067 up->fcr &= ~UART_FCR_TRIGGER_MASK;
3068 up->fcr |= (unsigned char)rxtrig;
3069 serial_out(up, UART_FCR, up->fcr);
3070 return 0;
3071 }
3072
do_serial8250_set_rxtrig(struct tty_port * port,unsigned char bytes)3073 static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes)
3074 {
3075 int ret;
3076
3077 mutex_lock(&port->mutex);
3078 ret = do_set_rxtrig(port, bytes);
3079 mutex_unlock(&port->mutex);
3080
3081 return ret;
3082 }
3083
rx_trig_bytes_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3084 static ssize_t rx_trig_bytes_store(struct device *dev,
3085 struct device_attribute *attr, const char *buf, size_t count)
3086 {
3087 struct tty_port *port = dev_get_drvdata(dev);
3088 struct uart_state *state = container_of(port, struct uart_state, port);
3089 struct uart_port *uport = state->uart_port;
3090 unsigned char bytes;
3091 int ret;
3092
3093 if (!count)
3094 return -EINVAL;
3095
3096 ret = kstrtou8(buf, 10, &bytes);
3097 if (ret < 0)
3098 return ret;
3099
3100 if (uport->set_rxtrig)
3101 ret = uport->set_rxtrig(uport, bytes);
3102 else
3103 ret = do_serial8250_set_rxtrig(port, bytes);
3104 if (ret < 0)
3105 return ret;
3106
3107 return count;
3108 }
3109
3110 static DEVICE_ATTR_RW(rx_trig_bytes);
3111
3112 static struct attribute *serial8250_dev_attrs[] = {
3113 &dev_attr_rx_trig_bytes.attr,
3114 NULL
3115 };
3116
3117 static struct attribute_group serial8250_dev_attr_group = {
3118 .attrs = serial8250_dev_attrs,
3119 };
3120
register_dev_spec_attr_grp(struct uart_8250_port * up)3121 static void register_dev_spec_attr_grp(struct uart_8250_port *up)
3122 {
3123 const struct serial8250_config *conf_type = &uart_config[up->port.type];
3124
3125 if (conf_type->rxtrig_bytes[0])
3126 up->port.attr_group = &serial8250_dev_attr_group;
3127 }
3128
serial8250_config_port(struct uart_port * port,int flags)3129 static void serial8250_config_port(struct uart_port *port, int flags)
3130 {
3131 struct uart_8250_port *up = up_to_u8250p(port);
3132 int ret;
3133
3134 /*
3135 * Find the region that we can probe for. This in turn
3136 * tells us whether we can probe for the type of port.
3137 */
3138 ret = serial8250_request_std_resource(up);
3139 if (ret < 0)
3140 return;
3141
3142 if (port->iotype != up->cur_iotype)
3143 set_io_from_upio(port);
3144
3145 if (flags & UART_CONFIG_TYPE)
3146 autoconfig(up);
3147
3148 /* HW bugs may trigger IRQ while IIR == NO_INT */
3149 if (port->type == PORT_TEGRA)
3150 up->bugs |= UART_BUG_NOMSR;
3151
3152 if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
3153 autoconfig_irq(up);
3154
3155 if (port->type == PORT_UNKNOWN)
3156 serial8250_release_std_resource(up);
3157
3158 register_dev_spec_attr_grp(up);
3159 up->fcr = uart_config[up->port.type].fcr;
3160 }
3161
3162 static int
serial8250_verify_port(struct uart_port * port,struct serial_struct * ser)3163 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
3164 {
3165 if (ser->irq >= irq_get_nr_irqs() || ser->irq < 0 ||
3166 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
3167 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
3168 ser->type == PORT_STARTECH)
3169 return -EINVAL;
3170 return 0;
3171 }
3172
serial8250_type(struct uart_port * port)3173 static const char *serial8250_type(struct uart_port *port)
3174 {
3175 int type = port->type;
3176
3177 if (type >= ARRAY_SIZE(uart_config))
3178 type = 0;
3179 return uart_config[type].name;
3180 }
3181
3182 static const struct uart_ops serial8250_pops = {
3183 .tx_empty = serial8250_tx_empty,
3184 .set_mctrl = serial8250_set_mctrl,
3185 .get_mctrl = serial8250_get_mctrl,
3186 .stop_tx = serial8250_stop_tx,
3187 .start_tx = serial8250_start_tx,
3188 .throttle = serial8250_throttle,
3189 .unthrottle = serial8250_unthrottle,
3190 .stop_rx = serial8250_stop_rx,
3191 .enable_ms = serial8250_enable_ms,
3192 .break_ctl = serial8250_break_ctl,
3193 .startup = serial8250_startup,
3194 .shutdown = serial8250_shutdown,
3195 .flush_buffer = serial8250_flush_buffer,
3196 .set_termios = serial8250_set_termios,
3197 .set_ldisc = serial8250_set_ldisc,
3198 .pm = serial8250_pm,
3199 .type = serial8250_type,
3200 .release_port = serial8250_release_port,
3201 .request_port = serial8250_request_port,
3202 .config_port = serial8250_config_port,
3203 .verify_port = serial8250_verify_port,
3204 #ifdef CONFIG_CONSOLE_POLL
3205 .poll_get_char = serial8250_get_poll_char,
3206 .poll_put_char = serial8250_put_poll_char,
3207 #endif
3208 };
3209
serial8250_init_port(struct uart_8250_port * up)3210 void serial8250_init_port(struct uart_8250_port *up)
3211 {
3212 struct uart_port *port = &up->port;
3213
3214 spin_lock_init(&port->lock);
3215 port->ctrl_id = 0;
3216 port->pm = NULL;
3217 port->ops = &serial8250_pops;
3218 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
3219
3220 up->cur_iotype = UPIO_UNKNOWN;
3221 }
3222 EXPORT_SYMBOL_GPL(serial8250_init_port);
3223
serial8250_set_defaults(struct uart_8250_port * up)3224 void serial8250_set_defaults(struct uart_8250_port *up)
3225 {
3226 struct uart_port *port = &up->port;
3227
3228 if (up->port.flags & UPF_FIXED_TYPE) {
3229 unsigned int type = up->port.type;
3230
3231 if (!up->port.fifosize)
3232 up->port.fifosize = uart_config[type].fifo_size;
3233 if (!up->tx_loadsz)
3234 up->tx_loadsz = uart_config[type].tx_loadsz;
3235 if (!up->capabilities)
3236 up->capabilities = uart_config[type].flags;
3237 }
3238
3239 set_io_from_upio(port);
3240
3241 /* default dma handlers */
3242 if (up->dma) {
3243 if (!up->dma->tx_dma)
3244 up->dma->tx_dma = serial8250_tx_dma;
3245 if (!up->dma->rx_dma)
3246 up->dma->rx_dma = serial8250_rx_dma;
3247 }
3248 }
3249 EXPORT_SYMBOL_GPL(serial8250_set_defaults);
3250
serial8250_fifo_wait_for_lsr_thre(struct uart_8250_port * up,struct nbcon_write_context * wctxt,unsigned int count)3251 void serial8250_fifo_wait_for_lsr_thre(struct uart_8250_port *up,
3252 struct nbcon_write_context *wctxt,
3253 unsigned int count)
3254 {
3255 unsigned int i;
3256
3257 /*
3258 * For console writing, enter/exit an unsafe section for each byte
3259 * in order to pass the ownership as quickly as possible if a higher
3260 * priority context wants ownership. Otherwise, an attempt to take
3261 * over the ownership might timeout. The new owner will wait for
3262 * UART_LSR_THRE before reusing the fifo.
3263 */
3264 for (i = 0; i < count; i++) {
3265 bool tx_ready;
3266
3267 if (wctxt && !nbcon_enter_unsafe(wctxt))
3268 return;
3269
3270 tx_ready = wait_for_lsr(up, UART_LSR_THRE);
3271
3272 if (wctxt)
3273 nbcon_exit_unsafe(wctxt);
3274
3275 if (tx_ready)
3276 break;
3277 }
3278 }
3279 EXPORT_SYMBOL_NS_GPL(serial8250_fifo_wait_for_lsr_thre, "SERIAL_8250");
3280
3281 #ifdef CONFIG_SERIAL_8250_CONSOLE
3282
serial8250_console_putchar(struct uart_port * port,unsigned char ch)3283 static void serial8250_console_putchar(struct uart_port *port, unsigned char ch)
3284 {
3285 struct uart_8250_port *up = up_to_u8250p(port);
3286
3287 serial_port_out(port, UART_TX, ch);
3288
3289 up->console_line_ended = (ch == '\n');
3290 }
3291
serial8250_console_wait_putchar(struct uart_port * port,unsigned char ch)3292 static void serial8250_console_wait_putchar(struct uart_port *port, unsigned char ch)
3293 {
3294 struct uart_8250_port *up = up_to_u8250p(port);
3295
3296 wait_for_xmitr(up, UART_LSR_THRE);
3297 serial8250_console_putchar(port, ch);
3298 }
3299
3300 /*
3301 * Restore serial console when h/w power-off detected
3302 */
serial8250_console_restore(struct uart_8250_port * up)3303 static void serial8250_console_restore(struct uart_8250_port *up)
3304 {
3305 struct uart_port *port = &up->port;
3306 struct ktermios termios;
3307 unsigned int baud, quot, frac = 0;
3308
3309 termios.c_cflag = port->cons->cflag;
3310 termios.c_ispeed = port->cons->ispeed;
3311 termios.c_ospeed = port->cons->ospeed;
3312 if (port->state->port.tty && termios.c_cflag == 0) {
3313 termios.c_cflag = port->state->port.tty->termios.c_cflag;
3314 termios.c_ispeed = port->state->port.tty->termios.c_ispeed;
3315 termios.c_ospeed = port->state->port.tty->termios.c_ospeed;
3316 }
3317
3318 baud = serial8250_get_baud_rate(port, &termios, NULL);
3319 quot = serial8250_get_divisor(port, baud, &frac);
3320
3321 serial8250_set_divisor(port, baud, quot, frac);
3322 serial_port_out(port, UART_LCR, up->lcr);
3323 serial8250_out_MCR(up, up->mcr | UART_MCR_DTR | UART_MCR_RTS);
3324 }
3325
3326 /*
3327 * Print a string to the serial port using the device FIFO
3328 *
3329 * It sends fifosize bytes and then waits for the fifo
3330 * to get empty.
3331 */
__serial8250_console_fifo_write(struct uart_8250_port * up,struct nbcon_write_context * wctxt,const char * s,unsigned int count)3332 static void __serial8250_console_fifo_write(struct uart_8250_port *up,
3333 struct nbcon_write_context *wctxt,
3334 const char *s, unsigned int count)
3335 {
3336 const char *end = s + count;
3337 unsigned int fifosize = up->tx_loadsz;
3338 struct uart_port *port = &up->port;
3339 unsigned int tx_count = 0;
3340 bool cr_sent = false;
3341 unsigned int i;
3342
3343 while (s != end) {
3344 /* Allow timeout for each byte of a possibly full FIFO */
3345 serial8250_fifo_wait_for_lsr_thre(up, wctxt, fifosize);
3346
3347 /*
3348 * Fill the FIFO. If a handover or takeover occurs, writing
3349 * must be aborted since the string data is no longer valid.
3350 */
3351 for (i = 0; i < fifosize && s != end; ++i) {
3352 if (!nbcon_enter_unsafe(wctxt))
3353 return;
3354
3355 if (*s == '\n' && !cr_sent) {
3356 serial8250_console_putchar(port, '\r');
3357 cr_sent = true;
3358 } else {
3359 serial8250_console_putchar(port, *s++);
3360 cr_sent = false;
3361 }
3362
3363 nbcon_exit_unsafe(wctxt);
3364 }
3365 tx_count = i;
3366 }
3367
3368 /*
3369 * Allow timeout for each byte written since the caller will only wait
3370 * for UART_LSR_BOTH_EMPTY using the timeout of a single character
3371 */
3372 serial8250_fifo_wait_for_lsr_thre(up, wctxt, tx_count);
3373 }
3374
serial8250_console_fifo_write(struct uart_8250_port * up,struct nbcon_write_context * wctxt)3375 static void serial8250_console_fifo_write(struct uart_8250_port *up,
3376 struct nbcon_write_context *wctxt)
3377 {
3378 __serial8250_console_fifo_write(up, wctxt, wctxt->outbuf, wctxt->len);
3379 }
3380
__serial8250_console_byte_write(struct uart_8250_port * up,struct nbcon_write_context * wctxt,const char * s,unsigned int count)3381 static void __serial8250_console_byte_write(struct uart_8250_port *up,
3382 struct nbcon_write_context *wctxt,
3383 const char *s, unsigned int count)
3384 {
3385 struct uart_port *port = &up->port;
3386 const char *end = s + count;
3387
3388 /*
3389 * Write out the message. If a handover or takeover occurs, writing
3390 * must be aborted since the string data is no longer valid.
3391 */
3392 while (s != end) {
3393 if (!nbcon_enter_unsafe(wctxt))
3394 return;
3395
3396 uart_console_write(port, s++, 1, serial8250_console_wait_putchar);
3397
3398 nbcon_exit_unsafe(wctxt);
3399 }
3400 }
3401
serial8250_console_byte_write(struct uart_8250_port * up,struct nbcon_write_context * wctxt)3402 static void serial8250_console_byte_write(struct uart_8250_port *up,
3403 struct nbcon_write_context *wctxt)
3404 {
3405 __serial8250_console_byte_write(up, wctxt, wctxt->outbuf, wctxt->len);
3406 }
3407
3408 /*
3409 * Print the console line using the appropriate variant. If ownership is lost
3410 * at any time during printing, the printing is aborted.
3411 */
__serial8250_console_write(struct uart_8250_port * up,struct nbcon_write_context * wctxt,bool use_fifo)3412 static void __serial8250_console_write(struct uart_8250_port *up,
3413 struct nbcon_write_context *wctxt,
3414 bool use_fifo)
3415 {
3416 /*
3417 * If the console printer did not fully output the previous line, it
3418 * must have been handed or taken over. Insert a newline in order to
3419 * maintain clean output.
3420 *
3421 * Braille consoles are an exception. The serial port is not used
3422 * for printk(). The driver is supposed to write exactly what it gets.
3423 */
3424 if (unlikely(!up->console_line_ended && !nbcon_write_context_is_braille(wctxt))) {
3425 if (use_fifo)
3426 __serial8250_console_fifo_write(up, wctxt, "\n", 1);
3427 else
3428 __serial8250_console_byte_write(up, wctxt, "\n", 1);
3429 }
3430
3431 if (use_fifo)
3432 serial8250_console_fifo_write(up, wctxt);
3433 else
3434 serial8250_console_byte_write(up, wctxt);
3435 }
3436
3437 /*
3438 * Print a string to the serial port trying not to disturb
3439 * any possible real use of the port...
3440 */
serial8250_console_write(struct uart_8250_port * up,struct nbcon_write_context * wctxt,bool is_atomic)3441 void serial8250_console_write(struct uart_8250_port *up,
3442 struct nbcon_write_context *wctxt,
3443 bool is_atomic)
3444 {
3445 struct uart_8250_em485 *em485 = up->em485;
3446 struct uart_port *port = &up->port;
3447 unsigned int ier;
3448 bool use_fifo;
3449
3450 if (!nbcon_enter_unsafe(wctxt))
3451 return;
3452
3453 /*
3454 * First, save the IER, then disable the interrupts. The special
3455 * variant to clear the IER is used because emergency and panic
3456 * printing is synchronized only by nbcon ownership without
3457 * holding the port lock.
3458 */
3459 ier = serial_port_in(port, UART_IER);
3460 __serial8250_clear_IER(up);
3461
3462 /* check scratch reg to see if port powered off during system sleep */
3463 if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) {
3464 serial8250_console_restore(up);
3465 up->canary = 0;
3466 }
3467
3468 if (em485) {
3469 if (em485->tx_stopped)
3470 up->rs485_start_tx(up, false);
3471 mdelay(port->rs485.delay_rts_before_send);
3472 }
3473
3474 use_fifo = (up->capabilities & UART_CAP_FIFO) &&
3475 /*
3476 * BCM283x requires to check the fifo
3477 * after each byte.
3478 */
3479 !(up->capabilities & UART_CAP_MINI) &&
3480 /*
3481 * tx_loadsz contains the transmit fifo size
3482 */
3483 up->tx_loadsz > 1 &&
3484 (up->fcr & UART_FCR_ENABLE_FIFO) &&
3485 port->state &&
3486 test_bit(TTY_PORT_INITIALIZED, &port->state->port.iflags) &&
3487 /*
3488 * After we put a data in the fifo, the controller will send
3489 * it regardless of the CTS state. Therefore, only use fifo
3490 * if we don't use control flow.
3491 */
3492 !uart_console_hwflow_active(&up->port);
3493
3494 nbcon_exit_unsafe(wctxt);
3495
3496 __serial8250_console_write(up, wctxt, use_fifo);
3497
3498 /*
3499 * Re-enter an unsafe section in order to perform final actions
3500 * (such as re-enabling interrupts). If ownership was lost, this
3501 * context must reacquire ownership.
3502 */
3503 while (!nbcon_enter_unsafe(wctxt))
3504 nbcon_reacquire_nobuf(wctxt);
3505
3506 /*
3507 * Finally, wait for transmitter to become empty
3508 * and restore the IER
3509 */
3510 wait_for_xmitr(up, UART_LSR_BOTH_EMPTY);
3511
3512 if (em485) {
3513 mdelay(port->rs485.delay_rts_after_send);
3514
3515 /* Toggle unsafe after possibly long delay */
3516 nbcon_exit_unsafe(wctxt);
3517 while (!nbcon_enter_unsafe(wctxt))
3518 nbcon_reacquire_nobuf(wctxt);
3519
3520 if (em485->tx_stopped)
3521 up->rs485_stop_tx(up, false);
3522 }
3523
3524 /* Toggle unsafe after possibly long delay */
3525 nbcon_exit_unsafe(wctxt);
3526 while (!nbcon_enter_unsafe(wctxt))
3527 nbcon_reacquire_nobuf(wctxt);
3528
3529 serial_port_out(port, UART_IER, ier);
3530
3531 /*
3532 * The receive handling will happen properly because the
3533 * receive ready bit will still be set; it is not cleared
3534 * on read. However, modem control will not, we must
3535 * call it if we have saved something in the saved flags
3536 * while processing with interrupts off.
3537 */
3538 if (up->msr_saved_flags) {
3539 if (is_atomic) {
3540 /*
3541 * For atomic, MSR handling must be deferred to
3542 * irq_work because this may be a context that does
3543 * not permit waking up tasks.
3544 *
3545 * But no irq_work may be queued when suspending.
3546 * In that case, the MSR handling will occur during
3547 * resume in serial8250_resume_port().
3548 */
3549 if (up->console_msr_work_allow)
3550 irq_work_queue(&up->console_msr_work);
3551 } else {
3552 serial8250_modem_status(up);
3553 }
3554 }
3555
3556 nbcon_exit_unsafe(wctxt);
3557 }
3558
probe_baud(struct uart_port * port)3559 static unsigned int probe_baud(struct uart_port *port)
3560 {
3561 unsigned char lcr, dll, dlm;
3562 unsigned int quot;
3563
3564 lcr = serial_port_in(port, UART_LCR);
3565 serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB);
3566 dll = serial_port_in(port, UART_DLL);
3567 dlm = serial_port_in(port, UART_DLM);
3568 serial_port_out(port, UART_LCR, lcr);
3569
3570 quot = (dlm << 8) | dll;
3571 return (port->uartclk / 16) / quot;
3572 }
3573
3574 /*
3575 * irq_work handler to perform modem control during console output.
3576 * Only triggered via ->write_atomic() callback because it may be
3577 * in a scheduler or NMI context, unable to wake tasks.
3578 */
console_msr_handler(struct irq_work * iwp)3579 static void console_msr_handler(struct irq_work *iwp)
3580 {
3581 struct uart_8250_port *up = container_of(iwp, struct uart_8250_port, console_msr_work);
3582 struct uart_port *port = &up->port;
3583
3584 guard(uart_port_lock)(port);
3585
3586 serial8250_modem_status(up);
3587 }
3588
serial8250_console_setup(struct uart_port * port,char * options,bool probe)3589 int serial8250_console_setup(struct uart_port *port, char *options, bool probe)
3590 {
3591 struct uart_8250_port *up = up_to_u8250p(port);
3592 int baud = 9600;
3593 int bits = 8;
3594 int parity = 'n';
3595 int flow = 'n';
3596 int ret;
3597
3598 if (!port->iobase && !port->membase)
3599 return -ENODEV;
3600
3601 up->console_line_ended = true;
3602 up->console_msr_work_allow = true;
3603 up->console_msr_work = IRQ_WORK_INIT_LAZY(console_msr_handler);
3604
3605 if (options)
3606 uart_parse_options(options, &baud, &parity, &bits, &flow);
3607 else if (probe)
3608 baud = probe_baud(port);
3609
3610 ret = uart_set_options(port, port->cons, baud, parity, bits, flow);
3611 if (ret)
3612 return ret;
3613
3614 /* Track user-specified console flow control. */
3615 uart_set_cons_flow_enabled(port, flow == 'r');
3616
3617 if (port->dev)
3618 pm_runtime_get_sync(port->dev);
3619
3620 return 0;
3621 }
3622
serial8250_console_exit(struct uart_port * port)3623 int serial8250_console_exit(struct uart_port *port)
3624 {
3625 struct uart_8250_port *up = up_to_u8250p(port);
3626
3627 irq_work_sync(&up->console_msr_work);
3628
3629 if (port->dev)
3630 pm_runtime_put_sync(port->dev);
3631
3632 return 0;
3633 }
3634
3635 #endif /* CONFIG_SERIAL_8250_CONSOLE */
3636
3637 MODULE_DESCRIPTION("Base port operations for 8250/16550-type serial ports");
3638 MODULE_LICENSE("GPL");
3639