2 * Copyright (c) 2014 Google, Inc
5 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
7 * Influenced by code from:
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10 * SPDX-License-Identifier: GPL-2.0+
21 DECLARE_GLOBAL_DATA_PTR;
23 struct soft_spi_platdata {
25 struct gpio_desc sclk;
26 struct gpio_desc mosi;
27 struct gpio_desc miso;
31 struct soft_spi_priv {
35 static int soft_spi_scl(struct udevice *dev, int bit)
37 struct soft_spi_platdata *plat = dev->platdata;
39 dm_gpio_set_value(&plat->sclk, bit);
44 static int soft_spi_sda(struct udevice *dev, int bit)
46 struct soft_spi_platdata *plat = dev->platdata;
48 dm_gpio_set_value(&plat->mosi, bit);
53 static int soft_spi_cs_activate(struct udevice *dev)
55 struct soft_spi_platdata *plat = dev->platdata;
57 dm_gpio_set_value(&plat->cs, 0);
58 dm_gpio_set_value(&plat->sclk, 0);
59 dm_gpio_set_value(&plat->cs, 1);
64 static int soft_spi_cs_deactivate(struct udevice *dev)
66 struct soft_spi_platdata *plat = dev->platdata;
68 dm_gpio_set_value(&plat->cs, 0);
73 static int soft_spi_claim_bus(struct udevice *dev)
76 * Make sure the SPI clock is in idle state as defined for
79 return soft_spi_scl(dev, 0);
82 static int soft_spi_release_bus(struct udevice *dev)
88 /*-----------------------------------------------------------------------
91 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
92 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
94 * The source of the outgoing bits is the "dout" parameter and the
95 * destination of the input bits is the "din" parameter. Note that "dout"
96 * and "din" can point to the same memory location, in which case the
97 * input data overwrites the output data (since both are buffered by
98 * temporary variables, this is OK).
100 static int soft_spi_xfer(struct udevice *dev, unsigned int bitlen,
101 const void *dout, void *din, unsigned long flags)
103 struct soft_spi_priv *priv = dev_get_priv(dev);
104 struct soft_spi_platdata *plat = dev->platdata;
107 const u8 *txd = dout;
109 int cpha = priv->mode & SPI_CPHA;
112 debug("spi_xfer: slave %s:%s dout %08X din %08X bitlen %u\n",
113 dev->parent->name, dev->name, *(uint *)txd, *(uint *)rxd,
116 if (flags & SPI_XFER_BEGIN)
117 soft_spi_cs_activate(dev);
119 for (j = 0; j < bitlen; j++) {
121 * Check if it is time to work on a new byte.
136 soft_spi_scl(dev, 0);
137 soft_spi_sda(dev, tmpdout & 0x80);
138 udelay(plat->spi_delay_us);
140 soft_spi_scl(dev, 0);
142 soft_spi_scl(dev, 1);
144 tmpdin |= dm_gpio_get_value(&plat->miso);
146 udelay(plat->spi_delay_us);
148 soft_spi_scl(dev, 1);
151 * If the number of bits isn't a multiple of 8, shift the last
152 * bits over to left-justify them. Then store the last byte
156 if ((bitlen % 8) != 0)
157 tmpdin <<= 8 - (bitlen % 8);
161 if (flags & SPI_XFER_END)
162 soft_spi_cs_deactivate(dev);
167 static int soft_spi_set_speed(struct udevice *dev, unsigned int speed)
169 /* Accept any speed */
173 static int soft_spi_set_mode(struct udevice *dev, unsigned int mode)
175 struct soft_spi_priv *priv = dev_get_priv(dev);
182 static const struct dm_spi_ops soft_spi_ops = {
183 .claim_bus = soft_spi_claim_bus,
184 .release_bus = soft_spi_release_bus,
185 .xfer = soft_spi_xfer,
186 .set_speed = soft_spi_set_speed,
187 .set_mode = soft_spi_set_mode,
190 static int soft_spi_ofdata_to_platdata(struct udevice *dev)
192 struct soft_spi_platdata *plat = dev->platdata;
193 const void *blob = gd->fdt_blob;
194 int node = dev->of_offset;
196 plat->spi_delay_us = fdtdec_get_int(blob, node, "spi-delay-us", 0);
201 static int soft_spi_probe(struct udevice *dev)
203 struct spi_slave *slave = dev_get_parent_priv(dev);
204 struct soft_spi_platdata *plat = dev->platdata;
205 int cs_flags, clk_flags;
207 cs_flags = (slave->mode & SPI_CS_HIGH) ? 0 : GPIOD_ACTIVE_LOW;
208 clk_flags = (slave->mode & SPI_CPOL) ? GPIOD_ACTIVE_LOW : 0;
209 if (gpio_request_by_name(dev, "cs-gpio", 0, &plat->cs,
210 GPIOD_IS_OUT | cs_flags) ||
211 gpio_request_by_name(dev, "sclk-gpio", 0, &plat->sclk,
212 GPIOD_IS_OUT | clk_flags) ||
213 gpio_request_by_name(dev, "mosi-gpio", 0, &plat->mosi,
214 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE) ||
215 gpio_request_by_name(dev, "miso-gpio", 0, &plat->miso,
222 static const struct udevice_id soft_spi_ids[] = {
223 { .compatible = "u-boot,soft-spi" },
227 U_BOOT_DRIVER(soft_spi) = {
230 .of_match = soft_spi_ids,
231 .ops = &soft_spi_ops,
232 .ofdata_to_platdata = soft_spi_ofdata_to_platdata,
233 .platdata_auto_alloc_size = sizeof(struct soft_spi_platdata),
234 .priv_auto_alloc_size = sizeof(struct soft_spi_priv),
235 .probe = soft_spi_probe,