0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #define pr_fmt(fmt) "bt1-ccu-pll: " fmt
0013
0014 #include <linux/kernel.h>
0015 #include <linux/printk.h>
0016 #include <linux/limits.h>
0017 #include <linux/bits.h>
0018 #include <linux/bitfield.h>
0019 #include <linux/slab.h>
0020 #include <linux/clk-provider.h>
0021 #include <linux/of.h>
0022 #include <linux/spinlock.h>
0023 #include <linux/regmap.h>
0024 #include <linux/iopoll.h>
0025 #include <linux/time64.h>
0026 #include <linux/rational.h>
0027 #include <linux/debugfs.h>
0028
0029 #include "ccu-pll.h"
0030
0031 #define CCU_PLL_CTL 0x000
0032 #define CCU_PLL_CTL_EN BIT(0)
0033 #define CCU_PLL_CTL_RST BIT(1)
0034 #define CCU_PLL_CTL_CLKR_FLD 2
0035 #define CCU_PLL_CTL_CLKR_MASK GENMASK(7, CCU_PLL_CTL_CLKR_FLD)
0036 #define CCU_PLL_CTL_CLKF_FLD 8
0037 #define CCU_PLL_CTL_CLKF_MASK GENMASK(20, CCU_PLL_CTL_CLKF_FLD)
0038 #define CCU_PLL_CTL_CLKOD_FLD 21
0039 #define CCU_PLL_CTL_CLKOD_MASK GENMASK(24, CCU_PLL_CTL_CLKOD_FLD)
0040 #define CCU_PLL_CTL_BYPASS BIT(30)
0041 #define CCU_PLL_CTL_LOCK BIT(31)
0042 #define CCU_PLL_CTL1 0x004
0043 #define CCU_PLL_CTL1_BWADJ_FLD 3
0044 #define CCU_PLL_CTL1_BWADJ_MASK GENMASK(14, CCU_PLL_CTL1_BWADJ_FLD)
0045
0046 #define CCU_PLL_LOCK_CHECK_RETRIES 50
0047
0048 #define CCU_PLL_NR_MAX \
0049 ((CCU_PLL_CTL_CLKR_MASK >> CCU_PLL_CTL_CLKR_FLD) + 1)
0050 #define CCU_PLL_NF_MAX \
0051 ((CCU_PLL_CTL_CLKF_MASK >> (CCU_PLL_CTL_CLKF_FLD + 1)) + 1)
0052 #define CCU_PLL_OD_MAX \
0053 ((CCU_PLL_CTL_CLKOD_MASK >> CCU_PLL_CTL_CLKOD_FLD) + 1)
0054 #define CCU_PLL_NB_MAX \
0055 ((CCU_PLL_CTL1_BWADJ_MASK >> CCU_PLL_CTL1_BWADJ_FLD) + 1)
0056 #define CCU_PLL_FDIV_MIN 427000UL
0057 #define CCU_PLL_FDIV_MAX 3500000000UL
0058 #define CCU_PLL_FOUT_MIN 200000000UL
0059 #define CCU_PLL_FOUT_MAX 2500000000UL
0060 #define CCU_PLL_FVCO_MIN 700000000UL
0061 #define CCU_PLL_FVCO_MAX 3500000000UL
0062 #define CCU_PLL_CLKOD_FACTOR 2
0063
0064 static inline unsigned long ccu_pll_lock_delay_us(unsigned long ref_clk,
0065 unsigned long nr)
0066 {
0067 u64 us = 500ULL * nr * USEC_PER_SEC;
0068
0069 do_div(us, ref_clk);
0070
0071 return us;
0072 }
0073
0074 static inline unsigned long ccu_pll_calc_freq(unsigned long ref_clk,
0075 unsigned long nr,
0076 unsigned long nf,
0077 unsigned long od)
0078 {
0079 u64 tmp = ref_clk;
0080
0081 do_div(tmp, nr);
0082 tmp *= nf;
0083 do_div(tmp, od);
0084
0085 return tmp;
0086 }
0087
0088 static int ccu_pll_reset(struct ccu_pll *pll, unsigned long ref_clk,
0089 unsigned long nr)
0090 {
0091 unsigned long ud, ut;
0092 u32 val;
0093
0094 ud = ccu_pll_lock_delay_us(ref_clk, nr);
0095 ut = ud * CCU_PLL_LOCK_CHECK_RETRIES;
0096
0097 regmap_update_bits(pll->sys_regs, pll->reg_ctl,
0098 CCU_PLL_CTL_RST, CCU_PLL_CTL_RST);
0099
0100 return regmap_read_poll_timeout_atomic(pll->sys_regs, pll->reg_ctl, val,
0101 val & CCU_PLL_CTL_LOCK, ud, ut);
0102 }
0103
0104 static int ccu_pll_enable(struct clk_hw *hw)
0105 {
0106 struct clk_hw *parent_hw = clk_hw_get_parent(hw);
0107 struct ccu_pll *pll = to_ccu_pll(hw);
0108 unsigned long flags;
0109 u32 val = 0;
0110 int ret;
0111
0112 if (!parent_hw) {
0113 pr_err("Can't enable '%s' with no parent", clk_hw_get_name(hw));
0114 return -EINVAL;
0115 }
0116
0117 regmap_read(pll->sys_regs, pll->reg_ctl, &val);
0118 if (val & CCU_PLL_CTL_EN)
0119 return 0;
0120
0121 spin_lock_irqsave(&pll->lock, flags);
0122 regmap_write(pll->sys_regs, pll->reg_ctl, val | CCU_PLL_CTL_EN);
0123 ret = ccu_pll_reset(pll, clk_hw_get_rate(parent_hw),
0124 FIELD_GET(CCU_PLL_CTL_CLKR_MASK, val) + 1);
0125 spin_unlock_irqrestore(&pll->lock, flags);
0126 if (ret)
0127 pr_err("PLL '%s' reset timed out\n", clk_hw_get_name(hw));
0128
0129 return ret;
0130 }
0131
0132 static void ccu_pll_disable(struct clk_hw *hw)
0133 {
0134 struct ccu_pll *pll = to_ccu_pll(hw);
0135 unsigned long flags;
0136
0137 spin_lock_irqsave(&pll->lock, flags);
0138 regmap_update_bits(pll->sys_regs, pll->reg_ctl, CCU_PLL_CTL_EN, 0);
0139 spin_unlock_irqrestore(&pll->lock, flags);
0140 }
0141
0142 static int ccu_pll_is_enabled(struct clk_hw *hw)
0143 {
0144 struct ccu_pll *pll = to_ccu_pll(hw);
0145 u32 val = 0;
0146
0147 regmap_read(pll->sys_regs, pll->reg_ctl, &val);
0148
0149 return !!(val & CCU_PLL_CTL_EN);
0150 }
0151
0152 static unsigned long ccu_pll_recalc_rate(struct clk_hw *hw,
0153 unsigned long parent_rate)
0154 {
0155 struct ccu_pll *pll = to_ccu_pll(hw);
0156 unsigned long nr, nf, od;
0157 u32 val = 0;
0158
0159 regmap_read(pll->sys_regs, pll->reg_ctl, &val);
0160 nr = FIELD_GET(CCU_PLL_CTL_CLKR_MASK, val) + 1;
0161 nf = FIELD_GET(CCU_PLL_CTL_CLKF_MASK, val) + 1;
0162 od = FIELD_GET(CCU_PLL_CTL_CLKOD_MASK, val) + 1;
0163
0164 return ccu_pll_calc_freq(parent_rate, nr, nf, od);
0165 }
0166
0167 static void ccu_pll_calc_factors(unsigned long rate, unsigned long parent_rate,
0168 unsigned long *nr, unsigned long *nf,
0169 unsigned long *od)
0170 {
0171 unsigned long err, freq, min_err = ULONG_MAX;
0172 unsigned long num, denom, n1, d1, nri;
0173 unsigned long nr_max, nf_max, od_max;
0174
0175
0176
0177
0178
0179
0180 nri = (parent_rate / CCU_PLL_FDIV_MAX) + 1;
0181 nr_max = min(parent_rate / CCU_PLL_FDIV_MIN, CCU_PLL_NR_MAX);
0182
0183
0184
0185
0186
0187
0188
0189 for (; nri <= nr_max; ++nri) {
0190
0191 num = CCU_PLL_CLKOD_FACTOR * rate;
0192 denom = parent_rate / nri;
0193
0194
0195
0196
0197
0198
0199
0200
0201 nf_max = min(CCU_PLL_FVCO_MAX / denom, CCU_PLL_NF_MAX);
0202 od_max = CCU_PLL_OD_MAX / CCU_PLL_CLKOD_FACTOR;
0203
0204
0205
0206
0207
0208 if (num / denom >= nf_max) {
0209 n1 = nf_max;
0210 d1 = 1;
0211 } else if (denom / num >= od_max) {
0212 n1 = 1;
0213 d1 = od_max;
0214 } else {
0215 rational_best_approximation(num, denom, nf_max, od_max,
0216 &n1, &d1);
0217 }
0218
0219
0220 freq = ccu_pll_calc_freq(parent_rate, nri, n1, d1);
0221 err = abs((int64_t)freq - num);
0222 if (err < min_err) {
0223 min_err = err;
0224 *nr = nri;
0225 *nf = n1;
0226 *od = CCU_PLL_CLKOD_FACTOR * d1;
0227 }
0228 }
0229 }
0230
0231 static long ccu_pll_round_rate(struct clk_hw *hw, unsigned long rate,
0232 unsigned long *parent_rate)
0233 {
0234 unsigned long nr = 1, nf = 1, od = 1;
0235
0236 ccu_pll_calc_factors(rate, *parent_rate, &nr, &nf, &od);
0237
0238 return ccu_pll_calc_freq(*parent_rate, nr, nf, od);
0239 }
0240
0241
0242
0243
0244
0245 static int ccu_pll_set_rate_reset(struct clk_hw *hw, unsigned long rate,
0246 unsigned long parent_rate)
0247 {
0248 struct ccu_pll *pll = to_ccu_pll(hw);
0249 unsigned long nr, nf, od;
0250 unsigned long flags;
0251 u32 mask, val;
0252 int ret;
0253
0254 ccu_pll_calc_factors(rate, parent_rate, &nr, &nf, &od);
0255
0256 mask = CCU_PLL_CTL_CLKR_MASK | CCU_PLL_CTL_CLKF_MASK |
0257 CCU_PLL_CTL_CLKOD_MASK;
0258 val = FIELD_PREP(CCU_PLL_CTL_CLKR_MASK, nr - 1) |
0259 FIELD_PREP(CCU_PLL_CTL_CLKF_MASK, nf - 1) |
0260 FIELD_PREP(CCU_PLL_CTL_CLKOD_MASK, od - 1);
0261
0262 spin_lock_irqsave(&pll->lock, flags);
0263 regmap_update_bits(pll->sys_regs, pll->reg_ctl, mask, val);
0264 ret = ccu_pll_reset(pll, parent_rate, nr);
0265 spin_unlock_irqrestore(&pll->lock, flags);
0266 if (ret)
0267 pr_err("PLL '%s' reset timed out\n", clk_hw_get_name(hw));
0268
0269 return ret;
0270 }
0271
0272
0273
0274
0275
0276 static int ccu_pll_set_rate_norst(struct clk_hw *hw, unsigned long rate,
0277 unsigned long parent_rate)
0278 {
0279 struct ccu_pll *pll = to_ccu_pll(hw);
0280 unsigned long nr, nf, od;
0281 unsigned long flags;
0282 u32 mask, val;
0283
0284 ccu_pll_calc_factors(rate, parent_rate, &nr, &nf, &od);
0285
0286
0287
0288
0289
0290 mask = CCU_PLL_CTL_CLKR_MASK | CCU_PLL_CTL_CLKF_MASK |
0291 CCU_PLL_CTL_CLKOD_MASK | CCU_PLL_CTL_EN;
0292 val = FIELD_PREP(CCU_PLL_CTL_CLKR_MASK, nr - 1) |
0293 FIELD_PREP(CCU_PLL_CTL_CLKF_MASK, nf - 1) |
0294 FIELD_PREP(CCU_PLL_CTL_CLKOD_MASK, od - 1);
0295
0296 spin_lock_irqsave(&pll->lock, flags);
0297 regmap_update_bits(pll->sys_regs, pll->reg_ctl, mask, val);
0298 spin_unlock_irqrestore(&pll->lock, flags);
0299
0300 return 0;
0301 }
0302
0303 #ifdef CONFIG_DEBUG_FS
0304
0305 struct ccu_pll_dbgfs_bit {
0306 struct ccu_pll *pll;
0307 const char *name;
0308 unsigned int reg;
0309 u32 mask;
0310 };
0311
0312 struct ccu_pll_dbgfs_fld {
0313 struct ccu_pll *pll;
0314 const char *name;
0315 unsigned int reg;
0316 unsigned int lsb;
0317 u32 mask;
0318 u32 min;
0319 u32 max;
0320 };
0321
0322 #define CCU_PLL_DBGFS_BIT_ATTR(_name, _reg, _mask) \
0323 { \
0324 .name = _name, \
0325 .reg = _reg, \
0326 .mask = _mask \
0327 }
0328
0329 #define CCU_PLL_DBGFS_FLD_ATTR(_name, _reg, _lsb, _mask, _min, _max) \
0330 { \
0331 .name = _name, \
0332 .reg = _reg, \
0333 .lsb = _lsb, \
0334 .mask = _mask, \
0335 .min = _min, \
0336 .max = _max \
0337 }
0338
0339 static const struct ccu_pll_dbgfs_bit ccu_pll_bits[] = {
0340 CCU_PLL_DBGFS_BIT_ATTR("pll_en", CCU_PLL_CTL, CCU_PLL_CTL_EN),
0341 CCU_PLL_DBGFS_BIT_ATTR("pll_rst", CCU_PLL_CTL, CCU_PLL_CTL_RST),
0342 CCU_PLL_DBGFS_BIT_ATTR("pll_bypass", CCU_PLL_CTL, CCU_PLL_CTL_BYPASS),
0343 CCU_PLL_DBGFS_BIT_ATTR("pll_lock", CCU_PLL_CTL, CCU_PLL_CTL_LOCK)
0344 };
0345
0346 #define CCU_PLL_DBGFS_BIT_NUM ARRAY_SIZE(ccu_pll_bits)
0347
0348 static const struct ccu_pll_dbgfs_fld ccu_pll_flds[] = {
0349 CCU_PLL_DBGFS_FLD_ATTR("pll_nr", CCU_PLL_CTL, CCU_PLL_CTL_CLKR_FLD,
0350 CCU_PLL_CTL_CLKR_MASK, 1, CCU_PLL_NR_MAX),
0351 CCU_PLL_DBGFS_FLD_ATTR("pll_nf", CCU_PLL_CTL, CCU_PLL_CTL_CLKF_FLD,
0352 CCU_PLL_CTL_CLKF_MASK, 1, CCU_PLL_NF_MAX),
0353 CCU_PLL_DBGFS_FLD_ATTR("pll_od", CCU_PLL_CTL, CCU_PLL_CTL_CLKOD_FLD,
0354 CCU_PLL_CTL_CLKOD_MASK, 1, CCU_PLL_OD_MAX),
0355 CCU_PLL_DBGFS_FLD_ATTR("pll_nb", CCU_PLL_CTL1, CCU_PLL_CTL1_BWADJ_FLD,
0356 CCU_PLL_CTL1_BWADJ_MASK, 1, CCU_PLL_NB_MAX)
0357 };
0358
0359 #define CCU_PLL_DBGFS_FLD_NUM ARRAY_SIZE(ccu_pll_flds)
0360
0361
0362
0363
0364
0365
0366 #undef CCU_PLL_ALLOW_WRITE_DEBUGFS
0367 #ifdef CCU_PLL_ALLOW_WRITE_DEBUGFS
0368
0369 static int ccu_pll_dbgfs_bit_set(void *priv, u64 val)
0370 {
0371 const struct ccu_pll_dbgfs_bit *bit = priv;
0372 struct ccu_pll *pll = bit->pll;
0373 unsigned long flags;
0374
0375 spin_lock_irqsave(&pll->lock, flags);
0376 regmap_update_bits(pll->sys_regs, pll->reg_ctl + bit->reg,
0377 bit->mask, val ? bit->mask : 0);
0378 spin_unlock_irqrestore(&pll->lock, flags);
0379
0380 return 0;
0381 }
0382
0383 static int ccu_pll_dbgfs_fld_set(void *priv, u64 val)
0384 {
0385 struct ccu_pll_dbgfs_fld *fld = priv;
0386 struct ccu_pll *pll = fld->pll;
0387 unsigned long flags;
0388 u32 data;
0389
0390 val = clamp_t(u64, val, fld->min, fld->max);
0391 data = ((val - 1) << fld->lsb) & fld->mask;
0392
0393 spin_lock_irqsave(&pll->lock, flags);
0394 regmap_update_bits(pll->sys_regs, pll->reg_ctl + fld->reg, fld->mask,
0395 data);
0396 spin_unlock_irqrestore(&pll->lock, flags);
0397
0398 return 0;
0399 }
0400
0401 #define ccu_pll_dbgfs_mode 0644
0402
0403 #else
0404
0405 #define ccu_pll_dbgfs_bit_set NULL
0406 #define ccu_pll_dbgfs_fld_set NULL
0407 #define ccu_pll_dbgfs_mode 0444
0408
0409 #endif
0410
0411 static int ccu_pll_dbgfs_bit_get(void *priv, u64 *val)
0412 {
0413 struct ccu_pll_dbgfs_bit *bit = priv;
0414 struct ccu_pll *pll = bit->pll;
0415 u32 data = 0;
0416
0417 regmap_read(pll->sys_regs, pll->reg_ctl + bit->reg, &data);
0418 *val = !!(data & bit->mask);
0419
0420 return 0;
0421 }
0422 DEFINE_DEBUGFS_ATTRIBUTE(ccu_pll_dbgfs_bit_fops,
0423 ccu_pll_dbgfs_bit_get, ccu_pll_dbgfs_bit_set, "%llu\n");
0424
0425 static int ccu_pll_dbgfs_fld_get(void *priv, u64 *val)
0426 {
0427 struct ccu_pll_dbgfs_fld *fld = priv;
0428 struct ccu_pll *pll = fld->pll;
0429 u32 data = 0;
0430
0431 regmap_read(pll->sys_regs, pll->reg_ctl + fld->reg, &data);
0432 *val = ((data & fld->mask) >> fld->lsb) + 1;
0433
0434 return 0;
0435 }
0436 DEFINE_DEBUGFS_ATTRIBUTE(ccu_pll_dbgfs_fld_fops,
0437 ccu_pll_dbgfs_fld_get, ccu_pll_dbgfs_fld_set, "%llu\n");
0438
0439 static void ccu_pll_debug_init(struct clk_hw *hw, struct dentry *dentry)
0440 {
0441 struct ccu_pll *pll = to_ccu_pll(hw);
0442 struct ccu_pll_dbgfs_bit *bits;
0443 struct ccu_pll_dbgfs_fld *flds;
0444 int idx;
0445
0446 bits = kcalloc(CCU_PLL_DBGFS_BIT_NUM, sizeof(*bits), GFP_KERNEL);
0447 if (!bits)
0448 return;
0449
0450 for (idx = 0; idx < CCU_PLL_DBGFS_BIT_NUM; ++idx) {
0451 bits[idx] = ccu_pll_bits[idx];
0452 bits[idx].pll = pll;
0453
0454 debugfs_create_file_unsafe(bits[idx].name, ccu_pll_dbgfs_mode,
0455 dentry, &bits[idx],
0456 &ccu_pll_dbgfs_bit_fops);
0457 }
0458
0459 flds = kcalloc(CCU_PLL_DBGFS_FLD_NUM, sizeof(*flds), GFP_KERNEL);
0460 if (!flds)
0461 return;
0462
0463 for (idx = 0; idx < CCU_PLL_DBGFS_FLD_NUM; ++idx) {
0464 flds[idx] = ccu_pll_flds[idx];
0465 flds[idx].pll = pll;
0466
0467 debugfs_create_file_unsafe(flds[idx].name, ccu_pll_dbgfs_mode,
0468 dentry, &flds[idx],
0469 &ccu_pll_dbgfs_fld_fops);
0470 }
0471 }
0472
0473 #else
0474
0475 #define ccu_pll_debug_init NULL
0476
0477 #endif
0478
0479 static const struct clk_ops ccu_pll_gate_to_set_ops = {
0480 .enable = ccu_pll_enable,
0481 .disable = ccu_pll_disable,
0482 .is_enabled = ccu_pll_is_enabled,
0483 .recalc_rate = ccu_pll_recalc_rate,
0484 .round_rate = ccu_pll_round_rate,
0485 .set_rate = ccu_pll_set_rate_norst,
0486 .debug_init = ccu_pll_debug_init
0487 };
0488
0489 static const struct clk_ops ccu_pll_straight_set_ops = {
0490 .enable = ccu_pll_enable,
0491 .disable = ccu_pll_disable,
0492 .is_enabled = ccu_pll_is_enabled,
0493 .recalc_rate = ccu_pll_recalc_rate,
0494 .round_rate = ccu_pll_round_rate,
0495 .set_rate = ccu_pll_set_rate_reset,
0496 .debug_init = ccu_pll_debug_init
0497 };
0498
0499 struct ccu_pll *ccu_pll_hw_register(const struct ccu_pll_init_data *pll_init)
0500 {
0501 struct clk_parent_data parent_data = { };
0502 struct clk_init_data hw_init = { };
0503 struct ccu_pll *pll;
0504 int ret;
0505
0506 if (!pll_init)
0507 return ERR_PTR(-EINVAL);
0508
0509 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
0510 if (!pll)
0511 return ERR_PTR(-ENOMEM);
0512
0513
0514
0515
0516
0517
0518 pll->hw.init = &hw_init;
0519 pll->reg_ctl = pll_init->base + CCU_PLL_CTL;
0520 pll->reg_ctl1 = pll_init->base + CCU_PLL_CTL1;
0521 pll->sys_regs = pll_init->sys_regs;
0522 pll->id = pll_init->id;
0523 spin_lock_init(&pll->lock);
0524
0525 hw_init.name = pll_init->name;
0526 hw_init.flags = pll_init->flags;
0527
0528 if (hw_init.flags & CLK_SET_RATE_GATE)
0529 hw_init.ops = &ccu_pll_gate_to_set_ops;
0530 else
0531 hw_init.ops = &ccu_pll_straight_set_ops;
0532
0533 if (!pll_init->parent_name) {
0534 ret = -EINVAL;
0535 goto err_free_pll;
0536 }
0537 parent_data.fw_name = pll_init->parent_name;
0538 hw_init.parent_data = &parent_data;
0539 hw_init.num_parents = 1;
0540
0541 ret = of_clk_hw_register(pll_init->np, &pll->hw);
0542 if (ret)
0543 goto err_free_pll;
0544
0545 return pll;
0546
0547 err_free_pll:
0548 kfree(pll);
0549
0550 return ERR_PTR(ret);
0551 }
0552
0553 void ccu_pll_hw_unregister(struct ccu_pll *pll)
0554 {
0555 clk_hw_unregister(&pll->hw);
0556
0557 kfree(pll);
0558 }