0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/clk-provider.h>
0012 #include <linux/err.h>
0013 #include <linux/export.h>
0014 #include <linux/io.h>
0015 #include <linux/iopoll.h>
0016 #include <linux/slab.h>
0017 #include <linux/bitfield.h>
0018
0019 #include "clk.h"
0020
0021
0022 #define PLL_CFG0 0x0
0023 #define PLL_CFG1 0x4
0024 #define PLL_CFG2 0x8
0025
0026 #define PLL_DIVF1_MASK GENMASK(18, 13)
0027 #define PLL_DIVF2_MASK GENMASK(12, 7)
0028 #define PLL_DIVR1_MASK GENMASK(27, 25)
0029 #define PLL_DIVR2_MASK GENMASK(24, 19)
0030 #define PLL_DIVQ_MASK GENMASK(6, 1)
0031 #define PLL_REF_MASK GENMASK(2, 0)
0032
0033 #define PLL_LOCK_MASK BIT(31)
0034 #define PLL_PD_MASK BIT(7)
0035
0036
0037 #define PLL_REF_MIN_FREQ 25000000UL
0038 #define PLL_REF_MAX_FREQ 235000000UL
0039
0040 #define PLL_STAGE1_MIN_FREQ 1600000000UL
0041 #define PLL_STAGE1_MAX_FREQ 2400000000UL
0042
0043 #define PLL_STAGE1_REF_MIN_FREQ 25000000UL
0044 #define PLL_STAGE1_REF_MAX_FREQ 54000000UL
0045
0046 #define PLL_STAGE2_MIN_FREQ 1200000000UL
0047 #define PLL_STAGE2_MAX_FREQ 2400000000UL
0048
0049 #define PLL_STAGE2_REF_MIN_FREQ 54000000UL
0050 #define PLL_STAGE2_REF_MAX_FREQ 75000000UL
0051
0052 #define PLL_OUT_MIN_FREQ 20000000UL
0053 #define PLL_OUT_MAX_FREQ 1200000000UL
0054
0055 #define PLL_DIVR1_MAX 7
0056 #define PLL_DIVR2_MAX 63
0057 #define PLL_DIVF1_MAX 63
0058 #define PLL_DIVF2_MAX 63
0059 #define PLL_DIVQ_MAX 63
0060
0061 #define PLL_BYPASS_NONE 0x0
0062 #define PLL_BYPASS1 0x2
0063 #define PLL_BYPASS2 0x1
0064
0065 #define SSCG_PLL_BYPASS1_MASK BIT(5)
0066 #define SSCG_PLL_BYPASS2_MASK BIT(4)
0067 #define SSCG_PLL_BYPASS_MASK GENMASK(5, 4)
0068
0069 #define PLL_SCCG_LOCK_TIMEOUT 70
0070
0071 struct clk_sscg_pll_setup {
0072 int divr1, divf1;
0073 int divr2, divf2;
0074 int divq;
0075 int bypass;
0076 uint64_t vco1;
0077 uint64_t vco2;
0078 uint64_t fout;
0079 uint64_t ref;
0080 uint64_t ref_div1;
0081 uint64_t ref_div2;
0082 uint64_t fout_request;
0083 int fout_error;
0084 };
0085
0086 struct clk_sscg_pll {
0087 struct clk_hw hw;
0088 const struct clk_ops ops;
0089 void __iomem *base;
0090 struct clk_sscg_pll_setup setup;
0091 u8 parent;
0092 u8 bypass1;
0093 u8 bypass2;
0094 };
0095
0096 #define to_clk_sscg_pll(_hw) container_of(_hw, struct clk_sscg_pll, hw)
0097
0098 static int clk_sscg_pll_wait_lock(struct clk_sscg_pll *pll)
0099 {
0100 u32 val;
0101
0102 val = readl_relaxed(pll->base + PLL_CFG0);
0103
0104
0105 if (!(val & SSCG_PLL_BYPASS2_MASK))
0106 return readl_poll_timeout(pll->base, val, val & PLL_LOCK_MASK,
0107 0, PLL_SCCG_LOCK_TIMEOUT);
0108
0109 return 0;
0110 }
0111
0112 static int clk_sscg_pll2_check_match(struct clk_sscg_pll_setup *setup,
0113 struct clk_sscg_pll_setup *temp_setup)
0114 {
0115 int new_diff = temp_setup->fout - temp_setup->fout_request;
0116 int diff = temp_setup->fout_error;
0117
0118 if (abs(diff) > abs(new_diff)) {
0119 temp_setup->fout_error = new_diff;
0120 memcpy(setup, temp_setup, sizeof(struct clk_sscg_pll_setup));
0121
0122 if (temp_setup->fout_request == temp_setup->fout)
0123 return 0;
0124 }
0125 return -1;
0126 }
0127
0128 static int clk_sscg_divq_lookup(struct clk_sscg_pll_setup *setup,
0129 struct clk_sscg_pll_setup *temp_setup)
0130 {
0131 int ret = -EINVAL;
0132
0133 for (temp_setup->divq = 0; temp_setup->divq <= PLL_DIVQ_MAX;
0134 temp_setup->divq++) {
0135 temp_setup->vco2 = temp_setup->vco1;
0136 do_div(temp_setup->vco2, temp_setup->divr2 + 1);
0137 temp_setup->vco2 *= 2;
0138 temp_setup->vco2 *= temp_setup->divf2 + 1;
0139 if (temp_setup->vco2 >= PLL_STAGE2_MIN_FREQ &&
0140 temp_setup->vco2 <= PLL_STAGE2_MAX_FREQ) {
0141 temp_setup->fout = temp_setup->vco2;
0142 do_div(temp_setup->fout, 2 * (temp_setup->divq + 1));
0143
0144 ret = clk_sscg_pll2_check_match(setup, temp_setup);
0145 if (!ret) {
0146 temp_setup->bypass = PLL_BYPASS1;
0147 return ret;
0148 }
0149 }
0150 }
0151
0152 return ret;
0153 }
0154
0155 static int clk_sscg_divf2_lookup(struct clk_sscg_pll_setup *setup,
0156 struct clk_sscg_pll_setup *temp_setup)
0157 {
0158 int ret = -EINVAL;
0159
0160 for (temp_setup->divf2 = 0; temp_setup->divf2 <= PLL_DIVF2_MAX;
0161 temp_setup->divf2++) {
0162 ret = clk_sscg_divq_lookup(setup, temp_setup);
0163 if (!ret)
0164 return ret;
0165 }
0166
0167 return ret;
0168 }
0169
0170 static int clk_sscg_divr2_lookup(struct clk_sscg_pll_setup *setup,
0171 struct clk_sscg_pll_setup *temp_setup)
0172 {
0173 int ret = -EINVAL;
0174
0175 for (temp_setup->divr2 = 0; temp_setup->divr2 <= PLL_DIVR2_MAX;
0176 temp_setup->divr2++) {
0177 temp_setup->ref_div2 = temp_setup->vco1;
0178 do_div(temp_setup->ref_div2, temp_setup->divr2 + 1);
0179 if (temp_setup->ref_div2 >= PLL_STAGE2_REF_MIN_FREQ &&
0180 temp_setup->ref_div2 <= PLL_STAGE2_REF_MAX_FREQ) {
0181 ret = clk_sscg_divf2_lookup(setup, temp_setup);
0182 if (!ret)
0183 return ret;
0184 }
0185 }
0186
0187 return ret;
0188 }
0189
0190 static int clk_sscg_pll2_find_setup(struct clk_sscg_pll_setup *setup,
0191 struct clk_sscg_pll_setup *temp_setup,
0192 uint64_t ref)
0193 {
0194 int ret;
0195
0196 if (ref < PLL_STAGE1_MIN_FREQ || ref > PLL_STAGE1_MAX_FREQ)
0197 return -EINVAL;
0198
0199 temp_setup->vco1 = ref;
0200
0201 ret = clk_sscg_divr2_lookup(setup, temp_setup);
0202 return ret;
0203 }
0204
0205 static int clk_sscg_divf1_lookup(struct clk_sscg_pll_setup *setup,
0206 struct clk_sscg_pll_setup *temp_setup)
0207 {
0208 int ret = -EINVAL;
0209
0210 for (temp_setup->divf1 = 0; temp_setup->divf1 <= PLL_DIVF1_MAX;
0211 temp_setup->divf1++) {
0212 uint64_t vco1 = temp_setup->ref;
0213
0214 do_div(vco1, temp_setup->divr1 + 1);
0215 vco1 *= 2;
0216 vco1 *= temp_setup->divf1 + 1;
0217
0218 ret = clk_sscg_pll2_find_setup(setup, temp_setup, vco1);
0219 if (!ret) {
0220 temp_setup->bypass = PLL_BYPASS_NONE;
0221 return ret;
0222 }
0223 }
0224
0225 return ret;
0226 }
0227
0228 static int clk_sscg_divr1_lookup(struct clk_sscg_pll_setup *setup,
0229 struct clk_sscg_pll_setup *temp_setup)
0230 {
0231 int ret = -EINVAL;
0232
0233 for (temp_setup->divr1 = 0; temp_setup->divr1 <= PLL_DIVR1_MAX;
0234 temp_setup->divr1++) {
0235 temp_setup->ref_div1 = temp_setup->ref;
0236 do_div(temp_setup->ref_div1, temp_setup->divr1 + 1);
0237 if (temp_setup->ref_div1 >= PLL_STAGE1_REF_MIN_FREQ &&
0238 temp_setup->ref_div1 <= PLL_STAGE1_REF_MAX_FREQ) {
0239 ret = clk_sscg_divf1_lookup(setup, temp_setup);
0240 if (!ret)
0241 return ret;
0242 }
0243 }
0244
0245 return ret;
0246 }
0247
0248 static int clk_sscg_pll1_find_setup(struct clk_sscg_pll_setup *setup,
0249 struct clk_sscg_pll_setup *temp_setup,
0250 uint64_t ref)
0251 {
0252 int ret;
0253
0254 if (ref < PLL_REF_MIN_FREQ || ref > PLL_REF_MAX_FREQ)
0255 return -EINVAL;
0256
0257 temp_setup->ref = ref;
0258
0259 ret = clk_sscg_divr1_lookup(setup, temp_setup);
0260
0261 return ret;
0262 }
0263
0264 static int clk_sscg_pll_find_setup(struct clk_sscg_pll_setup *setup,
0265 uint64_t prate,
0266 uint64_t rate, int try_bypass)
0267 {
0268 struct clk_sscg_pll_setup temp_setup;
0269 int ret = -EINVAL;
0270
0271 memset(&temp_setup, 0, sizeof(struct clk_sscg_pll_setup));
0272 memset(setup, 0, sizeof(struct clk_sscg_pll_setup));
0273
0274 temp_setup.fout_error = PLL_OUT_MAX_FREQ;
0275 temp_setup.fout_request = rate;
0276
0277 switch (try_bypass) {
0278 case PLL_BYPASS2:
0279 if (prate == rate) {
0280 setup->bypass = PLL_BYPASS2;
0281 setup->fout = rate;
0282 ret = 0;
0283 }
0284 break;
0285 case PLL_BYPASS1:
0286 ret = clk_sscg_pll2_find_setup(setup, &temp_setup, prate);
0287 break;
0288 case PLL_BYPASS_NONE:
0289 ret = clk_sscg_pll1_find_setup(setup, &temp_setup, prate);
0290 break;
0291 }
0292
0293 return ret;
0294 }
0295
0296 static int clk_sscg_pll_is_prepared(struct clk_hw *hw)
0297 {
0298 struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
0299
0300 u32 val = readl_relaxed(pll->base + PLL_CFG0);
0301
0302 return (val & PLL_PD_MASK) ? 0 : 1;
0303 }
0304
0305 static int clk_sscg_pll_prepare(struct clk_hw *hw)
0306 {
0307 struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
0308 u32 val;
0309
0310 val = readl_relaxed(pll->base + PLL_CFG0);
0311 val &= ~PLL_PD_MASK;
0312 writel_relaxed(val, pll->base + PLL_CFG0);
0313
0314 return clk_sscg_pll_wait_lock(pll);
0315 }
0316
0317 static void clk_sscg_pll_unprepare(struct clk_hw *hw)
0318 {
0319 struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
0320 u32 val;
0321
0322 val = readl_relaxed(pll->base + PLL_CFG0);
0323 val |= PLL_PD_MASK;
0324 writel_relaxed(val, pll->base + PLL_CFG0);
0325 }
0326
0327 static unsigned long clk_sscg_pll_recalc_rate(struct clk_hw *hw,
0328 unsigned long parent_rate)
0329 {
0330 struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
0331 u32 val, divr1, divf1, divr2, divf2, divq;
0332 u64 temp64;
0333
0334 val = readl_relaxed(pll->base + PLL_CFG2);
0335 divr1 = FIELD_GET(PLL_DIVR1_MASK, val);
0336 divr2 = FIELD_GET(PLL_DIVR2_MASK, val);
0337 divf1 = FIELD_GET(PLL_DIVF1_MASK, val);
0338 divf2 = FIELD_GET(PLL_DIVF2_MASK, val);
0339 divq = FIELD_GET(PLL_DIVQ_MASK, val);
0340
0341 temp64 = parent_rate;
0342
0343 val = readl(pll->base + PLL_CFG0);
0344 if (val & SSCG_PLL_BYPASS2_MASK) {
0345 temp64 = parent_rate;
0346 } else if (val & SSCG_PLL_BYPASS1_MASK) {
0347 temp64 *= divf2;
0348 do_div(temp64, (divr2 + 1) * (divq + 1));
0349 } else {
0350 temp64 *= 2;
0351 temp64 *= (divf1 + 1) * (divf2 + 1);
0352 do_div(temp64, (divr1 + 1) * (divr2 + 1) * (divq + 1));
0353 }
0354
0355 return temp64;
0356 }
0357
0358 static int clk_sscg_pll_set_rate(struct clk_hw *hw, unsigned long rate,
0359 unsigned long parent_rate)
0360 {
0361 struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
0362 struct clk_sscg_pll_setup *setup = &pll->setup;
0363 u32 val;
0364
0365
0366 val = readl(pll->base + PLL_CFG0);
0367 val &= ~SSCG_PLL_BYPASS_MASK;
0368 val |= FIELD_PREP(SSCG_PLL_BYPASS_MASK, setup->bypass);
0369 writel(val, pll->base + PLL_CFG0);
0370
0371 val = readl_relaxed(pll->base + PLL_CFG2);
0372 val &= ~(PLL_DIVF1_MASK | PLL_DIVF2_MASK);
0373 val &= ~(PLL_DIVR1_MASK | PLL_DIVR2_MASK | PLL_DIVQ_MASK);
0374 val |= FIELD_PREP(PLL_DIVF1_MASK, setup->divf1);
0375 val |= FIELD_PREP(PLL_DIVF2_MASK, setup->divf2);
0376 val |= FIELD_PREP(PLL_DIVR1_MASK, setup->divr1);
0377 val |= FIELD_PREP(PLL_DIVR2_MASK, setup->divr2);
0378 val |= FIELD_PREP(PLL_DIVQ_MASK, setup->divq);
0379 writel_relaxed(val, pll->base + PLL_CFG2);
0380
0381 return clk_sscg_pll_wait_lock(pll);
0382 }
0383
0384 static u8 clk_sscg_pll_get_parent(struct clk_hw *hw)
0385 {
0386 struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
0387 u32 val;
0388 u8 ret = pll->parent;
0389
0390 val = readl(pll->base + PLL_CFG0);
0391 if (val & SSCG_PLL_BYPASS2_MASK)
0392 ret = pll->bypass2;
0393 else if (val & SSCG_PLL_BYPASS1_MASK)
0394 ret = pll->bypass1;
0395 return ret;
0396 }
0397
0398 static int clk_sscg_pll_set_parent(struct clk_hw *hw, u8 index)
0399 {
0400 struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
0401 u32 val;
0402
0403 val = readl(pll->base + PLL_CFG0);
0404 val &= ~SSCG_PLL_BYPASS_MASK;
0405 val |= FIELD_PREP(SSCG_PLL_BYPASS_MASK, pll->setup.bypass);
0406 writel(val, pll->base + PLL_CFG0);
0407
0408 return clk_sscg_pll_wait_lock(pll);
0409 }
0410
0411 static int __clk_sscg_pll_determine_rate(struct clk_hw *hw,
0412 struct clk_rate_request *req,
0413 uint64_t min,
0414 uint64_t max,
0415 uint64_t rate,
0416 int bypass)
0417 {
0418 struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
0419 struct clk_sscg_pll_setup *setup = &pll->setup;
0420 struct clk_hw *parent_hw = NULL;
0421 int bypass_parent_index;
0422 int ret;
0423
0424 req->max_rate = max;
0425 req->min_rate = min;
0426
0427 switch (bypass) {
0428 case PLL_BYPASS2:
0429 bypass_parent_index = pll->bypass2;
0430 break;
0431 case PLL_BYPASS1:
0432 bypass_parent_index = pll->bypass1;
0433 break;
0434 default:
0435 bypass_parent_index = pll->parent;
0436 break;
0437 }
0438
0439 parent_hw = clk_hw_get_parent_by_index(hw, bypass_parent_index);
0440 ret = __clk_determine_rate(parent_hw, req);
0441 if (!ret) {
0442 ret = clk_sscg_pll_find_setup(setup, req->rate,
0443 rate, bypass);
0444 }
0445
0446 req->best_parent_hw = parent_hw;
0447 req->best_parent_rate = req->rate;
0448 req->rate = setup->fout;
0449
0450 return ret;
0451 }
0452
0453 static int clk_sscg_pll_determine_rate(struct clk_hw *hw,
0454 struct clk_rate_request *req)
0455 {
0456 struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
0457 struct clk_sscg_pll_setup *setup = &pll->setup;
0458 uint64_t rate = req->rate;
0459 uint64_t min = req->min_rate;
0460 uint64_t max = req->max_rate;
0461 int ret;
0462
0463 if (rate < PLL_OUT_MIN_FREQ || rate > PLL_OUT_MAX_FREQ)
0464 return -EINVAL;
0465
0466 ret = __clk_sscg_pll_determine_rate(hw, req, req->rate, req->rate,
0467 rate, PLL_BYPASS2);
0468 if (!ret)
0469 return ret;
0470
0471 ret = __clk_sscg_pll_determine_rate(hw, req, PLL_STAGE1_REF_MIN_FREQ,
0472 PLL_STAGE1_REF_MAX_FREQ, rate,
0473 PLL_BYPASS1);
0474 if (!ret)
0475 return ret;
0476
0477 ret = __clk_sscg_pll_determine_rate(hw, req, PLL_REF_MIN_FREQ,
0478 PLL_REF_MAX_FREQ, rate,
0479 PLL_BYPASS_NONE);
0480 if (!ret)
0481 return ret;
0482
0483 if (setup->fout >= min && setup->fout <= max)
0484 ret = 0;
0485
0486 return ret;
0487 }
0488
0489 static const struct clk_ops clk_sscg_pll_ops = {
0490 .prepare = clk_sscg_pll_prepare,
0491 .unprepare = clk_sscg_pll_unprepare,
0492 .is_prepared = clk_sscg_pll_is_prepared,
0493 .recalc_rate = clk_sscg_pll_recalc_rate,
0494 .set_rate = clk_sscg_pll_set_rate,
0495 .set_parent = clk_sscg_pll_set_parent,
0496 .get_parent = clk_sscg_pll_get_parent,
0497 .determine_rate = clk_sscg_pll_determine_rate,
0498 };
0499
0500 struct clk_hw *imx_clk_hw_sscg_pll(const char *name,
0501 const char * const *parent_names,
0502 u8 num_parents,
0503 u8 parent, u8 bypass1, u8 bypass2,
0504 void __iomem *base,
0505 unsigned long flags)
0506 {
0507 struct clk_sscg_pll *pll;
0508 struct clk_init_data init;
0509 struct clk_hw *hw;
0510 int ret;
0511
0512 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
0513 if (!pll)
0514 return ERR_PTR(-ENOMEM);
0515
0516 pll->parent = parent;
0517 pll->bypass1 = bypass1;
0518 pll->bypass2 = bypass2;
0519
0520 pll->base = base;
0521 init.name = name;
0522 init.ops = &clk_sscg_pll_ops;
0523
0524 init.flags = flags;
0525 init.parent_names = parent_names;
0526 init.num_parents = num_parents;
0527
0528 pll->hw.init = &init;
0529
0530 hw = &pll->hw;
0531
0532 ret = clk_hw_register(NULL, hw);
0533 if (ret) {
0534 kfree(pll);
0535 return ERR_PTR(ret);
0536 }
0537
0538 return hw;
0539 }
0540 EXPORT_SYMBOL_GPL(imx_clk_hw_sscg_pll);