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