blob: 7674f97e8dde04dbf5bf4112874cdb91847d7e4d [file] [log] [blame]
Wolfgang Denkf93ae782006-10-24 14:31:24 +02001/*
2 * Copyright (C) 2004-2006 Atmel Corporation
3 *
Andreas Bießmann125637c2010-09-03 10:28:05 +02004 * Modified to support C structur SoC access by
5 * Andreas Bießmann <biessmann@corscience.de>
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Wolfgang Denkf93ae782006-10-24 14:31:24 +02008 */
9#include <common.h>
Simon Glass0f65f482014-10-29 13:09:00 -060010#include <dm.h>
11#include <errno.h>
Jean-Christophe PLAGNIOL-VILLARD843a2652009-03-27 23:26:42 +010012#include <watchdog.h>
Marek Vasutcfba4572012-09-13 16:50:30 +020013#include <serial.h>
Wenyou Yang998cf3c2016-10-17 09:49:55 +080014#include <debug_uart.h>
Marek Vasutcfba4572012-09-13 16:50:30 +020015#include <linux/compiler.h>
Wolfgang Denkf93ae782006-10-24 14:31:24 +020016
Wolfgang Denkf93ae782006-10-24 14:31:24 +020017#include <asm/io.h>
Simon Glass0f65f482014-10-29 13:09:00 -060018#ifdef CONFIG_DM_SERIAL
19#include <asm/arch/atmel_serial.h>
20#endif
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010021#include <asm/arch/clk.h>
Reinhard Meyer329f0f52010-11-03 16:32:56 +010022#include <asm/arch/hardware.h>
Wolfgang Denkf93ae782006-10-24 14:31:24 +020023
24#include "atmel_usart.h"
25
26DECLARE_GLOBAL_DATA_PTR;
27
Simon Glass62137fc2014-10-29 13:08:59 -060028static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id,
29 int baudrate)
Wolfgang Denkf93ae782006-10-24 14:31:24 +020030{
31 unsigned long divisor;
32 unsigned long usart_hz;
33
34 /*
35 * Master Clock
36 * Baud Rate = --------------
37 * 16 * CD
38 */
Simon Glass62137fc2014-10-29 13:08:59 -060039 usart_hz = get_usart_clk_rate(id);
40 divisor = (usart_hz / 16 + baudrate / 2) / baudrate;
Andreas Bießmann125637c2010-09-03 10:28:05 +020041 writel(USART3_BF(CD, divisor), &usart->brgr);
Wolfgang Denkf93ae782006-10-24 14:31:24 +020042}
43
Simon Glass62137fc2014-10-29 13:08:59 -060044static void atmel_serial_init_internal(atmel_usart3_t *usart)
Wolfgang Denkf93ae782006-10-24 14:31:24 +020045{
Xu, Hong1f4faed2011-08-02 01:05:04 +000046 /*
47 * Just in case: drain transmitter register
48 * 1000us is enough for baudrate >= 9600
49 */
50 if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
51 __udelay(1000);
52
Andreas Bießmann125637c2010-09-03 10:28:05 +020053 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
Simon Glass62137fc2014-10-29 13:08:59 -060054}
Wolfgang Denkf93ae782006-10-24 14:31:24 +020055
Simon Glass62137fc2014-10-29 13:08:59 -060056static void atmel_serial_activate(atmel_usart3_t *usart)
57{
Andreas Bießmann125637c2010-09-03 10:28:05 +020058 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010059 | USART3_BF(USCLKS, USART3_USCLKS_MCK)
60 | USART3_BF(CHRL, USART3_CHRL_8)
61 | USART3_BF(PAR, USART3_PAR_NONE)
Andreas Bießmann125637c2010-09-03 10:28:05 +020062 | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
63 &usart->mr);
Xu, Hong1f4faed2011-08-02 01:05:04 +000064 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
65 /* 100us is enough for the new settings to be settled */
66 __udelay(100);
Simon Glass62137fc2014-10-29 13:08:59 -060067}
68
Simon Glass0f65f482014-10-29 13:09:00 -060069#ifndef CONFIG_DM_SERIAL
Simon Glass62137fc2014-10-29 13:08:59 -060070static void atmel_serial_setbrg(void)
71{
72 atmel_serial_setbrg_internal((atmel_usart3_t *)CONFIG_USART_BASE,
73 CONFIG_USART_ID, gd->baudrate);
74}
75
76static int atmel_serial_init(void)
77{
78 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
79
80 atmel_serial_init_internal(usart);
81 serial_setbrg();
82 atmel_serial_activate(usart);
Wolfgang Denkf93ae782006-10-24 14:31:24 +020083
84 return 0;
85}
86
Marek Vasutcfba4572012-09-13 16:50:30 +020087static void atmel_serial_putc(char c)
Wolfgang Denkf93ae782006-10-24 14:31:24 +020088{
Reinhard Meyer329f0f52010-11-03 16:32:56 +010089 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
Andreas Bießmann125637c2010-09-03 10:28:05 +020090
Wolfgang Denkf93ae782006-10-24 14:31:24 +020091 if (c == '\n')
92 serial_putc('\r');
93
Andreas Bießmann125637c2010-09-03 10:28:05 +020094 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
95 writel(c, &usart->thr);
Wolfgang Denkf93ae782006-10-24 14:31:24 +020096}
97
Marek Vasutcfba4572012-09-13 16:50:30 +020098static int atmel_serial_getc(void)
Wolfgang Denkf93ae782006-10-24 14:31:24 +020099{
Reinhard Meyer329f0f52010-11-03 16:32:56 +0100100 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
Andreas Bießmann125637c2010-09-03 10:28:05 +0200101
102 while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
Jean-Christophe PLAGNIOL-VILLARD843a2652009-03-27 23:26:42 +0100103 WATCHDOG_RESET();
Andreas Bießmann125637c2010-09-03 10:28:05 +0200104 return readl(&usart->rhr);
Wolfgang Denkf93ae782006-10-24 14:31:24 +0200105}
106
Marek Vasutcfba4572012-09-13 16:50:30 +0200107static int atmel_serial_tstc(void)
Wolfgang Denkf93ae782006-10-24 14:31:24 +0200108{
Reinhard Meyer329f0f52010-11-03 16:32:56 +0100109 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
Andreas Bießmann125637c2010-09-03 10:28:05 +0200110 return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
Wolfgang Denkf93ae782006-10-24 14:31:24 +0200111}
Marek Vasutcfba4572012-09-13 16:50:30 +0200112
Marek Vasutcfba4572012-09-13 16:50:30 +0200113static struct serial_device atmel_serial_drv = {
114 .name = "atmel_serial",
115 .start = atmel_serial_init,
116 .stop = NULL,
117 .setbrg = atmel_serial_setbrg,
118 .putc = atmel_serial_putc,
Marek Vasutec3fd682012-10-06 14:07:02 +0000119 .puts = default_serial_puts,
Marek Vasutcfba4572012-09-13 16:50:30 +0200120 .getc = atmel_serial_getc,
121 .tstc = atmel_serial_tstc,
122};
123
124void atmel_serial_initialize(void)
125{
126 serial_register(&atmel_serial_drv);
127}
128
129__weak struct serial_device *default_serial_console(void)
130{
131 return &atmel_serial_drv;
132}
Simon Glass0f65f482014-10-29 13:09:00 -0600133#endif
134
135#ifdef CONFIG_DM_SERIAL
136
137struct atmel_serial_priv {
138 atmel_usart3_t *usart;
139};
140
141int atmel_serial_setbrg(struct udevice *dev, int baudrate)
142{
143 struct atmel_serial_priv *priv = dev_get_priv(dev);
144
145 atmel_serial_setbrg_internal(priv->usart, 0 /* ignored */, baudrate);
146 atmel_serial_activate(priv->usart);
147
148 return 0;
149}
150
151static int atmel_serial_getc(struct udevice *dev)
152{
153 struct atmel_serial_priv *priv = dev_get_priv(dev);
154
155 if (!(readl(&priv->usart->csr) & USART3_BIT(RXRDY)))
156 return -EAGAIN;
157
158 return readl(&priv->usart->rhr);
159}
160
161static int atmel_serial_putc(struct udevice *dev, const char ch)
162{
163 struct atmel_serial_priv *priv = dev_get_priv(dev);
164
165 if (!(readl(&priv->usart->csr) & USART3_BIT(TXRDY)))
166 return -EAGAIN;
167
168 writel(ch, &priv->usart->thr);
169
170 return 0;
171}
172
173static int atmel_serial_pending(struct udevice *dev, bool input)
174{
175 struct atmel_serial_priv *priv = dev_get_priv(dev);
176 uint32_t csr = readl(&priv->usart->csr);
177
178 if (input)
179 return csr & USART3_BIT(RXRDY) ? 1 : 0;
180 else
181 return csr & USART3_BIT(TXEMPTY) ? 0 : 1;
182}
183
184static 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,
189};
190
191static int atmel_serial_probe(struct udevice *dev)
192{
193 struct atmel_serial_platdata *plat = dev->platdata;
194 struct atmel_serial_priv *priv = dev_get_priv(dev);
Wenyou Yangc1631c82016-06-01 08:36:56 +0800195#if CONFIG_IS_ENABLED(OF_CONTROL)
196 fdt_addr_t addr_base;
Simon Glass0f65f482014-10-29 13:09:00 -0600197
Wenyou Yangc1631c82016-06-01 08:36:56 +0800198 addr_base = dev_get_addr(dev);
199 if (addr_base == FDT_ADDR_T_NONE)
200 return -ENODEV;
201
202 plat->base_addr = (uint32_t)addr_base;
203#endif
Simon Glass0f65f482014-10-29 13:09:00 -0600204 priv->usart = (atmel_usart3_t *)plat->base_addr;
205 atmel_serial_init_internal(priv->usart);
206
207 return 0;
208}
209
Wenyou Yangc1631c82016-06-01 08:36:56 +0800210#if CONFIG_IS_ENABLED(OF_CONTROL)
211static const struct udevice_id atmel_serial_ids[] = {
212 { .compatible = "atmel,at91sam9260-usart" },
213 { }
214};
215#endif
216
Simon Glass0f65f482014-10-29 13:09:00 -0600217U_BOOT_DRIVER(serial_atmel) = {
218 .name = "serial_atmel",
219 .id = UCLASS_SERIAL,
Wenyou Yangc1631c82016-06-01 08:36:56 +0800220#if CONFIG_IS_ENABLED(OF_CONTROL)
221 .of_match = atmel_serial_ids,
222 .platdata_auto_alloc_size = sizeof(struct atmel_serial_platdata),
223#endif
Simon Glass0f65f482014-10-29 13:09:00 -0600224 .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),
228};
229#endif
Wenyou Yang998cf3c2016-10-17 09:49:55 +0800230
231#ifdef CONFIG_DEBUG_UART_ATMEL
232static inline void _debug_uart_init(void)
233{
234 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_DEBUG_UART_BASE;
235
236 atmel_serial_setbrg_internal(usart, 0, CONFIG_BAUDRATE);
237}
238
239static inline void _debug_uart_putc(int ch)
240{
241 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_DEBUG_UART_BASE;
242
243 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)))
244 ;
245
246 writel(ch, &usart->thr);
247}
248
249DEBUG_UART_FUNCS
250#endif