1 // SPDX-License-Identifier: GPL-2.0+
3 * Cadence UART driver (found in Xilinx Zynq)
5 * 2011 - 2014 (C) Xilinx Inc.
7 * This driver has originally been pushed by Xilinx using a Zynq-branding. This
8 * still shows in the naming of this file, the kconfig symbols and some symbols
12 #if defined(CONFIG_SERIAL_XILINX_PS_UART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
16 #include <linux/platform_device.h>
17 #include <linux/serial.h>
18 #include <linux/console.h>
19 #include <linux/serial_core.h>
20 #include <linux/slab.h>
21 #include <linux/tty.h>
22 #include <linux/tty_flip.h>
23 #include <linux/clk.h>
24 #include <linux/irq.h>
27 #include <linux/module.h>
28 #include <linux/pm_runtime.h>
30 #define CDNS_UART_TTY_NAME "ttyPS"
31 #define CDNS_UART_NAME "xuartps"
32 #define CDNS_UART_MAJOR 0 /* use dynamic node allocation */
33 #define CDNS_UART_NR_PORTS 2
34 #define CDNS_UART_FIFO_SIZE 64 /* FIFO size */
35 #define CDNS_UART_REGISTER_SPACE 0x1000
37 /* Rx Trigger level */
38 static int rx_trigger_level = 56;
39 module_param(rx_trigger_level, uint, S_IRUGO);
40 MODULE_PARM_DESC(rx_trigger_level, "Rx trigger level, 1-63 bytes");
43 static int rx_timeout = 10;
44 module_param(rx_timeout, uint, S_IRUGO);
45 MODULE_PARM_DESC(rx_timeout, "Rx timeout, 1-255");
47 /* Register offsets for the UART. */
48 #define CDNS_UART_CR 0x00 /* Control Register */
49 #define CDNS_UART_MR 0x04 /* Mode Register */
50 #define CDNS_UART_IER 0x08 /* Interrupt Enable */
51 #define CDNS_UART_IDR 0x0C /* Interrupt Disable */
52 #define CDNS_UART_IMR 0x10 /* Interrupt Mask */
53 #define CDNS_UART_ISR 0x14 /* Interrupt Status */
54 #define CDNS_UART_BAUDGEN 0x18 /* Baud Rate Generator */
55 #define CDNS_UART_RXTOUT 0x1C /* RX Timeout */
56 #define CDNS_UART_RXWM 0x20 /* RX FIFO Trigger Level */
57 #define CDNS_UART_MODEMCR 0x24 /* Modem Control */
58 #define CDNS_UART_MODEMSR 0x28 /* Modem Status */
59 #define CDNS_UART_SR 0x2C /* Channel Status */
60 #define CDNS_UART_FIFO 0x30 /* FIFO */
61 #define CDNS_UART_BAUDDIV 0x34 /* Baud Rate Divider */
62 #define CDNS_UART_FLOWDEL 0x38 /* Flow Delay */
63 #define CDNS_UART_IRRX_PWIDTH 0x3C /* IR Min Received Pulse Width */
64 #define CDNS_UART_IRTX_PWIDTH 0x40 /* IR Transmitted pulse Width */
65 #define CDNS_UART_TXWM 0x44 /* TX FIFO Trigger Level */
66 #define CDNS_UART_RXBS 0x48 /* RX FIFO byte status register */
68 /* Control Register Bit Definitions */
69 #define CDNS_UART_CR_STOPBRK 0x00000100 /* Stop TX break */
70 #define CDNS_UART_CR_STARTBRK 0x00000080 /* Set TX break */
71 #define CDNS_UART_CR_TX_DIS 0x00000020 /* TX disabled. */
72 #define CDNS_UART_CR_TX_EN 0x00000010 /* TX enabled */
73 #define CDNS_UART_CR_RX_DIS 0x00000008 /* RX disabled. */
74 #define CDNS_UART_CR_RX_EN 0x00000004 /* RX enabled */
75 #define CDNS_UART_CR_TXRST 0x00000002 /* TX logic reset */
76 #define CDNS_UART_CR_RXRST 0x00000001 /* RX logic reset */
77 #define CDNS_UART_CR_RST_TO 0x00000040 /* Restart Timeout Counter */
78 #define CDNS_UART_RXBS_PARITY 0x00000001 /* Parity error status */
79 #define CDNS_UART_RXBS_FRAMING 0x00000002 /* Framing error status */
80 #define CDNS_UART_RXBS_BRK 0x00000004 /* Overrun error status */
84 * The mode register (MR) defines the mode of transfer as well as the data
85 * format. If this register is modified during transmission or reception,
86 * data validity cannot be guaranteed.
88 #define CDNS_UART_MR_CLKSEL 0x00000001 /* Pre-scalar selection */
89 #define CDNS_UART_MR_CHMODE_L_LOOP 0x00000200 /* Local loop back mode */
90 #define CDNS_UART_MR_CHMODE_NORM 0x00000000 /* Normal mode */
91 #define CDNS_UART_MR_CHMODE_MASK 0x00000300 /* Mask for mode bits */
93 #define CDNS_UART_MR_STOPMODE_2_BIT 0x00000080 /* 2 stop bits */
94 #define CDNS_UART_MR_STOPMODE_1_BIT 0x00000000 /* 1 stop bit */
96 #define CDNS_UART_MR_PARITY_NONE 0x00000020 /* No parity mode */
97 #define CDNS_UART_MR_PARITY_MARK 0x00000018 /* Mark parity mode */
98 #define CDNS_UART_MR_PARITY_SPACE 0x00000010 /* Space parity mode */
99 #define CDNS_UART_MR_PARITY_ODD 0x00000008 /* Odd parity mode */
100 #define CDNS_UART_MR_PARITY_EVEN 0x00000000 /* Even parity mode */
102 #define CDNS_UART_MR_CHARLEN_6_BIT 0x00000006 /* 6 bits data */
103 #define CDNS_UART_MR_CHARLEN_7_BIT 0x00000004 /* 7 bits data */
104 #define CDNS_UART_MR_CHARLEN_8_BIT 0x00000000 /* 8 bits data */
107 * Interrupt Registers:
108 * Interrupt control logic uses the interrupt enable register (IER) and the
109 * interrupt disable register (IDR) to set the value of the bits in the
110 * interrupt mask register (IMR). The IMR determines whether to pass an
111 * interrupt to the interrupt status register (ISR).
112 * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
113 * interrupt. IMR and ISR are read only, and IER and IDR are write only.
114 * Reading either IER or IDR returns 0x00.
115 * All four registers have the same bit definitions.
117 #define CDNS_UART_IXR_TOUT 0x00000100 /* RX Timeout error interrupt */
118 #define CDNS_UART_IXR_PARITY 0x00000080 /* Parity error interrupt */
119 #define CDNS_UART_IXR_FRAMING 0x00000040 /* Framing error interrupt */
120 #define CDNS_UART_IXR_OVERRUN 0x00000020 /* Overrun error interrupt */
121 #define CDNS_UART_IXR_TXFULL 0x00000010 /* TX FIFO Full interrupt */
122 #define CDNS_UART_IXR_TXEMPTY 0x00000008 /* TX FIFO empty interrupt */
123 #define CDNS_UART_ISR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt */
124 #define CDNS_UART_IXR_RXTRIG 0x00000001 /* RX FIFO trigger interrupt */
125 #define CDNS_UART_IXR_RXFULL 0x00000004 /* RX FIFO full interrupt. */
126 #define CDNS_UART_IXR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt. */
127 #define CDNS_UART_IXR_MASK 0x00001FFF /* Valid bit mask */
130 * Do not enable parity error interrupt for the following
131 * reason: When parity error interrupt is enabled, each Rx
132 * parity error always results in 2 events. The first one
133 * being parity error interrupt and the second one with a
134 * proper Rx interrupt with the incoming data. Disabling
135 * parity error interrupt ensures better handling of parity
136 * error events. With this change, for a parity error case, we
137 * get a Rx interrupt with parity error set in ISR register
138 * and we still handle parity errors in the desired way.
141 #define CDNS_UART_RX_IRQS (CDNS_UART_IXR_FRAMING | \
142 CDNS_UART_IXR_OVERRUN | \
143 CDNS_UART_IXR_RXTRIG | \
146 /* Goes in read_status_mask for break detection as the HW doesn't do it*/
147 #define CDNS_UART_IXR_BRK 0x00002000
149 #define CDNS_UART_RXBS_SUPPORT BIT(1)
151 * Modem Control register:
152 * The read/write Modem Control register controls the interface with the modem
153 * or data set, or a peripheral device emulating a modem.
155 #define CDNS_UART_MODEMCR_FCM 0x00000020 /* Automatic flow control mode */
156 #define CDNS_UART_MODEMCR_RTS 0x00000002 /* Request to send output control */
157 #define CDNS_UART_MODEMCR_DTR 0x00000001 /* Data Terminal Ready */
160 * Channel Status Register:
161 * The channel status register (CSR) is provided to enable the control logic
162 * to monitor the status of bits in the channel interrupt status register,
163 * even if these are masked out by the interrupt mask register.
165 #define CDNS_UART_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
166 #define CDNS_UART_SR_TXEMPTY 0x00000008 /* TX FIFO empty */
167 #define CDNS_UART_SR_TXFULL 0x00000010 /* TX FIFO full */
168 #define CDNS_UART_SR_RXTRIG 0x00000001 /* Rx Trigger */
169 #define CDNS_UART_SR_TACTIVE 0x00000800 /* TX state machine active */
171 /* baud dividers min/max values */
172 #define CDNS_UART_BDIV_MIN 4
173 #define CDNS_UART_BDIV_MAX 255
174 #define CDNS_UART_CD_MAX 65535
175 #define UART_AUTOSUSPEND_TIMEOUT 3000
178 * struct cdns_uart - device data
179 * @port: Pointer to the UART port
180 * @uartclk: Reference clock
182 * @cdns_uart_driver: Pointer to UART driver
183 * @baud: Current baud rate
185 * @clk_rate_change_nb: Notifier block for clock changes
186 * @quirks: Flags for RXBS support.
189 struct uart_port *port;
192 struct uart_driver *cdns_uart_driver;
195 struct notifier_block clk_rate_change_nb;
198 struct cdns_platform_data {
201 #define to_cdns_uart(_nb) container_of(_nb, struct cdns_uart, \
205 * cdns_uart_handle_rx - Handle the received bytes along with Rx errors.
206 * @dev_id: Id of the UART port
207 * @isrstatus: The interrupt status register value as read
210 static void cdns_uart_handle_rx(void *dev_id, unsigned int isrstatus)
212 struct uart_port *port = (struct uart_port *)dev_id;
213 struct cdns_uart *cdns_uart = port->private_data;
215 unsigned int rxbs_status = 0;
216 unsigned int status_mask;
217 unsigned int framerrprocessed = 0;
218 char status = TTY_NORMAL;
219 bool is_rxbs_support;
221 is_rxbs_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
223 while ((readl(port->membase + CDNS_UART_SR) &
224 CDNS_UART_SR_RXEMPTY) != CDNS_UART_SR_RXEMPTY) {
226 rxbs_status = readl(port->membase + CDNS_UART_RXBS);
227 data = readl(port->membase + CDNS_UART_FIFO);
230 * There is no hardware break detection in Zynq, so we interpret
231 * framing error with all-zeros data as a break sequence.
232 * Most of the time, there's another non-zero byte at the
233 * end of the sequence.
235 if (!is_rxbs_support && (isrstatus & CDNS_UART_IXR_FRAMING)) {
237 port->read_status_mask |= CDNS_UART_IXR_BRK;
238 framerrprocessed = 1;
242 if (is_rxbs_support && (rxbs_status & CDNS_UART_RXBS_BRK)) {
245 if (uart_handle_break(port))
249 isrstatus &= port->read_status_mask;
250 isrstatus &= ~port->ignore_status_mask;
251 status_mask = port->read_status_mask;
252 status_mask &= ~port->ignore_status_mask;
255 (port->read_status_mask & CDNS_UART_IXR_BRK)) {
256 port->read_status_mask &= ~CDNS_UART_IXR_BRK;
258 if (uart_handle_break(port))
262 if (uart_handle_sysrq_char(port, data))
265 if (is_rxbs_support) {
266 if ((rxbs_status & CDNS_UART_RXBS_PARITY)
267 && (status_mask & CDNS_UART_IXR_PARITY)) {
268 port->icount.parity++;
271 if ((rxbs_status & CDNS_UART_RXBS_FRAMING)
272 && (status_mask & CDNS_UART_IXR_PARITY)) {
273 port->icount.frame++;
277 if (isrstatus & CDNS_UART_IXR_PARITY) {
278 port->icount.parity++;
281 if ((isrstatus & CDNS_UART_IXR_FRAMING) &&
283 port->icount.frame++;
287 if (isrstatus & CDNS_UART_IXR_OVERRUN) {
288 port->icount.overrun++;
289 tty_insert_flip_char(&port->state->port, 0,
292 tty_insert_flip_char(&port->state->port, data, status);
295 spin_unlock(&port->lock);
296 tty_flip_buffer_push(&port->state->port);
297 spin_lock(&port->lock);
301 * cdns_uart_handle_tx - Handle the bytes to be Txed.
302 * @dev_id: Id of the UART port
305 static void cdns_uart_handle_tx(void *dev_id)
307 struct uart_port *port = (struct uart_port *)dev_id;
308 unsigned int numbytes;
310 if (uart_circ_empty(&port->state->xmit)) {
311 writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IDR);
313 numbytes = port->fifosize;
314 while (numbytes && !uart_circ_empty(&port->state->xmit) &&
315 !(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXFULL)) {
317 * Get the data from the UART circular buffer
318 * and write it to the cdns_uart's TX_FIFO
322 port->state->xmit.buf[port->state->xmit.
323 tail], port->membase + CDNS_UART_FIFO);
328 * Adjust the tail of the UART buffer and wrap
329 * the buffer if it reaches limit.
331 port->state->xmit.tail =
332 (port->state->xmit.tail + 1) &
333 (UART_XMIT_SIZE - 1);
338 if (uart_circ_chars_pending(
339 &port->state->xmit) < WAKEUP_CHARS)
340 uart_write_wakeup(port);
345 * cdns_uart_isr - Interrupt handler
347 * @dev_id: Id of the port
351 static irqreturn_t cdns_uart_isr(int irq, void *dev_id)
353 struct uart_port *port = (struct uart_port *)dev_id;
354 unsigned int isrstatus;
356 spin_lock(&port->lock);
358 /* Read the interrupt status register to determine which
359 * interrupt(s) is/are active and clear them.
361 isrstatus = readl(port->membase + CDNS_UART_ISR);
362 writel(isrstatus, port->membase + CDNS_UART_ISR);
364 if (isrstatus & CDNS_UART_IXR_TXEMPTY) {
365 cdns_uart_handle_tx(dev_id);
366 isrstatus &= ~CDNS_UART_IXR_TXEMPTY;
368 if (isrstatus & CDNS_UART_IXR_MASK)
369 cdns_uart_handle_rx(dev_id, isrstatus);
371 spin_unlock(&port->lock);
376 * cdns_uart_calc_baud_divs - Calculate baud rate divisors
377 * @clk: UART module input clock
378 * @baud: Desired baud rate
379 * @rbdiv: BDIV value (return value)
380 * @rcd: CD value (return value)
381 * @div8: Value for clk_sel bit in mod (return value)
382 * Return: baud rate, requested baud when possible, or actual baud when there
383 * was too much error, zero if no valid divisors are found.
385 * Formula to obtain baud rate is
386 * baud_tx/rx rate = clk/CD * (BDIV + 1)
387 * input_clk = (Uart User Defined Clock or Apb Clock)
388 * depends on UCLKEN in MR Reg
389 * clk = input_clk or input_clk/8;
390 * depends on CLKS in MR reg
391 * CD and BDIV depends on values in
392 * baud rate generate register
393 * baud rate clock divisor register
395 static unsigned int cdns_uart_calc_baud_divs(unsigned int clk,
396 unsigned int baud, u32 *rbdiv, u32 *rcd, int *div8)
399 unsigned int calc_baud;
400 unsigned int bestbaud = 0;
401 unsigned int bauderror;
402 unsigned int besterror = ~0;
404 if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) {
411 for (bdiv = CDNS_UART_BDIV_MIN; bdiv <= CDNS_UART_BDIV_MAX; bdiv++) {
412 cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
413 if (cd < 1 || cd > CDNS_UART_CD_MAX)
416 calc_baud = clk / (cd * (bdiv + 1));
418 if (baud > calc_baud)
419 bauderror = baud - calc_baud;
421 bauderror = calc_baud - baud;
423 if (besterror > bauderror) {
426 bestbaud = calc_baud;
427 besterror = bauderror;
430 /* use the values when percent error is acceptable */
431 if (((besterror * 100) / baud) < 3)
438 * cdns_uart_set_baud_rate - Calculate and set the baud rate
439 * @port: Handle to the uart port structure
440 * @baud: Baud rate to set
441 * Return: baud rate, requested baud when possible, or actual baud when there
442 * was too much error, zero if no valid divisors are found.
444 static unsigned int cdns_uart_set_baud_rate(struct uart_port *port,
447 unsigned int calc_baud;
448 u32 cd = 0, bdiv = 0;
451 struct cdns_uart *cdns_uart = port->private_data;
453 calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
456 /* Write new divisors to hardware */
457 mreg = readl(port->membase + CDNS_UART_MR);
459 mreg |= CDNS_UART_MR_CLKSEL;
461 mreg &= ~CDNS_UART_MR_CLKSEL;
462 writel(mreg, port->membase + CDNS_UART_MR);
463 writel(cd, port->membase + CDNS_UART_BAUDGEN);
464 writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
465 cdns_uart->baud = baud;
470 #ifdef CONFIG_COMMON_CLK
472 * cdns_uart_clk_notitifer_cb - Clock notifier callback
473 * @nb: Notifier block
474 * @event: Notify event
475 * @data: Notifier data
476 * Return: NOTIFY_OK or NOTIFY_DONE on success, NOTIFY_BAD on error.
478 static int cdns_uart_clk_notifier_cb(struct notifier_block *nb,
479 unsigned long event, void *data)
482 struct uart_port *port;
484 struct clk_notifier_data *ndata = data;
485 unsigned long flags = 0;
486 struct cdns_uart *cdns_uart = to_cdns_uart(nb);
488 port = cdns_uart->port;
493 case PRE_RATE_CHANGE:
499 * Find out if current baud-rate can be achieved with new clock
502 if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud,
503 &bdiv, &cd, &div8)) {
504 dev_warn(port->dev, "clock rate change rejected\n");
508 spin_lock_irqsave(&cdns_uart->port->lock, flags);
510 /* Disable the TX and RX to set baud rate */
511 ctrl_reg = readl(port->membase + CDNS_UART_CR);
512 ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
513 writel(ctrl_reg, port->membase + CDNS_UART_CR);
515 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
519 case POST_RATE_CHANGE:
521 * Set clk dividers to generate correct baud with new clock
525 spin_lock_irqsave(&cdns_uart->port->lock, flags);
528 port->uartclk = ndata->new_rate;
530 cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port,
533 case ABORT_RATE_CHANGE:
535 spin_lock_irqsave(&cdns_uart->port->lock, flags);
537 /* Set TX/RX Reset */
538 ctrl_reg = readl(port->membase + CDNS_UART_CR);
539 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
540 writel(ctrl_reg, port->membase + CDNS_UART_CR);
542 while (readl(port->membase + CDNS_UART_CR) &
543 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
547 * Clear the RX disable and TX disable bits and then set the TX
548 * enable bit and RX enable bit to enable the transmitter and
551 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
552 ctrl_reg = readl(port->membase + CDNS_UART_CR);
553 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
554 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
555 writel(ctrl_reg, port->membase + CDNS_UART_CR);
557 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
567 * cdns_uart_start_tx - Start transmitting bytes
568 * @port: Handle to the uart port structure
570 static void cdns_uart_start_tx(struct uart_port *port)
574 if (uart_tx_stopped(port))
578 * Set the TX enable bit and clear the TX disable bit to enable the
581 status = readl(port->membase + CDNS_UART_CR);
582 status &= ~CDNS_UART_CR_TX_DIS;
583 status |= CDNS_UART_CR_TX_EN;
584 writel(status, port->membase + CDNS_UART_CR);
586 if (uart_circ_empty(&port->state->xmit))
589 cdns_uart_handle_tx(port);
591 writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_ISR);
592 /* Enable the TX Empty interrupt */
593 writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IER);
597 * cdns_uart_stop_tx - Stop TX
598 * @port: Handle to the uart port structure
600 static void cdns_uart_stop_tx(struct uart_port *port)
604 regval = readl(port->membase + CDNS_UART_CR);
605 regval |= CDNS_UART_CR_TX_DIS;
606 /* Disable the transmitter */
607 writel(regval, port->membase + CDNS_UART_CR);
611 * cdns_uart_stop_rx - Stop RX
612 * @port: Handle to the uart port structure
614 static void cdns_uart_stop_rx(struct uart_port *port)
618 /* Disable RX IRQs */
619 writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IDR);
621 /* Disable the receiver */
622 regval = readl(port->membase + CDNS_UART_CR);
623 regval |= CDNS_UART_CR_RX_DIS;
624 writel(regval, port->membase + CDNS_UART_CR);
628 * cdns_uart_tx_empty - Check whether TX is empty
629 * @port: Handle to the uart port structure
631 * Return: TIOCSER_TEMT on success, 0 otherwise
633 static unsigned int cdns_uart_tx_empty(struct uart_port *port)
637 status = readl(port->membase + CDNS_UART_SR) &
638 CDNS_UART_SR_TXEMPTY;
639 return status ? TIOCSER_TEMT : 0;
643 * cdns_uart_break_ctl - Based on the input ctl we have to start or stop
644 * transmitting char breaks
645 * @port: Handle to the uart port structure
646 * @ctl: Value based on which start or stop decision is taken
648 static void cdns_uart_break_ctl(struct uart_port *port, int ctl)
653 spin_lock_irqsave(&port->lock, flags);
655 status = readl(port->membase + CDNS_UART_CR);
658 writel(CDNS_UART_CR_STARTBRK | status,
659 port->membase + CDNS_UART_CR);
661 if ((status & CDNS_UART_CR_STOPBRK) == 0)
662 writel(CDNS_UART_CR_STOPBRK | status,
663 port->membase + CDNS_UART_CR);
665 spin_unlock_irqrestore(&port->lock, flags);
669 * cdns_uart_set_termios - termios operations, handling data length, parity,
670 * stop bits, flow control, baud rate
671 * @port: Handle to the uart port structure
672 * @termios: Handle to the input termios structure
673 * @old: Values of the previously saved termios structure
675 static void cdns_uart_set_termios(struct uart_port *port,
676 struct ktermios *termios, struct ktermios *old)
678 unsigned int cval = 0;
679 unsigned int baud, minbaud, maxbaud;
681 unsigned int ctrl_reg, mode_reg;
683 spin_lock_irqsave(&port->lock, flags);
685 /* Wait for the transmit FIFO to empty before making changes */
686 if (!(readl(port->membase + CDNS_UART_CR) &
687 CDNS_UART_CR_TX_DIS)) {
688 while (!(readl(port->membase + CDNS_UART_SR) &
689 CDNS_UART_SR_TXEMPTY)) {
694 /* Disable the TX and RX to set baud rate */
695 ctrl_reg = readl(port->membase + CDNS_UART_CR);
696 ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
697 writel(ctrl_reg, port->membase + CDNS_UART_CR);
700 * Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk
701 * min and max baud should be calculated here based on port->uartclk.
702 * this way we get a valid baud and can safely call set_baud()
704 minbaud = port->uartclk /
705 ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8);
706 maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1);
707 baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
708 baud = cdns_uart_set_baud_rate(port, baud);
709 if (tty_termios_baud_rate(termios))
710 tty_termios_encode_baud_rate(termios, baud, baud);
712 /* Update the per-port timeout. */
713 uart_update_timeout(port, termios->c_cflag, baud);
715 /* Set TX/RX Reset */
716 ctrl_reg = readl(port->membase + CDNS_UART_CR);
717 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
718 writel(ctrl_reg, port->membase + CDNS_UART_CR);
720 while (readl(port->membase + CDNS_UART_CR) &
721 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
725 * Clear the RX disable and TX disable bits and then set the TX enable
726 * bit and RX enable bit to enable the transmitter and receiver.
728 ctrl_reg = readl(port->membase + CDNS_UART_CR);
729 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
730 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
731 writel(ctrl_reg, port->membase + CDNS_UART_CR);
733 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
735 port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
736 CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
737 port->ignore_status_mask = 0;
739 if (termios->c_iflag & INPCK)
740 port->read_status_mask |= CDNS_UART_IXR_PARITY |
741 CDNS_UART_IXR_FRAMING;
743 if (termios->c_iflag & IGNPAR)
744 port->ignore_status_mask |= CDNS_UART_IXR_PARITY |
745 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
747 /* ignore all characters if CREAD is not set */
748 if ((termios->c_cflag & CREAD) == 0)
749 port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG |
750 CDNS_UART_IXR_TOUT | CDNS_UART_IXR_PARITY |
751 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
753 mode_reg = readl(port->membase + CDNS_UART_MR);
755 /* Handling Data Size */
756 switch (termios->c_cflag & CSIZE) {
758 cval |= CDNS_UART_MR_CHARLEN_6_BIT;
761 cval |= CDNS_UART_MR_CHARLEN_7_BIT;
765 cval |= CDNS_UART_MR_CHARLEN_8_BIT;
766 termios->c_cflag &= ~CSIZE;
767 termios->c_cflag |= CS8;
771 /* Handling Parity and Stop Bits length */
772 if (termios->c_cflag & CSTOPB)
773 cval |= CDNS_UART_MR_STOPMODE_2_BIT; /* 2 STOP bits */
775 cval |= CDNS_UART_MR_STOPMODE_1_BIT; /* 1 STOP bit */
777 if (termios->c_cflag & PARENB) {
778 /* Mark or Space parity */
779 if (termios->c_cflag & CMSPAR) {
780 if (termios->c_cflag & PARODD)
781 cval |= CDNS_UART_MR_PARITY_MARK;
783 cval |= CDNS_UART_MR_PARITY_SPACE;
785 if (termios->c_cflag & PARODD)
786 cval |= CDNS_UART_MR_PARITY_ODD;
788 cval |= CDNS_UART_MR_PARITY_EVEN;
791 cval |= CDNS_UART_MR_PARITY_NONE;
793 cval |= mode_reg & 1;
794 writel(cval, port->membase + CDNS_UART_MR);
796 spin_unlock_irqrestore(&port->lock, flags);
800 * cdns_uart_startup - Called when an application opens a cdns_uart port
801 * @port: Handle to the uart port structure
803 * Return: 0 on success, negative errno otherwise
805 static int cdns_uart_startup(struct uart_port *port)
807 struct cdns_uart *cdns_uart = port->private_data;
811 unsigned int status = 0;
813 is_brk_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
815 spin_lock_irqsave(&port->lock, flags);
817 /* Disable the TX and RX */
818 writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
819 port->membase + CDNS_UART_CR);
821 /* Set the Control Register with TX/RX Enable, TX/RX Reset,
824 writel(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST,
825 port->membase + CDNS_UART_CR);
827 while (readl(port->membase + CDNS_UART_CR) &
828 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
832 * Clear the RX disable bit and then set the RX enable bit to enable
835 status = readl(port->membase + CDNS_UART_CR);
836 status &= ~CDNS_UART_CR_RX_DIS;
837 status |= CDNS_UART_CR_RX_EN;
838 writel(status, port->membase + CDNS_UART_CR);
840 /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
843 writel(CDNS_UART_MR_CHMODE_NORM | CDNS_UART_MR_STOPMODE_1_BIT
844 | CDNS_UART_MR_PARITY_NONE | CDNS_UART_MR_CHARLEN_8_BIT,
845 port->membase + CDNS_UART_MR);
848 * Set the RX FIFO Trigger level to use most of the FIFO, but it
849 * can be tuned with a module parameter
851 writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
854 * Receive Timeout register is enabled but it
855 * can be tuned with a module parameter
857 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
859 /* Clear out any pending interrupts before enabling them */
860 writel(readl(port->membase + CDNS_UART_ISR),
861 port->membase + CDNS_UART_ISR);
863 spin_unlock_irqrestore(&port->lock, flags);
865 ret = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME, port);
867 dev_err(port->dev, "request_irq '%d' failed with %d\n",
872 /* Set the Interrupt Registers with desired interrupts */
874 writel(CDNS_UART_RX_IRQS | CDNS_UART_IXR_BRK,
875 port->membase + CDNS_UART_IER);
877 writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IER);
883 * cdns_uart_shutdown - Called when an application closes a cdns_uart port
884 * @port: Handle to the uart port structure
886 static void cdns_uart_shutdown(struct uart_port *port)
891 spin_lock_irqsave(&port->lock, flags);
893 /* Disable interrupts */
894 status = readl(port->membase + CDNS_UART_IMR);
895 writel(status, port->membase + CDNS_UART_IDR);
896 writel(0xffffffff, port->membase + CDNS_UART_ISR);
898 /* Disable the TX and RX */
899 writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
900 port->membase + CDNS_UART_CR);
902 spin_unlock_irqrestore(&port->lock, flags);
904 free_irq(port->irq, port);
908 * cdns_uart_type - Set UART type to cdns_uart port
909 * @port: Handle to the uart port structure
911 * Return: string on success, NULL otherwise
913 static const char *cdns_uart_type(struct uart_port *port)
915 return port->type == PORT_XUARTPS ? CDNS_UART_NAME : NULL;
919 * cdns_uart_verify_port - Verify the port params
920 * @port: Handle to the uart port structure
921 * @ser: Handle to the structure whose members are compared
923 * Return: 0 on success, negative errno otherwise.
925 static int cdns_uart_verify_port(struct uart_port *port,
926 struct serial_struct *ser)
928 if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
930 if (port->irq != ser->irq)
932 if (ser->io_type != UPIO_MEM)
934 if (port->iobase != ser->port)
942 * cdns_uart_request_port - Claim the memory region attached to cdns_uart port,
943 * called when the driver adds a cdns_uart port via
944 * uart_add_one_port()
945 * @port: Handle to the uart port structure
947 * Return: 0 on success, negative errno otherwise.
949 static int cdns_uart_request_port(struct uart_port *port)
951 if (!request_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE,
956 port->membase = ioremap(port->mapbase, CDNS_UART_REGISTER_SPACE);
957 if (!port->membase) {
958 dev_err(port->dev, "Unable to map registers\n");
959 release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
966 * cdns_uart_release_port - Release UART port
967 * @port: Handle to the uart port structure
969 * Release the memory region attached to a cdns_uart port. Called when the
970 * driver removes a cdns_uart port via uart_remove_one_port().
972 static void cdns_uart_release_port(struct uart_port *port)
974 release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
975 iounmap(port->membase);
976 port->membase = NULL;
980 * cdns_uart_config_port - Configure UART port
981 * @port: Handle to the uart port structure
984 static void cdns_uart_config_port(struct uart_port *port, int flags)
986 if (flags & UART_CONFIG_TYPE && cdns_uart_request_port(port) == 0)
987 port->type = PORT_XUARTPS;
991 * cdns_uart_get_mctrl - Get the modem control state
992 * @port: Handle to the uart port structure
994 * Return: the modem control state
996 static unsigned int cdns_uart_get_mctrl(struct uart_port *port)
998 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
1001 static void cdns_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1006 val = readl(port->membase + CDNS_UART_MODEMCR);
1007 mode_reg = readl(port->membase + CDNS_UART_MR);
1009 val &= ~(CDNS_UART_MODEMCR_RTS | CDNS_UART_MODEMCR_DTR |
1010 CDNS_UART_MODEMCR_FCM);
1011 mode_reg &= ~CDNS_UART_MR_CHMODE_MASK;
1013 if (mctrl & TIOCM_RTS || mctrl & TIOCM_DTR)
1014 val |= CDNS_UART_MODEMCR_FCM;
1015 if (mctrl & TIOCM_LOOP)
1016 mode_reg |= CDNS_UART_MR_CHMODE_L_LOOP;
1018 mode_reg |= CDNS_UART_MR_CHMODE_NORM;
1020 writel(val, port->membase + CDNS_UART_MODEMCR);
1021 writel(mode_reg, port->membase + CDNS_UART_MR);
1024 #ifdef CONFIG_CONSOLE_POLL
1025 static int cdns_uart_poll_get_char(struct uart_port *port)
1028 unsigned long flags;
1030 spin_lock_irqsave(&port->lock, flags);
1032 /* Check if FIFO is empty */
1033 if (readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_RXEMPTY)
1035 else /* Read a character */
1036 c = (unsigned char) readl(port->membase + CDNS_UART_FIFO);
1038 spin_unlock_irqrestore(&port->lock, flags);
1043 static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
1045 unsigned long flags;
1047 spin_lock_irqsave(&port->lock, flags);
1049 /* Wait until FIFO is empty */
1050 while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
1053 /* Write a character */
1054 writel(c, port->membase + CDNS_UART_FIFO);
1056 /* Wait until FIFO is empty */
1057 while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
1060 spin_unlock_irqrestore(&port->lock, flags);
1066 static void cdns_uart_pm(struct uart_port *port, unsigned int state,
1067 unsigned int oldstate)
1070 case UART_PM_STATE_OFF:
1071 pm_runtime_mark_last_busy(port->dev);
1072 pm_runtime_put_autosuspend(port->dev);
1075 pm_runtime_get_sync(port->dev);
1080 static const struct uart_ops cdns_uart_ops = {
1081 .set_mctrl = cdns_uart_set_mctrl,
1082 .get_mctrl = cdns_uart_get_mctrl,
1083 .start_tx = cdns_uart_start_tx,
1084 .stop_tx = cdns_uart_stop_tx,
1085 .stop_rx = cdns_uart_stop_rx,
1086 .tx_empty = cdns_uart_tx_empty,
1087 .break_ctl = cdns_uart_break_ctl,
1088 .set_termios = cdns_uart_set_termios,
1089 .startup = cdns_uart_startup,
1090 .shutdown = cdns_uart_shutdown,
1092 .type = cdns_uart_type,
1093 .verify_port = cdns_uart_verify_port,
1094 .request_port = cdns_uart_request_port,
1095 .release_port = cdns_uart_release_port,
1096 .config_port = cdns_uart_config_port,
1097 #ifdef CONFIG_CONSOLE_POLL
1098 .poll_get_char = cdns_uart_poll_get_char,
1099 .poll_put_char = cdns_uart_poll_put_char,
1103 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1105 * cdns_uart_console_putchar - write the character to the FIFO buffer
1106 * @port: Handle to the uart port structure
1107 * @ch: Character to be written
1109 static void cdns_uart_console_putchar(struct uart_port *port, int ch)
1111 while (readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXFULL)
1113 writel(ch, port->membase + CDNS_UART_FIFO);
1116 static void cdns_early_write(struct console *con, const char *s,
1119 struct earlycon_device *dev = con->data;
1121 uart_console_write(&dev->port, s, n, cdns_uart_console_putchar);
1124 static int __init cdns_early_console_setup(struct earlycon_device *device,
1127 struct uart_port *port = &device->port;
1132 /* initialise control register */
1133 writel(CDNS_UART_CR_TX_EN|CDNS_UART_CR_TXRST|CDNS_UART_CR_RXRST,
1134 port->membase + CDNS_UART_CR);
1136 /* only set baud if specified on command line - otherwise
1137 * assume it has been initialized by a boot loader.
1139 if (port->uartclk && device->baud) {
1140 u32 cd = 0, bdiv = 0;
1144 cdns_uart_calc_baud_divs(port->uartclk, device->baud,
1146 mr = CDNS_UART_MR_PARITY_NONE;
1148 mr |= CDNS_UART_MR_CLKSEL;
1150 writel(mr, port->membase + CDNS_UART_MR);
1151 writel(cd, port->membase + CDNS_UART_BAUDGEN);
1152 writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
1155 device->con->write = cdns_early_write;
1159 OF_EARLYCON_DECLARE(cdns, "xlnx,xuartps", cdns_early_console_setup);
1160 OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p8", cdns_early_console_setup);
1161 OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p12", cdns_early_console_setup);
1162 OF_EARLYCON_DECLARE(cdns, "xlnx,zynqmp-uart", cdns_early_console_setup);
1165 /* Static pointer to console port */
1166 static struct uart_port *console_port;
1169 * cdns_uart_console_write - perform write operation
1170 * @co: Console handle
1171 * @s: Pointer to character array
1172 * @count: No of characters
1174 static void cdns_uart_console_write(struct console *co, const char *s,
1177 struct uart_port *port = console_port;
1178 unsigned long flags;
1179 unsigned int imr, ctrl;
1184 else if (oops_in_progress)
1185 locked = spin_trylock_irqsave(&port->lock, flags);
1187 spin_lock_irqsave(&port->lock, flags);
1189 /* save and disable interrupt */
1190 imr = readl(port->membase + CDNS_UART_IMR);
1191 writel(imr, port->membase + CDNS_UART_IDR);
1194 * Make sure that the tx part is enabled. Set the TX enable bit and
1195 * clear the TX disable bit to enable the transmitter.
1197 ctrl = readl(port->membase + CDNS_UART_CR);
1198 ctrl &= ~CDNS_UART_CR_TX_DIS;
1199 ctrl |= CDNS_UART_CR_TX_EN;
1200 writel(ctrl, port->membase + CDNS_UART_CR);
1202 uart_console_write(port, s, count, cdns_uart_console_putchar);
1203 while ((readl(port->membase + CDNS_UART_SR) &
1204 (CDNS_UART_SR_TXEMPTY | CDNS_UART_SR_TACTIVE)) !=
1205 CDNS_UART_SR_TXEMPTY)
1208 /* restore interrupt state */
1209 writel(imr, port->membase + CDNS_UART_IER);
1212 spin_unlock_irqrestore(&port->lock, flags);
1216 * cdns_uart_console_setup - Initialize the uart to default config
1217 * @co: Console handle
1218 * @options: Initial settings of uart
1220 * Return: 0 on success, negative errno otherwise.
1222 static int cdns_uart_console_setup(struct console *co, char *options)
1224 struct uart_port *port = console_port;
1231 if (!port->membase) {
1232 pr_debug("console on " CDNS_UART_TTY_NAME "%i not present\n",
1238 uart_parse_options(options, &baud, &parity, &bits, &flow);
1240 return uart_set_options(port, co, baud, parity, bits, flow);
1242 #endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
1244 #ifdef CONFIG_PM_SLEEP
1246 * cdns_uart_suspend - suspend event
1247 * @device: Pointer to the device structure
1251 static int cdns_uart_suspend(struct device *device)
1253 struct uart_port *port = dev_get_drvdata(device);
1254 struct cdns_uart *cdns_uart = port->private_data;
1257 may_wake = device_may_wakeup(device);
1259 if (console_suspend_enabled && may_wake) {
1260 unsigned long flags = 0;
1262 spin_lock_irqsave(&port->lock, flags);
1263 /* Empty the receive FIFO 1st before making changes */
1264 while (!(readl(port->membase + CDNS_UART_SR) &
1265 CDNS_UART_SR_RXEMPTY))
1266 readl(port->membase + CDNS_UART_FIFO);
1267 /* set RX trigger level to 1 */
1268 writel(1, port->membase + CDNS_UART_RXWM);
1269 /* disable RX timeout interrups */
1270 writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IDR);
1271 spin_unlock_irqrestore(&port->lock, flags);
1275 * Call the API provided in serial_core.c file which handles
1278 return uart_suspend_port(cdns_uart->cdns_uart_driver, port);
1282 * cdns_uart_resume - Resume after a previous suspend
1283 * @device: Pointer to the device structure
1287 static int cdns_uart_resume(struct device *device)
1289 struct uart_port *port = dev_get_drvdata(device);
1290 struct cdns_uart *cdns_uart = port->private_data;
1291 unsigned long flags = 0;
1295 may_wake = device_may_wakeup(device);
1297 if (console_suspend_enabled && !may_wake) {
1298 clk_enable(cdns_uart->pclk);
1299 clk_enable(cdns_uart->uartclk);
1301 spin_lock_irqsave(&port->lock, flags);
1303 /* Set TX/RX Reset */
1304 ctrl_reg = readl(port->membase + CDNS_UART_CR);
1305 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
1306 writel(ctrl_reg, port->membase + CDNS_UART_CR);
1307 while (readl(port->membase + CDNS_UART_CR) &
1308 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
1311 /* restore rx timeout value */
1312 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
1314 ctrl_reg = readl(port->membase + CDNS_UART_CR);
1315 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
1316 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
1317 writel(ctrl_reg, port->membase + CDNS_UART_CR);
1319 clk_disable(cdns_uart->uartclk);
1320 clk_disable(cdns_uart->pclk);
1321 spin_unlock_irqrestore(&port->lock, flags);
1323 spin_lock_irqsave(&port->lock, flags);
1324 /* restore original rx trigger level */
1325 writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
1326 /* enable RX timeout interrupt */
1327 writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IER);
1328 spin_unlock_irqrestore(&port->lock, flags);
1331 return uart_resume_port(cdns_uart->cdns_uart_driver, port);
1333 #endif /* ! CONFIG_PM_SLEEP */
1334 static int __maybe_unused cdns_runtime_suspend(struct device *dev)
1336 struct uart_port *port = dev_get_drvdata(dev);
1337 struct cdns_uart *cdns_uart = port->private_data;
1339 clk_disable(cdns_uart->uartclk);
1340 clk_disable(cdns_uart->pclk);
1344 static int __maybe_unused cdns_runtime_resume(struct device *dev)
1346 struct uart_port *port = dev_get_drvdata(dev);
1347 struct cdns_uart *cdns_uart = port->private_data;
1349 clk_enable(cdns_uart->pclk);
1350 clk_enable(cdns_uart->uartclk);
1354 static const struct dev_pm_ops cdns_uart_dev_pm_ops = {
1355 SET_SYSTEM_SLEEP_PM_OPS(cdns_uart_suspend, cdns_uart_resume)
1356 SET_RUNTIME_PM_OPS(cdns_runtime_suspend,
1357 cdns_runtime_resume, NULL)
1360 static const struct cdns_platform_data zynqmp_uart_def = {
1361 .quirks = CDNS_UART_RXBS_SUPPORT, };
1363 /* Match table for of_platform binding */
1364 static const struct of_device_id cdns_uart_of_match[] = {
1365 { .compatible = "xlnx,xuartps", },
1366 { .compatible = "cdns,uart-r1p8", },
1367 { .compatible = "cdns,uart-r1p12", .data = &zynqmp_uart_def },
1368 { .compatible = "xlnx,zynqmp-uart", .data = &zynqmp_uart_def },
1371 MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
1374 * cdns_uart_probe - Platform driver probe
1375 * @pdev: Pointer to the platform device structure
1377 * Return: 0 on success, negative errno otherwise
1379 static int cdns_uart_probe(struct platform_device *pdev)
1382 struct uart_port *port;
1383 struct resource *res;
1384 struct cdns_uart *cdns_uart_data;
1385 const struct of_device_id *match;
1386 struct uart_driver *cdns_uart_uart_driver;
1388 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1389 struct console *cdns_uart_console;
1392 cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
1394 if (!cdns_uart_data)
1396 port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
1400 cdns_uart_uart_driver = devm_kzalloc(&pdev->dev,
1401 sizeof(*cdns_uart_uart_driver),
1403 if (!cdns_uart_uart_driver)
1406 /* Look for a serialN alias */
1407 cdns_uart_data->id = of_alias_get_id(pdev->dev.of_node, "serial");
1408 if (cdns_uart_data->id < 0)
1409 cdns_uart_data->id = 0;
1411 if (cdns_uart_data->id >= CDNS_UART_NR_PORTS) {
1412 dev_err(&pdev->dev, "Cannot get uart_port structure\n");
1416 /* There is a need to use unique driver name */
1417 driver_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s%d",
1418 CDNS_UART_NAME, cdns_uart_data->id);
1422 cdns_uart_uart_driver->owner = THIS_MODULE;
1423 cdns_uart_uart_driver->driver_name = driver_name;
1424 cdns_uart_uart_driver->dev_name = CDNS_UART_TTY_NAME;
1425 cdns_uart_uart_driver->major = CDNS_UART_MAJOR;
1426 cdns_uart_uart_driver->minor = cdns_uart_data->id;
1427 cdns_uart_uart_driver->nr = 1;
1429 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1430 cdns_uart_console = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_console),
1432 if (!cdns_uart_console)
1435 strncpy(cdns_uart_console->name, CDNS_UART_TTY_NAME,
1436 sizeof(cdns_uart_console->name));
1437 cdns_uart_console->index = cdns_uart_data->id;
1438 cdns_uart_console->write = cdns_uart_console_write;
1439 cdns_uart_console->device = uart_console_device;
1440 cdns_uart_console->setup = cdns_uart_console_setup;
1441 cdns_uart_console->flags = CON_PRINTBUFFER;
1442 cdns_uart_console->data = cdns_uart_uart_driver;
1443 cdns_uart_uart_driver->cons = cdns_uart_console;
1446 rc = uart_register_driver(cdns_uart_uart_driver);
1448 dev_err(&pdev->dev, "Failed to register driver\n");
1452 cdns_uart_data->cdns_uart_driver = cdns_uart_uart_driver;
1455 * Setting up proper name_base needs to be done after uart
1456 * registration because tty_driver structure is not filled.
1457 * name_base is 0 by default.
1459 cdns_uart_uart_driver->tty_driver->name_base = cdns_uart_data->id;
1461 match = of_match_node(cdns_uart_of_match, pdev->dev.of_node);
1462 if (match && match->data) {
1463 const struct cdns_platform_data *data = match->data;
1465 cdns_uart_data->quirks = data->quirks;
1468 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk");
1469 if (IS_ERR(cdns_uart_data->pclk)) {
1470 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk");
1471 if (!IS_ERR(cdns_uart_data->pclk))
1472 dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n");
1474 if (IS_ERR(cdns_uart_data->pclk)) {
1475 dev_err(&pdev->dev, "pclk clock not found.\n");
1476 rc = PTR_ERR(cdns_uart_data->pclk);
1477 goto err_out_unregister_driver;
1480 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk");
1481 if (IS_ERR(cdns_uart_data->uartclk)) {
1482 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk");
1483 if (!IS_ERR(cdns_uart_data->uartclk))
1484 dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n");
1486 if (IS_ERR(cdns_uart_data->uartclk)) {
1487 dev_err(&pdev->dev, "uart_clk clock not found.\n");
1488 rc = PTR_ERR(cdns_uart_data->uartclk);
1489 goto err_out_unregister_driver;
1492 rc = clk_prepare_enable(cdns_uart_data->pclk);
1494 dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
1495 goto err_out_unregister_driver;
1497 rc = clk_prepare_enable(cdns_uart_data->uartclk);
1499 dev_err(&pdev->dev, "Unable to enable device clock.\n");
1500 goto err_out_clk_dis_pclk;
1503 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1506 goto err_out_clk_disable;
1509 irq = platform_get_irq(pdev, 0);
1512 goto err_out_clk_disable;
1515 #ifdef CONFIG_COMMON_CLK
1516 cdns_uart_data->clk_rate_change_nb.notifier_call =
1517 cdns_uart_clk_notifier_cb;
1518 if (clk_notifier_register(cdns_uart_data->uartclk,
1519 &cdns_uart_data->clk_rate_change_nb))
1520 dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
1523 /* At this point, we've got an empty uart_port struct, initialize it */
1524 spin_lock_init(&port->lock);
1525 port->type = PORT_UNKNOWN;
1526 port->iotype = UPIO_MEM32;
1527 port->flags = UPF_BOOT_AUTOCONF;
1528 port->ops = &cdns_uart_ops;
1529 port->fifosize = CDNS_UART_FIFO_SIZE;
1532 * Register the port.
1533 * This function also registers this device with the tty layer
1534 * and triggers invocation of the config_port() entry point.
1536 port->mapbase = res->start;
1538 port->dev = &pdev->dev;
1539 port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
1540 port->private_data = cdns_uart_data;
1541 cdns_uart_data->port = port;
1542 platform_set_drvdata(pdev, port);
1544 pm_runtime_use_autosuspend(&pdev->dev);
1545 pm_runtime_set_autosuspend_delay(&pdev->dev, UART_AUTOSUSPEND_TIMEOUT);
1546 pm_runtime_set_active(&pdev->dev);
1547 pm_runtime_enable(&pdev->dev);
1549 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1551 * If console hasn't been found yet try to assign this port
1552 * because it is required to be assigned for console setup function.
1553 * If register_console() don't assign value, then console_port pointer
1557 console_port = port;
1560 rc = uart_add_one_port(cdns_uart_uart_driver, port);
1563 "uart_add_one_port() failed; err=%i\n", rc);
1564 goto err_out_pm_disable;
1567 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1568 /* This is not port which is used for console that's why clean it up */
1569 if (console_port == port &&
1570 !(cdns_uart_uart_driver->cons->flags & CON_ENABLED))
1571 console_port = NULL;
1577 pm_runtime_disable(&pdev->dev);
1578 pm_runtime_set_suspended(&pdev->dev);
1579 pm_runtime_dont_use_autosuspend(&pdev->dev);
1580 #ifdef CONFIG_COMMON_CLK
1581 clk_notifier_unregister(cdns_uart_data->uartclk,
1582 &cdns_uart_data->clk_rate_change_nb);
1584 err_out_clk_disable:
1585 clk_disable_unprepare(cdns_uart_data->uartclk);
1586 err_out_clk_dis_pclk:
1587 clk_disable_unprepare(cdns_uart_data->pclk);
1588 err_out_unregister_driver:
1589 uart_unregister_driver(cdns_uart_data->cdns_uart_driver);
1595 * cdns_uart_remove - called when the platform driver is unregistered
1596 * @pdev: Pointer to the platform device structure
1598 * Return: 0 on success, negative errno otherwise
1600 static int cdns_uart_remove(struct platform_device *pdev)
1602 struct uart_port *port = platform_get_drvdata(pdev);
1603 struct cdns_uart *cdns_uart_data = port->private_data;
1606 /* Remove the cdns_uart port from the serial core */
1607 #ifdef CONFIG_COMMON_CLK
1608 clk_notifier_unregister(cdns_uart_data->uartclk,
1609 &cdns_uart_data->clk_rate_change_nb);
1611 rc = uart_remove_one_port(cdns_uart_data->cdns_uart_driver, port);
1613 clk_disable_unprepare(cdns_uart_data->uartclk);
1614 clk_disable_unprepare(cdns_uart_data->pclk);
1615 pm_runtime_disable(&pdev->dev);
1616 pm_runtime_set_suspended(&pdev->dev);
1617 pm_runtime_dont_use_autosuspend(&pdev->dev);
1619 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1620 if (console_port == port)
1621 console_port = NULL;
1624 uart_unregister_driver(cdns_uart_data->cdns_uart_driver);
1628 static struct platform_driver cdns_uart_platform_driver = {
1629 .probe = cdns_uart_probe,
1630 .remove = cdns_uart_remove,
1632 .name = CDNS_UART_NAME,
1633 .of_match_table = cdns_uart_of_match,
1634 .pm = &cdns_uart_dev_pm_ops,
1638 static int __init cdns_uart_init(void)
1640 /* Register the platform driver */
1641 return platform_driver_register(&cdns_uart_platform_driver);
1644 static void __exit cdns_uart_exit(void)
1646 /* Unregister the platform driver */
1647 platform_driver_unregister(&cdns_uart_platform_driver);
1650 arch_initcall(cdns_uart_init);
1651 module_exit(cdns_uart_exit);
1653 MODULE_DESCRIPTION("Driver for Cadence UART");
1654 MODULE_AUTHOR("Xilinx Inc.");
1655 MODULE_LICENSE("GPL");