1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2004-2006 Atmel Corporation
5 * Modified to support C structur SoC access by
15 #include <debug_uart.h>
16 #include <linux/compiler.h>
17 #include <linux/delay.h>
20 #ifdef CONFIG_DM_SERIAL
21 #include <asm/arch/atmel_serial.h>
23 #include <asm/arch/clk.h>
24 #include <asm/arch/hardware.h>
26 #include "atmel_usart.h"
28 DECLARE_GLOBAL_DATA_PTR;
30 #ifndef CONFIG_DM_SERIAL
31 static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id,
34 unsigned long divisor;
35 unsigned long usart_hz;
39 * Baud Rate = --------------
42 usart_hz = get_usart_clk_rate(id);
43 divisor = (usart_hz / 16 + baudrate / 2) / baudrate;
44 writel(USART3_BF(CD, divisor), &usart->brgr);
47 static void atmel_serial_init_internal(atmel_usart3_t *usart)
50 * Just in case: drain transmitter register
51 * 1000us is enough for baudrate >= 9600
53 if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
56 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
59 static void atmel_serial_activate(atmel_usart3_t *usart)
61 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
62 | USART3_BF(USCLKS, USART3_USCLKS_MCK)
63 | USART3_BF(CHRL, USART3_CHRL_8)
64 | USART3_BF(PAR, USART3_PAR_NONE)
65 | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
67 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
68 /* 100us is enough for the new settings to be settled */
72 static void atmel_serial_setbrg(void)
74 atmel_serial_setbrg_internal((atmel_usart3_t *)CONFIG_USART_BASE,
75 CONFIG_USART_ID, gd->baudrate);
78 static int atmel_serial_init(void)
80 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
82 atmel_serial_init_internal(usart);
84 atmel_serial_activate(usart);
89 static void atmel_serial_putc(char c)
91 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
96 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
97 writel(c, &usart->thr);
100 static int atmel_serial_getc(void)
102 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
104 while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
106 return readl(&usart->rhr);
109 static int atmel_serial_tstc(void)
111 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
112 return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
115 static struct serial_device atmel_serial_drv = {
116 .name = "atmel_serial",
117 .start = atmel_serial_init,
119 .setbrg = atmel_serial_setbrg,
120 .putc = atmel_serial_putc,
121 .puts = default_serial_puts,
122 .getc = atmel_serial_getc,
123 .tstc = atmel_serial_tstc,
126 void atmel_serial_initialize(void)
128 serial_register(&atmel_serial_drv);
131 __weak struct serial_device *default_serial_console(void)
133 return &atmel_serial_drv;
137 #ifdef CONFIG_DM_SERIAL
138 enum serial_clk_type {
143 struct atmel_serial_priv {
144 atmel_usart3_t *usart;
145 ulong usart_clk_rate;
148 static void _atmel_serial_set_brg(atmel_usart3_t *usart,
149 ulong usart_clk_rate, int baudrate)
151 unsigned long divisor;
153 divisor = (usart_clk_rate / 16 + baudrate / 2) / baudrate;
154 writel(USART3_BF(CD, divisor), &usart->brgr);
157 void _atmel_serial_init(atmel_usart3_t *usart,
158 ulong usart_clk_rate, int baudrate)
160 writel(USART3_BIT(RXDIS) | USART3_BIT(TXDIS), &usart->cr);
162 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL) |
163 USART3_BF(USCLKS, USART3_USCLKS_MCK) |
164 USART3_BF(CHRL, USART3_CHRL_8) |
165 USART3_BF(PAR, USART3_PAR_NONE) |
166 USART3_BF(NBSTOP, USART3_NBSTOP_1)), &usart->mr);
168 _atmel_serial_set_brg(usart, usart_clk_rate, baudrate);
170 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
171 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
174 int atmel_serial_setbrg(struct udevice *dev, int baudrate)
176 struct atmel_serial_priv *priv = dev_get_priv(dev);
178 _atmel_serial_set_brg(priv->usart, priv->usart_clk_rate, baudrate);
183 static int atmel_serial_getc(struct udevice *dev)
185 struct atmel_serial_priv *priv = dev_get_priv(dev);
187 if (!(readl(&priv->usart->csr) & USART3_BIT(RXRDY)))
190 return readl(&priv->usart->rhr);
193 static int atmel_serial_putc(struct udevice *dev, const char ch)
195 struct atmel_serial_priv *priv = dev_get_priv(dev);
197 if (!(readl(&priv->usart->csr) & USART3_BIT(TXRDY)))
200 writel(ch, &priv->usart->thr);
205 static int atmel_serial_pending(struct udevice *dev, bool input)
207 struct atmel_serial_priv *priv = dev_get_priv(dev);
208 uint32_t csr = readl(&priv->usart->csr);
211 return csr & USART3_BIT(RXRDY) ? 1 : 0;
213 return csr & USART3_BIT(TXEMPTY) ? 0 : 1;
216 static const struct dm_serial_ops atmel_serial_ops = {
217 .putc = atmel_serial_putc,
218 .pending = atmel_serial_pending,
219 .getc = atmel_serial_getc,
220 .setbrg = atmel_serial_setbrg,
223 #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_CLK)
224 static int atmel_serial_enable_clk(struct udevice *dev)
226 struct atmel_serial_priv *priv = dev_get_priv(dev);
228 /* Use fixed clock value in SPL */
229 priv->usart_clk_rate = CONFIG_SPL_UART_CLOCK;
234 static int atmel_serial_enable_clk(struct udevice *dev)
236 struct atmel_serial_priv *priv = dev_get_priv(dev);
241 ret = clk_get_by_index(dev, 0, &clk);
245 if (dev_get_driver_data(dev) == CLK_TYPE_NORMAL) {
246 ret = clk_enable(&clk);
251 clk_rate = clk_get_rate(&clk);
255 priv->usart_clk_rate = clk_rate;
263 static int atmel_serial_probe(struct udevice *dev)
265 struct atmel_serial_platdata *plat = dev->platdata;
266 struct atmel_serial_priv *priv = dev_get_priv(dev);
268 #if CONFIG_IS_ENABLED(OF_CONTROL)
269 fdt_addr_t addr_base;
271 addr_base = dev_read_addr(dev);
272 if (addr_base == FDT_ADDR_T_NONE)
275 plat->base_addr = (uint32_t)addr_base;
277 priv->usart = (atmel_usart3_t *)plat->base_addr;
279 ret = atmel_serial_enable_clk(dev);
283 _atmel_serial_init(priv->usart, priv->usart_clk_rate, gd->baudrate);
288 #if CONFIG_IS_ENABLED(OF_CONTROL)
289 static const struct udevice_id atmel_serial_ids[] = {
291 .compatible = "atmel,at91sam9260-dbgu",
292 .data = CLK_TYPE_DBGU,
295 .compatible = "atmel,at91sam9260-usart",
296 .data = CLK_TYPE_NORMAL,
302 U_BOOT_DRIVER(serial_atmel) = {
303 .name = "serial_atmel",
305 #if CONFIG_IS_ENABLED(OF_CONTROL)
306 .of_match = atmel_serial_ids,
307 .platdata_auto_alloc_size = sizeof(struct atmel_serial_platdata),
309 .probe = atmel_serial_probe,
310 .ops = &atmel_serial_ops,
311 #if !CONFIG_IS_ENABLED(OF_CONTROL)
312 .flags = DM_FLAG_PRE_RELOC,
314 .priv_auto_alloc_size = sizeof(struct atmel_serial_priv),
318 #ifdef CONFIG_DEBUG_UART_ATMEL
319 static inline void _debug_uart_init(void)
321 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_DEBUG_UART_BASE;
323 _atmel_serial_init(usart, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
326 static inline void _debug_uart_putc(int ch)
328 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_DEBUG_UART_BASE;
330 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)))
333 writel(ch, &usart->thr);