Skip to content

Commit 37593c4

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 da9e969 commit 37593c4

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

mcux/scripts/imx_fixup_pinmux.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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_]+)\s+([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/omit-if-no-ref/ [\w_]+: ([\w_]+) {\n')
42+
pinctrl_line2_re = re.compile(r'\t\tpinmux = <(0x[\da-f]+) '
43+
r'(\d+) (0x[\da-f]+) (\d+) (0x[\da-f]+)>;')
44+
45+
ground_truth = {}
46+
while True:
47+
line = iomux_file.readline()
48+
if line == '':
49+
break
50+
match = iomuxc_re.match(line)
51+
if match:
52+
mux_reg = int(match.group(2), 0)
53+
mux = int(match.group(3), 0)
54+
daisy_reg = int(match.group(4), 0)
55+
daisy = int(match.group(5), 0)
56+
cfg_reg = int(match.group(6), 0)
57+
if (mux_reg, mux, cfg_reg) in ground_truth:
58+
raise RuntimeError("Duplicate mux_reg and mux value pairings")
59+
ground_truth[(mux_reg, mux)] = (match.group(1), daisy_reg, daisy, cfg_reg)
60+
61+
if not args.check:
62+
tmp = tempfile.TemporaryFile(mode='r+')
63+
while True:
64+
line = pinctrl_file.readline()
65+
if line == '':
66+
break
67+
if not args.check:
68+
if line == '&iomuxc {\n':
69+
# Write a comment before dts defintions
70+
tmp.write('/*\n'
71+
f' * NOTE: file fixup performed by {os.path.basename(__file__)}\n'
72+
" * to correct missing daisy register values\n"
73+
' */\n\n')
74+
# Write back untouched line
75+
tmp.write(line)
76+
match = pinctrl_line1_re.match(line)
77+
if match:
78+
line = pinctrl_file.readline()
79+
match2 = pinctrl_line2_re.match(line)
80+
if match2:
81+
mux_reg = int(match2.group(1), 0)
82+
mux = int(match2.group(2), 0)
83+
daisy_reg = int(match2.group(3), 0)
84+
daisy = int(match2.group(4), 0)
85+
cfg_reg = int(match2.group(5), 0)
86+
name = match.group(1)
87+
errors = False
88+
if (mux_reg, mux) in ground_truth:
89+
mux_truth = ground_truth[(mux_reg, mux)]
90+
# Get the MUX daisy values
91+
if mux_truth[1] != daisy_reg:
92+
print(f"Bad daisy reg on {name}")
93+
errors = True
94+
if mux_truth[2] != daisy:
95+
print(f"Bad daisy value on {name}")
96+
errors = True
97+
if mux_truth[3] != cfg_reg:
98+
print(f"Bad cfg value on {name}")
99+
errors = True
100+
if (not args.check) and (errors):
101+
# Fix up file
102+
new_line = (f"\t\tpinmux = <0x{mux_reg:x} {mux} "
103+
f"0x{mux_truth[1]:x} {mux_truth[2]} 0x{mux_truth[3]:x}>;\n")
104+
tmp.write(new_line)
105+
elif not args.check:
106+
# Write back untouched line
107+
tmp.write(line)
108+
else:
109+
print("WARNING: Mux name %s not preset in ground truth" % (name))
110+
if not args.check:
111+
# Write back untouched line
112+
tmp.write(line)
113+
if not args.check:
114+
# Commit operation by writing temp file to real file
115+
tmp.seek(0)
116+
pinctrl_file.seek(0)
117+
while True:
118+
tmpline = tmp.readline()
119+
if tmpline == '':
120+
break
121+
pinctrl_file.write(tmpline)
122+
123+
iomux_file.close()
124+
pinctrl_file.close()
125+
if not args.check:
126+
tmp.close()

0 commit comments

Comments
 (0)