1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2007 Atmel Corporation
15 #include <asm/arch/clk.h>
16 #include <asm/arch/hardware.h>
18 #include <asm/arch/at91_spi.h>
24 #include "atmel_spi.h"
28 static int spi_has_wdrbt(struct atmel_spi_slave *slave)
32 ver = spi_readl(slave, VERSION);
34 return (ATMEL_SPI_VERSION_REV(ver) >= 0x210);
37 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
38 unsigned int max_hz, unsigned int mode)
40 struct atmel_spi_slave *as;
45 if (!spi_cs_is_valid(bus, cs))
50 regs = (void *)ATMEL_BASE_SPI0;
52 #ifdef ATMEL_BASE_SPI1
54 regs = (void *)ATMEL_BASE_SPI1;
57 #ifdef ATMEL_BASE_SPI2
59 regs = (void *)ATMEL_BASE_SPI2;
62 #ifdef ATMEL_BASE_SPI3
64 regs = (void *)ATMEL_BASE_SPI3;
72 scbr = (get_spi_clk_rate(bus) + max_hz - 1) / max_hz;
73 if (scbr > ATMEL_SPI_CSRx_SCBR_MAX)
74 /* Too low max SCK rate */
79 csrx = ATMEL_SPI_CSRx_SCBR(scbr);
80 csrx |= ATMEL_SPI_CSRx_BITS(ATMEL_SPI_BITS_8);
81 if (!(mode & SPI_CPHA))
82 csrx |= ATMEL_SPI_CSRx_NCPHA;
84 csrx |= ATMEL_SPI_CSRx_CPOL;
86 as = spi_alloc_slave(struct atmel_spi_slave, bus, cs);
91 as->mr = ATMEL_SPI_MR_MSTR | ATMEL_SPI_MR_MODFDIS
92 | ATMEL_SPI_MR_PCS(~(1 << cs) & 0xf);
93 if (spi_has_wdrbt(as))
94 as->mr |= ATMEL_SPI_MR_WDRBT;
96 spi_writel(as, CSR(cs), csrx);
101 void spi_free_slave(struct spi_slave *slave)
103 struct atmel_spi_slave *as = to_atmel_spi(slave);
108 int spi_claim_bus(struct spi_slave *slave)
110 struct atmel_spi_slave *as = to_atmel_spi(slave);
112 /* Enable the SPI hardware */
113 spi_writel(as, CR, ATMEL_SPI_CR_SPIEN);
116 * Select the slave. This should set SCK to the correct
117 * initial state, etc.
119 spi_writel(as, MR, as->mr);
124 void spi_release_bus(struct spi_slave *slave)
126 struct atmel_spi_slave *as = to_atmel_spi(slave);
128 /* Disable the SPI hardware */
129 spi_writel(as, CR, ATMEL_SPI_CR_SPIDIS);
132 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
133 const void *dout, void *din, unsigned long flags)
135 struct atmel_spi_slave *as = to_atmel_spi(slave);
140 const u8 *txp = dout;
145 /* Finish any previously submitted transfers */
149 * TODO: The controller can do non-multiple-of-8 bit
150 * transfers, but this driver currently doesn't support it.
152 * It's also not clear how such transfers are supposed to be
153 * represented as a stream of bytes...this is a limitation of
154 * the current SPI interface.
157 /* Errors always terminate an ongoing transfer */
158 flags |= SPI_XFER_END;
165 * The controller can do automatic CS control, but it is
166 * somewhat quirky, and it doesn't really buy us much anyway
167 * in the context of U-Boot.
169 if (flags & SPI_XFER_BEGIN) {
170 spi_cs_activate(slave);
172 * sometimes the RDR is not empty when we get here,
173 * in theory that should not happen, but it DOES happen.
174 * Read it here to be on the safe side.
175 * That also clears the OVRES flag. Required if the
176 * following loop exits due to OVRES!
181 for (len_tx = 0, len_rx = 0; len_rx < len; ) {
182 status = spi_readl(as, SR);
184 if (status & ATMEL_SPI_SR_OVRES)
187 if (len_tx < len && (status & ATMEL_SPI_SR_TDRE)) {
192 spi_writel(as, TDR, value);
195 if (status & ATMEL_SPI_SR_RDRF) {
196 value = spi_readl(as, RDR);
204 if (flags & SPI_XFER_END) {
206 * Wait until the transfer is completely done before
210 status = spi_readl(as, SR);
211 } while (!(status & ATMEL_SPI_SR_TXEMPTY));
213 spi_cs_deactivate(slave);
221 #define MAX_CS_COUNT 4
223 struct atmel_spi_platdata {
224 struct at91_spi *regs;
227 struct atmel_spi_priv {
228 unsigned int freq; /* Default frequency */
231 #ifdef CONFIG_DM_GPIO
232 struct gpio_desc cs_gpios[MAX_CS_COUNT];
236 static int atmel_spi_claim_bus(struct udevice *dev)
238 struct udevice *bus = dev_get_parent(dev);
239 struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
240 struct atmel_spi_priv *priv = dev_get_priv(bus);
241 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
242 struct at91_spi *reg_base = bus_plat->regs;
243 u32 cs = slave_plat->cs;
244 u32 freq = priv->freq;
245 u32 scbr, csrx, mode;
247 scbr = (priv->bus_clk_rate + freq - 1) / freq;
248 if (scbr > ATMEL_SPI_CSRx_SCBR_MAX)
254 csrx = ATMEL_SPI_CSRx_SCBR(scbr);
255 csrx |= ATMEL_SPI_CSRx_BITS(ATMEL_SPI_BITS_8);
257 if (!(priv->mode & SPI_CPHA))
258 csrx |= ATMEL_SPI_CSRx_NCPHA;
259 if (priv->mode & SPI_CPOL)
260 csrx |= ATMEL_SPI_CSRx_CPOL;
262 writel(csrx, ®_base->csr[cs]);
264 mode = ATMEL_SPI_MR_MSTR |
265 ATMEL_SPI_MR_MODFDIS |
267 ATMEL_SPI_MR_PCS(~(1 << cs));
269 writel(mode, ®_base->mr);
271 writel(ATMEL_SPI_CR_SPIEN, ®_base->cr);
276 static int atmel_spi_release_bus(struct udevice *dev)
278 struct udevice *bus = dev_get_parent(dev);
279 struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
281 writel(ATMEL_SPI_CR_SPIDIS, &bus_plat->regs->cr);
286 static void atmel_spi_cs_activate(struct udevice *dev)
288 #ifdef CONFIG_DM_GPIO
289 struct udevice *bus = dev_get_parent(dev);
290 struct atmel_spi_priv *priv = dev_get_priv(bus);
291 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
292 u32 cs = slave_plat->cs;
294 if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
297 dm_gpio_set_value(&priv->cs_gpios[cs], 0);
301 static void atmel_spi_cs_deactivate(struct udevice *dev)
303 #ifdef CONFIG_DM_GPIO
304 struct udevice *bus = dev_get_parent(dev);
305 struct atmel_spi_priv *priv = dev_get_priv(bus);
306 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
307 u32 cs = slave_plat->cs;
309 if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
312 dm_gpio_set_value(&priv->cs_gpios[cs], 1);
316 static int atmel_spi_xfer(struct udevice *dev, unsigned int bitlen,
317 const void *dout, void *din, unsigned long flags)
319 struct udevice *bus = dev_get_parent(dev);
320 struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
321 struct at91_spi *reg_base = bus_plat->regs;
323 u32 len_tx, len_rx, len;
325 const u8 *txp = dout;
333 * The controller can do non-multiple-of-8 bit
334 * transfers, but this driver currently doesn't support it.
336 * It's also not clear how such transfers are supposed to be
337 * represented as a stream of bytes...this is a limitation of
338 * the current SPI interface.
341 /* Errors always terminate an ongoing transfer */
342 flags |= SPI_XFER_END;
349 * The controller can do automatic CS control, but it is
350 * somewhat quirky, and it doesn't really buy us much anyway
351 * in the context of U-Boot.
353 if (flags & SPI_XFER_BEGIN) {
354 atmel_spi_cs_activate(dev);
357 * sometimes the RDR is not empty when we get here,
358 * in theory that should not happen, but it DOES happen.
359 * Read it here to be on the safe side.
360 * That also clears the OVRES flag. Required if the
361 * following loop exits due to OVRES!
363 readl(®_base->rdr);
366 for (len_tx = 0, len_rx = 0; len_rx < len; ) {
367 status = readl(®_base->sr);
369 if (status & ATMEL_SPI_SR_OVRES)
372 if ((len_tx < len) && (status & ATMEL_SPI_SR_TDRE)) {
377 writel(value, ®_base->tdr);
381 if (status & ATMEL_SPI_SR_RDRF) {
382 value = readl(®_base->rdr);
390 if (flags & SPI_XFER_END) {
392 * Wait until the transfer is completely done before
395 wait_for_bit_le32(®_base->sr,
396 ATMEL_SPI_SR_TXEMPTY, true, 1000, false);
398 atmel_spi_cs_deactivate(dev);
404 static int atmel_spi_set_speed(struct udevice *bus, uint speed)
406 struct atmel_spi_priv *priv = dev_get_priv(bus);
413 static int atmel_spi_set_mode(struct udevice *bus, uint mode)
415 struct atmel_spi_priv *priv = dev_get_priv(bus);
422 static const struct dm_spi_ops atmel_spi_ops = {
423 .claim_bus = atmel_spi_claim_bus,
424 .release_bus = atmel_spi_release_bus,
425 .xfer = atmel_spi_xfer,
426 .set_speed = atmel_spi_set_speed,
427 .set_mode = atmel_spi_set_mode,
429 * cs_info is not needed, since we require all chip selects to be
430 * in the device tree explicitly
434 static int atmel_spi_enable_clk(struct udevice *bus)
436 struct atmel_spi_priv *priv = dev_get_priv(bus);
441 ret = clk_get_by_index(bus, 0, &clk);
445 ret = clk_enable(&clk);
449 clk_rate = clk_get_rate(&clk);
453 priv->bus_clk_rate = clk_rate;
460 static int atmel_spi_probe(struct udevice *bus)
462 struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
465 ret = atmel_spi_enable_clk(bus);
469 bus_plat->regs = (struct at91_spi *)devfdt_get_addr(bus);
471 #ifdef CONFIG_DM_GPIO
472 struct atmel_spi_priv *priv = dev_get_priv(bus);
475 ret = gpio_request_list_by_name(bus, "cs-gpios", priv->cs_gpios,
476 ARRAY_SIZE(priv->cs_gpios), 0);
478 pr_err("Can't get %s gpios! Error: %d", bus->name, ret);
482 for(i = 0; i < ARRAY_SIZE(priv->cs_gpios); i++) {
483 if (!dm_gpio_is_valid(&priv->cs_gpios[i]))
486 dm_gpio_set_dir_flags(&priv->cs_gpios[i],
487 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
491 writel(ATMEL_SPI_CR_SWRST, &bus_plat->regs->cr);
496 static const struct udevice_id atmel_spi_ids[] = {
497 { .compatible = "atmel,at91rm9200-spi" },
501 U_BOOT_DRIVER(atmel_spi) = {
504 .of_match = atmel_spi_ids,
505 .ops = &atmel_spi_ops,
506 .platdata_auto_alloc_size = sizeof(struct atmel_spi_platdata),
507 .priv_auto_alloc_size = sizeof(struct atmel_spi_priv),
508 .probe = atmel_spi_probe,