// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (c) 2014 MundoReader S.L. * Author: Heiko Stuebner <heiko@sntech.de> * * based on clk/samsung/clk-cpu.c * Copyright (c) 2014 Samsung Electronics Co., Ltd. * Author: Thomas Abraham <thomas.ab@samsung.com> * * 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 co-oridinated 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 rockchip_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. * @reg_base: base register for cpu-clock values. * @clk_nb: clock notifier registered for changes in clock speed of the * primary parent clock. * @rate_count: number of rates in the rate_table * @rate_table: pll-rates and their associated dividers * @reg_data: cpu-specific register settings * @lock: clock lock
*/ struct rockchip_cpuclk { struct clk_hw hw; struct clk *alt_parent; void __iomem *reg_base; struct notifier_block clk_nb; unsignedint rate_count; struct rockchip_cpuclk_rate_table *rate_table; conststruct rockchip_cpuclk_reg_data *reg_data;
spinlock_t *lock;
};
staticvoid rockchip_cpuclk_set_dividers(struct rockchip_cpuclk *cpuclk, conststruct rockchip_cpuclk_rate_table *rate)
{ int i;
/* alternate parent is active now. set the dividers */ for (i = 0; i < ARRAY_SIZE(rate->divs); i++) { conststruct rockchip_cpuclk_clksel *clksel = &rate->divs[i];
staticvoid rockchip_cpuclk_set_pre_muxs(struct rockchip_cpuclk *cpuclk, conststruct rockchip_cpuclk_rate_table *rate)
{ int i;
/* alternate parent is active now. set the pre_muxs */ for (i = 0; i < ARRAY_SIZE(rate->pre_muxs); i++) { conststruct rockchip_cpuclk_clksel *clksel = &rate->pre_muxs[i];
staticvoid rockchip_cpuclk_set_post_muxs(struct rockchip_cpuclk *cpuclk, conststruct rockchip_cpuclk_rate_table *rate)
{ int i;
/* alternate parent is active now. set the muxs */ for (i = 0; i < ARRAY_SIZE(rate->post_muxs); i++) { conststruct rockchip_cpuclk_clksel *clksel = &rate->post_muxs[i];
/* check validity of the new rate */
rate = rockchip_get_cpuclk_settings(cpuclk, ndata->new_rate); if (!rate) {
pr_err("%s: Invalid rate : %lu for cpuclk\n",
__func__, ndata->new_rate); return -EINVAL;
}
alt_prate = clk_get_rate(cpuclk->alt_parent);
spin_lock_irqsave(cpuclk->lock, flags);
/* * 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_rate until the dividers are * set.
*/ if (alt_prate > ndata->old_rate) { /* calculate dividers */
alt_div = DIV_ROUND_UP(alt_prate, ndata->old_rate) - 1; if (alt_div > reg_data->div_core_mask[0]) {
pr_warn("%s: limiting alt-divider %lu to %d\n",
__func__, alt_div, reg_data->div_core_mask[0]);
alt_div = reg_data->div_core_mask[0];
}
/* * Change parents and add dividers in a single transaction. * * NOTE: we do this in a single transaction so we're never * dividing the primary parent by the extra dividers that were * needed for the alt.
*/
pr_debug("%s: setting div %lu as alt-rate %lu > old-rate %lu\n",
__func__, alt_div, alt_prate, ndata->old_rate);
for (i = 0; i < reg_data->num_cores; i++) {
writel(HIWORD_UPDATE(alt_div, reg_data->div_core_mask[i],
reg_data->div_core_shift[i]),
cpuclk->reg_base + reg_data->core_reg[i]);
}
}
rate = rockchip_get_cpuclk_settings(cpuclk, ndata->new_rate); if (!rate) {
pr_err("%s: Invalid rate : %lu for cpuclk\n",
__func__, ndata->new_rate); return -EINVAL;
}
spin_lock_irqsave(cpuclk->lock, flags);
if (ndata->old_rate < ndata->new_rate)
rockchip_cpuclk_set_dividers(cpuclk, rate);
/* * post-rate change event, re-mux to primary parent and remove dividers. * * NOTE: we do this in a single transaction so we're never dividing the * primary parent by the extra dividers that were needed for the alt.
*/
/* * This clock notifier is called when the frequency of the parent clock * of cpuclk is to be changed. This notifier handles the setting up all * the divider clocks, remux to temporary parent and handling the safe * frequency levels when using temporary parent.
*/ staticint rockchip_cpuclk_notifier_cb(struct notifier_block *nb, unsignedlong event, void *data)
{ struct clk_notifier_data *ndata = data; struct rockchip_cpuclk *cpuclk = to_rockchip_cpuclk_nb(nb); int ret = 0;
pr_debug("%s: event %lu, old_rate %lu, new_rate: %lu\n",
__func__, event, ndata->old_rate, ndata->new_rate); if (event == PRE_RATE_CHANGE)
ret = rockchip_cpuclk_pre_rate_change(cpuclk, ndata); elseif (event == POST_RATE_CHANGE)
ret = rockchip_cpuclk_post_rate_change(cpuclk, ndata);
cpuclk->alt_parent = __clk_lookup(parent_names[reg_data->mux_core_alt]); if (!cpuclk->alt_parent) {
pr_err("%s: could not lookup alternate parent: (%d)\n",
__func__, reg_data->mux_core_alt);
ret = -EINVAL; goto free_cpuclk;
}
ret = clk_prepare_enable(cpuclk->alt_parent); if (ret) {
pr_err("%s: could not enable alternate parent\n",
__func__); goto free_cpuclk;
}
clk = __clk_lookup(parent_names[reg_data->mux_core_main]); if (!clk) {
pr_err("%s: could not lookup parent clock: (%d) %s\n",
__func__, reg_data->mux_core_main,
parent_names[reg_data->mux_core_main]);
ret = -EINVAL; goto free_alt_parent;
}
ret = clk_notifier_register(clk, &cpuclk->clk_nb); if (ret) {
pr_err("%s: failed to register clock notifier for %s\n",
__func__, name); goto free_alt_parent;
}
if (nrates > 0) {
cpuclk->rate_count = nrates;
cpuclk->rate_table = kmemdup_array(rates, nrates, sizeof(*rates),
GFP_KERNEL); if (!cpuclk->rate_table) {
ret = -ENOMEM; goto unregister_notifier;
}
}
cclk = clk_register(NULL, &cpuclk->hw); if (IS_ERR(cclk)) {
pr_err("%s: could not register cpuclk %s\n", __func__, name);
ret = PTR_ERR(cclk); goto free_rate_table;
}
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.