]> git.sur5r.net Git - u-boot/blobdiff - drivers/mtd/spi/sandbox.c
Merge branch 'master' of git://git.denx.de/u-boot-spi
[u-boot] / drivers / mtd / spi / sandbox.c
index 3024b988fef904884f4d6e4920d98859714f115c..f23c0e13e0c87124682dee147e530dad737a56c7 100644 (file)
@@ -24,8 +24,6 @@
 #include <dm/lists.h>
 #include <dm/uclass-internal.h>
 
-DECLARE_GLOBAL_DATA_PTR;
-
 /*
  * The different states that our SPI flash transitions between.
  * We need to keep track of this across multiple xfer calls since
@@ -88,7 +86,7 @@ struct sandbox_spi_flash {
        /* The current flash status (see STAT_XXX defines above) */
        u16 status;
        /* Data describing the flash we're emulating */
-       const struct spi_flash_params *data;
+       const struct spi_flash_info *data;
        /* The file on disk to serv up data from */
        int fd;
 };
@@ -112,7 +110,7 @@ static int sandbox_sf_probe(struct udevice *dev)
        struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
        const char *file;
        size_t len, idname_len;
-       const struct spi_flash_params *data;
+       const struct spi_flash_info *data;
        struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
        struct sandbox_state *state = state_get_current();
        struct udevice *bus = dev->parent;
@@ -129,7 +127,7 @@ static int sandbox_sf_probe(struct udevice *dev)
                }
        }
        if (cs == -1) {
-               printf("Error: Unknown chip select for device '%s'",
+               printf("Error: Unknown chip select for device '%s'\n",
                       dev->name);
                return -EINVAL;
        }
@@ -141,12 +139,16 @@ static int sandbox_sf_probe(struct udevice *dev)
                assert(bus->seq != -1);
                if (bus->seq < CONFIG_SANDBOX_SPI_MAX_BUS)
                        spec = state->spi[bus->seq][cs].spec;
-               if (!spec)
-                       return -ENOENT;
+               if (!spec) {
+                       debug("%s:  No spec found for bus %d, cs %d\n",
+                             __func__, bus->seq, cs);
+                       ret = -ENOENT;
+                       goto error;
+               }
 
                file = strchr(spec, ':');
                if (!file) {
-                       printf("sandbox_sf: unable to parse file\n");
+                       printf("%s: unable to parse file\n", __func__);
                        ret = -EINVAL;
                        goto error;
                }
@@ -164,7 +166,7 @@ static int sandbox_sf_probe(struct udevice *dev)
        }
        debug("%s: device='%s'\n", __func__, spec);
 
