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