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