4 * Copyright (c) 2011-2013 The Chromium OS Authors.
5 * See file CREDITS for list of people who contributed to this
8 * Licensed under the GPL-2 or later.
16 #include <spi_flash.h>
17 #include "sf_internal.h"
19 #include <asm/getopt.h>
21 #include <asm/state.h>
24 * The different states that our SPI flash transitions between.
25 * We need to keep track of this across multiple xfer calls since
26 * the SPI bus could possibly call down into us multiple times.
28 enum sandbox_sf_state {
29 SF_CMD, /* default state -- we're awaiting a command */
30 SF_ID, /* read the flash's (jedec) ID code */
31 SF_ADDR, /* processing the offset in the flash to read/etc... */
32 SF_READ, /* reading data from the flash */
33 SF_WRITE, /* writing data to the flash, i.e. page programming */
34 SF_ERASE, /* erase the flash */
35 SF_READ_STATUS, /* read the flash's status register */
36 SF_READ_STATUS1, /* read the flash's status register upper 8 bits*/
39 static const char *sandbox_sf_state_name(enum sandbox_sf_state state)
41 static const char * const states[] = {
42 "CMD", "ID", "ADDR", "READ", "WRITE", "ERASE", "READ_STATUS",
47 /* Bits for the status register */
48 #define STAT_WIP (1 << 0)
49 #define STAT_WEL (1 << 1)
51 /* Assume all SPI flashes have 3 byte addresses since they do atm */
54 struct sandbox_spi_flash_erase_commands {
59 #define MAX_ERASE_CMDS 3
60 struct sandbox_spi_flash_data {
62 u8 idcode[IDCODE_LEN];
64 const struct sandbox_spi_flash_erase_commands
65 erase_cmds[MAX_ERASE_CMDS];
68 /* Structure describing all the flashes we know how to emulate */
69 static const struct sandbox_spi_flash_data sandbox_sf_flashes[] = {
71 "M25P16", { 0x20, 0x20, 0x15 }, (2 << 20),
72 { /* erase commands */
73 { 0xd8, (64 << 10), }, /* sector */
74 { 0xc7, (2 << 20), }, /* bulk */
78 "W25Q32", { 0xef, 0x40, 0x16 }, (4 << 20),
79 { /* erase commands */
80 { 0x20, (4 << 10), }, /* 4KB */
81 { 0xd8, (64 << 10), }, /* sector */
82 { 0xc7, (4 << 20), }, /* bulk */
86 "W25Q128", { 0xef, 0x40, 0x18 }, (16 << 20),
87 { /* erase commands */
88 { 0x20, (4 << 10), }, /* 4KB */
89 { 0xd8, (64 << 10), }, /* sector */
90 { 0xc7, (16 << 20), }, /* bulk */
95 /* Used to quickly bulk erase backing store */
96 static u8 sandbox_sf_0xff[0x1000];
98 /* Internal state data for each SPI flash */
99 struct sandbox_spi_flash {
101 * As we receive data over the SPI bus, our flash transitions
102 * between states. For example, we start off in the SF_CMD
103 * state where the first byte tells us what operation to perform
104 * (such as read or write the flash). But the operation itself
105 * can go through a few states such as first reading in the
106 * offset in the flash to perform the requested operation.
107 * Thus "state" stores the exact state that our machine is in
108 * while "cmd" stores the overall command we're processing.
110 enum sandbox_sf_state state;
112 const void *cmd_data;
113 /* Current position in the flash; used when reading/writing/etc... */
115 /* How many address bytes we've consumed */
116 uint addr_bytes, pad_addr_bytes;
117 /* The current flash status (see STAT_XXX defines above) */
119 /* Data describing the flash we're emulating */
120 const struct sandbox_spi_flash_data *data;
121 /* The file on disk to serv up data from */
125 static int sandbox_sf_setup(void **priv, const char *spec)
127 /* spec = idcode:file */
128 struct sandbox_spi_flash *sbsf;
130 size_t i, len, idname_len;
131 const struct sandbox_spi_flash_data *data;
133 file = strchr(spec, ':');
135 printf("sandbox_sf: unable to parse file\n");
138 idname_len = file - spec;
141 for (i = 0; i < ARRAY_SIZE(sandbox_sf_flashes); ++i) {
142 data = &sandbox_sf_flashes[i];
143 len = strlen(data->name);
144 if (idname_len != len)
146 if (!memcmp(spec, data->name, len))
149 if (i == ARRAY_SIZE(sandbox_sf_flashes)) {
150 printf("sandbox_sf: unknown flash '%*s'\n", (int)idname_len,
155 if (sandbox_sf_0xff[0] == 0x00)
156 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
158 sbsf = calloc(sizeof(*sbsf), 1);
160 printf("sandbox_sf: out of memory\n");
164 sbsf->fd = os_open(file, 02);
165 if (sbsf->fd == -1) {
167 printf("sandbox_sf: unable to open file '%s'\n", file);
180 static void sandbox_sf_free(void *priv)
182 struct sandbox_spi_flash *sbsf = priv;
188 static void sandbox_sf_cs_activate(void *priv)
190 struct sandbox_spi_flash *sbsf = priv;
192 debug("sandbox_sf: CS activated; state is fresh!\n");
194 /* CS is asserted, so reset state */
196 sbsf->addr_bytes = 0;
197 sbsf->pad_addr_bytes = 0;
198 sbsf->state = SF_CMD;
202 static void sandbox_sf_cs_deactivate(void *priv)
204 debug("sandbox_sf: CS deactivated; cmd done processing!\n");
207 /* Figure out what command this stream is telling us to do */
208 static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
211 enum sandbox_sf_state oldstate = sbsf->state;
213 /* We need to output a byte for the cmd byte we just ate */
214 sandbox_spi_tristate(tx, 1);
222 case CMD_READ_ARRAY_FAST:
223 sbsf->pad_addr_bytes = 1;
224 case CMD_READ_ARRAY_SLOW:
225 case CMD_PAGE_PROGRAM:
227 sbsf->state = SF_ADDR;
229 case CMD_WRITE_DISABLE:
230 debug(" write disabled\n");
231 sbsf->status &= ~STAT_WEL;
233 case CMD_READ_STATUS:
234 sbsf->state = SF_READ_STATUS;
236 case CMD_READ_STATUS1:
237 sbsf->state = SF_READ_STATUS1;
239 case CMD_WRITE_ENABLE:
240 debug(" write enabled\n");
241 sbsf->status |= STAT_WEL;
246 /* handle erase commands first */
247 for (i = 0; i < MAX_ERASE_CMDS; ++i) {
248 const struct sandbox_spi_flash_erase_commands *
249 erase_cmd = &sbsf->data->erase_cmds[i];
251 if (erase_cmd->cmd == 0x00)
253 if (sbsf->cmd != erase_cmd->cmd)
256 sbsf->cmd_data = erase_cmd;
260 debug(" cmd unknown: %#x\n", sbsf->cmd);
265 if (oldstate != sbsf->state)
266 debug(" cmd: transition to %s state\n",
267 sandbox_sf_state_name(sbsf->state));
272 int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
278 todo = min(size, sizeof(sandbox_sf_0xff));
279 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
288 static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
291 struct sandbox_spi_flash *sbsf = priv;
295 debug("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
296 sandbox_sf_state_name(sbsf->state), bytes);
298 if (sbsf->state == SF_CMD) {
299 /* Figure out the initial state */
300 if (sandbox_sf_process_cmd(sbsf, rx, tx))
305 /* Process the remaining data */
306 while (pos < bytes) {
307 switch (sbsf->state) {
311 debug(" id: off:%u tx:", sbsf->off);
312 if (sbsf->off < IDCODE_LEN)
313 id = sbsf->data->idcode[sbsf->off];
322 debug(" addr: bytes:%u rx:%02x ", sbsf->addr_bytes,
325 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
326 sbsf->off = (sbsf->off << 8) | rx[pos];
327 debug("addr:%06x\n", sbsf->off);
329 sandbox_spi_tristate(&tx[pos++], 1);
331 /* See if we're done processing */
332 if (sbsf->addr_bytes <
333 SF_ADDR_LEN + sbsf->pad_addr_bytes)
337 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
338 puts("sandbox_sf: os_lseek() failed");
342 case CMD_READ_ARRAY_FAST:
343 case CMD_READ_ARRAY_SLOW:
344 sbsf->state = SF_READ;
346 case CMD_PAGE_PROGRAM:
347 sbsf->state = SF_WRITE;
350 /* assume erase state ... */
351 sbsf->state = SF_ERASE;
354 debug(" cmd: transition to %s state\n",
355 sandbox_sf_state_name(sbsf->state));
359 * XXX: need to handle exotic behavior:
360 * - reading past end of device
364 debug(" tx: read(%u)\n", cnt);
365 ret = os_read(sbsf->fd, tx + pos, cnt);
367 puts("sandbox_spi: os_read() failed\n");
373 debug(" read status: %#x\n", sbsf->status);
375 memset(tx + pos, sbsf->status, cnt);
378 case SF_READ_STATUS1:
379 debug(" read status: %#x\n", sbsf->status);
381 memset(tx + pos, sbsf->status >> 8, cnt);
386 * XXX: need to handle exotic behavior:
387 * - unaligned addresses
388 * - more than a page (256) worth of data
389 * - reading past end of device
391 if (!(sbsf->status & STAT_WEL)) {
392 puts("sandbox_sf: write enable not set before write\n");
397 debug(" rx: write(%u)\n", cnt);
398 sandbox_spi_tristate(&tx[pos], cnt);
399 ret = os_write(sbsf->fd, rx + pos, cnt);
401 puts("sandbox_spi: os_write() failed\n");
405 sbsf->status &= ~STAT_WEL;
409 const struct sandbox_spi_flash_erase_commands *
410 erase_cmd = sbsf->cmd_data;
412 if (!(sbsf->status & STAT_WEL)) {
413 puts("sandbox_sf: write enable not set before erase\n");
417 /* verify address is aligned */
418 if (sbsf->off & (erase_cmd->size - 1)) {
419 debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
420 erase_cmd->cmd, erase_cmd->size,
422 sbsf->status &= ~STAT_WEL;
426 debug(" sector erase addr: %u\n", sbsf->off);
429 sandbox_spi_tristate(&tx[pos], cnt);
433 * TODO(vapier@gentoo.org): latch WIP in status, and
434 * delay before clearing it ?
436 ret = sandbox_erase_part(sbsf, erase_cmd->size);
437 sbsf->status &= ~STAT_WEL;
439 debug("sandbox_sf: Erase failed\n");
445 debug(" ??? no idea what to do ???\n");
451 return pos == bytes ? 0 : 1;
454 static const struct sandbox_spi_emu_ops sandbox_sf_ops = {
455 .setup = sandbox_sf_setup,
456 .free = sandbox_sf_free,
457 .cs_activate = sandbox_sf_cs_activate,
458 .cs_deactivate = sandbox_sf_cs_deactivate,
459 .xfer = sandbox_sf_xfer,
462 static int sandbox_cmdline_cb_spi_sf(struct sandbox_state *state,
465 unsigned long bus, cs;
466 const char *spec = sandbox_spi_parse_spec(arg, &bus, &cs);
472 * It is safe to not make a copy of 'spec' because it comes from the
475 * TODO(sjg@chromium.org): It would be nice if we could parse the
476 * spec here, but the problem is that no U-Boot init has been done
477 * yet. Perhaps we can figure something out.
479 state->spi[bus][cs].ops = &sandbox_sf_ops;
480 state->spi[bus][cs].spec = spec;
483 SANDBOX_CMDLINE_OPT(spi_sf, 1, "connect a SPI flash: <bus>:<cs>:<id>:<file>");