Path: blob/master/arch/arm/mach-omap2/clkt2xxx_dpllcore.c
26292 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* DPLL + CORE_CLK composite clock functions3*4* Copyright (C) 2005-2008 Texas Instruments, Inc.5* Copyright (C) 2004-2010 Nokia Corporation6*7* Contacts:8* Richard Woodruff <[email protected]>9* Paul Walmsley10*11* Based on earlier work by Tuukka Tikkanen, Tony Lindgren,12* Gordon McNutt and RidgeRun, Inc.13*14* XXX The DPLL and CORE clocks should be split into two separate clock15* types.16*/17#undef DEBUG1819#include <linux/kernel.h>20#include <linux/errno.h>21#include <linux/clk.h>22#include <linux/clk/ti.h>23#include <linux/io.h>2425#include "clock.h"26#include "clock2xxx.h"27#include "opp2xxx.h"28#include "cm2xxx.h"29#include "cm-regbits-24xx.h"30#include "sdrc.h"31#include "sram.h"3233/* #define DOWN_VARIABLE_DPLL 1 */ /* Experimental */3435/*36* dpll_core_ck: pointer to the combined dpll_ck + core_ck on OMAP2xxx37* (currently defined as "dpll_ck" in the OMAP2xxx clock tree). Set38* during dpll_ck init and used later by omap2xxx_clk_get_core_rate().39*/40static struct clk_hw_omap *dpll_core_ck;4142/**43* omap2xxx_clk_get_core_rate - return the CORE_CLK rate44*45* Returns the CORE_CLK rate. CORE_CLK can have one of three rate46* sources on OMAP2xxx: the DPLL CLKOUT rate, DPLL CLKOUTX2, or 32KHz47* (the latter is unusual). This currently should be called with48* struct clk *dpll_ck, which is a composite clock of dpll_ck and49* core_ck.50*/51unsigned long omap2xxx_clk_get_core_rate(void)52{53long long core_clk;54u32 v;5556WARN_ON(!dpll_core_ck);5758core_clk = omap2_get_dpll_rate(dpll_core_ck);5960v = omap2xxx_cm_get_core_clk_src();6162if (v == CORE_CLK_SRC_32K)63core_clk = 32768;64else65core_clk *= v;6667return core_clk;68}6970/*71* Uses the current prcm set to tell if a rate is valid.72* You can go slower, but not faster within a given rate set.73*/74static long omap2_dpllcore_round_rate(unsigned long target_rate)75{76u32 high, low, core_clk_src;7778core_clk_src = omap2xxx_cm_get_core_clk_src();7980if (core_clk_src == CORE_CLK_SRC_DPLL) { /* DPLL clockout */81high = curr_prcm_set->dpll_speed * 2;82low = curr_prcm_set->dpll_speed;83} else { /* DPLL clockout x 2 */84high = curr_prcm_set->dpll_speed;85low = curr_prcm_set->dpll_speed / 2;86}8788#ifdef DOWN_VARIABLE_DPLL89if (target_rate > high)90return high;91else92return target_rate;93#else94if (target_rate > low)95return high;96else97return low;98#endif99100}101102unsigned long omap2_dpllcore_recalc(struct clk_hw *hw,103unsigned long parent_rate)104{105return omap2xxx_clk_get_core_rate();106}107108int omap2_reprogram_dpllcore(struct clk_hw *hw, unsigned long rate,109unsigned long parent_rate)110{111struct clk_hw_omap *clk = to_clk_hw_omap(hw);112u32 cur_rate, low, mult, div, valid_rate, done_rate;113u32 bypass = 0;114struct prcm_config tmpset;115const struct dpll_data *dd;116117cur_rate = omap2xxx_clk_get_core_rate();118mult = omap2xxx_cm_get_core_clk_src();119120if ((rate == (cur_rate / 2)) && (mult == 2)) {121omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL, 1);122} else if ((rate == (cur_rate * 2)) && (mult == 1)) {123omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);124} else if (rate != cur_rate) {125valid_rate = omap2_dpllcore_round_rate(rate);126if (valid_rate != rate)127return -EINVAL;128129if (mult == 1)130low = curr_prcm_set->dpll_speed;131else132low = curr_prcm_set->dpll_speed / 2;133134dd = clk->dpll_data;135if (!dd)136return -EINVAL;137138tmpset.cm_clksel1_pll =139omap_clk_ll_ops.clk_readl(&dd->mult_div1_reg);140tmpset.cm_clksel1_pll &= ~(dd->mult_mask |141dd->div1_mask);142div = ((curr_prcm_set->xtal_speed / 1000000) - 1);143tmpset.cm_clksel2_pll = omap2xxx_cm_get_core_pll_config();144tmpset.cm_clksel2_pll &= ~OMAP24XX_CORE_CLK_SRC_MASK;145if (rate > low) {146tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL_X2;147mult = ((rate / 2) / 1000000);148done_rate = CORE_CLK_SRC_DPLL_X2;149} else {150tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL;151mult = (rate / 1000000);152done_rate = CORE_CLK_SRC_DPLL;153}154tmpset.cm_clksel1_pll |= (div << __ffs(dd->mult_mask));155tmpset.cm_clksel1_pll |= (mult << __ffs(dd->div1_mask));156157/* Worst case */158tmpset.base_sdrc_rfr = SDRC_RFR_CTRL_BYPASS;159160if (rate == curr_prcm_set->xtal_speed) /* If asking for 1-1 */161bypass = 1;162163/* For omap2xxx_sdrc_init_params() */164omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);165166/* Force dll lock mode */167omap2_set_prcm(tmpset.cm_clksel1_pll, tmpset.base_sdrc_rfr,168bypass);169170/* Errata: ret dll entry state */171omap2xxx_sdrc_init_params(omap2xxx_sdrc_dll_is_unlocked());172omap2xxx_sdrc_reprogram(done_rate, 0);173}174175return 0;176}177178/**179* omap2xxx_clkt_dpllcore_init - clk init function for dpll_ck180* @clk: struct clk *dpll_ck181*182* Store a local copy of @clk in dpll_core_ck so other code can query183* the core rate without having to clk_get(), which can sleep. Must184* only be called once. No return value. XXX If the clock185* registration process is ever changed such that dpll_ck is no longer186* statically defined, this code may need to change to increment some187* kind of use count on dpll_ck.188*/189void omap2xxx_clkt_dpllcore_init(struct clk_hw *hw)190{191WARN(dpll_core_ck, "dpll_core_ck already set - should never happen");192dpll_core_ck = to_clk_hw_omap(hw);193}194195196