diff options
| author | Rbb666 <[email protected]> | 2026-07-31 15:07:36 +0800 |
|---|---|---|
| committer | Rbb666 <[email protected]> | 2026-08-12 12:54:50 +0800 |
| commit | 6b4f34204ed2da80b7c37b912612c65cebc30ce3 (patch) | |
| tree | 9451c2d5b1b08de290ecc4eddb5a3aed3487e273 /components | |
| parent | 9a12b2a7b991df7b72a7f6d0d9cc74a3ba5f7c93 (diff) | |
feat(dm): support OFW on Cortex-M targets
Add the ARM NVIC PIC driver, no-MMU I/O mapping, built-in FDT support, and Cortex-M CPU hooks required by the native RT-Thread device model.
Diffstat (limited to 'components')
| -rw-r--r-- | components/drivers/core/power.c | 2 | ||||
| -rwxr-xr-x | components/drivers/include/drivers/pic.h | 4 | ||||
| -rwxr-xr-x | components/drivers/ofw/Kconfig | 2 | ||||
| -rw-r--r-- | components/drivers/ofw/fdt.c | 10 | ||||
| -rwxr-xr-x | components/drivers/pic/Kconfig | 7 | ||||
| -rw-r--r-- | components/drivers/pic/SConscript | 3 | ||||
| -rw-r--r-- | components/drivers/pic/pic-arm-nvic.c | 297 | ||||
| -rw-r--r-- | components/drivers/pin/dev_pin_dm.c | 2 | ||||
| -rw-r--r-- | components/mm/SConscript | 4 | ||||
| -rw-r--r-- | components/mm/ioremap-nommu.c | 42 |
10 files changed, 369 insertions, 4 deletions
diff --git a/components/drivers/core/power.c b/components/drivers/core/power.c index 038e37c507..43eaad4b25 100644 --- a/components/drivers/core/power.c +++ b/components/drivers/core/power.c @@ -243,7 +243,7 @@ void rt_hw_cpu_shutdown(void) } MSH_CMD_EXPORT_ALIAS(rt_hw_cpu_shutdown, shutdown, shutdown machine); -void rt_hw_cpu_reset(void) +rt_weak void rt_hw_cpu_reset(void) { register rt_ubase_t level; diff --git a/components/drivers/include/drivers/pic.h b/components/drivers/include/drivers/pic.h index c624d249d1..71bdc50dde 100755 --- a/components/drivers/include/drivers/pic.h +++ b/components/drivers/include/drivers/pic.h @@ -214,4 +214,8 @@ rt_err_t rt_pic_irq_parent_set_triger_mode(struct rt_pic_irq *pirq, rt_uint32_t rt_err_t rt_pic_init(void); +#ifdef RT_PIC_ARM_NVIC +void rt_hw_nvic_dispatch(void); +#endif + #endif /* __PIC_H__ */ diff --git a/components/drivers/ofw/Kconfig b/components/drivers/ofw/Kconfig index 5d6f12bf9f..735488aa48 100755 --- a/components/drivers/ofw/Kconfig +++ b/components/drivers/ofw/Kconfig @@ -3,7 +3,7 @@ menuconfig RT_USING_OFW select RT_USING_ADT select RT_USING_ADT_REF select RT_USING_ADT_BITMAP - select RT_USING_MEMBLOCK + select RT_USING_MEMBLOCK if ARCH_MM_MMU depends on RT_USING_DM default n diff --git a/components/drivers/ofw/fdt.c b/components/drivers/ofw/fdt.c index dce20f8c2e..23709be301 100644 --- a/components/drivers/ofw/fdt.c +++ b/components/drivers/ofw/fdt.c @@ -15,7 +15,9 @@ #include <drivers/ofw_raw.h> #include <drivers/core/dm.h> +#ifdef RT_USING_MEMBLOCK #include <mm_memblock.h> +#endif #define DBG_TAG "rtdm.ofw" #define DBG_LVL DBG_INFO @@ -196,6 +198,7 @@ rt_err_t rt_fdt_scan_root(void) return err; } +#ifdef RT_USING_MEMBLOCK static rt_err_t fdt_reserved_mem_check_root(int nodeoffset) { rt_err_t err = RT_EOK; @@ -380,9 +383,11 @@ static rt_err_t fdt_scan_memory(void) return err; } +#endif /* RT_USING_MEMBLOCK */ rt_err_t rt_fdt_scan_memory(void) { +#ifdef RT_USING_MEMBLOCK rt_err_t err = -RT_EEMPTY; if (_fdt) @@ -391,6 +396,9 @@ rt_err_t rt_fdt_scan_memory(void) } return err; +#else + return -RT_ENOSYS; +#endif } static rt_err_t fdt_scan_initrd(rt_uint64_t *ranges, const char *name, const char *oem) @@ -477,7 +485,9 @@ static rt_err_t fdt_scan_initrd(rt_uint64_t *ranges, const char *name, const cha if (!err) { +#ifdef RT_USING_MEMBLOCK rt_memblock_reserve_memory("initrd", ranges[0], ranges[1], MEMBLOCK_NONE); +#endif } } else if (!ranges) diff --git a/components/drivers/pic/Kconfig b/components/drivers/pic/Kconfig index e2c05685a6..4ea164b50d 100755 --- a/components/drivers/pic/Kconfig +++ b/components/drivers/pic/Kconfig @@ -24,6 +24,13 @@ config RT_PIC_ARM_GIC select RT_USING_OFW default n +config RT_PIC_ARM_NVIC + bool "ARM Cortex-M NVIC" + depends on RT_USING_PIC + depends on ARCH_ARM_CORTEX_M + select RT_USING_OFW + default n + config RT_PIC_ARM_GIC_V2M bool "ARM GIC V2M" if RT_PIC_ARM_GIC && RT_PCI_MSI depends on RT_USING_OFW diff --git a/components/drivers/pic/SConscript b/components/drivers/pic/SConscript index 53cb66e1ff..4951866b39 100644 --- a/components/drivers/pic/SConscript +++ b/components/drivers/pic/SConscript @@ -10,6 +10,9 @@ CPPPATH = [cwd + '/../include'] src = ['pic.c', 'pic_rthw.c'] +if GetDepend(['RT_PIC_ARM_NVIC']): + src += ['pic-arm-nvic.c'] + if GetDepend(['RT_PIC_ARM_GIC']) or GetDepend(['RT_PIC_ARM_GIC_V3']): src += ['pic-gic-common.c'] diff --git a/components/drivers/pic/pic-arm-nvic.c b/components/drivers/pic/pic-arm-nvic.c new file mode 100644 index 0000000000..87fe598c8c --- /dev/null +++ b/components/drivers/pic/pic-arm-nvic.c @@ -0,0 +1,297 @@ +/* + * Copyright (c) 2006-2026, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-07-29 RTT the first version + */ + +#include <rtthread.h> + +#define DBG_TAG "pic.nvic" +#define DBG_LVL DBG_INFO +#include <rtdbg.h> + +#include <drivers/pic.h> +#include <drivers/ofw.h> +#include <drivers/ofw_io.h> + +#define NVIC_ICTR_ADDR 0xe000e004UL +#define NVIC_ISER_OFFSET 0x000UL +#define NVIC_ICER_OFFSET 0x080UL +#define NVIC_ISPR_OFFSET 0x100UL +#define NVIC_ICPR_OFFSET 0x180UL +#define NVIC_IABR_OFFSET 0x200UL +#define NVIC_IPR_OFFSET 0x300UL +#define NVIC_REG_INDEX(irq) ((rt_uint32_t)(irq) >> 5) +#define NVIC_REG_MASK(irq) RT_BIT((rt_uint32_t)(irq) & 0x1f) + +struct arm_nvic +{ + struct rt_pic parent; + volatile rt_uint8_t *base; + rt_uint32_t irq_nr; + rt_uint8_t priority_bits; +}; + +rt_inline void nvic_sync(void) +{ + __asm volatile ("dsb 0xf\n\tisb 0xf" ::: "memory"); +} + +static struct arm_nvic _nvic; + +static volatile rt_uint32_t *nvic_reg32(struct arm_nvic *nvic, + rt_ubase_t offset, int hwirq) +{ + return (volatile rt_uint32_t *)(nvic->base + offset) + NVIC_REG_INDEX(hwirq); +} + +static void nvic_irq_mask(struct rt_pic_irq *pirq) +{ + struct arm_nvic *nvic = pirq->pic->priv_data; + + *nvic_reg32(nvic, NVIC_ICER_OFFSET, pirq->hwirq) = NVIC_REG_MASK(pirq->hwirq); + nvic_sync(); +} + +static void nvic_irq_unmask(struct rt_pic_irq *pirq) +{ + struct arm_nvic *nvic = pirq->pic->priv_data; + + *nvic_reg32(nvic, NVIC_ISER_OFFSET, pirq->hwirq) = NVIC_REG_MASK(pirq->hwirq); + nvic_sync(); +} + +static rt_err_t nvic_irq_set_priority(struct rt_pic_irq *pirq, rt_uint32_t priority) +{ + struct arm_nvic *nvic = pirq->pic->priv_data; + rt_uint32_t priority_max = RT_BIT(nvic->priority_bits) - 1; + + if (priority > priority_max) + { + return -RT_EINVAL; + } + + *(volatile rt_uint8_t *)(nvic->base + NVIC_IPR_OFFSET + pirq->hwirq) = + priority << (8 - nvic->priority_bits); + pirq->priority = priority; + + return RT_EOK; +} + +static rt_err_t nvic_irq_set_state(struct rt_pic *pic, int hwirq, + int type, rt_bool_t state) +{ + struct arm_nvic *nvic = pic->priv_data; + volatile rt_uint32_t *reg; + + if (hwirq < 0 || hwirq >= nvic->irq_nr) + { + return -RT_EINVAL; + } + + switch (type) + { + case RT_IRQ_STATE_PENDING: + reg = nvic_reg32(nvic, state ? NVIC_ISPR_OFFSET : NVIC_ICPR_OFFSET, hwirq); + *reg = NVIC_REG_MASK(hwirq); + break; + + case RT_IRQ_STATE_MASKED: + reg = nvic_reg32(nvic, state ? NVIC_ICER_OFFSET : NVIC_ISER_OFFSET, hwirq); + *reg = NVIC_REG_MASK(hwirq); + break; + + default: + return -RT_ENOSYS; + } + + nvic_sync(); + + return RT_EOK; +} + +static rt_err_t nvic_irq_get_state(struct rt_pic *pic, int hwirq, + int type, rt_bool_t *out_state) +{ + rt_ubase_t offset; + struct arm_nvic *nvic = pic->priv_data; + + if (!out_state || hwirq < 0 || hwirq >= nvic->irq_nr) + { + return -RT_EINVAL; + } + + switch (type) + { + case RT_IRQ_STATE_PENDING: + offset = NVIC_ISPR_OFFSET; + break; + + case RT_IRQ_STATE_ACTIVE: + offset = NVIC_IABR_OFFSET; + break; + + case RT_IRQ_STATE_MASKED: + offset = NVIC_ISER_OFFSET; + break; + + default: + return -RT_ENOSYS; + } + + *out_state = !!(*nvic_reg32(nvic, offset, hwirq) & NVIC_REG_MASK(hwirq)); + if (type == RT_IRQ_STATE_MASKED) + { + *out_state = !*out_state; + } + + return RT_EOK; +} + +static int nvic_irq_map(struct rt_pic *pic, int hwirq, rt_uint32_t priority) +{ + int irq; + struct arm_nvic *nvic = pic->priv_data; + struct rt_pic_irq *pirq; + + if (hwirq < 0 || hwirq >= nvic->irq_nr) + { + return -RT_EINVAL; + } + + pirq = rt_pic_find_irq(pic, hwirq); + irq = rt_pic_config_irq(pic, hwirq, hwirq); + + if (irq >= 0 && nvic_irq_set_priority(pirq, priority)) + { + irq = -RT_EINVAL; + } + + return irq; +} + +static rt_err_t nvic_irq_parse(struct rt_pic *pic, + struct rt_ofw_cell_args *args, struct rt_pic_irq *out_pirq) +{ + if (args->args_count != 2 || args->args[0] >= pic->irq_nr) + { + return -RT_EINVAL; + } + + out_pirq->hwirq = args->args[0]; + out_pirq->mode = args->args[1]; + + return RT_EOK; +} + +static const struct rt_pic_ops nvic_ops = +{ + .name = "NVIC", + .irq_enable = nvic_irq_unmask, + .irq_disable = nvic_irq_mask, + .irq_mask = nvic_irq_mask, + .irq_unmask = nvic_irq_unmask, + .irq_set_priority = nvic_irq_set_priority, + .irq_set_state = nvic_irq_set_state, + .irq_get_state = nvic_irq_get_state, + .irq_map = nvic_irq_map, + .irq_parse = nvic_irq_parse, +}; + +void rt_hw_nvic_dispatch(void) +{ + rt_uint32_t exception; + + __asm volatile ("mrs %0, ipsr" : "=r" (exception)); + + if (exception >= 16) + { + int hwirq = exception - 16; + + if (hwirq < _nvic.irq_nr) + { + struct rt_pic_irq *pirq = rt_pic_find_irq(&_nvic.parent, hwirq); + + if (pirq->irq >= 0) + { + rt_interrupt_enter(); + rt_pic_handle_isr(pirq); + rt_interrupt_leave(); + } + } + } +} + +static rt_err_t nvic_ofw_init(struct rt_ofw_node *np, + const struct rt_ofw_node_id *id) +{ + rt_err_t err; + rt_uint32_t value; + rt_uint64_t reg, reg_size; + rt_uint32_t hw_irq_nr; + + RT_UNUSED(id); + + if (_nvic.parent.ops) + { + return -RT_EBUSY; + } + + if ((err = rt_ofw_get_address(np, 0, ®, ®_size))) + { + return err; + } + + if (reg_size < NVIC_IPR_OFFSET + 1) + { + return -RT_EINVAL; + } + + hw_irq_nr = ((*(volatile rt_uint32_t *)NVIC_ICTR_ADDR & 0xf) + 1) * 32; + if (!rt_ofw_prop_read_u32(np, "arm,num-irqs", &value)) + { + if (!value || value > hw_irq_nr) + { + return -RT_EINVAL; + } + hw_irq_nr = value; + } + + _nvic.priority_bits = 4; + if (!rt_ofw_prop_read_u32(np, "arm,num-irq-priority-bits", &value)) + { + if (!value || value > 8) + { + return -RT_EINVAL; + } + _nvic.priority_bits = value; + } + + _nvic.base = (volatile rt_uint8_t *)(rt_ubase_t)reg; + _nvic.irq_nr = hw_irq_nr; + _nvic.parent.priv_data = &_nvic; + _nvic.parent.ops = &nvic_ops; + + if ((err = rt_pic_linear_irq(&_nvic.parent, _nvic.irq_nr))) + { + _nvic.parent.ops = RT_NULL; + return err; + } + + rt_ofw_data(np) = &_nvic.parent; + rt_pic_user_extends(&_nvic.parent); + + return RT_EOK; +} + +static const struct rt_ofw_node_id nvic_ofw_ids[] = +{ + { .compatible = "arm,v7m-nvic" }, + { .compatible = "arm,v8m-nvic" }, + { /* sentinel */ } +}; +RT_PIC_OFW_DECLARE(arm_nvic, nvic_ofw_ids, nvic_ofw_init); diff --git a/components/drivers/pin/dev_pin_dm.c b/components/drivers/pin/dev_pin_dm.c index cd9b9acd1a..bd2a27836e 100644 --- a/components/drivers/pin/dev_pin_dm.c +++ b/components/drivers/pin/dev_pin_dm.c @@ -122,7 +122,7 @@ static rt_err_t pin_api_detach_irq(struct rt_device *device, rt_base_t pin) } else { - return gpio->ops->pin_detach_irq(&gpio->parent, pin); + return gpio->ops->pin_detach_irq(&gpio->parent, pin_index); } } diff --git a/components/mm/SConscript b/components/mm/SConscript index 5b259fee10..6bc146c3c5 100644 --- a/components/mm/SConscript +++ b/components/mm/SConscript @@ -2,9 +2,9 @@ import os from building import * group = [] +cwd = GetCurrentDir() if GetDepend('ARCH_ARM_CORTEX_A') or GetDepend('ARCH_ARMV8') or GetDepend('ARCH_RISCV64'): - cwd = GetCurrentDir() src = Glob('*.c') + Glob('*_gcc.S') if not GetDepend('RT_USING_MEMBLOCK'): SrcRemove(src, ['mm_memblock.c']) @@ -12,6 +12,8 @@ if GetDepend('ARCH_ARM_CORTEX_A') or GetDepend('ARCH_ARMV8') or GetDepend('ARCH_ CPPPATH = [cwd] group = DefineGroup('Memm', src, depend = ['ARCH_MM_MMU'], CPPPATH = CPPPATH) +elif GetDepend('RT_USING_OFW'): + group = DefineGroup('Memm', ['ioremap-nommu.c'], depend = ['RT_USING_OFW'], CPPPATH = [cwd]) group = group + SConscript(os.path.join('utest', 'SConscript')) diff --git a/components/mm/ioremap-nommu.c b/components/mm/ioremap-nommu.c new file mode 100644 index 0000000000..d58b457dc4 --- /dev/null +++ b/components/mm/ioremap-nommu.c @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2006-2026, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include <rtthread.h> + +#include <ioremap.h> + +void *rt_ioremap_start; +size_t rt_ioremap_size; + +void *rt_ioremap_early(void *paddr, size_t size) +{ + return size ? paddr : RT_NULL; +} + +void *rt_ioremap(void *paddr, size_t size) +{ + return rt_ioremap_early(paddr, size); +} + +void *rt_ioremap_nocache(void *paddr, size_t size) +{ + return rt_ioremap_early(paddr, size); +} + +void *rt_ioremap_cached(void *paddr, size_t size) +{ + return rt_ioremap_early(paddr, size); +} + +void *rt_ioremap_wt(void *paddr, size_t size) +{ + return rt_ioremap_early(paddr, size); +} + +void rt_iounmap(volatile void *addr) +{ + RT_UNUSED(addr); +} |
