mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2 0/8] RK3568 updates
@ 2021-10-12  7:33 Sascha Hauer
  2021-10-12  7:33 ` [PATCH 1/8] phy: rockchip: Add dummy driver for child node Sascha Hauer
                   ` (7 more replies)
  0 siblings, 8 replies; 11+ messages in thread
From: Sascha Hauer @ 2021-10-12  7:33 UTC (permalink / raw)
  To: Barebox List

This series has some updates for the Rockchip RK3568 support

Changes since v1:
- correctly count number of boot slots
- Drop wrong patch
- Use designated array initialization for boot sources
- implement determing active boot slot for failsafe update

Sascha Hauer (8):
  phy: rockchip: Add dummy driver for child node
  ARM: Rockchip rk3568 EVB: Enable deep probe
  ARM: Rockchip: rk3568 EVB: use 64bit partition sizes
  phy: rockchip-inno-usb2: handle disabled child nodes gracefully
  usb: dwc3: reset controller before using it
  ARM: rk3568: Detect USB boot
  cdev: Add function to get unallocated start of device
  ARM: Rockchip: RK3568: implement failsafe barebox update

 arch/arm/boards/rockchip-rk3568-evb/board.c   |   7 +-
 arch/arm/dts/rk3568-evb1-v10.dts              |  22 +--
 arch/arm/mach-rockchip/Makefile               |   1 +
 arch/arm/mach-rockchip/bbu.c                  | 137 ++++++++++++++++++
 arch/arm/mach-rockchip/include/mach/bbu.h     |   9 +-
 arch/arm/mach-rockchip/rk3568.c               |  12 +-
 common/partitions.c                           |  28 ++++
 drivers/phy/rockchip/phy-rockchip-inno-usb2.c |  12 +-
 drivers/usb/dwc3/core.c                       |  11 ++
 drivers/usb/dwc3/core.h                       |   2 +
 include/driver.h                              |   1 +
 11 files changed, 214 insertions(+), 28 deletions(-)
 create mode 100644 arch/arm/mach-rockchip/bbu.c

-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 1/8] phy: rockchip: Add dummy driver for child node
  2021-10-12  7:33 [PATCH v2 0/8] RK3568 updates Sascha Hauer
@ 2021-10-12  7:33 ` Sascha Hauer
  2021-10-12  7:33 ` [PATCH 2/8] ARM: Rockchip rk3568 EVB: Enable deep probe Sascha Hauer
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Sascha Hauer @ 2021-10-12  7:33 UTC (permalink / raw)
  To: Barebox List

