Skip to content

Commit b4a3f73

Browse files
committed
scripts: dts: edtlib: Add Map dataclass
Add Map dataclass to store `*-map` property. Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent 91b27b2 commit b4a3f73

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

scripts/dts/python-devicetree/src/devicetree/edtlib.py

+71
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,42 @@ def name_as_token(self):
868868
return str_as_token(self.name) if self.name is not None else None
869869

870870

871+
@dataclass
872+
class Map:
873+
"""
874+
Represents a "map" on a node.
875+
876+
node:
877+
The Node instance this map is from
878+
879+
specifier:
880+
The specifier of the map properties.
881+
882+
map:
883+
The <name>-map values
884+
885+
child_cell:
886+
The number of the child cells
887+
888+
child_addr:
889+
The length of the child address cells
890+
891+
parent_cells:
892+
The list of the number of each parent cells
893+
894+
parent_addrs:
895+
The list of the length of each parent address cells
896+
"""
897+
898+
node: 'Node'
899+
specifier: str
900+
map: list[Union[int, str, 'Node']]
901+
child_cell: int
902+
child_addr: int
903+
parent_cells: list[int]
904+
parent_addrs: list[int]
905+
906+
871907
class Node:
872908
"""
873909
Represents a devicetree node, augmented with information from bindings, and
@@ -1442,6 +1478,7 @@ def _init_crossrefs(
14421478
)
14431479
self._init_interrupts()
14441480
self._init_pinctrls()
1481+
self._init_maps()
14451482

14461483
def _init_props(self, default_prop_types: bool = False,
14471484
err_on_deprecated: bool = False) -> None:
@@ -1728,6 +1765,40 @@ def _init_regs(self) -> None:
17281765

17291766
_add_names(node, "reg", self.regs)
17301767

1768+
def _init_maps(self) -> None:
1769+
# Initializes self.maps
1770+
1771+
props = self._node.props
1772+
1773+
self.maps = []
1774+
1775+
def conv_enode(x):
1776+
return self.edt._node2enode.get(x) if isinstance(x, dtlib_Node) else x
1777+
1778+
def get_address_cells(n):
1779+
try:
1780+
return _cells(n, "address")
1781+
except Exception:
1782+
return _address_cells(n)
1783+
1784+
for k in [k for k in props if k.endswith('-map')]:
1785+
name = k[:-4]
1786+
map_list = list(map(conv_enode, props[k].to_compound()))
1787+
1788+
child_cell = _cells(self._node, name)
1789+
parent_cells = [_cells(x._node, name) for x in map_list if isinstance(x, Node)]
1790+
1791+
child_addr = 0
1792+
parent_addrs = [0] * len(parent_cells)
1793+
1794+
if name == "interrupt":
1795+
child_addr = get_address_cells(self._node)
1796+
parent_addrs = [get_address_cells(x._node) for x in map_list if isinstance(x, Node)]
1797+
1798+
self.maps.append(
1799+
Map(self, name, map_list, child_cell, child_addr, parent_cells, parent_addrs)
1800+
)
1801+
17311802
def _init_pinctrls(self) -> None:
17321803
# Initializes self.pinctrls from any pinctrl-<index> properties
17331804

scripts/dts/python-devicetree/tests/test_edtlib.py

+46
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,52 @@ def verify_regs(node, expected_tuples):
209209
verify_regs(edt.get_node("/reg-nested-ranges/grandparent/parent/node"),
210210
[(None, 0x30000000200000001, 0x1)])
211211

212+
def test_map():
213+
'''Tests for map properties'''
214+
with from_here():
215+
edt = edtlib.EDT("test.dts", ["test-bindings"])
216+
217+
def verify_maps(
218+
node,
219+
specifier,
220+
expected_maps,
221+
expected_child_cell,
222+
expected_child_addr,
223+
expected_parent_cells,
224+
expected_parent_addrs,
225+
):
226+
def node_to_path(n):
227+
return n if isinstance(n, int) else n.path
228+
229+
for m in [m for m in node.maps if m.specifier == specifier]:
230+
maps = list(map(node_to_path, m.map))
231+
assert maps == expected_maps
232+
assert m.child_cell == expected_child_cell
233+
assert m.child_addr == expected_child_addr
234+
assert m.parent_cells == expected_parent_cells
235+
assert m.parent_addrs == expected_parent_addrs
236+
237+
verify_maps(edt.get_node("/interrupt-map-test/nexus"), specifier="interrupt",
238+
expected_maps=[0, 0, 0, 0, "/interrupt-map-test/controller-0", 0, 0,
239+
0, 0, 0, 1, "/interrupt-map-test/controller-1", 0, 0, 0, 1,
240+
0, 0, 0, 2, "/interrupt-map-test/controller-2", 0, 0, 0, 0, 0, 2,
241+
0, 1, 0, 0, "/interrupt-map-test/controller-0", 0, 3,
242+
0, 1, 0, 1, "/interrupt-map-test/controller-1", 0, 0, 0, 4,
243+
0, 1, 0, 2, "/interrupt-map-test/controller-2", 0, 0, 0, 0, 0, 5],
244+
expected_child_cell=2, expected_child_addr=2,
245+
expected_parent_cells=[1, 2, 3, 1, 2, 3], expected_parent_addrs=[1, 2, 3, 1, 2, 3])
246+
247+
verify_maps(edt.get_node("/interrupt-map-bitops-test/nexus"), specifier="interrupt",
248+
expected_maps=[6, 6, 6, 6, "/interrupt-map-bitops-test/controller", 2, 1],
249+
expected_child_cell=2, expected_child_addr=2,
250+
expected_parent_cells=[2], expected_parent_addrs=[0])
251+
252+
verify_maps(edt.get_node("/gpio-map/connector"), specifier="gpio",
253+
expected_maps=[1, 2, "/gpio-map/destination", 5,
254+
3, 4, "/gpio-map/destination", 6],
255+
expected_child_cell=2, expected_child_addr=0,
256+
expected_parent_cells=[1, 1], expected_parent_addrs=[0, 0])
257+
212258
def test_pinctrl():
213259
'''Test 'pinctrl-<index>'.'''
214260
with from_here():

0 commit comments

Comments
 (0)