162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright 2012 Freescale Semiconductor, Inc. 462306a36Sopenharmony_ci * Copyright 2012 Linaro Ltd. 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci#include <linux/bits.h> 862306a36Sopenharmony_ci#include <linux/clk.h> 962306a36Sopenharmony_ci#include <linux/clk-provider.h> 1062306a36Sopenharmony_ci#include <linux/io.h> 1162306a36Sopenharmony_ci#include <linux/slab.h> 1262306a36Sopenharmony_ci#include <linux/jiffies.h> 1362306a36Sopenharmony_ci#include <linux/err.h> 1462306a36Sopenharmony_ci#include "clk.h" 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_cistatic int clk_busy_wait(void __iomem *reg, u8 shift) 1762306a36Sopenharmony_ci{ 1862306a36Sopenharmony_ci unsigned long timeout = jiffies + msecs_to_jiffies(10); 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci while (readl_relaxed(reg) & (1 << shift)) 2162306a36Sopenharmony_ci if (time_after(jiffies, timeout)) 2262306a36Sopenharmony_ci return -ETIMEDOUT; 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci return 0; 2562306a36Sopenharmony_ci} 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_cistruct clk_busy_divider { 2862306a36Sopenharmony_ci struct clk_divider div; 2962306a36Sopenharmony_ci const struct clk_ops *div_ops; 3062306a36Sopenharmony_ci void __iomem *reg; 3162306a36Sopenharmony_ci u8 shift; 3262306a36Sopenharmony_ci}; 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_cistatic inline struct clk_busy_divider *to_clk_busy_divider(struct clk_hw *hw) 3562306a36Sopenharmony_ci{ 3662306a36Sopenharmony_ci struct clk_divider *div = to_clk_divider(hw); 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci return container_of(div, struct clk_busy_divider, div); 3962306a36Sopenharmony_ci} 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_cistatic unsigned long clk_busy_divider_recalc_rate(struct clk_hw *hw, 4262306a36Sopenharmony_ci unsigned long parent_rate) 4362306a36Sopenharmony_ci{ 4462306a36Sopenharmony_ci struct clk_busy_divider *busy = to_clk_busy_divider(hw); 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci return busy->div_ops->recalc_rate(&busy->div.hw, parent_rate); 4762306a36Sopenharmony_ci} 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_cistatic long clk_busy_divider_round_rate(struct clk_hw *hw, unsigned long rate, 5062306a36Sopenharmony_ci unsigned long *prate) 5162306a36Sopenharmony_ci{ 5262306a36Sopenharmony_ci struct clk_busy_divider *busy = to_clk_busy_divider(hw); 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ci return busy->div_ops->round_rate(&busy->div.hw, rate, prate); 5562306a36Sopenharmony_ci} 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_cistatic int clk_busy_divider_set_rate(struct clk_hw *hw, unsigned long rate, 5862306a36Sopenharmony_ci unsigned long parent_rate) 5962306a36Sopenharmony_ci{ 6062306a36Sopenharmony_ci struct clk_busy_divider *busy = to_clk_busy_divider(hw); 6162306a36Sopenharmony_ci int ret; 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci ret = busy->div_ops->set_rate(&busy->div.hw, rate, parent_rate); 6462306a36Sopenharmony_ci if (!ret) 6562306a36Sopenharmony_ci ret = clk_busy_wait(busy->reg, busy->shift); 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci return ret; 6862306a36Sopenharmony_ci} 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_cistatic const struct clk_ops clk_busy_divider_ops = { 7162306a36Sopenharmony_ci .recalc_rate = clk_busy_divider_recalc_rate, 7262306a36Sopenharmony_ci .round_rate = clk_busy_divider_round_rate, 7362306a36Sopenharmony_ci .set_rate = clk_busy_divider_set_rate, 7462306a36Sopenharmony_ci}; 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_cistruct clk_hw *imx_clk_hw_busy_divider(const char *name, const char *parent_name, 7762306a36Sopenharmony_ci void __iomem *reg, u8 shift, u8 width, 7862306a36Sopenharmony_ci void __iomem *busy_reg, u8 busy_shift) 7962306a36Sopenharmony_ci{ 8062306a36Sopenharmony_ci struct clk_busy_divider *busy; 8162306a36Sopenharmony_ci struct clk_hw *hw; 8262306a36Sopenharmony_ci struct clk_init_data init; 8362306a36Sopenharmony_ci int ret; 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci busy = kzalloc(sizeof(*busy), GFP_KERNEL); 8662306a36Sopenharmony_ci if (!busy) 8762306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci busy->reg = busy_reg; 9062306a36Sopenharmony_ci busy->shift = busy_shift; 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci busy->div.reg = reg; 9362306a36Sopenharmony_ci busy->div.shift = shift; 9462306a36Sopenharmony_ci busy->div.width = width; 9562306a36Sopenharmony_ci busy->div.lock = &imx_ccm_lock; 9662306a36Sopenharmony_ci busy->div_ops = &clk_divider_ops; 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci init.name = name; 9962306a36Sopenharmony_ci init.ops = &clk_busy_divider_ops; 10062306a36Sopenharmony_ci init.flags = CLK_SET_RATE_PARENT | CLK_IS_CRITICAL; 10162306a36Sopenharmony_ci init.parent_names = &parent_name; 10262306a36Sopenharmony_ci init.num_parents = 1; 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci busy->div.hw.init = &init; 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci hw = &busy->div.hw; 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci ret = clk_hw_register(NULL, hw); 10962306a36Sopenharmony_ci if (ret) { 11062306a36Sopenharmony_ci kfree(busy); 11162306a36Sopenharmony_ci return ERR_PTR(ret); 11262306a36Sopenharmony_ci } 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci return hw; 11562306a36Sopenharmony_ci} 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_cistruct clk_busy_mux { 11862306a36Sopenharmony_ci struct clk_mux mux; 11962306a36Sopenharmony_ci const struct clk_ops *mux_ops; 12062306a36Sopenharmony_ci void __iomem *reg; 12162306a36Sopenharmony_ci u8 shift; 12262306a36Sopenharmony_ci}; 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_cistatic inline struct clk_busy_mux *to_clk_busy_mux(struct clk_hw *hw) 12562306a36Sopenharmony_ci{ 12662306a36Sopenharmony_ci struct clk_mux *mux = to_clk_mux(hw); 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci return container_of(mux, struct clk_busy_mux, mux); 12962306a36Sopenharmony_ci} 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_cistatic u8 clk_busy_mux_get_parent(struct clk_hw *hw) 13262306a36Sopenharmony_ci{ 13362306a36Sopenharmony_ci struct clk_busy_mux *busy = to_clk_busy_mux(hw); 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci return busy->mux_ops->get_parent(&busy->mux.hw); 13662306a36Sopenharmony_ci} 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_cistatic int clk_busy_mux_set_parent(struct clk_hw *hw, u8 index) 13962306a36Sopenharmony_ci{ 14062306a36Sopenharmony_ci struct clk_busy_mux *busy = to_clk_busy_mux(hw); 14162306a36Sopenharmony_ci int ret; 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci ret = busy->mux_ops->set_parent(&busy->mux.hw, index); 14462306a36Sopenharmony_ci if (!ret) 14562306a36Sopenharmony_ci ret = clk_busy_wait(busy->reg, busy->shift); 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ci return ret; 14862306a36Sopenharmony_ci} 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_cistatic const struct clk_ops clk_busy_mux_ops = { 15162306a36Sopenharmony_ci .determine_rate = clk_hw_determine_rate_no_reparent, 15262306a36Sopenharmony_ci .get_parent = clk_busy_mux_get_parent, 15362306a36Sopenharmony_ci .set_parent = clk_busy_mux_set_parent, 15462306a36Sopenharmony_ci}; 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_cistruct clk_hw *imx_clk_hw_busy_mux(const char *name, void __iomem *reg, u8 shift, 15762306a36Sopenharmony_ci u8 width, void __iomem *busy_reg, u8 busy_shift, 15862306a36Sopenharmony_ci const char * const *parent_names, int num_parents) 15962306a36Sopenharmony_ci{ 16062306a36Sopenharmony_ci struct clk_busy_mux *busy; 16162306a36Sopenharmony_ci struct clk_hw *hw; 16262306a36Sopenharmony_ci struct clk_init_data init; 16362306a36Sopenharmony_ci int ret; 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci busy = kzalloc(sizeof(*busy), GFP_KERNEL); 16662306a36Sopenharmony_ci if (!busy) 16762306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci busy->reg = busy_reg; 17062306a36Sopenharmony_ci busy->shift = busy_shift; 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci busy->mux.reg = reg; 17362306a36Sopenharmony_ci busy->mux.shift = shift; 17462306a36Sopenharmony_ci busy->mux.mask = BIT(width) - 1; 17562306a36Sopenharmony_ci busy->mux.lock = &imx_ccm_lock; 17662306a36Sopenharmony_ci busy->mux_ops = &clk_mux_ops; 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci init.name = name; 17962306a36Sopenharmony_ci init.ops = &clk_busy_mux_ops; 18062306a36Sopenharmony_ci init.flags = CLK_IS_CRITICAL; 18162306a36Sopenharmony_ci init.parent_names = parent_names; 18262306a36Sopenharmony_ci init.num_parents = num_parents; 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci busy->mux.hw.init = &init; 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci hw = &busy->mux.hw; 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci ret = clk_hw_register(NULL, hw); 18962306a36Sopenharmony_ci if (ret) { 19062306a36Sopenharmony_ci kfree(busy); 19162306a36Sopenharmony_ci return ERR_PTR(ret); 19262306a36Sopenharmony_ci } 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci return hw; 19562306a36Sopenharmony_ci} 196