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