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>
28 * The different states that our SPI flash transitions between.
29 * We need to keep track of this across multiple xfer calls since
30 * the SPI bus could possibly call down into us multiple times.
32 enum sandbox_sf_state {
33 SF_CMD, /* default state -- we're awaiting a command */
34 SF_ID, /* read the flash's (jedec) ID code */
35 SF_ADDR, /* processing the offset in the flash to read/etc... */
36 SF_READ, /* reading data from the flash */
37 SF_WRITE, /* writing data to the flash, i.e. page programming */
38 SF_ERASE, /* erase the flash */
39 SF_READ_STATUS, /* read the flash's status register */
40 SF_READ_STATUS1, /* read the flash's status register upper 8 bits*/
41 SF_WRITE_STATUS, /* write the flash's status register */
44 static const char *sandbox_sf_state_name(enum sandbox_sf_state state)
46 static const char * const states[] = {
47 "CMD", "ID", "ADDR", "READ", "WRITE", "ERASE", "READ_STATUS",
48 "READ_STATUS1", "WRITE_STATUS",
53 /* Bits for the status register */
54 #define STAT_WIP (1 << 0)
55 #define STAT_WEL (1 << 1)
57 /* Assume all SPI flashes have 3 byte addresses since they do atm */
62 /* Used to quickly bulk erase backing store */
63 static u8 sandbox_sf_0xff[0x1000];
65 /* Internal state data for each SPI flash */
66 struct sandbox_spi_flash {
67 unsigned int cs; /* Chip select we are attached to */
69 * As we receive data over the SPI bus, our flash transitions
70 * between states. For example, we start off in the SF_CMD
71 * state where the first byte tells us what operation to perform
72 * (such as read or write the flash). But the operation itself
73 * can go through a few states such as first reading in the
74 * offset in the flash to perform the requested operation.
75 * Thus "state" stores the exact state that our machine is in
76 * while "cmd" stores the overall command we're processing.
78 enum sandbox_sf_state state;
80 /* Erase size of current erase command */
82 /* Current position in the flash; used when reading/writing/etc... */
84 /* How many address bytes we've consumed */
85 uint addr_bytes, pad_addr_bytes;
86 /* The current flash status (see STAT_XXX defines above) */
88 /* Data describing the flash we're emulating */
89 const struct spi_flash_info *data;
90 /* The file on disk to serv up data from */
94 struct sandbox_spi_flash_plat_data {
96 const char *device_name;
102 * This is a very strange probe function. If it has platform data (which may
103 * have come from the device tree) then this function gets the filename and
104 * device type from there. Failing that it looks at the command line
107 static int sandbox_sf_probe(struct udevice *dev)
109 /* spec = idcode:file */
110 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
112 size_t len, idname_len;
113 const struct spi_flash_info *data;
114 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
115 struct sandbox_state *state = state_get_current();
116 struct udevice *bus = dev->parent;
117 const char *spec = NULL;
122 debug("%s: bus %d, looking for emul=%p: ", __func__, bus->seq, dev);
123 if (bus->seq >= 0 && bus->seq < CONFIG_SANDBOX_SPI_MAX_BUS) {
124 for (i = 0; i < CONFIG_SANDBOX_SPI_MAX_CS; i++) {
125 if (state->spi[bus->seq][i].emul == dev)
130 printf("Error: Unknown chip select for device '%s'\n",
134 debug("found at cs %d\n", cs);
136 if (!pdata->filename) {
137 struct sandbox_state *state = state_get_current();
139 assert(bus->seq != -1);
140 if (bus->seq < CONFIG_SANDBOX_SPI_MAX_BUS)
141 spec = state->spi[bus->seq][cs].spec;
143 debug("%s: No spec found for bus %d, cs %d\n",
144 __func__, bus->seq, cs);
149 file = strchr(spec, ':');
151 printf("%s: unable to parse file\n", __func__);
155 idname_len = file - spec;
156 pdata->filename = file + 1;
157 pdata->device_name = spec;
160 spec = strchr(pdata->device_name, ',');
164 spec = pdata->device_name;
165 idname_len = strlen(spec);
167 debug("%s: device='%s'\n", __func__, spec);
169 for (data = spi_flash_ids; data->name; data++) {
170 len = strlen(data->name);
171 if (idname_len != len)
173 if (!strncasecmp(spec, data->name, len))
177 printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len,
183 if (sandbox_sf_0xff[0] == 0x00)
184 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
186 sbsf->fd = os_open(pdata->filename, 02);
187 if (sbsf->fd == -1) {
188 printf("%s: unable to open file '%s'\n", __func__,
200 debug("%s: Got error %d\n", __func__, ret);
204 static int sandbox_sf_remove(struct udevice *dev)
206 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
213 static void sandbox_sf_cs_activate(struct udevice *dev)
215 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
217 debug("sandbox_sf: CS activated; state is fresh!\n");
219 /* CS is asserted, so reset state */
221 sbsf->addr_bytes = 0;
222 sbsf->pad_addr_bytes = 0;
223 sbsf->state = SF_CMD;
227 static void sandbox_sf_cs_deactivate(struct udevice *dev)
229 debug("sandbox_sf: CS deactivated; cmd done processing!\n");
233 * There are times when the data lines are allowed to tristate. What
234 * is actually sensed on the line depends on the hardware. It could
235 * always be 0xFF/0x00 (if there are pull ups/downs), or things could
236 * float and so we'd get garbage back. This func encapsulates that
237 * scenario so we can worry about the details here.
239 static void sandbox_spi_tristate(u8 *buf, uint len)
241 /* XXX: make this into a user config option ? */
242 memset(buf, 0xff, len);
245 /* Figure out what command this stream is telling us to do */
246 static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
249 enum sandbox_sf_state oldstate = sbsf->state;
251 /* We need to output a byte for the cmd byte we just ate */
253 sandbox_spi_tristate(tx, 1);
261 case CMD_READ_ARRAY_FAST:
262 sbsf->pad_addr_bytes = 1;
263 case CMD_READ_ARRAY_SLOW:
264 case CMD_PAGE_PROGRAM:
265 sbsf->state = SF_ADDR;
267 case CMD_WRITE_DISABLE:
268 debug(" write disabled\n");
269 sbsf->status &= ~STAT_WEL;
271 case CMD_READ_STATUS:
272 sbsf->state = SF_READ_STATUS;
274 case CMD_READ_STATUS1:
275 sbsf->state = SF_READ_STATUS1;
277 case CMD_WRITE_ENABLE:
278 debug(" write enabled\n");
279 sbsf->status |= STAT_WEL;
281 case CMD_WRITE_STATUS:
282 sbsf->state = SF_WRITE_STATUS;
285 int flags = sbsf->data->flags;
287 /* we only support erase here */
288 if (sbsf->cmd == CMD_ERASE_CHIP) {
289 sbsf->erase_size = sbsf->data->sector_size *
290 sbsf->data->n_sectors;
291 } else if (sbsf->cmd == CMD_ERASE_4K && (flags & SECT_4K)) {
292 sbsf->erase_size = 4 << 10;
293 } else if (sbsf->cmd == CMD_ERASE_64K && !(flags & SECT_4K)) {
294 sbsf->erase_size = 64 << 10;
296 debug(" cmd unknown: %#x\n", sbsf->cmd);
299 sbsf->state = SF_ADDR;
304 if (oldstate != sbsf->state)
305 debug(" cmd: transition to %s state\n",
306 sandbox_sf_state_name(sbsf->state));
311 int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
317 todo = min(size, (int)sizeof(sandbox_sf_0xff));
318 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
327 static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
328 const void *rxp, void *txp, unsigned long flags)
330 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
331 const uint8_t *rx = rxp;
334 int bytes = bitlen / 8;
337 debug("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
338 sandbox_sf_state_name(sbsf->state), bytes);
340 if ((flags & SPI_XFER_BEGIN))
341 sandbox_sf_cs_activate(dev);
343 if (sbsf->state == SF_CMD) {
344 /* Figure out the initial state */
345 ret = sandbox_sf_process_cmd(sbsf, rx, tx);
351 /* Process the remaining data */
352 while (pos < bytes) {
353 switch (sbsf->state) {
357 debug(" id: off:%u tx:", sbsf->off);
358 if (sbsf->off < IDCODE_LEN) {
359 /* Extract correct byte from ID 0x00aabbcc */
360 id = ((JEDEC_MFR(sbsf->data) << 16) |
361 JEDEC_ID(sbsf->data)) >>
362 (8 * (IDCODE_LEN - 1 - sbsf->off));
366 debug("%d %02x\n", sbsf->off, id);
372 debug(" addr: bytes:%u rx:%02x ", sbsf->addr_bytes,
375 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
376 sbsf->off = (sbsf->off << 8) | rx[pos];
377 debug("addr:%06x\n", sbsf->off);
380 sandbox_spi_tristate(&tx[pos], 1);
383 /* See if we're done processing */
384 if (sbsf->addr_bytes <
385 SF_ADDR_LEN + sbsf->pad_addr_bytes)
389 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
390 puts("sandbox_sf: os_lseek() failed");
394 case CMD_READ_ARRAY_FAST:
395 case CMD_READ_ARRAY_SLOW:
396 sbsf->state = SF_READ;
398 case CMD_PAGE_PROGRAM:
399 sbsf->state = SF_WRITE;
402 /* assume erase state ... */
403 sbsf->state = SF_ERASE;
406 debug(" cmd: transition to %s state\n",
407 sandbox_sf_state_name(sbsf->state));
411 * XXX: need to handle exotic behavior:
412 * - reading past end of device
416 debug(" tx: read(%u)\n", cnt);
418 ret = os_read(sbsf->fd, tx + pos, cnt);
420 puts("sandbox_sf: os_read() failed\n");
426 debug(" read status: %#x\n", sbsf->status);
428 memset(tx + pos, sbsf->status, cnt);
431 case SF_READ_STATUS1:
432 debug(" read status: %#x\n", sbsf->status);
434 memset(tx + pos, sbsf->status >> 8, cnt);
437 case SF_WRITE_STATUS:
438 debug(" write status: %#x (ignored)\n", rx[pos]);
443 * XXX: need to handle exotic behavior:
444 * - unaligned addresses
445 * - more than a page (256) worth of data
446 * - reading past end of device
448 if (!(sbsf->status & STAT_WEL)) {
449 puts("sandbox_sf: write enable not set before write\n");
454 debug(" rx: write(%u)\n", cnt);
456 sandbox_spi_tristate(&tx[pos], cnt);
457 ret = os_write(sbsf->fd, rx + pos, cnt);
459 puts("sandbox_spi: os_write() failed\n");
463 sbsf->status &= ~STAT_WEL;
467 if (!(sbsf->status & STAT_WEL)) {
468 puts("sandbox_sf: write enable not set before erase\n");
472 /* verify address is aligned */
473 if (sbsf->off & (sbsf->erase_size - 1)) {
474 debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
475 sbsf->cmd, sbsf->erase_size,
477 sbsf->status &= ~STAT_WEL;
481 debug(" sector erase addr: %u, size: %u\n", sbsf->off,
486 sandbox_spi_tristate(&tx[pos], cnt);
490 * TODO(vapier@gentoo.org): latch WIP in status, and
491 * delay before clearing it ?
493 ret = sandbox_erase_part(sbsf, sbsf->erase_size);
494 sbsf->status &= ~STAT_WEL;
496 debug("sandbox_sf: Erase failed\n");
502 debug(" ??? no idea what to do ???\n");
508 if (flags & SPI_XFER_END)
509 sandbox_sf_cs_deactivate(dev);
510 return pos == bytes ? 0 : -EIO;
513 int sandbox_sf_ofdata_to_platdata(struct udevice *dev)
515 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
517 pdata->filename = dev_read_string(dev, "sandbox,filename");
518 pdata->device_name = dev_read_string(dev, "compatible");
519 if (!pdata->filename || !pdata->device_name) {
520 debug("%s: Missing properties, filename=%s, device_name=%s\n",
521 __func__, pdata->filename, pdata->device_name);
528 static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
529 .xfer = sandbox_sf_xfer,
532 #ifdef CONFIG_SPI_FLASH
533 static int sandbox_cmdline_cb_spi_sf(struct sandbox_state *state,
536 unsigned long bus, cs;
537 const char *spec = sandbox_spi_parse_spec(arg, &bus, &cs);
543 * It is safe to not make a copy of 'spec' because it comes from the
546 * TODO(sjg@chromium.org): It would be nice if we could parse the
547 * spec here, but the problem is that no U-Boot init has been done
548 * yet. Perhaps we can figure something out.
550 state->spi[bus][cs].spec = spec;
551 debug("%s: Setting up spec '%s' for bus %ld, cs %ld\n", __func__,
556 SANDBOX_CMDLINE_OPT(spi_sf, 1, "connect a SPI flash: <bus>:<cs>:<id>:<file>");
558 int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
559 struct udevice *bus, ofnode node, const char *spec)
561 struct udevice *emul;
566 /* now the emulator */
567 strncpy(name, spec, sizeof(name) - 6);
568 name[sizeof(name) - 6] = '\0';
569 strcat(name, "-emul");
570 drv = lists_driver_lookup_name("sandbox_sf_emul");
572 puts("Cannot find sandbox_sf_emul driver\n");
578 ret = device_bind_ofnode(bus, drv, str, NULL, node, &emul);
581 printf("Cannot create emul device for spec '%s' (err=%d)\n",
585 state->spi[busnum][cs].emul = emul;
590 void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
594 dev = state->spi[busnum][cs].emul;
595 device_remove(dev, DM_REMOVE_NORMAL);
597 state->spi[busnum][cs].emul = NULL;
600 static int sandbox_sf_bind_bus_cs(struct sandbox_state *state, int busnum,
601 int cs, const char *spec)
603 struct udevice *bus, *slave;
606 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, true, &bus);
608 printf("Invalid bus %d for spec '%s' (err=%d)\n", busnum,
612 ret = spi_find_chip_select(bus, cs, &slave);
614 printf("Chip select %d already exists for spec '%s'\n", cs,
619 ret = device_bind_driver(bus, "spi_flash_std", spec, &slave);
623 return sandbox_sf_bind_emul(state, busnum, cs, bus, ofnode_null(),
627 int sandbox_spi_get_emul(struct sandbox_state *state,
628 struct udevice *bus, struct udevice *slave,
629 struct udevice **emulp)
631 struct sandbox_spi_info *info;
632 int busnum = bus->seq;
633 int cs = spi_chip_select(slave);
636 info = &state->spi[busnum][cs];
638 /* Use the same device tree node as the SPI flash device */
639 debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
640 __func__, busnum, cs);
641 ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
642 dev_ofnode(slave), slave->name);
644 debug("failed (err=%d)\n", ret);
654 int dm_scan_other(bool pre_reloc_only)
656 struct sandbox_state *state = state_get_current();
661 for (busnum = 0; busnum < CONFIG_SANDBOX_SPI_MAX_BUS; busnum++) {
662 for (cs = 0; cs < CONFIG_SANDBOX_SPI_MAX_CS; cs++) {
663 const char *spec = state->spi[busnum][cs].spec;
667 ret = sandbox_sf_bind_bus_cs(state, busnum,
670 debug("%s: Bind failed for bus %d, cs %d\n",
671 __func__, busnum, cs);
674 debug("%s: Setting up spec '%s' for bus %d, cs %d\n",
675 __func__, spec, busnum, cs);
684 static const struct udevice_id sandbox_sf_ids[] = {
685 { .compatible = "sandbox,spi-flash" },
689 U_BOOT_DRIVER(sandbox_sf_emul) = {
690 .name = "sandbox_sf_emul",
691 .id = UCLASS_SPI_EMUL,
692 .of_match = sandbox_sf_ids,
693 .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata,
694 .probe = sandbox_sf_probe,
695 .remove = sandbox_sf_remove,
696 .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash),
697 .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data),
698 .ops = &sandbox_sf_emul_ops,