1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 DENX Software Engineering
4 * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
6 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
7 * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
8 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
10 * Simple multiplexer clock implementation
14 * U-Boot CCF porting node:
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.
20 * At least for IMX6Q (but NOT IMX6QP) it is important when we set the parent
24 #define LOG_CATEGORY UCLASS_CLK
28 #include <clk-uclass.h>
32 #include <dm/device.h>
33 #include <dm/device_compat.h>
34 #include <dm/devres.h>
35 #include <dm/uclass.h>
36 #include <linux/bitops.h>
37 #include <linux/clk-provider.h>
38 #include <linux/err.h>
42 #define UBOOT_DM_CLK_CCF_MUX "ccf_clk_mux"
44 int clk_mux_val_to_index(struct clk *clk, u32 *table, unsigned int flags,
47 struct clk_mux *mux = to_clk_mux(clk);
48 int num_parents = mux->num_parents;
53 for (i = 0; i < num_parents; i++)
59 if (val && (flags & CLK_MUX_INDEX_BIT))
62 if (val && (flags & CLK_MUX_INDEX_ONE))
65 if (val >= num_parents)
71 unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index)
73 unsigned int val = index;
78 if (flags & CLK_MUX_INDEX_BIT)
81 if (flags & CLK_MUX_INDEX_ONE)
88 u8 clk_mux_get_parent(struct clk *clk)
90 struct clk_mux *mux = to_clk_mux(clk);
93 #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
94 val = mux->io_mux_val;
96 val = readl(mux->reg);
101 return clk_mux_val_to_index(clk, mux->table, mux->flags, val);
104 static int clk_fetch_parent_index(struct clk *clk,
107 struct clk_mux *mux = to_clk_mux(clk);
114 for (i = 0; i < mux->num_parents; i++) {
115 if (!strcmp(parent->dev->name, mux->parent_names[i]))
122 static int clk_mux_set_parent(struct clk *clk, struct clk *parent)
124 struct clk_mux *mux = to_clk_mux(clk);
129 index = clk_fetch_parent_index(clk, parent);
131 log_err("Could not fetch index\n");
135 val = clk_mux_index_to_val(mux->table, mux->flags, index);
137 if (mux->flags & CLK_MUX_HIWORD_MASK) {
138 reg = mux->mask << (mux->shift + 16);
140 #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
141 reg = mux->io_mux_val;
143 reg = readl(mux->reg);
145 reg &= ~(mux->mask << mux->shift);
147 val = val << mux->shift;
149 #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
150 mux->io_mux_val = reg;
152 writel(reg, mux->reg);
158 const struct clk_ops clk_mux_ops = {
159 .get_rate = clk_generic_get_rate,
160 .set_parent = clk_mux_set_parent,
163 struct clk *clk_hw_register_mux_table(struct device *dev, const char *name,
164 const char * const *parent_names, u8 num_parents,
166 void __iomem *reg, u8 shift, u32 mask,
167 u8 clk_mux_flags, u32 *table)
174 if (clk_mux_flags & CLK_MUX_HIWORD_MASK) {
175 width = fls(mask) - ffs(mask) + 1;
176 if (width + shift > 16) {
177 dev_err(dev, "mux value exceeds LOWORD field\n");
178 return ERR_PTR(-EINVAL);
182 /* allocate the mux */
183 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
185 return ERR_PTR(-ENOMEM);
187 /* U-boot specific assignments */
188 mux->parent_names = parent_names;
189 mux->num_parents = num_parents;
191 /* struct clk_mux assignments */
195 mux->flags = clk_mux_flags;
197 #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
198 mux->io_mux_val = *(u32 *)reg;
205 * Read the current mux setup - so we assign correct parent.
207 * Changing parent would require changing internals of udevice struct
208 * for the corresponding clock (to do that define .set_parent() method).
210 ret = clk_register(clk, UBOOT_DM_CLK_CCF_MUX, name,
211 parent_names[clk_mux_get_parent(clk)]);
220 struct clk *clk_register_mux_table(struct device *dev, const char *name,
221 const char * const *parent_names, u8 num_parents,
223 void __iomem *reg, u8 shift, u32 mask,
224 u8 clk_mux_flags, u32 *table)
228 clk = clk_hw_register_mux_table(dev, name, parent_names, num_parents,
229 flags, reg, shift, mask, clk_mux_flags,
232 return ERR_CAST(clk);
236 struct clk *clk_register_mux(struct device *dev, const char *name,
237 const char * const *parent_names, u8 num_parents,
239 void __iomem *reg, u8 shift, u8 width,
242 u32 mask = BIT(width) - 1;
244 return clk_register_mux_table(dev, name, parent_names, num_parents,
245 flags, reg, shift, mask, clk_mux_flags,
249 U_BOOT_DRIVER(ccf_clk_mux) = {
250 .name = UBOOT_DM_CLK_CCF_MUX,
253 .flags = DM_FLAG_PRE_RELOC,