Skip to contentSkip to conversation

MIPS: add generic device tree 2nd stage support

PATCH Applied Antony Pavlov 2 participants 3 messages Mar 10, 2024 – Mar 13, 2024 MIPS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 MIPS: add generic device tree 2nd stage support

Building and running barebox for 32-bit malta:

export ARCH=mips
export CROSS_COMPILE=mips-linux-gnu-
make -s qemu-malta_defconfig
sed -i "s/# \(CONFIG_BOARD_MIPS_GENERIC_DT\) is not set/\1=y/" .config make -s oldconfig
make -s

qemu-system-mips -M malta -cpu 24Kf -m 256M \
            -nographic -serial mon:stdio \
            -bios images/barebox-qemu-malta.img \
            -net user,tftp=$(pwd) -net nic,model=e1000

Building and running barebox for 64-bit malta:

export ARCH=mips
export CROSS_COMPILE=mips-linux-gnu-
make -s qemu-malta64el_defconfig
sed -i "s/# \(CONFIG_BOARD_MIPS_GENERIC_DT\) is not set/\1=y/" .config make -s oldconfig
make -s

qemu-system-mips64el -M malta -cpu MIPS64R2-generic -m 256M \
            -nographic -serial mon:stdio \
            -bios images/barebox-qemu-malta.img.swapped \
            -net user,tftp=$(pwd) -net nic,model=e1000

Use bootm to run barebox-dt-2nd.img (external device tree):

barebox@qemu malta:/ dhcp
barebox@qemu malta:/ cp /mnt/tftp/images/barebox-dt-2nd.img barebox.img barebox@qemu malta:/ cp /mnt/tftp/arch/mips/dts/qemu-malta.dtb . barebox@qemu malta:/ imd barebox.img
barebox@qemu malta:/ bootm -o qemu-malta.dtb barebox.img

Use bootm to run barebox-qemu-malta.img (encapsulated device tree):

barebox@qemu malta:/ dhcp
barebox@qemu malta:/ cp /mnt/tftp/images/barebox-qemu-malta.img barebox.img barebox@qemu malta:/ imd barebox.img
barebox@qemu malta:/ bootm barebox.img

N.B. Use just the same commands for both 32-bit and 64-bit malta boards.

---
 arch/mips/Kconfig               | 10 ++++++++++
 arch/mips/boards/Makefile       |  2 ++
 arch/mips/boards/board-dt-2nd.S | 35 +++++++++++++++++++++++++++++++++
 arch/mips/boot/main_entry-pbl.c |  4 ++++
 arch/mips/lib/bootm.c           | 13 ++++++++++--
 5 files changed, 62 insertions(+), 2 deletions(-)
arch/mips/Kconfig +10 -0
index 63ba6aa9d5a..14062dee347 100644
@@ -319,6 +319,16 @@ config 64BIT
 
 endchoice
 
+config BOARD_MIPS_GENERIC_DT
+	select BOARD_GENERIC_DT
+	depends on OFDEVICE
+	bool "Build generic MIPS device tree 2nd stage image"
+	help
+	  This enables compilation of a generic image that can be started 2nd
+	  stage from barebox or from qemu. It picks up a device tree passed
+	  in a1 like the Kernel does.
+	  The image will be called images/barebox-dt-2nd.img
+
 menu "MIPS specific settings"
 
 config CMD_MIPS_CPUINFO
arch/mips/boards/Makefile +2 -0
index 9402035856c..c4ce599c934 100644
@@ -13,3 +13,5 @@ obj-$(CONFIG_BOARD_QEMU_MALTA) += qemu-malta/
 obj-$(CONFIG_BOARD_RZX50) += ritmix-rzx50/
 obj-$(CONFIG_BOARD_TPLINK_MR3020) += tplink-mr3020/
 obj-$(CONFIG_BOARD_TPLINK_WDR4300) += tplink-wdr4300/
