Skip to content

Commit ee30513

Browse files
[nrf fromlist] cmake: extensions: add NAME parameter to DT registers
Upstream PR zephyrproject-rtos/zephyr#38182 Allow register address and size access by name. Example devicetree fragment: / { foo@deadbeef { reg = <0xdeadbeef 0x1000>, <0xfeedface 0x2000>; reg-names = "bar", "baz"; }; }; Example usage: dt_reg_addr(bar_addr PATH "/foo@deadbeef" NAME bar) dt_reg_size(bar_size PATH "/foo@deadbeef" NAME bar) dt_reg_addr(baz_addr PATH "/foo@deadbeef" NAME baz) dt_reg_size(baz_size PATH "/foo@deadbeef" NAME baz) Results: - bar_addr is 0xdeadbeef - bar_size is 0x1000 - baz_addr is 0xfeedface - baz_size is 0x2000 Signed-off-by: Martí Bolívar <[email protected]> (cherry picked from commit 2a2ffe8)
1 parent 30433f9 commit ee30513

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

cmake/extensions.cmake

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,24 +2517,26 @@ function(dt_num_regs var)
25172517
endfunction()
25182518

25192519
# Usage:
2520-
# dt_reg_addr(<var> PATH <path> [INDEX <idx>])
2520+
# dt_reg_addr(<var> PATH <path> [INDEX <idx>] [NAME <name>])
25212521
#
2522-
# Get the base address of the register block at index <idx>.
2523-
# If <idx> is omitted, then the value at index 0 will be returned.
2522+
# Get the base address of the register block at index <idx>, or with
2523+
# name <name>. If <idx> and <name> are both omitted, the value at
2524+
# index 0 will be returned. Do not give both <idx> and <name>.
25242525
#
25252526
# The value will be returned in the <var> parameter.
25262527
#
25272528
# Results can be:
25282529
# - The base address of the register block
25292530
# - <var> will be undefined if node does not exists or does not have a register
2530-
# block at the requested index.
2531+
# block at the requested index or with the requested name
25312532
#
25322533
# <var> : Return variable where the address value will be stored
25332534
# PATH <path> : Node path
2534-
# INDEX <idx> : Index number
2535+
# INDEX <idx> : Register block index number
2536+
# NAME <name> : Register block name
25352537
function(dt_reg_addr var)
25362538
set(req_single_args "PATH")
2537-
set(single_args "INDEX")
2539+
set(single_args "INDEX;NAME")
25382540
cmake_parse_arguments(DT_REG "" "${req_single_args};${single_args}" "" ${ARGN})
25392541

25402542
if(${ARGV0} IN_LIST req_single_args)
@@ -2549,8 +2551,16 @@ function(dt_reg_addr var)
25492551
endif()
25502552
endforeach()
25512553

2552-
if(NOT DEFINED DT_REG_INDEX)
2554+
if(DEFINED DT_REG_INDEX AND DEFINED DT_REG_NAME)
2555+
message(FATAL_ERROR "dt_reg_addr(${ARGV0} ...) given both INDEX and NAME")
2556+
elseif(NOT DEFINED DT_REG_INDEX AND NOT DEFINED DT_REG_NAME)
25532557
set(DT_REG_INDEX 0)
2558+
elseif(DEFINED DT_REG_NAME)
2559+
_dt_reg_get_index(DT_REG_INDEX "${DT_REG_PATH}" "${DT_REG_NAME}")
2560+
if(DT_REG_INDEX EQUAL "-1")
2561+
set(${var} PARENT_SCOPE)
2562+
return()
2563+
endif()
25542564
endif()
25552565

25562566
get_target_property(${var}_list devicetree_target "DT_REG|${DT_REG_PATH}|ADDR")
@@ -2565,19 +2575,21 @@ function(dt_reg_addr var)
25652575
endfunction()
25662576

25672577
# Usage:
2568-
# dt_reg_size(<var> PATH <path> [INDEX <idx>])
2578+
# dt_reg_size(<var> PATH <path> [INDEX <idx>] [NAME <name>])
25692579
#
2570-
# Get the size of the register block at index <idx>.
2571-
# If INDEX is omitted, then the value at index 0 will be returned.
2580+
# Get the size of the register block at index <idx>, or with
2581+
# name <name>. If <idx> and <name> are both omitted, the value at
2582+
# index 0 will be returned. Do not give both <idx> and <name>.
25722583
#
25732584
# The value will be returned in the <value> parameter.
25742585
#
25752586
# <var> : Return variable where the size value will be stored
25762587
# PATH <path> : Node path
2577-
# INDEX <idx> : Index number
2588+
# INDEX <idx> : Register block index number
2589+
# NAME <name> : Register block name
25782590
function(dt_reg_size var)
25792591
set(req_single_args "PATH")
2580-
set(single_args "INDEX")
2592+
set(single_args "INDEX;NAME")
25812593
cmake_parse_arguments(DT_REG "" "${req_single_args};${single_args}" "" ${ARGN})
25822594

25832595
if(${ARGV0} IN_LIST req_single_args)
@@ -2592,8 +2604,16 @@ function(dt_reg_size var)
25922604
endif()
25932605
endforeach()
25942606

2595-
if(NOT DEFINED DT_REG_INDEX)
2607+
if(DEFINED DT_REG_INDEX AND DEFINED DT_REG_NAME)
2608+
message(FATAL_ERROR "dt_reg_size(${ARGV0} ...) given both INDEX and NAME")
2609+
elseif(NOT DEFINED DT_REG_INDEX AND NOT DEFINED DT_REG_NAME)
25962610
set(DT_REG_INDEX 0)
2611+
elseif(DEFINED DT_REG_NAME)
2612+
_dt_reg_get_index(DT_REG_INDEX "${DT_REG_PATH}" "${DT_REG_NAME}")
2613+
if(DT_REG_INDEX EQUAL "-1")
2614+
set(${var} PARENT_SCOPE)
2615+
return()
2616+
endif()
25972617
endif()
25982618

25992619
get_target_property(${var}_list devicetree_target "DT_REG|${DT_REG_PATH}|SIZE")
@@ -2607,6 +2627,17 @@ function(dt_reg_size var)
26072627
set(${var} ${${var}} PARENT_SCOPE)
26082628
endfunction()
26092629

2630+
# Internal helper for dt_reg_addr/dt_reg_size; not meant to be used directly
2631+
function(_dt_reg_get_index var path name)
2632+
dt_prop(reg_names PATH "${path}" PROPERTY "reg-names")
2633+
if(NOT DEFINED reg_names)
2634+
set(index "-1")
2635+
else()
2636+
list(FIND reg_names "${name}" index)
2637+
endif()
2638+
set("${var}" "${index}" PARENT_SCOPE)
2639+
endfunction()
2640+
26102641
# Usage:
26112642
# dt_has_chosen(<var> PROPERTY <prop>)
26122643
#

0 commit comments

Comments
 (0)