blob: 9f8c4b8ae603d582005a5084b601dd0db24f5fa5 [file] [log] [blame]
Tang Yuantian555eae92013-04-09 16:46:26 +08001/*
2 * Copyright 2013 Freescale Semiconductor, Inc.
3 *
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.
7 *
Tang Yuantian93a17c02015-01-15 14:03:41 +08008 * clock driver for Freescale QorIQ SoCs.
Tang Yuantian555eae92013-04-09 16:46:26 +08009 */
Emil Medvec88b2b62015-01-21 04:03:29 -060010
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Scott Wood0dfc86b2015-09-19 23:29:54 -050013#include <linux/clk.h>
Tang Yuantian555eae92013-04-09 16:46:26 +080014#include <linux/clk-provider.h>
Scott Wood0dfc86b2015-09-19 23:29:54 -050015#include <linux/fsl/guts.h>
Tang Yuantian555eae92013-04-09 16:46:26 +080016#include <linux/io.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
Rob Herringc11eede2013-11-10 23:19:08 -060019#include <linux/of_address.h>
Tang Yuantian555eae92013-04-09 16:46:26 +080020#include <linux/of_platform.h>
21#include <linux/of.h>
22#include <linux/slab.h>
23
Scott Wood0dfc86b2015-09-19 23:29:54 -050024#define PLL_DIV1 0
25#define PLL_DIV2 1
26#define PLL_DIV3 2
27#define PLL_DIV4 3
28
29#define PLATFORM_PLL 0
30#define CGA_PLL1 1
31#define CGA_PLL2 2
32#define CGA_PLL3 3
33#define CGA_PLL4 4 /* only on clockgen-1.0, which lacks CGB */
34#define CGB_PLL1 4
35#define CGB_PLL2 5
36
37struct clockgen_pll_div {
38 struct clk *clk;
39 char name[32];
Tang Yuantian555eae92013-04-09 16:46:26 +080040};
41
Scott Wood0dfc86b2015-09-19 23:29:54 -050042struct clockgen_pll {
43 struct clockgen_pll_div div[4];
44};
Tang Yuantian555eae92013-04-09 16:46:26 +080045
Scott Wood0dfc86b2015-09-19 23:29:54 -050046#define CLKSEL_VALID 1
47#define CLKSEL_80PCT 2 /* Only allowed if PLL <= 80% of max cpu freq */
48
49struct clockgen_sourceinfo {
50 u32 flags; /* CLKSEL_xxx */
51 int pll; /* CGx_PLLn */
52 int div; /* PLL_DIVn */
53};
54
55#define NUM_MUX_PARENTS 16
56
57struct clockgen_muxinfo {
58 struct clockgen_sourceinfo clksel[NUM_MUX_PARENTS];
59};
60
61#define NUM_HWACCEL 5
62#define NUM_CMUX 8
63
64struct clockgen;
65
66/*
67 * cmux freq must be >= platform pll.
68 * If not set, cmux freq must be >= platform pll/2
69 */
70#define CG_CMUX_GE_PLAT 1
Scott Wood9e19ca22015-09-19 23:29:55 -050071
Scott Wood0dfc86b2015-09-19 23:29:54 -050072#define CG_PLL_8BIT 2 /* PLLCnGSR[CFG] is 8 bits, not 6 */
Scott Wood9e19ca22015-09-19 23:29:55 -050073#define CG_VER3 4 /* version 3 cg: reg layout different */
74#define CG_LITTLE_ENDIAN 8
Scott Wood0dfc86b2015-09-19 23:29:54 -050075
76struct 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 */
84};
85
86struct clockgen {
87 struct device_node *node;
88 void __iomem *regs;
89 struct clockgen_chipinfo info; /* mutable copy */
90 struct clk *sysclk;
91 struct clockgen_pll pll[6];
92 struct clk *cmux[NUM_CMUX];
93 struct clk *hwaccel[NUM_HWACCEL];
94 struct clk *fman[2];
95 struct ccsr_guts __iomem *guts;
96};
97
98static struct clockgen clockgen;
99
Scott Wood9e19ca22015-09-19 23:29:55 -0500100static void cg_out(struct clockgen *cg, u32 val, u32 __iomem *reg)
101{
102 if (cg->info.flags & CG_LITTLE_ENDIAN)
103 iowrite32(val, reg);
104 else
105 iowrite32be(val, reg);
106}
107
108static u32 cg_in(struct clockgen *cg, u32 __iomem *reg)
109{
110 u32 val;
111
112 if (cg->info.flags & CG_LITTLE_ENDIAN)
113 val = ioread32(reg);
114 else
115 val = ioread32be(reg);
116
117 return val;
118}
119
Scott Wood0dfc86b2015-09-19 23:29:54 -0500120static const struct clockgen_muxinfo p2041_cmux_grp1 = {
121 {
122 [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
123 [1] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
124 [4] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
125 }
126};
127
128static const struct clockgen_muxinfo p2041_cmux_grp2 = {
129 {
130 [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
Scott Wood2c7693e2015-10-22 23:21:46 -0500131 [4] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
132 [5] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
Scott Wood0dfc86b2015-09-19 23:29:54 -0500133 }
134};
135
136static const struct clockgen_muxinfo p5020_cmux_grp1 = {
137 {
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 },
141 }
142};
143
144static const struct clockgen_muxinfo p5020_cmux_grp2 = {
145 {
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 },
149 }
150};
151
152static const struct clockgen_muxinfo p5040_cmux_grp1 = {
153 {
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 },
158 }
159};
160
161static const struct clockgen_muxinfo p5040_cmux_grp2 = {
162 {
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 },
167 }
168};
169
170static const struct clockgen_muxinfo p4080_cmux_grp1 = {
171 {
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 },
177 }
178};
179
180static const struct clockgen_muxinfo p4080_cmux_grp2 = {
181 {
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 },
187 }
188};
189
190static const struct clockgen_muxinfo t1023_cmux = {
191 {
192 [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
193 [1] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
194 }
195};
196
197static const struct clockgen_muxinfo t1040_cmux = {
198 {
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 },
203 }
204};
205
206
207static const struct clockgen_muxinfo clockgen2_cmux_cga = {
208 {
209 { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
210 { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
211 { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
212 {},
213 { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
214 { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
215 { CLKSEL_VALID, CGA_PLL2, PLL_DIV4 },
216 {},
217 { CLKSEL_VALID, CGA_PLL3, PLL_DIV1 },
218 { CLKSEL_VALID, CGA_PLL3, PLL_DIV2 },
219 { CLKSEL_VALID, CGA_PLL3, PLL_DIV4 },
220 },
221};
222
223static const struct clockgen_muxinfo clockgen2_cmux_cga12 = {
224 {
225 { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
226 { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
227 { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
228 {},
229 { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
230 { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
231 { CLKSEL_VALID, CGA_PLL2, PLL_DIV4 },
232 },
233};
234
235static const struct clockgen_muxinfo clockgen2_cmux_cgb = {
236 {
237 { CLKSEL_VALID, CGB_PLL1, PLL_DIV1 },
238 { CLKSEL_VALID, CGB_PLL1, PLL_DIV2 },
239 { CLKSEL_VALID, CGB_PLL1, PLL_DIV4 },
240 {},
241 { CLKSEL_VALID, CGB_PLL2, PLL_DIV1 },
242 { CLKSEL_VALID, CGB_PLL2, PLL_DIV2 },
243 { CLKSEL_VALID, CGB_PLL2, PLL_DIV4 },
244 },
245};
246
Hou Zhiqiange994412c2015-10-23 16:01:21 +0800247static const struct clockgen_muxinfo ls1043a_hwa1 = {
248 {
249 {},
250 {},
251 { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
252 { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
253 {},
254 {},
255 { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
256 { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
257 },
258};
259
260static const struct clockgen_muxinfo ls1043a_hwa2 = {
261 {
262 {},
263 { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
264 {},
265 { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
266 },
267};
268
Mingkai Hu80e52192016-09-07 11:48:30 +0800269static const struct clockgen_muxinfo ls1046a_hwa1 = {
270 {
271 {},
272 {},
273 { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
274 { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
275 { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
276 { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
277 { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
278 { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
279 },
280};
281
282static const struct clockgen_muxinfo ls1046a_hwa2 = {
283 {
284 {},
285 { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
286 { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
287 { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
288 {},
289 {},
290 { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
291 },
292};
293
Scott Wood0dfc86b2015-09-19 23:29:54 -0500294static const struct clockgen_muxinfo t1023_hwa1 = {
295 {
296 {},
297 { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
298 { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
299 { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
300 },
301};
302
303static const struct clockgen_muxinfo t1023_hwa2 = {
304 {
305 [6] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
306 },
307};
308
309static const struct clockgen_muxinfo t2080_hwa1 = {
310 {
311 {},
312 { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
313 { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
314 { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
315 { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
316 { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
317 { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
318 { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
319 },
320};
321
322static const struct clockgen_muxinfo t2080_hwa2 = {
323 {
324 {},
325 { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
326 { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
327 { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
328 { CLKSEL_VALID, CGA_PLL2, PLL_DIV4 },
329 { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
330 { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
331 { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
332 },
333};
334
335static const struct clockgen_muxinfo t4240_hwa1 = {
336 {
337 { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV2 },
338 { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
339 { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
340 { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
341 { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
342 {},
343 { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
344 { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
345 },
346};
347
348static const struct clockgen_muxinfo t4240_hwa4 = {
349 {
350 [2] = { CLKSEL_VALID, CGB_PLL1, PLL_DIV2 },
351 [3] = { CLKSEL_VALID, CGB_PLL1, PLL_DIV3 },
352 [4] = { CLKSEL_VALID, CGB_PLL1, PLL_DIV4 },
353 [5] = { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
354 [6] = { CLKSEL_VALID, CGB_PLL2, PLL_DIV2 },
355 },
356};
357
358static const struct clockgen_muxinfo t4240_hwa5 = {
359 {
360 [2] = { CLKSEL_VALID, CGB_PLL2, PLL_DIV2 },
361 [3] = { CLKSEL_VALID, CGB_PLL2, PLL_DIV3 },
362 [4] = { CLKSEL_VALID, CGB_PLL2, PLL_DIV4 },
363 [5] = { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
364 [6] = { CLKSEL_VALID, CGB_PLL1, PLL_DIV2 },
365 [7] = { CLKSEL_VALID, CGB_PLL1, PLL_DIV3 },
366 },
367};
368
369#define RCWSR7_FM1_CLK_SEL 0x40000000
370#define RCWSR7_FM2_CLK_SEL 0x20000000
371#define RCWSR7_HWA_ASYNC_DIV 0x04000000
372
373static void __init p2041_init_periph(struct clockgen *cg)
Tang Yuantian555eae92013-04-09 16:46:26 +0800374{
Scott Wood0dfc86b2015-09-19 23:29:54 -0500375 u32 reg;
376
377 reg = ioread32be(&cg->guts->rcwsr[7]);
378
379 if (reg & RCWSR7_FM1_CLK_SEL)
380 cg->fman[0] = cg->pll[CGA_PLL2].div[PLL_DIV2].clk;
381 else
382 cg->fman[0] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
383}
384
385static void __init p4080_init_periph(struct clockgen *cg)
386{
387 u32 reg;
388
389 reg = ioread32be(&cg->guts->rcwsr[7]);
390
391 if (reg & RCWSR7_FM1_CLK_SEL)
392 cg->fman[0] = cg->pll[CGA_PLL3].div[PLL_DIV2].clk;
393 else
394 cg->fman[0] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
395
396 if (reg & RCWSR7_FM2_CLK_SEL)
397 cg->fman[1] = cg->pll[CGA_PLL3].div[PLL_DIV2].clk;
398 else
399 cg->fman[1] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
400}
401
402static void __init p5020_init_periph(struct clockgen *cg)
403{
404 u32 reg;
405 int div = PLL_DIV2;
406
407 reg = ioread32be(&cg->guts->rcwsr[7]);
408 if (reg & RCWSR7_HWA_ASYNC_DIV)
409 div = PLL_DIV4;
410
411 if (reg & RCWSR7_FM1_CLK_SEL)
412 cg->fman[0] = cg->pll[CGA_PLL2].div[div].clk;
413 else
414 cg->fman[0] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
415}
416
417static void __init p5040_init_periph(struct clockgen *cg)
418{
419 u32 reg;
420 int div = PLL_DIV2;
421
422 reg = ioread32be(&cg->guts->rcwsr[7]);
423 if (reg & RCWSR7_HWA_ASYNC_DIV)
424 div = PLL_DIV4;
425
426 if (reg & RCWSR7_FM1_CLK_SEL)
427 cg->fman[0] = cg->pll[CGA_PLL3].div[div].clk;
428 else
429 cg->fman[0] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
430
431 if (reg & RCWSR7_FM2_CLK_SEL)
432 cg->fman[1] = cg->pll[CGA_PLL3].div[div].clk;
433 else
434 cg->fman[1] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
435}
436
437static void __init t1023_init_periph(struct clockgen *cg)
438{
439 cg->fman[0] = cg->hwaccel[1];
440}
441
442static void __init t1040_init_periph(struct clockgen *cg)
443{
444 cg->fman[0] = cg->pll[PLATFORM_PLL].div[PLL_DIV1].clk;
445}
446
447static void __init t2080_init_periph(struct clockgen *cg)
448{
449 cg->fman[0] = cg->hwaccel[0];
450}
451
452static void __init t4240_init_periph(struct clockgen *cg)
453{
454 cg->fman[0] = cg->hwaccel[3];
455 cg->fman[1] = cg->hwaccel[4];
456}
457
458static const struct clockgen_chipinfo chipinfo[] = {
459 {
460 .compat = "fsl,b4420-clockgen",
461 .guts_compat = "fsl,b4860-device-config",
462 .init_periph = t2080_init_periph,
463 .cmux_groups = {
464 &clockgen2_cmux_cga12, &clockgen2_cmux_cgb
465 },
466 .hwaccel = {
467 &t2080_hwa1
468 },
469 .cmux_to_group = {
470 0, 1, 1, 1, -1
471 },
472 .pll_mask = 0x3f,
473 .flags = CG_PLL_8BIT,
474 },
475 {
476 .compat = "fsl,b4860-clockgen",
477 .guts_compat = "fsl,b4860-device-config",
478 .init_periph = t2080_init_periph,
479 .cmux_groups = {
480 &clockgen2_cmux_cga12, &clockgen2_cmux_cgb
481 },
482 .hwaccel = {
483 &t2080_hwa1
484 },
485 .cmux_to_group = {
486 0, 1, 1, 1, -1
487 },
488 .pll_mask = 0x3f,
489 .flags = CG_PLL_8BIT,
490 },
491 {
492 .compat = "fsl,ls1021a-clockgen",
493 .cmux_groups = {
494 &t1023_cmux
495 },
496 .cmux_to_group = {
497 0, -1
498 },
499 .pll_mask = 0x03,
500 },
501 {
Hou Zhiqiange994412c2015-10-23 16:01:21 +0800502 .compat = "fsl,ls1043a-clockgen",
503 .init_periph = t2080_init_periph,
504 .cmux_groups = {
505 &t1040_cmux
506 },
507 .hwaccel = {
508 &ls1043a_hwa1, &ls1043a_hwa2
509 },
510 .cmux_to_group = {
511 0, -1
512 },
513 .pll_mask = 0x07,
514 .flags = CG_PLL_8BIT,
515 },
516 {
Mingkai Hu80e52192016-09-07 11:48:30 +0800517 .compat = "fsl,ls1046a-clockgen",
518 .init_periph = t2080_init_periph,
519 .cmux_groups = {
520 &t1040_cmux
521 },
522 .hwaccel = {
523 &ls1046a_hwa1, &ls1046a_hwa2
524 },
525 .cmux_to_group = {
526 0, -1
527 },
528 .pll_mask = 0x07,
529 .flags = CG_PLL_8BIT,
530 },
531 {
Scott Wood9e19ca22015-09-19 23:29:55 -0500532 .compat = "fsl,ls2080a-clockgen",
533 .cmux_groups = {
534 &clockgen2_cmux_cga12, &clockgen2_cmux_cgb
535 },
536 .cmux_to_group = {
537 0, 0, 1, 1, -1
538 },
539 .pll_mask = 0x37,
540 .flags = CG_VER3 | CG_LITTLE_ENDIAN,
541 },
542 {
Scott Wood0dfc86b2015-09-19 23:29:54 -0500543 .compat = "fsl,p2041-clockgen",
544 .guts_compat = "fsl,qoriq-device-config-1.0",
545 .init_periph = p2041_init_periph,
546 .cmux_groups = {
547 &p2041_cmux_grp1, &p2041_cmux_grp2
548 },
549 .cmux_to_group = {
550 0, 0, 1, 1, -1
551 },
552 .pll_mask = 0x07,
553 },
554 {
555 .compat = "fsl,p3041-clockgen",
556 .guts_compat = "fsl,qoriq-device-config-1.0",
557 .init_periph = p2041_init_periph,
558 .cmux_groups = {
559 &p2041_cmux_grp1, &p2041_cmux_grp2
560 },
561 .cmux_to_group = {
562 0, 0, 1, 1, -1
563 },
564 .pll_mask = 0x07,
565 },
566 {
567 .compat = "fsl,p4080-clockgen",
568 .guts_compat = "fsl,qoriq-device-config-1.0",
569 .init_periph = p4080_init_periph,
570 .cmux_groups = {
571 &p4080_cmux_grp1, &p4080_cmux_grp2
572 },
573 .cmux_to_group = {
574 0, 0, 0, 0, 1, 1, 1, 1
575 },
576 .pll_mask = 0x1f,
577 },
578 {
579 .compat = "fsl,p5020-clockgen",
580 .guts_compat = "fsl,qoriq-device-config-1.0",
581 .init_periph = p5020_init_periph,
582 .cmux_groups = {
583 &p2041_cmux_grp1, &p2041_cmux_grp2
584 },
585 .cmux_to_group = {
586 0, 1, -1
587 },
588 .pll_mask = 0x07,
589 },
590 {
591 .compat = "fsl,p5040-clockgen",
592 .guts_compat = "fsl,p5040-device-config",
593 .init_periph = p5040_init_periph,
594 .cmux_groups = {
595 &p5040_cmux_grp1, &p5040_cmux_grp2
596 },
597 .cmux_to_group = {
598 0, 0, 1, 1, -1
599 },
600 .pll_mask = 0x0f,
601 },
602 {
603 .compat = "fsl,t1023-clockgen",
604 .guts_compat = "fsl,t1023-device-config",
605 .init_periph = t1023_init_periph,
606 .cmux_groups = {
607 &t1023_cmux
608 },
609 .hwaccel = {
610 &t1023_hwa1, &t1023_hwa2
611 },
612 .cmux_to_group = {
613 0, 0, -1
614 },
615 .pll_mask = 0x03,
616 .flags = CG_PLL_8BIT,
617 },
618 {
619 .compat = "fsl,t1040-clockgen",
620 .guts_compat = "fsl,t1040-device-config",
621 .init_periph = t1040_init_periph,
622 .cmux_groups = {
623 &t1040_cmux
624 },
625 .cmux_to_group = {
626 0, 0, 0, 0, -1
627 },
628 .pll_mask = 0x07,
629 .flags = CG_PLL_8BIT,
630 },
631 {
632 .compat = "fsl,t2080-clockgen",
633 .guts_compat = "fsl,t2080-device-config",
634 .init_periph = t2080_init_periph,
635 .cmux_groups = {
636 &clockgen2_cmux_cga12
637 },
638 .hwaccel = {
639 &t2080_hwa1, &t2080_hwa2
640 },
641 .cmux_to_group = {
642 0, -1
643 },
644 .pll_mask = 0x07,
645 .flags = CG_PLL_8BIT,
646 },
647 {
648 .compat = "fsl,t4240-clockgen",
649 .guts_compat = "fsl,t4240-device-config",
650 .init_periph = t4240_init_periph,
651 .cmux_groups = {
652 &clockgen2_cmux_cga, &clockgen2_cmux_cgb
653 },
654 .hwaccel = {
655 &t4240_hwa1, NULL, NULL, &t4240_hwa4, &t4240_hwa5
656 },
657 .cmux_to_group = {
658 0, 0, 1, -1
659 },
660 .pll_mask = 0x3f,
661 .flags = CG_PLL_8BIT,
662 },
663 {},
664};
665
666struct mux_hwclock {
667 struct clk_hw hw;
668 struct clockgen *cg;
669 const struct clockgen_muxinfo *info;
670 u32 __iomem *reg;
671 u8 parent_to_clksel[NUM_MUX_PARENTS];
672 s8 clksel_to_parent[NUM_MUX_PARENTS];
673 int num_parents;
674};
675
676#define to_mux_hwclock(p) container_of(p, struct mux_hwclock, hw)
677#define CLKSEL_MASK 0x78000000
678#define CLKSEL_SHIFT 27
679
680static int mux_set_parent(struct clk_hw *hw, u8 idx)
681{
682 struct mux_hwclock *hwc = to_mux_hwclock(hw);
Tang Yuantian555eae92013-04-09 16:46:26 +0800683 u32 clksel;
684
Scott Wood0dfc86b2015-09-19 23:29:54 -0500685 if (idx >= hwc->num_parents)
686 return -EINVAL;
687
688 clksel = hwc->parent_to_clksel[idx];
Scott Wood9e19ca22015-09-19 23:29:55 -0500689 cg_out(hwc->cg, (clksel << CLKSEL_SHIFT) & CLKSEL_MASK, hwc->reg);
Tang Yuantian555eae92013-04-09 16:46:26 +0800690
691 return 0;
692}
693
Scott Wood0dfc86b2015-09-19 23:29:54 -0500694static u8 mux_get_parent(struct clk_hw *hw)
Tang Yuantian555eae92013-04-09 16:46:26 +0800695{
Scott Wood0dfc86b2015-09-19 23:29:54 -0500696 struct mux_hwclock *hwc = to_mux_hwclock(hw);
Tang Yuantian555eae92013-04-09 16:46:26 +0800697 u32 clksel;
Scott Wood0dfc86b2015-09-19 23:29:54 -0500698 s8 ret;
Tang Yuantian555eae92013-04-09 16:46:26 +0800699
Scott Wood9e19ca22015-09-19 23:29:55 -0500700 clksel = (cg_in(hwc->cg, hwc->reg) & CLKSEL_MASK) >> CLKSEL_SHIFT;
Tang Yuantian555eae92013-04-09 16:46:26 +0800701
Scott Wood0dfc86b2015-09-19 23:29:54 -0500702 ret = hwc->clksel_to_parent[clksel];
703 if (ret < 0) {
704 pr_err("%s: mux at %p has bad clksel\n", __func__, hwc->reg);
705 return 0;
706 }
707
708 return ret;
Tang Yuantian555eae92013-04-09 16:46:26 +0800709}
710
Emil Medve334680d2015-01-21 04:03:27 -0600711static const struct clk_ops cmux_ops = {
Scott Wood0dfc86b2015-09-19 23:29:54 -0500712 .get_parent = mux_get_parent,
713 .set_parent = mux_set_parent,
Tang Yuantian555eae92013-04-09 16:46:26 +0800714};
715
Scott Wood0dfc86b2015-09-19 23:29:54 -0500716/*
717 * Don't allow setting for now, as the clock options haven't been
718 * sanitized for additional restrictions.
719 */
720static const struct clk_ops hwaccel_ops = {
721 .get_parent = mux_get_parent,
722};
723
724static const struct clockgen_pll_div *get_pll_div(struct clockgen *cg,
725 struct mux_hwclock *hwc,
726 int idx)
727{
728 int pll, div;
729
730 if (!(hwc->info->clksel[idx].flags & CLKSEL_VALID))
731 return NULL;
732
733 pll = hwc->info->clksel[idx].pll;
734 div = hwc->info->clksel[idx].div;
735
736 return &cg->pll[pll].div[div];
737}
738
739static struct clk * __init create_mux_common(struct clockgen *cg,
740 struct mux_hwclock *hwc,
741 const struct clk_ops *ops,
742 unsigned long min_rate,
743 unsigned long pct80_rate,
744 const char *fmt, int idx)
745{
746 struct clk_init_data init = {};
747 struct clk *clk;
748 const struct clockgen_pll_div *div;
749 const char *parent_names[NUM_MUX_PARENTS];
750 char name[32];
751 int i, j;
752
753 snprintf(name, sizeof(name), fmt, idx);
754
755 for (i = 0, j = 0; i < NUM_MUX_PARENTS; i++) {
756 unsigned long rate;
757
758 hwc->clksel_to_parent[i] = -1;
759
760 div = get_pll_div(cg, hwc, i);
761 if (!div)
762 continue;
763
764 rate = clk_get_rate(div->clk);
765
766 if (hwc->info->clksel[i].flags & CLKSEL_80PCT &&
767 rate > pct80_rate)
768 continue;
769 if (rate < min_rate)
770 continue;
771
772 parent_names[j] = div->name;
773 hwc->parent_to_clksel[j] = i;
774 hwc->clksel_to_parent[i] = j;
775 j++;
776 }
777
778 init.name = name;
779 init.ops = ops;
780 init.parent_names = parent_names;
781 init.num_parents = hwc->num_parents = j;
782 init.flags = 0;
783 hwc->hw.init = &init;
784 hwc->cg = cg;
785
786 clk = clk_register(NULL, &hwc->hw);
787 if (IS_ERR(clk)) {
788 pr_err("%s: Couldn't register %s: %ld\n", __func__, name,
789 PTR_ERR(clk));
790 kfree(hwc);
791 return NULL;
792 }
793
794 return clk;
795}
796
797static struct clk * __init create_one_cmux(struct clockgen *cg, int idx)
798{
799 struct mux_hwclock *hwc;
800 const struct clockgen_pll_div *div;
801 unsigned long plat_rate, min_rate;
802 u64 pct80_rate;
803 u32 clksel;
804
805 hwc = kzalloc(sizeof(*hwc), GFP_KERNEL);
806 if (!hwc)
807 return NULL;
808
Tang Yuantian89641932016-08-15 15:28:20 +0800809 if (cg->info.flags & CG_VER3)
810 hwc->reg = cg->regs + 0x70000 + 0x20 * idx;
811 else
812 hwc->reg = cg->regs + 0x20 * idx;
813
Scott Wood0dfc86b2015-09-19 23:29:54 -0500814 hwc->info = cg->info.cmux_groups[cg->info.cmux_to_group[idx]];
815
816 /*
817 * Find the rate for the default clksel, and treat it as the
818 * maximum rated core frequency. If this is an incorrect
819 * assumption, certain clock options (possibly including the
820 * default clksel) may be inappropriately excluded on certain
821 * chips.
822 */
Scott Wood9e19ca22015-09-19 23:29:55 -0500823 clksel = (cg_in(cg, hwc->reg) & CLKSEL_MASK) >> CLKSEL_SHIFT;
Scott Wood0dfc86b2015-09-19 23:29:54 -0500824 div = get_pll_div(cg, hwc, clksel);
Sudip Mukherjee279104e2015-11-23 15:36:50 +0530825 if (!div) {
826 kfree(hwc);
Scott Wood0dfc86b2015-09-19 23:29:54 -0500827 return NULL;
Sudip Mukherjee279104e2015-11-23 15:36:50 +0530828 }
Scott Wood0dfc86b2015-09-19 23:29:54 -0500829
830 pct80_rate = clk_get_rate(div->clk);
831 pct80_rate *= 8;
832 do_div(pct80_rate, 10);
833
834 plat_rate = clk_get_rate(cg->pll[PLATFORM_PLL].div[PLL_DIV1].clk);
835
836 if (cg->info.flags & CG_CMUX_GE_PLAT)
837 min_rate = plat_rate;
838 else
839 min_rate = plat_rate / 2;
840
841 return create_mux_common(cg, hwc, &cmux_ops, min_rate,
842 pct80_rate, "cg-cmux%d", idx);
843}
844
845static struct clk * __init create_one_hwaccel(struct clockgen *cg, int idx)
846{
847 struct mux_hwclock *hwc;
848
849 hwc = kzalloc(sizeof(*hwc), GFP_KERNEL);
850 if (!hwc)
851 return NULL;
852
853 hwc->reg = cg->regs + 0x20 * idx + 0x10;
854 hwc->info = cg->info.hwaccel[idx];
855
856 return create_mux_common(cg, hwc, &hwaccel_ops, 0, 0,
857 "cg-hwaccel%d", idx);
858}
859
860static void __init create_muxes(struct clockgen *cg)
861{
862 int i;
863
864 for (i = 0; i < ARRAY_SIZE(cg->cmux); i++) {
865 if (cg->info.cmux_to_group[i] < 0)
866 break;
867 if (cg->info.cmux_to_group[i] >=
868 ARRAY_SIZE(cg->info.cmux_groups)) {
869 WARN_ON_ONCE(1);
870 continue;
871 }
872
873 cg->cmux[i] = create_one_cmux(cg, i);
874 }
875
876 for (i = 0; i < ARRAY_SIZE(cg->hwaccel); i++) {
877 if (!cg->info.hwaccel[i])
878 continue;
879
880 cg->hwaccel[i] = create_one_hwaccel(cg, i);
881 }
882}
883
884static void __init clockgen_init(struct device_node *np);
885
886/* Legacy nodes may get probed before the parent clockgen node */
887static void __init legacy_init_clockgen(struct device_node *np)
888{
889 if (!clockgen.node)
890 clockgen_init(of_get_parent(np));
891}
892
893/* Legacy node */
Tang Yuantian555eae92013-04-09 16:46:26 +0800894static void __init core_mux_init(struct device_node *np)
895{
896 struct clk *clk;
Scott Wood0dfc86b2015-09-19 23:29:54 -0500897 struct resource res;
898 int idx, rc;
Tang Yuantian555eae92013-04-09 16:46:26 +0800899
Scott Wood0dfc86b2015-09-19 23:29:54 -0500900 legacy_init_clockgen(np);
Tang Yuantian555eae92013-04-09 16:46:26 +0800901
Scott Wood0dfc86b2015-09-19 23:29:54 -0500902 if (of_address_to_resource(np, 0, &res))
Tang Yuantian555eae92013-04-09 16:46:26 +0800903 return;
Tang Yuantian555eae92013-04-09 16:46:26 +0800904
Scott Wood0dfc86b2015-09-19 23:29:54 -0500905 idx = (res.start & 0xf0) >> 5;
906 clk = clockgen.cmux[idx];
Tang Yuantian555eae92013-04-09 16:46:26 +0800907
908 rc = of_clk_add_provider(np, of_clk_src_simple_get, clk);
909 if (rc) {
Scott Wood0dfc86b2015-09-19 23:29:54 -0500910 pr_err("%s: Couldn't register clk provider for node %s: %d\n",
911 __func__, np->name, rc);
912 return;
Tang Yuantian555eae92013-04-09 16:46:26 +0800913 }
Tang Yuantian555eae92013-04-09 16:46:26 +0800914}
915
Julia Lawall3432a2e2016-04-18 16:55:34 +0200916static struct clk __init
917*sysclk_from_fixed(struct device_node *node, const char *name)
Tang Yuantian555eae92013-04-09 16:46:26 +0800918{
Scott Wood0dfc86b2015-09-19 23:29:54 -0500919 u32 rate;
Tang Yuantian555eae92013-04-09 16:46:26 +0800920
Scott Wood0dfc86b2015-09-19 23:29:54 -0500921 if (of_property_read_u32(node, "clock-frequency", &rate))
922 return ERR_PTR(-ENODEV);
923
Stephen Boydec3f2fc2016-03-01 11:00:19 -0800924 return clk_register_fixed_rate(NULL, name, NULL, 0, rate);
Scott Wood0dfc86b2015-09-19 23:29:54 -0500925}
926
927static struct clk *sysclk_from_parent(const char *name)
928{
929 struct clk *clk;
930 const char *parent_name;
931
932 clk = of_clk_get(clockgen.node, 0);
933 if (IS_ERR(clk))
934 return clk;
935
936 /* Register the input clock under the desired name. */
937 parent_name = __clk_get_name(clk);
938 clk = clk_register_fixed_factor(NULL, name, parent_name,
939 0, 1, 1);
940 if (IS_ERR(clk))
941 pr_err("%s: Couldn't register %s: %ld\n", __func__, name,
942 PTR_ERR(clk));
943
944 return clk;
945}
946
947static struct clk * __init create_sysclk(const char *name)
948{
949 struct device_node *sysclk;
950 struct clk *clk;
951
952 clk = sysclk_from_fixed(clockgen.node, name);
953 if (!IS_ERR(clk))
954 return clk;
955
956 clk = sysclk_from_parent(name);
957 if (!IS_ERR(clk))
958 return clk;
959
960 sysclk = of_get_child_by_name(clockgen.node, "sysclk");
961 if (sysclk) {
962 clk = sysclk_from_fixed(sysclk, name);
963 if (!IS_ERR(clk))
964 return clk;
965 }
966
967 pr_err("%s: No input clock\n", __func__);
968 return NULL;
969}
970
971/* Legacy node */
972static void __init sysclk_init(struct device_node *node)
973{
974 struct clk *clk;
975
976 legacy_init_clockgen(node);
977
978 clk = clockgen.sysclk;
979 if (clk)
980 of_clk_add_provider(node, of_clk_src_simple_get, clk);
981}
982
983#define PLL_KILL BIT(31)
984
985static void __init create_one_pll(struct clockgen *cg, int idx)
986{
987 u32 __iomem *reg;
988 u32 mult;
989 struct clockgen_pll *pll = &cg->pll[idx];
990 int i;
991
992 if (!(cg->info.pll_mask & (1 << idx)))
993 return;
994
Scott Wood9e19ca22015-09-19 23:29:55 -0500995 if (cg->info.flags & CG_VER3) {
996 switch (idx) {
997 case PLATFORM_PLL:
998 reg = cg->regs + 0x60080;
999 break;
1000 case CGA_PLL1:
1001 reg = cg->regs + 0x80;
1002 break;
1003 case CGA_PLL2:
1004 reg = cg->regs + 0xa0;
1005 break;
1006 case CGB_PLL1:
1007 reg = cg->regs + 0x10080;
1008 break;
1009 case CGB_PLL2:
1010 reg = cg->regs + 0x100a0;
1011 break;
1012 default:
1013 WARN_ONCE(1, "index %d\n", idx);
1014 return;
1015 }
1016 } else {
1017 if (idx == PLATFORM_PLL)
1018 reg = cg->regs + 0xc00;
1019 else
1020 reg = cg->regs + 0x800 + 0x20 * (idx - 1);
1021 }
Scott Wood0dfc86b2015-09-19 23:29:54 -05001022
1023 /* Get the multiple of PLL */
Scott Wood9e19ca22015-09-19 23:29:55 -05001024 mult = cg_in(cg, reg);
Scott Wood0dfc86b2015-09-19 23:29:54 -05001025
1026 /* Check if this PLL is disabled */
1027 if (mult & PLL_KILL) {
1028 pr_debug("%s(): pll %p disabled\n", __func__, reg);
Tang Yuantian555eae92013-04-09 16:46:26 +08001029 return;
1030 }
1031
Scott Wood9e19ca22015-09-19 23:29:55 -05001032 if ((cg->info.flags & CG_VER3) ||
1033 ((cg->info.flags & CG_PLL_8BIT) && idx != PLATFORM_PLL))
Scott Wood0dfc86b2015-09-19 23:29:54 -05001034 mult = (mult & GENMASK(8, 1)) >> 1;
1035 else
1036 mult = (mult & GENMASK(6, 1)) >> 1;
Tang Yuantian555eae92013-04-09 16:46:26 +08001037
Scott Wood0dfc86b2015-09-19 23:29:54 -05001038 for (i = 0; i < ARRAY_SIZE(pll->div); i++) {
1039 struct clk *clk;
1040
1041 snprintf(pll->div[i].name, sizeof(pll->div[i].name),
1042 "cg-pll%d-div%d", idx, i + 1);
1043
1044 clk = clk_register_fixed_factor(NULL,
1045 pll->div[i].name, "cg-sysclk", 0, mult, i + 1);
1046 if (IS_ERR(clk)) {
1047 pr_err("%s: %s: register failed %ld\n",
1048 __func__, pll->div[i].name, PTR_ERR(clk));
1049 continue;
1050 }
1051
1052 pll->div[i].clk = clk;
Tang Yuantian555eae92013-04-09 16:46:26 +08001053 }
Scott Wood0dfc86b2015-09-19 23:29:54 -05001054}
Tang Yuantian555eae92013-04-09 16:46:26 +08001055
Scott Wood0dfc86b2015-09-19 23:29:54 -05001056static void __init create_plls(struct clockgen *cg)
1057{
1058 int i;
Tang Yuantian555eae92013-04-09 16:46:26 +08001059
Scott Wood0dfc86b2015-09-19 23:29:54 -05001060 for (i = 0; i < ARRAY_SIZE(cg->pll); i++)
1061 create_one_pll(cg, i);
1062}
1063
1064static void __init legacy_pll_init(struct device_node *np, int idx)
1065{
1066 struct clockgen_pll *pll;
1067 struct clk_onecell_data *onecell_data;
1068 struct clk **subclks;
1069 int count, rc;
1070
1071 legacy_init_clockgen(np);
1072
1073 pll = &clockgen.pll[idx];
Tang Yuantian555eae92013-04-09 16:46:26 +08001074 count = of_property_count_strings(np, "clock-output-names");
Tang Yuantian555eae92013-04-09 16:46:26 +08001075
Scott Wood0dfc86b2015-09-19 23:29:54 -05001076 BUILD_BUG_ON(ARRAY_SIZE(pll->div) < 4);
1077 subclks = kcalloc(4, sizeof(struct clk *), GFP_KERNEL);
Emil Medve8002cab2015-01-21 04:03:26 -06001078 if (!subclks)
Scott Wood0dfc86b2015-09-19 23:29:54 -05001079 return;
Tang Yuantian555eae92013-04-09 16:46:26 +08001080
Emil Medve6ef1cca2015-01-21 04:03:28 -06001081 onecell_data = kmalloc(sizeof(*onecell_data), GFP_KERNEL);
Emil Medve8002cab2015-01-21 04:03:26 -06001082 if (!onecell_data)
Tang Yuantian555eae92013-04-09 16:46:26 +08001083 goto err_clks;
Tang Yuantian555eae92013-04-09 16:46:26 +08001084
Scott Wood0dfc86b2015-09-19 23:29:54 -05001085 if (count <= 3) {
1086 subclks[0] = pll->div[0].clk;
1087 subclks[1] = pll->div[1].clk;
1088 subclks[2] = pll->div[3].clk;
1089 } else {
1090 subclks[0] = pll->div[0].clk;
1091 subclks[1] = pll->div[1].clk;
1092 subclks[2] = pll->div[2].clk;
1093 subclks[3] = pll->div[3].clk;
Tang Yuantian555eae92013-04-09 16:46:26 +08001094 }
1095
1096 onecell_data->clks = subclks;
1097 onecell_data->clk_num = count;
1098
1099 rc = of_clk_add_provider(np, of_clk_src_onecell_get, onecell_data);
1100 if (rc) {
Scott Wood0dfc86b2015-09-19 23:29:54 -05001101 pr_err("%s: Couldn't register clk provider for node %s: %d\n",
1102 __func__, np->name, rc);
Tang Yuantian555eae92013-04-09 16:46:26 +08001103 goto err_cell;
1104 }
1105
1106 return;
1107err_cell:
1108 kfree(onecell_data);
1109err_clks:
1110 kfree(subclks);
Tang Yuantian00fa6e52014-01-21 09:32:45 +08001111}
1112
Scott Wood0dfc86b2015-09-19 23:29:54 -05001113/* Legacy node */
Emil Medvea513b722015-01-21 04:03:31 -06001114static void __init pltfrm_pll_init(struct device_node *np)
1115{
Scott Wood0dfc86b2015-09-19 23:29:54 -05001116 legacy_pll_init(np, PLATFORM_PLL);
1117}
Emil Medvea513b722015-01-21 04:03:31 -06001118
Scott Wood0dfc86b2015-09-19 23:29:54 -05001119/* Legacy node */
1120static void __init core_pll_init(struct device_node *np)
1121{
1122 struct resource res;
1123 int idx;
1124
1125 if (of_address_to_resource(np, 0, &res))
1126 return;
1127
1128 if ((res.start & 0xfff) == 0xc00) {
1129 /*
1130 * ls1021a devtree labels the platform PLL
1131 * with the core PLL compatible
1132 */
1133 pltfrm_pll_init(np);
1134 } else {
1135 idx = (res.start & 0xf0) >> 5;
1136 legacy_pll_init(np, CGA_PLL1 + idx);
1137 }
1138}
1139
1140static struct clk *clockgen_clk_get(struct of_phandle_args *clkspec, void *data)
1141{
1142 struct clockgen *cg = data;
1143 struct clk *clk;
1144 struct clockgen_pll *pll;
1145 u32 type, idx;
1146
1147 if (clkspec->args_count < 2) {
1148 pr_err("%s: insufficient phandle args\n", __func__);
1149 return ERR_PTR(-EINVAL);
1150 }
1151
1152 type = clkspec->args[0];
1153 idx = clkspec->args[1];
1154
1155 switch (type) {
1156 case 0:
1157 if (idx != 0)
1158 goto bad_args;
1159 clk = cg->sysclk;
1160 break;
1161 case 1:
1162 if (idx >= ARRAY_SIZE(cg->cmux))
1163 goto bad_args;
1164 clk = cg->cmux[idx];
1165 break;
1166 case 2:
1167 if (idx >= ARRAY_SIZE(cg->hwaccel))
1168 goto bad_args;
1169 clk = cg->hwaccel[idx];
1170 break;
1171 case 3:
1172 if (idx >= ARRAY_SIZE(cg->fman))
1173 goto bad_args;
1174 clk = cg->fman[idx];
1175 break;
1176 case 4:
1177 pll = &cg->pll[PLATFORM_PLL];
1178 if (idx >= ARRAY_SIZE(pll->div))
1179 goto bad_args;
1180 clk = pll->div[idx].clk;
1181 break;
1182 default:
1183 goto bad_args;
1184 }
1185
1186 if (!clk)
1187 return ERR_PTR(-ENOENT);
1188 return clk;
1189
1190bad_args:
1191 pr_err("%s: Bad phandle args %u %u\n", __func__, type, idx);
1192 return ERR_PTR(-EINVAL);
1193}
1194
1195#ifdef CONFIG_PPC
1196#include <asm/mpc85xx.h>
1197
1198static const u32 a4510_svrs[] __initconst = {
1199 (SVR_P2040 << 8) | 0x10, /* P2040 1.0 */
1200 (SVR_P2040 << 8) | 0x11, /* P2040 1.1 */
1201 (SVR_P2041 << 8) | 0x10, /* P2041 1.0 */
1202 (SVR_P2041 << 8) | 0x11, /* P2041 1.1 */
1203 (SVR_P3041 << 8) | 0x10, /* P3041 1.0 */
1204 (SVR_P3041 << 8) | 0x11, /* P3041 1.1 */
1205 (SVR_P4040 << 8) | 0x20, /* P4040 2.0 */
1206 (SVR_P4080 << 8) | 0x20, /* P4080 2.0 */
1207 (SVR_P5010 << 8) | 0x10, /* P5010 1.0 */
1208 (SVR_P5010 << 8) | 0x20, /* P5010 2.0 */
1209 (SVR_P5020 << 8) | 0x10, /* P5020 1.0 */
1210 (SVR_P5021 << 8) | 0x10, /* P5021 1.0 */
1211 (SVR_P5040 << 8) | 0x10, /* P5040 1.0 */
1212};
1213
1214#define SVR_SECURITY 0x80000 /* The Security (E) bit */
1215
1216static bool __init has_erratum_a4510(void)
1217{
1218 u32 svr = mfspr(SPRN_SVR);
1219 int i;
1220
1221 svr &= ~SVR_SECURITY;
1222
1223 for (i = 0; i < ARRAY_SIZE(a4510_svrs); i++) {
1224 if (svr == a4510_svrs[i])
1225 return true;
1226 }
1227
1228 return false;
1229}
1230#else
1231static bool __init has_erratum_a4510(void)
1232{
1233 return false;
1234}
1235#endif
1236
1237static void __init clockgen_init(struct device_node *np)
1238{
1239 int i, ret;
1240 bool is_old_ls1021a = false;
1241
1242 /* May have already been called by a legacy probe */
1243 if (clockgen.node)
1244 return;
1245
1246 clockgen.node = np;
1247 clockgen.regs = of_iomap(np, 0);
1248 if (!clockgen.regs &&
1249 of_device_is_compatible(of_root, "fsl,ls1021a")) {
1250 /* Compatibility hack for old, broken device trees */
1251 clockgen.regs = ioremap(0x1ee1000, 0x1000);
1252 is_old_ls1021a = true;
1253 }
1254 if (!clockgen.regs) {
Emil Medvea513b722015-01-21 04:03:31 -06001255 pr_err("%s(): %s: of_iomap() failed\n", __func__, np->name);
1256 return;
1257 }
1258
Scott Wood0dfc86b2015-09-19 23:29:54 -05001259 for (i = 0; i < ARRAY_SIZE(chipinfo); i++) {
1260 if (of_device_is_compatible(np, chipinfo[i].compat))
1261 break;
1262 if (is_old_ls1021a &&
1263 !strcmp(chipinfo[i].compat, "fsl,ls1021a-clockgen"))
1264 break;
Emil Medvea513b722015-01-21 04:03:31 -06001265 }
1266
Scott Wood0dfc86b2015-09-19 23:29:54 -05001267 if (i == ARRAY_SIZE(chipinfo)) {
1268 pr_err("%s: unknown clockgen node %s\n", __func__,
1269 np->full_name);
1270 goto err;
Emil Medvea513b722015-01-21 04:03:31 -06001271 }
Scott Wood0dfc86b2015-09-19 23:29:54 -05001272 clockgen.info = chipinfo[i];
Emil Medvea513b722015-01-21 04:03:31 -06001273
Scott Wood0dfc86b2015-09-19 23:29:54 -05001274 if (clockgen.info.guts_compat) {
1275 struct device_node *guts;
Emil Medvea513b722015-01-21 04:03:31 -06001276
Scott Wood0dfc86b2015-09-19 23:29:54 -05001277 guts = of_find_compatible_node(NULL, NULL,
1278 clockgen.info.guts_compat);
1279 if (guts) {
1280 clockgen.guts = of_iomap(guts, 0);
1281 if (!clockgen.guts) {
1282 pr_err("%s: Couldn't map %s regs\n", __func__,
1283 guts->full_name);
1284 }
Emil Medvea513b722015-01-21 04:03:31 -06001285 }
1286
Emil Medvea513b722015-01-21 04:03:31 -06001287 }
1288
Scott Wood0dfc86b2015-09-19 23:29:54 -05001289 if (has_erratum_a4510())
1290 clockgen.info.flags |= CG_CMUX_GE_PLAT;
1291
1292 clockgen.sysclk = create_sysclk("cg-sysclk");
1293 create_plls(&clockgen);
1294 create_muxes(&clockgen);
1295
1296 if (clockgen.info.init_periph)
1297 clockgen.info.init_periph(&clockgen);
1298
1299 ret = of_clk_add_provider(np, clockgen_clk_get, &clockgen);
1300 if (ret) {
1301 pr_err("%s: Couldn't register clk provider for node %s: %d\n",
1302 __func__, np->name, ret);
Emil Medvea513b722015-01-21 04:03:31 -06001303 }
1304
1305 return;
Scott Wood0dfc86b2015-09-19 23:29:54 -05001306err:
1307 iounmap(clockgen.regs);
1308 clockgen.regs = NULL;
Emil Medvea513b722015-01-21 04:03:31 -06001309}
1310
Scott Wood0dfc86b2015-09-19 23:29:54 -05001311CLK_OF_DECLARE(qoriq_clockgen_1, "fsl,qoriq-clockgen-1.0", clockgen_init);
1312CLK_OF_DECLARE(qoriq_clockgen_2, "fsl,qoriq-clockgen-2.0", clockgen_init);
1313CLK_OF_DECLARE(qoriq_clockgen_ls1021a, "fsl,ls1021a-clockgen", clockgen_init);
Hou Zhiqiange994412c2015-10-23 16:01:21 +08001314CLK_OF_DECLARE(qoriq_clockgen_ls1043a, "fsl,ls1043a-clockgen", clockgen_init);
Mingkai Hu80e52192016-09-07 11:48:30 +08001315CLK_OF_DECLARE(qoriq_clockgen_ls1046a, "fsl,ls1046a-clockgen", clockgen_init);
Scott Wood9e19ca22015-09-19 23:29:55 -05001316CLK_OF_DECLARE(qoriq_clockgen_ls2080a, "fsl,ls2080a-clockgen", clockgen_init);
Scott Wood0dfc86b2015-09-19 23:29:54 -05001317
1318/* Legacy nodes */
Kevin Hao66619ac2014-12-03 16:53:53 +08001319CLK_OF_DECLARE(qoriq_sysclk_1, "fsl,qoriq-sysclk-1.0", sysclk_init);
1320CLK_OF_DECLARE(qoriq_sysclk_2, "fsl,qoriq-sysclk-2.0", sysclk_init);
1321CLK_OF_DECLARE(qoriq_core_pll_1, "fsl,qoriq-core-pll-1.0", core_pll_init);
1322CLK_OF_DECLARE(qoriq_core_pll_2, "fsl,qoriq-core-pll-2.0", core_pll_init);
1323CLK_OF_DECLARE(qoriq_core_mux_1, "fsl,qoriq-core-mux-1.0", core_mux_init);
1324CLK_OF_DECLARE(qoriq_core_mux_2, "fsl,qoriq-core-mux-2.0", core_mux_init);
Emil Medvea513b722015-01-21 04:03:31 -06001325CLK_OF_DECLARE(qoriq_pltfrm_pll_1, "fsl,qoriq-platform-pll-1.0", pltfrm_pll_init);
1326CLK_OF_DECLARE(qoriq_pltfrm_pll_2, "fsl,qoriq-platform-pll-2.0", pltfrm_pll_init);