Skip to contentSkip to conversation

mach-k3: Rework spl/get_boot_device()

RFC In review Wadim Egorov 2 participants 3 messages Aug 13, 2025 – Sep 10, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover mach-k3: Rework spl/get_boot_device()

This is an attempt to commonize some of the spl/get_boot_device() code. I am interested in factoring out more of the spl_boot_device() code out of the <soc>_init.c files to make it also available for non SPLs.

While at it, I came up with this change to improve the current situation and provide a basis for further cleanup.

Before refactoring any other SoCs to follow this approach, I would like to gather feedback on the concept. Is this a direction we want to go?

The idea is based on commit 784548efb2b7 ("ARM: uniphier: rework spl_boot_device() and related code").

Wadim Egorov (1):
arm: mach-k3: Rework spl/get_boot_device()

 arch/arm/mach-k3/Makefile                     |   2 +-
 arch/arm/mach-k3/am62x/boot.c                 | 129 +++++-------------
 arch/arm/mach-k3/boot-device.c                | 109 +++++++++++++++
 arch/arm/mach-k3/boot-device.h                |  27 ++++
 arch/arm/mach-k3/include/mach/am62_hardware.h |   1 +
 5 files changed, 169 insertions(+), 99 deletions(-)
 create mode 100644 arch/arm/mach-k3/boot-device.c
 create mode 100644 arch/arm/mach-k3/boot-device.h
Signature · 1 line
2.48.1
#1 lore raw

RFC 1/1 arm: mach-k3: Rework spl/get_boot_device()

The current implementation requires every K3 SoC to provide its own get_boot_device()/get_*_bootmedia() functions which look very identical. Rework it using table lookups for SoC data and reduce code duplication.

---
 arch/arm/mach-k3/Makefile                     |   2 +-
 arch/arm/mach-k3/am62x/boot.c                 | 129 +++++-------------
 arch/arm/mach-k3/boot-device.c                | 109 +++++++++++++++
 arch/arm/mach-k3/boot-device.h                |  27 ++++
 arch/arm/mach-k3/include/mach/am62_hardware.h |   1 +
 5 files changed, 169 insertions(+), 99 deletions(-)
 create mode 100644 arch/arm/mach-k3/boot-device.c
 create mode 100644 arch/arm/mach-k3/boot-device.h
arch/arm/mach-k3/Makefile +1 -1
index b2fd5810b67..7dc77328e5c 100644
@@ -6,7 +6,7 @@
 obj-$(CONFIG_ARM64) += arm64/
 obj-$(CONFIG_CPU_V7R) += r5/
 obj-$(CONFIG_OF_LIBFDT) += common_fdt.o
-obj-y += common.o security.o k3-ddr.o
+obj-y += common.o security.o k3-ddr.o boot-device.o
 obj-$(CONFIG_SOC_K3_AM62A7) += am62ax/
 obj-$(CONFIG_SOC_K3_AM62P5) += am62px/
 obj-$(CONFIG_SOC_K3_AM625) += am62x/
arch/arm/mach-k3/am62x/boot.c +31 -98

138 lines not on this page · raw message

arch/arm/mach-k3/boot-device.c new file +109 -0

109 lines not on this page · raw message

arch/arm/mach-k3/boot-device.h new file +27 -0
new file mode 100644
index 00000000000..351f710151b
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ *  Copyright (C) 2025 PHYTEC Messtechnik GmbH
+ *  Author: Wadim Egorov <[email protected]>
+ */
+
+#ifndef _K3_BOOT_DEVICE_H
+#define _K3_BOOT_DEVICE_H
+
+#include <linux/types.h>
+
+/* Descriptor for mode + optional cfg test -> normalized device */
+struct k3_boot_map {
+	u32 mode;	/* Raw PRIMARY_BOOTMODE value */
+	u32 cfg_mask;	/* Bits in devstat-cfg to test (0 if none) */
+	u32 cfg_shift;	/* Shift right before comparing */
+	u32 cfg_value;	/* Desired value after mask+shift */
+	u32 result;	/* Normalized device to return */
+};
+
+extern struct k3_boot_map am62_boot_device_primary_table[];
+extern unsigned int am62_boot_device_primary_table_count;
+
+extern struct k3_boot_map am62_boot_device_backup_table[];
+extern unsigned int am62_boot_device_backup_table_count;
+
+#endif /* _K3_BOOT_DEVICE_H */
arch/arm/mach-k3/include/mach/am62_hardware.h +1 -0
index 2f5655bf24a..084f16bf2d2 100644
@@ -10,6 +10,7 @@
 #define __ASM_ARCH_AM62_HARDWARE_H
 
 #include <config.h>
