1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
6 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
8 * Influenced by code from:
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
19 #include <asm/global_data.h>
21 #include <linux/bitops.h>
22 #include <linux/delay.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 struct soft_spi_plat {
28 struct gpio_desc sclk;
29 struct gpio_desc mosi;
30 struct gpio_desc miso;
35 #define SPI_MASTER_NO_RX BIT(0)
36 #define SPI_MASTER_NO_TX BIT(1)
38 struct soft_spi_priv {
42 static int soft_spi_scl(struct udevice *dev, int bit)
44 struct udevice *bus = dev_get_parent(dev);
45 struct soft_spi_plat *plat = dev_get_plat(bus);
47 dm_gpio_set_value(&plat->sclk, bit);
52 static int soft_spi_sda(struct udevice *dev, int bit)
54 struct udevice *bus = dev_get_parent(dev);
55 struct soft_spi_plat *plat = dev_get_plat(bus);
57 dm_gpio_set_value(&plat->mosi, bit);
62 static int soft_spi_cs_activate(struct udevice *dev)
64 struct udevice *bus = dev_get_parent(dev);
65 struct soft_spi_priv *priv = dev_get_priv(bus);
66 struct soft_spi_plat *plat = dev_get_plat(bus);
67 int cidle = !!(priv->mode & SPI_CPOL);
69 dm_gpio_set_value(&plat->cs, 0);
70 dm_gpio_set_value(&plat->sclk, cidle); /* to idle */
71 dm_gpio_set_value(&plat->cs, 1);
76 static int soft_spi_cs_deactivate(struct udevice *dev)
78 struct udevice *bus = dev_get_parent(dev);
79 struct soft_spi_plat *plat = dev_get_plat(bus);
81 dm_gpio_set_value(&plat->cs, 0);
86 static int soft_spi_claim_bus(struct udevice *dev)
88 struct udevice *bus = dev_get_parent(dev);
89 struct soft_spi_priv *priv = dev_get_priv(bus);
90 int cidle = !!(priv->mode & SPI_CPOL);
92 * Make sure the SPI clock is in idle state as defined for
95 return soft_spi_scl(dev, cidle);
98 static int soft_spi_release_bus(struct udevice *dev)
104 /*-----------------------------------------------------------------------
107 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
108 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
110 * The source of the outgoing bits is the "dout" parameter and the
111 * destination of the input bits is the "din" parameter. Note that "dout"
112 * and "din" can point to the same memory location, in which case the
113 * input data overwrites the output data (since both are buffered by
114 * temporary variables, this is OK).
116 static int soft_spi_xfer(struct udevice *dev, unsigned int bitlen,
117 const void *dout, void *din, unsigned long flags)
119 struct udevice *bus = dev_get_parent(dev);
120 struct soft_spi_priv *priv = dev_get_priv(bus);
121 struct soft_spi_plat *plat = dev_get_plat(bus);
124 const u8 *txd = dout;
126 int cpha = !!(priv->mode & SPI_CPHA);
127 int cidle = !!(priv->mode & SPI_CPOL);
130 debug("spi_xfer: slave %s:%s dout %08X din %08X bitlen %u\n",
131 dev->parent->name, dev->name, *(uint *)txd, *(uint *)rxd,
134 if (flags & SPI_XFER_BEGIN)
135 soft_spi_cs_activate(dev);
137 for (j = 0; j < bitlen; j++) {
139 * Check if it is time to work on a new byte.
154 * CPOL 0: idle is low (0), active is high (1)
155 * CPOL 1: idle is high (1), active is low (0)
160 * CPHA 1: CLK from idle to active
163 soft_spi_scl(dev, !cidle);
164 if ((plat->flags & SPI_MASTER_NO_TX) == 0)
165 soft_spi_sda(dev, !!(tmpdout & 0x80));
166 udelay(plat->spi_delay_us);
170 * CPHA 0: CLK from idle to active
171 * CPHA 1: CLK from active to idle
174 soft_spi_scl(dev, !cidle);
176 soft_spi_scl(dev, cidle);
178 if ((plat->flags & SPI_MASTER_NO_RX) == 0)
179 tmpdin |= dm_gpio_get_value(&plat->miso);
181 udelay(plat->spi_delay_us);
185 * CPHA 0: CLK from active to idle
188 soft_spi_scl(dev, cidle);
191 * If the number of bits isn't a multiple of 8, shift the last
192 * bits over to left-justify them. Then store the last byte
196 if ((bitlen % 8) != 0)
197 tmpdin <<= 8 - (bitlen % 8);
201 if (flags & SPI_XFER_END)
202 soft_spi_cs_deactivate(dev);
207 static int soft_spi_set_speed(struct udevice *dev, unsigned int speed)
209 /* Ignore any speed settings. Speed is implemented via "spi-delay-us" */
213 static int soft_spi_set_mode(struct udevice *dev, unsigned int mode)
215 struct soft_spi_priv *priv = dev_get_priv(dev);
222 static const struct dm_spi_ops soft_spi_ops = {
223 .claim_bus = soft_spi_claim_bus,
224 .release_bus = soft_spi_release_bus,
225 .xfer = soft_spi_xfer,
226 .set_speed = soft_spi_set_speed,
227 .set_mode = soft_spi_set_mode,
230 static int soft_spi_of_to_plat(struct udevice *dev)
232 struct soft_spi_plat *plat = dev_get_plat(dev);
233 const void *blob = gd->fdt_blob;
234 int node = dev_of_offset(dev);
236 plat->spi_delay_us = fdtdec_get_int(blob, node, "spi-delay-us", 0);
241 static int soft_spi_probe(struct udevice *dev)
243 struct spi_slave *slave = dev_get_parent_priv(dev);
244 struct soft_spi_plat *plat = dev_get_plat(dev);
245 int cs_flags, clk_flags;
248 cs_flags = (slave && slave->mode & SPI_CS_HIGH) ? 0 : GPIOD_ACTIVE_LOW;
249 clk_flags = (slave && slave->mode & SPI_CPOL) ? GPIOD_ACTIVE_LOW : 0;
251 if (gpio_request_by_name(dev, "cs-gpios", 0, &plat->cs,
252 GPIOD_IS_OUT | cs_flags) ||
253 gpio_request_by_name(dev, "gpio-sck", 0, &plat->sclk,
254 GPIOD_IS_OUT | clk_flags))
257 ret = gpio_request_by_name(dev, "gpio-mosi", 0, &plat->mosi,
258 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
260 plat->flags |= SPI_MASTER_NO_TX;
262 ret = gpio_request_by_name(dev, "gpio-miso", 0, &plat->miso,
265 plat->flags |= SPI_MASTER_NO_RX;
267 if ((plat->flags & (SPI_MASTER_NO_RX | SPI_MASTER_NO_TX)) ==
268 (SPI_MASTER_NO_RX | SPI_MASTER_NO_TX))
274 static const struct udevice_id soft_spi_ids[] = {
275 { .compatible = "spi-gpio" },
279 U_BOOT_DRIVER(soft_spi) = {
282 .of_match = soft_spi_ids,
283 .ops = &soft_spi_ops,
284 .of_to_plat = soft_spi_of_to_plat,
285 .plat_auto = sizeof(struct soft_spi_plat),
286 .priv_auto = sizeof(struct soft_spi_priv),
287 .probe = soft_spi_probe,