18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2012 Freescale Semiconductor, Inc.
48c2ecf20Sopenharmony_ci * Copyright 2012 Linaro Ltd.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/bits.h>
88c2ecf20Sopenharmony_ci#include <linux/clk.h>
98c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
108c2ecf20Sopenharmony_ci#include <linux/io.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
138c2ecf20Sopenharmony_ci#include <linux/err.h>
148c2ecf20Sopenharmony_ci#include "clk.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistatic int clk_busy_wait(void __iomem *reg, u8 shift)
178c2ecf20Sopenharmony_ci{
188c2ecf20Sopenharmony_ci	unsigned long timeout = jiffies + msecs_to_jiffies(10);
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci	while (readl_relaxed(reg) & (1 << shift))
218c2ecf20Sopenharmony_ci		if (time_after(jiffies, timeout))
228c2ecf20Sopenharmony_ci			return -ETIMEDOUT;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	return 0;
258c2ecf20Sopenharmony_ci}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct clk_busy_divider {
288c2ecf20Sopenharmony_ci	struct clk_divider div;
298c2ecf20Sopenharmony_ci	const struct clk_ops *div_ops;
308c2ecf20Sopenharmony_ci	void __iomem *reg;
318c2ecf20Sopenharmony_ci	u8 shift;
328c2ecf20Sopenharmony_ci};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic inline struct clk_busy_divider *to_clk_busy_divider(struct clk_hw *hw)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	struct clk_divider *div = to_clk_divider(hw);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	return container_of(div, struct clk_busy_divider, div);
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic unsigned long clk_busy_divider_recalc_rate(struct clk_hw *hw,
428c2ecf20Sopenharmony_ci						  unsigned long parent_rate)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	struct clk_busy_divider *busy = to_clk_busy_divider(hw);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	return busy->div_ops->recalc_rate(&busy->div.hw, parent_rate);
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic long clk_busy_divider_round_rate(struct clk_hw *hw, unsigned long rate,
508c2ecf20Sopenharmony_ci					unsigned long *prate)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	struct clk_busy_divider *busy = to_clk_busy_divider(hw);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	return busy->div_ops->round_rate(&busy->div.hw, rate, prate);
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic int clk_busy_divider_set_rate(struct clk_hw *hw, unsigned long rate,
588c2ecf20Sopenharmony_ci		unsigned long parent_rate)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	struct clk_busy_divider *busy = to_clk_busy_divider(hw);
618c2ecf20Sopenharmony_ci	int ret;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	ret = busy->div_ops->set_rate(&busy->div.hw, rate, parent_rate);
648c2ecf20Sopenharmony_ci	if (!ret)
658c2ecf20Sopenharmony_ci		ret = clk_busy_wait(busy->reg, busy->shift);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	return ret;
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic const struct clk_ops clk_busy_divider_ops = {
718c2ecf20Sopenharmony_ci	.recalc_rate = clk_busy_divider_recalc_rate,
728c2ecf20Sopenharmony_ci	.round_rate = clk_busy_divider_round_rate,
738c2ecf20Sopenharmony_ci	.set_rate = clk_busy_divider_set_rate,
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistruct clk_hw *imx_clk_hw_busy_divider(const char *name, const char *parent_name,
778c2ecf20Sopenharmony_ci				 void __iomem *reg, u8 shift, u8 width,
788c2ecf20Sopenharmony_ci				 void __iomem *busy_reg, u8 busy_shift)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	struct clk_busy_divider *busy;
818c2ecf20Sopenharmony_ci	struct clk_hw *hw;
828c2ecf20Sopenharmony_ci	struct clk_init_data init;
838c2ecf20Sopenharmony_ci	int ret;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	busy = kzalloc(sizeof(*busy), GFP_KERNEL);
868c2ecf20Sopenharmony_ci	if (!busy)
878c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	busy->reg = busy_reg;
908c2ecf20Sopenharmony_ci	busy->shift = busy_shift;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	busy->div.reg = reg;
938c2ecf20Sopenharmony_ci	busy->div.shift = shift;
948c2ecf20Sopenharmony_ci	busy->div.width = width;
958c2ecf20Sopenharmony_ci	busy->div.lock = &imx_ccm_lock;
968c2ecf20Sopenharmony_ci	busy->div_ops = &clk_divider_ops;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	init.name = name;
998c2ecf20Sopenharmony_ci	init.ops = &clk_busy_divider_ops;
1008c2ecf20Sopenharmony_ci	init.flags = CLK_SET_RATE_PARENT | CLK_IS_CRITICAL;
1018c2ecf20Sopenharmony_ci	init.parent_names = &parent_name;
1028c2ecf20Sopenharmony_ci	init.num_parents = 1;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	busy->div.hw.init = &init;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	hw = &busy->div.hw;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	ret = clk_hw_register(NULL, hw);
1098c2ecf20Sopenharmony_ci	if (ret) {
1108c2ecf20Sopenharmony_ci		kfree(busy);
1118c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return hw;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistruct clk_busy_mux {
1188c2ecf20Sopenharmony_ci	struct clk_mux mux;
1198c2ecf20Sopenharmony_ci	const struct clk_ops *mux_ops;
1208c2ecf20Sopenharmony_ci	void __iomem *reg;
1218c2ecf20Sopenharmony_ci	u8 shift;
1228c2ecf20Sopenharmony_ci};
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic inline struct clk_busy_mux *to_clk_busy_mux(struct clk_hw *hw)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	struct clk_mux *mux = to_clk_mux(hw);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	return container_of(mux, struct clk_busy_mux, mux);
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic u8 clk_busy_mux_get_parent(struct clk_hw *hw)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	struct clk_busy_mux *busy = to_clk_busy_mux(hw);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	return busy->mux_ops->get_parent(&busy->mux.hw);
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic int clk_busy_mux_set_parent(struct clk_hw *hw, u8 index)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	struct clk_busy_mux *busy = to_clk_busy_mux(hw);
1418c2ecf20Sopenharmony_ci	int ret;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	ret = busy->mux_ops->set_parent(&busy->mux.hw, index);
1448c2ecf20Sopenharmony_ci	if (!ret)
1458c2ecf20Sopenharmony_ci		ret = clk_busy_wait(busy->reg, busy->shift);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	return ret;
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic const struct clk_ops clk_busy_mux_ops = {
1518c2ecf20Sopenharmony_ci	.get_parent = clk_busy_mux_get_parent,
1528c2ecf20Sopenharmony_ci	.set_parent = clk_busy_mux_set_parent,
1538c2ecf20Sopenharmony_ci};
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistruct clk_hw *imx_clk_hw_busy_mux(const char *name, void __iomem *reg, u8 shift,
1568c2ecf20Sopenharmony_ci			     u8 width, void __iomem *busy_reg, u8 busy_shift,
1578c2ecf20Sopenharmony_ci			     const char * const *parent_names, int num_parents)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	struct clk_busy_mux *busy;
1608c2ecf20Sopenharmony_ci	struct clk_hw *hw;
1618c2ecf20Sopenharmony_ci	struct clk_init_data init;
1628c2ecf20Sopenharmony_ci	int ret;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	busy = kzalloc(sizeof(*busy), GFP_KERNEL);
1658c2ecf20Sopenharmony_ci	if (!busy)
1668c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	busy->reg = busy_reg;
1698c2ecf20Sopenharmony_ci	busy->shift = busy_shift;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	busy->mux.reg = reg;
1728c2ecf20Sopenharmony_ci	busy->mux.shift = shift;
1738c2ecf20Sopenharmony_ci	busy->mux.mask = BIT(width) - 1;
1748c2ecf20Sopenharmony_ci	busy->mux.lock = &imx_ccm_lock;
1758c2ecf20Sopenharmony_ci	busy->mux_ops = &clk_mux_ops;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	init.name = name;
1788c2ecf20Sopenharmony_ci	init.ops = &clk_busy_mux_ops;
1798c2ecf20Sopenharmony_ci	init.flags = CLK_IS_CRITICAL;
1808c2ecf20Sopenharmony_ci	init.parent_names = parent_names;
1818c2ecf20Sopenharmony_ci	init.num_parents = num_parents;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	busy->mux.hw.init = &init;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	hw = &busy->mux.hw;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	ret = clk_hw_register(NULL, hw);
1888c2ecf20Sopenharmony_ci	if (ret) {
1898c2ecf20Sopenharmony_ci		kfree(busy);
1908c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
1918c2ecf20Sopenharmony_ci	}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	return hw;
1948c2ecf20Sopenharmony_ci}
195