1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2019 周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>
7 #include <linux/clk-provider.h>
8 #include <linux/delay.h>
11 #include <linux/rational.h>
13 #include <dt-bindings/clock/ingenic,x1000-cgu.h>
18 /* CGU register offsets */
19 #define CGU_REG_CPCCR 0x00
20 #define CGU_REG_APLL 0x10
21 #define CGU_REG_MPLL 0x14
22 #define CGU_REG_CLKGR 0x20
23 #define CGU_REG_OPCR 0x24
24 #define CGU_REG_DDRCDR 0x2c
25 #define CGU_REG_USBPCR 0x3c
26 #define CGU_REG_USBPCR1 0x48
27 #define CGU_REG_USBCDR 0x50
28 #define CGU_REG_MACCDR 0x54
29 #define CGU_REG_I2SCDR 0x60
30 #define CGU_REG_LPCDR 0x64
31 #define CGU_REG_MSC0CDR 0x68
32 #define CGU_REG_I2SCDR1 0x70
33 #define CGU_REG_SSICDR 0x74
34 #define CGU_REG_CIMCDR 0x7c
35 #define CGU_REG_PCMCDR 0x84
36 #define CGU_REG_MSC1CDR 0xa4
37 #define CGU_REG_CMP_INTR 0xb0
38 #define CGU_REG_CMP_INTRE 0xb4
39 #define CGU_REG_DRCG 0xd0
40 #define CGU_REG_CPCSR 0xd4
41 #define CGU_REG_PCMCDR1 0xe0
42 #define CGU_REG_MACPHYC 0xe8
44 /* bits within the OPCR register */
45 #define OPCR_SPENDN0 BIT(7)
46 #define OPCR_SPENDN1 BIT(6)
48 /* bits within the USBPCR register */
49 #define USBPCR_SIDDQ BIT(21)
50 #define USBPCR_OTG_DISABLE BIT(20)
52 /* bits within the USBPCR1 register */
53 #define USBPCR1_REFCLKSEL_SHIFT 26
54 #define USBPCR1_REFCLKSEL_MASK (0x3 << USBPCR1_REFCLKSEL_SHIFT)
55 #define USBPCR1_REFCLKSEL_CORE (0x2 << USBPCR1_REFCLKSEL_SHIFT)
56 #define USBPCR1_REFCLKDIV_SHIFT 24
57 #define USBPCR1_REFCLKDIV_MASK (0x3 << USBPCR1_REFCLKDIV_SHIFT)
58 #define USBPCR1_REFCLKDIV_48 (0x2 << USBPCR1_REFCLKDIV_SHIFT)
59 #define USBPCR1_REFCLKDIV_24 (0x1 << USBPCR1_REFCLKDIV_SHIFT)
60 #define USBPCR1_REFCLKDIV_12 (0x0 << USBPCR1_REFCLKDIV_SHIFT)
62 static struct ingenic_cgu
*cgu
;
64 static unsigned long x1000_otg_phy_recalc_rate(struct clk_hw
*hw
,
65 unsigned long parent_rate
)
70 usbpcr1
= readl(cgu
->base
+ CGU_REG_USBPCR1
);
71 refclk_div
= usbpcr1
& USBPCR1_REFCLKDIV_MASK
;
74 case USBPCR1_REFCLKDIV_12
:
77 case USBPCR1_REFCLKDIV_24
:
80 case USBPCR1_REFCLKDIV_48
:
87 static long x1000_otg_phy_round_rate(struct clk_hw
*hw
, unsigned long req_rate
,
88 unsigned long *parent_rate
)
90 if (req_rate
< 18000000)
93 if (req_rate
< 36000000)
99 static int x1000_otg_phy_set_rate(struct clk_hw
*hw
, unsigned long req_rate
,
100 unsigned long parent_rate
)
103 u32 usbpcr1
, div_bits
;
107 div_bits
= USBPCR1_REFCLKDIV_12
;
111 div_bits
= USBPCR1_REFCLKDIV_24
;
115 div_bits
= USBPCR1_REFCLKDIV_48
;
122 spin_lock_irqsave(&cgu
->lock
, flags
);
124 usbpcr1
= readl(cgu
->base
+ CGU_REG_USBPCR1
);
125 usbpcr1
&= ~USBPCR1_REFCLKDIV_MASK
;
127 writel(usbpcr1
, cgu
->base
+ CGU_REG_USBPCR1
);
129 spin_unlock_irqrestore(&cgu
->lock
, flags
);
133 static int x1000_usb_phy_enable(struct clk_hw
*hw
)
135 void __iomem
*reg_opcr
= cgu
->base
+ CGU_REG_OPCR
;
136 void __iomem
*reg_usbpcr
= cgu
->base
+ CGU_REG_USBPCR
;
138 writel(readl(reg_opcr
) | OPCR_SPENDN0
, reg_opcr
);
139 writel(readl(reg_usbpcr
) & ~USBPCR_OTG_DISABLE
& ~USBPCR_SIDDQ
, reg_usbpcr
);
143 static void x1000_usb_phy_disable(struct clk_hw
*hw
)
145 void __iomem
*reg_opcr
= cgu
->base
+ CGU_REG_OPCR
;
146 void __iomem
*reg_usbpcr
= cgu
->base
+ CGU_REG_USBPCR
;
148 writel(readl(reg_opcr
) & ~OPCR_SPENDN0
, reg_opcr
);
149 writel(readl(reg_usbpcr
) | USBPCR_OTG_DISABLE
| USBPCR_SIDDQ
, reg_usbpcr
);
152 static int x1000_usb_phy_is_enabled(struct clk_hw
*hw
)
154 void __iomem
*reg_opcr
= cgu
->base
+ CGU_REG_OPCR
;
155 void __iomem
*reg_usbpcr
= cgu
->base
+ CGU_REG_USBPCR
;
157 return (readl(reg_opcr
) & OPCR_SPENDN0
) &&
158 !(readl(reg_usbpcr
) & USBPCR_SIDDQ
) &&
159 !(readl(reg_usbpcr
) & USBPCR_OTG_DISABLE
);
162 static const struct clk_ops x1000_otg_phy_ops
= {
163 .recalc_rate
= x1000_otg_phy_recalc_rate
,
164 .round_rate
= x1000_otg_phy_round_rate
,
165 .set_rate
= x1000_otg_phy_set_rate
,
167 .enable
= x1000_usb_phy_enable
,
168 .disable
= x1000_usb_phy_disable
,
169 .is_enabled
= x1000_usb_phy_is_enabled
,
173 x1000_i2spll_calc_m_n_od(const struct ingenic_cgu_pll_info
*pll_info
,
174 unsigned long rate
, unsigned long parent_rate
,
175 unsigned int *pm
, unsigned int *pn
, unsigned int *pod
)
177 const unsigned long m_max
= GENMASK(pll_info
->m_bits
- 1, 0);
178 const unsigned long n_max
= GENMASK(pll_info
->n_bits
- 1, 0);
181 rational_best_approximation(rate
, parent_rate
, m_max
, n_max
, &m
, &n
);
183 /* n should not be less than 2*m */
193 x1000_i2spll_set_rate_hook(const struct ingenic_cgu_pll_info
*pll_info
,
194 unsigned long rate
, unsigned long parent_rate
)
197 * Writing 0 causes I2SCDR1.I2SDIV_D to be automatically recalculated
198 * based on the current value of I2SCDR.I2SDIV_N, which is needed for
199 * the divider to function correctly.
201 writel(0, cgu
->base
+ CGU_REG_I2SCDR1
);
204 static const s8 pll_od_encoding
[8] = {
205 0x0, 0x1, -1, 0x2, -1, -1, -1, 0x3,
208 static const struct ingenic_cgu_clk_info x1000_cgu_clocks
[] = {
210 /* External clocks */
212 [X1000_CLK_EXCLK
] = { "ext", CGU_CLK_EXT
},
213 [X1000_CLK_RTCLK
] = { "rtc", CGU_CLK_EXT
},
219 .parents
= { X1000_CLK_EXCLK
},
222 .rate_multiplier
= 1,
232 .od_encoding
= pll_od_encoding
,
233 .bypass_reg
= CGU_REG_APLL
,
242 .parents
= { X1000_CLK_EXCLK
},
245 .rate_multiplier
= 1,
255 .od_encoding
= pll_od_encoding
,
256 .bypass_reg
= CGU_REG_MPLL
,
263 /* Custom (SoC-specific) OTG PHY */
265 [X1000_CLK_OTGPHY
] = {
266 "otg_phy", CGU_CLK_CUSTOM
,
267 .parents
= { -1, -1, X1000_CLK_EXCLK
, -1 },
268 .custom
= { &x1000_otg_phy_ops
},
271 /* Muxes & dividers */
273 [X1000_CLK_SCLKA
] = {
274 "sclk_a", CGU_CLK_MUX
,
275 .parents
= { -1, X1000_CLK_EXCLK
, X1000_CLK_APLL
, -1 },
276 .mux
= { CGU_REG_CPCCR
, 30, 2 },
279 [X1000_CLK_CPUMUX
] = {
280 "cpu_mux", CGU_CLK_MUX
,
281 .parents
= { -1, X1000_CLK_SCLKA
, X1000_CLK_MPLL
, -1 },
282 .mux
= { CGU_REG_CPCCR
, 28, 2 },
286 "cpu", CGU_CLK_DIV
| CGU_CLK_GATE
,
288 * Disabling the CPU clock or any parent clocks will hang the
289 * system; mark it critical.
291 .flags
= CLK_IS_CRITICAL
,
292 .parents
= { X1000_CLK_CPUMUX
},
293 .div
= { CGU_REG_CPCCR
, 0, 1, 4, 22, -1, -1 },
294 .gate
= { CGU_REG_CLKGR
, 30 },
297 [X1000_CLK_L2CACHE
] = {
298 "l2cache", CGU_CLK_DIV
,
300 * The L2 cache clock is critical if caches are enabled and
301 * disabling it or any parent clocks will hang the system.
303 .flags
= CLK_IS_CRITICAL
,
304 .parents
= { X1000_CLK_CPUMUX
},
305 .div
= { CGU_REG_CPCCR
, 4, 1, 4, 22, -1, -1 },
309 "ahb0", CGU_CLK_MUX
| CGU_CLK_DIV
,
310 .parents
= { -1, X1000_CLK_SCLKA
, X1000_CLK_MPLL
, -1 },
311 .mux
= { CGU_REG_CPCCR
, 26, 2 },
312 .div
= { CGU_REG_CPCCR
, 8, 1, 4, 21, -1, -1 },
315 [X1000_CLK_AHB2PMUX
] = {
316 "ahb2_apb_mux", CGU_CLK_MUX
,
317 .parents
= { -1, X1000_CLK_SCLKA
, X1000_CLK_MPLL
, -1 },
318 .mux
= { CGU_REG_CPCCR
, 24, 2 },
323 .parents
= { X1000_CLK_AHB2PMUX
},
324 .div
= { CGU_REG_CPCCR
, 12, 1, 4, 20, -1, -1 },
328 "pclk", CGU_CLK_DIV
| CGU_CLK_GATE
,
329 .parents
= { X1000_CLK_AHB2PMUX
},
330 .div
= { CGU_REG_CPCCR
, 16, 1, 4, 20, -1, -1 },
331 .gate
= { CGU_REG_CLKGR
, 28 },
335 "ddr", CGU_CLK_MUX
| CGU_CLK_DIV
| CGU_CLK_GATE
,
337 * Disabling DDR clock or its parents will render DRAM
338 * inaccessible; mark it critical.
340 .flags
= CLK_IS_CRITICAL
,
341 .parents
= { -1, X1000_CLK_SCLKA
, X1000_CLK_MPLL
, -1 },
342 .mux
= { CGU_REG_DDRCDR
, 30, 2 },
343 .div
= { CGU_REG_DDRCDR
, 0, 1, 4, 29, 28, 27 },
344 .gate
= { CGU_REG_CLKGR
, 31 },
348 "mac", CGU_CLK_MUX
| CGU_CLK_DIV
| CGU_CLK_GATE
,
349 .parents
= { X1000_CLK_SCLKA
, X1000_CLK_MPLL
},
350 .mux
= { CGU_REG_MACCDR
, 31, 1 },
351 .div
= { CGU_REG_MACCDR
, 0, 1, 8, 29, 28, 27 },
352 .gate
= { CGU_REG_CLKGR
, 25 },
355 [X1000_CLK_I2SPLLMUX
] = {
356 "i2s_pll_mux", CGU_CLK_MUX
,
357 .parents
= { X1000_CLK_SCLKA
, X1000_CLK_MPLL
},
358 .mux
= { CGU_REG_I2SCDR
, 31, 1 },
361 [X1000_CLK_I2SPLL
] = {
362 "i2s_pll", CGU_CLK_PLL
,
363 .parents
= { X1000_CLK_I2SPLLMUX
},
365 .reg
= CGU_REG_I2SCDR
,
366 .rate_multiplier
= 1,
371 .calc_m_n_od
= x1000_i2spll_calc_m_n_od
,
372 .set_rate_hook
= x1000_i2spll_set_rate_hook
,
378 .parents
= { X1000_CLK_EXCLK
, -1, -1, X1000_CLK_I2SPLL
},
380 * NOTE: the mux is at bit 30; bit 29 enables the M/N divider.
381 * Therefore, the divider is disabled when EXCLK is selected.
383 .mux
= { CGU_REG_I2SCDR
, 29, 2 },
387 "lcd", CGU_CLK_MUX
| CGU_CLK_DIV
| CGU_CLK_GATE
,
388 .parents
= { X1000_CLK_SCLKA
, X1000_CLK_MPLL
},
389 .mux
= { CGU_REG_LPCDR
, 31, 1 },
390 .div
= { CGU_REG_LPCDR
, 0, 1, 8, 28, 27, 26 },
391 .gate
= { CGU_REG_CLKGR
, 23 },
394 [X1000_CLK_MSCMUX
] = {
395 "msc_mux", CGU_CLK_MUX
,
396 .parents
= { X1000_CLK_SCLKA
, X1000_CLK_MPLL
},
397 .mux
= { CGU_REG_MSC0CDR
, 31, 1 },
401 "msc0", CGU_CLK_DIV
| CGU_CLK_GATE
,
402 .parents
= { X1000_CLK_MSCMUX
},
403 .div
= { CGU_REG_MSC0CDR
, 0, 2, 8, 29, 28, 27 },
404 .gate
= { CGU_REG_CLKGR
, 4 },
408 "msc1", CGU_CLK_DIV
| CGU_CLK_GATE
,
409 .parents
= { X1000_CLK_MSCMUX
, -1, -1, -1 },
410 .div
= { CGU_REG_MSC1CDR
, 0, 2, 8, 29, 28, 27 },
411 .gate
= { CGU_REG_CLKGR
, 5 },
415 "otg", CGU_CLK_DIV
| CGU_CLK_GATE
| CGU_CLK_MUX
,
416 .parents
= { X1000_CLK_EXCLK
, -1, X1000_CLK_APLL
, X1000_CLK_MPLL
},
417 .mux
= { CGU_REG_USBCDR
, 30, 2 },
418 .div
= { CGU_REG_USBCDR
, 0, 1, 8, 29, 28, 27 },
419 .gate
= { CGU_REG_CLKGR
, 3 },
422 [X1000_CLK_SSIPLL
] = {
423 "ssi_pll", CGU_CLK_MUX
| CGU_CLK_DIV
,
424 .parents
= { X1000_CLK_SCLKA
, X1000_CLK_MPLL
},
425 .mux
= { CGU_REG_SSICDR
, 31, 1 },
426 .div
= { CGU_REG_SSICDR
, 0, 1, 8, 29, 28, 27 },
429 [X1000_CLK_SSIPLL_DIV2
] = {
430 "ssi_pll_div2", CGU_CLK_FIXDIV
,
431 .parents
= { X1000_CLK_SSIPLL
},
435 [X1000_CLK_SSIMUX
] = {
436 "ssi_mux", CGU_CLK_MUX
,
437 .parents
= { X1000_CLK_EXCLK
, X1000_CLK_SSIPLL_DIV2
},
438 .mux
= { CGU_REG_SSICDR
, 30, 1 },
441 [X1000_CLK_EXCLK_DIV512
] = {
442 "exclk_div512", CGU_CLK_FIXDIV
,
443 .parents
= { X1000_CLK_EXCLK
},
448 "rtc_ercs", CGU_CLK_MUX
| CGU_CLK_GATE
,
449 .parents
= { X1000_CLK_EXCLK_DIV512
, X1000_CLK_RTCLK
},
450 .mux
= { CGU_REG_OPCR
, 2, 1},
451 .gate
= { CGU_REG_CLKGR
, 27 },
454 /* Gate-only clocks */
458 .parents
= { X1000_CLK_AHB2
},
459 .gate
= { CGU_REG_CLKGR
, 0 },
462 [X1000_CLK_EFUSE
] = {
463 "efuse", CGU_CLK_GATE
,
464 .parents
= { X1000_CLK_AHB2
},
465 .gate
= { CGU_REG_CLKGR
, 1 },
470 .parents
= { X1000_CLK_SSIPLL
},
471 .gate
= { CGU_REG_CLKGR
, 2 },
475 "i2c0", CGU_CLK_GATE
,
476 .parents
= { X1000_CLK_PCLK
},
477 .gate
= { CGU_REG_CLKGR
, 7 },
481 "i2c1", CGU_CLK_GATE
,
482 .parents
= { X1000_CLK_PCLK
},
483 .gate
= { CGU_REG_CLKGR
, 8 },
487 "i2c2", CGU_CLK_GATE
,
488 .parents
= { X1000_CLK_PCLK
},
489 .gate
= { CGU_REG_CLKGR
, 9 },
494 .parents
= { X1000_CLK_EXCLK
},
495 .gate
= { CGU_REG_CLKGR
, 11 },
498 [X1000_CLK_UART0
] = {
499 "uart0", CGU_CLK_GATE
,
500 .parents
= { X1000_CLK_EXCLK
},
501 .gate
= { CGU_REG_CLKGR
, 14 },
504 [X1000_CLK_UART1
] = {
505 "uart1", CGU_CLK_GATE
,
506 .parents
= { X1000_CLK_EXCLK
},
507 .gate
= { CGU_REG_CLKGR
, 15 },
510 [X1000_CLK_UART2
] = {
511 "uart2", CGU_CLK_GATE
,
512 .parents
= { X1000_CLK_EXCLK
},
513 .gate
= { CGU_REG_CLKGR
, 16 },
518 .parents
= { X1000_CLK_EXCLK
},
519 .gate
= { CGU_REG_CLKGR
, 18 },
524 .parents
= { X1000_CLK_SSIMUX
},
525 .gate
= { CGU_REG_CLKGR
, 19 },
530 .parents
= { X1000_CLK_EXCLK
},
531 .gate
= { CGU_REG_CLKGR
, 20 },
535 "pdma", CGU_CLK_GATE
,
536 .parents
= { X1000_CLK_EXCLK
},
537 .gate
= { CGU_REG_CLKGR
, 21 },
541 static void __init
x1000_cgu_init(struct device_node
*np
)
545 cgu
= ingenic_cgu_new(x1000_cgu_clocks
,
546 ARRAY_SIZE(x1000_cgu_clocks
), np
);
548 pr_err("%s: failed to initialise CGU\n", __func__
);
552 retval
= ingenic_cgu_register_clocks(cgu
);
554 pr_err("%s: failed to register CGU Clocks\n", __func__
);
558 ingenic_cgu_register_syscore_ops(cgu
);
561 * CGU has some children devices, this is useful for probing children devices
562 * in the case where the device node is compatible with "simple-mfd".
564 CLK_OF_DECLARE_DRIVER(x1000_cgu
, "ingenic,x1000-cgu", x1000_cgu_init
);