Skip to contentSkip to conversation

Amlogic: ADNL and Optimus protocols support

PATCH In review Arseniy Krasnov 2 participants 13 messages Mar 19, 2025 – Mar 21, 2025

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

cover Amlogic: ADNL and Optimus protocols support

This patchset adds support of two Amlogic firmware burning protocols:
ADNL and Optimus. Each protocol is supported on the following SoC:

axg, g12a, g12b, sm1 - Optimus.
a1, s4, a5, c1, c2, c3, sc2, t3, t7, p1 - ADNL.

Both work in fastboot manner, but significant feature is that they are supported by current Amlogic BootROMs. As transport both use USB:
device is switched to gadget mode and then it can communicate with PC. On PC side protocols are supported by:

1) https://github.com/superna9999/pyamlboot (with some limitation)

   For example:
   pyamlboot/./ubt.py --img <image> --wipe all --reset

Note, image has special format, specific for Amlogic.

2) Proprietary vendor tools.

As protocols are supported by BootROM, it allows to "resurrect" devices when boot image (BL2, Uboot) missed/invalid. Typical flow for such cases is (both protocols):

DEVICE PC

*-------------------*
| |
| Runs update tool, |
| waits for device |
| on USB bus. |
| |
*-------------------*
|
*---------------------* |
| Power on |<-----<------/
*---------------------*

          |
          |
*---------------------*
|                     |
| Boot ROM switches   |
| device to USB gadget|>----->------\
| mode and waits for  |             |
| commands.           |             |
|                     |             |
*---------------------*             |
                                    |
                          *-------------------*
                          | Detects device on |
                          |     USB bus.      |
                          *-------------------*
                                    |
                                    |
                          *-------------------*
                          |  Sends BL2 image  |
                          |     over USB.     |
                          *-------------------*
                                    |
*---------------------*             |
|     Boot ROM runs   |<-----<------/
|       BL2 image.    |
*---------------------*
          |
          |
*---------------------*
|    BL2 waits for    |>----->------\
|  Uboot image (FIP). |             |
*---------------------*             |
                                    |
                          *-------------------*
                          | Sends Uboot image |
                          |     over USB.     |
                          *-------------------*
                                    |
*---------------------*             |
|  BL2 runs received  |<-----<------/
|     Uboot image.    |
*---------------------*
           |
           |
*---------------------*
|   Uboot waits for   |>----->------\
|      commands.      |             |
*---------------------*             |
                                    |
                          *-------------------*
                          | Sends commands to |
                          |    over USB.      |
                          *-------------------*
                                    |
*---------------------*             |
| Uboot processes     |             |
| commands: erases,   |             |
| writes NAND, checks |<-----<------/
| CRC of written data |
| etc.                |
*---------------------*
           |
           |

*---------------------*
| Uboot does reboot |
*---------------------*

There is also another mode to operate both protocols when we run not from BootROM, but from Uboot shell. To do that, we enter protocol specific command in shell ("adnl" or "optimus"). In this case Uboot switches device to gadget mode, write some value to special register and does reboot. Then BootROM checks above mentioned register and then switches device to gadget mode. After that flow from above is executed. This mode has same purposes as image update by 'fastboot'.

Limitations:
1) Only limited set of each protocol commands are supported - it is enough to update image. But, for example 'secureboot' feature is not implemented.
2) ADNL was tested on A1 SoC only.
3) Optimus was tested on AXG SoC only.

For more details of each protocol pls see the following:
ADNL - drivers/usb/gadget/amlogic/adnl/f_adnl.c Optimus - drivers/usb/gadget/amlogic/optimus/f_optimus.c.

Arseniy Krasnov (8):
arch: arm: meson: use 'meson_sm_call()' in 'meson_sm_pwrdm_set()'
usb: gadget: fastboot: make part of USB fastboot code shared
usb: gadget: amlogic: common code for Amlogic flashing commands
mtd: rawnand: meson: move some defines to dedicated header
arch: arm: meson: bootloader write support
arm: meson: a1: add A1_SYSCTRL_SEC_STICKY_REG2
usb: gadget: amlogic: implement ADNL protocol
arm: meson: a1: ADNL protocol support

Vladimir Mitrofanov (3):
arch: arm: meson: sm: add commands to reboot device in different modes
usb: gadget: amlogic: implement Optimus protocol
arch: arm: axg: Optimus protocol support

 arch/arm/include/asm/arch-meson/a1.h          |  19 +
 arch/arm/include/asm/arch-meson/axg.h         |  12 +
 arch/arm/include/asm/arch-meson/nand.h        |  34 +
 arch/arm/include/asm/arch-meson/rawnand.h     |  38 +
 arch/arm/include/asm/arch-meson/sm.h          |  98 +-
 arch/arm/include/asm/arch-meson/spinand.h     |  43 +
 arch/arm/mach-meson/Kconfig                   |  31 +
 arch/arm/mach-meson/Makefile                  |   4 +-
 arch/arm/mach-meson/board-a1.c                |  32 +-
 arch/arm/mach-meson/board-axg.c               |  12 +
 arch/arm/mach-meson/board-common.c            |  32 +
 arch/arm/mach-meson/rawnand.c                 | 291 ++++++
 arch/arm/mach-meson/sm.c                      |  43 +-
 arch/arm/mach-meson/spinand.c                 | 158 ++++
 cmd/Kconfig                                   |  14 +
 cmd/meson/Makefile                            |   2 +
 cmd/meson/adnl.c                              |  27 +
 cmd/meson/gadget.c                            | 183 ++++
 cmd/meson/gadget.h                            |  28 +
 cmd/meson/optimus.c                           |  21 +
 drivers/mtd/nand/raw/meson_nand.c             |  13 +-
 drivers/sm/meson-sm.c                         |   3 +
 drivers/usb/gadget/Kconfig                    |   2 +
 drivers/usb/gadget/Makefile                   |   4 +-
 drivers/usb/gadget/amlogic/Kconfig            |  12 +
 drivers/usb/gadget/amlogic/adnl/Kconfig       |  29 +
 drivers/usb/gadget/amlogic/adnl/Makefile      |   4 +
 drivers/usb/gadget/amlogic/adnl/adnl.h        | 124 +++
 .../gadget/amlogic/adnl/adnl_buff_manager.c   | 316 +++++++
 drivers/usb/gadget/amlogic/adnl/adnl_media.c  | 235 +++++
 .../usb/gadget/amlogic/adnl/adnl_storage.c    | 140 +++
 drivers/usb/gadget/amlogic/adnl/f_adnl.c      | 835 ++++++++++++++++++
 drivers/usb/gadget/amlogic/optimus/Kconfig    |  12 +
 drivers/usb/gadget/amlogic/optimus/Makefile   |   7 +
 .../usb/gadget/amlogic/optimus/f_optimus.c    | 687 ++++++++++++++
 .../gadget/amlogic/optimus/optimus_download.c | 188 ++++
 .../gadget/amlogic/optimus/optimus_download.h |  86 ++
 drivers/usb/gadget/f_fastboot.c               | 295 +------
 drivers/usb/gadget/f_fastboot_common.c        | 320 +++++++
 drivers/usb/gadget/f_fastboot_common.h        |  71 ++
 include/meson/sm.h                            |   3 +
 41 files changed, 4202 insertions(+), 306 deletions(-)
 create mode 100644 arch/arm/include/asm/arch-meson/nand.h
 create mode 100644 arch/arm/include/asm/arch-meson/rawnand.h
 create mode 100644 arch/arm/include/asm/arch-meson/spinand.h
 create mode 100644 arch/arm/mach-meson/rawnand.c
 create mode 100644 arch/arm/mach-meson/spinand.c
 create mode 100644 cmd/meson/adnl.c
 create mode 100644 cmd/meson/gadget.c
 create mode 100644 cmd/meson/gadget.h
 create mode 100644 cmd/meson/optimus.c
 create mode 100644 drivers/usb/gadget/amlogic/Kconfig
 create mode 100644 drivers/usb/gadget/amlogic/adnl/Kconfig
 create mode 100644 drivers/usb/gadget/amlogic/adnl/Makefile
 create mode 100644 drivers/usb/gadget/amlogic/adnl/adnl.h
 create mode 100644 drivers/usb/gadget/amlogic/adnl/adnl_buff_manager.c
 create mode 100644 drivers/usb/gadget/amlogic/adnl/adnl_media.c
 create mode 100644 drivers/usb/gadget/amlogic/adnl/adnl_storage.c
 create mode 100644 drivers/usb/gadget/amlogic/adnl/f_adnl.c
 create mode 100644 drivers/usb/gadget/amlogic/optimus/Kconfig
 create mode 100644 drivers/usb/gadget/amlogic/optimus/Makefile
 create mode 100644 drivers/usb/gadget/amlogic/optimus/f_optimus.c
 create mode 100644 drivers/usb/gadget/amlogic/optimus/optimus_download.c
 create mode 100644 drivers/usb/gadget/amlogic/optimus/optimus_download.h
 create mode 100644 drivers/usb/gadget/f_fastboot_common.c
 create mode 100644 drivers/usb/gadget/f_fastboot_common.h
