blob: c69cce0565d24b452af4ec2d9a6ca4d9883e17d7 [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>
25#include <asm/io.h>
26#include <malloc.h>
27#include <clk-uclass.h>
28#include <dm/device.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070029#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060030#include <linux/bitops.h>
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020031#include <linux/clk-provider.h>
32#include <clk.h>
33#include "clk.h"
Simon Glassd66c5f72020-02-03 07:36:15 -070034#include <linux/err.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{
Peng Fan5106fbb2019-07-31 07:01:26 +000041 struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ?
42 dev_get_clk_ptr(clk->dev) : 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{
Peng Fan5106fbb2019-07-31 07:01:26 +000085 struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ?
86 dev_get_clk_ptr(clk->dev) : clk);
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020087 u32 val;
88
Lukasz Majewski669b7732019-06-24 15:50:49 +020089#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
90 val = mux->io_mux_val;
91#else
92 val = readl(mux->reg);
93#endif
94 val >>= mux->shift;
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020095 val &= mux->mask;
96
97 return clk_mux_val_to_index(clk, mux->table, mux->flags, val);
98}
99
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000100static int clk_fetch_parent_index(struct clk *clk,
101 struct clk *parent)
102{
103 struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ?
104 dev_get_clk_ptr(clk->dev) : clk);
105
106 int i;
107
108 if (!parent)
109 return -EINVAL;
110
111 for (i = 0; i < mux->num_parents; i++) {
112 if (!strcmp(parent->dev->name, mux->parent_names[i]))
113 return i;
114 }
115
116 return -EINVAL;
117}
118
119static int clk_mux_set_parent(struct clk *clk, struct clk *parent)
120{
121 struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ?
122 dev_get_clk_ptr(clk->dev) : clk);
123 int index;
124 u32 val;
125 u32 reg;
126
127 index = clk_fetch_parent_index(clk, parent);
128 if (index < 0) {
129 printf("Could not fetch index\n");
130 return index;
131 }
132
133 val = clk_mux_index_to_val(mux->table, mux->flags, index);
134
135 if (mux->flags & CLK_MUX_HIWORD_MASK) {
136 reg = mux->mask << (mux->shift + 16);
137 } else {
138 reg = readl(mux->reg);
139 reg &= ~(mux->mask << mux->shift);
140 }
141 val = val << mux->shift;
142 reg |= val;
143 writel(reg, mux->reg);
144
145 return 0;
146}
147
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200148const struct clk_ops clk_mux_ops = {
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000149 .get_rate = clk_generic_get_rate,
150 .set_parent = clk_mux_set_parent,
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200151};
152
153struct clk *clk_hw_register_mux_table(struct device *dev, const char *name,
154 const char * const *parent_names, u8 num_parents,
155 unsigned long flags,
156 void __iomem *reg, u8 shift, u32 mask,
157 u8 clk_mux_flags, u32 *table)
158{
159 struct clk_mux *mux;
160 struct clk *clk;
161 u8 width = 0;
162 int ret;
163
164 if (clk_mux_flags & CLK_MUX_HIWORD_MASK) {
165 width = fls(mask) - ffs(mask) + 1;
166 if (width + shift > 16) {
167 pr_err("mux value exceeds LOWORD field\n");
168 return ERR_PTR(-EINVAL);
169 }
170 }
171
172 /* allocate the mux */
173 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
174 if (!mux)
175 return ERR_PTR(-ENOMEM);
176
177 /* U-boot specific assignments */
178 mux->parent_names = parent_names;
179 mux->num_parents = num_parents;
180
181 /* struct clk_mux assignments */
182 mux->reg = reg;
183 mux->shift = shift;
184 mux->mask = mask;
185 mux->flags = clk_mux_flags;
186 mux->table = table;
Lukasz Majewski669b7732019-06-24 15:50:49 +0200187#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
188 mux->io_mux_val = *(u32 *)reg;
189#endif
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200190
191 clk = &mux->clk;
192
193 /*
194 * Read the current mux setup - so we assign correct parent.
195 *
196 * Changing parent would require changing internals of udevice struct
197 * for the corresponding clock (to do that define .set_parent() method.
198 */
199 ret = clk_register(clk, UBOOT_DM_CLK_CCF_MUX, name,
200 parent_names[clk_mux_get_parent(clk)]);
201 if (ret) {
202 kfree(mux);
203 return ERR_PTR(ret);
204 }
205
206 return clk;
207}
208
209struct clk *clk_register_mux_table(struct device *dev, const char *name,
210 const char * const *parent_names, u8 num_parents,
211 unsigned long flags,
212 void __iomem *reg, u8 shift, u32 mask,
213 u8 clk_mux_flags, u32 *table)
214{
215 struct clk *clk;
216
217 clk = clk_hw_register_mux_table(dev, name, parent_names, num_parents,
218 flags, reg, shift, mask, clk_mux_flags,
219 table);
220 if (IS_ERR(clk))
221 return ERR_CAST(clk);
222 return clk;
223}
224
225struct clk *clk_register_mux(struct device *dev, const char *name,
226 const char * const *parent_names, u8 num_parents,
227 unsigned long flags,
228 void __iomem *reg, u8 shift, u8 width,
229 u8 clk_mux_flags)
230{
231 u32 mask = BIT(width) - 1;
232
233 return clk_register_mux_table(dev, name, parent_names, num_parents,
234 flags, reg, shift, mask, clk_mux_flags,
235 NULL);
236}
237
238U_BOOT_DRIVER(ccf_clk_mux) = {
239 .name = UBOOT_DM_CLK_CCF_MUX,
240 .id = UCLASS_CLK,
241 .ops = &clk_mux_ops,
242 .flags = DM_FLAG_PRE_RELOC,
243};