]> Git Repo - J-u-boot.git/blame - drivers/serial/atmel_usart.c
Merge patch series "*** Commonize board code for K3 based SoMs ***"
[J-u-boot.git] / drivers / serial / atmel_usart.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
f93ae788
WD
2/*
3 * Copyright (C) 2004-2006 Atmel Corporation
4 *
125637c5
AB
5 * Modified to support C structur SoC access by
6 * Andreas Bießmann <[email protected]>
f93ae788 7 */
f8b7fff1 8#include <clk.h>
0f65f48b
SG
9#include <dm.h>
10#include <errno.h>
336d4615 11#include <malloc.h>
843a2654 12#include <watchdog.h>
cfba4573 13#include <serial.h>
998cf3c2 14#include <debug_uart.h>
401d1c4f 15#include <asm/global_data.h>
cfba4573 16#include <linux/compiler.h>
c05ed00a 17#include <linux/delay.h>
f93ae788 18
f93ae788 19#include <asm/io.h>
0478dac6 20#if CONFIG_IS_ENABLED(DM_SERIAL)
0f65f48b
SG
21#include <asm/arch/atmel_serial.h>
22#endif
df548d3c 23#include <asm/arch/clk.h>
329f0f52 24#include <asm/arch/hardware.h>
f93ae788
WD
25
26#include "atmel_usart.h"
27
28DECLARE_GLOBAL_DATA_PTR;
29
0478dac6 30#if !CONFIG_IS_ENABLED(DM_SERIAL)
62137fc0
SG
31static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id,
32 int baudrate)
f93ae788
WD
33{
34 unsigned long divisor;
35 unsigned long usart_hz;
36
37 /*
38 * Master Clock
39 * Baud Rate = --------------
40 * 16 * CD
41 */
62137fc0
SG
42 usart_hz = get_usart_clk_rate(id);
43 divisor = (usart_hz / 16 + baudrate / 2) / baudrate;
125637c5 44 writel(USART3_BF(CD, divisor), &usart->brgr);
f93ae788
WD
45}
46
62137fc0 47static void atmel_serial_init_internal(atmel_usart3_t *usart)
f93ae788 48{
1f4faedd
XH
49 /*
50 * Just in case: drain transmitter register
51 * 1000us is enough for baudrate >= 9600
52 */
53 if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
54 __udelay(1000);
55
125637c5 56 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
62137fc0 57}
f93ae788 58
62137fc0
SG
59static void atmel_serial_activate(atmel_usart3_t *usart)
60{
125637c5 61 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
df548d3c
HS
62 | USART3_BF(USCLKS, USART3_USCLKS_MCK)
63 | USART3_BF(CHRL, USART3_CHRL_8)
64 | USART3_BF(PAR, USART3_PAR_NONE)
125637c5
AB
65 | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
66 &usart->mr);
1f4faedd
XH
67 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
68 /* 100us is enough for the new settings to be settled */
69 __udelay(100);
62137fc0
SG
70}
71
72static void atmel_serial_setbrg(void)
73{
805482d1 74 atmel_serial_setbrg_internal((atmel_usart3_t *)CFG_USART_BASE,
61693acb 75 CFG_USART_ID, gd->baudrate);
62137fc0
SG
76}
77
78static int atmel_serial_init(void)
79{
805482d1 80 atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
62137fc0
SG
81
82 atmel_serial_init_internal(usart);
83 serial_setbrg();
84 atmel_serial_activate(usart);
f93ae788
WD
85
86 return 0;
87}
88
cfba4573 89static void atmel_serial_putc(char c)
f93ae788 90{
805482d1 91 atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
125637c5 92
f93ae788
WD
93 if (c == '\n')
94 serial_putc('\r');
95
125637c5
AB
96 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
97 writel(c, &usart->thr);
f93ae788
WD
98}
99
cfba4573 100static int atmel_serial_getc(void)
f93ae788 101{
805482d1 102 atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
125637c5
AB
103
104 while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
29caf930 105 schedule();
125637c5 106 return readl(&usart->rhr);
f93ae788
WD
107}
108
cfba4573 109static int atmel_serial_tstc(void)
f93ae788 110{
805482d1 111 atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
125637c5 112 return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
f93ae788 113}
cfba4573 114
cfba4573
MV
115static struct serial_device atmel_serial_drv = {
116 .name = "atmel_serial",
117 .start = atmel_serial_init,
118 .stop = NULL,
119 .setbrg = atmel_serial_setbrg,
120 .putc = atmel_serial_putc,
ec3fd689 121 .puts = default_serial_puts,
cfba4573
MV
122 .getc = atmel_serial_getc,
123 .tstc = atmel_serial_tstc,
124};
125
126void atmel_serial_initialize(void)
127{
128 serial_register(&atmel_serial_drv);
129}
130
131__weak struct serial_device *default_serial_console(void)
132{
133 return &atmel_serial_drv;
134}
0478dac6 135#else
f8b7fff1
WY
136enum serial_clk_type {
137 CLK_TYPE_NORMAL = 0,
138 CLK_TYPE_DBGU,
139};
0f65f48b
SG
140
141struct atmel_serial_priv {
142 atmel_usart3_t *usart;
f8b7fff1 143 ulong usart_clk_rate;
0f65f48b
SG
144};
145
5f29e799
WY
146static void _atmel_serial_set_brg(atmel_usart3_t *usart,
147 ulong usart_clk_rate, int baudrate)
148{
149 unsigned long divisor;
150
151 divisor = (usart_clk_rate / 16 + baudrate / 2) / baudrate;
152 writel(USART3_BF(CD, divisor), &usart->brgr);
153}
154
155void _atmel_serial_init(atmel_usart3_t *usart,
156 ulong usart_clk_rate, int baudrate)
157{
158 writel(USART3_BIT(RXDIS) | USART3_BIT(TXDIS), &usart->cr);
159
160 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL) |
161 USART3_BF(USCLKS, USART3_USCLKS_MCK) |
162 USART3_BF(CHRL, USART3_CHRL_8) |
163 USART3_BF(PAR, USART3_PAR_NONE) |
164 USART3_BF(NBSTOP, USART3_NBSTOP_1)), &usart->mr);
165
166 _atmel_serial_set_brg(usart, usart_clk_rate, baudrate);
167
168 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
169 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
170}
171
0f65f48b
SG
172int atmel_serial_setbrg(struct udevice *dev, int baudrate)
173{
174 struct atmel_serial_priv *priv = dev_get_priv(dev);
175
f8b7fff1 176 _atmel_serial_set_brg(priv->usart, priv->usart_clk_rate, baudrate);
0f65f48b
SG
177
178 return 0;
179}
180
181static int atmel_serial_getc(struct udevice *dev)
182{
183 struct atmel_serial_priv *priv = dev_get_priv(dev);
184
185 if (!(readl(&priv->usart->csr) & USART3_BIT(RXRDY)))
186 return -EAGAIN;
187
188 return readl(&priv->usart->rhr);
189}
190
191static int atmel_serial_putc(struct udevice *dev, const char ch)
192{
193 struct atmel_serial_priv *priv = dev_get_priv(dev);
194
195 if (!(readl(&priv->usart->csr) & USART3_BIT(TXRDY)))
196 return -EAGAIN;
197
198 writel(ch, &priv->usart->thr);
199
200 return 0;
201}
202
203static int atmel_serial_pending(struct udevice *dev, bool input)
204{
205 struct atmel_serial_priv *priv = dev_get_priv(dev);
206 uint32_t csr = readl(&priv->usart->csr);
207
208 if (input)
209 return csr & USART3_BIT(RXRDY) ? 1 : 0;
210 else
211 return csr & USART3_BIT(TXEMPTY) ? 0 : 1;
212}
213
214static const struct dm_serial_ops atmel_serial_ops = {
215 .putc = atmel_serial_putc,
216 .pending = atmel_serial_pending,
217 .getc = atmel_serial_getc,
218 .setbrg = atmel_serial_setbrg,
219};
220
e567dfb2
SR
221#if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_CLK)
222static int atmel_serial_enable_clk(struct udevice *dev)
223{
224 struct atmel_serial_priv *priv = dev_get_priv(dev);
225
226 /* Use fixed clock value in SPL */
227 priv->usart_clk_rate = CONFIG_SPL_UART_CLOCK;
228
229 return 0;
230}
231#else
f8b7fff1
WY
232static int atmel_serial_enable_clk(struct udevice *dev)
233{
234 struct atmel_serial_priv *priv = dev_get_priv(dev);
235 struct clk clk;
236 ulong clk_rate;
237 int ret;
238
239 ret = clk_get_by_index(dev, 0, &clk);
240 if (ret)
241 return -EINVAL;
242
243 if (dev_get_driver_data(dev) == CLK_TYPE_NORMAL) {
244 ret = clk_enable(&clk);
245 if (ret)
246 return ret;
247 }
248
249 clk_rate = clk_get_rate(&clk);
250 if (!clk_rate)
251 return -EINVAL;
252
253 priv->usart_clk_rate = clk_rate;
254
f8b7fff1
WY
255 return 0;
256}
e567dfb2 257#endif
f8b7fff1 258
0f65f48b
SG
259static int atmel_serial_probe(struct udevice *dev)
260{
0fd3d911 261 struct atmel_serial_plat *plat = dev_get_plat(dev);
0f65f48b 262 struct atmel_serial_priv *priv = dev_get_priv(dev);
f8b7fff1 263 int ret;
c1631c8a
WY
264#if CONFIG_IS_ENABLED(OF_CONTROL)
265 fdt_addr_t addr_base;
0f65f48b 266
2548493a 267 addr_base = dev_read_addr(dev);
c1631c8a
WY
268 if (addr_base == FDT_ADDR_T_NONE)
269 return -ENODEV;
270
271 plat->base_addr = (uint32_t)addr_base;
272#endif
0f65f48b 273 priv->usart = (atmel_usart3_t *)plat->base_addr;
5f29e799 274
f8b7fff1
WY
275 ret = atmel_serial_enable_clk(dev);
276 if (ret)
277 return ret;
278
279 _atmel_serial_init(priv->usart, priv->usart_clk_rate, gd->baudrate);
0f65f48b
SG
280
281 return 0;
282}
283
c1631c8a
WY
284#if CONFIG_IS_ENABLED(OF_CONTROL)
285static const struct udevice_id atmel_serial_ids[] = {
f8b7fff1
WY
286 {
287 .compatible = "atmel,at91sam9260-dbgu",
288 .data = CLK_TYPE_DBGU,
289 },
290 {
291 .compatible = "atmel,at91sam9260-usart",
292 .data = CLK_TYPE_NORMAL,
293 },
c1631c8a
WY
294 { }
295};
296#endif
297
0f65f48b
SG
298U_BOOT_DRIVER(serial_atmel) = {
299 .name = "serial_atmel",
300 .id = UCLASS_SERIAL,
c1631c8a
WY
301#if CONFIG_IS_ENABLED(OF_CONTROL)
302 .of_match = atmel_serial_ids,
8a8d24bd 303 .plat_auto = sizeof(struct atmel_serial_plat),
c1631c8a 304#endif
0f65f48b
SG
305 .probe = atmel_serial_probe,
306 .ops = &atmel_serial_ops,
46879196 307#if !CONFIG_IS_ENABLED(OF_CONTROL)
0f65f48b 308 .flags = DM_FLAG_PRE_RELOC,
46879196 309#endif
41575d8e 310 .priv_auto = sizeof(struct atmel_serial_priv),
0f65f48b
SG
311};
312#endif
998cf3c2
WY
313
314#ifdef CONFIG_DEBUG_UART_ATMEL
315static inline void _debug_uart_init(void)
316{
b62450cf 317 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_VAL(DEBUG_UART_BASE);
998cf3c2 318
5f29e799 319 _atmel_serial_init(usart, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
998cf3c2
WY
320}
321
322static inline void _debug_uart_putc(int ch)
323{
b62450cf 324 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_VAL(DEBUG_UART_BASE);
998cf3c2
WY
325
326 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)))
327 ;
328
329 writel(ch, &usart->thr);
330}
331
332DEBUG_UART_FUNCS
333#endif
This page took 0.614848 seconds and 4 git commands to generate.