Skip to contentSkip to conversation

mach-k3: Rework spl/get_boot_device()

PATCH Superseded Wadim Egorov 2 participants 6 messages Oct 2, 2025 – Oct 29, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover mach-k3: Rework spl/get_boot_device()

This may need an rebase after the MMC macros fixup patch from Judith [1]. Also, if we want to extend it to all K3 SoCs, some of the Jacinto devices would need an update of their DEVSTAT masks to be relativ to the bootmode config.

RFC: https://lists.denx.de/pipermail/u-boot/2025-August/596543.html
v1:
Update based on suggestions from Anshul:
- Use k3_get_boot_device_info() to get rid of multiple extern variables
- Implement k3_get_boot_device_info() for am62x/boot.c
- Removed k3_boot_device_info[] array
- Add info param to bootmedia functions
- Apply to AM64x and AM62Ax

[1] https://lists.denx.de/pipermail/u-boot/2025-September/598239.html

Wadim Egorov (3):
arm: mach-k3: Rework spl/get_boot_device()
arm: mach-k3: am62ax: Switch to boot-device helper
arm: mach-k3: am64x: Switch to boot-device helper

 arch/arm/mach-k3/Makefile                     |   2 +-
 arch/arm/mach-k3/am62ax/boot.c                | 141 ++++++------------
 arch/arm/mach-k3/am62x/boot.c                 | 141 ++++++------------
 arch/arm/mach-k3/am64x/boot.c                 | 139 +++++------------
 arch/arm/mach-k3/boot-device.c                |  90 +++++++++++
 arch/arm/mach-k3/boot-device.h                |  34 +++++
 arch/arm/mach-k3/include/mach/am62_hardware.h |   1 +
 7 files changed, 252 insertions(+), 296 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

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

Introduce a shared boot-device helper that translates DEVSTAT boot modes via SoC-specific lookup tables. This replaces the per SoC get_boot_device()/get_*_bootmedia() implementations and centralizes the logic in boot-device.c while keeping the mapping data SoC-owned.

Start with am62x as a reference implementation.

---
 arch/arm/mach-k3/Makefile                     |   2 +-
 arch/arm/mach-k3/am62x/boot.c                 | 141 ++++++------------
 arch/arm/mach-k3/boot-device.c                |  90 +++++++++++
 arch/arm/mach-k3/boot-device.h                |  34 +++++
 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 +43 -98

150 lines not on this page · raw message

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

90 lines not on this page · raw message

arch/arm/mach-k3/boot-device.h new file +34 -0
new file mode 100644
index 00000000000..965a4570710
@@ -0,0 +1,34 @@
+// 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>
+#include <asm/io.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 */
+};
+
+struct k3_boot_device_info {
+	const struct k3_boot_map *boot_device_primary_table;
+	unsigned int boot_device_primary_count;
+	const 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;
+};
+
+const struct k3_boot_device_info *k3_get_boot_device_info(void);
+
+#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
Acked-by on v1 1/3
#2 lore raw

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

Hi Wadim, I have noted a few comments below but overall the patch set looks good to me.

Quoted 195 lines · Wadim Egorov

Introduce a shared boot-device helper that translates DEVSTAT boot modes via SoC-specific lookup tables. This replaces the per SoC get_boot_device()/get_*_bootmedia() implementations and centralizes the logic in boot-device.c while keeping the mapping data SoC-owned.

Start with am62x as a reference implementation.

---
 arch/arm/mach-k3/Makefile                     |   2 +-
 arch/arm/mach-k3/am62x/boot.c                 | 141 ++++++------------
 arch/arm/mach-k3/boot-device.c                |  90 +++++++++++
 arch/arm/mach-k3/boot-device.h                |  34 +++++
 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 +43 -98
index a3a6cda6bdb..06bd04da99f 100644
@@ -3,104 +3,7 @@
 #include <asm/arch/hardware.h>
 #include <asm/arch/am62_spl.h>
 
