Skip to content

Commit f333cdb

Browse files
scripts: add pinmux fixup script
RT11xx series signal configuration lacks daisy register settings. Add script to identify these issues and fix them Signed-off-by: Daniel DeGrasse <[email protected]>
1 parent b593029 commit f333cdb

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

mcux/scripts/rt_fixup_pinmux.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
import tempfile
5+
import re
6+
import os
7+
8+
HELPSTR = """
9+
Fix generated pinctrl defintions
10+
Generated RT11xx pinctrl definitions will be incorrect, because the signal
11+
configuration XML file itself is not correct. Fix them using fsl_iomuxc
12+
definitions.
13+
"""
14+
15+
16+
parser = argparse.ArgumentParser(description=HELPSTR,
17+
formatter_class=argparse.RawDescriptionHelpFormatter)
18+
parser.add_argument('iomuxc_file', metavar = 'IOMUXC',
19+
type=str,
20+
help='iomuxc file to use as ground truth')
21+
parser.add_argument('pinctrl_file', metavar = 'pinctrl',
22+
type=str,
23+
help='pinctrl file to check against iomuxc file')
24+
parser.add_argument('--check', action='store_true',
25+
help='do not edit pinctrl file, just check for errors')
26+
27+
args = parser.parse_args()
28+
29+
try:
30+
iomux_file = open(args.iomuxc_file, 'r', encoding='utf8')
31+
except IOError:
32+
print(f"Could not open {args.iomuxc_file}")
33+
try:
34+
pinctrl_file = open(args.pinctrl_file, 'r+', encoding='utf8')
35+
except IOError:
36+
print(f"Could not open {args.pinctrl_file}")
37+
38+
iomuxc_re = re.compile(r'#define (IOMUXC_[\w_]+) ([0x]*[\dA-F]+)[U]*, ([0x]*[\dA-F]+)[U]*,'
39+
r' ([0x]*[\dA-F]+)[U]*, ([0x]*[\dA-F]+)[U]*, ([0x]*[\dA-F]+)[U]*\n')
40+
41+
pinctrl_line1_re = re.compile(r'\t[\w_]+: ([\w_]+) {\n')
42+
pinctrl_line2_re = re.compile(r'\t\tpinmux = <(0x[\da-f]+) (\d+) (0x[\da-f]+) (\d+) (0x[\da-f]+)>;')
43+
44+
ground_truth = {}
45+
while True:
46+
line = iomux_file.readline()
47+
if line == '':
48+
break
49+
match = iomuxc_re.match(line)
50+
if match:
51+
mux_reg = int(match.group(2), 0)
52+
mux = int(match.group(3), 0)
53+
daisy_reg = int(match.group(4), 0)
54+
daisy = int(match.group(5), 0)
55+
cfg_reg = int(match.group(6), 0)
56+
if (mux_reg, mux, cfg_reg) in ground_truth:
57+
raise RuntimeError("Duplicate mux_reg and mux value pairings")
58+
ground_truth[(mux_reg, mux, cfg_reg)] = (match.group(1), daisy_reg, daisy)
59+
60+
if not args.check:
61+
tmp = tempfile.TemporaryFile(mode='r+')
62+
while True:
63+
line = pinctrl_file.readline()
64+
if line == '':
65+
break
66+
if not args.check:
67+
if line == '&iomuxc {\n':
68+
# Write a comment before dts defintions
69+
tmp.write('/*\n'
70+
f' * NOTE: file fixup performed by {os.path.basename(__file__)}\n'
71+
" * to correct missing daisy register values\n"
72+
' */\n\n')
73+
# Write back untouched line
74+
tmp.write(line)
75+
match = pinctrl_line1_re.match(line)
76+
if match:
77+
line = pinctrl_file.readline()
78+
match2 = pinctrl_line2_re.match(line)
79+
if match2:
80+
mux_reg = int(match2.group(1), 0)
81+
mux = int(match2.group(2), 0)
82+
daisy_reg = int(match2.group(3), 0)
83+
daisy = int(match2.group(4), 0)
84+
cfg_reg = int(match2.group(5), 0)
85+
name = match.group(1)
86+
errors = False
87+
if (mux_reg, mux, cfg_reg) in ground_truth:
88+
mux_truth = ground_truth[(mux_reg, mux, cfg_reg)]
89+
# Get the MUX daisy values
90+
if mux_truth[1] != daisy_reg:
91+
print(f"Bad daisy reg on {name}")
92+
errors = True
93+
if mux_truth[2] != daisy:
94+
print(f"Bad daisy value on {name}")
95+
errors = True
96+
if (not args.check) and (errors):
97+
# Fix up file
98+
new_line = (f"\t\tpinmux = <0x{mux_reg:x} {mux} "
99+
f"0x{mux_truth[1]:x} {mux_truth[2]} 0x{cfg_reg:x}>;\n")
100+
tmp.write(new_line)
101+
elif not args.check:
102+
# Write back untouched line
103+
tmp.write(line)
104+
else:
105+
print("WARNING: Mux name %s not preset in ground truth" % (name))
106+
# Write back untouched line
107+
tmp.write(line)
108+
if not args.check:
109+
# Commit operation by writing temp file to real file
110+
tmp.seek(0)
111+
pinctrl_file.seek(0)
112+
while True:
113+
tmpline = tmp.readline()
114+
if tmpline == '':
115+
break
116+
pinctrl_file.write(tmpline)
117+
118+
iomux_file.close()
119+
pinctrl_file.close()
120+
tmp.close()

0 commit comments

Comments
 (0)