xref: /linux/drivers/clk/zynqmp/divider.c (revision 9d66e85784f196fb7442193a1d7f3896ed418806)
13fde0e16SJolly Shah // SPDX-License-Identifier: GPL-2.0
23fde0e16SJolly Shah /*
33fde0e16SJolly Shah  * Zynq UltraScale+ MPSoC Divider support
43fde0e16SJolly Shah  *
534bbe036STejas Patel  *  Copyright (C) 2016-2019 Xilinx
63fde0e16SJolly Shah  *
73fde0e16SJolly Shah  * Adjustable divider clock implementation
83fde0e16SJolly Shah  */
93fde0e16SJolly Shah 
103fde0e16SJolly Shah #include <linux/clk.h>
113fde0e16SJolly Shah #include <linux/clk-provider.h>
123fde0e16SJolly Shah #include <linux/slab.h>
133fde0e16SJolly Shah #include "clk-zynqmp.h"
143fde0e16SJolly Shah 
153fde0e16SJolly Shah /*
163fde0e16SJolly Shah  * DOC: basic adjustable divider clock that cannot gate
173fde0e16SJolly Shah  *
183fde0e16SJolly Shah  * Traits of this clock:
193fde0e16SJolly Shah  * prepare - clk_prepare only ensures that parents are prepared
203fde0e16SJolly Shah  * enable - clk_enable only ensures that parents are enabled
213fde0e16SJolly Shah  * rate - rate is adjustable.  clk->rate = ceiling(parent->rate / divisor)
223fde0e16SJolly Shah  * parent - fixed parent.  No clk_set_parent support
233fde0e16SJolly Shah  */
243fde0e16SJolly Shah 
253fde0e16SJolly Shah #define to_zynqmp_clk_divider(_hw)		\
263fde0e16SJolly Shah 	container_of(_hw, struct zynqmp_clk_divider, hw)
273fde0e16SJolly Shah 
283fde0e16SJolly Shah #define CLK_FRAC		BIT(13) /* has a fractional parent */
292ce7e495STejas Patel #define CUSTOM_FLAG_CLK_FRAC	BIT(0) /* has a fractional parent in custom type flag */
303fde0e16SJolly Shah 
313fde0e16SJolly Shah /**
323fde0e16SJolly Shah  * struct zynqmp_clk_divider - adjustable divider clock
333fde0e16SJolly Shah  * @hw:		handle between common and hardware-specific interfaces
343fde0e16SJolly Shah  * @flags:	Hardware specific flags
35c06e6440SMichael Tretter  * @is_frac:	The divider is a fractional divider
363fde0e16SJolly Shah  * @clk_id:	Id of clock
373fde0e16SJolly Shah  * @div_type:	divisor type (TYPE_DIV1 or TYPE_DIV2)
383fde0e16SJolly Shah  */
393fde0e16SJolly Shah struct zynqmp_clk_divider {
403fde0e16SJolly Shah 	struct clk_hw hw;
413fde0e16SJolly Shah 	u8 flags;
42c06e6440SMichael Tretter 	bool is_frac;
433fde0e16SJolly Shah 	u32 clk_id;
443fde0e16SJolly Shah 	u32 div_type;
45e942171bSRajan Vaja 	u16 max_div;
463fde0e16SJolly Shah };
473fde0e16SJolly Shah 
483fde0e16SJolly Shah static inline int zynqmp_divider_get_val(unsigned long parent_rate,
4934bbe036STejas Patel 					 unsigned long rate, u16 flags)
503fde0e16SJolly Shah {
5134bbe036STejas Patel 	int up, down;
5234bbe036STejas Patel 	unsigned long up_rate, down_rate;
5334bbe036STejas Patel 
5434bbe036STejas Patel 	if (flags & CLK_DIVIDER_POWER_OF_TWO) {
5534bbe036STejas Patel 		up = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
5634bbe036STejas Patel 		down = DIV_ROUND_DOWN_ULL((u64)parent_rate, rate);
5734bbe036STejas Patel 
5834bbe036STejas Patel 		up = __roundup_pow_of_two(up);
5934bbe036STejas Patel 		down = __rounddown_pow_of_two(down);
6034bbe036STejas Patel 
6134bbe036STejas Patel 		up_rate = DIV_ROUND_UP_ULL((u64)parent_rate, up);
6234bbe036STejas Patel 		down_rate = DIV_ROUND_UP_ULL((u64)parent_rate, down);
6334bbe036STejas Patel 
6434bbe036STejas Patel 		return (rate - up_rate) <= (down_rate - rate) ? up : down;
6534bbe036STejas Patel 
6634bbe036STejas Patel 	} else {
673fde0e16SJolly Shah 		return DIV_ROUND_CLOSEST(parent_rate, rate);
683fde0e16SJolly Shah 	}
6934bbe036STejas Patel }
703fde0e16SJolly Shah 
713fde0e16SJolly Shah /**
723fde0e16SJolly Shah  * zynqmp_clk_divider_recalc_rate() - Recalc rate of divider clock
733fde0e16SJolly Shah  * @hw:			handle between common and hardware-specific interfaces
743fde0e16SJolly Shah  * @parent_rate:	rate of parent clock
753fde0e16SJolly Shah  *
763fde0e16SJolly Shah  * Return: 0 on success else error+reason
773fde0e16SJolly Shah  */
783fde0e16SJolly Shah static unsigned long zynqmp_clk_divider_recalc_rate(struct clk_hw *hw,
793fde0e16SJolly Shah 						    unsigned long parent_rate)
803fde0e16SJolly Shah {
813fde0e16SJolly Shah 	struct zynqmp_clk_divider *divider = to_zynqmp_clk_divider(hw);
823fde0e16SJolly Shah 	const char *clk_name = clk_hw_get_name(hw);
833fde0e16SJolly Shah 	u32 clk_id = divider->clk_id;
843fde0e16SJolly Shah 	u32 div_type = divider->div_type;
853fde0e16SJolly Shah 	u32 div, value;
863fde0e16SJolly Shah 	int ret;
873fde0e16SJolly Shah 	const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
883fde0e16SJolly Shah 
893fde0e16SJolly Shah 	ret = eemi_ops->clock_getdivider(clk_id, &div);
903fde0e16SJolly Shah 
913fde0e16SJolly Shah 	if (ret)
923fde0e16SJolly Shah 		pr_warn_once("%s() get divider failed for %s, ret = %d\n",
933fde0e16SJolly Shah 			     __func__, clk_name, ret);
943fde0e16SJolly Shah 
953fde0e16SJolly Shah 	if (div_type == TYPE_DIV1)
963fde0e16SJolly Shah 		value = div & 0xFFFF;
973fde0e16SJolly Shah 	else
983fde0e16SJolly Shah 		value = div >> 16;
993fde0e16SJolly Shah 
10034bbe036STejas Patel 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
10134bbe036STejas Patel 		value = 1 << value;
10234bbe036STejas Patel 
10360d74e01SRajan Vaja 	if (!value) {
10460d74e01SRajan Vaja 		WARN(!(divider->flags & CLK_DIVIDER_ALLOW_ZERO),
10560d74e01SRajan Vaja 		     "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
10660d74e01SRajan Vaja 		     clk_name);
10760d74e01SRajan Vaja 		return parent_rate;
10860d74e01SRajan Vaja 	}
10960d74e01SRajan Vaja 
1103fde0e16SJolly Shah 	return DIV_ROUND_UP_ULL(parent_rate, value);
1113fde0e16SJolly Shah }
1123fde0e16SJolly Shah 
1134ebd92d2SRajan Vaja static void zynqmp_get_divider2_val(struct clk_hw *hw,
1144ebd92d2SRajan Vaja 				    unsigned long rate,
1154ebd92d2SRajan Vaja 				    struct zynqmp_clk_divider *divider,
1164ebd92d2SRajan Vaja 				    int *bestdiv)
1174ebd92d2SRajan Vaja {
1184ebd92d2SRajan Vaja 	int div1;
1194ebd92d2SRajan Vaja 	int div2;
1204ebd92d2SRajan Vaja 	long error = LONG_MAX;
121b8c1049cSTejas Patel 	unsigned long div1_prate;
122b8c1049cSTejas Patel 	struct clk_hw *div1_parent_hw;
123b8c1049cSTejas Patel 	struct clk_hw *div2_parent_hw = clk_hw_get_parent(hw);
124b8c1049cSTejas Patel 	struct zynqmp_clk_divider *pdivider =
125b8c1049cSTejas Patel 				to_zynqmp_clk_divider(div2_parent_hw);
1264ebd92d2SRajan Vaja 
1274ebd92d2SRajan Vaja 	if (!pdivider)
1284ebd92d2SRajan Vaja 		return;
1294ebd92d2SRajan Vaja 
130b8c1049cSTejas Patel 	div1_parent_hw = clk_hw_get_parent(div2_parent_hw);
131b8c1049cSTejas Patel 	if (!div1_parent_hw)
132b8c1049cSTejas Patel 		return;
133b8c1049cSTejas Patel 
134b8c1049cSTejas Patel 	div1_prate = clk_hw_get_rate(div1_parent_hw);
1354ebd92d2SRajan Vaja 	*bestdiv = 1;
1364ebd92d2SRajan Vaja 	for (div1 = 1; div1 <= pdivider->max_div;) {
1374ebd92d2SRajan Vaja 		for (div2 = 1; div2 <= divider->max_div;) {
138b8c1049cSTejas Patel 			long new_error = ((div1_prate / div1) / div2) - rate;
1394ebd92d2SRajan Vaja 
1404ebd92d2SRajan Vaja 			if (abs(new_error) < abs(error)) {
1414ebd92d2SRajan Vaja 				*bestdiv = div2;
1424ebd92d2SRajan Vaja 				error = new_error;
1434ebd92d2SRajan Vaja 			}
1444ebd92d2SRajan Vaja 			if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
1454ebd92d2SRajan Vaja 				div2 = div2 << 1;
1464ebd92d2SRajan Vaja 			else
1474ebd92d2SRajan Vaja 				div2++;
1484ebd92d2SRajan Vaja 		}
1494ebd92d2SRajan Vaja 		if (pdivider->flags & CLK_DIVIDER_POWER_OF_TWO)
1504ebd92d2SRajan Vaja 			div1 = div1 << 1;
1514ebd92d2SRajan Vaja 		else
1524ebd92d2SRajan Vaja 			div1++;
1534ebd92d2SRajan Vaja 	}
1544ebd92d2SRajan Vaja }
1554ebd92d2SRajan Vaja 
1563fde0e16SJolly Shah /**
1573fde0e16SJolly Shah  * zynqmp_clk_divider_round_rate() - Round rate of divider clock
1583fde0e16SJolly Shah  * @hw:			handle between common and hardware-specific interfaces
1593fde0e16SJolly Shah  * @rate:		rate of clock to be set
1603fde0e16SJolly Shah  * @prate:		rate of parent clock
1613fde0e16SJolly Shah  *
1623fde0e16SJolly Shah  * Return: 0 on success else error+reason
1633fde0e16SJolly Shah  */
1643fde0e16SJolly Shah static long zynqmp_clk_divider_round_rate(struct clk_hw *hw,
1653fde0e16SJolly Shah 					  unsigned long rate,
1663fde0e16SJolly Shah 					  unsigned long *prate)
1673fde0e16SJolly Shah {
1683fde0e16SJolly Shah 	struct zynqmp_clk_divider *divider = to_zynqmp_clk_divider(hw);
1693fde0e16SJolly Shah 	const char *clk_name = clk_hw_get_name(hw);
1703fde0e16SJolly Shah 	u32 clk_id = divider->clk_id;
1713fde0e16SJolly Shah 	u32 div_type = divider->div_type;
1723fde0e16SJolly Shah 	u32 bestdiv;
1733fde0e16SJolly Shah 	int ret;
1743fde0e16SJolly Shah 	const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
1753fde0e16SJolly Shah 
1763fde0e16SJolly Shah 	/* if read only, just return current value */
1773fde0e16SJolly Shah 	if (divider->flags & CLK_DIVIDER_READ_ONLY) {
1783fde0e16SJolly Shah 		ret = eemi_ops->clock_getdivider(clk_id, &bestdiv);
1793fde0e16SJolly Shah 
1803fde0e16SJolly Shah 		if (ret)
1813fde0e16SJolly Shah 			pr_warn_once("%s() get divider failed for %s, ret = %d\n",
1823fde0e16SJolly Shah 				     __func__, clk_name, ret);
1833fde0e16SJolly Shah 		if (div_type == TYPE_DIV1)
1843fde0e16SJolly Shah 			bestdiv = bestdiv & 0xFFFF;
1853fde0e16SJolly Shah 		else
1863fde0e16SJolly Shah 			bestdiv  = bestdiv >> 16;
1873fde0e16SJolly Shah 
18834bbe036STejas Patel 		if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
18934bbe036STejas Patel 			bestdiv = 1 << bestdiv;
19034bbe036STejas Patel 
1913fde0e16SJolly Shah 		return DIV_ROUND_UP_ULL((u64)*prate, bestdiv);
1923fde0e16SJolly Shah 	}
1933fde0e16SJolly Shah 
19434bbe036STejas Patel 	bestdiv = zynqmp_divider_get_val(*prate, rate, divider->flags);
1953fde0e16SJolly Shah 
1964ebd92d2SRajan Vaja 	/*
1974ebd92d2SRajan Vaja 	 * In case of two divisors, compute best divider values and return
1984ebd92d2SRajan Vaja 	 * divider2 value based on compute value. div1 will  be automatically
1994ebd92d2SRajan Vaja 	 * set to optimum based on required total divider value.
2004ebd92d2SRajan Vaja 	 */
2014ebd92d2SRajan Vaja 	if (div_type == TYPE_DIV2 &&
2024ebd92d2SRajan Vaja 	    (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
203b8c1049cSTejas Patel 		zynqmp_get_divider2_val(hw, rate, divider, &bestdiv);
2044ebd92d2SRajan Vaja 	}
2054ebd92d2SRajan Vaja 
206c06e6440SMichael Tretter 	if ((clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) && divider->is_frac)
2073fde0e16SJolly Shah 		bestdiv = rate % *prate ? 1 : bestdiv;
2080541e021SRajan Vaja 
2090541e021SRajan Vaja 	bestdiv = min_t(u32, bestdiv, divider->max_div);
2103fde0e16SJolly Shah 	*prate = rate * bestdiv;
2113fde0e16SJolly Shah 
2123fde0e16SJolly Shah 	return rate;
2133fde0e16SJolly Shah }
2143fde0e16SJolly Shah 
2153fde0e16SJolly Shah /**
2163fde0e16SJolly Shah  * zynqmp_clk_divider_set_rate() - Set rate of divider clock
2173fde0e16SJolly Shah  * @hw:			handle between common and hardware-specific interfaces
2183fde0e16SJolly Shah  * @rate:		rate of clock to be set
2193fde0e16SJolly Shah  * @parent_rate:	rate of parent clock
2203fde0e16SJolly Shah  *
2213fde0e16SJolly Shah  * Return: 0 on success else error+reason
2223fde0e16SJolly Shah  */
2233fde0e16SJolly Shah static int zynqmp_clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
2243fde0e16SJolly Shah 				       unsigned long parent_rate)
2253fde0e16SJolly Shah {
2263fde0e16SJolly Shah 	struct zynqmp_clk_divider *divider = to_zynqmp_clk_divider(hw);
2273fde0e16SJolly Shah 	const char *clk_name = clk_hw_get_name(hw);
2283fde0e16SJolly Shah 	u32 clk_id = divider->clk_id;
2293fde0e16SJolly Shah 	u32 div_type = divider->div_type;
2303fde0e16SJolly Shah 	u32 value, div;
2313fde0e16SJolly Shah 	int ret;
2323fde0e16SJolly Shah 	const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
2333fde0e16SJolly Shah 
23434bbe036STejas Patel 	value = zynqmp_divider_get_val(parent_rate, rate, divider->flags);
2353fde0e16SJolly Shah 	if (div_type == TYPE_DIV1) {
2363fde0e16SJolly Shah 		div = value & 0xFFFF;
2373fde0e16SJolly Shah 		div |= 0xffff << 16;
2383fde0e16SJolly Shah 	} else {
2393fde0e16SJolly Shah 		div = 0xffff;
2403fde0e16SJolly Shah 		div |= value << 16;
2413fde0e16SJolly Shah 	}
2423fde0e16SJolly Shah 
24334bbe036STejas Patel 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
24434bbe036STejas Patel 		div = __ffs(div);
24534bbe036STejas Patel 
2463fde0e16SJolly Shah 	ret = eemi_ops->clock_setdivider(clk_id, div);
2473fde0e16SJolly Shah 
2483fde0e16SJolly Shah 	if (ret)
2493fde0e16SJolly Shah 		pr_warn_once("%s() set divider failed for %s, ret = %d\n",
2503fde0e16SJolly Shah 			     __func__, clk_name, ret);
2513fde0e16SJolly Shah 
2523fde0e16SJolly Shah 	return ret;
2533fde0e16SJolly Shah }
2543fde0e16SJolly Shah 
2553fde0e16SJolly Shah static const struct clk_ops zynqmp_clk_divider_ops = {
2563fde0e16SJolly Shah 	.recalc_rate = zynqmp_clk_divider_recalc_rate,
2573fde0e16SJolly Shah 	.round_rate = zynqmp_clk_divider_round_rate,
2583fde0e16SJolly Shah 	.set_rate = zynqmp_clk_divider_set_rate,
2593fde0e16SJolly Shah };
2603fde0e16SJolly Shah 
2613fde0e16SJolly Shah /**
262e942171bSRajan Vaja  * zynqmp_clk_get_max_divisor() - Get maximum supported divisor from firmware.
263e942171bSRajan Vaja  * @clk_id:		Id of clock
264e942171bSRajan Vaja  * @type:		Divider type
265e942171bSRajan Vaja  *
266e942171bSRajan Vaja  * Return: Maximum divisor of a clock if query data is successful
267e942171bSRajan Vaja  *	   U16_MAX in case of query data is not success
268e942171bSRajan Vaja  */
269*9d66e857SYueHaibing static u32 zynqmp_clk_get_max_divisor(u32 clk_id, u32 type)
270e942171bSRajan Vaja {
271e942171bSRajan Vaja 	const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
272e942171bSRajan Vaja 	struct zynqmp_pm_query_data qdata = {0};
273e942171bSRajan Vaja 	u32 ret_payload[PAYLOAD_ARG_CNT];
274e942171bSRajan Vaja 	int ret;
275e942171bSRajan Vaja 
276e942171bSRajan Vaja 	qdata.qid = PM_QID_CLOCK_GET_MAX_DIVISOR;
277e942171bSRajan Vaja 	qdata.arg1 = clk_id;
278e942171bSRajan Vaja 	qdata.arg2 = type;
279e942171bSRajan Vaja 	ret = eemi_ops->query_data(qdata, ret_payload);
280e942171bSRajan Vaja 	/*
281e942171bSRajan Vaja 	 * To maintain backward compatibility return maximum possible value
282e942171bSRajan Vaja 	 * (0xFFFF) if query for max divisor is not successful.
283e942171bSRajan Vaja 	 */
284e942171bSRajan Vaja 	if (ret)
285e942171bSRajan Vaja 		return U16_MAX;
286e942171bSRajan Vaja 
287e942171bSRajan Vaja 	return ret_payload[1];
288e942171bSRajan Vaja }
289e942171bSRajan Vaja 
290e942171bSRajan Vaja /**
2913fde0e16SJolly Shah  * zynqmp_clk_register_divider() - Register a divider clock
2923fde0e16SJolly Shah  * @name:		Name of this clock
2933fde0e16SJolly Shah  * @clk_id:		Id of clock
2943fde0e16SJolly Shah  * @parents:		Name of this clock's parents
2953fde0e16SJolly Shah  * @num_parents:	Number of parents
2963fde0e16SJolly Shah  * @nodes:		Clock topology node
2973fde0e16SJolly Shah  *
2983fde0e16SJolly Shah  * Return: clock hardware to registered clock divider
2993fde0e16SJolly Shah  */
3003fde0e16SJolly Shah struct clk_hw *zynqmp_clk_register_divider(const char *name,
3013fde0e16SJolly Shah 					   u32 clk_id,
3023fde0e16SJolly Shah 					   const char * const *parents,
3033fde0e16SJolly Shah 					   u8 num_parents,
3043fde0e16SJolly Shah 					   const struct clock_topology *nodes)
3053fde0e16SJolly Shah {
3063fde0e16SJolly Shah 	struct zynqmp_clk_divider *div;
3073fde0e16SJolly Shah 	struct clk_hw *hw;
3083fde0e16SJolly Shah 	struct clk_init_data init;
3093fde0e16SJolly Shah 	int ret;
3103fde0e16SJolly Shah 
3113fde0e16SJolly Shah 	/* allocate the divider */
3123fde0e16SJolly Shah 	div = kzalloc(sizeof(*div), GFP_KERNEL);
3133fde0e16SJolly Shah 	if (!div)
3143fde0e16SJolly Shah 		return ERR_PTR(-ENOMEM);
3153fde0e16SJolly Shah 
3163fde0e16SJolly Shah 	init.name = name;
3173fde0e16SJolly Shah 	init.ops = &zynqmp_clk_divider_ops;
318c06e6440SMichael Tretter 	/* CLK_FRAC is not defined in the common clk framework */
319c06e6440SMichael Tretter 	init.flags = nodes->flag & ~CLK_FRAC;
3203fde0e16SJolly Shah 	init.parent_names = parents;
3213fde0e16SJolly Shah 	init.num_parents = 1;
3223fde0e16SJolly Shah 
3233fde0e16SJolly Shah 	/* struct clk_divider assignments */
3242ce7e495STejas Patel 	div->is_frac = !!((nodes->flag & CLK_FRAC) |
3252ce7e495STejas Patel 			  (nodes->custom_type_flag & CUSTOM_FLAG_CLK_FRAC));
3263fde0e16SJolly Shah 	div->flags = nodes->type_flag;
3273fde0e16SJolly Shah 	div->hw.init = &init;
3283fde0e16SJolly Shah 	div->clk_id = clk_id;
3293fde0e16SJolly Shah 	div->div_type = nodes->type;
3303fde0e16SJolly Shah 
331e942171bSRajan Vaja 	/*
332e942171bSRajan Vaja 	 * To achieve best possible rate, maximum limit of divider is required
333e942171bSRajan Vaja 	 * while computation.
334e942171bSRajan Vaja 	 */
335e942171bSRajan Vaja 	div->max_div = zynqmp_clk_get_max_divisor(clk_id, nodes->type);
336e942171bSRajan Vaja 
3373fde0e16SJolly Shah 	hw = &div->hw;
3383fde0e16SJolly Shah 	ret = clk_hw_register(NULL, hw);
3393fde0e16SJolly Shah 	if (ret) {
3403fde0e16SJolly Shah 		kfree(div);
3413fde0e16SJolly Shah 		hw = ERR_PTR(ret);
3423fde0e16SJolly Shah 	}
3433fde0e16SJolly Shah 
3443fde0e16SJolly Shah 	return hw;
3453fde0e16SJolly Shah }
346