sandbox.c 17 KB

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