Skip to content

Commit 02ae2bc

Browse files
wensmripard
authored andcommitted
clk: sunxi-ng: Add clk notifier to gate then ungate PLL clocks
In common PLL designs, changes to the dividers take effect almost immediately, while changes to the multipliers (implemented as dividers in the feedback loop) take a few cycles to work into the feedback loop for the PLL to stablize. Sometimes when the PLL clock rate is changed, the decrease in the divider is too much for the decrease in the multiplier to catch up. The PLL clock rate will spike, and in some cases, might lock up completely. This is especially the case if the divider changed is the pre-divider, which affects the reference frequency. This patch introduces a clk notifier callback that will gate and then ungate a clk after a rate change, effectively resetting it, so it continues to work, despite any possible lockups. Care must be taken to reparent any consumers to other temporary clocks during the rate change, and that this notifier callback must be the first to be registered. This is intended to fix occasional lockups with cpufreq on newer Allwinner SoCs, such as the A33 and the H3. Previously it was thought that reparenting the cpu clock away from the PLL while it stabilized was enough, as this worked quite well on the A31. On the A33, hangs have been observed after cpufreq was recently introduced. With the H3, a more thorough test [1] showed that reparenting alone isn't enough. The system still locks up unless the dividers are limited to 1. A hunch was if the PLL was stuck in some unknown state, perhaps gating then ungating it would bring it back to normal. Tests done by Icenowy Zheng using Ondrej's test firmware shows this to be a valid solution. [1] http://www.spinics.net/lists/arm-kernel/msg552501.html Reported-by: Ondrej Jirman <[email protected]> Signed-off-by: Chen-Yu Tsai <[email protected]> Tested-by: Icenowy Zheng <[email protected]> Tested-by: Quentin Schulz <[email protected]> Signed-off-by: Maxime Ripard <[email protected]>
1 parent e87741a commit 02ae2bc

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

drivers/clk/sunxi-ng/ccu_common.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
* GNU General Public License for more details.
1515
*/
1616

17+
#include <linux/clk.h>
1718
#include <linux/clk-provider.h>
1819
#include <linux/iopoll.h>
1920
#include <linux/slab.h>
2021

2122
#include "ccu_common.h"
23+
#include "ccu_gate.h"
2224
#include "ccu_reset.h"
2325

2426
static DEFINE_SPINLOCK(ccu_lock);
@@ -39,6 +41,53 @@ void ccu_helper_wait_for_lock(struct ccu_common *common, u32 lock)
3941
WARN_ON(readl_relaxed_poll_timeout(addr, reg, reg & lock, 100, 70000));
4042
}
4143

44+
/*
45+
* This clock notifier is called when the frequency of a PLL clock is
46+
* changed. In common PLL designs, changes to the dividers take effect
47+
* almost immediately, while changes to the multipliers (implemented
48+
* as dividers in the feedback loop) take a few cycles to work into
49+
* the feedback loop for the PLL to stablize.
50+
*
51+
* Sometimes when the PLL clock rate is changed, the decrease in the
52+
* divider is too much for the decrease in the multiplier to catch up.
53+
* The PLL clock rate will spike, and in some cases, might lock up
54+
* completely.
55+
*
56+
* This notifier callback will gate and then ungate the clock,
57+
* effectively resetting it, so it proceeds to work. Care must be
58+
* taken to reparent consumers to other temporary clocks during the
59+
* rate change, and that this notifier callback must be the first
60+
* to be registered.
61+
*/
62+
static int ccu_pll_notifier_cb(struct notifier_block *nb,
63+
unsigned long event, void *data)
64+
{
65+
struct ccu_pll_nb *pll = to_ccu_pll_nb(nb);
66+
int ret = 0;
67+
68+
if (event != POST_RATE_CHANGE)
69+
goto out;
70+
71+
ccu_gate_helper_disable(pll->common, pll->enable);
72+
73+
ret = ccu_gate_helper_enable(pll->common, pll->enable);
74+
if (ret)
75+
goto out;
76+
77+
ccu_helper_wait_for_lock(pll->common, pll->lock);
78+
79+
out:
80+
return notifier_from_errno(ret);
81+
}
82+
83+
int ccu_pll_notifier_register(struct ccu_pll_nb *pll_nb)
84+
{
85+
pll_nb->clk_nb.notifier_call = ccu_pll_notifier_cb;
86+
87+
return clk_notifier_register(pll_nb->common->hw.clk,
88+
&pll_nb->clk_nb);
89+
}
90+
4291
int sunxi_ccu_probe(struct device_node *node, void __iomem *reg,
4392
const struct sunxi_ccu_desc *desc)
4493
{

drivers/clk/sunxi-ng/ccu_common.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ struct sunxi_ccu_desc {
8383

8484
void ccu_helper_wait_for_lock(struct ccu_common *common, u32 lock);
8585

86+
struct ccu_pll_nb {
87+
struct notifier_block clk_nb;
88+
struct ccu_common *common;
89+
90+
u32 enable;
91+
u32 lock;
92+
};
93+
94+
#define to_ccu_pll_nb(_nb) container_of(_nb, struct ccu_pll_nb, clk_nb)
95+
96+
int ccu_pll_notifier_register(struct ccu_pll_nb *pll_nb);
97+
8698
int sunxi_ccu_probe(struct device_node *node, void __iomem *reg,
8799
const struct sunxi_ccu_desc *desc);
88100

0 commit comments

Comments
 (0)