This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit b8e7bed3b0c0c903aa454562df6ef2119ad90c27 Author: zhaoyiming5 <[email protected]> AuthorDate: Mon Jul 13 15:43:19 2026 +0800 can: ctucanfd: pass channel_id explicitly to ctucanfd_register() - ctucanfd_pci.h: add uint32_t id parameter to ctucanfd_register(). - ctucanfd_pci.c: drop the g_ctucanfd_count++ in ctucanfd_register() and use the id from the caller for /dev/canN and the netdev name. g_ctucanfd_count is now only used by the PCI probe. - fdt_ctucanfd.c: read can-id from FDT (or fall back to discovery order) and pass it to ctucanfd_register(). Only register nodes whose cpu-affinity property matches up_cpu_index(); nodes without the property are registered on every CPU. Signed-off-by: zhaoyiming5 <[email protected]> --- drivers/can/ctucanfd_pci.c | 12 ++++--- drivers/devicetree/fdt_ctucanfd.c | 70 +++++++++++++++++++++++++++++++++++++-- include/nuttx/can/ctucanfd_pci.h | 5 ++- 3 files changed, 80 insertions(+), 7 deletions(-) diff --git a/drivers/can/ctucanfd_pci.c b/drivers/can/ctucanfd_pci.c index 09bb6e78d99..dcf6769cfdd 100644 --- a/drivers/can/ctucanfd_pci.c +++ b/drivers/can/ctucanfd_pci.c @@ -30,6 +30,8 @@ #include <assert.h> #include <nuttx/debug.h> #include <errno.h> +#include <inttypes.h> +#include <stdbool.h> #include <stdio.h> #include <nuttx/arch.h> @@ -261,7 +263,7 @@ static struct pci_driver_s g_ctucanfd_drv = }; #endif -#ifdef CONFIG_CAN_CTUCANFD_CHARDEV +#if defined(CONFIG_PCI) && defined(CONFIG_CAN_CTUCANFD_CHARDEV) static uint8_t g_ctucanfd_count = 0; #endif @@ -1924,19 +1926,19 @@ int pci_ctucanfd_init(void) * Input Parameters: * base - MMIO base address of the CTU CAN FD controller * irq - IRQ number + * id - CAN channel number assigned by the caller * * Returned Value: * Zero on success; a negated errno on failure * *****************************************************************************/ -int ctucanfd_register(uintptr_t base, int irq) +int ctucanfd_register(uintptr_t base, int irq, uint32_t id) { FAR struct ctucanfd_driver_s *priv; FAR struct ctucanfd_can_s *dev; #ifdef CONFIG_CAN_CTUCANFD_CHARDEV char devpath[16]; - uint8_t canid = g_ctucanfd_count++; #endif int ret; @@ -1976,7 +1978,7 @@ int ctucanfd_register(uintptr_t base, int irq) /* Register the CAN interface */ #ifdef CONFIG_CAN_CTUCANFD_CHARDEV - snprintf(devpath, sizeof(devpath), "/dev/can%d", canid); + snprintf(devpath, sizeof(devpath), "/dev/can%" PRIu32, id); dev->dev.cd_ops = &g_ctucanfd_can_ops; dev->dev.cd_priv = dev; @@ -1994,6 +1996,8 @@ int ctucanfd_register(uintptr_t base, int irq) dev->dev.quota[NETPKT_RX] = CTUCANFD_RX_QUOTA; dev->dev.ops = &g_ctucanfd_net_ops; + snprintf(dev->dev.netdev.d_ifname, IFNAMSIZ, "can%" PRIu32, id); + ret = netdev_lower_register(&dev->dev, NET_LL_CAN); if (ret < 0) { diff --git a/drivers/devicetree/fdt_ctucanfd.c b/drivers/devicetree/fdt_ctucanfd.c index ab7046e2c3f..a010dd84325 100644 --- a/drivers/devicetree/fdt_ctucanfd.c +++ b/drivers/devicetree/fdt_ctucanfd.c @@ -24,12 +24,46 @@ #include <debug.h> #include <errno.h> +#include <stdbool.h> +#include <nuttx/arch.h> #include <nuttx/can/ctucanfd_pci.h> #include <nuttx/fdt.h> #include <libfdt.h> +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ctucanfd_enable_on_this_cpu + * + * Description: + * Return true if the FDT node's cpu-affinity property matches the + * current CPU. Nodes without the property are enabled on all + * cores (no affinity restriction); a present-but-mismatched property + * filters the node to its owning core only. The former is logged as + * info (a node that will be enabled by every core), while the + * latter is normal multi-core filtering noise and stays silent. + * + ****************************************************************************/ + +static bool ctucanfd_enable_on_this_cpu(FAR const void *fdt, int offset) +{ + int len; + FAR const fdt32_t *prop = fdt_getprop(fdt, offset, "cpu-affinity", &len); + + if (prop == NULL || len < (int)sizeof(fdt32_t)) + { + caninfo("ctucanfd: FDT node at offset %d has no cpu-affinity " + "property\n", offset); + return true; + } + + return fdt32_to_cpu(*prop) == up_cpu_index(); +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -41,6 +75,17 @@ * Scan the device tree for nodes with compatible "ctu,ctucanfd" and * register each via ctucanfd_register() (SysBus / MMIO path, no PCI). * + * The CAN channel number assigned to each node comes from the FDT + * can-id property when present (set by QEMU for ctucan-sysbus, + * supports non-contiguous / arbitrary channel numbering). When the + * property is absent, the FDT discovery order (next) is used + * instead, preserving the legacy contiguous can0-8 numbering + * (compatible with the PCI path and older QEMU builds that do not + * emit can-id). + * + * Each node's cpu-affinity property is checked against + * up_cpu_index(); only matching nodes are registered. + * * Input Parameters: * fdt - Device tree handle * irqbase - Interrupt base number (added to FDT-supplied SPI index) @@ -56,8 +101,10 @@ int fdt_ctucanfd_devices_register(FAR const void *fdt, int irqbase) int offset = -1; int irqnum; int ret; + uint32_t next; + uint32_t id; - for (; ; ) + for (next = 0; ; next++) { offset = fdt_node_offset_by_compatible(fdt, offset, "ctu,ctucanfd"); if (offset == -FDT_ERR_NOTFOUND) @@ -65,6 +112,25 @@ int fdt_ctucanfd_devices_register(FAR const void *fdt, int irqbase) break; } + if (!ctucanfd_enable_on_this_cpu(fdt, offset)) + { + continue; + } + + /* Read the CAN channel number from the FDT can-id property. + * When the property is present (set by QEMU for ctucan-sysbus), + * it provides the global hardware channel number, supporting + * non-contiguous numbering. When absent, fall back to the FDT + * discovery order index (compatible with PCI path and older + * QEMU builds that do not emit can-id). + */ + + ret = fdt_load_prop_u32(fdt, offset, "can-id", 0, &id); + if (ret != OK) + { + id = next; + } + addr = fdt_get_reg_base(fdt, offset, 0); irqnum = fdt_get_irq(fdt, offset, 1, irqbase); if (addr == 0 || irqnum < 0) @@ -72,7 +138,7 @@ int fdt_ctucanfd_devices_register(FAR const void *fdt, int irqbase) return -EINVAL; } - ret = ctucanfd_register(addr, irqnum); + ret = ctucanfd_register(addr, irqnum, id); if (ret < 0 && ret != -ENODEV) { return ret; diff --git a/include/nuttx/can/ctucanfd_pci.h b/include/nuttx/can/ctucanfd_pci.h index 04d4f5aa3d1..20e52c59149 100644 --- a/include/nuttx/can/ctucanfd_pci.h +++ b/include/nuttx/can/ctucanfd_pci.h @@ -61,13 +61,16 @@ int pci_ctucanfd_init(void); * Input Parameters: * base - MMIO base address of the CTU CAN FD controller * irq - IRQ number + * id - CAN channel number assigned by the caller (e.g. from + * the FDT can-id property or discovery order). Passed + * explicitly so the caller controls numbering. * * Returned Value: * Zero on success; a negated errno on failure * ****************************************************************************/ -int ctucanfd_register(uintptr_t base, int irq); +int ctucanfd_register(uintptr_t base, int irq, uint32_t id); #ifdef __cplusplus }
