Skip to content

Commit 8278cb7

Browse files
ytcooderobherring
authored andcommitted
of/fdt: Consolidate duplicate code into helper functions
Currently, there are many pieces of nearly identical code scattered across different places. Consolidate the duplicate code into helper functions to improve maintainability and reduce the likelihood of errors. Signed-off-by: Yuntao Wang <yuntao.wang@linux.dev> Link: https://patch.msgid.link/20251115134753.179931-2-yuntao.wang@linux.dev Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
1 parent a5387fb commit 8278cb7

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

drivers/of/fdt.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,47 @@ const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
625625
return fdt_getprop(initial_boot_params, node, name, size);
626626
}
627627

628+
const __be32 *__init of_flat_dt_get_addr_size_prop(unsigned long node,
629+
const char *name,
630+
int *entries)
631+
{
632+
const __be32 *prop;
633+
int len, elen = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
634+
635+
prop = of_get_flat_dt_prop(node, name, &len);
636+
if (!prop || len % elen) {
637+
*entries = 0;
638+
return NULL;
639+
}
640+
641+
*entries = len / elen;
642+
return prop;
643+
}
644+
645+
bool __init of_flat_dt_get_addr_size(unsigned long node, const char *name,
646+
u64 *addr, u64 *size)
647+
{
648+
const __be32 *prop;
649+
int entries;
650+
651+
prop = of_flat_dt_get_addr_size_prop(node, name, &entries);
652+
if (!prop || entries != 1)
653+
return false;
654+
655+
of_flat_dt_read_addr_size(prop, 0, addr, size);
656+
return true;
657+
}
658+
659+
void __init of_flat_dt_read_addr_size(const __be32 *prop, int entry_index,
660+
u64 *addr, u64 *size)
661+
{
662+
int entry_cells = dt_root_addr_cells + dt_root_size_cells;
663+
prop += entry_cells * entry_index;
664+
665+
*addr = dt_mem_next_cell(dt_root_addr_cells, &prop);
666+
*size = dt_mem_next_cell(dt_root_size_cells, &prop);
667+
}
668+
628669
/**
629670
* of_fdt_is_compatible - Return true if given node from the given blob has
630671
* compat in its compatible list

include/linux/of_fdt.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ extern int of_get_flat_dt_subnode_by_name(unsigned long node,
5555
const char *uname);
5656
extern const void *of_get_flat_dt_prop(unsigned long node, const char *name,
5757
int *size);
58+
59+
extern const __be32 *of_flat_dt_get_addr_size_prop(unsigned long node,
60+
const char *name,
61+
int *entries);
62+
extern bool of_flat_dt_get_addr_size(unsigned long node, const char *name,
63+
u64 *addr, u64 *size);
64+
extern void of_flat_dt_read_addr_size(const __be32 *prop, int entry_index,
65+
u64 *addr, u64 *size);
66+
5867
extern int of_flat_dt_is_compatible(unsigned long node, const char *name);
5968
extern unsigned long of_get_flat_dt_root(void);
6069
extern uint32_t of_get_flat_dt_phandle(unsigned long node);

0 commit comments

Comments
 (0)