Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 2 | /* |
| 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 Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 10 | */ |
| 11 | |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 12 | #include <dm.h> |
| 13 | #include <errno.h> |
| 14 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 16 | #include <malloc.h> |
| 17 | #include <spi.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 19 | #include <asm/gpio.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 20 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 21 | #include <linux/delay.h> |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 22 | |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 25 | struct soft_spi_plat { |
Simon Glass | 61d10b7 | 2015-01-05 20:05:40 -0700 | [diff] [blame] | 26 | struct gpio_desc cs; |
| 27 | struct gpio_desc sclk; |
| 28 | struct gpio_desc mosi; |
| 29 | struct gpio_desc miso; |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 30 | int spi_delay_us; |
Peng Fan | 54108e7 | 2016-05-03 10:02:21 +0800 | [diff] [blame] | 31 | int flags; |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 32 | }; |
| 33 | |
Peng Fan | 54108e7 | 2016-05-03 10:02:21 +0800 | [diff] [blame] | 34 | #define SPI_MASTER_NO_RX BIT(0) |
| 35 | #define SPI_MASTER_NO_TX BIT(1) |
| 36 | |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 37 | struct soft_spi_priv { |
| 38 | unsigned int mode; |
| 39 | }; |
| 40 | |
| 41 | static int soft_spi_scl(struct udevice *dev, int bit) |
| 42 | { |
Peng Fan | fa7dd9b | 2016-05-03 10:02:20 +0800 | [diff] [blame] | 43 | struct udevice *bus = dev_get_parent(dev); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 44 | struct soft_spi_plat *plat = dev_get_plat(bus); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 45 | |
Simon Glass | 61d10b7 | 2015-01-05 20:05:40 -0700 | [diff] [blame] | 46 | dm_gpio_set_value(&plat->sclk, bit); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int soft_spi_sda(struct udevice *dev, int bit) |
| 52 | { |
Peng Fan | fa7dd9b | 2016-05-03 10:02:20 +0800 | [diff] [blame] | 53 | struct udevice *bus = dev_get_parent(dev); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 54 | struct soft_spi_plat *plat = dev_get_plat(bus); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 55 | |
Simon Glass | 61d10b7 | 2015-01-05 20:05:40 -0700 | [diff] [blame] | 56 | dm_gpio_set_value(&plat->mosi, bit); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static int soft_spi_cs_activate(struct udevice *dev) |
| 62 | { |
Peng Fan | fa7dd9b | 2016-05-03 10:02:20 +0800 | [diff] [blame] | 63 | struct udevice *bus = dev_get_parent(dev); |
Johannes Holland | 8412062 | 2020-05-11 15:22:26 +0200 | [diff] [blame] | 64 | struct soft_spi_priv *priv = dev_get_priv(bus); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 65 | struct soft_spi_plat *plat = dev_get_plat(bus); |
Johannes Holland | 8412062 | 2020-05-11 15:22:26 +0200 | [diff] [blame] | 66 | int cidle = !!(priv->mode & SPI_CPOL); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 67 | |
Simon Glass | 61d10b7 | 2015-01-05 20:05:40 -0700 | [diff] [blame] | 68 | dm_gpio_set_value(&plat->cs, 0); |
Johannes Holland | 8412062 | 2020-05-11 15:22:26 +0200 | [diff] [blame] | 69 | dm_gpio_set_value(&plat->sclk, cidle); /* to idle */ |
Simon Glass | 61d10b7 | 2015-01-05 20:05:40 -0700 | [diff] [blame] | 70 | dm_gpio_set_value(&plat->cs, 1); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int soft_spi_cs_deactivate(struct udevice *dev) |
| 76 | { |
Peng Fan | fa7dd9b | 2016-05-03 10:02:20 +0800 | [diff] [blame] | 77 | struct udevice *bus = dev_get_parent(dev); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 78 | struct soft_spi_plat *plat = dev_get_plat(bus); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 79 | |
Simon Glass | 61d10b7 | 2015-01-05 20:05:40 -0700 | [diff] [blame] | 80 | dm_gpio_set_value(&plat->cs, 0); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int soft_spi_claim_bus(struct udevice *dev) |
| 86 | { |
Johannes Holland | 8412062 | 2020-05-11 15:22:26 +0200 | [diff] [blame] | 87 | struct udevice *bus = dev_get_parent(dev); |
| 88 | struct soft_spi_priv *priv = dev_get_priv(bus); |
| 89 | int cidle = !!(priv->mode & SPI_CPOL); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 90 | /* |
| 91 | * Make sure the SPI clock is in idle state as defined for |
| 92 | * this slave. |
| 93 | */ |
Johannes Holland | 8412062 | 2020-05-11 15:22:26 +0200 | [diff] [blame] | 94 | return soft_spi_scl(dev, cidle); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static int soft_spi_release_bus(struct udevice *dev) |
| 98 | { |
| 99 | /* Nothing to do */ |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /*----------------------------------------------------------------------- |
| 104 | * SPI transfer |
| 105 | * |
| 106 | * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks |
| 107 | * "bitlen" bits in the SPI MISO port. That's just the way SPI works. |
| 108 | * |
| 109 | * The source of the outgoing bits is the "dout" parameter and the |
| 110 | * destination of the input bits is the "din" parameter. Note that "dout" |
| 111 | * and "din" can point to the same memory location, in which case the |
| 112 | * input data overwrites the output data (since both are buffered by |
| 113 | * temporary variables, this is OK). |
| 114 | */ |
| 115 | static int soft_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 116 | const void *dout, void *din, unsigned long flags) |
| 117 | { |
Peng Fan | fa7dd9b | 2016-05-03 10:02:20 +0800 | [diff] [blame] | 118 | struct udevice *bus = dev_get_parent(dev); |
| 119 | struct soft_spi_priv *priv = dev_get_priv(bus); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 120 | struct soft_spi_plat *plat = dev_get_plat(bus); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 121 | uchar tmpdin = 0; |
| 122 | uchar tmpdout = 0; |
| 123 | const u8 *txd = dout; |
| 124 | u8 *rxd = din; |
Johannes Holland | 8412062 | 2020-05-11 15:22:26 +0200 | [diff] [blame] | 125 | int cpha = !!(priv->mode & SPI_CPHA); |
| 126 | int cidle = !!(priv->mode & SPI_CPOL); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 127 | unsigned int j; |
| 128 | |
| 129 | debug("spi_xfer: slave %s:%s dout %08X din %08X bitlen %u\n", |
| 130 | dev->parent->name, dev->name, *(uint *)txd, *(uint *)rxd, |
| 131 | bitlen); |
| 132 | |
| 133 | if (flags & SPI_XFER_BEGIN) |
| 134 | soft_spi_cs_activate(dev); |
| 135 | |
| 136 | for (j = 0; j < bitlen; j++) { |
| 137 | /* |
| 138 | * Check if it is time to work on a new byte. |
| 139 | */ |
| 140 | if ((j % 8) == 0) { |
| 141 | if (txd) |
| 142 | tmpdout = *txd++; |
| 143 | else |
| 144 | tmpdout = 0; |
| 145 | if (j != 0) { |
| 146 | if (rxd) |
| 147 | *rxd++ = tmpdin; |
| 148 | } |
| 149 | tmpdin = 0; |
| 150 | } |
| 151 | |
Johannes Holland | 8412062 | 2020-05-11 15:22:26 +0200 | [diff] [blame] | 152 | /* |
| 153 | * CPOL 0: idle is low (0), active is high (1) |
| 154 | * CPOL 1: idle is high (1), active is low (0) |
| 155 | */ |
| 156 | |
| 157 | /* |
| 158 | * drive bit |
| 159 | * CPHA 1: CLK from idle to active |
| 160 | */ |
| 161 | if (cpha) |
| 162 | soft_spi_scl(dev, !cidle); |
Peng Fan | 54108e7 | 2016-05-03 10:02:21 +0800 | [diff] [blame] | 163 | if ((plat->flags & SPI_MASTER_NO_TX) == 0) |
| 164 | soft_spi_sda(dev, !!(tmpdout & 0x80)); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 165 | udelay(plat->spi_delay_us); |
Johannes Holland | 8412062 | 2020-05-11 15:22:26 +0200 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * sample bit |
| 169 | * CPHA 0: CLK from idle to active |
| 170 | * CPHA 1: CLK from active to idle |
| 171 | */ |
| 172 | if (!cpha) |
| 173 | soft_spi_scl(dev, !cidle); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 174 | else |
Johannes Holland | 8412062 | 2020-05-11 15:22:26 +0200 | [diff] [blame] | 175 | soft_spi_scl(dev, cidle); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 176 | tmpdin <<= 1; |
Peng Fan | 54108e7 | 2016-05-03 10:02:21 +0800 | [diff] [blame] | 177 | if ((plat->flags & SPI_MASTER_NO_RX) == 0) |
| 178 | tmpdin |= dm_gpio_get_value(&plat->miso); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 179 | tmpdout <<= 1; |
| 180 | udelay(plat->spi_delay_us); |
Johannes Holland | 8412062 | 2020-05-11 15:22:26 +0200 | [diff] [blame] | 181 | |
| 182 | /* |
| 183 | * drive bit |
| 184 | * CPHA 0: CLK from active to idle |
| 185 | */ |
| 186 | if (!cpha) |
| 187 | soft_spi_scl(dev, cidle); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 188 | } |
| 189 | /* |
| 190 | * If the number of bits isn't a multiple of 8, shift the last |
| 191 | * bits over to left-justify them. Then store the last byte |
| 192 | * read in. |
| 193 | */ |
| 194 | if (rxd) { |
| 195 | if ((bitlen % 8) != 0) |
| 196 | tmpdin <<= 8 - (bitlen % 8); |
| 197 | *rxd++ = tmpdin; |
| 198 | } |
| 199 | |
| 200 | if (flags & SPI_XFER_END) |
| 201 | soft_spi_cs_deactivate(dev); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static int soft_spi_set_speed(struct udevice *dev, unsigned int speed) |
| 207 | { |
Johannes Holland | 8412062 | 2020-05-11 15:22:26 +0200 | [diff] [blame] | 208 | /* Ignore any speed settings. Speed is implemented via "spi-delay-us" */ |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int soft_spi_set_mode(struct udevice *dev, unsigned int mode) |
| 213 | { |
| 214 | struct soft_spi_priv *priv = dev_get_priv(dev); |
| 215 | |
| 216 | priv->mode = mode; |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 221 | static const struct dm_spi_ops soft_spi_ops = { |
| 222 | .claim_bus = soft_spi_claim_bus, |
| 223 | .release_bus = soft_spi_release_bus, |
| 224 | .xfer = soft_spi_xfer, |
| 225 | .set_speed = soft_spi_set_speed, |
| 226 | .set_mode = soft_spi_set_mode, |
| 227 | }; |
| 228 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 229 | static int soft_spi_of_to_plat(struct udevice *dev) |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 230 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 231 | struct soft_spi_plat *plat = dev_get_plat(dev); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 232 | const void *blob = gd->fdt_blob; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 233 | int node = dev_of_offset(dev); |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 234 | |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 235 | plat->spi_delay_us = fdtdec_get_int(blob, node, "spi-delay-us", 0); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
Michael Polyntsov | 16cc79c | 2024-07-31 08:11:29 +0400 | [diff] [blame] | 240 | static int retrieve_num_chipselects(struct udevice *dev) |
| 241 | { |
| 242 | int chipselects; |
| 243 | int ret; |
| 244 | |
| 245 | ret = ofnode_read_u32(dev_ofnode(dev), "num-chipselects", &chipselects); |
| 246 | if (ret) |
| 247 | return ret; |
| 248 | |
| 249 | return chipselects; |
| 250 | } |
| 251 | |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 252 | static int soft_spi_probe(struct udevice *dev) |
| 253 | { |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 254 | struct spi_slave *slave = dev_get_parent_priv(dev); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 255 | struct soft_spi_plat *plat = dev_get_plat(dev); |
Simon Glass | 61d10b7 | 2015-01-05 20:05:40 -0700 | [diff] [blame] | 256 | int cs_flags, clk_flags; |
Peng Fan | 54108e7 | 2016-05-03 10:02:21 +0800 | [diff] [blame] | 257 | int ret; |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 258 | |
Christophe Kerello | b34a10a | 2019-08-02 15:46:29 +0200 | [diff] [blame] | 259 | cs_flags = (slave && slave->mode & SPI_CS_HIGH) ? 0 : GPIOD_ACTIVE_LOW; |
| 260 | clk_flags = (slave && slave->mode & SPI_CPOL) ? GPIOD_ACTIVE_LOW : 0; |
Peng Fan | 54108e7 | 2016-05-03 10:02:21 +0800 | [diff] [blame] | 261 | |
Fabio Estevam | 51a7d41 | 2023-05-18 19:22:40 -0300 | [diff] [blame] | 262 | ret = gpio_request_by_name(dev, "cs-gpios", 0, &plat->cs, |
| 263 | GPIOD_IS_OUT | cs_flags); |
Michael Polyntsov | 16cc79c | 2024-07-31 08:11:29 +0400 | [diff] [blame] | 264 | /* |
| 265 | * If num-chipselects is zero we're ignoring absence of cs-gpios. This |
| 266 | * code relies on the fact that `gpio_request_by_name` call above |
| 267 | * initiailizes plat->cs to correct value with invalid GPIO even when |
| 268 | * there is no cs-gpios node in dts. All other functions which work |
| 269 | * with plat->cs verify it via `dm_gpio_is_valid` before using it, so |
| 270 | * such value doesn't cause any problems. |
| 271 | */ |
| 272 | if (ret && retrieve_num_chipselects(dev) != 0) |
Peng Fan | 54108e7 | 2016-05-03 10:02:21 +0800 | [diff] [blame] | 273 | return -EINVAL; |
| 274 | |
Fabio Estevam | 51a7d41 | 2023-05-18 19:22:40 -0300 | [diff] [blame] | 275 | ret = gpio_request_by_name(dev, "gpio-sck", 0, &plat->sclk, |
| 276 | GPIOD_IS_OUT | clk_flags); |
| 277 | if (ret) |
| 278 | ret = gpio_request_by_name(dev, "sck-gpios", 0, &plat->sclk, |
| 279 | GPIOD_IS_OUT | clk_flags); |
| 280 | if (ret) |
| 281 | return -EINVAL; |
| 282 | |
Peng Fan | 54108e7 | 2016-05-03 10:02:21 +0800 | [diff] [blame] | 283 | ret = gpio_request_by_name(dev, "gpio-mosi", 0, &plat->mosi, |
| 284 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE); |
| 285 | if (ret) |
Fabio Estevam | 51a7d41 | 2023-05-18 19:22:40 -0300 | [diff] [blame] | 286 | ret = gpio_request_by_name(dev, "mosi-gpios", 0, &plat->mosi, |
| 287 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE); |
| 288 | if (ret) |
Peng Fan | 54108e7 | 2016-05-03 10:02:21 +0800 | [diff] [blame] | 289 | plat->flags |= SPI_MASTER_NO_TX; |
| 290 | |
| 291 | ret = gpio_request_by_name(dev, "gpio-miso", 0, &plat->miso, |
| 292 | GPIOD_IS_IN); |
| 293 | if (ret) |
Mikhail Kshevetskiy | 7539fe1 | 2024-07-31 08:11:28 +0400 | [diff] [blame] | 294 | ret = gpio_request_by_name(dev, "miso-gpios", 0, &plat->miso, |
Fabio Estevam | 51a7d41 | 2023-05-18 19:22:40 -0300 | [diff] [blame] | 295 | GPIOD_IS_IN); |
| 296 | if (ret) |
Peng Fan | 54108e7 | 2016-05-03 10:02:21 +0800 | [diff] [blame] | 297 | plat->flags |= SPI_MASTER_NO_RX; |
| 298 | |
| 299 | if ((plat->flags & (SPI_MASTER_NO_RX | SPI_MASTER_NO_TX)) == |
| 300 | (SPI_MASTER_NO_RX | SPI_MASTER_NO_TX)) |
Simon Glass | 61d10b7 | 2015-01-05 20:05:40 -0700 | [diff] [blame] | 301 | return -EINVAL; |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static const struct udevice_id soft_spi_ids[] = { |
Peng Fan | 54108e7 | 2016-05-03 10:02:21 +0800 | [diff] [blame] | 307 | { .compatible = "spi-gpio" }, |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 308 | { } |
| 309 | }; |
| 310 | |
| 311 | U_BOOT_DRIVER(soft_spi) = { |
| 312 | .name = "soft_spi", |
| 313 | .id = UCLASS_SPI, |
| 314 | .of_match = soft_spi_ids, |
| 315 | .ops = &soft_spi_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 316 | .of_to_plat = soft_spi_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 317 | .plat_auto = sizeof(struct soft_spi_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 318 | .priv_auto = sizeof(struct soft_spi_priv), |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 319 | .probe = soft_spi_probe, |
Simon Glass | 456dd7c | 2014-10-13 23:42:00 -0600 | [diff] [blame] | 320 | }; |