+#include <asm/io.h>
 #ifndef __ASSEMBLY__
 #include <linux/bitops.h>
 #endif
Signature · 1 line
2.48.1
#2 lore raw

Re: [RFC 1/1] arm: mach-k3: Rework spl/get_boot_device()

Hello Wadim,

The direction of your patch looks good to me, thanks for taking the time to refactor!

A few comments below:

Quoted 32 lines · Wadim Egorov

The current implementation requires every K3 SoC to provide its own get_boot_device()/get_*_bootmedia() functions which look very identical. Rework it using table lookups for SoC data and reduce code duplication.

---
 arch/arm/mach-k3/Makefile                     |   2 +-
 arch/arm/mach-k3/am62x/boot.c                 | 129 +++++-------------
 arch/arm/mach-k3/boot-device.c                | 109 +++++++++++++++
 arch/arm/mach-k3/boot-device.h                |  27 ++++
 arch/arm/mach-k3/include/mach/am62_hardware.h |   1 +
 5 files changed, 169 insertions(+), 99 deletions(-)
 create mode 100644 arch/arm/mach-k3/boot-device.c
 create mode 100644 arch/arm/mach-k3/boot-device.h
arch/arm/mach-k3/Makefile +1 -1
index b2fd5810b67..7dc77328e5c 100644
@@ -6,7 +6,7 @@
 obj-$(CONFIG_ARM64) += arm64/
 obj-$(CONFIG_CPU_V7R) += r5/
 obj-$(CONFIG_OF_LIBFDT) += common_fdt.o
-obj-y += common.o security.o k3-ddr.o
+obj-y += common.o security.o k3-ddr.o boot-device.o
 obj-$(CONFIG_SOC_K3_AM62A7) += am62ax/
 obj-$(CONFIG_SOC_K3_AM62P5) += am62px/
 obj-$(CONFIG_SOC_K3_AM625) += am62x/
arch/arm/mach-k3/am62x/boot.c +0 -0
index a3a6cda6bdb..6d458f546a6 100644

[snip]

Quoted 24 lines
arch/arm/mach-k3/am62x/boot.c +20 -0
@@ -141,3 +44,33 @@ const char *get_reset_reason(void)
 
 	return "UNKNOWN";
 }
+
+struct k3_boot_map am62_boot_device_primary_table[] = {
+	{ BOOT_DEVICE_OSPI, 0, 0, 0, BOOT_DEVICE_SPI },
+	{ BOOT_DEVICE_QSPI, 0, 0, 0, BOOT_DEVICE_SPI },
+	{ BOOT_DEVICE_XSPI, 0, 0, 0, BOOT_DEVICE_SPI },
+	{ BOOT_DEVICE_SPI, 0, 0, 0, BOOT_DEVICE_SPI },
+	{ BOOT_DEVICE_ETHERNET_RGMII, 0, 0, 0, BOOT_DEVICE_ETHERNET },
+	{ BOOT_DEVICE_ETHERNET_RMII, 0, 0, 0, BOOT_DEVICE_ETHERNET },
+	{ BOOT_DEVICE_EMMC, 0, 0, 0, BOOT_DEVICE_MMC1 },
+	{ BOOT_DEVICE_MMC, MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK, MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT, 1, BOOT_DEVICE_MMC2 },
+	{ BOOT_DEVICE_MMC, MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK, MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT, 0, BOOT_DEVICE_MMC1 },
+	{ BOOT_DEVICE_DFU, MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK, MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT, 1, BOOT_DEVICE_USB },
+	{ BOOT_DEVICE_DFU, MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK, MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT, 0, BOOT_DEVICE_DFU },
+	{ BOOT_DEVICE_NOBOOT, 0, 0, 0, BOOT_DEVICE_RAM },
+};
+
+struct k3_boot_map am62_boot_device_backup_table[] = {
+	{ BACKUP_BOOT_DEVICE_UART, 0, 0, 0, BOOT_DEVICE_UART },
+	{ BACKUP_BOOT_DEVICE_USB, 0, 0, 0, BOOT_DEVICE_USB },
+	{ BACKUP_BOOT_DEVICE_ETHERNET, 0, 0, 0, BOOT_DEVICE_ETHERNET },

arch/arm/mach-k3/am62x/boot.c const char *get_reset_reason(void) · line 76

Quoted 10 lines
arch/arm/mach-k3/am62x/boot.c +10 -0
+	{ BACKUP_BOOT_DEVICE_MMC, MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK, MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT, 0, BOOT_DEVICE_MMC1 },
+	{ BACKUP_BOOT_DEVICE_MMC, MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK, MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT, 1, BOOT_DEVICE_MMC2 },
+	{ BACKUP_BOOT_DEVICE_SPI, 0, 0, 0, BOOT_DEVICE_SPI },
+	{ BACKUP_BOOT_DEVICE_I2C, 0, 0, 0, BOOT_DEVICE_I2C },
+	{ BACKUP_BOOT_DEVICE_DFU, MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK, MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT, 0, BOOT_DEVICE_DFU },
+	{ BACKUP_BOOT_DEVICE_DFU, MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK, MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT, 1, BOOT_DEVICE_USB },
+};
+
+unsigned int am62_boot_device_primary_table_count = ARRAY_SIZE(am62_boot_device_primary_table);
+unsigned int am62_boot_device_backup_table_count = ARRAY_SIZE(am62_boot_device_backup_table);

