blob: cbddd62424d9b0e8cdba25e17856c7f4a11a6f92 [file] [log] [blame]
Simon Glassbfb0bb22019-10-31 07:42:55 -06001#!/usr/bin/env python3
Tom Rini10e47792018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glass9d2eb922017-06-18 22:09:06 -06003# Copyright (c) 2012 The Chromium OS Authors.
4#
Simon Glass9d2eb922017-06-18 22:09:06 -06005
6"""Tests for the dtb_platdata module
7
Simon Glass70cd0d72018-07-06 10:27:20 -06008This includes unit tests for some functions and functional tests for the dtoc
9tool.
Simon Glass9d2eb922017-06-18 22:09:06 -060010"""
11
12import collections
Simon Glass768ff0a2021-02-03 06:00:51 -070013import copy
Simon Glass4e8e8462020-12-28 20:34:52 -070014import glob
Simon Glass9d2eb922017-06-18 22:09:06 -060015import os
16import struct
17import unittest
18
Simon Glass9d2eb922017-06-18 22:09:06 -060019from dtb_platdata import get_value
20from dtb_platdata import tab_to
Simon Glassa60cabd2020-12-28 20:34:47 -070021from dtoc import dtb_platdata
Simon Glassa997ea52020-04-17 18:09:04 -060022from dtoc import fdt
23from dtoc import fdt_util
Simon Glass768ff0a2021-02-03 06:00:51 -070024from dtoc import src_scan
Simon Glass9065bc92020-12-28 20:35:06 -070025from dtoc.src_scan import conv_name_to_c
26from dtoc.src_scan import get_compat_name
Simon Glassa997ea52020-04-17 18:09:04 -060027from patman import test_util
28from patman import tools
Simon Glass9d2eb922017-06-18 22:09:06 -060029
Simon Glassa60cabd2020-12-28 20:34:47 -070030OUR_PATH = os.path.dirname(os.path.realpath(__file__))
Simon Glass9d2eb922017-06-18 22:09:06 -060031
32
Simon Glasseb37e2d2017-11-12 21:52:17 -070033HEADER = '''/*
34 * DO NOT MODIFY
35 *
Simon Glass6b208842020-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 Glasseb37e2d2017-11-12 21:52:17 -070038 */
39
40#include <stdbool.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090041#include <linux/libfdt.h>'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070042
43C_HEADER = '''/*
44 * DO NOT MODIFY
45 *
Simon Glass6b208842020-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 Glasseb37e2d2017-11-12 21:52:17 -070048 */
49
Simon Glass1d8364a2020-12-28 20:34:54 -070050/* Allow use of U_BOOT_DRVINFO() in this file */
Simon Glassbeddd7a2020-12-28 20:35:01 -070051#define DT_PLAT_C
Simon Glass4c73d7b2020-10-03 11:31:41 -060052
Simon Glasseb37e2d2017-11-12 21:52:17 -070053#include <common.h>
54#include <dm.h>
55#include <dt-structs.h>
56'''
57
Simon Glass768ff0a2021-02-03 06:00:51 -070058# Scanner saved from a previous run of the tests (to speed things up)
59saved_scan = None
60
Simon Glassa60cabd2020-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 Glass3bce93d2018-07-06 10:27:37 -060064
65def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glass9d2eb922017-06-18 22:09:06 -060066 """Compile a .dts file to a .dtb
67
68 Args:
Simon Glassa60cabd2020-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 Glass9d2eb922017-06-18 22:09:06 -060071
72 Returns:
Simon Glassa60cabd2020-12-28 20:34:47 -070073 str: Filename of compiled file in output directory
Simon Glass9d2eb922017-06-18 22:09:06 -060074 """
Simon Glass4f4b2402021-02-03 06:00:56 -070075 return fdt_util.EnsureCompiled(os.path.join(OUR_PATH, 'test', dts_fname),
Simon Glass3bce93d2018-07-06 10:27:37 -060076 capture_stderr=capture_stderr)
Simon Glass9d2eb922017-06-18 22:09:06 -060077
Simon Glass768ff0a2021-02-03 06:00:51 -070078
79def 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
Simon Glass9d2eb922017-06-18 22:09:06 -060091
92class TestDtoc(unittest.TestCase):
93 """Tests for dtoc"""
94 @classmethod
95 def setUpClass(cls):
96 tools.PrepareOutputDir(None)
Simon Glass7f5e2262020-07-07 21:32:06 -060097 cls.maxDiff = None
Simon Glass9d2eb922017-06-18 22:09:06 -060098
99 @classmethod
100 def tearDownClass(cls):
Simon Glassa60cabd2020-12-28 20:34:47 -0700101 tools.FinaliseOutputDir()
Simon Glass9d2eb922017-06-18 22:09:06 -0600102
Simon Glassa60cabd2020-12-28 20:34:47 -0700103 @staticmethod
104 def _write_python_string(fname, data):
Simon Glassc47c2b32018-07-06 10:27:25 -0600105 """Write a string with tabs expanded as done in this Python file
106
107 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -0700108 fname (str): Filename to write to
109 data (str): Raw string to convert
Simon Glassc47c2b32018-07-06 10:27:25 -0600110 """
111 data = data.replace('\t', '\\t')
Simon Glassa60cabd2020-12-28 20:34:47 -0700112 with open(fname, 'w') as fout:
113 fout.write(data)
Simon Glassc47c2b32018-07-06 10:27:25 -0600114
Simon Glassa60cabd2020-12-28 20:34:47 -0700115 def _check_strings(self, expected, actual):
Simon Glassc47c2b32018-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 Glassa60cabd2020-12-28 20:34:47 -0700123 expected (str): Expected string
124 actual (str): Actual string
Simon Glassc47c2b32018-07-06 10:27:25 -0600125 """
126 if expected != actual:
Simon Glassa60cabd2020-12-28 20:34:47 -0700127 self._write_python_string('/tmp/binman.expected', expected)
128 self._write_python_string('/tmp/binman.actual', actual)
Simon Glass61b88e52019-05-17 22:00:31 -0600129 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glassa60cabd2020-12-28 20:34:47 -0700130 self.assertEqual(expected, actual)
Simon Glassc47c2b32018-07-06 10:27:25 -0600131
Simon Glassa60cabd2020-12-28 20:34:47 -0700132 @staticmethod
Simon Glass3809ad92021-02-03 06:01:12 -0700133 def run_test(args, dtb_file, output, instantiate=False):
Simon Glassa60cabd2020-12-28 20:34:47 -0700134 """Run a test using dtoc
Walter Lozanoa324e412020-06-25 01:10:08 -0300135
Simon Glassa60cabd2020-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 Glassbe88d2f2021-02-03 06:01:07 -0700140
141 Returns:
142 DtbPlatdata object
Simon Glassa60cabd2020-12-28 20:34:47 -0700143 """
Simon Glass80d782c42021-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 Glass3809ad92021-02-03 06:01:12 -0700146 return dtb_platdata.run_steps(
147 args, dtb_file, False, output, [], None, instantiate,
148 warning_disabled=True, scan=copy_scan())
Walter Lozanoa324e412020-06-25 01:10:08 -0300149
Simon Glass9d2eb922017-06-18 22:09:06 -0600150 def test_name(self):
151 """Test conversion of device tree names to C identifiers"""
152 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
153 self.assertEqual('vendor_clock_frequency',
154 conv_name_to_c('vendor,clock-frequency'))
155 self.assertEqual('rockchip_rk3399_sdhci_5_1',
156 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
157
158 def test_tab_to(self):
159 """Test operation of tab_to() function"""
160 self.assertEqual('fred ', tab_to(0, 'fred'))
161 self.assertEqual('fred\t', tab_to(1, 'fred'))
162 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
163 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
164 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
165 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
166
167 def test_get_value(self):
168 """Test operation of get_value() function"""
169 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700170 get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600171 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700172 get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600173 self.assertEqual('0x0',
Simon Glassc9a032c2020-11-08 20:36:17 -0700174 get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
175 self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
176 self.assertEqual('true', get_value(fdt.Type.BOOL, None))
Simon Glass9d2eb922017-06-18 22:09:06 -0600177
178 def test_get_compat_name(self):
179 """Test operation of get_compat_name() function"""
180 Prop = collections.namedtuple('Prop', ['value'])
181 Node = collections.namedtuple('Node', ['props'])
182
183 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
184 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300185 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600186 get_compat_name(node))
187
188 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
189 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300190 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600191 get_compat_name(node))
192
193 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
194 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300195 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
Simon Glassa60cabd2020-12-28 20:34:47 -0700196 'arasan_sdhci_5_1', 'third']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600197 get_compat_name(node))
198
199 def test_empty_file(self):
200 """Test output from a device tree file with no nodes"""
201 dtb_file = get_dtb_file('dtoc_test_empty.dts')
202 output = tools.GetOutputFilename('output')
Simon Glass768ff0a2021-02-03 06:00:51 -0700203
204 # Run this one without saved_scan to complete test coverage
Simon Glass3809ad92021-02-03 06:01:12 -0700205 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [], None,
206 False)
Simon Glass9d2eb922017-06-18 22:09:06 -0600207 with open(output) as infile:
208 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700209 self.assertEqual(HEADER.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600210
Walter Lozanoa324e412020-06-25 01:10:08 -0300211 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600212 with open(output) as infile:
213 lines = infile.read().splitlines()
Simon Glass2500de22020-12-28 20:35:05 -0700214 self.assertEqual(C_HEADER.splitlines() + [''], lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600215
Simon Glass07ee48e2020-12-28 20:34:49 -0700216 struct_text = HEADER + '''
Simon Glass32a8d222021-02-03 06:00:57 -0700217struct dtd_sandbox_i2c {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600218};
Simon Glass32a8d222021-02-03 06:00:57 -0700219struct dtd_sandbox_pmic {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600220\tbool\t\tlow_power;
221\tfdt64_t\t\treg[2];
222};
Simon Glass9d2eb922017-06-18 22:09:06 -0600223struct dtd_sandbox_spl_test {
Simon Glass7f5e2262020-07-07 21:32:06 -0600224\tconst char * acpi_name;
Simon Glass9d2eb922017-06-18 22:09:06 -0600225\tbool\t\tboolval;
226\tunsigned char\tbytearray[3];
227\tunsigned char\tbyteval;
228\tfdt32_t\t\tintarray[4];
229\tfdt32_t\t\tintval;
230\tunsigned char\tlongbytearray[9];
Simon Glass9c526332018-07-06 10:27:28 -0600231\tunsigned char\tnotstring[5];
Simon Glass9d2eb922017-06-18 22:09:06 -0600232\tconst char *\tstringarray[3];
233\tconst char *\tstringval;
234};
Simon Glass07ee48e2020-12-28 20:34:49 -0700235'''
Simon Glass9d2eb922017-06-18 22:09:06 -0600236
Simon Glass07ee48e2020-12-28 20:34:49 -0700237 platdata_text = C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600238/* Node /i2c@0 index 0 */
Simon Glass32a8d222021-02-03 06:00:57 -0700239static struct dtd_sandbox_i2c dtv_i2c_at_0 = {
Simon Glass192f8132020-10-03 11:31:25 -0600240};
Simon Glass1d8364a2020-12-28 20:34:54 -0700241U_BOOT_DRVINFO(i2c_at_0) = {
Simon Glass32a8d222021-02-03 06:00:57 -0700242\t.name\t\t= "sandbox_i2c",
Simon Glass71fa5b42020-12-03 16:55:18 -0700243\t.plat\t= &dtv_i2c_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700244\t.plat_size\t= sizeof(dtv_i2c_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600245\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600246};
247
248/* Node /i2c@0/pmic@9 index 1 */
Simon Glass32a8d222021-02-03 06:00:57 -0700249static struct dtd_sandbox_pmic dtv_pmic_at_9 = {
Simon Glass192f8132020-10-03 11:31:25 -0600250\t.low_power\t\t= true,
251\t.reg\t\t\t= {0x9, 0x0},
252};
Simon Glass1d8364a2020-12-28 20:34:54 -0700253U_BOOT_DRVINFO(pmic_at_9) = {
Simon Glass32a8d222021-02-03 06:00:57 -0700254\t.name\t\t= "sandbox_pmic",
Simon Glass71fa5b42020-12-03 16:55:18 -0700255\t.plat\t= &dtv_pmic_at_9,
Simon Glass39edb952020-12-03 16:55:19 -0700256\t.plat_size\t= sizeof(dtv_pmic_at_9),
Simon Glass36b15e22020-10-03 11:31:35 -0600257\t.parent_idx\t= 0,
Simon Glass192f8132020-10-03 11:31:25 -0600258};
259
260/* Node /spl-test index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300261static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glassc82de562019-05-17 22:00:32 -0600262\t.boolval\t\t= true,
Simon Glass9d2eb922017-06-18 22:09:06 -0600263\t.bytearray\t\t= {0x6, 0x0, 0x0},
264\t.byteval\t\t= 0x5,
Simon Glassc82de562019-05-17 22:00:32 -0600265\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600266\t.intval\t\t\t= 0x1,
Simon Glass131e0b02017-08-29 14:15:49 -0600267\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
268\t\t0x11},
Simon Glassc82de562019-05-17 22:00:32 -0600269\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600270\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glassc82de562019-05-17 22:00:32 -0600271\t.stringval\t\t= "message",
Simon Glass9d2eb922017-06-18 22:09:06 -0600272};
Simon Glass1d8364a2020-12-28 20:34:54 -0700273U_BOOT_DRVINFO(spl_test) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600274\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700275\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700276\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600277\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600278};
279
Simon Glass192f8132020-10-03 11:31:25 -0600280/* Node /spl-test2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300281static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass7f5e2262020-07-07 21:32:06 -0600282\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glass9d2eb922017-06-18 22:09:06 -0600283\t.bytearray\t\t= {0x1, 0x23, 0x34},
284\t.byteval\t\t= 0x8,
Simon Glassc82de562019-05-17 22:00:32 -0600285\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600286\t.intval\t\t\t= 0x3,
Simon Glass8034e4d2020-10-03 11:31:27 -0600287\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
Simon Glass131e0b02017-08-29 14:15:49 -0600288\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600289\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glassc82de562019-05-17 22:00:32 -0600290\t.stringval\t\t= "message2",
Simon Glass9d2eb922017-06-18 22:09:06 -0600291};
Simon Glass1d8364a2020-12-28 20:34:54 -0700292U_BOOT_DRVINFO(spl_test2) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600293\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700294\t.plat\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700295\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600296\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600297};
298
Simon Glass192f8132020-10-03 11:31:25 -0600299/* Node /spl-test3 index 4 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300300static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glass8034e4d2020-10-03 11:31:27 -0600301\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
302\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600303\t.stringarray\t\t= {"one", "", ""},
304};
Simon Glass1d8364a2020-12-28 20:34:54 -0700305U_BOOT_DRVINFO(spl_test3) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600306\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700307\t.plat\t= &dtv_spl_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700308\t.plat_size\t= sizeof(dtv_spl_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600309\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600310};
311
Simon Glass2500de22020-12-28 20:35:05 -0700312'''
Simon Glass07ee48e2020-12-28 20:34:49 -0700313
314 def test_simple(self):
315 """Test output from some simple nodes with various types of data"""
316 dtb_file = get_dtb_file('dtoc_test_simple.dts')
317 output = tools.GetOutputFilename('output')
318 self.run_test(['struct'], dtb_file, output)
319 with open(output) as infile:
320 data = infile.read()
321
322 self._check_strings(self.struct_text, data)
323
324 self.run_test(['platdata'], dtb_file, output)
325 with open(output) as infile:
326 data = infile.read()
327
328 self._check_strings(self.platdata_text, data)
Simon Glass9d2eb922017-06-18 22:09:06 -0600329
Simon Glass4e8e8462020-12-28 20:34:52 -0700330 # Try the 'all' command
331 self.run_test(['all'], dtb_file, output)
332 data = tools.ReadFile(output, binary=False)
333 self._check_strings(self.platdata_text + self.struct_text, data)
334
Walter Lozanoe675d962020-07-03 08:07:17 -0300335 def test_driver_alias(self):
336 """Test output from a device tree file with a driver alias"""
337 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
338 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300339 self.run_test(['struct'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300340 with open(output) as infile:
341 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700342 self._check_strings(HEADER + '''
Walter Lozanoe675d962020-07-03 08:07:17 -0300343struct dtd_sandbox_gpio {
344\tconst char *\tgpio_bank_name;
345\tbool\t\tgpio_controller;
346\tfdt32_t\t\tsandbox_gpio_count;
347};
Walter Lozanoe675d962020-07-03 08:07:17 -0300348''', data)
349
Walter Lozanoa324e412020-06-25 01:10:08 -0300350 self.run_test(['platdata'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300351 with open(output) as infile:
352 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700353 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600354/* Node /gpios@0 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300355static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300356\t.gpio_bank_name\t\t= "a",
357\t.gpio_controller\t= true,
358\t.sandbox_gpio_count\t= 0x14,
359};
Simon Glass1d8364a2020-12-28 20:34:54 -0700360U_BOOT_DRVINFO(gpios_at_0) = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300361\t.name\t\t= "sandbox_gpio",
Simon Glass71fa5b42020-12-03 16:55:18 -0700362\t.plat\t= &dtv_gpios_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700363\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600364\t.parent_idx\t= -1,
Walter Lozanoe675d962020-07-03 08:07:17 -0300365};
366
367''', data)
368
Walter Lozanoa324e412020-06-25 01:10:08 -0300369 def test_invalid_driver(self):
370 """Test output from a device tree file with an invalid driver"""
371 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
372 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700373 with test_util.capture_sys_output() as _:
Simon Glass3809ad92021-02-03 06:01:12 -0700374 dtb_platdata.run_steps(
375 ['struct'], dtb_file, False, output, [], None, False,
376 scan=copy_scan())
Walter Lozanoa324e412020-06-25 01:10:08 -0300377 with open(output) as infile:
378 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700379 self._check_strings(HEADER + '''
Walter Lozanoa324e412020-06-25 01:10:08 -0300380struct dtd_invalid {
381};
382''', data)
383
Simon Glassa60cabd2020-12-28 20:34:47 -0700384 with test_util.capture_sys_output() as _:
Simon Glass3809ad92021-02-03 06:01:12 -0700385 dtb_platdata.run_steps(
386 ['platdata'], dtb_file, False, output, [], None, False,
387 scan=copy_scan())
Walter Lozanoa324e412020-06-25 01:10:08 -0300388 with open(output) as infile:
389 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700390 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600391/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300392static struct dtd_invalid dtv_spl_test = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300393};
Simon Glass1d8364a2020-12-28 20:34:54 -0700394U_BOOT_DRVINFO(spl_test) = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300395\t.name\t\t= "invalid",
Simon Glass71fa5b42020-12-03 16:55:18 -0700396\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700397\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600398\t.parent_idx\t= -1,
Walter Lozanoa324e412020-06-25 01:10:08 -0300399};
400
401''', data)
402
Simon Glass9d2eb922017-06-18 22:09:06 -0600403 def test_phandle(self):
404 """Test output from a node containing a phandle reference"""
405 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
406 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300407 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600408 with open(output) as infile:
409 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700410 self._check_strings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600411struct dtd_source {
Simon Glass3deeb472017-08-29 14:15:59 -0600412\tstruct phandle_2_arg clocks[4];
Simon Glass9d2eb922017-06-18 22:09:06 -0600413};
414struct dtd_target {
415\tfdt32_t\t\tintval;
416};
417''', data)
418
Walter Lozanoa324e412020-06-25 01:10:08 -0300419 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600420 with open(output) as infile:
421 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700422 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600423/* Node /phandle2-target index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300424static struct dtd_target dtv_phandle2_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600425\t.intval\t\t\t= 0x1,
426};
Simon Glass1d8364a2020-12-28 20:34:54 -0700427U_BOOT_DRVINFO(phandle2_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600428\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700429\t.plat\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700430\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600431\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600432};
433
Simon Glass192f8132020-10-03 11:31:25 -0600434/* Node /phandle3-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300435static struct dtd_target dtv_phandle3_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600436\t.intval\t\t\t= 0x2,
437};
Simon Glass1d8364a2020-12-28 20:34:54 -0700438U_BOOT_DRVINFO(phandle3_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600439\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700440\t.plat\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700441\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600442\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600443};
444
Simon Glass192f8132020-10-03 11:31:25 -0600445/* Node /phandle-source index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300446static struct dtd_source dtv_phandle_source = {
Simon Glassd0cd0752017-08-29 14:15:57 -0600447\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600448\t\t\t{4, {}},
449\t\t\t{0, {11}},
450\t\t\t{1, {12, 13}},
451\t\t\t{4, {}},},
Simon Glass9d2eb922017-06-18 22:09:06 -0600452};
Simon Glass1d8364a2020-12-28 20:34:54 -0700453U_BOOT_DRVINFO(phandle_source) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600454\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700455\t.plat\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700456\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600457\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600458};
459
Simon Glass192f8132020-10-03 11:31:25 -0600460/* Node /phandle-source2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300461static struct dtd_source dtv_phandle_source2 = {
Simon Glass609e2b12018-07-06 10:27:31 -0600462\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600463\t\t\t{4, {}},},
Simon Glass609e2b12018-07-06 10:27:31 -0600464};
Simon Glass1d8364a2020-12-28 20:34:54 -0700465U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass609e2b12018-07-06 10:27:31 -0600466\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700467\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700468\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600469\t.parent_idx\t= -1,
Simon Glass609e2b12018-07-06 10:27:31 -0600470};
471
Simon Glass16382ce2020-12-28 20:35:04 -0700472/* Node /phandle-target index 4 */
473static struct dtd_target dtv_phandle_target = {
474\t.intval\t\t\t= 0x0,
475};
476U_BOOT_DRVINFO(phandle_target) = {
477\t.name\t\t= "target",
478\t.plat\t= &dtv_phandle_target,
479\t.plat_size\t= sizeof(dtv_phandle_target),
480\t.parent_idx\t= -1,
481};
482
Simon Glass9d2eb922017-06-18 22:09:06 -0600483''', data)
484
Simon Glass961c1ce2018-07-06 10:27:35 -0600485 def test_phandle_single(self):
486 """Test output from a node containing a phandle reference"""
487 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
488 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300489 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600490 with open(output) as infile:
491 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700492 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -0600493struct dtd_source {
494\tstruct phandle_0_arg clocks[1];
495};
496struct dtd_target {
497\tfdt32_t\t\tintval;
498};
499''', data)
500
501 def test_phandle_reorder(self):
502 """Test that phandle targets are generated before their references"""
503 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
504 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300505 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600506 with open(output) as infile:
507 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700508 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600509/* Node /phandle-source2 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300510static struct dtd_source dtv_phandle_source2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600511\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600512\t\t\t{1, {}},},
Simon Glass961c1ce2018-07-06 10:27:35 -0600513};
Simon Glass1d8364a2020-12-28 20:34:54 -0700514U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600515\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700516\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700517\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600518\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600519};
520
Simon Glass16382ce2020-12-28 20:35:04 -0700521/* Node /phandle-target index 1 */
522static struct dtd_target dtv_phandle_target = {
523};
524U_BOOT_DRVINFO(phandle_target) = {
525\t.name\t\t= "target",
526\t.plat\t= &dtv_phandle_target,
527\t.plat_size\t= sizeof(dtv_phandle_target),
528\t.parent_idx\t= -1,
529};
530
Simon Glass961c1ce2018-07-06 10:27:35 -0600531''', data)
532
Walter Lozano5541fc02020-06-25 01:10:17 -0300533 def test_phandle_cd_gpio(self):
534 """Test that phandle targets are generated when unsing cd-gpios"""
535 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
536 output = tools.GetOutputFilename('output')
Simon Glass3809ad92021-02-03 06:01:12 -0700537 dtb_platdata.run_steps(
538 ['platdata'], dtb_file, False, output, [], None, False,
539 warning_disabled=True, scan=copy_scan())
Walter Lozano5541fc02020-06-25 01:10:17 -0300540 with open(output) as infile:
541 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700542 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600543/* Node /phandle2-target index 0 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300544static struct dtd_target dtv_phandle2_target = {
545\t.intval\t\t\t= 0x1,
546};
Simon Glass1d8364a2020-12-28 20:34:54 -0700547U_BOOT_DRVINFO(phandle2_target) = {
Walter Lozano5541fc02020-06-25 01:10:17 -0300548\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700549\t.plat\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700550\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600551\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300552};
553
Simon Glass192f8132020-10-03 11:31:25 -0600554/* Node /phandle3-target index 1 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300555static struct dtd_target dtv_phandle3_target = {
556\t.intval\t\t\t= 0x2,
557};
Simon Glass1d8364a2020-12-28 20:34:54 -0700558U_BOOT_DRVINFO(phandle3_target) = {
Walter Lozano5541fc02020-06-25 01:10:17 -0300559\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700560\t.plat\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700561\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600562\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300563};
564
Simon Glass192f8132020-10-03 11:31:25 -0600565/* Node /phandle-source index 2 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300566static struct dtd_source dtv_phandle_source = {
567\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600568\t\t\t{4, {}},
569\t\t\t{0, {11}},
570\t\t\t{1, {12, 13}},
571\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300572};
Simon Glass1d8364a2020-12-28 20:34:54 -0700573U_BOOT_DRVINFO(phandle_source) = {
Walter Lozano5541fc02020-06-25 01:10:17 -0300574\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700575\t.plat\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700576\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600577\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300578};
579
Simon Glass192f8132020-10-03 11:31:25 -0600580/* Node /phandle-source2 index 3 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300581static struct dtd_source dtv_phandle_source2 = {
582\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600583\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300584};
Simon Glass1d8364a2020-12-28 20:34:54 -0700585U_BOOT_DRVINFO(phandle_source2) = {
Walter Lozano5541fc02020-06-25 01:10:17 -0300586\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700587\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700588\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600589\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300590};
591
Simon Glass16382ce2020-12-28 20:35:04 -0700592/* Node /phandle-target index 4 */
593static struct dtd_target dtv_phandle_target = {
594\t.intval\t\t\t= 0x0,
595};
596U_BOOT_DRVINFO(phandle_target) = {
597\t.name\t\t= "target",
598\t.plat\t= &dtv_phandle_target,
599\t.plat_size\t= sizeof(dtv_phandle_target),
600\t.parent_idx\t= -1,
601};
602
Walter Lozano5541fc02020-06-25 01:10:17 -0300603''', data)
604
Simon Glass961c1ce2018-07-06 10:27:35 -0600605 def test_phandle_bad(self):
606 """Test a node containing an invalid phandle fails"""
Simon Glass04150022018-10-01 21:12:43 -0600607 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
608 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600609 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700610 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300611 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600612 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
Simon Glassa60cabd2020-12-28 20:34:47 -0700613 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600614
615 def test_phandle_bad2(self):
616 """Test a phandle target missing its #*-cells property"""
Simon Glass04150022018-10-01 21:12:43 -0600617 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
618 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600619 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700620 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300621 self.run_test(['struct'], dtb_file, output)
Walter Lozano179f0b62020-06-25 01:10:16 -0300622 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glassa60cabd2020-12-28 20:34:47 -0700623 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600624
Simon Glass1b1fe412017-08-29 14:15:50 -0600625 def test_addresses64(self):
626 """Test output from a node with a 'reg' property with na=2, ns=2"""
627 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
628 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300629 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600630 with open(output) as infile:
631 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700632 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600633struct dtd_test1 {
634\tfdt64_t\t\treg[2];
635};
636struct dtd_test2 {
637\tfdt64_t\t\treg[2];
638};
639struct dtd_test3 {
640\tfdt64_t\t\treg[4];
641};
642''', data)
643
Walter Lozanoa324e412020-06-25 01:10:08 -0300644 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600645 with open(output) as infile:
646 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700647 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600648/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300649static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600650\t.reg\t\t\t= {0x1234, 0x5678},
651};
Simon Glass1d8364a2020-12-28 20:34:54 -0700652U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600653\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700654\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700655\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600656\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600657};
658
Simon Glass192f8132020-10-03 11:31:25 -0600659/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300660static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600661\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
662};
Simon Glass1d8364a2020-12-28 20:34:54 -0700663U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600664\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700665\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700666\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600667\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600668};
669
Simon Glass192f8132020-10-03 11:31:25 -0600670/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300671static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600672\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
673};
Simon Glass1d8364a2020-12-28 20:34:54 -0700674U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600675\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700676\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700677\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600678\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600679};
680
Simon Glass2500de22020-12-28 20:35:05 -0700681''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600682
683 def test_addresses32(self):
684 """Test output from a node with a 'reg' property with na=1, ns=1"""
685 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
686 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300687 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600688 with open(output) as infile:
689 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700690 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600691struct dtd_test1 {
692\tfdt32_t\t\treg[2];
693};
694struct dtd_test2 {
695\tfdt32_t\t\treg[4];
696};
697''', data)
698
Walter Lozanoa324e412020-06-25 01:10:08 -0300699 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600700 with open(output) as infile:
701 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700702 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600703/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300704static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600705\t.reg\t\t\t= {0x1234, 0x5678},
706};
Simon Glass1d8364a2020-12-28 20:34:54 -0700707U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600708\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700709\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700710\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600711\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600712};
713
Simon Glass192f8132020-10-03 11:31:25 -0600714/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300715static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600716\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
717};
Simon Glass1d8364a2020-12-28 20:34:54 -0700718U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600719\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700720\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700721\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600722\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600723};
724
Simon Glass2500de22020-12-28 20:35:05 -0700725''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600726
727 def test_addresses64_32(self):
728 """Test output from a node with a 'reg' property with na=2, ns=1"""
729 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
730 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300731 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600732 with open(output) as infile:
733 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700734 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600735struct dtd_test1 {
736\tfdt64_t\t\treg[2];
737};
738struct dtd_test2 {
739\tfdt64_t\t\treg[2];
740};
741struct dtd_test3 {
742\tfdt64_t\t\treg[4];
743};
744''', data)
745
Walter Lozanoa324e412020-06-25 01:10:08 -0300746 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600747 with open(output) as infile:
748 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700749 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600750/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300751static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600752\t.reg\t\t\t= {0x123400000000, 0x5678},
753};
Simon Glass1d8364a2020-12-28 20:34:54 -0700754U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600755\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700756\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700757\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600758\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600759};
760
Simon Glass192f8132020-10-03 11:31:25 -0600761/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300762static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600763\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
764};
Simon Glass1d8364a2020-12-28 20:34:54 -0700765U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600766\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700767\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700768\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600769\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600770};
771
Simon Glass192f8132020-10-03 11:31:25 -0600772/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300773static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600774\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
775};
Simon Glass1d8364a2020-12-28 20:34:54 -0700776U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600777\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700778\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700779\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600780\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600781};
782
Simon Glass2500de22020-12-28 20:35:05 -0700783''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600784
785 def test_addresses32_64(self):
786 """Test output from a node with a 'reg' property with na=1, ns=2"""
787 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
788 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300789 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600790 with open(output) as infile:
791 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700792 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600793struct dtd_test1 {
794\tfdt64_t\t\treg[2];
795};
796struct dtd_test2 {
797\tfdt64_t\t\treg[2];
798};
799struct dtd_test3 {
800\tfdt64_t\t\treg[4];
801};
802''', data)
803
Walter Lozanoa324e412020-06-25 01:10:08 -0300804 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600805 with open(output) as infile:
806 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700807 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600808/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300809static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600810\t.reg\t\t\t= {0x1234, 0x567800000000},
811};
Simon Glass1d8364a2020-12-28 20:34:54 -0700812U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600813\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700814\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700815\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600816\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600817};
818
Simon Glass192f8132020-10-03 11:31:25 -0600819/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300820static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600821\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
822};
Simon Glass1d8364a2020-12-28 20:34:54 -0700823U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600824\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700825\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700826\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600827\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600828};
829
Simon Glass192f8132020-10-03 11:31:25 -0600830/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300831static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600832\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
833};
Simon Glass1d8364a2020-12-28 20:34:54 -0700834U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600835\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700836\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700837\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600838\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600839};
840
Simon Glass2500de22020-12-28 20:35:05 -0700841''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600842
843 def test_bad_reg(self):
844 """Test that a reg property with an invalid type generates an error"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600845 # Capture stderr since dtc will emit warnings for this file
846 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600847 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700848 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300849 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600850 self.assertIn("Node 'spl-test' reg property is not an int",
Simon Glassa60cabd2020-12-28 20:34:47 -0700851 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600852
853 def test_bad_reg2(self):
854 """Test that a reg property with an invalid cell count is detected"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600855 # Capture stderr since dtc will emit warnings for this file
856 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600857 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700858 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300859 self.run_test(['struct'], dtb_file, output)
Simon Glassa60cabd2020-12-28 20:34:47 -0700860 self.assertIn(
861 "Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
862 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600863
864 def test_add_prop(self):
865 """Test that a subequent node can add a new property to a struct"""
866 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
867 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300868 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600869 with open(output) as infile:
870 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700871 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -0600872struct dtd_sandbox_spl_test {
873\tfdt32_t\t\tintarray;
874\tfdt32_t\t\tintval;
875};
876''', data)
877
Walter Lozanoa324e412020-06-25 01:10:08 -0300878 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600879 with open(output) as infile:
880 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700881 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600882/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300883static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600884\t.intval\t\t\t= 0x1,
885};
Simon Glass1d8364a2020-12-28 20:34:54 -0700886U_BOOT_DRVINFO(spl_test) = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600887\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700888\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700889\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600890\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600891};
892
Simon Glass192f8132020-10-03 11:31:25 -0600893/* Node /spl-test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300894static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600895\t.intarray\t\t= 0x5,
896};
Simon Glass1d8364a2020-12-28 20:34:54 -0700897U_BOOT_DRVINFO(spl_test2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600898\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700899\t.plat\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700900\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600901\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600902};
903
Simon Glass2500de22020-12-28 20:35:05 -0700904''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600905
Simon Glassa60cabd2020-12-28 20:34:47 -0700906 def test_stdout(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600907 """Test output to stdout"""
908 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass07ee48e2020-12-28 20:34:49 -0700909 with test_util.capture_sys_output() as (stdout, _):
Simon Glass6ca0c7a2020-12-28 20:34:48 -0700910 self.run_test(['struct'], dtb_file, None)
Simon Glass07ee48e2020-12-28 20:34:49 -0700911 self._check_strings(self.struct_text, stdout.getvalue())
Simon Glass961c1ce2018-07-06 10:27:35 -0600912
Simon Glassc3a310a82020-12-28 20:34:51 -0700913 def test_multi_to_file(self):
914 """Test output of multiple pieces to a single file"""
915 dtb_file = get_dtb_file('dtoc_test_simple.dts')
916 output = tools.GetOutputFilename('output')
Simon Glass4e8e8462020-12-28 20:34:52 -0700917 self.run_test(['all'], dtb_file, output)
Simon Glassc3a310a82020-12-28 20:34:51 -0700918 data = tools.ReadFile(output, binary=False)
Simon Glass4e8e8462020-12-28 20:34:52 -0700919 self._check_strings(self.platdata_text + self.struct_text, data)
Simon Glassc3a310a82020-12-28 20:34:51 -0700920
Simon Glassa60cabd2020-12-28 20:34:47 -0700921 def test_no_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600922 """Test running dtoc without a command"""
Simon Glassa60cabd2020-12-28 20:34:47 -0700923 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300924 self.run_test([], '', '')
Simon Glass961c1ce2018-07-06 10:27:35 -0600925 self.assertIn("Please specify a command: struct, platdata",
Simon Glassa60cabd2020-12-28 20:34:47 -0700926 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600927
Simon Glassa60cabd2020-12-28 20:34:47 -0700928 def test_bad_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600929 """Test running dtoc with an invalid command"""
930 dtb_file = get_dtb_file('dtoc_test_simple.dts')
931 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700932 with self.assertRaises(ValueError) as exc:
Simon Glass3809ad92021-02-03 06:01:12 -0700933 self.run_test(['invalid-cmd'], dtb_file, output, False)
Simon Glass4e8e8462020-12-28 20:34:52 -0700934 self.assertIn("Unknown command 'invalid-cmd': (use: platdata, struct)",
Simon Glassa60cabd2020-12-28 20:34:47 -0700935 str(exc.exception))
Walter Lozanod82062b2020-07-28 19:06:23 -0300936
Simon Glass4e8e8462020-12-28 20:34:52 -0700937 def test_output_conflict(self):
938 """Test a conflict between and output dirs and output file"""
939 with self.assertRaises(ValueError) as exc:
Simon Glass3809ad92021-02-03 06:01:12 -0700940 dtb_platdata.run_steps(
941 ['all'], None, False, 'out', ['cdir'], None, False,
942 warning_disabled=True, scan=copy_scan())
Simon Glass4e8e8462020-12-28 20:34:52 -0700943 self.assertIn("Must specify either output or output_dirs, not both",
944 str(exc.exception))
945
946 def test_output_dirs(self):
947 """Test outputting files to a directory"""
948 # Remove the directory so that files from other tests are not there
949 tools._RemoveOutputDir()
950 tools.PrepareOutputDir(None)
951
952 # This should create the .dts and .dtb in the output directory
953 dtb_file = get_dtb_file('dtoc_test_simple.dts')
954 outdir = tools.GetOutputDir()
955 fnames = glob.glob(outdir + '/*')
956 self.assertEqual(2, len(fnames))
957
Simon Glass3809ad92021-02-03 06:01:12 -0700958 dtb_platdata.run_steps(
959 ['all'], dtb_file, False, None, [outdir], None, False,
960 warning_disabled=True, scan=copy_scan())
Simon Glass4e8e8462020-12-28 20:34:52 -0700961 fnames = glob.glob(outdir + '/*')
962 self.assertEqual(4, len(fnames))
963
964 leafs = set(os.path.basename(fname) for fname in fnames)
965 self.assertEqual(
Simon Glassbeddd7a2020-12-28 20:35:01 -0700966 {'dt-structs-gen.h', 'source.dts', 'dt-plat.c', 'source.dtb'},
Simon Glass4e8e8462020-12-28 20:34:52 -0700967 leafs)
Simon Glass047a4802021-02-03 06:01:00 -0700968
969 def setup_process_test(self):
970 """Set up a test of process_nodes()
971
972 This uses saved_scan but returns a deep copy of it, so it is safe to
973 modify it in these tests
974
975 Returns:
976 tuple:
977 DtbPlatdata: object to test
978 Scanner: scanner to use
979 """
980 dtb_file = get_dtb_file('dtoc_test_simple.dts')
981 output = tools.GetOutputFilename('output')
982
983 # Take a copy before messing with it
Simon Glassc14fd0c2021-02-03 06:01:11 -0700984 scan = copy_scan()
Simon Glass047a4802021-02-03 06:01:00 -0700985 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
986 plat.scan_dtb()
Simon Glassc14fd0c2021-02-03 06:01:11 -0700987 plat.scan_tree(False)
Simon Glass047a4802021-02-03 06:01:00 -0700988 plat.prepare_nodes()
989 return plat, scan
990
991 def test_process_nodes(self):
992 """Test processing nodes to add various info"""
993 plat, scan = self.setup_process_test()
994 plat.process_nodes(True)
995
996 i2c_node = plat._fdt.GetNode('/i2c@0')
997 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
998 pmic = scan._drivers['sandbox_pmic']
999 i2c = scan._drivers['sandbox_i2c']
1000 self.assertEqual('DM_DEVICE_REF(pmic_at_9)', pmic_node.dev_ref)
1001 self.assertEqual(pmic, pmic_node.driver)
1002 self.assertEqual(i2c_node, pmic_node.parent)
1003 self.assertEqual(i2c, pmic_node.parent_driver)
1004
1005 # The pmic is the only child
1006 self.assertEqual(pmic_node.parent_seq, 0)
1007 self.assertEqual([pmic_node], i2c_node.child_devs)
1008
1009 # Start and end of the list should be the child_head
1010 ref = '&DM_DEVICE_REF(i2c_at_0)->child_head'
1011 self.assertEqual(
1012 {-1: ref, 0: '&DM_DEVICE_REF(pmic_at_9)->sibling_node', 1: ref},
1013 i2c_node.child_refs)
1014
1015 def test_process_nodes_bad_parent(self):
1016 # Pretend that i2c has a parent (the pmic) and delete that driver
1017 plat, scan = self.setup_process_test()
1018
1019 i2c_node = plat._fdt.GetNode('/i2c@0')
1020 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
1021 del scan._drivers['sandbox_pmic']
1022 i2c_node.parent = pmic_node
1023
1024 # Process twice, the second time to generate an exception
1025 plat.process_nodes(False)
1026 with self.assertRaises(ValueError) as exc:
1027 plat.process_nodes(True)
1028 self.assertIn(
1029 "Cannot parse/find parent driver 'sandbox_pmic' for 'sandbox_i2c",
1030 str(exc.exception))
1031
1032 def test_process_nodes_bad_node(self):
1033 plat, scan = self.setup_process_test()
1034
1035 # Now remove the pmic driver
1036 del scan._drivers['sandbox_pmic']
1037
1038 # Process twice, the second time to generate an exception
1039 plat.process_nodes(False)
1040 with self.assertRaises(ValueError) as exc:
1041 plat.process_nodes(True)
1042 self.assertIn("Cannot parse/find driver for 'sandbox_pmic",
1043 str(exc.exception))
Simon Glasseb3c2492021-02-03 06:01:01 -07001044
Simon Glass80d782c42021-02-03 06:01:10 -07001045 def test_process_nodes_bad_uclass(self):
1046 plat, scan = self.setup_process_test()
1047
1048 self.assertIn('UCLASS_I2C', scan._uclass)
1049 del scan._uclass['UCLASS_I2C']
1050 with self.assertRaises(ValueError) as exc:
1051 plat.process_nodes(True)
1052 self.assertIn("Cannot parse/find uclass 'UCLASS_I2C' for driver 'sandbox_i2c'",
1053 str(exc.exception))
1054
Simon Glasseb3c2492021-02-03 06:01:01 -07001055 def test_process_nodes_used(self):
1056 """Test processing nodes to add various info"""
1057 plat, scan = self.setup_process_test()
1058 plat.process_nodes(True)
1059
1060 pmic = scan._drivers['sandbox_pmic']
1061 self.assertTrue(pmic.used)
1062
1063 gpio = scan._drivers['sandbox_gpio']
1064 self.assertFalse(gpio.used)
Simon Glassbe88d2f2021-02-03 06:01:07 -07001065
1066 def test_alias_read(self):
1067 """Test obtaining aliases"""
1068 dtb_file = get_dtb_file('dtoc_test_inst.dts')
1069 output = tools.GetOutputFilename('output')
1070 plat = self.run_test(['struct'], dtb_file, output)
1071
1072 scan = plat._scan
1073 testfdt_node = plat._fdt.GetNode('/some-bus/test')
Simon Glass80d782c42021-02-03 06:01:10 -07001074 test0_node = plat._fdt.GetNode('/some-bus/test0')
Simon Glassbe88d2f2021-02-03 06:01:07 -07001075 self.assertIn('UCLASS_TEST_FDT', scan._uclass)
1076 uc = scan._uclass['UCLASS_TEST_FDT']
Simon Glass80d782c42021-02-03 06:01:10 -07001077 self.assertEqual({1: testfdt_node, 2: test0_node},
1078 uc.alias_num_to_node)
1079 self.assertEqual({'/some-bus/test': 1, '/some-bus/test0': 2},
1080 uc.alias_path_to_num)
Simon Glassbe88d2f2021-02-03 06:01:07 -07001081
1082 # Try adding an alias that doesn't exist
1083 self.assertFalse(scan.add_uclass_alias('fred', 3, None))
1084
1085 # Try adding an alias for a missing node
1086 self.assertIsNone(scan.add_uclass_alias('testfdt', 3, None))
1087
1088 def test_alias_read_bad(self):
1089 """Test invalid alias property name"""
1090 dtb_file = get_dtb_file('dtoc_test_alias_bad.dts')
1091 output = tools.GetOutputFilename('output')
1092 with self.assertRaises(ValueError) as exc:
1093 plat = self.run_test(['struct'], dtb_file, output)
1094 self.assertIn("Cannot decode alias 'i2c4-'", str(exc.exception))
1095
1096 def test_alias_read_bad_path(self):
1097 """Test alias pointing to a non-existent node"""
1098 # This line may produce a warning, so capture it:
1099 # Warning (alias_paths): /aliases:i2c4: aliases property is not a valid
1100 # node (/does/not/exist)
1101 dtb_file = get_dtb_file('dtoc_test_alias_bad_path.dts', True)
1102
1103 output = tools.GetOutputFilename('output')
1104 with self.assertRaises(ValueError) as exc:
1105 plat = self.run_test(['struct'], dtb_file, output)
1106 self.assertIn("Alias 'i2c4' path '/does/not/exist' not found",
1107 str(exc.exception))
1108
1109 def test_alias_read_bad_uclass(self):
1110 """Test alias for a uclass that doesn't exist"""
1111 dtb_file = get_dtb_file('dtoc_test_alias_bad_uc.dts')
1112 output = tools.GetOutputFilename('output')
1113 with test_util.capture_sys_output() as (stdout, _):
1114 plat = self.run_test(['struct'], dtb_file, output)
1115 self.assertEqual("Could not find uclass for alias 'other1'",
1116 stdout.getvalue().strip())
Simon Glassdf56e0b2021-02-03 06:01:09 -07001117
1118 def test_sequence(self):
1119 """Test assignment of sequence numnbers"""
1120 dtb_file = get_dtb_file('dtoc_test_inst.dts')
1121 output = tools.GetOutputFilename('output')
1122 plat = self.run_test(['struct'], dtb_file, output)
Simon Glass80d782c42021-02-03 06:01:10 -07001123
1124 scan = plat._scan
1125 testfdt = plat._fdt.GetNode('/some-bus/test')
1126 self.assertEqual(1, testfdt.seq)
1127 i2c = plat._fdt.GetNode('/i2c')
1128
1129 # For now this uclass is not compiled in, so no sequence is assigned
1130 self.assertEqual(4, i2c.seq)
1131 spl = plat._fdt.GetNode('/spl-test')
1132 self.assertEqual(0, spl.seq)
Simon Glassc14fd0c2021-02-03 06:01:11 -07001133
1134 def test_process_root(self):
1135 """Test assignment of sequence numnbers"""
1136 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1137 output = tools.GetOutputFilename('output')
1138
1139 # Take a copy before messing with it
1140 scan = copy_scan()
1141 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
1142 plat.scan_dtb()
1143 root = plat._fdt.GetRoot()
1144
1145 plat.scan_tree(False)
1146 self.assertNotIn(root, plat._valid_nodes)
1147
1148 plat.scan_tree(True)
1149 self.assertIn(root, plat._valid_nodes)
1150 self.assertEqual('root_driver',
1151 scan.get_normalized_compat_name(root)[0])