Skip to contentSkip to conversation

dm: spi: prevent setting a speed of 0 Hz

PATCH Superseded Simon Goldschmidt 5 participants 10 messages Oct 30, 2018 – Nov 23, 2018 SPI

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 dm: spi: prevent setting a speed of 0 Hz

When the device tree is missing a correct spi slave description below the bus (compatible "spi-flash" or spi-max-frequency are missing), the 'set_speed' callback can be called with 'speed' == 0 Hz. At least with cadence qspi, this leads to a division by zero.

Prevent this by initializing speed to 100 kHz in this case (same fallback value as is done in 'dm_spi_claim_bus') and issue a warning to console.

---

Changes in v2:
- create a define for the 100 kHz constant (used 3 times)
- assign default value when binding new device and speed is 0
- assign default value when spi_slave_ofdata_to_platdata finds that spi-max-frequency property is missing

 drivers/spi/spi-uclass.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/spi/spi-uclass.c +12 -3
index b84255bd27..2bc289a74c 100644
@@ -15,6 +15,8 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
+#define SPI_DEFAULT_SPEED_HZ 100000
+
 static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
 {
 	struct dm_spi_ops *ops;
@@ -58,7 +60,7 @@ int dm_spi_claim_bus(struct udevice *dev)
 			speed = spi->max_hz;
 	}
 	if (!speed)
-		speed = 100000;
+		speed = SPI_DEFAULT_SPEED_HZ;
 	if (speed != slave->speed) {
 		int ret = spi_set_speed_mode(bus, speed, slave->mode);
 
@@ -300,7 +302,13 @@ int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
 		}
 		plat = dev_get_parent_platdata(dev);
 		plat->cs = cs;
