xref: /openbmc/linux/drivers/clk/ti/clkt_dflt.c (revision 5a729246)
1*5a729246SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29f37e90eSTero Kristo /*
39f37e90eSTero Kristo  * Default clock type
49f37e90eSTero Kristo  *
59f37e90eSTero Kristo  * Copyright (C) 2005-2008, 2015 Texas Instruments, Inc.
69f37e90eSTero Kristo  * Copyright (C) 2004-2010 Nokia Corporation
79f37e90eSTero Kristo  *
89f37e90eSTero Kristo  * Contacts:
99f37e90eSTero Kristo  * Richard Woodruff <r-woodruff2@ti.com>
109f37e90eSTero Kristo  * Paul Walmsley
119f37e90eSTero Kristo  * Tero Kristo <t-kristo@ti.com>
129f37e90eSTero Kristo  */
139f37e90eSTero Kristo 
149f37e90eSTero Kristo #include <linux/kernel.h>
159f37e90eSTero Kristo #include <linux/errno.h>
169f37e90eSTero Kristo #include <linux/clk-provider.h>
179f37e90eSTero Kristo #include <linux/io.h>
189f37e90eSTero Kristo #include <linux/clk/ti.h>
199f37e90eSTero Kristo #include <linux/delay.h>
209f37e90eSTero Kristo 
219f37e90eSTero Kristo #include "clock.h"
229f37e90eSTero Kristo 
239f37e90eSTero Kristo /*
249f37e90eSTero Kristo  * MAX_MODULE_ENABLE_WAIT: maximum of number of microseconds to wait
259f37e90eSTero Kristo  * for a module to indicate that it is no longer in idle
269f37e90eSTero Kristo  */
279f37e90eSTero Kristo #define MAX_MODULE_ENABLE_WAIT		100000
289f37e90eSTero Kristo 
299f37e90eSTero Kristo /*
309f37e90eSTero Kristo  * CM module register offsets, used for calculating the companion
319f37e90eSTero Kristo  * register addresses.
329f37e90eSTero Kristo  */
339f37e90eSTero Kristo #define CM_FCLKEN			0x0000
349f37e90eSTero Kristo #define CM_ICLKEN			0x0010
359f37e90eSTero Kristo 
369f37e90eSTero Kristo /**
379f37e90eSTero Kristo  * _wait_idlest_generic - wait for a module to leave the idle state
389f37e90eSTero Kristo  * @clk: module clock to wait for (needed for register offsets)
399f37e90eSTero Kristo  * @reg: virtual address of module IDLEST register
409f37e90eSTero Kristo  * @mask: value to mask against to determine if the module is active
419f37e90eSTero Kristo  * @idlest: idle state indicator (0 or 1) for the clock
429f37e90eSTero Kristo  * @name: name of the clock (for printk)
439f37e90eSTero Kristo  *
449f37e90eSTero Kristo  * Wait for a module to leave idle, where its idle-status register is
459f37e90eSTero Kristo  * not inside the CM module.  Returns 1 if the module left idle
469f37e90eSTero Kristo  * promptly, or 0 if the module did not leave idle before the timeout
479f37e90eSTero Kristo  * elapsed.  XXX Deprecated - should be moved into drivers for the
489f37e90eSTero Kristo  * individual IP block that the IDLEST register exists in.
499f37e90eSTero Kristo  */
_wait_idlest_generic(struct clk_hw_omap * clk,struct clk_omap_reg * reg,u32 mask,u8 idlest,const char * name)506c0afb50STero Kristo static int _wait_idlest_generic(struct clk_hw_omap *clk,
516c0afb50STero Kristo 				struct clk_omap_reg *reg,
529f37e90eSTero Kristo 				u32 mask, u8 idlest, const char *name)
539f37e90eSTero Kristo {
549f37e90eSTero Kristo 	int i = 0, ena = 0;
559f37e90eSTero Kristo 
569f37e90eSTero Kristo 	ena = (idlest) ? 0 : mask;
579f37e90eSTero Kristo 
589f37e90eSTero Kristo 	/* Wait until module enters enabled state */
599f37e90eSTero Kristo 	for (i = 0; i < MAX_MODULE_ENABLE_WAIT; i++) {
609f37e90eSTero Kristo 		if ((ti_clk_ll_ops->clk_readl(reg) & mask) == ena)
619f37e90eSTero Kristo 			break;
629f37e90eSTero Kristo 		udelay(1);
639f37e90eSTero Kristo 	}
649f37e90eSTero Kristo 
659f37e90eSTero Kristo 	if (i < MAX_MODULE_ENABLE_WAIT)
669f37e90eSTero Kristo 		pr_debug("omap clock: module associated with clock %s ready after %d loops\n",
679f37e90eSTero Kristo 			 name, i);
689f37e90eSTero Kristo 	else
699f37e90eSTero Kristo 		pr_err("omap clock: module associated with clock %s didn't enable in %d tries\n",
709f37e90eSTero Kristo 		       name, MAX_MODULE_ENABLE_WAIT);
719f37e90eSTero Kristo 
729f37e90eSTero Kristo 	return (i < MAX_MODULE_ENABLE_WAIT) ? 1 : 0;
739f37e90eSTero Kristo }
749f37e90eSTero Kristo 
759f37e90eSTero Kristo /**
769f37e90eSTero Kristo  * _omap2_module_wait_ready - wait for an OMAP module to leave IDLE
779f37e90eSTero Kristo  * @clk: struct clk * belonging to the module
789f37e90eSTero Kristo  *
799f37e90eSTero Kristo  * If the necessary clocks for the OMAP hardware IP block that
809f37e90eSTero Kristo  * corresponds to clock @clk are enabled, then wait for the module to
819f37e90eSTero Kristo  * indicate readiness (i.e., to leave IDLE).  This code does not
829f37e90eSTero Kristo  * belong in the clock code and will be moved in the medium term to
839f37e90eSTero Kristo  * module-dependent code.  No return value.
849f37e90eSTero Kristo  */
_omap2_module_wait_ready(struct clk_hw_omap * clk)859f37e90eSTero Kristo static void _omap2_module_wait_ready(struct clk_hw_omap *clk)
869f37e90eSTero Kristo {
876c0afb50STero Kristo 	struct clk_omap_reg companion_reg, idlest_reg;
889f37e90eSTero Kristo 	u8 other_bit, idlest_bit, idlest_val, idlest_reg_id;
899f37e90eSTero Kristo 	s16 prcm_mod;
909f37e90eSTero Kristo 	int r;
919f37e90eSTero Kristo 
929f37e90eSTero Kristo 	/* Not all modules have multiple clocks that their IDLEST depends on */
939f37e90eSTero Kristo 	if (clk->ops->find_companion) {
949f37e90eSTero Kristo 		clk->ops->find_companion(clk, &companion_reg, &other_bit);
956c0afb50STero Kristo 		if (!(ti_clk_ll_ops->clk_readl(&companion_reg) &
969f37e90eSTero Kristo 		      (1 << other_bit)))
979f37e90eSTero Kristo 			return;
989f37e90eSTero Kristo 	}
999f37e90eSTero Kristo 
1009f37e90eSTero Kristo 	clk->ops->find_idlest(clk, &idlest_reg, &idlest_bit, &idlest_val);
1016c0afb50STero Kristo 	r = ti_clk_ll_ops->cm_split_idlest_reg(&idlest_reg, &prcm_mod,
1029f37e90eSTero Kristo 					       &idlest_reg_id);
1039f37e90eSTero Kristo 	if (r) {
1049f37e90eSTero Kristo 		/* IDLEST register not in the CM module */
1056c0afb50STero Kristo 		_wait_idlest_generic(clk, &idlest_reg, (1 << idlest_bit),
106836ee0f7SStephen Boyd 				     idlest_val, clk_hw_get_name(&clk->hw));
1079f37e90eSTero Kristo 	} else {
1089f37e90eSTero Kristo 		ti_clk_ll_ops->cm_wait_module_ready(0, prcm_mod, idlest_reg_id,
1099f37e90eSTero Kristo 						    idlest_bit);
1109f37e90eSTero Kristo 	}
1119f37e90eSTero Kristo }
1129f37e90eSTero Kristo 
1139f37e90eSTero Kristo /**
1149f37e90eSTero Kristo  * omap2_clk_dflt_find_companion - find companion clock to @clk
1159f37e90eSTero Kristo  * @clk: struct clk * to find the companion clock of
1169f37e90eSTero Kristo  * @other_reg: void __iomem ** to return the companion clock CM_*CLKEN va in
1179f37e90eSTero Kristo  * @other_bit: u8 ** to return the companion clock bit shift in
1189f37e90eSTero Kristo  *
1199f37e90eSTero Kristo  * Note: We don't need special code here for INVERT_ENABLE for the
1209f37e90eSTero Kristo  * time being since INVERT_ENABLE only applies to clocks enabled by
1219f37e90eSTero Kristo  * CM_CLKEN_PLL
1229f37e90eSTero Kristo  *
1239f37e90eSTero Kristo  * Convert CM_ICLKEN* <-> CM_FCLKEN*.  This conversion assumes it's
1249f37e90eSTero Kristo  * just a matter of XORing the bits.
1259f37e90eSTero Kristo  *
1269f37e90eSTero Kristo  * Some clocks don't have companion clocks.  For example, modules with
1279f37e90eSTero Kristo  * only an interface clock (such as MAILBOXES) don't have a companion
1289f37e90eSTero Kristo  * clock.  Right now, this code relies on the hardware exporting a bit
1299f37e90eSTero Kristo  * in the correct companion register that indicates that the
1309f37e90eSTero Kristo  * nonexistent 'companion clock' is active.  Future patches will
1319f37e90eSTero Kristo  * associate this type of code with per-module data structures to
1329f37e90eSTero Kristo  * avoid this issue, and remove the casts.  No return value.
1339f37e90eSTero Kristo  */
omap2_clk_dflt_find_companion(struct clk_hw_omap * clk,struct clk_omap_reg * other_reg,u8 * other_bit)1349f37e90eSTero Kristo void omap2_clk_dflt_find_companion(struct clk_hw_omap *clk,
1356c0afb50STero Kristo 				   struct clk_omap_reg *other_reg,
1366c0afb50STero Kristo 				   u8 *other_bit)
1379f37e90eSTero Kristo {
1386c0afb50STero Kristo 	memcpy(other_reg, &clk->enable_reg, sizeof(*other_reg));
1399f37e90eSTero Kristo 
1409f37e90eSTero Kristo 	/*
1419f37e90eSTero Kristo 	 * Convert CM_ICLKEN* <-> CM_FCLKEN*.  This conversion assumes
1429f37e90eSTero Kristo 	 * it's just a matter of XORing the bits.
1439f37e90eSTero Kristo 	 */
1446c0afb50STero Kristo 	other_reg->offset ^= (CM_FCLKEN ^ CM_ICLKEN);
1459f37e90eSTero Kristo 
1469f37e90eSTero Kristo 	*other_bit = clk->enable_bit;
1479f37e90eSTero Kristo }
1489f37e90eSTero Kristo 
1499f37e90eSTero Kristo /**
1509f37e90eSTero Kristo  * omap2_clk_dflt_find_idlest - find CM_IDLEST reg va, bit shift for @clk
1519f37e90eSTero Kristo  * @clk: struct clk * to find IDLEST info for
1529f37e90eSTero Kristo  * @idlest_reg: void __iomem ** to return the CM_IDLEST va in
1539f37e90eSTero Kristo  * @idlest_bit: u8 * to return the CM_IDLEST bit shift in
1549f37e90eSTero Kristo  * @idlest_val: u8 * to return the idle status indicator
1559f37e90eSTero Kristo  *
1569f37e90eSTero Kristo  * Return the CM_IDLEST register address and bit shift corresponding
1579f37e90eSTero Kristo  * to the module that "owns" this clock.  This default code assumes
1589f37e90eSTero Kristo  * that the CM_IDLEST bit shift is the CM_*CLKEN bit shift, and that
1599f37e90eSTero Kristo  * the IDLEST register address ID corresponds to the CM_*CLKEN
1609f37e90eSTero Kristo  * register address ID (e.g., that CM_FCLKEN2 corresponds to
1619f37e90eSTero Kristo  * CM_IDLEST2).  This is not true for all modules.  No return value.
1629f37e90eSTero Kristo  */
omap2_clk_dflt_find_idlest(struct clk_hw_omap * clk,struct clk_omap_reg * idlest_reg,u8 * idlest_bit,u8 * idlest_val)1639f37e90eSTero Kristo void omap2_clk_dflt_find_idlest(struct clk_hw_omap *clk,
1646c0afb50STero Kristo 				struct clk_omap_reg *idlest_reg, u8 *idlest_bit,
1659f37e90eSTero Kristo 				u8 *idlest_val)
1669f37e90eSTero Kristo {
1676c0afb50STero Kristo 	memcpy(idlest_reg, &clk->enable_reg, sizeof(*idlest_reg));
1689f37e90eSTero Kristo 
1696c0afb50STero Kristo 	idlest_reg->offset &= ~0xf0;
1706c0afb50STero Kristo 	idlest_reg->offset |= 0x20;
1716c0afb50STero Kristo 
1729f37e90eSTero Kristo 	*idlest_bit = clk->enable_bit;
1739f37e90eSTero Kristo 
1749f37e90eSTero Kristo 	/*
1759f37e90eSTero Kristo 	 * 24xx uses 0 to indicate not ready, and 1 to indicate ready.
1769f37e90eSTero Kristo 	 * 34xx reverses this, just to keep us on our toes
1779f37e90eSTero Kristo 	 * AM35xx uses both, depending on the module.
1789f37e90eSTero Kristo 	 */
1799f37e90eSTero Kristo 	*idlest_val = ti_clk_get_features()->cm_idlest_val;
1809f37e90eSTero Kristo }
1819f37e90eSTero Kristo 
1829f37e90eSTero Kristo /**
1839f37e90eSTero Kristo  * omap2_dflt_clk_enable - enable a clock in the hardware
1849f37e90eSTero Kristo  * @hw: struct clk_hw * of the clock to enable
1859f37e90eSTero Kristo  *
1869f37e90eSTero Kristo  * Enable the clock @hw in the hardware.  We first call into the OMAP
1879f37e90eSTero Kristo  * clockdomain code to "enable" the corresponding clockdomain if this
1889f37e90eSTero Kristo  * is the first enabled user of the clockdomain.  Then program the
1899f37e90eSTero Kristo  * hardware to enable the clock.  Then wait for the IP block that uses
1909f37e90eSTero Kristo  * this clock to leave idle (if applicable).  Returns the error value
1919f37e90eSTero Kristo  * from clkdm_clk_enable() if it terminated with an error, or -EINVAL
1929f37e90eSTero Kristo  * if @hw has a null clock enable_reg, or zero upon success.
1939f37e90eSTero Kristo  */
omap2_dflt_clk_enable(struct clk_hw * hw)1949f37e90eSTero Kristo int omap2_dflt_clk_enable(struct clk_hw *hw)
1959f37e90eSTero Kristo {
1969f37e90eSTero Kristo 	struct clk_hw_omap *clk;
1979f37e90eSTero Kristo 	u32 v;
1989f37e90eSTero Kristo 	int ret = 0;
1999f37e90eSTero Kristo 	bool clkdm_control;
2009f37e90eSTero Kristo 
2019f37e90eSTero Kristo 	if (ti_clk_get_features()->flags & TI_CLK_DISABLE_CLKDM_CONTROL)
2029f37e90eSTero Kristo 		clkdm_control = false;
2039f37e90eSTero Kristo 	else
2049f37e90eSTero Kristo 		clkdm_control = true;
2059f37e90eSTero Kristo 
2069f37e90eSTero Kristo 	clk = to_clk_hw_omap(hw);
2079f37e90eSTero Kristo 
2089f37e90eSTero Kristo 	if (clkdm_control && clk->clkdm) {
2099f37e90eSTero Kristo 		ret = ti_clk_ll_ops->clkdm_clk_enable(clk->clkdm, hw->clk);
2109f37e90eSTero Kristo 		if (ret) {
2119f37e90eSTero Kristo 			WARN(1,
2129f37e90eSTero Kristo 			     "%s: could not enable %s's clockdomain %s: %d\n",
213836ee0f7SStephen Boyd 			     __func__, clk_hw_get_name(hw),
2149f37e90eSTero Kristo 			     clk->clkdm_name, ret);
2159f37e90eSTero Kristo 			return ret;
2169f37e90eSTero Kristo 		}
2179f37e90eSTero Kristo 	}
2189f37e90eSTero Kristo 
2199f37e90eSTero Kristo 	/* FIXME should not have INVERT_ENABLE bit here */
2206c0afb50STero Kristo 	v = ti_clk_ll_ops->clk_readl(&clk->enable_reg);
2219f37e90eSTero Kristo 	if (clk->flags & INVERT_ENABLE)
2229f37e90eSTero Kristo 		v &= ~(1 << clk->enable_bit);
2239f37e90eSTero Kristo 	else
2249f37e90eSTero Kristo 		v |= (1 << clk->enable_bit);
2256c0afb50STero Kristo 	ti_clk_ll_ops->clk_writel(v, &clk->enable_reg);
2266c0afb50STero Kristo 	v = ti_clk_ll_ops->clk_readl(&clk->enable_reg); /* OCP barrier */
2279f37e90eSTero Kristo 
2289f37e90eSTero Kristo 	if (clk->ops && clk->ops->find_idlest)
2299f37e90eSTero Kristo 		_omap2_module_wait_ready(clk);
2309f37e90eSTero Kristo 
2319f37e90eSTero Kristo 	return 0;
2329f37e90eSTero Kristo }
2339f37e90eSTero Kristo 
2349f37e90eSTero Kristo /**
2359f37e90eSTero Kristo  * omap2_dflt_clk_disable - disable a clock in the hardware
2369f37e90eSTero Kristo  * @hw: struct clk_hw * of the clock to disable
2379f37e90eSTero Kristo  *
2389f37e90eSTero Kristo  * Disable the clock @hw in the hardware, and call into the OMAP
2399f37e90eSTero Kristo  * clockdomain code to "disable" the corresponding clockdomain if all
2409f37e90eSTero Kristo  * clocks/hwmods in that clockdomain are now disabled.  No return
2419f37e90eSTero Kristo  * value.
2429f37e90eSTero Kristo  */
omap2_dflt_clk_disable(struct clk_hw * hw)2439f37e90eSTero Kristo void omap2_dflt_clk_disable(struct clk_hw *hw)
2449f37e90eSTero Kristo {
2459f37e90eSTero Kristo 	struct clk_hw_omap *clk;
2469f37e90eSTero Kristo 	u32 v;
2479f37e90eSTero Kristo 
2489f37e90eSTero Kristo 	clk = to_clk_hw_omap(hw);
2499f37e90eSTero Kristo 
2506c0afb50STero Kristo 	v = ti_clk_ll_ops->clk_readl(&clk->enable_reg);
2519f37e90eSTero Kristo 	if (clk->flags & INVERT_ENABLE)
2529f37e90eSTero Kristo 		v |= (1 << clk->enable_bit);
2539f37e90eSTero Kristo 	else
2549f37e90eSTero Kristo 		v &= ~(1 << clk->enable_bit);
2556c0afb50STero Kristo 	ti_clk_ll_ops->clk_writel(v, &clk->enable_reg);
2569f37e90eSTero Kristo 	/* No OCP barrier needed here since it is a disable operation */
2579f37e90eSTero Kristo 
2589f37e90eSTero Kristo 	if (!(ti_clk_get_features()->flags & TI_CLK_DISABLE_CLKDM_CONTROL) &&
2599f37e90eSTero Kristo 	    clk->clkdm)
2609f37e90eSTero Kristo 		ti_clk_ll_ops->clkdm_clk_disable(clk->clkdm, hw->clk);
2619f37e90eSTero Kristo }
2629f37e90eSTero Kristo 
2639f37e90eSTero Kristo /**
2649f37e90eSTero Kristo  * omap2_dflt_clk_is_enabled - is clock enabled in the hardware?
2659f37e90eSTero Kristo  * @hw: struct clk_hw * to check
2669f37e90eSTero Kristo  *
2679f37e90eSTero Kristo  * Return 1 if the clock represented by @hw is enabled in the
2689f37e90eSTero Kristo  * hardware, or 0 otherwise.  Intended for use in the struct
2699f37e90eSTero Kristo  * clk_ops.is_enabled function pointer.
2709f37e90eSTero Kristo  */
omap2_dflt_clk_is_enabled(struct clk_hw * hw)2719f37e90eSTero Kristo int omap2_dflt_clk_is_enabled(struct clk_hw *hw)
2729f37e90eSTero Kristo {
2739f37e90eSTero Kristo 	struct clk_hw_omap *clk = to_clk_hw_omap(hw);
2749f37e90eSTero Kristo 	u32 v;
2759f37e90eSTero Kristo 
2766c0afb50STero Kristo 	v = ti_clk_ll_ops->clk_readl(&clk->enable_reg);
2779f37e90eSTero Kristo 
2789f37e90eSTero Kristo 	if (clk->flags & INVERT_ENABLE)
2799f37e90eSTero Kristo 		v ^= BIT(clk->enable_bit);
2809f37e90eSTero Kristo 
2819f37e90eSTero Kristo 	v &= BIT(clk->enable_bit);
2829f37e90eSTero Kristo 
2839f37e90eSTero Kristo 	return v ? 1 : 0;
2849f37e90eSTero Kristo }
2859f37e90eSTero Kristo 
2869f37e90eSTero Kristo const struct clk_hw_omap_ops clkhwops_wait = {
2879f37e90eSTero Kristo 	.find_idlest	= omap2_clk_dflt_find_idlest,
2889f37e90eSTero Kristo 	.find_companion	= omap2_clk_dflt_find_companion,
2899f37e90eSTero Kristo };
290