sandbox.c 17 KB

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