xref: /openbmc/linux/drivers/clk/tegra/clk-periph-gate.c (revision 5a88b0d10f198ddd5f988f40d34b52f34c87a5c6)
18f8f484bSPrashant Gaikwad /*
28f8f484bSPrashant Gaikwad  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
38f8f484bSPrashant Gaikwad  *
48f8f484bSPrashant Gaikwad  * This program is free software; you can redistribute it and/or modify it
58f8f484bSPrashant Gaikwad  * under the terms and conditions of the GNU General Public License,
68f8f484bSPrashant Gaikwad  * version 2, as published by the Free Software Foundation.
78f8f484bSPrashant Gaikwad  *
88f8f484bSPrashant Gaikwad  * This program is distributed in the hope it will be useful, but WITHOUT
98f8f484bSPrashant Gaikwad  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
108f8f484bSPrashant Gaikwad  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
118f8f484bSPrashant Gaikwad  * more details.
128f8f484bSPrashant Gaikwad  *
138f8f484bSPrashant Gaikwad  * You should have received a copy of the GNU General Public License
148f8f484bSPrashant Gaikwad  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
158f8f484bSPrashant Gaikwad  */
168f8f484bSPrashant Gaikwad 
178f8f484bSPrashant Gaikwad #include <linux/clk.h>
188f8f484bSPrashant Gaikwad #include <linux/clk-provider.h>
198f8f484bSPrashant Gaikwad #include <linux/slab.h>
208f8f484bSPrashant Gaikwad #include <linux/io.h>
218f8f484bSPrashant Gaikwad #include <linux/delay.h>
228f8f484bSPrashant Gaikwad #include <linux/err.h>
238f8f484bSPrashant Gaikwad #include <linux/tegra-soc.h>
248f8f484bSPrashant Gaikwad 
258f8f484bSPrashant Gaikwad #include "clk.h"
268f8f484bSPrashant Gaikwad 
278f8f484bSPrashant Gaikwad static DEFINE_SPINLOCK(periph_ref_lock);
288f8f484bSPrashant Gaikwad 
298f8f484bSPrashant Gaikwad /* Macros to assist peripheral gate clock */
308f8f484bSPrashant Gaikwad #define read_enb(gate) \
318f8f484bSPrashant Gaikwad 	readl_relaxed(gate->clk_base + (gate->regs->enb_reg))
328f8f484bSPrashant Gaikwad #define write_enb_set(val, gate) \
338f8f484bSPrashant Gaikwad 	writel_relaxed(val, gate->clk_base + (gate->regs->enb_set_reg))
348f8f484bSPrashant Gaikwad #define write_enb_clr(val, gate) \
358f8f484bSPrashant Gaikwad 	writel_relaxed(val, gate->clk_base + (gate->regs->enb_clr_reg))
368f8f484bSPrashant Gaikwad 
378f8f484bSPrashant Gaikwad #define read_rst(gate) \
388f8f484bSPrashant Gaikwad 	readl_relaxed(gate->clk_base + (gate->regs->rst_reg))
398f8f484bSPrashant Gaikwad #define write_rst_set(val, gate) \
408f8f484bSPrashant Gaikwad 	writel_relaxed(val, gate->clk_base + (gate->regs->rst_set_reg))
418f8f484bSPrashant Gaikwad #define write_rst_clr(val, gate) \
428f8f484bSPrashant Gaikwad 	writel_relaxed(val, gate->clk_base + (gate->regs->rst_clr_reg))
438f8f484bSPrashant Gaikwad 
44*5a88b0d1SYen Lin #define periph_clk_to_bit(gate) (1 << (gate->clk_num % 32))
458f8f484bSPrashant Gaikwad 
468f8f484bSPrashant Gaikwad /* Peripheral gate clock ops */
478f8f484bSPrashant Gaikwad static int clk_periph_is_enabled(struct clk_hw *hw)
488f8f484bSPrashant Gaikwad {
498f8f484bSPrashant Gaikwad 	struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
508f8f484bSPrashant Gaikwad 	int state = 1;
518f8f484bSPrashant Gaikwad 
528f8f484bSPrashant Gaikwad 	if (!(read_enb(gate) & periph_clk_to_bit(gate)))
538f8f484bSPrashant Gaikwad 		state = 0;
548f8f484bSPrashant Gaikwad 
558f8f484bSPrashant Gaikwad 	if (!(gate->flags & TEGRA_PERIPH_NO_RESET))
568f8f484bSPrashant Gaikwad 		if (read_rst(gate) & periph_clk_to_bit(gate))
578f8f484bSPrashant Gaikwad 			state = 0;
588f8f484bSPrashant Gaikwad 
598f8f484bSPrashant Gaikwad 	return state;
608f8f484bSPrashant Gaikwad }
618f8f484bSPrashant Gaikwad 
628f8f484bSPrashant Gaikwad static int clk_periph_enable(struct clk_hw *hw)
638f8f484bSPrashant Gaikwad {
648f8f484bSPrashant Gaikwad 	struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
658f8f484bSPrashant Gaikwad 	unsigned long flags = 0;
668f8f484bSPrashant Gaikwad 
678f8f484bSPrashant Gaikwad 	spin_lock_irqsave(&periph_ref_lock, flags);
688f8f484bSPrashant Gaikwad 
698f8f484bSPrashant Gaikwad 	gate->enable_refcnt[gate->clk_num]++;
708f8f484bSPrashant Gaikwad 	if (gate->enable_refcnt[gate->clk_num] > 1) {
718f8f484bSPrashant Gaikwad 		spin_unlock_irqrestore(&periph_ref_lock, flags);
728f8f484bSPrashant Gaikwad 		return 0;
738f8f484bSPrashant Gaikwad 	}
748f8f484bSPrashant Gaikwad 
758f8f484bSPrashant Gaikwad 	write_enb_set(periph_clk_to_bit(gate), gate);
768f8f484bSPrashant Gaikwad 	udelay(2);
778f8f484bSPrashant Gaikwad 
788f8f484bSPrashant Gaikwad 	if (!(gate->flags & TEGRA_PERIPH_NO_RESET) &&
798f8f484bSPrashant Gaikwad 	    !(gate->flags & TEGRA_PERIPH_MANUAL_RESET)) {
808f8f484bSPrashant Gaikwad 		if (read_rst(gate) & periph_clk_to_bit(gate)) {
818f8f484bSPrashant Gaikwad 			udelay(5); /* reset propogation delay */
828f8f484bSPrashant Gaikwad 			write_rst_clr(periph_clk_to_bit(gate), gate);
838f8f484bSPrashant Gaikwad 		}
848f8f484bSPrashant Gaikwad 	}
858f8f484bSPrashant Gaikwad 
868f8f484bSPrashant Gaikwad 	spin_unlock_irqrestore(&periph_ref_lock, flags);
878f8f484bSPrashant Gaikwad 
888f8f484bSPrashant Gaikwad 	return 0;
898f8f484bSPrashant Gaikwad }
908f8f484bSPrashant Gaikwad 
918f8f484bSPrashant Gaikwad static void clk_periph_disable(struct clk_hw *hw)
928f8f484bSPrashant Gaikwad {
938f8f484bSPrashant Gaikwad 	struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
948f8f484bSPrashant Gaikwad 	unsigned long flags = 0;
958f8f484bSPrashant Gaikwad 
968f8f484bSPrashant Gaikwad 	spin_lock_irqsave(&periph_ref_lock, flags);
978f8f484bSPrashant Gaikwad 
988f8f484bSPrashant Gaikwad 	gate->enable_refcnt[gate->clk_num]--;
998f8f484bSPrashant Gaikwad 	if (gate->enable_refcnt[gate->clk_num] > 0) {
1008f8f484bSPrashant Gaikwad 		spin_unlock_irqrestore(&periph_ref_lock, flags);
1018f8f484bSPrashant Gaikwad 		return;
1028f8f484bSPrashant Gaikwad 	}
1038f8f484bSPrashant Gaikwad 
1048f8f484bSPrashant Gaikwad 	/*
1058f8f484bSPrashant Gaikwad 	 * If peripheral is in the APB bus then read the APB bus to
1068f8f484bSPrashant Gaikwad 	 * flush the write operation in apb bus. This will avoid the
1078f8f484bSPrashant Gaikwad 	 * peripheral access after disabling clock
1088f8f484bSPrashant Gaikwad 	 */
1098f8f484bSPrashant Gaikwad 	if (gate->flags & TEGRA_PERIPH_ON_APB)
1108f8f484bSPrashant Gaikwad 		tegra_read_chipid();
1118f8f484bSPrashant Gaikwad 
1128f8f484bSPrashant Gaikwad 	write_enb_clr(periph_clk_to_bit(gate), gate);
1138f8f484bSPrashant Gaikwad 
1148f8f484bSPrashant Gaikwad 	spin_unlock_irqrestore(&periph_ref_lock, flags);
1158f8f484bSPrashant Gaikwad }
1168f8f484bSPrashant Gaikwad 
1178f8f484bSPrashant Gaikwad void tegra_periph_reset(struct tegra_clk_periph_gate *gate, bool assert)
1188f8f484bSPrashant Gaikwad {
1198f8f484bSPrashant Gaikwad 	if (gate->flags & TEGRA_PERIPH_NO_RESET)
1208f8f484bSPrashant Gaikwad 		return;
1218f8f484bSPrashant Gaikwad 
1228f8f484bSPrashant Gaikwad 	if (assert) {
1238f8f484bSPrashant Gaikwad 		/*
1248f8f484bSPrashant Gaikwad 		 * If peripheral is in the APB bus then read the APB bus to
1258f8f484bSPrashant Gaikwad 		 * flush the write operation in apb bus. This will avoid the
1268f8f484bSPrashant Gaikwad 		 * peripheral access after disabling clock
1278f8f484bSPrashant Gaikwad 		 */
1288f8f484bSPrashant Gaikwad 		if (gate->flags & TEGRA_PERIPH_ON_APB)
1298f8f484bSPrashant Gaikwad 			tegra_read_chipid();
1308f8f484bSPrashant Gaikwad 
1318f8f484bSPrashant Gaikwad 		write_rst_set(periph_clk_to_bit(gate), gate);
1328f8f484bSPrashant Gaikwad 	} else {
1338f8f484bSPrashant Gaikwad 		write_rst_clr(periph_clk_to_bit(gate), gate);
1348f8f484bSPrashant Gaikwad 	}
1358f8f484bSPrashant Gaikwad }
1368f8f484bSPrashant Gaikwad 
1378f8f484bSPrashant Gaikwad const struct clk_ops tegra_clk_periph_gate_ops = {
1388f8f484bSPrashant Gaikwad 	.is_enabled = clk_periph_is_enabled,
1398f8f484bSPrashant Gaikwad 	.enable = clk_periph_enable,
1408f8f484bSPrashant Gaikwad 	.disable = clk_periph_disable,
1418f8f484bSPrashant Gaikwad };
1428f8f484bSPrashant Gaikwad 
1438f8f484bSPrashant Gaikwad struct clk *tegra_clk_register_periph_gate(const char *name,
1448f8f484bSPrashant Gaikwad 		const char *parent_name, u8 gate_flags, void __iomem *clk_base,
1458f8f484bSPrashant Gaikwad 		unsigned long flags, int clk_num,
1468f8f484bSPrashant Gaikwad 		struct tegra_clk_periph_regs *pregs, int *enable_refcnt)
1478f8f484bSPrashant Gaikwad {
1488f8f484bSPrashant Gaikwad 	struct tegra_clk_periph_gate *gate;
1498f8f484bSPrashant Gaikwad 	struct clk *clk;
1508f8f484bSPrashant Gaikwad 	struct clk_init_data init;
1518f8f484bSPrashant Gaikwad 
1528f8f484bSPrashant Gaikwad 	gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1538f8f484bSPrashant Gaikwad 	if (!gate) {
1548f8f484bSPrashant Gaikwad 		pr_err("%s: could not allocate periph gate clk\n", __func__);
1558f8f484bSPrashant Gaikwad 		return ERR_PTR(-ENOMEM);
1568f8f484bSPrashant Gaikwad 	}
1578f8f484bSPrashant Gaikwad 
1588f8f484bSPrashant Gaikwad 	init.name = name;
1598f8f484bSPrashant Gaikwad 	init.flags = flags;
1608f8f484bSPrashant Gaikwad 	init.parent_names = parent_name ? &parent_name : NULL;
1618f8f484bSPrashant Gaikwad 	init.num_parents = parent_name ? 1 : 0;
1628f8f484bSPrashant Gaikwad 	init.ops = &tegra_clk_periph_gate_ops;
1638f8f484bSPrashant Gaikwad 
1648f8f484bSPrashant Gaikwad 	gate->magic = TEGRA_CLK_PERIPH_GATE_MAGIC;
1658f8f484bSPrashant Gaikwad 	gate->clk_base = clk_base;
1668f8f484bSPrashant Gaikwad 	gate->clk_num = clk_num;
1678f8f484bSPrashant Gaikwad 	gate->flags = gate_flags;
1688f8f484bSPrashant Gaikwad 	gate->enable_refcnt = enable_refcnt;
1698f8f484bSPrashant Gaikwad 	gate->regs = pregs;
1708f8f484bSPrashant Gaikwad 
1718f8f484bSPrashant Gaikwad 	/* Data in .init is copied by clk_register(), so stack variable OK */
1728f8f484bSPrashant Gaikwad 	gate->hw.init = &init;
1738f8f484bSPrashant Gaikwad 
1748f8f484bSPrashant Gaikwad 	clk = clk_register(NULL, &gate->hw);
1758f8f484bSPrashant Gaikwad 	if (IS_ERR(clk))
1768f8f484bSPrashant Gaikwad 		kfree(gate);
1778f8f484bSPrashant Gaikwad 
1788f8f484bSPrashant Gaikwad 	return clk;
1798f8f484bSPrashant Gaikwad }
180