public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Beiyan Yun <root@infi.wang>
To: u-boot@lists.denx.de
Cc: Tom Rini <trini@konsulko.com>, Yao Zi <ziyao@disroot.org>,
	Marek Vasut <marek.vasut+renesas@mailbox.org>,
	Beiyan Yun <root@infi.wang>,
	Jerome Forissier <jerome.forissier@linaro.org>,
	Joe Hershberger <joe.hershberger@ni.com>,
	"Lucien.Jheng" <lucienzx159@gmail.com>,
	Ramon Fried <rfried.dev@gmail.com>,
	Romain Gantois <romain.gantois@bootlin.com>,
	Siddharth Vadapalli <s-vadapalli@ti.com>,
	Sky Huang <SkyLake.Huang@mediatek.com>,
	Weijie Gao <weijie.gao@mediatek.com>
Subject: [PATCH v5 2/2] net: phy: aquantia: use generic firmware loader
Date: Sat,  1 Nov 2025 20:31:35 +0800	[thread overview]
Message-ID: <20251101123401.110681-3-root@infi.wang> (raw)
In-Reply-To: <20251101123401.110681-1-root@infi.wang>

Aquantia PHYs are being used w/o SPI flash in some routers recently.
Current firmware loader only attempts to load from FS on top of MMC,
limiting the use on many devices.

Removed the old firmware loader, migrate to generic script based firmware
loader to allow a wider range and runtime override of firmware source.
(e.g., MMC, USB, UBIFS).

Tested on Buffalo WXR18000BE10P with UBIFS.

Signed-off-by: Beiyan Yun <root@infi.wang>
---

Changes in v5:
- Remove fsloader codepath
- Drop binding changes
- Make script name static

Changes in v4:
- Split firmware upload helpers change
- Reorder `aquantia_read_fw`
- Make `aquantia_read_fw` weak to allow overide
- Rename exit label in `aquantia_read_fw`
- Kconfig polish

Changes in v3:
- Select FW_LOADER with PHY_AQUANTIA_UPLOAD_FW

Changes in v2:
- Add support for script based loader

 drivers/net/phy/Kconfig    | 23 ++++++------
 drivers/net/phy/aquantia.c | 76 ++++++++++++++------------------------
 2 files changed, 39 insertions(+), 60 deletions(-)

diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig
index 018be98705a..d8845469dd5 100644
--- a/drivers/net/phy/Kconfig
+++ b/drivers/net/phy/Kconfig
@@ -91,23 +91,22 @@ menuconfig PHY_AQUANTIA
 config PHY_AQUANTIA_UPLOAD_FW
 	bool "Aquantia firmware loading support"
 	depends on PHY_AQUANTIA
+	select FW_LOADER
 	help
-		Aquantia PHYs use firmware which can be either loaded automatically
-		from storage directly attached to the phy or loaded by the boot loader
-		via MDIO commands.  The firmware is loaded from a file, specified by
-		the PHY_AQUANTIA_FW_PART and PHY_AQUANTIA_FW_NAME options.
+	  Aquantia PHYs use firmware which can be either loaded automatically
+	  from storage directly attached to the phy or loaded by the boot loader
+	  via MDIO commands.
 
-config PHY_AQUANTIA_FW_PART
-	string "Aquantia firmware partition"
-	depends on PHY_AQUANTIA_UPLOAD_FW
-	help
-		Partition containing the firmware file.
+	  This option enables loading the firmware using the generic
+	  firmware loader framework.
 
-config PHY_AQUANTIA_FW_NAME
-	string "Aquantia firmware filename"
+config PHY_AQUANTIA_FW_MAX_SIZE
+	hex "Max firmware size"
 	depends on PHY_AQUANTIA_UPLOAD_FW
+	default 0x80000
 	help
-		Firmware filename.
+	  The maximum size of the Aquantia PHY firmware. This is used to
+	  allocate a buffer to load the firmware into.
 
 config PHY_ATHEROS
 	bool "Atheros Ethernet PHYs support"
diff --git a/drivers/net/phy/aquantia.c b/drivers/net/phy/aquantia.c
index fc2476fb038..e0deef58f52 100644
--- a/drivers/net/phy/aquantia.c
+++ b/drivers/net/phy/aquantia.c
@@ -16,7 +16,9 @@
 #include <u-boot/crc.h>
 #include <malloc.h>
 #include <asm/byteorder.h>
-#include <fs.h>
+#if (IS_ENABLED(CONFIG_PHY_AQUANTIA_UPLOAD_FW))
+#include <fw_loader.h>
+#endif
 
 #define AQUNTIA_10G_CTL		0x20
 #define AQUNTIA_VENDOR_P1	0xc400
