blob: 5d7faaa3eabf98ca550ee371104c32b85c4396a5 [file] [log] [blame]
Jerome Brunetd03f5f02019-02-11 16:45:01 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2018 - Beniamino Galvani <b.galvani@gmail.com>
4 * (C) Copyright 2018 - BayLibre, SAS
5 * Author: Neil Armstrong <narmstrong@baylibre.com>
6 */
7
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Jerome Brunetd03f5f02019-02-11 16:45:01 +01009#include <asm/arch/clock-g12a.h>
10#include <asm/io.h>
11#include <clk-uclass.h>
12#include <dm.h>
13#include <regmap.h>
14#include <syscon.h>
15#include <div64.h>
16#include <dt-bindings/clock/g12a-clkc.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070019#include <linux/err.h>
Neil Armstrong4371b9c2019-08-30 14:09:22 +020020#include <linux/kernel.h>
Jerome Brunetd03f5f02019-02-11 16:45:01 +010021#include "clk_meson.h"
22
Neil Armstrong4371b9c2019-08-30 14:09:22 +020023/* This driver support only basic clock tree operations :
24 * - Can calculate clock frequency on a limited tree
25 * - Can Read muxes and basic dividers (0-based only)
26 * - Can enable/disable gates with limited propagation
27 * - Can reparent without propagation, only on muxes
28 * - Can set rates without reparenting
29 * This driver is adapted to what is actually supported by U-Boot
30 */
31
32/* Only the clocks ids we don't want to expose, such as the internal muxes
33 * and dividers of composite clocks, will remain defined here.
34 */
35#define CLKID_MPEG_SEL 8
36#define CLKID_MPEG_DIV 9
37#define CLKID_SD_EMMC_A_CLK0_SEL 63
38#define CLKID_SD_EMMC_A_CLK0_DIV 64
39#define CLKID_SD_EMMC_B_CLK0_SEL 65
40#define CLKID_SD_EMMC_B_CLK0_DIV 66
41#define CLKID_SD_EMMC_C_CLK0_SEL 67
42#define CLKID_SD_EMMC_C_CLK0_DIV 68
43#define CLKID_MPLL0_DIV 69
44#define CLKID_MPLL1_DIV 70
45#define CLKID_MPLL2_DIV 71
46#define CLKID_MPLL3_DIV 72
47#define CLKID_MPLL_PREDIV 73
48#define CLKID_FCLK_DIV2_DIV 75
49#define CLKID_FCLK_DIV3_DIV 76
50#define CLKID_FCLK_DIV4_DIV 77
51#define CLKID_FCLK_DIV5_DIV 78
52#define CLKID_FCLK_DIV7_DIV 79
53#define CLKID_FCLK_DIV2P5_DIV 100
54#define CLKID_FIXED_PLL_DCO 101
55#define CLKID_SYS_PLL_DCO 102
56#define CLKID_GP0_PLL_DCO 103
57#define CLKID_HIFI_PLL_DCO 104
58#define CLKID_VPU_0_DIV 111
59#define CLKID_VPU_1_DIV 114
60#define CLKID_VAPB_0_DIV 118
61#define CLKID_VAPB_1_DIV 121
62#define CLKID_HDMI_PLL_DCO 125
63#define CLKID_HDMI_PLL_OD 126
64#define CLKID_HDMI_PLL_OD2 127
65#define CLKID_VID_PLL_SEL 130
66#define CLKID_VID_PLL_DIV 131
67#define CLKID_VCLK_SEL 132
68#define CLKID_VCLK2_SEL 133
69#define CLKID_VCLK_INPUT 134
70#define CLKID_VCLK2_INPUT 135
71#define CLKID_VCLK_DIV 136
72#define CLKID_VCLK2_DIV 137
73#define CLKID_VCLK_DIV2_EN 140
74#define CLKID_VCLK_DIV4_EN 141
75#define CLKID_VCLK_DIV6_EN 142
76#define CLKID_VCLK_DIV12_EN 143
77#define CLKID_VCLK2_DIV2_EN 144
78#define CLKID_VCLK2_DIV4_EN 145
79#define CLKID_VCLK2_DIV6_EN 146
80#define CLKID_VCLK2_DIV12_EN 147
81#define CLKID_CTS_ENCI_SEL 158
82#define CLKID_CTS_ENCP_SEL 159
83#define CLKID_CTS_VDAC_SEL 160
84#define CLKID_HDMI_TX_SEL 161
85#define CLKID_HDMI_SEL 166
86#define CLKID_HDMI_DIV 167
87#define CLKID_MALI_0_DIV 170
88#define CLKID_MALI_1_DIV 173
89
90#define CLKID_XTAL 0x10000000
91
Jerome Brunetd03f5f02019-02-11 16:45:01 +010092#define XTAL_RATE 24000000
93
94struct meson_clk {
95 struct regmap *map;
96};
97
Neil Armstrong4371b9c2019-08-30 14:09:22 +020098static ulong meson_div_get_rate(struct clk *clk, unsigned long id);
99static ulong meson_div_set_rate(struct clk *clk, unsigned long id, ulong rate,
100 ulong current_rate);
101static ulong meson_mux_set_parent(struct clk *clk, unsigned long id,
102 unsigned long parent_id);
103static ulong meson_mux_get_rate(struct clk *clk, unsigned long id);
Neil Armstrongfd6574e2019-05-28 10:50:37 +0200104static ulong meson_clk_set_rate_by_id(struct clk *clk, unsigned long id,
105 ulong rate, ulong current_rate);
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200106static ulong meson_mux_get_parent(struct clk *clk, unsigned long id);
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100107static ulong meson_clk_get_rate_by_id(struct clk *clk, unsigned long id);
108
109#define NUM_CLKS 178
110
111static struct meson_gate gates[NUM_CLKS] = {
112 /* Everything Else (EE) domain gates */
113 MESON_GATE(CLKID_SPICC0, HHI_GCLK_MPEG0, 8),
114 MESON_GATE(CLKID_I2C, HHI_GCLK_MPEG0, 9),
115 MESON_GATE(CLKID_UART0, HHI_GCLK_MPEG0, 13),
116 MESON_GATE(CLKID_SPICC1, HHI_GCLK_MPEG0, 14),
Neil Armstrongbe154282020-04-20 15:46:30 +0200117 MESON_GATE(CLKID_SD_EMMC_A, HHI_GCLK_MPEG0, 4),
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100118 MESON_GATE(CLKID_SD_EMMC_B, HHI_GCLK_MPEG0, 25),
119 MESON_GATE(CLKID_SD_EMMC_C, HHI_GCLK_MPEG0, 26),
120 MESON_GATE(CLKID_ETH, HHI_GCLK_MPEG1, 3),
121 MESON_GATE(CLKID_UART1, HHI_GCLK_MPEG1, 16),
Neil Armstronge2757512021-02-25 18:46:12 +0100122 MESON_GATE(CLKID_PCIE_COMB, HHI_GCLK_MPEG1, 24),
Neil Armstrongfd6574e2019-05-28 10:50:37 +0200123 MESON_GATE(CLKID_USB, HHI_GCLK_MPEG1, 25),
Neil Armstronge2757512021-02-25 18:46:12 +0100124 MESON_GATE(CLKID_PCIE_PHY, HHI_GCLK_MPEG1, 27),
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200125 MESON_GATE(CLKID_HTX_PCLK, HHI_GCLK_MPEG2, 4),
Neil Armstrongfd6574e2019-05-28 10:50:37 +0200126 MESON_GATE(CLKID_USB1_DDR_BRIDGE, HHI_GCLK_MPEG2, 8),
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200127 MESON_GATE(CLKID_VPU_INTR, HHI_GCLK_MPEG2, 25),
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100128
129 /* Peripheral Gates */
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200130 MESON_GATE(CLKID_FCLK_DIV2, HHI_FIX_PLL_CNTL1, 24),
131 MESON_GATE(CLKID_FCLK_DIV3, HHI_FIX_PLL_CNTL1, 20),
132 MESON_GATE(CLKID_FCLK_DIV4, HHI_FIX_PLL_CNTL1, 21),
133 MESON_GATE(CLKID_FCLK_DIV5, HHI_FIX_PLL_CNTL1, 22),
134 MESON_GATE(CLKID_FCLK_DIV7, HHI_FIX_PLL_CNTL1, 23),
Neil Armstrongbe154282020-04-20 15:46:30 +0200135 MESON_GATE(CLKID_SD_EMMC_A_CLK0, HHI_SD_EMMC_CLK_CNTL, 7),
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100136 MESON_GATE(CLKID_SD_EMMC_B_CLK0, HHI_SD_EMMC_CLK_CNTL, 23),
137 MESON_GATE(CLKID_SD_EMMC_C_CLK0, HHI_NAND_CLK_CNTL, 7),
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200138 MESON_GATE(CLKID_VPU_0, HHI_VPU_CLK_CNTL, 8),
139 MESON_GATE(CLKID_VPU_1, HHI_VPU_CLK_CNTL, 24),
140 MESON_GATE(CLKID_VAPB_0, HHI_VAPBCLK_CNTL, 8),
141 MESON_GATE(CLKID_VAPB_1, HHI_VAPBCLK_CNTL, 24),
142 MESON_GATE(CLKID_VAPB, HHI_VAPBCLK_CNTL, 30),
143 MESON_GATE(CLKID_HDMI, HHI_HDMI_CLK_CNTL, 8),
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100144};
145
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200146static int meson_set_gate_by_id(struct clk *clk, unsigned long id, bool on)
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100147{
148 struct meson_clk *priv = dev_get_priv(clk->dev);
149 struct meson_gate *gate;
150
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200151 debug("%s: %sabling %ld\n", __func__, on ? "en" : "dis", id);
152
153 /* Propagate through muxes */
154 switch (id) {
155 case CLKID_VPU:
156 return meson_set_gate_by_id(clk,
157 meson_mux_get_parent(clk, CLKID_VPU), on);
158 case CLKID_VAPB_SEL:
159 return meson_set_gate_by_id(clk,
160 meson_mux_get_parent(clk, CLKID_VAPB_SEL), on);
161 }
162
163 if (id >= ARRAY_SIZE(gates))
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100164 return -ENOENT;
165
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200166 gate = &gates[id];
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100167
168 if (gate->reg == 0)
169 return 0;
170
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200171 debug("%s: really %sabling %ld\n", __func__, on ? "en" : "dis", id);
172
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100173 regmap_update_bits(priv->map, gate->reg,
174 BIT(gate->bit), on ? BIT(gate->bit) : 0);
175
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200176 /* Propagate to next gate(s) */
177 switch (id) {
178 case CLKID_VAPB:
179 return meson_set_gate_by_id(clk, CLKID_VAPB_SEL, on);
180 case CLKID_VAPB_0:
181 return meson_set_gate_by_id(clk,
182 meson_mux_get_parent(clk, CLKID_VAPB_0_SEL), on);
183 case CLKID_VAPB_1:
184 return meson_set_gate_by_id(clk,
185 meson_mux_get_parent(clk, CLKID_VAPB_0_SEL), on);
186 case CLKID_VPU_0:
187 return meson_set_gate_by_id(clk,
188 meson_mux_get_parent(clk, CLKID_VPU_0_SEL), on);
189 case CLKID_VPU_1:
190 return meson_set_gate_by_id(clk,
191 meson_mux_get_parent(clk, CLKID_VPU_1_SEL), on);
192 }
193
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100194 return 0;
195}
196
197static int meson_clk_enable(struct clk *clk)
198{
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200199 return meson_set_gate_by_id(clk, clk->id, true);
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100200}
201
202static int meson_clk_disable(struct clk *clk)
203{
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200204 return meson_set_gate_by_id(clk, clk->id, false);
205}
206
207static struct parm meson_vpu_0_div_parm = {
208 HHI_VPU_CLK_CNTL, 0, 7,
209};
210
211int meson_vpu_0_div_parent = CLKID_VPU_0_SEL;
212
213static struct parm meson_vpu_1_div_parm = {
214 HHI_VPU_CLK_CNTL, 16, 7,
215};
216
217int meson_vpu_1_div_parent = CLKID_VPU_1_SEL;
218
219static struct parm meson_vapb_0_div_parm = {
220 HHI_VAPBCLK_CNTL, 0, 7,
221};
222
223int meson_vapb_0_div_parent = CLKID_VAPB_0_SEL;
224
225static struct parm meson_vapb_1_div_parm = {
226 HHI_VAPBCLK_CNTL, 16, 7,
227};
228
229int meson_vapb_1_div_parent = CLKID_VAPB_1_SEL;
230
231static struct parm meson_hdmi_div_parm = {
232 HHI_HDMI_CLK_CNTL, 0, 7,
233};
234
235int meson_hdmi_div_parent = CLKID_HDMI_SEL;
236
237static ulong meson_div_get_rate(struct clk *clk, unsigned long id)
238{
239 struct meson_clk *priv = dev_get_priv(clk->dev);
240 unsigned int rate, parent_rate;
241 struct parm *parm;
242 int parent;
243 uint reg;
244
245 switch (id) {
246 case CLKID_VPU_0_DIV:
247 parm = &meson_vpu_0_div_parm;
248 parent = meson_vpu_0_div_parent;
249 break;
250 case CLKID_VPU_1_DIV:
251 parm = &meson_vpu_1_div_parm;
252 parent = meson_vpu_1_div_parent;
253 break;
254 case CLKID_VAPB_0_DIV:
255 parm = &meson_vapb_0_div_parm;
256 parent = meson_vapb_0_div_parent;
257 break;
258 case CLKID_VAPB_1_DIV:
259 parm = &meson_vapb_1_div_parm;
260 parent = meson_vapb_1_div_parent;
261 break;
262 case CLKID_HDMI_DIV:
263 parm = &meson_hdmi_div_parm;
264 parent = meson_hdmi_div_parent;
265 break;
266 default:
267 return -ENOENT;
268 }
269
270 regmap_read(priv->map, parm->reg_off, &reg);
271 reg = PARM_GET(parm->width, parm->shift, reg);
272
273 debug("%s: div of %ld is %d\n", __func__, id, reg + 1);
274
275 parent_rate = meson_clk_get_rate_by_id(clk, parent);
276 if (IS_ERR_VALUE(parent_rate))
277 return parent_rate;
278
279 debug("%s: parent rate of %ld is %d\n", __func__, id, parent_rate);
280
281 rate = parent_rate / (reg + 1);
282
283 debug("%s: rate of %ld is %d\n", __func__, id, rate);
284
285 return rate;
286}
287
288static ulong meson_div_set_rate(struct clk *clk, unsigned long id, ulong rate,
289 ulong current_rate)
290{
291 struct meson_clk *priv = dev_get_priv(clk->dev);
292 unsigned int new_div = -EINVAL;
293 unsigned long parent_rate;
294 struct parm *parm;
295 int parent;
296 int ret;
297
298 if (current_rate == rate)
299 return 0;
300
301 debug("%s: setting rate of %ld from %ld to %ld\n",
302 __func__, id, current_rate, rate);
303
304 switch (id) {
305 case CLKID_VPU_0_DIV:
306 parm = &meson_vpu_0_div_parm;
307 parent = meson_vpu_0_div_parent;
308 break;
309 case CLKID_VPU_1_DIV:
310 parm = &meson_vpu_1_div_parm;
311 parent = meson_vpu_1_div_parent;
312 break;
313 case CLKID_VAPB_0_DIV:
314 parm = &meson_vapb_0_div_parm;
315 parent = meson_vapb_0_div_parent;
316 break;
317 case CLKID_VAPB_1_DIV:
318 parm = &meson_vapb_1_div_parm;
319 parent = meson_vapb_1_div_parent;
320 break;
321 case CLKID_HDMI_DIV:
322 parm = &meson_hdmi_div_parm;
323 parent = meson_hdmi_div_parent;
324 break;
325 default:
326 return -ENOENT;
327 }
328
329 parent_rate = meson_clk_get_rate_by_id(clk, parent);
330 if (IS_ERR_VALUE(parent_rate))
331 return parent_rate;
332
333 debug("%s: parent rate of %ld is %ld\n", __func__, id, parent_rate);
334
335 /* If can't divide, set parent instead */
336 if (!parent_rate || rate > parent_rate)
337 return meson_clk_set_rate_by_id(clk, parent, rate,
338 current_rate);
339
340 new_div = DIV_ROUND_CLOSEST(parent_rate, rate);
341
342 debug("%s: new div of %ld is %d\n", __func__, id, new_div);
343
344 /* If overflow, try to set parent rate and retry */
345 if (!new_div || new_div > (1 << parm->width)) {
346 ret = meson_clk_set_rate_by_id(clk, parent, rate, current_rate);
347 if (IS_ERR_VALUE(ret))
348 return ret;
349
350 parent_rate = meson_clk_get_rate_by_id(clk, parent);
351 if (IS_ERR_VALUE(parent_rate))
352 return parent_rate;
353
354 new_div = DIV_ROUND_CLOSEST(parent_rate, rate);
355
356 debug("%s: new new div of %ld is %d\n", __func__, id, new_div);
357
358 if (!new_div || new_div > (1 << parm->width))
359 return -EINVAL;
360 }
361
362 debug("%s: setting div of %ld to %d\n", __func__, id, new_div);
363
364 regmap_update_bits(priv->map, parm->reg_off,
365 SETPMASK(parm->width, parm->shift),
366 (new_div - 1) << parm->shift);
367
368 debug("%s: new rate of %ld is %ld\n",
369 __func__, id, meson_div_get_rate(clk, id));
370
371 return 0;
372}
373
374static struct parm meson_vpu_mux_parm = {
375 HHI_VPU_CLK_CNTL, 31, 1,
376};
377
378int meson_vpu_mux_parents[] = {
379 CLKID_VPU_0,
380 CLKID_VPU_1,
381};
382
383static struct parm meson_vpu_0_mux_parm = {
384 HHI_VPU_CLK_CNTL, 9, 3,
385};
386
387static struct parm meson_vpu_1_mux_parm = {
388 HHI_VPU_CLK_CNTL, 25, 3,
389};
390
391static int meson_vpu_0_1_mux_parents[] = {
392 CLKID_FCLK_DIV3,
393 CLKID_FCLK_DIV4,
394 CLKID_FCLK_DIV5,
395 CLKID_FCLK_DIV7,
396 -ENOENT,
397 -ENOENT,
398 -ENOENT,
399 -ENOENT,
400};
401
402static struct parm meson_vapb_sel_mux_parm = {
403 HHI_VAPBCLK_CNTL, 31, 1,
404};
405
406int meson_vapb_sel_mux_parents[] = {
407 CLKID_VAPB_0,
408 CLKID_VAPB_1,
409};
410
411static struct parm meson_vapb_0_mux_parm = {
412 HHI_VAPBCLK_CNTL, 9, 2,
413};
414
415static struct parm meson_vapb_1_mux_parm = {
416 HHI_VAPBCLK_CNTL, 25, 2,
417};
418
419static int meson_vapb_0_1_mux_parents[] = {
420 CLKID_FCLK_DIV4,
421 CLKID_FCLK_DIV3,
422 CLKID_FCLK_DIV5,
423 CLKID_FCLK_DIV7,
424};
425
426static struct parm meson_hdmi_mux_parm = {
427 HHI_HDMI_CLK_CNTL, 9, 2,
428};
429
430static int meson_hdmi_mux_parents[] = {
431 CLKID_XTAL,
432 CLKID_FCLK_DIV4,
433 CLKID_FCLK_DIV3,
434 CLKID_FCLK_DIV5,
435};
436
437static ulong meson_mux_get_parent(struct clk *clk, unsigned long id)
438{
439 struct meson_clk *priv = dev_get_priv(clk->dev);
440 struct parm *parm;
441 int *parents;
442 uint reg;
443
444 switch (id) {
445 case CLKID_VPU:
446 parm = &meson_vpu_mux_parm;
447 parents = meson_vpu_mux_parents;
448 break;
449 case CLKID_VPU_0_SEL:
450 parm = &meson_vpu_0_mux_parm;
451 parents = meson_vpu_0_1_mux_parents;
452 break;
453 case CLKID_VPU_1_SEL:
454 parm = &meson_vpu_1_mux_parm;
455 parents = meson_vpu_0_1_mux_parents;
456 break;
457 case CLKID_VAPB_SEL:
458 parm = &meson_vapb_sel_mux_parm;
459 parents = meson_vapb_sel_mux_parents;
460 break;
461 case CLKID_VAPB_0_SEL:
462 parm = &meson_vapb_0_mux_parm;
463 parents = meson_vapb_0_1_mux_parents;
464 break;
465 case CLKID_VAPB_1_SEL:
466 parm = &meson_vapb_1_mux_parm;
467 parents = meson_vapb_0_1_mux_parents;
468 break;
469 case CLKID_HDMI_SEL:
470 parm = &meson_hdmi_mux_parm;
471 parents = meson_hdmi_mux_parents;
472 break;
473 default:
474 return -ENOENT;
475 }
476
477 regmap_read(priv->map, parm->reg_off, &reg);
478 reg = PARM_GET(parm->width, parm->shift, reg);
479
480 debug("%s: parent of %ld is %d (%d)\n",
481 __func__, id, parents[reg], reg);
482
483 return parents[reg];
484}
485
486static ulong meson_mux_set_parent(struct clk *clk, unsigned long id,
487 unsigned long parent_id)
488{
489 unsigned long cur_parent = meson_mux_get_parent(clk, id);
490 struct meson_clk *priv = dev_get_priv(clk->dev);
491 unsigned int new_index = -EINVAL;
492 struct parm *parm;
493 int *parents;
494 int i;
495
496 if (IS_ERR_VALUE(cur_parent))
497 return cur_parent;
498
499 debug("%s: setting parent of %ld from %ld to %ld\n",
500 __func__, id, cur_parent, parent_id);
501
502 if (cur_parent == parent_id)
503 return 0;
504
505 switch (id) {
506 case CLKID_VPU:
507 parm = &meson_vpu_mux_parm;
508 parents = meson_vpu_mux_parents;
509 break;
510 case CLKID_VPU_0_SEL:
511 parm = &meson_vpu_0_mux_parm;
512 parents = meson_vpu_0_1_mux_parents;
513 break;
514 case CLKID_VPU_1_SEL:
515 parm = &meson_vpu_1_mux_parm;
516 parents = meson_vpu_0_1_mux_parents;
517 break;
518 case CLKID_VAPB_SEL:
519 parm = &meson_vapb_sel_mux_parm;
520 parents = meson_vapb_sel_mux_parents;
521 break;
522 case CLKID_VAPB_0_SEL:
523 parm = &meson_vapb_0_mux_parm;
524 parents = meson_vapb_0_1_mux_parents;
525 break;
526 case CLKID_VAPB_1_SEL:
527 parm = &meson_vapb_1_mux_parm;
528 parents = meson_vapb_0_1_mux_parents;
529 break;
530 case CLKID_HDMI_SEL:
531 parm = &meson_hdmi_mux_parm;
532 parents = meson_hdmi_mux_parents;
533 break;
534 default:
535 /* Not a mux */
536 return -ENOENT;
537 }
538
539 for (i = 0 ; i < (1 << parm->width) ; ++i) {
540 if (parents[i] == parent_id)
541 new_index = i;
542 }
543
544 if (IS_ERR_VALUE(new_index))
545 return new_index;
546
547 debug("%s: new index of %ld is %d\n", __func__, id, new_index);
548
549 regmap_update_bits(priv->map, parm->reg_off,
550 SETPMASK(parm->width, parm->shift),
551 new_index << parm->shift);
552
553 debug("%s: new parent of %ld is %ld\n",
554 __func__, id, meson_mux_get_parent(clk, id));
555
556 return 0;
557}
558
559static ulong meson_mux_get_rate(struct clk *clk, unsigned long id)
560{
561 int parent = meson_mux_get_parent(clk, id);
562
563 if (IS_ERR_VALUE(parent))
564 return parent;
565
566 return meson_clk_get_rate_by_id(clk, parent);
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100567}
568
569static unsigned long meson_clk81_get_rate(struct clk *clk)
570{
571 struct meson_clk *priv = dev_get_priv(clk->dev);
572 unsigned long parent_rate;
573 uint reg;
574 int parents[] = {
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200575 CLKID_XTAL,
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100576 -1,
577 CLKID_FCLK_DIV7,
578 CLKID_MPLL1,
579 CLKID_MPLL2,
580 CLKID_FCLK_DIV4,
581 CLKID_FCLK_DIV3,
582 CLKID_FCLK_DIV5
583 };
584
585 /* mux */
586 regmap_read(priv->map, HHI_MPEG_CLK_CNTL, &reg);
587 reg = (reg >> 12) & 7;
588
589 switch (reg) {
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100590 case 1:
591 return -ENOENT;
592 default:
593 parent_rate = meson_clk_get_rate_by_id(clk, parents[reg]);
594 }
595
596 /* divider */
597 regmap_read(priv->map, HHI_MPEG_CLK_CNTL, &reg);
598 reg = reg & ((1 << 7) - 1);
599
600 return parent_rate / reg;
601}
602
603static long mpll_rate_from_params(unsigned long parent_rate,
604 unsigned long sdm,
605 unsigned long n2)
606{
607 unsigned long divisor = (SDM_DEN * n2) + sdm;
608
609 if (n2 < N2_MIN)
610 return -EINVAL;
611
612 return DIV_ROUND_UP_ULL((u64)parent_rate * SDM_DEN, divisor);
613}
614
615static struct parm meson_mpll0_parm[2] = {
616 {HHI_MPLL_CNTL1, 0, 14}, /* psdm */
617 {HHI_MPLL_CNTL1, 20, 9}, /* pn2 */
618};
619
620static struct parm meson_mpll1_parm[2] = {
621 {HHI_MPLL_CNTL3, 0, 14}, /* psdm */
622 {HHI_MPLL_CNTL3, 20, 9}, /* pn2 */
623};
624
625static struct parm meson_mpll2_parm[2] = {
626 {HHI_MPLL_CNTL5, 0, 14}, /* psdm */
627 {HHI_MPLL_CNTL5, 20, 9}, /* pn2 */
628};
629
630/*
631 * MultiPhase Locked Loops are outputs from a PLL with additional frequency
632 * scaling capabilities. MPLL rates are calculated as:
633 *
634 * f(N2_integer, SDM_IN ) = 2.0G/(N2_integer + SDM_IN/16384)
635 */
636static ulong meson_mpll_get_rate(struct clk *clk, unsigned long id)
637{
638 struct meson_clk *priv = dev_get_priv(clk->dev);
639 struct parm *psdm, *pn2;
640 unsigned long sdm, n2;
641 unsigned long parent_rate;
642 uint reg;
643
644 switch (id) {
645 case CLKID_MPLL0:
646 psdm = &meson_mpll0_parm[0];
647 pn2 = &meson_mpll0_parm[1];
648 break;
649 case CLKID_MPLL1:
650 psdm = &meson_mpll1_parm[0];
651 pn2 = &meson_mpll1_parm[1];
652 break;
653 case CLKID_MPLL2:
654 psdm = &meson_mpll2_parm[0];
655 pn2 = &meson_mpll2_parm[1];
656 break;
657 default:
658 return -ENOENT;
659 }
660
661 parent_rate = meson_clk_get_rate_by_id(clk, CLKID_FIXED_PLL);
662 if (IS_ERR_VALUE(parent_rate))
663 return parent_rate;
664
665 regmap_read(priv->map, psdm->reg_off, &reg);
666 sdm = PARM_GET(psdm->width, psdm->shift, reg);
667
668 regmap_read(priv->map, pn2->reg_off, &reg);
669 n2 = PARM_GET(pn2->width, pn2->shift, reg);
670
671 return mpll_rate_from_params(parent_rate, sdm, n2);
672}
673
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200674static struct parm meson_fixed_pll_parm[4] = {
675 {HHI_FIX_PLL_CNTL0, 0, 9}, /* pm */
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100676 {HHI_FIX_PLL_CNTL0, 10, 5}, /* pn */
677 {HHI_FIX_PLL_CNTL0, 16, 2}, /* pod */
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200678 {HHI_FIX_PLL_CNTL1, 0, 17}, /* pfrac */
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100679};
680
681static struct parm meson_sys_pll_parm[3] = {
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200682 {HHI_SYS_PLL_CNTL0, 0, 9}, /* pm */
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100683 {HHI_SYS_PLL_CNTL0, 10, 5}, /* pn */
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200684 {HHI_SYS_PLL_CNTL0, 16, 3}, /* pod */
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100685};
686
687static ulong meson_pll_get_rate(struct clk *clk, unsigned long id)
688{
689 struct meson_clk *priv = dev_get_priv(clk->dev);
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200690 struct parm *pm, *pn, *pod, *pfrac = NULL;
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100691 unsigned long parent_rate_mhz = XTAL_RATE / 1000000;
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200692 u16 n, m, od, frac;
693 ulong rate;
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100694 uint reg;
695
696 /*
697 * FIXME: Between the unit conversion and the missing frac, we know
698 * rate will be slightly off ...
699 */
700
701 switch (id) {
702 case CLKID_FIXED_PLL:
703 pm = &meson_fixed_pll_parm[0];
704 pn = &meson_fixed_pll_parm[1];
705 pod = &meson_fixed_pll_parm[2];
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200706 pfrac = &meson_fixed_pll_parm[3];
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100707 break;
708 case CLKID_SYS_PLL:
709 pm = &meson_sys_pll_parm[0];
710 pn = &meson_sys_pll_parm[1];
711 pod = &meson_sys_pll_parm[2];
712 break;
713 default:
714 return -ENOENT;
715 }
716
717 regmap_read(priv->map, pn->reg_off, &reg);
718 n = PARM_GET(pn->width, pn->shift, reg);
719
720 regmap_read(priv->map, pm->reg_off, &reg);
721 m = PARM_GET(pm->width, pm->shift, reg);
722
723 regmap_read(priv->map, pod->reg_off, &reg);
724 od = PARM_GET(pod->width, pod->shift, reg);
725
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200726 rate = parent_rate_mhz * m;
727
728 if (pfrac) {
729 ulong frac_rate;
730
731 regmap_read(priv->map, pfrac->reg_off, &reg);
732 frac = PARM_GET(pfrac->width - 1, pfrac->shift, reg);
733
734 frac_rate = DIV_ROUND_UP_ULL((u64)parent_rate_mhz * frac,
735 1 << (pfrac->width - 2));
736
737 if (frac & BIT(pfrac->width - 1))
738 rate -= frac_rate;
739 else
740 rate += frac_rate;
741 }
742
743 return (DIV_ROUND_UP_ULL(rate, n) >> od) * 1000000;
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100744}
745
Neil Armstrongfd6574e2019-05-28 10:50:37 +0200746static struct parm meson_pcie_pll_parm[3] = {
747 {HHI_PCIE_PLL_CNTL0, 0, 8}, /* pm */
748 {HHI_PCIE_PLL_CNTL0, 10, 5}, /* pn */
749 {HHI_PCIE_PLL_CNTL0, 16, 5}, /* pod */
750};
751
752static ulong meson_pcie_pll_get_rate(struct clk *clk)
753{
754 struct meson_clk *priv = dev_get_priv(clk->dev);
755 struct parm *pm, *pn, *pod;
756 unsigned long parent_rate_mhz = XTAL_RATE / 1000000;
757 u16 n, m, od;
758 uint reg;
759
760 pm = &meson_pcie_pll_parm[0];
761 pn = &meson_pcie_pll_parm[1];
762 pod = &meson_pcie_pll_parm[2];
763
764 regmap_read(priv->map, pn->reg_off, &reg);
765 n = PARM_GET(pn->width, pn->shift, reg);
766
767 regmap_read(priv->map, pm->reg_off, &reg);
768 m = PARM_GET(pm->width, pm->shift, reg);
769
770 regmap_read(priv->map, pod->reg_off, &reg);
771 od = PARM_GET(pod->width, pod->shift, reg);
772
773 return ((parent_rate_mhz * m / n) / 2 / od / 2) * 1000000;
774}
775
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100776static ulong meson_clk_get_rate_by_id(struct clk *clk, unsigned long id)
777{
778 ulong rate;
779
780 switch (id) {
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200781 case CLKID_XTAL:
782 rate = XTAL_RATE;
783 break;
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100784 case CLKID_FIXED_PLL:
785 case CLKID_SYS_PLL:
786 rate = meson_pll_get_rate(clk, id);
787 break;
788 case CLKID_FCLK_DIV2:
789 rate = meson_pll_get_rate(clk, CLKID_FIXED_PLL) / 2;
790 break;
791 case CLKID_FCLK_DIV3:
792 rate = meson_pll_get_rate(clk, CLKID_FIXED_PLL) / 3;
793 break;
794 case CLKID_FCLK_DIV4:
795 rate = meson_pll_get_rate(clk, CLKID_FIXED_PLL) / 4;
796 break;
797 case CLKID_FCLK_DIV5:
798 rate = meson_pll_get_rate(clk, CLKID_FIXED_PLL) / 5;
799 break;
800 case CLKID_FCLK_DIV7:
801 rate = meson_pll_get_rate(clk, CLKID_FIXED_PLL) / 7;
802 break;
803 case CLKID_MPLL0:
804 case CLKID_MPLL1:
805 case CLKID_MPLL2:
806 rate = meson_mpll_get_rate(clk, id);
807 break;
808 case CLKID_CLK81:
809 rate = meson_clk81_get_rate(clk);
810 break;
Neil Armstrongfd6574e2019-05-28 10:50:37 +0200811 case CLKID_PCIE_PLL:
812 rate = meson_pcie_pll_get_rate(clk);
Heinrich Schuchardt6572c452020-02-15 21:10:54 +0100813 break;
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200814 case CLKID_VPU_0:
815 rate = meson_div_get_rate(clk, CLKID_VPU_0_DIV);
816 break;
817 case CLKID_VPU_1:
818 rate = meson_div_get_rate(clk, CLKID_VPU_1_DIV);
819 break;
820 case CLKID_VAPB:
821 rate = meson_mux_get_rate(clk, CLKID_VAPB_SEL);
822 break;
823 case CLKID_VAPB_0:
824 rate = meson_div_get_rate(clk, CLKID_VAPB_0_DIV);
825 break;
826 case CLKID_VAPB_1:
827 rate = meson_div_get_rate(clk, CLKID_VAPB_1_DIV);
828 break;
829 case CLKID_HDMI:
830 rate = meson_div_get_rate(clk, CLKID_HDMI_DIV);
831 break;
832 case CLKID_VPU_0_DIV:
833 case CLKID_VPU_1_DIV:
834 case CLKID_VAPB_0_DIV:
835 case CLKID_VAPB_1_DIV:
836 case CLKID_HDMI_DIV:
837 rate = meson_div_get_rate(clk, id);
838 break;
839 case CLKID_VPU:
840 case CLKID_VPU_0_SEL:
841 case CLKID_VPU_1_SEL:
842 case CLKID_VAPB_SEL:
843 case CLKID_VAPB_0_SEL:
844 case CLKID_VAPB_1_SEL:
845 case CLKID_HDMI_SEL:
846 rate = meson_mux_get_rate(clk, id);
Neil Armstrongfd6574e2019-05-28 10:50:37 +0200847 break;
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100848 default:
849 if (gates[id].reg != 0) {
850 /* a clock gate */
851 rate = meson_clk81_get_rate(clk);
852 break;
853 }
854 return -ENOENT;
855 }
856
857 debug("clock %lu has rate %lu\n", id, rate);
858 return rate;
859}
860
861static ulong meson_clk_get_rate(struct clk *clk)
862{
863 return meson_clk_get_rate_by_id(clk, clk->id);
864}
865
Neil Armstrongfd6574e2019-05-28 10:50:37 +0200866static ulong meson_pcie_pll_set_rate(struct clk *clk, ulong rate)
867{
868 struct meson_clk *priv = dev_get_priv(clk->dev);
869
870 regmap_write(priv->map, HHI_PCIE_PLL_CNTL0, 0x20090496);
871 regmap_write(priv->map, HHI_PCIE_PLL_CNTL0, 0x30090496);
872 regmap_write(priv->map, HHI_PCIE_PLL_CNTL1, 0x00000000);
873 regmap_write(priv->map, HHI_PCIE_PLL_CNTL2, 0x00001100);
874 regmap_write(priv->map, HHI_PCIE_PLL_CNTL3, 0x10058e00);
875 regmap_write(priv->map, HHI_PCIE_PLL_CNTL4, 0x000100c0);
876 regmap_write(priv->map, HHI_PCIE_PLL_CNTL5, 0x68000048);
877 regmap_write(priv->map, HHI_PCIE_PLL_CNTL5, 0x68000068);
878 udelay(20);
879 regmap_write(priv->map, HHI_PCIE_PLL_CNTL4, 0x008100c0);
880 udelay(10);
881 regmap_write(priv->map, HHI_PCIE_PLL_CNTL0, 0x34090496);
882 regmap_write(priv->map, HHI_PCIE_PLL_CNTL0, 0x14090496);
883 udelay(10);
884 regmap_write(priv->map, HHI_PCIE_PLL_CNTL2, 0x00001000);
885 regmap_update_bits(priv->map, HHI_PCIE_PLL_CNTL0,
886 0x1f << 16, 9 << 16);
887
888 return 100000000;
889}
890
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200891static int meson_clk_set_parent(struct clk *clk, struct clk *parent)
892{
893 return meson_mux_set_parent(clk, clk->id, parent->id);
894}
895
Neil Armstrongfd6574e2019-05-28 10:50:37 +0200896static ulong meson_clk_set_rate_by_id(struct clk *clk, unsigned long id,
897 ulong rate, ulong current_rate)
898{
899 if (current_rate == rate)
900 return 0;
901
902 switch (id) {
903 /* Fixed clocks */
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200904 case CLKID_FIXED_PLL:
905 case CLKID_SYS_PLL:
906 case CLKID_FCLK_DIV2:
907 case CLKID_FCLK_DIV3:
908 case CLKID_FCLK_DIV4:
909 case CLKID_FCLK_DIV5:
910 case CLKID_FCLK_DIV7:
911 case CLKID_MPLL0:
912 case CLKID_MPLL1:
913 case CLKID_MPLL2:
914 case CLKID_CLK81:
915 if (current_rate != rate)
916 return -EINVAL;
Neil Armstrongfd6574e2019-05-28 10:50:37 +0200917 case CLKID_PCIE_PLL:
918 return meson_pcie_pll_set_rate(clk, rate);
919
Neil Armstrong4371b9c2019-08-30 14:09:22 +0200920 return 0;
921 case CLKID_VPU:
922 return meson_clk_set_rate_by_id(clk,
923 meson_mux_get_parent(clk, CLKID_VPU), rate,
924 current_rate);
925 case CLKID_VAPB:
926 case CLKID_VAPB_SEL:
927 return meson_clk_set_rate_by_id(clk,
928 meson_mux_get_parent(clk, CLKID_VAPB_SEL),
929 rate, current_rate);
930 case CLKID_VPU_0:
931 return meson_div_set_rate(clk, CLKID_VPU_0_DIV, rate,
932 current_rate);
933 case CLKID_VPU_1:
934 return meson_div_set_rate(clk, CLKID_VPU_1_DIV, rate,
935 current_rate);
936 case CLKID_VAPB_0:
937 return meson_div_set_rate(clk, CLKID_VAPB_0_DIV, rate,
938 current_rate);
939 case CLKID_VAPB_1:
940 return meson_div_set_rate(clk, CLKID_VAPB_1_DIV, rate,
941 current_rate);
942 case CLKID_VPU_0_DIV:
943 case CLKID_VPU_1_DIV:
944 case CLKID_VAPB_0_DIV:
945 case CLKID_VAPB_1_DIV:
946 case CLKID_HDMI_DIV:
947 return meson_div_set_rate(clk, id, rate, current_rate);
948 case CLKID_HDMI:
949 return meson_clk_set_rate_by_id(clk, CLKID_HDMI_DIV,
950 rate, current_rate);
Neil Armstrongfd6574e2019-05-28 10:50:37 +0200951 default:
952 return -ENOENT;
953 }
954
955 return -EINVAL;
956}
957
Neil Armstrongfd6574e2019-05-28 10:50:37 +0200958static ulong meson_clk_set_rate(struct clk *clk, ulong rate)
959{
960 ulong current_rate = meson_clk_get_rate_by_id(clk, clk->id);
961 int ret;
962
963 if (IS_ERR_VALUE(current_rate))
964 return current_rate;
965
966 debug("%s: setting rate of %ld from %ld to %ld\n",
967 __func__, clk->id, current_rate, rate);
968
969 ret = meson_clk_set_rate_by_id(clk, clk->id, rate, current_rate);
970 if (IS_ERR_VALUE(ret))
971 return ret;
972
973 debug("clock %lu has new rate %lu\n", clk->id,
974 meson_clk_get_rate_by_id(clk, clk->id));
975
976 return 0;
977}
978
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100979static int meson_clk_probe(struct udevice *dev)
980{
981 struct meson_clk *priv = dev_get_priv(dev);
982
Simon Glassa7ece582020-12-19 10:40:14 -0700983 priv->map = syscon_node_to_regmap(dev_ofnode(dev_get_parent(dev)));
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100984 if (IS_ERR(priv->map))
985 return PTR_ERR(priv->map);
986
Jerome Brunet01c89ea2020-03-05 12:12:37 +0100987 /*
988 * Depending on the boot src, the state of the MMC clock might
989 * be different. Reset it to make sure we won't get stuck
990 */
991 regmap_write(priv->map, HHI_NAND_CLK_CNTL, 0);
992 regmap_write(priv->map, HHI_SD_EMMC_CLK_CNTL, 0);
993
Jerome Brunetd03f5f02019-02-11 16:45:01 +0100994 debug("meson-clk-g12a: probed\n");
995
996 return 0;
997}
998
999static struct clk_ops meson_clk_ops = {
1000 .disable = meson_clk_disable,
1001 .enable = meson_clk_enable,
1002 .get_rate = meson_clk_get_rate,
Neil Armstrong4371b9c2019-08-30 14:09:22 +02001003 .set_parent = meson_clk_set_parent,
Neil Armstrongfd6574e2019-05-28 10:50:37 +02001004 .set_rate = meson_clk_set_rate,
Jerome Brunetd03f5f02019-02-11 16:45:01 +01001005};
1006
1007static const struct udevice_id meson_clk_ids[] = {
1008 { .compatible = "amlogic,g12a-clkc" },
Mark Kettenisb670a5d2019-08-27 23:06:28 +02001009 { .compatible = "amlogic,g12b-clkc" },
Neil Armstrong3e448bd2019-10-11 17:33:53 +02001010 { .compatible = "amlogic,sm1-clkc" },
Jerome Brunetd03f5f02019-02-11 16:45:01 +01001011 { }
1012};
1013
1014U_BOOT_DRIVER(meson_clk_g12a) = {
1015 .name = "meson_clk_g12a",
1016 .id = UCLASS_CLK,
1017 .of_match = meson_clk_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001018 .priv_auto = sizeof(struct meson_clk),
Jerome Brunetd03f5f02019-02-11 16:45:01 +01001019 .ops = &meson_clk_ops,
1020 .probe = meson_clk_probe,
1021};