blob: e8d7758da03b998e8bce5e8a50ee597302112418 [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 Glasscd93d622020-05-10 11:40:13 -060020#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060021#include <linux/delay.h>
Simon Glass623b6382014-10-13 23:42:00 -060022
23DECLARE_GLOBAL_DATA_PTR;
24
25struct soft_spi_platdata {
Simon Glass050fb902015-01-05 20:05:40 -070026 struct gpio_desc cs;
27 struct gpio_desc sclk;
28 struct gpio_desc mosi;
29 struct gpio_desc miso;
Simon Glass623b6382014-10-13 23:42:00 -060030 int spi_delay_us;
Peng Fan102412c2016-05-03 10:02:21 +080031 int flags;
Simon Glass623b6382014-10-13 23:42:00 -060032};
33
Peng Fan102412c2016-05-03 10:02:21 +080034#define SPI_MASTER_NO_RX BIT(0)
35#define SPI_MASTER_NO_TX BIT(1)
36
Simon Glass623b6382014-10-13 23:42:00 -060037struct soft_spi_priv {
38 unsigned int mode;
39};
40
41static int soft_spi_scl(struct udevice *dev, int bit)
42{
Peng Fanb6d54d52016-05-03 10:02:20 +080043 struct udevice *bus = dev_get_parent(dev);
44 struct soft_spi_platdata *plat = dev_get_platdata(bus);
Simon Glass623b6382014-10-13 23:42:00 -060045
Simon Glass050fb902015-01-05 20:05:40 -070046 dm_gpio_set_value(&plat->sclk, bit);
Simon Glass623b6382014-10-13 23:42:00 -060047
48 return 0;
49}
50
51static int soft_spi_sda(struct udevice *dev, int bit)
52{
Peng Fanb6d54d52016-05-03 10:02:20 +080053 struct udevice *bus = dev_get_parent(dev);
54 struct soft_spi_platdata *plat = dev_get_platdata(bus);
Simon Glass623b6382014-10-13 23:42:00 -060055
Simon Glass050fb902015-01-05 20:05:40 -070056 dm_gpio_set_value(&plat->mosi, bit);
Simon Glass623b6382014-10-13 23:42:00 -060057
58 return 0;
59}
60
61static int soft_spi_cs_activate(struct udevice *dev)
62{
Peng Fanb6d54d52016-05-03 10:02:20 +080063 struct udevice *bus = dev_get_parent(dev);
64 struct soft_spi_platdata *plat = dev_get_platdata(bus);
Simon Glass623b6382014-10-13 23:42:00 -060065
Simon Glass050fb902015-01-05 20:05:40 -070066 dm_gpio_set_value(&plat->cs, 0);
67 dm_gpio_set_value(&plat->sclk, 0);
68 dm_gpio_set_value(&plat->cs, 1);
Simon Glass623b6382014-10-13 23:42:00 -060069
70 return 0;
71}
72
73static int soft_spi_cs_deactivate(struct udevice *dev)
74{
Peng Fanb6d54d52016-05-03 10:02:20 +080075 struct udevice *bus = dev_get_parent(dev);
76 struct soft_spi_platdata *plat = dev_get_platdata(bus);
Simon Glass623b6382014-10-13 23:42:00 -060077
Simon Glass050fb902015-01-05 20:05:40 -070078 dm_gpio_set_value(&plat->cs, 0);
Simon Glass623b6382014-10-13 23:42:00 -060079
80 return 0;
81}
82
83static int soft_spi_claim_bus(struct udevice *dev)
84{
85 /*
86 * Make sure the SPI clock is in idle state as defined for
87 * this slave.
88 */
89 return soft_spi_scl(dev, 0);
90}
91
92static int soft_spi_release_bus(struct udevice *dev)
93{
94 /* Nothing to do */
95 return 0;
96}
97
98/*-----------------------------------------------------------------------
99 * SPI transfer
100 *
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.
103 *
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).
109 */
110static int soft_spi_xfer(struct udevice *dev, unsigned int bitlen,
111 const void *dout, void *din, unsigned long flags)
112{
Peng Fanb6d54d52016-05-03 10:02:20 +0800113 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);
Simon Glass623b6382014-10-13 23:42:00 -0600116 uchar tmpdin = 0;
117 uchar tmpdout = 0;
118 const u8 *txd = dout;
119 u8 *rxd = din;
Simon Glass623b6382014-10-13 23:42:00 -0600120 int cpha = priv->mode & SPI_CPHA;
121 unsigned int j;
122
123 debug("spi_xfer: slave %s:%s dout %08X din %08X bitlen %u\n",
124 dev->parent->name, dev->name, *(uint *)txd, *(uint *)rxd,
125 bitlen);
126
127 if (flags & SPI_XFER_BEGIN)
128 soft_spi_cs_activate(dev);
129
130 for (j = 0; j < bitlen; j++) {
131 /*
132 * Check if it is time to work on a new byte.
133 */
134 if ((j % 8) == 0) {
135 if (txd)
136 tmpdout = *txd++;
137 else
138 tmpdout = 0;
139 if (j != 0) {
140 if (rxd)
141 *rxd++ = tmpdin;
142 }
143 tmpdin = 0;
144 }
145
146 if (!cpha)
Simon Glass050fb902015-01-05 20:05:40 -0700147 soft_spi_scl(dev, 0);
Peng Fan102412c2016-05-03 10:02:21 +0800148 if ((plat->flags & SPI_MASTER_NO_TX) == 0)
149 soft_spi_sda(dev, !!(tmpdout & 0x80));
Simon Glass623b6382014-10-13 23:42:00 -0600150 udelay(plat->spi_delay_us);
151 if (cpha)
Simon Glass050fb902015-01-05 20:05:40 -0700152 soft_spi_scl(dev, 0);
Simon Glass623b6382014-10-13 23:42:00 -0600153 else
Simon Glass050fb902015-01-05 20:05:40 -0700154 soft_spi_scl(dev, 1);
Simon Glass623b6382014-10-13 23:42:00 -0600155 tmpdin <<= 1;
Peng Fan102412c2016-05-03 10:02:21 +0800156 if ((plat->flags & SPI_MASTER_NO_RX) == 0)
157 tmpdin |= dm_gpio_get_value(&plat->miso);
Simon Glass623b6382014-10-13 23:42:00 -0600158 tmpdout <<= 1;
159 udelay(plat->spi_delay_us);
160 if (cpha)
Simon Glass050fb902015-01-05 20:05:40 -0700161 soft_spi_scl(dev, 1);
Simon Glass623b6382014-10-13 23:42:00 -0600162 }
163 /*
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
166 * read in.
167 */
168 if (rxd) {
169 if ((bitlen % 8) != 0)
170 tmpdin <<= 8 - (bitlen % 8);
171 *rxd++ = tmpdin;
172 }
173
174 if (flags & SPI_XFER_END)
175 soft_spi_cs_deactivate(dev);
176
177 return 0;
178}
179
180static int soft_spi_set_speed(struct udevice *dev, unsigned int speed)
181{
182 /* Accept any speed */
183 return 0;
184}
185
186static int soft_spi_set_mode(struct udevice *dev, unsigned int mode)
187{
188 struct soft_spi_priv *priv = dev_get_priv(dev);
189
190 priv->mode = mode;
191
192 return 0;
193}
194
Simon Glass623b6382014-10-13 23:42:00 -0600195static 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,
201};
202
203static int soft_spi_ofdata_to_platdata(struct udevice *dev)
204{
205 struct soft_spi_platdata *plat = dev->platdata;
206 const void *blob = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -0700207 int node = dev_of_offset(dev);
Simon Glass623b6382014-10-13 23:42:00 -0600208
Simon Glass623b6382014-10-13 23:42:00 -0600209 plat->spi_delay_us = fdtdec_get_int(blob, node, "spi-delay-us", 0);
210
211 return 0;
212}
213
214static int soft_spi_probe(struct udevice *dev)
215{
Simon Glassbcbe3d12015-09-28 23:32:01 -0600216 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glass623b6382014-10-13 23:42:00 -0600217 struct soft_spi_platdata *plat = dev->platdata;
Simon Glass050fb902015-01-05 20:05:40 -0700218 int cs_flags, clk_flags;
Peng Fan102412c2016-05-03 10:02:21 +0800219 int ret;
Simon Glass623b6382014-10-13 23:42:00 -0600220
Christophe Kerellodfe72d02019-08-02 15:46:29 +0200221 cs_flags = (slave && slave->mode & SPI_CS_HIGH) ? 0 : GPIOD_ACTIVE_LOW;
222 clk_flags = (slave && slave->mode & SPI_CPOL) ? GPIOD_ACTIVE_LOW : 0;
Peng Fan102412c2016-05-03 10:02:21 +0800223
224 if (gpio_request_by_name(dev, "cs-gpios", 0, &plat->cs,
Simon Glass050fb902015-01-05 20:05:40 -0700225 GPIOD_IS_OUT | cs_flags) ||
Peng Fan102412c2016-05-03 10:02:21 +0800226 gpio_request_by_name(dev, "gpio-sck", 0, &plat->sclk,
227 GPIOD_IS_OUT | clk_flags))
228 return -EINVAL;
229
230 ret = gpio_request_by_name(dev, "gpio-mosi", 0, &plat->mosi,
231 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
232 if (ret)
233 plat->flags |= SPI_MASTER_NO_TX;
234
235 ret = gpio_request_by_name(dev, "gpio-miso", 0, &plat->miso,
236 GPIOD_IS_IN);
237 if (ret)
238 plat->flags |= SPI_MASTER_NO_RX;
239
240 if ((plat->flags & (SPI_MASTER_NO_RX | SPI_MASTER_NO_TX)) ==
241 (SPI_MASTER_NO_RX | SPI_MASTER_NO_TX))
Simon Glass050fb902015-01-05 20:05:40 -0700242 return -EINVAL;
Simon Glass623b6382014-10-13 23:42:00 -0600243
244 return 0;
245}
246
247static const struct udevice_id soft_spi_ids[] = {
Peng Fan102412c2016-05-03 10:02:21 +0800248 { .compatible = "spi-gpio" },
Simon Glass623b6382014-10-13 23:42:00 -0600249 { }
250};
251
252U_BOOT_DRIVER(soft_spi) = {
253 .name = "soft_spi",
254 .id = UCLASS_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),
Simon Glass623b6382014-10-13 23:42:00 -0600260 .probe = soft_spi_probe,
Simon Glass623b6382014-10-13 23:42:00 -0600261};