blob: e450135c75745e7231fd83bf5f35bd4d98bb7591 [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>
14#include <linux/compiler.h>
Wolfgang Denkf93ae782006-10-24 14:31:24 +020015
Wolfgang Denkf93ae782006-10-24 14:31:24 +020016#include <asm/io.h>
Simon Glass0f65f482014-10-29 13:09:00 -060017#ifdef CONFIG_DM_SERIAL
18#include <asm/arch/atmel_serial.h>
19#endif
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010020#include <asm/arch/clk.h>
Reinhard Meyer329f0f52010-11-03 16:32:56 +010021#include <asm/arch/hardware.h>
Wolfgang Denkf93ae782006-10-24 14:31:24 +020022
23#include "atmel_usart.h"
24
25DECLARE_GLOBAL_DATA_PTR;
26
Simon Glass62137fc2014-10-29 13:08:59 -060027static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id,
28 int baudrate)
Wolfgang Denkf93ae782006-10-24 14:31:24 +020029{
30 unsigned long divisor;
31 unsigned long usart_hz;
32
33 /*
34 * Master Clock
35 * Baud Rate = --------------
36 * 16 * CD
37 */
Simon Glass62137fc2014-10-29 13:08:59 -060038 usart_hz = get_usart_clk_rate(id);
39 divisor = (usart_hz / 16 + baudrate / 2) / baudrate;
Andreas Bießmann125637c2010-09-03 10:28:05 +020040 writel(USART3_BF(CD, divisor), &usart->brgr);
Wolfgang Denkf93ae782006-10-24 14:31:24 +020041}
42
Simon Glass62137fc2014-10-29 13:08:59 -060043static void atmel_serial_init_internal(atmel_usart3_t *usart)
Wolfgang Denkf93ae782006-10-24 14:31:24 +020044{
Xu, Hong1f4faed2011-08-02 01:05:04 +000045 /*
46 * Just in case: drain transmitter register
47 * 1000us is enough for baudrate >= 9600
48 */
49 if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
50 __udelay(1000);
51
Andreas Bießmann125637c2010-09-03 10:28:05 +020052 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
Simon Glass62137fc2014-10-29 13:08:59 -060053}
Wolfgang Denkf93ae782006-10-24 14:31:24 +020054
Simon Glass62137fc2014-10-29 13:08:59 -060055static void atmel_serial_activate(atmel_usart3_t *usart)
56{
Andreas Bießmann125637c2010-09-03 10:28:05 +020057 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010058 | USART3_BF(USCLKS, USART3_USCLKS_MCK)
59 | USART3_BF(CHRL, USART3_CHRL_8)
60 | USART3_BF(PAR, USART3_PAR_NONE)
Andreas Bießmann125637c2010-09-03 10:28:05 +020061 | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
62 &usart->mr);
Xu, Hong1f4faed2011-08-02 01:05:04 +000063 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
64 /* 100us is enough for the new settings to be settled */
65 __udelay(100);
Simon Glass62137fc2014-10-29 13:08:59 -060066}
67
Simon Glass0f65f482014-10-29 13:09:00 -060068#ifndef CONFIG_DM_SERIAL
Simon Glass62137fc2014-10-29 13:08:59 -060069static void atmel_serial_setbrg(void)
70{
71 atmel_serial_setbrg_internal((atmel_usart3_t *)CONFIG_USART_BASE,
72 CONFIG_USART_ID, gd->baudrate);
73}
74
75static int atmel_serial_init(void)
76{
77 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
78
79 atmel_serial_init_internal(usart);
80 serial_setbrg();
81 atmel_serial_activate(usart);
Wolfgang Denkf93ae782006-10-24 14:31:24 +020082
83 return 0;
84}
85
Marek Vasutcfba4572012-09-13 16:50:30 +020086static void atmel_serial_putc(char c)
Wolfgang Denkf93ae782006-10-24 14:31:24 +020087{
Reinhard Meyer329f0f52010-11-03 16:32:56 +010088 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
Andreas Bießmann125637c2010-09-03 10:28:05 +020089
Wolfgang Denkf93ae782006-10-24 14:31:24 +020090 if (c == '\n')
91 serial_putc('\r');
92
Andreas Bießmann125637c2010-09-03 10:28:05 +020093 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
94 writel(c, &usart->thr);
Wolfgang Denkf93ae782006-10-24 14:31:24 +020095}
96
Marek Vasutcfba4572012-09-13 16:50:30 +020097static int atmel_serial_getc(void)
Wolfgang Denkf93ae782006-10-24 14:31:24 +020098{
Reinhard Meyer329f0f52010-11-03 16:32:56 +010099 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
Andreas Bießmann125637c2010-09-03 10:28:05 +0200100
101 while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
Jean-Christophe PLAGNIOL-VILLARD843a2652009-03-27 23:26:42 +0100102 WATCHDOG_RESET();
Andreas Bießmann125637c2010-09-03 10:28:05 +0200103 return readl(&usart->rhr);
Wolfgang Denkf93ae782006-10-24 14:31:24 +0200104}
105
Marek Vasutcfba4572012-09-13 16:50:30 +0200106static int atmel_serial_tstc(void)
Wolfgang Denkf93ae782006-10-24 14:31:24 +0200107{
Reinhard Meyer329f0f52010-11-03 16:32:56 +0100108 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
Andreas Bießmann125637c2010-09-03 10:28:05 +0200109 return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
Wolfgang Denkf93ae782006-10-24 14:31:24 +0200110}
Marek Vasutcfba4572012-09-13 16:50:30 +0200111
Marek Vasutcfba4572012-09-13 16:50:30 +0200112static struct serial_device atmel_serial_drv = {
113 .name = "atmel_serial",
114 .start = atmel_serial_init,
115 .stop = NULL,
116 .setbrg = atmel_serial_setbrg,
117 .putc = atmel_serial_putc,
Marek Vasutec3fd682012-10-06 14:07:02 +0000118 .puts = default_serial_puts,
Marek Vasutcfba4572012-09-13 16:50:30 +0200119 .getc = atmel_serial_getc,
120 .tstc = atmel_serial_tstc,
121};
122
123void atmel_serial_initialize(void)
124{
125 serial_register(&atmel_serial_drv);
126}
127
128__weak struct serial_device *default_serial_console(void)
129{
130 return &atmel_serial_drv;
131}
Simon Glass0f65f482014-10-29 13:09:00 -0600132#endif
133
134#ifdef CONFIG_DM_SERIAL
135
136struct atmel_serial_priv {
137 atmel_usart3_t *usart;
138};
139
140int atmel_serial_setbrg(struct udevice *dev, int baudrate)
141{
142 struct atmel_serial_priv *priv = dev_get_priv(dev);
143
144 atmel_serial_setbrg_internal(priv->usart, 0 /* ignored */, baudrate);
145 atmel_serial_activate(priv->usart);
146
147 return 0;
148}
149
150static int atmel_serial_getc(struct udevice *dev)
151{
152 struct atmel_serial_priv *priv = dev_get_priv(dev);
153
154 if (!(readl(&priv->usart->csr) & USART3_BIT(RXRDY)))
155 return -EAGAIN;
156
157 return readl(&priv->usart->rhr);
158}
159
160static int atmel_serial_putc(struct udevice *dev, const char ch)
161{
162 struct atmel_serial_priv *priv = dev_get_priv(dev);
163
164 if (!(readl(&priv->usart->csr) & USART3_BIT(TXRDY)))
165 return -EAGAIN;
166
167 writel(ch, &priv->usart->thr);
168
169 return 0;
170}
171
172static int atmel_serial_pending(struct udevice *dev, bool input)
173{
174 struct atmel_serial_priv *priv = dev_get_priv(dev);
175 uint32_t csr = readl(&priv->usart->csr);
176
177 if (input)
178 return csr & USART3_BIT(RXRDY) ? 1 : 0;
179 else
180 return csr & USART3_BIT(TXEMPTY) ? 0 : 1;
181}
182
183static const struct dm_serial_ops atmel_serial_ops = {
184 .putc = atmel_serial_putc,
185 .pending = atmel_serial_pending,
186 .getc = atmel_serial_getc,
187 .setbrg = atmel_serial_setbrg,
188};
189
190static int atmel_serial_probe(struct udevice *dev)
191{
192 struct atmel_serial_platdata *plat = dev->platdata;
193 struct atmel_serial_priv *priv = dev_get_priv(dev);
Wenyou Yangc1631c82016-06-01 08:36:56 +0800194#if CONFIG_IS_ENABLED(OF_CONTROL)
195 fdt_addr_t addr_base;
Simon Glass0f65f482014-10-29 13:09:00 -0600196
Wenyou Yangc1631c82016-06-01 08:36:56 +0800197 addr_base = dev_get_addr(dev);
198 if (addr_base == FDT_ADDR_T_NONE)
199 return -ENODEV;
200
201 plat->base_addr = (uint32_t)addr_base;
202#endif
Simon Glass0f65f482014-10-29 13:09:00 -0600203 priv->usart = (atmel_usart3_t *)plat->base_addr;
204 atmel_serial_init_internal(priv->usart);
205
206 return 0;
207}
208
Wenyou Yangc1631c82016-06-01 08:36:56 +0800209#if CONFIG_IS_ENABLED(OF_CONTROL)
210static const struct udevice_id atmel_serial_ids[] = {
211 { .compatible = "atmel,at91sam9260-usart" },
212 { }
213};
214#endif
215
Simon Glass0f65f482014-10-29 13:09:00 -0600216U_BOOT_DRIVER(serial_atmel) = {
217 .name = "serial_atmel",
218 .id = UCLASS_SERIAL,
Wenyou Yangc1631c82016-06-01 08:36:56 +0800219#if CONFIG_IS_ENABLED(OF_CONTROL)
220 .of_match = atmel_serial_ids,
221 .platdata_auto_alloc_size = sizeof(struct atmel_serial_platdata),
222#endif
Simon Glass0f65f482014-10-29 13:09:00 -0600223 .probe = atmel_serial_probe,
224 .ops = &atmel_serial_ops,
225 .flags = DM_FLAG_PRE_RELOC,
226 .priv_auto_alloc_size = sizeof(struct atmel_serial_priv),
227};
228#endif