[U-Boot Patch v2 0/4] Fix currently available support for flash on HiFive Unleashed

Currently device ID for flash mounted on HiFive Unleashed is added to U-Boot. Also there are few patches about to go in mainline (Thanks to Jagan Tekki and Bin Meng).
This series addresses few issues discussed there: Patch 1:Add num-cs to device tree which is used by spi-uclass to detect valid chip select numbers Patch 2:Handles valid chip selects only. Flash device is now detected only on chip select 0. Patch 3:Over-ride spi tx/rx width specified in hifive-unleshed-a00.dts file from 4 to 1 in corresponding -u-boot.dtsi This series is based on mainline commit c05b38df477a ("common: fix regression on block cache init") and two below mentioned patches from [1] [U-Boot,v2,4/5] riscv: dts: hifive-unleashed-a00: Add -u-boot.dtsi [U-Boot,v2,5/5] sifive: fu540: Enable spi-nor flash support
[1] https://patchwork.ozlabs.org/patch/1177979/
The above series is available for testing here[2] [2] https://github.com/sagsifive/u-boot/tree/dev/sagark/test_spi-nor_v2
Change History: V1-V2: 1. Dropped 6th and 7th patch sent in V1 from this series so as to separate out spi-nor related changes in different series and avoid adding TODO fixes to spi-nor-core framework. 2. Removed patch to include -uboot.dts, as it gets automatically included. 3. Over-ride tx/rx width from hifive-unleashed-a00.dts using hifive -unleashed-a00-u-boot.dtsi 4. Print fdt base address in board info for debugging.
V1: First version of series
================Log for reference===================== Flash detection only at valid Chip select => sf probe 0:0 SF: Detected is25wp256 with page size 256 Bytes, erase size 4 KiB, total 32 MiB => sf probe 0:1 Invalid cs number = 1 Failed to initialize SPI flash at 0:1 (error -22) => sf probe 0:2 Invalid cs number = 2 Failed to initialize SPI flash at 0:2 (error -22) => sf probe 0:0 SF: Detected is25wp256 with page size 256 Bytes, erase size 4 KiB, total 32 MiB
=>---------------------------------------------------------------- Full flash memory erase/write/read/validate => mw 0x80600000 0x12348765 0x800000 => sf erase 0x0 0x2000000 SF: 33554432 bytes @ 0x0 Erased: OK => sf write 0x80600000 0x0 0x2000000 device 0 whole chip SF: 33554432 bytes @ 0x0 Written: OK => sf read 0x82600000 0x0 0x2000000 device 0 whole chip SF: 33554432 bytes @ 0x0 Read: OK => cmp.b 0x80600000 0x82600000 0x2000000 Total of 33554432 byte(s) were the same =>----------------------------------------------------------------
Sagar Shrikant Kadam (4): fu540: dtsi: spi: add num-cs info to device tree spi: fu540: add claim and release method to spi-sifive.c dts: u-boot.dtsi: override flash tx-rx width bdinfo: fu540: print fdt base address for debugging
arch/riscv/dts/fu540-c000.dtsi | 3 +++ arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi | 8 ++++++ cmd/bdinfo.c | 1 + drivers/spi/spi-sifive.c | 36 +++++++++++++++++++++++++ 4 files changed, 48 insertions(+)

Add the number of chip select information to spi nodes which can be used by spi-uclass for error handling if invalid cs number passed from command.
Signed-off-by: Sagar Shrikant Kadam sagar.kadam@sifive.com --- arch/riscv/dts/fu540-c000.dtsi | 3 +++ 1 file changed, 3 insertions(+)
diff --git a/arch/riscv/dts/fu540-c000.dtsi b/arch/riscv/dts/fu540-c000.dtsi index afa43c7..9c6ab21 100644 --- a/arch/riscv/dts/fu540-c000.dtsi +++ b/arch/riscv/dts/fu540-c000.dtsi @@ -191,6 +191,7 @@ clocks = <&prci PRCI_CLK_TLCLK>; #address-cells = <1>; #size-cells = <0>; + num-cs = <1>; status = "disabled"; }; qspi1: spi@10041000 { @@ -202,6 +203,7 @@ clocks = <&prci PRCI_CLK_TLCLK>; #address-cells = <1>; #size-cells = <0>; + num-cs = <1>; status = "disabled"; }; qspi2: spi@10050000 { @@ -212,6 +214,7 @@ clocks = <&prci PRCI_CLK_TLCLK>; #address-cells = <1>; #size-cells = <0>; + num-cs = <1>; status = "disabled"; }; eth0: ethernet@10090000 {

