0001
0002
0003 #include <linux/clk.h>
0004 #include <linux/clk-provider.h>
0005 #include <linux/delay.h>
0006 #include <linux/err.h>
0007 #include <linux/io.h>
0008 #include <linux/math64.h>
0009 #include <linux/of.h>
0010 #include <linux/of_address.h>
0011 #include <linux/clk/ti.h>
0012
0013 #include "clock.h"
0014
0015
0016 #define FAPLL_MAIN_MULT_N_SHIFT 16
0017 #define FAPLL_MAIN_DIV_P_SHIFT 8
0018 #define FAPLL_MAIN_LOCK BIT(7)
0019 #define FAPLL_MAIN_PLLEN BIT(3)
0020 #define FAPLL_MAIN_BP BIT(2)
0021 #define FAPLL_MAIN_LOC_CTL BIT(0)
0022
0023 #define FAPLL_MAIN_MAX_MULT_N 0xffff
0024 #define FAPLL_MAIN_MAX_DIV_P 0xff
0025 #define FAPLL_MAIN_CLEAR_MASK \
0026 ((FAPLL_MAIN_MAX_MULT_N << FAPLL_MAIN_MULT_N_SHIFT) | \
0027 (FAPLL_MAIN_DIV_P_SHIFT << FAPLL_MAIN_DIV_P_SHIFT) | \
0028 FAPLL_MAIN_LOC_CTL)
0029
0030
0031 #define FAPLL_PWD_OFFSET 4
0032
0033 #define MAX_FAPLL_OUTPUTS 7
0034 #define FAPLL_MAX_RETRIES 1000
0035
0036 #define to_fapll(_hw) container_of(_hw, struct fapll_data, hw)
0037 #define to_synth(_hw) container_of(_hw, struct fapll_synth, hw)
0038
0039
0040 #define fapll_is_ddr_pll(va) (((u32)(va) & 0xffff) == 0x0440)
0041
0042
0043
0044
0045
0046 #define is_ddr_pll_clk1(va) (((u32)(va) & 0xffff) == 0x044c)
0047 #define is_audio_pll_clk1(va) (((u32)(va) & 0xffff) == 0x04a8)
0048
0049
0050 #define SYNTH_LDMDIV1 BIT(8)
0051
0052
0053 #define SYNTH_LDFREQ BIT(31)
0054
0055 #define SYNTH_PHASE_K 8
0056 #define SYNTH_MAX_INT_DIV 0xf
0057 #define SYNTH_MAX_DIV_M 0xff
0058
0059 struct fapll_data {
0060 struct clk_hw hw;
0061 void __iomem *base;
0062 const char *name;
0063 struct clk *clk_ref;
0064 struct clk *clk_bypass;
0065 struct clk_onecell_data outputs;
0066 bool bypass_bit_inverted;
0067 };
0068
0069 struct fapll_synth {
0070 struct clk_hw hw;
0071 struct fapll_data *fd;
0072 int index;
0073 void __iomem *freq;
0074 void __iomem *div;
0075 const char *name;
0076 struct clk *clk_pll;
0077 };
0078
0079 static bool ti_fapll_clock_is_bypass(struct fapll_data *fd)
0080 {
0081 u32 v = readl_relaxed(fd->base);
0082
0083 if (fd->bypass_bit_inverted)
0084 return !(v & FAPLL_MAIN_BP);
0085 else
0086 return !!(v & FAPLL_MAIN_BP);
0087 }
0088
0089 static void ti_fapll_set_bypass(struct fapll_data *fd)
0090 {
0091 u32 v = readl_relaxed(fd->base);
0092
0093 if (fd->bypass_bit_inverted)
0094 v &= ~FAPLL_MAIN_BP;
0095 else
0096 v |= FAPLL_MAIN_BP;
0097 writel_relaxed(v, fd->base);
0098 }
0099
0100 static void ti_fapll_clear_bypass(struct fapll_data *fd)
0101 {
0102 u32 v = readl_relaxed(fd->base);
0103
0104 if (fd->bypass_bit_inverted)
0105 v |= FAPLL_MAIN_BP;
0106 else
0107 v &= ~FAPLL_MAIN_BP;
0108 writel_relaxed(v, fd->base);
0109 }
0110
0111 static int ti_fapll_wait_lock(struct fapll_data *fd)
0112 {
0113 int retries = FAPLL_MAX_RETRIES;
0114 u32 v;
0115
0116 while ((v = readl_relaxed(fd->base))) {
0117 if (v & FAPLL_MAIN_LOCK)
0118 return 0;
0119
0120 if (retries-- <= 0)
0121 break;
0122
0123 udelay(1);
0124 }
0125
0126 pr_err("%s failed to lock\n", fd->name);
0127
0128 return -ETIMEDOUT;
0129 }
0130
0131 static int ti_fapll_enable(struct clk_hw *hw)
0132 {
0133 struct fapll_data *fd = to_fapll(hw);
0134 u32 v = readl_relaxed(fd->base);
0135
0136 v |= FAPLL_MAIN_PLLEN;
0137 writel_relaxed(v, fd->base);
0138 ti_fapll_wait_lock(fd);
0139
0140 return 0;
0141 }
0142
0143 static void ti_fapll_disable(struct clk_hw *hw)
0144 {
0145 struct fapll_data *fd = to_fapll(hw);
0146 u32 v = readl_relaxed(fd->base);
0147
0148 v &= ~FAPLL_MAIN_PLLEN;
0149 writel_relaxed(v, fd->base);
0150 }
0151
0152 static int ti_fapll_is_enabled(struct clk_hw *hw)
0153 {
0154 struct fapll_data *fd = to_fapll(hw);
0155 u32 v = readl_relaxed(fd->base);
0156
0157 return v & FAPLL_MAIN_PLLEN;
0158 }
0159
0160 static unsigned long ti_fapll_recalc_rate(struct clk_hw *hw,
0161 unsigned long parent_rate)
0162 {
0163 struct fapll_data *fd = to_fapll(hw);
0164 u32 fapll_n, fapll_p, v;
0165 u64 rate;
0166
0167 if (ti_fapll_clock_is_bypass(fd))
0168 return parent_rate;
0169
0170 rate = parent_rate;
0171
0172
0173 v = readl_relaxed(fd->base);
0174 fapll_p = (v >> 8) & 0xff;
0175 if (fapll_p)
0176 do_div(rate, fapll_p);
0177 fapll_n = v >> 16;
0178 if (fapll_n)
0179 rate *= fapll_n;
0180
0181 return rate;
0182 }
0183
0184 static u8 ti_fapll_get_parent(struct clk_hw *hw)
0185 {
0186 struct fapll_data *fd = to_fapll(hw);
0187
0188 if (ti_fapll_clock_is_bypass(fd))
0189 return 1;
0190
0191 return 0;
0192 }
0193
0194 static int ti_fapll_set_div_mult(unsigned long rate,
0195 unsigned long parent_rate,
0196 u32 *pre_div_p, u32 *mult_n)
0197 {
0198
0199
0200
0201
0202
0203
0204 if (rate < parent_rate) {
0205 pr_warn("FAPLL main divider rates unsupported\n");
0206 return -EINVAL;
0207 }
0208
0209 *mult_n = rate / parent_rate;
0210 if (*mult_n > FAPLL_MAIN_MAX_MULT_N)
0211 return -EINVAL;
0212 *pre_div_p = 1;
0213
0214 return 0;
0215 }
0216
0217 static long ti_fapll_round_rate(struct clk_hw *hw, unsigned long rate,
0218 unsigned long *parent_rate)
0219 {
0220 u32 pre_div_p, mult_n;
0221 int error;
0222
0223 if (!rate)
0224 return -EINVAL;
0225
0226 error = ti_fapll_set_div_mult(rate, *parent_rate,
0227 &pre_div_p, &mult_n);
0228 if (error)
0229 return error;
0230
0231 rate = *parent_rate / pre_div_p;
0232 rate *= mult_n;
0233
0234 return rate;
0235 }
0236
0237 static int ti_fapll_set_rate(struct clk_hw *hw, unsigned long rate,
0238 unsigned long parent_rate)
0239 {
0240 struct fapll_data *fd = to_fapll(hw);
0241 u32 pre_div_p, mult_n, v;
0242 int error;
0243
0244 if (!rate)
0245 return -EINVAL;
0246
0247 error = ti_fapll_set_div_mult(rate, parent_rate,
0248 &pre_div_p, &mult_n);
0249 if (error)
0250 return error;
0251
0252 ti_fapll_set_bypass(fd);
0253 v = readl_relaxed(fd->base);
0254 v &= ~FAPLL_MAIN_CLEAR_MASK;
0255 v |= pre_div_p << FAPLL_MAIN_DIV_P_SHIFT;
0256 v |= mult_n << FAPLL_MAIN_MULT_N_SHIFT;
0257 writel_relaxed(v, fd->base);
0258 if (ti_fapll_is_enabled(hw))
0259 ti_fapll_wait_lock(fd);
0260 ti_fapll_clear_bypass(fd);
0261
0262 return 0;
0263 }
0264
0265 static const struct clk_ops ti_fapll_ops = {
0266 .enable = ti_fapll_enable,
0267 .disable = ti_fapll_disable,
0268 .is_enabled = ti_fapll_is_enabled,
0269 .recalc_rate = ti_fapll_recalc_rate,
0270 .get_parent = ti_fapll_get_parent,
0271 .round_rate = ti_fapll_round_rate,
0272 .set_rate = ti_fapll_set_rate,
0273 };
0274
0275 static int ti_fapll_synth_enable(struct clk_hw *hw)
0276 {
0277 struct fapll_synth *synth = to_synth(hw);
0278 u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
0279
0280 v &= ~(1 << synth->index);
0281 writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET);
0282
0283 return 0;
0284 }
0285
0286 static void ti_fapll_synth_disable(struct clk_hw *hw)
0287 {
0288 struct fapll_synth *synth = to_synth(hw);
0289 u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
0290
0291 v |= 1 << synth->index;
0292 writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET);
0293 }
0294
0295 static int ti_fapll_synth_is_enabled(struct clk_hw *hw)
0296 {
0297 struct fapll_synth *synth = to_synth(hw);
0298 u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
0299
0300 return !(v & (1 << synth->index));
0301 }
0302
0303
0304
0305
0306 static unsigned long ti_fapll_synth_recalc_rate(struct clk_hw *hw,
0307 unsigned long parent_rate)
0308 {
0309 struct fapll_synth *synth = to_synth(hw);
0310 u32 synth_div_m;
0311 u64 rate;
0312
0313
0314 if (!synth->div)
0315 return 32768;
0316
0317
0318
0319
0320
0321
0322 if (ti_fapll_clock_is_bypass(synth->fd))
0323 return parent_rate;
0324
0325 rate = parent_rate;
0326
0327
0328
0329
0330
0331
0332 if (synth->freq) {
0333 u32 v, synth_int_div, synth_frac_div, synth_div_freq;
0334
0335 v = readl_relaxed(synth->freq);
0336 synth_int_div = (v >> 24) & 0xf;
0337 synth_frac_div = v & 0xffffff;
0338 synth_div_freq = (synth_int_div * 10000000) + synth_frac_div;
0339 rate *= 10000000;
0340 do_div(rate, synth_div_freq);
0341 rate *= SYNTH_PHASE_K;
0342 }
0343
0344
0345 synth_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M;
0346
0347 return DIV_ROUND_UP_ULL(rate, synth_div_m);
0348 }
0349
0350 static unsigned long ti_fapll_synth_get_frac_rate(struct clk_hw *hw,
0351 unsigned long parent_rate)
0352 {
0353 struct fapll_synth *synth = to_synth(hw);
0354 unsigned long current_rate, frac_rate;
0355 u32 post_div_m;
0356
0357 current_rate = ti_fapll_synth_recalc_rate(hw, parent_rate);
0358 post_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M;
0359 frac_rate = current_rate * post_div_m;
0360
0361 return frac_rate;
0362 }
0363
0364 static u32 ti_fapll_synth_set_frac_rate(struct fapll_synth *synth,
0365 unsigned long rate,
0366 unsigned long parent_rate)
0367 {
0368 u32 post_div_m, synth_int_div = 0, synth_frac_div = 0, v;
0369
0370 post_div_m = DIV_ROUND_UP_ULL((u64)parent_rate * SYNTH_PHASE_K, rate);
0371 post_div_m = post_div_m / SYNTH_MAX_INT_DIV;
0372 if (post_div_m > SYNTH_MAX_DIV_M)
0373 return -EINVAL;
0374 if (!post_div_m)
0375 post_div_m = 1;
0376
0377 for (; post_div_m < SYNTH_MAX_DIV_M; post_div_m++) {
0378 synth_int_div = DIV_ROUND_UP_ULL((u64)parent_rate *
0379 SYNTH_PHASE_K *
0380 10000000,
0381 rate * post_div_m);
0382 synth_frac_div = synth_int_div % 10000000;
0383 synth_int_div /= 10000000;
0384
0385 if (synth_int_div <= SYNTH_MAX_INT_DIV)
0386 break;
0387 }
0388
0389 if (synth_int_div > SYNTH_MAX_INT_DIV)
0390 return -EINVAL;
0391
0392 v = readl_relaxed(synth->freq);
0393 v &= ~0x1fffffff;
0394 v |= (synth_int_div & SYNTH_MAX_INT_DIV) << 24;
0395 v |= (synth_frac_div & 0xffffff);
0396 v |= SYNTH_LDFREQ;
0397 writel_relaxed(v, synth->freq);
0398
0399 return post_div_m;
0400 }
0401
0402 static long ti_fapll_synth_round_rate(struct clk_hw *hw, unsigned long rate,
0403 unsigned long *parent_rate)
0404 {
0405 struct fapll_synth *synth = to_synth(hw);
0406 struct fapll_data *fd = synth->fd;
0407 unsigned long r;
0408
0409 if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate)
0410 return -EINVAL;
0411
0412
0413 if (!synth->freq) {
0414 unsigned long frac_rate;
0415 u32 synth_post_div_m;
0416
0417 frac_rate = ti_fapll_synth_get_frac_rate(hw, *parent_rate);
0418 synth_post_div_m = DIV_ROUND_UP(frac_rate, rate);
0419 r = DIV_ROUND_UP(frac_rate, synth_post_div_m);
0420 goto out;
0421 }
0422
0423 r = *parent_rate * SYNTH_PHASE_K;
0424 if (rate > r)
0425 goto out;
0426
0427 r = DIV_ROUND_UP_ULL(r, SYNTH_MAX_INT_DIV * SYNTH_MAX_DIV_M);
0428 if (rate < r)
0429 goto out;
0430
0431 r = rate;
0432 out:
0433 return r;
0434 }
0435
0436 static int ti_fapll_synth_set_rate(struct clk_hw *hw, unsigned long rate,
0437 unsigned long parent_rate)
0438 {
0439 struct fapll_synth *synth = to_synth(hw);
0440 struct fapll_data *fd = synth->fd;
0441 unsigned long frac_rate, post_rate = 0;
0442 u32 post_div_m = 0, v;
0443
0444 if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate)
0445 return -EINVAL;
0446
0447
0448 frac_rate = ti_fapll_synth_get_frac_rate(hw, parent_rate);
0449 if (frac_rate < rate) {
0450 if (!synth->freq)
0451 return -EINVAL;
0452 } else {
0453 post_div_m = DIV_ROUND_UP(frac_rate, rate);
0454 if (post_div_m && (post_div_m <= SYNTH_MAX_DIV_M))
0455 post_rate = DIV_ROUND_UP(frac_rate, post_div_m);
0456 if (!synth->freq && !post_rate)
0457 return -EINVAL;
0458 }
0459
0460
0461 if ((post_rate != rate) && synth->freq)
0462 post_div_m = ti_fapll_synth_set_frac_rate(synth,
0463 rate,
0464 parent_rate);
0465
0466 v = readl_relaxed(synth->div);
0467 v &= ~SYNTH_MAX_DIV_M;
0468 v |= post_div_m;
0469 v |= SYNTH_LDMDIV1;
0470 writel_relaxed(v, synth->div);
0471
0472 return 0;
0473 }
0474
0475 static const struct clk_ops ti_fapll_synt_ops = {
0476 .enable = ti_fapll_synth_enable,
0477 .disable = ti_fapll_synth_disable,
0478 .is_enabled = ti_fapll_synth_is_enabled,
0479 .recalc_rate = ti_fapll_synth_recalc_rate,
0480 .round_rate = ti_fapll_synth_round_rate,
0481 .set_rate = ti_fapll_synth_set_rate,
0482 };
0483
0484 static struct clk * __init ti_fapll_synth_setup(struct fapll_data *fd,
0485 void __iomem *freq,
0486 void __iomem *div,
0487 int index,
0488 const char *name,
0489 const char *parent,
0490 struct clk *pll_clk)
0491 {
0492 struct clk_init_data *init;
0493 struct fapll_synth *synth;
0494 struct clk *clk = ERR_PTR(-ENOMEM);
0495
0496 init = kzalloc(sizeof(*init), GFP_KERNEL);
0497 if (!init)
0498 return ERR_PTR(-ENOMEM);
0499
0500 init->ops = &ti_fapll_synt_ops;
0501 init->name = name;
0502 init->parent_names = &parent;
0503 init->num_parents = 1;
0504
0505 synth = kzalloc(sizeof(*synth), GFP_KERNEL);
0506 if (!synth)
0507 goto free;
0508
0509 synth->fd = fd;
0510 synth->index = index;
0511 synth->freq = freq;
0512 synth->div = div;
0513 synth->name = name;
0514 synth->hw.init = init;
0515 synth->clk_pll = pll_clk;
0516
0517 clk = clk_register(NULL, &synth->hw);
0518 if (IS_ERR(clk)) {
0519 pr_err("failed to register clock\n");
0520 goto free;
0521 }
0522
0523 return clk;
0524
0525 free:
0526 kfree(synth);
0527 kfree(init);
0528
0529 return clk;
0530 }
0531
0532 static void __init ti_fapll_setup(struct device_node *node)
0533 {
0534 struct fapll_data *fd;
0535 struct clk_init_data *init = NULL;
0536 const char *parent_name[2];
0537 struct clk *pll_clk;
0538 const char *name;
0539 int i;
0540
0541 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
0542 if (!fd)
0543 return;
0544
0545 fd->outputs.clks = kzalloc(sizeof(struct clk *) *
0546 MAX_FAPLL_OUTPUTS + 1,
0547 GFP_KERNEL);
0548 if (!fd->outputs.clks)
0549 goto free;
0550
0551 init = kzalloc(sizeof(*init), GFP_KERNEL);
0552 if (!init)
0553 goto free;
0554
0555 init->ops = &ti_fapll_ops;
0556 name = ti_dt_clk_name(node);
0557 init->name = name;
0558
0559 init->num_parents = of_clk_get_parent_count(node);
0560 if (init->num_parents != 2) {
0561 pr_err("%pOFn must have two parents\n", node);
0562 goto free;
0563 }
0564
0565 of_clk_parent_fill(node, parent_name, 2);
0566 init->parent_names = parent_name;
0567
0568 fd->clk_ref = of_clk_get(node, 0);
0569 if (IS_ERR(fd->clk_ref)) {
0570 pr_err("%pOFn could not get clk_ref\n", node);
0571 goto free;
0572 }
0573
0574 fd->clk_bypass = of_clk_get(node, 1);
0575 if (IS_ERR(fd->clk_bypass)) {
0576 pr_err("%pOFn could not get clk_bypass\n", node);
0577 goto free;
0578 }
0579
0580 fd->base = of_iomap(node, 0);
0581 if (!fd->base) {
0582 pr_err("%pOFn could not get IO base\n", node);
0583 goto free;
0584 }
0585
0586 if (fapll_is_ddr_pll(fd->base))
0587 fd->bypass_bit_inverted = true;
0588
0589 fd->name = name;
0590 fd->hw.init = init;
0591
0592
0593 pll_clk = clk_register(NULL, &fd->hw);
0594 if (IS_ERR(pll_clk))
0595 goto unmap;
0596
0597 fd->outputs.clks[0] = pll_clk;
0598 fd->outputs.clk_num++;
0599
0600
0601
0602
0603
0604
0605
0606
0607 for (i = 0; i < MAX_FAPLL_OUTPUTS; i++) {
0608 const char *output_name;
0609 void __iomem *freq, *div;
0610 struct clk *synth_clk;
0611 int output_instance;
0612 u32 v;
0613
0614 if (of_property_read_string_index(node, "clock-output-names",
0615 i, &output_name))
0616 continue;
0617
0618 if (of_property_read_u32_index(node, "clock-indices", i,
0619 &output_instance))
0620 output_instance = i;
0621
0622 freq = fd->base + (output_instance * 8);
0623 div = freq + 4;
0624
0625
0626 if (is_audio_pll_clk1(freq)) {
0627 freq = NULL;
0628 div = NULL;
0629 } else {
0630
0631 v = readl_relaxed(freq);
0632 if (!v)
0633 freq = NULL;
0634 }
0635 synth_clk = ti_fapll_synth_setup(fd, freq, div, output_instance,
0636 output_name, name, pll_clk);
0637 if (IS_ERR(synth_clk))
0638 continue;
0639
0640 fd->outputs.clks[output_instance] = synth_clk;
0641 fd->outputs.clk_num++;
0642
0643 clk_register_clkdev(synth_clk, output_name, NULL);
0644 }
0645
0646
0647 of_clk_add_provider(node, of_clk_src_onecell_get, &fd->outputs);
0648
0649
0650 kfree(init);
0651
0652 return;
0653
0654 unmap:
0655 iounmap(fd->base);
0656 free:
0657 if (fd->clk_bypass)
0658 clk_put(fd->clk_bypass);
0659 if (fd->clk_ref)
0660 clk_put(fd->clk_ref);
0661 kfree(fd->outputs.clks);
0662 kfree(fd);
0663 kfree(init);
0664 }
0665
0666 CLK_OF_DECLARE(ti_fapll_clock, "ti,dm816-fapll-clock", ti_fapll_setup);