Mike Frysinger | 1d8e57c | 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 | 3b4b340 | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 11 | #define LOG_CATEGORY UCLASS_SPI |
| 12 | |
Mike Frysinger | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 13 | #include <common.h> |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 14 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Mike Frysinger | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 16 | #include <malloc.h> |
| 17 | #include <spi.h> |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 18 | #include <spi_flash.h> |
Mike Frysinger | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 19 | #include <os.h> |
| 20 | |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 21 | #include <linux/errno.h> |
Mike Frysinger | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 22 | #include <asm/spi.h> |
| 23 | #include <asm/state.h> |
Simon Glass | 711fd98 | 2020-07-07 13:11:49 -0600 | [diff] [blame] | 24 | #include <dm/acpi.h> |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 25 | #include <dm/device-internal.h> |
| 26 | |
Mike Frysinger | 1d8e57c | 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 | 05f3881 | 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 | 95429fe | 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 | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 51 | { |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 52 | return -ENOENT; |
Mike Frysinger | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 55 | static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen, |
| 56 | const void *dout, void *din, unsigned long flags) |
Mike Frysinger | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 57 | { |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 58 | struct udevice *bus = slave->parent; |
Mike Frysinger | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 59 | struct sandbox_state *state = state_get_current(); |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 60 | struct dm_spi_emul_ops *ops; |
| 61 | struct udevice *emul; |
Mike Frysinger | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 62 | uint bytes = bitlen / 8, i; |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 63 | int ret; |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 64 | uint busnum, cs; |
Mike Frysinger | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 65 | |
| 66 | if (bitlen == 0) |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 67 | return 0; |
Mike Frysinger | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 68 | |
| 69 | /* we can only do 8 bit transfers */ |
| 70 | if (bitlen % 8) { |
| 71 | printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n", |
| 72 | bitlen); |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 73 | return -EINVAL; |
Mike Frysinger | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 76 | busnum = dev_seq(bus); |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 77 | cs = spi_chip_select(slave); |
| 78 | if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS || |
| 79 | cs >= CONFIG_SANDBOX_SPI_MAX_CS) { |
| 80 | printf("%s: busnum=%u, cs=%u: out of range\n", __func__, |
| 81 | busnum, cs); |
| 82 | return -ENOENT; |
| 83 | } |
| 84 | ret = sandbox_spi_get_emul(state, bus, slave, &emul); |
| 85 | if (ret) { |
| 86 | printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n", |
| 87 | __func__, busnum, cs, ret); |
| 88 | return -ENOENT; |
| 89 | } |
| 90 | ret = device_probe(emul); |
| 91 | if (ret) |
| 92 | return ret; |
Mike Frysinger | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 93 | |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 94 | ops = spi_emul_get_ops(emul); |
| 95 | ret = ops->xfer(emul, bitlen, dout, din, flags); |
Mike Frysinger | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 96 | |
Simon Glass | 3b4b340 | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 97 | log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:", |
| 98 | ret, ret ? "bad" : "good"); |
| 99 | if (din) { |
| 100 | for (i = 0; i < bytes; ++i) |
| 101 | log_content(" %u:%02x", i, ((u8 *)din)[i]); |
| 102 | } |
| 103 | log_content("\n"); |
Mike Frysinger | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 104 | |
Mike Frysinger | 1d8e57c | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 105 | return ret; |
| 106 | } |
Simon Glass | fe27ded | 2014-02-27 13:26:24 -0700 | [diff] [blame] | 107 | |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 108 | static int sandbox_spi_set_speed(struct udevice *bus, uint speed) |
Simon Glass | fe27ded | 2014-02-27 13:26:24 -0700 | [diff] [blame] | 109 | { |
Ovidiu Panait | 05f3881 | 2020-12-14 19:06:48 +0200 | [diff] [blame^] | 110 | struct sandbox_spi_priv *priv = dev_get_priv(bus); |
| 111 | |
| 112 | priv->speed = speed; |
| 113 | |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 114 | return 0; |
Simon Glass | fe27ded | 2014-02-27 13:26:24 -0700 | [diff] [blame] | 115 | } |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 116 | |
| 117 | static int sandbox_spi_set_mode(struct udevice *bus, uint mode) |
| 118 | { |
Ovidiu Panait | 05f3881 | 2020-12-14 19:06:48 +0200 | [diff] [blame^] | 119 | struct sandbox_spi_priv *priv = dev_get_priv(bus); |
| 120 | |
| 121 | priv->mode = mode; |
| 122 | |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int sandbox_cs_info(struct udevice *bus, uint cs, |
| 127 | struct spi_cs_info *info) |
| 128 | { |
Ovidiu Panait | ae73473 | 2020-12-14 19:06:47 +0200 | [diff] [blame] | 129 | /* Always allow activity on CS 0, CS 1 */ |
| 130 | if (cs >= 2) |
Bin Meng | f8586f6 | 2019-09-09 06:00:01 -0700 | [diff] [blame] | 131 | return -EINVAL; |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Simon Glass | 37ad0fe | 2019-10-20 21:31:47 -0600 | [diff] [blame] | 136 | static int sandbox_spi_get_mmap(struct udevice *dev, ulong *map_basep, |
| 137 | uint *map_sizep, uint *offsetp) |
| 138 | { |
| 139 | *map_basep = 0x1000; |
| 140 | *map_sizep = 0x2000; |
| 141 | *offsetp = 0x100; |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 146 | static const struct dm_spi_ops sandbox_spi_ops = { |
| 147 | .xfer = sandbox_spi_xfer, |
| 148 | .set_speed = sandbox_spi_set_speed, |
| 149 | .set_mode = sandbox_spi_set_mode, |
| 150 | .cs_info = sandbox_cs_info, |
Simon Glass | 37ad0fe | 2019-10-20 21:31:47 -0600 | [diff] [blame] | 151 | .get_mmap = sandbox_spi_get_mmap, |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | static const struct udevice_id sandbox_spi_ids[] = { |
| 155 | { .compatible = "sandbox,spi" }, |
| 156 | { } |
| 157 | }; |
| 158 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 159 | U_BOOT_DRIVER(sandbox_spi) = { |
| 160 | .name = "sandbox_spi", |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 161 | .id = UCLASS_SPI, |
| 162 | .of_match = sandbox_spi_ids, |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 163 | .ops = &sandbox_spi_ops, |
Ovidiu Panait | 05f3881 | 2020-12-14 19:06:48 +0200 | [diff] [blame^] | 164 | .priv_auto = sizeof(struct sandbox_spi_priv), |
Simon Glass | 95429fe | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 165 | }; |