1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Anup Patel <anup@brainfault.org>
8 #include <debug_uart.h>
14 #include <asm/global_data.h>
16 #include <linux/compiler.h>
18 #include <linux/err.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 #define UART_TXFIFO_FULL 0x80000000
23 #define UART_RXFIFO_EMPTY 0x80000000
24 #define UART_RXFIFO_DATA 0x000000ff
25 #define UART_TXCTRL_TXEN 0x1
26 #define UART_RXCTRL_RXEN 0x1
29 #define UART_IP_RXWM 0x2
41 struct sifive_uart_plat {
43 struct uart_sifive *regs;
47 * Find minimum divisor divides in_freq to max_target_hz;
48 * Based on uart driver n SiFive FSBL.
50 * f_baud = f_in / (div + 1) => div = (f_in / f_baud) - 1
51 * The nearest integer solution requires rounding up as to not exceed
53 * div = ceil(f_in / f_baud) - 1
54 * = floor((f_in - 1 + f_baud) / f_baud) - 1
55 * This should not overflow as long as (f_in - 1 + f_baud) does not exceed
56 * 2^32 - 1, which is unlikely since we represent frequencies in kHz.
58 static inline unsigned int uart_min_clk_divisor(unsigned long in_freq,
59 unsigned long max_target_hz)
61 unsigned long quotient =
62 (in_freq + max_target_hz - 1) / (max_target_hz);
70 /* Set up the baud rate in gd struct */
71 static void _sifive_serial_setbrg(struct uart_sifive *regs,
72 unsigned long clock, unsigned long baud)
74 writel((uart_min_clk_divisor(clock, baud)), ®s->div);
77 static void _sifive_serial_init(struct uart_sifive *regs)
79 writel(UART_TXCTRL_TXEN, ®s->txctrl);
80 writel(UART_RXCTRL_RXEN, ®s->rxctrl);
84 static int _sifive_serial_putc(struct uart_sifive *regs, const char c)
86 if (readl(®s->txfifo) & UART_TXFIFO_FULL)
89 writel(c, ®s->txfifo);
94 static int _sifive_serial_getc(struct uart_sifive *regs)
96 int ch = readl(®s->rxfifo);
98 if (ch & UART_RXFIFO_EMPTY)
100 ch &= UART_RXFIFO_DATA;
105 static int sifive_serial_setbrg(struct udevice *dev, int baudrate)
109 struct sifive_uart_plat *plat = dev_get_plat(dev);
112 ret = clk_get_by_index(dev, 0, &clk);
113 if (IS_ERR_VALUE(ret)) {
114 debug("SiFive UART failed to get clock\n");
115 ret = dev_read_u32(dev, "clock-frequency", &clock);
116 if (IS_ERR_VALUE(ret)) {
117 debug("SiFive UART clock not defined\n");
121 clock = clk_get_rate(&clk);
122 if (IS_ERR_VALUE(clock)) {
123 debug("SiFive UART clock get rate failed\n");
128 _sifive_serial_setbrg(plat->regs, plat->clock, baudrate);
133 static int sifive_serial_probe(struct udevice *dev)
135 struct sifive_uart_plat *plat = dev_get_plat(dev);
137 /* No need to reinitialize the UART after relocation */
138 if (gd->flags & GD_FLG_RELOC)
141 _sifive_serial_init(plat->regs);
146 static int sifive_serial_getc(struct udevice *dev)
149 struct sifive_uart_plat *plat = dev_get_plat(dev);
150 struct uart_sifive *regs = plat->regs;
152 while ((c = _sifive_serial_getc(regs)) == -EAGAIN) ;
157 static int sifive_serial_putc(struct udevice *dev, const char ch)
160 struct sifive_uart_plat *plat = dev_get_plat(dev);
162 while ((rc = _sifive_serial_putc(plat->regs, ch)) == -EAGAIN) ;
167 static int sifive_serial_pending(struct udevice *dev, bool input)
169 struct sifive_uart_plat *plat = dev_get_plat(dev);
170 struct uart_sifive *regs = plat->regs;
173 return (readl(®s->ip) & UART_IP_RXWM);
175 return !!(readl(®s->txfifo) & UART_TXFIFO_FULL);
178 static int sifive_serial_of_to_plat(struct udevice *dev)
180 struct sifive_uart_plat *plat = dev_get_plat(dev);
182 plat->regs = (struct uart_sifive *)(uintptr_t)dev_read_addr(dev);
183 if (IS_ERR(plat->regs))
184 return PTR_ERR(plat->regs);
189 static const struct dm_serial_ops sifive_serial_ops = {
190 .putc = sifive_serial_putc,
191 .getc = sifive_serial_getc,
192 .pending = sifive_serial_pending,
193 .setbrg = sifive_serial_setbrg,
196 static const struct udevice_id sifive_serial_ids[] = {
197 { .compatible = "sifive,uart0" },
201 U_BOOT_DRIVER(serial_sifive) = {
202 .name = "serial_sifive",
204 .of_match = sifive_serial_ids,
205 .of_to_plat = sifive_serial_of_to_plat,
206 .plat_auto = sizeof(struct sifive_uart_plat),
207 .probe = sifive_serial_probe,
208 .ops = &sifive_serial_ops,
211 #ifdef CONFIG_DEBUG_UART_SIFIVE
212 static inline void _debug_uart_init(void)
214 struct uart_sifive *regs =
215 (struct uart_sifive *)CONFIG_DEBUG_UART_BASE;
217 _sifive_serial_setbrg(regs, CONFIG_DEBUG_UART_CLOCK,
219 _sifive_serial_init(regs);
222 static inline void _debug_uart_putc(int ch)
224 struct uart_sifive *regs =
225 (struct uart_sifive *)CONFIG_DEBUG_UART_BASE;
227 while (_sifive_serial_putc(regs, ch) == -EAGAIN)