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