common: Drop linux/bitops.h from common header
[platform/kernel/u-boot.git] / drivers / clk / clk-gate.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
4  * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
5  * Copyright 2019 NXP
6  *
7  * Gated clock implementation
8  */
9
10 #include <common.h>
11 #include <asm/io.h>
12 #include <malloc.h>
13 #include <clk-uclass.h>
14 #include <dm/device.h>
15 #include <dm/devres.h>
16 #include <linux/bitops.h>
17 #include <linux/clk-provider.h>
18 #include <clk.h>
19 #include "clk.h"
20 #include <linux/err.h>
21
22 #define UBOOT_DM_CLK_GATE "clk_gate"
23
24 /**
25  * DOC: basic gatable clock which can gate and ungate it's output
26  *
27  * Traits of this clock:
28  * prepare - clk_(un)prepare only ensures parent is (un)prepared
29  * enable - clk_enable and clk_disable are functional & control gating
30  * rate - inherits rate from parent.  No clk_set_rate support
31  * parent - fixed parent.  No clk_set_parent support
32  */
33
34 /*
35  * It works on following logic:
36  *
37  * For enabling clock, enable = 1
38  *      set2dis = 1     -> clear bit    -> set = 0
39  *      set2dis = 0     -> set bit      -> set = 1
40  *
41  * For disabling clock, enable = 0
42  *      set2dis = 1     -> set bit      -> set = 1
43  *      set2dis = 0     -> clear bit    -> set = 0
44  *
45  * So, result is always: enable xor set2dis.
46  */
47 static void clk_gate_endisable(struct clk *clk, int enable)
48 {
49         struct clk_gate *gate = to_clk_gate(clk_dev_binded(clk) ?
50                         dev_get_clk_ptr(clk->dev) : clk);
51         int set = gate->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
52         u32 reg;
53
54         set ^= enable;
55
56         if (gate->flags & CLK_GATE_HIWORD_MASK) {
57                 reg = BIT(gate->bit_idx + 16);
58                 if (set)
59                         reg |= BIT(gate->bit_idx);
60         } else {
61 #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
62                 reg = gate->io_gate_val;
63 #else
64                 reg = readl(gate->reg);
65 #endif
66
67                 if (set)
68                         reg |= BIT(gate->bit_idx);
69                 else
70                         reg &= ~BIT(gate->bit_idx);
71         }
72
73         writel(reg, gate->reg);
74 }
75
76 static int clk_gate_enable(struct clk *clk)
77 {
78         clk_gate_endisable(clk, 1);
79
80         return 0;
81 }
82
83 static int clk_gate_disable(struct clk *clk)
84 {
85         clk_gate_endisable(clk, 0);
86
87         return 0;
88 }
89
90 int clk_gate_is_enabled(struct clk *clk)
91 {
92         struct clk_gate *gate = to_clk_gate(clk_dev_binded(clk) ?
93                         dev_get_clk_ptr(clk->dev) : clk);
94         u32 reg;
95
96 #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
97         reg = gate->io_gate_val;
98 #else
99         reg = readl(gate->reg);
100 #endif
101
102         /* if a set bit disables this clk, flip it before masking */
103         if (gate->flags & CLK_GATE_SET_TO_DISABLE)
104                 reg ^= BIT(gate->bit_idx);
105
106         reg &= BIT(gate->bit_idx);
107
108         return reg ? 1 : 0;
109 }
110
111 const struct clk_ops clk_gate_ops = {
112         .enable = clk_gate_enable,
113         .disable = clk_gate_disable,
114         .get_rate = clk_generic_get_rate,
115 };
116
117 struct clk *clk_register_gate(struct device *dev, const char *name,
118                               const char *parent_name, unsigned long flags,
119                               void __iomem *reg, u8 bit_idx,
120                               u8 clk_gate_flags, spinlock_t *lock)
121 {
122         struct clk_gate *gate;
123         struct clk *clk;
124         int ret;
125
126         if (clk_gate_flags & CLK_GATE_HIWORD_MASK) {
127                 if (bit_idx > 15) {
128                         pr_err("gate bit exceeds LOWORD field\n");
129                         return ERR_PTR(-EINVAL);
130                 }
131         }
132
133         /* allocate the gate */
134         gate = kzalloc(sizeof(*gate), GFP_KERNEL);
135         if (!gate)
136                 return ERR_PTR(-ENOMEM);
137
138         /* struct clk_gate assignments */
139         gate->reg = reg;
140         gate->bit_idx = bit_idx;
141         gate->flags = clk_gate_flags;
142 #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
143         gate->io_gate_val = *(u32 *)reg;
144 #endif
145
146         clk = &gate->clk;
147
148         ret = clk_register(clk, UBOOT_DM_CLK_GATE, name, parent_name);
149         if (ret) {
150                 kfree(gate);
151                 return ERR_PTR(ret);
152         }
153
154         return clk;
155 }
156
157 U_BOOT_DRIVER(clk_gate) = {
158         .name   = UBOOT_DM_CLK_GATE,
159         .id     = UCLASS_CLK,
160         .ops    = &clk_gate_ops,
161         .flags = DM_FLAG_PRE_RELOC,
162 };