blob: ee17b8daf9a1db4aff1ab9c2900965ced73a2338 [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 Glassea74c952021-02-03 06:01:20 -070019from dtb_platdata import Ftype
Simon Glassc0791922017-06-18 22:09:06 -060020from dtb_platdata import get_value
21from dtb_platdata import tab_to
Simon Glass67b5ec52020-12-28 20:34:47 -070022from dtoc import dtb_platdata
Simon Glassbf776672020-04-17 18:09:04 -060023from dtoc import fdt
24from dtoc import fdt_util
Simon Glassa32eb7d2021-02-03 06:00:51 -070025from dtoc import src_scan
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 Glassbf776672020-04-17 18:09:04 -060028from patman import test_util
29from patman 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):
115 tools.PrepareOutputDir(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 Glass67b5ec52020-12-28 20:34:47 -0700120 tools.FinaliseOutputDir()
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')
221 output = tools.GetOutputFilename('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>
619u8 _denx_u_boot_test_bus_ucplat_some_bus[sizeof(struct dm_test_uclass_priv)]
620\t__attribute__ ((section (".priv_data")));
621#include <test.h>
622
623DM_DEVICE_INST(some_bus) = {
624\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_test_bus),
625\t.name\t\t= "denx_u_boot_test_bus",
626\t.plat_\t\t= &_denx_u_boot_test_bus_plat_some_bus,
627\t.uclass_plat_\t= _denx_u_boot_test_bus_ucplat_some_bus,
628\t.driver_data\t= DM_TEST_TYPE_FIRST,
629\t.priv_\t\t= _denx_u_boot_test_bus_priv_some_bus,
630\t.uclass\t\t= DM_UCLASS_REF(testbus),
631\t.uclass_node\t= {
632\t\t.prev = &DM_UCLASS_REF(testbus)->dev_head,
633\t\t.next = &DM_UCLASS_REF(testbus)->dev_head,
634\t},
635\t.child_head\t= {
636\t\t.prev = &DM_DEVICE_REF(test0)->sibling_node,
637\t\t.next = &DM_DEVICE_REF(test)->sibling_node,
638\t},
639\t.sibling_node\t= {
640\t\t.prev = &DM_DEVICE_REF(i2c)->sibling_node,
641\t\t.next = &DM_DEVICE_REF(spl_test)->sibling_node,
642\t},
643\t.seq_ = 2,
644};
645
646/*
647 * Node /spl-test index 3
648 * driver sandbox_spl_test parent root_driver
649*/
650static struct dtd_sandbox_spl_test dtv_spl_test = {
651\t.boolval\t\t= true,
652\t.intval\t\t\t= 0x1,
653};
654
655DM_DEVICE_INST(spl_test) = {
656\t.driver\t\t= DM_DRIVER_REF(sandbox_spl_test),
657\t.name\t\t= "sandbox_spl_test",
658\t.plat_\t\t= &dtv_spl_test,
659\t.uclass\t\t= DM_UCLASS_REF(misc),
660\t.uclass_node\t= {
661\t\t.prev = &DM_UCLASS_REF(misc)->dev_head,
662\t\t.next = &DM_DEVICE_REF(spl_test3)->uclass_node,
663\t},
664\t.child_head\t= {
665\t\t.prev = &DM_DEVICE_REF(spl_test)->child_head,
666\t\t.next = &DM_DEVICE_REF(spl_test)->child_head,
667\t},
668\t.sibling_node\t= {
669\t\t.prev = &DM_DEVICE_REF(some_bus)->sibling_node,
670\t\t.next = &DM_DEVICE_REF(spl_test3)->sibling_node,
671\t},
672\t.seq_ = 0,
673};
674
675/*
676 * Node /spl-test3 index 4
677 * driver sandbox_spl_test parent root_driver
678*/
679static struct dtd_sandbox_spl_test dtv_spl_test3 = {
680\t.longbytearray\t\t= {0x90a0b0c, 0xd0e0f10},
681\t.stringarray\t\t= "one",
682};
683
684DM_DEVICE_INST(spl_test3) = {
685\t.driver\t\t= DM_DRIVER_REF(sandbox_spl_test),
686\t.name\t\t= "sandbox_spl_test",
687\t.plat_\t\t= &dtv_spl_test3,
688\t.uclass\t\t= DM_UCLASS_REF(misc),
689\t.uclass_node\t= {
690\t\t.prev = &DM_DEVICE_REF(spl_test)->uclass_node,
691\t\t.next = &DM_UCLASS_REF(misc)->dev_head,
692\t},
693\t.child_head\t= {
694\t\t.prev = &DM_DEVICE_REF(spl_test3)->child_head,
695\t\t.next = &DM_DEVICE_REF(spl_test3)->child_head,
696\t},
697\t.sibling_node\t= {
698\t\t.prev = &DM_DEVICE_REF(spl_test)->sibling_node,
699\t\t.next = &DM_DEVICE_REF(root)->child_head,
700\t},
701\t.seq_ = 1,
702};
703
704/*
705 * Node /some-bus/test index 5
706 * driver denx_u_boot_fdt_test parent denx_u_boot_test_bus
707*/
708
709#include <dm/test.h>
710struct dm_test_pdata __attribute__ ((section (".priv_data")))
711\t_denx_u_boot_fdt_test_plat_test = {
712\t.dtplat = {
713\t\t.ping_add\t\t= 0x5,
714\t\t.ping_expect\t\t= 0x5,
Simon Glass3e200ca2021-03-26 16:17:26 +1300715\t\t.reg\t\t\t= {0x5},
Simon Glassd392d322021-02-03 06:01:21 -0700716\t},
717};
718#include <dm/test.h>
719u8 _denx_u_boot_fdt_test_priv_test[sizeof(struct dm_test_priv)]
720\t__attribute__ ((section (".priv_data")));
721#include <dm/test.h>
722u8 _denx_u_boot_fdt_test_parent_plat_test[sizeof(struct dm_test_parent_plat)]
723\t__attribute__ ((section (".priv_data")));
724#include <dm/test.h>
725u8 _denx_u_boot_fdt_test_parent_priv_test[sizeof(struct dm_test_parent_data)]
726\t__attribute__ ((section (".priv_data")));
727
728DM_DEVICE_INST(test) = {
729\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_fdt_test),
730\t.name\t\t= "denx_u_boot_fdt_test",
731\t.plat_\t\t= &_denx_u_boot_fdt_test_plat_test,
732\t.parent_plat_\t= _denx_u_boot_fdt_test_parent_plat_test,
733\t.driver_data\t= DM_TEST_TYPE_FIRST,
734\t.parent\t\t= DM_DEVICE_REF(some_bus),
735\t.priv_\t\t= _denx_u_boot_fdt_test_priv_test,
736\t.uclass\t\t= DM_UCLASS_REF(testfdt),
737\t.parent_priv_\t= _denx_u_boot_fdt_test_parent_priv_test,
738\t.uclass_node\t= {
739\t\t.prev = &DM_UCLASS_REF(testfdt)->dev_head,
740\t\t.next = &DM_DEVICE_REF(test0)->uclass_node,
741\t},
742\t.child_head\t= {
743\t\t.prev = &DM_DEVICE_REF(test)->child_head,
744\t\t.next = &DM_DEVICE_REF(test)->child_head,
745\t},
746\t.sibling_node\t= {
747\t\t.prev = &DM_DEVICE_REF(some_bus)->child_head,
748\t\t.next = &DM_DEVICE_REF(test0)->sibling_node,
749\t},
750\t.seq_ = 1,
751};
752
753/*
754 * Node /some-bus/test0 index 6
755 * driver denx_u_boot_fdt_test parent denx_u_boot_test_bus
756*/
757
758#include <dm/test.h>
759struct dm_test_pdata __attribute__ ((section (".priv_data")))
760\t_denx_u_boot_fdt_test_plat_test0 = {
761\t.dtplat = {
762\t},
763};
764#include <dm/test.h>
765u8 _denx_u_boot_fdt_test_priv_test0[sizeof(struct dm_test_priv)]
766\t__attribute__ ((section (".priv_data")));
767#include <dm/test.h>
768u8 _denx_u_boot_fdt_test_parent_plat_test0[sizeof(struct dm_test_parent_plat)]
769\t__attribute__ ((section (".priv_data")));
770#include <dm/test.h>
771u8 _denx_u_boot_fdt_test_parent_priv_test0[sizeof(struct dm_test_parent_data)]
772\t__attribute__ ((section (".priv_data")));
773
774DM_DEVICE_INST(test0) = {
775\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_fdt_test),
776\t.name\t\t= "denx_u_boot_fdt_test",
777\t.plat_\t\t= &_denx_u_boot_fdt_test_plat_test0,
778\t.parent_plat_\t= _denx_u_boot_fdt_test_parent_plat_test0,
779\t.driver_data\t= DM_TEST_TYPE_SECOND,
780\t.parent\t\t= DM_DEVICE_REF(some_bus),
781\t.priv_\t\t= _denx_u_boot_fdt_test_priv_test0,
782\t.uclass\t\t= DM_UCLASS_REF(testfdt),
783\t.parent_priv_\t= _denx_u_boot_fdt_test_parent_priv_test0,
784\t.uclass_node\t= {
785\t\t.prev = &DM_DEVICE_REF(test)->uclass_node,
786\t\t.next = &DM_UCLASS_REF(testfdt)->dev_head,
787\t},
788\t.child_head\t= {
789\t\t.prev = &DM_DEVICE_REF(test0)->child_head,
790\t\t.next = &DM_DEVICE_REF(test0)->child_head,
791\t},
792\t.sibling_node\t= {
793\t\t.prev = &DM_DEVICE_REF(test)->sibling_node,
794\t\t.next = &DM_DEVICE_REF(some_bus)->child_head,
795\t},
796\t.seq_ = 2,
797};
798
799'''
Simon Glassde846cb2020-12-28 20:34:49 -0700800
801 def test_simple(self):
802 """Test output from some simple nodes with various types of data"""
803 dtb_file = get_dtb_file('dtoc_test_simple.dts')
804 output = tools.GetOutputFilename('output')
805 self.run_test(['struct'], dtb_file, output)
806 with open(output) as infile:
807 data = infile.read()
808
809 self._check_strings(self.struct_text, data)
810
811 self.run_test(['platdata'], dtb_file, output)
812 with open(output) as infile:
813 data = infile.read()
814
815 self._check_strings(self.platdata_text, data)
Simon Glassc0791922017-06-18 22:09:06 -0600816
Simon Glass426d12f2021-02-03 06:01:14 -0700817 self.run_test(['decl'], dtb_file, output)
818 with open(output) as infile:
819 data = infile.read()
820
821 self._check_strings(self.decl_text, data)
822
Simon Glass10cbd3b2020-12-28 20:34:52 -0700823 # Try the 'all' command
824 self.run_test(['all'], dtb_file, output)
825 data = tools.ReadFile(output, binary=False)
Simon Glassd392d322021-02-03 06:01:21 -0700826 self._check_strings(
Simon Glass17073252021-04-27 08:19:48 +1200827 self.decl_text + self.platdata_text + self.struct_text, data)
Simon Glass10cbd3b2020-12-28 20:34:52 -0700828
Walter Lozanodac82282020-07-03 08:07:17 -0300829 def test_driver_alias(self):
830 """Test output from a device tree file with a driver alias"""
831 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
832 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300833 self.run_test(['struct'], dtb_file, output)
Walter Lozanodac82282020-07-03 08:07:17 -0300834 with open(output) as infile:
835 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700836 self._check_strings(HEADER + '''
Walter Lozanodac82282020-07-03 08:07:17 -0300837struct dtd_sandbox_gpio {
838\tconst char *\tgpio_bank_name;
839\tbool\t\tgpio_controller;
840\tfdt32_t\t\tsandbox_gpio_count;
841};
Walter Lozanodac82282020-07-03 08:07:17 -0300842''', data)
843
Walter Lozano361e7332020-06-25 01:10:08 -0300844 self.run_test(['platdata'], dtb_file, output)
Walter Lozanodac82282020-07-03 08:07:17 -0300845 with open(output) as infile:
846 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700847 self._check_strings(C_HEADER + '''
Simon Glass4b91be22021-02-03 06:01:15 -0700848/*
Simon Glass9763e4e2021-02-03 06:01:19 -0700849 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
850 *
851 * idx driver_info driver
852 * --- -------------------- --------------------
853 * 0: gpios_at_0 sandbox_gpio
854 * --- -------------------- --------------------
855 */
856
857/*
Simon Glass4b91be22021-02-03 06:01:15 -0700858 * Node /gpios@0 index 0
859 * driver sandbox_gpio parent None
860 */
Walter Lozano51f12632020-06-25 01:10:13 -0300861static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanodac82282020-07-03 08:07:17 -0300862\t.gpio_bank_name\t\t= "a",
863\t.gpio_controller\t= true,
864\t.sandbox_gpio_count\t= 0x14,
865};
Simon Glass20e442a2020-12-28 20:34:54 -0700866U_BOOT_DRVINFO(gpios_at_0) = {
Walter Lozanodac82282020-07-03 08:07:17 -0300867\t.name\t\t= "sandbox_gpio",
Simon Glass9763e4e2021-02-03 06:01:19 -0700868\t.plat\t\t= &dtv_gpios_at_0,
Simon Glass4f500862020-12-03 16:55:19 -0700869\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glasse41651f2020-10-03 11:31:35 -0600870\t.parent_idx\t= -1,
Walter Lozanodac82282020-07-03 08:07:17 -0300871};
872
873''', data)
874
Walter Lozano361e7332020-06-25 01:10:08 -0300875 def test_invalid_driver(self):
876 """Test output from a device tree file with an invalid driver"""
877 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
878 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700879 with test_util.capture_sys_output() as _:
Simon Glass4a092352021-02-03 06:01:12 -0700880 dtb_platdata.run_steps(
881 ['struct'], dtb_file, False, output, [], None, False,
882 scan=copy_scan())
Walter Lozano361e7332020-06-25 01:10:08 -0300883 with open(output) as infile:
884 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700885 self._check_strings(HEADER + '''
Walter Lozano361e7332020-06-25 01:10:08 -0300886struct dtd_invalid {
887};
888''', data)
889
Simon Glass67b5ec52020-12-28 20:34:47 -0700890 with test_util.capture_sys_output() as _:
Simon Glass4a092352021-02-03 06:01:12 -0700891 dtb_platdata.run_steps(
892 ['platdata'], dtb_file, False, output, [], None, False,
893 scan=copy_scan())
Walter Lozano361e7332020-06-25 01:10:08 -0300894 with open(output) as infile:
895 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700896 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -0700897/*
898 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
899 *
900 * idx driver_info driver
901 * --- -------------------- --------------------
902 * 0: spl_test invalid
903 * --- -------------------- --------------------
904 */
905
Simon Glass1b272732020-10-03 11:31:25 -0600906/* Node /spl-test index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300907static struct dtd_invalid dtv_spl_test = {
Walter Lozano361e7332020-06-25 01:10:08 -0300908};
Simon Glass20e442a2020-12-28 20:34:54 -0700909U_BOOT_DRVINFO(spl_test) = {
Walter Lozano361e7332020-06-25 01:10:08 -0300910\t.name\t\t= "invalid",
Simon Glass9763e4e2021-02-03 06:01:19 -0700911\t.plat\t\t= &dtv_spl_test,
Simon Glass4f500862020-12-03 16:55:19 -0700912\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glasse41651f2020-10-03 11:31:35 -0600913\t.parent_idx\t= -1,
Walter Lozano361e7332020-06-25 01:10:08 -0300914};
915
916''', data)
917
Simon Glassc0791922017-06-18 22:09:06 -0600918 def test_phandle(self):
919 """Test output from a node containing a phandle reference"""
920 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
921 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300922 self.run_test(['struct'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600923 with open(output) as infile:
924 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700925 self._check_strings(HEADER + '''
Simon Glassc0791922017-06-18 22:09:06 -0600926struct dtd_source {
Simon Glass634eba42017-08-29 14:15:59 -0600927\tstruct phandle_2_arg clocks[4];
Simon Glassc0791922017-06-18 22:09:06 -0600928};
929struct dtd_target {
930\tfdt32_t\t\tintval;
931};
932''', data)
933
Walter Lozano361e7332020-06-25 01:10:08 -0300934 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600935 with open(output) as infile:
936 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700937 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -0700938/*
939 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
940 *
941 * idx driver_info driver
942 * --- -------------------- --------------------
943 * 0: phandle2_target target
944 * 1: phandle3_target target
945 * 2: phandle_source source
946 * 3: phandle_source2 source
947 * 4: phandle_target target
948 * --- -------------------- --------------------
949 */
950
Simon Glass1b272732020-10-03 11:31:25 -0600951/* Node /phandle2-target index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300952static struct dtd_target dtv_phandle2_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600953\t.intval\t\t\t= 0x1,
954};
Simon Glass20e442a2020-12-28 20:34:54 -0700955U_BOOT_DRVINFO(phandle2_target) = {
Simon Glass634eba42017-08-29 14:15:59 -0600956\t.name\t\t= "target",
Simon Glass9763e4e2021-02-03 06:01:19 -0700957\t.plat\t\t= &dtv_phandle2_target,
Simon Glass4f500862020-12-03 16:55:19 -0700958\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600959\t.parent_idx\t= -1,
Simon Glass634eba42017-08-29 14:15:59 -0600960};
961
Simon Glass1b272732020-10-03 11:31:25 -0600962/* Node /phandle3-target index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300963static struct dtd_target dtv_phandle3_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600964\t.intval\t\t\t= 0x2,
965};
Simon Glass20e442a2020-12-28 20:34:54 -0700966U_BOOT_DRVINFO(phandle3_target) = {
Simon Glass634eba42017-08-29 14:15:59 -0600967\t.name\t\t= "target",
Simon Glass9763e4e2021-02-03 06:01:19 -0700968\t.plat\t\t= &dtv_phandle3_target,
Simon Glass4f500862020-12-03 16:55:19 -0700969\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600970\t.parent_idx\t= -1,
Simon Glass634eba42017-08-29 14:15:59 -0600971};
972
Simon Glass1b272732020-10-03 11:31:25 -0600973/* Node /phandle-source index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300974static struct dtd_source dtv_phandle_source = {
Simon Glass35d50372017-08-29 14:15:57 -0600975\t.clocks\t\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600976\t\t\t{4, {}},
977\t\t\t{0, {11}},
978\t\t\t{1, {12, 13}},
979\t\t\t{4, {}},},
Simon Glassc0791922017-06-18 22:09:06 -0600980};
Simon Glass20e442a2020-12-28 20:34:54 -0700981U_BOOT_DRVINFO(phandle_source) = {
Simon Glassc0791922017-06-18 22:09:06 -0600982\t.name\t\t= "source",
Simon Glass9763e4e2021-02-03 06:01:19 -0700983\t.plat\t\t= &dtv_phandle_source,
Simon Glass4f500862020-12-03 16:55:19 -0700984\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glasse41651f2020-10-03 11:31:35 -0600985\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600986};
987
Simon Glass1b272732020-10-03 11:31:25 -0600988/* Node /phandle-source2 index 3 */
Walter Lozano51f12632020-06-25 01:10:13 -0300989static struct dtd_source dtv_phandle_source2 = {
Simon Glass760b7172018-07-06 10:27:31 -0600990\t.clocks\t\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600991\t\t\t{4, {}},},
Simon Glass760b7172018-07-06 10:27:31 -0600992};
Simon Glass20e442a2020-12-28 20:34:54 -0700993U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass760b7172018-07-06 10:27:31 -0600994\t.name\t\t= "source",
Simon Glass9763e4e2021-02-03 06:01:19 -0700995\t.plat\t\t= &dtv_phandle_source2,
Simon Glass4f500862020-12-03 16:55:19 -0700996\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glasse41651f2020-10-03 11:31:35 -0600997\t.parent_idx\t= -1,
Simon Glass760b7172018-07-06 10:27:31 -0600998};
999
Simon Glass9eca08d2020-12-28 20:35:04 -07001000/* Node /phandle-target index 4 */
1001static struct dtd_target dtv_phandle_target = {
1002\t.intval\t\t\t= 0x0,
1003};
1004U_BOOT_DRVINFO(phandle_target) = {
1005\t.name\t\t= "target",
Simon Glass9763e4e2021-02-03 06:01:19 -07001006\t.plat\t\t= &dtv_phandle_target,
Simon Glass9eca08d2020-12-28 20:35:04 -07001007\t.plat_size\t= sizeof(dtv_phandle_target),
1008\t.parent_idx\t= -1,
1009};
1010
Simon Glassc0791922017-06-18 22:09:06 -06001011''', data)
1012
Simon Glass8512ea22018-07-06 10:27:35 -06001013 def test_phandle_single(self):
1014 """Test output from a node containing a phandle reference"""
1015 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
1016 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -03001017 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -06001018 with open(output) as infile:
1019 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001020 self._check_strings(HEADER + '''
Simon Glass8512ea22018-07-06 10:27:35 -06001021struct dtd_source {
1022\tstruct phandle_0_arg clocks[1];
1023};
1024struct dtd_target {
1025\tfdt32_t\t\tintval;
1026};
1027''', data)
1028
1029 def test_phandle_reorder(self):
1030 """Test that phandle targets are generated before their references"""
1031 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
1032 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -03001033 self.run_test(['platdata'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -06001034 with open(output) as infile:
1035 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001036 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -07001037/*
1038 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1039 *
1040 * idx driver_info driver
1041 * --- -------------------- --------------------
1042 * 0: phandle_source2 source
1043 * 1: phandle_target target
1044 * --- -------------------- --------------------
1045 */
1046
Simon Glass1b272732020-10-03 11:31:25 -06001047/* Node /phandle-source2 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -03001048static struct dtd_source dtv_phandle_source2 = {
Simon Glass8512ea22018-07-06 10:27:35 -06001049\t.clocks\t\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -06001050\t\t\t{1, {}},},
Simon Glass8512ea22018-07-06 10:27:35 -06001051};
Simon Glass20e442a2020-12-28 20:34:54 -07001052U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass8512ea22018-07-06 10:27:35 -06001053\t.name\t\t= "source",
Simon Glass9763e4e2021-02-03 06:01:19 -07001054\t.plat\t\t= &dtv_phandle_source2,
Simon Glass4f500862020-12-03 16:55:19 -07001055\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glasse41651f2020-10-03 11:31:35 -06001056\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -06001057};
1058
Simon Glass9eca08d2020-12-28 20:35:04 -07001059/* Node /phandle-target index 1 */
1060static struct dtd_target dtv_phandle_target = {
1061};
1062U_BOOT_DRVINFO(phandle_target) = {
1063\t.name\t\t= "target",
Simon Glass9763e4e2021-02-03 06:01:19 -07001064\t.plat\t\t= &dtv_phandle_target,
Simon Glass9eca08d2020-12-28 20:35:04 -07001065\t.plat_size\t= sizeof(dtv_phandle_target),
1066\t.parent_idx\t= -1,
1067};
1068
Simon Glass8512ea22018-07-06 10:27:35 -06001069''', data)
1070
Walter Lozano6c3fc502020-06-25 01:10:17 -03001071 def test_phandle_cd_gpio(self):
1072 """Test that phandle targets are generated when unsing cd-gpios"""
1073 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
1074 output = tools.GetOutputFilename('output')
Simon Glass4a092352021-02-03 06:01:12 -07001075 dtb_platdata.run_steps(
1076 ['platdata'], dtb_file, False, output, [], None, False,
1077 warning_disabled=True, scan=copy_scan())
Walter Lozano6c3fc502020-06-25 01:10:17 -03001078 with open(output) as infile:
1079 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001080 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -07001081/*
1082 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1083 *
1084 * idx driver_info driver
1085 * --- -------------------- --------------------
1086 * 0: phandle2_target target
1087 * 1: phandle3_target target
1088 * 2: phandle_source source
1089 * 3: phandle_source2 source
1090 * 4: phandle_target target
1091 * --- -------------------- --------------------
1092 */
1093
Simon Glass1b272732020-10-03 11:31:25 -06001094/* Node /phandle2-target index 0 */
Walter Lozano6c3fc502020-06-25 01:10:17 -03001095static struct dtd_target dtv_phandle2_target = {
1096\t.intval\t\t\t= 0x1,
1097};
Simon Glass20e442a2020-12-28 20:34:54 -07001098U_BOOT_DRVINFO(phandle2_target) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -03001099\t.name\t\t= "target",
Simon Glass9763e4e2021-02-03 06:01:19 -07001100\t.plat\t\t= &dtv_phandle2_target,
Simon Glass4f500862020-12-03 16:55:19 -07001101\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glasse41651f2020-10-03 11:31:35 -06001102\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -03001103};
1104
Simon Glass1b272732020-10-03 11:31:25 -06001105/* Node /phandle3-target index 1 */
Walter Lozano6c3fc502020-06-25 01:10:17 -03001106static struct dtd_target dtv_phandle3_target = {
1107\t.intval\t\t\t= 0x2,
1108};
Simon Glass20e442a2020-12-28 20:34:54 -07001109U_BOOT_DRVINFO(phandle3_target) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -03001110\t.name\t\t= "target",
Simon Glass9763e4e2021-02-03 06:01:19 -07001111\t.plat\t\t= &dtv_phandle3_target,
Simon Glass4f500862020-12-03 16:55:19 -07001112\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glasse41651f2020-10-03 11:31:35 -06001113\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -03001114};
1115
Simon Glass1b272732020-10-03 11:31:25 -06001116/* Node /phandle-source index 2 */
Walter Lozano6c3fc502020-06-25 01:10:17 -03001117static struct dtd_source dtv_phandle_source = {
1118\t.cd_gpios\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -06001119\t\t\t{4, {}},
1120\t\t\t{0, {11}},
1121\t\t\t{1, {12, 13}},
1122\t\t\t{4, {}},},
Walter Lozano6c3fc502020-06-25 01:10:17 -03001123};
Simon Glass20e442a2020-12-28 20:34:54 -07001124U_BOOT_DRVINFO(phandle_source) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -03001125\t.name\t\t= "source",
Simon Glass9763e4e2021-02-03 06:01:19 -07001126\t.plat\t\t= &dtv_phandle_source,
Simon Glass4f500862020-12-03 16:55:19 -07001127\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glasse41651f2020-10-03 11:31:35 -06001128\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -03001129};
1130
Simon Glass1b272732020-10-03 11:31:25 -06001131/* Node /phandle-source2 index 3 */
Walter Lozano6c3fc502020-06-25 01:10:17 -03001132static struct dtd_source dtv_phandle_source2 = {
1133\t.cd_gpios\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -06001134\t\t\t{4, {}},},
Walter Lozano6c3fc502020-06-25 01:10:17 -03001135};
Simon Glass20e442a2020-12-28 20:34:54 -07001136U_BOOT_DRVINFO(phandle_source2) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -03001137\t.name\t\t= "source",
Simon Glass9763e4e2021-02-03 06:01:19 -07001138\t.plat\t\t= &dtv_phandle_source2,
Simon Glass4f500862020-12-03 16:55:19 -07001139\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glasse41651f2020-10-03 11:31:35 -06001140\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -03001141};
1142
Simon Glass9eca08d2020-12-28 20:35:04 -07001143/* Node /phandle-target index 4 */
1144static struct dtd_target dtv_phandle_target = {
1145\t.intval\t\t\t= 0x0,
1146};
1147U_BOOT_DRVINFO(phandle_target) = {
1148\t.name\t\t= "target",
Simon Glass9763e4e2021-02-03 06:01:19 -07001149\t.plat\t\t= &dtv_phandle_target,
Simon Glass9eca08d2020-12-28 20:35:04 -07001150\t.plat_size\t= sizeof(dtv_phandle_target),
1151\t.parent_idx\t= -1,
1152};
1153
Walter Lozano6c3fc502020-06-25 01:10:17 -03001154''', data)
1155
Simon Glass8512ea22018-07-06 10:27:35 -06001156 def test_phandle_bad(self):
1157 """Test a node containing an invalid phandle fails"""
Simon Glass4b4bc062018-10-01 21:12:43 -06001158 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
1159 capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -06001160 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -07001161 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -03001162 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -06001163 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
Simon Glass67b5ec52020-12-28 20:34:47 -07001164 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -06001165
1166 def test_phandle_bad2(self):
1167 """Test a phandle target missing its #*-cells property"""
Simon Glass4b4bc062018-10-01 21:12:43 -06001168 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
1169 capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -06001170 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -07001171 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -03001172 self.run_test(['struct'], dtb_file, output)
Walter Lozanoad340172020-06-25 01:10:16 -03001173 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glass67b5ec52020-12-28 20:34:47 -07001174 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -06001175
Simon Glassc20ee0e2017-08-29 14:15:50 -06001176 def test_addresses64(self):
1177 """Test output from a node with a 'reg' property with na=2, ns=2"""
1178 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
1179 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -03001180 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001181 with open(output) as infile:
1182 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001183 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -06001184struct dtd_test1 {
1185\tfdt64_t\t\treg[2];
1186};
1187struct dtd_test2 {
1188\tfdt64_t\t\treg[2];
1189};
1190struct dtd_test3 {
1191\tfdt64_t\t\treg[4];
1192};
1193''', data)
1194
Walter Lozano361e7332020-06-25 01:10:08 -03001195 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001196 with open(output) as infile:
1197 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001198 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -07001199/*
1200 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1201 *
1202 * idx driver_info driver
1203 * --- -------------------- --------------------
1204 * 0: test1 test1
1205 * 1: test2 test2
1206 * 2: test3 test3
1207 * --- -------------------- --------------------
1208 */
1209
Simon Glass1b272732020-10-03 11:31:25 -06001210/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -03001211static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001212\t.reg\t\t\t= {0x1234, 0x5678},
1213};
Simon Glass20e442a2020-12-28 20:34:54 -07001214U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001215\t.name\t\t= "test1",
Simon Glass9763e4e2021-02-03 06:01:19 -07001216\t.plat\t\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -07001217\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -06001218\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001219};
1220
Simon Glass1b272732020-10-03 11:31:25 -06001221/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -03001222static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001223\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
1224};
Simon Glass20e442a2020-12-28 20:34:54 -07001225U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001226\t.name\t\t= "test2",
Simon Glass9763e4e2021-02-03 06:01:19 -07001227\t.plat\t\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -07001228\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -06001229\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001230};
1231
Simon Glass1b272732020-10-03 11:31:25 -06001232/* Node /test3 index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -03001233static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001234\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
1235};
Simon Glass20e442a2020-12-28 20:34:54 -07001236U_BOOT_DRVINFO(test3) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001237\t.name\t\t= "test3",
Simon Glass9763e4e2021-02-03 06:01:19 -07001238\t.plat\t\t= &dtv_test3,
Simon Glass4f500862020-12-03 16:55:19 -07001239\t.plat_size\t= sizeof(dtv_test3),
Simon Glasse41651f2020-10-03 11:31:35 -06001240\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001241};
1242
Simon Glassd960f0d2020-12-28 20:35:05 -07001243''', data)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001244
1245 def test_addresses32(self):
1246 """Test output from a node with a 'reg' property with na=1, ns=1"""
1247 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
1248 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -03001249 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001250 with open(output) as infile:
1251 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001252 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -06001253struct dtd_test1 {
1254\tfdt32_t\t\treg[2];
1255};
1256struct dtd_test2 {
1257\tfdt32_t\t\treg[4];
1258};
1259''', data)
1260
Walter Lozano361e7332020-06-25 01:10:08 -03001261 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001262 with open(output) as infile:
1263 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001264 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -07001265/*
1266 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1267 *
1268 * idx driver_info driver
1269 * --- -------------------- --------------------
1270 * 0: test1 test1
1271 * 1: test2 test2
1272 * --- -------------------- --------------------
1273 */
1274
Simon Glass1b272732020-10-03 11:31:25 -06001275/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -03001276static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001277\t.reg\t\t\t= {0x1234, 0x5678},
1278};
Simon Glass20e442a2020-12-28 20:34:54 -07001279U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001280\t.name\t\t= "test1",
Simon Glass9763e4e2021-02-03 06:01:19 -07001281\t.plat\t\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -07001282\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -06001283\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001284};
1285
Simon Glass1b272732020-10-03 11:31:25 -06001286/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -03001287static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001288\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
1289};
Simon Glass20e442a2020-12-28 20:34:54 -07001290U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001291\t.name\t\t= "test2",
Simon Glass9763e4e2021-02-03 06:01:19 -07001292\t.plat\t\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -07001293\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -06001294\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001295};
1296
Simon Glassd960f0d2020-12-28 20:35:05 -07001297''', data)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001298
1299 def test_addresses64_32(self):
1300 """Test output from a node with a 'reg' property with na=2, ns=1"""
1301 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
1302 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -03001303 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001304 with open(output) as infile:
1305 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001306 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -06001307struct dtd_test1 {
1308\tfdt64_t\t\treg[2];
1309};
1310struct dtd_test2 {
1311\tfdt64_t\t\treg[2];
1312};
1313struct dtd_test3 {
1314\tfdt64_t\t\treg[4];
1315};
1316''', data)
1317
Walter Lozano361e7332020-06-25 01:10:08 -03001318 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001319 with open(output) as infile:
1320 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001321 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -07001322/*
1323 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1324 *
1325 * idx driver_info driver
1326 * --- -------------------- --------------------
1327 * 0: test1 test1
1328 * 1: test2 test2
1329 * 2: test3 test3
1330 * --- -------------------- --------------------
1331 */
1332
Simon Glass1b272732020-10-03 11:31:25 -06001333/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -03001334static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001335\t.reg\t\t\t= {0x123400000000, 0x5678},
1336};
Simon Glass20e442a2020-12-28 20:34:54 -07001337U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001338\t.name\t\t= "test1",
Simon Glass9763e4e2021-02-03 06:01:19 -07001339\t.plat\t\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -07001340\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -06001341\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001342};
1343
Simon Glass1b272732020-10-03 11:31:25 -06001344/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -03001345static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001346\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
1347};
Simon Glass20e442a2020-12-28 20:34:54 -07001348U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001349\t.name\t\t= "test2",
Simon Glass9763e4e2021-02-03 06:01:19 -07001350\t.plat\t\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -07001351\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -06001352\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001353};
1354
Simon Glass1b272732020-10-03 11:31:25 -06001355/* Node /test3 index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -03001356static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001357\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
1358};
Simon Glass20e442a2020-12-28 20:34:54 -07001359U_BOOT_DRVINFO(test3) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001360\t.name\t\t= "test3",
Simon Glass9763e4e2021-02-03 06:01:19 -07001361\t.plat\t\t= &dtv_test3,
Simon Glass4f500862020-12-03 16:55:19 -07001362\t.plat_size\t= sizeof(dtv_test3),
Simon Glasse41651f2020-10-03 11:31:35 -06001363\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001364};
1365
Simon Glassd960f0d2020-12-28 20:35:05 -07001366''', data)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001367
1368 def test_addresses32_64(self):
1369 """Test output from a node with a 'reg' property with na=1, ns=2"""
1370 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
1371 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -03001372 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001373 with open(output) as infile:
1374 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001375 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -06001376struct dtd_test1 {
1377\tfdt64_t\t\treg[2];
1378};
1379struct dtd_test2 {
1380\tfdt64_t\t\treg[2];
1381};
1382struct dtd_test3 {
1383\tfdt64_t\t\treg[4];
1384};
1385''', data)
1386
Walter Lozano361e7332020-06-25 01:10:08 -03001387 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -06001388 with open(output) as infile:
1389 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001390 self._check_strings(C_HEADER + '''
Simon Glass9763e4e2021-02-03 06:01:19 -07001391/*
1392 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1393 *
1394 * idx driver_info driver
1395 * --- -------------------- --------------------
1396 * 0: test1 test1
1397 * 1: test2 test2
1398 * 2: test3 test3
1399 * --- -------------------- --------------------
1400 */
1401
Simon Glass1b272732020-10-03 11:31:25 -06001402/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -03001403static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001404\t.reg\t\t\t= {0x1234, 0x567800000000},
1405};
Simon Glass20e442a2020-12-28 20:34:54 -07001406U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001407\t.name\t\t= "test1",
Simon Glass9763e4e2021-02-03 06:01:19 -07001408\t.plat\t\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -07001409\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -06001410\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001411};
1412
Simon Glass1b272732020-10-03 11:31:25 -06001413/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -03001414static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001415\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
1416};
Simon Glass20e442a2020-12-28 20:34:54 -07001417U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001418\t.name\t\t= "test2",
Simon Glass9763e4e2021-02-03 06:01:19 -07001419\t.plat\t\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -07001420\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -06001421\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001422};
1423
Simon Glass1b272732020-10-03 11:31:25 -06001424/* Node /test3 index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -03001425static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001426\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
1427};
Simon Glass20e442a2020-12-28 20:34:54 -07001428U_BOOT_DRVINFO(test3) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -06001429\t.name\t\t= "test3",
Simon Glass9763e4e2021-02-03 06:01:19 -07001430\t.plat\t\t= &dtv_test3,
Simon Glass4f500862020-12-03 16:55:19 -07001431\t.plat_size\t= sizeof(dtv_test3),
Simon Glasse41651f2020-10-03 11:31:35 -06001432\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -06001433};
1434
Simon Glassd960f0d2020-12-28 20:35:05 -07001435''', data)
Simon Glass8512ea22018-07-06 10:27:35 -06001436
1437 def test_bad_reg(self):
1438 """Test that a reg property with an invalid type generates an error"""
Simon Glassfe57c782018-07-06 10:27:37 -06001439 # Capture stderr since dtc will emit warnings for this file
1440 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -06001441 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -07001442 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -03001443 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -06001444 self.assertIn("Node 'spl-test' reg property is not an int",
Simon Glass67b5ec52020-12-28 20:34:47 -07001445 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -06001446
1447 def test_bad_reg2(self):
1448 """Test that a reg property with an invalid cell count is detected"""
Simon Glassfe57c782018-07-06 10:27:37 -06001449 # Capture stderr since dtc will emit warnings for this file
1450 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -06001451 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -07001452 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -03001453 self.run_test(['struct'], dtb_file, output)
Simon Glass67b5ec52020-12-28 20:34:47 -07001454 self.assertIn(
Simon Glassda393412021-03-26 16:17:27 +13001455 "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 -07001456 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -06001457
1458 def test_add_prop(self):
1459 """Test that a subequent node can add a new property to a struct"""
1460 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
1461 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -03001462 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -06001463 with open(output) as infile:
1464 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001465 self._check_strings(HEADER + '''
Simon Glass8512ea22018-07-06 10:27:35 -06001466struct dtd_sandbox_spl_test {
1467\tfdt32_t\t\tintarray;
1468\tfdt32_t\t\tintval;
1469};
1470''', data)
1471
Walter Lozano361e7332020-06-25 01:10:08 -03001472 self.run_test(['platdata'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -06001473 with open(output) as infile:
1474 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -07001475 self._check_strings(C_HEADER + '''
Simon Glass4b91be22021-02-03 06:01:15 -07001476/*
Simon Glass9763e4e2021-02-03 06:01:19 -07001477 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1478 *
1479 * idx driver_info driver
1480 * --- -------------------- --------------------
1481 * 0: spl_test sandbox_spl_test
1482 * 1: spl_test2 sandbox_spl_test
1483 * --- -------------------- --------------------
1484 */
1485
1486/*
Simon Glass4b91be22021-02-03 06:01:15 -07001487 * Node /spl-test index 0
1488 * driver sandbox_spl_test parent None
1489 */
Walter Lozano51f12632020-06-25 01:10:13 -03001490static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass8512ea22018-07-06 10:27:35 -06001491\t.intval\t\t\t= 0x1,
1492};
Simon Glass20e442a2020-12-28 20:34:54 -07001493U_BOOT_DRVINFO(spl_test) = {
Simon Glass8512ea22018-07-06 10:27:35 -06001494\t.name\t\t= "sandbox_spl_test",
Simon Glass9763e4e2021-02-03 06:01:19 -07001495\t.plat\t\t= &dtv_spl_test,
Simon Glass4f500862020-12-03 16:55:19 -07001496\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glasse41651f2020-10-03 11:31:35 -06001497\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -06001498};
1499
Simon Glass4b91be22021-02-03 06:01:15 -07001500/*
1501 * Node /spl-test2 index 1
1502 * driver sandbox_spl_test parent None
1503 */
Walter Lozano51f12632020-06-25 01:10:13 -03001504static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass8512ea22018-07-06 10:27:35 -06001505\t.intarray\t\t= 0x5,
1506};
Simon Glass20e442a2020-12-28 20:34:54 -07001507U_BOOT_DRVINFO(spl_test2) = {
Simon Glass8512ea22018-07-06 10:27:35 -06001508\t.name\t\t= "sandbox_spl_test",
Simon Glass9763e4e2021-02-03 06:01:19 -07001509\t.plat\t\t= &dtv_spl_test2,
Simon Glass4f500862020-12-03 16:55:19 -07001510\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glasse41651f2020-10-03 11:31:35 -06001511\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -06001512};
1513
Simon Glassd960f0d2020-12-28 20:35:05 -07001514''', data)
Simon Glass8512ea22018-07-06 10:27:35 -06001515
Simon Glass67b5ec52020-12-28 20:34:47 -07001516 def test_stdout(self):
Simon Glass8512ea22018-07-06 10:27:35 -06001517 """Test output to stdout"""
1518 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glassde846cb2020-12-28 20:34:49 -07001519 with test_util.capture_sys_output() as (stdout, _):
Simon Glassf62cea02020-12-28 20:34:48 -07001520 self.run_test(['struct'], dtb_file, None)
Simon Glassde846cb2020-12-28 20:34:49 -07001521 self._check_strings(self.struct_text, stdout.getvalue())
Simon Glass8512ea22018-07-06 10:27:35 -06001522
Simon Glassbe44f272020-12-28 20:34:51 -07001523 def test_multi_to_file(self):
1524 """Test output of multiple pieces to a single file"""
1525 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1526 output = tools.GetOutputFilename('output')
Simon Glass10cbd3b2020-12-28 20:34:52 -07001527 self.run_test(['all'], dtb_file, output)
Simon Glassbe44f272020-12-28 20:34:51 -07001528 data = tools.ReadFile(output, binary=False)
Simon Glassd392d322021-02-03 06:01:21 -07001529 self._check_strings(
Simon Glass17073252021-04-27 08:19:48 +12001530 self.decl_text + self.platdata_text + self.struct_text, data)
Simon Glassbe44f272020-12-28 20:34:51 -07001531
Simon Glass67b5ec52020-12-28 20:34:47 -07001532 def test_no_command(self):
Simon Glass8512ea22018-07-06 10:27:35 -06001533 """Test running dtoc without a command"""
Simon Glass67b5ec52020-12-28 20:34:47 -07001534 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -03001535 self.run_test([], '', '')
Simon Glass8512ea22018-07-06 10:27:35 -06001536 self.assertIn("Please specify a command: struct, platdata",
Simon Glass67b5ec52020-12-28 20:34:47 -07001537 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -06001538
Simon Glass67b5ec52020-12-28 20:34:47 -07001539 def test_bad_command(self):
Simon Glass8512ea22018-07-06 10:27:35 -06001540 """Test running dtoc with an invalid command"""
1541 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1542 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -07001543 with self.assertRaises(ValueError) as exc:
Simon Glass426d12f2021-02-03 06:01:14 -07001544 self.run_test(['invalid-cmd'], dtb_file, output)
1545 self.assertIn(
Simon Glass8490c572021-03-25 06:40:51 +13001546 "Unknown command 'invalid-cmd': (use: decl, platdata, struct)",
Simon Glass426d12f2021-02-03 06:01:14 -07001547 str(exc.exception))
Walter Lozano6c74d1b2020-07-28 19:06:23 -03001548
Simon Glass10cbd3b2020-12-28 20:34:52 -07001549 def test_output_conflict(self):
1550 """Test a conflict between and output dirs and output file"""
1551 with self.assertRaises(ValueError) as exc:
Simon Glass4a092352021-02-03 06:01:12 -07001552 dtb_platdata.run_steps(
1553 ['all'], None, False, 'out', ['cdir'], None, False,
1554 warning_disabled=True, scan=copy_scan())
Simon Glass10cbd3b2020-12-28 20:34:52 -07001555 self.assertIn("Must specify either output or output_dirs, not both",
1556 str(exc.exception))
1557
Simon Glass17073252021-04-27 08:19:48 +12001558 def check_output_dirs(self, instantiate):
Simon Glass10cbd3b2020-12-28 20:34:52 -07001559 # Remove the directory so that files from other tests are not there
1560 tools._RemoveOutputDir()
1561 tools.PrepareOutputDir(None)
1562
1563 # This should create the .dts and .dtb in the output directory
1564 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1565 outdir = tools.GetOutputDir()
1566 fnames = glob.glob(outdir + '/*')
1567 self.assertEqual(2, len(fnames))
1568
Simon Glass4a092352021-02-03 06:01:12 -07001569 dtb_platdata.run_steps(
Simon Glass17073252021-04-27 08:19:48 +12001570 ['all'], dtb_file, False, None, [outdir], None, instantiate,
Simon Glass4a092352021-02-03 06:01:12 -07001571 warning_disabled=True, scan=copy_scan())
Simon Glass10cbd3b2020-12-28 20:34:52 -07001572 fnames = glob.glob(outdir + '/*')
Simon Glass17073252021-04-27 08:19:48 +12001573 return fnames
1574
1575 def test_output_dirs(self):
1576 """Test outputting files to a directory"""
1577 fnames = self.check_output_dirs(False)
1578 self.assertEqual(5, len(fnames))
Simon Glass10cbd3b2020-12-28 20:34:52 -07001579
1580 leafs = set(os.path.basename(fname) for fname in fnames)
1581 self.assertEqual(
Simon Glass426d12f2021-02-03 06:01:14 -07001582 {'dt-structs-gen.h', 'source.dts', 'dt-plat.c', 'source.dtb',
Simon Glass17073252021-04-27 08:19:48 +12001583 'dt-decl.h'},
1584 leafs)
1585
1586 def test_output_dirs_inst(self):
1587 """Test outputting files to a directory with instantiation"""
1588 fnames = self.check_output_dirs(True)
1589 self.assertEqual(6, len(fnames))
1590
1591 leafs = set(os.path.basename(fname) for fname in fnames)
1592 self.assertEqual(
1593 {'dt-structs-gen.h', 'source.dts', 'source.dtb',
Simon Glassd392d322021-02-03 06:01:21 -07001594 'dt-uclass.c', 'dt-decl.h', 'dt-device.c'},
Simon Glass10cbd3b2020-12-28 20:34:52 -07001595 leafs)
Simon Glassfd471e22021-02-03 06:01:00 -07001596
1597 def setup_process_test(self):
1598 """Set up a test of process_nodes()
1599
1600 This uses saved_scan but returns a deep copy of it, so it is safe to
1601 modify it in these tests
1602
1603 Returns:
1604 tuple:
1605 DtbPlatdata: object to test
1606 Scanner: scanner to use
1607 """
1608 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1609 output = tools.GetOutputFilename('output')
1610
1611 # Take a copy before messing with it
Simon Glass50aae3e2021-02-03 06:01:11 -07001612 scan = copy_scan()
Simon Glassfd471e22021-02-03 06:01:00 -07001613 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
1614 plat.scan_dtb()
Simon Glass50aae3e2021-02-03 06:01:11 -07001615 plat.scan_tree(False)
Simon Glassfd471e22021-02-03 06:01:00 -07001616 plat.prepare_nodes()
1617 return plat, scan
1618
1619 def test_process_nodes(self):
1620 """Test processing nodes to add various info"""
1621 plat, scan = self.setup_process_test()
1622 plat.process_nodes(True)
1623
1624 i2c_node = plat._fdt.GetNode('/i2c@0')
1625 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
1626 pmic = scan._drivers['sandbox_pmic']
1627 i2c = scan._drivers['sandbox_i2c']
1628 self.assertEqual('DM_DEVICE_REF(pmic_at_9)', pmic_node.dev_ref)
1629 self.assertEqual(pmic, pmic_node.driver)
1630 self.assertEqual(i2c_node, pmic_node.parent)
1631 self.assertEqual(i2c, pmic_node.parent_driver)
1632
1633 # The pmic is the only child
1634 self.assertEqual(pmic_node.parent_seq, 0)
1635 self.assertEqual([pmic_node], i2c_node.child_devs)
1636
1637 # Start and end of the list should be the child_head
1638 ref = '&DM_DEVICE_REF(i2c_at_0)->child_head'
1639 self.assertEqual(
1640 {-1: ref, 0: '&DM_DEVICE_REF(pmic_at_9)->sibling_node', 1: ref},
1641 i2c_node.child_refs)
1642
1643 def test_process_nodes_bad_parent(self):
1644 # Pretend that i2c has a parent (the pmic) and delete that driver
1645 plat, scan = self.setup_process_test()
1646
1647 i2c_node = plat._fdt.GetNode('/i2c@0')
1648 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
1649 del scan._drivers['sandbox_pmic']
1650 i2c_node.parent = pmic_node
1651
1652 # Process twice, the second time to generate an exception
1653 plat.process_nodes(False)
1654 with self.assertRaises(ValueError) as exc:
1655 plat.process_nodes(True)
1656 self.assertIn(
1657 "Cannot parse/find parent driver 'sandbox_pmic' for 'sandbox_i2c",
1658 str(exc.exception))
1659
1660 def test_process_nodes_bad_node(self):
1661 plat, scan = self.setup_process_test()
1662
1663 # Now remove the pmic driver
1664 del scan._drivers['sandbox_pmic']
1665
1666 # Process twice, the second time to generate an exception
1667 plat.process_nodes(False)
1668 with self.assertRaises(ValueError) as exc:
1669 plat.process_nodes(True)
1670 self.assertIn("Cannot parse/find driver for 'sandbox_pmic",
1671 str(exc.exception))
Simon Glassb9319c42021-02-03 06:01:01 -07001672
Simon Glass337d6972021-02-03 06:01:10 -07001673 def test_process_nodes_bad_uclass(self):
1674 plat, scan = self.setup_process_test()
1675
1676 self.assertIn('UCLASS_I2C', scan._uclass)
1677 del scan._uclass['UCLASS_I2C']
1678 with self.assertRaises(ValueError) as exc:
1679 plat.process_nodes(True)
1680 self.assertIn("Cannot parse/find uclass 'UCLASS_I2C' for driver 'sandbox_i2c'",
1681 str(exc.exception))
1682
Simon Glassb9319c42021-02-03 06:01:01 -07001683 def test_process_nodes_used(self):
1684 """Test processing nodes to add various info"""
1685 plat, scan = self.setup_process_test()
1686 plat.process_nodes(True)
1687
1688 pmic = scan._drivers['sandbox_pmic']
1689 self.assertTrue(pmic.used)
1690
1691 gpio = scan._drivers['sandbox_gpio']
1692 self.assertFalse(gpio.used)
Simon Glass05953522021-02-03 06:01:07 -07001693
1694 def test_alias_read(self):
1695 """Test obtaining aliases"""
1696 dtb_file = get_dtb_file('dtoc_test_inst.dts')
1697 output = tools.GetOutputFilename('output')
1698 plat = self.run_test(['struct'], dtb_file, output)
1699
1700 scan = plat._scan
1701 testfdt_node = plat._fdt.GetNode('/some-bus/test')
Simon Glass337d6972021-02-03 06:01:10 -07001702 test0_node = plat._fdt.GetNode('/some-bus/test0')
Simon Glass05953522021-02-03 06:01:07 -07001703 self.assertIn('UCLASS_TEST_FDT', scan._uclass)
1704 uc = scan._uclass['UCLASS_TEST_FDT']
Simon Glass337d6972021-02-03 06:01:10 -07001705 self.assertEqual({1: testfdt_node, 2: test0_node},
1706 uc.alias_num_to_node)
1707 self.assertEqual({'/some-bus/test': 1, '/some-bus/test0': 2},
1708 uc.alias_path_to_num)
Simon Glass05953522021-02-03 06:01:07 -07001709
1710 # Try adding an alias that doesn't exist
1711 self.assertFalse(scan.add_uclass_alias('fred', 3, None))
1712
1713 # Try adding an alias for a missing node
1714 self.assertIsNone(scan.add_uclass_alias('testfdt', 3, None))
1715
1716 def test_alias_read_bad(self):
1717 """Test invalid alias property name"""
1718 dtb_file = get_dtb_file('dtoc_test_alias_bad.dts')
1719 output = tools.GetOutputFilename('output')
1720 with self.assertRaises(ValueError) as exc:
1721 plat = self.run_test(['struct'], dtb_file, output)
1722 self.assertIn("Cannot decode alias 'i2c4-'", str(exc.exception))
1723
1724 def test_alias_read_bad_path(self):
1725 """Test alias pointing to a non-existent node"""
1726 # This line may produce a warning, so capture it:
1727 # Warning (alias_paths): /aliases:i2c4: aliases property is not a valid
1728 # node (/does/not/exist)
1729 dtb_file = get_dtb_file('dtoc_test_alias_bad_path.dts', True)
1730
1731 output = tools.GetOutputFilename('output')
1732 with self.assertRaises(ValueError) as exc:
1733 plat = self.run_test(['struct'], dtb_file, output)
1734 self.assertIn("Alias 'i2c4' path '/does/not/exist' not found",
1735 str(exc.exception))
1736
1737 def test_alias_read_bad_uclass(self):
1738 """Test alias for a uclass that doesn't exist"""
1739 dtb_file = get_dtb_file('dtoc_test_alias_bad_uc.dts')
1740 output = tools.GetOutputFilename('output')
1741 with test_util.capture_sys_output() as (stdout, _):
1742 plat = self.run_test(['struct'], dtb_file, output)
1743 self.assertEqual("Could not find uclass for alias 'other1'",
1744 stdout.getvalue().strip())
Simon Glass074197a2021-02-03 06:01:09 -07001745
1746 def test_sequence(self):
1747 """Test assignment of sequence numnbers"""
1748 dtb_file = get_dtb_file('dtoc_test_inst.dts')
1749 output = tools.GetOutputFilename('output')
1750 plat = self.run_test(['struct'], dtb_file, output)
Simon Glass337d6972021-02-03 06:01:10 -07001751
1752 scan = plat._scan
1753 testfdt = plat._fdt.GetNode('/some-bus/test')
1754 self.assertEqual(1, testfdt.seq)
1755 i2c = plat._fdt.GetNode('/i2c')
1756
1757 # For now this uclass is not compiled in, so no sequence is assigned
1758 self.assertEqual(4, i2c.seq)
1759 spl = plat._fdt.GetNode('/spl-test')
1760 self.assertEqual(0, spl.seq)
Simon Glass50aae3e2021-02-03 06:01:11 -07001761
1762 def test_process_root(self):
1763 """Test assignment of sequence numnbers"""
1764 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1765 output = tools.GetOutputFilename('output')
1766
1767 # Take a copy before messing with it
1768 scan = copy_scan()
1769 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
1770 plat.scan_dtb()
1771 root = plat._fdt.GetRoot()
1772
1773 plat.scan_tree(False)
1774 self.assertNotIn(root, plat._valid_nodes)
1775
1776 plat.scan_tree(True)
1777 self.assertIn(root, plat._valid_nodes)
1778 self.assertEqual('root_driver',
1779 scan.get_normalized_compat_name(root)[0])
Simon Glass426d12f2021-02-03 06:01:14 -07001780
1781 def test_simple_inst(self):
1782 """Test output from some simple nodes with instantiate enabled"""
Simon Glassea74c952021-02-03 06:01:20 -07001783 dtb_file = get_dtb_file('dtoc_test_inst.dts')
Simon Glass426d12f2021-02-03 06:01:14 -07001784 output = tools.GetOutputFilename('output')
1785
1786 self.run_test(['decl'], dtb_file, output, True)
1787 with open(output) as infile:
1788 data = infile.read()
1789
1790 self._check_strings(self.decl_text_inst, data)
Simon Glass4b91be22021-02-03 06:01:15 -07001791
Simon Glassea74c952021-02-03 06:01:20 -07001792 self.run_test(['uclass'], dtb_file, output, True)
1793 with open(output) as infile:
1794 data = infile.read()
1795
1796 self._check_strings(UCLASS_HEADER_COMMON + self.uclass_text_inst, data)
1797
Simon Glassd392d322021-02-03 06:01:21 -07001798 self.run_test(['device'], dtb_file, output, True)
1799 with open(output) as infile:
1800 data = infile.read()
1801
1802 self._check_strings(self.device_text_inst, data)
1803
Simon Glassea74c952021-02-03 06:01:20 -07001804 def test_inst_no_hdr(self):
Simon Glassd392d322021-02-03 06:01:21 -07001805 """Test dealing with a struct tsssshat has no header"""
Simon Glassea74c952021-02-03 06:01:20 -07001806 dtb_file = get_dtb_file('dtoc_test_inst.dts')
1807 output = tools.GetOutputFilename('output')
1808
1809 # Run it once to set everything up
1810 plat = self.run_test(['decl'], dtb_file, output, True)
1811 scan = plat._scan
1812
1813 # Restart the output file and delete any record of the uclass' struct
1814 plat.setup_output(Ftype.SOURCE, output)
1815 del scan._structs['dm_test_uc_priv']
1816
1817 # Now generate the uclasses, which should provide a warning
1818 with test_util.capture_sys_output() as (stdout, _):
1819 plat.generate_uclasses()
1820 self.assertEqual(
1821 'Warning: Cannot find header file for struct dm_test_uc_priv',
1822 stdout.getvalue().strip())
Simon Glassda393412021-03-26 16:17:27 +13001823
1824 def test_missing_props(self):
1825 """Test detection of a parent node with no properties"""
1826 dtb_file = get_dtb_file('dtoc_test_noprops.dts', capture_stderr=True)
1827 output = tools.GetOutputFilename('output')
1828 with self.assertRaises(ValueError) as exc:
1829 self.run_test(['struct'], dtb_file, output)
1830 self.assertIn("Parent node '/i2c@0' has no properties - do you need",
1831 str(exc.exception))
1832
1833 def test_single_reg(self):
1834 """Test detection of a parent node with no properties"""
1835 dtb_file = get_dtb_file('dtoc_test_single_reg.dts')
1836 output = tools.GetOutputFilename('output')
1837 self.run_test(['struct'], dtb_file, output)
Simon Glass1c564692021-06-27 17:51:10 -06001838
1839 def test_missing_parent(self):
1840 """Test detection of a parent node with no properties"""
1841 dtb_file = get_dtb_file('dtoc_test_noparent.dts', capture_stderr=True)
1842 output = tools.GetOutputFilename('output')
1843 with self.assertRaises(ValueError) as exc:
1844 self.run_test(['device'], dtb_file, output, instantiate=True)
1845 self.assertIn("Node '/i2c@0/spl-test/pmic@9' requires parent node "
1846 "'/i2c@0/spl-test' but it is not in the valid list",
1847 str(exc.exception))