2 * Copyright (C) 2012 Michal Simek <monstr@monstr.eu>
3 * Copyright (C) 2011-2012 Xilinx, Inc. All rights reserved.
5 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/compiler.h>
15 #include <asm/arch/clk.h>
16 #include <asm/arch/hardware.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 #define ZYNQ_UART_SR_TXFULL 0x00000010 /* TX FIFO full */
21 #define ZYNQ_UART_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
23 #define ZYNQ_UART_CR_TX_EN 0x00000010 /* TX enabled */
24 #define ZYNQ_UART_CR_RX_EN 0x00000004 /* RX enabled */
25 #define ZYNQ_UART_CR_TXRST 0x00000002 /* TX logic reset */
26 #define ZYNQ_UART_CR_RXRST 0x00000001 /* RX logic reset */
28 #define ZYNQ_UART_MR_PARITY_NONE 0x00000020 /* No parity mode */
31 u32 control; /* 0x0 - Control Register [8:0] */
32 u32 mode; /* 0x4 - Mode Register [10:0] */
34 u32 baud_rate_gen; /* 0x18 - Baud Rate Generator [15:0] */
36 u32 channel_sts; /* 0x2c - Channel Status [11:0] */
37 u32 tx_rx_fifo; /* 0x30 - FIFO [15:0] or [7:0] */
38 u32 baud_rate_divider; /* 0x34 - Baud Rate Divider [7:0] */
41 static struct uart_zynq *uart_zynq_ports[2] = {
42 [0] = (struct uart_zynq *)ZYNQ_SERIAL_BASEADDR0,
43 [1] = (struct uart_zynq *)ZYNQ_SERIAL_BASEADDR1,
46 /* Set up the baud rate in gd struct */
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 /* Set up the baud rate in gd struct */
88 static void uart_zynq_serial_setbrg(const int port)
90 unsigned long clock = get_uart_clk(port);
91 struct uart_zynq *regs = uart_zynq_ports[port];
93 return _uart_zynq_serial_setbrg(regs, clock, gd->baudrate);
96 /* Initialize the UART, with...some settings. */
97 static void _uart_zynq_serial_init(struct uart_zynq *regs)
99 /* RX/TX enabled & reset */
100 writel(ZYNQ_UART_CR_TX_EN | ZYNQ_UART_CR_RX_EN | ZYNQ_UART_CR_TXRST | \
101 ZYNQ_UART_CR_RXRST, ®s->control);
102 writel(ZYNQ_UART_MR_PARITY_NONE, ®s->mode); /* 8 bit, no parity */
105 /* Initialize the UART, with...some settings. */
106 static int uart_zynq_serial_init(const int port)
108 struct uart_zynq *regs = uart_zynq_ports[port];
113 _uart_zynq_serial_init(regs);
114 uart_zynq_serial_setbrg(port);
119 static int _uart_zynq_serial_putc(struct uart_zynq *regs, const char c)
121 if (readl(®s->channel_sts) & ZYNQ_UART_SR_TXFULL)
124 writel(c, ®s->tx_rx_fifo);
129 static void uart_zynq_serial_putc(const char c, const int port)
131 struct uart_zynq *regs = uart_zynq_ports[port];
133 while (_uart_zynq_serial_putc(regs, c) == -EAGAIN)
137 while (_uart_zynq_serial_putc(regs, '\r') == -EAGAIN)
142 static void uart_zynq_serial_puts(const char *s, const int port)
145 uart_zynq_serial_putc(*s++, port);
148 static int uart_zynq_serial_tstc(const int port)
150 struct uart_zynq *regs = uart_zynq_ports[port];
152 return (readl(®s->channel_sts) & ZYNQ_UART_SR_RXEMPTY) == 0;
155 static int uart_zynq_serial_getc(const int port)
157 struct uart_zynq *regs = uart_zynq_ports[port];
159 while (!uart_zynq_serial_tstc(port))
161 return readl(®s->tx_rx_fifo);
164 /* Multi serial device functions */
165 #define DECLARE_PSSERIAL_FUNCTIONS(port) \
166 static int uart_zynq##port##_init(void) \
167 { return uart_zynq_serial_init(port); } \
168 static void uart_zynq##port##_setbrg(void) \
169 { return uart_zynq_serial_setbrg(port); } \
170 static int uart_zynq##port##_getc(void) \
171 { return uart_zynq_serial_getc(port); } \
172 static int uart_zynq##port##_tstc(void) \
173 { return uart_zynq_serial_tstc(port); } \
174 static void uart_zynq##port##_putc(const char c) \
175 { uart_zynq_serial_putc(c, port); } \
176 static void uart_zynq##port##_puts(const char *s) \
177 { uart_zynq_serial_puts(s, port); }
179 /* Serial device descriptor */
180 #define INIT_PSSERIAL_STRUCTURE(port, __name) { \
182 .start = uart_zynq##port##_init, \
184 .setbrg = uart_zynq##port##_setbrg, \
185 .getc = uart_zynq##port##_getc, \
186 .tstc = uart_zynq##port##_tstc, \
187 .putc = uart_zynq##port##_putc, \
188 .puts = uart_zynq##port##_puts, \
191 DECLARE_PSSERIAL_FUNCTIONS(0);
192 static struct serial_device uart_zynq_serial0_device =
193 INIT_PSSERIAL_STRUCTURE(0, "ttyPS0");
194 DECLARE_PSSERIAL_FUNCTIONS(1);
195 static struct serial_device uart_zynq_serial1_device =
196 INIT_PSSERIAL_STRUCTURE(1, "ttyPS1");
198 __weak struct serial_device *default_serial_console(void)
200 const void *blob = gd->fdt_blob;
202 unsigned int base_addr;
204 node = fdt_path_offset(blob, "serial0");
208 base_addr = fdtdec_get_addr(blob, node, "reg");
209 if (base_addr == FDT_ADDR_T_NONE)
212 if (base_addr == ZYNQ_SERIAL_BASEADDR0)
213 return &uart_zynq_serial0_device;
215 if (base_addr == ZYNQ_SERIAL_BASEADDR1)
216 return &uart_zynq_serial1_device;
221 void zynq_serial_initialize(void)
223 serial_register(&uart_zynq_serial0_device);
224 serial_register(&uart_zynq_serial1_device);
227 #ifdef CONFIG_DEBUG_UART_ZYNQ
229 #include <debug_uart.h>
231 void _debug_uart_init(void)
233 struct uart_zynq *regs = (struct uart_zynq *)CONFIG_DEBUG_UART_BASE;
235 _uart_zynq_serial_init(regs);
236 _uart_zynq_serial_setbrg(regs, CONFIG_DEBUG_UART_CLOCK,
240 static inline void _debug_uart_putc(int ch)
242 struct uart_zynq *regs = (struct uart_zynq *)CONFIG_DEBUG_UART_BASE;
244 while (_uart_zynq_serial_putc(regs, ch) == -EAGAIN)