-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;
-
-	switch (bkup_bootmode) {
-	case BACKUP_BOOT_DEVICE_UART:
-		return BOOT_DEVICE_UART;
-
-	case BACKUP_BOOT_DEVICE_USB:
-		return BOOT_DEVICE_USB;
-
-	case BACKUP_BOOT_DEVICE_ETHERNET:
-		return BOOT_DEVICE_ETHERNET;
-
-	case BACKUP_BOOT_DEVICE_MMC:
-		if (bkup_bootmode_cfg)
-			return BOOT_DEVICE_MMC2;
-		return BOOT_DEVICE_MMC1;
-
-	case BACKUP_BOOT_DEVICE_SPI:
-		return BOOT_DEVICE_SPI;
-
-	case BACKUP_BOOT_DEVICE_I2C:
-		return BOOT_DEVICE_I2C;
-
-	case BACKUP_BOOT_DEVICE_DFU:
-		if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
-			return BOOT_DEVICE_USB;
-		return BOOT_DEVICE_DFU;
-	};
-
-	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;
-
-	switch (bootmode) {
-	case BOOT_DEVICE_OSPI:
-		fallthrough;
-	case BOOT_DEVICE_QSPI:
-		fallthrough;
-	case BOOT_DEVICE_XSPI:
-		fallthrough;
-	case BOOT_DEVICE_SPI:
-		return BOOT_DEVICE_SPI;
-
-	case BOOT_DEVICE_ETHERNET_RGMII:
-		fallthrough;
-	case BOOT_DEVICE_ETHERNET_RMII:
-		return BOOT_DEVICE_ETHERNET;
-
-	case BOOT_DEVICE_EMMC:
-		return BOOT_DEVICE_MMC1;
-
-	case BOOT_DEVICE_MMC:
-		if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
-				MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
-			return BOOT_DEVICE_MMC2;
-		return BOOT_DEVICE_MMC1;
-
-	case BOOT_DEVICE_DFU:
-		if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
-		    MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
-			return BOOT_DEVICE_USB;
-		return BOOT_DEVICE_DFU;
-
-	case BOOT_DEVICE_NOBOOT:
-		return BOOT_DEVICE_RAM;
-	}
-
-	return bootmode;
-}
-
-u32 get_boot_device(void)
-{
-	u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
-	u32 bootmode = *(u32 *)(K3_BOOT_PARAM_TABLE_INDEX_OCRAM);
-	u32 bootmedia;
-
-	if (bootmode == K3_PRIMARY_BOOTMODE)
-		bootmedia = __get_primary_bootmedia(devstat);
-	else
-		bootmedia = __get_backup_bootmedia(devstat);
-
-	debug("%s: devstat = 0x%x bootmedia = 0x%x bootmode = %d\n",
-	      __func__, devstat, bootmedia, bootmode);
-
-	return bootmedia;
-}
+#include "../boot-device.h"
 
 const char *get_reset_reason(void)
 {
@@ -141,3 +44,45 @@ const char *get_reset_reason(void)
 
 	return "UNKNOWN";
 }
+
+static const 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 },
+};
+
+static const 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 },
+	{ BACKUP_BOOT_DEVICE_MMC, 1, 0, 1, BOOT_DEVICE_MMC2 },
+	{ BACKUP_BOOT_DEVICE_MMC, 1, 0, 0, BOOT_DEVICE_MMC1 },
+	{ 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_USB_MODE_MASK, 0, 0, BOOT_DEVICE_DFU },
+	{ BACKUP_BOOT_DEVICE_DFU, MAIN_DEVSTAT_BACKUP_USB_MODE_MASK, 0, 1, BOOT_DEVICE_USB },
+};
+
+static const struct k3_boot_device_info am62_boot_device_info = {
+	.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 = NULL,
+	.bootmode_addr = (void __iomem *)K3_BOOT_PARAM_TABLE_INDEX_OCRAM,
+};
+
+const struct k3_boot_device_info *k3_get_boot_device_info(void)
+{
+	return &am62_boot_device_info;
+}
arch/arm/mach-k3/boot-device.c new file +2 -0
new file mode 100644
index 00000000000..9431dae71fa
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*

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

Quoted 10 lines
arch/arm/mach-k3/boot-device.c new file +10 -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>
+

We shouldn't be including an am62x specific header in a mach-k3 file meant for multiple SoCs. Please refactor the macros used below so it's no longer required.