+
+pbl-$(CONFIG_BOARD_MIPS_GENERIC_DT) += board-dt-2nd.o
arch/mips/boards/board-dt-2nd.S new file +35 -0
new file mode 100644
index 00000000000..a1465f09e36
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Startup Code for generic MIPS device tree 2nd stage image
+ *
+ * Copyright (C) 2024 Antony Pavlov <[email protected]>
+ */
+
+#include <asm/asm.h>
+#include <asm/pbl_macros.h>
+#include <linux/sizes.h>
+
+ENTRY_FUNCTION(start_dt_2nd)
+
+	/* save device tree address in v1 */
+	move	v1, a1
+
+	mips_cpu_setup
+
+	copy_to_link_location	start_dt_2nd
+
+	stack_setup
+
+	/* pbl_main_entry() computes fdt_len by itself
+	 * if fdt == fdt_end */
+	move	a0, v1 /* fdt */
+	move	a1, v1 /* fdt_end */
+	PTR_LI	a2, SZ_256M /* ram_size */
+	PTR_LA	v0, pbl_main_entry
+	jal	v0
+	 nop
+
+	/* No return */
+1:
+	b	1b
+	 nop
arch/mips/boot/main_entry-pbl.c +4 -0
index 78982fd995e..389dc94f370 100644
@@ -11,6 +11,7 @@
 #include <asm/sections.h>
 #include <asm-generic/memory_layout.h>
 #include <debug_ll.h>
+#include <asm/unaligned.h>
 
 extern void *input_data;
 extern void *input_data_end;
