Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Cadence UART driver (found in Xilinx Zynq)
0004  *
0005  * 2011 - 2014 (C) Xilinx Inc.
0006  *
0007  * This driver has originally been pushed by Xilinx using a Zynq-branding. This
0008  * still shows in the naming of this file, the kconfig symbols and some symbols
0009  * in the code.
0010  */
0011 
0012 #include <linux/platform_device.h>
0013 #include <linux/serial.h>
0014 #include <linux/console.h>
0015 #include <linux/serial_core.h>
0016 #include <linux/slab.h>
0017 #include <linux/tty.h>
0018 #include <linux/tty_flip.h>
0019 #include <linux/clk.h>
0020 #include <linux/irq.h>
0021 #include <linux/io.h>
0022 #include <linux/of.h>
0023 #include <linux/module.h>
0024 #include <linux/pm_runtime.h>
0025 #include <linux/iopoll.h>
0026 
0027 #define CDNS_UART_TTY_NAME  "ttyPS"
0028 #define CDNS_UART_NAME      "xuartps"
0029 #define CDNS_UART_MAJOR     0   /* use dynamic node allocation */
0030 #define CDNS_UART_MINOR     0   /* works best with devtmpfs */
0031 #define CDNS_UART_NR_PORTS  16
0032 #define CDNS_UART_FIFO_SIZE 64  /* FIFO size */
0033 #define CDNS_UART_REGISTER_SPACE    0x1000
0034 #define TX_TIMEOUT      500000
0035 
0036 /* Rx Trigger level */
0037 static int rx_trigger_level = 56;
0038 module_param(rx_trigger_level, uint, 0444);
0039 MODULE_PARM_DESC(rx_trigger_level, "Rx trigger level, 1-63 bytes");
0040 
0041 /* Rx Timeout */
0042 static int rx_timeout = 10;
0043 module_param(rx_timeout, uint, 0444);
0044 MODULE_PARM_DESC(rx_timeout, "Rx timeout, 1-255");
0045 
0046 /* Register offsets for the UART. */
0047 #define CDNS_UART_CR        0x00  /* Control Register */
0048 #define CDNS_UART_MR        0x04  /* Mode Register */
0049 #define CDNS_UART_IER       0x08  /* Interrupt Enable */
0050 #define CDNS_UART_IDR       0x0C  /* Interrupt Disable */
0051 #define CDNS_UART_IMR       0x10  /* Interrupt Mask */
0052 #define CDNS_UART_ISR       0x14  /* Interrupt Status */
0053 #define CDNS_UART_BAUDGEN   0x18  /* Baud Rate Generator */
0054 #define CDNS_UART_RXTOUT    0x1C  /* RX Timeout */
0055 #define CDNS_UART_RXWM      0x20  /* RX FIFO Trigger Level */
0056 #define CDNS_UART_MODEMCR   0x24  /* Modem Control */
0057 #define CDNS_UART_MODEMSR   0x28  /* Modem Status */
0058 #define CDNS_UART_SR        0x2C  /* Channel Status */
0059 #define CDNS_UART_FIFO      0x30  /* FIFO */
0060 #define CDNS_UART_BAUDDIV   0x34  /* Baud Rate Divider */
0061 #define CDNS_UART_FLOWDEL   0x38  /* Flow Delay */
0062 #define CDNS_UART_IRRX_PWIDTH   0x3C  /* IR Min Received Pulse Width */
0063 #define CDNS_UART_IRTX_PWIDTH   0x40  /* IR Transmitted pulse Width */
0064 #define CDNS_UART_TXWM      0x44  /* TX FIFO Trigger Level */
0065 #define CDNS_UART_RXBS      0x48  /* RX FIFO byte status register */
0066 
0067 /* Control Register Bit Definitions */
0068 #define CDNS_UART_CR_STOPBRK    0x00000100  /* Stop TX break */
0069 #define CDNS_UART_CR_STARTBRK   0x00000080  /* Set TX break */
0070 #define CDNS_UART_CR_TX_DIS 0x00000020  /* TX disabled. */
0071 #define CDNS_UART_CR_TX_EN  0x00000010  /* TX enabled */
0072 #define CDNS_UART_CR_RX_DIS 0x00000008  /* RX disabled. */
0073 #define CDNS_UART_CR_RX_EN  0x00000004  /* RX enabled */
0074 #define CDNS_UART_CR_TXRST  0x00000002  /* TX logic reset */
0075 #define CDNS_UART_CR_RXRST  0x00000001  /* RX logic reset */
0076 #define CDNS_UART_CR_RST_TO 0x00000040  /* Restart Timeout Counter */
0077 #define CDNS_UART_RXBS_PARITY    0x00000001 /* Parity error status */
0078 #define CDNS_UART_RXBS_FRAMING   0x00000002 /* Framing error status */
0079 #define CDNS_UART_RXBS_BRK       0x00000004 /* Overrun error status */
0080 
0081 /*
0082  * Mode Register:
0083  * The mode register (MR) defines the mode of transfer as well as the data
0084  * format. If this register is modified during transmission or reception,
0085  * data validity cannot be guaranteed.
0086  */
0087 #define CDNS_UART_MR_CLKSEL     0x00000001  /* Pre-scalar selection */
0088 #define CDNS_UART_MR_CHMODE_L_LOOP  0x00000200  /* Local loop back mode */
0089 #define CDNS_UART_MR_CHMODE_NORM    0x00000000  /* Normal mode */
0090 #define CDNS_UART_MR_CHMODE_MASK    0x00000300  /* Mask for mode bits */
0091 
0092 #define CDNS_UART_MR_STOPMODE_2_BIT 0x00000080  /* 2 stop bits */
0093 #define CDNS_UART_MR_STOPMODE_1_BIT 0x00000000  /* 1 stop bit */
0094 
0095 #define CDNS_UART_MR_PARITY_NONE    0x00000020  /* No parity mode */
0096 #define CDNS_UART_MR_PARITY_MARK    0x00000018  /* Mark parity mode */
0097 #define CDNS_UART_MR_PARITY_SPACE   0x00000010  /* Space parity mode */
0098 #define CDNS_UART_MR_PARITY_ODD     0x00000008  /* Odd parity mode */
0099 #define CDNS_UART_MR_PARITY_EVEN    0x00000000  /* Even parity mode */
0100 
0101 #define CDNS_UART_MR_CHARLEN_6_BIT  0x00000006  /* 6 bits data */
0102 #define CDNS_UART_MR_CHARLEN_7_BIT  0x00000004  /* 7 bits data */
0103 #define CDNS_UART_MR_CHARLEN_8_BIT  0x00000000  /* 8 bits data */
0104 
0105 /*
0106  * Interrupt Registers:
0107  * Interrupt control logic uses the interrupt enable register (IER) and the
0108  * interrupt disable register (IDR) to set the value of the bits in the
0109  * interrupt mask register (IMR). The IMR determines whether to pass an
0110  * interrupt to the interrupt status register (ISR).
0111  * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
0112  * interrupt. IMR and ISR are read only, and IER and IDR are write only.
0113  * Reading either IER or IDR returns 0x00.
0114  * All four registers have the same bit definitions.
0115  */
0116 #define CDNS_UART_IXR_TOUT  0x00000100 /* RX Timeout error interrupt */
0117 #define CDNS_UART_IXR_PARITY    0x00000080 /* Parity error interrupt */
0118 #define CDNS_UART_IXR_FRAMING   0x00000040 /* Framing error interrupt */
0119 #define CDNS_UART_IXR_OVERRUN   0x00000020 /* Overrun error interrupt */
0120 #define CDNS_UART_IXR_TXFULL    0x00000010 /* TX FIFO Full interrupt */
0121 #define CDNS_UART_IXR_TXEMPTY   0x00000008 /* TX FIFO empty interrupt */
0122 #define CDNS_UART_ISR_RXEMPTY   0x00000002 /* RX FIFO empty interrupt */
0123 #define CDNS_UART_IXR_RXTRIG    0x00000001 /* RX FIFO trigger interrupt */
0124 #define CDNS_UART_IXR_RXFULL    0x00000004 /* RX FIFO full interrupt. */
0125 #define CDNS_UART_IXR_RXEMPTY   0x00000002 /* RX FIFO empty interrupt. */
0126 #define CDNS_UART_IXR_RXMASK    0x000021e7 /* Valid RX bit mask */
0127 
0128     /*
0129      * Do not enable parity error interrupt for the following
0130      * reason: When parity error interrupt is enabled, each Rx
0131      * parity error always results in 2 events. The first one
0132      * being parity error interrupt and the second one with a
0133      * proper Rx interrupt with the incoming data.  Disabling
0134      * parity error interrupt ensures better handling of parity
0135      * error events. With this change, for a parity error case, we
0136      * get a Rx interrupt with parity error set in ISR register
0137      * and we still handle parity errors in the desired way.
0138      */
0139 
0140 #define CDNS_UART_RX_IRQS   (CDNS_UART_IXR_FRAMING | \
0141                  CDNS_UART_IXR_OVERRUN | \
0142                  CDNS_UART_IXR_RXTRIG |  \
0143                  CDNS_UART_IXR_TOUT)
0144 
0145 /* Goes in read_status_mask for break detection as the HW doesn't do it*/
0146 #define CDNS_UART_IXR_BRK   0x00002000
0147 
0148 #define CDNS_UART_RXBS_SUPPORT BIT(1)
0149 /*
0150  * Modem Control register:
0151  * The read/write Modem Control register controls the interface with the modem
0152  * or data set, or a peripheral device emulating a modem.
0153  */
0154 #define CDNS_UART_MODEMCR_FCM   0x00000020 /* Automatic flow control mode */
0155 #define CDNS_UART_MODEMCR_RTS   0x00000002 /* Request to send output control */
0156 #define CDNS_UART_MODEMCR_DTR   0x00000001 /* Data Terminal Ready */
0157 
0158 /*
0159  * Modem Status register:
0160  * The read/write Modem Status register reports the interface with the modem
0161  * or data set, or a peripheral device emulating a modem.
0162  */
0163 #define CDNS_UART_MODEMSR_DCD   BIT(7) /* Data Carrier Detect */
0164 #define CDNS_UART_MODEMSR_RI    BIT(6) /* Ting Indicator */
0165 #define CDNS_UART_MODEMSR_DSR   BIT(5) /* Data Set Ready */
0166 #define CDNS_UART_MODEMSR_CTS   BIT(4) /* Clear To Send */
0167 
0168 /*
0169  * Channel Status Register:
0170  * The channel status register (CSR) is provided to enable the control logic
0171  * to monitor the status of bits in the channel interrupt status register,
0172  * even if these are masked out by the interrupt mask register.
0173  */
0174 #define CDNS_UART_SR_RXEMPTY    0x00000002 /* RX FIFO empty */
0175 #define CDNS_UART_SR_TXEMPTY    0x00000008 /* TX FIFO empty */
0176 #define CDNS_UART_SR_TXFULL 0x00000010 /* TX FIFO full */
0177 #define CDNS_UART_SR_RXTRIG 0x00000001 /* Rx Trigger */
0178 #define CDNS_UART_SR_TACTIVE    0x00000800 /* TX state machine active */
0179 
0180 /* baud dividers min/max values */
0181 #define CDNS_UART_BDIV_MIN  4
0182 #define CDNS_UART_BDIV_MAX  255
0183 #define CDNS_UART_CD_MAX    65535
0184 #define UART_AUTOSUSPEND_TIMEOUT    3000
0185 
0186 /**
0187  * struct cdns_uart - device data
0188  * @port:       Pointer to the UART port
0189  * @uartclk:        Reference clock
0190  * @pclk:       APB clock
0191  * @cdns_uart_driver:   Pointer to UART driver
0192  * @baud:       Current baud rate
0193  * @clk_rate_change_nb: Notifier block for clock changes
0194  * @quirks:     Flags for RXBS support.
0195  * @cts_override:   Modem control state override
0196  */
0197 struct cdns_uart {
0198     struct uart_port    *port;
0199     struct clk      *uartclk;
0200     struct clk      *pclk;
0201     struct uart_driver  *cdns_uart_driver;
0202     unsigned int        baud;
0203     struct notifier_block   clk_rate_change_nb;
0204     u32         quirks;
0205     bool cts_override;
0206 };
0207 struct cdns_platform_data {
0208     u32 quirks;
0209 };
0210 #define to_cdns_uart(_nb) container_of(_nb, struct cdns_uart, \
0211         clk_rate_change_nb)
0212 
0213 /**
0214  * cdns_uart_handle_rx - Handle the received bytes along with Rx errors.
0215  * @dev_id: Id of the UART port
0216  * @isrstatus: The interrupt status register value as read
0217  * Return: None
0218  */
0219 static void cdns_uart_handle_rx(void *dev_id, unsigned int isrstatus)
0220 {
0221     struct uart_port *port = (struct uart_port *)dev_id;
0222     struct cdns_uart *cdns_uart = port->private_data;
0223     unsigned int data;
0224     unsigned int rxbs_status = 0;
0225     unsigned int status_mask;
0226     unsigned int framerrprocessed = 0;
0227     char status = TTY_NORMAL;
0228     bool is_rxbs_support;
0229 
0230     is_rxbs_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
0231 
0232     while ((readl(port->membase + CDNS_UART_SR) &
0233         CDNS_UART_SR_RXEMPTY) != CDNS_UART_SR_RXEMPTY) {
0234         if (is_rxbs_support)
0235             rxbs_status = readl(port->membase + CDNS_UART_RXBS);
0236         data = readl(port->membase + CDNS_UART_FIFO);
0237         port->icount.rx++;
0238         /*
0239          * There is no hardware break detection in Zynq, so we interpret
0240          * framing error with all-zeros data as a break sequence.
0241          * Most of the time, there's another non-zero byte at the
0242          * end of the sequence.
0243          */
0244         if (!is_rxbs_support && (isrstatus & CDNS_UART_IXR_FRAMING)) {
0245             if (!data) {
0246                 port->read_status_mask |= CDNS_UART_IXR_BRK;
0247                 framerrprocessed = 1;
0248                 continue;
0249             }
0250         }
0251         if (is_rxbs_support && (rxbs_status & CDNS_UART_RXBS_BRK)) {
0252             port->icount.brk++;
0253             status = TTY_BREAK;
0254             if (uart_handle_break(port))
0255                 continue;
0256         }
0257 
0258         isrstatus &= port->read_status_mask;
0259         isrstatus &= ~port->ignore_status_mask;
0260         status_mask = port->read_status_mask;
0261         status_mask &= ~port->ignore_status_mask;
0262 
0263         if (data &&
0264             (port->read_status_mask & CDNS_UART_IXR_BRK)) {
0265             port->read_status_mask &= ~CDNS_UART_IXR_BRK;
0266             port->icount.brk++;
0267             if (uart_handle_break(port))
0268                 continue;
0269         }
0270 
0271         if (uart_handle_sysrq_char(port, data))
0272             continue;
0273 
0274         if (is_rxbs_support) {
0275             if ((rxbs_status & CDNS_UART_RXBS_PARITY)
0276                 && (status_mask & CDNS_UART_IXR_PARITY)) {
0277                 port->icount.parity++;
0278                 status = TTY_PARITY;
0279             }
0280             if ((rxbs_status & CDNS_UART_RXBS_FRAMING)
0281                 && (status_mask & CDNS_UART_IXR_PARITY)) {
0282                 port->icount.frame++;
0283                 status = TTY_FRAME;
0284             }
0285         } else {
0286             if (isrstatus & CDNS_UART_IXR_PARITY) {
0287                 port->icount.parity++;
0288                 status = TTY_PARITY;
0289             }
0290             if ((isrstatus & CDNS_UART_IXR_FRAMING) &&
0291                 !framerrprocessed) {
0292                 port->icount.frame++;
0293                 status = TTY_FRAME;
0294             }
0295         }
0296         if (isrstatus & CDNS_UART_IXR_OVERRUN) {
0297             port->icount.overrun++;
0298             tty_insert_flip_char(&port->state->port, 0,
0299                          TTY_OVERRUN);
0300         }
0301         tty_insert_flip_char(&port->state->port, data, status);
0302         isrstatus = 0;
0303     }
0304 
0305     tty_flip_buffer_push(&port->state->port);
0306 }
0307 
0308 /**
0309  * cdns_uart_handle_tx - Handle the bytes to be Txed.
0310  * @dev_id: Id of the UART port
0311  * Return: None
0312  */
0313 static void cdns_uart_handle_tx(void *dev_id)
0314 {
0315     struct uart_port *port = (struct uart_port *)dev_id;
0316     struct circ_buf *xmit = &port->state->xmit;
0317     unsigned int numbytes;
0318 
0319     if (uart_circ_empty(xmit)) {
0320         writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IDR);
0321         return;
0322     }
0323 
0324     numbytes = port->fifosize;
0325     while (numbytes && !uart_circ_empty(xmit) &&
0326            !(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXFULL)) {
0327 
0328         writel(xmit->buf[xmit->tail], port->membase + CDNS_UART_FIFO);
0329 
0330         port->icount.tx++;
0331         xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
0332         numbytes--;
0333     }
0334 
0335     if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
0336         uart_write_wakeup(port);
0337 }
0338 
0339 /**
0340  * cdns_uart_isr - Interrupt handler
0341  * @irq: Irq number
0342  * @dev_id: Id of the port
0343  *
0344  * Return: IRQHANDLED
0345  */
0346 static irqreturn_t cdns_uart_isr(int irq, void *dev_id)
0347 {
0348     struct uart_port *port = (struct uart_port *)dev_id;
0349     unsigned int isrstatus;
0350 
0351     spin_lock(&port->lock);
0352 
0353     /* Read the interrupt status register to determine which
0354      * interrupt(s) is/are active and clear them.
0355      */
0356     isrstatus = readl(port->membase + CDNS_UART_ISR);
0357     writel(isrstatus, port->membase + CDNS_UART_ISR);
0358 
0359     if (isrstatus & CDNS_UART_IXR_TXEMPTY) {
0360         cdns_uart_handle_tx(dev_id);
0361         isrstatus &= ~CDNS_UART_IXR_TXEMPTY;
0362     }
0363 
0364     /*
0365      * Skip RX processing if RX is disabled as RXEMPTY will never be set
0366      * as read bytes will not be removed from the FIFO.
0367      */
0368     if (isrstatus & CDNS_UART_IXR_RXMASK &&
0369         !(readl(port->membase + CDNS_UART_CR) & CDNS_UART_CR_RX_DIS))
0370         cdns_uart_handle_rx(dev_id, isrstatus);
0371 
0372     spin_unlock(&port->lock);
0373     return IRQ_HANDLED;
0374 }
0375 
0376 /**
0377  * cdns_uart_calc_baud_divs - Calculate baud rate divisors
0378  * @clk: UART module input clock
0379  * @baud: Desired baud rate
0380  * @rbdiv: BDIV value (return value)
0381  * @rcd: CD value (return value)
0382  * @div8: Value for clk_sel bit in mod (return value)
0383  * Return: baud rate, requested baud when possible, or actual baud when there
0384  *  was too much error, zero if no valid divisors are found.
0385  *
0386  * Formula to obtain baud rate is
0387  *  baud_tx/rx rate = clk/CD * (BDIV + 1)
0388  *  input_clk = (Uart User Defined Clock or Apb Clock)
0389  *      depends on UCLKEN in MR Reg
0390  *  clk = input_clk or input_clk/8;
0391  *      depends on CLKS in MR reg
0392  *  CD and BDIV depends on values in
0393  *          baud rate generate register
0394  *          baud rate clock divisor register
0395  */
0396 static unsigned int cdns_uart_calc_baud_divs(unsigned int clk,
0397         unsigned int baud, u32 *rbdiv, u32 *rcd, int *div8)
0398 {
0399     u32 cd, bdiv;
0400     unsigned int calc_baud;
0401     unsigned int bestbaud = 0;
0402     unsigned int bauderror;
0403     unsigned int besterror = ~0;
0404 
0405     if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) {
0406         *div8 = 1;
0407         clk /= 8;
0408     } else {
0409         *div8 = 0;
0410     }
0411 
0412     for (bdiv = CDNS_UART_BDIV_MIN; bdiv <= CDNS_UART_BDIV_MAX; bdiv++) {
0413         cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
0414         if (cd < 1 || cd > CDNS_UART_CD_MAX)
0415             continue;
0416 
0417         calc_baud = clk / (cd * (bdiv + 1));
0418 
0419         if (baud > calc_baud)
0420             bauderror = baud - calc_baud;
0421         else
0422             bauderror = calc_baud - baud;
0423 
0424         if (besterror > bauderror) {
0425             *rbdiv = bdiv;
0426             *rcd = cd;
0427             bestbaud = calc_baud;
0428             besterror = bauderror;
0429         }
0430     }
0431     /* use the values when percent error is acceptable */
0432     if (((besterror * 100) / baud) < 3)
0433         bestbaud = baud;
0434 
0435     return bestbaud;
0436 }
0437 
0438 /**
0439  * cdns_uart_set_baud_rate - Calculate and set the baud rate
0440  * @port: Handle to the uart port structure
0441  * @baud: Baud rate to set
0442  * Return: baud rate, requested baud when possible, or actual baud when there
0443  *     was too much error, zero if no valid divisors are found.
0444  */
0445 static unsigned int cdns_uart_set_baud_rate(struct uart_port *port,
0446         unsigned int baud)
0447 {
0448     unsigned int calc_baud;
0449     u32 cd = 0, bdiv = 0;
0450     u32 mreg;
0451     int div8;
0452     struct cdns_uart *cdns_uart = port->private_data;
0453 
0454     calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
0455             &div8);
0456 
0457     /* Write new divisors to hardware */
0458     mreg = readl(port->membase + CDNS_UART_MR);
0459     if (div8)
0460         mreg |= CDNS_UART_MR_CLKSEL;
0461     else
0462         mreg &= ~CDNS_UART_MR_CLKSEL;
0463     writel(mreg, port->membase + CDNS_UART_MR);
0464     writel(cd, port->membase + CDNS_UART_BAUDGEN);
0465     writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
0466     cdns_uart->baud = baud;
0467 
0468     return calc_baud;
0469 }
0470 
0471 #ifdef CONFIG_COMMON_CLK
0472 /**
0473  * cdns_uart_clk_notifier_cb - Clock notifier callback
0474  * @nb:     Notifier block
0475  * @event:  Notify event
0476  * @data:   Notifier data
0477  * Return:  NOTIFY_OK or NOTIFY_DONE on success, NOTIFY_BAD on error.
0478  */
0479 static int cdns_uart_clk_notifier_cb(struct notifier_block *nb,
0480         unsigned long event, void *data)
0481 {
0482     u32 ctrl_reg;
0483     struct uart_port *port;
0484     int locked = 0;
0485     struct clk_notifier_data *ndata = data;
0486     struct cdns_uart *cdns_uart = to_cdns_uart(nb);
0487     unsigned long flags;
0488 
0489     port = cdns_uart->port;
0490     if (port->suspended)
0491         return NOTIFY_OK;
0492 
0493     switch (event) {
0494     case PRE_RATE_CHANGE:
0495     {
0496         u32 bdiv, cd;
0497         int div8;
0498 
0499         /*
0500          * Find out if current baud-rate can be achieved with new clock
0501          * frequency.
0502          */
0503         if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud,
0504                     &bdiv, &cd, &div8)) {
0505             dev_warn(port->dev, "clock rate change rejected\n");
0506             return NOTIFY_BAD;
0507         }
0508 
0509         spin_lock_irqsave(&cdns_uart->port->lock, flags);
0510 
0511         /* Disable the TX and RX to set baud rate */
0512         ctrl_reg = readl(port->membase + CDNS_UART_CR);
0513         ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
0514         writel(ctrl_reg, port->membase + CDNS_UART_CR);
0515 
0516         spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
0517 
0518         return NOTIFY_OK;
0519     }
0520     case POST_RATE_CHANGE:
0521         /*
0522          * Set clk dividers to generate correct baud with new clock
0523          * frequency.
0524          */
0525 
0526         spin_lock_irqsave(&cdns_uart->port->lock, flags);
0527 
0528         locked = 1;
0529         port->uartclk = ndata->new_rate;
0530 
0531         cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port,
0532                 cdns_uart->baud);
0533         fallthrough;
0534     case ABORT_RATE_CHANGE:
0535         if (!locked)
0536             spin_lock_irqsave(&cdns_uart->port->lock, flags);
0537 
0538         /* Set TX/RX Reset */
0539         ctrl_reg = readl(port->membase + CDNS_UART_CR);
0540         ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
0541         writel(ctrl_reg, port->membase + CDNS_UART_CR);
0542 
0543         while (readl(port->membase + CDNS_UART_CR) &
0544                 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
0545             cpu_relax();
0546 
0547         /*
0548          * Clear the RX disable and TX disable bits and then set the TX
0549          * enable bit and RX enable bit to enable the transmitter and
0550          * receiver.
0551          */
0552         writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
0553         ctrl_reg = readl(port->membase + CDNS_UART_CR);
0554         ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
0555         ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
0556         writel(ctrl_reg, port->membase + CDNS_UART_CR);
0557 
0558         spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
0559 
0560         return NOTIFY_OK;
0561     default:
0562         return NOTIFY_DONE;
0563     }
0564 }
0565 #endif
0566 
0567 /**
0568  * cdns_uart_start_tx -  Start transmitting bytes
0569  * @port: Handle to the uart port structure
0570  */
0571 static void cdns_uart_start_tx(struct uart_port *port)
0572 {
0573     unsigned int status;
0574 
0575     if (uart_tx_stopped(port))
0576         return;
0577 
0578     /*
0579      * Set the TX enable bit and clear the TX disable bit to enable the
0580      * transmitter.
0581      */
0582     status = readl(port->membase + CDNS_UART_CR);
0583     status &= ~CDNS_UART_CR_TX_DIS;
0584     status |= CDNS_UART_CR_TX_EN;
0585     writel(status, port->membase + CDNS_UART_CR);
0586 
0587     if (uart_circ_empty(&port->state->xmit))
0588         return;
0589 
0590     writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_ISR);
0591 
0592     cdns_uart_handle_tx(port);
0593 
0594     /* Enable the TX Empty interrupt */
0595     writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IER);
0596 }
0597 
0598 /**
0599  * cdns_uart_stop_tx - Stop TX
0600  * @port: Handle to the uart port structure
0601  */
0602 static void cdns_uart_stop_tx(struct uart_port *port)
0603 {
0604     unsigned int regval;
0605 
0606     regval = readl(port->membase + CDNS_UART_CR);
0607     regval |= CDNS_UART_CR_TX_DIS;
0608     /* Disable the transmitter */
0609     writel(regval, port->membase + CDNS_UART_CR);
0610 }
0611 
0612 /**
0613  * cdns_uart_stop_rx - Stop RX
0614  * @port: Handle to the uart port structure
0615  */
0616 static void cdns_uart_stop_rx(struct uart_port *port)
0617 {
0618     unsigned int regval;
0619 
0620     /* Disable RX IRQs */
0621     writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IDR);
0622 
0623     /* Disable the receiver */
0624     regval = readl(port->membase + CDNS_UART_CR);
0625     regval |= CDNS_UART_CR_RX_DIS;
0626     writel(regval, port->membase + CDNS_UART_CR);
0627 }
0628 
0629 /**
0630  * cdns_uart_tx_empty -  Check whether TX is empty
0631  * @port: Handle to the uart port structure
0632  *
0633  * Return: TIOCSER_TEMT on success, 0 otherwise
0634  */
0635 static unsigned int cdns_uart_tx_empty(struct uart_port *port)
0636 {
0637     unsigned int status;
0638 
0639     status = readl(port->membase + CDNS_UART_SR) &
0640                (CDNS_UART_SR_TXEMPTY | CDNS_UART_SR_TACTIVE);
0641     return (status == CDNS_UART_SR_TXEMPTY) ? TIOCSER_TEMT : 0;
0642 }
0643 
0644 /**
0645  * cdns_uart_break_ctl - Based on the input ctl we have to start or stop
0646  *          transmitting char breaks
0647  * @port: Handle to the uart port structure
0648  * @ctl: Value based on which start or stop decision is taken
0649  */
0650 static void cdns_uart_break_ctl(struct uart_port *port, int ctl)
0651 {
0652     unsigned int status;
0653     unsigned long flags;
0654 
0655     spin_lock_irqsave(&port->lock, flags);
0656 
0657     status = readl(port->membase + CDNS_UART_CR);
0658 
0659     if (ctl == -1)
0660         writel(CDNS_UART_CR_STARTBRK | status,
0661                 port->membase + CDNS_UART_CR);
0662     else {
0663         if ((status & CDNS_UART_CR_STOPBRK) == 0)
0664             writel(CDNS_UART_CR_STOPBRK | status,
0665                     port->membase + CDNS_UART_CR);
0666     }
0667     spin_unlock_irqrestore(&port->lock, flags);
0668 }
0669 
0670 /**
0671  * cdns_uart_set_termios - termios operations, handling data length, parity,
0672  *              stop bits, flow control, baud rate
0673  * @port: Handle to the uart port structure
0674  * @termios: Handle to the input termios structure
0675  * @old: Values of the previously saved termios structure
0676  */
0677 static void cdns_uart_set_termios(struct uart_port *port,
0678                 struct ktermios *termios, struct ktermios *old)
0679 {
0680     u32 cval = 0;
0681     unsigned int baud, minbaud, maxbaud;
0682     unsigned long flags;
0683     unsigned int ctrl_reg, mode_reg;
0684 
0685     spin_lock_irqsave(&port->lock, flags);
0686 
0687     /* Disable the TX and RX to set baud rate */
0688     ctrl_reg = readl(port->membase + CDNS_UART_CR);
0689     ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
0690     writel(ctrl_reg, port->membase + CDNS_UART_CR);
0691 
0692     /*
0693      * Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk
0694      * min and max baud should be calculated here based on port->uartclk.
0695      * this way we get a valid baud and can safely call set_baud()
0696      */
0697     minbaud = port->uartclk /
0698             ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8);
0699     maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1);
0700     baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
0701     baud = cdns_uart_set_baud_rate(port, baud);
0702     if (tty_termios_baud_rate(termios))
0703         tty_termios_encode_baud_rate(termios, baud, baud);
0704 
0705     /* Update the per-port timeout. */
0706     uart_update_timeout(port, termios->c_cflag, baud);
0707 
0708     /* Set TX/RX Reset */
0709     ctrl_reg = readl(port->membase + CDNS_UART_CR);
0710     ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
0711     writel(ctrl_reg, port->membase + CDNS_UART_CR);
0712 
0713     while (readl(port->membase + CDNS_UART_CR) &
0714         (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
0715         cpu_relax();
0716 
0717     /*
0718      * Clear the RX disable and TX disable bits and then set the TX enable
0719      * bit and RX enable bit to enable the transmitter and receiver.
0720      */
0721     ctrl_reg = readl(port->membase + CDNS_UART_CR);
0722     ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
0723     ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
0724     writel(ctrl_reg, port->membase + CDNS_UART_CR);
0725 
0726     writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
0727 
0728     port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
0729             CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
0730     port->ignore_status_mask = 0;
0731 
0732     if (termios->c_iflag & INPCK)
0733         port->read_status_mask |= CDNS_UART_IXR_PARITY |
0734         CDNS_UART_IXR_FRAMING;
0735 
0736     if (termios->c_iflag & IGNPAR)
0737         port->ignore_status_mask |= CDNS_UART_IXR_PARITY |
0738             CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
0739 
0740     /* ignore all characters if CREAD is not set */
0741     if ((termios->c_cflag & CREAD) == 0)
0742         port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG |
0743             CDNS_UART_IXR_TOUT | CDNS_UART_IXR_PARITY |
0744             CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
0745 
0746     mode_reg = readl(port->membase + CDNS_UART_MR);
0747 
0748     /* Handling Data Size */
0749     switch (termios->c_cflag & CSIZE) {
0750     case CS6:
0751         cval |= CDNS_UART_MR_CHARLEN_6_BIT;
0752         break;
0753     case CS7:
0754         cval |= CDNS_UART_MR_CHARLEN_7_BIT;
0755         break;
0756     default:
0757     case CS8:
0758         cval |= CDNS_UART_MR_CHARLEN_8_BIT;
0759         termios->c_cflag &= ~CSIZE;
0760         termios->c_cflag |= CS8;
0761         break;
0762     }
0763 
0764     /* Handling Parity and Stop Bits length */
0765     if (termios->c_cflag & CSTOPB)
0766         cval |= CDNS_UART_MR_STOPMODE_2_BIT; /* 2 STOP bits */
0767     else
0768         cval |= CDNS_UART_MR_STOPMODE_1_BIT; /* 1 STOP bit */
0769 
0770     if (termios->c_cflag & PARENB) {
0771         /* Mark or Space parity */
0772         if (termios->c_cflag & CMSPAR) {
0773             if (termios->c_cflag & PARODD)
0774                 cval |= CDNS_UART_MR_PARITY_MARK;
0775             else
0776                 cval |= CDNS_UART_MR_PARITY_SPACE;
0777         } else {
0778             if (termios->c_cflag & PARODD)
0779                 cval |= CDNS_UART_MR_PARITY_ODD;
0780             else
0781                 cval |= CDNS_UART_MR_PARITY_EVEN;
0782         }
0783     } else {
0784         cval |= CDNS_UART_MR_PARITY_NONE;
0785     }
0786     cval |= mode_reg & 1;
0787     writel(cval, port->membase + CDNS_UART_MR);
0788 
0789     cval = readl(port->membase + CDNS_UART_MODEMCR);
0790     if (termios->c_cflag & CRTSCTS)
0791         cval |= CDNS_UART_MODEMCR_FCM;
0792     else
0793         cval &= ~CDNS_UART_MODEMCR_FCM;
0794     writel(cval, port->membase + CDNS_UART_MODEMCR);
0795 
0796     spin_unlock_irqrestore(&port->lock, flags);
0797 }
0798 
0799 /**
0800  * cdns_uart_startup - Called when an application opens a cdns_uart port
0801  * @port: Handle to the uart port structure
0802  *
0803  * Return: 0 on success, negative errno otherwise
0804  */
0805 static int cdns_uart_startup(struct uart_port *port)
0806 {
0807     struct cdns_uart *cdns_uart = port->private_data;
0808     bool is_brk_support;
0809     int ret;
0810     unsigned long flags;
0811     unsigned int status = 0;
0812 
0813     is_brk_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
0814 
0815     spin_lock_irqsave(&port->lock, flags);
0816 
0817     /* Disable the TX and RX */
0818     writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
0819             port->membase + CDNS_UART_CR);
0820 
0821     /* Set the Control Register with TX/RX Enable, TX/RX Reset,
0822      * no break chars.
0823      */
0824     writel(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST,
0825             port->membase + CDNS_UART_CR);
0826 
0827     while (readl(port->membase + CDNS_UART_CR) &
0828         (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
0829         cpu_relax();
0830 
0831     /*
0832      * Clear the RX disable bit and then set the RX enable bit to enable
0833      * the receiver.
0834      */
0835     status = readl(port->membase + CDNS_UART_CR);
0836     status &= ~CDNS_UART_CR_RX_DIS;
0837     status |= CDNS_UART_CR_RX_EN;
0838     writel(status, port->membase + CDNS_UART_CR);
0839 
0840     /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
0841      * no parity.
0842      */
0843     writel(CDNS_UART_MR_CHMODE_NORM | CDNS_UART_MR_STOPMODE_1_BIT
0844         | CDNS_UART_MR_PARITY_NONE | CDNS_UART_MR_CHARLEN_8_BIT,
0845         port->membase + CDNS_UART_MR);
0846 
0847     /*
0848      * Set the RX FIFO Trigger level to use most of the FIFO, but it
0849      * can be tuned with a module parameter
0850      */
0851     writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
0852 
0853     /*
0854      * Receive Timeout register is enabled but it
0855      * can be tuned with a module parameter
0856      */
0857     writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
0858 
0859     /* Clear out any pending interrupts before enabling them */
0860     writel(readl(port->membase + CDNS_UART_ISR),
0861             port->membase + CDNS_UART_ISR);
0862 
0863     spin_unlock_irqrestore(&port->lock, flags);
0864 
0865     ret = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME, port);
0866     if (ret) {
0867         dev_err(port->dev, "request_irq '%d' failed with %d\n",
0868             port->irq, ret);
0869         return ret;
0870     }
0871 
0872     /* Set the Interrupt Registers with desired interrupts */
0873     if (is_brk_support)
0874         writel(CDNS_UART_RX_IRQS | CDNS_UART_IXR_BRK,
0875                     port->membase + CDNS_UART_IER);
0876     else
0877         writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IER);
0878 
0879     return 0;
0880 }
0881 
0882 /**
0883  * cdns_uart_shutdown - Called when an application closes a cdns_uart port
0884  * @port: Handle to the uart port structure
0885  */
0886 static void cdns_uart_shutdown(struct uart_port *port)
0887 {
0888     int status;
0889     unsigned long flags;
0890 
0891     spin_lock_irqsave(&port->lock, flags);
0892 
0893     /* Disable interrupts */
0894     status = readl(port->membase + CDNS_UART_IMR);
0895     writel(status, port->membase + CDNS_UART_IDR);
0896     writel(0xffffffff, port->membase + CDNS_UART_ISR);
0897 
0898     /* Disable the TX and RX */
0899     writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
0900             port->membase + CDNS_UART_CR);
0901 
0902     spin_unlock_irqrestore(&port->lock, flags);
0903 
0904     free_irq(port->irq, port);
0905 }
0906 
0907 /**
0908  * cdns_uart_type - Set UART type to cdns_uart port
0909  * @port: Handle to the uart port structure
0910  *
0911  * Return: string on success, NULL otherwise
0912  */
0913 static const char *cdns_uart_type(struct uart_port *port)
0914 {
0915     return port->type == PORT_XUARTPS ? CDNS_UART_NAME : NULL;
0916 }
0917 
0918 /**
0919  * cdns_uart_verify_port - Verify the port params
0920  * @port: Handle to the uart port structure
0921  * @ser: Handle to the structure whose members are compared
0922  *
0923  * Return: 0 on success, negative errno otherwise.
0924  */
0925 static int cdns_uart_verify_port(struct uart_port *port,
0926                     struct serial_struct *ser)
0927 {
0928     if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
0929         return -EINVAL;
0930     if (port->irq != ser->irq)
0931         return -EINVAL;
0932     if (ser->io_type != UPIO_MEM)
0933         return -EINVAL;
0934     if (port->iobase != ser->port)
0935         return -EINVAL;
0936     if (ser->hub6 != 0)
0937         return -EINVAL;
0938     return 0;
0939 }
0940 
0941 /**
0942  * cdns_uart_request_port - Claim the memory region attached to cdns_uart port,
0943  *              called when the driver adds a cdns_uart port via
0944  *              uart_add_one_port()
0945  * @port: Handle to the uart port structure
0946  *
0947  * Return: 0 on success, negative errno otherwise.
0948  */
0949 static int cdns_uart_request_port(struct uart_port *port)
0950 {
0951     if (!request_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE,
0952                      CDNS_UART_NAME)) {
0953         return -ENOMEM;
0954     }
0955 
0956     port->membase = ioremap(port->mapbase, CDNS_UART_REGISTER_SPACE);
0957     if (!port->membase) {
0958         dev_err(port->dev, "Unable to map registers\n");
0959         release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
0960         return -ENOMEM;
0961     }
0962     return 0;
0963 }
0964 
0965 /**
0966  * cdns_uart_release_port - Release UART port
0967  * @port: Handle to the uart port structure
0968  *
0969  * Release the memory region attached to a cdns_uart port. Called when the
0970  * driver removes a cdns_uart port via uart_remove_one_port().
0971  */
0972 static void cdns_uart_release_port(struct uart_port *port)
0973 {
0974     release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
0975     iounmap(port->membase);
0976     port->membase = NULL;
0977 }
0978 
0979 /**
0980  * cdns_uart_config_port - Configure UART port
0981  * @port: Handle to the uart port structure
0982  * @flags: If any
0983  */
0984 static void cdns_uart_config_port(struct uart_port *port, int flags)
0985 {
0986     if (flags & UART_CONFIG_TYPE && cdns_uart_request_port(port) == 0)
0987         port->type = PORT_XUARTPS;
0988 }
0989 
0990 /**
0991  * cdns_uart_get_mctrl - Get the modem control state
0992  * @port: Handle to the uart port structure
0993  *
0994  * Return: the modem control state
0995  */
0996 static unsigned int cdns_uart_get_mctrl(struct uart_port *port)
0997 {
0998     u32 val;
0999     unsigned int mctrl = 0;
1000     struct cdns_uart *cdns_uart_data = port->private_data;
1001 
1002     if (cdns_uart_data->cts_override)
1003         return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
1004 
1005     val = readl(port->membase + CDNS_UART_MODEMSR);
1006     if (val & CDNS_UART_MODEMSR_CTS)
1007         mctrl |= TIOCM_CTS;
1008     if (val & CDNS_UART_MODEMSR_DSR)
1009         mctrl |= TIOCM_DSR;
1010     if (val & CDNS_UART_MODEMSR_RI)
1011         mctrl |= TIOCM_RNG;
1012     if (val & CDNS_UART_MODEMSR_DCD)
1013         mctrl |= TIOCM_CAR;
1014 
1015     return mctrl;
1016 }
1017 
1018 static void cdns_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1019 {
1020     u32 val;
1021     u32 mode_reg;
1022     struct cdns_uart *cdns_uart_data = port->private_data;
1023 
1024     if (cdns_uart_data->cts_override)
1025         return;
1026 
1027     val = readl(port->membase + CDNS_UART_MODEMCR);
1028     mode_reg = readl(port->membase + CDNS_UART_MR);
1029 
1030     val &= ~(CDNS_UART_MODEMCR_RTS | CDNS_UART_MODEMCR_DTR);
1031     mode_reg &= ~CDNS_UART_MR_CHMODE_MASK;
1032 
1033     if (mctrl & TIOCM_RTS)
1034         val |= CDNS_UART_MODEMCR_RTS;
1035     if (mctrl & TIOCM_DTR)
1036         val |= CDNS_UART_MODEMCR_DTR;
1037     if (mctrl & TIOCM_LOOP)
1038         mode_reg |= CDNS_UART_MR_CHMODE_L_LOOP;
1039     else
1040         mode_reg |= CDNS_UART_MR_CHMODE_NORM;
1041 
1042     writel(val, port->membase + CDNS_UART_MODEMCR);
1043     writel(mode_reg, port->membase + CDNS_UART_MR);
1044 }
1045 
1046 #ifdef CONFIG_CONSOLE_POLL
1047 static int cdns_uart_poll_get_char(struct uart_port *port)
1048 {
1049     int c;
1050     unsigned long flags;
1051 
1052     spin_lock_irqsave(&port->lock, flags);
1053 
1054     /* Check if FIFO is empty */
1055     if (readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_RXEMPTY)
1056         c = NO_POLL_CHAR;
1057     else /* Read a character */
1058         c = (unsigned char) readl(port->membase + CDNS_UART_FIFO);
1059 
1060     spin_unlock_irqrestore(&port->lock, flags);
1061 
1062     return c;
1063 }
1064 
1065 static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
1066 {
1067     unsigned long flags;
1068 
1069     spin_lock_irqsave(&port->lock, flags);
1070 
1071     /* Wait until FIFO is empty */
1072     while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
1073         cpu_relax();
1074 
1075     /* Write a character */
1076     writel(c, port->membase + CDNS_UART_FIFO);
1077 
1078     /* Wait until FIFO is empty */
1079     while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
1080         cpu_relax();
1081 
1082     spin_unlock_irqrestore(&port->lock, flags);
1083 }
1084 #endif
1085 
1086 static void cdns_uart_pm(struct uart_port *port, unsigned int state,
1087            unsigned int oldstate)
1088 {
1089     switch (state) {
1090     case UART_PM_STATE_OFF:
1091         pm_runtime_mark_last_busy(port->dev);
1092         pm_runtime_put_autosuspend(port->dev);
1093         break;
1094     default:
1095         pm_runtime_get_sync(port->dev);
1096         break;
1097     }
1098 }
1099 
1100 static const struct uart_ops cdns_uart_ops = {
1101     .set_mctrl  = cdns_uart_set_mctrl,
1102     .get_mctrl  = cdns_uart_get_mctrl,
1103     .start_tx   = cdns_uart_start_tx,
1104     .stop_tx    = cdns_uart_stop_tx,
1105     .stop_rx    = cdns_uart_stop_rx,
1106     .tx_empty   = cdns_uart_tx_empty,
1107     .break_ctl  = cdns_uart_break_ctl,
1108     .set_termios    = cdns_uart_set_termios,
1109     .startup    = cdns_uart_startup,
1110     .shutdown   = cdns_uart_shutdown,
1111     .pm     = cdns_uart_pm,
1112     .type       = cdns_uart_type,
1113     .verify_port    = cdns_uart_verify_port,
1114     .request_port   = cdns_uart_request_port,
1115     .release_port   = cdns_uart_release_port,
1116     .config_port    = cdns_uart_config_port,
1117 #ifdef CONFIG_CONSOLE_POLL
1118     .poll_get_char  = cdns_uart_poll_get_char,
1119     .poll_put_char  = cdns_uart_poll_put_char,
1120 #endif
1121 };
1122 
1123 static struct uart_driver cdns_uart_uart_driver;
1124 
1125 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1126 /**
1127  * cdns_uart_console_putchar - write the character to the FIFO buffer
1128  * @port: Handle to the uart port structure
1129  * @ch: Character to be written
1130  */
1131 static void cdns_uart_console_putchar(struct uart_port *port, unsigned char ch)
1132 {
1133     while (readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXFULL)
1134         cpu_relax();
1135     writel(ch, port->membase + CDNS_UART_FIFO);
1136 }
1137 
1138 static void cdns_early_write(struct console *con, const char *s,
1139                     unsigned int n)
1140 {
1141     struct earlycon_device *dev = con->data;
1142 
1143     uart_console_write(&dev->port, s, n, cdns_uart_console_putchar);
1144 }
1145 
1146 static int __init cdns_early_console_setup(struct earlycon_device *device,
1147                        const char *opt)
1148 {
1149     struct uart_port *port = &device->port;
1150 
1151     if (!port->membase)
1152         return -ENODEV;
1153 
1154     /* initialise control register */
1155     writel(CDNS_UART_CR_TX_EN|CDNS_UART_CR_TXRST|CDNS_UART_CR_RXRST,
1156            port->membase + CDNS_UART_CR);
1157 
1158     /* only set baud if specified on command line - otherwise
1159      * assume it has been initialized by a boot loader.
1160      */
1161     if (port->uartclk && device->baud) {
1162         u32 cd = 0, bdiv = 0;
1163         u32 mr;
1164         int div8;
1165 
1166         cdns_uart_calc_baud_divs(port->uartclk, device->baud,
1167                      &bdiv, &cd, &div8);
1168         mr = CDNS_UART_MR_PARITY_NONE;
1169         if (div8)
1170             mr |= CDNS_UART_MR_CLKSEL;
1171 
1172         writel(mr,   port->membase + CDNS_UART_MR);
1173         writel(cd,   port->membase + CDNS_UART_BAUDGEN);
1174         writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
1175     }
1176 
1177     device->con->write = cdns_early_write;
1178 
1179     return 0;
1180 }
1181 OF_EARLYCON_DECLARE(cdns, "xlnx,xuartps", cdns_early_console_setup);
1182 OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p8", cdns_early_console_setup);
1183 OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p12", cdns_early_console_setup);
1184 OF_EARLYCON_DECLARE(cdns, "xlnx,zynqmp-uart", cdns_early_console_setup);
1185 
1186 
1187 /* Static pointer to console port */
1188 static struct uart_port *console_port;
1189 
1190 /**
1191  * cdns_uart_console_write - perform write operation
1192  * @co: Console handle
1193  * @s: Pointer to character array
1194  * @count: No of characters
1195  */
1196 static void cdns_uart_console_write(struct console *co, const char *s,
1197                 unsigned int count)
1198 {
1199     struct uart_port *port = console_port;
1200     unsigned long flags;
1201     unsigned int imr, ctrl;
1202     int locked = 1;
1203 
1204     if (port->sysrq)
1205         locked = 0;
1206     else if (oops_in_progress)
1207         locked = spin_trylock_irqsave(&port->lock, flags);
1208     else
1209         spin_lock_irqsave(&port->lock, flags);
1210 
1211     /* save and disable interrupt */
1212     imr = readl(port->membase + CDNS_UART_IMR);
1213     writel(imr, port->membase + CDNS_UART_IDR);
1214 
1215     /*
1216      * Make sure that the tx part is enabled. Set the TX enable bit and
1217      * clear the TX disable bit to enable the transmitter.
1218      */
1219     ctrl = readl(port->membase + CDNS_UART_CR);
1220     ctrl &= ~CDNS_UART_CR_TX_DIS;
1221     ctrl |= CDNS_UART_CR_TX_EN;
1222     writel(ctrl, port->membase + CDNS_UART_CR);
1223 
1224     uart_console_write(port, s, count, cdns_uart_console_putchar);
1225     while (cdns_uart_tx_empty(port) != TIOCSER_TEMT)
1226         cpu_relax();
1227 
1228     /* restore interrupt state */
1229     writel(imr, port->membase + CDNS_UART_IER);
1230 
1231     if (locked)
1232         spin_unlock_irqrestore(&port->lock, flags);
1233 }
1234 
1235 /**
1236  * cdns_uart_console_setup - Initialize the uart to default config
1237  * @co: Console handle
1238  * @options: Initial settings of uart
1239  *
1240  * Return: 0 on success, negative errno otherwise.
1241  */
1242 static int cdns_uart_console_setup(struct console *co, char *options)
1243 {
1244     struct uart_port *port = console_port;
1245 
1246     int baud = 9600;
1247     int bits = 8;
1248     int parity = 'n';
1249     int flow = 'n';
1250     unsigned long time_out;
1251 
1252     if (!port->membase) {
1253         pr_debug("console on " CDNS_UART_TTY_NAME "%i not present\n",
1254              co->index);
1255         return -ENODEV;
1256     }
1257 
1258     if (options)
1259         uart_parse_options(options, &baud, &parity, &bits, &flow);
1260 
1261     /* Wait for tx_empty before setting up the console */
1262     time_out = jiffies + usecs_to_jiffies(TX_TIMEOUT);
1263 
1264     while (time_before(jiffies, time_out) &&
1265            cdns_uart_tx_empty(port) != TIOCSER_TEMT)
1266         cpu_relax();
1267 
1268     return uart_set_options(port, co, baud, parity, bits, flow);
1269 }
1270 
1271 static struct console cdns_uart_console = {
1272     .name   = CDNS_UART_TTY_NAME,
1273     .write  = cdns_uart_console_write,
1274     .device = uart_console_device,
1275     .setup  = cdns_uart_console_setup,
1276     .flags  = CON_PRINTBUFFER,
1277     .index  = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
1278     .data   = &cdns_uart_uart_driver,
1279 };
1280 #endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
1281 
1282 #ifdef CONFIG_PM_SLEEP
1283 /**
1284  * cdns_uart_suspend - suspend event
1285  * @device: Pointer to the device structure
1286  *
1287  * Return: 0
1288  */
1289 static int cdns_uart_suspend(struct device *device)
1290 {
1291     struct uart_port *port = dev_get_drvdata(device);
1292     struct cdns_uart *cdns_uart = port->private_data;
1293     int may_wake;
1294 
1295     may_wake = device_may_wakeup(device);
1296 
1297     if (console_suspend_enabled && uart_console(port) && may_wake) {
1298         unsigned long flags;
1299 
1300         spin_lock_irqsave(&port->lock, flags);
1301         /* Empty the receive FIFO 1st before making changes */
1302         while (!(readl(port->membase + CDNS_UART_SR) &
1303                     CDNS_UART_SR_RXEMPTY))
1304             readl(port->membase + CDNS_UART_FIFO);
1305         /* set RX trigger level to 1 */
1306         writel(1, port->membase + CDNS_UART_RXWM);
1307         /* disable RX timeout interrups */
1308         writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IDR);
1309         spin_unlock_irqrestore(&port->lock, flags);
1310     }
1311 
1312     /*
1313      * Call the API provided in serial_core.c file which handles
1314      * the suspend.
1315      */
1316     return uart_suspend_port(cdns_uart->cdns_uart_driver, port);
1317 }
1318 
1319 /**
1320  * cdns_uart_resume - Resume after a previous suspend
1321  * @device: Pointer to the device structure
1322  *
1323  * Return: 0
1324  */
1325 static int cdns_uart_resume(struct device *device)
1326 {
1327     struct uart_port *port = dev_get_drvdata(device);
1328     struct cdns_uart *cdns_uart = port->private_data;
1329     unsigned long flags;
1330     u32 ctrl_reg;
1331     int may_wake;
1332 
1333     may_wake = device_may_wakeup(device);
1334 
1335     if (console_suspend_enabled && uart_console(port) && !may_wake) {
1336         clk_enable(cdns_uart->pclk);
1337         clk_enable(cdns_uart->uartclk);
1338 
1339         spin_lock_irqsave(&port->lock, flags);
1340 
1341         /* Set TX/RX Reset */
1342         ctrl_reg = readl(port->membase + CDNS_UART_CR);
1343         ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
1344         writel(ctrl_reg, port->membase + CDNS_UART_CR);
1345         while (readl(port->membase + CDNS_UART_CR) &
1346                 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
1347             cpu_relax();
1348 
1349         /* restore rx timeout value */
1350         writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
1351         /* Enable Tx/Rx */
1352         ctrl_reg = readl(port->membase + CDNS_UART_CR);
1353         ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
1354         ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
1355         writel(ctrl_reg, port->membase + CDNS_UART_CR);
1356 
1357         clk_disable(cdns_uart->uartclk);
1358         clk_disable(cdns_uart->pclk);
1359         spin_unlock_irqrestore(&port->lock, flags);
1360     } else {
1361         spin_lock_irqsave(&port->lock, flags);
1362         /* restore original rx trigger level */
1363         writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
1364         /* enable RX timeout interrupt */
1365         writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IER);
1366         spin_unlock_irqrestore(&port->lock, flags);
1367     }
1368 
1369     return uart_resume_port(cdns_uart->cdns_uart_driver, port);
1370 }
1371 #endif /* ! CONFIG_PM_SLEEP */
1372 static int __maybe_unused cdns_runtime_suspend(struct device *dev)
1373 {
1374     struct uart_port *port = dev_get_drvdata(dev);
1375     struct cdns_uart *cdns_uart = port->private_data;
1376 
1377     clk_disable(cdns_uart->uartclk);
1378     clk_disable(cdns_uart->pclk);
1379     return 0;
1380 };
1381 
1382 static int __maybe_unused cdns_runtime_resume(struct device *dev)
1383 {
1384     struct uart_port *port = dev_get_drvdata(dev);
1385     struct cdns_uart *cdns_uart = port->private_data;
1386 
1387     clk_enable(cdns_uart->pclk);
1388     clk_enable(cdns_uart->uartclk);
1389     return 0;
1390 };
1391 
1392 static const struct dev_pm_ops cdns_uart_dev_pm_ops = {
1393     SET_SYSTEM_SLEEP_PM_OPS(cdns_uart_suspend, cdns_uart_resume)
1394     SET_RUNTIME_PM_OPS(cdns_runtime_suspend,
1395                cdns_runtime_resume, NULL)
1396 };
1397 
1398 static const struct cdns_platform_data zynqmp_uart_def = {
1399                 .quirks = CDNS_UART_RXBS_SUPPORT, };
1400 
1401 /* Match table for of_platform binding */
1402 static const struct of_device_id cdns_uart_of_match[] = {
1403     { .compatible = "xlnx,xuartps", },
1404     { .compatible = "cdns,uart-r1p8", },
1405     { .compatible = "cdns,uart-r1p12", .data = &zynqmp_uart_def },
1406     { .compatible = "xlnx,zynqmp-uart", .data = &zynqmp_uart_def },
1407     {}
1408 };
1409 MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
1410 
1411 /* Temporary variable for storing number of instances */
1412 static int instances;
1413 
1414 /**
1415  * cdns_uart_probe - Platform driver probe
1416  * @pdev: Pointer to the platform device structure
1417  *
1418  * Return: 0 on success, negative errno otherwise
1419  */
1420 static int cdns_uart_probe(struct platform_device *pdev)
1421 {
1422     int rc, id, irq;
1423     struct uart_port *port;
1424     struct resource *res;
1425     struct cdns_uart *cdns_uart_data;
1426     const struct of_device_id *match;
1427 
1428     cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
1429             GFP_KERNEL);
1430     if (!cdns_uart_data)
1431         return -ENOMEM;
1432     port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
1433     if (!port)
1434         return -ENOMEM;
1435 
1436     /* Look for a serialN alias */
1437     id = of_alias_get_id(pdev->dev.of_node, "serial");
1438     if (id < 0)
1439         id = 0;
1440 
1441     if (id >= CDNS_UART_NR_PORTS) {
1442         dev_err(&pdev->dev, "Cannot get uart_port structure\n");
1443         return -ENODEV;
1444     }
1445 
1446     if (!cdns_uart_uart_driver.state) {
1447         cdns_uart_uart_driver.owner = THIS_MODULE;
1448         cdns_uart_uart_driver.driver_name = CDNS_UART_NAME;
1449         cdns_uart_uart_driver.dev_name = CDNS_UART_TTY_NAME;
1450         cdns_uart_uart_driver.major = CDNS_UART_MAJOR;
1451         cdns_uart_uart_driver.minor = CDNS_UART_MINOR;
1452         cdns_uart_uart_driver.nr = CDNS_UART_NR_PORTS;
1453 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1454         cdns_uart_uart_driver.cons = &cdns_uart_console;
1455 #endif
1456 
1457         rc = uart_register_driver(&cdns_uart_uart_driver);
1458         if (rc < 0) {
1459             dev_err(&pdev->dev, "Failed to register driver\n");
1460             return rc;
1461         }
1462     }
1463 
1464     cdns_uart_data->cdns_uart_driver = &cdns_uart_uart_driver;
1465 
1466     match = of_match_node(cdns_uart_of_match, pdev->dev.of_node);
1467     if (match && match->data) {
1468         const struct cdns_platform_data *data = match->data;
1469 
1470         cdns_uart_data->quirks = data->quirks;
1471     }
1472 
1473     cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk");
1474     if (PTR_ERR(cdns_uart_data->pclk) == -EPROBE_DEFER) {
1475         rc = PTR_ERR(cdns_uart_data->pclk);
1476         goto err_out_unregister_driver;
1477     }
1478 
1479     if (IS_ERR(cdns_uart_data->pclk)) {
1480         cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk");
1481         if (IS_ERR(cdns_uart_data->pclk)) {
1482             rc = PTR_ERR(cdns_uart_data->pclk);
1483             goto err_out_unregister_driver;
1484         }
1485         dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n");
1486     }
1487 
1488     cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk");
1489     if (PTR_ERR(cdns_uart_data->uartclk) == -EPROBE_DEFER) {
1490         rc = PTR_ERR(cdns_uart_data->uartclk);
1491         goto err_out_unregister_driver;
1492     }
1493 
1494     if (IS_ERR(cdns_uart_data->uartclk)) {
1495         cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk");
1496         if (IS_ERR(cdns_uart_data->uartclk)) {
1497             rc = PTR_ERR(cdns_uart_data->uartclk);
1498             goto err_out_unregister_driver;
1499         }
1500         dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n");
1501     }
1502 
1503     rc = clk_prepare_enable(cdns_uart_data->pclk);
1504     if (rc) {
1505         dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
1506         goto err_out_unregister_driver;
1507     }
1508     rc = clk_prepare_enable(cdns_uart_data->uartclk);
1509     if (rc) {
1510         dev_err(&pdev->dev, "Unable to enable device clock.\n");
1511         goto err_out_clk_dis_pclk;
1512     }
1513 
1514     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1515     if (!res) {
1516         rc = -ENODEV;
1517         goto err_out_clk_disable;
1518     }
1519 
1520     irq = platform_get_irq(pdev, 0);
1521     if (irq <= 0) {
1522         rc = -ENXIO;
1523         goto err_out_clk_disable;
1524     }
1525 
1526 #ifdef CONFIG_COMMON_CLK
1527     cdns_uart_data->clk_rate_change_nb.notifier_call =
1528             cdns_uart_clk_notifier_cb;
1529     if (clk_notifier_register(cdns_uart_data->uartclk,
1530                 &cdns_uart_data->clk_rate_change_nb))
1531         dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
1532 #endif
1533 
1534     /* At this point, we've got an empty uart_port struct, initialize it */
1535     spin_lock_init(&port->lock);
1536     port->type  = PORT_UNKNOWN;
1537     port->iotype    = UPIO_MEM32;
1538     port->flags = UPF_BOOT_AUTOCONF;
1539     port->ops   = &cdns_uart_ops;
1540     port->fifosize  = CDNS_UART_FIFO_SIZE;
1541     port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_XILINX_PS_UART_CONSOLE);
1542     port->line  = id;
1543 
1544     /*
1545      * Register the port.
1546      * This function also registers this device with the tty layer
1547      * and triggers invocation of the config_port() entry point.
1548      */
1549     port->mapbase = res->start;
1550     port->irq = irq;
1551     port->dev = &pdev->dev;
1552     port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
1553     port->private_data = cdns_uart_data;
1554     cdns_uart_data->port = port;
1555     platform_set_drvdata(pdev, port);
1556 
1557     pm_runtime_use_autosuspend(&pdev->dev);
1558     pm_runtime_set_autosuspend_delay(&pdev->dev, UART_AUTOSUSPEND_TIMEOUT);
1559     pm_runtime_set_active(&pdev->dev);
1560     pm_runtime_enable(&pdev->dev);
1561     device_init_wakeup(port->dev, true);
1562 
1563 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1564     /*
1565      * If console hasn't been found yet try to assign this port
1566      * because it is required to be assigned for console setup function.
1567      * If register_console() don't assign value, then console_port pointer
1568      * is cleanup.
1569      */
1570     if (!console_port) {
1571         cdns_uart_console.index = id;
1572         console_port = port;
1573     }
1574 #endif
1575 
1576     rc = uart_add_one_port(&cdns_uart_uart_driver, port);
1577     if (rc) {
1578         dev_err(&pdev->dev,
1579             "uart_add_one_port() failed; err=%i\n", rc);
1580         goto err_out_pm_disable;
1581     }
1582 
1583 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1584     /* This is not port which is used for console that's why clean it up */
1585     if (console_port == port &&
1586         !(cdns_uart_uart_driver.cons->flags & CON_ENABLED)) {
1587         console_port = NULL;
1588         cdns_uart_console.index = -1;
1589     }
1590 #endif
1591 
1592     cdns_uart_data->cts_override = of_property_read_bool(pdev->dev.of_node,
1593                                  "cts-override");
1594 
1595     instances++;
1596 
1597     return 0;
1598 
1599 err_out_pm_disable:
1600     pm_runtime_disable(&pdev->dev);
1601     pm_runtime_set_suspended(&pdev->dev);
1602     pm_runtime_dont_use_autosuspend(&pdev->dev);
1603 #ifdef CONFIG_COMMON_CLK
1604     clk_notifier_unregister(cdns_uart_data->uartclk,
1605             &cdns_uart_data->clk_rate_change_nb);
1606 #endif
1607 err_out_clk_disable:
1608     clk_disable_unprepare(cdns_uart_data->uartclk);
1609 err_out_clk_dis_pclk:
1610     clk_disable_unprepare(cdns_uart_data->pclk);
1611 err_out_unregister_driver:
1612     if (!instances)
1613         uart_unregister_driver(cdns_uart_data->cdns_uart_driver);
1614     return rc;
1615 }
1616 
1617 /**
1618  * cdns_uart_remove - called when the platform driver is unregistered
1619  * @pdev: Pointer to the platform device structure
1620  *
1621  * Return: 0 on success, negative errno otherwise
1622  */
1623 static int cdns_uart_remove(struct platform_device *pdev)
1624 {
1625     struct uart_port *port = platform_get_drvdata(pdev);
1626     struct cdns_uart *cdns_uart_data = port->private_data;
1627     int rc;
1628 
1629     /* Remove the cdns_uart port from the serial core */
1630 #ifdef CONFIG_COMMON_CLK
1631     clk_notifier_unregister(cdns_uart_data->uartclk,
1632             &cdns_uart_data->clk_rate_change_nb);
1633 #endif
1634     rc = uart_remove_one_port(cdns_uart_data->cdns_uart_driver, port);
1635     port->mapbase = 0;
1636     clk_disable_unprepare(cdns_uart_data->uartclk);
1637     clk_disable_unprepare(cdns_uart_data->pclk);
1638     pm_runtime_disable(&pdev->dev);
1639     pm_runtime_set_suspended(&pdev->dev);
1640     pm_runtime_dont_use_autosuspend(&pdev->dev);
1641     device_init_wakeup(&pdev->dev, false);
1642 
1643 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1644     if (console_port == port)
1645         console_port = NULL;
1646 #endif
1647 
1648     if (!--instances)
1649         uart_unregister_driver(cdns_uart_data->cdns_uart_driver);
1650     return rc;
1651 }
1652 
1653 static struct platform_driver cdns_uart_platform_driver = {
1654     .probe   = cdns_uart_probe,
1655     .remove  = cdns_uart_remove,
1656     .driver  = {
1657         .name = CDNS_UART_NAME,
1658         .of_match_table = cdns_uart_of_match,
1659         .pm = &cdns_uart_dev_pm_ops,
1660         .suppress_bind_attrs = IS_BUILTIN(CONFIG_SERIAL_XILINX_PS_UART),
1661         },
1662 };
1663 
1664 static int __init cdns_uart_init(void)
1665 {
1666     /* Register the platform driver */
1667     return platform_driver_register(&cdns_uart_platform_driver);
1668 }
1669 
1670 static void __exit cdns_uart_exit(void)
1671 {
1672     /* Unregister the platform driver */
1673     platform_driver_unregister(&cdns_uart_platform_driver);
1674 }
1675 
1676 arch_initcall(cdns_uart_init);
1677 module_exit(cdns_uart_exit);
1678 
1679 MODULE_DESCRIPTION("Driver for Cadence UART");
1680 MODULE_AUTHOR("Xilinx Inc.");
1681 MODULE_LICENSE("GPL");