blob: b077cf0e76d5d4c4eff15fb1f957a195f129a70a [file] [log] [blame]
Simon Glass3c19dc82019-10-31 07:42:55 -06001#!/usr/bin/env python3
Tom Rini83d290c2018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glassc0791922017-06-18 22:09:06 -06003# Copyright (c) 2012 The Chromium OS Authors.
4#
Simon Glassc0791922017-06-18 22:09:06 -06005
6"""Tests for the dtb_platdata module
7
Simon Glass3def0cf2018-07-06 10:27:20 -06008This includes unit tests for some functions and functional tests for the dtoc
9tool.
Simon Glassc0791922017-06-18 22:09:06 -060010"""
11
12import collections
Simon Glassa32eb7d2021-02-03 06:00:51 -070013import copy
Simon Glass10cbd3b2020-12-28 20:34:52 -070014import glob
Simon Glassc0791922017-06-18 22:09:06 -060015import os
16import struct
17import unittest
18
Simon Glassc0791922017-06-18 22:09:06 -060019from dtb_platdata import get_value
20from dtb_platdata import tab_to
Simon Glass67b5ec52020-12-28 20:34:47 -070021from dtoc import dtb_platdata
Simon Glassbf776672020-04-17 18:09:04 -060022from dtoc import fdt
23from dtoc import fdt_util
Simon Glassa32eb7d2021-02-03 06:00:51 -070024from dtoc import src_scan
Simon Glassa542a702020-12-28 20:35:06 -070025from dtoc.src_scan import conv_name_to_c
26from dtoc.src_scan import get_compat_name
Simon Glassbf776672020-04-17 18:09:04 -060027from patman import test_util
28from patman import tools
Simon Glassc0791922017-06-18 22:09:06 -060029
Simon Glass67b5ec52020-12-28 20:34:47 -070030OUR_PATH = os.path.dirname(os.path.realpath(__file__))
Simon Glassc0791922017-06-18 22:09:06 -060031
32
Simon Glassaab660f2017-11-12 21:52:17 -070033HEADER = '''/*
34 * DO NOT MODIFY
35 *
Simon Glassd1055d62020-12-28 20:35:00 -070036 * Defines the structs used to hold devicetree data.
37 * This was generated by dtoc from a .dtb (device tree binary) file.
Simon Glassaab660f2017-11-12 21:52:17 -070038 */
39
40#include <stdbool.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090041#include <linux/libfdt.h>'''
Simon Glassaab660f2017-11-12 21:52:17 -070042
43C_HEADER = '''/*
44 * DO NOT MODIFY
45 *
Simon Glassd1055d62020-12-28 20:35:00 -070046 * Declares the U_BOOT_DRIVER() records and platform data.
47 * This was generated by dtoc from a .dtb (device tree binary) file.
Simon Glassaab660f2017-11-12 21:52:17 -070048 */
49
Simon Glass20e442a2020-12-28 20:34:54 -070050/* Allow use of U_BOOT_DRVINFO() in this file */
Simon Glassf31fa992020-12-28 20:35:01 -070051#define DT_PLAT_C
Simon Glasscb43ac12020-10-03 11:31:41 -060052
Simon Glassaab660f2017-11-12 21:52:17 -070053#include <common.h>
54#include <dm.h>
55#include <dt-structs.h>
56'''
57
Simon Glassa32eb7d2021-02-03 06:00:51 -070058# Scanner saved from a previous run of the tests (to speed things up)
59saved_scan = None
60
Simon Glass67b5ec52020-12-28 20:34:47 -070061# This is a test so is allowed to access private things in the module it is
62# testing
63# pylint: disable=W0212
Simon Glassfe57c782018-07-06 10:27:37 -060064
65def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glassc0791922017-06-18 22:09:06 -060066 """Compile a .dts file to a .dtb
67
68 Args:
Simon Glass67b5ec52020-12-28 20:34:47 -070069 dts_fname (str): Filename of .dts file in the current directory
70 capture_stderr (bool): True to capture and discard stderr output
Simon Glassc0791922017-06-18 22:09:06 -060071
72 Returns:
Simon Glass67b5ec52020-12-28 20:34:47 -070073 str: Filename of compiled file in output directory
Simon Glassc0791922017-06-18 22:09:06 -060074 """
Simon Glassdff51a52021-02-03 06:00:56 -070075 return fdt_util.EnsureCompiled(os.path.join(OUR_PATH, 'test', dts_fname),
Simon Glassfe57c782018-07-06 10:27:37 -060076 capture_stderr=capture_stderr)
Simon Glassc0791922017-06-18 22:09:06 -060077
78
Simon Glassa32eb7d2021-02-03 06:00:51 -070079def setup():
80 global saved_scan
81
82 # Disable warnings so that calls to get_normalized_compat_name() will not
83 # output things.
84 saved_scan = src_scan.Scanner(None, True, False)
85 saved_scan.scan_drivers()
86
87def copy_scan():
88 """Get a copy of saved_scan so that each test can start clean"""
89 return copy.deepcopy(saved_scan)
90
91
Simon Glassc0791922017-06-18 22:09:06 -060092class TestDtoc(unittest.TestCase):
93 """Tests for dtoc"""
94 @classmethod
95 def setUpClass(cls):
96 tools.PrepareOutputDir(None)
Simon Glassf02d0eb2020-07-07 21:32:06 -060097 cls.maxDiff = None
Simon Glassc0791922017-06-18 22:09:06 -060098
99 @classmethod
100 def tearDownClass(cls):
Simon Glass67b5ec52020-12-28 20:34:47 -0700101 tools.FinaliseOutputDir()
Simon Glassc0791922017-06-18 22:09:06 -0600102
Simon Glass67b5ec52020-12-28 20:34:47 -0700103 @staticmethod
104 def _write_python_string(fname, data):
Simon Glass57f0bc42018-07-06 10:27:25 -0600105 """Write a string with tabs expanded as done in this Python file
106
107 Args:
Simon Glass67b5ec52020-12-28 20:34:47 -0700108 fname (str): Filename to write to
109 data (str): Raw string to convert
Simon Glass57f0bc42018-07-06 10:27:25 -0600110 """
111 data = data.replace('\t', '\\t')
Simon Glass67b5ec52020-12-28 20:34:47 -0700112 with open(fname, 'w') as fout:
113 fout.write(data)
Simon Glass57f0bc42018-07-06 10:27:25 -0600114
Simon Glass67b5ec52020-12-28 20:34:47 -0700115 def _check_strings(self, expected, actual):
Simon Glass57f0bc42018-07-06 10:27:25 -0600116 """Check that a string matches its expected value
117
118 If the strings do not match, they are written to the /tmp directory in
119 the same Python format as is used here in the test. This allows for
120 easy comparison and update of the tests.
121
122 Args:
Simon Glass67b5ec52020-12-28 20:34:47 -0700123 expected (str): Expected string
124 actual (str): Actual string
Simon Glass57f0bc42018-07-06 10:27:25 -0600125 """
126 if expected != actual:
Simon Glass67b5ec52020-12-28 20:34:47 -0700127 self._write_python_string('/tmp/binman.expected', expected)
128 self._write_python_string('/tmp/binman.actual', actual)
Simon Glass90a81322019-05-17 22:00:31 -0600129 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glass67b5ec52020-12-28 20:34:47 -0700130 self.assertEqual(expected, actual)
Simon Glass57f0bc42018-07-06 10:27:25 -0600131
Simon Glass67b5ec52020-12-28 20:34:47 -0700132 @staticmethod
133 def run_test(args, dtb_file, output):
134 """Run a test using dtoc
Walter Lozano361e7332020-06-25 01:10:08 -0300135
Simon Glass67b5ec52020-12-28 20:34:47 -0700136 Args:
137 args (list of str): List of arguments for dtoc
138 dtb_file (str): Filename of .dtb file
139 output (str): Filename of output file
Simon Glass05953522021-02-03 06:01:07 -0700140
141 Returns:
142 DtbPlatdata object
Simon Glass67b5ec52020-12-28 20:34:47 -0700143 """
Simon Glass337d6972021-02-03 06:01:10 -0700144 # Make a copy of the 'scan' object, since it includes uclasses and
145 # drivers, which get updated during execution.
Simon Glass05953522021-02-03 06:01:07 -0700146 return dtb_platdata.run_steps(args, dtb_file, False, output, [], None,
147 warning_disabled=True, scan=copy_scan())
Walter Lozano361e7332020-06-25 01:10:08 -0300148
Simon Glassc0791922017-06-18 22:09:06 -0600149 def test_name(self):
150 """Test conversion of device tree names to C identifiers"""
151 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
152 self.assertEqual('vendor_clock_frequency',
153 conv_name_to_c('vendor,clock-frequency'))
154 self.assertEqual('rockchip_rk3399_sdhci_5_1',
155 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
156
157 def test_tab_to(self):
158 """Test operation of tab_to() function"""
159 self.assertEqual('fred ', tab_to(0, 'fred'))
160 self.assertEqual('fred\t', tab_to(1, 'fred'))
161 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
162 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
163 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
164 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
165
166 def test_get_value(self):
167 """Test operation of get_value() function"""
168 self.assertEqual('0x45',
Simon Glass5ea9dcc2020-11-08 20:36:17 -0700169 get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
Simon Glassc0791922017-06-18 22:09:06 -0600170 self.assertEqual('0x45',
Simon Glass5ea9dcc2020-11-08 20:36:17 -0700171 get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
Simon Glassc0791922017-06-18 22:09:06 -0600172 self.assertEqual('0x0',
Simon Glass5ea9dcc2020-11-08 20:36:17 -0700173 get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
174 self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
175 self.assertEqual('true', get_value(fdt.Type.BOOL, None))
Simon Glassc0791922017-06-18 22:09:06 -0600176
177 def test_get_compat_name(self):
178 """Test operation of get_compat_name() function"""
179 Prop = collections.namedtuple('Prop', ['value'])
180 Node = collections.namedtuple('Node', ['props'])
181
182 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
183 node = Node({'compatible': prop})
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300184 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glassc0791922017-06-18 22:09:06 -0600185 get_compat_name(node))
186
187 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
188 node = Node({'compatible': prop})
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300189 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glassc0791922017-06-18 22:09:06 -0600190 get_compat_name(node))
191
192 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
193 node = Node({'compatible': prop})
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300194 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
Simon Glass67b5ec52020-12-28 20:34:47 -0700195 'arasan_sdhci_5_1', 'third']),
Simon Glassc0791922017-06-18 22:09:06 -0600196 get_compat_name(node))
197
198 def test_empty_file(self):
199 """Test output from a device tree file with no nodes"""
200 dtb_file = get_dtb_file('dtoc_test_empty.dts')
201 output = tools.GetOutputFilename('output')
Simon Glassa32eb7d2021-02-03 06:00:51 -0700202
203 # Run this one without saved_scan to complete test coverage
204 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [], True)
Simon Glassc0791922017-06-18 22:09:06 -0600205 with open(output) as infile:
206 lines = infile.read().splitlines()
Simon Glassaab660f2017-11-12 21:52:17 -0700207 self.assertEqual(HEADER.splitlines(), lines)
Simon Glassc0791922017-06-18 22:09:06 -0600208
Walter Lozano361e7332020-06-25 01:10:08 -0300209 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600210 with open(output) as infile:
211 lines = infile.read().splitlines()
Simon Glassd960f0d2020-12-28 20:35:05 -0700212 self.assertEqual(C_HEADER.splitlines() + [''], lines)
Simon Glassc0791922017-06-18 22:09:06 -0600213
Simon Glassde846cb2020-12-28 20:34:49 -0700214 struct_text = HEADER + '''
Simon Glassf38161c2021-02-03 06:00:57 -0700215struct dtd_sandbox_i2c {
Simon Glass5ec741f2017-08-29 14:15:51 -0600216};
Simon Glassf38161c2021-02-03 06:00:57 -0700217struct dtd_sandbox_pmic {
Simon Glass5ec741f2017-08-29 14:15:51 -0600218\tbool\t\tlow_power;
219\tfdt64_t\t\treg[2];
220};
Simon Glassc0791922017-06-18 22:09:06 -0600221struct dtd_sandbox_spl_test {
Simon Glassf02d0eb2020-07-07 21:32:06 -0600222\tconst char * acpi_name;
Simon Glassc0791922017-06-18 22:09:06 -0600223\tbool\t\tboolval;
224\tunsigned char\tbytearray[3];
225\tunsigned char\tbyteval;
226\tfdt32_t\t\tintarray[4];
227\tfdt32_t\t\tintval;
228\tunsigned char\tlongbytearray[9];
Simon Glass2a2d91d2018-07-06 10:27:28 -0600229\tunsigned char\tnotstring[5];
Simon Glassc0791922017-06-18 22:09:06 -0600230\tconst char *\tstringarray[3];
231\tconst char *\tstringval;
232};
Simon Glassde846cb2020-12-28 20:34:49 -0700233'''
Simon Glassc0791922017-06-18 22:09:06 -0600234
Simon Glassde846cb2020-12-28 20:34:49 -0700235 platdata_text = C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600236/* Node /i2c@0 index 0 */
Simon Glassf38161c2021-02-03 06:00:57 -0700237static struct dtd_sandbox_i2c dtv_i2c_at_0 = {
Simon Glass1b272732020-10-03 11:31:25 -0600238};
Simon Glass20e442a2020-12-28 20:34:54 -0700239U_BOOT_DRVINFO(i2c_at_0) = {
Simon Glassf38161c2021-02-03 06:00:57 -0700240\t.name\t\t= "sandbox_i2c",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700241\t.plat\t= &dtv_i2c_at_0,
Simon Glass4f500862020-12-03 16:55:19 -0700242\t.plat_size\t= sizeof(dtv_i2c_at_0),
Simon Glasse41651f2020-10-03 11:31:35 -0600243\t.parent_idx\t= -1,
Simon Glass1b272732020-10-03 11:31:25 -0600244};
245
246/* Node /i2c@0/pmic@9 index 1 */
Simon Glassf38161c2021-02-03 06:00:57 -0700247static struct dtd_sandbox_pmic dtv_pmic_at_9 = {
Simon Glass1b272732020-10-03 11:31:25 -0600248\t.low_power\t\t= true,
249\t.reg\t\t\t= {0x9, 0x0},
250};
Simon Glass20e442a2020-12-28 20:34:54 -0700251U_BOOT_DRVINFO(pmic_at_9) = {
Simon Glassf38161c2021-02-03 06:00:57 -0700252\t.name\t\t= "sandbox_pmic",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700253\t.plat\t= &dtv_pmic_at_9,
Simon Glass4f500862020-12-03 16:55:19 -0700254\t.plat_size\t= sizeof(dtv_pmic_at_9),
Simon Glasse41651f2020-10-03 11:31:35 -0600255\t.parent_idx\t= 0,
Simon Glass1b272732020-10-03 11:31:25 -0600256};
257
258/* Node /spl-test index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300259static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass1953ce72019-05-17 22:00:32 -0600260\t.boolval\t\t= true,
Simon Glassc0791922017-06-18 22:09:06 -0600261\t.bytearray\t\t= {0x6, 0x0, 0x0},
262\t.byteval\t\t= 0x5,
Simon Glass1953ce72019-05-17 22:00:32 -0600263\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600264\t.intval\t\t\t= 0x1,
Simon Glass21d54ac2017-08-29 14:15:49 -0600265\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
266\t\t0x11},
Simon Glass1953ce72019-05-17 22:00:32 -0600267\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600268\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glass1953ce72019-05-17 22:00:32 -0600269\t.stringval\t\t= "message",
Simon Glassc0791922017-06-18 22:09:06 -0600270};
Simon Glass20e442a2020-12-28 20:34:54 -0700271U_BOOT_DRVINFO(spl_test) = {
Simon Glassc0791922017-06-18 22:09:06 -0600272\t.name\t\t= "sandbox_spl_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700273\t.plat\t= &dtv_spl_test,
Simon Glass4f500862020-12-03 16:55:19 -0700274\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glasse41651f2020-10-03 11:31:35 -0600275\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600276};
277
Simon Glass1b272732020-10-03 11:31:25 -0600278/* Node /spl-test2 index 3 */
Walter Lozano51f12632020-06-25 01:10:13 -0300279static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glassf02d0eb2020-07-07 21:32:06 -0600280\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glassc0791922017-06-18 22:09:06 -0600281\t.bytearray\t\t= {0x1, 0x23, 0x34},
282\t.byteval\t\t= 0x8,
Simon Glass1953ce72019-05-17 22:00:32 -0600283\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600284\t.intval\t\t\t= 0x3,
Simon Glasse144caf2020-10-03 11:31:27 -0600285\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
Simon Glass21d54ac2017-08-29 14:15:49 -0600286\t\t0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600287\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glass1953ce72019-05-17 22:00:32 -0600288\t.stringval\t\t= "message2",
Simon Glassc0791922017-06-18 22:09:06 -0600289};
Simon Glass20e442a2020-12-28 20:34:54 -0700290U_BOOT_DRVINFO(spl_test2) = {
Simon Glassc0791922017-06-18 22:09:06 -0600291\t.name\t\t= "sandbox_spl_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700292\t.plat\t= &dtv_spl_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700293\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600294\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600295};
296
Simon Glass1b272732020-10-03 11:31:25 -0600297/* Node /spl-test3 index 4 */
Walter Lozano51f12632020-06-25 01:10:13 -0300298static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glasse144caf2020-10-03 11:31:27 -0600299\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
300\t\t0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600301\t.stringarray\t\t= {"one", "", ""},
302};
Simon Glass20e442a2020-12-28 20:34:54 -0700303U_BOOT_DRVINFO(spl_test3) = {
Simon Glassc0791922017-06-18 22:09:06 -0600304\t.name\t\t= "sandbox_spl_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700305\t.plat\t= &dtv_spl_test3,
Simon Glass4f500862020-12-03 16:55:19 -0700306\t.plat_size\t= sizeof(dtv_spl_test3),
Simon Glasse41651f2020-10-03 11:31:35 -0600307\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600308};
309
Simon Glassd960f0d2020-12-28 20:35:05 -0700310'''
Simon Glassde846cb2020-12-28 20:34:49 -0700311
312 def test_simple(self):
313 """Test output from some simple nodes with various types of data"""
314 dtb_file = get_dtb_file('dtoc_test_simple.dts')
315 output = tools.GetOutputFilename('output')
316 self.run_test(['struct'], dtb_file, output)
317 with open(output) as infile:
318 data = infile.read()
319
320 self._check_strings(self.struct_text, data)
321
322 self.run_test(['platdata'], dtb_file, output)
323 with open(output) as infile:
324 data = infile.read()
325
326 self._check_strings(self.platdata_text, data)
Simon Glassc0791922017-06-18 22:09:06 -0600327
Simon Glass10cbd3b2020-12-28 20:34:52 -0700328 # Try the 'all' command
329 self.run_test(['all'], dtb_file, output)
330 data = tools.ReadFile(output, binary=False)
331 self._check_strings(self.platdata_text + self.struct_text, data)
332
Walter Lozanodac82282020-07-03 08:07:17 -0300333 def test_driver_alias(self):
334 """Test output from a device tree file with a driver alias"""
335 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
336 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300337 self.run_test(['struct'], dtb_file, output)
Walter Lozanodac82282020-07-03 08:07:17 -0300338 with open(output) as infile:
339 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700340 self._check_strings(HEADER + '''
Walter Lozanodac82282020-07-03 08:07:17 -0300341struct dtd_sandbox_gpio {
342\tconst char *\tgpio_bank_name;
343\tbool\t\tgpio_controller;
344\tfdt32_t\t\tsandbox_gpio_count;
345};
Walter Lozanodac82282020-07-03 08:07:17 -0300346''', data)
347
Walter Lozano361e7332020-06-25 01:10:08 -0300348 self.run_test(['platdata'], dtb_file, output)
Walter Lozanodac82282020-07-03 08:07:17 -0300349 with open(output) as infile:
350 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700351 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600352/* Node /gpios@0 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300353static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanodac82282020-07-03 08:07:17 -0300354\t.gpio_bank_name\t\t= "a",
355\t.gpio_controller\t= true,
356\t.sandbox_gpio_count\t= 0x14,
357};
Simon Glass20e442a2020-12-28 20:34:54 -0700358U_BOOT_DRVINFO(gpios_at_0) = {
Walter Lozanodac82282020-07-03 08:07:17 -0300359\t.name\t\t= "sandbox_gpio",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700360\t.plat\t= &dtv_gpios_at_0,
Simon Glass4f500862020-12-03 16:55:19 -0700361\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glasse41651f2020-10-03 11:31:35 -0600362\t.parent_idx\t= -1,
Walter Lozanodac82282020-07-03 08:07:17 -0300363};
364
365''', data)
366
Walter Lozano361e7332020-06-25 01:10:08 -0300367 def test_invalid_driver(self):
368 """Test output from a device tree file with an invalid driver"""
369 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
370 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700371 with test_util.capture_sys_output() as _:
Simon Glassa32eb7d2021-02-03 06:00:51 -0700372 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [],
Simon Glassb00f0062021-02-03 06:01:02 -0700373 None, scan=copy_scan())
Walter Lozano361e7332020-06-25 01:10:08 -0300374 with open(output) as infile:
375 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700376 self._check_strings(HEADER + '''
Walter Lozano361e7332020-06-25 01:10:08 -0300377struct dtd_invalid {
378};
379''', data)
380
Simon Glass67b5ec52020-12-28 20:34:47 -0700381 with test_util.capture_sys_output() as _:
Simon Glassa32eb7d2021-02-03 06:00:51 -0700382 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [],
Simon Glassb00f0062021-02-03 06:01:02 -0700383 None, scan=copy_scan())
Walter Lozano361e7332020-06-25 01:10:08 -0300384 with open(output) as infile:
385 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700386 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600387/* Node /spl-test index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300388static struct dtd_invalid dtv_spl_test = {
Walter Lozano361e7332020-06-25 01:10:08 -0300389};
Simon Glass20e442a2020-12-28 20:34:54 -0700390U_BOOT_DRVINFO(spl_test) = {
Walter Lozano361e7332020-06-25 01:10:08 -0300391\t.name\t\t= "invalid",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700392\t.plat\t= &dtv_spl_test,
Simon Glass4f500862020-12-03 16:55:19 -0700393\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glasse41651f2020-10-03 11:31:35 -0600394\t.parent_idx\t= -1,
Walter Lozano361e7332020-06-25 01:10:08 -0300395};
396
397''', data)
398
Simon Glassc0791922017-06-18 22:09:06 -0600399 def test_phandle(self):
400 """Test output from a node containing a phandle reference"""
401 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
402 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300403 self.run_test(['struct'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600404 with open(output) as infile:
405 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700406 self._check_strings(HEADER + '''
Simon Glassc0791922017-06-18 22:09:06 -0600407struct dtd_source {
Simon Glass634eba42017-08-29 14:15:59 -0600408\tstruct phandle_2_arg clocks[4];
Simon Glassc0791922017-06-18 22:09:06 -0600409};
410struct dtd_target {
411\tfdt32_t\t\tintval;
412};
413''', data)
414
Walter Lozano361e7332020-06-25 01:10:08 -0300415 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600416 with open(output) as infile:
417 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700418 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600419/* Node /phandle2-target index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300420static struct dtd_target dtv_phandle2_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600421\t.intval\t\t\t= 0x1,
422};
Simon Glass20e442a2020-12-28 20:34:54 -0700423U_BOOT_DRVINFO(phandle2_target) = {
Simon Glass634eba42017-08-29 14:15:59 -0600424\t.name\t\t= "target",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700425\t.plat\t= &dtv_phandle2_target,
Simon Glass4f500862020-12-03 16:55:19 -0700426\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600427\t.parent_idx\t= -1,
Simon Glass634eba42017-08-29 14:15:59 -0600428};
429
Simon Glass1b272732020-10-03 11:31:25 -0600430/* Node /phandle3-target index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300431static struct dtd_target dtv_phandle3_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600432\t.intval\t\t\t= 0x2,
433};
Simon Glass20e442a2020-12-28 20:34:54 -0700434U_BOOT_DRVINFO(phandle3_target) = {
Simon Glass634eba42017-08-29 14:15:59 -0600435\t.name\t\t= "target",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700436\t.plat\t= &dtv_phandle3_target,
Simon Glass4f500862020-12-03 16:55:19 -0700437\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600438\t.parent_idx\t= -1,
Simon Glass634eba42017-08-29 14:15:59 -0600439};
440
Simon Glass1b272732020-10-03 11:31:25 -0600441/* Node /phandle-source index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300442static struct dtd_source dtv_phandle_source = {
Simon Glass35d50372017-08-29 14:15:57 -0600443\t.clocks\t\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600444\t\t\t{4, {}},
445\t\t\t{0, {11}},
446\t\t\t{1, {12, 13}},
447\t\t\t{4, {}},},
Simon Glassc0791922017-06-18 22:09:06 -0600448};
Simon Glass20e442a2020-12-28 20:34:54 -0700449U_BOOT_DRVINFO(phandle_source) = {
Simon Glassc0791922017-06-18 22:09:06 -0600450\t.name\t\t= "source",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700451\t.plat\t= &dtv_phandle_source,
Simon Glass4f500862020-12-03 16:55:19 -0700452\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glasse41651f2020-10-03 11:31:35 -0600453\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600454};
455
Simon Glass1b272732020-10-03 11:31:25 -0600456/* Node /phandle-source2 index 3 */
Walter Lozano51f12632020-06-25 01:10:13 -0300457static struct dtd_source dtv_phandle_source2 = {
Simon Glass760b7172018-07-06 10:27:31 -0600458\t.clocks\t\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600459\t\t\t{4, {}},},
Simon Glass760b7172018-07-06 10:27:31 -0600460};
Simon Glass20e442a2020-12-28 20:34:54 -0700461U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass760b7172018-07-06 10:27:31 -0600462\t.name\t\t= "source",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700463\t.plat\t= &dtv_phandle_source2,
Simon Glass4f500862020-12-03 16:55:19 -0700464\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glasse41651f2020-10-03 11:31:35 -0600465\t.parent_idx\t= -1,
Simon Glass760b7172018-07-06 10:27:31 -0600466};
467
Simon Glass9eca08d2020-12-28 20:35:04 -0700468/* Node /phandle-target index 4 */
469static struct dtd_target dtv_phandle_target = {
470\t.intval\t\t\t= 0x0,
471};
472U_BOOT_DRVINFO(phandle_target) = {
473\t.name\t\t= "target",
474\t.plat\t= &dtv_phandle_target,
475\t.plat_size\t= sizeof(dtv_phandle_target),
476\t.parent_idx\t= -1,
477};
478
Simon Glassc0791922017-06-18 22:09:06 -0600479''', data)
480
Simon Glass8512ea22018-07-06 10:27:35 -0600481 def test_phandle_single(self):
482 """Test output from a node containing a phandle reference"""
483 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
484 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300485 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600486 with open(output) as infile:
487 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700488 self._check_strings(HEADER + '''
Simon Glass8512ea22018-07-06 10:27:35 -0600489struct dtd_source {
490\tstruct phandle_0_arg clocks[1];
491};
492struct dtd_target {
493\tfdt32_t\t\tintval;
494};
495''', data)
496
497 def test_phandle_reorder(self):
498 """Test that phandle targets are generated before their references"""
499 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
500 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300501 self.run_test(['platdata'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600502 with open(output) as infile:
503 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700504 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600505/* Node /phandle-source2 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300506static struct dtd_source dtv_phandle_source2 = {
Simon Glass8512ea22018-07-06 10:27:35 -0600507\t.clocks\t\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600508\t\t\t{1, {}},},
Simon Glass8512ea22018-07-06 10:27:35 -0600509};
Simon Glass20e442a2020-12-28 20:34:54 -0700510U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass8512ea22018-07-06 10:27:35 -0600511\t.name\t\t= "source",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700512\t.plat\t= &dtv_phandle_source2,
Simon Glass4f500862020-12-03 16:55:19 -0700513\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glasse41651f2020-10-03 11:31:35 -0600514\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -0600515};
516
Simon Glass9eca08d2020-12-28 20:35:04 -0700517/* Node /phandle-target index 1 */
518static struct dtd_target dtv_phandle_target = {
519};
520U_BOOT_DRVINFO(phandle_target) = {
521\t.name\t\t= "target",
522\t.plat\t= &dtv_phandle_target,
523\t.plat_size\t= sizeof(dtv_phandle_target),
524\t.parent_idx\t= -1,
525};
526
Simon Glass8512ea22018-07-06 10:27:35 -0600527''', data)
528
Walter Lozano6c3fc502020-06-25 01:10:17 -0300529 def test_phandle_cd_gpio(self):
530 """Test that phandle targets are generated when unsing cd-gpios"""
531 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
532 output = tools.GetOutputFilename('output')
Simon Glassa32eb7d2021-02-03 06:00:51 -0700533 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [], True,
534 scan=copy_scan())
Walter Lozano6c3fc502020-06-25 01:10:17 -0300535 with open(output) as infile:
536 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700537 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600538/* Node /phandle2-target index 0 */
Walter Lozano6c3fc502020-06-25 01:10:17 -0300539static struct dtd_target dtv_phandle2_target = {
540\t.intval\t\t\t= 0x1,
541};
Simon Glass20e442a2020-12-28 20:34:54 -0700542U_BOOT_DRVINFO(phandle2_target) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -0300543\t.name\t\t= "target",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700544\t.plat\t= &dtv_phandle2_target,
Simon Glass4f500862020-12-03 16:55:19 -0700545\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600546\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -0300547};
548
Simon Glass1b272732020-10-03 11:31:25 -0600549/* Node /phandle3-target index 1 */
Walter Lozano6c3fc502020-06-25 01:10:17 -0300550static struct dtd_target dtv_phandle3_target = {
551\t.intval\t\t\t= 0x2,
552};
Simon Glass20e442a2020-12-28 20:34:54 -0700553U_BOOT_DRVINFO(phandle3_target) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -0300554\t.name\t\t= "target",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700555\t.plat\t= &dtv_phandle3_target,
Simon Glass4f500862020-12-03 16:55:19 -0700556\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600557\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -0300558};
559
Simon Glass1b272732020-10-03 11:31:25 -0600560/* Node /phandle-source index 2 */
Walter Lozano6c3fc502020-06-25 01:10:17 -0300561static struct dtd_source dtv_phandle_source = {
562\t.cd_gpios\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600563\t\t\t{4, {}},
564\t\t\t{0, {11}},
565\t\t\t{1, {12, 13}},
566\t\t\t{4, {}},},
Walter Lozano6c3fc502020-06-25 01:10:17 -0300567};
Simon Glass20e442a2020-12-28 20:34:54 -0700568U_BOOT_DRVINFO(phandle_source) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -0300569\t.name\t\t= "source",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700570\t.plat\t= &dtv_phandle_source,
Simon Glass4f500862020-12-03 16:55:19 -0700571\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glasse41651f2020-10-03 11:31:35 -0600572\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -0300573};
574
Simon Glass1b272732020-10-03 11:31:25 -0600575/* Node /phandle-source2 index 3 */
Walter Lozano6c3fc502020-06-25 01:10:17 -0300576static struct dtd_source dtv_phandle_source2 = {
577\t.cd_gpios\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600578\t\t\t{4, {}},},
Walter Lozano6c3fc502020-06-25 01:10:17 -0300579};
Simon Glass20e442a2020-12-28 20:34:54 -0700580U_BOOT_DRVINFO(phandle_source2) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -0300581\t.name\t\t= "source",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700582\t.plat\t= &dtv_phandle_source2,
Simon Glass4f500862020-12-03 16:55:19 -0700583\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glasse41651f2020-10-03 11:31:35 -0600584\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -0300585};
586
Simon Glass9eca08d2020-12-28 20:35:04 -0700587/* Node /phandle-target index 4 */
588static struct dtd_target dtv_phandle_target = {
589\t.intval\t\t\t= 0x0,
590};
591U_BOOT_DRVINFO(phandle_target) = {
592\t.name\t\t= "target",
593\t.plat\t= &dtv_phandle_target,
594\t.plat_size\t= sizeof(dtv_phandle_target),
595\t.parent_idx\t= -1,
596};
597
Walter Lozano6c3fc502020-06-25 01:10:17 -0300598''', data)
599
Simon Glass8512ea22018-07-06 10:27:35 -0600600 def test_phandle_bad(self):
601 """Test a node containing an invalid phandle fails"""
Simon Glass4b4bc062018-10-01 21:12:43 -0600602 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
603 capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600604 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700605 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300606 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600607 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
Simon Glass67b5ec52020-12-28 20:34:47 -0700608 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -0600609
610 def test_phandle_bad2(self):
611 """Test a phandle target missing its #*-cells property"""
Simon Glass4b4bc062018-10-01 21:12:43 -0600612 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
613 capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600614 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700615 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300616 self.run_test(['struct'], dtb_file, output)
Walter Lozanoad340172020-06-25 01:10:16 -0300617 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glass67b5ec52020-12-28 20:34:47 -0700618 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -0600619
Simon Glassc20ee0e2017-08-29 14:15:50 -0600620 def test_addresses64(self):
621 """Test output from a node with a 'reg' property with na=2, ns=2"""
622 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
623 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300624 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600625 with open(output) as infile:
626 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700627 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600628struct dtd_test1 {
629\tfdt64_t\t\treg[2];
630};
631struct dtd_test2 {
632\tfdt64_t\t\treg[2];
633};
634struct dtd_test3 {
635\tfdt64_t\t\treg[4];
636};
637''', data)
638
Walter Lozano361e7332020-06-25 01:10:08 -0300639 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600640 with open(output) as infile:
641 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700642 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600643/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300644static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600645\t.reg\t\t\t= {0x1234, 0x5678},
646};
Simon Glass20e442a2020-12-28 20:34:54 -0700647U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600648\t.name\t\t= "test1",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700649\t.plat\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -0700650\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -0600651\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600652};
653
Simon Glass1b272732020-10-03 11:31:25 -0600654/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300655static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600656\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
657};
Simon Glass20e442a2020-12-28 20:34:54 -0700658U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600659\t.name\t\t= "test2",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700660\t.plat\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700661\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600662\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600663};
664
Simon Glass1b272732020-10-03 11:31:25 -0600665/* Node /test3 index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300666static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600667\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
668};
Simon Glass20e442a2020-12-28 20:34:54 -0700669U_BOOT_DRVINFO(test3) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600670\t.name\t\t= "test3",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700671\t.plat\t= &dtv_test3,
Simon Glass4f500862020-12-03 16:55:19 -0700672\t.plat_size\t= sizeof(dtv_test3),
Simon Glasse41651f2020-10-03 11:31:35 -0600673\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600674};
675
Simon Glassd960f0d2020-12-28 20:35:05 -0700676''', data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600677
678 def test_addresses32(self):
679 """Test output from a node with a 'reg' property with na=1, ns=1"""
680 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
681 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300682 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600683 with open(output) as infile:
684 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700685 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600686struct dtd_test1 {
687\tfdt32_t\t\treg[2];
688};
689struct dtd_test2 {
690\tfdt32_t\t\treg[4];
691};
692''', data)
693
Walter Lozano361e7332020-06-25 01:10:08 -0300694 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600695 with open(output) as infile:
696 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700697 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600698/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300699static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600700\t.reg\t\t\t= {0x1234, 0x5678},
701};
Simon Glass20e442a2020-12-28 20:34:54 -0700702U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600703\t.name\t\t= "test1",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700704\t.plat\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -0700705\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -0600706\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600707};
708
Simon Glass1b272732020-10-03 11:31:25 -0600709/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300710static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600711\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
712};
Simon Glass20e442a2020-12-28 20:34:54 -0700713U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600714\t.name\t\t= "test2",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700715\t.plat\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700716\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600717\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600718};
719
Simon Glassd960f0d2020-12-28 20:35:05 -0700720''', data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600721
722 def test_addresses64_32(self):
723 """Test output from a node with a 'reg' property with na=2, ns=1"""
724 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
725 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300726 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600727 with open(output) as infile:
728 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700729 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600730struct dtd_test1 {
731\tfdt64_t\t\treg[2];
732};
733struct dtd_test2 {
734\tfdt64_t\t\treg[2];
735};
736struct dtd_test3 {
737\tfdt64_t\t\treg[4];
738};
739''', data)
740
Walter Lozano361e7332020-06-25 01:10:08 -0300741 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600742 with open(output) as infile:
743 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700744 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600745/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300746static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600747\t.reg\t\t\t= {0x123400000000, 0x5678},
748};
Simon Glass20e442a2020-12-28 20:34:54 -0700749U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600750\t.name\t\t= "test1",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700751\t.plat\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -0700752\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -0600753\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600754};
755
Simon Glass1b272732020-10-03 11:31:25 -0600756/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300757static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600758\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
759};
Simon Glass20e442a2020-12-28 20:34:54 -0700760U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600761\t.name\t\t= "test2",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700762\t.plat\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700763\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600764\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600765};
766
Simon Glass1b272732020-10-03 11:31:25 -0600767/* Node /test3 index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300768static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600769\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
770};
Simon Glass20e442a2020-12-28 20:34:54 -0700771U_BOOT_DRVINFO(test3) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600772\t.name\t\t= "test3",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700773\t.plat\t= &dtv_test3,
Simon Glass4f500862020-12-03 16:55:19 -0700774\t.plat_size\t= sizeof(dtv_test3),
Simon Glasse41651f2020-10-03 11:31:35 -0600775\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600776};
777
Simon Glassd960f0d2020-12-28 20:35:05 -0700778''', data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600779
780 def test_addresses32_64(self):
781 """Test output from a node with a 'reg' property with na=1, ns=2"""
782 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
783 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300784 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600785 with open(output) as infile:
786 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700787 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600788struct dtd_test1 {
789\tfdt64_t\t\treg[2];
790};
791struct dtd_test2 {
792\tfdt64_t\t\treg[2];
793};
794struct dtd_test3 {
795\tfdt64_t\t\treg[4];
796};
797''', data)
798
Walter Lozano361e7332020-06-25 01:10:08 -0300799 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600800 with open(output) as infile:
801 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700802 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600803/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300804static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600805\t.reg\t\t\t= {0x1234, 0x567800000000},
806};
Simon Glass20e442a2020-12-28 20:34:54 -0700807U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600808\t.name\t\t= "test1",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700809\t.plat\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -0700810\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -0600811\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600812};
813
Simon Glass1b272732020-10-03 11:31:25 -0600814/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300815static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600816\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
817};
Simon Glass20e442a2020-12-28 20:34:54 -0700818U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600819\t.name\t\t= "test2",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700820\t.plat\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700821\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600822\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600823};
824
Simon Glass1b272732020-10-03 11:31:25 -0600825/* Node /test3 index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300826static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600827\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
828};
Simon Glass20e442a2020-12-28 20:34:54 -0700829U_BOOT_DRVINFO(test3) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600830\t.name\t\t= "test3",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700831\t.plat\t= &dtv_test3,
Simon Glass4f500862020-12-03 16:55:19 -0700832\t.plat_size\t= sizeof(dtv_test3),
Simon Glasse41651f2020-10-03 11:31:35 -0600833\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600834};
835
Simon Glassd960f0d2020-12-28 20:35:05 -0700836''', data)
Simon Glass8512ea22018-07-06 10:27:35 -0600837
838 def test_bad_reg(self):
839 """Test that a reg property with an invalid type generates an error"""
Simon Glassfe57c782018-07-06 10:27:37 -0600840 # Capture stderr since dtc will emit warnings for this file
841 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600842 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700843 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300844 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600845 self.assertIn("Node 'spl-test' reg property is not an int",
Simon Glass67b5ec52020-12-28 20:34:47 -0700846 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -0600847
848 def test_bad_reg2(self):
849 """Test that a reg property with an invalid cell count is detected"""
Simon Glassfe57c782018-07-06 10:27:37 -0600850 # Capture stderr since dtc will emit warnings for this file
851 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600852 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700853 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300854 self.run_test(['struct'], dtb_file, output)
Simon Glass67b5ec52020-12-28 20:34:47 -0700855 self.assertIn(
856 "Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
857 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -0600858
859 def test_add_prop(self):
860 """Test that a subequent node can add a new property to a struct"""
861 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
862 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300863 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600864 with open(output) as infile:
865 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700866 self._check_strings(HEADER + '''
Simon Glass8512ea22018-07-06 10:27:35 -0600867struct dtd_sandbox_spl_test {
868\tfdt32_t\t\tintarray;
869\tfdt32_t\t\tintval;
870};
871''', data)
872
Walter Lozano361e7332020-06-25 01:10:08 -0300873 self.run_test(['platdata'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600874 with open(output) as infile:
875 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700876 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600877/* Node /spl-test index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300878static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass8512ea22018-07-06 10:27:35 -0600879\t.intval\t\t\t= 0x1,
880};
Simon Glass20e442a2020-12-28 20:34:54 -0700881U_BOOT_DRVINFO(spl_test) = {
Simon Glass8512ea22018-07-06 10:27:35 -0600882\t.name\t\t= "sandbox_spl_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700883\t.plat\t= &dtv_spl_test,
Simon Glass4f500862020-12-03 16:55:19 -0700884\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glasse41651f2020-10-03 11:31:35 -0600885\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -0600886};
887
Simon Glass1b272732020-10-03 11:31:25 -0600888/* Node /spl-test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300889static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass8512ea22018-07-06 10:27:35 -0600890\t.intarray\t\t= 0x5,
891};
Simon Glass20e442a2020-12-28 20:34:54 -0700892U_BOOT_DRVINFO(spl_test2) = {
Simon Glass8512ea22018-07-06 10:27:35 -0600893\t.name\t\t= "sandbox_spl_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700894\t.plat\t= &dtv_spl_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700895\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600896\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -0600897};
898
Simon Glassd960f0d2020-12-28 20:35:05 -0700899''', data)
Simon Glass8512ea22018-07-06 10:27:35 -0600900
Simon Glass67b5ec52020-12-28 20:34:47 -0700901 def test_stdout(self):
Simon Glass8512ea22018-07-06 10:27:35 -0600902 """Test output to stdout"""
903 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glassde846cb2020-12-28 20:34:49 -0700904 with test_util.capture_sys_output() as (stdout, _):
Simon Glassf62cea02020-12-28 20:34:48 -0700905 self.run_test(['struct'], dtb_file, None)
Simon Glassde846cb2020-12-28 20:34:49 -0700906 self._check_strings(self.struct_text, stdout.getvalue())
Simon Glass8512ea22018-07-06 10:27:35 -0600907
Simon Glassbe44f272020-12-28 20:34:51 -0700908 def test_multi_to_file(self):
909 """Test output of multiple pieces to a single file"""
910 dtb_file = get_dtb_file('dtoc_test_simple.dts')
911 output = tools.GetOutputFilename('output')
Simon Glass10cbd3b2020-12-28 20:34:52 -0700912 self.run_test(['all'], dtb_file, output)
Simon Glassbe44f272020-12-28 20:34:51 -0700913 data = tools.ReadFile(output, binary=False)
Simon Glass10cbd3b2020-12-28 20:34:52 -0700914 self._check_strings(self.platdata_text + self.struct_text, data)
Simon Glassbe44f272020-12-28 20:34:51 -0700915
Simon Glass67b5ec52020-12-28 20:34:47 -0700916 def test_no_command(self):
Simon Glass8512ea22018-07-06 10:27:35 -0600917 """Test running dtoc without a command"""
Simon Glass67b5ec52020-12-28 20:34:47 -0700918 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300919 self.run_test([], '', '')
Simon Glass8512ea22018-07-06 10:27:35 -0600920 self.assertIn("Please specify a command: struct, platdata",
Simon Glass67b5ec52020-12-28 20:34:47 -0700921 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -0600922
Simon Glass67b5ec52020-12-28 20:34:47 -0700923 def test_bad_command(self):
Simon Glass8512ea22018-07-06 10:27:35 -0600924 """Test running dtoc with an invalid command"""
925 dtb_file = get_dtb_file('dtoc_test_simple.dts')
926 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700927 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300928 self.run_test(['invalid-cmd'], dtb_file, output)
Simon Glass10cbd3b2020-12-28 20:34:52 -0700929 self.assertIn("Unknown command 'invalid-cmd': (use: platdata, struct)",
Simon Glass67b5ec52020-12-28 20:34:47 -0700930 str(exc.exception))
Walter Lozano6c74d1b2020-07-28 19:06:23 -0300931
Simon Glass10cbd3b2020-12-28 20:34:52 -0700932 def test_output_conflict(self):
933 """Test a conflict between and output dirs and output file"""
934 with self.assertRaises(ValueError) as exc:
Simon Glassb00f0062021-02-03 06:01:02 -0700935 dtb_platdata.run_steps(['all'], None, False, 'out', ['cdir'], None,
936 warning_disabled=True, scan=copy_scan())
Simon Glass10cbd3b2020-12-28 20:34:52 -0700937 self.assertIn("Must specify either output or output_dirs, not both",
938 str(exc.exception))
939
940 def test_output_dirs(self):
941 """Test outputting files to a directory"""
942 # Remove the directory so that files from other tests are not there
943 tools._RemoveOutputDir()
944 tools.PrepareOutputDir(None)
945
946 # This should create the .dts and .dtb in the output directory
947 dtb_file = get_dtb_file('dtoc_test_simple.dts')
948 outdir = tools.GetOutputDir()
949 fnames = glob.glob(outdir + '/*')
950 self.assertEqual(2, len(fnames))
951
Simon Glassb00f0062021-02-03 06:01:02 -0700952 dtb_platdata.run_steps(['all'], dtb_file, False, None, [outdir], None,
953 warning_disabled=True, scan=copy_scan())
Simon Glass10cbd3b2020-12-28 20:34:52 -0700954 fnames = glob.glob(outdir + '/*')
955 self.assertEqual(4, len(fnames))
956
957 leafs = set(os.path.basename(fname) for fname in fnames)
958 self.assertEqual(
Simon Glassf31fa992020-12-28 20:35:01 -0700959 {'dt-structs-gen.h', 'source.dts', 'dt-plat.c', 'source.dtb'},
Simon Glass10cbd3b2020-12-28 20:34:52 -0700960 leafs)
Simon Glassfd471e22021-02-03 06:01:00 -0700961
962 def setup_process_test(self):
963 """Set up a test of process_nodes()
964
965 This uses saved_scan but returns a deep copy of it, so it is safe to
966 modify it in these tests
967
968 Returns:
969 tuple:
970 DtbPlatdata: object to test
971 Scanner: scanner to use
972 """
973 dtb_file = get_dtb_file('dtoc_test_simple.dts')
974 output = tools.GetOutputFilename('output')
975
976 # Take a copy before messing with it
977 scan = copy.deepcopy(saved_scan)
978 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
979 plat.scan_dtb()
980 plat.scan_tree()
981 plat.prepare_nodes()
982 return plat, scan
983
984 def test_process_nodes(self):
985 """Test processing nodes to add various info"""
986 plat, scan = self.setup_process_test()
987 plat.process_nodes(True)
988
989 i2c_node = plat._fdt.GetNode('/i2c@0')
990 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
991 pmic = scan._drivers['sandbox_pmic']
992 i2c = scan._drivers['sandbox_i2c']
993 self.assertEqual('DM_DEVICE_REF(pmic_at_9)', pmic_node.dev_ref)
994 self.assertEqual(pmic, pmic_node.driver)
995 self.assertEqual(i2c_node, pmic_node.parent)
996 self.assertEqual(i2c, pmic_node.parent_driver)
997
998 # The pmic is the only child
999 self.assertEqual(pmic_node.parent_seq, 0)
1000 self.assertEqual([pmic_node], i2c_node.child_devs)
1001
1002 # Start and end of the list should be the child_head
1003 ref = '&DM_DEVICE_REF(i2c_at_0)->child_head'
1004 self.assertEqual(
1005 {-1: ref, 0: '&DM_DEVICE_REF(pmic_at_9)->sibling_node', 1: ref},
1006 i2c_node.child_refs)
1007
1008 def test_process_nodes_bad_parent(self):
1009 # Pretend that i2c has a parent (the pmic) and delete that driver
1010 plat, scan = self.setup_process_test()
1011
1012 i2c_node = plat._fdt.GetNode('/i2c@0')
1013 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
1014 del scan._drivers['sandbox_pmic']
1015 i2c_node.parent = pmic_node
1016
1017 # Process twice, the second time to generate an exception
1018 plat.process_nodes(False)
1019 with self.assertRaises(ValueError) as exc:
1020 plat.process_nodes(True)
1021 self.assertIn(
1022 "Cannot parse/find parent driver 'sandbox_pmic' for 'sandbox_i2c",
1023 str(exc.exception))
1024
1025 def test_process_nodes_bad_node(self):
1026 plat, scan = self.setup_process_test()
1027
1028 # Now remove the pmic driver
1029 del scan._drivers['sandbox_pmic']
1030
1031 # Process twice, the second time to generate an exception
1032 plat.process_nodes(False)
1033 with self.assertRaises(ValueError) as exc:
1034 plat.process_nodes(True)
1035 self.assertIn("Cannot parse/find driver for 'sandbox_pmic",
1036 str(exc.exception))
Simon Glassb9319c42021-02-03 06:01:01 -07001037
Simon Glass337d6972021-02-03 06:01:10 -07001038 def test_process_nodes_bad_uclass(self):
1039 plat, scan = self.setup_process_test()
1040
1041 self.assertIn('UCLASS_I2C', scan._uclass)
1042 del scan._uclass['UCLASS_I2C']
1043 with self.assertRaises(ValueError) as exc:
1044 plat.process_nodes(True)
1045 self.assertIn("Cannot parse/find uclass 'UCLASS_I2C' for driver 'sandbox_i2c'",
1046 str(exc.exception))
1047
Simon Glassb9319c42021-02-03 06:01:01 -07001048 def test_process_nodes_used(self):
1049 """Test processing nodes to add various info"""
1050 plat, scan = self.setup_process_test()
1051 plat.process_nodes(True)
1052
1053 pmic = scan._drivers['sandbox_pmic']
1054 self.assertTrue(pmic.used)
1055
1056 gpio = scan._drivers['sandbox_gpio']
1057 self.assertFalse(gpio.used)
Simon Glass05953522021-02-03 06:01:07 -07001058
1059 def test_alias_read(self):
1060 """Test obtaining aliases"""
1061 dtb_file = get_dtb_file('dtoc_test_inst.dts')
1062 output = tools.GetOutputFilename('output')
1063 plat = self.run_test(['struct'], dtb_file, output)
1064
1065 scan = plat._scan
1066 testfdt_node = plat._fdt.GetNode('/some-bus/test')
Simon Glass337d6972021-02-03 06:01:10 -07001067 test0_node = plat._fdt.GetNode('/some-bus/test0')
Simon Glass05953522021-02-03 06:01:07 -07001068 self.assertIn('UCLASS_TEST_FDT', scan._uclass)
1069 uc = scan._uclass['UCLASS_TEST_FDT']
Simon Glass337d6972021-02-03 06:01:10 -07001070 self.assertEqual({1: testfdt_node, 2: test0_node},
1071 uc.alias_num_to_node)
1072 self.assertEqual({'/some-bus/test': 1, '/some-bus/test0': 2},
1073 uc.alias_path_to_num)
Simon Glass05953522021-02-03 06:01:07 -07001074
1075 # Try adding an alias that doesn't exist
1076 self.assertFalse(scan.add_uclass_alias('fred', 3, None))
1077
1078 # Try adding an alias for a missing node
1079 self.assertIsNone(scan.add_uclass_alias('testfdt', 3, None))
1080
1081 def test_alias_read_bad(self):
1082 """Test invalid alias property name"""
1083 dtb_file = get_dtb_file('dtoc_test_alias_bad.dts')
1084 output = tools.GetOutputFilename('output')
1085 with self.assertRaises(ValueError) as exc:
1086 plat = self.run_test(['struct'], dtb_file, output)
1087 self.assertIn("Cannot decode alias 'i2c4-'", str(exc.exception))
1088
1089 def test_alias_read_bad_path(self):
1090 """Test alias pointing to a non-existent node"""
1091 # This line may produce a warning, so capture it:
1092 # Warning (alias_paths): /aliases:i2c4: aliases property is not a valid
1093 # node (/does/not/exist)
1094 dtb_file = get_dtb_file('dtoc_test_alias_bad_path.dts', True)
1095
1096 output = tools.GetOutputFilename('output')
1097 with self.assertRaises(ValueError) as exc:
1098 plat = self.run_test(['struct'], dtb_file, output)
1099 self.assertIn("Alias 'i2c4' path '/does/not/exist' not found",
1100 str(exc.exception))
1101
1102 def test_alias_read_bad_uclass(self):
1103 """Test alias for a uclass that doesn't exist"""
1104 dtb_file = get_dtb_file('dtoc_test_alias_bad_uc.dts')
1105 output = tools.GetOutputFilename('output')
1106 with test_util.capture_sys_output() as (stdout, _):
1107 plat = self.run_test(['struct'], dtb_file, output)
1108 self.assertEqual("Could not find uclass for alias 'other1'",
1109 stdout.getvalue().strip())
Simon Glass074197a2021-02-03 06:01:09 -07001110
1111 def test_sequence(self):
1112 """Test assignment of sequence numnbers"""
1113 dtb_file = get_dtb_file('dtoc_test_inst.dts')
1114 output = tools.GetOutputFilename('output')
1115 plat = self.run_test(['struct'], dtb_file, output)
Simon Glass337d6972021-02-03 06:01:10 -07001116
1117 scan = plat._scan
1118 testfdt = plat._fdt.GetNode('/some-bus/test')
1119 self.assertEqual(1, testfdt.seq)
1120 i2c = plat._fdt.GetNode('/i2c')
1121
1122 # For now this uclass is not compiled in, so no sequence is assigned
1123 self.assertEqual(4, i2c.seq)
1124 spl = plat._fdt.GetNode('/spl-test')
1125 self.assertEqual(0, spl.seq)