Skip to contentSkip to conversation

Add SG2044 eFUSE driver

PATCH In review Inochi Amaoto 5 participants 16 messages Oct 14 – Oct 15

Subsystems (4)
lore
Share & tools
lore

Inochi Amaoto

author maintainer
#0 lore raw

cover Add SG2044 eFUSE driver

Add SG2044 eFUSE driver, The eFUSE contains factory-programmed data, and it will be auto loaded into shadow registers while startup. So only MMIO read is needed.

Inochi Amaoto (3):
dt-bindings: nvmem: Add SG2044 eFuse controller
nvmem: Add Sophgo SG2044 eFuse driver
riscv: dts: sophgo: sg2044: Add eFUSE device

 .../bindings/nvmem/sophgo,efuse.yaml          |  61 ++++++
 .../boot/dts/sophgo/sg2044-sophgo-srd3-10.dts |   4 +
 arch/riscv/boot/dts/sophgo/sg2044.dtsi        |  12 ++
 drivers/nvmem/Kconfig                         |  12 ++
 drivers/nvmem/Makefile                        |   2 +
 drivers/nvmem/sophgo-efuse.c                  | 177 ++++++++++++++++++
 6 files changed, 268 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/nvmem/sophgo,efuse.yaml
 create mode 100644 drivers/nvmem/sophgo-efuse.c

--
2.56.0

author maintainer
#1 lore raw

PATCH 1/3 dt-bindings: nvmem: Add SG2044 eFuse controller

Sophgo SG2044 uses eFuses used to store factory-programmed data such as ROM patch, public keys and other factory information.

---
 .../bindings/nvmem/sophgo,efuse.yaml          | 61 +++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/nvmem/sophgo,efuse.yaml
Documentation/devicetree/bindings/nvmem/sophgo,efuse.yaml new file +61 -0
new file mode 100644
index 000000000000..d4bffe2724ac
@@ -0,0 +1,61 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/nvmem/sophgo,efuse.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Sophgo SoC eFuse-based NVMEM
+
+description:
+  Sophgo SoCs contain factory-programmed eFuses used to store ROM patch,
+  public key and other factory information.
+
+maintainers:
+  - Inochi Amaoto <[email protected]>
+
+allOf:
+  - $ref: nvmem.yaml#
+
+properties:
+  compatible:
+    enum:
+      - sophgo,sg2044-efuse
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    minItems: 1
+    items:
+      - description: Core clock
+      - description: APB clock
+
+  clock-names:
+    minItems: 1
+    items:
+      - const: core
+      - const: apb
+
+  resets:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - clocks
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    efuse@40000000 {
+        compatible = "sophgo,sg2044-efuse";
+        reg = <0x40000000 0x1000>;
+        #address-cells = <1>;
+        #size-cells = <1>;
+        clocks = <&clk 0>,
+                 <&clk 1>;
+        clock-names = "core", "apb";
+    };
+
+...
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH 1/3] dt-bindings: nvmem: Add SG2044 eFuse controller

Quoted 8 lines · Inochi Amaoto

Sophgo SG2044 uses eFuses used to store factory-programmed data such as ROM patch, public keys and other factory information.

---
 .../bindings/nvmem/sophgo,efuse.yaml          | 61 +++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/nvmem/sophgo,efuse.yaml

Filename should match compatible.

Quoted 25 lines
Documentation/devicetree/bindings/nvmem/sophgo,efuse.yaml new file +18 -0
new file mode 100644
index 000000000000..d4bffe2724ac
@@ -0,0 +1,61 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/nvmem/sophgo,efuse.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Sophgo SoC eFuse-based NVMEM
+
+description:
+  Sophgo SoCs contain factory-programmed eFuses used to store ROM patch,
+  public key and other factory information.
+
+maintainers:
+  - Inochi Amaoto <[email protected]>
+
+allOf:
+  - $ref: nvmem.yaml#
+

Documentation/devicetree/bindings/nvmem/sophgo,efuse.yaml line 28

Quoted 10 lines
Documentation/devicetree/bindings/nvmem/sophgo,efuse.yaml new file +10 -0
+properties:
+  compatible:
+    enum:
+      - sophgo,sg2044-efuse
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    minItems: 1

Why is this flexible?

Best regards,
Krzysztof

author maintainer
#3 lore raw

Re: [PATCH 1/3] dt-bindings: nvmem: Add SG2044 eFuse controller

Quoted 4 lines · Krzysztof Kozlowski
Quoted 3 lines · Inochi Amaoto

Sophgo SG2044 uses eFuses used to store factory-programmed data such as ROM patch, public keys and other factory information.

