public inbox for rulkc@linuxtesting.org
 help / color / mirror / Atom feed
From: avkrasnov at salutedevices.com (Arseniy Krasnov)
Subject: [rulkc] [PATCH v1 04/11] usb: gadget: amlogic: common code for Amlogic flashing commands
Date: Tue, 29 Jul 2025 21:29:03 +0300	[thread overview]
Message-ID: <20250729182910.16127-5-avkrasnov@salutedevices.com> (raw)
In-Reply-To: <20250729182910.16127-1-avkrasnov@salutedevices.com>

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.

Signed-off-by: Arseniy Krasnov <avkrasnov at salutedevices.com>
---
 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

diff --git a/cmd/meson/gadget.c b/cmd/meson/gadget.c
new file mode 100644
index 00000000000..6155043ae5a
--- /dev/null
+++ b/cmd/meson/gadget.c
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2025 SaluteDevices, Inc.
+ * Author: Arseniy Krasnov <avkrasnov at salutedevices.com>
+ */
+
+#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;
+}
diff --git a/cmd/meson/gadget.h b/cmd/meson/gadget.h
new file mode 100644
index 00000000000..a3771fb4746
--- /dev/null
+++ b/cmd/meson/gadget.h
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2025 SaluteDevices, Inc.
+ * Author: Arseniy Krasnov <avkrasnov at salutedevices.com>
+ */
+
+#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__ */
-- 
2.30.1



  parent reply	other threads:[~2025-07-29 18:29 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-29 18:28 [rulkc] [PATCH v1 00/11] Amlogic: ADNL and Optimus protocols support avkrasnov
2025-07-29 18:29 ` [rulkc] [PATCH v1 01/11] arch: arm: meson: sm: add commands to reboot device in different modes avkrasnov
2025-07-29 18:29 ` [rulkc] [PATCH v1 02/11] arch: arm: meson: use 'meson_sm_call()' in 'meson_sm_pwrdm_set()' avkrasnov
2025-07-29 18:29 ` [rulkc] [PATCH v1 03/11] usb: gadget: fastboot: make part of USB fastboot code shared avkrasnov
2025-07-29 18:29 ` avkrasnov [this message]
2025-07-29 18:29 ` [rulkc] [PATCH v1 05/11] mtd: rawnand: meson: move some defines to dedicated header avkrasnov
2025-07-29 18:29 ` [rulkc] [PATCH v1 06/11] arch: arm: meson: bootloader write support avkrasnov
2025-07-29 18:29 ` [rulkc] [PATCH v1 07/11] arm: meson: a1: add A1_SYSCTRL_SEC_STICKY_REG2 avkrasnov
2025-07-29 18:29 ` [rulkc] [PATCH v1 08/11] usb: gadget: amlogic: implement ADNL protocol avkrasnov
2025-07-29 18:29 ` [rulkc] [PATCH v1 09/11] arm: meson: a1: ADNL protocol support avkrasnov
2025-07-29 18:29 ` [rulkc] [PATCH v1 10/11] usb: gadget: amlogic: implement Optimus protocol avkrasnov
2025-07-29 18:29 ` [rulkc] [PATCH v1 11/11] arch: arm: axg: Optimus protocol support avkrasnov
2025-07-31  1:07 ` [rulkc] [PATCH v1 00/11] Amlogic: ADNL and Optimus protocols support slava

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=20250729182910.16127-5-avkrasnov@salutedevices.com \
    --to=rulkc@linuxtesting.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
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

all inboxes | Powered by JetHome®