Hi Sagar,
On Wed, Jan 29, 2020 at 2:02 AM Sagar Shrikant Kadam sagar.kadam@sifive.com wrote:
Add the number of chip select information to spi nodes which can be used by spi-uclass for error handling if invalid cs number passed from command.
Signed-off-by: Sagar Shrikant Kadam sagar.kadam@sifive.com
arch/riscv/dts/fu540-c000.dtsi | 3 +++ 1 file changed, 3 insertions(+)
diff --git a/arch/riscv/dts/fu540-c000.dtsi b/arch/riscv/dts/fu540-c000.dtsi index afa43c7..9c6ab21 100644 --- a/arch/riscv/dts/fu540-c000.dtsi +++ b/arch/riscv/dts/fu540-c000.dtsi @@ -191,6 +191,7 @@ clocks = <&prci PRCI_CLK_TLCLK>; #address-cells = <1>; #size-cells = <0>;
num-cs = <1>;
Why is this necessary? I can't find the codes that handle the num-cs property. In the SiFive SPI driver, num_cs is determined from register SIFIVE_SPI_REG_CSDEF.
status = "disabled"; }; qspi1: spi@10041000 {
@@ -202,6 +203,7 @@ clocks = <&prci PRCI_CLK_TLCLK>; #address-cells = <1>; #size-cells = <0>;
num-cs = <1>; status = "disabled"; }; qspi2: spi@10050000 {
@@ -212,6 +214,7 @@ clocks = <&prci PRCI_CLK_TLCLK>; #address-cells = <1>; #size-cells = <0>;
num-cs = <1>; status = "disabled"; }; eth0: ethernet@10090000 {
--
Regards, Bin

Hello Bin,
-----Original Message----- From: Bin Meng bmeng.cn@gmail.com Sent: Tuesday, February 4, 2020 5:43 PM To: Sagar Kadam sagar.kadam@sifive.com Cc: U-Boot Mailing List u-boot@lists.denx.de; Rick Chen rick@andestech.com; Paul Walmsley ( Sifive) paul.walmsley@sifive.com; Jagan Teki jagan@amarulasolutions.com; Anup Patel anup.patel@wdc.com Subject: Re: [U-Boot Patch v2 1/4] fu540: dtsi: spi: add num-cs info to device tree
Hi Sagar,
On Wed, Jan 29, 2020 at 2:02 AM Sagar Shrikant Kadam sagar.kadam@sifive.com wrote:
Add the number of chip select information to spi nodes which can be used by spi-uclass for error handling if invalid cs number passed from command.
Signed-off-by: Sagar Shrikant Kadam sagar.kadam@sifive.com
arch/riscv/dts/fu540-c000.dtsi | 3 +++ 1 file changed, 3 insertions(+)
diff --git a/arch/riscv/dts/fu540-c000.dtsi b/arch/riscv/dts/fu540-c000.dtsi index afa43c7..9c6ab21 100644 --- a/arch/riscv/dts/fu540-c000.dtsi +++ b/arch/riscv/dts/fu540-c000.dtsi @@ -191,6 +191,7 @@ clocks = <&prci PRCI_CLK_TLCLK>; #address-cells = <1>; #size-cells = <0>;
num-cs = <1>;
Why is this necessary? I can't find the codes that handle the num-cs property.
The code handling num-cs is a part of patch 2. I intended to keep it separate thinking better to isolate dt and c files patches. Please let me know if I need to keep it together.
In the SiFive SPI driver, num_cs is determined from register SIFIVE_SPI_REG_CSDEF.
The SiFive SPI driver does read the num_cs defined in the register you mentioned. but it's hard defined with cs_width, eg: QSPI0 = 1, QSPI1 = 4, QSPI2 = 1. I have added the option after sifive_spi_init_hw to read from device tree as well so that if the user want's to change the chip select's (less than that defined in SIFIVE_SPI_REG_CSDEF) then it can be done in hifive-unleashed-a00.dtsi Please let me know your thoughts over here.
BR, Sagar
status = "disabled"; }; qspi1: spi@10041000 {
@@ -202,6 +203,7 @@ clocks = <&prci PRCI_CLK_TLCLK>; #address-cells = <1>; #size-cells = <0>;
num-cs = <1>; status = "disabled"; }; qspi2: spi@10050000 {
@@ -212,6 +214,7 @@ clocks = <&prci PRCI_CLK_TLCLK>; #address-cells = <1>; #size-cells = <0>;
num-cs = <1>; status = "disabled"; }; eth0: ethernet@10090000 {
--
Regards, Bin

Hi Sagar,
On Thu, Feb 6, 2020 at 2:17 AM Sagar Kadam sagar.kadam@sifive.com wrote:
Hello Bin,
-----Original Message----- From: Bin Meng bmeng.cn@gmail.com Sent: Tuesday, February 4, 2020 5:43 PM To: Sagar Kadam sagar.kadam@sifive.com Cc: U-Boot Mailing List u-boot@lists.denx.de; Rick Chen rick@andestech.com; Paul Walmsley ( Sifive) paul.walmsley@sifive.com; Jagan Teki jagan@amarulasolutions.com; Anup Patel anup.patel@wdc.com Subject: Re: [U-Boot Patch v2 1/4] fu540: dtsi: spi: add num-cs info to device tree
Hi Sagar,
On Wed, Jan 29, 2020 at 2:02 AM Sagar Shrikant Kadam sagar.kadam@sifive.com wrote:
Add the number of chip select information to spi nodes which can be used by spi-uclass for error handling if invalid cs number passed from command.
Signed-off-by: Sagar Shrikant Kadam sagar.kadam@sifive.com
arch/riscv/dts/fu540-c000.dtsi | 3 +++ 1 file changed, 3 insertions(+)
diff --git a/arch/riscv/dts/fu540-c000.dtsi b/arch/riscv/dts/fu540-c000.dtsi index afa43c7..9c6ab21 100644 --- a/arch/riscv/dts/fu540-c000.dtsi +++ b/arch/riscv/dts/fu540-c000.dtsi @@ -191,6 +191,7 @@ clocks = <&prci PRCI_CLK_TLCLK>; #address-cells = <1>; #size-cells = <0>;
num-cs = <1>;
Why is this necessary? I can't find the codes that handle the num-cs property.
The code handling num-cs is a part of patch 2. I intended to keep it separate thinking better to isolate dt and c files patches. Please let me know if I need to keep it together.
In the SiFive SPI driver, num_cs is determined from register SIFIVE_SPI_REG_CSDEF.
The SiFive SPI driver does read the num_cs defined in the register you mentioned. but it's hard defined with cs_width, eg: QSPI0 = 1, QSPI1 = 4, QSPI2 = 1. I have added the option after sifive_spi_init_hw to read from device tree as well so that if the user want's to change the chip select's (less than that defined in SIFIVE_SPI_REG_CSDEF) then it can be done in hifive-unleashed-a00.dtsi Please let me know your thoughts over here.
My understanding is that "nun-cs" represents the actual number of chip selects a controller supports. In your patch you changed all 3 controllers num-cs to 1, but they should be 1/4/1 respectively.
I see changes are made in fu540-c000.dtsi. Was such change discussed in the kernel DT community?
Is this change actually fixing any issue with what we saw on the Unleashed board?
Regards, Bin

Add missing bus claim/release method to spi driver for HiFive Unleashed, and handle num_cs generously so that it generates an error if invalid cs number is passed to sf probe.
Signed-off-by: Sagar Shrikant Kadam sagar.kadam@sifive.com --- drivers/spi/spi-sifive.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
diff --git a/drivers/spi/spi-sifive.c b/drivers/spi/spi-sifive.c index 969bd4b..f990ad6 100644 --- a/drivers/spi/spi-sifive.c +++ b/drivers/spi/spi-sifive.c @@ -186,6 +186,36 @@ static void sifive_spi_tx(struct sifive_spi *spi, const u8 *tx_ptr) writel(tx_data, spi->regs + SIFIVE_SPI_REG_TXDATA); }
+static int sifive_spi_claim_bus(struct udevice *dev) +{ + int ret; + struct udevice *bus = dev->parent; + struct sifive_spi *spi = dev_get_priv(bus); + struct dm_spi_slave_platdata *slave = dev_get_parent_platdata(dev); + + if (!(slave->cs < spi->num_cs)) { + printf("Invalid cs number = %d\n", slave->cs); + return -EINVAL; + } + + sifive_spi_prep_device(spi, slave); + + ret = sifive_spi_set_cs(spi, slave); + if (ret) + return ret; + + return 0; +} + +static int sifive_spi_release_bus(struct udevice *dev) +{ + struct sifive_spi *spi = dev_get_priv(dev->parent); + + sifive_spi_clear_cs(spi); + + return 0; +} + static int sifive_spi_xfer(struct udevice *dev, unsigned int bitlen, const void *dout, void *din, unsigned long flags) { @@ -345,6 +375,10 @@ static int sifive_spi_probe(struct udevice *bus) /* init the sifive spi hw */ sifive_spi_init_hw(spi);
+ /* Fetch number of chip selects from DT if present */ + ret = dev_read_u32_default(bus, "num-cs", spi->num_cs); + spi->num_cs = ret; + return 0; }
@@ -353,6 +387,8 @@ static const struct dm_spi_ops sifive_spi_ops = { .set_speed = sifive_spi_set_speed, .set_mode = sifive_spi_set_mode, .cs_info = sifive_spi_cs_info, + .claim_bus = sifive_spi_claim_bus, + .release_bus = sifive_spi_release_bus, };
static const struct udevice_id sifive_spi_ids[] = {

Hi Sagar,
On Wed, Jan 29, 2020 at 2:02 AM Sagar Shrikant Kadam sagar.kadam@sifive.com wrote:
Add missing bus claim/release method to spi driver for HiFive Unleashed, and handle num_cs generously so that it generates an error if invalid cs number is passed to sf probe.
Is adding the claim/release method the fix to the weird "sf probe 0:2/4/6/8" issue?
Signed-off-by: Sagar Shrikant Kadam sagar.kadam@sifive.com
drivers/spi/spi-sifive.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
diff --git a/drivers/spi/spi-sifive.c b/drivers/spi/spi-sifive.c index 969bd4b..f990ad6 100644 --- a/drivers/spi/spi-sifive.c +++ b/drivers/spi/spi-sifive.c @@ -186,6 +186,36 @@ static void sifive_spi_tx(struct sifive_spi *spi, const u8 *tx_ptr) writel(tx_data, spi->regs + SIFIVE_SPI_REG_TXDATA); }
+static int sifive_spi_claim_bus(struct udevice *dev) +{
int ret;
struct udevice *bus = dev->parent;
struct sifive_spi *spi = dev_get_priv(bus);
struct dm_spi_slave_platdata *slave = dev_get_parent_platdata(dev);
if (!(slave->cs < spi->num_cs)) {
slave->cs >= spi->num_cs
But there is already check added recently in the spi-uclass driver, see below:
commit 7bacce524d48594dae399f9ee9280ab105f6c8cf Author: Bin Meng bmeng.cn@gmail.com Date: Mon Sep 9 06:00:02 2019 -0700
dm: spi: Check cs number before accessing slaves
Add chip select number check in spi_find_chip_select().
Signed-off-by: Bin Meng bmeng.cn@gmail.com Tested-by: Jagan Teki jagan@amarulasolutions.com # SoPine
Adding check in the sifive spi driver seems to unnecessary. Could you please confirm?
printf("Invalid cs number = %d\n", slave->cs);
return -EINVAL;
}
sifive_spi_prep_device(spi, slave);
ret = sifive_spi_set_cs(spi, slave);
if (ret)
return ret;
return 0;
+}
+static int sifive_spi_release_bus(struct udevice *dev) +{
struct sifive_spi *spi = dev_get_priv(dev->parent);
sifive_spi_clear_cs(spi);
return 0;
+}
What was done in the sifive_spi_claim_bus / sifive_spi_release_bus seems to be already done in sifive_spi_xfer(). See flags testing against SPI_XFER_BEGIN and SPI_XFER_END.
Could you please explain a little bit on why adding these 2 fixes things?
static int sifive_spi_xfer(struct udevice *dev, unsigned int bitlen, const void *dout, void *din, unsigned long flags) { @@ -345,6 +375,10 @@ static int sifive_spi_probe(struct udevice *bus) /* init the sifive spi hw */ sifive_spi_init_hw(spi);
/* Fetch number of chip selects from DT if present */
ret = dev_read_u32_default(bus, "num-cs", spi->num_cs);
spi->num_cs = ret;
spi->num_cs is already assigned a value in sifive_spi_init_hw(), Why do we override the value using DT's?
return 0;
}
@@ -353,6 +387,8 @@ static const struct dm_spi_ops sifive_spi_ops = { .set_speed = sifive_spi_set_speed, .set_mode = sifive_spi_set_mode, .cs_info = sifive_spi_cs_info,
.claim_bus = sifive_spi_claim_bus,
.release_bus = sifive_spi_release_bus,
};
static const struct udevice_id sifive_spi_ids[] = {
Regards, Bin

Hello Bin,
-----Original Message----- From: Bin Meng bmeng.cn@gmail.com Sent: Tuesday, February 4, 2020 5:43 PM To: Sagar Kadam sagar.kadam@sifive.com Cc: U-Boot Mailing List u-boot@lists.denx.de; Rick Chen rick@andestech.com; Paul Walmsley ( Sifive) paul.walmsley@sifive.com; Jagan Teki jagan@amarulasolutions.com; Anup Patel anup.patel@wdc.com Subject: Re: [U-Boot Patch v2 2/4] spi: fu540: add claim and release method to spi-sifive.c
Hi Sagar,
On Wed, Jan 29, 2020 at 2:02 AM Sagar Shrikant Kadam sagar.kadam@sifive.com wrote:
Add missing bus claim/release method to spi driver for HiFive Unleashed, and handle num_cs generously so that it generates an error if invalid cs number is passed to sf probe.
Is adding the claim/release method the fix to the weird "sf probe 0:2/4/6/8" issue?
Please see below.
Signed-off-by: Sagar Shrikant Kadam sagar.kadam@sifive.com
drivers/spi/spi-sifive.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
diff --git a/drivers/spi/spi-sifive.c b/drivers/spi/spi-sifive.c index 969bd4b..f990ad6 100644 --- a/drivers/spi/spi-sifive.c +++ b/drivers/spi/spi-sifive.c @@ -186,6 +186,36 @@ static void sifive_spi_tx(struct sifive_spi *spi, const
u8 *tx_ptr)
writel(tx_data, spi->regs + SIFIVE_SPI_REG_TXDATA); }
+static int sifive_spi_claim_bus(struct udevice *dev) {
int ret;
struct udevice *bus = dev->parent;
struct sifive_spi *spi = dev_get_priv(bus);
struct dm_spi_slave_platdata *slave =
+dev_get_parent_platdata(dev);
if (!(slave->cs < spi->num_cs)) {
slave->cs >= spi->num_cs
But there is already check added recently in the spi-uclass driver, see below:
commit 7bacce524d48594dae399f9ee9280ab105f6c8cf Author: Bin Meng bmeng.cn@gmail.com Date: Mon Sep 9 06:00:02 2019 -0700
dm: spi: Check cs number before accessing slaves Add chip select number check in spi_find_chip_select(). Signed-off-by: Bin Meng <bmeng.cn@gmail.com> Tested-by: Jagan Teki <jagan@amarulasolutions.com> # SoPine
Adding check in the sifive spi driver seems to unnecessary. Could you please confirm?
Ahh!!. Thanks for the pointer Bin. This V2 patch here is based on commit c05b38df477a ("common: fix regression on block cache init"), so didn't come across the patch you pointed out. So for now we can skip the check in sifive spi driver.
printf("Invalid cs number = %d\n", slave->cs);
return -EINVAL;
}
sifive_spi_prep_device(spi, slave);
ret = sifive_spi_set_cs(spi, slave);
if (ret)
return ret;
return 0;
+}
+static int sifive_spi_release_bus(struct udevice *dev) {
struct sifive_spi *spi = dev_get_priv(dev->parent);
sifive_spi_clear_cs(spi);
return 0;
+}
What was done in the sifive_spi_claim_bus / sifive_spi_release_bus seems to be already done in sifive_spi_xfer(). See flags testing against SPI_XFER_BEGIN and SPI_XFER_END.
Could you please explain a little bit on why adding these 2 fixes things?
What I saw was that sf probe was detecting flash even at wrong chip select inputs, Like sf probe 0:2/4/6/.. this indicated that a check to validate chip selects needs to be there. The gist of adding the claim function was to introduce this chip select check. The code within SPI_XFER_BEGIN and END functions missed this check. Now that the commit your pointed 7bacce524d48 handles this, I think we can drop this check as of now.
static int sifive_spi_xfer(struct udevice *dev, unsigned int bitlen, const void *dout, void *din, unsigned long flags) { @@ -345,6 +375,10 @@ static int sifive_spi_probe(struct udevice *bus) /* init the sifive spi hw */ sifive_spi_init_hw(spi);
/* Fetch number of chip selects from DT if present */
ret = dev_read_u32_default(bus, "num-cs", spi->num_cs);
spi->num_cs = ret;
spi->num_cs is already assigned a value in sifive_spi_init_hw(), Why do we override the value using DT's?
Yes, spi_init_hw does initialise the spi->num_cs by reading the register. For QSPI0 and QSPI2 it is set to 1 (as per the cs_width). But for QSPI1 it is set to 4. Consider a case where user wishes to populate only 1 chip select for QSPI1 then it can be done in respective <board>-dts file and can be updated after sifive_spi_init_hw is done. If the board device tree doesn't contain any such entry than above API will retain the value of spi->num_cs populated in sifive_spi_init_hw(). So to summarize, we can drop claim/release addition from this patch and just keep the dev_read_u32_default function done above. Please let me know your opinion here.
Thanks & BR, Sagar
return 0;
}
@@ -353,6 +387,8 @@ static const struct dm_spi_ops sifive_spi_ops = { .set_speed = sifive_spi_set_speed, .set_mode = sifive_spi_set_mode, .cs_info = sifive_spi_cs_info,
.claim_bus = sifive_spi_claim_bus,
.release_bus = sifive_spi_release_bus,
};
static const struct udevice_id sifive_spi_ids[] = {
Regards, Bin

Hi Sagar,
On Fri, Feb 7, 2020 at 2:25 AM Sagar Kadam sagar.kadam@sifive.com wrote:
Hello Bin,
-----Original Message----- From: Bin Meng bmeng.cn@gmail.com Sent: Tuesday, February 4, 2020 5:43 PM To: Sagar Kadam sagar.kadam@sifive.com Cc: U-Boot Mailing List u-boot@lists.denx.de; Rick Chen rick@andestech.com; Paul Walmsley ( Sifive) paul.walmsley@sifive.com; Jagan Teki jagan@amarulasolutions.com; Anup Patel anup.patel@wdc.com Subject: Re: [U-Boot Patch v2 2/4] spi: fu540: add claim and release method to spi-sifive.c
Hi Sagar,
On Wed, Jan 29, 2020 at 2:02 AM Sagar Shrikant Kadam sagar.kadam@sifive.com wrote:
Add missing bus claim/release method to spi driver for HiFive Unleashed, and handle num_cs generously so that it generates an error if invalid cs number is passed to sf probe.
Is adding the claim/release method the fix to the weird "sf probe 0:2/4/6/8" issue?
Please see below.
Signed-off-by: Sagar Shrikant Kadam sagar.kadam@sifive.com
drivers/spi/spi-sifive.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
diff --git a/drivers/spi/spi-sifive.c b/drivers/spi/spi-sifive.c index 969bd4b..f990ad6 100644 --- a/drivers/spi/spi-sifive.c +++ b/drivers/spi/spi-sifive.c @@ -186,6 +186,36 @@ static void sifive_spi_tx(struct sifive_spi *spi, const
u8 *tx_ptr)
writel(tx_data, spi->regs + SIFIVE_SPI_REG_TXDATA); }
+static int sifive_spi_claim_bus(struct udevice *dev) {
int ret;
struct udevice *bus = dev->parent;
struct sifive_spi *spi = dev_get_priv(bus);
struct dm_spi_slave_platdata *slave =
+dev_get_parent_platdata(dev);
if (!(slave->cs < spi->num_cs)) {
slave->cs >= spi->num_cs
But there is already check added recently in the spi-uclass driver, see below:
commit 7bacce524d48594dae399f9ee9280ab105f6c8cf Author: Bin Meng bmeng.cn@gmail.com Date: Mon Sep 9 06:00:02 2019 -0700
dm: spi: Check cs number before accessing slaves Add chip select number check in spi_find_chip_select(). Signed-off-by: Bin Meng <bmeng.cn@gmail.com> Tested-by: Jagan Teki <jagan@amarulasolutions.com> # SoPine
Adding check in the sifive spi driver seems to unnecessary. Could you please confirm?
Ahh!!. Thanks for the pointer Bin. This V2 patch here is based on commit c05b38df477a ("common: fix regression on block cache init"), so didn't come across the patch you pointed out. So for now we can skip the check in sifive spi driver.
printf("Invalid cs number = %d\n", slave->cs);
return -EINVAL;
}
sifive_spi_prep_device(spi, slave);
ret = sifive_spi_set_cs(spi, slave);
if (ret)
return ret;
return 0;
+}
+static int sifive_spi_release_bus(struct udevice *dev) {
struct sifive_spi *spi = dev_get_priv(dev->parent);
sifive_spi_clear_cs(spi);
return 0;
+}
What was done in the sifive_spi_claim_bus / sifive_spi_release_bus seems to be already done in sifive_spi_xfer(). See flags testing against SPI_XFER_BEGIN and SPI_XFER_END.
Could you please explain a little bit on why adding these 2 fixes things?
What I saw was that sf probe was detecting flash even at wrong chip select inputs, Like sf probe 0:2/4/6/.. this indicated that a check to validate chip selects needs to be there. The gist of adding the claim function was to introduce this chip select check. The code within SPI_XFER_BEGIN and END functions missed this check. Now that the commit your pointed 7bacce524d48 handles this, I think we can drop this check as of now.
static int sifive_spi_xfer(struct udevice *dev, unsigned int bitlen, const void *dout, void *din, unsigned long flags) { @@ -345,6 +375,10 @@ static int sifive_spi_probe(struct udevice *bus) /* init the sifive spi hw */ sifive_spi_init_hw(spi);
/* Fetch number of chip selects from DT if present */
ret = dev_read_u32_default(bus, "num-cs", spi->num_cs);
spi->num_cs = ret;
spi->num_cs is already assigned a value in sifive_spi_init_hw(), Why do we override the value using DT's?
Yes, spi_init_hw does initialise the spi->num_cs by reading the register. For QSPI0 and QSPI2 it is set to 1 (as per the cs_width). But for QSPI1 it is set to 4. Consider a case where user wishes to populate only 1 chip select for QSPI1 then it can be done in respective <board>-dts file and can be updated after sifive_spi_init_hw is done. If the board device tree doesn't contain any such entry than above API will retain the value of spi->num_cs populated in sifive_spi_init_hw().
So why is such override necessary? If we don't do this, would it cause any problem?
IOW, is this patch actually fixing anything that was seen on the Unleashed board?
So to summarize, we can drop claim/release addition from this patch and just keep the dev_read_u32_default function done above. Please let me know your opinion here.
Regards, Bin

The hifive-unleashed-a00.dts has flash spi-tx/rx width set to 4-bit mode. During sf probe, spi_nor_scan fails to read the JEDEC ID with reg_proto set to SNOR_PROTO_1_1_1. Setting it to 1-bit mode as of now will help read the JEDEC-ID and perform other flash operations.
Signed-off-by: Sagar Shrikant Kadam sagar.kadam@sifive.com --- arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi | 8 ++++++++ 1 file changed, 8 insertions(+)
diff --git a/arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi b/arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi index d7a6413..dae9f87 100644 --- a/arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi +++ b/arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi @@ -9,3 +9,11 @@ spi2 = &qspi2; }; }; + +&qspi0 { + flash@0 { + spi-tx-bus-width = <1>; + spi-rx-bus-width = <1>; + }; +}; +

