Skip to contentSkip to conversation

Load ASMedia XHCI controller firmware

PATCH In review Mark Kettenis 3 participants 12 messages Jul 14, 2023 – Jul 17, 2023

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

cover Load ASMedia XHCI controller firmware

Some of the Apple desktop machine have a PCI ASMedia XHCI controller that needs firmware to be uploaded to function. This firmware is placed on the EFI system partition by the Asahi installer. So this series sets up a file system firmware loader that reads from that partition and ports the Linux driver that uploads this firmware to the controller. With this series, users can use USB devices (such as a keyboard) when they are connected to USB ports handled by the ASMedia XHCI controller. This includes the type-A ports on the M2 Mac mini for example.

Mark Kettenis (3):
apple: Set up file system firmware loader
iopoll: Add readb_poll_sleep_timeout
usb: xhci-pci: Load ASMedia XHCI controller firmware

 MAINTAINERS                         |   1 +
 arch/arm/mach-apple/board.c         |  60 +++++
 drivers/usb/host/Kconfig            |  11 +
 drivers/usb/host/Makefile           |   1 +
 drivers/usb/host/xhci-pci-asmedia.c | 394 ++++++++++++++++++++++++++++
 drivers/usb/host/xhci-pci.c         |   9 +
 include/linux/iopoll.h              |   3 +
 include/usb/xhci.h                  |   3 +
 8 files changed, 482 insertions(+)
 create mode 100644 drivers/usb/host/xhci-pci-asmedia.c
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/3 apple: Set up file system firmware loader

Find the appropriate EFI system partition on the internal NVMe storage and set the U-Boot environment variables such that the file system firmware loader can load firmware from it.

---
 arch/arm/mach-apple/board.c | 60 +++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
arch/arm/mach-apple/board.c +60 -0
index d501948118..7799a0f916 100644
@@ -8,6 +8,8 @@
 #include <dm/uclass-internal.h>
 #include <efi_loader.h>
 #include <lmb.h>
+#include <nvme.h>
+#include <part.h>
 
 #include <asm/armv8/mmu.h>
 #include <asm/global_data.h>
@@ -539,6 +541,60 @@ u64 get_page_table_size(void)
 	return SZ_256K;
 }
 
+static char *asahi_esp_devpart(void)
+{
+	struct disk_partition info;
+	struct blk_desc *nvme_blk;
+	const char *uuid = NULL;
+	int devnum, len, p, part, ret;
+	static char devpart[64];
+	struct udevice *dev;
+	ofnode node;
+
+	if (devpart[0])
+		return devpart;
+
+	node = ofnode_path("/chosen");
+	if (ofnode_valid(node)) {
+		uuid = ofnode_get_property(node, "asahi,efi-system-partition",
+					   &len);
+	}
+
+	nvme_scan_namespace();
+	for (devnum = 0, part = 0;; devnum++) {
+		nvme_blk = blk_get_devnum_by_uclass_id(UCLASS_NVME, devnum);
+		if (nvme_blk == NULL)
+			break;
+
+		dev = dev_get_parent(nvme_blk->bdev);
+		if (!device_is_compatible(dev, "apple,nvme-ans2"))
+			continue;
+
+		for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
+			ret = part_get_info(nvme_blk, p, &info);
+			if (ret < 0)
+				break;
+
+			if (info.bootable & PART_EFI_SYSTEM_PARTITION) {
+				if (uuid && strcasecmp(uuid, info.uuid) == 0) {
+					part = p;
+					break;
+				}
+				if (part == 0)
+					part = p;
+			}
+		}
+
+		if (part > 0)
+			break;
+	}
+
+	if (part > 0)
+		snprintf(devpart, sizeof(devpart), "%d:%d", devnum, part);
+
+	return devpart;
+}
+
 #define KERNEL_COMP_SIZE	SZ_128M
 
 int board_late_init(void)
@@ -546,6 +602,10 @@ int board_late_init(void)
 	struct lmb lmb;
 	u32 status = 0;
 
