2 * Copyright 2013 Freescale Semiconductor, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * clock driver for Freescale QorIQ SoCs.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/clk.h>
14 #include <linux/clk-provider.h>
15 #include <linux/fsl/guts.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/of_address.h>
20 #include <linux/of_platform.h>
22 #include <linux/slab.h>
29 #define PLATFORM_PLL 0
33 #define CGA_PLL4 4 /* only on clockgen-1.0, which lacks CGB */
37 struct clockgen_pll_div
{
43 struct clockgen_pll_div div
[4];
46 #define CLKSEL_VALID 1
47 #define CLKSEL_80PCT 2 /* Only allowed if PLL <= 80% of max cpu freq */
49 struct clockgen_sourceinfo
{
50 u32 flags
; /* CLKSEL_xxx */
51 int pll
; /* CGx_PLLn */
52 int div
; /* PLL_DIVn */
55 #define NUM_MUX_PARENTS 16
57 struct clockgen_muxinfo
{
58 struct clockgen_sourceinfo clksel
[NUM_MUX_PARENTS
];
67 * cmux freq must be >= platform pll.
68 * If not set, cmux freq must be >= platform pll/2
70 #define CG_CMUX_GE_PLAT 1
72 #define CG_PLL_8BIT 2 /* PLLCnGSR[CFG] is 8 bits, not 6 */
73 #define CG_VER3 4 /* version 3 cg: reg layout different */
74 #define CG_LITTLE_ENDIAN 8
76 struct clockgen_chipinfo
{
77 const char *compat
, *guts_compat
;
78 const struct clockgen_muxinfo
*cmux_groups
[2];
79 const struct clockgen_muxinfo
*hwaccel
[NUM_HWACCEL
];
80 void (*init_periph
)(struct clockgen
*cg
);
81 int cmux_to_group
[NUM_CMUX
]; /* -1 terminates if fewer than NUM_CMUX */
82 u32 pll_mask
; /* 1 << n bit set if PLL n is valid */
83 u32 flags
; /* CG_xxx */
87 struct device_node
*node
;
89 struct clockgen_chipinfo info
; /* mutable copy */
91 struct clockgen_pll pll
[6];
92 struct clk
*cmux
[NUM_CMUX
];
93 struct clk
*hwaccel
[NUM_HWACCEL
];
95 struct ccsr_guts __iomem
*guts
;
98 static struct clockgen clockgen
;
100 static void cg_out(struct clockgen
*cg
, u32 val
, u32 __iomem
*reg
)
102 if (cg
->info
.flags
& CG_LITTLE_ENDIAN
)
105 iowrite32be(val
, reg
);
108 static u32
cg_in(struct clockgen
*cg
, u32 __iomem
*reg
)
112 if (cg
->info
.flags
& CG_LITTLE_ENDIAN
)
115 val
= ioread32be(reg
);
120 static const struct clockgen_muxinfo p2041_cmux_grp1
= {
122 [0] = { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV1
},
123 [1] = { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV2
},
124 [4] = { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV1
},
128 static const struct clockgen_muxinfo p2041_cmux_grp2
= {
130 [0] = { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV1
},
131 [4] = { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV1
},
132 [5] = { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV2
},
136 static const struct clockgen_muxinfo p5020_cmux_grp1
= {
138 [0] = { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV1
},
139 [1] = { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV2
},
140 [4] = { CLKSEL_VALID
| CLKSEL_80PCT
, CGA_PLL2
, PLL_DIV1
},
144 static const struct clockgen_muxinfo p5020_cmux_grp2
= {
146 [0] = { CLKSEL_VALID
| CLKSEL_80PCT
, CGA_PLL1
, PLL_DIV1
},
147 [4] = { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV1
},
148 [5] = { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV2
},
152 static const struct clockgen_muxinfo p5040_cmux_grp1
= {
154 [0] = { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV1
},
155 [1] = { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV2
},
156 [4] = { CLKSEL_VALID
| CLKSEL_80PCT
, CGA_PLL2
, PLL_DIV1
},
157 [5] = { CLKSEL_VALID
| CLKSEL_80PCT
, CGA_PLL2
, PLL_DIV2
},
161 static const struct clockgen_muxinfo p5040_cmux_grp2
= {
163 [0] = { CLKSEL_VALID
| CLKSEL_80PCT
, CGA_PLL1
, PLL_DIV1
},
164 [1] = { CLKSEL_VALID
| CLKSEL_80PCT
, CGA_PLL1
, PLL_DIV2
},
165 [4] = { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV1
},
166 [5] = { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV2
},
170 static const struct clockgen_muxinfo p4080_cmux_grp1
= {
172 [0] = { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV1
},
173 [1] = { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV2
},
174 [4] = { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV1
},
175 [5] = { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV2
},
176 [8] = { CLKSEL_VALID
| CLKSEL_80PCT
, CGA_PLL3
, PLL_DIV1
},
180 static const struct clockgen_muxinfo p4080_cmux_grp2
= {
182 [0] = { CLKSEL_VALID
| CLKSEL_80PCT
, CGA_PLL1
, PLL_DIV1
},
183 [8] = { CLKSEL_VALID
, CGA_PLL3
, PLL_DIV1
},
184 [9] = { CLKSEL_VALID
, CGA_PLL3
, PLL_DIV2
},
185 [12] = { CLKSEL_VALID
, CGA_PLL4
, PLL_DIV1
},
186 [13] = { CLKSEL_VALID
, CGA_PLL4
, PLL_DIV2
},
190 static const struct clockgen_muxinfo t1023_cmux
= {
192 [0] = { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV1
},
193 [1] = { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV2
},
197 static const struct clockgen_muxinfo t1040_cmux
= {
199 [0] = { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV1
},
200 [1] = { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV2
},
201 [4] = { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV1
},
202 [5] = { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV2
},
207 static const struct clockgen_muxinfo clockgen2_cmux_cga
= {
209 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV1
},
210 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV2
},
211 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV4
},
213 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV1
},
214 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV2
},
215 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV4
},
217 { CLKSEL_VALID
, CGA_PLL3
, PLL_DIV1
},
218 { CLKSEL_VALID
, CGA_PLL3
, PLL_DIV2
},
219 { CLKSEL_VALID
, CGA_PLL3
, PLL_DIV4
},
223 static const struct clockgen_muxinfo clockgen2_cmux_cga12
= {
225 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV1
},
226 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV2
},
227 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV4
},
229 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV1
},
230 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV2
},
231 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV4
},
235 static const struct clockgen_muxinfo clockgen2_cmux_cgb
= {
237 { CLKSEL_VALID
, CGB_PLL1
, PLL_DIV1
},
238 { CLKSEL_VALID
, CGB_PLL1
, PLL_DIV2
},
239 { CLKSEL_VALID
, CGB_PLL1
, PLL_DIV4
},
241 { CLKSEL_VALID
, CGB_PLL2
, PLL_DIV1
},
242 { CLKSEL_VALID
, CGB_PLL2
, PLL_DIV2
},
243 { CLKSEL_VALID
, CGB_PLL2
, PLL_DIV4
},
247 static const struct clockgen_muxinfo ls1043a_hwa1
= {
251 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV2
},
252 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV3
},
255 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV2
},
256 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV3
},
260 static const struct clockgen_muxinfo ls1043a_hwa2
= {
263 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV1
},
265 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV3
},
269 static const struct clockgen_muxinfo t1023_hwa1
= {
272 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV1
},
273 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV2
},
274 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV3
},
278 static const struct clockgen_muxinfo t1023_hwa2
= {
280 [6] = { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV2
},
284 static const struct clockgen_muxinfo t2080_hwa1
= {
287 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV1
},
288 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV2
},
289 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV3
},
290 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV4
},
291 { CLKSEL_VALID
, PLATFORM_PLL
, PLL_DIV1
},
292 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV2
},
293 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV3
},
297 static const struct clockgen_muxinfo t2080_hwa2
= {
300 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV1
},
301 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV2
},
302 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV3
},
303 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV4
},
304 { CLKSEL_VALID
, PLATFORM_PLL
, PLL_DIV1
},
305 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV2
},
306 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV3
},
310 static const struct clockgen_muxinfo t4240_hwa1
= {
312 { CLKSEL_VALID
, PLATFORM_PLL
, PLL_DIV2
},
313 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV1
},
314 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV2
},
315 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV3
},
316 { CLKSEL_VALID
, CGA_PLL1
, PLL_DIV4
},
318 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV2
},
319 { CLKSEL_VALID
, CGA_PLL2
, PLL_DIV3
},
323 static const struct clockgen_muxinfo t4240_hwa4
= {
325 [2] = { CLKSEL_VALID
, CGB_PLL1
, PLL_DIV2
},
326 [3] = { CLKSEL_VALID
, CGB_PLL1
, PLL_DIV3
},
327 [4] = { CLKSEL_VALID
, CGB_PLL1
, PLL_DIV4
},
328 [5] = { CLKSEL_VALID
, PLATFORM_PLL
, PLL_DIV1
},
329 [6] = { CLKSEL_VALID
, CGB_PLL2
, PLL_DIV2
},
333 static const struct clockgen_muxinfo t4240_hwa5
= {
335 [2] = { CLKSEL_VALID
, CGB_PLL2
, PLL_DIV2
},
336 [3] = { CLKSEL_VALID
, CGB_PLL2
, PLL_DIV3
},
337 [4] = { CLKSEL_VALID
, CGB_PLL2
, PLL_DIV4
},
338 [5] = { CLKSEL_VALID
, PLATFORM_PLL
, PLL_DIV1
},
339 [6] = { CLKSEL_VALID
, CGB_PLL1
, PLL_DIV2
},
340 [7] = { CLKSEL_VALID
, CGB_PLL1
, PLL_DIV3
},
344 #define RCWSR7_FM1_CLK_SEL 0x40000000
345 #define RCWSR7_FM2_CLK_SEL 0x20000000
346 #define RCWSR7_HWA_ASYNC_DIV 0x04000000
348 static void __init
p2041_init_periph(struct clockgen
*cg
)
352 reg
= ioread32be(&cg
->guts
->rcwsr
[7]);
354 if (reg
& RCWSR7_FM1_CLK_SEL
)
355 cg
->fman
[0] = cg
->pll
[CGA_PLL2
].div
[PLL_DIV2
].clk
;
357 cg
->fman
[0] = cg
->pll
[PLATFORM_PLL
].div
[PLL_DIV2
].clk
;
360 static void __init
p4080_init_periph(struct clockgen
*cg
)
364 reg
= ioread32be(&cg
->guts
->rcwsr
[7]);
366 if (reg
& RCWSR7_FM1_CLK_SEL
)
367 cg
->fman
[0] = cg
->pll
[CGA_PLL3
].div
[PLL_DIV2
].clk
;
369 cg
->fman
[0] = cg
->pll
[PLATFORM_PLL
].div
[PLL_DIV2
].clk
;
371 if (reg
& RCWSR7_FM2_CLK_SEL
)
372 cg
->fman
[1] = cg
->pll
[CGA_PLL3
].div
[PLL_DIV2
].clk
;
374 cg
->fman
[1] = cg
->pll
[PLATFORM_PLL
].div
[PLL_DIV2
].clk
;
377 static void __init
p5020_init_periph(struct clockgen
*cg
)
382 reg
= ioread32be(&cg
->guts
->rcwsr
[7]);
383 if (reg
& RCWSR7_HWA_ASYNC_DIV
)
386 if (reg
& RCWSR7_FM1_CLK_SEL
)
387 cg
->fman
[0] = cg
->pll
[CGA_PLL2
].div
[div
].clk
;
389 cg
->fman
[0] = cg
->pll
[PLATFORM_PLL
].div
[PLL_DIV2
].clk
;
392 static void __init
p5040_init_periph(struct clockgen
*cg
)
397 reg
= ioread32be(&cg
->guts
->rcwsr
[7]);
398 if (reg
& RCWSR7_HWA_ASYNC_DIV
)
401 if (reg
& RCWSR7_FM1_CLK_SEL
)
402 cg
->fman
[0] = cg
->pll
[CGA_PLL3
].div
[div
].clk
;
404 cg
->fman
[0] = cg
->pll
[PLATFORM_PLL
].div
[PLL_DIV2
].clk
;
406 if (reg
& RCWSR7_FM2_CLK_SEL
)
407 cg
->fman
[1] = cg
->pll
[CGA_PLL3
].div
[div
].clk
;
409 cg
->fman
[1] = cg
->pll
[PLATFORM_PLL
].div
[PLL_DIV2
].clk
;
412 static void __init
t1023_init_periph(struct clockgen
*cg
)
414 cg
->fman
[0] = cg
->hwaccel
[1];
417 static void __init
t1040_init_periph(struct clockgen
*cg
)
419 cg
->fman
[0] = cg
->pll
[PLATFORM_PLL
].div
[PLL_DIV1
].clk
;
422 static void __init
t2080_init_periph(struct clockgen
*cg
)
424 cg
->fman
[0] = cg
->hwaccel
[0];
427 static void __init
t4240_init_periph(struct clockgen
*cg
)
429 cg
->fman
[0] = cg
->hwaccel
[3];
430 cg
->fman
[1] = cg
->hwaccel
[4];
433 static const struct clockgen_chipinfo chipinfo
[] = {
435 .compat
= "fsl,b4420-clockgen",
436 .guts_compat
= "fsl,b4860-device-config",
437 .init_periph
= t2080_init_periph
,
439 &clockgen2_cmux_cga12
, &clockgen2_cmux_cgb
448 .flags
= CG_PLL_8BIT
,
451 .compat
= "fsl,b4860-clockgen",
452 .guts_compat
= "fsl,b4860-device-config",
453 .init_periph
= t2080_init_periph
,
455 &clockgen2_cmux_cga12
, &clockgen2_cmux_cgb
464 .flags
= CG_PLL_8BIT
,
467 .compat
= "fsl,ls1021a-clockgen",
477 .compat
= "fsl,ls1043a-clockgen",
478 .init_periph
= t2080_init_periph
,
483 &ls1043a_hwa1
, &ls1043a_hwa2
489 .flags
= CG_PLL_8BIT
,
492 .compat
= "fsl,ls2080a-clockgen",
494 &clockgen2_cmux_cga12
, &clockgen2_cmux_cgb
500 .flags
= CG_VER3
| CG_LITTLE_ENDIAN
,
503 .compat
= "fsl,p2041-clockgen",
504 .guts_compat
= "fsl,qoriq-device-config-1.0",
505 .init_periph
= p2041_init_periph
,
507 &p2041_cmux_grp1
, &p2041_cmux_grp2
515 .compat
= "fsl,p3041-clockgen",
516 .guts_compat
= "fsl,qoriq-device-config-1.0",
517 .init_periph
= p2041_init_periph
,
519 &p2041_cmux_grp1
, &p2041_cmux_grp2
527 .compat
= "fsl,p4080-clockgen",
528 .guts_compat
= "fsl,qoriq-device-config-1.0",
529 .init_periph
= p4080_init_periph
,
531 &p4080_cmux_grp1
, &p4080_cmux_grp2
534 0, 0, 0, 0, 1, 1, 1, 1
539 .compat
= "fsl,p5020-clockgen",
540 .guts_compat
= "fsl,qoriq-device-config-1.0",
541 .init_periph
= p5020_init_periph
,
543 &p2041_cmux_grp1
, &p2041_cmux_grp2
551 .compat
= "fsl,p5040-clockgen",
552 .guts_compat
= "fsl,p5040-device-config",
553 .init_periph
= p5040_init_periph
,
555 &p5040_cmux_grp1
, &p5040_cmux_grp2
563 .compat
= "fsl,t1023-clockgen",
564 .guts_compat
= "fsl,t1023-device-config",
565 .init_periph
= t1023_init_periph
,
570 &t1023_hwa1
, &t1023_hwa2
576 .flags
= CG_PLL_8BIT
,
579 .compat
= "fsl,t1040-clockgen",
580 .guts_compat
= "fsl,t1040-device-config",
581 .init_periph
= t1040_init_periph
,
589 .flags
= CG_PLL_8BIT
,
592 .compat
= "fsl,t2080-clockgen",
593 .guts_compat
= "fsl,t2080-device-config",
594 .init_periph
= t2080_init_periph
,
596 &clockgen2_cmux_cga12
599 &t2080_hwa1
, &t2080_hwa2
605 .flags
= CG_PLL_8BIT
,
608 .compat
= "fsl,t4240-clockgen",
609 .guts_compat
= "fsl,t4240-device-config",
610 .init_periph
= t4240_init_periph
,
612 &clockgen2_cmux_cga
, &clockgen2_cmux_cgb
615 &t4240_hwa1
, NULL
, NULL
, &t4240_hwa4
, &t4240_hwa5
621 .flags
= CG_PLL_8BIT
,
629 const struct clockgen_muxinfo
*info
;
631 u8 parent_to_clksel
[NUM_MUX_PARENTS
];
632 s8 clksel_to_parent
[NUM_MUX_PARENTS
];
636 #define to_mux_hwclock(p) container_of(p, struct mux_hwclock, hw)
637 #define CLKSEL_MASK 0x78000000
638 #define CLKSEL_SHIFT 27
640 static int mux_set_parent(struct clk_hw
*hw
, u8 idx
)
642 struct mux_hwclock
*hwc
= to_mux_hwclock(hw
);
645 if (idx
>= hwc
->num_parents
)
648 clksel
= hwc
->parent_to_clksel
[idx
];
649 cg_out(hwc
->cg
, (clksel
<< CLKSEL_SHIFT
) & CLKSEL_MASK
, hwc
->reg
);
654 static u8
mux_get_parent(struct clk_hw
*hw
)
656 struct mux_hwclock
*hwc
= to_mux_hwclock(hw
);
660 clksel
= (cg_in(hwc
->cg
, hwc
->reg
) & CLKSEL_MASK
) >> CLKSEL_SHIFT
;
662 ret
= hwc
->clksel_to_parent
[clksel
];
664 pr_err("%s: mux at %p has bad clksel\n", __func__
, hwc
->reg
);
671 static const struct clk_ops cmux_ops
= {
672 .get_parent
= mux_get_parent
,
673 .set_parent
= mux_set_parent
,
677 * Don't allow setting for now, as the clock options haven't been
678 * sanitized for additional restrictions.
680 static const struct clk_ops hwaccel_ops
= {
681 .get_parent
= mux_get_parent
,
684 static const struct clockgen_pll_div
*get_pll_div(struct clockgen
*cg
,
685 struct mux_hwclock
*hwc
,
690 if (!(hwc
->info
->clksel
[idx
].flags
& CLKSEL_VALID
))
693 pll
= hwc
->info
->clksel
[idx
].pll
;
694 div
= hwc
->info
->clksel
[idx
].div
;
696 return &cg
->pll
[pll
].div
[div
];
699 static struct clk
* __init
create_mux_common(struct clockgen
*cg
,
700 struct mux_hwclock
*hwc
,
701 const struct clk_ops
*ops
,
702 unsigned long min_rate
,
703 unsigned long pct80_rate
,
704 const char *fmt
, int idx
)
706 struct clk_init_data init
= {};
708 const struct clockgen_pll_div
*div
;
709 const char *parent_names
[NUM_MUX_PARENTS
];
713 snprintf(name
, sizeof(name
), fmt
, idx
);
715 for (i
= 0, j
= 0; i
< NUM_MUX_PARENTS
; i
++) {
718 hwc
->clksel_to_parent
[i
] = -1;
720 div
= get_pll_div(cg
, hwc
, i
);
724 rate
= clk_get_rate(div
->clk
);
726 if (hwc
->info
->clksel
[i
].flags
& CLKSEL_80PCT
&&
732 parent_names
[j
] = div
->name
;
733 hwc
->parent_to_clksel
[j
] = i
;
734 hwc
->clksel_to_parent
[i
] = j
;
740 init
.parent_names
= parent_names
;
741 init
.num_parents
= hwc
->num_parents
= j
;
743 hwc
->hw
.init
= &init
;
746 clk
= clk_register(NULL
, &hwc
->hw
);
748 pr_err("%s: Couldn't register %s: %ld\n", __func__
, name
,
757 static struct clk
* __init
create_one_cmux(struct clockgen
*cg
, int idx
)
759 struct mux_hwclock
*hwc
;
760 const struct clockgen_pll_div
*div
;
761 unsigned long plat_rate
, min_rate
;
765 hwc
= kzalloc(sizeof(*hwc
), GFP_KERNEL
);
769 hwc
->reg
= cg
->regs
+ 0x20 * idx
;
770 hwc
->info
= cg
->info
.cmux_groups
[cg
->info
.cmux_to_group
[idx
]];
773 * Find the rate for the default clksel, and treat it as the
774 * maximum rated core frequency. If this is an incorrect
775 * assumption, certain clock options (possibly including the
776 * default clksel) may be inappropriately excluded on certain
779 clksel
= (cg_in(cg
, hwc
->reg
) & CLKSEL_MASK
) >> CLKSEL_SHIFT
;
780 div
= get_pll_div(cg
, hwc
, clksel
);
786 pct80_rate
= clk_get_rate(div
->clk
);
788 do_div(pct80_rate
, 10);
790 plat_rate
= clk_get_rate(cg
->pll
[PLATFORM_PLL
].div
[PLL_DIV1
].clk
);
792 if (cg
->info
.flags
& CG_CMUX_GE_PLAT
)
793 min_rate
= plat_rate
;
795 min_rate
= plat_rate
/ 2;
797 return create_mux_common(cg
, hwc
, &cmux_ops
, min_rate
,
798 pct80_rate
, "cg-cmux%d", idx
);
801 static struct clk
* __init
create_one_hwaccel(struct clockgen
*cg
, int idx
)
803 struct mux_hwclock
*hwc
;
805 hwc
= kzalloc(sizeof(*hwc
), GFP_KERNEL
);
809 hwc
->reg
= cg
->regs
+ 0x20 * idx
+ 0x10;
810 hwc
->info
= cg
->info
.hwaccel
[idx
];
812 return create_mux_common(cg
, hwc
, &hwaccel_ops
, 0, 0,
813 "cg-hwaccel%d", idx
);
816 static void __init
create_muxes(struct clockgen
*cg
)
820 for (i
= 0; i
< ARRAY_SIZE(cg
->cmux
); i
++) {
821 if (cg
->info
.cmux_to_group
[i
] < 0)
823 if (cg
->info
.cmux_to_group
[i
] >=
824 ARRAY_SIZE(cg
->info
.cmux_groups
)) {
829 cg
->cmux
[i
] = create_one_cmux(cg
, i
);
832 for (i
= 0; i
< ARRAY_SIZE(cg
->hwaccel
); i
++) {
833 if (!cg
->info
.hwaccel
[i
])
836 cg
->hwaccel
[i
] = create_one_hwaccel(cg
, i
);
840 static void __init
clockgen_init(struct device_node
*np
);
842 /* Legacy nodes may get probed before the parent clockgen node */
843 static void __init
legacy_init_clockgen(struct device_node
*np
)
846 clockgen_init(of_get_parent(np
));
850 static void __init
core_mux_init(struct device_node
*np
)
856 legacy_init_clockgen(np
);
858 if (of_address_to_resource(np
, 0, &res
))
861 idx
= (res
.start
& 0xf0) >> 5;
862 clk
= clockgen
.cmux
[idx
];
864 rc
= of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
866 pr_err("%s: Couldn't register clk provider for node %s: %d\n",
867 __func__
, np
->name
, rc
);
872 static struct clk
*sysclk_from_fixed(struct device_node
*node
, const char *name
)
876 if (of_property_read_u32(node
, "clock-frequency", &rate
))
877 return ERR_PTR(-ENODEV
);
879 return clk_register_fixed_rate(NULL
, name
, NULL
, CLK_IS_ROOT
, rate
);
882 static struct clk
*sysclk_from_parent(const char *name
)
885 const char *parent_name
;
887 clk
= of_clk_get(clockgen
.node
, 0);
891 /* Register the input clock under the desired name. */
892 parent_name
= __clk_get_name(clk
);
893 clk
= clk_register_fixed_factor(NULL
, name
, parent_name
,
896 pr_err("%s: Couldn't register %s: %ld\n", __func__
, name
,
902 static struct clk
* __init
create_sysclk(const char *name
)
904 struct device_node
*sysclk
;
907 clk
= sysclk_from_fixed(clockgen
.node
, name
);
911 clk
= sysclk_from_parent(name
);
915 sysclk
= of_get_child_by_name(clockgen
.node
, "sysclk");
917 clk
= sysclk_from_fixed(sysclk
, name
);
922 pr_err("%s: No input clock\n", __func__
);
927 static void __init
sysclk_init(struct device_node
*node
)
931 legacy_init_clockgen(node
);
933 clk
= clockgen
.sysclk
;
935 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
938 #define PLL_KILL BIT(31)
940 static void __init
create_one_pll(struct clockgen
*cg
, int idx
)
944 struct clockgen_pll
*pll
= &cg
->pll
[idx
];
947 if (!(cg
->info
.pll_mask
& (1 << idx
)))
950 if (cg
->info
.flags
& CG_VER3
) {
953 reg
= cg
->regs
+ 0x60080;
956 reg
= cg
->regs
+ 0x80;
959 reg
= cg
->regs
+ 0xa0;
962 reg
= cg
->regs
+ 0x10080;
965 reg
= cg
->regs
+ 0x100a0;
968 WARN_ONCE(1, "index %d\n", idx
);
972 if (idx
== PLATFORM_PLL
)
973 reg
= cg
->regs
+ 0xc00;
975 reg
= cg
->regs
+ 0x800 + 0x20 * (idx
- 1);
978 /* Get the multiple of PLL */
979 mult
= cg_in(cg
, reg
);
981 /* Check if this PLL is disabled */
982 if (mult
& PLL_KILL
) {
983 pr_debug("%s(): pll %p disabled\n", __func__
, reg
);
987 if ((cg
->info
.flags
& CG_VER3
) ||
988 ((cg
->info
.flags
& CG_PLL_8BIT
) && idx
!= PLATFORM_PLL
))
989 mult
= (mult
& GENMASK(8, 1)) >> 1;
991 mult
= (mult
& GENMASK(6, 1)) >> 1;
993 for (i
= 0; i
< ARRAY_SIZE(pll
->div
); i
++) {
996 snprintf(pll
->div
[i
].name
, sizeof(pll
->div
[i
].name
),
997 "cg-pll%d-div%d", idx
, i
+ 1);
999 clk
= clk_register_fixed_factor(NULL
,
1000 pll
->div
[i
].name
, "cg-sysclk", 0, mult
, i
+ 1);
1002 pr_err("%s: %s: register failed %ld\n",
1003 __func__
, pll
->div
[i
].name
, PTR_ERR(clk
));
1007 pll
->div
[i
].clk
= clk
;
1011 static void __init
create_plls(struct clockgen
*cg
)
1015 for (i
= 0; i
< ARRAY_SIZE(cg
->pll
); i
++)
1016 create_one_pll(cg
, i
);
1019 static void __init
legacy_pll_init(struct device_node
*np
, int idx
)
1021 struct clockgen_pll
*pll
;
1022 struct clk_onecell_data
*onecell_data
;
1023 struct clk
**subclks
;
1026 legacy_init_clockgen(np
);
1028 pll
= &clockgen
.pll
[idx
];
1029 count
= of_property_count_strings(np
, "clock-output-names");
1031 BUILD_BUG_ON(ARRAY_SIZE(pll
->div
) < 4);
1032 subclks
= kcalloc(4, sizeof(struct clk
*), GFP_KERNEL
);
1036 onecell_data
= kmalloc(sizeof(*onecell_data
), GFP_KERNEL
);
1041 subclks
[0] = pll
->div
[0].clk
;
1042 subclks
[1] = pll
->div
[1].clk
;
1043 subclks
[2] = pll
->div
[3].clk
;
1045 subclks
[0] = pll
->div
[0].clk
;
1046 subclks
[1] = pll
->div
[1].clk
;
1047 subclks
[2] = pll
->div
[2].clk
;
1048 subclks
[3] = pll
->div
[3].clk
;
1051 onecell_data
->clks
= subclks
;
1052 onecell_data
->clk_num
= count
;
1054 rc
= of_clk_add_provider(np
, of_clk_src_onecell_get
, onecell_data
);
1056 pr_err("%s: Couldn't register clk provider for node %s: %d\n",
1057 __func__
, np
->name
, rc
);
1063 kfree(onecell_data
);
1069 static void __init
pltfrm_pll_init(struct device_node
*np
)
1071 legacy_pll_init(np
, PLATFORM_PLL
);
1075 static void __init
core_pll_init(struct device_node
*np
)
1077 struct resource res
;
1080 if (of_address_to_resource(np
, 0, &res
))
1083 if ((res
.start
& 0xfff) == 0xc00) {
1085 * ls1021a devtree labels the platform PLL
1086 * with the core PLL compatible
1088 pltfrm_pll_init(np
);
1090 idx
= (res
.start
& 0xf0) >> 5;
1091 legacy_pll_init(np
, CGA_PLL1
+ idx
);
1095 static struct clk
*clockgen_clk_get(struct of_phandle_args
*clkspec
, void *data
)
1097 struct clockgen
*cg
= data
;
1099 struct clockgen_pll
*pll
;
1102 if (clkspec
->args_count
< 2) {
1103 pr_err("%s: insufficient phandle args\n", __func__
);
1104 return ERR_PTR(-EINVAL
);
1107 type
= clkspec
->args
[0];
1108 idx
= clkspec
->args
[1];
1117 if (idx
>= ARRAY_SIZE(cg
->cmux
))
1119 clk
= cg
->cmux
[idx
];
1122 if (idx
>= ARRAY_SIZE(cg
->hwaccel
))
1124 clk
= cg
->hwaccel
[idx
];
1127 if (idx
>= ARRAY_SIZE(cg
->fman
))
1129 clk
= cg
->fman
[idx
];
1132 pll
= &cg
->pll
[PLATFORM_PLL
];
1133 if (idx
>= ARRAY_SIZE(pll
->div
))
1135 clk
= pll
->div
[idx
].clk
;
1142 return ERR_PTR(-ENOENT
);
1146 pr_err("%s: Bad phandle args %u %u\n", __func__
, type
, idx
);
1147 return ERR_PTR(-EINVAL
);
1151 #include <asm/mpc85xx.h>
1153 static const u32 a4510_svrs
[] __initconst
= {
1154 (SVR_P2040
<< 8) | 0x10, /* P2040 1.0 */
1155 (SVR_P2040
<< 8) | 0x11, /* P2040 1.1 */
1156 (SVR_P2041
<< 8) | 0x10, /* P2041 1.0 */
1157 (SVR_P2041
<< 8) | 0x11, /* P2041 1.1 */
1158 (SVR_P3041
<< 8) | 0x10, /* P3041 1.0 */
1159 (SVR_P3041
<< 8) | 0x11, /* P3041 1.1 */
1160 (SVR_P4040
<< 8) | 0x20, /* P4040 2.0 */
1161 (SVR_P4080
<< 8) | 0x20, /* P4080 2.0 */
1162 (SVR_P5010
<< 8) | 0x10, /* P5010 1.0 */
1163 (SVR_P5010
<< 8) | 0x20, /* P5010 2.0 */
1164 (SVR_P5020
<< 8) | 0x10, /* P5020 1.0 */
1165 (SVR_P5021
<< 8) | 0x10, /* P5021 1.0 */
1166 (SVR_P5040
<< 8) | 0x10, /* P5040 1.0 */
1169 #define SVR_SECURITY 0x80000 /* The Security (E) bit */
1171 static bool __init
has_erratum_a4510(void)
1173 u32 svr
= mfspr(SPRN_SVR
);
1176 svr
&= ~SVR_SECURITY
;
1178 for (i
= 0; i
< ARRAY_SIZE(a4510_svrs
); i
++) {
1179 if (svr
== a4510_svrs
[i
])
1186 static bool __init
has_erratum_a4510(void)
1192 static void __init
clockgen_init(struct device_node
*np
)
1195 bool is_old_ls1021a
= false;
1197 /* May have already been called by a legacy probe */
1202 clockgen
.regs
= of_iomap(np
, 0);
1203 if (!clockgen
.regs
&&
1204 of_device_is_compatible(of_root
, "fsl,ls1021a")) {
1205 /* Compatibility hack for old, broken device trees */
1206 clockgen
.regs
= ioremap(0x1ee1000, 0x1000);
1207 is_old_ls1021a
= true;
1209 if (!clockgen
.regs
) {
1210 pr_err("%s(): %s: of_iomap() failed\n", __func__
, np
->name
);
1214 for (i
= 0; i
< ARRAY_SIZE(chipinfo
); i
++) {
1215 if (of_device_is_compatible(np
, chipinfo
[i
].compat
))
1217 if (is_old_ls1021a
&&
1218 !strcmp(chipinfo
[i
].compat
, "fsl,ls1021a-clockgen"))
1222 if (i
== ARRAY_SIZE(chipinfo
)) {
1223 pr_err("%s: unknown clockgen node %s\n", __func__
,
1227 clockgen
.info
= chipinfo
[i
];
1229 if (clockgen
.info
.guts_compat
) {
1230 struct device_node
*guts
;
1232 guts
= of_find_compatible_node(NULL
, NULL
,
1233 clockgen
.info
.guts_compat
);
1235 clockgen
.guts
= of_iomap(guts
, 0);
1236 if (!clockgen
.guts
) {
1237 pr_err("%s: Couldn't map %s regs\n", __func__
,
1244 if (has_erratum_a4510())
1245 clockgen
.info
.flags
|= CG_CMUX_GE_PLAT
;
1247 clockgen
.sysclk
= create_sysclk("cg-sysclk");
1248 create_plls(&clockgen
);
1249 create_muxes(&clockgen
);
1251 if (clockgen
.info
.init_periph
)
1252 clockgen
.info
.init_periph(&clockgen
);
1254 ret
= of_clk_add_provider(np
, clockgen_clk_get
, &clockgen
);
1256 pr_err("%s: Couldn't register clk provider for node %s: %d\n",
1257 __func__
, np
->name
, ret
);
1262 iounmap(clockgen
.regs
);
1263 clockgen
.regs
= NULL
;
1266 CLK_OF_DECLARE(qoriq_clockgen_1
, "fsl,qoriq-clockgen-1.0", clockgen_init
);
1267 CLK_OF_DECLARE(qoriq_clockgen_2
, "fsl,qoriq-clockgen-2.0", clockgen_init
);
1268 CLK_OF_DECLARE(qoriq_clockgen_ls1021a
, "fsl,ls1021a-clockgen", clockgen_init
);
1269 CLK_OF_DECLARE(qoriq_clockgen_ls1043a
, "fsl,ls1043a-clockgen", clockgen_init
);
1270 CLK_OF_DECLARE(qoriq_clockgen_ls2080a
, "fsl,ls2080a-clockgen", clockgen_init
);
1273 CLK_OF_DECLARE(qoriq_sysclk_1
, "fsl,qoriq-sysclk-1.0", sysclk_init
);
1274 CLK_OF_DECLARE(qoriq_sysclk_2
, "fsl,qoriq-sysclk-2.0", sysclk_init
);
1275 CLK_OF_DECLARE(qoriq_core_pll_1
, "fsl,qoriq-core-pll-1.0", core_pll_init
);
1276 CLK_OF_DECLARE(qoriq_core_pll_2
, "fsl,qoriq-core-pll-2.0", core_pll_init
);
1277 CLK_OF_DECLARE(qoriq_core_mux_1
, "fsl,qoriq-core-mux-1.0", core_mux_init
);
1278 CLK_OF_DECLARE(qoriq_core_mux_2
, "fsl,qoriq-core-mux-2.0", core_mux_init
);
1279 CLK_OF_DECLARE(qoriq_pltfrm_pll_1
, "fsl,qoriq-platform-pll-1.0", pltfrm_pll_init
);
1280 CLK_OF_DECLARE(qoriq_pltfrm_pll_2
, "fsl,qoriq-platform-pll-2.0", pltfrm_pll_init
);