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