19952f691SThomas 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 */
clk_periph_is_enabled(struct clk_hw * hw)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
clk_periph_enable_locked(struct clk_hw * hw)51*c592c8a2SDmitry Osipenko static void clk_periph_enable_locked(struct clk_hw *hw)
528f8f484bSPrashant Gaikwad {
538f8f484bSPrashant Gaikwad struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
548f8f484bSPrashant Gaikwad
558f8f484bSPrashant Gaikwad write_enb_set(periph_clk_to_bit(gate), gate);
568f8f484bSPrashant Gaikwad udelay(2);
578f8f484bSPrashant Gaikwad
58fdcccbd8SPeter De Schrijver if (gate->flags & TEGRA_PERIPH_WAR_1005168) {
59fdcccbd8SPeter De Schrijver writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE);
60fdcccbd8SPeter De Schrijver writel_relaxed(BIT(22), gate->clk_base + LVL2_CLK_GATE_OVRE);
61fdcccbd8SPeter De Schrijver udelay(1);
62fdcccbd8SPeter De Schrijver writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE);
63fdcccbd8SPeter De Schrijver }
64*c592c8a2SDmitry Osipenko }
65*c592c8a2SDmitry Osipenko
clk_periph_disable_locked(struct clk_hw * hw)66*c592c8a2SDmitry Osipenko static void clk_periph_disable_locked(struct clk_hw *hw)
67*c592c8a2SDmitry Osipenko {
68*c592c8a2SDmitry Osipenko struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
69*c592c8a2SDmitry Osipenko
70*c592c8a2SDmitry Osipenko /*
71*c592c8a2SDmitry Osipenko * If peripheral is in the APB bus then read the APB bus to
72*c592c8a2SDmitry Osipenko * flush the write operation in apb bus. This will avoid the
73*c592c8a2SDmitry Osipenko * peripheral access after disabling clock
74*c592c8a2SDmitry Osipenko */
75*c592c8a2SDmitry Osipenko if (gate->flags & TEGRA_PERIPH_ON_APB)
76*c592c8a2SDmitry Osipenko tegra_read_chipid();
77*c592c8a2SDmitry Osipenko
78*c592c8a2SDmitry Osipenko write_enb_clr(periph_clk_to_bit(gate), gate);
79*c592c8a2SDmitry Osipenko }
80*c592c8a2SDmitry Osipenko
clk_periph_enable(struct clk_hw * hw)81*c592c8a2SDmitry Osipenko static int clk_periph_enable(struct clk_hw *hw)
82*c592c8a2SDmitry Osipenko {
83*c592c8a2SDmitry Osipenko struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
84*c592c8a2SDmitry Osipenko unsigned long flags = 0;
85*c592c8a2SDmitry Osipenko
86*c592c8a2SDmitry Osipenko spin_lock_irqsave(&periph_ref_lock, flags);
87*c592c8a2SDmitry Osipenko
88*c592c8a2SDmitry Osipenko if (!gate->enable_refcnt[gate->clk_num]++)
89*c592c8a2SDmitry Osipenko clk_periph_enable_locked(hw);
90fdcccbd8SPeter De Schrijver
918f8f484bSPrashant Gaikwad spin_unlock_irqrestore(&periph_ref_lock, flags);
928f8f484bSPrashant Gaikwad
938f8f484bSPrashant Gaikwad return 0;
948f8f484bSPrashant Gaikwad }
958f8f484bSPrashant Gaikwad
clk_periph_disable(struct clk_hw * hw)968f8f484bSPrashant Gaikwad static void clk_periph_disable(struct clk_hw *hw)
978f8f484bSPrashant Gaikwad {
988f8f484bSPrashant Gaikwad struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
998f8f484bSPrashant Gaikwad unsigned long flags = 0;
1008f8f484bSPrashant Gaikwad
1018f8f484bSPrashant Gaikwad spin_lock_irqsave(&periph_ref_lock, flags);
1028f8f484bSPrashant Gaikwad
103*c592c8a2SDmitry Osipenko WARN_ON(!gate->enable_refcnt[gate->clk_num]);
104*c592c8a2SDmitry Osipenko
105*c592c8a2SDmitry Osipenko if (--gate->enable_refcnt[gate->clk_num] == 0)
106*c592c8a2SDmitry Osipenko clk_periph_disable_locked(hw);
107*c592c8a2SDmitry Osipenko
1088f8f484bSPrashant Gaikwad spin_unlock_irqrestore(&periph_ref_lock, flags);
1098f8f484bSPrashant Gaikwad }
1108f8f484bSPrashant Gaikwad
clk_periph_disable_unused(struct clk_hw * hw)111*c592c8a2SDmitry Osipenko static void clk_periph_disable_unused(struct clk_hw *hw)
112*c592c8a2SDmitry Osipenko {
113*c592c8a2SDmitry Osipenko struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
114*c592c8a2SDmitry Osipenko unsigned long flags = 0;
1158f8f484bSPrashant Gaikwad
116*c592c8a2SDmitry Osipenko spin_lock_irqsave(&periph_ref_lock, flags);
117*c592c8a2SDmitry Osipenko
118*c592c8a2SDmitry Osipenko /*
119*c592c8a2SDmitry Osipenko * Some clocks are duplicated and some of them are marked as critical,
120*c592c8a2SDmitry Osipenko * like fuse and fuse_burn for example, thus the enable_refcnt will
121*c592c8a2SDmitry Osipenko * be non-zero here if the "unused" duplicate is disabled by CCF.
122*c592c8a2SDmitry Osipenko */
123*c592c8a2SDmitry Osipenko if (!gate->enable_refcnt[gate->clk_num])
124*c592c8a2SDmitry Osipenko clk_periph_disable_locked(hw);
1258f8f484bSPrashant Gaikwad
1268f8f484bSPrashant Gaikwad spin_unlock_irqrestore(&periph_ref_lock, flags);
1278f8f484bSPrashant Gaikwad }
1288f8f484bSPrashant Gaikwad
1298f8f484bSPrashant Gaikwad const struct clk_ops tegra_clk_periph_gate_ops = {
1308f8f484bSPrashant Gaikwad .is_enabled = clk_periph_is_enabled,
1318f8f484bSPrashant Gaikwad .enable = clk_periph_enable,
1328f8f484bSPrashant Gaikwad .disable = clk_periph_disable,
133*c592c8a2SDmitry Osipenko .disable_unused = clk_periph_disable_unused,
1348f8f484bSPrashant Gaikwad };
1358f8f484bSPrashant Gaikwad
tegra_clk_register_periph_gate(const char * name,const char * parent_name,u8 gate_flags,void __iomem * clk_base,unsigned long flags,int clk_num,int * enable_refcnt)1368f8f484bSPrashant Gaikwad struct clk *tegra_clk_register_periph_gate(const char *name,
1378f8f484bSPrashant Gaikwad const char *parent_name, u8 gate_flags, void __iomem *clk_base,
138d5ff89a8SPeter De Schrijver unsigned long flags, int clk_num, int *enable_refcnt)
1398f8f484bSPrashant Gaikwad {
1408f8f484bSPrashant Gaikwad struct tegra_clk_periph_gate *gate;
1418f8f484bSPrashant Gaikwad struct clk *clk;
1428f8f484bSPrashant Gaikwad struct clk_init_data init;
1437e14f223SThierry Reding const struct tegra_clk_periph_regs *pregs;
144d5ff89a8SPeter De Schrijver
145d5ff89a8SPeter De Schrijver pregs = get_reg_bank(clk_num);
146d5ff89a8SPeter De Schrijver if (!pregs)
147d5ff89a8SPeter De Schrijver return ERR_PTR(-EINVAL);
1488f8f484bSPrashant Gaikwad
1498f8f484bSPrashant Gaikwad gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1508f8f484bSPrashant Gaikwad if (!gate) {
1518f8f484bSPrashant Gaikwad pr_err("%s: could not allocate periph gate clk\n", __func__);
1528f8f484bSPrashant Gaikwad return ERR_PTR(-ENOMEM);
1538f8f484bSPrashant Gaikwad }
1548f8f484bSPrashant Gaikwad
1558f8f484bSPrashant Gaikwad init.name = name;
1568f8f484bSPrashant Gaikwad init.flags = flags;
1578f8f484bSPrashant Gaikwad init.parent_names = parent_name ? &parent_name : NULL;
1588f8f484bSPrashant Gaikwad init.num_parents = parent_name ? 1 : 0;
1598f8f484bSPrashant Gaikwad init.ops = &tegra_clk_periph_gate_ops;
1608f8f484bSPrashant Gaikwad
1618f8f484bSPrashant Gaikwad gate->magic = TEGRA_CLK_PERIPH_GATE_MAGIC;
1628f8f484bSPrashant Gaikwad gate->clk_base = clk_base;
1638f8f484bSPrashant Gaikwad gate->clk_num = clk_num;
1648f8f484bSPrashant Gaikwad gate->flags = gate_flags;
1658f8f484bSPrashant Gaikwad gate->enable_refcnt = enable_refcnt;
1668f8f484bSPrashant Gaikwad gate->regs = pregs;
1678f8f484bSPrashant Gaikwad
1688f8f484bSPrashant Gaikwad /* Data in .init is copied by clk_register(), so stack variable OK */
1698f8f484bSPrashant Gaikwad gate->hw.init = &init;
1708f8f484bSPrashant Gaikwad
1718f8f484bSPrashant Gaikwad clk = clk_register(NULL, &gate->hw);
1728f8f484bSPrashant Gaikwad if (IS_ERR(clk))
1738f8f484bSPrashant Gaikwad kfree(gate);
1748f8f484bSPrashant Gaikwad
1758f8f484bSPrashant Gaikwad return clk;
1768f8f484bSPrashant Gaikwad }
177