blob: f7241b7a29a35b4fe0e74d343ab7b530b84fcfe7 [file] [log] [blame]
Matt Spinler7d381e12016-09-27 14:27:24 -05001#!/usr/bin/env perl
2
3#Generates a BMC device tree syntax file from the machine
4#readable workbook.
5
6use strict;
7use XML::Simple;
8use mrw::Targets;
9use Getopt::Long;
Matt Spinler30b461c2016-10-10 16:50:07 -050010use YAML::Tiny qw(LoadFile);
11use Scalar::Util qw(looks_like_number);
Matt Spinler7d381e12016-09-27 14:27:24 -050012
Matt Spinler30b461c2016-10-10 16:50:07 -050013use constant {
14 VERSION => "/dts-v1/;",
15 ZERO_LENGTH_PROPERTY => "zero_length_property",
16 PRE_ROOT_INCLUDES => "pre-root-node",
17 ROOT_INCLUDES => "root-node",
18 POST_ROOT_INCLUDES => "post-root-node"
19};
Matt Spinler74909132016-10-07 13:52:19 -050020
Matt Spinler7d381e12016-09-27 14:27:24 -050021
22my $serverwizFile;
Matt Spinler30b461c2016-10-10 16:50:07 -050023my $configFile;
Matt Spinler7d381e12016-09-27 14:27:24 -050024my $outputFile;
25my $debug;
26
27GetOptions("x=s" => \$serverwizFile,
Matt Spinler30b461c2016-10-10 16:50:07 -050028 "y=s" => \$configFile,
Matt Spinler7d381e12016-09-27 14:27:24 -050029 "o=s" => \$outputFile,
30 "d" => \$debug)
31or printUsage();
32
Matt Spinler30b461c2016-10-10 16:50:07 -050033if ((not defined $serverwizFile) || (not defined $outputFile) ||
34 (not defined $configFile)) {
Matt Spinler7d381e12016-09-27 14:27:24 -050035 printUsage();
36}
37
Matt Spinler30b461c2016-10-10 16:50:07 -050038my %g_configuration = %{ LoadFile($configFile) };
39
Matt Spinler7d381e12016-09-27 14:27:24 -050040my $g_targetObj = Targets->new;
41$g_targetObj->loadXML($serverwizFile);
42
Matt Spinler74909132016-10-07 13:52:19 -050043my ($g_bmc, $g_bmcModel, $g_bmcMfgr, $g_systemName);
44setGlobalAttributes();
Matt Spinler7d381e12016-09-27 14:27:24 -050045
Matt Spinler30b461c2016-10-10 16:50:07 -050046my $g_i2cBusAdjust = 0;
47getI2CBusAdjust();
Matt Spinler7d381e12016-09-27 14:27:24 -050048
49open (my $f, ">$outputFile") or die "Could not open $outputFile\n";
50
51printVersion($f);
Matt Spinler30b461c2016-10-10 16:50:07 -050052printIncludes($f, PRE_ROOT_INCLUDES);
Matt Spinler7d381e12016-09-27 14:27:24 -050053printRootNodeStart($f);
54
55printPropertyList($f, 1, "model", getSystemBMCModel());
Matt Spinler7d381e12016-09-27 14:27:24 -050056printPropertyList($f, 1, "compatible", getBMCCompatibles());
Matt Spinler995f2a22016-09-30 13:07:31 -050057
Matt Spinler23d47c22016-10-04 12:31:21 -050058printNode($f, 1, "aliases", getAliases());
Matt Spinler7d381e12016-09-27 14:27:24 -050059printNode($f, 1, "chosen", getChosen());
Matt Spinler30b461c2016-10-10 16:50:07 -050060printNode($f, 1, "memory", getBmcMemory());
Matt Spinler7d381e12016-09-27 14:27:24 -050061
Matt Spinler25d60bb2016-10-31 15:16:03 -050062printNodes($f, 1, getBMCFlashNodes());
Matt Spinler995f2a22016-09-30 13:07:31 -050063
Matt Spinlerc0dff8a2016-11-02 15:47:30 -050064printNodes($f, 1, getOtherFlashNodes());
65
Matt Spinler995f2a22016-09-30 13:07:31 -050066printNode($f, 1, "leds", getLEDNode());
67
Matt Spinler30b461c2016-10-10 16:50:07 -050068printIncludes($f, ROOT_INCLUDES);
69
Matt Spinler7d381e12016-09-27 14:27:24 -050070printRootNodeEnd($f, 0);
71
Matt Spinler74909132016-10-07 13:52:19 -050072printNodes($f, 0, getI2CNodes());
Matt Spinler7d381e12016-09-27 14:27:24 -050073printNodes($f, 0, getMacNodes());
Matt Spinler995f2a22016-09-30 13:07:31 -050074printNodes($f, 0, getUARTNodes());
Matt Spinler7d381e12016-09-27 14:27:24 -050075printNodes($f, 0, getVuartNodes());
76
Matt Spinler30b461c2016-10-10 16:50:07 -050077printIncludes($f, POST_ROOT_INCLUDES);
78
Matt Spinler7d381e12016-09-27 14:27:24 -050079close $f;
80exit 0;
81
82
Matt Spinler74909132016-10-07 13:52:19 -050083#Finds the values for these globals:
84# $g_bmc, $g_bmcModel, $g_bmcMfgr, $g_systemName
85sub setGlobalAttributes()
86{
87 $g_bmc = getBMCTarget();
88 if (length($g_bmc) == 0) {
89 die "Unable to find a BMC in this system\n";
90 }
91
92 if ($g_targetObj->isBadAttribute($g_bmc, "MODEL")) {
93 die "The MODEL attribute on $g_bmc is missing or empty.\n";
94 }
95 $g_bmcModel = $g_targetObj->getAttribute($g_bmc, "MODEL");
96
97 if ($g_targetObj->isBadAttribute($g_bmc, "MANUFACTURER")) {
98 die "The MANUFACTURER attribute on $g_bmc is missing or empty.\n";
99 }
100 $g_bmcMfgr = $g_targetObj->getAttribute($g_bmc, "MANUFACTURER");
101
102 $g_systemName = $g_targetObj->getSystemName();
103 if (length($g_systemName) == 0) {
104 die "The SYSTEM_NAME attribute is not set on the system target.\n";
105 }
106}
107
108
Matt Spinler23d47c22016-10-04 12:31:21 -0500109#Returns a hash that represents the 'aliases' node.
110#Will look like:
111# aliases {
112# name1 = &val1;
113# name2 = &val2;
114# ...
115# }
116sub getAliases()
117{
118 my %aliases;
Matt Spinler23d47c22016-10-04 12:31:21 -0500119
Matt Spinler30b461c2016-10-10 16:50:07 -0500120 #Get the info from the config file
Matt Spinler23d47c22016-10-04 12:31:21 -0500121
Matt Spinler30b461c2016-10-10 16:50:07 -0500122 if ((not exists $g_configuration{aliases}) ||
123 (keys %{$g_configuration{aliases}} == 0)) {
124 print "WARNING: Missing or empty 'aliases' section in config file.\n";
125 return %aliases;
126 }
127 %aliases = %{ $g_configuration{aliases} };
128
129 #add a & reference if one is missing
130 foreach my $a (keys %aliases) {
131 if (($aliases{$a} !~ /^&/) && ($aliases{$a} !~ /^\(ref\)/)) {
132 $aliases{$a} = "(ref)$aliases{$a}";
Matt Spinler23d47c22016-10-04 12:31:21 -0500133 }
134 }
135
136 return %aliases;
137}
138
Matt Spinler7d381e12016-09-27 14:27:24 -0500139
140#Return a hash that represents the 'chosen' node
Matt Spinler995f2a22016-09-30 13:07:31 -0500141#Will look like:
142# chosen {
143# stdout-path = ...
144# bootargs = ...
145# }
Matt Spinler7d381e12016-09-27 14:27:24 -0500146sub getChosen()
147{
Matt Spinler7d381e12016-09-27 14:27:24 -0500148 my %chosen;
Matt Spinler30b461c2016-10-10 16:50:07 -0500149 my @allowed = qw(bootargs stdin-path stdout-path);
150
151 #Get the info from the config file
152
153 if (not exists $g_configuration{chosen}) {
154 die "ERROR: Missing 'chosen' section in config file.\n";
155 }
156 %chosen = %{ $g_configuration{chosen} };
157
158 #Check for allowed entries. Empty is OK.
159 foreach my $key (keys %chosen) {
160 my $found = 0;
161 foreach my $good (@allowed) {
162 if ($key eq $good) {
163 $found = 1;
164 }
165 }
166
167 if ($found == 0) {
168 die "Invalid entry $key in 'chosen' section in config file\n";
169 }
170 }
171
Matt Spinler7d381e12016-09-27 14:27:24 -0500172 return %chosen;
173}
174
175
Matt Spinler30b461c2016-10-10 16:50:07 -0500176#Return a hash that represents the 'memory' node.
Matt Spinler995f2a22016-09-30 13:07:31 -0500177#Will look like:
Matt Spinler30b461c2016-10-10 16:50:07 -0500178# memory {
179# reg = < base size >
180# }
181sub getBmcMemory()
182{
183 my %memory;
184
185 #Get the info from the config file
186
187 if (not exists $g_configuration{memory}) {
188 die "ERROR: Missing 'memory' section in config file.\n";
189 }
190
191 if ((not exists $g_configuration{memory}{base}) ||
192 ($g_configuration{memory}{base} !~ /0x/)) {
193 die "ERROR: The base entry in the memory section in the config " .
194 "file is either missing or invalid.\n";
195 }
196
197 if ((not exists $g_configuration{memory}{size}) ||
198 ($g_configuration{memory}{size} !~ /0x/)) {
199 die "ERROR: The size entry in the memory section in the config " .
200 "file is either missing or invalid.\n";
201 }
202
203 #Future: could do more validation on the actual values
204
205 $memory{reg} = "<$g_configuration{memory}{base} " .
206 "$g_configuration{memory}{size}>";
207
208 return %memory;
209}
210
211
Matt Spinler25d60bb2016-10-31 15:16:03 -0500212#Returns an array of hashes representing the device tree nodes for
213#the BMC flash. These nodes are BMC model specific because different
214#models can have different device drivers.
215sub getBMCFlashNodes()
Matt Spinler7d381e12016-09-27 14:27:24 -0500216{
Matt Spinler25d60bb2016-10-31 15:16:03 -0500217 my @nodes;
218
219 if ($g_bmcModel eq "AST2500") {
220 my %node = getAST2500BMCSPIFlashNode();
221 push @nodes, { %node };
222 }
223 else {
224 die "ERROR: No BMC SPI flash support yet for BMC model $g_bmcModel\n";
225 }
226
227 return @nodes;
228}
229
230
231#Returns a hash that represents the BMC SPI flash(es) by finding the SPI
232#connections that come from the unit tagged as BMC_CODE. The code also
233#looks in the config file for any additional properties to add. Supports
234#the hardware where the same SPI master unit can be wired to more than 1
235#flash (a chip select line is used to switch between them.) This is
236#specific to the ASPEED AST2500 hardware and device driver.
237#Will look like:
238# fmc {
239# status = "okay"
240# flash@0 {
241# ...
242# };
243# flash@1 {
244# ...
245# };
246sub getAST2500BMCSPIFlashNode()
247{
248 my %bmcFlash;
249 my $chipSelect = 0;
250 my $lastUnit = "";
251
252 my $connections = findConnections($g_bmc, "SPI", "FLASH");
253
254 if ($connections eq "") {
255 die "ERROR: No BMC SPI flashes found connected to the BMC\n";
256 }
257
258 $bmcFlash{fmc}{status} = "okay";
259
260 foreach my $spi (@{$connections->{CONN}}) {
261
262 #Looking for spi-masters with a function of 'BMC_CODE'.
263 #It's possible there are multiple flash chips here.
264 if (!$g_targetObj->isBadAttribute($spi->{SOURCE}, "SPI_FUNCTION")) {
265
266 my $function = $g_targetObj->getAttribute($spi->{SOURCE},
267 "SPI_FUNCTION");
268 if ($function eq "BMC_CODE") {
269
270 my $flashName = "flash@".$chipSelect;
271
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500272 $bmcFlash{fmc}{$flashName}{COMMENT} = connectionComment($spi);
Matt Spinler25d60bb2016-10-31 15:16:03 -0500273
274 $bmcFlash{fmc}{$flashName}{status} = "okay";
275
276 #Add in anything specified in the config file for this chip.
277 addBMCFlashConfigProperties(\%{$bmcFlash{fmc}{$flashName}},
278 $chipSelect);
279
280 #The code currently only supports the config where a chip
281 #select line is used to select between possibly multiple
282 #flash chips attached to the same SPI pins/unit. So we
283 #need to make sure if there are multiple chips found, that
284 #they are off of the same master unit.
285 if ($lastUnit eq "") {
286 $lastUnit = $spi->{SOURCE};
287 }
288 else {
289 if ($lastUnit ne $spi->{SOURCE}) {
290 die "ERROR: Currently only 1 spi-master unit is " .
291 "supported for BMC flash connections."
292 }
293 }
294
295 #Since we don't need anything chip select specific from the
296 #XML, we can just assign our own chip selects.
297 $chipSelect++;
298 }
299 }
300 }
301
302 if ($chipSelect == 0) {
303 die "ERROR: Didn't find any BMC flash chips connected";
304 }
305
306 return %bmcFlash;
307}
308
309
310#Looks in the bmc-flash-config section in the config file for the
311#chip select passed in to add any additional properties to the BMC
312#flash node.
313# $node = hash reference to the flash node
314# $cs = the flash chip select value
315sub addBMCFlashConfigProperties()
316{
317 my ($node, $cs) = @_;
318 my $section = "chip-select-$cs";
319
320 if (exists $g_configuration{"bmc-flash-config"}{$section}) {
321 foreach my $key (sort keys $g_configuration{"bmc-flash-config"}{$section}) {
322 $node->{$key} = $g_configuration{"bmc-flash-config"}{$section}{$key};
323 }
324 }
Matt Spinler995f2a22016-09-30 13:07:31 -0500325}
326
327
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500328#Returns an array of hashes representing the other flashes used by the
329#BMC besides the ones that hold the BMC code. This is BMC model specific
330#as different models can have different interfaces.
331#Typically, these are SPI flashes.
332sub getOtherFlashNodes()
333{
334 my @nodes;
335
336 if ($g_bmcModel eq "AST2500") {
337 @nodes = getAST2500SpiFlashNodes();
338 }
339 else {
340 die "ERROR: No SPI flash support yet for BMC model $g_bmcModel\n";
341 }
342
343 return @nodes;
344}
345
346
347#Returns an array of hashes representing the SPI flashes in an
348#AST2500. These are for the SPI1 and SPI2 interfaces in the chip.
349#Each SPI master interface can support multiple flash chips. If
350#no hardware is connected to the interface, the node won't be present.
351sub getAST2500SpiFlashNodes()
352{
353 my @nodes;
354
355 #The AST2500 has 2 SPI master units, 1 and 2.
356 my @units = (1, 2);
357
358 foreach my $unit (@units) {
359
360 my %node = getAST2500SpiMasterNode($unit);
361
362 if (keys %node) {
363 my %spiNode;
364 my $nodeName = "spi$unit";
365 $spiNode{$nodeName} = { %node };
366 push @nodes, { %spiNode };
367 }
368 }
369
370 return @nodes;
371}
372
373
374#Returns a hash that represents the device tree node for the SPI1
375#or SPI2 master interface on the AST2500. Each master can support
376#multiple chips by use of a chip select.
377#Will look like:
378# spi1 {
379# status = "okay";
380# flash@0 {
381# ...
382# };
383# };
384#
385# $spiNum = The SPI master unit number to use
386sub getAST2500SpiMasterNode()
387{
388 my $spiNum = shift;
389 my %spiMaster;
390 my $chipSelect = 0;
391
392 my $connections = findConnections($g_bmc, "SPI", "FLASH");
393
394 if ($connections eq "") {
395 return %spiMaster;
396 }
397
398 #Looking for spi-masters with a chip-unit of $spiNum
399 #It's possible there are multiple flash chips off the master
400 foreach my $spi (@{$connections->{CONN}}) {
401
402 my $unitNum = $g_targetObj->getAttribute($spi->{SOURCE},
403 "CHIP_UNIT");
404 if ($unitNum == $spiNum) {
405 $spiMaster{status} = "okay";
406 my $flashName = "flash@".$chipSelect;
407
408 $spiMaster{$flashName}{COMMENT} = connectionComment($spi);
409
410 $spiMaster{$flashName}{status} = "okay";
411
412 $chipSelect++;
413 }
414 }
415
416 return %spiMaster;
417}
418
419
Matt Spinler995f2a22016-09-30 13:07:31 -0500420#Returns a hash that represents the leds node by finding all of the
421#GPIO connections to LEDs.
422#Node will look like:
423# leds {
424# <ledname> {
425# gpios = &gpio ASPEED_GPIO(x, y) GPIO_ACTIVE_xxx>
426# };
427# <another ledname> {
428# ...
429# }
430sub getLEDNode()
431{
432 my %leds;
433
Matt Spinlereca7f062016-11-07 09:59:23 -0600434 $leds{compatible} = "gpio-leds";
Matt Spinler995f2a22016-09-30 13:07:31 -0500435
436 my $connections = findConnections($g_bmc, "GPIO", "LED");
437
438 if ($connections eq "") {
439 print "WARNING: No LEDs found connected to the BMC\n";
440 return %leds;
441 }
442
443 foreach my $gpio (@{$connections->{CONN}}) {
444 my %ledNode;
445
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500446 $ledNode{COMMENT} = connectionComment($gpio);
Matt Spinler995f2a22016-09-30 13:07:31 -0500447
448 #The node name will be the simplified LED name
449 my $name = $gpio->{DEST_PARENT};
450 $name =~ s/(-\d+$)//; #remove trailing position
451 $name =~ s/.*\///; #remove the front of the path
452
453 #For now only supports ASPEED.
454 if (uc($g_bmcMfgr) ne "ASPEED") {
455 die "ERROR: Unsupported BMC manufacturer $g_bmcMfgr\n";
456 }
457 my $num = $g_targetObj->getAttribute($gpio->{SOURCE}, "PIN_NUM");
458 my $macro = getAspeedGpioMacro($num);
459
460 #If it's active high or low
461 my $state = $g_targetObj->getAttribute($gpio->{DEST_PARENT}, "ON_STATE");
462 my $activeString = getGpioActiveString($state);
463
464 $ledNode{gpios} = "<&gpio $macro $activeString>";
465
466 $leds{$name} = { %ledNode };
467 }
468
469 return %leds;
470}
471
472
473#Returns a either GPIO_ACTIVE_HIGH or GPIO_ACTIVE_LOW
474# $val = either a 1 or a 0 for active high or low
475sub getGpioActiveString() {
476 my $val = shift;
477
478 if ($val == 0) {
479 return "GPIO_ACTIVE_LOW";
480 }
481
482 return "GPIO_ACTIVE_HIGH";
483}
484
485
486#Turns a GPIO number into something like ASPEED_GPIO(A, 0) for the
487#ASPEED GPIO numbering scheme A[0-7] -> Z[0-7] and then starts at
488#AA[0-7] after that.
489# $num = the GPIO number
490sub getAspeedGpioMacro() {
491 my $num = shift;
492 my $char;
493 my $offset = $num % 8;
494 my $block = int($num / 8);
495
496 #If past Z, wraps to AA, AB, etc
497 if ((ord('A') + $block) > ord('Z')) {
498 #how far past Z?
499 $char = $block - (ord('Z') - ord('A'));
500
501 #Don't let it wrap twice
502 if ($char > (ord('Z') - ord('A') + 1)) {
503 die "ERROR: Invalid PIN_NUM value $num found for GPIO\n";
504 }
505
506 #start back at 'A' again, and convert to a character
507 $char = chr($char + ord('A') - 1);
508
509 #Add in a bonus 'A', to get something like AB
510 $char = "A".$char;
511 }
512 else {
513 $char = ord('A') + $block;
514 $char = chr($char);
515 }
516
517 return "ASPEED_GPIO($char, $offset)";
518}
519
520
521#Returns a list of hashes that represent the UART nodes on the BMC by
522#finding the UART connections.
523#Nodes will look like:
524# &uartX {
525# status = "okay"
526# }
527sub getUARTNodes()
528{
529 my @nodes;
530
Matt Spinler23d47c22016-10-04 12:31:21 -0500531 #Using U750 for legacy MRW reasons
532 my $connections = findConnections($g_bmc, "U750");
Matt Spinler995f2a22016-09-30 13:07:31 -0500533
534 if ($connections eq "") {
535 print "WARNING: No UART buses found connected to the BMC\n";
536 return @nodes;
537 }
538
539 foreach my $uart (@{$connections->{CONN}}) {
540 my %node;
541
542 my $num = $g_targetObj->getAttribute($uart->{SOURCE}, "CHIP_UNIT");
543 my $name = "uart$num";
544
545 $node{$name}{status} = "okay";
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500546 $node{$name}{COMMENT} = connectionComment($uart);
Matt Spinler995f2a22016-09-30 13:07:31 -0500547
548 push @nodes, { %node };
549 }
550
Matt Spinler7d381e12016-09-27 14:27:24 -0500551 return @nodes;
552}
553
554
Matt Spinler995f2a22016-09-30 13:07:31 -0500555#Returns a list of hashes that represent the MAC (ethernet) nodes on the BMC
556#by finding the connections of type ETHERNET.
557#Nodes will look like:
558# &macX {
559# ...
560# }
561sub getMacNodes()
562{
563 my @nodes;
564
565 my $connections = findConnections($g_bmc, "ETHERNET");
566
567 if ($connections eq "") {
568 print "WARNING: No ethernet buses found connected to the BMC\n";
569 return @nodes;
570 }
571
572 foreach my $eth (@{$connections->{CONN}}) {
573 my %node;
574
575 my $num = $g_targetObj->getAttribute($eth->{SOURCE}, "CHIP_UNIT");
576 my $ncsi = $g_targetObj->getAttribute($eth->{SOURCE}, "NCSI_MODE");
577 my $hwChecksum = $g_targetObj->getAttribute($eth->{SOURCE},
578 "USE_HW_CHECKSUM");
579
580 my $name = "mac$num";
581 $node{$name}{status} = "okay";
582
583 if ($ncsi == 1) {
Matt Spinler74909132016-10-07 13:52:19 -0500584 $node{$name}{"use-ncsi"} = ZERO_LENGTH_PROPERTY;
Matt Spinler995f2a22016-09-30 13:07:31 -0500585 }
586 if ($hwChecksum == 0) {
Matt Spinler74909132016-10-07 13:52:19 -0500587 $node{$name}{"no-hw-checksum"} = ZERO_LENGTH_PROPERTY;
Matt Spinler995f2a22016-09-30 13:07:31 -0500588 }
589
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500590 $node{$name}{COMMENT} = connectionComment($eth);
Matt Spinler995f2a22016-09-30 13:07:31 -0500591
592 push @nodes, { %node };
593 }
594
595 return @nodes;
596}
597
598
599#Returns a list of hashes that represent the virtual UART nodes
600#Node will look like:
601# &vuart {
602# status = "okay"
603# }
Matt Spinler7d381e12016-09-27 14:27:24 -0500604sub getVuartNodes()
605{
606 my @nodes;
607 my %node;
608
609 #For now, enable 1 node all the time.
Matt Spinler995f2a22016-09-30 13:07:31 -0500610 #TBD if this needs to be fixed
Matt Spinler7d381e12016-09-27 14:27:24 -0500611 $node{vuart}{status} = "okay";
612
613 push @nodes, { %node };
614
615 return @nodes;
616}
617
Matt Spinler74909132016-10-07 13:52:19 -0500618#Returns a list of hashes that represent the I2C device nodes.
619#There is 1 parent node for each bus, which then have subnodes
620#for each device on that bus. If a bus doesn't have any
621#attached devices, it doesn't need to show up.
622#The nodes will look like:
623# &i2c0 {
624# status = "okay"
625# device1@addr { (addr = 7 bit I2C address)
626# reg = <addr>
627# compatible = ...
628# ...
629# }
630# device2@addr {
631# reg = <addr>
632# ...
633# }
634# }
635# &i2c1 {
636# ...
637# }
638sub getI2CNodes()
639{
640 my @nodes;
641 my %busNodes;
642
643 my $connections = findConnections($g_bmc, "I2C");
644
645 if ($connections eq "") {
646 print "WARNING: No I2C buses found connected to the BMC\n";
647 return @nodes;
648 }
649
650 foreach my $i2c (@{$connections->{CONN}}) {
651
652 my %deviceNode, my $deviceName;
653
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500654 $deviceNode{COMMENT} = connectionComment($i2c);
Matt Spinler74909132016-10-07 13:52:19 -0500655
656 $deviceName = lc $i2c->{DEST_PARENT};
657 $deviceName =~ s/-\d+$//; #remove trailing position
658 $deviceName =~ s/.*\///; #remove the front of the path
659
660 #Get the I2C address
661 my $i2cAddress = $g_targetObj->getAttribute($i2c->{DEST}, "I2C_ADDRESS");
662 $i2cAddress = hex($i2cAddress);
663 if ($i2cAddress == 0) {
664 die "ERROR: Missing I2C address on $i2c->{DEST}\n";
665 }
666
667 #Put it in the format we want to print it in
668 $i2cAddress = adjustI2CAddress($i2cAddress);
669 $deviceNode{reg} = "<$i2cAddress>";
670
671 $deviceName = makeNodeName($deviceName, $deviceNode{reg});
672
673 #Get the I2C bus number
674 if ($g_targetObj->isBadAttribute($i2c->{SOURCE},
675 "I2C_PORT")) {
676 die "ERROR: I2C_PORT attribute in $i2c->{DEST_PARENT} " .
677 "is either missing or empty.\n";
678 }
679
680 my $busNum = $g_targetObj->getAttribute($i2c->{SOURCE}, "I2C_PORT");
681 if ($busNum =~ /0x/i) {
682 $busNum = hex($busNum);
683 }
684
685 #Convert the number to the Linux numbering scheme.
Matt Spinler30b461c2016-10-10 16:50:07 -0500686 $busNum += $g_i2cBusAdjust;
Matt Spinler74909132016-10-07 13:52:19 -0500687
688 #Get the compatible property
689 if ($g_targetObj->isBadAttribute($i2c->{DEST_PARENT},
690 "BMC_DT_COMPATIBLE")) {
691 die "ERROR: BMC_DT_COMPATIBLE attribute in $i2c->{DEST_PARENT} " .
692 "is either missing or empty.\n";
693 }
694
695 $deviceNode{compatible} = $g_targetObj->getAttribute(
696 $i2c->{DEST_PARENT},
697 "BMC_DT_COMPATIBLE");
698
699 #Get any other part specific properties, where the property
700 #names are actually defined in the XML.
701 my %props = getPartDefinedDTProperties($i2c->{DEST_PARENT});
702 foreach my $prop (sort keys %props) {
703 $deviceNode{$prop} = $props{$prop};
704 }
705
706 #busNodeName is the hash twice so when we loop
707 #below it doesn't get lost
708 my $busNodeName = "i2c$busNum";
709 $busNodes{$busNodeName}{$busNodeName}{status} = "okay";
710 $busNodes{$busNodeName}{$busNodeName}{$deviceName} = { %deviceNode };
711 }
712
713 #Each bus gets its own hash entry in the array
714 for my $b (sort keys %busNodes) {
715 push @nodes, { %{$busNodes{$b}} };
716 }
717
718 return @nodes;
719}
720
721
722#Returns a hash of property names and values that should be stored in
723#the device tree node for this device. The names of the properties and
724#the attributes to find their values in are stored in the
725#BMC_DT_ATTR_NAMES attribute in the chip.
726# $chip = the chip target
727sub getPartDefinedDTProperties()
728{
729 my $chip = shift;
730 my %props;
731
732 if ($g_targetObj->isBadAttribute($chip, "BMC_DT_ATTR_NAMES")) {
733 return %props;
734 }
735
736 my $attr = $g_targetObj->getAttribute($chip, "BMC_DT_ATTR_NAMES");
737 $attr =~ s/\s//g;
738 my @names = split(',', $attr);
739
740 #There can be up to 4 entries in this attribute
741 for (my $i = 0; $i < scalar @names; $i += 2) {
742
743 #$names[$i] holds the name of the attribute.
744 #$names[$i+1] holds the name of the property to store its value in.
745 if (($names[$i] ne "NA") && ($names[$i] ne "")) {
746
747 my $val = $g_targetObj->getAttribute($chip, $names[$i]);
748
749 #if the value is empty, assume it's for a standalone property,
750 #which gets turned into: some-property;
751 if ($val eq "") {
752 $props{$names[$i+1]} = ZERO_LENGTH_PROPERTY;
753 }
754 else {
755 $props{$names[$i+1]} = "<$val>";
756 }
757 }
758 }
759
760 return %props;
761}
762
763
764#Convert the MRW I2C address into the format the dts needs
765# $addr = the I2C Address
766sub adjustI2CAddress()
767{
768 my $addr = shift;
769
770 #MRW holds the 8 bit value. We need the 7 bit one.
771 my $addr = $addr >> 1;
772 $addr = sprintf("0x%X", $addr);
773 $addr = lc $addr;
774
775 return $addr;
776}
777
778
Matt Spinler30b461c2016-10-10 16:50:07 -0500779#Sets the global $g_i2cBusAdjust from the configuration file.
780sub getI2CBusAdjust()
Matt Spinler74909132016-10-07 13:52:19 -0500781{
Matt Spinler30b461c2016-10-10 16:50:07 -0500782 if (exists $g_configuration{"i2c-bus-adjust"}) {
Matt Spinler74909132016-10-07 13:52:19 -0500783
Matt Spinler30b461c2016-10-10 16:50:07 -0500784 $g_i2cBusAdjust = $g_configuration{"i2c-bus-adjust"};
Matt Spinler74909132016-10-07 13:52:19 -0500785
Matt Spinler30b461c2016-10-10 16:50:07 -0500786 if (!looks_like_number($g_i2cBusAdjust)) {
787 die "ERROR: Invalid i2c-bus-adjust value $g_i2cBusAdjust " .
788 "found in config file.\n";
Matt Spinler7d381e12016-09-27 14:27:24 -0500789 }
790 }
Matt Spinler30b461c2016-10-10 16:50:07 -0500791 else {
792 $g_i2cBusAdjust = 0;
793 print "WARNING: No I2C Bus number adjustment done " .
794 "for this system.\n";
795 }
Matt Spinler7d381e12016-09-27 14:27:24 -0500796}
797
798
799#Returns a list of compatible fields for the BMC itself.
800sub getBMCCompatibles()
801{
802 my @compats;
803
Matt Spinler23d47c22016-10-04 12:31:21 -0500804 #1st entry: <system mfgr>,<system name>-bmc
805 #2nd entry: <bmc mfgr>,<bmc model>
Matt Spinler7d381e12016-09-27 14:27:24 -0500806
Matt Spinler23d47c22016-10-04 12:31:21 -0500807 foreach my $target (sort keys %{ $g_targetObj->getAllTargets() }) {
808 if ($g_targetObj->getType($target) eq "SYS") {
809 my $mfgr = $g_targetObj->getAttribute($target, "MANUFACTURER");
810 push @compats, lc "$mfgr,$g_systemName-bmc";
811 last;
812 }
Matt Spinler7d381e12016-09-27 14:27:24 -0500813 }
814
815 push @compats, lc($g_bmcMfgr).",".lc($g_bmcModel);
816
817 return @compats;
818}
819
820
821#Returns a string for the system's BMC model property
822sub getSystemBMCModel()
823{
Matt Spinler995f2a22016-09-30 13:07:31 -0500824 #'<System> BMC'
Matt Spinler7d381e12016-09-27 14:27:24 -0500825 my $sys = lc $g_systemName;
826 $sys = uc(substr($sys, 0, 1)) . substr($sys, 1);
827
828 return $sys . " BMC";
829}
830
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500831#Create the comment that will show up in the device tree
832#for a connection. In the output, will look like:
833# // sourceUnit ->
834# // destChip
835#
836# $conn = The connection hash reference
837sub connectionComment()
838{
839 my $conn = shift;
840 my $comment = "$conn->{SOURCE} ->\n$conn->{DEST_PARENT}";
841 return $comment;
842}
843
Matt Spinler7d381e12016-09-27 14:27:24 -0500844
845#Prints a list of nodes at the same indent level
846# $f = file handle
847# $level = indent level (0,1,etc)
848# @nodes = array of node hashes to print, where the
849# key for the hash is the name of the node
850sub printNodes()
851{
852 my ($f, $level, @nodes) = @_;
853
854 foreach my $n (@nodes) {
855 my %node = %$n;
856
857 foreach my $name (sort keys %node) {
858 my %n = %{ $node{$name} };
859 printNode($f, $level, $name, %n);
860 }
861 }
862}
863
864
865#Print a single node and its children
866# $f = file handle
867# $level = indent level (0,1,etc)
868# $name = the name of the node - shows up as:
869# name { ...
870# %vals = The contents of the node, with the following options:
871# if the key is:
872# - 'DTSI_INCLUDE', then value gets turned into a #include
Matt Spinler995f2a22016-09-30 13:07:31 -0500873# - 'COMMENT', then value gets turned into a // comment
Matt Spinler74909132016-10-07 13:52:19 -0500874# - 'ZERO_LENGTH_PROPERTY' then value gets turned into: value;
Matt Spinler7d381e12016-09-27 14:27:24 -0500875#
876# If the value is:
877# - a hash - then that hash gets turned into a child node
878# where the key is the name of the child node
Matt Spinler995f2a22016-09-30 13:07:31 -0500879# - an array of hashes indicates an array of child nodes
Matt Spinler7d381e12016-09-27 14:27:24 -0500880sub printNode()
881{
882 my ($f, $level, $name, %vals) = @_;
883 my $include = "";
884
Matt Spinlerc0dff8a2016-11-02 15:47:30 -0500885 #No reason to print an empty node
886 if (!keys %vals) {
887 return;
888 }
889
Matt Spinler7d381e12016-09-27 14:27:24 -0500890 if ($level == 0) {
891 $name = "&".$name;
892 }
893
Matt Spinler995f2a22016-09-30 13:07:31 -0500894 print $f "\n";
895
896 if (exists $vals{COMMENT}) {
897 my @lines = split('\n', $vals{COMMENT});
898 foreach my $l (@lines) {
899 print $f indent($level) . "// $l\n";
900 }
901 }
902
903 print $f indent($level) . "$name {\n";
Matt Spinler7d381e12016-09-27 14:27:24 -0500904
Matt Spinler74909132016-10-07 13:52:19 -0500905 #First print properties, then includes, then subnodes
906
907 #Print Properties
Matt Spinler7d381e12016-09-27 14:27:24 -0500908 foreach my $v (sort keys %vals) {
909
Matt Spinler995f2a22016-09-30 13:07:31 -0500910 next if ($v eq "COMMENT");
Matt Spinler74909132016-10-07 13:52:19 -0500911 next if ($v eq "DTSI_INCLUDE");
912 next if (ref($vals{$v}) eq "HASH");
913 next if (ref($vals{$v}) eq "ARRAY");
Matt Spinler995f2a22016-09-30 13:07:31 -0500914
Matt Spinler74909132016-10-07 13:52:19 -0500915 if ($vals{$v} ne ZERO_LENGTH_PROPERTY) {
916 printProperty($f, $level+1, $v, $vals{$v});
Matt Spinler7d381e12016-09-27 14:27:24 -0500917 }
Matt Spinler74909132016-10-07 13:52:19 -0500918 else {
919 printZeroLengthProperty($f, $level+1, $v);
920 }
921 }
922
923 #Print Includes
924 foreach my $v (sort keys %vals) {
925
926 if ($v eq "DTSI_INCLUDE") {
927 #print 1 include per line
928 my @incs = split(',', $vals{$v});
929 foreach my $i (@incs) {
930 print $f qq(#include "$i";\n);
931 }
932 }
933 }
934
935 #Print Nodes
936 foreach my $v (sort keys %vals) {
937
938 if (ref($vals{$v}) eq "HASH") {
Matt Spinler7d381e12016-09-27 14:27:24 -0500939 printNode($f, $level+1, $v, %{$vals{$v}});
940 }
Matt Spinler995f2a22016-09-30 13:07:31 -0500941 #An array of nested nodes
942 elsif (ref($vals{$v}) eq "ARRAY") {
943 my @array = @{$vals{$v}};
944 &printNodes($f, $level+1, @array);
945 }
Matt Spinler7d381e12016-09-27 14:27:24 -0500946 }
947
948 print $f indent($level) . "};\n";
949}
950
951
952#Prints a comma separated list of properties.
953#e.g. a = "b, c, d";
954# $f = file handle
955# $level = indent level (0,1,etc)
956# $name = name of property
957# @vals = list of property values
958sub printPropertyList()
959{
960 my ($f, $level, $name, @vals) = @_;
961
962 print $f indent($level) . "$name = ";
963
964 for (my $i = 0;$i < scalar @vals; $i++) {
Matt Spinler30b461c2016-10-10 16:50:07 -0500965 print $f qq("$vals[$i]");
Matt Spinler7d381e12016-09-27 14:27:24 -0500966 if ($i < (scalar(@vals) - 1)) {
967 print $f ", ";
968 }
969 }
970 print $f ";\n"
971}
972
973
974#Prints a single property. e.g. a = "b";
975# $f = file handle
976# $level = indent level (0,1,etc)
977# $name = name of property
978# @vals = property values
979sub printProperty()
980{
981 my ($f, $level, $name, $val) = @_;
Matt Spinler30b461c2016-10-10 16:50:07 -0500982 my $quoteChar = qq(");
Matt Spinler23d47c22016-10-04 12:31:21 -0500983
Matt Spinler30b461c2016-10-10 16:50:07 -0500984 $val = convertReference($val);
Matt Spinler23d47c22016-10-04 12:31:21 -0500985
986 #properties with < > or single word aliases don't need quotes
987 if (($val =~ /<.*>/) || ($val =~ /^&\w+$/)) {
Matt Spinler30b461c2016-10-10 16:50:07 -0500988 $quoteChar = "";
Matt Spinler23d47c22016-10-04 12:31:21 -0500989 }
990
Matt Spinler30b461c2016-10-10 16:50:07 -0500991 print $f indent($level) . "$name = $quoteChar$val$quoteChar;\n";
Matt Spinler7d381e12016-09-27 14:27:24 -0500992}
993
994
Matt Spinler30b461c2016-10-10 16:50:07 -0500995#Prints a zero length property e.g. some-property;
Matt Spinler7d381e12016-09-27 14:27:24 -0500996# $f = file handle
997# $level = indent level (0,1,etc)
998# $name = name of property
Matt Spinler30b461c2016-10-10 16:50:07 -0500999sub printZeroLengthProperty()
Matt Spinler7d381e12016-09-27 14:27:24 -05001000{
1001 my ($f, $level, $name) = @_;
1002 print $f indent($level) . "$name;\n";
1003}
1004
1005
Matt Spinler30b461c2016-10-10 16:50:07 -05001006#Replace '(ref)' with '&'.
Matt Spinler7d381e12016-09-27 14:27:24 -05001007#Needed because Serverwiz doesn't properly escape '&'s in the XML,
Matt Spinler30b461c2016-10-10 16:50:07 -05001008#so the '(ref)' string is used to represent the reference
Matt Spinler7d381e12016-09-27 14:27:24 -05001009#specifier instead of '&'.
Matt Spinler30b461c2016-10-10 16:50:07 -05001010sub convertReference() {
Matt Spinler7d381e12016-09-27 14:27:24 -05001011 my $val = shift;
Matt Spinler30b461c2016-10-10 16:50:07 -05001012 $val =~ s/\(ref\)/&/g;
Matt Spinler7d381e12016-09-27 14:27:24 -05001013 return $val
1014}
1015
1016
1017#Returns the target for the BMC chip.
1018#Not worrying about multiple BMC systems for now.
1019sub getBMCTarget()
1020{
1021 foreach my $target (sort keys %{ $g_targetObj->getAllTargets() })
1022 {
1023 if ($g_targetObj->getType($target) eq "BMC") {
1024 return $target;
1025 }
1026 }
1027 return "";
1028}
1029
1030
1031#Prints the device tree version line.
1032# $f = file handle
1033sub printVersion()
1034{
1035 my $f = shift;
1036 print $f VERSION."\n"
1037}
1038
1039
1040#Prints the #include line for pulling in an include file.
Matt Spinler30b461c2016-10-10 16:50:07 -05001041#The files to include come from the configuration file.
Matt Spinler7d381e12016-09-27 14:27:24 -05001042# $f = file handle
Matt Spinler30b461c2016-10-10 16:50:07 -05001043# $type = include type
Matt Spinler7d381e12016-09-27 14:27:24 -05001044sub printIncludes()
1045{
Matt Spinler30b461c2016-10-10 16:50:07 -05001046 my ($f, $type) = @_;
1047 my @includes = getIncludes($type);
Matt Spinler7d381e12016-09-27 14:27:24 -05001048
1049 foreach my $i (@includes) {
1050 #if a .dtsi, gets " ", otherwise < >
1051 if ($i =~ /\.dtsi$/) {
Matt Spinler30b461c2016-10-10 16:50:07 -05001052 $i = qq("$i");
Matt Spinler7d381e12016-09-27 14:27:24 -05001053 }
1054 else {
Matt Spinler30b461c2016-10-10 16:50:07 -05001055 $i = "<$i>";
Matt Spinler7d381e12016-09-27 14:27:24 -05001056 }
Matt Spinler30b461c2016-10-10 16:50:07 -05001057 print $f "#include $i\n";
Matt Spinler7d381e12016-09-27 14:27:24 -05001058 }
1059}
1060
1061
Matt Spinler30b461c2016-10-10 16:50:07 -05001062#Returns an array of include files found in the config file
1063#for the type specified.
1064# $type = the include type, which is the section name in the
1065# YAML configuration file.
Matt Spinler7d381e12016-09-27 14:27:24 -05001066sub getIncludes()
1067{
Matt Spinler30b461c2016-10-10 16:50:07 -05001068 my $type = shift;
Matt Spinler7d381e12016-09-27 14:27:24 -05001069 my @includes;
1070
Matt Spinler30b461c2016-10-10 16:50:07 -05001071 #The config file may have a section but no includes
1072 #listed in it, which is OK.
1073 if ((exists $g_configuration{includes}{$type}) &&
1074 (ref($g_configuration{includes}{$type}) eq "ARRAY")) {
Matt Spinler7d381e12016-09-27 14:27:24 -05001075
Matt Spinler30b461c2016-10-10 16:50:07 -05001076 @includes = @{$g_configuration{includes}{$type}};
Matt Spinler7d381e12016-09-27 14:27:24 -05001077 }
1078
1079 return @includes;
1080}
1081
Matt Spinler30b461c2016-10-10 16:50:07 -05001082
Matt Spinler74909132016-10-07 13:52:19 -05001083#Appends the first value of the 'reg' property
1084#passed in to the name passed in to create the
1085#full name for the node
1086# $name = node name that will be appended to
1087# $reg = the reg property values
1088sub makeNodeName()
1089{
1090 my ($name, $reg) = @_;
1091
1092 $reg =~ s/<//g;
1093 $reg =~ s/>//g;
1094 my @vals = split(' ', $reg);
1095
1096 if (scalar @vals > 0) {
1097 $vals[0] =~ s/0x//;
1098 $name .= "@" . lc $vals[0];
1099 }
1100
1101 return $name;
1102}
1103
Matt Spinler7d381e12016-09-27 14:27:24 -05001104
1105#Prints the root node starting bracket.
1106# $f = file handle
1107sub printRootNodeStart() {
1108 my $f = shift;
Matt Spinler30b461c2016-10-10 16:50:07 -05001109 print $f qq(/ {\n);
Matt Spinler7d381e12016-09-27 14:27:24 -05001110}
1111
1112
1113#Prints the root node ending bracket.
1114# $f = file handle
1115# $level = indent level (0,1,etc)
1116sub printRootNodeEnd() {
1117 my ($f, $level) = @_;
Matt Spinler30b461c2016-10-10 16:50:07 -05001118 print $f indent($level).qq(};\n);
Matt Spinler7d381e12016-09-27 14:27:24 -05001119}
1120
1121
1122#Returns a string that can be used to indent based on the
1123#level passed in. Each level is an additional 4 spaces.
1124# $level = indent level (0,1,etc)
1125sub indent() {
1126 my $level = shift;
1127 return ' ' x ($level * 4);
1128}
1129
1130
Matt Spinler995f2a22016-09-30 13:07:31 -05001131#Will look for all the connections of the specified type coming from
1132#any sub target of the specified target, instead of just 1 level down
1133#like the Targets inteface does. Needed because sometimes we have
1134#target->pingroup->sourceunit instead of just target->sourceunit
1135# $target = the target to find connections off of
1136# $bus = the bus type
1137# $partType = destination part type, leave off if a don't care
1138sub findConnections() {
1139 my ($target, $bus, $partType) = @_;
1140 my %allConnections;
1141 my $i = 0;
1142
1143 #get the ones from target->child
1144 my $connections = $g_targetObj->findConnections($target, $bus, $partType);
1145 if ($connections ne "") {
1146 foreach my $c (@{$connections->{CONN}}) {
1147 $allConnections{CONN}[$i] = { %{$c} };
1148 $i++;
1149 }
1150 }
1151
1152 #get everything deeper
1153 my @children = getAllTargetChildren($target);
1154 foreach my $c (@children) {
1155 my $connections = $g_targetObj->findConnections($c, $bus, $partType);
1156 if ($connections ne "") {
1157
1158 foreach my $c (@{$connections->{CONN}}) {
1159 $allConnections{CONN}[$i] = { %{$c} };
1160 $i++;
1161 }
1162 }
1163 }
1164
Matt Spinler23d47c22016-10-04 12:31:21 -05001165 #Match the Targets::findConnections return strategy
1166 if (!keys %allConnections) {
1167 return "";
1168 }
1169
Matt Spinler995f2a22016-09-30 13:07:31 -05001170 return \%allConnections;
1171}
1172
1173#Returns every sub target, not just the 1st level children.
1174# $target = the target to find the children of
1175sub getAllTargetChildren()
1176{
1177 my $target = shift;
1178 my @children;
1179
1180 my $targets = $g_targetObj->getTargetChildren($target);
1181 if ($targets ne "") {
1182
1183 foreach my $t (@$targets) {
1184 push @children, $t;
1185 my @more = getAllTargetChildren($t);
1186 push @children, @more;
1187 }
1188 }
1189
1190 return @children;
1191}
1192
1193
Matt Spinler7d381e12016-09-27 14:27:24 -05001194sub printUsage
1195{
Matt Spinler30b461c2016-10-10 16:50:07 -05001196 print "gen_devtree.pl -x [XML filename] -y [yaml config file] " .
1197 "-o [output filename]\n";
Matt Spinler7d381e12016-09-27 14:27:24 -05001198 exit(1);
1199}