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.
11 #define LOG_CATEGORY UCLASS_SPI_FLASH
19 #include <spi_flash.h>
20 #include "sf_internal.h"
22 #include <asm/getopt.h>
24 #include <asm/state.h>
25 #include <dm/device-internal.h>
27 #include <dm/uclass-internal.h>
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 #if CONFIG_IS_ENABLED(LOG)
47 static const char *sandbox_sf_state_name(enum sandbox_sf_state state)
49 static const char * const states[] = {
50 "CMD", "ID", "ADDR", "READ", "WRITE", "ERASE", "READ_STATUS",
51 "READ_STATUS1", "WRITE_STATUS",
57 /* Bits for the status register */
58 #define STAT_WIP (1 << 0)
59 #define STAT_WEL (1 << 1)
61 /* Assume all SPI flashes have 3 byte addresses since they do atm */
66 /* Used to quickly bulk erase backing store */
67 static u8 sandbox_sf_0xff[0x1000];
69 /* Internal state data for each SPI flash */
70 struct sandbox_spi_flash {
71 unsigned int cs; /* Chip select we are attached to */
73 * As we receive data over the SPI bus, our flash transitions
74 * between states. For example, we start off in the SF_CMD
75 * state where the first byte tells us what operation to perform
76 * (such as read or write the flash). But the operation itself
77 * can go through a few states such as first reading in the
78 * offset in the flash to perform the requested operation.
79 * Thus "state" stores the exact state that our machine is in
80 * while "cmd" stores the overall command we're processing.
82 enum sandbox_sf_state state;
84 /* Erase size of current erase command */
86 /* Current position in the flash; used when reading/writing/etc... */
88 /* How many address bytes we've consumed */
89 uint addr_bytes, pad_addr_bytes;
90 /* The current flash status (see STAT_XXX defines above) */
92 /* Data describing the flash we're emulating */
93 const struct spi_flash_info *data;
94 /* The file on disk to serv up data from */
98 struct sandbox_spi_flash_plat_data {
100 const char *device_name;
106 * This is a very strange probe function. If it has platform data (which may
107 * have come from the device tree) then this function gets the filename and
108 * device type from there.
110 static int sandbox_sf_probe(struct udevice *dev)
112 /* spec = idcode:file */
113 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 dm_spi_slave_platdata *slave_plat;
119 struct udevice *bus = dev->parent;
120 const char *spec = NULL;
121 struct udevice *emul;
125 debug("%s: bus %d, looking for emul=%p: ", __func__, bus->seq, dev);
126 ret = sandbox_spi_get_emul(state, bus, dev, &emul);
128 printf("Error: Unknown chip select for device '%s'\n",
132 slave_plat = dev_get_parent_platdata(dev);
134 debug("found at cs %d\n", cs);
136 if (!pdata->filename) {
137 printf("Error: No filename available\n");
140 spec = strchr(pdata->device_name, ',');
144 spec = pdata->device_name;
145 idname_len = strlen(spec);
146 debug("%s: device='%s'\n", __func__, spec);
148 for (data = spi_flash_ids; data->name; data++) {
149 len = strlen(data->name);
150 if (idname_len != len)
152 if (!strncasecmp(spec, data->name, len))
156 printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len,
162 if (sandbox_sf_0xff[0] == 0x00)
163 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
165 sbsf->fd = os_open(pdata->filename, 02);
166 if (sbsf->fd == -1) {
167 printf("%s: unable to open file '%s'\n", __func__,
179 debug("%s: Got error %d\n", __func__, ret);
183 static int sandbox_sf_remove(struct udevice *dev)
185 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
192 static void sandbox_sf_cs_activate(struct udevice *dev)
194 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
196 log_content("sandbox_sf: CS activated; state is fresh!\n");
198 /* CS is asserted, so reset state */
200 sbsf->addr_bytes = 0;
201 sbsf->pad_addr_bytes = 0;
202 sbsf->state = SF_CMD;
206 static void sandbox_sf_cs_deactivate(struct udevice *dev)
208 log_content("sandbox_sf: CS deactivated; cmd done processing!\n");
212 * There are times when the data lines are allowed to tristate. What
213 * is actually sensed on the line depends on the hardware. It could
214 * always be 0xFF/0x00 (if there are pull ups/downs), or things could
215 * float and so we'd get garbage back. This func encapsulates that
216 * scenario so we can worry about the details here.
218 static void sandbox_spi_tristate(u8 *buf, uint len)
220 /* XXX: make this into a user config option ? */
221 memset(buf, 0xff, len);
224 /* Figure out what command this stream is telling us to do */
225 static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
228 enum sandbox_sf_state oldstate = sbsf->state;
230 /* We need to output a byte for the cmd byte we just ate */
232 sandbox_spi_tristate(tx, 1);
240 case CMD_READ_ARRAY_FAST:
241 sbsf->pad_addr_bytes = 1;
242 case CMD_READ_ARRAY_SLOW:
243 case CMD_PAGE_PROGRAM:
244 sbsf->state = SF_ADDR;
246 case CMD_WRITE_DISABLE:
247 debug(" write disabled\n");
248 sbsf->status &= ~STAT_WEL;
250 case CMD_READ_STATUS:
251 sbsf->state = SF_READ_STATUS;
253 case CMD_READ_STATUS1:
254 sbsf->state = SF_READ_STATUS1;
256 case CMD_WRITE_ENABLE:
257 debug(" write enabled\n");
258 sbsf->status |= STAT_WEL;
260 case CMD_WRITE_STATUS:
261 sbsf->state = SF_WRITE_STATUS;
264 int flags = sbsf->data->flags;
266 /* we only support erase here */
267 if (sbsf->cmd == CMD_ERASE_CHIP) {
268 sbsf->erase_size = sbsf->data->sector_size *
269 sbsf->data->n_sectors;
270 } else if (sbsf->cmd == CMD_ERASE_4K && (flags & SECT_4K)) {
271 sbsf->erase_size = 4 << 10;
272 } else if (sbsf->cmd == CMD_ERASE_64K && !(flags & SECT_4K)) {
273 sbsf->erase_size = 64 << 10;
275 debug(" cmd unknown: %#x\n", sbsf->cmd);
278 sbsf->state = SF_ADDR;
283 if (oldstate != sbsf->state)
284 log_content(" cmd: transition to %s state\n",
285 sandbox_sf_state_name(sbsf->state));
290 int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
296 todo = min(size, (int)sizeof(sandbox_sf_0xff));
297 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
306 static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
307 const void *rxp, void *txp, unsigned long flags)
309 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
310 const uint8_t *rx = rxp;
313 int bytes = bitlen / 8;
316 log_content("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
317 sandbox_sf_state_name(sbsf->state), bytes);
319 if ((flags & SPI_XFER_BEGIN))
320 sandbox_sf_cs_activate(dev);
322 if (sbsf->state == SF_CMD) {
323 /* Figure out the initial state */
324 ret = sandbox_sf_process_cmd(sbsf, rx, tx);
330 /* Process the remaining data */
331 while (pos < bytes) {
332 switch (sbsf->state) {
336 log_content(" id: off:%u tx:", sbsf->off);
337 if (sbsf->off < IDCODE_LEN) {
338 /* Extract correct byte from ID 0x00aabbcc */
339 id = ((JEDEC_MFR(sbsf->data) << 16) |
340 JEDEC_ID(sbsf->data)) >>
341 (8 * (IDCODE_LEN - 1 - sbsf->off));
345 log_content("%d %02x\n", sbsf->off, id);
351 log_content(" addr: bytes:%u rx:%02x ",
352 sbsf->addr_bytes, rx[pos]);
354 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
355 sbsf->off = (sbsf->off << 8) | rx[pos];
356 log_content("addr:%06x\n", sbsf->off);
359 sandbox_spi_tristate(&tx[pos], 1);
362 /* See if we're done processing */
363 if (sbsf->addr_bytes <
364 SF_ADDR_LEN + sbsf->pad_addr_bytes)
368 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
369 puts("sandbox_sf: os_lseek() failed");
373 case CMD_READ_ARRAY_FAST:
374 case CMD_READ_ARRAY_SLOW:
375 sbsf->state = SF_READ;
377 case CMD_PAGE_PROGRAM:
378 sbsf->state = SF_WRITE;
381 /* assume erase state ... */
382 sbsf->state = SF_ERASE;
385 log_content(" cmd: transition to %s state\n",
386 sandbox_sf_state_name(sbsf->state));
390 * XXX: need to handle exotic behavior:
391 * - reading past end of device
395 log_content(" tx: read(%u)\n", cnt);
397 ret = os_read(sbsf->fd, tx + pos, cnt);
399 puts("sandbox_sf: os_read() failed\n");
405 log_content(" read status: %#x\n", sbsf->status);
407 memset(tx + pos, sbsf->status, cnt);
410 case SF_READ_STATUS1:
411 log_content(" read status: %#x\n", sbsf->status);
413 memset(tx + pos, sbsf->status >> 8, cnt);
416 case SF_WRITE_STATUS:
417 log_content(" write status: %#x (ignored)\n", rx[pos]);
422 * XXX: need to handle exotic behavior:
423 * - unaligned addresses
424 * - more than a page (256) worth of data
425 * - reading past end of device
427 if (!(sbsf->status & STAT_WEL)) {
428 puts("sandbox_sf: write enable not set before write\n");
433 log_content(" rx: write(%u)\n", cnt);
435 sandbox_spi_tristate(&tx[pos], cnt);
436 ret = os_write(sbsf->fd, rx + pos, cnt);
438 puts("sandbox_spi: os_write() failed\n");
442 sbsf->status &= ~STAT_WEL;
446 if (!(sbsf->status & STAT_WEL)) {
447 puts("sandbox_sf: write enable not set before erase\n");
451 /* verify address is aligned */
452 if (sbsf->off & (sbsf->erase_size - 1)) {
453 log_content(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
454 sbsf->cmd, sbsf->erase_size,
456 sbsf->status &= ~STAT_WEL;
460 log_content(" sector erase addr: %u, size: %u\n",
461 sbsf->off, sbsf->erase_size);
465 sandbox_spi_tristate(&tx[pos], cnt);
469 * TODO(vapier@gentoo.org): latch WIP in status, and
470 * delay before clearing it ?
472 ret = sandbox_erase_part(sbsf, sbsf->erase_size);
473 sbsf->status &= ~STAT_WEL;
475 log_content("sandbox_sf: Erase failed\n");
481 log_content(" ??? no idea what to do ???\n");
487 if (flags & SPI_XFER_END)
488 sandbox_sf_cs_deactivate(dev);
489 return pos == bytes ? 0 : -EIO;
492 int sandbox_sf_ofdata_to_platdata(struct udevice *dev)
494 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
496 pdata->filename = dev_read_string(dev, "sandbox,filename");
497 pdata->device_name = dev_read_string(dev, "compatible");
498 if (!pdata->filename || !pdata->device_name) {
499 debug("%s: Missing properties, filename=%s, device_name=%s\n",
500 __func__, pdata->filename, pdata->device_name);
507 static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
508 .xfer = sandbox_sf_xfer,
511 #ifdef CONFIG_SPI_FLASH
512 int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
513 struct udevice *bus, ofnode node, const char *spec)
515 struct udevice *emul;
520 /* now the emulator */
521 strncpy(name, spec, sizeof(name) - 6);
522 name[sizeof(name) - 6] = '\0';
523 strcat(name, "-emul");
524 drv = lists_driver_lookup_name("sandbox_sf_emul");
526 puts("Cannot find sandbox_sf_emul driver\n");
532 ret = device_bind_ofnode(bus, drv, str, NULL, node, &emul);
535 printf("Cannot create emul device for spec '%s' (err=%d)\n",
539 state->spi[busnum][cs].emul = emul;
544 void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
548 dev = state->spi[busnum][cs].emul;
549 device_remove(dev, DM_REMOVE_NORMAL);
551 state->spi[busnum][cs].emul = NULL;
554 int sandbox_spi_get_emul(struct sandbox_state *state,
555 struct udevice *bus, struct udevice *slave,
556 struct udevice **emulp)
558 struct sandbox_spi_info *info;
559 int busnum = bus->seq;
560 int cs = spi_chip_select(slave);
563 info = &state->spi[busnum][cs];
565 /* Use the same device tree node as the SPI flash device */
566 debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
567 __func__, busnum, cs);
568 ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
569 dev_ofnode(slave), slave->name);
571 debug("failed (err=%d)\n", ret);
582 static const struct udevice_id sandbox_sf_ids[] = {
583 { .compatible = "sandbox,spi-flash" },
587 U_BOOT_DRIVER(sandbox_sf_emul) = {
588 .name = "sandbox_sf_emul",
589 .id = UCLASS_SPI_EMUL,
590 .of_match = sandbox_sf_ids,
591 .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata,
592 .probe = sandbox_sf_probe,
593 .remove = sandbox_sf_remove,
594 .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash),
595 .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data),
596 .ops = &sandbox_sf_emul_ops,