diff options
author | Stephen Warren <swarren@nvidia.com> | 2014-06-12 02:03:36 +0400 |
---|---|---|
committer | Tom Rini <trini@ti.com> | 2014-08-09 19:16:59 +0400 |
commit | 6f12ebf6ea7bcae1b6d9ff090b5c1e7452be90e9 (patch) | |
tree | 6fc4e5046839b55b3ffc6d4e6cd9a8a215dc7bf8 /drivers/dfu | |
parent | cb7bd2e07e70aed7802e28619ce93d15d7ce10dc (diff) | |
download | u-boot-6f12ebf6ea7bcae1b6d9ff090b5c1e7452be90e9.tar.xz |
dfu: add SF backend
This allows SPI Flash to be programmed using DFU.
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Diffstat (limited to 'drivers/dfu')
-rw-r--r-- | drivers/dfu/Makefile | 1 | ||||
-rw-r--r-- | drivers/dfu/dfu.c | 3 | ||||
-rw-r--r-- | drivers/dfu/dfu_sf.c | 139 |
3 files changed, 143 insertions, 0 deletions
diff --git a/drivers/dfu/Makefile b/drivers/dfu/Makefile index def628dcdc..5cc535efdd 100644 --- a/drivers/dfu/Makefile +++ b/drivers/dfu/Makefile @@ -9,3 +9,4 @@ obj-$(CONFIG_DFU_FUNCTION) += dfu.o obj-$(CONFIG_DFU_MMC) += dfu_mmc.o obj-$(CONFIG_DFU_NAND) += dfu_nand.o obj-$(CONFIG_DFU_RAM) += dfu_ram.o +obj-$(CONFIG_DFU_SF) += dfu_sf.o diff --git a/drivers/dfu/dfu.c b/drivers/dfu/dfu.c index 897dfab77b..6cd3fbb58a 100644 --- a/drivers/dfu/dfu.c +++ b/drivers/dfu/dfu.c @@ -413,6 +413,9 @@ static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt, } else if (strcmp(interface, "ram") == 0) { if (dfu_fill_entity_ram(dfu, devstr, s)) return -1; + } else if (strcmp(interface, "sf") == 0) { + if (dfu_fill_entity_sf(dfu, devstr, s)) + return -1; } else { printf("%s: Device %s not (yet) supported!\n", __func__, interface); diff --git a/drivers/dfu/dfu_sf.c b/drivers/dfu/dfu_sf.c new file mode 100644 index 0000000000..91f6df220b --- /dev/null +++ b/drivers/dfu/dfu_sf.c @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved. + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <common.h> +#include <malloc.h> +#include <errno.h> +#include <div64.h> +#include <dfu.h> +#include <spi_flash.h> + +static long dfu_get_medium_size_sf(struct dfu_entity *dfu) +{ + return dfu->data.sf.size; +} + +static int dfu_read_medium_sf(struct dfu_entity *dfu, u64 offset, void *buf, + long *len) +{ + return spi_flash_read(dfu->data.sf.dev, offset, *len, buf); +} + +static int dfu_write_medium_sf(struct dfu_entity *dfu, + u64 offset, void *buf, long *len) +{ + int ret; + + ret = spi_flash_erase(dfu->data.sf.dev, offset, *len); + if (ret) + return ret; + + ret = spi_flash_write(dfu->data.sf.dev, offset, *len, buf); + if (ret) + return ret; + + return 0; +} + +static int dfu_flush_medium_sf(struct dfu_entity *dfu) +{ + return 0; +} + +static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu) +{ + return DFU_DEFAULT_POLL_TIMEOUT; +} + +static void dfu_free_entity_sf(struct dfu_entity *dfu) +{ + spi_flash_free(dfu->data.sf.dev); +} + +static struct spi_flash *parse_dev(char *devstr) +{ + unsigned int bus; + unsigned int cs; + unsigned int speed = CONFIG_SF_DEFAULT_SPEED; + unsigned int mode = CONFIG_SF_DEFAULT_MODE; + char *s, *endp; + struct spi_flash *dev; + + s = strsep(&devstr, ":"); + if (!s || !*s || (bus = simple_strtoul(s, &endp, 0), *endp)) { + printf("Invalid SPI bus %s\n", s); + return NULL; + } + + s = strsep(&devstr, ":"); + if (!s || !*s || (cs = simple_strtoul(s, &endp, 0), *endp)) { + printf("Invalid SPI chip-select %s\n", s); + return NULL; + } + + s = strsep(&devstr, ":"); + if (s && *s) { + speed = simple_strtoul(s, &endp, 0); + if (*endp || !speed) { + printf("Invalid SPI speed %s\n", s); + return NULL; + } + } + + s = strsep(&devstr, ":"); + if (s && *s) { + mode = simple_strtoul(s, &endp, 0); + if (*endp || mode > 3) { + printf("Invalid SPI mode %s\n", s); + return NULL; + } + } + + dev = spi_flash_probe(bus, cs, speed, mode); + if (!dev) { + printf("Failed to create SPI flash at %d:%d:%d:%d\n", + bus, cs, speed, mode); + return NULL; + } + + return dev; +} + +int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char *s) +{ + char *st; + + dfu->data.sf.dev = parse_dev(devstr); + if (!dfu->data.sf.dev) + return -ENODEV; + + dfu->dev_type = DFU_DEV_SF; + dfu->max_buf_size = dfu->data.sf.dev->sector_size; + + st = strsep(&s, " "); + if (!strcmp(st, "raw")) { + dfu->layout = DFU_RAW_ADDR; + dfu->data.sf.start = simple_strtoul(s, &s, 16); + s++; + dfu->data.sf.size = simple_strtoul(s, &s, 16); + } else { + printf("%s: Memory layout (%s) not supported!\n", __func__, st); + spi_flash_free(dfu->data.sf.dev); + return -1; + } + + dfu->get_medium_size = dfu_get_medium_size_sf; + dfu->read_medium = dfu_read_medium_sf; + dfu->write_medium = dfu_write_medium_sf; + dfu->flush_medium = dfu_flush_medium_sf; + dfu->poll_timeout = dfu_polltimeout_sf; + dfu->free_entity = dfu_free_entity_sf; + + /* initial state */ + dfu->inited = 0; + + return 0; +} |