+	status |= env_set("storage_interface",
+			  blk_get_uclass_name(UCLASS_NVME));
+	status |= env_set("fw_dev_part", asahi_esp_devpart());
+
 	lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
 
 	/* somewhat based on the Linux Kernel boot requirements:
Signature · 1 line
2.41.0
#2 lore raw

Re: [PATCH 1/3] apple: Set up file system firmware loader

Quoted 43 lines · Mark Kettenis

Find the appropriate EFI system partition on the internal NVMe storage and set the U-Boot environment variables such that the file system firmware loader can load firmware from it.

---
  arch/arm/mach-apple/board.c | 60 +++++++++++++++++++++++++++++++++++++
  1 file changed, 60 insertions(+)
arch/arm/mach-apple/board.c +19 -0
index d501948118..7799a0f916 100644
@@ -8,6 +8,8 @@
  #include <dm/uclass-internal.h>
  #include <efi_loader.h>
  #include <lmb.h>
+#include <nvme.h>
+#include <part.h>
 
  #include <asm/armv8/mmu.h>
  #include <asm/global_data.h>
@@ -539,6 +541,60 @@ u64 get_page_table_size(void)
  	return SZ_256K;
  }
 
+static char *asahi_esp_devpart(void)
+{
+	struct disk_partition info;
+	struct blk_desc *nvme_blk;
+	const char *uuid = NULL;
+	int devnum, len, p, part, ret;
+	static char devpart[64];
+	struct udevice *dev;
+	ofnode node;
+
+	if (devpart[0])
+		return devpart;
+
+	node = ofnode_path("/chosen");
+	if (ofnode_valid(node)) {
+		uuid = ofnode_get_property(node, "asahi,efi-system-partition",
+					   &len);

arch/arm/mach-apple/board.c u64 get_page_table_size(void) · line 570

Quoted 10 lines
arch/arm/mach-apple/board.c +10 -0
+	}
+
+	nvme_scan_namespace();
+	for (devnum = 0, part = 0;; devnum++) {
+		nvme_blk = blk_get_devnum_by_uclass_id(UCLASS_NVME, devnum);
+		if (nvme_blk == NULL)
+			break;
+
+		dev = dev_get_parent(nvme_blk->bdev);
+		if (!device_is_compatible(dev, "apple,nvme-ans2"))

Can we somehow use ofnode_for_each_compatible_node() here ? That might simplify this code.

Quoted 31 lines
arch/arm/mach-apple/board.c +27 -0
+			continue;
+
+		for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
+			ret = part_get_info(nvme_blk, p, &info);
+			if (ret < 0)
+				break;
+
+			if (info.bootable & PART_EFI_SYSTEM_PARTITION) {
+				if (uuid && strcasecmp(uuid, info.uuid) == 0) {
+					part = p;
+					break;
+				}
+				if (part == 0)
+					part = p;
+			}
+		}
+
+		if (part > 0)
+			break;
+	}
+
+	if (part > 0)
+		snprintf(devpart, sizeof(devpart), "%d:%d", devnum, part);
+
+	return devpart;
+}
+
  #define KERNEL_COMP_SIZE	SZ_128M
 
  int board_late_init(void)

arch/arm/mach-apple/board.c int board_late_init(void) · line 607

Quoted 6 lines
arch/arm/mach-apple/board.c +3 -0
@@ -546,6 +602,10 @@ int board_late_init(void)
  	struct lmb lmb;
  	u32 status = 0;
 
+	status |= env_set("storage_interface",
+			  blk_get_uclass_name(UCLASS_NVME));
+	status |= env_set("fw_dev_part", asahi_esp_devpart());

I think env_set() returns integer (and this could be negative too), so you might want to check the return value instead of casting it to unsigned integer.

#3 lore raw

Re: [PATCH 1/3] apple: Set up file system firmware loader

Quoted 52 lines

Date: Fri, 14 Jul 2023 23:27:42 +0200
From: Marek Vasut <[email protected]>

Quoted 48 lines · Mark Kettenis

Find the appropriate EFI system partition on the internal NVMe storage and set the U-Boot environment variables such that the file system firmware loader can load firmware from it.

---
  arch/arm/mach-apple/board.c | 60 +++++++++++++++++++++++++++++++++++++
  1 file changed, 60 insertions(+)
arch/arm/mach-apple/board.c +24 -0
index d501948118..7799a0f916 100644
@@ -8,6 +8,8 @@
  #include <dm/uclass-internal.h>
  #include <efi_loader.h>
  #include <lmb.h>
+#include <nvme.h>
+#include <part.h>
 
  #include <asm/armv8/mmu.h>
  #include <asm/global_data.h>
@@ -539,6 +541,60 @@ u64 get_page_table_size(void)
  	return SZ_256K;
  }
 
+static char *asahi_esp_devpart(void)
+{
+	struct disk_partition info;
+	struct blk_desc *nvme_blk;
+	const char *uuid = NULL;
+	int devnum, len, p, part, ret;
+	static char devpart[64];
+	struct udevice *dev;
+	ofnode node;
+
+	if (devpart[0])
+		return devpart;
+
+	node = ofnode_path("/chosen");
+	if (ofnode_valid(node)) {
+		uuid = ofnode_get_property(node, "asahi,efi-system-partition",
+					   &len);
+	}
+
+	nvme_scan_namespace();
+	for (devnum = 0, part = 0;; devnum++) {
+		nvme_blk = blk_get_devnum_by_uclass_id(UCLASS_NVME, devnum);
Quoted 8 lines

arch/arm/mach-apple/board.c u64 get_page_table_size(void) · line 570

Quoted 5 lines
arch/arm/mach-apple/board.c +5 -0
+		if (nvme_blk == NULL)
+			break;
+
+		dev = dev_get_parent(nvme_blk->bdev);
+		if (!device_is_compatible(dev, "apple,nvme-ans2"))

Can we somehow use ofnode_for_each_compatible_node() here ? That might simplify this code.

I don't really see how that would simplify things. I'm iterating over all NVMe devices here and then checking the compatible of the parent to make sure I pick the on-board one. I could do the inverse and lookup the node first and then use that to find the NVMe block device, but it will still involve a loop and several function calls.

Quoted 35 lines
Quoted 35 lines
arch/arm/mach-apple/board.c +27 -0
+			continue;
+
+		for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
+			ret = part_get_info(nvme_blk, p, &info);
+			if (ret < 0)
+				break;
+
+			if (info.bootable & PART_EFI_SYSTEM_PARTITION) {
+				if (uuid && strcasecmp(uuid, info.uuid) == 0) {
+					part = p;
+					break;
+				}
+				if (part == 0)
+					part = p;
+			}
+		}
+
+		if (part > 0)
+			break;
+	}
+
+	if (part > 0)
+		snprintf(devpart, sizeof(devpart), "%d:%d", devnum, part);
+
+	return devpart;
+}
+
  #define KERNEL_COMP_SIZE	SZ_128M
 
  int board_late_init(void)
@@ -546,6 +602,10 @@ int board_late_init(void)
  	struct lmb lmb;
  	u32 status = 0;
 
Quoted 7 lines

arch/arm/mach-apple/board.c int board_late_init(void) · line 607

Quoted 3 lines
arch/arm/mach-apple/board.c +3 -0
+	status |= env_set("storage_interface",
+			  blk_get_uclass_name(UCLASS_NVME));
+	status |= env_set("fw_dev_part", asahi_esp_devpart());

I think env_set() returns integer (and this could be negative too), so you might want to check the return value instead of casting it to unsigned integer.

I'm just using the existing idiom. But maybe I should just check the return value and throw a warning instead? Not having the firmware loader available isn't fatal. It just means some of the USB ports won't work.

#4 lore raw

Re: [PATCH 1/3] apple: Set up file system firmware loader

Quoted 61 lines · Mark Kettenis
Quoted 52 lines

Date: Fri, 14 Jul 2023 23:27:42 +0200
From: Marek Vasut <[email protected]>

Quoted 48 lines · Mark Kettenis

Find the appropriate EFI system partition on the internal NVMe storage and set the U-Boot environment variables such that the file system firmware loader can load firmware from it.

---
   arch/arm/mach-apple/board.c | 60 +++++++++++++++++++++++++++++++++++++
   1 file changed, 60 insertions(+)
arch/arm/mach-apple/board.c +24 -0
index d501948118..7799a0f916 100644
@@ -8,6 +8,8 @@
   #include <dm/uclass-internal.h>
   #include <efi_loader.h>
   #include <lmb.h>
+#include <nvme.h>
+#include <part.h>
 
   #include <asm/armv8/mmu.h>
   #include <asm/global_data.h>
@@ -539,6 +541,60 @@ u64 get_page_table_size(void)
   	return SZ_256K;
   }
 
+static char *asahi_esp_devpart(void)
+{
+	struct disk_partition info;
+	struct blk_desc *nvme_blk;
+	const char *uuid = NULL;
+	int devnum, len, p, part, ret;
+	static char devpart[64];
+	struct udevice *dev;
+	ofnode node;
+
+	if (devpart[0])
+		return devpart;
+
+	node = ofnode_path("/chosen");
+	if (ofnode_valid(node)) {
+		uuid = ofnode_get_property(node, "asahi,efi-system-partition",
+					   &len);
+	}
+
+	nvme_scan_namespace();
+	for (devnum = 0, part = 0;; devnum++) {
+		nvme_blk = blk_get_devnum_by_uclass_id(UCLASS_NVME, devnum);
Quoted 8 lines

arch/arm/mach-apple/board.c u64 get_page_table_size(void) · line 570

Quoted 5 lines
arch/arm/mach-apple/board.c +5 -0
+		if (nvme_blk == NULL)
+			break;
+
+		dev = dev_get_parent(nvme_blk->bdev);
+		if (!device_is_compatible(dev, "apple,nvme-ans2"))

Can we somehow use ofnode_for_each_compatible_node() here ? That might simplify this code.

Quoted 5 lines

I don't really see how that would simplify things. I'm iterating over all NVMe devices here and then checking the compatible of the parent to make sure I pick the on-board one. I could do the inverse and lookup the node first and then use that to find the NVMe block device, but it will still involve a loop and several function calls.

What about:

"
struct blk_desc *desc;

ofnode_for_each_compatible_node(node, "apple,nvme-ans2") {
uclass_get_device_by_ofnode(UCLASS_NVME, node, &blk_dev); desc = dev_get_uclass_plat(blk_dev);

  for (count = 0, part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
    part_get_info(desc, part, &info);
    ...

}
}
"

I'm _not_ sure anymore whether this is actually easier though. What do you think ?

Quoted 42 lines
Quoted 34 lines
Quoted 34 lines
arch/arm/mach-apple/board.c +27 -0
+			continue;
+
+		for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
+			ret = part_get_info(nvme_blk, p, &info);
+			if (ret < 0)
+				break;
+
+			if (info.bootable & PART_EFI_SYSTEM_PARTITION) {
+				if (uuid && strcasecmp(uuid, info.uuid) == 0) {
+					part = p;
+					break;
+				}
+				if (part == 0)
+					part = p;
+			}
+		}
+
+		if (part > 0)
+			break;
+	}
+
+	if (part > 0)
+		snprintf(devpart, sizeof(devpart), "%d:%d", devnum, part);
+
+	return devpart;
+}
+
   #define KERNEL_COMP_SIZE	SZ_128M
 
   int board_late_init(void)
@@ -546,6 +602,10 @@ int board_late_init(void)
   	struct lmb lmb;
   	u32 status = 0;
 
Quoted 7 lines

arch/arm/mach-apple/board.c int board_late_init(void) · line 607

Quoted 3 lines
arch/arm/mach-apple/board.c +3 -0
+	status |= env_set("storage_interface",
+			  blk_get_uclass_name(UCLASS_NVME));
+	status |= env_set("fw_dev_part", asahi_esp_devpart());

I think env_set() returns integer (and this could be negative too), so you might want to check the return value instead of casting it to unsigned integer.

Quoted 4 lines

I'm just using the existing idiom. But maybe I should just check the return value and throw a warning instead? Not having the firmware loader available isn't fatal. It just means some of the USB ports won't work.

That's better, yeah. I don't think you can safely do bitwise operations on signed types.

#5 lore raw

PATCH 2/3 iopoll: Add readb_poll_sleep_timeout

In reply to Mark Kettenis

Add a variant of readl_poll_sleep_timeout that reads a single byte to match the readb_poll_timeout API that Linux has.

---
 include/linux/iopoll.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/iopoll.h +3 -0
index 0ee2bddaa8..89990e95e9 100644
@@ -49,6 +49,9 @@
 #define readl_poll_sleep_timeout(addr, val, cond, sleep_us, timeout_us) \
 	readx_poll_sleep_timeout(readl, addr, val, cond, sleep_us, timeout_us)
 
+#define readb_poll_sleep_timeout(addr, val, cond, sleep_us, timeout_us) \
+	readx_poll_sleep_timeout(readb, addr, val, cond, sleep_us, timeout_us)
+
 #define readx_poll_timeout(op, addr, val, cond, timeout_us) \
 	read_poll_timeout(op, val, cond, false, timeout_us, addr)
 
Signature · 1 line
2.41.0
#6 lore raw

Re: [PATCH 2/3] iopoll: Add readb_poll_sleep_timeout

Quoted 13 lines · Mark Kettenis

Add a variant of readl_poll_sleep_timeout that reads a single byte to match the readb_poll_timeout API that Linux has.

---
  include/linux/iopoll.h | 3 +++
  1 file changed, 3 insertions(+)
include/linux/iopoll.h +0 -0
index 0ee2bddaa8..89990e95e9 100644

include/linux/iopoll.h line 53

Quoted 5 lines
include/linux/iopoll.h +2 -0
@@ -49,6 +49,9 @@
  #define readl_poll_sleep_timeout(addr, val, cond, sleep_us, timeout_us) \
  	readx_poll_sleep_timeout(readl, addr, val, cond, sleep_us, timeout_us)
 
+#define readb_poll_sleep_timeout(addr, val, cond, sleep_us, timeout_us) \
+	readx_poll_sleep_timeout(readb, addr, val, cond, sleep_us, timeout_us)

readb variant should go before readl variant I think.

#7 lore raw

Re: [PATCH 2/3] iopoll: Add readb_poll_sleep_timeout

Quoted 20 lines

Date: Fri, 14 Jul 2023 23:22:00 +0200
From: Marek Vasut <[email protected]>

Quoted 16 lines · Mark Kettenis

Add a variant of readl_poll_sleep_timeout that reads a single byte to match the readb_poll_timeout API that Linux has.

---
  include/linux/iopoll.h | 3 +++
  1 file changed, 3 insertions(+)
include/linux/iopoll.h +0 -0
index 0ee2bddaa8..89990e95e9 100644
@@ -49,6 +49,9 @@
  #define readl_poll_sleep_timeout(addr, val, cond, sleep_us, timeout_us) \
  	readx_poll_sleep_timeout(readl, addr, val, cond, sleep_us, timeout_us)
 
Quoted 4 lines

include/linux/iopoll.h line 53

Quoted 2 lines
include/linux/iopoll.h +2 -0
+#define readb_poll_sleep_timeout(addr, val, cond, sleep_us, timeout_us) \
+	readx_poll_sleep_timeout(readb, addr, val, cond, sleep_us, timeout_us)

readb variant should go before readl variant I think.

That makes to order consistent, so that makes sense. I'll fix that in the next revision.

#8 lore raw

PATCH 3/3 usb: xhci-pci: Load ASMedia XHCI controller firmware

In reply to Mark Kettenis

The ASMedia XHCI controller found on some of the Apple Silicon machines needs firmware to operate. Use the file system firmware loader interface to read the firmware and load it onto the controller. This allows keyboards connected to the type-A ports on these machines to function in U-Boot.

---
 MAINTAINERS                         |   1 +
 drivers/usb/host/Kconfig            |  11 +
 drivers/usb/host/Makefile           |   1 +
 drivers/usb/host/xhci-pci-asmedia.c | 394 ++++++++++++++++++++++++++++
 drivers/usb/host/xhci-pci.c         |   9 +
 include/usb/xhci.h                  |   3 +
 6 files changed, 419 insertions(+)
 create mode 100644 drivers/usb/host/xhci-pci-asmedia.c
MAINTAINERS +1 -0
index 2477923a52..0acf04fff8 100644
@@ -124,6 +124,7 @@ F:	drivers/iommu/apple_dart.c
 F:	drivers/nvme/nvme_apple.c
 F:	drivers/pci/pcie_apple.c
 F:	drivers/pinctrl/pinctrl-apple.c
+F:	drivers/usb/host/xhci-pci-asmedia.c
 F:	drivers/watchdog/apple_wdt.c
 F:	include/configs/apple.h
 
drivers/usb/host/Kconfig +11 -0
index 1a883babf4..ad8dfabeeb 100644
@@ -95,6 +95,17 @@ config USB_XHCI_PCI
 	help
 	  Enables support for the PCI-based xHCI controller.
 
+config USB_XHCI_PCI_ASMEDIA
+	bool "Support for ASMedia xHCI USB controller"
+	depends on USB_XHCI_PCI
+	default y if ARCH_APPLE
+	imply FS_LOADER
+	help
+	  Enables support for loading firmware for the ASMedia xHCI
+	  controller.  This is needed on Apple Silicon machines that
+	  contain ASMedia xHCI controllers without the full USB
+	  firmware.
+
 config USB_XHCI_RCAR
 	bool "Renesas RCar USB 3.0 support"
 	default y
drivers/usb/host/Makefile +1 -0
index 8dad36f936..2aa2d80263 100644
@@ -52,6 +52,7 @@ obj-$(CONFIG_USB_XHCI_MTK) += xhci-mtk.o
 obj-$(CONFIG_USB_XHCI_MVEBU) += xhci-mvebu.o
 obj-$(CONFIG_USB_XHCI_OMAP) += xhci-omap.o
 obj-$(CONFIG_USB_XHCI_PCI) += xhci-pci.o
+obj-$(CONFIG_USB_XHCI_PCI_ASMEDIA) += xhci-pci-asmedia.o
 obj-$(CONFIG_USB_XHCI_RCAR) += xhci-rcar.o
 obj-$(CONFIG_USB_XHCI_STI) += dwc3-sti-glue.o
 obj-$(CONFIG_USB_XHCI_OCTEON) += dwc3-octeon-glue.o
drivers/usb/host/xhci-pci-asmedia.c new file +394 -0

394 lines not on this page · raw message

drivers/usb/host/xhci-pci.c +9 -0
index 11f1c02000..ca0f1fbcef 100644
@@ -53,6 +53,7 @@ static int xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
 static int xhci_pci_probe(struct udevice *dev)
 {
 	struct xhci_pci_plat *plat = dev_get_plat(dev);
+	struct pci_child_plat *pplat = dev_get_parent_plat(dev);
 	struct xhci_hccr *hccr;
 	struct xhci_hcor *hcor;
 	int ret;
@@ -77,6 +78,14 @@ static int xhci_pci_probe(struct udevice *dev)
 	if (ret)
 		goto err_reset;
 
+	if (IS_ENABLED(CONFIG_USB_XHCI_PCI_ASMEDIA) &&
+	    pplat->vendor == PCI_VENDOR_ID_ASMEDIA &&
+	    pplat->device == 0x2142) {
+		ret = asmedia_xhci_check_request_fw(dev, hccr, hcor);
+		if (ret)
+			goto err_reset;
+	}
+
 	ret = xhci_register(dev, hccr, hcor);
 	if (ret)
 		goto err_reset;
include/usb/xhci.h +3 -0
index 4a4ac10229..16c9311b5b 100644
@@ -1312,4 +1312,7 @@ static inline void xhci_dma_unmap(struct xhci_ctrl *ctrl, dma_addr_t addr,
 #endif
 }
 
+int asmedia_xhci_check_request_fw(struct udevice *, struct xhci_hccr *,
+				  struct xhci_hcor *);
+
 #endif /* HOST_XHCI_H_ */
