2 # SPDX-License-Identifier: GPL-2.0+
4 # Copyright (C) 2016 Google, Inc
5 # Written by Simon Glass <sjg@chromium.org>
8 # Utility functions for reading from a device tree. Once the upstream pylibfdt
9 # implementation advances far enough, we should be able to drop these.
16 from patman import command
17 from patman import tools
19 def fdt32_to_cpu(val):
20 """Convert a device tree cell to an integer
23 Value to convert (4-character string representing the cell value)
26 A native-endian integer value
28 return struct.unpack('>I', val)[0]
30 def fdt64_to_cpu(val):
31 """Convert a device tree cell to an integer
34 val (list): Value to convert (list of 2 4-character strings representing
38 int: A native-endian integer value
40 return fdt32_to_cpu(val[0]) << 32 | fdt32_to_cpu(val[1])
42 def fdt_cells_to_cpu(val, cells):
43 """Convert one or two cells to a long integer
46 Value to convert (array of one or more 4-character strings)
49 A native-endian integer value
53 out = int(fdt32_to_cpu(val[0]))
55 out = out << 32 | fdt32_to_cpu(val[1])
58 def EnsureCompiled(fname, tmpdir=None, capture_stderr=False):
59 """Compile an fdt .dts source file into a .dtb binary blob if needed.
62 fname: Filename (if .dts it will be compiled). It not it will be
64 tmpdir: Temporary directory for output files, or None to use the
65 tools-module output directory
68 Filename of resulting .dtb file
70 _, ext = os.path.splitext(fname)
75 dts_input = os.path.join(tmpdir, 'source.dts')
76 dtb_output = os.path.join(tmpdir, 'source.dtb')
78 dts_input = tools.GetOutputFilename('source.dts')
79 dtb_output = tools.GetOutputFilename('source.dtb')
81 search_paths = [os.path.join(os.getcwd(), 'include')]
82 root, _ = os.path.splitext(fname)
83 cc, args = tools.GetTargetCompileTool('cc')
84 args += ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__']
86 for path in search_paths:
87 args.extend(['-I', path])
88 args += ['-o', dts_input, fname]
89 command.Run(cc, *args)
91 # If we don't have a directory, put it in the tools tempdir
93 for path in search_paths:
94 search_list.extend(['-i', path])
95 dtc, args = tools.GetTargetCompileTool('dtc')
96 args += ['-I', 'dts', '-o', dtb_output, '-O', 'dtb',
97 '-W', 'no-unit_address_vs_reg']
98 args.extend(search_list)
99 args.append(dts_input)
100 command.Run(dtc, *args, capture_stderr=capture_stderr)
103 def GetInt(node, propname, default=None):
104 """Get an integer from a property
107 node: Node object to read from
108 propname: property name to read
109 default: Default value to use if the node/property do not exist
112 Integer value read, or default if none
114 prop = node.props.get(propname)
117 if isinstance(prop.value, list):
118 raise ValueError("Node '%s' property '%s' has list value: expecting "
119 "a single integer" % (node.name, propname))
120 value = fdt32_to_cpu(prop.value)
123 def GetInt64(node, propname, default=None):
124 """Get a 64-bit integer from a property
127 node (Node): Node object to read from
128 propname (str): property name to read
129 default (int): Default value to use if the node/property do not exist
132 int: value read, or default if none
135 ValueError: Property is not of the correct size
137 prop = node.props.get(propname)
140 if not isinstance(prop.value, list) or len(prop.value) != 2:
141 raise ValueError("Node '%s' property '%s' should be a list with 2 items for 64-bit values" %
142 (node.name, propname))
143 value = fdt64_to_cpu(prop.value)
146 def GetString(node, propname, default=None):
147 """Get a string from a property
150 node: Node object to read from
151 propname: property name to read
152 default: Default value to use if the node/property do not exist
155 String value read, or default if none
157 prop = node.props.get(propname)
161 if isinstance(value, list):
162 raise ValueError("Node '%s' property '%s' has list value: expecting "
163 "a single string" % (node.name, propname))
166 def GetBool(node, propname, default=False):
167 """Get an boolean from a property
170 node: Node object to read from
171 propname: property name to read
172 default: Default value to use if the node/property do not exist
175 Boolean value read, or default if none (if you set this to True the
176 function will always return True)
178 if propname in node.props:
182 def GetByte(node, propname, default=None):
183 """Get an byte from a property
186 node: Node object to read from
187 propname: property name to read
188 default: Default value to use if the node/property do not exist
191 Byte value read, or default if none
193 prop = node.props.get(propname)
197 if isinstance(value, list):
198 raise ValueError("Node '%s' property '%s' has list value: expecting "
199 "a single byte" % (node.name, propname))
201 raise ValueError("Node '%s' property '%s' has length %d, expecting %d" %
202 (node.name, propname, len(value), 1))
205 def GetBytes(node, propname, size, default=None):
206 """Get a set of bytes from a property
209 node (Node): Node object to read from
210 propname (str): property name to read
211 size (int): Number of bytes to expect
212 default (bytes): Default value or None
215 bytes: Bytes value read, or default if none
217 prop = node.props.get(propname)
220 if len(prop.bytes) != size:
221 raise ValueError("Node '%s' property '%s' has length %d, expecting %d" %
222 (node.name, propname, len(prop.bytes), size))
225 def GetPhandleList(node, propname):
226 """Get a list of phandles from a property
229 node: Node object to read from
230 propname: property name to read
233 List of phandles read, each an integer
235 prop = node.props.get(propname)
239 if not isinstance(value, list):
241 return [fdt32_to_cpu(v) for v in value]
243 def GetDatatype(node, propname, datatype):
244 """Get a value of a given type from a property
247 node: Node object to read from
248 propname: property name to read
249 datatype: Type to read (str or int)
252 value read, or None if none
255 ValueError if datatype is not str or int
258 return GetString(node, propname)
259 elif datatype == int:
260 return GetInt(node, propname)
261 raise ValueError("fdt_util internal error: Unknown data type '%s'" %