blob: 7e45a80969e8935b06064b0d6985a8acbb0a327d [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 */
Wenyou Yang7c56e102017-04-14 15:01:28 +08008#include <clk.h>
Simon Glass7cbab242014-10-29 13:09:00 -06009#include <dm.h>
10#include <errno.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +010012#include <watchdog.h>
Marek Vasutff093ed2012-09-13 16:50:30 +020013#include <serial.h>
Wenyou Yang6b611e62016-10-17 09:49:55 +080014#include <debug_uart.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Marek Vasutff093ed2012-09-13 16:50:30 +020016#include <linux/compiler.h>
Simon Glassdbd79542020-05-10 11:40:11 -060017#include <linux/delay.h>
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020018
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020019#include <asm/io.h>
Tom Rini952cc382022-12-04 10:14:13 -050020#if CONFIG_IS_ENABLED(DM_SERIAL)
Simon Glass7cbab242014-10-29 13:09:00 -060021#include <asm/arch/atmel_serial.h>
22#endif
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010023#include <asm/arch/clk.h>
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010024#include <asm/arch/hardware.h>
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020025
26#include "atmel_usart.h"
27
28DECLARE_GLOBAL_DATA_PTR;
29
Tom Rini952cc382022-12-04 10:14:13 -050030#if !CONFIG_IS_ENABLED(DM_SERIAL)
Simon Glass1e5c2a82014-10-29 13:08:59 -060031static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id,
32 int baudrate)
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020033{
34 unsigned long divisor;
35 unsigned long usart_hz;
36
37 /*
38 * Master Clock
39 * Baud Rate = --------------
40 * 16 * CD
41 */
Simon Glass1e5c2a82014-10-29 13:08:59 -060042 usart_hz = get_usart_clk_rate(id);
43 divisor = (usart_hz / 16 + baudrate / 2) / baudrate;
Andreas Bießmannfb378682010-09-03 10:28:05 +020044 writel(USART3_BF(CD, divisor), &usart->brgr);
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020045}
46
Simon Glass1e5c2a82014-10-29 13:08:59 -060047static void atmel_serial_init_internal(atmel_usart3_t *usart)
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020048{
Xu, Hong9db57992011-08-02 01:05:04 +000049 /*
50 * Just in case: drain transmitter register
51 * 1000us is enough for baudrate >= 9600
52 */
53 if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
54 __udelay(1000);
55
Andreas Bießmannfb378682010-09-03 10:28:05 +020056 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
Simon Glass1e5c2a82014-10-29 13:08:59 -060057}
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020058
Simon Glass1e5c2a82014-10-29 13:08:59 -060059static void atmel_serial_activate(atmel_usart3_t *usart)
60{
Andreas Bießmannfb378682010-09-03 10:28:05 +020061 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010062 | USART3_BF(USCLKS, USART3_USCLKS_MCK)
63 | USART3_BF(CHRL, USART3_CHRL_8)
64 | USART3_BF(PAR, USART3_PAR_NONE)
Andreas Bießmannfb378682010-09-03 10:28:05 +020065 | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
66 &usart->mr);
Xu, Hong9db57992011-08-02 01:05:04 +000067 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
68 /* 100us is enough for the new settings to be settled */
69 __udelay(100);
Simon Glass1e5c2a82014-10-29 13:08:59 -060070}
71
72static void atmel_serial_setbrg(void)
73{
Tom Rini7abe2a92022-12-04 10:14:02 -050074 atmel_serial_setbrg_internal((atmel_usart3_t *)CFG_USART_BASE,
Tom Rinie111a122022-12-04 10:14:03 -050075 CFG_USART_ID, gd->baudrate);
Simon Glass1e5c2a82014-10-29 13:08:59 -060076}
77
78static int atmel_serial_init(void)
79{
Tom Rini7abe2a92022-12-04 10:14:02 -050080 atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
Simon Glass1e5c2a82014-10-29 13:08:59 -060081
82 atmel_serial_init_internal(usart);
83 serial_setbrg();
84 atmel_serial_activate(usart);
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020085
86 return 0;
87}
88
Marek Vasutff093ed2012-09-13 16:50:30 +020089static void atmel_serial_putc(char c)
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020090{
Tom Rini7abe2a92022-12-04 10:14:02 -050091 atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
Andreas Bießmannfb378682010-09-03 10:28:05 +020092
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020093 if (c == '\n')
94 serial_putc('\r');
95
Andreas Bießmannfb378682010-09-03 10:28:05 +020096 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
97 writel(c, &usart->thr);
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020098}
99
Marek Vasutff093ed2012-09-13 16:50:30 +0200100static int atmel_serial_getc(void)
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +0200101{
Tom Rini7abe2a92022-12-04 10:14:02 -0500102 atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
Andreas Bießmannfb378682010-09-03 10:28:05 +0200103
104 while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
Stefan Roese80877fa2022-09-02 14:10:46 +0200105 schedule();
Andreas Bießmannfb378682010-09-03 10:28:05 +0200106 return readl(&usart->rhr);
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +0200107}
108
Marek Vasutff093ed2012-09-13 16:50:30 +0200109static int atmel_serial_tstc(void)
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +0200110{
Tom Rini7abe2a92022-12-04 10:14:02 -0500111 atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
Andreas Bießmannfb378682010-09-03 10:28:05 +0200112 return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +0200113}
Marek Vasutff093ed2012-09-13 16:50:30 +0200114
Marek Vasutff093ed2012-09-13 16:50:30 +0200115static struct serial_device atmel_serial_drv = {
116 .name = "atmel_serial",
117 .start = atmel_serial_init,
118 .stop = NULL,
119 .setbrg = atmel_serial_setbrg,
120 .putc = atmel_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000121 .puts = default_serial_puts,
Marek Vasutff093ed2012-09-13 16:50:30 +0200122 .getc = atmel_serial_getc,
123 .tstc = atmel_serial_tstc,
124};
125
126void atmel_serial_initialize(void)
127{
128 serial_register(&atmel_serial_drv);
129}
130
131__weak struct serial_device *default_serial_console(void)
132{
133 return &atmel_serial_drv;
134}
Tom Rini952cc382022-12-04 10:14:13 -0500135#else
Wenyou Yang7c56e102017-04-14 15:01:28 +0800136enum serial_clk_type {
137 CLK_TYPE_NORMAL = 0,
138 CLK_TYPE_DBGU,
139};
Simon Glass7cbab242014-10-29 13:09:00 -0600140
141struct atmel_serial_priv {
142 atmel_usart3_t *usart;
Wenyou Yang7c56e102017-04-14 15:01:28 +0800143 ulong usart_clk_rate;
Simon Glass7cbab242014-10-29 13:09:00 -0600144};
145
Wenyou Yangb045a602017-04-14 15:01:27 +0800146static void _atmel_serial_set_brg(atmel_usart3_t *usart,
147 ulong usart_clk_rate, int baudrate)
148{
149 unsigned long divisor;
150
151 divisor = (usart_clk_rate / 16 + baudrate / 2) / baudrate;
152 writel(USART3_BF(CD, divisor), &usart->brgr);
153}
154
155void _atmel_serial_init(atmel_usart3_t *usart,
156 ulong usart_clk_rate, int baudrate)
157{
158 writel(USART3_BIT(RXDIS) | USART3_BIT(TXDIS), &usart->cr);
159
160 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL) |
161 USART3_BF(USCLKS, USART3_USCLKS_MCK) |
162 USART3_BF(CHRL, USART3_CHRL_8) |
163 USART3_BF(PAR, USART3_PAR_NONE) |
164 USART3_BF(NBSTOP, USART3_NBSTOP_1)), &usart->mr);
165
166 _atmel_serial_set_brg(usart, usart_clk_rate, baudrate);
167
168 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
169 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
170}
171
Simon Glass7cbab242014-10-29 13:09:00 -0600172int atmel_serial_setbrg(struct udevice *dev, int baudrate)
173{
174 struct atmel_serial_priv *priv = dev_get_priv(dev);
175
Wenyou Yang7c56e102017-04-14 15:01:28 +0800176 _atmel_serial_set_brg(priv->usart, priv->usart_clk_rate, baudrate);
Simon Glass7cbab242014-10-29 13:09:00 -0600177
178 return 0;
179}
180
181static int atmel_serial_getc(struct udevice *dev)
182{
183 struct atmel_serial_priv *priv = dev_get_priv(dev);
184
185 if (!(readl(&priv->usart->csr) & USART3_BIT(RXRDY)))
186 return -EAGAIN;
187
188 return readl(&priv->usart->rhr);
189}
190
191static int atmel_serial_putc(struct udevice *dev, const char ch)
192{
193 struct atmel_serial_priv *priv = dev_get_priv(dev);
194
195 if (!(readl(&priv->usart->csr) & USART3_BIT(TXRDY)))
196 return -EAGAIN;
197
198 writel(ch, &priv->usart->thr);
199
200 return 0;
201}
202
203static int atmel_serial_pending(struct udevice *dev, bool input)
204{
205 struct atmel_serial_priv *priv = dev_get_priv(dev);
206 uint32_t csr = readl(&priv->usart->csr);
207
208 if (input)
209 return csr & USART3_BIT(RXRDY) ? 1 : 0;
210 else
211 return csr & USART3_BIT(TXEMPTY) ? 0 : 1;
212}
213
214static const struct dm_serial_ops atmel_serial_ops = {
215 .putc = atmel_serial_putc,
216 .pending = atmel_serial_pending,
217 .getc = atmel_serial_getc,
218 .setbrg = atmel_serial_setbrg,
219};
220
Stefan Roeseb1f3da82019-04-03 15:24:19 +0200221#if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_CLK)
Wenyou Yang7c56e102017-04-14 15:01:28 +0800222static int atmel_serial_enable_clk(struct udevice *dev)
223{
224 struct atmel_serial_priv *priv = dev_get_priv(dev);
Stefan Roeseb1f3da82019-04-03 15:24:19 +0200225
226 /* Use fixed clock value in SPL */
227 priv->usart_clk_rate = CONFIG_SPL_UART_CLOCK;
228
229 return 0;
230}
231#else
232static int atmel_serial_enable_clk(struct udevice *dev)
233{
234 struct atmel_serial_priv *priv = dev_get_priv(dev);
Wenyou Yang7c56e102017-04-14 15:01:28 +0800235 struct clk clk;
236 ulong clk_rate;
237 int ret;
238
239 ret = clk_get_by_index(dev, 0, &clk);
240 if (ret)
241 return -EINVAL;
242
243 if (dev_get_driver_data(dev) == CLK_TYPE_NORMAL) {
244 ret = clk_enable(&clk);
245 if (ret)
246 return ret;
247 }
248
249 clk_rate = clk_get_rate(&clk);
250 if (!clk_rate)
251 return -EINVAL;
252
253 priv->usart_clk_rate = clk_rate;
254
Wenyou Yang7c56e102017-04-14 15:01:28 +0800255 return 0;
256}
Stefan Roeseb1f3da82019-04-03 15:24:19 +0200257#endif
Wenyou Yang7c56e102017-04-14 15:01:28 +0800258
Simon Glass7cbab242014-10-29 13:09:00 -0600259static int atmel_serial_probe(struct udevice *dev)
260{
Simon Glass95588622020-12-22 19:30:28 -0700261 struct atmel_serial_plat *plat = dev_get_plat(dev);
Simon Glass7cbab242014-10-29 13:09:00 -0600262 struct atmel_serial_priv *priv = dev_get_priv(dev);
Wenyou Yang7c56e102017-04-14 15:01:28 +0800263 int ret;
Wenyou Yangb0e59ee2016-06-01 08:36:56 +0800264#if CONFIG_IS_ENABLED(OF_CONTROL)
265 fdt_addr_t addr_base;
Simon Glass7cbab242014-10-29 13:09:00 -0600266
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900267 addr_base = dev_read_addr(dev);
Wenyou Yangb0e59ee2016-06-01 08:36:56 +0800268 if (addr_base == FDT_ADDR_T_NONE)
269 return -ENODEV;
270
271 plat->base_addr = (uint32_t)addr_base;
272#endif
Simon Glass7cbab242014-10-29 13:09:00 -0600273 priv->usart = (atmel_usart3_t *)plat->base_addr;
Wenyou Yangb045a602017-04-14 15:01:27 +0800274
Wenyou Yang7c56e102017-04-14 15:01:28 +0800275 ret = atmel_serial_enable_clk(dev);
276 if (ret)
277 return ret;
278
279 _atmel_serial_init(priv->usart, priv->usart_clk_rate, gd->baudrate);
Simon Glass7cbab242014-10-29 13:09:00 -0600280
281 return 0;
282}
283
Wenyou Yangb0e59ee2016-06-01 08:36:56 +0800284#if CONFIG_IS_ENABLED(OF_CONTROL)
285static const struct udevice_id atmel_serial_ids[] = {
Wenyou Yang7c56e102017-04-14 15:01:28 +0800286 {
287 .compatible = "atmel,at91sam9260-dbgu",
288 .data = CLK_TYPE_DBGU,
289 },
290 {
291 .compatible = "atmel,at91sam9260-usart",
292 .data = CLK_TYPE_NORMAL,
293 },
Wenyou Yangb0e59ee2016-06-01 08:36:56 +0800294 { }
295};
296#endif
297
Simon Glass7cbab242014-10-29 13:09:00 -0600298U_BOOT_DRIVER(serial_atmel) = {
299 .name = "serial_atmel",
300 .id = UCLASS_SERIAL,
Wenyou Yangb0e59ee2016-06-01 08:36:56 +0800301#if CONFIG_IS_ENABLED(OF_CONTROL)
302 .of_match = atmel_serial_ids,
Simon Glassb75b15b2020-12-03 16:55:23 -0700303 .plat_auto = sizeof(struct atmel_serial_plat),
Wenyou Yangb0e59ee2016-06-01 08:36:56 +0800304#endif
Simon Glass7cbab242014-10-29 13:09:00 -0600305 .probe = atmel_serial_probe,
306 .ops = &atmel_serial_ops,
Bin Mengbdb33d82018-10-24 06:36:36 -0700307#if !CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass7cbab242014-10-29 13:09:00 -0600308 .flags = DM_FLAG_PRE_RELOC,
Bin Mengbdb33d82018-10-24 06:36:36 -0700309#endif
Simon Glass8a2b47f2020-12-03 16:55:17 -0700310 .priv_auto = sizeof(struct atmel_serial_priv),
Simon Glass7cbab242014-10-29 13:09:00 -0600311};
312#endif
Wenyou Yang6b611e62016-10-17 09:49:55 +0800313
314#ifdef CONFIG_DEBUG_UART_ATMEL
315static inline void _debug_uart_init(void)
316{
Pali Rohár8864b352022-05-27 22:15:24 +0200317 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_VAL(DEBUG_UART_BASE);
Wenyou Yang6b611e62016-10-17 09:49:55 +0800318
Wenyou Yangb045a602017-04-14 15:01:27 +0800319 _atmel_serial_init(usart, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
Wenyou Yang6b611e62016-10-17 09:49:55 +0800320}
321
322static inline void _debug_uart_putc(int ch)
323{
Pali Rohár8864b352022-05-27 22:15:24 +0200324 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_VAL(DEBUG_UART_BASE);
Wenyou Yang6b611e62016-10-17 09:49:55 +0800325
326 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)))
327 ;
328
329 writel(ch, &usart->thr);
330}
331
332DEBUG_UART_FUNCS
333#endif