1*0836c860SPeng Fan // SPDX-License-Identifier: GPL-2.0+
2*0836c860SPeng Fan /*
3*0836c860SPeng Fan * Copyright 2022 NXP
4*0836c860SPeng Fan *
5*0836c860SPeng Fan * Peng Fan <peng.fan@nxp.com>
6*0836c860SPeng Fan */
7*0836c860SPeng Fan
8*0836c860SPeng Fan #include <linux/clk-provider.h>
9*0836c860SPeng Fan #include <linux/errno.h>
10*0836c860SPeng Fan #include <linux/export.h>
11*0836c860SPeng Fan #include <linux/io.h>
12*0836c860SPeng Fan #include <linux/iopoll.h>
13*0836c860SPeng Fan #include <linux/slab.h>
14*0836c860SPeng Fan
15*0836c860SPeng Fan #include "clk.h"
16*0836c860SPeng Fan
17*0836c860SPeng Fan #define DIRECT_OFFSET 0x0
18*0836c860SPeng Fan
19*0836c860SPeng Fan /*
20*0836c860SPeng Fan * 0b000 - LPCG will be OFF in any CPU mode.
21*0836c860SPeng Fan * 0b100 - LPCG will be ON in any CPU mode.
22*0836c860SPeng Fan */
23*0836c860SPeng Fan #define LPM_SETTING_OFF 0x0
24*0836c860SPeng Fan #define LPM_SETTING_ON 0x4
25*0836c860SPeng Fan
26*0836c860SPeng Fan #define LPM_CUR_OFFSET 0x1c
27*0836c860SPeng Fan
28*0836c860SPeng Fan #define AUTHEN_OFFSET 0x30
29*0836c860SPeng Fan #define CPULPM_EN BIT(2)
30*0836c860SPeng Fan #define TZ_NS_SHIFT 9
31*0836c860SPeng Fan #define TZ_NS_MASK BIT(9)
32*0836c860SPeng Fan
33*0836c860SPeng Fan #define WHITE_LIST_SHIFT 16
34*0836c860SPeng Fan
35*0836c860SPeng Fan struct imx93_clk_gate {
36*0836c860SPeng Fan struct clk_hw hw;
37*0836c860SPeng Fan void __iomem *reg;
38*0836c860SPeng Fan u32 bit_idx;
39*0836c860SPeng Fan u32 val;
40*0836c860SPeng Fan u32 mask;
41*0836c860SPeng Fan spinlock_t *lock;
42*0836c860SPeng Fan unsigned int *share_count;
43*0836c860SPeng Fan };
44*0836c860SPeng Fan
45*0836c860SPeng Fan #define to_imx93_clk_gate(_hw) container_of(_hw, struct imx93_clk_gate, hw)
46*0836c860SPeng Fan
imx93_clk_gate_do_hardware(struct clk_hw * hw,bool enable)47*0836c860SPeng Fan static void imx93_clk_gate_do_hardware(struct clk_hw *hw, bool enable)
48*0836c860SPeng Fan {
49*0836c860SPeng Fan struct imx93_clk_gate *gate = to_imx93_clk_gate(hw);
50*0836c860SPeng Fan u32 val;
51*0836c860SPeng Fan
52*0836c860SPeng Fan val = readl(gate->reg + AUTHEN_OFFSET);
53*0836c860SPeng Fan if (val & CPULPM_EN) {
54*0836c860SPeng Fan val = enable ? LPM_SETTING_ON : LPM_SETTING_OFF;
55*0836c860SPeng Fan writel(val, gate->reg + LPM_CUR_OFFSET);
56*0836c860SPeng Fan } else {
57*0836c860SPeng Fan val = readl(gate->reg + DIRECT_OFFSET);
58*0836c860SPeng Fan val &= ~(gate->mask << gate->bit_idx);
59*0836c860SPeng Fan if (enable)
60*0836c860SPeng Fan val |= (gate->val & gate->mask) << gate->bit_idx;
61*0836c860SPeng Fan writel(val, gate->reg + DIRECT_OFFSET);
62*0836c860SPeng Fan }
63*0836c860SPeng Fan }
64*0836c860SPeng Fan
imx93_clk_gate_enable(struct clk_hw * hw)65*0836c860SPeng Fan static int imx93_clk_gate_enable(struct clk_hw *hw)
66*0836c860SPeng Fan {
67*0836c860SPeng Fan struct imx93_clk_gate *gate = to_imx93_clk_gate(hw);
68*0836c860SPeng Fan unsigned long flags;
69*0836c860SPeng Fan
70*0836c860SPeng Fan spin_lock_irqsave(gate->lock, flags);
71*0836c860SPeng Fan
72*0836c860SPeng Fan if (gate->share_count && (*gate->share_count)++ > 0)
73*0836c860SPeng Fan goto out;
74*0836c860SPeng Fan
75*0836c860SPeng Fan imx93_clk_gate_do_hardware(hw, true);
76*0836c860SPeng Fan out:
77*0836c860SPeng Fan spin_unlock_irqrestore(gate->lock, flags);
78*0836c860SPeng Fan
79*0836c860SPeng Fan return 0;
80*0836c860SPeng Fan }
81*0836c860SPeng Fan
imx93_clk_gate_disable(struct clk_hw * hw)82*0836c860SPeng Fan static void imx93_clk_gate_disable(struct clk_hw *hw)
83*0836c860SPeng Fan {
84*0836c860SPeng Fan struct imx93_clk_gate *gate = to_imx93_clk_gate(hw);
85*0836c860SPeng Fan unsigned long flags;
86*0836c860SPeng Fan
87*0836c860SPeng Fan spin_lock_irqsave(gate->lock, flags);
88*0836c860SPeng Fan
89*0836c860SPeng Fan if (gate->share_count) {
90*0836c860SPeng Fan if (WARN_ON(*gate->share_count == 0))
91*0836c860SPeng Fan goto out;
92*0836c860SPeng Fan else if (--(*gate->share_count) > 0)
93*0836c860SPeng Fan goto out;
94*0836c860SPeng Fan }
95*0836c860SPeng Fan
96*0836c860SPeng Fan imx93_clk_gate_do_hardware(hw, false);
97*0836c860SPeng Fan out:
98*0836c860SPeng Fan spin_unlock_irqrestore(gate->lock, flags);
99*0836c860SPeng Fan }
100*0836c860SPeng Fan
imx93_clk_gate_reg_is_enabled(struct imx93_clk_gate * gate)101*0836c860SPeng Fan static int imx93_clk_gate_reg_is_enabled(struct imx93_clk_gate *gate)
102*0836c860SPeng Fan {
103*0836c860SPeng Fan u32 val = readl(gate->reg + AUTHEN_OFFSET);
104*0836c860SPeng Fan
105*0836c860SPeng Fan if (val & CPULPM_EN) {
106*0836c860SPeng Fan val = readl(gate->reg + LPM_CUR_OFFSET);
107*0836c860SPeng Fan if (val == LPM_SETTING_ON)
108*0836c860SPeng Fan return 1;
109*0836c860SPeng Fan } else {
110*0836c860SPeng Fan val = readl(gate->reg);
111*0836c860SPeng Fan if (((val >> gate->bit_idx) & gate->mask) == gate->val)
112*0836c860SPeng Fan return 1;
113*0836c860SPeng Fan }
114*0836c860SPeng Fan
115*0836c860SPeng Fan return 0;
116*0836c860SPeng Fan }
117*0836c860SPeng Fan
imx93_clk_gate_is_enabled(struct clk_hw * hw)118*0836c860SPeng Fan static int imx93_clk_gate_is_enabled(struct clk_hw *hw)
119*0836c860SPeng Fan {
120*0836c860SPeng Fan struct imx93_clk_gate *gate = to_imx93_clk_gate(hw);
121*0836c860SPeng Fan unsigned long flags;
122*0836c860SPeng Fan int ret;
123*0836c860SPeng Fan
124*0836c860SPeng Fan spin_lock_irqsave(gate->lock, flags);
125*0836c860SPeng Fan
126*0836c860SPeng Fan ret = imx93_clk_gate_reg_is_enabled(gate);
127*0836c860SPeng Fan
128*0836c860SPeng Fan spin_unlock_irqrestore(gate->lock, flags);
129*0836c860SPeng Fan
130*0836c860SPeng Fan return ret;
131*0836c860SPeng Fan }
132*0836c860SPeng Fan
imx93_clk_gate_disable_unused(struct clk_hw * hw)133*0836c860SPeng Fan static void imx93_clk_gate_disable_unused(struct clk_hw *hw)
134*0836c860SPeng Fan {
135*0836c860SPeng Fan struct imx93_clk_gate *gate = to_imx93_clk_gate(hw);
136*0836c860SPeng Fan unsigned long flags;
137*0836c860SPeng Fan
138*0836c860SPeng Fan spin_lock_irqsave(gate->lock, flags);
139*0836c860SPeng Fan
140*0836c860SPeng Fan if (!gate->share_count || *gate->share_count == 0)
141*0836c860SPeng Fan imx93_clk_gate_do_hardware(hw, false);
142*0836c860SPeng Fan
143*0836c860SPeng Fan spin_unlock_irqrestore(gate->lock, flags);
144*0836c860SPeng Fan }
145*0836c860SPeng Fan
146*0836c860SPeng Fan static const struct clk_ops imx93_clk_gate_ops = {
147*0836c860SPeng Fan .enable = imx93_clk_gate_enable,
148*0836c860SPeng Fan .disable = imx93_clk_gate_disable,
149*0836c860SPeng Fan .disable_unused = imx93_clk_gate_disable_unused,
150*0836c860SPeng Fan .is_enabled = imx93_clk_gate_is_enabled,
151*0836c860SPeng Fan };
152*0836c860SPeng Fan
153*0836c860SPeng Fan static const struct clk_ops imx93_clk_gate_ro_ops = {
154*0836c860SPeng Fan .is_enabled = imx93_clk_gate_is_enabled,
155*0836c860SPeng Fan };
156*0836c860SPeng Fan
imx93_clk_gate(struct device * dev,const char * name,const char * parent_name,unsigned long flags,void __iomem * reg,u32 bit_idx,u32 val,u32 mask,u32 domain_id,unsigned int * share_count)157*0836c860SPeng Fan struct clk_hw *imx93_clk_gate(struct device *dev, const char *name, const char *parent_name,
158*0836c860SPeng Fan unsigned long flags, void __iomem *reg, u32 bit_idx, u32 val,
159*0836c860SPeng Fan u32 mask, u32 domain_id, unsigned int *share_count)
160*0836c860SPeng Fan {
161*0836c860SPeng Fan struct imx93_clk_gate *gate;
162*0836c860SPeng Fan struct clk_hw *hw;
163*0836c860SPeng Fan struct clk_init_data init;
164*0836c860SPeng Fan int ret;
165*0836c860SPeng Fan u32 authen;
166*0836c860SPeng Fan
167*0836c860SPeng Fan gate = kzalloc(sizeof(struct imx93_clk_gate), GFP_KERNEL);
168*0836c860SPeng Fan if (!gate)
169*0836c860SPeng Fan return ERR_PTR(-ENOMEM);
170*0836c860SPeng Fan
171*0836c860SPeng Fan gate->reg = reg;
172*0836c860SPeng Fan gate->lock = &imx_ccm_lock;
173*0836c860SPeng Fan gate->bit_idx = bit_idx;
174*0836c860SPeng Fan gate->val = val;
175*0836c860SPeng Fan gate->mask = mask;
176*0836c860SPeng Fan gate->share_count = share_count;
177*0836c860SPeng Fan
178*0836c860SPeng Fan init.name = name;
179*0836c860SPeng Fan init.ops = &imx93_clk_gate_ops;
180*0836c860SPeng Fan init.flags = flags | CLK_SET_RATE_PARENT | CLK_OPS_PARENT_ENABLE;
181*0836c860SPeng Fan init.parent_names = parent_name ? &parent_name : NULL;
182*0836c860SPeng Fan init.num_parents = parent_name ? 1 : 0;
183*0836c860SPeng Fan
184*0836c860SPeng Fan gate->hw.init = &init;
185*0836c860SPeng Fan hw = &gate->hw;
186*0836c860SPeng Fan
187*0836c860SPeng Fan authen = readl(reg + AUTHEN_OFFSET);
188*0836c860SPeng Fan if (!(authen & TZ_NS_MASK) || !(authen & BIT(WHITE_LIST_SHIFT + domain_id)))
189*0836c860SPeng Fan init.ops = &imx93_clk_gate_ro_ops;
190*0836c860SPeng Fan
191*0836c860SPeng Fan ret = clk_hw_register(dev, hw);
192*0836c860SPeng Fan if (ret) {
193*0836c860SPeng Fan kfree(gate);
194*0836c860SPeng Fan return ERR_PTR(ret);
195*0836c860SPeng Fan }
196*0836c860SPeng Fan
197*0836c860SPeng Fan return hw;
198*0836c860SPeng Fan }
199*0836c860SPeng Fan EXPORT_SYMBOL_GPL(imx93_clk_gate);
200