1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2004-2006 Atmel Corporation
5 * Modified to support C structur SoC access by
6 * Andreas Bießmann <biessmann@corscience.de>
15 #include <debug_uart.h>
16 #include <asm/global_data.h>
17 #include <linux/compiler.h>
18 #include <linux/delay.h>
21 #ifdef CONFIG_DM_SERIAL
22 #include <asm/arch/atmel_serial.h>
24 #include <asm/arch/clk.h>
25 #include <asm/arch/hardware.h>
27 #include "atmel_usart.h"
29 DECLARE_GLOBAL_DATA_PTR;
31 #ifndef CONFIG_DM_SERIAL
32 static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id,
35 unsigned long divisor;
36 unsigned long usart_hz;
40 * Baud Rate = --------------
43 usart_hz = get_usart_clk_rate(id);
44 divisor = (usart_hz / 16 + baudrate / 2) / baudrate;
45 writel(USART3_BF(CD, divisor), &usart->brgr);
48 static void atmel_serial_init_internal(atmel_usart3_t *usart)
51 * Just in case: drain transmitter register
52 * 1000us is enough for baudrate >= 9600
54 if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
57 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
60 static void atmel_serial_activate(atmel_usart3_t *usart)
62 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
63 | USART3_BF(USCLKS, USART3_USCLKS_MCK)
64 | USART3_BF(CHRL, USART3_CHRL_8)
65 | USART3_BF(PAR, USART3_PAR_NONE)
66 | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
68 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
69 /* 100us is enough for the new settings to be settled */
73 static void atmel_serial_setbrg(void)
75 atmel_serial_setbrg_internal((atmel_usart3_t *)CONFIG_USART_BASE,
76 CONFIG_USART_ID, gd->baudrate);
79 static int atmel_serial_init(void)
81 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
83 atmel_serial_init_internal(usart);
85 atmel_serial_activate(usart);
90 static void atmel_serial_putc(char c)
92 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
97 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
98 writel(c, &usart->thr);
101 static int atmel_serial_getc(void)
103 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
105 while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
107 return readl(&usart->rhr);
110 static int atmel_serial_tstc(void)
112 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
113 return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
116 static struct serial_device atmel_serial_drv = {
117 .name = "atmel_serial",
118 .start = atmel_serial_init,
120 .setbrg = atmel_serial_setbrg,
121 .putc = atmel_serial_putc,
122 .puts = default_serial_puts,
123 .getc = atmel_serial_getc,
124 .tstc = atmel_serial_tstc,
127 void atmel_serial_initialize(void)
129 serial_register(&atmel_serial_drv);
132 __weak struct serial_device *default_serial_console(void)
134 return &atmel_serial_drv;
138 #ifdef CONFIG_DM_SERIAL
139 enum serial_clk_type {
144 struct atmel_serial_priv {
145 atmel_usart3_t *usart;
146 ulong usart_clk_rate;
149 static void _atmel_serial_set_brg(atmel_usart3_t *usart,
150 ulong usart_clk_rate, int baudrate)
152 unsigned long divisor;
154 divisor = (usart_clk_rate / 16 + baudrate / 2) / baudrate;
155 writel(USART3_BF(CD, divisor), &usart->brgr);
158 void _atmel_serial_init(atmel_usart3_t *usart,
159 ulong usart_clk_rate, int baudrate)
161 writel(USART3_BIT(RXDIS) | USART3_BIT(TXDIS), &usart->cr);
163 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL) |
164 USART3_BF(USCLKS, USART3_USCLKS_MCK) |
165 USART3_BF(CHRL, USART3_CHRL_8) |
166 USART3_BF(PAR, USART3_PAR_NONE) |
167 USART3_BF(NBSTOP, USART3_NBSTOP_1)), &usart->mr);
169 _atmel_serial_set_brg(usart, usart_clk_rate, baudrate);
171 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
172 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
175 int atmel_serial_setbrg(struct udevice *dev, int baudrate)
177 struct atmel_serial_priv *priv = dev_get_priv(dev);
179 _atmel_serial_set_brg(priv->usart, priv->usart_clk_rate, baudrate);
184 static int atmel_serial_getc(struct udevice *dev)
186 struct atmel_serial_priv *priv = dev_get_priv(dev);
188 if (!(readl(&priv->usart->csr) & USART3_BIT(RXRDY)))
191 return readl(&priv->usart->rhr);
194 static int atmel_serial_putc(struct udevice *dev, const char ch)
196 struct atmel_serial_priv *priv = dev_get_priv(dev);
198 if (!(readl(&priv->usart->csr) & USART3_BIT(TXRDY)))
201 writel(ch, &priv->usart->thr);
206 static int atmel_serial_pending(struct udevice *dev, bool input)
208 struct atmel_serial_priv *priv = dev_get_priv(dev);
209 uint32_t csr = readl(&priv->usart->csr);
212 return csr & USART3_BIT(RXRDY) ? 1 : 0;
214 return csr & USART3_BIT(TXEMPTY) ? 0 : 1;
217 static const struct dm_serial_ops atmel_serial_ops = {
218 .putc = atmel_serial_putc,
219 .pending = atmel_serial_pending,
220 .getc = atmel_serial_getc,
221 .setbrg = atmel_serial_setbrg,
224 #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_CLK)
225 static int atmel_serial_enable_clk(struct udevice *dev)
227 struct atmel_serial_priv *priv = dev_get_priv(dev);
229 /* Use fixed clock value in SPL */
230 priv->usart_clk_rate = CONFIG_SPL_UART_CLOCK;
235 static int atmel_serial_enable_clk(struct udevice *dev)
237 struct atmel_serial_priv *priv = dev_get_priv(dev);
242 ret = clk_get_by_index(dev, 0, &clk);
246 if (dev_get_driver_data(dev) == CLK_TYPE_NORMAL) {
247 ret = clk_enable(&clk);
252 clk_rate = clk_get_rate(&clk);
256 priv->usart_clk_rate = clk_rate;
264 static int atmel_serial_probe(struct udevice *dev)
266 struct atmel_serial_plat *plat = dev_get_plat(dev);
267 struct atmel_serial_priv *priv = dev_get_priv(dev);
269 #if CONFIG_IS_ENABLED(OF_CONTROL)
270 fdt_addr_t addr_base;
272 addr_base = dev_read_addr(dev);
273 if (addr_base == FDT_ADDR_T_NONE)
276 plat->base_addr = (uint32_t)addr_base;
278 priv->usart = (atmel_usart3_t *)plat->base_addr;
280 ret = atmel_serial_enable_clk(dev);
284 _atmel_serial_init(priv->usart, priv->usart_clk_rate, gd->baudrate);
289 #if CONFIG_IS_ENABLED(OF_CONTROL)
290 static const struct udevice_id atmel_serial_ids[] = {
292 .compatible = "atmel,at91sam9260-dbgu",
293 .data = CLK_TYPE_DBGU,
296 .compatible = "atmel,at91sam9260-usart",
297 .data = CLK_TYPE_NORMAL,
303 U_BOOT_DRIVER(serial_atmel) = {
304 .name = "serial_atmel",
306 #if CONFIG_IS_ENABLED(OF_CONTROL)
307 .of_match = atmel_serial_ids,
308 .plat_auto = sizeof(struct atmel_serial_plat),
310 .probe = atmel_serial_probe,
311 .ops = &atmel_serial_ops,
312 #if !CONFIG_IS_ENABLED(OF_CONTROL)
313 .flags = DM_FLAG_PRE_RELOC,
315 .priv_auto = sizeof(struct atmel_serial_priv),
319 #ifdef CONFIG_DEBUG_UART_ATMEL
320 static inline void _debug_uart_init(void)
322 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_DEBUG_UART_BASE;
324 _atmel_serial_init(usart, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
327 static inline void _debug_uart_putc(int ch)
329 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_DEBUG_UART_BASE;
331 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)))
334 writel(ch, &usart->thr);