def ScanPhandles(self): """Figure out what phandles each node uses We need to be careful when outputing nodes that use phandles since they must come after the declaration of the phandles in the C file. Otherwise we get a compiler error since the phandle struct is not yet declared. This function adds to each node a list of phandle nodes that the node depends on. This allows us to output things in the right order. """ for node in self._valid_nodes: node.phandles = set() for pname, prop in node.props.items(): if pname in PROP_IGNORE_LIST or pname[0] == '#': continue if type(prop.value) == list: if self.IsPhandle(prop): # Process the list as pairs of (phandle, id) it = iter(prop.value) for phandle_cell, id_cell in zip(it, it): phandle = fdt_util.fdt32_to_cpu(phandle_cell) id = fdt_util.fdt32_to_cpu(id_cell) target_node = self._phandle_node[phandle] node.phandles.add(target_node)
def OutputNode(self, node): """Output the C code for a node Args: node: node to output """ struct_name = self.GetCompatName(node) var_name = Conv_name_to_c(node.name) self.Buf('static struct %s%s %s%s = {\n' % (STRUCT_PREFIX, struct_name, VAL_PREFIX, var_name)) for pname, prop in node.props.items(): if pname in PROP_IGNORE_LIST or pname[0] == '#': continue ptype = TYPE_NAMES[prop.type] member_name = Conv_name_to_c(prop.name) self.Buf('\t%s= ' % TabTo(3, '.' + member_name)) # Special handling for lists if type(prop.value) == list: self.Buf('{') vals = [] # For phandles, output a reference to the platform data # of the target node. if self.IsPhandle(prop): # Process the list as pairs of (phandle, id) it = iter(prop.value) for phandle_cell, id_cell in zip(it, it): phandle = fdt_util.fdt32_to_cpu(phandle_cell) id = fdt_util.fdt32_to_cpu(id_cell) target_node = self._phandle_node[phandle] name = Conv_name_to_c(target_node.name) vals.append('{&%s%s, %d}' % (VAL_PREFIX, name, id)) else: for val in prop.value: vals.append(self.GetValue(prop.type, val)) self.Buf(', '.join(vals)) self.Buf('}') else: self.Buf(self.GetValue(prop.type, prop.value)) self.Buf(',\n') self.Buf('};\n') # Add a device declaration self.Buf('U_BOOT_DEVICE(%s) = {\n' % var_name) self.Buf('\t.name\t\t= "%s",\n' % struct_name) self.Buf('\t.platdata\t= &%s%s,\n' % (VAL_PREFIX, var_name)) self.Buf('\t.platdata_size\t= sizeof(%s%s),\n' % (VAL_PREFIX, var_name)) self.Buf('};\n') self.Buf('\n') self.Out(''.join(self.GetBuf()))
def output_node(self, node): """Output the C code for a node Args: node: node to output """ struct_name, _ = get_compat_name(node) var_name = conv_name_to_c(node.name) self.buf('static struct %s%s %s%s = {\n' % (STRUCT_PREFIX, struct_name, VAL_PREFIX, var_name)) for pname, prop in node.props.items(): if pname in PROP_IGNORE_LIST or pname[0] == '#': continue member_name = conv_name_to_c(prop.name) self.buf('\t%s= ' % tab_to(3, '.' + member_name)) # Special handling for lists if isinstance(prop.value, list): self.buf('{') vals = [] # For phandles, output a reference to the platform data # of the target node. if is_phandle(prop): # Process the list as pairs of (phandle, id) value_it = iter(prop.value) for phandle_cell, id_cell in zip(value_it, value_it): phandle = fdt_util.fdt32_to_cpu(phandle_cell) id_num = fdt_util.fdt32_to_cpu(id_cell) target_node = self._phandle_nodes[phandle] name = conv_name_to_c(target_node.name) vals.append('{&%s%s, %d}' % (VAL_PREFIX, name, id_num)) else: for val in prop.value: vals.append(get_value(prop.type, val)) self.buf(', '.join(vals)) self.buf('}') else: self.buf(get_value(prop.type, prop.value)) self.buf(',\n') self.buf('};\n') # Add a device declaration self.buf('U_BOOT_DEVICE(%s) = {\n' % var_name) self.buf('\t.name\t\t= "%s",\n' % struct_name) self.buf('\t.platdata\t= &%s%s,\n' % (VAL_PREFIX, var_name)) self.buf('\t.platdata_size\t= sizeof(%s%s),\n' % (VAL_PREFIX, var_name)) self.buf('};\n') self.buf('\n') self.out(''.join(self.get_buf()))
def testLookupPhandle(self): """Test looking up a single phandle""" dtb = fdt.FdtScan('tools/dtoc/dtoc_test_phandle.dts') node = dtb.GetNode('/phandle-source2') prop = node.props['clocks'] target = dtb.GetNode('/phandle-target') self.assertEqual(target, dtb.LookupPhandle(fdt32_to_cpu(prop.value)))
def scan_phandles(self): """Figure out what phandles each node uses We need to be careful when outputing nodes that use phandles since they must come after the declaration of the phandles in the C file. Otherwise we get a compiler error since the phandle struct is not yet declared. This function adds to each node a list of phandle nodes that the node depends on. This allows us to output things in the right order. """ for node in self._valid_nodes: node.phandles = set() for pname, prop in node.props.items(): if pname in PROP_IGNORE_LIST or pname[0] == '#': continue info = self.get_phandle_argc(prop, node.name) if info: if not isinstance(prop.value, list): prop.value = [prop.value] # Process the list as pairs of (phandle, id) pos = 0 for args in info.args: phandle_cell = prop.value[pos] phandle = fdt_util.fdt32_to_cpu(phandle_cell) target_node = self._fdt.phandle_to_node[phandle] node.phandles.add(target_node) pos += 1 + args
def scan_phandles(self): """Figure out what phandles each node uses We need to be careful when outputing nodes that use phandles since they must come after the declaration of the phandles in the C file. Otherwise we get a compiler error since the phandle struct is not yet declared. This function adds to each node a list of phandle nodes that the node depends on. This allows us to output things in the right order. """ for node in self._valid_nodes: node.phandles = set() for pname, prop in node.props.items(): if pname in PROP_IGNORE_LIST or pname[0] == '#': continue info = self.get_phandle_argc(prop, node.name) if info: # Process the list as pairs of (phandle, id) pos = 0 for args in info.args: phandle_cell = prop.value[pos] phandle = fdt_util.fdt32_to_cpu(phandle_cell) target_node = self._fdt.phandle_to_node[phandle] node.phandles.add(target_node) pos += 1 + args
def get_phandle_argc(self, prop, node_name): """Check if a node contains phandles We have no reliable way of detecting whether a node uses a phandle or not. As an interim measure, use a list of known property names. Args: prop: Prop object to check Return: Number of argument cells is this is a phandle, else None """ if prop.name in ['clocks']: if not isinstance(prop.value, list): prop.value = [prop.value] val = prop.value i = 0 max_args = 0 args = [] while i < len(val): phandle = fdt_util.fdt32_to_cpu(val[i]) # If we get to the end of the list, stop. This can happen # since some nodes have more phandles in the list than others, # but we allocate enough space for the largest list. So those # nodes with shorter lists end up with zeroes at the end. if not phandle: break target = self._fdt.phandle_to_node.get(phandle) if not target: raise ValueError("Cannot parse '%s' in node '%s'" % (prop.name, node_name)) prop_name = '#clock-cells' cells = target.props.get(prop_name) if not cells: raise ValueError("Node '%s' has no '%s' property" % (target.name, prop_name)) num_args = fdt_util.fdt32_to_cpu(cells.value) max_args = max(max_args, num_args) args.append(num_args) i += 1 + num_args return PhandleInfo(max_args, args) return None
def testAddMore(self): """Test various other methods for adding and setting properties""" self.node.AddZeroProp('one') self.dtb.Sync(auto_resize=True) data = self.fdt.getprop(self.node.Offset(), 'one') self.assertEqual(0, fdt32_to_cpu(data)) self.node.SetInt('one', 1) self.dtb.Sync(auto_resize=False) data = self.fdt.getprop(self.node.Offset(), 'one') self.assertEqual(1, fdt32_to_cpu(data)) val = '123' + chr(0) + '456' self.node.AddString('string', val) self.dtb.Sync(auto_resize=True) data = self.fdt.getprop(self.node.Offset(), 'string') self.assertEqual(tools.ToBytes(val) + b'\0', data) self.fdt.pack() self.node.SetString('string', val + 'x') with self.assertRaises(libfdt.FdtException) as e: self.dtb.Sync(auto_resize=False) self.assertIn('FDT_ERR_NOSPACE', str(e.exception)) self.node.SetString('string', val[:-1]) prop = self.node.props['string'] prop.SetData(tools.ToBytes(val)) self.dtb.Sync(auto_resize=False) data = self.fdt.getprop(self.node.Offset(), 'string') self.assertEqual(tools.ToBytes(val), data) self.node.AddEmptyProp('empty', 5) self.dtb.Sync(auto_resize=True) prop = self.node.props['empty'] prop.SetData(tools.ToBytes(val)) self.dtb.Sync(auto_resize=False) data = self.fdt.getprop(self.node.Offset(), 'empty') self.assertEqual(tools.ToBytes(val), data) self.node.SetData('empty', b'123') self.assertEqual(b'123', prop.bytes)
def testAddMore(self): """Test various other methods for adding and setting properties""" self.node.AddZeroProp('one') self.dtb.Sync(auto_resize=True) data = self.fdt.getprop(self.node.Offset(), 'one') self.assertEqual(0, fdt32_to_cpu(data)) self.node.SetInt('one', 1) self.dtb.Sync(auto_resize=False) data = self.fdt.getprop(self.node.Offset(), 'one') self.assertEqual(1, fdt32_to_cpu(data)) val = '123' + chr(0) + '456' self.node.AddString('string', val) self.dtb.Sync(auto_resize=True) data = self.fdt.getprop(self.node.Offset(), 'string') self.assertEqual(val + '\0', data) self.fdt.pack() self.node.SetString('string', val + 'x') with self.assertRaises(libfdt.FdtException) as e: self.dtb.Sync(auto_resize=False) self.assertIn('FDT_ERR_NOSPACE', str(e.exception)) self.node.SetString('string', val[:-1]) prop = self.node.props['string'] prop.SetData(val) self.dtb.Sync(auto_resize=False) data = self.fdt.getprop(self.node.Offset(), 'string') self.assertEqual(val, data) self.node.AddEmptyProp('empty', 5) self.dtb.Sync(auto_resize=True) prop = self.node.props['empty'] prop.SetData(val) self.dtb.Sync(auto_resize=False) data = self.fdt.getprop(self.node.Offset(), 'empty') self.assertEqual(val, data) self.node.SetData('empty', '123') self.assertEqual('123', prop.bytes)
def get_num_cells(node): """Get the number of cells in addresses and sizes for this node Args: node: Node to check Returns: Tuple: Number of address cells for this node Number of size cells for this node """ parent = node.parent na, ns = 2, 2 if parent: na_prop = parent.props.get('#address-cells') ns_prop = parent.props.get('#size-cells') if na_prop: na = fdt_util.fdt32_to_cpu(na_prop.value) if ns_prop: ns = fdt_util.fdt32_to_cpu(ns_prop.value) return na, ns
def AddNode(node, path): if node.name != '/': path += '/' + node.name #print 'path', path for subnode in node.subnodes: for prop in subnode.props.values(): if prop.name in node_names: prop_path = path + '/' + subnode.name + ':' + prop.name tree[prop_path[len('/binman/' ):]] = fdt_util.fdt32_to_cpu( prop.value) #print ' ', prop.name AddNode(subnode, path)
def testMakeProp(self): """Test we can convert all the the types that are supported""" prop = self._ConvertProp('boolval') self.assertEqual(fdt.TYPE_BOOL, prop.type) self.assertEqual(True, prop.value) prop = self._ConvertProp('intval') self.assertEqual(fdt.TYPE_INT, prop.type) self.assertEqual(1, fdt32_to_cpu(prop.value)) prop = self._ConvertProp('intarray') self.assertEqual(fdt.TYPE_INT, prop.type) val = [fdt32_to_cpu(val) for val in prop.value] self.assertEqual([2, 3, 4], val) prop = self._ConvertProp('byteval') self.assertEqual(fdt.TYPE_BYTE, prop.type) self.assertEqual(5, ord(prop.value)) prop = self._ConvertProp('longbytearray') self.assertEqual(fdt.TYPE_BYTE, prop.type) val = [ord(val) for val in prop.value] self.assertEqual([9, 10, 11, 12, 13, 14, 15, 16, 17], val) prop = self._ConvertProp('stringval') self.assertEqual(fdt.TYPE_STRING, prop.type) self.assertEqual('message', prop.value) prop = self._ConvertProp('stringarray') self.assertEqual(fdt.TYPE_STRING, prop.type) self.assertEqual(['multi-word', 'message'], prop.value) prop = self._ConvertProp('notstring') self.assertEqual(fdt.TYPE_BYTE, prop.type) val = [ord(val) for val in prop.value] self.assertEqual([0x20, 0x21, 0x22, 0x10, 0], val)
def get_phandle_argc(self, prop, node_name): """Check if a node contains phandles We have no reliable way of detecting whether a node uses a phandle or not. As an interim measure, use a list of known property names. Args: prop: Prop object to check Return: Number of argument cells is this is a phandle, else None """ if prop.name in ['clocks']: val = prop.value if not isinstance(val, list): val = [val] i = 0 max_args = 0 args = [] while i < len(val): phandle = fdt_util.fdt32_to_cpu(val[i]) target = self._fdt.phandle_to_node.get(phandle) if not target: raise ValueError("Cannot parse '%s' in node '%s'" % (prop.name, node_name)) prop_name = '#clock-cells' cells = target.props.get(prop_name) if not cells: raise ValueError("Node '%s' has no '%s' property" % (target.name, prop_name)) num_args = fdt_util.fdt32_to_cpu(cells.value) max_args = max(max_args, num_args) args.append(num_args) i += 1 + num_args return PhandleInfo(max_args, args) return None
def testWiden(self): """Test widening of values""" node2 = self.dtb.GetNode('/spl-test2') prop = self.node.props['intval'] # No action prop2 = node2.props['intval'] prop.Widen(prop2) self.assertEqual(fdt.TYPE_INT, prop.type) self.assertEqual(1, fdt32_to_cpu(prop.value)) # Convert singla value to array prop2 = self.node.props['intarray'] prop.Widen(prop2) self.assertEqual(fdt.TYPE_INT, prop.type) self.assertTrue(isinstance(prop.value, list)) # A 4-byte array looks like a single integer. When widened by a longer # byte array, it should turn into an array. prop = self.node.props['longbytearray'] prop2 = node2.props['longbytearray'] self.assertFalse(isinstance(prop2.value, list)) self.assertEqual(4, len(prop2.value)) prop2.Widen(prop) self.assertTrue(isinstance(prop2.value, list)) self.assertEqual(9, len(prop2.value)) # Similarly for a string array prop = self.node.props['stringval'] prop2 = node2.props['stringarray'] self.assertFalse(isinstance(prop.value, list)) self.assertEqual(7, len(prop.value)) prop.Widen(prop2) self.assertTrue(isinstance(prop.value, list)) self.assertEqual(3, len(prop.value)) # Enlarging an existing array prop = self.node.props['stringarray'] prop2 = node2.props['stringarray'] self.assertTrue(isinstance(prop.value, list)) self.assertEqual(2, len(prop.value)) prop.Widen(prop2) self.assertTrue(isinstance(prop.value, list)) self.assertEqual(3, len(prop.value))
def testFdtNormalProp(self): fname = self.GetCompiled('045_prop_test.dts') dt = FdtScan(fname) node = dt.GetNode('/binman/intel-me') self.assertEquals('intel-me', node.name) val = fdt_util.GetString(node, 'filename') self.assertEquals(str, type(val)) self.assertEquals('me.bin', val) prop = node.props['intval'] self.assertEquals(fdt.TYPE_INT, prop.type) self.assertEquals(3, fdt_util.GetInt(node, 'intval')) prop = node.props['intarray'] self.assertEquals(fdt.TYPE_INT, prop.type) self.assertEquals(list, type(prop.value)) self.assertEquals(2, len(prop.value)) self.assertEquals([5, 6], [fdt_util.fdt32_to_cpu(val) for val in prop.value]) prop = node.props['byteval'] self.assertEquals(fdt.TYPE_BYTE, prop.type) self.assertEquals(chr(8), prop.value) prop = node.props['bytearray'] self.assertEquals(fdt.TYPE_BYTE, prop.type) self.assertEquals(list, type(prop.value)) self.assertEquals(str, type(prop.value[0])) self.assertEquals(3, len(prop.value)) self.assertEquals([chr(1), '#', '4'], prop.value) prop = node.props['longbytearray'] self.assertEquals(fdt.TYPE_INT, prop.type) self.assertEquals(0x090a0b0c, fdt_util.GetInt(node, 'longbytearray')) prop = node.props['stringval'] self.assertEquals(fdt.TYPE_STRING, prop.type) self.assertEquals('message2', fdt_util.GetString(node, 'stringval')) prop = node.props['stringarray'] self.assertEquals(fdt.TYPE_STRING, prop.type) self.assertEquals(list, type(prop.value)) self.assertEquals(3, len(prop.value)) self.assertEquals(['another', 'multi-word', 'message'], prop.value)
def GetValue(self, type, value): """Get a value as a C expression For integers this returns a byte-swapped (little-endian) hex string For bytes this returns a hex string, e.g. 0x12 For strings this returns a literal string enclosed in quotes For booleans this return 'true' Args: type: Data type (fdt_util) value: Data value, as a string of bytes """ if type == fdt.TYPE_INT: return '%#x' % fdt_util.fdt32_to_cpu(value) elif type == fdt.TYPE_BYTE: return '%#x' % ord(value[0]) elif type == fdt.TYPE_STRING: return '"%s"' % value elif type == fdt.TYPE_BOOL: return 'true'
def GetValue(self, type, value): """Get a value as a C expression For integers this returns a byte-swapped (little-endian) hex string For bytes this returns a hex string, e.g. 0x12 For strings this returns a literal string enclosed in quotes For booleans this return 'true' Args: type: Data type (fdt_util) value: Data value, as a string of bytes """ if type == fdt_util.TYPE_INT: return '%#x' % fdt_util.fdt32_to_cpu(value) elif type == fdt_util.TYPE_BYTE: return '%#x' % ord(value[0]) elif type == fdt_util.TYPE_STRING: return '"%s"' % value elif type == fdt_util.TYPE_BOOL: return 'true'
def Scan(self): """Scan a node's properties and subnodes This fills in the props and subnodes properties, recursively searching into subnodes so that the entire tree is built. """ self.props = self._fdt.GetProps(self) phandle = self.props.get('phandle') if phandle: val = fdt_util.fdt32_to_cpu(phandle.value) self._fdt.phandle_to_node[val] = self offset = libfdt.fdt_first_subnode(self._fdt.GetFdt(), self.Offset()) while offset >= 0: sep = '' if self.path[-1] == '/' else '/' name = self._fdt._fdt_obj.get_name(offset) path = self.path + sep + name node = Node(self._fdt, self, offset, name, path) self.subnodes.append(node) node.Scan() offset = libfdt.fdt_next_subnode(self._fdt.GetFdt(), offset)
def scan_phandles(self): """Figure out what phandles each node uses We need to be careful when outputing nodes that use phandles since they must come after the declaration of the phandles in the C file. Otherwise we get a compiler error since the phandle struct is not yet declared. This function adds to each node a list of phandle nodes that the node depends on. This allows us to output things in the right order. """ for node in self._valid_nodes: node.phandles = set() for pname, prop in node.props.items(): if pname in PROP_IGNORE_LIST or pname[0] == '#': continue if isinstance(prop.value, list): if is_phandle(prop): # Process the list as pairs of (phandle, id) value_it = iter(prop.value) for phandle_cell, _ in zip(value_it, value_it): phandle = fdt_util.fdt32_to_cpu(phandle_cell) target_node = self._phandle_nodes[phandle] node.phandles.add(target_node)
def GetPhandle(self): """Get a (single) phandle value from a property Gets the phandle valuie from a property and returns it as an integer """ return fdt_util.fdt32_to_cpu(self.value[:4])
def output_node(self, node): """Output the C code for a node Args: node: node to output """ struct_name, _ = get_compat_name(node) var_name = conv_name_to_c(node.name) self.buf('static struct %s%s %s%s = {\n' % (STRUCT_PREFIX, struct_name, VAL_PREFIX, var_name)) for pname, prop in node.props.items(): if pname in PROP_IGNORE_LIST or pname[0] == '#': continue member_name = conv_name_to_c(prop.name) self.buf('\t%s= ' % tab_to(3, '.' + member_name)) # Special handling for lists if isinstance(prop.value, list): self.buf('{') vals = [] # For phandles, output a reference to the platform data # of the target node. info = self.get_phandle_argc(prop, node.name) if info: # Process the list as pairs of (phandle, id) pos = 0 for args in info.args: phandle_cell = prop.value[pos] phandle = fdt_util.fdt32_to_cpu(phandle_cell) target_node = self._fdt.phandle_to_node[phandle] name = conv_name_to_c(target_node.name) arg_values = [] for i in range(args): arg_values.append(str(fdt_util.fdt32_to_cpu(prop.value[pos + 1 + i]))) pos += 1 + args vals.append('\t{&%s%s, {%s}}' % (VAL_PREFIX, name, ', '.join(arg_values))) for val in vals: self.buf('\n\t\t%s,' % val) else: for val in prop.value: vals.append(get_value(prop.type, val)) # Put 8 values per line to avoid very long lines. for i in xrange(0, len(vals), 8): if i: self.buf(',\n\t\t') self.buf(', '.join(vals[i:i + 8])) self.buf('}') else: self.buf(get_value(prop.type, prop.value)) self.buf(',\n') self.buf('};\n') # Add a device declaration self.buf('U_BOOT_DEVICE(%s) = {\n' % var_name) self.buf('\t.name\t\t= "%s",\n' % struct_name) self.buf('\t.platdata\t= &%s%s,\n' % (VAL_PREFIX, var_name)) self.buf('\t.platdata_size\t= sizeof(%s%s),\n' % (VAL_PREFIX, var_name)) self.buf('};\n') self.buf('\n') self.out(''.join(self.get_buf()))
def GenerateTables(self): """Generate device defintions for the platform data This writes out C platform data initialisation data and U_BOOT_DEVICE() declarations for each valid node. See the documentation in README.of-plat for more information. """ self.Out('#include <common.h>\n') self.Out('#include <dm.h>\n') self.Out('#include <dt-structs.h>\n') self.Out('\n') node_txt_list = [] for node in self._valid_nodes: struct_name = self.GetCompatName(node) var_name = Conv_name_to_c(node.name) self.Buf('static struct %s%s %s%s = {\n' % (STRUCT_PREFIX, struct_name, VAL_PREFIX, var_name)) for pname, prop in node.props.iteritems(): if pname in PROP_IGNORE_LIST or pname[0] == '#': continue ptype = TYPE_NAMES[prop.type] member_name = Conv_name_to_c(prop.name) self.Buf('\t%s= ' % TabTo(3, '.' + member_name)) # Special handling for lists if type(prop.value) == list: self.Buf('{') vals = [] # For phandles, output a reference to the platform data # of the target node. if self.IsPhandle(prop): # Process the list as pairs of (phandle, id) it = iter(prop.value) for phandle_cell, id_cell in zip(it, it): phandle = fdt_util.fdt32_to_cpu(phandle_cell) id = fdt_util.fdt32_to_cpu(id_cell) target_node = self._phandle_node[phandle] name = Conv_name_to_c(target_node.name) vals.append('{&%s%s, %d}' % (VAL_PREFIX, name, id)) else: for val in prop.value: vals.append(self.GetValue(prop.type, val)) self.Buf(', '.join(vals)) self.Buf('}') else: self.Buf(self.GetValue(prop.type, prop.value)) self.Buf(',\n') self.Buf('};\n') # Add a device declaration self.Buf('U_BOOT_DEVICE(%s) = {\n' % var_name) self.Buf('\t.name\t\t= "%s",\n' % struct_name) self.Buf('\t.platdata\t= &%s%s,\n' % (VAL_PREFIX, var_name)) self.Buf('\t.platdata_size\t= sizeof(%s%s),\n' % (VAL_PREFIX, var_name)) self.Buf('};\n') self.Buf('\n') # Output phandle target nodes first, since they may be referenced # by others if 'phandle' in node.props: self.Out(''.join(self.GetBuf())) else: node_txt_list.append(self.GetBuf()) # Output all the nodes which are not phandle targets themselves, but # may reference them. This avoids the need for forward declarations. for node_txt in node_txt_list: self.Out(''.join(node_txt))
def testPhandle(self): dtb = fdt.FdtScan('tools/dtoc/dtoc_test_phandle.dts') node = dtb.GetNode('/phandle-source2') prop = node.props['clocks'] self.assertTrue(fdt32_to_cpu(prop.value) > 0)
def output_node(self, node): """Output the C code for a node Args: node: node to output """ struct_name, _ = get_compat_name(node) var_name = conv_name_to_c(node.name) self.buf('static const struct %s%s %s%s = {\n' % (STRUCT_PREFIX, struct_name, VAL_PREFIX, var_name)) for pname in sorted(node.props): prop = node.props[pname] if pname in PROP_IGNORE_LIST or pname[0] == '#': continue member_name = conv_name_to_c(prop.name) self.buf('\t%s= ' % tab_to(3, '.' + member_name)) # Special handling for lists if isinstance(prop.value, list): self.buf('{') vals = [] # For phandles, output a reference to the platform data # of the target node. info = self.get_phandle_argc(prop, node.name) if info: # Process the list as pairs of (phandle, id) pos = 0 for args in info.args: phandle_cell = prop.value[pos] phandle = fdt_util.fdt32_to_cpu(phandle_cell) target_node = self._fdt.phandle_to_node[phandle] name = conv_name_to_c(target_node.name) arg_values = [] for i in range(args): arg_values.append(str(fdt_util.fdt32_to_cpu(prop.value[pos + 1 + i]))) pos += 1 + args vals.append('\t{&%s%s, {%s}}' % (VAL_PREFIX, name, ', '.join(arg_values))) for val in vals: self.buf('\n\t\t%s,' % val) else: for val in prop.value: vals.append(get_value(prop.type, val)) # Put 8 values per line to avoid very long lines. for i in range(0, len(vals), 8): if i: self.buf(',\n\t\t') self.buf(', '.join(vals[i:i + 8])) self.buf('}') else: self.buf(get_value(prop.type, prop.value)) self.buf(',\n') self.buf('};\n') # Add a device declaration self.buf('U_BOOT_DEVICE(%s) = {\n' % var_name) self.buf('\t.name\t\t= "%s",\n' % struct_name) self.buf('\t.platdata\t= &%s%s,\n' % (VAL_PREFIX, var_name)) self.buf('\t.platdata_size\t= sizeof(%s%s),\n' % (VAL_PREFIX, var_name)) self.buf('};\n') self.buf('\n') self.out(''.join(self.get_buf()))
def GenerateTables(self): """Generate device defintions for the platform data This writes out C platform data initialisation data and U_BOOT_DEVICE() declarations for each valid node. See the documentation in README.of-plat for more information. """ self.Out('#include <common.h>\n') self.Out('#include <dm.h>\n') self.Out('#include <dt-structs.h>\n') self.Out('\n') node_txt_list = [] for node in self._valid_nodes: struct_name = self.GetCompatName(node) var_name = Conv_name_to_c(node.name) self.Buf('static struct %s%s %s%s = {\n' % (STRUCT_PREFIX, struct_name, VAL_PREFIX, var_name)) for pname, prop in node.props.items(): if pname in PROP_IGNORE_LIST or pname[0] == '#': continue ptype = TYPE_NAMES[prop.type] member_name = Conv_name_to_c(prop.name) self.Buf('\t%s= ' % TabTo(3, '.' + member_name)) # Special handling for lists if type(prop.value) == list: self.Buf('{') vals = [] # For phandles, output a reference to the platform data # of the target node. if self.IsPhandle(prop): # Process the list as pairs of (phandle, id) it = iter(prop.value) for phandle_cell, id_cell in zip(it, it): phandle = fdt_util.fdt32_to_cpu(phandle_cell) id = fdt_util.fdt32_to_cpu(id_cell) target_node = self._phandle_node[phandle] name = Conv_name_to_c(target_node.name) vals.append('{&%s%s, %d}' % (VAL_PREFIX, name, id)) else: for val in prop.value: vals.append(self.GetValue(prop.type, val)) self.Buf(', '.join(vals)) self.Buf('}') else: self.Buf(self.GetValue(prop.type, prop.value)) self.Buf(',\n') self.Buf('};\n') # Add a device declaration self.Buf('U_BOOT_DEVICE(%s) = {\n' % var_name) self.Buf('\t.name\t\t= "%s",\n' % struct_name) self.Buf('\t.platdata\t= &%s%s,\n' % (VAL_PREFIX, var_name)) self.Buf('\t.platdata_size\t= sizeof(%s%s),\n' % (VAL_PREFIX, var_name)) self.Buf('};\n') self.Buf('\n') # Output phandle target nodes first, since they may be referenced # by others if 'phandle' in node.props: self.Out(''.join(self.GetBuf())) else: node_txt_list.append(self.GetBuf()) # Output all the nodes which are not phandle targets themselves, but # may reference them. This avoids the need for forward declarations. for node_txt in node_txt_list: self.Out(''.join(node_txt))