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.
20 #include <linux/bitops.h>
21 #include <linux/delay.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 struct soft_spi_platdata {
27 struct gpio_desc sclk;
28 struct gpio_desc mosi;
29 struct gpio_desc miso;
34 #define SPI_MASTER_NO_RX BIT(0)
35 #define SPI_MASTER_NO_TX BIT(1)
37 struct soft_spi_priv {
41 static int soft_spi_scl(struct udevice *dev, int bit)
43 struct udevice *bus = dev_get_parent(dev);
44 struct soft_spi_platdata *plat = dev_get_platdata(bus);
46 dm_gpio_set_value(&plat->sclk, bit);
51 static int soft_spi_sda(struct udevice *dev, int bit)
53 struct udevice *bus = dev_get_parent(dev);
54 struct soft_spi_platdata *plat = dev_get_platdata(bus);
56 dm_gpio_set_value(&plat->mosi, bit);
61 static int soft_spi_cs_activate(struct udevice *dev)
63 struct udevice *bus = dev_get_parent(dev);
64 struct soft_spi_priv *priv = dev_get_priv(bus);
65 struct soft_spi_platdata *plat = dev_get_platdata(bus);
66 int cidle = !!(priv->mode & SPI_CPOL);
68 dm_gpio_set_value(&plat->cs, 0);
69 dm_gpio_set_value(&plat->sclk, cidle); /* to idle */
70 dm_gpio_set_value(&plat->cs, 1);
75 static int soft_spi_cs_deactivate(struct udevice *dev)
77 struct udevice *bus = dev_get_parent(dev);
78 struct soft_spi_platdata *plat = dev_get_platdata(bus);
80 dm_gpio_set_value(&plat->cs, 0);
85 static int soft_spi_claim_bus(struct udevice *dev)
87 struct udevice *bus = dev_get_parent(dev);
88 struct soft_spi_priv *priv = dev_get_priv(bus);
89 int cidle = !!(priv->mode & SPI_CPOL);
91 * Make sure the SPI clock is in idle state as defined for
94 return soft_spi_scl(dev, cidle);
97 static int soft_spi_release_bus(struct udevice *dev)
103 /*-----------------------------------------------------------------------
106 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
107 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
109 * The source of the outgoing bits is the "dout" parameter and the
110 * destination of the input bits is the "din" parameter. Note that "dout"
111 * and "din" can point to the same memory location, in which case the
112 * input data overwrites the output data (since both are buffered by
113 * temporary variables, this is OK).
115 static int soft_spi_xfer(struct udevice *dev, unsigned int bitlen,
116 const void *dout, void *din, unsigned long flags)
118 struct udevice *bus = dev_get_parent(dev);
119 struct soft_spi_priv *priv = dev_get_priv(bus);
120 struct soft_spi_platdata *plat = dev_get_platdata(bus);
123 const u8 *txd = dout;
125 int cpha = !!(priv->mode & SPI_CPHA);
126 int cidle = !!(priv->mode & SPI_CPOL);
129 debug("spi_xfer: slave %s:%s dout %08X din %08X bitlen %u\n",
130 dev->parent->name, dev->name, *(uint *)txd, *(uint *)rxd,
133 if (flags & SPI_XFER_BEGIN)
134 soft_spi_cs_activate(dev);
136 for (j = 0; j < bitlen; j++) {
138 * Check if it is time to work on a new byte.
153 * CPOL 0: idle is low (0), active is high (1)
154 * CPOL 1: idle is high (1), active is low (0)
159 * CPHA 1: CLK from idle to active
162 soft_spi_scl(dev, !cidle);
163 if ((plat->flags & SPI_MASTER_NO_TX) == 0)
164 soft_spi_sda(dev, !!(tmpdout & 0x80));
165 udelay(plat->spi_delay_us);
169 * CPHA 0: CLK from idle to active
170 * CPHA 1: CLK from active to idle
173 soft_spi_scl(dev, !cidle);
175 soft_spi_scl(dev, cidle);
177 if ((plat->flags & SPI_MASTER_NO_RX) == 0)
178 tmpdin |= dm_gpio_get_value(&plat->miso);
180 udelay(plat->spi_delay_us);
184 * CPHA 0: CLK from active to idle
187 soft_spi_scl(dev, cidle);
190 * If the number of bits isn't a multiple of 8, shift the last
191 * bits over to left-justify them. Then store the last byte
195 if ((bitlen % 8) != 0)
196 tmpdin <<= 8 - (bitlen % 8);
200 if (flags & SPI_XFER_END)
201 soft_spi_cs_deactivate(dev);
206 static int soft_spi_set_speed(struct udevice *dev, unsigned int speed)
208 /* Ignore any speed settings. Speed is implemented via "spi-delay-us" */
212 static int soft_spi_set_mode(struct udevice *dev, unsigned int mode)
214 struct soft_spi_priv *priv = dev_get_priv(dev);
221 static const struct dm_spi_ops soft_spi_ops = {
222 .claim_bus = soft_spi_claim_bus,
223 .release_bus = soft_spi_release_bus,
224 .xfer = soft_spi_xfer,
225 .set_speed = soft_spi_set_speed,
226 .set_mode = soft_spi_set_mode,
229 static int soft_spi_ofdata_to_platdata(struct udevice *dev)
231 struct soft_spi_platdata *plat = dev->platdata;
232 const void *blob = gd->fdt_blob;
233 int node = dev_of_offset(dev);
235 plat->spi_delay_us = fdtdec_get_int(blob, node, "spi-delay-us", 0);
240 static int soft_spi_probe(struct udevice *dev)
242 struct spi_slave *slave = dev_get_parent_priv(dev);
243 struct soft_spi_platdata *plat = dev->platdata;
244 int cs_flags, clk_flags;
247 cs_flags = (slave && slave->mode & SPI_CS_HIGH) ? 0 : GPIOD_ACTIVE_LOW;
248 clk_flags = (slave && slave->mode & SPI_CPOL) ? GPIOD_ACTIVE_LOW : 0;
250 if (gpio_request_by_name(dev, "cs-gpios", 0, &plat->cs,
251 GPIOD_IS_OUT | cs_flags) ||
252 gpio_request_by_name(dev, "gpio-sck", 0, &plat->sclk,
253 GPIOD_IS_OUT | clk_flags))
256 ret = gpio_request_by_name(dev, "gpio-mosi", 0, &plat->mosi,
257 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
259 plat->flags |= SPI_MASTER_NO_TX;
261 ret = gpio_request_by_name(dev, "gpio-miso", 0, &plat->miso,
264 plat->flags |= SPI_MASTER_NO_RX;
266 if ((plat->flags & (SPI_MASTER_NO_RX | SPI_MASTER_NO_TX)) ==
267 (SPI_MASTER_NO_RX | SPI_MASTER_NO_TX))
273 static const struct udevice_id soft_spi_ids[] = {
274 { .compatible = "spi-gpio" },
278 U_BOOT_DRIVER(soft_spi) = {
281 .of_match = soft_spi_ids,
282 .ops = &soft_spi_ops,
283 .ofdata_to_platdata = soft_spi_ofdata_to_platdata,
284 .platdata_auto_alloc_size = sizeof(struct soft_spi_platdata),
285 .priv_auto_alloc_size = sizeof(struct soft_spi_priv),
286 .probe = soft_spi_probe,