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 !(up->port.flags & UPF_FULL_PROBE))
1015 return;
1016
1017 /*
1018 * Check for presence of the EFR when DLAB is set.
1019 * Only ST16C650V1 UARTs pass this test.
1020 */
1021 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1022 if (serial_in(up, UART_EFR) == 0) {
1023 serial_out(up, UART_EFR, 0xA8);
1024 if (serial_in(up, UART_EFR) != 0) {
1025 DEBUG_AUTOCONF("EFRv1 ");
1026 up->port.type = PORT_16650;
1027 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
1028 } else {
1029 serial_out(up, UART_LCR, 0);
1030 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
1031 UART_FCR7_64BYTE);
1032 status1 = serial_in(up, UART_IIR) >> 5;
1033 serial_out(up, UART_FCR, 0);
1034 serial_out(up, UART_LCR, 0);
1035
1036 if (status1 == 7)
1037 up->port.type = PORT_16550A_FSL64;
1038 else
1039 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
1040 }
1041 serial_out(up, UART_EFR, 0);
1042 return;
1043 }
1044
1045 /*
1046 * Maybe it requires 0xbf to be written to the LCR.
1047 * (other ST16C650V2 UARTs, TI16C752A, etc)
1048 */
1049 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1050 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
1051 DEBUG_AUTOCONF("EFRv2 ");
1052 autoconfig_has_efr(up);
1053 return;
1054 }
1055
1056 /*
1057 * Check for a National Semiconductor SuperIO chip.
1058 * Attempt to switch to bank 2, read the value of the LOOP bit
1059 * from EXCR1. Switch back to bank 0, change it in MCR. Then
1060 * switch back to bank 2, read it from EXCR1 again and check
1061 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
1062 */
1063 serial_out(up, UART_LCR, 0);
1064 status1 = serial8250_in_MCR(up);
1065 serial_out(up, UART_LCR, 0xE0);
1066 status2 = serial_in(up, 0x02); /* EXCR1 */
1067
1068 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
1069 serial_out(up, UART_LCR, 0);
1070 serial8250_out_MCR(up, status1 ^ UART_MCR_LOOP);
1071 serial_out(up, UART_LCR, 0xE0);
1072 status2 = serial_in(up, 0x02); /* EXCR1 */
1073 serial_out(up, UART_LCR, 0);
1074 serial8250_out_MCR(up, status1);
1075
1076 if ((status2 ^ status1) & UART_MCR_LOOP) {
1077 unsigned short quot;
1078
1079 serial_out(up, UART_LCR, 0xE0);
1080
1081 quot = serial_dl_read(up);
1082 quot <<= 3;
1083
1084 if (ns16550a_goto_highspeed(up))
1085 serial_dl_write(up, quot);
1086
1087 serial_out(up, UART_LCR, 0);
1088
1089 up->port.uartclk = 921600*16;
1090 up->port.type = PORT_NS16550A;
1091 up->capabilities |= UART_NATSEMI;
1092 return;
1093 }
1094 }
1095
1096 /*
1097 * No EFR. Try to detect a TI16750, which only sets bit 5 of
1098 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1099 * Try setting it with and without DLAB set. Cheap clones
1100 * set bit 5 without DLAB set.
1101 */
1102 serial_out(up, UART_LCR, 0);
1103 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1104 status1 = serial_in(up, UART_IIR) >> 5;
1105 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1106 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1107 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1108 status2 = serial_in(up, UART_IIR) >> 5;
1109 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1110 serial_out(up, UART_LCR, 0);
1111
1112 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1113
1114 if (status1 == 6 && status2 == 7) {
1115 up->port.type = PORT_16750;
1116 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1117 return;
1118 }
1119
1120 /*
1121 * Try writing and reading the UART_IER_UUE bit (b6).
1122 * If it works, this is probably one of the Xscale platform's
1123 * internal UARTs.
1124 * We're going to explicitly set the UUE bit to 0 before
1125 * trying to write and read a 1 just to make sure it's not
1126 * already a 1 and maybe locked there before we even start start.
1127 */
1128 iersave = serial_in(up, UART_IER);
1129 serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
1130 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1131 /*
1132 * OK it's in a known zero state, try writing and reading
1133 * without disturbing the current state of the other bits.
1134 */
1135 serial_out(up, UART_IER, iersave | UART_IER_UUE);
1136 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1137 /*
1138 * It's an Xscale.
1139 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1140 */
1141 DEBUG_AUTOCONF("Xscale ");
1142 up->port.type = PORT_XSCALE;
1143 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
1144 return;
1145 }
1146 } else {
1147 /*
1148 * If we got here we couldn't force the IER_UUE bit to 0.
1149 * Log it and continue.
1150 */
1151 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1152 }
1153 serial_out(up, UART_IER, iersave);
1154
1155 /*
1156 * We distinguish between 16550A and U6 16550A by counting
1157 * how many bytes are in the FIFO.
1158 */
1159 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1160 up->port.type = PORT_U6_16550A;
1161 up->capabilities |= UART_CAP_AFE;
1162 }
1163 }
1164
1165 /*
1166 * This routine is called by rs_init() to initialize a specific serial
1167 * port. It determines what type of UART chip this serial port is
1168 * using: 8250, 16450, 16550, 16550A. The important question is
1169 * whether or not this UART is a 16550A or not, since this will
1170 * determine whether or not we can use its FIFO features or not.
1171 */
autoconfig(struct uart_8250_port * up)1172 static void autoconfig(struct uart_8250_port *up)
1173 {
1174 unsigned char status1, scratch, scratch2, scratch3;
1175 unsigned char save_lcr, save_mcr;
1176 struct uart_port *port = &up->port;
1177 unsigned long flags;
1178 unsigned int old_capabilities;
1179
1180 if (!port->iobase && !port->mapbase && !port->membase)
1181 return;
1182
1183 DEBUG_AUTOCONF("%s: autoconf (0x%04lx, 0x%p): ",
1184 port->name, port->iobase, port->membase);
1185
1186 /*
1187 * We really do need global IRQs disabled here - we're going to
1188 * be frobbing the chips IRQ enable register to see if it exists.
1189 */
1190 spin_lock_irqsave(&port->lock, flags);
1191
1192 up->capabilities = 0;
1193 up->bugs = 0;
1194
1195 if (!(port->flags & UPF_BUGGY_UART)) {
1196 /*
1197 * Do a simple existence test first; if we fail this,
1198 * there's no point trying anything else.
1199 *
1200 * 0x80 is used as a nonsense port to prevent against
1201 * false positives due to ISA bus float. The
1202 * assumption is that 0x80 is a non-existent port;
1203 * which should be safe since include/asm/io.h also
1204 * makes this assumption.
1205 *
1206 * Note: this is safe as long as MCR bit 4 is clear
1207 * and the device is in "PC" mode.
1208 */
1209 scratch = serial_in(up, UART_IER);
1210 serial_out(up, UART_IER, 0);
1211 #ifdef __i386__
1212 outb(0xff, 0x080);
1213 #endif
1214 /*
1215 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1216 * 16C754B) allow only to modify them if an EFR bit is set.
1217 */
1218 scratch2 = serial_in(up, UART_IER) & 0x0f;
1219 serial_out(up, UART_IER, 0x0F);
1220 #ifdef __i386__
1221 outb(0, 0x080);
1222 #endif
1223 scratch3 = serial_in(up, UART_IER) & 0x0f;
1224 serial_out(up, UART_IER, scratch);
1225 if (scratch2 != 0 || scratch3 != 0x0F) {
1226 /*
1227 * We failed; there's nothing here
1228 */
1229 spin_unlock_irqrestore(&port->lock, flags);
1230 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1231 scratch2, scratch3);
1232 goto out;
1233 }
1234 }
1235
1236 save_mcr = serial8250_in_MCR(up);
1237 save_lcr = serial_in(up, UART_LCR);
1238
1239 /*
1240 * Check to see if a UART is really there. Certain broken
1241 * internal modems based on the Rockwell chipset fail this
1242 * test, because they apparently don't implement the loopback
1243 * test mode. So this test is skipped on the COM 1 through
1244 * COM 4 ports. This *should* be safe, since no board
1245 * manufacturer would be stupid enough to design a board
1246 * that conflicts with COM 1-4 --- we hope!
1247 */
1248 if (!(port->flags & UPF_SKIP_TEST)) {
1249 serial8250_out_MCR(up, UART_MCR_LOOP | 0x0A);
1250 status1 = serial_in(up, UART_MSR) & 0xF0;
1251 serial8250_out_MCR(up, save_mcr);
1252 if (status1 != 0x90) {
1253 spin_unlock_irqrestore(&port->lock, flags);
1254 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1255 status1);
1256 goto out;
1257 }
1258 }
1259
1260 /*
1261 * We're pretty sure there's a port here. Lets find out what
1262 * type of port it is. The IIR top two bits allows us to find
1263 * out if it's 8250 or 16450, 16550, 16550A or later. This
1264 * determines what we test for next.
1265 *
1266 * We also initialise the EFR (if any) to zero for later. The
1267 * EFR occupies the same register location as the FCR and IIR.
1268 */
1269 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1270 serial_out(up, UART_EFR, 0);
1271 serial_out(up, UART_LCR, 0);
1272
1273 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1274
1275 /* Assign this as it is to truncate any bits above 7. */
1276 scratch = serial_in(up, UART_IIR);
1277
1278 switch (scratch >> 6) {
1279 case 0:
1280 autoconfig_8250(up);
1281 break;
1282 case 1:
1283 port->type = PORT_UNKNOWN;
1284 break;
1285 case 2:
1286 port->type = PORT_16550;
1287 break;
1288 case 3:
1289 autoconfig_16550a(up);
1290 break;
1291 }
1292
1293 #ifdef CONFIG_SERIAL_8250_RSA
1294 /*
1295 * Only probe for RSA ports if we got the region.
1296 */
1297 if (port->type == PORT_16550A && up->probe & UART_PROBE_RSA &&
1298 __enable_rsa(up))
1299 port->type = PORT_RSA;
1300 #endif
1301
1302 serial_out(up, UART_LCR, save_lcr);
1303
1304 port->fifosize = uart_config[up->port.type].fifo_size;
1305 old_capabilities = up->capabilities;
1306 up->capabilities = uart_config[port->type].flags;
1307 up->tx_loadsz = uart_config[port->type].tx_loadsz;
1308
1309 if (port->type == PORT_UNKNOWN)
1310 goto out_lock;
1311
1312 /*
1313 * Reset the UART.
1314 */
1315 #ifdef CONFIG_SERIAL_8250_RSA
1316 if (port->type == PORT_RSA)
1317 serial_out(up, UART_RSA_FRR, 0);
1318 #endif
1319 serial8250_out_MCR(up, save_mcr);
1320 serial8250_clear_fifos(up);
1321 serial_in(up, UART_RX);
1322 if (up->capabilities & UART_CAP_UUE)
1323 serial_out(up, UART_IER, UART_IER_UUE);
1324 else
1325 serial_out(up, UART_IER, 0);
1326
1327 out_lock:
1328 spin_unlock_irqrestore(&port->lock, flags);
1329
1330 /*
1331 * Check if the device is a Fintek F81216A
1332 */
1333 if (port->type == PORT_16550A && port->iotype == UPIO_PORT)
1334 fintek_8250_probe(up);
1335
1336 if (up->capabilities != old_capabilities) {
1337 dev_warn(port->dev, "detected caps %08x should be %08x\n",
1338 old_capabilities, up->capabilities);
1339 }
1340 out:
1341 DEBUG_AUTOCONF("iir=%d ", scratch);
1342 DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
1343 }
1344
autoconfig_irq(struct uart_8250_port * up)1345 static void autoconfig_irq(struct uart_8250_port *up)
1346 {
1347 struct uart_port *port = &up->port;
1348 unsigned char save_mcr, save_ier;
1349 unsigned char save_ICP = 0;
1350 unsigned int ICP = 0;
1351 unsigned long irqs;
1352 int irq;
1353
1354 if (port->flags & UPF_FOURPORT) {
1355 ICP = (port->iobase & 0xfe0) | 0x1f;
1356 save_ICP = inb_p(ICP);
1357 outb_p(0x80, ICP);
1358 inb_p(ICP);
1359 }
1360
1361 if (uart_console(port))
1362 console_lock();
1363
1364 /* forget possible initially masked and pending IRQ */
1365 probe_irq_off(probe_irq_on());
1366 save_mcr = serial8250_in_MCR(up);
1367 save_ier = serial_in(up, UART_IER);
1368 serial8250_out_MCR(up, UART_MCR_OUT1 | UART_MCR_OUT2);
1369
1370 irqs = probe_irq_on();
1371 serial8250_out_MCR(up, 0);
1372 udelay(10);
1373 if (port->flags & UPF_FOURPORT) {
1374 serial8250_out_MCR(up, UART_MCR_DTR | UART_MCR_RTS);
1375 } else {
1376 serial8250_out_MCR(up,
1377 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1378 }
1379 serial_out(up, UART_IER, 0x0f); /* enable all intrs */
1380 serial_in(up, UART_LSR);
1381 serial_in(up, UART_RX);
1382 serial_in(up, UART_IIR);
1383 serial_in(up, UART_MSR);
1384 serial_out(up, UART_TX, 0xFF);
1385 udelay(20);
1386 irq = probe_irq_off(irqs);
1387
1388 serial8250_out_MCR(up, save_mcr);
1389 serial_out(up, UART_IER, save_ier);
1390
1391 if (port->flags & UPF_FOURPORT)
1392 outb_p(save_ICP, ICP);
1393
1394 if (uart_console(port))
1395 console_unlock();
1396
1397 port->irq = (irq > 0) ? irq : 0;
1398 }
1399
serial8250_stop_rx(struct uart_port * port)1400 static void serial8250_stop_rx(struct uart_port *port)
1401 {
1402 struct uart_8250_port *up = up_to_u8250p(port);
1403
1404 serial8250_rpm_get(up);
1405
1406 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1407 up->port.read_status_mask &= ~UART_LSR_DR;
1408 serial_port_out(port, UART_IER, up->ier);
1409
1410 serial8250_rpm_put(up);
1411 }
1412
1413 /**
1414 * serial8250_em485_stop_tx() - generic ->rs485_stop_tx() callback
1415 * @p: uart 8250 port
1416 *
1417 * Generic callback usable by 8250 uart drivers to stop rs485 transmission.
1418 */
serial8250_em485_stop_tx(struct uart_8250_port * p)1419 void serial8250_em485_stop_tx(struct uart_8250_port *p)
1420 {
1421 unsigned char mcr = serial8250_in_MCR(p);
1422
1423 if (p->port.rs485.flags & SER_RS485_RTS_AFTER_SEND)
1424 mcr |= UART_MCR_RTS;
1425 else
1426 mcr &= ~UART_MCR_RTS;
1427 serial8250_out_MCR(p, mcr);
1428
1429 /*
1430 * Empty the RX FIFO, we are not interested in anything
1431 * received during the half-duplex transmission.
1432 * Enable previously disabled RX interrupts.
1433 */
1434 if (!(p->port.rs485.flags & SER_RS485_RX_DURING_TX)) {
1435 serial8250_clear_and_reinit_fifos(p);
1436
1437 p->ier |= UART_IER_RLSI | UART_IER_RDI;
1438 serial_port_out(&p->port, UART_IER, p->ier);
1439 }
1440 }
1441 EXPORT_SYMBOL_GPL(serial8250_em485_stop_tx);
1442
serial8250_em485_handle_stop_tx(struct hrtimer * t)1443 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t)
1444 {
1445 struct uart_8250_em485 *em485;
1446 struct uart_8250_port *p;
1447 unsigned long flags;
1448
1449 em485 = container_of(t, struct uart_8250_em485, stop_tx_timer);
1450 p = em485->port;
1451
1452 serial8250_rpm_get(p);
1453 spin_lock_irqsave(&p->port.lock, flags);
1454 if (em485->active_timer == &em485->stop_tx_timer) {
1455 p->rs485_stop_tx(p);
1456 em485->active_timer = NULL;
1457 em485->tx_stopped = true;
1458 }
1459 spin_unlock_irqrestore(&p->port.lock, flags);
1460 serial8250_rpm_put(p);
1461 return HRTIMER_NORESTART;
1462 }
1463
start_hrtimer_ms(struct hrtimer * hrt,unsigned long msec)1464 static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec)
1465 {
1466 long sec = msec / 1000;
1467 long nsec = (msec % 1000) * 1000000;
1468 ktime_t t = ktime_set(sec, nsec);
1469
1470 hrtimer_start(hrt, t, HRTIMER_MODE_REL);
1471 }
1472
__stop_tx_rs485(struct uart_8250_port * p)1473 static void __stop_tx_rs485(struct uart_8250_port *p)
1474 {
1475 struct uart_8250_em485 *em485 = p->em485;
1476
1477 /*
1478 * rs485_stop_tx() is going to set RTS according to config
1479 * AND flush RX FIFO if required.
1480 */
1481 if (p->port.rs485.delay_rts_after_send > 0) {
1482 em485->active_timer = &em485->stop_tx_timer;
1483 start_hrtimer_ms(&em485->stop_tx_timer,
1484 p->port.rs485.delay_rts_after_send);
1485 } else {
1486 p->rs485_stop_tx(p);
1487 em485->active_timer = NULL;
1488 em485->tx_stopped = true;
1489 }
1490 }
1491
__do_stop_tx(struct uart_8250_port * p)1492 static inline void __do_stop_tx(struct uart_8250_port *p)
1493 {
1494 if (serial8250_clear_THRI(p))
1495 serial8250_rpm_put_tx(p);
1496 }
1497
__stop_tx(struct uart_8250_port * p)1498 static inline void __stop_tx(struct uart_8250_port *p)
1499 {
1500 struct uart_8250_em485 *em485 = p->em485;
1501
1502 if (em485) {
1503 unsigned char lsr = serial_in(p, UART_LSR);
1504 p->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1505
1506 /*
1507 * To provide required timeing and allow FIFO transfer,
1508 * __stop_tx_rs485() must be called only when both FIFO and
1509 * shift register are empty. It is for device driver to enable
1510 * interrupt on TEMT.
1511 */
1512 if ((lsr & BOTH_EMPTY) != BOTH_EMPTY)
1513 return;
1514
1515 __stop_tx_rs485(p);
1516 }
1517 __do_stop_tx(p);
1518 }
1519
serial8250_stop_tx(struct uart_port * port)1520 static void serial8250_stop_tx(struct uart_port *port)
1521 {
1522 struct uart_8250_port *up = up_to_u8250p(port);
1523
1524 serial8250_rpm_get(up);
1525 __stop_tx(up);
1526
1527 /*
1528 * We really want to stop the transmitter from sending.
1529 */
1530 if (port->type == PORT_16C950) {
1531 up->acr |= UART_ACR_TXDIS;
1532 serial_icr_write(up, UART_ACR, up->acr);
1533 }
1534 serial8250_rpm_put(up);
1535 }
1536
__start_tx(struct uart_port * port)1537 static inline void __start_tx(struct uart_port *port)
1538 {
1539 struct uart_8250_port *up = up_to_u8250p(port);
1540
1541 if (up->dma && !up->dma->tx_dma(up))
1542 return;
1543
1544 if (serial8250_set_THRI(up)) {
1545 if (up->bugs & UART_BUG_TXEN) {
1546 unsigned char lsr;
1547
1548 lsr = serial_in(up, UART_LSR);
1549 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1550 if (lsr & UART_LSR_THRE)
1551 serial8250_tx_chars(up);
1552 }
1553 }
1554
1555 /*
1556 * Re-enable the transmitter if we disabled it.
1557 */
1558 if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1559 up->acr &= ~UART_ACR_TXDIS;
1560 serial_icr_write(up, UART_ACR, up->acr);
1561 }
1562 }
1563
1564 /**
1565 * serial8250_em485_start_tx() - generic ->rs485_start_tx() callback
1566 * @up: uart 8250 port
1567 *
1568 * Generic callback usable by 8250 uart drivers to start rs485 transmission.
1569 * Assumes that setting the RTS bit in the MCR register means RTS is high.
1570 * (Some chips use inverse semantics.) Further assumes that reception is
1571 * stoppable by disabling the UART_IER_RDI interrupt. (Some chips set the
1572 * UART_LSR_DR bit even when UART_IER_RDI is disabled, foiling this approach.)
1573 */
serial8250_em485_start_tx(struct uart_8250_port * up)1574 void serial8250_em485_start_tx(struct uart_8250_port *up)
1575 {
1576 unsigned char mcr = serial8250_in_MCR(up);
1577
1578 if (!(up->port.rs485.flags & SER_RS485_RX_DURING_TX))
1579 serial8250_stop_rx(&up->port);
1580
1581 if (up->port.rs485.flags & SER_RS485_RTS_ON_SEND)
1582 mcr |= UART_MCR_RTS;
1583 else
1584 mcr &= ~UART_MCR_RTS;
1585 serial8250_out_MCR(up, mcr);
1586 }
1587 EXPORT_SYMBOL_GPL(serial8250_em485_start_tx);
1588
start_tx_rs485(struct uart_port * port)1589 static inline void start_tx_rs485(struct uart_port *port)
1590 {
1591 struct uart_8250_port *up = up_to_u8250p(port);
1592 struct uart_8250_em485 *em485 = up->em485;
1593
1594 /*
1595 * While serial8250_em485_handle_stop_tx() is a noop if
1596 * em485->active_timer != &em485->stop_tx_timer, it might happen that
1597 * the timer is still armed and triggers only after the current bunch of
1598 * chars is send and em485->active_timer == &em485->stop_tx_timer again.
1599 * So cancel the timer. There is still a theoretical race condition if
1600 * the timer is already running and only comes around to check for
1601 * em485->active_timer when &em485->stop_tx_timer is armed again.
1602 */
1603 if (em485->active_timer == &em485->stop_tx_timer)
1604 hrtimer_try_to_cancel(&em485->stop_tx_timer);
1605
1606 em485->active_timer = NULL;
1607
1608 if (em485->tx_stopped) {
1609 em485->tx_stopped = false;
1610
1611 up->rs485_start_tx(up);
1612
1613 if (up->port.rs485.delay_rts_before_send > 0) {
1614 em485->active_timer = &em485->start_tx_timer;
1615 start_hrtimer_ms(&em485->start_tx_timer,
1616 up->port.rs485.delay_rts_before_send);
1617 return;
1618 }
1619 }
1620
1621 __start_tx(port);
1622 }
1623
serial8250_em485_handle_start_tx(struct hrtimer * t)1624 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t)
1625 {
1626 struct uart_8250_em485 *em485;
1627 struct uart_8250_port *p;
1628 unsigned long flags;
1629
1630 em485 = container_of(t, struct uart_8250_em485, start_tx_timer);
1631 p = em485->port;
1632
1633 spin_lock_irqsave(&p->port.lock, flags);
1634 if (em485->active_timer == &em485->start_tx_timer) {
1635 __start_tx(&p->port);
1636 em485->active_timer = NULL;
1637 }
1638 spin_unlock_irqrestore(&p->port.lock, flags);
1639 return HRTIMER_NORESTART;
1640 }
1641
serial8250_start_tx(struct uart_port * port)1642 static void serial8250_start_tx(struct uart_port *port)
1643 {
1644 struct uart_8250_port *up = up_to_u8250p(port);
1645 struct uart_8250_em485 *em485 = up->em485;
1646
1647 serial8250_rpm_get_tx(up);
1648
1649 if (em485 &&
1650 em485->active_timer == &em485->start_tx_timer)
1651 return;
1652
1653 if (em485)
1654 start_tx_rs485(port);
1655 else
1656 __start_tx(port);
1657 }
1658
serial8250_throttle(struct uart_port * port)1659 static void serial8250_throttle(struct uart_port *port)
1660 {
1661 port->throttle(port);
1662 }
1663
serial8250_unthrottle(struct uart_port * port)1664 static void serial8250_unthrottle(struct uart_port *port)
1665 {
1666 port->unthrottle(port);
1667 }
1668
serial8250_disable_ms(struct uart_port * port)1669 static void serial8250_disable_ms(struct uart_port *port)
1670 {
1671 struct uart_8250_port *up = up_to_u8250p(port);
1672
1673 /* no MSR capabilities */
1674 if (up->bugs & UART_BUG_NOMSR)
1675 return;
1676
1677 mctrl_gpio_disable_ms(up->gpios);
1678
1679 up->ier &= ~UART_IER_MSI;
1680 serial_port_out(port, UART_IER, up->ier);
1681 }
1682
serial8250_enable_ms(struct uart_port * port)1683 static void serial8250_enable_ms(struct uart_port *port)
1684 {
1685 struct uart_8250_port *up = up_to_u8250p(port);
1686
1687 /* no MSR capabilities */
1688 if (up->bugs & UART_BUG_NOMSR)
1689 return;
1690
1691 mctrl_gpio_enable_ms(up->gpios);
1692
1693 up->ier |= UART_IER_MSI;
1694
1695 serial8250_rpm_get(up);
1696 serial_port_out(port, UART_IER, up->ier);
1697 serial8250_rpm_put(up);
1698 }
1699
serial8250_read_char(struct uart_8250_port * up,unsigned char lsr)1700 void serial8250_read_char(struct uart_8250_port *up, unsigned char lsr)
1701 {
1702 struct uart_port *port = &up->port;
1703 unsigned char ch;
1704 char flag = TTY_NORMAL;
1705
1706 if (likely(lsr & UART_LSR_DR))
1707 ch = serial_in(up, UART_RX);
1708 else
1709 /*
1710 * Intel 82571 has a Serial Over Lan device that will
1711 * set UART_LSR_BI without setting UART_LSR_DR when
1712 * it receives a break. To avoid reading from the
1713 * receive buffer without UART_LSR_DR bit set, we
1714 * just force the read character to be 0
1715 */
1716 ch = 0;
1717
1718 port->icount.rx++;
1719
1720 lsr |= up->lsr_saved_flags;
1721 up->lsr_saved_flags = 0;
1722
1723 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1724 if (lsr & UART_LSR_BI) {
1725 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1726 port->icount.brk++;
1727 /*
1728 * We do the SysRQ and SAK checking
1729 * here because otherwise the break
1730 * may get masked by ignore_status_mask
1731 * or read_status_mask.
1732 */
1733 if (uart_handle_break(port))
1734 return;
1735 } else if (lsr & UART_LSR_PE)
1736 port->icount.parity++;
1737 else if (lsr & UART_LSR_FE)
1738 port->icount.frame++;
1739 if (lsr & UART_LSR_OE)
1740 port->icount.overrun++;
1741
1742 /*
1743 * Mask off conditions which should be ignored.
1744 */
1745 lsr &= port->read_status_mask;
1746
1747 if (lsr & UART_LSR_BI) {
1748 dev_dbg(port->dev, "handling break\n");
1749 flag = TTY_BREAK;
1750 } else if (lsr & UART_LSR_PE)
1751 flag = TTY_PARITY;
1752 else if (lsr & UART_LSR_FE)
1753 flag = TTY_FRAME;
1754 }
1755 if (uart_prepare_sysrq_char(port, ch))
1756 return;
1757
1758 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
1759 }
1760 EXPORT_SYMBOL_GPL(serial8250_read_char);
1761
1762 /*
1763 * serial8250_rx_chars: processes according to the passed in LSR
1764 * value, and returns the remaining LSR bits not handled
1765 * by this Rx routine.
1766 */
serial8250_rx_chars(struct uart_8250_port * up,unsigned char lsr)1767 unsigned char serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
1768 {
1769 struct uart_port *port = &up->port;
1770 int max_count = 256;
1771
1772 do {
1773 serial8250_read_char(up, lsr);
1774 if (--max_count == 0)
1775 break;
1776 lsr = serial_in(up, UART_LSR);
1777 } while (lsr & (UART_LSR_DR | UART_LSR_BI));
1778
1779 tty_flip_buffer_push(&port->state->port);
1780 return lsr;
1781 }
1782 EXPORT_SYMBOL_GPL(serial8250_rx_chars);
1783
serial8250_tx_chars(struct uart_8250_port * up)1784 void serial8250_tx_chars(struct uart_8250_port *up)
1785 {
1786 struct uart_port *port = &up->port;
1787 struct circ_buf *xmit = &port->state->xmit;
1788 int count;
1789
1790 if (port->x_char) {
1791 uart_xchar_out(port, UART_TX);
1792 return;
1793 }
1794 if (uart_tx_stopped(port)) {
1795 serial8250_stop_tx(port);
1796 return;
1797 }
1798 if (uart_circ_empty(xmit)) {
1799 __stop_tx(up);
1800 return;
1801 }
1802
1803 count = up->tx_loadsz;
1804 do {
1805 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1806 if (up->bugs & UART_BUG_TXRACE) {
1807 /*
1808 * The Aspeed BMC virtual UARTs have a bug where data
1809 * may get stuck in the BMC's Tx FIFO from bursts of
1810 * writes on the APB interface.
1811 *
1812 * Delay back-to-back writes by a read cycle to avoid
1813 * stalling the VUART. Read a register that won't have
1814 * side-effects and discard the result.
1815 */
1816 serial_in(up, UART_SCR);
1817 }
1818 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1819 port->icount.tx++;
1820 if (uart_circ_empty(xmit))
1821 break;
1822 if ((up->capabilities & UART_CAP_HFIFO) &&
1823 (serial_in(up, UART_LSR) & BOTH_EMPTY) != BOTH_EMPTY)
1824 break;
1825 /* The BCM2835 MINI UART THRE bit is really a not-full bit. */
1826 if ((up->capabilities & UART_CAP_MINI) &&
1827 !(serial_in(up, UART_LSR) & UART_LSR_THRE))
1828 break;
1829 } while (--count > 0);
1830
1831 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1832 uart_write_wakeup(port);
1833
1834 /*
1835 * With RPM enabled, we have to wait until the FIFO is empty before the
1836 * HW can go idle. So we get here once again with empty FIFO and disable
1837 * the interrupt and RPM in __stop_tx()
1838 */
1839 if (uart_circ_empty(xmit) && !(up->capabilities & UART_CAP_RPM))
1840 __stop_tx(up);
1841 }
1842 EXPORT_SYMBOL_GPL(serial8250_tx_chars);
1843
1844 /* Caller holds uart port lock */
serial8250_modem_status(struct uart_8250_port * up)1845 unsigned int serial8250_modem_status(struct uart_8250_port *up)
1846 {
1847 struct uart_port *port = &up->port;
1848 unsigned int status = serial_in(up, UART_MSR);
1849
1850 status |= up->msr_saved_flags;
1851 up->msr_saved_flags = 0;
1852 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1853 port->state != NULL) {
1854 if (status & UART_MSR_TERI)
1855 port->icount.rng++;
1856 if (status & UART_MSR_DDSR)
1857 port->icount.dsr++;
1858 if (status & UART_MSR_DDCD)
1859 uart_handle_dcd_change(port, status & UART_MSR_DCD);
1860 if (status & UART_MSR_DCTS)
1861 uart_handle_cts_change(port, status & UART_MSR_CTS);
1862
1863 wake_up_interruptible(&port->state->port.delta_msr_wait);
1864 }
1865
1866 return status;
1867 }
1868 EXPORT_SYMBOL_GPL(serial8250_modem_status);
1869
handle_rx_dma(struct uart_8250_port * up,unsigned int iir)1870 static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir)
1871 {
1872 switch (iir & 0x3f) {
1873 case UART_IIR_RDI:
1874 if (!up->dma->rx_running)
1875 break;
1876 fallthrough;
1877 case UART_IIR_RLSI:
1878 case UART_IIR_RX_TIMEOUT:
1879 serial8250_rx_dma_flush(up);
1880 return true;
1881 }
1882 return up->dma->rx_dma(up);
1883 }
1884
1885 /*
1886 * This handles the interrupt from one port.
1887 */
serial8250_handle_irq(struct uart_port * port,unsigned int iir)1888 int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
1889 {
1890 unsigned char status;
1891 unsigned long flags;
1892 struct uart_8250_port *up = up_to_u8250p(port);
1893 struct tty_port *tport = &port->state->port;
1894 bool skip_rx = false;
1895
1896 if (iir & UART_IIR_NO_INT)
1897 return 0;
1898
1899 spin_lock_irqsave(&port->lock, flags);
1900
1901 status = serial_port_in(port, UART_LSR);
1902
1903 /*
1904 * If port is stopped and there are no error conditions in the
1905 * FIFO, then don't drain the FIFO, as this may lead to TTY buffer
1906 * overflow. Not servicing, RX FIFO would trigger auto HW flow
1907 * control when FIFO occupancy reaches preset threshold, thus
1908 * halting RX. This only works when auto HW flow control is
1909 * available.
1910 */
1911 if (!(status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS)) &&
1912 (port->status & (UPSTAT_AUTOCTS | UPSTAT_AUTORTS)) &&
1913 !(port->read_status_mask & UART_LSR_DR))
1914 skip_rx = true;
1915
1916 if (status & (UART_LSR_DR | UART_LSR_BI) && !skip_rx) {
1917 if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
1918 pm_wakeup_event(tport->tty->dev, 0);
1919 if (!up->dma || handle_rx_dma(up, iir))
1920 status = serial8250_rx_chars(up, status);
1921 }
1922 serial8250_modem_status(up);
1923 if ((!up->dma || up->dma->tx_err) && (status & UART_LSR_THRE) &&
1924 (up->ier & UART_IER_THRI))
1925 serial8250_tx_chars(up);
1926
1927 uart_unlock_and_check_sysrq(port, flags);
1928 return 1;
1929 }
1930 EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1931
serial8250_default_handle_irq(struct uart_port * port)1932 static int serial8250_default_handle_irq(struct uart_port *port)
1933 {
1934 struct uart_8250_port *up = up_to_u8250p(port);
1935 unsigned int iir;
1936 int ret;
1937
1938 serial8250_rpm_get(up);
1939
1940 iir = serial_port_in(port, UART_IIR);
1941 ret = serial8250_handle_irq(port, iir);
1942
1943 serial8250_rpm_put(up);
1944 return ret;
1945 }
1946
1947 /*
1948 * Newer 16550 compatible parts such as the SC16C650 & Altera 16550 Soft IP
1949 * have a programmable TX threshold that triggers the THRE interrupt in
1950 * the IIR register. In this case, the THRE interrupt indicates the FIFO
1951 * has space available. Load it up with tx_loadsz bytes.
1952 */
serial8250_tx_threshold_handle_irq(struct uart_port * port)1953 static int serial8250_tx_threshold_handle_irq(struct uart_port *port)
1954 {
1955 unsigned long flags;
1956 unsigned int iir = serial_port_in(port, UART_IIR);
1957
1958 /* TX Threshold IRQ triggered so load up FIFO */
1959 if ((iir & UART_IIR_ID) == UART_IIR_THRI) {
1960 struct uart_8250_port *up = up_to_u8250p(port);
1961
1962 spin_lock_irqsave(&port->lock, flags);
1963 serial8250_tx_chars(up);
1964 spin_unlock_irqrestore(&port->lock, flags);
1965 }
1966
1967 iir = serial_port_in(port, UART_IIR);
1968 return serial8250_handle_irq(port, iir);
1969 }
1970
serial8250_tx_empty(struct uart_port * port)1971 static unsigned int serial8250_tx_empty(struct uart_port *port)
1972 {
1973 struct uart_8250_port *up = up_to_u8250p(port);
1974 unsigned int result = 0;
1975 unsigned long flags;
1976 unsigned int lsr;
1977
1978 serial8250_rpm_get(up);
1979
1980 spin_lock_irqsave(&port->lock, flags);
1981 if (!serial8250_tx_dma_running(up)) {
1982 lsr = serial_port_in(port, UART_LSR);
1983 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1984
1985 if ((lsr & BOTH_EMPTY) == BOTH_EMPTY)
1986 result = TIOCSER_TEMT;
1987 }
1988 spin_unlock_irqrestore(&port->lock, flags);
1989
1990 serial8250_rpm_put(up);
1991
1992 return result;
1993 }
1994
serial8250_do_get_mctrl(struct uart_port * port)1995 unsigned int serial8250_do_get_mctrl(struct uart_port *port)
1996 {
1997 struct uart_8250_port *up = up_to_u8250p(port);
1998 unsigned int status;
1999 unsigned int val;
2000
2001 serial8250_rpm_get(up);
2002 status = serial8250_modem_status(up);
2003 serial8250_rpm_put(up);
2004
2005 val = serial8250_MSR_to_TIOCM(status);
2006 if (up->gpios)
2007 return mctrl_gpio_get(up->gpios, &val);
2008
2009 return val;
2010 }
2011 EXPORT_SYMBOL_GPL(serial8250_do_get_mctrl);
2012
serial8250_get_mctrl(struct uart_port * port)2013 static unsigned int serial8250_get_mctrl(struct uart_port *port)
2014 {
2015 if (port->get_mctrl)
2016 return port->get_mctrl(port);
2017 return serial8250_do_get_mctrl(port);
2018 }
2019
serial8250_do_set_mctrl(struct uart_port * port,unsigned int mctrl)2020 void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl)
2021 {
2022 struct uart_8250_port *up = up_to_u8250p(port);
2023 unsigned char mcr;
2024
2025 mcr = serial8250_TIOCM_to_MCR(mctrl);
2026
2027 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
2028
2029 serial8250_out_MCR(up, mcr);
2030 }
2031 EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);
2032
serial8250_set_mctrl(struct uart_port * port,unsigned int mctrl)2033 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
2034 {
2035 if (port->rs485.flags & SER_RS485_ENABLED)
2036 return;
2037
2038 if (port->set_mctrl)
2039 port->set_mctrl(port, mctrl);
2040 else
2041 serial8250_do_set_mctrl(port, mctrl);
2042 }
2043
serial8250_break_ctl(struct uart_port * port,int break_state)2044 static void serial8250_break_ctl(struct uart_port *port, int break_state)
2045 {
2046 struct uart_8250_port *up = up_to_u8250p(port);
2047 unsigned long flags;
2048
2049 serial8250_rpm_get(up);
2050 spin_lock_irqsave(&port->lock, flags);
2051 if (break_state == -1)
2052 up->lcr |= UART_LCR_SBC;
2053 else
2054 up->lcr &= ~UART_LCR_SBC;
2055 serial_port_out(port, UART_LCR, up->lcr);
2056 spin_unlock_irqrestore(&port->lock, flags);
2057 serial8250_rpm_put(up);
2058 }
2059
2060 /*
2061 * Wait for transmitter & holding register to empty
2062 */
wait_for_xmitr(struct uart_8250_port * up,int bits)2063 static void wait_for_xmitr(struct uart_8250_port *up, int bits)
2064 {
2065 unsigned int status, tmout = 10000;
2066
2067 /* Wait up to 10ms for the character(s) to be sent. */
2068 for (;;) {
2069 status = serial_in(up, UART_LSR);
2070
2071 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
2072
2073 if ((status & bits) == bits)
2074 break;
2075 if (--tmout == 0)
2076 break;
2077 udelay(1);
2078 touch_nmi_watchdog();
2079 }
2080
2081 /* Wait up to 1s for flow control if necessary */
2082 if (up->port.flags & UPF_CONS_FLOW) {
2083 for (tmout = 1000000; tmout; tmout--) {
2084 unsigned int msr = serial_in(up, UART_MSR);
2085 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
2086 if (msr & UART_MSR_CTS)
2087 break;
2088 udelay(1);
2089 touch_nmi_watchdog();
2090 }
2091 }
2092 }
2093
2094 #ifdef CONFIG_CONSOLE_POLL
2095 /*
2096 * Console polling routines for writing and reading from the uart while
2097 * in an interrupt or debug context.
2098 */
2099
serial8250_get_poll_char(struct uart_port * port)2100 static int serial8250_get_poll_char(struct uart_port *port)
2101 {
2102 struct uart_8250_port *up = up_to_u8250p(port);
2103 unsigned char lsr;
2104 int status;
2105
2106 serial8250_rpm_get(up);
2107
2108 lsr = serial_port_in(port, UART_LSR);
2109
2110 if (!(lsr & UART_LSR_DR)) {
2111 status = NO_POLL_CHAR;
2112 goto out;
2113 }
2114
2115 status = serial_port_in(port, UART_RX);
2116 out:
2117 serial8250_rpm_put(up);
2118 return status;
2119 }
2120
2121
serial8250_put_poll_char(struct uart_port * port,unsigned char c)2122 static void serial8250_put_poll_char(struct uart_port *port,
2123 unsigned char c)
2124 {
2125 unsigned int ier;
2126 struct uart_8250_port *up = up_to_u8250p(port);
2127
2128 serial8250_rpm_get(up);
2129 /*
2130 * First save the IER then disable the interrupts
2131 */
2132 ier = serial_port_in(port, UART_IER);
2133 if (up->capabilities & UART_CAP_UUE)
2134 serial_port_out(port, UART_IER, UART_IER_UUE);
2135 else
2136 serial_port_out(port, UART_IER, 0);
2137
2138 wait_for_xmitr(up, BOTH_EMPTY);
2139 /*
2140 * Send the character out.
2141 */
2142 serial_port_out(port, UART_TX, c);
2143
2144 /*
2145 * Finally, wait for transmitter to become empty
2146 * and restore the IER
2147 */
2148 wait_for_xmitr(up, BOTH_EMPTY);
2149 serial_port_out(port, UART_IER, ier);
2150 serial8250_rpm_put(up);
2151 }
2152
2153 #endif /* CONFIG_CONSOLE_POLL */
2154
serial8250_do_startup(struct uart_port * port)2155 int serial8250_do_startup(struct uart_port *port)
2156 {
2157 struct uart_8250_port *up = up_to_u8250p(port);
2158 unsigned long flags;
2159 unsigned char lsr, iir;
2160 int retval;
2161
2162 if (!port->fifosize)
2163 port->fifosize = uart_config[port->type].fifo_size;
2164 if (!up->tx_loadsz)
2165 up->tx_loadsz = uart_config[port->type].tx_loadsz;
2166 if (!up->capabilities)
2167 up->capabilities = uart_config[port->type].flags;
2168 up->mcr = 0;
2169
2170 if (port->iotype != up->cur_iotype)
2171 set_io_from_upio(port);
2172
2173 serial8250_rpm_get(up);
2174 if (port->type == PORT_16C950) {
2175 /* Wake up and initialize UART */
2176 up->acr = 0;
2177 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2178 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2179 serial_port_out(port, UART_IER, 0);
2180 serial_port_out(port, UART_LCR, 0);
2181 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2182 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2183 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2184 serial_port_out(port, UART_LCR, 0);
2185 }
2186
2187 if (port->type == PORT_DA830) {
2188 /* Reset the port */
2189 serial_port_out(port, UART_IER, 0);
2190 serial_port_out(port, UART_DA830_PWREMU_MGMT, 0);
2191 mdelay(10);
2192
2193 /* Enable Tx, Rx and free run mode */
2194 serial_port_out(port, UART_DA830_PWREMU_MGMT,
2195 UART_DA830_PWREMU_MGMT_UTRST |
2196 UART_DA830_PWREMU_MGMT_URRST |
2197 UART_DA830_PWREMU_MGMT_FREE);
2198 }
2199
2200 if (port->type == PORT_NPCM) {
2201 /*
2202 * Nuvoton calls the scratch register 'UART_TOR' (timeout
2203 * register). Enable it, and set TIOC (timeout interrupt
2204 * comparator) to be 0x20 for correct operation.
2205 */
2206 serial_port_out(port, UART_NPCM_TOR, UART_NPCM_TOIE | 0x20);
2207 }
2208
2209 #ifdef CONFIG_SERIAL_8250_RSA
2210 /*
2211 * If this is an RSA port, see if we can kick it up to the
2212 * higher speed clock.
2213 */
2214 enable_rsa(up);
2215 #endif
2216
2217 /*
2218 * Clear the FIFO buffers and disable them.
2219 * (they will be reenabled in set_termios())
2220 */
2221 serial8250_clear_fifos(up);
2222
2223 /*
2224 * Clear the interrupt registers.
2225 */
2226 serial_port_in(port, UART_LSR);
2227 serial_port_in(port, UART_RX);
2228 serial_port_in(port, UART_IIR);
2229 serial_port_in(port, UART_MSR);
2230
2231 /*
2232 * At this point, there's no way the LSR could still be 0xff;
2233 * if it is, then bail out, because there's likely no UART
2234 * here.
2235 */
2236 if (!(port->flags & UPF_BUGGY_UART) &&
2237 (serial_port_in(port, UART_LSR) == 0xff)) {
2238 dev_info_ratelimited(port->dev, "LSR safety check engaged!\n");
2239 retval = -ENODEV;
2240 goto out;
2241 }
2242
2243 /*
2244 * For a XR16C850, we need to set the trigger levels
2245 */
2246 if (port->type == PORT_16850) {
2247 unsigned char fctr;
2248
2249 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
2250
2251 fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2252 serial_port_out(port, UART_FCTR,
2253 fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2254 serial_port_out(port, UART_TRG, UART_TRG_96);
2255 serial_port_out(port, UART_FCTR,
2256 fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2257 serial_port_out(port, UART_TRG, UART_TRG_96);
2258
2259 serial_port_out(port, UART_LCR, 0);
2260 }
2261
2262 /*
2263 * For the Altera 16550 variants, set TX threshold trigger level.
2264 */
2265 if (((port->type == PORT_ALTR_16550_F32) ||
2266 (port->type == PORT_ALTR_16550_F64) ||
2267 (port->type == PORT_ALTR_16550_F128)) && (port->fifosize > 1)) {
2268 /* Bounds checking of TX threshold (valid 0 to fifosize-2) */
2269 if ((up->tx_loadsz < 2) || (up->tx_loadsz > port->fifosize)) {
2270 dev_err(port->dev, "TX FIFO Threshold errors, skipping\n");
2271 } else {
2272 serial_port_out(port, UART_ALTR_AFR,
2273 UART_ALTR_EN_TXFIFO_LW);
2274 serial_port_out(port, UART_ALTR_TX_LOW,
2275 port->fifosize - up->tx_loadsz);
2276 port->handle_irq = serial8250_tx_threshold_handle_irq;
2277 }
2278 }
2279
2280 /* Check if we need to have shared IRQs */
2281 if (port->irq && (up->port.flags & UPF_SHARE_IRQ))
2282 up->port.irqflags |= IRQF_SHARED;
2283
2284 retval = up->ops->setup_irq(up);
2285 if (retval)
2286 goto out;
2287
2288 if (port->irq && !(up->port.flags & UPF_NO_THRE_TEST)) {
2289 unsigned char iir1;
2290
2291 if (port->irqflags & IRQF_SHARED)
2292 disable_irq_nosync(port->irq);
2293
2294 /*
2295 * Test for UARTs that do not reassert THRE when the
2296 * transmitter is idle and the interrupt has already
2297 * been cleared. Real 16550s should always reassert
2298 * this interrupt whenever the transmitter is idle and
2299 * the interrupt is enabled. Delays are necessary to
2300 * allow register changes to become visible.
2301 */
2302 spin_lock_irqsave(&port->lock, flags);
2303
2304 wait_for_xmitr(up, UART_LSR_THRE);
2305 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2306 udelay(1); /* allow THRE to set */
2307 iir1 = serial_port_in(port, UART_IIR);
2308 serial_port_out(port, UART_IER, 0);
2309 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2310 udelay(1); /* allow a working UART time to re-assert THRE */
2311 iir = serial_port_in(port, UART_IIR);
2312 serial_port_out(port, UART_IER, 0);
2313
2314 spin_unlock_irqrestore(&port->lock, flags);
2315
2316 if (port->irqflags & IRQF_SHARED)
2317 enable_irq(port->irq);
2318
2319 /*
2320 * If the interrupt is not reasserted, or we otherwise
2321 * don't trust the iir, setup a timer to kick the UART
2322 * on a regular basis.
2323 */
2324 if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
2325 up->port.flags & UPF_BUG_THRE) {
2326 up->bugs |= UART_BUG_THRE;
2327 }
2328 }
2329
2330 up->ops->setup_timer(up);
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 (!(c_cflag & PARODD))
2583 cval |= UART_LCR_EPAR;
2584 #ifdef CMSPAR
2585 if (c_cflag & CMSPAR)
2586 cval |= UART_LCR_SPAR;
2587 #endif
2588
2589 return cval;
2590 }
2591
serial8250_do_set_divisor(struct uart_port * port,unsigned int baud,unsigned int quot,unsigned int quot_frac)2592 void serial8250_do_set_divisor(struct uart_port *port, unsigned int baud,
2593 unsigned int quot, unsigned int quot_frac)
2594 {
2595 struct uart_8250_port *up = up_to_u8250p(port);
2596
2597 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2598 if (is_omap1510_8250(up)) {
2599 if (baud == 115200) {
2600 quot = 1;
2601 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
2602 } else
2603 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
2604 }
2605
2606 /*
2607 * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
2608 * otherwise just set DLAB
2609 */
2610 if (up->capabilities & UART_NATSEMI)
2611 serial_port_out(port, UART_LCR, 0xe0);
2612 else
2613 serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
2614
2615 serial_dl_write(up, quot);
2616 }
2617 EXPORT_SYMBOL_GPL(serial8250_do_set_divisor);
2618
serial8250_set_divisor(struct uart_port * port,unsigned int baud,unsigned int quot,unsigned int quot_frac)2619 static void serial8250_set_divisor(struct uart_port *port, unsigned int baud,
2620 unsigned int quot, unsigned int quot_frac)
2621 {
2622 if (port->set_divisor)
2623 port->set_divisor(port, baud, quot, quot_frac);
2624 else
2625 serial8250_do_set_divisor(port, baud, quot, quot_frac);
2626 }
2627
serial8250_get_baud_rate(struct uart_port * port,struct ktermios * termios,struct ktermios * old)2628 static unsigned int serial8250_get_baud_rate(struct uart_port *port,
2629 struct ktermios *termios,
2630 struct ktermios *old)
2631 {
2632 unsigned int tolerance = port->uartclk / 100;
2633 unsigned int min;
2634 unsigned int max;
2635
2636 /*
2637 * Handle magic divisors for baud rates above baud_base on SMSC
2638 * Super I/O chips. Enable custom rates of clk/4 and clk/8, but
2639 * disable divisor values beyond 32767, which are unavailable.
2640 */
2641 if (port->flags & UPF_MAGIC_MULTIPLIER) {
2642 min = port->uartclk / 16 / UART_DIV_MAX >> 1;
2643 max = (port->uartclk + tolerance) / 4;
2644 } else {
2645 min = port->uartclk / 16 / UART_DIV_MAX;
2646 max = (port->uartclk + tolerance) / 16;
2647 }
2648
2649 /*
2650 * Ask the core to calculate the divisor for us.
2651 * Allow 1% tolerance at the upper limit so uart clks marginally
2652 * slower than nominal still match standard baud rates without
2653 * causing transmission errors.
2654 */
2655 return uart_get_baud_rate(port, termios, old, min, max);
2656 }
2657
2658 /*
2659 * Note in order to avoid the tty port mutex deadlock don't use the next method
2660 * within the uart port callbacks. Primarily it's supposed to be utilized to
2661 * handle a sudden reference clock rate change.
2662 */
serial8250_update_uartclk(struct uart_port * port,unsigned int uartclk)2663 void serial8250_update_uartclk(struct uart_port *port, unsigned int uartclk)
2664 {
2665 struct uart_8250_port *up = up_to_u8250p(port);
2666 struct tty_port *tport = &port->state->port;
2667 unsigned int baud, quot, frac = 0;
2668 struct ktermios *termios;
2669 struct tty_struct *tty;
2670 unsigned long flags;
2671
2672 tty = tty_port_tty_get(tport);
2673 if (!tty) {
2674 mutex_lock(&tport->mutex);
2675 port->uartclk = uartclk;
2676 mutex_unlock(&tport->mutex);
2677 return;
2678 }
2679
2680 down_write(&tty->termios_rwsem);
2681 mutex_lock(&tport->mutex);
2682
2683 if (port->uartclk == uartclk)
2684 goto out_lock;
2685
2686 port->uartclk = uartclk;
2687
2688 if (!tty_port_initialized(tport))
2689 goto out_lock;
2690
2691 termios = &tty->termios;
2692
2693 baud = serial8250_get_baud_rate(port, termios, NULL);
2694 quot = serial8250_get_divisor(port, baud, &frac);
2695
2696 serial8250_rpm_get(up);
2697 spin_lock_irqsave(&port->lock, flags);
2698
2699 uart_update_timeout(port, termios->c_cflag, baud);
2700
2701 serial8250_set_divisor(port, baud, quot, frac);
2702 serial_port_out(port, UART_LCR, up->lcr);
2703
2704 spin_unlock_irqrestore(&port->lock, flags);
2705 serial8250_rpm_put(up);
2706
2707 out_lock:
2708 mutex_unlock(&tport->mutex);
2709 up_write(&tty->termios_rwsem);
2710 tty_kref_put(tty);
2711 }
2712 EXPORT_SYMBOL_GPL(serial8250_update_uartclk);
2713
2714 void
serial8250_do_set_termios(struct uart_port * port,struct ktermios * termios,struct ktermios * old)2715 serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2716 struct ktermios *old)
2717 {
2718 struct uart_8250_port *up = up_to_u8250p(port);
2719 unsigned char cval;
2720 unsigned long flags;
2721 unsigned int baud, quot, frac = 0;
2722
2723 if (up->capabilities & UART_CAP_MINI) {
2724 termios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CMSPAR);
2725 if ((termios->c_cflag & CSIZE) == CS5 ||
2726 (termios->c_cflag & CSIZE) == CS6)
2727 termios->c_cflag = (termios->c_cflag & ~CSIZE) | CS7;
2728 }
2729 cval = serial8250_compute_lcr(up, termios->c_cflag);
2730
2731 baud = serial8250_get_baud_rate(port, termios, old);
2732 quot = serial8250_get_divisor(port, baud, &frac);
2733
2734 /*
2735 * Ok, we're now changing the port state. Do it with
2736 * interrupts disabled.
2737 */
2738 serial8250_rpm_get(up);
2739 spin_lock_irqsave(&port->lock, flags);
2740
2741 up->lcr = cval; /* Save computed LCR */
2742
2743 if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
2744 if (baud < 2400 && !up->dma) {
2745 up->fcr &= ~UART_FCR_TRIGGER_MASK;
2746 up->fcr |= UART_FCR_TRIGGER_1;
2747 }
2748 }
2749
2750 /*
2751 * MCR-based auto flow control. When AFE is enabled, RTS will be
2752 * deasserted when the receive FIFO contains more characters than
2753 * the trigger, or the MCR RTS bit is cleared.
2754 */
2755 if (up->capabilities & UART_CAP_AFE) {
2756 up->mcr &= ~UART_MCR_AFE;
2757 if (termios->c_cflag & CRTSCTS)
2758 up->mcr |= UART_MCR_AFE;
2759 }
2760
2761 /*
2762 * Update the per-port timeout.
2763 */
2764 uart_update_timeout(port, termios->c_cflag, baud);
2765
2766 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2767 if (termios->c_iflag & INPCK)
2768 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2769 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2770 port->read_status_mask |= UART_LSR_BI;
2771
2772 /*
2773 * Characteres to ignore
2774 */
2775 port->ignore_status_mask = 0;
2776 if (termios->c_iflag & IGNPAR)
2777 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2778 if (termios->c_iflag & IGNBRK) {
2779 port->ignore_status_mask |= UART_LSR_BI;
2780 /*
2781 * If we're ignoring parity and break indicators,
2782 * ignore overruns too (for real raw support).
2783 */
2784 if (termios->c_iflag & IGNPAR)
2785 port->ignore_status_mask |= UART_LSR_OE;
2786 }
2787
2788 /*
2789 * ignore all characters if CREAD is not set
2790 */
2791 if ((termios->c_cflag & CREAD) == 0)
2792 port->ignore_status_mask |= UART_LSR_DR;
2793
2794 /*
2795 * CTS flow control flag and modem status interrupts
2796 */
2797 up->ier &= ~UART_IER_MSI;
2798 if (!(up->bugs & UART_BUG_NOMSR) &&
2799 UART_ENABLE_MS(&up->port, termios->c_cflag))
2800 up->ier |= UART_IER_MSI;
2801 if (up->capabilities & UART_CAP_UUE)
2802 up->ier |= UART_IER_UUE;
2803 if (up->capabilities & UART_CAP_RTOIE)
2804 up->ier |= UART_IER_RTOIE;
2805
2806 serial_port_out(port, UART_IER, up->ier);
2807
2808 if (up->capabilities & UART_CAP_EFR) {
2809 unsigned char efr = 0;
2810 /*
2811 * TI16C752/Startech hardware flow control. FIXME:
2812 * - TI16C752 requires control thresholds to be set.
2813 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2814 */
2815 if (termios->c_cflag & CRTSCTS)
2816 efr |= UART_EFR_CTS;
2817
2818 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2819 if (port->flags & UPF_EXAR_EFR)
2820 serial_port_out(port, UART_XR_EFR, efr);
2821 else
2822 serial_port_out(port, UART_EFR, efr);
2823 }
2824
2825 serial8250_set_divisor(port, baud, quot, frac);
2826
2827 /*
2828 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2829 * is written without DLAB set, this mode will be disabled.
2830 */
2831 if (port->type == PORT_16750)
2832 serial_port_out(port, UART_FCR, up->fcr);
2833
2834 serial_port_out(port, UART_LCR, up->lcr); /* reset DLAB */
2835 if (port->type != PORT_16750) {
2836 /* emulated UARTs (Lucent Venus 167x) need two steps */
2837 if (up->fcr & UART_FCR_ENABLE_FIFO)
2838 serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
2839 serial_port_out(port, UART_FCR, up->fcr); /* set fcr */
2840 }
2841 serial8250_set_mctrl(port, port->mctrl);
2842 spin_unlock_irqrestore(&port->lock, flags);
2843 serial8250_rpm_put(up);
2844
2845 /* Don't rewrite B0 */
2846 if (tty_termios_baud_rate(termios))
2847 tty_termios_encode_baud_rate(termios, baud, baud);
2848 }
2849 EXPORT_SYMBOL(serial8250_do_set_termios);
2850
2851 static void
serial8250_set_termios(struct uart_port * port,struct ktermios * termios,struct ktermios * old)2852 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2853 struct ktermios *old)
2854 {
2855 if (port->set_termios)
2856 port->set_termios(port, termios, old);
2857 else
2858 serial8250_do_set_termios(port, termios, old);
2859 }
2860
serial8250_do_set_ldisc(struct uart_port * port,struct ktermios * termios)2861 void serial8250_do_set_ldisc(struct uart_port *port, struct ktermios *termios)
2862 {
2863 if (termios->c_line == N_PPS) {
2864 port->flags |= UPF_HARDPPS_CD;
2865 spin_lock_irq(&port->lock);
2866 serial8250_enable_ms(port);
2867 spin_unlock_irq(&port->lock);
2868 } else {
2869 port->flags &= ~UPF_HARDPPS_CD;
2870 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2871 spin_lock_irq(&port->lock);
2872 serial8250_disable_ms(port);
2873 spin_unlock_irq(&port->lock);
2874 }
2875 }
2876 }
2877 EXPORT_SYMBOL_GPL(serial8250_do_set_ldisc);
2878
2879 static void
serial8250_set_ldisc(struct uart_port * port,struct ktermios * termios)2880 serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios)
2881 {
2882 if (port->set_ldisc)
2883 port->set_ldisc(port, termios);
2884 else
2885 serial8250_do_set_ldisc(port, termios);
2886 }
2887
serial8250_do_pm(struct uart_port * port,unsigned int state,unsigned int oldstate)2888 void serial8250_do_pm(struct uart_port *port, unsigned int state,
2889 unsigned int oldstate)
2890 {
2891 struct uart_8250_port *p = up_to_u8250p(port);
2892
2893 serial8250_set_sleep(p, state != 0);
2894 }
2895 EXPORT_SYMBOL(serial8250_do_pm);
2896
2897 static void
serial8250_pm(struct uart_port * port,unsigned int state,unsigned int oldstate)2898 serial8250_pm(struct uart_port *port, unsigned int state,
2899 unsigned int oldstate)
2900 {
2901 if (port->pm)
2902 port->pm(port, state, oldstate);
2903 else
2904 serial8250_do_pm(port, state, oldstate);
2905 }
2906
serial8250_port_size(struct uart_8250_port * pt)2907 static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2908 {
2909 if (pt->port.mapsize)
2910 return pt->port.mapsize;
2911 if (pt->port.iotype == UPIO_AU) {
2912 if (pt->port.type == PORT_RT2880)
2913 return 0x100;
2914 return 0x1000;
2915 }
2916 if (is_omap1_8250(pt))
2917 return 0x16 << pt->port.regshift;
2918
2919 return 8 << pt->port.regshift;
2920 }
2921
2922 /*
2923 * Resource handling.
2924 */
serial8250_request_std_resource(struct uart_8250_port * up)2925 static int serial8250_request_std_resource(struct uart_8250_port *up)
2926 {
2927 unsigned int size = serial8250_port_size(up);
2928 struct uart_port *port = &up->port;
2929 int ret = 0;
2930
2931 switch (port->iotype) {
2932 case UPIO_AU:
2933 case UPIO_TSI:
2934 case UPIO_MEM32:
2935 case UPIO_MEM32BE:
2936 case UPIO_MEM16:
2937 case UPIO_MEM:
2938 if (!port->mapbase) {
2939 ret = -EINVAL;
2940 break;
2941 }
2942
2943 if (!request_mem_region(port->mapbase, size, "serial")) {
2944 ret = -EBUSY;
2945 break;
2946 }
2947
2948 if (port->flags & UPF_IOREMAP) {
2949 port->membase = ioremap(port->mapbase, size);
2950 if (!port->membase) {
2951 release_mem_region(port->mapbase, size);
2952 ret = -ENOMEM;
2953 }
2954 }
2955 break;
2956
2957 case UPIO_HUB6:
2958 case UPIO_PORT:
2959 if (!request_region(port->iobase, size, "serial"))
2960 ret = -EBUSY;
2961 break;
2962 }
2963 return ret;
2964 }
2965
serial8250_release_std_resource(struct uart_8250_port * up)2966 static void serial8250_release_std_resource(struct uart_8250_port *up)
2967 {
2968 unsigned int size = serial8250_port_size(up);
2969 struct uart_port *port = &up->port;
2970
2971 switch (port->iotype) {
2972 case UPIO_AU:
2973 case UPIO_TSI:
2974 case UPIO_MEM32:
2975 case UPIO_MEM32BE:
2976 case UPIO_MEM16:
2977 case UPIO_MEM:
2978 if (!port->mapbase)
2979 break;
2980
2981 if (port->flags & UPF_IOREMAP) {
2982 iounmap(port->membase);
2983 port->membase = NULL;
2984 }
2985
2986 release_mem_region(port->mapbase, size);
2987 break;
2988
2989 case UPIO_HUB6:
2990 case UPIO_PORT:
2991 release_region(port->iobase, size);
2992 break;
2993 }
2994 }
2995
serial8250_release_port(struct uart_port * port)2996 static void serial8250_release_port(struct uart_port *port)
2997 {
2998 struct uart_8250_port *up = up_to_u8250p(port);
2999
3000 serial8250_release_std_resource(up);
3001 }
3002
serial8250_request_port(struct uart_port * port)3003 static int serial8250_request_port(struct uart_port *port)
3004 {
3005 struct uart_8250_port *up = up_to_u8250p(port);
3006
3007 return serial8250_request_std_resource(up);
3008 }
3009
fcr_get_rxtrig_bytes(struct uart_8250_port * up)3010 static int fcr_get_rxtrig_bytes(struct uart_8250_port *up)
3011 {
3012 const struct serial8250_config *conf_type = &uart_config[up->port.type];
3013 unsigned char bytes;
3014
3015 bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)];
3016
3017 return bytes ? bytes : -EOPNOTSUPP;
3018 }
3019
bytes_to_fcr_rxtrig(struct uart_8250_port * up,unsigned char bytes)3020 static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes)
3021 {
3022 const struct serial8250_config *conf_type = &uart_config[up->port.type];
3023 int i;
3024
3025 if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)])
3026 return -EOPNOTSUPP;
3027
3028 for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) {
3029 if (bytes < conf_type->rxtrig_bytes[i])
3030 /* Use the nearest lower value */
3031 return (--i) << UART_FCR_R_TRIG_SHIFT;
3032 }
3033
3034 return UART_FCR_R_TRIG_11;
3035 }
3036
do_get_rxtrig(struct tty_port * port)3037 static int do_get_rxtrig(struct tty_port *port)
3038 {
3039 struct uart_state *state = container_of(port, struct uart_state, port);
3040 struct uart_port *uport = state->uart_port;
3041 struct uart_8250_port *up = up_to_u8250p(uport);
3042
3043 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
3044 return -EINVAL;
3045
3046 return fcr_get_rxtrig_bytes(up);
3047 }
3048
do_serial8250_get_rxtrig(struct tty_port * port)3049 static int do_serial8250_get_rxtrig(struct tty_port *port)
3050 {
3051 int rxtrig_bytes;
3052
3053 mutex_lock(&port->mutex);
3054 rxtrig_bytes = do_get_rxtrig(port);
3055 mutex_unlock(&port->mutex);
3056
3057 return rxtrig_bytes;
3058 }
3059
rx_trig_bytes_show(struct device * dev,struct device_attribute * attr,char * buf)3060 static ssize_t rx_trig_bytes_show(struct device *dev,
3061 struct device_attribute *attr, char *buf)
3062 {
3063 struct tty_port *port = dev_get_drvdata(dev);
3064 int rxtrig_bytes;
3065
3066 rxtrig_bytes = do_serial8250_get_rxtrig(port);
3067 if (rxtrig_bytes < 0)
3068 return rxtrig_bytes;
3069
3070 return snprintf(buf, PAGE_SIZE, "%d\n", rxtrig_bytes);
3071 }
3072
do_set_rxtrig(struct tty_port * port,unsigned char bytes)3073 static int do_set_rxtrig(struct tty_port *port, unsigned char bytes)
3074 {
3075 struct uart_state *state = container_of(port, struct uart_state, port);
3076 struct uart_port *uport = state->uart_port;
3077 struct uart_8250_port *up = up_to_u8250p(uport);
3078 int rxtrig;
3079
3080 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
3081 return -EINVAL;
3082
3083 rxtrig = bytes_to_fcr_rxtrig(up, bytes);
3084 if (rxtrig < 0)
3085 return rxtrig;
3086
3087 serial8250_clear_fifos(up);
3088 up->fcr &= ~UART_FCR_TRIGGER_MASK;
3089 up->fcr |= (unsigned char)rxtrig;
3090 serial_out(up, UART_FCR, up->fcr);
3091 return 0;
3092 }
3093
do_serial8250_set_rxtrig(struct tty_port * port,unsigned char bytes)3094 static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes)
3095 {
3096 int ret;
3097
3098 mutex_lock(&port->mutex);
3099 ret = do_set_rxtrig(port, bytes);
3100 mutex_unlock(&port->mutex);
3101
3102 return ret;
3103 }
3104
rx_trig_bytes_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3105 static ssize_t rx_trig_bytes_store(struct device *dev,
3106 struct device_attribute *attr, const char *buf, size_t count)
3107 {
3108 struct tty_port *port = dev_get_drvdata(dev);
3109 unsigned char bytes;
3110 int ret;
3111
3112 if (!count)
3113 return -EINVAL;
3114
3115 ret = kstrtou8(buf, 10, &bytes);
3116 if (ret < 0)
3117 return ret;
3118
3119 ret = do_serial8250_set_rxtrig(port, bytes);
3120 if (ret < 0)
3121 return ret;
3122
3123 return count;
3124 }
3125
3126 static DEVICE_ATTR_RW(rx_trig_bytes);
3127
3128 static struct attribute *serial8250_dev_attrs[] = {
3129 &dev_attr_rx_trig_bytes.attr,
3130 NULL
3131 };
3132
3133 static struct attribute_group serial8250_dev_attr_group = {
3134 .attrs = serial8250_dev_attrs,
3135 };
3136
register_dev_spec_attr_grp(struct uart_8250_port * up)3137 static void register_dev_spec_attr_grp(struct uart_8250_port *up)
3138 {
3139 const struct serial8250_config *conf_type = &uart_config[up->port.type];
3140
3141 if (conf_type->rxtrig_bytes[0])
3142 up->port.attr_group = &serial8250_dev_attr_group;
3143 }
3144
serial8250_config_port(struct uart_port * port,int flags)3145 static void serial8250_config_port(struct uart_port *port, int flags)
3146 {
3147 struct uart_8250_port *up = up_to_u8250p(port);
3148 int ret;
3149
3150 /*
3151 * Find the region that we can probe for. This in turn
3152 * tells us whether we can probe for the type of port.
3153 */
3154 ret = serial8250_request_std_resource(up);
3155 if (ret < 0)
3156 return;
3157
3158 if (port->iotype != up->cur_iotype)
3159 set_io_from_upio(port);
3160
3161 if (flags & UART_CONFIG_TYPE)
3162 autoconfig(up);
3163
3164 /* if access method is AU, it is a 16550 with a quirk */
3165 if (port->type == PORT_16550A && port->iotype == UPIO_AU)
3166 up->bugs |= UART_BUG_NOMSR;
3167
3168 /* HW bugs may trigger IRQ while IIR == NO_INT */
3169 if (port->type == PORT_TEGRA)
3170 up->bugs |= UART_BUG_NOMSR;
3171
3172 if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
3173 autoconfig_irq(up);
3174
3175 if (port->type == PORT_UNKNOWN)
3176 serial8250_release_std_resource(up);
3177
3178 register_dev_spec_attr_grp(up);
3179 up->fcr = uart_config[up->port.type].fcr;
3180 }
3181
3182 static int
serial8250_verify_port(struct uart_port * port,struct serial_struct * ser)3183 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
3184 {
3185 if (ser->irq >= nr_irqs || ser->irq < 0 ||
3186 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
3187 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
3188 ser->type == PORT_STARTECH)
3189 return -EINVAL;
3190 return 0;
3191 }
3192
serial8250_type(struct uart_port * port)3193 static const char *serial8250_type(struct uart_port *port)
3194 {
3195 int type = port->type;
3196
3197 if (type >= ARRAY_SIZE(uart_config))
3198 type = 0;
3199 return uart_config[type].name;
3200 }
3201
3202 static const struct uart_ops serial8250_pops = {
3203 .tx_empty = serial8250_tx_empty,
3204 .set_mctrl = serial8250_set_mctrl,
3205 .get_mctrl = serial8250_get_mctrl,
3206 .stop_tx = serial8250_stop_tx,
3207 .start_tx = serial8250_start_tx,
3208 .throttle = serial8250_throttle,
3209 .unthrottle = serial8250_unthrottle,
3210 .stop_rx = serial8250_stop_rx,
3211 .enable_ms = serial8250_enable_ms,
3212 .break_ctl = serial8250_break_ctl,
3213 .startup = serial8250_startup,
3214 .shutdown = serial8250_shutdown,
3215 .set_termios = serial8250_set_termios,
3216 .set_ldisc = serial8250_set_ldisc,
3217 .pm = serial8250_pm,
3218 .type = serial8250_type,
3219 .release_port = serial8250_release_port,
3220 .request_port = serial8250_request_port,
3221 .config_port = serial8250_config_port,
3222 .verify_port = serial8250_verify_port,
3223 #ifdef CONFIG_CONSOLE_POLL
3224 .poll_get_char = serial8250_get_poll_char,
3225 .poll_put_char = serial8250_put_poll_char,
3226 #endif
3227 };
3228
serial8250_init_port(struct uart_8250_port * up)3229 void serial8250_init_port(struct uart_8250_port *up)
3230 {
3231 struct uart_port *port = &up->port;
3232
3233 spin_lock_init(&port->lock);
3234 port->pm = NULL;
3235 port->ops = &serial8250_pops;
3236 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
3237
3238 up->cur_iotype = 0xFF;
3239 }
3240 EXPORT_SYMBOL_GPL(serial8250_init_port);
3241
serial8250_set_defaults(struct uart_8250_port * up)3242 void serial8250_set_defaults(struct uart_8250_port *up)
3243 {
3244 struct uart_port *port = &up->port;
3245
3246 if (up->port.flags & UPF_FIXED_TYPE) {
3247 unsigned int type = up->port.type;
3248
3249 if (!up->port.fifosize)
3250 up->port.fifosize = uart_config[type].fifo_size;
3251 if (!up->tx_loadsz)
3252 up->tx_loadsz = uart_config[type].tx_loadsz;
3253 if (!up->capabilities)
3254 up->capabilities = uart_config[type].flags;
3255 }
3256
3257 set_io_from_upio(port);
3258
3259 /* default dma handlers */
3260 if (up->dma) {
3261 if (!up->dma->tx_dma)
3262 up->dma->tx_dma = serial8250_tx_dma;
3263 if (!up->dma->rx_dma)
3264 up->dma->rx_dma = serial8250_rx_dma;
3265 }
3266 }
3267 EXPORT_SYMBOL_GPL(serial8250_set_defaults);
3268
3269 #ifdef CONFIG_SERIAL_8250_CONSOLE
3270
serial8250_console_putchar(struct uart_port * port,int ch)3271 static void serial8250_console_putchar(struct uart_port *port, int ch)
3272 {
3273 struct uart_8250_port *up = up_to_u8250p(port);
3274
3275 wait_for_xmitr(up, UART_LSR_THRE);
3276 serial_port_out(port, UART_TX, ch);
3277 }
3278
3279 /*
3280 * Restore serial console when h/w power-off detected
3281 */
serial8250_console_restore(struct uart_8250_port * up)3282 static void serial8250_console_restore(struct uart_8250_port *up)
3283 {
3284 struct uart_port *port = &up->port;
3285 struct ktermios termios;
3286 unsigned int baud, quot, frac = 0;
3287
3288 termios.c_cflag = port->cons->cflag;
3289 termios.c_ispeed = port->cons->ispeed;
3290 termios.c_ospeed = port->cons->ospeed;
3291 if (port->state->port.tty && termios.c_cflag == 0) {
3292 termios.c_cflag = port->state->port.tty->termios.c_cflag;
3293 termios.c_ispeed = port->state->port.tty->termios.c_ispeed;
3294 termios.c_ospeed = port->state->port.tty->termios.c_ospeed;
3295 }
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