sandbox.c 17 KB

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