Signature · 1 line
2.41.0
#9 lore raw

Re: [PATCH 3/3] usb: xhci-pci: Load ASMedia XHCI controller firmware

Quoted 132 lines · Mark Kettenis

The ASMedia XHCI controller found on some of the Apple Silicon machines needs firmware to operate. Use the file system firmware loader interface to read the firmware and load it onto the controller. This allows keyboards connected to the type-A ports on these machines to function in U-Boot.

---
  MAINTAINERS                         |   1 +
  drivers/usb/host/Kconfig            |  11 +
  drivers/usb/host/Makefile           |   1 +
  drivers/usb/host/xhci-pci-asmedia.c | 394 ++++++++++++++++++++++++++++
  drivers/usb/host/xhci-pci.c         |   9 +
  include/usb/xhci.h                  |   3 +
  6 files changed, 419 insertions(+)

create mode 100644 drivers/usb/host/xhci-pci-asmedia.c

MAINTAINERS +1 -0
index 2477923a52..0acf04fff8 100644
@@ -124,6 +124,7 @@ F:	drivers/iommu/apple_dart.c
  F:	drivers/nvme/nvme_apple.c
  F:	drivers/pci/pcie_apple.c
  F:	drivers/pinctrl/pinctrl-apple.c
+F:	drivers/usb/host/xhci-pci-asmedia.c
  F:	drivers/watchdog/apple_wdt.c
  F:	include/configs/apple.h
 
