Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (C) 2017 Google, Inc |
| 4 | # Written by Simon Glass <sjg@chromium.org> |
| 5 | # |
| 6 | # SPDX-License-Identifier: GPL-2.0+ |
| 7 | # |
| 8 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 9 | """Device tree to platform data class |
| 10 | |
| 11 | This supports converting device tree data to C structures definitions and |
| 12 | static data. |
| 13 | """ |
| 14 | |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 15 | import copy |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 16 | import sys |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 17 | |
| 18 | import fdt |
| 19 | import fdt_util |
| 20 | |
| 21 | # When we see these properties we ignore them - i.e. do not create a structure member |
| 22 | PROP_IGNORE_LIST = [ |
| 23 | '#address-cells', |
| 24 | '#gpio-cells', |
| 25 | '#size-cells', |
| 26 | 'compatible', |
| 27 | 'linux,phandle', |
| 28 | "status", |
| 29 | 'phandle', |
| 30 | 'u-boot,dm-pre-reloc', |
| 31 | 'u-boot,dm-tpl', |
| 32 | 'u-boot,dm-spl', |
| 33 | ] |
| 34 | |
| 35 | # C type declarations for the tyues we support |
| 36 | TYPE_NAMES = { |
| 37 | fdt.TYPE_INT: 'fdt32_t', |
| 38 | fdt.TYPE_BYTE: 'unsigned char', |
| 39 | fdt.TYPE_STRING: 'const char *', |
| 40 | fdt.TYPE_BOOL: 'bool', |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 41 | } |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 42 | |
| 43 | STRUCT_PREFIX = 'dtd_' |
| 44 | VAL_PREFIX = 'dtv_' |
| 45 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 46 | def conv_name_to_c(name): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 47 | """Convert a device-tree name to a C identifier |
| 48 | |
| 49 | Args: |
| 50 | name: Name to convert |
| 51 | Return: |
| 52 | String containing the C version of this name |
| 53 | """ |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 54 | new = name.replace('@', '_at_') |
| 55 | new = new.replace('-', '_') |
| 56 | new = new.replace(',', '_') |
| 57 | new = new.replace('.', '_') |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 58 | return new |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 59 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 60 | def tab_to(num_tabs, line): |
| 61 | """Append tabs to a line of text to reach a tab stop. |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 62 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 63 | Args: |
| 64 | num_tabs: Tab stop to obtain (0 = column 0, 1 = column 8, etc.) |
| 65 | line: Line of text to append to |
| 66 | |
| 67 | Returns: |
| 68 | line with the correct number of tabs appeneded. If the line already |
| 69 | extends past that tab stop then a single space is appended. |
| 70 | """ |
| 71 | if len(line) >= num_tabs * 8: |
| 72 | return line + ' ' |
| 73 | return line + '\t' * (num_tabs - len(line) // 8) |
| 74 | |
| 75 | class DtbPlatdata(object): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 76 | """Provide a means to convert device tree binary data to platform data |
| 77 | |
| 78 | The output of this process is C structures which can be used in space- |
| 79 | constrained encvironments where the ~3KB code overhead of device tree |
| 80 | code is not affordable. |
| 81 | |
| 82 | Properties: |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 83 | _fdt: Fdt object, referencing the device tree |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 84 | _dtb_fname: Filename of the input device tree binary file |
| 85 | _valid_nodes: A list of Node object with compatible strings |
Simon Glass | e36024b | 2017-06-18 22:09:01 -0600 | [diff] [blame^] | 86 | _include_disabled: true to include nodes marked status = "disabled" |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 87 | _phandle_nodes: A dict of nodes indexed by phandle number (1, 2...) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 88 | _outfile: The current output file (sys.stdout or a real file) |
| 89 | _lines: Stashed list of output lines for outputting in the future |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 90 | _phandle_nodes: A dict of Nodes indexed by phandle (an integer) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 91 | """ |
Simon Glass | e36024b | 2017-06-18 22:09:01 -0600 | [diff] [blame^] | 92 | def __init__(self, dtb_fname, include_disabled): |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 93 | self._fdt = None |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 94 | self._dtb_fname = dtb_fname |
| 95 | self._valid_nodes = None |
Simon Glass | e36024b | 2017-06-18 22:09:01 -0600 | [diff] [blame^] | 96 | self._include_disabled = include_disabled |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 97 | self._phandle_nodes = {} |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 98 | self._outfile = None |
| 99 | self._lines = [] |
| 100 | self._aliases = {} |
| 101 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 102 | def setup_output(self, fname): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 103 | """Set up the output destination |
| 104 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 105 | Once this is done, future calls to self.out() will output to this |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 106 | file. |
| 107 | |
| 108 | Args: |
| 109 | fname: Filename to send output to, or '-' for stdout |
| 110 | """ |
| 111 | if fname == '-': |
| 112 | self._outfile = sys.stdout |
| 113 | else: |
| 114 | self._outfile = open(fname, 'w') |
| 115 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 116 | def out(self, line): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 117 | """Output a string to the output file |
| 118 | |
| 119 | Args: |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 120 | line: String to output |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 121 | """ |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 122 | self._outfile.write(line) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 123 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 124 | def buf(self, line): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 125 | """Buffer up a string to send later |
| 126 | |
| 127 | Args: |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 128 | line: String to add to our 'buffer' list |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 129 | """ |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 130 | self._lines.append(line) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 131 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 132 | def get_buf(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 133 | """Get the contents of the output buffer, and clear it |
| 134 | |
| 135 | Returns: |
| 136 | The output buffer, which is then cleared for future use |
| 137 | """ |
| 138 | lines = self._lines |
| 139 | self._lines = [] |
| 140 | return lines |
| 141 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 142 | @staticmethod |
| 143 | def get_value(ftype, value): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 144 | """Get a value as a C expression |
| 145 | |
| 146 | For integers this returns a byte-swapped (little-endian) hex string |
| 147 | For bytes this returns a hex string, e.g. 0x12 |
| 148 | For strings this returns a literal string enclosed in quotes |
| 149 | For booleans this return 'true' |
| 150 | |
| 151 | Args: |
| 152 | type: Data type (fdt_util) |
| 153 | value: Data value, as a string of bytes |
| 154 | """ |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 155 | if ftype == fdt.TYPE_INT: |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 156 | return '%#x' % fdt_util.fdt32_to_cpu(value) |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 157 | elif ftype == fdt.TYPE_BYTE: |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 158 | return '%#x' % ord(value[0]) |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 159 | elif ftype == fdt.TYPE_STRING: |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 160 | return '"%s"' % value |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 161 | elif ftype == fdt.TYPE_BOOL: |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 162 | return 'true' |
| 163 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 164 | @staticmethod |
| 165 | def get_compat_name(node): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 166 | """Get a node's first compatible string as a C identifier |
| 167 | |
| 168 | Args: |
| 169 | node: Node object to check |
| 170 | Return: |
| 171 | C identifier for the first compatible string |
| 172 | """ |
| 173 | compat = node.props['compatible'].value |
| 174 | aliases = [] |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 175 | if isinstance(compat, list): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 176 | compat, aliases = compat[0], compat[1:] |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 177 | return conv_name_to_c(compat), [conv_name_to_c(a) for a in aliases] |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 178 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 179 | def scan_dtb(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 180 | """Scan the device tree to obtain a tree of notes and properties |
| 181 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 182 | 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] | 183 | device tree root node, and progress from there. |
| 184 | """ |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 185 | self._fdt = fdt.FdtScan(self._dtb_fname) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 186 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 187 | def scan_node(self, root): |
| 188 | """Scan a node and subnodes to build a tree of node and phandle info |
| 189 | |
| 190 | This adds each node to self._valid_nodes and each phandle to |
| 191 | self._phandle_nodes. |
| 192 | |
| 193 | Args: |
| 194 | root: Root node for scan |
| 195 | """ |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 196 | for node in root.subnodes: |
| 197 | if 'compatible' in node.props: |
| 198 | status = node.props.get('status') |
Simon Glass | e36024b | 2017-06-18 22:09:01 -0600 | [diff] [blame^] | 199 | if (not self._include_disabled and not status or |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 200 | status.value != 'disabled'): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 201 | self._valid_nodes.append(node) |
| 202 | phandle_prop = node.props.get('phandle') |
| 203 | if phandle_prop: |
| 204 | phandle = phandle_prop.GetPhandle() |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 205 | self._phandle_nodes[phandle] = node |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 206 | |
| 207 | # recurse to handle any subnodes |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 208 | self.scan_node(node) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 209 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 210 | def scan_tree(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 211 | """Scan the device tree for useful information |
| 212 | |
| 213 | This fills in the following properties: |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 214 | _phandle_nodes: A dict of Nodes indexed by phandle (an integer) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 215 | _valid_nodes: A list of nodes we wish to consider include in the |
| 216 | platform data |
| 217 | """ |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 218 | self._phandle_nodes = {} |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 219 | self._valid_nodes = [] |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 220 | return self.scan_node(self._fdt.GetRoot()) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 221 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 222 | @staticmethod |
| 223 | def is_phandle(prop): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 224 | """Check if a node contains phandles |
| 225 | |
| 226 | We have no reliable way of detecting whether a node uses a phandle |
| 227 | or not. As an interim measure, use a list of known property names. |
| 228 | |
| 229 | Args: |
| 230 | prop: Prop object to check |
| 231 | Return: |
| 232 | True if the object value contains phandles, else False |
| 233 | """ |
| 234 | if prop.name in ['clocks']: |
| 235 | return True |
| 236 | return False |
| 237 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 238 | def scan_structs(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 239 | """Scan the device tree building up the C structures we will use. |
| 240 | |
| 241 | Build a dict keyed by C struct name containing a dict of Prop |
| 242 | object for each struct field (keyed by property name). Where the |
| 243 | same struct appears multiple times, try to use the 'widest' |
| 244 | property, i.e. the one with a type which can express all others. |
| 245 | |
| 246 | Once the widest property is determined, all other properties are |
| 247 | updated to match that width. |
| 248 | """ |
| 249 | structs = {} |
| 250 | for node in self._valid_nodes: |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 251 | node_name, _ = self.get_compat_name(node) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 252 | fields = {} |
| 253 | |
| 254 | # Get a list of all the valid properties in this node. |
| 255 | for name, prop in node.props.items(): |
| 256 | if name not in PROP_IGNORE_LIST and name[0] != '#': |
| 257 | fields[name] = copy.deepcopy(prop) |
| 258 | |
| 259 | # If we've seen this node_name before, update the existing struct. |
| 260 | if node_name in structs: |
| 261 | struct = structs[node_name] |
| 262 | for name, prop in fields.items(): |
| 263 | oldprop = struct.get(name) |
| 264 | if oldprop: |
| 265 | oldprop.Widen(prop) |
| 266 | else: |
| 267 | struct[name] = prop |
| 268 | |
| 269 | # Otherwise store this as a new struct. |
| 270 | else: |
| 271 | structs[node_name] = fields |
| 272 | |
| 273 | upto = 0 |
| 274 | for node in self._valid_nodes: |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 275 | node_name, _ = self.get_compat_name(node) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 276 | struct = structs[node_name] |
| 277 | for name, prop in node.props.items(): |
| 278 | if name not in PROP_IGNORE_LIST and name[0] != '#': |
| 279 | prop.Widen(struct[name]) |
| 280 | upto += 1 |
| 281 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 282 | struct_name, aliases = self.get_compat_name(node) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 283 | for alias in aliases: |
| 284 | self._aliases[alias] = struct_name |
| 285 | |
| 286 | return structs |
| 287 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 288 | def scan_phandles(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 289 | """Figure out what phandles each node uses |
| 290 | |
| 291 | We need to be careful when outputing nodes that use phandles since |
| 292 | they must come after the declaration of the phandles in the C file. |
| 293 | Otherwise we get a compiler error since the phandle struct is not yet |
| 294 | declared. |
| 295 | |
| 296 | This function adds to each node a list of phandle nodes that the node |
| 297 | depends on. This allows us to output things in the right order. |
| 298 | """ |
| 299 | for node in self._valid_nodes: |
| 300 | node.phandles = set() |
| 301 | for pname, prop in node.props.items(): |
| 302 | if pname in PROP_IGNORE_LIST or pname[0] == '#': |
| 303 | continue |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 304 | if isinstance(prop.value, list): |
| 305 | if self.is_phandle(prop): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 306 | # Process the list as pairs of (phandle, id) |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 307 | value_it = iter(prop.value) |
| 308 | for phandle_cell, _ in zip(value_it, value_it): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 309 | phandle = fdt_util.fdt32_to_cpu(phandle_cell) |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 310 | target_node = self._phandle_nodes[phandle] |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 311 | node.phandles.add(target_node) |
| 312 | |
| 313 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 314 | def generate_structs(self, structs): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 315 | """Generate struct defintions for the platform data |
| 316 | |
| 317 | This writes out the body of a header file consisting of structure |
| 318 | definitions for node in self._valid_nodes. See the documentation in |
| 319 | README.of-plat for more information. |
| 320 | """ |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 321 | self.out('#include <stdbool.h>\n') |
| 322 | self.out('#include <libfdt.h>\n') |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 323 | |
| 324 | # Output the struct definition |
| 325 | for name in sorted(structs): |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 326 | self.out('struct %s%s {\n' % (STRUCT_PREFIX, name)) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 327 | for pname in sorted(structs[name]): |
| 328 | prop = structs[name][pname] |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 329 | if self.is_phandle(prop): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 330 | # For phandles, include a reference to the target |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 331 | self.out('\t%s%s[%d]' % (tab_to(2, 'struct phandle_2_cell'), |
| 332 | conv_name_to_c(prop.name), |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 333 | len(prop.value) / 2)) |
| 334 | else: |
| 335 | ptype = TYPE_NAMES[prop.type] |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 336 | self.out('\t%s%s' % (tab_to(2, ptype), |
| 337 | conv_name_to_c(prop.name))) |
| 338 | if isinstance(prop.value, list): |
| 339 | self.out('[%d]' % len(prop.value)) |
| 340 | self.out(';\n') |
| 341 | self.out('};\n') |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 342 | |
| 343 | for alias, struct_name in self._aliases.iteritems(): |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 344 | self.out('#define %s%s %s%s\n'% (STRUCT_PREFIX, alias, |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 345 | STRUCT_PREFIX, struct_name)) |
| 346 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 347 | def output_node(self, node): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 348 | """Output the C code for a node |
| 349 | |
| 350 | Args: |
| 351 | node: node to output |
| 352 | """ |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 353 | struct_name, _ = self.get_compat_name(node) |
| 354 | var_name = conv_name_to_c(node.name) |
| 355 | self.buf('static struct %s%s %s%s = {\n' % |
| 356 | (STRUCT_PREFIX, struct_name, VAL_PREFIX, var_name)) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 357 | for pname, prop in node.props.items(): |
| 358 | if pname in PROP_IGNORE_LIST or pname[0] == '#': |
| 359 | continue |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 360 | member_name = conv_name_to_c(prop.name) |
| 361 | self.buf('\t%s= ' % tab_to(3, '.' + member_name)) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 362 | |
| 363 | # Special handling for lists |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 364 | if isinstance(prop.value, list): |
| 365 | self.buf('{') |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 366 | vals = [] |
| 367 | # For phandles, output a reference to the platform data |
| 368 | # of the target node. |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 369 | if self.is_phandle(prop): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 370 | # Process the list as pairs of (phandle, id) |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 371 | value_it = iter(prop.value) |
| 372 | for phandle_cell, id_cell in zip(value_it, value_it): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 373 | phandle = fdt_util.fdt32_to_cpu(phandle_cell) |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 374 | id_num = fdt_util.fdt32_to_cpu(id_cell) |
| 375 | target_node = self._phandle_nodes[phandle] |
| 376 | name = conv_name_to_c(target_node.name) |
| 377 | vals.append('{&%s%s, %d}' % (VAL_PREFIX, name, id_num)) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 378 | else: |
| 379 | for val in prop.value: |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 380 | vals.append(self.get_value(prop.type, val)) |
| 381 | self.buf(', '.join(vals)) |
| 382 | self.buf('}') |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 383 | else: |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 384 | self.buf(self.get_value(prop.type, prop.value)) |
| 385 | self.buf(',\n') |
| 386 | self.buf('};\n') |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 387 | |
| 388 | # Add a device declaration |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 389 | self.buf('U_BOOT_DEVICE(%s) = {\n' % var_name) |
| 390 | self.buf('\t.name\t\t= "%s",\n' % struct_name) |
| 391 | self.buf('\t.platdata\t= &%s%s,\n' % (VAL_PREFIX, var_name)) |
| 392 | self.buf('\t.platdata_size\t= sizeof(%s%s),\n' % (VAL_PREFIX, var_name)) |
| 393 | self.buf('};\n') |
| 394 | self.buf('\n') |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 395 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 396 | self.out(''.join(self.get_buf())) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 397 | |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 398 | def generate_tables(self): |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 399 | """Generate device defintions for the platform data |
| 400 | |
| 401 | This writes out C platform data initialisation data and |
| 402 | U_BOOT_DEVICE() declarations for each valid node. Where a node has |
| 403 | multiple compatible strings, a #define is used to make them equivalent. |
| 404 | |
| 405 | See the documentation in doc/driver-model/of-plat.txt for more |
| 406 | information. |
| 407 | """ |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 408 | self.out('#include <common.h>\n') |
| 409 | self.out('#include <dm.h>\n') |
| 410 | self.out('#include <dt-structs.h>\n') |
| 411 | self.out('\n') |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 412 | nodes_to_output = list(self._valid_nodes) |
| 413 | |
| 414 | # Keep outputing nodes until there is none left |
| 415 | while nodes_to_output: |
| 416 | node = nodes_to_output[0] |
| 417 | # Output all the node's dependencies first |
| 418 | for req_node in node.phandles: |
| 419 | if req_node in nodes_to_output: |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 420 | self.output_node(req_node) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 421 | nodes_to_output.remove(req_node) |
Simon Glass | 2be282c | 2017-06-18 22:08:59 -0600 | [diff] [blame] | 422 | self.output_node(node) |
Simon Glass | 7581c01 | 2017-06-18 22:08:58 -0600 | [diff] [blame] | 423 | nodes_to_output.remove(node) |