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