1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2022 MediaTek Inc.
8 #include <linux/iopoll.h>
11 #include "clk-pllfh.h"
12 #include "clk-fhctl.h"
14 #define PERCENT_TO_DDSLMT(dds, percent_m10) \
15 ((((dds) * (percent_m10)) >> 5) / 100)
17 static const struct fhctl_offset fhctl_offset = {
19 .offset_clk_con = 0x8,
20 .offset_rst_con = 0xc,
21 .offset_slope0 = 0x10,
22 .offset_slope1 = 0x14,
24 .offset_updnlmt = 0x4,
30 const struct fhctl_offset *fhctl_get_offset_table(void)
35 static void dump_hw(struct mtk_clk_pll *pll, struct fh_pll_regs *regs,
36 const struct fh_pll_data *data)
38 pr_info("hp_en<%x>,clk_con<%x>,slope0<%x>,slope1<%x>\n",
39 readl(regs->reg_hp_en), readl(regs->reg_clk_con),
40 readl(regs->reg_slope0), readl(regs->reg_slope1));
41 pr_info("cfg<%x>,lmt<%x>,dds<%x>,dvfs<%x>,mon<%x>\n",
42 readl(regs->reg_cfg), readl(regs->reg_updnlmt),
43 readl(regs->reg_dds), readl(regs->reg_dvfs),
44 readl(regs->reg_mon));
45 pr_info("pcw<%x>\n", readl(pll->pcw_addr));
48 static int fhctl_set_ssc_regs(struct mtk_clk_pll *pll, struct fh_pll_regs *regs,
49 const struct fh_pll_data *data, u32 rate)
53 writel((readl(regs->reg_cfg) & ~(data->frddsx_en)), regs->reg_cfg);
54 writel((readl(regs->reg_cfg) & ~(data->sfstrx_en)), regs->reg_cfg);
55 writel((readl(regs->reg_cfg) & ~(data->fhctlx_en)), regs->reg_cfg);
58 /* Set the relative parameter registers (dt/df/upbnd/downbnd) */
59 r = readl(regs->reg_cfg);
60 r &= ~(data->msk_frddsx_dys);
61 r |= (data->df_val << (ffs(data->msk_frddsx_dys) - 1));
62 writel(r, regs->reg_cfg);
64 r = readl(regs->reg_cfg);
65 r &= ~(data->msk_frddsx_dts);
66 r |= (data->dt_val << (ffs(data->msk_frddsx_dts) - 1));
67 writel(r, regs->reg_cfg);
69 writel((readl(pll->pcw_addr) & data->dds_mask) | data->tgl_org,
72 /* Calculate UPDNLMT */
73 updnlmt_val = PERCENT_TO_DDSLMT((readl(regs->reg_dds) &
74 data->dds_mask), rate) <<
77 writel(updnlmt_val, regs->reg_updnlmt);
78 writel(readl(regs->reg_hp_en) | BIT(data->fh_id),
81 writel(readl(regs->reg_cfg) | data->frddsx_en, regs->reg_cfg);
82 /* Enable Hopping control */
83 writel(readl(regs->reg_cfg) | data->fhctlx_en, regs->reg_cfg);
86 /* Switch to APMIXEDSYS control */
87 writel(readl(regs->reg_hp_en) & ~BIT(data->fh_id),
89 /* Wait for DDS to be stable */
96 static int hopping_hw_flow(struct mtk_clk_pll *pll, struct fh_pll_regs *regs,
97 const struct fh_pll_data *data,
98 struct fh_pll_state *state, unsigned int new_dds)
100 u32 dds_mask = data->dds_mask;
106 fhctl_set_ssc_regs(pll, regs, data, 0);
108 writel((readl(pll->pcw_addr) & dds_mask) | data->tgl_org,
111 writel(readl(regs->reg_cfg) | data->sfstrx_en, regs->reg_cfg);
112 writel(readl(regs->reg_cfg) | data->fhctlx_en, regs->reg_cfg);
113 writel(data->slope0_value, regs->reg_slope0);
114 writel(data->slope1_value, regs->reg_slope1);
116 writel(readl(regs->reg_hp_en) | BIT(data->fh_id), regs->reg_hp_en);
117 writel((new_dds) | (data->dvfs_tri), regs->reg_dvfs);
119 /* Wait 1000 us until DDS stable */
120 ret = readl_poll_timeout_atomic(regs->reg_mon, mon_dds,
121 (mon_dds & dds_mask) == new_dds,
124 pr_warn("%s: FHCTL hopping timeout\n", pll->data->name);
125 dump_hw(pll, regs, data);
128 con_pcw_tmp = readl(pll->pcw_addr) & (~dds_mask);
129 con_pcw_tmp = (con_pcw_tmp | (readl(regs->reg_mon) & dds_mask) |
132 writel(con_pcw_tmp, pll->pcw_addr);
133 writel(readl(regs->reg_hp_en) & ~BIT(data->fh_id), regs->reg_hp_en);
136 fhctl_set_ssc_regs(pll, regs, data, state->ssc_rate);
141 static unsigned int __get_postdiv(struct mtk_clk_pll *pll)
145 regval = readl(pll->pd_addr) >> pll->data->pd_shift;
146 regval &= POSTDIV_MASK;
151 static void __set_postdiv(struct mtk_clk_pll *pll, unsigned int postdiv)
155 regval = readl(pll->pd_addr);
156 regval &= ~(POSTDIV_MASK << pll->data->pd_shift);
157 regval |= (ffs(postdiv) - 1) << pll->data->pd_shift;
158 writel(regval, pll->pd_addr);
161 static int fhctl_hopping(struct mtk_fh *fh, unsigned int new_dds,
162 unsigned int postdiv)
164 const struct fh_pll_data *data = &fh->pllfh_data->data;
165 struct fh_pll_state *state = &fh->pllfh_data->state;
166 struct fh_pll_regs *regs = &fh->regs;
167 struct mtk_clk_pll *pll = &fh->clk_pll;
168 spinlock_t *lock = fh->lock;
169 unsigned int pll_postdiv;
170 unsigned long flags = 0;
174 pll_postdiv = __get_postdiv(pll);
176 if (postdiv > pll_postdiv)
177 __set_postdiv(pll, postdiv);
180 spin_lock_irqsave(lock, flags);
182 ret = hopping_hw_flow(pll, regs, data, state, new_dds);
184 spin_unlock_irqrestore(lock, flags);
186 if (postdiv && postdiv < pll_postdiv)
187 __set_postdiv(pll, postdiv);
192 static int fhctl_ssc_enable(struct mtk_fh *fh, u32 rate)
194 const struct fh_pll_data *data = &fh->pllfh_data->data;
195 struct fh_pll_state *state = &fh->pllfh_data->state;
196 struct fh_pll_regs *regs = &fh->regs;
197 struct mtk_clk_pll *pll = &fh->clk_pll;
198 spinlock_t *lock = fh->lock;
199 unsigned long flags = 0;
201 spin_lock_irqsave(lock, flags);
203 fhctl_set_ssc_regs(pll, regs, data, rate);
204 state->ssc_rate = rate;
206 spin_unlock_irqrestore(lock, flags);
211 static const struct fh_operation fhctl_ops = {
212 .hopping = fhctl_hopping,
213 .ssc_enable = fhctl_ssc_enable,
216 const struct fh_operation *fhctl_get_ops(void)
221 void fhctl_hw_init(struct mtk_fh *fh)
223 const struct fh_pll_data data = fh->pllfh_data->data;
224 struct fh_pll_state state = fh->pllfh_data->state;
225 struct fh_pll_regs regs = fh->regs;
228 /* initial hw register */
229 val = readl(regs.reg_clk_con) | BIT(data.fh_id);
230 writel(val, regs.reg_clk_con);
232 val = readl(regs.reg_rst_con) & ~BIT(data.fh_id);
233 writel(val, regs.reg_rst_con);
234 val = readl(regs.reg_rst_con) | BIT(data.fh_id);
235 writel(val, regs.reg_rst_con);
237 writel(0x0, regs.reg_cfg);
238 writel(0x0, regs.reg_updnlmt);
239 writel(0x0, regs.reg_dds);
241 /* enable ssc if needed */
243 fh->ops->ssc_enable(fh, state.ssc_rate);