sandbox.c 15 KB

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