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.
17 #include <spi_flash.h>
18 #include "sf_internal.h"
20 #include <asm/getopt.h>
22 #include <asm/state.h>
23 #include <dm/device-internal.h>
25 #include <dm/uclass-internal.h>
27 DECLARE_GLOBAL_DATA_PTR;
30 * The different states that our SPI flash transitions between.
31 * We need to keep track of this across multiple xfer calls since
32 * the SPI bus could possibly call down into us multiple times.
34 enum sandbox_sf_state {
35 SF_CMD, /* default state -- we're awaiting a command */
36 SF_ID, /* read the flash's (jedec) ID code */
37 SF_ADDR, /* processing the offset in the flash to read/etc... */
38 SF_READ, /* reading data from the flash */
39 SF_WRITE, /* writing data to the flash, i.e. page programming */
40 SF_ERASE, /* erase the flash */
41 SF_READ_STATUS, /* read the flash's status register */
42 SF_READ_STATUS1, /* read the flash's status register upper 8 bits*/
43 SF_WRITE_STATUS, /* write the flash's status register */
46 static const char *sandbox_sf_state_name(enum sandbox_sf_state state)
48 static const char * const states[] = {
49 "CMD", "ID", "ADDR", "READ", "WRITE", "ERASE", "READ_STATUS",
50 "READ_STATUS1", "WRITE_STATUS",
55 /* Bits for the status register */
56 #define STAT_WIP (1 << 0)
57 #define STAT_WEL (1 << 1)
59 /* Assume all SPI flashes have 3 byte addresses since they do atm */
64 /* Used to quickly bulk erase backing store */
65 static u8 sandbox_sf_0xff[0x1000];
67 /* Internal state data for each SPI flash */
68 struct sandbox_spi_flash {
69 unsigned int cs; /* Chip select we are attached to */
71 * As we receive data over the SPI bus, our flash transitions
72 * between states. For example, we start off in the SF_CMD
73 * state where the first byte tells us what operation to perform
74 * (such as read or write the flash). But the operation itself
75 * can go through a few states such as first reading in the
76 * offset in the flash to perform the requested operation.
77 * Thus "state" stores the exact state that our machine is in
78 * while "cmd" stores the overall command we're processing.
80 enum sandbox_sf_state state;
82 /* Erase size of current erase command */
84 /* Current position in the flash; used when reading/writing/etc... */
86 /* How many address bytes we've consumed */
87 uint addr_bytes, pad_addr_bytes;
88 /* The current flash status (see STAT_XXX defines above) */
90 /* Data describing the flash we're emulating */
91 const struct spi_flash_info *data;
92 /* The file on disk to serv up data from */
96 struct sandbox_spi_flash_plat_data {
98 const char *device_name;
104 * This is a very strange probe function. If it has platform data (which may
105 * have come from the device tree) then this function gets the filename and
106 * device type from there. Failing that it looks at the command line
109 static int sandbox_sf_probe(struct udevice *dev)
111 /* spec = idcode:file */
112 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
114 size_t len, idname_len;
115 const struct spi_flash_info *data;
116 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
117 struct sandbox_state *state = state_get_current();
118 struct udevice *bus = dev->parent;
119 const char *spec = NULL;
124 debug("%s: bus %d, looking for emul=%p: ", __func__, bus->seq, dev);
125 if (bus->seq >= 0 && bus->seq < CONFIG_SANDBOX_SPI_MAX_BUS) {
126 for (i = 0; i < CONFIG_SANDBOX_SPI_MAX_CS; i++) {
127 if (state->spi[bus->seq][i].emul == dev)
132 printf("Error: Unknown chip select for device '%s'\n",
136 debug("found at cs %d\n", cs);
138 if (!pdata->filename) {
139 struct sandbox_state *state = state_get_current();
141 assert(bus->seq != -1);
142 if (bus->seq < CONFIG_SANDBOX_SPI_MAX_BUS)
143 spec = state->spi[bus->seq][cs].spec;
145 debug("%s: No spec found for bus %d, cs %d\n",
146 __func__, bus->seq, cs);
151 file = strchr(spec, ':');
153 printf("%s: unable to parse file\n", __func__);
157 idname_len = file - spec;
158 pdata->filename = file + 1;
159 pdata->device_name = spec;
162 spec = strchr(pdata->device_name, ',');
166 spec = pdata->device_name;
167 idname_len = strlen(spec);
169 debug("%s: device='%s'\n", __func__, spec);
171 for (data = spi_flash_ids; data->name; data++) {
172 len = strlen(data->name);
173 if (idname_len != len)
175 if (!strncasecmp(spec, data->name, len))
179 printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len,
185 if (sandbox_sf_0xff[0] == 0x00)
186 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
188 sbsf->fd = os_open(pdata->filename, 02);
189 if (sbsf->fd == -1) {
190 printf("%s: unable to open file '%s'\n", __func__,
202 debug("%s: Got error %d\n", __func__, ret);
206 static int sandbox_sf_remove(struct udevice *dev)
208 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
215 static void sandbox_sf_cs_activate(struct udevice *dev)
217 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
219 debug("sandbox_sf: CS activated; state is fresh!\n");
221 /* CS is asserted, so reset state */
223 sbsf->addr_bytes = 0;
224 sbsf->pad_addr_bytes = 0;
225 sbsf->state = SF_CMD;
229 static void sandbox_sf_cs_deactivate(struct udevice *dev)
231 debug("sandbox_sf: CS deactivated; cmd done processing!\n");
235 * There are times when the data lines are allowed to tristate. What
236 * is actually sensed on the line depends on the hardware. It could
237 * always be 0xFF/0x00 (if there are pull ups/downs), or things could
238 * float and so we'd get garbage back. This func encapsulates that
239 * scenario so we can worry about the details here.
241 static void sandbox_spi_tristate(u8 *buf, uint len)
243 /* XXX: make this into a user config option ? */
244 memset(buf, 0xff, len);
247 /* Figure out what command this stream is telling us to do */
248 static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
251 enum sandbox_sf_state oldstate = sbsf->state;
253 /* We need to output a byte for the cmd byte we just ate */
255 sandbox_spi_tristate(tx, 1);
263 case CMD_READ_ARRAY_FAST:
264 sbsf->pad_addr_bytes = 1;
265 case CMD_READ_ARRAY_SLOW:
266 case CMD_PAGE_PROGRAM:
267 sbsf->state = SF_ADDR;
269 case CMD_WRITE_DISABLE:
270 debug(" write disabled\n");
271 sbsf->status &= ~STAT_WEL;
273 case CMD_READ_STATUS:
274 sbsf->state = SF_READ_STATUS;
276 case CMD_READ_STATUS1:
277 sbsf->state = SF_READ_STATUS1;
279 case CMD_WRITE_ENABLE:
280 debug(" write enabled\n");
281 sbsf->status |= STAT_WEL;
283 case CMD_WRITE_STATUS:
284 sbsf->state = SF_WRITE_STATUS;
287 int flags = sbsf->data->flags;
289 /* we only support erase here */
290 if (sbsf->cmd == CMD_ERASE_CHIP) {
291 sbsf->erase_size = sbsf->data->sector_size *
292 sbsf->data->nr_sectors;
293 } else if (sbsf->cmd == CMD_ERASE_4K && (flags & SECT_4K)) {
294 sbsf->erase_size = 4 << 10;
295 } else if (sbsf->cmd == CMD_ERASE_64K && !(flags & SECT_4K)) {
296 sbsf->erase_size = 64 << 10;
298 debug(" cmd unknown: %#x\n", sbsf->cmd);
301 sbsf->state = SF_ADDR;
306 if (oldstate != sbsf->state)
307 debug(" cmd: transition to %s state\n",
308 sandbox_sf_state_name(sbsf->state));
313 int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
319 todo = min(size, (int)sizeof(sandbox_sf_0xff));
320 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
329 static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
330 const void *rxp, void *txp, unsigned long flags)
332 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
333 const uint8_t *rx = rxp;
336 int bytes = bitlen / 8;
339 debug("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
340 sandbox_sf_state_name(sbsf->state), bytes);
342 if ((flags & SPI_XFER_BEGIN))
343 sandbox_sf_cs_activate(dev);
345 if (sbsf->state == SF_CMD) {
346 /* Figure out the initial state */
347 ret = sandbox_sf_process_cmd(sbsf, rx, tx);
353 /* Process the remaining data */
354 while (pos < bytes) {
355 switch (sbsf->state) {
359 debug(" id: off:%u tx:", sbsf->off);
360 if (sbsf->off < IDCODE_LEN) {
361 /* Extract correct byte from ID 0x00aabbcc */
362 id = ((((sbsf->data)->id[0]) << 16) |
363 (((sbsf->data)->id[1]) << 8 |
364 ((sbsf->data)->id[2]))) >>
365 (8 * (IDCODE_LEN - 1 - sbsf->off));
369 debug("%d %02x\n", sbsf->off, id);
375 debug(" addr: bytes:%u rx:%02x ", sbsf->addr_bytes,
378 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
379 sbsf->off = (sbsf->off << 8) | rx[pos];
380 debug("addr:%06x\n", sbsf->off);
383 sandbox_spi_tristate(&tx[pos], 1);
386 /* See if we're done processing */
387 if (sbsf->addr_bytes <
388 SF_ADDR_LEN + sbsf->pad_addr_bytes)
392 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
393 puts("sandbox_sf: os_lseek() failed");
397 case CMD_READ_ARRAY_FAST:
398 case CMD_READ_ARRAY_SLOW:
399 sbsf->state = SF_READ;
401 case CMD_PAGE_PROGRAM:
402 sbsf->state = SF_WRITE;
405 /* assume erase state ... */
406 sbsf->state = SF_ERASE;
409 debug(" cmd: transition to %s state\n",
410 sandbox_sf_state_name(sbsf->state));
414 * XXX: need to handle exotic behavior:
415 * - reading past end of device
419 debug(" tx: read(%u)\n", cnt);
421 ret = os_read(sbsf->fd, tx + pos, cnt);
423 puts("sandbox_sf: os_read() failed\n");
429 debug(" read status: %#x\n", sbsf->status);
431 memset(tx + pos, sbsf->status, cnt);
434 case SF_READ_STATUS1:
435 debug(" read status: %#x\n", sbsf->status);
437 memset(tx + pos, sbsf->status >> 8, cnt);
440 case SF_WRITE_STATUS:
441 debug(" write status: %#x (ignored)\n", rx[pos]);
446 * XXX: need to handle exotic behavior:
447 * - unaligned addresses
448 * - more than a page (256) worth of data
449 * - reading past end of device
451 if (!(sbsf->status & STAT_WEL)) {
452 puts("sandbox_sf: write enable not set before write\n");
457 debug(" rx: write(%u)\n", cnt);
459 sandbox_spi_tristate(&tx[pos], cnt);
460 ret = os_write(sbsf->fd, rx + pos, cnt);
462 puts("sandbox_spi: os_write() failed\n");
466 sbsf->status &= ~STAT_WEL;
470 if (!(sbsf->status & STAT_WEL)) {
471 puts("sandbox_sf: write enable not set before erase\n");
475 /* verify address is aligned */
476 if (sbsf->off & (sbsf->erase_size - 1)) {
477 debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
478 sbsf->cmd, sbsf->erase_size,
480 sbsf->status &= ~STAT_WEL;
484 debug(" sector erase addr: %u, size: %u\n", sbsf->off,
489 sandbox_spi_tristate(&tx[pos], cnt);
493 * TODO(vapier@gentoo.org): latch WIP in status, and
494 * delay before clearing it ?
496 ret = sandbox_erase_part(sbsf, sbsf->erase_size);
497 sbsf->status &= ~STAT_WEL;
499 debug("sandbox_sf: Erase failed\n");
505 debug(" ??? no idea what to do ???\n");
511 if (flags & SPI_XFER_END)
512 sandbox_sf_cs_deactivate(dev);
513 return pos == bytes ? 0 : -EIO;
516 int sandbox_sf_ofdata_to_platdata(struct udevice *dev)
518 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
519 const void *blob = gd->fdt_blob;
520 int node = dev->of_offset;
522 pdata->filename = fdt_getprop(blob, node, "sandbox,filename", NULL);
523 pdata->device_name = fdt_getprop(blob, node, "compatible", NULL);
524 if (!pdata->filename || !pdata->device_name) {
525 debug("%s: Missing properties, filename=%s, device_name=%s\n",
526 __func__, pdata->filename, pdata->device_name);
533 static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
534 .xfer = sandbox_sf_xfer,
537 #ifdef CONFIG_SPI_FLASH
538 static int sandbox_cmdline_cb_spi_sf(struct sandbox_state *state,
541 unsigned long bus, cs;
542 const char *spec = sandbox_spi_parse_spec(arg, &bus, &cs);
548 * It is safe to not make a copy of 'spec' because it comes from the
551 * TODO(sjg@chromium.org): It would be nice if we could parse the
552 * spec here, but the problem is that no U-Boot init has been done
553 * yet. Perhaps we can figure something out.
555 state->spi[bus][cs].spec = spec;
556 debug("%s: Setting up spec '%s' for bus %ld, cs %ld\n", __func__,
561 SANDBOX_CMDLINE_OPT(spi_sf, 1, "connect a SPI flash: <bus>:<cs>:<id>:<file>");
563 int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
564 struct udevice *bus, int of_offset, const char *spec)
566 struct udevice *emul;
571 /* now the emulator */
572 strncpy(name, spec, sizeof(name) - 6);
573 name[sizeof(name) - 6] = '\0';
574 strcat(name, "-emul");
578 drv = lists_driver_lookup_name("sandbox_sf_emul");
580 puts("Cannot find sandbox_sf_emul driver\n");
583 ret = device_bind(bus, drv, str, NULL, of_offset, &emul);
585 printf("Cannot create emul device for spec '%s' (err=%d)\n",
589 state->spi[busnum][cs].emul = emul;
594 void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
598 dev = state->spi[busnum][cs].emul;
601 state->spi[busnum][cs].emul = NULL;
604 static int sandbox_sf_bind_bus_cs(struct sandbox_state *state, int busnum,
605 int cs, const char *spec)
607 struct udevice *bus, *slave;
610 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, true, &bus);
612 printf("Invalid bus %d for spec '%s' (err=%d)\n", busnum,
616 ret = spi_find_chip_select(bus, cs, &slave);
618 printf("Chip select %d already exists for spec '%s'\n", cs,
623 ret = device_bind_driver(bus, "spi_flash_std", spec, &slave);
627 return sandbox_sf_bind_emul(state, busnum, cs, bus, -1, spec);
630 int sandbox_spi_get_emul(struct sandbox_state *state,
631 struct udevice *bus, struct udevice *slave,
632 struct udevice **emulp)
634 struct sandbox_spi_info *info;
635 int busnum = bus->seq;
636 int cs = spi_chip_select(slave);
639 info = &state->spi[busnum][cs];
641 /* Use the same device tree node as the SPI flash device */
642 debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
643 __func__, busnum, cs);
644 ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
645 slave->of_offset, slave->name);
647 debug("failed (err=%d)\n", ret);
657 int dm_scan_other(bool pre_reloc_only)
659 struct sandbox_state *state = state_get_current();
664 for (busnum = 0; busnum < CONFIG_SANDBOX_SPI_MAX_BUS; busnum++) {
665 for (cs = 0; cs < CONFIG_SANDBOX_SPI_MAX_CS; cs++) {
666 const char *spec = state->spi[busnum][cs].spec;
670 ret = sandbox_sf_bind_bus_cs(state, busnum,
673 debug("%s: Bind failed for bus %d, cs %d\n",
674 __func__, busnum, cs);
677 debug("%s: Setting up spec '%s' for bus %d, cs %d\n",
678 __func__, spec, busnum, cs);
687 static const struct udevice_id sandbox_sf_ids[] = {
688 { .compatible = "sandbox,spi-flash" },
692 U_BOOT_DRIVER(sandbox_sf_emul) = {
693 .name = "sandbox_sf_emul",
694 .id = UCLASS_SPI_EMUL,
695 .of_match = sandbox_sf_ids,
696 .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata,
697 .probe = sandbox_sf_probe,
698 .remove = sandbox_sf_remove,
699 .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash),
700 .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data),
701 .ops = &sandbox_sf_emul_ops,