dtb_platdata.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. val = prop.value
  177. if not isinstance(val, list):
  178. val = [val]
  179. i = 0
  180. max_args = 0
  181. args = []
  182. while i < len(val):
  183. phandle = fdt_util.fdt32_to_cpu(val[i])
  184. target = self._fdt.phandle_to_node.get(phandle)
  185. if not target:
  186. raise ValueError("Cannot parse '%s' in node '%s'" %
  187. (prop.name, node_name))
  188. prop_name = '#clock-cells'
  189. cells = target.props.get(prop_name)
  190. if not cells:
  191. raise ValueError("Node '%s' has no '%s' property" %
  192. (target.name, prop_name))
  193. num_args = fdt_util.fdt32_to_cpu(cells.value)
  194. max_args = max(max_args, num_args)
  195. args.append(num_args)
  196. i += 1 + num_args
  197. return PhandleInfo(max_args, args)
  198. return None
  199. def scan_dtb(self):
  200. """Scan the device tree to obtain a tree of nodes and properties
  201. Once this is done, self._fdt.GetRoot() can be called to obtain the
  202. device tree root node, and progress from there.
  203. """
  204. self._fdt = fdt.FdtScan(self._dtb_fname)
  205. def scan_node(self, root):
  206. """Scan a node and subnodes to build a tree of node and phandle info
  207. This adds each node to self._valid_nodes.
  208. Args:
  209. root: Root node for scan
  210. """
  211. for node in root.subnodes:
  212. if 'compatible' in node.props:
  213. status = node.props.get('status')
  214. if (not self._include_disabled and not status or
  215. status.value != 'disabled'):
  216. self._valid_nodes.append(node)
  217. # recurse to handle any subnodes
  218. self.scan_node(node)
  219. def scan_tree(self):
  220. """Scan the device tree for useful information
  221. This fills in the following properties:
  222. _valid_nodes: A list of nodes we wish to consider include in the
  223. platform data
  224. """
  225. self._valid_nodes = []
  226. return self.scan_node(self._fdt.GetRoot())
  227. @staticmethod
  228. def get_num_cells(node):
  229. """Get the number of cells in addresses and sizes for this node
  230. Args:
  231. node: Node to check
  232. Returns:
  233. Tuple:
  234. Number of address cells for this node
  235. Number of size cells for this node
  236. """
  237. parent = node.parent
  238. na, ns = 2, 2
  239. if parent:
  240. na_prop = parent.props.get('#address-cells')
  241. ns_prop = parent.props.get('#size-cells')
  242. if na_prop:
  243. na = fdt_util.fdt32_to_cpu(na_prop.value)
  244. if ns_prop:
  245. ns = fdt_util.fdt32_to_cpu(ns_prop.value)
  246. return na, ns
  247. def scan_reg_sizes(self):
  248. """Scan for 64-bit 'reg' properties and update the values
  249. This finds 'reg' properties with 64-bit data and converts the value to
  250. an array of 64-values. This allows it to be output in a way that the
  251. C code can read.
  252. """
  253. for node in self._valid_nodes:
  254. reg = node.props.get('reg')
  255. if not reg:
  256. continue
  257. na, ns = self.get_num_cells(node)
  258. total = na + ns
  259. if reg.type != fdt.TYPE_INT:
  260. raise ValueError("Node '%s' reg property is not an int")
  261. if len(reg.value) % total:
  262. raise ValueError("Node '%s' reg property has %d cells "
  263. 'which is not a multiple of na + ns = %d + %d)' %
  264. (node.name, len(reg.value), na, ns))
  265. reg.na = na
  266. reg.ns = ns
  267. if na != 1 or ns != 1:
  268. reg.type = fdt.TYPE_INT64
  269. i = 0
  270. new_value = []
  271. val = reg.value
  272. if not isinstance(val, list):
  273. val = [val]
  274. while i < len(val):
  275. addr = fdt_util.fdt_cells_to_cpu(val[i:], reg.na)
  276. i += na
  277. size = fdt_util.fdt_cells_to_cpu(val[i:], reg.ns)
  278. i += ns
  279. new_value += [addr, size]
  280. reg.value = new_value
  281. def scan_structs(self):
  282. """Scan the device tree building up the C structures we will use.
  283. Build a dict keyed by C struct name containing a dict of Prop
  284. object for each struct field (keyed by property name). Where the
  285. same struct appears multiple times, try to use the 'widest'
  286. property, i.e. the one with a type which can express all others.
  287. Once the widest property is determined, all other properties are
  288. updated to match that width.
  289. """
  290. structs = {}
  291. for node in self._valid_nodes:
  292. node_name, _ = get_compat_name(node)
  293. fields = {}
  294. # Get a list of all the valid properties in this node.
  295. for name, prop in node.props.items():
  296. if name not in PROP_IGNORE_LIST and name[0] != '#':
  297. fields[name] = copy.deepcopy(prop)
  298. # If we've seen this node_name before, update the existing struct.
  299. if node_name in structs:
  300. struct = structs[node_name]
  301. for name, prop in fields.items():
  302. oldprop = struct.get(name)
  303. if oldprop:
  304. oldprop.Widen(prop)
  305. else:
  306. struct[name] = prop
  307. # Otherwise store this as a new struct.
  308. else:
  309. structs[node_name] = fields
  310. upto = 0
  311. for node in self._valid_nodes:
  312. node_name, _ = get_compat_name(node)
  313. struct = structs[node_name]
  314. for name, prop in node.props.items():
  315. if name not in PROP_IGNORE_LIST and name[0] != '#':
  316. prop.Widen(struct[name])
  317. upto += 1
  318. struct_name, aliases = get_compat_name(node)
  319. for alias in aliases:
  320. self._aliases[alias] = struct_name
  321. return structs
  322. def scan_phandles(self):
  323. """Figure out what phandles each node uses
  324. We need to be careful when outputing nodes that use phandles since
  325. they must come after the declaration of the phandles in the C file.
  326. Otherwise we get a compiler error since the phandle struct is not yet
  327. declared.
  328. This function adds to each node a list of phandle nodes that the node
  329. depends on. This allows us to output things in the right order.
  330. """
  331. for node in self._valid_nodes:
  332. node.phandles = set()
  333. for pname, prop in node.props.items():
  334. if pname in PROP_IGNORE_LIST or pname[0] == '#':
  335. continue
  336. info = self.get_phandle_argc(prop, node.name)
  337. if info:
  338. if not isinstance(prop.value, list):
  339. prop.value = [prop.value]
  340. # Process the list as pairs of (phandle, id)
  341. pos = 0
  342. for args in info.args:
  343. phandle_cell = prop.value[pos]
  344. phandle = fdt_util.fdt32_to_cpu(phandle_cell)
  345. target_node = self._fdt.phandle_to_node[phandle]
  346. node.phandles.add(target_node)
  347. pos += 1 + args
  348. def generate_structs(self, structs):
  349. """Generate struct defintions for the platform data
  350. This writes out the body of a header file consisting of structure
  351. definitions for node in self._valid_nodes. See the documentation in
  352. README.of-plat for more information.
  353. """
  354. self.out_header()
  355. self.out('#include <stdbool.h>\n')
  356. self.out('#include <linux/libfdt.h>\n')
  357. # Output the struct definition
  358. for name in sorted(structs):
  359. self.out('struct %s%s {\n' % (STRUCT_PREFIX, name))
  360. for pname in sorted(structs[name]):
  361. prop = structs[name][pname]
  362. info = self.get_phandle_argc(prop, structs[name])
  363. if info:
  364. # For phandles, include a reference to the target
  365. struct_name = 'struct phandle_%d_arg' % info.max_args
  366. self.out('\t%s%s[%d]' % (tab_to(2, struct_name),
  367. conv_name_to_c(prop.name),
  368. len(info.args)))
  369. else:
  370. ptype = TYPE_NAMES[prop.type]
  371. self.out('\t%s%s' % (tab_to(2, ptype),
  372. conv_name_to_c(prop.name)))
  373. if isinstance(prop.value, list):
  374. self.out('[%d]' % len(prop.value))
  375. self.out(';\n')
  376. self.out('};\n')
  377. for alias, struct_name in self._aliases.iteritems():
  378. self.out('#define %s%s %s%s\n'% (STRUCT_PREFIX, alias,
  379. STRUCT_PREFIX, struct_name))
  380. def output_node(self, node):
  381. """Output the C code for a node
  382. Args:
  383. node: node to output
  384. """
  385. struct_name, _ = get_compat_name(node)
  386. var_name = conv_name_to_c(node.name)
  387. self.buf('static struct %s%s %s%s = {\n' %
  388. (STRUCT_PREFIX, struct_name, VAL_PREFIX, var_name))
  389. for pname, prop in node.props.items():
  390. if pname in PROP_IGNORE_LIST or pname[0] == '#':
  391. continue
  392. member_name = conv_name_to_c(prop.name)
  393. self.buf('\t%s= ' % tab_to(3, '.' + member_name))
  394. # Special handling for lists
  395. if isinstance(prop.value, list):
  396. self.buf('{')
  397. vals = []
  398. # For phandles, output a reference to the platform data
  399. # of the target node.
  400. info = self.get_phandle_argc(prop, node.name)
  401. if info:
  402. # Process the list as pairs of (phandle, id)
  403. pos = 0
  404. for args in info.args:
  405. phandle_cell = prop.value[pos]
  406. phandle = fdt_util.fdt32_to_cpu(phandle_cell)
  407. target_node = self._fdt.phandle_to_node[phandle]
  408. name = conv_name_to_c(target_node.name)
  409. arg_values = []
  410. for i in range(args):
  411. arg_values.append(str(fdt_util.fdt32_to_cpu(prop.value[pos + 1 + i])))
  412. pos += 1 + args
  413. vals.append('\t{&%s%s, {%s}}' % (VAL_PREFIX, name,
  414. ', '.join(arg_values)))
  415. for val in vals:
  416. self.buf('\n\t\t%s,' % val)
  417. else:
  418. for val in prop.value:
  419. vals.append(get_value(prop.type, val))
  420. # Put 8 values per line to avoid very long lines.
  421. for i in xrange(0, len(vals), 8):
  422. if i:
  423. self.buf(',\n\t\t')
  424. self.buf(', '.join(vals[i:i + 8]))
  425. self.buf('}')
  426. else:
  427. self.buf(get_value(prop.type, prop.value))
  428. self.buf(',\n')
  429. self.buf('};\n')
  430. # Add a device declaration
  431. self.buf('U_BOOT_DEVICE(%s) = {\n' % var_name)
  432. self.buf('\t.name\t\t= "%s",\n' % struct_name)
  433. self.buf('\t.platdata\t= &%s%s,\n' % (VAL_PREFIX, var_name))
  434. self.buf('\t.platdata_size\t= sizeof(%s%s),\n' % (VAL_PREFIX, var_name))
  435. self.buf('};\n')
  436. self.buf('\n')
  437. self.out(''.join(self.get_buf()))
  438. def generate_tables(self):
  439. """Generate device defintions for the platform data
  440. This writes out C platform data initialisation data and
  441. U_BOOT_DEVICE() declarations for each valid node. Where a node has
  442. multiple compatible strings, a #define is used to make them equivalent.
  443. See the documentation in doc/driver-model/of-plat.txt for more
  444. information.
  445. """
  446. self.out_header()
  447. self.out('#include <common.h>\n')
  448. self.out('#include <dm.h>\n')
  449. self.out('#include <dt-structs.h>\n')
  450. self.out('\n')
  451. nodes_to_output = list(self._valid_nodes)
  452. # Keep outputing nodes until there is none left
  453. while nodes_to_output:
  454. node = nodes_to_output[0]
  455. # Output all the node's dependencies first
  456. for req_node in node.phandles:
  457. if req_node in nodes_to_output:
  458. self.output_node(req_node)
  459. nodes_to_output.remove(req_node)
  460. self.output_node(node)
  461. nodes_to_output.remove(node)
  462. def run_steps(args, dtb_file, include_disabled, output):
  463. """Run all the steps of the dtoc tool
  464. Args:
  465. args: List of non-option arguments provided to the problem
  466. dtb_file: Filename of dtb file to process
  467. include_disabled: True to include disabled nodes
  468. output: Name of output file
  469. """
  470. if not args:
  471. raise ValueError('Please specify a command: struct, platdata')
  472. plat = DtbPlatdata(dtb_file, include_disabled)
  473. plat.scan_dtb()
  474. plat.scan_tree()
  475. plat.scan_reg_sizes()
  476. plat.setup_output(output)
  477. structs = plat.scan_structs()
  478. plat.scan_phandles()
  479. for cmd in args[0].split(','):
  480. if cmd == 'struct':
  481. plat.generate_structs(structs)
  482. elif cmd == 'platdata':
  483. plat.generate_tables()
  484. else:
  485. raise ValueError("Unknown command '%s': (use: struct, platdata)" %
  486. cmd)