mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ivaylo Ivanov <ivo.ivanov.ivanov1@gmail.com>
To: Sascha Hauer <s.hauer@pengutronix.de>,
	Ahmad Fatoum <a.fatoum@pengutronix.de>
Cc: barebox@lists.infradead.org
Subject: [PATCH v2 2/3] ARM: boards: add support for Samsung Galaxy S8 (dreamlte)
Date: Tue,  2 Sep 2025 17:00:05 +0300	[thread overview]
Message-ID: <20250902140006.1410408-3-ivo.ivanov.ivanov1@gmail.com> (raw)
In-Reply-To: <20250902140006.1410408-1-ivo.ivanov.ivanov1@gmail.com>

Phones utilizing an exynos SoC boot android with samsung's proprietary
bootloader, called s-boot (s-lk on newer devices). However, not only is
it closed source, it also enforces some limitations that prevent us from
booting mainline linux cleanly on them, such as an applied overlay device
tree, disabled framebuffer refreshing, misaligned kernel image at boot.
Therefore, having another stage bootloader, loaded as a linux kernel
image by s-boot, is best.

Add support for Samsung Galaxy S8, utilizing the exynos 8895 SoC. Support
is modelled to be as reusable on other devices as possible, requiring
only a minimal set of changes to boot - a barebox device tree, which in
this case is basically imported torvalds tree for dreamlte, that is then
matched from the downstream device tree, provided by s-boot at x0.

For certain devices the stack set up by the previous bootloader is not
enough. Since the idea of this board support is to be as generic as
possible, setting a fixed stack top via ENTRY_FUNCTION_WITHSTACK does
not make sense, due to different exynos devices having different memory
layouts - exynos8895's dram starts at 0x80000000, whereas exynos7870's
starts at 0x40000000. Instead, set the SP as early as possible in
assembly by offsetting the image by 0x80000 and letting it grow from
there.

Barebox has to be packaged as an android boot image:

mkbootimg --kernel images/barebox-exynos.img \
--ramdisk ramdisk.bin \
--dt stock.dtb
--cmdline "buildvariant=eng" \
--base 0x10000000 \
--kernel_offset 0x00008000 \
--ramdisk_offset 0x01000000 \
--second_offset 0x00f00000 \
--tags_offset 0x00000100 \
--os_version 9.0.0 \
--os_patch_level 2019-10 \
--pagesize 2048 \
--hash sha1 \
--output boot.img

And then flashed to the boot partition:

heimdall flash --BOOT boot.img

Currently, only a minimal set of features work. It's possible to
boot a FIT image packaged as a ramdisk with mkbootimg.

Signed-off-by: Ivaylo Ivanov <ivo.ivanov.ivanov1@gmail.com>
---
 arch/arm/Kconfig                              |  5 ++
 arch/arm/boards/Makefile                      |  1 +
 arch/arm/boards/samsung-exynos/Makefile       |  5 ++
 arch/arm/boards/samsung-exynos/board.c        | 65 +++++++++++++++
 .../defaultenv-exynos/boot/ramfit             | 12 +++
 .../defaultenv-exynos/init/bootsource         |  3 +
 arch/arm/boards/samsung-exynos/entry.S        | 31 +++++++
 arch/arm/boards/samsung-exynos/lowlevel.c     | 80 +++++++++++++++++++
 arch/arm/dts/Makefile                         |  1 +
 arch/arm/dts/exynos8895-dreamlte.dts          | 13 +++
 arch/arm/mach-samsung/Kconfig                 | 13 +++
 images/Makefile                               |  1 +
 images/Makefile.exynos                        |  8 ++
 13 files changed, 238 insertions(+)
 create mode 100644 arch/arm/boards/samsung-exynos/Makefile
 create mode 100644 arch/arm/boards/samsung-exynos/board.c
 create mode 100644 arch/arm/boards/samsung-exynos/defaultenv-exynos/boot/ramfit
 create mode 100644 arch/arm/boards/samsung-exynos/defaultenv-exynos/init/bootsource
 create mode 100644 arch/arm/boards/samsung-exynos/entry.S
 create mode 100644 arch/arm/boards/samsung-exynos/lowlevel.c
 create mode 100644 arch/arm/dts/exynos8895-dreamlte.dts
 create mode 100644 arch/arm/mach-samsung/Kconfig
 create mode 100644 images/Makefile.exynos

diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index 7a395270..095f189f 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -212,6 +212,10 @@ config ARCH_ROCKCHIP
 	select HAS_DEBUG_LL
 	imply GPIO_ROCKCHIP
 
+config ARCH_SAMSUNG
+	bool "ARM Exynos boards"
+	depends on ARCH_MULTIARCH
+
 config ARCH_STM32MP
 	bool "STMicroelectronics STM32MP"
 	depends on 32BIT
@@ -268,6 +272,7 @@ source "arch/arm/mach-k3/Kconfig"
 source "arch/arm/mach-omap/Kconfig"
 source "arch/arm/mach-pxa/Kconfig"
 source "arch/arm/mach-rockchip/Kconfig"
+source "arch/arm/mach-samsung/Kconfig"
 source "arch/arm/mach-socfpga/Kconfig"
 source "arch/arm/mach-sunxi/Kconfig"
 source "arch/arm/mach-stm32mp/Kconfig"
diff --git a/arch/arm/boards/Makefile b/arch/arm/boards/Makefile
index ac1fa74d..ff2efe04 100644
--- a/arch/arm/boards/Makefile
+++ b/arch/arm/boards/Makefile
@@ -30,6 +30,7 @@ obj-$(CONFIG_MACH_CHUMBY)			+= chumby_falconwing/
 obj-$(CONFIG_MACH_CLEP7212)			+= clep7212/
 obj-$(CONFIG_MACH_DFI_FS700_M60)		+= dfi-fs700-m60/
 obj-$(CONFIG_MACH_DIGI_CCIMX6ULSBCPRO)		+= digi-ccimx6ulsom/
+obj-$(CONFIG_MACH_EXYNOS)			+= samsung-exynos/
 obj-$(CONFIG_MACH_DUCKBILL)			+= duckbill/
 obj-$(CONFIG_MACH_DSS11)			+= dss11/
 obj-$(CONFIG_MACH_EFIKA_MX_SMARTBOOK)		+= efika-mx-smartbook/
