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+
12 #include <linux/compiler.h>
15 #include <asm/arch/clk.h>
16 #include <asm/arch/hardware.h>
18 #include "atmel_usart.h"
20 DECLARE_GLOBAL_DATA_PTR;
22 static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id,
25 unsigned long divisor;
26 unsigned long usart_hz;
30 * Baud Rate = --------------
33 usart_hz = get_usart_clk_rate(id);
34 divisor = (usart_hz / 16 + baudrate / 2) / baudrate;
35 writel(USART3_BF(CD, divisor), &usart->brgr);
38 static void atmel_serial_init_internal(atmel_usart3_t *usart)
41 * Just in case: drain transmitter register
42 * 1000us is enough for baudrate >= 9600
44 if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
47 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
50 static void atmel_serial_activate(atmel_usart3_t *usart)
52 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
53 | USART3_BF(USCLKS, USART3_USCLKS_MCK)
54 | USART3_BF(CHRL, USART3_CHRL_8)
55 | USART3_BF(PAR, USART3_PAR_NONE)
56 | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
58 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
59 /* 100us is enough for the new settings to be settled */
63 static void atmel_serial_setbrg(void)
65 atmel_serial_setbrg_internal((atmel_usart3_t *)CONFIG_USART_BASE,
66 CONFIG_USART_ID, gd->baudrate);
69 static int atmel_serial_init(void)
71 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
73 atmel_serial_init_internal(usart);
75 atmel_serial_activate(usart);
80 static void atmel_serial_putc(char c)
82 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
87 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
88 writel(c, &usart->thr);
91 static int atmel_serial_getc(void)
93 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
95 while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
97 return readl(&usart->rhr);
100 static int atmel_serial_tstc(void)
102 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
103 return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
106 static struct serial_device atmel_serial_drv = {
107 .name = "atmel_serial",
108 .start = atmel_serial_init,
110 .setbrg = atmel_serial_setbrg,
111 .putc = atmel_serial_putc,
112 .puts = default_serial_puts,
113 .getc = atmel_serial_getc,
114 .tstc = atmel_serial_tstc,
117 void atmel_serial_initialize(void)
119 serial_register(&atmel_serial_drv);
122 __weak struct serial_device *default_serial_console(void)
124 return &atmel_serial_drv;