]>
Commit | Line | Data |
---|---|---|
1d7993d1 LM |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Copyright (C) 2019 DENX Software Engineering | |
4 | * Lukasz Majewski, DENX Software Engineering, [email protected] | |
5 | * | |
6 | * Copyright (C) 2011 Sascha Hauer, Pengutronix <[email protected]> | |
7 | * Copyright (C) 2011 Richard Zhao, Linaro <[email protected]> | |
8 | * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <[email protected]> | |
9 | * | |
10 | * Simple multiplexer clock implementation | |
11 | */ | |
12 | ||
13 | /* | |
14 | * U-Boot CCF porting node: | |
15 | * | |
16 | * The Linux kernel - as of tag: 5.0-rc3 is using also the imx_clk_fixup_mux() | |
17 | * version of CCF mux. It is used on e.g. imx6q to provide fixes (like | |
18 | * imx_cscmr1_fixup) for broken HW. | |
19 | * | |
20 | * At least for IMX6Q (but NOT IMX6QP) it is important when we set the parent | |
21 | * clock. | |
22 | */ | |
23 | ||
560e1e00 PD |
24 | #define LOG_CATEGORY UCLASS_CLK |
25 | ||
76eaa2d0 | 26 | #include <clk.h> |
1d7993d1 | 27 | #include <clk-uclass.h> |
560e1e00 | 28 | #include <log.h> |
572c446e PD |
29 | #include <malloc.h> |
30 | #include <asm/io.h> | |
1d7993d1 | 31 | #include <dm/device.h> |
560e1e00 | 32 | #include <dm/device_compat.h> |
61b29b82 | 33 | #include <dm/devres.h> |
ebd3f1f0 | 34 | #include <dm/uclass.h> |
cd93d625 | 35 | #include <linux/bitops.h> |
1d7993d1 | 36 | #include <linux/clk-provider.h> |
61b29b82 | 37 | #include <linux/err.h> |
1e94b46f | 38 | #include <linux/printk.h> |
572c446e | 39 | |
76eaa2d0 | 40 | #include "clk.h" |
1d7993d1 LM |
41 | |
42 | #define UBOOT_DM_CLK_CCF_MUX "ccf_clk_mux" | |
43 | ||
44 | int clk_mux_val_to_index(struct clk *clk, u32 *table, unsigned int flags, | |
45 | unsigned int val) | |
46 | { | |
78ce0bd3 | 47 | struct clk_mux *mux = to_clk_mux(clk); |
1d7993d1 LM |
48 | int num_parents = mux->num_parents; |
49 | ||
50 | if (table) { | |
51 | int i; | |
52 | ||
53 | for (i = 0; i < num_parents; i++) | |
54 | if (table[i] == val) | |
55 | return i; | |
56 | return -EINVAL; | |
57 | } | |
58 | ||
59 | if (val && (flags & CLK_MUX_INDEX_BIT)) | |
60 | val = ffs(val) - 1; | |
61 | ||
62 | if (val && (flags & CLK_MUX_INDEX_ONE)) | |
63 | val--; | |
64 | ||
65 | if (val >= num_parents) | |
66 | return -EINVAL; | |
67 | ||
68 | return val; | |
69 | } | |
70 | ||
4b044082 PF |
71 | unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index) |
72 | { | |
73 | unsigned int val = index; | |
74 | ||
75 | if (table) { | |
76 | val = table[index]; | |
77 | } else { | |
78 | if (flags & CLK_MUX_INDEX_BIT) | |
79 | val = 1 << index; | |
80 | ||
81 | if (flags & CLK_MUX_INDEX_ONE) | |
82 | val++; | |
83 | } | |
84 | ||
85 | return val; | |
86 | } | |
87 | ||
88 | u8 clk_mux_get_parent(struct clk *clk) | |
1d7993d1 | 89 | { |
78ce0bd3 | 90 | struct clk_mux *mux = to_clk_mux(clk); |
1d7993d1 LM |
91 | u32 val; |
92 | ||
4051c400 | 93 | #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF) |
5da0095e LM |
94 | val = mux->io_mux_val; |
95 | #else | |
96 | val = readl(mux->reg); | |
97 | #endif | |
98 | val >>= mux->shift; | |
1d7993d1 LM |
99 | val &= mux->mask; |
100 | ||
101 | return clk_mux_val_to_index(clk, mux->table, mux->flags, val); | |
102 | } | |
103 | ||
9a827d91 | 104 | int clk_mux_fetch_parent_index(struct clk *clk, struct clk *parent) |
4b044082 | 105 | { |
78ce0bd3 | 106 | struct clk_mux *mux = to_clk_mux(clk); |
4b044082 PF |
107 | |
108 | int i; | |
109 | ||
110 | if (!parent) | |
111 | return -EINVAL; | |
112 | ||
113 | for (i = 0; i < mux->num_parents; i++) { | |
114 | if (!strcmp(parent->dev->name, mux->parent_names[i])) | |
115 | return i; | |
116 | } | |
117 | ||
118 | return -EINVAL; | |
119 | } | |
120 | ||
121 | static int clk_mux_set_parent(struct clk *clk, struct clk *parent) | |
122 | { | |
78ce0bd3 | 123 | struct clk_mux *mux = to_clk_mux(clk); |
4b044082 PF |
124 | int index; |
125 | u32 val; | |
126 | u32 reg; | |
127 | ||
9a827d91 | 128 | index = clk_mux_fetch_parent_index(clk, parent); |
4b044082 | 129 | if (index < 0) { |
560e1e00 | 130 | log_err("Could not fetch index\n"); |
4b044082 PF |
131 | return index; |
132 | } | |
133 | ||
134 | val = clk_mux_index_to_val(mux->table, mux->flags, index); | |
135 | ||
136 | if (mux->flags & CLK_MUX_HIWORD_MASK) { | |
137 | reg = mux->mask << (mux->shift + 16); | |
138 | } else { | |
4051c400 | 139 | #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF) |
e3b5d74c DB |
140 | reg = mux->io_mux_val; |
141 | #else | |
4b044082 | 142 | reg = readl(mux->reg); |
e3b5d74c | 143 | #endif |
4b044082 PF |
144 | reg &= ~(mux->mask << mux->shift); |
145 | } | |
146 | val = val << mux->shift; | |
147 | reg |= val; | |
4051c400 | 148 | #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF) |
e3b5d74c DB |
149 | mux->io_mux_val = reg; |
150 | #else | |
4b044082 | 151 | writel(reg, mux->reg); |
e3b5d74c | 152 | #endif |
4b044082 PF |
153 | |
154 | return 0; | |
155 | } | |
156 | ||
1d7993d1 | 157 | const struct clk_ops clk_mux_ops = { |
fa181d1a | 158 | .get_rate = clk_generic_get_rate, |
4b044082 | 159 | .set_parent = clk_mux_set_parent, |
1d7993d1 LM |
160 | }; |
161 | ||
162 | struct clk *clk_hw_register_mux_table(struct device *dev, const char *name, | |
163 | const char * const *parent_names, u8 num_parents, | |
164 | unsigned long flags, | |
165 | void __iomem *reg, u8 shift, u32 mask, | |
166 | u8 clk_mux_flags, u32 *table) | |
167 | { | |
168 | struct clk_mux *mux; | |
169 | struct clk *clk; | |
170 | u8 width = 0; | |
171 | int ret; | |
172 | ||
173 | if (clk_mux_flags & CLK_MUX_HIWORD_MASK) { | |
174 | width = fls(mask) - ffs(mask) + 1; | |
175 | if (width + shift > 16) { | |
560e1e00 | 176 | dev_err(dev, "mux value exceeds LOWORD field\n"); |
1d7993d1 LM |
177 | return ERR_PTR(-EINVAL); |
178 | } | |
179 | } | |
180 | ||
181 | /* allocate the mux */ | |
182 | mux = kzalloc(sizeof(*mux), GFP_KERNEL); | |
183 | if (!mux) | |
184 | return ERR_PTR(-ENOMEM); | |
185 | ||
1be82afa | 186 | /* U-Boot specific assignments */ |
1d7993d1 LM |
187 | mux->parent_names = parent_names; |
188 | mux->num_parents = num_parents; | |
189 | ||
190 | /* struct clk_mux assignments */ | |
191 | mux->reg = reg; | |
192 | mux->shift = shift; | |
193 | mux->mask = mask; | |
194 | mux->flags = clk_mux_flags; | |
195 | mux->table = table; | |
4051c400 | 196 | #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF) |
5da0095e LM |
197 | mux->io_mux_val = *(u32 *)reg; |
198 | #endif | |
1d7993d1 LM |
199 | |
200 | clk = &mux->clk; | |
16bdc85b | 201 | clk->flags = flags; |
1d7993d1 LM |
202 | |
203 | /* | |
204 | * Read the current mux setup - so we assign correct parent. | |
205 | * | |
206 | * Changing parent would require changing internals of udevice struct | |
40559d27 | 207 | * for the corresponding clock (to do that define .set_parent() method). |
1d7993d1 LM |
208 | */ |
209 | ret = clk_register(clk, UBOOT_DM_CLK_CCF_MUX, name, | |
210 | parent_names[clk_mux_get_parent(clk)]); | |
211 | if (ret) { | |
212 | kfree(mux); | |
213 | return ERR_PTR(ret); | |
214 | } | |
215 | ||
216 | return clk; | |
217 | } | |
218 | ||
219 | struct clk *clk_register_mux_table(struct device *dev, const char *name, | |
220 | const char * const *parent_names, u8 num_parents, | |
221 | unsigned long flags, | |
222 | void __iomem *reg, u8 shift, u32 mask, | |
223 | u8 clk_mux_flags, u32 *table) | |
224 | { | |
225 | struct clk *clk; | |
226 | ||
227 | clk = clk_hw_register_mux_table(dev, name, parent_names, num_parents, | |
228 | flags, reg, shift, mask, clk_mux_flags, | |
229 | table); | |
230 | if (IS_ERR(clk)) | |
231 | return ERR_CAST(clk); | |
232 | return clk; | |
233 | } | |
234 | ||
235 | struct clk *clk_register_mux(struct device *dev, const char *name, | |
236 | const char * const *parent_names, u8 num_parents, | |
237 | unsigned long flags, | |
238 | void __iomem *reg, u8 shift, u8 width, | |
239 | u8 clk_mux_flags) | |
240 | { | |
241 | u32 mask = BIT(width) - 1; | |
242 | ||
243 | return clk_register_mux_table(dev, name, parent_names, num_parents, | |
244 | flags, reg, shift, mask, clk_mux_flags, | |
245 | NULL); | |
246 | } | |
247 | ||
248 | U_BOOT_DRIVER(ccf_clk_mux) = { | |
249 | .name = UBOOT_DM_CLK_CCF_MUX, | |
250 | .id = UCLASS_CLK, | |
251 | .ops = &clk_mux_ops, | |
252 | .flags = DM_FLAG_PRE_RELOC, | |
253 | }; |