blob: 3c8e343b1ff8f71a2b30ffe7175a702d3d555e96 [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
13import os
14import struct
Walter Lozano361e7332020-06-25 01:10:08 -030015import sys
Simon Glassc0791922017-06-18 22:09:06 -060016import unittest
17
Simon Glassbf776672020-04-17 18:09:04 -060018from dtoc import dtb_platdata
Simon Glassc0791922017-06-18 22:09:06 -060019from dtb_platdata import conv_name_to_c
20from dtb_platdata import get_compat_name
21from dtb_platdata import get_value
22from dtb_platdata import tab_to
Simon Glassbf776672020-04-17 18:09:04 -060023from dtoc import fdt
24from dtoc import fdt_util
25from patman import test_util
26from patman import tools
Simon Glassc0791922017-06-18 22:09:06 -060027
28our_path = os.path.dirname(os.path.realpath(__file__))
29
30
Simon Glassaab660f2017-11-12 21:52:17 -070031HEADER = '''/*
32 * DO NOT MODIFY
33 *
34 * This file was generated by dtoc from a .dtb (device tree binary) file.
35 */
36
37#include <stdbool.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090038#include <linux/libfdt.h>'''
Simon Glassaab660f2017-11-12 21:52:17 -070039
40C_HEADER = '''/*
41 * DO NOT MODIFY
42 *
43 * This file was generated by dtoc from a .dtb (device tree binary) file.
44 */
45
46#include <common.h>
47#include <dm.h>
48#include <dt-structs.h>
49'''
50
Walter Lozano51f12632020-06-25 01:10:13 -030051C_EMPTY_POPULATE_PHANDLE_DATA = '''void dm_populate_phandle_data(void) {
52}
53'''
Simon Glassaab660f2017-11-12 21:52:17 -070054
Simon Glassfe57c782018-07-06 10:27:37 -060055
56def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glassc0791922017-06-18 22:09:06 -060057 """Compile a .dts file to a .dtb
58
59 Args:
60 dts_fname: Filename of .dts file in the current directory
Simon Glassfe57c782018-07-06 10:27:37 -060061 capture_stderr: True to capture and discard stderr output
Simon Glassc0791922017-06-18 22:09:06 -060062
63 Returns:
64 Filename of compiled file in output directory
65 """
Simon Glassfe57c782018-07-06 10:27:37 -060066 return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
67 capture_stderr=capture_stderr)
Simon Glassc0791922017-06-18 22:09:06 -060068
69
70class TestDtoc(unittest.TestCase):
71 """Tests for dtoc"""
72 @classmethod
73 def setUpClass(cls):
74 tools.PrepareOutputDir(None)
75
76 @classmethod
77 def tearDownClass(cls):
78 tools._RemoveOutputDir()
79
Simon Glass57f0bc42018-07-06 10:27:25 -060080 def _WritePythonString(self, fname, data):
81 """Write a string with tabs expanded as done in this Python file
82
83 Args:
84 fname: Filename to write to
85 data: Raw string to convert
86 """
87 data = data.replace('\t', '\\t')
88 with open(fname, 'w') as fd:
89 fd.write(data)
90
91 def _CheckStrings(self, expected, actual):
92 """Check that a string matches its expected value
93
94 If the strings do not match, they are written to the /tmp directory in
95 the same Python format as is used here in the test. This allows for
96 easy comparison and update of the tests.
97
98 Args:
99 expected: Expected string
100 actual: Actual string
101 """
102 if expected != actual:
103 self._WritePythonString('/tmp/binman.expected', expected)
104 self._WritePythonString('/tmp/binman.actual', actual)
Simon Glass90a81322019-05-17 22:00:31 -0600105 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glass57f0bc42018-07-06 10:27:25 -0600106 self.assertEquals(expected, actual)
107
Walter Lozano361e7332020-06-25 01:10:08 -0300108
109 def run_test(self, args, dtb_file, output):
110 dtb_platdata.run_steps(args, dtb_file, False, output, True)
111
Simon Glassc0791922017-06-18 22:09:06 -0600112 def test_name(self):
113 """Test conversion of device tree names to C identifiers"""
114 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
115 self.assertEqual('vendor_clock_frequency',
116 conv_name_to_c('vendor,clock-frequency'))
117 self.assertEqual('rockchip_rk3399_sdhci_5_1',
118 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
119
120 def test_tab_to(self):
121 """Test operation of tab_to() function"""
122 self.assertEqual('fred ', tab_to(0, 'fred'))
123 self.assertEqual('fred\t', tab_to(1, 'fred'))
124 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
125 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
126 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
127 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
128
129 def test_get_value(self):
130 """Test operation of get_value() function"""
131 self.assertEqual('0x45',
132 get_value(fdt.TYPE_INT, struct.pack('>I', 0x45)))
133 self.assertEqual('0x45',
134 get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45)))
135 self.assertEqual('0x0',
136 get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45)))
137 self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test'))
138 self.assertEqual('true', get_value(fdt.TYPE_BOOL, None))
139
140 def test_get_compat_name(self):
141 """Test operation of get_compat_name() function"""
142 Prop = collections.namedtuple('Prop', ['value'])
143 Node = collections.namedtuple('Node', ['props'])
144
145 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
146 node = Node({'compatible': prop})
147 self.assertEqual(('rockchip_rk3399_sdhci_5_1', ['arasan_sdhci_5_1']),
148 get_compat_name(node))
149
150 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
151 node = Node({'compatible': prop})
152 self.assertEqual(('rockchip_rk3399_sdhci_5_1', []),
153 get_compat_name(node))
154
155 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
156 node = Node({'compatible': prop})
157 self.assertEqual(('rockchip_rk3399_sdhci_5_1',
158 ['arasan_sdhci_5_1', 'third']),
159 get_compat_name(node))
160
161 def test_empty_file(self):
162 """Test output from a device tree file with no nodes"""
163 dtb_file = get_dtb_file('dtoc_test_empty.dts')
164 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300165 self.run_test(['struct'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600166 with open(output) as infile:
167 lines = infile.read().splitlines()
Simon Glassaab660f2017-11-12 21:52:17 -0700168 self.assertEqual(HEADER.splitlines(), lines)
Simon Glassc0791922017-06-18 22:09:06 -0600169
Walter Lozano361e7332020-06-25 01:10:08 -0300170 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600171 with open(output) as infile:
172 lines = infile.read().splitlines()
Walter Lozano51f12632020-06-25 01:10:13 -0300173 self.assertEqual(C_HEADER.splitlines() + [''] +
174 C_EMPTY_POPULATE_PHANDLE_DATA.splitlines(), lines)
Simon Glassc0791922017-06-18 22:09:06 -0600175
176 def test_simple(self):
177 """Test output from some simple nodes with various types of data"""
178 dtb_file = get_dtb_file('dtoc_test_simple.dts')
179 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300180 self.run_test(['struct'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600181 with open(output) as infile:
182 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600183 self._CheckStrings(HEADER + '''
Simon Glass5ec741f2017-08-29 14:15:51 -0600184struct dtd_sandbox_i2c_test {
185};
186struct dtd_sandbox_pmic_test {
187\tbool\t\tlow_power;
188\tfdt64_t\t\treg[2];
189};
Simon Glassc0791922017-06-18 22:09:06 -0600190struct dtd_sandbox_spl_test {
191\tbool\t\tboolval;
192\tunsigned char\tbytearray[3];
193\tunsigned char\tbyteval;
194\tfdt32_t\t\tintarray[4];
195\tfdt32_t\t\tintval;
196\tunsigned char\tlongbytearray[9];
Simon Glass2a2d91d2018-07-06 10:27:28 -0600197\tunsigned char\tnotstring[5];
Simon Glassc0791922017-06-18 22:09:06 -0600198\tconst char *\tstringarray[3];
199\tconst char *\tstringval;
200};
201struct dtd_sandbox_spl_test_2 {
202};
203''', data)
204
Walter Lozano361e7332020-06-25 01:10:08 -0300205 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600206 with open(output) as infile:
207 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600208 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300209static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass1953ce72019-05-17 22:00:32 -0600210\t.boolval\t\t= true,
Simon Glassc0791922017-06-18 22:09:06 -0600211\t.bytearray\t\t= {0x6, 0x0, 0x0},
212\t.byteval\t\t= 0x5,
Simon Glass1953ce72019-05-17 22:00:32 -0600213\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600214\t.intval\t\t\t= 0x1,
Simon Glass21d54ac2017-08-29 14:15:49 -0600215\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
216\t\t0x11},
Simon Glass1953ce72019-05-17 22:00:32 -0600217\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600218\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glass1953ce72019-05-17 22:00:32 -0600219\t.stringval\t\t= "message",
Simon Glassc0791922017-06-18 22:09:06 -0600220};
221U_BOOT_DEVICE(spl_test) = {
222\t.name\t\t= "sandbox_spl_test",
223\t.platdata\t= &dtv_spl_test,
224\t.platdata_size\t= sizeof(dtv_spl_test),
225};
226
Walter Lozano51f12632020-06-25 01:10:13 -0300227static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glassc0791922017-06-18 22:09:06 -0600228\t.bytearray\t\t= {0x1, 0x23, 0x34},
229\t.byteval\t\t= 0x8,
Simon Glass1953ce72019-05-17 22:00:32 -0600230\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600231\t.intval\t\t\t= 0x3,
Simon Glass21d54ac2017-08-29 14:15:49 -0600232\t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
233\t\t0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600234\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glass1953ce72019-05-17 22:00:32 -0600235\t.stringval\t\t= "message2",
Simon Glassc0791922017-06-18 22:09:06 -0600236};
237U_BOOT_DEVICE(spl_test2) = {
238\t.name\t\t= "sandbox_spl_test",
239\t.platdata\t= &dtv_spl_test2,
240\t.platdata_size\t= sizeof(dtv_spl_test2),
241};
242
Walter Lozano51f12632020-06-25 01:10:13 -0300243static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glassc0791922017-06-18 22:09:06 -0600244\t.stringarray\t\t= {"one", "", ""},
245};
246U_BOOT_DEVICE(spl_test3) = {
247\t.name\t\t= "sandbox_spl_test",
248\t.platdata\t= &dtv_spl_test3,
249\t.platdata_size\t= sizeof(dtv_spl_test3),
250};
251
Walter Lozano51f12632020-06-25 01:10:13 -0300252static struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
Simon Glassc0791922017-06-18 22:09:06 -0600253};
254U_BOOT_DEVICE(spl_test4) = {
255\t.name\t\t= "sandbox_spl_test_2",
256\t.platdata\t= &dtv_spl_test4,
257\t.platdata_size\t= sizeof(dtv_spl_test4),
258};
259
Walter Lozano51f12632020-06-25 01:10:13 -0300260static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
Simon Glass5ec741f2017-08-29 14:15:51 -0600261};
262U_BOOT_DEVICE(i2c_at_0) = {
263\t.name\t\t= "sandbox_i2c_test",
264\t.platdata\t= &dtv_i2c_at_0,
265\t.platdata_size\t= sizeof(dtv_i2c_at_0),
266};
267
Walter Lozano51f12632020-06-25 01:10:13 -0300268static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
Simon Glass5ec741f2017-08-29 14:15:51 -0600269\t.low_power\t\t= true,
270\t.reg\t\t\t= {0x9, 0x0},
271};
272U_BOOT_DEVICE(pmic_at_9) = {
273\t.name\t\t= "sandbox_pmic_test",
274\t.platdata\t= &dtv_pmic_at_9,
275\t.platdata_size\t= sizeof(dtv_pmic_at_9),
276};
277
Walter Lozano51f12632020-06-25 01:10:13 -0300278''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glassc0791922017-06-18 22:09:06 -0600279
Walter Lozanodac82282020-07-03 08:07:17 -0300280 def test_driver_alias(self):
281 """Test output from a device tree file with a driver alias"""
282 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
283 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300284 self.run_test(['struct'], dtb_file, output)
Walter Lozanodac82282020-07-03 08:07:17 -0300285 with open(output) as infile:
286 data = infile.read()
287 self._CheckStrings(HEADER + '''
288struct dtd_sandbox_gpio {
289\tconst char *\tgpio_bank_name;
290\tbool\t\tgpio_controller;
291\tfdt32_t\t\tsandbox_gpio_count;
292};
293#define dtd_sandbox_gpio_alias dtd_sandbox_gpio
294''', data)
295
Walter Lozano361e7332020-06-25 01:10:08 -0300296 self.run_test(['platdata'], dtb_file, output)
Walter Lozanodac82282020-07-03 08:07:17 -0300297 with open(output) as infile:
298 data = infile.read()
299 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300300static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanodac82282020-07-03 08:07:17 -0300301\t.gpio_bank_name\t\t= "a",
302\t.gpio_controller\t= true,
303\t.sandbox_gpio_count\t= 0x14,
304};
305U_BOOT_DEVICE(gpios_at_0) = {
306\t.name\t\t= "sandbox_gpio",
307\t.platdata\t= &dtv_gpios_at_0,
308\t.platdata_size\t= sizeof(dtv_gpios_at_0),
309};
310
Walter Lozano51f12632020-06-25 01:10:13 -0300311void dm_populate_phandle_data(void) {
312}
Walter Lozanodac82282020-07-03 08:07:17 -0300313''', data)
314
Walter Lozano361e7332020-06-25 01:10:08 -0300315 def test_invalid_driver(self):
316 """Test output from a device tree file with an invalid driver"""
317 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
318 output = tools.GetOutputFilename('output')
319 with test_util.capture_sys_output() as (stdout, stderr):
320 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
321 with open(output) as infile:
322 data = infile.read()
323 self._CheckStrings(HEADER + '''
324struct dtd_invalid {
325};
326''', data)
327
328 with test_util.capture_sys_output() as (stdout, stderr):
329 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
330 with open(output) as infile:
331 data = infile.read()
332 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300333static struct dtd_invalid dtv_spl_test = {
Walter Lozano361e7332020-06-25 01:10:08 -0300334};
335U_BOOT_DEVICE(spl_test) = {
336\t.name\t\t= "invalid",
337\t.platdata\t= &dtv_spl_test,
338\t.platdata_size\t= sizeof(dtv_spl_test),
339};
340
Walter Lozano51f12632020-06-25 01:10:13 -0300341void dm_populate_phandle_data(void) {
342}
Walter Lozano361e7332020-06-25 01:10:08 -0300343''', data)
344
Simon Glassc0791922017-06-18 22:09:06 -0600345 def test_phandle(self):
346 """Test output from a node containing a phandle reference"""
347 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
348 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300349 self.run_test(['struct'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600350 with open(output) as infile:
351 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600352 self._CheckStrings(HEADER + '''
Simon Glassc0791922017-06-18 22:09:06 -0600353struct dtd_source {
Simon Glass634eba42017-08-29 14:15:59 -0600354\tstruct phandle_2_arg clocks[4];
Simon Glassc0791922017-06-18 22:09:06 -0600355};
356struct dtd_target {
357\tfdt32_t\t\tintval;
358};
359''', data)
360
Walter Lozano361e7332020-06-25 01:10:08 -0300361 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600362 with open(output) as infile:
363 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600364 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300365static struct dtd_target dtv_phandle_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600366\t.intval\t\t\t= 0x0,
Simon Glassc0791922017-06-18 22:09:06 -0600367};
368U_BOOT_DEVICE(phandle_target) = {
369\t.name\t\t= "target",
370\t.platdata\t= &dtv_phandle_target,
371\t.platdata_size\t= sizeof(dtv_phandle_target),
372};
373
Walter Lozano51f12632020-06-25 01:10:13 -0300374static struct dtd_target dtv_phandle2_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600375\t.intval\t\t\t= 0x1,
376};
377U_BOOT_DEVICE(phandle2_target) = {
378\t.name\t\t= "target",
379\t.platdata\t= &dtv_phandle2_target,
380\t.platdata_size\t= sizeof(dtv_phandle2_target),
381};
382
Walter Lozano51f12632020-06-25 01:10:13 -0300383static struct dtd_target dtv_phandle3_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600384\t.intval\t\t\t= 0x2,
385};
386U_BOOT_DEVICE(phandle3_target) = {
387\t.name\t\t= "target",
388\t.platdata\t= &dtv_phandle3_target,
389\t.platdata_size\t= sizeof(dtv_phandle3_target),
390};
391
Walter Lozano51f12632020-06-25 01:10:13 -0300392static struct dtd_source dtv_phandle_source = {
Simon Glass35d50372017-08-29 14:15:57 -0600393\t.clocks\t\t\t= {
Walter Lozano51f12632020-06-25 01:10:13 -0300394\t\t\t{NULL, {}},
395\t\t\t{NULL, {11}},
396\t\t\t{NULL, {12, 13}},
397\t\t\t{NULL, {}},},
Simon Glassc0791922017-06-18 22:09:06 -0600398};
399U_BOOT_DEVICE(phandle_source) = {
400\t.name\t\t= "source",
401\t.platdata\t= &dtv_phandle_source,
402\t.platdata_size\t= sizeof(dtv_phandle_source),
403};
404
Walter Lozano51f12632020-06-25 01:10:13 -0300405static struct dtd_source dtv_phandle_source2 = {
Simon Glass760b7172018-07-06 10:27:31 -0600406\t.clocks\t\t\t= {
Walter Lozano51f12632020-06-25 01:10:13 -0300407\t\t\t{NULL, {}},},
Simon Glass760b7172018-07-06 10:27:31 -0600408};
409U_BOOT_DEVICE(phandle_source2) = {
410\t.name\t\t= "source",
411\t.platdata\t= &dtv_phandle_source2,
412\t.platdata_size\t= sizeof(dtv_phandle_source2),
413};
414
Walter Lozano51f12632020-06-25 01:10:13 -0300415void dm_populate_phandle_data(void) {
416\tdtv_phandle_source.clocks[0].node = DM_GET_DEVICE(phandle_target);
417\tdtv_phandle_source.clocks[1].node = DM_GET_DEVICE(phandle2_target);
418\tdtv_phandle_source.clocks[2].node = DM_GET_DEVICE(phandle3_target);
419\tdtv_phandle_source.clocks[3].node = DM_GET_DEVICE(phandle_target);
420\tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target);
421}
Simon Glassc0791922017-06-18 22:09:06 -0600422''', data)
423
Simon Glass8512ea22018-07-06 10:27:35 -0600424 def test_phandle_single(self):
425 """Test output from a node containing a phandle reference"""
426 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
427 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300428 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600429 with open(output) as infile:
430 data = infile.read()
431 self._CheckStrings(HEADER + '''
432struct dtd_source {
433\tstruct phandle_0_arg clocks[1];
434};
435struct dtd_target {
436\tfdt32_t\t\tintval;
437};
438''', data)
439
440 def test_phandle_reorder(self):
441 """Test that phandle targets are generated before their references"""
442 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
443 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300444 self.run_test(['platdata'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600445 with open(output) as infile:
446 data = infile.read()
447 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300448static struct dtd_target dtv_phandle_target = {
Simon Glass8512ea22018-07-06 10:27:35 -0600449};
450U_BOOT_DEVICE(phandle_target) = {
451\t.name\t\t= "target",
452\t.platdata\t= &dtv_phandle_target,
453\t.platdata_size\t= sizeof(dtv_phandle_target),
454};
455
Walter Lozano51f12632020-06-25 01:10:13 -0300456static struct dtd_source dtv_phandle_source2 = {
Simon Glass8512ea22018-07-06 10:27:35 -0600457\t.clocks\t\t\t= {
Walter Lozano51f12632020-06-25 01:10:13 -0300458\t\t\t{NULL, {}},},
Simon Glass8512ea22018-07-06 10:27:35 -0600459};
460U_BOOT_DEVICE(phandle_source2) = {
461\t.name\t\t= "source",
462\t.platdata\t= &dtv_phandle_source2,
463\t.platdata_size\t= sizeof(dtv_phandle_source2),
464};
465
Walter Lozano51f12632020-06-25 01:10:13 -0300466void dm_populate_phandle_data(void) {
467\tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target);
468}
Simon Glass8512ea22018-07-06 10:27:35 -0600469''', data)
470
Walter Lozano6c3fc502020-06-25 01:10:17 -0300471 def test_phandle_cd_gpio(self):
472 """Test that phandle targets are generated when unsing cd-gpios"""
473 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
474 output = tools.GetOutputFilename('output')
475 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, True)
476 with open(output) as infile:
477 data = infile.read()
478 self._CheckStrings(C_HEADER + '''
479static struct dtd_target dtv_phandle_target = {
480\t.intval\t\t\t= 0x0,
481};
482U_BOOT_DEVICE(phandle_target) = {
483\t.name\t\t= "target",
484\t.platdata\t= &dtv_phandle_target,
485\t.platdata_size\t= sizeof(dtv_phandle_target),
486};
487
488static struct dtd_target dtv_phandle2_target = {
489\t.intval\t\t\t= 0x1,
490};
491U_BOOT_DEVICE(phandle2_target) = {
492\t.name\t\t= "target",
493\t.platdata\t= &dtv_phandle2_target,
494\t.platdata_size\t= sizeof(dtv_phandle2_target),
495};
496
497static struct dtd_target dtv_phandle3_target = {
498\t.intval\t\t\t= 0x2,
499};
500U_BOOT_DEVICE(phandle3_target) = {
501\t.name\t\t= "target",
502\t.platdata\t= &dtv_phandle3_target,
503\t.platdata_size\t= sizeof(dtv_phandle3_target),
504};
505
506static struct dtd_source dtv_phandle_source = {
507\t.cd_gpios\t\t= {
508\t\t\t{NULL, {}},
509\t\t\t{NULL, {11}},
510\t\t\t{NULL, {12, 13}},
511\t\t\t{NULL, {}},},
512};
513U_BOOT_DEVICE(phandle_source) = {
514\t.name\t\t= "source",
515\t.platdata\t= &dtv_phandle_source,
516\t.platdata_size\t= sizeof(dtv_phandle_source),
517};
518
519static struct dtd_source dtv_phandle_source2 = {
520\t.cd_gpios\t\t= {
521\t\t\t{NULL, {}},},
522};
523U_BOOT_DEVICE(phandle_source2) = {
524\t.name\t\t= "source",
525\t.platdata\t= &dtv_phandle_source2,
526\t.platdata_size\t= sizeof(dtv_phandle_source2),
527};
528
529void dm_populate_phandle_data(void) {
530\tdtv_phandle_source.cd_gpios[0].node = DM_GET_DEVICE(phandle_target);
531\tdtv_phandle_source.cd_gpios[1].node = DM_GET_DEVICE(phandle2_target);
532\tdtv_phandle_source.cd_gpios[2].node = DM_GET_DEVICE(phandle3_target);
533\tdtv_phandle_source.cd_gpios[3].node = DM_GET_DEVICE(phandle_target);
534\tdtv_phandle_source2.cd_gpios[0].node = DM_GET_DEVICE(phandle_target);
535}
536''', data)
537
Simon Glass8512ea22018-07-06 10:27:35 -0600538 def test_phandle_bad(self):
539 """Test a node containing an invalid phandle fails"""
Simon Glass4b4bc062018-10-01 21:12:43 -0600540 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
541 capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600542 output = tools.GetOutputFilename('output')
543 with self.assertRaises(ValueError) as e:
Walter Lozano361e7332020-06-25 01:10:08 -0300544 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600545 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
546 str(e.exception))
547
548 def test_phandle_bad2(self):
549 """Test a phandle target missing its #*-cells property"""
Simon Glass4b4bc062018-10-01 21:12:43 -0600550 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
551 capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600552 output = tools.GetOutputFilename('output')
553 with self.assertRaises(ValueError) as e:
Walter Lozano361e7332020-06-25 01:10:08 -0300554 self.run_test(['struct'], dtb_file, output)
Walter Lozanoad340172020-06-25 01:10:16 -0300555 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glass8512ea22018-07-06 10:27:35 -0600556 str(e.exception))
557
Simon Glassc0791922017-06-18 22:09:06 -0600558 def test_aliases(self):
559 """Test output from a node with multiple compatible strings"""
560 dtb_file = get_dtb_file('dtoc_test_aliases.dts')
561 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300562 self.run_test(['struct'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600563 with open(output) as infile:
564 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600565 self._CheckStrings(HEADER + '''
Simon Glassc0791922017-06-18 22:09:06 -0600566struct dtd_compat1 {
567\tfdt32_t\t\tintval;
568};
569#define dtd_compat2_1_fred dtd_compat1
570#define dtd_compat3 dtd_compat1
571''', data)
572
Walter Lozano361e7332020-06-25 01:10:08 -0300573 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600574 with open(output) as infile:
575 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600576 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300577static struct dtd_compat1 dtv_spl_test = {
Simon Glassc0791922017-06-18 22:09:06 -0600578\t.intval\t\t\t= 0x1,
579};
580U_BOOT_DEVICE(spl_test) = {
581\t.name\t\t= "compat1",
582\t.platdata\t= &dtv_spl_test,
583\t.platdata_size\t= sizeof(dtv_spl_test),
584};
585
Walter Lozano51f12632020-06-25 01:10:13 -0300586''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600587
588 def test_addresses64(self):
589 """Test output from a node with a 'reg' property with na=2, ns=2"""
590 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
591 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300592 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600593 with open(output) as infile:
594 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600595 self._CheckStrings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600596struct dtd_test1 {
597\tfdt64_t\t\treg[2];
598};
599struct dtd_test2 {
600\tfdt64_t\t\treg[2];
601};
602struct dtd_test3 {
603\tfdt64_t\t\treg[4];
604};
605''', data)
606
Walter Lozano361e7332020-06-25 01:10:08 -0300607 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600608 with open(output) as infile:
609 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600610 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300611static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600612\t.reg\t\t\t= {0x1234, 0x5678},
613};
614U_BOOT_DEVICE(test1) = {
615\t.name\t\t= "test1",
616\t.platdata\t= &dtv_test1,
617\t.platdata_size\t= sizeof(dtv_test1),
618};
619
Walter Lozano51f12632020-06-25 01:10:13 -0300620static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600621\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
622};
623U_BOOT_DEVICE(test2) = {
624\t.name\t\t= "test2",
625\t.platdata\t= &dtv_test2,
626\t.platdata_size\t= sizeof(dtv_test2),
627};
628
Walter Lozano51f12632020-06-25 01:10:13 -0300629static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600630\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
631};
632U_BOOT_DEVICE(test3) = {
633\t.name\t\t= "test3",
634\t.platdata\t= &dtv_test3,
635\t.platdata_size\t= sizeof(dtv_test3),
636};
637
Walter Lozano51f12632020-06-25 01:10:13 -0300638''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600639
640 def test_addresses32(self):
641 """Test output from a node with a 'reg' property with na=1, ns=1"""
642 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
643 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300644 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600645 with open(output) as infile:
646 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600647 self._CheckStrings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600648struct dtd_test1 {
649\tfdt32_t\t\treg[2];
650};
651struct dtd_test2 {
652\tfdt32_t\t\treg[4];
653};
654''', data)
655
Walter Lozano361e7332020-06-25 01:10:08 -0300656 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600657 with open(output) as infile:
658 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600659 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300660static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600661\t.reg\t\t\t= {0x1234, 0x5678},
662};
663U_BOOT_DEVICE(test1) = {
664\t.name\t\t= "test1",
665\t.platdata\t= &dtv_test1,
666\t.platdata_size\t= sizeof(dtv_test1),
667};
668
Walter Lozano51f12632020-06-25 01:10:13 -0300669static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600670\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
671};
672U_BOOT_DEVICE(test2) = {
673\t.name\t\t= "test2",
674\t.platdata\t= &dtv_test2,
675\t.platdata_size\t= sizeof(dtv_test2),
676};
677
Walter Lozano51f12632020-06-25 01:10:13 -0300678''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600679
680 def test_addresses64_32(self):
681 """Test output from a node with a 'reg' property with na=2, ns=1"""
682 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
683 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300684 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600685 with open(output) as infile:
686 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600687 self._CheckStrings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600688struct dtd_test1 {
689\tfdt64_t\t\treg[2];
690};
691struct dtd_test2 {
692\tfdt64_t\t\treg[2];
693};
694struct dtd_test3 {
695\tfdt64_t\t\treg[4];
696};
697''', data)
698
Walter Lozano361e7332020-06-25 01:10:08 -0300699 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600700 with open(output) as infile:
701 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600702 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300703static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600704\t.reg\t\t\t= {0x123400000000, 0x5678},
705};
706U_BOOT_DEVICE(test1) = {
707\t.name\t\t= "test1",
708\t.platdata\t= &dtv_test1,
709\t.platdata_size\t= sizeof(dtv_test1),
710};
711
Walter Lozano51f12632020-06-25 01:10:13 -0300712static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600713\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
714};
715U_BOOT_DEVICE(test2) = {
716\t.name\t\t= "test2",
717\t.platdata\t= &dtv_test2,
718\t.platdata_size\t= sizeof(dtv_test2),
719};
720
Walter Lozano51f12632020-06-25 01:10:13 -0300721static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600722\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
723};
724U_BOOT_DEVICE(test3) = {
725\t.name\t\t= "test3",
726\t.platdata\t= &dtv_test3,
727\t.platdata_size\t= sizeof(dtv_test3),
728};
729
Walter Lozano51f12632020-06-25 01:10:13 -0300730''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600731
732 def test_addresses32_64(self):
733 """Test output from a node with a 'reg' property with na=1, ns=2"""
734 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
735 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300736 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600737 with open(output) as infile:
738 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600739 self._CheckStrings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600740struct dtd_test1 {
741\tfdt64_t\t\treg[2];
742};
743struct dtd_test2 {
744\tfdt64_t\t\treg[2];
745};
746struct dtd_test3 {
747\tfdt64_t\t\treg[4];
748};
749''', data)
750
Walter Lozano361e7332020-06-25 01:10:08 -0300751 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600752 with open(output) as infile:
753 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600754 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300755static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600756\t.reg\t\t\t= {0x1234, 0x567800000000},
757};
758U_BOOT_DEVICE(test1) = {
759\t.name\t\t= "test1",
760\t.platdata\t= &dtv_test1,
761\t.platdata_size\t= sizeof(dtv_test1),
762};
763
Walter Lozano51f12632020-06-25 01:10:13 -0300764static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600765\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
766};
767U_BOOT_DEVICE(test2) = {
768\t.name\t\t= "test2",
769\t.platdata\t= &dtv_test2,
770\t.platdata_size\t= sizeof(dtv_test2),
771};
772
Walter Lozano51f12632020-06-25 01:10:13 -0300773static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600774\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
775};
776U_BOOT_DEVICE(test3) = {
777\t.name\t\t= "test3",
778\t.platdata\t= &dtv_test3,
779\t.platdata_size\t= sizeof(dtv_test3),
780};
781
Walter Lozano51f12632020-06-25 01:10:13 -0300782''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass8512ea22018-07-06 10:27:35 -0600783
784 def test_bad_reg(self):
785 """Test that a reg property with an invalid type generates an error"""
Simon Glassfe57c782018-07-06 10:27:37 -0600786 # Capture stderr since dtc will emit warnings for this file
787 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600788 output = tools.GetOutputFilename('output')
789 with self.assertRaises(ValueError) as e:
Walter Lozano361e7332020-06-25 01:10:08 -0300790 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600791 self.assertIn("Node 'spl-test' reg property is not an int",
792 str(e.exception))
793
794 def test_bad_reg2(self):
795 """Test that a reg property with an invalid cell count is detected"""
Simon Glassfe57c782018-07-06 10:27:37 -0600796 # Capture stderr since dtc will emit warnings for this file
797 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600798 output = tools.GetOutputFilename('output')
799 with self.assertRaises(ValueError) as e:
Walter Lozano361e7332020-06-25 01:10:08 -0300800 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600801 self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
802 str(e.exception))
803
804 def test_add_prop(self):
805 """Test that a subequent node can add a new property to a struct"""
806 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
807 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300808 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600809 with open(output) as infile:
810 data = infile.read()
811 self._CheckStrings(HEADER + '''
812struct dtd_sandbox_spl_test {
813\tfdt32_t\t\tintarray;
814\tfdt32_t\t\tintval;
815};
816''', data)
817
Walter Lozano361e7332020-06-25 01:10:08 -0300818 self.run_test(['platdata'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600819 with open(output) as infile:
820 data = infile.read()
821 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300822static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass8512ea22018-07-06 10:27:35 -0600823\t.intval\t\t\t= 0x1,
824};
825U_BOOT_DEVICE(spl_test) = {
826\t.name\t\t= "sandbox_spl_test",
827\t.platdata\t= &dtv_spl_test,
828\t.platdata_size\t= sizeof(dtv_spl_test),
829};
830
Walter Lozano51f12632020-06-25 01:10:13 -0300831static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass8512ea22018-07-06 10:27:35 -0600832\t.intarray\t\t= 0x5,
833};
834U_BOOT_DEVICE(spl_test2) = {
835\t.name\t\t= "sandbox_spl_test",
836\t.platdata\t= &dtv_spl_test2,
837\t.platdata_size\t= sizeof(dtv_spl_test2),
838};
839
Walter Lozano51f12632020-06-25 01:10:13 -0300840''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass8512ea22018-07-06 10:27:35 -0600841
842 def testStdout(self):
843 """Test output to stdout"""
844 dtb_file = get_dtb_file('dtoc_test_simple.dts')
845 with test_util.capture_sys_output() as (stdout, stderr):
Walter Lozano361e7332020-06-25 01:10:08 -0300846 self.run_test(['struct'], dtb_file, '-')
Simon Glass8512ea22018-07-06 10:27:35 -0600847
848 def testNoCommand(self):
849 """Test running dtoc without a command"""
850 with self.assertRaises(ValueError) as e:
Walter Lozano361e7332020-06-25 01:10:08 -0300851 self.run_test([], '', '')
Simon Glass8512ea22018-07-06 10:27:35 -0600852 self.assertIn("Please specify a command: struct, platdata",
853 str(e.exception))
854
855 def testBadCommand(self):
856 """Test running dtoc with an invalid command"""
857 dtb_file = get_dtb_file('dtoc_test_simple.dts')
858 output = tools.GetOutputFilename('output')
859 with self.assertRaises(ValueError) as e:
Walter Lozano361e7332020-06-25 01:10:08 -0300860 self.run_test(['invalid-cmd'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600861 self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",
862 str(e.exception))