It might be better to have boot-device.h export an extern that the boards can set. Something like this:

boot-device.h:

	extern struct k3_boot_device_info *info;

am62x/boot.c:

static const struct k3_boot_device_info am62_map = {
.boot_device_primary_table = am62_boot_device_primary_table, .boot_device_primary_count = ARRAY_SIZE(am62_boot_device_primary_table), .boot_device_backup_table = am62_boot_device_backup_table, .boot_device_backup_count = ARRAY_SIZE(am62_boot_device_backup_table), .main_devstat_reg = (void __iomem *)CTRLMMR_MAIN_DEVSTAT, .wkup_devstat_reg = 0,
.bootmode_addr = (void __iomem *)K3_BOOT_PARAM_TABLE_INDEX_OCRAM,
};

struct k3_boot_device_info *info = &am62_map;

This would allow you to remove the SOC specifics ifdefs from boot-device.c below.

Quoted 31 lines
arch/arm/mach-k3/boot-device.c new file +25 -0
new file mode 100644
index 00000000000..9b4f0ae7aa6
@@ -0,0 +1,109 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ *  Copyright (C) 2025 PHYTEC Messtechnik GmbH
+ *  Author: Wadim Egorov <[email protected]>
+ */
+
+#include "boot-device.h"
+
+#include <asm/io.h>
+#include <asm/arch/hardware.h>
+#include <asm/arch/am62_spl.h>
+
+struct k3_boot_device_info {
+	struct k3_boot_map *boot_device_primary_table;
+	unsigned int *boot_device_primary_count;
+	struct k3_boot_map *boot_device_backup_table;
+	unsigned int *boot_device_backup_count;
+	void __iomem *main_devstat_reg;
+	void __iomem *wkup_devstat_reg;
+	void __iomem *bootmode_addr;
+};
+
+static const struct k3_boot_device_info k3_boot_device_info[] = {
+#if defined(CONFIG_SOC_K3_AM625)
+	{

arch/arm/mach-k3/boot-device.c line 35

Quoted 10 lines
arch/arm/mach-k3/boot-device.c new file +10 -0
+		.boot_device_primary_table = am62_boot_device_primary_table,
+		.boot_device_primary_count = &am62_boot_device_primary_table_count,
+		.boot_device_backup_table = am62_boot_device_backup_table,
+		.boot_device_backup_count = &am62_boot_device_backup_table_count,
+		.main_devstat_reg = (void __iomem *)CTRLMMR_MAIN_DEVSTAT,
+		.wkup_devstat_reg = 0,
+		.bootmode_addr = (void __iomem *)K3_BOOT_PARAM_TABLE_INDEX_OCRAM,
+	},
+#endif
+};

Why use an array here? We are only making use of the first entry anyways.