Signature · 1 line
2.30.1
#1 lore raw

PATCH 1/11 arch: arm: meson: sm: add commands to reboot device in different modes

From: Vladimir Mitrofanov <[email protected]>

There are several commands to reboot device in specific mode (cold, normal, etc.), these commands helps to enter flashing mode and set boot source device:

1) MESON_SMC_CMD_BL1_FIRST_BOOT_SRC_SET

This command tells BL1 that it must load BL2 from USB data path. It is used in ADNL protocol logic when burning process is started from the U-Boot command line: when 'adnl' is entered, this command tells BL1 to load BL2 from USB, then board is rebooted. After reboot BL2 and next stage loaders will be loaded from USB, sticky register has value that we are in ADNL mode, thus booted U-Boot enters ADNL mode to start data burning.

Used in adnl-based SoC families: a1, s4, c1, с2, sc2, t7, etc.

2) MESON_SMC_CMD_REBOOT_MODE_SET
Used in optimus-based SoC families: axg, g12a, g12b, sm1, etc.

3) MESON_SMC_CMD_USB_BOOT_MODE_SET
Used in optimus-based SoC families: axg, g12a, g12b, sm1, etc.

---
 arch/arm/include/asm/arch-meson/sm.h | 98 +++++++++++++++++++++++++++-
 arch/arm/mach-meson/sm.c             | 39 +++++++++++
 drivers/sm/meson-sm.c                |  3 +
 include/meson/sm.h                   |  3 +
 4 files changed, 142 insertions(+), 1 deletion(-)
arch/arm/include/asm/arch-meson/sm.h +97 -1
index 4d614955fc2..087f8b4d5fa 100644
@@ -83,7 +83,7 @@ int meson_sm_get_serial(void *buffer, size_t size);
  */
 int meson_sm_get_chip_id(struct meson_sm_chip_id *chip_id);
 
