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