diff options
Diffstat (limited to 'lib/utils/fdt/fdt_helper.c')
-rw-r--r-- | lib/utils/fdt/fdt_helper.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/utils/fdt/fdt_helper.c b/lib/utils/fdt/fdt_helper.c index 58f8951..887d6ed 100644 --- a/lib/utils/fdt/fdt_helper.c +++ b/lib/utils/fdt/fdt_helper.c @@ -9,10 +9,12 @@ #include <libfdt.h> #include <sbi/riscv_asm.h> #include <sbi/sbi_console.h> +#include <sbi/sbi_hartmask.h> #include <sbi/sbi_platform.h> #include <sbi/sbi_scratch.h> #include <sbi_utils/fdt/fdt_helper.h> #include <sbi_utils/irqchip/plic.h> +#include <sbi_utils/sys/clint.h> #define DEFAULT_UART_FREQ 0 #define DEFAULT_UART_BAUD 115200 @@ -296,6 +298,73 @@ int fdt_parse_plic(void *fdt, struct plic_data *plic, const char *compat) return fdt_parse_plic_node(fdt, nodeoffset, plic); } +int fdt_parse_clint_node(void *fdt, int nodeoffset, bool for_timer, + struct clint_data *clint) +{ + const fdt32_t *val; + unsigned long reg_addr, reg_size; + int i, rc, count, cpu_offset, cpu_intc_offset; + u32 phandle, hwirq, hartid, first_hartid, last_hartid; + u32 match_hwirq = (for_timer) ? IRQ_M_TIMER : IRQ_M_SOFT; + + if (nodeoffset < 0 || !clint || !fdt) + return SBI_ENODEV; + + rc = fdt_get_node_addr_size(fdt, nodeoffset, ®_addr, ®_size); + if (rc < 0 || !reg_addr || !reg_size) + return SBI_ENODEV; + clint->addr = reg_addr; + + val = fdt_getprop(fdt, nodeoffset, "interrupts-extended", &count); + if (!val || count < sizeof(fdt32_t)) + return SBI_EINVAL; + count = count / sizeof(fdt32_t); + + first_hartid = -1U; + last_hartid = 0; + clint->hart_count = 0; + for (i = 0; i < count; i += 2) { + phandle = fdt32_to_cpu(val[i]); + hwirq = fdt32_to_cpu(val[i + 1]); + + cpu_intc_offset = fdt_node_offset_by_phandle(fdt, phandle); + if (cpu_intc_offset < 0) + continue; + + cpu_offset = fdt_parent_offset(fdt, cpu_intc_offset); + if (cpu_intc_offset < 0) + continue; + + rc = fdt_parse_hart_id(fdt, cpu_offset, &hartid); + if (rc) + continue; + + if (SBI_HARTMASK_MAX_BITS <= hartid) + continue; + + if (match_hwirq == hwirq) { + if (hartid < first_hartid) + first_hartid = hartid; + if (hartid > last_hartid) + last_hartid = hartid; + clint->hart_count++; + } + } + + if ((last_hartid < first_hartid) || first_hartid == -1U) + return SBI_ENODEV; + + clint->first_hartid = first_hartid; + count = last_hartid - first_hartid + 1; + if (clint->hart_count < count) + clint->hart_count = count; + + /* TODO: We should figure-out CLINT has_64bit_mmio from DT node */ + clint->has_64bit_mmio = TRUE; + + return 0; +} + int fdt_parse_compat_addr(void *fdt, unsigned long *addr, const char *compatible) { |