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 | |
| 11 | #include <common.h> |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <spi.h> |
| 15 | #include <os.h> |
| 16 | |
| 17 | #include <spi_flash.h> |
| 18 | #include "sf_internal.h" |
| 19 | |
| 20 | #include <asm/getopt.h> |
| 21 | #include <asm/spi.h> |
| 22 | #include <asm/state.h> |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 23 | #include <dm/device-internal.h> |
| 24 | #include <dm/lists.h> |
| 25 | #include <dm/uclass-internal.h> |
| 26 | |
| 27 | DECLARE_GLOBAL_DATA_PTR; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 28 | |
| 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 | |
| 46 | static const char *sandbox_sf_state_name(enum sandbox_sf_state state) |
| 47 | { |
| 48 | static const char * const states[] = { |
| 49 | "CMD", "ID", "ADDR", "READ", "WRITE", "ERASE", "READ_STATUS", |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 50 | "READ_STATUS1", "WRITE_STATUS", |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 51 | }; |
| 52 | return states[state]; |
| 53 | } |
| 54 | |
| 55 | /* Bits for the status register */ |
| 56 | #define STAT_WIP (1 << 0) |
| 57 | #define STAT_WEL (1 << 1) |
| 58 | |
| 59 | /* Assume all SPI flashes have 3 byte addresses since they do atm */ |
| 60 | #define SF_ADDR_LEN 3 |
| 61 | |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 62 | #define IDCODE_LEN 3 |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 63 | |
| 64 | /* Used to quickly bulk erase backing store */ |
| 65 | static u8 sandbox_sf_0xff[0x1000]; |
| 66 | |
| 67 | /* Internal state data for each SPI flash */ |
| 68 | struct sandbox_spi_flash { |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 69 | unsigned int cs; /* Chip select we are attached to */ |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 70 | /* |
| 71 | * As we receive data over the SPI bus, our flash transitions |
| 72 | * between states. For example, we start off in the SF_CMD |
| 73 | * state where the first byte tells us what operation to perform |
| 74 | * (such as read or write the flash). But the operation itself |
| 75 | * can go through a few states such as first reading in the |
| 76 | * offset in the flash to perform the requested operation. |
| 77 | * Thus "state" stores the exact state that our machine is in |
| 78 | * while "cmd" stores the overall command we're processing. |
| 79 | */ |
| 80 | enum sandbox_sf_state state; |
| 81 | uint cmd; |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 82 | /* Erase size of current erase command */ |
| 83 | uint erase_size; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 84 | /* Current position in the flash; used when reading/writing/etc... */ |
| 85 | uint off; |
| 86 | /* How many address bytes we've consumed */ |
| 87 | uint addr_bytes, pad_addr_bytes; |
| 88 | /* The current flash status (see STAT_XXX defines above) */ |
| 89 | u16 status; |
| 90 | /* Data describing the flash we're emulating */ |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 91 | const struct spi_flash_params *data; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 92 | /* The file on disk to serv up data from */ |
| 93 | int fd; |
| 94 | }; |
| 95 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 96 | struct sandbox_spi_flash_plat_data { |
| 97 | const char *filename; |
| 98 | const char *device_name; |
| 99 | int bus; |
| 100 | int cs; |
| 101 | }; |
| 102 | |
| 103 | /** |
| 104 | * This is a very strange probe function. If it has platform data (which may |
| 105 | * have come from the device tree) then this function gets the filename and |
| 106 | * device type from there. Failing that it looks at the command line |
| 107 | * parameter. |
| 108 | */ |
| 109 | static int sandbox_sf_probe(struct udevice *dev) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 110 | { |
| 111 | /* spec = idcode:file */ |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 112 | struct sandbox_spi_flash *sbsf = dev_get_priv(dev); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 113 | const char *file; |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 114 | size_t len, idname_len; |
| 115 | const struct spi_flash_params *data; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 116 | struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev); |
| 117 | struct sandbox_state *state = state_get_current(); |
| 118 | struct udevice *bus = dev->parent; |
| 119 | const char *spec = NULL; |
| 120 | int ret = 0; |
| 121 | int cs = -1; |
| 122 | int i; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 123 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 124 | debug("%s: bus %d, looking for emul=%p: ", __func__, bus->seq, dev); |
| 125 | if (bus->seq >= 0 && bus->seq < CONFIG_SANDBOX_SPI_MAX_BUS) { |
| 126 | for (i = 0; i < CONFIG_SANDBOX_SPI_MAX_CS; i++) { |
| 127 | if (state->spi[bus->seq][i].emul == dev) |
| 128 | cs = i; |
| 129 | } |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 130 | } |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 131 | if (cs == -1) { |
| 132 | printf("Error: Unknown chip select for device '%s'", |
| 133 | dev->name); |
| 134 | return -EINVAL; |
| 135 | } |
| 136 | debug("found at cs %d\n", cs); |
| 137 | |
| 138 | if (!pdata->filename) { |
| 139 | struct sandbox_state *state = state_get_current(); |
| 140 | |
| 141 | assert(bus->seq != -1); |
| 142 | if (bus->seq < CONFIG_SANDBOX_SPI_MAX_BUS) |
| 143 | spec = state->spi[bus->seq][cs].spec; |
| 144 | if (!spec) |
| 145 | return -ENOENT; |
| 146 | |
| 147 | file = strchr(spec, ':'); |
| 148 | if (!file) { |
| 149 | printf("sandbox_sf: unable to parse file\n"); |
| 150 | ret = -EINVAL; |
| 151 | goto error; |
| 152 | } |
| 153 | idname_len = file - spec; |
| 154 | pdata->filename = file + 1; |
| 155 | pdata->device_name = spec; |
| 156 | ++file; |
| 157 | } else { |
| 158 | spec = strchr(pdata->device_name, ','); |
| 159 | if (spec) |
| 160 | spec++; |
| 161 | else |
| 162 | spec = pdata->device_name; |
| 163 | idname_len = strlen(spec); |
| 164 | } |
| 165 | debug("%s: device='%s'\n", __func__, spec); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 166 | |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 167 | for (data = spi_flash_params_table; data->name; data++) { |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 168 | len = strlen(data->name); |
| 169 | if (idname_len != len) |
| 170 | continue; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 171 | if (!strncasecmp(spec, data->name, len)) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 172 | break; |
| 173 | } |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 174 | if (!data->name) { |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 175 | printf("sandbox_sf: unknown flash '%*s'\n", (int)idname_len, |
| 176 | spec); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 177 | ret = -EINVAL; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 178 | goto error; |
| 179 | } |
| 180 | |
| 181 | if (sandbox_sf_0xff[0] == 0x00) |
| 182 | memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff)); |
| 183 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 184 | sbsf->fd = os_open(pdata->filename, 02); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 185 | if (sbsf->fd == -1) { |
| 186 | free(sbsf); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 187 | printf("sandbox_sf: unable to open file '%s'\n", |
| 188 | pdata->filename); |
| 189 | ret = -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 190 | goto error; |
| 191 | } |
| 192 | |
| 193 | sbsf->data = data; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 194 | sbsf->cs = cs; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 195 | |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 196 | return 0; |
| 197 | |
| 198 | error: |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 199 | return ret; |
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 int sandbox_sf_remove(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 | |
| 206 | os_close(sbsf->fd); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 207 | |
| 208 | return 0; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 211 | static void sandbox_sf_cs_activate(struct udevice *dev) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 212 | { |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 213 | struct sandbox_spi_flash *sbsf = dev_get_priv(dev); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 214 | |
| 215 | debug("sandbox_sf: CS activated; state is fresh!\n"); |
| 216 | |
| 217 | /* CS is asserted, so reset state */ |
| 218 | sbsf->off = 0; |
| 219 | sbsf->addr_bytes = 0; |
| 220 | sbsf->pad_addr_bytes = 0; |
| 221 | sbsf->state = SF_CMD; |
| 222 | sbsf->cmd = SF_CMD; |
| 223 | } |
| 224 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 225 | static void sandbox_sf_cs_deactivate(struct udevice *dev) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 226 | { |
| 227 | debug("sandbox_sf: CS deactivated; cmd done processing!\n"); |
| 228 | } |
| 229 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 230 | /* |
| 231 | * There are times when the data lines are allowed to tristate. What |
| 232 | * is actually sensed on the line depends on the hardware. It could |
| 233 | * always be 0xFF/0x00 (if there are pull ups/downs), or things could |
| 234 | * float and so we'd get garbage back. This func encapsulates that |
| 235 | * scenario so we can worry about the details here. |
| 236 | */ |
| 237 | static void sandbox_spi_tristate(u8 *buf, uint len) |
| 238 | { |
| 239 | /* XXX: make this into a user config option ? */ |
| 240 | memset(buf, 0xff, len); |
| 241 | } |
| 242 | |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 243 | /* Figure out what command this stream is telling us to do */ |
| 244 | static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx, |
| 245 | u8 *tx) |
| 246 | { |
| 247 | enum sandbox_sf_state oldstate = sbsf->state; |
| 248 | |
| 249 | /* 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] | 250 | if (tx) |
| 251 | sandbox_spi_tristate(tx, 1); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 252 | |
| 253 | sbsf->cmd = rx[0]; |
| 254 | switch (sbsf->cmd) { |
| 255 | case CMD_READ_ID: |
| 256 | sbsf->state = SF_ID; |
| 257 | sbsf->cmd = SF_ID; |
| 258 | break; |
| 259 | case CMD_READ_ARRAY_FAST: |
| 260 | sbsf->pad_addr_bytes = 1; |
| 261 | case CMD_READ_ARRAY_SLOW: |
| 262 | case CMD_PAGE_PROGRAM: |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 263 | sbsf->state = SF_ADDR; |
| 264 | break; |
| 265 | case CMD_WRITE_DISABLE: |
| 266 | debug(" write disabled\n"); |
| 267 | sbsf->status &= ~STAT_WEL; |
| 268 | break; |
| 269 | case CMD_READ_STATUS: |
| 270 | sbsf->state = SF_READ_STATUS; |
| 271 | break; |
| 272 | case CMD_READ_STATUS1: |
| 273 | sbsf->state = SF_READ_STATUS1; |
| 274 | break; |
| 275 | case CMD_WRITE_ENABLE: |
| 276 | debug(" write enabled\n"); |
| 277 | sbsf->status |= STAT_WEL; |
| 278 | break; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 279 | case CMD_WRITE_STATUS: |
| 280 | sbsf->state = SF_WRITE_STATUS; |
| 281 | break; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 282 | default: { |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 283 | int flags = sbsf->data->flags; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 284 | |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 285 | /* we only support erase here */ |
| 286 | if (sbsf->cmd == CMD_ERASE_CHIP) { |
| 287 | sbsf->erase_size = sbsf->data->sector_size * |
| 288 | sbsf->data->nr_sectors; |
| 289 | } else if (sbsf->cmd == CMD_ERASE_4K && (flags & SECT_4K)) { |
| 290 | sbsf->erase_size = 4 << 10; |
| 291 | } else if (sbsf->cmd == CMD_ERASE_32K && (flags & SECT_32K)) { |
| 292 | sbsf->erase_size = 32 << 10; |
| 293 | } else if (sbsf->cmd == CMD_ERASE_64K && |
| 294 | !(flags & (SECT_4K | SECT_32K))) { |
| 295 | sbsf->erase_size = 64 << 10; |
| 296 | } else { |
| 297 | debug(" cmd unknown: %#x\n", sbsf->cmd); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 298 | return -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 299 | } |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 300 | sbsf->state = SF_ADDR; |
| 301 | break; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | if (oldstate != sbsf->state) |
| 306 | debug(" cmd: transition to %s state\n", |
| 307 | sandbox_sf_state_name(sbsf->state)); |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size) |
| 313 | { |
| 314 | int todo; |
| 315 | int ret; |
| 316 | |
| 317 | while (size > 0) { |
| 318 | todo = min(size, sizeof(sandbox_sf_0xff)); |
| 319 | ret = os_write(sbsf->fd, sandbox_sf_0xff, todo); |
| 320 | if (ret != todo) |
| 321 | return ret; |
| 322 | size -= todo; |
| 323 | } |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 328 | static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen, |
| 329 | const void *rxp, void *txp, unsigned long flags) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 330 | { |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 331 | struct sandbox_spi_flash *sbsf = dev_get_priv(dev); |
| 332 | const uint8_t *rx = rxp; |
| 333 | uint8_t *tx = txp; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 334 | uint cnt, pos = 0; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 335 | int bytes = bitlen / 8; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 336 | int ret; |
| 337 | |
| 338 | debug("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state, |
| 339 | sandbox_sf_state_name(sbsf->state), bytes); |
| 340 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 341 | if ((flags & SPI_XFER_BEGIN)) |
| 342 | sandbox_sf_cs_activate(dev); |
| 343 | |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 344 | if (sbsf->state == SF_CMD) { |
| 345 | /* Figure out the initial state */ |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 346 | ret = sandbox_sf_process_cmd(sbsf, rx, tx); |
| 347 | if (ret) |
| 348 | return ret; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 349 | ++pos; |
| 350 | } |
| 351 | |
| 352 | /* Process the remaining data */ |
| 353 | while (pos < bytes) { |
| 354 | switch (sbsf->state) { |
| 355 | case SF_ID: { |
| 356 | u8 id; |
| 357 | |
| 358 | debug(" id: off:%u tx:", sbsf->off); |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 359 | if (sbsf->off < IDCODE_LEN) { |
| 360 | /* Extract correct byte from ID 0x00aabbcc */ |
| 361 | id = sbsf->data->jedec >> |
| 362 | (8 * (IDCODE_LEN - 1 - sbsf->off)); |
| 363 | } else { |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 364 | id = 0; |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 365 | } |
| 366 | debug("%d %02x\n", sbsf->off, id); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 367 | tx[pos++] = id; |
| 368 | ++sbsf->off; |
| 369 | break; |
| 370 | } |
| 371 | case SF_ADDR: |
| 372 | debug(" addr: bytes:%u rx:%02x ", sbsf->addr_bytes, |
| 373 | rx[pos]); |
| 374 | |
| 375 | if (sbsf->addr_bytes++ < SF_ADDR_LEN) |
| 376 | sbsf->off = (sbsf->off << 8) | rx[pos]; |
| 377 | debug("addr:%06x\n", sbsf->off); |
| 378 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 379 | if (tx) |
| 380 | sandbox_spi_tristate(&tx[pos], 1); |
| 381 | pos++; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 382 | |
| 383 | /* See if we're done processing */ |
| 384 | if (sbsf->addr_bytes < |
| 385 | SF_ADDR_LEN + sbsf->pad_addr_bytes) |
| 386 | break; |
| 387 | |
| 388 | /* Next state! */ |
| 389 | if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) { |
| 390 | puts("sandbox_sf: os_lseek() failed"); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 391 | return -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 392 | } |
| 393 | switch (sbsf->cmd) { |
| 394 | case CMD_READ_ARRAY_FAST: |
| 395 | case CMD_READ_ARRAY_SLOW: |
| 396 | sbsf->state = SF_READ; |
| 397 | break; |
| 398 | case CMD_PAGE_PROGRAM: |
| 399 | sbsf->state = SF_WRITE; |
| 400 | break; |
| 401 | default: |
| 402 | /* assume erase state ... */ |
| 403 | sbsf->state = SF_ERASE; |
| 404 | goto case_sf_erase; |
| 405 | } |
| 406 | debug(" cmd: transition to %s state\n", |
| 407 | sandbox_sf_state_name(sbsf->state)); |
| 408 | break; |
| 409 | case SF_READ: |
| 410 | /* |
| 411 | * XXX: need to handle exotic behavior: |
| 412 | * - reading past end of device |
| 413 | */ |
| 414 | |
| 415 | cnt = bytes - pos; |
| 416 | debug(" tx: read(%u)\n", cnt); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 417 | assert(tx); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 418 | ret = os_read(sbsf->fd, tx + pos, cnt); |
| 419 | if (ret < 0) { |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 420 | puts("sandbox_sf: os_read() failed\n"); |
| 421 | return -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 422 | } |
| 423 | pos += ret; |
| 424 | break; |
| 425 | case SF_READ_STATUS: |
| 426 | debug(" read status: %#x\n", sbsf->status); |
| 427 | cnt = bytes - pos; |
| 428 | memset(tx + pos, sbsf->status, cnt); |
| 429 | pos += cnt; |
| 430 | break; |
| 431 | case SF_READ_STATUS1: |
| 432 | debug(" read status: %#x\n", sbsf->status); |
| 433 | cnt = bytes - pos; |
| 434 | memset(tx + pos, sbsf->status >> 8, cnt); |
| 435 | pos += cnt; |
| 436 | break; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 437 | case SF_WRITE_STATUS: |
| 438 | debug(" write status: %#x (ignored)\n", rx[pos]); |
| 439 | pos = bytes; |
| 440 | break; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 441 | case SF_WRITE: |
| 442 | /* |
| 443 | * XXX: need to handle exotic behavior: |
| 444 | * - unaligned addresses |
| 445 | * - more than a page (256) worth of data |
| 446 | * - reading past end of device |
| 447 | */ |
| 448 | if (!(sbsf->status & STAT_WEL)) { |
| 449 | puts("sandbox_sf: write enable not set before write\n"); |
| 450 | goto done; |
| 451 | } |
| 452 | |
| 453 | cnt = bytes - pos; |
| 454 | debug(" rx: write(%u)\n", cnt); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 455 | if (tx) |
| 456 | sandbox_spi_tristate(&tx[pos], cnt); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 457 | ret = os_write(sbsf->fd, rx + pos, cnt); |
| 458 | if (ret < 0) { |
| 459 | puts("sandbox_spi: os_write() failed\n"); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 460 | return -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 461 | } |
| 462 | pos += ret; |
| 463 | sbsf->status &= ~STAT_WEL; |
| 464 | break; |
| 465 | case SF_ERASE: |
| 466 | case_sf_erase: { |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 467 | if (!(sbsf->status & STAT_WEL)) { |
| 468 | puts("sandbox_sf: write enable not set before erase\n"); |
| 469 | goto done; |
| 470 | } |
| 471 | |
| 472 | /* verify address is aligned */ |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 473 | if (sbsf->off & (sbsf->erase_size - 1)) { |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 474 | debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n", |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 475 | sbsf->cmd, sbsf->erase_size, |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 476 | sbsf->off); |
| 477 | sbsf->status &= ~STAT_WEL; |
| 478 | goto done; |
| 479 | } |
| 480 | |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 481 | debug(" sector erase addr: %u, size: %u\n", sbsf->off, |
| 482 | sbsf->erase_size); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 483 | |
| 484 | cnt = bytes - pos; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 485 | if (tx) |
| 486 | sandbox_spi_tristate(&tx[pos], cnt); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 487 | pos += cnt; |
| 488 | |
| 489 | /* |
| 490 | * TODO(vapier@gentoo.org): latch WIP in status, and |
| 491 | * delay before clearing it ? |
| 492 | */ |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 493 | ret = sandbox_erase_part(sbsf, sbsf->erase_size); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 494 | sbsf->status &= ~STAT_WEL; |
| 495 | if (ret) { |
| 496 | debug("sandbox_sf: Erase failed\n"); |
| 497 | goto done; |
| 498 | } |
| 499 | goto done; |
| 500 | } |
| 501 | default: |
| 502 | debug(" ??? no idea what to do ???\n"); |
| 503 | goto done; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | done: |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 508 | if (flags & SPI_XFER_END) |
| 509 | sandbox_sf_cs_deactivate(dev); |
| 510 | return pos == bytes ? 0 : -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 513 | int sandbox_sf_ofdata_to_platdata(struct udevice *dev) |
| 514 | { |
| 515 | struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev); |
| 516 | const void *blob = gd->fdt_blob; |
| 517 | int node = dev->of_offset; |
| 518 | |
| 519 | pdata->filename = fdt_getprop(blob, node, "sandbox,filename", NULL); |
| 520 | pdata->device_name = fdt_getprop(blob, node, "compatible", NULL); |
| 521 | if (!pdata->filename || !pdata->device_name) { |
| 522 | debug("%s: Missing properties, filename=%s, device_name=%s\n", |
| 523 | __func__, pdata->filename, pdata->device_name); |
| 524 | return -EINVAL; |
| 525 | } |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | static const struct dm_spi_emul_ops sandbox_sf_emul_ops = { |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 531 | .xfer = sandbox_sf_xfer, |
| 532 | }; |
| 533 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 534 | #ifdef CONFIG_SPI_FLASH |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 535 | static int sandbox_cmdline_cb_spi_sf(struct sandbox_state *state, |
| 536 | const char *arg) |
| 537 | { |
| 538 | unsigned long bus, cs; |
| 539 | const char *spec = sandbox_spi_parse_spec(arg, &bus, &cs); |
| 540 | |
| 541 | if (!spec) |
| 542 | return 1; |
| 543 | |
| 544 | /* |
| 545 | * It is safe to not make a copy of 'spec' because it comes from the |
| 546 | * command line. |
| 547 | * |
| 548 | * TODO(sjg@chromium.org): It would be nice if we could parse the |
| 549 | * spec here, but the problem is that no U-Boot init has been done |
| 550 | * yet. Perhaps we can figure something out. |
| 551 | */ |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 552 | state->spi[bus][cs].spec = spec; |
| 553 | return 0; |
| 554 | } |
| 555 | SANDBOX_CMDLINE_OPT(spi_sf, 1, "connect a SPI flash: <bus>:<cs>:<id>:<file>"); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 556 | |
| 557 | int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs, |
| 558 | struct udevice *bus, int of_offset, const char *spec) |
| 559 | { |
| 560 | struct udevice *emul; |
| 561 | char name[20], *str; |
| 562 | struct driver *drv; |
| 563 | int ret; |
| 564 | |
| 565 | /* now the emulator */ |
| 566 | strncpy(name, spec, sizeof(name) - 6); |
| 567 | name[sizeof(name) - 6] = '\0'; |
| 568 | strcat(name, "-emul"); |
| 569 | str = strdup(name); |
| 570 | if (!str) |
| 571 | return -ENOMEM; |
| 572 | drv = lists_driver_lookup_name("sandbox_sf_emul"); |
| 573 | if (!drv) { |
| 574 | puts("Cannot find sandbox_sf_emul driver\n"); |
| 575 | return -ENOENT; |
| 576 | } |
| 577 | ret = device_bind(bus, drv, str, NULL, of_offset, &emul); |
| 578 | if (ret) { |
| 579 | printf("Cannot create emul device for spec '%s' (err=%d)\n", |
| 580 | spec, ret); |
| 581 | return ret; |
| 582 | } |
| 583 | state->spi[busnum][cs].emul = emul; |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs) |
| 589 | { |
| 590 | state->spi[busnum][cs].emul = NULL; |
| 591 | } |
| 592 | |
| 593 | static int sandbox_sf_bind_bus_cs(struct sandbox_state *state, int busnum, |
| 594 | int cs, const char *spec) |
| 595 | { |
| 596 | struct udevice *bus, *slave; |
| 597 | int ret; |
| 598 | |
| 599 | ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, true, &bus); |
| 600 | if (ret) { |
| 601 | printf("Invalid bus %d for spec '%s' (err=%d)\n", busnum, |
| 602 | spec, ret); |
| 603 | return ret; |
| 604 | } |
| 605 | ret = device_find_child_by_seq(bus, cs, true, &slave); |
| 606 | if (!ret) { |
| 607 | printf("Chip select %d already exists for spec '%s'\n", cs, |
| 608 | spec); |
| 609 | return -EEXIST; |
| 610 | } |
| 611 | |
| 612 | ret = spi_bind_device(bus, cs, "spi_flash_std", spec, &slave); |
| 613 | if (ret) |
| 614 | return ret; |
| 615 | |
| 616 | return sandbox_sf_bind_emul(state, busnum, cs, bus, -1, spec); |
| 617 | } |
| 618 | |
| 619 | int sandbox_spi_get_emul(struct sandbox_state *state, |
| 620 | struct udevice *bus, struct udevice *slave, |
| 621 | struct udevice **emulp) |
| 622 | { |
| 623 | struct sandbox_spi_info *info; |
| 624 | int busnum = bus->seq; |
| 625 | int cs = spi_chip_select(slave); |
| 626 | int ret; |
| 627 | |
| 628 | info = &state->spi[busnum][cs]; |
| 629 | if (!info->emul) { |
| 630 | /* Use the same device tree node as the SPI flash device */ |
| 631 | debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ", |
| 632 | __func__, busnum, cs); |
| 633 | ret = sandbox_sf_bind_emul(state, busnum, cs, bus, |
| 634 | slave->of_offset, slave->name); |
| 635 | if (ret) { |
| 636 | debug("failed (err=%d)\n", ret); |
| 637 | return ret; |
| 638 | } |
| 639 | debug("OK\n"); |
| 640 | } |
| 641 | *emulp = info->emul; |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | int dm_scan_other(bool pre_reloc_only) |
| 647 | { |
| 648 | struct sandbox_state *state = state_get_current(); |
| 649 | int busnum, cs; |
| 650 | |
| 651 | if (pre_reloc_only) |
| 652 | return 0; |
| 653 | for (busnum = 0; busnum < CONFIG_SANDBOX_SPI_MAX_BUS; busnum++) { |
| 654 | for (cs = 0; cs < CONFIG_SANDBOX_SPI_MAX_CS; cs++) { |
| 655 | const char *spec = state->spi[busnum][cs].spec; |
| 656 | int ret; |
| 657 | |
| 658 | if (spec) { |
| 659 | ret = sandbox_sf_bind_bus_cs(state, busnum, |
| 660 | cs, spec); |
| 661 | if (ret) { |
| 662 | debug("%s: Bind failed for bus %d, cs %d\n", |
| 663 | __func__, busnum, cs); |
| 664 | return ret; |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | #endif |
| 673 | |
| 674 | static const struct udevice_id sandbox_sf_ids[] = { |
| 675 | { .compatible = "sandbox,spi-flash" }, |
| 676 | { } |
| 677 | }; |
| 678 | |
| 679 | U_BOOT_DRIVER(sandbox_sf_emul) = { |
| 680 | .name = "sandbox_sf_emul", |
| 681 | .id = UCLASS_SPI_EMUL, |
| 682 | .of_match = sandbox_sf_ids, |
| 683 | .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata, |
| 684 | .probe = sandbox_sf_probe, |
| 685 | .remove = sandbox_sf_remove, |
| 686 | .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash), |
| 687 | .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data), |
| 688 | .ops = &sandbox_sf_emul_ops, |
| 689 | }; |