blob: b4c0a042a9f935d303ebf6873b9cc4552aaf183e [file] [log] [blame]
Simon Glass3c19dc82019-10-31 07:42:55 -06001#!/usr/bin/env python3
Tom Rini83d290c2018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glassc0791922017-06-18 22:09:06 -06003# Copyright (c) 2012 The Chromium OS Authors.
4#
Simon Glassc0791922017-06-18 22:09:06 -06005
6"""Tests for the dtb_platdata module
7
Simon Glass3def0cf2018-07-06 10:27:20 -06008This includes unit tests for some functions and functional tests for the dtoc
9tool.
Simon Glassc0791922017-06-18 22:09:06 -060010"""
11
12import collections
Simon Glassa32eb7d2021-02-03 06:00:51 -070013import copy
Simon Glass10cbd3b2020-12-28 20:34:52 -070014import glob
Simon Glassc0791922017-06-18 22:09:06 -060015import os
16import struct
17import unittest
18
Simon Glassc0791922017-06-18 22:09:06 -060019from dtb_platdata import get_value
20from dtb_platdata import tab_to
Simon Glass67b5ec52020-12-28 20:34:47 -070021from dtoc import dtb_platdata
Simon Glassbf776672020-04-17 18:09:04 -060022from dtoc import fdt
23from dtoc import fdt_util
Simon Glassa32eb7d2021-02-03 06:00:51 -070024from dtoc import src_scan
Simon Glassa542a702020-12-28 20:35:06 -070025from dtoc.src_scan import conv_name_to_c
26from dtoc.src_scan import get_compat_name
Simon Glassbf776672020-04-17 18:09:04 -060027from patman import test_util
28from patman import tools
Simon Glassc0791922017-06-18 22:09:06 -060029
Simon Glass67b5ec52020-12-28 20:34:47 -070030OUR_PATH = os.path.dirname(os.path.realpath(__file__))
Simon Glassc0791922017-06-18 22:09:06 -060031
32
Simon Glassaab660f2017-11-12 21:52:17 -070033HEADER = '''/*
34 * DO NOT MODIFY
35 *
Simon Glassd1055d62020-12-28 20:35:00 -070036 * Defines the structs used to hold devicetree data.
37 * This was generated by dtoc from a .dtb (device tree binary) file.
Simon Glassaab660f2017-11-12 21:52:17 -070038 */
39
40#include <stdbool.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090041#include <linux/libfdt.h>'''
Simon Glassaab660f2017-11-12 21:52:17 -070042
43C_HEADER = '''/*
44 * DO NOT MODIFY
45 *
Simon Glassd1055d62020-12-28 20:35:00 -070046 * Declares the U_BOOT_DRIVER() records and platform data.
47 * This was generated by dtoc from a .dtb (device tree binary) file.
Simon Glassaab660f2017-11-12 21:52:17 -070048 */
49
Simon Glass20e442a2020-12-28 20:34:54 -070050/* Allow use of U_BOOT_DRVINFO() in this file */
Simon Glassf31fa992020-12-28 20:35:01 -070051#define DT_PLAT_C
Simon Glasscb43ac12020-10-03 11:31:41 -060052
Simon Glassaab660f2017-11-12 21:52:17 -070053#include <common.h>
54#include <dm.h>
55#include <dt-structs.h>
56'''
57
Simon Glassa32eb7d2021-02-03 06:00:51 -070058# Scanner saved from a previous run of the tests (to speed things up)
59saved_scan = None
60
Simon Glass67b5ec52020-12-28 20:34:47 -070061# This is a test so is allowed to access private things in the module it is
62# testing
63# pylint: disable=W0212
Simon Glassfe57c782018-07-06 10:27:37 -060064
65def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glassc0791922017-06-18 22:09:06 -060066 """Compile a .dts file to a .dtb
67
68 Args:
Simon Glass67b5ec52020-12-28 20:34:47 -070069 dts_fname (str): Filename of .dts file in the current directory
70 capture_stderr (bool): True to capture and discard stderr output
Simon Glassc0791922017-06-18 22:09:06 -060071
72 Returns:
Simon Glass67b5ec52020-12-28 20:34:47 -070073 str: Filename of compiled file in output directory
Simon Glassc0791922017-06-18 22:09:06 -060074 """
Simon Glassdff51a52021-02-03 06:00:56 -070075 return fdt_util.EnsureCompiled(os.path.join(OUR_PATH, 'test', dts_fname),
Simon Glassfe57c782018-07-06 10:27:37 -060076 capture_stderr=capture_stderr)
Simon Glassc0791922017-06-18 22:09:06 -060077
78
Simon Glassa32eb7d2021-02-03 06:00:51 -070079def setup():
80 global saved_scan
81
82 # Disable warnings so that calls to get_normalized_compat_name() will not
83 # output things.
84 saved_scan = src_scan.Scanner(None, True, False)
85 saved_scan.scan_drivers()
86
87def copy_scan():
88 """Get a copy of saved_scan so that each test can start clean"""
89 return copy.deepcopy(saved_scan)
90
91
Simon Glassc0791922017-06-18 22:09:06 -060092class TestDtoc(unittest.TestCase):
93 """Tests for dtoc"""
94 @classmethod
95 def setUpClass(cls):
96 tools.PrepareOutputDir(None)
Simon Glassf02d0eb2020-07-07 21:32:06 -060097 cls.maxDiff = None
Simon Glassc0791922017-06-18 22:09:06 -060098
99 @classmethod
100 def tearDownClass(cls):
Simon Glass67b5ec52020-12-28 20:34:47 -0700101 tools.FinaliseOutputDir()
Simon Glassc0791922017-06-18 22:09:06 -0600102
Simon Glass67b5ec52020-12-28 20:34:47 -0700103 @staticmethod
104 def _write_python_string(fname, data):
Simon Glass57f0bc42018-07-06 10:27:25 -0600105 """Write a string with tabs expanded as done in this Python file
106
107 Args:
Simon Glass67b5ec52020-12-28 20:34:47 -0700108 fname (str): Filename to write to
109 data (str): Raw string to convert
Simon Glass57f0bc42018-07-06 10:27:25 -0600110 """
111 data = data.replace('\t', '\\t')
Simon Glass67b5ec52020-12-28 20:34:47 -0700112 with open(fname, 'w') as fout:
113 fout.write(data)
Simon Glass57f0bc42018-07-06 10:27:25 -0600114
Simon Glass67b5ec52020-12-28 20:34:47 -0700115 def _check_strings(self, expected, actual):
Simon Glass57f0bc42018-07-06 10:27:25 -0600116 """Check that a string matches its expected value
117
118 If the strings do not match, they are written to the /tmp directory in
119 the same Python format as is used here in the test. This allows for
120 easy comparison and update of the tests.
121
122 Args:
Simon Glass67b5ec52020-12-28 20:34:47 -0700123 expected (str): Expected string
124 actual (str): Actual string
Simon Glass57f0bc42018-07-06 10:27:25 -0600125 """
126 if expected != actual:
Simon Glass67b5ec52020-12-28 20:34:47 -0700127 self._write_python_string('/tmp/binman.expected', expected)
128 self._write_python_string('/tmp/binman.actual', actual)
Simon Glass90a81322019-05-17 22:00:31 -0600129 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glass67b5ec52020-12-28 20:34:47 -0700130 self.assertEqual(expected, actual)
Simon Glass57f0bc42018-07-06 10:27:25 -0600131
Simon Glass67b5ec52020-12-28 20:34:47 -0700132 @staticmethod
133 def run_test(args, dtb_file, output):
134 """Run a test using dtoc
Walter Lozano361e7332020-06-25 01:10:08 -0300135
Simon Glass67b5ec52020-12-28 20:34:47 -0700136 Args:
137 args (list of str): List of arguments for dtoc
138 dtb_file (str): Filename of .dtb file
139 output (str): Filename of output file
Simon Glass05953522021-02-03 06:01:07 -0700140
141 Returns:
142 DtbPlatdata object
Simon Glass67b5ec52020-12-28 20:34:47 -0700143 """
Simon Glass05953522021-02-03 06:01:07 -0700144 return dtb_platdata.run_steps(args, dtb_file, False, output, [], None,
145 warning_disabled=True, scan=copy_scan())
Walter Lozano361e7332020-06-25 01:10:08 -0300146
Simon Glassc0791922017-06-18 22:09:06 -0600147 def test_name(self):
148 """Test conversion of device tree names to C identifiers"""
149 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
150 self.assertEqual('vendor_clock_frequency',
151 conv_name_to_c('vendor,clock-frequency'))
152 self.assertEqual('rockchip_rk3399_sdhci_5_1',
153 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
154
155 def test_tab_to(self):
156 """Test operation of tab_to() function"""
157 self.assertEqual('fred ', tab_to(0, 'fred'))
158 self.assertEqual('fred\t', tab_to(1, 'fred'))
159 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
160 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
161 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
162 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
163
164 def test_get_value(self):
165 """Test operation of get_value() function"""
166 self.assertEqual('0x45',
Simon Glass5ea9dcc2020-11-08 20:36:17 -0700167 get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
Simon Glassc0791922017-06-18 22:09:06 -0600168 self.assertEqual('0x45',
Simon Glass5ea9dcc2020-11-08 20:36:17 -0700169 get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
Simon Glassc0791922017-06-18 22:09:06 -0600170 self.assertEqual('0x0',
Simon Glass5ea9dcc2020-11-08 20:36:17 -0700171 get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
172 self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
173 self.assertEqual('true', get_value(fdt.Type.BOOL, None))
Simon Glassc0791922017-06-18 22:09:06 -0600174
175 def test_get_compat_name(self):
176 """Test operation of get_compat_name() function"""
177 Prop = collections.namedtuple('Prop', ['value'])
178 Node = collections.namedtuple('Node', ['props'])
179
180 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
181 node = Node({'compatible': prop})
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300182 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glassc0791922017-06-18 22:09:06 -0600183 get_compat_name(node))
184
185 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
186 node = Node({'compatible': prop})
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300187 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glassc0791922017-06-18 22:09:06 -0600188 get_compat_name(node))
189
190 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
191 node = Node({'compatible': prop})
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300192 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
Simon Glass67b5ec52020-12-28 20:34:47 -0700193 'arasan_sdhci_5_1', 'third']),
Simon Glassc0791922017-06-18 22:09:06 -0600194 get_compat_name(node))
195
196 def test_empty_file(self):
197 """Test output from a device tree file with no nodes"""
198 dtb_file = get_dtb_file('dtoc_test_empty.dts')
199 output = tools.GetOutputFilename('output')
Simon Glassa32eb7d2021-02-03 06:00:51 -0700200
201 # Run this one without saved_scan to complete test coverage
202 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [], True)
Simon Glassc0791922017-06-18 22:09:06 -0600203 with open(output) as infile:
204 lines = infile.read().splitlines()
Simon Glassaab660f2017-11-12 21:52:17 -0700205 self.assertEqual(HEADER.splitlines(), lines)
Simon Glassc0791922017-06-18 22:09:06 -0600206
Walter Lozano361e7332020-06-25 01:10:08 -0300207 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600208 with open(output) as infile:
209 lines = infile.read().splitlines()
Simon Glassd960f0d2020-12-28 20:35:05 -0700210 self.assertEqual(C_HEADER.splitlines() + [''], lines)
Simon Glassc0791922017-06-18 22:09:06 -0600211
Simon Glassde846cb2020-12-28 20:34:49 -0700212 struct_text = HEADER + '''
Simon Glassf38161c2021-02-03 06:00:57 -0700213struct dtd_sandbox_i2c {
Simon Glass5ec741f2017-08-29 14:15:51 -0600214};
Simon Glassf38161c2021-02-03 06:00:57 -0700215struct dtd_sandbox_pmic {
Simon Glass5ec741f2017-08-29 14:15:51 -0600216\tbool\t\tlow_power;
217\tfdt64_t\t\treg[2];
218};
Simon Glassc0791922017-06-18 22:09:06 -0600219struct dtd_sandbox_spl_test {
Simon Glassf02d0eb2020-07-07 21:32:06 -0600220\tconst char * acpi_name;
Simon Glassc0791922017-06-18 22:09:06 -0600221\tbool\t\tboolval;
222\tunsigned char\tbytearray[3];
223\tunsigned char\tbyteval;
224\tfdt32_t\t\tintarray[4];
225\tfdt32_t\t\tintval;
226\tunsigned char\tlongbytearray[9];
Simon Glass2a2d91d2018-07-06 10:27:28 -0600227\tunsigned char\tnotstring[5];
Simon Glassc0791922017-06-18 22:09:06 -0600228\tconst char *\tstringarray[3];
229\tconst char *\tstringval;
230};
Simon Glassde846cb2020-12-28 20:34:49 -0700231'''
Simon Glassc0791922017-06-18 22:09:06 -0600232
Simon Glassde846cb2020-12-28 20:34:49 -0700233 platdata_text = C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600234/* Node /i2c@0 index 0 */
Simon Glassf38161c2021-02-03 06:00:57 -0700235static struct dtd_sandbox_i2c dtv_i2c_at_0 = {
Simon Glass1b272732020-10-03 11:31:25 -0600236};
Simon Glass20e442a2020-12-28 20:34:54 -0700237U_BOOT_DRVINFO(i2c_at_0) = {
Simon Glassf38161c2021-02-03 06:00:57 -0700238\t.name\t\t= "sandbox_i2c",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700239\t.plat\t= &dtv_i2c_at_0,
Simon Glass4f500862020-12-03 16:55:19 -0700240\t.plat_size\t= sizeof(dtv_i2c_at_0),
Simon Glasse41651f2020-10-03 11:31:35 -0600241\t.parent_idx\t= -1,
Simon Glass1b272732020-10-03 11:31:25 -0600242};
243
244/* Node /i2c@0/pmic@9 index 1 */
Simon Glassf38161c2021-02-03 06:00:57 -0700245static struct dtd_sandbox_pmic dtv_pmic_at_9 = {
Simon Glass1b272732020-10-03 11:31:25 -0600246\t.low_power\t\t= true,
247\t.reg\t\t\t= {0x9, 0x0},
248};
Simon Glass20e442a2020-12-28 20:34:54 -0700249U_BOOT_DRVINFO(pmic_at_9) = {
Simon Glassf38161c2021-02-03 06:00:57 -0700250\t.name\t\t= "sandbox_pmic",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700251\t.plat\t= &dtv_pmic_at_9,
Simon Glass4f500862020-12-03 16:55:19 -0700252\t.plat_size\t= sizeof(dtv_pmic_at_9),
Simon Glasse41651f2020-10-03 11:31:35 -0600253\t.parent_idx\t= 0,
Simon Glass1b272732020-10-03 11:31:25 -0600254};
255
256/* Node /spl-test index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300257static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass1953ce72019-05-17 22:00:32 -0600258\t.boolval\t\t= true,
Simon Glassc0791922017-06-18 22:09:06 -0600259\t.bytearray\t\t= {0x6, 0x0, 0x0},
260\t.byteval\t\t= 0x5,
Simon Glass1953ce72019-05-17 22:00:32 -0600261\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600262\t.intval\t\t\t= 0x1,
Simon Glass21d54ac2017-08-29 14:15:49 -0600263\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
264\t\t0x11},
Simon Glass1953ce72019-05-17 22:00:32 -0600265\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600266\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glass1953ce72019-05-17 22:00:32 -0600267\t.stringval\t\t= "message",
Simon Glassc0791922017-06-18 22:09:06 -0600268};
Simon Glass20e442a2020-12-28 20:34:54 -0700269U_BOOT_DRVINFO(spl_test) = {
Simon Glassc0791922017-06-18 22:09:06 -0600270\t.name\t\t= "sandbox_spl_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700271\t.plat\t= &dtv_spl_test,
Simon Glass4f500862020-12-03 16:55:19 -0700272\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glasse41651f2020-10-03 11:31:35 -0600273\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600274};
275
Simon Glass1b272732020-10-03 11:31:25 -0600276/* Node /spl-test2 index 3 */
Walter Lozano51f12632020-06-25 01:10:13 -0300277static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glassf02d0eb2020-07-07 21:32:06 -0600278\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glassc0791922017-06-18 22:09:06 -0600279\t.bytearray\t\t= {0x1, 0x23, 0x34},
280\t.byteval\t\t= 0x8,
Simon Glass1953ce72019-05-17 22:00:32 -0600281\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600282\t.intval\t\t\t= 0x3,
Simon Glasse144caf2020-10-03 11:31:27 -0600283\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
Simon Glass21d54ac2017-08-29 14:15:49 -0600284\t\t0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600285\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glass1953ce72019-05-17 22:00:32 -0600286\t.stringval\t\t= "message2",
Simon Glassc0791922017-06-18 22:09:06 -0600287};
Simon Glass20e442a2020-12-28 20:34:54 -0700288U_BOOT_DRVINFO(spl_test2) = {
Simon Glassc0791922017-06-18 22:09:06 -0600289\t.name\t\t= "sandbox_spl_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700290\t.plat\t= &dtv_spl_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700291\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600292\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600293};
294
Simon Glass1b272732020-10-03 11:31:25 -0600295/* Node /spl-test3 index 4 */
Walter Lozano51f12632020-06-25 01:10:13 -0300296static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glasse144caf2020-10-03 11:31:27 -0600297\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
298\t\t0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600299\t.stringarray\t\t= {"one", "", ""},
300};
Simon Glass20e442a2020-12-28 20:34:54 -0700301U_BOOT_DRVINFO(spl_test3) = {
Simon Glassc0791922017-06-18 22:09:06 -0600302\t.name\t\t= "sandbox_spl_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700303\t.plat\t= &dtv_spl_test3,
Simon Glass4f500862020-12-03 16:55:19 -0700304\t.plat_size\t= sizeof(dtv_spl_test3),
Simon Glasse41651f2020-10-03 11:31:35 -0600305\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600306};
307
Simon Glassd960f0d2020-12-28 20:35:05 -0700308'''
Simon Glassde846cb2020-12-28 20:34:49 -0700309
310 def test_simple(self):
311 """Test output from some simple nodes with various types of data"""
312 dtb_file = get_dtb_file('dtoc_test_simple.dts')
313 output = tools.GetOutputFilename('output')
314 self.run_test(['struct'], dtb_file, output)
315 with open(output) as infile:
316 data = infile.read()
317
318 self._check_strings(self.struct_text, data)
319
320 self.run_test(['platdata'], dtb_file, output)
321 with open(output) as infile:
322 data = infile.read()
323
324 self._check_strings(self.platdata_text, data)
Simon Glassc0791922017-06-18 22:09:06 -0600325
Simon Glass10cbd3b2020-12-28 20:34:52 -0700326 # Try the 'all' command
327 self.run_test(['all'], dtb_file, output)
328 data = tools.ReadFile(output, binary=False)
329 self._check_strings(self.platdata_text + self.struct_text, data)
330
Walter Lozanodac82282020-07-03 08:07:17 -0300331 def test_driver_alias(self):
332 """Test output from a device tree file with a driver alias"""
333 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
334 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300335 self.run_test(['struct'], dtb_file, output)
Walter Lozanodac82282020-07-03 08:07:17 -0300336 with open(output) as infile:
337 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700338 self._check_strings(HEADER + '''
Walter Lozanodac82282020-07-03 08:07:17 -0300339struct dtd_sandbox_gpio {
340\tconst char *\tgpio_bank_name;
341\tbool\t\tgpio_controller;
342\tfdt32_t\t\tsandbox_gpio_count;
343};
Walter Lozanodac82282020-07-03 08:07:17 -0300344''', data)
345
Walter Lozano361e7332020-06-25 01:10:08 -0300346 self.run_test(['platdata'], dtb_file, output)
Walter Lozanodac82282020-07-03 08:07:17 -0300347 with open(output) as infile:
348 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700349 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600350/* Node /gpios@0 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300351static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanodac82282020-07-03 08:07:17 -0300352\t.gpio_bank_name\t\t= "a",
353\t.gpio_controller\t= true,
354\t.sandbox_gpio_count\t= 0x14,
355};
Simon Glass20e442a2020-12-28 20:34:54 -0700356U_BOOT_DRVINFO(gpios_at_0) = {
Walter Lozanodac82282020-07-03 08:07:17 -0300357\t.name\t\t= "sandbox_gpio",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700358\t.plat\t= &dtv_gpios_at_0,
Simon Glass4f500862020-12-03 16:55:19 -0700359\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glasse41651f2020-10-03 11:31:35 -0600360\t.parent_idx\t= -1,
Walter Lozanodac82282020-07-03 08:07:17 -0300361};
362
363''', data)
364
Walter Lozano361e7332020-06-25 01:10:08 -0300365 def test_invalid_driver(self):
366 """Test output from a device tree file with an invalid driver"""
367 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
368 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700369 with test_util.capture_sys_output() as _:
Simon Glassa32eb7d2021-02-03 06:00:51 -0700370 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [],
Simon Glassb00f0062021-02-03 06:01:02 -0700371 None, scan=copy_scan())
Walter Lozano361e7332020-06-25 01:10:08 -0300372 with open(output) as infile:
373 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700374 self._check_strings(HEADER + '''
Walter Lozano361e7332020-06-25 01:10:08 -0300375struct dtd_invalid {
376};
377''', data)
378
Simon Glass67b5ec52020-12-28 20:34:47 -0700379 with test_util.capture_sys_output() as _:
Simon Glassa32eb7d2021-02-03 06:00:51 -0700380 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [],
Simon Glassb00f0062021-02-03 06:01:02 -0700381 None, scan=copy_scan())
Walter Lozano361e7332020-06-25 01:10:08 -0300382 with open(output) as infile:
383 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700384 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600385/* Node /spl-test index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300386static struct dtd_invalid dtv_spl_test = {
Walter Lozano361e7332020-06-25 01:10:08 -0300387};
Simon Glass20e442a2020-12-28 20:34:54 -0700388U_BOOT_DRVINFO(spl_test) = {
Walter Lozano361e7332020-06-25 01:10:08 -0300389\t.name\t\t= "invalid",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700390\t.plat\t= &dtv_spl_test,
Simon Glass4f500862020-12-03 16:55:19 -0700391\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glasse41651f2020-10-03 11:31:35 -0600392\t.parent_idx\t= -1,
Walter Lozano361e7332020-06-25 01:10:08 -0300393};
394
395''', data)
396
Simon Glassc0791922017-06-18 22:09:06 -0600397 def test_phandle(self):
398 """Test output from a node containing a phandle reference"""
399 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
400 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300401 self.run_test(['struct'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600402 with open(output) as infile:
403 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700404 self._check_strings(HEADER + '''
Simon Glassc0791922017-06-18 22:09:06 -0600405struct dtd_source {
Simon Glass634eba42017-08-29 14:15:59 -0600406\tstruct phandle_2_arg clocks[4];
Simon Glassc0791922017-06-18 22:09:06 -0600407};
408struct dtd_target {
409\tfdt32_t\t\tintval;
410};
411''', data)
412
Walter Lozano361e7332020-06-25 01:10:08 -0300413 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600414 with open(output) as infile:
415 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700416 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600417/* Node /phandle2-target index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300418static struct dtd_target dtv_phandle2_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600419\t.intval\t\t\t= 0x1,
420};
Simon Glass20e442a2020-12-28 20:34:54 -0700421U_BOOT_DRVINFO(phandle2_target) = {
Simon Glass634eba42017-08-29 14:15:59 -0600422\t.name\t\t= "target",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700423\t.plat\t= &dtv_phandle2_target,
Simon Glass4f500862020-12-03 16:55:19 -0700424\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600425\t.parent_idx\t= -1,
Simon Glass634eba42017-08-29 14:15:59 -0600426};
427
Simon Glass1b272732020-10-03 11:31:25 -0600428/* Node /phandle3-target index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300429static struct dtd_target dtv_phandle3_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600430\t.intval\t\t\t= 0x2,
431};
Simon Glass20e442a2020-12-28 20:34:54 -0700432U_BOOT_DRVINFO(phandle3_target) = {
Simon Glass634eba42017-08-29 14:15:59 -0600433\t.name\t\t= "target",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700434\t.plat\t= &dtv_phandle3_target,
Simon Glass4f500862020-12-03 16:55:19 -0700435\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600436\t.parent_idx\t= -1,
Simon Glass634eba42017-08-29 14:15:59 -0600437};
438
Simon Glass1b272732020-10-03 11:31:25 -0600439/* Node /phandle-source index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300440static struct dtd_source dtv_phandle_source = {
Simon Glass35d50372017-08-29 14:15:57 -0600441\t.clocks\t\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600442\t\t\t{4, {}},
443\t\t\t{0, {11}},
444\t\t\t{1, {12, 13}},
445\t\t\t{4, {}},},
Simon Glassc0791922017-06-18 22:09:06 -0600446};
Simon Glass20e442a2020-12-28 20:34:54 -0700447U_BOOT_DRVINFO(phandle_source) = {
Simon Glassc0791922017-06-18 22:09:06 -0600448\t.name\t\t= "source",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700449\t.plat\t= &dtv_phandle_source,
Simon Glass4f500862020-12-03 16:55:19 -0700450\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glasse41651f2020-10-03 11:31:35 -0600451\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600452};
453
Simon Glass1b272732020-10-03 11:31:25 -0600454/* Node /phandle-source2 index 3 */
Walter Lozano51f12632020-06-25 01:10:13 -0300455static struct dtd_source dtv_phandle_source2 = {
Simon Glass760b7172018-07-06 10:27:31 -0600456\t.clocks\t\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600457\t\t\t{4, {}},},
Simon Glass760b7172018-07-06 10:27:31 -0600458};
Simon Glass20e442a2020-12-28 20:34:54 -0700459U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass760b7172018-07-06 10:27:31 -0600460\t.name\t\t= "source",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700461\t.plat\t= &dtv_phandle_source2,
Simon Glass4f500862020-12-03 16:55:19 -0700462\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glasse41651f2020-10-03 11:31:35 -0600463\t.parent_idx\t= -1,
Simon Glass760b7172018-07-06 10:27:31 -0600464};
465
Simon Glass9eca08d2020-12-28 20:35:04 -0700466/* Node /phandle-target index 4 */
467static struct dtd_target dtv_phandle_target = {
468\t.intval\t\t\t= 0x0,
469};
470U_BOOT_DRVINFO(phandle_target) = {
471\t.name\t\t= "target",
472\t.plat\t= &dtv_phandle_target,
473\t.plat_size\t= sizeof(dtv_phandle_target),
474\t.parent_idx\t= -1,
475};
476
Simon Glassc0791922017-06-18 22:09:06 -0600477''', data)
478
Simon Glass8512ea22018-07-06 10:27:35 -0600479 def test_phandle_single(self):
480 """Test output from a node containing a phandle reference"""
481 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
482 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300483 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600484 with open(output) as infile:
485 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700486 self._check_strings(HEADER + '''
Simon Glass8512ea22018-07-06 10:27:35 -0600487struct dtd_source {
488\tstruct phandle_0_arg clocks[1];
489};
490struct dtd_target {
491\tfdt32_t\t\tintval;
492};
493''', data)
494
495 def test_phandle_reorder(self):
496 """Test that phandle targets are generated before their references"""
497 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
498 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300499 self.run_test(['platdata'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600500 with open(output) as infile:
501 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700502 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600503/* Node /phandle-source2 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300504static struct dtd_source dtv_phandle_source2 = {
Simon Glass8512ea22018-07-06 10:27:35 -0600505\t.clocks\t\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600506\t\t\t{1, {}},},
Simon Glass8512ea22018-07-06 10:27:35 -0600507};
Simon Glass20e442a2020-12-28 20:34:54 -0700508U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass8512ea22018-07-06 10:27:35 -0600509\t.name\t\t= "source",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700510\t.plat\t= &dtv_phandle_source2,
Simon Glass4f500862020-12-03 16:55:19 -0700511\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glasse41651f2020-10-03 11:31:35 -0600512\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -0600513};
514
Simon Glass9eca08d2020-12-28 20:35:04 -0700515/* Node /phandle-target index 1 */
516static struct dtd_target dtv_phandle_target = {
517};
518U_BOOT_DRVINFO(phandle_target) = {
519\t.name\t\t= "target",
520\t.plat\t= &dtv_phandle_target,
521\t.plat_size\t= sizeof(dtv_phandle_target),
522\t.parent_idx\t= -1,
523};
524
Simon Glass8512ea22018-07-06 10:27:35 -0600525''', data)
526
Walter Lozano6c3fc502020-06-25 01:10:17 -0300527 def test_phandle_cd_gpio(self):
528 """Test that phandle targets are generated when unsing cd-gpios"""
529 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
530 output = tools.GetOutputFilename('output')
Simon Glassa32eb7d2021-02-03 06:00:51 -0700531 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [], True,
532 scan=copy_scan())
Walter Lozano6c3fc502020-06-25 01:10:17 -0300533 with open(output) as infile:
534 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700535 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600536/* Node /phandle2-target index 0 */
Walter Lozano6c3fc502020-06-25 01:10:17 -0300537static struct dtd_target dtv_phandle2_target = {
538\t.intval\t\t\t= 0x1,
539};
Simon Glass20e442a2020-12-28 20:34:54 -0700540U_BOOT_DRVINFO(phandle2_target) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -0300541\t.name\t\t= "target",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700542\t.plat\t= &dtv_phandle2_target,
Simon Glass4f500862020-12-03 16:55:19 -0700543\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600544\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -0300545};
546
Simon Glass1b272732020-10-03 11:31:25 -0600547/* Node /phandle3-target index 1 */
Walter Lozano6c3fc502020-06-25 01:10:17 -0300548static struct dtd_target dtv_phandle3_target = {
549\t.intval\t\t\t= 0x2,
550};
Simon Glass20e442a2020-12-28 20:34:54 -0700551U_BOOT_DRVINFO(phandle3_target) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -0300552\t.name\t\t= "target",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700553\t.plat\t= &dtv_phandle3_target,
Simon Glass4f500862020-12-03 16:55:19 -0700554\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600555\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -0300556};
557
Simon Glass1b272732020-10-03 11:31:25 -0600558/* Node /phandle-source index 2 */
Walter Lozano6c3fc502020-06-25 01:10:17 -0300559static struct dtd_source dtv_phandle_source = {
560\t.cd_gpios\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600561\t\t\t{4, {}},
562\t\t\t{0, {11}},
563\t\t\t{1, {12, 13}},
564\t\t\t{4, {}},},
Walter Lozano6c3fc502020-06-25 01:10:17 -0300565};
Simon Glass20e442a2020-12-28 20:34:54 -0700566U_BOOT_DRVINFO(phandle_source) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -0300567\t.name\t\t= "source",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700568\t.plat\t= &dtv_phandle_source,
Simon Glass4f500862020-12-03 16:55:19 -0700569\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glasse41651f2020-10-03 11:31:35 -0600570\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -0300571};
572
Simon Glass1b272732020-10-03 11:31:25 -0600573/* Node /phandle-source2 index 3 */
Walter Lozano6c3fc502020-06-25 01:10:17 -0300574static struct dtd_source dtv_phandle_source2 = {
575\t.cd_gpios\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600576\t\t\t{4, {}},},
Walter Lozano6c3fc502020-06-25 01:10:17 -0300577};
Simon Glass20e442a2020-12-28 20:34:54 -0700578U_BOOT_DRVINFO(phandle_source2) = {
Walter Lozano6c3fc502020-06-25 01:10:17 -0300579\t.name\t\t= "source",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700580\t.plat\t= &dtv_phandle_source2,
Simon Glass4f500862020-12-03 16:55:19 -0700581\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glasse41651f2020-10-03 11:31:35 -0600582\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -0300583};
584
Simon Glass9eca08d2020-12-28 20:35:04 -0700585/* Node /phandle-target index 4 */
586static struct dtd_target dtv_phandle_target = {
587\t.intval\t\t\t= 0x0,
588};
589U_BOOT_DRVINFO(phandle_target) = {
590\t.name\t\t= "target",
591\t.plat\t= &dtv_phandle_target,
592\t.plat_size\t= sizeof(dtv_phandle_target),
593\t.parent_idx\t= -1,
594};
595
Walter Lozano6c3fc502020-06-25 01:10:17 -0300596''', data)
597
Simon Glass8512ea22018-07-06 10:27:35 -0600598 def test_phandle_bad(self):
599 """Test a node containing an invalid phandle fails"""
Simon Glass4b4bc062018-10-01 21:12:43 -0600600 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
601 capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600602 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700603 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300604 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600605 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
Simon Glass67b5ec52020-12-28 20:34:47 -0700606 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -0600607
608 def test_phandle_bad2(self):
609 """Test a phandle target missing its #*-cells property"""
Simon Glass4b4bc062018-10-01 21:12:43 -0600610 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
611 capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600612 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700613 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300614 self.run_test(['struct'], dtb_file, output)
Walter Lozanoad340172020-06-25 01:10:16 -0300615 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glass67b5ec52020-12-28 20:34:47 -0700616 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -0600617
Simon Glassc20ee0e2017-08-29 14:15:50 -0600618 def test_addresses64(self):
619 """Test output from a node with a 'reg' property with na=2, ns=2"""
620 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
621 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300622 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600623 with open(output) as infile:
624 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700625 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600626struct dtd_test1 {
627\tfdt64_t\t\treg[2];
628};
629struct dtd_test2 {
630\tfdt64_t\t\treg[2];
631};
632struct dtd_test3 {
633\tfdt64_t\t\treg[4];
634};
635''', data)
636
Walter Lozano361e7332020-06-25 01:10:08 -0300637 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600638 with open(output) as infile:
639 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700640 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600641/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300642static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600643\t.reg\t\t\t= {0x1234, 0x5678},
644};
Simon Glass20e442a2020-12-28 20:34:54 -0700645U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600646\t.name\t\t= "test1",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700647\t.plat\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -0700648\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -0600649\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600650};
651
Simon Glass1b272732020-10-03 11:31:25 -0600652/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300653static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600654\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
655};
Simon Glass20e442a2020-12-28 20:34:54 -0700656U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600657\t.name\t\t= "test2",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700658\t.plat\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700659\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600660\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600661};
662
Simon Glass1b272732020-10-03 11:31:25 -0600663/* Node /test3 index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300664static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600665\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
666};
Simon Glass20e442a2020-12-28 20:34:54 -0700667U_BOOT_DRVINFO(test3) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600668\t.name\t\t= "test3",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700669\t.plat\t= &dtv_test3,
Simon Glass4f500862020-12-03 16:55:19 -0700670\t.plat_size\t= sizeof(dtv_test3),
Simon Glasse41651f2020-10-03 11:31:35 -0600671\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600672};
673
Simon Glassd960f0d2020-12-28 20:35:05 -0700674''', data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600675
676 def test_addresses32(self):
677 """Test output from a node with a 'reg' property with na=1, ns=1"""
678 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
679 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300680 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600681 with open(output) as infile:
682 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700683 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600684struct dtd_test1 {
685\tfdt32_t\t\treg[2];
686};
687struct dtd_test2 {
688\tfdt32_t\t\treg[4];
689};
690''', data)
691
Walter Lozano361e7332020-06-25 01:10:08 -0300692 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600693 with open(output) as infile:
694 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700695 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600696/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300697static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600698\t.reg\t\t\t= {0x1234, 0x5678},
699};
Simon Glass20e442a2020-12-28 20:34:54 -0700700U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600701\t.name\t\t= "test1",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700702\t.plat\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -0700703\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -0600704\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600705};
706
Simon Glass1b272732020-10-03 11:31:25 -0600707/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300708static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600709\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
710};
Simon Glass20e442a2020-12-28 20:34:54 -0700711U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600712\t.name\t\t= "test2",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700713\t.plat\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700714\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600715\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600716};
717
Simon Glassd960f0d2020-12-28 20:35:05 -0700718''', data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600719
720 def test_addresses64_32(self):
721 """Test output from a node with a 'reg' property with na=2, ns=1"""
722 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
723 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300724 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600725 with open(output) as infile:
726 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700727 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600728struct dtd_test1 {
729\tfdt64_t\t\treg[2];
730};
731struct dtd_test2 {
732\tfdt64_t\t\treg[2];
733};
734struct dtd_test3 {
735\tfdt64_t\t\treg[4];
736};
737''', data)
738
Walter Lozano361e7332020-06-25 01:10:08 -0300739 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600740 with open(output) as infile:
741 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700742 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600743/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300744static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600745\t.reg\t\t\t= {0x123400000000, 0x5678},
746};
Simon Glass20e442a2020-12-28 20:34:54 -0700747U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600748\t.name\t\t= "test1",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700749\t.plat\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -0700750\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -0600751\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600752};
753
Simon Glass1b272732020-10-03 11:31:25 -0600754/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300755static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600756\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
757};
Simon Glass20e442a2020-12-28 20:34:54 -0700758U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600759\t.name\t\t= "test2",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700760\t.plat\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700761\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600762\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600763};
764
Simon Glass1b272732020-10-03 11:31:25 -0600765/* Node /test3 index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300766static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600767\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
768};
Simon Glass20e442a2020-12-28 20:34:54 -0700769U_BOOT_DRVINFO(test3) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600770\t.name\t\t= "test3",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700771\t.plat\t= &dtv_test3,
Simon Glass4f500862020-12-03 16:55:19 -0700772\t.plat_size\t= sizeof(dtv_test3),
Simon Glasse41651f2020-10-03 11:31:35 -0600773\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600774};
775
Simon Glassd960f0d2020-12-28 20:35:05 -0700776''', data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600777
778 def test_addresses32_64(self):
779 """Test output from a node with a 'reg' property with na=1, ns=2"""
780 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
781 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300782 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600783 with open(output) as infile:
784 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700785 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600786struct dtd_test1 {
787\tfdt64_t\t\treg[2];
788};
789struct dtd_test2 {
790\tfdt64_t\t\treg[2];
791};
792struct dtd_test3 {
793\tfdt64_t\t\treg[4];
794};
795''', data)
796
Walter Lozano361e7332020-06-25 01:10:08 -0300797 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600798 with open(output) as infile:
799 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700800 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600801/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300802static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600803\t.reg\t\t\t= {0x1234, 0x567800000000},
804};
Simon Glass20e442a2020-12-28 20:34:54 -0700805U_BOOT_DRVINFO(test1) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600806\t.name\t\t= "test1",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700807\t.plat\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -0700808\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -0600809\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600810};
811
Simon Glass1b272732020-10-03 11:31:25 -0600812/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300813static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600814\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
815};
Simon Glass20e442a2020-12-28 20:34:54 -0700816U_BOOT_DRVINFO(test2) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600817\t.name\t\t= "test2",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700818\t.plat\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700819\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600820\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600821};
822
Simon Glass1b272732020-10-03 11:31:25 -0600823/* Node /test3 index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300824static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600825\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
826};
Simon Glass20e442a2020-12-28 20:34:54 -0700827U_BOOT_DRVINFO(test3) = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600828\t.name\t\t= "test3",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700829\t.plat\t= &dtv_test3,
Simon Glass4f500862020-12-03 16:55:19 -0700830\t.plat_size\t= sizeof(dtv_test3),
Simon Glasse41651f2020-10-03 11:31:35 -0600831\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600832};
833
Simon Glassd960f0d2020-12-28 20:35:05 -0700834''', data)
Simon Glass8512ea22018-07-06 10:27:35 -0600835
836 def test_bad_reg(self):
837 """Test that a reg property with an invalid type generates an error"""
Simon Glassfe57c782018-07-06 10:27:37 -0600838 # Capture stderr since dtc will emit warnings for this file
839 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600840 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700841 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300842 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600843 self.assertIn("Node 'spl-test' reg property is not an int",
Simon Glass67b5ec52020-12-28 20:34:47 -0700844 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -0600845
846 def test_bad_reg2(self):
847 """Test that a reg property with an invalid cell count is detected"""
Simon Glassfe57c782018-07-06 10:27:37 -0600848 # Capture stderr since dtc will emit warnings for this file
849 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600850 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700851 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300852 self.run_test(['struct'], dtb_file, output)
Simon Glass67b5ec52020-12-28 20:34:47 -0700853 self.assertIn(
854 "Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
855 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -0600856
857 def test_add_prop(self):
858 """Test that a subequent node can add a new property to a struct"""
859 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
860 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300861 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600862 with open(output) as infile:
863 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700864 self._check_strings(HEADER + '''
Simon Glass8512ea22018-07-06 10:27:35 -0600865struct dtd_sandbox_spl_test {
866\tfdt32_t\t\tintarray;
867\tfdt32_t\t\tintval;
868};
869''', data)
870
Walter Lozano361e7332020-06-25 01:10:08 -0300871 self.run_test(['platdata'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600872 with open(output) as infile:
873 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700874 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600875/* Node /spl-test index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300876static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass8512ea22018-07-06 10:27:35 -0600877\t.intval\t\t\t= 0x1,
878};
Simon Glass20e442a2020-12-28 20:34:54 -0700879U_BOOT_DRVINFO(spl_test) = {
Simon Glass8512ea22018-07-06 10:27:35 -0600880\t.name\t\t= "sandbox_spl_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700881\t.plat\t= &dtv_spl_test,
Simon Glass4f500862020-12-03 16:55:19 -0700882\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glasse41651f2020-10-03 11:31:35 -0600883\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -0600884};
885
Simon Glass1b272732020-10-03 11:31:25 -0600886/* Node /spl-test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300887static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass8512ea22018-07-06 10:27:35 -0600888\t.intarray\t\t= 0x5,
889};
Simon Glass20e442a2020-12-28 20:34:54 -0700890U_BOOT_DRVINFO(spl_test2) = {
Simon Glass8512ea22018-07-06 10:27:35 -0600891\t.name\t\t= "sandbox_spl_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700892\t.plat\t= &dtv_spl_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700893\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600894\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -0600895};
896
Simon Glassd960f0d2020-12-28 20:35:05 -0700897''', data)
Simon Glass8512ea22018-07-06 10:27:35 -0600898
Simon Glass67b5ec52020-12-28 20:34:47 -0700899 def test_stdout(self):
Simon Glass8512ea22018-07-06 10:27:35 -0600900 """Test output to stdout"""
901 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glassde846cb2020-12-28 20:34:49 -0700902 with test_util.capture_sys_output() as (stdout, _):
Simon Glassf62cea02020-12-28 20:34:48 -0700903 self.run_test(['struct'], dtb_file, None)
Simon Glassde846cb2020-12-28 20:34:49 -0700904 self._check_strings(self.struct_text, stdout.getvalue())
Simon Glass8512ea22018-07-06 10:27:35 -0600905
Simon Glassbe44f272020-12-28 20:34:51 -0700906 def test_multi_to_file(self):
907 """Test output of multiple pieces to a single file"""
908 dtb_file = get_dtb_file('dtoc_test_simple.dts')
909 output = tools.GetOutputFilename('output')
Simon Glass10cbd3b2020-12-28 20:34:52 -0700910 self.run_test(['all'], dtb_file, output)
Simon Glassbe44f272020-12-28 20:34:51 -0700911 data = tools.ReadFile(output, binary=False)
Simon Glass10cbd3b2020-12-28 20:34:52 -0700912 self._check_strings(self.platdata_text + self.struct_text, data)
Simon Glassbe44f272020-12-28 20:34:51 -0700913
Simon Glass67b5ec52020-12-28 20:34:47 -0700914 def test_no_command(self):
Simon Glass8512ea22018-07-06 10:27:35 -0600915 """Test running dtoc without a command"""
Simon Glass67b5ec52020-12-28 20:34:47 -0700916 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300917 self.run_test([], '', '')
Simon Glass8512ea22018-07-06 10:27:35 -0600918 self.assertIn("Please specify a command: struct, platdata",
Simon Glass67b5ec52020-12-28 20:34:47 -0700919 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -0600920
Simon Glass67b5ec52020-12-28 20:34:47 -0700921 def test_bad_command(self):
Simon Glass8512ea22018-07-06 10:27:35 -0600922 """Test running dtoc with an invalid command"""
923 dtb_file = get_dtb_file('dtoc_test_simple.dts')
924 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700925 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300926 self.run_test(['invalid-cmd'], dtb_file, output)
Simon Glass10cbd3b2020-12-28 20:34:52 -0700927 self.assertIn("Unknown command 'invalid-cmd': (use: platdata, struct)",
Simon Glass67b5ec52020-12-28 20:34:47 -0700928 str(exc.exception))
Walter Lozano6c74d1b2020-07-28 19:06:23 -0300929
Simon Glass10cbd3b2020-12-28 20:34:52 -0700930 def test_output_conflict(self):
931 """Test a conflict between and output dirs and output file"""
932 with self.assertRaises(ValueError) as exc:
Simon Glassb00f0062021-02-03 06:01:02 -0700933 dtb_platdata.run_steps(['all'], None, False, 'out', ['cdir'], None,
934 warning_disabled=True, scan=copy_scan())
Simon Glass10cbd3b2020-12-28 20:34:52 -0700935 self.assertIn("Must specify either output or output_dirs, not both",
936 str(exc.exception))
937
938 def test_output_dirs(self):
939 """Test outputting files to a directory"""
940 # Remove the directory so that files from other tests are not there
941 tools._RemoveOutputDir()
942 tools.PrepareOutputDir(None)
943
944 # This should create the .dts and .dtb in the output directory
945 dtb_file = get_dtb_file('dtoc_test_simple.dts')
946 outdir = tools.GetOutputDir()
947 fnames = glob.glob(outdir + '/*')
948 self.assertEqual(2, len(fnames))
949
Simon Glassb00f0062021-02-03 06:01:02 -0700950 dtb_platdata.run_steps(['all'], dtb_file, False, None, [outdir], None,
951 warning_disabled=True, scan=copy_scan())
Simon Glass10cbd3b2020-12-28 20:34:52 -0700952 fnames = glob.glob(outdir + '/*')
953 self.assertEqual(4, len(fnames))
954
955 leafs = set(os.path.basename(fname) for fname in fnames)
956 self.assertEqual(
Simon Glassf31fa992020-12-28 20:35:01 -0700957 {'dt-structs-gen.h', 'source.dts', 'dt-plat.c', 'source.dtb'},
Simon Glass10cbd3b2020-12-28 20:34:52 -0700958 leafs)
Simon Glassfd471e22021-02-03 06:01:00 -0700959
960 def setup_process_test(self):
961 """Set up a test of process_nodes()
962
963 This uses saved_scan but returns a deep copy of it, so it is safe to
964 modify it in these tests
965
966 Returns:
967 tuple:
968 DtbPlatdata: object to test
969 Scanner: scanner to use
970 """
971 dtb_file = get_dtb_file('dtoc_test_simple.dts')
972 output = tools.GetOutputFilename('output')
973
974 # Take a copy before messing with it
975 scan = copy.deepcopy(saved_scan)
976 plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
977 plat.scan_dtb()
978 plat.scan_tree()
979 plat.prepare_nodes()
980 return plat, scan
981
982 def test_process_nodes(self):
983 """Test processing nodes to add various info"""
984 plat, scan = self.setup_process_test()
985 plat.process_nodes(True)
986
987 i2c_node = plat._fdt.GetNode('/i2c@0')
988 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
989 pmic = scan._drivers['sandbox_pmic']
990 i2c = scan._drivers['sandbox_i2c']
991 self.assertEqual('DM_DEVICE_REF(pmic_at_9)', pmic_node.dev_ref)
992 self.assertEqual(pmic, pmic_node.driver)
993 self.assertEqual(i2c_node, pmic_node.parent)
994 self.assertEqual(i2c, pmic_node.parent_driver)
995
996 # The pmic is the only child
997 self.assertEqual(pmic_node.parent_seq, 0)
998 self.assertEqual([pmic_node], i2c_node.child_devs)
999
1000 # Start and end of the list should be the child_head
1001 ref = '&DM_DEVICE_REF(i2c_at_0)->child_head'
1002 self.assertEqual(
1003 {-1: ref, 0: '&DM_DEVICE_REF(pmic_at_9)->sibling_node', 1: ref},
1004 i2c_node.child_refs)
1005
1006 def test_process_nodes_bad_parent(self):
1007 # Pretend that i2c has a parent (the pmic) and delete that driver
1008 plat, scan = self.setup_process_test()
1009
1010 i2c_node = plat._fdt.GetNode('/i2c@0')
1011 pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
1012 del scan._drivers['sandbox_pmic']
1013 i2c_node.parent = pmic_node
1014
1015 # Process twice, the second time to generate an exception
1016 plat.process_nodes(False)
1017 with self.assertRaises(ValueError) as exc:
1018 plat.process_nodes(True)
1019 self.assertIn(
1020 "Cannot parse/find parent driver 'sandbox_pmic' for 'sandbox_i2c",
1021 str(exc.exception))
1022
1023 def test_process_nodes_bad_node(self):
1024 plat, scan = self.setup_process_test()
1025
1026 # Now remove the pmic driver
1027 del scan._drivers['sandbox_pmic']
1028
1029 # Process twice, the second time to generate an exception
1030 plat.process_nodes(False)
1031 with self.assertRaises(ValueError) as exc:
1032 plat.process_nodes(True)
1033 self.assertIn("Cannot parse/find driver for 'sandbox_pmic",
1034 str(exc.exception))
Simon Glassb9319c42021-02-03 06:01:01 -07001035
1036 def test_process_nodes_used(self):
1037 """Test processing nodes to add various info"""
1038 plat, scan = self.setup_process_test()
1039 plat.process_nodes(True)
1040
1041 pmic = scan._drivers['sandbox_pmic']
1042 self.assertTrue(pmic.used)
1043
1044 gpio = scan._drivers['sandbox_gpio']
1045 self.assertFalse(gpio.used)
Simon Glass05953522021-02-03 06:01:07 -07001046
1047 def test_alias_read(self):
1048 """Test obtaining aliases"""
1049 dtb_file = get_dtb_file('dtoc_test_inst.dts')
1050 output = tools.GetOutputFilename('output')
1051 plat = self.run_test(['struct'], dtb_file, output)
1052
1053 scan = plat._scan
1054 testfdt_node = plat._fdt.GetNode('/some-bus/test')
1055 self.assertIn('UCLASS_TEST_FDT', scan._uclass)
1056 uc = scan._uclass['UCLASS_TEST_FDT']
1057 self.assertEqual({1: testfdt_node}, uc.alias_num_to_node)
1058 self.assertEqual({'/some-bus/test': 1}, uc.alias_path_to_num)
1059
1060 # Try adding an alias that doesn't exist
1061 self.assertFalse(scan.add_uclass_alias('fred', 3, None))
1062
1063 # Try adding an alias for a missing node
1064 self.assertIsNone(scan.add_uclass_alias('testfdt', 3, None))
1065
1066 def test_alias_read_bad(self):
1067 """Test invalid alias property name"""
1068 dtb_file = get_dtb_file('dtoc_test_alias_bad.dts')
1069 output = tools.GetOutputFilename('output')
1070 with self.assertRaises(ValueError) as exc:
1071 plat = self.run_test(['struct'], dtb_file, output)
1072 self.assertIn("Cannot decode alias 'i2c4-'", str(exc.exception))
1073
1074 def test_alias_read_bad_path(self):
1075 """Test alias pointing to a non-existent node"""
1076 # This line may produce a warning, so capture it:
1077 # Warning (alias_paths): /aliases:i2c4: aliases property is not a valid
1078 # node (/does/not/exist)
1079 dtb_file = get_dtb_file('dtoc_test_alias_bad_path.dts', True)
1080
1081 output = tools.GetOutputFilename('output')
1082 with self.assertRaises(ValueError) as exc:
1083 plat = self.run_test(['struct'], dtb_file, output)
1084 self.assertIn("Alias 'i2c4' path '/does/not/exist' not found",
1085 str(exc.exception))
1086
1087 def test_alias_read_bad_uclass(self):
1088 """Test alias for a uclass that doesn't exist"""
1089 dtb_file = get_dtb_file('dtoc_test_alias_bad_uc.dts')
1090 output = tools.GetOutputFilename('output')
1091 with test_util.capture_sys_output() as (stdout, _):
1092 plat = self.run_test(['struct'], dtb_file, output)
1093 self.assertEqual("Could not find uclass for alias 'other1'",
1094 stdout.getvalue().strip())
Simon Glass074197a2021-02-03 06:01:09 -07001095
1096 def test_sequence(self):
1097 """Test assignment of sequence numnbers"""
1098 dtb_file = get_dtb_file('dtoc_test_inst.dts')
1099 output = tools.GetOutputFilename('output')
1100 plat = self.run_test(['struct'], dtb_file, output)