@@ -127,52 +129,29 @@ struct fw_header {
 
 #pragma pack()
 
-#if defined(CONFIG_PHY_AQUANTIA_UPLOAD_FW)
-static int aquantia_read_fw(u8 **fw_addr, size_t *fw_length)
+#if (IS_ENABLED(CONFIG_PHY_AQUANTIA_UPLOAD_FW))
+int __weak aquantia_read_fw(struct phy_device *phydev,
+			    u8 **fw_addr, size_t *fw_length)
 {
-	loff_t length, read;
 	int ret;
-	void *addr = NULL;
-
-	*fw_addr = NULL;
-	*fw_length = 0;
-	debug("Loading Aquantia microcode from %s %s\n",
-	      CONFIG_PHY_AQUANTIA_FW_PART, CONFIG_PHY_AQUANTIA_FW_NAME);
-	ret = fs_set_blk_dev("mmc", CONFIG_PHY_AQUANTIA_FW_PART, FS_TYPE_ANY);
-	if (ret < 0)
-		goto cleanup;
-
-	ret = fs_size(CONFIG_PHY_AQUANTIA_FW_NAME, &length);
-	if (ret < 0)
-		goto cleanup;
-
-	addr = malloc(length);
-	if (!addr) {
-		ret = -ENOMEM;
-		goto cleanup;
-	}
-
-	ret = fs_set_blk_dev("mmc", CONFIG_PHY_AQUANTIA_FW_PART, FS_TYPE_ANY);
-	if (ret < 0)
-		goto cleanup;
-
-	ret = fs_read(CONFIG_PHY_AQUANTIA_FW_NAME, (ulong)addr, 0, length,
-		      &read);
-	if (ret < 0)
-		goto cleanup;
+	u8 *microcode;
 
-	*fw_addr = addr;
-	*fw_length = length;
-	debug("Found Aquantia microcode.\n");
+	microcode = malloc(CONFIG_PHY_AQUANTIA_FW_MAX_SIZE);
+	if (!microcode) {
+		printf("Failed to allocate memory for firmware\n");
+		return -ENOMEM;
+	}
 
-cleanup:
-	if (ret < 0) {
-		printf("loading firmware file %s %s failed with error %d\n",
-		       CONFIG_PHY_AQUANTIA_FW_PART,
-		       CONFIG_PHY_AQUANTIA_FW_NAME, ret);
-		free(addr);
+	ret = request_firmware_into_buf_via_script(
+		microcode, CONFIG_PHY_AQUANTIA_FW_MAX_SIZE,
+		"aqr_phy_load_firmware", fw_length);
+	if (ret) {
+		free(microcode);
+		return ret;
 	}
-	return ret;
+
+	*fw_addr = microcode;
+	return 1;
 }
 
 /* load data into the phy's memory */
@@ -293,16 +272,17 @@ static int aquantia_do_upload_firmware(struct phy_device *phydev,
 
 static int aquantia_upload_firmware(struct phy_device *phydev)
 {
-	int ret;
+	int ret, fwrc;
 	u8 *addr = NULL;
-	size_t fw_length = 0;
+	size_t fw_length;
 
-	ret = aquantia_read_fw(&addr, &fw_length);
-	if (ret != 0)
-		return ret;
+	fwrc = aquantia_read_fw(phydev, &addr, &fw_length);
+	if (fwrc < 0)
+		return fwrc;
 
 	ret = aquantia_do_upload_firmware(phydev, addr, fw_length);
-	free(addr);
+	if (fwrc > 0)
+		free(addr);
 
 	return ret;
 }
-- 
2.47.3


  parent reply	other threads:[~2025-11-01 12:34 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-01 12:31 [PATCH v5 0/2] net: phy: aquantia: Switch to generic firmware loader Beiyan Yun
2025-11-01 12:31 ` [PATCH v5 1/2] net: phy: aquantia: refactor firmware upload helpers Beiyan Yun
2025-11-01 12:31 ` Beiyan Yun [this message]
2025-11-15  7:48 ` [PATCH v5 0/2] net: phy: aquantia: Switch to generic firmware loader Beiyan Yun
2025-11-26 14:22   ` Jerome Forissier
2025-11-26 14:24     ` Marek Vasut
2025-11-26 14:28       ` Jerome Forissier
2025-11-29  3:10       ` Beiyan Yun
2025-11-29  3:47         ` Marek Vasut

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=20251101123401.110681-3-root@infi.wang \
    --to=root@infi.wang \
    --cc=SkyLake.Huang@mediatek.com \
    --cc=jerome.forissier@linaro.org \
    --cc=joe.hershberger@ni.com \
    --cc=lucienzx159@gmail.com \
    --cc=marek.vasut+renesas@mailbox.org \
    --cc=rfried.dev@gmail.com \
    --cc=romain.gantois@bootlin.com \
    --cc=s-vadapalli@ti.com \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=weijie.gao@mediatek.com \
    --cc=ziyao@disroot.org \
    /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