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