blob: 863ede90b7a650dacfe6e41934829d58ae6d17cf [file] [log] [blame]
Simon Glassbfb0bb22019-10-31 07:42:55 -06001#!/usr/bin/env python3
Tom Rini10e47792018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glass9d2eb922017-06-18 22:09:06 -06003# Copyright (c) 2012 The Chromium OS Authors.
4#
Simon Glass9d2eb922017-06-18 22:09:06 -06005
6"""Tests for the dtb_platdata module
7
Simon Glass70cd0d72018-07-06 10:27:20 -06008This includes unit tests for some functions and functional tests for the dtoc
9tool.
Simon Glass9d2eb922017-06-18 22:09:06 -060010"""
11
12import collections
Simon Glass768ff0a2021-02-03 06:00:51 -070013import copy
Simon Glass4e8e8462020-12-28 20:34:52 -070014import glob
Simon Glass9d2eb922017-06-18 22:09:06 -060015import os
16import struct
17import unittest
18
Simon Glassc7b4b832021-02-03 06:01:20 -070019from dtb_platdata import Ftype
Simon Glass9d2eb922017-06-18 22:09:06 -060020from dtb_platdata import get_value
21from dtb_platdata import tab_to
Simon Glassa60cabd2020-12-28 20:34:47 -070022from dtoc import dtb_platdata
Simon Glassa997ea52020-04-17 18:09:04 -060023from dtoc import fdt
24from dtoc import fdt_util
Simon Glass768ff0a2021-02-03 06:00:51 -070025from dtoc import src_scan
Simon Glass9065bc92020-12-28 20:35:06 -070026from dtoc.src_scan import conv_name_to_c
27from dtoc.src_scan import get_compat_name
Simon Glassa997ea52020-04-17 18:09:04 -060028from patman import test_util
29from patman import tools
Simon Glass9d2eb922017-06-18 22:09:06 -060030
Simon Glassa60cabd2020-12-28 20:34:47 -070031OUR_PATH = os.path.dirname(os.path.realpath(__file__))
Simon Glass9d2eb922017-06-18 22:09:06 -060032
33
Simon Glasseb37e2d2017-11-12 21:52:17 -070034HEADER = '''/*
35 * DO NOT MODIFY
36 *
Simon Glass6b208842020-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 Glasseb37e2d2017-11-12 21:52:17 -070039 */
40
41#include <stdbool.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090042#include <linux/libfdt.h>'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070043
Simon Glass3fa3bbb2021-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 Glassbe749002021-02-03 06:01:15 -070052C_HEADER_PRE = '''/*
Simon Glasseb37e2d2017-11-12 21:52:17 -070053 * DO NOT MODIFY
54 *
Simon Glass6b208842020-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 Glasseb37e2d2017-11-12 21:52:17 -070057 */
Simon Glassbe749002021-02-03 06:01:15 -070058'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070059
Simon Glassbe749002021-02-03 06:01:15 -070060C_HEADER = C_HEADER_PRE + '''
Simon Glass1d8364a2020-12-28 20:34:54 -070061/* Allow use of U_BOOT_DRVINFO() in this file */
Simon Glassbeddd7a2020-12-28 20:35:01 -070062#define DT_PLAT_C
Simon Glass4c73d7b2020-10-03 11:31:41 -060063
Simon Glasseb37e2d2017-11-12 21:52:17 -070064#include <common.h>
65#include <dm.h>
66#include <dt-structs.h>
67'''
68
Simon Glassc7b4b832021-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 Glass768ff0a2021-02-03 06:00:51 -070077# Scanner saved from a previous run of the tests (to speed things up)
78saved_scan = None
79
Simon Glassa60cabd2020-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 Glass3bce93d2018-07-06 10:27:37 -060083
84def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glass9d2eb922017-06-18 22:09:06 -060085 """Compile a .dts file to a .dtb
86
87 Args:
Simon Glassa60cabd2020-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 Glass9d2eb922017-06-18 22:09:06 -060090
91 Returns:
Simon Glassa60cabd2020-12-28 20:34:47 -070092 str: Filename of compiled file in output directory
Simon Glass9d2eb922017-06-18 22:09:06 -060093 """
Simon Glass4f4b2402021-02-03 06:00:56 -070094 return fdt_util.EnsureCompiled(os.path.join(OUR_PATH, 'test', dts_fname),
Simon Glass3bce93d2018-07-06 10:27:37 -060095 capture_stderr=capture_stderr)
Simon Glass9d2eb922017-06-18 22:09:06 -060096
Simon Glass768ff0a2021-02-03 06:00:51 -070097
98def setup():
99 global saved_scan
100
101 # Disable warnings so that calls to get_normalized_compat_name() will not
102 # output things.
Simon Glass695077d2021-03-26 16:17:25 +1300103 saved_scan = src_scan.Scanner(None, False)
Simon Glass768ff0a2021-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
Simon Glass9d2eb922017-06-18 22:09:06 -0600110
111class TestDtoc(unittest.TestCase):
112 """Tests for dtoc"""
113 @classmethod
114 def setUpClass(cls):
115 tools.PrepareOutputDir(None)
Simon Glass7f5e2262020-07-07 21:32:06 -0600116 cls.maxDiff = None
Simon Glass9d2eb922017-06-18 22:09:06 -0600117
118 @classmethod
119 def tearDownClass(cls):
Simon Glassa60cabd2020-12-28 20:34:47 -0700120 tools.FinaliseOutputDir()
Simon Glass9d2eb922017-06-18 22:09:06 -0600121
Simon Glassa60cabd2020-12-28 20:34:47 -0700122 @staticmethod
123 def _write_python_string(fname, data):
Simon Glassc47c2b32018-07-06 10:27:25 -0600124 """Write a string with tabs expanded as done in this Python file
125
126 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -0700127 fname (str): Filename to write to
128 data (str): Raw string to convert
Simon Glassc47c2b32018-07-06 10:27:25 -0600129 """
130 data = data.replace('\t', '\\t')
Simon Glassa60cabd2020-12-28 20:34:47 -0700131 with open(fname, 'w') as fout:
132 fout.write(data)
Simon Glassc47c2b32018-07-06 10:27:25 -0600133
Simon Glassa60cabd2020-12-28 20:34:47 -0700134 def _check_strings(self, expected, actual):
Simon Glassc47c2b32018-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 Glassa60cabd2020-12-28 20:34:47 -0700142 expected (str): Expected string
143 actual (str): Actual string
Simon Glassc47c2b32018-07-06 10:27:25 -0600144 """
145 if expected != actual:
Simon Glassa60cabd2020-12-28 20:34:47 -0700146 self._write_python_string('/tmp/binman.expected', expected)
147 self._write_python_string('/tmp/binman.actual', actual)
Simon Glass61b88e52019-05-17 22:00:31 -0600148 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glassa60cabd2020-12-28 20:34:47 -0700149 self.assertEqual(expected, actual)
Simon Glassc47c2b32018-07-06 10:27:25 -0600150
Simon Glassa60cabd2020-12-28 20:34:47 -0700151 @staticmethod
Simon Glass3809ad92021-02-03 06:01:12 -0700152 def run_test(args, dtb_file, output, instantiate=False):
Simon Glassa60cabd2020-12-28 20:34:47 -0700153 """Run a test using dtoc
Walter Lozanoa324e412020-06-25 01:10:08 -0300154
Simon Glassa60cabd2020-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 Glassbe88d2f2021-02-03 06:01:07 -0700159
160 Returns:
161 DtbPlatdata object
Simon Glassa60cabd2020-12-28 20:34:47 -0700162 """
Simon Glass80d782c42021-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 Glass3809ad92021-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 Lozanoa324e412020-06-25 01:10:08 -0300168
Simon Glass9d2eb922017-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 Glassc9a032c2020-11-08 20:36:17 -0700189 get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600190 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700191 get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600192 self.assertEqual('0x0',
Simon Glassc9a032c2020-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 Glass9d2eb922017-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 Lozano5fe734c2020-07-23 00:22:03 -0300204 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glass9d2eb922017-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 Lozano5fe734c2020-07-23 00:22:03 -0300209 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glass9d2eb922017-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 Lozano5fe734c2020-07-23 00:22:03 -0300214 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
Simon Glassa60cabd2020-12-28 20:34:47 -0700215 'arasan_sdhci_5_1', 'third']),
Simon Glass9d2eb922017-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 Glass768ff0a2021-02-03 06:00:51 -0700222
223 # Run this one without saved_scan to complete test coverage
Simon Glass3809ad92021-02-03 06:01:12 -0700224 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [], None,
225 False)
Simon Glass9d2eb922017-06-18 22:09:06 -0600226 with open(output) as infile:
227 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700228 self.assertEqual(HEADER.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600229
Walter Lozanoa324e412020-06-25 01:10:08 -0300230 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600231 with open(output) as infile:
232 lines = infile.read().splitlines()
Simon Glass2500de22020-12-28 20:35:05 -0700233 self.assertEqual(C_HEADER.splitlines() + [''], lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600234
Simon Glass3fa3bbb2021-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 Glassed5e6ae2021-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 Glass3fa3bbb2021-02-03 06:01:14 -0700245
246/* uclass driver declarations - needed for DM_UCLASS_DRIVER_REF() */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300247extern UCLASS_DRIVER(i2c);
248extern UCLASS_DRIVER(misc);
249extern UCLASS_DRIVER(pmic);
Simon Glass3fa3bbb2021-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 Glassed5e6ae2021-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 Glass3fa3bbb2021-02-03 06:01:14 -0700263
264/* device declarations - these allow DM_DEVICE_REF() to be used */
Simon Glassed5e6ae2021-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 Glass3fa3bbb2021-02-03 06:01:14 -0700272
273/* uclass driver declarations - needed for DM_UCLASS_DRIVER_REF() */
Simon Glassed5e6ae2021-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 Glass3fa3bbb2021-02-03 06:01:14 -0700279
280/* uclass declarations - needed for DM_UCLASS_REF() */
Simon Glassed5e6ae2021-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 Glass3fa3bbb2021-02-03 06:01:14 -0700286'''
Simon Glass07ee48e2020-12-28 20:34:49 -0700287 struct_text = HEADER + '''
Simon Glass32a8d222021-02-03 06:00:57 -0700288struct dtd_sandbox_i2c {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600289};
Simon Glass32a8d222021-02-03 06:00:57 -0700290struct dtd_sandbox_pmic {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600291\tbool\t\tlow_power;
Simon Glass94ba59e2021-03-26 16:17:26 +1300292\tfdt32_t\t\treg[1];
Simon Glass90e5f0a2017-08-29 14:15:51 -0600293};
Simon Glass9d2eb922017-06-18 22:09:06 -0600294struct dtd_sandbox_spl_test {
Simon Glass7f5e2262020-07-07 21:32:06 -0600295\tconst char * acpi_name;
Simon Glass9d2eb922017-06-18 22:09:06 -0600296\tbool\t\tboolval;
297\tunsigned char\tbytearray[3];
298\tunsigned char\tbyteval;
299\tfdt32_t\t\tintarray[4];
300\tfdt32_t\t\tintval;
301\tunsigned char\tlongbytearray[9];
Simon Glass9c526332018-07-06 10:27:28 -0600302\tunsigned char\tnotstring[5];
Simon Glass9d2eb922017-06-18 22:09:06 -0600303\tconst char *\tstringarray[3];
304\tconst char *\tstringval;
305};
Simon Glass07ee48e2020-12-28 20:34:49 -0700306'''
Simon Glass07ee48e2020-12-28 20:34:49 -0700307 platdata_text = C_HEADER + '''
Simon Glassbe749002021-02-03 06:01:15 -0700308/*
Simon Glasse2119082021-02-03 06:01:19 -0700309 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
310 *
311 * idx driver_info driver
312 * --- -------------------- --------------------
313 * 0: i2c_at_0 sandbox_i2c
314 * 1: pmic_at_9 sandbox_pmic
315 * 2: spl_test sandbox_spl_test
316 * 3: spl_test2 sandbox_spl_test
317 * 4: spl_test3 sandbox_spl_test
318 * --- -------------------- --------------------
319 */
320
321/*
Simon Glassbe749002021-02-03 06:01:15 -0700322 * Node /i2c@0 index 0
323 * driver sandbox_i2c parent None
324 */
Simon Glass32a8d222021-02-03 06:00:57 -0700325static struct dtd_sandbox_i2c dtv_i2c_at_0 = {
Simon Glass192f8132020-10-03 11:31:25 -0600326};
Simon Glass1d8364a2020-12-28 20:34:54 -0700327U_BOOT_DRVINFO(i2c_at_0) = {
Simon Glass32a8d222021-02-03 06:00:57 -0700328\t.name\t\t= "sandbox_i2c",
Simon Glasse2119082021-02-03 06:01:19 -0700329\t.plat\t\t= &dtv_i2c_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700330\t.plat_size\t= sizeof(dtv_i2c_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600331\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600332};
333
Simon Glassbe749002021-02-03 06:01:15 -0700334/*
335 * Node /i2c@0/pmic@9 index 1
336 * driver sandbox_pmic parent sandbox_i2c
337 */
Simon Glass32a8d222021-02-03 06:00:57 -0700338static struct dtd_sandbox_pmic dtv_pmic_at_9 = {
Simon Glass192f8132020-10-03 11:31:25 -0600339\t.low_power\t\t= true,
Simon Glass94ba59e2021-03-26 16:17:26 +1300340\t.reg\t\t\t= {0x9},
Simon Glass192f8132020-10-03 11:31:25 -0600341};
Simon Glass1d8364a2020-12-28 20:34:54 -0700342U_BOOT_DRVINFO(pmic_at_9) = {
Simon Glass32a8d222021-02-03 06:00:57 -0700343\t.name\t\t= "sandbox_pmic",
Simon Glasse2119082021-02-03 06:01:19 -0700344\t.plat\t\t= &dtv_pmic_at_9,
Simon Glass39edb952020-12-03 16:55:19 -0700345\t.plat_size\t= sizeof(dtv_pmic_at_9),
Simon Glass36b15e22020-10-03 11:31:35 -0600346\t.parent_idx\t= 0,
Simon Glass192f8132020-10-03 11:31:25 -0600347};
348
Simon Glassbe749002021-02-03 06:01:15 -0700349/*
350 * Node /spl-test index 2
351 * driver sandbox_spl_test parent None
352 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300353static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glassc82de562019-05-17 22:00:32 -0600354\t.boolval\t\t= true,
Simon Glass9d2eb922017-06-18 22:09:06 -0600355\t.bytearray\t\t= {0x6, 0x0, 0x0},
356\t.byteval\t\t= 0x5,
Simon Glassc82de562019-05-17 22:00:32 -0600357\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600358\t.intval\t\t\t= 0x1,
Simon Glass131e0b02017-08-29 14:15:49 -0600359\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
360\t\t0x11},
Simon Glassc82de562019-05-17 22:00:32 -0600361\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600362\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glassc82de562019-05-17 22:00:32 -0600363\t.stringval\t\t= "message",
Simon Glass9d2eb922017-06-18 22:09:06 -0600364};
Simon Glass1d8364a2020-12-28 20:34:54 -0700365U_BOOT_DRVINFO(spl_test) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600366\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -0700367\t.plat\t\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700368\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600369\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600370};
371
Simon Glassbe749002021-02-03 06:01:15 -0700372/*
373 * Node /spl-test2 index 3
374 * driver sandbox_spl_test parent None
375 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300376static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass7f5e2262020-07-07 21:32:06 -0600377\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glass9d2eb922017-06-18 22:09:06 -0600378\t.bytearray\t\t= {0x1, 0x23, 0x34},
379\t.byteval\t\t= 0x8,
Simon Glassc82de562019-05-17 22:00:32 -0600380\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600381\t.intval\t\t\t= 0x3,
Simon Glass8034e4d2020-10-03 11:31:27 -0600382\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
Simon Glass131e0b02017-08-29 14:15:49 -0600383\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600384\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glassc82de562019-05-17 22:00:32 -0600385\t.stringval\t\t= "message2",
Simon Glass9d2eb922017-06-18 22:09:06 -0600386};
Simon Glass1d8364a2020-12-28 20:34:54 -0700387U_BOOT_DRVINFO(spl_test2) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600388\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -0700389\t.plat\t\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700390\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600391\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600392};
393
Simon Glassbe749002021-02-03 06:01:15 -0700394/*
395 * Node /spl-test3 index 4
396 * driver sandbox_spl_test parent None
397 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300398static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glass8034e4d2020-10-03 11:31:27 -0600399\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
400\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600401\t.stringarray\t\t= {"one", "", ""},
402};
Simon Glass1d8364a2020-12-28 20:34:54 -0700403U_BOOT_DRVINFO(spl_test3) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600404\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -0700405\t.plat\t\t= &dtv_spl_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700406\t.plat_size\t= sizeof(dtv_spl_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600407\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600408};
409
Simon Glass2500de22020-12-28 20:35:05 -0700410'''
Simon Glassc7b4b832021-02-03 06:01:20 -0700411 uclass_text_inst = '''
412
413#include <common.h>
414#include <dm.h>
415#include <dt-structs.h>
416
417/*
Simon Glassfea2f252021-02-03 06:01:21 -0700418 * uclass declarations, ordered by 'struct uclass' linker_list idx:
419 * 0: i2c
420 * 1: misc
421 * 2: root
422 * 3: testbus
423 * 4: testfdt
Simon Glassc7b4b832021-02-03 06:01:20 -0700424 *
Simon Glassfea2f252021-02-03 06:01:21 -0700425 * Sequence numbers allocated in each uclass:
Simon Glassc7b4b832021-02-03 06:01:20 -0700426 * i2c: UCLASS_I2C
427 * 4: /i2c
428 * misc: UCLASS_MISC
429 * 0: /spl-test
430 * 1: /spl-test3
431 * root: UCLASS_ROOT
432 * 0: /
433 * testbus: UCLASS_TEST_BUS
434 * 2: /some-bus
435 * testfdt: UCLASS_TEST_FDT
436 * 1: /some-bus/test
437 * 2: /some-bus/test0
438 */
439
440struct list_head uclass_head = {
441 .prev = &DM_UCLASS_REF(testfdt)->sibling_node,
442 .next = &DM_UCLASS_REF(i2c)->sibling_node,
443};
444
445DM_UCLASS_INST(i2c) = {
446 .uc_drv = DM_UCLASS_DRIVER_REF(i2c),
447 .sibling_node = {
448 .prev = &uclass_head,
449 .next = &DM_UCLASS_REF(misc)->sibling_node,
450 },
451 .dev_head = {
452 .prev = &DM_DEVICE_REF(i2c)->uclass_node,
453 .next = &DM_DEVICE_REF(i2c)->uclass_node,
454 },
455};
456
457DM_UCLASS_INST(misc) = {
458 .uc_drv = DM_UCLASS_DRIVER_REF(misc),
459 .sibling_node = {
460 .prev = &DM_UCLASS_REF(i2c)->sibling_node,
461 .next = &DM_UCLASS_REF(root)->sibling_node,
462 },
463 .dev_head = {
464 .prev = &DM_DEVICE_REF(spl_test3)->uclass_node,
465 .next = &DM_DEVICE_REF(spl_test)->uclass_node,
466 },
467};
468
469DM_UCLASS_INST(root) = {
470 .uc_drv = DM_UCLASS_DRIVER_REF(root),
471 .sibling_node = {
472 .prev = &DM_UCLASS_REF(misc)->sibling_node,
473 .next = &DM_UCLASS_REF(testbus)->sibling_node,
474 },
475 .dev_head = {
476 .prev = &DM_DEVICE_REF(root)->uclass_node,
477 .next = &DM_DEVICE_REF(root)->uclass_node,
478 },
479};
480
481DM_UCLASS_INST(testbus) = {
482 .uc_drv = DM_UCLASS_DRIVER_REF(testbus),
483 .sibling_node = {
484 .prev = &DM_UCLASS_REF(root)->sibling_node,
485 .next = &DM_UCLASS_REF(testfdt)->sibling_node,
486 },
487 .dev_head = {
488 .prev = &DM_DEVICE_REF(some_bus)->uclass_node,
489 .next = &DM_DEVICE_REF(some_bus)->uclass_node,
490 },
491};
492
493#include <dm/test.h>
494u8 _testfdt_priv_[sizeof(struct dm_test_uc_priv)]
495 __attribute__ ((section (".priv_data")));
496DM_UCLASS_INST(testfdt) = {
497 .priv_ = _testfdt_priv_,
498 .uc_drv = DM_UCLASS_DRIVER_REF(testfdt),
499 .sibling_node = {
500 .prev = &DM_UCLASS_REF(testbus)->sibling_node,
501 .next = &uclass_head,
502 },
503 .dev_head = {
504 .prev = &DM_DEVICE_REF(test0)->uclass_node,
505 .next = &DM_DEVICE_REF(test)->uclass_node,
506 },
507};
508
Simon Glassfea2f252021-02-03 06:01:21 -0700509'''
510 device_text_inst = '''/*
511 * DO NOT MODIFY
512 *
513 * Declares the DM_DEVICE_INST() records.
514 * This was generated by dtoc from a .dtb (device tree binary) file.
515 */
516
517#include <common.h>
518#include <dm.h>
519#include <dt-structs.h>
520
521/*
522 * udevice declarations, ordered by 'struct udevice' linker_list position:
523 *
524 * idx udevice driver
525 * --- -------------------- --------------------
526 * 0: i2c sandbox_i2c
527 * 1: root root_driver
528 * 2: some_bus denx_u_boot_test_bus
529 * 3: spl_test sandbox_spl_test
530 * 4: spl_test3 sandbox_spl_test
531 * 5: test denx_u_boot_fdt_test
532 * 6: test0 denx_u_boot_fdt_test
533 * --- -------------------- --------------------
534 */
535
536/*
537 * Node /i2c index 0
538 * driver sandbox_i2c parent root_driver
539*/
540static struct dtd_sandbox_i2c dtv_i2c = {
541\t.intval\t\t\t= 0x3,
542};
543
544#include <asm/i2c.h>
545u8 _sandbox_i2c_priv_i2c[sizeof(struct sandbox_i2c_priv)]
546\t__attribute__ ((section (".priv_data")));
547#include <i2c.h>
548u8 _sandbox_i2c_uc_priv_i2c[sizeof(struct dm_i2c_bus)]
549\t__attribute__ ((section (".priv_data")));
550
551DM_DEVICE_INST(i2c) = {
552\t.driver\t\t= DM_DRIVER_REF(sandbox_i2c),
553\t.name\t\t= "sandbox_i2c",
554\t.plat_\t\t= &dtv_i2c,
555\t.priv_\t\t= _sandbox_i2c_priv_i2c,
556\t.uclass\t\t= DM_UCLASS_REF(i2c),
557\t.uclass_priv_ = _sandbox_i2c_uc_priv_i2c,
558\t.uclass_node\t= {
559\t\t.prev = &DM_UCLASS_REF(i2c)->dev_head,
560\t\t.next = &DM_UCLASS_REF(i2c)->dev_head,
561\t},
562\t.child_head\t= {
563\t\t.prev = &DM_DEVICE_REF(i2c)->child_head,
564\t\t.next = &DM_DEVICE_REF(i2c)->child_head,
565\t},
566\t.sibling_node\t= {
567\t\t.prev = &DM_DEVICE_REF(root)->child_head,
568\t\t.next = &DM_DEVICE_REF(some_bus)->sibling_node,
569\t},
570\t.seq_ = 4,
571};
572
573/*
574 * Node / index 1
575 * driver root_driver parent None
576*/
577static struct dtd_root_driver dtv_root = {
578};
579
580DM_DEVICE_INST(root) = {
581\t.driver\t\t= DM_DRIVER_REF(root_driver),
582\t.name\t\t= "root_driver",
583\t.plat_\t\t= &dtv_root,
584\t.uclass\t\t= DM_UCLASS_REF(root),
585\t.uclass_node\t= {
586\t\t.prev = &DM_UCLASS_REF(root)->dev_head,
587\t\t.next = &DM_UCLASS_REF(root)->dev_head,
588\t},
589\t.child_head\t= {
590\t\t.prev = &DM_DEVICE_REF(spl_test3)->sibling_node,
591\t\t.next = &DM_DEVICE_REF(i2c)->sibling_node,
592\t},
593\t.seq_ = 0,
594};
595
596/*
597 * Node /some-bus index 2
598 * driver denx_u_boot_test_bus parent root_driver
599*/
600
601#include <dm/test.h>
602struct dm_test_pdata __attribute__ ((section (".priv_data")))
603\t_denx_u_boot_test_bus_plat_some_bus = {
604\t.dtplat = {
605\t\t.ping_add\t\t= 0x4,
606\t\t.ping_expect\t\t= 0x4,
607\t\t.reg\t\t\t= {0x3, 0x1},
608\t},
609};
610#include <dm/test.h>
611u8 _denx_u_boot_test_bus_priv_some_bus[sizeof(struct dm_test_priv)]
612\t__attribute__ ((section (".priv_data")));
613#include <dm/test.h>
614u8 _denx_u_boot_test_bus_ucplat_some_bus[sizeof(struct dm_test_uclass_priv)]
615\t__attribute__ ((section (".priv_data")));
616#include <test.h>
617
618DM_DEVICE_INST(some_bus) = {
619\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_test_bus),
620\t.name\t\t= "denx_u_boot_test_bus",
621\t.plat_\t\t= &_denx_u_boot_test_bus_plat_some_bus,
622\t.uclass_plat_\t= _denx_u_boot_test_bus_ucplat_some_bus,
623\t.driver_data\t= DM_TEST_TYPE_FIRST,
624\t.priv_\t\t= _denx_u_boot_test_bus_priv_some_bus,
625\t.uclass\t\t= DM_UCLASS_REF(testbus),
626\t.uclass_node\t= {
627\t\t.prev = &DM_UCLASS_REF(testbus)->dev_head,
628\t\t.next = &DM_UCLASS_REF(testbus)->dev_head,
629\t},
630\t.child_head\t= {
631\t\t.prev = &DM_DEVICE_REF(test0)->sibling_node,
632\t\t.next = &DM_DEVICE_REF(test)->sibling_node,
633\t},
634\t.sibling_node\t= {
635\t\t.prev = &DM_DEVICE_REF(i2c)->sibling_node,
636\t\t.next = &DM_DEVICE_REF(spl_test)->sibling_node,
637\t},
638\t.seq_ = 2,
639};
640
641/*
642 * Node /spl-test index 3
643 * driver sandbox_spl_test parent root_driver
644*/
645static struct dtd_sandbox_spl_test dtv_spl_test = {
646\t.boolval\t\t= true,
647\t.intval\t\t\t= 0x1,
648};
649
650DM_DEVICE_INST(spl_test) = {
651\t.driver\t\t= DM_DRIVER_REF(sandbox_spl_test),
652\t.name\t\t= "sandbox_spl_test",
653\t.plat_\t\t= &dtv_spl_test,
654\t.uclass\t\t= DM_UCLASS_REF(misc),
655\t.uclass_node\t= {
656\t\t.prev = &DM_UCLASS_REF(misc)->dev_head,
657\t\t.next = &DM_DEVICE_REF(spl_test3)->uclass_node,
658\t},
659\t.child_head\t= {
660\t\t.prev = &DM_DEVICE_REF(spl_test)->child_head,
661\t\t.next = &DM_DEVICE_REF(spl_test)->child_head,
662\t},
663\t.sibling_node\t= {
664\t\t.prev = &DM_DEVICE_REF(some_bus)->sibling_node,
665\t\t.next = &DM_DEVICE_REF(spl_test3)->sibling_node,
666\t},
667\t.seq_ = 0,
668};
669
670/*
671 * Node /spl-test3 index 4
672 * driver sandbox_spl_test parent root_driver
673*/
674static struct dtd_sandbox_spl_test dtv_spl_test3 = {
675\t.longbytearray\t\t= {0x90a0b0c, 0xd0e0f10},
676\t.stringarray\t\t= "one",
677};
678
679DM_DEVICE_INST(spl_test3) = {
680\t.driver\t\t= DM_DRIVER_REF(sandbox_spl_test),
681\t.name\t\t= "sandbox_spl_test",
682\t.plat_\t\t= &dtv_spl_test3,
683\t.uclass\t\t= DM_UCLASS_REF(misc),
684\t.uclass_node\t= {
685\t\t.prev = &DM_DEVICE_REF(spl_test)->uclass_node,
686\t\t.next = &DM_UCLASS_REF(misc)->dev_head,
687\t},
688\t.child_head\t= {
689\t\t.prev = &DM_DEVICE_REF(spl_test3)->child_head,
690\t\t.next = &DM_DEVICE_REF(spl_test3)->child_head,
691\t},
692\t.sibling_node\t= {
693\t\t.prev = &DM_DEVICE_REF(spl_test)->sibling_node,
694\t\t.next = &DM_DEVICE_REF(root)->child_head,
695\t},
696\t.seq_ = 1,
697};
698
699/*
700 * Node /some-bus/test index 5
701 * driver denx_u_boot_fdt_test parent denx_u_boot_test_bus
702*/
703
704#include <dm/test.h>
705struct dm_test_pdata __attribute__ ((section (".priv_data")))
706\t_denx_u_boot_fdt_test_plat_test = {
707\t.dtplat = {
708\t\t.ping_add\t\t= 0x5,
709\t\t.ping_expect\t\t= 0x5,
Simon Glass94ba59e2021-03-26 16:17:26 +1300710\t\t.reg\t\t\t= {0x5},
Simon Glassfea2f252021-02-03 06:01:21 -0700711\t},
712};
713#include <dm/test.h>
714u8 _denx_u_boot_fdt_test_priv_test[sizeof(struct dm_test_priv)]
715\t__attribute__ ((section (".priv_data")));
716#include <dm/test.h>
717u8 _denx_u_boot_fdt_test_parent_plat_test[sizeof(struct dm_test_parent_plat)]
718\t__attribute__ ((section (".priv_data")));
719#include <dm/test.h>
720u8 _denx_u_boot_fdt_test_parent_priv_test[sizeof(struct dm_test_parent_data)]
721\t__attribute__ ((section (".priv_data")));
722
723DM_DEVICE_INST(test) = {
724\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_fdt_test),
725\t.name\t\t= "denx_u_boot_fdt_test",
726\t.plat_\t\t= &_denx_u_boot_fdt_test_plat_test,
727\t.parent_plat_\t= _denx_u_boot_fdt_test_parent_plat_test,
728\t.driver_data\t= DM_TEST_TYPE_FIRST,
729\t.parent\t\t= DM_DEVICE_REF(some_bus),
730\t.priv_\t\t= _denx_u_boot_fdt_test_priv_test,
731\t.uclass\t\t= DM_UCLASS_REF(testfdt),
732\t.parent_priv_\t= _denx_u_boot_fdt_test_parent_priv_test,
733\t.uclass_node\t= {
734\t\t.prev = &DM_UCLASS_REF(testfdt)->dev_head,
735\t\t.next = &DM_DEVICE_REF(test0)->uclass_node,
736\t},
737\t.child_head\t= {
738\t\t.prev = &DM_DEVICE_REF(test)->child_head,
739\t\t.next = &DM_DEVICE_REF(test)->child_head,
740\t},
741\t.sibling_node\t= {
742\t\t.prev = &DM_DEVICE_REF(some_bus)->child_head,
743\t\t.next = &DM_DEVICE_REF(test0)->sibling_node,
744\t},
745\t.seq_ = 1,
746};
747
748/*
749 * Node /some-bus/test0 index 6
750 * driver denx_u_boot_fdt_test parent denx_u_boot_test_bus
751*/
752
753#include <dm/test.h>
754struct dm_test_pdata __attribute__ ((section (".priv_data")))
755\t_denx_u_boot_fdt_test_plat_test0 = {
756\t.dtplat = {
757\t},
758};
759#include <dm/test.h>
760u8 _denx_u_boot_fdt_test_priv_test0[sizeof(struct dm_test_priv)]
761\t__attribute__ ((section (".priv_data")));
762#include <dm/test.h>
763u8 _denx_u_boot_fdt_test_parent_plat_test0[sizeof(struct dm_test_parent_plat)]
764\t__attribute__ ((section (".priv_data")));
765#include <dm/test.h>
766u8 _denx_u_boot_fdt_test_parent_priv_test0[sizeof(struct dm_test_parent_data)]
767\t__attribute__ ((section (".priv_data")));
768
769DM_DEVICE_INST(test0) = {
770\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_fdt_test),
771\t.name\t\t= "denx_u_boot_fdt_test",
772\t.plat_\t\t= &_denx_u_boot_fdt_test_plat_test0,
773\t.parent_plat_\t= _denx_u_boot_fdt_test_parent_plat_test0,
774\t.driver_data\t= DM_TEST_TYPE_SECOND,
775\t.parent\t\t= DM_DEVICE_REF(some_bus),
776\t.priv_\t\t= _denx_u_boot_fdt_test_priv_test0,
777\t.uclass\t\t= DM_UCLASS_REF(testfdt),
778\t.parent_priv_\t= _denx_u_boot_fdt_test_parent_priv_test0,
779\t.uclass_node\t= {
780\t\t.prev = &DM_DEVICE_REF(test)->uclass_node,
781\t\t.next = &DM_UCLASS_REF(testfdt)->dev_head,
782\t},
783\t.child_head\t= {
784\t\t.prev = &DM_DEVICE_REF(test0)->child_head,
785\t\t.next = &DM_DEVICE_REF(test0)->child_head,
786\t},
787\t.sibling_node\t= {
788\t\t.prev = &DM_DEVICE_REF(test)->sibling_node,
789\t\t.next = &DM_DEVICE_REF(some_bus)->child_head,
790\t},
791\t.seq_ = 2,
792};
793
794'''
Simon Glass07ee48e2020-12-28 20:34:49 -0700795
796 def test_simple(self):
797 """Test output from some simple nodes with various types of data"""
798 dtb_file = get_dtb_file('dtoc_test_simple.dts')
799 output = tools.GetOutputFilename('output')
800 self.run_test(['struct'], dtb_file, output)
801 with open(output) as infile:
802 data = infile.read()
803
804 self._check_strings(self.struct_text, data)
805
806 self.run_test(['platdata'], dtb_file, output)
807 with open(output) as infile:
808 data = infile.read()
809
810 self._check_strings(self.platdata_text, data)
Simon Glass9d2eb922017-06-18 22:09:06 -0600811
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700812 self.run_test(['decl'], dtb_file, output)
813 with open(output) as infile:
814 data = infile.read()
815
816 self._check_strings(self.decl_text, data)
817
Simon Glass4e8e8462020-12-28 20:34:52 -0700818 # Try the 'all' command
819 self.run_test(['all'], dtb_file, output)
820 data = tools.ReadFile(output, binary=False)
Simon Glassfea2f252021-02-03 06:01:21 -0700821 self._check_strings(
Simon Glass705b84b2021-04-27 08:19:48 +1200822 self.decl_text + self.platdata_text + self.struct_text, data)
Simon Glass4e8e8462020-12-28 20:34:52 -0700823
Walter Lozanoe675d962020-07-03 08:07:17 -0300824 def test_driver_alias(self):
825 """Test output from a device tree file with a driver alias"""
826 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
827 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300828 self.run_test(['struct'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300829 with open(output) as infile:
830 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700831 self._check_strings(HEADER + '''
Walter Lozanoe675d962020-07-03 08:07:17 -0300832struct dtd_sandbox_gpio {
833\tconst char *\tgpio_bank_name;
834\tbool\t\tgpio_controller;
835\tfdt32_t\t\tsandbox_gpio_count;
836};
Walter Lozanoe675d962020-07-03 08:07:17 -0300837''', data)
838
Walter Lozanoa324e412020-06-25 01:10:08 -0300839 self.run_test(['platdata'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300840 with open(output) as infile:
841 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700842 self._check_strings(C_HEADER + '''
Simon Glassbe749002021-02-03 06:01:15 -0700843/*
Simon Glasse2119082021-02-03 06:01:19 -0700844 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
845 *
846 * idx driver_info driver
847 * --- -------------------- --------------------
848 * 0: gpios_at_0 sandbox_gpio
849 * --- -------------------- --------------------
850 */
851
852/*
Simon Glassbe749002021-02-03 06:01:15 -0700853 * Node /gpios@0 index 0
854 * driver sandbox_gpio parent None
855 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300856static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300857\t.gpio_bank_name\t\t= "a",
858\t.gpio_controller\t= true,
859\t.sandbox_gpio_count\t= 0x14,
860};
Simon Glass1d8364a2020-12-28 20:34:54 -0700861U_BOOT_DRVINFO(gpios_at_0) = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300862\t.name\t\t= "sandbox_gpio",
Simon Glasse2119082021-02-03 06:01:19 -0700863\t.plat\t\t= &dtv_gpios_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700864\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600865\t.parent_idx\t= -1,
Walter Lozanoe675d962020-07-03 08:07:17 -0300866};
867
868''', data)
869
Walter Lozanoa324e412020-06-25 01:10:08 -0300870 def test_invalid_driver(self):
871 """Test output from a device tree file with an invalid driver"""
872 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
873 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700874 with test_util.capture_sys_output() as _:
Simon Glass3809ad92021-02-03 06:01:12 -0700875 dtb_platdata.run_steps(
876 ['struct'], dtb_file, False, output, [], None, False,
877 scan=copy_scan())
Walter Lozanoa324e412020-06-25 01:10:08 -0300878 with open(output) as infile:
879 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700880 self._check_strings(HEADER + '''
Walter Lozanoa324e412020-06-25 01:10:08 -0300881struct dtd_invalid {
882};
883''', data)
884
Simon Glassa60cabd2020-12-28 20:34:47 -0700885 with test_util.capture_sys_output() as _:
Simon Glass3809ad92021-02-03 06:01:12 -0700886 dtb_platdata.run_steps(
887 ['platdata'], dtb_file, False, output, [], None, False,
888 scan=copy_scan())
Walter Lozanoa324e412020-06-25 01:10:08 -0300889 with open(output) as infile:
890 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700891 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -0700892/*
893 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
894 *
895 * idx driver_info driver
896 * --- -------------------- --------------------
897 * 0: spl_test invalid
898 * --- -------------------- --------------------
899 */
900
Simon Glass192f8132020-10-03 11:31:25 -0600901/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300902static struct dtd_invalid dtv_spl_test = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300903};
Simon Glass1d8364a2020-12-28 20:34:54 -0700904U_BOOT_DRVINFO(spl_test) = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300905\t.name\t\t= "invalid",
Simon Glasse2119082021-02-03 06:01:19 -0700906\t.plat\t\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700907\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600908\t.parent_idx\t= -1,
Walter Lozanoa324e412020-06-25 01:10:08 -0300909};
910
911''', data)
912
Simon Glass9d2eb922017-06-18 22:09:06 -0600913 def test_phandle(self):
914 """Test output from a node containing a phandle reference"""
915 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
916 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300917 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600918 with open(output) as infile:
919 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700920 self._check_strings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600921struct dtd_source {
Simon Glass3deeb472017-08-29 14:15:59 -0600922\tstruct phandle_2_arg clocks[4];
Simon Glass9d2eb922017-06-18 22:09:06 -0600923};
924struct dtd_target {
925\tfdt32_t\t\tintval;
926};
927''', data)
928
Walter Lozanoa324e412020-06-25 01:10:08 -0300929 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600930 with open(output) as infile:
931 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700932 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -0700933/*
934 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
935 *
936 * idx driver_info driver
937 * --- -------------------- --------------------
938 * 0: phandle2_target target
939 * 1: phandle3_target target
940 * 2: phandle_source source
941 * 3: phandle_source2 source
942 * 4: phandle_target target
943 * --- -------------------- --------------------
944 */
945
Simon Glass192f8132020-10-03 11:31:25 -0600946/* Node /phandle2-target index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300947static struct dtd_target dtv_phandle2_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600948\t.intval\t\t\t= 0x1,
949};
Simon Glass1d8364a2020-12-28 20:34:54 -0700950U_BOOT_DRVINFO(phandle2_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600951\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -0700952\t.plat\t\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700953\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600954\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600955};
956
Simon Glass192f8132020-10-03 11:31:25 -0600957/* Node /phandle3-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300958static struct dtd_target dtv_phandle3_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600959\t.intval\t\t\t= 0x2,
960};
Simon Glass1d8364a2020-12-28 20:34:54 -0700961U_BOOT_DRVINFO(phandle3_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600962\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -0700963\t.plat\t\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700964\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600965\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600966};
967
Simon Glass192f8132020-10-03 11:31:25 -0600968/* Node /phandle-source index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300969static struct dtd_source dtv_phandle_source = {
Simon Glassd0cd0752017-08-29 14:15:57 -0600970\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600971\t\t\t{4, {}},
972\t\t\t{0, {11}},
973\t\t\t{1, {12, 13}},
974\t\t\t{4, {}},},
Simon Glass9d2eb922017-06-18 22:09:06 -0600975};
Simon Glass1d8364a2020-12-28 20:34:54 -0700976U_BOOT_DRVINFO(phandle_source) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600977\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -0700978\t.plat\t\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700979\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600980\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600981};
982
Simon Glass192f8132020-10-03 11:31:25 -0600983/* Node /phandle-source2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300984static struct dtd_source dtv_phandle_source2 = {
Simon Glass609e2b12018-07-06 10:27:31 -0600985\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600986\t\t\t{4, {}},},
Simon Glass609e2b12018-07-06 10:27:31 -0600987};
Simon Glass1d8364a2020-12-28 20:34:54 -0700988U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass609e2b12018-07-06 10:27:31 -0600989\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -0700990\t.plat\t\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700991\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600992\t.parent_idx\t= -1,
Simon Glass609e2b12018-07-06 10:27:31 -0600993};
994
Simon Glass16382ce2020-12-28 20:35:04 -0700995/* Node /phandle-target index 4 */
996static struct dtd_target dtv_phandle_target = {
997\t.intval\t\t\t= 0x0,
998};
999U_BOOT_DRVINFO(phandle_target) = {
1000\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001001\t.plat\t\t= &dtv_phandle_target,
Simon Glass16382ce2020-12-28 20:35:04 -07001002\t.plat_size\t= sizeof(dtv_phandle_target),
1003\t.parent_idx\t= -1,
1004};
1005
Simon Glass9d2eb922017-06-18 22:09:06 -06001006''', data)
1007
Simon Glass961c1ce2018-07-06 10:27:35 -06001008 def test_phandle_single(self):
1009 """Test output from a node containing a phandle reference"""
1010 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
1011 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001012 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001013 with open(output) as infile:
1014 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001015 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -06001016struct dtd_source {
1017\tstruct phandle_0_arg clocks[1];
1018};
1019struct dtd_target {
1020\tfdt32_t\t\tintval;
1021};
1022''', data)
1023
1024 def test_phandle_reorder(self):
1025 """Test that phandle targets are generated before their references"""
1026 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
1027 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001028 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001029 with open(output) as infile:
1030 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001031 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001032/*
1033 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1034 *
1035 * idx driver_info driver
1036 * --- -------------------- --------------------
1037 * 0: phandle_source2 source
1038 * 1: phandle_target target
1039 * --- -------------------- --------------------
1040 */
1041
Simon Glass192f8132020-10-03 11:31:25 -06001042/* Node /phandle-source2 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001043static struct dtd_source dtv_phandle_source2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001044\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -06001045\t\t\t{1, {}},},
Simon Glass961c1ce2018-07-06 10:27:35 -06001046};
Simon Glass1d8364a2020-12-28 20:34:54 -07001047U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001048\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -07001049\t.plat\t\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -07001050\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -06001051\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -06001052};
1053
Simon Glass16382ce2020-12-28 20:35:04 -07001054/* Node /phandle-target index 1 */
1055static struct dtd_target dtv_phandle_target = {
1056};
1057U_BOOT_DRVINFO(phandle_target) = {
1058\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001059\t.plat\t\t= &dtv_phandle_target,
Simon Glass16382ce2020-12-28 20:35:04 -07001060\t.plat_size\t= sizeof(dtv_phandle_target),
1061\t.parent_idx\t= -1,
1062};
1063
Simon Glass961c1ce2018-07-06 10:27:35 -06001064''', data)
1065
Walter Lozano5541fc02020-06-25 01:10:17 -03001066 def test_phandle_cd_gpio(self):
1067 """Test that phandle targets are generated when unsing cd-gpios"""
1068 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
1069 output = tools.GetOutputFilename('output')
Simon Glass3809ad92021-02-03 06:01:12 -07001070 dtb_platdata.run_steps(
1071 ['platdata'], dtb_file, False, output, [], None, False,
1072 warning_disabled=True, scan=copy_scan())
Walter Lozano5541fc02020-06-25 01:10:17 -03001073 with open(output) as infile:
1074 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001075 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001076/*
1077 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1078 *
1079 * idx driver_info driver
1080 * --- -------------------- --------------------
1081 * 0: phandle2_target target
1082 * 1: phandle3_target target
1083 * 2: phandle_source source
1084 * 3: phandle_source2 source
1085 * 4: phandle_target target
1086 * --- -------------------- --------------------
1087 */
1088
Simon Glass192f8132020-10-03 11:31:25 -06001089/* Node /phandle2-target index 0 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001090static struct dtd_target dtv_phandle2_target = {
1091\t.intval\t\t\t= 0x1,
1092};
Simon Glass1d8364a2020-12-28 20:34:54 -07001093U_BOOT_DRVINFO(phandle2_target) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001094\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001095\t.plat\t\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -07001096\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -06001097\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -03001098};
1099
Simon Glass192f8132020-10-03 11:31:25 -06001100/* Node /phandle3-target index 1 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001101static struct dtd_target dtv_phandle3_target = {
1102\t.intval\t\t\t= 0x2,
1103};
Simon Glass1d8364a2020-12-28 20:34:54 -07001104U_BOOT_DRVINFO(phandle3_target) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001105\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001106\t.plat\t\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -07001107\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -06001108\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -03001109};
1110
Simon Glass192f8132020-10-03 11:31:25 -06001111/* Node /phandle-source index 2 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001112static struct dtd_source dtv_phandle_source = {
1113\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -06001114\t\t\t{4, {}},
1115\t\t\t{0, {11}},
1116\t\t\t{1, {12, 13}},
1117\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -03001118};
Simon Glass1d8364a2020-12-28 20:34:54 -07001119U_BOOT_DRVINFO(phandle_source) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001120\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -07001121\t.plat\t\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -07001122\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -06001123\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -03001124};
1125
Simon Glass192f8132020-10-03 11:31:25 -06001126/* Node /phandle-source2 index 3 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001127static struct dtd_source dtv_phandle_source2 = {
1128\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -06001129\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -03001130};
Simon Glass1d8364a2020-12-28 20:34:54 -07001131U_BOOT_DRVINFO(phandle_source2) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001132\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -07001133\t.plat\t\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -07001134\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -06001135\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -03001136};
1137
Simon Glass16382ce2020-12-28 20:35:04 -07001138/* Node /phandle-target index 4 */
1139static struct dtd_target dtv_phandle_target = {
1140\t.intval\t\t\t= 0x0,
1141};
1142U_BOOT_DRVINFO(phandle_target) = {
1143\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001144\t.plat\t\t= &dtv_phandle_target,
Simon Glass16382ce2020-12-28 20:35:04 -07001145\t.plat_size\t= sizeof(dtv_phandle_target),
1146\t.parent_idx\t= -1,
1147};
1148
Walter Lozano5541fc02020-06-25 01:10:17 -03001149''', data)
1150
Simon Glass961c1ce2018-07-06 10:27:35 -06001151 def test_phandle_bad(self):
1152 """Test a node containing an invalid phandle fails"""
Simon Glass04150022018-10-01 21:12:43 -06001153 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
1154 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -06001155 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001156 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001157 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001158 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
Simon Glassa60cabd2020-12-28 20:34:47 -07001159 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001160
1161 def test_phandle_bad2(self):
1162 """Test a phandle target missing its #*-cells property"""
Simon Glass04150022018-10-01 21:12:43 -06001163 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
1164 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -06001165 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001166 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001167 self.run_test(['struct'], dtb_file, output)
Walter Lozano179f0b62020-06-25 01:10:16 -03001168 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glassa60cabd2020-12-28 20:34:47 -07001169 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001170
Simon Glass1b1fe412017-08-29 14:15:50 -06001171 def test_addresses64(self):
1172 """Test output from a node with a 'reg' property with na=2, ns=2"""
1173 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
1174 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001175 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001176 with open(output) as infile:
1177 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001178 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001179struct dtd_test1 {
1180\tfdt64_t\t\treg[2];
1181};
1182struct dtd_test2 {
1183\tfdt64_t\t\treg[2];
1184};
1185struct dtd_test3 {
1186\tfdt64_t\t\treg[4];
1187};
1188''', data)
1189
Walter Lozanoa324e412020-06-25 01:10:08 -03001190 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001191 with open(output) as infile:
1192 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001193 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001194/*
1195 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1196 *
1197 * idx driver_info driver
1198 * --- -------------------- --------------------
1199 * 0: test1 test1
1200 * 1: test2 test2
1201 * 2: test3 test3
1202 * --- -------------------- --------------------
1203 */
1204
Simon Glass192f8132020-10-03 11:31:25 -06001205/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001206static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001207\t.reg\t\t\t= {0x1234, 0x5678},
1208};
Simon Glass1d8364a2020-12-28 20:34:54 -07001209U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001210\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001211\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001212\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001213\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001214};
1215
Simon Glass192f8132020-10-03 11:31:25 -06001216/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001217static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001218\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
1219};
Simon Glass1d8364a2020-12-28 20:34:54 -07001220U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001221\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001222\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001223\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001224\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001225};
1226
Simon Glass192f8132020-10-03 11:31:25 -06001227/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001228static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001229\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
1230};
Simon Glass1d8364a2020-12-28 20:34:54 -07001231U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001232\t.name\t\t= "test3",
Simon Glasse2119082021-02-03 06:01:19 -07001233\t.plat\t\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -07001234\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -06001235\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001236};
1237
Simon Glass2500de22020-12-28 20:35:05 -07001238''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -06001239
1240 def test_addresses32(self):
1241 """Test output from a node with a 'reg' property with na=1, ns=1"""
1242 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
1243 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001244 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001245 with open(output) as infile:
1246 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001247 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001248struct dtd_test1 {
1249\tfdt32_t\t\treg[2];
1250};
1251struct dtd_test2 {
1252\tfdt32_t\t\treg[4];
1253};
1254''', data)
1255
Walter Lozanoa324e412020-06-25 01:10:08 -03001256 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001257 with open(output) as infile:
1258 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001259 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001260/*
1261 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1262 *
1263 * idx driver_info driver
1264 * --- -------------------- --------------------
1265 * 0: test1 test1
1266 * 1: test2 test2
1267 * --- -------------------- --------------------
1268 */
1269
Simon Glass192f8132020-10-03 11:31:25 -06001270/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001271static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001272\t.reg\t\t\t= {0x1234, 0x5678},
1273};
Simon Glass1d8364a2020-12-28 20:34:54 -07001274U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001275\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001276\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001277\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001278\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001279};
1280
Simon Glass192f8132020-10-03 11:31:25 -06001281/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001282static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001283\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
1284};
Simon Glass1d8364a2020-12-28 20:34:54 -07001285U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001286\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001287\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001288\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001289\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001290};
1291
Simon Glass2500de22020-12-28 20:35:05 -07001292''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -06001293
1294 def test_addresses64_32(self):
1295 """Test output from a node with a 'reg' property with na=2, ns=1"""
1296 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
1297 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001298 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001299 with open(output) as infile:
1300 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001301 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001302struct dtd_test1 {
1303\tfdt64_t\t\treg[2];
1304};
1305struct dtd_test2 {
1306\tfdt64_t\t\treg[2];
1307};
1308struct dtd_test3 {
1309\tfdt64_t\t\treg[4];
1310};
1311''', data)
1312
Walter Lozanoa324e412020-06-25 01:10:08 -03001313 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001314 with open(output) as infile:
1315 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001316 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001317/*
1318 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1319 *
1320 * idx driver_info driver
1321 * --- -------------------- --------------------
1322 * 0: test1 test1
1323 * 1: test2 test2
1324 * 2: test3 test3
1325 * --- -------------------- --------------------
1326 */
1327
Simon Glass192f8132020-10-03 11:31:25 -06001328/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001329static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001330\t.reg\t\t\t= {0x123400000000, 0x5678},
1331};
Simon Glass1d8364a2020-12-28 20:34:54 -07001332U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001333\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001334\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001335\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001336\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001337};
1338
Simon Glass192f8132020-10-03 11:31:25 -06001339/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001340static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001341\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
1342};
Simon Glass1d8364a2020-12-28 20:34:54 -07001343U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001344\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001345\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001346\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001347\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001348};
1349
Simon Glass192f8132020-10-03 11:31:25 -06001350/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001351static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001352\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
1353};
Simon Glass1d8364a2020-12-28 20:34:54 -07001354U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001355\t.name\t\t= "test3",
Simon Glasse2119082021-02-03 06:01:19 -07001356\t.plat\t\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -07001357\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -06001358\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001359};
1360
Simon Glass2500de22020-12-28 20:35:05 -07001361''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -06001362
1363 def test_addresses32_64(self):
1364 """Test output from a node with a 'reg' property with na=1, ns=2"""
1365 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
1366 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001367 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001368 with open(output) as infile:
1369 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001370 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001371struct dtd_test1 {
1372\tfdt64_t\t\treg[2];
1373};
1374struct dtd_test2 {
1375\tfdt64_t\t\treg[2];
1376};
1377struct dtd_test3 {
1378\tfdt64_t\t\treg[4];
1379};
1380''', data)
1381
Walter Lozanoa324e412020-06-25 01:10:08 -03001382 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001383 with open(output) as infile:
1384 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001385 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001386/*
1387 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1388 *
1389 * idx driver_info driver
1390 * --- -------------------- --------------------
1391 * 0: test1 test1
1392 * 1: test2 test2
1393 * 2: test3 test3
1394 * --- -------------------- --------------------
1395 */
1396
Simon Glass192f8132020-10-03 11:31:25 -06001397/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001398static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001399\t.reg\t\t\t= {0x1234, 0x567800000000},
1400};
Simon Glass1d8364a2020-12-28 20:34:54 -07001401U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001402\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001403\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001404\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001405\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001406};
1407
Simon Glass192f8132020-10-03 11:31:25 -06001408/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001409static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001410\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
1411};
Simon Glass1d8364a2020-12-28 20:34:54 -07001412U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001413\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001414\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001415\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001416\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001417};
1418
Simon Glass192f8132020-10-03 11:31:25 -06001419/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001420static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001421\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
1422};
Simon Glass1d8364a2020-12-28 20:34:54 -07001423U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001424\t.name\t\t= "test3",
Simon Glasse2119082021-02-03 06:01:19 -07001425\t.plat\t\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -07001426\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -06001427\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001428};
1429
Simon Glass2500de22020-12-28 20:35:05 -07001430''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -06001431
1432 def test_bad_reg(self):
1433 """Test that a reg property with an invalid type generates an error"""
Simon Glass3bce93d2018-07-06 10:27:37 -06001434 # Capture stderr since dtc will emit warnings for this file
1435 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -06001436 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001437 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001438 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001439 self.assertIn("Node 'spl-test' reg property is not an int",
Simon Glassa60cabd2020-12-28 20:34:47 -07001440 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001441
1442 def test_bad_reg2(self):
1443 """Test that a reg property with an invalid cell count is detected"""
Simon Glass3bce93d2018-07-06 10:27:37 -06001444 # Capture stderr since dtc will emit warnings for this file
1445 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -06001446 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001447 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001448 self.run_test(['struct'], dtb_file, output)
Simon Glassa60cabd2020-12-28 20:34:47 -07001449 self.assertIn(
Simon Glass4415dc12021-03-26 16:17:27 +13001450 "Node 'spl-test' (parent '/') reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
Simon Glassa60cabd2020-12-28 20:34:47 -07001451 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001452
1453 def test_add_prop(self):
1454 """Test that a subequent node can add a new property to a struct"""
1455 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
1456 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001457 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001458 with open(output) as infile:
1459 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001460 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -06001461struct dtd_sandbox_spl_test {
1462\tfdt32_t\t\tintarray;
1463\tfdt32_t\t\tintval;
1464};
1465''', data)
1466
Walter Lozanoa324e412020-06-25 01:10:08 -03001467 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001468 with open(output) as infile:
1469 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001470 self._check_strings(C_HEADER + '''
Simon Glassbe749002021-02-03 06:01:15 -07001471/*
Simon Glasse2119082021-02-03 06:01:19 -07001472 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1473 *
1474 * idx driver_info driver
1475 * --- -------------------- --------------------
1476 * 0: spl_test sandbox_spl_test
1477 * 1: spl_test2 sandbox_spl_test
1478 * --- -------------------- --------------------
1479 */
1480
1481/*
Simon Glassbe749002021-02-03 06:01:15 -07001482 * Node /spl-test index 0
1483 * driver sandbox_spl_test parent None
1484 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001485static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001486\t.intval\t\t\t= 0x1,
1487};
Simon Glass1d8364a2020-12-28 20:34:54 -07001488U_BOOT_DRVINFO(spl_test) = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001489\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -07001490\t.plat\t\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -07001491\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -06001492\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -06001493};
1494
Simon Glassbe749002021-02-03 06:01:15 -07001495/*
1496 * Node /spl-test2 index 1
1497 * driver sandbox_spl_test parent None
1498 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001499static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001500\t.intarray\t\t= 0x5,
1501};
Simon Glass1d8364a2020-12-28 20:34:54 -07001502U_BOOT_DRVINFO(spl_test2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001503\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -07001504\t.plat\t\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001505\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001506\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -06001507};
1508
Simon Glass2500de22020-12-28 20:35:05 -07001509''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -06001510
Simon Glassa60cabd2020-12-28 20:34:47 -07001511 def test_stdout(self):
Simon Glass961c1ce2018-07-06 10:27:35 -06001512 """Test output to stdout"""
1513 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass07ee48e2020-12-28 20:34:49 -07001514 with test_util.capture_sys_output() as (stdout, _):
Simon Glass6ca0c7a2020-12-28 20:34:48 -07001515 self.run_test(['struct'], dtb_file, None)
Simon Glass07ee48e2020-12-28 20:34:49 -07001516 self._check_strings(self.struct_text, stdout.getvalue())
Simon Glass961c1ce2018-07-06 10:27:35 -06001517
Simon Glassc3a310a82020-12-28 20:34:51 -07001518 def test_multi_to_file(self):
1519 """Test output of multiple pieces to a single file"""
1520 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1521 output = tools.GetOutputFilename('output')
Simon Glass4e8e8462020-12-28 20:34:52 -07001522 self.run_test(['all'], dtb_file, output)
Simon Glassc3a310a82020-12-28 20:34:51 -07001523 data = tools.ReadFile(output, binary=False)
Simon Glassfea2f252021-02-03 06:01:21 -07001524 self._check_strings(
Simon Glass705b84b2021-04-27 08:19:48 +12001525 self.decl_text + self.platdata_text + self.struct_text, data)
Simon Glassc3a310a82020-12-28 20:34:51 -07001526
Simon Glassa60cabd2020-12-28 20:34:47 -07001527 def test_no_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -06001528 """Test running dtoc without a command"""
Simon Glassa60cabd2020-12-28 20:34:47 -07001529 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001530 self.run_test([], '', '')
Simon Glass961c1ce2018-07-06 10:27:35 -06001531 self.assertIn("Please specify a command: struct, platdata",
Simon Glassa60cabd2020-12-28 20:34:47 -07001532 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001533
Simon Glassa60cabd2020-12-28 20:34:47 -07001534 def test_bad_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -06001535 """Test running dtoc with an invalid command"""
1536 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1537 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001538 with self.assertRaises(ValueError) as exc:
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001539 self.run_test(['invalid-cmd'], dtb_file, output)
1540 self.assertIn(
Simon Glassdb2b9ca2021-03-25 06:40:51 +13001541 "Unknown command 'invalid-cmd': (use: decl, platdata, struct)",
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001542 str(exc.exception))
Walter Lozanod82062b2020-07-28 19:06:23 -03001543
Simon Glass4e8e8462020-12-28 20:34:52 -07001544 def test_output_conflict(self):
1545 """Test a conflict between and output dirs and output file"""
1546 with self.assertRaises(ValueError) as exc:
Simon Glass3809ad92021-02-03 06:01:12 -07001547 dtb_platdata.run_steps(
1548 ['all'], None, False, 'out', ['cdir'], None, False,
1549 warning_disabled=True, scan=copy_scan())
Simon Glass4e8e8462020-12-28 20:34:52 -07001550 self.assertIn("Must specify either output or output_dirs, not both",
1551 str(exc.exception))
1552
Simon Glass705b84b2021-04-27 08:19:48 +12001553 def check_output_dirs(self, instantiate):
Simon Glass4e8e8462020-12-28 20:34:52 -07001554 # Remove the directory so that files from other tests are not there
1555 tools._RemoveOutputDir()
1556 tools.PrepareOutputDir(None)
1557
1558 # This should create the .dts and .dtb in the output directory
1559 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1560 outdir = tools.GetOutputDir()
1561 fnames = glob.glob(outdir + '/*')
1562 self.assertEqual(2, len(fnames))
1563
Simon Glass3809ad92021-02-03 06:01:12 -07001564 dtb_platdata.run_steps(
Simon Glass705b84b2021-04-27 08:19:48 +12001565 ['all'], dtb_file, False, None, [outdir], None, instantiate,
Simon Glass3809ad92021-02-03 06:01:12 -07001566 warning_disabled=True, scan=copy_scan())
Simon Glass4e8e8462020-12-28 20:34:52 -07001567 fnames = glob.glob(outdir + '/*')
Simon Glass705b84b2021-04-27 08:19:48 +12001568 return fnames
1569
1570 def test_output_dirs(self):
1571 """Test outputting files to a directory"""
1572 fnames = self.check_output_dirs(False)
1573 self.assertEqual(5, len(fnames))
Simon Glass4e8e8462020-12-28 20:34:52 -07001574
1575 leafs = set(os.path.basename(fname) for fname in fnames)
1576 self.assertEqual(
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001577 {'dt-structs-gen.h', 'source.dts', 'dt-plat.c', 'source.dtb',
Simon Glass705b84b2021-04-27 08:19:48 +12001578 'dt-decl.h'},
1579 leafs)
1580
1581 def test_output_dirs_inst(self):
1582 """Test outputting files to a directory with instantiation"""
1583 fnames = self.check_output_dirs(True)
1584 self.assertEqual(6, len(fnames))
1585
1586 leafs = set(os.path.basename(fname) for fname in fnames)
1587 self.assertEqual(
1588 {'dt-structs-gen.h', 'source.dts', 'source.dtb',
Simon Glassfea2f252021-02-03 06:01:21 -07001589 'dt-uclass.c', 'dt-decl.h', 'dt-device.c'},
Simon Glass4e8e8462020-12-28 20:34:52 -07001590 leafs)
Simon Glass047a4802021-02-03 06:01:00 -07001591
1592 def setup_process_test(self):
1593 """Set up a test of process_nodes()
1594
1595 This uses saved_scan but returns a deep copy of it, so it is safe to
1596 modify it in these tests
1597
1598 Returns:
1599 tuple:
1600 DtbPlatdata: object to test
1601 Scanner: scanner to use
1602 """
1603 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1604 output = tools.GetOutputFilename('output')
1605
1606 # Take a copy before messing with it
Simon Glassc14fd0c2021-02-03 06:01:11 -07001607 scan = copy_scan()
Simon Glass047a4802021-02-03 06:01:00 -07001608 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
1609 plat.scan_dtb()
Simon Glassc14fd0c2021-02-03 06:01:11 -07001610 plat.scan_tree(False)
Simon Glass047a4802021-02-03 06:01:00 -07001611 plat.prepare_nodes()
1612 return plat, scan
1613
1614 def test_process_nodes(self):
1615 """Test processing nodes to add various info"""
1616 plat, scan = self.setup_process_test()
1617 plat.process_nodes(True)
1618
1619 i2c_node = plat._fdt.GetNode('/i2c@0')
1620 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
1621 pmic = scan._drivers['sandbox_pmic']
1622 i2c = scan._drivers['sandbox_i2c']
1623 self.assertEqual('DM_DEVICE_REF(pmic_at_9)', pmic_node.dev_ref)
1624 self.assertEqual(pmic, pmic_node.driver)
1625 self.assertEqual(i2c_node, pmic_node.parent)
1626 self.assertEqual(i2c, pmic_node.parent_driver)
1627
1628 # The pmic is the only child
1629 self.assertEqual(pmic_node.parent_seq, 0)
1630 self.assertEqual([pmic_node], i2c_node.child_devs)
1631
1632 # Start and end of the list should be the child_head
1633 ref = '&DM_DEVICE_REF(i2c_at_0)->child_head'
1634 self.assertEqual(
1635 {-1: ref, 0: '&DM_DEVICE_REF(pmic_at_9)->sibling_node', 1: ref},
1636 i2c_node.child_refs)
1637
1638 def test_process_nodes_bad_parent(self):
1639 # Pretend that i2c has a parent (the pmic) and delete that driver
1640 plat, scan = self.setup_process_test()
1641
1642 i2c_node = plat._fdt.GetNode('/i2c@0')
1643 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
1644 del scan._drivers['sandbox_pmic']
1645 i2c_node.parent = pmic_node
1646
1647 # Process twice, the second time to generate an exception
1648 plat.process_nodes(False)
1649 with self.assertRaises(ValueError) as exc:
1650 plat.process_nodes(True)
1651 self.assertIn(
1652 "Cannot parse/find parent driver 'sandbox_pmic' for 'sandbox_i2c",
1653 str(exc.exception))
1654
1655 def test_process_nodes_bad_node(self):
1656 plat, scan = self.setup_process_test()
1657
1658 # Now remove the pmic driver
1659 del scan._drivers['sandbox_pmic']
1660
1661 # Process twice, the second time to generate an exception
1662 plat.process_nodes(False)
1663 with self.assertRaises(ValueError) as exc:
1664 plat.process_nodes(True)
1665 self.assertIn("Cannot parse/find driver for 'sandbox_pmic",
1666 str(exc.exception))
Simon Glasseb3c2492021-02-03 06:01:01 -07001667
Simon Glass80d782c42021-02-03 06:01:10 -07001668 def test_process_nodes_bad_uclass(self):
1669 plat, scan = self.setup_process_test()
1670
1671 self.assertIn('UCLASS_I2C', scan._uclass)
1672 del scan._uclass['UCLASS_I2C']
1673 with self.assertRaises(ValueError) as exc:
1674 plat.process_nodes(True)
1675 self.assertIn("Cannot parse/find uclass 'UCLASS_I2C' for driver 'sandbox_i2c'",
1676 str(exc.exception))
1677
Simon Glasseb3c2492021-02-03 06:01:01 -07001678 def test_process_nodes_used(self):
1679 """Test processing nodes to add various info"""
1680 plat, scan = self.setup_process_test()
1681 plat.process_nodes(True)
1682
1683 pmic = scan._drivers['sandbox_pmic']
1684 self.assertTrue(pmic.used)
1685
1686 gpio = scan._drivers['sandbox_gpio']
1687 self.assertFalse(gpio.used)
Simon Glassbe88d2f2021-02-03 06:01:07 -07001688
1689 def test_alias_read(self):
1690 """Test obtaining aliases"""
1691 dtb_file = get_dtb_file('dtoc_test_inst.dts')
1692 output = tools.GetOutputFilename('output')
1693 plat = self.run_test(['struct'], dtb_file, output)
1694
1695 scan = plat._scan
1696 testfdt_node = plat._fdt.GetNode('/some-bus/test')
Simon Glass80d782c42021-02-03 06:01:10 -07001697 test0_node = plat._fdt.GetNode('/some-bus/test0')
Simon Glassbe88d2f2021-02-03 06:01:07 -07001698 self.assertIn('UCLASS_TEST_FDT', scan._uclass)
1699 uc = scan._uclass['UCLASS_TEST_FDT']
Simon Glass80d782c42021-02-03 06:01:10 -07001700 self.assertEqual({1: testfdt_node, 2: test0_node},
1701 uc.alias_num_to_node)
1702 self.assertEqual({'/some-bus/test': 1, '/some-bus/test0': 2},
1703 uc.alias_path_to_num)
Simon Glassbe88d2f2021-02-03 06:01:07 -07001704
1705 # Try adding an alias that doesn't exist
1706 self.assertFalse(scan.add_uclass_alias('fred', 3, None))
1707
1708 # Try adding an alias for a missing node
1709 self.assertIsNone(scan.add_uclass_alias('testfdt', 3, None))
1710
1711 def test_alias_read_bad(self):
1712 """Test invalid alias property name"""
1713 dtb_file = get_dtb_file('dtoc_test_alias_bad.dts')
1714 output = tools.GetOutputFilename('output')
1715 with self.assertRaises(ValueError) as exc:
1716 plat = self.run_test(['struct'], dtb_file, output)
1717 self.assertIn("Cannot decode alias 'i2c4-'", str(exc.exception))
1718
1719 def test_alias_read_bad_path(self):
1720 """Test alias pointing to a non-existent node"""
1721 # This line may produce a warning, so capture it:
1722 # Warning (alias_paths): /aliases:i2c4: aliases property is not a valid
1723 # node (/does/not/exist)
1724 dtb_file = get_dtb_file('dtoc_test_alias_bad_path.dts', True)
1725
1726 output = tools.GetOutputFilename('output')
1727 with self.assertRaises(ValueError) as exc:
1728 plat = self.run_test(['struct'], dtb_file, output)
1729 self.assertIn("Alias 'i2c4' path '/does/not/exist' not found",
1730 str(exc.exception))
1731
1732 def test_alias_read_bad_uclass(self):
1733 """Test alias for a uclass that doesn't exist"""
1734 dtb_file = get_dtb_file('dtoc_test_alias_bad_uc.dts')
1735 output = tools.GetOutputFilename('output')
1736 with test_util.capture_sys_output() as (stdout, _):
1737 plat = self.run_test(['struct'], dtb_file, output)
1738 self.assertEqual("Could not find uclass for alias 'other1'",
1739 stdout.getvalue().strip())
Simon Glassdf56e0b2021-02-03 06:01:09 -07001740
1741 def test_sequence(self):
1742 """Test assignment of sequence numnbers"""
1743 dtb_file = get_dtb_file('dtoc_test_inst.dts')
1744 output = tools.GetOutputFilename('output')
1745 plat = self.run_test(['struct'], dtb_file, output)
Simon Glass80d782c42021-02-03 06:01:10 -07001746
1747 scan = plat._scan
1748 testfdt = plat._fdt.GetNode('/some-bus/test')
1749 self.assertEqual(1, testfdt.seq)
1750 i2c = plat._fdt.GetNode('/i2c')
1751
1752 # For now this uclass is not compiled in, so no sequence is assigned
1753 self.assertEqual(4, i2c.seq)
1754 spl = plat._fdt.GetNode('/spl-test')
1755 self.assertEqual(0, spl.seq)
Simon Glassc14fd0c2021-02-03 06:01:11 -07001756
1757 def test_process_root(self):
1758 """Test assignment of sequence numnbers"""
1759 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1760 output = tools.GetOutputFilename('output')
1761
1762 # Take a copy before messing with it
1763 scan = copy_scan()
1764 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
1765 plat.scan_dtb()
1766 root = plat._fdt.GetRoot()
1767
1768 plat.scan_tree(False)
1769 self.assertNotIn(root, plat._valid_nodes)
1770
1771 plat.scan_tree(True)
1772 self.assertIn(root, plat._valid_nodes)
1773 self.assertEqual('root_driver',
1774 scan.get_normalized_compat_name(root)[0])
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001775
1776 def test_simple_inst(self):
1777 """Test output from some simple nodes with instantiate enabled"""
Simon Glassc7b4b832021-02-03 06:01:20 -07001778 dtb_file = get_dtb_file('dtoc_test_inst.dts')
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001779 output = tools.GetOutputFilename('output')
1780
1781 self.run_test(['decl'], dtb_file, output, True)
1782 with open(output) as infile:
1783 data = infile.read()
1784
1785 self._check_strings(self.decl_text_inst, data)
Simon Glassbe749002021-02-03 06:01:15 -07001786
Simon Glassc7b4b832021-02-03 06:01:20 -07001787 self.run_test(['uclass'], dtb_file, output, True)
1788 with open(output) as infile:
1789 data = infile.read()
1790
1791 self._check_strings(UCLASS_HEADER_COMMON + self.uclass_text_inst, data)
1792
Simon Glassfea2f252021-02-03 06:01:21 -07001793 self.run_test(['device'], dtb_file, output, True)
1794 with open(output) as infile:
1795 data = infile.read()
1796
1797 self._check_strings(self.device_text_inst, data)
1798
Simon Glassc7b4b832021-02-03 06:01:20 -07001799 def test_inst_no_hdr(self):
Simon Glassfea2f252021-02-03 06:01:21 -07001800 """Test dealing with a struct tsssshat has no header"""
Simon Glassc7b4b832021-02-03 06:01:20 -07001801 dtb_file = get_dtb_file('dtoc_test_inst.dts')
1802 output = tools.GetOutputFilename('output')
1803
1804 # Run it once to set everything up
1805 plat = self.run_test(['decl'], dtb_file, output, True)
1806 scan = plat._scan
1807
1808 # Restart the output file and delete any record of the uclass' struct
1809 plat.setup_output(Ftype.SOURCE, output)
1810 del scan._structs['dm_test_uc_priv']
1811
1812 # Now generate the uclasses, which should provide a warning
1813 with test_util.capture_sys_output() as (stdout, _):
1814 plat.generate_uclasses()
1815 self.assertEqual(
1816 'Warning: Cannot find header file for struct dm_test_uc_priv',
1817 stdout.getvalue().strip())
Simon Glass4415dc12021-03-26 16:17:27 +13001818
1819 def test_missing_props(self):
1820 """Test detection of a parent node with no properties"""
1821 dtb_file = get_dtb_file('dtoc_test_noprops.dts', capture_stderr=True)
1822 output = tools.GetOutputFilename('output')
1823 with self.assertRaises(ValueError) as exc:
1824 self.run_test(['struct'], dtb_file, output)
1825 self.assertIn("Parent node '/i2c@0' has no properties - do you need",
1826 str(exc.exception))
1827
1828 def test_single_reg(self):
1829 """Test detection of a parent node with no properties"""
1830 dtb_file = get_dtb_file('dtoc_test_single_reg.dts')
1831 output = tools.GetOutputFilename('output')
1832 self.run_test(['struct'], dtb_file, output)
Simon Glass9ebd5512021-06-27 17:51:10 -06001833
1834 def test_missing_parent(self):
1835 """Test detection of a parent node with no properties"""
1836 dtb_file = get_dtb_file('dtoc_test_noparent.dts', capture_stderr=True)
1837 output = tools.GetOutputFilename('output')
1838 with self.assertRaises(ValueError) as exc:
1839 self.run_test(['device'], dtb_file, output, instantiate=True)
1840 self.assertIn("Node '/i2c@0/spl-test/pmic@9' requires parent node "
1841 "'/i2c@0/spl-test' but it is not in the valid list",
1842 str(exc.exception))