1 // SPDX-License-Identifier: BSD-3-Clause
3 * Clock drivers for Qualcomm APQ8016, APQ8096
7 * Based on Little Kernel driver, simplified
11 #include <clk-uclass.h>
15 #include <linux/bitops.h>
16 #include "clock-snapdragon.h"
18 /* CBCR register fields */
19 #define CBCR_BRANCH_ENABLE_BIT BIT(0)
20 #define CBCR_BRANCH_OFF_BIT BIT(31)
22 extern ulong msm_set_rate(struct clk *clk, ulong rate);
23 extern int msm_enable(struct clk *clk);
25 /* Enable clock controlled by CBC soft macro */
26 void clk_enable_cbc(phys_addr_t cbcr)
28 setbits_le32(cbcr, CBCR_BRANCH_ENABLE_BIT);
30 while (readl(cbcr) & CBCR_BRANCH_OFF_BIT)
34 void clk_enable_gpll0(phys_addr_t base, const struct pll_vote_clk *gpll0)
36 if (readl(base + gpll0->status) & gpll0->status_bit)
37 return; /* clock already enabled */
39 setbits_le32(base + gpll0->ena_vote, gpll0->vote_bit);
41 while ((readl(base + gpll0->status) & gpll0->status_bit) == 0)
45 #define BRANCH_ON_VAL (0)
46 #define BRANCH_NOC_FSM_ON_VAL BIT(29)
47 #define BRANCH_CHECK_MASK GENMASK(31, 28)
49 void clk_enable_vote_clk(phys_addr_t base, const struct vote_clk *vclk)
53 setbits_le32(base + vclk->ena_vote, vclk->vote_bit);
55 val = readl(base + vclk->cbcr_reg);
56 val &= BRANCH_CHECK_MASK;
57 } while ((val != BRANCH_ON_VAL) && (val != BRANCH_NOC_FSM_ON_VAL));
60 #define APPS_CMD_RCGR_UPDATE BIT(0)
62 /* Update clock command via CMD_RCGR */
63 void clk_bcr_update(phys_addr_t apps_cmd_rcgr)
65 setbits_le32(apps_cmd_rcgr, APPS_CMD_RCGR_UPDATE);
67 /* Wait for frequency to be updated. */
68 while (readl(apps_cmd_rcgr) & APPS_CMD_RCGR_UPDATE)
72 #define CFG_MODE_DUAL_EDGE (0x2 << 12) /* Counter mode */
74 #define CFG_MASK 0x3FFF
76 #define CFG_DIVIDER_MASK 0x1F
78 /* root set rate for clocks with half integer and MND divider */
79 void clk_rcg_set_rate_mnd(phys_addr_t base, const struct bcr_regs *regs,
80 int div, int m, int n, int source)
83 /* M value for MND divider. */
85 /* NOT(N-M) value for MND divider. */
86 u32 n_val = ~((n) - (m)) * !!(n);
87 /* NOT 2D value for MND divider. */
90 /* Program MND values */
91 writel(m_val, base + regs->M);
92 writel(n_val, base + regs->N);
93 writel(d_val, base + regs->D);
95 /* setup src select and divider */
96 cfg = readl(base + regs->cfg_rcgr);
98 cfg |= source & CFG_CLK_SRC_MASK; /* Select clock source */
100 /* Set the divider; HW permits fraction dividers (+0.5), but
101 for simplicity, we will support integers only */
103 cfg |= (2 * div - 1) & CFG_DIVIDER_MASK;
106 cfg |= CFG_MODE_DUAL_EDGE;
108 writel(cfg, base + regs->cfg_rcgr); /* Write new clock configuration */
110 /* Inform h/w to start using the new config. */
111 clk_bcr_update(base + regs->cmd_rcgr);
114 /* root set rate for clocks with half integer and mnd_width=0 */
115 void clk_rcg_set_rate(phys_addr_t base, const struct bcr_regs *regs, int div,
120 /* setup src select and divider */
121 cfg = readl(base + regs->cfg_rcgr);
123 cfg |= source & CFG_CLK_SRC_MASK; /* Select clock source */
126 * Set the divider; HW permits fraction dividers (+0.5), but
127 * for simplicity, we will support integers only
130 cfg |= (2 * div - 1) & CFG_DIVIDER_MASK;
132 writel(cfg, base + regs->cfg_rcgr); /* Write new clock configuration */
134 /* Inform h/w to start using the new config. */
135 clk_bcr_update(base + regs->cmd_rcgr);
138 static int msm_clk_probe(struct udevice *dev)
140 struct msm_clk_priv *priv = dev_get_priv(dev);
142 priv->base = dev_read_addr(dev);
143 if (priv->base == FDT_ADDR_T_NONE)
149 static ulong msm_clk_set_rate(struct clk *clk, ulong rate)
151 return msm_set_rate(clk, rate);
154 static int msm_clk_enable(struct clk *clk)
156 return msm_enable(clk);
159 static struct clk_ops msm_clk_ops = {
160 .set_rate = msm_clk_set_rate,
161 .enable = msm_clk_enable,
164 static const struct udevice_id msm_clk_ids[] = {
165 { .compatible = "qcom,gcc-msm8916" },
166 { .compatible = "qcom,gcc-apq8016" },
167 { .compatible = "qcom,gcc-msm8996" },
168 { .compatible = "qcom,gcc-apq8096" },
169 { .compatible = "qcom,gcc-sdm845" },
170 { .compatible = "qcom,gcc-qcs404" },
174 U_BOOT_DRIVER(clk_msm) = {
177 .of_match = msm_clk_ids,
179 .priv_auto = sizeof(struct msm_clk_priv),
180 .probe = msm_clk_probe,