Hi Sagar,
On Wed, Jan 29, 2020 at 2:02 AM Sagar Shrikant Kadam sagar.kadam@sifive.com wrote:
The hifive-unleashed-a00.dts has flash spi-tx/rx width set to 4-bit mode. During sf probe, spi_nor_scan fails to read the JEDEC ID with reg_proto set to SNOR_PROTO_1_1_1. Setting it to 1-bit mode as of now will help read the JEDEC-ID and perform other flash operations.
So previously with Jagan's series that did not have these changes in this commit, the flash driver worked well. I wonder what real issue was fixed in this commit?
Signed-off-by: Sagar Shrikant Kadam sagar.kadam@sifive.com
arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi | 8 ++++++++ 1 file changed, 8 insertions(+)
diff --git a/arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi b/arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi index d7a6413..dae9f87 100644 --- a/arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi +++ b/arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi @@ -9,3 +9,11 @@ spi2 = &qspi2; }; };
+&qspi0 {
flash@0 {
spi-tx-bus-width = <1>;
spi-rx-bus-width = <1>;
};
+};
Regards, Bin

Hello Bin,
-----Original Message----- From: Bin Meng bmeng.cn@gmail.com Sent: Tuesday, February 4, 2020 5:43 PM To: Sagar Kadam sagar.kadam@sifive.com Cc: U-Boot Mailing List u-boot@lists.denx.de; Rick Chen rick@andestech.com; Paul Walmsley ( Sifive) paul.walmsley@sifive.com; Jagan Teki jagan@amarulasolutions.com; Anup Patel anup.patel@wdc.com Subject: Re: [U-Boot Patch v2 3/4] dts: u-boot.dtsi: override flash tx-rx width
Hi Sagar,
On Wed, Jan 29, 2020 at 2:02 AM Sagar Shrikant Kadam sagar.kadam@sifive.com wrote:
The hifive-unleashed-a00.dts has flash spi-tx/rx width set to 4-bit mode. During sf probe, spi_nor_scan fails to read the JEDEC ID with reg_proto set to SNOR_PROTO_1_1_1. Setting it to 1-bit mode as of now will help read the JEDEC-ID and perform other flash operations.
So previously with Jagan's series that did not have these changes in this commit, the flash driver worked well. I wonder what real issue was fixed in this commit?
Yes true, I had observed that flash device was working with Jagan's series you are indicating here. The flash device was getting probed at cs 2/4/6/8 etc.. but it couldn't get detected on CS0 which is actually connected on the board to the flash device. With the check of spi->num_cs done in patch 2 of this series or the one handled in commit 7bacce524d48 ("dm: spi: Check cs number before accessing slaves") invalid chip select is taken care of and flash is not detected on wrong chip selects, but spi transfer still fails as the sifive-spi driver set's the SIFIVE_SPI_FMT_PROTO_QUAD mode based on device tree information. While reading Device ID in spi_nor_scan the reg proto is set to 1_1_1 bit mode and this contradicts here in the driver due to which spi_nor_scan fails, due to which one cannot use the flash. So for now I have added this override here to 1 bit mode so that users can use it. Maybe I will update the commit message to indicate that this is workaround for a bug in SPI driver for FU540 which currently is not able to handle QUAD mode operation. Please let me know your views here.
Thanks & BR, Sagar
Signed-off-by: Sagar Shrikant Kadam sagar.kadam@sifive.com
arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi | 8 ++++++++ 1 file changed, 8 insertions(+)
diff --git a/arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi b/arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi index d7a6413..dae9f87 100644 --- a/arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi +++ b/arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi @@ -9,3 +9,11 @@ spi2 = &qspi2; }; };
+&qspi0 {
flash@0 {
spi-tx-bus-width = <1>;
spi-rx-bus-width = <1>;
};
+};
Regards, Bin

