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