1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2012 Michal Simek <monstr@monstr.eu>
4 * Copyright (C) 2011-2012 Xilinx, Inc. All rights reserved.
9 #include <debug_uart.h>
15 #include <dm/device_compat.h>
16 #include <linux/compiler.h>
18 #include <linux/err.h>
20 #define ZYNQ_UART_SR_TXACTIVE BIT(11) /* TX active */
21 #define ZYNQ_UART_SR_TXFULL BIT(4) /* TX FIFO full */
22 #define ZYNQ_UART_SR_RXEMPTY BIT(1) /* RX FIFO empty */
24 #define ZYNQ_UART_CR_TX_EN BIT(4) /* TX enabled */
25 #define ZYNQ_UART_CR_RX_EN BIT(2) /* RX enabled */
26 #define ZYNQ_UART_CR_TXRST BIT(1) /* TX logic reset */
27 #define ZYNQ_UART_CR_RXRST BIT(0) /* RX logic reset */
29 #define ZYNQ_UART_MR_PARITY_NONE 0x00000020 /* No parity mode */
32 u32 control; /* 0x0 - Control Register [8:0] */
33 u32 mode; /* 0x4 - Mode Register [10:0] */
35 u32 baud_rate_gen; /* 0x18 - Baud Rate Generator [15:0] */
37 u32 channel_sts; /* 0x2c - Channel Status [11:0] */
38 u32 tx_rx_fifo; /* 0x30 - FIFO [15:0] or [7:0] */
39 u32 baud_rate_divider; /* 0x34 - Baud Rate Divider [7:0] */
42 struct zynq_uart_platdata {
43 struct uart_zynq *regs;
46 /* Set up the baud rate */
47 static void _uart_zynq_serial_setbrg(struct uart_zynq *regs,
48 unsigned long clock, unsigned long baud)
50 /* Calculation results. */
51 unsigned int calc_bauderror, bdiv, bgen;
52 unsigned long calc_baud = 0;
54 /* Covering case where input clock is so slow */
55 if (clock < 1000000 && baud > 4800)
59 * Baud rate = ------------------
62 * Find acceptable values for baud generation.
64 for (bdiv = 4; bdiv < 255; bdiv++) {
65 bgen = clock / (baud * (bdiv + 1));
66 if (bgen < 2 || bgen > 65535)
69 calc_baud = clock / (bgen * (bdiv + 1));
72 * Use first calculated baudrate with
73 * an acceptable (<3%) error
76 calc_bauderror = baud - calc_baud;
78 calc_bauderror = calc_baud - baud;
79 if (((calc_bauderror * 100) / baud) < 3)
83 writel(bdiv, ®s->baud_rate_divider);
84 writel(bgen, ®s->baud_rate_gen);
87 /* Initialize the UART, with...some settings. */
88 static void _uart_zynq_serial_init(struct uart_zynq *regs)
90 /* RX/TX enabled & reset */
91 writel(ZYNQ_UART_CR_TX_EN | ZYNQ_UART_CR_RX_EN | ZYNQ_UART_CR_TXRST | \
92 ZYNQ_UART_CR_RXRST, ®s->control);
93 writel(ZYNQ_UART_MR_PARITY_NONE, ®s->mode); /* 8 bit, no parity */
96 static int _uart_zynq_serial_putc(struct uart_zynq *regs, const char c)
98 if (readl(®s->channel_sts) & ZYNQ_UART_SR_TXFULL)
101 writel(c, ®s->tx_rx_fifo);
106 static int zynq_serial_setbrg(struct udevice *dev, int baudrate)
108 struct zynq_uart_platdata *platdata = dev_get_platdata(dev);
114 ret = clk_get_by_index(dev, 0, &clk);
116 dev_err(dev, "failed to get clock\n");
120 clock = clk_get_rate(&clk);
121 if (IS_ERR_VALUE(clock)) {
122 dev_err(dev, "failed to get rate\n");
125 debug("%s: CLK %ld\n", __func__, clock);
127 ret = clk_enable(&clk);
128 if (ret && ret != -ENOSYS) {
129 dev_err(dev, "failed to enable clock\n");
133 _uart_zynq_serial_setbrg(platdata->regs, clock, baudrate);
138 static int zynq_serial_probe(struct udevice *dev)
140 struct zynq_uart_platdata *platdata = dev_get_platdata(dev);
141 struct uart_zynq *regs = platdata->regs;
144 /* No need to reinitialize the UART if TX already enabled */
145 val = readl(®s->control);
146 if (val & ZYNQ_UART_CR_TX_EN)
149 _uart_zynq_serial_init(platdata->regs);
154 static int zynq_serial_getc(struct udevice *dev)
156 struct zynq_uart_platdata *platdata = dev_get_platdata(dev);
157 struct uart_zynq *regs = platdata->regs;
159 if (readl(®s->channel_sts) & ZYNQ_UART_SR_RXEMPTY)
162 return readl(®s->tx_rx_fifo);
165 static int zynq_serial_putc(struct udevice *dev, const char ch)
167 struct zynq_uart_platdata *platdata = dev_get_platdata(dev);
169 return _uart_zynq_serial_putc(platdata->regs, ch);
172 static int zynq_serial_pending(struct udevice *dev, bool input)
174 struct zynq_uart_platdata *platdata = dev_get_platdata(dev);
175 struct uart_zynq *regs = platdata->regs;
178 return !(readl(®s->channel_sts) & ZYNQ_UART_SR_RXEMPTY);
180 return !!(readl(®s->channel_sts) & ZYNQ_UART_SR_TXACTIVE);
183 static int zynq_serial_ofdata_to_platdata(struct udevice *dev)
185 struct zynq_uart_platdata *platdata = dev_get_platdata(dev);
187 platdata->regs = (struct uart_zynq *)dev_read_addr(dev);
188 if (IS_ERR(platdata->regs))
189 return PTR_ERR(platdata->regs);
194 static const struct dm_serial_ops zynq_serial_ops = {
195 .putc = zynq_serial_putc,
196 .pending = zynq_serial_pending,
197 .getc = zynq_serial_getc,
198 .setbrg = zynq_serial_setbrg,
201 static const struct udevice_id zynq_serial_ids[] = {
202 { .compatible = "xlnx,xuartps" },
203 { .compatible = "cdns,uart-r1p8" },
204 { .compatible = "cdns,uart-r1p12" },
208 U_BOOT_DRIVER(serial_zynq) = {
209 .name = "serial_zynq",
211 .of_match = zynq_serial_ids,
212 .ofdata_to_platdata = zynq_serial_ofdata_to_platdata,
213 .platdata_auto_alloc_size = sizeof(struct zynq_uart_platdata),
214 .probe = zynq_serial_probe,
215 .ops = &zynq_serial_ops,
218 #ifdef CONFIG_DEBUG_UART_ZYNQ
219 static inline void _debug_uart_init(void)
221 struct uart_zynq *regs = (struct uart_zynq *)CONFIG_DEBUG_UART_BASE;
223 _uart_zynq_serial_init(regs);
224 _uart_zynq_serial_setbrg(regs, CONFIG_DEBUG_UART_CLOCK,
228 static inline void _debug_uart_putc(int ch)
230 struct uart_zynq *regs = (struct uart_zynq *)CONFIG_DEBUG_UART_BASE;
232 while (_uart_zynq_serial_putc(regs, ch) == -EAGAIN)