One idea could be to let the SoC's boot.c handle parsing the devstat and bootmode (primary or not). With get_boot_device in boot-device.c only receiving the final bootmode and bootmode_cfg as part of the k3_boot_device_info struct.

This would also allow Jacinto platforms to easily make use of this refactor since the handling of wkup and main devstat would be handled as part of the SoC's boot.c.

Quoted 45 lines
arch/arm/mach-k3/boot-device.c new file +45 -0
+__weak const struct k3_boot_device_info *k3_get_boot_device_info(void)
+{
+	return NULL;
+}
+
+static u32 __get_backup_bootmedia(const struct k3_boot_device_info *info, 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;
+	unsigned int i;
+
+	for (i = 0; i < info->boot_device_backup_count; i++) {
+		const 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(const struct k3_boot_device_info *info, 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;
+	unsigned int i;
+
+	for (i = 0; i < info->boot_device_primary_count; i++) {
+		const 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 67

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 __weak get_boot_device(void)
+{

Having a k3 specific get_boot_device called by U-Boot's generic spl_boot_device from SoC's init file is convoluted.

We should probably just replace get_boot_device with spl_boot_device in a later patch after this series.

Quoted 40 lines
arch/arm/mach-k3/boot-device.c new file +23 -0
+	const struct k3_boot_device_info *info = k3_get_boot_device_info();
+	u32 bootmode;
+	u32 main_devstat;
+	u32 bootmedia;
+
+	if (!info) {
+		pr_err("%s: no boot-device info for this SoC\n", __func__);
+		return BOOT_DEVICE_NOBOOT;
+	}
+
+	bootmode = readl(info->bootmode_addr);
+	main_devstat = readl(info->main_devstat_reg);
+
+	if (bootmode == K3_PRIMARY_BOOTMODE)
+		bootmedia = __get_primary_bootmedia(info, main_devstat);
+	else
+		bootmedia = __get_backup_bootmedia(info, 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 +11 -0
new file mode 100644
index 00000000000..965a4570710
@@ -0,0 +1,34 @@
+// 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>
+#include <asm/io.h>

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

Quoted 10 lines
arch/arm/mach-k3/boot-device.h new file +10 -0
+
+/* 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 */
+};
+

Nit: This could be better documented following the standard '@' in a comment above the struct.

Quoted 18 lines
arch/arm/mach-k3/boot-device.h new file +13 -0
+struct k3_boot_device_info {
+	const struct k3_boot_map *boot_device_primary_table;
+	unsigned int boot_device_primary_count;
+	const 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;
+};
+
+const struct k3_boot_device_info *k3_get_boot_device_info(void);
+
+#endif /* _K3_BOOT_DEVICE_H */
arch/arm/mach-k3/include/mach/am62_hardware.h +0 -0
index 2f5655bf24a..084f16bf2d2 100644

arch/arm/mach-k3/include/mach/am62_hardware.h line 13

Quoted 4 lines
arch/arm/mach-k3/include/mach/am62_hardware.h +1 -0
@@ -10,6 +10,7 @@
 #define __ASM_ARCH_AM62_HARDWARE_H
 
 #include <config.h>
+#include <asm/io.h>

Why do we need this header here?

arch/arm/mach-k3/include/mach/am62_hardware.h line 16

Quoted 3 lines
arch/arm/mach-k3/include/mach/am62_hardware.h +0 -0
 #ifndef __ASSEMBLY__
 #include <linux/bitops.h>
 #endif

Regards,
Anshul

#3 lore raw

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

Hi Anshul,

Quoted 205 lines · Anshul Dalal

Hi Wadim, I have noted a few comments below but overall the patch set looks good to me.

Quoted 201 lines · Wadim Egorov

Introduce a shared boot-device helper that translates DEVSTAT boot modes via SoC-specific lookup tables. This replaces the per SoC get_boot_device()/get_*_bootmedia() implementations and centralizes the logic in boot-device.c while keeping the mapping data SoC-owned.

Start with am62x as a reference implementation.

---
  arch/arm/mach-k3/Makefile                     |   2 +-
  arch/arm/mach-k3/am62x/boot.c                 | 141 ++++++------------
  arch/arm/mach-k3/boot-device.c                |  90 +++++++++++
  arch/arm/mach-k3/boot-device.h                |  34 +++++
  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 +43 -98
index a3a6cda6bdb..06bd04da99f 100644
@@ -3,104 +3,7 @@
  #include <asm/arch/hardware.h>
  #include <asm/arch/am62_spl.h>
 
-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;
-
-	switch (bkup_bootmode) {
-	case BACKUP_BOOT_DEVICE_UART:
-		return BOOT_DEVICE_UART;
-
-	case BACKUP_BOOT_DEVICE_USB:
-		return BOOT_DEVICE_USB;
-
-	case BACKUP_BOOT_DEVICE_ETHERNET:
-		return BOOT_DEVICE_ETHERNET;
-
-	case BACKUP_BOOT_DEVICE_MMC:
-		if (bkup_bootmode_cfg)
-			return BOOT_DEVICE_MMC2;
-		return BOOT_DEVICE_MMC1;
-
-	case BACKUP_BOOT_DEVICE_SPI:
-		return BOOT_DEVICE_SPI;
-
-	case BACKUP_BOOT_DEVICE_I2C:
-		return BOOT_DEVICE_I2C;
-
-	case BACKUP_BOOT_DEVICE_DFU:
-		if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
-			return BOOT_DEVICE_USB;
-		return BOOT_DEVICE_DFU;
-	};
-
-	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;
-
-	switch (bootmode) {
-	case BOOT_DEVICE_OSPI:
-		fallthrough;
-	case BOOT_DEVICE_QSPI:
-		fallthrough;
-	case BOOT_DEVICE_XSPI:
-		fallthrough;
-	case BOOT_DEVICE_SPI:
-		return BOOT_DEVICE_SPI;
-
-	case BOOT_DEVICE_ETHERNET_RGMII:
-		fallthrough;
-	case BOOT_DEVICE_ETHERNET_RMII:
-		return BOOT_DEVICE_ETHERNET;
-
-	case BOOT_DEVICE_EMMC:
-		return BOOT_DEVICE_MMC1;
-
-	case BOOT_DEVICE_MMC:
-		if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
-				MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
-			return BOOT_DEVICE_MMC2;
-		return BOOT_DEVICE_MMC1;
-
-	case BOOT_DEVICE_DFU:
-		if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
-		    MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
-			return BOOT_DEVICE_USB;
-		return BOOT_DEVICE_DFU;
-
-	case BOOT_DEVICE_NOBOOT:
-		return BOOT_DEVICE_RAM;
-	}
-
-	return bootmode;
-}
-
-u32 get_boot_device(void)
-{
-	u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
-	u32 bootmode = *(u32 *)(K3_BOOT_PARAM_TABLE_INDEX_OCRAM);
-	u32 bootmedia;
-
-	if (bootmode == K3_PRIMARY_BOOTMODE)
-		bootmedia = __get_primary_bootmedia(devstat);
-	else
-		bootmedia = __get_backup_bootmedia(devstat);
-
-	debug("%s: devstat = 0x%x bootmedia = 0x%x bootmode = %d\n",
-	      __func__, devstat, bootmedia, bootmode);
-
-	return bootmedia;
-}
+#include "../boot-device.h"
 
  const char *get_reset_reason(void)
  {
@@ -141,3 +44,45 @@ const char *get_reset_reason(void)
 
  	return "UNKNOWN";
  }
+
+static const 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 },
+};
+
+static const 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 },
+	{ BACKUP_BOOT_DEVICE_MMC, 1, 0, 1, BOOT_DEVICE_MMC2 },
+	{ BACKUP_BOOT_DEVICE_MMC, 1, 0, 0, BOOT_DEVICE_MMC1 },
+	{ 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_USB_MODE_MASK, 0, 0, BOOT_DEVICE_DFU },
+	{ BACKUP_BOOT_DEVICE_DFU, MAIN_DEVSTAT_BACKUP_USB_MODE_MASK, 0, 1, BOOT_DEVICE_USB },
+};
+
+static const struct k3_boot_device_info am62_boot_device_info = {
+	.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 = NULL,
+	.bootmode_addr = (void __iomem *)K3_BOOT_PARAM_TABLE_INDEX_OCRAM,
+};
+
+const struct k3_boot_device_info *k3_get_boot_device_info(void)
+{
+	return &am62_boot_device_info;
+}
arch/arm/mach-k3/boot-device.c new file +8 -0
new file mode 100644
index 00000000000..9431dae71fa
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ *  Copyright (C) 2025 PHYTEC Messtechnik GmbH
+ *  Author: Wadim Egorov <[email protected]>
+ */
+
+#include "boot-device.h"
+
Quoted 8 lines

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

