blob: e5f9f49caf8235cfdf2f42debfec8134efd3a914 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass623b6382014-10-13 23:42:00 -06002/*
3 * Copyright (c) 2014 Google, Inc
4 *
5 * (C) Copyright 2002
6 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
7 *
8 * Influenced by code from:
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass623b6382014-10-13 23:42:00 -060010 */
11
12#include <common.h>
13#include <dm.h>
14#include <errno.h>
15#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Simon Glass623b6382014-10-13 23:42:00 -060017#include <malloc.h>
18#include <spi.h>
19#include <asm/gpio.h>
Simon Glassc05ed002020-05-10 11:40:11 -060020#include <linux/delay.h>
Simon Glass623b6382014-10-13 23:42:00 -060021
22DECLARE_GLOBAL_DATA_PTR;
23
24struct soft_spi_platdata {
Simon Glass050fb902015-01-05 20:05:40 -070025 struct gpio_desc cs;
26 struct gpio_desc sclk;
27 struct gpio_desc mosi;
28 struct gpio_desc miso;
Simon Glass623b6382014-10-13 23:42:00 -060029 int spi_delay_us;
Peng Fan102412c2016-05-03 10:02:21 +080030 int flags;
Simon Glass623b6382014-10-13 23:42:00 -060031};
32
Peng Fan102412c2016-05-03 10:02:21 +080033#define SPI_MASTER_NO_RX BIT(0)
34#define SPI_MASTER_NO_TX BIT(1)
35
Simon Glass623b6382014-10-13 23:42:00 -060036struct soft_spi_priv {
37 unsigned int mode;
38};
39
40static int soft_spi_scl(struct udevice *dev, int bit)
41{
Peng Fanb6d54d52016-05-03 10:02:20 +080042 struct udevice *bus = dev_get_parent(dev);
43 struct soft_spi_platdata *plat = dev_get_platdata(bus);
Simon Glass623b6382014-10-13 23:42:00 -060044
Simon Glass050fb902015-01-05 20:05:40 -070045 dm_gpio_set_value(&plat->sclk, bit);
Simon Glass623b6382014-10-13 23:42:00 -060046
47 return 0;
48}
49
50static int soft_spi_sda(struct udevice *dev, int bit)
51{
Peng Fanb6d54d52016-05-03 10:02:20 +080052 struct udevice *bus = dev_get_parent(dev);
53 struct soft_spi_platdata *plat = dev_get_platdata(bus);
Simon Glass623b6382014-10-13 23:42:00 -060054
Simon Glass050fb902015-01-05 20:05:40 -070055 dm_gpio_set_value(&plat->mosi, bit);
Simon Glass623b6382014-10-13 23:42:00 -060056
57 return 0;
58}
59
60static int soft_spi_cs_activate(struct udevice *dev)
61{
Peng Fanb6d54d52016-05-03 10:02:20 +080062 struct udevice *bus = dev_get_parent(dev);
63 struct soft_spi_platdata *plat = dev_get_platdata(bus);
Simon Glass623b6382014-10-13 23:42:00 -060064
Simon Glass050fb902015-01-05 20:05:40 -070065 dm_gpio_set_value(&plat->cs, 0);
66 dm_gpio_set_value(&plat->sclk, 0);
67 dm_gpio_set_value(&plat->cs, 1);
Simon Glass623b6382014-10-13 23:42:00 -060068
69 return 0;
70}
71
72static int soft_spi_cs_deactivate(struct udevice *dev)
73{
Peng Fanb6d54d52016-05-03 10:02:20 +080074 struct udevice *bus = dev_get_parent(dev);
75 struct soft_spi_platdata *plat = dev_get_platdata(bus);
Simon Glass623b6382014-10-13 23:42:00 -060076
Simon Glass050fb902015-01-05 20:05:40 -070077 dm_gpio_set_value(&plat->cs, 0);
Simon Glass623b6382014-10-13 23:42:00 -060078
79 return 0;
80}
81
82static int soft_spi_claim_bus(struct udevice *dev)
83{
84 /*
85 * Make sure the SPI clock is in idle state as defined for
86 * this slave.
87 */
88 return soft_spi_scl(dev, 0);
89}
90
91static int soft_spi_release_bus(struct udevice *dev)
92{
93 /* Nothing to do */
94 return 0;
95}
96
97/*-----------------------------------------------------------------------
98 * SPI transfer
99 *
100 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
101 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
102 *
103 * The source of the outgoing bits is the "dout" parameter and the
104 * destination of the input bits is the "din" parameter. Note that "dout"
105 * and "din" can point to the same memory location, in which case the
106 * input data overwrites the output data (since both are buffered by
107 * temporary variables, this is OK).
108 */
109static int soft_spi_xfer(struct udevice *dev, unsigned int bitlen,
110 const void *dout, void *din, unsigned long flags)
111{
Peng Fanb6d54d52016-05-03 10:02:20 +0800112 struct udevice *bus = dev_get_parent(dev);
113 struct soft_spi_priv *priv = dev_get_priv(bus);
114 struct soft_spi_platdata *plat = dev_get_platdata(bus);
Simon Glass623b6382014-10-13 23:42:00 -0600115 uchar tmpdin = 0;
116 uchar tmpdout = 0;
117 const u8 *txd = dout;
118 u8 *rxd = din;
Simon Glass623b6382014-10-13 23:42:00 -0600119 int cpha = priv->mode & SPI_CPHA;
120 unsigned int j;
121
122 debug("spi_xfer: slave %s:%s dout %08X din %08X bitlen %u\n",
123 dev->parent->name, dev->name, *(uint *)txd, *(uint *)rxd,
124 bitlen);
125
126 if (flags & SPI_XFER_BEGIN)
127 soft_spi_cs_activate(dev);
128
129 for (j = 0; j < bitlen; j++) {
130 /*
131 * Check if it is time to work on a new byte.
132 */
133 if ((j % 8) == 0) {
134 if (txd)
135 tmpdout = *txd++;
136 else
137 tmpdout = 0;
138 if (j != 0) {
139 if (rxd)
140 *rxd++ = tmpdin;
141 }
142 tmpdin = 0;
143 }
144
145 if (!cpha)
Simon Glass050fb902015-01-05 20:05:40 -0700146 soft_spi_scl(dev, 0);
Peng Fan102412c2016-05-03 10:02:21 +0800147 if ((plat->flags & SPI_MASTER_NO_TX) == 0)
148 soft_spi_sda(dev, !!(tmpdout & 0x80));
Simon Glass623b6382014-10-13 23:42:00 -0600149 udelay(plat->spi_delay_us);
150 if (cpha)
Simon Glass050fb902015-01-05 20:05:40 -0700151 soft_spi_scl(dev, 0);
Simon Glass623b6382014-10-13 23:42:00 -0600152 else
Simon Glass050fb902015-01-05 20:05:40 -0700153 soft_spi_scl(dev, 1);
Simon Glass623b6382014-10-13 23:42:00 -0600154 tmpdin <<= 1;
Peng Fan102412c2016-05-03 10:02:21 +0800155 if ((plat->flags & SPI_MASTER_NO_RX) == 0)
156 tmpdin |= dm_gpio_get_value(&plat->miso);
Simon Glass623b6382014-10-13 23:42:00 -0600157 tmpdout <<= 1;
158 udelay(plat->spi_delay_us);
159 if (cpha)
Simon Glass050fb902015-01-05 20:05:40 -0700160 soft_spi_scl(dev, 1);
Simon Glass623b6382014-10-13 23:42:00 -0600161 }
162 /*
163 * If the number of bits isn't a multiple of 8, shift the last
164 * bits over to left-justify them. Then store the last byte
165 * read in.
166 */
167 if (rxd) {
168 if ((bitlen % 8) != 0)
169 tmpdin <<= 8 - (bitlen % 8);
170 *rxd++ = tmpdin;
171 }
172
173 if (flags & SPI_XFER_END)
174 soft_spi_cs_deactivate(dev);
175
176 return 0;
177}
178
179static int soft_spi_set_speed(struct udevice *dev, unsigned int speed)
180{
181 /* Accept any speed */
182 return 0;
183}
184
185static int soft_spi_set_mode(struct udevice *dev, unsigned int mode)
186{
187 struct soft_spi_priv *priv = dev_get_priv(dev);
188
189 priv->mode = mode;
190
191 return 0;
192}
193
Simon Glass623b6382014-10-13 23:42:00 -0600194static const struct dm_spi_ops soft_spi_ops = {
195 .claim_bus = soft_spi_claim_bus,
196 .release_bus = soft_spi_release_bus,
197 .xfer = soft_spi_xfer,
198 .set_speed = soft_spi_set_speed,
199 .set_mode = soft_spi_set_mode,
200};
201
202static int soft_spi_ofdata_to_platdata(struct udevice *dev)
203{
204 struct soft_spi_platdata *plat = dev->platdata;
205 const void *blob = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -0700206 int node = dev_of_offset(dev);
Simon Glass623b6382014-10-13 23:42:00 -0600207
Simon Glass623b6382014-10-13 23:42:00 -0600208 plat->spi_delay_us = fdtdec_get_int(blob, node, "spi-delay-us", 0);
209
210 return 0;
211}
212
213static int soft_spi_probe(struct udevice *dev)
214{
Simon Glassbcbe3d12015-09-28 23:32:01 -0600215 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glass623b6382014-10-13 23:42:00 -0600216 struct soft_spi_platdata *plat = dev->platdata;
Simon Glass050fb902015-01-05 20:05:40 -0700217 int cs_flags, clk_flags;
Peng Fan102412c2016-05-03 10:02:21 +0800218 int ret;
Simon Glass623b6382014-10-13 23:42:00 -0600219
Christophe Kerellodfe72d02019-08-02 15:46:29 +0200220 cs_flags = (slave && slave->mode & SPI_CS_HIGH) ? 0 : GPIOD_ACTIVE_LOW;
221 clk_flags = (slave && slave->mode & SPI_CPOL) ? GPIOD_ACTIVE_LOW : 0;
Peng Fan102412c2016-05-03 10:02:21 +0800222
223 if (gpio_request_by_name(dev, "cs-gpios", 0, &plat->cs,
Simon Glass050fb902015-01-05 20:05:40 -0700224 GPIOD_IS_OUT | cs_flags) ||
Peng Fan102412c2016-05-03 10:02:21 +0800225 gpio_request_by_name(dev, "gpio-sck", 0, &plat->sclk,
226 GPIOD_IS_OUT | clk_flags))
227 return -EINVAL;
228
229 ret = gpio_request_by_name(dev, "gpio-mosi", 0, &plat->mosi,
230 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
231 if (ret)
232 plat->flags |= SPI_MASTER_NO_TX;
233
234 ret = gpio_request_by_name(dev, "gpio-miso", 0, &plat->miso,
235 GPIOD_IS_IN);
236 if (ret)
237 plat->flags |= SPI_MASTER_NO_RX;
238
239 if ((plat->flags & (SPI_MASTER_NO_RX | SPI_MASTER_NO_TX)) ==
240 (SPI_MASTER_NO_RX | SPI_MASTER_NO_TX))
Simon Glass050fb902015-01-05 20:05:40 -0700241 return -EINVAL;
Simon Glass623b6382014-10-13 23:42:00 -0600242
243 return 0;
244}
245
246static const struct udevice_id soft_spi_ids[] = {
Peng Fan102412c2016-05-03 10:02:21 +0800247 { .compatible = "spi-gpio" },
Simon Glass623b6382014-10-13 23:42:00 -0600248 { }
249};
250
251U_BOOT_DRIVER(soft_spi) = {
252 .name = "soft_spi",
253 .id = UCLASS_SPI,
254 .of_match = soft_spi_ids,
255 .ops = &soft_spi_ops,
256 .ofdata_to_platdata = soft_spi_ofdata_to_platdata,
257 .platdata_auto_alloc_size = sizeof(struct soft_spi_platdata),
258 .priv_auto_alloc_size = sizeof(struct soft_spi_priv),
Simon Glass623b6382014-10-13 23:42:00 -0600259 .probe = soft_spi_probe,
Simon Glass623b6382014-10-13 23:42:00 -0600260};