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