Skip to content

Commit ad0fc26

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 1a82ed2 commit ad0fc26

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
@@ -866,6 +866,42 @@ def name_as_token(self):
866866
return str_as_token(self.name) if self.name is not None else None
867867

868868

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

14441481
def _init_props(self, default_prop_types: bool = False,
14451482
err_on_deprecated: bool = False) -> None:
@@ -1726,6 +1763,40 @@ def _init_regs(self) -> None:
17261763

17271764
_add_names(node, "reg", self.regs)
17281765

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

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)