blob: 597c93e8a8705c51cfd47991eb13cb6df64b1450 [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
Simon Glassbd310df2023-02-23 18:18:16 -070016import pathlib
Simon Glass9d2eb922017-06-18 22:09:06 -060017import struct
18import unittest
19
Simon Glassa60cabd2020-12-28 20:34:47 -070020from dtoc import dtb_platdata
Simon Glassa997ea52020-04-17 18:09:04 -060021from dtoc import fdt
22from dtoc import fdt_util
Simon Glass768ff0a2021-02-03 06:00:51 -070023from dtoc import src_scan
Simon Glass2b160072022-02-11 13:23:20 -070024from dtoc.dtb_platdata import Ftype
25from dtoc.dtb_platdata import get_value
26from dtoc.dtb_platdata import tab_to
Simon Glass9065bc92020-12-28 20:35:06 -070027from dtoc.src_scan import conv_name_to_c
28from dtoc.src_scan import get_compat_name
Simon Glass131444f2023-02-23 18:18:04 -070029from u_boot_pylib import test_util
30from u_boot_pylib import tools
Simon Glass9d2eb922017-06-18 22:09:06 -060031
Simon Glassbd310df2023-02-23 18:18:16 -070032DTOC_DIR = pathlib.Path(__file__).parent
33TEST_DATA_DIR = DTOC_DIR / 'test/'
Simon Glass9d2eb922017-06-18 22:09:06 -060034
35
Simon Glasseb37e2d2017-11-12 21:52:17 -070036HEADER = '''/*
37 * DO NOT MODIFY
38 *
Simon Glass6b208842020-12-28 20:35:00 -070039 * Defines the structs used to hold devicetree data.
40 * This was generated by dtoc from a .dtb (device tree binary) file.
Simon Glasseb37e2d2017-11-12 21:52:17 -070041 */
42
43#include <stdbool.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090044#include <linux/libfdt.h>'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070045
Simon Glass3fa3bbb2021-02-03 06:01:14 -070046DECL_HEADER = '''/*
47 * DO NOT MODIFY
48 *
49 * Declares externs for all device/uclass instances.
50 * This was generated by dtoc from a .dtb (device tree binary) file.
51 */
52'''
53
Simon Glassbe749002021-02-03 06:01:15 -070054C_HEADER_PRE = '''/*
Simon Glasseb37e2d2017-11-12 21:52:17 -070055 * DO NOT MODIFY
56 *
Simon Glass6b208842020-12-28 20:35:00 -070057 * Declares the U_BOOT_DRIVER() records and platform data.
58 * This was generated by dtoc from a .dtb (device tree binary) file.
Simon Glasseb37e2d2017-11-12 21:52:17 -070059 */
Simon Glassbe749002021-02-03 06:01:15 -070060'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070061
Simon Glassbe749002021-02-03 06:01:15 -070062C_HEADER = C_HEADER_PRE + '''
Simon Glass1d8364a2020-12-28 20:34:54 -070063/* Allow use of U_BOOT_DRVINFO() in this file */
Simon Glassbeddd7a2020-12-28 20:35:01 -070064#define DT_PLAT_C
Simon Glass4c73d7b2020-10-03 11:31:41 -060065
Simon Glasseb37e2d2017-11-12 21:52:17 -070066#include <common.h>
67#include <dm.h>
68#include <dt-structs.h>
69'''
70
Simon Glassc7b4b832021-02-03 06:01:20 -070071UCLASS_HEADER_COMMON = '''/*
72 * DO NOT MODIFY
73 *
74 * Declares the uclass instances (struct uclass).
75 * This was generated by dtoc from a .dtb (device tree binary) file.
76 */
77'''
78
Simon Glass768ff0a2021-02-03 06:00:51 -070079# Scanner saved from a previous run of the tests (to speed things up)
80saved_scan = None
81
Simon Glassa60cabd2020-12-28 20:34:47 -070082# This is a test so is allowed to access private things in the module it is
83# testing
84# pylint: disable=W0212
Simon Glass3bce93d2018-07-06 10:27:37 -060085
86def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glass9d2eb922017-06-18 22:09:06 -060087 """Compile a .dts file to a .dtb
88
89 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -070090 dts_fname (str): Filename of .dts file in the current directory
91 capture_stderr (bool): True to capture and discard stderr output
Simon Glass9d2eb922017-06-18 22:09:06 -060092
93 Returns:
Simon Glassa60cabd2020-12-28 20:34:47 -070094 str: Filename of compiled file in output directory
Simon Glass9d2eb922017-06-18 22:09:06 -060095 """
Simon Glassbd310df2023-02-23 18:18:16 -070096 return fdt_util.EnsureCompiled(str(TEST_DATA_DIR / dts_fname),
Simon Glass3bce93d2018-07-06 10:27:37 -060097 capture_stderr=capture_stderr)
Simon Glass9d2eb922017-06-18 22:09:06 -060098
Simon Glass768ff0a2021-02-03 06:00:51 -070099
100def setup():
101 global saved_scan
102
103 # Disable warnings so that calls to get_normalized_compat_name() will not
104 # output things.
Simon Glass695077d2021-03-26 16:17:25 +1300105 saved_scan = src_scan.Scanner(None, False)
Simon Glass768ff0a2021-02-03 06:00:51 -0700106 saved_scan.scan_drivers()
107
108def copy_scan():
109 """Get a copy of saved_scan so that each test can start clean"""
110 return copy.deepcopy(saved_scan)
111
Simon Glass9d2eb922017-06-18 22:09:06 -0600112
113class TestDtoc(unittest.TestCase):
114 """Tests for dtoc"""
115 @classmethod
116 def setUpClass(cls):
Simon Glass80025522022-01-29 14:14:04 -0700117 tools.prepare_output_dir(None)
Simon Glass7f5e2262020-07-07 21:32:06 -0600118 cls.maxDiff = None
Simon Glass9d2eb922017-06-18 22:09:06 -0600119
120 @classmethod
121 def tearDownClass(cls):
Simon Glass80025522022-01-29 14:14:04 -0700122 tools.finalise_output_dir()
Simon Glass9d2eb922017-06-18 22:09:06 -0600123
Simon Glassa60cabd2020-12-28 20:34:47 -0700124 @staticmethod
125 def _write_python_string(fname, data):
Simon Glassc47c2b32018-07-06 10:27:25 -0600126 """Write a string with tabs expanded as done in this Python file
127
128 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -0700129 fname (str): Filename to write to
130 data (str): Raw string to convert
Simon Glassc47c2b32018-07-06 10:27:25 -0600131 """
132 data = data.replace('\t', '\\t')
Simon Glassa60cabd2020-12-28 20:34:47 -0700133 with open(fname, 'w') as fout:
134 fout.write(data)
Simon Glassc47c2b32018-07-06 10:27:25 -0600135
Simon Glassa60cabd2020-12-28 20:34:47 -0700136 def _check_strings(self, expected, actual):
Simon Glassc47c2b32018-07-06 10:27:25 -0600137 """Check that a string matches its expected value
138
139 If the strings do not match, they are written to the /tmp directory in
140 the same Python format as is used here in the test. This allows for
141 easy comparison and update of the tests.
142
143 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -0700144 expected (str): Expected string
145 actual (str): Actual string
Simon Glassc47c2b32018-07-06 10:27:25 -0600146 """
147 if expected != actual:
Simon Glassa60cabd2020-12-28 20:34:47 -0700148 self._write_python_string('/tmp/binman.expected', expected)
149 self._write_python_string('/tmp/binman.actual', actual)
Simon Glass61b88e52019-05-17 22:00:31 -0600150 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glassa60cabd2020-12-28 20:34:47 -0700151 self.assertEqual(expected, actual)
Simon Glassc47c2b32018-07-06 10:27:25 -0600152
Simon Glassa60cabd2020-12-28 20:34:47 -0700153 @staticmethod
Simon Glass3809ad92021-02-03 06:01:12 -0700154 def run_test(args, dtb_file, output, instantiate=False):
Simon Glassa60cabd2020-12-28 20:34:47 -0700155 """Run a test using dtoc
Walter Lozanoa324e412020-06-25 01:10:08 -0300156
Simon Glassa60cabd2020-12-28 20:34:47 -0700157 Args:
158 args (list of str): List of arguments for dtoc
159 dtb_file (str): Filename of .dtb file
160 output (str): Filename of output file
Simon Glassbe88d2f2021-02-03 06:01:07 -0700161
162 Returns:
163 DtbPlatdata object
Simon Glassa60cabd2020-12-28 20:34:47 -0700164 """
Simon Glass80d782c42021-02-03 06:01:10 -0700165 # Make a copy of the 'scan' object, since it includes uclasses and
166 # drivers, which get updated during execution.
Simon Glass3809ad92021-02-03 06:01:12 -0700167 return dtb_platdata.run_steps(
168 args, dtb_file, False, output, [], None, instantiate,
169 warning_disabled=True, scan=copy_scan())
Walter Lozanoa324e412020-06-25 01:10:08 -0300170
Simon Glass9d2eb922017-06-18 22:09:06 -0600171 def test_name(self):
172 """Test conversion of device tree names to C identifiers"""
173 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
174 self.assertEqual('vendor_clock_frequency',
175 conv_name_to_c('vendor,clock-frequency'))
176 self.assertEqual('rockchip_rk3399_sdhci_5_1',
177 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
178
179 def test_tab_to(self):
180 """Test operation of tab_to() function"""
181 self.assertEqual('fred ', tab_to(0, 'fred'))
182 self.assertEqual('fred\t', tab_to(1, 'fred'))
183 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
184 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
185 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
186 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
187
188 def test_get_value(self):
189 """Test operation of get_value() function"""
190 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700191 get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600192 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700193 get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600194 self.assertEqual('0x0',
Simon Glassc9a032c2020-11-08 20:36:17 -0700195 get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
196 self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
197 self.assertEqual('true', get_value(fdt.Type.BOOL, None))
Simon Glass9d2eb922017-06-18 22:09:06 -0600198
199 def test_get_compat_name(self):
200 """Test operation of get_compat_name() function"""
201 Prop = collections.namedtuple('Prop', ['value'])
202 Node = collections.namedtuple('Node', ['props'])
203
204 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
205 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300206 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600207 get_compat_name(node))
208
209 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
210 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300211 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600212 get_compat_name(node))
213
214 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
215 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300216 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
Simon Glassa60cabd2020-12-28 20:34:47 -0700217 'arasan_sdhci_5_1', 'third']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600218 get_compat_name(node))
219
220 def test_empty_file(self):
221 """Test output from a device tree file with no nodes"""
222 dtb_file = get_dtb_file('dtoc_test_empty.dts')
Simon Glass80025522022-01-29 14:14:04 -0700223 output = tools.get_output_filename('output')
Simon Glass768ff0a2021-02-03 06:00:51 -0700224
225 # Run this one without saved_scan to complete test coverage
Simon Glass3809ad92021-02-03 06:01:12 -0700226 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [], None,
227 False)
Simon Glass9d2eb922017-06-18 22:09:06 -0600228 with open(output) as infile:
229 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700230 self.assertEqual(HEADER.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600231
Walter Lozanoa324e412020-06-25 01:10:08 -0300232 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600233 with open(output) as infile:
234 lines = infile.read().splitlines()
Simon Glass2500de22020-12-28 20:35:05 -0700235 self.assertEqual(C_HEADER.splitlines() + [''], lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600236
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700237 decl_text = DECL_HEADER + '''
238#include <dm/device-internal.h>
239#include <dm/uclass-internal.h>
240
241/* driver declarations - these allow DM_DRIVER_GET() to be used */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300242extern U_BOOT_DRIVER(sandbox_i2c);
243extern U_BOOT_DRIVER(sandbox_pmic);
244extern U_BOOT_DRIVER(sandbox_spl_test);
245extern U_BOOT_DRIVER(sandbox_spl_test);
246extern U_BOOT_DRIVER(sandbox_spl_test);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700247
248/* uclass driver declarations - needed for DM_UCLASS_DRIVER_REF() */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300249extern UCLASS_DRIVER(i2c);
250extern UCLASS_DRIVER(misc);
251extern UCLASS_DRIVER(pmic);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700252'''
253 decl_text_inst = DECL_HEADER + '''
254#include <dm/device-internal.h>
255#include <dm/uclass-internal.h>
256
257/* driver declarations - these allow DM_DRIVER_GET() to be used */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300258extern U_BOOT_DRIVER(sandbox_i2c);
259extern U_BOOT_DRIVER(root_driver);
260extern U_BOOT_DRIVER(denx_u_boot_test_bus);
261extern U_BOOT_DRIVER(sandbox_spl_test);
262extern U_BOOT_DRIVER(sandbox_spl_test);
263extern U_BOOT_DRIVER(denx_u_boot_fdt_test);
264extern U_BOOT_DRIVER(denx_u_boot_fdt_test);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700265
266/* device declarations - these allow DM_DEVICE_REF() to be used */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300267extern DM_DEVICE_INST(i2c);
268extern DM_DEVICE_INST(root);
269extern DM_DEVICE_INST(some_bus);
270extern DM_DEVICE_INST(spl_test);
271extern DM_DEVICE_INST(spl_test3);
272extern DM_DEVICE_INST(test);
273extern DM_DEVICE_INST(test0);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700274
275/* uclass driver declarations - needed for DM_UCLASS_DRIVER_REF() */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300276extern UCLASS_DRIVER(i2c);
277extern UCLASS_DRIVER(misc);
278extern UCLASS_DRIVER(root);
279extern UCLASS_DRIVER(testbus);
280extern UCLASS_DRIVER(testfdt);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700281
282/* uclass declarations - needed for DM_UCLASS_REF() */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300283extern DM_UCLASS_INST(i2c);
284extern DM_UCLASS_INST(misc);
285extern DM_UCLASS_INST(root);
286extern DM_UCLASS_INST(testbus);
287extern DM_UCLASS_INST(testfdt);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700288'''
Simon Glass07ee48e2020-12-28 20:34:49 -0700289 struct_text = HEADER + '''
Simon Glass32a8d222021-02-03 06:00:57 -0700290struct dtd_sandbox_i2c {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600291};
Simon Glass32a8d222021-02-03 06:00:57 -0700292struct dtd_sandbox_pmic {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600293\tbool\t\tlow_power;
Simon Glass94ba59e2021-03-26 16:17:26 +1300294\tfdt32_t\t\treg[1];
Simon Glass90e5f0a2017-08-29 14:15:51 -0600295};
Simon Glass9d2eb922017-06-18 22:09:06 -0600296struct dtd_sandbox_spl_test {
Simon Glass7f5e2262020-07-07 21:32:06 -0600297\tconst char * acpi_name;
Simon Glass9d2eb922017-06-18 22:09:06 -0600298\tbool\t\tboolval;
299\tunsigned char\tbytearray[3];
300\tunsigned char\tbyteval;
Simon Glass3b55e3f2021-11-23 11:03:39 -0700301\tfdt32_t\t\tint64val[2];
Simon Glassa7d66982021-07-28 19:23:10 -0600302\tfdt32_t\t\tintarray[3];
Simon Glass9d2eb922017-06-18 22:09:06 -0600303\tfdt32_t\t\tintval;
304\tunsigned char\tlongbytearray[9];
Simon Glass43118322021-07-28 19:23:11 -0600305\tfdt32_t\t\tmaybe_empty_int[1];
Simon Glass9c526332018-07-06 10:27:28 -0600306\tunsigned char\tnotstring[5];
Simon Glass9d2eb922017-06-18 22:09:06 -0600307\tconst char *\tstringarray[3];
308\tconst char *\tstringval;
309};
Simon Glass07ee48e2020-12-28 20:34:49 -0700310'''
Simon Glass07ee48e2020-12-28 20:34:49 -0700311 platdata_text = C_HEADER + '''
Simon Glassbe749002021-02-03 06:01:15 -0700312/*
Simon Glasse2119082021-02-03 06:01:19 -0700313 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
314 *
315 * idx driver_info driver
316 * --- -------------------- --------------------
317 * 0: i2c_at_0 sandbox_i2c
318 * 1: pmic_at_9 sandbox_pmic
319 * 2: spl_test sandbox_spl_test
320 * 3: spl_test2 sandbox_spl_test
321 * 4: spl_test3 sandbox_spl_test
322 * --- -------------------- --------------------
323 */
324
325/*
Simon Glassbe749002021-02-03 06:01:15 -0700326 * Node /i2c@0 index 0
327 * driver sandbox_i2c parent None
328 */
Simon Glass32a8d222021-02-03 06:00:57 -0700329static struct dtd_sandbox_i2c dtv_i2c_at_0 = {
Simon Glass192f8132020-10-03 11:31:25 -0600330};
Simon Glass1d8364a2020-12-28 20:34:54 -0700331U_BOOT_DRVINFO(i2c_at_0) = {
Simon Glass32a8d222021-02-03 06:00:57 -0700332\t.name\t\t= "sandbox_i2c",
Simon Glasse2119082021-02-03 06:01:19 -0700333\t.plat\t\t= &dtv_i2c_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700334\t.plat_size\t= sizeof(dtv_i2c_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600335\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600336};
337
Simon Glassbe749002021-02-03 06:01:15 -0700338/*
339 * Node /i2c@0/pmic@9 index 1
340 * driver sandbox_pmic parent sandbox_i2c
341 */
Simon Glass32a8d222021-02-03 06:00:57 -0700342static struct dtd_sandbox_pmic dtv_pmic_at_9 = {
Simon Glass192f8132020-10-03 11:31:25 -0600343\t.low_power\t\t= true,
Simon Glass94ba59e2021-03-26 16:17:26 +1300344\t.reg\t\t\t= {0x9},
Simon Glass192f8132020-10-03 11:31:25 -0600345};
Simon Glass1d8364a2020-12-28 20:34:54 -0700346U_BOOT_DRVINFO(pmic_at_9) = {
Simon Glass32a8d222021-02-03 06:00:57 -0700347\t.name\t\t= "sandbox_pmic",
Simon Glasse2119082021-02-03 06:01:19 -0700348\t.plat\t\t= &dtv_pmic_at_9,
Simon Glass39edb952020-12-03 16:55:19 -0700349\t.plat_size\t= sizeof(dtv_pmic_at_9),
Simon Glass36b15e22020-10-03 11:31:35 -0600350\t.parent_idx\t= 0,
Simon Glass192f8132020-10-03 11:31:25 -0600351};
352
Simon Glassbe749002021-02-03 06:01:15 -0700353/*
354 * Node /spl-test index 2
355 * driver sandbox_spl_test parent None
356 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300357static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glassc82de562019-05-17 22:00:32 -0600358\t.boolval\t\t= true,
Simon Glass9d2eb922017-06-18 22:09:06 -0600359\t.bytearray\t\t= {0x6, 0x0, 0x0},
360\t.byteval\t\t= 0x5,
Simon Glass3b55e3f2021-11-23 11:03:39 -0700361\t.int64val\t\t= {0x12345678, 0x9abcdef0},
Simon Glassa7d66982021-07-28 19:23:10 -0600362\t.intarray\t\t= {0x2, 0x3, 0x4},
Simon Glass9d2eb922017-06-18 22:09:06 -0600363\t.intval\t\t\t= 0x1,
Simon Glass131e0b02017-08-29 14:15:49 -0600364\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
365\t\t0x11},
Simon Glass43118322021-07-28 19:23:11 -0600366\t.maybe_empty_int\t= {0x0},
Simon Glassc82de562019-05-17 22:00:32 -0600367\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600368\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glassc82de562019-05-17 22:00:32 -0600369\t.stringval\t\t= "message",
Simon Glass9d2eb922017-06-18 22:09:06 -0600370};
Simon Glass1d8364a2020-12-28 20:34:54 -0700371U_BOOT_DRVINFO(spl_test) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600372\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -0700373\t.plat\t\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700374\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600375\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600376};
377
Simon Glassbe749002021-02-03 06:01:15 -0700378/*
379 * Node /spl-test2 index 3
380 * driver sandbox_spl_test parent None
381 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300382static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass7f5e2262020-07-07 21:32:06 -0600383\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glass9d2eb922017-06-18 22:09:06 -0600384\t.bytearray\t\t= {0x1, 0x23, 0x34},
385\t.byteval\t\t= 0x8,
Simon Glassa7d66982021-07-28 19:23:10 -0600386\t.intarray\t\t= {0x5, 0x0, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600387\t.intval\t\t\t= 0x3,
Simon Glass8034e4d2020-10-03 11:31:27 -0600388\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
Simon Glass131e0b02017-08-29 14:15:49 -0600389\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600390\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glassc82de562019-05-17 22:00:32 -0600391\t.stringval\t\t= "message2",
Simon Glass9d2eb922017-06-18 22:09:06 -0600392};
Simon Glass1d8364a2020-12-28 20:34:54 -0700393U_BOOT_DRVINFO(spl_test2) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600394\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -0700395\t.plat\t\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700396\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600397\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600398};
399
Simon Glassbe749002021-02-03 06:01:15 -0700400/*
401 * Node /spl-test3 index 4
402 * driver sandbox_spl_test parent None
403 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300404static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glass8034e4d2020-10-03 11:31:27 -0600405\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
406\t\t0x0},
Simon Glass43118322021-07-28 19:23:11 -0600407\t.maybe_empty_int\t= {0x1},
Simon Glass9d2eb922017-06-18 22:09:06 -0600408\t.stringarray\t\t= {"one", "", ""},
409};
Simon Glass1d8364a2020-12-28 20:34:54 -0700410U_BOOT_DRVINFO(spl_test3) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600411\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -0700412\t.plat\t\t= &dtv_spl_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700413\t.plat_size\t= sizeof(dtv_spl_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600414\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600415};
416
Simon Glass2500de22020-12-28 20:35:05 -0700417'''
Simon Glassc7b4b832021-02-03 06:01:20 -0700418 uclass_text_inst = '''
419
420#include <common.h>
421#include <dm.h>
422#include <dt-structs.h>
423
424/*
Simon Glassfea2f252021-02-03 06:01:21 -0700425 * uclass declarations, ordered by 'struct uclass' linker_list idx:
426 * 0: i2c
427 * 1: misc
428 * 2: root
429 * 3: testbus
430 * 4: testfdt
Simon Glassc7b4b832021-02-03 06:01:20 -0700431 *
Simon Glassfea2f252021-02-03 06:01:21 -0700432 * Sequence numbers allocated in each uclass:
Simon Glassc7b4b832021-02-03 06:01:20 -0700433 * i2c: UCLASS_I2C
434 * 4: /i2c
435 * misc: UCLASS_MISC
436 * 0: /spl-test
437 * 1: /spl-test3
438 * root: UCLASS_ROOT
439 * 0: /
440 * testbus: UCLASS_TEST_BUS
441 * 2: /some-bus
442 * testfdt: UCLASS_TEST_FDT
443 * 1: /some-bus/test
444 * 2: /some-bus/test0
445 */
446
447struct list_head uclass_head = {
448 .prev = &DM_UCLASS_REF(testfdt)->sibling_node,
449 .next = &DM_UCLASS_REF(i2c)->sibling_node,
450};
451
452DM_UCLASS_INST(i2c) = {
453 .uc_drv = DM_UCLASS_DRIVER_REF(i2c),
454 .sibling_node = {
455 .prev = &uclass_head,
456 .next = &DM_UCLASS_REF(misc)->sibling_node,
457 },
458 .dev_head = {
459 .prev = &DM_DEVICE_REF(i2c)->uclass_node,
460 .next = &DM_DEVICE_REF(i2c)->uclass_node,
461 },
462};
463
464DM_UCLASS_INST(misc) = {
465 .uc_drv = DM_UCLASS_DRIVER_REF(misc),
466 .sibling_node = {
467 .prev = &DM_UCLASS_REF(i2c)->sibling_node,
468 .next = &DM_UCLASS_REF(root)->sibling_node,
469 },
470 .dev_head = {
471 .prev = &DM_DEVICE_REF(spl_test3)->uclass_node,
472 .next = &DM_DEVICE_REF(spl_test)->uclass_node,
473 },
474};
475
476DM_UCLASS_INST(root) = {
477 .uc_drv = DM_UCLASS_DRIVER_REF(root),
478 .sibling_node = {
479 .prev = &DM_UCLASS_REF(misc)->sibling_node,
480 .next = &DM_UCLASS_REF(testbus)->sibling_node,
481 },
482 .dev_head = {
483 .prev = &DM_DEVICE_REF(root)->uclass_node,
484 .next = &DM_DEVICE_REF(root)->uclass_node,
485 },
486};
487
488DM_UCLASS_INST(testbus) = {
489 .uc_drv = DM_UCLASS_DRIVER_REF(testbus),
490 .sibling_node = {
491 .prev = &DM_UCLASS_REF(root)->sibling_node,
492 .next = &DM_UCLASS_REF(testfdt)->sibling_node,
493 },
494 .dev_head = {
495 .prev = &DM_DEVICE_REF(some_bus)->uclass_node,
496 .next = &DM_DEVICE_REF(some_bus)->uclass_node,
497 },
498};
499
500#include <dm/test.h>
501u8 _testfdt_priv_[sizeof(struct dm_test_uc_priv)]
502 __attribute__ ((section (".priv_data")));
503DM_UCLASS_INST(testfdt) = {
504 .priv_ = _testfdt_priv_,
505 .uc_drv = DM_UCLASS_DRIVER_REF(testfdt),
506 .sibling_node = {
507 .prev = &DM_UCLASS_REF(testbus)->sibling_node,
508 .next = &uclass_head,
509 },
510 .dev_head = {
511 .prev = &DM_DEVICE_REF(test0)->uclass_node,
512 .next = &DM_DEVICE_REF(test)->uclass_node,
513 },
514};
515
Simon Glassfea2f252021-02-03 06:01:21 -0700516'''
517 device_text_inst = '''/*
518 * DO NOT MODIFY
519 *
520 * Declares the DM_DEVICE_INST() records.
521 * This was generated by dtoc from a .dtb (device tree binary) file.
522 */
523
524#include <common.h>
525#include <dm.h>
526#include <dt-structs.h>
527
528/*
529 * udevice declarations, ordered by 'struct udevice' linker_list position:
530 *
531 * idx udevice driver
532 * --- -------------------- --------------------
533 * 0: i2c sandbox_i2c
534 * 1: root root_driver
535 * 2: some_bus denx_u_boot_test_bus
536 * 3: spl_test sandbox_spl_test
537 * 4: spl_test3 sandbox_spl_test
538 * 5: test denx_u_boot_fdt_test
539 * 6: test0 denx_u_boot_fdt_test
540 * --- -------------------- --------------------
541 */
542
543/*
544 * Node /i2c index 0
545 * driver sandbox_i2c parent root_driver
546*/
547static struct dtd_sandbox_i2c dtv_i2c = {
548\t.intval\t\t\t= 0x3,
549};
550
551#include <asm/i2c.h>
552u8 _sandbox_i2c_priv_i2c[sizeof(struct sandbox_i2c_priv)]
553\t__attribute__ ((section (".priv_data")));
554#include <i2c.h>
555u8 _sandbox_i2c_uc_priv_i2c[sizeof(struct dm_i2c_bus)]
556\t__attribute__ ((section (".priv_data")));
557
558DM_DEVICE_INST(i2c) = {
559\t.driver\t\t= DM_DRIVER_REF(sandbox_i2c),
560\t.name\t\t= "sandbox_i2c",
561\t.plat_\t\t= &dtv_i2c,
562\t.priv_\t\t= _sandbox_i2c_priv_i2c,
563\t.uclass\t\t= DM_UCLASS_REF(i2c),
564\t.uclass_priv_ = _sandbox_i2c_uc_priv_i2c,
565\t.uclass_node\t= {
566\t\t.prev = &DM_UCLASS_REF(i2c)->dev_head,
567\t\t.next = &DM_UCLASS_REF(i2c)->dev_head,
568\t},
569\t.child_head\t= {
570\t\t.prev = &DM_DEVICE_REF(i2c)->child_head,
571\t\t.next = &DM_DEVICE_REF(i2c)->child_head,
572\t},
573\t.sibling_node\t= {
574\t\t.prev = &DM_DEVICE_REF(root)->child_head,
575\t\t.next = &DM_DEVICE_REF(some_bus)->sibling_node,
576\t},
577\t.seq_ = 4,
578};
579
580/*
581 * Node / index 1
582 * driver root_driver parent None
583*/
584static struct dtd_root_driver dtv_root = {
585};
586
587DM_DEVICE_INST(root) = {
588\t.driver\t\t= DM_DRIVER_REF(root_driver),
589\t.name\t\t= "root_driver",
590\t.plat_\t\t= &dtv_root,
591\t.uclass\t\t= DM_UCLASS_REF(root),
592\t.uclass_node\t= {
593\t\t.prev = &DM_UCLASS_REF(root)->dev_head,
594\t\t.next = &DM_UCLASS_REF(root)->dev_head,
595\t},
596\t.child_head\t= {
597\t\t.prev = &DM_DEVICE_REF(spl_test3)->sibling_node,
598\t\t.next = &DM_DEVICE_REF(i2c)->sibling_node,
599\t},
600\t.seq_ = 0,
601};
602
603/*
604 * Node /some-bus index 2
605 * driver denx_u_boot_test_bus parent root_driver
606*/
607
608#include <dm/test.h>
609struct dm_test_pdata __attribute__ ((section (".priv_data")))
610\t_denx_u_boot_test_bus_plat_some_bus = {
611\t.dtplat = {
612\t\t.ping_add\t\t= 0x4,
613\t\t.ping_expect\t\t= 0x4,
614\t\t.reg\t\t\t= {0x3, 0x1},
615\t},
616};
617#include <dm/test.h>
618u8 _denx_u_boot_test_bus_priv_some_bus[sizeof(struct dm_test_priv)]
619\t__attribute__ ((section (".priv_data")));
620#include <dm/test.h>
Simon Glasse3304472022-05-08 04:39:23 -0600621u8 _denx_u_boot_test_bus_ucplat_some_bus[sizeof(struct dm_test_uclass_plat)]
Simon Glassfea2f252021-02-03 06:01:21 -0700622\t__attribute__ ((section (".priv_data")));
Simon Glassd1f12cf2022-05-08 04:39:24 -0600623#include <dm/test.h>
624u8 _denx_u_boot_test_bus_uc_priv_some_bus[sizeof(struct dm_test_uclass_priv)]
625 __attribute__ ((section (".priv_data")));
Simon Glassfea2f252021-02-03 06:01:21 -0700626#include <test.h>
627
628DM_DEVICE_INST(some_bus) = {
629\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_test_bus),
630\t.name\t\t= "denx_u_boot_test_bus",
631\t.plat_\t\t= &_denx_u_boot_test_bus_plat_some_bus,
632\t.uclass_plat_\t= _denx_u_boot_test_bus_ucplat_some_bus,
633\t.driver_data\t= DM_TEST_TYPE_FIRST,
634\t.priv_\t\t= _denx_u_boot_test_bus_priv_some_bus,
635\t.uclass\t\t= DM_UCLASS_REF(testbus),
Simon Glassd1f12cf2022-05-08 04:39:24 -0600636\t.uclass_priv_ = _denx_u_boot_test_bus_uc_priv_some_bus,
Simon Glassfea2f252021-02-03 06:01:21 -0700637\t.uclass_node\t= {
638\t\t.prev = &DM_UCLASS_REF(testbus)->dev_head,
639\t\t.next = &DM_UCLASS_REF(testbus)->dev_head,
640\t},
641\t.child_head\t= {
642\t\t.prev = &DM_DEVICE_REF(test0)->sibling_node,
643\t\t.next = &DM_DEVICE_REF(test)->sibling_node,
644\t},
645\t.sibling_node\t= {
646\t\t.prev = &DM_DEVICE_REF(i2c)->sibling_node,
647\t\t.next = &DM_DEVICE_REF(spl_test)->sibling_node,
648\t},
649\t.seq_ = 2,
650};
651
652/*
653 * Node /spl-test index 3
654 * driver sandbox_spl_test parent root_driver
655*/
656static struct dtd_sandbox_spl_test dtv_spl_test = {
657\t.boolval\t\t= true,
658\t.intval\t\t\t= 0x1,
659};
660
661DM_DEVICE_INST(spl_test) = {
662\t.driver\t\t= DM_DRIVER_REF(sandbox_spl_test),
663\t.name\t\t= "sandbox_spl_test",
664\t.plat_\t\t= &dtv_spl_test,
665\t.uclass\t\t= DM_UCLASS_REF(misc),
666\t.uclass_node\t= {
667\t\t.prev = &DM_UCLASS_REF(misc)->dev_head,
668\t\t.next = &DM_DEVICE_REF(spl_test3)->uclass_node,
669\t},
670\t.child_head\t= {
671\t\t.prev = &DM_DEVICE_REF(spl_test)->child_head,
672\t\t.next = &DM_DEVICE_REF(spl_test)->child_head,
673\t},
674\t.sibling_node\t= {
675\t\t.prev = &DM_DEVICE_REF(some_bus)->sibling_node,
676\t\t.next = &DM_DEVICE_REF(spl_test3)->sibling_node,
677\t},
678\t.seq_ = 0,
679};
680
681/*
682 * Node /spl-test3 index 4
683 * driver sandbox_spl_test parent root_driver
684*/
685static struct dtd_sandbox_spl_test dtv_spl_test3 = {
686\t.longbytearray\t\t= {0x90a0b0c, 0xd0e0f10},
687\t.stringarray\t\t= "one",
688};
689
690DM_DEVICE_INST(spl_test3) = {
691\t.driver\t\t= DM_DRIVER_REF(sandbox_spl_test),
692\t.name\t\t= "sandbox_spl_test",
693\t.plat_\t\t= &dtv_spl_test3,
694\t.uclass\t\t= DM_UCLASS_REF(misc),
695\t.uclass_node\t= {
696\t\t.prev = &DM_DEVICE_REF(spl_test)->uclass_node,
697\t\t.next = &DM_UCLASS_REF(misc)->dev_head,
698\t},
699\t.child_head\t= {
700\t\t.prev = &DM_DEVICE_REF(spl_test3)->child_head,
701\t\t.next = &DM_DEVICE_REF(spl_test3)->child_head,
702\t},
703\t.sibling_node\t= {
704\t\t.prev = &DM_DEVICE_REF(spl_test)->sibling_node,
705\t\t.next = &DM_DEVICE_REF(root)->child_head,
706\t},
707\t.seq_ = 1,
708};
709
710/*
711 * Node /some-bus/test index 5
712 * driver denx_u_boot_fdt_test parent denx_u_boot_test_bus
713*/
714
715#include <dm/test.h>
716struct dm_test_pdata __attribute__ ((section (".priv_data")))
717\t_denx_u_boot_fdt_test_plat_test = {
718\t.dtplat = {
719\t\t.ping_add\t\t= 0x5,
720\t\t.ping_expect\t\t= 0x5,
Simon Glass94ba59e2021-03-26 16:17:26 +1300721\t\t.reg\t\t\t= {0x5},
Simon Glassfea2f252021-02-03 06:01:21 -0700722\t},
723};
724#include <dm/test.h>
725u8 _denx_u_boot_fdt_test_priv_test[sizeof(struct dm_test_priv)]
726\t__attribute__ ((section (".priv_data")));
727#include <dm/test.h>
728u8 _denx_u_boot_fdt_test_parent_plat_test[sizeof(struct dm_test_parent_plat)]
729\t__attribute__ ((section (".priv_data")));
730#include <dm/test.h>
731u8 _denx_u_boot_fdt_test_parent_priv_test[sizeof(struct dm_test_parent_data)]
732\t__attribute__ ((section (".priv_data")));
733
734DM_DEVICE_INST(test) = {
735\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_fdt_test),
736\t.name\t\t= "denx_u_boot_fdt_test",
737\t.plat_\t\t= &_denx_u_boot_fdt_test_plat_test,
738\t.parent_plat_\t= _denx_u_boot_fdt_test_parent_plat_test,
739\t.driver_data\t= DM_TEST_TYPE_FIRST,
740\t.parent\t\t= DM_DEVICE_REF(some_bus),
741\t.priv_\t\t= _denx_u_boot_fdt_test_priv_test,
742\t.uclass\t\t= DM_UCLASS_REF(testfdt),
743\t.parent_priv_\t= _denx_u_boot_fdt_test_parent_priv_test,
744\t.uclass_node\t= {
745\t\t.prev = &DM_UCLASS_REF(testfdt)->dev_head,
746\t\t.next = &DM_DEVICE_REF(test0)->uclass_node,
747\t},
748\t.child_head\t= {
749\t\t.prev = &DM_DEVICE_REF(test)->child_head,
750\t\t.next = &DM_DEVICE_REF(test)->child_head,
751\t},
752\t.sibling_node\t= {
753\t\t.prev = &DM_DEVICE_REF(some_bus)->child_head,
754\t\t.next = &DM_DEVICE_REF(test0)->sibling_node,
755\t},
756\t.seq_ = 1,
757};
758
759/*
760 * Node /some-bus/test0 index 6
761 * driver denx_u_boot_fdt_test parent denx_u_boot_test_bus
762*/
763
764#include <dm/test.h>
765struct dm_test_pdata __attribute__ ((section (".priv_data")))
766\t_denx_u_boot_fdt_test_plat_test0 = {
767\t.dtplat = {
768\t},
769};
770#include <dm/test.h>
771u8 _denx_u_boot_fdt_test_priv_test0[sizeof(struct dm_test_priv)]
772\t__attribute__ ((section (".priv_data")));
773#include <dm/test.h>
774u8 _denx_u_boot_fdt_test_parent_plat_test0[sizeof(struct dm_test_parent_plat)]
775\t__attribute__ ((section (".priv_data")));
776#include <dm/test.h>
777u8 _denx_u_boot_fdt_test_parent_priv_test0[sizeof(struct dm_test_parent_data)]
778\t__attribute__ ((section (".priv_data")));
779
780DM_DEVICE_INST(test0) = {
781\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_fdt_test),
782\t.name\t\t= "denx_u_boot_fdt_test",
783\t.plat_\t\t= &_denx_u_boot_fdt_test_plat_test0,
784\t.parent_plat_\t= _denx_u_boot_fdt_test_parent_plat_test0,
785\t.driver_data\t= DM_TEST_TYPE_SECOND,
786\t.parent\t\t= DM_DEVICE_REF(some_bus),
787\t.priv_\t\t= _denx_u_boot_fdt_test_priv_test0,
788\t.uclass\t\t= DM_UCLASS_REF(testfdt),
789\t.parent_priv_\t= _denx_u_boot_fdt_test_parent_priv_test0,
790\t.uclass_node\t= {
791\t\t.prev = &DM_DEVICE_REF(test)->uclass_node,
792\t\t.next = &DM_UCLASS_REF(testfdt)->dev_head,
793\t},
794\t.child_head\t= {
795\t\t.prev = &DM_DEVICE_REF(test0)->child_head,
796\t\t.next = &DM_DEVICE_REF(test0)->child_head,
797\t},
798\t.sibling_node\t= {
799\t\t.prev = &DM_DEVICE_REF(test)->sibling_node,
800\t\t.next = &DM_DEVICE_REF(some_bus)->child_head,
801\t},
802\t.seq_ = 2,
803};
804
805'''
Simon Glass07ee48e2020-12-28 20:34:49 -0700806
807 def test_simple(self):
808 """Test output from some simple nodes with various types of data"""
809 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass80025522022-01-29 14:14:04 -0700810 output = tools.get_output_filename('output')
Simon Glass07ee48e2020-12-28 20:34:49 -0700811 self.run_test(['struct'], dtb_file, output)
812 with open(output) as infile:
813 data = infile.read()
814
815 self._check_strings(self.struct_text, data)
816
817 self.run_test(['platdata'], dtb_file, output)
818 with open(output) as infile:
819 data = infile.read()
820
821 self._check_strings(self.platdata_text, data)
Simon Glass9d2eb922017-06-18 22:09:06 -0600822
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700823 self.run_test(['decl'], dtb_file, output)
824 with open(output) as infile:
825 data = infile.read()
826
827 self._check_strings(self.decl_text, data)
828
Simon Glass4e8e8462020-12-28 20:34:52 -0700829 # Try the 'all' command
830 self.run_test(['all'], dtb_file, output)
Simon Glass80025522022-01-29 14:14:04 -0700831 data = tools.read_file(output, binary=False)
Simon Glassfea2f252021-02-03 06:01:21 -0700832 self._check_strings(
Simon Glass705b84b2021-04-27 08:19:48 +1200833 self.decl_text + self.platdata_text + self.struct_text, data)
Simon Glass4e8e8462020-12-28 20:34:52 -0700834
Walter Lozanoe675d962020-07-03 08:07:17 -0300835 def test_driver_alias(self):
836 """Test output from a device tree file with a driver alias"""
837 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
Simon Glass80025522022-01-29 14:14:04 -0700838 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300839 self.run_test(['struct'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300840 with open(output) as infile:
841 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700842 self._check_strings(HEADER + '''
Walter Lozanoe675d962020-07-03 08:07:17 -0300843struct dtd_sandbox_gpio {
844\tconst char *\tgpio_bank_name;
845\tbool\t\tgpio_controller;
846\tfdt32_t\t\tsandbox_gpio_count;
847};
Walter Lozanoe675d962020-07-03 08:07:17 -0300848''', data)
849
Walter Lozanoa324e412020-06-25 01:10:08 -0300850 self.run_test(['platdata'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300851 with open(output) as infile:
852 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700853 self._check_strings(C_HEADER + '''
Simon Glassbe749002021-02-03 06:01:15 -0700854/*
Simon Glasse2119082021-02-03 06:01:19 -0700855 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
856 *
857 * idx driver_info driver
858 * --- -------------------- --------------------
859 * 0: gpios_at_0 sandbox_gpio
860 * --- -------------------- --------------------
861 */
862
863/*
Simon Glassbe749002021-02-03 06:01:15 -0700864 * Node /gpios@0 index 0
865 * driver sandbox_gpio parent None
866 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300867static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300868\t.gpio_bank_name\t\t= "a",
869\t.gpio_controller\t= true,
870\t.sandbox_gpio_count\t= 0x14,
871};
Simon Glass1d8364a2020-12-28 20:34:54 -0700872U_BOOT_DRVINFO(gpios_at_0) = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300873\t.name\t\t= "sandbox_gpio",
Simon Glasse2119082021-02-03 06:01:19 -0700874\t.plat\t\t= &dtv_gpios_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700875\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600876\t.parent_idx\t= -1,
Walter Lozanoe675d962020-07-03 08:07:17 -0300877};
878
879''', data)
880
Walter Lozanoa324e412020-06-25 01:10:08 -0300881 def test_invalid_driver(self):
882 """Test output from a device tree file with an invalid driver"""
883 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
Simon Glass80025522022-01-29 14:14:04 -0700884 output = tools.get_output_filename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700885 with test_util.capture_sys_output() as _:
Simon Glass3809ad92021-02-03 06:01:12 -0700886 dtb_platdata.run_steps(
887 ['struct'], dtb_file, False, output, [], None, False,
888 scan=copy_scan())
Walter Lozanoa324e412020-06-25 01:10:08 -0300889 with open(output) as infile:
890 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700891 self._check_strings(HEADER + '''
Walter Lozanoa324e412020-06-25 01:10:08 -0300892struct dtd_invalid {
893};
894''', data)
895
Simon Glassa60cabd2020-12-28 20:34:47 -0700896 with test_util.capture_sys_output() as _:
Simon Glass3809ad92021-02-03 06:01:12 -0700897 dtb_platdata.run_steps(
898 ['platdata'], dtb_file, False, output, [], None, False,
899 scan=copy_scan())
Walter Lozanoa324e412020-06-25 01:10:08 -0300900 with open(output) as infile:
901 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700902 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -0700903/*
904 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
905 *
906 * idx driver_info driver
907 * --- -------------------- --------------------
908 * 0: spl_test invalid
909 * --- -------------------- --------------------
910 */
911
Simon Glass192f8132020-10-03 11:31:25 -0600912/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300913static struct dtd_invalid dtv_spl_test = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300914};
Simon Glass1d8364a2020-12-28 20:34:54 -0700915U_BOOT_DRVINFO(spl_test) = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300916\t.name\t\t= "invalid",
Simon Glasse2119082021-02-03 06:01:19 -0700917\t.plat\t\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700918\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600919\t.parent_idx\t= -1,
Walter Lozanoa324e412020-06-25 01:10:08 -0300920};
921
922''', data)
923
Simon Glass9d2eb922017-06-18 22:09:06 -0600924 def test_phandle(self):
925 """Test output from a node containing a phandle reference"""
926 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
Simon Glass80025522022-01-29 14:14:04 -0700927 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300928 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600929 with open(output) as infile:
930 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700931 self._check_strings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600932struct dtd_source {
Simon Glass3deeb472017-08-29 14:15:59 -0600933\tstruct phandle_2_arg clocks[4];
Simon Glasse8cea0e2023-01-11 16:10:18 -0700934\tunsigned char phandle_name_offset[13];
Simon Glass9d2eb922017-06-18 22:09:06 -0600935};
936struct dtd_target {
937\tfdt32_t\t\tintval;
938};
939''', data)
940
Walter Lozanoa324e412020-06-25 01:10:08 -0300941 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600942 with open(output) as infile:
943 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700944 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -0700945/*
946 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
947 *
948 * idx driver_info driver
949 * --- -------------------- --------------------
950 * 0: phandle2_target target
951 * 1: phandle3_target target
952 * 2: phandle_source source
953 * 3: phandle_source2 source
954 * 4: phandle_target target
955 * --- -------------------- --------------------
956 */
957
Simon Glass192f8132020-10-03 11:31:25 -0600958/* Node /phandle2-target index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300959static struct dtd_target dtv_phandle2_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600960\t.intval\t\t\t= 0x1,
961};
Simon Glass1d8364a2020-12-28 20:34:54 -0700962U_BOOT_DRVINFO(phandle2_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600963\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -0700964\t.plat\t\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700965\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600966\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600967};
968
Simon Glass192f8132020-10-03 11:31:25 -0600969/* Node /phandle3-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300970static struct dtd_target dtv_phandle3_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600971\t.intval\t\t\t= 0x2,
972};
Simon Glass1d8364a2020-12-28 20:34:54 -0700973U_BOOT_DRVINFO(phandle3_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600974\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -0700975\t.plat\t\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700976\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600977\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600978};
979
Simon Glass192f8132020-10-03 11:31:25 -0600980/* Node /phandle-source index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300981static struct dtd_source dtv_phandle_source = {
Simon Glassd0cd0752017-08-29 14:15:57 -0600982\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600983\t\t\t{4, {}},
984\t\t\t{0, {11}},
985\t\t\t{1, {12, 13}},
986\t\t\t{4, {}},},
Simon Glasse8cea0e2023-01-11 16:10:18 -0700987\t.phandle_name_offset = {0x0, 0x0, 0x0, 0x3, 0x66, 0x72, 0x65, 0x64,
988\t\t0x0, 0x0, 0x0, 0x0, 0x7b},
Simon Glass9d2eb922017-06-18 22:09:06 -0600989};
Simon Glass1d8364a2020-12-28 20:34:54 -0700990U_BOOT_DRVINFO(phandle_source) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600991\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -0700992\t.plat\t\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700993\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600994\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600995};
996
Simon Glass192f8132020-10-03 11:31:25 -0600997/* Node /phandle-source2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300998static struct dtd_source dtv_phandle_source2 = {
Simon Glass609e2b12018-07-06 10:27:31 -0600999\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -06001000\t\t\t{4, {}},},
Simon Glass609e2b12018-07-06 10:27:31 -06001001};
Simon Glass1d8364a2020-12-28 20:34:54 -07001002U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass609e2b12018-07-06 10:27:31 -06001003\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -07001004\t.plat\t\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -07001005\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -06001006\t.parent_idx\t= -1,
Simon Glass609e2b12018-07-06 10:27:31 -06001007};
1008
Simon Glass16382ce2020-12-28 20:35:04 -07001009/* Node /phandle-target index 4 */
1010static struct dtd_target dtv_phandle_target = {
1011\t.intval\t\t\t= 0x0,
1012};
1013U_BOOT_DRVINFO(phandle_target) = {
1014\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001015\t.plat\t\t= &dtv_phandle_target,
Simon Glass16382ce2020-12-28 20:35:04 -07001016\t.plat_size\t= sizeof(dtv_phandle_target),
1017\t.parent_idx\t= -1,
1018};
1019
Simon Glass9d2eb922017-06-18 22:09:06 -06001020''', data)
1021
Simon Glass961c1ce2018-07-06 10:27:35 -06001022 def test_phandle_single(self):
1023 """Test output from a node containing a phandle reference"""
1024 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
Simon Glass80025522022-01-29 14:14:04 -07001025 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001026 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001027 with open(output) as infile:
1028 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001029 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -06001030struct dtd_source {
1031\tstruct phandle_0_arg clocks[1];
1032};
1033struct dtd_target {
1034\tfdt32_t\t\tintval;
1035};
1036''', data)
1037
1038 def test_phandle_reorder(self):
1039 """Test that phandle targets are generated before their references"""
1040 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
Simon Glass80025522022-01-29 14:14:04 -07001041 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001042 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001043 with open(output) as infile:
1044 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001045 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001046/*
1047 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1048 *
1049 * idx driver_info driver
1050 * --- -------------------- --------------------
1051 * 0: phandle_source2 source
1052 * 1: phandle_target target
1053 * --- -------------------- --------------------
1054 */
1055
Simon Glass192f8132020-10-03 11:31:25 -06001056/* Node /phandle-source2 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001057static struct dtd_source dtv_phandle_source2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001058\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -06001059\t\t\t{1, {}},},
Simon Glass961c1ce2018-07-06 10:27:35 -06001060};
Simon Glass1d8364a2020-12-28 20:34:54 -07001061U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001062\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -07001063\t.plat\t\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -07001064\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -06001065\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -06001066};
1067
Simon Glass16382ce2020-12-28 20:35:04 -07001068/* Node /phandle-target index 1 */
1069static struct dtd_target dtv_phandle_target = {
1070};
1071U_BOOT_DRVINFO(phandle_target) = {
1072\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001073\t.plat\t\t= &dtv_phandle_target,
Simon Glass16382ce2020-12-28 20:35:04 -07001074\t.plat_size\t= sizeof(dtv_phandle_target),
1075\t.parent_idx\t= -1,
1076};
1077
Simon Glass961c1ce2018-07-06 10:27:35 -06001078''', data)
1079
Walter Lozano5541fc02020-06-25 01:10:17 -03001080 def test_phandle_cd_gpio(self):
1081 """Test that phandle targets are generated when unsing cd-gpios"""
1082 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
Simon Glass80025522022-01-29 14:14:04 -07001083 output = tools.get_output_filename('output')
Simon Glass3809ad92021-02-03 06:01:12 -07001084 dtb_platdata.run_steps(
1085 ['platdata'], dtb_file, False, output, [], None, False,
1086 warning_disabled=True, scan=copy_scan())
Walter Lozano5541fc02020-06-25 01:10:17 -03001087 with open(output) as infile:
1088 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001089 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001090/*
1091 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1092 *
1093 * idx driver_info driver
1094 * --- -------------------- --------------------
1095 * 0: phandle2_target target
1096 * 1: phandle3_target target
1097 * 2: phandle_source source
1098 * 3: phandle_source2 source
1099 * 4: phandle_target target
1100 * --- -------------------- --------------------
1101 */
1102
Simon Glass192f8132020-10-03 11:31:25 -06001103/* Node /phandle2-target index 0 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001104static struct dtd_target dtv_phandle2_target = {
1105\t.intval\t\t\t= 0x1,
1106};
Simon Glass1d8364a2020-12-28 20:34:54 -07001107U_BOOT_DRVINFO(phandle2_target) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001108\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001109\t.plat\t\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -07001110\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -06001111\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -03001112};
1113
Simon Glass192f8132020-10-03 11:31:25 -06001114/* Node /phandle3-target index 1 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001115static struct dtd_target dtv_phandle3_target = {
1116\t.intval\t\t\t= 0x2,
1117};
Simon Glass1d8364a2020-12-28 20:34:54 -07001118U_BOOT_DRVINFO(phandle3_target) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001119\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001120\t.plat\t\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -07001121\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -06001122\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -03001123};
1124
Simon Glass192f8132020-10-03 11:31:25 -06001125/* Node /phandle-source index 2 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001126static struct dtd_source dtv_phandle_source = {
1127\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -06001128\t\t\t{4, {}},
1129\t\t\t{0, {11}},
1130\t\t\t{1, {12, 13}},
1131\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -03001132};
Simon Glass1d8364a2020-12-28 20:34:54 -07001133U_BOOT_DRVINFO(phandle_source) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001134\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -07001135\t.plat\t\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -07001136\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -06001137\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -03001138};
1139
Simon Glass192f8132020-10-03 11:31:25 -06001140/* Node /phandle-source2 index 3 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001141static struct dtd_source dtv_phandle_source2 = {
1142\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -06001143\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -03001144};
Simon Glass1d8364a2020-12-28 20:34:54 -07001145U_BOOT_DRVINFO(phandle_source2) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001146\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -07001147\t.plat\t\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -07001148\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -06001149\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -03001150};
1151
Simon Glass16382ce2020-12-28 20:35:04 -07001152/* Node /phandle-target index 4 */
1153static struct dtd_target dtv_phandle_target = {
1154\t.intval\t\t\t= 0x0,
1155};
1156U_BOOT_DRVINFO(phandle_target) = {
1157\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001158\t.plat\t\t= &dtv_phandle_target,
Simon Glass16382ce2020-12-28 20:35:04 -07001159\t.plat_size\t= sizeof(dtv_phandle_target),
1160\t.parent_idx\t= -1,
1161};
1162
Walter Lozano5541fc02020-06-25 01:10:17 -03001163''', data)
1164
Simon Glass961c1ce2018-07-06 10:27:35 -06001165 def test_phandle_bad(self):
1166 """Test a node containing an invalid phandle fails"""
Simon Glass04150022018-10-01 21:12:43 -06001167 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
1168 capture_stderr=True)
Simon Glass80025522022-01-29 14:14:04 -07001169 output = tools.get_output_filename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001170 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001171 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001172 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
Simon Glassa60cabd2020-12-28 20:34:47 -07001173 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001174
1175 def test_phandle_bad2(self):
1176 """Test a phandle target missing its #*-cells property"""
Simon Glass04150022018-10-01 21:12:43 -06001177 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
1178 capture_stderr=True)
Simon Glass80025522022-01-29 14:14:04 -07001179 output = tools.get_output_filename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001180 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001181 self.run_test(['struct'], dtb_file, output)
Walter Lozano179f0b62020-06-25 01:10:16 -03001182 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glassa60cabd2020-12-28 20:34:47 -07001183 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001184
Simon Glass1b1fe412017-08-29 14:15:50 -06001185 def test_addresses64(self):
1186 """Test output from a node with a 'reg' property with na=2, ns=2"""
1187 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
Simon Glass80025522022-01-29 14:14:04 -07001188 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001189 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001190 with open(output) as infile:
1191 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001192 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001193struct dtd_test1 {
1194\tfdt64_t\t\treg[2];
1195};
1196struct dtd_test2 {
1197\tfdt64_t\t\treg[2];
1198};
1199struct dtd_test3 {
1200\tfdt64_t\t\treg[4];
1201};
1202''', data)
1203
Walter Lozanoa324e412020-06-25 01:10:08 -03001204 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001205 with open(output) as infile:
1206 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001207 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001208/*
1209 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1210 *
1211 * idx driver_info driver
1212 * --- -------------------- --------------------
1213 * 0: test1 test1
1214 * 1: test2 test2
1215 * 2: test3 test3
1216 * --- -------------------- --------------------
1217 */
1218
Simon Glass192f8132020-10-03 11:31:25 -06001219/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001220static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001221\t.reg\t\t\t= {0x1234, 0x5678},
1222};
Simon Glass1d8364a2020-12-28 20:34:54 -07001223U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001224\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001225\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001226\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001227\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001228};
1229
Simon Glass192f8132020-10-03 11:31:25 -06001230/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001231static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001232\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
1233};
Simon Glass1d8364a2020-12-28 20:34:54 -07001234U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001235\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001236\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001237\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001238\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001239};
1240
Simon Glass192f8132020-10-03 11:31:25 -06001241/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001242static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001243\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
1244};
Simon Glass1d8364a2020-12-28 20:34:54 -07001245U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001246\t.name\t\t= "test3",
Simon Glasse2119082021-02-03 06:01:19 -07001247\t.plat\t\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -07001248\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -06001249\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001250};
1251
Simon Glass2500de22020-12-28 20:35:05 -07001252''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -06001253
1254 def test_addresses32(self):
1255 """Test output from a node with a 'reg' property with na=1, ns=1"""
1256 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
Simon Glass80025522022-01-29 14:14:04 -07001257 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001258 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001259 with open(output) as infile:
1260 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001261 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001262struct dtd_test1 {
1263\tfdt32_t\t\treg[2];
1264};
1265struct dtd_test2 {
1266\tfdt32_t\t\treg[4];
1267};
1268''', data)
1269
Walter Lozanoa324e412020-06-25 01:10:08 -03001270 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001271 with open(output) as infile:
1272 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001273 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001274/*
1275 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1276 *
1277 * idx driver_info driver
1278 * --- -------------------- --------------------
1279 * 0: test1 test1
1280 * 1: test2 test2
1281 * --- -------------------- --------------------
1282 */
1283
Simon Glass192f8132020-10-03 11:31:25 -06001284/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001285static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001286\t.reg\t\t\t= {0x1234, 0x5678},
1287};
Simon Glass1d8364a2020-12-28 20:34:54 -07001288U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001289\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001290\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001291\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001292\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001293};
1294
Simon Glass192f8132020-10-03 11:31:25 -06001295/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001296static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001297\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
1298};
Simon Glass1d8364a2020-12-28 20:34:54 -07001299U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001300\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001301\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001302\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001303\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001304};
1305
Simon Glass2500de22020-12-28 20:35:05 -07001306''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -06001307
1308 def test_addresses64_32(self):
1309 """Test output from a node with a 'reg' property with na=2, ns=1"""
1310 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
Simon Glass80025522022-01-29 14:14:04 -07001311 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001312 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001313 with open(output) as infile:
1314 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001315 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001316struct dtd_test1 {
1317\tfdt64_t\t\treg[2];
1318};
1319struct dtd_test2 {
1320\tfdt64_t\t\treg[2];
1321};
1322struct dtd_test3 {
1323\tfdt64_t\t\treg[4];
1324};
1325''', data)
1326
Walter Lozanoa324e412020-06-25 01:10:08 -03001327 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001328 with open(output) as infile:
1329 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001330 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001331/*
1332 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1333 *
1334 * idx driver_info driver
1335 * --- -------------------- --------------------
1336 * 0: test1 test1
1337 * 1: test2 test2
1338 * 2: test3 test3
1339 * --- -------------------- --------------------
1340 */
1341
Simon Glass192f8132020-10-03 11:31:25 -06001342/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001343static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001344\t.reg\t\t\t= {0x123400000000, 0x5678},
1345};
Simon Glass1d8364a2020-12-28 20:34:54 -07001346U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001347\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001348\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001349\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001350\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001351};
1352
Simon Glass192f8132020-10-03 11:31:25 -06001353/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001354static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001355\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
1356};
Simon Glass1d8364a2020-12-28 20:34:54 -07001357U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001358\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001359\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001360\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001361\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001362};
1363
Simon Glass192f8132020-10-03 11:31:25 -06001364/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001365static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001366\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
1367};
Simon Glass1d8364a2020-12-28 20:34:54 -07001368U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001369\t.name\t\t= "test3",
Simon Glasse2119082021-02-03 06:01:19 -07001370\t.plat\t\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -07001371\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -06001372\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001373};
1374
Simon Glass2500de22020-12-28 20:35:05 -07001375''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -06001376
1377 def test_addresses32_64(self):
1378 """Test output from a node with a 'reg' property with na=1, ns=2"""
1379 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
Simon Glass80025522022-01-29 14:14:04 -07001380 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001381 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001382 with open(output) as infile:
1383 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001384 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001385struct dtd_test1 {
1386\tfdt64_t\t\treg[2];
1387};
1388struct dtd_test2 {
1389\tfdt64_t\t\treg[2];
1390};
1391struct dtd_test3 {
1392\tfdt64_t\t\treg[4];
1393};
1394''', data)
1395
Walter Lozanoa324e412020-06-25 01:10:08 -03001396 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001397 with open(output) as infile:
1398 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001399 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001400/*
1401 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1402 *
1403 * idx driver_info driver
1404 * --- -------------------- --------------------
1405 * 0: test1 test1
1406 * 1: test2 test2
1407 * 2: test3 test3
1408 * --- -------------------- --------------------
1409 */
1410
Simon Glass192f8132020-10-03 11:31:25 -06001411/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001412static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001413\t.reg\t\t\t= {0x1234, 0x567800000000},
1414};
Simon Glass1d8364a2020-12-28 20:34:54 -07001415U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001416\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001417\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001418\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001419\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001420};
1421
Simon Glass192f8132020-10-03 11:31:25 -06001422/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001423static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001424\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
1425};
Simon Glass1d8364a2020-12-28 20:34:54 -07001426U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001427\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001428\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001429\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001430\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001431};
1432
Simon Glass192f8132020-10-03 11:31:25 -06001433/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001434static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001435\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
1436};
Simon Glass1d8364a2020-12-28 20:34:54 -07001437U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001438\t.name\t\t= "test3",
Simon Glasse2119082021-02-03 06:01:19 -07001439\t.plat\t\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -07001440\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -06001441\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001442};
1443
Simon Glass2500de22020-12-28 20:35:05 -07001444''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -06001445
1446 def test_bad_reg(self):
1447 """Test that a reg property with an invalid type generates an error"""
Simon Glass3bce93d2018-07-06 10:27:37 -06001448 # Capture stderr since dtc will emit warnings for this file
1449 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass80025522022-01-29 14:14:04 -07001450 output = tools.get_output_filename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001451 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001452 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001453 self.assertIn("Node 'spl-test' reg property is not an int",
Simon Glassa60cabd2020-12-28 20:34:47 -07001454 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001455
1456 def test_bad_reg2(self):
1457 """Test that a reg property with an invalid cell count is detected"""
Simon Glass3bce93d2018-07-06 10:27:37 -06001458 # Capture stderr since dtc will emit warnings for this file
1459 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass80025522022-01-29 14:14:04 -07001460 output = tools.get_output_filename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001461 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001462 self.run_test(['struct'], dtb_file, output)
Simon Glassa60cabd2020-12-28 20:34:47 -07001463 self.assertIn(
Simon Glass4415dc12021-03-26 16:17:27 +13001464 "Node 'spl-test' (parent '/') reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
Simon Glassa60cabd2020-12-28 20:34:47 -07001465 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001466
1467 def test_add_prop(self):
1468 """Test that a subequent node can add a new property to a struct"""
1469 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
Simon Glass80025522022-01-29 14:14:04 -07001470 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001471 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001472 with open(output) as infile:
1473 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001474 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -06001475struct dtd_sandbox_spl_test {
1476\tfdt32_t\t\tintarray;
1477\tfdt32_t\t\tintval;
1478};
1479''', data)
1480
Walter Lozanoa324e412020-06-25 01:10:08 -03001481 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001482 with open(output) as infile:
1483 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001484 self._check_strings(C_HEADER + '''
Simon Glassbe749002021-02-03 06:01:15 -07001485/*
Simon Glasse2119082021-02-03 06:01:19 -07001486 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1487 *
1488 * idx driver_info driver
1489 * --- -------------------- --------------------
1490 * 0: spl_test sandbox_spl_test
1491 * 1: spl_test2 sandbox_spl_test
1492 * --- -------------------- --------------------
1493 */
1494
1495/*
Simon Glassbe749002021-02-03 06:01:15 -07001496 * Node /spl-test index 0
1497 * driver sandbox_spl_test parent None
1498 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001499static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001500\t.intval\t\t\t= 0x1,
1501};
Simon Glass1d8364a2020-12-28 20:34:54 -07001502U_BOOT_DRVINFO(spl_test) = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001503\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -07001504\t.plat\t\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -07001505\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -06001506\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -06001507};
1508
Simon Glassbe749002021-02-03 06:01:15 -07001509/*
1510 * Node /spl-test2 index 1
1511 * driver sandbox_spl_test parent None
1512 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001513static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001514\t.intarray\t\t= 0x5,
1515};
Simon Glass1d8364a2020-12-28 20:34:54 -07001516U_BOOT_DRVINFO(spl_test2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001517\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -07001518\t.plat\t\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001519\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001520\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -06001521};
1522
Simon Glass2500de22020-12-28 20:35:05 -07001523''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -06001524
Simon Glassa60cabd2020-12-28 20:34:47 -07001525 def test_stdout(self):
Simon Glass961c1ce2018-07-06 10:27:35 -06001526 """Test output to stdout"""
1527 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass07ee48e2020-12-28 20:34:49 -07001528 with test_util.capture_sys_output() as (stdout, _):
Simon Glass6ca0c7a2020-12-28 20:34:48 -07001529 self.run_test(['struct'], dtb_file, None)
Simon Glass07ee48e2020-12-28 20:34:49 -07001530 self._check_strings(self.struct_text, stdout.getvalue())
Simon Glass961c1ce2018-07-06 10:27:35 -06001531
Simon Glassc3a310a82020-12-28 20:34:51 -07001532 def test_multi_to_file(self):
1533 """Test output of multiple pieces to a single file"""
1534 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass80025522022-01-29 14:14:04 -07001535 output = tools.get_output_filename('output')
Simon Glass4e8e8462020-12-28 20:34:52 -07001536 self.run_test(['all'], dtb_file, output)
Simon Glass80025522022-01-29 14:14:04 -07001537 data = tools.read_file(output, binary=False)
Simon Glassfea2f252021-02-03 06:01:21 -07001538 self._check_strings(
Simon Glass705b84b2021-04-27 08:19:48 +12001539 self.decl_text + self.platdata_text + self.struct_text, data)
Simon Glassc3a310a82020-12-28 20:34:51 -07001540
Simon Glassa60cabd2020-12-28 20:34:47 -07001541 def test_no_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -06001542 """Test running dtoc without a command"""
Simon Glassa60cabd2020-12-28 20:34:47 -07001543 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001544 self.run_test([], '', '')
Simon Glass961c1ce2018-07-06 10:27:35 -06001545 self.assertIn("Please specify a command: struct, platdata",
Simon Glassa60cabd2020-12-28 20:34:47 -07001546 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001547
Simon Glassa60cabd2020-12-28 20:34:47 -07001548 def test_bad_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -06001549 """Test running dtoc with an invalid command"""
1550 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass80025522022-01-29 14:14:04 -07001551 output = tools.get_output_filename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001552 with self.assertRaises(ValueError) as exc:
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001553 self.run_test(['invalid-cmd'], dtb_file, output)
1554 self.assertIn(
Simon Glassdb2b9ca2021-03-25 06:40:51 +13001555 "Unknown command 'invalid-cmd': (use: decl, platdata, struct)",
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001556 str(exc.exception))
Walter Lozanod82062b2020-07-28 19:06:23 -03001557
Simon Glass4e8e8462020-12-28 20:34:52 -07001558 def test_output_conflict(self):
1559 """Test a conflict between and output dirs and output file"""
1560 with self.assertRaises(ValueError) as exc:
Simon Glass3809ad92021-02-03 06:01:12 -07001561 dtb_platdata.run_steps(
1562 ['all'], None, False, 'out', ['cdir'], None, False,
1563 warning_disabled=True, scan=copy_scan())
Simon Glass4e8e8462020-12-28 20:34:52 -07001564 self.assertIn("Must specify either output or output_dirs, not both",
1565 str(exc.exception))
1566
Simon Glass705b84b2021-04-27 08:19:48 +12001567 def check_output_dirs(self, instantiate):
Simon Glass4e8e8462020-12-28 20:34:52 -07001568 # Remove the directory so that files from other tests are not there
Simon Glass80025522022-01-29 14:14:04 -07001569 tools._remove_output_dir()
1570 tools.prepare_output_dir(None)
Simon Glass4e8e8462020-12-28 20:34:52 -07001571
1572 # This should create the .dts and .dtb in the output directory
1573 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass80025522022-01-29 14:14:04 -07001574 outdir = tools.get_output_dir()
Simon Glass4e8e8462020-12-28 20:34:52 -07001575 fnames = glob.glob(outdir + '/*')
1576 self.assertEqual(2, len(fnames))
1577
Simon Glass3809ad92021-02-03 06:01:12 -07001578 dtb_platdata.run_steps(
Simon Glass705b84b2021-04-27 08:19:48 +12001579 ['all'], dtb_file, False, None, [outdir], None, instantiate,
Simon Glass3809ad92021-02-03 06:01:12 -07001580 warning_disabled=True, scan=copy_scan())
Simon Glass4e8e8462020-12-28 20:34:52 -07001581 fnames = glob.glob(outdir + '/*')
Simon Glass705b84b2021-04-27 08:19:48 +12001582 return fnames
1583
1584 def test_output_dirs(self):
1585 """Test outputting files to a directory"""
1586 fnames = self.check_output_dirs(False)
1587 self.assertEqual(5, len(fnames))
Simon Glass4e8e8462020-12-28 20:34:52 -07001588
1589 leafs = set(os.path.basename(fname) for fname in fnames)
1590 self.assertEqual(
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001591 {'dt-structs-gen.h', 'source.dts', 'dt-plat.c', 'source.dtb',
Simon Glass705b84b2021-04-27 08:19:48 +12001592 'dt-decl.h'},
1593 leafs)
1594
1595 def test_output_dirs_inst(self):
1596 """Test outputting files to a directory with instantiation"""
1597 fnames = self.check_output_dirs(True)
1598 self.assertEqual(6, len(fnames))
1599
1600 leafs = set(os.path.basename(fname) for fname in fnames)
1601 self.assertEqual(
1602 {'dt-structs-gen.h', 'source.dts', 'source.dtb',
Simon Glassfea2f252021-02-03 06:01:21 -07001603 'dt-uclass.c', 'dt-decl.h', 'dt-device.c'},
Simon Glass4e8e8462020-12-28 20:34:52 -07001604 leafs)
Simon Glass047a4802021-02-03 06:01:00 -07001605
1606 def setup_process_test(self):
1607 """Set up a test of process_nodes()
1608
1609 This uses saved_scan but returns a deep copy of it, so it is safe to
1610 modify it in these tests
1611
1612 Returns:
1613 tuple:
1614 DtbPlatdata: object to test
1615 Scanner: scanner to use
1616 """
1617 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass80025522022-01-29 14:14:04 -07001618 output = tools.get_output_filename('output')
Simon Glass047a4802021-02-03 06:01:00 -07001619
1620 # Take a copy before messing with it
Simon Glassc14fd0c2021-02-03 06:01:11 -07001621 scan = copy_scan()
Simon Glass047a4802021-02-03 06:01:00 -07001622 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
1623 plat.scan_dtb()
Simon Glassc14fd0c2021-02-03 06:01:11 -07001624 plat.scan_tree(False)
Simon Glass047a4802021-02-03 06:01:00 -07001625 plat.prepare_nodes()
1626 return plat, scan
1627
1628 def test_process_nodes(self):
1629 """Test processing nodes to add various info"""
1630 plat, scan = self.setup_process_test()
1631 plat.process_nodes(True)
1632
1633 i2c_node = plat._fdt.GetNode('/i2c@0')
1634 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
1635 pmic = scan._drivers['sandbox_pmic']
1636 i2c = scan._drivers['sandbox_i2c']
1637 self.assertEqual('DM_DEVICE_REF(pmic_at_9)', pmic_node.dev_ref)
1638 self.assertEqual(pmic, pmic_node.driver)
1639 self.assertEqual(i2c_node, pmic_node.parent)
1640 self.assertEqual(i2c, pmic_node.parent_driver)
1641
1642 # The pmic is the only child
1643 self.assertEqual(pmic_node.parent_seq, 0)
1644 self.assertEqual([pmic_node], i2c_node.child_devs)
1645
1646 # Start and end of the list should be the child_head
1647 ref = '&DM_DEVICE_REF(i2c_at_0)->child_head'
1648 self.assertEqual(
1649 {-1: ref, 0: '&DM_DEVICE_REF(pmic_at_9)->sibling_node', 1: ref},
1650 i2c_node.child_refs)
1651
1652 def test_process_nodes_bad_parent(self):
1653 # Pretend that i2c has a parent (the pmic) and delete that driver
1654 plat, scan = self.setup_process_test()
1655
1656 i2c_node = plat._fdt.GetNode('/i2c@0')
1657 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
1658 del scan._drivers['sandbox_pmic']
1659 i2c_node.parent = pmic_node
1660
1661 # Process twice, the second time to generate an exception
1662 plat.process_nodes(False)
1663 with self.assertRaises(ValueError) as exc:
1664 plat.process_nodes(True)
1665 self.assertIn(
1666 "Cannot parse/find parent driver 'sandbox_pmic' for 'sandbox_i2c",
1667 str(exc.exception))
1668
1669 def test_process_nodes_bad_node(self):
1670 plat, scan = self.setup_process_test()
1671
1672 # Now remove the pmic driver
1673 del scan._drivers['sandbox_pmic']
1674
1675 # Process twice, the second time to generate an exception
1676 plat.process_nodes(False)
1677 with self.assertRaises(ValueError) as exc:
1678 plat.process_nodes(True)
1679 self.assertIn("Cannot parse/find driver for 'sandbox_pmic",
1680 str(exc.exception))
Simon Glasseb3c2492021-02-03 06:01:01 -07001681
Simon Glass80d782c42021-02-03 06:01:10 -07001682 def test_process_nodes_bad_uclass(self):
1683 plat, scan = self.setup_process_test()
1684
1685 self.assertIn('UCLASS_I2C', scan._uclass)
1686 del scan._uclass['UCLASS_I2C']
1687 with self.assertRaises(ValueError) as exc:
1688 plat.process_nodes(True)
1689 self.assertIn("Cannot parse/find uclass 'UCLASS_I2C' for driver 'sandbox_i2c'",
1690 str(exc.exception))
1691
Simon Glasseb3c2492021-02-03 06:01:01 -07001692 def test_process_nodes_used(self):
1693 """Test processing nodes to add various info"""
1694 plat, scan = self.setup_process_test()
1695 plat.process_nodes(True)
1696
1697 pmic = scan._drivers['sandbox_pmic']
1698 self.assertTrue(pmic.used)
1699
1700 gpio = scan._drivers['sandbox_gpio']
1701 self.assertFalse(gpio.used)
Simon Glassbe88d2f2021-02-03 06:01:07 -07001702
1703 def test_alias_read(self):
1704 """Test obtaining aliases"""
1705 dtb_file = get_dtb_file('dtoc_test_inst.dts')
Simon Glass80025522022-01-29 14:14:04 -07001706 output = tools.get_output_filename('output')
Simon Glassbe88d2f2021-02-03 06:01:07 -07001707 plat = self.run_test(['struct'], dtb_file, output)
1708
1709 scan = plat._scan
1710 testfdt_node = plat._fdt.GetNode('/some-bus/test')
Simon Glass80d782c42021-02-03 06:01:10 -07001711 test0_node = plat._fdt.GetNode('/some-bus/test0')
Simon Glassbe88d2f2021-02-03 06:01:07 -07001712 self.assertIn('UCLASS_TEST_FDT', scan._uclass)
1713 uc = scan._uclass['UCLASS_TEST_FDT']
Simon Glass80d782c42021-02-03 06:01:10 -07001714 self.assertEqual({1: testfdt_node, 2: test0_node},
1715 uc.alias_num_to_node)
1716 self.assertEqual({'/some-bus/test': 1, '/some-bus/test0': 2},
1717 uc.alias_path_to_num)
Simon Glassbe88d2f2021-02-03 06:01:07 -07001718
1719 # Try adding an alias that doesn't exist
1720 self.assertFalse(scan.add_uclass_alias('fred', 3, None))
1721
1722 # Try adding an alias for a missing node
1723 self.assertIsNone(scan.add_uclass_alias('testfdt', 3, None))
1724
1725 def test_alias_read_bad(self):
1726 """Test invalid alias property name"""
1727 dtb_file = get_dtb_file('dtoc_test_alias_bad.dts')
Simon Glass80025522022-01-29 14:14:04 -07001728 output = tools.get_output_filename('output')
Simon Glassbe88d2f2021-02-03 06:01:07 -07001729 with self.assertRaises(ValueError) as exc:
1730 plat = self.run_test(['struct'], dtb_file, output)
1731 self.assertIn("Cannot decode alias 'i2c4-'", str(exc.exception))
1732
1733 def test_alias_read_bad_path(self):
1734 """Test alias pointing to a non-existent node"""
1735 # This line may produce a warning, so capture it:
1736 # Warning (alias_paths): /aliases:i2c4: aliases property is not a valid
1737 # node (/does/not/exist)
1738 dtb_file = get_dtb_file('dtoc_test_alias_bad_path.dts', True)
1739
Simon Glass80025522022-01-29 14:14:04 -07001740 output = tools.get_output_filename('output')
Simon Glassbe88d2f2021-02-03 06:01:07 -07001741 with self.assertRaises(ValueError) as exc:
1742 plat = self.run_test(['struct'], dtb_file, output)
1743 self.assertIn("Alias 'i2c4' path '/does/not/exist' not found",
1744 str(exc.exception))
1745
1746 def test_alias_read_bad_uclass(self):
1747 """Test alias for a uclass that doesn't exist"""
1748 dtb_file = get_dtb_file('dtoc_test_alias_bad_uc.dts')
Simon Glass80025522022-01-29 14:14:04 -07001749 output = tools.get_output_filename('output')
Simon Glassbe88d2f2021-02-03 06:01:07 -07001750 with test_util.capture_sys_output() as (stdout, _):
1751 plat = self.run_test(['struct'], dtb_file, output)
1752 self.assertEqual("Could not find uclass for alias 'other1'",
1753 stdout.getvalue().strip())
Simon Glassdf56e0b2021-02-03 06:01:09 -07001754
1755 def test_sequence(self):
1756 """Test assignment of sequence numnbers"""
1757 dtb_file = get_dtb_file('dtoc_test_inst.dts')
Simon Glass80025522022-01-29 14:14:04 -07001758 output = tools.get_output_filename('output')
Simon Glassdf56e0b2021-02-03 06:01:09 -07001759 plat = self.run_test(['struct'], dtb_file, output)
Simon Glass80d782c42021-02-03 06:01:10 -07001760
1761 scan = plat._scan
1762 testfdt = plat._fdt.GetNode('/some-bus/test')
1763 self.assertEqual(1, testfdt.seq)
1764 i2c = plat._fdt.GetNode('/i2c')
1765
1766 # For now this uclass is not compiled in, so no sequence is assigned
1767 self.assertEqual(4, i2c.seq)
1768 spl = plat._fdt.GetNode('/spl-test')
1769 self.assertEqual(0, spl.seq)
Simon Glassc14fd0c2021-02-03 06:01:11 -07001770
1771 def test_process_root(self):
1772 """Test assignment of sequence numnbers"""
1773 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass80025522022-01-29 14:14:04 -07001774 output = tools.get_output_filename('output')
Simon Glassc14fd0c2021-02-03 06:01:11 -07001775
1776 # Take a copy before messing with it
1777 scan = copy_scan()
1778 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
1779 plat.scan_dtb()
1780 root = plat._fdt.GetRoot()
1781
1782 plat.scan_tree(False)
1783 self.assertNotIn(root, plat._valid_nodes)
1784
1785 plat.scan_tree(True)
1786 self.assertIn(root, plat._valid_nodes)
1787 self.assertEqual('root_driver',
1788 scan.get_normalized_compat_name(root)[0])
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001789
1790 def test_simple_inst(self):
1791 """Test output from some simple nodes with instantiate enabled"""
Simon Glassc7b4b832021-02-03 06:01:20 -07001792 dtb_file = get_dtb_file('dtoc_test_inst.dts')
Simon Glass80025522022-01-29 14:14:04 -07001793 output = tools.get_output_filename('output')
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001794
1795 self.run_test(['decl'], dtb_file, output, True)
1796 with open(output) as infile:
1797 data = infile.read()
1798
1799 self._check_strings(self.decl_text_inst, data)
Simon Glassbe749002021-02-03 06:01:15 -07001800
Simon Glassc7b4b832021-02-03 06:01:20 -07001801 self.run_test(['uclass'], dtb_file, output, True)
1802 with open(output) as infile:
1803 data = infile.read()
1804
1805 self._check_strings(UCLASS_HEADER_COMMON + self.uclass_text_inst, data)
1806
Simon Glassfea2f252021-02-03 06:01:21 -07001807 self.run_test(['device'], dtb_file, output, True)
1808 with open(output) as infile:
1809 data = infile.read()
1810
1811 self._check_strings(self.device_text_inst, data)
1812
Simon Glassc7b4b832021-02-03 06:01:20 -07001813 def test_inst_no_hdr(self):
Simon Glassfea2f252021-02-03 06:01:21 -07001814 """Test dealing with a struct tsssshat has no header"""
Simon Glassc7b4b832021-02-03 06:01:20 -07001815 dtb_file = get_dtb_file('dtoc_test_inst.dts')
Simon Glass80025522022-01-29 14:14:04 -07001816 output = tools.get_output_filename('output')
Simon Glassc7b4b832021-02-03 06:01:20 -07001817
1818 # Run it once to set everything up
1819 plat = self.run_test(['decl'], dtb_file, output, True)
1820 scan = plat._scan
1821
1822 # Restart the output file and delete any record of the uclass' struct
1823 plat.setup_output(Ftype.SOURCE, output)
1824 del scan._structs['dm_test_uc_priv']
1825
1826 # Now generate the uclasses, which should provide a warning
1827 with test_util.capture_sys_output() as (stdout, _):
1828 plat.generate_uclasses()
1829 self.assertEqual(
1830 'Warning: Cannot find header file for struct dm_test_uc_priv',
1831 stdout.getvalue().strip())
Simon Glass4415dc12021-03-26 16:17:27 +13001832
1833 def test_missing_props(self):
1834 """Test detection of a parent node with no properties"""
1835 dtb_file = get_dtb_file('dtoc_test_noprops.dts', capture_stderr=True)
Simon Glass80025522022-01-29 14:14:04 -07001836 output = tools.get_output_filename('output')
Simon Glass4415dc12021-03-26 16:17:27 +13001837 with self.assertRaises(ValueError) as exc:
1838 self.run_test(['struct'], dtb_file, output)
1839 self.assertIn("Parent node '/i2c@0' has no properties - do you need",
1840 str(exc.exception))
1841
1842 def test_single_reg(self):
1843 """Test detection of a parent node with no properties"""
1844 dtb_file = get_dtb_file('dtoc_test_single_reg.dts')
Simon Glass80025522022-01-29 14:14:04 -07001845 output = tools.get_output_filename('output')
Simon Glass4415dc12021-03-26 16:17:27 +13001846 self.run_test(['struct'], dtb_file, output)
Simon Glass9ebd5512021-06-27 17:51:10 -06001847
1848 def test_missing_parent(self):
1849 """Test detection of a parent node with no properties"""
1850 dtb_file = get_dtb_file('dtoc_test_noparent.dts', capture_stderr=True)
Simon Glass80025522022-01-29 14:14:04 -07001851 output = tools.get_output_filename('output')
Simon Glass9ebd5512021-06-27 17:51:10 -06001852 with self.assertRaises(ValueError) as exc:
1853 self.run_test(['device'], dtb_file, output, instantiate=True)
1854 self.assertIn("Node '/i2c@0/spl-test/pmic@9' requires parent node "
1855 "'/i2c@0/spl-test' but it is not in the valid list",
1856 str(exc.exception))