2 * Copyright (C) 2014 Broadcom Corporation
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/kernel.h>
15 #include <linux/err.h>
16 #include <linux/clk-provider.h>
19 #include <linux/clkdev.h>
20 #include <linux/of_address.h>
21 #include <linux/delay.h>
23 #include "clk-iproc.h"
25 #define PLL_VCO_HIGH_SHIFT 19
26 #define PLL_VCO_LOW_SHIFT 30
28 /* number of delay loops waiting for PLL to lock */
29 #define LOCK_DELAY 100
31 /* number of VCO frequency bands */
32 #define NUM_FREQ_BANDS 8
34 #define NUM_KP_BANDS 3
41 static const unsigned int kp_table[NUM_KP_BANDS][NUM_FREQ_BANDS] = {
42 { 5, 6, 6, 7, 7, 8, 9, 10 },
43 { 4, 4, 5, 5, 6, 7, 8, 9 },
44 { 4, 5, 5, 6, 7, 8, 9, 10 },
47 static const unsigned long ref_freq_table[NUM_FREQ_BANDS][2] = {
48 { 10000000, 12500000 },
49 { 12500000, 15000000 },
50 { 15000000, 20000000 },
51 { 20000000, 25000000 },
52 { 25000000, 50000000 },
53 { 50000000, 75000000 },
54 { 75000000, 100000000 },
55 { 100000000, 125000000 },
60 VCO_MID = 1200000000U,
61 VCO_HIGH = 2200000000U,
62 VCO_HIGH_HIGH = 3100000000U,
63 VCO_MAX = 4000000000U,
71 struct iproc_pll *pll;
73 const struct iproc_clk_ctrl *ctrl;
77 void __iomem *pll_base;
78 void __iomem *pwr_base;
79 void __iomem *asiu_base;
81 const struct iproc_pll_ctrl *ctrl;
82 const struct iproc_pll_vco_param *vco_param;
83 unsigned int num_vco_entries;
85 struct clk_onecell_data clk_data;
86 struct iproc_clk *clks;
89 #define to_iproc_clk(hw) container_of(hw, struct iproc_clk, hw)
92 * Based on the target frequency, find a match from the VCO frequency parameter
93 * table and return its index
95 static int pll_get_rate_index(struct iproc_pll *pll, unsigned int target_rate)
99 for (i = 0; i < pll->num_vco_entries; i++)
100 if (target_rate == pll->vco_param[i].rate)
103 if (i >= pll->num_vco_entries)
109 static int get_kp(unsigned long ref_freq, enum kp_band kp_index)
113 if (ref_freq < ref_freq_table[0][0])
116 for (i = 0; i < NUM_FREQ_BANDS; i++) {
117 if (ref_freq >= ref_freq_table[i][0] &&
118 ref_freq < ref_freq_table[i][1])
119 return kp_table[kp_index][i];
124 static int pll_wait_for_lock(struct iproc_pll *pll)
127 const struct iproc_pll_ctrl *ctrl = pll->ctrl;
129 for (i = 0; i < LOCK_DELAY; i++) {
130 u32 val = readl(pll->pll_base + ctrl->status.offset);
132 if (val & (1 << ctrl->status.shift))
140 static void __pll_disable(struct iproc_pll *pll)
142 const struct iproc_pll_ctrl *ctrl = pll->ctrl;
145 if (ctrl->flags & IPROC_CLK_PLL_ASIU) {
146 val = readl(pll->asiu_base + ctrl->asiu.offset);
147 val &= ~(1 << ctrl->asiu.en_shift);
148 writel(val, pll->asiu_base + ctrl->asiu.offset);
151 /* latch input value so core power can be shut down */
152 val = readl(pll->pwr_base + ctrl->aon.offset);
153 val |= (1 << ctrl->aon.iso_shift);
154 writel(val, pll->pwr_base + ctrl->aon.offset);
156 /* power down the core */
157 val &= ~(bit_mask(ctrl->aon.pwr_width) << ctrl->aon.pwr_shift);
158 writel(val, pll->pwr_base + ctrl->aon.offset);
161 static int __pll_enable(struct iproc_pll *pll)
163 const struct iproc_pll_ctrl *ctrl = pll->ctrl;
166 /* power up the PLL and make sure it's not latched */
167 val = readl(pll->pwr_base + ctrl->aon.offset);
168 val |= bit_mask(ctrl->aon.pwr_width) << ctrl->aon.pwr_shift;
169 val &= ~(1 << ctrl->aon.iso_shift);
170 writel(val, pll->pwr_base + ctrl->aon.offset);
172 /* certain PLLs also need to be ungated from the ASIU top level */
173 if (ctrl->flags & IPROC_CLK_PLL_ASIU) {
174 val = readl(pll->asiu_base + ctrl->asiu.offset);
175 val |= (1 << ctrl->asiu.en_shift);
176 writel(val, pll->asiu_base + ctrl->asiu.offset);
182 static void __pll_put_in_reset(struct iproc_pll *pll)
185 const struct iproc_pll_ctrl *ctrl = pll->ctrl;
186 const struct iproc_pll_reset_ctrl *reset = &ctrl->reset;
188 val = readl(pll->pll_base + reset->offset);
189 val &= ~(1 << reset->reset_shift | 1 << reset->p_reset_shift);
190 writel(val, pll->pll_base + reset->offset);
191 if (unlikely(ctrl->flags & IPROC_CLK_NEEDS_READ_BACK))
192 readl(pll->pll_base + reset->offset);
195 static void __pll_bring_out_reset(struct iproc_pll *pll, unsigned int kp,
196 unsigned int ka, unsigned int ki)
199 const struct iproc_pll_ctrl *ctrl = pll->ctrl;
200 const struct iproc_pll_reset_ctrl *reset = &ctrl->reset;
202 val = readl(pll->pll_base + reset->offset);
203 val &= ~(bit_mask(reset->ki_width) << reset->ki_shift |
204 bit_mask(reset->kp_width) << reset->kp_shift |
205 bit_mask(reset->ka_width) << reset->ka_shift);
206 val |= ki << reset->ki_shift | kp << reset->kp_shift |
207 ka << reset->ka_shift;
208 val |= 1 << reset->reset_shift | 1 << reset->p_reset_shift;
209 writel(val, pll->pll_base + reset->offset);
210 if (unlikely(ctrl->flags & IPROC_CLK_NEEDS_READ_BACK))
211 readl(pll->pll_base + reset->offset);
214 static int pll_set_rate(struct iproc_clk *clk, unsigned int rate_index,
215 unsigned long parent_rate)
217 struct iproc_pll *pll = clk->pll;
218 const struct iproc_pll_vco_param *vco = &pll->vco_param[rate_index];
219 const struct iproc_pll_ctrl *ctrl = pll->ctrl;
220 int ka = 0, ki, kp, ret;
221 unsigned long rate = vco->rate;
223 enum kp_band kp_index;
224 unsigned long ref_freq;
227 * reference frequency = parent frequency / PDIV
228 * If PDIV = 0, then it becomes a multiplier (x2)
231 ref_freq = parent_rate * 2;
233 ref_freq = parent_rate / vco->pdiv;
235 /* determine Ki and Kp index based on target VCO frequency */
236 if (rate >= VCO_LOW && rate < VCO_HIGH) {
238 kp_index = KP_BAND_MID;
239 } else if (rate >= VCO_HIGH && rate && rate < VCO_HIGH_HIGH) {
241 kp_index = KP_BAND_HIGH;
242 } else if (rate >= VCO_HIGH_HIGH && rate < VCO_MAX) {
244 kp_index = KP_BAND_HIGH_HIGH;
246 pr_err("%s: pll: %s has invalid rate: %lu\n", __func__,
251 kp = get_kp(ref_freq, kp_index);
253 pr_err("%s: pll: %s has invalid kp\n", __func__, clk->name);
257 ret = __pll_enable(pll);
259 pr_err("%s: pll: %s fails to enable\n", __func__, clk->name);
263 /* put PLL in reset */
264 __pll_put_in_reset(pll);
266 writel(0, pll->pll_base + ctrl->vco_ctrl.u_offset);
267 if (unlikely(ctrl->flags & IPROC_CLK_NEEDS_READ_BACK))
268 readl(pll->pll_base + ctrl->vco_ctrl.u_offset);
269 val = readl(pll->pll_base + ctrl->vco_ctrl.l_offset);
271 if (rate >= VCO_LOW && rate < VCO_MID)
272 val |= (1 << PLL_VCO_LOW_SHIFT);
275 val &= ~(1 << PLL_VCO_HIGH_SHIFT);
277 val |= (1 << PLL_VCO_HIGH_SHIFT);
279 writel(val, pll->pll_base + ctrl->vco_ctrl.l_offset);
280 if (unlikely(ctrl->flags & IPROC_CLK_NEEDS_READ_BACK))
281 readl(pll->pll_base + ctrl->vco_ctrl.l_offset);
283 /* program integer part of NDIV */
284 val = readl(pll->pll_base + ctrl->ndiv_int.offset);
285 val &= ~(bit_mask(ctrl->ndiv_int.width) << ctrl->ndiv_int.shift);
286 val |= vco->ndiv_int << ctrl->ndiv_int.shift;
287 writel(val, pll->pll_base + ctrl->ndiv_int.offset);
288 if (unlikely(ctrl->flags & IPROC_CLK_NEEDS_READ_BACK))
289 readl(pll->pll_base + ctrl->ndiv_int.offset);
291 /* program fractional part of NDIV */
292 if (ctrl->flags & IPROC_CLK_PLL_HAS_NDIV_FRAC) {
293 val = readl(pll->pll_base + ctrl->ndiv_frac.offset);
294 val &= ~(bit_mask(ctrl->ndiv_frac.width) <<
295 ctrl->ndiv_frac.shift);
296 val |= vco->ndiv_frac << ctrl->ndiv_frac.shift;
297 writel(val, pll->pll_base + ctrl->ndiv_frac.offset);
298 if (unlikely(ctrl->flags & IPROC_CLK_NEEDS_READ_BACK))
299 readl(pll->pll_base + ctrl->ndiv_frac.offset);
303 val = readl(pll->pll_base + ctrl->pdiv.offset);
304 val &= ~(bit_mask(ctrl->pdiv.width) << ctrl->pdiv.shift);
305 val |= vco->pdiv << ctrl->pdiv.shift;
306 writel(val, pll->pll_base + ctrl->pdiv.offset);
307 if (unlikely(ctrl->flags & IPROC_CLK_NEEDS_READ_BACK))
308 readl(pll->pll_base + ctrl->pdiv.offset);
310 __pll_bring_out_reset(pll, kp, ka, ki);
312 ret = pll_wait_for_lock(pll);
314 pr_err("%s: pll: %s failed to lock\n", __func__, clk->name);
321 static int iproc_pll_enable(struct clk_hw *hw)
323 struct iproc_clk *clk = to_iproc_clk(hw);
324 struct iproc_pll *pll = clk->pll;
326 return __pll_enable(pll);
329 static void iproc_pll_disable(struct clk_hw *hw)
331 struct iproc_clk *clk = to_iproc_clk(hw);
332 struct iproc_pll *pll = clk->pll;
333 const struct iproc_pll_ctrl *ctrl = pll->ctrl;
335 if (ctrl->flags & IPROC_CLK_AON)
341 static unsigned long iproc_pll_recalc_rate(struct clk_hw *hw,
342 unsigned long parent_rate)
344 struct iproc_clk *clk = to_iproc_clk(hw);
345 struct iproc_pll *pll = clk->pll;
346 const struct iproc_pll_ctrl *ctrl = pll->ctrl;
348 u64 ndiv, ndiv_int, ndiv_frac;
351 if (parent_rate == 0)
354 /* PLL needs to be locked */
355 val = readl(pll->pll_base + ctrl->status.offset);
356 if ((val & (1 << ctrl->status.shift)) == 0) {
362 * PLL output frequency =
364 * ((ndiv_int + ndiv_frac / 2^20) * (parent clock rate / pdiv)
366 val = readl(pll->pll_base + ctrl->ndiv_int.offset);
367 ndiv_int = (val >> ctrl->ndiv_int.shift) &
368 bit_mask(ctrl->ndiv_int.width);
369 ndiv = ndiv_int << 20;
371 if (ctrl->flags & IPROC_CLK_PLL_HAS_NDIV_FRAC) {
372 val = readl(pll->pll_base + ctrl->ndiv_frac.offset);
373 ndiv_frac = (val >> ctrl->ndiv_frac.shift) &
374 bit_mask(ctrl->ndiv_frac.width);
378 val = readl(pll->pll_base + ctrl->pdiv.offset);
379 pdiv = (val >> ctrl->pdiv.shift) & bit_mask(ctrl->pdiv.width);
381 clk->rate = (ndiv * parent_rate) >> 20;
391 static long iproc_pll_round_rate(struct clk_hw *hw, unsigned long rate,
392 unsigned long *parent_rate)
395 struct iproc_clk *clk = to_iproc_clk(hw);
396 struct iproc_pll *pll = clk->pll;
398 if (rate == 0 || *parent_rate == 0 || !pll->vco_param)
401 for (i = 0; i < pll->num_vco_entries; i++) {
402 if (rate <= pll->vco_param[i].rate)
406 if (i == pll->num_vco_entries)
409 return pll->vco_param[i].rate;
412 static int iproc_pll_set_rate(struct clk_hw *hw, unsigned long rate,
413 unsigned long parent_rate)
415 struct iproc_clk *clk = to_iproc_clk(hw);
416 struct iproc_pll *pll = clk->pll;
419 rate_index = pll_get_rate_index(pll, rate);
423 ret = pll_set_rate(clk, rate_index, parent_rate);
427 static const struct clk_ops iproc_pll_ops = {
428 .enable = iproc_pll_enable,
429 .disable = iproc_pll_disable,
430 .recalc_rate = iproc_pll_recalc_rate,
431 .round_rate = iproc_pll_round_rate,
432 .set_rate = iproc_pll_set_rate,
435 static int iproc_clk_enable(struct clk_hw *hw)
437 struct iproc_clk *clk = to_iproc_clk(hw);
438 const struct iproc_clk_ctrl *ctrl = clk->ctrl;
439 struct iproc_pll *pll = clk->pll;
442 /* channel enable is active low */
443 val = readl(pll->pll_base + ctrl->enable.offset);
444 val &= ~(1 << ctrl->enable.enable_shift);
445 writel(val, pll->pll_base + ctrl->enable.offset);
447 /* also make sure channel is not held */
448 val = readl(pll->pll_base + ctrl->enable.offset);
449 val &= ~(1 << ctrl->enable.hold_shift);
450 writel(val, pll->pll_base + ctrl->enable.offset);
451 if (unlikely(ctrl->flags & IPROC_CLK_NEEDS_READ_BACK))
452 readl(pll->pll_base + ctrl->enable.offset);
457 static void iproc_clk_disable(struct clk_hw *hw)
459 struct iproc_clk *clk = to_iproc_clk(hw);
460 const struct iproc_clk_ctrl *ctrl = clk->ctrl;
461 struct iproc_pll *pll = clk->pll;
464 if (ctrl->flags & IPROC_CLK_AON)
467 val = readl(pll->pll_base + ctrl->enable.offset);
468 val |= 1 << ctrl->enable.enable_shift;
469 writel(val, pll->pll_base + ctrl->enable.offset);
470 if (unlikely(ctrl->flags & IPROC_CLK_NEEDS_READ_BACK))
471 readl(pll->pll_base + ctrl->enable.offset);
474 static unsigned long iproc_clk_recalc_rate(struct clk_hw *hw,
475 unsigned long parent_rate)
477 struct iproc_clk *clk = to_iproc_clk(hw);
478 const struct iproc_clk_ctrl *ctrl = clk->ctrl;
479 struct iproc_pll *pll = clk->pll;
483 if (parent_rate == 0)
486 val = readl(pll->pll_base + ctrl->mdiv.offset);
487 mdiv = (val >> ctrl->mdiv.shift) & bit_mask(ctrl->mdiv.width);
491 clk->rate = parent_rate / mdiv;
496 static long iproc_clk_round_rate(struct clk_hw *hw, unsigned long rate,
497 unsigned long *parent_rate)
501 if (rate == 0 || *parent_rate == 0)
504 if (rate == *parent_rate)
507 div = DIV_ROUND_UP(*parent_rate, rate);
514 return *parent_rate / div;
517 static int iproc_clk_set_rate(struct clk_hw *hw, unsigned long rate,
518 unsigned long parent_rate)
520 struct iproc_clk *clk = to_iproc_clk(hw);
521 const struct iproc_clk_ctrl *ctrl = clk->ctrl;
522 struct iproc_pll *pll = clk->pll;
526 if (rate == 0 || parent_rate == 0)
529 div = DIV_ROUND_UP(parent_rate, rate);
533 val = readl(pll->pll_base + ctrl->mdiv.offset);
535 val &= ~(bit_mask(ctrl->mdiv.width) << ctrl->mdiv.shift);
537 val &= ~(bit_mask(ctrl->mdiv.width) << ctrl->mdiv.shift);
538 val |= div << ctrl->mdiv.shift;
540 writel(val, pll->pll_base + ctrl->mdiv.offset);
541 if (unlikely(ctrl->flags & IPROC_CLK_NEEDS_READ_BACK))
542 readl(pll->pll_base + ctrl->mdiv.offset);
543 clk->rate = parent_rate / div;
548 static const struct clk_ops iproc_clk_ops = {
549 .enable = iproc_clk_enable,
550 .disable = iproc_clk_disable,
551 .recalc_rate = iproc_clk_recalc_rate,
552 .round_rate = iproc_clk_round_rate,
553 .set_rate = iproc_clk_set_rate,
557 * Some PLLs require the PLL SW override bit to be set before changes can be
560 static void iproc_pll_sw_cfg(struct iproc_pll *pll)
562 const struct iproc_pll_ctrl *ctrl = pll->ctrl;
564 if (ctrl->flags & IPROC_CLK_PLL_NEEDS_SW_CFG) {
567 val = readl(pll->pll_base + ctrl->sw_ctrl.offset);
568 val |= BIT(ctrl->sw_ctrl.shift);
569 writel(val, pll->pll_base + ctrl->sw_ctrl.offset);
570 if (unlikely(ctrl->flags & IPROC_CLK_NEEDS_READ_BACK))
571 readl(pll->pll_base + ctrl->sw_ctrl.offset);
575 void __init iproc_pll_clk_setup(struct device_node *node,
576 const struct iproc_pll_ctrl *pll_ctrl,
577 const struct iproc_pll_vco_param *vco,
578 unsigned int num_vco_entries,
579 const struct iproc_clk_ctrl *clk_ctrl,
580 unsigned int num_clks)
584 struct iproc_pll *pll;
585 struct iproc_clk *iclk;
586 struct clk_init_data init;
587 const char *parent_name;
589 if (WARN_ON(!pll_ctrl) || WARN_ON(!clk_ctrl))
592 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
596 pll->clk_data.clk_num = num_clks;
597 pll->clk_data.clks = kcalloc(num_clks, sizeof(*pll->clk_data.clks),
599 if (WARN_ON(!pll->clk_data.clks))
602 pll->clks = kcalloc(num_clks, sizeof(*pll->clks), GFP_KERNEL);
603 if (WARN_ON(!pll->clks))
606 pll->pll_base = of_iomap(node, 0);
607 if (WARN_ON(!pll->pll_base))
610 pll->pwr_base = of_iomap(node, 1);
611 if (WARN_ON(!pll->pwr_base))
614 /* some PLLs require gating control at the top ASIU level */
615 if (pll_ctrl->flags & IPROC_CLK_PLL_ASIU) {
616 pll->asiu_base = of_iomap(node, 2);
617 if (WARN_ON(!pll->asiu_base))
621 /* initialize and register the PLL itself */
622 pll->ctrl = pll_ctrl;
624 iclk = &pll->clks[0];
626 iclk->name = node->name;
628 init.name = node->name;
629 init.ops = &iproc_pll_ops;
631 parent_name = of_clk_get_parent_name(node, 0);
632 init.parent_names = (parent_name ? &parent_name : NULL);
633 init.num_parents = (parent_name ? 1 : 0);
634 iclk->hw.init = &init;
637 pll->num_vco_entries = num_vco_entries;
638 pll->vco_param = vco;
641 iproc_pll_sw_cfg(pll);
643 clk = clk_register(NULL, &iclk->hw);
644 if (WARN_ON(IS_ERR(clk)))
645 goto err_pll_register;
647 pll->clk_data.clks[0] = clk;
649 /* now initialize and register all leaf clocks */
650 for (i = 1; i < num_clks; i++) {
651 const char *clk_name;
653 memset(&init, 0, sizeof(init));
654 parent_name = node->name;
656 ret = of_property_read_string_index(node, "clock-output-names",
659 goto err_clk_register;
661 iclk = &pll->clks[i];
662 iclk->name = clk_name;
664 iclk->ctrl = &clk_ctrl[i];
666 init.name = clk_name;
667 init.ops = &iproc_clk_ops;
669 init.parent_names = (parent_name ? &parent_name : NULL);
670 init.num_parents = (parent_name ? 1 : 0);
671 iclk->hw.init = &init;
673 clk = clk_register(NULL, &iclk->hw);
674 if (WARN_ON(IS_ERR(clk)))
675 goto err_clk_register;
677 pll->clk_data.clks[i] = clk;
680 ret = of_clk_add_provider(node, of_clk_src_onecell_get, &pll->clk_data);
682 goto err_clk_register;
687 for (i = 0; i < num_clks; i++)
688 clk_unregister(pll->clk_data.clks[i]);
692 iounmap(pll->asiu_base);
695 iounmap(pll->pwr_base);
698 iounmap(pll->pll_base);
704 kfree(pll->clk_data.clks);