blob: e95128345740e7ff50d5203c950822e7cb62aa8d [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
77UCLASS_HEADER = UCLASS_HEADER_COMMON + '''
78/* This file is not used: --instantiate was not enabled */
79'''
80
Simon Glass768ff0a2021-02-03 06:00:51 -070081# Scanner saved from a previous run of the tests (to speed things up)
82saved_scan = None
83
Simon Glassa60cabd2020-12-28 20:34:47 -070084# This is a test so is allowed to access private things in the module it is
85# testing
86# pylint: disable=W0212
Simon Glass3bce93d2018-07-06 10:27:37 -060087
88def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glass9d2eb922017-06-18 22:09:06 -060089 """Compile a .dts file to a .dtb
90
91 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -070092 dts_fname (str): Filename of .dts file in the current directory
93 capture_stderr (bool): True to capture and discard stderr output
Simon Glass9d2eb922017-06-18 22:09:06 -060094
95 Returns:
Simon Glassa60cabd2020-12-28 20:34:47 -070096 str: Filename of compiled file in output directory
Simon Glass9d2eb922017-06-18 22:09:06 -060097 """
Simon Glass4f4b2402021-02-03 06:00:56 -070098 return fdt_util.EnsureCompiled(os.path.join(OUR_PATH, 'test', dts_fname),
Simon Glass3bce93d2018-07-06 10:27:37 -060099 capture_stderr=capture_stderr)
Simon Glass9d2eb922017-06-18 22:09:06 -0600100
Simon Glass768ff0a2021-02-03 06:00:51 -0700101
102def setup():
103 global saved_scan
104
105 # Disable warnings so that calls to get_normalized_compat_name() will not
106 # output things.
107 saved_scan = src_scan.Scanner(None, True, False)
108 saved_scan.scan_drivers()
109
110def copy_scan():
111 """Get a copy of saved_scan so that each test can start clean"""
112 return copy.deepcopy(saved_scan)
113
Simon Glass9d2eb922017-06-18 22:09:06 -0600114
115class TestDtoc(unittest.TestCase):
116 """Tests for dtoc"""
117 @classmethod
118 def setUpClass(cls):
119 tools.PrepareOutputDir(None)
Simon Glass7f5e2262020-07-07 21:32:06 -0600120 cls.maxDiff = None
Simon Glass9d2eb922017-06-18 22:09:06 -0600121
122 @classmethod
123 def tearDownClass(cls):
Simon Glassa60cabd2020-12-28 20:34:47 -0700124 tools.FinaliseOutputDir()
Simon Glass9d2eb922017-06-18 22:09:06 -0600125
Simon Glassa60cabd2020-12-28 20:34:47 -0700126 @staticmethod
127 def _write_python_string(fname, data):
Simon Glassc47c2b32018-07-06 10:27:25 -0600128 """Write a string with tabs expanded as done in this Python file
129
130 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -0700131 fname (str): Filename to write to
132 data (str): Raw string to convert
Simon Glassc47c2b32018-07-06 10:27:25 -0600133 """
134 data = data.replace('\t', '\\t')
Simon Glassa60cabd2020-12-28 20:34:47 -0700135 with open(fname, 'w') as fout:
136 fout.write(data)
Simon Glassc47c2b32018-07-06 10:27:25 -0600137
Simon Glassa60cabd2020-12-28 20:34:47 -0700138 def _check_strings(self, expected, actual):
Simon Glassc47c2b32018-07-06 10:27:25 -0600139 """Check that a string matches its expected value
140
141 If the strings do not match, they are written to the /tmp directory in
142 the same Python format as is used here in the test. This allows for
143 easy comparison and update of the tests.
144
145 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -0700146 expected (str): Expected string
147 actual (str): Actual string
Simon Glassc47c2b32018-07-06 10:27:25 -0600148 """
149 if expected != actual:
Simon Glassa60cabd2020-12-28 20:34:47 -0700150 self._write_python_string('/tmp/binman.expected', expected)
151 self._write_python_string('/tmp/binman.actual', actual)
Simon Glass61b88e52019-05-17 22:00:31 -0600152 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glassa60cabd2020-12-28 20:34:47 -0700153 self.assertEqual(expected, actual)
Simon Glassc47c2b32018-07-06 10:27:25 -0600154
Simon Glassa60cabd2020-12-28 20:34:47 -0700155 @staticmethod
Simon Glass3809ad92021-02-03 06:01:12 -0700156 def run_test(args, dtb_file, output, instantiate=False):
Simon Glassa60cabd2020-12-28 20:34:47 -0700157 """Run a test using dtoc
Walter Lozanoa324e412020-06-25 01:10:08 -0300158
Simon Glassa60cabd2020-12-28 20:34:47 -0700159 Args:
160 args (list of str): List of arguments for dtoc
161 dtb_file (str): Filename of .dtb file
162 output (str): Filename of output file
Simon Glassbe88d2f2021-02-03 06:01:07 -0700163
164 Returns:
165 DtbPlatdata object
Simon Glassa60cabd2020-12-28 20:34:47 -0700166 """
Simon Glass80d782c42021-02-03 06:01:10 -0700167 # Make a copy of the 'scan' object, since it includes uclasses and
168 # drivers, which get updated during execution.
Simon Glass3809ad92021-02-03 06:01:12 -0700169 return dtb_platdata.run_steps(
170 args, dtb_file, False, output, [], None, instantiate,
171 warning_disabled=True, scan=copy_scan())
Walter Lozanoa324e412020-06-25 01:10:08 -0300172
Simon Glass9d2eb922017-06-18 22:09:06 -0600173 def test_name(self):
174 """Test conversion of device tree names to C identifiers"""
175 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
176 self.assertEqual('vendor_clock_frequency',
177 conv_name_to_c('vendor,clock-frequency'))
178 self.assertEqual('rockchip_rk3399_sdhci_5_1',
179 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
180
181 def test_tab_to(self):
182 """Test operation of tab_to() function"""
183 self.assertEqual('fred ', tab_to(0, 'fred'))
184 self.assertEqual('fred\t', tab_to(1, 'fred'))
185 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
186 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
187 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
188 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
189
190 def test_get_value(self):
191 """Test operation of get_value() function"""
192 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700193 get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600194 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700195 get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600196 self.assertEqual('0x0',
Simon Glassc9a032c2020-11-08 20:36:17 -0700197 get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
198 self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
199 self.assertEqual('true', get_value(fdt.Type.BOOL, None))
Simon Glass9d2eb922017-06-18 22:09:06 -0600200
201 def test_get_compat_name(self):
202 """Test operation of get_compat_name() function"""
203 Prop = collections.namedtuple('Prop', ['value'])
204 Node = collections.namedtuple('Node', ['props'])
205
206 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
207 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300208 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600209 get_compat_name(node))
210
211 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
212 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300213 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600214 get_compat_name(node))
215
216 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
217 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300218 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
Simon Glassa60cabd2020-12-28 20:34:47 -0700219 'arasan_sdhci_5_1', 'third']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600220 get_compat_name(node))
221
222 def test_empty_file(self):
223 """Test output from a device tree file with no nodes"""
224 dtb_file = get_dtb_file('dtoc_test_empty.dts')
225 output = tools.GetOutputFilename('output')
Simon Glass768ff0a2021-02-03 06:00:51 -0700226
227 # Run this one without saved_scan to complete test coverage
Simon Glass3809ad92021-02-03 06:01:12 -0700228 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [], None,
229 False)
Simon Glass9d2eb922017-06-18 22:09:06 -0600230 with open(output) as infile:
231 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700232 self.assertEqual(HEADER.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600233
Walter Lozanoa324e412020-06-25 01:10:08 -0300234 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600235 with open(output) as infile:
236 lines = infile.read().splitlines()
Simon Glass2500de22020-12-28 20:35:05 -0700237 self.assertEqual(C_HEADER.splitlines() + [''], lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600238
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700239 decl_text = DECL_HEADER + '''
240#include <dm/device-internal.h>
241#include <dm/uclass-internal.h>
242
243/* driver declarations - these allow DM_DRIVER_GET() to be used */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300244extern U_BOOT_DRIVER(sandbox_i2c);
245extern U_BOOT_DRIVER(sandbox_pmic);
246extern U_BOOT_DRIVER(sandbox_spl_test);
247extern U_BOOT_DRIVER(sandbox_spl_test);
248extern U_BOOT_DRIVER(sandbox_spl_test);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700249
250/* uclass driver declarations - needed for DM_UCLASS_DRIVER_REF() */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300251extern UCLASS_DRIVER(i2c);
252extern UCLASS_DRIVER(misc);
253extern UCLASS_DRIVER(pmic);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700254'''
255 decl_text_inst = DECL_HEADER + '''
256#include <dm/device-internal.h>
257#include <dm/uclass-internal.h>
258
259/* driver declarations - these allow DM_DRIVER_GET() to be used */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300260extern U_BOOT_DRIVER(sandbox_i2c);
261extern U_BOOT_DRIVER(root_driver);
262extern U_BOOT_DRIVER(denx_u_boot_test_bus);
263extern U_BOOT_DRIVER(sandbox_spl_test);
264extern U_BOOT_DRIVER(sandbox_spl_test);
265extern U_BOOT_DRIVER(denx_u_boot_fdt_test);
266extern U_BOOT_DRIVER(denx_u_boot_fdt_test);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700267
268/* device declarations - these allow DM_DEVICE_REF() to be used */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300269extern DM_DEVICE_INST(i2c);
270extern DM_DEVICE_INST(root);
271extern DM_DEVICE_INST(some_bus);
272extern DM_DEVICE_INST(spl_test);
273extern DM_DEVICE_INST(spl_test3);
274extern DM_DEVICE_INST(test);
275extern DM_DEVICE_INST(test0);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700276
277/* uclass driver declarations - needed for DM_UCLASS_DRIVER_REF() */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300278extern UCLASS_DRIVER(i2c);
279extern UCLASS_DRIVER(misc);
280extern UCLASS_DRIVER(root);
281extern UCLASS_DRIVER(testbus);
282extern UCLASS_DRIVER(testfdt);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700283
284/* uclass declarations - needed for DM_UCLASS_REF() */
Simon Glassed5e6ae2021-03-15 17:25:11 +1300285extern DM_UCLASS_INST(i2c);
286extern DM_UCLASS_INST(misc);
287extern DM_UCLASS_INST(root);
288extern DM_UCLASS_INST(testbus);
289extern DM_UCLASS_INST(testfdt);
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700290'''
Simon Glass07ee48e2020-12-28 20:34:49 -0700291 struct_text = HEADER + '''
Simon Glass32a8d222021-02-03 06:00:57 -0700292struct dtd_sandbox_i2c {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600293};
Simon Glass32a8d222021-02-03 06:00:57 -0700294struct dtd_sandbox_pmic {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600295\tbool\t\tlow_power;
296\tfdt64_t\t\treg[2];
297};
Simon Glass9d2eb922017-06-18 22:09:06 -0600298struct dtd_sandbox_spl_test {
Simon Glass7f5e2262020-07-07 21:32:06 -0600299\tconst char * acpi_name;
Simon Glass9d2eb922017-06-18 22:09:06 -0600300\tbool\t\tboolval;
301\tunsigned char\tbytearray[3];
302\tunsigned char\tbyteval;
303\tfdt32_t\t\tintarray[4];
304\tfdt32_t\t\tintval;
305\tunsigned char\tlongbytearray[9];
Simon Glass9c526332018-07-06 10:27:28 -0600306\tunsigned char\tnotstring[5];
Simon Glass9d2eb922017-06-18 22:09:06 -0600307\tconst char *\tstringarray[3];
308\tconst char *\tstringval;
309};
Simon Glass07ee48e2020-12-28 20:34:49 -0700310'''
Simon Glass07ee48e2020-12-28 20:34:49 -0700311 platdata_text = C_HEADER + '''
Simon Glassbe749002021-02-03 06:01:15 -0700312/*
Simon Glasse2119082021-02-03 06:01:19 -0700313 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
314 *
315 * idx driver_info driver
316 * --- -------------------- --------------------
317 * 0: i2c_at_0 sandbox_i2c
318 * 1: pmic_at_9 sandbox_pmic
319 * 2: spl_test sandbox_spl_test
320 * 3: spl_test2 sandbox_spl_test
321 * 4: spl_test3 sandbox_spl_test
322 * --- -------------------- --------------------
323 */
324
325/*
Simon Glassbe749002021-02-03 06:01:15 -0700326 * Node /i2c@0 index 0
327 * driver sandbox_i2c parent None
328 */
Simon Glass32a8d222021-02-03 06:00:57 -0700329static struct dtd_sandbox_i2c dtv_i2c_at_0 = {
Simon Glass192f8132020-10-03 11:31:25 -0600330};
Simon Glass1d8364a2020-12-28 20:34:54 -0700331U_BOOT_DRVINFO(i2c_at_0) = {
Simon Glass32a8d222021-02-03 06:00:57 -0700332\t.name\t\t= "sandbox_i2c",
Simon Glasse2119082021-02-03 06:01:19 -0700333\t.plat\t\t= &dtv_i2c_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700334\t.plat_size\t= sizeof(dtv_i2c_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600335\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600336};
337
Simon Glassbe749002021-02-03 06:01:15 -0700338/*
339 * Node /i2c@0/pmic@9 index 1
340 * driver sandbox_pmic parent sandbox_i2c
341 */
Simon Glass32a8d222021-02-03 06:00:57 -0700342static struct dtd_sandbox_pmic dtv_pmic_at_9 = {
Simon Glass192f8132020-10-03 11:31:25 -0600343\t.low_power\t\t= true,
344\t.reg\t\t\t= {0x9, 0x0},
345};
Simon Glass1d8364a2020-12-28 20:34:54 -0700346U_BOOT_DRVINFO(pmic_at_9) = {
Simon Glass32a8d222021-02-03 06:00:57 -0700347\t.name\t\t= "sandbox_pmic",
Simon Glasse2119082021-02-03 06:01:19 -0700348\t.plat\t\t= &dtv_pmic_at_9,
Simon Glass39edb952020-12-03 16:55:19 -0700349\t.plat_size\t= sizeof(dtv_pmic_at_9),
Simon Glass36b15e22020-10-03 11:31:35 -0600350\t.parent_idx\t= 0,
Simon Glass192f8132020-10-03 11:31:25 -0600351};
352
Simon Glassbe749002021-02-03 06:01:15 -0700353/*
354 * Node /spl-test index 2
355 * driver sandbox_spl_test parent None
356 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300357static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glassc82de562019-05-17 22:00:32 -0600358\t.boolval\t\t= true,
Simon Glass9d2eb922017-06-18 22:09:06 -0600359\t.bytearray\t\t= {0x6, 0x0, 0x0},
360\t.byteval\t\t= 0x5,
Simon Glassc82de562019-05-17 22:00:32 -0600361\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600362\t.intval\t\t\t= 0x1,
Simon Glass131e0b02017-08-29 14:15:49 -0600363\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
364\t\t0x11},
Simon Glassc82de562019-05-17 22:00:32 -0600365\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600366\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glassc82de562019-05-17 22:00:32 -0600367\t.stringval\t\t= "message",
Simon Glass9d2eb922017-06-18 22:09:06 -0600368};
Simon Glass1d8364a2020-12-28 20:34:54 -0700369U_BOOT_DRVINFO(spl_test) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600370\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -0700371\t.plat\t\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700372\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600373\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600374};
375
Simon Glassbe749002021-02-03 06:01:15 -0700376/*
377 * Node /spl-test2 index 3
378 * driver sandbox_spl_test parent None
379 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300380static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass7f5e2262020-07-07 21:32:06 -0600381\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glass9d2eb922017-06-18 22:09:06 -0600382\t.bytearray\t\t= {0x1, 0x23, 0x34},
383\t.byteval\t\t= 0x8,
Simon Glassc82de562019-05-17 22:00:32 -0600384\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600385\t.intval\t\t\t= 0x3,
Simon Glass8034e4d2020-10-03 11:31:27 -0600386\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
Simon Glass131e0b02017-08-29 14:15:49 -0600387\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600388\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glassc82de562019-05-17 22:00:32 -0600389\t.stringval\t\t= "message2",
Simon Glass9d2eb922017-06-18 22:09:06 -0600390};
Simon Glass1d8364a2020-12-28 20:34:54 -0700391U_BOOT_DRVINFO(spl_test2) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600392\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -0700393\t.plat\t\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700394\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600395\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600396};
397
Simon Glassbe749002021-02-03 06:01:15 -0700398/*
399 * Node /spl-test3 index 4
400 * driver sandbox_spl_test parent None
401 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300402static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glass8034e4d2020-10-03 11:31:27 -0600403\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
404\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600405\t.stringarray\t\t= {"one", "", ""},
406};
Simon Glass1d8364a2020-12-28 20:34:54 -0700407U_BOOT_DRVINFO(spl_test3) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600408\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -0700409\t.plat\t\t= &dtv_spl_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700410\t.plat_size\t= sizeof(dtv_spl_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600411\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600412};
413
Simon Glass2500de22020-12-28 20:35:05 -0700414'''
Simon Glassc7b4b832021-02-03 06:01:20 -0700415 uclass_text = UCLASS_HEADER
416 uclass_text_inst = '''
417
418#include <common.h>
419#include <dm.h>
420#include <dt-structs.h>
421
422/*
Simon Glassfea2f252021-02-03 06:01:21 -0700423 * uclass declarations, ordered by 'struct uclass' linker_list idx:
424 * 0: i2c
425 * 1: misc
426 * 2: root
427 * 3: testbus
428 * 4: testfdt
Simon Glassc7b4b832021-02-03 06:01:20 -0700429 *
Simon Glassfea2f252021-02-03 06:01:21 -0700430 * Sequence numbers allocated in each uclass:
Simon Glassc7b4b832021-02-03 06:01:20 -0700431 * i2c: UCLASS_I2C
432 * 4: /i2c
433 * misc: UCLASS_MISC
434 * 0: /spl-test
435 * 1: /spl-test3
436 * root: UCLASS_ROOT
437 * 0: /
438 * testbus: UCLASS_TEST_BUS
439 * 2: /some-bus
440 * testfdt: UCLASS_TEST_FDT
441 * 1: /some-bus/test
442 * 2: /some-bus/test0
443 */
444
445struct list_head uclass_head = {
446 .prev = &DM_UCLASS_REF(testfdt)->sibling_node,
447 .next = &DM_UCLASS_REF(i2c)->sibling_node,
448};
449
450DM_UCLASS_INST(i2c) = {
451 .uc_drv = DM_UCLASS_DRIVER_REF(i2c),
452 .sibling_node = {
453 .prev = &uclass_head,
454 .next = &DM_UCLASS_REF(misc)->sibling_node,
455 },
456 .dev_head = {
457 .prev = &DM_DEVICE_REF(i2c)->uclass_node,
458 .next = &DM_DEVICE_REF(i2c)->uclass_node,
459 },
460};
461
462DM_UCLASS_INST(misc) = {
463 .uc_drv = DM_UCLASS_DRIVER_REF(misc),
464 .sibling_node = {
465 .prev = &DM_UCLASS_REF(i2c)->sibling_node,
466 .next = &DM_UCLASS_REF(root)->sibling_node,
467 },
468 .dev_head = {
469 .prev = &DM_DEVICE_REF(spl_test3)->uclass_node,
470 .next = &DM_DEVICE_REF(spl_test)->uclass_node,
471 },
472};
473
474DM_UCLASS_INST(root) = {
475 .uc_drv = DM_UCLASS_DRIVER_REF(root),
476 .sibling_node = {
477 .prev = &DM_UCLASS_REF(misc)->sibling_node,
478 .next = &DM_UCLASS_REF(testbus)->sibling_node,
479 },
480 .dev_head = {
481 .prev = &DM_DEVICE_REF(root)->uclass_node,
482 .next = &DM_DEVICE_REF(root)->uclass_node,
483 },
484};
485
486DM_UCLASS_INST(testbus) = {
487 .uc_drv = DM_UCLASS_DRIVER_REF(testbus),
488 .sibling_node = {
489 .prev = &DM_UCLASS_REF(root)->sibling_node,
490 .next = &DM_UCLASS_REF(testfdt)->sibling_node,
491 },
492 .dev_head = {
493 .prev = &DM_DEVICE_REF(some_bus)->uclass_node,
494 .next = &DM_DEVICE_REF(some_bus)->uclass_node,
495 },
496};
497
498#include <dm/test.h>
499u8 _testfdt_priv_[sizeof(struct dm_test_uc_priv)]
500 __attribute__ ((section (".priv_data")));
501DM_UCLASS_INST(testfdt) = {
502 .priv_ = _testfdt_priv_,
503 .uc_drv = DM_UCLASS_DRIVER_REF(testfdt),
504 .sibling_node = {
505 .prev = &DM_UCLASS_REF(testbus)->sibling_node,
506 .next = &uclass_head,
507 },
508 .dev_head = {
509 .prev = &DM_DEVICE_REF(test0)->uclass_node,
510 .next = &DM_DEVICE_REF(test)->uclass_node,
511 },
512};
513
514'''
Simon Glassfea2f252021-02-03 06:01:21 -0700515 device_text = '''/*
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/* This file is not used: --instantiate was not enabled */
523'''
524 device_text_inst = '''/*
525 * DO NOT MODIFY
526 *
527 * Declares the DM_DEVICE_INST() records.
528 * This was generated by dtoc from a .dtb (device tree binary) file.
529 */
530
531#include <common.h>
532#include <dm.h>
533#include <dt-structs.h>
534
535/*
536 * udevice declarations, ordered by 'struct udevice' linker_list position:
537 *
538 * idx udevice driver
539 * --- -------------------- --------------------
540 * 0: i2c sandbox_i2c
541 * 1: root root_driver
542 * 2: some_bus denx_u_boot_test_bus
543 * 3: spl_test sandbox_spl_test
544 * 4: spl_test3 sandbox_spl_test
545 * 5: test denx_u_boot_fdt_test
546 * 6: test0 denx_u_boot_fdt_test
547 * --- -------------------- --------------------
548 */
549
550/*
551 * Node /i2c index 0
552 * driver sandbox_i2c parent root_driver
553*/
554static struct dtd_sandbox_i2c dtv_i2c = {
555\t.intval\t\t\t= 0x3,
556};
557
558#include <asm/i2c.h>
559u8 _sandbox_i2c_priv_i2c[sizeof(struct sandbox_i2c_priv)]
560\t__attribute__ ((section (".priv_data")));
561#include <i2c.h>
562u8 _sandbox_i2c_uc_priv_i2c[sizeof(struct dm_i2c_bus)]
563\t__attribute__ ((section (".priv_data")));
564
565DM_DEVICE_INST(i2c) = {
566\t.driver\t\t= DM_DRIVER_REF(sandbox_i2c),
567\t.name\t\t= "sandbox_i2c",
568\t.plat_\t\t= &dtv_i2c,
569\t.priv_\t\t= _sandbox_i2c_priv_i2c,
570\t.uclass\t\t= DM_UCLASS_REF(i2c),
571\t.uclass_priv_ = _sandbox_i2c_uc_priv_i2c,
572\t.uclass_node\t= {
573\t\t.prev = &DM_UCLASS_REF(i2c)->dev_head,
574\t\t.next = &DM_UCLASS_REF(i2c)->dev_head,
575\t},
576\t.child_head\t= {
577\t\t.prev = &DM_DEVICE_REF(i2c)->child_head,
578\t\t.next = &DM_DEVICE_REF(i2c)->child_head,
579\t},
580\t.sibling_node\t= {
581\t\t.prev = &DM_DEVICE_REF(root)->child_head,
582\t\t.next = &DM_DEVICE_REF(some_bus)->sibling_node,
583\t},
584\t.seq_ = 4,
585};
586
587/*
588 * Node / index 1
589 * driver root_driver parent None
590*/
591static struct dtd_root_driver dtv_root = {
592};
593
594DM_DEVICE_INST(root) = {
595\t.driver\t\t= DM_DRIVER_REF(root_driver),
596\t.name\t\t= "root_driver",
597\t.plat_\t\t= &dtv_root,
598\t.uclass\t\t= DM_UCLASS_REF(root),
599\t.uclass_node\t= {
600\t\t.prev = &DM_UCLASS_REF(root)->dev_head,
601\t\t.next = &DM_UCLASS_REF(root)->dev_head,
602\t},
603\t.child_head\t= {
604\t\t.prev = &DM_DEVICE_REF(spl_test3)->sibling_node,
605\t\t.next = &DM_DEVICE_REF(i2c)->sibling_node,
606\t},
607\t.seq_ = 0,
608};
609
610/*
611 * Node /some-bus index 2
612 * driver denx_u_boot_test_bus parent root_driver
613*/
614
615#include <dm/test.h>
616struct dm_test_pdata __attribute__ ((section (".priv_data")))
617\t_denx_u_boot_test_bus_plat_some_bus = {
618\t.dtplat = {
619\t\t.ping_add\t\t= 0x4,
620\t\t.ping_expect\t\t= 0x4,
621\t\t.reg\t\t\t= {0x3, 0x1},
622\t},
623};
624#include <dm/test.h>
625u8 _denx_u_boot_test_bus_priv_some_bus[sizeof(struct dm_test_priv)]
626\t__attribute__ ((section (".priv_data")));
627#include <dm/test.h>
628u8 _denx_u_boot_test_bus_ucplat_some_bus[sizeof(struct dm_test_uclass_priv)]
629\t__attribute__ ((section (".priv_data")));
630#include <test.h>
631
632DM_DEVICE_INST(some_bus) = {
633\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_test_bus),
634\t.name\t\t= "denx_u_boot_test_bus",
635\t.plat_\t\t= &_denx_u_boot_test_bus_plat_some_bus,
636\t.uclass_plat_\t= _denx_u_boot_test_bus_ucplat_some_bus,
637\t.driver_data\t= DM_TEST_TYPE_FIRST,
638\t.priv_\t\t= _denx_u_boot_test_bus_priv_some_bus,
639\t.uclass\t\t= DM_UCLASS_REF(testbus),
640\t.uclass_node\t= {
641\t\t.prev = &DM_UCLASS_REF(testbus)->dev_head,
642\t\t.next = &DM_UCLASS_REF(testbus)->dev_head,
643\t},
644\t.child_head\t= {
645\t\t.prev = &DM_DEVICE_REF(test0)->sibling_node,
646\t\t.next = &DM_DEVICE_REF(test)->sibling_node,
647\t},
648\t.sibling_node\t= {
649\t\t.prev = &DM_DEVICE_REF(i2c)->sibling_node,
650\t\t.next = &DM_DEVICE_REF(spl_test)->sibling_node,
651\t},
652\t.seq_ = 2,
653};
654
655/*
656 * Node /spl-test index 3
657 * driver sandbox_spl_test parent root_driver
658*/
659static struct dtd_sandbox_spl_test dtv_spl_test = {
660\t.boolval\t\t= true,
661\t.intval\t\t\t= 0x1,
662};
663
664DM_DEVICE_INST(spl_test) = {
665\t.driver\t\t= DM_DRIVER_REF(sandbox_spl_test),
666\t.name\t\t= "sandbox_spl_test",
667\t.plat_\t\t= &dtv_spl_test,
668\t.uclass\t\t= DM_UCLASS_REF(misc),
669\t.uclass_node\t= {
670\t\t.prev = &DM_UCLASS_REF(misc)->dev_head,
671\t\t.next = &DM_DEVICE_REF(spl_test3)->uclass_node,
672\t},
673\t.child_head\t= {
674\t\t.prev = &DM_DEVICE_REF(spl_test)->child_head,
675\t\t.next = &DM_DEVICE_REF(spl_test)->child_head,
676\t},
677\t.sibling_node\t= {
678\t\t.prev = &DM_DEVICE_REF(some_bus)->sibling_node,
679\t\t.next = &DM_DEVICE_REF(spl_test3)->sibling_node,
680\t},
681\t.seq_ = 0,
682};
683
684/*
685 * Node /spl-test3 index 4
686 * driver sandbox_spl_test parent root_driver
687*/
688static struct dtd_sandbox_spl_test dtv_spl_test3 = {
689\t.longbytearray\t\t= {0x90a0b0c, 0xd0e0f10},
690\t.stringarray\t\t= "one",
691};
692
693DM_DEVICE_INST(spl_test3) = {
694\t.driver\t\t= DM_DRIVER_REF(sandbox_spl_test),
695\t.name\t\t= "sandbox_spl_test",
696\t.plat_\t\t= &dtv_spl_test3,
697\t.uclass\t\t= DM_UCLASS_REF(misc),
698\t.uclass_node\t= {
699\t\t.prev = &DM_DEVICE_REF(spl_test)->uclass_node,
700\t\t.next = &DM_UCLASS_REF(misc)->dev_head,
701\t},
702\t.child_head\t= {
703\t\t.prev = &DM_DEVICE_REF(spl_test3)->child_head,
704\t\t.next = &DM_DEVICE_REF(spl_test3)->child_head,
705\t},
706\t.sibling_node\t= {
707\t\t.prev = &DM_DEVICE_REF(spl_test)->sibling_node,
708\t\t.next = &DM_DEVICE_REF(root)->child_head,
709\t},
710\t.seq_ = 1,
711};
712
713/*
714 * Node /some-bus/test index 5
715 * driver denx_u_boot_fdt_test parent denx_u_boot_test_bus
716*/
717
718#include <dm/test.h>
719struct dm_test_pdata __attribute__ ((section (".priv_data")))
720\t_denx_u_boot_fdt_test_plat_test = {
721\t.dtplat = {
722\t\t.ping_add\t\t= 0x5,
723\t\t.ping_expect\t\t= 0x5,
724\t\t.reg\t\t\t= {0x5, 0x0},
725\t},
726};
727#include <dm/test.h>
728u8 _denx_u_boot_fdt_test_priv_test[sizeof(struct dm_test_priv)]
729\t__attribute__ ((section (".priv_data")));
730#include <dm/test.h>
731u8 _denx_u_boot_fdt_test_parent_plat_test[sizeof(struct dm_test_parent_plat)]
732\t__attribute__ ((section (".priv_data")));
733#include <dm/test.h>
734u8 _denx_u_boot_fdt_test_parent_priv_test[sizeof(struct dm_test_parent_data)]
735\t__attribute__ ((section (".priv_data")));
736
737DM_DEVICE_INST(test) = {
738\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_fdt_test),
739\t.name\t\t= "denx_u_boot_fdt_test",
740\t.plat_\t\t= &_denx_u_boot_fdt_test_plat_test,
741\t.parent_plat_\t= _denx_u_boot_fdt_test_parent_plat_test,
742\t.driver_data\t= DM_TEST_TYPE_FIRST,
743\t.parent\t\t= DM_DEVICE_REF(some_bus),
744\t.priv_\t\t= _denx_u_boot_fdt_test_priv_test,
745\t.uclass\t\t= DM_UCLASS_REF(testfdt),
746\t.parent_priv_\t= _denx_u_boot_fdt_test_parent_priv_test,
747\t.uclass_node\t= {
748\t\t.prev = &DM_UCLASS_REF(testfdt)->dev_head,
749\t\t.next = &DM_DEVICE_REF(test0)->uclass_node,
750\t},
751\t.child_head\t= {
752\t\t.prev = &DM_DEVICE_REF(test)->child_head,
753\t\t.next = &DM_DEVICE_REF(test)->child_head,
754\t},
755\t.sibling_node\t= {
756\t\t.prev = &DM_DEVICE_REF(some_bus)->child_head,
757\t\t.next = &DM_DEVICE_REF(test0)->sibling_node,
758\t},
759\t.seq_ = 1,
760};
761
762/*
763 * Node /some-bus/test0 index 6
764 * driver denx_u_boot_fdt_test parent denx_u_boot_test_bus
765*/
766
767#include <dm/test.h>
768struct dm_test_pdata __attribute__ ((section (".priv_data")))
769\t_denx_u_boot_fdt_test_plat_test0 = {
770\t.dtplat = {
771\t},
772};
773#include <dm/test.h>
774u8 _denx_u_boot_fdt_test_priv_test0[sizeof(struct dm_test_priv)]
775\t__attribute__ ((section (".priv_data")));
776#include <dm/test.h>
777u8 _denx_u_boot_fdt_test_parent_plat_test0[sizeof(struct dm_test_parent_plat)]
778\t__attribute__ ((section (".priv_data")));
779#include <dm/test.h>
780u8 _denx_u_boot_fdt_test_parent_priv_test0[sizeof(struct dm_test_parent_data)]
781\t__attribute__ ((section (".priv_data")));
782
783DM_DEVICE_INST(test0) = {
784\t.driver\t\t= DM_DRIVER_REF(denx_u_boot_fdt_test),
785\t.name\t\t= "denx_u_boot_fdt_test",
786\t.plat_\t\t= &_denx_u_boot_fdt_test_plat_test0,
787\t.parent_plat_\t= _denx_u_boot_fdt_test_parent_plat_test0,
788\t.driver_data\t= DM_TEST_TYPE_SECOND,
789\t.parent\t\t= DM_DEVICE_REF(some_bus),
790\t.priv_\t\t= _denx_u_boot_fdt_test_priv_test0,
791\t.uclass\t\t= DM_UCLASS_REF(testfdt),
792\t.parent_priv_\t= _denx_u_boot_fdt_test_parent_priv_test0,
793\t.uclass_node\t= {
794\t\t.prev = &DM_DEVICE_REF(test)->uclass_node,
795\t\t.next = &DM_UCLASS_REF(testfdt)->dev_head,
796\t},
797\t.child_head\t= {
798\t\t.prev = &DM_DEVICE_REF(test0)->child_head,
799\t\t.next = &DM_DEVICE_REF(test0)->child_head,
800\t},
801\t.sibling_node\t= {
802\t\t.prev = &DM_DEVICE_REF(test)->sibling_node,
803\t\t.next = &DM_DEVICE_REF(some_bus)->child_head,
804\t},
805\t.seq_ = 2,
806};
807
808'''
Simon Glass07ee48e2020-12-28 20:34:49 -0700809
810 def test_simple(self):
811 """Test output from some simple nodes with various types of data"""
812 dtb_file = get_dtb_file('dtoc_test_simple.dts')
813 output = tools.GetOutputFilename('output')
814 self.run_test(['struct'], dtb_file, output)
815 with open(output) as infile:
816 data = infile.read()
817
818 self._check_strings(self.struct_text, data)
819
820 self.run_test(['platdata'], dtb_file, output)
821 with open(output) as infile:
822 data = infile.read()
823
824 self._check_strings(self.platdata_text, data)
Simon Glass9d2eb922017-06-18 22:09:06 -0600825
Simon Glass3fa3bbb2021-02-03 06:01:14 -0700826 self.run_test(['decl'], dtb_file, output)
827 with open(output) as infile:
828 data = infile.read()
829
830 self._check_strings(self.decl_text, data)
831
Simon Glass4e8e8462020-12-28 20:34:52 -0700832 # Try the 'all' command
833 self.run_test(['all'], dtb_file, output)
834 data = tools.ReadFile(output, binary=False)
Simon Glassfea2f252021-02-03 06:01:21 -0700835 self._check_strings(
836 self.decl_text + self.device_text + self.platdata_text +
837 self.struct_text + self.uclass_text, data)
Simon Glass4e8e8462020-12-28 20:34:52 -0700838
Walter Lozanoe675d962020-07-03 08:07:17 -0300839 def test_driver_alias(self):
840 """Test output from a device tree file with a driver alias"""
841 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
842 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300843 self.run_test(['struct'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300844 with open(output) as infile:
845 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700846 self._check_strings(HEADER + '''
Walter Lozanoe675d962020-07-03 08:07:17 -0300847struct dtd_sandbox_gpio {
848\tconst char *\tgpio_bank_name;
849\tbool\t\tgpio_controller;
850\tfdt32_t\t\tsandbox_gpio_count;
851};
Walter Lozanoe675d962020-07-03 08:07:17 -0300852''', data)
853
Walter Lozanoa324e412020-06-25 01:10:08 -0300854 self.run_test(['platdata'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300855 with open(output) as infile:
856 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700857 self._check_strings(C_HEADER + '''
Simon Glassbe749002021-02-03 06:01:15 -0700858/*
Simon Glasse2119082021-02-03 06:01:19 -0700859 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
860 *
861 * idx driver_info driver
862 * --- -------------------- --------------------
863 * 0: gpios_at_0 sandbox_gpio
864 * --- -------------------- --------------------
865 */
866
867/*
Simon Glassbe749002021-02-03 06:01:15 -0700868 * Node /gpios@0 index 0
869 * driver sandbox_gpio parent None
870 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300871static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300872\t.gpio_bank_name\t\t= "a",
873\t.gpio_controller\t= true,
874\t.sandbox_gpio_count\t= 0x14,
875};
Simon Glass1d8364a2020-12-28 20:34:54 -0700876U_BOOT_DRVINFO(gpios_at_0) = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300877\t.name\t\t= "sandbox_gpio",
Simon Glasse2119082021-02-03 06:01:19 -0700878\t.plat\t\t= &dtv_gpios_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700879\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600880\t.parent_idx\t= -1,
Walter Lozanoe675d962020-07-03 08:07:17 -0300881};
882
883''', data)
884
Walter Lozanoa324e412020-06-25 01:10:08 -0300885 def test_invalid_driver(self):
886 """Test output from a device tree file with an invalid driver"""
887 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
888 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700889 with test_util.capture_sys_output() as _:
Simon Glass3809ad92021-02-03 06:01:12 -0700890 dtb_platdata.run_steps(
891 ['struct'], dtb_file, False, output, [], None, False,
892 scan=copy_scan())
Walter Lozanoa324e412020-06-25 01:10:08 -0300893 with open(output) as infile:
894 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700895 self._check_strings(HEADER + '''
Walter Lozanoa324e412020-06-25 01:10:08 -0300896struct dtd_invalid {
897};
898''', data)
899
Simon Glassa60cabd2020-12-28 20:34:47 -0700900 with test_util.capture_sys_output() as _:
Simon Glass3809ad92021-02-03 06:01:12 -0700901 dtb_platdata.run_steps(
902 ['platdata'], dtb_file, False, output, [], None, False,
903 scan=copy_scan())
Walter Lozanoa324e412020-06-25 01:10:08 -0300904 with open(output) as infile:
905 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700906 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -0700907/*
908 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
909 *
910 * idx driver_info driver
911 * --- -------------------- --------------------
912 * 0: spl_test invalid
913 * --- -------------------- --------------------
914 */
915
Simon Glass192f8132020-10-03 11:31:25 -0600916/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300917static struct dtd_invalid dtv_spl_test = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300918};
Simon Glass1d8364a2020-12-28 20:34:54 -0700919U_BOOT_DRVINFO(spl_test) = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300920\t.name\t\t= "invalid",
Simon Glasse2119082021-02-03 06:01:19 -0700921\t.plat\t\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700922\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600923\t.parent_idx\t= -1,
Walter Lozanoa324e412020-06-25 01:10:08 -0300924};
925
926''', data)
927
Simon Glass9d2eb922017-06-18 22:09:06 -0600928 def test_phandle(self):
929 """Test output from a node containing a phandle reference"""
930 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
931 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300932 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600933 with open(output) as infile:
934 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700935 self._check_strings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600936struct dtd_source {
Simon Glass3deeb472017-08-29 14:15:59 -0600937\tstruct phandle_2_arg clocks[4];
Simon Glass9d2eb922017-06-18 22:09:06 -0600938};
939struct dtd_target {
940\tfdt32_t\t\tintval;
941};
942''', data)
943
Walter Lozanoa324e412020-06-25 01:10:08 -0300944 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600945 with open(output) as infile:
946 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700947 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -0700948/*
949 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
950 *
951 * idx driver_info driver
952 * --- -------------------- --------------------
953 * 0: phandle2_target target
954 * 1: phandle3_target target
955 * 2: phandle_source source
956 * 3: phandle_source2 source
957 * 4: phandle_target target
958 * --- -------------------- --------------------
959 */
960
Simon Glass192f8132020-10-03 11:31:25 -0600961/* Node /phandle2-target index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300962static struct dtd_target dtv_phandle2_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600963\t.intval\t\t\t= 0x1,
964};
Simon Glass1d8364a2020-12-28 20:34:54 -0700965U_BOOT_DRVINFO(phandle2_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600966\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -0700967\t.plat\t\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700968\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600969\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600970};
971
Simon Glass192f8132020-10-03 11:31:25 -0600972/* Node /phandle3-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300973static struct dtd_target dtv_phandle3_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600974\t.intval\t\t\t= 0x2,
975};
Simon Glass1d8364a2020-12-28 20:34:54 -0700976U_BOOT_DRVINFO(phandle3_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600977\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -0700978\t.plat\t\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700979\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600980\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600981};
982
Simon Glass192f8132020-10-03 11:31:25 -0600983/* Node /phandle-source index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300984static struct dtd_source dtv_phandle_source = {
Simon Glassd0cd0752017-08-29 14:15:57 -0600985\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600986\t\t\t{4, {}},
987\t\t\t{0, {11}},
988\t\t\t{1, {12, 13}},
989\t\t\t{4, {}},},
Simon Glass9d2eb922017-06-18 22:09:06 -0600990};
Simon Glass1d8364a2020-12-28 20:34:54 -0700991U_BOOT_DRVINFO(phandle_source) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600992\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -0700993\t.plat\t\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700994\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600995\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600996};
997
Simon Glass192f8132020-10-03 11:31:25 -0600998/* Node /phandle-source2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300999static struct dtd_source dtv_phandle_source2 = {
Simon Glass609e2b12018-07-06 10:27:31 -06001000\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -06001001\t\t\t{4, {}},},
Simon Glass609e2b12018-07-06 10:27:31 -06001002};
Simon Glass1d8364a2020-12-28 20:34:54 -07001003U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass609e2b12018-07-06 10:27:31 -06001004\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -07001005\t.plat\t\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -07001006\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -06001007\t.parent_idx\t= -1,
Simon Glass609e2b12018-07-06 10:27:31 -06001008};
1009
Simon Glass16382ce2020-12-28 20:35:04 -07001010/* Node /phandle-target index 4 */
1011static struct dtd_target dtv_phandle_target = {
1012\t.intval\t\t\t= 0x0,
1013};
1014U_BOOT_DRVINFO(phandle_target) = {
1015\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001016\t.plat\t\t= &dtv_phandle_target,
Simon Glass16382ce2020-12-28 20:35:04 -07001017\t.plat_size\t= sizeof(dtv_phandle_target),
1018\t.parent_idx\t= -1,
1019};
1020
Simon Glass9d2eb922017-06-18 22:09:06 -06001021''', data)
1022
Simon Glass961c1ce2018-07-06 10:27:35 -06001023 def test_phandle_single(self):
1024 """Test output from a node containing a phandle reference"""
1025 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
1026 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001027 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001028 with open(output) as infile:
1029 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001030 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -06001031struct dtd_source {
1032\tstruct phandle_0_arg clocks[1];
1033};
1034struct dtd_target {
1035\tfdt32_t\t\tintval;
1036};
1037''', data)
1038
1039 def test_phandle_reorder(self):
1040 """Test that phandle targets are generated before their references"""
1041 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
1042 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001043 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001044 with open(output) as infile:
1045 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001046 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001047/*
1048 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1049 *
1050 * idx driver_info driver
1051 * --- -------------------- --------------------
1052 * 0: phandle_source2 source
1053 * 1: phandle_target target
1054 * --- -------------------- --------------------
1055 */
1056
Simon Glass192f8132020-10-03 11:31:25 -06001057/* Node /phandle-source2 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001058static struct dtd_source dtv_phandle_source2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001059\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -06001060\t\t\t{1, {}},},
Simon Glass961c1ce2018-07-06 10:27:35 -06001061};
Simon Glass1d8364a2020-12-28 20:34:54 -07001062U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001063\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -07001064\t.plat\t\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -07001065\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -06001066\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -06001067};
1068
Simon Glass16382ce2020-12-28 20:35:04 -07001069/* Node /phandle-target index 1 */
1070static struct dtd_target dtv_phandle_target = {
1071};
1072U_BOOT_DRVINFO(phandle_target) = {
1073\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001074\t.plat\t\t= &dtv_phandle_target,
Simon Glass16382ce2020-12-28 20:35:04 -07001075\t.plat_size\t= sizeof(dtv_phandle_target),
1076\t.parent_idx\t= -1,
1077};
1078
Simon Glass961c1ce2018-07-06 10:27:35 -06001079''', data)
1080
Walter Lozano5541fc02020-06-25 01:10:17 -03001081 def test_phandle_cd_gpio(self):
1082 """Test that phandle targets are generated when unsing cd-gpios"""
1083 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
1084 output = tools.GetOutputFilename('output')
Simon Glass3809ad92021-02-03 06:01:12 -07001085 dtb_platdata.run_steps(
1086 ['platdata'], dtb_file, False, output, [], None, False,
1087 warning_disabled=True, scan=copy_scan())
Walter Lozano5541fc02020-06-25 01:10:17 -03001088 with open(output) as infile:
1089 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001090 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001091/*
1092 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1093 *
1094 * idx driver_info driver
1095 * --- -------------------- --------------------
1096 * 0: phandle2_target target
1097 * 1: phandle3_target target
1098 * 2: phandle_source source
1099 * 3: phandle_source2 source
1100 * 4: phandle_target target
1101 * --- -------------------- --------------------
1102 */
1103
Simon Glass192f8132020-10-03 11:31:25 -06001104/* Node /phandle2-target index 0 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001105static struct dtd_target dtv_phandle2_target = {
1106\t.intval\t\t\t= 0x1,
1107};
Simon Glass1d8364a2020-12-28 20:34:54 -07001108U_BOOT_DRVINFO(phandle2_target) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001109\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001110\t.plat\t\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -07001111\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -06001112\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -03001113};
1114
Simon Glass192f8132020-10-03 11:31:25 -06001115/* Node /phandle3-target index 1 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001116static struct dtd_target dtv_phandle3_target = {
1117\t.intval\t\t\t= 0x2,
1118};
Simon Glass1d8364a2020-12-28 20:34:54 -07001119U_BOOT_DRVINFO(phandle3_target) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001120\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001121\t.plat\t\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -07001122\t.plat_size\t= sizeof(dtv_phandle3_target),
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-source index 2 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001127static struct dtd_source dtv_phandle_source = {
1128\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -06001129\t\t\t{4, {}},
1130\t\t\t{0, {11}},
1131\t\t\t{1, {12, 13}},
1132\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -03001133};
Simon Glass1d8364a2020-12-28 20:34:54 -07001134U_BOOT_DRVINFO(phandle_source) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001135\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -07001136\t.plat\t\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -07001137\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -06001138\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -03001139};
1140
Simon Glass192f8132020-10-03 11:31:25 -06001141/* Node /phandle-source2 index 3 */
Walter Lozano5541fc02020-06-25 01:10:17 -03001142static struct dtd_source dtv_phandle_source2 = {
1143\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -06001144\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -03001145};
Simon Glass1d8364a2020-12-28 20:34:54 -07001146U_BOOT_DRVINFO(phandle_source2) = {
Walter Lozano5541fc02020-06-25 01:10:17 -03001147\t.name\t\t= "source",
Simon Glasse2119082021-02-03 06:01:19 -07001148\t.plat\t\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -07001149\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -06001150\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -03001151};
1152
Simon Glass16382ce2020-12-28 20:35:04 -07001153/* Node /phandle-target index 4 */
1154static struct dtd_target dtv_phandle_target = {
1155\t.intval\t\t\t= 0x0,
1156};
1157U_BOOT_DRVINFO(phandle_target) = {
1158\t.name\t\t= "target",
Simon Glasse2119082021-02-03 06:01:19 -07001159\t.plat\t\t= &dtv_phandle_target,
Simon Glass16382ce2020-12-28 20:35:04 -07001160\t.plat_size\t= sizeof(dtv_phandle_target),
1161\t.parent_idx\t= -1,
1162};
1163
Walter Lozano5541fc02020-06-25 01:10:17 -03001164''', data)
1165
Simon Glass961c1ce2018-07-06 10:27:35 -06001166 def test_phandle_bad(self):
1167 """Test a node containing an invalid phandle fails"""
Simon Glass04150022018-10-01 21:12:43 -06001168 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
1169 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -06001170 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001171 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001172 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001173 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
Simon Glassa60cabd2020-12-28 20:34:47 -07001174 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001175
1176 def test_phandle_bad2(self):
1177 """Test a phandle target missing its #*-cells property"""
Simon Glass04150022018-10-01 21:12:43 -06001178 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
1179 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -06001180 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001181 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001182 self.run_test(['struct'], dtb_file, output)
Walter Lozano179f0b62020-06-25 01:10:16 -03001183 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glassa60cabd2020-12-28 20:34:47 -07001184 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001185
Simon Glass1b1fe412017-08-29 14:15:50 -06001186 def test_addresses64(self):
1187 """Test output from a node with a 'reg' property with na=2, ns=2"""
1188 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
1189 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001190 self.run_test(['struct'], 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(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001194struct dtd_test1 {
1195\tfdt64_t\t\treg[2];
1196};
1197struct dtd_test2 {
1198\tfdt64_t\t\treg[2];
1199};
1200struct dtd_test3 {
1201\tfdt64_t\t\treg[4];
1202};
1203''', data)
1204
Walter Lozanoa324e412020-06-25 01:10:08 -03001205 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001206 with open(output) as infile:
1207 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001208 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001209/*
1210 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1211 *
1212 * idx driver_info driver
1213 * --- -------------------- --------------------
1214 * 0: test1 test1
1215 * 1: test2 test2
1216 * 2: test3 test3
1217 * --- -------------------- --------------------
1218 */
1219
Simon Glass192f8132020-10-03 11:31:25 -06001220/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001221static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001222\t.reg\t\t\t= {0x1234, 0x5678},
1223};
Simon Glass1d8364a2020-12-28 20:34:54 -07001224U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001225\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001226\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001227\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001228\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001229};
1230
Simon Glass192f8132020-10-03 11:31:25 -06001231/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001232static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001233\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
1234};
Simon Glass1d8364a2020-12-28 20:34:54 -07001235U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001236\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001237\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001238\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001239\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001240};
1241
Simon Glass192f8132020-10-03 11:31:25 -06001242/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001243static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001244\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
1245};
Simon Glass1d8364a2020-12-28 20:34:54 -07001246U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001247\t.name\t\t= "test3",
Simon Glasse2119082021-02-03 06:01:19 -07001248\t.plat\t\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -07001249\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -06001250\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001251};
1252
Simon Glass2500de22020-12-28 20:35:05 -07001253''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -06001254
1255 def test_addresses32(self):
1256 """Test output from a node with a 'reg' property with na=1, ns=1"""
1257 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
1258 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001259 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001260 with open(output) as infile:
1261 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001262 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001263struct dtd_test1 {
1264\tfdt32_t\t\treg[2];
1265};
1266struct dtd_test2 {
1267\tfdt32_t\t\treg[4];
1268};
1269''', data)
1270
Walter Lozanoa324e412020-06-25 01:10:08 -03001271 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001272 with open(output) as infile:
1273 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001274 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001275/*
1276 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1277 *
1278 * idx driver_info driver
1279 * --- -------------------- --------------------
1280 * 0: test1 test1
1281 * 1: test2 test2
1282 * --- -------------------- --------------------
1283 */
1284
Simon Glass192f8132020-10-03 11:31:25 -06001285/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001286static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001287\t.reg\t\t\t= {0x1234, 0x5678},
1288};
Simon Glass1d8364a2020-12-28 20:34:54 -07001289U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001290\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001291\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001292\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001293\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001294};
1295
Simon Glass192f8132020-10-03 11:31:25 -06001296/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001297static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001298\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
1299};
Simon Glass1d8364a2020-12-28 20:34:54 -07001300U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001301\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001302\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001303\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001304\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001305};
1306
Simon Glass2500de22020-12-28 20:35:05 -07001307''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -06001308
1309 def test_addresses64_32(self):
1310 """Test output from a node with a 'reg' property with na=2, ns=1"""
1311 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
1312 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001313 self.run_test(['struct'], 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(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001317struct dtd_test1 {
1318\tfdt64_t\t\treg[2];
1319};
1320struct dtd_test2 {
1321\tfdt64_t\t\treg[2];
1322};
1323struct dtd_test3 {
1324\tfdt64_t\t\treg[4];
1325};
1326''', data)
1327
Walter Lozanoa324e412020-06-25 01:10:08 -03001328 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001329 with open(output) as infile:
1330 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001331 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001332/*
1333 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1334 *
1335 * idx driver_info driver
1336 * --- -------------------- --------------------
1337 * 0: test1 test1
1338 * 1: test2 test2
1339 * 2: test3 test3
1340 * --- -------------------- --------------------
1341 */
1342
Simon Glass192f8132020-10-03 11:31:25 -06001343/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001344static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001345\t.reg\t\t\t= {0x123400000000, 0x5678},
1346};
Simon Glass1d8364a2020-12-28 20:34:54 -07001347U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001348\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001349\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001350\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001351\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001352};
1353
Simon Glass192f8132020-10-03 11:31:25 -06001354/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001355static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001356\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
1357};
Simon Glass1d8364a2020-12-28 20:34:54 -07001358U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001359\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001360\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001361\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001362\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001363};
1364
Simon Glass192f8132020-10-03 11:31:25 -06001365/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001366static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001367\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
1368};
Simon Glass1d8364a2020-12-28 20:34:54 -07001369U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001370\t.name\t\t= "test3",
Simon Glasse2119082021-02-03 06:01:19 -07001371\t.plat\t\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -07001372\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -06001373\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001374};
1375
Simon Glass2500de22020-12-28 20:35:05 -07001376''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -06001377
1378 def test_addresses32_64(self):
1379 """Test output from a node with a 'reg' property with na=1, ns=2"""
1380 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
1381 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001382 self.run_test(['struct'], 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(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -06001386struct dtd_test1 {
1387\tfdt64_t\t\treg[2];
1388};
1389struct dtd_test2 {
1390\tfdt64_t\t\treg[2];
1391};
1392struct dtd_test3 {
1393\tfdt64_t\t\treg[4];
1394};
1395''', data)
1396
Walter Lozanoa324e412020-06-25 01:10:08 -03001397 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -06001398 with open(output) as infile:
1399 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001400 self._check_strings(C_HEADER + '''
Simon Glasse2119082021-02-03 06:01:19 -07001401/*
1402 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1403 *
1404 * idx driver_info driver
1405 * --- -------------------- --------------------
1406 * 0: test1 test1
1407 * 1: test2 test2
1408 * 2: test3 test3
1409 * --- -------------------- --------------------
1410 */
1411
Simon Glass192f8132020-10-03 11:31:25 -06001412/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001413static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001414\t.reg\t\t\t= {0x1234, 0x567800000000},
1415};
Simon Glass1d8364a2020-12-28 20:34:54 -07001416U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001417\t.name\t\t= "test1",
Simon Glasse2119082021-02-03 06:01:19 -07001418\t.plat\t\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -07001419\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -06001420\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001421};
1422
Simon Glass192f8132020-10-03 11:31:25 -06001423/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001424static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001425\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
1426};
Simon Glass1d8364a2020-12-28 20:34:54 -07001427U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001428\t.name\t\t= "test2",
Simon Glasse2119082021-02-03 06:01:19 -07001429\t.plat\t\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001430\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001431\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001432};
1433
Simon Glass192f8132020-10-03 11:31:25 -06001434/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001435static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001436\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
1437};
Simon Glass1d8364a2020-12-28 20:34:54 -07001438U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -06001439\t.name\t\t= "test3",
Simon Glasse2119082021-02-03 06:01:19 -07001440\t.plat\t\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -07001441\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -06001442\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -06001443};
1444
Simon Glass2500de22020-12-28 20:35:05 -07001445''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -06001446
1447 def test_bad_reg(self):
1448 """Test that a reg property with an invalid type generates an error"""
Simon Glass3bce93d2018-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_reg.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -06001451 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001452 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001453 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001454 self.assertIn("Node 'spl-test' reg property is not an int",
Simon Glassa60cabd2020-12-28 20:34:47 -07001455 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001456
1457 def test_bad_reg2(self):
1458 """Test that a reg property with an invalid cell count is detected"""
Simon Glass3bce93d2018-07-06 10:27:37 -06001459 # Capture stderr since dtc will emit warnings for this file
1460 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -06001461 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001462 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001463 self.run_test(['struct'], dtb_file, output)
Simon Glassa60cabd2020-12-28 20:34:47 -07001464 self.assertIn(
1465 "Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
1466 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001467
1468 def test_add_prop(self):
1469 """Test that a subequent node can add a new property to a struct"""
1470 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
1471 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -03001472 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001473 with open(output) as infile:
1474 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001475 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -06001476struct dtd_sandbox_spl_test {
1477\tfdt32_t\t\tintarray;
1478\tfdt32_t\t\tintval;
1479};
1480''', data)
1481
Walter Lozanoa324e412020-06-25 01:10:08 -03001482 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -06001483 with open(output) as infile:
1484 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -07001485 self._check_strings(C_HEADER + '''
Simon Glassbe749002021-02-03 06:01:15 -07001486/*
Simon Glasse2119082021-02-03 06:01:19 -07001487 * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
1488 *
1489 * idx driver_info driver
1490 * --- -------------------- --------------------
1491 * 0: spl_test sandbox_spl_test
1492 * 1: spl_test2 sandbox_spl_test
1493 * --- -------------------- --------------------
1494 */
1495
1496/*
Simon Glassbe749002021-02-03 06:01:15 -07001497 * Node /spl-test index 0
1498 * driver sandbox_spl_test parent None
1499 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001500static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001501\t.intval\t\t\t= 0x1,
1502};
Simon Glass1d8364a2020-12-28 20:34:54 -07001503U_BOOT_DRVINFO(spl_test) = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001504\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -07001505\t.plat\t\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -07001506\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -06001507\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -06001508};
1509
Simon Glassbe749002021-02-03 06:01:15 -07001510/*
1511 * Node /spl-test2 index 1
1512 * driver sandbox_spl_test parent None
1513 */
Walter Lozanodc5b4372020-06-25 01:10:13 -03001514static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001515\t.intarray\t\t= 0x5,
1516};
Simon Glass1d8364a2020-12-28 20:34:54 -07001517U_BOOT_DRVINFO(spl_test2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -06001518\t.name\t\t= "sandbox_spl_test",
Simon Glasse2119082021-02-03 06:01:19 -07001519\t.plat\t\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -07001520\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -06001521\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -06001522};
1523
Simon Glass2500de22020-12-28 20:35:05 -07001524''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -06001525
Simon Glassa60cabd2020-12-28 20:34:47 -07001526 def test_stdout(self):
Simon Glass961c1ce2018-07-06 10:27:35 -06001527 """Test output to stdout"""
1528 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass07ee48e2020-12-28 20:34:49 -07001529 with test_util.capture_sys_output() as (stdout, _):
Simon Glass6ca0c7a2020-12-28 20:34:48 -07001530 self.run_test(['struct'], dtb_file, None)
Simon Glass07ee48e2020-12-28 20:34:49 -07001531 self._check_strings(self.struct_text, stdout.getvalue())
Simon Glass961c1ce2018-07-06 10:27:35 -06001532
Simon Glassc3a310a82020-12-28 20:34:51 -07001533 def test_multi_to_file(self):
1534 """Test output of multiple pieces to a single file"""
1535 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1536 output = tools.GetOutputFilename('output')
Simon Glass4e8e8462020-12-28 20:34:52 -07001537 self.run_test(['all'], dtb_file, output)
Simon Glassc3a310a82020-12-28 20:34:51 -07001538 data = tools.ReadFile(output, binary=False)
Simon Glassfea2f252021-02-03 06:01:21 -07001539 self._check_strings(
1540 self.decl_text + self.device_text + self.platdata_text +
1541 self.struct_text + self.uclass_text, data)
Simon Glassc3a310a82020-12-28 20:34:51 -07001542
Simon Glassa60cabd2020-12-28 20:34:47 -07001543 def test_no_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -06001544 """Test running dtoc without a command"""
Simon Glassa60cabd2020-12-28 20:34:47 -07001545 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -03001546 self.run_test([], '', '')
Simon Glass961c1ce2018-07-06 10:27:35 -06001547 self.assertIn("Please specify a command: struct, platdata",
Simon Glassa60cabd2020-12-28 20:34:47 -07001548 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -06001549
Simon Glassa60cabd2020-12-28 20:34:47 -07001550 def test_bad_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -06001551 """Test running dtoc with an invalid command"""
1552 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1553 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -07001554 with self.assertRaises(ValueError) as exc:
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001555 self.run_test(['invalid-cmd'], dtb_file, output)
1556 self.assertIn(
Simon Glassdb2b9ca2021-03-25 06:40:51 +13001557 "Unknown command 'invalid-cmd': (use: decl, platdata, struct)",
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001558 str(exc.exception))
Walter Lozanod82062b2020-07-28 19:06:23 -03001559
Simon Glass4e8e8462020-12-28 20:34:52 -07001560 def test_output_conflict(self):
1561 """Test a conflict between and output dirs and output file"""
1562 with self.assertRaises(ValueError) as exc:
Simon Glass3809ad92021-02-03 06:01:12 -07001563 dtb_platdata.run_steps(
1564 ['all'], None, False, 'out', ['cdir'], None, False,
1565 warning_disabled=True, scan=copy_scan())
Simon Glass4e8e8462020-12-28 20:34:52 -07001566 self.assertIn("Must specify either output or output_dirs, not both",
1567 str(exc.exception))
1568
1569 def test_output_dirs(self):
1570 """Test outputting files to a directory"""
1571 # Remove the directory so that files from other tests are not there
1572 tools._RemoveOutputDir()
1573 tools.PrepareOutputDir(None)
1574
1575 # This should create the .dts and .dtb in the output directory
1576 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1577 outdir = tools.GetOutputDir()
1578 fnames = glob.glob(outdir + '/*')
1579 self.assertEqual(2, len(fnames))
1580
Simon Glass3809ad92021-02-03 06:01:12 -07001581 dtb_platdata.run_steps(
1582 ['all'], dtb_file, False, None, [outdir], None, False,
1583 warning_disabled=True, scan=copy_scan())
Simon Glass4e8e8462020-12-28 20:34:52 -07001584 fnames = glob.glob(outdir + '/*')
Simon Glassfea2f252021-02-03 06:01:21 -07001585 self.assertEqual(7, len(fnames))
Simon Glass4e8e8462020-12-28 20:34:52 -07001586
1587 leafs = set(os.path.basename(fname) for fname in fnames)
1588 self.assertEqual(
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001589 {'dt-structs-gen.h', 'source.dts', 'dt-plat.c', 'source.dtb',
Simon Glassfea2f252021-02-03 06:01:21 -07001590 'dt-uclass.c', 'dt-decl.h', 'dt-device.c'},
Simon Glass4e8e8462020-12-28 20:34:52 -07001591 leafs)
Simon Glass047a4802021-02-03 06:01:00 -07001592
1593 def setup_process_test(self):
1594 """Set up a test of process_nodes()
1595
1596 This uses saved_scan but returns a deep copy of it, so it is safe to
1597 modify it in these tests
1598
1599 Returns:
1600 tuple:
1601 DtbPlatdata: object to test
1602 Scanner: scanner to use
1603 """
1604 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1605 output = tools.GetOutputFilename('output')
1606
1607 # Take a copy before messing with it
Simon Glassc14fd0c2021-02-03 06:01:11 -07001608 scan = copy_scan()
Simon Glass047a4802021-02-03 06:01:00 -07001609 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
1610 plat.scan_dtb()
Simon Glassc14fd0c2021-02-03 06:01:11 -07001611 plat.scan_tree(False)
Simon Glass047a4802021-02-03 06:01:00 -07001612 plat.prepare_nodes()
1613 return plat, scan
1614
1615 def test_process_nodes(self):
1616 """Test processing nodes to add various info"""
1617 plat, scan = self.setup_process_test()
1618 plat.process_nodes(True)
1619
1620 i2c_node = plat._fdt.GetNode('/i2c@0')
1621 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
1622 pmic = scan._drivers['sandbox_pmic']
1623 i2c = scan._drivers['sandbox_i2c']
1624 self.assertEqual('DM_DEVICE_REF(pmic_at_9)', pmic_node.dev_ref)
1625 self.assertEqual(pmic, pmic_node.driver)
1626 self.assertEqual(i2c_node, pmic_node.parent)
1627 self.assertEqual(i2c, pmic_node.parent_driver)
1628
1629 # The pmic is the only child
1630 self.assertEqual(pmic_node.parent_seq, 0)
1631 self.assertEqual([pmic_node], i2c_node.child_devs)
1632
1633 # Start and end of the list should be the child_head
1634 ref = '&DM_DEVICE_REF(i2c_at_0)->child_head'
1635 self.assertEqual(
1636 {-1: ref, 0: '&DM_DEVICE_REF(pmic_at_9)->sibling_node', 1: ref},
1637 i2c_node.child_refs)
1638
1639 def test_process_nodes_bad_parent(self):
1640 # Pretend that i2c has a parent (the pmic) and delete that driver
1641 plat, scan = self.setup_process_test()
1642
1643 i2c_node = plat._fdt.GetNode('/i2c@0')
1644 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
1645 del scan._drivers['sandbox_pmic']
1646 i2c_node.parent = pmic_node
1647
1648 # Process twice, the second time to generate an exception
1649 plat.process_nodes(False)
1650 with self.assertRaises(ValueError) as exc:
1651 plat.process_nodes(True)
1652 self.assertIn(
1653 "Cannot parse/find parent driver 'sandbox_pmic' for 'sandbox_i2c",
1654 str(exc.exception))
1655
1656 def test_process_nodes_bad_node(self):
1657 plat, scan = self.setup_process_test()
1658
1659 # Now remove the pmic driver
1660 del scan._drivers['sandbox_pmic']
1661
1662 # Process twice, the second time to generate an exception
1663 plat.process_nodes(False)
1664 with self.assertRaises(ValueError) as exc:
1665 plat.process_nodes(True)
1666 self.assertIn("Cannot parse/find driver for 'sandbox_pmic",
1667 str(exc.exception))
Simon Glasseb3c2492021-02-03 06:01:01 -07001668
Simon Glass80d782c42021-02-03 06:01:10 -07001669 def test_process_nodes_bad_uclass(self):
1670 plat, scan = self.setup_process_test()
1671
1672 self.assertIn('UCLASS_I2C', scan._uclass)
1673 del scan._uclass['UCLASS_I2C']
1674 with self.assertRaises(ValueError) as exc:
1675 plat.process_nodes(True)
1676 self.assertIn("Cannot parse/find uclass 'UCLASS_I2C' for driver 'sandbox_i2c'",
1677 str(exc.exception))
1678
Simon Glasseb3c2492021-02-03 06:01:01 -07001679 def test_process_nodes_used(self):
1680 """Test processing nodes to add various info"""
1681 plat, scan = self.setup_process_test()
1682 plat.process_nodes(True)
1683
1684 pmic = scan._drivers['sandbox_pmic']
1685 self.assertTrue(pmic.used)
1686
1687 gpio = scan._drivers['sandbox_gpio']
1688 self.assertFalse(gpio.used)
Simon Glassbe88d2f2021-02-03 06:01:07 -07001689
1690 def test_alias_read(self):
1691 """Test obtaining aliases"""
1692 dtb_file = get_dtb_file('dtoc_test_inst.dts')
1693 output = tools.GetOutputFilename('output')
1694 plat = self.run_test(['struct'], dtb_file, output)
1695
1696 scan = plat._scan
1697 testfdt_node = plat._fdt.GetNode('/some-bus/test')
Simon Glass80d782c42021-02-03 06:01:10 -07001698 test0_node = plat._fdt.GetNode('/some-bus/test0')
Simon Glassbe88d2f2021-02-03 06:01:07 -07001699 self.assertIn('UCLASS_TEST_FDT', scan._uclass)
1700 uc = scan._uclass['UCLASS_TEST_FDT']
Simon Glass80d782c42021-02-03 06:01:10 -07001701 self.assertEqual({1: testfdt_node, 2: test0_node},
1702 uc.alias_num_to_node)
1703 self.assertEqual({'/some-bus/test': 1, '/some-bus/test0': 2},
1704 uc.alias_path_to_num)
Simon Glassbe88d2f2021-02-03 06:01:07 -07001705
1706 # Try adding an alias that doesn't exist
1707 self.assertFalse(scan.add_uclass_alias('fred', 3, None))
1708
1709 # Try adding an alias for a missing node
1710 self.assertIsNone(scan.add_uclass_alias('testfdt', 3, None))
1711
1712 def test_alias_read_bad(self):
1713 """Test invalid alias property name"""
1714 dtb_file = get_dtb_file('dtoc_test_alias_bad.dts')
1715 output = tools.GetOutputFilename('output')
1716 with self.assertRaises(ValueError) as exc:
1717 plat = self.run_test(['struct'], dtb_file, output)
1718 self.assertIn("Cannot decode alias 'i2c4-'", str(exc.exception))
1719
1720 def test_alias_read_bad_path(self):
1721 """Test alias pointing to a non-existent node"""
1722 # This line may produce a warning, so capture it:
1723 # Warning (alias_paths): /aliases:i2c4: aliases property is not a valid
1724 # node (/does/not/exist)
1725 dtb_file = get_dtb_file('dtoc_test_alias_bad_path.dts', True)
1726
1727 output = tools.GetOutputFilename('output')
1728 with self.assertRaises(ValueError) as exc:
1729 plat = self.run_test(['struct'], dtb_file, output)
1730 self.assertIn("Alias 'i2c4' path '/does/not/exist' not found",
1731 str(exc.exception))
1732
1733 def test_alias_read_bad_uclass(self):
1734 """Test alias for a uclass that doesn't exist"""
1735 dtb_file = get_dtb_file('dtoc_test_alias_bad_uc.dts')
1736 output = tools.GetOutputFilename('output')
1737 with test_util.capture_sys_output() as (stdout, _):
1738 plat = self.run_test(['struct'], dtb_file, output)
1739 self.assertEqual("Could not find uclass for alias 'other1'",
1740 stdout.getvalue().strip())
Simon Glassdf56e0b2021-02-03 06:01:09 -07001741
1742 def test_sequence(self):
1743 """Test assignment of sequence numnbers"""
1744 dtb_file = get_dtb_file('dtoc_test_inst.dts')
1745 output = tools.GetOutputFilename('output')
1746 plat = self.run_test(['struct'], dtb_file, output)
Simon Glass80d782c42021-02-03 06:01:10 -07001747
1748 scan = plat._scan
1749 testfdt = plat._fdt.GetNode('/some-bus/test')
1750 self.assertEqual(1, testfdt.seq)
1751 i2c = plat._fdt.GetNode('/i2c')
1752
1753 # For now this uclass is not compiled in, so no sequence is assigned
1754 self.assertEqual(4, i2c.seq)
1755 spl = plat._fdt.GetNode('/spl-test')
1756 self.assertEqual(0, spl.seq)
Simon Glassc14fd0c2021-02-03 06:01:11 -07001757
1758 def test_process_root(self):
1759 """Test assignment of sequence numnbers"""
1760 dtb_file = get_dtb_file('dtoc_test_simple.dts')
1761 output = tools.GetOutputFilename('output')
1762
1763 # Take a copy before messing with it
1764 scan = copy_scan()
1765 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
1766 plat.scan_dtb()
1767 root = plat._fdt.GetRoot()
1768
1769 plat.scan_tree(False)
1770 self.assertNotIn(root, plat._valid_nodes)
1771
1772 plat.scan_tree(True)
1773 self.assertIn(root, plat._valid_nodes)
1774 self.assertEqual('root_driver',
1775 scan.get_normalized_compat_name(root)[0])
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001776
1777 def test_simple_inst(self):
1778 """Test output from some simple nodes with instantiate enabled"""
Simon Glassc7b4b832021-02-03 06:01:20 -07001779 dtb_file = get_dtb_file('dtoc_test_inst.dts')
Simon Glass3fa3bbb2021-02-03 06:01:14 -07001780 output = tools.GetOutputFilename('output')
1781
1782 self.run_test(['decl'], dtb_file, output, True)
1783 with open(output) as infile:
1784 data = infile.read()
1785
1786 self._check_strings(self.decl_text_inst, data)
Simon Glassbe749002021-02-03 06:01:15 -07001787
1788 self.run_test(['platdata'], dtb_file, output, True)
1789 with open(output) as infile:
1790 data = infile.read()
1791
1792 self._check_strings(C_HEADER_PRE + '''
1793/* This file is not used: --instantiate was enabled */
1794''', data)
Simon Glassc7b4b832021-02-03 06:01:20 -07001795
1796 self.run_test(['uclass'], dtb_file, output, True)
1797 with open(output) as infile:
1798 data = infile.read()
1799
1800 self._check_strings(UCLASS_HEADER_COMMON + self.uclass_text_inst, data)
1801
Simon Glassfea2f252021-02-03 06:01:21 -07001802 self.run_test(['device'], dtb_file, output, True)
1803 with open(output) as infile:
1804 data = infile.read()
1805
1806 self._check_strings(self.device_text_inst, data)
1807
Simon Glassc7b4b832021-02-03 06:01:20 -07001808 def test_inst_no_hdr(self):
Simon Glassfea2f252021-02-03 06:01:21 -07001809 """Test dealing with a struct tsssshat has no header"""
Simon Glassc7b4b832021-02-03 06:01:20 -07001810 dtb_file = get_dtb_file('dtoc_test_inst.dts')
1811 output = tools.GetOutputFilename('output')
1812
1813 # Run it once to set everything up
1814 plat = self.run_test(['decl'], dtb_file, output, True)
1815 scan = plat._scan
1816
1817 # Restart the output file and delete any record of the uclass' struct
1818 plat.setup_output(Ftype.SOURCE, output)
1819 del scan._structs['dm_test_uc_priv']
1820
1821 # Now generate the uclasses, which should provide a warning
1822 with test_util.capture_sys_output() as (stdout, _):
1823 plat.generate_uclasses()
1824 self.assertEqual(
1825 'Warning: Cannot find header file for struct dm_test_uc_priv',
1826 stdout.getvalue().strip())