Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 1 | #!/usr/bin/python |
Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 3 | # |
| 4 | # Copyright (C) 2017 Google, Inc |
| 5 | # Written by Simon Glass <sjg@chromium.org> |
| 6 | # |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 7 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 8 | """Device tree to platform data class |
| 9 | |
| 10 | This supports converting device tree data to C structures definitions and |
| 11 | static data. |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 12 | |
| 13 | See doc/driver-model/of-plat.rst for more informaiton |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 14 | """ |
| 15 | |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 16 | import collections |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 17 | import copy |
Simon Glass | c3a310a8 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 18 | from enum import IntEnum |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 19 | import os |
| 20 | import re |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 21 | import sys |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 22 | |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 23 | from dtoc import fdt |
| 24 | from dtoc import fdt_util |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 25 | |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 26 | # When we see these properties we ignore them - i.e. do not create a structure |
| 27 | # member |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 28 | PROP_IGNORE_LIST = [ |
| 29 | '#address-cells', |
| 30 | '#gpio-cells', |
| 31 | '#size-cells', |
| 32 | 'compatible', |
| 33 | 'linux,phandle', |
| 34 | "status", |
| 35 | 'phandle', |
| 36 | 'u-boot,dm-pre-reloc', |
| 37 | 'u-boot,dm-tpl', |
| 38 | 'u-boot,dm-spl', |
| 39 | ] |
| 40 | |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 41 | # C type declarations for the types we support |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 42 | TYPE_NAMES = { |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 43 | fdt.Type.INT: 'fdt32_t', |
| 44 | fdt.Type.BYTE: 'unsigned char', |
| 45 | fdt.Type.STRING: 'const char *', |
| 46 | fdt.Type.BOOL: 'bool', |
| 47 | fdt.Type.INT64: 'fdt64_t', |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 48 | } |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 49 | |
| 50 | STRUCT_PREFIX = 'dtd_' |
| 51 | VAL_PREFIX = 'dtv_' |
| 52 | |
Simon Glass | c3a310a8 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 53 | class Ftype(IntEnum): |
| 54 | SOURCE, HEADER = range(2) |
| 55 | |
| 56 | |
| 57 | # This holds information about each type of output file dtoc can create |
| 58 | # type: Type of file (Ftype) |
| 59 | # fname: Filename excluding directory, e.g. 'dt-platdata.c' |
| 60 | OutputFile = collections.namedtuple('OutputFile', ['ftype', 'fname']) |
| 61 | |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 62 | # This holds information about a property which includes phandles. |
| 63 | # |
| 64 | # max_args: integer: Maximum number or arguments that any phandle uses (int). |
| 65 | # args: Number of args for each phandle in the property. The total number of |
| 66 | # phandles is len(args). This is a list of integers. |
| 67 | PhandleInfo = collections.namedtuple('PhandleInfo', ['max_args', 'args']) |
| 68 | |
Simon Glass | c162211 | 2020-10-03 09:25:19 -0600 | [diff] [blame] | 69 | # Holds a single phandle link, allowing a C struct value to be assigned to point |
| 70 | # to a device |
| 71 | # |
| 72 | # var_node: C variable to assign (e.g. 'dtv_mmc.clocks[0].node') |
| 73 | # dev_name: Name of device to assign to (e.g. 'clock') |
| 74 | PhandleLink = collections.namedtuple('PhandleLink', ['var_node', 'dev_name']) |
| 75 | |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 76 | |
Simon Glass | b42ed51 | 2020-12-23 08:11:23 -0700 | [diff] [blame] | 77 | class Driver: |
| 78 | """Information about a driver in U-Boot |
| 79 | |
| 80 | Attributes: |
| 81 | name: Name of driver. For U_BOOT_DRIVER(x) this is 'x' |
| 82 | """ |
| 83 | def __init__(self, name): |
| 84 | self.name = name |
| 85 | |
| 86 | def __eq__(self, other): |
| 87 | return self.name == other.name |
| 88 | |
| 89 | def __repr__(self): |
| 90 | return "Driver(name='%s')" % self.name |
| 91 | |
| 92 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 93 | def conv_name_to_c(name): |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 94 | """Convert a device-tree name to a C identifier |
| 95 | |
Simon Glass | 2e0bf3f | 2017-06-18 22:09:04 -0600 | [diff] [blame] | 96 | This uses multiple replace() calls instead of re.sub() since it is faster |
| 97 | (400ms for 1m calls versus 1000ms for the 're' version). |
| 98 | |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 99 | Args: |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 100 | name (str): Name to convert |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 101 | Return: |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 102 | str: String containing the C version of this name |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 103 | """ |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 104 | new = name.replace('@', '_at_') |
| 105 | new = new.replace('-', '_') |
| 106 | new = new.replace(',', '_') |
| 107 | new = new.replace('.', '_') |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 108 | return new |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 109 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 110 | def tab_to(num_tabs, line): |
| 111 | """Append tabs to a line of text to reach a tab stop. |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 112 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 113 | Args: |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 114 | num_tabs (int): Tab stop to obtain (0 = column 0, 1 = column 8, etc.) |
| 115 | line (str): Line of text to append to |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 116 | |
| 117 | Returns: |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 118 | str: line with the correct number of tabs appeneded. If the line already |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 119 | extends past that tab stop then a single space is appended. |
| 120 | """ |
| 121 | if len(line) >= num_tabs * 8: |
| 122 | return line + ' ' |
| 123 | return line + '\t' * (num_tabs - len(line) // 8) |
| 124 | |
Simon Glass | 7a2add1 | 2017-06-18 22:09:02 -0600 | [diff] [blame] | 125 | def get_value(ftype, value): |
| 126 | """Get a value as a C expression |
| 127 | |
| 128 | For integers this returns a byte-swapped (little-endian) hex string |
| 129 | For bytes this returns a hex string, e.g. 0x12 |
| 130 | For strings this returns a literal string enclosed in quotes |
| 131 | For booleans this return 'true' |
| 132 | |
| 133 | Args: |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 134 | ftype (fdt.Type): Data type (fdt_util) |
| 135 | value (bytes): Data value, as a string of bytes |
| 136 | |
| 137 | Returns: |
| 138 | str: String representation of the value |
Simon Glass | 7a2add1 | 2017-06-18 22:09:02 -0600 | [diff] [blame] | 139 | """ |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 140 | if ftype == fdt.Type.INT: |
Simon Glass | 2751123 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 141 | val = '%#x' % fdt_util.fdt32_to_cpu(value) |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 142 | elif ftype == fdt.Type.BYTE: |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 143 | char = value[0] |
Simon Glass | 2751123 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 144 | val = '%#x' % (ord(char) if isinstance(char, str) else char) |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 145 | elif ftype == fdt.Type.STRING: |
Simon Glass | 7f5e226 | 2020-07-07 21:32:06 -0600 | [diff] [blame] | 146 | # Handle evil ACPI backslashes by adding another backslash before them. |
| 147 | # So "\\_SB.GPO0" in the device tree effectively stays like that in C |
Simon Glass | 2751123 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 148 | val = '"%s"' % value.replace('\\', '\\\\') |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 149 | elif ftype == fdt.Type.BOOL: |
Simon Glass | 2751123 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 150 | val = 'true' |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 151 | else: # ftype == fdt.Type.INT64: |
Simon Glass | 2751123 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 152 | val = '%#x' % value |
| 153 | return val |
Simon Glass | 7a2add1 | 2017-06-18 22:09:02 -0600 | [diff] [blame] | 154 | |
| 155 | def get_compat_name(node): |
Walter Lozano | 5fe734c | 2020-07-23 00:22:03 -0300 | [diff] [blame] | 156 | """Get the node's list of compatible string as a C identifiers |
Simon Glass | 7a2add1 | 2017-06-18 22:09:02 -0600 | [diff] [blame] | 157 | |
| 158 | Args: |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 159 | node (fdt.Node): Node object to check |
Simon Glass | 7a2add1 | 2017-06-18 22:09:02 -0600 | [diff] [blame] | 160 | Return: |
Simon Glass | 2751123 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 161 | list of str: List of C identifiers for all the compatible strings |
Simon Glass | 7a2add1 | 2017-06-18 22:09:02 -0600 | [diff] [blame] | 162 | """ |
| 163 | compat = node.props['compatible'].value |
Walter Lozano | 5fe734c | 2020-07-23 00:22:03 -0300 | [diff] [blame] | 164 | if not isinstance(compat, list): |
| 165 | compat = [compat] |
| 166 | return [conv_name_to_c(c) for c in compat] |
Simon Glass | 7a2add1 | 2017-06-18 22:09:02 -0600 | [diff] [blame] | 167 | |
Simon Glass | 7a2add1 | 2017-06-18 22:09:02 -0600 | [diff] [blame] | 168 | |
Simon Glass | 2751123 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 169 | class DtbPlatdata(): |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 170 | """Provide a means to convert device tree binary data to platform data |
| 171 | |
| 172 | The output of this process is C structures which can be used in space- |
| 173 | constrained encvironments where the ~3KB code overhead of device tree |
| 174 | code is not affordable. |
| 175 | |
| 176 | Properties: |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 177 | _fdt: Fdt object, referencing the device tree |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 178 | _dtb_fname: Filename of the input device tree binary file |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 179 | _valid_nodes: A list of Node object with compatible strings. The list |
| 180 | is ordered by conv_name_to_c(node.name) |
Simon Glass | eab3f62 | 2017-06-18 22:09:01 -0600 | [diff] [blame] | 181 | _include_disabled: true to include nodes marked status = "disabled" |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 182 | _outfile: The current output file (sys.stdout or a real file) |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 183 | _warning_disabled: true to disable warnings about driver names not found |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 184 | _lines: Stashed list of output lines for outputting in the future |
Simon Glass | b42ed51 | 2020-12-23 08:11:23 -0700 | [diff] [blame] | 185 | _drivers: Dict of valid driver names found in drivers/ |
| 186 | key: Driver name |
| 187 | value: Driver for that driver |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 188 | _driver_aliases: Dict that holds aliases for driver names |
| 189 | key: Driver alias declared with |
Simon Glass | df65db8 | 2020-12-28 20:34:57 -0700 | [diff] [blame^] | 190 | DM_DRIVER_ALIAS(driver_alias, driver_name) |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 191 | value: Driver name declared with U_BOOT_DRIVER(driver_name) |
Walter Lozano | d82062b | 2020-07-28 19:06:23 -0300 | [diff] [blame] | 192 | _drivers_additional: List of additional drivers to use during scanning |
Simon Glass | c3a310a8 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 193 | _dirname: Directory to hold output files, or None for none (all files |
| 194 | go to stdout) |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 195 | """ |
Walter Lozano | d82062b | 2020-07-28 19:06:23 -0300 | [diff] [blame] | 196 | def __init__(self, dtb_fname, include_disabled, warning_disabled, |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 197 | drivers_additional=None): |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 198 | self._fdt = None |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 199 | self._dtb_fname = dtb_fname |
| 200 | self._valid_nodes = None |
Simon Glass | eab3f62 | 2017-06-18 22:09:01 -0600 | [diff] [blame] | 201 | self._include_disabled = include_disabled |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 202 | self._outfile = None |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 203 | self._warning_disabled = warning_disabled |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 204 | self._lines = [] |
Simon Glass | b42ed51 | 2020-12-23 08:11:23 -0700 | [diff] [blame] | 205 | self._drivers = {} |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 206 | self._driver_aliases = {} |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 207 | self._drivers_additional = drivers_additional or [] |
Simon Glass | c3a310a8 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 208 | self._dirnames = [None] * len(Ftype) |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 209 | |
| 210 | def get_normalized_compat_name(self, node): |
| 211 | """Get a node's normalized compat name |
| 212 | |
Walter Lozano | 5fe734c | 2020-07-23 00:22:03 -0300 | [diff] [blame] | 213 | Returns a valid driver name by retrieving node's list of compatible |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 214 | string as a C identifier and performing a check against _drivers |
| 215 | and a lookup in driver_aliases printing a warning in case of failure. |
| 216 | |
| 217 | Args: |
Simon Glass | 2751123 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 218 | node (Node): Node object to check |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 219 | Return: |
| 220 | Tuple: |
| 221 | Driver name associated with the first compatible string |
| 222 | List of C identifiers for all the other compatible strings |
| 223 | (possibly empty) |
| 224 | In case of no match found, the return will be the same as |
| 225 | get_compat_name() |
| 226 | """ |
Walter Lozano | 5fe734c | 2020-07-23 00:22:03 -0300 | [diff] [blame] | 227 | compat_list_c = get_compat_name(node) |
| 228 | |
| 229 | for compat_c in compat_list_c: |
Simon Glass | b42ed51 | 2020-12-23 08:11:23 -0700 | [diff] [blame] | 230 | if not compat_c in self._drivers.keys(): |
Walter Lozano | 5fe734c | 2020-07-23 00:22:03 -0300 | [diff] [blame] | 231 | compat_c = self._driver_aliases.get(compat_c) |
| 232 | if not compat_c: |
| 233 | continue |
| 234 | |
| 235 | aliases_c = compat_list_c |
| 236 | if compat_c in aliases_c: |
| 237 | aliases_c.remove(compat_c) |
| 238 | return compat_c, aliases_c |
| 239 | |
| 240 | if not self._warning_disabled: |
| 241 | print('WARNING: the driver %s was not found in the driver list' |
| 242 | % (compat_list_c[0])) |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 243 | |
Walter Lozano | 5fe734c | 2020-07-23 00:22:03 -0300 | [diff] [blame] | 244 | return compat_list_c[0], compat_list_c[1:] |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 245 | |
Simon Glass | c3a310a8 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 246 | def setup_output_dirs(self, output_dirs): |
| 247 | """Set up the output directories |
| 248 | |
| 249 | This should be done before setup_output() is called |
| 250 | |
| 251 | Args: |
| 252 | output_dirs (tuple of str): |
| 253 | Directory to use for C output files. |
| 254 | Use None to write files relative current directory |
| 255 | Directory to use for H output files. |
| 256 | Defaults to the C output dir |
| 257 | """ |
| 258 | def process_dir(ftype, dirname): |
| 259 | if dirname: |
| 260 | os.makedirs(dirname, exist_ok=True) |
| 261 | self._dirnames[ftype] = dirname |
| 262 | |
| 263 | if output_dirs: |
| 264 | c_dirname = output_dirs[0] |
| 265 | h_dirname = output_dirs[1] if len(output_dirs) > 1 else c_dirname |
| 266 | process_dir(Ftype.SOURCE, c_dirname) |
| 267 | process_dir(Ftype.HEADER, h_dirname) |
| 268 | |
| 269 | def setup_output(self, ftype, fname): |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 270 | """Set up the output destination |
| 271 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 272 | Once this is done, future calls to self.out() will output to this |
Simon Glass | c3a310a8 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 273 | file. The file used is as follows: |
| 274 | |
| 275 | self._dirnames[ftype] is None: output to fname, or stdout if None |
| 276 | self._dirnames[ftype] is not None: output to fname in that directory |
| 277 | |
| 278 | Calling this function multiple times will close the old file and open |
| 279 | the new one. If they are the same file, nothing happens and output will |
| 280 | continue to the same file. |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 281 | |
| 282 | Args: |
Simon Glass | c3a310a8 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 283 | ftype (str): Type of file to create ('c' or 'h') |
| 284 | fname (str): Filename to send output to. If there is a directory in |
| 285 | self._dirnames for this file type, it will be put in that |
| 286 | directory |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 287 | """ |
Simon Glass | c3a310a8 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 288 | dirname = self._dirnames[ftype] |
| 289 | if dirname: |
| 290 | pathname = os.path.join(dirname, fname) |
| 291 | if self._outfile: |
| 292 | self._outfile.close() |
| 293 | self._outfile = open(pathname, 'w') |
| 294 | elif fname: |
| 295 | if not self._outfile: |
| 296 | self._outfile = open(fname, 'w') |
Simon Glass | 6ca0c7a | 2020-12-28 20:34:48 -0700 | [diff] [blame] | 297 | else: |
| 298 | self._outfile = sys.stdout |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 299 | |
Simon Glass | c3a310a8 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 300 | def finish_output(self): |
| 301 | """Finish outputing to a file |
| 302 | |
| 303 | This closes the output file, if one is in use |
| 304 | """ |
| 305 | if self._outfile != sys.stdout: |
| 306 | self._outfile.close() |
| 307 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 308 | def out(self, line): |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 309 | """Output a string to the output file |
| 310 | |
| 311 | Args: |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 312 | line (str): String to output |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 313 | """ |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 314 | self._outfile.write(line) |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 315 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 316 | def buf(self, line): |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 317 | """Buffer up a string to send later |
| 318 | |
| 319 | Args: |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 320 | line (str): String to add to our 'buffer' list |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 321 | """ |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 322 | self._lines.append(line) |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 323 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 324 | def get_buf(self): |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 325 | """Get the contents of the output buffer, and clear it |
| 326 | |
| 327 | Returns: |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 328 | list(str): The output buffer, which is then cleared for future use |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 329 | """ |
| 330 | lines = self._lines |
| 331 | self._lines = [] |
| 332 | return lines |
| 333 | |
Simon Glass | 68d32c7 | 2017-08-29 14:16:01 -0600 | [diff] [blame] | 334 | def out_header(self): |
| 335 | """Output a message indicating that this is an auto-generated file""" |
| 336 | self.out('''/* |
| 337 | * DO NOT MODIFY |
| 338 | * |
| 339 | * This file was generated by dtoc from a .dtb (device tree binary) file. |
| 340 | */ |
| 341 | |
| 342 | ''') |
| 343 | |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 344 | def get_phandle_argc(self, prop, node_name): |
| 345 | """Check if a node contains phandles |
Simon Glass | ce5621d | 2017-08-29 14:15:54 -0600 | [diff] [blame] | 346 | |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 347 | We have no reliable way of detecting whether a node uses a phandle |
| 348 | or not. As an interim measure, use a list of known property names. |
| 349 | |
| 350 | Args: |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 351 | prop (fdt.Prop): Prop object to check |
| 352 | node_name (str): Node name, only used for raising an error |
| 353 | Returns: |
| 354 | int or None: Number of argument cells is this is a phandle, |
| 355 | else None |
| 356 | Raises: |
| 357 | ValueError: if the phandle cannot be parsed or the required property |
| 358 | is not present |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 359 | """ |
Walter Lozano | 179f0b6 | 2020-06-25 01:10:16 -0300 | [diff] [blame] | 360 | if prop.name in ['clocks', 'cd-gpios']: |
Simon Glass | 609e2b1 | 2018-07-06 10:27:31 -0600 | [diff] [blame] | 361 | if not isinstance(prop.value, list): |
| 362 | prop.value = [prop.value] |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 363 | val = prop.value |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 364 | i = 0 |
Simon Glass | ce5621d | 2017-08-29 14:15:54 -0600 | [diff] [blame] | 365 | |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 366 | max_args = 0 |
| 367 | args = [] |
| 368 | while i < len(val): |
| 369 | phandle = fdt_util.fdt32_to_cpu(val[i]) |
Simon Glass | 609e2b1 | 2018-07-06 10:27:31 -0600 | [diff] [blame] | 370 | # If we get to the end of the list, stop. This can happen |
| 371 | # since some nodes have more phandles in the list than others, |
| 372 | # but we allocate enough space for the largest list. So those |
| 373 | # nodes with shorter lists end up with zeroes at the end. |
| 374 | if not phandle: |
| 375 | break |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 376 | target = self._fdt.phandle_to_node.get(phandle) |
| 377 | if not target: |
| 378 | raise ValueError("Cannot parse '%s' in node '%s'" % |
| 379 | (prop.name, node_name)) |
Walter Lozano | 179f0b6 | 2020-06-25 01:10:16 -0300 | [diff] [blame] | 380 | cells = None |
| 381 | for prop_name in ['#clock-cells', '#gpio-cells']: |
| 382 | cells = target.props.get(prop_name) |
| 383 | if cells: |
| 384 | break |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 385 | if not cells: |
Walter Lozano | 179f0b6 | 2020-06-25 01:10:16 -0300 | [diff] [blame] | 386 | raise ValueError("Node '%s' has no cells property" % |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 387 | (target.name)) |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 388 | num_args = fdt_util.fdt32_to_cpu(cells.value) |
| 389 | max_args = max(max_args, num_args) |
| 390 | args.append(num_args) |
| 391 | i += 1 + num_args |
| 392 | return PhandleInfo(max_args, args) |
| 393 | return None |
Simon Glass | ce5621d | 2017-08-29 14:15:54 -0600 | [diff] [blame] | 394 | |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 395 | def scan_driver(self, fname): |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 396 | """Scan a driver file to build a list of driver names and aliases |
| 397 | |
| 398 | This procedure will populate self._drivers and self._driver_aliases |
| 399 | |
| 400 | Args |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 401 | fname: Driver filename to scan |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 402 | """ |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 403 | with open(fname, encoding='utf-8') as inf: |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 404 | try: |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 405 | buff = inf.read() |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 406 | except UnicodeDecodeError: |
| 407 | # This seems to happen on older Python versions |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 408 | print("Skipping file '%s' due to unicode error" % fname) |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 409 | return |
| 410 | |
| 411 | # The following re will search for driver names declared as |
| 412 | # U_BOOT_DRIVER(driver_name) |
Simon Glass | 2751123 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 413 | drivers = re.findall(r'U_BOOT_DRIVER\((.*)\)', buff) |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 414 | |
| 415 | for driver in drivers: |
Simon Glass | b42ed51 | 2020-12-23 08:11:23 -0700 | [diff] [blame] | 416 | self._drivers[driver] = Driver(driver) |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 417 | |
| 418 | # The following re will search for driver aliases declared as |
Simon Glass | df65db8 | 2020-12-28 20:34:57 -0700 | [diff] [blame^] | 419 | # DM_DRIVER_ALIAS(alias, driver_name) |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 420 | driver_aliases = re.findall( |
Simon Glass | df65db8 | 2020-12-28 20:34:57 -0700 | [diff] [blame^] | 421 | r'DM_DRIVER_ALIAS\(\s*(\w+)\s*,\s*(\w+)\s*\)', |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 422 | buff) |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 423 | |
| 424 | for alias in driver_aliases: # pragma: no cover |
| 425 | if len(alias) != 2: |
| 426 | continue |
| 427 | self._driver_aliases[alias[1]] = alias[0] |
| 428 | |
| 429 | def scan_drivers(self): |
| 430 | """Scan the driver folders to build a list of driver names and aliases |
| 431 | |
| 432 | This procedure will populate self._drivers and self._driver_aliases |
| 433 | |
| 434 | """ |
| 435 | basedir = sys.argv[0].replace('tools/dtoc/dtoc', '') |
| 436 | if basedir == '': |
| 437 | basedir = './' |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 438 | for (dirpath, _, filenames) in os.walk(basedir): |
| 439 | for fname in filenames: |
| 440 | if not fname.endswith('.c'): |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 441 | continue |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 442 | self.scan_driver(dirpath + '/' + fname) |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 443 | |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 444 | for fname in self._drivers_additional: |
| 445 | if not isinstance(fname, str) or len(fname) == 0: |
Walter Lozano | d82062b | 2020-07-28 19:06:23 -0300 | [diff] [blame] | 446 | continue |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 447 | if fname[0] == '/': |
| 448 | self.scan_driver(fname) |
Walter Lozano | d82062b | 2020-07-28 19:06:23 -0300 | [diff] [blame] | 449 | else: |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 450 | self.scan_driver(basedir + '/' + fname) |
Walter Lozano | d82062b | 2020-07-28 19:06:23 -0300 | [diff] [blame] | 451 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 452 | def scan_dtb(self): |
Anatolij Gustschin | da707d4 | 2017-08-18 17:58:51 +0200 | [diff] [blame] | 453 | """Scan the device tree to obtain a tree of nodes and properties |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 454 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 455 | Once this is done, self._fdt.GetRoot() can be called to obtain the |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 456 | device tree root node, and progress from there. |
| 457 | """ |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 458 | self._fdt = fdt.FdtScan(self._dtb_fname) |
| 459 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 460 | def scan_node(self, root, valid_nodes): |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 461 | """Scan a node and subnodes to build a tree of node and phandle info |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 462 | |
Simon Glass | 17ffd62 | 2017-08-29 14:15:53 -0600 | [diff] [blame] | 463 | This adds each node to self._valid_nodes. |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 464 | |
| 465 | Args: |
Simon Glass | 2751123 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 466 | root (Node): Root node for scan |
| 467 | valid_nodes (list of Node): List of Node objects to add to |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 468 | """ |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 469 | for node in root.subnodes: |
| 470 | if 'compatible' in node.props: |
| 471 | status = node.props.get('status') |
Simon Glass | eab3f62 | 2017-06-18 22:09:01 -0600 | [diff] [blame] | 472 | if (not self._include_disabled and not status or |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 473 | status.value != 'disabled'): |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 474 | valid_nodes.append(node) |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 475 | |
| 476 | # recurse to handle any subnodes |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 477 | self.scan_node(node, valid_nodes) |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 478 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 479 | def scan_tree(self): |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 480 | """Scan the device tree for useful information |
| 481 | |
| 482 | This fills in the following properties: |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 483 | _valid_nodes: A list of nodes we wish to consider include in the |
| 484 | platform data |
| 485 | """ |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 486 | valid_nodes = [] |
| 487 | self.scan_node(self._fdt.GetRoot(), valid_nodes) |
| 488 | self._valid_nodes = sorted(valid_nodes, |
| 489 | key=lambda x: conv_name_to_c(x.name)) |
| 490 | for idx, node in enumerate(self._valid_nodes): |
| 491 | node.idx = idx |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 492 | |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 493 | @staticmethod |
| 494 | def get_num_cells(node): |
| 495 | """Get the number of cells in addresses and sizes for this node |
| 496 | |
| 497 | Args: |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 498 | node (fdt.None): Node to check |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 499 | |
| 500 | Returns: |
| 501 | Tuple: |
| 502 | Number of address cells for this node |
| 503 | Number of size cells for this node |
| 504 | """ |
| 505 | parent = node.parent |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 506 | num_addr, num_size = 2, 2 |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 507 | if parent: |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 508 | addr_prop = parent.props.get('#address-cells') |
| 509 | size_prop = parent.props.get('#size-cells') |
| 510 | if addr_prop: |
| 511 | num_addr = fdt_util.fdt32_to_cpu(addr_prop.value) |
| 512 | if size_prop: |
| 513 | num_size = fdt_util.fdt32_to_cpu(size_prop.value) |
| 514 | return num_addr, num_size |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 515 | |
| 516 | def scan_reg_sizes(self): |
| 517 | """Scan for 64-bit 'reg' properties and update the values |
| 518 | |
| 519 | This finds 'reg' properties with 64-bit data and converts the value to |
| 520 | an array of 64-values. This allows it to be output in a way that the |
| 521 | C code can read. |
| 522 | """ |
| 523 | for node in self._valid_nodes: |
| 524 | reg = node.props.get('reg') |
| 525 | if not reg: |
| 526 | continue |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 527 | num_addr, num_size = self.get_num_cells(node) |
| 528 | total = num_addr + num_size |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 529 | |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 530 | if reg.type != fdt.Type.INT: |
Simon Glass | c38fee04 | 2018-07-06 10:27:32 -0600 | [diff] [blame] | 531 | raise ValueError("Node '%s' reg property is not an int" % |
| 532 | node.name) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 533 | if len(reg.value) % total: |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 534 | raise ValueError( |
| 535 | "Node '%s' reg property has %d cells " |
| 536 | 'which is not a multiple of na + ns = %d + %d)' % |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 537 | (node.name, len(reg.value), num_addr, num_size)) |
| 538 | reg.num_addr = num_addr |
| 539 | reg.num_size = num_size |
| 540 | if num_addr != 1 or num_size != 1: |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 541 | reg.type = fdt.Type.INT64 |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 542 | i = 0 |
| 543 | new_value = [] |
| 544 | val = reg.value |
| 545 | if not isinstance(val, list): |
| 546 | val = [val] |
| 547 | while i < len(val): |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 548 | addr = fdt_util.fdt_cells_to_cpu(val[i:], reg.num_addr) |
| 549 | i += num_addr |
| 550 | size = fdt_util.fdt_cells_to_cpu(val[i:], reg.num_size) |
| 551 | i += num_size |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 552 | new_value += [addr, size] |
| 553 | reg.value = new_value |
| 554 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 555 | def scan_structs(self): |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 556 | """Scan the device tree building up the C structures we will use. |
| 557 | |
| 558 | Build a dict keyed by C struct name containing a dict of Prop |
| 559 | object for each struct field (keyed by property name). Where the |
| 560 | same struct appears multiple times, try to use the 'widest' |
| 561 | property, i.e. the one with a type which can express all others. |
| 562 | |
| 563 | Once the widest property is determined, all other properties are |
| 564 | updated to match that width. |
Simon Glass | 941f8f0 | 2020-10-03 11:31:24 -0600 | [diff] [blame] | 565 | |
| 566 | Returns: |
Simon Glass | 2751123 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 567 | dict of dict: dict containing structures: |
Simon Glass | 941f8f0 | 2020-10-03 11:31:24 -0600 | [diff] [blame] | 568 | key (str): Node name, as a C identifier |
| 569 | value: dict containing structure fields: |
| 570 | key (str): Field name |
| 571 | value: Prop object with field information |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 572 | """ |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 573 | structs = collections.OrderedDict() |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 574 | for node in self._valid_nodes: |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 575 | node_name, _ = self.get_normalized_compat_name(node) |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 576 | fields = {} |
| 577 | |
| 578 | # Get a list of all the valid properties in this node. |
| 579 | for name, prop in node.props.items(): |
| 580 | if name not in PROP_IGNORE_LIST and name[0] != '#': |
| 581 | fields[name] = copy.deepcopy(prop) |
| 582 | |
| 583 | # If we've seen this node_name before, update the existing struct. |
| 584 | if node_name in structs: |
| 585 | struct = structs[node_name] |
| 586 | for name, prop in fields.items(): |
| 587 | oldprop = struct.get(name) |
| 588 | if oldprop: |
| 589 | oldprop.Widen(prop) |
| 590 | else: |
| 591 | struct[name] = prop |
| 592 | |
| 593 | # Otherwise store this as a new struct. |
| 594 | else: |
| 595 | structs[node_name] = fields |
| 596 | |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 597 | for node in self._valid_nodes: |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 598 | node_name, _ = self.get_normalized_compat_name(node) |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 599 | struct = structs[node_name] |
| 600 | for name, prop in node.props.items(): |
| 601 | if name not in PROP_IGNORE_LIST and name[0] != '#': |
| 602 | prop.Widen(struct[name]) |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 603 | |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 604 | return structs |
| 605 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 606 | def scan_phandles(self): |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 607 | """Figure out what phandles each node uses |
| 608 | |
| 609 | We need to be careful when outputing nodes that use phandles since |
| 610 | they must come after the declaration of the phandles in the C file. |
| 611 | Otherwise we get a compiler error since the phandle struct is not yet |
| 612 | declared. |
| 613 | |
| 614 | This function adds to each node a list of phandle nodes that the node |
| 615 | depends on. This allows us to output things in the right order. |
| 616 | """ |
| 617 | for node in self._valid_nodes: |
| 618 | node.phandles = set() |
| 619 | for pname, prop in node.props.items(): |
| 620 | if pname in PROP_IGNORE_LIST or pname[0] == '#': |
| 621 | continue |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 622 | info = self.get_phandle_argc(prop, node.name) |
| 623 | if info: |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 624 | # Process the list as pairs of (phandle, id) |
Simon Glass | 3deeb47 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 625 | pos = 0 |
| 626 | for args in info.args: |
| 627 | phandle_cell = prop.value[pos] |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 628 | phandle = fdt_util.fdt32_to_cpu(phandle_cell) |
| 629 | target_node = self._fdt.phandle_to_node[phandle] |
| 630 | node.phandles.add(target_node) |
Simon Glass | 3deeb47 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 631 | pos += 1 + args |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 632 | |
| 633 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 634 | def generate_structs(self, structs): |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 635 | """Generate struct defintions for the platform data |
| 636 | |
| 637 | This writes out the body of a header file consisting of structure |
| 638 | definitions for node in self._valid_nodes. See the documentation in |
Heinrich Schuchardt | c79f03c | 2020-02-25 21:35:39 +0100 | [diff] [blame] | 639 | doc/driver-model/of-plat.rst for more information. |
Simon Glass | 941f8f0 | 2020-10-03 11:31:24 -0600 | [diff] [blame] | 640 | |
| 641 | Args: |
Simon Glass | 2751123 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 642 | structs (dict): dict containing structures: |
Simon Glass | 941f8f0 | 2020-10-03 11:31:24 -0600 | [diff] [blame] | 643 | key (str): Node name, as a C identifier |
| 644 | value: dict containing structure fields: |
| 645 | key (str): Field name |
| 646 | value: Prop object with field information |
| 647 | |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 648 | """ |
Simon Glass | 68d32c7 | 2017-08-29 14:16:01 -0600 | [diff] [blame] | 649 | self.out_header() |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 650 | self.out('#include <stdbool.h>\n') |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 651 | self.out('#include <linux/libfdt.h>\n') |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 652 | |
| 653 | # Output the struct definition |
| 654 | for name in sorted(structs): |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 655 | self.out('struct %s%s {\n' % (STRUCT_PREFIX, name)) |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 656 | for pname in sorted(structs[name]): |
| 657 | prop = structs[name][pname] |
Simon Glass | ec3b5e4 | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 658 | info = self.get_phandle_argc(prop, structs[name]) |
| 659 | if info: |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 660 | # For phandles, include a reference to the target |
Simon Glass | e94414b | 2017-08-29 14:15:56 -0600 | [diff] [blame] | 661 | struct_name = 'struct phandle_%d_arg' % info.max_args |
| 662 | self.out('\t%s%s[%d]' % (tab_to(2, struct_name), |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 663 | conv_name_to_c(prop.name), |
Simon Glass | 3deeb47 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 664 | len(info.args))) |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 665 | else: |
| 666 | ptype = TYPE_NAMES[prop.type] |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 667 | self.out('\t%s%s' % (tab_to(2, ptype), |
| 668 | conv_name_to_c(prop.name))) |
| 669 | if isinstance(prop.value, list): |
| 670 | self.out('[%d]' % len(prop.value)) |
| 671 | self.out(';\n') |
| 672 | self.out('};\n') |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 673 | |
Simon Glass | 9d98b6e | 2020-12-23 08:11:20 -0700 | [diff] [blame] | 674 | def _output_list(self, node, prop): |
| 675 | """Output the C code for a devicetree property that holds a list |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 676 | |
| 677 | Args: |
Simon Glass | 9d98b6e | 2020-12-23 08:11:20 -0700 | [diff] [blame] | 678 | node (fdt.Node): Node to output |
| 679 | prop (fdt.Prop): Prop to output |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 680 | """ |
Simon Glass | 9d98b6e | 2020-12-23 08:11:20 -0700 | [diff] [blame] | 681 | self.buf('{') |
| 682 | vals = [] |
| 683 | # For phandles, output a reference to the platform data |
| 684 | # of the target node. |
| 685 | info = self.get_phandle_argc(prop, node.name) |
| 686 | if info: |
| 687 | # Process the list as pairs of (phandle, id) |
| 688 | pos = 0 |
| 689 | for args in info.args: |
| 690 | phandle_cell = prop.value[pos] |
| 691 | phandle = fdt_util.fdt32_to_cpu(phandle_cell) |
| 692 | target_node = self._fdt.phandle_to_node[phandle] |
| 693 | arg_values = [] |
| 694 | for i in range(args): |
| 695 | arg_values.append( |
| 696 | str(fdt_util.fdt32_to_cpu(prop.value[pos + 1 + i]))) |
| 697 | pos += 1 + args |
| 698 | vals.append('\t{%d, {%s}}' % (target_node.idx, |
| 699 | ', '.join(arg_values))) |
| 700 | for val in vals: |
| 701 | self.buf('\n\t\t%s,' % val) |
| 702 | else: |
| 703 | for val in prop.value: |
| 704 | vals.append(get_value(prop.type, val)) |
Simon Glass | bc9e268 | 2020-10-03 09:25:18 -0600 | [diff] [blame] | 705 | |
Simon Glass | 9d98b6e | 2020-12-23 08:11:20 -0700 | [diff] [blame] | 706 | # Put 8 values per line to avoid very long lines. |
| 707 | for i in range(0, len(vals), 8): |
| 708 | if i: |
| 709 | self.buf(',\n\t\t') |
| 710 | self.buf(', '.join(vals[i:i + 8])) |
| 711 | self.buf('}') |
Simon Glass | bc9e268 | 2020-10-03 09:25:18 -0600 | [diff] [blame] | 712 | |
Simon Glass | 9fdd0c3 | 2020-12-23 08:11:21 -0700 | [diff] [blame] | 713 | def _declare_device(self, var_name, struct_name, node_parent): |
| 714 | """Add a device declaration to the output |
| 715 | |
Simon Glass | 1d8364a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 716 | This declares a U_BOOT_DRVINFO() for the device being processed |
Simon Glass | 9fdd0c3 | 2020-12-23 08:11:21 -0700 | [diff] [blame] | 717 | |
| 718 | Args: |
| 719 | var_name (str): C name for the node |
| 720 | struct_name (str): Name for the dt struct associated with the node |
| 721 | node_parent (Node): Parent of the node (or None if none) |
| 722 | """ |
Simon Glass | 1d8364a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 723 | self.buf('U_BOOT_DRVINFO(%s) = {\n' % var_name) |
Simon Glass | 9fdd0c3 | 2020-12-23 08:11:21 -0700 | [diff] [blame] | 724 | self.buf('\t.name\t\t= "%s",\n' % struct_name) |
| 725 | self.buf('\t.plat\t= &%s%s,\n' % (VAL_PREFIX, var_name)) |
| 726 | self.buf('\t.plat_size\t= sizeof(%s%s),\n' % (VAL_PREFIX, var_name)) |
| 727 | idx = -1 |
| 728 | if node_parent and node_parent in self._valid_nodes: |
| 729 | idx = node_parent.idx |
| 730 | self.buf('\t.parent_idx\t= %d,\n' % idx) |
| 731 | self.buf('};\n') |
| 732 | self.buf('\n') |
| 733 | |
Simon Glass | 9829eea | 2020-12-23 08:11:22 -0700 | [diff] [blame] | 734 | def _output_prop(self, node, prop): |
| 735 | """Output a line containing the value of a struct member |
| 736 | |
| 737 | Args: |
| 738 | node (Node): Node being output |
| 739 | prop (Prop): Prop object to output |
| 740 | """ |
| 741 | if prop.name in PROP_IGNORE_LIST or prop.name[0] == '#': |
| 742 | return |
| 743 | member_name = conv_name_to_c(prop.name) |
| 744 | self.buf('\t%s= ' % tab_to(3, '.' + member_name)) |
| 745 | |
| 746 | # Special handling for lists |
| 747 | if isinstance(prop.value, list): |
| 748 | self._output_list(node, prop) |
| 749 | else: |
| 750 | self.buf(get_value(prop.type, prop.value)) |
| 751 | self.buf(',\n') |
| 752 | |
| 753 | def _output_values(self, var_name, struct_name, node): |
| 754 | """Output the definition of a device's struct values |
| 755 | |
| 756 | Args: |
| 757 | var_name (str): C name for the node |
| 758 | struct_name (str): Name for the dt struct associated with the node |
| 759 | node (Node): Node being output |
| 760 | """ |
| 761 | self.buf('static struct %s%s %s%s = {\n' % |
| 762 | (STRUCT_PREFIX, struct_name, VAL_PREFIX, var_name)) |
| 763 | for pname in sorted(node.props): |
| 764 | self._output_prop(node, node.props[pname]) |
| 765 | self.buf('};\n') |
| 766 | |
Simon Glass | 9d98b6e | 2020-12-23 08:11:20 -0700 | [diff] [blame] | 767 | def output_node(self, node): |
| 768 | """Output the C code for a node |
Simon Glass | bc9e268 | 2020-10-03 09:25:18 -0600 | [diff] [blame] | 769 | |
Simon Glass | 9d98b6e | 2020-12-23 08:11:20 -0700 | [diff] [blame] | 770 | Args: |
| 771 | node (fdt.Node): node to output |
| 772 | """ |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 773 | struct_name, _ = self.get_normalized_compat_name(node) |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 774 | var_name = conv_name_to_c(node.name) |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 775 | self.buf('/* Node %s index %d */\n' % (node.path, node.idx)) |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 776 | |
Simon Glass | 9829eea | 2020-12-23 08:11:22 -0700 | [diff] [blame] | 777 | self._output_values(var_name, struct_name, node) |
Simon Glass | 9fdd0c3 | 2020-12-23 08:11:21 -0700 | [diff] [blame] | 778 | self._declare_device(var_name, struct_name, node.parent) |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 779 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 780 | self.out(''.join(self.get_buf())) |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 781 | |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 782 | def generate_tables(self): |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 783 | """Generate device defintions for the platform data |
| 784 | |
| 785 | This writes out C platform data initialisation data and |
Simon Glass | 1d8364a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 786 | U_BOOT_DRVINFO() declarations for each valid node. Where a node has |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 787 | multiple compatible strings, a #define is used to make them equivalent. |
| 788 | |
Heinrich Schuchardt | c79f03c | 2020-02-25 21:35:39 +0100 | [diff] [blame] | 789 | See the documentation in doc/driver-model/of-plat.rst for more |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 790 | information. |
| 791 | """ |
Simon Glass | 68d32c7 | 2017-08-29 14:16:01 -0600 | [diff] [blame] | 792 | self.out_header() |
Simon Glass | 1d8364a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 793 | self.out('/* Allow use of U_BOOT_DRVINFO() in this file */\n') |
Simon Glass | 4c73d7b | 2020-10-03 11:31:41 -0600 | [diff] [blame] | 794 | self.out('#define DT_PLATDATA_C\n') |
| 795 | self.out('\n') |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 796 | self.out('#include <common.h>\n') |
| 797 | self.out('#include <dm.h>\n') |
| 798 | self.out('#include <dt-structs.h>\n') |
| 799 | self.out('\n') |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 800 | nodes_to_output = list(self._valid_nodes) |
| 801 | |
| 802 | # Keep outputing nodes until there is none left |
| 803 | while nodes_to_output: |
| 804 | node = nodes_to_output[0] |
| 805 | # Output all the node's dependencies first |
| 806 | for req_node in node.phandles: |
| 807 | if req_node in nodes_to_output: |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 808 | self.output_node(req_node) |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 809 | nodes_to_output.remove(req_node) |
Simon Glass | 1f730c8 | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 810 | self.output_node(node) |
Simon Glass | d570dec | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 811 | nodes_to_output.remove(node) |
Simon Glass | 3fa797a | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 812 | |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 813 | # Define dm_populate_phandle_data() which will add the linking between |
Simon Glass | e10c0ff | 2020-12-28 20:34:55 -0700 | [diff] [blame] | 814 | # nodes using DM_DRVINFO_GET |
| 815 | # dtv_dmc_at_xxx.clocks[0].node = DM_DRVINFO_GET(clock_controller_at_xxx) |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 816 | self.buf('void dm_populate_phandle_data(void) {\n') |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 817 | self.buf('}\n') |
| 818 | |
| 819 | self.out(''.join(self.get_buf())) |
Simon Glass | 3fa797a | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 820 | |
Simon Glass | c3a310a8 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 821 | |
| 822 | # Types of output file we understand |
| 823 | # key: Command used to generate this file |
| 824 | # value: OutputFile for this command |
| 825 | OUTPUT_FILES = { |
| 826 | 'struct': OutputFile(Ftype.HEADER, 'dt-structs-gen.h'), |
| 827 | 'platdata': OutputFile(Ftype.SOURCE, 'dt-platdata.c'), |
| 828 | } |
| 829 | |
Simon Glass | 6a65d8a | 2020-12-28 20:34:50 -0700 | [diff] [blame] | 830 | |
| 831 | def run_steps(args, dtb_file, include_disabled, output, output_dirs, |
| 832 | warning_disabled=False, drivers_additional=None): |
Simon Glass | 3fa797a | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 833 | """Run all the steps of the dtoc tool |
| 834 | |
| 835 | Args: |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 836 | args (list): List of non-option arguments provided to the problem |
| 837 | dtb_file (str): Filename of dtb file to process |
| 838 | include_disabled (bool): True to include disabled nodes |
Simon Glass | 6ca0c7a | 2020-12-28 20:34:48 -0700 | [diff] [blame] | 839 | output (str): Name of output file (None for stdout) |
Simon Glass | 6a65d8a | 2020-12-28 20:34:50 -0700 | [diff] [blame] | 840 | output_dirs (tuple of str): |
| 841 | Directory to put C output files |
| 842 | Directory to put H output files |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 843 | warning_disabled (bool): True to avoid showing warnings about missing |
| 844 | drivers |
Simon Glass | 2751123 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 845 | drivers_additional (list): List of additional drivers to use during |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 846 | scanning |
Simon Glass | 94ee3bd | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 847 | Raises: |
| 848 | ValueError: if args has no command, or an unknown command |
Simon Glass | 3fa797a | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 849 | """ |
| 850 | if not args: |
Simon Glass | c3a310a8 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 851 | raise ValueError('Please specify a command: struct, platdata, all') |
| 852 | if output and output_dirs and any(output_dirs): |
| 853 | raise ValueError('Must specify either output or output_dirs, not both') |
Simon Glass | 3fa797a | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 854 | |
Simon Glass | 93daa01 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 855 | plat = DtbPlatdata(dtb_file, include_disabled, warning_disabled, |
| 856 | drivers_additional) |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 857 | plat.scan_drivers() |
Simon Glass | 3fa797a | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 858 | plat.scan_dtb() |
| 859 | plat.scan_tree() |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 860 | plat.scan_reg_sizes() |
Simon Glass | c3a310a8 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 861 | plat.setup_output_dirs(output_dirs) |
Simon Glass | 3fa797a | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 862 | structs = plat.scan_structs() |
| 863 | plat.scan_phandles() |
| 864 | |
Simon Glass | 4e8e846 | 2020-12-28 20:34:52 -0700 | [diff] [blame] | 865 | cmds = args[0].split(',') |
| 866 | if 'all' in cmds: |
| 867 | cmds = sorted(OUTPUT_FILES.keys()) |
| 868 | for cmd in cmds: |
Simon Glass | c3a310a8 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 869 | outfile = OUTPUT_FILES.get(cmd) |
| 870 | if not outfile: |
| 871 | raise ValueError("Unknown command '%s': (use: %s)" % |
Simon Glass | 4e8e846 | 2020-12-28 20:34:52 -0700 | [diff] [blame] | 872 | (cmd, ', '.join(sorted(OUTPUT_FILES.keys())))) |
Simon Glass | c3a310a8 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 873 | plat.setup_output(outfile.ftype, |
| 874 | outfile.fname if output_dirs else output) |
Simon Glass | 3fa797a | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 875 | if cmd == 'struct': |
| 876 | plat.generate_structs(structs) |
| 877 | elif cmd == 'platdata': |
| 878 | plat.generate_tables() |
Simon Glass | c3a310a8 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 879 | plat.finish_output() |