1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
7 * Each of the CPU clusters (Power and Perf) on msm8996 are
8 * clocked via 2 PLLs, a primary and alternate. There are also
9 * 2 Mux'es, a primary and secondary all connected together
14 * +------------------>0 |
16 * +------------------>3 |
21 * | +-------+ | +-------+
24 * +---------------+ | +----------->1 | CPU clk
25 * |Primary PLL +----+ PLL_EARLY | | +------>
26 * | +------+-----------+ +------>2 PMUX |
27 * +---------------+ | | | |
28 * | +------+ | +-->3 |
29 * +--^+ ACD +-----+ | +-------+
30 * +---------------+ +------+ |
32 * | +---------------------------+
33 * +---------------+ PLL_EARLY
35 * The primary PLL is what drives the CPU clk, except for times
36 * when we are reprogramming the PLL itself (for rate changes) when
37 * we temporarily switch to an alternate PLL.
39 * The primary PLL operates on a single VCO range, between 600MHz
40 * and 3GHz. However the CPUs do support OPPs with frequencies
41 * between 300MHz and 600MHz. In order to support running the CPUs
42 * at those frequencies we end up having to lock the PLL at twice
43 * the rate and drive the CPU clk via the PLL/2 output and SMUX.
45 * So for frequencies above 600MHz we follow the following path
46 * Primary PLL --> PLL_EARLY --> PMUX(1) --> CPU clk
47 * and for frequencies between 300MHz and 600MHz we follow
48 * Primary PLL --> PLL/2 --> SMUX(1) --> PMUX(0) --> CPU clk
50 * ACD stands for Adaptive Clock Distribution and is used to
51 * detect voltage droops.
54 #include <linux/bitfield.h>
55 #include <linux/clk.h>
56 #include <linux/clk-provider.h>
58 #include <linux/module.h>
59 #include <linux/platform_device.h>
60 #include <linux/regmap.h>
61 #include <soc/qcom/kryo-l2-accessors.h>
63 #include <asm/cputype.h>
65 #include "clk-alpha-pll.h"
66 #include "clk-regmap.h"
67 #include "clk-regmap-mux.h"
77 #define DIV_2_THRESHOLD 600000000
78 #define PWRCL_REG_OFFSET 0x0
79 #define PERFCL_REG_OFFSET 0x80000
80 #define MUX_OFFSET 0x40
81 #define CLK_CTL_OFFSET 0x44
82 #define CLK_CTL_AUTO_CLK_SEL BIT(8)
83 #define ALT_PLL_OFFSET 0x100
84 #define SSSCTL_OFFSET 0x160
85 #define PSCTL_OFFSET 0x164
88 #define MUX_AUTO_CLK_SEL_ALWAYS_ON_MASK GENMASK(5, 4)
89 #define MUX_AUTO_CLK_SEL_ALWAYS_ON_GPLL0_SEL \
90 FIELD_PREP(MUX_AUTO_CLK_SEL_ALWAYS_ON_MASK, 0x03)
92 static const u8 prim_pll_regs[PLL_OFF_MAX_REGS] = {
93 [PLL_OFF_L_VAL] = 0x04,
94 [PLL_OFF_ALPHA_VAL] = 0x08,
95 [PLL_OFF_USER_CTL] = 0x10,
96 [PLL_OFF_CONFIG_CTL] = 0x18,
97 [PLL_OFF_CONFIG_CTL_U] = 0x1c,
98 [PLL_OFF_TEST_CTL] = 0x20,
99 [PLL_OFF_TEST_CTL_U] = 0x24,
100 [PLL_OFF_STATUS] = 0x28,
103 static const u8 alt_pll_regs[PLL_OFF_MAX_REGS] = {
104 [PLL_OFF_L_VAL] = 0x04,
105 [PLL_OFF_ALPHA_VAL] = 0x08,
106 [PLL_OFF_USER_CTL] = 0x10,
107 [PLL_OFF_CONFIG_CTL] = 0x18,
108 [PLL_OFF_TEST_CTL] = 0x20,
109 [PLL_OFF_STATUS] = 0x28,
114 static const struct alpha_pll_config hfpll_config = {
116 .config_ctl_val = 0x200d4828,
117 .config_ctl_hi_val = 0x006,
118 .test_ctl_val = 0x1c000000,
119 .test_ctl_hi_val = 0x00004000,
120 .pre_div_mask = BIT(12),
121 .post_div_mask = 0x3 << 8,
122 .post_div_val = 0x1 << 8,
123 .main_output_mask = BIT(0),
124 .early_output_mask = BIT(3),
127 static const struct clk_parent_data pll_parent[] = {
131 static struct clk_alpha_pll pwrcl_pll = {
132 .offset = PWRCL_REG_OFFSET,
133 .regs = prim_pll_regs,
134 .flags = SUPPORTS_DYNAMIC_UPDATE | SUPPORTS_FSM_MODE,
135 .clkr.hw.init = &(struct clk_init_data){
137 .parent_data = pll_parent,
138 .num_parents = ARRAY_SIZE(pll_parent),
139 .ops = &clk_alpha_pll_hwfsm_ops,
143 static struct clk_alpha_pll perfcl_pll = {
144 .offset = PERFCL_REG_OFFSET,
145 .regs = prim_pll_regs,
146 .flags = SUPPORTS_DYNAMIC_UPDATE | SUPPORTS_FSM_MODE,
147 .clkr.hw.init = &(struct clk_init_data){
148 .name = "perfcl_pll",
149 .parent_data = pll_parent,
150 .num_parents = ARRAY_SIZE(pll_parent),
151 .ops = &clk_alpha_pll_hwfsm_ops,
155 static struct clk_fixed_factor pwrcl_pll_postdiv = {
158 .hw.init = &(struct clk_init_data){
159 .name = "pwrcl_pll_postdiv",
160 .parent_data = &(const struct clk_parent_data){
161 .hw = &pwrcl_pll.clkr.hw
164 .ops = &clk_fixed_factor_ops,
165 .flags = CLK_SET_RATE_PARENT,
169 static struct clk_fixed_factor perfcl_pll_postdiv = {
172 .hw.init = &(struct clk_init_data){
173 .name = "perfcl_pll_postdiv",
174 .parent_data = &(const struct clk_parent_data){
175 .hw = &perfcl_pll.clkr.hw
178 .ops = &clk_fixed_factor_ops,
179 .flags = CLK_SET_RATE_PARENT,
183 static struct clk_fixed_factor perfcl_pll_acd = {
186 .hw.init = &(struct clk_init_data){
187 .name = "perfcl_pll_acd",
188 .parent_data = &(const struct clk_parent_data){
189 .hw = &perfcl_pll.clkr.hw
192 .ops = &clk_fixed_factor_ops,
193 .flags = CLK_SET_RATE_PARENT,
197 static struct clk_fixed_factor pwrcl_pll_acd = {
200 .hw.init = &(struct clk_init_data){
201 .name = "pwrcl_pll_acd",
202 .parent_data = &(const struct clk_parent_data){
203 .hw = &pwrcl_pll.clkr.hw
206 .ops = &clk_fixed_factor_ops,
207 .flags = CLK_SET_RATE_PARENT,
211 static const struct pll_vco alt_pll_vco_modes[] = {
212 VCO(3, 250000000, 500000000),
213 VCO(2, 500000000, 750000000),
214 VCO(1, 750000000, 1000000000),
215 VCO(0, 1000000000, 2150400000),
218 static const struct alpha_pll_config altpll_config = {
220 .vco_val = 0x3 << 20,
221 .vco_mask = 0x3 << 20,
222 .config_ctl_val = 0x4001051b,
223 .post_div_mask = 0x3 << 8,
224 .post_div_val = 0x1 << 8,
225 .main_output_mask = BIT(0),
226 .early_output_mask = BIT(3),
229 static struct clk_alpha_pll pwrcl_alt_pll = {
230 .offset = PWRCL_REG_OFFSET + ALT_PLL_OFFSET,
231 .regs = alt_pll_regs,
232 .vco_table = alt_pll_vco_modes,
233 .num_vco = ARRAY_SIZE(alt_pll_vco_modes),
234 .flags = SUPPORTS_OFFLINE_REQ | SUPPORTS_FSM_MODE,
235 .clkr.hw.init = &(struct clk_init_data) {
236 .name = "pwrcl_alt_pll",
237 .parent_data = pll_parent,
238 .num_parents = ARRAY_SIZE(pll_parent),
239 .ops = &clk_alpha_pll_hwfsm_ops,
243 static struct clk_alpha_pll perfcl_alt_pll = {
244 .offset = PERFCL_REG_OFFSET + ALT_PLL_OFFSET,
245 .regs = alt_pll_regs,
246 .vco_table = alt_pll_vco_modes,
247 .num_vco = ARRAY_SIZE(alt_pll_vco_modes),
248 .flags = SUPPORTS_OFFLINE_REQ | SUPPORTS_FSM_MODE,
249 .clkr.hw.init = &(struct clk_init_data) {
250 .name = "perfcl_alt_pll",
251 .parent_data = pll_parent,
252 .num_parents = ARRAY_SIZE(pll_parent),
253 .ops = &clk_alpha_pll_hwfsm_ops,
257 struct clk_cpu_8996_pmux {
259 struct notifier_block nb;
260 struct clk_regmap clkr;
263 static int cpu_clk_notifier_cb(struct notifier_block *nb, unsigned long event,
266 #define to_clk_cpu_8996_pmux_nb(_nb) \
267 container_of(_nb, struct clk_cpu_8996_pmux, nb)
269 static inline struct clk_cpu_8996_pmux *to_clk_cpu_8996_pmux_hw(struct clk_hw *hw)
271 return container_of(to_clk_regmap(hw), struct clk_cpu_8996_pmux, clkr);
274 static u8 clk_cpu_8996_pmux_get_parent(struct clk_hw *hw)
276 struct clk_regmap *clkr = to_clk_regmap(hw);
277 struct clk_cpu_8996_pmux *cpuclk = to_clk_cpu_8996_pmux_hw(hw);
280 regmap_read(clkr->regmap, cpuclk->reg, &val);
282 return FIELD_GET(PMUX_MASK, val);
285 static int clk_cpu_8996_pmux_set_parent(struct clk_hw *hw, u8 index)
287 struct clk_regmap *clkr = to_clk_regmap(hw);
288 struct clk_cpu_8996_pmux *cpuclk = to_clk_cpu_8996_pmux_hw(hw);
291 val = FIELD_PREP(PMUX_MASK, index);
293 return regmap_update_bits(clkr->regmap, cpuclk->reg, PMUX_MASK, val);
296 static int clk_cpu_8996_pmux_determine_rate(struct clk_hw *hw,
297 struct clk_rate_request *req)
299 struct clk_hw *parent;
301 if (req->rate < (DIV_2_THRESHOLD / 2))
304 if (req->rate < DIV_2_THRESHOLD)
305 parent = clk_hw_get_parent_by_index(hw, SMUX_INDEX);
307 parent = clk_hw_get_parent_by_index(hw, ACD_INDEX);
311 req->best_parent_rate = clk_hw_round_rate(parent, req->rate);
312 req->best_parent_hw = parent;
317 static const struct clk_ops clk_cpu_8996_pmux_ops = {
318 .set_parent = clk_cpu_8996_pmux_set_parent,
319 .get_parent = clk_cpu_8996_pmux_get_parent,
320 .determine_rate = clk_cpu_8996_pmux_determine_rate,
323 static const struct parent_map smux_parent_map[] = {
324 { .cfg = 0, }, /* xo */
325 { .cfg = 1, }, /* pll */
326 { .cfg = 3, }, /* sys_apcs_aux */
329 static const struct clk_parent_data pwrcl_smux_parents[] = {
331 { .hw = &pwrcl_pll_postdiv.hw },
332 { .fw_name = "sys_apcs_aux" },
335 static const struct clk_parent_data perfcl_smux_parents[] = {
337 { .hw = &perfcl_pll_postdiv.hw },
338 { .fw_name = "sys_apcs_aux" },
341 static struct clk_regmap_mux pwrcl_smux = {
342 .reg = PWRCL_REG_OFFSET + MUX_OFFSET,
345 .parent_map = smux_parent_map,
346 .clkr.hw.init = &(struct clk_init_data) {
347 .name = "pwrcl_smux",
348 .parent_data = pwrcl_smux_parents,
349 .num_parents = ARRAY_SIZE(pwrcl_smux_parents),
350 .ops = &clk_regmap_mux_closest_ops,
351 .flags = CLK_SET_RATE_PARENT,
355 static struct clk_regmap_mux perfcl_smux = {
356 .reg = PERFCL_REG_OFFSET + MUX_OFFSET,
359 .parent_map = smux_parent_map,
360 .clkr.hw.init = &(struct clk_init_data) {
361 .name = "perfcl_smux",
362 .parent_data = perfcl_smux_parents,
363 .num_parents = ARRAY_SIZE(perfcl_smux_parents),
364 .ops = &clk_regmap_mux_closest_ops,
365 .flags = CLK_SET_RATE_PARENT,
369 static const struct clk_hw *pwrcl_pmux_parents[] = {
370 [SMUX_INDEX] = &pwrcl_smux.clkr.hw,
371 [PLL_INDEX] = &pwrcl_pll.clkr.hw,
372 [ACD_INDEX] = &pwrcl_pll_acd.hw,
373 [ALT_INDEX] = &pwrcl_alt_pll.clkr.hw,
376 static const struct clk_hw *perfcl_pmux_parents[] = {
377 [SMUX_INDEX] = &perfcl_smux.clkr.hw,
378 [PLL_INDEX] = &perfcl_pll.clkr.hw,
379 [ACD_INDEX] = &perfcl_pll_acd.hw,
380 [ALT_INDEX] = &perfcl_alt_pll.clkr.hw,
383 static struct clk_cpu_8996_pmux pwrcl_pmux = {
384 .reg = PWRCL_REG_OFFSET + MUX_OFFSET,
385 .nb.notifier_call = cpu_clk_notifier_cb,
386 .clkr.hw.init = &(struct clk_init_data) {
387 .name = "pwrcl_pmux",
388 .parent_hws = pwrcl_pmux_parents,
389 .num_parents = ARRAY_SIZE(pwrcl_pmux_parents),
390 .ops = &clk_cpu_8996_pmux_ops,
391 /* CPU clock is critical and should never be gated */
392 .flags = CLK_SET_RATE_PARENT | CLK_IS_CRITICAL,
396 static struct clk_cpu_8996_pmux perfcl_pmux = {
397 .reg = PERFCL_REG_OFFSET + MUX_OFFSET,
398 .nb.notifier_call = cpu_clk_notifier_cb,
399 .clkr.hw.init = &(struct clk_init_data) {
400 .name = "perfcl_pmux",
401 .parent_hws = perfcl_pmux_parents,
402 .num_parents = ARRAY_SIZE(perfcl_pmux_parents),
403 .ops = &clk_cpu_8996_pmux_ops,
404 /* CPU clock is critical and should never be gated */
405 .flags = CLK_SET_RATE_PARENT | CLK_IS_CRITICAL,
409 static const struct regmap_config cpu_msm8996_regmap_config = {
413 .max_register = 0x80210,
415 .val_format_endian = REGMAP_ENDIAN_LITTLE,
418 static struct clk_hw *cpu_msm8996_hw_clks[] = {
419 &pwrcl_pll_postdiv.hw,
420 &perfcl_pll_postdiv.hw,
425 static struct clk_regmap *cpu_msm8996_clks[] = {
429 &perfcl_alt_pll.clkr,
436 static void qcom_cpu_clk_msm8996_acd_init(struct regmap *regmap);
438 static int qcom_cpu_clk_msm8996_register_clks(struct device *dev,
439 struct regmap *regmap)
443 /* Select GPLL0 for 300MHz for both clusters */
444 regmap_write(regmap, PERFCL_REG_OFFSET + MUX_OFFSET, 0xc);
445 regmap_write(regmap, PWRCL_REG_OFFSET + MUX_OFFSET, 0xc);
447 /* Ensure write goes through before PLLs are reconfigured */
450 /* Set the auto clock sel always-on source to GPLL0/2 (300MHz) */
451 regmap_update_bits(regmap, PWRCL_REG_OFFSET + MUX_OFFSET,
452 MUX_AUTO_CLK_SEL_ALWAYS_ON_MASK,
453 MUX_AUTO_CLK_SEL_ALWAYS_ON_GPLL0_SEL);
454 regmap_update_bits(regmap, PERFCL_REG_OFFSET + MUX_OFFSET,
455 MUX_AUTO_CLK_SEL_ALWAYS_ON_MASK,
456 MUX_AUTO_CLK_SEL_ALWAYS_ON_GPLL0_SEL);
458 clk_alpha_pll_configure(&pwrcl_pll, regmap, &hfpll_config);
459 clk_alpha_pll_configure(&perfcl_pll, regmap, &hfpll_config);
460 clk_alpha_pll_configure(&pwrcl_alt_pll, regmap, &altpll_config);
461 clk_alpha_pll_configure(&perfcl_alt_pll, regmap, &altpll_config);
463 /* Wait for PLL(s) to lock */
466 /* Enable auto clock selection for both clusters */
467 regmap_update_bits(regmap, PWRCL_REG_OFFSET + CLK_CTL_OFFSET,
468 CLK_CTL_AUTO_CLK_SEL, CLK_CTL_AUTO_CLK_SEL);
469 regmap_update_bits(regmap, PERFCL_REG_OFFSET + CLK_CTL_OFFSET,
470 CLK_CTL_AUTO_CLK_SEL, CLK_CTL_AUTO_CLK_SEL);
472 /* Ensure write goes through before muxes are switched */
475 qcom_cpu_clk_msm8996_acd_init(regmap);
477 /* Pulse swallower and soft-start settings */
478 regmap_write(regmap, PWRCL_REG_OFFSET + PSCTL_OFFSET, 0x00030005);
479 regmap_write(regmap, PERFCL_REG_OFFSET + PSCTL_OFFSET, 0x00030005);
481 /* Switch clusters to use the ACD leg */
482 regmap_write(regmap, PWRCL_REG_OFFSET + MUX_OFFSET, 0x32);
483 regmap_write(regmap, PERFCL_REG_OFFSET + MUX_OFFSET, 0x32);
485 for (i = 0; i < ARRAY_SIZE(cpu_msm8996_hw_clks); i++) {
486 ret = devm_clk_hw_register(dev, cpu_msm8996_hw_clks[i]);
491 for (i = 0; i < ARRAY_SIZE(cpu_msm8996_clks); i++) {
492 ret = devm_clk_register_regmap(dev, cpu_msm8996_clks[i]);
497 /* Enable alt PLLs */
498 clk_prepare_enable(pwrcl_alt_pll.clkr.hw.clk);
499 clk_prepare_enable(perfcl_alt_pll.clkr.hw.clk);
501 devm_clk_notifier_register(dev, pwrcl_pmux.clkr.hw.clk, &pwrcl_pmux.nb);
502 devm_clk_notifier_register(dev, perfcl_pmux.clkr.hw.clk, &perfcl_pmux.nb);
507 #define CPU_CLUSTER_AFFINITY_MASK 0xf00
508 #define PWRCL_AFFINITY_MASK 0x000
509 #define PERFCL_AFFINITY_MASK 0x100
511 #define L2ACDCR_REG 0x580ULL
512 #define L2ACDTD_REG 0x581ULL
513 #define L2ACDDVMRC_REG 0x584ULL
514 #define L2ACDSSCR_REG 0x589ULL
516 static DEFINE_SPINLOCK(qcom_clk_acd_lock);
518 static void qcom_cpu_clk_msm8996_acd_init(struct regmap *regmap)
524 spin_lock_irqsave(&qcom_clk_acd_lock, flags);
526 val = kryo_l2_get_indirect_reg(L2ACDTD_REG);
527 if (val == 0x00006a11)
530 kryo_l2_set_indirect_reg(L2ACDTD_REG, 0x00006a11);
531 kryo_l2_set_indirect_reg(L2ACDDVMRC_REG, 0x000e0f0f);
532 kryo_l2_set_indirect_reg(L2ACDSSCR_REG, 0x00000601);
534 kryo_l2_set_indirect_reg(L2ACDCR_REG, 0x002c5ffd);
536 hwid = read_cpuid_mpidr();
537 if ((hwid & CPU_CLUSTER_AFFINITY_MASK) == PWRCL_AFFINITY_MASK)
538 regmap_write(regmap, PWRCL_REG_OFFSET + SSSCTL_OFFSET, 0xf);
540 regmap_write(regmap, PERFCL_REG_OFFSET + SSSCTL_OFFSET, 0xf);
543 spin_unlock_irqrestore(&qcom_clk_acd_lock, flags);
546 static int cpu_clk_notifier_cb(struct notifier_block *nb, unsigned long event,
549 struct clk_cpu_8996_pmux *cpuclk = to_clk_cpu_8996_pmux_nb(nb);
550 struct clk_notifier_data *cnd = data;
553 case PRE_RATE_CHANGE:
554 qcom_cpu_clk_msm8996_acd_init(cpuclk->clkr.regmap);
557 * Avoid overvolting. clk_core_set_rate_nolock() walks from top
558 * to bottom, so it will change the rate of the PLL before
559 * chaging the parent of PMUX. This can result in pmux getting
560 * clocked twice the expected rate.
562 * Manually switch to PLL/2 here.
564 if (cnd->new_rate < DIV_2_THRESHOLD &&
565 cnd->old_rate > DIV_2_THRESHOLD)
566 clk_cpu_8996_pmux_set_parent(&cpuclk->clkr.hw, SMUX_INDEX);
569 case ABORT_RATE_CHANGE:
570 /* Revert manual change */
571 if (cnd->new_rate < DIV_2_THRESHOLD &&
572 cnd->old_rate > DIV_2_THRESHOLD)
573 clk_cpu_8996_pmux_set_parent(&cpuclk->clkr.hw, ACD_INDEX);
582 static int qcom_cpu_clk_msm8996_driver_probe(struct platform_device *pdev)
584 static void __iomem *base;
585 struct regmap *regmap;
586 struct clk_hw_onecell_data *data;
587 struct device *dev = &pdev->dev;
590 data = devm_kzalloc(dev, struct_size(data, hws, 2), GFP_KERNEL);
594 base = devm_platform_ioremap_resource(pdev, 0);
596 return PTR_ERR(base);
598 regmap = devm_regmap_init_mmio(dev, base, &cpu_msm8996_regmap_config);
600 return PTR_ERR(regmap);
602 ret = qcom_cpu_clk_msm8996_register_clks(dev, regmap);
606 data->hws[0] = &pwrcl_pmux.clkr.hw;
607 data->hws[1] = &perfcl_pmux.clkr.hw;
610 return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, data);
613 static const struct of_device_id qcom_cpu_clk_msm8996_match_table[] = {
614 { .compatible = "qcom,msm8996-apcc" },
617 MODULE_DEVICE_TABLE(of, qcom_cpu_clk_msm8996_match_table);
619 static struct platform_driver qcom_cpu_clk_msm8996_driver = {
620 .probe = qcom_cpu_clk_msm8996_driver_probe,
622 .name = "qcom-msm8996-apcc",
623 .of_match_table = qcom_cpu_clk_msm8996_match_table,
626 module_platform_driver(qcom_cpu_clk_msm8996_driver);
628 MODULE_DESCRIPTION("QCOM MSM8996 CPU Clock Driver");
629 MODULE_LICENSE("GPL v2");