Hi Sagar,
On Fri, Feb 7, 2020 at 2:43 AM Sagar Kadam sagar.kadam@sifive.com wrote:
Hello Bin,
-----Original Message----- From: Bin Meng bmeng.cn@gmail.com Sent: Tuesday, February 4, 2020 5:43 PM To: Sagar Kadam sagar.kadam@sifive.com Cc: U-Boot Mailing List u-boot@lists.denx.de; Rick Chen rick@andestech.com; Paul Walmsley ( Sifive) paul.walmsley@sifive.com; Jagan Teki jagan@amarulasolutions.com; Anup Patel anup.patel@wdc.com Subject: Re: [U-Boot Patch v2 3/4] dts: u-boot.dtsi: override flash tx-rx width
Hi Sagar,
On Wed, Jan 29, 2020 at 2:02 AM Sagar Shrikant Kadam sagar.kadam@sifive.com wrote:
The hifive-unleashed-a00.dts has flash spi-tx/rx width set to 4-bit mode. During sf probe, spi_nor_scan fails to read the JEDEC ID with reg_proto set to SNOR_PROTO_1_1_1. Setting it to 1-bit mode as of now will help read the JEDEC-ID and perform other flash operations.
So previously with Jagan's series that did not have these changes in this commit, the flash driver worked well. I wonder what real issue was fixed in this commit?
Yes true, I had observed that flash device was working with Jagan's series you are indicating here. The flash device was getting probed at cs 2/4/6/8 etc.. but it couldn't get detected on CS0 which is actually connected on the board to the flash device. With the check of spi->num_cs done in patch 2 of this series or the one handled in commit 7bacce524d48 ("dm: spi: Check cs number before accessing slaves") invalid chip select is taken care of and flash is not detected on wrong chip selects, but spi transfer still fails as the sifive-spi driver set's the SIFIVE_SPI_FMT_PROTO_QUAD mode based on device tree information. While reading Device ID in spi_nor_scan the reg proto is set to 1_1_1 bit mode and this contradicts here in the driver due to which spi_nor_scan fails, due to which one cannot use the flash. So for now I have added this override here to 1 bit mode so that users can use it. Maybe I will update the commit message to indicate that this is workaround for a bug in SPI driver for FU540 which currently is not able to handle QUAD mode operation. Please let me know your views here.
Thanks for the details. Based on your description, I think there is something wrong with the SPI-NOR driver. Your change to 1 line only seems to be a workaround.
Jagan, would you please comment?
Regards, Bin

