blob: ee5d561bfd195f620f68d6bb93fe9250b59224f5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Álvaro Fernández Rojas30581042017-04-25 00:39:16 +02002/*
3 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
4 *
5 * Derived from linux/drivers/tty/serial/bcm63xx_uart.c:
6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
Álvaro Fernández Rojas30581042017-04-25 00:39:16 +02007 */
8
9#include <clk.h>
Simon Glass9d922452017-05-17 17:18:03 -060010#include <dm.h>
Álvaro Fernández Rojas30581042017-04-25 00:39:16 +020011#include <debug_uart.h>
12#include <errno.h>
13#include <serial.h>
14#include <asm/io.h>
15#include <asm/types.h>
Álvaro Fernández Rojas30581042017-04-25 00:39:16 +020016
17/* UART Control register */
18#define UART_CTL_REG 0x0
19#define UART_CTL_RXTIMEOUT_MASK 0x1f
20#define UART_CTL_RXTIMEOUT_5 0x5
21#define UART_CTL_RSTRXFIFO_SHIFT 6
22#define UART_CTL_RSTRXFIFO_MASK (1 << UART_CTL_RSTRXFIFO_SHIFT)
23#define UART_CTL_RSTTXFIFO_SHIFT 7
24#define UART_CTL_RSTTXFIFO_MASK (1 << UART_CTL_RSTTXFIFO_SHIFT)
25#define UART_CTL_STOPBITS_SHIFT 8
26#define UART_CTL_STOPBITS_MASK (0xf << UART_CTL_STOPBITS_SHIFT)
27#define UART_CTL_STOPBITS_1 (0x7 << UART_CTL_STOPBITS_SHIFT)
28#define UART_CTL_BITSPERSYM_SHIFT 12
29#define UART_CTL_BITSPERSYM_MASK (0x3 << UART_CTL_BITSPERSYM_SHIFT)
30#define UART_CTL_BITSPERSYM_8 (0x3 << UART_CTL_BITSPERSYM_SHIFT)
31#define UART_CTL_XMITBRK_SHIFT 14
32#define UART_CTL_XMITBRK_MASK (1 << UART_CTL_XMITBRK_SHIFT)
33#define UART_CTL_RSVD_SHIFT 15
34#define UART_CTL_RSVD_MASK (1 << UART_CTL_RSVD_SHIFT)
35#define UART_CTL_RXPAREVEN_SHIFT 16
36#define UART_CTL_RXPAREVEN_MASK (1 << UART_CTL_RXPAREVEN_SHIFT)
37#define UART_CTL_RXPAREN_SHIFT 17
38#define UART_CTL_RXPAREN_MASK (1 << UART_CTL_RXPAREN_SHIFT)
39#define UART_CTL_TXPAREVEN_SHIFT 18
40#define UART_CTL_TXPAREVEN_MASK (1 << UART_CTL_TXPAREVEN_SHIFT)
41#define UART_CTL_TXPAREN_SHIFT 19
42#define UART_CTL_TXPAREN_MASK (1 << UART_CTL_TXPAREN_SHIFT)
43#define UART_CTL_LOOPBACK_SHIFT 20
44#define UART_CTL_LOOPBACK_MASK (1 << UART_CTL_LOOPBACK_SHIFT)
45#define UART_CTL_RXEN_SHIFT 21
46#define UART_CTL_RXEN_MASK (1 << UART_CTL_RXEN_SHIFT)
47#define UART_CTL_TXEN_SHIFT 22
48#define UART_CTL_TXEN_MASK (1 << UART_CTL_TXEN_SHIFT)
49#define UART_CTL_BRGEN_SHIFT 23
50#define UART_CTL_BRGEN_MASK (1 << UART_CTL_BRGEN_SHIFT)
51
52/* UART Baudword register */
53#define UART_BAUD_REG 0x4
54
55/* UART FIFO Config register */
56#define UART_FIFO_CFG_REG 0x8
57#define UART_FIFO_CFG_RX_SHIFT 8
58#define UART_FIFO_CFG_RX_MASK (0xf << UART_FIFO_CFG_RX_SHIFT)
59#define UART_FIFO_CFG_RX_4 (0x4 << UART_FIFO_CFG_RX_SHIFT)
60#define UART_FIFO_CFG_TX_SHIFT 12
61#define UART_FIFO_CFG_TX_MASK (0xf << UART_FIFO_CFG_TX_SHIFT)
62#define UART_FIFO_CFG_TX_4 (0x4 << UART_FIFO_CFG_TX_SHIFT)
63
64/* UART Interrupt register */
65#define UART_IR_REG 0x10
66#define UART_IR_STAT(x) (1 << (x))
67#define UART_IR_TXEMPTY 5
68#define UART_IR_RXOVER 7
69#define UART_IR_RXNOTEMPTY 11
70
71/* UART FIFO register */
72#define UART_FIFO_REG 0x14
73#define UART_FIFO_VALID_MASK 0xff
74#define UART_FIFO_FRAMEERR_SHIFT 8
75#define UART_FIFO_FRAMEERR_MASK (1 << UART_FIFO_FRAMEERR_SHIFT)
76#define UART_FIFO_PARERR_SHIFT 9
77#define UART_FIFO_PARERR_MASK (1 << UART_FIFO_PARERR_SHIFT)
78#define UART_FIFO_BRKDET_SHIFT 10
79#define UART_FIFO_BRKDET_MASK (1 << UART_FIFO_BRKDET_SHIFT)
80#define UART_FIFO_ANYERR_MASK (UART_FIFO_FRAMEERR_MASK | \
81 UART_FIFO_PARERR_MASK | \
82 UART_FIFO_BRKDET_MASK)
83
84struct bcm6345_serial_priv {
85 void __iomem *base;
86 ulong uartclk;
87};
88
89/* enable rx & tx operation on uart */
90static void bcm6345_serial_enable(void __iomem *base)
91{
92 setbits_be32(base + UART_CTL_REG, UART_CTL_BRGEN_MASK |
93 UART_CTL_TXEN_MASK | UART_CTL_RXEN_MASK);
94}
95
96/* disable rx & tx operation on uart */
97static void bcm6345_serial_disable(void __iomem *base)
98{
99 clrbits_be32(base + UART_CTL_REG, UART_CTL_BRGEN_MASK |
100 UART_CTL_TXEN_MASK | UART_CTL_RXEN_MASK);
101}
102
103/* clear all unread data in rx fifo and unsent data in tx fifo */
104static void bcm6345_serial_flush(void __iomem *base)
105{
106 /* empty rx and tx fifo */
107 setbits_be32(base + UART_CTL_REG, UART_CTL_RSTRXFIFO_MASK |
108 UART_CTL_RSTTXFIFO_MASK);
109
110 /* read any pending char to make sure all irq status are cleared */
111 readl_be(base + UART_FIFO_REG);
112}
113
114static int bcm6345_serial_init(void __iomem *base, ulong clk, u32 baudrate)
115{
116 u32 val;
117
118 /* mask all irq and flush port */
119 bcm6345_serial_disable(base);
120 bcm6345_serial_flush(base);
121
122 /* set uart control config */
123 clrsetbits_be32(base + UART_CTL_REG,
124 /* clear rx timeout */
125 UART_CTL_RXTIMEOUT_MASK |
126 /* clear stop bits */
127 UART_CTL_STOPBITS_MASK |
128 /* clear bits per symbol */
129 UART_CTL_BITSPERSYM_MASK |
130 /* clear xmit break */
131 UART_CTL_XMITBRK_MASK |
132 /* clear reserved bit */
133 UART_CTL_RSVD_MASK |
134 /* disable parity */
135 UART_CTL_RXPAREN_MASK |
136 UART_CTL_TXPAREN_MASK |
137 /* disable loopback */
138 UART_CTL_LOOPBACK_MASK,
139 /* set timeout to 5 */
140 UART_CTL_RXTIMEOUT_5 |
141 /* set 8 bits/symbol */
142 UART_CTL_BITSPERSYM_8 |
Álvaro Fernández Rojas6b7185f2017-05-18 23:09:45 +0200143 /* set 1 stop bit */
144 UART_CTL_STOPBITS_1 |
Álvaro Fernández Rojas30581042017-04-25 00:39:16 +0200145 /* set parity to even */
146 UART_CTL_RXPAREVEN_MASK |
147 UART_CTL_TXPAREVEN_MASK);
148
149 /* set uart fifo config */
150 clrsetbits_be32(base + UART_FIFO_CFG_REG,
151 /* clear fifo config */
152 UART_FIFO_CFG_RX_MASK |
153 UART_FIFO_CFG_TX_MASK,
154 /* set fifo config to 4 */
155 UART_FIFO_CFG_RX_4 |
156 UART_FIFO_CFG_TX_4);
157
158 /* set baud rate */
Álvaro Fernández Rojas24f85482017-05-22 20:01:46 +0200159 val = ((clk / baudrate) >> 4);
Álvaro Fernández Rojas30581042017-04-25 00:39:16 +0200160 if (val & 0x1)
Álvaro Fernández Rojas24f85482017-05-22 20:01:46 +0200161 val = (val >> 1);
Álvaro Fernández Rojas30581042017-04-25 00:39:16 +0200162 else
Álvaro Fernández Rojas24f85482017-05-22 20:01:46 +0200163 val = (val >> 1) - 1;
Álvaro Fernández Rojas30581042017-04-25 00:39:16 +0200164 writel_be(val, base + UART_BAUD_REG);
165
166 /* clear interrupts */
167 writel_be(0, base + UART_IR_REG);
168
169 /* enable uart */
170 bcm6345_serial_enable(base);
171
172 return 0;
173}
174
175static int bcm6345_serial_pending(struct udevice *dev, bool input)
176{
177 struct bcm6345_serial_priv *priv = dev_get_priv(dev);
178 u32 val = readl_be(priv->base + UART_IR_REG);
179
180 if (input)
181 return !!(val & UART_IR_STAT(UART_IR_RXNOTEMPTY));
182 else
183 return !(val & UART_IR_STAT(UART_IR_TXEMPTY));
184}
185
186static int bcm6345_serial_setbrg(struct udevice *dev, int baudrate)
187{
188 struct bcm6345_serial_priv *priv = dev_get_priv(dev);
189
190 return bcm6345_serial_init(priv->base, priv->uartclk, baudrate);
191}
192
193static int bcm6345_serial_putc(struct udevice *dev, const char ch)
194{
195 struct bcm6345_serial_priv *priv = dev_get_priv(dev);
196 u32 val;
197
198 val = readl_be(priv->base + UART_IR_REG);
199 if (!(val & UART_IR_STAT(UART_IR_TXEMPTY)))
200 return -EAGAIN;
201
202 writel_be(ch, priv->base + UART_FIFO_REG);
203
204 return 0;
205}
206
207static int bcm6345_serial_getc(struct udevice *dev)
208{
209 struct bcm6345_serial_priv *priv = dev_get_priv(dev);
210 u32 val;
211
212 val = readl_be(priv->base + UART_IR_REG);
213 if (val & UART_IR_STAT(UART_IR_RXOVER))
214 setbits_be32(priv->base + UART_CTL_REG,
215 UART_CTL_RSTRXFIFO_MASK);
216 if (!(val & UART_IR_STAT(UART_IR_RXNOTEMPTY)))
217 return -EAGAIN;
218
219 val = readl_be(priv->base + UART_FIFO_REG);
220 if (val & UART_FIFO_ANYERR_MASK)
221 return -EAGAIN;
222
223 return val & UART_FIFO_VALID_MASK;
224}
225
226static int bcm6345_serial_probe(struct udevice *dev)
227{
228 struct bcm6345_serial_priv *priv = dev_get_priv(dev);
229 struct clk clk;
Álvaro Fernández Rojas30581042017-04-25 00:39:16 +0200230 int ret;
231
232 /* get address */
Álvaro Fernández Rojas0f569932018-03-22 19:39:29 +0100233 priv->base = dev_remap_addr(dev);
234 if (!priv->base)
Álvaro Fernández Rojas30581042017-04-25 00:39:16 +0200235 return -EINVAL;
236
Álvaro Fernández Rojas30581042017-04-25 00:39:16 +0200237 /* get clock rate */
238 ret = clk_get_by_index(dev, 0, &clk);
239 if (ret < 0)
240 return ret;
Álvaro Fernández Rojas24f85482017-05-22 20:01:46 +0200241 priv->uartclk = clk_get_rate(&clk);
Álvaro Fernández Rojas30581042017-04-25 00:39:16 +0200242 clk_free(&clk);
243
244 /* initialize serial */
245 return bcm6345_serial_init(priv->base, priv->uartclk, CONFIG_BAUDRATE);
246}
247
248static const struct dm_serial_ops bcm6345_serial_ops = {
249 .putc = bcm6345_serial_putc,
250 .pending = bcm6345_serial_pending,
251 .getc = bcm6345_serial_getc,
252 .setbrg = bcm6345_serial_setbrg,
253};
254
255static const struct udevice_id bcm6345_serial_ids[] = {
256 { .compatible = "brcm,bcm6345-uart" },
257 { /* sentinel */ }
258};
259
260U_BOOT_DRIVER(bcm6345_serial) = {
261 .name = "bcm6345-uart",
262 .id = UCLASS_SERIAL,
263 .of_match = bcm6345_serial_ids,
264 .probe = bcm6345_serial_probe,
265 .priv_auto_alloc_size = sizeof(struct bcm6345_serial_priv),
266 .ops = &bcm6345_serial_ops,
267 .flags = DM_FLAG_PRE_RELOC,
268};
269
270#ifdef CONFIG_DEBUG_UART_BCM6345
271static inline void _debug_uart_init(void)
272{
273 void __iomem *base = (void __iomem *)CONFIG_DEBUG_UART_BASE;
274
275 bcm6345_serial_init(base, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
276}
277
278static inline void wait_xfered(void __iomem *base)
279{
280 do {
281 u32 val = readl_be(base + UART_IR_REG);
282 if (val & UART_IR_STAT(UART_IR_TXEMPTY))
283 break;
284 } while (1);
285}
286
287static inline void _debug_uart_putc(int ch)
288{
289 void __iomem *base = (void __iomem *)CONFIG_DEBUG_UART_BASE;
290
291 wait_xfered(base);
292 writel_be(ch, base + UART_FIFO_REG);
293 wait_xfered(base);
294}
295
296DEBUG_UART_FUNCS
297#endif