2 * Copyright (C) 2004-2006 Atmel Corporation
4 * Modified to support C structur SoC access by
5 * Andreas Bießmann <biessmann@corscience.de>
7 * SPDX-License-Identifier: GPL-2.0+
15 #include <debug_uart.h>
16 #include <linux/compiler.h>
19 #ifdef CONFIG_DM_SERIAL
20 #include <asm/arch/atmel_serial.h>
22 #include <asm/arch/clk.h>
23 #include <asm/arch/hardware.h>
25 #include "atmel_usart.h"
27 DECLARE_GLOBAL_DATA_PTR;
29 #ifndef CONFIG_DM_SERIAL
30 static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id,
33 unsigned long divisor;
34 unsigned long usart_hz;
38 * Baud Rate = --------------
41 usart_hz = get_usart_clk_rate(id);
42 divisor = (usart_hz / 16 + baudrate / 2) / baudrate;
43 writel(USART3_BF(CD, divisor), &usart->brgr);
46 static void atmel_serial_init_internal(atmel_usart3_t *usart)
49 * Just in case: drain transmitter register
50 * 1000us is enough for baudrate >= 9600
52 if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
55 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
58 static void atmel_serial_activate(atmel_usart3_t *usart)
60 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
61 | USART3_BF(USCLKS, USART3_USCLKS_MCK)
62 | USART3_BF(CHRL, USART3_CHRL_8)
63 | USART3_BF(PAR, USART3_PAR_NONE)
64 | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
66 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
67 /* 100us is enough for the new settings to be settled */
71 static void atmel_serial_setbrg(void)
73 atmel_serial_setbrg_internal((atmel_usart3_t *)CONFIG_USART_BASE,
74 CONFIG_USART_ID, gd->baudrate);
77 static int atmel_serial_init(void)
79 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
81 atmel_serial_init_internal(usart);
83 atmel_serial_activate(usart);
88 static void atmel_serial_putc(char c)
90 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
95 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
96 writel(c, &usart->thr);
99 static int atmel_serial_getc(void)
101 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
103 while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
105 return readl(&usart->rhr);
108 static int atmel_serial_tstc(void)
110 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
111 return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
114 static struct serial_device atmel_serial_drv = {
115 .name = "atmel_serial",
116 .start = atmel_serial_init,
118 .setbrg = atmel_serial_setbrg,
119 .putc = atmel_serial_putc,
120 .puts = default_serial_puts,
121 .getc = atmel_serial_getc,
122 .tstc = atmel_serial_tstc,
125 void atmel_serial_initialize(void)
127 serial_register(&atmel_serial_drv);
130 __weak struct serial_device *default_serial_console(void)
132 return &atmel_serial_drv;
136 #ifdef CONFIG_DM_SERIAL
137 enum serial_clk_type {
142 struct atmel_serial_priv {
143 atmel_usart3_t *usart;
144 ulong usart_clk_rate;
147 static void _atmel_serial_set_brg(atmel_usart3_t *usart,
148 ulong usart_clk_rate, int baudrate)
150 unsigned long divisor;
152 divisor = (usart_clk_rate / 16 + baudrate / 2) / baudrate;
153 writel(USART3_BF(CD, divisor), &usart->brgr);
156 void _atmel_serial_init(atmel_usart3_t *usart,
157 ulong usart_clk_rate, int baudrate)
159 writel(USART3_BIT(RXDIS) | USART3_BIT(TXDIS), &usart->cr);
161 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL) |
162 USART3_BF(USCLKS, USART3_USCLKS_MCK) |
163 USART3_BF(CHRL, USART3_CHRL_8) |
164 USART3_BF(PAR, USART3_PAR_NONE) |
165 USART3_BF(NBSTOP, USART3_NBSTOP_1)), &usart->mr);
167 _atmel_serial_set_brg(usart, usart_clk_rate, baudrate);
169 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
170 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
173 int atmel_serial_setbrg(struct udevice *dev, int baudrate)
175 struct atmel_serial_priv *priv = dev_get_priv(dev);
177 _atmel_serial_set_brg(priv->usart, priv->usart_clk_rate, baudrate);
182 static int atmel_serial_getc(struct udevice *dev)
184 struct atmel_serial_priv *priv = dev_get_priv(dev);
186 if (!(readl(&priv->usart->csr) & USART3_BIT(RXRDY)))
189 return readl(&priv->usart->rhr);
192 static int atmel_serial_putc(struct udevice *dev, const char ch)
194 struct atmel_serial_priv *priv = dev_get_priv(dev);
196 if (!(readl(&priv->usart->csr) & USART3_BIT(TXRDY)))
199 writel(ch, &priv->usart->thr);
204 static int atmel_serial_pending(struct udevice *dev, bool input)
206 struct atmel_serial_priv *priv = dev_get_priv(dev);
207 uint32_t csr = readl(&priv->usart->csr);
210 return csr & USART3_BIT(RXRDY) ? 1 : 0;
212 return csr & USART3_BIT(TXEMPTY) ? 0 : 1;
215 static const struct dm_serial_ops atmel_serial_ops = {
216 .putc = atmel_serial_putc,
217 .pending = atmel_serial_pending,
218 .getc = atmel_serial_getc,
219 .setbrg = atmel_serial_setbrg,
222 static int atmel_serial_enable_clk(struct udevice *dev)
224 struct atmel_serial_priv *priv = dev_get_priv(dev);
229 ret = clk_get_by_index(dev, 0, &clk);
233 if (dev_get_driver_data(dev) == CLK_TYPE_NORMAL) {
234 ret = clk_enable(&clk);
239 clk_rate = clk_get_rate(&clk);
243 priv->usart_clk_rate = clk_rate;
250 static int atmel_serial_probe(struct udevice *dev)
252 struct atmel_serial_platdata *plat = dev->platdata;
253 struct atmel_serial_priv *priv = dev_get_priv(dev);
255 #if CONFIG_IS_ENABLED(OF_CONTROL)
256 fdt_addr_t addr_base;
258 addr_base = dev_get_addr(dev);
259 if (addr_base == FDT_ADDR_T_NONE)
262 plat->base_addr = (uint32_t)addr_base;
264 priv->usart = (atmel_usart3_t *)plat->base_addr;
266 ret = atmel_serial_enable_clk(dev);
270 _atmel_serial_init(priv->usart, priv->usart_clk_rate, gd->baudrate);
275 #if CONFIG_IS_ENABLED(OF_CONTROL)
276 static const struct udevice_id atmel_serial_ids[] = {
278 .compatible = "atmel,at91sam9260-dbgu",
279 .data = CLK_TYPE_DBGU,
282 .compatible = "atmel,at91sam9260-usart",
283 .data = CLK_TYPE_NORMAL,
289 U_BOOT_DRIVER(serial_atmel) = {
290 .name = "serial_atmel",
292 #if CONFIG_IS_ENABLED(OF_CONTROL)
293 .of_match = atmel_serial_ids,
294 .platdata_auto_alloc_size = sizeof(struct atmel_serial_platdata),
296 .probe = atmel_serial_probe,
297 .ops = &atmel_serial_ops,
298 .flags = DM_FLAG_PRE_RELOC,
299 .priv_auto_alloc_size = sizeof(struct atmel_serial_priv),
303 #ifdef CONFIG_DEBUG_UART_ATMEL
304 static inline void _debug_uart_init(void)
306 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_DEBUG_UART_BASE;
308 _atmel_serial_init(usart, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
311 static inline void _debug_uart_putc(int ch)
313 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_DEBUG_UART_BASE;
315 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)))
318 writel(ch, &usart->thr);