diff mbox

[U-Boot,4/4] dfu: add SF backend

Message ID 1402524216-9999-4-git-send-email-swarren@wwwdotorg.org
State Accepted
Headers show

Commit Message

Stephen Warren June 11, 2014, 10:03 p.m. UTC
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

Comments

Łukasz Majewski June 20, 2014, 7:55 a.m. UTC | #1
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.
Fabio Estevam Sept. 21, 2015, 5:13 p.m. UTC | #2
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
Stephen Warren Sept. 21, 2015, 5:31 p.m. UTC | #3
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>
Fabio Estevam Sept. 21, 2015, 5:37 p.m. UTC | #4
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
Fabio Estevam Sept. 22, 2015, 1:50 a.m. UTC | #5
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
Fabio Estevam Sept. 22, 2015, 2:55 a.m. UTC | #6
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.
Stephen Warren Sept. 22, 2015, 3:23 a.m. UTC | #7
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?
Fabio Estevam Sept. 22, 2015, 3:37 a.m. UTC | #8
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
Łukasz Majewski Sept. 22, 2015, 7:23 a.m. UTC | #9
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
diff mbox

Patch

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_ */