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