@@ -45,6 +46,9 @@ void __section(.text_entry) pbl_main_entry(void *fdt, void *fdt_end,
 	barebox_uncompress(&input_data, piggy_len);
 
 	fdt_len = (unsigned long)fdt_end - (unsigned long)fdt;
+	if (!fdt_len) {
+		fdt_len = get_unaligned_be32((void *)((unsigned long)fdt + 4));
+	}
 	fdt_new = (void *)PAGE_ALIGN_DOWN(TEXT_BASE - MALLOC_SIZE - STACK_SIZE - fdt_len);
 	memcpy(fdt_new, fdt, fdt_len);
 
arch/mips/lib/bootm.c +11 -2
index 643ad57e4db..86573dec7f1 100644
@@ -16,12 +16,21 @@
 
 static int do_bootm_barebox(struct image_data *data)
 {
-	void (*barebox)(void);
+	void (*barebox)(int, void *);
+	void *fdt = NULL;
 
 	barebox = read_file(data->os_file, NULL);
 	if (!barebox)
 		return -EINVAL;
 
+	if (data->oftree_file) {
+		fdt = bootm_get_devicetree(data);
+		if (IS_ERR(fdt)) {
+			pr_err("Failed to load dtb\n");
+			return PTR_ERR(fdt);
+		}
+	}
+
 	if (data->dryrun) {
 		free(barebox);
 		return 0;
@@ -29,7 +38,7 @@ static int do_bootm_barebox(struct image_data *data)
 
 	shutdown_barebox();
 
-	barebox();
+	barebox(-2, fdt);
 
 	restart_machine();
 }
Signature · 1 line
2.39.0
Applied
#1 lore raw
Quoted 8 lines · Antony Pavlov

Building and running barebox for 32-bit malta:

export ARCH=mips
export CROSS_COMPILE=mips-linux-gnu-
make -s qemu-malta_defconfig
sed -i "s/# \(CONFIG_BOARD_MIPS_GENERIC_DT\) is not set/\1=y/" .config make -s oldconfig
make -s

Quoted 6 lines
qemu-system-mips -M malta -cpu 24Kf -m 256M \
            -nographic -serial mon:stdio \
            -bios images/barebox-qemu-malta.img \
            -net user,tftp=$(pwd) -net nic,model=e1000

[...]

Applied, thanks!

[1/1] MIPS: add generic device tree 2nd stage support

      https://git.pengutronix.de/cgit/barebox/commit/?id=041702182a27 (link may not be stable)

Best regards,

Signature · 1 line
Sascha Hauer <[email protected]>
#2 lore raw
In reply to Antony Pavlov
Quoted 106 lines · Antony Pavlov

Building and running barebox for 32-bit malta:

export ARCH=mips
export CROSS_COMPILE=mips-linux-gnu-
make -s qemu-malta_defconfig
sed -i "s/# \(CONFIG_BOARD_MIPS_GENERIC_DT\) is not set/\1=y/" .config make -s oldconfig
make -s

qemu-system-mips -M malta -cpu 24Kf -m 256M \
            -nographic -serial mon:stdio \
            -bios images/barebox-qemu-malta.img \
            -net user,tftp=$(pwd) -net nic,model=e1000

Building and running barebox for 64-bit malta:

export ARCH=mips
export CROSS_COMPILE=mips-linux-gnu-
make -s qemu-malta64el_defconfig
sed -i "s/# \(CONFIG_BOARD_MIPS_GENERIC_DT\) is not set/\1=y/" .config make -s oldconfig
make -s

qemu-system-mips64el -M malta -cpu MIPS64R2-generic -m 256M \
            -nographic -serial mon:stdio \
            -bios images/barebox-qemu-malta.img.swapped \
            -net user,tftp=$(pwd) -net nic,model=e1000

Use bootm to run barebox-dt-2nd.img (external device tree):

barebox@qemu malta:/ dhcp
barebox@qemu malta:/ cp /mnt/tftp/images/barebox-dt-2nd.img barebox.img barebox@qemu malta:/ cp /mnt/tftp/arch/mips/dts/qemu-malta.dtb . barebox@qemu malta:/ imd barebox.img
barebox@qemu malta:/ bootm -o qemu-malta.dtb barebox.img

Use bootm to run barebox-qemu-malta.img (encapsulated device tree):

barebox@qemu malta:/ dhcp
barebox@qemu malta:/ cp /mnt/tftp/images/barebox-qemu-malta.img barebox.img barebox@qemu malta:/ imd barebox.img
barebox@qemu malta:/ bootm barebox.img

N.B. Use just the same commands for both 32-bit and 64-bit malta boards.

---
 arch/mips/Kconfig               | 10 ++++++++++
 arch/mips/boards/Makefile       |  2 ++
 arch/mips/boards/board-dt-2nd.S | 35 +++++++++++++++++++++++++++++++++
 arch/mips/boot/main_entry-pbl.c |  4 ++++
 arch/mips/lib/bootm.c           | 13 ++++++++++--
 5 files changed, 62 insertions(+), 2 deletions(-)
arch/mips/Kconfig +10 -0
index 63ba6aa9d5a..14062dee347 100644
@@ -319,6 +319,16 @@ config 64BIT
 
 endchoice
 
+config BOARD_MIPS_GENERIC_DT
+	select BOARD_GENERIC_DT
+	depends on OFDEVICE
+	bool "Build generic MIPS device tree 2nd stage image"
+	help
+	  This enables compilation of a generic image that can be started 2nd
+	  stage from barebox or from qemu. It picks up a device tree passed
+	  in a1 like the Kernel does.
+	  The image will be called images/barebox-dt-2nd.img
+
 menu "MIPS specific settings"
 
 config CMD_MIPS_CPUINFO
arch/mips/boards/Makefile +2 -0
index 9402035856c..c4ce599c934 100644
@@ -13,3 +13,5 @@ obj-$(CONFIG_BOARD_QEMU_MALTA) += qemu-malta/
 obj-$(CONFIG_BOARD_RZX50) += ritmix-rzx50/
 obj-$(CONFIG_BOARD_TPLINK_MR3020) += tplink-mr3020/
 obj-$(CONFIG_BOARD_TPLINK_WDR4300) += tplink-wdr4300/
+
+pbl-$(CONFIG_BOARD_MIPS_GENERIC_DT) += board-dt-2nd.o
arch/mips/boards/board-dt-2nd.S new file +17 -0
new file mode 100644
index 00000000000..a1465f09e36
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Startup Code for generic MIPS device tree 2nd stage image
+ *
+ * Copyright (C) 2024 Antony Pavlov <[email protected]>
+ */
+
+#include <asm/asm.h>
+#include <asm/pbl_macros.h>
+#include <linux/sizes.h>
+
+ENTRY_FUNCTION(start_dt_2nd)
+
+	/* save device tree address in v1 */
+	move	v1, a1
+
+	mips_cpu_setup

arch/mips/boards/board-dt-2nd.S line 27

Quoted 10 lines
arch/mips/boards/board-dt-2nd.S new file +10 -0
+
+	copy_to_link_location	start_dt_2nd
+
+	stack_setup
+
+	/* pbl_main_entry() computes fdt_len by itself
+	 * if fdt == fdt_end */
+	move	a0, v1 /* fdt */
+	move	a1, v1 /* fdt_end */
+	PTR_LI	a2, SZ_256M /* ram_size */

Could this be taken from dt finally? I think it's good enough for now, but would be a nice improvement.

Sascha

Signature · 4 lines
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 |