Quoted 8 lines
Quoted 5 lines
---
 .../bindings/nvmem/sophgo,efuse.yaml          | 61 +++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/nvmem/sophgo,efuse.yaml

Filename should match compatible.

Thanks, I will fix it.

Quoted 38 lines
Quoted 25 lines
Documentation/devicetree/bindings/nvmem/sophgo,efuse.yaml new file +18 -0
new file mode 100644
index 000000000000..d4bffe2724ac
@@ -0,0 +1,61 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/nvmem/sophgo,efuse.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Sophgo SoC eFuse-based NVMEM
+
+description:
+  Sophgo SoCs contain factory-programmed eFuses used to store ROM patch,
+  public key and other factory information.
+
+maintainers:
+  - Inochi Amaoto <[email protected]>
+
+allOf:
+  - $ref: nvmem.yaml#
+

Documentation/devicetree/bindings/nvmem/sophgo,efuse.yaml line 28

Quoted 10 lines
Documentation/devicetree/bindings/nvmem/sophgo,efuse.yaml new file +10 -0
+properties:
+  compatible:
+    enum:
+      - sophgo,sg2044-efuse
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    minItems: 1

Why is this flexible?

Quoted 3 lines

Best regards,
Krzysztof

This is a mistake I missed. It should be the only clock, so maxItem is needed.

Regards,
Inochi

author maintainer
#4 lore raw

PATCH 2/3 nvmem: Add Sophgo SG2044 eFuse driver

In reply to Inochi Amaoto

Sophgo SoCs such as SG2044 contain eFuses used to store factory-programmed data.

As for SG2044, HW automatically loads the eFuse content into shadow registers which are organized as 32bit values exposed as MMIO.

---
 drivers/nvmem/Kconfig        |  12 +++
 drivers/nvmem/Makefile       |   2 +
 drivers/nvmem/sophgo-efuse.c | 176 +++++++++++++++++++++++++++++++++++
 3 files changed, 190 insertions(+)
 create mode 100644 drivers/nvmem/sophgo-efuse.c
drivers/nvmem/Kconfig +12 -0
index c36c2a4c2a0b..a7936dd65bdd 100644
@@ -465,6 +465,18 @@ config NVMEM_SNVS_LPGPR
 	  This driver can also be built as a module. If so, the module
 	  will be called nvmem-snvs-lpgpr.
 
+config NVMEM_SOPHGO_EFUSE
+	tristate "Sophgo eFuse support"
+	depends on ARCH_SOPHGO || COMPILE_TEST
+	default ARCH_SOPHGO
+	help
+	  Say y here to enable support for reading eFuses on Sophgo SoCs
+	  such as the CV1800B. These are e.g. used to store factory programmed
+	  calibration data required for the builtin ethernet PHY.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called nvmem-sophgo-efuse.
+
 config NVMEM_SPMI_SDAM
 	tristate "SPMI SDAM Support"
 	depends on SPMI
drivers/nvmem/Makefile +2 -0
index 2bbfb9ff1885..430ddddf8c3c 100644
@@ -79,6 +79,8 @@ obj-$(CONFIG_NVMEM_SC27XX_EFUSE)	+= nvmem-sc27xx-efuse.o
 nvmem-sc27xx-efuse-y			:= sc27xx-efuse.o
 obj-$(CONFIG_NVMEM_SNVS_LPGPR)		+= nvmem_snvs_lpgpr.o
 nvmem_snvs_lpgpr-y			:= snvs_lpgpr.o
+obj-$(CONFIG_NVMEM_SOPHGO_EFUSE)	+= nvmem-sophgo-efuse.o
+nvmem-sophgo-efuse-y			:= sophgo-efuse.o
 obj-$(CONFIG_NVMEM_SPMI_SDAM)		+= nvmem_qcom-spmi-sdam.o
 nvmem_qcom-spmi-sdam-y			+= qcom-spmi-sdam.o
 obj-$(CONFIG_NVMEM_SPRD_EFUSE)		+= nvmem_sprd_efuse.o
drivers/nvmem/sophgo-efuse.c new file +176 -0

176 lines not on this page · raw message

Signature · 1 line
2.56.0
#5 lore raw

Re: [PATCH 2/3] nvmem: Add Sophgo SG2044 eFuse driver

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [Critical] mm: heap buffer overflow in sg2044_efuses_read allocation - [High] nvmem: sophgo-efuse: heap…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [Critical] mm: heap buffer overflow in sg2044_efuses_read allocation
- [High] nvmem: sophgo-efuse: heap buffer overflow due to word-to-byte calculation error
- [High] nvmem: sophgo-efuse: uninitialized mutex triggers kernel panic or undefined behavior
- [High] locking: uninitialized mutex efuse->mutex causes crash
- [Low] nvmem: sophgo-efuse: sg2044_efuses_read_strip() returns negative error code as u32
- [Low] endianness: byte order reversal on big-endian via memcpy
--

