From 29c7278edc8bd47f0263143cf85568c8c0e6e61e Mon Sep 17 00:00:00 2001 From: xieyangrun Date: Tue, 15 Nov 2022 14:30:58 +0800 Subject: [PATCH] driver/fdt] add parse reg array, get address and size by index. --- components/drivers/fdt/inc/dtb_node.h | 1 + components/drivers/fdt/src/dtb_base.c | 37 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/components/drivers/fdt/inc/dtb_node.h b/components/drivers/fdt/inc/dtb_node.h index b42e101dee..b783dfdf8c 100644 --- a/components/drivers/fdt/inc/dtb_node.h +++ b/components/drivers/fdt/inc/dtb_node.h @@ -329,6 +329,7 @@ struct dtb_node *dtb_node_get_parent(const struct dtb_node *node); const char *dtb_node_get_name(const struct dtb_node *node); struct dtb_node *dtb_node_get_by_phandle(uint32_t phandle); int dtb_node_read_size(const struct dtb_node *node, const char *propname); +int dtb_node_get_addr_and_size_by_index(const struct dtb_node *node, int index, size_t *addr, size_t *size); size_t dtb_node_get_addr_index(const struct dtb_node *node, int index); size_t dtb_node_get_addr(const struct dtb_node *node); int dtb_node_stringlist_search(const struct dtb_node *node, const char *property, diff --git a/components/drivers/fdt/src/dtb_base.c b/components/drivers/fdt/src/dtb_base.c index d3378a0151..86b23a8e1c 100644 --- a/components/drivers/fdt/src/dtb_base.c +++ b/components/drivers/fdt/src/dtb_base.c @@ -206,6 +206,43 @@ int dtb_node_read_size(const struct dtb_node *node, const char *propname) return -EINVAL; } +int dtb_node_get_addr_and_size_by_index(const struct dtb_node *node, int index, size_t *addr, size_t *size) +{ + const uint32_t *prop; + int psize; + int onesize, na, ns; + + na = dtb_node_n_addr_cells(node); + ns = dtb_node_n_size_cells(node); + + prop = dtb_node_get_dtb_node_property_value(node, "reg", &psize); + if (prop == NULL) + { + return -1; + } + + psize /= 4; + onesize = na + ns; + + if (psize >= (index + 1) * onesize) + { + prop += index * onesize; + + if (addr) + { + *addr = dtb_node_read_number(prop, na); + } + if (size) + { + *size = dtb_node_read_number(prop + na, ns); + } + + return 0; + } + + return -1; +} + size_t dtb_node_get_addr_index(const struct dtb_node *node, int index) { int na;