blob: 0f544f9f54a3f9d9499cad0cf12c1dda2e77be1c [file] [log] [blame]
Simon Glass3c19dc82019-10-31 07:42:55 -06001#!/usr/bin/env python3
Tom Rini83d290c2018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glassc0791922017-06-18 22:09:06 -06003# Copyright (c) 2012 The Chromium OS Authors.
4#
Simon Glassc0791922017-06-18 22:09:06 -06005
6"""Tests for the dtb_platdata module
7
Simon Glass3def0cf2018-07-06 10:27:20 -06008This includes unit tests for some functions and functional tests for the dtoc
9tool.
Simon Glassc0791922017-06-18 22:09:06 -060010"""
11
12import collections
Simon Glassa32eb7d2021-02-03 06:00:51 -070013import copy
Simon Glass10cbd3b2020-12-28 20:34:52 -070014import glob
Simon Glassc0791922017-06-18 22:09:06 -060015import os
16import struct
17import unittest
18
Simon Glass67b5ec52020-12-28 20:34:47 -070019from dtoc import dtb_platdata
Simon Glassbf776672020-04-17 18:09:04 -060020from dtoc import fdt
21from dtoc import fdt_util
Simon Glassa32eb7d2021-02-03 06:00:51 -070022from dtoc import src_scan
Simon Glass8a455fc2022-02-11 13:23:20 -070023from dtoc.dtb_platdata import Ftype
24from dtoc.dtb_platdata import get_value
25from dtoc.dtb_platdata import tab_to
Simon Glassa542a702020-12-28 20:35:06 -070026from dtoc.src_scan import conv_name_to_c
27from dtoc.src_scan import get_compat_name
Simon Glass4583c002023-02-23 18:18:04 -070028from u_boot_pylib import test_util
29from u_boot_pylib import tools
Simon Glassc0791922017-06-18 22:09:06 -060030
Simon Glass67b5ec52020-12-28 20:34:47 -070031OUR_PATH = os.path.dirname(os.path.realpath(__file__))
Simon Glassc0791922017-06-18 22:09:06 -060032
33
Simon Glassaab660f2017-11-12 21:52:17 -070034HEADER = '''/*
35 * DO NOT MODIFY
36 *
Simon Glassd1055d62020-12-28 20:35:00 -070037 * Defines the structs used to hold devicetree data.
38 * This was generated by dtoc from a .dtb (device tree binary) file.
Simon Glassaab660f2017-11-12 21:52:17 -070039 */
40
41#include <stdbool.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090042#include <linux/libfdt.h>'''
Simon Glassaab660f2017-11-12 21:52:17 -070043
Simon Glass426d12f2021-02-03 06:01:14 -070044DECL_HEADER = '''/*
45 * DO NOT MODIFY
46 *
47 * Declares externs for all device/uclass instances.
48 * This was generated by dtoc from a .dtb (device tree binary) file.
49 */
50'''
51
Simon Glass4b91be22021-02-03 06:01:15 -070052C_HEADER_PRE = '''/*
Simon Glassaab660f2017-11-12 21:52:17 -070053 * DO NOT MODIFY
54 *
Simon Glassd1055d62020-12-28 20:35:00 -070055 * Declares the U_BOOT_DRIVER() records and platform data.
56 * This was generated by dtoc from a .dtb (device tree binary) file.
Simon Glassaab660f2017-11-12 21:52:17 -070057 */
Simon Glass4b91be22021-02-03 06:01:15 -070058'''
Simon Glassaab660f2017-11-12 21:52:17 -070059
Simon Glass4b91be22021-02-03 06:01:15 -070060C_HEADER = C_HEADER_PRE + '''
Simon Glass20e442a2020-12-28 20:34:54 -070061/* Allow use of U_BOOT_DRVINFO() in this file */
Simon Glassf31fa992020-12-28 20:35:01 -070062#define DT_PLAT_C
Simon Glasscb43ac12020-10-03 11:31:41 -060063
Simon Glassaab660f2017-11-12 21:52:17 -070064#include <common.h>
65#include <dm.h>
66#include <dt-structs.h>
67'''
68
Simon Glassea74c952021-02-03 06:01:20 -070069UCLASS_HEADER_COMMON = '''/*
70 * DO NOT MODIFY
71 *
72 * Declares the uclass instances (struct uclass).
73 * This was generated by dtoc from a .dtb (device tree binary) file.
74 */
75'''
76
Simon Glassa32eb7d2021-02-03 06:00:51 -070077# Scanner saved from a previous run of the tests (to speed things up)
78saved_scan = None
79
Simon Glass67b5ec52020-12-28 20:34:47 -070080# This is a test so is allowed to access private things in the module it is
81# testing
82# pylint: disable=W0212
Simon Glassfe57c782018-07-06 10:27:37 -060083
84def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glassc0791922017-06-18 22:09:06 -060085 """Compile a .dts file to a .dtb
86
87 Args:
Simon Glass67b5ec52020-12-28 20:34:47 -070088 dts_fname (str): Filename of .dts file in the current directory
89 capture_stderr (bool): True to capture and discard stderr output
Simon Glassc0791922017-06-18 22:09:06 -060090
91 Returns:
Simon Glass67b5ec52020-12-28 20:34:47 -070092 str: Filename of compiled file in output directory
Simon Glassc0791922017-06-18 22:09:06 -060093 """
Simon Glassdff51a52021-02-03 06:00:56 -070094 return fdt_util.EnsureCompiled(os.path.join(OUR_PATH, 'test', dts_fname),
Simon Glassfe57c782018-07-06 10:27:37 -060095 capture_stderr=capture_stderr)
Simon Glassc0791922017-06-18 22:09:06 -060096
97
Simon Glassa32eb7d2021-02-03 06:00:51 -070098def setup():
99 global saved_scan
100
101 # Disable warnings so that calls to get_normalized_compat_name() will not
102 # output things.
Simon Glass0c59ace2021-03-26 16:17:25 +1300103 saved_scan = src_scan.Scanner(None, False)
Simon Glassa32eb7d2021-02-03 06:00:51 -0700104 saved_scan.scan_drivers()
105
106def copy_scan():
107 """Get a copy of saved_scan so that each test can start clean"""
108 return copy.deepcopy(saved_scan)
109
110
Simon Glassc0791922017-06-18 22:09:06 -0600111class TestDtoc(unittest.TestCase):
112 """Tests for dtoc"""
113 @classmethod
114 def setUpClass(cls):
Simon Glassc1aa66e2022-01-29 14:14:04 -0700115 tools.prepare_output_dir(None)
Simon Glassf02d0eb2020-07-07 21:32:06 -0600116 cls.maxDiff = None
Simon Glassc0791922017-06-18 22:09:06 -0600117
118 @classmethod
119 def tearDownClass(cls):
Simon Glassc1aa66e2022-01-29 14:14:04 -0700120 tools.finalise_output_dir()
Simon Glassc0791922017-06-18 22:09:06 -0600121
Simon Glass67b5ec52020-12-28 20:34:47 -0700122 @staticmethod
123 def _write_python_string(fname, data):
Simon Glass57f0bc42018-07-06 10:27:25 -0600124 """Write a string with tabs expanded as done in this Python file
125
126 Args:
Simon Glass67b5ec52020-12-28 20:34:47 -0700127 fname (str): Filename to write to
128 data (str): Raw string to convert
Simon Glass57f0bc42018-07-06 10:27:25 -0600129 """
130 data = data.replace('\t', '\\t')
Simon Glass67b5ec52020-12-28 20:34:47 -0700131 with open(fname, 'w') as fout:
132 fout.write(data)
Simon Glass57f0bc42018-07-06 10:27:25 -0600133
Simon Glass67b5ec52020-12-28 20:34:47 -0700134 def _check_strings(self, expected, actual):
Simon Glass57f0bc42018-07-06 10:27:25 -0600135 """Check that a string matches its expected value
136
137 If the strings do not match, they are written to the /tmp directory in
138 the same Python format as is used here in the test. This allows for
139 easy comparison and update of the tests.
140
141 Args:
Simon Glass67b5ec52020-12-28 20:34:47 -0700142 expected (str): Expected string
143 actual (str): Actual string
Simon Glass57f0bc42018-07-06 10:27:25 -0600144 """
145 if expected != actual:
Simon Glass67b5ec52020-12-28 20:34:47 -0700146 self._write_python_string('/tmp/binman.expected', expected)
147 self._write_python_string('/tmp/binman.actual', actual)
Simon Glass90a81322019-05-17 22:00:31 -0600148 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glass67b5ec52020-12-28 20:34:47 -0700149 self.assertEqual(expected, actual)
Simon Glass57f0bc42018-07-06 10:27:25 -0600150
Simon Glass67b5ec52020-12-28 20:34:47 -0700151 @staticmethod
Simon Glass4a092352021-02-03 06:01:12 -0700152 def run_test(args, dtb_file, output, instantiate=False):
Simon Glass67b5ec52020-12-28 20:34:47 -0700153 """Run a test using dtoc
Walter Lozano361e7332020-06-25 01:10:08 -0300154
Simon Glass67b5ec52020-12-28 20:34:47 -0700155 Args:
156 args (list of str): List of arguments for dtoc
157 dtb_file (str): Filename of .dtb file
158 output (str): Filename of output file
Simon Glass05953522021-02-03 06:01:07 -0700159
160 Returns:
161 DtbPlatdata object
Simon Glass67b5ec52020-12-28 20:34:47 -0700162 """
Simon Glass337d6972021-02-03 06:01:10 -0700163 # Make a copy of the 'scan' object, since it includes uclasses and
164 # drivers, which get updated during execution.
Simon Glass4a092352021-02-03 06:01:12 -0700165 return dtb_platdata.run_steps(
166 args, dtb_file, False, output, [], None, instantiate,
167 warning_disabled=True, scan=copy_scan())
Walter Lozano361e7332020-06-25 01:10:08 -0300168
Simon Glassc0791922017-06-18 22:09:06 -0600169 def test_name(self):
170 """Test conversion of device tree names to C identifiers"""
171 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
172 self.assertEqual('vendor_clock_frequency',
173 conv_name_to_c('vendor,clock-frequency'))
174 self.assertEqual('rockchip_rk3399_sdhci_5_1',
175 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
176
177 def test_tab_to(self):
178 """Test operation of tab_to() function"""
179 self.assertEqual('fred ', tab_to(0, 'fred'))
180 self.assertEqual('fred\t', tab_to(1, 'fred'))
181 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
182 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
183 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
184 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
185
186 def test_get_value(self):
187 """Test operation of get_value() function"""
188 self.assertEqual('0x45',
Simon Glass5ea9dcc2020-11-08 20:36:17 -0700189 get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
Simon Glassc0791922017-06-18 22:09:06 -0600190 self.assertEqual('0x45',
Simon Glass5ea9dcc2020-11-08 20:36:17 -0700191 get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
Simon Glassc0791922017-06-18 22:09:06 -0600192 self.assertEqual('0x0',
Simon Glass5ea9dcc2020-11-08 20:36:17 -0700193 get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
194 self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
195 self.assertEqual('true', get_value(fdt.Type.BOOL, None))
Simon Glassc0791922017-06-18 22:09:06 -0600196
197 def test_get_compat_name(self):
198 """Test operation of get_compat_name() function"""
199 Prop = collections.namedtuple('Prop', ['value'])
200 Node = collections.namedtuple('Node', ['props'])
201
202 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
203 node = Node({'compatible': prop})
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300204 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glassc0791922017-06-18 22:09:06 -0600205 get_compat_name(node))
206
207 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
208 node = Node({'compatible': prop})
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300209 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glassc0791922017-06-18 22:09:06 -0600210 get_compat_name(node))
211
212 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
213 node = Node({'compatible': prop})
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300214 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
Simon Glass67b5ec52020-12-28 20:34:47 -0700215 'arasan_sdhci_5_1', 'third']),
Simon Glassc0791922017-06-18 22:09:06 -0600216 get_compat_name(node))
217
218 def test_empty_file(self):
219 """Test output from a device tree file with no nodes"""
220 dtb_file = get_dtb_file('dtoc_test_empty.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -0700221 output = tools.get_output_filename('output')
Simon Glassa32eb7d2021-02-03 06:00:51 -0700222
223 # Run this one without saved_scan to complete test coverage
Simon Glass4a092352021-02-03 06:01:12 -0700224 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [], None,
225 False)
Simon Glassc0791922017-06-18 22:09:06 -0600226 with open(output) as infile:
227 lines = infile.read().splitlines()
Simon Glassaab660f2017-11-12 21:52:17 -0700228 self.assertEqual(HEADER.splitlines(), lines)
Simon Glassc0791922017-06-18 22:09:06 -0600229
Walter Lozano361e7332020-06-25 01:10:08 -0300230 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600231 with open(output) as infile:
232 lines = infile.read().splitlines()
Simon Glassd960f0d2020-12-28 20:35:05 -0700233 self.assertEqual(C_HEADER.splitlines() + [''], lines)
Simon Glassc0791922017-06-18 22:09:06 -0600234
Simon Glass426d12f2021-02-03 06:01:14 -0700235 decl_text = DECL_HEADER + '''
236#include <dm/device-internal.h>
237#include <dm/uclass-internal.h>
238
239/* driver declarations - these allow DM_DRIVER_GET() to be used */
Simon Glasscff7dcf2021-03-15 17:25:11 +1300240extern U_BOOT_DRIVER(sandbox_i2c);
241extern U_BOOT_DRIVER(sandbox_pmic);
242extern U_BOOT_DRIVER(sandbox_spl_test);
243extern U_BOOT_DRIVER(sandbox_spl_test);
244extern U_BOOT_DRIVER(sandbox_spl_test);
Simon Glass426d12f2021-02-03 06:01:14 -0700245
246/* uclass driver declarations - needed for DM_UCLASS_DRIVER_REF() */
Simon Glasscff7dcf2021-03-15 17:25:11 +1300247extern UCLASS_DRIVER(i2c);
248extern UCLASS_DRIVER(misc);
249extern UCLASS_DRIVER(pmic);
Simon Glass426d12f2021-02-03 06:01:14 -0700250'''
251 decl_text_inst = DECL_HEADER + '''
252#include <dm/device-internal.h>
253#include <dm/uclass-internal.h>
254
255/* driver declarations - these allow DM_DRIVER_GET() to be used */
Simon Glasscff7dcf2021-03-15 17:25:11 +1300256extern U_BOOT_DRIVER(sandbox_i2c);
257extern U_BOOT_DRIVER(root_driver);
258extern U_BOOT_DRIVER(denx_u_boot_test_bus);
259extern U_BOOT_DRIVER(sandbox_spl_test);
260extern U_BOOT_DRIVER(sandbox_spl_test);
261extern U_BOOT_DRIVER(denx_u_boot_fdt_test);
262extern U_BOOT_DRIVER(denx_u_boot_fdt_test);
Simon Glass426d12f2021-02-03 06:01:14 -0700263
264/* device declarations - these allow DM_DEVICE_REF() to be used */
Simon Glasscff7dcf2021-03-15 17:25:11 +1300265extern DM_DEVICE_INST(i2c);
266extern DM_DEVICE_INST(root);
267extern DM_DEVICE_INST(some_bus);
268extern DM_DEVICE_INST(spl_test);
269extern DM_DEVICE_INST(spl_test3);
270extern DM_DEVICE_INST(test);
271extern DM_DEVICE_INST(test0);
Simon Glass426d12f2021-02-03 06:01:14 -0700272
273/* uclass driver declarations - needed for DM_UCLASS_DRIVER_REF() */
Simon Glasscff7dcf2021-03-15 17:25:11 +1300274extern UCLASS_DRIVER(i2c);
275extern UCLASS_DRIVER(misc);
276extern UCLASS_DRIVER(root);
277extern UCLASS_DRIVER(testbus);
278extern UCLASS_DRIVER(testfdt);
Simon Glass426d12f2021-02-03 06:01:14 -0700279
280/* uclass declarations - needed for DM_UCLASS_REF() */
Simon Glasscff7dcf2021-03-15 17:25:11 +1300281extern DM_UCLASS_INST(i2c);
282extern DM_UCLASS_INST(misc);
283extern DM_UCLASS_INST(root);
284extern DM_UCLASS_INST(testbus);
285extern DM_UCLASS_INST(testfdt);
Simon Glass426d12f2021-02-03 06:01:14 -0700286'''
Simon Glassde846cb2020-12-28 20:34:49 -0700287 struct_text = HEADER + '''
Simon Glassf38161c2021-02-03 06:00:57 -0700288struct dtd_sandbox_i2c {
Simon Glass5ec741f2017-08-29 14:15:51 -0600289};
Simon Glassf38161c2021-02-03 06:00:57 -0700290struct dtd_sandbox_pmic {
Simon Glass5ec741f2017-08-29 14:15:51 -0600291\tbool\t\tlow_power;
Simon Glass3e200ca2021-03-26 16:17:26 +1300292\tfdt32_t\t\treg[1];
Simon Glass5ec741f2017-08-29 14:15:51 -0600293};
Simon Glassc0791922017-06-18 22:09:06 -0600294struct dtd_sandbox_spl_test {
Simon Glassf02d0eb2020-07-07 21:32:06 -0600295\tconst char * acpi_name;
Simon Glassc0791922017-06-18 22:09:06 -0600296\tbool\t\tboolval;
297\tunsigned char\tbytearray[3];
298\tunsigned char\tbyteval;
Simon Glassd866e622021-11-23 11:03:39 -0700299\tfdt32_t\t\tint64val[2];
Simon Glassca044942021-07-28 19:23:10 -0600300\tfdt32_t\t\tintarray[3];
Simon Glassc0791922017-06-18 22:09:06 -0600301\tfdt32_t\t\tintval;
302\tunsigned char\tlongbytearray[9];
Simon Glasseec44c72021-07-28 19:23:11 -0600303\tfdt32_t\t\tmaybe_empty_int[1];
Simon Glass2a2d91d2018-07-06 10:27:28 -0600304\tunsigned char\tnotstring[5];
Simon Glassc0791922017-06-18 22:09:06 -0600305\tconst char *\tstringarray[3];
306\tconst char *\tstringval;
307};
Simon Glassde846cb2020-12-28 20:34:49 -0700308'''
Simon Glassde846cb2020-12-28 20:34:49 -0700309 platdata_text = C_HEADER + '''
Simon Glass4b91be22021-02-03 06:01:15 -0700310/*
Simon Glass9763e4e2021-02-03 06:01:19 -0700311 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
312 *
313 * idx driver_info driver
314 * --- -------------------- --------------------
315 * 0: i2c_at_0 sandbox_i2c
316 * 1: pmic_at_9 sandbox_pmic
317 * 2: spl_test sandbox_spl_test
318 * 3: spl_test2 sandbox_spl_test
319 * 4: spl_test3 sandbox_spl_test
320 * --- -------------------- --------------------
321 */
322
323/*
Simon Glass4b91be22021-02-03 06:01:15 -0700324 * Node /i2c@0 index 0
325 * driver sandbox_i2c parent None
326 */
Simon Glassf38161c2021-02-03 06:00:57 -0700327static struct dtd_sandbox_i2c dtv_i2c_at_0 = {
Simon Glass1b272732020-10-03 11:31:25 -0600328};
Simon Glass20e442a2020-12-28 20:34:54 -0700329U_BOOT_DRVINFO(i2c_at_0) = {
Simon Glassf38161c2021-02-03 06:00:57 -0700330\t.name\t\t= "sandbox_i2c",
Simon Glass9763e4e2021-02-03 06:01:19 -0700331\t.plat\t\t= &dtv_i2c_at_0,
Simon Glass4f500862020-12-03 16:55:19 -0700332\t.plat_size\t= sizeof(dtv_i2c_at_0),
Simon Glasse41651f2020-10-03 11:31:35 -0600333\t.parent_idx\t= -1,
Simon Glass1b272732020-10-03 11:31:25 -0600334};
335
Simon Glass4b91be22021-02-03 06:01:15 -0700336/*
337 * Node /i2c@0/pmic@9 index 1
338 * driver sandbox_pmic parent sandbox_i2c
339 */
Simon Glassf38161c2021-02-03 06:00:57 -0700340static struct dtd_sandbox_pmic dtv_pmic_at_9 = {
Simon Glass1b272732020-10-03 11:31:25 -0600341\t.low_power\t\t= true,
Simon Glass3e200ca2021-03-26 16:17:26 +1300342\t.reg\t\t\t= {0x9},
Simon Glass1b272732020-10-03 11:31:25 -0600343};
Simon Glass20e442a2020-12-28 20:34:54 -0700344U_BOOT_DRVINFO(pmic_at_9) = {
Simon Glassf38161c2021-02-03 06:00:57 -0700345\t.name\t\t= "sandbox_pmic",
Simon Glass9763e4e2021-02-03 06:01:19 -0700346\t.plat\t\t= &dtv_pmic_at_9,
Simon Glass4f500862020-12-03 16:55:19 -0700347\t.plat_size\t= sizeof(dtv_pmic_at_9),
Simon Glasse41651f2020-10-03 11:31:35 -0600348\t.parent_idx\t= 0,
Simon Glass1b272732020-10-03 11:31:25 -0600349};
350
Simon Glass4b91be22021-02-03 06:01:15 -0700351/*
352 * Node /spl-test index 2
353 * driver sandbox_spl_test parent None
354 */
Walter Lozano51f12632020-06-25 01:10:13 -0300355static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass1953ce72019-05-17 22:00:32 -0600356\t.boolval\t\t= true,
Simon Glassc0791922017-06-18 22:09:06 -0600357\t.bytearray\t\t= {0x6, 0x0, 0x0},
358\t.byteval\t\t= 0x5,
Simon Glassd866e622021-11-23 11:03:39 -0700359\t.int64val\t\t= {0x12345678, 0x9abcdef0},
Simon Glassca044942021-07-28 19:23:10 -0600360\t.intarray\t\t= {0x2, 0x3, 0x4},
Simon Glassc0791922017-06-18 22:09:06 -0600361\t.intval\t\t\t= 0x1,
Simon Glass21d54ac2017-08-29 14:15:49 -0600362\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
363\t\t0x11},
Simon Glasseec44c72021-07-28 19:23:11 -0600364\t.maybe_empty_int\t= {0x0},
Simon Glass1953ce72019-05-17 22:00:32 -0600365\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600366\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glass1953ce72019-05-17 22:00:32 -0600367\t.stringval\t\t= "message",
Simon Glassc0791922017-06-18 22:09:06 -0600368};
Simon Glass20e442a2020-12-28 20:34:54 -0700369U_BOOT_DRVINFO(spl_test) = {
Simon Glassc0791922017-06-18 22:09:06 -0600370\t.name\t\t= "sandbox_spl_test",
Simon Glass9763e4e2021-02-03 06:01:19 -0700371\t.plat\t\t= &dtv_spl_test,
Simon Glass4f500862020-12-03 16:55:19 -0700372\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glasse41651f2020-10-03 11:31:35 -0600373\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600374};
375
Simon Glass4b91be22021-02-03 06:01:15 -0700376/*
377 * Node /spl-test2 index 3
378 * driver sandbox_spl_test parent None
379 */
Walter Lozano51f12632020-06-25 01:10:13 -0300380static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glassf02d0eb2020-07-07 21:32:06 -0600381\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glassc0791922017-06-18 22:09:06 -0600382\t.bytearray\t\t= {0x1, 0x23, 0x34},
383\t.byteval\t\t= 0x8,
Simon Glassca044942021-07-28 19:23:10 -0600384\t.intarray\t\t= {0x5, 0x0, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600385\t.intval\t\t\t= 0x3,
Simon Glasse144caf2020-10-03 11:31:27 -0600386\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
Simon Glass21d54ac2017-08-29 14:15:49 -0600387\t\t0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600388\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glass1953ce72019-05-17 22:00:32 -0600389\t.stringval\t\t= "message2",
Simon Glassc0791922017-06-18 22:09:06 -0600390};
Simon Glass20e442a2020-12-28 20:34:54 -0700391U_BOOT_DRVINFO(spl_test2) = {
Simon Glassc0791922017-06-18 22:09:06 -0600392\t.name\t\t= "sandbox_spl_test",
Simon Glass9763e4e2021-02-03 06:01:19 -0700393\t.plat\t\t= &dtv_spl_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700394\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600395\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600396};
397
Simon Glass4b91be22021-02-03 06:01:15 -0700398/*
399 * Node /spl-test3 index 4
400 * driver sandbox_spl_test parent None
401 */
Walter Lozano51f12632020-06-25 01:10:13 -0300402static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glasse144caf2020-10-03 11:31:27 -0600403\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
404\t\t0x0},
Simon Glasseec44c72021-07-28 19:23:11 -0600405\t.maybe_empty_int\t= {0x1},
Simon Glassc0791922017-06-18 22:09:06 -0600406\t.stringarray\t\t= {"one", "", ""},
407};
Simon Glass20e442a2020-12-28 20:34:54 -0700408U_BOOT_DRVINFO(spl_test3) = {
Simon Glassc0791922017-06-18 22:09:06 -0600409\t.name\t\t= "sandbox_spl_test",
Simon Glass9763e4e2021-02-03 06:01:19 -0700410\t.plat\t\t= &dtv_spl_test3,
Simon Glass4f500862020-12-03 16:55:19 -0700411\t.plat_size\t= sizeof(dtv_spl_test3),
Simon Glasse41651f2020-10-03 11:31:35 -0600412\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600413};
414
Simon Glassd960f0d2020-12-28 20:35:05 -0700415'''
Simon Glassea74c952021-02-03 06:01:20 -0700416 uclass_text_inst = '''
417
418#include <common.h>
419#include <dm.h>
420#include <dt-structs.h>
421
422/*
Simon Glassd392d322021-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 Glassea74c952021-02-03 06:01:20 -0700429 *
Simon Glassd392d322021-02-03 06:01:21 -0700430 * Sequence numbers allocated in each uclass:
Simon Glassea74c952021-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
514'''
Simon Glassd392d322021-02-03 06:01:21 -0700515 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
522#include <common.h>
523#include <dm.h>
524#include <dt-structs.h>
525
526/*
527 * udevice declarations, ordered by 'struct udevice' linker_list position:
528 *
529 * idx udevice driver
530 * --- -------------------- --------------------
531 * 0: i2c sandbox_i2c
532 * 1: root root_driver
533 * 2: some_bus denx_u_boot_test_bus
534 * 3: spl_test sandbox_spl_test
535 * 4: spl_test3 sandbox_spl_test
536 * 5: test denx_u_boot_fdt_test
537 * 6: test0 denx_u_boot_fdt_test
538 * --- -------------------- --------------------
539 */
540
541/*
542 * Node /i2c index 0
543 * driver sandbox_i2c parent root_driver
544*/
545static struct dtd_sandbox_i2c dtv_i2c = {
546\t.intval\t\t\t= 0x3,
547};
548
549#include <asm/i2c.h>
550u8 _sandbox_i2c_priv_i2c[sizeof(struct sandbox_i2c_priv)]
551\t__attribute__ ((section (".priv_data")));
552#include <i2c.h>
553u8 _sandbox_i2c_uc_priv_i2c[sizeof(struct dm_i2c_bus)]
554\t__attribute__ ((section (".priv_data")));
555
556DM_DEVICE_INST(i2c) = {
557\t.driver\t\t= DM_DRIVER_REF(sandbox_i2c),
558\t.name\t\t= "sandbox_i2c",
559\t.plat_\t\t= &dtv_i2c,
560\t.priv_\t\t= _sandbox_i2c_priv_i2c,
561\t.uclass\t\t= DM_UCLASS_REF(i2c),
562\t.uclass_priv_ = _sandbox_i2c_uc_priv_i2c,
563\t.uclass_node\t= {
564\t\t.prev = &DM_UCLASS_REF(i2c)->dev_head,
565\t\t.next = &DM_UCLASS_REF(i2c)->dev_head,
566\t},
567\t.child_head\t= {
568\t\t.prev = &DM_DEVICE_REF(i2c)->child_head,
569\t\t.next = &DM_DEVICE_REF(i2c)->child_head,
570\t},
571\t.sibling_node\t= {
572\t\t.prev = &DM_DEVICE_REF(root)->child_head,
573\t\t.next = &DM_DEVICE_REF(some_bus)->sibling_node,
574\t},
575\t.seq_ = 4,
576};
577
578/*
579 * Node / index 1
580 * driver root_driver parent None
581*/
582static struct dtd_root_driver dtv_root = {
583};
584
585DM_DEVICE_INST(root) = {
586\t.driver\t\t= DM_DRIVER_REF(root_driver),
587\t.name\t\t= "root_driver",
588\t.plat_\t\t= &dtv_root,
589\t.uclass\t\t= DM_UCLASS_REF(root),
590\t.uclass_node\t= {
591\t\t.prev = &DM_UCLASS_REF(root)->dev_head,
592\t\t.next = &DM_UCLASS_REF(root)->dev_head,
593\t},
594\t.child_head\t= {
595\t\t.prev = &DM_DEVICE_REF(spl_test3)->sibling_node,
596\t\t.next = &DM_DEVICE_REF(i2c)->sibling_node,
597\t},
598\t.seq_ = 0,
599};
600
601/*
602 * Node /some-bus index 2
603 * driver denx_u_boot_test_bus parent root_driver
604*/
605
606#include <dm/test.h>
607struct dm_test_pdata __attribute__ ((section (".priv_data")))
608\t_denx_u_boot_test_bus_plat_some_bus = {
609\t.dtplat = {
610\t\t.ping_add\t\t= 0x4,
611\t\t.ping_expect\t\t= 0x4,
612\t\t.reg\t\t\t= {0x3, 0x1},
613\t},
614};
615#include <dm/test.h>
616u8 _denx_u_boot_test_bus_priv_some_bus[sizeof(struct dm_test_priv)]
617\t__attribute__ ((section (".priv_data")));
618#include <dm/test.h>
Simon Glass53c20be2022-05-08 04:39:23 -0600619u8 _denx_u_boot_test_bus_ucplat_some_bus[sizeof(struct dm_test_uclass_plat)]
Simon Glassd392d322021-02-03 06:01:21 -0700620\t__attribute__ ((section (".priv_data")));
Simon Glass930a3dd2022-05-08 04:39:24 -0600621#include <dm/test.h>
622u8 _denx_u_boot_test_bus_uc_priv_some_bus[sizeof(struct dm_test_uclass_priv)]
623 __attribute__ ((section (".priv_data")));
Simon Glassd392d322021-02-03 06:01:21 -0700624#include <test.h>
625
626DM_DEVICE_INST(some_bus) = {
627\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_test_bus),
628\t.name\t\t= "denx_u_boot_test_bus",
629\t.plat_\t\t= &_denx_u_boot_test_bus_plat_some_bus,
630\t.uclass_plat_\t= _denx_u_boot_test_bus_ucplat_some_bus,
631\t.driver_data\t= DM_TEST_TYPE_FIRST,
632\t.priv_\t\t= _denx_u_boot_test_bus_priv_some_bus,
633\t.uclass\t\t= DM_UCLASS_REF(testbus),
Simon Glass930a3dd2022-05-08 04:39:24 -0600634\t.uclass_priv_ = _denx_u_boot_test_bus_uc_priv_some_bus,
Simon Glassd392d322021-02-03 06:01:21 -0700635\t.uclass_node\t= {
636\t\t.prev = &DM_UCLASS_REF(testbus)->dev_head,
637\t\t.next = &DM_UCLASS_REF(testbus)->dev_head,
638\t},
639\t.child_head\t= {
640\t\t.prev = &DM_DEVICE_REF(test0)->sibling_node,
641\t\t.next = &DM_DEVICE_REF(test)->sibling_node,
642\t},
643\t.sibling_node\t= {
644\t\t.prev = &DM_DEVICE_REF(i2c)->sibling_node,
645\t\t.next = &DM_DEVICE_REF(spl_test)->sibling_node,
646\t},
647\t.seq_ = 2,
648};
649
650/*
651 * Node /spl-test index 3
652 * driver sandbox_spl_test parent root_driver
653*/
654static struct dtd_sandbox_spl_test dtv_spl_test = {
655\t.boolval\t\t= true,
656\t.intval\t\t\t= 0x1,
657};
658
659DM_DEVICE_INST(spl_test) = {
660\t.driver\t\t= DM_DRIVER_REF(sandbox_spl_test),
661\t.name\t\t= "sandbox_spl_test",
662\t.plat_\t\t= &dtv_spl_test,
663\t.uclass\t\t= DM_UCLASS_REF(misc),
664\t.uclass_node\t= {
665\t\t.prev = &DM_UCLASS_REF(misc)->dev_head,
666\t\t.next = &DM_DEVICE_REF(spl_test3)->uclass_node,
667\t},
668\t.child_head\t= {
669\t\t.prev = &DM_DEVICE_REF(spl_test)->child_head,
670\t\t.next = &DM_DEVICE_REF(spl_test)->child_head,
671\t},
672\t.sibling_node\t= {
673\t\t.prev = &DM_DEVICE_REF(some_bus)->sibling_node,
674\t\t.next = &DM_DEVICE_REF(spl_test3)->sibling_node,
675\t},
676\t.seq_ = 0,
677};
678
679/*
680 * Node /spl-test3 index 4
681 * driver sandbox_spl_test parent root_driver
682*/
683static struct dtd_sandbox_spl_test dtv_spl_test3 = {
684\t.longbytearray\t\t= {0x90a0b0c, 0xd0e0f10},
685\t.stringarray\t\t= "one",
686};
687
688DM_DEVICE_INST(spl_test3) = {
689\t.driver\t\t= DM_DRIVER_REF(sandbox_spl_test),
690\t.name\t\t= "sandbox_spl_test",
691\t.plat_\t\t= &dtv_spl_test3,
692\t.uclass\t\t= DM_UCLASS_REF(misc),
693\t.uclass_node\t= {
694\t\t.prev = &DM_DEVICE_REF(spl_test)->uclass_node,
695\t\t.next = &DM_UCLASS_REF(misc)->dev_head,
696\t},
697\t.child_head\t= {
698\t\t.prev = &DM_DEVICE_REF(spl_test3)->child_head,
699\t\t.next = &DM_DEVICE_REF(spl_test3)->child_head,
700\t},
701\t.sibling_node\t= {
702\t\t.prev = &DM_DEVICE_REF(spl_test)->sibling_node,
703\t\t.next = &DM_DEVICE_REF(root)->child_head,
704\t},
705\t.seq_ = 1,
706};
707
708/*
709 * Node /some-bus/test index 5
710 * driver denx_u_boot_fdt_test parent denx_u_boot_test_bus
711*/
712
713#include <dm/test.h>
714struct dm_test_pdata __attribute__ ((section (".priv_data")))
715\t_denx_u_boot_fdt_test_plat_test = {
716\t.dtplat = {
717\t\t.ping_add\t\t= 0x5,
718\t\t.ping_expect\t\t= 0x5,
Simon Glass3e200ca2021-03-26 16:17:26 +1300719\t\t.reg\t\t\t= {0x5},
Simon Glassd392d322021-02-03 06:01:21 -0700720\t},
721};
722#include <dm/test.h>
723u8 _denx_u_boot_fdt_test_priv_test[sizeof(struct dm_test_priv)]
724\t__attribute__ ((section (".priv_data")));
725#include <dm/test.h>
726u8 _denx_u_boot_fdt_test_parent_plat_test[sizeof(struct dm_test_parent_plat)]
727\t__attribute__ ((section (".priv_data")));
728#include <dm/test.h>
729u8 _denx_u_boot_fdt_test_parent_priv_test[sizeof(struct dm_test_parent_data)]
730\t__attribute__ ((section (".priv_data")));
731
732DM_DEVICE_INST(test) = {
733\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_fdt_test),
734\t.name\t\t= "denx_u_boot_fdt_test",
735\t.plat_\t\t= &_denx_u_boot_fdt_test_plat_test,
736\t.parent_plat_\t= _denx_u_boot_fdt_test_parent_plat_test,
737\t.driver_data\t= DM_TEST_TYPE_FIRST,
738\t.parent\t\t= DM_DEVICE_REF(some_bus),
739\t.priv_\t\t= _denx_u_boot_fdt_test_priv_test,
740\t.uclass\t\t= DM_UCLASS_REF(testfdt),
741\t.parent_priv_\t= _denx_u_boot_fdt_test_parent_priv_test,
742\t.uclass_node\t= {
743\t\t.prev = &DM_UCLASS_REF(testfdt)->dev_head,
744\t\t.next = &DM_DEVICE_REF(test0)->uclass_node,
745\t},
746\t.child_head\t= {
747\t\t.prev = &DM_DEVICE_REF(test)->child_head,
748\t\t.next = &DM_DEVICE_REF(test)->child_head,
749\t},
750\t.sibling_node\t= {
751\t\t.prev = &DM_DEVICE_REF(some_bus)->child_head,
752\t\t.next = &DM_DEVICE_REF(test0)->sibling_node,
753\t},
754\t.seq_ = 1,
755};
756
757/*
758 * Node /some-bus/test0 index 6
759 * driver denx_u_boot_fdt_test parent denx_u_boot_test_bus
760*/
761
762#include <dm/test.h>
763struct dm_test_pdata __attribute__ ((section (".priv_data")))
764\t_denx_u_boot_fdt_test_plat_test0 = {
765\t.dtplat = {
766\t},
767};
768#include <dm/test.h>
769u8 _denx_u_boot_fdt_test_priv_test0[sizeof(struct dm_test_priv)]
770\t__attribute__ ((section (".priv_data")));
771#include <dm/test.h>
772u8 _denx_u_boot_fdt_test_parent_plat_test0[sizeof(struct dm_test_parent_plat)]
773\t__attribute__ ((section (".priv_data")));
774#include <dm/test.h>
775u8 _denx_u_boot_fdt_test_parent_priv_test0[sizeof(struct dm_test_parent_data)]
776\t__attribute__ ((section (".priv_data")));
777
778DM_DEVICE_INST(test0) = {
779\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_fdt_test),
780\t.name\t\t= "denx_u_boot_fdt_test",
781\t.plat_\t\t= &_denx_u_boot_fdt_test_plat_test0,
782\t.parent_plat_\t= _denx_u_boot_fdt_test_parent_plat_test0,
783\t.driver_data\t= DM_TEST_TYPE_SECOND,
784\t.parent\t\t= DM_DEVICE_REF(some_bus),
785\t.priv_\t\t= _denx_u_boot_fdt_test_priv_test0,
786\t.uclass\t\t= DM_UCLASS_REF(testfdt),
787\t.parent_priv_\t= _denx_u_boot_fdt_test_parent_priv_test0,
788\t.uclass_node\t= {
789\t\t.prev = &DM_DEVICE_REF(test)->uclass_node,
790\t\t.next = &DM_UCLASS_REF(testfdt)->dev_head,
791\t},
792\t.child_head\t= {
793\t\t.prev = &DM_DEVICE_REF(test0)->child_head,
794\t\t.next = &DM_DEVICE_REF(test0)->child_head,
795\t},
796\t.sibling_node\t= {
797\t\t.prev = &DM_DEVICE_REF(test)->sibling_node,
798\t\t.next = &DM_DEVICE_REF(some_bus)->child_head,
799\t},
800\t.seq_ = 2,
801};
802
803'''
Simon Glassde846cb2020-12-28 20:34:49 -0700804
805 def test_simple(self):
806 """Test output from some simple nodes with various types of data"""
807 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -0700808 output = tools.get_output_filename('output')
Simon Glassde846cb2020-12-28 20:34:49 -0700809 self.run_test(['struct'], dtb_file, output)
810 with open(output) as infile:
811 data = infile.read()
812
813 self._check_strings(self.struct_text, data)
814
815 self.run_test(['platdata'], dtb_file, output)
816 with open(output) as infile:
817 data = infile.read()
818
819 self._check_strings(self.platdata_text, data)
Simon Glassc0791922017-06-18 22:09:06 -0600820
Simon Glass426d12f2021-02-03 06:01:14 -0700821 self.run_test(['decl'], dtb_file, output)
822 with open(output) as infile:
823 data = infile.read()
824
825 self._check_strings(self.decl_text, data)
826
Simon Glass10cbd3b2020-12-28 20:34:52 -0700827 # Try the 'all' command
828 self.run_test(['all'], dtb_file, output)
Simon Glassc1aa66e2022-01-29 14:14:04 -0700829 data = tools.read_file(output, binary=False)
Simon Glassd392d322021-02-03 06:01:21 -0700830 self._check_strings(
Simon Glass17073252021-04-27 08:19:48 +1200831 self.decl_text + self.platdata_text + self.struct_text, data)
Simon Glass10cbd3b2020-12-28 20:34:52 -0700832
Walter Lozanodac82282020-07-03 08:07:17 -0300833 def test_driver_alias(self):
834 """Test output from a device tree file with a driver alias"""
835 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -0700836 output = tools.get_output_filename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300837 self.run_test(['struct'], dtb_file, output)
Walter Lozanodac82282020-07-03 08:07:17 -0300838 with open(output) as infile:
839 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700840 self._check_strings(HEADER + '''
Walter Lozanodac82282020-07-03 08:07:17 -0300841struct dtd_sandbox_gpio {
842\tconst char *\tgpio_bank_name;
843\tbool\t\tgpio_controller;
844\tfdt32_t\t\tsandbox_gpio_count;
845};
Walter Lozanodac82282020-07-03 08:07:17 -0300846''', data)
847
Walter Lozano361e7332020-06-25 01:10:08 -0300848 self.run_test(['platdata'], dtb_file, output)
Walter Lozanodac82282020-07-03 08:07:17 -0300849 with open(output) as infile:
850 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700851 self._check_strings(C_HEADER + '''
Simon Glass4b91be22021-02-03 06:01:15 -0700852/*
Simon Glass9763e4e2021-02-03 06:01:19 -0700853 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
854 *
855 * idx driver_info driver
856 * --- -------------------- --------------------
857 * 0: gpios_at_0 sandbox_gpio
858 * --- -------------------- --------------------
859 */
860
861/*
Simon Glass4b91be22021-02-03 06:01:15 -0700862 * Node /gpios@0 index 0
863 * driver sandbox_gpio parent None
864 */
Walter Lozano51f12632020-06-25 01:10:13 -0300865static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanodac82282020-07-03 08:07:17 -0300866\t.gpio_bank_name\t\t= "a",
867\t.gpio_controller\t= true,
868\t.sandbox_gpio_count\t= 0x14,
869};
Simon Glass20e442a2020-12-28 20:34:54 -0700870U_BOOT_DRVINFO(gpios_at_0) = {
Walter Lozanodac82282020-07-03 08:07:17 -0300871\t.name\t\t= "sandbox_gpio",
Simon Glass9763e4e2021-02-03 06:01:19 -0700872\t.plat\t\t= &dtv_gpios_at_0,
Simon Glass4f500862020-12-03 16:55:19 -0700873\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glasse41651f2020-10-03 11:31:35 -0600874\t.parent_idx\t= -1,
Walter Lozanodac82282020-07-03 08:07:17 -0300875};
876
877''', data)
878
Walter Lozano361e7332020-06-25 01:10:08 -0300879 def test_invalid_driver(self):
880 """Test output from a device tree file with an invalid driver"""
881 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -0700882 output = tools.get_output_filename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700883 with test_util.capture_sys_output() as _:
Simon Glass4a092352021-02-03 06:01:12 -0700884 dtb_platdata.run_steps(
885 ['struct'], dtb_file, False, output, [], None, False,
886 scan=copy_scan())
Walter Lozano361e7332020-06-25 01:10:08 -0300887 with open(output) as infile:
888 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700889 self._check_strings(HEADER + '''
Walter Lozano361e7332020-06-25 01:10:08 -0300890struct dtd_invalid {
891};
892''', data)
893
Simon Glass67b5ec52020-12-28 20:34:47 -0700894 with test_util.capture_sys_output() as _:
Simon Glass4a092352021-02-03 06:01:12 -0700895 dtb_platdata.run_steps(
896 ['platdata'], dtb_file, False, output, [], None, False,
897 scan=copy_scan())
Walter Lozano361e7332020-06-25 01:10:08 -0300898 with open(output) as infile:
899 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700900 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -0700901/*
902 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
903 *
904 * idx driver_info driver
905 * --- -------------------- --------------------
906 * 0: spl_test invalid
907 * --- -------------------- --------------------
908 */
909
Simon Glass1b272732020-10-03 11:31:25 -0600910/* Node /spl-test index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300911static struct dtd_invalid dtv_spl_test = {
Walter Lozano361e7332020-06-25 01:10:08 -0300912};
Simon Glass20e442a2020-12-28 20:34:54 -0700913U_BOOT_DRVINFO(spl_test) = {
Walter Lozano361e7332020-06-25 01:10:08 -0300914\t.name\t\t= "invalid",
Simon Glass9763e4e2021-02-03 06:01:19 -0700915\t.plat\t\t= &dtv_spl_test,
Simon Glass4f500862020-12-03 16:55:19 -0700916\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glasse41651f2020-10-03 11:31:35 -0600917\t.parent_idx\t= -1,
Walter Lozano361e7332020-06-25 01:10:08 -0300918};
919
920''', data)
921
Simon Glassc0791922017-06-18 22:09:06 -0600922 def test_phandle(self):
923 """Test output from a node containing a phandle reference"""
924 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -0700925 output = tools.get_output_filename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300926 self.run_test(['struct'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600927 with open(output) as infile:
928 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700929 self._check_strings(HEADER + '''
Simon Glassc0791922017-06-18 22:09:06 -0600930struct dtd_source {
Simon Glass634eba42017-08-29 14:15:59 -0600931\tstruct phandle_2_arg clocks[4];
Simon Glass8f5afe22023-01-11 16:10:18 -0700932\tunsigned char phandle_name_offset[13];
Simon Glassc0791922017-06-18 22:09:06 -0600933};
934struct dtd_target {
935\tfdt32_t\t\tintval;
936};
937''', data)
938
Walter Lozano361e7332020-06-25 01:10:08 -0300939 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600940 with open(output) as infile:
941 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700942 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -0700943/*
944 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
945 *
946 * idx driver_info driver
947 * --- -------------------- --------------------
948 * 0: phandle2_target target
949 * 1: phandle3_target target
950 * 2: phandle_source source
951 * 3: phandle_source2 source
952 * 4: phandle_target target
953 * --- -------------------- --------------------
954 */
955
Simon Glass1b272732020-10-03 11:31:25 -0600956/* Node /phandle2-target index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300957static struct dtd_target dtv_phandle2_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600958\t.intval\t\t\t= 0x1,
959};
Simon Glass20e442a2020-12-28 20:34:54 -0700960U_BOOT_DRVINFO(phandle2_target) = {
Simon Glass634eba42017-08-29 14:15:59 -0600961\t.name\t\t= "target",
Simon Glass9763e4e2021-02-03 06:01:19 -0700962\t.plat\t\t= &dtv_phandle2_target,
Simon Glass4f500862020-12-03 16:55:19 -0700963\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600964\t.parent_idx\t= -1,
Simon Glass634eba42017-08-29 14:15:59 -0600965};
966
Simon Glass1b272732020-10-03 11:31:25 -0600967/* Node /phandle3-target index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300968static struct dtd_target dtv_phandle3_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600969\t.intval\t\t\t= 0x2,
970};
Simon Glass20e442a2020-12-28 20:34:54 -0700971U_BOOT_DRVINFO(phandle3_target) = {
Simon Glass634eba42017-08-29 14:15:59 -0600972\t.name\t\t= "target",
Simon Glass9763e4e2021-02-03 06:01:19 -0700973\t.plat\t\t= &dtv_phandle3_target,
Simon Glass4f500862020-12-03 16:55:19 -0700974\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600975\t.parent_idx\t= -1,
Simon Glass634eba42017-08-29 14:15:59 -0600976};
977
Simon Glass1b272732020-10-03 11:31:25 -0600978/* Node /phandle-source index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300979static struct dtd_source dtv_phandle_source = {
Simon Glass35d50372017-08-29 14:15:57 -0600980\t.clocks\t\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600981\t\t\t{4, {}},
982\t\t\t{0, {11}},
983\t\t\t{1, {12, 13}},
984\t\t\t{4, {}},},
Simon Glass8f5afe22023-01-11 16:10:18 -0700985\t.phandle_name_offset = {0x0, 0x0, 0x0, 0x3, 0x66, 0x72, 0x65, 0x64,
986\t\t0x0, 0x0, 0x0, 0x0, 0x7b},
Simon Glassc0791922017-06-18 22:09:06 -0600987};
Simon Glass20e442a2020-12-28 20:34:54 -0700988U_BOOT_DRVINFO(phandle_source) = {
Simon Glassc0791922017-06-18 22:09:06 -0600989\t.name\t\t= "source",
Simon Glass9763e4e2021-02-03 06:01:19 -0700990\t.plat\t\t= &dtv_phandle_source,
Simon Glass4f500862020-12-03 16:55:19 -0700991\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glasse41651f2020-10-03 11:31:35 -0600992\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600993};
994
Simon Glass1b272732020-10-03 11:31:25 -0600995/* Node /phandle-source2 index 3 */
Walter Lozano51f12632020-06-25 01:10:13 -0300996static struct dtd_source dtv_phandle_source2 = {
Simon Glass760b7172018-07-06 10:27:31 -0600997\t.clocks\t\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600998\t\t\t{4, {}},},
Simon Glass760b7172018-07-06 10:27:31 -0600999};
Simon Glass20e442a2020-12-28 20:34:54 -07001000U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass760b7172018-07-06 10:27:31 -06001001\t.name\t\t= "source",
Simon Glass9763e4e2021-02-03 06:01:19 -07001002\t.plat\t\t= &dtv_phandle_source2,
Simon Glass4f500862020-12-03 16:55:19 -07001003\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glasse41651f2020-10-03 11:31:35 -06001004\t.parent_idx\t= -1,
Simon Glass760b7172018-07-06 10:27:31 -06001005};
1006
Simon Glass9eca08d2020-12-28 20:35:04 -07001007/* Node /phandle-target index 4 */
1008static struct dtd_target dtv_phandle_target = {
1009\t.intval\t\t\t= 0x0,
1010};
1011U_BOOT_DRVINFO(phandle_target) = {
1012\t.name\t\t= "target",
Simon Glass9763e4e2021-02-03 06:01:19 -07001013\t.plat\t\t= &dtv_phandle_target,
Simon Glass9eca08d2020-12-28 20:35:04 -07001014\t.plat_size\t= sizeof(dtv_phandle_target),
1015\t.parent_idx\t= -1,
1016};
1017
Simon Glassc0791922017-06-18 22:09:06 -06001018''', data)
1019
Simon Glass8512ea22018-07-06 10:27:35 -06001020 def test_phandle_single(self):
1021 """Test output from a node containing a phandle reference"""
1022 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001023 output = tools.get_output_filename('output')
Walter Lozano361e7332020-06-25 01:10:08 -03001024 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -06001025 with open(output) as infile:
1026 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001027 self._check_strings(HEADER + '''
Simon Glass8512ea22018-07-06 10:27:35 -06001028struct dtd_source {
1029\tstruct phandle_0_arg clocks[1];
1030};
1031struct dtd_target {
1032\tfdt32_t\t\tintval;
1033};
1034''', data)
1035
1036 def test_phandle_reorder(self):
1037 """Test that phandle targets are generated before their references"""
1038 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001039 output = tools.get_output_filename('output')
Walter Lozano361e7332020-06-25 01:10:08 -03001040 self.run_test(['platdata'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -06001041 with open(output) as infile:
1042 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001043 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -07001044/*
1045 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1046 *
1047 * idx driver_info driver
1048 * --- -------------------- --------------------
1049 * 0: phandle_source2 source
1050 * 1: phandle_target target
1051 * --- -------------------- --------------------
1052 */
1053
Simon Glass1b272732020-10-03 11:31:25 -06001054/* Node /phandle-source2 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -03001055static struct dtd_source dtv_phandle_source2 = {
Simon Glass8512ea22018-07-06 10:27:35 -06001056\t.clocks\t\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -06001057\t\t\t{1, {}},},
Simon Glass8512ea22018-07-06 10:27:35 -06001058};
Simon Glass20e442a2020-12-28 20:34:54 -07001059U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass8512ea22018-07-06 10:27:35 -06001060\t.name\t\t= "source",
Simon Glass9763e4e2021-02-03 06:01:19 -07001061\t.plat\t\t= &dtv_phandle_source2,
Simon Glass4f500862020-12-03 16:55:19 -07001062\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glasse41651f2020-10-03 11:31:35 -06001063\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -06001064};
1065
Simon Glass9eca08d2020-12-28 20:35:04 -07001066/* Node /phandle-target index 1 */
1067static struct dtd_target dtv_phandle_target = {
1068};
1069U_BOOT_DRVINFO(phandle_target) = {
1070\t.name\t\t= "target",
Simon Glass9763e4e2021-02-03 06:01:19 -07001071\t.plat\t\t= &dtv_phandle_target,
Simon Glass9eca08d2020-12-28 20:35:04 -07001072\t.plat_size\t= sizeof(dtv_phandle_target),
1073\t.parent_idx\t= -1,
1074};
1075
Simon Glass8512ea22018-07-06 10:27:35 -06001076''', data)
1077
Walter Lozano6c3fc502020-06-25 01:10:17 -03001078 def test_phandle_cd_gpio(self):
1079 """Test that phandle targets are generated when unsing cd-gpios"""
1080 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001081 output = tools.get_output_filename('output')
Simon Glass4a092352021-02-03 06:01:12 -07001082 dtb_platdata.run_steps(
1083 ['platdata'], dtb_file, False, output, [], None, False,
1084 warning_disabled=True, scan=copy_scan())
Walter Lozano6c3fc502020-06-25 01:10:17 -03001085 with open(output) as infile:
1086 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001087 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -07001088/*
1089 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1090 *
1091 * idx driver_info driver
1092 * --- -------------------- --------------------
1093 * 0: phandle2_target target
1094 * 1: phandle3_target target
1095 * 2: phandle_source source
1096 * 3: phandle_source2 source
1097 * 4: phandle_target target
1098 * --- -------------------- --------------------
1099 */
1100
Simon Glass1b272732020-10-03 11:31:25 -06001101/* Node /phandle2-target index 0 */
Walter Lozano6c3fc502020-06-25 01:10:17 -03001102static struct dtd_target dtv_phandle2_target = {
1103\t.intval\t\t\t= 0x1,
1104};
Simon Glass20e442a2020-12-28 20:34:54 -07001105U_BOOT_DRVINFO(phandle2_target) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -03001106\t.name\t\t= "target",
Simon Glass9763e4e2021-02-03 06:01:19 -07001107\t.plat\t\t= &dtv_phandle2_target,
Simon Glass4f500862020-12-03 16:55:19 -07001108\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glasse41651f2020-10-03 11:31:35 -06001109\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -03001110};
1111
Simon Glass1b272732020-10-03 11:31:25 -06001112/* Node /phandle3-target index 1 */
Walter Lozano6c3fc502020-06-25 01:10:17 -03001113static struct dtd_target dtv_phandle3_target = {
1114\t.intval\t\t\t= 0x2,
1115};
Simon Glass20e442a2020-12-28 20:34:54 -07001116U_BOOT_DRVINFO(phandle3_target) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -03001117\t.name\t\t= "target",
Simon Glass9763e4e2021-02-03 06:01:19 -07001118\t.plat\t\t= &dtv_phandle3_target,
Simon Glass4f500862020-12-03 16:55:19 -07001119\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glasse41651f2020-10-03 11:31:35 -06001120\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -03001121};
1122
Simon Glass1b272732020-10-03 11:31:25 -06001123/* Node /phandle-source index 2 */
Walter Lozano6c3fc502020-06-25 01:10:17 -03001124static struct dtd_source dtv_phandle_source = {
1125\t.cd_gpios\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -06001126\t\t\t{4, {}},
1127\t\t\t{0, {11}},
1128\t\t\t{1, {12, 13}},
1129\t\t\t{4, {}},},
Walter Lozano6c3fc502020-06-25 01:10:17 -03001130};
Simon Glass20e442a2020-12-28 20:34:54 -07001131U_BOOT_DRVINFO(phandle_source) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -03001132\t.name\t\t= "source",
Simon Glass9763e4e2021-02-03 06:01:19 -07001133\t.plat\t\t= &dtv_phandle_source,
Simon Glass4f500862020-12-03 16:55:19 -07001134\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glasse41651f2020-10-03 11:31:35 -06001135\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -03001136};
1137
Simon Glass1b272732020-10-03 11:31:25 -06001138/* Node /phandle-source2 index 3 */
Walter Lozano6c3fc502020-06-25 01:10:17 -03001139static struct dtd_source dtv_phandle_source2 = {
1140\t.cd_gpios\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -06001141\t\t\t{4, {}},},
Walter Lozano6c3fc502020-06-25 01:10:17 -03001142};
Simon Glass20e442a2020-12-28 20:34:54 -07001143U_BOOT_DRVINFO(phandle_source2) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -03001144\t.name\t\t= "source",
Simon Glass9763e4e2021-02-03 06:01:19 -07001145\t.plat\t\t= &dtv_phandle_source2,
Simon Glass4f500862020-12-03 16:55:19 -07001146\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glasse41651f2020-10-03 11:31:35 -06001147\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -03001148};
1149
Simon Glass9eca08d2020-12-28 20:35:04 -07001150/* Node /phandle-target index 4 */
1151static struct dtd_target dtv_phandle_target = {
1152\t.intval\t\t\t= 0x0,
1153};
1154U_BOOT_DRVINFO(phandle_target) = {
1155\t.name\t\t= "target",
Simon Glass9763e4e2021-02-03 06:01:19 -07001156\t.plat\t\t= &dtv_phandle_target,
Simon Glass9eca08d2020-12-28 20:35:04 -07001157\t.plat_size\t= sizeof(dtv_phandle_target),
1158\t.parent_idx\t= -1,
1159};
1160
Walter Lozano6c3fc502020-06-25 01:10:17 -03001161''', data)
1162
Simon Glass8512ea22018-07-06 10:27:35 -06001163 def test_phandle_bad(self):
1164 """Test a node containing an invalid phandle fails"""
Simon Glass4b4bc062018-10-01 21:12:43 -06001165 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
1166 capture_stderr=True)
Simon Glassc1aa66e2022-01-29 14:14:04 -07001167 output = tools.get_output_filename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -07001168 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -03001169 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -06001170 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
Simon Glass67b5ec52020-12-28 20:34:47 -07001171 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -06001172
1173 def test_phandle_bad2(self):
1174 """Test a phandle target missing its #*-cells property"""
Simon Glass4b4bc062018-10-01 21:12:43 -06001175 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
1176 capture_stderr=True)
Simon Glassc1aa66e2022-01-29 14:14:04 -07001177 output = tools.get_output_filename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -07001178 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -03001179 self.run_test(['struct'], dtb_file, output)
Walter Lozanoad340172020-06-25 01:10:16 -03001180 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glass67b5ec52020-12-28 20:34:47 -07001181 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -06001182
Simon Glassc20ee0e2017-08-29 14:15:50 -06001183 def test_addresses64(self):
1184 """Test output from a node with a 'reg' property with na=2, ns=2"""
1185 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001186 output = tools.get_output_filename('output')
Walter Lozano361e7332020-06-25 01:10:08 -03001187 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001188 with open(output) as infile:
1189 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001190 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -06001191struct dtd_test1 {
1192\tfdt64_t\t\treg[2];
1193};
1194struct dtd_test2 {
1195\tfdt64_t\t\treg[2];
1196};
1197struct dtd_test3 {
1198\tfdt64_t\t\treg[4];
1199};
1200''', data)
1201
Walter Lozano361e7332020-06-25 01:10:08 -03001202 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001203 with open(output) as infile:
1204 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001205 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -07001206/*
1207 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1208 *
1209 * idx driver_info driver
1210 * --- -------------------- --------------------
1211 * 0: test1 test1
1212 * 1: test2 test2
1213 * 2: test3 test3
1214 * --- -------------------- --------------------
1215 */
1216
Simon Glass1b272732020-10-03 11:31:25 -06001217/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -03001218static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001219\t.reg\t\t\t= {0x1234, 0x5678},
1220};
Simon Glass20e442a2020-12-28 20:34:54 -07001221U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001222\t.name\t\t= "test1",
Simon Glass9763e4e2021-02-03 06:01:19 -07001223\t.plat\t\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -07001224\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -06001225\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001226};
1227
Simon Glass1b272732020-10-03 11:31:25 -06001228/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -03001229static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001230\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
1231};
Simon Glass20e442a2020-12-28 20:34:54 -07001232U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001233\t.name\t\t= "test2",
Simon Glass9763e4e2021-02-03 06:01:19 -07001234\t.plat\t\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -07001235\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -06001236\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001237};
1238
Simon Glass1b272732020-10-03 11:31:25 -06001239/* Node /test3 index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -03001240static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001241\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
1242};
Simon Glass20e442a2020-12-28 20:34:54 -07001243U_BOOT_DRVINFO(test3) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001244\t.name\t\t= "test3",
Simon Glass9763e4e2021-02-03 06:01:19 -07001245\t.plat\t\t= &dtv_test3,
Simon Glass4f500862020-12-03 16:55:19 -07001246\t.plat_size\t= sizeof(dtv_test3),
Simon Glasse41651f2020-10-03 11:31:35 -06001247\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001248};
1249
Simon Glassd960f0d2020-12-28 20:35:05 -07001250''', data)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001251
1252 def test_addresses32(self):
1253 """Test output from a node with a 'reg' property with na=1, ns=1"""
1254 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001255 output = tools.get_output_filename('output')
Walter Lozano361e7332020-06-25 01:10:08 -03001256 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001257 with open(output) as infile:
1258 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001259 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -06001260struct dtd_test1 {
1261\tfdt32_t\t\treg[2];
1262};
1263struct dtd_test2 {
1264\tfdt32_t\t\treg[4];
1265};
1266''', data)
1267
Walter Lozano361e7332020-06-25 01:10:08 -03001268 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001269 with open(output) as infile:
1270 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001271 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -07001272/*
1273 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1274 *
1275 * idx driver_info driver
1276 * --- -------------------- --------------------
1277 * 0: test1 test1
1278 * 1: test2 test2
1279 * --- -------------------- --------------------
1280 */
1281
Simon Glass1b272732020-10-03 11:31:25 -06001282/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -03001283static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001284\t.reg\t\t\t= {0x1234, 0x5678},
1285};
Simon Glass20e442a2020-12-28 20:34:54 -07001286U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001287\t.name\t\t= "test1",
Simon Glass9763e4e2021-02-03 06:01:19 -07001288\t.plat\t\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -07001289\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -06001290\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001291};
1292
Simon Glass1b272732020-10-03 11:31:25 -06001293/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -03001294static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001295\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
1296};
Simon Glass20e442a2020-12-28 20:34:54 -07001297U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001298\t.name\t\t= "test2",
Simon Glass9763e4e2021-02-03 06:01:19 -07001299\t.plat\t\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -07001300\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -06001301\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001302};
1303
Simon Glassd960f0d2020-12-28 20:35:05 -07001304''', data)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001305
1306 def test_addresses64_32(self):
1307 """Test output from a node with a 'reg' property with na=2, ns=1"""
1308 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001309 output = tools.get_output_filename('output')
Walter Lozano361e7332020-06-25 01:10:08 -03001310 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001311 with open(output) as infile:
1312 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001313 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -06001314struct dtd_test1 {
1315\tfdt64_t\t\treg[2];
1316};
1317struct dtd_test2 {
1318\tfdt64_t\t\treg[2];
1319};
1320struct dtd_test3 {
1321\tfdt64_t\t\treg[4];
1322};
1323''', data)
1324
Walter Lozano361e7332020-06-25 01:10:08 -03001325 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001326 with open(output) as infile:
1327 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001328 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -07001329/*
1330 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1331 *
1332 * idx driver_info driver
1333 * --- -------------------- --------------------
1334 * 0: test1 test1
1335 * 1: test2 test2
1336 * 2: test3 test3
1337 * --- -------------------- --------------------
1338 */
1339
Simon Glass1b272732020-10-03 11:31:25 -06001340/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -03001341static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001342\t.reg\t\t\t= {0x123400000000, 0x5678},
1343};
Simon Glass20e442a2020-12-28 20:34:54 -07001344U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001345\t.name\t\t= "test1",
Simon Glass9763e4e2021-02-03 06:01:19 -07001346\t.plat\t\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -07001347\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -06001348\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001349};
1350
Simon Glass1b272732020-10-03 11:31:25 -06001351/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -03001352static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001353\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
1354};
Simon Glass20e442a2020-12-28 20:34:54 -07001355U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001356\t.name\t\t= "test2",
Simon Glass9763e4e2021-02-03 06:01:19 -07001357\t.plat\t\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -07001358\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -06001359\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001360};
1361
Simon Glass1b272732020-10-03 11:31:25 -06001362/* Node /test3 index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -03001363static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001364\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
1365};
Simon Glass20e442a2020-12-28 20:34:54 -07001366U_BOOT_DRVINFO(test3) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001367\t.name\t\t= "test3",
Simon Glass9763e4e2021-02-03 06:01:19 -07001368\t.plat\t\t= &dtv_test3,
Simon Glass4f500862020-12-03 16:55:19 -07001369\t.plat_size\t= sizeof(dtv_test3),
Simon Glasse41651f2020-10-03 11:31:35 -06001370\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001371};
1372
Simon Glassd960f0d2020-12-28 20:35:05 -07001373''', data)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001374
1375 def test_addresses32_64(self):
1376 """Test output from a node with a 'reg' property with na=1, ns=2"""
1377 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001378 output = tools.get_output_filename('output')
Walter Lozano361e7332020-06-25 01:10:08 -03001379 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001380 with open(output) as infile:
1381 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001382 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -06001383struct dtd_test1 {
1384\tfdt64_t\t\treg[2];
1385};
1386struct dtd_test2 {
1387\tfdt64_t\t\treg[2];
1388};
1389struct dtd_test3 {
1390\tfdt64_t\t\treg[4];
1391};
1392''', data)
1393
Walter Lozano361e7332020-06-25 01:10:08 -03001394 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001395 with open(output) as infile:
1396 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001397 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -07001398/*
1399 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1400 *
1401 * idx driver_info driver
1402 * --- -------------------- --------------------
1403 * 0: test1 test1
1404 * 1: test2 test2
1405 * 2: test3 test3
1406 * --- -------------------- --------------------
1407 */
1408
Simon Glass1b272732020-10-03 11:31:25 -06001409/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -03001410static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001411\t.reg\t\t\t= {0x1234, 0x567800000000},
1412};
Simon Glass20e442a2020-12-28 20:34:54 -07001413U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001414\t.name\t\t= "test1",
Simon Glass9763e4e2021-02-03 06:01:19 -07001415\t.plat\t\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -07001416\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -06001417\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001418};
1419
Simon Glass1b272732020-10-03 11:31:25 -06001420/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -03001421static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001422\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
1423};
Simon Glass20e442a2020-12-28 20:34:54 -07001424U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001425\t.name\t\t= "test2",
Simon Glass9763e4e2021-02-03 06:01:19 -07001426\t.plat\t\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -07001427\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -06001428\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001429};
1430
Simon Glass1b272732020-10-03 11:31:25 -06001431/* Node /test3 index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -03001432static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001433\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
1434};
Simon Glass20e442a2020-12-28 20:34:54 -07001435U_BOOT_DRVINFO(test3) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001436\t.name\t\t= "test3",
Simon Glass9763e4e2021-02-03 06:01:19 -07001437\t.plat\t\t= &dtv_test3,
Simon Glass4f500862020-12-03 16:55:19 -07001438\t.plat_size\t= sizeof(dtv_test3),
Simon Glasse41651f2020-10-03 11:31:35 -06001439\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001440};
1441
Simon Glassd960f0d2020-12-28 20:35:05 -07001442''', data)
Simon Glass8512ea22018-07-06 10:27:35 -06001443
1444 def test_bad_reg(self):
1445 """Test that a reg property with an invalid type generates an error"""
Simon Glassfe57c782018-07-06 10:27:37 -06001446 # Capture stderr since dtc will emit warnings for this file
1447 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glassc1aa66e2022-01-29 14:14:04 -07001448 output = tools.get_output_filename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -07001449 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -03001450 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -06001451 self.assertIn("Node 'spl-test' reg property is not an int",
Simon Glass67b5ec52020-12-28 20:34:47 -07001452 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -06001453
1454 def test_bad_reg2(self):
1455 """Test that a reg property with an invalid cell count is detected"""
Simon Glassfe57c782018-07-06 10:27:37 -06001456 # Capture stderr since dtc will emit warnings for this file
1457 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glassc1aa66e2022-01-29 14:14:04 -07001458 output = tools.get_output_filename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -07001459 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -03001460 self.run_test(['struct'], dtb_file, output)
Simon Glass67b5ec52020-12-28 20:34:47 -07001461 self.assertIn(
Simon Glassda393412021-03-26 16:17:27 +13001462 "Node 'spl-test' (parent '/') reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
Simon Glass67b5ec52020-12-28 20:34:47 -07001463 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -06001464
1465 def test_add_prop(self):
1466 """Test that a subequent node can add a new property to a struct"""
1467 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001468 output = tools.get_output_filename('output')
Walter Lozano361e7332020-06-25 01:10:08 -03001469 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -06001470 with open(output) as infile:
1471 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001472 self._check_strings(HEADER + '''
Simon Glass8512ea22018-07-06 10:27:35 -06001473struct dtd_sandbox_spl_test {
1474\tfdt32_t\t\tintarray;
1475\tfdt32_t\t\tintval;
1476};
1477''', data)
1478
Walter Lozano361e7332020-06-25 01:10:08 -03001479 self.run_test(['platdata'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -06001480 with open(output) as infile:
1481 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001482 self._check_strings(C_HEADER + '''
Simon Glass4b91be22021-02-03 06:01:15 -07001483/*
Simon Glass9763e4e2021-02-03 06:01:19 -07001484 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1485 *
1486 * idx driver_info driver
1487 * --- -------------------- --------------------
1488 * 0: spl_test sandbox_spl_test
1489 * 1: spl_test2 sandbox_spl_test
1490 * --- -------------------- --------------------
1491 */
1492
1493/*
Simon Glass4b91be22021-02-03 06:01:15 -07001494 * Node /spl-test index 0
1495 * driver sandbox_spl_test parent None
1496 */
Walter Lozano51f12632020-06-25 01:10:13 -03001497static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass8512ea22018-07-06 10:27:35 -06001498\t.intval\t\t\t= 0x1,
1499};
Simon Glass20e442a2020-12-28 20:34:54 -07001500U_BOOT_DRVINFO(spl_test) = {
Simon Glass8512ea22018-07-06 10:27:35 -06001501\t.name\t\t= "sandbox_spl_test",
Simon Glass9763e4e2021-02-03 06:01:19 -07001502\t.plat\t\t= &dtv_spl_test,
Simon Glass4f500862020-12-03 16:55:19 -07001503\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glasse41651f2020-10-03 11:31:35 -06001504\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -06001505};
1506
Simon Glass4b91be22021-02-03 06:01:15 -07001507/*
1508 * Node /spl-test2 index 1
1509 * driver sandbox_spl_test parent None
1510 */
Walter Lozano51f12632020-06-25 01:10:13 -03001511static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass8512ea22018-07-06 10:27:35 -06001512\t.intarray\t\t= 0x5,
1513};
Simon Glass20e442a2020-12-28 20:34:54 -07001514U_BOOT_DRVINFO(spl_test2) = {
Simon Glass8512ea22018-07-06 10:27:35 -06001515\t.name\t\t= "sandbox_spl_test",
Simon Glass9763e4e2021-02-03 06:01:19 -07001516\t.plat\t\t= &dtv_spl_test2,
Simon Glass4f500862020-12-03 16:55:19 -07001517\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glasse41651f2020-10-03 11:31:35 -06001518\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -06001519};
1520
Simon Glassd960f0d2020-12-28 20:35:05 -07001521''', data)
Simon Glass8512ea22018-07-06 10:27:35 -06001522
Simon Glass67b5ec52020-12-28 20:34:47 -07001523 def test_stdout(self):
Simon Glass8512ea22018-07-06 10:27:35 -06001524 """Test output to stdout"""
1525 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glassde846cb2020-12-28 20:34:49 -07001526 with test_util.capture_sys_output() as (stdout, _):
Simon Glassf62cea02020-12-28 20:34:48 -07001527 self.run_test(['struct'], dtb_file, None)
Simon Glassde846cb2020-12-28 20:34:49 -07001528 self._check_strings(self.struct_text, stdout.getvalue())
Simon Glass8512ea22018-07-06 10:27:35 -06001529
Simon Glassbe44f272020-12-28 20:34:51 -07001530 def test_multi_to_file(self):
1531 """Test output of multiple pieces to a single file"""
1532 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001533 output = tools.get_output_filename('output')
Simon Glass10cbd3b2020-12-28 20:34:52 -07001534 self.run_test(['all'], dtb_file, output)
Simon Glassc1aa66e2022-01-29 14:14:04 -07001535 data = tools.read_file(output, binary=False)
Simon Glassd392d322021-02-03 06:01:21 -07001536 self._check_strings(
Simon Glass17073252021-04-27 08:19:48 +12001537 self.decl_text + self.platdata_text + self.struct_text, data)
Simon Glassbe44f272020-12-28 20:34:51 -07001538
Simon Glass67b5ec52020-12-28 20:34:47 -07001539 def test_no_command(self):
Simon Glass8512ea22018-07-06 10:27:35 -06001540 """Test running dtoc without a command"""
Simon Glass67b5ec52020-12-28 20:34:47 -07001541 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -03001542 self.run_test([], '', '')
Simon Glass8512ea22018-07-06 10:27:35 -06001543 self.assertIn("Please specify a command: struct, platdata",
Simon Glass67b5ec52020-12-28 20:34:47 -07001544 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -06001545
Simon Glass67b5ec52020-12-28 20:34:47 -07001546 def test_bad_command(self):
Simon Glass8512ea22018-07-06 10:27:35 -06001547 """Test running dtoc with an invalid command"""
1548 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001549 output = tools.get_output_filename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -07001550 with self.assertRaises(ValueError) as exc:
Simon Glass426d12f2021-02-03 06:01:14 -07001551 self.run_test(['invalid-cmd'], dtb_file, output)
1552 self.assertIn(
Simon Glass8490c572021-03-25 06:40:51 +13001553 "Unknown command 'invalid-cmd': (use: decl, platdata, struct)",
Simon Glass426d12f2021-02-03 06:01:14 -07001554 str(exc.exception))
Walter Lozano6c74d1b2020-07-28 19:06:23 -03001555
Simon Glass10cbd3b2020-12-28 20:34:52 -07001556 def test_output_conflict(self):
1557 """Test a conflict between and output dirs and output file"""
1558 with self.assertRaises(ValueError) as exc:
Simon Glass4a092352021-02-03 06:01:12 -07001559 dtb_platdata.run_steps(
1560 ['all'], None, False, 'out', ['cdir'], None, False,
1561 warning_disabled=True, scan=copy_scan())
Simon Glass10cbd3b2020-12-28 20:34:52 -07001562 self.assertIn("Must specify either output or output_dirs, not both",
1563 str(exc.exception))
1564
Simon Glass17073252021-04-27 08:19:48 +12001565 def check_output_dirs(self, instantiate):
Simon Glass10cbd3b2020-12-28 20:34:52 -07001566 # Remove the directory so that files from other tests are not there
Simon Glassc1aa66e2022-01-29 14:14:04 -07001567 tools._remove_output_dir()
1568 tools.prepare_output_dir(None)
Simon Glass10cbd3b2020-12-28 20:34:52 -07001569
1570 # This should create the .dts and .dtb in the output directory
1571 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001572 outdir = tools.get_output_dir()
Simon Glass10cbd3b2020-12-28 20:34:52 -07001573 fnames = glob.glob(outdir + '/*')
1574 self.assertEqual(2, len(fnames))
1575
Simon Glass4a092352021-02-03 06:01:12 -07001576 dtb_platdata.run_steps(
Simon Glass17073252021-04-27 08:19:48 +12001577 ['all'], dtb_file, False, None, [outdir], None, instantiate,
Simon Glass4a092352021-02-03 06:01:12 -07001578 warning_disabled=True, scan=copy_scan())
Simon Glass10cbd3b2020-12-28 20:34:52 -07001579 fnames = glob.glob(outdir + '/*')
Simon Glass17073252021-04-27 08:19:48 +12001580 return fnames
1581
1582 def test_output_dirs(self):
1583 """Test outputting files to a directory"""
1584 fnames = self.check_output_dirs(False)
1585 self.assertEqual(5, len(fnames))
Simon Glass10cbd3b2020-12-28 20:34:52 -07001586
1587 leafs = set(os.path.basename(fname) for fname in fnames)
1588 self.assertEqual(
Simon Glass426d12f2021-02-03 06:01:14 -07001589 {'dt-structs-gen.h', 'source.dts', 'dt-plat.c', 'source.dtb',
Simon Glass17073252021-04-27 08:19:48 +12001590 'dt-decl.h'},
1591 leafs)
1592
1593 def test_output_dirs_inst(self):
1594 """Test outputting files to a directory with instantiation"""
1595 fnames = self.check_output_dirs(True)
1596 self.assertEqual(6, len(fnames))
1597
1598 leafs = set(os.path.basename(fname) for fname in fnames)
1599 self.assertEqual(
1600 {'dt-structs-gen.h', 'source.dts', 'source.dtb',
Simon Glassd392d322021-02-03 06:01:21 -07001601 'dt-uclass.c', 'dt-decl.h', 'dt-device.c'},
Simon Glass10cbd3b2020-12-28 20:34:52 -07001602 leafs)
Simon Glassfd471e22021-02-03 06:01:00 -07001603
1604 def setup_process_test(self):
1605 """Set up a test of process_nodes()
1606
1607 This uses saved_scan but returns a deep copy of it, so it is safe to
1608 modify it in these tests
1609
1610 Returns:
1611 tuple:
1612 DtbPlatdata: object to test
1613 Scanner: scanner to use
1614 """
1615 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001616 output = tools.get_output_filename('output')
Simon Glassfd471e22021-02-03 06:01:00 -07001617
1618 # Take a copy before messing with it
Simon Glass50aae3e2021-02-03 06:01:11 -07001619 scan = copy_scan()
Simon Glassfd471e22021-02-03 06:01:00 -07001620 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
1621 plat.scan_dtb()
Simon Glass50aae3e2021-02-03 06:01:11 -07001622 plat.scan_tree(False)
Simon Glassfd471e22021-02-03 06:01:00 -07001623 plat.prepare_nodes()
1624 return plat, scan
1625
1626 def test_process_nodes(self):
1627 """Test processing nodes to add various info"""
1628 plat, scan = self.setup_process_test()
1629 plat.process_nodes(True)
1630
1631 i2c_node = plat._fdt.GetNode('/i2c@0')
1632 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
1633 pmic = scan._drivers['sandbox_pmic']
1634 i2c = scan._drivers['sandbox_i2c']
1635 self.assertEqual('DM_DEVICE_REF(pmic_at_9)', pmic_node.dev_ref)
1636 self.assertEqual(pmic, pmic_node.driver)
1637 self.assertEqual(i2c_node, pmic_node.parent)
1638 self.assertEqual(i2c, pmic_node.parent_driver)
1639
1640 # The pmic is the only child
1641 self.assertEqual(pmic_node.parent_seq, 0)
1642 self.assertEqual([pmic_node], i2c_node.child_devs)
1643
1644 # Start and end of the list should be the child_head
1645 ref = '&DM_DEVICE_REF(i2c_at_0)->child_head'
1646 self.assertEqual(
1647 {-1: ref, 0: '&DM_DEVICE_REF(pmic_at_9)->sibling_node', 1: ref},
1648 i2c_node.child_refs)
1649
1650 def test_process_nodes_bad_parent(self):
1651 # Pretend that i2c has a parent (the pmic) and delete that driver
1652 plat, scan = self.setup_process_test()
1653
1654 i2c_node = plat._fdt.GetNode('/i2c@0')
1655 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
1656 del scan._drivers['sandbox_pmic']
1657 i2c_node.parent = pmic_node
1658
1659 # Process twice, the second time to generate an exception
1660 plat.process_nodes(False)
1661 with self.assertRaises(ValueError) as exc:
1662 plat.process_nodes(True)
1663 self.assertIn(
1664 "Cannot parse/find parent driver 'sandbox_pmic' for 'sandbox_i2c",
1665 str(exc.exception))
1666
1667 def test_process_nodes_bad_node(self):
1668 plat, scan = self.setup_process_test()
1669
1670 # Now remove the pmic driver
1671 del scan._drivers['sandbox_pmic']
1672
1673 # Process twice, the second time to generate an exception
1674 plat.process_nodes(False)
1675 with self.assertRaises(ValueError) as exc:
1676 plat.process_nodes(True)
1677 self.assertIn("Cannot parse/find driver for 'sandbox_pmic",
1678 str(exc.exception))
Simon Glassb9319c42021-02-03 06:01:01 -07001679
Simon Glass337d6972021-02-03 06:01:10 -07001680 def test_process_nodes_bad_uclass(self):
1681 plat, scan = self.setup_process_test()
1682
1683 self.assertIn('UCLASS_I2C', scan._uclass)
1684 del scan._uclass['UCLASS_I2C']
1685 with self.assertRaises(ValueError) as exc:
1686 plat.process_nodes(True)
1687 self.assertIn("Cannot parse/find uclass 'UCLASS_I2C' for driver 'sandbox_i2c'",
1688 str(exc.exception))
1689
Simon Glassb9319c42021-02-03 06:01:01 -07001690 def test_process_nodes_used(self):
1691 """Test processing nodes to add various info"""
1692 plat, scan = self.setup_process_test()
1693 plat.process_nodes(True)
1694
1695 pmic = scan._drivers['sandbox_pmic']
1696 self.assertTrue(pmic.used)
1697
1698 gpio = scan._drivers['sandbox_gpio']
1699 self.assertFalse(gpio.used)
Simon Glass05953522021-02-03 06:01:07 -07001700
1701 def test_alias_read(self):
1702 """Test obtaining aliases"""
1703 dtb_file = get_dtb_file('dtoc_test_inst.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001704 output = tools.get_output_filename('output')
Simon Glass05953522021-02-03 06:01:07 -07001705 plat = self.run_test(['struct'], dtb_file, output)
1706
1707 scan = plat._scan
1708 testfdt_node = plat._fdt.GetNode('/some-bus/test')
Simon Glass337d6972021-02-03 06:01:10 -07001709 test0_node = plat._fdt.GetNode('/some-bus/test0')
Simon Glass05953522021-02-03 06:01:07 -07001710 self.assertIn('UCLASS_TEST_FDT', scan._uclass)
1711 uc = scan._uclass['UCLASS_TEST_FDT']
Simon Glass337d6972021-02-03 06:01:10 -07001712 self.assertEqual({1: testfdt_node, 2: test0_node},
1713 uc.alias_num_to_node)
1714 self.assertEqual({'/some-bus/test': 1, '/some-bus/test0': 2},
1715 uc.alias_path_to_num)
Simon Glass05953522021-02-03 06:01:07 -07001716
1717 # Try adding an alias that doesn't exist
1718 self.assertFalse(scan.add_uclass_alias('fred', 3, None))
1719
1720 # Try adding an alias for a missing node
1721 self.assertIsNone(scan.add_uclass_alias('testfdt', 3, None))
1722
1723 def test_alias_read_bad(self):
1724 """Test invalid alias property name"""
1725 dtb_file = get_dtb_file('dtoc_test_alias_bad.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001726 output = tools.get_output_filename('output')
Simon Glass05953522021-02-03 06:01:07 -07001727 with self.assertRaises(ValueError) as exc:
1728 plat = self.run_test(['struct'], dtb_file, output)
1729 self.assertIn("Cannot decode alias 'i2c4-'", str(exc.exception))
1730
1731 def test_alias_read_bad_path(self):
1732 """Test alias pointing to a non-existent node"""
1733 # This line may produce a warning, so capture it:
1734 # Warning (alias_paths): /aliases:i2c4: aliases property is not a valid
1735 # node (/does/not/exist)
1736 dtb_file = get_dtb_file('dtoc_test_alias_bad_path.dts', True)
1737
Simon Glassc1aa66e2022-01-29 14:14:04 -07001738 output = tools.get_output_filename('output')
Simon Glass05953522021-02-03 06:01:07 -07001739 with self.assertRaises(ValueError) as exc:
1740 plat = self.run_test(['struct'], dtb_file, output)
1741 self.assertIn("Alias 'i2c4' path '/does/not/exist' not found",
1742 str(exc.exception))
1743
1744 def test_alias_read_bad_uclass(self):
1745 """Test alias for a uclass that doesn't exist"""
1746 dtb_file = get_dtb_file('dtoc_test_alias_bad_uc.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001747 output = tools.get_output_filename('output')
Simon Glass05953522021-02-03 06:01:07 -07001748 with test_util.capture_sys_output() as (stdout, _):
1749 plat = self.run_test(['struct'], dtb_file, output)
1750 self.assertEqual("Could not find uclass for alias 'other1'",
1751 stdout.getvalue().strip())
Simon Glass074197a2021-02-03 06:01:09 -07001752
1753 def test_sequence(self):
1754 """Test assignment of sequence numnbers"""
1755 dtb_file = get_dtb_file('dtoc_test_inst.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001756 output = tools.get_output_filename('output')
Simon Glass074197a2021-02-03 06:01:09 -07001757 plat = self.run_test(['struct'], dtb_file, output)
Simon Glass337d6972021-02-03 06:01:10 -07001758
1759 scan = plat._scan
1760 testfdt = plat._fdt.GetNode('/some-bus/test')
1761 self.assertEqual(1, testfdt.seq)
1762 i2c = plat._fdt.GetNode('/i2c')
1763
1764 # For now this uclass is not compiled in, so no sequence is assigned
1765 self.assertEqual(4, i2c.seq)
1766 spl = plat._fdt.GetNode('/spl-test')
1767 self.assertEqual(0, spl.seq)
Simon Glass50aae3e2021-02-03 06:01:11 -07001768
1769 def test_process_root(self):
1770 """Test assignment of sequence numnbers"""
1771 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001772 output = tools.get_output_filename('output')
Simon Glass50aae3e2021-02-03 06:01:11 -07001773
1774 # Take a copy before messing with it
1775 scan = copy_scan()
1776 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
1777 plat.scan_dtb()
1778 root = plat._fdt.GetRoot()
1779
1780 plat.scan_tree(False)
1781 self.assertNotIn(root, plat._valid_nodes)
1782
1783 plat.scan_tree(True)
1784 self.assertIn(root, plat._valid_nodes)
1785 self.assertEqual('root_driver',
1786 scan.get_normalized_compat_name(root)[0])
Simon Glass426d12f2021-02-03 06:01:14 -07001787
1788 def test_simple_inst(self):
1789 """Test output from some simple nodes with instantiate enabled"""
Simon Glassea74c952021-02-03 06:01:20 -07001790 dtb_file = get_dtb_file('dtoc_test_inst.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001791 output = tools.get_output_filename('output')
Simon Glass426d12f2021-02-03 06:01:14 -07001792
1793 self.run_test(['decl'], dtb_file, output, True)
1794 with open(output) as infile:
1795 data = infile.read()
1796
1797 self._check_strings(self.decl_text_inst, data)
Simon Glass4b91be22021-02-03 06:01:15 -07001798
Simon Glassea74c952021-02-03 06:01:20 -07001799 self.run_test(['uclass'], dtb_file, output, True)
1800 with open(output) as infile:
1801 data = infile.read()
1802
1803 self._check_strings(UCLASS_HEADER_COMMON + self.uclass_text_inst, data)
1804
Simon Glassd392d322021-02-03 06:01:21 -07001805 self.run_test(['device'], dtb_file, output, True)
1806 with open(output) as infile:
1807 data = infile.read()
1808
1809 self._check_strings(self.device_text_inst, data)
1810
Simon Glassea74c952021-02-03 06:01:20 -07001811 def test_inst_no_hdr(self):
Simon Glassd392d322021-02-03 06:01:21 -07001812 """Test dealing with a struct tsssshat has no header"""
Simon Glassea74c952021-02-03 06:01:20 -07001813 dtb_file = get_dtb_file('dtoc_test_inst.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001814 output = tools.get_output_filename('output')
Simon Glassea74c952021-02-03 06:01:20 -07001815
1816 # Run it once to set everything up
1817 plat = self.run_test(['decl'], dtb_file, output, True)
1818 scan = plat._scan
1819
1820 # Restart the output file and delete any record of the uclass' struct
1821 plat.setup_output(Ftype.SOURCE, output)
1822 del scan._structs['dm_test_uc_priv']
1823
1824 # Now generate the uclasses, which should provide a warning
1825 with test_util.capture_sys_output() as (stdout, _):
1826 plat.generate_uclasses()
1827 self.assertEqual(
1828 'Warning: Cannot find header file for struct dm_test_uc_priv',
1829 stdout.getvalue().strip())
Simon Glassda393412021-03-26 16:17:27 +13001830
1831 def test_missing_props(self):
1832 """Test detection of a parent node with no properties"""
1833 dtb_file = get_dtb_file('dtoc_test_noprops.dts', capture_stderr=True)
Simon Glassc1aa66e2022-01-29 14:14:04 -07001834 output = tools.get_output_filename('output')
Simon Glassda393412021-03-26 16:17:27 +13001835 with self.assertRaises(ValueError) as exc:
1836 self.run_test(['struct'], dtb_file, output)
1837 self.assertIn("Parent node '/i2c@0' has no properties - do you need",
1838 str(exc.exception))
1839
1840 def test_single_reg(self):
1841 """Test detection of a parent node with no properties"""
1842 dtb_file = get_dtb_file('dtoc_test_single_reg.dts')
Simon Glassc1aa66e2022-01-29 14:14:04 -07001843 output = tools.get_output_filename('output')
Simon Glassda393412021-03-26 16:17:27 +13001844 self.run_test(['struct'], dtb_file, output)
Simon Glass1c564692021-06-27 17:51:10 -06001845
1846 def test_missing_parent(self):
1847 """Test detection of a parent node with no properties"""
1848 dtb_file = get_dtb_file('dtoc_test_noparent.dts', capture_stderr=True)
Simon Glassc1aa66e2022-01-29 14:14:04 -07001849 output = tools.get_output_filename('output')
Simon Glass1c564692021-06-27 17:51:10 -06001850 with self.assertRaises(ValueError) as exc:
1851 self.run_test(['device'], dtb_file, output, instantiate=True)
1852 self.assertIn("Node '/i2c@0/spl-test/pmic@9' requires parent node "
1853 "'/i2c@0/spl-test' but it is not in the valid list",
1854 str(exc.exception))