blob: c4a0889aebe73f7cef6783517bea9fba4e63fd55 [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 <dm.h>
67#include <dt-structs.h>
68'''
69
Simon Glassc7b4b832021-02-03 06:01:20 -070070UCLASS_HEADER_COMMON = '''/*
71 * DO NOT MODIFY
72 *
73 * Declares the uclass instances (struct uclass).
74 * This was generated by dtoc from a .dtb (device tree binary) file.
75 */
76'''
77
Simon Glass768ff0a2021-02-03 06:00:51 -070078# Scanner saved from a previous run of the tests (to speed things up)
79saved_scan = None
80
Simon Glassa60cabd2020-12-28 20:34:47 -070081# This is a test so is allowed to access private things in the module it is
82# testing
83# pylint: disable=W0212
Simon Glass3bce93d2018-07-06 10:27:37 -060084
85def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glass9d2eb922017-06-18 22:09:06 -060086 """Compile a .dts file to a .dtb
87
88 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -070089 dts_fname (str): Filename of .dts file in the current directory
90 capture_stderr (bool): True to capture and discard stderr output
Simon Glass9d2eb922017-06-18 22:09:06 -060091
92 Returns:
Simon Glassa60cabd2020-12-28 20:34:47 -070093 str: Filename of compiled file in output directory
Simon Glass9d2eb922017-06-18 22:09:06 -060094 """
Simon Glassbd310df2023-02-23 18:18:16 -070095 return fdt_util.EnsureCompiled(str(TEST_DATA_DIR / dts_fname),
Simon Glass3bce93d2018-07-06 10:27:37 -060096 capture_stderr=capture_stderr)
Simon Glass9d2eb922017-06-18 22:09:06 -060097
Simon Glass768ff0a2021-02-03 06:00:51 -070098
99def setup():
100 global saved_scan
101
102 # Disable warnings so that calls to get_normalized_compat_name() will not
103 # output things.
Simon Glass695077d2021-03-26 16:17:25 +1300104 saved_scan = src_scan.Scanner(None, False)
Simon Glass768ff0a2021-02-03 06:00:51 -0700105 saved_scan.scan_drivers()
106
107def copy_scan():
108 """Get a copy of saved_scan so that each test can start clean"""
109 return copy.deepcopy(saved_scan)
110
Simon Glass9d2eb922017-06-18 22:09:06 -0600111
112class TestDtoc(unittest.TestCase):
113 """Tests for dtoc"""
114 @classmethod
115 def setUpClass(cls):
Simon Glass80025522022-01-29 14:14:04 -0700116 tools.prepare_output_dir(None)
Simon Glass7f5e2262020-07-07 21:32:06 -0600117 cls.maxDiff = None
Simon Glass9d2eb922017-06-18 22:09:06 -0600118
119 @classmethod
120 def tearDownClass(cls):
Simon Glass80025522022-01-29 14:14:04 -0700121 tools.finalise_output_dir()
Simon Glass9d2eb922017-06-18 22:09:06 -0600122
Simon Glassa60cabd2020-12-28 20:34:47 -0700123 @staticmethod
124 def _write_python_string(fname, data):
Simon Glassc47c2b32018-07-06 10:27:25 -0600125 """Write a string with tabs expanded as done in this Python file
126
127 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -0700128 fname (str): Filename to write to
129 data (str): Raw string to convert
Simon Glassc47c2b32018-07-06 10:27:25 -0600130 """
131 data = data.replace('\t', '\\t')
Simon Glassa60cabd2020-12-28 20:34:47 -0700132 with open(fname, 'w') as fout:
133 fout.write(data)
Simon Glassc47c2b32018-07-06 10:27:25 -0600134
Simon Glassa60cabd2020-12-28 20:34:47 -0700135 def _check_strings(self, expected, actual):
Simon Glassc47c2b32018-07-06 10:27:25 -0600136 """Check that a string matches its expected value
137
138 If the strings do not match, they are written to the /tmp directory in
139 the same Python format as is used here in the test. This allows for
140 easy comparison and update of the tests.
141
142 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -0700143 expected (str): Expected string
144 actual (str): Actual string
Simon Glassc47c2b32018-07-06 10:27:25 -0600145 """
146 if expected != actual:
Simon Glassa60cabd2020-12-28 20:34:47 -0700147 self._write_python_string('/tmp/binman.expected', expected)
148 self._write_python_string('/tmp/binman.actual', actual)
Simon Glass61b88e52019-05-17 22:00:31 -0600149 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glassa60cabd2020-12-28 20:34:47 -0700150 self.assertEqual(expected, actual)
Simon Glassc47c2b32018-07-06 10:27:25 -0600151
Simon Glassa60cabd2020-12-28 20:34:47 -0700152 @staticmethod
Simon Glass3809ad92021-02-03 06:01:12 -0700153 def run_test(args, dtb_file, output, instantiate=False):
Simon Glassa60cabd2020-12-28 20:34:47 -0700154 """Run a test using dtoc
Walter Lozanoa324e412020-06-25 01:10:08 -0300155
Simon Glassa60cabd2020-12-28 20:34:47 -0700156 Args:
157 args (list of str): List of arguments for dtoc
158 dtb_file (str): Filename of .dtb file
159 output (str): Filename of output file
Simon Glassbe88d2f2021-02-03 06:01:07 -0700160
161 Returns:
162 DtbPlatdata object
Simon Glassa60cabd2020-12-28 20:34:47 -0700163 """
Simon Glass80d782c42021-02-03 06:01:10 -0700164 # Make a copy of the 'scan' object, since it includes uclasses and
165 # drivers, which get updated during execution.
Simon Glass3809ad92021-02-03 06:01:12 -0700166 return dtb_platdata.run_steps(
167 args, dtb_file, False, output, [], None, instantiate,
168 warning_disabled=True, scan=copy_scan())
Walter Lozanoa324e412020-06-25 01:10:08 -0300169
Simon Glass9d2eb922017-06-18 22:09:06 -0600170 def test_name(self):
171 """Test conversion of device tree names to C identifiers"""
172 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
173 self.assertEqual('vendor_clock_frequency',
174 conv_name_to_c('vendor,clock-frequency'))
175 self.assertEqual('rockchip_rk3399_sdhci_5_1',
176 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
177
178 def test_tab_to(self):
179 """Test operation of tab_to() function"""
180 self.assertEqual('fred ', tab_to(0, 'fred'))
181 self.assertEqual('fred\t', tab_to(1, 'fred'))
182 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
183 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
184 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
185 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
186
187 def test_get_value(self):
188 """Test operation of get_value() function"""
189 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700190 get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600191 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700192 get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600193 self.assertEqual('0x0',
Simon Glassc9a032c2020-11-08 20:36:17 -0700194 get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
195 self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
196 self.assertEqual('true', get_value(fdt.Type.BOOL, None))
Simon Glass9d2eb922017-06-18 22:09:06 -0600197
198 def test_get_compat_name(self):
199 """Test operation of get_compat_name() function"""
200 Prop = collections.namedtuple('Prop', ['value'])
201 Node = collections.namedtuple('Node', ['props'])
202
203 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
204 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300205 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600206 get_compat_name(node))
207
208 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
209 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300210 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600211 get_compat_name(node))
212
213 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
214 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300215 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
Simon Glassa60cabd2020-12-28 20:34:47 -0700216 'arasan_sdhci_5_1', 'third']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600217 get_compat_name(node))
218
219 def test_empty_file(self):
220 """Test output from a device tree file with no nodes"""
221 dtb_file = get_dtb_file('dtoc_test_empty.dts')
Simon Glass80025522022-01-29 14:14:04 -0700222 output = tools.get_output_filename('output')
Simon Glass768ff0a2021-02-03 06:00:51 -0700223
224 # Run this one without saved_scan to complete test coverage
Simon Glass3809ad92021-02-03 06:01:12 -0700225 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [], None,
226 False)
Simon Glass9d2eb922017-06-18 22:09:06 -0600227 with open(output) as infile:
228 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700229 self.assertEqual(HEADER.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600230
Walter Lozanoa324e412020-06-25 01:10:08 -0300231 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600232 with open(output) as infile:
233 lines = infile.read().splitlines()
Simon Glass2500de22020-12-28 20:35:05 -0700234 self.assertEqual(C_HEADER.splitlines() + [''], lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600235
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700236 decl_text = DECL_HEADER + '''
237#include <dm/device-internal.h>
238#include <dm/uclass-internal.h>
239
240/* driver declarations - these allow DM_DRIVER_GET() to be used */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300241extern U_BOOT_DRIVER(sandbox_i2c);
242extern U_BOOT_DRIVER(sandbox_pmic);
243extern U_BOOT_DRIVER(sandbox_spl_test);
244extern U_BOOT_DRIVER(sandbox_spl_test);
245extern U_BOOT_DRIVER(sandbox_spl_test);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700246
247/* uclass driver declarations - needed for DM_UCLASS_DRIVER_REF() */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300248extern UCLASS_DRIVER(i2c);
249extern UCLASS_DRIVER(misc);
250extern UCLASS_DRIVER(pmic);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700251'''
252 decl_text_inst = DECL_HEADER + '''
253#include <dm/device-internal.h>
254#include <dm/uclass-internal.h>
255
256/* driver declarations - these allow DM_DRIVER_GET() to be used */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300257extern U_BOOT_DRIVER(sandbox_i2c);
258extern U_BOOT_DRIVER(root_driver);
259extern U_BOOT_DRIVER(denx_u_boot_test_bus);
260extern U_BOOT_DRIVER(sandbox_spl_test);
261extern U_BOOT_DRIVER(sandbox_spl_test);
262extern U_BOOT_DRIVER(denx_u_boot_fdt_test);
263extern U_BOOT_DRIVER(denx_u_boot_fdt_test);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700264
265/* device declarations - these allow DM_DEVICE_REF() to be used */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300266extern DM_DEVICE_INST(i2c);
267extern DM_DEVICE_INST(root);
268extern DM_DEVICE_INST(some_bus);
269extern DM_DEVICE_INST(spl_test);
270extern DM_DEVICE_INST(spl_test3);
271extern DM_DEVICE_INST(test);
272extern DM_DEVICE_INST(test0);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700273
274/* uclass driver declarations - needed for DM_UCLASS_DRIVER_REF() */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300275extern UCLASS_DRIVER(i2c);
276extern UCLASS_DRIVER(misc);
277extern UCLASS_DRIVER(root);
278extern UCLASS_DRIVER(testbus);
279extern UCLASS_DRIVER(testfdt);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700280
281/* uclass declarations - needed for DM_UCLASS_REF() */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300282extern DM_UCLASS_INST(i2c);
283extern DM_UCLASS_INST(misc);
284extern DM_UCLASS_INST(root);
285extern DM_UCLASS_INST(testbus);
286extern DM_UCLASS_INST(testfdt);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700287'''
Simon Glass07ee48e2020-12-28 20:34:49 -0700288 struct_text = HEADER + '''
Simon Glass32a8d222021-02-03 06:00:57 -0700289struct dtd_sandbox_i2c {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600290};
Simon Glass32a8d222021-02-03 06:00:57 -0700291struct dtd_sandbox_pmic {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600292\tbool\t\tlow_power;
Simon Glass94ba59e2021-03-26 16:17:26 +1300293\tfdt32_t\t\treg[1];
Simon Glass90e5f0a2017-08-29 14:15:51 -0600294};
Simon Glass9d2eb922017-06-18 22:09:06 -0600295struct dtd_sandbox_spl_test {
Simon Glass7f5e2262020-07-07 21:32:06 -0600296\tconst char * acpi_name;
Simon Glass9d2eb922017-06-18 22:09:06 -0600297\tbool\t\tboolval;
298\tunsigned char\tbytearray[3];
299\tunsigned char\tbyteval;
Simon Glass3b55e3f2021-11-23 11:03:39 -0700300\tfdt32_t\t\tint64val[2];
Simon Glassa7d66982021-07-28 19:23:10 -0600301\tfdt32_t\t\tintarray[3];
Simon Glass9d2eb922017-06-18 22:09:06 -0600302\tfdt32_t\t\tintval;
303\tunsigned char\tlongbytearray[9];
Simon Glass43118322021-07-28 19:23:11 -0600304\tfdt32_t\t\tmaybe_empty_int[1];
Simon Glass9c526332018-07-06 10:27:28 -0600305\tunsigned char\tnotstring[5];
Simon Glass9d2eb922017-06-18 22:09:06 -0600306\tconst char *\tstringarray[3];
307\tconst char *\tstringval;
308};
Simon Glass07ee48e2020-12-28 20:34:49 -0700309'''
Simon Glass07ee48e2020-12-28 20:34:49 -0700310 platdata_text = C_HEADER + '''
Simon Glassbe749002021-02-03 06:01:15 -0700311/*
Simon Glasse2119082021-02-03 06:01:19 -0700312 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
313 *
314 * idx driver_info driver
315 * --- -------------------- --------------------
316 * 0: i2c_at_0 sandbox_i2c
317 * 1: pmic_at_9 sandbox_pmic
318 * 2: spl_test sandbox_spl_test
319 * 3: spl_test2 sandbox_spl_test
320 * 4: spl_test3 sandbox_spl_test
321 * --- -------------------- --------------------
322 */
323
324/*
Simon Glassbe749002021-02-03 06:01:15 -0700325 * Node /i2c@0 index 0
326 * driver sandbox_i2c parent None
327 */
Simon Glass32a8d222021-02-03 06:00:57 -0700328static struct dtd_sandbox_i2c dtv_i2c_at_0 = {
Simon Glass192f8132020-10-03 11:31:25 -0600329};
Simon Glass1d8364a2020-12-28 20:34:54 -0700330U_BOOT_DRVINFO(i2c_at_0) = {
Simon Glass32a8d222021-02-03 06:00:57 -0700331\t.name\t\t= "sandbox_i2c",
Simon Glasse2119082021-02-03 06:01:19 -0700332\t.plat\t\t= &dtv_i2c_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700333\t.plat_size\t= sizeof(dtv_i2c_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600334\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600335};
336
Simon Glassbe749002021-02-03 06:01:15 -0700337/*
338 * Node /i2c@0/pmic@9 index 1
339 * driver sandbox_pmic parent sandbox_i2c
340 */
Simon Glass32a8d222021-02-03 06:00:57 -0700341static struct dtd_sandbox_pmic dtv_pmic_at_9 = {
Simon Glass192f8132020-10-03 11:31:25 -0600342\t.low_power\t\t= true,
Simon Glass94ba59e2021-03-26 16:17:26 +1300343\t.reg\t\t\t= {0x9},
Simon Glass192f8132020-10-03 11:31:25 -0600344};
Simon Glass1d8364a2020-12-28 20:34:54 -0700345U_BOOT_DRVINFO(pmic_at_9) = {
Simon Glass32a8d222021-02-03 06:00:57 -0700346\t.name\t\t= "sandbox_pmic",
Simon Glasse2119082021-02-03 06:01:19 -0700347\t.plat\t\t= &dtv_pmic_at_9,
Simon Glass39edb952020-12-03 16:55:19 -0700348\t.plat_size\t= sizeof(dtv_pmic_at_9),
Simon Glass36b15e22020-10-03 11:31:35 -0600349\t.parent_idx\t= 0,
Simon Glass192f8132020-10-03 11:31:25 -0600350};
351
Simon Glassbe749002021-02-03 06:01:15 -0700352/*
353 * Node /spl-test index 2
354 * driver sandbox_spl_test parent None
355 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300356static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glassc82de562019-05-17 22:00:32 -0600357\t.boolval\t\t= true,
Simon Glass9d2eb922017-06-18 22:09:06 -0600358\t.bytearray\t\t= {0x6, 0x0, 0x0},
359\t.byteval\t\t= 0x5,
Simon Glass3b55e3f2021-11-23 11:03:39 -0700360\t.int64val\t\t= {0x12345678, 0x9abcdef0},
Simon Glassa7d66982021-07-28 19:23:10 -0600361\t.intarray\t\t= {0x2, 0x3, 0x4},
Simon Glass9d2eb922017-06-18 22:09:06 -0600362\t.intval\t\t\t= 0x1,
Simon Glass131e0b02017-08-29 14:15:49 -0600363\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
364\t\t0x11},
Simon Glass43118322021-07-28 19:23:11 -0600365\t.maybe_empty_int\t= {0x0},
Simon Glassc82de562019-05-17 22:00:32 -0600366\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600367\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glassc82de562019-05-17 22:00:32 -0600368\t.stringval\t\t= "message",
Simon Glass9d2eb922017-06-18 22:09:06 -0600369};
Simon Glass1d8364a2020-12-28 20:34:54 -0700370U_BOOT_DRVINFO(spl_test) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600371\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -0700372\t.plat\t\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700373\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600374\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600375};
376
Simon Glassbe749002021-02-03 06:01:15 -0700377/*
378 * Node /spl-test2 index 3
379 * driver sandbox_spl_test parent None
380 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300381static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass7f5e2262020-07-07 21:32:06 -0600382\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glass9d2eb922017-06-18 22:09:06 -0600383\t.bytearray\t\t= {0x1, 0x23, 0x34},
384\t.byteval\t\t= 0x8,
Simon Glassa7d66982021-07-28 19:23:10 -0600385\t.intarray\t\t= {0x5, 0x0, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600386\t.intval\t\t\t= 0x3,
Simon Glass8034e4d2020-10-03 11:31:27 -0600387\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
Simon Glass131e0b02017-08-29 14:15:49 -0600388\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600389\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glassc82de562019-05-17 22:00:32 -0600390\t.stringval\t\t= "message2",
Simon Glass9d2eb922017-06-18 22:09:06 -0600391};
Simon Glass1d8364a2020-12-28 20:34:54 -0700392U_BOOT_DRVINFO(spl_test2) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600393\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -0700394\t.plat\t\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700395\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600396\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600397};
398
Simon Glassbe749002021-02-03 06:01:15 -0700399/*
400 * Node /spl-test3 index 4
401 * driver sandbox_spl_test parent None
402 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300403static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glass8034e4d2020-10-03 11:31:27 -0600404\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
405\t\t0x0},
Simon Glass43118322021-07-28 19:23:11 -0600406\t.maybe_empty_int\t= {0x1},
Simon Glass9d2eb922017-06-18 22:09:06 -0600407\t.stringarray\t\t= {"one", "", ""},
408};
Simon Glass1d8364a2020-12-28 20:34:54 -0700409U_BOOT_DRVINFO(spl_test3) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600410\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -0700411\t.plat\t\t= &dtv_spl_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700412\t.plat_size\t= sizeof(dtv_spl_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600413\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600414};
415
Simon Glass2500de22020-12-28 20:35:05 -0700416'''
Simon Glassc7b4b832021-02-03 06:01:20 -0700417 uclass_text_inst = '''
418
Simon Glassc7b4b832021-02-03 06:01:20 -0700419#include <dm.h>
420#include <dt-structs.h>
421
422/*
Simon Glassfea2f252021-02-03 06:01:21 -0700423 * uclass declarations, ordered by 'struct uclass' linker_list idx:
424 * 0: i2c
425 * 1: misc
426 * 2: root
427 * 3: testbus
428 * 4: testfdt
Simon Glassc7b4b832021-02-03 06:01:20 -0700429 *
Simon Glassfea2f252021-02-03 06:01:21 -0700430 * Sequence numbers allocated in each uclass:
Simon Glassc7b4b832021-02-03 06:01:20 -0700431 * i2c: UCLASS_I2C
432 * 4: /i2c
433 * misc: UCLASS_MISC
434 * 0: /spl-test
435 * 1: /spl-test3
436 * root: UCLASS_ROOT
437 * 0: /
438 * testbus: UCLASS_TEST_BUS
439 * 2: /some-bus
440 * testfdt: UCLASS_TEST_FDT
441 * 1: /some-bus/test
442 * 2: /some-bus/test0
443 */
444
445struct list_head uclass_head = {
446 .prev = &DM_UCLASS_REF(testfdt)->sibling_node,
447 .next = &DM_UCLASS_REF(i2c)->sibling_node,
448};
449
450DM_UCLASS_INST(i2c) = {
451 .uc_drv = DM_UCLASS_DRIVER_REF(i2c),
452 .sibling_node = {
453 .prev = &uclass_head,
454 .next = &DM_UCLASS_REF(misc)->sibling_node,
455 },
456 .dev_head = {
457 .prev = &DM_DEVICE_REF(i2c)->uclass_node,
458 .next = &DM_DEVICE_REF(i2c)->uclass_node,
459 },
460};
461
462DM_UCLASS_INST(misc) = {
463 .uc_drv = DM_UCLASS_DRIVER_REF(misc),
464 .sibling_node = {
465 .prev = &DM_UCLASS_REF(i2c)->sibling_node,
466 .next = &DM_UCLASS_REF(root)->sibling_node,
467 },
468 .dev_head = {
469 .prev = &DM_DEVICE_REF(spl_test3)->uclass_node,
470 .next = &DM_DEVICE_REF(spl_test)->uclass_node,
471 },
472};
473
474DM_UCLASS_INST(root) = {
475 .uc_drv = DM_UCLASS_DRIVER_REF(root),
476 .sibling_node = {
477 .prev = &DM_UCLASS_REF(misc)->sibling_node,
478 .next = &DM_UCLASS_REF(testbus)->sibling_node,
479 },
480 .dev_head = {
481 .prev = &DM_DEVICE_REF(root)->uclass_node,
482 .next = &DM_DEVICE_REF(root)->uclass_node,
483 },
484};
485
486DM_UCLASS_INST(testbus) = {
487 .uc_drv = DM_UCLASS_DRIVER_REF(testbus),
488 .sibling_node = {
489 .prev = &DM_UCLASS_REF(root)->sibling_node,
490 .next = &DM_UCLASS_REF(testfdt)->sibling_node,
491 },
492 .dev_head = {
493 .prev = &DM_DEVICE_REF(some_bus)->uclass_node,
494 .next = &DM_DEVICE_REF(some_bus)->uclass_node,
495 },
496};
497
498#include <dm/test.h>
499u8 _testfdt_priv_[sizeof(struct dm_test_uc_priv)]
500 __attribute__ ((section (".priv_data")));
501DM_UCLASS_INST(testfdt) = {
502 .priv_ = _testfdt_priv_,
503 .uc_drv = DM_UCLASS_DRIVER_REF(testfdt),
504 .sibling_node = {
505 .prev = &DM_UCLASS_REF(testbus)->sibling_node,
506 .next = &uclass_head,
507 },
508 .dev_head = {
509 .prev = &DM_DEVICE_REF(test0)->uclass_node,
510 .next = &DM_DEVICE_REF(test)->uclass_node,
511 },
512};
513
Simon Glassfea2f252021-02-03 06:01:21 -0700514'''
515 device_text_inst = '''/*
516 * DO NOT MODIFY
517 *
518 * Declares the DM_DEVICE_INST() records.
519 * This was generated by dtoc from a .dtb (device tree binary) file.
520 */
521
Simon Glassfea2f252021-02-03 06:01:21 -0700522#include <dm.h>
523#include <dt-structs.h>
524
525/*
526 * udevice declarations, ordered by 'struct udevice' linker_list position:
527 *
528 * idx udevice driver
529 * --- -------------------- --------------------
530 * 0: i2c sandbox_i2c
531 * 1: root root_driver
532 * 2: some_bus denx_u_boot_test_bus
533 * 3: spl_test sandbox_spl_test
534 * 4: spl_test3 sandbox_spl_test
535 * 5: test denx_u_boot_fdt_test
536 * 6: test0 denx_u_boot_fdt_test
537 * --- -------------------- --------------------
538 */
539
540/*
541 * Node /i2c index 0
542 * driver sandbox_i2c parent root_driver
543*/
544static struct dtd_sandbox_i2c dtv_i2c = {
545\t.intval\t\t\t= 0x3,
546};
547
548#include <asm/i2c.h>
549u8 _sandbox_i2c_priv_i2c[sizeof(struct sandbox_i2c_priv)]
550\t__attribute__ ((section (".priv_data")));
551#include <i2c.h>
552u8 _sandbox_i2c_uc_priv_i2c[sizeof(struct dm_i2c_bus)]
553\t__attribute__ ((section (".priv_data")));
554
555DM_DEVICE_INST(i2c) = {
556\t.driver\t\t= DM_DRIVER_REF(sandbox_i2c),
557\t.name\t\t= "sandbox_i2c",
558\t.plat_\t\t= &dtv_i2c,
559\t.priv_\t\t= _sandbox_i2c_priv_i2c,
560\t.uclass\t\t= DM_UCLASS_REF(i2c),
561\t.uclass_priv_ = _sandbox_i2c_uc_priv_i2c,
562\t.uclass_node\t= {
563\t\t.prev = &DM_UCLASS_REF(i2c)->dev_head,
564\t\t.next = &DM_UCLASS_REF(i2c)->dev_head,
565\t},
566\t.child_head\t= {
567\t\t.prev = &DM_DEVICE_REF(i2c)->child_head,
568\t\t.next = &DM_DEVICE_REF(i2c)->child_head,
569\t},
570\t.sibling_node\t= {
571\t\t.prev = &DM_DEVICE_REF(root)->child_head,
572\t\t.next = &DM_DEVICE_REF(some_bus)->sibling_node,
573\t},
574\t.seq_ = 4,
575};
576
577/*
578 * Node / index 1
579 * driver root_driver parent None
580*/
581static struct dtd_root_driver dtv_root = {
582};
583
584DM_DEVICE_INST(root) = {
585\t.driver\t\t= DM_DRIVER_REF(root_driver),
586\t.name\t\t= "root_driver",
587\t.plat_\t\t= &dtv_root,
588\t.uclass\t\t= DM_UCLASS_REF(root),
589\t.uclass_node\t= {
590\t\t.prev = &DM_UCLASS_REF(root)->dev_head,
591\t\t.next = &DM_UCLASS_REF(root)->dev_head,
592\t},
593\t.child_head\t= {
594\t\t.prev = &DM_DEVICE_REF(spl_test3)->sibling_node,
595\t\t.next = &DM_DEVICE_REF(i2c)->sibling_node,
596\t},
597\t.seq_ = 0,
598};
599
600/*
601 * Node /some-bus index 2
602 * driver denx_u_boot_test_bus parent root_driver
603*/
604
605#include <dm/test.h>
606struct dm_test_pdata __attribute__ ((section (".priv_data")))
607\t_denx_u_boot_test_bus_plat_some_bus = {
608\t.dtplat = {
609\t\t.ping_add\t\t= 0x4,
610\t\t.ping_expect\t\t= 0x4,
611\t\t.reg\t\t\t= {0x3, 0x1},
612\t},
613};
614#include <dm/test.h>
615u8 _denx_u_boot_test_bus_priv_some_bus[sizeof(struct dm_test_priv)]
616\t__attribute__ ((section (".priv_data")));
617#include <dm/test.h>
Simon Glasse3304472022-05-08 04:39:23 -0600618u8 _denx_u_boot_test_bus_ucplat_some_bus[sizeof(struct dm_test_uclass_plat)]
Simon Glassfea2f252021-02-03 06:01:21 -0700619\t__attribute__ ((section (".priv_data")));
Simon Glassd1f12cf2022-05-08 04:39:24 -0600620#include <dm/test.h>
621u8 _denx_u_boot_test_bus_uc_priv_some_bus[sizeof(struct dm_test_uclass_priv)]
622 __attribute__ ((section (".priv_data")));
Simon Glassfea2f252021-02-03 06:01:21 -0700623#include <test.h>
624
625DM_DEVICE_INST(some_bus) = {
626\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_test_bus),
627\t.name\t\t= "denx_u_boot_test_bus",
628\t.plat_\t\t= &_denx_u_boot_test_bus_plat_some_bus,
629\t.uclass_plat_\t= _denx_u_boot_test_bus_ucplat_some_bus,
630\t.driver_data\t= DM_TEST_TYPE_FIRST,
631\t.priv_\t\t= _denx_u_boot_test_bus_priv_some_bus,
632\t.uclass\t\t= DM_UCLASS_REF(testbus),
Simon Glassd1f12cf2022-05-08 04:39:24 -0600633\t.uclass_priv_ = _denx_u_boot_test_bus_uc_priv_some_bus,
Simon Glassfea2f252021-02-03 06:01:21 -0700634\t.uclass_node\t= {
635\t\t.prev = &DM_UCLASS_REF(testbus)->dev_head,
636\t\t.next = &DM_UCLASS_REF(testbus)->dev_head,
637\t},
638\t.child_head\t= {
639\t\t.prev = &DM_DEVICE_REF(test0)->sibling_node,
640\t\t.next = &DM_DEVICE_REF(test)->sibling_node,
641\t},
642\t.sibling_node\t= {
643\t\t.prev = &DM_DEVICE_REF(i2c)->sibling_node,
644\t\t.next = &DM_DEVICE_REF(spl_test)->sibling_node,
645\t},
646\t.seq_ = 2,
647};
648
649/*
650 * Node /spl-test index 3
651 * driver sandbox_spl_test parent root_driver
652*/
653static struct dtd_sandbox_spl_test dtv_spl_test = {
654\t.boolval\t\t= true,
655\t.intval\t\t\t= 0x1,
656};
657
658DM_DEVICE_INST(spl_test) = {
659\t.driver\t\t= DM_DRIVER_REF(sandbox_spl_test),
660\t.name\t\t= "sandbox_spl_test",
661\t.plat_\t\t= &dtv_spl_test,
662\t.uclass\t\t= DM_UCLASS_REF(misc),
663\t.uclass_node\t= {
664\t\t.prev = &DM_UCLASS_REF(misc)->dev_head,
665\t\t.next = &DM_DEVICE_REF(spl_test3)->uclass_node,
666\t},
667\t.child_head\t= {
668\t\t.prev = &DM_DEVICE_REF(spl_test)->child_head,
669\t\t.next = &DM_DEVICE_REF(spl_test)->child_head,
670\t},
671\t.sibling_node\t= {
672\t\t.prev = &DM_DEVICE_REF(some_bus)->sibling_node,
673\t\t.next = &DM_DEVICE_REF(spl_test3)->sibling_node,
674\t},
675\t.seq_ = 0,
676};
677
678/*
679 * Node /spl-test3 index 4
680 * driver sandbox_spl_test parent root_driver
681*/
682static struct dtd_sandbox_spl_test dtv_spl_test3 = {
683\t.longbytearray\t\t= {0x90a0b0c, 0xd0e0f10},
684\t.stringarray\t\t= "one",
685};
686
687DM_DEVICE_INST(spl_test3) = {
688\t.driver\t\t= DM_DRIVER_REF(sandbox_spl_test),
689\t.name\t\t= "sandbox_spl_test",
690\t.plat_\t\t= &dtv_spl_test3,
691\t.uclass\t\t= DM_UCLASS_REF(misc),
692\t.uclass_node\t= {
693\t\t.prev = &DM_DEVICE_REF(spl_test)->uclass_node,
694\t\t.next = &DM_UCLASS_REF(misc)->dev_head,
695\t},
696\t.child_head\t= {
697\t\t.prev = &DM_DEVICE_REF(spl_test3)->child_head,
698\t\t.next = &DM_DEVICE_REF(spl_test3)->child_head,
699\t},
700\t.sibling_node\t= {
701\t\t.prev = &DM_DEVICE_REF(spl_test)->sibling_node,
702\t\t.next = &DM_DEVICE_REF(root)->child_head,
703\t},
704\t.seq_ = 1,
705};
706
707/*
708 * Node /some-bus/test index 5
709 * driver denx_u_boot_fdt_test parent denx_u_boot_test_bus
710*/
711
712#include <dm/test.h>
713struct dm_test_pdata __attribute__ ((section (".priv_data")))
714\t_denx_u_boot_fdt_test_plat_test = {
715\t.dtplat = {
716\t\t.ping_add\t\t= 0x5,
717\t\t.ping_expect\t\t= 0x5,
Simon Glass94ba59e2021-03-26 16:17:26 +1300718\t\t.reg\t\t\t= {0x5},
Simon Glassfea2f252021-02-03 06:01:21 -0700719\t},
720};
721#include <dm/test.h>
722u8 _denx_u_boot_fdt_test_priv_test[sizeof(struct dm_test_priv)]
723\t__attribute__ ((section (".priv_data")));
724#include <dm/test.h>
725u8 _denx_u_boot_fdt_test_parent_plat_test[sizeof(struct dm_test_parent_plat)]
726\t__attribute__ ((section (".priv_data")));
727#include <dm/test.h>
728u8 _denx_u_boot_fdt_test_parent_priv_test[sizeof(struct dm_test_parent_data)]
729\t__attribute__ ((section (".priv_data")));
730
731DM_DEVICE_INST(test) = {
732\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_fdt_test),
733\t.name\t\t= "denx_u_boot_fdt_test",
734\t.plat_\t\t= &_denx_u_boot_fdt_test_plat_test,
735\t.parent_plat_\t= _denx_u_boot_fdt_test_parent_plat_test,
736\t.driver_data\t= DM_TEST_TYPE_FIRST,
737\t.parent\t\t= DM_DEVICE_REF(some_bus),
738\t.priv_\t\t= _denx_u_boot_fdt_test_priv_test,
739\t.uclass\t\t= DM_UCLASS_REF(testfdt),
740\t.parent_priv_\t= _denx_u_boot_fdt_test_parent_priv_test,
741\t.uclass_node\t= {
742\t\t.prev = &DM_UCLASS_REF(testfdt)->dev_head,
743\t\t.next = &DM_DEVICE_REF(test0)->uclass_node,
744\t},
745\t.child_head\t= {
746\t\t.prev = &DM_DEVICE_REF(test)->child_head,
747\t\t.next = &DM_DEVICE_REF(test)->child_head,
748\t},
749\t.sibling_node\t= {
750\t\t.prev = &DM_DEVICE_REF(some_bus)->child_head,
751\t\t.next = &DM_DEVICE_REF(test0)->sibling_node,
752\t},
753\t.seq_ = 1,
754};
755
756/*
757 * Node /some-bus/test0 index 6
758 * driver denx_u_boot_fdt_test parent denx_u_boot_test_bus
759*/
760
761#include <dm/test.h>
762struct dm_test_pdata __attribute__ ((section (".priv_data")))
763\t_denx_u_boot_fdt_test_plat_test0 = {
764\t.dtplat = {
765\t},
766};
767#include <dm/test.h>
768u8 _denx_u_boot_fdt_test_priv_test0[sizeof(struct dm_test_priv)]
769\t__attribute__ ((section (".priv_data")));
770#include <dm/test.h>
771u8 _denx_u_boot_fdt_test_parent_plat_test0[sizeof(struct dm_test_parent_plat)]
772\t__attribute__ ((section (".priv_data")));
773#include <dm/test.h>
774u8 _denx_u_boot_fdt_test_parent_priv_test0[sizeof(struct dm_test_parent_data)]
775\t__attribute__ ((section (".priv_data")));
776
777DM_DEVICE_INST(test0) = {
778\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_fdt_test),
779\t.name\t\t= "denx_u_boot_fdt_test",
780\t.plat_\t\t= &_denx_u_boot_fdt_test_plat_test0,
781\t.parent_plat_\t= _denx_u_boot_fdt_test_parent_plat_test0,
782\t.driver_data\t= DM_TEST_TYPE_SECOND,
783\t.parent\t\t= DM_DEVICE_REF(some_bus),
784\t.priv_\t\t= _denx_u_boot_fdt_test_priv_test0,
785\t.uclass\t\t= DM_UCLASS_REF(testfdt),
786\t.parent_priv_\t= _denx_u_boot_fdt_test_parent_priv_test0,
787\t.uclass_node\t= {
788\t\t.prev = &DM_DEVICE_REF(test)->uclass_node,
789\t\t.next = &DM_UCLASS_REF(testfdt)->dev_head,
790\t},
791\t.child_head\t= {
792\t\t.prev = &DM_DEVICE_REF(test0)->child_head,
793\t\t.next = &DM_DEVICE_REF(test0)->child_head,
794\t},
795\t.sibling_node\t= {
796\t\t.prev = &DM_DEVICE_REF(test)->sibling_node,
797\t\t.next = &DM_DEVICE_REF(some_bus)->child_head,
798\t},
799\t.seq_ = 2,
800};
801
802'''
Simon Glass07ee48e2020-12-28 20:34:49 -0700803
804 def test_simple(self):
805 """Test output from some simple nodes with various types of data"""
806 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass80025522022-01-29 14:14:04 -0700807 output = tools.get_output_filename('output')
Simon Glass07ee48e2020-12-28 20:34:49 -0700808 self.run_test(['struct'], dtb_file, output)
809 with open(output) as infile:
810 data = infile.read()
811
812 self._check_strings(self.struct_text, data)
813
814 self.run_test(['platdata'], dtb_file, output)
815 with open(output) as infile:
816 data = infile.read()
817
818 self._check_strings(self.platdata_text, data)
Simon Glass9d2eb922017-06-18 22:09:06 -0600819
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700820 self.run_test(['decl'], dtb_file, output)
821 with open(output) as infile:
822 data = infile.read()
823
824 self._check_strings(self.decl_text, data)
825
Simon Glass4e8e8462020-12-28 20:34:52 -0700826 # Try the 'all' command
827 self.run_test(['all'], dtb_file, output)
Simon Glass80025522022-01-29 14:14:04 -0700828 data = tools.read_file(output, binary=False)
Simon Glassfea2f252021-02-03 06:01:21 -0700829 self._check_strings(
Simon Glass705b84b2021-04-27 08:19:48 +1200830 self.decl_text + self.platdata_text + self.struct_text, data)
Simon Glass4e8e8462020-12-28 20:34:52 -0700831
Walter Lozanoe675d962020-07-03 08:07:17 -0300832 def test_driver_alias(self):
833 """Test output from a device tree file with a driver alias"""
834 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
Simon Glass80025522022-01-29 14:14:04 -0700835 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300836 self.run_test(['struct'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300837 with open(output) as infile:
838 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700839 self._check_strings(HEADER + '''
Walter Lozanoe675d962020-07-03 08:07:17 -0300840struct dtd_sandbox_gpio {
841\tconst char *\tgpio_bank_name;
842\tbool\t\tgpio_controller;
843\tfdt32_t\t\tsandbox_gpio_count;
844};
Walter Lozanoe675d962020-07-03 08:07:17 -0300845''', data)
846
Walter Lozanoa324e412020-06-25 01:10:08 -0300847 self.run_test(['platdata'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300848 with open(output) as infile:
849 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700850 self._check_strings(C_HEADER + '''
Simon Glassbe749002021-02-03 06:01:15 -0700851/*
Simon Glasse2119082021-02-03 06:01:19 -0700852 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
853 *
854 * idx driver_info driver
855 * --- -------------------- --------------------
856 * 0: gpios_at_0 sandbox_gpio
857 * --- -------------------- --------------------
858 */
859
860/*
Simon Glassbe749002021-02-03 06:01:15 -0700861 * Node /gpios@0 index 0
862 * driver sandbox_gpio parent None
863 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300864static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300865\t.gpio_bank_name\t\t= "a",
866\t.gpio_controller\t= true,
867\t.sandbox_gpio_count\t= 0x14,
868};
Simon Glass1d8364a2020-12-28 20:34:54 -0700869U_BOOT_DRVINFO(gpios_at_0) = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300870\t.name\t\t= "sandbox_gpio",
Simon Glasse2119082021-02-03 06:01:19 -0700871\t.plat\t\t= &dtv_gpios_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700872\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600873\t.parent_idx\t= -1,
Walter Lozanoe675d962020-07-03 08:07:17 -0300874};
875
876''', data)
877
Walter Lozanoa324e412020-06-25 01:10:08 -0300878 def test_invalid_driver(self):
879 """Test output from a device tree file with an invalid driver"""
880 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
Simon Glass80025522022-01-29 14:14:04 -0700881 output = tools.get_output_filename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700882 with test_util.capture_sys_output() as _:
Simon Glass3809ad92021-02-03 06:01:12 -0700883 dtb_platdata.run_steps(
884 ['struct'], dtb_file, False, output, [], None, False,
885 scan=copy_scan())
Walter Lozanoa324e412020-06-25 01:10:08 -0300886 with open(output) as infile:
887 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700888 self._check_strings(HEADER + '''
Walter Lozanoa324e412020-06-25 01:10:08 -0300889struct dtd_invalid {
890};
891''', data)
892
Simon Glassa60cabd2020-12-28 20:34:47 -0700893 with test_util.capture_sys_output() as _:
Simon Glass3809ad92021-02-03 06:01:12 -0700894 dtb_platdata.run_steps(
895 ['platdata'], dtb_file, False, output, [], None, False,
896 scan=copy_scan())
Walter Lozanoa324e412020-06-25 01:10:08 -0300897 with open(output) as infile:
898 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700899 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -0700900/*
901 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
902 *
903 * idx driver_info driver
904 * --- -------------------- --------------------
905 * 0: spl_test invalid
906 * --- -------------------- --------------------
907 */
908
Simon Glass192f8132020-10-03 11:31:25 -0600909/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300910static struct dtd_invalid dtv_spl_test = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300911};
Simon Glass1d8364a2020-12-28 20:34:54 -0700912U_BOOT_DRVINFO(spl_test) = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300913\t.name\t\t= "invalid",
Simon Glasse2119082021-02-03 06:01:19 -0700914\t.plat\t\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700915\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600916\t.parent_idx\t= -1,
Walter Lozanoa324e412020-06-25 01:10:08 -0300917};
918
919''', data)
920
Simon Glass9d2eb922017-06-18 22:09:06 -0600921 def test_phandle(self):
922 """Test output from a node containing a phandle reference"""
923 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
Simon Glass80025522022-01-29 14:14:04 -0700924 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300925 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600926 with open(output) as infile:
927 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700928 self._check_strings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600929struct dtd_source {
Simon Glass3deeb472017-08-29 14:15:59 -0600930\tstruct phandle_2_arg clocks[4];
Simon Glasse8cea0e2023-01-11 16:10:18 -0700931\tunsigned char phandle_name_offset[13];
Simon Glass9d2eb922017-06-18 22:09:06 -0600932};
933struct dtd_target {
934\tfdt32_t\t\tintval;
935};
936''', data)
937
Walter Lozanoa324e412020-06-25 01:10:08 -0300938 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600939 with open(output) as infile:
940 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700941 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -0700942/*
943 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
944 *
945 * idx driver_info driver
946 * --- -------------------- --------------------
947 * 0: phandle2_target target
948 * 1: phandle3_target target
949 * 2: phandle_source source
950 * 3: phandle_source2 source
951 * 4: phandle_target target
952 * --- -------------------- --------------------
953 */
954
Simon Glass192f8132020-10-03 11:31:25 -0600955/* Node /phandle2-target index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300956static struct dtd_target dtv_phandle2_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600957\t.intval\t\t\t= 0x1,
958};
Simon Glass1d8364a2020-12-28 20:34:54 -0700959U_BOOT_DRVINFO(phandle2_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600960\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -0700961\t.plat\t\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700962\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600963\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600964};
965
Simon Glass192f8132020-10-03 11:31:25 -0600966/* Node /phandle3-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300967static struct dtd_target dtv_phandle3_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600968\t.intval\t\t\t= 0x2,
969};
Simon Glass1d8364a2020-12-28 20:34:54 -0700970U_BOOT_DRVINFO(phandle3_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600971\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -0700972\t.plat\t\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700973\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600974\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600975};
976
Simon Glass192f8132020-10-03 11:31:25 -0600977/* Node /phandle-source index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300978static struct dtd_source dtv_phandle_source = {
Simon Glassd0cd0752017-08-29 14:15:57 -0600979\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600980\t\t\t{4, {}},
981\t\t\t{0, {11}},
982\t\t\t{1, {12, 13}},
983\t\t\t{4, {}},},
Simon Glasse8cea0e2023-01-11 16:10:18 -0700984\t.phandle_name_offset = {0x0, 0x0, 0x0, 0x3, 0x66, 0x72, 0x65, 0x64,
985\t\t0x0, 0x0, 0x0, 0x0, 0x7b},
Simon Glass9d2eb922017-06-18 22:09:06 -0600986};
Simon Glass1d8364a2020-12-28 20:34:54 -0700987U_BOOT_DRVINFO(phandle_source) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600988\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -0700989\t.plat\t\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700990\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600991\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600992};
993
Simon Glass192f8132020-10-03 11:31:25 -0600994/* Node /phandle-source2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300995static struct dtd_source dtv_phandle_source2 = {
Simon Glass609e2b12018-07-06 10:27:31 -0600996\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600997\t\t\t{4, {}},},
Simon Glass609e2b12018-07-06 10:27:31 -0600998};
Simon Glass1d8364a2020-12-28 20:34:54 -0700999U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass609e2b12018-07-06 10:27:31 -06001000\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -07001001\t.plat\t\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -07001002\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -06001003\t.parent_idx\t= -1,
Simon Glass609e2b12018-07-06 10:27:31 -06001004};
1005
Simon Glass16382ce2020-12-28 20:35:04 -07001006/* Node /phandle-target index 4 */
1007static struct dtd_target dtv_phandle_target = {
1008\t.intval\t\t\t= 0x0,
1009};
1010U_BOOT_DRVINFO(phandle_target) = {
1011\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001012\t.plat\t\t= &dtv_phandle_target,
Simon Glass16382ce2020-12-28 20:35:04 -07001013\t.plat_size\t= sizeof(dtv_phandle_target),
1014\t.parent_idx\t= -1,
1015};
1016
Simon Glass9d2eb922017-06-18 22:09:06 -06001017''', data)
1018
Simon Glass961c1ce2018-07-06 10:27:35 -06001019 def test_phandle_single(self):
1020 """Test output from a node containing a phandle reference"""
1021 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
Simon Glass80025522022-01-29 14:14:04 -07001022 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001023 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001024 with open(output) as infile:
1025 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001026 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -06001027struct dtd_source {
1028\tstruct phandle_0_arg clocks[1];
1029};
1030struct dtd_target {
1031\tfdt32_t\t\tintval;
1032};
1033''', data)
1034
1035 def test_phandle_reorder(self):
1036 """Test that phandle targets are generated before their references"""
1037 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
Simon Glass80025522022-01-29 14:14:04 -07001038 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001039 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001040 with open(output) as infile:
1041 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001042 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001043/*
1044 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1045 *
1046 * idx driver_info driver
1047 * --- -------------------- --------------------
1048 * 0: phandle_source2 source
1049 * 1: phandle_target target
1050 * --- -------------------- --------------------
1051 */
1052
Simon Glass192f8132020-10-03 11:31:25 -06001053/* Node /phandle-source2 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001054static struct dtd_source dtv_phandle_source2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001055\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -06001056\t\t\t{1, {}},},
Simon Glass961c1ce2018-07-06 10:27:35 -06001057};
Simon Glass1d8364a2020-12-28 20:34:54 -07001058U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001059\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -07001060\t.plat\t\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -07001061\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -06001062\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -06001063};
1064
Simon Glass16382ce2020-12-28 20:35:04 -07001065/* Node /phandle-target index 1 */
1066static struct dtd_target dtv_phandle_target = {
1067};
1068U_BOOT_DRVINFO(phandle_target) = {
1069\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001070\t.plat\t\t= &dtv_phandle_target,
Simon Glass16382ce2020-12-28 20:35:04 -07001071\t.plat_size\t= sizeof(dtv_phandle_target),
1072\t.parent_idx\t= -1,
1073};
1074
Simon Glass961c1ce2018-07-06 10:27:35 -06001075''', data)
1076
Walter Lozano5541fc02020-06-25 01:10:17 -03001077 def test_phandle_cd_gpio(self):
1078 """Test that phandle targets are generated when unsing cd-gpios"""
1079 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
Simon Glass80025522022-01-29 14:14:04 -07001080 output = tools.get_output_filename('output')
Simon Glass3809ad92021-02-03 06:01:12 -07001081 dtb_platdata.run_steps(
1082 ['platdata'], dtb_file, False, output, [], None, False,
1083 warning_disabled=True, scan=copy_scan())
Walter Lozano5541fc02020-06-25 01:10:17 -03001084 with open(output) as infile:
1085 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001086 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001087/*
1088 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1089 *
1090 * idx driver_info driver
1091 * --- -------------------- --------------------
1092 * 0: phandle2_target target
1093 * 1: phandle3_target target
1094 * 2: phandle_source source
1095 * 3: phandle_source2 source
1096 * 4: phandle_target target
1097 * --- -------------------- --------------------
1098 */
1099
Simon Glass192f8132020-10-03 11:31:25 -06001100/* Node /phandle2-target index 0 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001101static struct dtd_target dtv_phandle2_target = {
1102\t.intval\t\t\t= 0x1,
1103};
Simon Glass1d8364a2020-12-28 20:34:54 -07001104U_BOOT_DRVINFO(phandle2_target) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001105\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001106\t.plat\t\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -07001107\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -06001108\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -03001109};
1110
Simon Glass192f8132020-10-03 11:31:25 -06001111/* Node /phandle3-target index 1 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001112static struct dtd_target dtv_phandle3_target = {
1113\t.intval\t\t\t= 0x2,
1114};
Simon Glass1d8364a2020-12-28 20:34:54 -07001115U_BOOT_DRVINFO(phandle3_target) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001116\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001117\t.plat\t\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -07001118\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -06001119\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -03001120};
1121
Simon Glass192f8132020-10-03 11:31:25 -06001122/* Node /phandle-source index 2 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001123static struct dtd_source dtv_phandle_source = {
1124\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -06001125\t\t\t{4, {}},
1126\t\t\t{0, {11}},
1127\t\t\t{1, {12, 13}},
1128\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -03001129};
Simon Glass1d8364a2020-12-28 20:34:54 -07001130U_BOOT_DRVINFO(phandle_source) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001131\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -07001132\t.plat\t\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -07001133\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -06001134\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -03001135};
1136
Simon Glass192f8132020-10-03 11:31:25 -06001137/* Node /phandle-source2 index 3 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001138static struct dtd_source dtv_phandle_source2 = {
1139\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -06001140\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -03001141};
Simon Glass1d8364a2020-12-28 20:34:54 -07001142U_BOOT_DRVINFO(phandle_source2) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001143\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -07001144\t.plat\t\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -07001145\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -06001146\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -03001147};
1148
Simon Glass16382ce2020-12-28 20:35:04 -07001149/* Node /phandle-target index 4 */
1150static struct dtd_target dtv_phandle_target = {
1151\t.intval\t\t\t= 0x0,
1152};
1153U_BOOT_DRVINFO(phandle_target) = {
1154\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001155\t.plat\t\t= &dtv_phandle_target,
Simon Glass16382ce2020-12-28 20:35:04 -07001156\t.plat_size\t= sizeof(dtv_phandle_target),
1157\t.parent_idx\t= -1,
1158};
1159
Walter Lozano5541fc02020-06-25 01:10:17 -03001160''', data)
1161
Simon Glass961c1ce2018-07-06 10:27:35 -06001162 def test_phandle_bad(self):
1163 """Test a node containing an invalid phandle fails"""
Simon Glass04150022018-10-01 21:12:43 -06001164 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
1165 capture_stderr=True)
Simon Glass80025522022-01-29 14:14:04 -07001166 output = tools.get_output_filename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001167 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001168 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001169 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
Simon Glassa60cabd2020-12-28 20:34:47 -07001170 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001171
1172 def test_phandle_bad2(self):
1173 """Test a phandle target missing its #*-cells property"""
Simon Glass04150022018-10-01 21:12:43 -06001174 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
1175 capture_stderr=True)
Simon Glass80025522022-01-29 14:14:04 -07001176 output = tools.get_output_filename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001177 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001178 self.run_test(['struct'], dtb_file, output)
Walter Lozano179f0b62020-06-25 01:10:16 -03001179 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glassa60cabd2020-12-28 20:34:47 -07001180 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001181
Simon Glass1b1fe412017-08-29 14:15:50 -06001182 def test_addresses64(self):
1183 """Test output from a node with a 'reg' property with na=2, ns=2"""
1184 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
Simon Glass80025522022-01-29 14:14:04 -07001185 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001186 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001187 with open(output) as infile:
1188 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001189 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001190struct dtd_test1 {
1191\tfdt64_t\t\treg[2];
1192};
1193struct dtd_test2 {
1194\tfdt64_t\t\treg[2];
1195};
1196struct dtd_test3 {
1197\tfdt64_t\t\treg[4];
1198};
1199''', data)
1200
Walter Lozanoa324e412020-06-25 01:10:08 -03001201 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001202 with open(output) as infile:
1203 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001204 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001205/*
1206 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1207 *
1208 * idx driver_info driver
1209 * --- -------------------- --------------------
1210 * 0: test1 test1
1211 * 1: test2 test2
1212 * 2: test3 test3
1213 * --- -------------------- --------------------
1214 */
1215
Simon Glass192f8132020-10-03 11:31:25 -06001216/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001217static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001218\t.reg\t\t\t= {0x1234, 0x5678},
1219};
Simon Glass1d8364a2020-12-28 20:34:54 -07001220U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001221\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001222\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001223\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001224\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001225};
1226
Simon Glass192f8132020-10-03 11:31:25 -06001227/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001228static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001229\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
1230};
Simon Glass1d8364a2020-12-28 20:34:54 -07001231U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001232\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001233\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001234\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001235\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001236};
1237
Simon Glass192f8132020-10-03 11:31:25 -06001238/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001239static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001240\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
1241};
Simon Glass1d8364a2020-12-28 20:34:54 -07001242U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001243\t.name\t\t= "test3",
Simon Glasse2119082021-02-03 06:01:19 -07001244\t.plat\t\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -07001245\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -06001246\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001247};
1248
Simon Glass2500de22020-12-28 20:35:05 -07001249''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -06001250
1251 def test_addresses32(self):
1252 """Test output from a node with a 'reg' property with na=1, ns=1"""
1253 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
Simon Glass80025522022-01-29 14:14:04 -07001254 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001255 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001256 with open(output) as infile:
1257 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001258 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001259struct dtd_test1 {
1260\tfdt32_t\t\treg[2];
1261};
1262struct dtd_test2 {
1263\tfdt32_t\t\treg[4];
1264};
1265''', data)
1266
Walter Lozanoa324e412020-06-25 01:10:08 -03001267 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001268 with open(output) as infile:
1269 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001270 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001271/*
1272 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1273 *
1274 * idx driver_info driver
1275 * --- -------------------- --------------------
1276 * 0: test1 test1
1277 * 1: test2 test2
1278 * --- -------------------- --------------------
1279 */
1280
Simon Glass192f8132020-10-03 11:31:25 -06001281/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001282static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001283\t.reg\t\t\t= {0x1234, 0x5678},
1284};
Simon Glass1d8364a2020-12-28 20:34:54 -07001285U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001286\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001287\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001288\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001289\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001290};
1291
Simon Glass192f8132020-10-03 11:31:25 -06001292/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001293static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001294\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
1295};
Simon Glass1d8364a2020-12-28 20:34:54 -07001296U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001297\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001298\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001299\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001300\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001301};
1302
Simon Glass2500de22020-12-28 20:35:05 -07001303''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -06001304
1305 def test_addresses64_32(self):
1306 """Test output from a node with a 'reg' property with na=2, ns=1"""
1307 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
Simon Glass80025522022-01-29 14:14:04 -07001308 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001309 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001310 with open(output) as infile:
1311 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001312 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001313struct dtd_test1 {
1314\tfdt64_t\t\treg[2];
1315};
1316struct dtd_test2 {
1317\tfdt64_t\t\treg[2];
1318};
1319struct dtd_test3 {
1320\tfdt64_t\t\treg[4];
1321};
1322''', data)
1323
Walter Lozanoa324e412020-06-25 01:10:08 -03001324 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001325 with open(output) as infile:
1326 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001327 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001328/*
1329 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1330 *
1331 * idx driver_info driver
1332 * --- -------------------- --------------------
1333 * 0: test1 test1
1334 * 1: test2 test2
1335 * 2: test3 test3
1336 * --- -------------------- --------------------
1337 */
1338
Simon Glass192f8132020-10-03 11:31:25 -06001339/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001340static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001341\t.reg\t\t\t= {0x123400000000, 0x5678},
1342};
Simon Glass1d8364a2020-12-28 20:34:54 -07001343U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001344\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001345\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001346\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001347\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001348};
1349
Simon Glass192f8132020-10-03 11:31:25 -06001350/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001351static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001352\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
1353};
Simon Glass1d8364a2020-12-28 20:34:54 -07001354U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001355\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001356\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001357\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001358\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001359};
1360
Simon Glass192f8132020-10-03 11:31:25 -06001361/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001362static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001363\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
1364};
Simon Glass1d8364a2020-12-28 20:34:54 -07001365U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001366\t.name\t\t= "test3",
Simon Glasse2119082021-02-03 06:01:19 -07001367\t.plat\t\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -07001368\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -06001369\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001370};
1371
Simon Glass2500de22020-12-28 20:35:05 -07001372''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -06001373
1374 def test_addresses32_64(self):
1375 """Test output from a node with a 'reg' property with na=1, ns=2"""
1376 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
Simon Glass80025522022-01-29 14:14:04 -07001377 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001378 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001379 with open(output) as infile:
1380 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001381 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001382struct dtd_test1 {
1383\tfdt64_t\t\treg[2];
1384};
1385struct dtd_test2 {
1386\tfdt64_t\t\treg[2];
1387};
1388struct dtd_test3 {
1389\tfdt64_t\t\treg[4];
1390};
1391''', data)
1392
Walter Lozanoa324e412020-06-25 01:10:08 -03001393 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001394 with open(output) as infile:
1395 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001396 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001397/*
1398 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1399 *
1400 * idx driver_info driver
1401 * --- -------------------- --------------------
1402 * 0: test1 test1
1403 * 1: test2 test2
1404 * 2: test3 test3
1405 * --- -------------------- --------------------
1406 */
1407
Simon Glass192f8132020-10-03 11:31:25 -06001408/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001409static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001410\t.reg\t\t\t= {0x1234, 0x567800000000},
1411};
Simon Glass1d8364a2020-12-28 20:34:54 -07001412U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001413\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001414\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001415\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001416\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001417};
1418
Simon Glass192f8132020-10-03 11:31:25 -06001419/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001420static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001421\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
1422};
Simon Glass1d8364a2020-12-28 20:34:54 -07001423U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001424\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001425\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001426\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001427\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001428};
1429
Simon Glass192f8132020-10-03 11:31:25 -06001430/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001431static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001432\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
1433};
Simon Glass1d8364a2020-12-28 20:34:54 -07001434U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001435\t.name\t\t= "test3",
Simon Glasse2119082021-02-03 06:01:19 -07001436\t.plat\t\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -07001437\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -06001438\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001439};
1440
Simon Glass2500de22020-12-28 20:35:05 -07001441''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -06001442
1443 def test_bad_reg(self):
1444 """Test that a reg property with an invalid type generates an error"""
Simon Glass3bce93d2018-07-06 10:27:37 -06001445 # Capture stderr since dtc will emit warnings for this file
1446 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass80025522022-01-29 14:14:04 -07001447 output = tools.get_output_filename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001448 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001449 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001450 self.assertIn("Node 'spl-test' reg property is not an int",
Simon Glassa60cabd2020-12-28 20:34:47 -07001451 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001452
1453 def test_bad_reg2(self):
1454 """Test that a reg property with an invalid cell count is detected"""
Simon Glass3bce93d2018-07-06 10:27:37 -06001455 # Capture stderr since dtc will emit warnings for this file
1456 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass80025522022-01-29 14:14:04 -07001457 output = tools.get_output_filename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001458 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001459 self.run_test(['struct'], dtb_file, output)
Simon Glassa60cabd2020-12-28 20:34:47 -07001460 self.assertIn(
Simon Glass4415dc12021-03-26 16:17:27 +13001461 "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 -07001462 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001463
1464 def test_add_prop(self):
1465 """Test that a subequent node can add a new property to a struct"""
1466 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
Simon Glass80025522022-01-29 14:14:04 -07001467 output = tools.get_output_filename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001468 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001469 with open(output) as infile:
1470 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001471 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -06001472struct dtd_sandbox_spl_test {
1473\tfdt32_t\t\tintarray;
1474\tfdt32_t\t\tintval;
1475};
1476''', data)
1477
Walter Lozanoa324e412020-06-25 01:10:08 -03001478 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001479 with open(output) as infile:
1480 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001481 self._check_strings(C_HEADER + '''
Simon Glassbe749002021-02-03 06:01:15 -07001482/*
Simon Glasse2119082021-02-03 06:01:19 -07001483 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1484 *
1485 * idx driver_info driver
1486 * --- -------------------- --------------------
1487 * 0: spl_test sandbox_spl_test
1488 * 1: spl_test2 sandbox_spl_test
1489 * --- -------------------- --------------------
1490 */
1491
1492/*
Simon Glassbe749002021-02-03 06:01:15 -07001493 * Node /spl-test index 0
1494 * driver sandbox_spl_test parent None
1495 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001496static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001497\t.intval\t\t\t= 0x1,
1498};
Simon Glass1d8364a2020-12-28 20:34:54 -07001499U_BOOT_DRVINFO(spl_test) = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001500\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -07001501\t.plat\t\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -07001502\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -06001503\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -06001504};
1505
Simon Glassbe749002021-02-03 06:01:15 -07001506/*
1507 * Node /spl-test2 index 1
1508 * driver sandbox_spl_test parent None
1509 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001510static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001511\t.intarray\t\t= 0x5,
1512};
Simon Glass1d8364a2020-12-28 20:34:54 -07001513U_BOOT_DRVINFO(spl_test2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001514\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -07001515\t.plat\t\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001516\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001517\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -06001518};
1519
Simon Glass2500de22020-12-28 20:35:05 -07001520''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -06001521
Simon Glassa60cabd2020-12-28 20:34:47 -07001522 def test_stdout(self):
Simon Glass961c1ce2018-07-06 10:27:35 -06001523 """Test output to stdout"""
1524 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass07ee48e2020-12-28 20:34:49 -07001525 with test_util.capture_sys_output() as (stdout, _):
Simon Glass6ca0c7a2020-12-28 20:34:48 -07001526 self.run_test(['struct'], dtb_file, None)
Simon Glass07ee48e2020-12-28 20:34:49 -07001527 self._check_strings(self.struct_text, stdout.getvalue())
Simon Glass961c1ce2018-07-06 10:27:35 -06001528
Simon Glassc3a310a82020-12-28 20:34:51 -07001529 def test_multi_to_file(self):
1530 """Test output of multiple pieces to a single file"""
1531 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass80025522022-01-29 14:14:04 -07001532 output = tools.get_output_filename('output')
Simon Glass4e8e8462020-12-28 20:34:52 -07001533 self.run_test(['all'], dtb_file, output)
Simon Glass80025522022-01-29 14:14:04 -07001534 data = tools.read_file(output, binary=False)
Simon Glassfea2f252021-02-03 06:01:21 -07001535 self._check_strings(
Simon Glass705b84b2021-04-27 08:19:48 +12001536 self.decl_text + self.platdata_text + self.struct_text, data)
Simon Glassc3a310a82020-12-28 20:34:51 -07001537
Simon Glassa60cabd2020-12-28 20:34:47 -07001538 def test_no_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -06001539 """Test running dtoc without a command"""
Simon Glassa60cabd2020-12-28 20:34:47 -07001540 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001541 self.run_test([], '', '')
Simon Glass961c1ce2018-07-06 10:27:35 -06001542 self.assertIn("Please specify a command: struct, platdata",
Simon Glassa60cabd2020-12-28 20:34:47 -07001543 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001544
Simon Glassa60cabd2020-12-28 20:34:47 -07001545 def test_bad_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -06001546 """Test running dtoc with an invalid command"""
1547 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass80025522022-01-29 14:14:04 -07001548 output = tools.get_output_filename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001549 with self.assertRaises(ValueError) as exc:
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001550 self.run_test(['invalid-cmd'], dtb_file, output)
1551 self.assertIn(
Simon Glassdb2b9ca2021-03-25 06:40:51 +13001552 "Unknown command 'invalid-cmd': (use: decl, platdata, struct)",
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001553 str(exc.exception))
Walter Lozanod82062b2020-07-28 19:06:23 -03001554
Simon Glass4e8e8462020-12-28 20:34:52 -07001555 def test_output_conflict(self):
1556 """Test a conflict between and output dirs and output file"""
1557 with self.assertRaises(ValueError) as exc:
Simon Glass3809ad92021-02-03 06:01:12 -07001558 dtb_platdata.run_steps(
1559 ['all'], None, False, 'out', ['cdir'], None, False,
1560 warning_disabled=True, scan=copy_scan())
Simon Glass4e8e8462020-12-28 20:34:52 -07001561 self.assertIn("Must specify either output or output_dirs, not both",
1562 str(exc.exception))
1563
Simon Glass705b84b2021-04-27 08:19:48 +12001564 def check_output_dirs(self, instantiate):
Simon Glass4e8e8462020-12-28 20:34:52 -07001565 # Remove the directory so that files from other tests are not there
Simon Glass80025522022-01-29 14:14:04 -07001566 tools._remove_output_dir()
1567 tools.prepare_output_dir(None)
Simon Glass4e8e8462020-12-28 20:34:52 -07001568
1569 # This should create the .dts and .dtb in the output directory
1570 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass80025522022-01-29 14:14:04 -07001571 outdir = tools.get_output_dir()
Simon Glass4e8e8462020-12-28 20:34:52 -07001572 fnames = glob.glob(outdir + '/*')
1573 self.assertEqual(2, len(fnames))
1574
Simon Glass3809ad92021-02-03 06:01:12 -07001575 dtb_platdata.run_steps(
Simon Glass705b84b2021-04-27 08:19:48 +12001576 ['all'], dtb_file, False, None, [outdir], None, instantiate,
Simon Glass3809ad92021-02-03 06:01:12 -07001577 warning_disabled=True, scan=copy_scan())
Simon Glass4e8e8462020-12-28 20:34:52 -07001578 fnames = glob.glob(outdir + '/*')
Simon Glass705b84b2021-04-27 08:19:48 +12001579 return fnames
1580
1581 def test_output_dirs(self):
1582 """Test outputting files to a directory"""
1583 fnames = self.check_output_dirs(False)
1584 self.assertEqual(5, len(fnames))
Simon Glass4e8e8462020-12-28 20:34:52 -07001585
1586 leafs = set(os.path.basename(fname) for fname in fnames)
1587 self.assertEqual(
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001588 {'dt-structs-gen.h', 'source.dts', 'dt-plat.c', 'source.dtb',
Simon Glass705b84b2021-04-27 08:19:48 +12001589 'dt-decl.h'},
1590 leafs)
1591
1592 def test_output_dirs_inst(self):
1593 """Test outputting files to a directory with instantiation"""
1594 fnames = self.check_output_dirs(True)
1595 self.assertEqual(6, len(fnames))
1596
1597 leafs = set(os.path.basename(fname) for fname in fnames)
1598 self.assertEqual(
1599 {'dt-structs-gen.h', 'source.dts', 'source.dtb',
Simon Glassfea2f252021-02-03 06:01:21 -07001600 'dt-uclass.c', 'dt-decl.h', 'dt-device.c'},
Simon Glass4e8e8462020-12-28 20:34:52 -07001601 leafs)
Simon Glass047a4802021-02-03 06:01:00 -07001602
1603 def setup_process_test(self):
1604 """Set up a test of process_nodes()
1605
1606 This uses saved_scan but returns a deep copy of it, so it is safe to
1607 modify it in these tests
1608
1609 Returns:
1610 tuple:
1611 DtbPlatdata: object to test
1612 Scanner: scanner to use
1613 """
1614 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass80025522022-01-29 14:14:04 -07001615 output = tools.get_output_filename('output')
Simon Glass047a4802021-02-03 06:01:00 -07001616
1617 # Take a copy before messing with it
Simon Glassc14fd0c2021-02-03 06:01:11 -07001618 scan = copy_scan()
Simon Glass047a4802021-02-03 06:01:00 -07001619 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
1620 plat.scan_dtb()
Simon Glassc14fd0c2021-02-03 06:01:11 -07001621 plat.scan_tree(False)
Simon Glass047a4802021-02-03 06:01:00 -07001622 plat.prepare_nodes()
1623 return plat, scan
1624
1625 def test_process_nodes(self):
1626 """Test processing nodes to add various info"""
1627 plat, scan = self.setup_process_test()
1628 plat.process_nodes(True)
1629
1630 i2c_node = plat._fdt.GetNode('/i2c@0')
1631 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
1632 pmic = scan._drivers['sandbox_pmic']
1633 i2c = scan._drivers['sandbox_i2c']
1634 self.assertEqual('DM_DEVICE_REF(pmic_at_9)', pmic_node.dev_ref)
1635 self.assertEqual(pmic, pmic_node.driver)
1636 self.assertEqual(i2c_node, pmic_node.parent)
1637 self.assertEqual(i2c, pmic_node.parent_driver)
1638
1639 # The pmic is the only child
1640 self.assertEqual(pmic_node.parent_seq, 0)
1641 self.assertEqual([pmic_node], i2c_node.child_devs)
1642
1643 # Start and end of the list should be the child_head
1644 ref = '&DM_DEVICE_REF(i2c_at_0)->child_head'
1645 self.assertEqual(
1646 {-1: ref, 0: '&DM_DEVICE_REF(pmic_at_9)->sibling_node', 1: ref},
1647 i2c_node.child_refs)
1648
1649 def test_process_nodes_bad_parent(self):
1650 # Pretend that i2c has a parent (the pmic) and delete that driver
1651 plat, scan = self.setup_process_test()
1652
1653 i2c_node = plat._fdt.GetNode('/i2c@0')
1654 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
1655 del scan._drivers['sandbox_pmic']
1656 i2c_node.parent = pmic_node
1657
1658 # Process twice, the second time to generate an exception
1659 plat.process_nodes(False)
1660 with self.assertRaises(ValueError) as exc:
1661 plat.process_nodes(True)
1662 self.assertIn(
1663 "Cannot parse/find parent driver 'sandbox_pmic' for 'sandbox_i2c",
1664 str(exc.exception))
1665
1666 def test_process_nodes_bad_node(self):
1667 plat, scan = self.setup_process_test()
1668
1669 # Now remove the pmic driver
1670 del scan._drivers['sandbox_pmic']
1671
1672 # Process twice, the second time to generate an exception
1673 plat.process_nodes(False)
1674 with self.assertRaises(ValueError) as exc:
1675 plat.process_nodes(True)
1676 self.assertIn("Cannot parse/find driver for 'sandbox_pmic",
1677 str(exc.exception))
Simon Glasseb3c2492021-02-03 06:01:01 -07001678
Simon Glass80d782c42021-02-03 06:01:10 -07001679 def test_process_nodes_bad_uclass(self):
1680 plat, scan = self.setup_process_test()
1681
1682 self.assertIn('UCLASS_I2C', scan._uclass)
1683 del scan._uclass['UCLASS_I2C']
1684 with self.assertRaises(ValueError) as exc:
1685 plat.process_nodes(True)
1686 self.assertIn("Cannot parse/find uclass 'UCLASS_I2C' for driver 'sandbox_i2c'",
1687 str(exc.exception))
1688
Simon Glasseb3c2492021-02-03 06:01:01 -07001689 def test_process_nodes_used(self):
1690 """Test processing nodes to add various info"""
1691 plat, scan = self.setup_process_test()
1692 plat.process_nodes(True)
1693
1694 pmic = scan._drivers['sandbox_pmic']
1695 self.assertTrue(pmic.used)
1696
1697 gpio = scan._drivers['sandbox_gpio']
1698 self.assertFalse(gpio.used)
Simon Glassbe88d2f2021-02-03 06:01:07 -07001699
1700 def test_alias_read(self):
1701 """Test obtaining aliases"""
1702 dtb_file = get_dtb_file('dtoc_test_inst.dts')
Simon Glass80025522022-01-29 14:14:04 -07001703 output = tools.get_output_filename('output')
Simon Glassbe88d2f2021-02-03 06:01:07 -07001704 plat = self.run_test(['struct'], dtb_file, output)
1705
1706 scan = plat._scan
1707 testfdt_node = plat._fdt.GetNode('/some-bus/test')
Simon Glass80d782c42021-02-03 06:01:10 -07001708 test0_node = plat._fdt.GetNode('/some-bus/test0')
Simon Glassbe88d2f2021-02-03 06:01:07 -07001709 self.assertIn('UCLASS_TEST_FDT', scan._uclass)
1710 uc = scan._uclass['UCLASS_TEST_FDT']
Simon Glass80d782c42021-02-03 06:01:10 -07001711 self.assertEqual({1: testfdt_node, 2: test0_node},
1712 uc.alias_num_to_node)
1713 self.assertEqual({'/some-bus/test': 1, '/some-bus/test0': 2},
1714 uc.alias_path_to_num)
Simon Glassbe88d2f2021-02-03 06:01:07 -07001715
1716 # Try adding an alias that doesn't exist
1717 self.assertFalse(scan.add_uclass_alias('fred', 3, None))
1718
1719 # Try adding an alias for a missing node
1720 self.assertIsNone(scan.add_uclass_alias('testfdt', 3, None))
1721
1722 def test_alias_read_bad(self):
1723 """Test invalid alias property name"""
1724 dtb_file = get_dtb_file('dtoc_test_alias_bad.dts')
Simon Glass80025522022-01-29 14:14:04 -07001725 output = tools.get_output_filename('output')
Simon Glassbe88d2f2021-02-03 06:01:07 -07001726 with self.assertRaises(ValueError) as exc:
1727 plat = self.run_test(['struct'], dtb_file, output)
1728 self.assertIn("Cannot decode alias 'i2c4-'", str(exc.exception))
1729
1730 def test_alias_read_bad_path(self):
1731 """Test alias pointing to a non-existent node"""
1732 # This line may produce a warning, so capture it:
1733 # Warning (alias_paths): /aliases:i2c4: aliases property is not a valid
1734 # node (/does/not/exist)
1735 dtb_file = get_dtb_file('dtoc_test_alias_bad_path.dts', True)
1736
Simon Glass80025522022-01-29 14:14:04 -07001737 output = tools.get_output_filename('output')
Simon Glassbe88d2f2021-02-03 06:01:07 -07001738 with self.assertRaises(ValueError) as exc:
1739 plat = self.run_test(['struct'], dtb_file, output)
1740 self.assertIn("Alias 'i2c4' path '/does/not/exist' not found",
1741 str(exc.exception))
1742
1743 def test_alias_read_bad_uclass(self):
1744 """Test alias for a uclass that doesn't exist"""
1745 dtb_file = get_dtb_file('dtoc_test_alias_bad_uc.dts')
Simon Glass80025522022-01-29 14:14:04 -07001746 output = tools.get_output_filename('output')
Simon Glassbe88d2f2021-02-03 06:01:07 -07001747 with test_util.capture_sys_output() as (stdout, _):
1748 plat = self.run_test(['struct'], dtb_file, output)
1749 self.assertEqual("Could not find uclass for alias 'other1'",
1750 stdout.getvalue().strip())
Simon Glassdf56e0b2021-02-03 06:01:09 -07001751
1752 def test_sequence(self):
1753 """Test assignment of sequence numnbers"""
1754 dtb_file = get_dtb_file('dtoc_test_inst.dts')
Simon Glass80025522022-01-29 14:14:04 -07001755 output = tools.get_output_filename('output')
Simon Glassdf56e0b2021-02-03 06:01:09 -07001756 plat = self.run_test(['struct'], dtb_file, output)
Simon Glass80d782c42021-02-03 06:01:10 -07001757
1758 scan = plat._scan
1759 testfdt = plat._fdt.GetNode('/some-bus/test')
1760 self.assertEqual(1, testfdt.seq)
1761 i2c = plat._fdt.GetNode('/i2c')
1762
1763 # For now this uclass is not compiled in, so no sequence is assigned
1764 self.assertEqual(4, i2c.seq)
1765 spl = plat._fdt.GetNode('/spl-test')
1766 self.assertEqual(0, spl.seq)
Simon Glassc14fd0c2021-02-03 06:01:11 -07001767
1768 def test_process_root(self):
1769 """Test assignment of sequence numnbers"""
1770 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass80025522022-01-29 14:14:04 -07001771 output = tools.get_output_filename('output')
Simon Glassc14fd0c2021-02-03 06:01:11 -07001772
1773 # Take a copy before messing with it
1774 scan = copy_scan()
1775 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
1776 plat.scan_dtb()
1777 root = plat._fdt.GetRoot()
1778
1779 plat.scan_tree(False)
1780 self.assertNotIn(root, plat._valid_nodes)
1781
1782 plat.scan_tree(True)
1783 self.assertIn(root, plat._valid_nodes)
1784 self.assertEqual('root_driver',
1785 scan.get_normalized_compat_name(root)[0])
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001786
1787 def test_simple_inst(self):
1788 """Test output from some simple nodes with instantiate enabled"""
Simon Glassc7b4b832021-02-03 06:01:20 -07001789 dtb_file = get_dtb_file('dtoc_test_inst.dts')
Simon Glass80025522022-01-29 14:14:04 -07001790 output = tools.get_output_filename('output')
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001791
1792 self.run_test(['decl'], dtb_file, output, True)
1793 with open(output) as infile:
1794 data = infile.read()
1795
1796 self._check_strings(self.decl_text_inst, data)
Simon Glassbe749002021-02-03 06:01:15 -07001797
Simon Glassc7b4b832021-02-03 06:01:20 -07001798 self.run_test(['uclass'], dtb_file, output, True)
1799 with open(output) as infile:
1800 data = infile.read()
1801
1802 self._check_strings(UCLASS_HEADER_COMMON + self.uclass_text_inst, data)
1803
Simon Glassfea2f252021-02-03 06:01:21 -07001804 self.run_test(['device'], dtb_file, output, True)
1805 with open(output) as infile:
1806 data = infile.read()
1807
1808 self._check_strings(self.device_text_inst, data)
1809
Simon Glassc7b4b832021-02-03 06:01:20 -07001810 def test_inst_no_hdr(self):
Simon Glassfea2f252021-02-03 06:01:21 -07001811 """Test dealing with a struct tsssshat has no header"""
Simon Glassc7b4b832021-02-03 06:01:20 -07001812 dtb_file = get_dtb_file('dtoc_test_inst.dts')
Simon Glass80025522022-01-29 14:14:04 -07001813 output = tools.get_output_filename('output')
Simon Glassc7b4b832021-02-03 06:01:20 -07001814
1815 # Run it once to set everything up
1816 plat = self.run_test(['decl'], dtb_file, output, True)
1817 scan = plat._scan
1818
1819 # Restart the output file and delete any record of the uclass' struct
1820 plat.setup_output(Ftype.SOURCE, output)
1821 del scan._structs['dm_test_uc_priv']
1822
1823 # Now generate the uclasses, which should provide a warning
1824 with test_util.capture_sys_output() as (stdout, _):
1825 plat.generate_uclasses()
1826 self.assertEqual(
1827 'Warning: Cannot find header file for struct dm_test_uc_priv',
1828 stdout.getvalue().strip())
Simon Glass4415dc12021-03-26 16:17:27 +13001829
1830 def test_missing_props(self):
1831 """Test detection of a parent node with no properties"""
1832 dtb_file = get_dtb_file('dtoc_test_noprops.dts', capture_stderr=True)
Simon Glass80025522022-01-29 14:14:04 -07001833 output = tools.get_output_filename('output')
Simon Glass4415dc12021-03-26 16:17:27 +13001834 with self.assertRaises(ValueError) as exc:
1835 self.run_test(['struct'], dtb_file, output)
1836 self.assertIn("Parent node '/i2c@0' has no properties - do you need",
1837 str(exc.exception))
1838
1839 def test_single_reg(self):
1840 """Test detection of a parent node with no properties"""
1841 dtb_file = get_dtb_file('dtoc_test_single_reg.dts')
Simon Glass80025522022-01-29 14:14:04 -07001842 output = tools.get_output_filename('output')
Simon Glass4415dc12021-03-26 16:17:27 +13001843 self.run_test(['struct'], dtb_file, output)
Simon Glass9ebd5512021-06-27 17:51:10 -06001844
1845 def test_missing_parent(self):
1846 """Test detection of a parent node with no properties"""
1847 dtb_file = get_dtb_file('dtoc_test_noparent.dts', capture_stderr=True)
Simon Glass80025522022-01-29 14:14:04 -07001848 output = tools.get_output_filename('output')
Simon Glass9ebd5512021-06-27 17:51:10 -06001849 with self.assertRaises(ValueError) as exc:
1850 self.run_test(['device'], dtb_file, output, instantiate=True)
1851 self.assertIn("Node '/i2c@0/spl-test/pmic@9' requires parent node "
1852 "'/i2c@0/spl-test' but it is not in the valid list",
1853 str(exc.exception))