blob: 3f6c9d9e8d243734788e9d5f3b4610a3b8e7bae9 [file] [log] [blame]
Matt Spinler7d381e12016-09-27 14:27:24 -05001#!/usr/bin/env perl
2
Matt Spinler5cb5f4e2017-01-31 14:17:09 -06003#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 Spinler7d381e12016-09-27 14:27:24 -050013
14use strict;
Matt Spinler889343f2017-01-30 14:14:11 -060015use warnings;
Matt Spinler7d381e12016-09-27 14:27:24 -050016use XML::Simple;
17use mrw::Targets;
Matt Spinlere2bf4392017-01-30 13:16:28 -060018use mrw::Util;
Matt Spinler7d381e12016-09-27 14:27:24 -050019use Getopt::Long;
Matt Spinler30b461c2016-10-10 16:50:07 -050020use YAML::Tiny qw(LoadFile);
21use Scalar::Util qw(looks_like_number);
Matt Spinler7d381e12016-09-27 14:27:24 -050022
Matt Spinler30b461c2016-10-10 16:50:07 -050023use 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 Spinler6d391252017-01-31 13:46:06 -060028 POST_ROOT_INCLUDES => "post-root-node",
29 HOST_SPI_FLASH_MEM_REGION_NODE_LABEL => "flash_memory"
Matt Spinler30b461c2016-10-10 16:50:07 -050030};
Matt Spinler74909132016-10-07 13:52:19 -050031
Matt Spinler7d381e12016-09-27 14:27:24 -050032
33my $serverwizFile;
Matt Spinler30b461c2016-10-10 16:50:07 -050034my $configFile;
Matt Spinler7d381e12016-09-27 14:27:24 -050035my $outputFile;
36my $debug;
37
38GetOptions("x=s" => \$serverwizFile,
Matt Spinler30b461c2016-10-10 16:50:07 -050039 "y=s" => \$configFile,
Matt Spinler7d381e12016-09-27 14:27:24 -050040 "o=s" => \$outputFile,
41 "d" => \$debug)
42or printUsage();
43
Matt Spinler30b461c2016-10-10 16:50:07 -050044if ((not defined $serverwizFile) || (not defined $outputFile) ||
45 (not defined $configFile)) {
Matt Spinler7d381e12016-09-27 14:27:24 -050046 printUsage();
47}
48
Matt Spinler908e1822017-01-31 14:02:58 -060049my $g_pnorNodeName = undef;
Matt Spinler30b461c2016-10-10 16:50:07 -050050my %g_configuration = %{ LoadFile($configFile) };
51
Matt Spinler7d381e12016-09-27 14:27:24 -050052my $g_targetObj = Targets->new;
53$g_targetObj->loadXML($serverwizFile);
54
Matt Spinler74909132016-10-07 13:52:19 -050055my ($g_bmc, $g_bmcModel, $g_bmcMfgr, $g_systemName);
56setGlobalAttributes();
Matt Spinler7d381e12016-09-27 14:27:24 -050057
Matt Spinler30b461c2016-10-10 16:50:07 -050058my $g_i2cBusAdjust = 0;
59getI2CBusAdjust();
Matt Spinler7d381e12016-09-27 14:27:24 -050060
61open (my $f, ">$outputFile") or die "Could not open $outputFile\n";
62
63printVersion($f);
Matt Spinler30b461c2016-10-10 16:50:07 -050064printIncludes($f, PRE_ROOT_INCLUDES);
Matt Spinler7d381e12016-09-27 14:27:24 -050065printRootNodeStart($f);
66
67printPropertyList($f, 1, "model", getSystemBMCModel());
Matt Spinler7d381e12016-09-27 14:27:24 -050068printPropertyList($f, 1, "compatible", getBMCCompatibles());
Matt Spinler995f2a22016-09-30 13:07:31 -050069
Matt Spinler23d47c22016-10-04 12:31:21 -050070printNode($f, 1, "aliases", getAliases());
Matt Spinler7d381e12016-09-27 14:27:24 -050071printNode($f, 1, "chosen", getChosen());
Matt Spinler30b461c2016-10-10 16:50:07 -050072printNode($f, 1, "memory", getBmcMemory());
Matt Spinler6d391252017-01-31 13:46:06 -060073printNode($f, 1, "reserved-memory", getReservedMemory());
Matt Spinler7d381e12016-09-27 14:27:24 -050074
Matt Spinler995f2a22016-09-30 13:07:31 -050075printNode($f, 1, "leds", getLEDNode());
76
Matt Spinler30b461c2016-10-10 16:50:07 -050077printIncludes($f, ROOT_INCLUDES);
78
Matt Spinler7d381e12016-09-27 14:27:24 -050079printRootNodeEnd($f, 0);
80
Matt Spinler96f8f242016-11-28 16:26:57 -060081printNodes($f, 0, getBMCFlashNodes());
Matt Spinler96f8f242016-11-28 16:26:57 -060082printNodes($f, 0, getOtherFlashNodes());
83
Matt Spinler908e1822017-01-31 14:02:58 -060084printNode($f, 0, "lpc_ctrl", getLPCNode());
85printNode($f, 0, "mbox", getMBoxNode());
86
Matt Spinler995f2a22016-09-30 13:07:31 -050087printNodes($f, 0, getUARTNodes());
Matt Spinler41dcb622017-01-31 14:55:19 -060088printNodes($f, 0, getMacNodes());
89
90printNodes($f, 0, getI2CNodes());
Matt Spinler7d381e12016-09-27 14:27:24 -050091printNodes($f, 0, getVuartNodes());
92
Matt Spinler30b461c2016-10-10 16:50:07 -050093printIncludes($f, POST_ROOT_INCLUDES);
94
Matt Spinler7d381e12016-09-27 14:27:24 -050095close $f;
96exit 0;
97
98
Matt Spinler74909132016-10-07 13:52:19 -050099#Finds the values for these globals:
100# $g_bmc, $g_bmcModel, $g_bmcMfgr, $g_systemName
Matt Spinler889343f2017-01-30 14:14:11 -0600101sub setGlobalAttributes
Matt Spinler74909132016-10-07 13:52:19 -0500102{
Matt Spinlere2bf4392017-01-30 13:16:28 -0600103 $g_bmc = Util::getBMCTarget($g_targetObj);
Matt Spinler74909132016-10-07 13:52:19 -0500104
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 Spinler23d47c22016-10-04 12:31:21 -0500122#Returns a hash that represents the 'aliases' node.
123#Will look like:
124# aliases {
125# name1 = &val1;
126# name2 = &val2;
127# ...
128# }
Matt Spinler889343f2017-01-30 14:14:11 -0600129sub getAliases
Matt Spinler23d47c22016-10-04 12:31:21 -0500130{
131 my %aliases;
Matt Spinler23d47c22016-10-04 12:31:21 -0500132
Matt Spinler30b461c2016-10-10 16:50:07 -0500133 #Get the info from the config file
Matt Spinler23d47c22016-10-04 12:31:21 -0500134
Matt Spinler30b461c2016-10-10 16:50:07 -0500135 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 Spinler23d47c22016-10-04 12:31:21 -0500146 }
147 }
148
149 return %aliases;
150}
151
Matt Spinler7d381e12016-09-27 14:27:24 -0500152
153#Return a hash that represents the 'chosen' node
Matt Spinler995f2a22016-09-30 13:07:31 -0500154#Will look like:
155# chosen {
156# stdout-path = ...
157# bootargs = ...
158# }
Matt Spinler889343f2017-01-30 14:14:11 -0600159sub getChosen
Matt Spinler7d381e12016-09-27 14:27:24 -0500160{
Matt Spinler7d381e12016-09-27 14:27:24 -0500161 my %chosen;
Matt Spinler30b461c2016-10-10 16:50:07 -0500162 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 Spinler30b461c2016-10-10 16:50:07 -0500171 foreach my $key (keys %chosen) {
Matt Spinler30b461c2016-10-10 16:50:07 -0500172
Matt Spinler28fb1a92017-01-30 12:54:10 -0600173 #Check for allowed entries. Empty is OK.
174 if (!grep(/^$key$/, @allowed)) {
Matt Spinler30b461c2016-10-10 16:50:07 -0500175 die "Invalid entry $key in 'chosen' section in config file\n";
176 }
Matt Spinler28fb1a92017-01-30 12:54:10 -0600177
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 Spinler30b461c2016-10-10 16:50:07 -0500182 }
183
Matt Spinler7d381e12016-09-27 14:27:24 -0500184 return %chosen;
185}
186
187
Matt Spinler30b461c2016-10-10 16:50:07 -0500188#Return a hash that represents the 'memory' node.
Matt Spinler995f2a22016-09-30 13:07:31 -0500189#Will look like:
Matt Spinler30b461c2016-10-10 16:50:07 -0500190# memory {
191# reg = < base size >
192# }
Matt Spinler889343f2017-01-30 14:14:11 -0600193sub getBmcMemory
Matt Spinler30b461c2016-10-10 16:50:07 -0500194{
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 Spinler9ac5cbe2017-01-31 14:33:25 -0600217 addRegProp(\%memory,
218 $g_configuration{memory}{base},
219 $g_configuration{memory}{size});
Matt Spinler30b461c2016-10-10 16:50:07 -0500220
221 return %memory;
222}
223
224
Matt Spinler6d391252017-01-31 13:46:06 -0600225#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# };
238sub 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.
261sub 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 Spinler9ac5cbe2017-01-31 14:33:25 -0600297 addRegProp(\%node, $base, $size);
Matt Spinler6d391252017-01-31 13:46:06 -0600298 my $name = makeNodeName("region", $node{reg});
299
300 return ($name, \%node);
301}
302
303
Matt Spinler25d60bb2016-10-31 15:16:03 -0500304#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 Spinler889343f2017-01-30 14:14:11 -0600307sub getBMCFlashNodes
Matt Spinler7d381e12016-09-27 14:27:24 -0500308{
Matt Spinler25d60bb2016-10-31 15:16:03 -0500309 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 Spinler889343f2017-01-30 14:14:11 -0600338sub getAST2500BMCSPIFlashNode
Matt Spinler25d60bb2016-10-31 15:16:03 -0500339{
340 my %bmcFlash;
341 my $chipSelect = 0;
342 my $lastUnit = "";
343
Matt Spinler18d5f572016-11-15 15:25:45 -0600344 my $connections = $g_targetObj->findConnections($g_bmc, "SPI", "FLASH");
Matt Spinler25d60bb2016-10-31 15:16:03 -0500345
346 if ($connections eq "") {
347 die "ERROR: No BMC SPI flashes found connected to the BMC\n";
348 }
349
Matt Spinler9ac5cbe2017-01-31 14:33:25 -0600350 statusOK(\%{$bmcFlash{fmc}});
Matt Spinler25d60bb2016-10-31 15:16:03 -0500351
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 Spinlerc0dff8a2016-11-02 15:47:30 -0500364 $bmcFlash{fmc}{$flashName}{COMMENT} = connectionComment($spi);
Matt Spinler25d60bb2016-10-31 15:16:03 -0500365
Matt Spinler9ac5cbe2017-01-31 14:33:25 -0600366 statusOK(\%{$bmcFlash{fmc}{$flashName}});
Matt Spinler25d60bb2016-10-31 15:16:03 -0500367
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 Spinler889343f2017-01-30 14:14:11 -0600407sub addBMCFlashConfigProperties
Matt Spinler25d60bb2016-10-31 15:16:03 -0500408{
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 Spinler995f2a22016-09-30 13:07:31 -0500417}
418
419
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500420#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 Spinler889343f2017-01-30 14:14:11 -0600424sub getOtherFlashNodes
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500425{
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 Spinler889343f2017-01-30 14:14:11 -0600443sub getAST2500SpiFlashNodes
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500444{
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 Spinler908e1822017-01-31 14:02:58 -0600452 my ($node, $foundPNOR) = getAST2500SpiMasterNode($unit);
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500453
Matt Spinler908e1822017-01-31 14:02:58 -0600454 if (keys %$node) {
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500455 my %spiNode;
456 my $nodeName = "spi$unit";
Matt Spinler908e1822017-01-31 14:02:58 -0600457 $spiNode{$nodeName} = { %$node };
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500458 push @nodes, { %spiNode };
Matt Spinler908e1822017-01-31 14:02:58 -0600459
460 #Save off the PNOR SPI node name for use by LPC node
461 if ($foundPNOR) {
462 $g_pnorNodeName = $nodeName;
463 }
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500464 }
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 Spinler889343f2017-01-30 14:14:11 -0600483sub getAST2500SpiMasterNode
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500484{
485 my $spiNum = shift;
486 my %spiMaster;
487 my $chipSelect = 0;
Matt Spinler908e1822017-01-31 14:02:58 -0600488 my $foundPNOR = 0;
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500489
Matt Spinler18d5f572016-11-15 15:25:45 -0600490 my $connections = $g_targetObj->findConnections($g_bmc, "SPI", "FLASH");
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500491
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 Spinler9ac5cbe2017-01-31 14:33:25 -0600503 statusOK(\%spiMaster);
Matt Spinler2efdcba2016-11-08 15:37:20 -0600504
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 Spinlerc0dff8a2016-11-02 15:47:30 -0500511 my $flashName = "flash@".$chipSelect;
512
513 $spiMaster{$flashName}{COMMENT} = connectionComment($spi);
514
Matt Spinler9ac5cbe2017-01-31 14:33:25 -0600515 statusOK(\%{$spiMaster{$flashName}});
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500516
Matt Spinler0eda4882016-11-30 15:20:11 -0600517 #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 Spinler908e1822017-01-31 14:02:58 -0600522 $foundPNOR = 1;
Matt Spinler0eda4882016-11-30 15:20:11 -0600523 }
524
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500525 $chipSelect++;
526 }
527 }
528
Matt Spinler908e1822017-01-31 14:02:58 -0600529 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# }
540sub getMBoxNode
541{
542 my %node;
543 if (exists $g_configuration{"lpc-host-spi-flash-mailbox"}) {
Matt Spinler9ac5cbe2017-01-31 14:33:25 -0600544 statusOK(\%node);
Matt Spinler908e1822017-01-31 14:02:58 -0600545 }
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#};
559sub getLPCNode
560{
561 my %node;
562 if (exists $g_configuration{"lpc-host-spi-flash-mailbox"}) {
563
Matt Spinler9ac5cbe2017-01-31 14:33:25 -0600564 statusOK(\%node);
Matt Spinler908e1822017-01-31 14:02:58 -0600565
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 Spinlerc0dff8a2016-11-02 15:47:30 -0500579}
580
581
Matt Spinler995f2a22016-09-30 13:07:31 -0500582#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 Spinler889343f2017-01-30 14:14:11 -0600592sub getLEDNode
Matt Spinler995f2a22016-09-30 13:07:31 -0500593{
594 my %leds;
595
Matt Spinlereca7f062016-11-07 09:59:23 -0600596 $leds{compatible} = "gpio-leds";
Matt Spinler995f2a22016-09-30 13:07:31 -0500597
Matt Spinler18d5f572016-11-15 15:25:45 -0600598 my $connections = $g_targetObj->findConnections($g_bmc, "GPIO", "LED");
Matt Spinler995f2a22016-09-30 13:07:31 -0500599
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 Spinlerc0dff8a2016-11-02 15:47:30 -0500608 $ledNode{COMMENT} = connectionComment($gpio);
Matt Spinler995f2a22016-09-30 13:07:31 -0500609
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 Spinler889343f2017-01-30 14:14:11 -0600637sub getGpioActiveString
638{
Matt Spinler995f2a22016-09-30 13:07:31 -0500639 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 Spinler889343f2017-01-30 14:14:11 -0600653sub getAspeedGpioMacro
654{
Matt Spinler995f2a22016-09-30 13:07:31 -0500655 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 Spinler889343f2017-01-30 14:14:11 -0600691sub getUARTNodes
Matt Spinler995f2a22016-09-30 13:07:31 -0500692{
693 my @nodes;
694
Matt Spinler23d47c22016-10-04 12:31:21 -0500695 #Using U750 for legacy MRW reasons
Matt Spinler18d5f572016-11-15 15:25:45 -0600696 my $connections = $g_targetObj->findConnections($g_bmc, "U750");
Matt Spinler995f2a22016-09-30 13:07:31 -0500697
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 Spinler9ac5cbe2017-01-31 14:33:25 -0600709 statusOK(\%{$node{$name}});
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500710 $node{$name}{COMMENT} = connectionComment($uart);
Matt Spinler995f2a22016-09-30 13:07:31 -0500711
Matt Spinler2efdcba2016-11-08 15:37:20 -0600712 #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 Spinler995f2a22016-09-30 13:07:31 -0500718 push @nodes, { %node };
719 }
720
Matt Spinler7d381e12016-09-27 14:27:24 -0500721 return @nodes;
722}
723
724
Matt Spinler995f2a22016-09-30 13:07:31 -0500725#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 Spinler889343f2017-01-30 14:14:11 -0600731sub getMacNodes
Matt Spinler995f2a22016-09-30 13:07:31 -0500732{
733 my @nodes;
734
Matt Spinler18d5f572016-11-15 15:25:45 -0600735 my $connections = $g_targetObj->findConnections($g_bmc, "ETHERNET");
Matt Spinler995f2a22016-09-30 13:07:31 -0500736
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 Spinler9ac5cbe2017-01-31 14:33:25 -0600751 statusOK(\%{$node{$name}});
Matt Spinler995f2a22016-09-30 13:07:31 -0500752
753 if ($ncsi == 1) {
Matt Spinler74909132016-10-07 13:52:19 -0500754 $node{$name}{"use-ncsi"} = ZERO_LENGTH_PROPERTY;
Matt Spinler995f2a22016-09-30 13:07:31 -0500755 }
756 if ($hwChecksum == 0) {
Matt Spinler74909132016-10-07 13:52:19 -0500757 $node{$name}{"no-hw-checksum"} = ZERO_LENGTH_PROPERTY;
Matt Spinler995f2a22016-09-30 13:07:31 -0500758 }
759
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500760 $node{$name}{COMMENT} = connectionComment($eth);
Matt Spinler995f2a22016-09-30 13:07:31 -0500761
Matt Spinler2efdcba2016-11-08 15:37:20 -0600762 #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 Spinler995f2a22016-09-30 13:07:31 -0500768 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 Spinler889343f2017-01-30 14:14:11 -0600780sub getVuartNodes
Matt Spinler7d381e12016-09-27 14:27:24 -0500781{
782 my @nodes;
783 my %node;
784
785 #For now, enable 1 node all the time.
Matt Spinler995f2a22016-09-30 13:07:31 -0500786 #TBD if this needs to be fixed
Matt Spinler9ac5cbe2017-01-31 14:33:25 -0600787 statusOK(\%{$node{vuart}});
Matt Spinler7d381e12016-09-27 14:27:24 -0500788
789 push @nodes, { %node };
790
791 return @nodes;
792}
793
Matt Spinler74909132016-10-07 13:52:19 -0500794#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 Spinler889343f2017-01-30 14:14:11 -0600814sub getI2CNodes
Matt Spinler74909132016-10-07 13:52:19 -0500815{
816 my @nodes;
817 my %busNodes;
818
Matt Spinler18d5f572016-11-15 15:25:45 -0600819 my $connections = $g_targetObj->findConnections($g_bmc, "I2C");
Matt Spinler74909132016-10-07 13:52:19 -0500820
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 Spinlerc0dff8a2016-11-02 15:47:30 -0500830 $deviceNode{COMMENT} = connectionComment($i2c);
Matt Spinler74909132016-10-07 13:52:19 -0500831
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 Spinler9ac5cbe2017-01-31 14:33:25 -0600845 addRegProp(\%deviceNode, $i2cAddress);
Matt Spinler74909132016-10-07 13:52:19 -0500846
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 Spinler30b461c2016-10-10 16:50:07 -0500862 $busNum += $g_i2cBusAdjust;
Matt Spinler74909132016-10-07 13:52:19 -0500863
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 Spinler9ac5cbe2017-01-31 14:33:25 -0600885 statusOK(\%{$busNodes{$busNodeName}{$busNodeName}});
Matt Spinler74909132016-10-07 13:52:19 -0500886 $busNodes{$busNodeName}{$busNodeName}{$deviceName} = { %deviceNode };
Matt Spinler2efdcba2016-11-08 15:37:20 -0600887
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 Spinler74909132016-10-07 13:52:19 -0500893 }
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 Spinler889343f2017-01-30 14:14:11 -0600909sub getPartDefinedDTProperties
Matt Spinler74909132016-10-07 13:52:19 -0500910{
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 Spinler889343f2017-01-30 14:14:11 -0600948sub adjustI2CAddress
Matt Spinler74909132016-10-07 13:52:19 -0500949{
950 my $addr = shift;
951
952 #MRW holds the 8 bit value. We need the 7 bit one.
Matt Spinler96f8f242016-11-28 16:26:57 -0600953 $addr = $addr >> 1;
Matt Spinler74909132016-10-07 13:52:19 -0500954 $addr = sprintf("0x%X", $addr);
955 $addr = lc $addr;
956
957 return $addr;
958}
959
960
Matt Spinler30b461c2016-10-10 16:50:07 -0500961#Sets the global $g_i2cBusAdjust from the configuration file.
Matt Spinler889343f2017-01-30 14:14:11 -0600962sub getI2CBusAdjust
Matt Spinler74909132016-10-07 13:52:19 -0500963{
Matt Spinler30b461c2016-10-10 16:50:07 -0500964 if (exists $g_configuration{"i2c-bus-adjust"}) {
Matt Spinler74909132016-10-07 13:52:19 -0500965
Matt Spinler30b461c2016-10-10 16:50:07 -0500966 $g_i2cBusAdjust = $g_configuration{"i2c-bus-adjust"};
Matt Spinler74909132016-10-07 13:52:19 -0500967
Matt Spinler30b461c2016-10-10 16:50:07 -0500968 if (!looks_like_number($g_i2cBusAdjust)) {
969 die "ERROR: Invalid i2c-bus-adjust value $g_i2cBusAdjust " .
970 "found in config file.\n";
Matt Spinler7d381e12016-09-27 14:27:24 -0500971 }
972 }
Matt Spinler30b461c2016-10-10 16:50:07 -0500973 else {
974 $g_i2cBusAdjust = 0;
975 print "WARNING: No I2C Bus number adjustment done " .
976 "for this system.\n";
977 }
Matt Spinler7d381e12016-09-27 14:27:24 -0500978}
979
980
Matt Spinler2efdcba2016-11-08 15:37:20 -0600981
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 Spinler889343f2017-01-30 14:14:11 -0600990sub addPinCtrlProps
Matt Spinler2efdcba2016-11-08 15:37:20 -0600991{
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 Spinler889343f2017-01-30 14:14:11 -06001010sub makePinCtrl0PropValue
Matt Spinler2efdcba2016-11-08 15:37:20 -06001011{
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 Spinler7d381e12016-09-27 14:27:24 -05001038#Returns a list of compatible fields for the BMC itself.
Matt Spinler889343f2017-01-30 14:14:11 -06001039sub getBMCCompatibles
Matt Spinler7d381e12016-09-27 14:27:24 -05001040{
1041 my @compats;
1042
Matt Spinler23d47c22016-10-04 12:31:21 -05001043 #1st entry: <system mfgr>,<system name>-bmc
1044 #2nd entry: <bmc mfgr>,<bmc model>
Matt Spinler7d381e12016-09-27 14:27:24 -05001045
Matt Spinler23d47c22016-10-04 12:31:21 -05001046 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 Spinler7d381e12016-09-27 14:27:24 -05001052 }
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 Spinler889343f2017-01-30 14:14:11 -06001061sub getSystemBMCModel
Matt Spinler7d381e12016-09-27 14:27:24 -05001062{
Matt Spinler995f2a22016-09-30 13:07:31 -05001063 #'<System> BMC'
Matt Spinler7d381e12016-09-27 14:27:24 -05001064 my $sys = lc $g_systemName;
1065 $sys = uc(substr($sys, 0, 1)) . substr($sys, 1);
1066
1067 return $sys . " BMC";
1068}
1069
Matt Spinlerc0dff8a2016-11-02 15:47:30 -05001070#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 Spinler889343f2017-01-30 14:14:11 -06001076sub connectionComment
Matt Spinlerc0dff8a2016-11-02 15:47:30 -05001077{
1078 my $conn = shift;
1079 my $comment = "$conn->{SOURCE} ->\n$conn->{DEST_PARENT}";
1080 return $comment;
1081}
1082
Matt Spinler7d381e12016-09-27 14:27:24 -05001083
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 Spinler889343f2017-01-30 14:14:11 -06001089sub printNodes
Matt Spinler7d381e12016-09-27 14:27:24 -05001090{
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 Spinler995f2a22016-09-30 13:07:31 -05001112# - 'COMMENT', then value gets turned into a // comment
Matt Spinler74909132016-10-07 13:52:19 -05001113# - 'ZERO_LENGTH_PROPERTY' then value gets turned into: value;
Matt Spinler7d381e12016-09-27 14:27:24 -05001114#
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 Spinler995f2a22016-09-30 13:07:31 -05001118# - an array of hashes indicates an array of child nodes
Matt Spinler889343f2017-01-30 14:14:11 -06001119sub printNode
Matt Spinler7d381e12016-09-27 14:27:24 -05001120{
1121 my ($f, $level, $name, %vals) = @_;
1122 my $include = "";
1123
Matt Spinlerc0dff8a2016-11-02 15:47:30 -05001124 #No reason to print an empty node
1125 if (!keys %vals) {
1126 return;
1127 }
1128
Matt Spinler7d381e12016-09-27 14:27:24 -05001129 if ($level == 0) {
1130 $name = "&".$name;
1131 }
1132
Matt Spinler995f2a22016-09-30 13:07:31 -05001133 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 Spinler6d391252017-01-31 13:46:06 -06001142 #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 Spinler7d381e12016-09-27 14:27:24 -05001150
Matt Spinler74909132016-10-07 13:52:19 -05001151 #First print properties, then includes, then subnodes
1152
1153 #Print Properties
Matt Spinler7d381e12016-09-27 14:27:24 -05001154 foreach my $v (sort keys %vals) {
1155
Matt Spinler995f2a22016-09-30 13:07:31 -05001156 next if ($v eq "COMMENT");
Matt Spinler74909132016-10-07 13:52:19 -05001157 next if ($v eq "DTSI_INCLUDE");
Matt Spinler6d391252017-01-31 13:46:06 -06001158 next if ($v eq "NODE_LABEL");
Matt Spinler74909132016-10-07 13:52:19 -05001159 next if (ref($vals{$v}) eq "HASH");
1160 next if (ref($vals{$v}) eq "ARRAY");
Matt Spinler995f2a22016-09-30 13:07:31 -05001161
Matt Spinler74909132016-10-07 13:52:19 -05001162 if ($vals{$v} ne ZERO_LENGTH_PROPERTY) {
1163 printProperty($f, $level+1, $v, $vals{$v});
Matt Spinler7d381e12016-09-27 14:27:24 -05001164 }
Matt Spinler74909132016-10-07 13:52:19 -05001165 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 Spinler41dcb622017-01-31 14:55:19 -06001177 print $f qq(#include "$i"\n);
Matt Spinler74909132016-10-07 13:52:19 -05001178 }
1179 }
1180 }
1181
1182 #Print Nodes
1183 foreach my $v (sort keys %vals) {
1184
1185 if (ref($vals{$v}) eq "HASH") {
Matt Spinler7d381e12016-09-27 14:27:24 -05001186 printNode($f, $level+1, $v, %{$vals{$v}});
1187 }
Matt Spinler995f2a22016-09-30 13:07:31 -05001188 #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 Spinler7d381e12016-09-27 14:27:24 -05001193 }
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 Spinler889343f2017-01-30 14:14:11 -06001205sub printPropertyList
Matt Spinler7d381e12016-09-27 14:27:24 -05001206{
1207 my ($f, $level, $name, @vals) = @_;
1208
1209 print $f indent($level) . "$name = ";
1210
1211 for (my $i = 0;$i < scalar @vals; $i++) {
Matt Spinler30b461c2016-10-10 16:50:07 -05001212 print $f qq("$vals[$i]");
Matt Spinler7d381e12016-09-27 14:27:24 -05001213 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 Spinler889343f2017-01-30 14:14:11 -06001226sub printProperty
Matt Spinler7d381e12016-09-27 14:27:24 -05001227{
1228 my ($f, $level, $name, $val) = @_;
Matt Spinler30b461c2016-10-10 16:50:07 -05001229 my $quoteChar = qq(");
Matt Spinler23d47c22016-10-04 12:31:21 -05001230
Matt Spinler30b461c2016-10-10 16:50:07 -05001231 $val = convertReference($val);
Matt Spinler23d47c22016-10-04 12:31:21 -05001232
1233 #properties with < > or single word aliases don't need quotes
1234 if (($val =~ /<.*>/) || ($val =~ /^&\w+$/)) {
Matt Spinler30b461c2016-10-10 16:50:07 -05001235 $quoteChar = "";
Matt Spinler23d47c22016-10-04 12:31:21 -05001236 }
1237
Matt Spinler30b461c2016-10-10 16:50:07 -05001238 print $f indent($level) . "$name = $quoteChar$val$quoteChar;\n";
Matt Spinler7d381e12016-09-27 14:27:24 -05001239}
1240
1241
Matt Spinler30b461c2016-10-10 16:50:07 -05001242#Prints a zero length property e.g. some-property;
Matt Spinler7d381e12016-09-27 14:27:24 -05001243# $f = file handle
1244# $level = indent level (0,1,etc)
1245# $name = name of property
Matt Spinler889343f2017-01-30 14:14:11 -06001246sub printZeroLengthProperty
Matt Spinler7d381e12016-09-27 14:27:24 -05001247{
1248 my ($f, $level, $name) = @_;
1249 print $f indent($level) . "$name;\n";
1250}
1251
1252
Matt Spinler30b461c2016-10-10 16:50:07 -05001253#Replace '(ref)' with '&'.
Matt Spinler7d381e12016-09-27 14:27:24 -05001254#Needed because Serverwiz doesn't properly escape '&'s in the XML,
Matt Spinler30b461c2016-10-10 16:50:07 -05001255#so the '(ref)' string is used to represent the reference
Matt Spinler7d381e12016-09-27 14:27:24 -05001256#specifier instead of '&'.
Matt Spinler889343f2017-01-30 14:14:11 -06001257sub convertReference
1258{
Matt Spinler7d381e12016-09-27 14:27:24 -05001259 my $val = shift;
Matt Spinler30b461c2016-10-10 16:50:07 -05001260 $val =~ s/\(ref\)/&/g;
Matt Spinler7d381e12016-09-27 14:27:24 -05001261 return $val
1262}
1263
1264
Matt Spinler7d381e12016-09-27 14:27:24 -05001265#Prints the device tree version line.
1266# $f = file handle
Matt Spinler889343f2017-01-30 14:14:11 -06001267sub printVersion
Matt Spinler7d381e12016-09-27 14:27:24 -05001268{
1269 my $f = shift;
1270 print $f VERSION."\n"
1271}
1272
1273
1274#Prints the #include line for pulling in an include file.
Matt Spinler30b461c2016-10-10 16:50:07 -05001275#The files to include come from the configuration file.
Matt Spinler7d381e12016-09-27 14:27:24 -05001276# $f = file handle
Matt Spinler30b461c2016-10-10 16:50:07 -05001277# $type = include type
Matt Spinler889343f2017-01-30 14:14:11 -06001278sub printIncludes
Matt Spinler7d381e12016-09-27 14:27:24 -05001279{
Matt Spinler30b461c2016-10-10 16:50:07 -05001280 my ($f, $type) = @_;
1281 my @includes = getIncludes($type);
Matt Spinler7d381e12016-09-27 14:27:24 -05001282
1283 foreach my $i (@includes) {
1284 #if a .dtsi, gets " ", otherwise < >
1285 if ($i =~ /\.dtsi$/) {
Matt Spinler30b461c2016-10-10 16:50:07 -05001286 $i = qq("$i");
Matt Spinler7d381e12016-09-27 14:27:24 -05001287 }
1288 else {
Matt Spinler30b461c2016-10-10 16:50:07 -05001289 $i = "<$i>";
Matt Spinler7d381e12016-09-27 14:27:24 -05001290 }
Matt Spinler30b461c2016-10-10 16:50:07 -05001291 print $f "#include $i\n";
Matt Spinler7d381e12016-09-27 14:27:24 -05001292 }
1293}
1294
1295
Matt Spinler30b461c2016-10-10 16:50:07 -05001296#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 Spinler889343f2017-01-30 14:14:11 -06001300sub getIncludes
Matt Spinler7d381e12016-09-27 14:27:24 -05001301{
Matt Spinler30b461c2016-10-10 16:50:07 -05001302 my $type = shift;
Matt Spinler7d381e12016-09-27 14:27:24 -05001303 my @includes;
1304
Matt Spinler30b461c2016-10-10 16:50:07 -05001305 #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 Spinler7d381e12016-09-27 14:27:24 -05001309
Matt Spinler30b461c2016-10-10 16:50:07 -05001310 @includes = @{$g_configuration{includes}{$type}};
Matt Spinler7d381e12016-09-27 14:27:24 -05001311 }
1312
1313 return @includes;
1314}
1315
Matt Spinler30b461c2016-10-10 16:50:07 -05001316
Matt Spinler74909132016-10-07 13:52:19 -05001317#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 Spinler889343f2017-01-30 14:14:11 -06001322sub makeNodeName
Matt Spinler74909132016-10-07 13:52:19 -05001323{
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 Spinler7d381e12016-09-27 14:27:24 -05001338
1339#Prints the root node starting bracket.
1340# $f = file handle
Matt Spinler889343f2017-01-30 14:14:11 -06001341sub printRootNodeStart
1342{
Matt Spinler7d381e12016-09-27 14:27:24 -05001343 my $f = shift;
Matt Spinler30b461c2016-10-10 16:50:07 -05001344 print $f qq(/ {\n);
Matt Spinler7d381e12016-09-27 14:27:24 -05001345}
1346
1347
1348#Prints the root node ending bracket.
1349# $f = file handle
1350# $level = indent level (0,1,etc)
Matt Spinler889343f2017-01-30 14:14:11 -06001351sub printRootNodeEnd
1352{
Matt Spinler7d381e12016-09-27 14:27:24 -05001353 my ($f, $level) = @_;
Matt Spinler30b461c2016-10-10 16:50:07 -05001354 print $f indent($level).qq(};\n);
Matt Spinler7d381e12016-09-27 14:27:24 -05001355}
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 Spinler889343f2017-01-30 14:14:11 -06001361sub indent
1362{
Matt Spinler7d381e12016-09-27 14:27:24 -05001363 my $level = shift;
1364 return ' ' x ($level * 4);
1365}
1366
1367
Matt Spinler9ac5cbe2017-01-31 14:33:25 -06001368#Adds a {status} = "okay" element to the hash passed in.
1369# $node = reference to the hash to add element to
1370sub 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.
1382sub 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 Spinler7d381e12016-09-27 14:27:24 -05001398sub printUsage
1399{
Matt Spinler30b461c2016-10-10 16:50:07 -05001400 print "gen_devtree.pl -x [XML filename] -y [yaml config file] " .
1401 "-o [output filename]\n";
Matt Spinler7d381e12016-09-27 14:27:24 -05001402 exit(1);
1403}