Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Simulate a SPI port |
| 3 | * |
| 4 | * Copyright (c) 2011-2013 The Chromium OS Authors. |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * Licensed under the GPL-2 or later. |
| 9 | */ |
| 10 | |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 11 | #define LOG_CATEGORY UCLASS_SPI |
| 12 | |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 13 | #include <common.h> |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 14 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 16 | #include <malloc.h> |
| 17 | #include <spi.h> |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 18 | #include <spi_flash.h> |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 19 | #include <os.h> |
| 20 | |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 21 | #include <linux/errno.h> |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 22 | #include <asm/spi.h> |
| 23 | #include <asm/state.h> |
Simon Glass | 70e5e67 | 2020-07-07 13:11:49 -0600 | [diff] [blame] | 24 | #include <dm/acpi.h> |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 25 | #include <dm/device-internal.h> |
| 26 | |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 27 | #ifndef CONFIG_SPI_IDLE_VAL |
| 28 | # define CONFIG_SPI_IDLE_VAL 0xFF |
| 29 | #endif |
| 30 | |
Ovidiu Panait | 2da1800 | 2020-12-14 19:06:48 +0200 | [diff] [blame] | 31 | /** |
| 32 | * struct sandbox_spi_priv - Sandbox SPI private data |
| 33 | * |
| 34 | * Helper struct to keep track of the sandbox SPI bus internal state. It is |
| 35 | * used in unit tests to verify that dm spi functions update the bus |
| 36 | * speed/mode properly (for instance, when jumping back and forth between spi |
| 37 | * slaves claiming the bus, we need to make sure that the bus speed is updated |
| 38 | * accordingly for each slave). |
| 39 | * |
| 40 | * @speed: Current bus speed. |
| 41 | * @mode: Current bus mode. |
| 42 | */ |
| 43 | struct sandbox_spi_priv { |
| 44 | uint speed; |
| 45 | uint mode; |
| 46 | }; |
| 47 | |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 48 | __weak int sandbox_spi_get_emul(struct sandbox_state *state, |
| 49 | struct udevice *bus, struct udevice *slave, |
| 50 | struct udevice **emulp) |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 51 | { |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 52 | return -ENOENT; |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Ovidiu Panait | add685f | 2020-12-14 19:06:49 +0200 | [diff] [blame] | 55 | uint sandbox_spi_get_speed(struct udevice *dev) |
| 56 | { |
| 57 | struct sandbox_spi_priv *priv = dev_get_priv(dev); |
| 58 | |
| 59 | return priv->speed; |
| 60 | } |
| 61 | |
| 62 | uint sandbox_spi_get_mode(struct udevice *dev) |
| 63 | { |
| 64 | struct sandbox_spi_priv *priv = dev_get_priv(dev); |
| 65 | |
| 66 | return priv->mode; |
| 67 | } |
| 68 | |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 69 | static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen, |
| 70 | const void *dout, void *din, unsigned long flags) |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 71 | { |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 72 | struct udevice *bus = slave->parent; |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 73 | struct sandbox_state *state = state_get_current(); |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 74 | struct dm_spi_emul_ops *ops; |
| 75 | struct udevice *emul; |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 76 | uint bytes = bitlen / 8, i; |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 77 | int ret; |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 78 | uint busnum, cs; |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 79 | |
| 80 | if (bitlen == 0) |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 81 | return 0; |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 82 | |
| 83 | /* we can only do 8 bit transfers */ |
| 84 | if (bitlen % 8) { |
| 85 | printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n", |
| 86 | bitlen); |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 87 | return -EINVAL; |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 90 | busnum = dev_seq(bus); |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 91 | cs = spi_chip_select(slave); |
| 92 | if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS || |
| 93 | cs >= CONFIG_SANDBOX_SPI_MAX_CS) { |
| 94 | printf("%s: busnum=%u, cs=%u: out of range\n", __func__, |
| 95 | busnum, cs); |
| 96 | return -ENOENT; |
| 97 | } |
| 98 | ret = sandbox_spi_get_emul(state, bus, slave, &emul); |
| 99 | if (ret) { |
| 100 | printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n", |
| 101 | __func__, busnum, cs, ret); |
| 102 | return -ENOENT; |
| 103 | } |
| 104 | ret = device_probe(emul); |
| 105 | if (ret) |
| 106 | return ret; |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 107 | |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 108 | ops = spi_emul_get_ops(emul); |
| 109 | ret = ops->xfer(emul, bitlen, dout, din, flags); |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 110 | |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 111 | log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:", |
| 112 | ret, ret ? "bad" : "good"); |
| 113 | if (din) { |
| 114 | for (i = 0; i < bytes; ++i) |
| 115 | log_content(" %u:%02x", i, ((u8 *)din)[i]); |
| 116 | } |
| 117 | log_content("\n"); |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 118 | |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 119 | return ret; |
| 120 | } |
Simon Glass | 6e16d90 | 2014-02-27 13:26:24 -0700 | [diff] [blame] | 121 | |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 122 | static int sandbox_spi_set_speed(struct udevice *bus, uint speed) |
Simon Glass | 6e16d90 | 2014-02-27 13:26:24 -0700 | [diff] [blame] | 123 | { |
Ovidiu Panait | 2da1800 | 2020-12-14 19:06:48 +0200 | [diff] [blame] | 124 | struct sandbox_spi_priv *priv = dev_get_priv(bus); |
| 125 | |
| 126 | priv->speed = speed; |
| 127 | |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 128 | return 0; |
Simon Glass | 6e16d90 | 2014-02-27 13:26:24 -0700 | [diff] [blame] | 129 | } |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 130 | |
| 131 | static int sandbox_spi_set_mode(struct udevice *bus, uint mode) |
| 132 | { |
Ovidiu Panait | 2da1800 | 2020-12-14 19:06:48 +0200 | [diff] [blame] | 133 | struct sandbox_spi_priv *priv = dev_get_priv(bus); |
| 134 | |
| 135 | priv->mode = mode; |
| 136 | |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int sandbox_cs_info(struct udevice *bus, uint cs, |
| 141 | struct spi_cs_info *info) |
| 142 | { |
Ovidiu Panait | 1dc53ce | 2020-12-14 19:06:47 +0200 | [diff] [blame] | 143 | /* Always allow activity on CS 0, CS 1 */ |
| 144 | if (cs >= 2) |
Bin Meng | 4b06000 | 2019-09-09 06:00:01 -0700 | [diff] [blame] | 145 | return -EINVAL; |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
Simon Glass | c53b318 | 2019-10-20 21:31:47 -0600 | [diff] [blame] | 150 | static int sandbox_spi_get_mmap(struct udevice *dev, ulong *map_basep, |
| 151 | uint *map_sizep, uint *offsetp) |
| 152 | { |
| 153 | *map_basep = 0x1000; |
| 154 | *map_sizep = 0x2000; |
| 155 | *offsetp = 0x100; |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 160 | static const struct dm_spi_ops sandbox_spi_ops = { |
| 161 | .xfer = sandbox_spi_xfer, |
| 162 | .set_speed = sandbox_spi_set_speed, |
| 163 | .set_mode = sandbox_spi_set_mode, |
| 164 | .cs_info = sandbox_cs_info, |
Simon Glass | c53b318 | 2019-10-20 21:31:47 -0600 | [diff] [blame] | 165 | .get_mmap = sandbox_spi_get_mmap, |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | static const struct udevice_id sandbox_spi_ids[] = { |
| 169 | { .compatible = "sandbox,spi" }, |
| 170 | { } |
| 171 | }; |
| 172 | |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 173 | U_BOOT_DRIVER(sandbox_spi) = { |
| 174 | .name = "sandbox_spi", |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 175 | .id = UCLASS_SPI, |
| 176 | .of_match = sandbox_spi_ids, |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 177 | .ops = &sandbox_spi_ops, |
Ovidiu Panait | 2da1800 | 2020-12-14 19:06:48 +0200 | [diff] [blame] | 178 | .priv_auto = sizeof(struct sandbox_spi_priv), |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 179 | }; |