1 // SPDX-License-Identifier: GPL-2.0+
5 * Peng Fan <peng.fan@nxp.com>
8 #include <linux/clk-provider.h>
9 #include <linux/errno.h>
10 #include <linux/export.h>
12 #include <linux/iopoll.h>
13 #include <linux/slab.h>
17 #define DIRECT_OFFSET 0x0
20 * 0b000 - LPCG will be OFF in any CPU mode.
21 * 0b100 - LPCG will be ON in any CPU mode.
23 #define LPM_SETTING_OFF 0x0
24 #define LPM_SETTING_ON 0x4
26 #define LPM_CUR_OFFSET 0x1c
28 #define AUTHEN_OFFSET 0x30
29 #define CPULPM_EN BIT(2)
31 #define TZ_NS_MASK BIT(9)
33 #define WHITE_LIST_SHIFT 16
35 struct imx93_clk_gate
{
42 unsigned int *share_count
;
45 #define to_imx93_clk_gate(_hw) container_of(_hw, struct imx93_clk_gate, hw)
47 static void imx93_clk_gate_do_hardware(struct clk_hw
*hw
, bool enable
)
49 struct imx93_clk_gate
*gate
= to_imx93_clk_gate(hw
);
52 val
= readl(gate
->reg
+ AUTHEN_OFFSET
);
53 if (val
& CPULPM_EN
) {
54 val
= enable
? LPM_SETTING_ON
: LPM_SETTING_OFF
;
55 writel(val
, gate
->reg
+ LPM_CUR_OFFSET
);
57 val
= readl(gate
->reg
+ DIRECT_OFFSET
);
58 val
&= ~(gate
->mask
<< gate
->bit_idx
);
60 val
|= (gate
->val
& gate
->mask
) << gate
->bit_idx
;
61 writel(val
, gate
->reg
+ DIRECT_OFFSET
);
65 static int imx93_clk_gate_enable(struct clk_hw
*hw
)
67 struct imx93_clk_gate
*gate
= to_imx93_clk_gate(hw
);
70 spin_lock_irqsave(gate
->lock
, flags
);
72 if (gate
->share_count
&& (*gate
->share_count
)++ > 0)
75 imx93_clk_gate_do_hardware(hw
, true);
77 spin_unlock_irqrestore(gate
->lock
, flags
);
82 static void imx93_clk_gate_disable(struct clk_hw
*hw
)
84 struct imx93_clk_gate
*gate
= to_imx93_clk_gate(hw
);
87 spin_lock_irqsave(gate
->lock
, flags
);
89 if (gate
->share_count
) {
90 if (WARN_ON(*gate
->share_count
== 0))
92 else if (--(*gate
->share_count
) > 0)
96 imx93_clk_gate_do_hardware(hw
, false);
98 spin_unlock_irqrestore(gate
->lock
, flags
);
101 static int imx93_clk_gate_reg_is_enabled(struct imx93_clk_gate
*gate
)
103 u32 val
= readl(gate
->reg
+ AUTHEN_OFFSET
);
105 if (val
& CPULPM_EN
) {
106 val
= readl(gate
->reg
+ LPM_CUR_OFFSET
);
107 if (val
== LPM_SETTING_ON
)
110 val
= readl(gate
->reg
);
111 if (((val
>> gate
->bit_idx
) & gate
->mask
) == gate
->val
)
118 static int imx93_clk_gate_is_enabled(struct clk_hw
*hw
)
120 struct imx93_clk_gate
*gate
= to_imx93_clk_gate(hw
);
124 spin_lock_irqsave(gate
->lock
, flags
);
126 ret
= imx93_clk_gate_reg_is_enabled(gate
);
128 spin_unlock_irqrestore(gate
->lock
, flags
);
133 static void imx93_clk_gate_disable_unused(struct clk_hw
*hw
)
135 struct imx93_clk_gate
*gate
= to_imx93_clk_gate(hw
);
138 spin_lock_irqsave(gate
->lock
, flags
);
140 if (!gate
->share_count
|| *gate
->share_count
== 0)
141 imx93_clk_gate_do_hardware(hw
, false);
143 spin_unlock_irqrestore(gate
->lock
, flags
);
146 static const struct clk_ops imx93_clk_gate_ops
= {
147 .enable
= imx93_clk_gate_enable
,
148 .disable
= imx93_clk_gate_disable
,
149 .disable_unused
= imx93_clk_gate_disable_unused
,
150 .is_enabled
= imx93_clk_gate_is_enabled
,
153 static const struct clk_ops imx93_clk_gate_ro_ops
= {
154 .is_enabled
= imx93_clk_gate_is_enabled
,
157 struct clk_hw
*imx93_clk_gate(struct device
*dev
, const char *name
, const char *parent_name
,
158 unsigned long flags
, void __iomem
*reg
, u32 bit_idx
, u32 val
,
159 u32 mask
, u32 domain_id
, unsigned int *share_count
)
161 struct imx93_clk_gate
*gate
;
163 struct clk_init_data init
;
167 gate
= kzalloc(sizeof(struct imx93_clk_gate
), GFP_KERNEL
);
169 return ERR_PTR(-ENOMEM
);
172 gate
->lock
= &imx_ccm_lock
;
173 gate
->bit_idx
= bit_idx
;
176 gate
->share_count
= share_count
;
179 init
.ops
= &imx93_clk_gate_ops
;
180 init
.flags
= flags
| CLK_SET_RATE_PARENT
| CLK_OPS_PARENT_ENABLE
;
181 init
.parent_names
= parent_name
? &parent_name
: NULL
;
182 init
.num_parents
= parent_name
? 1 : 0;
184 gate
->hw
.init
= &init
;
187 authen
= readl(reg
+ AUTHEN_OFFSET
);
188 if (!(authen
& TZ_NS_MASK
) || !(authen
& BIT(WHITE_LIST_SHIFT
+ domain_id
)))
189 init
.ops
= &imx93_clk_gate_ro_ops
;
191 ret
= clk_hw_register(dev
, hw
);
199 EXPORT_SYMBOL_GPL(imx93_clk_gate
);