-		plat->max_hz = speed;
+		if (speed) {
+			plat->max_hz = speed;
+		} else {
+			printf("Warning: SPI speed fallback to %u kHz\n",
+			       SPI_DEFAULT_SPEED_HZ / 1000);
+			plat->max_hz = SPI_DEFAULT_SPEED_HZ;
+		}
 		plat->mode = mode;
 		created = true;
 	} else if (ret) {
@@ -374,7 +382,8 @@ int spi_slave_ofdata_to_platdata(struct udevice *dev,
 	int value;
 
 	plat->cs = dev_read_u32_default(dev, "reg", -1);
-	plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency", 0);
+	plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
+					    SPI_DEFAULT_SPEED_HZ);
 	if (dev_read_bool(dev, "spi-cpol"))
 		mode |= SPI_CPOL;
 	if (dev_read_bool(dev, "spi-cpha"))
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/1 dm: spi: prevent setting a speed of 0 Hz

Quoted 9 lines · Simon Goldschmidt

When the device tree is missing a correct spi slave description below the bus (compatible "spi-flash" or spi-max-frequency are missing), the 'set_speed' callback can be called with 'speed' == 0 Hz. At least with cadence qspi, this leads to a division by zero.

Prevent this by initializing speed to 100 kHz in this case (same fallback value as is done in 'dm_spi_claim_bus') and issue a warning to console.

Why can't driver plat->frequency in cadence driver initialize 100KH? plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", 100000)

#2 lore raw

PATCH v2 1/1 dm: spi: prevent setting a speed of 0 Hz

Quoted 9 lines · Jagan Teki
Quoted 6 lines · Simon Goldschmidt

When the device tree is missing a correct spi slave description below the bus (compatible "spi-flash" or spi-max-frequency are missing), the 'set_speed' callback can be called with 'speed' == 0 Hz. At least with cadence qspi, this leads to a division by zero.

Quoted 6 lines
Quoted 3 lines

Prevent this by initializing speed to 100 kHz in this case (same fallback value as is done in 'dm_spi_claim_bus') and issue a warning to console.

Why can't driver plat->frequency in cadence driver initialize 100KH? plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", 100000)

I'm not sure I understand.
The cadence driver initializes its 'plat->max_hz' from "spi-max-frequency" property and defaults to 500 kHz. No problem there.

However, the problem I want to solve is if someone puts a flash chip below there which has compatible != "spi-flash", they will get a hard fault which is hard to debug. This is because the node is not parsed because of the wrong compatible string (even if there is an "spi-max-frequency" property) and thus, "sf probe" tries to continue with 0 Hz.

And this can happen easily when porting device trees from Linux as there, the boards have compatible "n25q00" etc. instead of "spi-flash", which is U-Boot specific (sadly).

This patch is not required to make valid U-Boot devicetrees work, it is meant to get better error handling for devicetrees ported from Linux.

An even better fix would be for U-Boot not to require the compatible = "spi-flash" string but just work correctly with Linux device trees, but that's not within my possibilities right now :-(

Simon

#3 lore raw

PATCH v2 1/1 dm: spi: prevent setting a speed of 0 Hz

Quoted 29 lines · Simon Goldschmidt
Quoted 7 lines · Jagan Teki
Quoted 5 lines · Simon Goldschmidt

When the device tree is missing a correct spi slave description below the bus (compatible "spi-flash" or spi-max-frequency are missing), the 'set_speed' callback can be called with 'speed' == 0 Hz. At least with cadence qspi, this leads to a division by zero.

Quoted 5 lines
Quoted 3 lines

Prevent this by initializing speed to 100 kHz in this case (same fallback value as is done in 'dm_spi_claim_bus') and issue a warning to console.

Why can't driver plat->frequency in cadence driver initialize 100KH? plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", 100000)

I'm not sure I understand.
The cadence driver initializes its 'plat->max_hz' from "spi-max-frequency" property and defaults to 500 kHz. No problem there.

However, the problem I want to solve is if someone puts a flash chip below there which has compatible != "spi-flash", they will get a hard fault which is hard to debug. This is because the node is not parsed because of the wrong compatible string (even if there is an "spi-max-frequency" property) and thus, "sf probe" tries to continue with 0 Hz.

And this can happen easily when porting device trees from Linux as there, the boards have compatible "n25q00" etc. instead of "spi-flash", which is U-Boot specific (sadly).

Quoted 7 lines

This patch is not required to make valid U-Boot devicetrees work, it is meant to get better error handling for devicetrees ported from Linux.

An even better fix would be for U-Boot not to require the compatible = "spi-flash" string but just work correctly with Linux device trees, but that's not within my possibilities right now :-(

Ping? This is a bug that should hit nearly everyone porting a board dts working in Linux to U-Boot, can we please have some kind of fix for this? Currently, using a dts with an spi flash chip that works in Linux may abort in U-Boot...

Simon

#4 lore raw

PATCH v2 1/1 dm: spi: prevent setting a speed of 0 Hz

Quoted 35 lines · Simon Goldschmidt
Quoted 33 lines · Simon Goldschmidt
Quoted 7 lines · Jagan Teki
Quoted 5 lines · Simon Goldschmidt

When the device tree is missing a correct spi slave description below the bus (compatible "spi-flash" or spi-max-frequency are missing), the 'set_speed' callback can be called with 'speed' == 0 Hz. At least with cadence qspi, this leads to a division by zero.

Quoted 5 lines
Quoted 3 lines

Prevent this by initializing speed to 100 kHz in this case (same fallback value as is done in 'dm_spi_claim_bus') and issue a warning to console.

Why can't driver plat->frequency in cadence driver initialize 100KH? plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", 100000)

I'm not sure I understand.
The cadence driver initializes its 'plat->max_hz' from "spi-max-frequency" property and defaults to 500 kHz. No problem there.

However, the problem I want to solve is if someone puts a flash chip below there which has compatible != "spi-flash", they will get a hard fault which is hard to debug. This is because the node is not parsed because of the wrong compatible string (even if there is an "spi-max-frequency" property) and thus, "sf probe" tries to continue with 0 Hz.

And this can happen easily when porting device trees from Linux as there, the boards have compatible "n25q00" etc. instead of "spi-flash", which is U-Boot specific (sadly).

This patch is not required to make valid U-Boot devicetrees work, it is meant to get better error handling for devicetrees ported from Linux.

Quoted 8 lines
Quoted 3 lines

An even better fix would be for U-Boot not to require the compatible = "spi-flash" string but just work correctly with Linux device trees, but that's not within my possibilities right now :-(

Ping? This is a bug that should hit nearly everyone porting a board dts working in Linux to U-Boot, can we please have some kind of fix for this? Currently, using a dts with an spi flash chip that works in Linux may abort in U-Boot...

#5 lore raw

PATCH v2 1/1 dm: spi: prevent setting a speed of 0 Hz

Quoted 35 lines · Simon Goldschmidt
Quoted 33 lines · Simon Goldschmidt
Quoted 7 lines · Jagan Teki
Quoted 5 lines · Simon Goldschmidt

When the device tree is missing a correct spi slave description below the bus (compatible "spi-flash" or spi-max-frequency are missing), the 'set_speed' callback can be called with 'speed' == 0 Hz. At least with cadence qspi, this leads to a division by zero.

Quoted 5 lines
Quoted 3 lines

Prevent this by initializing speed to 100 kHz in this case (same fallback value as is done in 'dm_spi_claim_bus') and issue a warning to console.

Why can't driver plat->frequency in cadence driver initialize 100KH? plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", 100000)

I'm not sure I understand.
The cadence driver initializes its 'plat->max_hz' from "spi-max-frequency" property and defaults to 500 kHz. No problem there.

However, the problem I want to solve is if someone puts a flash chip below there which has compatible != "spi-flash", they will get a hard fault which is hard to debug. This is because the node is not parsed because of the wrong compatible string (even if there is an "spi-max-frequency" property) and thus, "sf probe" tries to continue with 0 Hz.

And this can happen easily when porting device trees from Linux as there, the boards have compatible "n25q00" etc. instead of "spi-flash", which is U-Boot specific (sadly).

This patch is not required to make valid U-Boot devicetrees work, it is meant to get better error handling for devicetrees ported from Linux.

Quoted 8 lines
Quoted 3 lines

An even better fix would be for U-Boot not to require the compatible = "spi-flash" string but just work correctly with Linux device trees, but that's not within my possibilities right now :-(

Ping? This is a bug that should hit nearly everyone porting a board dts working in Linux to U-Boot, can we please have some kind of fix for this? Currently, using a dts with an spi flash chip that works in Linux may abort in U-Boot...

Applied to u-boot-dm/master, thanks!

#6 lore raw

PATCH v2 1/1 dm: spi: prevent setting a speed of 0 Hz

Quoted 42 lines · On Fri, Nov 23, 2018 at 1:51 AM <[email protected]> wrote:
Quoted 39 lines · Simon Goldschmidt
Quoted 36 lines · Simon Goldschmidt
Quoted 7 lines · Jagan Teki
Quoted 5 lines · Simon Goldschmidt

When the device tree is missing a correct spi slave description below the bus (compatible "spi-flash" or spi-max-frequency are missing), the 'set_speed' callback can be called with 'speed' == 0 Hz. At least with cadence qspi, this leads to a division by zero.

Quoted 5 lines
Quoted 3 lines

Prevent this by initializing speed to 100 kHz in this case (same fallback value as is done in 'dm_spi_claim_bus') and issue a warning to console.

Why can't driver plat->frequency in cadence driver initialize 100KH? plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", 100000)

I'm not sure I understand.
The cadence driver initializes its 'plat->max_hz' from "spi-max-frequency" property and defaults to 500 kHz. No problem there.

However, the problem I want to solve is if someone puts a flash chip below there which has compatible != "spi-flash", they will get a hard fault which is hard to debug. This is because the node is not parsed because of the wrong compatible string (even if there is an "spi-max-frequency" property) and thus, "sf probe" tries to continue with 0 Hz.

And this can happen easily when porting device trees from Linux as there, the boards have compatible "n25q00" etc. instead of "spi-flash", which is U-Boot specific (sadly).

This patch is not required to make valid U-Boot devicetrees work, it is meant to get better error handling for devicetrees ported from Linux.

An even better fix would be for U-Boot not to require the compatible = "spi-flash" string but just work correctly with Linux device trees, but that's not within my possibilities right now :-(

Quoted 8 lines
Quoted 4 lines

Ping? This is a bug that should hit nearly everyone porting a board dts working in Linux to U-Boot, can we please have some kind of fix for this? Currently, using a dts with an spi flash chip that works in Linux may abort in U-Boot...

Applied to u-boot-dm/master, thanks!

I hold this because, there is a discussion going on with me and Simon.k about Linux compatible "jedec,spi-nor". adding support for this binding would eventually not reproduce this issue.

#7 lore raw

PATCH v2 1/1 dm: spi: prevent setting a speed of 0 Hz

[Responding from my work address due to problems with gmail]

Quoted 48 lines · Jagan Teki
Quoted 47 lines · On Fri, Nov 23, 2018 at 1:51 AM <[email protected]> wrote:
Quoted 43 lines · Simon Goldschmidt
Quoted 29 lines · Simon Goldschmidt
Quoted 7 lines · Jagan Teki
Quoted 5 lines · Simon Goldschmidt

When the device tree is missing a correct spi slave description below the bus (compatible "spi-flash" or spi-max-frequency are missing), the 'set_speed' callback can be called with 'speed' == 0 Hz. At least with cadence qspi, this leads to a division by zero.

Quoted 5 lines
Quoted 3 lines

Prevent this by initializing speed to 100 kHz in this case (same fallback value as is done in 'dm_spi_claim_bus') and issue a warning to console.

Why can't driver plat->frequency in cadence driver initialize 100KH? plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", 100000)

I'm not sure I understand.
The cadence driver initializes its 'plat->max_hz' from "spi-max-frequency" property and defaults to 500 kHz. No problem there.

However, the problem I want to solve is if someone puts a flash chip below there which has compatible != "spi-flash", they will get a hard fault which is hard to debug. This is because the node is not parsed because of the wrong compatible string (even if there is an "spi-max-frequency" property) and thus, "sf probe" tries to continue with 0 Hz.

And this can happen easily when porting device trees from Linux as there, the boards have compatible "n25q00" etc. instead of "spi-flash", which is U-Boot specific (sadly).

Quoted 7 lines

This patch is not required to make valid U-Boot devicetrees work, it is meant to get better error handling for devicetrees ported from Linux.

An even better fix would be for U-Boot not to require the compatible = "spi-flash" string but just work correctly with Linux device trees, but that's not within my possibilities right now :-(

Ping? This is a bug that should hit nearly everyone porting a board dts working in Linux to U-Boot, can we please have some kind of fix for this? Currently, using a dts with an spi flash chip that works in Linux may abort in U-Boot...

Quoted 7 lines
Quoted 3 lines

Applied to u-boot-dm/master, thanks!

I hold this because, there is a discussion going on with me and Simon.k about Linux compatible "jedec,spi-nor". adding support for this binding would eventually not reproduce this issue.

That's not completely true. Adding "jedec,spi-nor" to U-Boot would definitively make it less likely to run into this issue.

However, Linux works correctly without "jedec,spi-nor", so people can still run into problems when porting a dts from Linux. This is why I still would like to get this in.

Simon

#8 lore raw

PATCH v2 1/1 dm: spi: prevent setting a speed of 0 Hz

Quoted 54 lines · Simon Goldschmidt

[Responding from my work address due to problems with gmail]

Quoted 50 lines · Jagan Teki
Quoted 49 lines · On Fri, Nov 23, 2018 at 1:51 AM <[email protected]> wrote:
Quoted 35 lines · Simon Goldschmidt
Quoted 33 lines · Simon Goldschmidt
Quoted 7 lines · Jagan Teki
Quoted 5 lines · Simon Goldschmidt

When the device tree is missing a correct spi slave description below the bus (compatible "spi-flash" or spi-max-frequency are missing), the 'set_speed' callback can be called with 'speed' == 0 Hz. At least with cadence qspi, this leads to a division by zero.

Quoted 5 lines
Quoted 3 lines

Prevent this by initializing speed to 100 kHz in this case (same fallback value as is done in 'dm_spi_claim_bus') and issue a warning to console.

Why can't driver plat->frequency in cadence driver initialize 100KH? plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", 100000)

I'm not sure I understand.
The cadence driver initializes its 'plat->max_hz' from "spi-max-frequency" property and defaults to 500 kHz. No problem there.

However, the problem I want to solve is if someone puts a flash chip below there which has compatible != "spi-flash", they will get a hard fault which is hard to debug. This is because the node is not parsed because of the wrong compatible string (even if there is an "spi-max-frequency" property) and thus, "sf probe" tries to continue with 0 Hz.

And this can happen easily when porting device trees from Linux as there, the boards have compatible "n25q00" etc. instead of "spi-flash", which is U-Boot specific (sadly).

This patch is not required to make valid U-Boot devicetrees work, it is meant to get better error handling for devicetrees ported from Linux.

Quoted 8 lines
Quoted 3 lines

An even better fix would be for U-Boot not to require the compatible = "spi-flash" string but just work correctly with Linux device trees, but that's not within my possibilities right now :-(

Ping? This is a bug that should hit nearly everyone porting a board dts working in Linux to U-Boot, can we please have some kind of fix for this? Currently, using a dts with an spi flash chip that works in Linux may abort in U-Boot...

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Applied to u-boot-dm/master, thanks!

I hold this because, there is a discussion going on with me and Simon.k about Linux compatible "jedec,spi-nor". adding support for this binding would eventually not reproduce this issue.

That's not completely true. Adding "jedec,spi-nor" to U-Boot would definitively make it less likely to run into this issue.

I don't this is what we discussed above, adding the Linux compatible with relevant spi-frequnecy make the issue non-reproducible this is what exactly we ended-up.

Quoted 4 lines

However, Linux works correctly without "jedec,spi-nor", so people can still run into problems when porting a dts from Linux. This is why I still would like to get this in.

Why? do they specify property?

#9 lore raw

PATCH v2 1/1 dm: spi: prevent setting a speed of 0 Hz

Quoted 71 lines · Jagan Teki
Quoted 54 lines · Simon Goldschmidt

[Responding from my work address due to problems with gmail]

Quoted 50 lines · Jagan Teki
Quoted 49 lines · On Fri, Nov 23, 2018 at 1:51 AM <[email protected]> wrote:
Quoted 35 lines · Simon Goldschmidt
Quoted 33 lines · Simon Goldschmidt
Quoted 7 lines · Jagan Teki
Quoted 5 lines · Simon Goldschmidt

When the device tree is missing a correct spi slave description below the bus (compatible "spi-flash" or spi-max-frequency are missing), the 'set_speed' callback can be called with 'speed' == 0 Hz. At least with cadence qspi, this leads to a division by zero.

Quoted 5 lines
Quoted 3 lines

Prevent this by initializing speed to 100 kHz in this case (same fallback value as is done in 'dm_spi_claim_bus') and issue a warning to console.

Why can't driver plat->frequency in cadence driver initialize 100KH? plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", 100000)

I'm not sure I understand.
The cadence driver initializes its 'plat->max_hz' from "spi-max-frequency" property and defaults to 500 kHz. No problem there.

However, the problem I want to solve is if someone puts a flash chip below there which has compatible != "spi-flash", they will get a hard fault which is hard to debug. This is because the node is not parsed because of the wrong compatible string (even if there is an "spi-max-frequency" property) and thus, "sf probe" tries to continue with 0 Hz.

And this can happen easily when porting device trees from Linux as there, the boards have compatible "n25q00" etc. instead of "spi-flash", which is U-Boot specific (sadly).

This patch is not required to make valid U-Boot devicetrees work, it is meant to get better error handling for devicetrees ported from Linux.

Quoted 8 lines
Quoted 3 lines

An even better fix would be for U-Boot not to require the compatible = "spi-flash" string but just work correctly with Linux device trees, but that's not within my possibilities right now :-(

Ping? This is a bug that should hit nearly everyone porting a board dts working in Linux to U-Boot, can we please have some kind of fix for this? Currently, using a dts with an spi flash chip that works in Linux may abort in U-Boot...

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Applied to u-boot-dm/master, thanks!

I hold this because, there is a discussion going on with me and Simon.k about Linux compatible "jedec,spi-nor". adding support for this binding would eventually not reproduce this issue.

That's not completely true. Adding "jedec,spi-nor" to U-Boot would definitively make it less likely to run into this issue.

I don't this is what we discussed above, adding the Linux compatible with relevant spi-frequnecy make the issue non-reproducible this is what exactly we ended-up.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines

However, Linux works correctly without "jedec,spi-nor", so people can still run into problems when porting a dts from Linux. This is why I still would like to get this in.

Why? do they specify property?

There are dts files with compatible = "n25q256a" and others. I don't find the thread right now, but I said I would try to upstream changes to Linux so that the socfgpa_cyclone5 dts files would have an additional "jedec,spi-nor" compatible strings.

However, I cannot change all Linux devicetress and having "jedec-spi-nor" does *not* seem mandatory for Linux devicetrees.

This patch ensures a devicetree that works with Linux does not lead to a "divide-by-zero" abort with U-Boot. I do think that's an improvement for U-Boot!

Regards,
Simon