gen_devtree: Add reserved-memory node

Add the reserved-memory node to define the memory region
the PNOR maps into if the host SPI flash LPC mailbox
is enabled.

Change-Id: Ic7c0b42fdd6d03cb7816dbdab6da8e7fbcc5a725
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/gen_devtree.pl b/gen_devtree.pl
index baa4056..bb7c9b7 100755
--- a/gen_devtree.pl
+++ b/gen_devtree.pl
@@ -17,7 +17,8 @@
     ZERO_LENGTH_PROPERTY => "zero_length_property",
     PRE_ROOT_INCLUDES => "pre-root-node",
     ROOT_INCLUDES => "root-node",
-    POST_ROOT_INCLUDES => "post-root-node"
+    POST_ROOT_INCLUDES => "post-root-node",
+    HOST_SPI_FLASH_MEM_REGION_NODE_LABEL => "flash_memory"
 };
 
 
@@ -60,6 +61,7 @@
 printNode($f, 1, "aliases", getAliases());
 printNode($f, 1, "chosen", getChosen());
 printNode($f, 1, "memory", getBmcMemory());
+printNode($f, 1, "reserved-memory", getReservedMemory());
 
 printNode($f, 1, "leds", getLEDNode());
 
@@ -207,6 +209,85 @@
 }
 
 
+#Returns a hash that represents the 'reserved-memory' node.
+#This currently only supports the memory region for the LPC
+#host spi flash mailbox.  Will look like:
+#  reserved-memory {
+#     #address-cells = <1>;
+#     #size-cells = <1>;
+#     ranges;
+#
+#     flash_memory: region@94000000 {
+#       no-map;
+#       reg = <0x94000000 0x04000000>;
+#     };
+#  };
+sub getReservedMemory
+{
+    my %memory;
+
+    if (not exists $g_configuration{"lpc-host-spi-flash-mailbox"}) {
+        return %memory;
+    }
+
+    $memory{"#address-cells"} = "<1>";
+    $memory{"#size-cells"} = "<1>";
+    $memory{ranges} = ZERO_LENGTH_PROPERTY;
+
+    #Get the sub node that contains the address range
+    my ($name, $node) = getHostSpiFlashMboxRegion();
+    $memory{$name} = { %$node };
+
+    return %memory;
+}
+
+
+#Returns a hash that represents a child node of the
+#reserved-memory node which contains the address range
+#that the host spi flash is mapped to.
+sub getHostSpiFlashMboxRegion
+{
+    my %node;
+
+    $node{"no-map"} = ZERO_LENGTH_PROPERTY;
+
+    #This node needs a label the LPC node can refer to.
+    $node{NODE_LABEL} = HOST_SPI_FLASH_MEM_REGION_NODE_LABEL;
+
+    #Get the memory region's base address and size from the config file
+    if (not exists $g_configuration{"lpc-host-spi-flash-mailbox"}
+                                   {"bmc-address-range"}{base}) {
+        die "Could not find lpc-host-spi-flash-mailbox base " .
+            "address in config file\n";
+    }
+
+    my $base = $g_configuration{"lpc-host-spi-flash-mailbox"}
+                               {"bmc-address-range"}{base};
+    #Allow 1 hex value, up to 4B
+    if ($base !~ /^0x[0-9a-fA-F]{1,8}$/) {
+        die "lpc-host-spi-flash-mailbox base address $base is invalid\n";
+    }
+
+    if (not exists $g_configuration{"lpc-host-spi-flash-mailbox"}
+                                   {"bmc-address-range"}{size}) {
+        die "Could not find lpc-host-spi-flash-mailbox address size " .
+            "in config file\n";
+    }
+
+    my $size = $g_configuration{"lpc-host-spi-flash-mailbox"}
+                               {"bmc-address-range"}{size};
+    if ($size !~ /^0x[0-9a-fA-F]{1,8}$/) {
+        die "lpc-host-spi-flash-mailbox address range size " .
+            "$size is invalid\n";
+    }
+
+    $node{"reg"} = "<$base $size>";
+    my $name = makeNodeName("region", $node{reg});
+
+    return ($name, \%node);
+}
+
+
 #Returns an array of hashes representing the device tree nodes for
 #the BMC flash.  These nodes are BMC model specific because different
 #models can have different device drivers.
@@ -989,7 +1070,14 @@
         }
     }
 
-    print $f indent($level) . "$name {\n";
+    #The node can have a label, which looks like:
+    #label : name {
+    my $label = "";
+    if (exists $vals{NODE_LABEL}) {
+        $label = $vals{NODE_LABEL} . ": ";
+    }
+
+    print $f indent($level) . $label . "$name {\n";
 
     #First print properties, then includes, then subnodes
 
@@ -998,6 +1086,7 @@
 
         next if ($v eq "COMMENT");
         next if ($v eq "DTSI_INCLUDE");
+        next if ($v eq "NODE_LABEL");
         next if (ref($vals{$v}) eq "HASH");
         next if (ref($vals{$v}) eq "ARRAY");