-enum {
+enum meson_reboot_mode {
 	REBOOT_REASON_COLD = 0,
 	REBOOT_REASON_NORMAL = 1,
 	REBOOT_REASON_RECOVERY = 2,
@@ -134,4 +134,100 @@ int meson_sm_pwrdm_set(size_t index, int cmd);
 #define meson_sm_pwrdm_on(index) \
 	meson_sm_pwrdm_set(index, PWRDM_ON)
 
+/*
+ * Below is the list of SCPI_CMD commands, issued by U-Boot and passed to SCP
+ * co-processor via SMC to BL31 (TF-A). BL31 routes these commands to SCP
+ * via mailbox.
+ *
+ * Commands instruct the SCP how to set/control POC pins in order to choose
+ * first boot source, specified by the particular command below. These POC pins
+ * are readable by BootROM and used to determine the boot sequence. Usually,
+ * the normal default boot sequence, seen on production device is
+ *     NAND/eMMC -> SD -> USB
+ *
+ * Commands below change the first boot source.
+ *
+ * NOTE:
+ * these commands are used along with meson_sm_set_bl1_first_boot_source() on
+ * Amlogic SoCs where ADNL burning protocol is utilized.
+ * Refer to meson_sm_set_usb_boot_mode() for the same functionality but on SoCs
+ * with Optimus burning protocol.
+ */
+
+/**
+ * define SCPI_CMD_USB_BOOT - sets the first boot source into USB with timeout
+ *
+ * Additionally, force the BootROM (BL1) to wait for the Host's enumeration
+ * with timeout
+ */
+#define SCPI_CMD_USB_BOOT 0xB0
+
+/**
+ * define SCPI_CMD_USB_UNBOOT - sets the first boot source into USB w/o timeout
+ *
+ * Additionally, force the BootROM to wait for the Host's enumeration with
+ * uninterruptible wait, i.e. forever
+ */
+#define SCPI_CMD_USB_UNBOOT 0xB1
+
+/**
+ * define SCPI_CMD_SDCARD_BOOT - sets the first boot source into SD card
+ */
+#define SCPI_CMD_SDCARD_BOOT 0xB2
+
+/**
+ * define SCPI_CMD_CLEAR_BOOT - sets the first boot source into default one
+ *
+ * BootROM follows a pre-defined boot sequence stored in SoC ROM code.
+ */
+#define SCPI_CMD_CLEAR_BOOT 0xB3
+
+/**
+ * meson_sm_set_bl1_first_boot_source - sets the first boot source for BootROM.
+ * @cmd: command to set first boot source visible to BootROM during boot.
+ *       Use SCPI_CMD_* as commands.
+ *
+ * BootROM checks POC-pins (POC0, POC1, POC2) to determine in which order it
+ * must probe the various methods of booting the system. The order is usually
+ * pre-defined. But function changes the first boot source of that pre-defined
+ * order.
+ *
+ * @return: zero on success or error code on failure.
+ */
+int meson_sm_set_bl1_first_boot_source(u64 cmd);
+
+/**
+ * meson_sm_reboot - reboots device in a appropriate mode
+ * All possible modes described in &enum meson_reboot_mode
+ *
+ * @mode: reboot mode.
+ * @return: zero on success or error code on failure.
+ */
+int meson_sm_reboot(u64 mode);
+
+/**
+ * enum meson_usb_boot_mode - modes for meson_sm_set_usb_boot_mode()
+ * @FORCE_USB_BOOT: Forces BootROM to boot with firmware transferred by PC
+ * 		    burning tool via USB. This mode has influence despite of the
+ * 		    previously selected boot source. This mode persists between
+ * 		    reboots and must be cleared.
+ * @CLEAR_USB_BOOT: Clears previous mode.
+ *
+ * These modes are used in conjunction with meson_sm_set_usb_boot_mode() on
+ * Amlogic SoCs where Optimus burning protocol is utilized.
+ */
+enum meson_usb_boot_mode {
+	CLEAR_USB_BOOT = 1,
+	FORCE_USB_BOOT = 2,
+};
+
+/**
+ * meson_sm_set_usb_boot_mode - sets the mode of usb interface that will be used during boot
+ * All possible modes described in &enum meson_usb_boot_mode
+ *
+ * @mode: reboot mode.
+ * @return: zero on success or error code on failure.
+ */
+int meson_sm_set_usb_boot_mode(u64 mode);
+
 #endif /* __MESON_SM_H__ */
arch/arm/mach-meson/sm.c +39 -0
index b1f91ca29ce..53b07e1dfe9 100644
@@ -236,6 +236,17 @@ int meson_sm_get_reboot_reason(void)
 	return FIELD_GET(REBOOT_REASON_MASK, reason);
 }
 
+static int meson_sm_call(u32 smc, struct pt_regs *regs, s32 *retval)
+{
+	struct udevice *dev;
+
+	dev = meson_get_sm_device();
+	if (IS_ERR(dev))
+		return PTR_ERR(dev);
+
+	return sm_call(dev, smc, retval, regs);
+}
+
 int meson_sm_pwrdm_set(size_t index, int cmd)
 {
 	struct udevice *dev;
@@ -256,3 +267,31 @@ int meson_sm_pwrdm_set(size_t index, int cmd)
 
 	return err;
 }
+
+int meson_sm_set_bl1_first_boot_source(u64 cmd)
+{
+	struct pt_regs regs = { 0 };
+
+	regs.regs[1] = cmd;
+
+	return meson_sm_call(MESON_SMC_CMD_BL1_FIRST_BOOT_SRC_SET,
+			     &regs, NULL);
+}
+
+int meson_sm_reboot(u64 mode)
+{
+	struct pt_regs regs = { 0 };
+
+	regs.regs[1] = mode;
+
+	return meson_sm_call(MESON_SMC_CMD_REBOOT_MODE_SET, &regs, NULL);
+}
+
+int meson_sm_set_usb_boot_mode(u64 mode)
+{
+	struct pt_regs regs = { 0 };
+
+	regs.regs[1] = mode;
+
+	return meson_sm_call(MESON_SMC_CMD_USB_BOOT_MODE_SET, &regs, NULL);
+}
drivers/sm/meson-sm.c +3 -0
index 87eba1486db..f413d8335b3 100644
@@ -168,8 +168,11 @@ static const struct meson_sm_data meson_sm_gxbb_data = {
 	.cmd = {
 		SET_CMD(MESON_SMC_CMD_EFUSE_READ,  0x82000030),
 		SET_CMD(MESON_SMC_CMD_EFUSE_WRITE, 0x82000031),
+		SET_CMD(MESON_SMC_CMD_USB_BOOT_MODE_SET, 0x82000043),
 		SET_CMD(MESON_SMC_CMD_CHIP_ID_GET, 0x82000044),
+		SET_CMD(MESON_SMC_CMD_BL1_FIRST_BOOT_SRC_SET, 0x82000087),
 		SET_CMD(MESON_SMC_CMD_PWRDM_SET,   0x82000093),
+		SET_CMD(MESON_SMC_CMD_REBOOT_MODE_SET, 0x84000009),
 	},
 };
 
include/meson/sm.h +3 -0
index fbaab1f1ee0..b9621f95cbe 100644
@@ -13,6 +13,9 @@ enum meson_smc_cmd {
 	MESON_SMC_CMD_EFUSE_WRITE, /* write efuse memory */
 	MESON_SMC_CMD_CHIP_ID_GET, /* readh chip unique id */
 	MESON_SMC_CMD_PWRDM_SET,   /* do command at specified power domain */
+	MESON_SMC_CMD_BL1_FIRST_BOOT_SRC_SET, /* setup first boot source for BootROM */
+	MESON_SMC_CMD_REBOOT_MODE_SET, /* Reboot device in desired mode */
+	MESON_SMC_CMD_USB_BOOT_MODE_SET, /* Run boot from USB device */
 	MESON_SMC_CMD_COUNT,
 };
 
Signature · 1 line
2.30.1
#2 lore raw

PATCH 2/11 arch: arm: meson: use 'meson_sm_call()' in 'meson_sm_pwrdm_set()'

In reply to Arseniy Krasnov

Use more generic function in 'meson_sm_pwrdm_set()'.

---
 arch/arm/mach-meson/sm.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
arch/arm/mach-meson/sm.c +1 -7
index 53b07e1dfe9..0a781e9c47b 100644
@@ -249,22 +249,16 @@ static int meson_sm_call(u32 smc, struct pt_regs *regs, s32 *retval)
 
 int meson_sm_pwrdm_set(size_t index, int cmd)
 {
-	struct udevice *dev;
 	struct pt_regs regs = { 0 };
 	int err;
 
-	dev = meson_get_sm_device();
-	if (IS_ERR(dev))
-		return PTR_ERR(dev);
-
 	regs.regs[1] = index;
 	regs.regs[2] = cmd;
 
-	err = sm_call(dev, MESON_SMC_CMD_PWRDM_SET, NULL, &regs);
+	err = meson_sm_call(MESON_SMC_CMD_PWRDM_SET, &regs, NULL);
 	if (err)
 		pr_err("Failed to %s power domain ind=%zu (%d)\n", cmd == PWRDM_ON ?
 				"enable" : "disable", index, err);
-
 	return err;
 }
 
Signature · 1 line
2.30.1
#3 lore raw

PATCH 3/11 usb: gadget: fastboot: make part of USB fastboot code shared

In reply to Arseniy Krasnov

This is preparation for fastboot-based flashing protocols (such as Amlogic ADNL). When device enters ADNL mode, it becomes "fastboot" device from USB point of view, so to avoid copy-paste of USB part of fastboot protocol, reimplement it as shared code between fastboot and ADNL implementations.

---
 drivers/usb/gadget/Makefile            |   2 +-
 drivers/usb/gadget/f_fastboot.c        | 295 +----------------------
 drivers/usb/gadget/f_fastboot_common.c | 320 +++++++++++++++++++++++++
 drivers/usb/gadget/f_fastboot_common.h |  71 ++++++
 4 files changed, 403 insertions(+), 285 deletions(-)
 create mode 100644 drivers/usb/gadget/f_fastboot_common.c
 create mode 100644 drivers/usb/gadget/f_fastboot_common.h
drivers/usb/gadget/Makefile +1 -1
index 4bda224ff1a..6c16ad83f6e 100644
@@ -27,7 +27,7 @@ obj-$(CONFIG_USB_GADGET_DOWNLOAD) += g_dnl.o
 obj-$(CONFIG_USB_FUNCTION_THOR) += f_thor.o
 obj-$(CONFIG_DFU_OVER_USB) += f_dfu.o
 obj-$(CONFIG_USB_FUNCTION_MASS_STORAGE) += f_mass_storage.o
-obj-$(CONFIG_USB_FUNCTION_FASTBOOT) += f_fastboot.o
+obj-$(CONFIG_USB_FUNCTION_FASTBOOT) += f_fastboot.o f_fastboot_common.o
 obj-$(CONFIG_USB_FUNCTION_SDP) += f_sdp.o
 obj-$(CONFIG_USB_FUNCTION_ROCKUSB) += f_rockusb.o
 obj-$(CONFIG_USB_FUNCTION_ACM)	+= f_acm.o
drivers/usb/gadget/f_fastboot.c +11 -284

339 lines not on this page · raw message

drivers/usb/gadget/f_fastboot_common.c new file +320 -0

320 lines not on this page · raw message

drivers/usb/gadget/f_fastboot_common.h new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
2.30.1
#4 lore raw

PATCH 4/11 usb: gadget: amlogic: common code for Amlogic flashing commands

In reply to Arseniy Krasnov

Amlogic has Optimus and ADNL protocols to update firmware image. Both are operatable by special commands and such commands use same approaches, so let's implement such code before implementing both protocols.

---
 cmd/meson/gadget.c | 183 +++++++++++++++++++++++++++++++++++++++++++++
 cmd/meson/gadget.h |  28 +++++++
 2 files changed, 211 insertions(+)
 create mode 100644 cmd/meson/gadget.c
 create mode 100644 cmd/meson/gadget.h
cmd/meson/gadget.c new file +183 -0
new file mode 100644
index 00000000000..6155043ae5a
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2025 SaluteDevices, Inc.
+ * Author: Arseniy Krasnov <[email protected]>
+ */
+
+#include <hexdump.h>
+#include <command.h>
+#include <g_dnl.h>
+#include <mtd.h>
+#include <time.h>
+#include <asm/arch/sm.h>
+#include <asm/types.h>
+#include <linux/delay.h>
+
+#define BURN_BOOT_UDELAY	(2 * 1000 * 1000)
+static unsigned long burn_identify_time_ms;
+static unsigned long burn_start_time_ms;
+
+void amlogic_gadget_set_times(unsigned long start_time, unsigned long identify_time)
+{
+	burn_start_time_ms = start_time;
+	burn_identify_time_ms = identify_time;
+}
+
+/*
+ * This function utilizes serial from inside Amlogic chip_id with one
+ * distinctive feature that this serial must be in big-endian.
+ *
+ * This is needed, because serial string, utilized in BootROM, must match
+ * the serial string, used inside U-Boot. But BootROM utilizes serial string
+ * in big-endian: being in BootROM try
+ * - for new SoCs (a1, s4, etc - those which utilize ADNL protocol): run
+ *   host command 'fastboot devices'
+ * - for old SoCs (axg, g12a, g12b, etc - those, utilizing optimus protocol):
+ *   send "chipid" command to bootROM
+ */
+static int amlogic_serial_hex(char serial_str[SM_SERIAL_SIZE * 2 + 1])
+{
+	u8 serial[SM_SERIAL_SIZE];
+	int ret;
+
+	/*
+	 * ADNL/OPTIMUS uses serial value in big-endian order.
+	 * meson_sm_get_serial() returns exactly such value.
+	 */
+	ret = meson_sm_get_serial(serial, sizeof(serial));
+	if (ret)
+		return ret;
+
+	bin2hex(serial_str, serial, sizeof(serial));
+	return ret;
+}
+
+int amlogic_gadget_run(const char *mode, int argc, char *const argv[])
+{
+	char serial_string[SM_SERIAL_SIZE * 2 + 1] = { 0 };
+	unsigned long identify_timeout_ms;
+	unsigned long start_timeout_ms;
+	struct udevice *udc;
+	char *saved_serial;
+	int ret;
+
+	if (mtd_probe_devices()) {
+		pr_err("Mtd devices probe failed\n");
+		return CMD_RET_FAILURE;
+	}
+
+	/*
+	 * During ADNL/OPTIMUS burning process, device enters USB gadget mode
+	 * twice: first time when it was stopped in ROM boot, second -
+	 * here, after ROM and BL2 stages are done and we are ready
+	 * to run BL33 (U-boot) stage. From the PC side, client waits
+	 * reentering to USB gadget mode (in BL33 stage). Without delay
+	 * below, PC client waits for USB device forever with the
+	 * following message.
+	 *
+	 * OPTIMUS do not use timeout described below
+	 * MSG[TLS]Waiting for DNL[<serial>] with timeout 0
+	 *
+	 * At the same time, there is no difference (from the PC point
+	 * of view) between USB device state with or w/o this delay, so
+	 * seems this delay is needed only to make PC client work
+	 * correctly due to is internal logic.
+	 */
+	udelay(BURN_BOOT_UDELAY);
+
+	ret = amlogic_serial_hex(serial_string);
+	if (ret)
+		return CMD_RET_FAILURE;
+
+	saved_serial = env_get("serial#");
+	if (saved_serial) {
+		saved_serial = strdup(saved_serial);
+		if (!saved_serial)
+			return CMD_RET_FAILURE;
+	}
+
+	/*
+	 * Use "force" flag, because 'serial#' is protected
+	 * from both delete and rewrite. This variable is used
+	 * by USB gadget code as serial number of USB device.
+	 */
+	ret = run_commandf("env set -f serial# %s", serial_string);
+	if (ret) {
+		ret = CMD_RET_FAILURE;
+		goto exit_ret;
+	}
+
+	amlogic_gadget_set_times(get_timer(0), 0);
+
+	ret = udc_device_get_by_index(0, &udc);
+	if (ret) {
+		pr_err("Failed to get UDC device: %d\n", ret);
+		ret = CMD_RET_FAILURE;
+		goto exit_ret;
+	}
+
+	g_dnl_clear_detach();
+	ret = g_dnl_register(mode);
+	if (ret) {
+		ret = CMD_RET_FAILURE;
+		goto exit_ret;
+	}
+
+	if (!g_dnl_board_usb_cable_connected()) {
+		pr_err("Usb cable isn't detected\n");
+		ret = CMD_RET_FAILURE;
+		goto exit;
+	}
+
+	identify_timeout_ms = (argc == 3) ? simple_strtoul(argv[2], NULL, 0) : 0;
+	start_timeout_ms = (argc >= 2) ? simple_strtoul(argv[1], NULL, 0) : 0;
+
+	while (1) {
+		if (g_dnl_detach())
+			break;
+
+		if (ctrlc())
+			break;
+
+		schedule();
+
+		if (start_timeout_ms && burn_start_time_ms) {
+			unsigned long start_delta = get_timer(burn_start_time_ms);
+
+			if (start_delta > start_timeout_ms) {
+				pr_err("Burn init timeout: %lu > %lu\n",
+				       start_delta, start_timeout_ms);
+				ret = CMD_RET_FAILURE;
+				goto exit;
+			}
+		}
+
+		if (identify_timeout_ms && burn_identify_time_ms) {
+			unsigned long identify_delta_ms = get_timer(burn_identify_time_ms);
+
+			if (identify_delta_ms > identify_timeout_ms) {
+				pr_err("Burn identify timeout: %lu > %lu\n",
+				       identify_delta_ms, identify_timeout_ms);
+				ret = CMD_RET_FAILURE;
+				goto exit;
+			}
+		}
+
+		dm_usb_gadget_handle_interrupts(udc);
+	}
+
+	ret = CMD_RET_SUCCESS;
+
+exit:
+	g_dnl_unregister();
+	g_dnl_clear_detach();
+	udc_device_put(udc);
+
+exit_ret:
+	if (saved_serial) {
+		env_set("serial#", saved_serial);
+		free(saved_serial);
+	}
+
+	return ret;
+}
cmd/meson/gadget.h new file +28 -0
new file mode 100644
index 00000000000..a3771fb4746
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2025 SaluteDevices, Inc.
+ * Author: Arseniy Krasnov <[email protected]>
+ */
+
+#ifndef __GADGET_H__
+#define __GADGET_H__
+
+/**
+ * amlogic_gadget_set_times() - set timeouts for ADNL/Optimus protocols
+ *
+ * @start_time: timeout in ms, until device enters ADNL/Optimus mode
+ * @identify_time: timeout in ms, until device gets first command from host
+ */
+void amlogic_gadget_set_times(unsigned long start_time, unsigned long identify_time);
+
+/**
+ * amlogic_gadget_run() - switch device to USB gadget mode
+ *
+ * @mode: USB function name.
+ * @argc: number of arguments in 'argv'
+ * @argv: arguments, passed from command
+ * Return: 0 (CMD_RET_SUCCESS) if there is no error.
+ */
+int amlogic_gadget_run(const char *mode, int argc, char *const argv[]);
+
+#endif /* __GADGET_H__ */
Signature · 1 line
2.30.1
#5 lore raw

PATCH 5/11 mtd: rawnand: meson: move some defines to dedicated header

In reply to Arseniy Krasnov

We need these values and macro to correctly write "bootloader" partition in Optimus protocol, so let's export them to arch-meson header and also provide more detailed information about each macro.

---
 arch/arm/include/asm/arch-meson/rawnand.h | 38 +++++++++++++++++++++++
 drivers/mtd/nand/raw/meson_nand.c         | 13 +-------
 2 files changed, 39 insertions(+), 12 deletions(-)
 create mode 100644 arch/arm/include/asm/arch-meson/rawnand.h
arch/arm/include/asm/arch-meson/rawnand.h new file +38 -0
new file mode 100644
index 00000000000..81bf6ef7e37
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * (C) Copyright 2025 SaluteDevices, Inc.
+ */
+
+#ifndef __MESON_RAWNAND_H__
+#define __MESON_RAWNAND_H__
+
+#include <asm/arch/nand.h>
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/rawnand.h>
+
+/* M2N means 'memory to NAND', e.g. write. */
+#define NFC_CMD_M2N	((0 << 17) | BIT(21))
+/* N2M means 'NAND to memory', e.g. read. */
+#define NFC_CMD_N2M	((1 << 17) | BIT(21))
+
+/*
+ * Macro to create value for read/write command.
+ *
+ * 'cmd_dir' - direction, e.g. read or write.
+ * 'ran' - scrambler mode (enabled or not).
+ * 'bch' - ECC mode (strength and size).
+ * 'short_mode' - short mode (enabled or not).
+ * 'page_size' - size of page.
+ * 'pages' - number of pages to access.
+ */
+#define CMDRWGEN(cmd_dir, ran, bch, short_mode, page_size, pages)	\
+	(								\
+		(cmd_dir)		    	|			\
+		((ran))	        		|			\
+		((bch) << 14)			|			\
+		((short_mode) << 13)		|			\
+		(((page_size) & 0x7F) << 6)	|			\
+		((pages) & 0x3F)					\
+	)
+
+#endif
drivers/mtd/nand/raw/meson_nand.c +1 -12
index 28c851f103b..81122315f45 100644
@@ -11,6 +11,7 @@
 
 #include <nand.h>
 #include <asm/io.h>
+#include <asm/arch/rawnand.h>
 #include <dm.h>
 #include <dm/device_compat.h>
 #include <dm/ofnode.h>
@@ -34,8 +35,6 @@
 #define NFC_CMD_AIL			((2 << 16) | (3 << 20))
 #define NFC_CMD_AIH			((3 << 16) | (3 << 20))
 #define NFC_CMD_SEED			((8 << 16) | (3 << 20))
-#define NFC_CMD_M2N			((0 << 17) | (2 << 20))
-#define NFC_CMD_N2M			((1 << 17) | (2 << 20))
 #define NFC_CMD_RB			BIT(20)
 #define NFC_CMD_SCRAMBLER_ENABLE	BIT(19)
 #define NFC_CMD_SCRAMBLER_DISABLE	0
@@ -61,16 +60,6 @@
 #define NFC_REG_PINS		0x30
 #define NFC_REG_VER		0x38
 
-#define CMDRWGEN(cmd_dir, ran, bch, short_mode, page_size, pages)	\
-	(								\
-		(cmd_dir)			|			\
-		(ran)				|			\
-		((bch) << 14)			|			\
-		((short_mode) << 13)		|			\
-		(((page_size) & 0x7f) << 6)	|			\
-		((pages) & 0x3f)					\
-	)
-
 #define GENCMDDADDRL(adl, addr)		((adl) | ((addr) & 0xffff))
 #define GENCMDDADDRH(adh, addr)		((adh) | (((addr) >> 16) & 0xffff))
 #define GENCMDIADDRL(ail, addr)		((ail) | ((addr) & 0xffff))
Signature · 1 line
2.30.1
#6 lore raw

PATCH 6/11 arch: arm: meson: bootloader write support

In reply to Arseniy Krasnov

BootROM of Amlogic SoCs that use ADNL/Optimus protocols needs special layout of "bootloader" partition. So let's implement functions that support bootloader writing on such SoCs. This is prerequisite for ADNL/Optimus implementation.

We place such functions to 'arch/arm/mach-meson', because this code is also needed by 'fastboot' protocol which can also write "bootloader" partition.

---
 arch/arm/include/asm/arch-meson/nand.h    |  34 +++
 arch/arm/include/asm/arch-meson/spinand.h |  43 ++++
 arch/arm/mach-meson/Kconfig               |  31 +++
 arch/arm/mach-meson/Makefile              |   4 +-
 arch/arm/mach-meson/rawnand.c             | 291 ++++++++++++++++++++++
 arch/arm/mach-meson/spinand.c             | 158 ++++++++++++
 6 files changed, 559 insertions(+), 2 deletions(-)
 create mode 100644 arch/arm/include/asm/arch-meson/nand.h
 create mode 100644 arch/arm/include/asm/arch-meson/spinand.h
 create mode 100644 arch/arm/mach-meson/rawnand.c
 create mode 100644 arch/arm/mach-meson/spinand.c
arch/arm/include/asm/arch-meson/nand.h new file +34 -0
new file mode 100644
index 00000000000..1f5a20d237b
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * (C) Copyright 2023-2024 SaluteDevices, Inc.
+ */
+
+#ifndef __MESON_NAND_H__
+#define __MESON_NAND_H__
+
+#define BOOT_LOADER     "bootloader"
+#define BOOT_BL2        "bl2"
+#define BOOT_SPL        "spl"
+#define BOOT_TPL        "tpl"
+#define BOOT_FIP        "fip"
+
+#define BL2_COPY_NUM            (CONFIG_MESON_BL2_COPY_NUM)
+#define BL2_IMAGE_SIZE_PER_COPY (CONFIG_MESON_BL2_IMAGE_SIZE)
+#define BL2_TOTAL_PAGES         1024
+
+#define TPL_COPY_NUM            (CONFIG_MESON_TPL_COPY_NUM)
+#define TPL_SIZE_PER_COPY       0x200000
+
+#define NAND_RSV_BLOCK_NUM      48
+
+/**
+ * meson_bootloader_write - write 'bootloader' partition to NAND
+ *                          according to the required layout. It will
+ *                          write BL2, TPL and info pages.
+ *
+ * @buf: buffer with BL2 and TPL
+ * @length: buffer length
+ * @return: 0 on success, -errno otherwise.
+ */
+int meson_bootloader_write(unsigned char *buf, size_t length);
+#endif
arch/arm/include/asm/arch-meson/spinand.h new file +43 -0

43 lines not on this page · raw message

arch/arm/mach-meson/Kconfig +31 -0
index 7570f48e25f..883ee78f49b 100644
@@ -93,4 +93,35 @@ config SYS_BOARD
 	  Based on this option board/<CONFIG_SYS_VENDOR>/<CONFIG_SYS_BOARD> will
 	  be used.
 
+config MESON_BL2_COPY_NUM
+	depends on ADNL || FASTBOOT_FLASH
+	int "Number of BL2 copies written to storage"
+	default 8
+	help
+		The ADNL / Optimus / fastboot protocol writes several copies of BL2
+		bootloader during firmware update process.
+
+config MESON_TPL_COPY_NUM
+	depends on ADNL || FASTBOOT_FLASH
+	int "Number of TPL copies written to storage"
+	default 4
+	help
+		The ADNL / Optimus / fastboot protocol writes several copies of TPL
+		bootloader during firmware update process.
+
+config MESON_BL2_IMAGE_SIZE
+	depends on ADNL || FASTBOOT_FLASH
+	hex "Size of BL2 image"
+	default 0x10000
+	help
+		Size of BL2 image, passed by ADNL / Optimus / fastboot protocols.
+
+config MESON_BL2_IMAGE_OFFSET
+	depends on ADNL || FASTBOOT_FLASH
+	hex "Offset from info page to BL2 image"
+	default 0x800
+	help
+		For each copy of BL2, this is offset from info page to BL2 image. Used
+		by Optimus and fastboot protocols.
+
 endif
arch/arm/mach-meson/Makefile +2 -2
index 535b0878b91..f63f91dde35 100644
@@ -4,6 +4,6 @@
 
 obj-y += board-common.o sm.o board-info.o
 obj-$(CONFIG_MESON_GX) += board-gx.o
-obj-$(CONFIG_MESON_AXG) += board-axg.o
+obj-$(CONFIG_MESON_AXG) += board-axg.o rawnand.o
 obj-$(CONFIG_MESON_G12A) += board-g12a.o
-obj-$(CONFIG_MESON_A1) += board-a1.o
+obj-$(CONFIG_MESON_A1) += board-a1.o spinand.o
arch/arm/mach-meson/rawnand.c new file +291 -0

291 lines not on this page · raw message

arch/arm/mach-meson/spinand.c new file +158 -0

158 lines not on this page · raw message

Signature · 1 line
2.30.1
#7 lore raw

PATCH 7/11 arm: meson: a1: add A1_SYSCTRL_SEC_STICKY_REG2

In reply to Arseniy Krasnov

This register is used by ADNL protocol, to check boot source of current U-boot instance.

---
 arch/arm/include/asm/arch-meson/a1.h | 6 ++++++
 1 file changed, 6 insertions(+)
arch/arm/include/asm/arch-meson/a1.h +6 -0
index 86d1a68de8c..16e209ed932 100644
@@ -13,6 +13,12 @@
 #define A1_SYSCTRL_ADDR(off)		(A1_SYSCTRL_BASE + ((off) << 2))
 
 #define A1_SYSCTRL_SEC_STATUS_REG4	A1_SYSCTRL_ADDR(0xc4)
+/*
+ * Checked during boot. Tells, that previous code, e.g.
+ * executed before reboot requested to continue boot
+ * from USB. Used by ADNL protocol.
+ */
+#define A1_SYSCTRL_SEC_STICKY_REG2	A1_SYSCTRL_ADDR(0xe2)
 
 #define A1_SYSCTRL_MEM_SIZE_MASK	0xFFFF0000
 #define A1_SYSCTRL_MEM_SIZE_SHIFT	16
Signature · 1 line
2.30.1
#8 lore raw

PATCH 8/11 usb: gadget: amlogic: implement ADNL protocol

In reply to Arseniy Krasnov

This adds support for ADNL (Amlogic DowNLoad) protocol. This protocol is supported by some Amlogic SoCs (a1, s4, c1, с2, sc2, t7, etc) in BootRom code (e.g. it is already supported in "out of the box" mode). It has similar goals as fastboot - upload and update firmware images on the device. Difference is that, it is specific for Amlogic SoCs, and for example it allows to upload and run image starting from ROM boot stage, it also uses SoC specific registers and SMC commands.

---
 cmd/Kconfig                                   |   7 +
 cmd/meson/Makefile                            |   1 +
 cmd/meson/adnl.c                              |  27 +
 drivers/usb/gadget/Kconfig                    |   2 +
 drivers/usb/gadget/Makefile                   |   1 +
 drivers/usb/gadget/amlogic/Kconfig            |  11 +
 drivers/usb/gadget/amlogic/adnl/Kconfig       |  29 +
 drivers/usb/gadget/amlogic/adnl/Makefile      |   4 +
 drivers/usb/gadget/amlogic/adnl/adnl.h        | 124 +++
 .../gadget/amlogic/adnl/adnl_buff_manager.c   | 316 +++++++
 drivers/usb/gadget/amlogic/adnl/adnl_media.c  | 235 +++++
 .../usb/gadget/amlogic/adnl/adnl_storage.c    | 140 +++
 drivers/usb/gadget/amlogic/adnl/f_adnl.c      | 835 ++++++++++++++++++
 13 files changed, 1732 insertions(+)
 create mode 100644 cmd/meson/adnl.c
 create mode 100644 drivers/usb/gadget/amlogic/Kconfig
 create mode 100644 drivers/usb/gadget/amlogic/adnl/Kconfig
 create mode 100644 drivers/usb/gadget/amlogic/adnl/Makefile
 create mode 100644 drivers/usb/gadget/amlogic/adnl/adnl.h
 create mode 100644 drivers/usb/gadget/amlogic/adnl/adnl_buff_manager.c
 create mode 100644 drivers/usb/gadget/amlogic/adnl/adnl_media.c
 create mode 100644 drivers/usb/gadget/amlogic/adnl/adnl_storage.c
 create mode 100644 drivers/usb/gadget/amlogic/adnl/f_adnl.c
cmd/Kconfig +7 -0
index 9e8a9c3cb3a..2f195d03848 100644
@@ -1146,6 +1146,13 @@ config CMD_FASTBOOT
 
 	  See doc/android/fastboot.rst for more information.
 
+config CMD_ADNL
+	bool "ADNL - Amlogic download protocol"
+	depends on ADNL
+	help
+          This command, "adnl", enables the ADNL listen mode, providing
+	  the ability to listen in ADNL mode.
+
 config CMD_FLASH
 	bool "flinfo, erase, protect"
 	default y
cmd/meson/Makefile +1 -0
index ee26c175cfe..1cf984378a3 100644
@@ -3,3 +3,4 @@
 # Copyright (c) 2022, SberDevices. All rights reserved.
 
 obj-y += sm.o
+obj-$(CONFIG_CMD_ADNL) += gadget.o adnl.o
cmd/meson/adnl.c new file +27 -0
new file mode 100644
index 00000000000..13aa1f2113e
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2024 SaluteDevices, Inc.
+ * Author: Arseniy Krasnov <[email protected]>
+ */
+
+#include <command.h>
+
+#include "gadget.h"
+
+static int do_adnl(struct cmd_tbl *cmdtp, int flag, int argc,
+		   char *const argv[])
+{
+	return amlogic_gadget_run("usb_dnl_adnl", argc, argv);
+}
+
+U_BOOT_LONGHELP(adnl,
+	"- run as an ADNL USB device\n\n"
+	"With:\n"
+	"\t[init timeout]: time between command start and moment when\n"
+	"\t                device enters gadget mode, in ms\n"
+	"\t[identify timeout]: time between moment when device enters gadget\n"
+	"\t                    mode and first 'identify' command from PC, in ms\n");
+
+U_BOOT_CMD(adnl, 3, 1, do_adnl,
+	   "ADNL protocol mode",
+	   adnl_help_text);
drivers/usb/gadget/Kconfig +2 -0
index c815764c2bc..6391ceb9cfa 100644
@@ -289,6 +289,8 @@ config USBNET_HOST_ADDR
 
 endif # USB_ETHER
 
+source "drivers/usb/gadget/amlogic/Kconfig"
+
 endif # USB_GADGET
 
 if SPL_USB_GADGET
drivers/usb/gadget/Makefile +1 -0
index 6c16ad83f6e..2b6705c52be 100644
@@ -28,6 +28,7 @@ obj-$(CONFIG_USB_FUNCTION_THOR) += f_thor.o
 obj-$(CONFIG_DFU_OVER_USB) += f_dfu.o
 obj-$(CONFIG_USB_FUNCTION_MASS_STORAGE) += f_mass_storage.o
 obj-$(CONFIG_USB_FUNCTION_FASTBOOT) += f_fastboot.o f_fastboot_common.o
+obj-$(CONFIG_ADNL) += f_fastboot_common.o amlogic/adnl/
 obj-$(CONFIG_USB_FUNCTION_SDP) += f_sdp.o
 obj-$(CONFIG_USB_FUNCTION_ROCKUSB) += f_rockusb.o
 obj-$(CONFIG_USB_FUNCTION_ACM)	+= f_acm.o
drivers/usb/gadget/amlogic/Kconfig new file +11 -0
new file mode 100644
index 00000000000..460dc6fe235
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# (C) Copyright 2024 SaluteDevices, Inc.
+
+choice
+	prompt "Amlogic burning protocols"
+	optional
+
+source "drivers/usb/gadget/amlogic/adnl/Kconfig"
+
+endchoice
drivers/usb/gadget/amlogic/adnl/Kconfig new file +29 -0
new file mode 100644
index 00000000000..21438b716ef
@@ -0,0 +1,29 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# (C) Copyright 2023 SaluteDevices, Inc.
+
+config ADNL
+	bool "Enable ADNL protocol"
+	depends on USB_GADGET
+	depends on MESON_A1
+	depends on MTD_SPI_NAND
+	help
+	  This enables ADNL protocol.
+
+if ADNL
+
+config ADNL_BUF_ADDR
+	hex "Define ADNL buffer address"
+	help
+	  The ADNL protocol requires a large memory buffer for
+	  downloads. Define this to the starting RAM address to use for
+	  downloaded images.
+
+config ADNL_BUF_SIZE
+	hex "Define ADNL buffer size"
+	help
+	  The ADNL protocol requires a large memory buffer for
+	  downloads. This buffer should be as large as possible for a
+	  platform. Define this to the size of available RAM for ADNL.
+
+endif
drivers/usb/gadget/amlogic/adnl/Makefile new file +4 -0
new file mode 100644
index 00000000000..2b8c933a35d
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+subdir-ccflags-y += -I$(src)/../ -I$(src)/../../ -I$(src)/../../../../../
+obj-y += adnl_buff_manager.o adnl_media.o adnl_storage.o f_adnl.o
drivers/usb/gadget/amlogic/adnl/adnl.h new file +124 -0

124 lines not on this page · raw message

drivers/usb/gadget/amlogic/adnl/adnl_buff_manager.c new file +316 -0

316 lines not on this page · raw message

drivers/usb/gadget/amlogic/adnl/adnl_media.c new file +235 -0

235 lines not on this page · raw message

drivers/usb/gadget/amlogic/adnl/adnl_storage.c new file +140 -0

140 lines not on this page · raw message

drivers/usb/gadget/amlogic/adnl/f_adnl.c new file +835 -0

835 lines not on this page · raw message

Signature · 1 line
2.30.1
#9 lore raw

PATCH 9/11 arm: meson: a1: ADNL protocol support

In reply to Arseniy Krasnov

During ADNL protocol operations, BootROM may request to enter ADNL mode after boot. So to support this, let's check for values in sticky registers after boot (such registers could be updated earlier by boot ROM, telling U-boot to enter ADNL mode.

---
 arch/arm/include/asm/arch-meson/a1.h | 13 +++++++++++
 arch/arm/mach-meson/board-a1.c       | 32 +++++++++++++++++++++++++++-
 arch/arm/mach-meson/board-common.c   | 17 +++++++++++++++
 3 files changed, 61 insertions(+), 1 deletion(-)
arch/arm/include/asm/arch-meson/a1.h +13 -0
index 16e209ed932..4a22aaf5627 100644
@@ -12,6 +12,7 @@
 /* SYSCTRL registers */
 #define A1_SYSCTRL_ADDR(off)		(A1_SYSCTRL_BASE + ((off) << 2))
 
+#define A1_SYSCTRL_SEC_STATUS_REG1	A1_SYSCTRL_ADDR(0xc1)
 #define A1_SYSCTRL_SEC_STATUS_REG4	A1_SYSCTRL_ADDR(0xc4)
 /*
  * Checked during boot. Tells, that previous code, e.g.
@@ -20,7 +21,19 @@
  */
 #define A1_SYSCTRL_SEC_STICKY_REG2	A1_SYSCTRL_ADDR(0xe2)
 
+#define A1_AO_BOOT_DEVICE		0xF
 #define A1_SYSCTRL_MEM_SIZE_MASK	0xFFFF0000
 #define A1_SYSCTRL_MEM_SIZE_SHIFT	16
 
+#if CONFIG_IS_ENABLED(ADNL)
+/**
+ * is_tpl_loaded_from_usb - Checks, that this instance of U-boot was
+ *                          loaded from USB during firmware update
+ *                          process.
+ *
+ * returns: true, if so.
+ */
+int is_tpl_loaded_from_usb(void);
+#endif
+
 #endif /* __MESON_A1_H__ */
arch/arm/mach-meson/board-a1.c +31 -1
index f848c0f068e..9cc410f1e57 100644
@@ -3,6 +3,7 @@
  * (C) Copyright 2023 SberDevices, Inc.
  */
 
+#include <command.h>
 #include <asm/arch/a1.h>
 #include <asm/arch/boot.h>
 #include <asm/armv8/mmu.h>
@@ -23,9 +24,38 @@ void meson_init_reserved_memory(__maybe_unused void *fdt)
 
 int meson_get_boot_device(void)
 {
-	return -ENOSYS;
+	return readl(A1_SYSCTRL_SEC_STATUS_REG4) & A1_AO_BOOT_DEVICE;
 }
 
+#if CONFIG_IS_ENABLED(ADNL)
+
+static bool is_boot_device_usb(void)
+{
+	return meson_get_boot_device() == BOOT_DEVICE_USB;
+}
+
+#define MESON_ADNL_BL1_USB_PROTO_DNL_BIT	BIT(12)
+
+static bool is_bl1_usb_protocol_DNL(void)
+{
+	u32 val = readl(A1_SYSCTRL_SEC_STATUS_REG1);
+
+	return !(val & MESON_ADNL_BL1_USB_PROTO_DNL_BIT);
+}
+
+int is_tpl_loaded_from_usb(void)
+{
+	if (!is_boot_device_usb())
+		return false;
+
+	if (!is_bl1_usb_protocol_DNL())
+		return false;
+
+	return true;
+}
+
+#endif
+
 static struct mm_region a1_mem_map[] = {
 	{
 		.virt = 0x00000000UL,
arch/arm/mach-meson/board-common.c +17 -0
index 39774c43049..0624d557e40 100644
@@ -3,6 +3,7 @@
  * (C) Copyright 2016 Beniamino Galvani <[email protected]>
  */
 
+#include <command.h>
 #include <cpu_func.h>
 #include <fastboot.h>
 #include <init.h>
@@ -14,9 +15,11 @@
 #include <asm/ptrace.h>
 #include <linux/libfdt.h>
 #include <linux/err.h>
+#include <asm/arch/a1.h>
 #include <asm/arch/mem.h>
 #include <asm/arch/sm.h>
 #include <asm/armv8/mmu.h>
+#include <asm/io.h>
 #include <asm/unaligned.h>
 #include <efi_loader.h>
 #include <u-boot/crc.h>
@@ -141,10 +144,24 @@ __weak int meson_board_late_init(void)
 	return 0;
 }
 
+static void meson_board_try_enter_adnl(void)
+{
+#if (IS_ENABLED(CONFIG_CMD_ADNL))
+	if (!is_tpl_loaded_from_usb())
+		return;
+
+	meson_sm_set_bl1_first_boot_source(SCPI_CMD_CLEAR_BOOT);
+
+	run_command("adnl", 0);
+#endif
+}
+
 int board_late_init(void)
 {
 	meson_set_boot_source();
 
+	meson_board_try_enter_adnl();
+
 	return meson_board_late_init();
 }
 
Signature · 1 line
2.30.1
#10 lore raw

PATCH 10/11 usb: gadget: amlogic: implement Optimus protocol

In reply to Arseniy Krasnov

From: Vladimir Mitrofanov <[email protected]>

Add Amlogic's Optimus protocol support for USB transport.

---
 cmd/Kconfig                                   |   7 +
 cmd/meson/Makefile                            |   1 +
 cmd/meson/optimus.c                           |  21 +
 drivers/usb/gadget/Makefile                   |   1 +
 drivers/usb/gadget/amlogic/Kconfig            |   1 +
 drivers/usb/gadget/amlogic/optimus/Kconfig    |  12 +
 drivers/usb/gadget/amlogic/optimus/Makefile   |   7 +
 .../usb/gadget/amlogic/optimus/f_optimus.c    | 687 ++++++++++++++++++
 .../gadget/amlogic/optimus/optimus_download.c | 188 +++++
 .../gadget/amlogic/optimus/optimus_download.h |  86 +++
 10 files changed, 1011 insertions(+)
 create mode 100644 cmd/meson/optimus.c
 create mode 100644 drivers/usb/gadget/amlogic/optimus/Kconfig
 create mode 100644 drivers/usb/gadget/amlogic/optimus/Makefile
 create mode 100644 drivers/usb/gadget/amlogic/optimus/f_optimus.c
 create mode 100644 drivers/usb/gadget/amlogic/optimus/optimus_download.c
 create mode 100644 drivers/usb/gadget/amlogic/optimus/optimus_download.h
cmd/Kconfig +7 -0
index 2f195d03848..159e52d5298 100644
@@ -1153,6 +1153,13 @@ config CMD_ADNL
           This command, "adnl", enables the ADNL listen mode, providing
 	  the ability to listen in ADNL mode.
 
+config CMD_OPTIMUS
+	bool "OPTIMUS - Amlogic download protocol"
+	depends on OPTIMUS
+	help
+          This command, "optimus", enables the OPTIMUS listen mode, providing
+	  the ability to communicate with host by OPTIMUS protocol.
+
 config CMD_FLASH
 	bool "flinfo, erase, protect"
 	default y
cmd/meson/Makefile +1 -0
index 1cf984378a3..611d1549736 100644
@@ -4,3 +4,4 @@
 
 obj-y += sm.o
 obj-$(CONFIG_CMD_ADNL) += gadget.o adnl.o
+obj-$(CONFIG_CMD_OPTIMUS) += gadget.o optimus.o
cmd/meson/optimus.c new file +21 -0
new file mode 100644
index 00000000000..97a1435c0ab
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2024 SaluteDevices, Inc.
+ * Author: Vladimir Mitrofanov <[email protected]>
+ */
+
+#include <command.h>
+
+#include "gadget.h"
+
+static int do_optimus(struct cmd_tbl *cmdtp, int flag, int argc,
+		      char *const argv[])
+{
+	return amlogic_gadget_run("usb_dnl_optimus", argc, argv);
+}
+
+U_BOOT_LONGHELP(optimus,
+		"- run as an OPTIMUS USB device\n\n");
+
+U_BOOT_CMD(optimus, 1, 1, do_optimus,
+	   "OPTIMUS protocol mode", optimus_help_text);
drivers/usb/gadget/Makefile +1 -0
index 2b6705c52be..f5899a96221 100644
@@ -22,6 +22,7 @@ obj-$(CONFIG_USB_GADGET_DWC2_OTG) += dwc2_udc_otg.o
 obj-$(CONFIG_USB_GADGET_DWC2_OTG_PHY) += dwc2_udc_otg_phy.o
 obj-$(CONFIG_USB_GADGET_MAX3420) += max3420_udc.o
 obj-$(CONFIG_USB_RENESAS_USBHS) += rcar/
+obj-$(CONFIG_OPTIMUS) += f_fastboot_common.o amlogic/optimus/
 ifndef CONFIG_XPL_BUILD
 obj-$(CONFIG_USB_GADGET_DOWNLOAD) += g_dnl.o
 obj-$(CONFIG_USB_FUNCTION_THOR) += f_thor.o
drivers/usb/gadget/amlogic/Kconfig +1 -0
index 460dc6fe235..5a16fb25998 100644
@@ -7,5 +7,6 @@ choice
 	optional
 
 source "drivers/usb/gadget/amlogic/adnl/Kconfig"
+source "drivers/usb/gadget/amlogic/optimus/Kconfig"
 
 endchoice
drivers/usb/gadget/amlogic/optimus/Kconfig new file +12 -0
new file mode 100644
index 00000000000..6a730670d34
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# (C) Copyright 2024 SaluteDevices, Inc.
+
+config OPTIMUS
+	bool "Enable OPTIMUS protocol"
+	depends on USB_GADGET
+	depends on MESON_AXG
+	help
+	  This enables usb optimus function protocol
+	  "Optimus" protocol is used to flash Amlogic devices
+	  from bootRom or from U-Boot mode
drivers/usb/gadget/amlogic/optimus/Makefile new file +7 -0
new file mode 100644
index 00000000000..f48806414aa
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+obj-y += f_optimus_cb.o
+
+subdir-ccflags-y += -I$(src)/../ -I$(src)/../../
+f_optimus_cb-y += f_optimus.o
+f_optimus_cb-y += optimus_download.o
drivers/usb/gadget/amlogic/optimus/f_optimus.c new file +687 -0

687 lines not on this page · raw message

drivers/usb/gadget/amlogic/optimus/optimus_download.c new file +188 -0

188 lines not on this page · raw message

drivers/usb/gadget/amlogic/optimus/optimus_download.h new file +86 -0

86 lines not on this page · raw message

Signature · 1 line
2.30.1
#11 lore raw

PATCH 11/11 arch: arm: axg: Optimus protocol support

In reply to Arseniy Krasnov

From: Vladimir Mitrofanov <[email protected]>

During Optimus protocol operations, BootROM may request to enter Optimus mode after boot. So to support this, let's check for values in sticky registers after boot (such registers could be updated earlier by boot ROM, telling U-boot to enter Optimus mode).

---
 arch/arm/include/asm/arch-meson/axg.h | 12 ++++++++++++
 arch/arm/mach-meson/board-axg.c       | 12 ++++++++++++
 arch/arm/mach-meson/board-common.c    | 15 +++++++++++++++
 3 files changed, 39 insertions(+)
arch/arm/include/asm/arch-meson/axg.h +12 -0
index 12042de9359..e99cd3aa780 100644
@@ -23,6 +23,7 @@
 #define AXG_AO_SEC_GP_CFG3	AXG_AO_ADDR(0x93)
 #define AXG_AO_SEC_GP_CFG4	AXG_AO_ADDR(0x94)
 #define AXG_AO_SEC_GP_CFG5	AXG_AO_ADDR(0x95)
+#define AXG_AO_SEC_GP_CFG7	AXG_AO_ADDR(0x97)
 
 #define AXG_AO_BOOT_DEVICE	0xF
 #define AXG_AO_MEM_SIZE_MASK	0xFFFF0000
@@ -31,4 +32,15 @@
 #define AXG_AO_BL31_RSVMEM_SIZE_SHIFT	16
 #define AXG_AO_BL32_RSVMEM_SIZE_MASK	0xFFFF
 
+#if CONFIG_IS_ENABLED(CMD_OPTIMUS)
+/**
+ * is_tpl_loaded_from_usb - Checks, that this instance of U-boot was
+ *                          loaded from USB during firmware update
+ *                          process.
+ *
+ * returns: true, if so.
+ */
+int is_tpl_loaded_from_usb(void);
+#endif
+
 #endif /* __AXG_H__ */
arch/arm/mach-meson/board-axg.c +12 -0
index 6535539184c..13eb66dfc21 100644
@@ -4,12 +4,14 @@
  * (C) Copyright 2018 Neil Armstrong <[email protected]>
  */
 
+#include <command.h>
 #include <init.h>
 #include <net.h>
 #include <asm/arch/boot.h>
 #include <asm/arch/eth.h>
 #include <asm/arch/axg.h>
 #include <asm/arch/mem.h>
+#include <asm/arch-meson/sm.h>
 #include <asm/global_data.h>
 #include <asm/io.h>
 #include <asm/armv8/mmu.h>
@@ -62,6 +64,16 @@ phys_size_t get_effective_memsize(void)
 			>> AXG_AO_MEM_SIZE_SHIFT) * SZ_1M;
 }
 
