2019-06-04 11:11:33 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2011-04-19 10:33:45 +04: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>
|
2020-07-30 04:22:51 +03:00
|
|
|
#include <linux/export.h>
|
2011-04-19 10:33:45 +04:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/string.h>
|
2013-03-25 16:20:35 +04:00
|
|
|
#include "clk.h"
|
2011-04-19 10:33:45 +04:00
|
|
|
|
|
|
|
/**
|
2020-09-05 18:10:16 +03:00
|
|
|
* DOC: basic gateable clock which can gate and ungate its output
|
2011-04-19 10:33:45 +04:00
|
|
|
*
|
|
|
|
* 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 11:55:16 +04:00
|
|
|
struct clk_gate2 {
|
|
|
|
struct clk_hw hw;
|
|
|
|
void __iomem *reg;
|
|
|
|
u8 bit_idx;
|
2016-03-10 05:16:47 +03:00
|
|
|
u8 cgr_val;
|
2020-10-28 15:59:01 +03:00
|
|
|
u8 cgr_mask;
|
2014-04-18 11:55:16 +04:00
|
|
|
u8 flags;
|
|
|
|
spinlock_t *lock;
|
2014-04-19 06:58:22 +04:00
|
|
|
unsigned int *share_count;
|
2014-04-18 11:55:16 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw)
|
2011-04-19 10:33:45 +04:00
|
|
|
|
2020-10-28 15:58:59 +03:00
|
|
|
static void clk_gate2_do_shared_clks(struct clk_hw *hw, bool enable)
|
2011-04-19 10:33:45 +04:00
|
|
|
{
|
2014-04-18 11:55:16 +04:00
|
|
|
struct clk_gate2 *gate = to_clk_gate2(hw);
|
2011-04-19 10:33:45 +04:00
|
|
|
u32 reg;
|
2020-10-28 15:58:59 +03:00
|
|
|
|
|
|
|
reg = readl(gate->reg);
|
2020-10-28 15:59:01 +03:00
|
|
|
reg &= ~(gate->cgr_mask << gate->bit_idx);
|
2020-10-28 15:58:59 +03:00
|
|
|
if (enable)
|
2020-10-28 15:59:01 +03:00
|
|
|
reg |= (gate->cgr_val & gate->cgr_mask) << gate->bit_idx;
|
2020-10-28 15:58:59 +03:00
|
|
|
writel(reg, gate->reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int clk_gate2_enable(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_gate2 *gate = to_clk_gate2(hw);
|
2020-02-12 12:03:00 +03:00
|
|
|
unsigned long flags;
|
2011-04-19 10:33:45 +04:00
|
|
|
|
2014-04-18 12:07:44 +04:00
|
|
|
spin_lock_irqsave(gate->lock, flags);
|
2011-04-19 10:33:45 +04:00
|
|
|
|
2014-04-19 06:58:22 +04:00
|
|
|
if (gate->share_count && (*gate->share_count)++ > 0)
|
|
|
|
goto out;
|
|
|
|
|
2020-10-28 15:58:59 +03:00
|
|
|
clk_gate2_do_shared_clks(hw, true);
|
2014-04-19 06:58:22 +04:00
|
|
|
out:
|
2014-04-18 12:07:44 +04:00
|
|
|
spin_unlock_irqrestore(gate->lock, flags);
|
2011-04-19 10:33:45 +04:00
|
|
|
|
2020-11-03 14:32:14 +03:00
|
|
|
return 0;
|
2011-04-19 10:33:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void clk_gate2_disable(struct clk_hw *hw)
|
|
|
|
{
|
2014-04-18 11:55:16 +04:00
|
|
|
struct clk_gate2 *gate = to_clk_gate2(hw);
|
2020-02-12 12:03:00 +03:00
|
|
|
unsigned long flags;
|
2011-04-19 10:33:45 +04:00
|
|
|
|
2014-04-18 12:07:44 +04:00
|
|
|
spin_lock_irqsave(gate->lock, flags);
|
2011-04-19 10:33:45 +04:00
|
|
|
|
2014-07-07 06:53:51 +04: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 06:58:22 +04:00
|
|
|
|
2020-10-28 15:58:59 +03:00
|
|
|
clk_gate2_do_shared_clks(hw, false);
|
2014-04-19 06:58:22 +04:00
|
|
|
out:
|
2014-04-18 12:07:44 +04:00
|
|
|
spin_unlock_irqrestore(gate->lock, flags);
|
2011-04-19 10:33:45 +04:00
|
|
|
}
|
|
|
|
|
2020-10-28 15:59:01 +03:00
|
|
|
static int clk_gate2_reg_is_enabled(void __iomem *reg, u8 bit_idx,
|
|
|
|
u8 cgr_val, u8 cgr_mask)
|
2011-04-19 10:33:45 +04:00
|
|
|
{
|
2014-07-07 06:53:51 +04:00
|
|
|
u32 val = readl(reg);
|
2011-04-19 10:33:45 +04:00
|
|
|
|
2020-10-28 15:59:01 +03:00
|
|
|
if (((val >> bit_idx) & cgr_mask) == cgr_val)
|
2011-04-19 10:33:45 +04:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-07 06:53:51 +04:00
|
|
|
static int clk_gate2_is_enabled(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_gate2 *gate = to_clk_gate2(hw);
|
2020-10-28 15:59:02 +03:00
|
|
|
unsigned long flags;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
spin_lock_irqsave(gate->lock, flags);
|
2014-07-07 06:53:51 +04:00
|
|
|
|
2020-10-28 15:59:02 +03:00
|
|
|
ret = clk_gate2_reg_is_enabled(gate->reg, gate->bit_idx,
|
2020-10-28 15:59:01 +03:00
|
|
|
gate->cgr_val, gate->cgr_mask);
|
2020-10-28 15:59:02 +03:00
|
|
|
|
|
|
|
spin_unlock_irqrestore(gate->lock, flags);
|
|
|
|
|
|
|
|
return ret;
|
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 12:51:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void clk_gate2_disable_unused(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_gate2 *gate = to_clk_gate2(hw);
|
2020-02-12 12:03:00 +03:00
|
|
|
unsigned long flags;
|
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 12:51:42 +03:00
|
|
|
|
|
|
|
spin_lock_irqsave(gate->lock, flags);
|
|
|
|
|
2020-10-28 15:58:59 +03:00
|
|
|
if (!gate->share_count || *gate->share_count == 0)
|
|
|
|
clk_gate2_do_shared_clks(hw, false);
|
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 12:51:42 +03:00
|
|
|
|
|
|
|
spin_unlock_irqrestore(gate->lock, flags);
|
2014-07-07 06:53:51 +04:00
|
|
|
}
|
|
|
|
|
2017-08-22 16:18:29 +03:00
|
|
|
static const struct clk_ops clk_gate2_ops = {
|
2011-04-19 10:33:45 +04: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 12:51:42 +03:00
|
|
|
.disable_unused = clk_gate2_disable_unused,
|
2011-04-19 10:33:45 +04:00
|
|
|
.is_enabled = clk_gate2_is_enabled,
|
|
|
|
};
|
|
|
|
|
2019-05-29 15:26:42 +03:00
|
|
|
struct clk_hw *clk_hw_register_gate2(struct device *dev, const char *name,
|
2011-04-19 10:33:45 +04:00
|
|
|
const char *parent_name, unsigned long flags,
|
2020-10-28 15:59:01 +03:00
|
|
|
void __iomem *reg, u8 bit_idx, u8 cgr_val, u8 cgr_mask,
|
2014-04-19 06:58:22 +04:00
|
|
|
u8 clk_gate2_flags, spinlock_t *lock,
|
|
|
|
unsigned int *share_count)
|
2011-04-19 10:33:45 +04:00
|
|
|
{
|
2014-04-18 11:55:16 +04:00
|
|
|
struct clk_gate2 *gate;
|
2019-05-29 15:26:42 +03:00
|
|
|
struct clk_hw *hw;
|
2011-04-19 10:33:45 +04:00
|
|
|
struct clk_init_data init;
|
2019-05-29 15:26:42 +03:00
|
|
|
int ret;
|
2011-04-19 10:33:45 +04:00
|
|
|
|
2014-04-18 11:55:16 +04:00
|
|
|
gate = kzalloc(sizeof(struct clk_gate2), GFP_KERNEL);
|
2011-04-19 10:33:45 +04:00
|
|
|
if (!gate)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2014-04-18 11:55:16 +04:00
|
|
|
/* struct clk_gate2 assignments */
|
2011-04-19 10:33:45 +04:00
|
|
|
gate->reg = reg;
|
|
|
|
gate->bit_idx = bit_idx;
|
2016-03-10 05:16:47 +03:00
|
|
|
gate->cgr_val = cgr_val;
|
2020-10-28 15:59:01 +03:00
|
|
|
gate->cgr_mask = cgr_mask;
|
2011-04-19 10:33:45 +04:00
|
|
|
gate->flags = clk_gate2_flags;
|
|
|
|
gate->lock = lock;
|
2014-04-19 06:58:22 +04:00
|
|
|
gate->share_count = share_count;
|
2011-04-19 10:33:45 +04: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 15:26:42 +03:00
|
|
|
hw = &gate->hw;
|
2011-04-19 10:33:45 +04:00
|
|
|
|
2020-03-13 19:10:19 +03:00
|
|
|
ret = clk_hw_register(dev, hw);
|
2019-05-29 15:26:42 +03:00
|
|
|
if (ret) {
|
2012-10-25 19:02:18 +04:00
|
|
|
kfree(gate);
|
2019-05-29 15:26:42 +03:00
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
2011-04-19 10:33:45 +04:00
|
|
|
|
2019-05-29 15:26:42 +03:00
|
|
|
return hw;
|
2011-04-19 10:33:45 +04:00
|
|
|
}
|
2020-07-30 04:22:51 +03:00
|
|
|
EXPORT_SYMBOL_GPL(clk_hw_register_gate2);
|