Hello Jagan,
-----Original Message----- From: Bin Meng bmeng.cn@gmail.com Sent: Wednesday, February 19, 2020 9:59 PM To: Sagar Kadam sagar.kadam@sifive.com Cc: U-Boot Mailing List u-boot@lists.denx.de; Rick Chen rick@andestech.com; Paul Walmsley ( Sifive) paul.walmsley@sifive.com; Jagan Teki jagan@amarulasolutions.com; Anup Patel anup.patel@wdc.com Subject: Re: [U-Boot Patch v2 3/4] dts: u-boot.dtsi: override flash tx-rx width
Hi Sagar,
On Fri, Feb 7, 2020 at 2:43 AM Sagar Kadam sagar.kadam@sifive.com wrote:
Hello Bin,
-----Original Message----- From: Bin Meng bmeng.cn@gmail.com Sent: Tuesday, February 4, 2020 5:43 PM To: Sagar Kadam sagar.kadam@sifive.com Cc: U-Boot Mailing List u-boot@lists.denx.de; Rick Chen rick@andestech.com; Paul Walmsley ( Sifive) paul.walmsley@sifive.com; Jagan Teki
Anup Patel anup.patel@wdc.com Subject: Re: [U-Boot Patch v2 3/4] dts: u-boot.dtsi: override flash tx-rx width
Hi Sagar,
On Wed, Jan 29, 2020 at 2:02 AM Sagar Shrikant Kadam sagar.kadam@sifive.com wrote:
The hifive-unleashed-a00.dts has flash spi-tx/rx width set to 4-bit mode. During sf probe, spi_nor_scan fails to read the JEDEC ID with reg_proto set to SNOR_PROTO_1_1_1. Setting it to 1-bit mode as of now will help read the JEDEC-ID and perform other flash
operations.
So previously with Jagan's series that did not have these changes in this commit, the flash driver worked well. I wonder what real issue was fixed in this commit?
Yes true, I had observed that flash device was working with Jagan's series you are indicating here. The flash device was getting probed at cs
2/4/6/8 etc..
but it couldn't get detected on CS0 which is actually connected on the board to the flash device. With the check of spi->num_cs done in patch 2 of this series or the one handled in commit 7bacce524d48 ("dm: spi: Check cs number before accessing slaves") invalid chip select is taken care of and flash is not detected on wrong chip selects, but spi transfer still fails as the sifive-spi driver set's the SIFIVE_SPI_FMT_PROTO_QUAD mode based on device tree information. While reading Device ID in spi_nor_scan the reg proto is set to 1_1_1 bit mode and this contradicts here in the driver
due to which spi_nor_scan fails, due to which one cannot use the flash.
So for now I have added this override here to 1 bit mode so that users can
use it.
Maybe I will update the commit message to indicate that this is workaround for a bug in SPI driver for FU540 which currently is not able to
handle QUAD mode operation.
Please let me know your views here.
Thanks for the details. Based on your description, I think there is something wrong with the SPI-NOR driver. Your change to 1 line only seems to be a workaround.
Jagan, would you please comment?
It would be good if you could also share some input's here.
Thanks & BR, Sagar Kadam
Regards, Bin

