1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/clk-provider.h>
7*4882a593Smuzhiyun #include <linux/slab.h>
8*4882a593Smuzhiyun #include <linux/io.h>
9*4882a593Smuzhiyun #include <linux/delay.h>
10*4882a593Smuzhiyun #include <linux/err.h>
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <soc/tegra/fuse.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include "clk.h"
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun static DEFINE_SPINLOCK(periph_ref_lock);
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /* Macros to assist peripheral gate clock */
19*4882a593Smuzhiyun #define read_enb(gate) \
20*4882a593Smuzhiyun readl_relaxed(gate->clk_base + (gate->regs->enb_reg))
21*4882a593Smuzhiyun #define write_enb_set(val, gate) \
22*4882a593Smuzhiyun writel_relaxed(val, gate->clk_base + (gate->regs->enb_set_reg))
23*4882a593Smuzhiyun #define write_enb_clr(val, gate) \
24*4882a593Smuzhiyun writel_relaxed(val, gate->clk_base + (gate->regs->enb_clr_reg))
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #define read_rst(gate) \
27*4882a593Smuzhiyun readl_relaxed(gate->clk_base + (gate->regs->rst_reg))
28*4882a593Smuzhiyun #define write_rst_clr(val, gate) \
29*4882a593Smuzhiyun writel_relaxed(val, gate->clk_base + (gate->regs->rst_clr_reg))
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #define periph_clk_to_bit(gate) (1 << (gate->clk_num % 32))
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #define LVL2_CLK_GATE_OVRE 0x554
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /* Peripheral gate clock ops */
clk_periph_is_enabled(struct clk_hw * hw)36*4882a593Smuzhiyun static int clk_periph_is_enabled(struct clk_hw *hw)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
39*4882a593Smuzhiyun int state = 1;
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun if (!(read_enb(gate) & periph_clk_to_bit(gate)))
42*4882a593Smuzhiyun state = 0;
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun if (!(gate->flags & TEGRA_PERIPH_NO_RESET))
45*4882a593Smuzhiyun if (read_rst(gate) & periph_clk_to_bit(gate))
46*4882a593Smuzhiyun state = 0;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun return state;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun
clk_periph_enable_locked(struct clk_hw * hw)51*4882a593Smuzhiyun static void clk_periph_enable_locked(struct clk_hw *hw)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun write_enb_set(periph_clk_to_bit(gate), gate);
56*4882a593Smuzhiyun udelay(2);
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun if (!(gate->flags & TEGRA_PERIPH_NO_RESET) &&
59*4882a593Smuzhiyun !(gate->flags & TEGRA_PERIPH_MANUAL_RESET)) {
60*4882a593Smuzhiyun if (read_rst(gate) & periph_clk_to_bit(gate)) {
61*4882a593Smuzhiyun udelay(5); /* reset propogation delay */
62*4882a593Smuzhiyun write_rst_clr(periph_clk_to_bit(gate), gate);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun if (gate->flags & TEGRA_PERIPH_WAR_1005168) {
67*4882a593Smuzhiyun writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE);
68*4882a593Smuzhiyun writel_relaxed(BIT(22), gate->clk_base + LVL2_CLK_GATE_OVRE);
69*4882a593Smuzhiyun udelay(1);
70*4882a593Smuzhiyun writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
clk_periph_disable_locked(struct clk_hw * hw)74*4882a593Smuzhiyun static void clk_periph_disable_locked(struct clk_hw *hw)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun * If peripheral is in the APB bus then read the APB bus to
80*4882a593Smuzhiyun * flush the write operation in apb bus. This will avoid the
81*4882a593Smuzhiyun * peripheral access after disabling clock
82*4882a593Smuzhiyun */
83*4882a593Smuzhiyun if (gate->flags & TEGRA_PERIPH_ON_APB)
84*4882a593Smuzhiyun tegra_read_chipid();
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun write_enb_clr(periph_clk_to_bit(gate), gate);
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
clk_periph_enable(struct clk_hw * hw)89*4882a593Smuzhiyun static int clk_periph_enable(struct clk_hw *hw)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
92*4882a593Smuzhiyun unsigned long flags = 0;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun spin_lock_irqsave(&periph_ref_lock, flags);
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun if (!gate->enable_refcnt[gate->clk_num]++)
97*4882a593Smuzhiyun clk_periph_enable_locked(hw);
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun spin_unlock_irqrestore(&periph_ref_lock, flags);
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun return 0;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
clk_periph_disable(struct clk_hw * hw)104*4882a593Smuzhiyun static void clk_periph_disable(struct clk_hw *hw)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
107*4882a593Smuzhiyun unsigned long flags = 0;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun spin_lock_irqsave(&periph_ref_lock, flags);
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun WARN_ON(!gate->enable_refcnt[gate->clk_num]);
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun if (--gate->enable_refcnt[gate->clk_num] == 0)
114*4882a593Smuzhiyun clk_periph_disable_locked(hw);
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun spin_unlock_irqrestore(&periph_ref_lock, flags);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
clk_periph_disable_unused(struct clk_hw * hw)119*4882a593Smuzhiyun static void clk_periph_disable_unused(struct clk_hw *hw)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
122*4882a593Smuzhiyun unsigned long flags = 0;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun spin_lock_irqsave(&periph_ref_lock, flags);
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun /*
127*4882a593Smuzhiyun * Some clocks are duplicated and some of them are marked as critical,
128*4882a593Smuzhiyun * like fuse and fuse_burn for example, thus the enable_refcnt will
129*4882a593Smuzhiyun * be non-zero here if the "unused" duplicate is disabled by CCF.
130*4882a593Smuzhiyun */
131*4882a593Smuzhiyun if (!gate->enable_refcnt[gate->clk_num])
132*4882a593Smuzhiyun clk_periph_disable_locked(hw);
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun spin_unlock_irqrestore(&periph_ref_lock, flags);
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun const struct clk_ops tegra_clk_periph_gate_ops = {
138*4882a593Smuzhiyun .is_enabled = clk_periph_is_enabled,
139*4882a593Smuzhiyun .enable = clk_periph_enable,
140*4882a593Smuzhiyun .disable = clk_periph_disable,
141*4882a593Smuzhiyun .disable_unused = clk_periph_disable_unused,
142*4882a593Smuzhiyun };
143*4882a593Smuzhiyun
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)144*4882a593Smuzhiyun struct clk *tegra_clk_register_periph_gate(const char *name,
145*4882a593Smuzhiyun const char *parent_name, u8 gate_flags, void __iomem *clk_base,
146*4882a593Smuzhiyun unsigned long flags, int clk_num, int *enable_refcnt)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun struct tegra_clk_periph_gate *gate;
149*4882a593Smuzhiyun struct clk *clk;
150*4882a593Smuzhiyun struct clk_init_data init;
151*4882a593Smuzhiyun const struct tegra_clk_periph_regs *pregs;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun pregs = get_reg_bank(clk_num);
154*4882a593Smuzhiyun if (!pregs)
155*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun gate = kzalloc(sizeof(*gate), GFP_KERNEL);
158*4882a593Smuzhiyun if (!gate) {
159*4882a593Smuzhiyun pr_err("%s: could not allocate periph gate clk\n", __func__);
160*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun init.name = name;
164*4882a593Smuzhiyun init.flags = flags;
165*4882a593Smuzhiyun init.parent_names = parent_name ? &parent_name : NULL;
166*4882a593Smuzhiyun init.num_parents = parent_name ? 1 : 0;
167*4882a593Smuzhiyun init.ops = &tegra_clk_periph_gate_ops;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun gate->magic = TEGRA_CLK_PERIPH_GATE_MAGIC;
170*4882a593Smuzhiyun gate->clk_base = clk_base;
171*4882a593Smuzhiyun gate->clk_num = clk_num;
172*4882a593Smuzhiyun gate->flags = gate_flags;
173*4882a593Smuzhiyun gate->enable_refcnt = enable_refcnt;
174*4882a593Smuzhiyun gate->regs = pregs;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun /* Data in .init is copied by clk_register(), so stack variable OK */
177*4882a593Smuzhiyun gate->hw.init = &init;
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun clk = clk_register(NULL, &gate->hw);
180*4882a593Smuzhiyun if (IS_ERR(clk))
181*4882a593Smuzhiyun kfree(gate);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun return clk;
184*4882a593Smuzhiyun }
185