diff --git a/arch/arm/boards/samsung-exynos/Makefile b/arch/arm/boards/samsung-exynos/Makefile
new file mode 100644
index 00000000..6694e90d
--- /dev/null
+++ b/arch/arm/boards/samsung-exynos/Makefile
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+obj-y += board.o
+lwl-y += entry.o lowlevel.o
+bbenv-y += defaultenv-exynos
diff --git a/arch/arm/boards/samsung-exynos/board.c b/arch/arm/boards/samsung-exynos/board.c
new file mode 100644
index 00000000..2d0c2248
--- /dev/null
+++ b/arch/arm/boards/samsung-exynos/board.c
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2025 Ivaylo Ivanov <ivo.ivanov.ivanov1@gmail.com>
+ */
+#include <common.h>
+#include <envfs.h>
+#include <init.h>
+#include <of.h>
+#include <deep-probe.h>
+#include <asm/memory.h>
+#include <linux/sizes.h>
+#include <asm/system.h>
+#include <of_address.h>
+
+#define EXYNOS8895_DECON0	0x12860000
+#define HW_SW_TRIG_CONTROL	0x70
+#define TRIG_AUTO_MASK_EN	BIT(12)
+#define SW_TRIG_EN		BIT(8)
+#define HW_TRIG_EN		BIT(0)
+
+static int exynos_postcore_init(void)
+{
+	void __iomem *trig_ctrl;
+
+	/*
+	 * ARM64 s-boot usually keeps framebuffer refreshing disabled after
+	 * jumping to kernel. Set the required bit so we can have output. This
+	 * should ideally be dropped from board files once we have a decon
+	 * driver.
+	 */
+	if (of_machine_is_compatible("samsung,exynos8895"))
+		trig_ctrl = IOMEM(EXYNOS8895_DECON0 + HW_SW_TRIG_CONTROL);
+	else
+		return 0;
+
+	writel(TRIG_AUTO_MASK_EN | SW_TRIG_EN | HW_TRIG_EN,
+	       trig_ctrl);
+
+	return 0;
+}
+coredevice_initcall(exynos_postcore_init);
+
+static inline int exynos_init(struct device *dev)
+{
+	barebox_set_model(of_get_model());
+	barebox_set_hostname("samsung-exynos");
+
+	defaultenv_append_directory(defaultenv_exynos);
+
+	return 0;
+}
+
+static const struct of_device_id exynos_of_match[] = {
+	{ .compatible = "samsung,dreamlte" },
+	{ /* Sentinel */},
+};
+
+MODULE_DEVICE_TABLE(of, exynos_of_match);
+
+static struct driver exynos_board_driver = {
+	.name = "board-exynos",
+	.probe = exynos_init,
+	.of_compatible = exynos_of_match,
+};
+postcore_platform_driver(exynos_board_driver);
diff --git a/arch/arm/boards/samsung-exynos/defaultenv-exynos/boot/ramfit b/arch/arm/boards/samsung-exynos/defaultenv-exynos/boot/ramfit
new file mode 100644
index 00000000..0912c9fc
--- /dev/null
+++ b/arch/arm/boards/samsung-exynos/defaultenv-exynos/boot/ramfit
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+#
+# Boot a FIT image, packaged as ramdisk in the boot.img and placed
+# in memory by s-boot.
+#
+if test "$global.model" = "Samsung Galaxy S8 (SM-G950F)"; then
+	global fit_offset=0x9000000
+fi
+
+addpart /dev/ram0 0x2000000@$global.fit_offset(fit)
+bootm -f /dev/ram0.fit
diff --git a/arch/arm/boards/samsung-exynos/defaultenv-exynos/init/bootsource b/arch/arm/boards/samsung-exynos/defaultenv-exynos/init/bootsource
new file mode 100644
index 00000000..99016c98
--- /dev/null
+++ b/arch/arm/boards/samsung-exynos/defaultenv-exynos/init/bootsource
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+global.boot.default=ramfit
diff --git a/arch/arm/boards/samsung-exynos/entry.S b/arch/arm/boards/samsung-exynos/entry.S
new file mode 100644
index 00000000..e9607f9f
--- /dev/null
+++ b/arch/arm/boards/samsung-exynos/entry.S
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#include <linux/linkage.h>
+#include <asm/barebox-arm64.h>
+#include <asm/image.h>
+
+#define IMAGE_FLAGS \
+	(ARM64_IMAGE_FLAG_PAGE_SIZE_4K << ARM64_IMAGE_FLAG_PAGE_SIZE_SHIFT) | \
+	(ARM64_IMAGE_FLAG_PHYS_BASE << ARM64_IMAGE_FLAG_PHYS_BASE_SHIFT)
+
+.section .text_head_entry_exynos
+ENTRY("start_exynos")
+	nop                        /* code0 */
+	b 2f                       /* code1 */
+	.xword 0x80000             /* Image load offset */
+	.xword _barebox_image_size /* Effective Image size */
+	.xword IMAGE_FLAGS	   /* Kernel flags */
+	.xword 0                   /* reserved */
+	.xword 0                   /* reserved */
+	.xword 0                   /* reserved */
+	.ascii ARM64_IMAGE_MAGIC   /* magic number */
+	.int   0                   /* reserved (PE-COFF offset) */
+	.asciz "barebox"	   /* unused for now */
+2:
+	adr x1, _text - .
+	mov sp, x1
+	/* Stack now grows into the 0x80000 image load offset specified
+	 * above. This is more than enough until FDT /memory is decoded.
+	 */
+	b lowlevel_exynos
+
+ENTRY_PROC_END(start_exynos)
diff --git a/arch/arm/boards/samsung-exynos/lowlevel.c b/arch/arm/boards/samsung-exynos/lowlevel.c
new file mode 100644
index 00000000..bb040611
--- /dev/null
+++ b/arch/arm/boards/samsung-exynos/lowlevel.c
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2025 Ivaylo Ivanov <ivo.ivanov.ivanov1@gmail.com>
+ */
+#include <common.h>
+#include <pbl.h>
+#include <linux/sizes.h>
+#include <asm/barebox-arm-head.h>
+#include <asm/barebox-arm.h>
+#include <asm/sections.h>
+#include <asm/cache.h>
+#include <asm/mmu.h>
+
+extern char __dtb_exynos8895_dreamlte_start[];
+
+/* called from assembly */
+void lowlevel_exynos(void *downstream_fdt);
+
+static bool is_model(const void *fdt, const char *prefix)
+{
+	int node, len;
+	const char *compat;
+
+	node = fdt_path_offset(fdt, "/");
+	if (node < 0)
+		return false;
+
+	/*
+	 * Samsung doesn't keep compatibles as consistent as model
+	 * strings. For example:
+	 *
+	 * compatible = "samsung,X1S EUR OPEN 05\0samsung,EXYNOS990";
+	 * compatible = "samsung, DREAMLTE KOR rev01", "samsung,EXYNOS8895";
+	 * compatible = "samsung, J7VE LTE LTN OPEN 00", "samsung,exynos7870";
+	 *
+	 * Use models for matching instead to avoid confusion.
+	 */
+	compat = fdt_getprop(fdt, node, "model", &len);
+	if (!compat)
+		return false;
+
+	while (*prefix) {
+		if (*compat++ != *prefix++)
+			return false;
+	}
+	return true;
+}
+
+static noinline void exynos_continue(void *downstream_fdt)
+{
+	void *fdt;
+	unsigned long membase, memsize;
+	char *__dtb_start;
+
+	/* select device tree dynamically */
+	if (is_model(downstream_fdt, "Samsung DREAMLTE")) {
+		__dtb_start = __dtb_exynos8895_dreamlte_start;
+	} else {
+		/* we didn't match any device */
+		return;
+	}
+	fdt = __dtb_start + get_runtime_offset();
+	fdt_find_mem(fdt, &membase, &memsize);
+
+	barebox_arm_entry(membase, memsize, fdt);
+}
+
+void lowlevel_exynos(void *downstream_fdt)
+{
+	arm_cpu_lowlevel_init();
+
+	relocate_to_current_adr();
+
+	setup_c();
+
+	if (!downstream_fdt || fdt_check_header(downstream_fdt))
+		return;
+
+	exynos_continue(downstream_fdt);
+}
diff --git a/arch/arm/dts/Makefile b/arch/arm/dts/Makefile
index 6612a514..a53834f7 100644
--- a/arch/arm/dts/Makefile
+++ b/arch/arm/dts/Makefile
@@ -13,6 +13,7 @@ lwl-$(CONFIG_MACH_BEAGLEPLAY) += k3-am625-beagleplay.dtb.o k3-am625-r5-beaglepla
 lwl-$(CONFIG_MACH_CLEP7212) += ep7212-clep7212.dtb.o
 lwl-$(CONFIG_MACH_CM_FX6) += imx6dl-cm-fx6.dtb.o imx6q-cm-fx6.dtb.o imx6q-utilite.dtb.o
 lwl-$(CONFIG_MACH_DFI_FS700_M60) += imx6q-dfi-fs700-m60-6q.dtb.o imx6dl-dfi-fs700-m60-6s.dtb.o
