arm64/qemu-bringup: use common fdt function to register virtio and pci

Use common implementation is better.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
This commit is contained in:
Bowen Wang
2024-11-07 00:48:41 +08:00
committed by Alan C. Assis
parent 9f351523a7
commit b65d2544d2
@@ -31,15 +31,9 @@
#include <syslog.h>
#include <nuttx/fs/fs.h>
#include <nuttx/virtio/virtio-mmio.h>
#include <nuttx/fdt.h>
#include <nuttx/pci/pci_ecam.h>
#include <nuttx/rpmsg/rpmsg_port.h>
#ifdef CONFIG_LIBC_FDT
# include <libfdt.h>
#endif
#include "qemu-armv8a.h"
/****************************************************************************
@@ -50,103 +44,12 @@
#define QEMU_SPI_IRQ_BASE 32
#endif
#define FDT_PCI_TYPE_IO 0x01000000
#define FDT_PCI_TYPE_MEM32 0x02000000
#define FDT_PCI_TYPE_MEM64 0x03000000
#define FDT_PCI_TYPE_MASK 0x03000000
#define FDT_PCI_PREFETCH 0x40000000
/****************************************************************************
* Private Functions
****************************************************************************/
#if defined(CONFIG_LIBC_FDT) && defined(CONFIG_DEVICE_TREE)
/****************************************************************************
* Name: register_pci_host_from_fdt
****************************************************************************/
#ifdef CONFIG_PCI
static void register_pci_host_from_fdt(const void *fdt)
{
struct pci_resource_s prefetch;
struct pci_resource_s cfg;
struct pci_resource_s mem;
struct pci_resource_s io;
const fdt32_t *ranges;
int offset;
/* #address-size must be 3
* defined in the PCI Bus Binding to IEEE Std 1275-1994 :
* Bit#
*
* phys.hi cell: npt000ss bbbbbbbb dddddfff rrrrrrrr
* phys.mid cell: hhhhhhhh hhhhhhhh hhhhhhhh hhhhhhhh
* phys.lo cell: llllllll llllllll llllllll llllllll
*/
const int na = 3;
/* #size-cells must be 2 */
const int ns = 2;
int rlen;
int pna;
memset(&prefetch, 0, sizeof(prefetch));
memset(&cfg, 0, sizeof(cfg));
memset(&mem, 0, sizeof(mem));
memset(&io, 0, sizeof(io));
offset = fdt_node_offset_by_compatible(fdt, -1,
"pci-host-ecam-generic");
if (offset < 0)
{
return;
}
/* Get the reg address, 64 or 32 */
cfg.start = fdt_get_reg_base(fdt, offset, 0);
cfg.end = cfg.start + fdt_get_reg_size(fdt, offset);
/* Get the ranges address */
ranges = fdt_getprop(fdt, offset, "ranges", &rlen);
if (ranges < 0)
{
return;
}
pna = fdt_get_parent_address_cells(fdt, offset);
for (rlen /= 4; (rlen -= na + pna + ns) >= 0; ranges += na + pna + ns)
{
uint32_t type = fdt32_ld(ranges);
if ((type & FDT_PCI_TYPE_MASK) == FDT_PCI_TYPE_IO)
{
io.start = fdt_ld_by_cells(ranges + na, pna);
io.end = io.start + fdt_ld_by_cells(ranges + na + pna, ns);
}
else if ((type & FDT_PCI_PREFETCH) == FDT_PCI_PREFETCH ||
(type & FDT_PCI_TYPE_MASK) == FDT_PCI_TYPE_MEM64)
{
prefetch.start = fdt_ld_by_cells(ranges + na, pna);
prefetch.end = prefetch.start +
fdt_ld_by_cells(ranges + na + pna, ns);
}
else
{
mem.start = fdt_ld_by_cells(ranges + na, pna);
mem.end = mem.start + fdt_ld_by_cells(ranges + na + pna, ns);
}
}
pci_ecam_register(&cfg, &io, &mem, NULL);
}
#endif
/****************************************************************************
* Name: register_devices_from_fdt
****************************************************************************/
@@ -162,7 +65,6 @@ static void register_devices_from_fdt(void)
}
#ifdef CONFIG_DRIVERS_VIRTIO_MMIO
ret = fdt_virtio_mmio_devices_register(fdt, QEMU_SPI_IRQ_BASE);
if (ret < 0)
{
@@ -172,7 +74,11 @@ static void register_devices_from_fdt(void)
#endif
#ifdef CONFIG_PCI
register_pci_host_from_fdt(fdt);
ret = fdt_pci_ecam_register(fdt);
if (ret < 0)
{
syslog(LOG_ERR, "fdt_pci_ecam_register failed, ret=%d\n", ret);
}
#endif
UNUSED(ret);