2018-11-14 21:01:51 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Freescale Semiconductor, Inc.
|
|
|
|
* Copyright 2017~2018 NXP
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-08-05 07:17:29 +08:00
|
|
|
#include <linux/bits.h>
|
2018-11-14 21:01:51 +08:00
|
|
|
#include <linux/clk-provider.h>
|
|
|
|
#include <linux/err.h>
|
clk: imx: disable i.mx7ulp composite clock during initialization
i.MX7ULP peripheral clock ONLY allow parent/rate to be changed
with clock gated, however, during clock tree initialization, the
peripheral clock could be enabled by bootloader, but the prepare
count in clock tree is still zero, so clock core driver will allow
parent/rate changed even with CLK_SET_RATE_GATE/CLK_SET_PARENT_GATE
set, but the change will fail due to HW NOT allow parent/rate change
with clock enabled. It will cause clock HW status mismatch with
clock tree info and lead to function issue. Below is an example:
usdhc0's pcc clock value is 0xC5000000 during kernel boot up, it
means usdhc0 clock is enabled, its parent is APLL_PFD1. In DT file,
the usdhc0 clock settings are as below:
assigned-clocks = <&pcc2 IMX7ULP_CLK_USDHC0>;
assigned-clock-parents = <&scg1 IMX7ULP_CLK_NIC1_DIV>;
when kernel boot up, the clock tree info is as below, but the usdhc0
PCC register is still 0xC5000000, which means its parent is still
from APLL_PFD1, which is incorrect and cause usdhc0 NOT work.
nic1_clk 2 2 0 176000000 0 0 50000
usdhc0 0 0 0 176000000 0 0 50000
After making sure the peripheral clock is disabled during clock tree
initialization, the usdhc0 is working, and this change is necessary
for all i.MX7ULP peripheral clocks.
Signed-off-by: Anson Huang <Anson.Huang@nxp.com>
Reviewed-by: Abel Vesa <abel.vesa@nxp.com>
Link: https://lore.kernel.org/r/20210914065208.3582128-5-ping.bai@nxp.com
Signed-off-by: Abel Vesa <abel.vesa@nxp.com>
2021-09-14 14:52:03 +08:00
|
|
|
#include <linux/io.h>
|
2018-11-14 21:01:51 +08:00
|
|
|
#include <linux/slab.h>
|
|
|
|
|
2021-08-13 01:00:23 +08:00
|
|
|
#include "../clk-fractional-divider.h"
|
2018-11-14 21:01:51 +08:00
|
|
|
#include "clk.h"
|
|
|
|
|
|
|
|
#define PCG_PCS_SHIFT 24
|
|
|
|
#define PCG_PCS_MASK 0x7
|
|
|
|
#define PCG_CGC_SHIFT 30
|
|
|
|
#define PCG_FRAC_SHIFT 3
|
|
|
|
#define PCG_FRAC_WIDTH 1
|
|
|
|
#define PCG_FRAC_MASK BIT(3)
|
|
|
|
#define PCG_PCD_SHIFT 0
|
|
|
|
#define PCG_PCD_WIDTH 3
|
|
|
|
#define PCG_PCD_MASK 0x7
|
|
|
|
|
2021-09-14 14:52:02 +08:00
|
|
|
#define SW_RST BIT(28)
|
|
|
|
|
|
|
|
static int pcc_gate_enable(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_gate *gate = to_clk_gate(hw);
|
2021-09-14 14:52:08 +08:00
|
|
|
unsigned long flags;
|
2021-09-14 14:52:02 +08:00
|
|
|
u32 val;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = clk_gate_ops.enable(hw);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2021-09-14 14:52:08 +08:00
|
|
|
spin_lock_irqsave(gate->lock, flags);
|
2021-09-14 14:52:02 +08:00
|
|
|
/*
|
|
|
|
* release the sw reset for peripherals associated with
|
|
|
|
* with this pcc clock.
|
|
|
|
*/
|
|
|
|
val = readl(gate->reg);
|
|
|
|
val |= SW_RST;
|
|
|
|
writel(val, gate->reg);
|
|
|
|
|
2021-09-14 14:52:08 +08:00
|
|
|
spin_unlock_irqrestore(gate->lock, flags);
|
|
|
|
|
2021-09-14 14:52:02 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pcc_gate_disable(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
clk_gate_ops.disable(hw);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pcc_gate_is_enabled(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
return clk_gate_ops.is_enabled(hw);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct clk_ops pcc_gate_ops = {
|
|
|
|
.enable = pcc_gate_enable,
|
|
|
|
.disable = pcc_gate_disable,
|
|
|
|
.is_enabled = pcc_gate_is_enabled,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct clk_hw *imx_ulp_clk_hw_composite(const char *name,
|
2018-11-14 21:01:51 +08:00
|
|
|
const char * const *parent_names,
|
|
|
|
int num_parents, bool mux_present,
|
|
|
|
bool rate_present, bool gate_present,
|
2021-09-14 14:52:02 +08:00
|
|
|
void __iomem *reg, bool has_swrst)
|
2018-11-14 21:01:51 +08:00
|
|
|
{
|
|
|
|
struct clk_hw *mux_hw = NULL, *fd_hw = NULL, *gate_hw = NULL;
|
|
|
|
struct clk_fractional_divider *fd = NULL;
|
|
|
|
struct clk_gate *gate = NULL;
|
|
|
|
struct clk_mux *mux = NULL;
|
|
|
|
struct clk_hw *hw;
|
clk: imx: disable i.mx7ulp composite clock during initialization
i.MX7ULP peripheral clock ONLY allow parent/rate to be changed
with clock gated, however, during clock tree initialization, the
peripheral clock could be enabled by bootloader, but the prepare
count in clock tree is still zero, so clock core driver will allow
parent/rate changed even with CLK_SET_RATE_GATE/CLK_SET_PARENT_GATE
set, but the change will fail due to HW NOT allow parent/rate change
with clock enabled. It will cause clock HW status mismatch with
clock tree info and lead to function issue. Below is an example:
usdhc0's pcc clock value is 0xC5000000 during kernel boot up, it
means usdhc0 clock is enabled, its parent is APLL_PFD1. In DT file,
the usdhc0 clock settings are as below:
assigned-clocks = <&pcc2 IMX7ULP_CLK_USDHC0>;
assigned-clock-parents = <&scg1 IMX7ULP_CLK_NIC1_DIV>;
when kernel boot up, the clock tree info is as below, but the usdhc0
PCC register is still 0xC5000000, which means its parent is still
from APLL_PFD1, which is incorrect and cause usdhc0 NOT work.
nic1_clk 2 2 0 176000000 0 0 50000
usdhc0 0 0 0 176000000 0 0 50000
After making sure the peripheral clock is disabled during clock tree
initialization, the usdhc0 is working, and this change is necessary
for all i.MX7ULP peripheral clocks.
Signed-off-by: Anson Huang <Anson.Huang@nxp.com>
Reviewed-by: Abel Vesa <abel.vesa@nxp.com>
Link: https://lore.kernel.org/r/20210914065208.3582128-5-ping.bai@nxp.com
Signed-off-by: Abel Vesa <abel.vesa@nxp.com>
2021-09-14 14:52:03 +08:00
|
|
|
u32 val;
|
2018-11-14 21:01:51 +08:00
|
|
|
|
|
|
|
if (mux_present) {
|
|
|
|
mux = kzalloc(sizeof(*mux), GFP_KERNEL);
|
|
|
|
if (!mux)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
mux_hw = &mux->hw;
|
|
|
|
mux->reg = reg;
|
|
|
|
mux->shift = PCG_PCS_SHIFT;
|
|
|
|
mux->mask = PCG_PCS_MASK;
|
2021-09-14 14:52:08 +08:00
|
|
|
if (has_swrst)
|
|
|
|
mux->lock = &imx_ccm_lock;
|
2018-11-14 21:01:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rate_present) {
|
|
|
|
fd = kzalloc(sizeof(*fd), GFP_KERNEL);
|
|
|
|
if (!fd) {
|
|
|
|
kfree(mux);
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
}
|
|
|
|
fd_hw = &fd->hw;
|
|
|
|
fd->reg = reg;
|
|
|
|
fd->mshift = PCG_FRAC_SHIFT;
|
|
|
|
fd->mwidth = PCG_FRAC_WIDTH;
|
|
|
|
fd->mmask = PCG_FRAC_MASK;
|
|
|
|
fd->nshift = PCG_PCD_SHIFT;
|
|
|
|
fd->nwidth = PCG_PCD_WIDTH;
|
|
|
|
fd->nmask = PCG_PCD_MASK;
|
|
|
|
fd->flags = CLK_FRAC_DIVIDER_ZERO_BASED;
|
2021-09-14 14:52:08 +08:00
|
|
|
if (has_swrst)
|
|
|
|
fd->lock = &imx_ccm_lock;
|
2018-11-14 21:01:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gate_present) {
|
|
|
|
gate = kzalloc(sizeof(*gate), GFP_KERNEL);
|
|
|
|
if (!gate) {
|
|
|
|
kfree(mux);
|
|
|
|
kfree(fd);
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
}
|
|
|
|
gate_hw = &gate->hw;
|
|
|
|
gate->reg = reg;
|
|
|
|
gate->bit_idx = PCG_CGC_SHIFT;
|
2021-09-14 14:52:08 +08:00
|
|
|
if (has_swrst)
|
|
|
|
gate->lock = &imx_ccm_lock;
|
clk: imx: disable i.mx7ulp composite clock during initialization
i.MX7ULP peripheral clock ONLY allow parent/rate to be changed
with clock gated, however, during clock tree initialization, the
peripheral clock could be enabled by bootloader, but the prepare
count in clock tree is still zero, so clock core driver will allow
parent/rate changed even with CLK_SET_RATE_GATE/CLK_SET_PARENT_GATE
set, but the change will fail due to HW NOT allow parent/rate change
with clock enabled. It will cause clock HW status mismatch with
clock tree info and lead to function issue. Below is an example:
usdhc0's pcc clock value is 0xC5000000 during kernel boot up, it
means usdhc0 clock is enabled, its parent is APLL_PFD1. In DT file,
the usdhc0 clock settings are as below:
assigned-clocks = <&pcc2 IMX7ULP_CLK_USDHC0>;
assigned-clock-parents = <&scg1 IMX7ULP_CLK_NIC1_DIV>;
when kernel boot up, the clock tree info is as below, but the usdhc0
PCC register is still 0xC5000000, which means its parent is still
from APLL_PFD1, which is incorrect and cause usdhc0 NOT work.
nic1_clk 2 2 0 176000000 0 0 50000
usdhc0 0 0 0 176000000 0 0 50000
After making sure the peripheral clock is disabled during clock tree
initialization, the usdhc0 is working, and this change is necessary
for all i.MX7ULP peripheral clocks.
Signed-off-by: Anson Huang <Anson.Huang@nxp.com>
Reviewed-by: Abel Vesa <abel.vesa@nxp.com>
Link: https://lore.kernel.org/r/20210914065208.3582128-5-ping.bai@nxp.com
Signed-off-by: Abel Vesa <abel.vesa@nxp.com>
2021-09-14 14:52:03 +08:00
|
|
|
/*
|
|
|
|
* make sure clock is gated during clock tree initialization,
|
|
|
|
* the HW ONLY allow clock parent/rate changed with clock gated,
|
|
|
|
* during clock tree initialization, clocks could be enabled
|
|
|
|
* by bootloader, so the HW status will mismatch with clock tree
|
|
|
|
* prepare count, then clock core driver will allow parent/rate
|
|
|
|
* change since the prepare count is zero, but HW actually
|
|
|
|
* prevent the parent/rate change due to the clock is enabled.
|
|
|
|
*/
|
|
|
|
val = readl_relaxed(reg);
|
|
|
|
val &= ~(1 << PCG_CGC_SHIFT);
|
|
|
|
writel_relaxed(val, reg);
|
2018-11-14 21:01:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
hw = clk_hw_register_composite(NULL, name, parent_names, num_parents,
|
|
|
|
mux_hw, &clk_mux_ops, fd_hw,
|
|
|
|
&clk_fractional_divider_ops, gate_hw,
|
2021-09-14 14:52:02 +08:00
|
|
|
has_swrst ? &pcc_gate_ops : &clk_gate_ops, CLK_SET_RATE_GATE |
|
2021-09-14 14:52:04 +08:00
|
|
|
CLK_SET_PARENT_GATE | CLK_SET_RATE_NO_REPARENT);
|
2018-11-14 21:01:51 +08:00
|
|
|
if (IS_ERR(hw)) {
|
|
|
|
kfree(mux);
|
|
|
|
kfree(fd);
|
|
|
|
kfree(gate);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hw;
|
|
|
|
}
|
2021-09-14 14:52:02 +08:00
|
|
|
|
|
|
|
struct clk_hw *imx7ulp_clk_hw_composite(const char *name, const char * const *parent_names,
|
|
|
|
int num_parents, bool mux_present, bool rate_present,
|
|
|
|
bool gate_present, void __iomem *reg)
|
|
|
|
{
|
|
|
|
return imx_ulp_clk_hw_composite(name, parent_names, num_parents, mux_present, rate_present,
|
|
|
|
gate_present, reg, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct clk_hw *imx8ulp_clk_hw_composite(const char *name, const char * const *parent_names,
|
|
|
|
int num_parents, bool mux_present, bool rate_present,
|
|
|
|
bool gate_present, void __iomem *reg, bool has_swrst)
|
|
|
|
{
|
|
|
|
return imx_ulp_clk_hw_composite(name, parent_names, num_parents, mux_present, rate_present,
|
|
|
|
gate_present, reg, has_swrst);
|
|
|
|
}
|
2021-09-17 14:16:29 +08:00
|
|
|
EXPORT_SYMBOL_GPL(imx8ulp_clk_hw_composite);
|