xref: /openbmc/linux/drivers/clk/tegra/clk-periph-gate.c (revision 9952f6918daa4ab5fc81307a9f90e31a4df3b200)
1*9952f691SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28f8f484bSPrashant Gaikwad /*
38f8f484bSPrashant Gaikwad  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
48f8f484bSPrashant Gaikwad  */
58f8f484bSPrashant Gaikwad 
68f8f484bSPrashant Gaikwad #include <linux/clk-provider.h>
78f8f484bSPrashant Gaikwad #include <linux/slab.h>
88f8f484bSPrashant Gaikwad #include <linux/io.h>
98f8f484bSPrashant Gaikwad #include <linux/delay.h>
108f8f484bSPrashant Gaikwad #include <linux/err.h>
11306a7f91SThierry Reding 
12306a7f91SThierry Reding #include <soc/tegra/fuse.h>
138f8f484bSPrashant Gaikwad 
148f8f484bSPrashant Gaikwad #include "clk.h"
158f8f484bSPrashant Gaikwad 
168f8f484bSPrashant Gaikwad static DEFINE_SPINLOCK(periph_ref_lock);
178f8f484bSPrashant Gaikwad 
188f8f484bSPrashant Gaikwad /* Macros to assist peripheral gate clock */
198f8f484bSPrashant Gaikwad #define read_enb(gate) \
208f8f484bSPrashant Gaikwad 	readl_relaxed(gate->clk_base + (gate->regs->enb_reg))
218f8f484bSPrashant Gaikwad #define write_enb_set(val, gate) \
228f8f484bSPrashant Gaikwad 	writel_relaxed(val, gate->clk_base + (gate->regs->enb_set_reg))
238f8f484bSPrashant Gaikwad #define write_enb_clr(val, gate) \
248f8f484bSPrashant Gaikwad 	writel_relaxed(val, gate->clk_base + (gate->regs->enb_clr_reg))
258f8f484bSPrashant Gaikwad 
268f8f484bSPrashant Gaikwad #define read_rst(gate) \
278f8f484bSPrashant Gaikwad 	readl_relaxed(gate->clk_base + (gate->regs->rst_reg))
288f8f484bSPrashant Gaikwad #define write_rst_clr(val, gate) \
298f8f484bSPrashant Gaikwad 	writel_relaxed(val, gate->clk_base + (gate->regs->rst_clr_reg))
308f8f484bSPrashant Gaikwad 
315a88b0d1SYen Lin #define periph_clk_to_bit(gate) (1 << (gate->clk_num % 32))
328f8f484bSPrashant Gaikwad 
33fdcccbd8SPeter De Schrijver #define LVL2_CLK_GATE_OVRE 0x554
34fdcccbd8SPeter De Schrijver 
358f8f484bSPrashant Gaikwad /* Peripheral gate clock ops */
368f8f484bSPrashant Gaikwad static int clk_periph_is_enabled(struct clk_hw *hw)
378f8f484bSPrashant Gaikwad {
388f8f484bSPrashant Gaikwad 	struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
398f8f484bSPrashant Gaikwad 	int state = 1;
408f8f484bSPrashant Gaikwad 
418f8f484bSPrashant Gaikwad 	if (!(read_enb(gate) & periph_clk_to_bit(gate)))
428f8f484bSPrashant Gaikwad 		state = 0;
438f8f484bSPrashant Gaikwad 
448f8f484bSPrashant Gaikwad 	if (!(gate->flags & TEGRA_PERIPH_NO_RESET))
458f8f484bSPrashant Gaikwad 		if (read_rst(gate) & periph_clk_to_bit(gate))
468f8f484bSPrashant Gaikwad 			state = 0;
478f8f484bSPrashant Gaikwad 
488f8f484bSPrashant Gaikwad 	return state;
498f8f484bSPrashant Gaikwad }
508f8f484bSPrashant Gaikwad 
518f8f484bSPrashant Gaikwad static int clk_periph_enable(struct clk_hw *hw)
528f8f484bSPrashant Gaikwad {
538f8f484bSPrashant Gaikwad 	struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
548f8f484bSPrashant Gaikwad 	unsigned long flags = 0;
558f8f484bSPrashant Gaikwad 
568f8f484bSPrashant Gaikwad 	spin_lock_irqsave(&periph_ref_lock, flags);
578f8f484bSPrashant Gaikwad 
588f8f484bSPrashant Gaikwad 	gate->enable_refcnt[gate->clk_num]++;
598f8f484bSPrashant Gaikwad 	if (gate->enable_refcnt[gate->clk_num] > 1) {
608f8f484bSPrashant Gaikwad 		spin_unlock_irqrestore(&periph_ref_lock, flags);
618f8f484bSPrashant Gaikwad 		return 0;
628f8f484bSPrashant Gaikwad 	}
638f8f484bSPrashant Gaikwad 
648f8f484bSPrashant Gaikwad 	write_enb_set(periph_clk_to_bit(gate), gate);
658f8f484bSPrashant Gaikwad 	udelay(2);
668f8f484bSPrashant Gaikwad 
678f8f484bSPrashant Gaikwad 	if (!(gate->flags & TEGRA_PERIPH_NO_RESET) &&
688f8f484bSPrashant Gaikwad 	    !(gate->flags & TEGRA_PERIPH_MANUAL_RESET)) {
698f8f484bSPrashant Gaikwad 		if (read_rst(gate) & periph_clk_to_bit(gate)) {
708f8f484bSPrashant Gaikwad 			udelay(5); /* reset propogation delay */
718f8f484bSPrashant Gaikwad 			write_rst_clr(periph_clk_to_bit(gate), gate);
728f8f484bSPrashant Gaikwad 		}
738f8f484bSPrashant Gaikwad 	}
748f8f484bSPrashant Gaikwad 
75fdcccbd8SPeter De Schrijver 	if (gate->flags & TEGRA_PERIPH_WAR_1005168) {
76fdcccbd8SPeter De Schrijver 		writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE);
77fdcccbd8SPeter De Schrijver 		writel_relaxed(BIT(22), gate->clk_base + LVL2_CLK_GATE_OVRE);
78fdcccbd8SPeter De Schrijver 		udelay(1);
79fdcccbd8SPeter De Schrijver 		writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE);
80fdcccbd8SPeter De Schrijver 	}
81fdcccbd8SPeter De Schrijver 
828f8f484bSPrashant Gaikwad 	spin_unlock_irqrestore(&periph_ref_lock, flags);
838f8f484bSPrashant Gaikwad 
848f8f484bSPrashant Gaikwad 	return 0;
858f8f484bSPrashant Gaikwad }
868f8f484bSPrashant Gaikwad 
878f8f484bSPrashant Gaikwad static void clk_periph_disable(struct clk_hw *hw)
888f8f484bSPrashant Gaikwad {
898f8f484bSPrashant Gaikwad 	struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
908f8f484bSPrashant Gaikwad 	unsigned long flags = 0;
918f8f484bSPrashant Gaikwad 
928f8f484bSPrashant Gaikwad 	spin_lock_irqsave(&periph_ref_lock, flags);
938f8f484bSPrashant Gaikwad 
948f8f484bSPrashant Gaikwad 	gate->enable_refcnt[gate->clk_num]--;
958f8f484bSPrashant Gaikwad 	if (gate->enable_refcnt[gate->clk_num] > 0) {
968f8f484bSPrashant Gaikwad 		spin_unlock_irqrestore(&periph_ref_lock, flags);
978f8f484bSPrashant Gaikwad 		return;
988f8f484bSPrashant Gaikwad 	}
998f8f484bSPrashant Gaikwad 
1008f8f484bSPrashant Gaikwad 	/*
1018f8f484bSPrashant Gaikwad 	 * If peripheral is in the APB bus then read the APB bus to
1028f8f484bSPrashant Gaikwad 	 * flush the write operation in apb bus. This will avoid the
1038f8f484bSPrashant Gaikwad 	 * peripheral access after disabling clock
1048f8f484bSPrashant Gaikwad 	 */
1058f8f484bSPrashant Gaikwad 	if (gate->flags & TEGRA_PERIPH_ON_APB)
1068f8f484bSPrashant Gaikwad 		tegra_read_chipid();
1078f8f484bSPrashant Gaikwad 
1088f8f484bSPrashant Gaikwad 	write_enb_clr(periph_clk_to_bit(gate), gate);
1098f8f484bSPrashant Gaikwad 
1108f8f484bSPrashant Gaikwad 	spin_unlock_irqrestore(&periph_ref_lock, flags);
1118f8f484bSPrashant Gaikwad }
1128f8f484bSPrashant Gaikwad 
1138f8f484bSPrashant Gaikwad const struct clk_ops tegra_clk_periph_gate_ops = {
1148f8f484bSPrashant Gaikwad 	.is_enabled = clk_periph_is_enabled,
1158f8f484bSPrashant Gaikwad 	.enable = clk_periph_enable,
1168f8f484bSPrashant Gaikwad 	.disable = clk_periph_disable,
1178f8f484bSPrashant Gaikwad };
1188f8f484bSPrashant Gaikwad 
1198f8f484bSPrashant Gaikwad struct clk *tegra_clk_register_periph_gate(const char *name,
1208f8f484bSPrashant Gaikwad 		const char *parent_name, u8 gate_flags, void __iomem *clk_base,
121d5ff89a8SPeter De Schrijver 		unsigned long flags, int clk_num, int *enable_refcnt)
1228f8f484bSPrashant Gaikwad {
1238f8f484bSPrashant Gaikwad 	struct tegra_clk_periph_gate *gate;
1248f8f484bSPrashant Gaikwad 	struct clk *clk;
1258f8f484bSPrashant Gaikwad 	struct clk_init_data init;
1267e14f223SThierry Reding 	const struct tegra_clk_periph_regs *pregs;
127d5ff89a8SPeter De Schrijver 
128d5ff89a8SPeter De Schrijver 	pregs = get_reg_bank(clk_num);
129d5ff89a8SPeter De Schrijver 	if (!pregs)
130d5ff89a8SPeter De Schrijver 		return ERR_PTR(-EINVAL);
1318f8f484bSPrashant Gaikwad 
1328f8f484bSPrashant Gaikwad 	gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1338f8f484bSPrashant Gaikwad 	if (!gate) {
1348f8f484bSPrashant Gaikwad 		pr_err("%s: could not allocate periph gate clk\n", __func__);
1358f8f484bSPrashant Gaikwad 		return ERR_PTR(-ENOMEM);
1368f8f484bSPrashant Gaikwad 	}
1378f8f484bSPrashant Gaikwad 
1388f8f484bSPrashant Gaikwad 	init.name = name;
1398f8f484bSPrashant Gaikwad 	init.flags = flags;
1408f8f484bSPrashant Gaikwad 	init.parent_names = parent_name ? &parent_name : NULL;
1418f8f484bSPrashant Gaikwad 	init.num_parents = parent_name ? 1 : 0;
1428f8f484bSPrashant Gaikwad 	init.ops = &tegra_clk_periph_gate_ops;
1438f8f484bSPrashant Gaikwad 
1448f8f484bSPrashant Gaikwad 	gate->magic = TEGRA_CLK_PERIPH_GATE_MAGIC;
1458f8f484bSPrashant Gaikwad 	gate->clk_base = clk_base;
1468f8f484bSPrashant Gaikwad 	gate->clk_num = clk_num;
1478f8f484bSPrashant Gaikwad 	gate->flags = gate_flags;
1488f8f484bSPrashant Gaikwad 	gate->enable_refcnt = enable_refcnt;
1498f8f484bSPrashant Gaikwad 	gate->regs = pregs;
1508f8f484bSPrashant Gaikwad 
1519619dba8SPeter De Schrijver 	if (read_enb(gate) & periph_clk_to_bit(gate))
1529619dba8SPeter De Schrijver 		enable_refcnt[clk_num]++;
1539619dba8SPeter De Schrijver 
1548f8f484bSPrashant Gaikwad 	/* Data in .init is copied by clk_register(), so stack variable OK */
1558f8f484bSPrashant Gaikwad 	gate->hw.init = &init;
1568f8f484bSPrashant Gaikwad 
1578f8f484bSPrashant Gaikwad 	clk = clk_register(NULL, &gate->hw);
1588f8f484bSPrashant Gaikwad 	if (IS_ERR(clk))
1598f8f484bSPrashant Gaikwad 		kfree(gate);
1608f8f484bSPrashant Gaikwad 
1618f8f484bSPrashant Gaikwad 	return clk;
1628f8f484bSPrashant Gaikwad }
163