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