diff --git a/ports/candidate/linux-postmarketos-qcom-sdm660/0015-clk-qcom-sdm660-cpu-osm-driver.patch b/ports/candidate/linux-postmarketos-qcom-sdm660/0015-clk-qcom-sdm660-cpu-osm-driver.patch index 199ee03..4f3209f 100644 --- a/ports/candidate/linux-postmarketos-qcom-sdm660/0015-clk-qcom-sdm660-cpu-osm-driver.patch +++ b/ports/candidate/linux-postmarketos-qcom-sdm660/0015-clk-qcom-sdm660-cpu-osm-driver.patch @@ -1,874 +1,312 @@ -From: pmOS Mi8Lite -Subject: [PATCH] clk: qcom: add SDM660 CPU (OSM) clock driver +From: Platina Linux contributors +Subject: [PATCH] clk: qcom: drive SDM660 APCS Agera HFPLLs directly for CPU DVFS -CPU frequency scaling driver for SDM660. Programs the OSM (LUT, cycle -counters, boost/DCVS FSMs, PLL/RCGR config, APM, ITM handoff, sequencer -microcode, mem-acc/APM voltage, cluster PLLs) then enables it; cpufreq-dt -scales via the cpu OPP tables. The cluster PLL reconfig parks the mux on -GPLL0 first to avoid hanging the (little) cluster that runs from it. +On SDM660 the OSM <0x1000 sequencer microcode region is XPU-locked, leaving +the OSM FSM inert while bootloaders park the Gold cluster on GPLL0 (600 MHz). +Switch clk-cpu-sdm660.c to program each cluster's APCS Agera HFPLL and +glitch-free RCG2 mux directly while keeping the safe NOM voltage rail. -Signed-off-by: pmOS Mi8Lite --- diff --git a/drivers/clk/qcom/clk-cpu-sdm660.c b/drivers/clk/qcom/clk-cpu-sdm660.c new file mode 100644 index 0000000..d89c18c --- /dev/null +++ b/drivers/clk/qcom/clk-cpu-sdm660.c -@@ -0,0 +1,855 @@ +@@ -0,0 +1,295 @@ +// SPDX-License-Identifier: GPL-2.0 +/* -+ * SDM660 CPU (OSM) clock driver for mainline Linux. ++ * SDM660 direct APCS Agera HFPLL + RCG2 CPU clock driver. + * -+ * The SDM660 scales its two Kryo-260 CPU clusters with the OSM (Operating -+ * State Manager) hardware, like the SDM845 - but unlike the SDM845 the -+ * SDM660 bootloader does NOT program the OSM: it leaves ENABLE=0 and the -+ * frequency/voltage LUT empty and runs the cores from a fixed bypass source -+ * (this is why mainline has no cpufreq on SDM660). On Android the secure -+ * world (TrustZone) programs the OSM sequencer/config; on mainline there is -+ * no TZ doing that, so this driver does the full "secure_init" setup itself: -+ * -+ * per cluster: program the freq/voltage LUT from the DT speedbin tables, -+ * program the PLL/RCGR config words and the APM voltage switch; -+ * once: program the ITM handoff and load the OSM sequencer microcode; -+ * then: enable the OSM. From then on cpufreq only writes a LUT index -+ * into DCVS_PERF_STATE_DESIRED and the OSM drives PLL, mux and -+ * voltage on its own. -+ * -+ * No ACD, LLM, trace or FSM optimisations (all optional on SDM660), open-loop -+ * voltages (no CPR3). Reverse-engineered and reduced from the LineageOS -+ * clk-cpu-osm-660 driver. -+ * -+ * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. ++ * The SDM660 OSM <0x1000 sequencer microcode region is XPU-protected from ++ * non-secure world on mainline boot, leaving the OSM FSM inert while the ++ * bootloader parks Gold on GPLL0 (600 MHz). Each cluster's Agera HFPLL and ++ * glitch-free RCGR mux are accessible from Linux, so park on GPLL0, update ++ * PLL_L_VAL, wait for lock, and switch back to PLL_EARLY. + */ + -+#include +#include +#include +#include -+#include +#include ++#include +#include +#include +#include +#include ++#include + -+/* OSM register offsets (relative to each cluster's OSM base) */ -+#define ENABLE_REG 0x1004 -+#define SPM_CC_CTRL 0x1028 -+#define SEQ_MEM_ADDR 0x500 -+#define SEQ_CFG_BR_ADDR 0x170 -+#define INDEX_REG 0x1150 -+#define FREQ_REG 0x1154 -+#define VOLT_REG 0x1158 -+#define OVERRIDE_REG 0x115c -+#define SPARE_REG 0x1164 -+#define OSM_REG_SIZE 32 -+#define OSM_TABLE_SIZE 40 -+#define DCVS_PERF_STATE_DESIRED_REG 0x1f10 -+#define SEQ_REG(n) (0x300 + (n) * 4) ++#define RCGR_CMD 0x00 ++#define RCGR_CFG 0x04 ++#define RCGR_CMD_UPDATE BIT(0) ++#define RCGR_SRC_SHIFT 8 ++#define RCGR_SRC_MASK 0x7 ++#define RCGR_DIV_MASK 0x1f ++#define RCGR_SRC_PLL_EARLY 5 ++#define RCGR_CFG_GPLL0_DIV1 0x401 ++#define RCGR_CFG_GPLL0_DIV2 0x403 ++#define RCGR_CFG_PLL_DIV1 0x501 ++#define RCGR_CFG_PLL_DIV2 0x503 + -+/* Voltage word: bits[21:16] = virtual corner, bits[11:0] = open-loop mV */ -+#define OSM_VC_MASK GENMASK(21, 16) -+#define OSM_OLV_MASK GENMASK(11, 0) ++#define PLL_L_VAL 0x04 ++#define PLL_ALPHA_VAL 0x08 ++#define PLL_STATUS 0x2c ++#define PLL_LOCK_DET BIT(16) + -+/* PLL / RCGR config words programmed into the OSM sequencer registers */ -+#define PLL_MIN_LVAL 43 -+#define PLL_POST_DIV1 0x1f -+#define PLL_POST_DIV2 0x11f -+#define GPLL_SEL 0x400 -+#define PLL_EARLY_SEL 0x500 -+#define PLL_MAIN_SEL 0x300 -+#define RCG_UPDATE 0x3 -+#define RCG_UPDATE_SUCCESS 0x2 -+ -+/* APM (Array Power Mux) switch words */ -+#define APM_MX_MODE 0 -+#define APM_APC_MODE BIT(1) -+#define APM_MODE_SWITCH_MASK (FIELD_PREP(GENMASK(4, 2), 7) | \ -+ FIELD_PREP(GENMASK(1, 0), 3)) -+#define APM_MX_MODE_VAL 0 -+#define APM_APC_MODE_VAL 0x3 -+ -+/* ITM handoff */ -+#define ITM_CL0_DISABLE_CL1_ENABLED 0x2 -+#define ITM_CL0_ENABLED_CL1_DISABLE 0x1 -+ -+/* Cluster PLL (relative to each cluster's PLL base) */ -+#define PLL_MODE 0x0 -+#define PLL_L_VAL 0x4 -+#define PLL_USER_CTRL 0xc -+#define PLL_CONFIG_CTL_LO 0x10 -+#define PLL_WAIT_LOCK_TIME_US 10 -+#define PLL_SETUP_L_VAL 0x20 -+#define PLL_SETUP_USER_CTRL 0x01000008 -+#define PLL_SETUP_CONFIG_CTL 0x20004aa8 -+ -+/* DCVS boost/safe FSMs (relative to OSM base) */ -+#define PDN_FSM_CTRL_REG 0x1070 -+#define CC_BOOST_EN_MASK BIT(0) -+#define CC_BOOST_TIMER_REG0 0x1074 -+#define CC_BOOST_TIMER_REG1 0x1078 -+#define CC_BOOST_TIMER_REG2 0x107c -+#define DCVS_BOOST_EN_MASK BIT(2) -+#define DCVS_BOOST_TIMER_REG0 0x1084 -+#define DCVS_BOOST_TIMER_REG1 0x1088 -+#define DCVS_BOOST_TIMER_REG2 0x108c -+#define PS_BOOST_EN_MASK BIT(1) -+#define PS_BOOST_TIMER_REG0 0x1094 -+#define PS_BOOST_TIMER_REG1 0x1098 -+#define PS_BOOST_TIMER_REG2 0x109c -+#define BOOST_PROG_SYNC_DELAY_REG 0x10a0 -+#define OSM_PLL_SW_OVERRIDE_EN 0x10c0 -+#define PLL_SW_OVERRIDE_DROOP_EN BIT(0) -+#define PLL_WAIT_LOCK_TIME_NS 10000 -+#define SAFE_FREQ_WAIT_NS 5000 -+#define DEXT_DECREMENT_WAIT_NS 1000 -+ -+/* Cycle counter */ -+#define OSM_CYCLE_COUNTER_CTRL_REG 0x1f00 -+#define OSM_CYCLE_COUNTER_USE_XO_EDGE BIT(8) -+ -+/* clocks (Hz) */ -+#define OSM_CLK_RATE 200000000 -+#define XO_CLK_RATE 19200000 -+ -+#define OSM_CORE_TABLE_SIZE 0x2000 ++#define XO_CLK_RATE 19200000UL ++#define GPLL0_CLK_RATE 600000000UL +#define NUM_CLUSTERS 2 ++#define MAX_CLUSTER_RATES 6 + -+/* nsec -> OSM clock cycles (osm_clk_rate * nsec / 1e9) */ -+static inline u32 osm_count_ns(u64 nsec) -+{ -+ u64 t = (u64)OSM_CLK_RATE * nsec; -+ -+ do_div(t, 1000000000); -+ return t; -+} -+ -+/* -+ * OSM sequencer microcode. This is the "program" the OSM executes to perform -+ * the PLL/mux/voltage transitions; without it ENABLE=1 does nothing. Verbatim -+ * from the downstream clk-cpu-osm-660 (identical for pwrcl and perfcl). -+ */ -+static const u32 seq_instr[] = { -+ 0xc2005000, 0x2c9e3b21, 0xc0ab2cdc, 0xc2882525, 0x359dc491, 0x700a500b, -+ 0x5001aefc, 0xaefd7000, 0x390938c8, 0xcb44c833, 0xce56cd54, 0x341336e0, -+ 0xa4baadba, 0xb480a493, 0x10004000, 0x70005001, 0x1000500c, 0xc792c5a1, -+ 0x501625e1, 0x3da335a2, 0x50170006, 0x50150006, 0x1000c633, 0x1000acb3, -+ 0xc422acb4, 0xaefc1000, 0x700a500b, 0x70005001, 0x5010aefd, 0x5012700b, -+ 0xad41700c, 0x84e5adb9, 0xb3808566, 0x239b0003, 0x856484e3, 0xb9800007, -+ 0x2bad0003, 0xac3aa20b, 0x0003181b, 0x0003bb40, 0xa30d239b, 0x500c181b, -+ 0x5011500f, 0x181b3413, 0x853984b9, 0x0003bd80, 0xa0012ba4, 0x72050803, -+ 0x500e1000, 0x500c1000, 0x1c011c0a, 0x3b181c06, 0x1c073b43, 0x1c061000, -+ 0x1c073983, 0x1c02500c, 0x10001c0a, 0x70015002, 0x81031000, 0x70025003, -+ 0x70035004, 0x3b441000, 0x81553985, 0x70025003, 0x50054003, 0xa1467009, -+ 0x0003b1c0, 0x4005238b, 0x835a1000, 0x855c84db, 0x1000a51f, 0x84de835d, -+ 0xa52c855c, 0x50061000, 0x39cd3a4c, 0x3ad03a8f, 0x10004006, 0x70065007, -+ 0xa00f2c12, 0x08034007, 0xaefc7205, 0xaefd700d, 0xa9641000, 0x40071c1a, -+ 0x700daefc, 0x1000aefd, 0x70065007, 0x50101c16, 0x40075012, 0x700daefc, -+ 0x2411aefd, 0xa8211000, 0x0803a00f, 0x500c7005, 0x1c1591e0, 0x500f5014, -+ 0x10005011, 0x500c2bd4, 0x0803a00f, 0x10007205, 0xa00fa9d1, 0x0803a821, -+ 0xa9d07005, 0x91e0500c, 0x500f1c15, 0x10005011, 0x1c162bce, 0x50125010, -+ 0xa022a82a, 0x70050803, 0x1c1591df, 0x5011500f, 0x5014500c, 0x0803a00f, -+ 0x10007205, 0x501391a4, 0x22172217, 0x70075008, 0xa9634008, 0x1c1a0006, -+ 0x70085009, 0x10004009, 0x00008ed9, 0x3e05c8dd, 0x1c033604, 0xabaf1000, -+ 0x856284e1, 0x0003bb80, 0x1000239f, 0x0803a037, 0x10007205, 0x8dc61000, -+ 0x38a71c2a, 0x1c2a8dc4, 0x100038a6, 0x1c2a8dc5, 0x8dc73867, 0x38681c2a, -+ 0x8c491000, 0x8d4b8cca, 0x10001c00, 0x8ccd8c4c, 0x1c008d4e, 0x8c4f1000, -+ 0x8d518cd0, 0x10001c00, 0xa759a79a, 0x1000a718, 0xbf80af9b, 0x00001000, -+}; -+ -+static const u32 seq_br_instr[] = { -+ 0x00000248, 0x0000020e, 0x0000021c, 0x000000f6, 0x00000112, 0x0000011c, -+ 0x000000e4, 0x000000ea, 0x000000c6, 0x000000d6, 0x00000126, 0x00000108, -+ 0x00000184, 0x000001a8, 0x000001b0, 0x00000134, 0x00000158, 0x0000016e, -+ 0x0000014a, 0x000000c2, 0x00000190, 0x000001d2, 0x000001cc, 0x000001d4, -+ 0x000001e8, 0x00000000, 0x000001f6, 0x00000032, 0x00000066, 0x000000b0, -+ 0x000000a6, 0x000001fc, 0x0000003c, 0x00000044, 0x0000005c, 0x00000060, -+ 0x00000204, 0x00000030, 0x0000022a, 0x00000234, 0x0000023e, 0x00000000, -+ 0x00000250, 0x00000000, 0x00000000, 0x0000009a, 0x0000020c, -+}; -+ -+struct osm_entry { -+ u32 freq_data; -+ u32 override_data; -+ u32 spare_data; -+ u32 virtual_corner; /* 0-based */ -+ u32 open_loop_volt; /* mV */ -+ unsigned long frequency; -+}; -+ -+struct sdm660_osm_clk { ++struct sdm660_apcs_clk { + struct clk_hw hw; -+ void __iomem *base; /* this cluster's OSM base (read) */ -+ phys_addr_t phys; /* this cluster's OSM base (phys, for SCM) */ -+ void __iomem *pll_base; /* this cluster's HFPLL base */ -+ void __iomem *rcgr_base; /* this cluster's APCS RCGR mux */ -+ /* per-cluster config addresses (physical values fed to the OSM) */ -+ u32 l_val_base; -+ u32 apcs_pll_user_ctl; -+ u32 apcs_cfg_rcgr; -+ u32 apcs_cmd_rcgr; -+ u32 apcs_itm_present; -+ u32 apm_mode_ctl; -+ u32 apm_ctrl_status; -+ u32 itm_handoff; /* ITM_CL0_* value for this cluster */ -+ struct osm_entry table[OSM_TABLE_SIZE]; -+ int num_entries; ++ void __iomem *pll_base; ++ void __iomem *rcgr_base; ++ const unsigned long *rates; ++ int num_rates; ++ spinlock_t lock; +}; + -+#define to_osm_clk(_hw) container_of(_hw, struct sdm660_osm_clk, hw) ++#define to_apcs_clk(_hw) container_of(_hw, struct sdm660_apcs_clk, hw) + -+/* -+ * The OSM register block has two write-protection domains on this SoC, -+ * confirmed live via /dev/mem: control/LUT registers at offset >= 0x1000 -+ * (ENABLE, the freq/volt LUT, the boost/DCVS FSMs, DCVS_PERF_STATE_DESIRED) -+ * accept ordinary non-secure writes, but the whole sequencer region below -+ * that - the branch table at 0x170 and the SEQ_MEM microcode / SEQ_REG -+ * parameter area at 0x300-0x5ff - is XPU-protected: a plain writel from the -+ * kernel is silently dropped, leaving the sequencer empty so the OSM enables -+ * but never transitions. Those writes must go through the secure world, which -+ * is exactly what the downstream driver does (scm_io_write) - the TrustZone on -+ * this device implements SCM_IO_WRITE (the OSM is programmed this way under -+ * Android). So route < 0x1000 through qcom_scm_io_writel and the rest direct. -+ */ -+#define OSM_SECURE_LIMIT 0x1000 ++struct cluster_desc { ++ const char *name; ++ u32 pll_phys; ++ u32 rcgr_phys; ++ const unsigned long rates[MAX_CLUSTER_RATES]; ++ int num_rates; ++ unsigned long init_rate; ++}; + -+static void osm_w(struct sdm660_osm_clk *c, u32 val, u32 off) -+{ -+ /* -+ * r54 diagnostic: previously off < OSM_SECURE_LIMIT was routed through -+ * qcom_scm_io_writel, which is a silent no-op on this SoC (SCM readback -+ * returned 0). Write everything directly to test whether a plain kernel -+ * writel to the sequencer region actually lands (checked via SCM -+ * readback in probe: reg21 should read back PLL_MIN_LVAL if it does). -+ */ -+ writel_relaxed(val, c->base + off); -+} ++static const struct cluster_desc cluster_descs[NUM_CLUSTERS] = { ++ { ++ .name = "pwrcl_clk", ++ .pll_phys = 0x17916000, ++ .rcgr_phys = 0x17911050, ++ .rates = { ++ 300000000UL, ++ 633600000UL, ++ 902400000UL, ++ 1113600000UL, ++ 1401600000UL, ++ }, ++ .num_rates = 5, ++ .init_rate = 1401600000UL, ++ }, ++ { ++ .name = "perfcl_clk", ++ .pll_phys = 0x17816000, ++ .rcgr_phys = 0x17811050, ++ .rates = { ++ 300000000UL, ++ 1113600000UL, ++ 1401600000UL, ++ 1747200000UL, ++ }, ++ .num_rates = 4, ++ .init_rate = 1747200000UL, ++ }, ++}; + -+static inline u32 osm_r(struct sdm660_osm_clk *c, u32 off) -+{ -+ return readl_relaxed(c->base + off); -+} -+ -+/* --- clk_ops: with the OSM running, set_rate is a single index vote --- */ -+ -+static int osm_search(struct sdm660_osm_clk *c, unsigned long rate) ++static int rcgr_update_locked(void __iomem *rcgr) +{ ++ u32 cmd = readl_relaxed(rcgr + RCGR_CMD); + int i; + -+ for (i = 0; i < c->num_entries; i++) -+ if (c->table[i].frequency == rate) -+ return i; -+ return -EINVAL; ++ writel_relaxed(cmd | RCGR_CMD_UPDATE, rcgr + RCGR_CMD); ++ wmb(); ++ for (i = 0; i < 500; i++) { ++ if (!(readl_relaxed(rcgr + RCGR_CMD) & RCGR_CMD_UPDATE)) ++ return 0; ++ udelay(1); ++ } ++ return -ETIMEDOUT; +} + -+static int sdm660_osm_set_rate(struct clk_hw *hw, unsigned long rate, -+ unsigned long parent_rate) ++static int apcs_set_rate_locked(struct sdm660_apcs_clk *c, unsigned long rate) +{ -+ struct sdm660_osm_clk *c = to_osm_clk(hw); -+ int index = osm_search(c, rate); ++ u32 l_val, cfg; ++ int i, ret; + -+ if (index < 0) -+ return index; -+ osm_w(c, index, DCVS_PERF_STATE_DESIRED_REG); -+ wmb(); /* ensure the vote reaches the OSM */ -+ return 0; ++ if (rate <= 300000000UL) { ++ writel_relaxed(RCGR_CFG_GPLL0_DIV2, c->rcgr_base + RCGR_CFG); ++ return rcgr_update_locked(c->rcgr_base); ++ } ++ ++ /* Park glitch-free RCGR on GPLL0 (600 MHz) before changing PLL_L_VAL */ ++ writel_relaxed(RCGR_CFG_GPLL0_DIV1, c->rcgr_base + RCGR_CFG); ++ ret = rcgr_update_locked(c->rcgr_base); ++ if (ret) ++ return ret; ++ ++ if (rate <= 633600000UL) { ++ l_val = 66; ++ cfg = RCGR_CFG_PLL_DIV2; ++ } else { ++ l_val = DIV_ROUND_CLOSEST(rate, XO_CLK_RATE); ++ cfg = RCGR_CFG_PLL_DIV1; ++ } ++ ++ writel_relaxed(l_val, c->pll_base + PLL_L_VAL); ++ writel_relaxed(0, c->pll_base + PLL_ALPHA_VAL); ++ wmb(); ++ udelay(2); ++ ++ for (i = 0; i < 500; i++) { ++ if (readl_relaxed(c->pll_base + PLL_STATUS) & PLL_LOCK_DET) ++ break; ++ udelay(1); ++ } ++ if (i == 500) ++ return -ETIMEDOUT; ++ ++ writel_relaxed(cfg, c->rcgr_base + RCGR_CFG); ++ return rcgr_update_locked(c->rcgr_base); +} + -+static unsigned long sdm660_osm_recalc_rate(struct clk_hw *hw, -+ unsigned long parent_rate) ++static int sdm660_apcs_set_rate(struct clk_hw *hw, unsigned long rate, ++ unsigned long parent_rate) +{ -+ struct sdm660_osm_clk *c = to_osm_clk(hw); -+ u32 index = osm_r(c, DCVS_PERF_STATE_DESIRED_REG); ++ struct sdm660_apcs_clk *c = to_apcs_clk(hw); ++ unsigned long flags; ++ int ret; + -+ if (index >= c->num_entries) -+ index = c->num_entries - 1; -+ return c->table[index].frequency; ++ spin_lock_irqsave(&c->lock, flags); ++ ret = apcs_set_rate_locked(c, rate); ++ spin_unlock_irqrestore(&c->lock, flags); ++ return ret; +} + -+static int sdm660_osm_determine_rate(struct clk_hw *hw, -+ struct clk_rate_request *req) ++static unsigned long sdm660_apcs_recalc_rate(struct clk_hw *hw, ++ unsigned long parent_rate) +{ -+ struct sdm660_osm_clk *c = to_osm_clk(hw); -+ unsigned long best = c->table[0].frequency; ++ struct sdm660_apcs_clk *c = to_apcs_clk(hw); ++ u32 cfg = readl_relaxed(c->rcgr_base + RCGR_CFG); ++ u32 src = (cfg >> RCGR_SRC_SHIFT) & RCGR_SRC_MASK; ++ u32 div_sel = cfg & RCGR_DIV_MASK; ++ unsigned long base_rate; ++ ++ if (src == RCGR_SRC_PLL_EARLY) ++ base_rate = (unsigned long)(readl_relaxed(c->pll_base + PLL_L_VAL) & 0xff) * XO_CLK_RATE; ++ else ++ base_rate = GPLL0_CLK_RATE; ++ ++ if (div_sel <= 1) ++ return base_rate; ++ return (base_rate * 2UL) / (div_sel + 1); ++} ++ ++static int sdm660_apcs_determine_rate(struct clk_hw *hw, ++ struct clk_rate_request *req) ++{ ++ struct sdm660_apcs_clk *c = to_apcs_clk(hw); ++ unsigned long best = c->rates[0]; + int i; + -+ for (i = 0; i < c->num_entries; i++) -+ if (c->table[i].frequency <= req->rate && -+ c->table[i].frequency > best) -+ best = c->table[i].frequency; ++ for (i = 0; i < c->num_rates; i++) { ++ if (c->rates[i] <= req->rate) ++ best = c->rates[i]; ++ } + req->rate = best; + return 0; +} + -+static const struct clk_ops sdm660_osm_ops = { -+ .set_rate = sdm660_osm_set_rate, -+ .recalc_rate = sdm660_osm_recalc_rate, -+ .determine_rate = sdm660_osm_determine_rate, ++static const struct clk_ops sdm660_apcs_ops = { ++ .set_rate = sdm660_apcs_set_rate, ++ .recalc_rate = sdm660_apcs_recalc_rate, ++ .determine_rate = sdm660_apcs_determine_rate, +}; + -+/* --- one-time OSM programming --- */ ++static struct clk_hw *apcs_hws[NUM_CLUSTERS]; + -+static void osm_setup_lut(struct sdm660_osm_clk *c) -+{ -+ int i; -+ -+ for (i = 0; i < OSM_TABLE_SIZE; i++) { -+ u32 freq = 0, volt = 0, override = 0, spare = 0; -+ u32 off = i * OSM_REG_SIZE; -+ -+ if (i < c->num_entries) { -+ freq = c->table[i].freq_data; -+ volt = FIELD_PREP(OSM_VC_MASK, c->table[i].virtual_corner) | -+ FIELD_PREP(OSM_OLV_MASK, c->table[i].open_loop_volt); -+ override = c->table[i].override_data; -+ spare = c->table[i].spare_data; -+ } -+ osm_w(c, i, INDEX_REG + off); -+ osm_w(c, freq, FREQ_REG + off); -+ osm_w(c, volt, VOLT_REG + off); -+ osm_w(c, override, OVERRIDE_REG + off); -+ osm_w(c, spare, SPARE_REG + off); -+ } -+ wmb(); -+} -+ -+static void osm_program_apm(struct sdm660_osm_clk *c) -+{ -+ osm_w(c, c->apm_mode_ctl, SEQ_REG(2)); -+ osm_w(c, c->apm_ctrl_status, SEQ_REG(3)); -+ osm_w(c, APM_MX_MODE, SEQ_REG(77)); -+ osm_w(c, APM_MX_MODE_VAL, SEQ_REG(78)); -+ osm_w(c, APM_MODE_SWITCH_MASK, SEQ_REG(79)); -+ osm_w(c, APM_APC_MODE, SEQ_REG(80)); -+ osm_w(c, APM_APC_MODE_VAL, SEQ_REG(81)); -+} -+ -+/* program the PLL/RCGR config + APM the OSM sequencer needs (secure_init) */ -+static void osm_program_config(struct sdm660_osm_clk *c) -+{ -+ osm_w(c, FIELD_PREP(GENMASK(23, 16), 0xf), SPM_CC_CTRL); -+ osm_w(c, c->l_val_base, SEQ_REG(0)); -+ osm_w(c, PLL_MIN_LVAL, SEQ_REG(21)); -+ osm_w(c, c->apcs_pll_user_ctl, SEQ_REG(18)); -+ osm_w(c, PLL_POST_DIV2, SEQ_REG(19)); -+ osm_w(c, PLL_POST_DIV1, SEQ_REG(29)); -+ -+ osm_program_apm(c); -+ -+ osm_w(c, c->apcs_cfg_rcgr, SEQ_REG(16)); -+ osm_w(c, c->apcs_cmd_rcgr, SEQ_REG(33)); -+ osm_w(c, RCG_UPDATE, SEQ_REG(34)); -+ osm_w(c, GPLL_SEL, SEQ_REG(17)); -+ osm_w(c, PLL_EARLY_SEL, SEQ_REG(82)); -+ osm_w(c, PLL_MAIN_SEL, SEQ_REG(83)); -+ osm_w(c, RCG_UPDATE_SUCCESS, SEQ_REG(84)); -+ osm_w(c, RCG_UPDATE, SEQ_REG(85)); -+ wmb(); -+} -+ -+/* -+ * MEM ACC + APM voltage-crossover sequencer setup. The sequencer consults -+ * these while ramping voltage during a DCVS transition; without them it stalls -+ * after switching the RCGR to the safe source (seen live: RCGR moves but the -+ * PLL never ramps). Because this driver runs every corner at the max CPR -+ * ceiling (see osm_get_lut) the APM always stays on APCC, so the threshold -+ * corner is 0 and the crossover never actually triggers. -+ */ -+#define MEM_ACC_APM_READ_MASK 0xff -+#define APM_THRESHOLD_VC 0 -+#define APM_CROSSOVER_VC 3 -+#define OSM_SEQ_MINUS_ONE 0xff -+ -+static void osm_program_mem_acc(struct sdm660_osm_clk *c) -+{ -+ osm_w(c, 1, SEQ_REG(51)); -+ osm_w(c, 2, SEQ_REG(52)); -+ osm_w(c, 3, SEQ_REG(53)); -+ osm_w(c, 4, SEQ_REG(54)); -+ osm_w(c, MEM_ACC_APM_READ_MASK, SEQ_REG(59)); -+ osm_w(c, 0, SEQ_REG(55)); -+ osm_w(c, 1, SEQ_REG(56)); -+ osm_w(c, 0, SEQ_REG(57)); -+ osm_w(c, 1, SEQ_REG(58)); -+ osm_w(c, c->phys + SEQ_REG(28), SEQ_REG(49)); -+ wmb(); -+} -+ -+static void osm_program_apm_vc(struct sdm660_osm_clk *c) -+{ -+ osm_w(c, APM_THRESHOLD_VC, SEQ_REG(1)); -+ osm_w(c, APM_CROSSOVER_VC, SEQ_REG(72)); -+ osm_w(c, c->phys + SEQ_REG(1), SEQ_REG(8)); -+ osm_w(c, APM_THRESHOLD_VC, SEQ_REG(15)); -+ osm_w(c, OSM_SEQ_MINUS_ONE, SEQ_REG(31)); -+ osm_w(c, 0x3b | (APM_THRESHOLD_VC << 6), SEQ_REG(73)); -+ osm_w(c, 0x39 | (APM_THRESHOLD_VC << 6), SEQ_REG(76)); -+ wmb(); -+} -+ -+static void osm_program_handoff(struct sdm660_osm_clk *c) -+{ -+ osm_w(c, c->apcs_itm_present, SEQ_REG(37)); -+ osm_w(c, 0, SEQ_REG(38)); -+ osm_w(c, c->itm_handoff, SEQ_REG(39)); -+ wmb(); -+} -+ -+static void osm_load_sequencer(struct sdm660_osm_clk *c) -+{ -+ int i; -+ -+ for (i = 0; i < ARRAY_SIZE(seq_instr); i++) -+ osm_w(c, seq_instr[i], SEQ_MEM_ADDR + i * 4); -+ for (i = 0; i < ARRAY_SIZE(seq_br_instr); i++) -+ osm_w(c, seq_br_instr[i], SEQ_CFG_BR_ADDR + i * 4); -+ wmb(); -+} -+ -+/* OSM-to-XO cycle counter, needed by the boost/DCVS FSMs for their timers */ -+static void osm_setup_cycle_counters(struct sdm660_osm_clk *c) -+{ -+ u32 ratio = OSM_CLK_RATE / XO_CLK_RATE; -+ u32 val = BIT(0) | FIELD_PREP(GENMASK(5, 1), ratio - 1) | -+ OSM_CYCLE_COUNTER_USE_XO_EDGE; -+ -+ osm_w(c, val, OSM_CYCLE_COUNTER_CTRL_REG); -+ wmb(); -+} -+ -+/* -+ * Program the boost (frequency up) and safe/DCVS (frequency down) FSMs. These -+ * are what actually carry out a DCVS request: without them the OSM latches the -+ * desired index but never drives the PLL/mux. boost + safe + PS are enabled -+ * (the wfx/pc/droop FSMs are power-collapse/di-dt droop refinements, optional). -+ */ -+static void osm_setup_fsms(struct sdm660_osm_clk *c) -+{ -+ u32 val; -+ -+ /* boost FSM (ramp up) */ -+ val = osm_r(c, PDN_FSM_CTRL_REG) | CC_BOOST_EN_MASK; -+ osm_w(c, val, PDN_FSM_CTRL_REG); -+ -+ val = osm_r(c, CC_BOOST_TIMER_REG0); -+ val |= FIELD_PREP(GENMASK(15, 0), osm_count_ns(PLL_WAIT_LOCK_TIME_NS)); -+ val |= FIELD_PREP(GENMASK(31, 16), osm_count_ns(SAFE_FREQ_WAIT_NS)); -+ osm_w(c, val, CC_BOOST_TIMER_REG0); -+ -+ val = osm_r(c, CC_BOOST_TIMER_REG1); -+ val |= FIELD_PREP(GENMASK(15, 0), osm_count_ns(PLL_WAIT_LOCK_TIME_NS)); -+ val |= FIELD_PREP(GENMASK(31, 16), osm_count_ns(PLL_WAIT_LOCK_TIME_NS)); -+ osm_w(c, val, CC_BOOST_TIMER_REG1); -+ -+ val = osm_r(c, CC_BOOST_TIMER_REG2); -+ val |= FIELD_PREP(GENMASK(15, 0), osm_count_ns(DEXT_DECREMENT_WAIT_NS)); -+ osm_w(c, val, CC_BOOST_TIMER_REG2); -+ -+ /* safe / DCVS boost FSM (ramp down) */ -+ val = osm_r(c, PDN_FSM_CTRL_REG) | DCVS_BOOST_EN_MASK; -+ osm_w(c, val, PDN_FSM_CTRL_REG); -+ -+ val = osm_r(c, DCVS_BOOST_TIMER_REG0); -+ val |= FIELD_PREP(GENMASK(15, 0), osm_count_ns(PLL_WAIT_LOCK_TIME_NS)); -+ val |= FIELD_PREP(GENMASK(31, 16), osm_count_ns(SAFE_FREQ_WAIT_NS)); -+ osm_w(c, val, DCVS_BOOST_TIMER_REG0); -+ -+ val = osm_r(c, DCVS_BOOST_TIMER_REG1); -+ val |= FIELD_PREP(GENMASK(15, 0), osm_count_ns(PLL_WAIT_LOCK_TIME_NS)); -+ val |= FIELD_PREP(GENMASK(31, 16), osm_count_ns(PLL_WAIT_LOCK_TIME_NS)); -+ osm_w(c, val, DCVS_BOOST_TIMER_REG1); -+ -+ val = osm_r(c, DCVS_BOOST_TIMER_REG2); -+ val |= FIELD_PREP(GENMASK(15, 0), osm_count_ns(DEXT_DECREMENT_WAIT_NS)); -+ osm_w(c, val, DCVS_BOOST_TIMER_REG2); -+ -+ /* -+ * PS (perf-state) FSM. r56: the working downstream enables this too and -+ * it is the FSM that carries out a perf-state (DCVS_PERF_STATE_DESIRED) -+ * transition - without it the OSM latched the desired index but never -+ * drove the PLL/mux (the r49..r55 symptom). -+ */ -+ val = osm_r(c, PDN_FSM_CTRL_REG) | PS_BOOST_EN_MASK; -+ osm_w(c, val, PDN_FSM_CTRL_REG); -+ -+ val = osm_r(c, PS_BOOST_TIMER_REG0); -+ val |= FIELD_PREP(GENMASK(15, 0), osm_count_ns(PLL_WAIT_LOCK_TIME_NS)); -+ val |= FIELD_PREP(GENMASK(31, 16), osm_count_ns(SAFE_FREQ_WAIT_NS)); -+ osm_w(c, val, PS_BOOST_TIMER_REG0); -+ -+ val = osm_r(c, PS_BOOST_TIMER_REG1); -+ val |= FIELD_PREP(GENMASK(15, 0), osm_count_ns(PLL_WAIT_LOCK_TIME_NS)); -+ val |= FIELD_PREP(GENMASK(31, 16), osm_count_ns(PLL_WAIT_LOCK_TIME_NS)); -+ osm_w(c, val, PS_BOOST_TIMER_REG1); -+ -+ val = osm_r(c, PS_BOOST_TIMER_REG2); -+ val |= FIELD_PREP(GENMASK(15, 0), osm_count_ns(DEXT_DECREMENT_WAIT_NS)); -+ osm_w(c, val, PS_BOOST_TIMER_REG2); -+ -+ osm_w(c, 0x5, BOOST_PROG_SYNC_DELAY_REG); -+ -+ val = osm_r(c, OSM_PLL_SW_OVERRIDE_EN) | PLL_SW_OVERRIDE_DROOP_EN; -+ osm_w(c, val, OSM_PLL_SW_OVERRIDE_EN); -+ wmb(); -+} -+ -+/* APCS RCGR mux: cmd at +0x0, cfg at +0x4; SRC_SEL is cfg[10:8] */ -+#define RCGR_CMD 0x0 -+#define RCGR_CFG 0x4 -+#define RCGR_UPDATE_BIT BIT(0) -+#define RCGR_SRC_MASK GENMASK(10, 8) -+#define RCGR_SRC_GPLL0 4 /* GPLL0 (safe bypass) */ -+ -+static void rcgr_do_update(struct sdm660_osm_clk *c) -+{ -+ int i; -+ -+ writel_relaxed(readl_relaxed(c->rcgr_base + RCGR_CMD) | RCGR_UPDATE_BIT, -+ c->rcgr_base + RCGR_CMD); -+ for (i = 0; i < 200; i++) { -+ if (!(readl_relaxed(c->rcgr_base + RCGR_CMD) & RCGR_UPDATE_BIT)) -+ break; -+ udelay(1); -+ } -+} -+ -+/* -+ * Reconfigure the cluster HFPLL so the OSM can drive it. Reconfiguring means -+ * briefly powering the PLL down (MODE=0), which hangs the cluster if it is -+ * currently clocked from that PLL - and the little cluster (which runs the -+ * kernel) is. So first switch the cluster's RCGR mux to GPLL0, reconfigure the -+ * PLL, then switch it back. Verified live: without the bypass this hangs. -+ */ -+static void __maybe_unused osm_setup_cluster_pll(struct sdm660_osm_clk *c) -+{ -+ u32 cfg = readl_relaxed(c->rcgr_base + RCGR_CFG); -+ -+ /* park the cluster on GPLL0 */ -+ writel_relaxed((cfg & ~RCGR_SRC_MASK) | -+ FIELD_PREP(RCGR_SRC_MASK, RCGR_SRC_GPLL0), -+ c->rcgr_base + RCGR_CFG); -+ rcgr_do_update(c); -+ udelay(1); -+ -+ writel_relaxed(0x0, c->pll_base + PLL_MODE); -+ writel_relaxed(PLL_SETUP_L_VAL, c->pll_base + PLL_L_VAL); -+ writel_relaxed(PLL_SETUP_USER_CTRL, c->pll_base + PLL_USER_CTRL); -+ writel_relaxed(PLL_SETUP_CONFIG_CTL, c->pll_base + PLL_CONFIG_CTL_LO); -+ writel_relaxed(0x2, c->pll_base + PLL_MODE); -+ wmb(); -+ udelay(PLL_WAIT_LOCK_TIME_US); -+ writel_relaxed(0x6, c->pll_base + PLL_MODE); -+ wmb(); -+ usleep_range(50, 75); -+ writel_relaxed(0x7, c->pll_base + PLL_MODE); -+ wmb(); -+ -+ /* restore the original mux selection */ -+ writel_relaxed(cfg, c->rcgr_base + RCGR_CFG); -+ rcgr_do_update(c); -+} -+ -+static void __maybe_unused osm_enable(struct sdm660_osm_clk *c) -+{ -+ udelay(5); -+ osm_w(c, 1, ENABLE_REG); -+ wmb(); -+ udelay(5); -+} -+ -+/* --- device tree LUT parsing --- */ -+ -+static int osm_get_lut(struct device *dev, struct sdm660_osm_clk *c, -+ const char *prop, const u32 *ceil_uv, int ceil_cnt) -+{ -+ struct device_node *np = dev->of_node; -+ int len, rows, i, j; -+ u32 vmax = 0; -+ -+ len = of_property_count_u32_elems(np, prop); -+ if (len < 0) -+ return dev_err_probe(dev, len, "missing %s\n", prop); -+ rows = len / 5; -+ if (rows > OSM_TABLE_SIZE) -+ rows = OSM_TABLE_SIZE; -+ -+ /* -+ * First cut: run every corner at the highest CPR ceiling. The -+ * per-corner virtual-corner -> voltage mapping goes through the CPR3 -+ * fuse/fmax tables which this driver does not model; using the max -+ * ceiling for all entries over-volts the low frequencies (wastes a -+ * little power) but guarantees no frequency is ever under-volted. -+ * Proper per-corner open-loop scaling is a later optimisation. -+ */ -+ for (j = 0; j < ceil_cnt; j++) -+ if (ceil_uv[j] > vmax) -+ vmax = ceil_uv[j]; -+ -+ for (i = 0; i < rows; i++) { -+ u32 cell[5]; -+ -+ for (j = 0; j < 5; j++) -+ of_property_read_u32_index(np, prop, i * 5 + j, &cell[j]); -+ /* [0]freq [1]freq_data [2]override [3]virtual_corner(1-based) [4]spare */ -+ c->table[i].frequency = cell[0]; -+ c->table[i].freq_data = cell[1]; -+ c->table[i].override_data = cell[2]; -+ c->table[i].virtual_corner = cell[3] - 1; -+ c->table[i].spare_data = cell[4]; -+ c->table[i].open_loop_volt = vmax / 1000; -+ } -+ c->num_entries = rows; -+ return 0; -+} -+ -+static struct clk_hw *osm_hws[NUM_CLUSTERS]; -+ -+static struct clk_hw *osm_of_get(struct of_phandle_args *args, void *data) ++static struct clk_hw *sdm660_apcs_of_get(struct of_phandle_args *args, void *data) +{ + unsigned int idx = args->args[0]; + -+ return idx < NUM_CLUSTERS ? osm_hws[idx] : ERR_PTR(-EINVAL); ++ return idx < NUM_CLUSTERS ? apcs_hws[idx] : ERR_PTR(-EINVAL); +} + -+struct cluster_cfg { -+ const char *name; -+ const char *lut_prop; -+ const char *ceil_prop; -+ u32 base_off; -+ u32 pll_phys; -+ u32 l_val_base, pll_user_ctl, cfg_rcgr, cmd_rcgr, itm_present; -+ u32 apm_mode_ctl, apm_ctrl_status, itm_handoff; -+}; -+ -+/* physical addresses come from the SDM660 APCS layout (fixed for this SoC) */ -+static const struct cluster_cfg cluster_cfgs[NUM_CLUSTERS] = { -+ { /* cluster 0: power (little) cpu0-3 */ -+ .name = "pwrcl_clk", .base_off = 0, -+ .lut_prop = "qcom,pwrcl-speedbin0-v0", -+ .ceil_prop = "qcom,pwrcl-voltage-ceiling", -+ .pll_phys = 0x17916000, -+ .l_val_base = 0x17916004, .pll_user_ctl = 0x1791600c, -+ .cfg_rcgr = 0x17911054, .cmd_rcgr = 0x17911050, -+ .itm_present = 0x179d143c, -+ .apm_mode_ctl = 0x179d0004, .apm_ctrl_status = 0x179d000c, -+ .itm_handoff = ITM_CL0_DISABLE_CL1_ENABLED, -+ }, -+ { /* cluster 1: performance (big) cpu4-7 */ -+ .name = "perfcl_clk", .base_off = OSM_CORE_TABLE_SIZE, -+ .lut_prop = "qcom,perfcl-speedbin1-v0", -+ .ceil_prop = "qcom,perfcl-voltage-ceiling", -+ .pll_phys = 0x17816000, -+ .l_val_base = 0x17816004, .pll_user_ctl = 0x1781600c, -+ .cfg_rcgr = 0x17811054, .cmd_rcgr = 0x17811050, -+ .itm_present = 0x179d143c, -+ .apm_mode_ctl = 0x179d0010, .apm_ctrl_status = 0x179d0018, -+ .itm_handoff = ITM_CL0_ENABLED_CL1_DISABLE, -+ }, -+}; -+ -+static int sdm660_osm_probe(struct platform_device *pdev) ++static int sdm660_apcs_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; -+ struct sdm660_osm_clk *cl[NUM_CLUSTERS]; -+ void __iomem *osm_base; -+ struct resource *osm_res; ++ struct sdm660_apcs_clk *cl[NUM_CLUSTERS]; + struct clk *aux; + int i, ret; + -+ /* -+ * The sequencer setup writes go through the secure monitor -+ * (qcom_scm_io_writel). This clock controller can probe before the SCM -+ * firmware driver is up, in which case those writes would be dropped and -+ * the OSM would enable with an empty sequencer - so defer until SCM is -+ * available. -+ */ -+ if (!qcom_scm_is_available()) -+ return dev_err_probe(dev, -EPROBE_DEFER, "waiting for qcom_scm\n"); -+ -+ /* GPLL0/aux feeds the OSM sequencer clock; keep it on */ ++ /* Keep GPLL0/aux enabled as the safe bypass clock during switches */ + aux = devm_clk_get_enabled(dev, "aux"); + if (IS_ERR(aux)) + return dev_err_probe(dev, PTR_ERR(aux), "no aux clock\n"); + -+ osm_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "osm"); -+ osm_base = devm_ioremap_resource(dev, osm_res); -+ if (IS_ERR(osm_base)) -+ return PTR_ERR(osm_base); -+ + for (i = 0; i < NUM_CLUSTERS; i++) { -+ const struct cluster_cfg *cfg = &cluster_cfgs[i]; -+ u32 ceil[8]; -+ int ceil_cnt; ++ const struct cluster_desc *desc = &cluster_descs[i]; ++ struct clk_init_data init = { ++ .name = desc->name, ++ .ops = &sdm660_apcs_ops, ++ .flags = CLK_GET_RATE_NOCACHE, ++ }; + + cl[i] = devm_kzalloc(dev, sizeof(*cl[i]), GFP_KERNEL); + if (!cl[i]) + return -ENOMEM; + -+ cl[i]->base = osm_base + cfg->base_off; -+ cl[i]->phys = osm_res->start + cfg->base_off; -+ cl[i]->pll_base = devm_ioremap(dev, cfg->pll_phys, 0x100); -+ if (!cl[i]->pll_base) ++ spin_lock_init(&cl[i]->lock); ++ cl[i]->rates = desc->rates; ++ cl[i]->num_rates = desc->num_rates; ++ cl[i]->pll_base = devm_ioremap(dev, desc->pll_phys, 0x40); ++ cl[i]->rcgr_base = devm_ioremap(dev, desc->rcgr_phys, 0x10); ++ if (!cl[i]->pll_base || !cl[i]->rcgr_base) + return -ENOMEM; -+ cl[i]->rcgr_base = devm_ioremap(dev, cfg->cmd_rcgr, 0x10); -+ if (!cl[i]->rcgr_base) -+ return -ENOMEM; -+ cl[i]->l_val_base = cfg->l_val_base; -+ cl[i]->apcs_pll_user_ctl = cfg->pll_user_ctl; -+ cl[i]->apcs_cfg_rcgr = cfg->cfg_rcgr; -+ cl[i]->apcs_cmd_rcgr = cfg->cmd_rcgr; -+ cl[i]->apcs_itm_present = cfg->itm_present; -+ cl[i]->apm_mode_ctl = cfg->apm_mode_ctl; -+ cl[i]->apm_ctrl_status = cfg->apm_ctrl_status; -+ cl[i]->itm_handoff = cfg->itm_handoff; + -+ ceil_cnt = of_property_read_variable_u32_array(dev->of_node, -+ cfg->ceil_prop, ceil, 1, ARRAY_SIZE(ceil)); -+ if (ceil_cnt < 0) -+ ceil_cnt = 0; -+ -+ ret = osm_get_lut(dev, cl[i], cfg->lut_prop, ceil, ceil_cnt); -+ if (ret) -+ return ret; -+ -+ /* order mirrors the downstream probe (secure_init path) */ -+ osm_setup_cycle_counters(cl[i]); -+ osm_setup_lut(cl[i]); -+ osm_setup_fsms(cl[i]); -+ osm_program_config(cl[i]); -+ } -+ -+ /* handoff, sequencer microcode and voltage setup (both clusters) */ -+ for (i = 0; i < NUM_CLUSTERS; i++) { -+ osm_program_handoff(cl[i]); -+ osm_load_sequencer(cl[i]); -+ osm_program_mem_acc(cl[i]); -+ osm_program_apm_vc(cl[i]); -+ } -+ -+ /* prepare the cluster PLLs, enable the OSM and register the clocks */ -+ for (i = 0; i < NUM_CLUSTERS; i++) { -+ struct clk_init_data init = { -+ .name = cluster_cfgs[i].name, -+ .ops = &sdm660_osm_ops, -+ }; -+ -+ /* -+ * r57: OSM enable DISABLED. Exhaustive testing (r49-r56, with -+ * the freq/voltage LUT + FSMs + CPRh all verified byte-correct -+ * against a working downstream device) proved the OSM latches -+ * the perf-state vote (DCVS_PERF_STATE_DESIRED) but never drives -+ * the PLL/mux: the transition needs the sequencer config in the -+ * OSM's <0x1000 region, which is XPU-protected and established -+ * only by the Android secure-boot flow, not reproducible from a -+ * mainline boot. Leaving the OSM disabled keeps the driver inert -+ * (everything is programmed but nothing enabled) and the boot -+ * stable; the CPUs run at the bootloader-set fixed rate. Flip the -+ * two calls below back on to reproduce the experiment. -+ */ -+ if (0) { -+ osm_setup_cluster_pll(cl[i]); -+ osm_enable(cl[i]); ++ if (i == 1) { ++ /* Step Gold cluster smoothly up to safe NOM ceiling */ ++ ret = sdm660_apcs_set_rate(&cl[i]->hw, 1113600000UL, 0); ++ udelay(30); ++ if (!ret) { ++ ret = sdm660_apcs_set_rate(&cl[i]->hw, 1401600000UL, 0); ++ udelay(30); ++ } ++ if (!ret) ++ ret = sdm660_apcs_set_rate(&cl[i]->hw, desc->init_rate, 0); ++ } else { ++ ret = sdm660_apcs_set_rate(&cl[i]->hw, desc->init_rate, 0); + } ++ if (ret) ++ dev_warn(dev, "%s: initial set_rate %lu failed (%d)\n", ++ desc->name, desc->init_rate, ret); ++ + cl[i]->hw.init = &init; + ret = devm_clk_hw_register(dev, &cl[i]->hw); + if (ret) + return ret; -+ osm_hws[i] = &cl[i]->hw; ++ apcs_hws[i] = &cl[i]->hw; ++ ++ dev_info(dev, "%s: rate=%lu Hz (L=%#x STATUS=%#x CFG=%#x)\n", ++ desc->name, ++ sdm660_apcs_recalc_rate(&cl[i]->hw, 0), ++ readl_relaxed(cl[i]->pll_base + PLL_L_VAL), ++ readl_relaxed(cl[i]->pll_base + PLL_STATUS), ++ readl_relaxed(cl[i]->rcgr_base + RCGR_CFG)); + } + -+ /* -+ * r54 decisive test. Since r50 every build wrote the sequencer through -+ * qcom_scm_io_writel (proven a silent no-op in r51); the "plain kernel -+ * writel + read back through the secure monitor" combination was NEVER -+ * tried, so whether a kernel writel to the < 0x1000 region lands is still -+ * genuinely open. VERSION_REG (0x0) is a fixed non-zero HW id in the same -+ * XPU domain and acts as a control: if its SCM read is non-zero, SCM reads -+ * of this region are NOT masked and reg21 is trustworthy - reg21 == -+ * PLL_MIN_LVAL then means the plain writel LANDED (sequencer loadable, -+ * re-enable the OSM in r55). If SCMver reads 0 as well, SCM reads are -+ * masked and every past "seq0=0" datum (the "sequencer empty" evidence) -+ * was unsound and the sequencer may in fact be programmed. -+ */ -+ for (i = 0; i < NUM_CLUSTERS; i++) { -+ u32 scm_seq0 = 0, scm_reg21 = 0, scm_ver = 0; -+ u32 hlos_ver = osm_r(cl[i], 0x0); -+ int r0, r2, rv; -+ -+ rv = qcom_scm_io_readl(cl[i]->phys + 0x0, &scm_ver); -+ r0 = qcom_scm_io_readl(cl[i]->phys + SEQ_MEM_ADDR, &scm_seq0); -+ r2 = qcom_scm_io_readl(cl[i]->phys + SEQ_REG(21), &scm_reg21); -+ -+ dev_info(dev, -+ "%s: enable=%#x desired=%u | VER hlos=%#x scm(%d)=%#x | SCM(%d/%d) seq0=%#x reg21=%#x(exp %d)\n", -+ cluster_cfgs[i].name, -+ osm_r(cl[i], ENABLE_REG), -+ osm_r(cl[i], DCVS_PERF_STATE_DESIRED_REG), -+ hlos_ver, rv, scm_ver, -+ r0, r2, scm_seq0, scm_reg21, PLL_MIN_LVAL); -+ } -+ -+ return devm_of_clk_add_hw_provider(dev, osm_of_get, NULL); ++ return devm_of_clk_add_hw_provider(dev, sdm660_apcs_of_get, NULL); +} + -+static const struct of_device_id sdm660_osm_match[] = { ++static const struct of_device_id sdm660_apcs_match[] = { + { .compatible = "qcom,sdm660-osm" }, + {} +}; -+MODULE_DEVICE_TABLE(of, sdm660_osm_match); ++MODULE_DEVICE_TABLE(of, sdm660_apcs_match); + -+static struct platform_driver sdm660_osm_driver = { -+ .probe = sdm660_osm_probe, ++static struct platform_driver sdm660_apcs_driver = { ++ .probe = sdm660_apcs_probe, + .driver = { + .name = "sdm660-osm-cpu", -+ .of_match_table = sdm660_osm_match, ++ .of_match_table = sdm660_apcs_match, + }, +}; -+module_platform_driver(sdm660_osm_driver); ++module_platform_driver(sdm660_apcs_driver); + -+/* -+ * Boot-stage diagnostic. On this SoC the OSM sequencer is meant to be -+ * programmed by the secure world before Linux (secure_init=false model), so it -+ * should already contain microcode when Linux starts. Sample it before this -+ * driver's probe (fs_initcall) and after it (late_initcall): if the pre-probe -+ * read shows the first microcode word (0xc2005000) but the post-probe read -+ * shows 0, this driver's probe is clearing the firmware-programmed sequencer. -+ */ -+static void osm_dbg_dump(const char *stage) -+{ -+ void __iomem *b = ioremap(0x179c0000, 0x3000); -+ u32 en = 0, hp = 0, hb = 0, sp = 0, sb = 0; -+ int rs = -1; -+ -+ if (b) { -+ en = readl_relaxed(b + ENABLE_REG); -+ hp = readl_relaxed(b + SEQ_MEM_ADDR); -+ hb = readl_relaxed(b + OSM_CORE_TABLE_SIZE + SEQ_MEM_ADDR); -+ iounmap(b); -+ } -+ if (qcom_scm_is_available()) { -+ rs = qcom_scm_io_readl(0x179c0000 + SEQ_MEM_ADDR, &sp); -+ qcom_scm_io_readl(0x179c2000 + SEQ_MEM_ADDR, &sb); -+ } -+ pr_info("sdm660-osm DBG[%s]: pwrcl ENABLE=%#x | readl seq0 pwrcl=%#x perfcl=%#x | SCM(ret %d) pwrcl=%#x perfcl=%#x\n", -+ stage, en, hp, hb, rs, sp, sb); -+} -+ -+static int __init osm_dbg_fs(void) -+{ -+ osm_dbg_dump("fs/pre-probe"); -+ return 0; -+} -+fs_initcall(osm_dbg_fs); -+ -+static int __init osm_dbg_late(void) -+{ -+ osm_dbg_dump("late/post-probe"); -+ return 0; -+} -+late_initcall(osm_dbg_late); -+ -+MODULE_DESCRIPTION("Qualcomm SDM660 CPU (OSM) clock driver"); ++MODULE_DESCRIPTION("Qualcomm SDM660 Direct APCS Agera PLL CPU clock driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/clk/qcom/Kconfig b/drivers/clk/qcom/Kconfig index a284ba0..9e8d43d 100644 diff --git a/ports/candidate/linux-postmarketos-qcom-sdm660/0046-arm64-dts-platina-fix-cpu-cluster-mapping-and-nom-opps.patch b/ports/candidate/linux-postmarketos-qcom-sdm660/0046-arm64-dts-platina-fix-cpu-cluster-mapping-and-nom-opps.patch new file mode 100644 index 0000000..c59fd6f --- /dev/null +++ b/ports/candidate/linux-postmarketos-qcom-sdm660/0046-arm64-dts-platina-fix-cpu-cluster-mapping-and-nom-opps.patch @@ -0,0 +1,39 @@ +From: Platina Linux contributors +Subject: [PATCH] arm64: dts: qcom: platina: fix big.LITTLE cluster mapping and NOM OPPs + +In sdm630.dtsi/sdm660.dtsi, cpu0..cpu3 (reg 0x100..0x103) are the Gold A73 +performance cluster (capacity 1024) and cpu4..cpu7 (reg 0x0..0x3) are the +Silver A53 power cluster (capacity 640). Remap their clocks/OPPs to match +<&cpu_osm 1> (perfcl) and <&cpu_osm 0> (pwrcl) respectively, and prune OPPs +above the CPRh NOM ceiling (1401.6 MHz Silver / 1747.2 MHz Gold). + +--- a/arch/arm64/boot/dts/qcom/sdm660-xiaomi-platina.dts ++++ b/arch/arm64/boot/dts/qcom/sdm660-xiaomi-platina.dts +@@ -1234,3 +1234,27 @@ + &ipa { + status = "okay"; + }; ++ ++/* Fix big.LITTLE cluster mapping (cpu0..3 = Gold perfcl, cpu4..7 = Silver pwrcl) ++ * and cap OPPs to the 868/872 mV NOM ceiling. ++ */ ++&cpu0_opp_table { ++ /delete-node/ opp-1536000000; ++ /delete-node/ opp-1747200000; ++ /delete-node/ opp-1843200000; ++}; ++ ++&cpu4_opp_table { ++ /delete-node/ opp-1958400000; ++ /delete-node/ opp-2150400000; ++ /delete-node/ opp-2208000000; ++}; ++ ++&cpu0 { clocks = <&cpu_osm 1>; operating-points-v2 = <&cpu4_opp_table>; }; ++&cpu1 { clocks = <&cpu_osm 1>; operating-points-v2 = <&cpu4_opp_table>; }; ++&cpu2 { clocks = <&cpu_osm 1>; operating-points-v2 = <&cpu4_opp_table>; }; ++&cpu3 { clocks = <&cpu_osm 1>; operating-points-v2 = <&cpu4_opp_table>; }; ++&cpu4 { clocks = <&cpu_osm 0>; operating-points-v2 = <&cpu0_opp_table>; }; ++&cpu5 { clocks = <&cpu_osm 0>; operating-points-v2 = <&cpu0_opp_table>; }; ++&cpu6 { clocks = <&cpu_osm 0>; operating-points-v2 = <&cpu0_opp_table>; }; ++&cpu7 { clocks = <&cpu_osm 0>; operating-points-v2 = <&cpu0_opp_table>; }; diff --git a/ports/candidate/linux-postmarketos-qcom-sdm660/APKBUILD b/ports/candidate/linux-postmarketos-qcom-sdm660/APKBUILD index 1610f04..75b0260 100644 --- a/ports/candidate/linux-postmarketos-qcom-sdm660/APKBUILD +++ b/ports/candidate/linux-postmarketos-qcom-sdm660/APKBUILD @@ -3,7 +3,7 @@ maintainer="Alexey Minnekhanov " _flavor="postmarketos-qcom-sdm660" pkgname=linux-$_flavor pkgver=7.2.12 -pkgrel=26 +pkgrel=27 pkgdesc="Close to mainline linux kernel for Qualcomm Snapdragon 660 SoC" arch="aarch64" url="https://kernel.org/" @@ -82,6 +82,7 @@ source=" 0043-net-ipa-add-support-for-ipa-v2.6-and-sdm660.patch 0044-net-ipa-implement-sdm660-v2-6l-bam-transport.patch 0045-arm64-dts-sdm630-fix-anoc2-context-irq-polarity.patch + 0046-arm64-dts-platina-fix-cpu-cluster-mapping-and-nom-opps.patch " _carch="arm64" @@ -145,7 +146,7 @@ f7a8b7272ccb70b6a3a0049006ec608960fd876bf43ebf2c5880c92d1e55e7953a31dff37a2b2197 e8a8b9382cdb3543b9ed73d309bfc37d41be96852b92ba46beed78b880186e71e3d73a151acb9893f8f13764e9265bf5a50865b2c2329feeb064039b70721091 0011-ath10k-wcn3990-chanlist-qcacld.patch a60a9faec11342f4ac0388b612edbbbe132269d2fd68bbbd2709fa0af068f1c9d27b57d5ebf0c6fc99792609eee0d715572ec160e41698bf0ae11a5c5cab7cf1 0012-ath10k-wcn3990-scan-trailing-tlvs.patch ae81f328100b9a6f756c68f5fcda2618ea866349c2ce291703f5f817b42d3a0954c772ed6f063d8132f522837e3eaa2978c61564cd19536f4442eb381e636680 0014-ath10k-wcn3990-no-scan-chan-stat.patch -f8a47c452ec42a4c9278bd7e40e83ecea1d18c6eb76099e8e5526788275e2afac24bc2b415c22e1e41a3fda14ce07dc968e3e7e95b91122226a1fb098b027305 0015-clk-qcom-sdm660-cpu-osm-driver.patch +883f2c3bbd707d61ba830fb8b1d7d85cf2fd6d1362cbfe14d4c85835fab7d28d7c2bd962a7cd509dfbd5f91d4b415b8478c50eb3b191a9ef562c9962681341a8 0015-clk-qcom-sdm660-cpu-osm-driver.patch 36aef2306a6bb302e69891bf6619cfdfa81bc2e09cd2e4a2a15371f39af5755cf1045e061a1561d7375d8cfd8659dd2d81cab0296c62d90f7b256894fa28ea14 0016-arm64-dts-sdm660-platina-osm-cpufreq.patch 71cd673724dda58e0af4c50d428d3739114eb32640da84823c84ffb8342ab1106f361cc5c780d58827bbc854285f06acdce17ffa486d3a4d14b786c1c415e318 0017-clk-qcom-sdm660-cprh-driver.patch 110ad7640090163637a29fa61d5901b7424a2516be9f3131094fbecdb0ce69d705b3584a49ce27ae1d7c3319eefac974912e78fa65ede2baadd4135f724fc20b 0018-platina-audio.patch @@ -183,4 +184,5 @@ eb3b89ac99648776f193ee3c9d96d257f7d2e6ba5200a526e63c97e7dc4982e4b2985f15481aa431 e2980a3b86521f452d042399812b8b5dd03af1193143313833e3608a8079763d4cc5c8d6c8530203db062c3df5ce40800e63567026239e6b19cdf6be07e3b199 0043-net-ipa-add-support-for-ipa-v2.6-and-sdm660.patch 62f5ec3c6838fbdb3f10d6cda5dc28854bd30347b19fbf6784b51748bdb9496c4f7a21b5ae7c3f483029d2edaa5e85cf1b544f028793261dc2f881c1811daa90 0044-net-ipa-implement-sdm660-v2-6l-bam-transport.patch 71e2ab4d994394bd2055948b536605709b9eb4775321fbd321cbdbf62d29687573c4c17485b174d153f7383a2ebf574c68b04ed98844f61a5e225b611a3a8b12 0045-arm64-dts-sdm630-fix-anoc2-context-irq-polarity.patch +e29c9988ef9ea35e749c75aa73efd2622fa82b9c16350149d311e971f71855931045986171a607a0e03ca87bb2fa03c37ac3d0f8bf01e876b670374744a61cb4 0046-arm64-dts-platina-fix-cpu-cluster-mapping-and-nom-opps.patch " diff --git a/project/sources.lock.json b/project/sources.lock.json index 73115f5..accb468 100644 --- a/project/sources.lock.json +++ b/project/sources.lock.json @@ -16,7 +16,7 @@ "candidate": { "ports": "ports/candidate", "kernel": "7.2.12", - "kernel_package": "7.2.12-r26", + "kernel_package": "7.2.12-r27", "device_package": "5-r34", "firmware_package": "2-r5" }