Path: blob/master/arch/arm/mach-omap2/clkt2xxx_virt_prcm_set.c
26292 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 long omap2_round_to_table_rate(struct clk_hw *hw, unsigned long rate,73unsigned long *parent_rate)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 <= rate)90break;91}92return highest_rate;93}9495/* Sets basic clocks based on the specified rate */96static int omap2_select_table_rate(struct clk_hw *hw, unsigned long rate,97unsigned long parent_rate)98{99u32 cur_rate, done_rate, bypass = 0;100const struct prcm_config *prcm;101unsigned long found_speed = 0;102unsigned long flags;103104for (prcm = rate_table; prcm->mpu_speed; prcm++) {105if (!(prcm->flags & cpu_mask))106continue;107108if (prcm->xtal_speed != sys_ck_rate)109continue;110111if (prcm->mpu_speed <= rate) {112found_speed = prcm->mpu_speed;113break;114}115}116117if (!found_speed) {118printk(KERN_INFO "Could not set MPU rate to %luMHz\n",119rate / 1000000);120return -EINVAL;121}122123curr_prcm_set = prcm;124cur_rate = omap2xxx_clk_get_core_rate();125126if (prcm->dpll_speed == cur_rate / 2) {127omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL, 1);128} else if (prcm->dpll_speed == cur_rate * 2) {129omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);130} else if (prcm->dpll_speed != cur_rate) {131local_irq_save(flags);132133if (prcm->dpll_speed == prcm->xtal_speed)134bypass = 1;135136if ((prcm->cm_clksel2_pll & OMAP24XX_CORE_CLK_SRC_MASK) ==137CORE_CLK_SRC_DPLL_X2)138done_rate = CORE_CLK_SRC_DPLL_X2;139else140done_rate = CORE_CLK_SRC_DPLL;141142omap2xxx_cm_set_mod_dividers(prcm->cm_clksel_mpu,143prcm->cm_clksel_dsp,144prcm->cm_clksel_gfx,145prcm->cm_clksel1_core,146prcm->cm_clksel_mdm);147148/* x2 to enter omap2xxx_sdrc_init_params() */149omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);150151omap2_set_prcm(prcm->cm_clksel1_pll, prcm->base_sdrc_rfr,152bypass);153154omap2xxx_sdrc_init_params(omap2xxx_sdrc_dll_is_unlocked());155omap2xxx_sdrc_reprogram(done_rate, 0);156157local_irq_restore(flags);158}159160return 0;161}162163/**164* omap2xxx_clkt_vps_check_bootloader_rates - determine which of the rate165* table sets matches the current CORE DPLL hardware rate166*167* Check the MPU rate set by bootloader. Sets the 'curr_prcm_set'168* global to point to the active rate set when found; otherwise, sets169* it to NULL. No return value;170*/171static void omap2xxx_clkt_vps_check_bootloader_rates(void)172{173const struct prcm_config *prcm = NULL;174unsigned long rate;175176rate = omap2xxx_clk_get_core_rate();177for (prcm = rate_table; prcm->mpu_speed; prcm++) {178if (!(prcm->flags & cpu_mask))179continue;180if (prcm->xtal_speed != sys_ck_rate)181continue;182if (prcm->dpll_speed <= rate)183break;184}185curr_prcm_set = prcm;186}187188/**189* omap2xxx_clkt_vps_late_init - store a copy of the sys_ck rate190*191* Store a copy of the sys_ck rate for later use by the OMAP2xxx DVFS192* code. (The sys_ck rate does not -- or rather, must not -- change193* during kernel runtime.) Must be called after we have a valid194* sys_ck rate, but before the virt_prcm_set clock rate is195* recalculated. No return value.196*/197static void omap2xxx_clkt_vps_late_init(void)198{199struct clk *c;200201c = clk_get(NULL, "sys_ck");202if (IS_ERR(c)) {203WARN(1, "could not locate sys_ck\n");204} else {205sys_ck_rate = clk_get_rate(c);206clk_put(c);207}208}209210#ifdef CONFIG_OF211#include <linux/clk-provider.h>212#include <linux/clkdev.h>213214static const struct clk_ops virt_prcm_set_ops = {215.recalc_rate = &omap2_table_mpu_recalc,216.set_rate = &omap2_select_table_rate,217.round_rate = &omap2_round_to_table_rate,218};219220/**221* omap2xxx_clkt_vps_init - initialize virt_prcm_set clock222*223* Does a manual init for the virtual prcm DVFS clock for OMAP2. This224* function is called only from omap2 DT clock init, as the virtual225* node is not modelled in the DT clock data.226*/227void omap2xxx_clkt_vps_init(void)228{229struct clk_init_data init = { NULL };230struct clk_hw_omap *hw = NULL;231struct clk *clk;232const char *parent_name = "mpu_ck";233234omap2xxx_clkt_vps_late_init();235omap2xxx_clkt_vps_check_bootloader_rates();236237hw = kzalloc(sizeof(*hw), GFP_KERNEL);238if (!hw)239return;240init.name = "virt_prcm_set";241init.ops = &virt_prcm_set_ops;242init.parent_names = &parent_name;243init.num_parents = 1;244245hw->hw.init = &init;246247clk = clk_register(NULL, &hw->hw);248if (IS_ERR(clk)) {249printk(KERN_ERR "Failed to register clock\n");250kfree(hw);251return;252}253254clkdev_create(clk, "cpufreq_ck", NULL);255}256#endif257258259