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 */
56 /* Used to quickly bulk erase backing store */
57 static u8 sandbox_sf_0xff[0x1000];
59 /* Internal state data for each SPI flash */
60 struct sandbox_spi_flash {
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.
71 enum sandbox_sf_state state;
73 /* Erase size of current erase command */
75 /* Current position in the flash; used when reading/writing/etc... */
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) */
81 /* Data describing the flash we're emulating */
82 const struct spi_flash_params *data;
83 /* The file on disk to serv up data from */
87 static int sandbox_sf_setup(void **priv, const char *spec)
89 /* spec = idcode:file */
90 struct sandbox_spi_flash *sbsf;
92 size_t len, idname_len;
93 const struct spi_flash_params *data;
95 file = strchr(spec, ':');
97 printf("sandbox_sf: unable to parse file\n");
100 idname_len = file - spec;
103 for (data = spi_flash_params_table; data->name; data++) {
104 len = strlen(data->name);
105 if (idname_len != len)
107 if (!memcmp(spec, data->name, len))
111 printf("sandbox_sf: unknown flash '%*s'\n", (int)idname_len,
116 if (sandbox_sf_0xff[0] == 0x00)
117 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
119 sbsf = calloc(sizeof(*sbsf), 1);
121 printf("sandbox_sf: out of memory\n");
125 sbsf->fd = os_open(file, 02);
126 if (sbsf->fd == -1) {
128 printf("sandbox_sf: unable to open file '%s'\n", file);
141 static void sandbox_sf_free(void *priv)
143 struct sandbox_spi_flash *sbsf = priv;
149 static void sandbox_sf_cs_activate(void *priv)
151 struct sandbox_spi_flash *sbsf = priv;
153 debug("sandbox_sf: CS activated; state is fresh!\n");
155 /* CS is asserted, so reset state */
157 sbsf->addr_bytes = 0;
158 sbsf->pad_addr_bytes = 0;
159 sbsf->state = SF_CMD;
163 static void sandbox_sf_cs_deactivate(void *priv)
165 debug("sandbox_sf: CS deactivated; cmd done processing!\n");
168 /* Figure out what command this stream is telling us to do */
169 static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
172 enum sandbox_sf_state oldstate = sbsf->state;
174 /* We need to output a byte for the cmd byte we just ate */
175 sandbox_spi_tristate(tx, 1);
183 case CMD_READ_ARRAY_FAST:
184 sbsf->pad_addr_bytes = 1;
185 case CMD_READ_ARRAY_SLOW:
186 case CMD_PAGE_PROGRAM:
187 sbsf->state = SF_ADDR;
189 case CMD_WRITE_DISABLE:
190 debug(" write disabled\n");
191 sbsf->status &= ~STAT_WEL;
193 case CMD_READ_STATUS:
194 sbsf->state = SF_READ_STATUS;
196 case CMD_READ_STATUS1:
197 sbsf->state = SF_READ_STATUS1;
199 case CMD_WRITE_ENABLE:
200 debug(" write enabled\n");
201 sbsf->status |= STAT_WEL;
204 int flags = sbsf->data->flags;
206 /* we only support erase here */
207 if (sbsf->cmd == CMD_ERASE_CHIP) {
208 sbsf->erase_size = sbsf->data->sector_size *
209 sbsf->data->nr_sectors;
210 } else if (sbsf->cmd == CMD_ERASE_4K && (flags & SECT_4K)) {
211 sbsf->erase_size = 4 << 10;
212 } else if (sbsf->cmd == CMD_ERASE_32K && (flags & SECT_32K)) {
213 sbsf->erase_size = 32 << 10;
214 } else if (sbsf->cmd == CMD_ERASE_64K &&
215 !(flags & (SECT_4K | SECT_32K))) {
216 sbsf->erase_size = 64 << 10;
218 debug(" cmd unknown: %#x\n", sbsf->cmd);
221 sbsf->state = SF_ADDR;
226 if (oldstate != sbsf->state)
227 debug(" cmd: transition to %s state\n",
228 sandbox_sf_state_name(sbsf->state));
233 int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
239 todo = min(size, sizeof(sandbox_sf_0xff));
240 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
249 static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
252 struct sandbox_spi_flash *sbsf = priv;
256 debug("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
257 sandbox_sf_state_name(sbsf->state), bytes);
259 if (sbsf->state == SF_CMD) {
260 /* Figure out the initial state */
261 if (sandbox_sf_process_cmd(sbsf, rx, tx))
266 /* Process the remaining data */
267 while (pos < bytes) {
268 switch (sbsf->state) {
272 debug(" id: off:%u tx:", sbsf->off);
273 if (sbsf->off < IDCODE_LEN) {
274 /* Extract correct byte from ID 0x00aabbcc */
275 id = sbsf->data->jedec >>
276 (8 * (IDCODE_LEN - 1 - sbsf->off));
280 debug("%d %02x\n", sbsf->off, id);
286 debug(" addr: bytes:%u rx:%02x ", sbsf->addr_bytes,
289 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
290 sbsf->off = (sbsf->off << 8) | rx[pos];
291 debug("addr:%06x\n", sbsf->off);
293 sandbox_spi_tristate(&tx[pos++], 1);
295 /* See if we're done processing */
296 if (sbsf->addr_bytes <
297 SF_ADDR_LEN + sbsf->pad_addr_bytes)
301 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
302 puts("sandbox_sf: os_lseek() failed");
306 case CMD_READ_ARRAY_FAST:
307 case CMD_READ_ARRAY_SLOW:
308 sbsf->state = SF_READ;
310 case CMD_PAGE_PROGRAM:
311 sbsf->state = SF_WRITE;
314 /* assume erase state ... */
315 sbsf->state = SF_ERASE;
318 debug(" cmd: transition to %s state\n",
319 sandbox_sf_state_name(sbsf->state));
323 * XXX: need to handle exotic behavior:
324 * - reading past end of device
328 debug(" tx: read(%u)\n", cnt);
329 ret = os_read(sbsf->fd, tx + pos, cnt);
331 puts("sandbox_spi: os_read() failed\n");
337 debug(" read status: %#x\n", sbsf->status);
339 memset(tx + pos, sbsf->status, cnt);
342 case SF_READ_STATUS1:
343 debug(" read status: %#x\n", sbsf->status);
345 memset(tx + pos, sbsf->status >> 8, cnt);
350 * XXX: need to handle exotic behavior:
351 * - unaligned addresses
352 * - more than a page (256) worth of data
353 * - reading past end of device
355 if (!(sbsf->status & STAT_WEL)) {
356 puts("sandbox_sf: write enable not set before write\n");
361 debug(" rx: write(%u)\n", cnt);
362 sandbox_spi_tristate(&tx[pos], cnt);
363 ret = os_write(sbsf->fd, rx + pos, cnt);
365 puts("sandbox_spi: os_write() failed\n");
369 sbsf->status &= ~STAT_WEL;
373 if (!(sbsf->status & STAT_WEL)) {
374 puts("sandbox_sf: write enable not set before erase\n");
378 /* verify address is aligned */
379 if (sbsf->off & (sbsf->erase_size - 1)) {
380 debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
381 sbsf->cmd, sbsf->erase_size,
383 sbsf->status &= ~STAT_WEL;
387 debug(" sector erase addr: %u, size: %u\n", sbsf->off,
391 sandbox_spi_tristate(&tx[pos], cnt);
395 * TODO(vapier@gentoo.org): latch WIP in status, and
396 * delay before clearing it ?
398 ret = sandbox_erase_part(sbsf, sbsf->erase_size);
399 sbsf->status &= ~STAT_WEL;
401 debug("sandbox_sf: Erase failed\n");
407 debug(" ??? no idea what to do ???\n");
413 return pos == bytes ? 0 : 1;
416 static const struct sandbox_spi_emu_ops sandbox_sf_ops = {
417 .setup = sandbox_sf_setup,
418 .free = sandbox_sf_free,
419 .cs_activate = sandbox_sf_cs_activate,
420 .cs_deactivate = sandbox_sf_cs_deactivate,
421 .xfer = sandbox_sf_xfer,
424 static int sandbox_cmdline_cb_spi_sf(struct sandbox_state *state,
427 unsigned long bus, cs;
428 const char *spec = sandbox_spi_parse_spec(arg, &bus, &cs);
434 * It is safe to not make a copy of 'spec' because it comes from the
437 * TODO(sjg@chromium.org): It would be nice if we could parse the
438 * spec here, but the problem is that no U-Boot init has been done
439 * yet. Perhaps we can figure something out.
441 state->spi[bus][cs].ops = &sandbox_sf_ops;
442 state->spi[bus][cs].spec = spec;
445 SANDBOX_CMDLINE_OPT(spi_sf, 1, "connect a SPI flash: <bus>:<cs>:<id>:<file>");