blob: e1a3c0af308baf8a0c128e7fe9dbba6255a46eac [file] [log] [blame]
Peng Fand69d0a62019-08-19 07:54:01 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2019 NXP
4 */
5
Simon Glass0f2af882020-05-10 11:40:05 -06006#include <log.h>
Peng Fand69d0a62019-08-19 07:54:01 +00007#include <asm/io.h>
8#include <malloc.h>
9#include <clk-uclass.h>
10#include <dm/device.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070011#include <dm/devres.h>
Peng Fand69d0a62019-08-19 07:54:01 +000012#include <linux/clk-provider.h>
13#include <clk.h>
14#include "clk.h"
Simon Glassd66c5f72020-02-03 07:36:15 -070015#include <linux/err.h>
Peng Fand69d0a62019-08-19 07:54:01 +000016
17#define UBOOT_DM_CLK_IMX_COMPOSITE "imx_clk_composite"
18
19#define PCG_PREDIV_SHIFT 16
20#define PCG_PREDIV_WIDTH 3
21#define PCG_PREDIV_MAX 8
22
23#define PCG_DIV_SHIFT 0
24#define PCG_DIV_WIDTH 6
25#define PCG_DIV_MAX 64
26
27#define PCG_PCS_SHIFT 24
28#define PCG_PCS_MASK 0x7
29
30#define PCG_CGC_SHIFT 28
31
32static unsigned long imx8m_clk_composite_divider_recalc_rate(struct clk *clk)
33{
34 struct clk_divider *divider = (struct clk_divider *)to_clk_divider(clk);
35 struct clk_composite *composite = (struct clk_composite *)clk->data;
36 ulong parent_rate = clk_get_parent_rate(&composite->clk);
37 unsigned long prediv_rate;
38 unsigned int prediv_value;
39 unsigned int div_value;
40
41 debug("%s: name %s prate: %lu reg: %p\n", __func__,
42 (&composite->clk)->dev->name, parent_rate, divider->reg);
43 prediv_value = readl(divider->reg) >> divider->shift;
44 prediv_value &= clk_div_mask(divider->width);
45
46 prediv_rate = divider_recalc_rate(clk, parent_rate, prediv_value,
47 NULL, divider->flags,
48 divider->width);
49
50 div_value = readl(divider->reg) >> PCG_DIV_SHIFT;
51 div_value &= clk_div_mask(PCG_DIV_WIDTH);
52
53 return divider_recalc_rate(clk, prediv_rate, div_value, NULL,
54 divider->flags, PCG_DIV_WIDTH);
55}
56
57static int imx8m_clk_composite_compute_dividers(unsigned long rate,
58 unsigned long parent_rate,
59 int *prediv, int *postdiv)
60{
61 int div1, div2;
62 int error = INT_MAX;
63 int ret = -EINVAL;
64
65 *prediv = 1;
66 *postdiv = 1;
67
68 for (div1 = 1; div1 <= PCG_PREDIV_MAX; div1++) {
69 for (div2 = 1; div2 <= PCG_DIV_MAX; div2++) {
70 int new_error = ((parent_rate / div1) / div2) - rate;
71
72 if (abs(new_error) < abs(error)) {
73 *prediv = div1;
74 *postdiv = div2;
75 error = new_error;
76 ret = 0;
77 }
78 }
79 }
80 return ret;
81}
82
83/*
84 * The clk are bound to a dev, because it is part of composite clk
85 * use composite clk to get dev
86 */
87static ulong imx8m_clk_composite_divider_set_rate(struct clk *clk,
88 unsigned long rate)
89{
90 struct clk_divider *divider = (struct clk_divider *)to_clk_divider(clk);
91 struct clk_composite *composite = (struct clk_composite *)clk->data;
92 ulong parent_rate = clk_get_parent_rate(&composite->clk);
93 int prediv_value;
94 int div_value;
95 int ret;
96 u32 val;
97
98 ret = imx8m_clk_composite_compute_dividers(rate, parent_rate,
99 &prediv_value, &div_value);
100 if (ret)
101 return ret;
102
103 val = readl(divider->reg);
104 val &= ~((clk_div_mask(divider->width) << divider->shift) |
105 (clk_div_mask(PCG_DIV_WIDTH) << PCG_DIV_SHIFT));
106
107 val |= (u32)(prediv_value - 1) << divider->shift;
108 val |= (u32)(div_value - 1) << PCG_DIV_SHIFT;
109 writel(val, divider->reg);
110
111 return clk_get_rate(&composite->clk);
112}
113
114static const struct clk_ops imx8m_clk_composite_divider_ops = {
115 .get_rate = imx8m_clk_composite_divider_recalc_rate,
116 .set_rate = imx8m_clk_composite_divider_set_rate,
117};
118
Marek Vasut7b0d6ef2025-04-27 17:39:34 +0200119static int imx8m_clk_mux_fetch_parent_index(struct udevice *cdev, struct clk *clk, struct clk *parent)
120{
121 struct clk_mux *mux = to_clk_mux(clk);
122 struct clk cclk;
123 int ret;
124 int i;
125
126 if (!parent)
127 return -EINVAL;
128
129 for (i = 0; i < mux->num_parents; i++) {
130 ret = clk_get_by_name(cdev, mux->parent_names[i], &cclk);
131 if (!ret && ofnode_equal(dev_ofnode(parent->dev), dev_ofnode(cclk.dev)))
132 return i;
133
134 if (!strcmp(parent->dev->name, mux->parent_names[i]))
135 return i;
136 if (!strcmp(parent->dev->name,
137 clk_resolve_parent_clk(clk->dev,
138 mux->parent_names[i])))
139 return i;
140 }
141
142 return -EINVAL;
143}
144
145
Michael Trimarchic09a2c02024-07-05 09:19:52 +0200146static int imx8m_clk_mux_set_parent(struct clk *clk, struct clk *parent)
147{
148 struct clk_mux *mux = to_clk_mux(clk);
Marek Vasut7b0d6ef2025-04-27 17:39:34 +0200149 struct clk_composite *composite = (struct clk_composite *)clk->data;
Michael Trimarchic09a2c02024-07-05 09:19:52 +0200150 int index;
151 u32 val;
152 u32 reg;
153
Marek Vasut7b0d6ef2025-04-27 17:39:34 +0200154 index = imx8m_clk_mux_fetch_parent_index(composite->dev, clk, parent);
Michael Trimarchic09a2c02024-07-05 09:19:52 +0200155 if (index < 0) {
156 log_err("Could not fetch index\n");
157 return index;
158 }
159
160 val = clk_mux_index_to_val(mux->table, mux->flags, index);
161
162 reg = readl(mux->reg);
163 reg &= ~(mux->mask << mux->shift);
164 val = val << mux->shift;
165 reg |= val;
166
167 /*
168 * write twice to make sure non-target interface
169 * SEL_A/B point the same clk input.
170 */
171 writel(reg, mux->reg);
172 writel(reg, mux->reg);
173
174 return 0;
175}
176
177const struct clk_ops imx8m_clk_mux_ops = {
178 .get_rate = clk_generic_get_rate,
179 .set_parent = imx8m_clk_mux_set_parent,
180};
181
Marek Vasut3668ec72025-03-23 16:58:44 +0100182struct clk *imx8m_clk_composite_flags(struct udevice *dev, const char *name,
Peng Fand69d0a62019-08-19 07:54:01 +0000183 const char * const *parent_names,
184 int num_parents, void __iomem *reg,
185 unsigned long flags)
186{
187 struct clk *clk = ERR_PTR(-ENOMEM);
188 struct clk_divider *div = NULL;
189 struct clk_gate *gate = NULL;
190 struct clk_mux *mux = NULL;
191
192 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
193 if (!mux)
194 goto fail;
195
196 mux->reg = reg;
197 mux->shift = PCG_PCS_SHIFT;
198 mux->mask = PCG_PCS_MASK;
199 mux->num_parents = num_parents;
Peng Fand69d0a62019-08-19 07:54:01 +0000200 mux->parent_names = parent_names;
201
202 div = kzalloc(sizeof(*div), GFP_KERNEL);
203 if (!div)
204 goto fail;
205
206 div->reg = reg;
207 div->shift = PCG_PREDIV_SHIFT;
208 div->width = PCG_PREDIV_WIDTH;
Michael Trimarchia1d6ea82024-07-02 12:26:17 +0200209 div->flags = CLK_DIVIDER_ROUND_CLOSEST;
Peng Fand69d0a62019-08-19 07:54:01 +0000210
211 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
212 if (!gate)
213 goto fail;
214
215 gate->reg = reg;
216 gate->bit_idx = PCG_CGC_SHIFT;
Peng Fand69d0a62019-08-19 07:54:01 +0000217
Marek Vasut3668ec72025-03-23 16:58:44 +0100218 clk = clk_register_composite(dev, name,
Peng Fand69d0a62019-08-19 07:54:01 +0000219 parent_names, num_parents,
Michael Trimarchic09a2c02024-07-05 09:19:52 +0200220 &mux->clk, &imx8m_clk_mux_ops, &div->clk,
Peng Fand69d0a62019-08-19 07:54:01 +0000221 &imx8m_clk_composite_divider_ops,
222 &gate->clk, &clk_gate_ops, flags);
223 if (IS_ERR(clk))
224 goto fail;
225
226 return clk;
227
228fail:
229 kfree(gate);
230 kfree(div);
231 kfree(mux);
232 return ERR_CAST(clk);
233}