Files
mi8-platina-linux/ports/candidate/linux-postmarketos-qcom-sdm660/0015-clk-qcom-sdm660-cpu-osm-driver.patch
T
2026-11-18 14:26:58 +09:00

343 lines
9.5 KiB
Diff

From: Platina Linux contributors
Subject: [PATCH] clk: qcom: drive SDM660 APCS Agera HFPLLs directly for CPU DVFS
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.
---
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,295 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * SDM660 direct APCS Agera HFPLL + RCG2 CPU clock driver.
+ *
+ * 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 <linux/clk.h>
+#include <linux/clk-provider.h>
+#include <linux/delay.h>
+#include <linux/io.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+
+#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
+
+#define PLL_L_VAL 0x04
+#define PLL_ALPHA_VAL 0x08
+#define PLL_STATUS 0x2c
+#define PLL_LOCK_DET BIT(16)
+
+#define XO_CLK_RATE 19200000UL
+#define GPLL0_CLK_RATE 600000000UL
+#define NUM_CLUSTERS 2
+#define MAX_CLUSTER_RATES 6
+
+struct sdm660_apcs_clk {
+ struct clk_hw hw;
+ void __iomem *pll_base;
+ void __iomem *rcgr_base;
+ const unsigned long *rates;
+ int num_rates;
+ spinlock_t lock;
+};
+
+#define to_apcs_clk(_hw) container_of(_hw, struct sdm660_apcs_clk, hw)
+
+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 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 int rcgr_update_locked(void __iomem *rcgr)
+{
+ u32 cmd = readl_relaxed(rcgr + RCGR_CMD);
+ int i;
+
+ 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 apcs_set_rate_locked(struct sdm660_apcs_clk *c, unsigned long rate)
+{
+ u32 l_val, cfg;
+ int i, ret;
+
+ 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 int sdm660_apcs_set_rate(struct clk_hw *hw, unsigned long rate,
+ unsigned long parent_rate)
+{
+ struct sdm660_apcs_clk *c = to_apcs_clk(hw);
+ unsigned long flags;
+ int ret;
+
+ spin_lock_irqsave(&c->lock, flags);
+ ret = apcs_set_rate_locked(c, rate);
+ spin_unlock_irqrestore(&c->lock, flags);
+ return ret;
+}
+
+static unsigned long sdm660_apcs_recalc_rate(struct clk_hw *hw,
+ unsigned long parent_rate)
+{
+ 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_rates; i++) {
+ if (c->rates[i] <= req->rate)
+ best = c->rates[i];
+ }
+ req->rate = best;
+ return 0;
+}
+
+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,
+};
+
+static struct clk_hw *apcs_hws[NUM_CLUSTERS];
+
+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 ? apcs_hws[idx] : ERR_PTR(-EINVAL);
+}
+
+static int sdm660_apcs_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct sdm660_apcs_clk *cl[NUM_CLUSTERS];
+ struct clk *aux;
+ int i, ret;
+
+ /* 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");
+
+ for (i = 0; i < NUM_CLUSTERS; i++) {
+ 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;
+
+ 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;
+
+ 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;
+ 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));
+ }
+
+ return devm_of_clk_add_hw_provider(dev, sdm660_apcs_of_get, NULL);
+}
+
+static const struct of_device_id sdm660_apcs_match[] = {
+ { .compatible = "qcom,sdm660-osm" },
+ {}
+};
+MODULE_DEVICE_TABLE(of, sdm660_apcs_match);
+
+static struct platform_driver sdm660_apcs_driver = {
+ .probe = sdm660_apcs_probe,
+ .driver = {
+ .name = "sdm660-osm-cpu",
+ .of_match_table = sdm660_apcs_match,
+ },
+};
+module_platform_driver(sdm660_apcs_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
--- a/drivers/clk/qcom/Kconfig
+++ b/drivers/clk/qcom/Kconfig
@@ -871,6 +871,15 @@ config SDM_GCC_660
Say Y if you want to use peripheral devices such as UART, SPI,
i2C, USB, UFS, SDDC, PCIe, etc.
+config SDM660_CPU_CLK
+ tristate "SDM660 CPU (OSM) Clock Controller"
+ depends on ARM64 || COMPILE_TEST
+ help
+ Support for the Operating State Manager (OSM) that drives the two
+ Kryo-260 CPU clusters on SDM660. Unlike SDM845 the bootloader does
+ not program the OSM, so this driver loads the frequency/voltage
+ tables and the sequencer microcode itself, enabling cpufreq.
+
config SDM_MMCC_660
tristate "SDM660 Multimedia Clock Controller"
depends on ARM64 || COMPILE_TEST
diff --git a/drivers/clk/qcom/Makefile b/drivers/clk/qcom/Makefile
index 0ac8a90..e7d1ff2 100644
--- a/drivers/clk/qcom/Makefile
+++ b/drivers/clk/qcom/Makefile
@@ -121,6 +121,7 @@ obj-$(CONFIG_SDM_DISPCC_845) += dispcc-sdm845.o
obj-$(CONFIG_SDM_GCC_660) += gcc-sdm660.o
obj-$(CONFIG_SDM_MMCC_660) += mmcc-sdm660.o
obj-$(CONFIG_SDM_GPUCC_660) += gpucc-sdm660.o
+obj-$(CONFIG_SDM660_CPU_CLK) += clk-cpu-sdm660.o
obj-$(CONFIG_SDM_GCC_845) += gcc-sdm845.o
obj-$(CONFIG_SDM_GPUCC_845) += gpucc-sdm845.o
obj-$(CONFIG_SDM_LPASSCC_845) += lpasscc-sdm845.o