sandbox.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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->nr_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 = ((((sbsf->data)->id[0]) << 16) |
  317. (((sbsf->data)->id[1]) << 8 |
  318. ((sbsf->data)->id[2]))) >>
  319. (8 * (IDCODE_LEN - 1 - sbsf->off));
  320. } else {
  321. id = 0;
  322. }
  323. debug("%d %02x\n", sbsf->off, id);
  324. tx[pos++] = id;
  325. ++sbsf->off;
  326. break;
  327. }
  328. case SF_ADDR:
  329. debug(" addr: bytes:%u rx:%02x ", sbsf->addr_bytes,
  330. rx[pos]);
  331. if (sbsf->addr_bytes++ < SF_ADDR_LEN)
  332. sbsf->off = (sbsf->off << 8) | rx[pos];
  333. debug("addr:%06x\n", sbsf->off);
  334. if (tx)
  335. sandbox_spi_tristate(&tx[pos], 1);
  336. pos++;
  337. /* See if we're done processing */
  338. if (sbsf->addr_bytes <
  339. SF_ADDR_LEN + sbsf->pad_addr_bytes)
  340. break;
  341. /* Next state! */
  342. if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
  343. puts("sandbox_sf: os_lseek() failed");
  344. return -EIO;
  345. }
  346. switch (sbsf->cmd) {
  347. case CMD_READ_ARRAY_FAST:
  348. case CMD_READ_ARRAY_SLOW:
  349. sbsf->state = SF_READ;
  350. break;
  351. case CMD_PAGE_PROGRAM:
  352. sbsf->state = SF_WRITE;
  353. break;
  354. default:
  355. /* assume erase state ... */
  356. sbsf->state = SF_ERASE;
  357. goto case_sf_erase;
  358. }
  359. debug(" cmd: transition to %s state\n",
  360. sandbox_sf_state_name(sbsf->state));
  361. break;
  362. case SF_READ:
  363. /*
  364. * XXX: need to handle exotic behavior:
  365. * - reading past end of device
  366. */
  367. cnt = bytes - pos;
  368. debug(" tx: read(%u)\n", cnt);
  369. assert(tx);
  370. ret = os_read(sbsf->fd, tx + pos, cnt);
  371. if (ret < 0) {
  372. puts("sandbox_sf: os_read() failed\n");
  373. return -EIO;
  374. }
  375. pos += ret;
  376. break;
  377. case SF_READ_STATUS:
  378. debug(" read status: %#x\n", sbsf->status);
  379. cnt = bytes - pos;
  380. memset(tx + pos, sbsf->status, cnt);
  381. pos += cnt;
  382. break;
  383. case SF_READ_STATUS1:
  384. debug(" read status: %#x\n", sbsf->status);
  385. cnt = bytes - pos;
  386. memset(tx + pos, sbsf->status >> 8, cnt);
  387. pos += cnt;
  388. break;
  389. case SF_WRITE_STATUS:
  390. debug(" write status: %#x (ignored)\n", rx[pos]);
  391. pos = bytes;
  392. break;
  393. case SF_WRITE:
  394. /*
  395. * XXX: need to handle exotic behavior:
  396. * - unaligned addresses
  397. * - more than a page (256) worth of data
  398. * - reading past end of device
  399. */
  400. if (!(sbsf->status & STAT_WEL)) {
  401. puts("sandbox_sf: write enable not set before write\n");
  402. goto done;
  403. }
  404. cnt = bytes - pos;
  405. debug(" rx: write(%u)\n", cnt);
  406. if (tx)
  407. sandbox_spi_tristate(&tx[pos], cnt);
  408. ret = os_write(sbsf->fd, rx + pos, cnt);
  409. if (ret < 0) {
  410. puts("sandbox_spi: os_write() failed\n");
  411. return -EIO;
  412. }
  413. pos += ret;
  414. sbsf->status &= ~STAT_WEL;
  415. break;
  416. case SF_ERASE:
  417. case_sf_erase: {
  418. if (!(sbsf->status & STAT_WEL)) {
  419. puts("sandbox_sf: write enable not set before erase\n");
  420. goto done;
  421. }
  422. /* verify address is aligned */
  423. if (sbsf->off & (sbsf->erase_size - 1)) {
  424. debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
  425. sbsf->cmd, sbsf->erase_size,
  426. sbsf->off);
  427. sbsf->status &= ~STAT_WEL;
  428. goto done;
  429. }
  430. debug(" sector erase addr: %u, size: %u\n", sbsf->off,
  431. sbsf->erase_size);
  432. cnt = bytes - pos;
  433. if (tx)
  434. sandbox_spi_tristate(&tx[pos], cnt);
  435. pos += cnt;
  436. /*
  437. * TODO(vapier@gentoo.org): latch WIP in status, and
  438. * delay before clearing it ?
  439. */
  440. ret = sandbox_erase_part(sbsf, sbsf->erase_size);
  441. sbsf->status &= ~STAT_WEL;
  442. if (ret) {
  443. debug("sandbox_sf: Erase failed\n");
  444. goto done;
  445. }
  446. goto done;
  447. }
  448. default:
  449. debug(" ??? no idea what to do ???\n");
  450. goto done;
  451. }
  452. }
  453. done:
  454. if (flags & SPI_XFER_END)
  455. sandbox_sf_cs_deactivate(dev);
  456. return pos == bytes ? 0 : -EIO;
  457. }
  458. int sandbox_sf_ofdata_to_platdata(struct udevice *dev)
  459. {
  460. struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
  461. const void *blob = gd->fdt_blob;
  462. int node = dev->of_offset;
  463. pdata->filename = fdt_getprop(blob, node, "sandbox,filename", NULL);
  464. pdata->device_name = fdt_getprop(blob, node, "compatible", NULL);
  465. if (!pdata->filename || !pdata->device_name) {
  466. debug("%s: Missing properties, filename=%s, device_name=%s\n",
  467. __func__, pdata->filename, pdata->device_name);
  468. return -EINVAL;
  469. }
  470. return 0;
  471. }
  472. static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
  473. .xfer = sandbox_sf_xfer,
  474. };
  475. #ifdef CONFIG_SPI_FLASH
  476. static int sandbox_cmdline_cb_spi_sf(struct sandbox_state *state,
  477. const char *arg)
  478. {
  479. unsigned long bus, cs;
  480. const char *spec = sandbox_spi_parse_spec(arg, &bus, &cs);
  481. if (!spec)
  482. return 1;
  483. /*
  484. * It is safe to not make a copy of 'spec' because it comes from the
  485. * command line.
  486. *
  487. * TODO(sjg@chromium.org): It would be nice if we could parse the
  488. * spec here, but the problem is that no U-Boot init has been done
  489. * yet. Perhaps we can figure something out.
  490. */
  491. state->spi[bus][cs].spec = spec;
  492. debug("%s: Setting up spec '%s' for bus %ld, cs %ld\n", __func__,
  493. spec, bus, cs);
  494. return 0;
  495. }
  496. SANDBOX_CMDLINE_OPT(spi_sf, 1, "connect a SPI flash: <bus>:<cs>:<id>:<file>");
  497. int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
  498. struct udevice *bus, int of_offset, const char *spec)
  499. {
  500. struct udevice *emul;
  501. char name[20], *str;
  502. struct driver *drv;
  503. int ret;
  504. /* now the emulator */
  505. strncpy(name, spec, sizeof(name) - 6);
  506. name[sizeof(name) - 6] = '\0';
  507. strcat(name, "-emul");
  508. str = strdup(name);
  509. if (!str)
  510. return -ENOMEM;
  511. drv = lists_driver_lookup_name("sandbox_sf_emul");
  512. if (!drv) {
  513. puts("Cannot find sandbox_sf_emul driver\n");
  514. return -ENOENT;
  515. }
  516. ret = device_bind(bus, drv, str, NULL, of_offset, &emul);
  517. if (ret) {
  518. printf("Cannot create emul device for spec '%s' (err=%d)\n",
  519. spec, ret);
  520. return ret;
  521. }
  522. state->spi[busnum][cs].emul = emul;
  523. return 0;
  524. }
  525. void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
  526. {
  527. struct udevice *dev;
  528. dev = state->spi[busnum][cs].emul;
  529. device_remove(dev);
  530. device_unbind(dev);
  531. state->spi[busnum][cs].emul = NULL;
  532. }
  533. static int sandbox_sf_bind_bus_cs(struct sandbox_state *state, int busnum,
  534. int cs, const char *spec)
  535. {
  536. struct udevice *bus, *slave;
  537. int ret;
  538. ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, true, &bus);
  539. if (ret) {
  540. printf("Invalid bus %d for spec '%s' (err=%d)\n", busnum,
  541. spec, ret);
  542. return ret;
  543. }
  544. ret = spi_find_chip_select(bus, cs, &slave);
  545. if (!ret) {
  546. printf("Chip select %d already exists for spec '%s'\n", cs,
  547. spec);
  548. return -EEXIST;
  549. }
  550. ret = device_bind_driver(bus, "spi_flash_std", spec, &slave);
  551. if (ret)
  552. return ret;
  553. return sandbox_sf_bind_emul(state, busnum, cs, bus, -1, spec);
  554. }
  555. int sandbox_spi_get_emul(struct sandbox_state *state,
  556. struct udevice *bus, struct udevice *slave,
  557. struct udevice **emulp)
  558. {
  559. struct sandbox_spi_info *info;
  560. int busnum = bus->seq;
  561. int cs = spi_chip_select(slave);
  562. int ret;
  563. info = &state->spi[busnum][cs];
  564. if (!info->emul) {
  565. /* Use the same device tree node as the SPI flash device */
  566. debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
  567. __func__, busnum, cs);
  568. ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
  569. slave->of_offset, slave->name);
  570. if (ret) {
  571. debug("failed (err=%d)\n", ret);
  572. return ret;
  573. }
  574. debug("OK\n");
  575. }
  576. *emulp = info->emul;
  577. return 0;
  578. }
  579. int dm_scan_other(bool pre_reloc_only)
  580. {
  581. struct sandbox_state *state = state_get_current();
  582. int busnum, cs;
  583. if (pre_reloc_only)
  584. return 0;
  585. for (busnum = 0; busnum < CONFIG_SANDBOX_SPI_MAX_BUS; busnum++) {
  586. for (cs = 0; cs < CONFIG_SANDBOX_SPI_MAX_CS; cs++) {
  587. const char *spec = state->spi[busnum][cs].spec;
  588. int ret;
  589. if (spec) {
  590. ret = sandbox_sf_bind_bus_cs(state, busnum,
  591. cs, spec);
  592. if (ret) {
  593. debug("%s: Bind failed for bus %d, cs %d\n",
  594. __func__, busnum, cs);
  595. return ret;
  596. }
  597. debug("%s: Setting up spec '%s' for bus %d, cs %d\n",
  598. __func__, spec, busnum, cs);
  599. }
  600. }
  601. }
  602. return 0;
  603. }
  604. #endif
  605. static const struct udevice_id sandbox_sf_ids[] = {
  606. { .compatible = "sandbox,spi-flash" },
  607. { }
  608. };
  609. U_BOOT_DRIVER(sandbox_sf_emul) = {
  610. .name = "sandbox_sf_emul",
  611. .id = UCLASS_SPI_EMUL,
  612. .of_match = sandbox_sf_ids,
  613. .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata,
  614. .probe = sandbox_sf_probe,
  615. .remove = sandbox_sf_remove,
  616. .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash),
  617. .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data),
  618. .ops = &sandbox_sf_emul_ops,
  619. };