sandbox.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 <malloc.h>
  12. #include <spi.h>
  13. #include <os.h>
  14. #include <spi_flash.h>
  15. #include "sf_internal.h"
  16. #include <asm/getopt.h>
  17. #include <asm/spi.h>
  18. #include <asm/state.h>
  19. /*
  20. * The different states that our SPI flash transitions between.
  21. * We need to keep track of this across multiple xfer calls since
  22. * the SPI bus could possibly call down into us multiple times.
  23. */
  24. enum sandbox_sf_state {
  25. SF_CMD, /* default state -- we're awaiting a command */
  26. SF_ID, /* read the flash's (jedec) ID code */
  27. SF_ADDR, /* processing the offset in the flash to read/etc... */
  28. SF_READ, /* reading data from the flash */
  29. SF_WRITE, /* writing data to the flash, i.e. page programming */
  30. SF_ERASE, /* erase the flash */
  31. SF_READ_STATUS, /* read the flash's status register */
  32. SF_READ_STATUS1, /* read the flash's status register upper 8 bits*/
  33. };
  34. static const char *sandbox_sf_state_name(enum sandbox_sf_state state)
  35. {
  36. static const char * const states[] = {
  37. "CMD", "ID", "ADDR", "READ", "WRITE", "ERASE", "READ_STATUS",
  38. };
  39. return states[state];
  40. }
  41. /* Bits for the status register */
  42. #define STAT_WIP (1 << 0)
  43. #define STAT_WEL (1 << 1)
  44. /* Assume all SPI flashes have 3 byte addresses since they do atm */
  45. #define SF_ADDR_LEN 3
  46. struct sandbox_spi_flash_erase_commands {
  47. u8 cmd;
  48. u32 size;
  49. };
  50. #define IDCODE_LEN 5
  51. #define MAX_ERASE_CMDS 3
  52. struct sandbox_spi_flash_data {
  53. const char *name;
  54. u8 idcode[IDCODE_LEN];
  55. u32 size;
  56. const struct sandbox_spi_flash_erase_commands
  57. erase_cmds[MAX_ERASE_CMDS];
  58. };
  59. /* Structure describing all the flashes we know how to emulate */
  60. static const struct sandbox_spi_flash_data sandbox_sf_flashes[] = {
  61. {
  62. "M25P16", { 0x20, 0x20, 0x15 }, (2 << 20),
  63. { /* erase commands */
  64. { 0xd8, (64 << 10), }, /* sector */
  65. { 0xc7, (2 << 20), }, /* bulk */
  66. },
  67. },
  68. {
  69. "W25Q32", { 0xef, 0x40, 0x16 }, (4 << 20),
  70. { /* erase commands */
  71. { 0x20, (4 << 10), }, /* 4KB */
  72. { 0xd8, (64 << 10), }, /* sector */
  73. { 0xc7, (4 << 20), }, /* bulk */
  74. },
  75. },
  76. {
  77. "W25Q128", { 0xef, 0x40, 0x18 }, (16 << 20),
  78. { /* erase commands */
  79. { 0x20, (4 << 10), }, /* 4KB */
  80. { 0xd8, (64 << 10), }, /* sector */
  81. { 0xc7, (16 << 20), }, /* bulk */
  82. },
  83. },
  84. };
  85. /* Used to quickly bulk erase backing store */
  86. static u8 sandbox_sf_0xff[0x1000];
  87. /* Internal state data for each SPI flash */
  88. struct sandbox_spi_flash {
  89. /*
  90. * As we receive data over the SPI bus, our flash transitions
  91. * between states. For example, we start off in the SF_CMD
  92. * state where the first byte tells us what operation to perform
  93. * (such as read or write the flash). But the operation itself
  94. * can go through a few states such as first reading in the
  95. * offset in the flash to perform the requested operation.
  96. * Thus "state" stores the exact state that our machine is in
  97. * while "cmd" stores the overall command we're processing.
  98. */
  99. enum sandbox_sf_state state;
  100. uint cmd;
  101. const void *cmd_data;
  102. /* Current position in the flash; used when reading/writing/etc... */
  103. uint off;
  104. /* How many address bytes we've consumed */
  105. uint addr_bytes, pad_addr_bytes;
  106. /* The current flash status (see STAT_XXX defines above) */
  107. u16 status;
  108. /* Data describing the flash we're emulating */
  109. const struct sandbox_spi_flash_data *data;
  110. /* The file on disk to serv up data from */
  111. int fd;
  112. };
  113. static int sandbox_sf_setup(void **priv, const char *spec)
  114. {
  115. /* spec = idcode:file */
  116. struct sandbox_spi_flash *sbsf;
  117. const char *file;
  118. size_t i, len, idname_len;
  119. const struct sandbox_spi_flash_data *data;
  120. file = strchr(spec, ':');
  121. if (!file) {
  122. printf("sandbox_sf: unable to parse file\n");
  123. goto error;
  124. }
  125. idname_len = file - spec;
  126. ++file;
  127. for (i = 0; i < ARRAY_SIZE(sandbox_sf_flashes); ++i) {
  128. data = &sandbox_sf_flashes[i];
  129. len = strlen(data->name);
  130. if (idname_len != len)
  131. continue;
  132. if (!memcmp(spec, data->name, len))
  133. break;
  134. }
  135. if (i == ARRAY_SIZE(sandbox_sf_flashes)) {
  136. printf("sandbox_sf: unknown flash '%*s'\n", (int)idname_len,
  137. spec);
  138. goto error;
  139. }
  140. if (sandbox_sf_0xff[0] == 0x00)
  141. memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
  142. sbsf = calloc(sizeof(*sbsf), 1);
  143. if (!sbsf) {
  144. printf("sandbox_sf: out of memory\n");
  145. goto error;
  146. }
  147. sbsf->fd = os_open(file, 02);
  148. if (sbsf->fd == -1) {
  149. free(sbsf);
  150. printf("sandbox_sf: unable to open file '%s'\n", file);
  151. goto error;
  152. }
  153. sbsf->data = data;
  154. *priv = sbsf;
  155. return 0;
  156. error:
  157. return 1;
  158. }
  159. static void sandbox_sf_free(void *priv)
  160. {
  161. struct sandbox_spi_flash *sbsf = priv;
  162. os_close(sbsf->fd);
  163. free(sbsf);
  164. }
  165. static void sandbox_sf_cs_activate(void *priv)
  166. {
  167. struct sandbox_spi_flash *sbsf = priv;
  168. debug("sandbox_sf: CS activated; state is fresh!\n");
  169. /* CS is asserted, so reset state */
  170. sbsf->off = 0;
  171. sbsf->addr_bytes = 0;
  172. sbsf->pad_addr_bytes = 0;
  173. sbsf->state = SF_CMD;
  174. sbsf->cmd = SF_CMD;
  175. }
  176. static void sandbox_sf_cs_deactivate(void *priv)
  177. {
  178. debug("sandbox_sf: CS deactivated; cmd done processing!\n");
  179. }
  180. /* Figure out what command this stream is telling us to do */
  181. static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
  182. u8 *tx)
  183. {
  184. enum sandbox_sf_state oldstate = sbsf->state;
  185. /* We need to output a byte for the cmd byte we just ate */
  186. sandbox_spi_tristate(tx, 1);
  187. sbsf->cmd = rx[0];
  188. switch (sbsf->cmd) {
  189. case CMD_READ_ID:
  190. sbsf->state = SF_ID;
  191. sbsf->cmd = SF_ID;
  192. break;
  193. case CMD_READ_ARRAY_FAST:
  194. sbsf->pad_addr_bytes = 1;
  195. case CMD_READ_ARRAY_SLOW:
  196. case CMD_PAGE_PROGRAM:
  197. state_addr:
  198. sbsf->state = SF_ADDR;
  199. break;
  200. case CMD_WRITE_DISABLE:
  201. debug(" write disabled\n");
  202. sbsf->status &= ~STAT_WEL;
  203. break;
  204. case CMD_READ_STATUS:
  205. sbsf->state = SF_READ_STATUS;
  206. break;
  207. case CMD_READ_STATUS1:
  208. sbsf->state = SF_READ_STATUS1;
  209. break;
  210. case CMD_WRITE_ENABLE:
  211. debug(" write enabled\n");
  212. sbsf->status |= STAT_WEL;
  213. break;
  214. default: {
  215. size_t i;
  216. /* handle erase commands first */
  217. for (i = 0; i < MAX_ERASE_CMDS; ++i) {
  218. const struct sandbox_spi_flash_erase_commands *
  219. erase_cmd = &sbsf->data->erase_cmds[i];
  220. if (erase_cmd->cmd == 0x00)
  221. continue;
  222. if (sbsf->cmd != erase_cmd->cmd)
  223. continue;
  224. sbsf->cmd_data = erase_cmd;
  225. goto state_addr;
  226. }
  227. debug(" cmd unknown: %#x\n", sbsf->cmd);
  228. return 1;
  229. }
  230. }
  231. if (oldstate != sbsf->state)
  232. debug(" cmd: transition to %s state\n",
  233. sandbox_sf_state_name(sbsf->state));
  234. return 0;
  235. }
  236. int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
  237. {
  238. int todo;
  239. int ret;
  240. while (size > 0) {
  241. todo = min(size, sizeof(sandbox_sf_0xff));
  242. ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
  243. if (ret != todo)
  244. return ret;
  245. size -= todo;
  246. }
  247. return 0;
  248. }
  249. static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
  250. uint bytes)
  251. {
  252. struct sandbox_spi_flash *sbsf = priv;
  253. uint cnt, pos = 0;
  254. int ret;
  255. debug("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
  256. sandbox_sf_state_name(sbsf->state), bytes);
  257. if (sbsf->state == SF_CMD) {
  258. /* Figure out the initial state */
  259. if (sandbox_sf_process_cmd(sbsf, rx, tx))
  260. return 1;
  261. ++pos;
  262. }
  263. /* Process the remaining data */
  264. while (pos < bytes) {
  265. switch (sbsf->state) {
  266. case SF_ID: {
  267. u8 id;
  268. debug(" id: off:%u tx:", sbsf->off);
  269. if (sbsf->off < IDCODE_LEN)
  270. id = sbsf->data->idcode[sbsf->off];
  271. else
  272. id = 0;
  273. debug("%02x\n", id);
  274. tx[pos++] = id;
  275. ++sbsf->off;
  276. break;
  277. }
  278. case SF_ADDR:
  279. debug(" addr: bytes:%u rx:%02x ", sbsf->addr_bytes,
  280. rx[pos]);
  281. if (sbsf->addr_bytes++ < SF_ADDR_LEN)
  282. sbsf->off = (sbsf->off << 8) | rx[pos];
  283. debug("addr:%06x\n", sbsf->off);
  284. sandbox_spi_tristate(&tx[pos++], 1);
  285. /* See if we're done processing */
  286. if (sbsf->addr_bytes <
  287. SF_ADDR_LEN + sbsf->pad_addr_bytes)
  288. break;
  289. /* Next state! */
  290. if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
  291. puts("sandbox_sf: os_lseek() failed");
  292. return 1;
  293. }
  294. switch (sbsf->cmd) {
  295. case CMD_READ_ARRAY_FAST:
  296. case CMD_READ_ARRAY_SLOW:
  297. sbsf->state = SF_READ;
  298. break;
  299. case CMD_PAGE_PROGRAM:
  300. sbsf->state = SF_WRITE;
  301. break;
  302. default:
  303. /* assume erase state ... */
  304. sbsf->state = SF_ERASE;
  305. goto case_sf_erase;
  306. }
  307. debug(" cmd: transition to %s state\n",
  308. sandbox_sf_state_name(sbsf->state));
  309. break;
  310. case SF_READ:
  311. /*
  312. * XXX: need to handle exotic behavior:
  313. * - reading past end of device
  314. */
  315. cnt = bytes - pos;
  316. debug(" tx: read(%u)\n", cnt);
  317. ret = os_read(sbsf->fd, tx + pos, cnt);
  318. if (ret < 0) {
  319. puts("sandbox_spi: os_read() failed\n");
  320. return 1;
  321. }
  322. pos += ret;
  323. break;
  324. case SF_READ_STATUS:
  325. debug(" read status: %#x\n", sbsf->status);
  326. cnt = bytes - pos;
  327. memset(tx + pos, sbsf->status, cnt);
  328. pos += cnt;
  329. break;
  330. case SF_READ_STATUS1:
  331. debug(" read status: %#x\n", sbsf->status);
  332. cnt = bytes - pos;
  333. memset(tx + pos, sbsf->status >> 8, cnt);
  334. pos += cnt;
  335. break;
  336. case SF_WRITE:
  337. /*
  338. * XXX: need to handle exotic behavior:
  339. * - unaligned addresses
  340. * - more than a page (256) worth of data
  341. * - reading past end of device
  342. */
  343. if (!(sbsf->status & STAT_WEL)) {
  344. puts("sandbox_sf: write enable not set before write\n");
  345. goto done;
  346. }
  347. cnt = bytes - pos;
  348. debug(" rx: write(%u)\n", cnt);
  349. sandbox_spi_tristate(&tx[pos], cnt);
  350. ret = os_write(sbsf->fd, rx + pos, cnt);
  351. if (ret < 0) {
  352. puts("sandbox_spi: os_write() failed\n");
  353. return 1;
  354. }
  355. pos += ret;
  356. sbsf->status &= ~STAT_WEL;
  357. break;
  358. case SF_ERASE:
  359. case_sf_erase: {
  360. const struct sandbox_spi_flash_erase_commands *
  361. erase_cmd = sbsf->cmd_data;
  362. if (!(sbsf->status & STAT_WEL)) {
  363. puts("sandbox_sf: write enable not set before erase\n");
  364. goto done;
  365. }
  366. /* verify address is aligned */
  367. if (sbsf->off & (erase_cmd->size - 1)) {
  368. debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
  369. erase_cmd->cmd, erase_cmd->size,
  370. sbsf->off);
  371. sbsf->status &= ~STAT_WEL;
  372. goto done;
  373. }
  374. debug(" sector erase addr: %u\n", sbsf->off);
  375. cnt = bytes - pos;
  376. sandbox_spi_tristate(&tx[pos], cnt);
  377. pos += cnt;
  378. /*
  379. * TODO(vapier@gentoo.org): latch WIP in status, and
  380. * delay before clearing it ?
  381. */
  382. ret = sandbox_erase_part(sbsf, erase_cmd->size);
  383. sbsf->status &= ~STAT_WEL;
  384. if (ret) {
  385. debug("sandbox_sf: Erase failed\n");
  386. goto done;
  387. }
  388. goto done;
  389. }
  390. default:
  391. debug(" ??? no idea what to do ???\n");
  392. goto done;
  393. }
  394. }
  395. done:
  396. return pos == bytes ? 0 : 1;
  397. }
  398. static const struct sandbox_spi_emu_ops sandbox_sf_ops = {
  399. .setup = sandbox_sf_setup,
  400. .free = sandbox_sf_free,
  401. .cs_activate = sandbox_sf_cs_activate,
  402. .cs_deactivate = sandbox_sf_cs_deactivate,
  403. .xfer = sandbox_sf_xfer,
  404. };
  405. static int sandbox_cmdline_cb_spi_sf(struct sandbox_state *state,
  406. const char *arg)
  407. {
  408. unsigned long bus, cs;
  409. const char *spec = sandbox_spi_parse_spec(arg, &bus, &cs);
  410. if (!spec)
  411. return 1;
  412. /*
  413. * It is safe to not make a copy of 'spec' because it comes from the
  414. * command line.
  415. *
  416. * TODO(sjg@chromium.org): It would be nice if we could parse the
  417. * spec here, but the problem is that no U-Boot init has been done
  418. * yet. Perhaps we can figure something out.
  419. */
  420. state->spi[bus][cs].ops = &sandbox_sf_ops;
  421. state->spi[bus][cs].spec = spec;
  422. return 0;
  423. }
  424. SANDBOX_CMDLINE_OPT(spi_sf, 1, "connect a SPI flash: <bus>:<cs>:<id>:<file>");