drivers/usb/host/Kconfig +11 -0
index 1a883babf4..ad8dfabeeb 100644
@@ -95,6 +95,17 @@ config USB_XHCI_PCI
  	help
  	  Enables support for the PCI-based xHCI controller.
 
+config USB_XHCI_PCI_ASMEDIA
+	bool "Support for ASMedia xHCI USB controller"
+	depends on USB_XHCI_PCI
+	default y if ARCH_APPLE
+	imply FS_LOADER
+	help
+	  Enables support for loading firmware for the ASMedia xHCI
+	  controller.  This is needed on Apple Silicon machines that
+	  contain ASMedia xHCI controllers without the full USB
+	  firmware.
+
  config USB_XHCI_RCAR
  	bool "Renesas RCar USB 3.0 support"
  	default y
drivers/usb/host/Makefile +1 -0
index 8dad36f936..2aa2d80263 100644
@@ -52,6 +52,7 @@ obj-$(CONFIG_USB_XHCI_MTK) += xhci-mtk.o
  obj-$(CONFIG_USB_XHCI_MVEBU) += xhci-mvebu.o
  obj-$(CONFIG_USB_XHCI_OMAP) += xhci-omap.o
  obj-$(CONFIG_USB_XHCI_PCI) += xhci-pci.o
+obj-$(CONFIG_USB_XHCI_PCI_ASMEDIA) += xhci-pci-asmedia.o
  obj-$(CONFIG_USB_XHCI_RCAR) += xhci-rcar.o
  obj-$(CONFIG_USB_XHCI_STI) += dwc3-sti-glue.o
  obj-$(CONFIG_USB_XHCI_OCTEON) += dwc3-octeon-glue.o