Add fdt->gd info to bdinfo so that it is useful for debugging and easily use it with fdt util.
Signed-off-by: Sagar Shrikant Kadam sagar.kadam@sifive.com --- cmd/bdinfo.c | 1 + 1 file changed, 1 insertion(+)
diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c index d6a7175..96892b3 100644 --- a/cmd/bdinfo.c +++ b/cmd/bdinfo.c @@ -433,6 +433,7 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) print_bi_dram(bd); print_num("relocaddr", gd->relocaddr); print_num("reloc off", gd->reloc_off); + print_num("fdt_blob", (ulong)gd->fdt_blob); print_eth_ip_addr(); print_baudrate();

Hi Sagar,
On Wed, Jan 29, 2020 at 2:02 AM Sagar Shrikant Kadam sagar.kadam@sifive.com wrote:
Add fdt->gd info to bdinfo so that it is useful for debugging and easily use it with fdt util.
The commit title should be tagged with "riscv: bdinfo" as it is not fu540 specific.
Signed-off-by: Sagar Shrikant Kadam sagar.kadam@sifive.com
cmd/bdinfo.c | 1 + 1 file changed, 1 insertion(+)
diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c index d6a7175..96892b3 100644 --- a/cmd/bdinfo.c +++ b/cmd/bdinfo.c @@ -433,6 +433,7 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) print_bi_dram(bd); print_num("relocaddr", gd->relocaddr); print_num("reloc off", gd->reloc_off);
print_num("fdt_blob", (ulong)gd->fdt_blob); print_eth_ip_addr(); print_baudrate();
--
Other than above, Reviewed-by: Bin Meng bmeng.cn@gmail.com
Regards, Bin

