2019-05-27 09:55:08 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2012-04-04 12:20:56 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Freescale Semiconductor, Inc.
|
|
|
|
* Copyright 2012 Linaro Ltd.
|
|
|
|
*/
|
|
|
|
|
2020-08-05 02:17:29 +03:00
|
|
|
#include <linux/bits.h>
|
2012-04-04 12:20:56 +04:00
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/clk-provider.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/jiffies.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include "clk.h"
|
|
|
|
|
|
|
|
static int clk_busy_wait(void __iomem *reg, u8 shift)
|
|
|
|
{
|
|
|
|
unsigned long timeout = jiffies + msecs_to_jiffies(10);
|
|
|
|
|
|
|
|
while (readl_relaxed(reg) & (1 << shift))
|
|
|
|
if (time_after(jiffies, timeout))
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct clk_busy_divider {
|
|
|
|
struct clk_divider div;
|
|
|
|
const struct clk_ops *div_ops;
|
|
|
|
void __iomem *reg;
|
|
|
|
u8 shift;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline struct clk_busy_divider *to_clk_busy_divider(struct clk_hw *hw)
|
|
|
|
{
|
2016-01-08 18:51:46 +03:00
|
|
|
struct clk_divider *div = to_clk_divider(hw);
|
2012-04-04 12:20:56 +04:00
|
|
|
|
|
|
|
return container_of(div, struct clk_busy_divider, div);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long clk_busy_divider_recalc_rate(struct clk_hw *hw,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
struct clk_busy_divider *busy = to_clk_busy_divider(hw);
|
|
|
|
|
|
|
|
return busy->div_ops->recalc_rate(&busy->div.hw, parent_rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
static long clk_busy_divider_round_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long *prate)
|
|
|
|
{
|
|
|
|
struct clk_busy_divider *busy = to_clk_busy_divider(hw);
|
|
|
|
|
|
|
|
return busy->div_ops->round_rate(&busy->div.hw, rate, prate);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int clk_busy_divider_set_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
struct clk_busy_divider *busy = to_clk_busy_divider(hw);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = busy->div_ops->set_rate(&busy->div.hw, rate, parent_rate);
|
|
|
|
if (!ret)
|
|
|
|
ret = clk_busy_wait(busy->reg, busy->shift);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-22 16:18:29 +03:00
|
|
|
static const struct clk_ops clk_busy_divider_ops = {
|
2012-04-04 12:20:56 +04:00
|
|
|
.recalc_rate = clk_busy_divider_recalc_rate,
|
|
|
|
.round_rate = clk_busy_divider_round_rate,
|
|
|
|
.set_rate = clk_busy_divider_set_rate,
|
|
|
|
};
|
|
|
|
|
2019-05-29 15:26:42 +03:00
|
|
|
struct clk_hw *imx_clk_hw_busy_divider(const char *name, const char *parent_name,
|
2012-04-04 12:20:56 +04:00
|
|
|
void __iomem *reg, u8 shift, u8 width,
|
|
|
|
void __iomem *busy_reg, u8 busy_shift)
|
|
|
|
{
|
|
|
|
struct clk_busy_divider *busy;
|
2019-05-29 15:26:42 +03:00
|
|
|
struct clk_hw *hw;
|
2012-04-04 12:20:56 +04:00
|
|
|
struct clk_init_data init;
|
2019-05-29 15:26:42 +03:00
|
|
|
int ret;
|
2012-04-04 12:20:56 +04:00
|
|
|
|
|
|
|
busy = kzalloc(sizeof(*busy), GFP_KERNEL);
|
|
|
|
if (!busy)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
busy->reg = busy_reg;
|
|
|
|
busy->shift = busy_shift;
|
|
|
|
|
|
|
|
busy->div.reg = reg;
|
|
|
|
busy->div.shift = shift;
|
|
|
|
busy->div.width = width;
|
|
|
|
busy->div.lock = &imx_ccm_lock;
|
|
|
|
busy->div_ops = &clk_divider_ops;
|
|
|
|
|
|
|
|
init.name = name;
|
|
|
|
init.ops = &clk_busy_divider_ops;
|
2018-03-20 05:24:01 +03:00
|
|
|
init.flags = CLK_SET_RATE_PARENT | CLK_IS_CRITICAL;
|
2012-04-04 12:20:56 +04:00
|
|
|
init.parent_names = &parent_name;
|
|
|
|
init.num_parents = 1;
|
|
|
|
|
|
|
|
busy->div.hw.init = &init;
|
|
|
|
|
2019-05-29 15:26:42 +03:00
|
|
|
hw = &busy->div.hw;
|
|
|
|
|
|
|
|
ret = clk_hw_register(NULL, hw);
|
|
|
|
if (ret) {
|
2012-04-04 12:20:56 +04:00
|
|
|
kfree(busy);
|
2019-05-29 15:26:42 +03:00
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
2012-04-04 12:20:56 +04:00
|
|
|
|
2019-05-29 15:26:42 +03:00
|
|
|
return hw;
|
2012-04-04 12:20:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct clk_busy_mux {
|
|
|
|
struct clk_mux mux;
|
|
|
|
const struct clk_ops *mux_ops;
|
|
|
|
void __iomem *reg;
|
|
|
|
u8 shift;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline struct clk_busy_mux *to_clk_busy_mux(struct clk_hw *hw)
|
|
|
|
{
|
2016-01-08 18:51:46 +03:00
|
|
|
struct clk_mux *mux = to_clk_mux(hw);
|
2012-04-04 12:20:56 +04:00
|
|
|
|
|
|
|
return container_of(mux, struct clk_busy_mux, mux);
|
|
|
|
}
|
|
|
|
|
|
|
|
static u8 clk_busy_mux_get_parent(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_busy_mux *busy = to_clk_busy_mux(hw);
|
|
|
|
|
|
|
|
return busy->mux_ops->get_parent(&busy->mux.hw);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int clk_busy_mux_set_parent(struct clk_hw *hw, u8 index)
|
|
|
|
{
|
|
|
|
struct clk_busy_mux *busy = to_clk_busy_mux(hw);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = busy->mux_ops->set_parent(&busy->mux.hw, index);
|
|
|
|
if (!ret)
|
|
|
|
ret = clk_busy_wait(busy->reg, busy->shift);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-22 16:18:29 +03:00
|
|
|
static const struct clk_ops clk_busy_mux_ops = {
|
2012-04-04 12:20:56 +04:00
|
|
|
.get_parent = clk_busy_mux_get_parent,
|
|
|
|
.set_parent = clk_busy_mux_set_parent,
|
|
|
|
};
|
|
|
|
|
2019-05-29 15:26:42 +03:00
|
|
|
struct clk_hw *imx_clk_hw_busy_mux(const char *name, void __iomem *reg, u8 shift,
|
2012-04-04 12:20:56 +04:00
|
|
|
u8 width, void __iomem *busy_reg, u8 busy_shift,
|
2018-11-14 16:02:00 +03:00
|
|
|
const char * const *parent_names, int num_parents)
|
2012-04-04 12:20:56 +04:00
|
|
|
{
|
|
|
|
struct clk_busy_mux *busy;
|
2019-05-29 15:26:42 +03:00
|
|
|
struct clk_hw *hw;
|
2012-04-04 12:20:56 +04:00
|
|
|
struct clk_init_data init;
|
2019-05-29 15:26:42 +03:00
|
|
|
int ret;
|
2012-04-04 12:20:56 +04:00
|
|
|
|
|
|
|
busy = kzalloc(sizeof(*busy), GFP_KERNEL);
|
|
|
|
if (!busy)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
busy->reg = busy_reg;
|
|
|
|
busy->shift = busy_shift;
|
|
|
|
|
|
|
|
busy->mux.reg = reg;
|
|
|
|
busy->mux.shift = shift;
|
2013-03-26 17:42:10 +04:00
|
|
|
busy->mux.mask = BIT(width) - 1;
|
2012-04-04 12:20:56 +04:00
|
|
|
busy->mux.lock = &imx_ccm_lock;
|
|
|
|
busy->mux_ops = &clk_mux_ops;
|
|
|
|
|
|
|
|
init.name = name;
|
|
|
|
init.ops = &clk_busy_mux_ops;
|
2018-03-20 05:24:01 +03:00
|
|
|
init.flags = CLK_IS_CRITICAL;
|
2012-04-04 12:20:56 +04:00
|
|
|
init.parent_names = parent_names;
|
|
|
|
init.num_parents = num_parents;
|
|
|
|
|
|
|
|
busy->mux.hw.init = &init;
|
|
|
|
|
2019-05-29 15:26:42 +03:00
|
|
|
hw = &busy->mux.hw;
|
|
|
|
|
|
|
|
ret = clk_hw_register(NULL, hw);
|
|
|
|
if (ret) {
|
2012-04-04 12:20:56 +04:00
|
|
|
kfree(busy);
|
2019-05-29 15:26:42 +03:00
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
2012-04-04 12:20:56 +04:00
|
|
|
|
2019-05-29 15:26:42 +03:00
|
|
|
return hw;
|
2012-04-04 12:20:56 +04:00
|
|
|
}
|