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