+lwl-$(CONFIG_MACH_EXYNOS) += exynos8895-dreamlte.dtb.o
 lwl-$(CONFIG_MACH_DUCKBILL) += imx28-duckbill.dtb.o
 lwl-$(CONFIG_MACH_KINDLE_MX50) += imx50-kindle-d01100.dtb.o imx50-kindle-d01200.dtb.o imx50-kindle-ey21.dtb.o
 lwl-$(CONFIG_MACH_EFIKA_MX_SMARTBOOK) += imx51-genesi-efika-sb.dtb.o
diff --git a/arch/arm/dts/exynos8895-dreamlte.dts b/arch/arm/dts/exynos8895-dreamlte.dts
new file mode 100644
index 00000000..d28b7ec2
--- /dev/null
+++ b/arch/arm/dts/exynos8895-dreamlte.dts
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
+/*
+ * Samsung Galaxy S8 (dreamlte/SM-G950F) barebox device tree source
+ *
+ * Copyright (c) 2025, Ivaylo Ivanov <ivo.ivanov.ivanov1@gmail.com>
+ */
+
+/dts-v1/;
+#include <arm64/exynos/exynos8895-dreamlte.dts>
+
+/ {
+	barebox,deep-probe;
+};
diff --git a/arch/arm/mach-samsung/Kconfig b/arch/arm/mach-samsung/Kconfig
new file mode 100644
index 00000000..106a48a6
--- /dev/null
+++ b/arch/arm/mach-samsung/Kconfig
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+if ARCH_SAMSUNG
+
+config MACH_EXYNOS
+	bool "Samsung Exynos boards support"
+	depends on 64BIT
+	select CPU_V8
+	select ARM_PSCI_CLIENT
+	select HW_HAS_PCI
+	select OF_OVERLAY
+
+endif
diff --git a/images/Makefile b/images/Makefile
index e20d11e1..4be5c3cd 100644
--- a/images/Makefile
+++ b/images/Makefile
@@ -176,6 +176,7 @@ include $(srctree)/images/Makefile.mvebu
 include $(srctree)/images/Makefile.mxs
 include $(srctree)/images/Makefile.omap3
 include $(srctree)/images/Makefile.rockchip
+include $(srctree)/images/Makefile.exynos
 include $(srctree)/images/Makefile.sandbox
 include $(srctree)/images/Makefile.socfpga
 include $(srctree)/images/Makefile.stm32mp
diff --git a/images/Makefile.exynos b/images/Makefile.exynos
new file mode 100644
index 00000000..3beb9107
--- /dev/null
+++ b/images/Makefile.exynos
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# barebox image generation Makefile for exynos images
+#
+
+pblb-$(CONFIG_MACH_EXYNOS) += start_exynos
+FILE_barebox-exynos.img = start_exynos.pblb
+image-$(CONFIG_MACH_EXYNOS) += barebox-exynos.img
-- 
2.43.0




  parent reply	other threads:[~2025-09-02 19:12 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-02 14:00 [PATCH v2 0/3] ARM: boards: add support for Samsung Galaxy S8 and S20 5G Ivaylo Ivanov
2025-09-02 14:00 ` [PATCH v2 1/3] video: simplefb-client: switch to dev_get_resource Ivaylo Ivanov
2025-09-02 14:00 ` Ivaylo Ivanov [this message]
2025-09-02 14:00 ` [PATCH v2 3/3] ARM: boards: add support for Samsung Galaxy S20 5G (x1s) Ivaylo Ivanov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250902140006.1410408-3-ivo.ivanov.ivanov1@gmail.com \
    --to=ivo.ivanov.ivanov1@gmail.com \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox