blob: bb79b9729579a661084425d6b3c5357da31dc3c0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Beniamino Galvanid1037e42016-05-08 08:30:16 +02002/*
3 * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
Beniamino Galvanid1037e42016-05-08 08:30:16 +02004 */
5
Beniamino Galvanid1037e42016-05-08 08:30:16 +02006#include <dm.h>
7#include <errno.h>
8#include <fdtdec.h>
Edoardo Tomelleri1a340492022-09-18 18:17:01 +02009#include <linux/kernel.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060010#include <linux/bitops.h>
Beniamino Galvanid1037e42016-05-08 08:30:16 +020011#include <linux/compiler.h>
12#include <serial.h>
Edoardo Tomelleri1a340492022-09-18 18:17:01 +020013#include <clk.h>
Beniamino Galvanid1037e42016-05-08 08:30:16 +020014
Beniamino Galvanid1037e42016-05-08 08:30:16 +020015struct meson_uart {
16 u32 wfifo;
17 u32 rfifo;
18 u32 control;
19 u32 status;
20 u32 misc;
Edoardo Tomelleri1a340492022-09-18 18:17:01 +020021 u32 reg5; /* New baud control register */
Beniamino Galvanid1037e42016-05-08 08:30:16 +020022};
23
Simon Glassb75b15b2020-12-03 16:55:23 -070024struct meson_serial_plat {
Beniamino Galvanid1037e42016-05-08 08:30:16 +020025 struct meson_uart *reg;
26};
27
28/* AML_UART_STATUS bits */
29#define AML_UART_PARITY_ERR BIT(16)
30#define AML_UART_FRAME_ERR BIT(17)
31#define AML_UART_TX_FIFO_WERR BIT(18)
32#define AML_UART_RX_EMPTY BIT(20)
33#define AML_UART_TX_FULL BIT(21)
34#define AML_UART_TX_EMPTY BIT(22)
35#define AML_UART_XMIT_BUSY BIT(25)
36#define AML_UART_ERR (AML_UART_PARITY_ERR | \
37 AML_UART_FRAME_ERR | \
38 AML_UART_TX_FIFO_WERR)
39
40/* AML_UART_CONTROL bits */
41#define AML_UART_TX_EN BIT(12)
42#define AML_UART_RX_EN BIT(13)
43#define AML_UART_TX_RST BIT(22)
44#define AML_UART_RX_RST BIT(23)
45#define AML_UART_CLR_ERR BIT(24)
46
Edoardo Tomelleri1a340492022-09-18 18:17:01 +020047/* AML_UART_REG5 bits */
48#define AML_UART_REG5_XTAL_DIV2 BIT(27)
49#define AML_UART_REG5_XTAL_CLK_SEL BIT(26) /* default 0 (div by 3), 1 for no div */
50#define AML_UART_REG5_USE_XTAL_CLK BIT(24) /* default 1 (use crystal as clock source) */
51#define AML_UART_REG5_USE_NEW_BAUD BIT(23) /* default 1 (use new baud rate register) */
52#define AML_UART_REG5_BAUD_MASK 0x7fffff
53
54static u32 meson_calc_baud_divisor(ulong src_rate, u32 baud)
55{
56 /*
57 * Usually src_rate is 24 MHz (from crystal) as clock source for serial
58 * device. Since 8 Mb/s is the maximum supported baud rate, use div by 3
59 * to derive baud rate. This choice is used also in meson_serial_setbrg.
60 */
61 return DIV_ROUND_CLOSEST(src_rate / 3, baud) - 1;
62}
63
64static void meson_serial_set_baud(struct meson_uart *uart, ulong src_rate, u32 baud)
65{
66 /*
67 * Set crystal divided by 3 (regardless of device tree clock property)
68 * as clock source and the corresponding divisor to approximate baud
69 */
70 u32 divisor = meson_calc_baud_divisor(src_rate, baud);
71 u32 val = AML_UART_REG5_USE_XTAL_CLK | AML_UART_REG5_USE_NEW_BAUD |
72 (divisor & AML_UART_REG5_BAUD_MASK);
73 writel(val, &uart->reg5);
74}
75
Beniamino Galvanid1037e42016-05-08 08:30:16 +020076static void meson_serial_init(struct meson_uart *uart)
77{
78 u32 val;
79
80 val = readl(&uart->control);
81 val |= (AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
82 writel(val, &uart->control);
83 val &= ~(AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
84 writel(val, &uart->control);
85 val |= (AML_UART_RX_EN | AML_UART_TX_EN);
86 writel(val, &uart->control);
87}
88
89static int meson_serial_probe(struct udevice *dev)
90{
Simon Glass95588622020-12-22 19:30:28 -070091 struct meson_serial_plat *plat = dev_get_plat(dev);
Beniamino Galvanid1037e42016-05-08 08:30:16 +020092 struct meson_uart *const uart = plat->reg;
Edoardo Tomelleri1a340492022-09-18 18:17:01 +020093 struct clk per_clk;
94 int ret = clk_get_by_name(dev, "baud", &per_clk);
Beniamino Galvanid1037e42016-05-08 08:30:16 +020095
Edoardo Tomelleri1a340492022-09-18 18:17:01 +020096 if (ret)
97 return ret;
98 ulong rate = clk_get_rate(&per_clk);
99
100 meson_serial_set_baud(uart, rate, CONFIG_BAUDRATE);
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200101 meson_serial_init(uart);
102
103 return 0;
104}
105
Neil Armstrong5018be72020-07-21 13:41:14 +0200106static void meson_serial_rx_error(struct udevice *dev)
107{
Simon Glass95588622020-12-22 19:30:28 -0700108 struct meson_serial_plat *plat = dev_get_plat(dev);
Neil Armstrong5018be72020-07-21 13:41:14 +0200109 struct meson_uart *const uart = plat->reg;
110 u32 val = readl(&uart->control);
111
112 /* Clear error */
113 val |= AML_UART_CLR_ERR;
114 writel(val, &uart->control);
115 val &= ~AML_UART_CLR_ERR;
116 writel(val, &uart->control);
117
118 /* Remove spurious byte from fifo */
119 readl(&uart->rfifo);
120}
121
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200122static int meson_serial_getc(struct udevice *dev)
123{
Simon Glass95588622020-12-22 19:30:28 -0700124 struct meson_serial_plat *plat = dev_get_plat(dev);
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200125 struct meson_uart *const uart = plat->reg;
Neil Armstrong5018be72020-07-21 13:41:14 +0200126 uint32_t status = readl(&uart->status);
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200127
Neil Armstrong5018be72020-07-21 13:41:14 +0200128 if (status & AML_UART_RX_EMPTY)
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200129 return -EAGAIN;
130
Neil Armstrong5018be72020-07-21 13:41:14 +0200131 if (status & AML_UART_ERR) {
132 meson_serial_rx_error(dev);
133 return -EIO;
134 }
135
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200136 return readl(&uart->rfifo) & 0xff;
137}
138
139static int meson_serial_putc(struct udevice *dev, const char ch)
140{
Simon Glass95588622020-12-22 19:30:28 -0700141 struct meson_serial_plat *plat = dev_get_plat(dev);
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200142 struct meson_uart *const uart = plat->reg;
143
144 if (readl(&uart->status) & AML_UART_TX_FULL)
145 return -EAGAIN;
146
147 writel(ch, &uart->wfifo);
148
149 return 0;
150}
151
Edoardo Tomelleri1a340492022-09-18 18:17:01 +0200152static int meson_serial_setbrg(struct udevice *dev, const int baud)
153{
154 /*
155 * Change device baud rate if baud is reasonable (considering a 23 bit
156 * counter with an 8 MHz clock input) and the actual baud
157 * rate is within 2% of the requested value (2% is arbitrary).
158 */
159 if (baud < 1 || baud > 8000000)
160 return -EINVAL;
161
162 struct meson_serial_plat *const plat = dev_get_plat(dev);
163 struct meson_uart *const uart = plat->reg;
164 struct clk per_clk;
165 int ret = clk_get_by_name(dev, "baud", &per_clk);
166
167 if (ret)
168 return ret;
169 ulong rate = clk_get_rate(&per_clk);
170 u32 divisor = meson_calc_baud_divisor(rate, baud);
171 u32 calc_baud = (rate / 3) / (divisor + 1);
172 u32 calc_err = baud > calc_baud ? baud - calc_baud : calc_baud - baud;
173
174 if (((calc_err * 100) / baud) > 2)
175 return -EINVAL;
176
177 meson_serial_set_baud(uart, rate, baud);
178
179 return 0;
180}
181
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200182static int meson_serial_pending(struct udevice *dev, bool input)
183{
Simon Glass95588622020-12-22 19:30:28 -0700184 struct meson_serial_plat *plat = dev_get_plat(dev);
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200185 struct meson_uart *const uart = plat->reg;
186 uint32_t status = readl(&uart->status);
187
Neil Armstrong5018be72020-07-21 13:41:14 +0200188 if (input) {
189 if (status & AML_UART_RX_EMPTY)
190 return false;
191
192 /*
193 * Handle and drop any RX error here to avoid
194 * returning true here when an error byte is in the FIFO
195 */
196 if (status & AML_UART_ERR) {
197 meson_serial_rx_error(dev);
198 return false;
199 }
200
201 return true;
202 } else {
Mattijs Korpershoeka9183eb2023-06-06 18:07:48 +0200203 if (status & AML_UART_TX_EMPTY)
204 return false;
205
206 return true;
Neil Armstrong5018be72020-07-21 13:41:14 +0200207 }
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200208}
209
Simon Glassaad29ae2020-12-03 16:55:21 -0700210static int meson_serial_of_to_plat(struct udevice *dev)
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200211{
Simon Glass95588622020-12-22 19:30:28 -0700212 struct meson_serial_plat *plat = dev_get_plat(dev);
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200213 fdt_addr_t addr;
214
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900215 addr = dev_read_addr(dev);
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200216 if (addr == FDT_ADDR_T_NONE)
217 return -EINVAL;
218
219 plat->reg = (struct meson_uart *)addr;
220
221 return 0;
222}
223
224static const struct dm_serial_ops meson_serial_ops = {
225 .putc = meson_serial_putc,
226 .pending = meson_serial_pending,
227 .getc = meson_serial_getc,
Edoardo Tomelleri1a340492022-09-18 18:17:01 +0200228 .setbrg = meson_serial_setbrg,
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200229};
230
231static const struct udevice_id meson_serial_ids[] = {
232 { .compatible = "amlogic,meson-uart" },
Neil Armstrongd5a22452018-03-29 14:56:02 +0200233 { .compatible = "amlogic,meson-gx-uart" },
Igor Prusov8be6c9c2023-10-18 00:32:10 +0300234 { .compatible = "amlogic,meson-a1-uart" },
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200235 { }
236};
237
238U_BOOT_DRIVER(serial_meson) = {
239 .name = "serial_meson",
240 .id = UCLASS_SERIAL,
241 .of_match = meson_serial_ids,
242 .probe = meson_serial_probe,
243 .ops = &meson_serial_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700244 .of_to_plat = meson_serial_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700245 .plat_auto = sizeof(struct meson_serial_plat),
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200246};
247
248#ifdef CONFIG_DEBUG_UART_MESON
249
250#include <debug_uart.h>
251
252static inline void _debug_uart_init(void)
253{
254}
255
256static inline void _debug_uart_putc(int ch)
257{
Pali Rohár8864b352022-05-27 22:15:24 +0200258 struct meson_uart *regs = (struct meson_uart *)CONFIG_VAL(DEBUG_UART_BASE);
Beniamino Galvanid1037e42016-05-08 08:30:16 +0200259
260 while (readl(&regs->status) & AML_UART_TX_FULL)
261 ;
262
263 writel(ch, &regs->wfifo);
264}
265
266DEBUG_UART_FUNCS
267
268#endif