blob: 423c98d1a5abad52322ab6324f0df82d61c65eab [file] [log] [blame]
Simon Glass456dd7c2014-10-13 23:42:00 -06001/*
2 * Copyright (c) 2014 Google, Inc
3 *
4 * (C) Copyright 2002
5 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
6 *
7 * Influenced by code from:
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 *
10 * SPDX-License-Identifier: GPL-2.0+
11 */
12
13#include <common.h>
14#include <dm.h>
15#include <errno.h>
16#include <fdtdec.h>
17#include <malloc.h>
18#include <spi.h>
19#include <asm/gpio.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
23struct soft_spi_platdata {
Simon Glass61d10b72015-01-05 20:05:40 -070024 struct gpio_desc cs;
25 struct gpio_desc sclk;
26 struct gpio_desc mosi;
27 struct gpio_desc miso;
Simon Glass456dd7c2014-10-13 23:42:00 -060028 int spi_delay_us;
29};
30
31struct soft_spi_priv {
32 unsigned int mode;
33};
34
35static int soft_spi_scl(struct udevice *dev, int bit)
36{
37 struct soft_spi_platdata *plat = dev->platdata;
Simon Glass456dd7c2014-10-13 23:42:00 -060038
Simon Glass61d10b72015-01-05 20:05:40 -070039 dm_gpio_set_value(&plat->sclk, bit);
Simon Glass456dd7c2014-10-13 23:42:00 -060040
41 return 0;
42}
43
44static int soft_spi_sda(struct udevice *dev, int bit)
45{
46 struct soft_spi_platdata *plat = dev->platdata;
47
Simon Glass61d10b72015-01-05 20:05:40 -070048 dm_gpio_set_value(&plat->mosi, bit);
Simon Glass456dd7c2014-10-13 23:42:00 -060049
50 return 0;
51}
52
53static int soft_spi_cs_activate(struct udevice *dev)
54{
55 struct soft_spi_platdata *plat = dev->platdata;
Simon Glass456dd7c2014-10-13 23:42:00 -060056
Simon Glass61d10b72015-01-05 20:05:40 -070057 dm_gpio_set_value(&plat->cs, 0);
58 dm_gpio_set_value(&plat->sclk, 0);
59 dm_gpio_set_value(&plat->cs, 1);
Simon Glass456dd7c2014-10-13 23:42:00 -060060
61 return 0;
62}
63
64static int soft_spi_cs_deactivate(struct udevice *dev)
65{
66 struct soft_spi_platdata *plat = dev->platdata;
Simon Glass456dd7c2014-10-13 23:42:00 -060067
Simon Glass61d10b72015-01-05 20:05:40 -070068 dm_gpio_set_value(&plat->cs, 0);
Simon Glass456dd7c2014-10-13 23:42:00 -060069
70 return 0;
71}
72
73static int soft_spi_claim_bus(struct udevice *dev)
74{
75 /*
76 * Make sure the SPI clock is in idle state as defined for
77 * this slave.
78 */
79 return soft_spi_scl(dev, 0);
80}
81
82static int soft_spi_release_bus(struct udevice *dev)
83{
84 /* Nothing to do */
85 return 0;
86}
87
88/*-----------------------------------------------------------------------
89 * SPI transfer
90 *
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.
93 *
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).
99 */
100static int soft_spi_xfer(struct udevice *dev, unsigned int bitlen,
101 const void *dout, void *din, unsigned long flags)
102{
103 struct soft_spi_priv *priv = dev_get_priv(dev);
104 struct soft_spi_platdata *plat = dev->platdata;
105 uchar tmpdin = 0;
106 uchar tmpdout = 0;
107 const u8 *txd = dout;
108 u8 *rxd = din;
Simon Glass456dd7c2014-10-13 23:42:00 -0600109 int cpha = priv->mode & SPI_CPHA;
110 unsigned int j;
111
112 debug("spi_xfer: slave %s:%s dout %08X din %08X bitlen %u\n",
113 dev->parent->name, dev->name, *(uint *)txd, *(uint *)rxd,
114 bitlen);
115
116 if (flags & SPI_XFER_BEGIN)
117 soft_spi_cs_activate(dev);
118
119 for (j = 0; j < bitlen; j++) {
120 /*
121 * Check if it is time to work on a new byte.
122 */
123 if ((j % 8) == 0) {
124 if (txd)
125 tmpdout = *txd++;
126 else
127 tmpdout = 0;
128 if (j != 0) {
129 if (rxd)
130 *rxd++ = tmpdin;
131 }
132 tmpdin = 0;
133 }
134
135 if (!cpha)
Simon Glass61d10b72015-01-05 20:05:40 -0700136 soft_spi_scl(dev, 0);
Simon Glass456dd7c2014-10-13 23:42:00 -0600137 soft_spi_sda(dev, tmpdout & 0x80);
138 udelay(plat->spi_delay_us);
139 if (cpha)
Simon Glass61d10b72015-01-05 20:05:40 -0700140 soft_spi_scl(dev, 0);
Simon Glass456dd7c2014-10-13 23:42:00 -0600141 else
Simon Glass61d10b72015-01-05 20:05:40 -0700142 soft_spi_scl(dev, 1);
Simon Glass456dd7c2014-10-13 23:42:00 -0600143 tmpdin <<= 1;
Simon Glass61d10b72015-01-05 20:05:40 -0700144 tmpdin |= dm_gpio_get_value(&plat->miso);
Simon Glass456dd7c2014-10-13 23:42:00 -0600145 tmpdout <<= 1;
146 udelay(plat->spi_delay_us);
147 if (cpha)
Simon Glass61d10b72015-01-05 20:05:40 -0700148 soft_spi_scl(dev, 1);
Simon Glass456dd7c2014-10-13 23:42:00 -0600149 }
150 /*
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
153 * read in.
154 */
155 if (rxd) {
156 if ((bitlen % 8) != 0)
157 tmpdin <<= 8 - (bitlen % 8);
158 *rxd++ = tmpdin;
159 }
160
161 if (flags & SPI_XFER_END)
162 soft_spi_cs_deactivate(dev);
163
164 return 0;
165}
166
167static int soft_spi_set_speed(struct udevice *dev, unsigned int speed)
168{
169 /* Accept any speed */
170 return 0;
171}
172
173static int soft_spi_set_mode(struct udevice *dev, unsigned int mode)
174{
175 struct soft_spi_priv *priv = dev_get_priv(dev);
176
177 priv->mode = mode;
178
179 return 0;
180}
181
182static int soft_spi_child_pre_probe(struct udevice *dev)
183{
184 struct spi_slave *slave = dev_get_parentdata(dev);
185
186 slave->dev = dev;
187 return spi_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, slave);
188}
189
190static const struct dm_spi_ops soft_spi_ops = {
191 .claim_bus = soft_spi_claim_bus,
192 .release_bus = soft_spi_release_bus,
193 .xfer = soft_spi_xfer,
194 .set_speed = soft_spi_set_speed,
195 .set_mode = soft_spi_set_mode,
196};
197
198static int soft_spi_ofdata_to_platdata(struct udevice *dev)
199{
200 struct soft_spi_platdata *plat = dev->platdata;
201 const void *blob = gd->fdt_blob;
202 int node = dev->of_offset;
203
Simon Glass456dd7c2014-10-13 23:42:00 -0600204 plat->spi_delay_us = fdtdec_get_int(blob, node, "spi-delay-us", 0);
205
206 return 0;
207}
208
209static int soft_spi_probe(struct udevice *dev)
210{
211 struct spi_slave *slave = dev_get_parentdata(dev);
212 struct soft_spi_platdata *plat = dev->platdata;
Simon Glass61d10b72015-01-05 20:05:40 -0700213 int cs_flags, clk_flags;
Simon Glass456dd7c2014-10-13 23:42:00 -0600214
Simon Glass61d10b72015-01-05 20:05:40 -0700215 cs_flags = (slave->mode & SPI_CS_HIGH) ? 0 : GPIOD_ACTIVE_LOW;
216 clk_flags = (slave->mode & SPI_CPOL) ? GPIOD_ACTIVE_LOW : 0;
217 if (gpio_request_by_name(dev, "cs-gpio", 0, &plat->cs,
218 GPIOD_IS_OUT | cs_flags) ||
219 gpio_request_by_name(dev, "sclk-gpio", 0, &plat->sclk,
220 GPIOD_IS_OUT | clk_flags) ||
221 gpio_request_by_name(dev, "mosi-gpio", 0, &plat->mosi,
222 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE) ||
223 gpio_request_by_name(dev, "miso-gpio", 0, &plat->miso,
224 GPIOD_IS_IN))
225 return -EINVAL;
Simon Glass456dd7c2014-10-13 23:42:00 -0600226
227 return 0;
228}
229
230static const struct udevice_id soft_spi_ids[] = {
231 { .compatible = "u-boot,soft-spi" },
232 { }
233};
234
235U_BOOT_DRIVER(soft_spi) = {
236 .name = "soft_spi",
237 .id = UCLASS_SPI,
238 .of_match = soft_spi_ids,
239 .ops = &soft_spi_ops,
240 .ofdata_to_platdata = soft_spi_ofdata_to_platdata,
241 .platdata_auto_alloc_size = sizeof(struct soft_spi_platdata),
242 .priv_auto_alloc_size = sizeof(struct soft_spi_priv),
243 .per_child_auto_alloc_size = sizeof(struct spi_slave),
244 .probe = soft_spi_probe,
245 .child_pre_probe = soft_spi_child_pre_probe,
246};