From patchwork Wed Apr 8 09:35:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Brugger X-Patchwork-Id: 237393 List-Id: U-Boot discussion From: matthias.bgg at kernel.org (matthias.bgg at kernel.org) Date: Wed, 8 Apr 2020 11:35:28 +0200 Subject: [PATCH 2/2] dm: core: Walk the tree to find address- and size-cells properties In-Reply-To: <20200408093528.24276-1-matthias.bgg@kernel.org> References: <20200408093528.24276-1-matthias.bgg@kernel.org> Message-ID: <20200408093528.24276-3-matthias.bgg@kernel.org> From: Matthias Brugger Walk the tree when reading size-cells or address-cells properties. Reported-by: Robin Randhawa Signed-off-by: Matthias Brugger Reviewed-by: Simon Glass --- drivers/core/ofnode.c | 8 ++++---- include/dm/ofnode.h | 36 ++++++++++++++++++++++++++++++++++++ include/dm/read.h | 6 ++---- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c index 96a5dd20bd..5f23826b70 100644 --- a/drivers/core/ofnode.c +++ b/drivers/core/ofnode.c @@ -697,16 +697,16 @@ int ofnode_read_addr_cells(ofnode node) { if (ofnode_is_np(node)) return of_n_addr_cells(ofnode_to_np(node)); - else /* NOTE: this call should walk up the parent stack */ - return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node)); + else + return __ofnode_read_address_cells(node); } int ofnode_read_size_cells(ofnode node) { if (ofnode_is_np(node)) return of_n_size_cells(ofnode_to_np(node)); - else /* NOTE: this call should walk up the parent stack */ - return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node)); + else + return __ofnode_read_size_cells(node); } int ofnode_read_simple_addr_cells(ofnode node) diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h index b5a50e8849..c6b768763d 100644 --- a/include/dm/ofnode.h +++ b/include/dm/ofnode.h @@ -660,6 +660,24 @@ int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device); */ int ofnode_read_addr_cells(ofnode node); +static inline int __ofnode_read_address_cells(ofnode node) +{ + /* NOTE: this call walks up the parent stack */ + int val = -FDT_ERR_NOTFOUND; + ofnode nd = node; + + while (val == -FDT_ERR_NOTFOUND) { + val = fdt_cells(gd->fdt_blob, ofnode_to_offset(nd), + "#address-cells"); + nd = ofnode_get_parent(nd); + } + + if (val == -FDT_ERR_NOTFOUND) + return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; + else + return val; +} + /** * ofnode_read_size_cells() - Get the number of size cells for a node * @@ -671,6 +689,24 @@ int ofnode_read_addr_cells(ofnode node); */ int ofnode_read_size_cells(ofnode node); +static inline int __ofnode_read_size_cells(ofnode node) +{ + /* NOTE: this call walks up the parent stack */ + int val = -FDT_ERR_NOTFOUND; + ofnode nd = node; + + while (val == -FDT_ERR_NOTFOUND) { + val = fdt_cells(gd->fdt_blob, ofnode_to_offset(nd), + "#size-cells"); + nd = ofnode_get_parent(nd); + } + + if (val == -FDT_ERR_NOTFOUND) + return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; + else + return val; +} + /** * ofnode_read_simple_addr_cells() - Get the address cells property in a node * diff --git a/include/dm/read.h b/include/dm/read.h index da8c7f25e7..0302c7bffb 100644 --- a/include/dm/read.h +++ b/include/dm/read.h @@ -789,14 +789,12 @@ static inline int dev_count_phandle_with_args(const struct udevice *dev, static inline int dev_read_addr_cells(const struct udevice *dev) { - /* NOTE: this call should walk up the parent stack */ - return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev)); + return __ofnode_read_address_cells(dev_ofnode(dev)); } static inline int dev_read_size_cells(const struct udevice *dev) { - /* NOTE: this call should walk up the parent stack */ - return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev)); + return __ofnode_read_size_cells(dev_ofnode(dev)); } static inline int dev_read_simple_addr_cells(const struct udevice *dev)