1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2018 - Beniamino Galvani <b.galvani@gmail.com>
4 * (C) Copyright 2018 - BayLibre, SAS
5 * Author: Neil Armstrong <narmstrong@baylibre.com>
10 #include <asm/arch/clock-g12a.h>
12 #include <clk-uclass.h>
17 #include <dt-bindings/clock/g12a-clkc.h>
18 #include <linux/err.h>
19 #include <linux/kernel.h>
20 #include "clk_meson.h"
22 /* This driver support only basic clock tree operations :
23 * - Can calculate clock frequency on a limited tree
24 * - Can Read muxes and basic dividers (0-based only)
25 * - Can enable/disable gates with limited propagation
26 * - Can reparent without propagation, only on muxes
27 * - Can set rates without reparenting
28 * This driver is adapted to what is actually supported by U-Boot
31 /* Only the clocks ids we don't want to expose, such as the internal muxes
32 * and dividers of composite clocks, will remain defined here.
34 #define CLKID_MPEG_SEL 8
35 #define CLKID_MPEG_DIV 9
36 #define CLKID_SD_EMMC_A_CLK0_SEL 63
37 #define CLKID_SD_EMMC_A_CLK0_DIV 64
38 #define CLKID_SD_EMMC_B_CLK0_SEL 65
39 #define CLKID_SD_EMMC_B_CLK0_DIV 66
40 #define CLKID_SD_EMMC_C_CLK0_SEL 67
41 #define CLKID_SD_EMMC_C_CLK0_DIV 68
42 #define CLKID_MPLL0_DIV 69
43 #define CLKID_MPLL1_DIV 70
44 #define CLKID_MPLL2_DIV 71
45 #define CLKID_MPLL3_DIV 72
46 #define CLKID_MPLL_PREDIV 73
47 #define CLKID_FCLK_DIV2_DIV 75
48 #define CLKID_FCLK_DIV3_DIV 76
49 #define CLKID_FCLK_DIV4_DIV 77
50 #define CLKID_FCLK_DIV5_DIV 78
51 #define CLKID_FCLK_DIV7_DIV 79
52 #define CLKID_FCLK_DIV2P5_DIV 100
53 #define CLKID_FIXED_PLL_DCO 101
54 #define CLKID_SYS_PLL_DCO 102
55 #define CLKID_GP0_PLL_DCO 103
56 #define CLKID_HIFI_PLL_DCO 104
57 #define CLKID_VPU_0_DIV 111
58 #define CLKID_VPU_1_DIV 114
59 #define CLKID_VAPB_0_DIV 118
60 #define CLKID_VAPB_1_DIV 121
61 #define CLKID_HDMI_PLL_DCO 125
62 #define CLKID_HDMI_PLL_OD 126
63 #define CLKID_HDMI_PLL_OD2 127
64 #define CLKID_VID_PLL_SEL 130
65 #define CLKID_VID_PLL_DIV 131
66 #define CLKID_VCLK_SEL 132
67 #define CLKID_VCLK2_SEL 133
68 #define CLKID_VCLK_INPUT 134
69 #define CLKID_VCLK2_INPUT 135
70 #define CLKID_VCLK_DIV 136
71 #define CLKID_VCLK2_DIV 137
72 #define CLKID_VCLK_DIV2_EN 140
73 #define CLKID_VCLK_DIV4_EN 141
74 #define CLKID_VCLK_DIV6_EN 142
75 #define CLKID_VCLK_DIV12_EN 143
76 #define CLKID_VCLK2_DIV2_EN 144
77 #define CLKID_VCLK2_DIV4_EN 145
78 #define CLKID_VCLK2_DIV6_EN 146
79 #define CLKID_VCLK2_DIV12_EN 147
80 #define CLKID_CTS_ENCI_SEL 158
81 #define CLKID_CTS_ENCP_SEL 159
82 #define CLKID_CTS_VDAC_SEL 160
83 #define CLKID_HDMI_TX_SEL 161
84 #define CLKID_HDMI_SEL 166
85 #define CLKID_HDMI_DIV 167
86 #define CLKID_MALI_0_DIV 170
87 #define CLKID_MALI_1_DIV 173
89 #define CLKID_XTAL 0x10000000
91 #define XTAL_RATE 24000000
97 static ulong meson_div_get_rate(struct clk *clk, unsigned long id);
98 static ulong meson_div_set_rate(struct clk *clk, unsigned long id, ulong rate,
100 static ulong meson_mux_set_parent(struct clk *clk, unsigned long id,
101 unsigned long parent_id);
102 static ulong meson_mux_get_rate(struct clk *clk, unsigned long id);
103 static ulong meson_clk_set_rate_by_id(struct clk *clk, unsigned long id,
104 ulong rate, ulong current_rate);
105 static ulong meson_mux_get_parent(struct clk *clk, unsigned long id);
106 static ulong meson_clk_get_rate_by_id(struct clk *clk, unsigned long id);
110 static struct meson_gate gates[NUM_CLKS] = {
111 /* Everything Else (EE) domain gates */
112 MESON_GATE(CLKID_SPICC0, HHI_GCLK_MPEG0, 8),
113 MESON_GATE(CLKID_I2C, HHI_GCLK_MPEG0, 9),
114 MESON_GATE(CLKID_UART0, HHI_GCLK_MPEG0, 13),
115 MESON_GATE(CLKID_SPICC1, HHI_GCLK_MPEG0, 14),
116 MESON_GATE(CLKID_SD_EMMC_A, HHI_GCLK_MPEG0, 4),
117 MESON_GATE(CLKID_SD_EMMC_B, HHI_GCLK_MPEG0, 25),
118 MESON_GATE(CLKID_SD_EMMC_C, HHI_GCLK_MPEG0, 26),
119 MESON_GATE(CLKID_ETH, HHI_GCLK_MPEG1, 3),
120 MESON_GATE(CLKID_UART1, HHI_GCLK_MPEG1, 16),
121 MESON_GATE(CLKID_USB, HHI_GCLK_MPEG1, 25),
122 MESON_GATE(CLKID_HTX_PCLK, HHI_GCLK_MPEG2, 4),
123 MESON_GATE(CLKID_USB1_DDR_BRIDGE, HHI_GCLK_MPEG2, 8),
124 MESON_GATE(CLKID_VPU_INTR, HHI_GCLK_MPEG2, 25),
126 /* Peripheral Gates */
127 MESON_GATE(CLKID_FCLK_DIV2, HHI_FIX_PLL_CNTL1, 24),
128 MESON_GATE(CLKID_FCLK_DIV3, HHI_FIX_PLL_CNTL1, 20),
129 MESON_GATE(CLKID_FCLK_DIV4, HHI_FIX_PLL_CNTL1, 21),
130 MESON_GATE(CLKID_FCLK_DIV5, HHI_FIX_PLL_CNTL1, 22),
131 MESON_GATE(CLKID_FCLK_DIV7, HHI_FIX_PLL_CNTL1, 23),
132 MESON_GATE(CLKID_SD_EMMC_A_CLK0, HHI_SD_EMMC_CLK_CNTL, 7),
133 MESON_GATE(CLKID_SD_EMMC_B_CLK0, HHI_SD_EMMC_CLK_CNTL, 23),
134 MESON_GATE(CLKID_SD_EMMC_C_CLK0, HHI_NAND_CLK_CNTL, 7),
135 MESON_GATE(CLKID_VPU_0, HHI_VPU_CLK_CNTL, 8),
136 MESON_GATE(CLKID_VPU_1, HHI_VPU_CLK_CNTL, 24),
137 MESON_GATE(CLKID_VAPB_0, HHI_VAPBCLK_CNTL, 8),
138 MESON_GATE(CLKID_VAPB_1, HHI_VAPBCLK_CNTL, 24),
139 MESON_GATE(CLKID_VAPB, HHI_VAPBCLK_CNTL, 30),
140 MESON_GATE(CLKID_HDMI, HHI_HDMI_CLK_CNTL, 8),
143 static int meson_set_gate_by_id(struct clk *clk, unsigned long id, bool on)
145 struct meson_clk *priv = dev_get_priv(clk->dev);
146 struct meson_gate *gate;
148 debug("%s: %sabling %ld\n", __func__, on ? "en" : "dis", id);
150 /* Propagate through muxes */
153 return meson_set_gate_by_id(clk,
154 meson_mux_get_parent(clk, CLKID_VPU), on);
156 return meson_set_gate_by_id(clk,
157 meson_mux_get_parent(clk, CLKID_VAPB_SEL), on);
160 if (id >= ARRAY_SIZE(gates))
168 debug("%s: really %sabling %ld\n", __func__, on ? "en" : "dis", id);
170 regmap_update_bits(priv->map, gate->reg,
171 BIT(gate->bit), on ? BIT(gate->bit) : 0);
173 /* Propagate to next gate(s) */
176 return meson_set_gate_by_id(clk, CLKID_VAPB_SEL, on);
178 return meson_set_gate_by_id(clk,
179 meson_mux_get_parent(clk, CLKID_VAPB_0_SEL), on);
181 return meson_set_gate_by_id(clk,
182 meson_mux_get_parent(clk, CLKID_VAPB_0_SEL), on);
184 return meson_set_gate_by_id(clk,
185 meson_mux_get_parent(clk, CLKID_VPU_0_SEL), on);
187 return meson_set_gate_by_id(clk,
188 meson_mux_get_parent(clk, CLKID_VPU_1_SEL), on);
194 static int meson_clk_enable(struct clk *clk)
196 return meson_set_gate_by_id(clk, clk->id, true);
199 static int meson_clk_disable(struct clk *clk)
201 return meson_set_gate_by_id(clk, clk->id, false);
204 static struct parm meson_vpu_0_div_parm = {
205 HHI_VPU_CLK_CNTL, 0, 7,
208 int meson_vpu_0_div_parent = CLKID_VPU_0_SEL;
210 static struct parm meson_vpu_1_div_parm = {
211 HHI_VPU_CLK_CNTL, 16, 7,
214 int meson_vpu_1_div_parent = CLKID_VPU_1_SEL;
216 static struct parm meson_vapb_0_div_parm = {
217 HHI_VAPBCLK_CNTL, 0, 7,
220 int meson_vapb_0_div_parent = CLKID_VAPB_0_SEL;
222 static struct parm meson_vapb_1_div_parm = {
223 HHI_VAPBCLK_CNTL, 16, 7,
226 int meson_vapb_1_div_parent = CLKID_VAPB_1_SEL;
228 static struct parm meson_hdmi_div_parm = {
229 HHI_HDMI_CLK_CNTL, 0, 7,
232 int meson_hdmi_div_parent = CLKID_HDMI_SEL;
234 static ulong meson_div_get_rate(struct clk *clk, unsigned long id)
236 struct meson_clk *priv = dev_get_priv(clk->dev);
237 unsigned int rate, parent_rate;
243 case CLKID_VPU_0_DIV:
244 parm = &meson_vpu_0_div_parm;
245 parent = meson_vpu_0_div_parent;
247 case CLKID_VPU_1_DIV:
248 parm = &meson_vpu_1_div_parm;
249 parent = meson_vpu_1_div_parent;
251 case CLKID_VAPB_0_DIV:
252 parm = &meson_vapb_0_div_parm;
253 parent = meson_vapb_0_div_parent;
255 case CLKID_VAPB_1_DIV:
256 parm = &meson_vapb_1_div_parm;
257 parent = meson_vapb_1_div_parent;
260 parm = &meson_hdmi_div_parm;
261 parent = meson_hdmi_div_parent;
267 regmap_read(priv->map, parm->reg_off, ®);
268 reg = PARM_GET(parm->width, parm->shift, reg);
270 debug("%s: div of %ld is %d\n", __func__, id, reg + 1);
272 parent_rate = meson_clk_get_rate_by_id(clk, parent);
273 if (IS_ERR_VALUE(parent_rate))
276 debug("%s: parent rate of %ld is %d\n", __func__, id, parent_rate);
278 rate = parent_rate / (reg + 1);
280 debug("%s: rate of %ld is %d\n", __func__, id, rate);
285 static ulong meson_div_set_rate(struct clk *clk, unsigned long id, ulong rate,
288 struct meson_clk *priv = dev_get_priv(clk->dev);
289 unsigned int new_div = -EINVAL;
290 unsigned long parent_rate;
295 if (current_rate == rate)
298 debug("%s: setting rate of %ld from %ld to %ld\n",
299 __func__, id, current_rate, rate);
302 case CLKID_VPU_0_DIV:
303 parm = &meson_vpu_0_div_parm;
304 parent = meson_vpu_0_div_parent;
306 case CLKID_VPU_1_DIV:
307 parm = &meson_vpu_1_div_parm;
308 parent = meson_vpu_1_div_parent;
310 case CLKID_VAPB_0_DIV:
311 parm = &meson_vapb_0_div_parm;
312 parent = meson_vapb_0_div_parent;
314 case CLKID_VAPB_1_DIV:
315 parm = &meson_vapb_1_div_parm;
316 parent = meson_vapb_1_div_parent;
319 parm = &meson_hdmi_div_parm;
320 parent = meson_hdmi_div_parent;
326 parent_rate = meson_clk_get_rate_by_id(clk, parent);
327 if (IS_ERR_VALUE(parent_rate))
330 debug("%s: parent rate of %ld is %ld\n", __func__, id, parent_rate);
332 /* If can't divide, set parent instead */
333 if (!parent_rate || rate > parent_rate)
334 return meson_clk_set_rate_by_id(clk, parent, rate,
337 new_div = DIV_ROUND_CLOSEST(parent_rate, rate);
339 debug("%s: new div of %ld is %d\n", __func__, id, new_div);
341 /* If overflow, try to set parent rate and retry */
342 if (!new_div || new_div > (1 << parm->width)) {
343 ret = meson_clk_set_rate_by_id(clk, parent, rate, current_rate);
344 if (IS_ERR_VALUE(ret))
347 parent_rate = meson_clk_get_rate_by_id(clk, parent);
348 if (IS_ERR_VALUE(parent_rate))
351 new_div = DIV_ROUND_CLOSEST(parent_rate, rate);
353 debug("%s: new new div of %ld is %d\n", __func__, id, new_div);
355 if (!new_div || new_div > (1 << parm->width))
359 debug("%s: setting div of %ld to %d\n", __func__, id, new_div);
361 regmap_update_bits(priv->map, parm->reg_off,
362 SETPMASK(parm->width, parm->shift),
363 (new_div - 1) << parm->shift);
365 debug("%s: new rate of %ld is %ld\n",
366 __func__, id, meson_div_get_rate(clk, id));
371 static struct parm meson_vpu_mux_parm = {
372 HHI_VPU_CLK_CNTL, 31, 1,
375 int meson_vpu_mux_parents[] = {
380 static struct parm meson_vpu_0_mux_parm = {
381 HHI_VPU_CLK_CNTL, 9, 3,
384 static struct parm meson_vpu_1_mux_parm = {
385 HHI_VPU_CLK_CNTL, 25, 3,
388 static int meson_vpu_0_1_mux_parents[] = {
399 static struct parm meson_vapb_sel_mux_parm = {
400 HHI_VAPBCLK_CNTL, 31, 1,
403 int meson_vapb_sel_mux_parents[] = {
408 static struct parm meson_vapb_0_mux_parm = {
409 HHI_VAPBCLK_CNTL, 9, 2,
412 static struct parm meson_vapb_1_mux_parm = {
413 HHI_VAPBCLK_CNTL, 25, 2,
416 static int meson_vapb_0_1_mux_parents[] = {
423 static struct parm meson_hdmi_mux_parm = {
424 HHI_HDMI_CLK_CNTL, 9, 2,
427 static int meson_hdmi_mux_parents[] = {
434 static ulong meson_mux_get_parent(struct clk *clk, unsigned long id)
436 struct meson_clk *priv = dev_get_priv(clk->dev);
443 parm = &meson_vpu_mux_parm;
444 parents = meson_vpu_mux_parents;
446 case CLKID_VPU_0_SEL:
447 parm = &meson_vpu_0_mux_parm;
448 parents = meson_vpu_0_1_mux_parents;
450 case CLKID_VPU_1_SEL:
451 parm = &meson_vpu_1_mux_parm;
452 parents = meson_vpu_0_1_mux_parents;
455 parm = &meson_vapb_sel_mux_parm;
456 parents = meson_vapb_sel_mux_parents;
458 case CLKID_VAPB_0_SEL:
459 parm = &meson_vapb_0_mux_parm;
460 parents = meson_vapb_0_1_mux_parents;
462 case CLKID_VAPB_1_SEL:
463 parm = &meson_vapb_1_mux_parm;
464 parents = meson_vapb_0_1_mux_parents;
467 parm = &meson_hdmi_mux_parm;
468 parents = meson_hdmi_mux_parents;
474 regmap_read(priv->map, parm->reg_off, ®);
475 reg = PARM_GET(parm->width, parm->shift, reg);
477 debug("%s: parent of %ld is %d (%d)\n",
478 __func__, id, parents[reg], reg);
483 static ulong meson_mux_set_parent(struct clk *clk, unsigned long id,
484 unsigned long parent_id)
486 unsigned long cur_parent = meson_mux_get_parent(clk, id);
487 struct meson_clk *priv = dev_get_priv(clk->dev);
488 unsigned int new_index = -EINVAL;
493 if (IS_ERR_VALUE(cur_parent))
496 debug("%s: setting parent of %ld from %ld to %ld\n",
497 __func__, id, cur_parent, parent_id);
499 if (cur_parent == parent_id)
504 parm = &meson_vpu_mux_parm;
505 parents = meson_vpu_mux_parents;
507 case CLKID_VPU_0_SEL:
508 parm = &meson_vpu_0_mux_parm;
509 parents = meson_vpu_0_1_mux_parents;
511 case CLKID_VPU_1_SEL:
512 parm = &meson_vpu_1_mux_parm;
513 parents = meson_vpu_0_1_mux_parents;
516 parm = &meson_vapb_sel_mux_parm;
517 parents = meson_vapb_sel_mux_parents;
519 case CLKID_VAPB_0_SEL:
520 parm = &meson_vapb_0_mux_parm;
521 parents = meson_vapb_0_1_mux_parents;
523 case CLKID_VAPB_1_SEL:
524 parm = &meson_vapb_1_mux_parm;
525 parents = meson_vapb_0_1_mux_parents;
528 parm = &meson_hdmi_mux_parm;
529 parents = meson_hdmi_mux_parents;
536 for (i = 0 ; i < (1 << parm->width) ; ++i) {
537 if (parents[i] == parent_id)
541 if (IS_ERR_VALUE(new_index))
544 debug("%s: new index of %ld is %d\n", __func__, id, new_index);
546 regmap_update_bits(priv->map, parm->reg_off,
547 SETPMASK(parm->width, parm->shift),
548 new_index << parm->shift);
550 debug("%s: new parent of %ld is %ld\n",
551 __func__, id, meson_mux_get_parent(clk, id));
556 static ulong meson_mux_get_rate(struct clk *clk, unsigned long id)
558 int parent = meson_mux_get_parent(clk, id);
560 if (IS_ERR_VALUE(parent))
563 return meson_clk_get_rate_by_id(clk, parent);
566 static unsigned long meson_clk81_get_rate(struct clk *clk)
568 struct meson_clk *priv = dev_get_priv(clk->dev);
569 unsigned long parent_rate;
583 regmap_read(priv->map, HHI_MPEG_CLK_CNTL, ®);
584 reg = (reg >> 12) & 7;
590 parent_rate = meson_clk_get_rate_by_id(clk, parents[reg]);
594 regmap_read(priv->map, HHI_MPEG_CLK_CNTL, ®);
595 reg = reg & ((1 << 7) - 1);
597 return parent_rate / reg;
600 static long mpll_rate_from_params(unsigned long parent_rate,
604 unsigned long divisor = (SDM_DEN * n2) + sdm;
609 return DIV_ROUND_UP_ULL((u64)parent_rate * SDM_DEN, divisor);
612 static struct parm meson_mpll0_parm[2] = {
613 {HHI_MPLL_CNTL1, 0, 14}, /* psdm */
614 {HHI_MPLL_CNTL1, 20, 9}, /* pn2 */
617 static struct parm meson_mpll1_parm[2] = {
618 {HHI_MPLL_CNTL3, 0, 14}, /* psdm */
619 {HHI_MPLL_CNTL3, 20, 9}, /* pn2 */
622 static struct parm meson_mpll2_parm[2] = {
623 {HHI_MPLL_CNTL5, 0, 14}, /* psdm */
624 {HHI_MPLL_CNTL5, 20, 9}, /* pn2 */
628 * MultiPhase Locked Loops are outputs from a PLL with additional frequency
629 * scaling capabilities. MPLL rates are calculated as:
631 * f(N2_integer, SDM_IN ) = 2.0G/(N2_integer + SDM_IN/16384)
633 static ulong meson_mpll_get_rate(struct clk *clk, unsigned long id)
635 struct meson_clk *priv = dev_get_priv(clk->dev);
636 struct parm *psdm, *pn2;
637 unsigned long sdm, n2;
638 unsigned long parent_rate;
643 psdm = &meson_mpll0_parm[0];
644 pn2 = &meson_mpll0_parm[1];
647 psdm = &meson_mpll1_parm[0];
648 pn2 = &meson_mpll1_parm[1];
651 psdm = &meson_mpll2_parm[0];
652 pn2 = &meson_mpll2_parm[1];
658 parent_rate = meson_clk_get_rate_by_id(clk, CLKID_FIXED_PLL);
659 if (IS_ERR_VALUE(parent_rate))
662 regmap_read(priv->map, psdm->reg_off, ®);
663 sdm = PARM_GET(psdm->width, psdm->shift, reg);
665 regmap_read(priv->map, pn2->reg_off, ®);
666 n2 = PARM_GET(pn2->width, pn2->shift, reg);
668 return mpll_rate_from_params(parent_rate, sdm, n2);
671 static struct parm meson_fixed_pll_parm[4] = {
672 {HHI_FIX_PLL_CNTL0, 0, 9}, /* pm */
673 {HHI_FIX_PLL_CNTL0, 10, 5}, /* pn */
674 {HHI_FIX_PLL_CNTL0, 16, 2}, /* pod */
675 {HHI_FIX_PLL_CNTL1, 0, 17}, /* pfrac */
678 static struct parm meson_sys_pll_parm[3] = {
679 {HHI_SYS_PLL_CNTL0, 0, 9}, /* pm */
680 {HHI_SYS_PLL_CNTL0, 10, 5}, /* pn */
681 {HHI_SYS_PLL_CNTL0, 16, 3}, /* pod */
684 static ulong meson_pll_get_rate(struct clk *clk, unsigned long id)
686 struct meson_clk *priv = dev_get_priv(clk->dev);
687 struct parm *pm, *pn, *pod, *pfrac = NULL;
688 unsigned long parent_rate_mhz = XTAL_RATE / 1000000;
694 * FIXME: Between the unit conversion and the missing frac, we know
695 * rate will be slightly off ...
699 case CLKID_FIXED_PLL:
700 pm = &meson_fixed_pll_parm[0];
701 pn = &meson_fixed_pll_parm[1];
702 pod = &meson_fixed_pll_parm[2];
703 pfrac = &meson_fixed_pll_parm[3];
706 pm = &meson_sys_pll_parm[0];
707 pn = &meson_sys_pll_parm[1];
708 pod = &meson_sys_pll_parm[2];
714 regmap_read(priv->map, pn->reg_off, ®);
715 n = PARM_GET(pn->width, pn->shift, reg);
717 regmap_read(priv->map, pm->reg_off, ®);
718 m = PARM_GET(pm->width, pm->shift, reg);
720 regmap_read(priv->map, pod->reg_off, ®);
721 od = PARM_GET(pod->width, pod->shift, reg);
723 rate = parent_rate_mhz * m;
728 regmap_read(priv->map, pfrac->reg_off, ®);
729 frac = PARM_GET(pfrac->width - 1, pfrac->shift, reg);
731 frac_rate = DIV_ROUND_UP_ULL((u64)parent_rate_mhz * frac,
732 1 << (pfrac->width - 2));
734 if (frac & BIT(pfrac->width - 1))
740 return (DIV_ROUND_UP_ULL(rate, n) >> od) * 1000000;
743 static struct parm meson_pcie_pll_parm[3] = {
744 {HHI_PCIE_PLL_CNTL0, 0, 8}, /* pm */
745 {HHI_PCIE_PLL_CNTL0, 10, 5}, /* pn */
746 {HHI_PCIE_PLL_CNTL0, 16, 5}, /* pod */
749 static ulong meson_pcie_pll_get_rate(struct clk *clk)
751 struct meson_clk *priv = dev_get_priv(clk->dev);
752 struct parm *pm, *pn, *pod;
753 unsigned long parent_rate_mhz = XTAL_RATE / 1000000;
757 pm = &meson_pcie_pll_parm[0];
758 pn = &meson_pcie_pll_parm[1];
759 pod = &meson_pcie_pll_parm[2];
761 regmap_read(priv->map, pn->reg_off, ®);
762 n = PARM_GET(pn->width, pn->shift, reg);
764 regmap_read(priv->map, pm->reg_off, ®);
765 m = PARM_GET(pm->width, pm->shift, reg);
767 regmap_read(priv->map, pod->reg_off, ®);
768 od = PARM_GET(pod->width, pod->shift, reg);
770 return ((parent_rate_mhz * m / n) / 2 / od / 2) * 1000000;
773 static ulong meson_clk_get_rate_by_id(struct clk *clk, unsigned long id)
781 case CLKID_FIXED_PLL:
783 rate = meson_pll_get_rate(clk, id);
785 case CLKID_FCLK_DIV2:
786 rate = meson_pll_get_rate(clk, CLKID_FIXED_PLL) / 2;
788 case CLKID_FCLK_DIV3:
789 rate = meson_pll_get_rate(clk, CLKID_FIXED_PLL) / 3;
791 case CLKID_FCLK_DIV4:
792 rate = meson_pll_get_rate(clk, CLKID_FIXED_PLL) / 4;
794 case CLKID_FCLK_DIV5:
795 rate = meson_pll_get_rate(clk, CLKID_FIXED_PLL) / 5;
797 case CLKID_FCLK_DIV7:
798 rate = meson_pll_get_rate(clk, CLKID_FIXED_PLL) / 7;
803 rate = meson_mpll_get_rate(clk, id);
806 rate = meson_clk81_get_rate(clk);
809 rate = meson_pcie_pll_get_rate(clk);
812 rate = meson_div_get_rate(clk, CLKID_VPU_0_DIV);
815 rate = meson_div_get_rate(clk, CLKID_VPU_1_DIV);
818 rate = meson_mux_get_rate(clk, CLKID_VAPB_SEL);
821 rate = meson_div_get_rate(clk, CLKID_VAPB_0_DIV);
824 rate = meson_div_get_rate(clk, CLKID_VAPB_1_DIV);
827 rate = meson_div_get_rate(clk, CLKID_HDMI_DIV);
829 case CLKID_VPU_0_DIV:
830 case CLKID_VPU_1_DIV:
831 case CLKID_VAPB_0_DIV:
832 case CLKID_VAPB_1_DIV:
834 rate = meson_div_get_rate(clk, id);
837 case CLKID_VPU_0_SEL:
838 case CLKID_VPU_1_SEL:
840 case CLKID_VAPB_0_SEL:
841 case CLKID_VAPB_1_SEL:
843 rate = meson_mux_get_rate(clk, id);
846 if (gates[id].reg != 0) {
848 rate = meson_clk81_get_rate(clk);
854 debug("clock %lu has rate %lu\n", id, rate);
858 static ulong meson_clk_get_rate(struct clk *clk)
860 return meson_clk_get_rate_by_id(clk, clk->id);
863 static ulong meson_pcie_pll_set_rate(struct clk *clk, ulong rate)
865 struct meson_clk *priv = dev_get_priv(clk->dev);
867 regmap_write(priv->map, HHI_PCIE_PLL_CNTL0, 0x20090496);
868 regmap_write(priv->map, HHI_PCIE_PLL_CNTL0, 0x30090496);
869 regmap_write(priv->map, HHI_PCIE_PLL_CNTL1, 0x00000000);
870 regmap_write(priv->map, HHI_PCIE_PLL_CNTL2, 0x00001100);
871 regmap_write(priv->map, HHI_PCIE_PLL_CNTL3, 0x10058e00);
872 regmap_write(priv->map, HHI_PCIE_PLL_CNTL4, 0x000100c0);
873 regmap_write(priv->map, HHI_PCIE_PLL_CNTL5, 0x68000048);
874 regmap_write(priv->map, HHI_PCIE_PLL_CNTL5, 0x68000068);
876 regmap_write(priv->map, HHI_PCIE_PLL_CNTL4, 0x008100c0);
878 regmap_write(priv->map, HHI_PCIE_PLL_CNTL0, 0x34090496);
879 regmap_write(priv->map, HHI_PCIE_PLL_CNTL0, 0x14090496);
881 regmap_write(priv->map, HHI_PCIE_PLL_CNTL2, 0x00001000);
882 regmap_update_bits(priv->map, HHI_PCIE_PLL_CNTL0,
883 0x1f << 16, 9 << 16);
888 static int meson_clk_set_parent(struct clk *clk, struct clk *parent)
890 return meson_mux_set_parent(clk, clk->id, parent->id);
893 static ulong meson_clk_set_rate_by_id(struct clk *clk, unsigned long id,
894 ulong rate, ulong current_rate)
896 if (current_rate == rate)
901 case CLKID_FIXED_PLL:
903 case CLKID_FCLK_DIV2:
904 case CLKID_FCLK_DIV3:
905 case CLKID_FCLK_DIV4:
906 case CLKID_FCLK_DIV5:
907 case CLKID_FCLK_DIV7:
912 if (current_rate != rate)
915 return meson_pcie_pll_set_rate(clk, rate);
919 return meson_clk_set_rate_by_id(clk,
920 meson_mux_get_parent(clk, CLKID_VPU), rate,
924 return meson_clk_set_rate_by_id(clk,
925 meson_mux_get_parent(clk, CLKID_VAPB_SEL),
928 return meson_div_set_rate(clk, CLKID_VPU_0_DIV, rate,
931 return meson_div_set_rate(clk, CLKID_VPU_1_DIV, rate,
934 return meson_div_set_rate(clk, CLKID_VAPB_0_DIV, rate,
937 return meson_div_set_rate(clk, CLKID_VAPB_1_DIV, rate,
939 case CLKID_VPU_0_DIV:
940 case CLKID_VPU_1_DIV:
941 case CLKID_VAPB_0_DIV:
942 case CLKID_VAPB_1_DIV:
944 return meson_div_set_rate(clk, id, rate, current_rate);
946 return meson_clk_set_rate_by_id(clk, CLKID_HDMI_DIV,
955 static ulong meson_clk_set_rate(struct clk *clk, ulong rate)
957 ulong current_rate = meson_clk_get_rate_by_id(clk, clk->id);
960 if (IS_ERR_VALUE(current_rate))
963 debug("%s: setting rate of %ld from %ld to %ld\n",
964 __func__, clk->id, current_rate, rate);
966 ret = meson_clk_set_rate_by_id(clk, clk->id, rate, current_rate);
967 if (IS_ERR_VALUE(ret))
970 debug("clock %lu has new rate %lu\n", clk->id,
971 meson_clk_get_rate_by_id(clk, clk->id));
976 static int meson_clk_probe(struct udevice *dev)
978 struct meson_clk *priv = dev_get_priv(dev);
980 priv->map = syscon_node_to_regmap(dev_get_parent(dev)->node);
981 if (IS_ERR(priv->map))
982 return PTR_ERR(priv->map);
985 * Depending on the boot src, the state of the MMC clock might
986 * be different. Reset it to make sure we won't get stuck
988 regmap_write(priv->map, HHI_NAND_CLK_CNTL, 0);
989 regmap_write(priv->map, HHI_SD_EMMC_CLK_CNTL, 0);
991 debug("meson-clk-g12a: probed\n");
996 static struct clk_ops meson_clk_ops = {
997 .disable = meson_clk_disable,
998 .enable = meson_clk_enable,
999 .get_rate = meson_clk_get_rate,
1000 .set_parent = meson_clk_set_parent,
1001 .set_rate = meson_clk_set_rate,
1004 static const struct udevice_id meson_clk_ids[] = {
1005 { .compatible = "amlogic,g12a-clkc" },
1006 { .compatible = "amlogic,g12b-clkc" },
1007 { .compatible = "amlogic,sm1-clkc" },
1011 U_BOOT_DRIVER(meson_clk_g12a) = {
1012 .name = "meson_clk_g12a",
1014 .of_match = meson_clk_ids,
1015 .priv_auto_alloc_size = sizeof(struct meson_clk),
1016 .ops = &meson_clk_ops,
1017 .probe = meson_clk_probe,