diff --git a/patches/20-arch-dts-bindings.patch b/patches/20-arch-dts-bindings.patch index 207b8aa..f67468d 100644 --- a/patches/20-arch-dts-bindings.patch +++ b/patches/20-arch-dts-bindings.patch @@ -2310,7 +2310,7 @@ new file mode 100644 index 000000000..a6d26cc2b --- /dev/null +++ b/arch/arm/boot/dts/rockchip/rv1106.dtsi -@@ -0,0 +1,1554 @@ +@@ -0,0 +1,1557 @@ +// SPDX-License-Identifier: (GPL-2.0+ OR MIT) +/* + * Copyright (c) 2022 Rockchip Electronics Co., Ltd. @@ -3010,15 +3010,18 @@ index 000000000..a6d26cc2b + #clock-cells = <1>; + #reset-cells = <1>; + ++ /* CPLL 900 MHz: dclk_vop /30, GMAC 50 MHz /18 and APB 100 MHz /9 are exact (bfe-core1106-sdk#43). */ + assigned-clocks = + <&cru PLL_GPLL>, <&cru PLL_CPLL>, ++ <&cru CLK_50M_SRC>, <&cru CLK_100M_SRC>, <&cru DCLK_VOP_SRC>, + <&cru ARMCLK>, + <&cru ACLK_PERI_ROOT>, <&cru HCLK_PERI_ROOT>, + <&cru PCLK_PERI_ROOT>, <&cru ACLK_BUS_ROOT>, + <&cru PCLK_TOP_ROOT>, <&cru PCLK_PMU_ROOT>, + <&cru HCLK_PMU_ROOT>; + assigned-clock-rates = -+ <1188000000>, <1000000000>, ++ <1188000000>, <900000000>, ++ <50000000>, <100000000>, <30000000>, + <1104000000>, + <400000000>, <200000000>, + <100000000>, <300000000>, diff --git a/tests/vop-dclk/check-vop-dclk.py b/tests/vop-dclk/check-vop-dclk.py new file mode 100755 index 0000000..343a8c5 --- /dev/null +++ b/tests/vop-dclk/check-vop-dclk.py @@ -0,0 +1,227 @@ +#!/usr/bin/env python3 +"""Check that the Linux CRU clock plan puts the VOP pixel clock on the 30 MHz mode. + +bfe-core1106-sdk#43: with the Linux CPLL at its 1000 MHz default, dclk_vop_src (a +composite with a 5-bit divider, so the largest divisor is 32) lands on 31.25 MHz. +The panel then refreshes at 50.9 Hz, not the 48.87 Hz of the 30 MHz mode. U-Boot +retunes CPLL to 960 MHz, which breaks the 50 MHz GMAC reference. Linux therefore +runs CPLL at 900 MHz and pins the three clocks whose exact rates depend on it: +30 MHz (/30), 50 MHz (/18) and 100 MHz (/9). + +The check reads the series the kernel build applies, not a built tree: + +- patches/10-clk-rv1106.patch: drivers/clk/rockchip/clk-rv1106.c (a new file): + the CPLL rate table, the PLL entries, the composite clocks and their parents. +- patches/20-arch-dts-bindings.patch: arch/arm/boot/dts/rockchip/rv1106.dtsi (the + cru node) and include/dt-bindings/clock/rv1106-cru.h (the clock IDs). + +Checks: +1. Each new-file hunk header in those two patches matches its added line count + (a wrong count makes `git apply` refuse the series). +2. The cru node's assigned-clocks and assigned-clock-rates have equal length. +3. PLL_CPLL is assigned 900000000 Hz, and CLK_50M_SRC (50 MHz), CLK_100M_SRC + (100 MHz) and DCLK_VOP_SRC (30 MHz) follow it at once, in that order. +4. 900000000 Hz is in the CPLL rate table, and cpll uses that table. +5. Every clock ID the cru node assigns is defined in the CRU header. +6. Every pinned clock is a composite with cpll among its parents, and its divider + reaches the rate exactly: cpll / d == rate for an integer d in 1..2**width. + +Usage: check-vop-dclk.py +Exit 0 when every check holds, 1 with one FAIL line per problem, 2 on bad usage. +""" +import pathlib +import re +import sys + +PATCH_CLK = "10-clk-rv1106.patch" +PATCH_DTS = "20-arch-dts-bindings.patch" +CLK_PATH = "drivers/clk/rockchip/clk-rv1106.c" +DTSI_PATH = "arch/arm/boot/dts/rockchip/rv1106.dtsi" +HDR_PATH = "include/dt-bindings/clock/rv1106-cru.h" + +CPLL_RATE = 900_000_000 +# The CRU node must list these right after PLL_CPLL, in this order. +PINS = [ + ("CLK_50M_SRC", 50_000_000), + ("CLK_100M_SRC", 100_000_000), + ("DCLK_VOP_SRC", 30_000_000), +] + + +def new_files(patch_text): + """Map each new-file path to (declared hunk line count, added lines).""" + files = {} + for section in re.split(r"(?m)^(?=diff --git )", patch_text): + head = re.match(r"diff --git a/(\S+) b/", section) + if not head or "\n--- /dev/null\n" not in section: + continue + lines = section.split("\n") + hunks = [l for l in lines if l.startswith("@@")] + declared = None + if hunks: + m = re.match(r"@@ -0,0 \+1,(\d+) @@", hunks[0]) + if m: + declared = int(m.group(1)) + body = [l[1:] for l in lines if l.startswith("+") and not l.startswith("+++ ")] + files[head.group(1)] = (declared, body) + return files + + +def strip_c_comments(text): + text = re.sub(r"/\*.*?\*/", "", text, flags=re.S) + return re.sub(r"//[^\n]*", "", text) + + +def macro_args(text, open_idx): + """Split the arguments of the macro call whose '(' is at open_idx.""" + depth, args, cur = 0, [], [] + for ch in text[open_idx:]: + if ch == "(": + depth += 1 + if depth == 1: + continue + elif ch == ")": + depth -= 1 + if depth == 0: + args.append("".join(cur).strip()) + return args + elif ch == "," and depth == 1: + args.append("".join(cur).strip()) + cur = [] + continue + cur.append(ch) + raise ValueError("unbalanced macro call") + + +def cru_body(dtsi): + m = re.search(r"cru:\s*clock-controller@ff3a0000\s*\{", dtsi) + if not m: + return None + depth, i = 1, m.end() + while depth and i < len(dtsi): + depth += {"{": 1, "}": -1}.get(dtsi[i], 0) + i += 1 + return dtsi[m.end():i - 1] + + +def check(patches_dir): + problems = [] + root = pathlib.Path(patches_dir) + try: + clk_files = new_files((root / PATCH_CLK).read_text(encoding="utf-8")) + dts_files = new_files((root / PATCH_DTS).read_text(encoding="utf-8")) + except OSError as err: + return [f"cannot read the patch series: {err}"] + + for path, files in ((CLK_PATH, clk_files), (DTSI_PATH, dts_files), (HDR_PATH, dts_files)): + if path not in files: + problems.append(f"{path}: no new-file section in the patch series") + continue + declared, body = files[path] + if declared != len(body): + problems.append(f"{path}: hunk header declares {declared} lines, " + f"the patch adds {len(body)}") + if CLK_PATH not in clk_files or DTSI_PATH not in dts_files or HDR_PATH not in dts_files: + return problems + + clk = "\n".join(clk_files[CLK_PATH][1]) + dtsi = strip_c_comments("\n".join(dts_files[DTSI_PATH][1])) + hdr = "\n".join(dts_files[HDR_PATH][1]) + + # Checks 2 and 3: the cru node assigns the PLL and the pins in order. + body = cru_body(dtsi) + if body is None: + return problems + [f"{DTSI_PATH}: no cru node (clock-controller@ff3a0000)"] + m_clk = re.search(r"assigned-clocks\s*=\s*([^;]*);", body) + m_rate = re.search(r"assigned-clock-rates\s*=\s*([^;]*);", body) + if not m_clk or not m_rate: + return problems + ["cru node has no assigned-clocks / assigned-clock-rates"] + clk_text, rate_text = m_clk.group(1), m_rate.group(1) + if any(not re.fullmatch(r"<&cru\s+\w+>", t) for t in re.findall(r"<[^>]*>", clk_text)): + problems.append("cru assigned-clocks has an entry that is not <&cru NAME>") + if any(not re.fullmatch(r"<\d+>", t) for t in re.findall(r"<[^>]*>", rate_text)): + problems.append("cru assigned-clock-rates has an entry that is not ") + clocks = re.findall(r"<&cru\s+(\w+)>", clk_text) + rates = [int(x) for x in re.findall(r"<(\d+)>", rate_text)] + if len(clocks) != len(rates): + problems.append(f"cru assigned-clocks has {len(clocks)} entries, " + f"assigned-clock-rates has {len(rates)}") + return problems + + assigned = dict(zip(clocks, rates)) + if "PLL_CPLL" not in assigned: + problems.append("cru node does not assign PLL_CPLL") + return problems + i = clocks.index("PLL_CPLL") + cpll_rate = rates[i] + if cpll_rate != CPLL_RATE: + problems.append(f"PLL_CPLL is assigned {cpll_rate} Hz, expected {CPLL_RATE} Hz") + expect_next = [name for name, _ in PINS] + if clocks[i + 1:i + 1 + len(PINS)] != expect_next: + problems.append(f"after PLL_CPLL the cru node must assign {', '.join(expect_next)}, " + f"found {', '.join(clocks[i + 1:i + 1 + len(PINS)]) or 'nothing'}") + for pos, (name, rate) in enumerate(PINS, start=1): + if i + pos < len(clocks) and clocks[i + pos] == name and rates[i + pos] != rate: + problems.append(f"{name} is assigned {rates[i + pos]} Hz, expected {rate} Hz") + + # Check 5: the IDs exist in the CRU header. + ids = dict(re.findall(r"#define\s+(\w+)\s+(\d+)", hdr)) + for name in ["PLL_CPLL"] + [n for n, _ in PINS]: + if name not in ids: + problems.append(f"{name} is not defined in {HDR_PATH}") + + # Check 4: CPLL can be programmed to 900 MHz. + m_tab = re.search(r"rv1106_pll_rates\[\]\s*=\s*\{(.*?)\n\};", clk, flags=re.S) + table = [int(x) for x in re.findall(r"RK3036_PLL_RATE\(\s*(\d+)", m_tab.group(1))] if m_tab else [] + if CPLL_RATE not in table: + problems.append(f"{CLK_PATH}: rv1106_pll_rates has no {CPLL_RATE} Hz entry") + cpll_macro = None + for m in re.finditer(r"\bPLL\(", clk): + args = macro_args(clk, m.end() - 1) + if len(args) > 1 and args[1] == "PLL_CPLL": + cpll_macro = args + if cpll_macro is None or "rv1106_pll_rates" not in cpll_macro: + problems.append(f"{CLK_PATH}: the PLL_CPLL entry does not use rv1106_pll_rates") + + # Check 6: each pinned clock is reachable exactly from cpll. + pnames = {} + for m in re.finditer(r"PNAME\((\w+)\)\s*=\s*\{([^}]*)\}", clk): + pnames[m.group(1)] = re.findall(r'"([^"]+)"', m.group(2)) + composites = {} + for m in re.finditer(r"\bCOMPOSITE\(", clk): + args = macro_args(clk, m.end() - 1) + if len(args) >= 14: + composites[args[0]] = args + for name, rate in PINS: + args = composites.get(name) + if args is None: + problems.append(f"{name}: no COMPOSITE definition in {CLK_PATH}") + continue + parents = pnames.get(args[2], []) + if "cpll" not in parents: + problems.append(f"{name}: parents {parents} do not include cpll") + continue + width = int(args[9]) + exact = [d for d in range(1, 2 ** width + 1) + if cpll_rate % d == 0 and cpll_rate // d == rate] + if not exact: + problems.append(f"{name}: cpll {cpll_rate} Hz cannot reach {rate} Hz with a " + f"{width}-bit divider") + return problems + + +def main(argv): + if len(argv) != 2: + print("usage: check-vop-dclk.py ", file=sys.stderr) + return 2 + problems = check(argv[1]) + for p in problems: + print(f"FAIL {p}") + if problems: + return 1 + print("PASS vop-dclk clock plan (bfe-core1106-sdk#43)") + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/tests/vop-dclk/run-check-vop-dclk-tests.sh b/tests/vop-dclk/run-check-vop-dclk-tests.sh new file mode 100755 index 0000000..a4c050f --- /dev/null +++ b/tests/vop-dclk/run-check-vop-dclk-tests.sh @@ -0,0 +1,70 @@ +#!/bin/bash +# Regression tests for tests/vop-dclk/check-vop-dclk.py (bfe-core1106-sdk#43). +# +# The checker guards the Linux VOP pixel clock: CPLL at 900 MHz with the 30 MHz, +# 50 MHz and 100 MHz pins in the CRU node (the 1000 MHz default gave 31.25 MHz). +# Each case copies the real series, breaks one thing, and expects the checker's +# exit status. The unmodified series must pass; the real patched tree is checked +# by the sprint firmware build, which applies the series to the kernel. +set -uo pipefail + +HERE="$(cd "$(dirname "$0")" && pwd)" +CHECK="$HERE/check-vop-dclk.py" +PATCHES="$HERE/../../patches" +TMP="$(mktemp -d)" +trap 'rm -rf "$TMP"' EXIT + +fail=0 +total=0 + +# case +# An empty old text means no mutation. The old text must occur exactly once. +case_run() { + local name="$1" want="$2" patch="$3" old="$4" new="$5" + local dir="$TMP/$name" + mkdir -p "$dir" + cp "$PATCHES/10-clk-rv1106.patch" "$PATCHES/20-arch-dts-bindings.patch" "$dir/" + if [ -n "$old" ]; then + python3 - "$dir/$patch" "$old" "$new" <<'PY' || { echo "FAIL $name: mutation did not apply"; fail=$((fail + 1)); total=$((total + 1)); return; } +import sys +path, old, new = sys.argv[1], sys.argv[2], sys.argv[3] +text = open(path, encoding="utf-8").read() +if text.count(old) != 1: + sys.exit(1) +open(path, "w", encoding="utf-8").write(text.replace(old, new)) +PY + fi + local out rc + out="$(python3 "$CHECK" "$dir" 2>&1)" + rc=$? + total=$((total + 1)) + if [ "$rc" -eq "$want" ]; then + echo "ok $name (rc $rc): $(echo "$out" | head -n 1)" + else + echo "FAIL $name: want rc $want, got $rc" + echo "$out" | sed 's/^/ /' + fail=$((fail + 1)) + fi +} + +T=$'\t' +DTS=20-arch-dts-bindings.patch +CLK=10-clk-rv1106.patch + +case_run real-series 0 "$DTS" "" "" +case_run cpll-1000-regress 1 "$DTS" $'+\t\t\t<1188000000>, <900000000>,\n' $'+\t\t\t<1188000000>, <1000000000>,\n' +case_run pin-missing 1 "$DTS" "<&cru CLK_50M_SRC>, <&cru CLK_100M_SRC>, <&cru DCLK_VOP_SRC>," "<&cru CLK_50M_SRC>, <&cru CLK_100M_SRC>, <&cru ARMCLK>," +case_run pins-reordered 1 "$DTS" "<&cru CLK_50M_SRC>, <&cru CLK_100M_SRC>, <&cru DCLK_VOP_SRC>," "<&cru DCLK_VOP_SRC>, <&cru CLK_100M_SRC>, <&cru CLK_50M_SRC>," +case_run pin-rate-wrong 1 "$DTS" "<50000000>, <100000000>, <30000000>," "<50000000>, <100000000>, <31250000>," +case_run list-length-differs 1 "$DTS" "<50000000>, <100000000>, <30000000>," "<50000000>, <100000000>," +case_run hunk-count-wrong 1 "$DTS" "@@ -0,0 +1,1557 @@" "@@ -0,0 +1,1556 @@" +case_run table-lacks-900 1 "$CLK" "RK3036_PLL_RATE(900000000, 1, 75, 2, 1, 1, 0)," "RK3036_PLL_RATE(901000000, 1, 75, 2, 1, 1, 0)," +case_run divider-too-narrow 1 "$CLK" "3, 5, DFLAGS," "3, 4, DFLAGS," +case_run cpll-not-table-user 1 "$CLK" "RV1106_MODE_CON, 2, 10, 0, rv1106_pll_rates)" "RV1106_MODE_CON, 2, 10, 0, rv1106_pll_rate)" +case_run id-undefined 1 "$DTS" "#define DCLK_VOP_SRC 201" "#define DCLK_VOP_SRX 201" + +echo "$((total - fail))/$total cases as expected" +if [ "$fail" -ne 0 ]; then + exit 1 +fi +exit 0