|
| 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/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, cfg_reg)] = (match.group(1), daisy_reg, daisy) |
| 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, cfg_reg) in ground_truth: |
| 89 | + mux_truth = ground_truth[(mux_reg, mux, cfg_reg)] |
| 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 (not args.check) and (errors): |
| 98 | + # Fix up file |
| 99 | + new_line = (f"\t\tpinmux = <0x{mux_reg:x} {mux} " |
| 100 | + f"0x{mux_truth[1]:x} {mux_truth[2]} 0x{cfg_reg:x}>;\n") |
| 101 | + tmp.write(new_line) |
| 102 | + elif not args.check: |
| 103 | + # Write back untouched line |
| 104 | + tmp.write(line) |
| 105 | + else: |
| 106 | + print("WARNING: Mux name %s not preset in ground truth" % (name)) |
| 107 | + if not args.check: |
| 108 | + # Write back untouched line |
| 109 | + tmp.write(line) |
| 110 | +if not args.check: |
| 111 | + # Commit operation by writing temp file to real file |
| 112 | + tmp.seek(0) |
| 113 | + pinctrl_file.seek(0) |
| 114 | + while True: |
| 115 | + tmpline = tmp.readline() |
| 116 | + if tmpline == '': |
| 117 | + break |
| 118 | + pinctrl_file.write(tmpline) |
| 119 | + |
| 120 | +iomux_file.close() |
| 121 | +pinctrl_file.close() |
| 122 | +if not args.check: |
| 123 | + tmp.close() |
0 commit comments