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