blob: 122c6d107d1da06100909460559c01d51eb16209 [file] [log] [blame]
Hans-Christian Egtvedt60445cb2008-05-16 11:10:32 +02001/*
2 * Copyright (C) 2007 Atmel Corporation
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Hans-Christian Egtvedt60445cb2008-05-16 11:10:32 +02005 */
6#include <common.h>
Wenyou Yang0eafd4b2016-10-28 14:17:49 +08007#include <clk.h>
8#include <dm.h>
9#include <fdtdec.h>
Hans-Christian Egtvedt60445cb2008-05-16 11:10:32 +020010#include <spi.h>
11#include <malloc.h>
Wenyou Yang0eafd4b2016-10-28 14:17:49 +080012#include <wait_bit.h>
Hans-Christian Egtvedt60445cb2008-05-16 11:10:32 +020013
14#include <asm/io.h>
15
16#include <asm/arch/clk.h>
Reinhard Meyer329f0f52010-11-03 16:32:56 +010017#include <asm/arch/hardware.h>
Wenyou Yang0eafd4b2016-10-28 14:17:49 +080018#ifdef CONFIG_DM_SPI
19#include <asm/arch/at91_spi.h>
20#endif
21#ifdef CONFIG_DM_GPIO
22#include <asm/gpio.h>
23#endif
Hans-Christian Egtvedt60445cb2008-05-16 11:10:32 +020024
Tom Rini5270df22018-04-07 09:15:06 -040025#include "atmel_spi.h"
26
Wenyou Yang0eafd4b2016-10-28 14:17:49 +080027DECLARE_GLOBAL_DATA_PTR;
28
Tom Rini5270df22018-04-07 09:15:06 -040029#define MAX_CS_COUNT 4
Wenyou Yang0eafd4b2016-10-28 14:17:49 +080030
31struct atmel_spi_platdata {
32 struct at91_spi *regs;
33};
34
35struct atmel_spi_priv {
36 unsigned int freq; /* Default frequency */
37 unsigned int mode;
38 ulong bus_clk_rate;
Jagan Teki9bf48e22018-03-14 18:46:31 +053039#ifdef CONFIG_DM_GPIO
Wenyou Yang0eafd4b2016-10-28 14:17:49 +080040 struct gpio_desc cs_gpios[MAX_CS_COUNT];
Jagan Teki9bf48e22018-03-14 18:46:31 +053041#endif
Wenyou Yang0eafd4b2016-10-28 14:17:49 +080042};
43
44static int atmel_spi_claim_bus(struct udevice *dev)
45{
46 struct udevice *bus = dev_get_parent(dev);
47 struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
48 struct atmel_spi_priv *priv = dev_get_priv(bus);
49 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
50 struct at91_spi *reg_base = bus_plat->regs;
51 u32 cs = slave_plat->cs;
52 u32 freq = priv->freq;
53 u32 scbr, csrx, mode;
54
55 scbr = (priv->bus_clk_rate + freq - 1) / freq;
Tom Rini5270df22018-04-07 09:15:06 -040056 if (scbr > ATMEL_SPI_CSRx_SCBR_MAX)
Wenyou Yang0eafd4b2016-10-28 14:17:49 +080057 return -EINVAL;
58
59 if (scbr < 1)
60 scbr = 1;
61
Tom Rini5270df22018-04-07 09:15:06 -040062 csrx = ATMEL_SPI_CSRx_SCBR(scbr);
63 csrx |= ATMEL_SPI_CSRx_BITS(ATMEL_SPI_BITS_8);
Wenyou Yang0eafd4b2016-10-28 14:17:49 +080064
65 if (!(priv->mode & SPI_CPHA))
Tom Rini5270df22018-04-07 09:15:06 -040066 csrx |= ATMEL_SPI_CSRx_NCPHA;
Wenyou Yang0eafd4b2016-10-28 14:17:49 +080067 if (priv->mode & SPI_CPOL)
Tom Rini5270df22018-04-07 09:15:06 -040068 csrx |= ATMEL_SPI_CSRx_CPOL;
Wenyou Yang0eafd4b2016-10-28 14:17:49 +080069
70 writel(csrx, &reg_base->csr[cs]);
71
72 mode = ATMEL_SPI_MR_MSTR |
73 ATMEL_SPI_MR_MODFDIS |
74 ATMEL_SPI_MR_WDRBT |
75 ATMEL_SPI_MR_PCS(~(1 << cs));
76
77 writel(mode, &reg_base->mr);
78
79 writel(ATMEL_SPI_CR_SPIEN, &reg_base->cr);
80
81 return 0;
82}
83
84static int atmel_spi_release_bus(struct udevice *dev)
85{
86 struct udevice *bus = dev_get_parent(dev);
87 struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
88
89 writel(ATMEL_SPI_CR_SPIDIS, &bus_plat->regs->cr);
90
91 return 0;
92}
93
94static void atmel_spi_cs_activate(struct udevice *dev)
95{
Jagan Teki9bf48e22018-03-14 18:46:31 +053096#ifdef CONFIG_DM_GPIO
Wenyou Yang0eafd4b2016-10-28 14:17:49 +080097 struct udevice *bus = dev_get_parent(dev);
98 struct atmel_spi_priv *priv = dev_get_priv(bus);
99 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
100 u32 cs = slave_plat->cs;
101
Wenyou Yang61a77ce2017-04-07 15:14:46 +0800102 if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
103 return;
104
Wenyou Yang0eafd4b2016-10-28 14:17:49 +0800105 dm_gpio_set_value(&priv->cs_gpios[cs], 0);
Jagan Teki9bf48e22018-03-14 18:46:31 +0530106#endif
Wenyou Yang0eafd4b2016-10-28 14:17:49 +0800107}
108
109static void atmel_spi_cs_deactivate(struct udevice *dev)
110{
Jagan Teki9bf48e22018-03-14 18:46:31 +0530111#ifdef CONFIG_DM_GPIO
Wenyou Yang0eafd4b2016-10-28 14:17:49 +0800112 struct udevice *bus = dev_get_parent(dev);
113 struct atmel_spi_priv *priv = dev_get_priv(bus);
114 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
115 u32 cs = slave_plat->cs;
116
Wenyou Yang61a77ce2017-04-07 15:14:46 +0800117 if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
118 return;
119
Wenyou Yang0eafd4b2016-10-28 14:17:49 +0800120 dm_gpio_set_value(&priv->cs_gpios[cs], 1);
Jagan Teki9bf48e22018-03-14 18:46:31 +0530121#endif
Wenyou Yang0eafd4b2016-10-28 14:17:49 +0800122}
123
124static int atmel_spi_xfer(struct udevice *dev, unsigned int bitlen,
125 const void *dout, void *din, unsigned long flags)
126{
127 struct udevice *bus = dev_get_parent(dev);
128 struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
129 struct at91_spi *reg_base = bus_plat->regs;
130
131 u32 len_tx, len_rx, len;
132 u32 status;
133 const u8 *txp = dout;
134 u8 *rxp = din;
135 u8 value;
136
137 if (bitlen == 0)
138 goto out;
139
140 /*
141 * The controller can do non-multiple-of-8 bit
142 * transfers, but this driver currently doesn't support it.
143 *
144 * It's also not clear how such transfers are supposed to be
145 * represented as a stream of bytes...this is a limitation of
146 * the current SPI interface.
147 */
148 if (bitlen % 8) {
149 /* Errors always terminate an ongoing transfer */
150 flags |= SPI_XFER_END;
151 goto out;
152 }
153
154 len = bitlen / 8;
155
156 /*
157 * The controller can do automatic CS control, but it is
158 * somewhat quirky, and it doesn't really buy us much anyway
159 * in the context of U-Boot.
160 */
161 if (flags & SPI_XFER_BEGIN) {
162 atmel_spi_cs_activate(dev);
163
164 /*
165 * sometimes the RDR is not empty when we get here,
166 * in theory that should not happen, but it DOES happen.
167 * Read it here to be on the safe side.
168 * That also clears the OVRES flag. Required if the
169 * following loop exits due to OVRES!
170 */
171 readl(&reg_base->rdr);
172 }
173
174 for (len_tx = 0, len_rx = 0; len_rx < len; ) {
175 status = readl(&reg_base->sr);
176
177 if (status & ATMEL_SPI_SR_OVRES)
178 return -1;
179
180 if ((len_tx < len) && (status & ATMEL_SPI_SR_TDRE)) {
181 if (txp)
182 value = *txp++;
183 else
184 value = 0;
185 writel(value, &reg_base->tdr);
186 len_tx++;
187 }
188
189 if (status & ATMEL_SPI_SR_RDRF) {
190 value = readl(&reg_base->rdr);
191 if (rxp)
192 *rxp++ = value;
193 len_rx++;
194 }
195 }
196
197out:
198 if (flags & SPI_XFER_END) {
199 /*
200 * Wait until the transfer is completely done before
201 * we deactivate CS.
202 */
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100203 wait_for_bit_le32(&reg_base->sr,
204 ATMEL_SPI_SR_TXEMPTY, true, 1000, false);
Wenyou Yang0eafd4b2016-10-28 14:17:49 +0800205
206 atmel_spi_cs_deactivate(dev);
207 }
208
209 return 0;
210}
211
212static int atmel_spi_set_speed(struct udevice *bus, uint speed)
213{
214 struct atmel_spi_priv *priv = dev_get_priv(bus);
215
216 priv->freq = speed;
217
218 return 0;
219}
220
221static int atmel_spi_set_mode(struct udevice *bus, uint mode)
222{
223 struct atmel_spi_priv *priv = dev_get_priv(bus);
224
225 priv->mode = mode;
226
227 return 0;
228}
229
230static const struct dm_spi_ops atmel_spi_ops = {
231 .claim_bus = atmel_spi_claim_bus,
232 .release_bus = atmel_spi_release_bus,
233 .xfer = atmel_spi_xfer,
234 .set_speed = atmel_spi_set_speed,
235 .set_mode = atmel_spi_set_mode,
236 /*
237 * cs_info is not needed, since we require all chip selects to be
238 * in the device tree explicitly
239 */
240};
241
242static int atmel_spi_enable_clk(struct udevice *bus)
243{
244 struct atmel_spi_priv *priv = dev_get_priv(bus);
245 struct clk clk;
246 ulong clk_rate;
247 int ret;
248
249 ret = clk_get_by_index(bus, 0, &clk);
250 if (ret)
251 return -EINVAL;
252
253 ret = clk_enable(&clk);
254 if (ret)
255 return ret;
256
257 clk_rate = clk_get_rate(&clk);
258 if (!clk_rate)
259 return -EINVAL;
260
261 priv->bus_clk_rate = clk_rate;
262
263 clk_free(&clk);
264
265 return 0;
266}
267
268static int atmel_spi_probe(struct udevice *bus)
269{
270 struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
Jagan Teki9bf48e22018-03-14 18:46:31 +0530271 int ret;
Wenyou Yang0eafd4b2016-10-28 14:17:49 +0800272
273 ret = atmel_spi_enable_clk(bus);
274 if (ret)
275 return ret;
276
Simon Glassa821c4a2017-05-17 17:18:05 -0600277 bus_plat->regs = (struct at91_spi *)devfdt_get_addr(bus);
Wenyou Yang0eafd4b2016-10-28 14:17:49 +0800278
Jagan Teki9bf48e22018-03-14 18:46:31 +0530279#ifdef CONFIG_DM_GPIO
280 struct atmel_spi_priv *priv = dev_get_priv(bus);
281 int i;
282
Wenyou Yang0eafd4b2016-10-28 14:17:49 +0800283 ret = gpio_request_list_by_name(bus, "cs-gpios", priv->cs_gpios,
284 ARRAY_SIZE(priv->cs_gpios), 0);
285 if (ret < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900286 pr_err("Can't get %s gpios! Error: %d", bus->name, ret);
Wenyou Yang0eafd4b2016-10-28 14:17:49 +0800287 return ret;
288 }
289
Tom Rini5270df22018-04-07 09:15:06 -0400290 for(i = 0; i < ARRAY_SIZE(priv->cs_gpios); i++) {
Wenyou Yang61a77ce2017-04-07 15:14:46 +0800291 if (!dm_gpio_is_valid(&priv->cs_gpios[i]))
292 continue;
293
Wenyou Yang0eafd4b2016-10-28 14:17:49 +0800294 dm_gpio_set_dir_flags(&priv->cs_gpios[i],
295 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
296 }
Jagan Teki9bf48e22018-03-14 18:46:31 +0530297#endif
Wenyou Yang0eafd4b2016-10-28 14:17:49 +0800298
299 writel(ATMEL_SPI_CR_SWRST, &bus_plat->regs->cr);
300
301 return 0;
302}
303
304static const struct udevice_id atmel_spi_ids[] = {
305 { .compatible = "atmel,at91rm9200-spi" },
306 { }
307};
308
309U_BOOT_DRIVER(atmel_spi) = {
310 .name = "atmel_spi",
311 .id = UCLASS_SPI,
312 .of_match = atmel_spi_ids,
313 .ops = &atmel_spi_ops,
314 .platdata_auto_alloc_size = sizeof(struct atmel_spi_platdata),
315 .priv_auto_alloc_size = sizeof(struct atmel_spi_priv),
316 .probe = atmel_spi_probe,
317};