blob: f1becd20d8bf8f75cf83f6c4bd31ecb5869449b0 [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>
Lukasz Majewski2dbf94b2020-08-24 11:12:18 +020029#include <dm/uclass.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060030#include <linux/bitops.h>
Dario Binacchi3b32e6a2020-05-02 17:58:31 +020031#include <malloc.h>
32#include <asm/io.h>
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020033#include <linux/clk-provider.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070034#include <linux/err.h>
Dario Binacchi3b32e6a2020-05-02 17:58:31 +020035#include "clk.h"
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020036
37#define UBOOT_DM_CLK_CCF_MUX "ccf_clk_mux"
38
39int clk_mux_val_to_index(struct clk *clk, u32 *table, unsigned int flags,
40 unsigned int val)
41{
Sean Andersoncfc2f022020-06-24 06:41:06 -040042 struct clk_mux *mux = to_clk_mux(clk);
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020043 int num_parents = mux->num_parents;
44
45 if (table) {
46 int i;
47
48 for (i = 0; i < num_parents; i++)
49 if (table[i] == val)
50 return i;
51 return -EINVAL;
52 }
53
54 if (val && (flags & CLK_MUX_INDEX_BIT))
55 val = ffs(val) - 1;
56
57 if (val && (flags & CLK_MUX_INDEX_ONE))
58 val--;
59
60 if (val >= num_parents)
61 return -EINVAL;
62
63 return val;
64}
65
Peng Fan6a8c2ad2019-07-31 07:01:28 +000066unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index)
67{
68 unsigned int val = index;
69
70 if (table) {
71 val = table[index];
72 } else {
73 if (flags & CLK_MUX_INDEX_BIT)
74 val = 1 << index;
75
76 if (flags & CLK_MUX_INDEX_ONE)
77 val++;
78 }
79
80 return val;
81}
82
83u8 clk_mux_get_parent(struct clk *clk)
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020084{
Sean Andersoncfc2f022020-06-24 06:41:06 -040085 struct clk_mux *mux = to_clk_mux(clk);
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020086 u32 val;
87
Lukasz Majewski669b7732019-06-24 15:50:49 +020088#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
89 val = mux->io_mux_val;
90#else
91 val = readl(mux->reg);
92#endif
93 val >>= mux->shift;
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020094 val &= mux->mask;
95
96 return clk_mux_val_to_index(clk, mux->table, mux->flags, val);
97}
98
Peng Fan6a8c2ad2019-07-31 07:01:28 +000099static int clk_fetch_parent_index(struct clk *clk,
100 struct clk *parent)
101{
Sean Andersoncfc2f022020-06-24 06:41:06 -0400102 struct clk_mux *mux = to_clk_mux(clk);
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000103
104 int i;
105
106 if (!parent)
107 return -EINVAL;
108
109 for (i = 0; i < mux->num_parents; i++) {
110 if (!strcmp(parent->dev->name, mux->parent_names[i]))
111 return i;
112 }
113
114 return -EINVAL;
115}
116
117static int clk_mux_set_parent(struct clk *clk, struct clk *parent)
118{
Sean Andersoncfc2f022020-06-24 06:41:06 -0400119 struct clk_mux *mux = to_clk_mux(clk);
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000120 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 {
Dario Binacchi88ea8df2020-05-02 17:58:33 +0200135#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
136 reg = mux->io_mux_val;
137#else
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000138 reg = readl(mux->reg);
Dario Binacchi88ea8df2020-05-02 17:58:33 +0200139#endif
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000140 reg &= ~(mux->mask << mux->shift);
141 }
142 val = val << mux->shift;
143 reg |= val;
Dario Binacchi88ea8df2020-05-02 17:58:33 +0200144#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
145 mux->io_mux_val = reg;
146#else
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000147 writel(reg, mux->reg);
Dario Binacchi88ea8df2020-05-02 17:58:33 +0200148#endif
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000149
150 return 0;
151}
152
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200153const struct clk_ops clk_mux_ops = {
Dario Binacchi863efd82020-10-14 23:42:17 +0200154 .get_rate = clk_generic_get_rate,
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000155 .set_parent = clk_mux_set_parent,
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200156};
157
158struct clk *clk_hw_register_mux_table(struct device *dev, const char *name,
159 const char * const *parent_names, u8 num_parents,
160 unsigned long flags,
161 void __iomem *reg, u8 shift, u32 mask,
162 u8 clk_mux_flags, u32 *table)
163{
164 struct clk_mux *mux;
165 struct clk *clk;
166 u8 width = 0;
167 int ret;
168
169 if (clk_mux_flags & CLK_MUX_HIWORD_MASK) {
170 width = fls(mask) - ffs(mask) + 1;
171 if (width + shift > 16) {
172 pr_err("mux value exceeds LOWORD field\n");
173 return ERR_PTR(-EINVAL);
174 }
175 }
176
177 /* allocate the mux */
178 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
179 if (!mux)
180 return ERR_PTR(-ENOMEM);
181
182 /* U-boot specific assignments */
183 mux->parent_names = parent_names;
184 mux->num_parents = num_parents;
185
186 /* struct clk_mux assignments */
187 mux->reg = reg;
188 mux->shift = shift;
189 mux->mask = mask;
190 mux->flags = clk_mux_flags;
191 mux->table = table;
Lukasz Majewski669b7732019-06-24 15:50:49 +0200192#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
193 mux->io_mux_val = *(u32 *)reg;
194#endif
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200195
196 clk = &mux->clk;
Dario Binacchi1a62dc12020-04-13 14:36:27 +0200197 clk->flags = flags;
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200198
199 /*
200 * Read the current mux setup - so we assign correct parent.
201 *
202 * Changing parent would require changing internals of udevice struct
Dario Binacchi5217bb12020-05-02 17:58:32 +0200203 * for the corresponding clock (to do that define .set_parent() method).
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200204 */
205 ret = clk_register(clk, UBOOT_DM_CLK_CCF_MUX, name,
206 parent_names[clk_mux_get_parent(clk)]);
207 if (ret) {
208 kfree(mux);
209 return ERR_PTR(ret);
210 }
211
212 return clk;
213}
214
215struct clk *clk_register_mux_table(struct device *dev, const char *name,
216 const char * const *parent_names, u8 num_parents,
217 unsigned long flags,
218 void __iomem *reg, u8 shift, u32 mask,
219 u8 clk_mux_flags, u32 *table)
220{
221 struct clk *clk;
222
223 clk = clk_hw_register_mux_table(dev, name, parent_names, num_parents,
224 flags, reg, shift, mask, clk_mux_flags,
225 table);
226 if (IS_ERR(clk))
227 return ERR_CAST(clk);
228 return clk;
229}
230
231struct clk *clk_register_mux(struct device *dev, const char *name,
232 const char * const *parent_names, u8 num_parents,
233 unsigned long flags,
234 void __iomem *reg, u8 shift, u8 width,
235 u8 clk_mux_flags)
236{
237 u32 mask = BIT(width) - 1;
238
239 return clk_register_mux_table(dev, name, parent_names, num_parents,
240 flags, reg, shift, mask, clk_mux_flags,
241 NULL);
242}
243
244U_BOOT_DRIVER(ccf_clk_mux) = {
245 .name = UBOOT_DM_CLK_CCF_MUX,
246 .id = UCLASS_CLK,
247 .ops = &clk_mux_ops,
248 .flags = DM_FLAG_PRE_RELOC,
249};