Quoted 4 lines
arch/arm/mach-k3/boot-device.c new file +4 -0
+#include <asm/io.h>
+#include <asm/arch/hardware.h>
+#include <asm/arch/am62_spl.h>
+

We shouldn't be including an am62x specific header in a mach-k3 file meant for multiple SoCs. Please refactor the macros used below so it's no longer required.

Good point, I will update it.

Quoted 5 lines

One idea could be to let the SoC's boot.c handle parsing the devstat and bootmode (primary or not). With get_boot_device in boot-device.c only receiving the final bootmode and bootmode_cfg as part of the k3_boot_device_info struct.

This means we will end up with code duplication again, every SoC/boot.c will have identical parsing code. Let's not do this.

Quoted 4 lines

This would also allow Jacinto platforms to easily make use of this refactor since the handling of wkup and main devstat would be handled as part of the SoC's boot.c.

I don't think it is a big deal to extend for jacinto here.

Quoted 60 lines
Quoted 46 lines
arch/arm/mach-k3/boot-device.c new file +45 -0
+__weak const struct k3_boot_device_info *k3_get_boot_device_info(void)
+{
+	return NULL;
+}
+
+static u32 __get_backup_bootmedia(const struct k3_boot_device_info *info, 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;
+	unsigned int i;
+
+	for (i = 0; i < info->boot_device_backup_count; i++) {
+		const 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(const struct k3_boot_device_info *info, 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;
+	unsigned int i;
+
+	for (i = 0; i < info->boot_device_primary_count; i++) {
+		const 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 67

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 __weak get_boot_device(void)
+{

Having a k3 specific get_boot_device called by U-Boot's generic spl_boot_device from SoC's init file is convoluted.

Quoted 2 lines

We should probably just replace get_boot_device with spl_boot_device in a later patch after this series.

I have introduced get_boot_device() because our board code needs this information in various stages. See board/phytec/common/k3/board.c

Quoted 46 lines
Quoted 46 lines
arch/arm/mach-k3/boot-device.c new file +23 -0
+	const struct k3_boot_device_info *info = k3_get_boot_device_info();
+	u32 bootmode;
+	u32 main_devstat;
+	u32 bootmedia;
+
+	if (!info) {
+		pr_err("%s: no boot-device info for this SoC\n", __func__);
+		return BOOT_DEVICE_NOBOOT;
+	}
+
+	bootmode = readl(info->bootmode_addr);
+	main_devstat = readl(info->main_devstat_reg);
+
+	if (bootmode == K3_PRIMARY_BOOTMODE)
+		bootmedia = __get_primary_bootmedia(info, main_devstat);
+	else
+		bootmedia = __get_backup_bootmedia(info, 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..965a4570710
@@ -0,0 +1,34 @@
+// 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>
+#include <asm/io.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) */
Quoted 8 lines

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

Quoted 5 lines
arch/arm/mach-k3/boot-device.h new file +5 -0
+	u32 cfg_shift;	/* Shift right before comparing */
+	u32 cfg_value;	/* Desired value after mask+shift */
+	u32 result;	/* Normalized device to return */
+};
+

Nit: This could be better documented following the standard '@' in a comment above the struct.

Can do

Quoted 21 lines
Quoted 21 lines
arch/arm/mach-k3/boot-device.h new file +13 -0
+struct k3_boot_device_info {
+	const struct k3_boot_map *boot_device_primary_table;
+	unsigned int boot_device_primary_count;
+	const 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;
+};
+
+const struct k3_boot_device_info *k3_get_boot_device_info(void);
+
+#endif /* _K3_BOOT_DEVICE_H */
arch/arm/mach-k3/include/mach/am62_hardware.h +0 -0
index 2f5655bf24a..084f16bf2d2 100644
@@ -10,6 +10,7 @@
  #define __ASM_ARCH_AM62_HARDWARE_H
 
Quoted 4 lines

arch/arm/mach-k3/include/mach/am62_hardware.h line 13

Quoted 2 lines
arch/arm/mach-k3/include/mach/am62_hardware.h +1 -0
  #include <config.h>
+#include <asm/io.h>

Why do we need this header here?

There was a compile error in a previous version. A separate patch would be better, sorry. But I will drop it since there is no Issue anymore.

Regards,
Wadim

Quoted 9 lines

arch/arm/mach-k3/include/mach/am62_hardware.h line 16

Quoted 4 lines
arch/arm/mach-k3/include/mach/am62_hardware.h +0 -0
  #ifndef __ASSEMBLY__
  #include <linux/bitops.h>
  #endif

Regards,
Anshul

#4 lore raw

PATCH 2/3 arm: mach-k3: am62ax: Switch to boot-device helper

In reply to Wadim Egorov

Update am62ax to use the new shared boot-device helper instead of its own get_boot_device() implementation.

---
 arch/arm/mach-k3/am62ax/boot.c | 141 ++++++++++-----------------------
 1 file changed, 42 insertions(+), 99 deletions(-)
arch/arm/mach-k3/am62ax/boot.c +42 -99
index af7fce1e5e9..364bf9ed026 100644
@@ -3,104 +3,47 @@
 #include <asm/arch/hardware.h>
 #include <asm/arch/am62a_spl.h>
 
-static u32 __get_backup_bootmedia(u32 devstat)
+#include "../boot-device.h"
+
+static const struct k3_boot_map am62a_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_SPI_NAND, 0, 0, 0, BOOT_DEVICE_SPINAND },
+	{ 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 },
+};
+
+static const struct k3_boot_map am62a_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 },
+	{ BACKUP_BOOT_DEVICE_MMC, 1, 0, 1, BOOT_DEVICE_MMC2 },
+	{ BACKUP_BOOT_DEVICE_MMC, 1, 0, 0, BOOT_DEVICE_MMC1 },
+	{ 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_USB_MODE_MASK, 0, 0, BOOT_DEVICE_DFU },
+	{ BACKUP_BOOT_DEVICE_DFU, MAIN_DEVSTAT_BACKUP_USB_MODE_MASK, 0, 1, BOOT_DEVICE_USB },
+};
+
+static const struct k3_boot_device_info am62a_boot_device_info = {
+	.boot_device_primary_table = am62a_boot_device_primary_table,
+	.boot_device_primary_count = ARRAY_SIZE(am62a_boot_device_primary_table),
+	.boot_device_backup_table = am62a_boot_device_backup_table,
+	.boot_device_backup_count = ARRAY_SIZE(am62a_boot_device_backup_table),
+	.main_devstat_reg = (void __iomem *)CTRLMMR_MAIN_DEVSTAT,
+	.wkup_devstat_reg = NULL,
+	.bootmode_addr = (void __iomem *)K3_BOOT_PARAM_TABLE_INDEX_OCRAM,
+};
+
+const struct k3_boot_device_info *k3_get_boot_device_info(void)
 {
-	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;
-
-	switch (bkup_bootmode) {
-	case BACKUP_BOOT_DEVICE_UART:
-		return BOOT_DEVICE_UART;
-
-	case BACKUP_BOOT_DEVICE_USB:
-		return BOOT_DEVICE_USB;
-
-	case BACKUP_BOOT_DEVICE_ETHERNET:
-		return BOOT_DEVICE_ETHERNET;
-
-	case BACKUP_BOOT_DEVICE_MMC:
-		if (bkup_bootmode_cfg)
-			return BOOT_DEVICE_MMC2;
-		return BOOT_DEVICE_MMC1;
-
-	case BACKUP_BOOT_DEVICE_SPI:
-		return BOOT_DEVICE_SPI;
-
-	case BACKUP_BOOT_DEVICE_I2C:
-		return BOOT_DEVICE_I2C;
-
-	case BACKUP_BOOT_DEVICE_DFU:
-		if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
-			return BOOT_DEVICE_USB;
-		return BOOT_DEVICE_DFU;
-	};
-
-	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;
-
-	switch (bootmode) {
-	case BOOT_DEVICE_OSPI:
-		fallthrough;
-	case BOOT_DEVICE_QSPI:
-		fallthrough;
-	case BOOT_DEVICE_XSPI:
-		fallthrough;
-	case BOOT_DEVICE_SPI:
-		return BOOT_DEVICE_SPI;
-
-	case BOOT_DEVICE_ETHERNET_RGMII:
-		fallthrough;
-	case BOOT_DEVICE_ETHERNET_RMII:
-		return BOOT_DEVICE_ETHERNET;
-
-	case BOOT_DEVICE_EMMC:
-		return BOOT_DEVICE_MMC1;
-
-	case BOOT_DEVICE_SPI_NAND:
-		return BOOT_DEVICE_SPINAND;
-
-	case BOOT_DEVICE_MMC:
-		if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
-				MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
-			return BOOT_DEVICE_MMC2;
-		return BOOT_DEVICE_MMC1;
-
-	case BOOT_DEVICE_DFU:
-		if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
-		    MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
-			return BOOT_DEVICE_USB;
-		return BOOT_DEVICE_DFU;
-
-	case BOOT_DEVICE_NOBOOT:
-		return BOOT_DEVICE_RAM;
-	}
-
-	return bootmode;
-}
-
-u32 get_boot_device(void)
-{
-	u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
-	u32 bootmode = *(u32 *)(K3_BOOT_PARAM_TABLE_INDEX_OCRAM);
-	u32 bootmedia;
-
-	if (bootmode == K3_PRIMARY_BOOTMODE)
-		bootmedia = __get_primary_bootmedia(devstat);
-	else
-		bootmedia = __get_backup_bootmedia(devstat);
-
-	debug("%s: devstat = 0x%x bootmedia = 0x%x bootmode = %d\n",
-	      __func__, devstat, bootmedia, bootmode);
-
-	return bootmedia;
+	return &am62a_boot_device_info;
 }
Signature · 1 line
2.48.1
#5 lore raw

PATCH 3/3 arm: mach-k3: am64x: Switch to boot-device helper

In reply to Wadim Egorov

Update am64x to use the new shared boot-device helper instead of its own get_boot_device() implementation.

---
 arch/arm/mach-k3/am64x/boot.c | 139 ++++++++++------------------------
 1 file changed, 41 insertions(+), 98 deletions(-)
arch/arm/mach-k3/am64x/boot.c +41 -98
index ce8ae941be6..3ef6e60e989 100644
@@ -3,103 +3,46 @@
 #include <asm/arch/hardware.h>
 #include <asm/arch/spl.h>
 
-static u32 __get_backup_bootmedia(u32 main_devstat)
+#include "../boot-device.h"
+
+static const struct k3_boot_map am64_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_NAND, 0, 0, 0, BOOT_DEVICE_NAND },
+	{ 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 },
+};
+
+static const struct k3_boot_map am64_boot_device_backup_table[] = {
+	{ BACKUP_BOOT_DEVICE_UART, 0, 0, 0, BOOT_DEVICE_UART },
+	{ BACKUP_BOOT_DEVICE_DFU, MAIN_DEVSTAT_BACKUP_USB_MODE_MASK, 0, 0, BOOT_DEVICE_DFU },
+	{ BACKUP_BOOT_DEVICE_DFU, MAIN_DEVSTAT_BACKUP_USB_MODE_MASK, 0, 1, BOOT_DEVICE_USB },
+	{ BACKUP_BOOT_DEVICE_ETHERNET, 0, 0, 0, BOOT_DEVICE_ETHERNET },
+	{ BACKUP_BOOT_DEVICE_MMC, 1, 0, 1, BOOT_DEVICE_MMC2 },
+	{ BACKUP_BOOT_DEVICE_MMC, 1, 0, 0, BOOT_DEVICE_MMC1 },
+	{ BACKUP_BOOT_DEVICE_SPI, 0, 0, 0, BOOT_DEVICE_SPI },
+	{ BACKUP_BOOT_DEVICE_I2C, 0, 0, 0, BOOT_DEVICE_I2C },
+};
+
+static const struct k3_boot_device_info am64_boot_device_info = {
+	.boot_device_primary_table = am64_boot_device_primary_table,
+	.boot_device_primary_count = ARRAY_SIZE(am64_boot_device_primary_table),
+	.boot_device_backup_table = am64_boot_device_backup_table,
+	.boot_device_backup_count = ARRAY_SIZE(am64_boot_device_backup_table),
+	.main_devstat_reg = (void __iomem *)CTRLMMR_MAIN_DEVSTAT,
+	.wkup_devstat_reg = NULL,
+	.bootmode_addr = (void __iomem *)CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX,
+};
+
+const struct k3_boot_device_info *k3_get_boot_device_info(void)
 {
-	u32 bkup_bootmode =
-	    (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
-	    MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
-	u32 bkup_bootmode_cfg =
-	    (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
-	    MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
-
-	switch (bkup_bootmode) {
-	case BACKUP_BOOT_DEVICE_UART:
-		return BOOT_DEVICE_UART;
-
-	case BACKUP_BOOT_DEVICE_DFU:
-		if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
-			return BOOT_DEVICE_USB;
-		return BOOT_DEVICE_DFU;
-
-	case BACKUP_BOOT_DEVICE_ETHERNET:
-		return BOOT_DEVICE_ETHERNET;
-
-	case BACKUP_BOOT_DEVICE_MMC:
-		if (bkup_bootmode_cfg)
-			return BOOT_DEVICE_MMC2;
-		return BOOT_DEVICE_MMC1;
-
-	case BACKUP_BOOT_DEVICE_SPI:
-		return BOOT_DEVICE_SPI;
-
-	case BACKUP_BOOT_DEVICE_I2C:
-		return BOOT_DEVICE_I2C;
-	};
-
-	return BOOT_DEVICE_RAM;
-}
-
-static u32 __get_primary_bootmedia(u32 main_devstat)
-{
-	u32 bootmode = (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
-	    MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
-	u32 bootmode_cfg =
-	    (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
-	    MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
-
-	switch (bootmode) {
-	case BOOT_DEVICE_OSPI:
-		fallthrough;
-	case BOOT_DEVICE_QSPI:
-		fallthrough;
-	case BOOT_DEVICE_XSPI:
-		fallthrough;
-	case BOOT_DEVICE_SPI:
-		return BOOT_DEVICE_SPI;
-
-	case BOOT_DEVICE_ETHERNET_RGMII:
-		fallthrough;
-	case BOOT_DEVICE_ETHERNET_RMII:
-		return BOOT_DEVICE_ETHERNET;
-
-	case BOOT_DEVICE_EMMC:
-		return BOOT_DEVICE_MMC1;
-
-	case BOOT_DEVICE_NAND:
-		return BOOT_DEVICE_NAND;
-
-	case BOOT_DEVICE_MMC:
-		if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
-		     MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
-			return BOOT_DEVICE_MMC2;
-		return BOOT_DEVICE_MMC1;
-
-	case BOOT_DEVICE_DFU:
-		if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
-		    MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
-			return BOOT_DEVICE_USB;
-		return BOOT_DEVICE_DFU;
-
-	case BOOT_DEVICE_NOBOOT:
-		return BOOT_DEVICE_RAM;
-	}
-
-	return bootmode;
-}
-
-u32 get_boot_device(void)
-{
-	u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
-	u32 bootmode = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX);
-	u32 bootmedia;
-
-	if (bootmode == K3_PRIMARY_BOOTMODE)
-		bootmedia = __get_primary_bootmedia(devstat);
-	else
-		bootmedia = __get_backup_bootmedia(devstat);
-
-	debug("%s: devstat = 0x%x bootmedia = 0x%x bootmode = %d\n",
-	      __func__, devstat, bootmedia, bootmode);
-
-	return bootmedia;
+	return &am64_boot_device_info;
 }
Signature · 1 line
2.48.1