• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2019 DENX Software Engineering
4  * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
5  *
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>
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 
24 #include <common.h>
25 #include <asm/io.h>
26 #include <malloc.h>
27 #include <clk-uclass.h>
28 #include <dm/device.h>
29 #include <linux/clk-provider.h>
30 #include <clk.h>
31 #include "clk.h"
32 
33 #define UBOOT_DM_CLK_CCF_MUX "ccf_clk_mux"
34 
clk_mux_val_to_index(struct clk * clk,u32 * table,unsigned int flags,unsigned int val)35 int clk_mux_val_to_index(struct clk *clk, u32 *table, unsigned int flags,
36 			 unsigned int val)
37 {
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;
41 
42 	if (table) {
43 		int i;
44 
45 		for (i = 0; i < num_parents; i++)
46 			if (table[i] == val)
47 				return i;
48 		return -EINVAL;
49 	}
50 
51 	if (val && (flags & CLK_MUX_INDEX_BIT))
52 		val = ffs(val) - 1;
53 
54 	if (val && (flags & CLK_MUX_INDEX_ONE))
55 		val--;
56 
57 	if (val >= num_parents)
58 		return -EINVAL;
59 
60 	return val;
61 }
62 
clk_mux_index_to_val(u32 * table,unsigned int flags,u8 index)63 unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index)
64 {
65 	unsigned int val = index;
66 
67 	if (table) {
68 		val = table[index];
69 	} else {
70 		if (flags & CLK_MUX_INDEX_BIT)
71 			val = 1 << index;
72 
73 		if (flags & CLK_MUX_INDEX_ONE)
74 			val++;
75 	}
76 
77 	return val;
78 }
79 
clk_mux_get_parent(struct clk * clk)80 u8 clk_mux_get_parent(struct clk *clk)
81 {
82 	struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ?
83 			dev_get_clk_ptr(clk->dev) : clk);
84 	u32 val;
85 
86 #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
87 	val = mux->io_mux_val;
88 #else
89 	val = readl(mux->reg);
90 #endif
91 	val >>= mux->shift;
92 	val &= mux->mask;
93 
94 	return clk_mux_val_to_index(clk, mux->table, mux->flags, val);
95 }
96 
clk_fetch_parent_index(struct clk * clk,struct clk * parent)97 static int clk_fetch_parent_index(struct clk *clk,
98 				  struct clk *parent)
99 {
100 	struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ?
101 			dev_get_clk_ptr(clk->dev) : clk);
102 
103 	int i;
104 
105 	if (!parent)
106 		return -EINVAL;
107 
108 	for (i = 0; i < mux->num_parents; i++) {
109 		if (!strcmp(parent->dev->name, mux->parent_names[i]))
110 			return i;
111 	}
112 
113 	return -EINVAL;
114 }
115 
clk_mux_set_parent(struct clk * clk,struct clk * parent)116 static int clk_mux_set_parent(struct clk *clk, struct clk *parent)
117 {
118 	struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ?
119 			dev_get_clk_ptr(clk->dev) : clk);
120 	int index;
121 	u32 val;
122 	u32 reg;
123 
124 	index = clk_fetch_parent_index(clk, parent);
125 	if (index < 0) {
126 		printf("Could not fetch index\n");
127 		return index;
128 	}
129 
130 	val = clk_mux_index_to_val(mux->table, mux->flags, index);
131 
132 	if (mux->flags & CLK_MUX_HIWORD_MASK) {
133 		reg = mux->mask << (mux->shift + 16);
134 	} else {
135 		reg = readl(mux->reg);
136 		reg &= ~(mux->mask << mux->shift);
137 	}
138 	val = val << mux->shift;
139 	reg |= val;
140 	writel(reg, mux->reg);
141 
142 	return 0;
143 }
144 
145 const struct clk_ops clk_mux_ops = {
146 	.get_rate = clk_generic_get_rate,
147 	.set_parent = clk_mux_set_parent,
148 };
149 
clk_hw_register_mux_table(struct device * dev,const char * name,const char * const * parent_names,u8 num_parents,unsigned long flags,void __iomem * reg,u8 shift,u32 mask,u8 clk_mux_flags,u32 * table)150 struct clk *clk_hw_register_mux_table(struct device *dev, const char *name,
151 		const char * const *parent_names, u8 num_parents,
152 		unsigned long flags,
153 		void __iomem *reg, u8 shift, u32 mask,
154 		u8 clk_mux_flags, u32 *table)
155 {
156 	struct clk_mux *mux;
157 	struct clk *clk;
158 	u8 width = 0;
159 	int ret;
160 
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);
166 		}
167 	}
168 
169 	/* allocate the mux */
170 	mux = kzalloc(sizeof(*mux), GFP_KERNEL);
171 	if (!mux)
172 		return ERR_PTR(-ENOMEM);
173 
174 	/* U-boot specific assignments */
175 	mux->parent_names = parent_names;
176 	mux->num_parents = num_parents;
177 
178 	/* struct clk_mux assignments */
179 	mux->reg = reg;
180 	mux->shift = shift;
181 	mux->mask = mask;
182 	mux->flags = clk_mux_flags;
183 	mux->table = table;
184 #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
185 	mux->io_mux_val = *(u32 *)reg;
186 #endif
187 
188 	clk = &mux->clk;
189 
190 	/*
191 	 * Read the current mux setup - so we assign correct parent.
192 	 *
193 	 * Changing parent would require changing internals of udevice struct
194 	 * for the corresponding clock (to do that define .set_parent() method.
195 	 */
196 	ret = clk_register(clk, UBOOT_DM_CLK_CCF_MUX, name,
197 			   parent_names[clk_mux_get_parent(clk)]);
198 	if (ret) {
199 		kfree(mux);
200 		return ERR_PTR(ret);
201 	}
202 
203 	return clk;
204 }
205 
clk_register_mux_table(struct device * dev,const char * name,const char * const * parent_names,u8 num_parents,unsigned long flags,void __iomem * reg,u8 shift,u32 mask,u8 clk_mux_flags,u32 * table)206 struct clk *clk_register_mux_table(struct device *dev, const char *name,
207 		const char * const *parent_names, u8 num_parents,
208 		unsigned long flags,
209 		void __iomem *reg, u8 shift, u32 mask,
210 		u8 clk_mux_flags, u32 *table)
211 {
212 	struct clk *clk;
213 
214 	clk = clk_hw_register_mux_table(dev, name, parent_names, num_parents,
215 				       flags, reg, shift, mask, clk_mux_flags,
216 				       table);
217 	if (IS_ERR(clk))
218 		return ERR_CAST(clk);
219 	return clk;
220 }
221 
clk_register_mux(struct device * dev,const char * name,const char * const * parent_names,u8 num_parents,unsigned long flags,void __iomem * reg,u8 shift,u8 width,u8 clk_mux_flags)222 struct clk *clk_register_mux(struct device *dev, const char *name,
223 		const char * const *parent_names, u8 num_parents,
224 		unsigned long flags,
225 		void __iomem *reg, u8 shift, u8 width,
226 		u8 clk_mux_flags)
227 {
228 	u32 mask = BIT(width) - 1;
229 
230 	return clk_register_mux_table(dev, name, parent_names, num_parents,
231 				      flags, reg, shift, mask, clk_mux_flags,
232 				      NULL);
233 }
234 
235 U_BOOT_DRIVER(ccf_clk_mux) = {
236 	.name	= UBOOT_DM_CLK_CCF_MUX,
237 	.id	= UCLASS_CLK,
238 	.ops	= &clk_mux_ops,
239 	.flags = DM_FLAG_PRE_RELOC,
240 };
241