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