Skip to content
Snippets Groups Projects
Commit 56e0bbe0 authored by Simon Glass's avatar Simon Glass
Browse files

dtoc: Move static functions out of the class


Rather than using static functions within the class, move them out of the
class. This will make it slightly easier for tests to call them.

Signed-off-by: default avatarSimon Glass <sjg@chromium.org>
parent e36024b0
Branches
Tags
No related merge requests found
...@@ -72,6 +72,60 @@ def tab_to(num_tabs, line): ...@@ -72,6 +72,60 @@ def tab_to(num_tabs, line):
return line + ' ' return line + ' '
return line + '\t' * (num_tabs - len(line) // 8) return line + '\t' * (num_tabs - len(line) // 8)
def get_value(ftype, 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 ftype == fdt.TYPE_INT:
return '%#x' % fdt_util.fdt32_to_cpu(value)
elif ftype == fdt.TYPE_BYTE:
return '%#x' % ord(value[0])
elif ftype == fdt.TYPE_STRING:
return '"%s"' % value
elif ftype == fdt.TYPE_BOOL:
return 'true'
def get_compat_name(node):
"""Get a node's first compatible string as a C identifier
Args:
node: Node object to check
Return:
Tuple:
C identifier for the first compatible string
List of C identifiers for all the other compatible strings
(possibly empty)
"""
compat = node.props['compatible'].value
aliases = []
if isinstance(compat, list):
compat, aliases = compat[0], compat[1:]
return conv_name_to_c(compat), [conv_name_to_c(a) for a in aliases]
def is_phandle(prop):
"""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:
True if the object value contains phandles, else False
"""
if prop.name in ['clocks']:
return True
return False
class DtbPlatdata(object): class DtbPlatdata(object):
"""Provide a means to convert device tree binary data to platform data """Provide a means to convert device tree binary data to platform data
...@@ -139,43 +193,6 @@ class DtbPlatdata(object): ...@@ -139,43 +193,6 @@ class DtbPlatdata(object):
self._lines = [] self._lines = []
return lines return lines
@staticmethod
def get_value(ftype, 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 ftype == fdt.TYPE_INT:
return '%#x' % fdt_util.fdt32_to_cpu(value)
elif ftype == fdt.TYPE_BYTE:
return '%#x' % ord(value[0])
elif ftype == fdt.TYPE_STRING:
return '"%s"' % value
elif ftype == fdt.TYPE_BOOL:
return 'true'
@staticmethod
def get_compat_name(node):
"""Get a node's first compatible string as a C identifier
Args:
node: Node object to check
Return:
C identifier for the first compatible string
"""
compat = node.props['compatible'].value
aliases = []
if isinstance(compat, list):
compat, aliases = compat[0], compat[1:]
return conv_name_to_c(compat), [conv_name_to_c(a) for a in aliases]
def scan_dtb(self): def scan_dtb(self):
"""Scan the device tree to obtain a tree of notes and properties """Scan the device tree to obtain a tree of notes and properties
...@@ -219,22 +236,6 @@ class DtbPlatdata(object): ...@@ -219,22 +236,6 @@ class DtbPlatdata(object):
self._valid_nodes = [] self._valid_nodes = []
return self.scan_node(self._fdt.GetRoot()) return self.scan_node(self._fdt.GetRoot())
@staticmethod
def is_phandle(prop):
"""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:
True if the object value contains phandles, else False
"""
if prop.name in ['clocks']:
return True
return False
def scan_structs(self): def scan_structs(self):
"""Scan the device tree building up the C structures we will use. """Scan the device tree building up the C structures we will use.
...@@ -248,7 +249,7 @@ class DtbPlatdata(object): ...@@ -248,7 +249,7 @@ class DtbPlatdata(object):
""" """
structs = {} structs = {}
for node in self._valid_nodes: for node in self._valid_nodes:
node_name, _ = self.get_compat_name(node) node_name, _ = get_compat_name(node)
fields = {} fields = {}
# Get a list of all the valid properties in this node. # Get a list of all the valid properties in this node.
...@@ -272,14 +273,14 @@ class DtbPlatdata(object): ...@@ -272,14 +273,14 @@ class DtbPlatdata(object):
upto = 0 upto = 0
for node in self._valid_nodes: for node in self._valid_nodes:
node_name, _ = self.get_compat_name(node) node_name, _ = get_compat_name(node)
struct = structs[node_name] struct = structs[node_name]
for name, prop in node.props.items(): for name, prop in node.props.items():
if name not in PROP_IGNORE_LIST and name[0] != '#': if name not in PROP_IGNORE_LIST and name[0] != '#':
prop.Widen(struct[name]) prop.Widen(struct[name])
upto += 1 upto += 1
struct_name, aliases = self.get_compat_name(node) struct_name, aliases = get_compat_name(node)
for alias in aliases: for alias in aliases:
self._aliases[alias] = struct_name self._aliases[alias] = struct_name
...@@ -302,7 +303,7 @@ class DtbPlatdata(object): ...@@ -302,7 +303,7 @@ class DtbPlatdata(object):
if pname in PROP_IGNORE_LIST or pname[0] == '#': if pname in PROP_IGNORE_LIST or pname[0] == '#':
continue continue
if isinstance(prop.value, list): if isinstance(prop.value, list):
if self.is_phandle(prop): if is_phandle(prop):
# Process the list as pairs of (phandle, id) # Process the list as pairs of (phandle, id)
value_it = iter(prop.value) value_it = iter(prop.value)
for phandle_cell, _ in zip(value_it, value_it): for phandle_cell, _ in zip(value_it, value_it):
...@@ -326,7 +327,7 @@ class DtbPlatdata(object): ...@@ -326,7 +327,7 @@ class DtbPlatdata(object):
self.out('struct %s%s {\n' % (STRUCT_PREFIX, name)) self.out('struct %s%s {\n' % (STRUCT_PREFIX, name))
for pname in sorted(structs[name]): for pname in sorted(structs[name]):
prop = structs[name][pname] prop = structs[name][pname]
if self.is_phandle(prop): if is_phandle(prop):
# For phandles, include a reference to the target # For phandles, include a reference to the target
self.out('\t%s%s[%d]' % (tab_to(2, 'struct phandle_2_cell'), self.out('\t%s%s[%d]' % (tab_to(2, 'struct phandle_2_cell'),
conv_name_to_c(prop.name), conv_name_to_c(prop.name),
...@@ -350,7 +351,7 @@ class DtbPlatdata(object): ...@@ -350,7 +351,7 @@ class DtbPlatdata(object):
Args: Args:
node: node to output node: node to output
""" """
struct_name, _ = self.get_compat_name(node) struct_name, _ = get_compat_name(node)
var_name = conv_name_to_c(node.name) var_name = conv_name_to_c(node.name)
self.buf('static struct %s%s %s%s = {\n' % self.buf('static struct %s%s %s%s = {\n' %
(STRUCT_PREFIX, struct_name, VAL_PREFIX, var_name)) (STRUCT_PREFIX, struct_name, VAL_PREFIX, var_name))
...@@ -366,7 +367,7 @@ class DtbPlatdata(object): ...@@ -366,7 +367,7 @@ class DtbPlatdata(object):
vals = [] vals = []
# For phandles, output a reference to the platform data # For phandles, output a reference to the platform data
# of the target node. # of the target node.
if self.is_phandle(prop): if is_phandle(prop):
# Process the list as pairs of (phandle, id) # Process the list as pairs of (phandle, id)
value_it = iter(prop.value) value_it = iter(prop.value)
for phandle_cell, id_cell in zip(value_it, value_it): for phandle_cell, id_cell in zip(value_it, value_it):
...@@ -377,11 +378,11 @@ class DtbPlatdata(object): ...@@ -377,11 +378,11 @@ class DtbPlatdata(object):
vals.append('{&%s%s, %d}' % (VAL_PREFIX, name, id_num)) vals.append('{&%s%s, %d}' % (VAL_PREFIX, name, id_num))
else: else:
for val in prop.value: for val in prop.value:
vals.append(self.get_value(prop.type, val)) vals.append(get_value(prop.type, val))
self.buf(', '.join(vals)) self.buf(', '.join(vals))
self.buf('}') self.buf('}')
else: else:
self.buf(self.get_value(prop.type, prop.value)) self.buf(get_value(prop.type, prop.value))
self.buf(',\n') self.buf(',\n')
self.buf('};\n') self.buf('};\n')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment