sandbox.c 17 KB

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