sandbox.c 15 KB

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