-       for (data = spi_flash_params_table; data->name; data++) {
+       for (data = spi_flash_ids; data->name; data++) {
                len = strlen(data->name);
                if (idname_len != len)
                        continue;
@@ -172,7 +174,7 @@ static int sandbox_sf_probe(struct udevice *dev)
                        break;
        }
        if (!data->name) {
-               printf("sandbox_sf: unknown flash '%*s'\n", (int)idname_len,
+               printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len,
                       spec);
                ret = -EINVAL;
                goto error;
@@ -183,8 +185,7 @@ static int sandbox_sf_probe(struct udevice *dev)
 
        sbsf->fd = os_open(pdata->filename, 02);
        if (sbsf->fd == -1) {
-               free(sbsf);
-               printf("sandbox_sf: unable to open file '%s'\n",
+               printf("%s: unable to open file '%s'\n", __func__,
                       pdata->filename);
                ret = -EIO;
                goto error;
@@ -196,6 +197,7 @@ static int sandbox_sf_probe(struct udevice *dev)
        return 0;
 
  error:
+       debug("%s: Got error %d\n", __func__, ret);
        return ret;
 }
 
@@ -285,13 +287,10 @@ static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
                /* we only support erase here */
                if (sbsf->cmd == CMD_ERASE_CHIP) {
                        sbsf->erase_size = sbsf->data->sector_size *
-                               sbsf->data->nr_sectors;
+                               sbsf->data->n_sectors;
                } else if (sbsf->cmd == CMD_ERASE_4K && (flags & SECT_4K)) {
                        sbsf->erase_size = 4 << 10;
-               } else if (sbsf->cmd == CMD_ERASE_32K && (flags & SECT_32K)) {
-                       sbsf->erase_size = 32 << 10;
-               } else if (sbsf->cmd == CMD_ERASE_64K &&
-                          !(flags & (SECT_4K | SECT_32K))) {
+               } else if (sbsf->cmd == CMD_ERASE_64K && !(flags & SECT_4K)) {
                        sbsf->erase_size = 64 << 10;
                } else {
                        debug(" cmd unknown: %#x\n", sbsf->cmd);
@@ -358,7 +357,8 @@ static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
                        debug(" id: off:%u tx:", sbsf->off);
                        if (sbsf->off < IDCODE_LEN) {
                                /* Extract correct byte from ID 0x00aabbcc */
-                               id = sbsf->data->jedec >>
+                               id = ((JEDEC_MFR(sbsf->data) << 16) |
+                                       JEDEC_ID(sbsf->data)) >>
                                        (8 * (IDCODE_LEN - 1 - sbsf->off));
                        } else {
                                id = 0;
@@ -513,11 +513,9 @@ static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
 int sandbox_sf_ofdata_to_platdata(struct udevice *dev)
 {
        struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
-       const void *blob = gd->fdt_blob;
-       int node = dev->of_offset;
 
-       pdata->filename = fdt_getprop(blob, node, "sandbox,filename", NULL);
-       pdata->device_name = fdt_getprop(blob, node, "compatible", NULL);
+       pdata->filename = dev_read_string(dev, "sandbox,filename");
+       pdata->device_name = dev_read_string(dev, "compatible");
        if (!pdata->filename || !pdata->device_name) {
                debug("%s: Missing properties, filename=%s, device_name=%s\n",
                      __func__, pdata->filename, pdata->device_name);
@@ -550,6 +548,9 @@ static int sandbox_cmdline_cb_spi_sf(struct sandbox_state *state,
         * yet. Perhaps we can figure something out.
         */
        state->spi[bus][cs].spec = spec;
+       debug("%s:  Setting up spec '%s' for bus %ld, cs %ld\n", __func__,
+             spec, bus, cs);
+
        return 0;
 }
 SANDBOX_CMDLINE_OPT(spi_sf, 1, "connect a SPI flash: <bus>:<cs>:<id>:<file>");
@@ -566,16 +567,17 @@ int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
        strncpy(name, spec, sizeof(name) - 6);
        name[sizeof(name) - 6] = '\0';
        strcat(name, "-emul");
-       str = strdup(name);
-       if (!str)
-               return -ENOMEM;
        drv = lists_driver_lookup_name("sandbox_sf_emul");
        if (!drv) {
                puts("Cannot find sandbox_sf_emul driver\n");
                return -ENOENT;
        }
+       str = strdup(name);
+       if (!str)
+               return -ENOMEM;
        ret = device_bind(bus, drv, str, NULL, of_offset, &emul);
        if (ret) {
+               free(str);
                printf("Cannot create emul device for spec '%s' (err=%d)\n",
                       spec, ret);
                return ret;
@@ -587,6 +589,11 @@ int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
 
 void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
 {
+       struct udevice *dev;
+
+       dev = state->spi[busnum][cs].emul;
+       device_remove(dev, DM_REMOVE_NORMAL);
+       device_unbind(dev);
        state->spi[busnum][cs].emul = NULL;
 }
 
@@ -631,7 +638,7 @@ int sandbox_spi_get_emul(struct sandbox_state *state,
                debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
                      __func__, busnum, cs);
                ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
-                                          slave->of_offset, slave->name);
+                                          dev_of_offset(slave), slave->name);
                if (ret) {
                        debug("failed (err=%d)\n", ret);
                        return ret;
@@ -663,6 +670,8 @@ int dm_scan_other(bool pre_reloc_only)
                                              __func__, busnum, cs);
                                        return ret;
                                }
+                               debug("%s:  Setting up spec '%s' for bus %d, cs %d\n",
+                                     __func__, spec, busnum, cs);
                        }
                }
        }