Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Simulate a SPI flash |
| 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_FLASH |
| 12 | |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 13 | #include <common.h> |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 14 | #include <dm.h> |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
| 16 | #include <spi.h> |
| 17 | #include <os.h> |
| 18 | |
| 19 | #include <spi_flash.h> |
| 20 | #include "sf_internal.h" |
| 21 | |
| 22 | #include <asm/getopt.h> |
| 23 | #include <asm/spi.h> |
| 24 | #include <asm/state.h> |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 25 | #include <dm/device-internal.h> |
| 26 | #include <dm/lists.h> |
| 27 | #include <dm/uclass-internal.h> |
| 28 | |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 29 | /* |
| 30 | * The different states that our SPI flash transitions between. |
| 31 | * We need to keep track of this across multiple xfer calls since |
| 32 | * the SPI bus could possibly call down into us multiple times. |
| 33 | */ |
| 34 | enum sandbox_sf_state { |
| 35 | SF_CMD, /* default state -- we're awaiting a command */ |
| 36 | SF_ID, /* read the flash's (jedec) ID code */ |
| 37 | SF_ADDR, /* processing the offset in the flash to read/etc... */ |
| 38 | SF_READ, /* reading data from the flash */ |
| 39 | SF_WRITE, /* writing data to the flash, i.e. page programming */ |
| 40 | SF_ERASE, /* erase the flash */ |
| 41 | SF_READ_STATUS, /* read the flash's status register */ |
| 42 | SF_READ_STATUS1, /* read the flash's status register upper 8 bits*/ |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 43 | SF_WRITE_STATUS, /* write the flash's status register */ |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 46 | #if CONFIG_IS_ENABLED(LOG) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 47 | static const char *sandbox_sf_state_name(enum sandbox_sf_state state) |
| 48 | { |
| 49 | static const char * const states[] = { |
| 50 | "CMD", "ID", "ADDR", "READ", "WRITE", "ERASE", "READ_STATUS", |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 51 | "READ_STATUS1", "WRITE_STATUS", |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 52 | }; |
| 53 | return states[state]; |
| 54 | } |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 55 | #endif /* LOG */ |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 56 | |
| 57 | /* Bits for the status register */ |
| 58 | #define STAT_WIP (1 << 0) |
| 59 | #define STAT_WEL (1 << 1) |
Simon Glass | a58986c | 2018-11-06 15:21:41 -0700 | [diff] [blame] | 60 | #define STAT_BP_SHIFT 2 |
| 61 | #define STAT_BP_MASK (7 << STAT_BP_SHIFT) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 62 | |
| 63 | /* Assume all SPI flashes have 3 byte addresses since they do atm */ |
| 64 | #define SF_ADDR_LEN 3 |
| 65 | |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 66 | #define IDCODE_LEN 3 |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 67 | |
| 68 | /* Used to quickly bulk erase backing store */ |
| 69 | static u8 sandbox_sf_0xff[0x1000]; |
| 70 | |
| 71 | /* Internal state data for each SPI flash */ |
| 72 | struct sandbox_spi_flash { |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 73 | unsigned int cs; /* Chip select we are attached to */ |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 74 | /* |
| 75 | * As we receive data over the SPI bus, our flash transitions |
| 76 | * between states. For example, we start off in the SF_CMD |
| 77 | * state where the first byte tells us what operation to perform |
| 78 | * (such as read or write the flash). But the operation itself |
| 79 | * can go through a few states such as first reading in the |
| 80 | * offset in the flash to perform the requested operation. |
| 81 | * Thus "state" stores the exact state that our machine is in |
| 82 | * while "cmd" stores the overall command we're processing. |
| 83 | */ |
| 84 | enum sandbox_sf_state state; |
| 85 | uint cmd; |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 86 | /* Erase size of current erase command */ |
| 87 | uint erase_size; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 88 | /* Current position in the flash; used when reading/writing/etc... */ |
| 89 | uint off; |
| 90 | /* How many address bytes we've consumed */ |
| 91 | uint addr_bytes, pad_addr_bytes; |
| 92 | /* The current flash status (see STAT_XXX defines above) */ |
| 93 | u16 status; |
| 94 | /* Data describing the flash we're emulating */ |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 95 | const struct flash_info *data; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 96 | /* The file on disk to serv up data from */ |
| 97 | int fd; |
| 98 | }; |
| 99 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 100 | struct sandbox_spi_flash_plat_data { |
| 101 | const char *filename; |
| 102 | const char *device_name; |
| 103 | int bus; |
| 104 | int cs; |
| 105 | }; |
| 106 | |
Simon Glass | a58986c | 2018-11-06 15:21:41 -0700 | [diff] [blame] | 107 | void sandbox_sf_set_block_protect(struct udevice *dev, int bp_mask) |
| 108 | { |
| 109 | struct sandbox_spi_flash *sbsf = dev_get_priv(dev); |
| 110 | |
| 111 | sbsf->status &= ~STAT_BP_MASK; |
| 112 | sbsf->status |= bp_mask << STAT_BP_SHIFT; |
| 113 | } |
| 114 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 115 | /** |
| 116 | * This is a very strange probe function. If it has platform data (which may |
| 117 | * have come from the device tree) then this function gets the filename and |
Simon Glass | 1289e96 | 2018-10-01 11:55:09 -0600 | [diff] [blame] | 118 | * device type from there. |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 119 | */ |
| 120 | static int sandbox_sf_probe(struct udevice *dev) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 121 | { |
| 122 | /* spec = idcode:file */ |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 123 | struct sandbox_spi_flash *sbsf = dev_get_priv(dev); |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 124 | size_t len, idname_len; |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 125 | const struct flash_info *data; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 126 | struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev); |
| 127 | struct sandbox_state *state = state_get_current(); |
Simon Glass | 66613f5 | 2018-10-01 11:55:19 -0600 | [diff] [blame] | 128 | struct dm_spi_slave_platdata *slave_plat; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 129 | struct udevice *bus = dev->parent; |
| 130 | const char *spec = NULL; |
Simon Glass | 66613f5 | 2018-10-01 11:55:19 -0600 | [diff] [blame] | 131 | struct udevice *emul; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 132 | int ret = 0; |
| 133 | int cs = -1; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 134 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 135 | debug("%s: bus %d, looking for emul=%p: ", __func__, bus->seq, dev); |
Simon Glass | 66613f5 | 2018-10-01 11:55:19 -0600 | [diff] [blame] | 136 | ret = sandbox_spi_get_emul(state, bus, dev, &emul); |
| 137 | if (ret) { |
Simon Glass | 832adb2 | 2015-05-04 11:31:10 -0600 | [diff] [blame] | 138 | printf("Error: Unknown chip select for device '%s'\n", |
Simon Glass | 66613f5 | 2018-10-01 11:55:19 -0600 | [diff] [blame] | 139 | dev->name); |
| 140 | return ret; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 141 | } |
Simon Glass | 66613f5 | 2018-10-01 11:55:19 -0600 | [diff] [blame] | 142 | slave_plat = dev_get_parent_platdata(dev); |
| 143 | cs = slave_plat->cs; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 144 | debug("found at cs %d\n", cs); |
| 145 | |
| 146 | if (!pdata->filename) { |
Simon Glass | 1289e96 | 2018-10-01 11:55:09 -0600 | [diff] [blame] | 147 | printf("Error: No filename available\n"); |
| 148 | return -EINVAL; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 149 | } |
Simon Glass | 1289e96 | 2018-10-01 11:55:09 -0600 | [diff] [blame] | 150 | spec = strchr(pdata->device_name, ','); |
| 151 | if (spec) |
| 152 | spec++; |
| 153 | else |
| 154 | spec = pdata->device_name; |
| 155 | idname_len = strlen(spec); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 156 | debug("%s: device='%s'\n", __func__, spec); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 157 | |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 158 | for (data = spi_nor_ids; data->name; data++) { |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 159 | len = strlen(data->name); |
| 160 | if (idname_len != len) |
| 161 | continue; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 162 | if (!strncasecmp(spec, data->name, len)) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 163 | break; |
| 164 | } |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 165 | if (!data->name) { |
Simon Glass | 20f655d | 2016-02-24 09:14:54 -0700 | [diff] [blame] | 166 | printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len, |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 167 | spec); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 168 | ret = -EINVAL; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 169 | goto error; |
| 170 | } |
| 171 | |
| 172 | if (sandbox_sf_0xff[0] == 0x00) |
| 173 | memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff)); |
| 174 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 175 | sbsf->fd = os_open(pdata->filename, 02); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 176 | if (sbsf->fd == -1) { |
Simon Glass | 20f655d | 2016-02-24 09:14:54 -0700 | [diff] [blame] | 177 | printf("%s: unable to open file '%s'\n", __func__, |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 178 | pdata->filename); |
| 179 | ret = -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 180 | goto error; |
| 181 | } |
| 182 | |
| 183 | sbsf->data = data; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 184 | sbsf->cs = cs; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 185 | |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 186 | return 0; |
| 187 | |
| 188 | error: |
Simon Glass | 1603bf3 | 2015-01-25 08:27:09 -0700 | [diff] [blame] | 189 | debug("%s: Got error %d\n", __func__, ret); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 190 | return ret; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 193 | static int sandbox_sf_remove(struct udevice *dev) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 194 | { |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 195 | struct sandbox_spi_flash *sbsf = dev_get_priv(dev); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 196 | |
| 197 | os_close(sbsf->fd); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 198 | |
| 199 | return 0; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 202 | static void sandbox_sf_cs_activate(struct udevice *dev) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 203 | { |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 204 | struct sandbox_spi_flash *sbsf = dev_get_priv(dev); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 205 | |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 206 | log_content("sandbox_sf: CS activated; state is fresh!\n"); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 207 | |
| 208 | /* CS is asserted, so reset state */ |
| 209 | sbsf->off = 0; |
| 210 | sbsf->addr_bytes = 0; |
| 211 | sbsf->pad_addr_bytes = 0; |
| 212 | sbsf->state = SF_CMD; |
| 213 | sbsf->cmd = SF_CMD; |
| 214 | } |
| 215 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 216 | static void sandbox_sf_cs_deactivate(struct udevice *dev) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 217 | { |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 218 | log_content("sandbox_sf: CS deactivated; cmd done processing!\n"); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 221 | /* |
| 222 | * There are times when the data lines are allowed to tristate. What |
| 223 | * is actually sensed on the line depends on the hardware. It could |
| 224 | * always be 0xFF/0x00 (if there are pull ups/downs), or things could |
| 225 | * float and so we'd get garbage back. This func encapsulates that |
| 226 | * scenario so we can worry about the details here. |
| 227 | */ |
| 228 | static void sandbox_spi_tristate(u8 *buf, uint len) |
| 229 | { |
| 230 | /* XXX: make this into a user config option ? */ |
| 231 | memset(buf, 0xff, len); |
| 232 | } |
| 233 | |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 234 | /* Figure out what command this stream is telling us to do */ |
| 235 | static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx, |
| 236 | u8 *tx) |
| 237 | { |
| 238 | enum sandbox_sf_state oldstate = sbsf->state; |
| 239 | |
| 240 | /* We need to output a byte for the cmd byte we just ate */ |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 241 | if (tx) |
| 242 | sandbox_spi_tristate(tx, 1); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 243 | |
| 244 | sbsf->cmd = rx[0]; |
| 245 | switch (sbsf->cmd) { |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 246 | case SPINOR_OP_RDID: |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 247 | sbsf->state = SF_ID; |
| 248 | sbsf->cmd = SF_ID; |
| 249 | break; |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 250 | case SPINOR_OP_READ_FAST: |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 251 | sbsf->pad_addr_bytes = 1; |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 252 | case SPINOR_OP_READ: |
| 253 | case SPINOR_OP_PP: |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 254 | sbsf->state = SF_ADDR; |
| 255 | break; |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 256 | case SPINOR_OP_WRDI: |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 257 | debug(" write disabled\n"); |
| 258 | sbsf->status &= ~STAT_WEL; |
| 259 | break; |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 260 | case SPINOR_OP_RDSR: |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 261 | sbsf->state = SF_READ_STATUS; |
| 262 | break; |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 263 | case SPINOR_OP_RDSR2: |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 264 | sbsf->state = SF_READ_STATUS1; |
| 265 | break; |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 266 | case SPINOR_OP_WREN: |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 267 | debug(" write enabled\n"); |
| 268 | sbsf->status |= STAT_WEL; |
| 269 | break; |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 270 | case SPINOR_OP_WRSR: |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 271 | sbsf->state = SF_WRITE_STATUS; |
| 272 | break; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 273 | default: { |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 274 | int flags = sbsf->data->flags; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 275 | |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 276 | /* we only support erase here */ |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 277 | if (sbsf->cmd == SPINOR_OP_CHIP_ERASE) { |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 278 | sbsf->erase_size = sbsf->data->sector_size * |
Jagan Teki | eccb6be | 2016-10-30 23:16:15 +0530 | [diff] [blame] | 279 | sbsf->data->n_sectors; |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 280 | } else if (sbsf->cmd == SPINOR_OP_BE_4K && (flags & SECT_4K)) { |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 281 | sbsf->erase_size = 4 << 10; |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 282 | } else if (sbsf->cmd == SPINOR_OP_SE && !(flags & SECT_4K)) { |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 283 | sbsf->erase_size = 64 << 10; |
| 284 | } else { |
| 285 | debug(" cmd unknown: %#x\n", sbsf->cmd); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 286 | return -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 287 | } |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 288 | sbsf->state = SF_ADDR; |
| 289 | break; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | if (oldstate != sbsf->state) |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 294 | log_content(" cmd: transition to %s state\n", |
| 295 | sandbox_sf_state_name(sbsf->state)); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size) |
| 301 | { |
| 302 | int todo; |
| 303 | int ret; |
| 304 | |
| 305 | while (size > 0) { |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 306 | todo = min(size, (int)sizeof(sandbox_sf_0xff)); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 307 | ret = os_write(sbsf->fd, sandbox_sf_0xff, todo); |
| 308 | if (ret != todo) |
| 309 | return ret; |
| 310 | size -= todo; |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 316 | static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen, |
| 317 | const void *rxp, void *txp, unsigned long flags) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 318 | { |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 319 | struct sandbox_spi_flash *sbsf = dev_get_priv(dev); |
| 320 | const uint8_t *rx = rxp; |
| 321 | uint8_t *tx = txp; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 322 | uint cnt, pos = 0; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 323 | int bytes = bitlen / 8; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 324 | int ret; |
| 325 | |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 326 | log_content("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state, |
| 327 | sandbox_sf_state_name(sbsf->state), bytes); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 328 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 329 | if ((flags & SPI_XFER_BEGIN)) |
| 330 | sandbox_sf_cs_activate(dev); |
| 331 | |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 332 | if (sbsf->state == SF_CMD) { |
| 333 | /* Figure out the initial state */ |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 334 | ret = sandbox_sf_process_cmd(sbsf, rx, tx); |
| 335 | if (ret) |
| 336 | return ret; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 337 | ++pos; |
| 338 | } |
| 339 | |
| 340 | /* Process the remaining data */ |
| 341 | while (pos < bytes) { |
| 342 | switch (sbsf->state) { |
| 343 | case SF_ID: { |
| 344 | u8 id; |
| 345 | |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 346 | log_content(" id: off:%u tx:", sbsf->off); |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 347 | if (sbsf->off < IDCODE_LEN) { |
| 348 | /* Extract correct byte from ID 0x00aabbcc */ |
Jagan Teki | 523b4e3 | 2016-10-30 23:16:12 +0530 | [diff] [blame] | 349 | id = ((JEDEC_MFR(sbsf->data) << 16) | |
| 350 | JEDEC_ID(sbsf->data)) >> |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 351 | (8 * (IDCODE_LEN - 1 - sbsf->off)); |
| 352 | } else { |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 353 | id = 0; |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 354 | } |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 355 | log_content("%d %02x\n", sbsf->off, id); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 356 | tx[pos++] = id; |
| 357 | ++sbsf->off; |
| 358 | break; |
| 359 | } |
| 360 | case SF_ADDR: |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 361 | log_content(" addr: bytes:%u rx:%02x ", |
| 362 | sbsf->addr_bytes, rx[pos]); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 363 | |
| 364 | if (sbsf->addr_bytes++ < SF_ADDR_LEN) |
| 365 | sbsf->off = (sbsf->off << 8) | rx[pos]; |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 366 | log_content("addr:%06x\n", sbsf->off); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 367 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 368 | if (tx) |
| 369 | sandbox_spi_tristate(&tx[pos], 1); |
| 370 | pos++; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 371 | |
| 372 | /* See if we're done processing */ |
| 373 | if (sbsf->addr_bytes < |
| 374 | SF_ADDR_LEN + sbsf->pad_addr_bytes) |
| 375 | break; |
| 376 | |
| 377 | /* Next state! */ |
| 378 | if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) { |
| 379 | puts("sandbox_sf: os_lseek() failed"); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 380 | return -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 381 | } |
| 382 | switch (sbsf->cmd) { |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 383 | case SPINOR_OP_READ_FAST: |
| 384 | case SPINOR_OP_READ: |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 385 | sbsf->state = SF_READ; |
| 386 | break; |
Vignesh R | c4e8862 | 2019-02-05 11:29:23 +0530 | [diff] [blame^] | 387 | case SPINOR_OP_PP: |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 388 | sbsf->state = SF_WRITE; |
| 389 | break; |
| 390 | default: |
| 391 | /* assume erase state ... */ |
| 392 | sbsf->state = SF_ERASE; |
| 393 | goto case_sf_erase; |
| 394 | } |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 395 | log_content(" cmd: transition to %s state\n", |
| 396 | sandbox_sf_state_name(sbsf->state)); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 397 | break; |
| 398 | case SF_READ: |
| 399 | /* |
| 400 | * XXX: need to handle exotic behavior: |
| 401 | * - reading past end of device |
| 402 | */ |
| 403 | |
| 404 | cnt = bytes - pos; |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 405 | log_content(" tx: read(%u)\n", cnt); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 406 | assert(tx); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 407 | ret = os_read(sbsf->fd, tx + pos, cnt); |
| 408 | if (ret < 0) { |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 409 | puts("sandbox_sf: os_read() failed\n"); |
| 410 | return -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 411 | } |
| 412 | pos += ret; |
| 413 | break; |
| 414 | case SF_READ_STATUS: |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 415 | log_content(" read status: %#x\n", sbsf->status); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 416 | cnt = bytes - pos; |
| 417 | memset(tx + pos, sbsf->status, cnt); |
| 418 | pos += cnt; |
| 419 | break; |
| 420 | case SF_READ_STATUS1: |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 421 | log_content(" read status: %#x\n", sbsf->status); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 422 | cnt = bytes - pos; |
| 423 | memset(tx + pos, sbsf->status >> 8, cnt); |
| 424 | pos += cnt; |
| 425 | break; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 426 | case SF_WRITE_STATUS: |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 427 | log_content(" write status: %#x (ignored)\n", rx[pos]); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 428 | pos = bytes; |
| 429 | break; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 430 | case SF_WRITE: |
| 431 | /* |
| 432 | * XXX: need to handle exotic behavior: |
| 433 | * - unaligned addresses |
| 434 | * - more than a page (256) worth of data |
| 435 | * - reading past end of device |
| 436 | */ |
| 437 | if (!(sbsf->status & STAT_WEL)) { |
| 438 | puts("sandbox_sf: write enable not set before write\n"); |
| 439 | goto done; |
| 440 | } |
| 441 | |
| 442 | cnt = bytes - pos; |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 443 | log_content(" rx: write(%u)\n", cnt); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 444 | if (tx) |
| 445 | sandbox_spi_tristate(&tx[pos], cnt); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 446 | ret = os_write(sbsf->fd, rx + pos, cnt); |
| 447 | if (ret < 0) { |
| 448 | puts("sandbox_spi: os_write() failed\n"); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 449 | return -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 450 | } |
| 451 | pos += ret; |
| 452 | sbsf->status &= ~STAT_WEL; |
| 453 | break; |
| 454 | case SF_ERASE: |
| 455 | case_sf_erase: { |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 456 | if (!(sbsf->status & STAT_WEL)) { |
| 457 | puts("sandbox_sf: write enable not set before erase\n"); |
| 458 | goto done; |
| 459 | } |
| 460 | |
| 461 | /* verify address is aligned */ |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 462 | if (sbsf->off & (sbsf->erase_size - 1)) { |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 463 | log_content(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n", |
| 464 | sbsf->cmd, sbsf->erase_size, |
| 465 | sbsf->off); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 466 | sbsf->status &= ~STAT_WEL; |
| 467 | goto done; |
| 468 | } |
| 469 | |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 470 | log_content(" sector erase addr: %u, size: %u\n", |
| 471 | sbsf->off, sbsf->erase_size); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 472 | |
| 473 | cnt = bytes - pos; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 474 | if (tx) |
| 475 | sandbox_spi_tristate(&tx[pos], cnt); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 476 | pos += cnt; |
| 477 | |
| 478 | /* |
| 479 | * TODO(vapier@gentoo.org): latch WIP in status, and |
| 480 | * delay before clearing it ? |
| 481 | */ |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 482 | ret = sandbox_erase_part(sbsf, sbsf->erase_size); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 483 | sbsf->status &= ~STAT_WEL; |
| 484 | if (ret) { |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 485 | log_content("sandbox_sf: Erase failed\n"); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 486 | goto done; |
| 487 | } |
| 488 | goto done; |
| 489 | } |
| 490 | default: |
Simon Glass | c3aed5d | 2018-10-01 11:55:13 -0600 | [diff] [blame] | 491 | log_content(" ??? no idea what to do ???\n"); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 492 | goto done; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | done: |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 497 | if (flags & SPI_XFER_END) |
| 498 | sandbox_sf_cs_deactivate(dev); |
| 499 | return pos == bytes ? 0 : -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 502 | int sandbox_sf_ofdata_to_platdata(struct udevice *dev) |
| 503 | { |
| 504 | struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 505 | |
Simon Glass | a118179 | 2017-05-18 20:09:58 -0600 | [diff] [blame] | 506 | pdata->filename = dev_read_string(dev, "sandbox,filename"); |
| 507 | pdata->device_name = dev_read_string(dev, "compatible"); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 508 | if (!pdata->filename || !pdata->device_name) { |
| 509 | debug("%s: Missing properties, filename=%s, device_name=%s\n", |
| 510 | __func__, pdata->filename, pdata->device_name); |
| 511 | return -EINVAL; |
| 512 | } |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static const struct dm_spi_emul_ops sandbox_sf_emul_ops = { |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 518 | .xfer = sandbox_sf_xfer, |
| 519 | }; |
| 520 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 521 | #ifdef CONFIG_SPI_FLASH |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 522 | int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs, |
Simon Glass | 008dcdd | 2018-06-11 13:07:16 -0600 | [diff] [blame] | 523 | struct udevice *bus, ofnode node, const char *spec) |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 524 | { |
| 525 | struct udevice *emul; |
| 526 | char name[20], *str; |
| 527 | struct driver *drv; |
| 528 | int ret; |
| 529 | |
| 530 | /* now the emulator */ |
| 531 | strncpy(name, spec, sizeof(name) - 6); |
| 532 | name[sizeof(name) - 6] = '\0'; |
| 533 | strcat(name, "-emul"); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 534 | drv = lists_driver_lookup_name("sandbox_sf_emul"); |
| 535 | if (!drv) { |
| 536 | puts("Cannot find sandbox_sf_emul driver\n"); |
| 537 | return -ENOENT; |
| 538 | } |
Simon Glass | fb95283 | 2018-06-12 00:05:01 -0600 | [diff] [blame] | 539 | str = strdup(name); |
| 540 | if (!str) |
| 541 | return -ENOMEM; |
Simon Glass | 008dcdd | 2018-06-11 13:07:16 -0600 | [diff] [blame] | 542 | ret = device_bind_ofnode(bus, drv, str, NULL, node, &emul); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 543 | if (ret) { |
Simon Glass | fb95283 | 2018-06-12 00:05:01 -0600 | [diff] [blame] | 544 | free(str); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 545 | printf("Cannot create emul device for spec '%s' (err=%d)\n", |
| 546 | spec, ret); |
| 547 | return ret; |
| 548 | } |
| 549 | state->spi[busnum][cs].emul = emul; |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs) |
| 555 | { |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 556 | struct udevice *dev; |
| 557 | |
| 558 | dev = state->spi[busnum][cs].emul; |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 559 | device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 560 | device_unbind(dev); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 561 | state->spi[busnum][cs].emul = NULL; |
| 562 | } |
| 563 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 564 | int sandbox_spi_get_emul(struct sandbox_state *state, |
| 565 | struct udevice *bus, struct udevice *slave, |
| 566 | struct udevice **emulp) |
| 567 | { |
| 568 | struct sandbox_spi_info *info; |
| 569 | int busnum = bus->seq; |
| 570 | int cs = spi_chip_select(slave); |
| 571 | int ret; |
| 572 | |
| 573 | info = &state->spi[busnum][cs]; |
| 574 | if (!info->emul) { |
| 575 | /* Use the same device tree node as the SPI flash device */ |
| 576 | debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ", |
| 577 | __func__, busnum, cs); |
| 578 | ret = sandbox_sf_bind_emul(state, busnum, cs, bus, |
Simon Glass | 008dcdd | 2018-06-11 13:07:16 -0600 | [diff] [blame] | 579 | dev_ofnode(slave), slave->name); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 580 | if (ret) { |
| 581 | debug("failed (err=%d)\n", ret); |
| 582 | return ret; |
| 583 | } |
| 584 | debug("OK\n"); |
| 585 | } |
| 586 | *emulp = info->emul; |
| 587 | |
| 588 | return 0; |
| 589 | } |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 590 | #endif |
| 591 | |
| 592 | static const struct udevice_id sandbox_sf_ids[] = { |
| 593 | { .compatible = "sandbox,spi-flash" }, |
| 594 | { } |
| 595 | }; |
| 596 | |
| 597 | U_BOOT_DRIVER(sandbox_sf_emul) = { |
| 598 | .name = "sandbox_sf_emul", |
| 599 | .id = UCLASS_SPI_EMUL, |
| 600 | .of_match = sandbox_sf_ids, |
| 601 | .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata, |
| 602 | .probe = sandbox_sf_probe, |
| 603 | .remove = sandbox_sf_remove, |
| 604 | .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash), |
| 605 | .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data), |
| 606 | .ops = &sandbox_sf_emul_ops, |
| 607 | }; |