blob: 99cf392e0576673e0772a98a9007f2505e57bab4 [file] [log] [blame]
Martí Bolívardc85edd2020-02-28 15:26:52 -08001#!/usr/bin/env python3
2
3# Copyright (c) 2019 - 2020 Nordic Semiconductor ASA
4# Copyright (c) 2019 Linaro Limited
5# SPDX-License-Identifier: BSD-3-Clause
6
7# This script uses edtlib to generate a header file from a devicetree
8# (.dts) file. Information from binding files in YAML format is used
9# as well.
10#
11# Bindings are files that describe devicetree nodes. Devicetree nodes are
12# usually mapped to bindings via their 'compatible = "..."' property.
13#
14# See Zephyr's Devicetree user guide for details.
15#
16# Note: Do not access private (_-prefixed) identifiers from edtlib here (and
17# also note that edtlib is not meant to expose the dtlib API directly).
18# Instead, think of what API you need, and add it as a public documented API in
19# edtlib. This will keep this script simple.
20
21import argparse
Martí Bolívara3fae2f2020-03-25 14:18:27 -070022from collections import defaultdict
Martí Bolívar09858492020-12-08 09:41:49 -080023import logging
Martí Bolívardc85edd2020-02-28 15:26:52 -080024import os
25import pathlib
Martí Bolívar533f4512020-07-01 10:43:43 -070026import pickle
Martí Bolívardc85edd2020-02-28 15:26:52 -080027import re
28import sys
29
Martí Bolívar53328472021-03-26 16:18:58 -070030sys.path.append(os.path.join(os.path.dirname(__file__), 'python-devicetree',
31 'src'))
32
33from devicetree import edtlib
Martí Bolívardc85edd2020-02-28 15:26:52 -080034
Martí Bolívar9c229a42021-04-14 15:26:42 -070035# The set of binding types whose values can be iterated over with
36# DT_FOREACH_PROP_ELEM(). If you change this, make sure to update the
37# doxygen string for that macro.
38FOREACH_PROP_ELEM_TYPES = set(['string', 'array', 'uint8-array', 'string-array',
39 'phandles', 'phandle-array'])
40
Martí Bolívar09858492020-12-08 09:41:49 -080041class LogFormatter(logging.Formatter):
42 '''A log formatter that prints the level name in lower case,
43 for compatibility with earlier versions of edtlib.'''
44
45 def __init__(self):
46 super().__init__(fmt='%(levelnamelower)s: %(message)s')
47
48 def format(self, record):
49 record.levelnamelower = record.levelname.lower()
50 return super().format(record)
51
Martí Bolívardc85edd2020-02-28 15:26:52 -080052def main():
53 global header_file
Kumar Galabd973782020-05-06 20:54:29 -050054 global flash_area_num
Martí Bolívardc85edd2020-02-28 15:26:52 -080055
56 args = parse_args()
57
Martí Bolívar09858492020-12-08 09:41:49 -080058 setup_edtlib_logging()
59
Martí Bolívar85837e12021-08-19 11:09:05 -070060 vendor_prefixes = {}
61 for prefixes_file in args.vendor_prefixes:
62 vendor_prefixes.update(edtlib.load_vendor_prefixes_txt(prefixes_file))
Martí Bolívarf261d772021-05-18 15:09:49 -070063
Martí Bolívardc85edd2020-02-28 15:26:52 -080064 try:
65 edt = edtlib.EDT(args.dts, args.bindings_dirs,
66 # Suppress this warning if it's suppressed in dtc
67 warn_reg_unit_address_mismatch=
Kumar Galabc48f1c2020-05-01 12:33:00 -050068 "-Wno-simple_bus_reg" not in args.dtc_flags,
Peter Bigot932532e2020-09-02 05:05:19 -050069 default_prop_types=True,
Martí Bolívardf5a55c2021-02-14 17:52:57 -080070 infer_binding_for_paths=["/zephyr,user"],
Martí Bolívarc4079e42021-07-30 15:43:27 -070071 werror=args.edtlib_Werror,
Martí Bolívarf261d772021-05-18 15:09:49 -070072 vendor_prefixes=vendor_prefixes)
Martí Bolívardc85edd2020-02-28 15:26:52 -080073 except edtlib.EDTError as e:
74 sys.exit(f"devicetree error: {e}")
75
Kumar Galabd973782020-05-06 20:54:29 -050076 flash_area_num = 0
77
Martí Bolívardc85edd2020-02-28 15:26:52 -080078 # Save merged DTS source, as a debugging aid
79 with open(args.dts_out, "w", encoding="utf-8") as f:
80 print(edt.dts_source, file=f)
81
Martí Bolívare96ca542020-05-07 12:07:02 -070082 # The raw index into edt.compat2nodes[compat] is used for node
83 # instance numbering within a compatible.
84 #
85 # As a way to satisfy people's intuitions about instance numbers,
86 # though, we sort this list so enabled instances come first.
87 #
88 # This might look like a hack, but it keeps drivers and
89 # applications which don't use instance numbers carefully working
90 # as expected, since e.g. instance number 0 is always the
91 # singleton instance if there's just one enabled node of a
92 # particular compatible.
93 #
94 # This doesn't violate any devicetree.h API guarantees about
95 # instance ordering, since we make no promises that instance
96 # numbers are stable across builds.
97 for compat, nodes in edt.compat2nodes.items():
98 edt.compat2nodes[compat] = sorted(
99 nodes, key=lambda node: 0 if node.status == "okay" else 1)
100
Martí Bolívar533f4512020-07-01 10:43:43 -0700101 # Create the generated header.
Martí Bolívardc85edd2020-02-28 15:26:52 -0800102 with open(args.header_out, "w", encoding="utf-8") as header_file:
103 write_top_comment(edt)
104
Dominik Ermelba8b74d2020-04-17 06:32:28 +0000105 # populate all z_path_id first so any children references will
106 # work correctly.
Martí Bolívardc85edd2020-02-28 15:26:52 -0800107 for node in sorted(edt.nodes, key=lambda node: node.dep_ordinal):
Martí Bolívar186bace2020-04-08 15:02:18 -0700108 node.z_path_id = node_z_path_id(node)
Dominik Ermelba8b74d2020-04-17 06:32:28 +0000109
110 for node in sorted(edt.nodes, key=lambda node: node.dep_ordinal):
Martí Bolívardc85edd2020-02-28 15:26:52 -0800111 write_node_comment(node)
112
Kumar Gala270a05f2021-02-24 11:28:21 -0600113 out_comment("Node's full path:")
Martí Bolívar00ffc7e2020-12-13 12:27:04 -0800114 out_dt_define(f"{node.z_path_id}_PATH", f'"{escape(node.path)}"')
115
Kumar Gala4aac9082021-02-24 10:44:07 -0600116 out_comment("Node's name with unit-address:")
117 out_dt_define(f"{node.z_path_id}_FULL_NAME",
118 f'"{escape(node.name)}"')
119
Martí Bolívar6e273432020-04-08 15:04:15 -0700120 if node.parent is not None:
121 out_comment(f"Node parent ({node.parent.path}) identifier:")
122 out_dt_define(f"{node.z_path_id}_PARENT",
123 f"DT_{node.parent.z_path_id}")
124
Dominik Ermelba8b74d2020-04-17 06:32:28 +0000125 write_child_functions(node)
Hou Zhiqiang0700a242021-04-26 16:22:38 +0800126 write_child_functions_status_okay(node)
Martí Bolívar305379e2020-06-08 14:59:19 -0700127 write_dep_info(node)
Martí Bolívardc85edd2020-02-28 15:26:52 -0800128 write_idents_and_existence(node)
129 write_bus(node)
130 write_special_props(node)
131 write_vanilla_props(node)
132
133 write_chosen(edt)
Martí Bolívara3fae2f2020-03-25 14:18:27 -0700134 write_global_compat_info(edt)
Martí Bolívardc85edd2020-02-28 15:26:52 -0800135
Kumar Gala98b6e4f2021-01-13 11:06:46 -0600136 write_device_extern_header(args.device_header_out, edt)
137
Martí Bolívar533f4512020-07-01 10:43:43 -0700138 if args.edt_pickle_out:
139 write_pickled_edt(edt, args.edt_pickle_out)
140
Kumar Gala98b6e4f2021-01-13 11:06:46 -0600141
142def write_device_extern_header(device_header_out, edt):
143 # Generate header that will extern devicetree struct device's
144
145 with open(device_header_out, "w", encoding="utf-8") as dev_header_file:
146 print("#ifndef DEVICE_EXTERN_GEN_H", file=dev_header_file)
147 print("#define DEVICE_EXTERN_GEN_H", file=dev_header_file)
148 print("", file=dev_header_file)
149 print("#ifdef __cplusplus", file=dev_header_file)
150 print('extern "C" {', file=dev_header_file)
151 print("#endif", file=dev_header_file)
152 print("", file=dev_header_file)
153
154 for node in sorted(edt.nodes, key=lambda node: node.dep_ordinal):
Peter Bigotf91e9fb2021-01-23 07:56:09 -0600155 print(f"extern const struct device DEVICE_DT_NAME_GET(DT_{node.z_path_id}); /* dts_ord_{node.dep_ordinal} */",
156 file=dev_header_file)
Kumar Gala98b6e4f2021-01-13 11:06:46 -0600157
158 print("", file=dev_header_file)
159 print("#ifdef __cplusplus", file=dev_header_file)
160 print("}", file=dev_header_file)
161 print("#endif", file=dev_header_file)
162 print("", file=dev_header_file)
163 print("#endif /* DEVICE_EXTERN_GEN_H */", file=dev_header_file)
164
165
Martí Bolívar09858492020-12-08 09:41:49 -0800166def setup_edtlib_logging():
167 # The edtlib module emits logs using the standard 'logging' module.
168 # Configure it so that warnings and above are printed to stderr,
169 # using the LogFormatter class defined above to format each message.
170
171 handler = logging.StreamHandler(sys.stderr)
172 handler.setFormatter(LogFormatter())
173
174 logger = logging.getLogger('edtlib')
175 logger.setLevel(logging.WARNING)
176 logger.addHandler(handler)
Martí Bolívardc85edd2020-02-28 15:26:52 -0800177
Martí Bolívar186bace2020-04-08 15:02:18 -0700178def node_z_path_id(node):
179 # Return the node specific bit of the node's path identifier:
180 #
181 # - the root node's path "/" has path identifier "N"
182 # - "/foo" has "N_S_foo"
183 # - "/foo/bar" has "N_S_foo_S_bar"
184 # - "/foo/bar@123" has "N_S_foo_S_bar_123"
185 #
186 # This is used throughout this file to generate macros related to
187 # the node.
188
189 components = ["N"]
190 if node.parent is not None:
191 components.extend(f"S_{str2ident(component)}" for component in
192 node.path.split("/")[1:])
193
194 return "_".join(components)
195
Martí Bolívardc85edd2020-02-28 15:26:52 -0800196def parse_args():
197 # Returns parsed command-line arguments
198
199 parser = argparse.ArgumentParser()
200 parser.add_argument("--dts", required=True, help="DTS file")
201 parser.add_argument("--dtc-flags",
202 help="'dtc' devicetree compiler flags, some of which "
203 "might be respected here")
204 parser.add_argument("--bindings-dirs", nargs='+', required=True,
205 help="directory with bindings in YAML format, "
206 "we allow multiple")
207 parser.add_argument("--header-out", required=True,
208 help="path to write header to")
209 parser.add_argument("--dts-out", required=True,
210 help="path to write merged DTS source code to (e.g. "
211 "as a debugging aid)")
Kumar Gala98b6e4f2021-01-13 11:06:46 -0600212 parser.add_argument("--device-header-out", required=True,
213 help="path to write device struct extern header to")
Martí Bolívar533f4512020-07-01 10:43:43 -0700214 parser.add_argument("--edt-pickle-out",
215 help="path to write pickled edtlib.EDT object to")
Martí Bolívar85837e12021-08-19 11:09:05 -0700216 parser.add_argument("--vendor-prefixes", action='append', default=[],
217 help="vendor-prefixes.txt path; used for validation; "
218 "may be given multiple times")
Martí Bolívarc4079e42021-07-30 15:43:27 -0700219 parser.add_argument("--edtlib-Werror", action="store_true",
220 help="if set, edtlib-specific warnings become errors. "
221 "(this does not apply to warnings shared "
222 "with dtc.)")
Martí Bolívardc85edd2020-02-28 15:26:52 -0800223
224 return parser.parse_args()
225
226
227def write_top_comment(edt):
228 # Writes an overview comment with misc. info at the top of the header and
229 # configuration file
230
231 s = f"""\
232Generated by gen_defines.py
233
234DTS input file:
235 {edt.dts_path}
236
237Directories with bindings:
238 {", ".join(map(relativize, edt.bindings_dirs))}
239
Martí Bolívar305379e2020-06-08 14:59:19 -0700240Node dependency ordering (ordinal and path):
Martí Bolívardc85edd2020-02-28 15:26:52 -0800241"""
242
Martí Bolívarb6db2012020-08-24 13:33:53 -0700243 for scc in edt.scc_order:
Martí Bolívardc85edd2020-02-28 15:26:52 -0800244 if len(scc) > 1:
245 err("cycle in devicetree involving "
246 + ", ".join(node.path for node in scc))
247 s += f" {scc[0].dep_ordinal:<3} {scc[0].path}\n"
248
249 s += """
250Definitions derived from these nodes in dependency order are next,
251followed by /chosen nodes.
252"""
253
254 out_comment(s, blank_before=False)
255
256
257def write_node_comment(node):
258 # Writes a comment describing 'node' to the header and configuration file
259
260 s = f"""\
Martí Bolívarb6e6ba02020-04-08 15:09:46 -0700261Devicetree node: {node.path}
262
Martí Bolívar305379e2020-06-08 14:59:19 -0700263Node identifier: DT_{node.z_path_id}
Martí Bolívardc85edd2020-02-28 15:26:52 -0800264"""
265
266 if node.matching_compat:
Peter Bigot932532e2020-09-02 05:05:19 -0500267 if node.binding_path:
268 s += f"""
Martí Bolívardc85edd2020-02-28 15:26:52 -0800269Binding (compatible = {node.matching_compat}):
270 {relativize(node.binding_path)}
271"""
Peter Bigot932532e2020-09-02 05:05:19 -0500272 else:
273 s += f"""
274Binding (compatible = {node.matching_compat}):
275 No yaml (bindings inferred from properties)
276"""
Martí Bolívardc85edd2020-02-28 15:26:52 -0800277
Martí Bolívardc85edd2020-02-28 15:26:52 -0800278 if node.description:
Martí Bolívarf7d33f22020-10-30 17:45:27 -0700279 # We used to put descriptions in the generated file, but
280 # devicetree bindings now have pages in the HTML
281 # documentation. Let users who are accustomed to digging
282 # around in the generated file where to find the descriptions
283 # now.
284 #
285 # Keeping them here would mean that the descriptions
286 # themselves couldn't contain C multi-line comments, which is
287 # inconvenient when we want to do things like quote snippets
288 # of .dtsi files within the descriptions, or otherwise
289 # include the string "*/".
290 s += ("\n(Descriptions have moved to the Devicetree Bindings Index\n"
291 "in the documentation.)\n")
Martí Bolívardc85edd2020-02-28 15:26:52 -0800292
293 out_comment(s)
294
295
296def relativize(path):
297 # If 'path' is within $ZEPHYR_BASE, returns it relative to $ZEPHYR_BASE,
298 # with a "$ZEPHYR_BASE/..." hint at the start of the string. Otherwise,
299 # returns 'path' unchanged.
300
301 zbase = os.getenv("ZEPHYR_BASE")
302 if zbase is None:
303 return path
304
305 try:
306 return str("$ZEPHYR_BASE" / pathlib.Path(path).relative_to(zbase))
307 except ValueError:
308 # Not within ZEPHYR_BASE
309 return path
310
311
312def write_idents_and_existence(node):
313 # Writes macros related to the node's aliases, labels, etc.,
314 # as well as existence flags.
315
316 # Aliases
317 idents = [f"N_ALIAS_{str2ident(alias)}" for alias in node.aliases]
318 # Instances
319 for compat in node.compats:
Martí Bolívar7e0eed92020-05-06 11:23:07 -0700320 instance_no = node.edt.compat2nodes[compat].index(node)
Martí Bolívardc85edd2020-02-28 15:26:52 -0800321 idents.append(f"N_INST_{instance_no}_{str2ident(compat)}")
322 # Node labels
323 idents.extend(f"N_NODELABEL_{str2ident(label)}" for label in node.labels)
324
325 out_comment("Existence and alternate IDs:")
326 out_dt_define(node.z_path_id + "_EXISTS", 1)
327
328 # Only determine maxlen if we have any idents
329 if idents:
330 maxlen = max(len("DT_" + ident) for ident in idents)
331 for ident in idents:
332 out_dt_define(ident, "DT_" + node.z_path_id, width=maxlen)
333
334
335def write_bus(node):
336 # Macros about the node's bus controller, if there is one
337
338 bus = node.bus_node
339 if not bus:
340 return
341
342 if not bus.label:
343 err(f"missing 'label' property on bus node {bus!r}")
344
345 out_comment(f"Bus info (controller: '{bus.path}', type: '{node.on_bus}')")
346 out_dt_define(f"{node.z_path_id}_BUS_{str2ident(node.on_bus)}", 1)
347 out_dt_define(f"{node.z_path_id}_BUS", f"DT_{bus.z_path_id}")
348
349
350def write_special_props(node):
351 # Writes required macros for special case properties, when the
352 # data cannot otherwise be obtained from write_vanilla_props()
353 # results
354
Martí Bolívardc85edd2020-02-28 15:26:52 -0800355 # Macros that are special to the devicetree specification
Martí Bolívar7f69a032021-08-11 15:14:51 -0700356 out_comment("Macros for properties that are special in the specification:")
Martí Bolívardc85edd2020-02-28 15:26:52 -0800357 write_regs(node)
358 write_interrupts(node)
359 write_compatibles(node)
Martí Bolívar7e0eed92020-05-06 11:23:07 -0700360 write_status(node)
Martí Bolívardc85edd2020-02-28 15:26:52 -0800361
Martí Bolívar7f69a032021-08-11 15:14:51 -0700362 # Macros that are special to bindings inherited from Linux, which
363 # we can't capture with the current bindings language.
Martí Bolívar9df04932021-08-11 15:43:24 -0700364 write_pinctrls(node)
Martí Bolívar7f69a032021-08-11 15:14:51 -0700365 write_fixed_partitions(node)
Martí Bolívardc85edd2020-02-28 15:26:52 -0800366
367def write_regs(node):
368 # reg property: edtlib knows the right #address-cells and
369 # #size-cells, and can therefore pack the register base addresses
370 # and sizes correctly
371
372 idx_vals = []
373 name_vals = []
374 path_id = node.z_path_id
375
376 if node.regs is not None:
377 idx_vals.append((f"{path_id}_REG_NUM", len(node.regs)))
378
379 for i, reg in enumerate(node.regs):
Kumar Gala4e2ad002020-04-14 14:27:20 -0500380 idx_vals.append((f"{path_id}_REG_IDX_{i}_EXISTS", 1))
Martí Bolívardc85edd2020-02-28 15:26:52 -0800381 if reg.addr is not None:
382 idx_macro = f"{path_id}_REG_IDX_{i}_VAL_ADDRESS"
383 idx_vals.append((idx_macro,
384 f"{reg.addr} /* {hex(reg.addr)} */"))
385 if reg.name:
386 name_macro = f"{path_id}_REG_NAME_{reg.name}_VAL_ADDRESS"
387 name_vals.append((name_macro, f"DT_{idx_macro}"))
388
389 if reg.size is not None:
390 idx_macro = f"{path_id}_REG_IDX_{i}_VAL_SIZE"
391 idx_vals.append((idx_macro,
392 f"{reg.size} /* {hex(reg.size)} */"))
393 if reg.name:
394 name_macro = f"{path_id}_REG_NAME_{reg.name}_VAL_SIZE"
395 name_vals.append((name_macro, f"DT_{idx_macro}"))
396
397 for macro, val in idx_vals:
398 out_dt_define(macro, val)
399 for macro, val in name_vals:
400 out_dt_define(macro, val)
401
402def write_interrupts(node):
403 # interrupts property: we have some hard-coded logic for interrupt
404 # mapping here.
405 #
406 # TODO: can we push map_arm_gic_irq_type() and
407 # encode_zephyr_multi_level_irq() out of Python and into C with
408 # macro magic in devicetree.h?
409
410 def map_arm_gic_irq_type(irq, irq_num):
411 # Maps ARM GIC IRQ (type)+(index) combo to linear IRQ number
412 if "type" not in irq.data:
413 err(f"Expected binding for {irq.controller!r} to have 'type' in "
414 "interrupt-cells")
415 irq_type = irq.data["type"]
416
417 if irq_type == 0: # GIC_SPI
418 return irq_num + 32
419 if irq_type == 1: # GIC_PPI
420 return irq_num + 16
421 err(f"Invalid interrupt type specified for {irq!r}")
422
423 def encode_zephyr_multi_level_irq(irq, irq_num):
424 # See doc/reference/kernel/other/interrupts.rst for details
425 # on how this encoding works
426
427 irq_ctrl = irq.controller
428 # Look for interrupt controller parent until we have none
429 while irq_ctrl.interrupts:
430 irq_num = (irq_num + 1) << 8
431 if "irq" not in irq_ctrl.interrupts[0].data:
432 err(f"Expected binding for {irq_ctrl!r} to have 'irq' in "
433 "interrupt-cells")
434 irq_num |= irq_ctrl.interrupts[0].data["irq"]
435 irq_ctrl = irq_ctrl.interrupts[0].controller
436 return irq_num
437
438 idx_vals = []
439 name_vals = []
440 path_id = node.z_path_id
441
442 if node.interrupts is not None:
443 idx_vals.append((f"{path_id}_IRQ_NUM", len(node.interrupts)))
444
445 for i, irq in enumerate(node.interrupts):
446 for cell_name, cell_value in irq.data.items():
447 name = str2ident(cell_name)
448
449 if cell_name == "irq":
450 if "arm,gic" in irq.controller.compats:
451 cell_value = map_arm_gic_irq_type(irq, cell_value)
452 cell_value = encode_zephyr_multi_level_irq(irq, cell_value)
453
Kumar Gala4e2ad002020-04-14 14:27:20 -0500454 idx_vals.append((f"{path_id}_IRQ_IDX_{i}_EXISTS", 1))
Martí Bolívardc85edd2020-02-28 15:26:52 -0800455 idx_macro = f"{path_id}_IRQ_IDX_{i}_VAL_{name}"
456 idx_vals.append((idx_macro, cell_value))
457 idx_vals.append((idx_macro + "_EXISTS", 1))
458 if irq.name:
459 name_macro = \
460 f"{path_id}_IRQ_NAME_{str2ident(irq.name)}_VAL_{name}"
461 name_vals.append((name_macro, f"DT_{idx_macro}"))
462 name_vals.append((name_macro + "_EXISTS", 1))
463
464 for macro, val in idx_vals:
465 out_dt_define(macro, val)
466 for macro, val in name_vals:
467 out_dt_define(macro, val)
468
469
470def write_compatibles(node):
471 # Writes a macro for each of the node's compatibles. We don't care
472 # about whether edtlib / Zephyr's binding language recognizes
473 # them. The compatibles the node provides are what is important.
474
475 for compat in node.compats:
476 out_dt_define(
477 f"{node.z_path_id}_COMPAT_MATCHES_{str2ident(compat)}", 1)
478
479
Dominik Ermelba8b74d2020-04-17 06:32:28 +0000480def write_child_functions(node):
481 # Writes macro that are helpers that will call a macro/function
482 # for each child node.
483
Kumar Gala4a5a90a2020-05-08 12:25:25 -0500484 out_dt_define(f"{node.z_path_id}_FOREACH_CHILD(fn)",
485 " ".join(f"fn(DT_{child.z_path_id})" for child in
486 node.children.values()))
Dominik Ermelba8b74d2020-04-17 06:32:28 +0000487
Arvin Farahmandd0b9c032021-05-06 11:19:29 -0400488 out_dt_define(f"{node.z_path_id}_FOREACH_CHILD_VARGS(fn, ...)",
489 " ".join(f"fn(DT_{child.z_path_id}, __VA_ARGS__)" for child in
490 node.children.values()))
Dominik Ermelba8b74d2020-04-17 06:32:28 +0000491
Hou Zhiqiang0700a242021-04-26 16:22:38 +0800492def write_child_functions_status_okay(node):
Martí Bolívar32528212021-08-11 14:18:16 -0700493 # Writes macros that are helpers that will call a macro/function
Hou Zhiqiang0700a242021-04-26 16:22:38 +0800494 # for each child node with status "okay".
495
496 functions = ''
Arvin Farahmandd0b9c032021-05-06 11:19:29 -0400497 functions_args = ''
Hou Zhiqiang0700a242021-04-26 16:22:38 +0800498 for child in node.children.values():
499 if child.status == "okay":
500 functions = functions + f"fn(DT_{child.z_path_id}) "
Arvin Farahmandd0b9c032021-05-06 11:19:29 -0400501 functions_args = functions_args + f"fn(DT_{child.z_path_id}, " \
502 "__VA_ARGS__) "
Hou Zhiqiang0700a242021-04-26 16:22:38 +0800503
504 out_dt_define(f"{node.z_path_id}_FOREACH_CHILD_STATUS_OKAY(fn)", functions)
Arvin Farahmandd0b9c032021-05-06 11:19:29 -0400505 out_dt_define(f"{node.z_path_id}_FOREACH_CHILD_STATUS_OKAY_VARGS(fn, ...)",
506 functions_args)
Hou Zhiqiang0700a242021-04-26 16:22:38 +0800507
508
Martí Bolívar7e0eed92020-05-06 11:23:07 -0700509def write_status(node):
510 out_dt_define(f"{node.z_path_id}_STATUS_{str2ident(node.status)}", 1)
511
512
Martí Bolívar9df04932021-08-11 15:43:24 -0700513def write_pinctrls(node):
514 # Write special macros for pinctrl-<index> and pinctrl-names properties.
515
516 out_comment("Pin control (pinctrl-<i>, pinctrl-names) properties:")
517
518 out_dt_define(f"{node.z_path_id}_PINCTRL_NUM", len(node.pinctrls))
519
520 if not node.pinctrls:
521 return
522
523 for pc_idx, pinctrl in enumerate(node.pinctrls):
524 out_dt_define(f"{node.z_path_id}_PINCTRL_IDX_{pc_idx}_EXISTS", 1)
525
526 if not pinctrl.name:
527 continue
528
529 name = pinctrl.name_as_token
530
531 # Below we rely on the fact that edtlib ensures the
532 # pinctrl-<pc_idx> properties are contiguous, start from 0,
533 # and contain only phandles.
534 out_dt_define(f"{node.z_path_id}_PINCTRL_IDX_{pc_idx}_TOKEN", name)
535 out_dt_define(f"{node.z_path_id}_PINCTRL_IDX_{pc_idx}_UPPER_TOKEN", name.upper())
536 out_dt_define(f"{node.z_path_id}_PINCTRL_NAME_{name}_EXISTS", 1)
537 out_dt_define(f"{node.z_path_id}_PINCTRL_NAME_{name}_IDX", pc_idx)
538 for idx, ph in enumerate(pinctrl.conf_nodes):
539 out_dt_define(f"{node.z_path_id}_PINCTRL_NAME_{name}_IDX_{idx}_PH",
540 f"DT_{ph.z_path_id}")
541
542
Martí Bolívar7f69a032021-08-11 15:14:51 -0700543def write_fixed_partitions(node):
544 # Macros for child nodes of each fixed-partitions node.
545
546 if not (node.parent and "fixed-partitions" in node.parent.compats):
547 return
548
549 global flash_area_num
550 out_comment("fixed-partitions identifier:")
551 out_dt_define(f"{node.z_path_id}_PARTITION_ID", flash_area_num)
552 flash_area_num += 1
553
554
Martí Bolívardc85edd2020-02-28 15:26:52 -0800555def write_vanilla_props(node):
556 # Writes macros for any and all properties defined in the
557 # "properties" section of the binding for the node.
558 #
559 # This does generate macros for special properties as well, like
560 # regs, etc. Just let that be rather than bothering to add
561 # never-ending amounts of special case code here to skip special
562 # properties. This function's macros can't conflict with
563 # write_special_props() macros, because they're in different
564 # namespaces. Special cases aren't special enough to break the rules.
565
566 macro2val = {}
567 for prop_name, prop in node.props.items():
Martí Bolívar9c229a42021-04-14 15:26:42 -0700568 prop_id = str2ident(prop_name)
569 macro = f"{node.z_path_id}_P_{prop_id}"
Martí Bolívardc85edd2020-02-28 15:26:52 -0800570 val = prop2value(prop)
571 if val is not None:
572 # DT_N_<node-id>_P_<prop-id>
573 macro2val[macro] = val
574
Carlo Caionef4db14f2021-05-17 17:24:27 +0200575 if prop.spec.type == 'string':
576 macro2val[macro + "_STRING_TOKEN"] = prop.val_as_token
577 macro2val[macro + "_STRING_UPPER_TOKEN"] = prop.val_as_token.upper()
578
Martí Bolívardc85edd2020-02-28 15:26:52 -0800579 if prop.enum_index is not None:
580 # DT_N_<node-id>_P_<prop-id>_ENUM_IDX
581 macro2val[macro + "_ENUM_IDX"] = prop.enum_index
Peter Bigot345da782020-12-02 09:04:27 -0600582 spec = prop.spec
583
584 if spec.enum_tokenizable:
585 as_token = prop.val_as_token
586
587 # DT_N_<node-id>_P_<prop-id>_ENUM_TOKEN
588 macro2val[macro + "_ENUM_TOKEN"] = as_token
589
590 if spec.enum_upper_tokenizable:
591 # DT_N_<node-id>_P_<prop-id>_ENUM_UPPER_TOKEN
592 macro2val[macro + "_ENUM_UPPER_TOKEN"] = as_token.upper()
Martí Bolívardc85edd2020-02-28 15:26:52 -0800593
594 if "phandle" in prop.type:
595 macro2val.update(phandle_macros(prop, macro))
596 elif "array" in prop.type:
597 # DT_N_<node-id>_P_<prop-id>_IDX_<i>
Martí Bolívarffc03122020-11-12 20:27:20 -0800598 # DT_N_<node-id>_P_<prop-id>_IDX_<i>_EXISTS
Martí Bolívardc85edd2020-02-28 15:26:52 -0800599 for i, subval in enumerate(prop.val):
600 if isinstance(subval, str):
601 macro2val[macro + f"_IDX_{i}"] = quote_str(subval)
602 else:
603 macro2val[macro + f"_IDX_{i}"] = subval
Martí Bolívarffc03122020-11-12 20:27:20 -0800604 macro2val[macro + f"_IDX_{i}_EXISTS"] = 1
Martí Bolívardc85edd2020-02-28 15:26:52 -0800605
Martí Bolívar9c229a42021-04-14 15:26:42 -0700606 if prop.type in FOREACH_PROP_ELEM_TYPES:
607 # DT_N_<node-id>_P_<prop-id>_FOREACH_PROP_ELEM
608 macro2val[f"{macro}_FOREACH_PROP_ELEM(fn)"] = \
609 ' \\\n\t'.join(f'fn(DT_{node.z_path_id}, {prop_id}, {i})'
610 for i in range(len(prop.val)))
611
Arvin Farahmandd0b9c032021-05-06 11:19:29 -0400612 macro2val[f"{macro}_FOREACH_PROP_ELEM_VARGS(fn, ...)"] = \
613 ' \\\n\t'.join(f'fn(DT_{node.z_path_id}, {prop_id}, {i},'
614 ' __VA_ARGS__)'
615 for i in range(len(prop.val)))
616
Martí Bolívardc85edd2020-02-28 15:26:52 -0800617 plen = prop_len(prop)
618 if plen is not None:
619 # DT_N_<node-id>_P_<prop-id>_LEN
620 macro2val[macro + "_LEN"] = plen
621
622 macro2val[f"{macro}_EXISTS"] = 1
623
624 if macro2val:
625 out_comment("Generic property macros:")
626 for macro, val in macro2val.items():
627 out_dt_define(macro, val)
628 else:
629 out_comment("(No generic property macros)")
630
631
Martí Bolívar305379e2020-06-08 14:59:19 -0700632def write_dep_info(node):
633 # Write dependency-related information about the node.
634
635 def fmt_dep_list(dep_list):
636 if dep_list:
637 # Sort the list by dependency ordinal for predictability.
638 sorted_list = sorted(dep_list, key=lambda node: node.dep_ordinal)
639 return "\\\n\t" + \
640 " \\\n\t".join(f"{n.dep_ordinal}, /* {n.path} */"
641 for n in sorted_list)
642 else:
643 return "/* nothing */"
644
645 out_comment("Node's dependency ordinal:")
646 out_dt_define(f"{node.z_path_id}_ORD", node.dep_ordinal)
647
648 out_comment("Ordinals for what this node depends on directly:")
649 out_dt_define(f"{node.z_path_id}_REQUIRES_ORDS",
650 fmt_dep_list(node.depends_on))
651
652 out_comment("Ordinals for what depends directly on this node:")
653 out_dt_define(f"{node.z_path_id}_SUPPORTS_ORDS",
654 fmt_dep_list(node.required_by))
655
656
Martí Bolívardc85edd2020-02-28 15:26:52 -0800657def prop2value(prop):
658 # Gets the macro value for property 'prop', if there is
659 # a single well-defined C rvalue that it can be represented as.
660 # Returns None if there isn't one.
661
662 if prop.type == "string":
663 return quote_str(prop.val)
664
665 if prop.type == "int":
666 return prop.val
667
668 if prop.type == "boolean":
669 return 1 if prop.val else 0
670
671 if prop.type in ["array", "uint8-array"]:
672 return list2init(f"{val} /* {hex(val)} */" for val in prop.val)
673
674 if prop.type == "string-array":
675 return list2init(quote_str(val) for val in prop.val)
676
677 # phandle, phandles, phandle-array, path, compound: nothing
678 return None
679
680
681def prop_len(prop):
682 # Returns the property's length if and only if we should generate
683 # a _LEN macro for the property. Otherwise, returns None.
684 #
685 # This deliberately excludes reg and interrupts.
686 # While they have array type, their lengths as arrays are
687 # basically nonsense semantically due to #address-cells and
688 # #size-cells for "reg" and #interrupt-cells for "interrupts".
689 #
690 # We have special purpose macros for the number of register blocks
691 # / interrupt specifiers. Excluding them from this list means
692 # DT_PROP_LEN(node_id, ...) fails fast at the devicetree.h layer
693 # with a build error. This forces users to switch to the right
694 # macros.
695
696 if prop.type == "phandle":
697 return 1
698
699 if (prop.type in ["array", "uint8-array", "string-array",
700 "phandles", "phandle-array"] and
701 prop.name not in ["reg", "interrupts"]):
702 return len(prop.val)
703
704 return None
705
706
707def phandle_macros(prop, macro):
708 # Returns a dict of macros for phandle or phandles property 'prop'.
709 #
710 # The 'macro' argument is the N_<node-id>_P_<prop-id> bit.
711 #
712 # These are currently special because we can't serialize their
713 # values without using label properties, which we're trying to get
714 # away from needing in Zephyr. (Label properties are great for
715 # humans, but have drawbacks for code size and boot time.)
716 #
717 # The names look a bit weird to make it easier for devicetree.h
718 # to use the same macros for phandle, phandles, and phandle-array.
719
720 ret = {}
721
722 if prop.type == "phandle":
723 # A phandle is treated as a phandles with fixed length 1.
Kumar Gala7b9fbcd2021-08-12 14:57:49 -0500724 ret[f"{macro}"] = f"DT_{prop.val.z_path_id}"
725 ret[f"{macro}_IDX_0"] = f"DT_{prop.val.z_path_id}"
Martí Bolívardc85edd2020-02-28 15:26:52 -0800726 ret[f"{macro}_IDX_0_PH"] = f"DT_{prop.val.z_path_id}"
Martí Bolívarffc03122020-11-12 20:27:20 -0800727 ret[f"{macro}_IDX_0_EXISTS"] = 1
Martí Bolívardc85edd2020-02-28 15:26:52 -0800728 elif prop.type == "phandles":
729 for i, node in enumerate(prop.val):
Kumar Gala7b9fbcd2021-08-12 14:57:49 -0500730 ret[f"{macro}_IDX_{i}"] = f"DT_{node.z_path_id}"
Martí Bolívardc85edd2020-02-28 15:26:52 -0800731 ret[f"{macro}_IDX_{i}_PH"] = f"DT_{node.z_path_id}"
Martí Bolívarffc03122020-11-12 20:27:20 -0800732 ret[f"{macro}_IDX_{i}_EXISTS"] = 1
Martí Bolívardc85edd2020-02-28 15:26:52 -0800733 elif prop.type == "phandle-array":
734 for i, entry in enumerate(prop.val):
Martí Bolívar38ede5a2020-12-17 14:12:01 -0800735 if entry is None:
736 # Unspecified element. The phandle-array at this index
737 # does not point at a ControllerAndData value, but
738 # subsequent indices in the array may.
739 ret[f"{macro}_IDX_{i}_EXISTS"] = 0
740 continue
741
Martí Bolívardc85edd2020-02-28 15:26:52 -0800742 ret.update(controller_and_data_macros(entry, i, macro))
743
744 return ret
745
746
747def controller_and_data_macros(entry, i, macro):
748 # Helper procedure used by phandle_macros().
749 #
750 # Its purpose is to write the "controller" (i.e. label property of
751 # the phandle's node) and associated data macros for a
752 # ControllerAndData.
753
754 ret = {}
755 data = entry.data
756
Martí Bolívarffc03122020-11-12 20:27:20 -0800757 # DT_N_<node-id>_P_<prop-id>_IDX_<i>_EXISTS
758 ret[f"{macro}_IDX_{i}_EXISTS"] = 1
Martí Bolívardc85edd2020-02-28 15:26:52 -0800759 # DT_N_<node-id>_P_<prop-id>_IDX_<i>_PH
760 ret[f"{macro}_IDX_{i}_PH"] = f"DT_{entry.controller.z_path_id}"
761 # DT_N_<node-id>_P_<prop-id>_IDX_<i>_VAL_<VAL>
762 for cell, val in data.items():
763 ret[f"{macro}_IDX_{i}_VAL_{str2ident(cell)}"] = val
764 ret[f"{macro}_IDX_{i}_VAL_{str2ident(cell)}_EXISTS"] = 1
765
766 if not entry.name:
767 return ret
768
769 name = str2ident(entry.name)
Erwan Gouriou6c8617a2020-04-06 14:56:11 +0200770 # DT_N_<node-id>_P_<prop-id>_IDX_<i>_EXISTS
771 ret[f"{macro}_IDX_{i}_EXISTS"] = 1
Martí Bolívardc85edd2020-02-28 15:26:52 -0800772 # DT_N_<node-id>_P_<prop-id>_IDX_<i>_NAME
773 ret[f"{macro}_IDX_{i}_NAME"] = quote_str(entry.name)
774 # DT_N_<node-id>_P_<prop-id>_NAME_<NAME>_PH
775 ret[f"{macro}_NAME_{name}_PH"] = f"DT_{entry.controller.z_path_id}"
Erwan Gouriou6c8617a2020-04-06 14:56:11 +0200776 # DT_N_<node-id>_P_<prop-id>_NAME_<NAME>_EXISTS
777 ret[f"{macro}_NAME_{name}_EXISTS"] = 1
Martí Bolívardc85edd2020-02-28 15:26:52 -0800778 # DT_N_<node-id>_P_<prop-id>_NAME_<NAME>_VAL_<VAL>
779 for cell, val in data.items():
780 cell_ident = str2ident(cell)
781 ret[f"{macro}_NAME_{name}_VAL_{cell_ident}"] = \
782 f"DT_{macro}_IDX_{i}_VAL_{cell_ident}"
783 ret[f"{macro}_NAME_{name}_VAL_{cell_ident}_EXISTS"] = 1
784
785 return ret
786
787
788def write_chosen(edt):
789 # Tree-wide information such as chosen nodes is printed here.
790
791 out_comment("Chosen nodes\n")
792 chosen = {}
793 for name, node in edt.chosen_nodes.items():
794 chosen[f"DT_CHOSEN_{str2ident(name)}"] = f"DT_{node.z_path_id}"
795 chosen[f"DT_CHOSEN_{str2ident(name)}_EXISTS"] = 1
Kumar Gala299bfd02020-03-25 15:32:58 -0500796 max_len = max(map(len, chosen), default=0)
Martí Bolívardc85edd2020-02-28 15:26:52 -0800797 for macro, value in chosen.items():
798 out_define(macro, value, width=max_len)
799
800
Martí Bolívara3fae2f2020-03-25 14:18:27 -0700801def write_global_compat_info(edt):
802 # Tree-wide information related to each compatible, such as number
Martí Bolívar7e0eed92020-05-06 11:23:07 -0700803 # of instances with status "okay", is printed here.
Martí Bolívardc85edd2020-02-28 15:26:52 -0800804
Martí Bolívar7e0eed92020-05-06 11:23:07 -0700805 n_okay_macros = {}
806 for_each_macros = {}
807 compat2buses = defaultdict(list) # just for "okay" nodes
808 for compat, okay_nodes in edt.compat2okay.items():
809 for node in okay_nodes:
Martí Bolívara3fae2f2020-03-25 14:18:27 -0700810 bus = node.on_bus
811 if bus is not None and bus not in compat2buses[compat]:
812 compat2buses[compat].append(bus)
Martí Bolívardc85edd2020-02-28 15:26:52 -0800813
Martí Bolívar63d55292020-04-06 15:13:53 -0700814 ident = str2ident(compat)
Martí Bolívar7e0eed92020-05-06 11:23:07 -0700815 n_okay_macros[f"DT_N_INST_{ident}_NUM_OKAY"] = len(okay_nodes)
Martí Bolívare7d42ff2021-08-05 15:24:50 -0700816
817 # Helpers for non-INST for-each macros that take node
818 # identifiers as arguments.
819 for_each_macros[f"DT_FOREACH_OKAY_{ident}(fn)"] = \
820 " ".join(f"fn(DT_{node.z_path_id})"
821 for node in okay_nodes)
822 for_each_macros[f"DT_FOREACH_OKAY_VARGS_{ident}(fn, ...)"] = \
823 " ".join(f"fn(DT_{node.z_path_id}, __VA_ARGS__)"
824 for node in okay_nodes)
825
826 # Helpers for INST versions of for-each macros, which take
827 # instance numbers. We emit separate helpers for these because
828 # avoiding an intermediate node_id --> instance number
829 # conversion in the preprocessor helps to keep the macro
830 # expansions simpler. That hopefully eases debugging.
Martí Bolívar7e0eed92020-05-06 11:23:07 -0700831 for_each_macros[f"DT_FOREACH_OKAY_INST_{ident}(fn)"] = \
832 " ".join(f"fn({edt.compat2nodes[compat].index(node)})"
833 for node in okay_nodes)
Arvin Farahmandd0b9c032021-05-06 11:19:29 -0400834 for_each_macros[f"DT_FOREACH_OKAY_INST_VARGS_{ident}(fn, ...)"] = \
835 " ".join(f"fn({edt.compat2nodes[compat].index(node)}, __VA_ARGS__)"
836 for node in okay_nodes)
837
Kumar Galabd973782020-05-06 20:54:29 -0500838 for compat, nodes in edt.compat2nodes.items():
839 for node in nodes:
840 if compat == "fixed-partitions":
841 for child in node.children.values():
842 if "label" in child.props:
843 label = child.props["label"].val
844 macro = f"COMPAT_{str2ident(compat)}_LABEL_{str2ident(label)}"
845 val = f"DT_{child.z_path_id}"
846
847 out_dt_define(macro, val)
848 out_dt_define(macro + "_EXISTS", 1)
849
Martí Bolívar7e0eed92020-05-06 11:23:07 -0700850 out_comment('Macros for compatibles with status "okay" nodes\n')
851 for compat, okay_nodes in edt.compat2okay.items():
852 if okay_nodes:
853 out_define(f"DT_COMPAT_HAS_OKAY_{str2ident(compat)}", 1)
854
855 out_comment('Macros for status "okay" instances of each compatible\n')
856 for macro, value in n_okay_macros.items():
Martí Bolívar63d55292020-04-06 15:13:53 -0700857 out_define(macro, value)
858 for macro, value in for_each_macros.items():
859 out_define(macro, value)
Martí Bolívardc85edd2020-02-28 15:26:52 -0800860
Martí Bolívar7e0eed92020-05-06 11:23:07 -0700861 out_comment('Bus information for status "okay" nodes of each compatible\n')
Martí Bolívara3fae2f2020-03-25 14:18:27 -0700862 for compat, buses in compat2buses.items():
863 for bus in buses:
864 out_define(
865 f"DT_COMPAT_{str2ident(compat)}_BUS_{str2ident(bus)}", 1)
Martí Bolívardc85edd2020-02-28 15:26:52 -0800866
867def str2ident(s):
868 # Converts 's' to a form suitable for (part of) an identifier
869
870 return re.sub('[-,.@/+]', '_', s.lower())
871
872
873def list2init(l):
874 # Converts 'l', a Python list (or iterable), to a C array initializer
875
876 return "{" + ", ".join(l) + "}"
877
878
879def out_dt_define(macro, val, width=None, deprecation_msg=None):
880 # Writes "#define DT_<macro> <val>" to the header file
881 #
882 # The macro will be left-justified to 'width' characters if that
883 # is specified, and the value will follow immediately after in
884 # that case. Otherwise, this function decides how to add
885 # whitespace between 'macro' and 'val'.
886 #
887 # If a 'deprecation_msg' string is passed, the generated identifiers will
888 # generate a warning if used, via __WARN(<deprecation_msg>)).
889 #
890 # Returns the full generated macro for 'macro', with leading "DT_".
891 ret = "DT_" + macro
892 out_define(ret, val, width=width, deprecation_msg=deprecation_msg)
893 return ret
894
895
896def out_define(macro, val, width=None, deprecation_msg=None):
897 # Helper for out_dt_define(). Outputs "#define <macro> <val>",
898 # adds a deprecation message if given, and allocates whitespace
899 # unless told not to.
900
901 warn = fr' __WARN("{deprecation_msg}")' if deprecation_msg else ""
902
903 if width:
904 s = f"#define {macro.ljust(width)}{warn} {val}"
905 else:
906 s = f"#define {macro}{warn} {val}"
907
908 print(s, file=header_file)
909
910
911def out_comment(s, blank_before=True):
912 # Writes 's' as a comment to the header and configuration file. 's' is
913 # allowed to have multiple lines. blank_before=True adds a blank line
914 # before the comment.
915
916 if blank_before:
917 print(file=header_file)
918
919 if "\n" in s:
920 # Format multi-line comments like
921 #
922 # /*
923 # * first line
924 # * second line
925 # *
926 # * empty line before this line
927 # */
928 res = ["/*"]
929 for line in s.splitlines():
930 # Avoid an extra space after '*' for empty lines. They turn red in
931 # Vim if space error checking is on, which is annoying.
932 res.append(" *" if not line.strip() else " * " + line)
933 res.append(" */")
934 print("\n".join(res), file=header_file)
935 else:
936 # Format single-line comments like
937 #
938 # /* foo bar */
939 print("/* " + s + " */", file=header_file)
940
941
942def escape(s):
943 # Backslash-escapes any double quotes and backslashes in 's'
944
945 # \ must be escaped before " to avoid double escaping
946 return s.replace("\\", "\\\\").replace('"', '\\"')
947
948
949def quote_str(s):
950 # Puts quotes around 's' and escapes any double quotes and
951 # backslashes within it
952
953 return f'"{escape(s)}"'
954
955
Martí Bolívar533f4512020-07-01 10:43:43 -0700956def write_pickled_edt(edt, out_file):
957 # Writes the edt object in pickle format to out_file.
958
959 with open(out_file, 'wb') as f:
960 # Pickle protocol version 4 is the default as of Python 3.8
961 # and was introduced in 3.4, so it is both available and
962 # recommended on all versions of Python that Zephyr supports
963 # (at time of writing, Python 3.6 was Zephyr's minimum
964 # version, and 3.8 the most recent CPython release).
965 #
966 # Using a common protocol version here will hopefully avoid
967 # reproducibility issues in different Python installations.
968 pickle.dump(edt, f, protocol=4)
969
970
Martí Bolívardc85edd2020-02-28 15:26:52 -0800971def err(s):
972 raise Exception(s)
973
974
975if __name__ == "__main__":
976 main()