sandbox.c 11 KB

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