Hi Bin,
-----Original Message----- From: Bin Meng bmeng.cn@gmail.com Sent: Tuesday, February 4, 2020 5:43 PM To: Sagar Kadam sagar.kadam@sifive.com Cc: U-Boot Mailing List u-boot@lists.denx.de; Rick Chen rick@andestech.com; Paul Walmsley ( Sifive) paul.walmsley@sifive.com; Jagan Teki jagan@amarulasolutions.com; Anup Patel anup.patel@wdc.com Subject: Re: [U-Boot Patch v2 4/4] bdinfo: fu540: print fdt base address for debugging
Hi Sagar,
On Wed, Jan 29, 2020 at 2:02 AM Sagar Shrikant Kadam sagar.kadam@sifive.com wrote:
Add fdt->gd info to bdinfo so that it is useful for debugging and easily use it with fdt util.
The commit title should be tagged with "riscv: bdinfo" as it is not fu540 specific.
Thanks for the Reviewed-by tag. I will include the change in next series
BR, Sagar
Signed-off-by: Sagar Shrikant Kadam sagar.kadam@sifive.com
cmd/bdinfo.c | 1 + 1 file changed, 1 insertion(+)
diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c index d6a7175..96892b3 100644 --- a/cmd/bdinfo.c +++ b/cmd/bdinfo.c @@ -433,6 +433,7 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc,
char * const argv[])
print_bi_dram(bd); print_num("relocaddr", gd->relocaddr); print_num("reloc off", gd->reloc_off);
print_num("fdt_blob", (ulong)gd->fdt_blob); print_eth_ip_addr(); print_baudrate();
--
Other than above, Reviewed-by: Bin Meng bmeng.cn@gmail.com
Regards, Bin