drivers/usb/host/xhci-pci-asmedia.c new file +63 -0
new file mode 100644
index 0000000000..cc1d25bde2
@@ -0,0 +1,394 @@
+// SPDX-License-Identifier: GPL-2.0 OR MIT
+/*
+ * ASMedia xHCI firmware loader
+ * Copyright (C) The Asahi Linux Contributors
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <dm/device_compat.h>
+#include <fs_loader.h>
+#include <linux/iopoll.h>
+#include <pci.h>
+#include <usb.h>
+#include <usb/xhci.h>
+
+/* Configuration space registers */
+#define ASMT_CFG_CONTROL		0xe0
+#define ASMT_CFG_CONTROL_WRITE		BIT(1)
+#define ASMT_CFG_CONTROL_READ		BIT(0)
+
+#define ASMT_CFG_SRAM_ADDR		0xe2
+
+#define ASMT_CFG_SRAM_ACCESS		0xef
+#define ASMT_CFG_SRAM_ACCESS_READ	BIT(6)
+#define ASMT_CFG_SRAM_ACCESS_ENABLE	BIT(7)
+
+#define ASMT_CFG_DATA_READ0		0xf0
+#define ASMT_CFG_DATA_READ1		0xf4
+
+#define ASMT_CFG_DATA_WRITE0		0xf8
+#define ASMT_CFG_DATA_WRITE1		0xfc
+
+#define ASMT_CMD_GET_FWVER		0x8000060840
+#define ASMT_FWVER_ROM			0x010250090816
+
+/* BAR0 registers */
+#define ASMT_REG_ADDR			0x3000
+
+#define ASMT_REG_WDATA			0x3004
+#define ASMT_REG_RDATA			0x3008
+
+#define ASMT_REG_STATUS			0x3009
+#define ASMT_REG_STATUS_BUSY		BIT(7)
+
+#define ASMT_REG_CODE_WDATA		0x3010
+#define ASMT_REG_CODE_RDATA		0x3018
+
+#define ASMT_MMIO_CPU_MISC		0x500e
+#define ASMT_MMIO_CPU_MISC_CODE_RAM_WR	BIT(0)
+
+#define ASMT_MMIO_CPU_MODE_NEXT		0x5040
+#define ASMT_MMIO_CPU_MODE_CUR		0x5041
+
+#define ASMT_MMIO_CPU_MODE_RAM		BIT(0)
+#define ASMT_MMIO_CPU_MODE_HALFSPEED	BIT(1)
+
+#define ASMT_MMIO_CPU_EXEC_CTRL		0x5042
+#define ASMT_MMIO_CPU_EXEC_CTRL_RESET	BIT(0)
+#define ASMT_MMIO_CPU_EXEC_CTRL_HALT	BIT(1)
+
+#define TIMEOUT_USEC			10000
+#define RESET_TIMEOUT_USEC		500000
+

