8cdb520608c8d48d69880c31792c5524adcabe42
[oweals/u-boot.git] / drivers / spi / soft_spi.c
1 /*
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
21 DECLARE_GLOBAL_DATA_PTR;
22
23 struct soft_spi_platdata {
24         struct gpio_desc cs;
25         struct gpio_desc sclk;
26         struct gpio_desc mosi;
27         struct gpio_desc miso;
28         int spi_delay_us;
29 };
30
31 struct soft_spi_priv {
32         unsigned int mode;
33 };
34
35 static int soft_spi_scl(struct udevice *dev, int bit)
36 {
37         struct udevice *bus = dev_get_parent(dev);
38         struct soft_spi_platdata *plat = dev_get_platdata(bus);
39
40         dm_gpio_set_value(&plat->sclk, bit);
41
42         return 0;
43 }
44
45 static int soft_spi_sda(struct udevice *dev, int bit)
46 {
47         struct udevice *bus = dev_get_parent(dev);
48         struct soft_spi_platdata *plat = dev_get_platdata(bus);
49
50         dm_gpio_set_value(&plat->mosi, bit);
51
52         return 0;
53 }
54
55 static int soft_spi_cs_activate(struct udevice *dev)
56 {
57         struct udevice *bus = dev_get_parent(dev);
58         struct soft_spi_platdata *plat = dev_get_platdata(bus);
59
60         dm_gpio_set_value(&plat->cs, 0);
61         dm_gpio_set_value(&plat->sclk, 0);
62         dm_gpio_set_value(&plat->cs, 1);
63
64         return 0;
65 }
66
67 static int soft_spi_cs_deactivate(struct udevice *dev)
68 {
69         struct udevice *bus = dev_get_parent(dev);
70         struct soft_spi_platdata *plat = dev_get_platdata(bus);
71
72         dm_gpio_set_value(&plat->cs, 0);
73
74         return 0;
75 }
76
77 static int soft_spi_claim_bus(struct udevice *dev)
78 {
79         /*
80          * Make sure the SPI clock is in idle state as defined for
81          * this slave.
82          */
83         return soft_spi_scl(dev, 0);
84 }
85
86 static int soft_spi_release_bus(struct udevice *dev)
87 {
88         /* Nothing to do */
89         return 0;
90 }
91
92 /*-----------------------------------------------------------------------
93  * SPI transfer
94  *
95  * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
96  * "bitlen" bits in the SPI MISO port.  That's just the way SPI works.
97  *
98  * The source of the outgoing bits is the "dout" parameter and the
99  * destination of the input bits is the "din" parameter.  Note that "dout"
100  * and "din" can point to the same memory location, in which case the
101  * input data overwrites the output data (since both are buffered by
102  * temporary variables, this is OK).
103  */
104 static int soft_spi_xfer(struct udevice *dev, unsigned int bitlen,
105                          const void *dout, void *din, unsigned long flags)
106 {
107         struct udevice *bus = dev_get_parent(dev);
108         struct soft_spi_priv *priv = dev_get_priv(bus);
109         struct soft_spi_platdata *plat = dev_get_platdata(bus);
110         uchar           tmpdin  = 0;
111         uchar           tmpdout = 0;
112         const u8        *txd = dout;
113         u8              *rxd = din;
114         int             cpha = priv->mode & SPI_CPHA;
115         unsigned int    j;
116
117         debug("spi_xfer: slave %s:%s dout %08X din %08X bitlen %u\n",
118               dev->parent->name, dev->name, *(uint *)txd, *(uint *)rxd,
119               bitlen);
120
121         if (flags & SPI_XFER_BEGIN)
122                 soft_spi_cs_activate(dev);
123
124         for (j = 0; j < bitlen; j++) {
125                 /*
126                  * Check if it is time to work on a new byte.
127                  */
128                 if ((j % 8) == 0) {
129                         if (txd)
130                                 tmpdout = *txd++;
131                         else
132                                 tmpdout = 0;
133                         if (j != 0) {
134                                 if (rxd)
135                                         *rxd++ = tmpdin;
136                         }
137                         tmpdin  = 0;
138                 }
139
140                 if (!cpha)
141                         soft_spi_scl(dev, 0);
142                 soft_spi_sda(dev, !!(tmpdout & 0x80));
143                 udelay(plat->spi_delay_us);
144                 if (cpha)
145                         soft_spi_scl(dev, 0);
146                 else
147                         soft_spi_scl(dev, 1);
148                 tmpdin  <<= 1;
149                 tmpdin  |= dm_gpio_get_value(&plat->miso);
150                 tmpdout <<= 1;
151                 udelay(plat->spi_delay_us);
152                 if (cpha)
153                         soft_spi_scl(dev, 1);
154         }
155         /*
156          * If the number of bits isn't a multiple of 8, shift the last
157          * bits over to left-justify them.  Then store the last byte
158          * read in.
159          */
160         if (rxd) {
161                 if ((bitlen % 8) != 0)
162                         tmpdin <<= 8 - (bitlen % 8);
163                 *rxd++ = tmpdin;
164         }
165
166         if (flags & SPI_XFER_END)
167                 soft_spi_cs_deactivate(dev);
168
169         return 0;
170 }
171
172 static int soft_spi_set_speed(struct udevice *dev, unsigned int speed)
173 {
174         /* Accept any speed */
175         return 0;
176 }
177
178 static int soft_spi_set_mode(struct udevice *dev, unsigned int mode)
179 {
180         struct soft_spi_priv *priv = dev_get_priv(dev);
181
182         priv->mode = mode;
183
184         return 0;
185 }
186
187 static const struct dm_spi_ops soft_spi_ops = {
188         .claim_bus      = soft_spi_claim_bus,
189         .release_bus    = soft_spi_release_bus,
190         .xfer           = soft_spi_xfer,
191         .set_speed      = soft_spi_set_speed,
192         .set_mode       = soft_spi_set_mode,
193 };
194
195 static int soft_spi_ofdata_to_platdata(struct udevice *dev)
196 {
197         struct soft_spi_platdata *plat = dev->platdata;
198         const void *blob = gd->fdt_blob;
199         int node = dev->of_offset;
200
201         plat->spi_delay_us = fdtdec_get_int(blob, node, "spi-delay-us", 0);
202
203         return 0;
204 }
205
206 static int soft_spi_probe(struct udevice *dev)
207 {
208         struct spi_slave *slave = dev_get_parent_priv(dev);
209         struct soft_spi_platdata *plat = dev->platdata;
210         int cs_flags, clk_flags;
211
212         cs_flags = (slave->mode & SPI_CS_HIGH) ? 0 : GPIOD_ACTIVE_LOW;
213         clk_flags = (slave->mode & SPI_CPOL) ? GPIOD_ACTIVE_LOW : 0;
214         if (gpio_request_by_name(dev, "cs-gpio", 0, &plat->cs,
215                                  GPIOD_IS_OUT | cs_flags) ||
216             gpio_request_by_name(dev, "sclk-gpio", 0, &plat->sclk,
217                                  GPIOD_IS_OUT | clk_flags) ||
218             gpio_request_by_name(dev, "mosi-gpio", 0, &plat->mosi,
219                                  GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE) ||
220             gpio_request_by_name(dev, "miso-gpio", 0, &plat->miso,
221                                  GPIOD_IS_IN))
222                 return -EINVAL;
223
224         return 0;
225 }
226
227 static const struct udevice_id soft_spi_ids[] = {
228         { .compatible = "u-boot,soft-spi" },
229         { }
230 };
231
232 U_BOOT_DRIVER(soft_spi) = {
233         .name   = "soft_spi",
234         .id     = UCLASS_SPI,
235         .of_match = soft_spi_ids,
236         .ops    = &soft_spi_ops,
237         .ofdata_to_platdata = soft_spi_ofdata_to_platdata,
238         .platdata_auto_alloc_size = sizeof(struct soft_spi_platdata),
239         .priv_auto_alloc_size = sizeof(struct soft_spi_priv),
240         .probe  = soft_spi_probe,
241 };