2 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <linux/clk.h>
18 #include <linux/clk-provider.h>
19 #include <linux/slab.h>
21 #include <linux/delay.h>
22 #include <linux/err.h>
23 #include <linux/tegra-soc.h>
27 static DEFINE_SPINLOCK(periph_ref_lock
);
29 /* Macros to assist peripheral gate clock */
30 #define read_enb(gate) \
31 readl_relaxed(gate->clk_base + (gate->regs->enb_reg))
32 #define write_enb_set(val, gate) \
33 writel_relaxed(val, gate->clk_base + (gate->regs->enb_set_reg))
34 #define write_enb_clr(val, gate) \
35 writel_relaxed(val, gate->clk_base + (gate->regs->enb_clr_reg))
37 #define read_rst(gate) \
38 readl_relaxed(gate->clk_base + (gate->regs->rst_reg))
39 #define write_rst_set(val, gate) \
40 writel_relaxed(val, gate->clk_base + (gate->regs->rst_set_reg))
41 #define write_rst_clr(val, gate) \
42 writel_relaxed(val, gate->clk_base + (gate->regs->rst_clr_reg))
44 #define periph_clk_to_bit(gate) (1 << (gate->clk_num % 32))
46 #define LVL2_CLK_GATE_OVRE 0x554
48 /* Peripheral gate clock ops */
49 static int clk_periph_is_enabled(struct clk_hw
*hw
)
51 struct tegra_clk_periph_gate
*gate
= to_clk_periph_gate(hw
);
54 if (!(read_enb(gate
) & periph_clk_to_bit(gate
)))
57 if (!(gate
->flags
& TEGRA_PERIPH_NO_RESET
))
58 if (read_rst(gate
) & periph_clk_to_bit(gate
))
64 static int clk_periph_enable(struct clk_hw
*hw
)
66 struct tegra_clk_periph_gate
*gate
= to_clk_periph_gate(hw
);
67 unsigned long flags
= 0;
69 spin_lock_irqsave(&periph_ref_lock
, flags
);
71 gate
->enable_refcnt
[gate
->clk_num
]++;
72 if (gate
->enable_refcnt
[gate
->clk_num
] > 1) {
73 spin_unlock_irqrestore(&periph_ref_lock
, flags
);
77 write_enb_set(periph_clk_to_bit(gate
), gate
);
80 if (!(gate
->flags
& TEGRA_PERIPH_NO_RESET
) &&
81 !(gate
->flags
& TEGRA_PERIPH_MANUAL_RESET
)) {
82 if (read_rst(gate
) & periph_clk_to_bit(gate
)) {
83 udelay(5); /* reset propogation delay */
84 write_rst_clr(periph_clk_to_bit(gate
), gate
);
88 if (gate
->flags
& TEGRA_PERIPH_WAR_1005168
) {
89 writel_relaxed(0, gate
->clk_base
+ LVL2_CLK_GATE_OVRE
);
90 writel_relaxed(BIT(22), gate
->clk_base
+ LVL2_CLK_GATE_OVRE
);
92 writel_relaxed(0, gate
->clk_base
+ LVL2_CLK_GATE_OVRE
);
95 spin_unlock_irqrestore(&periph_ref_lock
, flags
);
100 static void clk_periph_disable(struct clk_hw
*hw
)
102 struct tegra_clk_periph_gate
*gate
= to_clk_periph_gate(hw
);
103 unsigned long flags
= 0;
105 spin_lock_irqsave(&periph_ref_lock
, flags
);
107 gate
->enable_refcnt
[gate
->clk_num
]--;
108 if (gate
->enable_refcnt
[gate
->clk_num
] > 0) {
109 spin_unlock_irqrestore(&periph_ref_lock
, flags
);
114 * If peripheral is in the APB bus then read the APB bus to
115 * flush the write operation in apb bus. This will avoid the
116 * peripheral access after disabling clock
118 if (gate
->flags
& TEGRA_PERIPH_ON_APB
)
121 write_enb_clr(periph_clk_to_bit(gate
), gate
);
123 spin_unlock_irqrestore(&periph_ref_lock
, flags
);
126 void tegra_periph_reset(struct tegra_clk_periph_gate
*gate
, bool assert)
128 if (gate
->flags
& TEGRA_PERIPH_NO_RESET
)
133 * If peripheral is in the APB bus then read the APB bus to
134 * flush the write operation in apb bus. This will avoid the
135 * peripheral access after disabling clock
137 if (gate
->flags
& TEGRA_PERIPH_ON_APB
)
140 write_rst_set(periph_clk_to_bit(gate
), gate
);
142 write_rst_clr(periph_clk_to_bit(gate
), gate
);
146 const struct clk_ops tegra_clk_periph_gate_ops
= {
147 .is_enabled
= clk_periph_is_enabled
,
148 .enable
= clk_periph_enable
,
149 .disable
= clk_periph_disable
,
152 struct clk
*tegra_clk_register_periph_gate(const char *name
,
153 const char *parent_name
, u8 gate_flags
, void __iomem
*clk_base
,
154 unsigned long flags
, int clk_num
,
155 struct tegra_clk_periph_regs
*pregs
, int *enable_refcnt
)
157 struct tegra_clk_periph_gate
*gate
;
159 struct clk_init_data init
;
161 gate
= kzalloc(sizeof(*gate
), GFP_KERNEL
);
163 pr_err("%s: could not allocate periph gate clk\n", __func__
);
164 return ERR_PTR(-ENOMEM
);
169 init
.parent_names
= parent_name
? &parent_name
: NULL
;
170 init
.num_parents
= parent_name
? 1 : 0;
171 init
.ops
= &tegra_clk_periph_gate_ops
;
173 gate
->magic
= TEGRA_CLK_PERIPH_GATE_MAGIC
;
174 gate
->clk_base
= clk_base
;
175 gate
->clk_num
= clk_num
;
176 gate
->flags
= gate_flags
;
177 gate
->enable_refcnt
= enable_refcnt
;
180 /* Data in .init is copied by clk_register(), so stack variable OK */
181 gate
->hw
.init
= &init
;
183 clk
= clk_register(NULL
, &gate
->hw
);