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.
106 static int sandbox_sf_probe(struct udevice *dev)
108 /* spec = idcode:file */
109 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
110 size_t len, idname_len;
111 const struct spi_flash_info *data;
112 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
113 struct sandbox_state *state = state_get_current();
114 struct udevice *bus = dev->parent;
115 const char *spec = NULL;
120 debug("%s: bus %d, looking for emul=%p: ", __func__, bus->seq, dev);
121 if (bus->seq >= 0 && bus->seq < CONFIG_SANDBOX_SPI_MAX_BUS) {
122 for (i = 0; i < CONFIG_SANDBOX_SPI_MAX_CS; i++) {
123 if (state->spi[bus->seq][i].emul == dev)
128 printf("Error: Unknown chip select for device '%s'\n",
132 debug("found at cs %d\n", cs);
134 if (!pdata->filename) {
135 printf("Error: No filename available\n");
138 spec = strchr(pdata->device_name, ',');
142 spec = pdata->device_name;
143 idname_len = strlen(spec);
144 debug("%s: device='%s'\n", __func__, spec);
146 for (data = spi_flash_ids; data->name; data++) {
147 len = strlen(data->name);
148 if (idname_len != len)
150 if (!strncasecmp(spec, data->name, len))
154 printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len,
160 if (sandbox_sf_0xff[0] == 0x00)
161 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
163 sbsf->fd = os_open(pdata->filename, 02);
164 if (sbsf->fd == -1) {
165 printf("%s: unable to open file '%s'\n", __func__,
177 debug("%s: Got error %d\n", __func__, ret);
181 static int sandbox_sf_remove(struct udevice *dev)
183 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
190 static void sandbox_sf_cs_activate(struct udevice *dev)
192 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
194 debug("sandbox_sf: CS activated; state is fresh!\n");
196 /* CS is asserted, so reset state */
198 sbsf->addr_bytes = 0;
199 sbsf->pad_addr_bytes = 0;
200 sbsf->state = SF_CMD;
204 static void sandbox_sf_cs_deactivate(struct udevice *dev)
206 debug("sandbox_sf: CS deactivated; cmd done processing!\n");
210 * There are times when the data lines are allowed to tristate. What
211 * is actually sensed on the line depends on the hardware. It could
212 * always be 0xFF/0x00 (if there are pull ups/downs), or things could
213 * float and so we'd get garbage back. This func encapsulates that
214 * scenario so we can worry about the details here.
216 static void sandbox_spi_tristate(u8 *buf, uint len)
218 /* XXX: make this into a user config option ? */
219 memset(buf, 0xff, len);
222 /* Figure out what command this stream is telling us to do */
223 static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
226 enum sandbox_sf_state oldstate = sbsf->state;
228 /* We need to output a byte for the cmd byte we just ate */
230 sandbox_spi_tristate(tx, 1);
238 case CMD_READ_ARRAY_FAST:
239 sbsf->pad_addr_bytes = 1;
240 case CMD_READ_ARRAY_SLOW:
241 case CMD_PAGE_PROGRAM:
242 sbsf->state = SF_ADDR;
244 case CMD_WRITE_DISABLE:
245 debug(" write disabled\n");
246 sbsf->status &= ~STAT_WEL;
248 case CMD_READ_STATUS:
249 sbsf->state = SF_READ_STATUS;
251 case CMD_READ_STATUS1:
252 sbsf->state = SF_READ_STATUS1;
254 case CMD_WRITE_ENABLE:
255 debug(" write enabled\n");
256 sbsf->status |= STAT_WEL;
258 case CMD_WRITE_STATUS:
259 sbsf->state = SF_WRITE_STATUS;
262 int flags = sbsf->data->flags;
264 /* we only support erase here */
265 if (sbsf->cmd == CMD_ERASE_CHIP) {
266 sbsf->erase_size = sbsf->data->sector_size *
267 sbsf->data->n_sectors;
268 } else if (sbsf->cmd == CMD_ERASE_4K && (flags & SECT_4K)) {
269 sbsf->erase_size = 4 << 10;
270 } else if (sbsf->cmd == CMD_ERASE_64K && !(flags & SECT_4K)) {
271 sbsf->erase_size = 64 << 10;
273 debug(" cmd unknown: %#x\n", sbsf->cmd);
276 sbsf->state = SF_ADDR;
281 if (oldstate != sbsf->state)
282 debug(" cmd: transition to %s state\n",
283 sandbox_sf_state_name(sbsf->state));
288 int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
294 todo = min(size, (int)sizeof(sandbox_sf_0xff));
295 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
304 static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
305 const void *rxp, void *txp, unsigned long flags)
307 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
308 const uint8_t *rx = rxp;
311 int bytes = bitlen / 8;
314 debug("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
315 sandbox_sf_state_name(sbsf->state), bytes);
317 if ((flags & SPI_XFER_BEGIN))
318 sandbox_sf_cs_activate(dev);
320 if (sbsf->state == SF_CMD) {
321 /* Figure out the initial state */
322 ret = sandbox_sf_process_cmd(sbsf, rx, tx);
328 /* Process the remaining data */
329 while (pos < bytes) {
330 switch (sbsf->state) {
334 debug(" id: off:%u tx:", sbsf->off);
335 if (sbsf->off < IDCODE_LEN) {
336 /* Extract correct byte from ID 0x00aabbcc */
337 id = ((JEDEC_MFR(sbsf->data) << 16) |
338 JEDEC_ID(sbsf->data)) >>
339 (8 * (IDCODE_LEN - 1 - sbsf->off));
343 debug("%d %02x\n", sbsf->off, id);
349 debug(" addr: bytes:%u rx:%02x ", sbsf->addr_bytes,
352 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
353 sbsf->off = (sbsf->off << 8) | rx[pos];
354 debug("addr:%06x\n", sbsf->off);
357 sandbox_spi_tristate(&tx[pos], 1);
360 /* See if we're done processing */
361 if (sbsf->addr_bytes <
362 SF_ADDR_LEN + sbsf->pad_addr_bytes)
366 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
367 puts("sandbox_sf: os_lseek() failed");
371 case CMD_READ_ARRAY_FAST:
372 case CMD_READ_ARRAY_SLOW:
373 sbsf->state = SF_READ;
375 case CMD_PAGE_PROGRAM:
376 sbsf->state = SF_WRITE;
379 /* assume erase state ... */
380 sbsf->state = SF_ERASE;
383 debug(" cmd: transition to %s state\n",
384 sandbox_sf_state_name(sbsf->state));
388 * XXX: need to handle exotic behavior:
389 * - reading past end of device
393 debug(" tx: read(%u)\n", cnt);
395 ret = os_read(sbsf->fd, tx + pos, cnt);
397 puts("sandbox_sf: os_read() failed\n");
403 debug(" read status: %#x\n", sbsf->status);
405 memset(tx + pos, sbsf->status, cnt);
408 case SF_READ_STATUS1:
409 debug(" read status: %#x\n", sbsf->status);
411 memset(tx + pos, sbsf->status >> 8, cnt);
414 case SF_WRITE_STATUS:
415 debug(" write status: %#x (ignored)\n", rx[pos]);
420 * XXX: need to handle exotic behavior:
421 * - unaligned addresses
422 * - more than a page (256) worth of data
423 * - reading past end of device
425 if (!(sbsf->status & STAT_WEL)) {
426 puts("sandbox_sf: write enable not set before write\n");
431 debug(" rx: write(%u)\n", cnt);
433 sandbox_spi_tristate(&tx[pos], cnt);
434 ret = os_write(sbsf->fd, rx + pos, cnt);
436 puts("sandbox_spi: os_write() failed\n");
440 sbsf->status &= ~STAT_WEL;
444 if (!(sbsf->status & STAT_WEL)) {
445 puts("sandbox_sf: write enable not set before erase\n");
449 /* verify address is aligned */
450 if (sbsf->off & (sbsf->erase_size - 1)) {
451 debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
452 sbsf->cmd, sbsf->erase_size,
454 sbsf->status &= ~STAT_WEL;
458 debug(" sector erase addr: %u, size: %u\n", sbsf->off,
463 sandbox_spi_tristate(&tx[pos], cnt);
467 * TODO(vapier@gentoo.org): latch WIP in status, and
468 * delay before clearing it ?
470 ret = sandbox_erase_part(sbsf, sbsf->erase_size);
471 sbsf->status &= ~STAT_WEL;
473 debug("sandbox_sf: Erase failed\n");
479 debug(" ??? no idea what to do ???\n");
485 if (flags & SPI_XFER_END)
486 sandbox_sf_cs_deactivate(dev);
487 return pos == bytes ? 0 : -EIO;
490 int sandbox_sf_ofdata_to_platdata(struct udevice *dev)
492 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
494 pdata->filename = dev_read_string(dev, "sandbox,filename");
495 pdata->device_name = dev_read_string(dev, "compatible");
496 if (!pdata->filename || !pdata->device_name) {
497 debug("%s: Missing properties, filename=%s, device_name=%s\n",
498 __func__, pdata->filename, pdata->device_name);
505 static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
506 .xfer = sandbox_sf_xfer,
509 #ifdef CONFIG_SPI_FLASH
510 int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
511 struct udevice *bus, ofnode node, const char *spec)
513 struct udevice *emul;
518 /* now the emulator */
519 strncpy(name, spec, sizeof(name) - 6);
520 name[sizeof(name) - 6] = '\0';
521 strcat(name, "-emul");
522 drv = lists_driver_lookup_name("sandbox_sf_emul");
524 puts("Cannot find sandbox_sf_emul driver\n");
530 ret = device_bind_ofnode(bus, drv, str, NULL, node, &emul);
533 printf("Cannot create emul device for spec '%s' (err=%d)\n",
537 state->spi[busnum][cs].emul = emul;
542 void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
546 dev = state->spi[busnum][cs].emul;
547 device_remove(dev, DM_REMOVE_NORMAL);
549 state->spi[busnum][cs].emul = NULL;
552 int sandbox_spi_get_emul(struct sandbox_state *state,
553 struct udevice *bus, struct udevice *slave,
554 struct udevice **emulp)
556 struct sandbox_spi_info *info;
557 int busnum = bus->seq;
558 int cs = spi_chip_select(slave);
561 info = &state->spi[busnum][cs];
563 /* Use the same device tree node as the SPI flash device */
564 debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
565 __func__, busnum, cs);
566 ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
567 dev_ofnode(slave), slave->name);
569 debug("failed (err=%d)\n", ret);
580 static const struct udevice_id sandbox_sf_ids[] = {
581 { .compatible = "sandbox,spi-flash" },
585 U_BOOT_DRIVER(sandbox_sf_emul) = {
586 .name = "sandbox_sf_emul",
587 .id = UCLASS_SPI_EMUL,
588 .of_match = sandbox_sf_ids,
589 .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata,
590 .probe = sandbox_sf_probe,
591 .remove = sandbox_sf_remove,
592 .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash),
593 .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data),
594 .ops = &sandbox_sf_emul_ops,