u-boot/common/spl/spl_dfu.c
Hrushikesh Salunke cde77583cf spl: Add support for Device Firmware Upgrade (DFU) over PCIe
Introduces support for Device Firmware Upgrade (DFU) over PCIe in
U-Boot. Traditionally, the DFU protocol is used over USB, where a
device enters DFU mode and allows a host to upload firmware or binary
images directly via the USB interface. This is a widely adopted and
convenient method for updating firmware.

In the context of Texas Instruments (TI) SoCs, PCIe can be used as a
boot interface in a manner that differs from the conventional
"PCIe Boot" process, which typically refers to booting an OS or
firmware image from an NVMe SSD or other PCIe-attached storage devices.
Instead, TI SoCs can be configured as a PCIe Endpoint, allowing a
connected PCIe Root Complex (host) to transfer images directly into the
device’s memory over the PCIe bus for boot purposes. This mechanism is
analogous to DFU over USB, but leverages the high-speed PCIe link and
does not depend on traditional storage devices.

By extending the DFU framework in U-Boot to support PCIe, it will be
possible to flash images over PCIe. While this implementation is
motivated by TI SoC use cases, the framework is generic and can be
adopted by everyone for platforms that support PCIe Endpoint mode.
Platforms with hardware support for PCIe-based memory loading can use
this to implement PCIe as a boot mode, as well as to enable flashing
and recovery scenarios similar to DFU over USB.

In summary, enable support for:
- DFU-style flashing of firmware/images over PCIe, analogous to existing
USB DFU workflows
- PCIe as a boot mode where a host can load images directly into device
memory using DFU over PCIe

Signed-off-by: Hrushikesh Salunke <h-salunke@ti.com>
Reviewed-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
Link: https://lore.kernel.org/r/20251023080922.3527052-1-h-salunke@ti.com
Signed-off-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
2025-11-06 10:17:05 +01:00

148 lines
3.2 KiB
C

// SPDX-License-Identifier: GPL-2.0+
/*
* (C) Copyright 2016
* Texas Instruments, <www.ti.com>
*
* Ravi B <ravibabu@ti.com>
*/
#include <env.h>
#include <spl.h>
#include <linux/compiler.h>
#include <errno.h>
#include <watchdog.h>
#include <console.h>
#include <g_dnl.h>
#include <usb.h>
#include <dfu.h>
#include <linux/printk.h>
#include <pci_ep.h>
#include <dm/uclass.h>
#include <cpu_func.h>
#include <linux/io.h>
/*
* Macros define size of magic word and boot phase string
* in bytes.
*/
#define MAGIC_WORD_SIZE 4
#define BOOT_PHASE_STRING_SIZE 63
static int run_dfu(int usb_index, char *interface, char *devstring)
{
int ret;
ret = dfu_init_env_entities(interface, devstring);
if (ret) {
dfu_free_entities();
goto exit;
}
run_usb_dnl_gadget(usb_index, "usb_dnl_dfu");
exit:
dfu_free_entities();
return ret;
}
#ifdef CONFIG_SPL_PCI_DFU
static int dfu_over_pcie(void)
{
u32 offset, magic_word;
volatile void *addr;
struct udevice *dev;
struct pci_bar bar;
struct pci_ep_header hdr;
uint fn = 0;
int ret;
char *bootphase;
uclass_get_device_by_seq(UCLASS_PCI_EP, 0, &dev);
if (!dev) {
pr_err("Failed to get pci ep device\n");
return -ENODEV;
}
hdr.deviceid = CONFIG_SPL_PCI_DFU_DEVICE_ID;
hdr.vendorid = CONFIG_SPL_PCI_DFU_VENDOR_ID;
hdr.baseclass_code = PCI_BASE_CLASS_MEMORY;
hdr.subclass_code = PCI_CLASS_MEMORY_RAM;
ret = pci_ep_write_header(dev, fn, &hdr);
if (ret) {
pr_err("Failed to write header: %d\n", ret);
return ret;
}
bar.barno = BAR_0;
bar.phys_addr = (dma_addr_t)CONFIG_SPL_PCI_DFU_SPL_LOAD_FIT_ADDRESS;
bar.flags = PCI_BASE_ADDRESS_SPACE_MEMORY |
PCI_BASE_ADDRESS_MEM_TYPE_32 |
PCI_BASE_ADDRESS_MEM_PREFETCH;
bar.size = CONFIG_SPL_PCI_DFU_BAR_SIZE;
ret = pci_ep_set_bar(dev, fn, &bar);
if (ret) {
pr_err("Failed to set bar: %d\n", ret);
return ret;
}
ret = pci_ep_start(dev);
if (ret) {
pr_err("Failed to start ep: %d\n", ret);
return ret;
}
addr = (void *)CONFIG_SPL_PCI_DFU_SPL_LOAD_FIT_ADDRESS;
offset = CONFIG_SPL_PCI_DFU_BAR_SIZE - MAGIC_WORD_SIZE;
if (sizeof(CONFIG_SPL_PCI_DFU_BOOT_PHASE) > BOOT_PHASE_STRING_SIZE) {
pr_err("Not copying boot phase. String too long\n");
} else {
bootphase = (char *)(addr + CONFIG_SPL_PCI_DFU_BAR_SIZE -
(BOOT_PHASE_STRING_SIZE + MAGIC_WORD_SIZE + 1));
strlcpy(bootphase, CONFIG_SPL_PCI_DFU_BOOT_PHASE,
sizeof(CONFIG_SPL_PCI_DFU_BOOT_PHASE) + 1);
}
addr = addr + offset;
magic_word = CONFIG_SPL_PCI_DFU_MAGIC_WORD;
(*(int *)addr) = 0;
flush_dcache_all();
for (;;) {
if (*(int *)addr == magic_word)
break;
invalidate_dcache_all();
}
return 0;
}
#endif
int spl_dfu_cmd(int usbctrl, char *dfu_alt_info, char *interface, char *devstr)
{
char *str_env;
int ret;
#ifdef CONFIG_SPL_PCI_DFU
if (spl_boot_device() == BOOT_DEVICE_PCIE)
return dfu_over_pcie();
#endif
/* set default environment */
env_set_default(NULL, 0);
str_env = env_get(dfu_alt_info);
if (!str_env) {
pr_err("\"%s\" env variable not defined!\n", dfu_alt_info);
return -EINVAL;
}
ret = env_set("dfu_alt_info", str_env);
if (ret) {
pr_err("unable to set env variable \"dfu_alt_info\"!\n");
return -EINVAL;
}
/* invoke dfu command */
return run_dfu(usbctrl, interface, devstr);
}