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
27 #include <clk-uclass.h>
28 #include <dm/device.h>
29 #include <linux/clk-provider.h>
33 #define UBOOT_DM_CLK_CCF_MUX "ccf_clk_mux"
35 int clk_mux_val_to_index(struct clk *clk, u32 *table, unsigned int flags,
38 struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ?
39 dev_get_clk_ptr(clk->dev) : clk);
40 int num_parents = mux->num_parents;
45 for (i = 0; i < num_parents; i++)
51 if (val && (flags & CLK_MUX_INDEX_BIT))
54 if (val && (flags & CLK_MUX_INDEX_ONE))
57 if (val >= num_parents)
63 unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index)
65 unsigned int val = index;
70 if (flags & CLK_MUX_INDEX_BIT)
73 if (flags & CLK_MUX_INDEX_ONE)
80 u8 clk_mux_get_parent(struct clk *clk)
82 struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ?
83 dev_get_clk_ptr(clk->dev) : clk);
86 #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
87 val = mux->io_mux_val;
89 val = readl(mux->reg);
94 return clk_mux_val_to_index(clk, mux->table, mux->flags, val);
97 static int clk_fetch_parent_index(struct clk *clk,
100 struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ?
101 dev_get_clk_ptr(clk->dev) : clk);
108 for (i = 0; i < mux->num_parents; i++) {
109 if (!strcmp(parent->dev->name, mux->parent_names[i]))
116 static int clk_mux_set_parent(struct clk *clk, struct clk *parent)
118 struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ?
119 dev_get_clk_ptr(clk->dev) : clk);
124 index = clk_fetch_parent_index(clk, parent);
126 printf("Could not fetch index\n");
130 val = clk_mux_index_to_val(mux->table, mux->flags, index);
132 if (mux->flags & CLK_MUX_HIWORD_MASK) {
133 reg = mux->mask << (mux->shift + 16);
135 reg = readl(mux->reg);
136 reg &= ~(mux->mask << mux->shift);
138 val = val << mux->shift;
140 writel(reg, mux->reg);
145 const struct clk_ops clk_mux_ops = {
146 .get_rate = clk_generic_get_rate,
147 .set_parent = clk_mux_set_parent,
150 struct clk *clk_hw_register_mux_table(struct device *dev, const char *name,
151 const char * const *parent_names, u8 num_parents,
153 void __iomem *reg, u8 shift, u32 mask,
154 u8 clk_mux_flags, u32 *table)
161 if (clk_mux_flags & CLK_MUX_HIWORD_MASK) {
162 width = fls(mask) - ffs(mask) + 1;
163 if (width + shift > 16) {
164 pr_err("mux value exceeds LOWORD field\n");
165 return ERR_PTR(-EINVAL);
169 /* allocate the mux */
170 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
172 return ERR_PTR(-ENOMEM);
174 /* U-boot specific assignments */
175 mux->parent_names = parent_names;
176 mux->num_parents = num_parents;
178 /* struct clk_mux assignments */
182 mux->flags = clk_mux_flags;
184 #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
185 mux->io_mux_val = *(u32 *)reg;
191 * Read the current mux setup - so we assign correct parent.
193 * Changing parent would require changing internals of udevice struct
194 * for the corresponding clock (to do that define .set_parent() method.
196 ret = clk_register(clk, UBOOT_DM_CLK_CCF_MUX, name,
197 parent_names[clk_mux_get_parent(clk)]);
206 struct clk *clk_register_mux_table(struct device *dev, const char *name,
207 const char * const *parent_names, u8 num_parents,
209 void __iomem *reg, u8 shift, u32 mask,
210 u8 clk_mux_flags, u32 *table)
214 clk = clk_hw_register_mux_table(dev, name, parent_names, num_parents,
215 flags, reg, shift, mask, clk_mux_flags,
218 return ERR_CAST(clk);
222 struct clk *clk_register_mux(struct device *dev, const char *name,
223 const char * const *parent_names, u8 num_parents,
225 void __iomem *reg, u8 shift, u8 width,
228 u32 mask = BIT(width) - 1;
230 return clk_register_mux_table(dev, name, parent_names, num_parents,
231 flags, reg, shift, mask, clk_mux_flags,
235 U_BOOT_DRIVER(ccf_clk_mux) = {
236 .name = UBOOT_DM_CLK_CCF_MUX,
239 .flags = DM_FLAG_PRE_RELOC,