blob: 5a02f0c8feb5b5e46b010cee702791716d8055ff [file] [log] [blame]
Anup Patel4b1d68f2018-12-15 11:35:15 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Anup Patel <anup@brainfault.org>
4 */
5
Anup Patel4b1d68f2018-12-15 11:35:15 +05306#include <common.h>
Jagan Teki9ee724f2019-05-08 19:56:16 +05307#include <clk.h>
Anup Patel4b1d68f2018-12-15 11:35:15 +05308#include <debug_uart.h>
9#include <dm.h>
10#include <errno.h>
11#include <fdtdec.h>
12#include <watchdog.h>
13#include <asm/io.h>
14#include <linux/compiler.h>
15#include <serial.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <linux/err.h>
Anup Patel4b1d68f2018-12-15 11:35:15 +053017
18DECLARE_GLOBAL_DATA_PTR;
19
20#define UART_TXFIFO_FULL 0x80000000
21#define UART_RXFIFO_EMPTY 0x80000000
22#define UART_RXFIFO_DATA 0x000000ff
23#define UART_TXCTRL_TXEN 0x1
24#define UART_RXCTRL_RXEN 0x1
25
Sagar Shrikant Kadam024d0602019-07-09 05:23:44 -070026/* IP register */
27#define UART_IP_RXWM 0x2
28
Anup Patel4b1d68f2018-12-15 11:35:15 +053029struct uart_sifive {
30 u32 txfifo;
31 u32 rxfifo;
32 u32 txctrl;
33 u32 rxctrl;
34 u32 ie;
35 u32 ip;
36 u32 div;
37};
38
39struct sifive_uart_platdata {
Atish Patrabc922d42019-02-25 08:15:02 +000040 unsigned long clock;
Anup Patel4b1d68f2018-12-15 11:35:15 +053041 struct uart_sifive *regs;
42};
43
Atish Patrabc922d42019-02-25 08:15:02 +000044/**
45 * Find minimum divisor divides in_freq to max_target_hz;
46 * Based on uart driver n SiFive FSBL.
47 *
48 * f_baud = f_in / (div + 1) => div = (f_in / f_baud) - 1
49 * The nearest integer solution requires rounding up as to not exceed
50 * max_target_hz.
51 * div = ceil(f_in / f_baud) - 1
52 * = floor((f_in - 1 + f_baud) / f_baud) - 1
53 * This should not overflow as long as (f_in - 1 + f_baud) does not exceed
54 * 2^32 - 1, which is unlikely since we represent frequencies in kHz.
55 */
56static inline unsigned int uart_min_clk_divisor(unsigned long in_freq,
57 unsigned long max_target_hz)
58{
59 unsigned long quotient =
60 (in_freq + max_target_hz - 1) / (max_target_hz);
61 /* Avoid underflow */
62 if (quotient == 0)
63 return 0;
64 else
65 return quotient - 1;
66}
67
Anup Patel4b1d68f2018-12-15 11:35:15 +053068/* Set up the baud rate in gd struct */
69static void _sifive_serial_setbrg(struct uart_sifive *regs,
70 unsigned long clock, unsigned long baud)
71{
Atish Patrabc922d42019-02-25 08:15:02 +000072 writel((uart_min_clk_divisor(clock, baud)), &regs->div);
Anup Patel4b1d68f2018-12-15 11:35:15 +053073}
74
75static void _sifive_serial_init(struct uart_sifive *regs)
76{
77 writel(UART_TXCTRL_TXEN, &regs->txctrl);
78 writel(UART_RXCTRL_RXEN, &regs->rxctrl);
79 writel(0, &regs->ie);
80}
81
82static int _sifive_serial_putc(struct uart_sifive *regs, const char c)
83{
84 if (readl(&regs->txfifo) & UART_TXFIFO_FULL)
85 return -EAGAIN;
86
87 writel(c, &regs->txfifo);
88
89 return 0;
90}
91
92static int _sifive_serial_getc(struct uart_sifive *regs)
93{
94 int ch = readl(&regs->rxfifo);
95
96 if (ch & UART_RXFIFO_EMPTY)
97 return -EAGAIN;
98 ch &= UART_RXFIFO_DATA;
99
Sagar Shrikant Kadam024d0602019-07-09 05:23:44 -0700100 return ch;
Anup Patel4b1d68f2018-12-15 11:35:15 +0530101}
102
103static int sifive_serial_setbrg(struct udevice *dev, int baudrate)
104{
Atish Patra574fa692019-02-25 08:15:08 +0000105 int ret;
Anup Patel4b1d68f2018-12-15 11:35:15 +0530106 struct clk clk;
107 struct sifive_uart_platdata *platdata = dev_get_platdata(dev);
Atish Patra574fa692019-02-25 08:15:08 +0000108 u32 clock = 0;
Anup Patel4b1d68f2018-12-15 11:35:15 +0530109
Atish Patra574fa692019-02-25 08:15:08 +0000110 ret = clk_get_by_index(dev, 0, &clk);
111 if (IS_ERR_VALUE(ret)) {
Anup Patel4b1d68f2018-12-15 11:35:15 +0530112 debug("SiFive UART failed to get clock\n");
Atish Patra574fa692019-02-25 08:15:08 +0000113 ret = dev_read_u32(dev, "clock-frequency", &clock);
114 if (IS_ERR_VALUE(ret)) {
115 debug("SiFive UART clock not defined\n");
116 return 0;
117 }
118 } else {
119 clock = clk_get_rate(&clk);
120 if (IS_ERR_VALUE(clock)) {
121 debug("SiFive UART clock get rate failed\n");
122 return 0;
123 }
Anup Patel4b1d68f2018-12-15 11:35:15 +0530124 }
Atish Patra574fa692019-02-25 08:15:08 +0000125 platdata->clock = clock;
Anup Patel4b1d68f2018-12-15 11:35:15 +0530126 _sifive_serial_setbrg(platdata->regs, platdata->clock, baudrate);
127
128 return 0;
129}
130
131static int sifive_serial_probe(struct udevice *dev)
132{
133 struct sifive_uart_platdata *platdata = dev_get_platdata(dev);
134
135 /* No need to reinitialize the UART after relocation */
136 if (gd->flags & GD_FLG_RELOC)
137 return 0;
138
Anup Patel4b1d68f2018-12-15 11:35:15 +0530139 _sifive_serial_init(platdata->regs);
140
141 return 0;
142}
143
144static int sifive_serial_getc(struct udevice *dev)
145{
146 int c;
147 struct sifive_uart_platdata *platdata = dev_get_platdata(dev);
148 struct uart_sifive *regs = platdata->regs;
149
Anup Patel4b1d68f2018-12-15 11:35:15 +0530150 while ((c = _sifive_serial_getc(regs)) == -EAGAIN) ;
151
152 return c;
153}
154
155static int sifive_serial_putc(struct udevice *dev, const char ch)
156{
157 int rc;
158 struct sifive_uart_platdata *platdata = dev_get_platdata(dev);
159
160 while ((rc = _sifive_serial_putc(platdata->regs, ch)) == -EAGAIN) ;
161
162 return rc;
163}
164
165static int sifive_serial_pending(struct udevice *dev, bool input)
166{
167 struct sifive_uart_platdata *platdata = dev_get_platdata(dev);
168 struct uart_sifive *regs = platdata->regs;
169
Sagar Shrikant Kadam024d0602019-07-09 05:23:44 -0700170 if (input)
171 return (readl(&regs->ip) & UART_IP_RXWM);
172 else
Anup Patel4b1d68f2018-12-15 11:35:15 +0530173 return !!(readl(&regs->txfifo) & UART_TXFIFO_FULL);
Anup Patel4b1d68f2018-12-15 11:35:15 +0530174}
175
176static int sifive_serial_ofdata_to_platdata(struct udevice *dev)
177{
178 struct sifive_uart_platdata *platdata = dev_get_platdata(dev);
179
180 platdata->regs = (struct uart_sifive *)dev_read_addr(dev);
181 if (IS_ERR(platdata->regs))
182 return PTR_ERR(platdata->regs);
183
184 return 0;
185}
186
187static const struct dm_serial_ops sifive_serial_ops = {
188 .putc = sifive_serial_putc,
189 .getc = sifive_serial_getc,
190 .pending = sifive_serial_pending,
191 .setbrg = sifive_serial_setbrg,
192};
193
194static const struct udevice_id sifive_serial_ids[] = {
195 { .compatible = "sifive,uart0" },
196 { }
197};
198
199U_BOOT_DRIVER(serial_sifive) = {
200 .name = "serial_sifive",
201 .id = UCLASS_SERIAL,
202 .of_match = sifive_serial_ids,
203 .ofdata_to_platdata = sifive_serial_ofdata_to_platdata,
204 .platdata_auto_alloc_size = sizeof(struct sifive_uart_platdata),
205 .probe = sifive_serial_probe,
206 .ops = &sifive_serial_ops,
207};
208
209#ifdef CONFIG_DEBUG_UART_SIFIVE
210static inline void _debug_uart_init(void)
211{
212 struct uart_sifive *regs =
213 (struct uart_sifive *)CONFIG_DEBUG_UART_BASE;
214
215 _sifive_serial_setbrg(regs, CONFIG_DEBUG_UART_CLOCK,
216 CONFIG_BAUDRATE);
217 _sifive_serial_init(regs);
218}
219
220static inline void _debug_uart_putc(int ch)
221{
222 struct uart_sifive *regs =
223 (struct uart_sifive *)CONFIG_DEBUG_UART_BASE;
224
225 while (_sifive_serial_putc(regs, ch) == -EAGAIN)
226 WATCHDOG_RESET();
227}
228
229DEBUG_UART_FUNCS
230
231#endif