From e571c885653830bc643e27f1e920018a1241d6e6 Mon Sep 17 00:00:00 2001 From: Peter Bigot Date: Sat, 31 Oct 2020 15:43:34 -0500 Subject: [PATCH] devicetree: flash-partitions: expose helper macros Make generally available the macro that provides the flash device in which a particular partition can be found. Signed-off-by: Peter Bigot --- include/devicetree.h | 21 ++++++++++++++++++++ include/devicetree/fixed-partitions.h | 11 ++++++++++ subsys/storage/flash_map/flash_map_default.c | 17 ++++------------ 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/include/devicetree.h b/include/devicetree.h index 75574202ec3..87943d9a443 100644 --- a/include/devicetree.h +++ b/include/devicetree.h @@ -317,6 +317,27 @@ */ #define DT_PARENT(node_id) UTIL_CAT(node_id, _PARENT) +/** + * @brief Get a node identifier for a grandparent node + * + * Example devicetree fragment: + * + * gparent: grandparent-node { + * parent: parent-node { + * child: child-node { ... } + * }; + * }; + * + * The following are equivalent ways to get the same node identifier: + * + * DT_GPARENT(DT_NODELABEL(child)) + * DT_PARENT(DT_PARENT(DT_NODELABEL(child)) + * + * @param node_id node identifier + * @return a node identifier for the node's parent's parent + */ +#define DT_GPARENT(node_id) DT_PARENT(DT_PARENT(node_id)) + /** * @brief Get a node identifier for a child node * diff --git a/include/devicetree/fixed-partitions.h b/include/devicetree/fixed-partitions.h index 0c1370fae74..4eeb31055c5 100644 --- a/include/devicetree/fixed-partitions.h +++ b/include/devicetree/fixed-partitions.h @@ -68,6 +68,17 @@ extern "C" { */ #define DT_FIXED_PARTITION_ID(node_id) DT_CAT(node_id, _PARTITION_ID) +/** + * @brief Get the node identifier of the flash device for a partition + * @param node_id node identifier for a fixed-partitions child node + * @return the node identifier of the memory technology device that + * contains the fixed-partitions node. + */ +#define DT_MTD_FROM_FIXED_PARTITION(node_id) \ + COND_CODE_1(DT_NODE_HAS_COMPAT(DT_GPARENT(node_id), soc_nv_flash), \ + (DT_PARENT(DT_GPARENT(node_id))), \ + (DT_GPARENT(node_id))) + /** * @} */ diff --git a/subsys/storage/flash_map/flash_map_default.c b/subsys/storage/flash_map/flash_map_default.c index f627d6c5c4b..4f2830571af 100644 --- a/subsys/storage/flash_map/flash_map_default.c +++ b/subsys/storage/flash_map/flash_map_default.c @@ -10,19 +10,10 @@ #include #include -/* Get the grand parent of a node */ -#define GPARENT(node_id) DT_PARENT(DT_PARENT(node_id)) - -/* return great-grandparent label if 'soc-nv-flash' else grandparent label */ -#define DT_FLASH_DEV_FROM_PARTITION(part) \ - DT_LABEL(COND_CODE_1(DT_NODE_HAS_COMPAT(GPARENT(part), soc_nv_flash), \ - (DT_PARENT(GPARENT(part))), \ - (GPARENT(part)))) - -#define FLASH_AREA_FOO(part) \ - {.fa_id = DT_FIXED_PARTITION_ID(part), \ - .fa_off = DT_REG_ADDR(part), \ - .fa_dev_name = DT_FLASH_DEV_FROM_PARTITION(part), \ +#define FLASH_AREA_FOO(part) \ + {.fa_id = DT_FIXED_PARTITION_ID(part), \ + .fa_off = DT_REG_ADDR(part), \ + .fa_dev_name = DT_LABEL(DT_MTD_FROM_FIXED_PARTITION(part)), \ .fa_size = DT_REG_SIZE(part),}, #define FOREACH_PARTITION(n) DT_FOREACH_CHILD(DT_DRV_INST(n), FLASH_AREA_FOO)