Quoted 44 lines
arch/arm/mach-k3/boot-device.c new file +44 -0
+
+static u32 __get_backup_bootmedia(u32 devstat)
+{
+	u32 bkup_bootmode = (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
+			     MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
+	u32 bkup_bootmode_cfg = (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
+				 MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
+
+	const struct k3_boot_device_info *info = &k3_boot_device_info[0];
+	unsigned int i;
+
+	for (i = 0; i < *info->boot_device_backup_count; i++) {
+		struct k3_boot_map *m = &info->boot_device_backup_table[i];
+
+		if (bkup_bootmode != m->mode)
+			continue;
+
+		if (m->cfg_mask == 0)
+			return m->result;
+
+		if (((bkup_bootmode_cfg & m->cfg_mask) >> m->cfg_shift) == m->cfg_value)
+			return m->result;
+	}
+
+	return BOOT_DEVICE_RAM;
+}
+
+static u32 __get_primary_bootmedia(u32 devstat)
+{
+	u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
+			MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
+	u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
+			MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
+	const struct k3_boot_device_info *info = &k3_boot_device_info[0];
+	unsigned int i;
+
+	for (i = 0; i < *info->boot_device_primary_count; i++) {
+		struct k3_boot_map *m = &info->boot_device_primary_table[i];
+
+		if (bootmode != m->mode)
+			continue;
+
+		if (m->cfg_mask == 0)
+			return m->result;

arch/arm/mach-k3/boot-device.c line 89

Quoted 10 lines
arch/arm/mach-k3/boot-device.c new file +10 -0
+
+		if (((bootmode_cfg & m->cfg_mask) >> m->cfg_shift) == m->cfg_value)
+			return m->result;
+	}
+
+	return bootmode;
+}
+
+u32 get_boot_device(void)
+{

This might be better kept as a weak function.

Quoted 42 lines
arch/arm/mach-k3/boot-device.c new file +20 -0
+	const struct k3_boot_device_info *info = &k3_boot_device_info[0];
+	u32 bootmode = readl(info->bootmode_addr);
+	u32 main_devstat = readl(info->main_devstat_reg);
+	u32 bootmedia;
+
+	if (!ARRAY_SIZE(k3_boot_device_info)) {
+		pr_err("%s: no boot-device info for this SoC\n", __func__);
+		return BOOT_DEVICE_NOBOOT;
+	}
+
+	if (bootmode == K3_PRIMARY_BOOTMODE)
+		bootmedia = __get_primary_bootmedia(main_devstat);
+	else
+		bootmedia = __get_backup_bootmedia(main_devstat);
+
+	debug("%s: devstat = 0x%x bootmedia = 0x%x bootmode = %d\n",
+	      __func__, main_devstat, bootmedia, bootmode);
+
+	return bootmedia;
+}
arch/arm/mach-k3/boot-device.h new file +16 -0
new file mode 100644
index 00000000000..351f710151b
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ *  Copyright (C) 2025 PHYTEC Messtechnik GmbH
+ *  Author: Wadim Egorov <[email protected]>
+ */
+
+#ifndef _K3_BOOT_DEVICE_H
+#define _K3_BOOT_DEVICE_H
+
+#include <linux/types.h>
+
+/* Descriptor for mode + optional cfg test -> normalized device */
+struct k3_boot_map {
+	u32 mode;	/* Raw PRIMARY_BOOTMODE value */
+	u32 cfg_mask;	/* Bits in devstat-cfg to test (0 if none) */
+	u32 cfg_shift;	/* Shift right before comparing */

arch/arm/mach-k3/boot-device.h line 26

Quoted 10 lines
arch/arm/mach-k3/boot-device.h new file +10 -0
+	u32 cfg_value;	/* Desired value after mask+shift */
+	u32 result;	/* Normalized device to return */
+};
+
+extern struct k3_boot_map am62_boot_device_primary_table[];
+extern unsigned int am62_boot_device_primary_table_count;
+
+extern struct k3_boot_map am62_boot_device_backup_table[];
+extern unsigned int am62_boot_device_backup_table_count;
+

You wouldn't need these 4 externs per SoC too with the above suggestion.

Regards,
Anshul

Quoted 13 lines
arch/arm/mach-k3/boot-device.h new file +1 -0
+#endif /* _K3_BOOT_DEVICE_H */
arch/arm/mach-k3/include/mach/am62_hardware.h +1 -0
index 2f5655bf24a..084f16bf2d2 100644
@@ -10,6 +10,7 @@
 #define __ASM_ARCH_AM62_HARDWARE_H
 
 #include <config.h>
+#include <asm/io.h>
 #ifndef __ASSEMBLY__
 #include <linux/bitops.h>
 #endif