[U-Boot] [PATCH 1/4] dfu: defer parsing of device string to IO backend

From: Stephen Warren swarren@nvidia.com
Devices are not all identified by a single integer. To support this, defer the parsing of the device string to the IO backed, so that it can apply the appropriate rules.
SPI devices are specified as controller:chip_select. SPI/SF support will be added soon.
MMC devices can also be specified as controller[.hwpart][:partition] in many commands, although we don't support that syntax in DFU.
Signed-off-by: Stephen Warren swarren@nvidia.com --- common/cmd_dfu.c | 3 +-- drivers/dfu/dfu.c | 20 ++++++++++---------- drivers/dfu/dfu_mmc.c | 21 ++++++++++++--------- drivers/dfu/dfu_nand.c | 2 +- drivers/dfu/dfu_ram.c | 2 +- include/dfu.h | 22 +++++++++++++--------- 6 files changed, 38 insertions(+), 32 deletions(-)
diff --git a/common/cmd_dfu.c b/common/cmd_dfu.c index 433bddd5d2bd..2633b30e556f 100644 --- a/common/cmd_dfu.c +++ b/common/cmd_dfu.c @@ -24,8 +24,7 @@ static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
int ret, i = 0;
- ret = dfu_init_env_entities(interface, simple_strtoul(devstring, - NULL, 10)); + ret = dfu_init_env_entities(interface, devstring); if (ret) goto done;
diff --git a/drivers/dfu/dfu.c b/drivers/dfu/dfu.c index 1bf66d0613c9..26d3b44e40f5 100644 --- a/drivers/dfu/dfu.c +++ b/drivers/dfu/dfu.c @@ -44,7 +44,7 @@ static int dfu_find_alt_num(const char *s) return ++i; }
-int dfu_init_env_entities(char *interface, int dev) +int dfu_init_env_entities(char *interface, char *devstr) { const char *str_env; char *env_bkp; @@ -57,7 +57,7 @@ int dfu_init_env_entities(char *interface, int dev) }
env_bkp = strdup(str_env); - ret = dfu_config_entities(env_bkp, interface, dev); + ret = dfu_config_entities(env_bkp, interface, devstr); if (ret) { error("DFU entities configuration failed!\n"); return ret; @@ -389,26 +389,25 @@ int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) }
static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt, - char *interface, int num) + char *interface, char *devstr) { char *st;
- debug("%s: %s interface: %s num: %d\n", __func__, s, interface, num); + debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr); st = strsep(&s, " "); strcpy(dfu->name, st);
- dfu->dev_num = num; dfu->alt = alt;
/* Specific for mmc device */ if (strcmp(interface, "mmc") == 0) { - if (dfu_fill_entity_mmc(dfu, s)) + if (dfu_fill_entity_mmc(dfu, devstr, s)) return -1; } else if (strcmp(interface, "nand") == 0) { - if (dfu_fill_entity_nand(dfu, s)) + if (dfu_fill_entity_nand(dfu, devstr, s)) return -1; } else if (strcmp(interface, "ram") == 0) { - if (dfu_fill_entity_ram(dfu, s)) + if (dfu_fill_entity_ram(dfu, devstr, s)) return -1; } else { printf("%s: Device %s not (yet) supported!\n", @@ -434,7 +433,7 @@ void dfu_free_entities(void) alt_num_cnt = 0; }
-int dfu_config_entities(char *env, char *interface, int num) +int dfu_config_entities(char *env, char *interface, char *devstr) { struct dfu_entity *dfu; int i, ret; @@ -457,7 +456,8 @@ int dfu_config_entities(char *env, char *interface, int num) for (i = 0; i < dfu_alt_num; i++) {
s = strsep(&env, ";"); - ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface, num); + ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface, + devstr); if (ret) return -1;
diff --git a/drivers/dfu/dfu_mmc.c b/drivers/dfu/dfu_mmc.c index 322bd8c5d2de..72fa03eedaec 100644 --- a/drivers/dfu/dfu_mmc.c +++ b/drivers/dfu/dfu_mmc.c @@ -27,7 +27,7 @@ static int mmc_access_part(struct dfu_entity *dfu, struct mmc *mmc, int part) if (part == mmc->part_num) return 0;
- ret = mmc_switch_part(dfu->dev_num, part); + ret = mmc_switch_part(dfu->data.mmc.dev_num, part); if (ret) { error("Cannot switch to partition %d\n", part); return ret; @@ -40,7 +40,7 @@ static int mmc_access_part(struct dfu_entity *dfu, struct mmc *mmc, int part) static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu, u64 offset, void *buf, long *len) { - struct mmc *mmc = find_mmc_device(dfu->dev_num); + struct mmc *mmc = find_mmc_device(dfu->data.mmc.dev_num); u32 blk_start, blk_count, n = 0; int ret, part_num_bkp = 0;
@@ -67,15 +67,15 @@ static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu, }
debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__, - op == DFU_OP_READ ? "MMC READ" : "MMC WRITE", dfu->dev_num, - blk_start, blk_count, buf); + op == DFU_OP_READ ? "MMC READ" : "MMC WRITE", + dfu->data.mmc.dev_num, blk_start, blk_count, buf); switch (op) { case DFU_OP_READ: - n = mmc->block_dev.block_read(dfu->dev_num, blk_start, + n = mmc->block_dev.block_read(dfu->data.mmc.dev_num, blk_start, blk_count, buf); break; case DFU_OP_WRITE: - n = mmc->block_dev.block_write(dfu->dev_num, blk_start, + n = mmc->block_dev.block_write(dfu->data.mmc.dev_num, blk_start, blk_count, buf); break; default: @@ -270,7 +270,7 @@ int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf, * 4th (optional): * mmcpart <num> (access to HW eMMC partitions) */ -int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s) +int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char *s) { const char *entity_type; size_t second_arg; @@ -281,6 +281,8 @@ int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s) const char *argv[3]; const char **parg = argv;
+ dfu->data.mmc.dev_num = simple_strtoul(devstr, NULL, 10); + for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) { *parg = strsep(&s, " "); if (*parg == NULL) { @@ -297,9 +299,10 @@ int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s) second_arg = simple_strtoul(argv[1], NULL, 0); third_arg = simple_strtoul(argv[2], NULL, 0);
- mmc = find_mmc_device(dfu->dev_num); + mmc = find_mmc_device(dfu->data.mmc.dev_num); if (mmc == NULL) { - error("Couldn't find MMC device no. %d.\n", dfu->dev_num); + error("Couldn't find MMC device no. %d.\n", + dfu->data.mmc.dev_num); return -ENODEV; }
diff --git a/drivers/dfu/dfu_nand.c b/drivers/dfu/dfu_nand.c index e1c9a8849246..f9ee18999ab7 100644 --- a/drivers/dfu/dfu_nand.c +++ b/drivers/dfu/dfu_nand.c @@ -179,7 +179,7 @@ unsigned int dfu_polltimeout_nand(struct dfu_entity *dfu) return DFU_DEFAULT_POLL_TIMEOUT; }
-int dfu_fill_entity_nand(struct dfu_entity *dfu, char *s) +int dfu_fill_entity_nand(struct dfu_entity *dfu, char *devstr, char *s) { char *st; int ret, dev, part; diff --git a/drivers/dfu/dfu_ram.c b/drivers/dfu/dfu_ram.c index b6c6e60c443c..e094a946f6be 100644 --- a/drivers/dfu/dfu_ram.c +++ b/drivers/dfu/dfu_ram.c @@ -52,7 +52,7 @@ static int dfu_read_medium_ram(struct dfu_entity *dfu, u64 offset, return dfu_transfer_medium_ram(DFU_OP_READ, dfu, offset, buf, len); }
-int dfu_fill_entity_ram(struct dfu_entity *dfu, char *s) +int dfu_fill_entity_ram(struct dfu_entity *dfu, char *devstr, char *s) { char *st;
diff --git a/include/dfu.h b/include/dfu.h index df720310f2cc..21390aa9b7b3 100644 --- a/include/dfu.h +++ b/include/dfu.h @@ -39,6 +39,8 @@ enum dfu_op { };
struct mmc_internal_data { + int dev_num; + /* RAW programming */ unsigned int lba_start; unsigned int lba_size; @@ -87,7 +89,6 @@ struct dfu_entity { char name[DFU_NAME_SIZE]; int alt; void *dev_private; - int dev_num; enum dfu_device_type dev_type; enum dfu_layout layout;
@@ -125,7 +126,7 @@ struct dfu_entity { unsigned int inited:1; };
-int dfu_config_entities(char *s, char *interface, int num); +int dfu_config_entities(char *s, char *interface, char *devstr); void dfu_free_entities(void); void dfu_show_entities(void); int dfu_get_alt_number(void); @@ -136,7 +137,7 @@ char *dfu_extract_token(char** e, int *n); void dfu_trigger_reset(void); int dfu_get_alt(char *name); bool dfu_reset(void); -int dfu_init_env_entities(char *interface, int dev); +int dfu_init_env_entities(char *interface, char *devstr); unsigned char *dfu_get_buf(void); unsigned char *dfu_free_buf(void); unsigned long dfu_get_buf_size(void); @@ -146,9 +147,10 @@ int dfu_write(struct dfu_entity *de, void *buf, int size, int blk_seq_num); int dfu_flush(struct dfu_entity *de, void *buf, int size, int blk_seq_num); /* Device specific */ #ifdef CONFIG_DFU_MMC -extern int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s); +extern int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char *s); #else -static inline int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s) +static inline int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, + char *s) { puts("MMC support not available!\n"); return -1; @@ -156,9 +158,10 @@ static inline int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s) #endif
#ifdef CONFIG_DFU_NAND -extern int dfu_fill_entity_nand(struct dfu_entity *dfu, char *s); +extern int dfu_fill_entity_nand(struct dfu_entity *dfu, char *devstr, char *s); #else -static inline int dfu_fill_entity_nand(struct dfu_entity *dfu, char *s) +static inline int dfu_fill_entity_nand(struct dfu_entity *dfu, char *devstr, + char *s) { puts("NAND support not available!\n"); return -1; @@ -166,9 +169,10 @@ static inline int dfu_fill_entity_nand(struct dfu_entity *dfu, char *s) #endif
#ifdef CONFIG_DFU_RAM -extern int dfu_fill_entity_ram(struct dfu_entity *dfu, char *s); +extern int dfu_fill_entity_ram(struct dfu_entity *dfu, char *devstr, char *s); #else -static inline int dfu_fill_entity_ram(struct dfu_entity *dfu, char *s) +static inline int dfu_fill_entity_ram(struct dfu_entity *dfu, char *devstr, + char *s) { puts("RAM support not available!\n"); return -1;

From: Stephen Warren swarren@nvidia.com
CONFIG_SYS_DFU_DATA_BUF_SIZE may be large to allow for FAT/ext layouts to transfer large files. However, this means that individual write operations will take a long time. Allow backends to specify a maximum buffer size, so that each write operation is limited to a smaller data block. This prevents the DFU protocol from timing out when e.g. writing to SPI flash. I would guess that NAND might benefit from setting this value too, but I can't test that.
Signed-off-by: Stephen Warren swarren@nvidia.com --- drivers/dfu/dfu.c | 13 ++++++++----- drivers/usb/gadget/f_thor.c | 5 +++-- include/dfu.h | 3 ++- 3 files changed, 13 insertions(+), 8 deletions(-)
diff --git a/drivers/dfu/dfu.c b/drivers/dfu/dfu.c index 26d3b44e40f5..b8d382d9b5df 100644 --- a/drivers/dfu/dfu.c +++ b/drivers/dfu/dfu.c @@ -82,7 +82,7 @@ unsigned long dfu_get_buf_size(void) return dfu_buf_size; }
-unsigned char *dfu_get_buf(void) +unsigned char *dfu_get_buf(struct dfu_entity *dfu) { char *s;
@@ -92,6 +92,8 @@ unsigned char *dfu_get_buf(void) s = getenv("dfu_bufsiz"); dfu_buf_size = s ? (unsigned long)simple_strtol(s, NULL, 16) : CONFIG_SYS_DFU_DATA_BUF_SIZE; + if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size) + dfu_buf_size = dfu->max_buf_size;
dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size); if (dfu_buf == NULL) @@ -194,10 +196,10 @@ int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) dfu->offset = 0; dfu->bad_skip = 0; dfu->i_blk_seq_num = 0; - dfu->i_buf_start = dfu_get_buf(); + dfu->i_buf_start = dfu_get_buf(dfu); if (dfu->i_buf_start == NULL) return -ENOMEM; - dfu->i_buf_end = dfu_get_buf() + dfu_buf_size; + dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size; dfu->i_buf = dfu->i_buf_start;
dfu->inited = 1; @@ -318,7 +320,7 @@ int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
if (!dfu->inited) { - dfu->i_buf_start = dfu_get_buf(); + dfu->i_buf_start = dfu_get_buf(dfu); if (dfu->i_buf_start == NULL) return -ENOMEM;
@@ -342,7 +344,7 @@ int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) dfu->i_blk_seq_num = 0; dfu->crc = 0; dfu->offset = 0; - dfu->i_buf_end = dfu_get_buf() + dfu_buf_size; + dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size; dfu->i_buf = dfu->i_buf_start; dfu->b_left = 0;
@@ -398,6 +400,7 @@ static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt, strcpy(dfu->name, st);
dfu->alt = alt; + dfu->max_buf_size = 0;
/* Specific for mmc device */ if (strcmp(interface, "mmc") == 0) { diff --git a/drivers/usb/gadget/f_thor.c b/drivers/usb/gadget/f_thor.c index 28f215e07c6e..c5f10a054d95 100644 --- a/drivers/usb/gadget/f_thor.c +++ b/drivers/usb/gadget/f_thor.c @@ -142,7 +142,8 @@ static long long int download_head(unsigned long long total, int *cnt) { long long int rcv_cnt = 0, left_to_rcv, ret_rcv; - void *transfer_buffer = dfu_get_buf(); + struct dfu_entity *dfu_entity = dfu_get_entity(alt_setting_num); + void *transfer_buffer = dfu_get_buf(dfu_entity); void *buf = transfer_buffer; int usb_pkt_cnt = 0, ret;
@@ -205,7 +206,7 @@ static long long int download_head(unsigned long long total, static int download_tail(long long int left, int cnt) { struct dfu_entity *dfu_entity = dfu_get_entity(alt_setting_num); - void *transfer_buffer = dfu_get_buf(); + void *transfer_buffer = dfu_get_buf(dfu_entity); int ret;
debug("%s: left: %llu cnt: %d\n", __func__, left, cnt); diff --git a/include/dfu.h b/include/dfu.h index 21390aa9b7b3..d5562dcb37d1 100644 --- a/include/dfu.h +++ b/include/dfu.h @@ -91,6 +91,7 @@ struct dfu_entity { void *dev_private; enum dfu_device_type dev_type; enum dfu_layout layout; + unsigned long max_buf_size;
union { struct mmc_internal_data mmc; @@ -138,7 +139,7 @@ void dfu_trigger_reset(void); int dfu_get_alt(char *name); bool dfu_reset(void); int dfu_init_env_entities(char *interface, char *devstr); -unsigned char *dfu_get_buf(void); +unsigned char *dfu_get_buf(struct dfu_entity *dfu); unsigned char *dfu_free_buf(void); unsigned long dfu_get_buf_size(void);

Hi Stephen,
From: Stephen Warren swarren@nvidia.com
CONFIG_SYS_DFU_DATA_BUF_SIZE may be large to allow for FAT/ext layouts to transfer large files. However, this means that individual write operations will take a long time. Allow backends to specify a maximum buffer size, so that each write operation is limited to a smaller data block. This prevents the DFU protocol from timing out when e.g. writing to SPI flash. I would guess that NAND might benefit from setting this value too, but I can't test that.
Signed-off-by: Stephen Warren swarren@nvidia.com
Applied to u-boot-dfu
Thanks for the patch.

From: Stephen Warren swarren@nvidia.com
This allows the backend to free any resources allocated during the relevant dfu_fill_entity_*() call. This will soon be used by the SF backend.
Signed-off-by: Stephen Warren swarren@nvidia.com --- drivers/dfu/dfu.c | 3 +++ include/dfu.h | 2 ++ 2 files changed, 5 insertions(+)
diff --git a/drivers/dfu/dfu.c b/drivers/dfu/dfu.c index b8d382d9b5df..897dfab77be6 100644 --- a/drivers/dfu/dfu.c +++ b/drivers/dfu/dfu.c @@ -401,6 +401,7 @@ static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
dfu->alt = alt; dfu->max_buf_size = 0; + dfu->free_entity = NULL;
/* Specific for mmc device */ if (strcmp(interface, "mmc") == 0) { @@ -427,6 +428,8 @@ void dfu_free_entities(void)
list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) { list_del(&dfu->list); + if (dfu->free_entity) + dfu->free_entity(dfu); t = dfu; } if (t) diff --git a/include/dfu.h b/include/dfu.h index d5562dcb37d1..43814b38ec6d 100644 --- a/include/dfu.h +++ b/include/dfu.h @@ -110,6 +110,8 @@ struct dfu_entity { int (*flush_medium)(struct dfu_entity *dfu); unsigned int (*poll_timeout)(struct dfu_entity *dfu);
+ void (*free_entity)(struct dfu_entity *dfu); + struct list_head list;
/* on the fly state */

Hi Stephen,
From: Stephen Warren swarren@nvidia.com
This allows the backend to free any resources allocated during the relevant dfu_fill_entity_*() call. This will soon be used by the SF backend.
Signed-off-by: Stephen Warren swarren@nvidia.com
Applied to u-boot-dfu
Thanks for the patch.

From: Stephen Warren swarren@nvidia.com
This allows SPI Flash to be programmed using DFU.
Signed-off-by: Stephen Warren swarren@nvidia.com --- drivers/dfu/Makefile | 1 + drivers/dfu/dfu.c | 3 ++ drivers/dfu/dfu_sf.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/dfu.h | 22 ++++++++ 4 files changed, 165 insertions(+) create mode 100644 drivers/dfu/dfu_sf.c
diff --git a/drivers/dfu/Makefile b/drivers/dfu/Makefile index def628dcdcc4..5cc535efdd47 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 897dfab77be6..6cd3fbb58ae4 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 000000000000..91f6df220b1d --- /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; +} diff --git a/include/dfu.h b/include/dfu.h index 43814b38ec6d..7e0a99908ca4 100644 --- a/include/dfu.h +++ b/include/dfu.h @@ -14,6 +14,7 @@ #include <common.h> #include <linux/list.h> #include <mmc.h> +#include <spi_flash.h> #include <linux/usb/composite.h>
enum dfu_device_type { @@ -21,6 +22,7 @@ enum dfu_device_type { DFU_DEV_ONENAND, DFU_DEV_NAND, DFU_DEV_RAM, + DFU_DEV_SF, };
enum dfu_layout { @@ -70,6 +72,14 @@ struct ram_internal_data { unsigned int size; };
+struct sf_internal_data { + struct spi_flash *dev; + + /* RAW programming */ + u64 start; + u64 size; +}; + #define DFU_NAME_SIZE 32 #define DFU_CMD_BUF_SIZE 128 #ifndef CONFIG_SYS_DFU_DATA_BUF_SIZE @@ -97,6 +107,7 @@ struct dfu_entity { struct mmc_internal_data mmc; struct nand_internal_data nand; struct ram_internal_data ram; + struct sf_internal_data sf; } data;
long (*get_medium_size)(struct dfu_entity *dfu); @@ -182,5 +193,16 @@ static inline int dfu_fill_entity_ram(struct dfu_entity *dfu, char *devstr, } #endif
+#ifdef CONFIG_DFU_SF +extern int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char *s); +#else +static inline int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, + char *s) +{ + puts("SF support not available!\n"); + return -1; +} +#endif + int dfu_add(struct usb_configuration *c); #endif /* __DFU_ENTITY_H_ */

Hi Stephen,
From: Stephen Warren swarren@nvidia.com
This allows SPI Flash to be programmed using DFU.
Signed-off-by: Stephen Warren swarren@nvidia.com
Applied to u-boot-dfu
Thanks for the patch.

Hi Stephen,
On Wed, Jun 11, 2014 at 7:03 PM, Stephen Warren swarren@wwwdotorg.org wrote:
From: Stephen Warren swarren@nvidia.com
This allows SPI Flash to be programmed using DFU.
Signed-off-by: Stephen Warren swarren@nvidia.com
Is this still working in mainline U-boot?
I am getting the following error on the PC host side:
sudo dfu-util -D u-boot.imx -a u-boot dfu-util 0.8
Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc. Copyright 2010-2014 Tormod Volden and Stefan Schmidt This program is Free Software and has ABSOLUTELY NO WARRANTY Please report bugs to dfu-util@lists.gnumonks.org
dfu-util: Invalid DFU suffix signature dfu-util: A valid DFU suffix will be required in a future dfu-util release!!! Opening DFU capable USB device... ID 0525:a4a5 Run-time device DFU version 0110 Claiming USB DFU Interface... Setting Alternate Setting #0 ... Determining device status: state = dfuIDLE, status = 0 dfuIDLE, continuing DFU mode device DFU version 0110 Device returned transfer size 4096 Copying data from PC to DFU device Download [=========================] 100% 478208 bytes Download done. state(7) = dfuMANIFEST, status(0) = No error condition is present state(10) = dfuERROR, status(14) = Something went wrong, but the device does not know what it was Done!
On the target side:
=> dfu 0 sf 0:0:25000000:0 SF: Detected M25P32 with page size 256 Bytes, erase size 64 KiB, total 4 MiB
Any ideas?
Thanks,
Fabio Estevam

On 09/21/2015 11:13 AM, Fabio Estevam wrote:
Hi Stephen,
On Wed, Jun 11, 2014 at 7:03 PM, Stephen Warren swarren@wwwdotorg.org wrote:
From: Stephen Warren swarren@nvidia.com
This allows SPI Flash to be programmed using DFU.
Signed-off-by: Stephen Warren swarren@nvidia.com
Is this still working in mainline U-boot?
I haven't tested SF support recently (only MMC). However, it should still work:-) Are you using ci_udc? If so, make sure you have this very recent patch:
commit b337b3b2a53f112a217f4bd31307b02f830bb787 Author: Stephen Warren swarren@nvidia.com Date: Fri Sep 11 17:10:02 2015 -0600
usb: ci_udc: fix emissions of ZLPs Commit 6a132416359e "ci_udc: Update the ci_udc driver to support bulk transfers" caused the value of "len" to change without updating subsquent users of that variable in ci_ep_submit_next_request(). This caused the code that detects when to emit ZLPs (Zero Length Packets) never to trigger, which in turn caused host timeouts when a ZLP was required, which in turn broke tests/dfu/, even despite the assertion in that commit's description that "These changes are tested for both the DFU and lthor." Fix this by modifying the added dtd iteration code not to modify "len", but rather to keep state in a separate variable. Rename the variables while we're at it so they describe their purpose better. Fixes: 6a132416359e ("ci_udc: Update the ci_udc driver to support bulk transfers") Cc: Siva Durga Prasad Paladugu <siva.durga.paladugu@xilinx.com> Signed-off-by: Stephen Warren <swarren@nvidia.com>

On Mon, Sep 21, 2015 at 2:31 PM, Stephen Warren swarren@wwwdotorg.org wrote:
I haven't tested SF support recently (only MMC). However, it should still work:-) Are you using ci_udc? If so, make sure you have this very recent patch:
commit b337b3b2a53f112a217f4bd31307b02f830bb787 Author: Stephen Warren swarren@nvidia.com Date: Fri Sep 11 17:10:02 2015 -0600
usb: ci_udc: fix emissions of ZLPs
Yes, I am using ci_udc and this patch is applied.
I am able to flash SD/eMMC via DFU but SPI does not work here.
Thanks

Stephen,
On Mon, Sep 21, 2015 at 2:37 PM, Fabio Estevam festevam@gmail.com wrote:
On Mon, Sep 21, 2015 at 2:31 PM, Stephen Warren swarren@wwwdotorg.org wrote:
I haven't tested SF support recently (only MMC). However, it should still work:-) Are you using ci_udc? If so, make sure you have this very recent patch:
commit b337b3b2a53f112a217f4bd31307b02f830bb787 Author: Stephen Warren swarren@nvidia.com Date: Fri Sep 11 17:10:02 2015 -0600
usb: ci_udc: fix emissions of ZLPs
Yes, I am using ci_udc and this patch is applied.
Do you recall what you used as 'dfu_alt_info' variable when flashing SPI via DFU?
Thanks

On Mon, Sep 21, 2015 at 10:50 PM, Fabio Estevam festevam@gmail.com wrote:
Stephen,
On Mon, Sep 21, 2015 at 2:37 PM, Fabio Estevam festevam@gmail.com wrote:
On Mon, Sep 21, 2015 at 2:31 PM, Stephen Warren swarren@wwwdotorg.org wrote:
I haven't tested SF support recently (only MMC). However, it should still work:-) Are you using ci_udc? If so, make sure you have this very recent patch:
commit b337b3b2a53f112a217f4bd31307b02f830bb787 Author: Stephen Warren swarren@nvidia.com Date: Fri Sep 11 17:10:02 2015 -0600
usb: ci_udc: fix emissions of ZLPs
Yes, I am using ci_udc and this patch is applied.
Do you recall what you used as 'dfu_alt_info' variable when flashing SPI via DFU?
Nevermind. I fixed the issue and will submit the patch shortly.

On 09/21/2015 08:55 PM, Fabio Estevam wrote:
On Mon, Sep 21, 2015 at 10:50 PM, Fabio Estevam festevam@gmail.com wrote:
Stephen,
On Mon, Sep 21, 2015 at 2:37 PM, Fabio Estevam festevam@gmail.com wrote:
On Mon, Sep 21, 2015 at 2:31 PM, Stephen Warren swarren@wwwdotorg.org wrote:
I haven't tested SF support recently (only MMC). However, it should still work:-) Are you using ci_udc? If so, make sure you have this very recent patch:
commit b337b3b2a53f112a217f4bd31307b02f830bb787 Author: Stephen Warren swarren@nvidia.com Date: Fri Sep 11 17:10:02 2015 -0600
usb: ci_udc: fix emissions of ZLPs
Yes, I am using ci_udc and this patch is applied.
Do you recall what you used as 'dfu_alt_info' variable when flashing SPI via DFU?
Nevermind. I fixed the issue and will submit the patch shortly.
Oh good:-)
I was going to give you my dfu_alt_info, but it's at work right now, so I was waiting until tomorrow. I did try to repro this, but I was having a lot of trouble with both the two boards I have that have SPI, so wasn't able to test yet.
One thing I did notice: dfu_sf.c's parsing code doesn't work with multiple entries in dfu_alt_info (e.g. for different partitions) since the parsing of the device parameter (to the dfu command; not part of dfu_info) modifies that parameter so it can't be parsed the second time around. Since you're set up to test this, perhaps you could fix it too? I assume all it'd need is to add a strdup()/free() to the start/end of the alt info parsing function?

On Tue, Sep 22, 2015 at 12:23 AM, Stephen Warren swarren@wwwdotorg.org wrote:
Oh good:-)
I was going to give you my dfu_alt_info, but it's at work right now, so I was waiting until tomorrow. I did try to repro this, but I was having a lot of trouble with both the two boards I have that have SPI, so wasn't able to test yet.
One thing I did notice: dfu_sf.c's parsing code doesn't work with multiple entries in dfu_alt_info (e.g. for different partitions) since the parsing of the device parameter (to the dfu command; not part of dfu_info) modifies that parameter so it can't be parsed the second time around. Since you're set up to test this, perhaps you could fix it too? I assume all it'd need is to add a strdup()/free() to the start/end of the alt info parsing function?
Yes, I noticed a problem here on parsing dfu_alt_info. In my case I need to flash the u-boot binary at offset 0x400 of the SPI NOR, but I was not able to find the correct syntax for this. I have tried:
dfu_alt_info=u-boot raw 0x400
,but I am not familiar with the dfu_alt_info syntax, so maybe this needs some adjustment.
For the eMMC I am able to flash it at offset 0x400 using the dfu_alt_info from warp.h: "dfu_alt_info=boot raw 0x2 0x400 mmcpart 1\0" \ ---> I am not sure what the 0x2 means.
At least the SPI NOR is getting flashed now (at offset 0x0 instead of 0x400) with the fix I have made. Will submit the fix soon.
About the incorrect offset, it is a different issue and I will investigate more tomorrow.
Thanks

Hi Fabio,
On Tue, Sep 22, 2015 at 12:23 AM, Stephen Warren swarren@wwwdotorg.org wrote:
Oh good:-)
Nice to hear, that the bug is fixed.
I was going to give you my dfu_alt_info, but it's at work right now, so I was waiting until tomorrow. I did try to repro this, but I was having a lot of trouble with both the two boards I have that have SPI, so wasn't able to test yet.
One thing I did notice: dfu_sf.c's parsing code doesn't work with multiple entries in dfu_alt_info (e.g. for different partitions) since the parsing of the device parameter (to the dfu command; not part of dfu_info) modifies that parameter so it can't be parsed the second time around. Since you're set up to test this, perhaps you could fix it too? I assume all it'd need is to add a strdup()/free() to the start/end of the alt info parsing function?
Yes, I noticed a problem here on parsing dfu_alt_info. In my case I need to flash the u-boot binary at offset 0x400 of the SPI NOR, but I was not able to find the correct syntax for this. I have tried:
dfu_alt_info=u-boot raw 0x400
For some dfu_alt_info example, please refer to ./include/configs/trats[2].h files.
,but I am not familiar with the dfu_alt_info syntax, so maybe this needs some adjustment.
For the eMMC I am able to flash it at offset 0x400 using the dfu_alt_info from warp.h: "dfu_alt_info=boot raw 0x2 0x400 mmcpart 1\0" \ ---> I am not sure what the 0x2 means.
As fair as I remember - 0x2 is the starting LBA number for eMMC. 0x400 is the number of consecutive LBAs to write to the medium.
At least the SPI NOR is getting flashed now (at offset 0x0 instead of 0x400) with the fix I have made. Will submit the fix soon.
About the incorrect offset, it is a different issue and I will investigate more tomorrow.
Since I don't have any target with SPI Flash, I can only guess or provide analogy to eMMC.
In the eMMC case it is also possible to flash "raw" eMMC with passing starting LBA and number of LBAs for write.
In ./include/configs/trats.h : dfu_alt_info="params.bin raw 0x38 0x8"
Thanks

Hi Stephen,
From: Stephen Warren swarren@nvidia.com
Devices are not all identified by a single integer. To support this, defer the parsing of the device string to the IO backed, so that it can apply the appropriate rules.
SPI devices are specified as controller:chip_select. SPI/SF support will be added soon.
MMC devices can also be specified as controller[.hwpart][:partition] in many commands, although we don't support that syntax in DFU.
Signed-off-by: Stephen Warren swarren@nvidia.com
Applied to u-boot-dfu
Thanks for the patch.
participants (3)
-
Fabio Estevam
-
Lukasz Majewski
-
Stephen Warren