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 fdt_cells_to_cpu(val, cells):
31 """Convert one or two cells to a long integer
34 Value to convert (array of one or more 4-character strings)
37 A native-endian integer value
41 out = int(fdt32_to_cpu(val[0]))
43 out = out << 32 | fdt32_to_cpu(val[1])
46 def EnsureCompiled(fname, tmpdir=None, capture_stderr=False):
47 """Compile an fdt .dts source file into a .dtb binary blob if needed.
50 fname: Filename (if .dts it will be compiled). It not it will be
52 tmpdir: Temporary directory for output files, or None to use the
53 tools-module output directory
56 Filename of resulting .dtb file
58 _, ext = os.path.splitext(fname)
63 dts_input = os.path.join(tmpdir, 'source.dts')
64 dtb_output = os.path.join(tmpdir, 'source.dtb')
66 dts_input = tools.GetOutputFilename('source.dts')
67 dtb_output = tools.GetOutputFilename('source.dtb')
69 search_paths = [os.path.join(os.getcwd(), 'include')]
70 root, _ = os.path.splitext(fname)
71 cc, args = tools.GetTargetCompileTool('cc')
72 args += ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__']
74 for path in search_paths:
75 args.extend(['-I', path])
76 args += ['-o', dts_input, fname]
77 command.Run(cc, *args)
79 # If we don't have a directory, put it in the tools tempdir
81 for path in search_paths:
82 search_list.extend(['-i', path])
83 dtc, args = tools.GetTargetCompileTool('dtc')
84 args += ['-I', 'dts', '-o', dtb_output, '-O', 'dtb',
85 '-W', 'no-unit_address_vs_reg']
86 args.extend(search_list)
87 args.append(dts_input)
88 command.Run(dtc, *args, capture_stderr=capture_stderr)
91 def GetInt(node, propname, default=None):
92 """Get an integer from a property
95 node: Node object to read from
96 propname: property name to read
97 default: Default value to use if the node/property do not exist
100 Integer value read, or default if none
102 prop = node.props.get(propname)
105 if isinstance(prop.value, list):
106 raise ValueError("Node '%s' property '%s' has list value: expecting "
107 "a single integer" % (node.name, propname))
108 value = fdt32_to_cpu(prop.value)
111 def GetString(node, propname, default=None):
112 """Get a string from a property
115 node: Node object to read from
116 propname: property name to read
117 default: Default value to use if the node/property do not exist
120 String value read, or default if none
122 prop = node.props.get(propname)
126 if isinstance(value, list):
127 raise ValueError("Node '%s' property '%s' has list value: expecting "
128 "a single string" % (node.name, propname))
131 def GetBool(node, propname, default=False):
132 """Get an boolean from a property
135 node: Node object to read from
136 propname: property name to read
137 default: Default value to use if the node/property do not exist
140 Boolean value read, or default if none (if you set this to True the
141 function will always return True)
143 if propname in node.props:
147 def GetByte(node, propname, default=None):
148 """Get an byte from a property
151 node: Node object to read from
152 propname: property name to read
153 default: Default value to use if the node/property do not exist
156 Byte value read, or default if none
158 prop = node.props.get(propname)
162 if isinstance(value, list):
163 raise ValueError("Node '%s' property '%s' has list value: expecting "
164 "a single byte" % (node.name, propname))
166 raise ValueError("Node '%s' property '%s' has length %d, expecting %d" %
167 (node.name, propname, len(value), 1))
170 def GetPhandleList(node, propname):
171 """Get a list of phandles from a property
174 node: Node object to read from
175 propname: property name to read
178 List of phandles read, each an integer
180 prop = node.props.get(propname)
184 if not isinstance(value, list):
186 return [fdt32_to_cpu(v) for v in value]
188 def GetDatatype(node, propname, datatype):
189 """Get a value of a given type from a property
192 node: Node object to read from
193 propname: property name to read
194 datatype: Type to read (str or int)
197 value read, or None if none
200 ValueError if datatype is not str or int
203 return GetString(node, propname)
204 elif datatype == int:
205 return GetInt(node, propname)
206 raise ValueError("fdt_util internal error: Unknown data type '%s'" %