1 // SPDX-License-Identifier: GPL-2.0
3 * JZ4760 SoC CGU driver
4 * Copyright 2018, Paul Cercueil <paul@crapouillou.net>
7 #include <linux/bitops.h>
8 #include <linux/clk-provider.h>
9 #include <linux/delay.h>
13 #include <linux/clk.h>
15 #include <dt-bindings/clock/ingenic,jz4760-cgu.h>
20 #define MHZ (1000 * 1000)
23 * CPM registers offset address definition
25 #define CGU_REG_CPCCR 0x00
26 #define CGU_REG_LCR 0x04
27 #define CGU_REG_CPPCR0 0x10
28 #define CGU_REG_CLKGR0 0x20
29 #define CGU_REG_OPCR 0x24
30 #define CGU_REG_CLKGR1 0x28
31 #define CGU_REG_CPPCR1 0x30
32 #define CGU_REG_USBPCR 0x3c
33 #define CGU_REG_USBCDR 0x50
34 #define CGU_REG_I2SCDR 0x60
35 #define CGU_REG_LPCDR 0x64
36 #define CGU_REG_MSCCDR 0x68
37 #define CGU_REG_UHCCDR 0x6c
38 #define CGU_REG_SSICDR 0x74
39 #define CGU_REG_CIMCDR 0x7c
40 #define CGU_REG_GPSCDR 0x80
41 #define CGU_REG_PCMCDR 0x84
42 #define CGU_REG_GPUCDR 0x88
44 static const s8 pll_od_encoding
[8] = {
45 0x0, 0x1, -1, 0x2, -1, -1, -1, 0x3,
48 static const u8 jz4760_cgu_cpccr_div_table
[] = {
52 static const u8 jz4760_cgu_pll_half_div_table
[] = {
57 jz4760_cgu_calc_m_n_od(const struct ingenic_cgu_pll_info
*pll_info
,
58 unsigned long rate
, unsigned long parent_rate
,
59 unsigned int *pm
, unsigned int *pn
, unsigned int *pod
)
61 unsigned int m
, n
, od
, m_max
= (1 << pll_info
->m_bits
) - 1;
63 /* The frequency after the N divider must be between 1 and 50 MHz. */
64 n
= parent_rate
/ (1 * MHZ
);
66 /* The N divider must be >= 2. */
67 n
= clamp_val(n
, 2, 1 << pll_info
->n_bits
);
72 for (m
= m_max
; m
>= m_max
&& n
>= 2; n
--) {
73 m
= rate
* n
/ parent_rate
;
83 static const struct ingenic_cgu_clk_info jz4760_cgu_clocks
[] = {
87 [JZ4760_CLK_EXT
] = { "ext", CGU_CLK_EXT
},
88 [JZ4760_CLK_OSC32K
] = { "osc32k", CGU_CLK_EXT
},
94 .parents
= { JZ4760_CLK_EXT
},
96 .reg
= CGU_REG_CPPCR0
,
107 .od_encoding
= pll_od_encoding
,
108 .bypass_reg
= CGU_REG_CPPCR0
,
112 .calc_m_n_od
= jz4760_cgu_calc_m_n_od
,
116 [JZ4760_CLK_PLL1
] = {
117 /* TODO: PLL1 can depend on PLL0 */
119 .parents
= { JZ4760_CLK_EXT
},
121 .reg
= CGU_REG_CPPCR1
,
122 .rate_multiplier
= 1,
132 .od_encoding
= pll_od_encoding
,
136 .calc_m_n_od
= jz4760_cgu_calc_m_n_od
,
142 [JZ4760_CLK_CCLK
] = {
145 * Disabling the CPU clock or any parent clocks will hang the
146 * system; mark it critical.
148 .flags
= CLK_IS_CRITICAL
,
149 .parents
= { JZ4760_CLK_PLL0
, },
151 CGU_REG_CPCCR
, 0, 1, 4, 22, -1, -1, 0,
152 jz4760_cgu_cpccr_div_table
,
155 [JZ4760_CLK_HCLK
] = {
157 .parents
= { JZ4760_CLK_PLL0
, },
159 CGU_REG_CPCCR
, 4, 1, 4, 22, -1, -1, 0,
160 jz4760_cgu_cpccr_div_table
,
163 [JZ4760_CLK_SCLK
] = {
165 .parents
= { JZ4760_CLK_PLL0
, },
167 CGU_REG_CPCCR
, 24, 1, 4, 22, -1, -1, 0,
168 jz4760_cgu_cpccr_div_table
,
171 [JZ4760_CLK_H2CLK
] = {
172 "h2clk", CGU_CLK_DIV
,
173 .parents
= { JZ4760_CLK_PLL0
, },
175 CGU_REG_CPCCR
, 16, 1, 4, 22, -1, -1, 0,
176 jz4760_cgu_cpccr_div_table
,
179 [JZ4760_CLK_MCLK
] = {
182 * Disabling MCLK or its parents will render DRAM
183 * inaccessible; mark it critical.
185 .flags
= CLK_IS_CRITICAL
,
186 .parents
= { JZ4760_CLK_PLL0
, },
188 CGU_REG_CPCCR
, 12, 1, 4, 22, -1, -1, 0,
189 jz4760_cgu_cpccr_div_table
,
192 [JZ4760_CLK_PCLK
] = {
194 .parents
= { JZ4760_CLK_PLL0
, },
196 CGU_REG_CPCCR
, 8, 1, 4, 22, -1, -1, 0,
197 jz4760_cgu_cpccr_div_table
,
203 [JZ4760_CLK_PLL0_HALF
] = {
204 "pll0_half", CGU_CLK_DIV
,
205 .parents
= { JZ4760_CLK_PLL0
},
207 CGU_REG_CPCCR
, 21, 1, 1, 22, -1, -1, 0,
208 jz4760_cgu_pll_half_div_table
,
212 /* Those divided clocks can connect to PLL0 or PLL1 */
215 "uhc", CGU_CLK_DIV
| CGU_CLK_GATE
| CGU_CLK_MUX
,
216 .parents
= { JZ4760_CLK_PLL0_HALF
, JZ4760_CLK_PLL1
, },
217 .mux
= { CGU_REG_UHCCDR
, 31, 1 },
218 .div
= { CGU_REG_UHCCDR
, 0, 1, 4, -1, -1, -1 },
219 .gate
= { CGU_REG_CLKGR0
, 24 },
222 "gpu", CGU_CLK_DIV
| CGU_CLK_GATE
| CGU_CLK_MUX
,
223 .parents
= { JZ4760_CLK_PLL0_HALF
, JZ4760_CLK_PLL1
, },
224 .mux
= { CGU_REG_GPUCDR
, 31, 1 },
225 .div
= { CGU_REG_GPUCDR
, 0, 1, 3, -1, -1, -1 },
226 .gate
= { CGU_REG_CLKGR1
, 9 },
228 [JZ4760_CLK_LPCLK_DIV
] = {
229 "lpclk_div", CGU_CLK_DIV
| CGU_CLK_MUX
,
230 .parents
= { JZ4760_CLK_PLL0_HALF
, JZ4760_CLK_PLL1
, },
231 .mux
= { CGU_REG_LPCDR
, 29, 1 },
232 .div
= { CGU_REG_LPCDR
, 0, 1, 11, -1, -1, -1 },
235 "tve", CGU_CLK_GATE
| CGU_CLK_MUX
,
236 .parents
= { JZ4760_CLK_LPCLK_DIV
, JZ4760_CLK_EXT
, },
237 .mux
= { CGU_REG_LPCDR
, 31, 1 },
238 .gate
= { CGU_REG_CLKGR0
, 27 },
240 [JZ4760_CLK_LPCLK
] = {
241 "lpclk", CGU_CLK_GATE
| CGU_CLK_MUX
,
242 .parents
= { JZ4760_CLK_LPCLK_DIV
, JZ4760_CLK_TVE
, },
243 .mux
= { CGU_REG_LPCDR
, 30, 1 },
244 .gate
= { CGU_REG_CLKGR0
, 28 },
247 "gps", CGU_CLK_DIV
| CGU_CLK_GATE
| CGU_CLK_MUX
,
248 .parents
= { JZ4760_CLK_PLL0_HALF
, JZ4760_CLK_PLL1
, },
249 .mux
= { CGU_REG_GPSCDR
, 31, 1 },
250 .div
= { CGU_REG_GPSCDR
, 0, 1, 4, -1, -1, -1 },
251 .gate
= { CGU_REG_CLKGR0
, 22 },
254 /* Those divided clocks can connect to EXT, PLL0 or PLL1 */
257 "pcm", CGU_CLK_DIV
| CGU_CLK_GATE
| CGU_CLK_MUX
,
258 .parents
= { JZ4760_CLK_EXT
, -1,
259 JZ4760_CLK_PLL0_HALF
, JZ4760_CLK_PLL1
},
260 .mux
= { CGU_REG_PCMCDR
, 30, 2 },
261 .div
= { CGU_REG_PCMCDR
, 0, 1, 9, -1, -1, -1, BIT(0) },
262 .gate
= { CGU_REG_CLKGR1
, 8 },
265 "i2s", CGU_CLK_DIV
| CGU_CLK_MUX
,
266 .parents
= { JZ4760_CLK_EXT
, -1,
267 JZ4760_CLK_PLL0_HALF
, JZ4760_CLK_PLL1
},
268 .mux
= { CGU_REG_I2SCDR
, 30, 2 },
269 .div
= { CGU_REG_I2SCDR
, 0, 1, 9, -1, -1, -1, BIT(0) },
272 "usb", CGU_CLK_DIV
| CGU_CLK_GATE
| CGU_CLK_MUX
,
273 .parents
= { JZ4760_CLK_EXT
, -1,
274 JZ4760_CLK_PLL0_HALF
, JZ4760_CLK_PLL1
},
275 .mux
= { CGU_REG_USBCDR
, 30, 2 },
276 .div
= { CGU_REG_USBCDR
, 0, 1, 8, -1, -1, -1 },
277 .gate
= { CGU_REG_CLKGR0
, 2 },
280 /* Those divided clocks can connect to EXT or PLL0 */
281 [JZ4760_CLK_MMC_MUX
] = {
282 "mmc_mux", CGU_CLK_MUX
| CGU_CLK_DIV
,
283 .parents
= { JZ4760_CLK_EXT
, JZ4760_CLK_PLL0_HALF
, },
284 .mux
= { CGU_REG_MSCCDR
, 31, 1 },
285 .div
= { CGU_REG_MSCCDR
, 0, 1, 6, -1, -1, -1, BIT(0) },
287 [JZ4760_CLK_SSI_MUX
] = {
288 "ssi_mux", CGU_CLK_DIV
| CGU_CLK_MUX
,
289 .parents
= { JZ4760_CLK_EXT
, JZ4760_CLK_PLL0_HALF
, },
290 .mux
= { CGU_REG_SSICDR
, 31, 1 },
291 .div
= { CGU_REG_SSICDR
, 0, 1, 6, -1, -1, -1, BIT(0) },
294 /* These divided clock can connect to PLL0 only */
296 "cim", CGU_CLK_DIV
| CGU_CLK_GATE
,
297 .parents
= { JZ4760_CLK_PLL0_HALF
},
298 .div
= { CGU_REG_CIMCDR
, 0, 1, 8, -1, -1, -1 },
299 .gate
= { CGU_REG_CLKGR0
, 26 },
302 /* Gate-only clocks */
304 [JZ4760_CLK_SSI0
] = {
305 "ssi0", CGU_CLK_GATE
,
306 .parents
= { JZ4760_CLK_SSI_MUX
, },
307 .gate
= { CGU_REG_CLKGR0
, 4 },
309 [JZ4760_CLK_SSI1
] = {
310 "ssi1", CGU_CLK_GATE
,
311 .parents
= { JZ4760_CLK_SSI_MUX
, },
312 .gate
= { CGU_REG_CLKGR0
, 19 },
314 [JZ4760_CLK_SSI2
] = {
315 "ssi2", CGU_CLK_GATE
,
316 .parents
= { JZ4760_CLK_SSI_MUX
, },
317 .gate
= { CGU_REG_CLKGR0
, 20 },
321 .parents
= { JZ4760_CLK_H2CLK
, },
322 .gate
= { CGU_REG_CLKGR0
, 21 },
324 [JZ4760_CLK_MDMA
] = {
325 "mdma", CGU_CLK_GATE
,
326 .parents
= { JZ4760_CLK_HCLK
, },
327 .gate
= { CGU_REG_CLKGR0
, 25 },
329 [JZ4760_CLK_BDMA
] = {
330 "bdma", CGU_CLK_GATE
,
331 .parents
= { JZ4760_CLK_HCLK
, },
332 .gate
= { CGU_REG_CLKGR1
, 0 },
334 [JZ4760_CLK_I2C0
] = {
335 "i2c0", CGU_CLK_GATE
,
336 .parents
= { JZ4760_CLK_EXT
, },
337 .gate
= { CGU_REG_CLKGR0
, 5 },
339 [JZ4760_CLK_I2C1
] = {
340 "i2c1", CGU_CLK_GATE
,
341 .parents
= { JZ4760_CLK_EXT
, },
342 .gate
= { CGU_REG_CLKGR0
, 6 },
344 [JZ4760_CLK_UART0
] = {
345 "uart0", CGU_CLK_GATE
,
346 .parents
= { JZ4760_CLK_EXT
, },
347 .gate
= { CGU_REG_CLKGR0
, 15 },
349 [JZ4760_CLK_UART1
] = {
350 "uart1", CGU_CLK_GATE
,
351 .parents
= { JZ4760_CLK_EXT
, },
352 .gate
= { CGU_REG_CLKGR0
, 16 },
354 [JZ4760_CLK_UART2
] = {
355 "uart2", CGU_CLK_GATE
,
356 .parents
= { JZ4760_CLK_EXT
, },
357 .gate
= { CGU_REG_CLKGR0
, 17 },
359 [JZ4760_CLK_UART3
] = {
360 "uart3", CGU_CLK_GATE
,
361 .parents
= { JZ4760_CLK_EXT
, },
362 .gate
= { CGU_REG_CLKGR0
, 18 },
366 .parents
= { JZ4760_CLK_HCLK
, },
367 .gate
= { CGU_REG_CLKGR0
, 29 },
371 .parents
= { JZ4760_CLK_EXT
, },
372 .gate
= { CGU_REG_CLKGR0
, 14 },
376 .parents
= { JZ4760_CLK_EXT
, },
377 .gate
= { CGU_REG_CLKGR0
, 8 },
381 .parents
= { JZ4760_CLK_HCLK
, },
382 .gate
= { CGU_REG_LCR
, 30, false, 150 },
384 [JZ4760_CLK_MMC0
] = {
385 "mmc0", CGU_CLK_GATE
,
386 .parents
= { JZ4760_CLK_MMC_MUX
, },
387 .gate
= { CGU_REG_CLKGR0
, 3 },
389 [JZ4760_CLK_MMC1
] = {
390 "mmc1", CGU_CLK_GATE
,
391 .parents
= { JZ4760_CLK_MMC_MUX
, },
392 .gate
= { CGU_REG_CLKGR0
, 11 },
394 [JZ4760_CLK_MMC2
] = {
395 "mmc2", CGU_CLK_GATE
,
396 .parents
= { JZ4760_CLK_MMC_MUX
, },
397 .gate
= { CGU_REG_CLKGR0
, 12 },
399 [JZ4760_CLK_UHC_PHY
] = {
400 "uhc_phy", CGU_CLK_GATE
,
401 .parents
= { JZ4760_CLK_UHC
, },
402 .gate
= { CGU_REG_OPCR
, 5 },
404 [JZ4760_CLK_OTG_PHY
] = {
405 "usb_phy", CGU_CLK_GATE
,
406 .parents
= { JZ4760_CLK_OTG
},
407 .gate
= { CGU_REG_OPCR
, 7, true, 50 },
411 [JZ4760_CLK_EXT512
] = {
412 "ext/512", CGU_CLK_FIXDIV
,
413 .parents
= { JZ4760_CLK_EXT
},
418 .parents
= { JZ4760_CLK_EXT512
, JZ4760_CLK_OSC32K
, },
419 .mux
= { CGU_REG_OPCR
, 2, 1},
423 static void __init
jz4760_cgu_init(struct device_node
*np
)
425 struct ingenic_cgu
*cgu
;
428 cgu
= ingenic_cgu_new(jz4760_cgu_clocks
,
429 ARRAY_SIZE(jz4760_cgu_clocks
), np
);
431 pr_err("%s: failed to initialise CGU\n", __func__
);
435 retval
= ingenic_cgu_register_clocks(cgu
);
437 pr_err("%s: failed to register CGU Clocks\n", __func__
);
439 ingenic_cgu_register_syscore_ops(cgu
);
442 /* We only probe via devicetree, no need for a platform driver */
443 CLK_OF_DECLARE_DRIVER(jz4760_cgu
, "ingenic,jz4760-cgu", jz4760_cgu_init
);
445 /* JZ4760B has some small differences, but we don't implement them. */
446 CLK_OF_DECLARE_DRIVER(jz4760b_cgu
, "ingenic,jz4760b-cgu", jz4760_cgu_init
);