xref: /openbmc/linux/drivers/clk/tegra/clk-periph-gate.c (revision 7e14f22305b09bd57fc2da3bf2fd3bfd53dc285b)
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-provider.h>
188f8f484bSPrashant Gaikwad #include <linux/slab.h>
198f8f484bSPrashant Gaikwad #include <linux/io.h>
208f8f484bSPrashant Gaikwad #include <linux/delay.h>
218f8f484bSPrashant Gaikwad #include <linux/err.h>
22306a7f91SThierry Reding 
23306a7f91SThierry Reding #include <soc/tegra/fuse.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_clr(val, gate) \
408f8f484bSPrashant Gaikwad 	writel_relaxed(val, gate->clk_base + (gate->regs->rst_clr_reg))
418f8f484bSPrashant Gaikwad 
425a88b0d1SYen Lin #define periph_clk_to_bit(gate) (1 << (gate->clk_num % 32))
438f8f484bSPrashant Gaikwad 
44fdcccbd8SPeter De Schrijver #define LVL2_CLK_GATE_OVRE 0x554
45fdcccbd8SPeter De Schrijver 
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 
86fdcccbd8SPeter De Schrijver 	if (gate->flags & TEGRA_PERIPH_WAR_1005168) {
87fdcccbd8SPeter De Schrijver 		writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE);
88fdcccbd8SPeter De Schrijver 		writel_relaxed(BIT(22), gate->clk_base + LVL2_CLK_GATE_OVRE);
89fdcccbd8SPeter De Schrijver 		udelay(1);
90fdcccbd8SPeter De Schrijver 		writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE);
91fdcccbd8SPeter De Schrijver 	}
92fdcccbd8SPeter De Schrijver 
938f8f484bSPrashant Gaikwad 	spin_unlock_irqrestore(&periph_ref_lock, flags);
948f8f484bSPrashant Gaikwad 
958f8f484bSPrashant Gaikwad 	return 0;
968f8f484bSPrashant Gaikwad }
978f8f484bSPrashant Gaikwad 
988f8f484bSPrashant Gaikwad static void clk_periph_disable(struct clk_hw *hw)
998f8f484bSPrashant Gaikwad {
1008f8f484bSPrashant Gaikwad 	struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
1018f8f484bSPrashant Gaikwad 	unsigned long flags = 0;
1028f8f484bSPrashant Gaikwad 
1038f8f484bSPrashant Gaikwad 	spin_lock_irqsave(&periph_ref_lock, flags);
1048f8f484bSPrashant Gaikwad 
1058f8f484bSPrashant Gaikwad 	gate->enable_refcnt[gate->clk_num]--;
1068f8f484bSPrashant Gaikwad 	if (gate->enable_refcnt[gate->clk_num] > 0) {
1078f8f484bSPrashant Gaikwad 		spin_unlock_irqrestore(&periph_ref_lock, flags);
1088f8f484bSPrashant Gaikwad 		return;
1098f8f484bSPrashant Gaikwad 	}
1108f8f484bSPrashant Gaikwad 
1118f8f484bSPrashant Gaikwad 	/*
1128f8f484bSPrashant Gaikwad 	 * If peripheral is in the APB bus then read the APB bus to
1138f8f484bSPrashant Gaikwad 	 * flush the write operation in apb bus. This will avoid the
1148f8f484bSPrashant Gaikwad 	 * peripheral access after disabling clock
1158f8f484bSPrashant Gaikwad 	 */
1168f8f484bSPrashant Gaikwad 	if (gate->flags & TEGRA_PERIPH_ON_APB)
1178f8f484bSPrashant Gaikwad 		tegra_read_chipid();
1188f8f484bSPrashant Gaikwad 
1198f8f484bSPrashant Gaikwad 	write_enb_clr(periph_clk_to_bit(gate), gate);
1208f8f484bSPrashant Gaikwad 
1218f8f484bSPrashant Gaikwad 	spin_unlock_irqrestore(&periph_ref_lock, flags);
1228f8f484bSPrashant Gaikwad }
1238f8f484bSPrashant Gaikwad 
1248f8f484bSPrashant Gaikwad const struct clk_ops tegra_clk_periph_gate_ops = {
1258f8f484bSPrashant Gaikwad 	.is_enabled = clk_periph_is_enabled,
1268f8f484bSPrashant Gaikwad 	.enable = clk_periph_enable,
1278f8f484bSPrashant Gaikwad 	.disable = clk_periph_disable,
1288f8f484bSPrashant Gaikwad };
1298f8f484bSPrashant Gaikwad 
1308f8f484bSPrashant Gaikwad struct clk *tegra_clk_register_periph_gate(const char *name,
1318f8f484bSPrashant Gaikwad 		const char *parent_name, u8 gate_flags, void __iomem *clk_base,
132d5ff89a8SPeter De Schrijver 		unsigned long flags, int clk_num, int *enable_refcnt)
1338f8f484bSPrashant Gaikwad {
1348f8f484bSPrashant Gaikwad 	struct tegra_clk_periph_gate *gate;
1358f8f484bSPrashant Gaikwad 	struct clk *clk;
1368f8f484bSPrashant Gaikwad 	struct clk_init_data init;
137*7e14f223SThierry Reding 	const struct tegra_clk_periph_regs *pregs;
138d5ff89a8SPeter De Schrijver 
139d5ff89a8SPeter De Schrijver 	pregs = get_reg_bank(clk_num);
140d5ff89a8SPeter De Schrijver 	if (!pregs)
141d5ff89a8SPeter De Schrijver 		return ERR_PTR(-EINVAL);
1428f8f484bSPrashant Gaikwad 
1438f8f484bSPrashant Gaikwad 	gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1448f8f484bSPrashant Gaikwad 	if (!gate) {
1458f8f484bSPrashant Gaikwad 		pr_err("%s: could not allocate periph gate clk\n", __func__);
1468f8f484bSPrashant Gaikwad 		return ERR_PTR(-ENOMEM);
1478f8f484bSPrashant Gaikwad 	}
1488f8f484bSPrashant Gaikwad 
1498f8f484bSPrashant Gaikwad 	init.name = name;
1508f8f484bSPrashant Gaikwad 	init.flags = flags;
1518f8f484bSPrashant Gaikwad 	init.parent_names = parent_name ? &parent_name : NULL;
1528f8f484bSPrashant Gaikwad 	init.num_parents = parent_name ? 1 : 0;
1538f8f484bSPrashant Gaikwad 	init.ops = &tegra_clk_periph_gate_ops;
1548f8f484bSPrashant Gaikwad 
1558f8f484bSPrashant Gaikwad 	gate->magic = TEGRA_CLK_PERIPH_GATE_MAGIC;
1568f8f484bSPrashant Gaikwad 	gate->clk_base = clk_base;
1578f8f484bSPrashant Gaikwad 	gate->clk_num = clk_num;
1588f8f484bSPrashant Gaikwad 	gate->flags = gate_flags;
1598f8f484bSPrashant Gaikwad 	gate->enable_refcnt = enable_refcnt;
1608f8f484bSPrashant Gaikwad 	gate->regs = pregs;
1618f8f484bSPrashant Gaikwad 
1628f8f484bSPrashant Gaikwad 	/* Data in .init is copied by clk_register(), so stack variable OK */
1638f8f484bSPrashant Gaikwad 	gate->hw.init = &init;
1648f8f484bSPrashant Gaikwad 
1658f8f484bSPrashant Gaikwad 	clk = clk_register(NULL, &gate->hw);
1668f8f484bSPrashant Gaikwad 	if (IS_ERR(clk))
1678f8f484bSPrashant Gaikwad 		kfree(gate);
1688f8f484bSPrashant Gaikwad 
1698f8f484bSPrashant Gaikwad 	return clk;
1708f8f484bSPrashant Gaikwad }
171