linux/drivers/tty/serial/xilinx_uartps.c
<<
>>
Prefs
   1/*
   2 * Cadence UART driver (found in Xilinx Zynq)
   3 *
   4 * 2011 - 2014 (C) Xilinx Inc.
   5 *
   6 * This program is free software; you can redistribute it
   7 * and/or modify it under the terms of the GNU General Public
   8 * License as published by the Free Software Foundation;
   9 * either version 2 of the License, or (at your option) any
  10 * later version.
  11 *
  12 * This driver has originally been pushed by Xilinx using a Zynq-branding. This
  13 * still shows in the naming of this file, the kconfig symbols and some symbols
  14 * in the code.
  15 */
  16
  17#if defined(CONFIG_SERIAL_XILINX_PS_UART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  18#define SUPPORT_SYSRQ
  19#endif
  20
  21#include <linux/platform_device.h>
  22#include <linux/serial.h>
  23#include <linux/console.h>
  24#include <linux/serial_core.h>
  25#include <linux/slab.h>
  26#include <linux/tty.h>
  27#include <linux/tty_flip.h>
  28#include <linux/clk.h>
  29#include <linux/irq.h>
  30#include <linux/io.h>
  31#include <linux/of.h>
  32#include <linux/module.h>
  33
  34#define CDNS_UART_TTY_NAME      "ttyPS"
  35#define CDNS_UART_NAME          "xuartps"
  36#define CDNS_UART_MAJOR         0       /* use dynamic node allocation */
  37#define CDNS_UART_MINOR         0       /* works best with devtmpfs */
  38#define CDNS_UART_NR_PORTS      2
  39#define CDNS_UART_FIFO_SIZE     64      /* FIFO size */
  40#define CDNS_UART_REGISTER_SPACE        0x1000
  41
  42#define cdns_uart_readl(offset)         ioread32(port->membase + offset)
  43#define cdns_uart_writel(val, offset)   iowrite32(val, port->membase + offset)
  44
  45/* Rx Trigger level */
  46static int rx_trigger_level = 56;
  47module_param(rx_trigger_level, uint, S_IRUGO);
  48MODULE_PARM_DESC(rx_trigger_level, "Rx trigger level, 1-63 bytes");
  49
  50/* Rx Timeout */
  51static int rx_timeout = 10;
  52module_param(rx_timeout, uint, S_IRUGO);
  53MODULE_PARM_DESC(rx_timeout, "Rx timeout, 1-255");
  54
  55/* Register offsets for the UART. */
  56#define CDNS_UART_CR_OFFSET             0x00  /* Control Register */
  57#define CDNS_UART_MR_OFFSET             0x04  /* Mode Register */
  58#define CDNS_UART_IER_OFFSET            0x08  /* Interrupt Enable */
  59#define CDNS_UART_IDR_OFFSET            0x0C  /* Interrupt Disable */
  60#define CDNS_UART_IMR_OFFSET            0x10  /* Interrupt Mask */
  61#define CDNS_UART_ISR_OFFSET            0x14  /* Interrupt Status */
  62#define CDNS_UART_BAUDGEN_OFFSET        0x18  /* Baud Rate Generator */
  63#define CDNS_UART_RXTOUT_OFFSET         0x1C  /* RX Timeout */
  64#define CDNS_UART_RXWM_OFFSET           0x20  /* RX FIFO Trigger Level */
  65#define CDNS_UART_MODEMCR_OFFSET        0x24  /* Modem Control */
  66#define CDNS_UART_MODEMSR_OFFSET        0x28  /* Modem Status */
  67#define CDNS_UART_SR_OFFSET             0x2C  /* Channel Status */
  68#define CDNS_UART_FIFO_OFFSET           0x30  /* FIFO */
  69#define CDNS_UART_BAUDDIV_OFFSET        0x34  /* Baud Rate Divider */
  70#define CDNS_UART_FLOWDEL_OFFSET        0x38  /* Flow Delay */
  71#define CDNS_UART_IRRX_PWIDTH_OFFSET    0x3C  /* IR Min Received Pulse Width */
  72#define CDNS_UART_IRTX_PWIDTH_OFFSET    0x40  /* IR Transmitted pulse Width */
  73#define CDNS_UART_TXWM_OFFSET           0x44  /* TX FIFO Trigger Level */
  74#define CDNS_UART_RXBS_OFFSET           0x48  /* RX FIFO byte status register */
  75
  76/* Control Register Bit Definitions */
  77#define CDNS_UART_CR_STOPBRK    0x00000100  /* Stop TX break */
  78#define CDNS_UART_CR_STARTBRK   0x00000080  /* Set TX break */
  79#define CDNS_UART_CR_TX_DIS     0x00000020  /* TX disabled. */
  80#define CDNS_UART_CR_TX_EN      0x00000010  /* TX enabled */
  81#define CDNS_UART_CR_RX_DIS     0x00000008  /* RX disabled. */
  82#define CDNS_UART_CR_RX_EN      0x00000004  /* RX enabled */
  83#define CDNS_UART_CR_TXRST      0x00000002  /* TX logic reset */
  84#define CDNS_UART_CR_RXRST      0x00000001  /* RX logic reset */
  85#define CDNS_UART_CR_RST_TO     0x00000040  /* Restart Timeout Counter */
  86
  87/* RX FIFO byte status Register it Definitions */
  88#define CDNS_UART_RXBS_PARITY    0x00000001 /* Parity error status */
  89#define CDNS_UART_RXBS_FRAMING   0x00000002 /* Framing error status */
  90#define CDNS_UART_RXBS_BRK       0x00000004 /* Overrun error status */
  91
  92/*
  93 * Mode Register:
  94 * The mode register (MR) defines the mode of transfer as well as the data
  95 * format. If this register is modified during transmission or reception,
  96 * data validity cannot be guaranteed.
  97 */
  98#define CDNS_UART_MR_CLKSEL             0x00000001  /* Pre-scalar selection */
  99#define CDNS_UART_MR_CHMODE_L_LOOP      0x00000200  /* Local loop back mode */
 100#define CDNS_UART_MR_CHMODE_NORM        0x00000000  /* Normal mode */
 101
 102#define CDNS_UART_MR_STOPMODE_2_BIT     0x00000080  /* 2 stop bits */
 103#define CDNS_UART_MR_STOPMODE_1_BIT     0x00000000  /* 1 stop bit */
 104
 105#define CDNS_UART_MR_PARITY_NONE        0x00000020  /* No parity mode */
 106#define CDNS_UART_MR_PARITY_MARK        0x00000018  /* Mark parity mode */
 107#define CDNS_UART_MR_PARITY_SPACE       0x00000010  /* Space parity mode */
 108#define CDNS_UART_MR_PARITY_ODD         0x00000008  /* Odd parity mode */
 109#define CDNS_UART_MR_PARITY_EVEN        0x00000000  /* Even parity mode */
 110
 111#define CDNS_UART_MR_CHARLEN_6_BIT      0x00000006  /* 6 bits data */
 112#define CDNS_UART_MR_CHARLEN_7_BIT      0x00000004  /* 7 bits data */
 113#define CDNS_UART_MR_CHARLEN_8_BIT      0x00000000  /* 8 bits data */
 114
 115/*
 116 * Interrupt Registers:
 117 * Interrupt control logic uses the interrupt enable register (IER) and the
 118 * interrupt disable register (IDR) to set the value of the bits in the
 119 * interrupt mask register (IMR). The IMR determines whether to pass an
 120 * interrupt to the interrupt status register (ISR).
 121 * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
 122 * interrupt. IMR and ISR are read only, and IER and IDR are write only.
 123 * Reading either IER or IDR returns 0x00.
 124 * All four registers have the same bit definitions.
 125 */
 126#define CDNS_UART_IXR_TOUT      0x00000100 /* RX Timeout error interrupt */
 127#define CDNS_UART_IXR_PARITY    0x00000080 /* Parity error interrupt */
 128#define CDNS_UART_IXR_FRAMING   0x00000040 /* Framing error interrupt */
 129#define CDNS_UART_IXR_OVERRUN   0x00000020 /* Overrun error interrupt */
 130#define CDNS_UART_IXR_TXFULL    0x00000010 /* TX FIFO Full interrupt */
 131#define CDNS_UART_IXR_TXEMPTY   0x00000008 /* TX FIFO empty interrupt */
 132#define CDNS_UART_ISR_RXEMPTY   0x00000002 /* RX FIFO empty interrupt */
 133#define CDNS_UART_IXR_RXTRIG    0x00000001 /* RX FIFO trigger interrupt */
 134#define CDNS_UART_IXR_RXFULL    0x00000004 /* RX FIFO full interrupt. */
 135#define CDNS_UART_IXR_RXEMPTY   0x00000002 /* RX FIFO empty interrupt. */
 136#define CDNS_UART_IXR_MASK      0x00003FFF /* Valid bit mask */
 137
 138/* Goes in read_status_mask for break detection as the HW doesn't do it*/
 139#define CDNS_UART_IXR_BRK       0x00002000
 140
 141#define CDNS_UART_RXBS_SUPPORT BIT(1)
 142/*
 143 * Modem Control register:
 144 * The read/write Modem Control register controls the interface with the modem
 145 * or data set, or a peripheral device emulating a modem.
 146 */
 147#define CDNS_UART_MODEMCR_FCM   0x00000020 /* Automatic flow control mode */
 148#define CDNS_UART_MODEMCR_RTS   0x00000002 /* Request to send output control */
 149#define CDNS_UART_MODEMCR_DTR   0x00000001 /* Data Terminal Ready */
 150
 151/*
 152 * Channel Status Register:
 153 * The channel status register (CSR) is provided to enable the control logic
 154 * to monitor the status of bits in the channel interrupt status register,
 155 * even if these are masked out by the interrupt mask register.
 156 */
 157#define CDNS_UART_SR_RXEMPTY    0x00000002 /* RX FIFO empty */
 158#define CDNS_UART_SR_TXEMPTY    0x00000008 /* TX FIFO empty */
 159#define CDNS_UART_SR_TXFULL     0x00000010 /* TX FIFO full */
 160#define CDNS_UART_SR_RXTRIG     0x00000001 /* Rx Trigger */
 161
 162/* baud dividers min/max values */
 163#define CDNS_UART_BDIV_MIN      4
 164#define CDNS_UART_BDIV_MAX      255
 165#define CDNS_UART_CD_MAX        65535
 166
 167/**
 168 * struct cdns_uart - device data
 169 * @port:               Pointer to the UART port
 170 * @uartclk:            Reference clock
 171 * @pclk:               APB clock
 172 * @baud:               Current baud rate
 173 * @clk_rate_change_nb: Notifier block for clock changes
 174 */
 175struct cdns_uart {
 176        struct uart_port        *port;
 177        struct clk              *uartclk;
 178        struct clk              *pclk;
 179        unsigned int            baud;
 180        struct notifier_block   clk_rate_change_nb;
 181        u32                     quirks;
 182};
 183struct cdns_platform_data {
 184        u32 quirks;
 185};
 186
 187#define to_cdns_uart(_nb) container_of(_nb, struct cdns_uart, \
 188                clk_rate_change_nb);
 189
 190/**
 191 * cdns_uart_handle_tx - Handle the bytes to be Txed.
 192 * @dev_id: Id of the UART port
 193 * Return: None
 194 */
 195static void cdns_uart_handle_tx(void *dev_id)
 196{
 197        struct uart_port *port = (struct uart_port *)dev_id;
 198        unsigned int numbytes;
 199
 200        if (uart_circ_empty(&port->state->xmit)) {
 201                cdns_uart_writel(CDNS_UART_IXR_TXEMPTY, CDNS_UART_IDR_OFFSET);
 202        } else {
 203                numbytes = port->fifosize;
 204                /* Break if no more data available in the UART buffer */
 205                while (numbytes--) {
 206                        if (uart_circ_empty(&port->state->xmit))
 207                                break;
 208                        /*
 209                         * Get the data from the UART circular buffer
 210                         * and write it to the cdns_uart's TX_FIFO
 211                         * register.
 212                         */
 213                        cdns_uart_writel(
 214                                port->state->xmit.buf[port->state->xmit.
 215                                tail], CDNS_UART_FIFO_OFFSET);
 216
 217                        port->icount.tx++;
 218
 219                        /*
 220                         * Adjust the tail of the UART buffer and wrap
 221                         * the buffer if it reaches limit.
 222                         */
 223                        port->state->xmit.tail =
 224                                (port->state->xmit.tail + 1) &
 225                                        (UART_XMIT_SIZE - 1);
 226                }
 227
 228                if (uart_circ_chars_pending(
 229                                &port->state->xmit) < WAKEUP_CHARS)
 230                        uart_write_wakeup(port);
 231        }
 232}
 233
 234/**
 235 * cdns_uart_handle_rx - Handle the received bytes along with Rx errors.
 236 * @dev_id: Id of the UART port
 237 * @isrstatus: The interrupt status register value as read
 238 * Return: None
 239 */
 240static void cdns_uart_handle_rx(void *dev_id, unsigned int isrstatus)
 241{
 242        struct uart_port *port = (struct uart_port *)dev_id;
 243        struct cdns_uart *cdns_uart = port->private_data;
 244        unsigned int data;
 245        unsigned int rxbs_status = 0;
 246        unsigned int status_mask;
 247        unsigned int framerrprocessed = 0;
 248        char status = TTY_NORMAL;
 249        bool is_rxbs_support;
 250
 251        is_rxbs_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
 252
 253        while ((cdns_uart_readl(CDNS_UART_SR_OFFSET) &
 254                CDNS_UART_SR_RXEMPTY) != CDNS_UART_SR_RXEMPTY) {
 255                if (is_rxbs_support)
 256                        rxbs_status = cdns_uart_readl(CDNS_UART_RXBS_OFFSET);
 257                data = cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
 258                port->icount.rx++;
 259                /*
 260                 * There is no hardware break detection in Zynq, so we interpret
 261                 * framing error with all-zeros data as a break sequence.
 262                 * Most of the time, there's another non-zero byte at the
 263                 * end of the sequence.
 264                 */
 265                if (!is_rxbs_support && (isrstatus & CDNS_UART_IXR_FRAMING)) {
 266                        if (!data) {
 267                                port->read_status_mask |= CDNS_UART_IXR_BRK;
 268                                framerrprocessed = 1;
 269                                continue;
 270                        }
 271                }
 272                if (is_rxbs_support && (rxbs_status & CDNS_UART_RXBS_BRK)) {
 273                        port->icount.brk++;
 274                        status = TTY_BREAK;
 275                        if (uart_handle_break(port))
 276                                continue;
 277                }
 278
 279                isrstatus &= port->read_status_mask;
 280                isrstatus &= ~port->ignore_status_mask;
 281                status_mask = port->read_status_mask;
 282                status_mask &= ~port->ignore_status_mask;
 283
 284                if (data &&
 285                    (port->read_status_mask & CDNS_UART_IXR_BRK)) {
 286                        port->read_status_mask &= ~CDNS_UART_IXR_BRK;
 287                        port->icount.brk++;
 288                        if (uart_handle_break(port))
 289                                continue;
 290                }
 291
 292#ifdef SUPPORT_SYSRQ
 293                /*
 294                 * uart_handle_sysrq_char() doesn't work if
 295                 * spinlocked, for some reason
 296                 */
 297                 if (port->sysrq) {
 298                        spin_unlock(&port->lock);
 299                        if (uart_handle_sysrq_char(port,
 300                                                (unsigned char)data)) {
 301                                spin_lock(&port->lock);
 302                                continue;
 303                        }
 304                        spin_lock(&port->lock);
 305                }
 306#endif
 307                if (is_rxbs_support) {
 308                        if ((rxbs_status & CDNS_UART_RXBS_PARITY)
 309                                && (status_mask & CDNS_UART_IXR_PARITY)) {
 310                                port->icount.parity++;
 311                                status = TTY_PARITY;
 312                        }
 313                        if ((rxbs_status & CDNS_UART_RXBS_FRAMING)
 314                                && (status_mask & CDNS_UART_IXR_PARITY)) {
 315                                port->icount.frame++;
 316                                status = TTY_FRAME;
 317                        }
 318                } else {
 319                        if (isrstatus & CDNS_UART_IXR_PARITY) {
 320                                port->icount.parity++;
 321                                status = TTY_PARITY;
 322                        }
 323                        if ((isrstatus & CDNS_UART_IXR_FRAMING) &&
 324                                                !framerrprocessed) {
 325                                port->icount.frame++;
 326                                status = TTY_FRAME;
 327                        }
 328                }
 329                if (isrstatus & CDNS_UART_IXR_OVERRUN) {
 330                        port->icount.overrun++;
 331                        tty_insert_flip_char(&port->state->port, 0,
 332                                             TTY_OVERRUN);
 333                }
 334                tty_insert_flip_char(&port->state->port, data, status);
 335                isrstatus = 0;
 336        }
 337        spin_unlock(&port->lock);
 338        tty_flip_buffer_push(&port->state->port);
 339        spin_lock(&port->lock);
 340}
 341
 342/**
 343 * cdns_uart_isr - Interrupt handler
 344 * @irq: Irq number
 345 * @dev_id: Id of the port
 346 *
 347 * Return: IRQHANDLED
 348 */
 349static irqreturn_t cdns_uart_isr(int irq, void *dev_id)
 350{
 351        struct uart_port *port = (struct uart_port *)dev_id;
 352        unsigned int isrstatus;
 353
 354        spin_lock(&port->lock);
 355
 356        /* Read the interrupt status register to determine which
 357         * interrupt(s) is/are active and clear them.
 358         */
 359        isrstatus = cdns_uart_readl(CDNS_UART_ISR_OFFSET);
 360        cdns_uart_writel(isrstatus, CDNS_UART_ISR_OFFSET);
 361
 362        if (isrstatus & CDNS_UART_IXR_TXEMPTY) {
 363                cdns_uart_handle_tx(dev_id);
 364                isrstatus &= ~CDNS_UART_IXR_TXEMPTY;
 365        }
 366        if (isrstatus & CDNS_UART_IXR_MASK)
 367                cdns_uart_handle_rx(dev_id, isrstatus);
 368
 369        spin_unlock(&port->lock);
 370        return IRQ_HANDLED;
 371}
 372
 373/**
 374 * cdns_uart_calc_baud_divs - Calculate baud rate divisors
 375 * @clk: UART module input clock
 376 * @baud: Desired baud rate
 377 * @rbdiv: BDIV value (return value)
 378 * @rcd: CD value (return value)
 379 * @div8: Value for clk_sel bit in mod (return value)
 380 * Return: baud rate, requested baud when possible, or actual baud when there
 381 *      was too much error, zero if no valid divisors are found.
 382 *
 383 * Formula to obtain baud rate is
 384 *      baud_tx/rx rate = clk/CD * (BDIV + 1)
 385 *      input_clk = (Uart User Defined Clock or Apb Clock)
 386 *              depends on UCLKEN in MR Reg
 387 *      clk = input_clk or input_clk/8;
 388 *              depends on CLKS in MR reg
 389 *      CD and BDIV depends on values in
 390 *                      baud rate generate register
 391 *                      baud rate clock divisor register
 392 */
 393static unsigned int cdns_uart_calc_baud_divs(unsigned int clk,
 394                unsigned int baud, u32 *rbdiv, u32 *rcd, int *div8)
 395{
 396        u32 cd, bdiv;
 397        unsigned int calc_baud;
 398        unsigned int bestbaud = 0;
 399        unsigned int bauderror;
 400        unsigned int besterror = ~0;
 401
 402        if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) {
 403                *div8 = 1;
 404                clk /= 8;
 405        } else {
 406                *div8 = 0;
 407        }
 408
 409        for (bdiv = CDNS_UART_BDIV_MIN; bdiv <= CDNS_UART_BDIV_MAX; bdiv++) {
 410                cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
 411                if (cd < 1 || cd > CDNS_UART_CD_MAX)
 412                        continue;
 413
 414                calc_baud = clk / (cd * (bdiv + 1));
 415
 416                if (baud > calc_baud)
 417                        bauderror = baud - calc_baud;
 418                else
 419                        bauderror = calc_baud - baud;
 420
 421                if (besterror > bauderror) {
 422                        *rbdiv = bdiv;
 423                        *rcd = cd;
 424                        bestbaud = calc_baud;
 425                        besterror = bauderror;
 426                }
 427        }
 428        /* use the values when percent error is acceptable */
 429        if (((besterror * 100) / baud) < 3)
 430                bestbaud = baud;
 431
 432        return bestbaud;
 433}
 434
 435/**
 436 * cdns_uart_set_baud_rate - Calculate and set the baud rate
 437 * @port: Handle to the uart port structure
 438 * @baud: Baud rate to set
 439 * Return: baud rate, requested baud when possible, or actual baud when there
 440 *         was too much error, zero if no valid divisors are found.
 441 */
 442static unsigned int cdns_uart_set_baud_rate(struct uart_port *port,
 443                unsigned int baud)
 444{
 445        unsigned int calc_baud;
 446        u32 cd = 0, bdiv = 0;
 447        u32 mreg;
 448        int div8;
 449        struct cdns_uart *cdns_uart = port->private_data;
 450
 451        calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
 452                        &div8);
 453
 454        /* Write new divisors to hardware */
 455        mreg = cdns_uart_readl(CDNS_UART_MR_OFFSET);
 456        if (div8)
 457                mreg |= CDNS_UART_MR_CLKSEL;
 458        else
 459                mreg &= ~CDNS_UART_MR_CLKSEL;
 460        cdns_uart_writel(mreg, CDNS_UART_MR_OFFSET);
 461        cdns_uart_writel(cd, CDNS_UART_BAUDGEN_OFFSET);
 462        cdns_uart_writel(bdiv, CDNS_UART_BAUDDIV_OFFSET);
 463        cdns_uart->baud = baud;
 464
 465        return calc_baud;
 466}
 467
 468#ifdef CONFIG_COMMON_CLK
 469/**
 470 * cdns_uart_clk_notitifer_cb - Clock notifier callback
 471 * @nb:         Notifier block
 472 * @event:      Notify event
 473 * @data:       Notifier data
 474 * Return:      NOTIFY_OK or NOTIFY_DONE on success, NOTIFY_BAD on error.
 475 */
 476static int cdns_uart_clk_notifier_cb(struct notifier_block *nb,
 477                unsigned long event, void *data)
 478{
 479        u32 ctrl_reg;
 480        struct uart_port *port;
 481        int locked = 0;
 482        struct clk_notifier_data *ndata = data;
 483        unsigned long flags = 0;
 484        struct cdns_uart *cdns_uart = to_cdns_uart(nb);
 485
 486        port = cdns_uart->port;
 487        if (port->suspended)
 488                return NOTIFY_OK;
 489
 490        switch (event) {
 491        case PRE_RATE_CHANGE:
 492        {
 493                u32 bdiv, cd;
 494                int div8;
 495
 496                /*
 497                 * Find out if current baud-rate can be achieved with new clock
 498                 * frequency.
 499                 */
 500                if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud,
 501                                        &bdiv, &cd, &div8)) {
 502                        dev_warn(port->dev, "clock rate change rejected\n");
 503                        return NOTIFY_BAD;
 504                }
 505
 506                spin_lock_irqsave(&cdns_uart->port->lock, flags);
 507
 508                /* Disable the TX and RX to set baud rate */
 509                ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
 510                ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
 511                cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
 512
 513                spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
 514
 515                return NOTIFY_OK;
 516        }
 517        case POST_RATE_CHANGE:
 518                /*
 519                 * Set clk dividers to generate correct baud with new clock
 520                 * frequency.
 521                 */
 522
 523                spin_lock_irqsave(&cdns_uart->port->lock, flags);
 524
 525                locked = 1;
 526                port->uartclk = ndata->new_rate;
 527
 528                cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port,
 529                                cdns_uart->baud);
 530                /* fall through */
 531        case ABORT_RATE_CHANGE:
 532                if (!locked)
 533                        spin_lock_irqsave(&cdns_uart->port->lock, flags);
 534
 535                /* Set TX/RX Reset */
 536                ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
 537                ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
 538                cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
 539
 540                while (cdns_uart_readl(CDNS_UART_CR_OFFSET) &
 541                                (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
 542                        cpu_relax();
 543
 544                /*
 545                 * Clear the RX disable and TX disable bits and then set the TX
 546                 * enable bit and RX enable bit to enable the transmitter and
 547                 * receiver.
 548                 */
 549                cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
 550                ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
 551                ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
 552                ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
 553                cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
 554
 555                spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
 556
 557                return NOTIFY_OK;
 558        default:
 559                return NOTIFY_DONE;
 560        }
 561}
 562#endif
 563
 564/**
 565 * cdns_uart_start_tx -  Start transmitting bytes
 566 * @port: Handle to the uart port structure
 567 */
 568static void cdns_uart_start_tx(struct uart_port *port)
 569{
 570        unsigned int status, numbytes = port->fifosize;
 571
 572        if (uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port))
 573                return;
 574
 575        status = cdns_uart_readl(CDNS_UART_CR_OFFSET);
 576        /* Set the TX enable bit and clear the TX disable bit to enable the
 577         * transmitter.
 578         */
 579        cdns_uart_writel((status & ~CDNS_UART_CR_TX_DIS) | CDNS_UART_CR_TX_EN,
 580                CDNS_UART_CR_OFFSET);
 581
 582        while (numbytes-- && ((cdns_uart_readl(CDNS_UART_SR_OFFSET) &
 583                                CDNS_UART_SR_TXFULL)) != CDNS_UART_SR_TXFULL) {
 584                /* Break if no more data available in the UART buffer */
 585                if (uart_circ_empty(&port->state->xmit))
 586                        break;
 587
 588                /* Get the data from the UART circular buffer and
 589                 * write it to the cdns_uart's TX_FIFO register.
 590                 */
 591                cdns_uart_writel(
 592                        port->state->xmit.buf[port->state->xmit.tail],
 593                        CDNS_UART_FIFO_OFFSET);
 594                port->icount.tx++;
 595
 596                /* Adjust the tail of the UART buffer and wrap
 597                 * the buffer if it reaches limit.
 598                 */
 599                port->state->xmit.tail = (port->state->xmit.tail + 1) &
 600                                        (UART_XMIT_SIZE - 1);
 601        }
 602        cdns_uart_writel(CDNS_UART_IXR_TXEMPTY, CDNS_UART_ISR_OFFSET);
 603        /* Enable the TX Empty interrupt */
 604        cdns_uart_writel(CDNS_UART_IXR_TXEMPTY, CDNS_UART_IER_OFFSET);
 605
 606        if (uart_circ_chars_pending(&port->state->xmit) < WAKEUP_CHARS)
 607                uart_write_wakeup(port);
 608}
 609
 610/**
 611 * cdns_uart_stop_tx - Stop TX
 612 * @port: Handle to the uart port structure
 613 */
 614static void cdns_uart_stop_tx(struct uart_port *port)
 615{
 616        unsigned int regval;
 617
 618        regval = cdns_uart_readl(CDNS_UART_CR_OFFSET);
 619        regval |= CDNS_UART_CR_TX_DIS;
 620        /* Disable the transmitter */
 621        cdns_uart_writel(regval, CDNS_UART_CR_OFFSET);
 622}
 623
 624/**
 625 * cdns_uart_stop_rx - Stop RX
 626 * @port: Handle to the uart port structure
 627 */
 628static void cdns_uart_stop_rx(struct uart_port *port)
 629{
 630        unsigned int regval;
 631
 632        regval = cdns_uart_readl(CDNS_UART_CR_OFFSET);
 633        regval |= CDNS_UART_CR_RX_DIS;
 634        /* Disable the receiver */
 635        cdns_uart_writel(regval, CDNS_UART_CR_OFFSET);
 636}
 637
 638/**
 639 * cdns_uart_tx_empty -  Check whether TX is empty
 640 * @port: Handle to the uart port structure
 641 *
 642 * Return: TIOCSER_TEMT on success, 0 otherwise
 643 */
 644static unsigned int cdns_uart_tx_empty(struct uart_port *port)
 645{
 646        unsigned int status;
 647
 648        status = cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY;
 649        return status ? TIOCSER_TEMT : 0;
 650}
 651
 652/**
 653 * cdns_uart_break_ctl - Based on the input ctl we have to start or stop
 654 *                      transmitting char breaks
 655 * @port: Handle to the uart port structure
 656 * @ctl: Value based on which start or stop decision is taken
 657 */
 658static void cdns_uart_break_ctl(struct uart_port *port, int ctl)
 659{
 660        unsigned int status;
 661        unsigned long flags;
 662
 663        spin_lock_irqsave(&port->lock, flags);
 664
 665        status = cdns_uart_readl(CDNS_UART_CR_OFFSET);
 666
 667        if (ctl == -1)
 668                cdns_uart_writel(CDNS_UART_CR_STARTBRK | status,
 669                                        CDNS_UART_CR_OFFSET);
 670        else {
 671                if ((status & CDNS_UART_CR_STOPBRK) == 0)
 672                        cdns_uart_writel(CDNS_UART_CR_STOPBRK | status,
 673                                         CDNS_UART_CR_OFFSET);
 674        }
 675        spin_unlock_irqrestore(&port->lock, flags);
 676}
 677
 678/**
 679 * cdns_uart_set_termios - termios operations, handling data length, parity,
 680 *                              stop bits, flow control, baud rate
 681 * @port: Handle to the uart port structure
 682 * @termios: Handle to the input termios structure
 683 * @old: Values of the previously saved termios structure
 684 */
 685static void cdns_uart_set_termios(struct uart_port *port,
 686                                struct ktermios *termios, struct ktermios *old)
 687{
 688        unsigned int cval = 0;
 689        unsigned int baud, minbaud, maxbaud;
 690        unsigned long flags;
 691        unsigned int ctrl_reg, mode_reg;
 692
 693        spin_lock_irqsave(&port->lock, flags);
 694
 695        /* Wait for the transmit FIFO to empty before making changes */
 696        if (!(cdns_uart_readl(CDNS_UART_CR_OFFSET) & CDNS_UART_CR_TX_DIS)) {
 697                while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) &
 698                                CDNS_UART_SR_TXEMPTY)) {
 699                        cpu_relax();
 700                }
 701        }
 702
 703        /* Disable the TX and RX to set baud rate */
 704        ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
 705        ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
 706        cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
 707
 708        /*
 709         * Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk
 710         * min and max baud should be calculated here based on port->uartclk.
 711         * this way we get a valid baud and can safely call set_baud()
 712         */
 713        minbaud = port->uartclk /
 714                        ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8);
 715        maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1);
 716        baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
 717        baud = cdns_uart_set_baud_rate(port, baud);
 718        if (tty_termios_baud_rate(termios))
 719                tty_termios_encode_baud_rate(termios, baud, baud);
 720
 721        /* Update the per-port timeout. */
 722        uart_update_timeout(port, termios->c_cflag, baud);
 723
 724        /* Set TX/RX Reset */
 725        ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
 726        ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
 727        cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
 728
 729        while (readl(port->membase + CDNS_UART_CR_OFFSET) &
 730                (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
 731                cpu_relax();
 732        /*
 733         * Clear the RX disable and TX disable bits and then set the TX enable
 734         * bit and RX enable bit to enable the transmitter and receiver.
 735         */
 736        ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
 737        ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
 738        ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
 739        cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
 740
 741        cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
 742
 743        port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
 744                        CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
 745        port->ignore_status_mask = 0;
 746
 747        if (termios->c_iflag & INPCK)
 748                port->read_status_mask |= CDNS_UART_IXR_PARITY |
 749                CDNS_UART_IXR_FRAMING;
 750
 751        if (termios->c_iflag & IGNPAR)
 752                port->ignore_status_mask |= CDNS_UART_IXR_PARITY |
 753                                            CDNS_UART_IXR_FRAMING;
 754
 755        /* ignore all characters if CREAD is not set */
 756        if ((termios->c_cflag & CREAD) == 0)
 757                port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG |
 758                        CDNS_UART_IXR_TOUT | CDNS_UART_IXR_PARITY |
 759                        CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
 760
 761        mode_reg = cdns_uart_readl(CDNS_UART_MR_OFFSET);
 762
 763        /* Handling Data Size */
 764        switch (termios->c_cflag & CSIZE) {
 765        case CS6:
 766                cval |= CDNS_UART_MR_CHARLEN_6_BIT;
 767                break;
 768        case CS7:
 769                cval |= CDNS_UART_MR_CHARLEN_7_BIT;
 770                break;
 771        default:
 772        case CS8:
 773                cval |= CDNS_UART_MR_CHARLEN_8_BIT;
 774                termios->c_cflag &= ~CSIZE;
 775                termios->c_cflag |= CS8;
 776                break;
 777        }
 778
 779        /* Handling Parity and Stop Bits length */
 780        if (termios->c_cflag & CSTOPB)
 781                cval |= CDNS_UART_MR_STOPMODE_2_BIT; /* 2 STOP bits */
 782        else
 783                cval |= CDNS_UART_MR_STOPMODE_1_BIT; /* 1 STOP bit */
 784
 785        if ((termios->c_cflag & PARENB) || (termios->c_cflag & PARODD)) {
 786                /* Mark or Space parity */
 787                if (termios->c_cflag & CMSPAR) {
 788                        if (termios->c_cflag & PARODD)
 789                                cval |= CDNS_UART_MR_PARITY_MARK;
 790                        else
 791                                cval |= CDNS_UART_MR_PARITY_SPACE;
 792                } else {
 793                        if (termios->c_cflag & PARODD)
 794                                cval |= CDNS_UART_MR_PARITY_ODD;
 795                        else
 796                                cval |= CDNS_UART_MR_PARITY_EVEN;
 797                }
 798        } else {
 799                cval |= CDNS_UART_MR_PARITY_NONE;
 800        }
 801        cval |= mode_reg & 1;
 802        cdns_uart_writel(cval, CDNS_UART_MR_OFFSET);
 803
 804        spin_unlock_irqrestore(&port->lock, flags);
 805}
 806
 807/**
 808 * cdns_uart_startup - Called when an application opens a cdns_uart port
 809 * @port: Handle to the uart port structure
 810 *
 811 * Return: 0 on success, negative errno otherwise
 812 */
 813static int cdns_uart_startup(struct uart_port *port)
 814{
 815        struct cdns_uart *cdns_uart = port->private_data;
 816        bool is_brk_support;
 817        unsigned int retval = 0, status = 0;
 818
 819        is_brk_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
 820        retval = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME,
 821                                                                (void *)port);
 822        if (retval)
 823                return retval;
 824
 825        /* Disable the TX and RX */
 826        cdns_uart_writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
 827                                                CDNS_UART_CR_OFFSET);
 828
 829        /* Set the Control Register with TX/RX Enable, TX/RX Reset,
 830         * no break chars.
 831         */
 832        cdns_uart_writel(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST,
 833                                CDNS_UART_CR_OFFSET);
 834
 835        while (readl(port->membase + CDNS_UART_CR_OFFSET) &
 836                (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
 837                cpu_relax();
 838
 839        status = cdns_uart_readl(CDNS_UART_CR_OFFSET);
 840
 841        /* Clear the RX disable and TX disable bits and then set the TX enable
 842         * bit and RX enable bit to enable the transmitter and receiver.
 843         */
 844        cdns_uart_writel((status & ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS))
 845                        | (CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN |
 846                        CDNS_UART_CR_STOPBRK), CDNS_UART_CR_OFFSET);
 847
 848        /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
 849         * no parity.
 850         */
 851        cdns_uart_writel(CDNS_UART_MR_CHMODE_NORM | CDNS_UART_MR_STOPMODE_1_BIT
 852                | CDNS_UART_MR_PARITY_NONE | CDNS_UART_MR_CHARLEN_8_BIT,
 853                 CDNS_UART_MR_OFFSET);
 854
 855        /*
 856         * Set the RX FIFO Trigger level to use most of the FIFO, but it
 857         * can be tuned with a module parameter
 858         */
 859        cdns_uart_writel(rx_trigger_level, CDNS_UART_RXWM_OFFSET);
 860
 861        /*
 862         * Receive Timeout register is enabled but it
 863         * can be tuned with a module parameter
 864         */
 865        cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
 866
 867        /* Clear out any pending interrupts before enabling them */
 868        cdns_uart_writel(cdns_uart_readl(CDNS_UART_ISR_OFFSET),
 869                        CDNS_UART_ISR_OFFSET);
 870
 871        /*
 872         * Set the Interrupt Registers with desired interrupts. Do not
 873         * enable parity error interrupt for the following reason:
 874         * When parity error interrupt is enabled, each Rx parity error always
 875         * results in 2 events. The first one being parity error interrupt
 876         * and the second one with a proper Rx interrupt with the incoming data.
 877         * Disabling parity error interrupt ensures better handling of parity
 878         * error events. With this change, for a parity error case, we get a
 879         * Rx interrupt with parity error set in ISR register and we still
 880         * handle parity errors in the desired way.
 881         */
 882        cdns_uart_writel(CDNS_UART_IXR_TXEMPTY |
 883                CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN |
 884                CDNS_UART_IXR_RXTRIG | CDNS_UART_IXR_TOUT,
 885                CDNS_UART_IER_OFFSET);
 886        if (is_brk_support) {
 887                status = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
 888                cdns_uart_writel((status | CDNS_UART_IXR_BRK),
 889                                        CDNS_UART_IER_OFFSET);
 890        }
 891
 892        return retval;
 893}
 894
 895/**
 896 * cdns_uart_shutdown - Called when an application closes a cdns_uart port
 897 * @port: Handle to the uart port structure
 898 */
 899static void cdns_uart_shutdown(struct uart_port *port)
 900{
 901        int status;
 902
 903        /* Disable interrupts */
 904        status = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
 905        cdns_uart_writel(status, CDNS_UART_IDR_OFFSET);
 906
 907        /* Disable the TX and RX */
 908        cdns_uart_writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
 909                                 CDNS_UART_CR_OFFSET);
 910        free_irq(port->irq, port);
 911}
 912
 913/**
 914 * cdns_uart_type - Set UART type to cdns_uart port
 915 * @port: Handle to the uart port structure
 916 *
 917 * Return: string on success, NULL otherwise
 918 */
 919static const char *cdns_uart_type(struct uart_port *port)
 920{
 921        return port->type == PORT_XUARTPS ? CDNS_UART_NAME : NULL;
 922}
 923
 924/**
 925 * cdns_uart_verify_port - Verify the port params
 926 * @port: Handle to the uart port structure
 927 * @ser: Handle to the structure whose members are compared
 928 *
 929 * Return: 0 on success, negative errno otherwise.
 930 */
 931static int cdns_uart_verify_port(struct uart_port *port,
 932                                        struct serial_struct *ser)
 933{
 934        if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
 935                return -EINVAL;
 936        if (port->irq != ser->irq)
 937                return -EINVAL;
 938        if (ser->io_type != UPIO_MEM)
 939                return -EINVAL;
 940        if (port->iobase != ser->port)
 941                return -EINVAL;
 942        if (ser->hub6 != 0)
 943                return -EINVAL;
 944        return 0;
 945}
 946
 947/**
 948 * cdns_uart_request_port - Claim the memory region attached to cdns_uart port,
 949 *                              called when the driver adds a cdns_uart port via
 950 *                              uart_add_one_port()
 951 * @port: Handle to the uart port structure
 952 *
 953 * Return: 0 on success, negative errno otherwise.
 954 */
 955static int cdns_uart_request_port(struct uart_port *port)
 956{
 957        if (!request_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE,
 958                                         CDNS_UART_NAME)) {
 959                return -ENOMEM;
 960        }
 961
 962        port->membase = ioremap(port->mapbase, CDNS_UART_REGISTER_SPACE);
 963        if (!port->membase) {
 964                dev_err(port->dev, "Unable to map registers\n");
 965                release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
 966                return -ENOMEM;
 967        }
 968        return 0;
 969}
 970
 971/**
 972 * cdns_uart_release_port - Release UART port
 973 * @port: Handle to the uart port structure
 974 *
 975 * Release the memory region attached to a cdns_uart port. Called when the
 976 * driver removes a cdns_uart port via uart_remove_one_port().
 977 */
 978static void cdns_uart_release_port(struct uart_port *port)
 979{
 980        release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
 981        iounmap(port->membase);
 982        port->membase = NULL;
 983}
 984
 985/**
 986 * cdns_uart_config_port - Configure UART port
 987 * @port: Handle to the uart port structure
 988 * @flags: If any
 989 */
 990static void cdns_uart_config_port(struct uart_port *port, int flags)
 991{
 992        if (flags & UART_CONFIG_TYPE && cdns_uart_request_port(port) == 0)
 993                port->type = PORT_XUARTPS;
 994}
 995
 996/**
 997 * cdns_uart_get_mctrl - Get the modem control state
 998 * @port: Handle to the uart port structure
 999 *
1000 * Return: the modem control state
1001 */
1002static unsigned int cdns_uart_get_mctrl(struct uart_port *port)
1003{
1004        return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
1005}
1006
1007static void cdns_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1008{
1009        u32 val;
1010
1011        val = cdns_uart_readl(CDNS_UART_MODEMCR_OFFSET);
1012
1013        val &= ~(CDNS_UART_MODEMCR_RTS | CDNS_UART_MODEMCR_DTR);
1014
1015        if (mctrl & TIOCM_RTS)
1016                val |= CDNS_UART_MODEMCR_RTS;
1017        if (mctrl & TIOCM_DTR)
1018                val |= CDNS_UART_MODEMCR_DTR;
1019
1020        cdns_uart_writel(val, CDNS_UART_MODEMCR_OFFSET);
1021}
1022
1023#ifdef CONFIG_CONSOLE_POLL
1024static int cdns_uart_poll_get_char(struct uart_port *port)
1025{
1026        u32 imr;
1027        int c;
1028
1029        /* Disable all interrupts */
1030        imr = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
1031        cdns_uart_writel(imr, CDNS_UART_IDR_OFFSET);
1032
1033        /* Check if FIFO is empty */
1034        if (cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_RXEMPTY)
1035                c = NO_POLL_CHAR;
1036        else /* Read a character */
1037                c = (unsigned char) cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
1038
1039        /* Enable interrupts */
1040        cdns_uart_writel(imr, CDNS_UART_IER_OFFSET);
1041
1042        return c;
1043}
1044
1045static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
1046{
1047        u32 imr;
1048
1049        /* Disable all interrupts */
1050        imr = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
1051        cdns_uart_writel(imr, CDNS_UART_IDR_OFFSET);
1052
1053        /* Wait until FIFO is empty */
1054        while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY))
1055                cpu_relax();
1056
1057        /* Write a character */
1058        cdns_uart_writel(c, CDNS_UART_FIFO_OFFSET);
1059
1060        /* Wait until FIFO is empty */
1061        while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY))
1062                cpu_relax();
1063
1064        /* Enable interrupts */
1065        cdns_uart_writel(imr, CDNS_UART_IER_OFFSET);
1066
1067        return;
1068}
1069#endif
1070
1071static void cdns_uart_pm(struct uart_port *port, unsigned int state,
1072                   unsigned int oldstate)
1073{
1074        struct cdns_uart *cdns_uart = port->private_data;
1075
1076        switch (state) {
1077        case UART_PM_STATE_OFF:
1078                clk_disable(cdns_uart->uartclk);
1079                clk_disable(cdns_uart->pclk);
1080                break;
1081        default:
1082                clk_enable(cdns_uart->pclk);
1083                clk_enable(cdns_uart->uartclk);
1084                break;
1085        }
1086}
1087
1088static struct uart_ops cdns_uart_ops = {
1089        .set_mctrl      = cdns_uart_set_mctrl,
1090        .get_mctrl      = cdns_uart_get_mctrl,
1091        .start_tx       = cdns_uart_start_tx,
1092        .stop_tx        = cdns_uart_stop_tx,
1093        .stop_rx        = cdns_uart_stop_rx,
1094        .tx_empty       = cdns_uart_tx_empty,
1095        .break_ctl      = cdns_uart_break_ctl,
1096        .set_termios    = cdns_uart_set_termios,
1097        .startup        = cdns_uart_startup,
1098        .shutdown       = cdns_uart_shutdown,
1099        .pm             = cdns_uart_pm,
1100        .type           = cdns_uart_type,
1101        .verify_port    = cdns_uart_verify_port,
1102        .request_port   = cdns_uart_request_port,
1103        .release_port   = cdns_uart_release_port,
1104        .config_port    = cdns_uart_config_port,
1105#ifdef CONFIG_CONSOLE_POLL
1106        .poll_get_char  = cdns_uart_poll_get_char,
1107        .poll_put_char  = cdns_uart_poll_put_char,
1108#endif
1109};
1110
1111static struct uart_port cdns_uart_port[CDNS_UART_NR_PORTS];
1112
1113/**
1114 * cdns_uart_get_port - Configure the port from platform device resource info
1115 * @id: Port id
1116 *
1117 * Return: a pointer to a uart_port or NULL for failure
1118 */
1119static struct uart_port *cdns_uart_get_port(int id)
1120{
1121        struct uart_port *port;
1122
1123        /* Try the given port id if failed use default method */
1124        if (cdns_uart_port[id].mapbase != 0) {
1125                /* Find the next unused port */
1126                for (id = 0; id < CDNS_UART_NR_PORTS; id++)
1127                        if (cdns_uart_port[id].mapbase == 0)
1128                                break;
1129        }
1130
1131        if (id >= CDNS_UART_NR_PORTS)
1132                return NULL;
1133
1134        port = &cdns_uart_port[id];
1135
1136        /* At this point, we've got an empty uart_port struct, initialize it */
1137        spin_lock_init(&port->lock);
1138        port->membase   = NULL;
1139        port->irq       = 0;
1140        port->type      = PORT_UNKNOWN;
1141        port->iotype    = UPIO_MEM32;
1142        port->flags     = UPF_BOOT_AUTOCONF;
1143        port->ops       = &cdns_uart_ops;
1144        port->fifosize  = CDNS_UART_FIFO_SIZE;
1145        port->line      = id;
1146        port->dev       = NULL;
1147        return port;
1148}
1149
1150#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1151/**
1152 * cdns_uart_console_wait_tx - Wait for the TX to be full
1153 * @port: Handle to the uart port structure
1154 */
1155static void cdns_uart_console_wait_tx(struct uart_port *port)
1156{
1157        while ((cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY)
1158                                != CDNS_UART_SR_TXEMPTY)
1159                barrier();
1160}
1161
1162/**
1163 * cdns_uart_console_putchar - write the character to the FIFO buffer
1164 * @port: Handle to the uart port structure
1165 * @ch: Character to be written
1166 */
1167static void cdns_uart_console_putchar(struct uart_port *port, int ch)
1168{
1169        cdns_uart_console_wait_tx(port);
1170        cdns_uart_writel(ch, CDNS_UART_FIFO_OFFSET);
1171}
1172
1173static void __init cdns_early_write(struct console *con, const char *s,
1174                                    unsigned n)
1175{
1176        struct earlycon_device *dev = con->data;
1177
1178        uart_console_write(&dev->port, s, n, cdns_uart_console_putchar);
1179}
1180
1181static int __init cdns_early_console_setup(struct earlycon_device *device,
1182                                           const char *opt)
1183{
1184        if (!device->port.membase)
1185                return -ENODEV;
1186
1187        device->con->write = cdns_early_write;
1188
1189        return 0;
1190}
1191EARLYCON_DECLARE(cdns, cdns_early_console_setup);
1192OF_EARLYCON_DECLARE(cdns_a, "xlnx,xuartps", cdns_early_console_setup);
1193OF_EARLYCON_DECLARE(cdns_b, "cdns,uart-r1p8", cdns_early_console_setup);
1194OF_EARLYCON_DECLARE(cdns_c, "cdns,uart-r1p12", cdns_early_console_setup);
1195
1196/**
1197 * cdns_uart_console_write - perform write operation
1198 * @co: Console handle
1199 * @s: Pointer to character array
1200 * @count: No of characters
1201 */
1202static void cdns_uart_console_write(struct console *co, const char *s,
1203                                unsigned int count)
1204{
1205        struct uart_port *port = &cdns_uart_port[co->index];
1206        unsigned long flags;
1207        unsigned int imr, ctrl;
1208        int locked = 1;
1209
1210        if (oops_in_progress)
1211                locked = spin_trylock_irqsave(&port->lock, flags);
1212        else
1213                spin_lock_irqsave(&port->lock, flags);
1214
1215        /* save and disable interrupt */
1216        imr = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
1217        cdns_uart_writel(imr, CDNS_UART_IDR_OFFSET);
1218
1219        /*
1220         * Make sure that the tx part is enabled. Set the TX enable bit and
1221         * clear the TX disable bit to enable the transmitter.
1222         */
1223        ctrl = cdns_uart_readl(CDNS_UART_CR_OFFSET);
1224        cdns_uart_writel((ctrl & ~CDNS_UART_CR_TX_DIS) | CDNS_UART_CR_TX_EN,
1225                CDNS_UART_CR_OFFSET);
1226
1227        uart_console_write(port, s, count, cdns_uart_console_putchar);
1228        cdns_uart_console_wait_tx(port);
1229
1230        cdns_uart_writel(ctrl, CDNS_UART_CR_OFFSET);
1231
1232        /* restore interrupt state */
1233        cdns_uart_writel(imr, CDNS_UART_IER_OFFSET);
1234
1235        if (locked)
1236                spin_unlock_irqrestore(&port->lock, flags);
1237}
1238
1239/**
1240 * cdns_uart_console_setup - Initialize the uart to default config
1241 * @co: Console handle
1242 * @options: Initial settings of uart
1243 *
1244 * Return: 0 on success, negative errno otherwise.
1245 */
1246static int __init cdns_uart_console_setup(struct console *co, char *options)
1247{
1248        struct uart_port *port = &cdns_uart_port[co->index];
1249        int baud = 9600;
1250        int bits = 8;
1251        int parity = 'n';
1252        int flow = 'n';
1253
1254        if (co->index < 0 || co->index >= CDNS_UART_NR_PORTS)
1255                return -EINVAL;
1256
1257        if (!port->membase) {
1258                pr_debug("console on ttyPS%i not present\n", co->index);
1259                return -ENODEV;
1260        }
1261
1262        if (options)
1263                uart_parse_options(options, &baud, &parity, &bits, &flow);
1264
1265        return uart_set_options(port, co, baud, parity, bits, flow);
1266}
1267
1268static struct uart_driver cdns_uart_uart_driver;
1269
1270static struct console cdns_uart_console = {
1271        .name   = CDNS_UART_TTY_NAME,
1272        .write  = cdns_uart_console_write,
1273        .device = uart_console_device,
1274        .setup  = cdns_uart_console_setup,
1275        .flags  = CON_PRINTBUFFER,
1276        .index  = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
1277        .data   = &cdns_uart_uart_driver,
1278};
1279
1280/**
1281 * cdns_uart_console_init - Initialization call
1282 *
1283 * Return: 0 on success, negative errno otherwise
1284 */
1285static int __init cdns_uart_console_init(void)
1286{
1287        register_console(&cdns_uart_console);
1288        return 0;
1289}
1290
1291console_initcall(cdns_uart_console_init);
1292
1293#endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
1294
1295static struct uart_driver cdns_uart_uart_driver = {
1296        .owner          = THIS_MODULE,
1297        .driver_name    = CDNS_UART_NAME,
1298        .dev_name       = CDNS_UART_TTY_NAME,
1299        .major          = CDNS_UART_MAJOR,
1300        .minor          = CDNS_UART_MINOR,
1301        .nr             = CDNS_UART_NR_PORTS,
1302#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1303        .cons           = &cdns_uart_console,
1304#endif
1305};
1306
1307#ifdef CONFIG_PM_SLEEP
1308/**
1309 * cdns_uart_suspend - suspend event
1310 * @device: Pointer to the device structure
1311 *
1312 * Return: 0
1313 */
1314static int cdns_uart_suspend(struct device *device)
1315{
1316        struct uart_port *port = dev_get_drvdata(device);
1317        struct tty_struct *tty;
1318        struct device *tty_dev;
1319        int may_wake = 0;
1320
1321        /* Get the tty which could be NULL so don't assume it's valid */
1322        tty = tty_port_tty_get(&port->state->port);
1323        if (tty) {
1324                tty_dev = tty->dev;
1325                may_wake = device_may_wakeup(tty_dev);
1326                tty_kref_put(tty);
1327        }
1328
1329        /*
1330         * Call the API provided in serial_core.c file which handles
1331         * the suspend.
1332         */
1333        uart_suspend_port(&cdns_uart_uart_driver, port);
1334        if (console_suspend_enabled && !may_wake) {
1335                struct cdns_uart *cdns_uart = port->private_data;
1336
1337                clk_disable(cdns_uart->uartclk);
1338                clk_disable(cdns_uart->pclk);
1339        } else {
1340                unsigned long flags = 0;
1341
1342                spin_lock_irqsave(&port->lock, flags);
1343                /* Empty the receive FIFO 1st before making changes */
1344                while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) &
1345                                        CDNS_UART_SR_RXEMPTY))
1346                        cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
1347                /* set RX trigger level to 1 */
1348                cdns_uart_writel(1, CDNS_UART_RXWM_OFFSET);
1349                /* disable RX timeout interrups */
1350                cdns_uart_writel(CDNS_UART_IXR_TOUT, CDNS_UART_IDR_OFFSET);
1351                spin_unlock_irqrestore(&port->lock, flags);
1352        }
1353
1354        return 0;
1355}
1356
1357/**
1358 * cdns_uart_resume - Resume after a previous suspend
1359 * @device: Pointer to the device structure
1360 *
1361 * Return: 0
1362 */
1363static int cdns_uart_resume(struct device *device)
1364{
1365        struct uart_port *port = dev_get_drvdata(device);
1366        unsigned long flags = 0;
1367        u32 ctrl_reg;
1368        struct tty_struct *tty;
1369        struct device *tty_dev;
1370        int may_wake = 0;
1371
1372        /* Get the tty which could be NULL so don't assume it's valid */
1373        tty = tty_port_tty_get(&port->state->port);
1374        if (tty) {
1375                tty_dev = tty->dev;
1376                may_wake = device_may_wakeup(tty_dev);
1377                tty_kref_put(tty);
1378        }
1379
1380        if (console_suspend_enabled && !may_wake) {
1381                struct cdns_uart *cdns_uart = port->private_data;
1382
1383                clk_enable(cdns_uart->pclk);
1384                clk_enable(cdns_uart->uartclk);
1385
1386                spin_lock_irqsave(&port->lock, flags);
1387
1388                /* Set TX/RX Reset */
1389                ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
1390                ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
1391                cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
1392                while (cdns_uart_readl(CDNS_UART_CR_OFFSET) &
1393                                (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
1394                        cpu_relax();
1395
1396                /* restore rx timeout value */
1397                cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
1398                /* Enable Tx/Rx */
1399                ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
1400                ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
1401                ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
1402                cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
1403
1404                spin_unlock_irqrestore(&port->lock, flags);
1405        } else {
1406                spin_lock_irqsave(&port->lock, flags);
1407                /* restore original rx trigger level */
1408                cdns_uart_writel(rx_trigger_level, CDNS_UART_RXWM_OFFSET);
1409                /* enable RX timeout interrupt */
1410                cdns_uart_writel(CDNS_UART_IXR_TOUT, CDNS_UART_IER_OFFSET);
1411                spin_unlock_irqrestore(&port->lock, flags);
1412        }
1413
1414        return uart_resume_port(&cdns_uart_uart_driver, port);
1415}
1416#endif /* ! CONFIG_PM_SLEEP */
1417
1418static SIMPLE_DEV_PM_OPS(cdns_uart_dev_pm_ops, cdns_uart_suspend,
1419                cdns_uart_resume);
1420static const struct cdns_platform_data zynqmp_uart_def = {
1421                                        .quirks = CDNS_UART_RXBS_SUPPORT, };
1422/* Match table for of_platform binding */
1423static const struct of_device_id cdns_uart_of_match[] = {
1424        { .compatible = "xlnx,xuartps", },
1425        { .compatible = "cdns,uart-r1p8", },
1426        { .compatible = "cdns,uart-r1p12", .data = &zynqmp_uart_def },
1427        {}
1428};
1429MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
1430
1431/**
1432 * cdns_uart_probe - Platform driver probe
1433 * @pdev: Pointer to the platform device structure
1434 *
1435 * Return: 0 on success, negative errno otherwise
1436 */
1437static int cdns_uart_probe(struct platform_device *pdev)
1438{
1439        int rc, id, irq;
1440        struct uart_port *port;
1441        struct resource *res;
1442        struct cdns_uart *cdns_uart_data;
1443        const struct of_device_id *match;
1444
1445        cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
1446                        GFP_KERNEL);
1447        if (!cdns_uart_data)
1448                return -ENOMEM;
1449
1450        match = of_match_node(cdns_uart_of_match, pdev->dev.of_node);
1451        if (match && match->data) {
1452                const struct cdns_platform_data *data = match->data;
1453
1454                cdns_uart_data->quirks = data->quirks;
1455        }
1456
1457        cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk");
1458        if (IS_ERR(cdns_uart_data->pclk)) {
1459                cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk");
1460                if (!IS_ERR(cdns_uart_data->pclk))
1461                        dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n");
1462        }
1463        if (IS_ERR(cdns_uart_data->pclk)) {
1464                dev_err(&pdev->dev, "pclk clock not found.\n");
1465                return PTR_ERR(cdns_uart_data->pclk);
1466        }
1467
1468        cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk");
1469        if (IS_ERR(cdns_uart_data->uartclk)) {
1470                cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk");
1471                if (!IS_ERR(cdns_uart_data->uartclk))
1472                        dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n");
1473        }
1474        if (IS_ERR(cdns_uart_data->uartclk)) {
1475                dev_err(&pdev->dev, "uart_clk clock not found.\n");
1476                return PTR_ERR(cdns_uart_data->uartclk);
1477        }
1478
1479        rc = clk_prepare(cdns_uart_data->pclk);
1480        if (rc) {
1481                dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
1482                return rc;
1483        }
1484        rc = clk_prepare(cdns_uart_data->uartclk);
1485        if (rc) {
1486                dev_err(&pdev->dev, "Unable to enable device clock.\n");
1487                goto err_out_clk_dis_pclk;
1488        }
1489
1490        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1491        if (!res) {
1492                rc = -ENODEV;
1493                goto err_out_clk_disable;
1494        }
1495
1496        irq = platform_get_irq(pdev, 0);
1497        if (irq <= 0) {
1498                rc = -ENXIO;
1499                goto err_out_clk_disable;
1500        }
1501
1502#ifdef CONFIG_COMMON_CLK
1503        cdns_uart_data->clk_rate_change_nb.notifier_call =
1504                        cdns_uart_clk_notifier_cb;
1505        if (clk_notifier_register(cdns_uart_data->uartclk,
1506                                &cdns_uart_data->clk_rate_change_nb))
1507                dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
1508#endif
1509        /* Look for a serialN alias */
1510        id = of_alias_get_id(pdev->dev.of_node, "serial");
1511        if (id < 0)
1512                id = 0;
1513
1514        /* Initialize the port structure */
1515        port = cdns_uart_get_port(id);
1516
1517        if (!port) {
1518                dev_err(&pdev->dev, "Cannot get uart_port structure\n");
1519                rc = -ENODEV;
1520                goto err_out_notif_unreg;
1521        } else {
1522                /* Register the port.
1523                 * This function also registers this device with the tty layer
1524                 * and triggers invocation of the config_port() entry point.
1525                 */
1526                port->mapbase = res->start;
1527                port->irq = irq;
1528                port->dev = &pdev->dev;
1529                port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
1530                port->private_data = cdns_uart_data;
1531                cdns_uart_data->port = port;
1532                platform_set_drvdata(pdev, port);
1533                rc = uart_add_one_port(&cdns_uart_uart_driver, port);
1534                if (rc) {
1535                        dev_err(&pdev->dev,
1536                                "uart_add_one_port() failed; err=%i\n", rc);
1537                        goto err_out_notif_unreg;
1538                }
1539                return 0;
1540        }
1541
1542err_out_notif_unreg:
1543#ifdef CONFIG_COMMON_CLK
1544        clk_notifier_unregister(cdns_uart_data->uartclk,
1545                        &cdns_uart_data->clk_rate_change_nb);
1546#endif
1547err_out_clk_disable:
1548        clk_unprepare(cdns_uart_data->uartclk);
1549err_out_clk_dis_pclk:
1550        clk_unprepare(cdns_uart_data->pclk);
1551
1552        return rc;
1553}
1554
1555/**
1556 * cdns_uart_remove - called when the platform driver is unregistered
1557 * @pdev: Pointer to the platform device structure
1558 *
1559 * Return: 0 on success, negative errno otherwise
1560 */
1561static int cdns_uart_remove(struct platform_device *pdev)
1562{
1563        struct uart_port *port = platform_get_drvdata(pdev);
1564        struct cdns_uart *cdns_uart_data = port->private_data;
1565        int rc;
1566
1567        /* Remove the cdns_uart port from the serial core */
1568#ifdef CONFIG_COMMON_CLK
1569        clk_notifier_unregister(cdns_uart_data->uartclk,
1570                        &cdns_uart_data->clk_rate_change_nb);
1571#endif
1572        rc = uart_remove_one_port(&cdns_uart_uart_driver, port);
1573        port->mapbase = 0;
1574        clk_unprepare(cdns_uart_data->uartclk);
1575        clk_unprepare(cdns_uart_data->pclk);
1576        return rc;
1577}
1578
1579static struct platform_driver cdns_uart_platform_driver = {
1580        .probe   = cdns_uart_probe,
1581        .remove  = cdns_uart_remove,
1582        .driver  = {
1583                .name = CDNS_UART_NAME,
1584                .of_match_table = cdns_uart_of_match,
1585                .pm = &cdns_uart_dev_pm_ops,
1586                },
1587};
1588
1589static int __init cdns_uart_init(void)
1590{
1591        int retval = 0;
1592
1593        /* Register the cdns_uart driver with the serial core */
1594        retval = uart_register_driver(&cdns_uart_uart_driver);
1595        if (retval)
1596                return retval;
1597
1598        /* Register the platform driver */
1599        retval = platform_driver_register(&cdns_uart_platform_driver);
1600        if (retval)
1601                uart_unregister_driver(&cdns_uart_uart_driver);
1602
1603        return retval;
1604}
1605
1606static void __exit cdns_uart_exit(void)
1607{
1608        /* Unregister the platform driver */
1609        platform_driver_unregister(&cdns_uart_platform_driver);
1610
1611        /* Unregister the cdns_uart driver */
1612        uart_unregister_driver(&cdns_uart_uart_driver);
1613}
1614
1615module_init(cdns_uart_init);
1616module_exit(cdns_uart_exit);
1617
1618MODULE_DESCRIPTION("Driver for Cadence UART");
1619MODULE_AUTHOR("Xilinx Inc.");
1620MODULE_LICENSE("GPL");
1621