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
20 #include <spi_flash.h>
21 #include "sf_internal.h"
23 #include <asm/getopt.h>
25 #include <asm/state.h>
26 #include <dm/device-internal.h>
28 #include <dm/uclass-internal.h>
31 * The different states that our SPI flash transitions between.
32 * We need to keep track of this across multiple xfer calls since
33 * the SPI bus could possibly call down into us multiple times.
35 enum sandbox_sf_state {
36 SF_CMD, /* default state -- we're awaiting a command */
37 SF_ID, /* read the flash's (jedec) ID code */
38 SF_ADDR, /* processing the offset in the flash to read/etc... */
39 SF_READ, /* reading data from the flash */
40 SF_WRITE, /* writing data to the flash, i.e. page programming */
41 SF_ERASE, /* erase the flash */
42 SF_READ_STATUS, /* read the flash's status register */
43 SF_READ_STATUS1, /* read the flash's status register upper 8 bits*/
44 SF_WRITE_STATUS, /* write the flash's status register */
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",
56 /* Bits for the status register */
57 #define STAT_WIP (1 << 0)
58 #define STAT_WEL (1 << 1)
59 #define STAT_BP_SHIFT 2
60 #define STAT_BP_MASK (7 << STAT_BP_SHIFT)
62 /* Assume all SPI flashes have 3 byte addresses since they do atm */
67 /* Used to quickly bulk erase backing store */
68 static u8 sandbox_sf_0xff[0x1000];
70 /* Internal state data for each SPI flash */
71 struct sandbox_spi_flash {
72 unsigned int cs; /* Chip select we are attached to */
74 * As we receive data over the SPI bus, our flash transitions
75 * between states. For example, we start off in the SF_CMD
76 * state where the first byte tells us what operation to perform
77 * (such as read or write the flash). But the operation itself
78 * can go through a few states such as first reading in the
79 * offset in the flash to perform the requested operation.
80 * Thus "state" stores the exact state that our machine is in
81 * while "cmd" stores the overall command we're processing.
83 enum sandbox_sf_state state;
85 /* Erase size of current erase command */
87 /* Current position in the flash; used when reading/writing/etc... */
89 /* How many address bytes we've consumed */
90 uint addr_bytes, pad_addr_bytes;
91 /* The current flash status (see STAT_XXX defines above) */
93 /* Data describing the flash we're emulating */
94 const struct flash_info *data;
95 /* The file on disk to serv up data from */
99 struct sandbox_spi_flash_plat_data {
100 const char *filename;
101 const char *device_name;
106 void sandbox_sf_set_block_protect(struct udevice *dev, int bp_mask)
108 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
110 sbsf->status &= ~STAT_BP_MASK;
111 sbsf->status |= bp_mask << STAT_BP_SHIFT;
115 * This is a very strange probe function. If it has platform data (which may
116 * have come from the device tree) then this function gets the filename and
117 * device type from there.
119 static int sandbox_sf_probe(struct udevice *dev)
121 /* spec = idcode:file */
122 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
123 size_t len, idname_len;
124 const struct flash_info *data;
125 struct sandbox_spi_flash_plat_data *pdata = dev_get_plat(dev);
126 struct sandbox_state *state = state_get_current();
127 struct dm_spi_slave_plat *slave_plat;
128 struct udevice *bus = dev->parent;
129 const char *spec = NULL;
130 struct udevice *emul;
134 debug("%s: bus %d, looking for emul=%p: ", __func__, dev_seq(bus), dev);
135 ret = sandbox_spi_get_emul(state, bus, dev, &emul);
137 printf("Error: Unknown chip select for device '%s'\n",
141 slave_plat = dev_get_parent_plat(dev);
143 debug("found at cs %d\n", cs);
145 if (!pdata->filename) {
146 printf("Error: No filename available\n");
149 spec = strchr(pdata->device_name, ',');
153 spec = pdata->device_name;
154 idname_len = strlen(spec);
155 debug("%s: device='%s'\n", __func__, spec);
157 for (data = spi_nor_ids; data->name; data++) {
158 len = strlen(data->name);
159 if (idname_len != len)
161 if (!strncasecmp(spec, data->name, len))
165 printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len,
171 if (sandbox_sf_0xff[0] == 0x00)
172 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
174 sbsf->fd = os_open(pdata->filename, 02);
175 if (sbsf->fd == -1) {
176 printf("%s: unable to open file '%s'\n", __func__,
188 debug("%s: Got error %d\n", __func__, ret);
192 static int sandbox_sf_remove(struct udevice *dev)
194 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
201 static void sandbox_sf_cs_activate(struct udevice *dev)
203 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
205 log_content("sandbox_sf: CS activated; state is fresh!\n");
207 /* CS is asserted, so reset state */
209 sbsf->addr_bytes = 0;
210 sbsf->pad_addr_bytes = 0;
211 sbsf->state = SF_CMD;
215 static void sandbox_sf_cs_deactivate(struct udevice *dev)
217 log_content("sandbox_sf: CS deactivated; cmd done processing!\n");
221 * There are times when the data lines are allowed to tristate. What
222 * is actually sensed on the line depends on the hardware. It could
223 * always be 0xFF/0x00 (if there are pull ups/downs), or things could
224 * float and so we'd get garbage back. This func encapsulates that
225 * scenario so we can worry about the details here.
227 static void sandbox_spi_tristate(u8 *buf, uint len)
229 /* XXX: make this into a user config option ? */
230 memset(buf, 0xff, len);
233 /* Figure out what command this stream is telling us to do */
234 static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
237 enum sandbox_sf_state oldstate = sbsf->state;
239 /* We need to output a byte for the cmd byte we just ate */
241 sandbox_spi_tristate(tx, 1);
249 case SPINOR_OP_READ_FAST:
250 sbsf->pad_addr_bytes = 1;
253 sbsf->state = SF_ADDR;
256 debug(" write disabled\n");
257 sbsf->status &= ~STAT_WEL;
260 sbsf->state = SF_READ_STATUS;
262 case SPINOR_OP_RDSR2:
263 sbsf->state = SF_READ_STATUS1;
266 debug(" write enabled\n");
267 sbsf->status |= STAT_WEL;
270 sbsf->state = SF_WRITE_STATUS;
273 int flags = sbsf->data->flags;
275 /* we only support erase here */
276 if (sbsf->cmd == SPINOR_OP_CHIP_ERASE) {
277 sbsf->erase_size = sbsf->data->sector_size *
278 sbsf->data->n_sectors;
279 } else if (sbsf->cmd == SPINOR_OP_BE_4K && (flags & SECT_4K)) {
280 sbsf->erase_size = 4 << 10;
281 } else if (sbsf->cmd == SPINOR_OP_SE && !(flags & SECT_4K)) {
282 sbsf->erase_size = 64 << 10;
284 debug(" cmd unknown: %#x\n", sbsf->cmd);
287 sbsf->state = SF_ADDR;
292 if (oldstate != sbsf->state)
293 log_content(" cmd: transition to %s state\n",
294 sandbox_sf_state_name(sbsf->state));
299 int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
305 todo = min(size, (int)sizeof(sandbox_sf_0xff));
306 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
315 static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
316 const void *rxp, void *txp, unsigned long flags)
318 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
319 const uint8_t *rx = rxp;
322 int bytes = bitlen / 8;
325 log_content("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
326 sandbox_sf_state_name(sbsf->state), bytes);
328 if ((flags & SPI_XFER_BEGIN))
329 sandbox_sf_cs_activate(dev);
331 if (sbsf->state == SF_CMD) {
332 /* Figure out the initial state */
333 ret = sandbox_sf_process_cmd(sbsf, rx, tx);
339 /* Process the remaining data */
340 while (pos < bytes) {
341 switch (sbsf->state) {
345 log_content(" id: off:%u tx:", sbsf->off);
346 if (sbsf->off < IDCODE_LEN) {
347 /* Extract correct byte from ID 0x00aabbcc */
348 id = ((JEDEC_MFR(sbsf->data) << 16) |
349 JEDEC_ID(sbsf->data)) >>
350 (8 * (IDCODE_LEN - 1 - sbsf->off));
354 log_content("%d %02x\n", sbsf->off, id);
360 log_content(" addr: bytes:%u rx:%02x ",
361 sbsf->addr_bytes, rx[pos]);
363 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
364 sbsf->off = (sbsf->off << 8) | rx[pos];
365 log_content("addr:%06x\n", sbsf->off);
368 sandbox_spi_tristate(&tx[pos], 1);
371 /* See if we're done processing */
372 if (sbsf->addr_bytes <
373 SF_ADDR_LEN + sbsf->pad_addr_bytes)
377 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
378 puts("sandbox_sf: os_lseek() failed");
382 case SPINOR_OP_READ_FAST:
384 sbsf->state = SF_READ;
387 sbsf->state = SF_WRITE;
390 /* assume erase state ... */
391 sbsf->state = SF_ERASE;
394 log_content(" cmd: transition to %s state\n",
395 sandbox_sf_state_name(sbsf->state));
399 * XXX: need to handle exotic behavior:
400 * - reading past end of device
404 log_content(" tx: read(%u)\n", cnt);
406 ret = os_read(sbsf->fd, tx + pos, cnt);
408 puts("sandbox_sf: os_read() failed\n");
414 log_content(" read status: %#x\n", sbsf->status);
416 memset(tx + pos, sbsf->status, cnt);
419 case SF_READ_STATUS1:
420 log_content(" read status: %#x\n", sbsf->status);
422 memset(tx + pos, sbsf->status >> 8, cnt);
425 case SF_WRITE_STATUS:
426 log_content(" write status: %#x (ignored)\n", rx[pos]);
431 * XXX: need to handle exotic behavior:
432 * - unaligned addresses
433 * - more than a page (256) worth of data
434 * - reading past end of device
436 if (!(sbsf->status & STAT_WEL)) {
437 puts("sandbox_sf: write enable not set before write\n");
442 log_content(" rx: write(%u)\n", cnt);
444 sandbox_spi_tristate(&tx[pos], cnt);
445 ret = os_write(sbsf->fd, rx + pos, cnt);
447 puts("sandbox_spi: os_write() failed\n");
451 sbsf->status &= ~STAT_WEL;
455 if (!(sbsf->status & STAT_WEL)) {
456 puts("sandbox_sf: write enable not set before erase\n");
460 /* verify address is aligned */
461 if (sbsf->off & (sbsf->erase_size - 1)) {
462 log_content(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
463 sbsf->cmd, sbsf->erase_size,
465 sbsf->status &= ~STAT_WEL;
469 log_content(" sector erase addr: %u, size: %u\n",
470 sbsf->off, sbsf->erase_size);
474 sandbox_spi_tristate(&tx[pos], cnt);
478 * TODO(vapier@gentoo.org): latch WIP in status, and
479 * delay before clearing it ?
481 ret = sandbox_erase_part(sbsf, sbsf->erase_size);
482 sbsf->status &= ~STAT_WEL;
484 log_content("sandbox_sf: Erase failed\n");
490 log_content(" ??? no idea what to do ???\n");
496 if (flags & SPI_XFER_END)
497 sandbox_sf_cs_deactivate(dev);
498 return pos == bytes ? 0 : -EIO;
501 int sandbox_sf_of_to_plat(struct udevice *dev)
503 struct sandbox_spi_flash_plat_data *pdata = dev_get_plat(dev);
505 pdata->filename = dev_read_string(dev, "sandbox,filename");
506 pdata->device_name = dev_read_string(dev, "compatible");
507 if (!pdata->filename || !pdata->device_name) {
508 debug("%s: Missing properties, filename=%s, device_name=%s\n",
509 __func__, pdata->filename, pdata->device_name);
516 static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
517 .xfer = sandbox_sf_xfer,
520 #ifdef CONFIG_SPI_FLASH
521 int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
522 struct udevice *bus, ofnode node, const char *spec)
524 struct udevice *emul;
529 /* now the emulator */
530 strncpy(name, spec, sizeof(name) - 6);
531 name[sizeof(name) - 6] = '\0';
532 strcat(name, "-emul");
533 drv = lists_driver_lookup_name("sandbox_sf_emul");
535 puts("Cannot find sandbox_sf_emul driver\n");
541 ret = device_bind(bus, drv, str, NULL, node, &emul);
544 printf("Cannot create emul device for spec '%s' (err=%d)\n",
548 state->spi[busnum][cs].emul = emul;
553 void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
557 dev = state->spi[busnum][cs].emul;
558 device_remove(dev, DM_REMOVE_NORMAL);
560 state->spi[busnum][cs].emul = NULL;
563 int sandbox_spi_get_emul(struct sandbox_state *state,
564 struct udevice *bus, struct udevice *slave,
565 struct udevice **emulp)
567 struct sandbox_spi_info *info;
568 int busnum = dev_seq(bus);
569 int cs = spi_chip_select(slave);
572 info = &state->spi[busnum][cs];
574 /* Use the same device tree node as the SPI flash device */
575 debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
576 __func__, busnum, cs);
577 ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
578 dev_ofnode(slave), slave->name);
580 debug("failed (err=%d)\n", ret);
591 static const struct udevice_id sandbox_sf_ids[] = {
592 { .compatible = "sandbox,spi-flash" },
596 U_BOOT_DRIVER(sandbox_sf_emul) = {
597 .name = "sandbox_sf_emul",
598 .id = UCLASS_SPI_EMUL,
599 .of_match = sandbox_sf_ids,
600 .of_to_plat = sandbox_sf_of_to_plat,
601 .probe = sandbox_sf_probe,
602 .remove = sandbox_sf_remove,
603 .priv_auto = sizeof(struct sandbox_spi_flash),
604 .plat_auto = sizeof(struct sandbox_spi_flash_plat_data),
605 .ops = &sandbox_sf_emul_ops,