test_dtoc.py 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0+
  3. # Copyright (c) 2012 The Chromium OS Authors.
  4. #
  5. """Tests for the dtb_platdata module
  6. This includes unit tests for some functions and functional tests for the dtoc
  7. tool.
  8. """
  9. import collections
  10. import copy
  11. import glob
  12. import os
  13. import struct
  14. import unittest
  15. from dtb_platdata import Ftype
  16. from dtb_platdata import get_value
  17. from dtb_platdata import tab_to
  18. from dtoc import dtb_platdata
  19. from dtoc import fdt
  20. from dtoc import fdt_util
  21. from dtoc import src_scan
  22. from dtoc.src_scan import conv_name_to_c
  23. from dtoc.src_scan import get_compat_name
  24. from patman import test_util
  25. from patman import tools
  26. OUR_PATH = os.path.dirname(os.path.realpath(__file__))
  27. HEADER = '''/*
  28. * DO NOT MODIFY
  29. *
  30. * Defines the structs used to hold devicetree data.
  31. * This was generated by dtoc from a .dtb (device tree binary) file.
  32. */
  33. #include <stdbool.h>
  34. #include <linux/libfdt.h>'''
  35. DECL_HEADER = '''/*
  36. * DO NOT MODIFY
  37. *
  38. * Declares externs for all device/uclass instances.
  39. * This was generated by dtoc from a .dtb (device tree binary) file.
  40. */
  41. '''
  42. C_HEADER_PRE = '''/*
  43. * DO NOT MODIFY
  44. *
  45. * Declares the U_BOOT_DRIVER() records and platform data.
  46. * This was generated by dtoc from a .dtb (device tree binary) file.
  47. */
  48. '''
  49. C_HEADER = C_HEADER_PRE + '''
  50. /* Allow use of U_BOOT_DRVINFO() in this file */
  51. #define DT_PLAT_C
  52. #include <common.h>
  53. #include <dm.h>
  54. #include <dt-structs.h>
  55. '''
  56. UCLASS_HEADER_COMMON = '''/*
  57. * DO NOT MODIFY
  58. *
  59. * Declares the uclass instances (struct uclass).
  60. * This was generated by dtoc from a .dtb (device tree binary) file.
  61. */
  62. '''
  63. # Scanner saved from a previous run of the tests (to speed things up)
  64. saved_scan = None
  65. # This is a test so is allowed to access private things in the module it is
  66. # testing
  67. # pylint: disable=W0212
  68. def get_dtb_file(dts_fname, capture_stderr=False):
  69. """Compile a .dts file to a .dtb
  70. Args:
  71. dts_fname (str): Filename of .dts file in the current directory
  72. capture_stderr (bool): True to capture and discard stderr output
  73. Returns:
  74. str: Filename of compiled file in output directory
  75. """
  76. return fdt_util.EnsureCompiled(os.path.join(OUR_PATH, 'test', dts_fname),
  77. capture_stderr=capture_stderr)
  78. def setup():
  79. global saved_scan
  80. # Disable warnings so that calls to get_normalized_compat_name() will not
  81. # output things.
  82. saved_scan = src_scan.Scanner(None, False)
  83. saved_scan.scan_drivers()
  84. def copy_scan():
  85. """Get a copy of saved_scan so that each test can start clean"""
  86. return copy.deepcopy(saved_scan)
  87. class TestDtoc(unittest.TestCase):
  88. """Tests for dtoc"""
  89. @classmethod
  90. def setUpClass(cls):
  91. tools.PrepareOutputDir(None)
  92. cls.maxDiff = None
  93. @classmethod
  94. def tearDownClass(cls):
  95. tools.FinaliseOutputDir()
  96. @staticmethod
  97. def _write_python_string(fname, data):
  98. """Write a string with tabs expanded as done in this Python file
  99. Args:
  100. fname (str): Filename to write to
  101. data (str): Raw string to convert
  102. """
  103. data = data.replace('\t', '\\t')
  104. with open(fname, 'w') as fout:
  105. fout.write(data)
  106. def _check_strings(self, expected, actual):
  107. """Check that a string matches its expected value
  108. If the strings do not match, they are written to the /tmp directory in
  109. the same Python format as is used here in the test. This allows for
  110. easy comparison and update of the tests.
  111. Args:
  112. expected (str): Expected string
  113. actual (str): Actual string
  114. """
  115. if expected != actual:
  116. self._write_python_string('/tmp/binman.expected', expected)
  117. self._write_python_string('/tmp/binman.actual', actual)
  118. print('Failures written to /tmp/binman.{expected,actual}')
  119. self.assertEqual(expected, actual)
  120. @staticmethod
  121. def run_test(args, dtb_file, output, instantiate=False):
  122. """Run a test using dtoc
  123. Args:
  124. args (list of str): List of arguments for dtoc
  125. dtb_file (str): Filename of .dtb file
  126. output (str): Filename of output file
  127. Returns:
  128. DtbPlatdata object
  129. """
  130. # Make a copy of the 'scan' object, since it includes uclasses and
  131. # drivers, which get updated during execution.
  132. return dtb_platdata.run_steps(
  133. args, dtb_file, False, output, [], None, instantiate,
  134. warning_disabled=True, scan=copy_scan())
  135. def test_name(self):
  136. """Test conversion of device tree names to C identifiers"""
  137. self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
  138. self.assertEqual('vendor_clock_frequency',
  139. conv_name_to_c('vendor,clock-frequency'))
  140. self.assertEqual('rockchip_rk3399_sdhci_5_1',
  141. conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
  142. def test_tab_to(self):
  143. """Test operation of tab_to() function"""
  144. self.assertEqual('fred ', tab_to(0, 'fred'))
  145. self.assertEqual('fred\t', tab_to(1, 'fred'))
  146. self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
  147. self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
  148. self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
  149. self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
  150. def test_get_value(self):
  151. """Test operation of get_value() function"""
  152. self.assertEqual('0x45',
  153. get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
  154. self.assertEqual('0x45',
  155. get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
  156. self.assertEqual('0x0',
  157. get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
  158. self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
  159. self.assertEqual('true', get_value(fdt.Type.BOOL, None))
  160. def test_get_compat_name(self):
  161. """Test operation of get_compat_name() function"""
  162. Prop = collections.namedtuple('Prop', ['value'])
  163. Node = collections.namedtuple('Node', ['props'])
  164. prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
  165. node = Node({'compatible': prop})
  166. self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
  167. get_compat_name(node))
  168. prop = Prop(['rockchip,rk3399-sdhci-5.1'])
  169. node = Node({'compatible': prop})
  170. self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
  171. get_compat_name(node))
  172. prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
  173. node = Node({'compatible': prop})
  174. self.assertEqual((['rockchip_rk3399_sdhci_5_1',
  175. 'arasan_sdhci_5_1', 'third']),
  176. get_compat_name(node))
  177. def test_empty_file(self):
  178. """Test output from a device tree file with no nodes"""
  179. dtb_file = get_dtb_file('dtoc_test_empty.dts')
  180. output = tools.GetOutputFilename('output')
  181. # Run this one without saved_scan to complete test coverage
  182. dtb_platdata.run_steps(['struct'], dtb_file, False, output, [], None,
  183. False)
  184. with open(output) as infile:
  185. lines = infile.read().splitlines()
  186. self.assertEqual(HEADER.splitlines(), lines)
  187. self.run_test(['platdata'], dtb_file, output)
  188. with open(output) as infile:
  189. lines = infile.read().splitlines()
  190. self.assertEqual(C_HEADER.splitlines() + [''], lines)
  191. decl_text = DECL_HEADER + '''
  192. #include <dm/device-internal.h>
  193. #include <dm/uclass-internal.h>
  194. /* driver declarations - these allow DM_DRIVER_GET() to be used */
  195. extern U_BOOT_DRIVER(sandbox_i2c);
  196. extern U_BOOT_DRIVER(sandbox_pmic);
  197. extern U_BOOT_DRIVER(sandbox_spl_test);
  198. extern U_BOOT_DRIVER(sandbox_spl_test);
  199. extern U_BOOT_DRIVER(sandbox_spl_test);
  200. /* uclass driver declarations - needed for DM_UCLASS_DRIVER_REF() */
  201. extern UCLASS_DRIVER(i2c);
  202. extern UCLASS_DRIVER(misc);
  203. extern UCLASS_DRIVER(pmic);
  204. '''
  205. decl_text_inst = DECL_HEADER + '''
  206. #include <dm/device-internal.h>
  207. #include <dm/uclass-internal.h>
  208. /* driver declarations - these allow DM_DRIVER_GET() to be used */
  209. extern U_BOOT_DRIVER(sandbox_i2c);
  210. extern U_BOOT_DRIVER(root_driver);
  211. extern U_BOOT_DRIVER(denx_u_boot_test_bus);
  212. extern U_BOOT_DRIVER(sandbox_spl_test);
  213. extern U_BOOT_DRIVER(sandbox_spl_test);
  214. extern U_BOOT_DRIVER(denx_u_boot_fdt_test);
  215. extern U_BOOT_DRIVER(denx_u_boot_fdt_test);
  216. /* device declarations - these allow DM_DEVICE_REF() to be used */
  217. extern DM_DEVICE_INST(i2c);
  218. extern DM_DEVICE_INST(root);
  219. extern DM_DEVICE_INST(some_bus);
  220. extern DM_DEVICE_INST(spl_test);
  221. extern DM_DEVICE_INST(spl_test3);
  222. extern DM_DEVICE_INST(test);
  223. extern DM_DEVICE_INST(test0);
  224. /* uclass driver declarations - needed for DM_UCLASS_DRIVER_REF() */
  225. extern UCLASS_DRIVER(i2c);
  226. extern UCLASS_DRIVER(misc);
  227. extern UCLASS_DRIVER(root);
  228. extern UCLASS_DRIVER(testbus);
  229. extern UCLASS_DRIVER(testfdt);
  230. /* uclass declarations - needed for DM_UCLASS_REF() */
  231. extern DM_UCLASS_INST(i2c);
  232. extern DM_UCLASS_INST(misc);
  233. extern DM_UCLASS_INST(root);
  234. extern DM_UCLASS_INST(testbus);
  235. extern DM_UCLASS_INST(testfdt);
  236. '''
  237. struct_text = HEADER + '''
  238. struct dtd_sandbox_i2c {
  239. };
  240. struct dtd_sandbox_pmic {
  241. \tbool\t\tlow_power;
  242. \tfdt32_t\t\treg[1];
  243. };
  244. struct dtd_sandbox_spl_test {
  245. \tconst char * acpi_name;
  246. \tbool\t\tboolval;
  247. \tunsigned char\tbytearray[3];
  248. \tunsigned char\tbyteval;
  249. \tfdt32_t\t\tintarray[3];
  250. \tfdt32_t\t\tintval;
  251. \tunsigned char\tlongbytearray[9];
  252. \tfdt32_t\t\tmaybe_empty_int[1];
  253. \tunsigned char\tnotstring[5];
  254. \tconst char *\tstringarray[3];
  255. \tconst char *\tstringval;
  256. };
  257. '''
  258. platdata_text = C_HEADER + '''
  259. /*
  260. * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
  261. *
  262. * idx driver_info driver
  263. * --- -------------------- --------------------
  264. * 0: i2c_at_0 sandbox_i2c
  265. * 1: pmic_at_9 sandbox_pmic
  266. * 2: spl_test sandbox_spl_test
  267. * 3: spl_test2 sandbox_spl_test
  268. * 4: spl_test3 sandbox_spl_test
  269. * --- -------------------- --------------------
  270. */
  271. /*
  272. * Node /i2c@0 index 0
  273. * driver sandbox_i2c parent None
  274. */
  275. static struct dtd_sandbox_i2c dtv_i2c_at_0 = {
  276. };
  277. U_BOOT_DRVINFO(i2c_at_0) = {
  278. \t.name\t\t= "sandbox_i2c",
  279. \t.plat\t\t= &dtv_i2c_at_0,
  280. \t.plat_size\t= sizeof(dtv_i2c_at_0),
  281. \t.parent_idx\t= -1,
  282. };
  283. /*
  284. * Node /i2c@0/pmic@9 index 1
  285. * driver sandbox_pmic parent sandbox_i2c
  286. */
  287. static struct dtd_sandbox_pmic dtv_pmic_at_9 = {
  288. \t.low_power\t\t= true,
  289. \t.reg\t\t\t= {0x9},
  290. };
  291. U_BOOT_DRVINFO(pmic_at_9) = {
  292. \t.name\t\t= "sandbox_pmic",
  293. \t.plat\t\t= &dtv_pmic_at_9,
  294. \t.plat_size\t= sizeof(dtv_pmic_at_9),
  295. \t.parent_idx\t= 0,
  296. };
  297. /*
  298. * Node /spl-test index 2
  299. * driver sandbox_spl_test parent None
  300. */
  301. static struct dtd_sandbox_spl_test dtv_spl_test = {
  302. \t.boolval\t\t= true,
  303. \t.bytearray\t\t= {0x6, 0x0, 0x0},
  304. \t.byteval\t\t= 0x5,
  305. \t.intarray\t\t= {0x2, 0x3, 0x4},
  306. \t.intval\t\t\t= 0x1,
  307. \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
  308. \t\t0x11},
  309. \t.maybe_empty_int\t= {0x0},
  310. \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
  311. \t.stringarray\t\t= {"multi-word", "message", ""},
  312. \t.stringval\t\t= "message",
  313. };
  314. U_BOOT_DRVINFO(spl_test) = {
  315. \t.name\t\t= "sandbox_spl_test",
  316. \t.plat\t\t= &dtv_spl_test,
  317. \t.plat_size\t= sizeof(dtv_spl_test),
  318. \t.parent_idx\t= -1,
  319. };
  320. /*
  321. * Node /spl-test2 index 3
  322. * driver sandbox_spl_test parent None
  323. */
  324. static struct dtd_sandbox_spl_test dtv_spl_test2 = {
  325. \t.acpi_name\t\t= "\\\\_SB.GPO0",
  326. \t.bytearray\t\t= {0x1, 0x23, 0x34},
  327. \t.byteval\t\t= 0x8,
  328. \t.intarray\t\t= {0x5, 0x0, 0x0},
  329. \t.intval\t\t\t= 0x3,
  330. \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
  331. \t\t0x0},
  332. \t.stringarray\t\t= {"another", "multi-word", "message"},
  333. \t.stringval\t\t= "message2",
  334. };
  335. U_BOOT_DRVINFO(spl_test2) = {
  336. \t.name\t\t= "sandbox_spl_test",
  337. \t.plat\t\t= &dtv_spl_test2,
  338. \t.plat_size\t= sizeof(dtv_spl_test2),
  339. \t.parent_idx\t= -1,
  340. };
  341. /*
  342. * Node /spl-test3 index 4
  343. * driver sandbox_spl_test parent None
  344. */
  345. static struct dtd_sandbox_spl_test dtv_spl_test3 = {
  346. \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
  347. \t\t0x0},
  348. \t.maybe_empty_int\t= {0x1},
  349. \t.stringarray\t\t= {"one", "", ""},
  350. };
  351. U_BOOT_DRVINFO(spl_test3) = {
  352. \t.name\t\t= "sandbox_spl_test",
  353. \t.plat\t\t= &dtv_spl_test3,
  354. \t.plat_size\t= sizeof(dtv_spl_test3),
  355. \t.parent_idx\t= -1,
  356. };
  357. '''
  358. uclass_text_inst = '''
  359. #include <common.h>
  360. #include <dm.h>
  361. #include <dt-structs.h>
  362. /*
  363. * uclass declarations, ordered by 'struct uclass' linker_list idx:
  364. * 0: i2c
  365. * 1: misc
  366. * 2: root
  367. * 3: testbus
  368. * 4: testfdt
  369. *
  370. * Sequence numbers allocated in each uclass:
  371. * i2c: UCLASS_I2C
  372. * 4: /i2c
  373. * misc: UCLASS_MISC
  374. * 0: /spl-test
  375. * 1: /spl-test3
  376. * root: UCLASS_ROOT
  377. * 0: /
  378. * testbus: UCLASS_TEST_BUS
  379. * 2: /some-bus
  380. * testfdt: UCLASS_TEST_FDT
  381. * 1: /some-bus/test
  382. * 2: /some-bus/test0
  383. */
  384. struct list_head uclass_head = {
  385. .prev = &DM_UCLASS_REF(testfdt)->sibling_node,
  386. .next = &DM_UCLASS_REF(i2c)->sibling_node,
  387. };
  388. DM_UCLASS_INST(i2c) = {
  389. .uc_drv = DM_UCLASS_DRIVER_REF(i2c),
  390. .sibling_node = {
  391. .prev = &uclass_head,
  392. .next = &DM_UCLASS_REF(misc)->sibling_node,
  393. },
  394. .dev_head = {
  395. .prev = &DM_DEVICE_REF(i2c)->uclass_node,
  396. .next = &DM_DEVICE_REF(i2c)->uclass_node,
  397. },
  398. };
  399. DM_UCLASS_INST(misc) = {
  400. .uc_drv = DM_UCLASS_DRIVER_REF(misc),
  401. .sibling_node = {
  402. .prev = &DM_UCLASS_REF(i2c)->sibling_node,
  403. .next = &DM_UCLASS_REF(root)->sibling_node,
  404. },
  405. .dev_head = {
  406. .prev = &DM_DEVICE_REF(spl_test3)->uclass_node,
  407. .next = &DM_DEVICE_REF(spl_test)->uclass_node,
  408. },
  409. };
  410. DM_UCLASS_INST(root) = {
  411. .uc_drv = DM_UCLASS_DRIVER_REF(root),
  412. .sibling_node = {
  413. .prev = &DM_UCLASS_REF(misc)->sibling_node,
  414. .next = &DM_UCLASS_REF(testbus)->sibling_node,
  415. },
  416. .dev_head = {
  417. .prev = &DM_DEVICE_REF(root)->uclass_node,
  418. .next = &DM_DEVICE_REF(root)->uclass_node,
  419. },
  420. };
  421. DM_UCLASS_INST(testbus) = {
  422. .uc_drv = DM_UCLASS_DRIVER_REF(testbus),
  423. .sibling_node = {
  424. .prev = &DM_UCLASS_REF(root)->sibling_node,
  425. .next = &DM_UCLASS_REF(testfdt)->sibling_node,
  426. },
  427. .dev_head = {
  428. .prev = &DM_DEVICE_REF(some_bus)->uclass_node,
  429. .next = &DM_DEVICE_REF(some_bus)->uclass_node,
  430. },
  431. };
  432. #include <dm/test.h>
  433. u8 _testfdt_priv_[sizeof(struct dm_test_uc_priv)]
  434. __attribute__ ((section (".priv_data")));
  435. DM_UCLASS_INST(testfdt) = {
  436. .priv_ = _testfdt_priv_,
  437. .uc_drv = DM_UCLASS_DRIVER_REF(testfdt),
  438. .sibling_node = {
  439. .prev = &DM_UCLASS_REF(testbus)->sibling_node,
  440. .next = &uclass_head,
  441. },
  442. .dev_head = {
  443. .prev = &DM_DEVICE_REF(test0)->uclass_node,
  444. .next = &DM_DEVICE_REF(test)->uclass_node,
  445. },
  446. };
  447. '''
  448. device_text_inst = '''/*
  449. * DO NOT MODIFY
  450. *
  451. * Declares the DM_DEVICE_INST() records.
  452. * This was generated by dtoc from a .dtb (device tree binary) file.
  453. */
  454. #include <common.h>
  455. #include <dm.h>
  456. #include <dt-structs.h>
  457. /*
  458. * udevice declarations, ordered by 'struct udevice' linker_list position:
  459. *
  460. * idx udevice driver
  461. * --- -------------------- --------------------
  462. * 0: i2c sandbox_i2c
  463. * 1: root root_driver
  464. * 2: some_bus denx_u_boot_test_bus
  465. * 3: spl_test sandbox_spl_test
  466. * 4: spl_test3 sandbox_spl_test
  467. * 5: test denx_u_boot_fdt_test
  468. * 6: test0 denx_u_boot_fdt_test
  469. * --- -------------------- --------------------
  470. */
  471. /*
  472. * Node /i2c index 0
  473. * driver sandbox_i2c parent root_driver
  474. */
  475. static struct dtd_sandbox_i2c dtv_i2c = {
  476. \t.intval\t\t\t= 0x3,
  477. };
  478. #include <asm/i2c.h>
  479. u8 _sandbox_i2c_priv_i2c[sizeof(struct sandbox_i2c_priv)]
  480. \t__attribute__ ((section (".priv_data")));
  481. #include <i2c.h>
  482. u8 _sandbox_i2c_uc_priv_i2c[sizeof(struct dm_i2c_bus)]
  483. \t__attribute__ ((section (".priv_data")));
  484. DM_DEVICE_INST(i2c) = {
  485. \t.driver\t\t= DM_DRIVER_REF(sandbox_i2c),
  486. \t.name\t\t= "sandbox_i2c",
  487. \t.plat_\t\t= &dtv_i2c,
  488. \t.priv_\t\t= _sandbox_i2c_priv_i2c,
  489. \t.uclass\t\t= DM_UCLASS_REF(i2c),
  490. \t.uclass_priv_ = _sandbox_i2c_uc_priv_i2c,
  491. \t.uclass_node\t= {
  492. \t\t.prev = &DM_UCLASS_REF(i2c)->dev_head,
  493. \t\t.next = &DM_UCLASS_REF(i2c)->dev_head,
  494. \t},
  495. \t.child_head\t= {
  496. \t\t.prev = &DM_DEVICE_REF(i2c)->child_head,
  497. \t\t.next = &DM_DEVICE_REF(i2c)->child_head,
  498. \t},
  499. \t.sibling_node\t= {
  500. \t\t.prev = &DM_DEVICE_REF(root)->child_head,
  501. \t\t.next = &DM_DEVICE_REF(some_bus)->sibling_node,
  502. \t},
  503. \t.seq_ = 4,
  504. };
  505. /*
  506. * Node / index 1
  507. * driver root_driver parent None
  508. */
  509. static struct dtd_root_driver dtv_root = {
  510. };
  511. DM_DEVICE_INST(root) = {
  512. \t.driver\t\t= DM_DRIVER_REF(root_driver),
  513. \t.name\t\t= "root_driver",
  514. \t.plat_\t\t= &dtv_root,
  515. \t.uclass\t\t= DM_UCLASS_REF(root),
  516. \t.uclass_node\t= {
  517. \t\t.prev = &DM_UCLASS_REF(root)->dev_head,
  518. \t\t.next = &DM_UCLASS_REF(root)->dev_head,
  519. \t},
  520. \t.child_head\t= {
  521. \t\t.prev = &DM_DEVICE_REF(spl_test3)->sibling_node,
  522. \t\t.next = &DM_DEVICE_REF(i2c)->sibling_node,
  523. \t},
  524. \t.seq_ = 0,
  525. };
  526. /*
  527. * Node /some-bus index 2
  528. * driver denx_u_boot_test_bus parent root_driver
  529. */
  530. #include <dm/test.h>
  531. struct dm_test_pdata __attribute__ ((section (".priv_data")))
  532. \t_denx_u_boot_test_bus_plat_some_bus = {
  533. \t.dtplat = {
  534. \t\t.ping_add\t\t= 0x4,
  535. \t\t.ping_expect\t\t= 0x4,
  536. \t\t.reg\t\t\t= {0x3, 0x1},
  537. \t},
  538. };
  539. #include <dm/test.h>
  540. u8 _denx_u_boot_test_bus_priv_some_bus[sizeof(struct dm_test_priv)]
  541. \t__attribute__ ((section (".priv_data")));
  542. #include <dm/test.h>
  543. u8 _denx_u_boot_test_bus_ucplat_some_bus[sizeof(struct dm_test_uclass_priv)]
  544. \t__attribute__ ((section (".priv_data")));
  545. #include <test.h>
  546. DM_DEVICE_INST(some_bus) = {
  547. \t.driver\t\t= DM_DRIVER_REF(denx_u_boot_test_bus),
  548. \t.name\t\t= "denx_u_boot_test_bus",
  549. \t.plat_\t\t= &_denx_u_boot_test_bus_plat_some_bus,
  550. \t.uclass_plat_\t= _denx_u_boot_test_bus_ucplat_some_bus,
  551. \t.driver_data\t= DM_TEST_TYPE_FIRST,
  552. \t.priv_\t\t= _denx_u_boot_test_bus_priv_some_bus,
  553. \t.uclass\t\t= DM_UCLASS_REF(testbus),
  554. \t.uclass_node\t= {
  555. \t\t.prev = &DM_UCLASS_REF(testbus)->dev_head,
  556. \t\t.next = &DM_UCLASS_REF(testbus)->dev_head,
  557. \t},
  558. \t.child_head\t= {
  559. \t\t.prev = &DM_DEVICE_REF(test0)->sibling_node,
  560. \t\t.next = &DM_DEVICE_REF(test)->sibling_node,
  561. \t},
  562. \t.sibling_node\t= {
  563. \t\t.prev = &DM_DEVICE_REF(i2c)->sibling_node,
  564. \t\t.next = &DM_DEVICE_REF(spl_test)->sibling_node,
  565. \t},
  566. \t.seq_ = 2,
  567. };
  568. /*
  569. * Node /spl-test index 3
  570. * driver sandbox_spl_test parent root_driver
  571. */
  572. static struct dtd_sandbox_spl_test dtv_spl_test = {
  573. \t.boolval\t\t= true,
  574. \t.intval\t\t\t= 0x1,
  575. };
  576. DM_DEVICE_INST(spl_test) = {
  577. \t.driver\t\t= DM_DRIVER_REF(sandbox_spl_test),
  578. \t.name\t\t= "sandbox_spl_test",
  579. \t.plat_\t\t= &dtv_spl_test,
  580. \t.uclass\t\t= DM_UCLASS_REF(misc),
  581. \t.uclass_node\t= {
  582. \t\t.prev = &DM_UCLASS_REF(misc)->dev_head,
  583. \t\t.next = &DM_DEVICE_REF(spl_test3)->uclass_node,
  584. \t},
  585. \t.child_head\t= {
  586. \t\t.prev = &DM_DEVICE_REF(spl_test)->child_head,
  587. \t\t.next = &DM_DEVICE_REF(spl_test)->child_head,
  588. \t},
  589. \t.sibling_node\t= {
  590. \t\t.prev = &DM_DEVICE_REF(some_bus)->sibling_node,
  591. \t\t.next = &DM_DEVICE_REF(spl_test3)->sibling_node,
  592. \t},
  593. \t.seq_ = 0,
  594. };
  595. /*
  596. * Node /spl-test3 index 4
  597. * driver sandbox_spl_test parent root_driver
  598. */
  599. static struct dtd_sandbox_spl_test dtv_spl_test3 = {
  600. \t.longbytearray\t\t= {0x90a0b0c, 0xd0e0f10},
  601. \t.stringarray\t\t= "one",
  602. };
  603. DM_DEVICE_INST(spl_test3) = {
  604. \t.driver\t\t= DM_DRIVER_REF(sandbox_spl_test),
  605. \t.name\t\t= "sandbox_spl_test",
  606. \t.plat_\t\t= &dtv_spl_test3,
  607. \t.uclass\t\t= DM_UCLASS_REF(misc),
  608. \t.uclass_node\t= {
  609. \t\t.prev = &DM_DEVICE_REF(spl_test)->uclass_node,
  610. \t\t.next = &DM_UCLASS_REF(misc)->dev_head,
  611. \t},
  612. \t.child_head\t= {
  613. \t\t.prev = &DM_DEVICE_REF(spl_test3)->child_head,
  614. \t\t.next = &DM_DEVICE_REF(spl_test3)->child_head,
  615. \t},
  616. \t.sibling_node\t= {
  617. \t\t.prev = &DM_DEVICE_REF(spl_test)->sibling_node,
  618. \t\t.next = &DM_DEVICE_REF(root)->child_head,
  619. \t},
  620. \t.seq_ = 1,
  621. };
  622. /*
  623. * Node /some-bus/test index 5
  624. * driver denx_u_boot_fdt_test parent denx_u_boot_test_bus
  625. */
  626. #include <dm/test.h>
  627. struct dm_test_pdata __attribute__ ((section (".priv_data")))
  628. \t_denx_u_boot_fdt_test_plat_test = {
  629. \t.dtplat = {
  630. \t\t.ping_add\t\t= 0x5,
  631. \t\t.ping_expect\t\t= 0x5,
  632. \t\t.reg\t\t\t= {0x5},
  633. \t},
  634. };
  635. #include <dm/test.h>
  636. u8 _denx_u_boot_fdt_test_priv_test[sizeof(struct dm_test_priv)]
  637. \t__attribute__ ((section (".priv_data")));
  638. #include <dm/test.h>
  639. u8 _denx_u_boot_fdt_test_parent_plat_test[sizeof(struct dm_test_parent_plat)]
  640. \t__attribute__ ((section (".priv_data")));
  641. #include <dm/test.h>
  642. u8 _denx_u_boot_fdt_test_parent_priv_test[sizeof(struct dm_test_parent_data)]
  643. \t__attribute__ ((section (".priv_data")));
  644. DM_DEVICE_INST(test) = {
  645. \t.driver\t\t= DM_DRIVER_REF(denx_u_boot_fdt_test),
  646. \t.name\t\t= "denx_u_boot_fdt_test",
  647. \t.plat_\t\t= &_denx_u_boot_fdt_test_plat_test,
  648. \t.parent_plat_\t= _denx_u_boot_fdt_test_parent_plat_test,
  649. \t.driver_data\t= DM_TEST_TYPE_FIRST,
  650. \t.parent\t\t= DM_DEVICE_REF(some_bus),
  651. \t.priv_\t\t= _denx_u_boot_fdt_test_priv_test,
  652. \t.uclass\t\t= DM_UCLASS_REF(testfdt),
  653. \t.parent_priv_\t= _denx_u_boot_fdt_test_parent_priv_test,
  654. \t.uclass_node\t= {
  655. \t\t.prev = &DM_UCLASS_REF(testfdt)->dev_head,
  656. \t\t.next = &DM_DEVICE_REF(test0)->uclass_node,
  657. \t},
  658. \t.child_head\t= {
  659. \t\t.prev = &DM_DEVICE_REF(test)->child_head,
  660. \t\t.next = &DM_DEVICE_REF(test)->child_head,
  661. \t},
  662. \t.sibling_node\t= {
  663. \t\t.prev = &DM_DEVICE_REF(some_bus)->child_head,
  664. \t\t.next = &DM_DEVICE_REF(test0)->sibling_node,
  665. \t},
  666. \t.seq_ = 1,
  667. };
  668. /*
  669. * Node /some-bus/test0 index 6
  670. * driver denx_u_boot_fdt_test parent denx_u_boot_test_bus
  671. */
  672. #include <dm/test.h>
  673. struct dm_test_pdata __attribute__ ((section (".priv_data")))
  674. \t_denx_u_boot_fdt_test_plat_test0 = {
  675. \t.dtplat = {
  676. \t},
  677. };
  678. #include <dm/test.h>
  679. u8 _denx_u_boot_fdt_test_priv_test0[sizeof(struct dm_test_priv)]
  680. \t__attribute__ ((section (".priv_data")));
  681. #include <dm/test.h>
  682. u8 _denx_u_boot_fdt_test_parent_plat_test0[sizeof(struct dm_test_parent_plat)]
  683. \t__attribute__ ((section (".priv_data")));
  684. #include <dm/test.h>
  685. u8 _denx_u_boot_fdt_test_parent_priv_test0[sizeof(struct dm_test_parent_data)]
  686. \t__attribute__ ((section (".priv_data")));
  687. DM_DEVICE_INST(test0) = {
  688. \t.driver\t\t= DM_DRIVER_REF(denx_u_boot_fdt_test),
  689. \t.name\t\t= "denx_u_boot_fdt_test",
  690. \t.plat_\t\t= &_denx_u_boot_fdt_test_plat_test0,
  691. \t.parent_plat_\t= _denx_u_boot_fdt_test_parent_plat_test0,
  692. \t.driver_data\t= DM_TEST_TYPE_SECOND,
  693. \t.parent\t\t= DM_DEVICE_REF(some_bus),
  694. \t.priv_\t\t= _denx_u_boot_fdt_test_priv_test0,
  695. \t.uclass\t\t= DM_UCLASS_REF(testfdt),
  696. \t.parent_priv_\t= _denx_u_boot_fdt_test_parent_priv_test0,
  697. \t.uclass_node\t= {
  698. \t\t.prev = &DM_DEVICE_REF(test)->uclass_node,
  699. \t\t.next = &DM_UCLASS_REF(testfdt)->dev_head,
  700. \t},
  701. \t.child_head\t= {
  702. \t\t.prev = &DM_DEVICE_REF(test0)->child_head,
  703. \t\t.next = &DM_DEVICE_REF(test0)->child_head,
  704. \t},
  705. \t.sibling_node\t= {
  706. \t\t.prev = &DM_DEVICE_REF(test)->sibling_node,
  707. \t\t.next = &DM_DEVICE_REF(some_bus)->child_head,
  708. \t},
  709. \t.seq_ = 2,
  710. };
  711. '''
  712. def test_simple(self):
  713. """Test output from some simple nodes with various types of data"""
  714. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  715. output = tools.GetOutputFilename('output')
  716. self.run_test(['struct'], dtb_file, output)
  717. with open(output) as infile:
  718. data = infile.read()
  719. self._check_strings(self.struct_text, data)
  720. self.run_test(['platdata'], dtb_file, output)
  721. with open(output) as infile:
  722. data = infile.read()
  723. self._check_strings(self.platdata_text, data)
  724. self.run_test(['decl'], dtb_file, output)
  725. with open(output) as infile:
  726. data = infile.read()
  727. self._check_strings(self.decl_text, data)
  728. # Try the 'all' command
  729. self.run_test(['all'], dtb_file, output)
  730. data = tools.ReadFile(output, binary=False)
  731. self._check_strings(
  732. self.decl_text + self.platdata_text + self.struct_text, data)
  733. def test_driver_alias(self):
  734. """Test output from a device tree file with a driver alias"""
  735. dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
  736. output = tools.GetOutputFilename('output')
  737. self.run_test(['struct'], dtb_file, output)
  738. with open(output) as infile:
  739. data = infile.read()
  740. self._check_strings(HEADER + '''
  741. struct dtd_sandbox_gpio {
  742. \tconst char *\tgpio_bank_name;
  743. \tbool\t\tgpio_controller;
  744. \tfdt32_t\t\tsandbox_gpio_count;
  745. };
  746. ''', data)
  747. self.run_test(['platdata'], dtb_file, output)
  748. with open(output) as infile:
  749. data = infile.read()
  750. self._check_strings(C_HEADER + '''
  751. /*
  752. * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
  753. *
  754. * idx driver_info driver
  755. * --- -------------------- --------------------
  756. * 0: gpios_at_0 sandbox_gpio
  757. * --- -------------------- --------------------
  758. */
  759. /*
  760. * Node /gpios@0 index 0
  761. * driver sandbox_gpio parent None
  762. */
  763. static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
  764. \t.gpio_bank_name\t\t= "a",
  765. \t.gpio_controller\t= true,
  766. \t.sandbox_gpio_count\t= 0x14,
  767. };
  768. U_BOOT_DRVINFO(gpios_at_0) = {
  769. \t.name\t\t= "sandbox_gpio",
  770. \t.plat\t\t= &dtv_gpios_at_0,
  771. \t.plat_size\t= sizeof(dtv_gpios_at_0),
  772. \t.parent_idx\t= -1,
  773. };
  774. ''', data)
  775. def test_invalid_driver(self):
  776. """Test output from a device tree file with an invalid driver"""
  777. dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
  778. output = tools.GetOutputFilename('output')
  779. with test_util.capture_sys_output() as _:
  780. dtb_platdata.run_steps(
  781. ['struct'], dtb_file, False, output, [], None, False,
  782. scan=copy_scan())
  783. with open(output) as infile:
  784. data = infile.read()
  785. self._check_strings(HEADER + '''
  786. struct dtd_invalid {
  787. };
  788. ''', data)
  789. with test_util.capture_sys_output() as _:
  790. dtb_platdata.run_steps(
  791. ['platdata'], dtb_file, False, output, [], None, False,
  792. scan=copy_scan())
  793. with open(output) as infile:
  794. data = infile.read()
  795. self._check_strings(C_HEADER + '''
  796. /*
  797. * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
  798. *
  799. * idx driver_info driver
  800. * --- -------------------- --------------------
  801. * 0: spl_test invalid
  802. * --- -------------------- --------------------
  803. */
  804. /* Node /spl-test index 0 */
  805. static struct dtd_invalid dtv_spl_test = {
  806. };
  807. U_BOOT_DRVINFO(spl_test) = {
  808. \t.name\t\t= "invalid",
  809. \t.plat\t\t= &dtv_spl_test,
  810. \t.plat_size\t= sizeof(dtv_spl_test),
  811. \t.parent_idx\t= -1,
  812. };
  813. ''', data)
  814. def test_phandle(self):
  815. """Test output from a node containing a phandle reference"""
  816. dtb_file = get_dtb_file('dtoc_test_phandle.dts')
  817. output = tools.GetOutputFilename('output')
  818. self.run_test(['struct'], dtb_file, output)
  819. with open(output) as infile:
  820. data = infile.read()
  821. self._check_strings(HEADER + '''
  822. struct dtd_source {
  823. \tstruct phandle_2_arg clocks[4];
  824. };
  825. struct dtd_target {
  826. \tfdt32_t\t\tintval;
  827. };
  828. ''', data)
  829. self.run_test(['platdata'], dtb_file, output)
  830. with open(output) as infile:
  831. data = infile.read()
  832. self._check_strings(C_HEADER + '''
  833. /*
  834. * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
  835. *
  836. * idx driver_info driver
  837. * --- -------------------- --------------------
  838. * 0: phandle2_target target
  839. * 1: phandle3_target target
  840. * 2: phandle_source source
  841. * 3: phandle_source2 source
  842. * 4: phandle_target target
  843. * --- -------------------- --------------------
  844. */
  845. /* Node /phandle2-target index 0 */
  846. static struct dtd_target dtv_phandle2_target = {
  847. \t.intval\t\t\t= 0x1,
  848. };
  849. U_BOOT_DRVINFO(phandle2_target) = {
  850. \t.name\t\t= "target",
  851. \t.plat\t\t= &dtv_phandle2_target,
  852. \t.plat_size\t= sizeof(dtv_phandle2_target),
  853. \t.parent_idx\t= -1,
  854. };
  855. /* Node /phandle3-target index 1 */
  856. static struct dtd_target dtv_phandle3_target = {
  857. \t.intval\t\t\t= 0x2,
  858. };
  859. U_BOOT_DRVINFO(phandle3_target) = {
  860. \t.name\t\t= "target",
  861. \t.plat\t\t= &dtv_phandle3_target,
  862. \t.plat_size\t= sizeof(dtv_phandle3_target),
  863. \t.parent_idx\t= -1,
  864. };
  865. /* Node /phandle-source index 2 */
  866. static struct dtd_source dtv_phandle_source = {
  867. \t.clocks\t\t\t= {
  868. \t\t\t{4, {}},
  869. \t\t\t{0, {11}},
  870. \t\t\t{1, {12, 13}},
  871. \t\t\t{4, {}},},
  872. };
  873. U_BOOT_DRVINFO(phandle_source) = {
  874. \t.name\t\t= "source",
  875. \t.plat\t\t= &dtv_phandle_source,
  876. \t.plat_size\t= sizeof(dtv_phandle_source),
  877. \t.parent_idx\t= -1,
  878. };
  879. /* Node /phandle-source2 index 3 */
  880. static struct dtd_source dtv_phandle_source2 = {
  881. \t.clocks\t\t\t= {
  882. \t\t\t{4, {}},},
  883. };
  884. U_BOOT_DRVINFO(phandle_source2) = {
  885. \t.name\t\t= "source",
  886. \t.plat\t\t= &dtv_phandle_source2,
  887. \t.plat_size\t= sizeof(dtv_phandle_source2),
  888. \t.parent_idx\t= -1,
  889. };
  890. /* Node /phandle-target index 4 */
  891. static struct dtd_target dtv_phandle_target = {
  892. \t.intval\t\t\t= 0x0,
  893. };
  894. U_BOOT_DRVINFO(phandle_target) = {
  895. \t.name\t\t= "target",
  896. \t.plat\t\t= &dtv_phandle_target,
  897. \t.plat_size\t= sizeof(dtv_phandle_target),
  898. \t.parent_idx\t= -1,
  899. };
  900. ''', data)
  901. def test_phandle_single(self):
  902. """Test output from a node containing a phandle reference"""
  903. dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
  904. output = tools.GetOutputFilename('output')
  905. self.run_test(['struct'], dtb_file, output)
  906. with open(output) as infile:
  907. data = infile.read()
  908. self._check_strings(HEADER + '''
  909. struct dtd_source {
  910. \tstruct phandle_0_arg clocks[1];
  911. };
  912. struct dtd_target {
  913. \tfdt32_t\t\tintval;
  914. };
  915. ''', data)
  916. def test_phandle_reorder(self):
  917. """Test that phandle targets are generated before their references"""
  918. dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
  919. output = tools.GetOutputFilename('output')
  920. self.run_test(['platdata'], dtb_file, output)
  921. with open(output) as infile:
  922. data = infile.read()
  923. self._check_strings(C_HEADER + '''
  924. /*
  925. * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
  926. *
  927. * idx driver_info driver
  928. * --- -------------------- --------------------
  929. * 0: phandle_source2 source
  930. * 1: phandle_target target
  931. * --- -------------------- --------------------
  932. */
  933. /* Node /phandle-source2 index 0 */
  934. static struct dtd_source dtv_phandle_source2 = {
  935. \t.clocks\t\t\t= {
  936. \t\t\t{1, {}},},
  937. };
  938. U_BOOT_DRVINFO(phandle_source2) = {
  939. \t.name\t\t= "source",
  940. \t.plat\t\t= &dtv_phandle_source2,
  941. \t.plat_size\t= sizeof(dtv_phandle_source2),
  942. \t.parent_idx\t= -1,
  943. };
  944. /* Node /phandle-target index 1 */
  945. static struct dtd_target dtv_phandle_target = {
  946. };
  947. U_BOOT_DRVINFO(phandle_target) = {
  948. \t.name\t\t= "target",
  949. \t.plat\t\t= &dtv_phandle_target,
  950. \t.plat_size\t= sizeof(dtv_phandle_target),
  951. \t.parent_idx\t= -1,
  952. };
  953. ''', data)
  954. def test_phandle_cd_gpio(self):
  955. """Test that phandle targets are generated when unsing cd-gpios"""
  956. dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
  957. output = tools.GetOutputFilename('output')
  958. dtb_platdata.run_steps(
  959. ['platdata'], dtb_file, False, output, [], None, False,
  960. warning_disabled=True, scan=copy_scan())
  961. with open(output) as infile:
  962. data = infile.read()
  963. self._check_strings(C_HEADER + '''
  964. /*
  965. * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
  966. *
  967. * idx driver_info driver
  968. * --- -------------------- --------------------
  969. * 0: phandle2_target target
  970. * 1: phandle3_target target
  971. * 2: phandle_source source
  972. * 3: phandle_source2 source
  973. * 4: phandle_target target
  974. * --- -------------------- --------------------
  975. */
  976. /* Node /phandle2-target index 0 */
  977. static struct dtd_target dtv_phandle2_target = {
  978. \t.intval\t\t\t= 0x1,
  979. };
  980. U_BOOT_DRVINFO(phandle2_target) = {
  981. \t.name\t\t= "target",
  982. \t.plat\t\t= &dtv_phandle2_target,
  983. \t.plat_size\t= sizeof(dtv_phandle2_target),
  984. \t.parent_idx\t= -1,
  985. };
  986. /* Node /phandle3-target index 1 */
  987. static struct dtd_target dtv_phandle3_target = {
  988. \t.intval\t\t\t= 0x2,
  989. };
  990. U_BOOT_DRVINFO(phandle3_target) = {
  991. \t.name\t\t= "target",
  992. \t.plat\t\t= &dtv_phandle3_target,
  993. \t.plat_size\t= sizeof(dtv_phandle3_target),
  994. \t.parent_idx\t= -1,
  995. };
  996. /* Node /phandle-source index 2 */
  997. static struct dtd_source dtv_phandle_source = {
  998. \t.cd_gpios\t\t= {
  999. \t\t\t{4, {}},
  1000. \t\t\t{0, {11}},
  1001. \t\t\t{1, {12, 13}},
  1002. \t\t\t{4, {}},},
  1003. };
  1004. U_BOOT_DRVINFO(phandle_source) = {
  1005. \t.name\t\t= "source",
  1006. \t.plat\t\t= &dtv_phandle_source,
  1007. \t.plat_size\t= sizeof(dtv_phandle_source),
  1008. \t.parent_idx\t= -1,
  1009. };
  1010. /* Node /phandle-source2 index 3 */
  1011. static struct dtd_source dtv_phandle_source2 = {
  1012. \t.cd_gpios\t\t= {
  1013. \t\t\t{4, {}},},
  1014. };
  1015. U_BOOT_DRVINFO(phandle_source2) = {
  1016. \t.name\t\t= "source",
  1017. \t.plat\t\t= &dtv_phandle_source2,
  1018. \t.plat_size\t= sizeof(dtv_phandle_source2),
  1019. \t.parent_idx\t= -1,
  1020. };
  1021. /* Node /phandle-target index 4 */
  1022. static struct dtd_target dtv_phandle_target = {
  1023. \t.intval\t\t\t= 0x0,
  1024. };
  1025. U_BOOT_DRVINFO(phandle_target) = {
  1026. \t.name\t\t= "target",
  1027. \t.plat\t\t= &dtv_phandle_target,
  1028. \t.plat_size\t= sizeof(dtv_phandle_target),
  1029. \t.parent_idx\t= -1,
  1030. };
  1031. ''', data)
  1032. def test_phandle_bad(self):
  1033. """Test a node containing an invalid phandle fails"""
  1034. dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
  1035. capture_stderr=True)
  1036. output = tools.GetOutputFilename('output')
  1037. with self.assertRaises(ValueError) as exc:
  1038. self.run_test(['struct'], dtb_file, output)
  1039. self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
  1040. str(exc.exception))
  1041. def test_phandle_bad2(self):
  1042. """Test a phandle target missing its #*-cells property"""
  1043. dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
  1044. capture_stderr=True)
  1045. output = tools.GetOutputFilename('output')
  1046. with self.assertRaises(ValueError) as exc:
  1047. self.run_test(['struct'], dtb_file, output)
  1048. self.assertIn("Node 'phandle-target' has no cells property",
  1049. str(exc.exception))
  1050. def test_addresses64(self):
  1051. """Test output from a node with a 'reg' property with na=2, ns=2"""
  1052. dtb_file = get_dtb_file('dtoc_test_addr64.dts')
  1053. output = tools.GetOutputFilename('output')
  1054. self.run_test(['struct'], dtb_file, output)
  1055. with open(output) as infile:
  1056. data = infile.read()
  1057. self._check_strings(HEADER + '''
  1058. struct dtd_test1 {
  1059. \tfdt64_t\t\treg[2];
  1060. };
  1061. struct dtd_test2 {
  1062. \tfdt64_t\t\treg[2];
  1063. };
  1064. struct dtd_test3 {
  1065. \tfdt64_t\t\treg[4];
  1066. };
  1067. ''', data)
  1068. self.run_test(['platdata'], dtb_file, output)
  1069. with open(output) as infile:
  1070. data = infile.read()
  1071. self._check_strings(C_HEADER + '''
  1072. /*
  1073. * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
  1074. *
  1075. * idx driver_info driver
  1076. * --- -------------------- --------------------
  1077. * 0: test1 test1
  1078. * 1: test2 test2
  1079. * 2: test3 test3
  1080. * --- -------------------- --------------------
  1081. */
  1082. /* Node /test1 index 0 */
  1083. static struct dtd_test1 dtv_test1 = {
  1084. \t.reg\t\t\t= {0x1234, 0x5678},
  1085. };
  1086. U_BOOT_DRVINFO(test1) = {
  1087. \t.name\t\t= "test1",
  1088. \t.plat\t\t= &dtv_test1,
  1089. \t.plat_size\t= sizeof(dtv_test1),
  1090. \t.parent_idx\t= -1,
  1091. };
  1092. /* Node /test2 index 1 */
  1093. static struct dtd_test2 dtv_test2 = {
  1094. \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
  1095. };
  1096. U_BOOT_DRVINFO(test2) = {
  1097. \t.name\t\t= "test2",
  1098. \t.plat\t\t= &dtv_test2,
  1099. \t.plat_size\t= sizeof(dtv_test2),
  1100. \t.parent_idx\t= -1,
  1101. };
  1102. /* Node /test3 index 2 */
  1103. static struct dtd_test3 dtv_test3 = {
  1104. \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
  1105. };
  1106. U_BOOT_DRVINFO(test3) = {
  1107. \t.name\t\t= "test3",
  1108. \t.plat\t\t= &dtv_test3,
  1109. \t.plat_size\t= sizeof(dtv_test3),
  1110. \t.parent_idx\t= -1,
  1111. };
  1112. ''', data)
  1113. def test_addresses32(self):
  1114. """Test output from a node with a 'reg' property with na=1, ns=1"""
  1115. dtb_file = get_dtb_file('dtoc_test_addr32.dts')
  1116. output = tools.GetOutputFilename('output')
  1117. self.run_test(['struct'], dtb_file, output)
  1118. with open(output) as infile:
  1119. data = infile.read()
  1120. self._check_strings(HEADER + '''
  1121. struct dtd_test1 {
  1122. \tfdt32_t\t\treg[2];
  1123. };
  1124. struct dtd_test2 {
  1125. \tfdt32_t\t\treg[4];
  1126. };
  1127. ''', data)
  1128. self.run_test(['platdata'], dtb_file, output)
  1129. with open(output) as infile:
  1130. data = infile.read()
  1131. self._check_strings(C_HEADER + '''
  1132. /*
  1133. * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
  1134. *
  1135. * idx driver_info driver
  1136. * --- -------------------- --------------------
  1137. * 0: test1 test1
  1138. * 1: test2 test2
  1139. * --- -------------------- --------------------
  1140. */
  1141. /* Node /test1 index 0 */
  1142. static struct dtd_test1 dtv_test1 = {
  1143. \t.reg\t\t\t= {0x1234, 0x5678},
  1144. };
  1145. U_BOOT_DRVINFO(test1) = {
  1146. \t.name\t\t= "test1",
  1147. \t.plat\t\t= &dtv_test1,
  1148. \t.plat_size\t= sizeof(dtv_test1),
  1149. \t.parent_idx\t= -1,
  1150. };
  1151. /* Node /test2 index 1 */
  1152. static struct dtd_test2 dtv_test2 = {
  1153. \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
  1154. };
  1155. U_BOOT_DRVINFO(test2) = {
  1156. \t.name\t\t= "test2",
  1157. \t.plat\t\t= &dtv_test2,
  1158. \t.plat_size\t= sizeof(dtv_test2),
  1159. \t.parent_idx\t= -1,
  1160. };
  1161. ''', data)
  1162. def test_addresses64_32(self):
  1163. """Test output from a node with a 'reg' property with na=2, ns=1"""
  1164. dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
  1165. output = tools.GetOutputFilename('output')
  1166. self.run_test(['struct'], dtb_file, output)
  1167. with open(output) as infile:
  1168. data = infile.read()
  1169. self._check_strings(HEADER + '''
  1170. struct dtd_test1 {
  1171. \tfdt64_t\t\treg[2];
  1172. };
  1173. struct dtd_test2 {
  1174. \tfdt64_t\t\treg[2];
  1175. };
  1176. struct dtd_test3 {
  1177. \tfdt64_t\t\treg[4];
  1178. };
  1179. ''', data)
  1180. self.run_test(['platdata'], dtb_file, output)
  1181. with open(output) as infile:
  1182. data = infile.read()
  1183. self._check_strings(C_HEADER + '''
  1184. /*
  1185. * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
  1186. *
  1187. * idx driver_info driver
  1188. * --- -------------------- --------------------
  1189. * 0: test1 test1
  1190. * 1: test2 test2
  1191. * 2: test3 test3
  1192. * --- -------------------- --------------------
  1193. */
  1194. /* Node /test1 index 0 */
  1195. static struct dtd_test1 dtv_test1 = {
  1196. \t.reg\t\t\t= {0x123400000000, 0x5678},
  1197. };
  1198. U_BOOT_DRVINFO(test1) = {
  1199. \t.name\t\t= "test1",
  1200. \t.plat\t\t= &dtv_test1,
  1201. \t.plat_size\t= sizeof(dtv_test1),
  1202. \t.parent_idx\t= -1,
  1203. };
  1204. /* Node /test2 index 1 */
  1205. static struct dtd_test2 dtv_test2 = {
  1206. \t.reg\t\t\t= {0x1234567890123456, 0x98765432},
  1207. };
  1208. U_BOOT_DRVINFO(test2) = {
  1209. \t.name\t\t= "test2",
  1210. \t.plat\t\t= &dtv_test2,
  1211. \t.plat_size\t= sizeof(dtv_test2),
  1212. \t.parent_idx\t= -1,
  1213. };
  1214. /* Node /test3 index 2 */
  1215. static struct dtd_test3 dtv_test3 = {
  1216. \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
  1217. };
  1218. U_BOOT_DRVINFO(test3) = {
  1219. \t.name\t\t= "test3",
  1220. \t.plat\t\t= &dtv_test3,
  1221. \t.plat_size\t= sizeof(dtv_test3),
  1222. \t.parent_idx\t= -1,
  1223. };
  1224. ''', data)
  1225. def test_addresses32_64(self):
  1226. """Test output from a node with a 'reg' property with na=1, ns=2"""
  1227. dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
  1228. output = tools.GetOutputFilename('output')
  1229. self.run_test(['struct'], dtb_file, output)
  1230. with open(output) as infile:
  1231. data = infile.read()
  1232. self._check_strings(HEADER + '''
  1233. struct dtd_test1 {
  1234. \tfdt64_t\t\treg[2];
  1235. };
  1236. struct dtd_test2 {
  1237. \tfdt64_t\t\treg[2];
  1238. };
  1239. struct dtd_test3 {
  1240. \tfdt64_t\t\treg[4];
  1241. };
  1242. ''', data)
  1243. self.run_test(['platdata'], dtb_file, output)
  1244. with open(output) as infile:
  1245. data = infile.read()
  1246. self._check_strings(C_HEADER + '''
  1247. /*
  1248. * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
  1249. *
  1250. * idx driver_info driver
  1251. * --- -------------------- --------------------
  1252. * 0: test1 test1
  1253. * 1: test2 test2
  1254. * 2: test3 test3
  1255. * --- -------------------- --------------------
  1256. */
  1257. /* Node /test1 index 0 */
  1258. static struct dtd_test1 dtv_test1 = {
  1259. \t.reg\t\t\t= {0x1234, 0x567800000000},
  1260. };
  1261. U_BOOT_DRVINFO(test1) = {
  1262. \t.name\t\t= "test1",
  1263. \t.plat\t\t= &dtv_test1,
  1264. \t.plat_size\t= sizeof(dtv_test1),
  1265. \t.parent_idx\t= -1,
  1266. };
  1267. /* Node /test2 index 1 */
  1268. static struct dtd_test2 dtv_test2 = {
  1269. \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
  1270. };
  1271. U_BOOT_DRVINFO(test2) = {
  1272. \t.name\t\t= "test2",
  1273. \t.plat\t\t= &dtv_test2,
  1274. \t.plat_size\t= sizeof(dtv_test2),
  1275. \t.parent_idx\t= -1,
  1276. };
  1277. /* Node /test3 index 2 */
  1278. static struct dtd_test3 dtv_test3 = {
  1279. \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
  1280. };
  1281. U_BOOT_DRVINFO(test3) = {
  1282. \t.name\t\t= "test3",
  1283. \t.plat\t\t= &dtv_test3,
  1284. \t.plat_size\t= sizeof(dtv_test3),
  1285. \t.parent_idx\t= -1,
  1286. };
  1287. ''', data)
  1288. def test_bad_reg(self):
  1289. """Test that a reg property with an invalid type generates an error"""
  1290. # Capture stderr since dtc will emit warnings for this file
  1291. dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
  1292. output = tools.GetOutputFilename('output')
  1293. with self.assertRaises(ValueError) as exc:
  1294. self.run_test(['struct'], dtb_file, output)
  1295. self.assertIn("Node 'spl-test' reg property is not an int",
  1296. str(exc.exception))
  1297. def test_bad_reg2(self):
  1298. """Test that a reg property with an invalid cell count is detected"""
  1299. # Capture stderr since dtc will emit warnings for this file
  1300. dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
  1301. output = tools.GetOutputFilename('output')
  1302. with self.assertRaises(ValueError) as exc:
  1303. self.run_test(['struct'], dtb_file, output)
  1304. self.assertIn(
  1305. "Node 'spl-test' (parent '/') reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
  1306. str(exc.exception))
  1307. def test_add_prop(self):
  1308. """Test that a subequent node can add a new property to a struct"""
  1309. dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
  1310. output = tools.GetOutputFilename('output')
  1311. self.run_test(['struct'], dtb_file, output)
  1312. with open(output) as infile:
  1313. data = infile.read()
  1314. self._check_strings(HEADER + '''
  1315. struct dtd_sandbox_spl_test {
  1316. \tfdt32_t\t\tintarray;
  1317. \tfdt32_t\t\tintval;
  1318. };
  1319. ''', data)
  1320. self.run_test(['platdata'], dtb_file, output)
  1321. with open(output) as infile:
  1322. data = infile.read()
  1323. self._check_strings(C_HEADER + '''
  1324. /*
  1325. * driver_info declarations, ordered by 'struct driver_info' linker_list idx:
  1326. *
  1327. * idx driver_info driver
  1328. * --- -------------------- --------------------
  1329. * 0: spl_test sandbox_spl_test
  1330. * 1: spl_test2 sandbox_spl_test
  1331. * --- -------------------- --------------------
  1332. */
  1333. /*
  1334. * Node /spl-test index 0
  1335. * driver sandbox_spl_test parent None
  1336. */
  1337. static struct dtd_sandbox_spl_test dtv_spl_test = {
  1338. \t.intval\t\t\t= 0x1,
  1339. };
  1340. U_BOOT_DRVINFO(spl_test) = {
  1341. \t.name\t\t= "sandbox_spl_test",
  1342. \t.plat\t\t= &dtv_spl_test,
  1343. \t.plat_size\t= sizeof(dtv_spl_test),
  1344. \t.parent_idx\t= -1,
  1345. };
  1346. /*
  1347. * Node /spl-test2 index 1
  1348. * driver sandbox_spl_test parent None
  1349. */
  1350. static struct dtd_sandbox_spl_test dtv_spl_test2 = {
  1351. \t.intarray\t\t= 0x5,
  1352. };
  1353. U_BOOT_DRVINFO(spl_test2) = {
  1354. \t.name\t\t= "sandbox_spl_test",
  1355. \t.plat\t\t= &dtv_spl_test2,
  1356. \t.plat_size\t= sizeof(dtv_spl_test2),
  1357. \t.parent_idx\t= -1,
  1358. };
  1359. ''', data)
  1360. def test_stdout(self):
  1361. """Test output to stdout"""
  1362. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  1363. with test_util.capture_sys_output() as (stdout, _):
  1364. self.run_test(['struct'], dtb_file, None)
  1365. self._check_strings(self.struct_text, stdout.getvalue())
  1366. def test_multi_to_file(self):
  1367. """Test output of multiple pieces to a single file"""
  1368. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  1369. output = tools.GetOutputFilename('output')
  1370. self.run_test(['all'], dtb_file, output)
  1371. data = tools.ReadFile(output, binary=False)
  1372. self._check_strings(
  1373. self.decl_text + self.platdata_text + self.struct_text, data)
  1374. def test_no_command(self):
  1375. """Test running dtoc without a command"""
  1376. with self.assertRaises(ValueError) as exc:
  1377. self.run_test([], '', '')
  1378. self.assertIn("Please specify a command: struct, platdata",
  1379. str(exc.exception))
  1380. def test_bad_command(self):
  1381. """Test running dtoc with an invalid command"""
  1382. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  1383. output = tools.GetOutputFilename('output')
  1384. with self.assertRaises(ValueError) as exc:
  1385. self.run_test(['invalid-cmd'], dtb_file, output)
  1386. self.assertIn(
  1387. "Unknown command 'invalid-cmd': (use: decl, platdata, struct)",
  1388. str(exc.exception))
  1389. def test_output_conflict(self):
  1390. """Test a conflict between and output dirs and output file"""
  1391. with self.assertRaises(ValueError) as exc:
  1392. dtb_platdata.run_steps(
  1393. ['all'], None, False, 'out', ['cdir'], None, False,
  1394. warning_disabled=True, scan=copy_scan())
  1395. self.assertIn("Must specify either output or output_dirs, not both",
  1396. str(exc.exception))
  1397. def check_output_dirs(self, instantiate):
  1398. # Remove the directory so that files from other tests are not there
  1399. tools._RemoveOutputDir()
  1400. tools.PrepareOutputDir(None)
  1401. # This should create the .dts and .dtb in the output directory
  1402. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  1403. outdir = tools.GetOutputDir()
  1404. fnames = glob.glob(outdir + '/*')
  1405. self.assertEqual(2, len(fnames))
  1406. dtb_platdata.run_steps(
  1407. ['all'], dtb_file, False, None, [outdir], None, instantiate,
  1408. warning_disabled=True, scan=copy_scan())
  1409. fnames = glob.glob(outdir + '/*')
  1410. return fnames
  1411. def test_output_dirs(self):
  1412. """Test outputting files to a directory"""
  1413. fnames = self.check_output_dirs(False)
  1414. self.assertEqual(5, len(fnames))
  1415. leafs = set(os.path.basename(fname) for fname in fnames)
  1416. self.assertEqual(
  1417. {'dt-structs-gen.h', 'source.dts', 'dt-plat.c', 'source.dtb',
  1418. 'dt-decl.h'},
  1419. leafs)
  1420. def test_output_dirs_inst(self):
  1421. """Test outputting files to a directory with instantiation"""
  1422. fnames = self.check_output_dirs(True)
  1423. self.assertEqual(6, len(fnames))
  1424. leafs = set(os.path.basename(fname) for fname in fnames)
  1425. self.assertEqual(
  1426. {'dt-structs-gen.h', 'source.dts', 'source.dtb',
  1427. 'dt-uclass.c', 'dt-decl.h', 'dt-device.c'},
  1428. leafs)
  1429. def setup_process_test(self):
  1430. """Set up a test of process_nodes()
  1431. This uses saved_scan but returns a deep copy of it, so it is safe to
  1432. modify it in these tests
  1433. Returns:
  1434. tuple:
  1435. DtbPlatdata: object to test
  1436. Scanner: scanner to use
  1437. """
  1438. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  1439. output = tools.GetOutputFilename('output')
  1440. # Take a copy before messing with it
  1441. scan = copy_scan()
  1442. plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
  1443. plat.scan_dtb()
  1444. plat.scan_tree(False)
  1445. plat.prepare_nodes()
  1446. return plat, scan
  1447. def test_process_nodes(self):
  1448. """Test processing nodes to add various info"""
  1449. plat, scan = self.setup_process_test()
  1450. plat.process_nodes(True)
  1451. i2c_node = plat._fdt.GetNode('/i2c@0')
  1452. pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
  1453. pmic = scan._drivers['sandbox_pmic']
  1454. i2c = scan._drivers['sandbox_i2c']
  1455. self.assertEqual('DM_DEVICE_REF(pmic_at_9)', pmic_node.dev_ref)
  1456. self.assertEqual(pmic, pmic_node.driver)
  1457. self.assertEqual(i2c_node, pmic_node.parent)
  1458. self.assertEqual(i2c, pmic_node.parent_driver)
  1459. # The pmic is the only child
  1460. self.assertEqual(pmic_node.parent_seq, 0)
  1461. self.assertEqual([pmic_node], i2c_node.child_devs)
  1462. # Start and end of the list should be the child_head
  1463. ref = '&DM_DEVICE_REF(i2c_at_0)->child_head'
  1464. self.assertEqual(
  1465. {-1: ref, 0: '&DM_DEVICE_REF(pmic_at_9)->sibling_node', 1: ref},
  1466. i2c_node.child_refs)
  1467. def test_process_nodes_bad_parent(self):
  1468. # Pretend that i2c has a parent (the pmic) and delete that driver
  1469. plat, scan = self.setup_process_test()
  1470. i2c_node = plat._fdt.GetNode('/i2c@0')
  1471. pmic_node = plat._fdt.GetNode('/i2c@0/pmic@9')
  1472. del scan._drivers['sandbox_pmic']
  1473. i2c_node.parent = pmic_node
  1474. # Process twice, the second time to generate an exception
  1475. plat.process_nodes(False)
  1476. with self.assertRaises(ValueError) as exc:
  1477. plat.process_nodes(True)
  1478. self.assertIn(
  1479. "Cannot parse/find parent driver 'sandbox_pmic' for 'sandbox_i2c",
  1480. str(exc.exception))
  1481. def test_process_nodes_bad_node(self):
  1482. plat, scan = self.setup_process_test()
  1483. # Now remove the pmic driver
  1484. del scan._drivers['sandbox_pmic']
  1485. # Process twice, the second time to generate an exception
  1486. plat.process_nodes(False)
  1487. with self.assertRaises(ValueError) as exc:
  1488. plat.process_nodes(True)
  1489. self.assertIn("Cannot parse/find driver for 'sandbox_pmic",
  1490. str(exc.exception))
  1491. def test_process_nodes_bad_uclass(self):
  1492. plat, scan = self.setup_process_test()
  1493. self.assertIn('UCLASS_I2C', scan._uclass)
  1494. del scan._uclass['UCLASS_I2C']
  1495. with self.assertRaises(ValueError) as exc:
  1496. plat.process_nodes(True)
  1497. self.assertIn("Cannot parse/find uclass 'UCLASS_I2C' for driver 'sandbox_i2c'",
  1498. str(exc.exception))
  1499. def test_process_nodes_used(self):
  1500. """Test processing nodes to add various info"""
  1501. plat, scan = self.setup_process_test()
  1502. plat.process_nodes(True)
  1503. pmic = scan._drivers['sandbox_pmic']
  1504. self.assertTrue(pmic.used)
  1505. gpio = scan._drivers['sandbox_gpio']
  1506. self.assertFalse(gpio.used)
  1507. def test_alias_read(self):
  1508. """Test obtaining aliases"""
  1509. dtb_file = get_dtb_file('dtoc_test_inst.dts')
  1510. output = tools.GetOutputFilename('output')
  1511. plat = self.run_test(['struct'], dtb_file, output)
  1512. scan = plat._scan
  1513. testfdt_node = plat._fdt.GetNode('/some-bus/test')
  1514. test0_node = plat._fdt.GetNode('/some-bus/test0')
  1515. self.assertIn('UCLASS_TEST_FDT', scan._uclass)
  1516. uc = scan._uclass['UCLASS_TEST_FDT']
  1517. self.assertEqual({1: testfdt_node, 2: test0_node},
  1518. uc.alias_num_to_node)
  1519. self.assertEqual({'/some-bus/test': 1, '/some-bus/test0': 2},
  1520. uc.alias_path_to_num)
  1521. # Try adding an alias that doesn't exist
  1522. self.assertFalse(scan.add_uclass_alias('fred', 3, None))
  1523. # Try adding an alias for a missing node
  1524. self.assertIsNone(scan.add_uclass_alias('testfdt', 3, None))
  1525. def test_alias_read_bad(self):
  1526. """Test invalid alias property name"""
  1527. dtb_file = get_dtb_file('dtoc_test_alias_bad.dts')
  1528. output = tools.GetOutputFilename('output')
  1529. with self.assertRaises(ValueError) as exc:
  1530. plat = self.run_test(['struct'], dtb_file, output)
  1531. self.assertIn("Cannot decode alias 'i2c4-'", str(exc.exception))
  1532. def test_alias_read_bad_path(self):
  1533. """Test alias pointing to a non-existent node"""
  1534. # This line may produce a warning, so capture it:
  1535. # Warning (alias_paths): /aliases:i2c4: aliases property is not a valid
  1536. # node (/does/not/exist)
  1537. dtb_file = get_dtb_file('dtoc_test_alias_bad_path.dts', True)
  1538. output = tools.GetOutputFilename('output')
  1539. with self.assertRaises(ValueError) as exc:
  1540. plat = self.run_test(['struct'], dtb_file, output)
  1541. self.assertIn("Alias 'i2c4' path '/does/not/exist' not found",
  1542. str(exc.exception))
  1543. def test_alias_read_bad_uclass(self):
  1544. """Test alias for a uclass that doesn't exist"""
  1545. dtb_file = get_dtb_file('dtoc_test_alias_bad_uc.dts')
  1546. output = tools.GetOutputFilename('output')
  1547. with test_util.capture_sys_output() as (stdout, _):
  1548. plat = self.run_test(['struct'], dtb_file, output)
  1549. self.assertEqual("Could not find uclass for alias 'other1'",
  1550. stdout.getvalue().strip())
  1551. def test_sequence(self):
  1552. """Test assignment of sequence numnbers"""
  1553. dtb_file = get_dtb_file('dtoc_test_inst.dts')
  1554. output = tools.GetOutputFilename('output')
  1555. plat = self.run_test(['struct'], dtb_file, output)
  1556. scan = plat._scan
  1557. testfdt = plat._fdt.GetNode('/some-bus/test')
  1558. self.assertEqual(1, testfdt.seq)
  1559. i2c = plat._fdt.GetNode('/i2c')
  1560. # For now this uclass is not compiled in, so no sequence is assigned
  1561. self.assertEqual(4, i2c.seq)
  1562. spl = plat._fdt.GetNode('/spl-test')
  1563. self.assertEqual(0, spl.seq)
  1564. def test_process_root(self):
  1565. """Test assignment of sequence numnbers"""
  1566. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  1567. output = tools.GetOutputFilename('output')
  1568. # Take a copy before messing with it
  1569. scan = copy_scan()
  1570. plat = dtb_platdata.DtbPlatdata(scan, dtb_file, False)
  1571. plat.scan_dtb()
  1572. root = plat._fdt.GetRoot()
  1573. plat.scan_tree(False)
  1574. self.assertNotIn(root, plat._valid_nodes)
  1575. plat.scan_tree(True)
  1576. self.assertIn(root, plat._valid_nodes)
  1577. self.assertEqual('root_driver',
  1578. scan.get_normalized_compat_name(root)[0])
  1579. def test_simple_inst(self):
  1580. """Test output from some simple nodes with instantiate enabled"""
  1581. dtb_file = get_dtb_file('dtoc_test_inst.dts')
  1582. output = tools.GetOutputFilename('output')
  1583. self.run_test(['decl'], dtb_file, output, True)
  1584. with open(output) as infile:
  1585. data = infile.read()
  1586. self._check_strings(self.decl_text_inst, data)
  1587. self.run_test(['uclass'], dtb_file, output, True)
  1588. with open(output) as infile:
  1589. data = infile.read()
  1590. self._check_strings(UCLASS_HEADER_COMMON + self.uclass_text_inst, data)
  1591. self.run_test(['device'], dtb_file, output, True)
  1592. with open(output) as infile:
  1593. data = infile.read()
  1594. self._check_strings(self.device_text_inst, data)
  1595. def test_inst_no_hdr(self):
  1596. """Test dealing with a struct tsssshat has no header"""
  1597. dtb_file = get_dtb_file('dtoc_test_inst.dts')
  1598. output = tools.GetOutputFilename('output')
  1599. # Run it once to set everything up
  1600. plat = self.run_test(['decl'], dtb_file, output, True)
  1601. scan = plat._scan
  1602. # Restart the output file and delete any record of the uclass' struct
  1603. plat.setup_output(Ftype.SOURCE, output)
  1604. del scan._structs['dm_test_uc_priv']
  1605. # Now generate the uclasses, which should provide a warning
  1606. with test_util.capture_sys_output() as (stdout, _):
  1607. plat.generate_uclasses()
  1608. self.assertEqual(
  1609. 'Warning: Cannot find header file for struct dm_test_uc_priv',
  1610. stdout.getvalue().strip())
  1611. def test_missing_props(self):
  1612. """Test detection of a parent node with no properties"""
  1613. dtb_file = get_dtb_file('dtoc_test_noprops.dts', capture_stderr=True)
  1614. output = tools.GetOutputFilename('output')
  1615. with self.assertRaises(ValueError) as exc:
  1616. self.run_test(['struct'], dtb_file, output)
  1617. self.assertIn("Parent node '/i2c@0' has no properties - do you need",
  1618. str(exc.exception))
  1619. def test_single_reg(self):
  1620. """Test detection of a parent node with no properties"""
  1621. dtb_file = get_dtb_file('dtoc_test_single_reg.dts')
  1622. output = tools.GetOutputFilename('output')
  1623. self.run_test(['struct'], dtb_file, output)
  1624. def test_missing_parent(self):
  1625. """Test detection of a parent node with no properties"""
  1626. dtb_file = get_dtb_file('dtoc_test_noparent.dts', capture_stderr=True)
  1627. output = tools.GetOutputFilename('output')
  1628. with self.assertRaises(ValueError) as exc:
  1629. self.run_test(['device'], dtb_file, output, instantiate=True)
  1630. self.assertIn("Node '/i2c@0/spl-test/pmic@9' requires parent node "
  1631. "'/i2c@0/spl-test' but it is not in the valid list",
  1632. str(exc.exception))