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_platdata *plat = dev_get_platdata(bus);
66 dm_gpio_set_value(&plat->cs, 0);
67 dm_gpio_set_value(&plat->sclk, 0);
68 dm_gpio_set_value(&plat->cs, 1);
73 static int soft_spi_cs_deactivate(struct udevice *dev)
75 struct udevice *bus = dev_get_parent(dev);
76 struct soft_spi_platdata *plat = dev_get_platdata(bus);
78 dm_gpio_set_value(&plat->cs, 0);
83 static int soft_spi_claim_bus(struct udevice *dev)
86 * Make sure the SPI clock is in idle state as defined for
89 return soft_spi_scl(dev, 0);
92 static int soft_spi_release_bus(struct udevice *dev)
98 /*-----------------------------------------------------------------------
101 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
102 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
104 * The source of the outgoing bits is the "dout" parameter and the
105 * destination of the input bits is the "din" parameter. Note that "dout"
106 * and "din" can point to the same memory location, in which case the
107 * input data overwrites the output data (since both are buffered by
108 * temporary variables, this is OK).
110 static int soft_spi_xfer(struct udevice *dev, unsigned int bitlen,
111 const void *dout, void *din, unsigned long flags)
113 struct udevice *bus = dev_get_parent(dev);
114 struct soft_spi_priv *priv = dev_get_priv(bus);
115 struct soft_spi_platdata *plat = dev_get_platdata(bus);
118 const u8 *txd = dout;
120 int cpha = priv->mode & SPI_CPHA;
123 debug("spi_xfer: slave %s:%s dout %08X din %08X bitlen %u\n",
124 dev->parent->name, dev->name, *(uint *)txd, *(uint *)rxd,
127 if (flags & SPI_XFER_BEGIN)
128 soft_spi_cs_activate(dev);
130 for (j = 0; j < bitlen; j++) {
132 * Check if it is time to work on a new byte.
147 soft_spi_scl(dev, 0);
148 if ((plat->flags & SPI_MASTER_NO_TX) == 0)
149 soft_spi_sda(dev, !!(tmpdout & 0x80));
150 udelay(plat->spi_delay_us);
152 soft_spi_scl(dev, 0);
154 soft_spi_scl(dev, 1);
156 if ((plat->flags & SPI_MASTER_NO_RX) == 0)
157 tmpdin |= dm_gpio_get_value(&plat->miso);
159 udelay(plat->spi_delay_us);
161 soft_spi_scl(dev, 1);
164 * If the number of bits isn't a multiple of 8, shift the last
165 * bits over to left-justify them. Then store the last byte
169 if ((bitlen % 8) != 0)
170 tmpdin <<= 8 - (bitlen % 8);
174 if (flags & SPI_XFER_END)
175 soft_spi_cs_deactivate(dev);
180 static int soft_spi_set_speed(struct udevice *dev, unsigned int speed)
182 /* Accept any speed */
186 static int soft_spi_set_mode(struct udevice *dev, unsigned int mode)
188 struct soft_spi_priv *priv = dev_get_priv(dev);
195 static const struct dm_spi_ops soft_spi_ops = {
196 .claim_bus = soft_spi_claim_bus,
197 .release_bus = soft_spi_release_bus,
198 .xfer = soft_spi_xfer,
199 .set_speed = soft_spi_set_speed,
200 .set_mode = soft_spi_set_mode,
203 static int soft_spi_ofdata_to_platdata(struct udevice *dev)
205 struct soft_spi_platdata *plat = dev->platdata;
206 const void *blob = gd->fdt_blob;
207 int node = dev_of_offset(dev);
209 plat->spi_delay_us = fdtdec_get_int(blob, node, "spi-delay-us", 0);
214 static int soft_spi_probe(struct udevice *dev)
216 struct spi_slave *slave = dev_get_parent_priv(dev);
217 struct soft_spi_platdata *plat = dev->platdata;
218 int cs_flags, clk_flags;
221 cs_flags = (slave && slave->mode & SPI_CS_HIGH) ? 0 : GPIOD_ACTIVE_LOW;
222 clk_flags = (slave && slave->mode & SPI_CPOL) ? GPIOD_ACTIVE_LOW : 0;
224 if (gpio_request_by_name(dev, "cs-gpios", 0, &plat->cs,
225 GPIOD_IS_OUT | cs_flags) ||
226 gpio_request_by_name(dev, "gpio-sck", 0, &plat->sclk,
227 GPIOD_IS_OUT | clk_flags))
230 ret = gpio_request_by_name(dev, "gpio-mosi", 0, &plat->mosi,
231 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
233 plat->flags |= SPI_MASTER_NO_TX;
235 ret = gpio_request_by_name(dev, "gpio-miso", 0, &plat->miso,
238 plat->flags |= SPI_MASTER_NO_RX;
240 if ((plat->flags & (SPI_MASTER_NO_RX | SPI_MASTER_NO_TX)) ==
241 (SPI_MASTER_NO_RX | SPI_MASTER_NO_TX))
247 static const struct udevice_id soft_spi_ids[] = {
248 { .compatible = "spi-gpio" },
252 U_BOOT_DRIVER(soft_spi) = {
255 .of_match = soft_spi_ids,
256 .ops = &soft_spi_ops,
257 .ofdata_to_platdata = soft_spi_ofdata_to_platdata,
258 .platdata_auto_alloc_size = sizeof(struct soft_spi_platdata),
259 .priv_auto_alloc_size = sizeof(struct soft_spi_priv),
260 .probe = soft_spi_probe,