Path: blob/master/arch/arm/mach-omap2/clkt2xxx_virt_prcm_set.c
29266 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* OMAP2xxx DVFS virtual clock functions3*4* Copyright (C) 2005-2008, 2012 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 Some of this code should be replaceable by the upcoming OPP layer15* code. However, some notion of "rate set" is probably still necessary16* for OMAP2xxx at least. Rate sets should be generalized so they can be17* used for any OMAP chip, not just OMAP2xxx. In particular, Richard Woodruff18* has in the past expressed a preference to use rate sets for OPP changes,19* rather than dynamically recalculating the clock tree, so if someone wants20* this badly enough to write the code to handle it, we should support it21* as an option.22*/23#undef DEBUG2425#include <linux/kernel.h>26#include <linux/errno.h>27#include <linux/clk.h>28#include <linux/io.h>29#include <linux/cpufreq.h>30#include <linux/slab.h>3132#include "soc.h"33#include "clock.h"34#include "clock2xxx.h"35#include "opp2xxx.h"36#include "cm2xxx.h"37#include "cm-regbits-24xx.h"38#include "sdrc.h"39#include "sram.h"4041static u16 cpu_mask;4243const struct prcm_config *curr_prcm_set;44const struct prcm_config *rate_table;4546/*47* sys_ck_rate: the rate of the external high-frequency clock48* oscillator on the board. Set by the SoC-specific clock init code.49* Once set during a boot, will not change.50*/51static unsigned long sys_ck_rate;5253/**54* omap2_table_mpu_recalc - just return the MPU speed55* @clk: virt_prcm_set struct clk56*57* Set virt_prcm_set's rate to the mpu_speed field of the current PRCM set.58*/59static unsigned long omap2_table_mpu_recalc(struct clk_hw *clk,60unsigned long parent_rate)61{62return curr_prcm_set->mpu_speed;63}6465/*66* Look for a rate equal or less than the target rate given a configuration set.67*68* What's not entirely clear is "which" field represents the key field.69* Some might argue L3-DDR, others ARM, others IVA. This code is simple and70* just uses the ARM rates.71*/72static int omap2_determine_rate_to_table(struct clk_hw *hw,73struct clk_rate_request *req)74{75const struct prcm_config *ptr;76long highest_rate;7778highest_rate = -EINVAL;7980for (ptr = rate_table; ptr->mpu_speed; ptr++) {81if (!(ptr->flags & cpu_mask))82continue;83if (ptr->xtal_speed != sys_ck_rate)84continue;8586highest_rate = ptr->mpu_speed;8788/* Can check only after xtal frequency check */89if (ptr->mpu_speed <= req->rate)90break;91}92req->rate = highest_rate;9394return 0;95}9697/* Sets basic clocks based on the specified rate */98static int omap2_select_table_rate(struct clk_hw *hw, unsigned long rate,99unsigned long parent_rate)100{101u32 cur_rate, done_rate, bypass = 0;102const struct prcm_config *prcm;103unsigned long found_speed = 0;104unsigned long flags;105106for (prcm = rate_table; prcm->mpu_speed; prcm++) {107if (!(prcm->flags & cpu_mask))108continue;109110if (prcm->xtal_speed != sys_ck_rate)111continue;112113if (prcm->mpu_speed <= rate) {114found_speed = prcm->mpu_speed;115break;116}117}118119if (!found_speed) {120printk(KERN_INFO "Could not set MPU rate to %luMHz\n",121rate / 1000000);122return -EINVAL;123}124125curr_prcm_set = prcm;126cur_rate = omap2xxx_clk_get_core_rate();127128if (prcm->dpll_speed == cur_rate / 2) {129omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL, 1);130} else if (prcm->dpll_speed == cur_rate * 2) {131omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);132} else if (prcm->dpll_speed != cur_rate) {133local_irq_save(flags);134135if (prcm->dpll_speed == prcm->xtal_speed)136bypass = 1;137138if ((prcm->cm_clksel2_pll & OMAP24XX_CORE_CLK_SRC_MASK) ==139CORE_CLK_SRC_DPLL_X2)140done_rate = CORE_CLK_SRC_DPLL_X2;141else142done_rate = CORE_CLK_SRC_DPLL;143144omap2xxx_cm_set_mod_dividers(prcm->cm_clksel_mpu,145prcm->cm_clksel_dsp,146prcm->cm_clksel_gfx,147prcm->cm_clksel1_core,148prcm->cm_clksel_mdm);149150/* x2 to enter omap2xxx_sdrc_init_params() */151omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);152153omap2_set_prcm(prcm->cm_clksel1_pll, prcm->base_sdrc_rfr,154bypass);155156omap2xxx_sdrc_init_params(omap2xxx_sdrc_dll_is_unlocked());157omap2xxx_sdrc_reprogram(done_rate, 0);158159local_irq_restore(flags);160}161162return 0;163}164165/**166* omap2xxx_clkt_vps_check_bootloader_rates - determine which of the rate167* table sets matches the current CORE DPLL hardware rate168*169* Check the MPU rate set by bootloader. Sets the 'curr_prcm_set'170* global to point to the active rate set when found; otherwise, sets171* it to NULL. No return value;172*/173static void omap2xxx_clkt_vps_check_bootloader_rates(void)174{175const struct prcm_config *prcm = NULL;176unsigned long rate;177178rate = omap2xxx_clk_get_core_rate();179for (prcm = rate_table; prcm->mpu_speed; prcm++) {180if (!(prcm->flags & cpu_mask))181continue;182if (prcm->xtal_speed != sys_ck_rate)183continue;184if (prcm->dpll_speed <= rate)185break;186}187curr_prcm_set = prcm;188}189190/**191* omap2xxx_clkt_vps_late_init - store a copy of the sys_ck rate192*193* Store a copy of the sys_ck rate for later use by the OMAP2xxx DVFS194* code. (The sys_ck rate does not -- or rather, must not -- change195* during kernel runtime.) Must be called after we have a valid196* sys_ck rate, but before the virt_prcm_set clock rate is197* recalculated. No return value.198*/199static void omap2xxx_clkt_vps_late_init(void)200{201struct clk *c;202203c = clk_get(NULL, "sys_ck");204if (IS_ERR(c)) {205WARN(1, "could not locate sys_ck\n");206} else {207sys_ck_rate = clk_get_rate(c);208clk_put(c);209}210}211212#ifdef CONFIG_OF213#include <linux/clk-provider.h>214#include <linux/clkdev.h>215216static const struct clk_ops virt_prcm_set_ops = {217.recalc_rate = &omap2_table_mpu_recalc,218.set_rate = &omap2_select_table_rate,219.determine_rate = &omap2_determine_rate_to_table,220};221222/**223* omap2xxx_clkt_vps_init - initialize virt_prcm_set clock224*225* Does a manual init for the virtual prcm DVFS clock for OMAP2. This226* function is called only from omap2 DT clock init, as the virtual227* node is not modelled in the DT clock data.228*/229void omap2xxx_clkt_vps_init(void)230{231struct clk_init_data init = { NULL };232struct clk_hw_omap *hw = NULL;233struct clk *clk;234const char *parent_name = "mpu_ck";235236omap2xxx_clkt_vps_late_init();237omap2xxx_clkt_vps_check_bootloader_rates();238239hw = kzalloc(sizeof(*hw), GFP_KERNEL);240if (!hw)241return;242init.name = "virt_prcm_set";243init.ops = &virt_prcm_set_ops;244init.parent_names = &parent_name;245init.num_parents = 1;246247hw->hw.init = &init;248249clk = clk_register(NULL, &hw->hw);250if (IS_ERR(clk)) {251printk(KERN_ERR "Failed to register clock\n");252kfree(hw);253return;254}255256clkdev_create(clk, "cpufreq_ck", NULL);257}258#endif259260261