Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  *  Base port operations for 8250/16550-type serial ports
0004  *
0005  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
0006  *  Split from 8250_core.c, Copyright (C) 2001 Russell King.
0007  *
0008  * A note about mapbase / membase
0009  *
0010  *  mapbase is the physical address of the IO port.
0011  *  membase is an 'ioremapped' cookie.
0012  */
0013 
0014 #include <linux/module.h>
0015 #include <linux/moduleparam.h>
0016 #include <linux/ioport.h>
0017 #include <linux/init.h>
0018 #include <linux/console.h>
0019 #include <linux/gpio/consumer.h>
0020 #include <linux/sysrq.h>
0021 #include <linux/delay.h>
0022 #include <linux/platform_device.h>
0023 #include <linux/tty.h>
0024 #include <linux/ratelimit.h>
0025 #include <linux/tty_flip.h>
0026 #include <linux/serial.h>
0027 #include <linux/serial_8250.h>
0028 #include <linux/nmi.h>
0029 #include <linux/mutex.h>
0030 #include <linux/slab.h>
0031 #include <linux/uaccess.h>
0032 #include <linux/pm_runtime.h>
0033 #include <linux/ktime.h>
0034 
0035 #include <asm/io.h>
0036 #include <asm/irq.h>
0037 
0038 #include "8250.h"
0039 
0040 /* Nuvoton NPCM timeout register */
0041 #define UART_NPCM_TOR          7
0042 #define UART_NPCM_TOIE         BIT(7)  /* Timeout Interrupt Enable */
0043 
0044 /*
0045  * Debugging.
0046  */
0047 #if 0
0048 #define DEBUG_AUTOCONF(fmt...)  printk(fmt)
0049 #else
0050 #define DEBUG_AUTOCONF(fmt...)  do { } while (0)
0051 #endif
0052 
0053 /*
0054  * Here we define the default xmit fifo size used for each type of UART.
0055  */
0056 static const struct serial8250_config uart_config[] = {
0057     [PORT_UNKNOWN] = {
0058         .name       = "unknown",
0059         .fifo_size  = 1,
0060         .tx_loadsz  = 1,
0061     },
0062     [PORT_8250] = {
0063         .name       = "8250",
0064         .fifo_size  = 1,
0065         .tx_loadsz  = 1,
0066     },
0067     [PORT_16450] = {
0068         .name       = "16450",
0069         .fifo_size  = 1,
0070         .tx_loadsz  = 1,
0071     },
0072     [PORT_16550] = {
0073         .name       = "16550",
0074         .fifo_size  = 1,
0075         .tx_loadsz  = 1,
0076     },
0077     [PORT_16550A] = {
0078         .name       = "16550A",
0079         .fifo_size  = 16,
0080         .tx_loadsz  = 16,
0081         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
0082         .rxtrig_bytes   = {1, 4, 8, 14},
0083         .flags      = UART_CAP_FIFO,
0084     },
0085     [PORT_CIRRUS] = {
0086         .name       = "Cirrus",
0087         .fifo_size  = 1,
0088         .tx_loadsz  = 1,
0089     },
0090     [PORT_16650] = {
0091         .name       = "ST16650",
0092         .fifo_size  = 1,
0093         .tx_loadsz  = 1,
0094         .flags      = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
0095     },
0096     [PORT_16650V2] = {
0097         .name       = "ST16650V2",
0098         .fifo_size  = 32,
0099         .tx_loadsz  = 16,
0100         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
0101                   UART_FCR_T_TRIG_00,
0102         .rxtrig_bytes   = {8, 16, 24, 28},
0103         .flags      = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
0104     },
0105     [PORT_16750] = {
0106         .name       = "TI16750",
0107         .fifo_size  = 64,
0108         .tx_loadsz  = 64,
0109         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
0110                   UART_FCR7_64BYTE,
0111         .rxtrig_bytes   = {1, 16, 32, 56},
0112         .flags      = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
0113     },
0114     [PORT_STARTECH] = {
0115         .name       = "Startech",
0116         .fifo_size  = 1,
0117         .tx_loadsz  = 1,
0118     },
0119     [PORT_16C950] = {
0120         .name       = "16C950/954",
0121         .fifo_size  = 128,
0122         .tx_loadsz  = 128,
0123         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01,
0124         .rxtrig_bytes   = {16, 32, 112, 120},
0125         /* UART_CAP_EFR breaks billionon CF bluetooth card. */
0126         .flags      = UART_CAP_FIFO | UART_CAP_SLEEP,
0127     },
0128     [PORT_16654] = {
0129         .name       = "ST16654",
0130         .fifo_size  = 64,
0131         .tx_loadsz  = 32,
0132         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
0133                   UART_FCR_T_TRIG_10,
0134         .rxtrig_bytes   = {8, 16, 56, 60},
0135         .flags      = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
0136     },
0137     [PORT_16850] = {
0138         .name       = "XR16850",
0139         .fifo_size  = 128,
0140         .tx_loadsz  = 128,
0141         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
0142         .flags      = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
0143     },
0144     [PORT_RSA] = {
0145         .name       = "RSA",
0146         .fifo_size  = 2048,
0147         .tx_loadsz  = 2048,
0148         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
0149         .flags      = UART_CAP_FIFO,
0150     },
0151     [PORT_NS16550A] = {
0152         .name       = "NS16550A",
0153         .fifo_size  = 16,
0154         .tx_loadsz  = 16,
0155         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
0156         .flags      = UART_CAP_FIFO | UART_NATSEMI,
0157     },
0158     [PORT_XSCALE] = {
0159         .name       = "XScale",
0160         .fifo_size  = 32,
0161         .tx_loadsz  = 32,
0162         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
0163         .flags      = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
0164     },
0165     [PORT_OCTEON] = {
0166         .name       = "OCTEON",
0167         .fifo_size  = 64,
0168         .tx_loadsz  = 64,
0169         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
0170         .flags      = UART_CAP_FIFO,
0171     },
0172     [PORT_AR7] = {
0173         .name       = "AR7",
0174         .fifo_size  = 16,
0175         .tx_loadsz  = 16,
0176         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
0177         .flags      = UART_CAP_FIFO /* | UART_CAP_AFE */,
0178     },
0179     [PORT_U6_16550A] = {
0180         .name       = "U6_16550A",
0181         .fifo_size  = 64,
0182         .tx_loadsz  = 64,
0183         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
0184         .flags      = UART_CAP_FIFO | UART_CAP_AFE,
0185     },
0186     [PORT_TEGRA] = {
0187         .name       = "Tegra",
0188         .fifo_size  = 32,
0189         .tx_loadsz  = 8,
0190         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
0191                   UART_FCR_T_TRIG_01,
0192         .rxtrig_bytes   = {1, 4, 8, 14},
0193         .flags      = UART_CAP_FIFO | UART_CAP_RTOIE,
0194     },
0195     [PORT_XR17D15X] = {
0196         .name       = "XR17D15X",
0197         .fifo_size  = 64,
0198         .tx_loadsz  = 64,
0199         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
0200         .flags      = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
0201                   UART_CAP_SLEEP,
0202     },
0203     [PORT_XR17V35X] = {
0204         .name       = "XR17V35X",
0205         .fifo_size  = 256,
0206         .tx_loadsz  = 256,
0207         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11 |
0208                   UART_FCR_T_TRIG_11,
0209         .flags      = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
0210                   UART_CAP_SLEEP,
0211     },
0212     [PORT_LPC3220] = {
0213         .name       = "LPC3220",
0214         .fifo_size  = 64,
0215         .tx_loadsz  = 32,
0216         .fcr        = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
0217                   UART_FCR_R_TRIG_00 | UART_FCR_T_TRIG_00,
0218         .flags      = UART_CAP_FIFO,
0219     },
0220     [PORT_BRCM_TRUMANAGE] = {
0221         .name       = "TruManage",
0222         .fifo_size  = 1,
0223         .tx_loadsz  = 1024,
0224         .flags      = UART_CAP_HFIFO,
0225     },
0226     [PORT_8250_CIR] = {
0227         .name       = "CIR port"
0228     },
0229     [PORT_ALTR_16550_F32] = {
0230         .name       = "Altera 16550 FIFO32",
0231         .fifo_size  = 32,
0232         .tx_loadsz  = 32,
0233         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
0234         .rxtrig_bytes   = {1, 8, 16, 30},
0235         .flags      = UART_CAP_FIFO | UART_CAP_AFE,
0236     },
0237     [PORT_ALTR_16550_F64] = {
0238         .name       = "Altera 16550 FIFO64",
0239         .fifo_size  = 64,
0240         .tx_loadsz  = 64,
0241         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
0242         .rxtrig_bytes   = {1, 16, 32, 62},
0243         .flags      = UART_CAP_FIFO | UART_CAP_AFE,
0244     },
0245     [PORT_ALTR_16550_F128] = {
0246         .name       = "Altera 16550 FIFO128",
0247         .fifo_size  = 128,
0248         .tx_loadsz  = 128,
0249         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
0250         .rxtrig_bytes   = {1, 32, 64, 126},
0251         .flags      = UART_CAP_FIFO | UART_CAP_AFE,
0252     },
0253     /*
0254      * tx_loadsz is set to 63-bytes instead of 64-bytes to implement
0255      * workaround of errata A-008006 which states that tx_loadsz should
0256      * be configured less than Maximum supported fifo bytes.
0257      */
0258     [PORT_16550A_FSL64] = {
0259         .name       = "16550A_FSL64",
0260         .fifo_size  = 64,
0261         .tx_loadsz  = 63,
0262         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
0263                   UART_FCR7_64BYTE,
0264         .flags      = UART_CAP_FIFO | UART_CAP_NOTEMT,
0265     },
0266     [PORT_RT2880] = {
0267         .name       = "Palmchip BK-3103",
0268         .fifo_size  = 16,
0269         .tx_loadsz  = 16,
0270         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
0271         .rxtrig_bytes   = {1, 4, 8, 14},
0272         .flags      = UART_CAP_FIFO,
0273     },
0274     [PORT_DA830] = {
0275         .name       = "TI DA8xx/66AK2x",
0276         .fifo_size  = 16,
0277         .tx_loadsz  = 16,
0278         .fcr        = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
0279                   UART_FCR_R_TRIG_10,
0280         .rxtrig_bytes   = {1, 4, 8, 14},
0281         .flags      = UART_CAP_FIFO | UART_CAP_AFE,
0282     },
0283     [PORT_MTK_BTIF] = {
0284         .name       = "MediaTek BTIF",
0285         .fifo_size  = 16,
0286         .tx_loadsz  = 16,
0287         .fcr        = UART_FCR_ENABLE_FIFO |
0288                   UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
0289         .flags      = UART_CAP_FIFO,
0290     },
0291     [PORT_NPCM] = {
0292         .name       = "Nuvoton 16550",
0293         .fifo_size  = 16,
0294         .tx_loadsz  = 16,
0295         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
0296                   UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
0297         .rxtrig_bytes   = {1, 4, 8, 14},
0298         .flags      = UART_CAP_FIFO,
0299     },
0300     [PORT_SUNIX] = {
0301         .name       = "Sunix",
0302         .fifo_size  = 128,
0303         .tx_loadsz  = 128,
0304         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
0305         .rxtrig_bytes   = {1, 32, 64, 112},
0306         .flags      = UART_CAP_FIFO | UART_CAP_SLEEP,
0307     },
0308     [PORT_ASPEED_VUART] = {
0309         .name       = "ASPEED VUART",
0310         .fifo_size  = 16,
0311         .tx_loadsz  = 16,
0312         .fcr        = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
0313         .rxtrig_bytes   = {1, 4, 8, 14},
0314         .flags      = UART_CAP_FIFO,
0315     },
0316 };
0317 
0318 /* Uart divisor latch read */
0319 static int default_serial_dl_read(struct uart_8250_port *up)
0320 {
0321     /* Assign these in pieces to truncate any bits above 7.  */
0322     unsigned char dll = serial_in(up, UART_DLL);
0323     unsigned char dlm = serial_in(up, UART_DLM);
0324 
0325     return dll | dlm << 8;
0326 }
0327 
0328 /* Uart divisor latch write */
0329 static void default_serial_dl_write(struct uart_8250_port *up, int value)
0330 {
0331     serial_out(up, UART_DLL, value & 0xff);
0332     serial_out(up, UART_DLM, value >> 8 & 0xff);
0333 }
0334 
0335 #ifdef CONFIG_SERIAL_8250_RT288X
0336 
0337 #define UART_REG_UNMAPPED   -1
0338 
0339 /* Au1x00/RT288x UART hardware has a weird register layout */
0340 static const s8 au_io_in_map[8] = {
0341     [UART_RX]   = 0,
0342     [UART_IER]  = 2,
0343     [UART_IIR]  = 3,
0344     [UART_LCR]  = 5,
0345     [UART_MCR]  = 6,
0346     [UART_LSR]  = 7,
0347     [UART_MSR]  = 8,
0348     [UART_SCR]  = UART_REG_UNMAPPED,
0349 };
0350 
0351 static const s8 au_io_out_map[8] = {
0352     [UART_TX]   = 1,
0353     [UART_IER]  = 2,
0354     [UART_FCR]  = 4,
0355     [UART_LCR]  = 5,
0356     [UART_MCR]  = 6,
0357     [UART_LSR]  = UART_REG_UNMAPPED,
0358     [UART_MSR]  = UART_REG_UNMAPPED,
0359     [UART_SCR]  = UART_REG_UNMAPPED,
0360 };
0361 
0362 unsigned int au_serial_in(struct uart_port *p, int offset)
0363 {
0364     if (offset >= ARRAY_SIZE(au_io_in_map))
0365         return UINT_MAX;
0366     offset = au_io_in_map[offset];
0367     if (offset == UART_REG_UNMAPPED)
0368         return UINT_MAX;
0369     return __raw_readl(p->membase + (offset << p->regshift));
0370 }
0371 
0372 void au_serial_out(struct uart_port *p, int offset, int value)
0373 {
0374     if (offset >= ARRAY_SIZE(au_io_out_map))
0375         return;
0376     offset = au_io_out_map[offset];
0377     if (offset == UART_REG_UNMAPPED)
0378         return;
0379     __raw_writel(value, p->membase + (offset << p->regshift));
0380 }
0381 
0382 /* Au1x00 haven't got a standard divisor latch */
0383 static int au_serial_dl_read(struct uart_8250_port *up)
0384 {
0385     return __raw_readl(up->port.membase + 0x28);
0386 }
0387 
0388 static void au_serial_dl_write(struct uart_8250_port *up, int value)
0389 {
0390     __raw_writel(value, up->port.membase + 0x28);
0391 }
0392 
0393 #endif
0394 
0395 static unsigned int hub6_serial_in(struct uart_port *p, int offset)
0396 {
0397     offset = offset << p->regshift;
0398     outb(p->hub6 - 1 + offset, p->iobase);
0399     return inb(p->iobase + 1);
0400 }
0401 
0402 static void hub6_serial_out(struct uart_port *p, int offset, int value)
0403 {
0404     offset = offset << p->regshift;
0405     outb(p->hub6 - 1 + offset, p->iobase);
0406     outb(value, p->iobase + 1);
0407 }
0408 
0409 static unsigned int mem_serial_in(struct uart_port *p, int offset)
0410 {
0411     offset = offset << p->regshift;
0412     return readb(p->membase + offset);
0413 }
0414 
0415 static void mem_serial_out(struct uart_port *p, int offset, int value)
0416 {
0417     offset = offset << p->regshift;
0418     writeb(value, p->membase + offset);
0419 }
0420 
0421 static void mem16_serial_out(struct uart_port *p, int offset, int value)
0422 {
0423     offset = offset << p->regshift;
0424     writew(value, p->membase + offset);
0425 }
0426 
0427 static unsigned int mem16_serial_in(struct uart_port *p, int offset)
0428 {
0429     offset = offset << p->regshift;
0430     return readw(p->membase + offset);
0431 }
0432 
0433 static void mem32_serial_out(struct uart_port *p, int offset, int value)
0434 {
0435     offset = offset << p->regshift;
0436     writel(value, p->membase + offset);
0437 }
0438 
0439 static unsigned int mem32_serial_in(struct uart_port *p, int offset)
0440 {
0441     offset = offset << p->regshift;
0442     return readl(p->membase + offset);
0443 }
0444 
0445 static void mem32be_serial_out(struct uart_port *p, int offset, int value)
0446 {
0447     offset = offset << p->regshift;
0448     iowrite32be(value, p->membase + offset);
0449 }
0450 
0451 static unsigned int mem32be_serial_in(struct uart_port *p, int offset)
0452 {
0453     offset = offset << p->regshift;
0454     return ioread32be(p->membase + offset);
0455 }
0456 
0457 static unsigned int io_serial_in(struct uart_port *p, int offset)
0458 {
0459     offset = offset << p->regshift;
0460     return inb(p->iobase + offset);
0461 }
0462 
0463 static void io_serial_out(struct uart_port *p, int offset, int value)
0464 {
0465     offset = offset << p->regshift;
0466     outb(value, p->iobase + offset);
0467 }
0468 
0469 static int serial8250_default_handle_irq(struct uart_port *port);
0470 
0471 static void set_io_from_upio(struct uart_port *p)
0472 {
0473     struct uart_8250_port *up = up_to_u8250p(p);
0474 
0475     up->dl_read = default_serial_dl_read;
0476     up->dl_write = default_serial_dl_write;
0477 
0478     switch (p->iotype) {
0479     case UPIO_HUB6:
0480         p->serial_in = hub6_serial_in;
0481         p->serial_out = hub6_serial_out;
0482         break;
0483 
0484     case UPIO_MEM:
0485         p->serial_in = mem_serial_in;
0486         p->serial_out = mem_serial_out;
0487         break;
0488 
0489     case UPIO_MEM16:
0490         p->serial_in = mem16_serial_in;
0491         p->serial_out = mem16_serial_out;
0492         break;
0493 
0494     case UPIO_MEM32:
0495         p->serial_in = mem32_serial_in;
0496         p->serial_out = mem32_serial_out;
0497         break;
0498 
0499     case UPIO_MEM32BE:
0500         p->serial_in = mem32be_serial_in;
0501         p->serial_out = mem32be_serial_out;
0502         break;
0503 
0504 #ifdef CONFIG_SERIAL_8250_RT288X
0505     case UPIO_AU:
0506         p->serial_in = au_serial_in;
0507         p->serial_out = au_serial_out;
0508         up->dl_read = au_serial_dl_read;
0509         up->dl_write = au_serial_dl_write;
0510         break;
0511 #endif
0512 
0513     default:
0514         p->serial_in = io_serial_in;
0515         p->serial_out = io_serial_out;
0516         break;
0517     }
0518     /* Remember loaded iotype */
0519     up->cur_iotype = p->iotype;
0520     p->handle_irq = serial8250_default_handle_irq;
0521 }
0522 
0523 static void
0524 serial_port_out_sync(struct uart_port *p, int offset, int value)
0525 {
0526     switch (p->iotype) {
0527     case UPIO_MEM:
0528     case UPIO_MEM16:
0529     case UPIO_MEM32:
0530     case UPIO_MEM32BE:
0531     case UPIO_AU:
0532         p->serial_out(p, offset, value);
0533         p->serial_in(p, UART_LCR);  /* safe, no side-effects */
0534         break;
0535     default:
0536         p->serial_out(p, offset, value);
0537     }
0538 }
0539 
0540 /*
0541  * FIFO support.
0542  */
0543 static void serial8250_clear_fifos(struct uart_8250_port *p)
0544 {
0545     if (p->capabilities & UART_CAP_FIFO) {
0546         serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
0547         serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
0548                    UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
0549         serial_out(p, UART_FCR, 0);
0550     }
0551 }
0552 
0553 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t);
0554 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t);
0555 
0556 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
0557 {
0558     serial8250_clear_fifos(p);
0559     serial_out(p, UART_FCR, p->fcr);
0560 }
0561 EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);
0562 
0563 void serial8250_rpm_get(struct uart_8250_port *p)
0564 {
0565     if (!(p->capabilities & UART_CAP_RPM))
0566         return;
0567     pm_runtime_get_sync(p->port.dev);
0568 }
0569 EXPORT_SYMBOL_GPL(serial8250_rpm_get);
0570 
0571 void serial8250_rpm_put(struct uart_8250_port *p)
0572 {
0573     if (!(p->capabilities & UART_CAP_RPM))
0574         return;
0575     pm_runtime_mark_last_busy(p->port.dev);
0576     pm_runtime_put_autosuspend(p->port.dev);
0577 }
0578 EXPORT_SYMBOL_GPL(serial8250_rpm_put);
0579 
0580 /**
0581  *  serial8250_em485_init() - put uart_8250_port into rs485 emulating
0582  *  @p: uart_8250_port port instance
0583  *
0584  *  The function is used to start rs485 software emulating on the
0585  *  &struct uart_8250_port* @p. Namely, RTS is switched before/after
0586  *  transmission. The function is idempotent, so it is safe to call it
0587  *  multiple times.
0588  *
0589  *  The caller MUST enable interrupt on empty shift register before
0590  *  calling serial8250_em485_init(). This interrupt is not a part of
0591  *  8250 standard, but implementation defined.
0592  *
0593  *  The function is supposed to be called from .rs485_config callback
0594  *  or from any other callback protected with p->port.lock spinlock.
0595  *
0596  *  See also serial8250_em485_destroy()
0597  *
0598  *  Return 0 - success, -errno - otherwise
0599  */
0600 static int serial8250_em485_init(struct uart_8250_port *p)
0601 {
0602     if (p->em485)
0603         return 0;
0604 
0605     p->em485 = kmalloc(sizeof(struct uart_8250_em485), GFP_ATOMIC);
0606     if (!p->em485)
0607         return -ENOMEM;
0608 
0609     hrtimer_init(&p->em485->stop_tx_timer, CLOCK_MONOTONIC,
0610              HRTIMER_MODE_REL);
0611     hrtimer_init(&p->em485->start_tx_timer, CLOCK_MONOTONIC,
0612              HRTIMER_MODE_REL);
0613     p->em485->stop_tx_timer.function = &serial8250_em485_handle_stop_tx;
0614     p->em485->start_tx_timer.function = &serial8250_em485_handle_start_tx;
0615     p->em485->port = p;
0616     p->em485->active_timer = NULL;
0617     p->em485->tx_stopped = true;
0618 
0619     p->rs485_stop_tx(p);
0620 
0621     return 0;
0622 }
0623 
0624 /**
0625  *  serial8250_em485_destroy() - put uart_8250_port into normal state
0626  *  @p: uart_8250_port port instance
0627  *
0628  *  The function is used to stop rs485 software emulating on the
0629  *  &struct uart_8250_port* @p. The function is idempotent, so it is safe to
0630  *  call it multiple times.
0631  *
0632  *  The function is supposed to be called from .rs485_config callback
0633  *  or from any other callback protected with p->port.lock spinlock.
0634  *
0635  *  See also serial8250_em485_init()
0636  */
0637 void serial8250_em485_destroy(struct uart_8250_port *p)
0638 {
0639     if (!p->em485)
0640         return;
0641 
0642     hrtimer_cancel(&p->em485->start_tx_timer);
0643     hrtimer_cancel(&p->em485->stop_tx_timer);
0644 
0645     kfree(p->em485);
0646     p->em485 = NULL;
0647 }
0648 EXPORT_SYMBOL_GPL(serial8250_em485_destroy);
0649 
0650 struct serial_rs485 serial8250_em485_supported = {
0651     .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
0652          SER_RS485_TERMINATE_BUS | SER_RS485_RX_DURING_TX,
0653     .delay_rts_before_send = 1,
0654     .delay_rts_after_send = 1,
0655 };
0656 EXPORT_SYMBOL_GPL(serial8250_em485_supported);
0657 
0658 /**
0659  * serial8250_em485_config() - generic ->rs485_config() callback
0660  * @port: uart port
0661  * @rs485: rs485 settings
0662  *
0663  * Generic callback usable by 8250 uart drivers to activate rs485 settings
0664  * if the uart is incapable of driving RTS as a Transmit Enable signal in
0665  * hardware, relying on software emulation instead.
0666  */
0667 int serial8250_em485_config(struct uart_port *port, struct ktermios *termios,
0668                 struct serial_rs485 *rs485)
0669 {
0670     struct uart_8250_port *up = up_to_u8250p(port);
0671 
0672     /* pick sane settings if the user hasn't */
0673     if (!!(rs485->flags & SER_RS485_RTS_ON_SEND) ==
0674         !!(rs485->flags & SER_RS485_RTS_AFTER_SEND)) {
0675         rs485->flags |= SER_RS485_RTS_ON_SEND;
0676         rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
0677     }
0678 
0679     /*
0680      * Both serial8250_em485_init() and serial8250_em485_destroy()
0681      * are idempotent.
0682      */
0683     if (rs485->flags & SER_RS485_ENABLED)
0684         return serial8250_em485_init(up);
0685 
0686     serial8250_em485_destroy(up);
0687     return 0;
0688 }
0689 EXPORT_SYMBOL_GPL(serial8250_em485_config);
0690 
0691 /*
0692  * These two wrappers ensure that enable_runtime_pm_tx() can be called more than
0693  * once and disable_runtime_pm_tx() will still disable RPM because the fifo is
0694  * empty and the HW can idle again.
0695  */
0696 void serial8250_rpm_get_tx(struct uart_8250_port *p)
0697 {
0698     unsigned char rpm_active;
0699 
0700     if (!(p->capabilities & UART_CAP_RPM))
0701         return;
0702 
0703     rpm_active = xchg(&p->rpm_tx_active, 1);
0704     if (rpm_active)
0705         return;
0706     pm_runtime_get_sync(p->port.dev);
0707 }
0708 EXPORT_SYMBOL_GPL(serial8250_rpm_get_tx);
0709 
0710 void serial8250_rpm_put_tx(struct uart_8250_port *p)
0711 {
0712     unsigned char rpm_active;
0713 
0714     if (!(p->capabilities & UART_CAP_RPM))
0715         return;
0716 
0717     rpm_active = xchg(&p->rpm_tx_active, 0);
0718     if (!rpm_active)
0719         return;
0720     pm_runtime_mark_last_busy(p->port.dev);
0721     pm_runtime_put_autosuspend(p->port.dev);
0722 }
0723 EXPORT_SYMBOL_GPL(serial8250_rpm_put_tx);
0724 
0725 /*
0726  * IER sleep support.  UARTs which have EFRs need the "extended
0727  * capability" bit enabled.  Note that on XR16C850s, we need to
0728  * reset LCR to write to IER.
0729  */
0730 static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
0731 {
0732     unsigned char lcr = 0, efr = 0;
0733 
0734     serial8250_rpm_get(p);
0735 
0736     if (p->capabilities & UART_CAP_SLEEP) {
0737         if (p->capabilities & UART_CAP_EFR) {
0738             lcr = serial_in(p, UART_LCR);
0739             efr = serial_in(p, UART_EFR);
0740             serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
0741             serial_out(p, UART_EFR, UART_EFR_ECB);
0742             serial_out(p, UART_LCR, 0);
0743         }
0744         serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
0745         if (p->capabilities & UART_CAP_EFR) {
0746             serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
0747             serial_out(p, UART_EFR, efr);
0748             serial_out(p, UART_LCR, lcr);
0749         }
0750     }
0751 
0752     serial8250_rpm_put(p);
0753 }
0754 
0755 #ifdef CONFIG_SERIAL_8250_RSA
0756 /*
0757  * Attempts to turn on the RSA FIFO.  Returns zero on failure.
0758  * We set the port uart clock rate if we succeed.
0759  */
0760 static int __enable_rsa(struct uart_8250_port *up)
0761 {
0762     unsigned char mode;
0763     int result;
0764 
0765     mode = serial_in(up, UART_RSA_MSR);
0766     result = mode & UART_RSA_MSR_FIFO;
0767 
0768     if (!result) {
0769         serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
0770         mode = serial_in(up, UART_RSA_MSR);
0771         result = mode & UART_RSA_MSR_FIFO;
0772     }
0773 
0774     if (result)
0775         up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
0776 
0777     return result;
0778 }
0779 
0780 static void enable_rsa(struct uart_8250_port *up)
0781 {
0782     if (up->port.type == PORT_RSA) {
0783         if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
0784             spin_lock_irq(&up->port.lock);
0785             __enable_rsa(up);
0786             spin_unlock_irq(&up->port.lock);
0787         }
0788         if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
0789             serial_out(up, UART_RSA_FRR, 0);
0790     }
0791 }
0792 
0793 /*
0794  * Attempts to turn off the RSA FIFO.  Returns zero on failure.
0795  * It is unknown why interrupts were disabled in here.  However,
0796  * the caller is expected to preserve this behaviour by grabbing
0797  * the spinlock before calling this function.
0798  */
0799 static void disable_rsa(struct uart_8250_port *up)
0800 {
0801     unsigned char mode;
0802     int result;
0803 
0804     if (up->port.type == PORT_RSA &&
0805         up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
0806         spin_lock_irq(&up->port.lock);
0807 
0808         mode = serial_in(up, UART_RSA_MSR);
0809         result = !(mode & UART_RSA_MSR_FIFO);
0810 
0811         if (!result) {
0812             serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
0813             mode = serial_in(up, UART_RSA_MSR);
0814             result = !(mode & UART_RSA_MSR_FIFO);
0815         }
0816 
0817         if (result)
0818             up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
0819         spin_unlock_irq(&up->port.lock);
0820     }
0821 }
0822 #endif /* CONFIG_SERIAL_8250_RSA */
0823 
0824 /*
0825  * This is a quickie test to see how big the FIFO is.
0826  * It doesn't work at all the time, more's the pity.
0827  */
0828 static int size_fifo(struct uart_8250_port *up)
0829 {
0830     unsigned char old_fcr, old_mcr, old_lcr;
0831     unsigned short old_dl;
0832     int count;
0833 
0834     old_lcr = serial_in(up, UART_LCR);
0835     serial_out(up, UART_LCR, 0);
0836     old_fcr = serial_in(up, UART_FCR);
0837     old_mcr = serial8250_in_MCR(up);
0838     serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
0839             UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
0840     serial8250_out_MCR(up, UART_MCR_LOOP);
0841     serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
0842     old_dl = serial_dl_read(up);
0843     serial_dl_write(up, 0x0001);
0844     serial_out(up, UART_LCR, UART_LCR_WLEN8);
0845     for (count = 0; count < 256; count++)
0846         serial_out(up, UART_TX, count);
0847     mdelay(20);/* FIXME - schedule_timeout */
0848     for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
0849          (count < 256); count++)
0850         serial_in(up, UART_RX);
0851     serial_out(up, UART_FCR, old_fcr);
0852     serial8250_out_MCR(up, old_mcr);
0853     serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
0854     serial_dl_write(up, old_dl);
0855     serial_out(up, UART_LCR, old_lcr);
0856 
0857     return count;
0858 }
0859 
0860 /*
0861  * Read UART ID using the divisor method - set DLL and DLM to zero
0862  * and the revision will be in DLL and device type in DLM.  We
0863  * preserve the device state across this.
0864  */
0865 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
0866 {
0867     unsigned char old_lcr;
0868     unsigned int id, old_dl;
0869 
0870     old_lcr = serial_in(p, UART_LCR);
0871     serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
0872     old_dl = serial_dl_read(p);
0873     serial_dl_write(p, 0);
0874     id = serial_dl_read(p);
0875     serial_dl_write(p, old_dl);
0876 
0877     serial_out(p, UART_LCR, old_lcr);
0878 
0879     return id;
0880 }
0881 
0882 /*
0883  * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
0884  * When this function is called we know it is at least a StarTech
0885  * 16650 V2, but it might be one of several StarTech UARTs, or one of
0886  * its clones.  (We treat the broken original StarTech 16650 V1 as a
0887  * 16550, and why not?  Startech doesn't seem to even acknowledge its
0888  * existence.)
0889  *
0890  * What evil have men's minds wrought...
0891  */
0892 static void autoconfig_has_efr(struct uart_8250_port *up)
0893 {
0894     unsigned int id1, id2, id3, rev;
0895 
0896     /*
0897      * Everything with an EFR has SLEEP
0898      */
0899     up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
0900 
0901     /*
0902      * First we check to see if it's an Oxford Semiconductor UART.
0903      *
0904      * If we have to do this here because some non-National
0905      * Semiconductor clone chips lock up if you try writing to the
0906      * LSR register (which serial_icr_read does)
0907      */
0908 
0909     /*
0910      * Check for Oxford Semiconductor 16C950.
0911      *
0912      * EFR [4] must be set else this test fails.
0913      *
0914      * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
0915      * claims that it's needed for 952 dual UART's (which are not
0916      * recommended for new designs).
0917      */
0918     up->acr = 0;
0919     serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
0920     serial_out(up, UART_EFR, UART_EFR_ECB);
0921     serial_out(up, UART_LCR, 0x00);
0922     id1 = serial_icr_read(up, UART_ID1);
0923     id2 = serial_icr_read(up, UART_ID2);
0924     id3 = serial_icr_read(up, UART_ID3);
0925     rev = serial_icr_read(up, UART_REV);
0926 
0927     DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
0928 
0929     if (id1 == 0x16 && id2 == 0xC9 &&
0930         (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
0931         up->port.type = PORT_16C950;
0932 
0933         /*
0934          * Enable work around for the Oxford Semiconductor 952 rev B
0935          * chip which causes it to seriously miscalculate baud rates
0936          * when DLL is 0.
0937          */
0938         if (id3 == 0x52 && rev == 0x01)
0939             up->bugs |= UART_BUG_QUOT;
0940         return;
0941     }
0942 
0943     /*
0944      * We check for a XR16C850 by setting DLL and DLM to 0, and then
0945      * reading back DLL and DLM.  The chip type depends on the DLM
0946      * value read back:
0947      *  0x10 - XR16C850 and the DLL contains the chip revision.
0948      *  0x12 - XR16C2850.
0949      *  0x14 - XR16C854.
0950      */
0951     id1 = autoconfig_read_divisor_id(up);
0952     DEBUG_AUTOCONF("850id=%04x ", id1);
0953 
0954     id2 = id1 >> 8;
0955     if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
0956         up->port.type = PORT_16850;
0957         return;
0958     }
0959 
0960     /*
0961      * It wasn't an XR16C850.
0962      *
0963      * We distinguish between the '654 and the '650 by counting
0964      * how many bytes are in the FIFO.  I'm using this for now,
0965      * since that's the technique that was sent to me in the
0966      * serial driver update, but I'm not convinced this works.
0967      * I've had problems doing this in the past.  -TYT
0968      */
0969     if (size_fifo(up) == 64)
0970         up->port.type = PORT_16654;
0971     else
0972         up->port.type = PORT_16650V2;
0973 }
0974 
0975 /*
0976  * We detected a chip without a FIFO.  Only two fall into
0977  * this category - the original 8250 and the 16450.  The
0978  * 16450 has a scratch register (accessible with LCR=0)
0979  */
0980 static void autoconfig_8250(struct uart_8250_port *up)
0981 {
0982     unsigned char scratch, status1, status2;
0983 
0984     up->port.type = PORT_8250;
0985 
0986     scratch = serial_in(up, UART_SCR);
0987     serial_out(up, UART_SCR, 0xa5);
0988     status1 = serial_in(up, UART_SCR);
0989     serial_out(up, UART_SCR, 0x5a);
0990     status2 = serial_in(up, UART_SCR);
0991     serial_out(up, UART_SCR, scratch);
0992 
0993     if (status1 == 0xa5 && status2 == 0x5a)
0994         up->port.type = PORT_16450;
0995 }
0996 
0997 static int broken_efr(struct uart_8250_port *up)
0998 {
0999     /*
1000      * Exar ST16C2550 "A2" devices incorrectly detect as
1001      * having an EFR, and report an ID of 0x0201.  See
1002      * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
1003      */
1004     if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
1005         return 1;
1006 
1007     return 0;
1008 }
1009 
1010 /*
1011  * We know that the chip has FIFOs.  Does it have an EFR?  The
1012  * EFR is located in the same register position as the IIR and
1013  * we know the top two bits of the IIR are currently set.  The
1014  * EFR should contain zero.  Try to read the EFR.
1015  */
1016 static void autoconfig_16550a(struct uart_8250_port *up)
1017 {
1018     unsigned char status1, status2;
1019     unsigned int iersave;
1020 
1021     up->port.type = PORT_16550A;
1022     up->capabilities |= UART_CAP_FIFO;
1023 
1024     if (!IS_ENABLED(CONFIG_SERIAL_8250_16550A_VARIANTS))
1025         return;
1026 
1027     /*
1028      * Check for presence of the EFR when DLAB is set.
1029      * Only ST16C650V1 UARTs pass this test.
1030      */
1031     serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1032     if (serial_in(up, UART_EFR) == 0) {
1033         serial_out(up, UART_EFR, 0xA8);
1034         if (serial_in(up, UART_EFR) != 0) {
1035             DEBUG_AUTOCONF("EFRv1 ");
1036             up->port.type = PORT_16650;
1037             up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
1038         } else {
1039             serial_out(up, UART_LCR, 0);
1040             serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
1041                    UART_FCR7_64BYTE);
1042             status1 = serial_in(up, UART_IIR) >> 5;
1043             serial_out(up, UART_FCR, 0);
1044             serial_out(up, UART_LCR, 0);
1045 
1046             if (status1 == 7)
1047                 up->port.type = PORT_16550A_FSL64;
1048             else
1049                 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
1050         }
1051         serial_out(up, UART_EFR, 0);
1052         return;
1053     }
1054 
1055     /*
1056      * Maybe it requires 0xbf to be written to the LCR.
1057      * (other ST16C650V2 UARTs, TI16C752A, etc)
1058      */
1059     serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1060     if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
1061         DEBUG_AUTOCONF("EFRv2 ");
1062         autoconfig_has_efr(up);
1063         return;
1064     }
1065 
1066     /*
1067      * Check for a National Semiconductor SuperIO chip.
1068      * Attempt to switch to bank 2, read the value of the LOOP bit
1069      * from EXCR1. Switch back to bank 0, change it in MCR. Then
1070      * switch back to bank 2, read it from EXCR1 again and check
1071      * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
1072      */
1073     serial_out(up, UART_LCR, 0);
1074     status1 = serial8250_in_MCR(up);
1075     serial_out(up, UART_LCR, 0xE0);
1076     status2 = serial_in(up, 0x02); /* EXCR1 */
1077 
1078     if (!((status2 ^ status1) & UART_MCR_LOOP)) {
1079         serial_out(up, UART_LCR, 0);
1080         serial8250_out_MCR(up, status1 ^ UART_MCR_LOOP);
1081         serial_out(up, UART_LCR, 0xE0);
1082         status2 = serial_in(up, 0x02); /* EXCR1 */
1083         serial_out(up, UART_LCR, 0);
1084         serial8250_out_MCR(up, status1);
1085 
1086         if ((status2 ^ status1) & UART_MCR_LOOP) {
1087             unsigned short quot;
1088 
1089             serial_out(up, UART_LCR, 0xE0);
1090 
1091             quot = serial_dl_read(up);
1092             quot <<= 3;
1093 
1094             if (ns16550a_goto_highspeed(up))
1095                 serial_dl_write(up, quot);
1096 
1097             serial_out(up, UART_LCR, 0);
1098 
1099             up->port.uartclk = 921600*16;
1100             up->port.type = PORT_NS16550A;
1101             up->capabilities |= UART_NATSEMI;
1102             return;
1103         }
1104     }
1105 
1106     /*
1107      * No EFR.  Try to detect a TI16750, which only sets bit 5 of
1108      * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1109      * Try setting it with and without DLAB set.  Cheap clones
1110      * set bit 5 without DLAB set.
1111      */
1112     serial_out(up, UART_LCR, 0);
1113     serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1114     status1 = serial_in(up, UART_IIR) >> 5;
1115     serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1116     serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1117     serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1118     status2 = serial_in(up, UART_IIR) >> 5;
1119     serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1120     serial_out(up, UART_LCR, 0);
1121 
1122     DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1123 
1124     if (status1 == 6 && status2 == 7) {
1125         up->port.type = PORT_16750;
1126         up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1127         return;
1128     }
1129 
1130     /*
1131      * Try writing and reading the UART_IER_UUE bit (b6).
1132      * If it works, this is probably one of the Xscale platform's
1133      * internal UARTs.
1134      * We're going to explicitly set the UUE bit to 0 before
1135      * trying to write and read a 1 just to make sure it's not
1136      * already a 1 and maybe locked there before we even start start.
1137      */
1138     iersave = serial_in(up, UART_IER);
1139     serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
1140     if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1141         /*
1142          * OK it's in a known zero state, try writing and reading
1143          * without disturbing the current state of the other bits.
1144          */
1145         serial_out(up, UART_IER, iersave | UART_IER_UUE);
1146         if (serial_in(up, UART_IER) & UART_IER_UUE) {
1147             /*
1148              * It's an Xscale.
1149              * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1150              */
1151             DEBUG_AUTOCONF("Xscale ");
1152             up->port.type = PORT_XSCALE;
1153             up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
1154             return;
1155         }
1156     } else {
1157         /*
1158          * If we got here we couldn't force the IER_UUE bit to 0.
1159          * Log it and continue.
1160          */
1161         DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1162     }
1163     serial_out(up, UART_IER, iersave);
1164 
1165     /*
1166      * We distinguish between 16550A and U6 16550A by counting
1167      * how many bytes are in the FIFO.
1168      */
1169     if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1170         up->port.type = PORT_U6_16550A;
1171         up->capabilities |= UART_CAP_AFE;
1172     }
1173 }
1174 
1175 /*
1176  * This routine is called by rs_init() to initialize a specific serial
1177  * port.  It determines what type of UART chip this serial port is
1178  * using: 8250, 16450, 16550, 16550A.  The important question is
1179  * whether or not this UART is a 16550A or not, since this will
1180  * determine whether or not we can use its FIFO features or not.
1181  */
1182 static void autoconfig(struct uart_8250_port *up)
1183 {
1184     unsigned char status1, scratch, scratch2, scratch3;
1185     unsigned char save_lcr, save_mcr;
1186     struct uart_port *port = &up->port;
1187     unsigned long flags;
1188     unsigned int old_capabilities;
1189 
1190     if (!port->iobase && !port->mapbase && !port->membase)
1191         return;
1192 
1193     DEBUG_AUTOCONF("%s: autoconf (0x%04lx, 0x%p): ",
1194                port->name, port->iobase, port->membase);
1195 
1196     /*
1197      * We really do need global IRQs disabled here - we're going to
1198      * be frobbing the chips IRQ enable register to see if it exists.
1199      */
1200     spin_lock_irqsave(&port->lock, flags);
1201 
1202     up->capabilities = 0;
1203     up->bugs = 0;
1204 
1205     if (!(port->flags & UPF_BUGGY_UART)) {
1206         /*
1207          * Do a simple existence test first; if we fail this,
1208          * there's no point trying anything else.
1209          *
1210          * 0x80 is used as a nonsense port to prevent against
1211          * false positives due to ISA bus float.  The
1212          * assumption is that 0x80 is a non-existent port;
1213          * which should be safe since include/asm/io.h also
1214          * makes this assumption.
1215          *
1216          * Note: this is safe as long as MCR bit 4 is clear
1217          * and the device is in "PC" mode.
1218          */
1219         scratch = serial_in(up, UART_IER);
1220         serial_out(up, UART_IER, 0);
1221 #ifdef __i386__
1222         outb(0xff, 0x080);
1223 #endif
1224         /*
1225          * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1226          * 16C754B) allow only to modify them if an EFR bit is set.
1227          */
1228         scratch2 = serial_in(up, UART_IER) & 0x0f;
1229         serial_out(up, UART_IER, 0x0F);
1230 #ifdef __i386__
1231         outb(0, 0x080);
1232 #endif
1233         scratch3 = serial_in(up, UART_IER) & 0x0f;
1234         serial_out(up, UART_IER, scratch);
1235         if (scratch2 != 0 || scratch3 != 0x0F) {
1236             /*
1237              * We failed; there's nothing here
1238              */
1239             spin_unlock_irqrestore(&port->lock, flags);
1240             DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1241                        scratch2, scratch3);
1242             goto out;
1243         }
1244     }
1245 
1246     save_mcr = serial8250_in_MCR(up);
1247     save_lcr = serial_in(up, UART_LCR);
1248 
1249     /*
1250      * Check to see if a UART is really there.  Certain broken
1251      * internal modems based on the Rockwell chipset fail this
1252      * test, because they apparently don't implement the loopback
1253      * test mode.  So this test is skipped on the COM 1 through
1254      * COM 4 ports.  This *should* be safe, since no board
1255      * manufacturer would be stupid enough to design a board
1256      * that conflicts with COM 1-4 --- we hope!
1257      */
1258     if (!(port->flags & UPF_SKIP_TEST)) {
1259         serial8250_out_MCR(up, UART_MCR_LOOP | 0x0A);
1260         status1 = serial_in(up, UART_MSR) & 0xF0;
1261         serial8250_out_MCR(up, save_mcr);
1262         if (status1 != 0x90) {
1263             spin_unlock_irqrestore(&port->lock, flags);
1264             DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1265                        status1);
1266             goto out;
1267         }
1268     }
1269 
1270     /*
1271      * We're pretty sure there's a port here.  Lets find out what
1272      * type of port it is.  The IIR top two bits allows us to find
1273      * out if it's 8250 or 16450, 16550, 16550A or later.  This
1274      * determines what we test for next.
1275      *
1276      * We also initialise the EFR (if any) to zero for later.  The
1277      * EFR occupies the same register location as the FCR and IIR.
1278      */
1279     serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1280     serial_out(up, UART_EFR, 0);
1281     serial_out(up, UART_LCR, 0);
1282 
1283     serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1284 
1285     /* Assign this as it is to truncate any bits above 7.  */
1286     scratch = serial_in(up, UART_IIR);
1287 
1288     switch (scratch >> 6) {
1289     case 0:
1290         autoconfig_8250(up);
1291         break;
1292     case 1:
1293         port->type = PORT_UNKNOWN;
1294         break;
1295     case 2:
1296         port->type = PORT_16550;
1297         break;
1298     case 3:
1299         autoconfig_16550a(up);
1300         break;
1301     }
1302 
1303 #ifdef CONFIG_SERIAL_8250_RSA
1304     /*
1305      * Only probe for RSA ports if we got the region.
1306      */
1307     if (port->type == PORT_16550A && up->probe & UART_PROBE_RSA &&
1308         __enable_rsa(up))
1309         port->type = PORT_RSA;
1310 #endif
1311 
1312     serial_out(up, UART_LCR, save_lcr);
1313 
1314     port->fifosize = uart_config[up->port.type].fifo_size;
1315     old_capabilities = up->capabilities;
1316     up->capabilities = uart_config[port->type].flags;
1317     up->tx_loadsz = uart_config[port->type].tx_loadsz;
1318 
1319     if (port->type == PORT_UNKNOWN)
1320         goto out_unlock;
1321 
1322     /*
1323      * Reset the UART.
1324      */
1325 #ifdef CONFIG_SERIAL_8250_RSA
1326     if (port->type == PORT_RSA)
1327         serial_out(up, UART_RSA_FRR, 0);
1328 #endif
1329     serial8250_out_MCR(up, save_mcr);
1330     serial8250_clear_fifos(up);
1331     serial_in(up, UART_RX);
1332     if (up->capabilities & UART_CAP_UUE)
1333         serial_out(up, UART_IER, UART_IER_UUE);
1334     else
1335         serial_out(up, UART_IER, 0);
1336 
1337 out_unlock:
1338     spin_unlock_irqrestore(&port->lock, flags);
1339 
1340     /*
1341      * Check if the device is a Fintek F81216A
1342      */
1343     if (port->type == PORT_16550A && port->iotype == UPIO_PORT)
1344         fintek_8250_probe(up);
1345 
1346     if (up->capabilities != old_capabilities) {
1347         dev_warn(port->dev, "detected caps %08x should be %08x\n",
1348              old_capabilities, up->capabilities);
1349     }
1350 out:
1351     DEBUG_AUTOCONF("iir=%d ", scratch);
1352     DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
1353 }
1354 
1355 static void autoconfig_irq(struct uart_8250_port *up)
1356 {
1357     struct uart_port *port = &up->port;
1358     unsigned char save_mcr, save_ier;
1359     unsigned char save_ICP = 0;
1360     unsigned int ICP = 0;
1361     unsigned long irqs;
1362     int irq;
1363 
1364     if (port->flags & UPF_FOURPORT) {
1365         ICP = (port->iobase & 0xfe0) | 0x1f;
1366         save_ICP = inb_p(ICP);
1367         outb_p(0x80, ICP);
1368         inb_p(ICP);
1369     }
1370 
1371     if (uart_console(port))
1372         console_lock();
1373 
1374     /* forget possible initially masked and pending IRQ */
1375     probe_irq_off(probe_irq_on());
1376     save_mcr = serial8250_in_MCR(up);
1377     save_ier = serial_in(up, UART_IER);
1378     serial8250_out_MCR(up, UART_MCR_OUT1 | UART_MCR_OUT2);
1379 
1380     irqs = probe_irq_on();
1381     serial8250_out_MCR(up, 0);
1382     udelay(10);
1383     if (port->flags & UPF_FOURPORT) {
1384         serial8250_out_MCR(up, UART_MCR_DTR | UART_MCR_RTS);
1385     } else {
1386         serial8250_out_MCR(up,
1387             UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1388     }
1389     serial_out(up, UART_IER, 0x0f); /* enable all intrs */
1390     serial_in(up, UART_LSR);
1391     serial_in(up, UART_RX);
1392     serial_in(up, UART_IIR);
1393     serial_in(up, UART_MSR);
1394     serial_out(up, UART_TX, 0xFF);
1395     udelay(20);
1396     irq = probe_irq_off(irqs);
1397 
1398     serial8250_out_MCR(up, save_mcr);
1399     serial_out(up, UART_IER, save_ier);
1400 
1401     if (port->flags & UPF_FOURPORT)
1402         outb_p(save_ICP, ICP);
1403 
1404     if (uart_console(port))
1405         console_unlock();
1406 
1407     port->irq = (irq > 0) ? irq : 0;
1408 }
1409 
1410 static void serial8250_stop_rx(struct uart_port *port)
1411 {
1412     struct uart_8250_port *up = up_to_u8250p(port);
1413 
1414     serial8250_rpm_get(up);
1415 
1416     up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1417     up->port.read_status_mask &= ~UART_LSR_DR;
1418     serial_port_out(port, UART_IER, up->ier);
1419 
1420     serial8250_rpm_put(up);
1421 }
1422 
1423 /**
1424  * serial8250_em485_stop_tx() - generic ->rs485_stop_tx() callback
1425  * @p: uart 8250 port
1426  *
1427  * Generic callback usable by 8250 uart drivers to stop rs485 transmission.
1428  */
1429 void serial8250_em485_stop_tx(struct uart_8250_port *p)
1430 {
1431     unsigned char mcr = serial8250_in_MCR(p);
1432 
1433     if (p->port.rs485.flags & SER_RS485_RTS_AFTER_SEND)
1434         mcr |= UART_MCR_RTS;
1435     else
1436         mcr &= ~UART_MCR_RTS;
1437     serial8250_out_MCR(p, mcr);
1438 
1439     /*
1440      * Empty the RX FIFO, we are not interested in anything
1441      * received during the half-duplex transmission.
1442      * Enable previously disabled RX interrupts.
1443      */
1444     if (!(p->port.rs485.flags & SER_RS485_RX_DURING_TX)) {
1445         serial8250_clear_and_reinit_fifos(p);
1446 
1447         p->ier |= UART_IER_RLSI | UART_IER_RDI;
1448         serial_port_out(&p->port, UART_IER, p->ier);
1449     }
1450 }
1451 EXPORT_SYMBOL_GPL(serial8250_em485_stop_tx);
1452 
1453 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t)
1454 {
1455     struct uart_8250_em485 *em485 = container_of(t, struct uart_8250_em485,
1456             stop_tx_timer);
1457     struct uart_8250_port *p = em485->port;
1458     unsigned long flags;
1459 
1460     serial8250_rpm_get(p);
1461     spin_lock_irqsave(&p->port.lock, flags);
1462     if (em485->active_timer == &em485->stop_tx_timer) {
1463         p->rs485_stop_tx(p);
1464         em485->active_timer = NULL;
1465         em485->tx_stopped = true;
1466     }
1467     spin_unlock_irqrestore(&p->port.lock, flags);
1468     serial8250_rpm_put(p);
1469 
1470     return HRTIMER_NORESTART;
1471 }
1472 
1473 static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec)
1474 {
1475     hrtimer_start(hrt, ms_to_ktime(msec), HRTIMER_MODE_REL);
1476 }
1477 
1478 static void __stop_tx_rs485(struct uart_8250_port *p, u64 stop_delay)
1479 {
1480     struct uart_8250_em485 *em485 = p->em485;
1481 
1482     stop_delay += (u64)p->port.rs485.delay_rts_after_send * NSEC_PER_MSEC;
1483 
1484     /*
1485      * rs485_stop_tx() is going to set RTS according to config
1486      * AND flush RX FIFO if required.
1487      */
1488     if (stop_delay > 0) {
1489         em485->active_timer = &em485->stop_tx_timer;
1490         hrtimer_start(&em485->stop_tx_timer, ns_to_ktime(stop_delay), HRTIMER_MODE_REL);
1491     } else {
1492         p->rs485_stop_tx(p);
1493         em485->active_timer = NULL;
1494         em485->tx_stopped = true;
1495     }
1496 }
1497 
1498 static inline void __stop_tx(struct uart_8250_port *p)
1499 {
1500     struct uart_8250_em485 *em485 = p->em485;
1501 
1502     if (em485) {
1503         u16 lsr = serial_lsr_in(p);
1504         u64 stop_delay = 0;
1505 
1506         p->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1507 
1508         if (!(lsr & UART_LSR_THRE))
1509             return;
1510         /*
1511          * To provide required timing and allow FIFO transfer,
1512          * __stop_tx_rs485() must be called only when both FIFO and
1513          * shift register are empty. The device driver should either
1514          * enable interrupt on TEMT or set UART_CAP_NOTEMT that will
1515          * enlarge stop_tx_timer by the tx time of one frame to cover
1516          * for emptying of the shift register.
1517          */
1518         if (!(lsr & UART_LSR_TEMT)) {
1519             if (!(p->capabilities & UART_CAP_NOTEMT))
1520                 return;
1521             /*
1522              * RTS might get deasserted too early with the normal
1523              * frame timing formula. It seems to suggest THRE might
1524              * get asserted already during tx of the stop bit
1525              * rather than after it is fully sent.
1526              * Roughly estimate 1 extra bit here with / 7.
1527              */
1528             stop_delay = p->port.frame_time + DIV_ROUND_UP(p->port.frame_time, 7);
1529         }
1530 
1531         __stop_tx_rs485(p, stop_delay);
1532     }
1533 
1534     if (serial8250_clear_THRI(p))
1535         serial8250_rpm_put_tx(p);
1536 }
1537 
1538 static void serial8250_stop_tx(struct uart_port *port)
1539 {
1540     struct uart_8250_port *up = up_to_u8250p(port);
1541 
1542     serial8250_rpm_get(up);
1543     __stop_tx(up);
1544 
1545     /*
1546      * We really want to stop the transmitter from sending.
1547      */
1548     if (port->type == PORT_16C950) {
1549         up->acr |= UART_ACR_TXDIS;
1550         serial_icr_write(up, UART_ACR, up->acr);
1551     }
1552     serial8250_rpm_put(up);
1553 }
1554 
1555 static inline void __start_tx(struct uart_port *port)
1556 {
1557     struct uart_8250_port *up = up_to_u8250p(port);
1558 
1559     if (up->dma && !up->dma->tx_dma(up))
1560         return;
1561 
1562     if (serial8250_set_THRI(up)) {
1563         if (up->bugs & UART_BUG_TXEN) {
1564             u16 lsr = serial_lsr_in(up);
1565 
1566             if (lsr & UART_LSR_THRE)
1567                 serial8250_tx_chars(up);
1568         }
1569     }
1570 
1571     /*
1572      * Re-enable the transmitter if we disabled it.
1573      */
1574     if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1575         up->acr &= ~UART_ACR_TXDIS;
1576         serial_icr_write(up, UART_ACR, up->acr);
1577     }
1578 }
1579 
1580 /**
1581  * serial8250_em485_start_tx() - generic ->rs485_start_tx() callback
1582  * @up: uart 8250 port
1583  *
1584  * Generic callback usable by 8250 uart drivers to start rs485 transmission.
1585  * Assumes that setting the RTS bit in the MCR register means RTS is high.
1586  * (Some chips use inverse semantics.)  Further assumes that reception is
1587  * stoppable by disabling the UART_IER_RDI interrupt.  (Some chips set the
1588  * UART_LSR_DR bit even when UART_IER_RDI is disabled, foiling this approach.)
1589  */
1590 void serial8250_em485_start_tx(struct uart_8250_port *up)
1591 {
1592     unsigned char mcr = serial8250_in_MCR(up);
1593 
1594     if (!(up->port.rs485.flags & SER_RS485_RX_DURING_TX))
1595         serial8250_stop_rx(&up->port);
1596 
1597     if (up->port.rs485.flags & SER_RS485_RTS_ON_SEND)
1598         mcr |= UART_MCR_RTS;
1599     else
1600         mcr &= ~UART_MCR_RTS;
1601     serial8250_out_MCR(up, mcr);
1602 }
1603 EXPORT_SYMBOL_GPL(serial8250_em485_start_tx);
1604 
1605 /* Returns false, if start_tx_timer was setup to defer TX start */
1606 static bool start_tx_rs485(struct uart_port *port)
1607 {
1608     struct uart_8250_port *up = up_to_u8250p(port);
1609     struct uart_8250_em485 *em485 = up->em485;
1610 
1611     /*
1612      * While serial8250_em485_handle_stop_tx() is a noop if
1613      * em485->active_timer != &em485->stop_tx_timer, it might happen that
1614      * the timer is still armed and triggers only after the current bunch of
1615      * chars is send and em485->active_timer == &em485->stop_tx_timer again.
1616      * So cancel the timer. There is still a theoretical race condition if
1617      * the timer is already running and only comes around to check for
1618      * em485->active_timer when &em485->stop_tx_timer is armed again.
1619      */
1620     if (em485->active_timer == &em485->stop_tx_timer)
1621         hrtimer_try_to_cancel(&em485->stop_tx_timer);
1622 
1623     em485->active_timer = NULL;
1624 
1625     if (em485->tx_stopped) {
1626         em485->tx_stopped = false;
1627 
1628         up->rs485_start_tx(up);
1629 
1630         if (up->port.rs485.delay_rts_before_send > 0) {
1631             em485->active_timer = &em485->start_tx_timer;
1632             start_hrtimer_ms(&em485->start_tx_timer,
1633                      up->port.rs485.delay_rts_before_send);
1634             return false;
1635         }
1636     }
1637 
1638     return true;
1639 }
1640 
1641 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t)
1642 {
1643     struct uart_8250_em485 *em485 = container_of(t, struct uart_8250_em485,
1644             start_tx_timer);
1645     struct uart_8250_port *p = em485->port;
1646     unsigned long flags;
1647 
1648     spin_lock_irqsave(&p->port.lock, flags);
1649     if (em485->active_timer == &em485->start_tx_timer) {
1650         __start_tx(&p->port);
1651         em485->active_timer = NULL;
1652     }
1653     spin_unlock_irqrestore(&p->port.lock, flags);
1654 
1655     return HRTIMER_NORESTART;
1656 }
1657 
1658 static void serial8250_start_tx(struct uart_port *port)
1659 {
1660     struct uart_8250_port *up = up_to_u8250p(port);
1661     struct uart_8250_em485 *em485 = up->em485;
1662 
1663     if (!port->x_char && uart_circ_empty(&port->state->xmit))
1664         return;
1665 
1666     serial8250_rpm_get_tx(up);
1667 
1668     if (em485) {
1669         if ((em485->active_timer == &em485->start_tx_timer) ||
1670             !start_tx_rs485(port))
1671             return;
1672     }
1673     __start_tx(port);
1674 }
1675 
1676 static void serial8250_throttle(struct uart_port *port)
1677 {
1678     port->throttle(port);
1679 }
1680 
1681 static void serial8250_unthrottle(struct uart_port *port)
1682 {
1683     port->unthrottle(port);
1684 }
1685 
1686 static void serial8250_disable_ms(struct uart_port *port)
1687 {
1688     struct uart_8250_port *up = up_to_u8250p(port);
1689 
1690     /* no MSR capabilities */
1691     if (up->bugs & UART_BUG_NOMSR)
1692         return;
1693 
1694     mctrl_gpio_disable_ms(up->gpios);
1695 
1696     up->ier &= ~UART_IER_MSI;
1697     serial_port_out(port, UART_IER, up->ier);
1698 }
1699 
1700 static void serial8250_enable_ms(struct uart_port *port)
1701 {
1702     struct uart_8250_port *up = up_to_u8250p(port);
1703 
1704     /* no MSR capabilities */
1705     if (up->bugs & UART_BUG_NOMSR)
1706         return;
1707 
1708     mctrl_gpio_enable_ms(up->gpios);
1709 
1710     up->ier |= UART_IER_MSI;
1711 
1712     serial8250_rpm_get(up);
1713     serial_port_out(port, UART_IER, up->ier);
1714     serial8250_rpm_put(up);
1715 }
1716 
1717 void serial8250_read_char(struct uart_8250_port *up, u16 lsr)
1718 {
1719     struct uart_port *port = &up->port;
1720     unsigned char ch;
1721     char flag = TTY_NORMAL;
1722 
1723     if (likely(lsr & UART_LSR_DR))
1724         ch = serial_in(up, UART_RX);
1725     else
1726         /*
1727          * Intel 82571 has a Serial Over Lan device that will
1728          * set UART_LSR_BI without setting UART_LSR_DR when
1729          * it receives a break. To avoid reading from the
1730          * receive buffer without UART_LSR_DR bit set, we
1731          * just force the read character to be 0
1732          */
1733         ch = 0;
1734 
1735     port->icount.rx++;
1736 
1737     lsr |= up->lsr_saved_flags;
1738     up->lsr_saved_flags = 0;
1739 
1740     if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1741         if (lsr & UART_LSR_BI) {
1742             lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1743             port->icount.brk++;
1744             /*
1745              * We do the SysRQ and SAK checking
1746              * here because otherwise the break
1747              * may get masked by ignore_status_mask
1748              * or read_status_mask.
1749              */
1750             if (uart_handle_break(port))
1751                 return;
1752         } else if (lsr & UART_LSR_PE)
1753             port->icount.parity++;
1754         else if (lsr & UART_LSR_FE)
1755             port->icount.frame++;
1756         if (lsr & UART_LSR_OE)
1757             port->icount.overrun++;
1758 
1759         /*
1760          * Mask off conditions which should be ignored.
1761          */
1762         lsr &= port->read_status_mask;
1763 
1764         if (lsr & UART_LSR_BI) {
1765             dev_dbg(port->dev, "handling break\n");
1766             flag = TTY_BREAK;
1767         } else if (lsr & UART_LSR_PE)
1768             flag = TTY_PARITY;
1769         else if (lsr & UART_LSR_FE)
1770             flag = TTY_FRAME;
1771     }
1772     if (uart_prepare_sysrq_char(port, ch))
1773         return;
1774 
1775     uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
1776 }
1777 EXPORT_SYMBOL_GPL(serial8250_read_char);
1778 
1779 /*
1780  * serial8250_rx_chars - Read characters. The first LSR value must be passed in.
1781  *
1782  * Returns LSR bits. The caller should rely only on non-Rx related LSR bits
1783  * (such as THRE) because the LSR value might come from an already consumed
1784  * character.
1785  */
1786 u16 serial8250_rx_chars(struct uart_8250_port *up, u16 lsr)
1787 {
1788     struct uart_port *port = &up->port;
1789     int max_count = 256;
1790 
1791     do {
1792         serial8250_read_char(up, lsr);
1793         if (--max_count == 0)
1794             break;
1795         lsr = serial_in(up, UART_LSR);
1796     } while (lsr & (UART_LSR_DR | UART_LSR_BI));
1797 
1798     tty_flip_buffer_push(&port->state->port);
1799     return lsr;
1800 }
1801 EXPORT_SYMBOL_GPL(serial8250_rx_chars);
1802 
1803 void serial8250_tx_chars(struct uart_8250_port *up)
1804 {
1805     struct uart_port *port = &up->port;
1806     struct circ_buf *xmit = &port->state->xmit;
1807     int count;
1808 
1809     if (port->x_char) {
1810         uart_xchar_out(port, UART_TX);
1811         return;
1812     }
1813     if (uart_tx_stopped(port)) {
1814         serial8250_stop_tx(port);
1815         return;
1816     }
1817     if (uart_circ_empty(xmit)) {
1818         __stop_tx(up);
1819         return;
1820     }
1821 
1822     count = up->tx_loadsz;
1823     do {
1824         serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1825         if (up->bugs & UART_BUG_TXRACE) {
1826             /*
1827              * The Aspeed BMC virtual UARTs have a bug where data
1828              * may get stuck in the BMC's Tx FIFO from bursts of
1829              * writes on the APB interface.
1830              *
1831              * Delay back-to-back writes by a read cycle to avoid
1832              * stalling the VUART. Read a register that won't have
1833              * side-effects and discard the result.
1834              */
1835             serial_in(up, UART_SCR);
1836         }
1837         xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1838         port->icount.tx++;
1839         if (uart_circ_empty(xmit))
1840             break;
1841         if ((up->capabilities & UART_CAP_HFIFO) &&
1842             !uart_lsr_tx_empty(serial_in(up, UART_LSR)))
1843             break;
1844         /* The BCM2835 MINI UART THRE bit is really a not-full bit. */
1845         if ((up->capabilities & UART_CAP_MINI) &&
1846             !(serial_in(up, UART_LSR) & UART_LSR_THRE))
1847             break;
1848     } while (--count > 0);
1849 
1850     if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1851         uart_write_wakeup(port);
1852 
1853     /*
1854      * With RPM enabled, we have to wait until the FIFO is empty before the
1855      * HW can go idle. So we get here once again with empty FIFO and disable
1856      * the interrupt and RPM in __stop_tx()
1857      */
1858     if (uart_circ_empty(xmit) && !(up->capabilities & UART_CAP_RPM))
1859         __stop_tx(up);
1860 }
1861 EXPORT_SYMBOL_GPL(serial8250_tx_chars);
1862 
1863 /* Caller holds uart port lock */
1864 unsigned int serial8250_modem_status(struct uart_8250_port *up)
1865 {
1866     struct uart_port *port = &up->port;
1867     unsigned int status = serial_in(up, UART_MSR);
1868 
1869     status |= up->msr_saved_flags;
1870     up->msr_saved_flags = 0;
1871     if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1872         port->state != NULL) {
1873         if (status & UART_MSR_TERI)
1874             port->icount.rng++;
1875         if (status & UART_MSR_DDSR)
1876             port->icount.dsr++;
1877         if (status & UART_MSR_DDCD)
1878             uart_handle_dcd_change(port, status & UART_MSR_DCD);
1879         if (status & UART_MSR_DCTS)
1880             uart_handle_cts_change(port, status & UART_MSR_CTS);
1881 
1882         wake_up_interruptible(&port->state->port.delta_msr_wait);
1883     }
1884 
1885     return status;
1886 }
1887 EXPORT_SYMBOL_GPL(serial8250_modem_status);
1888 
1889 static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir)
1890 {
1891     switch (iir & 0x3f) {
1892     case UART_IIR_RX_TIMEOUT:
1893         serial8250_rx_dma_flush(up);
1894         fallthrough;
1895     case UART_IIR_RLSI:
1896         return true;
1897     }
1898     return up->dma->rx_dma(up);
1899 }
1900 
1901 /*
1902  * This handles the interrupt from one port.
1903  */
1904 int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
1905 {
1906     struct uart_8250_port *up = up_to_u8250p(port);
1907     bool skip_rx = false;
1908     unsigned long flags;
1909     u16 status;
1910 
1911     if (iir & UART_IIR_NO_INT)
1912         return 0;
1913 
1914     spin_lock_irqsave(&port->lock, flags);
1915 
1916     status = serial_lsr_in(up);
1917 
1918     /*
1919      * If port is stopped and there are no error conditions in the
1920      * FIFO, then don't drain the FIFO, as this may lead to TTY buffer
1921      * overflow. Not servicing, RX FIFO would trigger auto HW flow
1922      * control when FIFO occupancy reaches preset threshold, thus
1923      * halting RX. This only works when auto HW flow control is
1924      * available.
1925      */
1926     if (!(status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS)) &&
1927         (port->status & (UPSTAT_AUTOCTS | UPSTAT_AUTORTS)) &&
1928         !(port->read_status_mask & UART_LSR_DR))
1929         skip_rx = true;
1930 
1931     if (status & (UART_LSR_DR | UART_LSR_BI) && !skip_rx) {
1932         if (!up->dma || handle_rx_dma(up, iir))
1933             status = serial8250_rx_chars(up, status);
1934     }
1935     serial8250_modem_status(up);
1936     if ((status & UART_LSR_THRE) && (up->ier & UART_IER_THRI)) {
1937         if (!up->dma || up->dma->tx_err)
1938             serial8250_tx_chars(up);
1939         else if (!up->dma->tx_running)
1940             __stop_tx(up);
1941     }
1942 
1943     uart_unlock_and_check_sysrq_irqrestore(port, flags);
1944 
1945     return 1;
1946 }
1947 EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1948 
1949 static int serial8250_default_handle_irq(struct uart_port *port)
1950 {
1951     struct uart_8250_port *up = up_to_u8250p(port);
1952     unsigned int iir;
1953     int ret;
1954 
1955     serial8250_rpm_get(up);
1956 
1957     iir = serial_port_in(port, UART_IIR);
1958     ret = serial8250_handle_irq(port, iir);
1959 
1960     serial8250_rpm_put(up);
1961     return ret;
1962 }
1963 
1964 /*
1965  * Newer 16550 compatible parts such as the SC16C650 & Altera 16550 Soft IP
1966  * have a programmable TX threshold that triggers the THRE interrupt in
1967  * the IIR register. In this case, the THRE interrupt indicates the FIFO
1968  * has space available. Load it up with tx_loadsz bytes.
1969  */
1970 static int serial8250_tx_threshold_handle_irq(struct uart_port *port)
1971 {
1972     unsigned long flags;
1973     unsigned int iir = serial_port_in(port, UART_IIR);
1974 
1975     /* TX Threshold IRQ triggered so load up FIFO */
1976     if ((iir & UART_IIR_ID) == UART_IIR_THRI) {
1977         struct uart_8250_port *up = up_to_u8250p(port);
1978 
1979         spin_lock_irqsave(&port->lock, flags);
1980         serial8250_tx_chars(up);
1981         spin_unlock_irqrestore(&port->lock, flags);
1982     }
1983 
1984     iir = serial_port_in(port, UART_IIR);
1985     return serial8250_handle_irq(port, iir);
1986 }
1987 
1988 static unsigned int serial8250_tx_empty(struct uart_port *port)
1989 {
1990     struct uart_8250_port *up = up_to_u8250p(port);
1991     unsigned long flags;
1992     u16 lsr;
1993 
1994     serial8250_rpm_get(up);
1995 
1996     spin_lock_irqsave(&port->lock, flags);
1997     lsr = serial_lsr_in(up);
1998     spin_unlock_irqrestore(&port->lock, flags);
1999 
2000     serial8250_rpm_put(up);
2001 
2002     return uart_lsr_tx_empty(lsr) ? TIOCSER_TEMT : 0;
2003 }
2004 
2005 unsigned int serial8250_do_get_mctrl(struct uart_port *port)
2006 {
2007     struct uart_8250_port *up = up_to_u8250p(port);
2008     unsigned int status;
2009     unsigned int val;
2010 
2011     serial8250_rpm_get(up);
2012     status = serial8250_modem_status(up);
2013     serial8250_rpm_put(up);
2014 
2015     val = serial8250_MSR_to_TIOCM(status);
2016     if (up->gpios)
2017         return mctrl_gpio_get(up->gpios, &val);
2018 
2019     return val;
2020 }
2021 EXPORT_SYMBOL_GPL(serial8250_do_get_mctrl);
2022 
2023 static unsigned int serial8250_get_mctrl(struct uart_port *port)
2024 {
2025     if (port->get_mctrl)
2026         return port->get_mctrl(port);
2027     return serial8250_do_get_mctrl(port);
2028 }
2029 
2030 void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl)
2031 {
2032     struct uart_8250_port *up = up_to_u8250p(port);
2033     unsigned char mcr;
2034 
2035     mcr = serial8250_TIOCM_to_MCR(mctrl);
2036 
2037     mcr |= up->mcr;
2038 
2039     serial8250_out_MCR(up, mcr);
2040 }
2041 EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);
2042 
2043 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
2044 {
2045     if (port->set_mctrl)
2046         port->set_mctrl(port, mctrl);
2047     else
2048         serial8250_do_set_mctrl(port, mctrl);
2049 }
2050 
2051 static void serial8250_break_ctl(struct uart_port *port, int break_state)
2052 {
2053     struct uart_8250_port *up = up_to_u8250p(port);
2054     unsigned long flags;
2055 
2056     serial8250_rpm_get(up);
2057     spin_lock_irqsave(&port->lock, flags);
2058     if (break_state == -1)
2059         up->lcr |= UART_LCR_SBC;
2060     else
2061         up->lcr &= ~UART_LCR_SBC;
2062     serial_port_out(port, UART_LCR, up->lcr);
2063     spin_unlock_irqrestore(&port->lock, flags);
2064     serial8250_rpm_put(up);
2065 }
2066 
2067 static void wait_for_lsr(struct uart_8250_port *up, int bits)
2068 {
2069     unsigned int status, tmout = 10000;
2070 
2071     /* Wait up to 10ms for the character(s) to be sent. */
2072     for (;;) {
2073         status = serial_lsr_in(up);
2074 
2075         if ((status & bits) == bits)
2076             break;
2077         if (--tmout == 0)
2078             break;
2079         udelay(1);
2080         touch_nmi_watchdog();
2081     }
2082 }
2083 
2084 /*
2085  *  Wait for transmitter & holding register to empty
2086  */
2087 static void wait_for_xmitr(struct uart_8250_port *up, int bits)
2088 {
2089     unsigned int tmout;
2090 
2091     wait_for_lsr(up, bits);
2092 
2093     /* Wait up to 1s for flow control if necessary */
2094     if (up->port.flags & UPF_CONS_FLOW) {
2095         for (tmout = 1000000; tmout; tmout--) {
2096             unsigned int msr = serial_in(up, UART_MSR);
2097             up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
2098             if (msr & UART_MSR_CTS)
2099                 break;
2100             udelay(1);
2101             touch_nmi_watchdog();
2102         }
2103     }
2104 }
2105 
2106 #ifdef CONFIG_CONSOLE_POLL
2107 /*
2108  * Console polling routines for writing and reading from the uart while
2109  * in an interrupt or debug context.
2110  */
2111 
2112 static int serial8250_get_poll_char(struct uart_port *port)
2113 {
2114     struct uart_8250_port *up = up_to_u8250p(port);
2115     int status;
2116     u16 lsr;
2117 
2118     serial8250_rpm_get(up);
2119 
2120     lsr = serial_port_in(port, UART_LSR);
2121 
2122     if (!(lsr & UART_LSR_DR)) {
2123         status = NO_POLL_CHAR;
2124         goto out;
2125     }
2126 
2127     status = serial_port_in(port, UART_RX);
2128 out:
2129     serial8250_rpm_put(up);
2130     return status;
2131 }
2132 
2133 
2134 static void serial8250_put_poll_char(struct uart_port *port,
2135              unsigned char c)
2136 {
2137     unsigned int ier;
2138     struct uart_8250_port *up = up_to_u8250p(port);
2139 
2140     serial8250_rpm_get(up);
2141     /*
2142      *  First save the IER then disable the interrupts
2143      */
2144     ier = serial_port_in(port, UART_IER);
2145     if (up->capabilities & UART_CAP_UUE)
2146         serial_port_out(port, UART_IER, UART_IER_UUE);
2147     else
2148         serial_port_out(port, UART_IER, 0);
2149 
2150     wait_for_xmitr(up, UART_LSR_BOTH_EMPTY);
2151     /*
2152      *  Send the character out.
2153      */
2154     serial_port_out(port, UART_TX, c);
2155 
2156     /*
2157      *  Finally, wait for transmitter to become empty
2158      *  and restore the IER
2159      */
2160     wait_for_xmitr(up, UART_LSR_BOTH_EMPTY);
2161     serial_port_out(port, UART_IER, ier);
2162     serial8250_rpm_put(up);
2163 }
2164 
2165 #endif /* CONFIG_CONSOLE_POLL */
2166 
2167 int serial8250_do_startup(struct uart_port *port)
2168 {
2169     struct uart_8250_port *up = up_to_u8250p(port);
2170     unsigned long flags;
2171     unsigned char iir;
2172     int retval;
2173     u16 lsr;
2174 
2175     if (!port->fifosize)
2176         port->fifosize = uart_config[port->type].fifo_size;
2177     if (!up->tx_loadsz)
2178         up->tx_loadsz = uart_config[port->type].tx_loadsz;
2179     if (!up->capabilities)
2180         up->capabilities = uart_config[port->type].flags;
2181     up->mcr = 0;
2182 
2183     if (port->iotype != up->cur_iotype)
2184         set_io_from_upio(port);
2185 
2186     serial8250_rpm_get(up);
2187     if (port->type == PORT_16C950) {
2188         /* Wake up and initialize UART */
2189         up->acr = 0;
2190         serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2191         serial_port_out(port, UART_EFR, UART_EFR_ECB);
2192         serial_port_out(port, UART_IER, 0);
2193         serial_port_out(port, UART_LCR, 0);
2194         serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2195         serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2196         serial_port_out(port, UART_EFR, UART_EFR_ECB);
2197         serial_port_out(port, UART_LCR, 0);
2198     }
2199 
2200     if (port->type == PORT_DA830) {
2201         /* Reset the port */
2202         serial_port_out(port, UART_IER, 0);
2203         serial_port_out(port, UART_DA830_PWREMU_MGMT, 0);
2204         mdelay(10);
2205 
2206         /* Enable Tx, Rx and free run mode */
2207         serial_port_out(port, UART_DA830_PWREMU_MGMT,
2208                 UART_DA830_PWREMU_MGMT_UTRST |
2209                 UART_DA830_PWREMU_MGMT_URRST |
2210                 UART_DA830_PWREMU_MGMT_FREE);
2211     }
2212 
2213     if (port->type == PORT_NPCM) {
2214         /*
2215          * Nuvoton calls the scratch register 'UART_TOR' (timeout
2216          * register). Enable it, and set TIOC (timeout interrupt
2217          * comparator) to be 0x20 for correct operation.
2218          */
2219         serial_port_out(port, UART_NPCM_TOR, UART_NPCM_TOIE | 0x20);
2220     }
2221 
2222 #ifdef CONFIG_SERIAL_8250_RSA
2223     /*
2224      * If this is an RSA port, see if we can kick it up to the
2225      * higher speed clock.
2226      */
2227     enable_rsa(up);
2228 #endif
2229 
2230     /*
2231      * Clear the FIFO buffers and disable them.
2232      * (they will be reenabled in set_termios())
2233      */
2234     serial8250_clear_fifos(up);
2235 
2236     /*
2237      * Clear the interrupt registers.
2238      */
2239     serial_port_in(port, UART_LSR);
2240     serial_port_in(port, UART_RX);
2241     serial_port_in(port, UART_IIR);
2242     serial_port_in(port, UART_MSR);
2243 
2244     /*
2245      * At this point, there's no way the LSR could still be 0xff;
2246      * if it is, then bail out, because there's likely no UART
2247      * here.
2248      */
2249     if (!(port->flags & UPF_BUGGY_UART) &&
2250         (serial_port_in(port, UART_LSR) == 0xff)) {
2251         dev_info_ratelimited(port->dev, "LSR safety check engaged!\n");
2252         retval = -ENODEV;
2253         goto out;
2254     }
2255 
2256     /*
2257      * For a XR16C850, we need to set the trigger levels
2258      */
2259     if (port->type == PORT_16850) {
2260         unsigned char fctr;
2261 
2262         serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
2263 
2264         fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2265         serial_port_out(port, UART_FCTR,
2266                 fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2267         serial_port_out(port, UART_TRG, UART_TRG_96);
2268         serial_port_out(port, UART_FCTR,
2269                 fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2270         serial_port_out(port, UART_TRG, UART_TRG_96);
2271 
2272         serial_port_out(port, UART_LCR, 0);
2273     }
2274 
2275     /*
2276      * For the Altera 16550 variants, set TX threshold trigger level.
2277      */
2278     if (((port->type == PORT_ALTR_16550_F32) ||
2279          (port->type == PORT_ALTR_16550_F64) ||
2280          (port->type == PORT_ALTR_16550_F128)) && (port->fifosize > 1)) {
2281         /* Bounds checking of TX threshold (valid 0 to fifosize-2) */
2282         if ((up->tx_loadsz < 2) || (up->tx_loadsz > port->fifosize)) {
2283             dev_err(port->dev, "TX FIFO Threshold errors, skipping\n");
2284         } else {
2285             serial_port_out(port, UART_ALTR_AFR,
2286                     UART_ALTR_EN_TXFIFO_LW);
2287             serial_port_out(port, UART_ALTR_TX_LOW,
2288                     port->fifosize - up->tx_loadsz);
2289             port->handle_irq = serial8250_tx_threshold_handle_irq;
2290         }
2291     }
2292 
2293     /* Check if we need to have shared IRQs */
2294     if (port->irq && (up->port.flags & UPF_SHARE_IRQ))
2295         up->port.irqflags |= IRQF_SHARED;
2296 
2297     if (port->irq && !(up->port.flags & UPF_NO_THRE_TEST)) {
2298         unsigned char iir1;
2299 
2300         if (port->irqflags & IRQF_SHARED)
2301             disable_irq_nosync(port->irq);
2302 
2303         /*
2304          * Test for UARTs that do not reassert THRE when the
2305          * transmitter is idle and the interrupt has already
2306          * been cleared.  Real 16550s should always reassert
2307          * this interrupt whenever the transmitter is idle and
2308          * the interrupt is enabled.  Delays are necessary to
2309          * allow register changes to become visible.
2310          */
2311         spin_lock_irqsave(&port->lock, flags);
2312 
2313         wait_for_xmitr(up, UART_LSR_THRE);
2314         serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2315         udelay(1); /* allow THRE to set */
2316         iir1 = serial_port_in(port, UART_IIR);
2317         serial_port_out(port, UART_IER, 0);
2318         serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2319         udelay(1); /* allow a working UART time to re-assert THRE */
2320         iir = serial_port_in(port, UART_IIR);
2321         serial_port_out(port, UART_IER, 0);
2322 
2323         spin_unlock_irqrestore(&port->lock, flags);
2324 
2325         if (port->irqflags & IRQF_SHARED)
2326             enable_irq(port->irq);
2327 
2328         /*
2329          * If the interrupt is not reasserted, or we otherwise
2330          * don't trust the iir, setup a timer to kick the UART
2331          * on a regular basis.
2332          */
2333         if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
2334             up->port.flags & UPF_BUG_THRE) {
2335             up->bugs |= UART_BUG_THRE;
2336         }
2337     }
2338 
2339     retval = up->ops->setup_irq(up);
2340     if (retval)
2341         goto out;
2342 
2343     /*
2344      * Now, initialize the UART
2345      */
2346     serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
2347 
2348     spin_lock_irqsave(&port->lock, flags);
2349     if (up->port.flags & UPF_FOURPORT) {
2350         if (!up->port.irq)
2351             up->port.mctrl |= TIOCM_OUT1;
2352     } else
2353         /*
2354          * Most PC uarts need OUT2 raised to enable interrupts.
2355          */
2356         if (port->irq)
2357             up->port.mctrl |= TIOCM_OUT2;
2358 
2359     serial8250_set_mctrl(port, port->mctrl);
2360 
2361     /*
2362      * Serial over Lan (SoL) hack:
2363      * Intel 8257x Gigabit ethernet chips have a 16550 emulation, to be
2364      * used for Serial Over Lan.  Those chips take a longer time than a
2365      * normal serial device to signalize that a transmission data was
2366      * queued. Due to that, the above test generally fails. One solution
2367      * would be to delay the reading of iir. However, this is not
2368      * reliable, since the timeout is variable. So, let's just don't
2369      * test if we receive TX irq.  This way, we'll never enable
2370      * UART_BUG_TXEN.
2371      */
2372     if (up->port.quirks & UPQ_NO_TXEN_TEST)
2373         goto dont_test_tx_en;
2374 
2375     /*
2376      * Do a quick test to see if we receive an interrupt when we enable
2377      * the TX irq.
2378      */
2379     serial_port_out(port, UART_IER, UART_IER_THRI);
2380     lsr = serial_port_in(port, UART_LSR);
2381     iir = serial_port_in(port, UART_IIR);
2382     serial_port_out(port, UART_IER, 0);
2383 
2384     if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2385         if (!(up->bugs & UART_BUG_TXEN)) {
2386             up->bugs |= UART_BUG_TXEN;
2387             dev_dbg(port->dev, "enabling bad tx status workarounds\n");
2388         }
2389     } else {
2390         up->bugs &= ~UART_BUG_TXEN;
2391     }
2392 
2393 dont_test_tx_en:
2394     spin_unlock_irqrestore(&port->lock, flags);
2395 
2396     /*
2397      * Clear the interrupt registers again for luck, and clear the
2398      * saved flags to avoid getting false values from polling
2399      * routines or the previous session.
2400      */
2401     serial_port_in(port, UART_LSR);
2402     serial_port_in(port, UART_RX);
2403     serial_port_in(port, UART_IIR);
2404     serial_port_in(port, UART_MSR);
2405     up->lsr_saved_flags = 0;
2406     up->msr_saved_flags = 0;
2407 
2408     /*
2409      * Request DMA channels for both RX and TX.
2410      */
2411     if (up->dma) {
2412         const char *msg = NULL;
2413 
2414         if (uart_console(port))
2415             msg = "forbid DMA for kernel console";
2416         else if (serial8250_request_dma(up))
2417             msg = "failed to request DMA";
2418         if (msg) {
2419             dev_warn_ratelimited(port->dev, "%s\n", msg);
2420             up->dma = NULL;
2421         }
2422     }
2423 
2424     /*
2425      * Set the IER shadow for rx interrupts but defer actual interrupt
2426      * enable until after the FIFOs are enabled; otherwise, an already-
2427      * active sender can swamp the interrupt handler with "too much work".
2428      */
2429     up->ier = UART_IER_RLSI | UART_IER_RDI;
2430 
2431     if (port->flags & UPF_FOURPORT) {
2432         unsigned int icp;
2433         /*
2434          * Enable interrupts on the AST Fourport board
2435          */
2436         icp = (port->iobase & 0xfe0) | 0x01f;
2437         outb_p(0x80, icp);
2438         inb_p(icp);
2439     }
2440     retval = 0;
2441 out:
2442     serial8250_rpm_put(up);
2443     return retval;
2444 }
2445 EXPORT_SYMBOL_GPL(serial8250_do_startup);
2446 
2447 static int serial8250_startup(struct uart_port *port)
2448 {
2449     if (port->startup)
2450         return port->startup(port);
2451     return serial8250_do_startup(port);
2452 }
2453 
2454 void serial8250_do_shutdown(struct uart_port *port)
2455 {
2456     struct uart_8250_port *up = up_to_u8250p(port);
2457     unsigned long flags;
2458 
2459     serial8250_rpm_get(up);
2460     /*
2461      * Disable interrupts from this port
2462      */
2463     spin_lock_irqsave(&port->lock, flags);
2464     up->ier = 0;
2465     serial_port_out(port, UART_IER, 0);
2466     spin_unlock_irqrestore(&port->lock, flags);
2467 
2468     synchronize_irq(port->irq);
2469 
2470     if (up->dma)
2471         serial8250_release_dma(up);
2472 
2473     spin_lock_irqsave(&port->lock, flags);
2474     if (port->flags & UPF_FOURPORT) {
2475         /* reset interrupts on the AST Fourport board */
2476         inb((port->iobase & 0xfe0) | 0x1f);
2477         port->mctrl |= TIOCM_OUT1;
2478     } else
2479         port->mctrl &= ~TIOCM_OUT2;
2480 
2481     serial8250_set_mctrl(port, port->mctrl);
2482     spin_unlock_irqrestore(&port->lock, flags);
2483 
2484     /*
2485      * Disable break condition and FIFOs
2486      */
2487     serial_port_out(port, UART_LCR,
2488             serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
2489     serial8250_clear_fifos(up);
2490 
2491 #ifdef CONFIG_SERIAL_8250_RSA
2492     /*
2493      * Reset the RSA board back to 115kbps compat mode.
2494      */
2495     disable_rsa(up);
2496 #endif
2497 
2498     /*
2499      * Read data port to reset things, and then unlink from
2500      * the IRQ chain.
2501      */
2502     serial_port_in(port, UART_RX);
2503     serial8250_rpm_put(up);
2504 
2505     up->ops->release_irq(up);
2506 }
2507 EXPORT_SYMBOL_GPL(serial8250_do_shutdown);
2508 
2509 static void serial8250_shutdown(struct uart_port *port)
2510 {
2511     if (port->shutdown)
2512         port->shutdown(port);
2513     else
2514         serial8250_do_shutdown(port);
2515 }
2516 
2517 /* Nuvoton NPCM UARTs have a custom divisor calculation */
2518 static unsigned int npcm_get_divisor(struct uart_8250_port *up,
2519         unsigned int baud)
2520 {
2521     struct uart_port *port = &up->port;
2522 
2523     return DIV_ROUND_CLOSEST(port->uartclk, 16 * baud + 2) - 2;
2524 }
2525 
2526 static unsigned int serial8250_do_get_divisor(struct uart_port *port,
2527                           unsigned int baud,
2528                           unsigned int *frac)
2529 {
2530     upf_t magic_multiplier = port->flags & UPF_MAGIC_MULTIPLIER;
2531     struct uart_8250_port *up = up_to_u8250p(port);
2532     unsigned int quot;
2533 
2534     /*
2535      * Handle magic divisors for baud rates above baud_base on SMSC
2536      * Super I/O chips.  We clamp custom rates from clk/6 and clk/12
2537      * up to clk/4 (0x8001) and clk/8 (0x8002) respectively.  These
2538      * magic divisors actually reprogram the baud rate generator's
2539      * reference clock derived from chips's 14.318MHz clock input.
2540      *
2541      * Documentation claims that with these magic divisors the base
2542      * frequencies of 7.3728MHz and 3.6864MHz are used respectively
2543      * for the extra baud rates of 460800bps and 230400bps rather
2544      * than the usual base frequency of 1.8462MHz.  However empirical
2545      * evidence contradicts that.
2546      *
2547      * Instead bit 7 of the DLM register (bit 15 of the divisor) is
2548      * effectively used as a clock prescaler selection bit for the
2549      * base frequency of 7.3728MHz, always used.  If set to 0, then
2550      * the base frequency is divided by 4 for use by the Baud Rate
2551      * Generator, for the usual arrangement where the value of 1 of
2552      * the divisor produces the baud rate of 115200bps.  Conversely,
2553      * if set to 1 and high-speed operation has been enabled with the
2554      * Serial Port Mode Register in the Device Configuration Space,
2555      * then the base frequency is supplied directly to the Baud Rate
2556      * Generator, so for the divisor values of 0x8001, 0x8002, 0x8003,
2557      * 0x8004, etc. the respective baud rates produced are 460800bps,
2558      * 230400bps, 153600bps, 115200bps, etc.
2559      *
2560      * In all cases only low 15 bits of the divisor are used to divide
2561      * the baud base and therefore 32767 is the maximum divisor value
2562      * possible, even though documentation says that the programmable
2563      * Baud Rate Generator is capable of dividing the internal PLL
2564      * clock by any divisor from 1 to 65535.
2565      */
2566     if (magic_multiplier && baud >= port->uartclk / 6)
2567         quot = 0x8001;
2568     else if (magic_multiplier && baud >= port->uartclk / 12)
2569         quot = 0x8002;
2570     else if (up->port.type == PORT_NPCM)
2571         quot = npcm_get_divisor(up, baud);
2572     else
2573         quot = uart_get_divisor(port, baud);
2574 
2575     /*
2576      * Oxford Semi 952 rev B workaround
2577      */
2578     if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2579         quot++;
2580 
2581     return quot;
2582 }
2583 
2584 static unsigned int serial8250_get_divisor(struct uart_port *port,
2585                        unsigned int baud,
2586                        unsigned int *frac)
2587 {
2588     if (port->get_divisor)
2589         return port->get_divisor(port, baud, frac);
2590 
2591     return serial8250_do_get_divisor(port, baud, frac);
2592 }
2593 
2594 static unsigned char serial8250_compute_lcr(struct uart_8250_port *up,
2595                         tcflag_t c_cflag)
2596 {
2597     unsigned char cval;
2598 
2599     cval = UART_LCR_WLEN(tty_get_char_size(c_cflag));
2600 
2601     if (c_cflag & CSTOPB)
2602         cval |= UART_LCR_STOP;
2603     if (c_cflag & PARENB) {
2604         cval |= UART_LCR_PARITY;
2605         if (up->bugs & UART_BUG_PARITY)
2606             up->fifo_bug = true;
2607     }
2608     if (!(c_cflag & PARODD))
2609         cval |= UART_LCR_EPAR;
2610     if (c_cflag & CMSPAR)
2611         cval |= UART_LCR_SPAR;
2612 
2613     return cval;
2614 }
2615 
2616 void serial8250_do_set_divisor(struct uart_port *port, unsigned int baud,
2617                    unsigned int quot, unsigned int quot_frac)
2618 {
2619     struct uart_8250_port *up = up_to_u8250p(port);
2620 
2621     /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2622     if (is_omap1510_8250(up)) {
2623         if (baud == 115200) {
2624             quot = 1;
2625             serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
2626         } else
2627             serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
2628     }
2629 
2630     /*
2631      * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
2632      * otherwise just set DLAB
2633      */
2634     if (up->capabilities & UART_NATSEMI)
2635         serial_port_out(port, UART_LCR, 0xe0);
2636     else
2637         serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
2638 
2639     serial_dl_write(up, quot);
2640 }
2641 EXPORT_SYMBOL_GPL(serial8250_do_set_divisor);
2642 
2643 static void serial8250_set_divisor(struct uart_port *port, unsigned int baud,
2644                    unsigned int quot, unsigned int quot_frac)
2645 {
2646     if (port->set_divisor)
2647         port->set_divisor(port, baud, quot, quot_frac);
2648     else
2649         serial8250_do_set_divisor(port, baud, quot, quot_frac);
2650 }
2651 
2652 static unsigned int serial8250_get_baud_rate(struct uart_port *port,
2653                          struct ktermios *termios,
2654                          struct ktermios *old)
2655 {
2656     unsigned int tolerance = port->uartclk / 100;
2657     unsigned int min;
2658     unsigned int max;
2659 
2660     /*
2661      * Handle magic divisors for baud rates above baud_base on SMSC
2662      * Super I/O chips.  Enable custom rates of clk/4 and clk/8, but
2663      * disable divisor values beyond 32767, which are unavailable.
2664      */
2665     if (port->flags & UPF_MAGIC_MULTIPLIER) {
2666         min = port->uartclk / 16 / UART_DIV_MAX >> 1;
2667         max = (port->uartclk + tolerance) / 4;
2668     } else {
2669         min = port->uartclk / 16 / UART_DIV_MAX;
2670         max = (port->uartclk + tolerance) / 16;
2671     }
2672 
2673     /*
2674      * Ask the core to calculate the divisor for us.
2675      * Allow 1% tolerance at the upper limit so uart clks marginally
2676      * slower than nominal still match standard baud rates without
2677      * causing transmission errors.
2678      */
2679     return uart_get_baud_rate(port, termios, old, min, max);
2680 }
2681 
2682 /*
2683  * Note in order to avoid the tty port mutex deadlock don't use the next method
2684  * within the uart port callbacks. Primarily it's supposed to be utilized to
2685  * handle a sudden reference clock rate change.
2686  */
2687 void serial8250_update_uartclk(struct uart_port *port, unsigned int uartclk)
2688 {
2689     struct uart_8250_port *up = up_to_u8250p(port);
2690     struct tty_port *tport = &port->state->port;
2691     unsigned int baud, quot, frac = 0;
2692     struct ktermios *termios;
2693     struct tty_struct *tty;
2694     unsigned long flags;
2695 
2696     tty = tty_port_tty_get(tport);
2697     if (!tty) {
2698         mutex_lock(&tport->mutex);
2699         port->uartclk = uartclk;
2700         mutex_unlock(&tport->mutex);
2701         return;
2702     }
2703 
2704     down_write(&tty->termios_rwsem);
2705     mutex_lock(&tport->mutex);
2706 
2707     if (port->uartclk == uartclk)
2708         goto out_unlock;
2709 
2710     port->uartclk = uartclk;
2711 
2712     if (!tty_port_initialized(tport))
2713         goto out_unlock;
2714 
2715     termios = &tty->termios;
2716 
2717     baud = serial8250_get_baud_rate(port, termios, NULL);
2718     quot = serial8250_get_divisor(port, baud, &frac);
2719 
2720     serial8250_rpm_get(up);
2721     spin_lock_irqsave(&port->lock, flags);
2722 
2723     uart_update_timeout(port, termios->c_cflag, baud);
2724 
2725     serial8250_set_divisor(port, baud, quot, frac);
2726     serial_port_out(port, UART_LCR, up->lcr);
2727 
2728     spin_unlock_irqrestore(&port->lock, flags);
2729     serial8250_rpm_put(up);
2730 
2731 out_unlock:
2732     mutex_unlock(&tport->mutex);
2733     up_write(&tty->termios_rwsem);
2734     tty_kref_put(tty);
2735 }
2736 EXPORT_SYMBOL_GPL(serial8250_update_uartclk);
2737 
2738 void
2739 serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2740               struct ktermios *old)
2741 {
2742     struct uart_8250_port *up = up_to_u8250p(port);
2743     unsigned char cval;
2744     unsigned long flags;
2745     unsigned int baud, quot, frac = 0;
2746 
2747     if (up->capabilities & UART_CAP_MINI) {
2748         termios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CMSPAR);
2749         if ((termios->c_cflag & CSIZE) == CS5 ||
2750             (termios->c_cflag & CSIZE) == CS6)
2751             termios->c_cflag = (termios->c_cflag & ~CSIZE) | CS7;
2752     }
2753     cval = serial8250_compute_lcr(up, termios->c_cflag);
2754 
2755     baud = serial8250_get_baud_rate(port, termios, old);
2756     quot = serial8250_get_divisor(port, baud, &frac);
2757 
2758     /*
2759      * Ok, we're now changing the port state.  Do it with
2760      * interrupts disabled.
2761      */
2762     serial8250_rpm_get(up);
2763     spin_lock_irqsave(&port->lock, flags);
2764 
2765     up->lcr = cval;                 /* Save computed LCR */
2766 
2767     if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
2768         /* NOTE: If fifo_bug is not set, a user can set RX_trigger. */
2769         if ((baud < 2400 && !up->dma) || up->fifo_bug) {
2770             up->fcr &= ~UART_FCR_TRIGGER_MASK;
2771             up->fcr |= UART_FCR_TRIGGER_1;
2772         }
2773     }
2774 
2775     /*
2776      * MCR-based auto flow control.  When AFE is enabled, RTS will be
2777      * deasserted when the receive FIFO contains more characters than
2778      * the trigger, or the MCR RTS bit is cleared.
2779      */
2780     if (up->capabilities & UART_CAP_AFE) {
2781         up->mcr &= ~UART_MCR_AFE;
2782         if (termios->c_cflag & CRTSCTS)
2783             up->mcr |= UART_MCR_AFE;
2784     }
2785 
2786     /*
2787      * Update the per-port timeout.
2788      */
2789     uart_update_timeout(port, termios->c_cflag, baud);
2790 
2791     port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2792     if (termios->c_iflag & INPCK)
2793         port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2794     if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2795         port->read_status_mask |= UART_LSR_BI;
2796 
2797     /*
2798      * Characters to ignore
2799      */
2800     port->ignore_status_mask = 0;
2801     if (termios->c_iflag & IGNPAR)
2802         port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2803     if (termios->c_iflag & IGNBRK) {
2804         port->ignore_status_mask |= UART_LSR_BI;
2805         /*
2806          * If we're ignoring parity and break indicators,
2807          * ignore overruns too (for real raw support).
2808          */
2809         if (termios->c_iflag & IGNPAR)
2810             port->ignore_status_mask |= UART_LSR_OE;
2811     }
2812 
2813     /*
2814      * ignore all characters if CREAD is not set
2815      */
2816     if ((termios->c_cflag & CREAD) == 0)
2817         port->ignore_status_mask |= UART_LSR_DR;
2818 
2819     /*
2820      * CTS flow control flag and modem status interrupts
2821      */
2822     up->ier &= ~UART_IER_MSI;
2823     if (!(up->bugs & UART_BUG_NOMSR) &&
2824             UART_ENABLE_MS(&up->port, termios->c_cflag))
2825         up->ier |= UART_IER_MSI;
2826     if (up->capabilities & UART_CAP_UUE)
2827         up->ier |= UART_IER_UUE;
2828     if (up->capabilities & UART_CAP_RTOIE)
2829         up->ier |= UART_IER_RTOIE;
2830 
2831     serial_port_out(port, UART_IER, up->ier);
2832 
2833     if (up->capabilities & UART_CAP_EFR) {
2834         unsigned char efr = 0;
2835         /*
2836          * TI16C752/Startech hardware flow control.  FIXME:
2837          * - TI16C752 requires control thresholds to be set.
2838          * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2839          */
2840         if (termios->c_cflag & CRTSCTS)
2841             efr |= UART_EFR_CTS;
2842 
2843         serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2844         if (port->flags & UPF_EXAR_EFR)
2845             serial_port_out(port, UART_XR_EFR, efr);
2846         else
2847             serial_port_out(port, UART_EFR, efr);
2848     }
2849 
2850     serial8250_set_divisor(port, baud, quot, frac);
2851 
2852     /*
2853      * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2854      * is written without DLAB set, this mode will be disabled.
2855      */
2856     if (port->type == PORT_16750)
2857         serial_port_out(port, UART_FCR, up->fcr);
2858 
2859     serial_port_out(port, UART_LCR, up->lcr);   /* reset DLAB */
2860     if (port->type != PORT_16750) {
2861         /* emulated UARTs (Lucent Venus 167x) need two steps */
2862         if (up->fcr & UART_FCR_ENABLE_FIFO)
2863             serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
2864         serial_port_out(port, UART_FCR, up->fcr);   /* set fcr */
2865     }
2866     serial8250_set_mctrl(port, port->mctrl);
2867     spin_unlock_irqrestore(&port->lock, flags);
2868     serial8250_rpm_put(up);
2869 
2870     /* Don't rewrite B0 */
2871     if (tty_termios_baud_rate(termios))
2872         tty_termios_encode_baud_rate(termios, baud, baud);
2873 }
2874 EXPORT_SYMBOL(serial8250_do_set_termios);
2875 
2876 static void
2877 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2878                struct ktermios *old)
2879 {
2880     if (port->set_termios)
2881         port->set_termios(port, termios, old);
2882     else
2883         serial8250_do_set_termios(port, termios, old);
2884 }
2885 
2886 void serial8250_do_set_ldisc(struct uart_port *port, struct ktermios *termios)
2887 {
2888     if (termios->c_line == N_PPS) {
2889         port->flags |= UPF_HARDPPS_CD;
2890         spin_lock_irq(&port->lock);
2891         serial8250_enable_ms(port);
2892         spin_unlock_irq(&port->lock);
2893     } else {
2894         port->flags &= ~UPF_HARDPPS_CD;
2895         if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2896             spin_lock_irq(&port->lock);
2897             serial8250_disable_ms(port);
2898             spin_unlock_irq(&port->lock);
2899         }
2900     }
2901 }
2902 EXPORT_SYMBOL_GPL(serial8250_do_set_ldisc);
2903 
2904 static void
2905 serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios)
2906 {
2907     if (port->set_ldisc)
2908         port->set_ldisc(port, termios);
2909     else
2910         serial8250_do_set_ldisc(port, termios);
2911 }
2912 
2913 void serial8250_do_pm(struct uart_port *port, unsigned int state,
2914               unsigned int oldstate)
2915 {
2916     struct uart_8250_port *p = up_to_u8250p(port);
2917 
2918     serial8250_set_sleep(p, state != 0);
2919 }
2920 EXPORT_SYMBOL(serial8250_do_pm);
2921 
2922 static void
2923 serial8250_pm(struct uart_port *port, unsigned int state,
2924           unsigned int oldstate)
2925 {
2926     if (port->pm)
2927         port->pm(port, state, oldstate);
2928     else
2929         serial8250_do_pm(port, state, oldstate);
2930 }
2931 
2932 static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2933 {
2934     if (pt->port.mapsize)
2935         return pt->port.mapsize;
2936     if (pt->port.iotype == UPIO_AU) {
2937         if (pt->port.type == PORT_RT2880)
2938             return 0x100;
2939         return 0x1000;
2940     }
2941     if (is_omap1_8250(pt))
2942         return 0x16 << pt->port.regshift;
2943 
2944     return 8 << pt->port.regshift;
2945 }
2946 
2947 /*
2948  * Resource handling.
2949  */
2950 static int serial8250_request_std_resource(struct uart_8250_port *up)
2951 {
2952     unsigned int size = serial8250_port_size(up);
2953     struct uart_port *port = &up->port;
2954     int ret = 0;
2955 
2956     switch (port->iotype) {
2957     case UPIO_AU:
2958     case UPIO_TSI:
2959     case UPIO_MEM32:
2960     case UPIO_MEM32BE:
2961     case UPIO_MEM16:
2962     case UPIO_MEM:
2963         if (!port->mapbase) {
2964             ret = -EINVAL;
2965             break;
2966         }
2967 
2968         if (!request_mem_region(port->mapbase, size, "serial")) {
2969             ret = -EBUSY;
2970             break;
2971         }
2972 
2973         if (port->flags & UPF_IOREMAP) {
2974             port->membase = ioremap(port->mapbase, size);
2975             if (!port->membase) {
2976                 release_mem_region(port->mapbase, size);
2977                 ret = -ENOMEM;
2978             }
2979         }
2980         break;
2981 
2982     case UPIO_HUB6:
2983     case UPIO_PORT:
2984         if (!request_region(port->iobase, size, "serial"))
2985             ret = -EBUSY;
2986         break;
2987     }
2988     return ret;
2989 }
2990 
2991 static void serial8250_release_std_resource(struct uart_8250_port *up)
2992 {
2993     unsigned int size = serial8250_port_size(up);
2994     struct uart_port *port = &up->port;
2995 
2996     switch (port->iotype) {
2997     case UPIO_AU:
2998     case UPIO_TSI:
2999     case UPIO_MEM32:
3000     case UPIO_MEM32BE:
3001     case UPIO_MEM16:
3002     case UPIO_MEM:
3003         if (!port->mapbase)
3004             break;
3005 
3006         if (port->flags & UPF_IOREMAP) {
3007             iounmap(port->membase);
3008             port->membase = NULL;
3009         }
3010 
3011         release_mem_region(port->mapbase, size);
3012         break;
3013 
3014     case UPIO_HUB6:
3015     case UPIO_PORT:
3016         release_region(port->iobase, size);
3017         break;
3018     }
3019 }
3020 
3021 static void serial8250_release_port(struct uart_port *port)
3022 {
3023     struct uart_8250_port *up = up_to_u8250p(port);
3024 
3025     serial8250_release_std_resource(up);
3026 }
3027 
3028 static int serial8250_request_port(struct uart_port *port)
3029 {
3030     struct uart_8250_port *up = up_to_u8250p(port);
3031 
3032     return serial8250_request_std_resource(up);
3033 }
3034 
3035 static int fcr_get_rxtrig_bytes(struct uart_8250_port *up)
3036 {
3037     const struct serial8250_config *conf_type = &uart_config[up->port.type];
3038     unsigned char bytes;
3039 
3040     bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)];
3041 
3042     return bytes ? bytes : -EOPNOTSUPP;
3043 }
3044 
3045 static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes)
3046 {
3047     const struct serial8250_config *conf_type = &uart_config[up->port.type];
3048     int i;
3049 
3050     if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)])
3051         return -EOPNOTSUPP;
3052 
3053     for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) {
3054         if (bytes < conf_type->rxtrig_bytes[i])
3055             /* Use the nearest lower value */
3056             return (--i) << UART_FCR_R_TRIG_SHIFT;
3057     }
3058 
3059     return UART_FCR_R_TRIG_11;
3060 }
3061 
3062 static int do_get_rxtrig(struct tty_port *port)
3063 {
3064     struct uart_state *state = container_of(port, struct uart_state, port);
3065     struct uart_port *uport = state->uart_port;
3066     struct uart_8250_port *up = up_to_u8250p(uport);
3067 
3068     if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
3069         return -EINVAL;
3070 
3071     return fcr_get_rxtrig_bytes(up);
3072 }
3073 
3074 static int do_serial8250_get_rxtrig(struct tty_port *port)
3075 {
3076     int rxtrig_bytes;
3077 
3078     mutex_lock(&port->mutex);
3079     rxtrig_bytes = do_get_rxtrig(port);
3080     mutex_unlock(&port->mutex);
3081 
3082     return rxtrig_bytes;
3083 }
3084 
3085 static ssize_t rx_trig_bytes_show(struct device *dev,
3086     struct device_attribute *attr, char *buf)
3087 {
3088     struct tty_port *port = dev_get_drvdata(dev);
3089     int rxtrig_bytes;
3090 
3091     rxtrig_bytes = do_serial8250_get_rxtrig(port);
3092     if (rxtrig_bytes < 0)
3093         return rxtrig_bytes;
3094 
3095     return sysfs_emit(buf, "%d\n", rxtrig_bytes);
3096 }
3097 
3098 static int do_set_rxtrig(struct tty_port *port, unsigned char bytes)
3099 {
3100     struct uart_state *state = container_of(port, struct uart_state, port);
3101     struct uart_port *uport = state->uart_port;
3102     struct uart_8250_port *up = up_to_u8250p(uport);
3103     int rxtrig;
3104 
3105     if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1 ||
3106         up->fifo_bug)
3107         return -EINVAL;
3108 
3109     rxtrig = bytes_to_fcr_rxtrig(up, bytes);
3110     if (rxtrig < 0)
3111         return rxtrig;
3112 
3113     serial8250_clear_fifos(up);
3114     up->fcr &= ~UART_FCR_TRIGGER_MASK;
3115     up->fcr |= (unsigned char)rxtrig;
3116     serial_out(up, UART_FCR, up->fcr);
3117     return 0;
3118 }
3119 
3120 static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes)
3121 {
3122     int ret;
3123 
3124     mutex_lock(&port->mutex);
3125     ret = do_set_rxtrig(port, bytes);
3126     mutex_unlock(&port->mutex);
3127 
3128     return ret;
3129 }
3130 
3131 static ssize_t rx_trig_bytes_store(struct device *dev,
3132     struct device_attribute *attr, const char *buf, size_t count)
3133 {
3134     struct tty_port *port = dev_get_drvdata(dev);
3135     unsigned char bytes;
3136     int ret;
3137 
3138     if (!count)
3139         return -EINVAL;
3140 
3141     ret = kstrtou8(buf, 10, &bytes);
3142     if (ret < 0)
3143         return ret;
3144 
3145     ret = do_serial8250_set_rxtrig(port, bytes);
3146     if (ret < 0)
3147         return ret;
3148 
3149     return count;
3150 }
3151 
3152 static DEVICE_ATTR_RW(rx_trig_bytes);
3153 
3154 static struct attribute *serial8250_dev_attrs[] = {
3155     &dev_attr_rx_trig_bytes.attr,
3156     NULL
3157 };
3158 
3159 static struct attribute_group serial8250_dev_attr_group = {
3160     .attrs = serial8250_dev_attrs,
3161 };
3162 
3163 static void register_dev_spec_attr_grp(struct uart_8250_port *up)
3164 {
3165     const struct serial8250_config *conf_type = &uart_config[up->port.type];
3166 
3167     if (conf_type->rxtrig_bytes[0])
3168         up->port.attr_group = &serial8250_dev_attr_group;
3169 }
3170 
3171 static void serial8250_config_port(struct uart_port *port, int flags)
3172 {
3173     struct uart_8250_port *up = up_to_u8250p(port);
3174     int ret;
3175 
3176     /*
3177      * Find the region that we can probe for.  This in turn
3178      * tells us whether we can probe for the type of port.
3179      */
3180     ret = serial8250_request_std_resource(up);
3181     if (ret < 0)
3182         return;
3183 
3184     if (port->iotype != up->cur_iotype)
3185         set_io_from_upio(port);
3186 
3187     if (flags & UART_CONFIG_TYPE)
3188         autoconfig(up);
3189 
3190     if (port->rs485.flags & SER_RS485_ENABLED)
3191         uart_rs485_config(port);
3192 
3193     /* if access method is AU, it is a 16550 with a quirk */
3194     if (port->type == PORT_16550A && port->iotype == UPIO_AU)
3195         up->bugs |= UART_BUG_NOMSR;
3196 
3197     /* HW bugs may trigger IRQ while IIR == NO_INT */
3198     if (port->type == PORT_TEGRA)
3199         up->bugs |= UART_BUG_NOMSR;
3200 
3201     if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
3202         autoconfig_irq(up);
3203 
3204     if (port->type == PORT_UNKNOWN)
3205         serial8250_release_std_resource(up);
3206 
3207     register_dev_spec_attr_grp(up);
3208     up->fcr = uart_config[up->port.type].fcr;
3209 }
3210 
3211 static int
3212 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
3213 {
3214     if (ser->irq >= nr_irqs || ser->irq < 0 ||
3215         ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
3216         ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
3217         ser->type == PORT_STARTECH)
3218         return -EINVAL;
3219     return 0;
3220 }
3221 
3222 static const char *serial8250_type(struct uart_port *port)
3223 {
3224     int type = port->type;
3225 
3226     if (type >= ARRAY_SIZE(uart_config))
3227         type = 0;
3228     return uart_config[type].name;
3229 }
3230 
3231 static const struct uart_ops serial8250_pops = {
3232     .tx_empty   = serial8250_tx_empty,
3233     .set_mctrl  = serial8250_set_mctrl,
3234     .get_mctrl  = serial8250_get_mctrl,
3235     .stop_tx    = serial8250_stop_tx,
3236     .start_tx   = serial8250_start_tx,
3237     .throttle   = serial8250_throttle,
3238     .unthrottle = serial8250_unthrottle,
3239     .stop_rx    = serial8250_stop_rx,
3240     .enable_ms  = serial8250_enable_ms,
3241     .break_ctl  = serial8250_break_ctl,
3242     .startup    = serial8250_startup,
3243     .shutdown   = serial8250_shutdown,
3244     .set_termios    = serial8250_set_termios,
3245     .set_ldisc  = serial8250_set_ldisc,
3246     .pm     = serial8250_pm,
3247     .type       = serial8250_type,
3248     .release_port   = serial8250_release_port,
3249     .request_port   = serial8250_request_port,
3250     .config_port    = serial8250_config_port,
3251     .verify_port    = serial8250_verify_port,
3252 #ifdef CONFIG_CONSOLE_POLL
3253     .poll_get_char = serial8250_get_poll_char,
3254     .poll_put_char = serial8250_put_poll_char,
3255 #endif
3256 };
3257 
3258 void serial8250_init_port(struct uart_8250_port *up)
3259 {
3260     struct uart_port *port = &up->port;
3261 
3262     spin_lock_init(&port->lock);
3263     port->ops = &serial8250_pops;
3264     port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
3265 
3266     up->cur_iotype = 0xFF;
3267 }
3268 EXPORT_SYMBOL_GPL(serial8250_init_port);
3269 
3270 void serial8250_set_defaults(struct uart_8250_port *up)
3271 {
3272     struct uart_port *port = &up->port;
3273 
3274     if (up->port.flags & UPF_FIXED_TYPE) {
3275         unsigned int type = up->port.type;
3276 
3277         if (!up->port.fifosize)
3278             up->port.fifosize = uart_config[type].fifo_size;
3279         if (!up->tx_loadsz)
3280             up->tx_loadsz = uart_config[type].tx_loadsz;
3281         if (!up->capabilities)
3282             up->capabilities = uart_config[type].flags;
3283     }
3284 
3285     set_io_from_upio(port);
3286 
3287     /* default dma handlers */
3288     if (up->dma) {
3289         if (!up->dma->tx_dma)
3290             up->dma->tx_dma = serial8250_tx_dma;
3291         if (!up->dma->rx_dma)
3292             up->dma->rx_dma = serial8250_rx_dma;
3293     }
3294 }
3295 EXPORT_SYMBOL_GPL(serial8250_set_defaults);
3296 
3297 #ifdef CONFIG_SERIAL_8250_CONSOLE
3298 
3299 static void serial8250_console_putchar(struct uart_port *port, unsigned char ch)
3300 {
3301     struct uart_8250_port *up = up_to_u8250p(port);
3302 
3303     wait_for_xmitr(up, UART_LSR_THRE);
3304     serial_port_out(port, UART_TX, ch);
3305 }
3306 
3307 /*
3308  *  Restore serial console when h/w power-off detected
3309  */
3310 static void serial8250_console_restore(struct uart_8250_port *up)
3311 {
3312     struct uart_port *port = &up->port;
3313     struct ktermios termios;
3314     unsigned int baud, quot, frac = 0;
3315 
3316     termios.c_cflag = port->cons->cflag;
3317     if (port->state->port.tty && termios.c_cflag == 0)
3318         termios.c_cflag = port->state->port.tty->termios.c_cflag;
3319 
3320     baud = serial8250_get_baud_rate(port, &termios, NULL);
3321     quot = serial8250_get_divisor(port, baud, &frac);
3322 
3323     serial8250_set_divisor(port, baud, quot, frac);
3324     serial_port_out(port, UART_LCR, up->lcr);
3325     serial8250_out_MCR(up, up->mcr | UART_MCR_DTR | UART_MCR_RTS);
3326 }
3327 
3328 /*
3329  * Print a string to the serial port using the device FIFO
3330  *
3331  * It sends fifosize bytes and then waits for the fifo
3332  * to get empty.
3333  */
3334 static void serial8250_console_fifo_write(struct uart_8250_port *up,
3335                       const char *s, unsigned int count)
3336 {
3337     int i;
3338     const char *end = s + count;
3339     unsigned int fifosize = up->tx_loadsz;
3340     bool cr_sent = false;
3341 
3342     while (s != end) {
3343         wait_for_lsr(up, UART_LSR_THRE);
3344 
3345         for (i = 0; i < fifosize && s != end; ++i) {
3346             if (*s == '\n' && !cr_sent) {
3347                 serial_out(up, UART_TX, '\r');
3348                 cr_sent = true;
3349             } else {
3350                 serial_out(up, UART_TX, *s++);
3351                 cr_sent = false;
3352             }
3353         }
3354     }
3355 }
3356 
3357 /*
3358  *  Print a string to the serial port trying not to disturb
3359  *  any possible real use of the port...
3360  *
3361  *  The console_lock must be held when we get here.
3362  *
3363  *  Doing runtime PM is really a bad idea for the kernel console.
3364  *  Thus, we assume the function is called when device is powered up.
3365  */
3366 void serial8250_console_write(struct uart_8250_port *up, const char *s,
3367                   unsigned int count)
3368 {
3369     struct uart_8250_em485 *em485 = up->em485;
3370     struct uart_port *port = &up->port;
3371     unsigned long flags;
3372     unsigned int ier, use_fifo;
3373     int locked = 1;
3374 
3375     touch_nmi_watchdog();
3376 
3377     if (oops_in_progress)
3378         locked = spin_trylock_irqsave(&port->lock, flags);
3379     else
3380         spin_lock_irqsave(&port->lock, flags);
3381 
3382     /*
3383      *  First save the IER then disable the interrupts
3384      */
3385     ier = serial_port_in(port, UART_IER);
3386 
3387     if (up->capabilities & UART_CAP_UUE)
3388         serial_port_out(port, UART_IER, UART_IER_UUE);
3389     else
3390         serial_port_out(port, UART_IER, 0);
3391 
3392     /* check scratch reg to see if port powered off during system sleep */
3393     if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) {
3394         serial8250_console_restore(up);
3395         up->canary = 0;
3396     }
3397 
3398     if (em485) {
3399         if (em485->tx_stopped)
3400             up->rs485_start_tx(up);
3401         mdelay(port->rs485.delay_rts_before_send);
3402     }
3403 
3404     use_fifo = (up->capabilities & UART_CAP_FIFO) &&
3405         /*
3406          * BCM283x requires to check the fifo
3407          * after each byte.
3408          */
3409         !(up->capabilities & UART_CAP_MINI) &&
3410         /*
3411          * tx_loadsz contains the transmit fifo size
3412          */
3413         up->tx_loadsz > 1 &&
3414         (up->fcr & UART_FCR_ENABLE_FIFO) &&
3415         port->state &&
3416         test_bit(TTY_PORT_INITIALIZED, &port->state->port.iflags) &&
3417         /*
3418          * After we put a data in the fifo, the controller will send
3419          * it regardless of the CTS state. Therefore, only use fifo
3420          * if we don't use control flow.
3421          */
3422         !(up->port.flags & UPF_CONS_FLOW);
3423 
3424     if (likely(use_fifo))
3425         serial8250_console_fifo_write(up, s, count);
3426     else
3427         uart_console_write(port, s, count, serial8250_console_putchar);
3428 
3429     /*
3430      *  Finally, wait for transmitter to become empty
3431      *  and restore the IER
3432      */
3433     wait_for_xmitr(up, UART_LSR_BOTH_EMPTY);
3434 
3435     if (em485) {
3436         mdelay(port->rs485.delay_rts_after_send);
3437         if (em485->tx_stopped)
3438             up->rs485_stop_tx(up);
3439     }
3440 
3441     serial_port_out(port, UART_IER, ier);
3442 
3443     /*
3444      *  The receive handling will happen properly because the
3445      *  receive ready bit will still be set; it is not cleared
3446      *  on read.  However, modem control will not, we must
3447      *  call it if we have saved something in the saved flags
3448      *  while processing with interrupts off.
3449      */
3450     if (up->msr_saved_flags)
3451         serial8250_modem_status(up);
3452 
3453     if (locked)
3454         spin_unlock_irqrestore(&port->lock, flags);
3455 }
3456 
3457 static unsigned int probe_baud(struct uart_port *port)
3458 {
3459     unsigned char lcr, dll, dlm;
3460     unsigned int quot;
3461 
3462     lcr = serial_port_in(port, UART_LCR);
3463     serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB);
3464     dll = serial_port_in(port, UART_DLL);
3465     dlm = serial_port_in(port, UART_DLM);
3466     serial_port_out(port, UART_LCR, lcr);
3467 
3468     quot = (dlm << 8) | dll;
3469     return (port->uartclk / 16) / quot;
3470 }
3471 
3472 int serial8250_console_setup(struct uart_port *port, char *options, bool probe)
3473 {
3474     int baud = 9600;
3475     int bits = 8;
3476     int parity = 'n';
3477     int flow = 'n';
3478     int ret;
3479 
3480     if (!port->iobase && !port->membase)
3481         return -ENODEV;
3482 
3483     if (options)
3484         uart_parse_options(options, &baud, &parity, &bits, &flow);
3485     else if (probe)
3486         baud = probe_baud(port);
3487 
3488     ret = uart_set_options(port, port->cons, baud, parity, bits, flow);
3489     if (ret)
3490         return ret;
3491 
3492     if (port->dev)
3493         pm_runtime_get_sync(port->dev);
3494 
3495     return 0;
3496 }
3497 
3498 int serial8250_console_exit(struct uart_port *port)
3499 {
3500     if (port->dev)
3501         pm_runtime_put_sync(port->dev);
3502 
3503     return 0;
3504 }
3505 
3506 #endif /* CONFIG_SERIAL_8250_CONSOLE */
3507 
3508 MODULE_LICENSE("GPL");