blob: 8f42a5cb1b72d62c951d8c7f5e4bb7052e6e553b [file] [log] [blame]
Sébastien Szymanski8d163f52023-07-25 10:08:53 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2021 NXP
4 */
5
Sébastien Szymanski8d163f52023-07-25 10:08:53 +02006#include <asm/io.h>
7#include <malloc.h>
8#include <clk-uclass.h>
9#include <dm/device.h>
10#include <dm/devres.h>
11#include <linux/bitfield.h>
12#include <linux/bitops.h>
13#include <linux/clk-provider.h>
14#include <linux/delay.h>
15#include <linux/err.h>
16#include <linux/iopoll.h>
17#include <clk.h>
18#include <div64.h>
19
20#include "clk.h"
21
22#define UBOOT_DM_CLK_IMX_FRACN_GPPLL "imx_clk_fracn_gppll"
23
24#define PLL_CTRL 0x0
25#define HW_CTRL_SEL BIT(16)
26#define CLKMUX_BYPASS BIT(2)
27#define CLKMUX_EN BIT(1)
28#define POWERUP_MASK BIT(0)
29
30#define PLL_ANA_PRG 0x10
31#define PLL_SPREAD_SPECTRUM 0x30
32
33#define PLL_NUMERATOR 0x40
34#define PLL_MFN_MASK GENMASK(31, 2)
35
36#define PLL_DENOMINATOR 0x50
37#define PLL_MFD_MASK GENMASK(29, 0)
38
39#define PLL_DIV 0x60
40#define PLL_MFI_MASK GENMASK(24, 16)
41#define PLL_RDIV_MASK GENMASK(15, 13)
42#define PLL_ODIV_MASK GENMASK(7, 0)
43
44#define PLL_DFS_CTRL(x) (0x70 + (x) * 0x10)
45
46#define PLL_STATUS 0xF0
47#define LOCK_STATUS BIT(0)
48
49#define DFS_STATUS 0xF4
50
51#define LOCK_TIMEOUT_US 200
52
53#define PLL_FRACN_GP(_rate, _mfi, _mfn, _mfd, _rdiv, _odiv) \
54 { \
55 .rate = (_rate), \
56 .mfi = (_mfi), \
57 .mfn = (_mfn), \
58 .mfd = (_mfd), \
59 .rdiv = (_rdiv), \
60 .odiv = (_odiv), \
61 }
62
63#define PLL_FRACN_GP_INTEGER(_rate, _mfi, _rdiv, _odiv) \
64 { \
65 .rate = (_rate), \
66 .mfi = (_mfi), \
67 .mfn = 0, \
68 .mfd = 0, \
69 .rdiv = (_rdiv), \
70 .odiv = (_odiv), \
71 }
72
73struct clk_fracn_gppll {
74 struct clk clk;
75 void __iomem *base;
76 const struct imx_fracn_gppll_rate_table *rate_table;
77 int rate_count;
78 u32 flags;
79};
80
81/*
82 * Fvco = (Fref / rdiv) * (MFI + MFN / MFD)
83 * Fout = Fvco / odiv
84 * The (Fref / rdiv) should be in range 20MHz to 40MHz
85 * The Fvco should be in range 2.5Ghz to 5Ghz
86 */
87static const struct imx_fracn_gppll_rate_table fracn_tbl[] = {
88 PLL_FRACN_GP(650000000U, 162, 50, 100, 0, 6),
89 PLL_FRACN_GP(594000000U, 198, 0, 1, 0, 8),
90 PLL_FRACN_GP(560000000U, 140, 0, 1, 0, 6),
91 PLL_FRACN_GP(498000000U, 166, 0, 1, 0, 8),
92 PLL_FRACN_GP(484000000U, 121, 0, 1, 0, 6),
93 PLL_FRACN_GP(445333333U, 167, 0, 1, 0, 9),
94 PLL_FRACN_GP(400000000U, 200, 0, 1, 0, 12),
95 PLL_FRACN_GP(393216000U, 163, 84, 100, 0, 10),
96 PLL_FRACN_GP(300000000U, 150, 0, 1, 0, 12)
97};
98
99struct imx_fracn_gppll_clk imx_fracn_gppll = {
100 .rate_table = fracn_tbl,
101 .rate_count = ARRAY_SIZE(fracn_tbl),
102};
103
104/*
105 * Fvco = (Fref / rdiv) * MFI
106 * Fout = Fvco / odiv
107 * The (Fref / rdiv) should be in range 20MHz to 40MHz
108 * The Fvco should be in range 2.5Ghz to 5Ghz
109 */
110static const struct imx_fracn_gppll_rate_table int_tbl[] = {
111 PLL_FRACN_GP_INTEGER(1700000000U, 141, 1, 2),
112 PLL_FRACN_GP_INTEGER(1400000000U, 175, 1, 3),
113 PLL_FRACN_GP_INTEGER(900000000U, 150, 1, 4),
114};
115
116struct imx_fracn_gppll_clk imx_fracn_gppll_integer = {
117 .rate_table = int_tbl,
118 .rate_count = ARRAY_SIZE(int_tbl),
119};
120
121#define to_clk_fracn_gppll(_clk) container_of(_clk, struct clk_fracn_gppll, clk)
122
123static const struct imx_fracn_gppll_rate_table *
124imx_get_pll_settings(struct clk_fracn_gppll *pll, unsigned long rate)
125{
126 const struct imx_fracn_gppll_rate_table *rate_table = pll->rate_table;
127 int i;
128
129 for (i = 0; i < pll->rate_count; i++)
130 if (rate == rate_table[i].rate)
131 return &rate_table[i];
132
133 return NULL;
134}
135
136static unsigned long clk_fracn_gppll_round_rate(struct clk *clk, unsigned long rate)
137{
138 struct clk_fracn_gppll *pll = to_clk_fracn_gppll(clk);
139 const struct imx_fracn_gppll_rate_table *rate_table = pll->rate_table;
140 int i;
141
142 /* Assuming rate_table is in descending order */
143 for (i = 0; i < pll->rate_count; i++)
144 if (rate >= rate_table[i].rate)
145 return rate_table[i].rate;
146
147 /* return minimum supported value */
148 return rate_table[pll->rate_count - 1].rate;
149}
150
151static unsigned long clk_fracn_gppll_recalc_rate(struct clk *clk)
152{
153 struct clk_fracn_gppll *pll = to_clk_fracn_gppll(clk);
154 const struct imx_fracn_gppll_rate_table *rate_table = pll->rate_table;
155 u32 pll_numerator, pll_denominator, pll_div;
156 u32 mfi, mfn, mfd, rdiv, odiv;
157 u64 fvco = clk_get_parent_rate(clk);
158 long rate = 0;
159 int i;
160
161 pll_numerator = readl_relaxed(pll->base + PLL_NUMERATOR);
162 mfn = FIELD_GET(PLL_MFN_MASK, pll_numerator);
163
164 pll_denominator = readl_relaxed(pll->base + PLL_DENOMINATOR);
165 mfd = FIELD_GET(PLL_MFD_MASK, pll_denominator);
166
167 pll_div = readl_relaxed(pll->base + PLL_DIV);
168 mfi = FIELD_GET(PLL_MFI_MASK, pll_div);
169
170 rdiv = FIELD_GET(PLL_RDIV_MASK, pll_div);
171 odiv = FIELD_GET(PLL_ODIV_MASK, pll_div);
172
173 /*
174 * Sometimes, the recalculated rate has deviation due to
175 * the frac part. So find the accurate pll rate from the table
176 * first, if no match rate in the table, use the rate calculated
177 * from the equation below.
178 */
179 for (i = 0; i < pll->rate_count; i++) {
180 if (rate_table[i].mfn == mfn && rate_table[i].mfi == mfi &&
181 rate_table[i].mfd == mfd && rate_table[i].rdiv == rdiv &&
182 rate_table[i].odiv == odiv)
183 rate = rate_table[i].rate;
184 }
185
186 if (rate)
187 return (unsigned long)rate;
188
189 if (!rdiv)
190 rdiv = rdiv + 1;
191
192 switch (odiv) {
193 case 0:
194 odiv = 2;
195 break;
196 case 1:
197 odiv = 3;
198 break;
199 default:
200 break;
201 }
202
203 if (pll->flags & CLK_FRACN_GPPLL_INTEGER) {
204 /* Fvco = (Fref / rdiv) * MFI */
205 fvco = fvco * mfi;
206 do_div(fvco, rdiv * odiv);
207 } else {
208 /* Fvco = (Fref / rdiv) * (MFI + MFN / MFD) */
209 fvco = fvco * mfi * mfd + fvco * mfn;
210 do_div(fvco, mfd * rdiv * odiv);
211 }
212
213 return (unsigned long)fvco;
214}
215
216static int clk_fracn_gppll_wait_lock(struct clk_fracn_gppll *pll)
217{
218 u32 val;
219
220 return readl_poll_timeout(pll->base + PLL_STATUS, val,
221 val & LOCK_STATUS, LOCK_TIMEOUT_US);
222}
223
224static ulong clk_fracn_gppll_set_rate(struct clk *clk, unsigned long drate)
225{
226 struct clk_fracn_gppll *pll = to_clk_fracn_gppll(clk);
227 const struct imx_fracn_gppll_rate_table *rate;
228 u32 tmp, pll_div, ana_mfn;
229 int ret;
230
231 rate = imx_get_pll_settings(pll, drate);
232
233 /* Hardware control select disable. PLL is control by register */
234 tmp = readl_relaxed(pll->base + PLL_CTRL);
235 tmp &= ~HW_CTRL_SEL;
236 writel_relaxed(tmp, pll->base + PLL_CTRL);
237
238 /* Disable output */
239 tmp = readl_relaxed(pll->base + PLL_CTRL);
240 tmp &= ~CLKMUX_EN;
241 writel_relaxed(tmp, pll->base + PLL_CTRL);
242
243 /* Power Down */
244 tmp &= ~POWERUP_MASK;
245 writel_relaxed(tmp, pll->base + PLL_CTRL);
246
247 /* Disable BYPASS */
248 tmp &= ~CLKMUX_BYPASS;
249 writel_relaxed(tmp, pll->base + PLL_CTRL);
250
251 pll_div = FIELD_PREP(PLL_RDIV_MASK, rate->rdiv) | rate->odiv |
252 FIELD_PREP(PLL_MFI_MASK, rate->mfi);
253 writel_relaxed(pll_div, pll->base + PLL_DIV);
254 if (pll->flags & CLK_FRACN_GPPLL_FRACN) {
255 writel_relaxed(rate->mfd, pll->base + PLL_DENOMINATOR);
256 writel_relaxed(FIELD_PREP(PLL_MFN_MASK, rate->mfn), pll->base + PLL_NUMERATOR);
257 }
258
259 /* Wait for 5us according to fracn mode pll doc */
260 udelay(5);
261
262 /* Enable Powerup */
263 tmp |= POWERUP_MASK;
264 writel_relaxed(tmp, pll->base + PLL_CTRL);
265
266 /* Wait Lock */
267 ret = clk_fracn_gppll_wait_lock(pll);
268 if (ret)
269 return ret;
270
271 /* Enable output */
272 tmp |= CLKMUX_EN;
273 writel_relaxed(tmp, pll->base + PLL_CTRL);
274
275 ana_mfn = readl_relaxed(pll->base + PLL_STATUS);
276 ana_mfn = FIELD_GET(PLL_MFN_MASK, ana_mfn);
277
278 WARN(ana_mfn != rate->mfn, "ana_mfn != rate->mfn\n");
279
280 return 0;
281}
282
283static int clk_fracn_gppll_prepare(struct clk *clk)
284{
285 struct clk_fracn_gppll *pll = to_clk_fracn_gppll(clk);
286 u32 val;
287 int ret;
288
289 val = readl_relaxed(pll->base + PLL_CTRL);
290 if (val & POWERUP_MASK)
291 return 0;
292
293 val |= CLKMUX_BYPASS;
294 writel_relaxed(val, pll->base + PLL_CTRL);
295
296 val |= POWERUP_MASK;
297 writel_relaxed(val, pll->base + PLL_CTRL);
298
299 val |= CLKMUX_EN;
300 writel_relaxed(val, pll->base + PLL_CTRL);
301
302 ret = clk_fracn_gppll_wait_lock(pll);
303 if (ret)
304 return ret;
305
306 val &= ~CLKMUX_BYPASS;
307 writel_relaxed(val, pll->base + PLL_CTRL);
308
309 return 0;
310}
311
312static int clk_fracn_gppll_unprepare(struct clk *clk)
313{
314 struct clk_fracn_gppll *pll = to_clk_fracn_gppll(dev_get_clk_ptr(clk->dev));
315 u32 val;
316
317 val = readl_relaxed(pll->base + PLL_CTRL);
318 val &= ~POWERUP_MASK;
319 writel_relaxed(val, pll->base + PLL_CTRL);
320
321 return 0;
322}
323
324static const struct clk_ops clk_fracn_gppll_ops = {
325 .enable = clk_fracn_gppll_prepare,
326 .disable = clk_fracn_gppll_unprepare,
327 .get_rate = clk_fracn_gppll_recalc_rate,
328 .set_rate = clk_fracn_gppll_set_rate,
329 .round_rate = clk_fracn_gppll_round_rate,
330};
331
332static struct clk *_imx_clk_fracn_gppll(const char *name, const char *parent_name,
333 void __iomem *base,
334 const struct imx_fracn_gppll_clk *pll_clk,
335 u32 pll_flags)
336{
337 struct clk_fracn_gppll *pll;
338 struct clk *clk;
339 int ret;
340
341 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
342 if (!pll)
343 return ERR_PTR(-ENOMEM);
344
345 pll->base = base;
346 pll->rate_table = pll_clk->rate_table;
347 pll->rate_count = pll_clk->rate_count;
348 pll->flags = pll_flags;
349
350 clk = &pll->clk;
351
352 ret = clk_register(clk, UBOOT_DM_CLK_IMX_FRACN_GPPLL,
353 name, parent_name);
354 if (ret) {
355 pr_err("%s: failed to register pll %s %d\n", __func__, name, ret);
356 kfree(pll);
357 return ERR_PTR(ret);
358 }
359
360 return clk;
361}
362
363struct clk *imx_clk_fracn_gppll(const char *name, const char *parent_name, void __iomem *base,
364 const struct imx_fracn_gppll_clk *pll_clk)
365{
366 return _imx_clk_fracn_gppll(name, parent_name, base, pll_clk, CLK_FRACN_GPPLL_FRACN);
367}
368
369struct clk *imx_clk_fracn_gppll_integer(const char *name, const char *parent_name,
370 void __iomem *base,
371 const struct imx_fracn_gppll_clk *pll_clk)
372{
373 return _imx_clk_fracn_gppll(name, parent_name, base, pll_clk, CLK_FRACN_GPPLL_INTEGER);
374}
375
376U_BOOT_DRIVER(clk_fracn_gppll) = {
377 .name = UBOOT_DM_CLK_IMX_FRACN_GPPLL,
378 .id = UCLASS_CLK,
379 .ops = &clk_fracn_gppll_ops,
380 .flags = DM_FLAG_PRE_RELOC,
381};