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