Hi Sagar,
On Wed, Jan 29, 2020 at 2:02 AM Sagar Shrikant Kadam sagar.kadam@sifive.com wrote:
Currently device ID for flash mounted on HiFive Unleashed is added to U-Boot. Also there are few patches about to go in mainline (Thanks to Jagan Tekki and Bin Meng).
This series addresses few issues discussed there: Patch 1:Add num-cs to device tree which is used by spi-uclass to detect valid chip select numbers Patch 2:Handles valid chip selects only. Flash device is now detected only on chip select 0. Patch 3:Over-ride spi tx/rx width specified in hifive-unleshed-a00.dts file from 4 to 1 in corresponding -u-boot.dtsi
Thank you for fixing all these SPI flash issues!
This series is based on mainline commit c05b38df477a ("common: fix regression on block cache init") and two below mentioned patches from [1] [U-Boot,v2,4/5] riscv: dts: hifive-unleashed-a00: Add -u-boot.dtsi [U-Boot,v2,5/5] sifive: fu540: Enable spi-nor flash support
[1] https://patchwork.ozlabs.org/patch/1177979/
The above series is available for testing here[2] [2] https://github.com/sagsifive/u-boot/tree/dev/sagark/test_spi-nor_v2
Change History: V1-V2:
- Dropped 6th and 7th patch sent in V1 from this series so as to separate out spi-nor related changes in different series and avoid adding TODO fixes to spi-nor-core framework.
Did you resend the 6th and 7th patches as a separate series? I can't find them on the ML.
- Removed patch to include -uboot.dts, as it gets automatically included.
- Over-ride tx/rx width from hifive-unleashed-a00.dts using hifive -unleashed-a00-u-boot.dtsi
- Print fdt base address in board info for debugging.
V1: First version of series
================Log for reference===================== Flash detection only at valid Chip select => sf probe 0:0 SF: Detected is25wp256 with page size 256 Bytes, erase size 4 KiB, total 32 MiB => sf probe 0:1 Invalid cs number = 1 Failed to initialize SPI flash at 0:1 (error -22) => sf probe 0:2 Invalid cs number = 2 Failed to initialize SPI flash at 0:2 (error -22) => sf probe 0:0 SF: Detected is25wp256 with page size 256 Bytes, erase size 4 KiB, total 32 MiB
=>---------------------------------------------------------------- Full flash memory erase/write/read/validate => mw 0x80600000 0x12348765 0x800000 => sf erase 0x0 0x2000000 SF: 33554432 bytes @ 0x0 Erased: OK => sf write 0x80600000 0x0 0x2000000 device 0 whole chip SF: 33554432 bytes @ 0x0 Written: OK => sf read 0x82600000 0x0 0x2000000 device 0 whole chip SF: 33554432 bytes @ 0x0 Read: OK => cmp.b 0x80600000 0x82600000 0x2000000 Total of 33554432 byte(s) were the same =>----------------------------------------------------------------
Regards, Bin

Hello Bin,
-----Original Message----- From: Bin Meng bmeng.cn@gmail.com Sent: Tuesday, February 4, 2020 5:21 PM To: Sagar Kadam sagar.kadam@sifive.com Cc: U-Boot Mailing List u-boot@lists.denx.de; Rick Chen rick@andestech.com; Paul Walmsley ( Sifive) paul.walmsley@sifive.com; Jagan Teki jagan@amarulasolutions.com; Anup Patel anup.patel@wdc.com Subject: Re: [U-Boot Patch v2 0/4] Fix currently available support for flash on HiFive Unleashed
Hi Sagar,
On Wed, Jan 29, 2020 at 2:02 AM Sagar Shrikant Kadam sagar.kadam@sifive.com wrote:
Currently device ID for flash mounted on HiFive Unleashed is added to U-Boot. Also there are few patches about to go in mainline (Thanks to Jagan Tekki and Bin Meng).
This series addresses few issues discussed there: Patch 1:Add num-cs to device tree which is used by spi-uclass to detect valid chip select numbers Patch 2:Handles valid chip selects only. Flash device is now detected only on chip select 0. Patch 3:Over-ride spi tx/rx width specified in hifive-unleshed-a00.dts file from 4 to 1 in corresponding -u-boot.dtsi
Thank you for fixing all these SPI flash issues!
This series is based on mainline commit c05b38df477a ("common: fix regression on block cache init") and two below mentioned patches from [1] [U-Boot,v2,4/5] riscv: dts: hifive-unleashed-a00: Add -u-boot.dtsi [U-Boot,v2,5/5] sifive: fu540: Enable spi-nor flash support
[1] https://patchwork.ozlabs.org/patch/1177979/
The above series is available for testing here[2] [2] https://github.com/sagsifive/u-boot/tree/dev/sagark/test_spi-nor_v2
Change History: V1-V2:
- Dropped 6th and 7th patch sent in V1 from this series so as to separate out spi-nor related changes in different series and avoid adding TODO fixes to spi-nor-core framework.
Did you resend the 6th and 7th patches as a separate series? I can't find them on the ML.
Thanks for your review Bin. I have differed 6th and 7th patches as of now, as these could be handled separately as it contained a TODO part which was not suitable to be introduced into the spi-nor framework. So for now, I have added a workaround in the 3rd patch using which one can a working flash support.
BR, Sagar
- Removed patch to include -uboot.dts, as it gets automatically included.
- Over-ride tx/rx width from hifive-unleashed-a00.dts using hifive -unleashed-a00-u-boot.dtsi
- Print fdt base address in board info for debugging.
V1: First version of series
================Log for reference===================== Flash
detection
only at valid Chip select => sf probe 0:0 SF: Detected is25wp256 with page size 256 Bytes, erase size 4 KiB, total 32 MiB => sf probe 0:1 Invalid cs number = 1 Failed to initialize SPI flash at 0:1 (error -22) => sf probe 0:2 Invalid cs number = 2 Failed to initialize SPI flash at 0:2 (error -22) => sf probe 0:0 SF: Detected is25wp256 with page size 256 Bytes, erase size 4 KiB, total 32 MiB
=>---------------------------------------------------------------- Full flash memory erase/write/read/validate => mw 0x80600000 0x12348765 0x800000 => sf erase 0x0 0x2000000 SF: 33554432 bytes @ 0x0 Erased: OK => sf write 0x80600000 0x0 0x2000000 device 0 whole chip SF: 33554432 bytes @ 0x0 Written: OK => sf read 0x82600000 0x0 0x2000000 device 0 whole chip SF: 33554432 bytes @ 0x0 Read: OK => cmp.b 0x80600000 0x82600000 0x2000000 Total of 33554432 byte(s) were the same =>----------------------------------------------------------------
Regards, Bin
participants (3)
-
Bin Meng
-
Sagar Kadam
-
Sagar Shrikant Kadam