1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b1823d86SPaul Walmsley /*
3b1823d86SPaul Walmsley * DPLL + CORE_CLK composite clock functions
4b1823d86SPaul Walmsley *
5b1823d86SPaul Walmsley * Copyright (C) 2005-2008 Texas Instruments, Inc.
6b1823d86SPaul Walmsley * Copyright (C) 2004-2010 Nokia Corporation
7b1823d86SPaul Walmsley *
8b1823d86SPaul Walmsley * Contacts:
9b1823d86SPaul Walmsley * Richard Woodruff <r-woodruff2@ti.com>
10b1823d86SPaul Walmsley * Paul Walmsley
11b1823d86SPaul Walmsley *
12b1823d86SPaul Walmsley * Based on earlier work by Tuukka Tikkanen, Tony Lindgren,
13b1823d86SPaul Walmsley * Gordon McNutt and RidgeRun, Inc.
14b1823d86SPaul Walmsley *
15b1823d86SPaul Walmsley * XXX The DPLL and CORE clocks should be split into two separate clock
16b1823d86SPaul Walmsley * types.
17b1823d86SPaul Walmsley */
18b1823d86SPaul Walmsley #undef DEBUG
19b1823d86SPaul Walmsley
20b1823d86SPaul Walmsley #include <linux/kernel.h>
21b1823d86SPaul Walmsley #include <linux/errno.h>
22b1823d86SPaul Walmsley #include <linux/clk.h>
23*cbcf7833SArnd Bergmann #include <linux/clk/ti.h>
24b1823d86SPaul Walmsley #include <linux/io.h>
25b1823d86SPaul Walmsley
26b1823d86SPaul Walmsley #include "clock.h"
27b1823d86SPaul Walmsley #include "clock2xxx.h"
28b1823d86SPaul Walmsley #include "opp2xxx.h"
29d9a16f9aSPaul Walmsley #include "cm2xxx.h"
30b1823d86SPaul Walmsley #include "cm-regbits-24xx.h"
313e6ece13SPaul Walmsley #include "sdrc.h"
32bf027ca1STony Lindgren #include "sram.h"
33b1823d86SPaul Walmsley
34b1823d86SPaul Walmsley /* #define DOWN_VARIABLE_DPLL 1 */ /* Experimental */
35b1823d86SPaul Walmsley
365f039377SPaul Walmsley /*
375f039377SPaul Walmsley * dpll_core_ck: pointer to the combined dpll_ck + core_ck on OMAP2xxx
385f039377SPaul Walmsley * (currently defined as "dpll_ck" in the OMAP2xxx clock tree). Set
395f039377SPaul Walmsley * during dpll_ck init and used later by omap2xxx_clk_get_core_rate().
405f039377SPaul Walmsley */
41ed1ebc49SRajendra Nayak static struct clk_hw_omap *dpll_core_ck;
425f039377SPaul Walmsley
43b1823d86SPaul Walmsley /**
44b1823d86SPaul Walmsley * omap2xxx_clk_get_core_rate - return the CORE_CLK rate
45b1823d86SPaul Walmsley *
46b1823d86SPaul Walmsley * Returns the CORE_CLK rate. CORE_CLK can have one of three rate
47b1823d86SPaul Walmsley * sources on OMAP2xxx: the DPLL CLKOUT rate, DPLL CLKOUTX2, or 32KHz
48b1823d86SPaul Walmsley * (the latter is unusual). This currently should be called with
49b1823d86SPaul Walmsley * struct clk *dpll_ck, which is a composite clock of dpll_ck and
50b1823d86SPaul Walmsley * core_ck.
51b1823d86SPaul Walmsley */
omap2xxx_clk_get_core_rate(void)525f039377SPaul Walmsley unsigned long omap2xxx_clk_get_core_rate(void)
53b1823d86SPaul Walmsley {
54b1823d86SPaul Walmsley long long core_clk;
55b1823d86SPaul Walmsley u32 v;
56b1823d86SPaul Walmsley
575f039377SPaul Walmsley WARN_ON(!dpll_core_ck);
585f039377SPaul Walmsley
595f039377SPaul Walmsley core_clk = omap2_get_dpll_rate(dpll_core_ck);
60b1823d86SPaul Walmsley
61cd6e9db2STero Kristo v = omap2xxx_cm_get_core_clk_src();
62b1823d86SPaul Walmsley
63b1823d86SPaul Walmsley if (v == CORE_CLK_SRC_32K)
64b1823d86SPaul Walmsley core_clk = 32768;
65b1823d86SPaul Walmsley else
66b1823d86SPaul Walmsley core_clk *= v;
67b1823d86SPaul Walmsley
68b1823d86SPaul Walmsley return core_clk;
69b1823d86SPaul Walmsley }
70b1823d86SPaul Walmsley
71b1823d86SPaul Walmsley /*
72b1823d86SPaul Walmsley * Uses the current prcm set to tell if a rate is valid.
73b1823d86SPaul Walmsley * You can go slower, but not faster within a given rate set.
74b1823d86SPaul Walmsley */
omap2_dpllcore_round_rate(unsigned long target_rate)75b1823d86SPaul Walmsley static long omap2_dpllcore_round_rate(unsigned long target_rate)
76b1823d86SPaul Walmsley {
77b1823d86SPaul Walmsley u32 high, low, core_clk_src;
78b1823d86SPaul Walmsley
79cd6e9db2STero Kristo core_clk_src = omap2xxx_cm_get_core_clk_src();
80b1823d86SPaul Walmsley
81b1823d86SPaul Walmsley if (core_clk_src == CORE_CLK_SRC_DPLL) { /* DPLL clockout */
82b1823d86SPaul Walmsley high = curr_prcm_set->dpll_speed * 2;
83b1823d86SPaul Walmsley low = curr_prcm_set->dpll_speed;
84b1823d86SPaul Walmsley } else { /* DPLL clockout x 2 */
85b1823d86SPaul Walmsley high = curr_prcm_set->dpll_speed;
86b1823d86SPaul Walmsley low = curr_prcm_set->dpll_speed / 2;
87b1823d86SPaul Walmsley }
88b1823d86SPaul Walmsley
89b1823d86SPaul Walmsley #ifdef DOWN_VARIABLE_DPLL
90b1823d86SPaul Walmsley if (target_rate > high)
91b1823d86SPaul Walmsley return high;
92b1823d86SPaul Walmsley else
93b1823d86SPaul Walmsley return target_rate;
94b1823d86SPaul Walmsley #else
95b1823d86SPaul Walmsley if (target_rate > low)
96b1823d86SPaul Walmsley return high;
97b1823d86SPaul Walmsley else
98b1823d86SPaul Walmsley return low;
99b1823d86SPaul Walmsley #endif
100b1823d86SPaul Walmsley
101b1823d86SPaul Walmsley }
102b1823d86SPaul Walmsley
omap2_dpllcore_recalc(struct clk_hw * hw,unsigned long parent_rate)103ed1ebc49SRajendra Nayak unsigned long omap2_dpllcore_recalc(struct clk_hw *hw,
104ed1ebc49SRajendra Nayak unsigned long parent_rate)
105b1823d86SPaul Walmsley {
1065f039377SPaul Walmsley return omap2xxx_clk_get_core_rate();
107b1823d86SPaul Walmsley }
108b1823d86SPaul Walmsley
omap2_reprogram_dpllcore(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)109ed1ebc49SRajendra Nayak int omap2_reprogram_dpllcore(struct clk_hw *hw, unsigned long rate,
110ed1ebc49SRajendra Nayak unsigned long parent_rate)
111b1823d86SPaul Walmsley {
112ed1ebc49SRajendra Nayak struct clk_hw_omap *clk = to_clk_hw_omap(hw);
113b1823d86SPaul Walmsley u32 cur_rate, low, mult, div, valid_rate, done_rate;
114b1823d86SPaul Walmsley u32 bypass = 0;
115b1823d86SPaul Walmsley struct prcm_config tmpset;
116b1823d86SPaul Walmsley const struct dpll_data *dd;
117b1823d86SPaul Walmsley
1185f039377SPaul Walmsley cur_rate = omap2xxx_clk_get_core_rate();
119cd6e9db2STero Kristo mult = omap2xxx_cm_get_core_clk_src();
120b1823d86SPaul Walmsley
121b1823d86SPaul Walmsley if ((rate == (cur_rate / 2)) && (mult == 2)) {
122b1823d86SPaul Walmsley omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL, 1);
123b1823d86SPaul Walmsley } else if ((rate == (cur_rate * 2)) && (mult == 1)) {
124b1823d86SPaul Walmsley omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
125b1823d86SPaul Walmsley } else if (rate != cur_rate) {
126b1823d86SPaul Walmsley valid_rate = omap2_dpllcore_round_rate(rate);
127b1823d86SPaul Walmsley if (valid_rate != rate)
128b1823d86SPaul Walmsley return -EINVAL;
129b1823d86SPaul Walmsley
130b1823d86SPaul Walmsley if (mult == 1)
131b1823d86SPaul Walmsley low = curr_prcm_set->dpll_speed;
132b1823d86SPaul Walmsley else
133b1823d86SPaul Walmsley low = curr_prcm_set->dpll_speed / 2;
134b1823d86SPaul Walmsley
135b1823d86SPaul Walmsley dd = clk->dpll_data;
136b1823d86SPaul Walmsley if (!dd)
137b1823d86SPaul Walmsley return -EINVAL;
138b1823d86SPaul Walmsley
1396c0afb50STero Kristo tmpset.cm_clksel1_pll =
1406c0afb50STero Kristo omap_clk_ll_ops.clk_readl(&dd->mult_div1_reg);
141b1823d86SPaul Walmsley tmpset.cm_clksel1_pll &= ~(dd->mult_mask |
142b1823d86SPaul Walmsley dd->div1_mask);
143b1823d86SPaul Walmsley div = ((curr_prcm_set->xtal_speed / 1000000) - 1);
144cd6e9db2STero Kristo tmpset.cm_clksel2_pll = omap2xxx_cm_get_core_pll_config();
145b1823d86SPaul Walmsley tmpset.cm_clksel2_pll &= ~OMAP24XX_CORE_CLK_SRC_MASK;
146b1823d86SPaul Walmsley if (rate > low) {
147b1823d86SPaul Walmsley tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL_X2;
148b1823d86SPaul Walmsley mult = ((rate / 2) / 1000000);
149b1823d86SPaul Walmsley done_rate = CORE_CLK_SRC_DPLL_X2;
150b1823d86SPaul Walmsley } else {
151b1823d86SPaul Walmsley tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL;
152b1823d86SPaul Walmsley mult = (rate / 1000000);
153b1823d86SPaul Walmsley done_rate = CORE_CLK_SRC_DPLL;
154b1823d86SPaul Walmsley }
155b1823d86SPaul Walmsley tmpset.cm_clksel1_pll |= (div << __ffs(dd->mult_mask));
156b1823d86SPaul Walmsley tmpset.cm_clksel1_pll |= (mult << __ffs(dd->div1_mask));
157b1823d86SPaul Walmsley
158b1823d86SPaul Walmsley /* Worst case */
159b1823d86SPaul Walmsley tmpset.base_sdrc_rfr = SDRC_RFR_CTRL_BYPASS;
160b1823d86SPaul Walmsley
161b1823d86SPaul Walmsley if (rate == curr_prcm_set->xtal_speed) /* If asking for 1-1 */
162b1823d86SPaul Walmsley bypass = 1;
163b1823d86SPaul Walmsley
164b1823d86SPaul Walmsley /* For omap2xxx_sdrc_init_params() */
165b1823d86SPaul Walmsley omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
166b1823d86SPaul Walmsley
167b1823d86SPaul Walmsley /* Force dll lock mode */
168b1823d86SPaul Walmsley omap2_set_prcm(tmpset.cm_clksel1_pll, tmpset.base_sdrc_rfr,
169b1823d86SPaul Walmsley bypass);
170b1823d86SPaul Walmsley
171b1823d86SPaul Walmsley /* Errata: ret dll entry state */
172b1823d86SPaul Walmsley omap2xxx_sdrc_init_params(omap2xxx_sdrc_dll_is_unlocked());
173b1823d86SPaul Walmsley omap2xxx_sdrc_reprogram(done_rate, 0);
174b1823d86SPaul Walmsley }
175b1823d86SPaul Walmsley
176b1823d86SPaul Walmsley return 0;
177b1823d86SPaul Walmsley }
178b1823d86SPaul Walmsley
1795f039377SPaul Walmsley /**
1805f039377SPaul Walmsley * omap2xxx_clkt_dpllcore_init - clk init function for dpll_ck
1815f039377SPaul Walmsley * @clk: struct clk *dpll_ck
1825f039377SPaul Walmsley *
1835f039377SPaul Walmsley * Store a local copy of @clk in dpll_core_ck so other code can query
1845f039377SPaul Walmsley * the core rate without having to clk_get(), which can sleep. Must
1855f039377SPaul Walmsley * only be called once. No return value. XXX If the clock
1865f039377SPaul Walmsley * registration process is ever changed such that dpll_ck is no longer
1875f039377SPaul Walmsley * statically defined, this code may need to change to increment some
1885f039377SPaul Walmsley * kind of use count on dpll_ck.
1895f039377SPaul Walmsley */
omap2xxx_clkt_dpllcore_init(struct clk_hw * hw)190ed1ebc49SRajendra Nayak void omap2xxx_clkt_dpllcore_init(struct clk_hw *hw)
1915f039377SPaul Walmsley {
1925f039377SPaul Walmsley WARN(dpll_core_ck, "dpll_core_ck already set - should never happen");
193ed1ebc49SRajendra Nayak dpll_core_ck = to_clk_hw_omap(hw);
1945f039377SPaul Walmsley }
195