sandbox.c 15 KB

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