// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (c) 2014 Samsung Electronics Co., Ltd. * Author: Thomas Abraham <thomas.ab@samsung.com> * * Copyright (c) 2015 Samsung Electronics Co., Ltd. * Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com> * * This file contains the utility function to register CPU clock for Samsung * Exynos platforms. A CPU clock is defined as a clock supplied to a CPU or a * group of CPUs. The CPU clock is typically derived from a hierarchy of clock * blocks which includes mux and divider blocks. There are a number of other * auxiliary clocks supplied to the CPU domain such as the debug blocks and AXI * clock for CPU domain. The rates of these auxiliary clocks are related to the * CPU clock rate and this relation is usually specified in the hardware manual * of the SoC or supplied after the SoC characterization. * * The below implementation of the CPU clock allows the rate changes of the CPU * clock and the corresponding rate changes of the auxiliary clocks of the CPU * domain. The platform clock driver provides a clock register configuration * for each configurable rate which is then used to program the clock hardware * registers to achieve a fast coordinated rate change for all the CPU domain * clocks. * * On a rate change request for the CPU clock, the rate change is propagated * up to the PLL supplying the clock to the CPU domain clock blocks. While the * CPU domain PLL is reconfigured, the CPU domain clocks are driven using an * alternate clock source. If required, the alternate clock source is divided * down in order to keep the output clock rate within the previous OPP limits.
*/
/** * struct exynos_cpuclk_regs - Register offsets for CPU related clocks * @mux_sel: offset of CPU MUX_SEL register (for selecting MUX clock parent) * @mux_stat: offset of CPU MUX_STAT register (for checking MUX clock status) * @div_cpu0: offset of CPU DIV0 register (for modifying divider values) * @div_cpu1: offset of CPU DIV1 register (for modifying divider values) * @div_stat_cpu0: offset of CPU DIV0_STAT register (for checking DIV status) * @div_stat_cpu1: offset of CPU DIV1_STAT register (for checking DIV status) * @mux: offset of MUX register for choosing CPU clock source * @divs: offsets of DIV registers (ACLK, ATCLK, PCLKDBG and PERIPHCLK)
*/ struct exynos_cpuclk_regs {
u32 mux_sel;
u32 mux_stat;
u32 div_cpu0;
u32 div_cpu1;
u32 div_stat_cpu0;
u32 div_stat_cpu1;
u32 mux;
u32 divs[4];
};
/** * struct exynos_cpuclk_chip - Chip specific data for CPU clock * @regs: register offsets for CPU related clocks * @pre_rate_cb: callback to run before CPU clock rate change * @post_rate_cb: callback to run after CPU clock rate change
*/ struct exynos_cpuclk_chip { conststruct exynos_cpuclk_regs *regs;
exynos_rate_change_fn_t pre_rate_cb;
exynos_rate_change_fn_t post_rate_cb;
};
/** * struct exynos_cpuclk - information about clock supplied to a CPU core * @hw: handle between CCF and CPU clock * @alt_parent: alternate parent clock to use when switching the speed * of the primary parent clock * @base: start address of the CPU clock registers block * @lock: cpu clock domain register access lock * @cfg: cpu clock rate configuration data * @num_cfgs: number of array elements in @cfg array * @clk_nb: clock notifier registered for changes in clock speed of the * primary parent clock * @flags: configuration flags for the CPU clock * @chip: chip-specific data for the CPU clock * * This structure holds information required for programming the CPU clock for * various clock speeds.
*/ struct exynos_cpuclk { struct clk_hw hw; conststruct clk_hw *alt_parent; void __iomem *base;
spinlock_t *lock; conststruct exynos_cpuclk_cfg_data *cfg; constunsignedlong num_cfgs; struct notifier_block clk_nb; unsignedlong flags; conststruct exynos_cpuclk_chip *chip;
};
/* ---- Common code --------------------------------------------------------- */
/* * Helper function to wait until divider(s) have stabilized after the divider * value has changed.
*/ staticvoid wait_until_divider_stable(void __iomem *div_reg, unsignedlong mask)
{ unsignedlong timeout = jiffies + msecs_to_jiffies(MAX_STAB_TIME);
do { if (!(readl(div_reg) & mask)) return;
} while (time_before(jiffies, timeout));
if (!(readl(div_reg) & mask)) return;
pr_err("%s: timeout in divider stabilization\n", __func__);
}
/* * Helper function to wait until mux has stabilized after the mux selection * value was changed.
*/ staticvoid wait_until_mux_stable(void __iomem *mux_reg, u32 mux_pos, unsignedlong mask, unsignedlong mux_value)
{ unsignedlong timeout = jiffies + msecs_to_jiffies(MAX_STAB_TIME);
do { if (((readl(mux_reg) >> mux_pos) & mask) == mux_value) return;
} while (time_before(jiffies, timeout));
if (((readl(mux_reg) >> mux_pos) & mask) == mux_value) return;
/* * Helper function to set the 'safe' dividers for the CPU clock. The parameters * div and mask contain the divider value and the register bit mask of the * dividers to be programmed.
*/ staticvoid exynos_set_safe_div(struct exynos_cpuclk *cpuclk, unsignedlong div, unsignedlong mask)
{ conststruct exynos_cpuclk_regs * const regs = cpuclk->chip->regs; void __iomem *base = cpuclk->base; unsignedlong div0;
/* find out the divider values to use for clock data */ while ((cfg_data->prate * 1000) != ndata->new_rate) { if (cfg_data->prate == 0) return -EINVAL;
cfg_data++;
}
spin_lock_irqsave(cpuclk->lock, flags);
/* * For the selected PLL clock frequency, get the pre-defined divider * values. If the clock for sclk_hpm is not sourced from apll, then * the values for DIV_COPY and DIV_HPM dividers need not be set.
*/
div0 = cfg_data->div0; if (cpuclk->flags & CLK_CPU_HAS_DIV1) {
div1 = cfg_data->div1; if (readl(base + regs->mux_sel) & E4210_MUX_HPM_MASK)
div1 = readl(base + regs->div_cpu1) &
(E4210_DIV1_HPM_MASK | E4210_DIV1_COPY_MASK);
}
/* * If the old parent clock speed is less than the clock speed of * the alternate parent, then it should be ensured that at no point * the armclk speed is more than the old_prate until the dividers are * set. Also workaround the issue of the dividers being set to lower * values before the parent clock speed is set to new lower speed * (this can result in too high speed of armclk output clocks).
*/ if (alt_prate > ndata->old_rate || ndata->old_rate > ndata->new_rate) { unsignedlong tmp_rate = min(ndata->old_rate, ndata->new_rate); unsignedlong alt_div, alt_div_mask = DIV_MASK;
if (cpuclk->flags & CLK_CPU_NEEDS_DEBUG_ALT_DIV) { /* * In Exynos4210, ATB clock parent is also mout_core. So * ATB clock also needs to be maintained at safe speed.
*/
alt_div |= E4210_DIV0_ATB_MASK;
alt_div_mask |= E4210_DIV0_ATB_MASK;
}
exynos_set_safe_div(cpuclk, alt_div, alt_div_mask);
div0 |= alt_div;
}
/* select sclk_mpll as the alternate parent */
mux_reg = readl(base + regs->mux_sel);
writel(mux_reg | (1 << 16), base + regs->mux_sel);
wait_until_mux_stable(base + regs->mux_stat, 16, MUX_MASK, 2);
/* alternate parent is active now. set the dividers */
writel(div0, base + regs->div_cpu0);
wait_until_divider_stable(base + regs->div_stat_cpu0, DIV_MASK_ALL);
if (cpuclk->flags & CLK_CPU_HAS_DIV1) {
writel(div1, base + regs->div_cpu1);
wait_until_divider_stable(base + regs->div_stat_cpu1,
DIV_MASK_ALL);
}
/* find out the divider values to use for clock data */ if (cpuclk->flags & CLK_CPU_NEEDS_DEBUG_ALT_DIV) { while ((cfg_data->prate * 1000) != ndata->new_rate) { if (cfg_data->prate == 0) return -EINVAL;
cfg_data++;
}
}
spin_lock_irqsave(cpuclk->lock, flags);
/* select mout_apll as the alternate parent */
mux_reg = readl(base + regs->mux_sel);
writel(mux_reg & ~(1 << 16), base + regs->mux_sel);
wait_until_mux_stable(base + regs->mux_stat, 16, MUX_MASK, 1);
if (cpuclk->flags & CLK_CPU_NEEDS_DEBUG_ALT_DIV) {
div |= (cfg_data->div0 & E4210_DIV0_ATB_MASK);
div_mask |= E4210_DIV0_ATB_MASK;
}
/* find out the divider values to use for clock data */ while ((cfg_data->prate * 1000) != ndata->new_rate) { if (cfg_data->prate == 0) return -EINVAL;
cfg_data++;
}
spin_lock_irqsave(cpuclk->lock, flags);
/* * For the selected PLL clock frequency, get the pre-defined divider * values.
*/
div0 = cfg_data->div0;
div1 = cfg_data->div1;
/* * If the old parent clock speed is less than the clock speed of * the alternate parent, then it should be ensured that at no point * the armclk speed is more than the old_prate until the dividers are * set. Also workaround the issue of the dividers being set to lower * values before the parent clock speed is set to new lower speed * (this can result in too high speed of armclk output clocks).
*/ if (alt_prate > ndata->old_rate || ndata->old_rate > ndata->new_rate) { unsignedlong tmp_rate = min(ndata->old_rate, ndata->new_rate); unsignedlong alt_div, alt_div_mask = DIV_MASK;
/* alternate parent is active now. set the dividers */
writel(div0, base + regs->div_cpu0);
wait_until_divider_stable(base + regs->div_stat_cpu0, DIV_MASK_ALL);
writel(div1, base + regs->div_cpu1);
wait_until_divider_stable(base + regs->div_stat_cpu1, DIV_MASK_ALL);
/* * Set alternate parent rate to "rate" value or less. * * rate: Desired alt_parent rate, or 0 for max alt_parent rate * * Exynos850 doesn't have CPU clock divider in CMU_CPUCLx block (CMUREF divider * doesn't affect CPU speed). So CPUCLx_SWITCH divider from CMU_TOP is used * instead to adjust alternate parent speed. * * It's possible to use clk_set_max_rate() instead of this function, but it * would set overly pessimistic rate values to alternate parent.
*/ staticint exynos850_alt_parent_set_max_rate(conststruct clk_hw *alt_parent, unsignedlong rate)
{ struct clk_hw *clk_div, *clk_divp; unsignedlong divp_rate, div_rate, div; int ret;
/* Divider from CMU_TOP */
clk_div = clk_hw_get_parent(alt_parent); if (!clk_div) return -ENOENT; /* Divider's parent from CMU_TOP */
clk_divp = clk_hw_get_parent(clk_div); if (!clk_divp) return -ENOENT; /* Divider input rate */
divp_rate = clk_hw_get_rate(clk_divp); if (!divp_rate) return -EINVAL;
/* Calculate new alt_parent rate for integer divider value */ if (rate == 0)
div = 1; else
div = DIV_ROUND_UP(divp_rate, rate);
div_rate = DIV_ROUND_UP(divp_rate, div);
WARN_ON(div >= MAX_DIV);
/* alt_parent will propagate this change up to the divider */
ret = clk_set_rate(alt_parent->clk, div_rate); if (ret) return ret;
udelay(E850_DIV_MUX_STAB_TIME);
/* No actions are needed when switching to or from OSCCLK parent */ if (ndata->new_rate == E850_OSCCLK || ndata->old_rate == E850_OSCCLK) return 0;
/* Find out the divider values to use for clock data */ while ((cfg_data->prate * 1000) != ndata->new_rate) { if (cfg_data->prate == 0) return -EINVAL;
cfg_data++;
}
/* * If the old parent clock speed is less than the clock speed of * the alternate parent, then it should be ensured that at no point * the armclk speed is more than the old_prate until the dividers are * set. Also workaround the issue of the dividers being set to lower * values before the parent clock speed is set to new lower speed * (this can result in too high speed of armclk output clocks).
*/ if (alt_prate > ndata->old_rate || ndata->old_rate > ndata->new_rate) { unsignedlong tmp_rate = min(ndata->old_rate, ndata->new_rate);
ret = exynos850_alt_parent_set_max_rate(alt_parent, tmp_rate); if (ret) return ret;
}
/* Alternate parent is active now. Set the dividers */ for (i = 0; i < ARRAY_SIZE(shifts); ++i) { unsignedlong div = (cfg_data->div0 >> shifts[i]) & 0xf;
u32 val;
val = readl(base + regs->divs[i]);
val = (val & ~E850_DIV_RATIO_MASK) | div;
writel(val, base + regs->divs[i]);
wait_until_divider_stable(base + regs->divs[i], E850_BUSY_MASK);
}
/* Common round rate callback usable for all types of CPU clocks */ staticlong exynos_cpuclk_round_rate(struct clk_hw *hw, unsignedlong drate, unsignedlong *prate)
{ struct clk_hw *parent = clk_hw_get_parent(hw);
*prate = clk_hw_round_rate(parent, drate); return *prate;
}
/* Common recalc rate callback usable for all types of CPU clocks */ staticunsignedlong exynos_cpuclk_recalc_rate(struct clk_hw *hw, unsignedlong parent_rate)
{ /* * The CPU clock output (armclk) rate is the same as its parent * rate. Although there exist certain dividers inside the CPU * clock block that could be used to divide the parent clock, * the driver does not make use of them currently, except during * frequency transitions.
*/ return parent_rate;
}
/* * This notifier function is called for the pre-rate and post-rate change * notifications of the parent clock of cpuclk.
*/ staticint exynos_cpuclk_notifier_cb(struct notifier_block *nb, unsignedlong event, void *data)
{ struct clk_notifier_data *ndata = data; struct exynos_cpuclk *cpuclk; int err = 0;
ret = clk_notifier_register(parent->clk, &cpuclk->clk_nb); if (ret) {
pr_err("%s: failed to register clock notifier for %s\n",
__func__, clk_data->name); goto free_cpuclk;
}
/* Find count of configuration rates in cfg */ for (num_cfgs = 0; clk_data->cfg[num_cfgs].prate != 0; )
num_cfgs++;
cpuclk->cfg = kmemdup_array(clk_data->cfg, num_cfgs, sizeof(*cpuclk->cfg),
GFP_KERNEL); if (!cpuclk->cfg) {
ret = -ENOMEM; goto unregister_clk_nb;
}
ret = clk_hw_register(NULL, &cpuclk->hw); if (ret) {
pr_err("%s: could not register cpuclk %s\n", __func__,
clk_data->name); goto free_cpuclk_data;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.