2019-06-04 15:11:33 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2011-04-19 13:33:45 +07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
|
|
|
|
* Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
|
|
|
|
*
|
|
|
|
* Gated clock implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk-provider.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/string.h>
|
2013-03-25 19:20:35 +07:00
|
|
|
#include "clk.h"
|
2011-04-19 13:33:45 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DOC: basic gatable clock which can gate and ungate it's ouput
|
|
|
|
*
|
|
|
|
* Traits of this clock:
|
|
|
|
* prepare - clk_(un)prepare only ensures parent is (un)prepared
|
|
|
|
* enable - clk_enable and clk_disable are functional & control gating
|
|
|
|
* rate - inherits rate from parent. No clk_set_rate support
|
|
|
|
* parent - fixed parent. No clk_set_parent support
|
|
|
|
*/
|
|
|
|
|
2014-04-18 14:55:16 +07:00
|
|
|
struct clk_gate2 {
|
|
|
|
struct clk_hw hw;
|
|
|
|
void __iomem *reg;
|
|
|
|
u8 bit_idx;
|
2016-03-10 09:16:47 +07:00
|
|
|
u8 cgr_val;
|
2014-04-18 14:55:16 +07:00
|
|
|
u8 flags;
|
|
|
|
spinlock_t *lock;
|
2014-04-19 09:58:22 +07:00
|
|
|
unsigned int *share_count;
|
2014-04-18 14:55:16 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
#define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw)
|
2011-04-19 13:33:45 +07:00
|
|
|
|
|
|
|
static int clk_gate2_enable(struct clk_hw *hw)
|
|
|
|
{
|
2014-04-18 14:55:16 +07:00
|
|
|
struct clk_gate2 *gate = to_clk_gate2(hw);
|
2011-04-19 13:33:45 +07:00
|
|
|
u32 reg;
|
|
|
|
unsigned long flags = 0;
|
|
|
|
|
2014-04-18 15:07:44 +07:00
|
|
|
spin_lock_irqsave(gate->lock, flags);
|
2011-04-19 13:33:45 +07:00
|
|
|
|
2014-04-19 09:58:22 +07:00
|
|
|
if (gate->share_count && (*gate->share_count)++ > 0)
|
|
|
|
goto out;
|
|
|
|
|
2011-04-19 13:33:45 +07:00
|
|
|
reg = readl(gate->reg);
|
2016-03-10 09:16:47 +07:00
|
|
|
reg &= ~(3 << gate->bit_idx);
|
|
|
|
reg |= gate->cgr_val << gate->bit_idx;
|
2011-04-19 13:33:45 +07:00
|
|
|
writel(reg, gate->reg);
|
|
|
|
|
2014-04-19 09:58:22 +07:00
|
|
|
out:
|
2014-04-18 15:07:44 +07:00
|
|
|
spin_unlock_irqrestore(gate->lock, flags);
|
2011-04-19 13:33:45 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clk_gate2_disable(struct clk_hw *hw)
|
|
|
|
{
|
2014-04-18 14:55:16 +07:00
|
|
|
struct clk_gate2 *gate = to_clk_gate2(hw);
|
2011-04-19 13:33:45 +07:00
|
|
|
u32 reg;
|
|
|
|
unsigned long flags = 0;
|
|
|
|
|
2014-04-18 15:07:44 +07:00
|
|
|
spin_lock_irqsave(gate->lock, flags);
|
2011-04-19 13:33:45 +07:00
|
|
|
|
2014-07-07 09:53:51 +07:00
|
|
|
if (gate->share_count) {
|
|
|
|
if (WARN_ON(*gate->share_count == 0))
|
|
|
|
goto out;
|
|
|
|
else if (--(*gate->share_count) > 0)
|
|
|
|
goto out;
|
|
|
|
}
|
2014-04-19 09:58:22 +07:00
|
|
|
|
2011-04-19 13:33:45 +07:00
|
|
|
reg = readl(gate->reg);
|
|
|
|
reg &= ~(3 << gate->bit_idx);
|
|
|
|
writel(reg, gate->reg);
|
|
|
|
|
2014-04-19 09:58:22 +07:00
|
|
|
out:
|
2014-04-18 15:07:44 +07:00
|
|
|
spin_unlock_irqrestore(gate->lock, flags);
|
2011-04-19 13:33:45 +07:00
|
|
|
}
|
|
|
|
|
2014-07-07 09:53:51 +07:00
|
|
|
static int clk_gate2_reg_is_enabled(void __iomem *reg, u8 bit_idx)
|
2011-04-19 13:33:45 +07:00
|
|
|
{
|
2014-07-07 09:53:51 +07:00
|
|
|
u32 val = readl(reg);
|
2011-04-19 13:33:45 +07:00
|
|
|
|
2014-07-07 09:53:51 +07:00
|
|
|
if (((val >> bit_idx) & 1) == 1)
|
2011-04-19 13:33:45 +07:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-07 09:53:51 +07:00
|
|
|
static int clk_gate2_is_enabled(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_gate2 *gate = to_clk_gate2(hw);
|
|
|
|
|
ARM: imx: correct the hardware clock gate setting for shared nodes
For those clk gates which hold share count, since its is_enabled
callback is only checking the share count rather than reading
the hardware register setting, in the late phase of kernel bootup,
the clk_disable_unused action will NOT handle the scenario of
share_count is 0 but the hardware setting is enabled, actually,
U-Boot normally enables all clk gates, then those shared clk gates
will be always enabled until they are used by some modules.
So the problem would be: when kernel boot up, the usecount cat
from clk tree is 0, but the clk gates actually is enabled in
hardware register, it will confuse user and bring unnecessary power
consumption.
This patch adds .disable_unused callback and using hardware register
check for .is_enabled callback of shared nodes to handle such scenario
in late phase of kernel boot up, then the hardware status will match the
clk tree info.
Signed-off-by: Anson Huang <b20788@freescale.com>
Signed-off-by: Shawn Guo <shawn.guo@linaro.org>
2014-12-10 16:51:42 +07:00
|
|
|
return clk_gate2_reg_is_enabled(gate->reg, gate->bit_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clk_gate2_disable_unused(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_gate2 *gate = to_clk_gate2(hw);
|
|
|
|
unsigned long flags = 0;
|
|
|
|
u32 reg;
|
|
|
|
|
|
|
|
spin_lock_irqsave(gate->lock, flags);
|
|
|
|
|
|
|
|
if (!gate->share_count || *gate->share_count == 0) {
|
|
|
|
reg = readl(gate->reg);
|
|
|
|
reg &= ~(3 << gate->bit_idx);
|
|
|
|
writel(reg, gate->reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(gate->lock, flags);
|
2014-07-07 09:53:51 +07:00
|
|
|
}
|
|
|
|
|
2017-08-22 20:18:29 +07:00
|
|
|
static const struct clk_ops clk_gate2_ops = {
|
2011-04-19 13:33:45 +07:00
|
|
|
.enable = clk_gate2_enable,
|
|
|
|
.disable = clk_gate2_disable,
|
ARM: imx: correct the hardware clock gate setting for shared nodes
For those clk gates which hold share count, since its is_enabled
callback is only checking the share count rather than reading
the hardware register setting, in the late phase of kernel bootup,
the clk_disable_unused action will NOT handle the scenario of
share_count is 0 but the hardware setting is enabled, actually,
U-Boot normally enables all clk gates, then those shared clk gates
will be always enabled until they are used by some modules.
So the problem would be: when kernel boot up, the usecount cat
from clk tree is 0, but the clk gates actually is enabled in
hardware register, it will confuse user and bring unnecessary power
consumption.
This patch adds .disable_unused callback and using hardware register
check for .is_enabled callback of shared nodes to handle such scenario
in late phase of kernel boot up, then the hardware status will match the
clk tree info.
Signed-off-by: Anson Huang <b20788@freescale.com>
Signed-off-by: Shawn Guo <shawn.guo@linaro.org>
2014-12-10 16:51:42 +07:00
|
|
|
.disable_unused = clk_gate2_disable_unused,
|
2011-04-19 13:33:45 +07:00
|
|
|
.is_enabled = clk_gate2_is_enabled,
|
|
|
|
};
|
|
|
|
|
2019-05-29 19:26:42 +07:00
|
|
|
struct clk_hw *clk_hw_register_gate2(struct device *dev, const char *name,
|
2011-04-19 13:33:45 +07:00
|
|
|
const char *parent_name, unsigned long flags,
|
2016-03-10 09:16:47 +07:00
|
|
|
void __iomem *reg, u8 bit_idx, u8 cgr_val,
|
2014-04-19 09:58:22 +07:00
|
|
|
u8 clk_gate2_flags, spinlock_t *lock,
|
|
|
|
unsigned int *share_count)
|
2011-04-19 13:33:45 +07:00
|
|
|
{
|
2014-04-18 14:55:16 +07:00
|
|
|
struct clk_gate2 *gate;
|
2019-05-29 19:26:42 +07:00
|
|
|
struct clk_hw *hw;
|
2011-04-19 13:33:45 +07:00
|
|
|
struct clk_init_data init;
|
2019-05-29 19:26:42 +07:00
|
|
|
int ret;
|
2011-04-19 13:33:45 +07:00
|
|
|
|
2014-04-18 14:55:16 +07:00
|
|
|
gate = kzalloc(sizeof(struct clk_gate2), GFP_KERNEL);
|
2011-04-19 13:33:45 +07:00
|
|
|
if (!gate)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2014-04-18 14:55:16 +07:00
|
|
|
/* struct clk_gate2 assignments */
|
2011-04-19 13:33:45 +07:00
|
|
|
gate->reg = reg;
|
|
|
|
gate->bit_idx = bit_idx;
|
2016-03-10 09:16:47 +07:00
|
|
|
gate->cgr_val = cgr_val;
|
2011-04-19 13:33:45 +07:00
|
|
|
gate->flags = clk_gate2_flags;
|
|
|
|
gate->lock = lock;
|
2014-04-19 09:58:22 +07:00
|
|
|
gate->share_count = share_count;
|
2011-04-19 13:33:45 +07:00
|
|
|
|
|
|
|
init.name = name;
|
|
|
|
init.ops = &clk_gate2_ops;
|
|
|
|
init.flags = flags;
|
|
|
|
init.parent_names = parent_name ? &parent_name : NULL;
|
|
|
|
init.num_parents = parent_name ? 1 : 0;
|
|
|
|
|
|
|
|
gate->hw.init = &init;
|
2019-05-29 19:26:42 +07:00
|
|
|
hw = &gate->hw;
|
2011-04-19 13:33:45 +07:00
|
|
|
|
2019-05-29 19:26:42 +07:00
|
|
|
ret = clk_hw_register(NULL, hw);
|
|
|
|
if (ret) {
|
2012-10-25 22:02:18 +07:00
|
|
|
kfree(gate);
|
2019-05-29 19:26:42 +07:00
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
2011-04-19 13:33:45 +07:00
|
|
|
|
2019-05-29 19:26:42 +07:00
|
|
|
return hw;
|
2011-04-19 13:33:45 +07:00
|
|
|
}
|