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+
14 #include <debug_uart.h>
15 #include <linux/compiler.h>
18 #ifdef CONFIG_DM_SERIAL
19 #include <asm/arch/atmel_serial.h>
21 #include <asm/arch/clk.h>
22 #include <asm/arch/hardware.h>
24 #include "atmel_usart.h"
26 DECLARE_GLOBAL_DATA_PTR;
28 static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id,
31 unsigned long divisor;
32 unsigned long usart_hz;
36 * Baud Rate = --------------
39 usart_hz = get_usart_clk_rate(id);
40 divisor = (usart_hz / 16 + baudrate / 2) / baudrate;
41 writel(USART3_BF(CD, divisor), &usart->brgr);
44 static void atmel_serial_init_internal(atmel_usart3_t *usart)
47 * Just in case: drain transmitter register
48 * 1000us is enough for baudrate >= 9600
50 if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
53 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
56 static void atmel_serial_activate(atmel_usart3_t *usart)
58 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
59 | USART3_BF(USCLKS, USART3_USCLKS_MCK)
60 | USART3_BF(CHRL, USART3_CHRL_8)
61 | USART3_BF(PAR, USART3_PAR_NONE)
62 | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
64 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
65 /* 100us is enough for the new settings to be settled */
69 #ifndef CONFIG_DM_SERIAL
70 static void atmel_serial_setbrg(void)
72 atmel_serial_setbrg_internal((atmel_usart3_t *)CONFIG_USART_BASE,
73 CONFIG_USART_ID, gd->baudrate);
76 static int atmel_serial_init(void)
78 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
80 atmel_serial_init_internal(usart);
82 atmel_serial_activate(usart);
87 static void atmel_serial_putc(char c)
89 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
94 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
95 writel(c, &usart->thr);
98 static int atmel_serial_getc(void)
100 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
102 while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
104 return readl(&usart->rhr);
107 static int atmel_serial_tstc(void)
109 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
110 return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
113 static struct serial_device atmel_serial_drv = {
114 .name = "atmel_serial",
115 .start = atmel_serial_init,
117 .setbrg = atmel_serial_setbrg,
118 .putc = atmel_serial_putc,
119 .puts = default_serial_puts,
120 .getc = atmel_serial_getc,
121 .tstc = atmel_serial_tstc,
124 void atmel_serial_initialize(void)
126 serial_register(&atmel_serial_drv);
129 __weak struct serial_device *default_serial_console(void)
131 return &atmel_serial_drv;
135 #ifdef CONFIG_DM_SERIAL
137 struct atmel_serial_priv {
138 atmel_usart3_t *usart;
141 int atmel_serial_setbrg(struct udevice *dev, int baudrate)
143 struct atmel_serial_priv *priv = dev_get_priv(dev);
145 atmel_serial_setbrg_internal(priv->usart, 0 /* ignored */, baudrate);
146 atmel_serial_activate(priv->usart);
151 static int atmel_serial_getc(struct udevice *dev)
153 struct atmel_serial_priv *priv = dev_get_priv(dev);
155 if (!(readl(&priv->usart->csr) & USART3_BIT(RXRDY)))
158 return readl(&priv->usart->rhr);
161 static int atmel_serial_putc(struct udevice *dev, const char ch)
163 struct atmel_serial_priv *priv = dev_get_priv(dev);
165 if (!(readl(&priv->usart->csr) & USART3_BIT(TXRDY)))
168 writel(ch, &priv->usart->thr);
173 static int atmel_serial_pending(struct udevice *dev, bool input)
175 struct atmel_serial_priv *priv = dev_get_priv(dev);
176 uint32_t csr = readl(&priv->usart->csr);
179 return csr & USART3_BIT(RXRDY) ? 1 : 0;
181 return csr & USART3_BIT(TXEMPTY) ? 0 : 1;
184 static const struct dm_serial_ops atmel_serial_ops = {
185 .putc = atmel_serial_putc,
186 .pending = atmel_serial_pending,
187 .getc = atmel_serial_getc,
188 .setbrg = atmel_serial_setbrg,
191 static int atmel_serial_probe(struct udevice *dev)
193 struct atmel_serial_platdata *plat = dev->platdata;
194 struct atmel_serial_priv *priv = dev_get_priv(dev);
195 #if CONFIG_IS_ENABLED(OF_CONTROL)
196 fdt_addr_t addr_base;
198 addr_base = dev_get_addr(dev);
199 if (addr_base == FDT_ADDR_T_NONE)
202 plat->base_addr = (uint32_t)addr_base;
204 priv->usart = (atmel_usart3_t *)plat->base_addr;
205 atmel_serial_init_internal(priv->usart);
210 #if CONFIG_IS_ENABLED(OF_CONTROL)
211 static const struct udevice_id atmel_serial_ids[] = {
212 { .compatible = "atmel,at91sam9260-usart" },
217 U_BOOT_DRIVER(serial_atmel) = {
218 .name = "serial_atmel",
220 #if CONFIG_IS_ENABLED(OF_CONTROL)
221 .of_match = atmel_serial_ids,
222 .platdata_auto_alloc_size = sizeof(struct atmel_serial_platdata),
224 .probe = atmel_serial_probe,
225 .ops = &atmel_serial_ops,
226 .flags = DM_FLAG_PRE_RELOC,
227 .priv_auto_alloc_size = sizeof(struct atmel_serial_priv),
231 #ifdef CONFIG_DEBUG_UART_ATMEL
232 static inline void _debug_uart_init(void)
234 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_DEBUG_UART_BASE;
236 atmel_serial_setbrg_internal(usart, 0, CONFIG_BAUDRATE);
239 static inline void _debug_uart_putc(int ch)
241 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_DEBUG_UART_BASE;
243 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)))
246 writel(ch, &usart->thr);