+#if CONFIG_IS_ENABLED(CMD_OPTIMUS)
+int is_tpl_loaded_from_usb(void)
+{
+	const int boot_id = meson_get_boot_device();
+	const unsigned int force_usb_boot = readl(AXG_AO_SEC_GP_CFG7);
+
+	return (boot_id == BOOT_DEVICE_USB || (force_usb_boot & BIT(31)));
+}
+#endif
+
 static struct mm_region axg_mem_map[] = {
 	{
 		.virt = 0x0UL,
arch/arm/mach-meson/board-common.c +15 -0
index 0624d557e40..639c7a64507 100644
@@ -16,6 +16,7 @@
 #include <linux/libfdt.h>
 #include <linux/err.h>
 #include <asm/arch/a1.h>
+#include <asm/arch/axg.h>
 #include <asm/arch/mem.h>
 #include <asm/arch/sm.h>
 #include <asm/armv8/mmu.h>
@@ -144,6 +145,18 @@ __weak int meson_board_late_init(void)
 	return 0;
 }
 
+static void meson_board_try_enter_optimus(void)
+{
+#if (IS_ENABLED(CONFIG_CMD_OPTIMUS))
+	if (!is_tpl_loaded_from_usb())
+		return;
+
+	meson_sm_set_usb_boot_mode(CLEAR_USB_BOOT);
+
+	run_command("optimus", 0);
+#endif
+}
+
 static void meson_board_try_enter_adnl(void)
 {
 #if (IS_ENABLED(CONFIG_CMD_ADNL))
@@ -162,6 +175,8 @@ int board_late_init(void)
 
 	meson_board_try_enter_adnl();
 
+	meson_board_try_enter_optimus();
+
 	return meson_board_late_init();
 }
 
Signature · 1 line
2.30.1
#12 lore raw
In reply to Arseniy Krasnov
Quoted 6 lines · Arseniy Krasnov via groups.io

This patchset adds support of two Amlogic firmware burning protocols:
ADNL and Optimus. Each protocol is supported on the following SoC:

axg, g12a, g12b, sm1 - Optimus.
a1, s4, a5, c1, c2, c3, sc2, t3, t7, p1 - ADNL.

Out of curiosity, don't GXL and GXBB also support Optimus? Or is it completely different?

Quoted 11 lines

Both work in fastboot manner, but significant feature is that they are supported by current Amlogic BootROMs. As transport both use USB:
device is switched to gadget mode and then it can communicate with PC. On PC side protocols are supported by:

1) https://github.com/superna9999/pyamlboot (with some limitation)

   For example:
   pyamlboot/./ubt.py --img <image> --wipe all --reset

Note, image has special format, specific for Amlogic.

Awesome :)

Regards.