To satisfy deep probe mechanism.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/phy/rockchip/phy-rockchip-inno-usb2.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/phy/rockchip/phy-rockchip-inno-usb2.c b/drivers/phy/rockchip/phy-rockchip-inno-usb2.c
index bb1a5c747e..37c2ae1d16 100644
--- a/drivers/phy/rockchip/phy-rockchip-inno-usb2.c
+++ b/drivers/phy/rockchip/phy-rockchip-inno-usb2.c
@@ -423,6 +423,7 @@ static int rockchip_usb2phy_probe(struct device_d *dev)
 	for_each_child_of_node(np, child) {
 		struct rockchip_usb2phy_phy *p;
 		struct phy *phy;
+		struct device_d *phydev;
 
 		if (!strcmp(child->name, "host-port")) {
 			port = USB2PHY_PORT_OTG;
@@ -436,7 +437,10 @@ static int rockchip_usb2phy_probe(struct device_d *dev)
 		if (rphy->phys[port].phy)
 			return -EINVAL;
 
-		phy = phy_create(dev, child, &rockchip_usb2phy_ops);
+		phydev = of_platform_device_create(child, dev);
+		of_platform_device_dummy_drv(phydev);
+
+		phy = phy_create(phydev, child, &rockchip_usb2phy_ops);
 		if (IS_ERR(phy)) {
 			ret = PTR_ERR(phy);
 			if (ret != -EPROBE_DEFER)
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 2/8] ARM: Rockchip rk3568 EVB: Enable deep probe
  2021-10-12  7:33 [PATCH v2 0/8] RK3568 updates Sascha Hauer
  2021-10-12  7:33 ` [PATCH 1/8] phy: rockchip: Add dummy driver for child node Sascha Hauer
@ 2021-10-12  7:33 ` Sascha Hauer
  2021-10-12  7:33 ` [PATCH 3/8] ARM: Rockchip: rk3568 EVB: use 64bit partition sizes Sascha Hauer
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Sascha Hauer @ 2021-10-12  7:33 UTC (permalink / raw)
  To: Barebox List

The rk3568 EVB board is successfully tested with deep probe support,
enable it.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 arch/arm/boards/rockchip-rk3568-evb/board.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/arm/boards/rockchip-rk3568-evb/board.c b/arch/arm/boards/rockchip-rk3568-evb/board.c
index 9cd84d7fb9..2d472d1331 100644
--- a/arch/arm/boards/rockchip-rk3568-evb/board.c
+++ b/arch/arm/boards/rockchip-rk3568-evb/board.c
@@ -10,6 +10,7 @@
 #include <environment.h>
 #include <globalvar.h>
 #include <magicvar.h>
+#include <deep-probe.h>
 
 static bool machine_is_rk3568_evb = false;
 
@@ -45,6 +46,8 @@ static struct driver_d rk3568_evb_board_driver = {
 };
 coredevice_platform_driver(rk3568_evb_board_driver);
 
+BAREBOX_DEEP_PROBE_ENABLE(rk3568_evb_of_match);
+
 static int rk3568_evb_detect_hwid(void)
 {
 	int ret;
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 3/8] ARM: Rockchip: rk3568 EVB: use 64bit partition sizes
  2021-10-12  7:33 [PATCH v2 0/8] RK3568 updates Sascha Hauer
  2021-10-12  7:33 ` [PATCH 1/8] phy: rockchip: Add dummy driver for child node Sascha Hauer
  2021-10-12  7:33 ` [PATCH 2/8] ARM: Rockchip rk3568 EVB: Enable deep probe Sascha Hauer
@ 2021-10-12  7:33 ` Sascha Hauer
  2021-10-12  7:33 ` [PATCH 4/8] phy: rockchip-inno-usb2: handle disabled child nodes gracefully Sascha Hauer
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Sascha Hauer @ 2021-10-12  7:33 UTC (permalink / raw)
  To: Barebox List

The ofpart partition fixup will use #address-cells = <2> and #size-cells
= <2>. Use the same in the dts file to make the diff between the live
tree and its fixed version smaller.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 arch/arm/dts/rk3568-evb1-v10.dts | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/arch/arm/dts/rk3568-evb1-v10.dts b/arch/arm/dts/rk3568-evb1-v10.dts
index bd583015e8..ebfd45ada8 100644
--- a/arch/arm/dts/rk3568-evb1-v10.dts
+++ b/arch/arm/dts/rk3568-evb1-v10.dts
@@ -472,17 +472,17 @@
 
 	partitions {
 		compatible = "fixed-partitions";
-		#address-cells = <1>;
-		#size-cells = <1>;
+		#address-cells = <2>;
+		#size-cells = <2>;
 
 		partition@8000 {
 			label = "barebox";
-			reg = <0x8000 0x400000>;
+			reg = <0x0 0x8000 0x0 0x400000>;
 		};
 
 		environment_emmc: partition@408000 {
 			label = "barebox-environment";
-			reg = <0x408000 0x8000>;
+			reg = <0x0 0x408000 0x0 0x8000>;
 		};
 	};
 };
@@ -503,17 +503,17 @@
 
 	partitions {
 		compatible = "fixed-partitions";
-		#address-cells = <1>;
-		#size-cells = <1>;
+		#address-cells = <2>;
+		#size-cells = <2>;
 
 		partition@8000 {
 			label = "barebox";
-			reg = <0x8000 0x400000>;
+			reg = <0x0 0x8000 0x0 0x400000>;
 		};
 
 		environment_sd: partition@408000 {
 			label = "barebox-environment";
-			reg = <0x408000 0x8000>;
+			reg = <0x0 0x408000 0x0 0x8000>;
 		};
 	};
 };
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 4/8] phy: rockchip-inno-usb2: handle disabled child nodes gracefully
  2021-10-12  7:33 [PATCH v2 0/8] RK3568 updates Sascha Hauer
                   ` (2 preceding siblings ...)
  2021-10-12  7:33 ` [PATCH 3/8] ARM: Rockchip: rk3568 EVB: use 64bit partition sizes Sascha Hauer
@ 2021-10-12  7:33 ` Sascha Hauer
  2021-10-12  7:33 ` [PATCH 5/8] usb: dwc3: reset controller before using it Sascha Hauer
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Sascha Hauer @ 2021-10-12  7:33 UTC (permalink / raw)
  To: Barebox List

When child nodes of the phy node are disabled then
of_platform_device_create() on them returns a NULL pointer. Make sure we
skip initialization for these disabled nodes and also skip them when
iterating over them in the xlate function.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/phy/rockchip/phy-rockchip-inno-usb2.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/phy/rockchip/phy-rockchip-inno-usb2.c b/drivers/phy/rockchip/phy-rockchip-inno-usb2.c
index 37c2ae1d16..6b9d54f344 100644
--- a/drivers/phy/rockchip/phy-rockchip-inno-usb2.c
+++ b/drivers/phy/rockchip/phy-rockchip-inno-usb2.c
@@ -264,6 +264,9 @@ static struct phy *rockchip_usb2phy_of_xlate(struct device_d *dev,
 	int port;
 
 	for (port = 0; port < 2; port++) {
+		if (!rphy->phys[port].phy)
+			continue;
+
 		if (phynode == rphy->phys[port].phy->dev.device_node) {
 			p = &rphy->phys[port];
 			return p->phy;
@@ -438,6 +441,9 @@ static int rockchip_usb2phy_probe(struct device_d *dev)
 			return -EINVAL;
 
 		phydev = of_platform_device_create(child, dev);
+		if (!phydev)
+			continue;
+
 		of_platform_device_dummy_drv(phydev);
 
 		phy = phy_create(phydev, child, &rockchip_usb2phy_ops);
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 5/8] usb: dwc3: reset controller before using it
  2021-10-12  7:33 [PATCH v2 0/8] RK3568 updates Sascha Hauer
                   ` (3 preceding siblings ...)
  2021-10-12  7:33 ` [PATCH 4/8] phy: rockchip-inno-usb2: handle disabled child nodes gracefully Sascha Hauer
@ 2021-10-12  7:33 ` Sascha Hauer
  2021-10-12  7:33 ` [PATCH 6/8] ARM: rk3568: Detect USB boot Sascha Hauer
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Sascha Hauer @ 2021-10-12  7:33 UTC (permalink / raw)
  To: Barebox List

Some dwc3 controllers have a reset assigned to them. Request it and
reset the device before using it.
Linux upstream driver uses an array of resets here, but in barebox we
currently do have this functionality, so for now use the first reset
only.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/usb/dwc3/core.c | 11 +++++++++++
 drivers/usb/dwc3/core.h |  2 ++
 2 files changed, 13 insertions(+)

diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index 04b700d12d..f618435cb6 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -14,6 +14,7 @@
 #include <dma.h>
 #include <driver.h>
 #include <init.h>
+#include <linux/reset.h>
 
 #include "gadget.h"
 #include "core.h"
@@ -1125,6 +1126,16 @@ static int dwc3_probe(struct device_d *dev)
 	if (ret)
 		return ret;
 
+	dwc->reset = reset_control_get(dev, NULL);
+	if (IS_ERR(dwc->reset)) {
+		dev_err(dev, "Failed to get reset control: %pe\n", dwc->reset);
+		return PTR_ERR(dwc->reset);
+	}
+
+	reset_control_assert(dwc->reset);
+	mdelay(1);
+	reset_control_deassert(dwc->reset);
+
 	dwc3_coresoft_reset(dwc);
 
 	dwc3_cache_hwparams(dwc);
diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
index f2f7a311d1..94cc594920 100644
--- a/drivers/usb/dwc3/core.h
+++ b/drivers/usb/dwc3/core.h
@@ -1003,6 +1003,8 @@ struct dwc3 {
 	struct clk_bulk_data	*clks;
 	int			num_clks;
 
+	struct reset_control	*reset;
+
 	struct phy		*usb2_generic_phy;
 	struct phy		*usb3_generic_phy;
 
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 6/8] ARM: rk3568: Detect USB boot
  2021-10-12  7:33 [PATCH v2 0/8] RK3568 updates Sascha Hauer
                   ` (4 preceding siblings ...)
  2021-10-12  7:33 ` [PATCH 5/8] usb: dwc3: reset controller before using it Sascha Hauer
@ 2021-10-12  7:33 ` Sascha Hauer
  2021-10-12  7:33 ` [PATCH 7/8] cdev: Add function to get unallocated start of device Sascha Hauer
  2021-10-12  7:33 ` [PATCH 8/8] ARM: Rockchip: RK3568: implement failsafe barebox update Sascha Hauer
  7 siblings, 0 replies; 11+ messages in thread
From: Sascha Hauer @ 2021-10-12  7:33 UTC (permalink / raw)
  To: Barebox List

The table of register content to bootsource mapping has been taken from
the vendor U-Boot. This table lacks an entry for USB boot. Add this
entry. It's unknown if this entry is entirely correct, it reflects the
value read from the register when doing USB boot.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 arch/arm/mach-rockchip/rk3568.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/arch/arm/mach-rockchip/rk3568.c b/arch/arm/mach-rockchip/rk3568.c
index fcf3cb7053..4e6d3eef88 100644
--- a/arch/arm/mach-rockchip/rk3568.c
+++ b/arch/arm/mach-rockchip/rk3568.c
@@ -145,12 +145,12 @@ struct rk_bootsource {
 };
 
 static struct rk_bootsource bootdev_map[] = {
-	{ .src = BOOTSOURCE_UNKNOWN, .instance = 0 },
-	{ .src = BOOTSOURCE_NAND, .instance = 0 },
-	{ .src = BOOTSOURCE_MMC, .instance = 0 },
-	{ .src = BOOTSOURCE_SPI_NOR, .instance = 0 },
-	{ .src = BOOTSOURCE_SPI_NAND, .instance = 0 },
-	{ .src = BOOTSOURCE_MMC, .instance = 1 },
+	[0x1] = { .src = BOOTSOURCE_NAND, .instance = 0 },
+	[0x2] = { .src = BOOTSOURCE_MMC, .instance = 0 },
+	[0x3] = { .src = BOOTSOURCE_SPI_NOR, .instance = 0 },
+	[0x4] = { .src = BOOTSOURCE_SPI_NAND, .instance = 0 },
+	[0x5] = { .src = BOOTSOURCE_MMC, .instance = 1 },
+	[0xa] = { .src = BOOTSOURCE_USB, .instance = 0 },
 };
 
 static enum bootsource rk3568_bootsource(void)
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 7/8] cdev: Add function to get unallocated start of device
  2021-10-12  7:33 [PATCH v2 0/8] RK3568 updates Sascha Hauer
                   ` (5 preceding siblings ...)
  2021-10-12  7:33 ` [PATCH 6/8] ARM: rk3568: Detect USB boot Sascha Hauer
@ 2021-10-12  7:33 ` Sascha Hauer
  2021-10-12  7:52   ` Ahmad Fatoum
  2021-10-12  7:33 ` [PATCH 8/8] ARM: Rockchip: RK3568: implement failsafe barebox update Sascha Hauer
  7 siblings, 1 reply; 11+ messages in thread
From: Sascha Hauer @ 2021-10-12  7:33 UTC (permalink / raw)
  To: Barebox List

On several SoCs barebox is written to the raw device in front of the
first partition. So far we blindly trust that there is enough space
available for the barebox image. Start changing this by adding a
function that retrieves the available space.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/partitions.c | 28 ++++++++++++++++++++++++++++
 include/driver.h    |  1 +
 2 files changed, 29 insertions(+)

diff --git a/common/partitions.c b/common/partitions.c
index d80878e065..e36341fc1e 100644
--- a/common/partitions.c
+++ b/common/partitions.c
@@ -156,3 +156,31 @@ int partition_parser_register(struct partition_parser *p)
 
 	return 0;
 }
+
+/**
+ * cdev_unallocated_start - return unallocated space
+ * @name: The cdev name
+ *
+ * This function returns the space that is not allocated by any partition
+ * at the start of a device.
+ *
+ * Return: The unallocated space at the start of the device in bytes
+ */
+loff_t cdev_unallocated_start(const char *name)
+{
+	struct cdev *cdev, *partcdev;
+	loff_t start;
+
+	cdev = cdev_by_name(name);
+	if (!cdev)
+		return 0;
+
+	start = cdev->size;
+
+	list_for_each_entry(partcdev, &cdev->partitions, partition_entry) {
+		if (partcdev->offset < start)
+			start = partcdev->offset;
+	}
+
+	return start;
+}
diff --git a/include/driver.h b/include/driver.h
index c7f5903fce..38aa3cf8f5 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -494,6 +494,7 @@ ssize_t cdev_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, ulo
 ssize_t cdev_write(struct cdev *cdev, const void *buf, size_t count, loff_t offset, ulong flags);
 int cdev_ioctl(struct cdev *cdev, int cmd, void *buf);
 int cdev_erase(struct cdev *cdev, loff_t count, loff_t offset);
+loff_t cdev_unallocated_start(const char *name);
 
 #define DEVFS_PARTITION_FIXED		(1U << 0)
 #define DEVFS_PARTITION_READONLY	(1U << 1)
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 8/8] ARM: Rockchip: RK3568: implement failsafe barebox update
  2021-10-12  7:33 [PATCH v2 0/8] RK3568 updates Sascha Hauer
                   ` (6 preceding siblings ...)
  2021-10-12  7:33 ` [PATCH 7/8] cdev: Add function to get unallocated start of device Sascha Hauer
@ 2021-10-12  7:33 ` Sascha Hauer
  7 siblings, 0 replies; 11+ messages in thread
From: Sascha Hauer @ 2021-10-12  7:33 UTC (permalink / raw)
  To: Barebox List

The RK3568 ROM searches for valid boot images at different positions
on SD/eMMC cards. This can be used to implement a failsafe barebox
update which is immune against power failures.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 arch/arm/boards/rockchip-rk3568-evb/board.c |   4 +-
 arch/arm/dts/rk3568-evb1-v10.dts            |  10 --
 arch/arm/mach-rockchip/Makefile             |   1 +
 arch/arm/mach-rockchip/bbu.c                | 137 ++++++++++++++++++++
 arch/arm/mach-rockchip/include/mach/bbu.h   |   9 +-
 5 files changed, 146 insertions(+), 15 deletions(-)
 create mode 100644 arch/arm/mach-rockchip/bbu.c

diff --git a/arch/arm/boards/rockchip-rk3568-evb/board.c b/arch/arm/boards/rockchip-rk3568-evb/board.c
index 2d472d1331..09385bea29 100644
--- a/arch/arm/boards/rockchip-rk3568-evb/board.c
+++ b/arch/arm/boards/rockchip-rk3568-evb/board.c
@@ -28,8 +28,8 @@ static int rk3568_evb_probe(struct device_d *dev)
 	else
 		of_device_enable_path("/chosen/environment-emmc");
 
-	rk3568_bbu_mmc_register("emmc", BBU_HANDLER_FLAG_DEFAULT, "/dev/emmc.barebox");
-	rk3568_bbu_mmc_register("sd", 0, "/dev/sd.barebox");
+	rk3568_bbu_mmc_register("emmc", BBU_HANDLER_FLAG_DEFAULT, "/dev/emmc");
+	rk3568_bbu_mmc_register("sd", 0, "/dev/sd");
 
 	return 0;
 }
diff --git a/arch/arm/dts/rk3568-evb1-v10.dts b/arch/arm/dts/rk3568-evb1-v10.dts
index ebfd45ada8..6f1eebc619 100644
--- a/arch/arm/dts/rk3568-evb1-v10.dts
+++ b/arch/arm/dts/rk3568-evb1-v10.dts
@@ -475,11 +475,6 @@
 		#address-cells = <2>;
 		#size-cells = <2>;
 
-		partition@8000 {
-			label = "barebox";
-			reg = <0x0 0x8000 0x0 0x400000>;
-		};
-
 		environment_emmc: partition@408000 {
 			label = "barebox-environment";
 			reg = <0x0 0x408000 0x0 0x8000>;
@@ -506,11 +501,6 @@
 		#address-cells = <2>;
 		#size-cells = <2>;
 
-		partition@8000 {
-			label = "barebox";
-			reg = <0x0 0x8000 0x0 0x400000>;
-		};
-
 		environment_sd: partition@408000 {
 			label = "barebox-environment";
 			reg = <0x0 0x408000 0x0 0x8000>;
diff --git a/arch/arm/mach-rockchip/Makefile b/arch/arm/mach-rockchip/Makefile
index ebaa3a5450..66bcdba2eb 100644
--- a/arch/arm/mach-rockchip/Makefile
+++ b/arch/arm/mach-rockchip/Makefile
@@ -4,3 +4,4 @@ obj-$(CONFIG_ARCH_RK3188) += rk3188.o
 obj-$(CONFIG_ARCH_RK3288) += rk3288.o
 obj-pbl-$(CONFIG_ARCH_RK3568) += rk3568.o
 obj-$(CONFIG_ARCH_RK3568) += bootm.o
+obj-$(CONFIG_BAREBOX_UPDATE) += bbu.o
diff --git a/arch/arm/mach-rockchip/bbu.c b/arch/arm/mach-rockchip/bbu.c
new file mode 100644
index 0000000000..0a580c908d
--- /dev/null
+++ b/arch/arm/mach-rockchip/bbu.c
@@ -0,0 +1,137 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+#include <common.h>
+#include <malloc.h>
+#include <bbu.h>
+#include <filetype.h>
+#include <errno.h>
+#include <fs.h>
+#include <fcntl.h>
+#include <linux/sizes.h>
+#include <linux/stat.h>
+#include <ioctl.h>
+#include <environment.h>
+#include <mach/bbu.h>
+#include <libfile.h>
+#include <linux/bitfield.h>
+#include <mach/rk3568-regs.h>
+
+/* The MaskROM looks for images on these locations: */
+#define IMG_OFFSET_0	(0 * SZ_1K + SZ_32K)
+#define IMG_OFFSET_1	(512 * SZ_1K + SZ_32K)
+#define IMG_OFFSET_2	(1024 * SZ_1K + SZ_32K)
+#define IMG_OFFSET_3	(1536 * SZ_1K + SZ_32K)
+#define IMG_OFFSET_4	(2048 * SZ_1K + SZ_32K)
+
+#define RK3568_IRAM_ACTIVE_BOOT_SLOT	GENMASK(12, 10)
+
+static int rk3568_get_active_slot(void)
+{
+	return FIELD_GET(RK3568_IRAM_ACTIVE_BOOT_SLOT,
+			 readl(RK3568_IRAM_BASE + 0x14));
+}
+
+/*
+ * The strategy here is:
+ * The MaskROM iterates over the above five locations until it finds a valid
+ * boot image. The images are protected with sha sums, so any change to an
+ * image on disk is invalidating it. We first check if we have enough space to
+ * write two copies of barebox. To make it simple we only use IMG_OFFSET_0 and
+ * IMG_OFFSET_4 which leaves the maximum size for a single image. When there's
+ * not enough free space on the beginning of the disk we only write a single
+ * image. When we have enough space for two images we first write the inactive one
+ * (leaving the active one intact). Afterwards we write the active one which
+ * leaves the previously written inactive image as a fallback in case writing the
+ * first one gets interrupted.
+ */
+static int rk3568_bbu_mmc_handler(struct bbu_handler *handler,
+				  struct bbu_data *data)
+{
+	enum filetype filetype;
+	int ret, fd, wr0, wr1;
+	loff_t space;
+
+	filetype = file_detect_type(data->image, data->len);
+	if (filetype != filetype_rockchip_rkns_image) {
+		if (!bbu_force(data, "incorrect image type. Expected: %s, got %s",
+				file_type_to_string(filetype_rockchip_rkns_image),
+				file_type_to_string(filetype)))
+			return -EINVAL;
+	}
+
+	device_detect_by_name(devpath_to_name(data->devicefile));
+
+	ret = bbu_confirm(data);
+	if (ret)
+		return ret;
+
+	space = cdev_unallocated_start(devpath_to_name(data->devicefile));
+
+	if (space < IMG_OFFSET_0 + data->len) {
+		pr_err("Unallocated space on %s is too small for one image\n", data->devicefile);
+		return -ENOSPC;
+	}
+
+	fd = open(data->devicefile, O_WRONLY);
+	if (fd < 0)
+		return fd;
+
+	if (space >= IMG_OFFSET_4 + data->len) {
+		int slot = rk3568_get_active_slot();
+
+		pr_info("Unallocated space is enough for two copies, doing failsafe update\n");
+
+		if (slot == 0) {
+			wr0 = IMG_OFFSET_4;
+			wr1 = IMG_OFFSET_0;
+		} else {
+			wr0 = IMG_OFFSET_0;
+			wr1 = IMG_OFFSET_4;
+		}
+	} else {
+		wr0 = IMG_OFFSET_0;
+		wr1 = 0;
+	}
+
+	ret = pwrite_full(fd, data->image, data->len, wr0);
+	if (ret < 0) {
+		pr_err("writing to %s failed with %s\n", data->devicefile,
+			strerror(-ret));
+		goto err_close;
+	}
+
+	if (wr1) {
+		ret = pwrite_full(fd, data->image, data->len, wr1);
+		if (ret < 0) {
+			pr_err("writing to %s failed with %s\n", data->devicefile,
+				strerror(-ret));
+			goto err_close;
+		}
+	}
+
+	ret = 0;
+
+err_close:
+	close(fd);
+
+	return ret;
+}
+
+int rk3568_bbu_mmc_register(const char *name, unsigned long flags,
+                const char *devicefile)
+{
+	struct bbu_handler *handler;
+	int ret;
+
+	handler = xzalloc(sizeof(*handler));
+
+	handler->flags = flags;
+	handler->devicefile = devicefile;
+	handler->name = name;
+	handler->handler = rk3568_bbu_mmc_handler;
+
+	ret = bbu_register_handler(handler);
+	if (ret)
+		free(handler);
+
+	return ret;
+}
diff --git a/arch/arm/mach-rockchip/include/mach/bbu.h b/arch/arm/mach-rockchip/include/mach/bbu.h
index e61e0615e2..7fb08a0a9e 100644
--- a/arch/arm/mach-rockchip/include/mach/bbu.h
+++ b/arch/arm/mach-rockchip/include/mach/bbu.h
@@ -3,12 +3,15 @@
 
 #include <bbu.h>
 
+#ifdef CONFIG_BAREBOX_UPDATE
+int rk3568_bbu_mmc_register(const char *name, unsigned long flags,
+                const char *devicefile);
+#else
 static inline int rk3568_bbu_mmc_register(const char *name, unsigned long flags,
                 const char *devicefile)
 {
-	return bbu_register_std_file_update(name, flags,
-                devicefile, filetype_rockchip_rkns_image);
-
+	return -ENOSYS;
 }
+#endif
 
 # endif /* __MACH_ROCKCHIP_BBU_H */
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 7/8] cdev: Add function to get unallocated start of device
  2021-10-12  7:33 ` [PATCH 7/8] cdev: Add function to get unallocated start of device Sascha Hauer
@ 2021-10-12  7:52   ` Ahmad Fatoum
  2021-10-12  8:31     ` Sascha Hauer
  0 siblings, 1 reply; 11+ messages in thread
From: Ahmad Fatoum @ 2021-10-12  7:52 UTC (permalink / raw)
  To: Sascha Hauer, Barebox List

On 12.10.21 09:33, Sascha Hauer wrote:
> On several SoCs barebox is written to the raw device in front of the
> first partition. So far we blindly trust that there is enough space
> available for the barebox image. Start changing this by adding a
> function that retrieves the available space.
> 
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> ---
>  common/partitions.c | 28 ++++++++++++++++++++++++++++
>  include/driver.h    |  1 +
>  2 files changed, 29 insertions(+)
> 
> diff --git a/common/partitions.c b/common/partitions.c
> index d80878e065..e36341fc1e 100644
> --- a/common/partitions.c
> +++ b/common/partitions.c
> @@ -156,3 +156,31 @@ int partition_parser_register(struct partition_parser *p)
>  
>  	return 0;
>  }
> +
> +/**
> + * cdev_unallocated_start - return unallocated space
> + * @name: The cdev name
> + *
> + * This function returns the space that is not allocated by any partition
> + * at the start of a device.
> + *
> + * Return: The unallocated space at the start of the device in bytes
> + */
> +loff_t cdev_unallocated_start(const char *name)

That name is a bit misleading. It's either cdev_allocated_start
or cdev_unallocated_space. Also cdev_ for a function not taking a cdev
is unexpected. Perhaps define devpath_to_cdev(s) as
cdev_by_name(devpath_to_name(s)) and have the caller pass in the cdev?

> +{
> +	struct cdev *cdev, *partcdev;
> +	loff_t start;
> +
> +	cdev = cdev_by_name(name);
> +	if (!cdev)
> +		return 0;
> +
> +	start = cdev->size;
> +
> +	list_for_each_entry(partcdev, &cdev->partitions, partition_entry) {
> +		if (partcdev->offset < start)
> +			start = partcdev->offset;
> +	}
> +
> +	return start;
> +}
> diff --git a/include/driver.h b/include/driver.h
> index c7f5903fce..38aa3cf8f5 100644
> --- a/include/driver.h
> +++ b/include/driver.h
> @@ -494,6 +494,7 @@ ssize_t cdev_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, ulo
>  ssize_t cdev_write(struct cdev *cdev, const void *buf, size_t count, loff_t offset, ulong flags);
>  int cdev_ioctl(struct cdev *cdev, int cmd, void *buf);
>  int cdev_erase(struct cdev *cdev, loff_t count, loff_t offset);
> +loff_t cdev_unallocated_start(const char *name);
>  
>  #define DEVFS_PARTITION_FIXED		(1U << 0)
>  #define DEVFS_PARTITION_READONLY	(1U << 1)
> 


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 7/8] cdev: Add function to get unallocated start of device
  2021-10-12  7:52   ` Ahmad Fatoum
@ 2021-10-12  8:31     ` Sascha Hauer
  0 siblings, 0 replies; 11+ messages in thread
From: Sascha Hauer @ 2021-10-12  8:31 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: Barebox List

On Tue, Oct 12, 2021 at 09:52:51AM +0200, Ahmad Fatoum wrote:
> On 12.10.21 09:33, Sascha Hauer wrote:
> > On several SoCs barebox is written to the raw device in front of the
> > first partition. So far we blindly trust that there is enough space
> > available for the barebox image. Start changing this by adding a
> > function that retrieves the available space.
> > 
> > Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> > ---
> >  common/partitions.c | 28 ++++++++++++++++++++++++++++
> >  include/driver.h    |  1 +
> >  2 files changed, 29 insertions(+)
> > 
> > diff --git a/common/partitions.c b/common/partitions.c
> > index d80878e065..e36341fc1e 100644
> > --- a/common/partitions.c
> > +++ b/common/partitions.c
> > @@ -156,3 +156,31 @@ int partition_parser_register(struct partition_parser *p)
> >  
> >  	return 0;
> >  }
> > +
> > +/**
> > + * cdev_unallocated_start - return unallocated space
> > + * @name: The cdev name
> > + *
> > + * This function returns the space that is not allocated by any partition
> > + * at the start of a device.
> > + *
> > + * Return: The unallocated space at the start of the device in bytes
> > + */
> > +loff_t cdev_unallocated_start(const char *name)
> 
> That name is a bit misleading. It's either cdev_allocated_start
> or cdev_unallocated_space. Also cdev_ for a function not taking a cdev
> is unexpected. Perhaps define devpath_to_cdev(s) as
> cdev_by_name(devpath_to_name(s)) and have the caller pass in the cdev?

That's a case of "wow, what a horrible name, let's write the function
first and rethink the name later". That rethinking didn't happen on my
side, thanks for doing this ;)

Sascha

-----------------------------8<-------------------------------

>From 839d934c79aee95a7d67d2f8dceacbfd0e5ea848 Mon Sep 17 00:00:00 2001
From: Sascha Hauer <s.hauer@pengutronix.de>
Date: Tue, 12 Oct 2021 09:33:51 +0200
Subject: [PATCH] cdev: Add function to get unallocated space at start of
 device

On several SoCs barebox is written to the raw device in front of the
first partition. So far we blindly trust that there is enough space
available for the barebox image. Start changing this by adding a
function that retrieves the available space.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Link: https://lore.barebox.org/20211012073352.4071559-8-s.hauer@pengutronix.de
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/partitions.c | 27 +++++++++++++++++++++++++++
 include/driver.h    |  1 +
 2 files changed, 28 insertions(+)

diff --git a/common/partitions.c b/common/partitions.c
index d80878e065..b579559672 100644
--- a/common/partitions.c
+++ b/common/partitions.c
@@ -156,3 +156,30 @@ int partition_parser_register(struct partition_parser *p)
 
 	return 0;
 }
+
+/**
+ * cdev_unallocated_space - return unallocated space
+ * cdev: The cdev
+ *
+ * This function returns the space that is not allocated by any partition
+ * at the start of a device.
+ *
+ * Return: The unallocated space at the start of the device in bytes
+ */
+loff_t cdev_unallocated_space(struct cdev *cdev)
+{
+	struct cdev *partcdev;
+	loff_t start;
+
+	if (!cdev)
+		return 0;
+
+	start = cdev->size;
+
+	list_for_each_entry(partcdev, &cdev->partitions, partition_entry) {
+		if (partcdev->offset < start)
+			start = partcdev->offset;
+	}
+
+	return start;
+}
diff --git a/include/driver.h b/include/driver.h
index c7f5903fce..4f6d40e17c 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -494,6 +494,7 @@ ssize_t cdev_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, ulo
 ssize_t cdev_write(struct cdev *cdev, const void *buf, size_t count, loff_t offset, ulong flags);
 int cdev_ioctl(struct cdev *cdev, int cmd, void *buf);
 int cdev_erase(struct cdev *cdev, loff_t count, loff_t offset);
+loff_t cdev_unallocated_space(struct cdev *cdev);
 
 #define DEVFS_PARTITION_FIXED		(1U << 0)
 #define DEVFS_PARTITION_READONLY	(1U << 1)
-- 
2.30.2

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2021-10-12  8:32 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-12  7:33 [PATCH v2 0/8] RK3568 updates Sascha Hauer
2021-10-12  7:33 ` [PATCH 1/8] phy: rockchip: Add dummy driver for child node Sascha Hauer
2021-10-12  7:33 ` [PATCH 2/8] ARM: Rockchip rk3568 EVB: Enable deep probe Sascha Hauer
2021-10-12  7:33 ` [PATCH 3/8] ARM: Rockchip: rk3568 EVB: use 64bit partition sizes Sascha Hauer
2021-10-12  7:33 ` [PATCH 4/8] phy: rockchip-inno-usb2: handle disabled child nodes gracefully Sascha Hauer
2021-10-12  7:33 ` [PATCH 5/8] usb: dwc3: reset controller before using it Sascha Hauer
2021-10-12  7:33 ` [PATCH 6/8] ARM: rk3568: Detect USB boot Sascha Hauer
2021-10-12  7:33 ` [PATCH 7/8] cdev: Add function to get unallocated start of device Sascha Hauer
2021-10-12  7:52   ` Ahmad Fatoum
2021-10-12  8:31     ` Sascha Hauer
2021-10-12  7:33 ` [PATCH 8/8] ARM: Rockchip: RK3568: implement failsafe barebox update Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox