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 | 1b27273 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 139 | _valid_nodes: A list of Node object with compatible strings. The list |
| 140 | is ordered by conv_name_to_c(node.name) |
Simon Glass | e36024b | 2017-06-18 22:09:01 -0600 | [diff] [blame] | 141 | _include_disabled: true to include nodes marked status = "disabled" |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 142 | _outfile: The current output file (sys.stdout or a real file) |
| 143 | _lines: Stashed list of output lines for outputting in the future |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 144 | _dirname: Directory to hold output files, or None for none (all files |
| 145 | go to stdout) |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 146 | _struct_data (dict): OrderedDict of dtplat structures to output |
| 147 | key (str): Node name, as a C identifier |
| 148 | value: dict containing structure fields: |
| 149 | key (str): Field name |
| 150 | value: Prop object with field information |
Simon Glass | 1e0f3f4 | 2020-12-28 20:35:03 -0700 | [diff] [blame] | 151 | _basedir (str): Base directory of source tree |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 152 | """ |
Simon Glass | a542a70 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 153 | def __init__(self, scan, dtb_fname, include_disabled): |
| 154 | self._scan = scan |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 155 | self._fdt = None |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 156 | self._dtb_fname = dtb_fname |
| 157 | self._valid_nodes = None |
Simon Glass | e36024b | 2017-06-18 22:09:01 -0600 | [diff] [blame] | 158 | self._include_disabled = include_disabled |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 159 | self._outfile = None |
| 160 | self._lines = [] |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 161 | self._dirnames = [None] * len(Ftype) |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 162 | self._struct_data = collections.OrderedDict() |
Simon Glass | 1e0f3f4 | 2020-12-28 20:35:03 -0700 | [diff] [blame] | 163 | self._basedir = None |
Walter Lozano | dac8228 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 164 | |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 165 | def setup_output_dirs(self, output_dirs): |
| 166 | """Set up the output directories |
| 167 | |
| 168 | This should be done before setup_output() is called |
| 169 | |
| 170 | Args: |
| 171 | output_dirs (tuple of str): |
| 172 | Directory to use for C output files. |
| 173 | Use None to write files relative current directory |
| 174 | Directory to use for H output files. |
| 175 | Defaults to the C output dir |
| 176 | """ |
| 177 | def process_dir(ftype, dirname): |
| 178 | if dirname: |
| 179 | os.makedirs(dirname, exist_ok=True) |
| 180 | self._dirnames[ftype] = dirname |
| 181 | |
| 182 | if output_dirs: |
| 183 | c_dirname = output_dirs[0] |
| 184 | h_dirname = output_dirs[1] if len(output_dirs) > 1 else c_dirname |
| 185 | process_dir(Ftype.SOURCE, c_dirname) |
| 186 | process_dir(Ftype.HEADER, h_dirname) |
| 187 | |
| 188 | def setup_output(self, ftype, fname): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 189 | """Set up the output destination |
| 190 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 191 | 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] | 192 | file. The file used is as follows: |
| 193 | |
| 194 | self._dirnames[ftype] is None: output to fname, or stdout if None |
| 195 | self._dirnames[ftype] is not None: output to fname in that directory |
| 196 | |
| 197 | Calling this function multiple times will close the old file and open |
| 198 | the new one. If they are the same file, nothing happens and output will |
| 199 | continue to the same file. |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 200 | |
| 201 | Args: |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 202 | ftype (str): Type of file to create ('c' or 'h') |
| 203 | fname (str): Filename to send output to. If there is a directory in |
| 204 | self._dirnames for this file type, it will be put in that |
| 205 | directory |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 206 | """ |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 207 | dirname = self._dirnames[ftype] |
| 208 | if dirname: |
| 209 | pathname = os.path.join(dirname, fname) |
| 210 | if self._outfile: |
| 211 | self._outfile.close() |
| 212 | self._outfile = open(pathname, 'w') |
| 213 | elif fname: |
| 214 | if not self._outfile: |
| 215 | self._outfile = open(fname, 'w') |
Simon Glass | f62cea0 | 2020-12-28 20:34:48 -0700 | [diff] [blame] | 216 | else: |
| 217 | self._outfile = sys.stdout |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 218 | |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 219 | def finish_output(self): |
| 220 | """Finish outputing to a file |
| 221 | |
| 222 | This closes the output file, if one is in use |
| 223 | """ |
| 224 | if self._outfile != sys.stdout: |
| 225 | self._outfile.close() |
| 226 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 227 | def out(self, line): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 228 | """Output a string to the output file |
| 229 | |
| 230 | Args: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 231 | line (str): String to output |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 232 | """ |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 233 | self._outfile.write(line) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 234 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 235 | def buf(self, line): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 236 | """Buffer up a string to send later |
| 237 | |
| 238 | Args: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 239 | line (str): String to add to our 'buffer' list |
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 | self._lines.append(line) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 242 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 243 | def get_buf(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 244 | """Get the contents of the output buffer, and clear it |
| 245 | |
| 246 | Returns: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 247 | list(str): The output buffer, which is then cleared for future use |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 248 | """ |
| 249 | lines = self._lines |
| 250 | self._lines = [] |
| 251 | return lines |
| 252 | |
Simon Glass | d1055d6 | 2020-12-28 20:35:00 -0700 | [diff] [blame] | 253 | def out_header(self, outfile): |
| 254 | """Output a message indicating that this is an auto-generated file |
| 255 | |
| 256 | Args: |
| 257 | outfile: OutputFile describing the file being generated |
| 258 | """ |
Simon Glass | d503114 | 2017-08-29 14:16:01 -0600 | [diff] [blame] | 259 | self.out('''/* |
| 260 | * DO NOT MODIFY |
| 261 | * |
Simon Glass | d1055d6 | 2020-12-28 20:35:00 -0700 | [diff] [blame] | 262 | * %s. |
| 263 | * This was generated by dtoc from a .dtb (device tree binary) file. |
Simon Glass | d503114 | 2017-08-29 14:16:01 -0600 | [diff] [blame] | 264 | */ |
| 265 | |
Simon Glass | d1055d6 | 2020-12-28 20:35:00 -0700 | [diff] [blame] | 266 | ''' % outfile.hdr_comment) |
Simon Glass | d503114 | 2017-08-29 14:16:01 -0600 | [diff] [blame] | 267 | |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 268 | def get_phandle_argc(self, prop, node_name): |
| 269 | """Check if a node contains phandles |
Simon Glass | 2925c26 | 2017-08-29 14:15:54 -0600 | [diff] [blame] | 270 | |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 271 | We have no reliable way of detecting whether a node uses a phandle |
| 272 | 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] | 273 | |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 274 | Args: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 275 | prop (fdt.Prop): Prop object to check |
| 276 | node_name (str): Node name, only used for raising an error |
| 277 | Returns: |
| 278 | int or None: Number of argument cells is this is a phandle, |
| 279 | else None |
| 280 | Raises: |
| 281 | ValueError: if the phandle cannot be parsed or the required property |
| 282 | is not present |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 283 | """ |
Walter Lozano | ad34017 | 2020-06-25 01:10:16 -0300 | [diff] [blame] | 284 | if prop.name in ['clocks', 'cd-gpios']: |
Simon Glass | 760b717 | 2018-07-06 10:27:31 -0600 | [diff] [blame] | 285 | if not isinstance(prop.value, list): |
| 286 | prop.value = [prop.value] |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 287 | val = prop.value |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 288 | i = 0 |
| 289 | |
| 290 | max_args = 0 |
| 291 | args = [] |
| 292 | while i < len(val): |
| 293 | phandle = fdt_util.fdt32_to_cpu(val[i]) |
Simon Glass | 760b717 | 2018-07-06 10:27:31 -0600 | [diff] [blame] | 294 | # If we get to the end of the list, stop. This can happen |
| 295 | # since some nodes have more phandles in the list than others, |
| 296 | # but we allocate enough space for the largest list. So those |
| 297 | # nodes with shorter lists end up with zeroes at the end. |
| 298 | if not phandle: |
| 299 | break |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 300 | target = self._fdt.phandle_to_node.get(phandle) |
| 301 | if not target: |
| 302 | raise ValueError("Cannot parse '%s' in node '%s'" % |
| 303 | (prop.name, node_name)) |
Walter Lozano | ad34017 | 2020-06-25 01:10:16 -0300 | [diff] [blame] | 304 | cells = None |
| 305 | for prop_name in ['#clock-cells', '#gpio-cells']: |
| 306 | cells = target.props.get(prop_name) |
| 307 | if cells: |
| 308 | break |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 309 | if not cells: |
Walter Lozano | ad34017 | 2020-06-25 01:10:16 -0300 | [diff] [blame] | 310 | raise ValueError("Node '%s' has no cells property" % |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 311 | (target.name)) |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 312 | num_args = fdt_util.fdt32_to_cpu(cells.value) |
| 313 | max_args = max(max_args, num_args) |
| 314 | args.append(num_args) |
| 315 | i += 1 + num_args |
| 316 | return PhandleInfo(max_args, args) |
| 317 | return None |
Simon Glass | 2925c26 | 2017-08-29 14:15:54 -0600 | [diff] [blame] | 318 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 319 | def scan_dtb(self): |
Anatolij Gustschin | f1a7ba1 | 2017-08-18 17:58:51 +0200 | [diff] [blame] | 320 | """Scan the device tree to obtain a tree of nodes and properties |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 321 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 322 | 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] | 323 | device tree root node, and progress from there. |
| 324 | """ |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 325 | self._fdt = fdt.FdtScan(self._dtb_fname) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 326 | |
Simon Glass | 1b27273 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 327 | def scan_node(self, root, valid_nodes): |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 328 | """Scan a node and subnodes to build a tree of node and phandle info |
| 329 | |
Simon Glass | 72ab7c5 | 2017-08-29 14:15:53 -0600 | [diff] [blame] | 330 | This adds each node to self._valid_nodes. |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 331 | |
| 332 | Args: |
Simon Glass | ccc3da7 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 333 | root (Node): Root node for scan |
| 334 | valid_nodes (list of Node): List of Node objects to add to |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 335 | """ |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 336 | for node in root.subnodes: |
| 337 | if 'compatible' in node.props: |
| 338 | status = node.props.get('status') |
Simon Glass | e36024b | 2017-06-18 22:09:01 -0600 | [diff] [blame] | 339 | if (not self._include_disabled and not status or |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 340 | status.value != 'disabled'): |
Simon Glass | 1b27273 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 341 | valid_nodes.append(node) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 342 | |
| 343 | # recurse to handle any subnodes |
Simon Glass | 1b27273 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 344 | self.scan_node(node, valid_nodes) |
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 | def scan_tree(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 347 | """Scan the device tree for useful information |
| 348 | |
| 349 | This fills in the following properties: |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 350 | _valid_nodes: A list of nodes we wish to consider include in the |
| 351 | platform data |
| 352 | """ |
Simon Glass | 1b27273 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 353 | valid_nodes = [] |
| 354 | self.scan_node(self._fdt.GetRoot(), valid_nodes) |
| 355 | self._valid_nodes = sorted(valid_nodes, |
| 356 | key=lambda x: conv_name_to_c(x.name)) |
Simon Glass | 51d5d05 | 2021-02-03 06:00:58 -0700 | [diff] [blame] | 357 | |
| 358 | def prepare_nodes(self): |
| 359 | """Add extra properties to the nodes we are using |
| 360 | |
| 361 | The following properties are added for use by dtoc: |
| 362 | idx: Index number of this node (0=first, etc.) |
| 363 | struct_name: Name of the struct dtd used by this node |
| 364 | var_name: C name for this node |
| 365 | child_devs: List of child devices for this node, each a None |
| 366 | child_refs: Dict of references for each child: |
| 367 | key: Position in child list (-1=head, 0=first, 1=second, ... |
| 368 | n-1=last, n=head) |
| 369 | seq: Sequence number of the device (unique within its uclass), or |
| 370 | -1 not not known yet |
| 371 | dev_ref: Reference to this device, e.g. 'DM_DEVICE_REF(serial)' |
| 372 | driver: Driver record for this node, or None if not known |
| 373 | uclass: Uclass record for this node, or None if not known |
| 374 | uclass_seq: Position of this device within the uclass list (0=first, |
| 375 | n-1=last) |
| 376 | parent_seq: Position of this device within it siblings (0=first, |
| 377 | n-1=last) |
| 378 | parent_driver: Driver record of the node's parent, or None if none. |
| 379 | We don't use node.parent.driver since node.parent may not be in |
| 380 | the list of valid nodes |
| 381 | """ |
Simon Glass | 1b27273 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 382 | for idx, node in enumerate(self._valid_nodes): |
| 383 | node.idx = idx |
Simon Glass | 51d5d05 | 2021-02-03 06:00:58 -0700 | [diff] [blame] | 384 | node.struct_name, _ = self._scan.get_normalized_compat_name(node) |
| 385 | node.var_name = conv_name_to_c(node.name) |
| 386 | node.child_devs = [] |
| 387 | node.child_refs = {} |
| 388 | node.seq = -1 |
| 389 | node.dev_ref = None |
| 390 | node.driver = None |
| 391 | node.uclass = None |
| 392 | node.uclass_seq = None |
| 393 | node.parent_seq = None |
| 394 | node.parent_driver = None |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 395 | |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 396 | @staticmethod |
| 397 | def get_num_cells(node): |
| 398 | """Get the number of cells in addresses and sizes for this node |
| 399 | |
| 400 | Args: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 401 | node (fdt.None): Node to check |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 402 | |
| 403 | Returns: |
| 404 | Tuple: |
| 405 | Number of address cells for this node |
| 406 | Number of size cells for this node |
| 407 | """ |
| 408 | parent = node.parent |
Simon Glass | 78128d5 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 409 | num_addr, num_size = 2, 2 |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 410 | if parent: |
Simon Glass | 78128d5 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 411 | addr_prop = parent.props.get('#address-cells') |
| 412 | size_prop = parent.props.get('#size-cells') |
| 413 | if addr_prop: |
| 414 | num_addr = fdt_util.fdt32_to_cpu(addr_prop.value) |
| 415 | if size_prop: |
| 416 | num_size = fdt_util.fdt32_to_cpu(size_prop.value) |
| 417 | return num_addr, num_size |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 418 | |
| 419 | def scan_reg_sizes(self): |
| 420 | """Scan for 64-bit 'reg' properties and update the values |
| 421 | |
| 422 | This finds 'reg' properties with 64-bit data and converts the value to |
| 423 | an array of 64-values. This allows it to be output in a way that the |
| 424 | C code can read. |
| 425 | """ |
| 426 | for node in self._valid_nodes: |
| 427 | reg = node.props.get('reg') |
| 428 | if not reg: |
| 429 | continue |
Simon Glass | 78128d5 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 430 | num_addr, num_size = self.get_num_cells(node) |
| 431 | total = num_addr + num_size |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 432 | |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 433 | if reg.type != fdt.Type.INT: |
Simon Glass | dfe5f5b | 2018-07-06 10:27:32 -0600 | [diff] [blame] | 434 | raise ValueError("Node '%s' reg property is not an int" % |
| 435 | node.name) |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 436 | if len(reg.value) % total: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 437 | raise ValueError( |
| 438 | "Node '%s' reg property has %d cells " |
| 439 | 'which is not a multiple of na + ns = %d + %d)' % |
Simon Glass | 78128d5 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 440 | (node.name, len(reg.value), num_addr, num_size)) |
| 441 | reg.num_addr = num_addr |
| 442 | reg.num_size = num_size |
| 443 | if num_addr != 1 or num_size != 1: |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 444 | reg.type = fdt.Type.INT64 |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 445 | i = 0 |
| 446 | new_value = [] |
| 447 | val = reg.value |
| 448 | if not isinstance(val, list): |
| 449 | val = [val] |
| 450 | while i < len(val): |
Simon Glass | 78128d5 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 451 | addr = fdt_util.fdt_cells_to_cpu(val[i:], reg.num_addr) |
| 452 | i += num_addr |
| 453 | size = fdt_util.fdt_cells_to_cpu(val[i:], reg.num_size) |
| 454 | i += num_size |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 455 | new_value += [addr, size] |
| 456 | reg.value = new_value |
| 457 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 458 | def scan_structs(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 459 | """Scan the device tree building up the C structures we will use. |
| 460 | |
| 461 | Build a dict keyed by C struct name containing a dict of Prop |
| 462 | object for each struct field (keyed by property name). Where the |
| 463 | same struct appears multiple times, try to use the 'widest' |
| 464 | property, i.e. the one with a type which can express all others. |
| 465 | |
| 466 | Once the widest property is determined, all other properties are |
| 467 | updated to match that width. |
Simon Glass | e4fb5fa | 2020-10-03 11:31:24 -0600 | [diff] [blame] | 468 | |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 469 | The results are written to self._struct_data |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 470 | """ |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 471 | structs = self._struct_data |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 472 | for node in self._valid_nodes: |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 473 | fields = {} |
| 474 | |
| 475 | # Get a list of all the valid properties in this node. |
| 476 | for name, prop in node.props.items(): |
| 477 | if name not in PROP_IGNORE_LIST and name[0] != '#': |
| 478 | fields[name] = copy.deepcopy(prop) |
| 479 | |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame^] | 480 | # If we've seen this struct_name before, update the existing struct |
| 481 | if node.struct_name in structs: |
| 482 | struct = structs[node.struct_name] |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 483 | for name, prop in fields.items(): |
| 484 | oldprop = struct.get(name) |
| 485 | if oldprop: |
| 486 | oldprop.Widen(prop) |
| 487 | else: |
| 488 | struct[name] = prop |
| 489 | |
| 490 | # Otherwise store this as a new struct. |
| 491 | else: |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame^] | 492 | structs[node.struct_name] = fields |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 493 | |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 494 | for node in self._valid_nodes: |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame^] | 495 | struct = structs[node.struct_name] |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 496 | for name, prop in node.props.items(): |
| 497 | if name not in PROP_IGNORE_LIST and name[0] != '#': |
| 498 | prop.Widen(struct[name]) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 499 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 500 | def scan_phandles(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 501 | """Figure out what phandles each node uses |
| 502 | |
| 503 | We need to be careful when outputing nodes that use phandles since |
| 504 | they must come after the declaration of the phandles in the C file. |
| 505 | Otherwise we get a compiler error since the phandle struct is not yet |
| 506 | declared. |
| 507 | |
| 508 | This function adds to each node a list of phandle nodes that the node |
| 509 | depends on. This allows us to output things in the right order. |
| 510 | """ |
| 511 | for node in self._valid_nodes: |
| 512 | node.phandles = set() |
| 513 | for pname, prop in node.props.items(): |
| 514 | if pname in PROP_IGNORE_LIST or pname[0] == '#': |
| 515 | continue |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 516 | info = self.get_phandle_argc(prop, node.name) |
| 517 | if info: |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 518 | # Process the list as pairs of (phandle, id) |
Simon Glass | 634eba4 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 519 | pos = 0 |
| 520 | for args in info.args: |
| 521 | phandle_cell = prop.value[pos] |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 522 | phandle = fdt_util.fdt32_to_cpu(phandle_cell) |
| 523 | target_node = self._fdt.phandle_to_node[phandle] |
| 524 | node.phandles.add(target_node) |
Simon Glass | 634eba4 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 525 | pos += 1 + args |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 526 | |
| 527 | |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 528 | def generate_structs(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 529 | """Generate struct defintions for the platform data |
| 530 | |
| 531 | This writes out the body of a header file consisting of structure |
| 532 | definitions for node in self._valid_nodes. See the documentation in |
Heinrich Schuchardt | 2799a69 | 2020-02-25 21:35:39 +0100 | [diff] [blame] | 533 | doc/driver-model/of-plat.rst for more information. |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 534 | """ |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 535 | structs = self._struct_data |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 536 | self.out('#include <stdbool.h>\n') |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 537 | self.out('#include <linux/libfdt.h>\n') |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 538 | |
| 539 | # Output the struct definition |
| 540 | for name in sorted(structs): |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 541 | self.out('struct %s%s {\n' % (STRUCT_PREFIX, name)) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 542 | for pname in sorted(structs[name]): |
| 543 | prop = structs[name][pname] |
Simon Glass | 8fed2eb | 2017-08-29 14:15:55 -0600 | [diff] [blame] | 544 | info = self.get_phandle_argc(prop, structs[name]) |
| 545 | if info: |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 546 | # For phandles, include a reference to the target |
Simon Glass | 0d15463 | 2017-08-29 14:15:56 -0600 | [diff] [blame] | 547 | struct_name = 'struct phandle_%d_arg' % info.max_args |
| 548 | self.out('\t%s%s[%d]' % (tab_to(2, struct_name), |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 549 | conv_name_to_c(prop.name), |
Simon Glass | 634eba4 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 550 | len(info.args))) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 551 | else: |
| 552 | ptype = TYPE_NAMES[prop.type] |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 553 | self.out('\t%s%s' % (tab_to(2, ptype), |
| 554 | conv_name_to_c(prop.name))) |
| 555 | if isinstance(prop.value, list): |
| 556 | self.out('[%d]' % len(prop.value)) |
| 557 | self.out(';\n') |
| 558 | self.out('};\n') |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 559 | |
Simon Glass | abf0c80 | 2020-12-23 08:11:20 -0700 | [diff] [blame] | 560 | def _output_list(self, node, prop): |
| 561 | """Output the C code for a devicetree property that holds a list |
| 562 | |
| 563 | Args: |
| 564 | node (fdt.Node): Node to output |
| 565 | prop (fdt.Prop): Prop to output |
| 566 | """ |
| 567 | self.buf('{') |
| 568 | vals = [] |
| 569 | # For phandles, output a reference to the platform data |
| 570 | # of the target node. |
| 571 | info = self.get_phandle_argc(prop, node.name) |
| 572 | if info: |
| 573 | # Process the list as pairs of (phandle, id) |
| 574 | pos = 0 |
| 575 | for args in info.args: |
| 576 | phandle_cell = prop.value[pos] |
| 577 | phandle = fdt_util.fdt32_to_cpu(phandle_cell) |
| 578 | target_node = self._fdt.phandle_to_node[phandle] |
| 579 | arg_values = [] |
| 580 | for i in range(args): |
| 581 | arg_values.append( |
| 582 | str(fdt_util.fdt32_to_cpu(prop.value[pos + 1 + i]))) |
| 583 | pos += 1 + args |
| 584 | vals.append('\t{%d, {%s}}' % (target_node.idx, |
| 585 | ', '.join(arg_values))) |
| 586 | for val in vals: |
| 587 | self.buf('\n\t\t%s,' % val) |
| 588 | else: |
| 589 | for val in prop.value: |
| 590 | vals.append(get_value(prop.type, val)) |
| 591 | |
| 592 | # Put 8 values per line to avoid very long lines. |
| 593 | for i in range(0, len(vals), 8): |
| 594 | if i: |
| 595 | self.buf(',\n\t\t') |
| 596 | self.buf(', '.join(vals[i:i + 8])) |
| 597 | self.buf('}') |
| 598 | |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame^] | 599 | def _declare_device(self, node): |
Simon Glass | 221ddc1 | 2020-12-23 08:11:21 -0700 | [diff] [blame] | 600 | """Add a device declaration to the output |
| 601 | |
Simon Glass | 20e442a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 602 | This declares a U_BOOT_DRVINFO() for the device being processed |
Simon Glass | 221ddc1 | 2020-12-23 08:11:21 -0700 | [diff] [blame] | 603 | |
| 604 | Args: |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame^] | 605 | node: Node to process |
Simon Glass | 221ddc1 | 2020-12-23 08:11:21 -0700 | [diff] [blame] | 606 | """ |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame^] | 607 | self.buf('U_BOOT_DRVINFO(%s) = {\n' % node.var_name) |
| 608 | self.buf('\t.name\t\t= "%s",\n' % node.struct_name) |
| 609 | self.buf('\t.plat\t= &%s%s,\n' % (VAL_PREFIX, node.var_name)) |
| 610 | self.buf('\t.plat_size\t= sizeof(%s%s),\n' % |
| 611 | (VAL_PREFIX, node.var_name)) |
Simon Glass | 221ddc1 | 2020-12-23 08:11:21 -0700 | [diff] [blame] | 612 | idx = -1 |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame^] | 613 | if node.parent and node.parent in self._valid_nodes: |
| 614 | idx = node.parent.idx |
Simon Glass | 221ddc1 | 2020-12-23 08:11:21 -0700 | [diff] [blame] | 615 | self.buf('\t.parent_idx\t= %d,\n' % idx) |
| 616 | self.buf('};\n') |
| 617 | self.buf('\n') |
| 618 | |
Simon Glass | 161dac1 | 2020-12-23 08:11:22 -0700 | [diff] [blame] | 619 | def _output_prop(self, node, prop): |
| 620 | """Output a line containing the value of a struct member |
| 621 | |
| 622 | Args: |
| 623 | node (Node): Node being output |
| 624 | prop (Prop): Prop object to output |
| 625 | """ |
| 626 | if prop.name in PROP_IGNORE_LIST or prop.name[0] == '#': |
| 627 | return |
| 628 | member_name = conv_name_to_c(prop.name) |
| 629 | self.buf('\t%s= ' % tab_to(3, '.' + member_name)) |
| 630 | |
| 631 | # Special handling for lists |
| 632 | if isinstance(prop.value, list): |
| 633 | self._output_list(node, prop) |
| 634 | else: |
| 635 | self.buf(get_value(prop.type, prop.value)) |
| 636 | self.buf(',\n') |
| 637 | |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame^] | 638 | def _output_values(self, node): |
Simon Glass | 161dac1 | 2020-12-23 08:11:22 -0700 | [diff] [blame] | 639 | """Output the definition of a device's struct values |
| 640 | |
| 641 | Args: |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame^] | 642 | node (Node): Node to output |
Simon Glass | 161dac1 | 2020-12-23 08:11:22 -0700 | [diff] [blame] | 643 | """ |
| 644 | self.buf('static struct %s%s %s%s = {\n' % |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame^] | 645 | (STRUCT_PREFIX, node.struct_name, VAL_PREFIX, node.var_name)) |
Simon Glass | 161dac1 | 2020-12-23 08:11:22 -0700 | [diff] [blame] | 646 | for pname in sorted(node.props): |
| 647 | self._output_prop(node, node.props[pname]) |
| 648 | self.buf('};\n') |
| 649 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 650 | def output_node(self, node): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 651 | """Output the C code for a node |
| 652 | |
| 653 | Args: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 654 | node (fdt.Node): node to output |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 655 | """ |
Simon Glass | 1b27273 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 656 | self.buf('/* Node %s index %d */\n' % (node.path, node.idx)) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 657 | |
Simon Glass | e525fea | 2021-02-03 06:00:59 -0700 | [diff] [blame^] | 658 | self._output_values(node) |
| 659 | self._declare_device(node) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 660 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 661 | self.out(''.join(self.get_buf())) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 662 | |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 663 | def generate_plat(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 664 | """Generate device defintions for the platform data |
| 665 | |
| 666 | This writes out C platform data initialisation data and |
Simon Glass | 20e442a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 667 | U_BOOT_DRVINFO() declarations for each valid node. Where a node has |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 668 | multiple compatible strings, a #define is used to make them equivalent. |
| 669 | |
Heinrich Schuchardt | 2799a69 | 2020-02-25 21:35:39 +0100 | [diff] [blame] | 670 | See the documentation in doc/driver-model/of-plat.rst for more |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 671 | information. |
| 672 | """ |
Simon Glass | 20e442a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 673 | self.out('/* Allow use of U_BOOT_DRVINFO() in this file */\n') |
Simon Glass | f31fa99 | 2020-12-28 20:35:01 -0700 | [diff] [blame] | 674 | self.out('#define DT_PLAT_C\n') |
Simon Glass | cb43ac1 | 2020-10-03 11:31:41 -0600 | [diff] [blame] | 675 | self.out('\n') |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 676 | self.out('#include <common.h>\n') |
| 677 | self.out('#include <dm.h>\n') |
| 678 | self.out('#include <dt-structs.h>\n') |
| 679 | self.out('\n') |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 680 | |
Simon Glass | 9eca08d | 2020-12-28 20:35:04 -0700 | [diff] [blame] | 681 | for node in self._valid_nodes: |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 682 | self.output_node(node) |
Simon Glass | fa0ea5b | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 683 | |
Walter Lozano | 51f1263 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 684 | self.out(''.join(self.get_buf())) |
Simon Glass | fa0ea5b | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 685 | |
Simon Glass | 192c111 | 2020-12-28 20:34:50 -0700 | [diff] [blame] | 686 | |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 687 | # Types of output file we understand |
| 688 | # key: Command used to generate this file |
| 689 | # value: OutputFile for this command |
| 690 | OUTPUT_FILES = { |
Simon Glass | d1055d6 | 2020-12-28 20:35:00 -0700 | [diff] [blame] | 691 | 'struct': |
| 692 | OutputFile(Ftype.HEADER, 'dt-structs-gen.h', |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 693 | DtbPlatdata.generate_structs, |
Simon Glass | d1055d6 | 2020-12-28 20:35:00 -0700 | [diff] [blame] | 694 | 'Defines the structs used to hold devicetree data'), |
| 695 | 'platdata': |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 696 | OutputFile(Ftype.SOURCE, 'dt-plat.c', DtbPlatdata.generate_plat, |
Simon Glass | d1055d6 | 2020-12-28 20:35:00 -0700 | [diff] [blame] | 697 | 'Declares the U_BOOT_DRIVER() records and platform data'), |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | |
Simon Glass | 192c111 | 2020-12-28 20:34:50 -0700 | [diff] [blame] | 701 | def run_steps(args, dtb_file, include_disabled, output, output_dirs, |
Simon Glass | a32eb7d | 2021-02-03 06:00:51 -0700 | [diff] [blame] | 702 | warning_disabled=False, drivers_additional=None, basedir=None, |
| 703 | scan=None): |
Simon Glass | fa0ea5b | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 704 | """Run all the steps of the dtoc tool |
| 705 | |
| 706 | Args: |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 707 | args (list): List of non-option arguments provided to the problem |
| 708 | dtb_file (str): Filename of dtb file to process |
| 709 | include_disabled (bool): True to include disabled nodes |
Simon Glass | f62cea0 | 2020-12-28 20:34:48 -0700 | [diff] [blame] | 710 | output (str): Name of output file (None for stdout) |
Simon Glass | 192c111 | 2020-12-28 20:34:50 -0700 | [diff] [blame] | 711 | output_dirs (tuple of str): |
| 712 | Directory to put C output files |
| 713 | Directory to put H output files |
Simon Glass | 78128d5 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 714 | warning_disabled (bool): True to avoid showing warnings about missing |
| 715 | drivers |
Simon Glass | ccc3da7 | 2020-12-23 08:11:19 -0700 | [diff] [blame] | 716 | drivers_additional (list): List of additional drivers to use during |
Simon Glass | 78128d5 | 2020-12-03 16:55:16 -0700 | [diff] [blame] | 717 | scanning |
Simon Glass | 1e0f3f4 | 2020-12-28 20:35:03 -0700 | [diff] [blame] | 718 | basedir (str): Base directory of U-Boot source code. Defaults to the |
| 719 | grandparent of this file's directory |
Simon Glass | a32eb7d | 2021-02-03 06:00:51 -0700 | [diff] [blame] | 720 | scan (src_src.Scanner): Scanner from a previous run. This can help speed |
| 721 | up tests. Use None for normal operation |
| 722 | |
Simon Glass | 9b33038 | 2020-11-08 20:36:21 -0700 | [diff] [blame] | 723 | Raises: |
| 724 | ValueError: if args has no command, or an unknown command |
Simon Glass | fa0ea5b | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 725 | """ |
| 726 | if not args: |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 727 | raise ValueError('Please specify a command: struct, platdata, all') |
| 728 | if output and output_dirs and any(output_dirs): |
| 729 | raise ValueError('Must specify either output or output_dirs, not both') |
Simon Glass | fa0ea5b | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 730 | |
Simon Glass | a32eb7d | 2021-02-03 06:00:51 -0700 | [diff] [blame] | 731 | if not scan: |
| 732 | scan = src_scan.Scanner(basedir, warning_disabled, drivers_additional) |
| 733 | scan.scan_drivers() |
Simon Glass | a542a70 | 2020-12-28 20:35:06 -0700 | [diff] [blame] | 734 | plat = DtbPlatdata(scan, dtb_file, include_disabled) |
Simon Glass | fa0ea5b | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 735 | plat.scan_dtb() |
| 736 | plat.scan_tree() |
Simon Glass | 51d5d05 | 2021-02-03 06:00:58 -0700 | [diff] [blame] | 737 | plat.prepare_nodes() |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 738 | plat.scan_reg_sizes() |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 739 | plat.setup_output_dirs(output_dirs) |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 740 | plat.scan_structs() |
Simon Glass | fa0ea5b | 2017-06-18 22:09:03 -0600 | [diff] [blame] | 741 | plat.scan_phandles() |
| 742 | |
Simon Glass | 10cbd3b | 2020-12-28 20:34:52 -0700 | [diff] [blame] | 743 | cmds = args[0].split(',') |
| 744 | if 'all' in cmds: |
| 745 | cmds = sorted(OUTPUT_FILES.keys()) |
| 746 | for cmd in cmds: |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 747 | outfile = OUTPUT_FILES.get(cmd) |
| 748 | if not outfile: |
| 749 | raise ValueError("Unknown command '%s': (use: %s)" % |
Simon Glass | 10cbd3b | 2020-12-28 20:34:52 -0700 | [diff] [blame] | 750 | (cmd, ', '.join(sorted(OUTPUT_FILES.keys())))) |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 751 | plat.setup_output(outfile.ftype, |
| 752 | outfile.fname if output_dirs else output) |
Simon Glass | d1055d6 | 2020-12-28 20:35:00 -0700 | [diff] [blame] | 753 | plat.out_header(outfile) |
Simon Glass | a7d5f96 | 2020-12-28 20:35:02 -0700 | [diff] [blame] | 754 | outfile.method(plat) |
Simon Glass | be44f27 | 2020-12-28 20:34:51 -0700 | [diff] [blame] | 755 | plat.finish_output() |