drivers/usb/host/xhci-pci-asmedia.c line 73

Quoted 10 lines
drivers/usb/host/xhci-pci-asmedia.c new file +10 -0
+static int asmedia_mbox_tx(struct udevice *pdev, u64 data)
+{
+	u8 op;
+	int i;
+
+	for (i = 0; i < TIMEOUT_USEC; i++) {
+		dm_pci_read_config8(pdev, ASMT_CFG_CONTROL, &op);
+		if (!(op & ASMT_CFG_CONTROL_WRITE))
+			break;
+		udelay(1);

Would it be possible to use the wait_for_bit*() here ? Or read*poll*timeout() ?

Quoted 13 lines
drivers/usb/host/xhci-pci-asmedia.c new file +13 -0
+	}
+
+	if (op & ASMT_CFG_CONTROL_WRITE) {
+		dev_err(pdev,
+			"Timed out on mailbox tx: 0x%llx\n",
+			data);
+		return -ETIMEDOUT;
+	}
+
+	dm_pci_write_config32(pdev, ASMT_CFG_DATA_WRITE0, data);
+	dm_pci_write_config32(pdev, ASMT_CFG_DATA_WRITE1, data >> 32);
+	dm_pci_write_config8(pdev, ASMT_CFG_CONTROL,
+			     ASMT_CFG_CONTROL_WRITE);

[...]

Quoted 5 lines
drivers/usb/host/xhci-pci-asmedia.c new file +5 -0
+static u8 asmedia_read_reg(struct udevice *pdev, struct xhci_hccr *hccr,
+			   u16 addr)
+{
+	void __iomem *regs = (void *)hccr;
+	u8 status;

drivers/usb/host/xhci-pci-asmedia.c line 202

Quoted 10 lines
drivers/usb/host/xhci-pci-asmedia.c new file +10 -0
+	int ret;
+
+	ret = readb_poll_sleep_timeout(regs + ASMT_REG_STATUS, status,
+				       !(status & ASMT_REG_STATUS_BUSY),
+				       1000, TIMEOUT_USEC);
+
+	if (ret) {
+		dev_err(pdev,
+			"Read reg wait timed out ([%04x])\n", addr);
+		return ~0;

It might make sense to return the value as parameter, and make return value of this function into integer, so error codes can be returned that way.

[...]

#10 lore raw

Re: [PATCH 3/3] usb: xhci-pci: Load ASMedia XHCI controller firmware

Quoted 2 lines

Date: Fri, 14 Jul 2023 23:30:43 +0200
From: Marek Vasut <[email protected]>

Hi Marek,

Your suggestions are reasonable, but would make the driver deviate further from its Linux equivalent:

https://github.com/AsahiLinux/linux/blob/asahi/drivers/usb/host/xhci-pci-asmedia.c

Right now I can actually easily diff the two drivers to pick up improvements. Changing things the way you suggest would make that harder.

Cheers,

Mark

Quoted 185 lines · Mark Kettenis
Quoted 132 lines

The ASMedia XHCI controller found on some of the Apple Silicon machines needs firmware to operate. Use the file system firmware loader interface to read the firmware and load it onto the controller. This allows keyboards connected to the type-A ports on these machines to function in U-Boot.

---
  MAINTAINERS                         |   1 +
  drivers/usb/host/Kconfig            |  11 +
  drivers/usb/host/Makefile           |   1 +
  drivers/usb/host/xhci-pci-asmedia.c | 394 ++++++++++++++++++++++++++++
  drivers/usb/host/xhci-pci.c         |   9 +
  include/usb/xhci.h                  |   3 +
  6 files changed, 419 insertions(+)

create mode 100644 drivers/usb/host/xhci-pci-asmedia.c

MAINTAINERS +1 -0
index 2477923a52..0acf04fff8 100644
@@ -124,6 +124,7 @@ F:	drivers/iommu/apple_dart.c
  F:	drivers/nvme/nvme_apple.c
  F:	drivers/pci/pcie_apple.c
  F:	drivers/pinctrl/pinctrl-apple.c
+F:	drivers/usb/host/xhci-pci-asmedia.c
  F:	drivers/watchdog/apple_wdt.c
  F:	include/configs/apple.h
 
drivers/usb/host/Kconfig +11 -0
index 1a883babf4..ad8dfabeeb 100644
@@ -95,6 +95,17 @@ config USB_XHCI_PCI
  	help
  	  Enables support for the PCI-based xHCI controller.
 
+config USB_XHCI_PCI_ASMEDIA
+	bool "Support for ASMedia xHCI USB controller"
+	depends on USB_XHCI_PCI
+	default y if ARCH_APPLE
+	imply FS_LOADER
+	help
+	  Enables support for loading firmware for the ASMedia xHCI
+	  controller.  This is needed on Apple Silicon machines that
+	  contain ASMedia xHCI controllers without the full USB
+	  firmware.
+
  config USB_XHCI_RCAR
  	bool "Renesas RCar USB 3.0 support"
  	default y
drivers/usb/host/Makefile +1 -0
index 8dad36f936..2aa2d80263 100644
@@ -52,6 +52,7 @@ obj-$(CONFIG_USB_XHCI_MTK) += xhci-mtk.o
  obj-$(CONFIG_USB_XHCI_MVEBU) += xhci-mvebu.o
  obj-$(CONFIG_USB_XHCI_OMAP) += xhci-omap.o
  obj-$(CONFIG_USB_XHCI_PCI) += xhci-pci.o
+obj-$(CONFIG_USB_XHCI_PCI_ASMEDIA) += xhci-pci-asmedia.o
  obj-$(CONFIG_USB_XHCI_RCAR) += xhci-rcar.o
  obj-$(CONFIG_USB_XHCI_STI) += dwc3-sti-glue.o
  obj-$(CONFIG_USB_XHCI_OCTEON) += dwc3-octeon-glue.o
drivers/usb/host/xhci-pci-asmedia.c new file +63 -0
new file mode 100644
index 0000000000..cc1d25bde2
@@ -0,0 +1,394 @@
+// SPDX-License-Identifier: GPL-2.0 OR MIT
+/*
+ * ASMedia xHCI firmware loader
+ * Copyright (C) The Asahi Linux Contributors
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <dm/device_compat.h>
+#include <fs_loader.h>
+#include <linux/iopoll.h>
+#include <pci.h>
+#include <usb.h>
+#include <usb/xhci.h>
+
+/* Configuration space registers */
+#define ASMT_CFG_CONTROL		0xe0
+#define ASMT_CFG_CONTROL_WRITE		BIT(1)
+#define ASMT_CFG_CONTROL_READ		BIT(0)
+
+#define ASMT_CFG_SRAM_ADDR		0xe2
+
+#define ASMT_CFG_SRAM_ACCESS		0xef
+#define ASMT_CFG_SRAM_ACCESS_READ	BIT(6)
+#define ASMT_CFG_SRAM_ACCESS_ENABLE	BIT(7)
+
+#define ASMT_CFG_DATA_READ0		0xf0
+#define ASMT_CFG_DATA_READ1		0xf4
+
+#define ASMT_CFG_DATA_WRITE0		0xf8
+#define ASMT_CFG_DATA_WRITE1		0xfc
+
+#define ASMT_CMD_GET_FWVER		0x8000060840
+#define ASMT_FWVER_ROM			0x010250090816
+
+/* BAR0 registers */
+#define ASMT_REG_ADDR			0x3000
+
+#define ASMT_REG_WDATA			0x3004
+#define ASMT_REG_RDATA			0x3008
+
+#define ASMT_REG_STATUS			0x3009
+#define ASMT_REG_STATUS_BUSY		BIT(7)
+
+#define ASMT_REG_CODE_WDATA		0x3010
+#define ASMT_REG_CODE_RDATA		0x3018
+
+#define ASMT_MMIO_CPU_MISC		0x500e
+#define ASMT_MMIO_CPU_MISC_CODE_RAM_WR	BIT(0)
+
+#define ASMT_MMIO_CPU_MODE_NEXT		0x5040
+#define ASMT_MMIO_CPU_MODE_CUR		0x5041
+
+#define ASMT_MMIO_CPU_MODE_RAM		BIT(0)
+#define ASMT_MMIO_CPU_MODE_HALFSPEED	BIT(1)
+
+#define ASMT_MMIO_CPU_EXEC_CTRL		0x5042
+#define ASMT_MMIO_CPU_EXEC_CTRL_RESET	BIT(0)
+#define ASMT_MMIO_CPU_EXEC_CTRL_HALT	BIT(1)
+
+#define TIMEOUT_USEC			10000
+#define RESET_TIMEOUT_USEC		500000
+

drivers/usb/host/xhci-pci-asmedia.c line 73

Quoted 10 lines
drivers/usb/host/xhci-pci-asmedia.c new file +10 -0
+static int asmedia_mbox_tx(struct udevice *pdev, u64 data)
+{
+	u8 op;
+	int i;
+
+	for (i = 0; i < TIMEOUT_USEC; i++) {
+		dm_pci_read_config8(pdev, ASMT_CFG_CONTROL, &op);
+		if (!(op & ASMT_CFG_CONTROL_WRITE))
+			break;
+		udelay(1);

Would it be possible to use the wait_for_bit*() here ? Or read*poll*timeout() ?

Quoted 13 lines
drivers/usb/host/xhci-pci-asmedia.c new file +13 -0
+	}
+
+	if (op & ASMT_CFG_CONTROL_WRITE) {
+		dev_err(pdev,
+			"Timed out on mailbox tx: 0x%llx\n",
+			data);
+		return -ETIMEDOUT;
+	}
+
+	dm_pci_write_config32(pdev, ASMT_CFG_DATA_WRITE0, data);
+	dm_pci_write_config32(pdev, ASMT_CFG_DATA_WRITE1, data >> 32);
+	dm_pci_write_config8(pdev, ASMT_CFG_CONTROL,
+			     ASMT_CFG_CONTROL_WRITE);

[...]

Quoted 5 lines
drivers/usb/host/xhci-pci-asmedia.c new file +5 -0
+static u8 asmedia_read_reg(struct udevice *pdev, struct xhci_hccr *hccr,
+			   u16 addr)
+{
+	void __iomem *regs = (void *)hccr;
+	u8 status;

drivers/usb/host/xhci-pci-asmedia.c line 202

Quoted 10 lines
drivers/usb/host/xhci-pci-asmedia.c new file +10 -0
+	int ret;
+
+	ret = readb_poll_sleep_timeout(regs + ASMT_REG_STATUS, status,
+				       !(status & ASMT_REG_STATUS_BUSY),
+				       1000, TIMEOUT_USEC);
+
+	if (ret) {
+		dev_err(pdev,
+			"Read reg wait timed out ([%04x])\n", addr);
+		return ~0;

It might make sense to return the value as parameter, and make return value of this function into integer, so error codes can be returned that way.

[...]

#11 lore raw

Re: [PATCH 3/3] usb: xhci-pci: Load ASMedia XHCI controller firmware

Quoted 4 lines · Mark Kettenis
Quoted 2 lines

Date: Fri, 14 Jul 2023 23:30:43 +0200
From: Marek Vasut <[email protected]>

Hi Marek,

Hi,

Quoted 4 lines

Your suggestions are reasonable, but would make the driver deviate further from its Linux equivalent:

https://github.com/AsahiLinux/linux/blob/asahi/drivers/usb/host/xhci-pci-asmedia.c

Can the changes be upstreamed to Linux, if they sound reasonable ?

Quoted 3 lines

Right now I can actually easily diff the two drivers to pick up improvements. Changing things the way you suggest would make that harder.

[...]