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 |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 18 | from enum import IntEnum |
Walter Lozano | dac8228 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 19 | import os |
| 20 | import re |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 21 | import sys |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 22 | |
Simon Glass | bf77667 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 23 | from dtoc import fdt |
| 24 | from dtoc import fdt_util |
Simon Glass | a542a70 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 25 | from dtoc import src_scan |
| 26 | from dtoc.src_scan import conv_name_to_c |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 27 | |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 28 | # When we see these properties we ignore them - i.e. do not create a structure |
| 29 | # member |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 30 | PROP_IGNORE_LIST = [ |
| 31 | '#address-cells', |
| 32 | '#gpio-cells', |
| 33 | '#size-cells', |
| 34 | 'compatible', |
| 35 | 'linux,phandle', |
| 36 | "status", |
| 37 | 'phandle', |
| 38 | 'u-boot,dm-pre-reloc', |
| 39 | 'u-boot,dm-tpl', |
| 40 | 'u-boot,dm-spl', |
| 41 | ] |
| 42 | |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 43 | # C type declarations for the types we support |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 44 | TYPE_NAMES = { |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 45 | fdt.Type.INT: 'fdt32_t', |
| 46 | fdt.Type.BYTE: 'unsigned char', |
| 47 | fdt.Type.STRING: 'const char *', |
| 48 | fdt.Type.BOOL: 'bool', |
| 49 | fdt.Type.INT64: 'fdt64_t', |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 50 | } |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 51 | |
| 52 | STRUCT_PREFIX = 'dtd_' |
| 53 | VAL_PREFIX = 'dtv_' |
| 54 | |
Simon Glass | 8840bc5 | 2021-02-03 06:01:18 -0700 | [diff] [blame] | 55 | # Properties which are considered to be phandles |
| 56 | # key: property name |
| 57 | # value: name of associated #cells property in the target node |
| 58 | # |
| 59 | # New phandle properties must be added here; otherwise they will come through as |
| 60 | # simple integers and finding devices by phandle will not work. |
| 61 | # Any property that ends with one of these (e.g. 'cd-gpios') will be considered |
| 62 | # a phandle property. |
| 63 | PHANDLE_PROPS = { |
| 64 | 'clocks': '#clock-cells', |
| 65 | 'gpios': '#gpio-cells', |
| 66 | 'sandbox,emul': '#emul-cells', |
| 67 | } |
| 68 | |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 69 | class Ftype(IntEnum): |
| 70 | SOURCE, HEADER = range(2) |
| 71 | |
| 72 | |
| 73 | # This holds information about each type of output file dtoc can create |
| 74 | # type: Type of file (Ftype) |
Simon Glass | d1055d6 | 2020-12-28 20:35:00 -0700 | [diff] [blame] | 75 | # fname: Filename excluding directory, e.g. 'dt-plat.c' |
| 76 | # hdr_comment: Comment explaining the purpose of the file |
| 77 | OutputFile = collections.namedtuple('OutputFile', |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 78 | ['ftype', 'fname', 'method', 'hdr_comment']) |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 79 | |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 80 | # This holds information about a property which includes phandles. |
| 81 | # |
| 82 | # max_args: integer: Maximum number or arguments that any phandle uses (int). |
| 83 | # args: Number of args for each phandle in the property. The total number of |
| 84 | # phandles is len(args). This is a list of integers. |
| 85 | PhandleInfo = collections.namedtuple('PhandleInfo', ['max_args', 'args']) |
| 86 | |
Simon Glass | 97136eb | 2020-10-03 09:25:19 -0600 | [diff] [blame] | 87 | # Holds a single phandle link, allowing a C struct value to be assigned to point |
| 88 | # to a device |
| 89 | # |
| 90 | # var_node: C variable to assign (e.g. 'dtv_mmc.clocks[0].node') |
| 91 | # dev_name: Name of device to assign to (e.g. 'clock') |
| 92 | PhandleLink = collections.namedtuple('PhandleLink', ['var_node', 'dev_name']) |
| 93 | |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 94 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 95 | def tab_to(num_tabs, line): |
| 96 | """Append tabs to a line of text to reach a tab stop. |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 97 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 98 | Args: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 99 | num_tabs (int): Tab stop to obtain (0 = column 0, 1 = column 8, etc.) |
| 100 | line (str): Line of text to append to |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 101 | |
| 102 | Returns: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 103 | 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] | 104 | extends past that tab stop then a single space is appended. |
| 105 | """ |
| 106 | if len(line) >= num_tabs * 8: |
| 107 | return line + ' ' |
| 108 | return line + '\t' * (num_tabs - len(line) // 8) |
| 109 | |
Simon Glass | 56e0bbe | 2017-06-18 22:09:02 -0600 | [diff] [blame] | 110 | def get_value(ftype, value): |
| 111 | """Get a value as a C expression |
| 112 | |
| 113 | For integers this returns a byte-swapped (little-endian) hex string |
| 114 | For bytes this returns a hex string, e.g. 0x12 |
| 115 | For strings this returns a literal string enclosed in quotes |
| 116 | For booleans this return 'true' |
| 117 | |
| 118 | Args: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 119 | ftype (fdt.Type): Data type (fdt_util) |
| 120 | value (bytes): Data value, as a string of bytes |
| 121 | |
| 122 | Returns: |
| 123 | str: String representation of the value |
Simon Glass | 56e0bbe | 2017-06-18 22:09:02 -0600 | [diff] [blame] | 124 | """ |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 125 | if ftype == fdt.Type.INT: |
Simon Glass | ccc3da7 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 126 | val = '%#x' % fdt_util.fdt32_to_cpu(value) |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 127 | elif ftype == fdt.Type.BYTE: |
Simon Glass | 78128d5 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 128 | char = value[0] |
Simon Glass | ccc3da7 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 129 | val = '%#x' % (ord(char) if isinstance(char, str) else char) |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 130 | elif ftype == fdt.Type.STRING: |
Simon Glass | f02d0eb | 2020-07-07 21:32:06 -0600 | [diff] [blame] | 131 | # Handle evil ACPI backslashes by adding another backslash before them. |
| 132 | # So "\\_SB.GPO0" in the device tree effectively stays like that in C |
Simon Glass | ccc3da7 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 133 | val = '"%s"' % value.replace('\\', '\\\\') |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 134 | elif ftype == fdt.Type.BOOL: |
Simon Glass | ccc3da7 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 135 | val = 'true' |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 136 | else: # ftype == fdt.Type.INT64: |
Simon Glass | ccc3da7 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 137 | val = '%#x' % value |
| 138 | return val |
Simon Glass | 56e0bbe | 2017-06-18 22:09:02 -0600 | [diff] [blame] | 139 | |
Simon Glass | 56e0bbe | 2017-06-18 22:09:02 -0600 | [diff] [blame] | 140 | |
Simon Glass | ccc3da7 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 141 | class DtbPlatdata(): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 142 | """Provide a means to convert device tree binary data to platform data |
| 143 | |
| 144 | The output of this process is C structures which can be used in space- |
| 145 | constrained encvironments where the ~3KB code overhead of device tree |
| 146 | code is not affordable. |
| 147 | |
| 148 | Properties: |
Simon Glass | a542a70 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 149 | _scan: Scan object, for scanning and reporting on useful information |
| 150 | from the U-Boot source code |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 151 | _fdt: Fdt object, referencing the device tree |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 152 | _dtb_fname: Filename of the input device tree binary file |
Simon Glass | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 153 | _valid_nodes_unsorted: A list of Node object with compatible strings, |
| 154 | ordered by devicetree node order |
| 155 | _valid_nodes: A list of Node object with compatible strings, ordered by |
| 156 | conv_name_to_c(node.name) |
Simon Glass | e36024b | 2017-06-18 22:09:01 -0600 | [diff] [blame] | 157 | _include_disabled: true to include nodes marked status = "disabled" |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 158 | _outfile: The current output file (sys.stdout or a real file) |
| 159 | _lines: Stashed list of output lines for outputting in the future |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 160 | _dirname: Directory to hold output files, or None for none (all files |
| 161 | go to stdout) |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 162 | _struct_data (dict): OrderedDict of dtplat structures to output |
| 163 | key (str): Node name, as a C identifier |
| 164 | value: dict containing structure fields: |
| 165 | key (str): Field name |
| 166 | value: Prop object with field information |
Simon Glass | 1e0f3f4 | 2020-12-28 20:35:03 -0700 | [diff] [blame] | 167 | _basedir (str): Base directory of source tree |
Simon Glass | 337d697 | 2021-02-03 06:01:10 -0700 | [diff] [blame] | 168 | _valid_uclasses (list of src_scan.Uclass): List of uclasses needed for |
| 169 | the selected devices (see _valid_node), in alphabetical order |
Simon Glass | 4a09235 | 2021-02-03 06:01:12 -0700 | [diff] [blame] | 170 | _instantiate: Instantiate devices so they don't need to be bound at |
| 171 | run-time |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 172 | """ |
Simon Glass | 4a09235 | 2021-02-03 06:01:12 -0700 | [diff] [blame] | 173 | def __init__(self, scan, dtb_fname, include_disabled, instantiate=False): |
Simon Glass | a542a70 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 174 | self._scan = scan |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 175 | self._fdt = None |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 176 | self._dtb_fname = dtb_fname |
| 177 | self._valid_nodes = None |
Simon Glass | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 178 | self._valid_nodes_unsorted = None |
Simon Glass | e36024b | 2017-06-18 22:09:01 -0600 | [diff] [blame] | 179 | self._include_disabled = include_disabled |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 180 | self._outfile = None |
| 181 | self._lines = [] |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 182 | self._dirnames = [None] * len(Ftype) |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 183 | self._struct_data = collections.OrderedDict() |
Simon Glass | 1e0f3f4 | 2020-12-28 20:35:03 -0700 | [diff] [blame] | 184 | self._basedir = None |
Simon Glass | 337d697 | 2021-02-03 06:01:10 -0700 | [diff] [blame] | 185 | self._valid_uclasses = None |
Simon Glass | 4a09235 | 2021-02-03 06:01:12 -0700 | [diff] [blame] | 186 | self._instantiate = instantiate |
Walter Lozano | dac8228 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 187 | |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 188 | def setup_output_dirs(self, output_dirs): |
| 189 | """Set up the output directories |
| 190 | |
| 191 | This should be done before setup_output() is called |
| 192 | |
| 193 | Args: |
| 194 | output_dirs (tuple of str): |
| 195 | Directory to use for C output files. |
| 196 | Use None to write files relative current directory |
| 197 | Directory to use for H output files. |
| 198 | Defaults to the C output dir |
| 199 | """ |
| 200 | def process_dir(ftype, dirname): |
| 201 | if dirname: |
| 202 | os.makedirs(dirname, exist_ok=True) |
| 203 | self._dirnames[ftype] = dirname |
| 204 | |
| 205 | if output_dirs: |
| 206 | c_dirname = output_dirs[0] |
| 207 | h_dirname = output_dirs[1] if len(output_dirs) > 1 else c_dirname |
| 208 | process_dir(Ftype.SOURCE, c_dirname) |
| 209 | process_dir(Ftype.HEADER, h_dirname) |
| 210 | |
| 211 | def setup_output(self, ftype, fname): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 212 | """Set up the output destination |
| 213 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 214 | Once this is done, future calls to self.out() will output to this |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 215 | file. The file used is as follows: |
| 216 | |
| 217 | self._dirnames[ftype] is None: output to fname, or stdout if None |
| 218 | self._dirnames[ftype] is not None: output to fname in that directory |
| 219 | |
| 220 | Calling this function multiple times will close the old file and open |
| 221 | the new one. If they are the same file, nothing happens and output will |
| 222 | continue to the same file. |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 223 | |
| 224 | Args: |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 225 | ftype (str): Type of file to create ('c' or 'h') |
| 226 | fname (str): Filename to send output to. If there is a directory in |
| 227 | self._dirnames for this file type, it will be put in that |
| 228 | directory |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 229 | """ |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 230 | dirname = self._dirnames[ftype] |
| 231 | if dirname: |
| 232 | pathname = os.path.join(dirname, fname) |
| 233 | if self._outfile: |
| 234 | self._outfile.close() |
| 235 | self._outfile = open(pathname, 'w') |
| 236 | elif fname: |
| 237 | if not self._outfile: |
| 238 | self._outfile = open(fname, 'w') |
Simon Glass | f62cea0 | 2020-12-28 20:34:48 -0700 | [diff] [blame] | 239 | else: |
| 240 | self._outfile = sys.stdout |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 241 | |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 242 | def finish_output(self): |
| 243 | """Finish outputing to a file |
| 244 | |
| 245 | This closes the output file, if one is in use |
| 246 | """ |
| 247 | if self._outfile != sys.stdout: |
| 248 | self._outfile.close() |
Simon Glass | ea74c95 | 2021-02-03 06:01:20 -0700 | [diff] [blame^] | 249 | self._outfile = None |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 250 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 251 | def out(self, line): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 252 | """Output a string to the output file |
| 253 | |
| 254 | Args: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 255 | line (str): String to output |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 256 | """ |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 257 | self._outfile.write(line) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 258 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 259 | def buf(self, line): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 260 | """Buffer up a string to send later |
| 261 | |
| 262 | Args: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 263 | line (str): String to add to our 'buffer' list |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 264 | """ |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 265 | self._lines.append(line) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 266 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 267 | def get_buf(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 268 | """Get the contents of the output buffer, and clear it |
| 269 | |
| 270 | Returns: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 271 | list(str): The output buffer, which is then cleared for future use |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 272 | """ |
| 273 | lines = self._lines |
| 274 | self._lines = [] |
| 275 | return lines |
| 276 | |
Simon Glass | d1055d6 | 2020-12-28 20:35:00 -0700 | [diff] [blame] | 277 | def out_header(self, outfile): |
| 278 | """Output a message indicating that this is an auto-generated file |
| 279 | |
| 280 | Args: |
| 281 | outfile: OutputFile describing the file being generated |
| 282 | """ |
Simon Glass | d503114 | 2017-08-29 14:16:01 -0600 | [diff] [blame] | 283 | self.out('''/* |
| 284 | * DO NOT MODIFY |
| 285 | * |
Simon Glass | d1055d6 | 2020-12-28 20:35:00 -0700 | [diff] [blame] | 286 | * %s. |
| 287 | * This was generated by dtoc from a .dtb (device tree binary) file. |
Simon Glass | d503114 | 2017-08-29 14:16:01 -0600 | [diff] [blame] | 288 | */ |
| 289 | |
Simon Glass | d1055d6 | 2020-12-28 20:35:00 -0700 | [diff] [blame] | 290 | ''' % outfile.hdr_comment) |
Simon Glass | d503114 | 2017-08-29 14:16:01 -0600 | [diff] [blame] | 291 | |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 292 | def get_phandle_argc(self, prop, node_name): |
| 293 | """Check if a node contains phandles |
Simon Glass | 2925c26 | 2017-08-29 14:15:54 -0600 | [diff] [blame] | 294 | |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 295 | We have no reliable way of detecting whether a node uses a phandle |
| 296 | 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] | 297 | |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 298 | Args: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 299 | prop (fdt.Prop): Prop object to check |
| 300 | node_name (str): Node name, only used for raising an error |
| 301 | Returns: |
| 302 | int or None: Number of argument cells is this is a phandle, |
| 303 | else None |
| 304 | Raises: |
| 305 | ValueError: if the phandle cannot be parsed or the required property |
| 306 | is not present |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 307 | """ |
Simon Glass | 8840bc5 | 2021-02-03 06:01:18 -0700 | [diff] [blame] | 308 | cells_prop = None |
| 309 | for name, cprop in PHANDLE_PROPS.items(): |
| 310 | if prop.name.endswith(name): |
| 311 | cells_prop = cprop |
| 312 | if cells_prop: |
Simon Glass | 760b717 | 2018-07-06 10:27:31 -0600 | [diff] [blame] | 313 | if not isinstance(prop.value, list): |
| 314 | prop.value = [prop.value] |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 315 | val = prop.value |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 316 | i = 0 |
| 317 | |
| 318 | max_args = 0 |
| 319 | args = [] |
| 320 | while i < len(val): |
| 321 | phandle = fdt_util.fdt32_to_cpu(val[i]) |
Simon Glass | 760b717 | 2018-07-06 10:27:31 -0600 | [diff] [blame] | 322 | # If we get to the end of the list, stop. This can happen |
| 323 | # since some nodes have more phandles in the list than others, |
| 324 | # but we allocate enough space for the largest list. So those |
| 325 | # nodes with shorter lists end up with zeroes at the end. |
| 326 | if not phandle: |
| 327 | break |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 328 | target = self._fdt.phandle_to_node.get(phandle) |
| 329 | if not target: |
| 330 | raise ValueError("Cannot parse '%s' in node '%s'" % |
| 331 | (prop.name, node_name)) |
Simon Glass | 8840bc5 | 2021-02-03 06:01:18 -0700 | [diff] [blame] | 332 | cells = target.props.get(cells_prop) |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 333 | if not cells: |
Walter Lozano | ad34017 | 2020-06-25 01:10:16 -0300 | [diff] [blame] | 334 | raise ValueError("Node '%s' has no cells property" % |
Simon Glass | 8840bc5 | 2021-02-03 06:01:18 -0700 | [diff] [blame] | 335 | target.name) |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 336 | num_args = fdt_util.fdt32_to_cpu(cells.value) |
| 337 | max_args = max(max_args, num_args) |
| 338 | args.append(num_args) |
| 339 | i += 1 + num_args |
| 340 | return PhandleInfo(max_args, args) |
| 341 | return None |
Simon Glass | 2925c26 | 2017-08-29 14:15:54 -0600 | [diff] [blame] | 342 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 343 | def scan_dtb(self): |
Anatolij Gustschin | f1a7ba1 | 2017-08-18 17:58:51 +0200 | [diff] [blame] | 344 | """Scan the device tree to obtain a tree of nodes and properties |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 345 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 346 | 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] | 347 | device tree root node, and progress from there. |
| 348 | """ |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 349 | self._fdt = fdt.FdtScan(self._dtb_fname) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 350 | |
Simon Glass | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 351 | def scan_node(self, node, valid_nodes): |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 352 | """Scan a node and subnodes to build a tree of node and phandle info |
| 353 | |
Simon Glass | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 354 | This adds each subnode to self._valid_nodes if it is enabled and has a |
| 355 | compatible string. |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 356 | |
| 357 | Args: |
Simon Glass | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 358 | node (Node): Node for scan for subnodes |
Simon Glass | ccc3da7 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 359 | valid_nodes (list of Node): List of Node objects to add to |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 360 | """ |
Simon Glass | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 361 | for subnode in node.subnodes: |
| 362 | if 'compatible' in subnode.props: |
| 363 | status = subnode.props.get('status') |
Simon Glass | e36024b | 2017-06-18 22:09:01 -0600 | [diff] [blame] | 364 | if (not self._include_disabled and not status or |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 365 | status.value != 'disabled'): |
Simon Glass | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 366 | valid_nodes.append(subnode) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 367 | |
| 368 | # recurse to handle any subnodes |
Simon Glass | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 369 | self.scan_node(subnode, valid_nodes) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 370 | |
Simon Glass | 50aae3e | 2021-02-03 06:01:11 -0700 | [diff] [blame] | 371 | def scan_tree(self, add_root): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 372 | """Scan the device tree for useful information |
| 373 | |
| 374 | This fills in the following properties: |
Simon Glass | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 375 | _valid_nodes_unsorted: A list of nodes we wish to consider include |
| 376 | in the platform data (in devicetree node order) |
| 377 | _valid_nodes: Sorted version of _valid_nodes_unsorted |
Simon Glass | 50aae3e | 2021-02-03 06:01:11 -0700 | [diff] [blame] | 378 | |
| 379 | Args: |
| 380 | add_root: True to add the root node also (which wouldn't normally |
| 381 | be added as it may not have a compatible string) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 382 | """ |
Simon Glass | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 383 | root = self._fdt.GetRoot() |
Simon Glass | 1b27273 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 384 | valid_nodes = [] |
Simon Glass | 50aae3e | 2021-02-03 06:01:11 -0700 | [diff] [blame] | 385 | if add_root: |
| 386 | valid_nodes.append(root) |
Simon Glass | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 387 | self.scan_node(root, valid_nodes) |
| 388 | self._valid_nodes_unsorted = valid_nodes |
Simon Glass | 1b27273 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 389 | self._valid_nodes = sorted(valid_nodes, |
| 390 | key=lambda x: conv_name_to_c(x.name)) |
Simon Glass | 51d5d05 | 2021-02-03 06:00:58 -0700 | [diff] [blame] | 391 | |
| 392 | def prepare_nodes(self): |
| 393 | """Add extra properties to the nodes we are using |
| 394 | |
| 395 | The following properties are added for use by dtoc: |
| 396 | idx: Index number of this node (0=first, etc.) |
| 397 | struct_name: Name of the struct dtd used by this node |
| 398 | var_name: C name for this node |
| 399 | child_devs: List of child devices for this node, each a None |
| 400 | child_refs: Dict of references for each child: |
| 401 | key: Position in child list (-1=head, 0=first, 1=second, ... |
| 402 | n-1=last, n=head) |
| 403 | seq: Sequence number of the device (unique within its uclass), or |
| 404 | -1 not not known yet |
| 405 | dev_ref: Reference to this device, e.g. 'DM_DEVICE_REF(serial)' |
| 406 | driver: Driver record for this node, or None if not known |
| 407 | uclass: Uclass record for this node, or None if not known |
| 408 | uclass_seq: Position of this device within the uclass list (0=first, |
| 409 | n-1=last) |
| 410 | parent_seq: Position of this device within it siblings (0=first, |
| 411 | n-1=last) |
| 412 | parent_driver: Driver record of the node's parent, or None if none. |
| 413 | We don't use node.parent.driver since node.parent may not be in |
| 414 | the list of valid nodes |
| 415 | """ |
Simon Glass | 1b27273 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 416 | for idx, node in enumerate(self._valid_nodes): |
| 417 | node.idx = idx |
Simon Glass | 51d5d05 | 2021-02-03 06:00:58 -0700 | [diff] [blame] | 418 | node.struct_name, _ = self._scan.get_normalized_compat_name(node) |
| 419 | node.var_name = conv_name_to_c(node.name) |
| 420 | node.child_devs = [] |
| 421 | node.child_refs = {} |
| 422 | node.seq = -1 |
| 423 | node.dev_ref = None |
| 424 | node.driver = None |
| 425 | node.uclass = None |
| 426 | node.uclass_seq = None |
| 427 | node.parent_seq = None |
| 428 | node.parent_driver = None |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 429 | |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 430 | @staticmethod |
| 431 | def get_num_cells(node): |
| 432 | """Get the number of cells in addresses and sizes for this node |
| 433 | |
| 434 | Args: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 435 | node (fdt.None): Node to check |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 436 | |
| 437 | Returns: |
| 438 | Tuple: |
| 439 | Number of address cells for this node |
| 440 | Number of size cells for this node |
| 441 | """ |
| 442 | parent = node.parent |
Simon Glass | 78128d5 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 443 | num_addr, num_size = 2, 2 |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 444 | if parent: |
Simon Glass | 78128d5 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 445 | addr_prop = parent.props.get('#address-cells') |
| 446 | size_prop = parent.props.get('#size-cells') |
| 447 | if addr_prop: |
| 448 | num_addr = fdt_util.fdt32_to_cpu(addr_prop.value) |
| 449 | if size_prop: |
| 450 | num_size = fdt_util.fdt32_to_cpu(size_prop.value) |
| 451 | return num_addr, num_size |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 452 | |
| 453 | def scan_reg_sizes(self): |
| 454 | """Scan for 64-bit 'reg' properties and update the values |
| 455 | |
| 456 | This finds 'reg' properties with 64-bit data and converts the value to |
| 457 | an array of 64-values. This allows it to be output in a way that the |
| 458 | C code can read. |
| 459 | """ |
| 460 | for node in self._valid_nodes: |
| 461 | reg = node.props.get('reg') |
| 462 | if not reg: |
| 463 | continue |
Simon Glass | 78128d5 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 464 | num_addr, num_size = self.get_num_cells(node) |
| 465 | total = num_addr + num_size |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 466 | |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 467 | if reg.type != fdt.Type.INT: |
Simon Glass | dfe5f5b | 2018-07-06 10:27:32 -0600 | [diff] [blame] | 468 | raise ValueError("Node '%s' reg property is not an int" % |
| 469 | node.name) |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 470 | if len(reg.value) % total: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 471 | raise ValueError( |
| 472 | "Node '%s' reg property has %d cells " |
| 473 | 'which is not a multiple of na + ns = %d + %d)' % |
Simon Glass | 78128d5 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 474 | (node.name, len(reg.value), num_addr, num_size)) |
| 475 | reg.num_addr = num_addr |
| 476 | reg.num_size = num_size |
| 477 | if num_addr != 1 or num_size != 1: |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 478 | reg.type = fdt.Type.INT64 |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 479 | i = 0 |
| 480 | new_value = [] |
| 481 | val = reg.value |
| 482 | if not isinstance(val, list): |
| 483 | val = [val] |
| 484 | while i < len(val): |
Simon Glass | 78128d5 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 485 | addr = fdt_util.fdt_cells_to_cpu(val[i:], reg.num_addr) |
| 486 | i += num_addr |
| 487 | size = fdt_util.fdt_cells_to_cpu(val[i:], reg.num_size) |
| 488 | i += num_size |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 489 | new_value += [addr, size] |
| 490 | reg.value = new_value |
| 491 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 492 | def scan_structs(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 493 | """Scan the device tree building up the C structures we will use. |
| 494 | |
| 495 | Build a dict keyed by C struct name containing a dict of Prop |
| 496 | object for each struct field (keyed by property name). Where the |
| 497 | same struct appears multiple times, try to use the 'widest' |
| 498 | property, i.e. the one with a type which can express all others. |
| 499 | |
| 500 | Once the widest property is determined, all other properties are |
| 501 | updated to match that width. |
Simon Glass | e4fb5fa | 2020-10-03 11:31:24 -0600 | [diff] [blame] | 502 | |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 503 | The results are written to self._struct_data |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 504 | """ |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 505 | structs = self._struct_data |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 506 | for node in self._valid_nodes: |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 507 | fields = {} |
| 508 | |
| 509 | # Get a list of all the valid properties in this node. |
| 510 | for name, prop in node.props.items(): |
| 511 | if name not in PROP_IGNORE_LIST and name[0] != '#': |
| 512 | fields[name] = copy.deepcopy(prop) |
| 513 | |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame] | 514 | # If we've seen this struct_name before, update the existing struct |
| 515 | if node.struct_name in structs: |
| 516 | struct = structs[node.struct_name] |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 517 | for name, prop in fields.items(): |
| 518 | oldprop = struct.get(name) |
| 519 | if oldprop: |
| 520 | oldprop.Widen(prop) |
| 521 | else: |
| 522 | struct[name] = prop |
| 523 | |
| 524 | # Otherwise store this as a new struct. |
| 525 | else: |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame] | 526 | structs[node.struct_name] = fields |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 527 | |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 528 | for node in self._valid_nodes: |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame] | 529 | struct = structs[node.struct_name] |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 530 | for name, prop in node.props.items(): |
| 531 | if name not in PROP_IGNORE_LIST and name[0] != '#': |
| 532 | prop.Widen(struct[name]) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 533 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 534 | def scan_phandles(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 535 | """Figure out what phandles each node uses |
| 536 | |
| 537 | We need to be careful when outputing nodes that use phandles since |
| 538 | they must come after the declaration of the phandles in the C file. |
| 539 | Otherwise we get a compiler error since the phandle struct is not yet |
| 540 | declared. |
| 541 | |
| 542 | This function adds to each node a list of phandle nodes that the node |
| 543 | depends on. This allows us to output things in the right order. |
| 544 | """ |
| 545 | for node in self._valid_nodes: |
| 546 | node.phandles = set() |
| 547 | for pname, prop in node.props.items(): |
| 548 | if pname in PROP_IGNORE_LIST or pname[0] == '#': |
| 549 | continue |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 550 | info = self.get_phandle_argc(prop, node.name) |
| 551 | if info: |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 552 | # Process the list as pairs of (phandle, id) |
Simon Glass | 634eba4 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 553 | pos = 0 |
| 554 | for args in info.args: |
| 555 | phandle_cell = prop.value[pos] |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 556 | phandle = fdt_util.fdt32_to_cpu(phandle_cell) |
| 557 | target_node = self._fdt.phandle_to_node[phandle] |
| 558 | node.phandles.add(target_node) |
Simon Glass | 634eba4 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 559 | pos += 1 + args |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 560 | |
| 561 | |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 562 | def generate_structs(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 563 | """Generate struct defintions for the platform data |
| 564 | |
| 565 | This writes out the body of a header file consisting of structure |
| 566 | definitions for node in self._valid_nodes. See the documentation in |
Heinrich Schuchardt | 2799a69 | 2020-02-25 21:35:39 +0100 | [diff] [blame] | 567 | doc/driver-model/of-plat.rst for more information. |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 568 | """ |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 569 | structs = self._struct_data |
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 | abf0c80 | 2020-12-23 08:11:20 -0700 | [diff] [blame] | 594 | def _output_list(self, node, prop): |
| 595 | """Output the C code for a devicetree property that holds a list |
| 596 | |
| 597 | Args: |
| 598 | node (fdt.Node): Node to output |
| 599 | prop (fdt.Prop): Prop to output |
| 600 | """ |
| 601 | self.buf('{') |
| 602 | vals = [] |
| 603 | # For phandles, output a reference to the platform data |
| 604 | # of the target node. |
| 605 | info = self.get_phandle_argc(prop, node.name) |
| 606 | if info: |
| 607 | # Process the list as pairs of (phandle, id) |
| 608 | pos = 0 |
| 609 | for args in info.args: |
| 610 | phandle_cell = prop.value[pos] |
| 611 | phandle = fdt_util.fdt32_to_cpu(phandle_cell) |
| 612 | target_node = self._fdt.phandle_to_node[phandle] |
| 613 | arg_values = [] |
| 614 | for i in range(args): |
| 615 | arg_values.append( |
| 616 | str(fdt_util.fdt32_to_cpu(prop.value[pos + 1 + i]))) |
| 617 | pos += 1 + args |
| 618 | vals.append('\t{%d, {%s}}' % (target_node.idx, |
| 619 | ', '.join(arg_values))) |
| 620 | for val in vals: |
| 621 | self.buf('\n\t\t%s,' % val) |
| 622 | else: |
| 623 | for val in prop.value: |
| 624 | vals.append(get_value(prop.type, val)) |
| 625 | |
| 626 | # Put 8 values per line to avoid very long lines. |
| 627 | for i in range(0, len(vals), 8): |
| 628 | if i: |
| 629 | self.buf(',\n\t\t') |
| 630 | self.buf(', '.join(vals[i:i + 8])) |
| 631 | self.buf('}') |
| 632 | |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame] | 633 | def _declare_device(self, node): |
Simon Glass | 221ddc1 | 2020-12-23 08:11:21 -0700 | [diff] [blame] | 634 | """Add a device declaration to the output |
| 635 | |
Simon Glass | 20e442a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 636 | This declares a U_BOOT_DRVINFO() for the device being processed |
Simon Glass | 221ddc1 | 2020-12-23 08:11:21 -0700 | [diff] [blame] | 637 | |
| 638 | Args: |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame] | 639 | node: Node to process |
Simon Glass | 221ddc1 | 2020-12-23 08:11:21 -0700 | [diff] [blame] | 640 | """ |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame] | 641 | self.buf('U_BOOT_DRVINFO(%s) = {\n' % node.var_name) |
| 642 | self.buf('\t.name\t\t= "%s",\n' % node.struct_name) |
Simon Glass | 9763e4e | 2021-02-03 06:01:19 -0700 | [diff] [blame] | 643 | self.buf('\t.plat\t\t= &%s%s,\n' % (VAL_PREFIX, node.var_name)) |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame] | 644 | self.buf('\t.plat_size\t= sizeof(%s%s),\n' % |
| 645 | (VAL_PREFIX, node.var_name)) |
Simon Glass | 221ddc1 | 2020-12-23 08:11:21 -0700 | [diff] [blame] | 646 | idx = -1 |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame] | 647 | if node.parent and node.parent in self._valid_nodes: |
| 648 | idx = node.parent.idx |
Simon Glass | 221ddc1 | 2020-12-23 08:11:21 -0700 | [diff] [blame] | 649 | self.buf('\t.parent_idx\t= %d,\n' % idx) |
| 650 | self.buf('};\n') |
| 651 | self.buf('\n') |
| 652 | |
Simon Glass | ea74c95 | 2021-02-03 06:01:20 -0700 | [diff] [blame^] | 653 | def prep_priv(self, struc, name, suffix, section='.priv_data'): |
| 654 | if not struc: |
| 655 | return None |
| 656 | var_name = '_%s%s' % (name, suffix) |
| 657 | hdr = self._scan._structs.get(struc) |
| 658 | if hdr: |
| 659 | self.buf('#include <%s>\n' % hdr.fname) |
| 660 | else: |
| 661 | print('Warning: Cannot find header file for struct %s' % struc) |
| 662 | attr = '__attribute__ ((section ("%s")))' % section |
| 663 | return var_name, struc, attr |
| 664 | |
| 665 | def alloc_priv(self, info, name, extra, suffix='_priv'): |
| 666 | result = self.prep_priv(info, name, suffix) |
| 667 | if not result: |
| 668 | return None |
| 669 | var_name, struc, section = result |
| 670 | self.buf('u8 %s_%s[sizeof(struct %s)]\n\t%s;\n' % |
| 671 | (var_name, extra, struc.strip(), section)) |
| 672 | return '%s_%s' % (var_name, extra) |
| 673 | |
Simon Glass | 161dac1 | 2020-12-23 08:11:22 -0700 | [diff] [blame] | 674 | def _output_prop(self, node, prop): |
| 675 | """Output a line containing the value of a struct member |
| 676 | |
| 677 | Args: |
| 678 | node (Node): Node being output |
| 679 | prop (Prop): Prop object to output |
| 680 | """ |
| 681 | if prop.name in PROP_IGNORE_LIST or prop.name[0] == '#': |
| 682 | return |
| 683 | member_name = conv_name_to_c(prop.name) |
| 684 | self.buf('\t%s= ' % tab_to(3, '.' + member_name)) |
| 685 | |
| 686 | # Special handling for lists |
| 687 | if isinstance(prop.value, list): |
| 688 | self._output_list(node, prop) |
| 689 | else: |
| 690 | self.buf(get_value(prop.type, prop.value)) |
| 691 | self.buf(',\n') |
| 692 | |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame] | 693 | def _output_values(self, node): |
Simon Glass | 161dac1 | 2020-12-23 08:11:22 -0700 | [diff] [blame] | 694 | """Output the definition of a device's struct values |
| 695 | |
| 696 | Args: |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame] | 697 | node (Node): Node to output |
Simon Glass | 161dac1 | 2020-12-23 08:11:22 -0700 | [diff] [blame] | 698 | """ |
| 699 | self.buf('static struct %s%s %s%s = {\n' % |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame] | 700 | (STRUCT_PREFIX, node.struct_name, VAL_PREFIX, node.var_name)) |
Simon Glass | 161dac1 | 2020-12-23 08:11:22 -0700 | [diff] [blame] | 701 | for pname in sorted(node.props): |
| 702 | self._output_prop(node, node.props[pname]) |
| 703 | self.buf('};\n') |
| 704 | |
Simon Glass | ea74c95 | 2021-02-03 06:01:20 -0700 | [diff] [blame^] | 705 | def list_head(self, head_member, node_member, node_refs, var_name): |
| 706 | self.buf('\t.%s\t= {\n' % head_member) |
| 707 | if node_refs: |
| 708 | last = node_refs[-1].dev_ref |
| 709 | first = node_refs[0].dev_ref |
| 710 | member = node_member |
| 711 | else: |
| 712 | last = 'DM_DEVICE_REF(%s)' % var_name |
| 713 | first = last |
| 714 | member = head_member |
| 715 | self.buf('\t\t.prev = &%s->%s,\n' % (last, member)) |
| 716 | self.buf('\t\t.next = &%s->%s,\n' % (first, member)) |
| 717 | self.buf('\t},\n') |
| 718 | |
| 719 | def list_node(self, member, node_refs, seq): |
| 720 | self.buf('\t.%s\t= {\n' % member) |
| 721 | self.buf('\t\t.prev = %s,\n' % node_refs[seq - 1]) |
| 722 | self.buf('\t\t.next = %s,\n' % node_refs[seq + 1]) |
| 723 | self.buf('\t},\n') |
| 724 | |
| 725 | def generate_uclasses(self): |
| 726 | if not self.check_instantiate(True): |
| 727 | return |
| 728 | self.out('\n') |
| 729 | self.out('#include <common.h>\n') |
| 730 | self.out('#include <dm.h>\n') |
| 731 | self.out('#include <dt-structs.h>\n') |
| 732 | self.out('\n') |
| 733 | self.buf('/*\n') |
| 734 | self.buf(' * uclass declarations\n') |
| 735 | self.buf(' *\n') |
| 736 | self.buf(' * Sequence numbers:\n') |
| 737 | uclass_list = self._valid_uclasses |
| 738 | for uclass in uclass_list: |
| 739 | if uclass.alias_num_to_node: |
| 740 | self.buf(' * %s: %s\n' % (uclass.name, uclass.uclass_id)) |
| 741 | for seq, node in uclass.alias_num_to_node.items(): |
| 742 | self.buf(' * %d: %s\n' % (seq, node.path)) |
| 743 | self.buf(' */\n') |
| 744 | |
| 745 | uclass_node = {} |
| 746 | for seq, uclass in enumerate(uclass_list): |
| 747 | uclass_node[seq] = ('&DM_UCLASS_REF(%s)->sibling_node' % |
| 748 | uclass.name) |
| 749 | uclass_node[-1] = '&uclass_head' |
| 750 | uclass_node[len(uclass_list)] = '&uclass_head' |
| 751 | self.buf('\n') |
| 752 | self.buf('struct list_head %s = {\n' % 'uclass_head') |
| 753 | self.buf('\t.prev = %s,\n' % uclass_node[len(uclass_list) -1]) |
| 754 | self.buf('\t.next = %s,\n' % uclass_node[0]) |
| 755 | self.buf('};\n') |
| 756 | self.buf('\n') |
| 757 | |
| 758 | for seq, uclass in enumerate(uclass_list): |
| 759 | uc_drv = self._scan._uclass.get(uclass.uclass_id) |
| 760 | |
| 761 | priv_name = self.alloc_priv(uc_drv.priv, uc_drv.name, '') |
| 762 | |
| 763 | self.buf('DM_UCLASS_INST(%s) = {\n' % uclass.name) |
| 764 | if priv_name: |
| 765 | self.buf('\t.priv_\t\t= %s,\n' % priv_name) |
| 766 | self.buf('\t.uc_drv\t\t= DM_UCLASS_DRIVER_REF(%s),\n' % uclass.name) |
| 767 | self.list_node('sibling_node', uclass_node, seq) |
| 768 | self.list_head('dev_head', 'uclass_node', uc_drv.devs, None) |
| 769 | self.buf('};\n') |
| 770 | self.buf('\n') |
| 771 | self.out(''.join(self.get_buf())) |
| 772 | |
Simon Glass | 0595352 | 2021-02-03 06:01:07 -0700 | [diff] [blame] | 773 | def read_aliases(self): |
| 774 | """Read the aliases and attach the information to self._alias |
| 775 | |
| 776 | Raises: |
| 777 | ValueError: The alias path is not found |
| 778 | """ |
| 779 | alias_node = self._fdt.GetNode('/aliases') |
| 780 | if not alias_node: |
| 781 | return |
| 782 | re_num = re.compile('(^[a-z0-9-]+[a-z]+)([0-9]+)$') |
| 783 | for prop in alias_node.props.values(): |
| 784 | m_alias = re_num.match(prop.name) |
| 785 | if not m_alias: |
| 786 | raise ValueError("Cannot decode alias '%s'" % prop.name) |
| 787 | name, num = m_alias.groups() |
| 788 | node = self._fdt.GetNode(prop.value) |
| 789 | result = self._scan.add_uclass_alias(name, num, node) |
| 790 | if result is None: |
| 791 | raise ValueError("Alias '%s' path '%s' not found" % |
| 792 | (prop.name, prop.value)) |
| 793 | elif result is False: |
| 794 | print("Could not find uclass for alias '%s'" % prop.name) |
| 795 | |
Simon Glass | 426d12f | 2021-02-03 06:01:14 -0700 | [diff] [blame] | 796 | def generate_decl(self): |
| 797 | nodes_to_output = list(self._valid_nodes) |
| 798 | |
| 799 | self.buf('#include <dm/device-internal.h>\n') |
| 800 | self.buf('#include <dm/uclass-internal.h>\n') |
| 801 | self.buf('\n') |
| 802 | self.buf( |
| 803 | '/* driver declarations - these allow DM_DRIVER_GET() to be used */\n') |
| 804 | for node in nodes_to_output: |
| 805 | self.buf('DM_DRIVER_DECL(%s);\n' % node.struct_name); |
| 806 | self.buf('\n') |
| 807 | |
| 808 | if self._instantiate: |
| 809 | self.buf( |
| 810 | '/* device declarations - these allow DM_DEVICE_REF() to be used */\n') |
| 811 | for node in nodes_to_output: |
| 812 | self.buf('DM_DEVICE_DECL(%s);\n' % node.var_name) |
| 813 | self.buf('\n') |
| 814 | |
| 815 | uclass_list = self._valid_uclasses |
| 816 | |
| 817 | self.buf( |
| 818 | '/* uclass driver declarations - needed for DM_UCLASS_DRIVER_REF() */\n') |
| 819 | for uclass in uclass_list: |
| 820 | self.buf('DM_UCLASS_DRIVER_DECL(%s);\n' % uclass.name) |
| 821 | |
| 822 | if self._instantiate: |
| 823 | self.buf('\n') |
| 824 | self.buf('/* uclass declarations - needed for DM_UCLASS_REF() */\n') |
| 825 | for uclass in uclass_list: |
| 826 | self.buf('DM_UCLASS_DECL(%s);\n' % uclass.name) |
| 827 | self.out(''.join(self.get_buf())) |
| 828 | |
Simon Glass | 337d697 | 2021-02-03 06:01:10 -0700 | [diff] [blame] | 829 | def assign_seqs(self): |
Simon Glass | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 830 | """Assign a sequence number to each node""" |
| 831 | for node in self._valid_nodes_unsorted: |
Simon Glass | 337d697 | 2021-02-03 06:01:10 -0700 | [diff] [blame] | 832 | seq = self._scan.assign_seq(node) |
| 833 | if seq is not None: |
| 834 | node.seq = seq |
Simon Glass | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 835 | |
Simon Glass | fd471e2 | 2021-02-03 06:01:00 -0700 | [diff] [blame] | 836 | def process_nodes(self, need_drivers): |
| 837 | nodes_to_output = list(self._valid_nodes) |
| 838 | |
Simon Glass | b9319c4 | 2021-02-03 06:01:01 -0700 | [diff] [blame] | 839 | # Figure out which drivers we actually use |
| 840 | self._scan.mark_used(nodes_to_output) |
| 841 | |
Simon Glass | fd471e2 | 2021-02-03 06:01:00 -0700 | [diff] [blame] | 842 | for node in nodes_to_output: |
| 843 | node.dev_ref = 'DM_DEVICE_REF(%s)' % node.var_name |
| 844 | driver = self._scan.get_driver(node.struct_name) |
| 845 | if not driver: |
| 846 | if not need_drivers: |
| 847 | continue |
| 848 | raise ValueError("Cannot parse/find driver for '%s'" % |
| 849 | node.struct_name) |
| 850 | node.driver = driver |
Simon Glass | 337d697 | 2021-02-03 06:01:10 -0700 | [diff] [blame] | 851 | uclass = self._scan._uclass.get(driver.uclass_id) |
| 852 | if not uclass: |
| 853 | raise ValueError("Cannot parse/find uclass '%s' for driver '%s'" % |
| 854 | (driver.uclass_id, node.struct_name)) |
| 855 | node.uclass = uclass |
| 856 | node.uclass_seq = len(node.uclass.devs) |
| 857 | node.uclass.devs.append(node) |
| 858 | uclass.node_refs[node.uclass_seq] = \ |
| 859 | '&%s->uclass_node' % node.dev_ref |
| 860 | |
Simon Glass | fd471e2 | 2021-02-03 06:01:00 -0700 | [diff] [blame] | 861 | parent_driver = None |
| 862 | if node.parent in self._valid_nodes: |
| 863 | parent_driver = self._scan.get_driver(node.parent.struct_name) |
| 864 | if not parent_driver: |
| 865 | if not need_drivers: |
| 866 | continue |
| 867 | raise ValueError( |
| 868 | "Cannot parse/find parent driver '%s' for '%s'" % |
| 869 | (node.parent.struct_name, node.struct_name)) |
| 870 | node.parent_seq = len(node.parent.child_devs) |
| 871 | node.parent.child_devs.append(node) |
| 872 | node.parent.child_refs[node.parent_seq] = \ |
| 873 | '&%s->sibling_node' % node.dev_ref |
| 874 | node.parent_driver = parent_driver |
| 875 | |
| 876 | for node in nodes_to_output: |
| 877 | ref = '&%s->child_head' % node.dev_ref |
| 878 | node.child_refs[-1] = ref |
| 879 | node.child_refs[len(node.child_devs)] = ref |
| 880 | |
Simon Glass | 337d697 | 2021-02-03 06:01:10 -0700 | [diff] [blame] | 881 | uclass_set = set() |
| 882 | for driver in self._scan._drivers.values(): |
| 883 | if driver.used and driver.uclass: |
| 884 | uclass_set.add(driver.uclass) |
| 885 | self._valid_uclasses = sorted(list(uclass_set), |
| 886 | key=lambda uc: uc.uclass_id) |
| 887 | |
| 888 | for seq, uclass in enumerate(uclass_set): |
| 889 | ref = '&DM_UCLASS_REF(%s)->dev_head' % uclass.name |
| 890 | uclass.node_refs[-1] = ref |
| 891 | uclass.node_refs[len(uclass.devs)] = ref |
| 892 | |
Simon Glass | 4b91be2 | 2021-02-03 06:01:15 -0700 | [diff] [blame] | 893 | def output_node_plat(self, node): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 894 | """Output the C code for a node |
| 895 | |
| 896 | Args: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 897 | node (fdt.Node): node to output |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 898 | """ |
Simon Glass | 4b91be2 | 2021-02-03 06:01:15 -0700 | [diff] [blame] | 899 | driver = node.driver |
| 900 | parent_driver = node.parent_driver |
| 901 | |
| 902 | line1 = 'Node %s index %d' % (node.path, node.idx) |
| 903 | if driver: |
| 904 | self.buf('/*\n') |
| 905 | self.buf(' * %s\n' % line1) |
| 906 | self.buf(' * driver %s parent %s\n' % (driver.name, |
| 907 | parent_driver.name if parent_driver else 'None')) |
| 908 | self.buf(' */\n') |
| 909 | else: |
| 910 | self.buf('/* %s */\n' % line1) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 911 | |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame] | 912 | self._output_values(node) |
| 913 | self._declare_device(node) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 914 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 915 | self.out(''.join(self.get_buf())) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 916 | |
Simon Glass | 4b91be2 | 2021-02-03 06:01:15 -0700 | [diff] [blame] | 917 | def check_instantiate(self, require): |
| 918 | """Check if self._instantiate is set to the required value |
| 919 | |
| 920 | If not, this outputs a message into the current file |
| 921 | |
| 922 | Args: |
| 923 | require: True to require --instantiate, False to require that it not |
| 924 | be enabled |
| 925 | """ |
| 926 | if require != self._instantiate: |
| 927 | self.out( |
| 928 | '/* This file is not used: --instantiate was %senabled */\n' % |
| 929 | ('not ' if require else '')) |
| 930 | return False |
| 931 | return True |
| 932 | |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 933 | def generate_plat(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 934 | """Generate device defintions for the platform data |
| 935 | |
| 936 | This writes out C platform data initialisation data and |
Simon Glass | 20e442a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 937 | U_BOOT_DRVINFO() declarations for each valid node. Where a node has |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 938 | multiple compatible strings, a #define is used to make them equivalent. |
| 939 | |
Heinrich Schuchardt | 2799a69 | 2020-02-25 21:35:39 +0100 | [diff] [blame] | 940 | See the documentation in doc/driver-model/of-plat.rst for more |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 941 | information. |
| 942 | """ |
Simon Glass | 4b91be2 | 2021-02-03 06:01:15 -0700 | [diff] [blame] | 943 | if not self.check_instantiate(False): |
| 944 | return |
Simon Glass | 20e442a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 945 | self.out('/* Allow use of U_BOOT_DRVINFO() in this file */\n') |
Simon Glass | f31fa99 | 2020-12-28 20:35:01 -0700 | [diff] [blame] | 946 | self.out('#define DT_PLAT_C\n') |
Simon Glass | cb43ac1 | 2020-10-03 11:31:41 -0600 | [diff] [blame] | 947 | self.out('\n') |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 948 | self.out('#include <common.h>\n') |
| 949 | self.out('#include <dm.h>\n') |
| 950 | self.out('#include <dt-structs.h>\n') |
| 951 | self.out('\n') |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 952 | |
Simon Glass | 9763e4e | 2021-02-03 06:01:19 -0700 | [diff] [blame] | 953 | if self._valid_nodes: |
| 954 | self.out('/*\n') |
| 955 | self.out( |
| 956 | " * driver_info declarations, ordered by 'struct driver_info' linker_list idx:\n") |
| 957 | self.out(' *\n') |
| 958 | self.out(' * idx %-20s %-s\n' % ('driver_info', 'driver')) |
| 959 | self.out(' * --- %-20s %-s\n' % ('-' * 20, '-' * 20)) |
| 960 | for node in self._valid_nodes: |
| 961 | self.out(' * %3d: %-20s %-s\n' % |
| 962 | (node.idx, node.var_name, node.struct_name)) |
| 963 | self.out(' * --- %-20s %-s\n' % ('-' * 20, '-' * 20)) |
| 964 | self.out(' */\n') |
| 965 | self.out('\n') |
| 966 | |
| 967 | for node in self._valid_nodes: |
| 968 | self.output_node_plat(node) |
Simon Glass | fa0ea5b | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 969 | |
Walter Lozano | 51f1263 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 970 | self.out(''.join(self.get_buf())) |
Simon Glass | fa0ea5b | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 971 | |
Simon Glass | 192c111 | 2020-12-28 20:34:50 -0700 | [diff] [blame] | 972 | |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 973 | # Types of output file we understand |
| 974 | # key: Command used to generate this file |
| 975 | # value: OutputFile for this command |
| 976 | OUTPUT_FILES = { |
Simon Glass | 426d12f | 2021-02-03 06:01:14 -0700 | [diff] [blame] | 977 | 'decl': |
| 978 | OutputFile(Ftype.HEADER, 'dt-decl.h', DtbPlatdata.generate_decl, |
| 979 | 'Declares externs for all device/uclass instances'), |
Simon Glass | d1055d6 | 2020-12-28 20:35:00 -0700 | [diff] [blame] | 980 | 'struct': |
| 981 | OutputFile(Ftype.HEADER, 'dt-structs-gen.h', |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 982 | DtbPlatdata.generate_structs, |
Simon Glass | d1055d6 | 2020-12-28 20:35:00 -0700 | [diff] [blame] | 983 | 'Defines the structs used to hold devicetree data'), |
| 984 | 'platdata': |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 985 | OutputFile(Ftype.SOURCE, 'dt-plat.c', DtbPlatdata.generate_plat, |
Simon Glass | d1055d6 | 2020-12-28 20:35:00 -0700 | [diff] [blame] | 986 | 'Declares the U_BOOT_DRIVER() records and platform data'), |
Simon Glass | ea74c95 | 2021-02-03 06:01:20 -0700 | [diff] [blame^] | 987 | 'uclass': |
| 988 | OutputFile(Ftype.SOURCE, 'dt-uclass.c', DtbPlatdata.generate_uclasses, |
| 989 | 'Declares the uclass instances (struct uclass)'), |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | |
Simon Glass | b00f006 | 2021-02-03 06:01:02 -0700 | [diff] [blame] | 993 | def run_steps(args, dtb_file, include_disabled, output, output_dirs, phase, |
Simon Glass | 4a09235 | 2021-02-03 06:01:12 -0700 | [diff] [blame] | 994 | instantiate, warning_disabled=False, drivers_additional=None, |
| 995 | basedir=None, scan=None): |
Simon Glass | fa0ea5b | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 996 | """Run all the steps of the dtoc tool |
| 997 | |
| 998 | Args: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 999 | args (list): List of non-option arguments provided to the problem |
| 1000 | dtb_file (str): Filename of dtb file to process |
| 1001 | include_disabled (bool): True to include disabled nodes |
Simon Glass | f62cea0 | 2020-12-28 20:34:48 -0700 | [diff] [blame] | 1002 | output (str): Name of output file (None for stdout) |
Simon Glass | 192c111 | 2020-12-28 20:34:50 -0700 | [diff] [blame] | 1003 | output_dirs (tuple of str): |
| 1004 | Directory to put C output files |
| 1005 | Directory to put H output files |
Simon Glass | b00f006 | 2021-02-03 06:01:02 -0700 | [diff] [blame] | 1006 | phase: The phase of U-Boot that we are generating data for, e.g. 'spl' |
| 1007 | or 'tpl'. None if not known |
Simon Glass | 4a09235 | 2021-02-03 06:01:12 -0700 | [diff] [blame] | 1008 | instantiate: Instantiate devices so they don't need to be bound at |
| 1009 | run-time |
Simon Glass | 78128d5 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 1010 | warning_disabled (bool): True to avoid showing warnings about missing |
| 1011 | drivers |
Simon Glass | ccc3da7 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 1012 | drivers_additional (list): List of additional drivers to use during |
Simon Glass | 78128d5 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 1013 | scanning |
Simon Glass | 1e0f3f4 | 2020-12-28 20:35:03 -0700 | [diff] [blame] | 1014 | basedir (str): Base directory of U-Boot source code. Defaults to the |
| 1015 | grandparent of this file's directory |
Simon Glass | a32eb7d | 2021-02-03 06:00:51 -0700 | [diff] [blame] | 1016 | scan (src_src.Scanner): Scanner from a previous run. This can help speed |
| 1017 | up tests. Use None for normal operation |
| 1018 | |
Simon Glass | 0595352 | 2021-02-03 06:01:07 -0700 | [diff] [blame] | 1019 | Returns: |
| 1020 | DtbPlatdata object |
| 1021 | |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 1022 | Raises: |
| 1023 | ValueError: if args has no command, or an unknown command |
Simon Glass | fa0ea5b | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 1024 | """ |
| 1025 | if not args: |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 1026 | raise ValueError('Please specify a command: struct, platdata, all') |
| 1027 | if output and output_dirs and any(output_dirs): |
| 1028 | raise ValueError('Must specify either output or output_dirs, not both') |
Simon Glass | fa0ea5b | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 1029 | |
Simon Glass | a32eb7d | 2021-02-03 06:00:51 -0700 | [diff] [blame] | 1030 | if not scan: |
Simon Glass | b00f006 | 2021-02-03 06:01:02 -0700 | [diff] [blame] | 1031 | scan = src_scan.Scanner(basedir, warning_disabled, drivers_additional, |
| 1032 | phase) |
Simon Glass | a32eb7d | 2021-02-03 06:00:51 -0700 | [diff] [blame] | 1033 | scan.scan_drivers() |
Simon Glass | fd471e2 | 2021-02-03 06:01:00 -0700 | [diff] [blame] | 1034 | do_process = True |
| 1035 | else: |
| 1036 | do_process = False |
Simon Glass | 4a09235 | 2021-02-03 06:01:12 -0700 | [diff] [blame] | 1037 | plat = DtbPlatdata(scan, dtb_file, include_disabled, instantiate) |
Simon Glass | fa0ea5b | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 1038 | plat.scan_dtb() |
Simon Glass | 4a09235 | 2021-02-03 06:01:12 -0700 | [diff] [blame] | 1039 | plat.scan_tree(add_root=instantiate) |
Simon Glass | 51d5d05 | 2021-02-03 06:00:58 -0700 | [diff] [blame] | 1040 | plat.prepare_nodes() |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 1041 | plat.scan_reg_sizes() |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 1042 | plat.setup_output_dirs(output_dirs) |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 1043 | plat.scan_structs() |
Simon Glass | fa0ea5b | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 1044 | plat.scan_phandles() |
Simon Glass | 4a09235 | 2021-02-03 06:01:12 -0700 | [diff] [blame] | 1045 | plat.process_nodes(instantiate) |
Simon Glass | 0595352 | 2021-02-03 06:01:07 -0700 | [diff] [blame] | 1046 | plat.read_aliases() |
Simon Glass | 337d697 | 2021-02-03 06:01:10 -0700 | [diff] [blame] | 1047 | plat.assign_seqs() |
Simon Glass | fa0ea5b | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 1048 | |
Simon Glass | 10cbd3b | 2020-12-28 20:34:52 -0700 | [diff] [blame] | 1049 | cmds = args[0].split(',') |
| 1050 | if 'all' in cmds: |
| 1051 | cmds = sorted(OUTPUT_FILES.keys()) |
| 1052 | for cmd in cmds: |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 1053 | outfile = OUTPUT_FILES.get(cmd) |
| 1054 | if not outfile: |
| 1055 | raise ValueError("Unknown command '%s': (use: %s)" % |
Simon Glass | 10cbd3b | 2020-12-28 20:34:52 -0700 | [diff] [blame] | 1056 | (cmd, ', '.join(sorted(OUTPUT_FILES.keys())))) |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 1057 | plat.setup_output(outfile.ftype, |
| 1058 | outfile.fname if output_dirs else output) |
Simon Glass | d1055d6 | 2020-12-28 20:35:00 -0700 | [diff] [blame] | 1059 | plat.out_header(outfile) |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 1060 | outfile.method(plat) |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 1061 | plat.finish_output() |
Simon Glass | 0595352 | 2021-02-03 06:01:07 -0700 | [diff] [blame] | 1062 | return plat |