dtb_platdata.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. #!/usr/bin/python
  2. # SPDX-License-Identifier: GPL-2.0+
  3. #
  4. # Copyright (C) 2017 Google, Inc
  5. # Written by Simon Glass <sjg@chromium.org>
  6. #
  7. """Device tree to platform data class
  8. This supports converting device tree data to C structures definitions and
  9. static data.
  10. """
  11. import collections
  12. import copy
  13. import sys
  14. import fdt
  15. import fdt_util
  16. # When we see these properties we ignore them - i.e. do not create a structure member
  17. PROP_IGNORE_LIST = [
  18. '#address-cells',
  19. '#gpio-cells',
  20. '#size-cells',
  21. 'compatible',
  22. 'linux,phandle',
  23. "status",
  24. 'phandle',
  25. 'u-boot,dm-pre-reloc',
  26. 'u-boot,dm-tpl',
  27. 'u-boot,dm-spl',
  28. ]
  29. # C type declarations for the tyues we support
  30. TYPE_NAMES = {
  31. fdt.TYPE_INT: 'fdt32_t',
  32. fdt.TYPE_BYTE: 'unsigned char',
  33. fdt.TYPE_STRING: 'const char *',
  34. fdt.TYPE_BOOL: 'bool',
  35. fdt.TYPE_INT64: 'fdt64_t',
  36. }
  37. STRUCT_PREFIX = 'dtd_'
  38. VAL_PREFIX = 'dtv_'
  39. # This holds information about a property which includes phandles.
  40. #
  41. # max_args: integer: Maximum number or arguments that any phandle uses (int).
  42. # args: Number of args for each phandle in the property. The total number of
  43. # phandles is len(args). This is a list of integers.
  44. PhandleInfo = collections.namedtuple('PhandleInfo', ['max_args', 'args'])
  45. def conv_name_to_c(name):
  46. """Convert a device-tree name to a C identifier
  47. This uses multiple replace() calls instead of re.sub() since it is faster
  48. (400ms for 1m calls versus 1000ms for the 're' version).
  49. Args:
  50. name: Name to convert
  51. Return:
  52. String containing the C version of this name
  53. """
  54. new = name.replace('@', '_at_')
  55. new = new.replace('-', '_')
  56. new = new.replace(',', '_')
  57. new = new.replace('.', '_')
  58. return new
  59. def tab_to(num_tabs, line):
  60. """Append tabs to a line of text to reach a tab stop.
  61. Args:
  62. num_tabs: Tab stop to obtain (0 = column 0, 1 = column 8, etc.)
  63. line: Line of text to append to
  64. Returns:
  65. line with the correct number of tabs appeneded. If the line already
  66. extends past that tab stop then a single space is appended.
  67. """
  68. if len(line) >= num_tabs * 8:
  69. return line + ' '
  70. return line + '\t' * (num_tabs - len(line) // 8)
  71. def get_value(ftype, value):
  72. """Get a value as a C expression
  73. For integers this returns a byte-swapped (little-endian) hex string
  74. For bytes this returns a hex string, e.g. 0x12
  75. For strings this returns a literal string enclosed in quotes
  76. For booleans this return 'true'
  77. Args:
  78. type: Data type (fdt_util)
  79. value: Data value, as a string of bytes
  80. """
  81. if ftype == fdt.TYPE_INT:
  82. return '%#x' % fdt_util.fdt32_to_cpu(value)
  83. elif ftype == fdt.TYPE_BYTE:
  84. return '%#x' % ord(value[0])
  85. elif ftype == fdt.TYPE_STRING:
  86. return '"%s"' % value
  87. elif ftype == fdt.TYPE_BOOL:
  88. return 'true'
  89. elif ftype == fdt.TYPE_INT64:
  90. return '%#x' % value
  91. def get_compat_name(node):
  92. """Get a node's first compatible string as a C identifier
  93. Args:
  94. node: Node object to check
  95. Return:
  96. Tuple:
  97. C identifier for the first compatible string
  98. List of C identifiers for all the other compatible strings
  99. (possibly empty)
  100. """
  101. compat = node.props['compatible'].value
  102. aliases = []
  103. if isinstance(compat, list):
  104. compat, aliases = compat[0], compat[1:]
  105. return conv_name_to_c(compat), [conv_name_to_c(a) for a in aliases]
  106. class DtbPlatdata(object):
  107. """Provide a means to convert device tree binary data to platform data
  108. The output of this process is C structures which can be used in space-
  109. constrained encvironments where the ~3KB code overhead of device tree
  110. code is not affordable.
  111. Properties:
  112. _fdt: Fdt object, referencing the device tree
  113. _dtb_fname: Filename of the input device tree binary file
  114. _valid_nodes: A list of Node object with compatible strings
  115. _include_disabled: true to include nodes marked status = "disabled"
  116. _outfile: The current output file (sys.stdout or a real file)
  117. _lines: Stashed list of output lines for outputting in the future
  118. """
  119. def __init__(self, dtb_fname, include_disabled):
  120. self._fdt = None
  121. self._dtb_fname = dtb_fname
  122. self._valid_nodes = None
  123. self._include_disabled = include_disabled
  124. self._outfile = None
  125. self._lines = []
  126. self._aliases = {}
  127. def setup_output(self, fname):
  128. """Set up the output destination
  129. Once this is done, future calls to self.out() will output to this
  130. file.
  131. Args:
  132. fname: Filename to send output to, or '-' for stdout
  133. """
  134. if fname == '-':
  135. self._outfile = sys.stdout
  136. else:
  137. self._outfile = open(fname, 'w')
  138. def out(self, line):
  139. """Output a string to the output file
  140. Args:
  141. line: String to output
  142. """
  143. self._outfile.write(line)
  144. def buf(self, line):
  145. """Buffer up a string to send later
  146. Args:
  147. line: String to add to our 'buffer' list
  148. """
  149. self._lines.append(line)
  150. def get_buf(self):
  151. """Get the contents of the output buffer, and clear it
  152. Returns:
  153. The output buffer, which is then cleared for future use
  154. """
  155. lines = self._lines
  156. self._lines = []
  157. return lines
  158. def out_header(self):
  159. """Output a message indicating that this is an auto-generated file"""
  160. self.out('''/*
  161. * DO NOT MODIFY
  162. *
  163. * This file was generated by dtoc from a .dtb (device tree binary) file.
  164. */
  165. ''')
  166. def get_phandle_argc(self, prop, node_name):
  167. """Check if a node contains phandles
  168. We have no reliable way of detecting whether a node uses a phandle
  169. or not. As an interim measure, use a list of known property names.
  170. Args:
  171. prop: Prop object to check
  172. Return:
  173. Number of argument cells is this is a phandle, else None
  174. """
  175. if prop.name in ['clocks']:
  176. if not isinstance(prop.value, list):
  177. prop.value = [prop.value]
  178. val = prop.value
  179. i = 0
  180. max_args = 0
  181. args = []
  182. while i < len(val):
  183. phandle = fdt_util.fdt32_to_cpu(val[i])
  184. # If we get to the end of the list, stop. This can happen
  185. # since some nodes have more phandles in the list than others,
  186. # but we allocate enough space for the largest list. So those
  187. # nodes with shorter lists end up with zeroes at the end.
  188. if not phandle:
  189. break
  190. target = self._fdt.phandle_to_node.get(phandle)
  191. if not target:
  192. raise ValueError("Cannot parse '%s' in node '%s'" %
  193. (prop.name, node_name))
  194. prop_name = '#clock-cells'
  195. cells = target.props.get(prop_name)
  196. if not cells:
  197. raise ValueError("Node '%s' has no '%s' property" %
  198. (target.name, prop_name))
  199. num_args = fdt_util.fdt32_to_cpu(cells.value)
  200. max_args = max(max_args, num_args)
  201. args.append(num_args)
  202. i += 1 + num_args
  203. return PhandleInfo(max_args, args)
  204. return None
  205. def scan_dtb(self):
  206. """Scan the device tree to obtain a tree of nodes and properties
  207. Once this is done, self._fdt.GetRoot() can be called to obtain the
  208. device tree root node, and progress from there.
  209. """
  210. self._fdt = fdt.FdtScan(self._dtb_fname)
  211. def scan_node(self, root):
  212. """Scan a node and subnodes to build a tree of node and phandle info
  213. This adds each node to self._valid_nodes.
  214. Args:
  215. root: Root node for scan
  216. """
  217. for node in root.subnodes:
  218. if 'compatible' in node.props:
  219. status = node.props.get('status')
  220. if (not self._include_disabled and not status or
  221. status.value != 'disabled'):
  222. self._valid_nodes.append(node)
  223. # recurse to handle any subnodes
  224. self.scan_node(node)
  225. def scan_tree(self):
  226. """Scan the device tree for useful information
  227. This fills in the following properties:
  228. _valid_nodes: A list of nodes we wish to consider include in the
  229. platform data
  230. """
  231. self._valid_nodes = []
  232. return self.scan_node(self._fdt.GetRoot())
  233. @staticmethod
  234. def get_num_cells(node):
  235. """Get the number of cells in addresses and sizes for this node
  236. Args:
  237. node: Node to check
  238. Returns:
  239. Tuple:
  240. Number of address cells for this node
  241. Number of size cells for this node
  242. """
  243. parent = node.parent
  244. na, ns = 2, 2
  245. if parent:
  246. na_prop = parent.props.get('#address-cells')
  247. ns_prop = parent.props.get('#size-cells')
  248. if na_prop:
  249. na = fdt_util.fdt32_to_cpu(na_prop.value)
  250. if ns_prop:
  251. ns = fdt_util.fdt32_to_cpu(ns_prop.value)
  252. return na, ns
  253. def scan_reg_sizes(self):
  254. """Scan for 64-bit 'reg' properties and update the values
  255. This finds 'reg' properties with 64-bit data and converts the value to
  256. an array of 64-values. This allows it to be output in a way that the
  257. C code can read.
  258. """
  259. for node in self._valid_nodes:
  260. reg = node.props.get('reg')
  261. if not reg:
  262. continue
  263. na, ns = self.get_num_cells(node)
  264. total = na + ns
  265. if reg.type != fdt.TYPE_INT:
  266. raise ValueError("Node '%s' reg property is not an int" %
  267. node.name)
  268. if len(reg.value) % total:
  269. raise ValueError("Node '%s' reg property has %d cells "
  270. 'which is not a multiple of na + ns = %d + %d)' %
  271. (node.name, len(reg.value), na, ns))
  272. reg.na = na
  273. reg.ns = ns
  274. if na != 1 or ns != 1:
  275. reg.type = fdt.TYPE_INT64
  276. i = 0
  277. new_value = []
  278. val = reg.value
  279. if not isinstance(val, list):
  280. val = [val]
  281. while i < len(val):
  282. addr = fdt_util.fdt_cells_to_cpu(val[i:], reg.na)
  283. i += na
  284. size = fdt_util.fdt_cells_to_cpu(val[i:], reg.ns)
  285. i += ns
  286. new_value += [addr, size]
  287. reg.value = new_value
  288. def scan_structs(self):
  289. """Scan the device tree building up the C structures we will use.
  290. Build a dict keyed by C struct name containing a dict of Prop
  291. object for each struct field (keyed by property name). Where the
  292. same struct appears multiple times, try to use the 'widest'
  293. property, i.e. the one with a type which can express all others.
  294. Once the widest property is determined, all other properties are
  295. updated to match that width.
  296. """
  297. structs = {}
  298. for node in self._valid_nodes:
  299. node_name, _ = get_compat_name(node)
  300. fields = {}
  301. # Get a list of all the valid properties in this node.
  302. for name, prop in node.props.items():
  303. if name not in PROP_IGNORE_LIST and name[0] != '#':
  304. fields[name] = copy.deepcopy(prop)
  305. # If we've seen this node_name before, update the existing struct.
  306. if node_name in structs:
  307. struct = structs[node_name]
  308. for name, prop in fields.items():
  309. oldprop = struct.get(name)
  310. if oldprop:
  311. oldprop.Widen(prop)
  312. else:
  313. struct[name] = prop
  314. # Otherwise store this as a new struct.
  315. else:
  316. structs[node_name] = fields
  317. upto = 0
  318. for node in self._valid_nodes:
  319. node_name, _ = get_compat_name(node)
  320. struct = structs[node_name]
  321. for name, prop in node.props.items():
  322. if name not in PROP_IGNORE_LIST and name[0] != '#':
  323. prop.Widen(struct[name])
  324. upto += 1
  325. struct_name, aliases = get_compat_name(node)
  326. for alias in aliases:
  327. self._aliases[alias] = struct_name
  328. return structs
  329. def scan_phandles(self):
  330. """Figure out what phandles each node uses
  331. We need to be careful when outputing nodes that use phandles since
  332. they must come after the declaration of the phandles in the C file.
  333. Otherwise we get a compiler error since the phandle struct is not yet
  334. declared.
  335. This function adds to each node a list of phandle nodes that the node
  336. depends on. This allows us to output things in the right order.
  337. """
  338. for node in self._valid_nodes:
  339. node.phandles = set()
  340. for pname, prop in node.props.items():
  341. if pname in PROP_IGNORE_LIST or pname[0] == '#':
  342. continue
  343. info = self.get_phandle_argc(prop, node.name)
  344. if info:
  345. # Process the list as pairs of (phandle, id)
  346. pos = 0
  347. for args in info.args:
  348. phandle_cell = prop.value[pos]
  349. phandle = fdt_util.fdt32_to_cpu(phandle_cell)
  350. target_node = self._fdt.phandle_to_node[phandle]
  351. node.phandles.add(target_node)
  352. pos += 1 + args
  353. def generate_structs(self, structs):
  354. """Generate struct defintions for the platform data
  355. This writes out the body of a header file consisting of structure
  356. definitions for node in self._valid_nodes. See the documentation in
  357. README.of-plat for more information.
  358. """
  359. self.out_header()
  360. self.out('#include <stdbool.h>\n')
  361. self.out('#include <linux/libfdt.h>\n')
  362. # Output the struct definition
  363. for name in sorted(structs):
  364. self.out('struct %s%s {\n' % (STRUCT_PREFIX, name))
  365. for pname in sorted(structs[name]):
  366. prop = structs[name][pname]
  367. info = self.get_phandle_argc(prop, structs[name])
  368. if info:
  369. # For phandles, include a reference to the target
  370. struct_name = 'struct phandle_%d_arg' % info.max_args
  371. self.out('\t%s%s[%d]' % (tab_to(2, struct_name),
  372. conv_name_to_c(prop.name),
  373. len(info.args)))
  374. else:
  375. ptype = TYPE_NAMES[prop.type]
  376. self.out('\t%s%s' % (tab_to(2, ptype),
  377. conv_name_to_c(prop.name)))
  378. if isinstance(prop.value, list):
  379. self.out('[%d]' % len(prop.value))
  380. self.out(';\n')
  381. self.out('};\n')
  382. for alias, struct_name in self._aliases.iteritems():
  383. self.out('#define %s%s %s%s\n'% (STRUCT_PREFIX, alias,
  384. STRUCT_PREFIX, struct_name))
  385. def output_node(self, node):
  386. """Output the C code for a node
  387. Args:
  388. node: node to output
  389. """
  390. struct_name, _ = get_compat_name(node)
  391. var_name = conv_name_to_c(node.name)
  392. self.buf('static struct %s%s %s%s = {\n' %
  393. (STRUCT_PREFIX, struct_name, VAL_PREFIX, var_name))
  394. for pname, prop in node.props.items():
  395. if pname in PROP_IGNORE_LIST or pname[0] == '#':
  396. continue
  397. member_name = conv_name_to_c(prop.name)
  398. self.buf('\t%s= ' % tab_to(3, '.' + member_name))
  399. # Special handling for lists
  400. if isinstance(prop.value, list):
  401. self.buf('{')
  402. vals = []
  403. # For phandles, output a reference to the platform data
  404. # of the target node.
  405. info = self.get_phandle_argc(prop, node.name)
  406. if info:
  407. # Process the list as pairs of (phandle, id)
  408. pos = 0
  409. for args in info.args:
  410. phandle_cell = prop.value[pos]
  411. phandle = fdt_util.fdt32_to_cpu(phandle_cell)
  412. target_node = self._fdt.phandle_to_node[phandle]
  413. name = conv_name_to_c(target_node.name)
  414. arg_values = []
  415. for i in range(args):
  416. arg_values.append(str(fdt_util.fdt32_to_cpu(prop.value[pos + 1 + i])))
  417. pos += 1 + args
  418. vals.append('\t{&%s%s, {%s}}' % (VAL_PREFIX, name,
  419. ', '.join(arg_values)))
  420. for val in vals:
  421. self.buf('\n\t\t%s,' % val)
  422. else:
  423. for val in prop.value:
  424. vals.append(get_value(prop.type, val))
  425. # Put 8 values per line to avoid very long lines.
  426. for i in xrange(0, len(vals), 8):
  427. if i:
  428. self.buf(',\n\t\t')
  429. self.buf(', '.join(vals[i:i + 8]))
  430. self.buf('}')
  431. else:
  432. self.buf(get_value(prop.type, prop.value))
  433. self.buf(',\n')
  434. self.buf('};\n')
  435. # Add a device declaration
  436. self.buf('U_BOOT_DEVICE(%s) = {\n' % var_name)
  437. self.buf('\t.name\t\t= "%s",\n' % struct_name)
  438. self.buf('\t.platdata\t= &%s%s,\n' % (VAL_PREFIX, var_name))
  439. self.buf('\t.platdata_size\t= sizeof(%s%s),\n' % (VAL_PREFIX, var_name))
  440. self.buf('};\n')
  441. self.buf('\n')
  442. self.out(''.join(self.get_buf()))
  443. def generate_tables(self):
  444. """Generate device defintions for the platform data
  445. This writes out C platform data initialisation data and
  446. U_BOOT_DEVICE() declarations for each valid node. Where a node has
  447. multiple compatible strings, a #define is used to make them equivalent.
  448. See the documentation in doc/driver-model/of-plat.txt for more
  449. information.
  450. """
  451. self.out_header()
  452. self.out('#include <common.h>\n')
  453. self.out('#include <dm.h>\n')
  454. self.out('#include <dt-structs.h>\n')
  455. self.out('\n')
  456. nodes_to_output = list(self._valid_nodes)
  457. # Keep outputing nodes until there is none left
  458. while nodes_to_output:
  459. node = nodes_to_output[0]
  460. # Output all the node's dependencies first
  461. for req_node in node.phandles:
  462. if req_node in nodes_to_output:
  463. self.output_node(req_node)
  464. nodes_to_output.remove(req_node)
  465. self.output_node(node)
  466. nodes_to_output.remove(node)
  467. def run_steps(args, dtb_file, include_disabled, output):
  468. """Run all the steps of the dtoc tool
  469. Args:
  470. args: List of non-option arguments provided to the problem
  471. dtb_file: Filename of dtb file to process
  472. include_disabled: True to include disabled nodes
  473. output: Name of output file
  474. """
  475. if not args:
  476. raise ValueError('Please specify a command: struct, platdata')
  477. plat = DtbPlatdata(dtb_file, include_disabled)
  478. plat.scan_dtb()
  479. plat.scan_tree()
  480. plat.scan_reg_sizes()
  481. plat.setup_output(output)
  482. structs = plat.scan_structs()
  483. plat.scan_phandles()
  484. for cmd in args[0].split(','):
  485. if cmd == 'struct':
  486. plat.generate_structs(structs)
  487. elif cmd == 'platdata':
  488. plat.generate_tables()
  489. else:
  490. raise ValueError("Unknown command '%s': (use: struct, platdata)" %
  491. cmd)