devicetree: Add support for fixed-partitions

Add DT_NODE_BY_FIXED_PARTITION_LABEL that given a "label" in any
fixed-partitions map will return the node_id for that partition node.

Add DT_NODE_HAS_FIXED_PARTITION_LABEL that will test if a given
fixed-partitions "label" is valid.

Add DT_FIXED_PARTITION_ID that will return an unique ordinal value for
the partition give a node_id to the partition.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
diff --git a/scripts/dts/gen_defines.py b/scripts/dts/gen_defines.py
index 87f5234..94f385a 100755
--- a/scripts/dts/gen_defines.py
+++ b/scripts/dts/gen_defines.py
@@ -29,6 +29,7 @@
 
 def main():
     global header_file
+    global flash_area_num
 
     args = parse_args()
 
@@ -41,6 +42,8 @@
     except edtlib.EDTError as e:
         sys.exit(f"devicetree error: {e}")
 
+    flash_area_num = 0
+
     # Save merged DTS source, as a debugging aid
     with open(args.dts_out, "w", encoding="utf-8") as f:
         print(edt.dts_source, file=f)
@@ -254,6 +257,8 @@
     # data cannot otherwise be obtained from write_vanilla_props()
     # results
 
+    global flash_area_num
+
     out_comment("Special property macros:")
 
     # Macros that are special to the devicetree specification
@@ -262,6 +267,10 @@
     write_compatibles(node)
     write_status(node)
 
+    if node.parent and "fixed-partitions" in node.parent.compats:
+        macro = f"{node.z_path_id}_PARTITION_ID"
+        out_dt_define(macro, flash_area_num)
+        flash_area_num += 1
 
 def write_regs(node):
     # reg property: edtlib knows the right #address-cells and
@@ -586,6 +595,18 @@
             " ".join(f"fn({edt.compat2nodes[compat].index(node)})"
                      for node in okay_nodes)
 
+    for compat, nodes in edt.compat2nodes.items():
+        for node in nodes:
+            if compat == "fixed-partitions":
+                for child in node.children.values():
+                    if "label" in child.props:
+                        label = child.props["label"].val
+                        macro = f"COMPAT_{str2ident(compat)}_LABEL_{str2ident(label)}"
+                        val = f"DT_{child.z_path_id}"
+
+                        out_dt_define(macro, val)
+                        out_dt_define(macro + "_EXISTS", 1)
+
     out_comment('Macros for compatibles with status "okay" nodes\n')
     for compat, okay_nodes in edt.compat2okay.items():
         if okay_nodes: