test_dtoc.py 56 KB

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