blob: e10baaf83b4704e4a52004293c3847701ee819be [file] [log] [blame]
Lukasz Majewski4de44bb2019-06-24 15:50:45 +02001// 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>
Dario Binacchi3b32e6a2020-05-02 17:58:31 +020025#include <clk.h>
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020026#include <clk-uclass.h>
27#include <dm/device.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070028#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060029#include <linux/bitops.h>
Dario Binacchi3b32e6a2020-05-02 17:58:31 +020030#include <malloc.h>
31#include <asm/io.h>
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020032#include <linux/clk-provider.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070033#include <linux/err.h>
Dario Binacchi3b32e6a2020-05-02 17:58:31 +020034#include "clk.h"
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020035
36#define UBOOT_DM_CLK_CCF_MUX "ccf_clk_mux"
37
38int clk_mux_val_to_index(struct clk *clk, u32 *table, unsigned int flags,
39 unsigned int val)
40{
Sean Andersoncfc2f022020-06-24 06:41:06 -040041 struct clk_mux *mux = to_clk_mux(clk);
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020042 int num_parents = mux->num_parents;
43
44 if (table) {
45 int i;
46
47 for (i = 0; i < num_parents; i++)
48 if (table[i] == val)
49 return i;
50 return -EINVAL;
51 }
52
53 if (val && (flags & CLK_MUX_INDEX_BIT))
54 val = ffs(val) - 1;
55
56 if (val && (flags & CLK_MUX_INDEX_ONE))
57 val--;
58
59 if (val >= num_parents)
60 return -EINVAL;
61
62 return val;
63}
64
Peng Fan6a8c2ad2019-07-31 07:01:28 +000065unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index)
66{
67 unsigned int val = index;
68
69 if (table) {
70 val = table[index];
71 } else {
72 if (flags & CLK_MUX_INDEX_BIT)
73 val = 1 << index;
74
75 if (flags & CLK_MUX_INDEX_ONE)
76 val++;
77 }
78
79 return val;
80}
81
82u8 clk_mux_get_parent(struct clk *clk)
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020083{
Sean Andersoncfc2f022020-06-24 06:41:06 -040084 struct clk_mux *mux = to_clk_mux(clk);
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020085 u32 val;
86
Lukasz Majewski669b7732019-06-24 15:50:49 +020087#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
88 val = mux->io_mux_val;
89#else
90 val = readl(mux->reg);
91#endif
92 val >>= mux->shift;
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020093 val &= mux->mask;
94
95 return clk_mux_val_to_index(clk, mux->table, mux->flags, val);
96}
97
Peng Fan6a8c2ad2019-07-31 07:01:28 +000098static int clk_fetch_parent_index(struct clk *clk,
99 struct clk *parent)
100{
Sean Andersoncfc2f022020-06-24 06:41:06 -0400101 struct clk_mux *mux = to_clk_mux(clk);
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000102
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
116static int clk_mux_set_parent(struct clk *clk, struct clk *parent)
117{
Sean Andersoncfc2f022020-06-24 06:41:06 -0400118 struct clk_mux *mux = to_clk_mux(clk);
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000119 int index;
120 u32 val;
121 u32 reg;
122
123 index = clk_fetch_parent_index(clk, parent);
124 if (index < 0) {
125 printf("Could not fetch index\n");
126 return index;
127 }
128
129 val = clk_mux_index_to_val(mux->table, mux->flags, index);
130
131 if (mux->flags & CLK_MUX_HIWORD_MASK) {
132 reg = mux->mask << (mux->shift + 16);
133 } else {
Dario Binacchi88ea8df2020-05-02 17:58:33 +0200134#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
135 reg = mux->io_mux_val;
136#else
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000137 reg = readl(mux->reg);
Dario Binacchi88ea8df2020-05-02 17:58:33 +0200138#endif
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000139 reg &= ~(mux->mask << mux->shift);
140 }
141 val = val << mux->shift;
142 reg |= val;
Dario Binacchi88ea8df2020-05-02 17:58:33 +0200143#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
144 mux->io_mux_val = reg;
145#else
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000146 writel(reg, mux->reg);
Dario Binacchi88ea8df2020-05-02 17:58:33 +0200147#endif
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000148
149 return 0;
150}
151
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200152const struct clk_ops clk_mux_ops = {
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000153 .get_rate = clk_generic_get_rate,
154 .set_parent = clk_mux_set_parent,
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200155};
156
157struct clk *clk_hw_register_mux_table(struct device *dev, const char *name,
158 const char * const *parent_names, u8 num_parents,
159 unsigned long flags,
160 void __iomem *reg, u8 shift, u32 mask,
161 u8 clk_mux_flags, u32 *table)
162{
163 struct clk_mux *mux;
164 struct clk *clk;
165 u8 width = 0;
166 int ret;
167
168 if (clk_mux_flags & CLK_MUX_HIWORD_MASK) {
169 width = fls(mask) - ffs(mask) + 1;
170 if (width + shift > 16) {
171 pr_err("mux value exceeds LOWORD field\n");
172 return ERR_PTR(-EINVAL);
173 }
174 }
175
176 /* allocate the mux */
177 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
178 if (!mux)
179 return ERR_PTR(-ENOMEM);
180
181 /* U-boot specific assignments */
182 mux->parent_names = parent_names;
183 mux->num_parents = num_parents;
184
185 /* struct clk_mux assignments */
186 mux->reg = reg;
187 mux->shift = shift;
188 mux->mask = mask;
189 mux->flags = clk_mux_flags;
190 mux->table = table;
Lukasz Majewski669b7732019-06-24 15:50:49 +0200191#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
192 mux->io_mux_val = *(u32 *)reg;
193#endif
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200194
195 clk = &mux->clk;
Dario Binacchi1a62dc12020-04-13 14:36:27 +0200196 clk->flags = flags;
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200197
198 /*
199 * Read the current mux setup - so we assign correct parent.
200 *
201 * Changing parent would require changing internals of udevice struct
Dario Binacchi5217bb12020-05-02 17:58:32 +0200202 * for the corresponding clock (to do that define .set_parent() method).
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200203 */
204 ret = clk_register(clk, UBOOT_DM_CLK_CCF_MUX, name,
205 parent_names[clk_mux_get_parent(clk)]);
206 if (ret) {
207 kfree(mux);
208 return ERR_PTR(ret);
209 }
210
211 return clk;
212}
213
214struct clk *clk_register_mux_table(struct device *dev, const char *name,
215 const char * const *parent_names, u8 num_parents,
216 unsigned long flags,
217 void __iomem *reg, u8 shift, u32 mask,
218 u8 clk_mux_flags, u32 *table)
219{
220 struct clk *clk;
221
222 clk = clk_hw_register_mux_table(dev, name, parent_names, num_parents,
223 flags, reg, shift, mask, clk_mux_flags,
224 table);
225 if (IS_ERR(clk))
226 return ERR_CAST(clk);
227 return clk;
228}
229
230struct clk *clk_register_mux(struct device *dev, const char *name,
231 const char * const *parent_names, u8 num_parents,
232 unsigned long flags,
233 void __iomem *reg, u8 shift, u8 width,
234 u8 clk_mux_flags)
235{
236 u32 mask = BIT(width) - 1;
237
238 return clk_register_mux_table(dev, name, parent_names, num_parents,
239 flags, reg, shift, mask, clk_mux_flags,
240 NULL);
241}
242
243U_BOOT_DRIVER(ccf_clk_mux) = {
244 .name = UBOOT_DM_CLK_CCF_MUX,
245 .id = UCLASS_CLK,
246 .ops = &clk_mux_ops,
247 .flags = DM_FLAG_PRE_RELOC,
248};