Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | |
Matt Spinler | 5cb5f4e | 2017-01-31 14:17:09 -0600 | [diff] [blame] | 3 | #Generates an OpenBMC device tree syntax file from the machine |
| 4 | #readable workbook. It relies on the fact that the dts include |
| 5 | #file for the BMC chip itself contains the BMC chip specific |
| 6 | #data, so this can just generate the system specific data. |
| 7 | #It also makes use of a YAML configuration file to contain |
| 8 | #settings that are outside the scope of the MRW. |
| 9 | # |
| 10 | #This doesn't attempt to support every possible type of device |
| 11 | #tree node from the start. Support may need to be added as newer |
| 12 | #systems come along that make use of different features. |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 13 | |
| 14 | use strict; |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 15 | use warnings; |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 16 | use XML::Simple; |
| 17 | use mrw::Targets; |
Matt Spinler | e2bf439 | 2017-01-30 13:16:28 -0600 | [diff] [blame] | 18 | use mrw::Util; |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 19 | use Getopt::Long; |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 20 | use YAML::Tiny qw(LoadFile); |
| 21 | use Scalar::Util qw(looks_like_number); |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 22 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 23 | use constant { |
| 24 | VERSION => "/dts-v1/;", |
| 25 | ZERO_LENGTH_PROPERTY => "zero_length_property", |
| 26 | PRE_ROOT_INCLUDES => "pre-root-node", |
| 27 | ROOT_INCLUDES => "root-node", |
Matt Spinler | 6d39125 | 2017-01-31 13:46:06 -0600 | [diff] [blame] | 28 | POST_ROOT_INCLUDES => "post-root-node", |
| 29 | HOST_SPI_FLASH_MEM_REGION_NODE_LABEL => "flash_memory" |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 30 | }; |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 31 | |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 32 | |
| 33 | my $serverwizFile; |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 34 | my $configFile; |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 35 | my $outputFile; |
| 36 | my $debug; |
| 37 | |
| 38 | GetOptions("x=s" => \$serverwizFile, |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 39 | "y=s" => \$configFile, |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 40 | "o=s" => \$outputFile, |
| 41 | "d" => \$debug) |
| 42 | or printUsage(); |
| 43 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 44 | if ((not defined $serverwizFile) || (not defined $outputFile) || |
| 45 | (not defined $configFile)) { |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 46 | printUsage(); |
| 47 | } |
| 48 | |
Matt Spinler | 908e182 | 2017-01-31 14:02:58 -0600 | [diff] [blame] | 49 | my $g_pnorNodeName = undef; |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 50 | my %g_configuration = %{ LoadFile($configFile) }; |
| 51 | |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 52 | my $g_targetObj = Targets->new; |
| 53 | $g_targetObj->loadXML($serverwizFile); |
| 54 | |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 55 | my ($g_bmc, $g_bmcModel, $g_bmcMfgr, $g_systemName); |
| 56 | setGlobalAttributes(); |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 57 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 58 | my $g_i2cBusAdjust = 0; |
| 59 | getI2CBusAdjust(); |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 60 | |
| 61 | open (my $f, ">$outputFile") or die "Could not open $outputFile\n"; |
| 62 | |
| 63 | printVersion($f); |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 64 | printIncludes($f, PRE_ROOT_INCLUDES); |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 65 | printRootNodeStart($f); |
| 66 | |
| 67 | printPropertyList($f, 1, "model", getSystemBMCModel()); |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 68 | printPropertyList($f, 1, "compatible", getBMCCompatibles()); |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 69 | |
Matt Spinler | 23d47c2 | 2016-10-04 12:31:21 -0500 | [diff] [blame] | 70 | printNode($f, 1, "aliases", getAliases()); |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 71 | printNode($f, 1, "chosen", getChosen()); |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 72 | printNode($f, 1, "memory", getBmcMemory()); |
Matt Spinler | 6d39125 | 2017-01-31 13:46:06 -0600 | [diff] [blame] | 73 | printNode($f, 1, "reserved-memory", getReservedMemory()); |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 74 | |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 75 | printNode($f, 1, "leds", getLEDNode()); |
| 76 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 77 | printIncludes($f, ROOT_INCLUDES); |
| 78 | |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 79 | printRootNodeEnd($f, 0); |
| 80 | |
Matt Spinler | 96f8f24 | 2016-11-28 16:26:57 -0600 | [diff] [blame] | 81 | printNodes($f, 0, getBMCFlashNodes()); |
Matt Spinler | 96f8f24 | 2016-11-28 16:26:57 -0600 | [diff] [blame] | 82 | printNodes($f, 0, getOtherFlashNodes()); |
| 83 | |
Matt Spinler | 908e182 | 2017-01-31 14:02:58 -0600 | [diff] [blame] | 84 | printNode($f, 0, "lpc_ctrl", getLPCNode()); |
| 85 | printNode($f, 0, "mbox", getMBoxNode()); |
| 86 | |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 87 | printNodes($f, 0, getUARTNodes()); |
Matt Spinler | 41dcb62 | 2017-01-31 14:55:19 -0600 | [diff] [blame] | 88 | printNodes($f, 0, getMacNodes()); |
| 89 | |
| 90 | printNodes($f, 0, getI2CNodes()); |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 91 | printNodes($f, 0, getVuartNodes()); |
| 92 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 93 | printIncludes($f, POST_ROOT_INCLUDES); |
| 94 | |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 95 | close $f; |
| 96 | exit 0; |
| 97 | |
| 98 | |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 99 | #Finds the values for these globals: |
| 100 | # $g_bmc, $g_bmcModel, $g_bmcMfgr, $g_systemName |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 101 | sub setGlobalAttributes |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 102 | { |
Matt Spinler | e2bf439 | 2017-01-30 13:16:28 -0600 | [diff] [blame] | 103 | $g_bmc = Util::getBMCTarget($g_targetObj); |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 104 | |
| 105 | if ($g_targetObj->isBadAttribute($g_bmc, "MODEL")) { |
| 106 | die "The MODEL attribute on $g_bmc is missing or empty.\n"; |
| 107 | } |
| 108 | $g_bmcModel = $g_targetObj->getAttribute($g_bmc, "MODEL"); |
| 109 | |
| 110 | if ($g_targetObj->isBadAttribute($g_bmc, "MANUFACTURER")) { |
| 111 | die "The MANUFACTURER attribute on $g_bmc is missing or empty.\n"; |
| 112 | } |
| 113 | $g_bmcMfgr = $g_targetObj->getAttribute($g_bmc, "MANUFACTURER"); |
| 114 | |
| 115 | $g_systemName = $g_targetObj->getSystemName(); |
| 116 | if (length($g_systemName) == 0) { |
| 117 | die "The SYSTEM_NAME attribute is not set on the system target.\n"; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
Matt Spinler | 23d47c2 | 2016-10-04 12:31:21 -0500 | [diff] [blame] | 122 | #Returns a hash that represents the 'aliases' node. |
| 123 | #Will look like: |
| 124 | # aliases { |
| 125 | # name1 = &val1; |
| 126 | # name2 = &val2; |
| 127 | # ... |
| 128 | # } |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 129 | sub getAliases |
Matt Spinler | 23d47c2 | 2016-10-04 12:31:21 -0500 | [diff] [blame] | 130 | { |
| 131 | my %aliases; |
Matt Spinler | 23d47c2 | 2016-10-04 12:31:21 -0500 | [diff] [blame] | 132 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 133 | #Get the info from the config file |
Matt Spinler | 23d47c2 | 2016-10-04 12:31:21 -0500 | [diff] [blame] | 134 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 135 | if ((not exists $g_configuration{aliases}) || |
| 136 | (keys %{$g_configuration{aliases}} == 0)) { |
| 137 | print "WARNING: Missing or empty 'aliases' section in config file.\n"; |
| 138 | return %aliases; |
| 139 | } |
| 140 | %aliases = %{ $g_configuration{aliases} }; |
| 141 | |
| 142 | #add a & reference if one is missing |
| 143 | foreach my $a (keys %aliases) { |
| 144 | if (($aliases{$a} !~ /^&/) && ($aliases{$a} !~ /^\(ref\)/)) { |
| 145 | $aliases{$a} = "(ref)$aliases{$a}"; |
Matt Spinler | 23d47c2 | 2016-10-04 12:31:21 -0500 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | return %aliases; |
| 150 | } |
| 151 | |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 152 | |
| 153 | #Return a hash that represents the 'chosen' node |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 154 | #Will look like: |
| 155 | # chosen { |
| 156 | # stdout-path = ... |
| 157 | # bootargs = ... |
| 158 | # } |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 159 | sub getChosen |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 160 | { |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 161 | my %chosen; |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 162 | my @allowed = qw(bootargs stdin-path stdout-path); |
| 163 | |
| 164 | #Get the info from the config file |
| 165 | |
| 166 | if (not exists $g_configuration{chosen}) { |
| 167 | die "ERROR: Missing 'chosen' section in config file.\n"; |
| 168 | } |
| 169 | %chosen = %{ $g_configuration{chosen} }; |
| 170 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 171 | foreach my $key (keys %chosen) { |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 172 | |
Matt Spinler | 28fb1a9 | 2017-01-30 12:54:10 -0600 | [diff] [blame] | 173 | #Check for allowed entries. Empty is OK. |
| 174 | if (!grep(/^$key$/, @allowed)) { |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 175 | die "Invalid entry $key in 'chosen' section in config file\n"; |
| 176 | } |
Matt Spinler | 28fb1a9 | 2017-01-30 12:54:10 -0600 | [diff] [blame] | 177 | |
| 178 | #stdout-path and stdin-path can use aliases, which will look like |
| 179 | #(alias)uart5 in the yaml. Change to (ref)uart5 so it will be |
| 180 | #converted to a '&' later. |
| 181 | $chosen{$key} =~ s/\(alias\)/\(ref\)/g; |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 182 | } |
| 183 | |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 184 | return %chosen; |
| 185 | } |
| 186 | |
| 187 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 188 | #Return a hash that represents the 'memory' node. |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 189 | #Will look like: |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 190 | # memory { |
| 191 | # reg = < base size > |
| 192 | # } |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 193 | sub getBmcMemory |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 194 | { |
| 195 | my %memory; |
| 196 | |
| 197 | #Get the info from the config file |
| 198 | |
| 199 | if (not exists $g_configuration{memory}) { |
| 200 | die "ERROR: Missing 'memory' section in config file.\n"; |
| 201 | } |
| 202 | |
| 203 | if ((not exists $g_configuration{memory}{base}) || |
| 204 | ($g_configuration{memory}{base} !~ /0x/)) { |
| 205 | die "ERROR: The base entry in the memory section in the config " . |
| 206 | "file is either missing or invalid.\n"; |
| 207 | } |
| 208 | |
| 209 | if ((not exists $g_configuration{memory}{size}) || |
| 210 | ($g_configuration{memory}{size} !~ /0x/)) { |
| 211 | die "ERROR: The size entry in the memory section in the config " . |
| 212 | "file is either missing or invalid.\n"; |
| 213 | } |
| 214 | |
| 215 | #Future: could do more validation on the actual values |
| 216 | |
Matt Spinler | 9ac5cbe | 2017-01-31 14:33:25 -0600 | [diff] [blame] | 217 | addRegProp(\%memory, |
| 218 | $g_configuration{memory}{base}, |
| 219 | $g_configuration{memory}{size}); |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 220 | |
| 221 | return %memory; |
| 222 | } |
| 223 | |
| 224 | |
Matt Spinler | 6d39125 | 2017-01-31 13:46:06 -0600 | [diff] [blame] | 225 | #Returns a hash that represents the 'reserved-memory' node. |
| 226 | #This currently only supports the memory region for the LPC |
| 227 | #host spi flash mailbox. Will look like: |
| 228 | # reserved-memory { |
| 229 | # #address-cells = <1>; |
| 230 | # #size-cells = <1>; |
| 231 | # ranges; |
| 232 | # |
| 233 | # flash_memory: region@94000000 { |
| 234 | # no-map; |
| 235 | # reg = <0x94000000 0x04000000>; |
| 236 | # }; |
| 237 | # }; |
| 238 | sub getReservedMemory |
| 239 | { |
| 240 | my %memory; |
| 241 | |
| 242 | if (not exists $g_configuration{"lpc-host-spi-flash-mailbox"}) { |
| 243 | return %memory; |
| 244 | } |
| 245 | |
| 246 | $memory{"#address-cells"} = "<1>"; |
| 247 | $memory{"#size-cells"} = "<1>"; |
| 248 | $memory{ranges} = ZERO_LENGTH_PROPERTY; |
| 249 | |
| 250 | #Get the sub node that contains the address range |
| 251 | my ($name, $node) = getHostSpiFlashMboxRegion(); |
| 252 | $memory{$name} = { %$node }; |
| 253 | |
| 254 | return %memory; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | #Returns a hash that represents a child node of the |
| 259 | #reserved-memory node which contains the address range |
| 260 | #that the host spi flash is mapped to. |
| 261 | sub getHostSpiFlashMboxRegion |
| 262 | { |
| 263 | my %node; |
| 264 | |
| 265 | $node{"no-map"} = ZERO_LENGTH_PROPERTY; |
| 266 | |
| 267 | #This node needs a label the LPC node can refer to. |
| 268 | $node{NODE_LABEL} = HOST_SPI_FLASH_MEM_REGION_NODE_LABEL; |
| 269 | |
| 270 | #Get the memory region's base address and size from the config file |
| 271 | if (not exists $g_configuration{"lpc-host-spi-flash-mailbox"} |
| 272 | {"bmc-address-range"}{base}) { |
| 273 | die "Could not find lpc-host-spi-flash-mailbox base " . |
| 274 | "address in config file\n"; |
| 275 | } |
| 276 | |
| 277 | my $base = $g_configuration{"lpc-host-spi-flash-mailbox"} |
| 278 | {"bmc-address-range"}{base}; |
| 279 | #Allow 1 hex value, up to 4B |
| 280 | if ($base !~ /^0x[0-9a-fA-F]{1,8}$/) { |
| 281 | die "lpc-host-spi-flash-mailbox base address $base is invalid\n"; |
| 282 | } |
| 283 | |
| 284 | if (not exists $g_configuration{"lpc-host-spi-flash-mailbox"} |
| 285 | {"bmc-address-range"}{size}) { |
| 286 | die "Could not find lpc-host-spi-flash-mailbox address size " . |
| 287 | "in config file\n"; |
| 288 | } |
| 289 | |
| 290 | my $size = $g_configuration{"lpc-host-spi-flash-mailbox"} |
| 291 | {"bmc-address-range"}{size}; |
| 292 | if ($size !~ /^0x[0-9a-fA-F]{1,8}$/) { |
| 293 | die "lpc-host-spi-flash-mailbox address range size " . |
| 294 | "$size is invalid\n"; |
| 295 | } |
| 296 | |
Matt Spinler | 9ac5cbe | 2017-01-31 14:33:25 -0600 | [diff] [blame] | 297 | addRegProp(\%node, $base, $size); |
Matt Spinler | 6d39125 | 2017-01-31 13:46:06 -0600 | [diff] [blame] | 298 | my $name = makeNodeName("region", $node{reg}); |
| 299 | |
| 300 | return ($name, \%node); |
| 301 | } |
| 302 | |
| 303 | |
Matt Spinler | 25d60bb | 2016-10-31 15:16:03 -0500 | [diff] [blame] | 304 | #Returns an array of hashes representing the device tree nodes for |
| 305 | #the BMC flash. These nodes are BMC model specific because different |
| 306 | #models can have different device drivers. |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 307 | sub getBMCFlashNodes |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 308 | { |
Matt Spinler | 25d60bb | 2016-10-31 15:16:03 -0500 | [diff] [blame] | 309 | my @nodes; |
| 310 | |
| 311 | if ($g_bmcModel eq "AST2500") { |
| 312 | my %node = getAST2500BMCSPIFlashNode(); |
| 313 | push @nodes, { %node }; |
| 314 | } |
| 315 | else { |
| 316 | die "ERROR: No BMC SPI flash support yet for BMC model $g_bmcModel\n"; |
| 317 | } |
| 318 | |
| 319 | return @nodes; |
| 320 | } |
| 321 | |
| 322 | |
| 323 | #Returns a hash that represents the BMC SPI flash(es) by finding the SPI |
| 324 | #connections that come from the unit tagged as BMC_CODE. The code also |
| 325 | #looks in the config file for any additional properties to add. Supports |
| 326 | #the hardware where the same SPI master unit can be wired to more than 1 |
| 327 | #flash (a chip select line is used to switch between them.) This is |
| 328 | #specific to the ASPEED AST2500 hardware and device driver. |
| 329 | #Will look like: |
| 330 | # fmc { |
| 331 | # status = "okay" |
| 332 | # flash@0 { |
| 333 | # ... |
| 334 | # }; |
| 335 | # flash@1 { |
| 336 | # ... |
| 337 | # }; |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 338 | sub getAST2500BMCSPIFlashNode |
Matt Spinler | 25d60bb | 2016-10-31 15:16:03 -0500 | [diff] [blame] | 339 | { |
| 340 | my %bmcFlash; |
| 341 | my $chipSelect = 0; |
| 342 | my $lastUnit = ""; |
| 343 | |
Matt Spinler | 18d5f57 | 2016-11-15 15:25:45 -0600 | [diff] [blame] | 344 | my $connections = $g_targetObj->findConnections($g_bmc, "SPI", "FLASH"); |
Matt Spinler | 25d60bb | 2016-10-31 15:16:03 -0500 | [diff] [blame] | 345 | |
| 346 | if ($connections eq "") { |
| 347 | die "ERROR: No BMC SPI flashes found connected to the BMC\n"; |
| 348 | } |
| 349 | |
Matt Spinler | 9ac5cbe | 2017-01-31 14:33:25 -0600 | [diff] [blame] | 350 | statusOK(\%{$bmcFlash{fmc}}); |
Matt Spinler | 25d60bb | 2016-10-31 15:16:03 -0500 | [diff] [blame] | 351 | |
| 352 | foreach my $spi (@{$connections->{CONN}}) { |
| 353 | |
| 354 | #Looking for spi-masters with a function of 'BMC_CODE'. |
| 355 | #It's possible there are multiple flash chips here. |
| 356 | if (!$g_targetObj->isBadAttribute($spi->{SOURCE}, "SPI_FUNCTION")) { |
| 357 | |
| 358 | my $function = $g_targetObj->getAttribute($spi->{SOURCE}, |
| 359 | "SPI_FUNCTION"); |
| 360 | if ($function eq "BMC_CODE") { |
| 361 | |
| 362 | my $flashName = "flash@".$chipSelect; |
| 363 | |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 364 | $bmcFlash{fmc}{$flashName}{COMMENT} = connectionComment($spi); |
Matt Spinler | 25d60bb | 2016-10-31 15:16:03 -0500 | [diff] [blame] | 365 | |
Matt Spinler | 9ac5cbe | 2017-01-31 14:33:25 -0600 | [diff] [blame] | 366 | statusOK(\%{$bmcFlash{fmc}{$flashName}}); |
Matt Spinler | 25d60bb | 2016-10-31 15:16:03 -0500 | [diff] [blame] | 367 | |
| 368 | #Add in anything specified in the config file for this chip. |
| 369 | addBMCFlashConfigProperties(\%{$bmcFlash{fmc}{$flashName}}, |
| 370 | $chipSelect); |
| 371 | |
| 372 | #The code currently only supports the config where a chip |
| 373 | #select line is used to select between possibly multiple |
| 374 | #flash chips attached to the same SPI pins/unit. So we |
| 375 | #need to make sure if there are multiple chips found, that |
| 376 | #they are off of the same master unit. |
| 377 | if ($lastUnit eq "") { |
| 378 | $lastUnit = $spi->{SOURCE}; |
| 379 | } |
| 380 | else { |
| 381 | if ($lastUnit ne $spi->{SOURCE}) { |
| 382 | die "ERROR: Currently only 1 spi-master unit is " . |
| 383 | "supported for BMC flash connections." |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | #Since we don't need anything chip select specific from the |
| 388 | #XML, we can just assign our own chip selects. |
| 389 | $chipSelect++; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if ($chipSelect == 0) { |
| 395 | die "ERROR: Didn't find any BMC flash chips connected"; |
| 396 | } |
| 397 | |
| 398 | return %bmcFlash; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | #Looks in the bmc-flash-config section in the config file for the |
| 403 | #chip select passed in to add any additional properties to the BMC |
| 404 | #flash node. |
| 405 | # $node = hash reference to the flash node |
| 406 | # $cs = the flash chip select value |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 407 | sub addBMCFlashConfigProperties |
Matt Spinler | 25d60bb | 2016-10-31 15:16:03 -0500 | [diff] [blame] | 408 | { |
| 409 | my ($node, $cs) = @_; |
| 410 | my $section = "chip-select-$cs"; |
| 411 | |
| 412 | if (exists $g_configuration{"bmc-flash-config"}{$section}) { |
| 413 | foreach my $key (sort keys $g_configuration{"bmc-flash-config"}{$section}) { |
| 414 | $node->{$key} = $g_configuration{"bmc-flash-config"}{$section}{$key}; |
| 415 | } |
| 416 | } |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 420 | #Returns an array of hashes representing the other flashes used by the |
| 421 | #BMC besides the ones that hold the BMC code. This is BMC model specific |
| 422 | #as different models can have different interfaces. |
| 423 | #Typically, these are SPI flashes. |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 424 | sub getOtherFlashNodes |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 425 | { |
| 426 | my @nodes; |
| 427 | |
| 428 | if ($g_bmcModel eq "AST2500") { |
| 429 | @nodes = getAST2500SpiFlashNodes(); |
| 430 | } |
| 431 | else { |
| 432 | die "ERROR: No SPI flash support yet for BMC model $g_bmcModel\n"; |
| 433 | } |
| 434 | |
| 435 | return @nodes; |
| 436 | } |
| 437 | |
| 438 | |
| 439 | #Returns an array of hashes representing the SPI flashes in an |
| 440 | #AST2500. These are for the SPI1 and SPI2 interfaces in the chip. |
| 441 | #Each SPI master interface can support multiple flash chips. If |
| 442 | #no hardware is connected to the interface, the node won't be present. |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 443 | sub getAST2500SpiFlashNodes |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 444 | { |
| 445 | my @nodes; |
| 446 | |
| 447 | #The AST2500 has 2 SPI master units, 1 and 2. |
| 448 | my @units = (1, 2); |
| 449 | |
| 450 | foreach my $unit (@units) { |
| 451 | |
Matt Spinler | 908e182 | 2017-01-31 14:02:58 -0600 | [diff] [blame] | 452 | my ($node, $foundPNOR) = getAST2500SpiMasterNode($unit); |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 453 | |
Matt Spinler | 908e182 | 2017-01-31 14:02:58 -0600 | [diff] [blame] | 454 | if (keys %$node) { |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 455 | my %spiNode; |
| 456 | my $nodeName = "spi$unit"; |
Matt Spinler | 908e182 | 2017-01-31 14:02:58 -0600 | [diff] [blame] | 457 | $spiNode{$nodeName} = { %$node }; |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 458 | push @nodes, { %spiNode }; |
Matt Spinler | 908e182 | 2017-01-31 14:02:58 -0600 | [diff] [blame] | 459 | |
| 460 | #Save off the PNOR SPI node name for use by LPC node |
| 461 | if ($foundPNOR) { |
| 462 | $g_pnorNodeName = $nodeName; |
| 463 | } |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | return @nodes; |
| 468 | } |
| 469 | |
| 470 | |
| 471 | #Returns a hash that represents the device tree node for the SPI1 |
| 472 | #or SPI2 master interface on the AST2500. Each master can support |
| 473 | #multiple chips by use of a chip select. |
| 474 | #Will look like: |
| 475 | # spi1 { |
| 476 | # status = "okay"; |
| 477 | # flash@0 { |
| 478 | # ... |
| 479 | # }; |
| 480 | # }; |
| 481 | # |
| 482 | # $spiNum = The SPI master unit number to use |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 483 | sub getAST2500SpiMasterNode |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 484 | { |
| 485 | my $spiNum = shift; |
| 486 | my %spiMaster; |
| 487 | my $chipSelect = 0; |
Matt Spinler | 908e182 | 2017-01-31 14:02:58 -0600 | [diff] [blame] | 488 | my $foundPNOR = 0; |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 489 | |
Matt Spinler | 18d5f57 | 2016-11-15 15:25:45 -0600 | [diff] [blame] | 490 | my $connections = $g_targetObj->findConnections($g_bmc, "SPI", "FLASH"); |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 491 | |
| 492 | if ($connections eq "") { |
| 493 | return %spiMaster; |
| 494 | } |
| 495 | |
| 496 | #Looking for spi-masters with a chip-unit of $spiNum |
| 497 | #It's possible there are multiple flash chips off the master |
| 498 | foreach my $spi (@{$connections->{CONN}}) { |
| 499 | |
| 500 | my $unitNum = $g_targetObj->getAttribute($spi->{SOURCE}, |
| 501 | "CHIP_UNIT"); |
| 502 | if ($unitNum == $spiNum) { |
Matt Spinler | 9ac5cbe | 2017-01-31 14:33:25 -0600 | [diff] [blame] | 503 | statusOK(\%spiMaster); |
Matt Spinler | 2efdcba | 2016-11-08 15:37:20 -0600 | [diff] [blame] | 504 | |
| 505 | #Add in any pinctrl properties. These would come from the parent |
| 506 | #of $spi{SOURCE}, which would be a unit-pingroup-bmc if the |
| 507 | #pins for this connection are multi-function. |
| 508 | addPinCtrlProps($g_targetObj->getTargetParent($spi->{SOURCE}), |
| 509 | \%spiMaster); |
| 510 | |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 511 | my $flashName = "flash@".$chipSelect; |
| 512 | |
| 513 | $spiMaster{$flashName}{COMMENT} = connectionComment($spi); |
| 514 | |
Matt Spinler | 9ac5cbe | 2017-01-31 14:33:25 -0600 | [diff] [blame] | 515 | statusOK(\%{$spiMaster{$flashName}}); |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 516 | |
Matt Spinler | 0eda488 | 2016-11-30 15:20:11 -0600 | [diff] [blame] | 517 | #AST2500 PNORs need a label |
| 518 | my $function = $g_targetObj->getAttribute($spi->{SOURCE}, |
| 519 | "SPI_FUNCTION"); |
| 520 | if ($function eq "PNOR") { |
| 521 | $spiMaster{$flashName}{label} = "pnor"; |
Matt Spinler | 908e182 | 2017-01-31 14:02:58 -0600 | [diff] [blame] | 522 | $foundPNOR = 1; |
Matt Spinler | 0eda488 | 2016-11-30 15:20:11 -0600 | [diff] [blame] | 523 | } |
| 524 | |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 525 | $chipSelect++; |
| 526 | } |
| 527 | } |
| 528 | |
Matt Spinler | 908e182 | 2017-01-31 14:02:58 -0600 | [diff] [blame] | 529 | return (\%spiMaster, $foundPNOR); |
| 530 | } |
| 531 | |
| 532 | |
| 533 | #Returns a hash that represents the mbox node. |
| 534 | #This node is used by the LPC mailbox device driver. |
| 535 | #Only present if the LPC mailbox is enabled in the config file. |
| 536 | #Node looks like: |
| 537 | # &mbox { |
| 538 | # status = "okay"; |
| 539 | # } |
| 540 | sub getMBoxNode |
| 541 | { |
| 542 | my %node; |
| 543 | if (exists $g_configuration{"lpc-host-spi-flash-mailbox"}) { |
Matt Spinler | 9ac5cbe | 2017-01-31 14:33:25 -0600 | [diff] [blame] | 544 | statusOK(\%node); |
Matt Spinler | 908e182 | 2017-01-31 14:02:58 -0600 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | return %node; |
| 548 | } |
| 549 | |
| 550 | |
| 551 | #Returns a hash that represents the LPC node. |
| 552 | #Only present if the LPC mailbox is enabled in the config file. |
| 553 | #Node looks like: |
| 554 | # &lpc_ctrl { |
| 555 | # flash = <&spi1>; |
| 556 | # memory-region = <&flash_memory>; |
| 557 | # status = "okay"; |
| 558 | #}; |
| 559 | sub getLPCNode |
| 560 | { |
| 561 | my %node; |
| 562 | if (exists $g_configuration{"lpc-host-spi-flash-mailbox"}) { |
| 563 | |
Matt Spinler | 9ac5cbe | 2017-01-31 14:33:25 -0600 | [diff] [blame] | 564 | statusOK(\%node); |
Matt Spinler | 908e182 | 2017-01-31 14:02:58 -0600 | [diff] [blame] | 565 | |
| 566 | #Point to the reserved-memory region label |
| 567 | $node{"memory-region"} = "<(ref)" . |
| 568 | HOST_SPI_FLASH_MEM_REGION_NODE_LABEL . ">"; |
| 569 | |
| 570 | if (not defined $g_pnorNodeName) { |
| 571 | die "The PNOR SPI flash node cannot be found but is required " . |
| 572 | "if the LPC mailbox is enabled.\n"; |
| 573 | } |
| 574 | |
| 575 | $node{flash} = "<(ref)$g_pnorNodeName>"; |
| 576 | } |
| 577 | |
| 578 | return %node; |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 582 | #Returns a hash that represents the leds node by finding all of the |
| 583 | #GPIO connections to LEDs. |
| 584 | #Node will look like: |
| 585 | # leds { |
| 586 | # <ledname> { |
| 587 | # gpios = &gpio ASPEED_GPIO(x, y) GPIO_ACTIVE_xxx> |
| 588 | # }; |
| 589 | # <another ledname> { |
| 590 | # ... |
| 591 | # } |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 592 | sub getLEDNode |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 593 | { |
| 594 | my %leds; |
| 595 | |
Matt Spinler | eca7f06 | 2016-11-07 09:59:23 -0600 | [diff] [blame] | 596 | $leds{compatible} = "gpio-leds"; |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 597 | |
Matt Spinler | 18d5f57 | 2016-11-15 15:25:45 -0600 | [diff] [blame] | 598 | my $connections = $g_targetObj->findConnections($g_bmc, "GPIO", "LED"); |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 599 | |
| 600 | if ($connections eq "") { |
| 601 | print "WARNING: No LEDs found connected to the BMC\n"; |
| 602 | return %leds; |
| 603 | } |
| 604 | |
| 605 | foreach my $gpio (@{$connections->{CONN}}) { |
| 606 | my %ledNode; |
| 607 | |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 608 | $ledNode{COMMENT} = connectionComment($gpio); |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 609 | |
| 610 | #The node name will be the simplified LED name |
| 611 | my $name = $gpio->{DEST_PARENT}; |
| 612 | $name =~ s/(-\d+$)//; #remove trailing position |
| 613 | $name =~ s/.*\///; #remove the front of the path |
| 614 | |
| 615 | #For now only supports ASPEED. |
| 616 | if (uc($g_bmcMfgr) ne "ASPEED") { |
| 617 | die "ERROR: Unsupported BMC manufacturer $g_bmcMfgr\n"; |
| 618 | } |
| 619 | my $num = $g_targetObj->getAttribute($gpio->{SOURCE}, "PIN_NUM"); |
| 620 | my $macro = getAspeedGpioMacro($num); |
| 621 | |
| 622 | #If it's active high or low |
| 623 | my $state = $g_targetObj->getAttribute($gpio->{DEST_PARENT}, "ON_STATE"); |
| 624 | my $activeString = getGpioActiveString($state); |
| 625 | |
| 626 | $ledNode{gpios} = "<&gpio $macro $activeString>"; |
| 627 | |
| 628 | $leds{$name} = { %ledNode }; |
| 629 | } |
| 630 | |
| 631 | return %leds; |
| 632 | } |
| 633 | |
| 634 | |
| 635 | #Returns a either GPIO_ACTIVE_HIGH or GPIO_ACTIVE_LOW |
| 636 | # $val = either a 1 or a 0 for active high or low |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 637 | sub getGpioActiveString |
| 638 | { |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 639 | my $val = shift; |
| 640 | |
| 641 | if ($val == 0) { |
| 642 | return "GPIO_ACTIVE_LOW"; |
| 643 | } |
| 644 | |
| 645 | return "GPIO_ACTIVE_HIGH"; |
| 646 | } |
| 647 | |
| 648 | |
| 649 | #Turns a GPIO number into something like ASPEED_GPIO(A, 0) for the |
| 650 | #ASPEED GPIO numbering scheme A[0-7] -> Z[0-7] and then starts at |
| 651 | #AA[0-7] after that. |
| 652 | # $num = the GPIO number |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 653 | sub getAspeedGpioMacro |
| 654 | { |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 655 | my $num = shift; |
| 656 | my $char; |
| 657 | my $offset = $num % 8; |
| 658 | my $block = int($num / 8); |
| 659 | |
| 660 | #If past Z, wraps to AA, AB, etc |
| 661 | if ((ord('A') + $block) > ord('Z')) { |
| 662 | #how far past Z? |
| 663 | $char = $block - (ord('Z') - ord('A')); |
| 664 | |
| 665 | #Don't let it wrap twice |
| 666 | if ($char > (ord('Z') - ord('A') + 1)) { |
| 667 | die "ERROR: Invalid PIN_NUM value $num found for GPIO\n"; |
| 668 | } |
| 669 | |
| 670 | #start back at 'A' again, and convert to a character |
| 671 | $char = chr($char + ord('A') - 1); |
| 672 | |
| 673 | #Add in a bonus 'A', to get something like AB |
| 674 | $char = "A".$char; |
| 675 | } |
| 676 | else { |
| 677 | $char = ord('A') + $block; |
| 678 | $char = chr($char); |
| 679 | } |
| 680 | |
| 681 | return "ASPEED_GPIO($char, $offset)"; |
| 682 | } |
| 683 | |
| 684 | |
| 685 | #Returns a list of hashes that represent the UART nodes on the BMC by |
| 686 | #finding the UART connections. |
| 687 | #Nodes will look like: |
| 688 | # &uartX { |
| 689 | # status = "okay" |
| 690 | # } |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 691 | sub getUARTNodes |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 692 | { |
| 693 | my @nodes; |
| 694 | |
Matt Spinler | 23d47c2 | 2016-10-04 12:31:21 -0500 | [diff] [blame] | 695 | #Using U750 for legacy MRW reasons |
Matt Spinler | 18d5f57 | 2016-11-15 15:25:45 -0600 | [diff] [blame] | 696 | my $connections = $g_targetObj->findConnections($g_bmc, "U750"); |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 697 | |
| 698 | if ($connections eq "") { |
| 699 | print "WARNING: No UART buses found connected to the BMC\n"; |
| 700 | return @nodes; |
| 701 | } |
| 702 | |
| 703 | foreach my $uart (@{$connections->{CONN}}) { |
| 704 | my %node; |
| 705 | |
| 706 | my $num = $g_targetObj->getAttribute($uart->{SOURCE}, "CHIP_UNIT"); |
| 707 | my $name = "uart$num"; |
| 708 | |
Matt Spinler | 9ac5cbe | 2017-01-31 14:33:25 -0600 | [diff] [blame] | 709 | statusOK(\%{$node{$name}}); |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 710 | $node{$name}{COMMENT} = connectionComment($uart); |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 711 | |
Matt Spinler | 2efdcba | 2016-11-08 15:37:20 -0600 | [diff] [blame] | 712 | #Add in any pinctrl properties. These would come from the parent |
| 713 | #of $uart{SOURCE}, which would be a unit-pingroup-bmc if the |
| 714 | #pins for this connection are multi-function. |
| 715 | addPinCtrlProps($g_targetObj->getTargetParent($uart->{SOURCE}), |
| 716 | \%{$node{$name}}); |
| 717 | |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 718 | push @nodes, { %node }; |
| 719 | } |
| 720 | |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 721 | return @nodes; |
| 722 | } |
| 723 | |
| 724 | |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 725 | #Returns a list of hashes that represent the MAC (ethernet) nodes on the BMC |
| 726 | #by finding the connections of type ETHERNET. |
| 727 | #Nodes will look like: |
| 728 | # &macX { |
| 729 | # ... |
| 730 | # } |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 731 | sub getMacNodes |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 732 | { |
| 733 | my @nodes; |
| 734 | |
Matt Spinler | 18d5f57 | 2016-11-15 15:25:45 -0600 | [diff] [blame] | 735 | my $connections = $g_targetObj->findConnections($g_bmc, "ETHERNET"); |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 736 | |
| 737 | if ($connections eq "") { |
| 738 | print "WARNING: No ethernet buses found connected to the BMC\n"; |
| 739 | return @nodes; |
| 740 | } |
| 741 | |
| 742 | foreach my $eth (@{$connections->{CONN}}) { |
| 743 | my %node; |
| 744 | |
| 745 | my $num = $g_targetObj->getAttribute($eth->{SOURCE}, "CHIP_UNIT"); |
| 746 | my $ncsi = $g_targetObj->getAttribute($eth->{SOURCE}, "NCSI_MODE"); |
| 747 | my $hwChecksum = $g_targetObj->getAttribute($eth->{SOURCE}, |
| 748 | "USE_HW_CHECKSUM"); |
| 749 | |
| 750 | my $name = "mac$num"; |
Matt Spinler | 9ac5cbe | 2017-01-31 14:33:25 -0600 | [diff] [blame] | 751 | statusOK(\%{$node{$name}}); |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 752 | |
| 753 | if ($ncsi == 1) { |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 754 | $node{$name}{"use-ncsi"} = ZERO_LENGTH_PROPERTY; |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 755 | } |
| 756 | if ($hwChecksum == 0) { |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 757 | $node{$name}{"no-hw-checksum"} = ZERO_LENGTH_PROPERTY; |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 758 | } |
| 759 | |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 760 | $node{$name}{COMMENT} = connectionComment($eth); |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 761 | |
Matt Spinler | 2efdcba | 2016-11-08 15:37:20 -0600 | [diff] [blame] | 762 | #Add in any pinctrl properties. These would come from the parent |
| 763 | #of $eth{SOURCE}, which would be a unit-pingroup-bmc if the |
| 764 | #pins for this connection are multi-function. |
| 765 | addPinCtrlProps($g_targetObj->getTargetParent($eth->{SOURCE}), |
| 766 | \%{$node{$name}}); |
| 767 | |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 768 | push @nodes, { %node }; |
| 769 | } |
| 770 | |
| 771 | return @nodes; |
| 772 | } |
| 773 | |
| 774 | |
| 775 | #Returns a list of hashes that represent the virtual UART nodes |
| 776 | #Node will look like: |
| 777 | # &vuart { |
| 778 | # status = "okay" |
| 779 | # } |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 780 | sub getVuartNodes |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 781 | { |
| 782 | my @nodes; |
| 783 | my %node; |
| 784 | |
| 785 | #For now, enable 1 node all the time. |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 786 | #TBD if this needs to be fixed |
Matt Spinler | 9ac5cbe | 2017-01-31 14:33:25 -0600 | [diff] [blame] | 787 | statusOK(\%{$node{vuart}}); |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 788 | |
| 789 | push @nodes, { %node }; |
| 790 | |
| 791 | return @nodes; |
| 792 | } |
| 793 | |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 794 | #Returns a list of hashes that represent the I2C device nodes. |
| 795 | #There is 1 parent node for each bus, which then have subnodes |
| 796 | #for each device on that bus. If a bus doesn't have any |
| 797 | #attached devices, it doesn't need to show up. |
| 798 | #The nodes will look like: |
| 799 | # &i2c0 { |
| 800 | # status = "okay" |
| 801 | # device1@addr { (addr = 7 bit I2C address) |
| 802 | # reg = <addr> |
| 803 | # compatible = ... |
| 804 | # ... |
| 805 | # } |
| 806 | # device2@addr { |
| 807 | # reg = <addr> |
| 808 | # ... |
| 809 | # } |
| 810 | # } |
| 811 | # &i2c1 { |
| 812 | # ... |
| 813 | # } |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 814 | sub getI2CNodes |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 815 | { |
| 816 | my @nodes; |
| 817 | my %busNodes; |
| 818 | |
Matt Spinler | 18d5f57 | 2016-11-15 15:25:45 -0600 | [diff] [blame] | 819 | my $connections = $g_targetObj->findConnections($g_bmc, "I2C"); |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 820 | |
| 821 | if ($connections eq "") { |
| 822 | print "WARNING: No I2C buses found connected to the BMC\n"; |
| 823 | return @nodes; |
| 824 | } |
| 825 | |
| 826 | foreach my $i2c (@{$connections->{CONN}}) { |
| 827 | |
| 828 | my %deviceNode, my $deviceName; |
| 829 | |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 830 | $deviceNode{COMMENT} = connectionComment($i2c); |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 831 | |
| 832 | $deviceName = lc $i2c->{DEST_PARENT}; |
| 833 | $deviceName =~ s/-\d+$//; #remove trailing position |
| 834 | $deviceName =~ s/.*\///; #remove the front of the path |
| 835 | |
| 836 | #Get the I2C address |
| 837 | my $i2cAddress = $g_targetObj->getAttribute($i2c->{DEST}, "I2C_ADDRESS"); |
| 838 | $i2cAddress = hex($i2cAddress); |
| 839 | if ($i2cAddress == 0) { |
| 840 | die "ERROR: Missing I2C address on $i2c->{DEST}\n"; |
| 841 | } |
| 842 | |
| 843 | #Put it in the format we want to print it in |
| 844 | $i2cAddress = adjustI2CAddress($i2cAddress); |
Matt Spinler | 9ac5cbe | 2017-01-31 14:33:25 -0600 | [diff] [blame] | 845 | addRegProp(\%deviceNode, $i2cAddress); |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 846 | |
| 847 | $deviceName = makeNodeName($deviceName, $deviceNode{reg}); |
| 848 | |
| 849 | #Get the I2C bus number |
| 850 | if ($g_targetObj->isBadAttribute($i2c->{SOURCE}, |
| 851 | "I2C_PORT")) { |
| 852 | die "ERROR: I2C_PORT attribute in $i2c->{DEST_PARENT} " . |
| 853 | "is either missing or empty.\n"; |
| 854 | } |
| 855 | |
| 856 | my $busNum = $g_targetObj->getAttribute($i2c->{SOURCE}, "I2C_PORT"); |
| 857 | if ($busNum =~ /0x/i) { |
| 858 | $busNum = hex($busNum); |
| 859 | } |
| 860 | |
| 861 | #Convert the number to the Linux numbering scheme. |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 862 | $busNum += $g_i2cBusAdjust; |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 863 | |
| 864 | #Get the compatible property |
| 865 | if ($g_targetObj->isBadAttribute($i2c->{DEST_PARENT}, |
| 866 | "BMC_DT_COMPATIBLE")) { |
| 867 | die "ERROR: BMC_DT_COMPATIBLE attribute in $i2c->{DEST_PARENT} " . |
| 868 | "is either missing or empty.\n"; |
| 869 | } |
| 870 | |
| 871 | $deviceNode{compatible} = $g_targetObj->getAttribute( |
| 872 | $i2c->{DEST_PARENT}, |
| 873 | "BMC_DT_COMPATIBLE"); |
| 874 | |
| 875 | #Get any other part specific properties, where the property |
| 876 | #names are actually defined in the XML. |
| 877 | my %props = getPartDefinedDTProperties($i2c->{DEST_PARENT}); |
| 878 | foreach my $prop (sort keys %props) { |
| 879 | $deviceNode{$prop} = $props{$prop}; |
| 880 | } |
| 881 | |
| 882 | #busNodeName is the hash twice so when we loop |
| 883 | #below it doesn't get lost |
| 884 | my $busNodeName = "i2c$busNum"; |
Matt Spinler | 9ac5cbe | 2017-01-31 14:33:25 -0600 | [diff] [blame] | 885 | statusOK(\%{$busNodes{$busNodeName}{$busNodeName}}); |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 886 | $busNodes{$busNodeName}{$busNodeName}{$deviceName} = { %deviceNode }; |
Matt Spinler | 2efdcba | 2016-11-08 15:37:20 -0600 | [diff] [blame] | 887 | |
| 888 | #Add in any pinctrl properties. These would come from the parent |
| 889 | #of $i2c{SOURCE}, which would be a unit-pingroup-bmc if the |
| 890 | #pins for this connection are multi-function. |
| 891 | addPinCtrlProps($g_targetObj->getTargetParent($i2c->{SOURCE}), |
| 892 | \%{$busNodes{$busNodeName}{$busNodeName}}); |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | #Each bus gets its own hash entry in the array |
| 896 | for my $b (sort keys %busNodes) { |
| 897 | push @nodes, { %{$busNodes{$b}} }; |
| 898 | } |
| 899 | |
| 900 | return @nodes; |
| 901 | } |
| 902 | |
| 903 | |
| 904 | #Returns a hash of property names and values that should be stored in |
| 905 | #the device tree node for this device. The names of the properties and |
| 906 | #the attributes to find their values in are stored in the |
| 907 | #BMC_DT_ATTR_NAMES attribute in the chip. |
| 908 | # $chip = the chip target |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 909 | sub getPartDefinedDTProperties |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 910 | { |
| 911 | my $chip = shift; |
| 912 | my %props; |
| 913 | |
| 914 | if ($g_targetObj->isBadAttribute($chip, "BMC_DT_ATTR_NAMES")) { |
| 915 | return %props; |
| 916 | } |
| 917 | |
| 918 | my $attr = $g_targetObj->getAttribute($chip, "BMC_DT_ATTR_NAMES"); |
| 919 | $attr =~ s/\s//g; |
| 920 | my @names = split(',', $attr); |
| 921 | |
| 922 | #There can be up to 4 entries in this attribute |
| 923 | for (my $i = 0; $i < scalar @names; $i += 2) { |
| 924 | |
| 925 | #$names[$i] holds the name of the attribute. |
| 926 | #$names[$i+1] holds the name of the property to store its value in. |
| 927 | if (($names[$i] ne "NA") && ($names[$i] ne "")) { |
| 928 | |
| 929 | my $val = $g_targetObj->getAttribute($chip, $names[$i]); |
| 930 | |
| 931 | #if the value is empty, assume it's for a standalone property, |
| 932 | #which gets turned into: some-property; |
| 933 | if ($val eq "") { |
| 934 | $props{$names[$i+1]} = ZERO_LENGTH_PROPERTY; |
| 935 | } |
| 936 | else { |
| 937 | $props{$names[$i+1]} = "<$val>"; |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | return %props; |
| 943 | } |
| 944 | |
| 945 | |
| 946 | #Convert the MRW I2C address into the format the dts needs |
| 947 | # $addr = the I2C Address |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 948 | sub adjustI2CAddress |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 949 | { |
| 950 | my $addr = shift; |
| 951 | |
| 952 | #MRW holds the 8 bit value. We need the 7 bit one. |
Matt Spinler | 96f8f24 | 2016-11-28 16:26:57 -0600 | [diff] [blame] | 953 | $addr = $addr >> 1; |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 954 | $addr = sprintf("0x%X", $addr); |
| 955 | $addr = lc $addr; |
| 956 | |
| 957 | return $addr; |
| 958 | } |
| 959 | |
| 960 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 961 | #Sets the global $g_i2cBusAdjust from the configuration file. |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 962 | sub getI2CBusAdjust |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 963 | { |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 964 | if (exists $g_configuration{"i2c-bus-adjust"}) { |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 965 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 966 | $g_i2cBusAdjust = $g_configuration{"i2c-bus-adjust"}; |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 967 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 968 | if (!looks_like_number($g_i2cBusAdjust)) { |
| 969 | die "ERROR: Invalid i2c-bus-adjust value $g_i2cBusAdjust " . |
| 970 | "found in config file.\n"; |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 971 | } |
| 972 | } |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 973 | else { |
| 974 | $g_i2cBusAdjust = 0; |
| 975 | print "WARNING: No I2C Bus number adjustment done " . |
| 976 | "for this system.\n"; |
| 977 | } |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | |
Matt Spinler | 2efdcba | 2016-11-08 15:37:20 -0600 | [diff] [blame] | 981 | |
| 982 | #Adds two pinctrl properties to the device node hash passed in, |
| 983 | #if specified in the MRW. Pin Control refers to a mechanism for |
| 984 | #Linux to know which function of a multi-function pin to configure. |
| 985 | #For example, a pin could either be configured to be a GPIO, or |
| 986 | #an I2C clock line. The pin function depends on board wiring, |
| 987 | #so is known by the MRW. |
| 988 | # $target = the target to get the BMC_DT_PINCTRL_FUNCTS attribute from |
| 989 | # $node = a hash reference to the device tree node to add the properties to |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 990 | sub addPinCtrlProps |
Matt Spinler | 2efdcba | 2016-11-08 15:37:20 -0600 | [diff] [blame] | 991 | { |
| 992 | my ($target, $node) = @_; |
| 993 | |
| 994 | if (!$g_targetObj->isBadAttribute($target, "BMC_DT_PINCTRL_FUNCS")) { |
| 995 | my $attr = $g_targetObj->getAttribute($target, |
| 996 | "BMC_DT_PINCTRL_FUNCS"); |
| 997 | |
| 998 | my $pinCtrl0Prop = makePinCtrl0PropValue($attr); |
| 999 | if ($pinCtrl0Prop ne "") { |
| 1000 | $node->{"pinctrl-names"} = "default"; |
| 1001 | $node->{"pinctrl-0"} = $pinCtrl0Prop; |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | |
| 1007 | #Constructs the pinctrl-0 property value based on the |
| 1008 | #BMC_DT_PINCTRL_FUNCS attribute passed in. |
| 1009 | # $attr = BMC_DT_PINCTRL_FUNCS attribute value, which is an array |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1010 | sub makePinCtrl0PropValue |
Matt Spinler | 2efdcba | 2016-11-08 15:37:20 -0600 | [diff] [blame] | 1011 | { |
| 1012 | my $attr = shift; |
| 1013 | my @entries; |
| 1014 | my $value = ""; |
| 1015 | |
| 1016 | $attr =~ s/\s//g; |
| 1017 | my @funcs = split(',', $attr); |
| 1018 | foreach my $func (@funcs) { |
| 1019 | if (($func ne "NA") && ($func ne "")) { |
| 1020 | push @entries, $func; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | #<&pinctrl_funcA_default &pinctrl_funcB_default ...> |
| 1025 | if (scalar @entries) { |
| 1026 | $value = "<"; |
| 1027 | foreach my $entry (@entries) { |
| 1028 | $value .= "&pinctrl_".$entry."_default "; |
| 1029 | } |
| 1030 | $value =~ s/\s$//; #Remove the trailing space |
| 1031 | $value .= ">"; |
| 1032 | } |
| 1033 | |
| 1034 | return $value; |
| 1035 | } |
| 1036 | |
| 1037 | |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1038 | #Returns a list of compatible fields for the BMC itself. |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1039 | sub getBMCCompatibles |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1040 | { |
| 1041 | my @compats; |
| 1042 | |
Matt Spinler | 23d47c2 | 2016-10-04 12:31:21 -0500 | [diff] [blame] | 1043 | #1st entry: <system mfgr>,<system name>-bmc |
| 1044 | #2nd entry: <bmc mfgr>,<bmc model> |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1045 | |
Matt Spinler | 23d47c2 | 2016-10-04 12:31:21 -0500 | [diff] [blame] | 1046 | foreach my $target (sort keys %{ $g_targetObj->getAllTargets() }) { |
| 1047 | if ($g_targetObj->getType($target) eq "SYS") { |
| 1048 | my $mfgr = $g_targetObj->getAttribute($target, "MANUFACTURER"); |
| 1049 | push @compats, lc "$mfgr,$g_systemName-bmc"; |
| 1050 | last; |
| 1051 | } |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | push @compats, lc($g_bmcMfgr).",".lc($g_bmcModel); |
| 1055 | |
| 1056 | return @compats; |
| 1057 | } |
| 1058 | |
| 1059 | |
| 1060 | #Returns a string for the system's BMC model property |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1061 | sub getSystemBMCModel |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1062 | { |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 1063 | #'<System> BMC' |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1064 | my $sys = lc $g_systemName; |
| 1065 | $sys = uc(substr($sys, 0, 1)) . substr($sys, 1); |
| 1066 | |
| 1067 | return $sys . " BMC"; |
| 1068 | } |
| 1069 | |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 1070 | #Create the comment that will show up in the device tree |
| 1071 | #for a connection. In the output, will look like: |
| 1072 | # // sourceUnit -> |
| 1073 | # // destChip |
| 1074 | # |
| 1075 | # $conn = The connection hash reference |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1076 | sub connectionComment |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 1077 | { |
| 1078 | my $conn = shift; |
| 1079 | my $comment = "$conn->{SOURCE} ->\n$conn->{DEST_PARENT}"; |
| 1080 | return $comment; |
| 1081 | } |
| 1082 | |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1083 | |
| 1084 | #Prints a list of nodes at the same indent level |
| 1085 | # $f = file handle |
| 1086 | # $level = indent level (0,1,etc) |
| 1087 | # @nodes = array of node hashes to print, where the |
| 1088 | # key for the hash is the name of the node |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1089 | sub printNodes |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1090 | { |
| 1091 | my ($f, $level, @nodes) = @_; |
| 1092 | |
| 1093 | foreach my $n (@nodes) { |
| 1094 | my %node = %$n; |
| 1095 | |
| 1096 | foreach my $name (sort keys %node) { |
| 1097 | my %n = %{ $node{$name} }; |
| 1098 | printNode($f, $level, $name, %n); |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | |
| 1104 | #Print a single node and its children |
| 1105 | # $f = file handle |
| 1106 | # $level = indent level (0,1,etc) |
| 1107 | # $name = the name of the node - shows up as: |
| 1108 | # name { ... |
| 1109 | # %vals = The contents of the node, with the following options: |
| 1110 | # if the key is: |
| 1111 | # - 'DTSI_INCLUDE', then value gets turned into a #include |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 1112 | # - 'COMMENT', then value gets turned into a // comment |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 1113 | # - 'ZERO_LENGTH_PROPERTY' then value gets turned into: value; |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1114 | # |
| 1115 | # If the value is: |
| 1116 | # - a hash - then that hash gets turned into a child node |
| 1117 | # where the key is the name of the child node |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 1118 | # - an array of hashes indicates an array of child nodes |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1119 | sub printNode |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1120 | { |
| 1121 | my ($f, $level, $name, %vals) = @_; |
| 1122 | my $include = ""; |
| 1123 | |
Matt Spinler | c0dff8a | 2016-11-02 15:47:30 -0500 | [diff] [blame] | 1124 | #No reason to print an empty node |
| 1125 | if (!keys %vals) { |
| 1126 | return; |
| 1127 | } |
| 1128 | |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1129 | if ($level == 0) { |
| 1130 | $name = "&".$name; |
| 1131 | } |
| 1132 | |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 1133 | print $f "\n"; |
| 1134 | |
| 1135 | if (exists $vals{COMMENT}) { |
| 1136 | my @lines = split('\n', $vals{COMMENT}); |
| 1137 | foreach my $l (@lines) { |
| 1138 | print $f indent($level) . "// $l\n"; |
| 1139 | } |
| 1140 | } |
| 1141 | |
Matt Spinler | 6d39125 | 2017-01-31 13:46:06 -0600 | [diff] [blame] | 1142 | #The node can have a label, which looks like: |
| 1143 | #label : name { |
| 1144 | my $label = ""; |
| 1145 | if (exists $vals{NODE_LABEL}) { |
| 1146 | $label = $vals{NODE_LABEL} . ": "; |
| 1147 | } |
| 1148 | |
| 1149 | print $f indent($level) . $label . "$name {\n"; |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1150 | |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 1151 | #First print properties, then includes, then subnodes |
| 1152 | |
| 1153 | #Print Properties |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1154 | foreach my $v (sort keys %vals) { |
| 1155 | |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 1156 | next if ($v eq "COMMENT"); |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 1157 | next if ($v eq "DTSI_INCLUDE"); |
Matt Spinler | 6d39125 | 2017-01-31 13:46:06 -0600 | [diff] [blame] | 1158 | next if ($v eq "NODE_LABEL"); |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 1159 | next if (ref($vals{$v}) eq "HASH"); |
| 1160 | next if (ref($vals{$v}) eq "ARRAY"); |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 1161 | |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 1162 | if ($vals{$v} ne ZERO_LENGTH_PROPERTY) { |
| 1163 | printProperty($f, $level+1, $v, $vals{$v}); |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1164 | } |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 1165 | else { |
| 1166 | printZeroLengthProperty($f, $level+1, $v); |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | #Print Includes |
| 1171 | foreach my $v (sort keys %vals) { |
| 1172 | |
| 1173 | if ($v eq "DTSI_INCLUDE") { |
| 1174 | #print 1 include per line |
| 1175 | my @incs = split(',', $vals{$v}); |
| 1176 | foreach my $i (@incs) { |
Matt Spinler | 41dcb62 | 2017-01-31 14:55:19 -0600 | [diff] [blame] | 1177 | print $f qq(#include "$i"\n); |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | #Print Nodes |
| 1183 | foreach my $v (sort keys %vals) { |
| 1184 | |
| 1185 | if (ref($vals{$v}) eq "HASH") { |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1186 | printNode($f, $level+1, $v, %{$vals{$v}}); |
| 1187 | } |
Matt Spinler | 995f2a2 | 2016-09-30 13:07:31 -0500 | [diff] [blame] | 1188 | #An array of nested nodes |
| 1189 | elsif (ref($vals{$v}) eq "ARRAY") { |
| 1190 | my @array = @{$vals{$v}}; |
| 1191 | &printNodes($f, $level+1, @array); |
| 1192 | } |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | print $f indent($level) . "};\n"; |
| 1196 | } |
| 1197 | |
| 1198 | |
| 1199 | #Prints a comma separated list of properties. |
| 1200 | #e.g. a = "b, c, d"; |
| 1201 | # $f = file handle |
| 1202 | # $level = indent level (0,1,etc) |
| 1203 | # $name = name of property |
| 1204 | # @vals = list of property values |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1205 | sub printPropertyList |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1206 | { |
| 1207 | my ($f, $level, $name, @vals) = @_; |
| 1208 | |
| 1209 | print $f indent($level) . "$name = "; |
| 1210 | |
| 1211 | for (my $i = 0;$i < scalar @vals; $i++) { |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1212 | print $f qq("$vals[$i]"); |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1213 | if ($i < (scalar(@vals) - 1)) { |
| 1214 | print $f ", "; |
| 1215 | } |
| 1216 | } |
| 1217 | print $f ";\n" |
| 1218 | } |
| 1219 | |
| 1220 | |
| 1221 | #Prints a single property. e.g. a = "b"; |
| 1222 | # $f = file handle |
| 1223 | # $level = indent level (0,1,etc) |
| 1224 | # $name = name of property |
| 1225 | # @vals = property values |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1226 | sub printProperty |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1227 | { |
| 1228 | my ($f, $level, $name, $val) = @_; |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1229 | my $quoteChar = qq("); |
Matt Spinler | 23d47c2 | 2016-10-04 12:31:21 -0500 | [diff] [blame] | 1230 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1231 | $val = convertReference($val); |
Matt Spinler | 23d47c2 | 2016-10-04 12:31:21 -0500 | [diff] [blame] | 1232 | |
| 1233 | #properties with < > or single word aliases don't need quotes |
| 1234 | if (($val =~ /<.*>/) || ($val =~ /^&\w+$/)) { |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1235 | $quoteChar = ""; |
Matt Spinler | 23d47c2 | 2016-10-04 12:31:21 -0500 | [diff] [blame] | 1236 | } |
| 1237 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1238 | print $f indent($level) . "$name = $quoteChar$val$quoteChar;\n"; |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1242 | #Prints a zero length property e.g. some-property; |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1243 | # $f = file handle |
| 1244 | # $level = indent level (0,1,etc) |
| 1245 | # $name = name of property |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1246 | sub printZeroLengthProperty |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1247 | { |
| 1248 | my ($f, $level, $name) = @_; |
| 1249 | print $f indent($level) . "$name;\n"; |
| 1250 | } |
| 1251 | |
| 1252 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1253 | #Replace '(ref)' with '&'. |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1254 | #Needed because Serverwiz doesn't properly escape '&'s in the XML, |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1255 | #so the '(ref)' string is used to represent the reference |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1256 | #specifier instead of '&'. |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1257 | sub convertReference |
| 1258 | { |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1259 | my $val = shift; |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1260 | $val =~ s/\(ref\)/&/g; |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1261 | return $val |
| 1262 | } |
| 1263 | |
| 1264 | |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1265 | #Prints the device tree version line. |
| 1266 | # $f = file handle |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1267 | sub printVersion |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1268 | { |
| 1269 | my $f = shift; |
| 1270 | print $f VERSION."\n" |
| 1271 | } |
| 1272 | |
| 1273 | |
| 1274 | #Prints the #include line for pulling in an include file. |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1275 | #The files to include come from the configuration file. |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1276 | # $f = file handle |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1277 | # $type = include type |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1278 | sub printIncludes |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1279 | { |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1280 | my ($f, $type) = @_; |
| 1281 | my @includes = getIncludes($type); |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1282 | |
| 1283 | foreach my $i (@includes) { |
| 1284 | #if a .dtsi, gets " ", otherwise < > |
| 1285 | if ($i =~ /\.dtsi$/) { |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1286 | $i = qq("$i"); |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1287 | } |
| 1288 | else { |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1289 | $i = "<$i>"; |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1290 | } |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1291 | print $f "#include $i\n"; |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1296 | #Returns an array of include files found in the config file |
| 1297 | #for the type specified. |
| 1298 | # $type = the include type, which is the section name in the |
| 1299 | # YAML configuration file. |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1300 | sub getIncludes |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1301 | { |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1302 | my $type = shift; |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1303 | my @includes; |
| 1304 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1305 | #The config file may have a section but no includes |
| 1306 | #listed in it, which is OK. |
| 1307 | if ((exists $g_configuration{includes}{$type}) && |
| 1308 | (ref($g_configuration{includes}{$type}) eq "ARRAY")) { |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1309 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1310 | @includes = @{$g_configuration{includes}{$type}}; |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | return @includes; |
| 1314 | } |
| 1315 | |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1316 | |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 1317 | #Appends the first value of the 'reg' property |
| 1318 | #passed in to the name passed in to create the |
| 1319 | #full name for the node |
| 1320 | # $name = node name that will be appended to |
| 1321 | # $reg = the reg property values |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1322 | sub makeNodeName |
Matt Spinler | 7490913 | 2016-10-07 13:52:19 -0500 | [diff] [blame] | 1323 | { |
| 1324 | my ($name, $reg) = @_; |
| 1325 | |
| 1326 | $reg =~ s/<//g; |
| 1327 | $reg =~ s/>//g; |
| 1328 | my @vals = split(' ', $reg); |
| 1329 | |
| 1330 | if (scalar @vals > 0) { |
| 1331 | $vals[0] =~ s/0x//; |
| 1332 | $name .= "@" . lc $vals[0]; |
| 1333 | } |
| 1334 | |
| 1335 | return $name; |
| 1336 | } |
| 1337 | |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1338 | |
| 1339 | #Prints the root node starting bracket. |
| 1340 | # $f = file handle |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1341 | sub printRootNodeStart |
| 1342 | { |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1343 | my $f = shift; |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1344 | print $f qq(/ {\n); |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | |
| 1348 | #Prints the root node ending bracket. |
| 1349 | # $f = file handle |
| 1350 | # $level = indent level (0,1,etc) |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1351 | sub printRootNodeEnd |
| 1352 | { |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1353 | my ($f, $level) = @_; |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1354 | print $f indent($level).qq(};\n); |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | |
| 1358 | #Returns a string that can be used to indent based on the |
| 1359 | #level passed in. Each level is an additional 4 spaces. |
| 1360 | # $level = indent level (0,1,etc) |
Matt Spinler | 889343f | 2017-01-30 14:14:11 -0600 | [diff] [blame] | 1361 | sub indent |
| 1362 | { |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1363 | my $level = shift; |
| 1364 | return ' ' x ($level * 4); |
| 1365 | } |
| 1366 | |
| 1367 | |
Matt Spinler | 9ac5cbe | 2017-01-31 14:33:25 -0600 | [diff] [blame] | 1368 | #Adds a {status} = "okay" element to the hash passed in. |
| 1369 | # $node = reference to the hash to add element to |
| 1370 | sub statusOK |
| 1371 | { |
| 1372 | my $node = shift; |
| 1373 | $node->{status} = "okay"; |
| 1374 | } |
| 1375 | |
| 1376 | |
| 1377 | #Adds the {reg} element to the hash passed in using the values |
| 1378 | #passed in. Resulting value looks like: "<val1 val2 etc>" |
| 1379 | # $node = reference to the hash to add element to |
| 1380 | # @values = the values for the property. May be passed in one at |
| 1381 | # a time and not as an array. |
| 1382 | sub addRegProp |
| 1383 | { |
| 1384 | my $node = shift; |
| 1385 | my @values = @_; |
| 1386 | |
| 1387 | $node->{reg} = "<"; |
| 1388 | for (my $i = 0; $i < scalar @values; $i++) { |
| 1389 | $node->{reg} .= $values[$i]; |
| 1390 | if ($i < (scalar @values) - 1) { |
| 1391 | $node->{reg} .= " "; |
| 1392 | } |
| 1393 | } |
| 1394 | $node->{reg} .= ">"; |
| 1395 | } |
| 1396 | |
| 1397 | |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1398 | sub printUsage |
| 1399 | { |
Matt Spinler | 30b461c | 2016-10-10 16:50:07 -0500 | [diff] [blame] | 1400 | print "gen_devtree.pl -x [XML filename] -y [yaml config file] " . |
| 1401 | "-o [output filename]\n"; |
Matt Spinler | 7d381e1 | 2016-09-27 14:27:24 -0500 | [diff] [blame] | 1402 | exit(1); |
| 1403 | } |