blob: 9853f49c94ff4a892c7071cc073a05fc4371e4d2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +02002/*
3 * Copyright (C) 2004-2006 Atmel Corporation
4 *
Andreas Bießmannfb378682010-09-03 10:28:05 +02005 * Modified to support C structur SoC access by
6 * Andreas Bießmann <biessmann@corscience.de>
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +02007 */
8#include <common.h>
Wenyou Yang7c56e102017-04-14 15:01:28 +08009#include <clk.h>
Simon Glass7cbab242014-10-29 13:09:00 -060010#include <dm.h>
11#include <errno.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +010013#include <watchdog.h>
Marek Vasutff093ed2012-09-13 16:50:30 +020014#include <serial.h>
Wenyou Yang6b611e62016-10-17 09:49:55 +080015#include <debug_uart.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Marek Vasutff093ed2012-09-13 16:50:30 +020017#include <linux/compiler.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020019
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020020#include <asm/io.h>
Tom Rini952cc382022-12-04 10:14:13 -050021#if CONFIG_IS_ENABLED(DM_SERIAL)
Simon Glass7cbab242014-10-29 13:09:00 -060022#include <asm/arch/atmel_serial.h>
23#endif
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010024#include <asm/arch/clk.h>
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010025#include <asm/arch/hardware.h>
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020026
27#include "atmel_usart.h"
28
29DECLARE_GLOBAL_DATA_PTR;
30
Tom Rini952cc382022-12-04 10:14:13 -050031#if !CONFIG_IS_ENABLED(DM_SERIAL)
Simon Glass1e5c2a82014-10-29 13:08:59 -060032static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id,
33 int baudrate)
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020034{
35 unsigned long divisor;
36 unsigned long usart_hz;
37
38 /*
39 * Master Clock
40 * Baud Rate = --------------
41 * 16 * CD
42 */
Simon Glass1e5c2a82014-10-29 13:08:59 -060043 usart_hz = get_usart_clk_rate(id);
44 divisor = (usart_hz / 16 + baudrate / 2) / baudrate;
Andreas Bießmannfb378682010-09-03 10:28:05 +020045 writel(USART3_BF(CD, divisor), &usart->brgr);
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020046}
47
Simon Glass1e5c2a82014-10-29 13:08:59 -060048static void atmel_serial_init_internal(atmel_usart3_t *usart)
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020049{
Xu, Hong9db57992011-08-02 01:05:04 +000050 /*
51 * Just in case: drain transmitter register
52 * 1000us is enough for baudrate >= 9600
53 */
54 if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
55 __udelay(1000);
56
Andreas Bießmannfb378682010-09-03 10:28:05 +020057 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
Simon Glass1e5c2a82014-10-29 13:08:59 -060058}
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020059
Simon Glass1e5c2a82014-10-29 13:08:59 -060060static void atmel_serial_activate(atmel_usart3_t *usart)
61{
Andreas Bießmannfb378682010-09-03 10:28:05 +020062 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010063 | USART3_BF(USCLKS, USART3_USCLKS_MCK)
64 | USART3_BF(CHRL, USART3_CHRL_8)
65 | USART3_BF(PAR, USART3_PAR_NONE)
Andreas Bießmannfb378682010-09-03 10:28:05 +020066 | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
67 &usart->mr);
Xu, Hong9db57992011-08-02 01:05:04 +000068 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
69 /* 100us is enough for the new settings to be settled */
70 __udelay(100);
Simon Glass1e5c2a82014-10-29 13:08:59 -060071}
72
73static void atmel_serial_setbrg(void)
74{
Tom Rini7abe2a92022-12-04 10:14:02 -050075 atmel_serial_setbrg_internal((atmel_usart3_t *)CFG_USART_BASE,
Tom Rinie111a122022-12-04 10:14:03 -050076 CFG_USART_ID, gd->baudrate);
Simon Glass1e5c2a82014-10-29 13:08:59 -060077}
78
79static int atmel_serial_init(void)
80{
Tom Rini7abe2a92022-12-04 10:14:02 -050081 atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
Simon Glass1e5c2a82014-10-29 13:08:59 -060082
83 atmel_serial_init_internal(usart);
84 serial_setbrg();
85 atmel_serial_activate(usart);
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020086
87 return 0;
88}
89
Marek Vasutff093ed2012-09-13 16:50:30 +020090static void atmel_serial_putc(char c)
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020091{
Tom Rini7abe2a92022-12-04 10:14:02 -050092 atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
Andreas Bießmannfb378682010-09-03 10:28:05 +020093
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020094 if (c == '\n')
95 serial_putc('\r');
96
Andreas Bießmannfb378682010-09-03 10:28:05 +020097 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
98 writel(c, &usart->thr);
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020099}
100
Marek Vasutff093ed2012-09-13 16:50:30 +0200101static int atmel_serial_getc(void)
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +0200102{
Tom Rini7abe2a92022-12-04 10:14:02 -0500103 atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
Andreas Bießmannfb378682010-09-03 10:28:05 +0200104
105 while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
Stefan Roese80877fa2022-09-02 14:10:46 +0200106 schedule();
Andreas Bießmannfb378682010-09-03 10:28:05 +0200107 return readl(&usart->rhr);
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +0200108}
109
Marek Vasutff093ed2012-09-13 16:50:30 +0200110static int atmel_serial_tstc(void)
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +0200111{
Tom Rini7abe2a92022-12-04 10:14:02 -0500112 atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
Andreas Bießmannfb378682010-09-03 10:28:05 +0200113 return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +0200114}
Marek Vasutff093ed2012-09-13 16:50:30 +0200115
Marek Vasutff093ed2012-09-13 16:50:30 +0200116static struct serial_device atmel_serial_drv = {
117 .name = "atmel_serial",
118 .start = atmel_serial_init,
119 .stop = NULL,
120 .setbrg = atmel_serial_setbrg,
121 .putc = atmel_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000122 .puts = default_serial_puts,
Marek Vasutff093ed2012-09-13 16:50:30 +0200123 .getc = atmel_serial_getc,
124 .tstc = atmel_serial_tstc,
125};
126
127void atmel_serial_initialize(void)
128{
129 serial_register(&atmel_serial_drv);
130}
131
132__weak struct serial_device *default_serial_console(void)
133{
134 return &atmel_serial_drv;
135}
Tom Rini952cc382022-12-04 10:14:13 -0500136#else
Wenyou Yang7c56e102017-04-14 15:01:28 +0800137enum serial_clk_type {
138 CLK_TYPE_NORMAL = 0,
139 CLK_TYPE_DBGU,
140};
Simon Glass7cbab242014-10-29 13:09:00 -0600141
142struct atmel_serial_priv {
143 atmel_usart3_t *usart;
Wenyou Yang7c56e102017-04-14 15:01:28 +0800144 ulong usart_clk_rate;
Simon Glass7cbab242014-10-29 13:09:00 -0600145};
146
Wenyou Yangb045a602017-04-14 15:01:27 +0800147static void _atmel_serial_set_brg(atmel_usart3_t *usart,
148 ulong usart_clk_rate, int baudrate)
149{
150 unsigned long divisor;
151
152 divisor = (usart_clk_rate / 16 + baudrate / 2) / baudrate;
153 writel(USART3_BF(CD, divisor), &usart->brgr);
154}
155
156void _atmel_serial_init(atmel_usart3_t *usart,
157 ulong usart_clk_rate, int baudrate)
158{
159 writel(USART3_BIT(RXDIS) | USART3_BIT(TXDIS), &usart->cr);
160
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);
166
167 _atmel_serial_set_brg(usart, usart_clk_rate, baudrate);
168
169 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
170 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
171}
172
Simon Glass7cbab242014-10-29 13:09:00 -0600173int atmel_serial_setbrg(struct udevice *dev, int baudrate)
174{
175 struct atmel_serial_priv *priv = dev_get_priv(dev);
176
Wenyou Yang7c56e102017-04-14 15:01:28 +0800177 _atmel_serial_set_brg(priv->usart, priv->usart_clk_rate, baudrate);
Simon Glass7cbab242014-10-29 13:09:00 -0600178
179 return 0;
180}
181
182static int atmel_serial_getc(struct udevice *dev)
183{
184 struct atmel_serial_priv *priv = dev_get_priv(dev);
185
186 if (!(readl(&priv->usart->csr) & USART3_BIT(RXRDY)))
187 return -EAGAIN;
188
189 return readl(&priv->usart->rhr);
190}
191
192static int atmel_serial_putc(struct udevice *dev, const char ch)
193{
194 struct atmel_serial_priv *priv = dev_get_priv(dev);
195
196 if (!(readl(&priv->usart->csr) & USART3_BIT(TXRDY)))
197 return -EAGAIN;
198
199 writel(ch, &priv->usart->thr);
200
201 return 0;
202}
203
204static int atmel_serial_pending(struct udevice *dev, bool input)
205{
206 struct atmel_serial_priv *priv = dev_get_priv(dev);
207 uint32_t csr = readl(&priv->usart->csr);
208
209 if (input)
210 return csr & USART3_BIT(RXRDY) ? 1 : 0;
211 else
212 return csr & USART3_BIT(TXEMPTY) ? 0 : 1;
213}
214
215static 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,
220};
221
Stefan Roeseb1f3da82019-04-03 15:24:19 +0200222#if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_CLK)
Wenyou Yang7c56e102017-04-14 15:01:28 +0800223static int atmel_serial_enable_clk(struct udevice *dev)
224{
225 struct atmel_serial_priv *priv = dev_get_priv(dev);
Stefan Roeseb1f3da82019-04-03 15:24:19 +0200226
227 /* Use fixed clock value in SPL */
228 priv->usart_clk_rate = CONFIG_SPL_UART_CLOCK;
229
230 return 0;
231}
232#else
233static int atmel_serial_enable_clk(struct udevice *dev)
234{
235 struct atmel_serial_priv *priv = dev_get_priv(dev);
Wenyou Yang7c56e102017-04-14 15:01:28 +0800236 struct clk clk;
237 ulong clk_rate;
238 int ret;
239
240 ret = clk_get_by_index(dev, 0, &clk);
241 if (ret)
242 return -EINVAL;
243
244 if (dev_get_driver_data(dev) == CLK_TYPE_NORMAL) {
245 ret = clk_enable(&clk);
246 if (ret)
247 return ret;
248 }
249
250 clk_rate = clk_get_rate(&clk);
251 if (!clk_rate)
252 return -EINVAL;
253
254 priv->usart_clk_rate = clk_rate;
255
256 clk_free(&clk);
257
258 return 0;
259}
Stefan Roeseb1f3da82019-04-03 15:24:19 +0200260#endif
Wenyou Yang7c56e102017-04-14 15:01:28 +0800261
Simon Glass7cbab242014-10-29 13:09:00 -0600262static int atmel_serial_probe(struct udevice *dev)
263{
Simon Glass95588622020-12-22 19:30:28 -0700264 struct atmel_serial_plat *plat = dev_get_plat(dev);
Simon Glass7cbab242014-10-29 13:09:00 -0600265 struct atmel_serial_priv *priv = dev_get_priv(dev);
Wenyou Yang7c56e102017-04-14 15:01:28 +0800266 int ret;
Wenyou Yangb0e59ee2016-06-01 08:36:56 +0800267#if CONFIG_IS_ENABLED(OF_CONTROL)
268 fdt_addr_t addr_base;
Simon Glass7cbab242014-10-29 13:09:00 -0600269
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900270 addr_base = dev_read_addr(dev);
Wenyou Yangb0e59ee2016-06-01 08:36:56 +0800271 if (addr_base == FDT_ADDR_T_NONE)
272 return -ENODEV;
273
274 plat->base_addr = (uint32_t)addr_base;
275#endif
Simon Glass7cbab242014-10-29 13:09:00 -0600276 priv->usart = (atmel_usart3_t *)plat->base_addr;
Wenyou Yangb045a602017-04-14 15:01:27 +0800277
Wenyou Yang7c56e102017-04-14 15:01:28 +0800278 ret = atmel_serial_enable_clk(dev);
279 if (ret)
280 return ret;
281
282 _atmel_serial_init(priv->usart, priv->usart_clk_rate, gd->baudrate);
Simon Glass7cbab242014-10-29 13:09:00 -0600283
284 return 0;
285}
286
Wenyou Yangb0e59ee2016-06-01 08:36:56 +0800287#if CONFIG_IS_ENABLED(OF_CONTROL)
288static const struct udevice_id atmel_serial_ids[] = {
Wenyou Yang7c56e102017-04-14 15:01:28 +0800289 {
290 .compatible = "atmel,at91sam9260-dbgu",
291 .data = CLK_TYPE_DBGU,
292 },
293 {
294 .compatible = "atmel,at91sam9260-usart",
295 .data = CLK_TYPE_NORMAL,
296 },
Wenyou Yangb0e59ee2016-06-01 08:36:56 +0800297 { }
298};
299#endif
300
Simon Glass7cbab242014-10-29 13:09:00 -0600301U_BOOT_DRIVER(serial_atmel) = {
302 .name = "serial_atmel",
303 .id = UCLASS_SERIAL,
Wenyou Yangb0e59ee2016-06-01 08:36:56 +0800304#if CONFIG_IS_ENABLED(OF_CONTROL)
305 .of_match = atmel_serial_ids,
Simon Glassb75b15b2020-12-03 16:55:23 -0700306 .plat_auto = sizeof(struct atmel_serial_plat),
Wenyou Yangb0e59ee2016-06-01 08:36:56 +0800307#endif
Simon Glass7cbab242014-10-29 13:09:00 -0600308 .probe = atmel_serial_probe,
309 .ops = &atmel_serial_ops,
Bin Mengbdb33d82018-10-24 06:36:36 -0700310#if !CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass7cbab242014-10-29 13:09:00 -0600311 .flags = DM_FLAG_PRE_RELOC,
Bin Mengbdb33d82018-10-24 06:36:36 -0700312#endif
Simon Glass8a2b47f2020-12-03 16:55:17 -0700313 .priv_auto = sizeof(struct atmel_serial_priv),
Simon Glass7cbab242014-10-29 13:09:00 -0600314};
315#endif
Wenyou Yang6b611e62016-10-17 09:49:55 +0800316
317#ifdef CONFIG_DEBUG_UART_ATMEL
318static inline void _debug_uart_init(void)
319{
Pali Rohár8864b352022-05-27 22:15:24 +0200320 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_VAL(DEBUG_UART_BASE);
Wenyou Yang6b611e62016-10-17 09:49:55 +0800321
Wenyou Yangb045a602017-04-14 15:01:27 +0800322 _atmel_serial_init(usart, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
Wenyou Yang6b611e62016-10-17 09:49:55 +0800323}
324
325static inline void _debug_uart_putc(int ch)
326{
Pali Rohár8864b352022-05-27 22:15:24 +0200327 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_VAL(DEBUG_UART_BASE);
Wenyou Yang6b611e62016-10-17 09:49:55 +0800328
329 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)))
330 ;
331
332 writel(ch, &usart->thr);
333}
334
335DEBUG_UART_FUNCS
336#endif