commit ff98cc30d6e5cb08398ecc6957a9d51875c1b34b
Author: Inochi Amaoto <[email protected]>

nvmem: Add Sophgo SG2044 eFuse driver

This patch adds a new nvmem driver for Sophgo eFuses used on system-on-chips such as the SG2044. The driver exposes the shadow registers, which are organized as 32-bit values, as memory-mapped io.

Quoted 5 lines
drivers/nvmem/sophgo-efuse.c new file +0 -0
new file mode 100644
index 0000000000000..5f90adaf8e4fc

[ ... ]

drivers/nvmem/sophgo-efuse.c line 74

Quoted 13 lines
drivers/nvmem/sophgo-efuse.c new file +13 -0
+static u32 sg2044_efuses_read_strip(struct sophgo_efuses *efuse,
+				    unsigned int offset, u32 *strip)
+{
+	u32 val = FIELD_PREP(SG2044_EFUSE_ADR_ADDR, offset);
+	int ret;
+
+	guard(mutex)(&efuse->mutex);
+
+	writel(val, efuse->base + SG2044_EFUSE_ADR);
+
+	ret = sg2044_efuse_set_mode(efuse, SG2044_EFUSE_MODE_READ);
+	if (ret < 0)
+		return ret;

[Severity: Low]
Does this code return a negative error code as a u32?

sg2044_efuse_set_mode() returns an int (such as -ETIMEDOUT), which gets cast to a large unsigned integer when returned here. Even though the caller assigns it back to an int ret, returning negative values via an unsigned type is poor practice and obscures the logic.

Should the return type of sg2044_efuses_read_strip() be int instead of u32?

[ ... ]

Quoted 13 lines
drivers/nvmem/sophgo-efuse.c new file +13 -0
+static int sg2044_efuses_read(void *context, unsigned int offset, void *val,
+			      size_t bytes)
+{
+	struct sophgo_efuses *efuse = context;
+	unsigned int start, start_offset, end, i;
+	u32 value;
+	u8 *buf;
+	int ret;
+
+	start = rounddown(offset, SG2044_EFUSE_ALIGN);
+	end = roundup(offset + bytes, SG2044_EFUSE_ALIGN);
+	start_offset = offset - start;
+

drivers/nvmem/sophgo-efuse.c line 103

Quoted 10 lines
drivers/nvmem/sophgo-efuse.c new file +10 -0
+	start /= SG2044_EFUSE_ALIGN;
+	end /= SG2044_EFUSE_ALIGN;
+
+	ret = readl_poll_timeout(efuse->base + SG2044_EFUSE_MD, value,
+				 (value & SG2044_EFUSE_BOOT_DONE),
+				 1, SG2044_BOOT_TIMEOUT);
+	if (ret < 0)
+		return ret;
+
+	buf = kzalloc(end - start, GFP_KERNEL);

[Severity: Critical]
Does this allocate an undersized buffer?

The variables start and end were divided by SG2044_EFUSE_ALIGN (4), changing them from byte offsets to 32-bit word indices. The allocation size is exactly end - start bytes, effectively allocating only 1 byte per 4-byte word instead of (end - start) * SG2044_EFUSE_ALIGN.

drivers/nvmem/sophgo-efuse.c line 112

Quoted 9 lines
drivers/nvmem/sophgo-efuse.c new file +9 -0
+	if (!buf)
+		return -ENOMEM;
+
+	for (i = start; i < end; i++) {
+		ret = sg2044_efuses_read_strip(efuse, i, &value);
+		if (ret)
+			goto failed;
+
+		memcpy(&buf[(i - start) * 4], &value, SG2044_EFUSE_ALIGN);

[Severity: Critical]
Because the buffer allocation size is implicitly treated as words but allocated as bytes, this loop writes 4 bytes per word iteration into the undersized buffer, leading to an out-of-bounds heap write.

[Severity: Low]
Additionally, can this cause a byte order reversal on big-endian architectures?

readl() returns a 32-bit integer in native endianness. The loop then uses memcpy() to write this native integer's bytes into a byte buffer.

On big-endian systems, this places the most-significant byte at the lowest address, reversing the intended nvmem byte-stream order.

[ ... ]

Quoted 7 lines
drivers/nvmem/sophgo-efuse.c new file +7 -0
+static int sophgo_efuses_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct sophgo_efuses *efuse;
+	struct nvmem_config config = {
+		.dev = &pdev->dev,
+		.add_legacy_fixed_of_cells = true,

drivers/nvmem/sophgo-efuse.c line 139

Quoted 10 lines
drivers/nvmem/sophgo-efuse.c new file +10 -0
+		.read_only = true,
+		.reg_read = sg2044_efuses_read,
+		.stride = 1,
+		.word_size = 1,
+		.name = "sophgo-efuse",
+		.id = NVMEM_DEVID_AUTO,
+		.root_only = true,
+	};
+
+	efuse = devm_kzalloc(dev, sizeof(*efuse), GFP_KERNEL);

[Severity: High]
Is efuse->mutex left uninitialized?

This allocates the efuse structure (zero-initialized), but mutex_init() is never called on efuse->mutex anywhere in the probe function.

Later, reading from the eFuse calls sg2044_efuses_read_strip() which attempts to acquire the lock:

    guard(mutex)(&efuse->mutex);

Locking a zero-initialized mutex will cause a null-pointer dereference crash on lock contention because the internal wait lists are not initialized.

Should mutex_init(&efuse->mutex) be added after allocation?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

Re: [PATCH 2/3] nvmem: Add Sophgo SG2044 eFuse driver

In reply to Inochi Amaoto

hello,

drivers/nvmem/sophgo-efuse.c line 16

Quoted 3 lines · Inochi Amaoto
drivers/nvmem/sophgo-efuse.c new file +2 -0
+#include <linux/mod_devicetable.h>
[...]
+#include <linux/platform_device.h>

<linux/platform_device.h> already provides of_device_id and <linux/mod_devicetable.h> pulls in a looot of dependencies this driver doesn't need. So please drop the latter.

drivers/nvmem/sophgo-efuse.c line 158

Quoted 2 lines
drivers/nvmem/sophgo-efuse.c new file +2 -0
+static const struct of_device_id sophgo_efuses_of_match[] = {
+	{ .compatible = "sophgo,sg2044-efuse", },

s/,//

drivers/nvmem/sophgo-efuse.c line 159

Quoted 1 line
drivers/nvmem/sophgo-efuse.c new file +1 -0
+	{}

s/{}/{ }/

Review

Quoted 2 lines
(quoted diff) +2 -0
+};
+

Drop this empty line

drivers/nvmem/sophgo-efuse.c line 171

Quoted 10 lines
drivers/nvmem/sophgo-efuse.c new file +10 -0
+MODULE_DEVICE_TABLE(of, sophgo_efuses_of_match);
+
+static struct platform_driver sophgo_efuses_driver = {
+	.driver = {
+		.name = "sophgo_efuse",
+		.of_match_table = sophgo_efuses_of_match,
+	},
+	.probe = sophgo_efuses_probe,
+};
+

and this one, too.

drivers/nvmem/sophgo-efuse.c line 173

Quoted 2 lines
drivers/nvmem/sophgo-efuse.c new file +2 -0
+module_platform_driver(sophgo_efuses_driver);
+

Best regards
Uwe

author maintainer
#7 lore raw

Re: [PATCH 2/3] nvmem: Add Sophgo SG2044 eFuse driver

Quoted 38 lines · Uwe Kleine-König

hello,

drivers/nvmem/sophgo-efuse.c line 16

Quoted 3 lines · Inochi Amaoto
drivers/nvmem/sophgo-efuse.c new file +2 -0
+#include <linux/mod_devicetable.h>
[...]
+#include <linux/platform_device.h>

<linux/platform_device.h> already provides of_device_id and <linux/mod_devicetable.h> pulls in a looot of dependencies this driver doesn't need. So please drop the latter.

drivers/nvmem/sophgo-efuse.c line 158

Quoted 2 lines
drivers/nvmem/sophgo-efuse.c new file +2 -0
+static const struct of_device_id sophgo_efuses_of_match[] = {
+	{ .compatible = "sophgo,sg2044-efuse", },

s/,//

drivers/nvmem/sophgo-efuse.c line 159

Quoted 1 line
drivers/nvmem/sophgo-efuse.c new file +1 -0
+	{}

s/{}/{ }/

Review

Quoted 2 lines
(quoted diff) +2 -0
+};
+

Drop this empty line

drivers/nvmem/sophgo-efuse.c line 171

Quoted 10 lines
drivers/nvmem/sophgo-efuse.c new file +10 -0
+MODULE_DEVICE_TABLE(of, sophgo_efuses_of_match);
+
+static struct platform_driver sophgo_efuses_driver = {
+	.driver = {
+		.name = "sophgo_efuse",
+		.of_match_table = sophgo_efuses_of_match,
+	},
+	.probe = sophgo_efuses_probe,
+};
+

and this one, too.

Quoted 5 lines

drivers/nvmem/sophgo-efuse.c line 173

Quoted 2 lines
drivers/nvmem/sophgo-efuse.c new file +2 -0
+module_platform_driver(sophgo_efuses_driver);
+

Best regards
Uwe

Thanks, I will strip them.

Regards,
Inochi

maintainer
#8 lore raw

Re: [PATCH 2/3] nvmem: Add Sophgo SG2044 eFuse driver

In reply to Inochi Amaoto

Hello,

Quoted 20 lines · Inochi Amaoto

Sophgo SoCs such as SG2044 contain eFuses used to store factory-programmed data.

As for SG2044, HW automatically loads the eFuse content into shadow registers which are organized as 32bit values exposed as MMIO.

---
  drivers/nvmem/Kconfig        |  12 +++
  drivers/nvmem/Makefile       |   2 +
  drivers/nvmem/sophgo-efuse.c | 176 +++++++++++++++++++++++++++++++++++
  3 files changed, 190 insertions(+)

create mode 100644 drivers/nvmem/sophgo-efuse.c

drivers/nvmem/Kconfig +0 -0
index c36c2a4c2a0b..a7936dd65bdd 100644

drivers/nvmem/Kconfig config NVMEM_SNVS_LPGPR · line 474

Quoted 10 lines
drivers/nvmem/Kconfig +7 -0
@@ -465,6 +465,18 @@ config NVMEM_SNVS_LPGPR
  	  This driver can also be built as a module. If so, the module
  	  will be called nvmem-snvs-lpgpr.
 
+config NVMEM_SOPHGO_EFUSE
+	tristate "Sophgo eFuse support"
+	depends on ARCH_SOPHGO || COMPILE_TEST
+	default ARCH_SOPHGO
+	help
+	  Say y here to enable support for reading eFuses on Sophgo SoCs
+	  such as the CV1800B. These are e.g. used to store factory programmed

Why is "CV1800B"?

[...]

author maintainer
#9 lore raw

Re: [PATCH 2/3] nvmem: Add Sophgo SG2044 eFuse driver

Quoted 18 lines · Chen Wang

Hello,

Quoted 15 lines · Inochi Amaoto

Sophgo SoCs such as SG2044 contain eFuses used to store factory-programmed data.

As for SG2044, HW automatically loads the eFuse content into shadow registers which are organized as 32bit values exposed as MMIO.

---
  drivers/nvmem/Kconfig        |  12 +++
  drivers/nvmem/Makefile       |   2 +
  drivers/nvmem/sophgo-efuse.c | 176 +++++++++++++++++++++++++++++++++++
  3 files changed, 190 insertions(+)

create mode 100644 drivers/nvmem/sophgo-efuse.c

Quoted 17 lines
Quoted 5 lines
drivers/nvmem/Kconfig +0 -0
index c36c2a4c2a0b..a7936dd65bdd 100644

drivers/nvmem/Kconfig config NVMEM_SNVS_LPGPR · line 473

Quoted 9 lines
drivers/nvmem/Kconfig +7 -0
@@ -465,6 +465,18 @@ config NVMEM_SNVS_LPGPR
  	  This driver can also be built as a module. If so, the module
  	  will be called nvmem-snvs-lpgpr.
+config NVMEM_SOPHGO_EFUSE
+	tristate "Sophgo eFuse support"
+	depends on ARCH_SOPHGO || COMPILE_TEST
+	default ARCH_SOPHGO
+	help
+	  Say y here to enable support for reading eFuses on Sophgo SoCs
+	  such as the CV1800B. These are e.g. used to store factory programmed

Why is "CV1800B"?

This is an old mistake, this patch follows Jishengs's patch in the first design. But then almost everything changed and only thing for SG2044 left. This is the thing that is left to change.

I am not sure whether it should keep this generic name as I see it should be compatible for most sophgo SoCs (at least it should be for CV1800X, by just change offset). Feel free to give if you have some suggestion for a better name

Regards,
Inochi

maintainer
#10 lore raw

Re: [PATCH 2/3] nvmem: Add Sophgo SG2044 eFuse driver

Quoted 35 lines · Inochi Amaoto
Quoted 18 lines · Chen Wang

Hello,

Quoted 15 lines · Inochi Amaoto

Sophgo SoCs such as SG2044 contain eFuses used to store factory-programmed data.

As for SG2044, HW automatically loads the eFuse content into shadow registers which are organized as 32bit values exposed as MMIO.

---
   drivers/nvmem/Kconfig        |  12 +++
   drivers/nvmem/Makefile       |   2 +
   drivers/nvmem/sophgo-efuse.c | 176 +++++++++++++++++++++++++++++++++++
   3 files changed, 190 insertions(+)

create mode 100644 drivers/nvmem/sophgo-efuse.c

Quoted 16 lines
Quoted 5 lines
drivers/nvmem/Kconfig +0 -0
index c36c2a4c2a0b..a7936dd65bdd 100644

drivers/nvmem/Kconfig config NVMEM_SNVS_LPGPR · line 473

Quoted 9 lines
drivers/nvmem/Kconfig +7 -0
@@ -465,6 +465,18 @@ config NVMEM_SNVS_LPGPR
   	  This driver can also be built as a module. If so, the module
   	  will be called nvmem-snvs-lpgpr.
+config NVMEM_SOPHGO_EFUSE
+	tristate "Sophgo eFuse support"
+	depends on ARCH_SOPHGO || COMPILE_TEST
+	default ARCH_SOPHGO
+	help
+	  Say y here to enable support for reading eFuses on Sophgo SoCs
+	  such as the CV1800B. These are e.g. used to store factory programmed

Why is "CV1800B"?

Quoted 8 lines

This is an old mistake, this patch follows Jishengs's patch in the first design. But then almost everything changed and only thing for SG2044 left. This is the thing that is left to change.

I am not sure whether it should keep this generic name as I see it should be compatible for most sophgo SoCs (at least it should be for CV1800X, by just change offset). Feel free to give if you have some suggestion for a better name

I raise this point because the subject of your patch indicates it is intended for the SG2044.

If you are simply using it as an example ("such as ..."), then specifying "SG2044" is sufficient, even though, based on your current explanation, it would also apply to other Sophgo chips, such as the CV1800X.

Quoted 3 lines

Regards,
Inochi

author maintainer
#11 lore raw

PATCH 3/3 riscv: dts: sophgo: sg2044: Add eFUSE device

In reply to Inochi Amaoto

Add eFUSE controller node for SG2044.

---
 arch/riscv/boot/dts/sophgo/sg2044-sophgo-srd3-10.dts |  4 ++++
 arch/riscv/boot/dts/sophgo/sg2044.dtsi               | 12 ++++++++++++
 drivers/nvmem/sophgo-efuse.c                         |  1 +
 3 files changed, 17 insertions(+)
arch/riscv/boot/dts/sophgo/sg2044-sophgo-srd3-10.dts +4 -0
index fed3d9a384a0..1b506972d465 100644
@@ -36,6 +36,10 @@ &emmc {
 	status = "okay";
 };
 
+&efuse0 {
+	status = "okay";
+};
+
 &gmac0 {
 	phy-handle = <&phy0>;
 	phy-mode = "rgmii-id";
arch/riscv/boot/dts/sophgo/sg2044.dtsi +12 -0
index 320c4d1d08e6..9577aae08f7f 100644
@@ -408,6 +408,18 @@ sd: mmc@703000b000 {
 			status = "disabled";
 		};
 
+		efuse0: efuse@7040000000 {
+			compatible = "sophgo,sg2044-efuse";
+			reg = <0x70 0x40000000 0x0 0x1000>;
+			#address-cells = <1>;
+			#size-cells = <1>;
+			clocks = <&clk CLK_GATE_EFUSE>,
+				 <&clk CLK_GATE_APB_EFUSE>;
+			clock-names = "core", "apb";
+			resets = <&rst RST_EFUSE0>;
+			status = "disabled";
+		};
+
 		i2c0: i2c@7040005000 {
 			compatible = "sophgo,sg2044-i2c", "snps,designware-i2c";
 			reg = <0x70 0x40005000 0x0 0x1000>;
drivers/nvmem/sophgo-efuse.c +1 -0
index 5f90adaf8e4f..568d17b88ff6 100644
@@ -14,6 +14,7 @@
 #include <linux/mutex.h>
 #include <linux/nvmem-provider.h>
 #include <linux/platform_device.h>
+#include <linux/slab.h>
 
 #define SG2044_EFUSE_CONTENT_SIZE		0x400
 
Signature · 1 line
2.56.0
#12 lore raw

Re: [PATCH 3/3] riscv: dts: sophgo: sg2044: Add eFUSE device

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] nvmem: sophgo: heap buffer overflow in sg2044_efuses_read() - [High] nvmem: sophgo-efuse…

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] nvmem: sophgo: heap buffer overflow in sg2044_efuses_read()
- [High] nvmem: sophgo-efuse: uninitialized mutex usage
- [High] nvmem: sophgo-efuse: missing reset line deassertion
- [Low] patch: undocumented driver code modification in a Device Tree patch
- [Low] nvmem: sophgo-efuse: sg2044_efuses_read_strip() return type mismatch --

commit 9dc44262e764574361fe929e8bb24ed2e5ee7f12
Author: Inochi Amaoto <[email protected]>

riscv: dts: sophgo: sg2044: Add eFUSE device

Add eFUSE controller node for SG2044.

Quoted 3 lines
arch/riscv/boot/dts/sophgo/sg2044.dtsi +0 -0

[ ... ]

arch/riscv/boot/dts/sophgo/sg2044.dtsi sd: mmc@703000b000 { · line 419

Quoted 13 lines
arch/riscv/boot/dts/sophgo/sg2044.dtsi +9 -0
@@ -408,6 +408,18 @@ sd: mmc@703000b000 {
 			status = "disabled";
 		};
 
+		efuse0: efuse@7040000000 {
+			compatible = "sophgo,sg2044-efuse";
+			reg = <0x70 0x40000000 0x0 0x1000>;
+			#address-cells = <1>;
+			#size-cells = <1>;
+			clocks = <&clk CLK_GATE_EFUSE>,
+				 <&clk CLK_GATE_APB_EFUSE>;
+			clock-names = "core", "apb";
+			resets = <&rst RST_EFUSE0>;

[Severity: High]
Does the driver need to deassert this reset line during probe?

The device tree node specifies a reset line, indicating the hardware requires it.

However, looking at the probe function:

drivers/nvmem/sophgo-efuse.c:sophgo_efuses_probe() {
	...
	efuse->num_clks = devm_clk_bulk_get_all_enabled(&pdev->dev, &efuse->clks);
	if (efuse->num_clks < 0)
		return dev_err_probe(dev, efuse->num_clks, "failed to get clocks\n");

	config.priv = efuse;
	...
}

It seems the reset control is omitted entirely.

If the bootloader leaves the device in a reset state, subsequent MMIO accesses in sg2044_efuse_wait_mode() could cause a bus hang or timeout.

Quoted 4 lines
arch/riscv/boot/dts/sophgo/sg2044.dtsi +3 -0
+			status = "disabled";
+		};
+
 		i2c0: i2c@7040005000 {

[ ... ]

drivers/nvmem/sophgo-efuse.c line 17

Quoted 8 lines
drivers/nvmem/sophgo-efuse.c +1 -0
@@ -14,6 +14,7 @@
 #include <linux/mutex.h>
 #include <linux/nvmem-provider.h>
 #include <linux/platform_device.h>
+#include <linux/slab.h>

[Severity: Low]
This isn't a bug, but is it intended to include an undocumented driver modification in a Device Tree patch?

The patch is scoped as a Device Tree update for the eFUSE DT node, but silently modifies drivers/nvmem/sophgo-efuse.c to add this include.

Separating driver modifications from DTS patches helps maintain clean subsystem boundaries and facilitates bisection.

[Severity: High]
Does this code overflow the allocation for the read buffer?

When sg2044_efuses_read() calculates start and end indices:

drivers/nvmem/sophgo-efuse.c:sg2044_efuses_read() {
	...
	start /= SG2044_EFUSE_ALIGN;
	end /= SG2044_EFUSE_ALIGN;
	...
	buf = kzalloc(end - start, GFP_KERNEL);
	...
}

It converts start and end from byte offsets to word counts, but then allocates the buffer using the word count rather than multiplying by 4.

Later in the loop:

drivers/nvmem/sophgo-efuse.c:sg2044_efuses_read() {
	...
	for (i = start; i < end; i++) {
		ret = sg2044_efuses_read_strip(efuse, i, &value);
		if (ret)
			goto failed;

		memcpy(&buf[(i - start) * 4], &value, SG2044_EFUSE_ALIGN);
	}
	...
}

The memcpy writes 4 bytes per word into the under-allocated buffer.

Could this write past the end of the allocation and cause memory corruption?

[Severity: High]
Is the mutex inside the efuse structure properly initialized?

In the probe function, the structure is allocated and zero-initialized:

drivers/nvmem/sophgo-efuse.c:sophgo_efuses_probe() {
	...
	efuse = devm_kzalloc(dev, sizeof(*efuse), GFP_KERNEL);
	if (!efuse)
		return -ENOMEM;
	...
}

However, it appears devm_mutex_init() or mutex_init() is never called for efuse->mutex.

When the sysfs read callback executes:

drivers/nvmem/sophgo-efuse.c:sg2044_efuses_read_strip() {
	...
	guard(mutex)(&efuse->mutex);
	...
}

Could locking this uninitialized mutex trigger lockdep warnings or system crashes due to the uninitialized internal wait lock and wait list?

[Severity: Low]
This isn't a bug, but could the return type of sg2044_efuses_read_strip() cause issues if negative error codes are implicitly cast?

The function is declared to return a u32:

drivers/nvmem/sophgo-efuse.c:
static u32 sg2044_efuses_read_strip(struct sophgo_efuses *efuse,
unsigned int offset, u32 *strip)

But it returns negative error codes directly:

	ret = sg2044_efuse_set_mode(efuse, SG2044_EFUSE_MODE_READ);
	if (ret < 0)
		return ret;

While the single caller sg2044_efuses_read() assigns this back to an int and checks it correctly, returning negative errors as unsigned integers might lead to unexpected behavior if future callers check for less than zero.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#13 lore raw

Re: [PATCH 3/3] riscv: dts: sophgo: sg2044: Add eFUSE device

In reply to Inochi Amaoto
Quoted 7 lines · Inochi Amaoto

Add eFUSE controller node for SG2044.

---
 arch/riscv/boot/dts/sophgo/sg2044-sophgo-srd3-10.dts |  4 ++++
 arch/riscv/boot/dts/sophgo/sg2044.dtsi               | 12 ++++++++++++
 drivers/nvmem/sophgo-efuse.c                         |  1 +

Review code before sending.

Best regards,
Krzysztof

author maintainer
#14 lore raw

Re: [PATCH 3/3] riscv: dts: sophgo: sg2044: Add eFUSE device

Quoted 9 lines · Krzysztof Kozlowski
Quoted 7 lines · Inochi Amaoto

Add eFUSE controller node for SG2044.

---
 arch/riscv/boot/dts/sophgo/sg2044-sophgo-srd3-10.dts |  4 ++++
 arch/riscv/boot/dts/sophgo/sg2044.dtsi               | 12 ++++++++++++
 drivers/nvmem/sophgo-efuse.c                         |  1 +
Quoted 5 lines

Review code before sending.

Best regards,
Krzysztof

Yeah, I forgot this, I merge it in the wrong patch. Thanks.

Regards,
Inochi

maintainer
#15 lore raw

Re: [PATCH 3/3] riscv: dts: sophgo: sg2044: Add eFUSE device

In reply to Inochi Amaoto
Quoted 52 lines · Inochi Amaoto

Add eFUSE controller node for SG2044.

---
  arch/riscv/boot/dts/sophgo/sg2044-sophgo-srd3-10.dts |  4 ++++
  arch/riscv/boot/dts/sophgo/sg2044.dtsi               | 12 ++++++++++++
  drivers/nvmem/sophgo-efuse.c                         |  1 +
  3 files changed, 17 insertions(+)
arch/riscv/boot/dts/sophgo/sg2044-sophgo-srd3-10.dts +4 -0
index fed3d9a384a0..1b506972d465 100644
@@ -36,6 +36,10 @@ &emmc {
  	status = "okay";
  };
 
+&efuse0 {
+	status = "okay";
+};
+
  &gmac0 {
  	phy-handle = <&phy0>;
  	phy-mode = "rgmii-id";
arch/riscv/boot/dts/sophgo/sg2044.dtsi +12 -0
index 320c4d1d08e6..9577aae08f7f 100644
@@ -408,6 +408,18 @@ sd: mmc@703000b000 {
  			status = "disabled";
  		};
 
+		efuse0: efuse@7040000000 {
+			compatible = "sophgo,sg2044-efuse";
+			reg = <0x70 0x40000000 0x0 0x1000>;
+			#address-cells = <1>;
+			#size-cells = <1>;
+			clocks = <&clk CLK_GATE_EFUSE>,
+				 <&clk CLK_GATE_APB_EFUSE>;
+			clock-names = "core", "apb";
+			resets = <&rst RST_EFUSE0>;
+			status = "disabled";
+		};
+
  		i2c0: i2c@7040005000 {
  			compatible = "sophgo,sg2044-i2c", "snps,designware-i2c";
  			reg = <0x70 0x40005000 0x0 0x1000>;
drivers/nvmem/sophgo-efuse.c +0 -0
index 5f90adaf8e4f..568d17b88ff6 100644

drivers/nvmem/sophgo-efuse.c line 19

Quoted 6 lines
drivers/nvmem/sophgo-efuse.c +1 -0
@@ -14,6 +14,7 @@
  #include <linux/mutex.h>
  #include <linux/nvmem-provider.h>
  #include <linux/platform_device.h>
+#include <linux/slab.h>
 
  #define SG2044_EFUSE_CONTENT_SIZE		0x400

This change is not related to DTS, should it be in patch [2/3]?