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 | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 24 | #include <dm/device-internal.h> |
| 25 | |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 26 | #ifndef CONFIG_SPI_IDLE_VAL |
| 27 | # define CONFIG_SPI_IDLE_VAL 0xFF |
| 28 | #endif |
| 29 | |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 30 | const char *sandbox_spi_parse_spec(const char *arg, unsigned long *bus, |
| 31 | unsigned long *cs) |
| 32 | { |
| 33 | char *endp; |
| 34 | |
| 35 | *bus = simple_strtoul(arg, &endp, 0); |
| 36 | if (*endp != ':' || *bus >= CONFIG_SANDBOX_SPI_MAX_BUS) |
| 37 | return NULL; |
| 38 | |
| 39 | *cs = simple_strtoul(endp + 1, &endp, 0); |
| 40 | if (*endp != ':' || *cs >= CONFIG_SANDBOX_SPI_MAX_CS) |
| 41 | return NULL; |
| 42 | |
| 43 | return endp + 1; |
| 44 | } |
| 45 | |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 46 | __weak int sandbox_spi_get_emul(struct sandbox_state *state, |
| 47 | struct udevice *bus, struct udevice *slave, |
| 48 | struct udevice **emulp) |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 49 | { |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 50 | return -ENOENT; |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 53 | static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen, |
| 54 | const void *dout, void *din, unsigned long flags) |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 55 | { |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 56 | struct udevice *bus = slave->parent; |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 57 | struct sandbox_state *state = state_get_current(); |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 58 | struct dm_spi_emul_ops *ops; |
| 59 | struct udevice *emul; |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 60 | uint bytes = bitlen / 8, i; |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 61 | int ret; |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 62 | uint busnum, cs; |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 63 | |
| 64 | if (bitlen == 0) |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 65 | return 0; |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 66 | |
| 67 | /* we can only do 8 bit transfers */ |
| 68 | if (bitlen % 8) { |
| 69 | printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n", |
| 70 | bitlen); |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 71 | return -EINVAL; |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 74 | busnum = bus->seq; |
| 75 | cs = spi_chip_select(slave); |
| 76 | if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS || |
| 77 | cs >= CONFIG_SANDBOX_SPI_MAX_CS) { |
| 78 | printf("%s: busnum=%u, cs=%u: out of range\n", __func__, |
| 79 | busnum, cs); |
| 80 | return -ENOENT; |
| 81 | } |
| 82 | ret = sandbox_spi_get_emul(state, bus, slave, &emul); |
| 83 | if (ret) { |
| 84 | printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n", |
| 85 | __func__, busnum, cs, ret); |
| 86 | return -ENOENT; |
| 87 | } |
| 88 | ret = device_probe(emul); |
| 89 | if (ret) |
| 90 | return ret; |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 91 | |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 92 | ops = spi_emul_get_ops(emul); |
| 93 | ret = ops->xfer(emul, bitlen, dout, din, flags); |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 94 | |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 95 | log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:", |
| 96 | ret, ret ? "bad" : "good"); |
| 97 | if (din) { |
| 98 | for (i = 0; i < bytes; ++i) |
| 99 | log_content(" %u:%02x", i, ((u8 *)din)[i]); |
| 100 | } |
| 101 | log_content("\n"); |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 102 | |
Mike Frysinger | 6122813 | 2013-12-03 16:43:26 -0700 | [diff] [blame] | 103 | return ret; |
| 104 | } |
Simon Glass | 6e16d90 | 2014-02-27 13:26:24 -0700 | [diff] [blame] | 105 | |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 106 | static int sandbox_spi_set_speed(struct udevice *bus, uint speed) |
Simon Glass | 6e16d90 | 2014-02-27 13:26:24 -0700 | [diff] [blame] | 107 | { |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 108 | return 0; |
Simon Glass | 6e16d90 | 2014-02-27 13:26:24 -0700 | [diff] [blame] | 109 | } |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 110 | |
| 111 | static int sandbox_spi_set_mode(struct udevice *bus, uint mode) |
| 112 | { |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static int sandbox_cs_info(struct udevice *bus, uint cs, |
| 117 | struct spi_cs_info *info) |
| 118 | { |
| 119 | /* Always allow activity on CS 0 */ |
| 120 | if (cs >= 1) |
Bin Meng | 4b06000 | 2019-09-09 06:00:01 -0700 | [diff] [blame] | 121 | return -EINVAL; |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
Simon Glass | c53b318 | 2019-10-20 21:31:47 -0600 | [diff] [blame] | 126 | static int sandbox_spi_get_mmap(struct udevice *dev, ulong *map_basep, |
| 127 | uint *map_sizep, uint *offsetp) |
| 128 | { |
| 129 | *map_basep = 0x1000; |
| 130 | *map_sizep = 0x2000; |
| 131 | *offsetp = 0x100; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 136 | static const struct dm_spi_ops sandbox_spi_ops = { |
| 137 | .xfer = sandbox_spi_xfer, |
| 138 | .set_speed = sandbox_spi_set_speed, |
| 139 | .set_mode = sandbox_spi_set_mode, |
| 140 | .cs_info = sandbox_cs_info, |
Simon Glass | c53b318 | 2019-10-20 21:31:47 -0600 | [diff] [blame] | 141 | .get_mmap = sandbox_spi_get_mmap, |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | static const struct udevice_id sandbox_spi_ids[] = { |
| 145 | { .compatible = "sandbox,spi" }, |
| 146 | { } |
| 147 | }; |
| 148 | |
| 149 | U_BOOT_DRIVER(spi_sandbox) = { |
| 150 | .name = "spi_sandbox", |
| 151 | .id = UCLASS_SPI, |
| 152 | .of_match = sandbox_spi_ids, |
Simon Glass | 49b5d6e | 2014-10-13 23:41:57 -0600 | [diff] [blame] | 153 | .ops = &sandbox_spi_ops, |
| 154 | }; |