Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright 2013 Freescale Semiconductor, Inc.
0004  * Copyright 2021 NXP
0005  *
0006  * clock driver for Freescale QorIQ SoCs.
0007  */
0008 
0009 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0010 
0011 #include <dt-bindings/clock/fsl,qoriq-clockgen.h>
0012 #include <linux/clk.h>
0013 #include <linux/clk-provider.h>
0014 #include <linux/clkdev.h>
0015 #include <linux/fsl/guts.h>
0016 #include <linux/io.h>
0017 #include <linux/kernel.h>
0018 #include <linux/module.h>
0019 #include <linux/of_address.h>
0020 #include <linux/of_platform.h>
0021 #include <linux/of.h>
0022 #include <linux/slab.h>
0023 
0024 #define PLL_DIV1    0
0025 #define PLL_DIV2    1
0026 #define PLL_DIV3    2
0027 #define PLL_DIV4    3
0028 
0029 #define PLATFORM_PLL    0
0030 #define CGA_PLL1    1
0031 #define CGA_PLL2    2
0032 #define CGA_PLL3    3
0033 #define CGA_PLL4    4   /* only on clockgen-1.0, which lacks CGB */
0034 #define CGB_PLL1    4
0035 #define CGB_PLL2    5
0036 #define MAX_PLL_DIV 32
0037 
0038 struct clockgen_pll_div {
0039     struct clk *clk;
0040     char name[32];
0041 };
0042 
0043 struct clockgen_pll {
0044     struct clockgen_pll_div div[MAX_PLL_DIV];
0045 };
0046 
0047 #define CLKSEL_VALID    1
0048 #define CLKSEL_80PCT    2   /* Only allowed if PLL <= 80% of max cpu freq */
0049 
0050 struct clockgen_sourceinfo {
0051     u32 flags;  /* CLKSEL_xxx */
0052     int pll;    /* CGx_PLLn */
0053     int div;    /* PLL_DIVn */
0054 };
0055 
0056 #define NUM_MUX_PARENTS 16
0057 
0058 struct clockgen_muxinfo {
0059     struct clockgen_sourceinfo clksel[NUM_MUX_PARENTS];
0060 };
0061 
0062 #define NUM_HWACCEL 5
0063 #define NUM_CMUX    8
0064 
0065 struct clockgen;
0066 
0067 /*
0068  * cmux freq must be >= platform pll.
0069  * If not set, cmux freq must be >= platform pll/2
0070  */
0071 #define CG_CMUX_GE_PLAT     1
0072 
0073 #define CG_PLL_8BIT     2   /* PLLCnGSR[CFG] is 8 bits, not 6 */
0074 #define CG_VER3         4   /* version 3 cg: reg layout different */
0075 #define CG_LITTLE_ENDIAN    8
0076 
0077 struct clockgen_chipinfo {
0078     const char *compat, *guts_compat;
0079     const struct clockgen_muxinfo *cmux_groups[2];
0080     const struct clockgen_muxinfo *hwaccel[NUM_HWACCEL];
0081     void (*init_periph)(struct clockgen *cg);
0082     int cmux_to_group[NUM_CMUX + 1]; /* array should be -1 terminated */
0083     u32 pll_mask;   /* 1 << n bit set if PLL n is valid */
0084     u32 flags;  /* CG_xxx */
0085 };
0086 
0087 struct clockgen {
0088     struct device_node *node;
0089     void __iomem *regs;
0090     struct clockgen_chipinfo info; /* mutable copy */
0091     struct clk *sysclk, *coreclk;
0092     struct clockgen_pll pll[6];
0093     struct clk *cmux[NUM_CMUX];
0094     struct clk *hwaccel[NUM_HWACCEL];
0095     struct clk *fman[2];
0096     struct ccsr_guts __iomem *guts;
0097 };
0098 
0099 static struct clockgen clockgen;
0100 static bool add_cpufreq_dev __initdata;
0101 
0102 static void cg_out(struct clockgen *cg, u32 val, u32 __iomem *reg)
0103 {
0104     if (cg->info.flags & CG_LITTLE_ENDIAN)
0105         iowrite32(val, reg);
0106     else
0107         iowrite32be(val, reg);
0108 }
0109 
0110 static u32 cg_in(struct clockgen *cg, u32 __iomem *reg)
0111 {
0112     u32 val;
0113 
0114     if (cg->info.flags & CG_LITTLE_ENDIAN)
0115         val = ioread32(reg);
0116     else
0117         val = ioread32be(reg);
0118 
0119     return val;
0120 }
0121 
0122 static const struct clockgen_muxinfo p2041_cmux_grp1 = {
0123     {
0124         [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0125         [1] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0126         [4] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
0127     }
0128 };
0129 
0130 static const struct clockgen_muxinfo p2041_cmux_grp2 = {
0131     {
0132         [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0133         [4] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
0134         [5] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0135     }
0136 };
0137 
0138 static const struct clockgen_muxinfo p5020_cmux_grp1 = {
0139     {
0140         [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0141         [1] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0142         [4] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL2, PLL_DIV1 },
0143     }
0144 };
0145 
0146 static const struct clockgen_muxinfo p5020_cmux_grp2 = {
0147     {
0148         [0] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL1, PLL_DIV1 },
0149         [4] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
0150         [5] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0151     }
0152 };
0153 
0154 static const struct clockgen_muxinfo p5040_cmux_grp1 = {
0155     {
0156         [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0157         [1] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0158         [4] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL2, PLL_DIV1 },
0159         [5] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL2, PLL_DIV2 },
0160     }
0161 };
0162 
0163 static const struct clockgen_muxinfo p5040_cmux_grp2 = {
0164     {
0165         [0] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL1, PLL_DIV1 },
0166         [1] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL1, PLL_DIV2 },
0167         [4] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
0168         [5] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0169     }
0170 };
0171 
0172 static const struct clockgen_muxinfo p4080_cmux_grp1 = {
0173     {
0174         [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0175         [1] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0176         [4] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
0177         [5] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0178         [8] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL3, PLL_DIV1 },
0179     }
0180 };
0181 
0182 static const struct clockgen_muxinfo p4080_cmux_grp2 = {
0183     {
0184         [0] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL1, PLL_DIV1 },
0185         [8] = { CLKSEL_VALID, CGA_PLL3, PLL_DIV1 },
0186         [9] = { CLKSEL_VALID, CGA_PLL3, PLL_DIV2 },
0187         [12] = { CLKSEL_VALID, CGA_PLL4, PLL_DIV1 },
0188         [13] = { CLKSEL_VALID, CGA_PLL4, PLL_DIV2 },
0189     }
0190 };
0191 
0192 static const struct clockgen_muxinfo t1023_cmux = {
0193     {
0194         [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0195         [1] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0196     }
0197 };
0198 
0199 static const struct clockgen_muxinfo t1040_cmux = {
0200     {
0201         [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0202         [1] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0203         [4] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
0204         [5] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0205     }
0206 };
0207 
0208 
0209 static const struct clockgen_muxinfo clockgen2_cmux_cga = {
0210     {
0211         { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0212         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0213         { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
0214         {},
0215         { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
0216         { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0217         { CLKSEL_VALID, CGA_PLL2, PLL_DIV4 },
0218         {},
0219         { CLKSEL_VALID, CGA_PLL3, PLL_DIV1 },
0220         { CLKSEL_VALID, CGA_PLL3, PLL_DIV2 },
0221         { CLKSEL_VALID, CGA_PLL3, PLL_DIV4 },
0222     },
0223 };
0224 
0225 static const struct clockgen_muxinfo clockgen2_cmux_cga12 = {
0226     {
0227         { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0228         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0229         { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
0230         {},
0231         { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
0232         { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0233         { CLKSEL_VALID, CGA_PLL2, PLL_DIV4 },
0234     },
0235 };
0236 
0237 static const struct clockgen_muxinfo clockgen2_cmux_cgb = {
0238     {
0239         { CLKSEL_VALID, CGB_PLL1, PLL_DIV1 },
0240         { CLKSEL_VALID, CGB_PLL1, PLL_DIV2 },
0241         { CLKSEL_VALID, CGB_PLL1, PLL_DIV4 },
0242         {},
0243         { CLKSEL_VALID, CGB_PLL2, PLL_DIV1 },
0244         { CLKSEL_VALID, CGB_PLL2, PLL_DIV2 },
0245         { CLKSEL_VALID, CGB_PLL2, PLL_DIV4 },
0246     },
0247 };
0248 
0249 static const struct clockgen_muxinfo ls1021a_cmux = {
0250     {
0251         { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0252         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0253         { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
0254     }
0255 };
0256 
0257 static const struct clockgen_muxinfo ls1028a_hwa1 = {
0258     {
0259         { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
0260         { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0261         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0262         { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
0263         { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
0264         {},
0265         { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0266         { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
0267     },
0268 };
0269 
0270 static const struct clockgen_muxinfo ls1028a_hwa2 = {
0271     {
0272         { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
0273         { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
0274         { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0275         { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
0276         { CLKSEL_VALID, CGA_PLL2, PLL_DIV4 },
0277         {},
0278         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0279         { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
0280     },
0281 };
0282 
0283 static const struct clockgen_muxinfo ls1028a_hwa3 = {
0284     {
0285         { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
0286         { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0287         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0288         { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
0289         { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
0290         {},
0291         { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0292         { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
0293     },
0294 };
0295 
0296 static const struct clockgen_muxinfo ls1028a_hwa4 = {
0297     {
0298         { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
0299         { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
0300         { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0301         { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
0302         { CLKSEL_VALID, CGA_PLL2, PLL_DIV4 },
0303         {},
0304         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0305         { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
0306     },
0307 };
0308 
0309 static const struct clockgen_muxinfo ls1043a_hwa1 = {
0310     {
0311         {},
0312         {},
0313         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0314         { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
0315         {},
0316         {},
0317         { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0318         { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
0319     },
0320 };
0321 
0322 static const struct clockgen_muxinfo ls1043a_hwa2 = {
0323     {
0324         {},
0325         { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
0326         {},
0327         { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
0328     },
0329 };
0330 
0331 static const struct clockgen_muxinfo ls1046a_hwa1 = {
0332     {
0333         {},
0334         {},
0335         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0336         { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
0337         { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
0338         { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
0339         { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0340         { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
0341     },
0342 };
0343 
0344 static const struct clockgen_muxinfo ls1046a_hwa2 = {
0345     {
0346         {},
0347         { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
0348         { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0349         { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
0350         {},
0351         {},
0352         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0353     },
0354 };
0355 
0356 static const struct clockgen_muxinfo ls1088a_hwa1 = {
0357     {
0358         {},
0359         { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0360         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0361         { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
0362         { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
0363         {},
0364         { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0365         { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
0366     },
0367 };
0368 
0369 static const struct clockgen_muxinfo ls1088a_hwa2 = {
0370     {
0371         {},
0372         { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
0373         { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0374         { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
0375         { CLKSEL_VALID, CGA_PLL2, PLL_DIV4 },
0376         {},
0377         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0378         { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
0379     },
0380 };
0381 
0382 static const struct clockgen_muxinfo ls1012a_cmux = {
0383     {
0384         [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0385         {},
0386         [2] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0387     }
0388 };
0389 
0390 static const struct clockgen_muxinfo t1023_hwa1 = {
0391     {
0392         {},
0393         { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0394         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0395         { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
0396     },
0397 };
0398 
0399 static const struct clockgen_muxinfo t1023_hwa2 = {
0400     {
0401         [6] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0402     },
0403 };
0404 
0405 static const struct clockgen_muxinfo t2080_hwa1 = {
0406     {
0407         {},
0408         { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0409         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0410         { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
0411         { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
0412         { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
0413         { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0414         { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
0415     },
0416 };
0417 
0418 static const struct clockgen_muxinfo t2080_hwa2 = {
0419     {
0420         {},
0421         { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
0422         { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0423         { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
0424         { CLKSEL_VALID, CGA_PLL2, PLL_DIV4 },
0425         { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
0426         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0427         { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
0428     },
0429 };
0430 
0431 static const struct clockgen_muxinfo t4240_hwa1 = {
0432     {
0433         { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV2 },
0434         { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
0435         { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
0436         { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
0437         { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
0438         {},
0439         { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
0440         { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
0441     },
0442 };
0443 
0444 static const struct clockgen_muxinfo t4240_hwa4 = {
0445     {
0446         [2] = { CLKSEL_VALID, CGB_PLL1, PLL_DIV2 },
0447         [3] = { CLKSEL_VALID, CGB_PLL1, PLL_DIV3 },
0448         [4] = { CLKSEL_VALID, CGB_PLL1, PLL_DIV4 },
0449         [5] = { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
0450         [6] = { CLKSEL_VALID, CGB_PLL2, PLL_DIV2 },
0451     },
0452 };
0453 
0454 static const struct clockgen_muxinfo t4240_hwa5 = {
0455     {
0456         [2] = { CLKSEL_VALID, CGB_PLL2, PLL_DIV2 },
0457         [3] = { CLKSEL_VALID, CGB_PLL2, PLL_DIV3 },
0458         [4] = { CLKSEL_VALID, CGB_PLL2, PLL_DIV4 },
0459         [5] = { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
0460         [6] = { CLKSEL_VALID, CGB_PLL1, PLL_DIV2 },
0461         [7] = { CLKSEL_VALID, CGB_PLL1, PLL_DIV3 },
0462     },
0463 };
0464 
0465 #define RCWSR7_FM1_CLK_SEL  0x40000000
0466 #define RCWSR7_FM2_CLK_SEL  0x20000000
0467 #define RCWSR7_HWA_ASYNC_DIV    0x04000000
0468 
0469 static void __init p2041_init_periph(struct clockgen *cg)
0470 {
0471     u32 reg;
0472 
0473     reg = ioread32be(&cg->guts->rcwsr[7]);
0474 
0475     if (reg & RCWSR7_FM1_CLK_SEL)
0476         cg->fman[0] = cg->pll[CGA_PLL2].div[PLL_DIV2].clk;
0477     else
0478         cg->fman[0] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
0479 }
0480 
0481 static void __init p4080_init_periph(struct clockgen *cg)
0482 {
0483     u32 reg;
0484 
0485     reg = ioread32be(&cg->guts->rcwsr[7]);
0486 
0487     if (reg & RCWSR7_FM1_CLK_SEL)
0488         cg->fman[0] = cg->pll[CGA_PLL3].div[PLL_DIV2].clk;
0489     else
0490         cg->fman[0] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
0491 
0492     if (reg & RCWSR7_FM2_CLK_SEL)
0493         cg->fman[1] = cg->pll[CGA_PLL3].div[PLL_DIV2].clk;
0494     else
0495         cg->fman[1] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
0496 }
0497 
0498 static void __init p5020_init_periph(struct clockgen *cg)
0499 {
0500     u32 reg;
0501     int div = PLL_DIV2;
0502 
0503     reg = ioread32be(&cg->guts->rcwsr[7]);
0504     if (reg & RCWSR7_HWA_ASYNC_DIV)
0505         div = PLL_DIV4;
0506 
0507     if (reg & RCWSR7_FM1_CLK_SEL)
0508         cg->fman[0] = cg->pll[CGA_PLL2].div[div].clk;
0509     else
0510         cg->fman[0] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
0511 }
0512 
0513 static void __init p5040_init_periph(struct clockgen *cg)
0514 {
0515     u32 reg;
0516     int div = PLL_DIV2;
0517 
0518     reg = ioread32be(&cg->guts->rcwsr[7]);
0519     if (reg & RCWSR7_HWA_ASYNC_DIV)
0520         div = PLL_DIV4;
0521 
0522     if (reg & RCWSR7_FM1_CLK_SEL)
0523         cg->fman[0] = cg->pll[CGA_PLL3].div[div].clk;
0524     else
0525         cg->fman[0] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
0526 
0527     if (reg & RCWSR7_FM2_CLK_SEL)
0528         cg->fman[1] = cg->pll[CGA_PLL3].div[div].clk;
0529     else
0530         cg->fman[1] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
0531 }
0532 
0533 static void __init t1023_init_periph(struct clockgen *cg)
0534 {
0535     cg->fman[0] = cg->hwaccel[1];
0536 }
0537 
0538 static void __init t1040_init_periph(struct clockgen *cg)
0539 {
0540     cg->fman[0] = cg->pll[PLATFORM_PLL].div[PLL_DIV1].clk;
0541 }
0542 
0543 static void __init t2080_init_periph(struct clockgen *cg)
0544 {
0545     cg->fman[0] = cg->hwaccel[0];
0546 }
0547 
0548 static void __init t4240_init_periph(struct clockgen *cg)
0549 {
0550     cg->fman[0] = cg->hwaccel[3];
0551     cg->fman[1] = cg->hwaccel[4];
0552 }
0553 
0554 static const struct clockgen_chipinfo chipinfo[] = {
0555     {
0556         .compat = "fsl,b4420-clockgen",
0557         .guts_compat = "fsl,b4860-device-config",
0558         .init_periph = t2080_init_periph,
0559         .cmux_groups = {
0560             &clockgen2_cmux_cga12, &clockgen2_cmux_cgb
0561         },
0562         .hwaccel = {
0563             &t2080_hwa1
0564         },
0565         .cmux_to_group = {
0566             0, 1, 1, 1, -1
0567         },
0568         .pll_mask = BIT(PLATFORM_PLL) |
0569                 BIT(CGA_PLL1) | BIT(CGA_PLL2) | BIT(CGA_PLL3) |
0570                 BIT(CGB_PLL1) | BIT(CGB_PLL2),
0571         .flags = CG_PLL_8BIT,
0572     },
0573     {
0574         .compat = "fsl,b4860-clockgen",
0575         .guts_compat = "fsl,b4860-device-config",
0576         .init_periph = t2080_init_periph,
0577         .cmux_groups = {
0578             &clockgen2_cmux_cga12, &clockgen2_cmux_cgb
0579         },
0580         .hwaccel = {
0581             &t2080_hwa1
0582         },
0583         .cmux_to_group = {
0584             0, 1, 1, 1, -1
0585         },
0586         .pll_mask = BIT(PLATFORM_PLL) |
0587                 BIT(CGA_PLL1) | BIT(CGA_PLL2) | BIT(CGA_PLL3) |
0588                 BIT(CGB_PLL1) | BIT(CGB_PLL2),
0589         .flags = CG_PLL_8BIT,
0590     },
0591     {
0592         .compat = "fsl,ls1021a-clockgen",
0593         .cmux_groups = {
0594             &ls1021a_cmux
0595         },
0596         .cmux_to_group = {
0597             0, -1
0598         },
0599         .pll_mask = BIT(PLATFORM_PLL) |
0600                 BIT(CGA_PLL1) | BIT(CGA_PLL2),
0601     },
0602     {
0603         .compat = "fsl,ls1028a-clockgen",
0604         .cmux_groups = {
0605             &clockgen2_cmux_cga12
0606         },
0607         .hwaccel = {
0608             &ls1028a_hwa1, &ls1028a_hwa2,
0609             &ls1028a_hwa3, &ls1028a_hwa4
0610         },
0611         .cmux_to_group = {
0612             0, 0, 0, 0, -1
0613         },
0614         .pll_mask = BIT(PLATFORM_PLL) |
0615                 BIT(CGA_PLL1) | BIT(CGA_PLL2),
0616         .flags = CG_VER3 | CG_LITTLE_ENDIAN,
0617     },
0618     {
0619         .compat = "fsl,ls1043a-clockgen",
0620         .init_periph = t2080_init_periph,
0621         .cmux_groups = {
0622             &t1040_cmux
0623         },
0624         .hwaccel = {
0625             &ls1043a_hwa1, &ls1043a_hwa2
0626         },
0627         .cmux_to_group = {
0628             0, -1
0629         },
0630         .pll_mask = BIT(PLATFORM_PLL) |
0631                 BIT(CGA_PLL1) | BIT(CGA_PLL2),
0632         .flags = CG_PLL_8BIT,
0633     },
0634     {
0635         .compat = "fsl,ls1046a-clockgen",
0636         .init_periph = t2080_init_periph,
0637         .cmux_groups = {
0638             &t1040_cmux
0639         },
0640         .hwaccel = {
0641             &ls1046a_hwa1, &ls1046a_hwa2
0642         },
0643         .cmux_to_group = {
0644             0, -1
0645         },
0646         .pll_mask = BIT(PLATFORM_PLL) |
0647                 BIT(CGA_PLL1) | BIT(CGA_PLL2),
0648         .flags = CG_PLL_8BIT,
0649     },
0650     {
0651         .compat = "fsl,ls1088a-clockgen",
0652         .cmux_groups = {
0653             &clockgen2_cmux_cga12
0654         },
0655         .hwaccel = {
0656             &ls1088a_hwa1, &ls1088a_hwa2
0657         },
0658         .cmux_to_group = {
0659             0, 0, -1
0660         },
0661         .pll_mask = BIT(PLATFORM_PLL) |
0662                 BIT(CGA_PLL1) | BIT(CGA_PLL2),
0663         .flags = CG_VER3 | CG_LITTLE_ENDIAN,
0664     },
0665     {
0666         .compat = "fsl,ls1012a-clockgen",
0667         .cmux_groups = {
0668             &ls1012a_cmux
0669         },
0670         .cmux_to_group = {
0671             0, -1
0672         },
0673         .pll_mask = BIT(PLATFORM_PLL) | BIT(CGA_PLL1),
0674     },
0675     {
0676         .compat = "fsl,ls2080a-clockgen",
0677         .cmux_groups = {
0678             &clockgen2_cmux_cga12, &clockgen2_cmux_cgb
0679         },
0680         .cmux_to_group = {
0681             0, 0, 1, 1, -1
0682         },
0683         .pll_mask = BIT(PLATFORM_PLL) |
0684                 BIT(CGA_PLL1) | BIT(CGA_PLL2) |
0685                 BIT(CGB_PLL1) | BIT(CGB_PLL2),
0686         .flags = CG_VER3 | CG_LITTLE_ENDIAN,
0687     },
0688     {
0689         .compat = "fsl,lx2160a-clockgen",
0690         .cmux_groups = {
0691             &clockgen2_cmux_cga12, &clockgen2_cmux_cgb
0692         },
0693         .cmux_to_group = {
0694             0, 0, 0, 0, 1, 1, 1, 1, -1
0695         },
0696         .pll_mask = BIT(PLATFORM_PLL) |
0697                 BIT(CGA_PLL1) | BIT(CGA_PLL2) |
0698                 BIT(CGB_PLL1) | BIT(CGB_PLL2),
0699         .flags = CG_VER3 | CG_LITTLE_ENDIAN,
0700     },
0701     {
0702         .compat = "fsl,p2041-clockgen",
0703         .guts_compat = "fsl,qoriq-device-config-1.0",
0704         .init_periph = p2041_init_periph,
0705         .cmux_groups = {
0706             &p2041_cmux_grp1, &p2041_cmux_grp2
0707         },
0708         .cmux_to_group = {
0709             0, 0, 1, 1, -1
0710         },
0711         .pll_mask = BIT(PLATFORM_PLL) |
0712                 BIT(CGA_PLL1) | BIT(CGA_PLL2),
0713     },
0714     {
0715         .compat = "fsl,p3041-clockgen",
0716         .guts_compat = "fsl,qoriq-device-config-1.0",
0717         .init_periph = p2041_init_periph,
0718         .cmux_groups = {
0719             &p2041_cmux_grp1, &p2041_cmux_grp2
0720         },
0721         .cmux_to_group = {
0722             0, 0, 1, 1, -1
0723         },
0724         .pll_mask = BIT(PLATFORM_PLL) |
0725                 BIT(CGA_PLL1) | BIT(CGA_PLL2),
0726     },
0727     {
0728         .compat = "fsl,p4080-clockgen",
0729         .guts_compat = "fsl,qoriq-device-config-1.0",
0730         .init_periph = p4080_init_periph,
0731         .cmux_groups = {
0732             &p4080_cmux_grp1, &p4080_cmux_grp2
0733         },
0734         .cmux_to_group = {
0735             0, 0, 0, 0, 1, 1, 1, 1, -1
0736         },
0737         .pll_mask = BIT(PLATFORM_PLL) |
0738                 BIT(CGA_PLL1) | BIT(CGA_PLL2) |
0739                 BIT(CGA_PLL3) | BIT(CGA_PLL4),
0740     },
0741     {
0742         .compat = "fsl,p5020-clockgen",
0743         .guts_compat = "fsl,qoriq-device-config-1.0",
0744         .init_periph = p5020_init_periph,
0745         .cmux_groups = {
0746             &p5020_cmux_grp1, &p5020_cmux_grp2
0747         },
0748         .cmux_to_group = {
0749             0, 1, -1
0750         },
0751         .pll_mask = BIT(PLATFORM_PLL) |
0752                 BIT(CGA_PLL1) | BIT(CGA_PLL2),
0753     },
0754     {
0755         .compat = "fsl,p5040-clockgen",
0756         .guts_compat = "fsl,p5040-device-config",
0757         .init_periph = p5040_init_periph,
0758         .cmux_groups = {
0759             &p5040_cmux_grp1, &p5040_cmux_grp2
0760         },
0761         .cmux_to_group = {
0762             0, 0, 1, 1, -1
0763         },
0764         .pll_mask = BIT(PLATFORM_PLL) |
0765                 BIT(CGA_PLL1) | BIT(CGA_PLL2) | BIT(CGA_PLL3),
0766     },
0767     {
0768         .compat = "fsl,t1023-clockgen",
0769         .guts_compat = "fsl,t1023-device-config",
0770         .init_periph = t1023_init_periph,
0771         .cmux_groups = {
0772             &t1023_cmux
0773         },
0774         .hwaccel = {
0775             &t1023_hwa1, &t1023_hwa2
0776         },
0777         .cmux_to_group = {
0778             0, 0, -1
0779         },
0780         .pll_mask = BIT(PLATFORM_PLL) | BIT(CGA_PLL1),
0781         .flags = CG_PLL_8BIT,
0782     },
0783     {
0784         .compat = "fsl,t1040-clockgen",
0785         .guts_compat = "fsl,t1040-device-config",
0786         .init_periph = t1040_init_periph,
0787         .cmux_groups = {
0788             &t1040_cmux
0789         },
0790         .cmux_to_group = {
0791             0, 0, 0, 0, -1
0792         },
0793         .pll_mask = BIT(PLATFORM_PLL) |
0794                 BIT(CGA_PLL1) | BIT(CGA_PLL2),
0795         .flags = CG_PLL_8BIT,
0796     },
0797     {
0798         .compat = "fsl,t2080-clockgen",
0799         .guts_compat = "fsl,t2080-device-config",
0800         .init_periph = t2080_init_periph,
0801         .cmux_groups = {
0802             &clockgen2_cmux_cga12
0803         },
0804         .hwaccel = {
0805             &t2080_hwa1, &t2080_hwa2
0806         },
0807         .cmux_to_group = {
0808             0, -1
0809         },
0810         .pll_mask = BIT(PLATFORM_PLL) |
0811                 BIT(CGA_PLL1) | BIT(CGA_PLL2),
0812         .flags = CG_PLL_8BIT,
0813     },
0814     {
0815         .compat = "fsl,t4240-clockgen",
0816         .guts_compat = "fsl,t4240-device-config",
0817         .init_periph = t4240_init_periph,
0818         .cmux_groups = {
0819             &clockgen2_cmux_cga, &clockgen2_cmux_cgb
0820         },
0821         .hwaccel = {
0822             &t4240_hwa1, NULL, NULL, &t4240_hwa4, &t4240_hwa5
0823         },
0824         .cmux_to_group = {
0825             0, 0, 1, -1
0826         },
0827         .pll_mask = BIT(PLATFORM_PLL) |
0828                 BIT(CGA_PLL1) | BIT(CGA_PLL2) | BIT(CGA_PLL3) |
0829                 BIT(CGB_PLL1) | BIT(CGB_PLL2),
0830         .flags = CG_PLL_8BIT,
0831     },
0832     {},
0833 };
0834 
0835 struct mux_hwclock {
0836     struct clk_hw hw;
0837     struct clockgen *cg;
0838     const struct clockgen_muxinfo *info;
0839     u32 __iomem *reg;
0840     u8 parent_to_clksel[NUM_MUX_PARENTS];
0841     s8 clksel_to_parent[NUM_MUX_PARENTS];
0842     int num_parents;
0843 };
0844 
0845 #define to_mux_hwclock(p)   container_of(p, struct mux_hwclock, hw)
0846 #define CLKSEL_MASK     0x78000000
0847 #define CLKSEL_SHIFT        27
0848 
0849 static int mux_set_parent(struct clk_hw *hw, u8 idx)
0850 {
0851     struct mux_hwclock *hwc = to_mux_hwclock(hw);
0852     u32 clksel;
0853 
0854     if (idx >= hwc->num_parents)
0855         return -EINVAL;
0856 
0857     clksel = hwc->parent_to_clksel[idx];
0858     cg_out(hwc->cg, (clksel << CLKSEL_SHIFT) & CLKSEL_MASK, hwc->reg);
0859 
0860     return 0;
0861 }
0862 
0863 static u8 mux_get_parent(struct clk_hw *hw)
0864 {
0865     struct mux_hwclock *hwc = to_mux_hwclock(hw);
0866     u32 clksel;
0867     s8 ret;
0868 
0869     clksel = (cg_in(hwc->cg, hwc->reg) & CLKSEL_MASK) >> CLKSEL_SHIFT;
0870 
0871     ret = hwc->clksel_to_parent[clksel];
0872     if (ret < 0) {
0873         pr_err("%s: mux at %p has bad clksel\n", __func__, hwc->reg);
0874         return 0;
0875     }
0876 
0877     return ret;
0878 }
0879 
0880 static const struct clk_ops cmux_ops = {
0881     .get_parent = mux_get_parent,
0882     .set_parent = mux_set_parent,
0883 };
0884 
0885 /*
0886  * Don't allow setting for now, as the clock options haven't been
0887  * sanitized for additional restrictions.
0888  */
0889 static const struct clk_ops hwaccel_ops = {
0890     .get_parent = mux_get_parent,
0891 };
0892 
0893 static const struct clockgen_pll_div *get_pll_div(struct clockgen *cg,
0894                           struct mux_hwclock *hwc,
0895                           int idx)
0896 {
0897     int pll, div;
0898 
0899     if (!(hwc->info->clksel[idx].flags & CLKSEL_VALID))
0900         return NULL;
0901 
0902     pll = hwc->info->clksel[idx].pll;
0903     div = hwc->info->clksel[idx].div;
0904 
0905     return &cg->pll[pll].div[div];
0906 }
0907 
0908 static struct clk * __init create_mux_common(struct clockgen *cg,
0909                          struct mux_hwclock *hwc,
0910                          const struct clk_ops *ops,
0911                          unsigned long min_rate,
0912                          unsigned long max_rate,
0913                          unsigned long pct80_rate,
0914                          const char *fmt, int idx)
0915 {
0916     struct clk_init_data init = {};
0917     struct clk *clk;
0918     const struct clockgen_pll_div *div;
0919     const char *parent_names[NUM_MUX_PARENTS];
0920     char name[32];
0921     int i, j;
0922 
0923     snprintf(name, sizeof(name), fmt, idx);
0924 
0925     for (i = 0, j = 0; i < NUM_MUX_PARENTS; i++) {
0926         unsigned long rate;
0927 
0928         hwc->clksel_to_parent[i] = -1;
0929 
0930         div = get_pll_div(cg, hwc, i);
0931         if (!div)
0932             continue;
0933 
0934         rate = clk_get_rate(div->clk);
0935 
0936         if (hwc->info->clksel[i].flags & CLKSEL_80PCT &&
0937             rate > pct80_rate)
0938             continue;
0939         if (rate < min_rate)
0940             continue;
0941         if (rate > max_rate)
0942             continue;
0943 
0944         parent_names[j] = div->name;
0945         hwc->parent_to_clksel[j] = i;
0946         hwc->clksel_to_parent[i] = j;
0947         j++;
0948     }
0949 
0950     init.name = name;
0951     init.ops = ops;
0952     init.parent_names = parent_names;
0953     init.num_parents = hwc->num_parents = j;
0954     init.flags = 0;
0955     hwc->hw.init = &init;
0956     hwc->cg = cg;
0957 
0958     clk = clk_register(NULL, &hwc->hw);
0959     if (IS_ERR(clk)) {
0960         pr_err("%s: Couldn't register %s: %ld\n", __func__, name,
0961                PTR_ERR(clk));
0962         kfree(hwc);
0963         return NULL;
0964     }
0965 
0966     return clk;
0967 }
0968 
0969 static struct clk * __init create_one_cmux(struct clockgen *cg, int idx)
0970 {
0971     struct mux_hwclock *hwc;
0972     const struct clockgen_pll_div *div;
0973     unsigned long plat_rate, min_rate;
0974     u64 max_rate, pct80_rate;
0975     u32 clksel;
0976 
0977     hwc = kzalloc(sizeof(*hwc), GFP_KERNEL);
0978     if (!hwc)
0979         return NULL;
0980 
0981     if (cg->info.flags & CG_VER3)
0982         hwc->reg = cg->regs + 0x70000 + 0x20 * idx;
0983     else
0984         hwc->reg = cg->regs + 0x20 * idx;
0985 
0986     hwc->info = cg->info.cmux_groups[cg->info.cmux_to_group[idx]];
0987 
0988     /*
0989      * Find the rate for the default clksel, and treat it as the
0990      * maximum rated core frequency.  If this is an incorrect
0991      * assumption, certain clock options (possibly including the
0992      * default clksel) may be inappropriately excluded on certain
0993      * chips.
0994      */
0995     clksel = (cg_in(cg, hwc->reg) & CLKSEL_MASK) >> CLKSEL_SHIFT;
0996     div = get_pll_div(cg, hwc, clksel);
0997     if (!div) {
0998         kfree(hwc);
0999         return NULL;
1000     }
1001 
1002     max_rate = clk_get_rate(div->clk);
1003     pct80_rate = max_rate * 8;
1004     do_div(pct80_rate, 10);
1005 
1006     plat_rate = clk_get_rate(cg->pll[PLATFORM_PLL].div[PLL_DIV1].clk);
1007 
1008     if (cg->info.flags & CG_CMUX_GE_PLAT)
1009         min_rate = plat_rate;
1010     else
1011         min_rate = plat_rate / 2;
1012 
1013     return create_mux_common(cg, hwc, &cmux_ops, min_rate, max_rate,
1014                  pct80_rate, "cg-cmux%d", idx);
1015 }
1016 
1017 static struct clk * __init create_one_hwaccel(struct clockgen *cg, int idx)
1018 {
1019     struct mux_hwclock *hwc;
1020 
1021     hwc = kzalloc(sizeof(*hwc), GFP_KERNEL);
1022     if (!hwc)
1023         return NULL;
1024 
1025     hwc->reg = cg->regs + 0x20 * idx + 0x10;
1026     hwc->info = cg->info.hwaccel[idx];
1027 
1028     return create_mux_common(cg, hwc, &hwaccel_ops, 0, ULONG_MAX, 0,
1029                  "cg-hwaccel%d", idx);
1030 }
1031 
1032 static void __init create_muxes(struct clockgen *cg)
1033 {
1034     int i;
1035 
1036     for (i = 0; i < ARRAY_SIZE(cg->cmux); i++) {
1037         if (cg->info.cmux_to_group[i] < 0)
1038             break;
1039         if (cg->info.cmux_to_group[i] >=
1040             ARRAY_SIZE(cg->info.cmux_groups)) {
1041             WARN_ON_ONCE(1);
1042             continue;
1043         }
1044 
1045         cg->cmux[i] = create_one_cmux(cg, i);
1046     }
1047 
1048     for (i = 0; i < ARRAY_SIZE(cg->hwaccel); i++) {
1049         if (!cg->info.hwaccel[i])
1050             continue;
1051 
1052         cg->hwaccel[i] = create_one_hwaccel(cg, i);
1053     }
1054 }
1055 
1056 static void __init _clockgen_init(struct device_node *np, bool legacy);
1057 
1058 /*
1059  * Legacy nodes may get probed before the parent clockgen node.
1060  * It is assumed that device trees with legacy nodes will not
1061  * contain a "clocks" property -- otherwise the input clocks may
1062  * not be initialized at this point.
1063  */
1064 static void __init legacy_init_clockgen(struct device_node *np)
1065 {
1066     if (!clockgen.node)
1067         _clockgen_init(of_get_parent(np), true);
1068 }
1069 
1070 /* Legacy node */
1071 static void __init core_mux_init(struct device_node *np)
1072 {
1073     struct clk *clk;
1074     struct resource res;
1075     int idx, rc;
1076 
1077     legacy_init_clockgen(np);
1078 
1079     if (of_address_to_resource(np, 0, &res))
1080         return;
1081 
1082     idx = (res.start & 0xf0) >> 5;
1083     clk = clockgen.cmux[idx];
1084 
1085     rc = of_clk_add_provider(np, of_clk_src_simple_get, clk);
1086     if (rc) {
1087         pr_err("%s: Couldn't register clk provider for node %pOFn: %d\n",
1088                __func__, np, rc);
1089         return;
1090     }
1091 }
1092 
1093 static struct clk __init
1094 *sysclk_from_fixed(struct device_node *node, const char *name)
1095 {
1096     u32 rate;
1097 
1098     if (of_property_read_u32(node, "clock-frequency", &rate))
1099         return ERR_PTR(-ENODEV);
1100 
1101     return clk_register_fixed_rate(NULL, name, NULL, 0, rate);
1102 }
1103 
1104 static struct clk __init *input_clock(const char *name, struct clk *clk)
1105 {
1106     const char *input_name;
1107 
1108     /* Register the input clock under the desired name. */
1109     input_name = __clk_get_name(clk);
1110     clk = clk_register_fixed_factor(NULL, name, input_name,
1111                     0, 1, 1);
1112     if (IS_ERR(clk))
1113         pr_err("%s: Couldn't register %s: %ld\n", __func__, name,
1114                PTR_ERR(clk));
1115 
1116     return clk;
1117 }
1118 
1119 static struct clk __init *input_clock_by_name(const char *name,
1120                           const char *dtname)
1121 {
1122     struct clk *clk;
1123 
1124     clk = of_clk_get_by_name(clockgen.node, dtname);
1125     if (IS_ERR(clk))
1126         return clk;
1127 
1128     return input_clock(name, clk);
1129 }
1130 
1131 static struct clk __init *input_clock_by_index(const char *name, int idx)
1132 {
1133     struct clk *clk;
1134 
1135     clk = of_clk_get(clockgen.node, 0);
1136     if (IS_ERR(clk))
1137         return clk;
1138 
1139     return input_clock(name, clk);
1140 }
1141 
1142 static struct clk * __init create_sysclk(const char *name)
1143 {
1144     struct device_node *sysclk;
1145     struct clk *clk;
1146 
1147     clk = sysclk_from_fixed(clockgen.node, name);
1148     if (!IS_ERR(clk))
1149         return clk;
1150 
1151     clk = input_clock_by_name(name, "sysclk");
1152     if (!IS_ERR(clk))
1153         return clk;
1154 
1155     clk = input_clock_by_index(name, 0);
1156     if (!IS_ERR(clk))
1157         return clk;
1158 
1159     sysclk = of_get_child_by_name(clockgen.node, "sysclk");
1160     if (sysclk) {
1161         clk = sysclk_from_fixed(sysclk, name);
1162         if (!IS_ERR(clk))
1163             return clk;
1164     }
1165 
1166     pr_err("%s: No input sysclk\n", __func__);
1167     return NULL;
1168 }
1169 
1170 static struct clk * __init create_coreclk(const char *name)
1171 {
1172     struct clk *clk;
1173 
1174     clk = input_clock_by_name(name, "coreclk");
1175     if (!IS_ERR(clk))
1176         return clk;
1177 
1178     /*
1179      * This indicates a mix of legacy nodes with the new coreclk
1180      * mechanism, which should never happen.  If this error occurs,
1181      * don't use the wrong input clock just because coreclk isn't
1182      * ready yet.
1183      */
1184     if (WARN_ON(PTR_ERR(clk) == -EPROBE_DEFER))
1185         return clk;
1186 
1187     return NULL;
1188 }
1189 
1190 /* Legacy node */
1191 static void __init sysclk_init(struct device_node *node)
1192 {
1193     struct clk *clk;
1194 
1195     legacy_init_clockgen(node);
1196 
1197     clk = clockgen.sysclk;
1198     if (clk)
1199         of_clk_add_provider(node, of_clk_src_simple_get, clk);
1200 }
1201 
1202 #define PLL_KILL BIT(31)
1203 
1204 static void __init create_one_pll(struct clockgen *cg, int idx)
1205 {
1206     u32 __iomem *reg;
1207     u32 mult;
1208     struct clockgen_pll *pll = &cg->pll[idx];
1209     const char *input = "cg-sysclk";
1210     int i;
1211 
1212     if (!(cg->info.pll_mask & (1 << idx)))
1213         return;
1214 
1215     if (cg->coreclk && idx != PLATFORM_PLL) {
1216         if (IS_ERR(cg->coreclk))
1217             return;
1218 
1219         input = "cg-coreclk";
1220     }
1221 
1222     if (cg->info.flags & CG_VER3) {
1223         switch (idx) {
1224         case PLATFORM_PLL:
1225             reg = cg->regs + 0x60080;
1226             break;
1227         case CGA_PLL1:
1228             reg = cg->regs + 0x80;
1229             break;
1230         case CGA_PLL2:
1231             reg = cg->regs + 0xa0;
1232             break;
1233         case CGB_PLL1:
1234             reg = cg->regs + 0x10080;
1235             break;
1236         case CGB_PLL2:
1237             reg = cg->regs + 0x100a0;
1238             break;
1239         default:
1240             WARN_ONCE(1, "index %d\n", idx);
1241             return;
1242         }
1243     } else {
1244         if (idx == PLATFORM_PLL)
1245             reg = cg->regs + 0xc00;
1246         else
1247             reg = cg->regs + 0x800 + 0x20 * (idx - 1);
1248     }
1249 
1250     /* Get the multiple of PLL */
1251     mult = cg_in(cg, reg);
1252 
1253     /* Check if this PLL is disabled */
1254     if (mult & PLL_KILL) {
1255         pr_debug("%s(): pll %p disabled\n", __func__, reg);
1256         return;
1257     }
1258 
1259     if ((cg->info.flags & CG_VER3) ||
1260         ((cg->info.flags & CG_PLL_8BIT) && idx != PLATFORM_PLL))
1261         mult = (mult & GENMASK(8, 1)) >> 1;
1262     else
1263         mult = (mult & GENMASK(6, 1)) >> 1;
1264 
1265     for (i = 0; i < ARRAY_SIZE(pll->div); i++) {
1266         struct clk *clk;
1267         int ret;
1268 
1269         /*
1270          * For platform PLL, there are MAX_PLL_DIV divider clocks.
1271          * For core PLL, there are 4 divider clocks at most.
1272          */
1273         if (idx != PLATFORM_PLL && i >= 4)
1274             break;
1275 
1276         snprintf(pll->div[i].name, sizeof(pll->div[i].name),
1277              "cg-pll%d-div%d", idx, i + 1);
1278 
1279         clk = clk_register_fixed_factor(NULL,
1280                 pll->div[i].name, input, 0, mult, i + 1);
1281         if (IS_ERR(clk)) {
1282             pr_err("%s: %s: register failed %ld\n",
1283                    __func__, pll->div[i].name, PTR_ERR(clk));
1284             continue;
1285         }
1286 
1287         pll->div[i].clk = clk;
1288         ret = clk_register_clkdev(clk, pll->div[i].name, NULL);
1289         if (ret != 0)
1290             pr_err("%s: %s: register to lookup table failed %d\n",
1291                    __func__, pll->div[i].name, ret);
1292 
1293     }
1294 }
1295 
1296 static void __init create_plls(struct clockgen *cg)
1297 {
1298     int i;
1299 
1300     for (i = 0; i < ARRAY_SIZE(cg->pll); i++)
1301         create_one_pll(cg, i);
1302 }
1303 
1304 static void __init legacy_pll_init(struct device_node *np, int idx)
1305 {
1306     struct clockgen_pll *pll;
1307     struct clk_onecell_data *onecell_data;
1308     struct clk **subclks;
1309     int count, rc;
1310 
1311     legacy_init_clockgen(np);
1312 
1313     pll = &clockgen.pll[idx];
1314     count = of_property_count_strings(np, "clock-output-names");
1315 
1316     BUILD_BUG_ON(ARRAY_SIZE(pll->div) < 4);
1317     subclks = kcalloc(4, sizeof(struct clk *), GFP_KERNEL);
1318     if (!subclks)
1319         return;
1320 
1321     onecell_data = kmalloc(sizeof(*onecell_data), GFP_KERNEL);
1322     if (!onecell_data)
1323         goto err_clks;
1324 
1325     if (count <= 3) {
1326         subclks[0] = pll->div[0].clk;
1327         subclks[1] = pll->div[1].clk;
1328         subclks[2] = pll->div[3].clk;
1329     } else {
1330         subclks[0] = pll->div[0].clk;
1331         subclks[1] = pll->div[1].clk;
1332         subclks[2] = pll->div[2].clk;
1333         subclks[3] = pll->div[3].clk;
1334     }
1335 
1336     onecell_data->clks = subclks;
1337     onecell_data->clk_num = count;
1338 
1339     rc = of_clk_add_provider(np, of_clk_src_onecell_get, onecell_data);
1340     if (rc) {
1341         pr_err("%s: Couldn't register clk provider for node %pOFn: %d\n",
1342                __func__, np, rc);
1343         goto err_cell;
1344     }
1345 
1346     return;
1347 err_cell:
1348     kfree(onecell_data);
1349 err_clks:
1350     kfree(subclks);
1351 }
1352 
1353 /* Legacy node */
1354 static void __init pltfrm_pll_init(struct device_node *np)
1355 {
1356     legacy_pll_init(np, PLATFORM_PLL);
1357 }
1358 
1359 /* Legacy node */
1360 static void __init core_pll_init(struct device_node *np)
1361 {
1362     struct resource res;
1363     int idx;
1364 
1365     if (of_address_to_resource(np, 0, &res))
1366         return;
1367 
1368     if ((res.start & 0xfff) == 0xc00) {
1369         /*
1370          * ls1021a devtree labels the platform PLL
1371          * with the core PLL compatible
1372          */
1373         pltfrm_pll_init(np);
1374     } else {
1375         idx = (res.start & 0xf0) >> 5;
1376         legacy_pll_init(np, CGA_PLL1 + idx);
1377     }
1378 }
1379 
1380 static struct clk *clockgen_clk_get(struct of_phandle_args *clkspec, void *data)
1381 {
1382     struct clockgen *cg = data;
1383     struct clk *clk;
1384     struct clockgen_pll *pll;
1385     u32 type, idx;
1386 
1387     if (clkspec->args_count < 2) {
1388         pr_err("%s: insufficient phandle args\n", __func__);
1389         return ERR_PTR(-EINVAL);
1390     }
1391 
1392     type = clkspec->args[0];
1393     idx = clkspec->args[1];
1394 
1395     switch (type) {
1396     case QORIQ_CLK_SYSCLK:
1397         if (idx != 0)
1398             goto bad_args;
1399         clk = cg->sysclk;
1400         break;
1401     case QORIQ_CLK_CMUX:
1402         if (idx >= ARRAY_SIZE(cg->cmux))
1403             goto bad_args;
1404         clk = cg->cmux[idx];
1405         break;
1406     case QORIQ_CLK_HWACCEL:
1407         if (idx >= ARRAY_SIZE(cg->hwaccel))
1408             goto bad_args;
1409         clk = cg->hwaccel[idx];
1410         break;
1411     case QORIQ_CLK_FMAN:
1412         if (idx >= ARRAY_SIZE(cg->fman))
1413             goto bad_args;
1414         clk = cg->fman[idx];
1415         break;
1416     case QORIQ_CLK_PLATFORM_PLL:
1417         pll = &cg->pll[PLATFORM_PLL];
1418         if (idx >= ARRAY_SIZE(pll->div))
1419             goto bad_args;
1420         clk = pll->div[idx].clk;
1421         break;
1422     case QORIQ_CLK_CORECLK:
1423         if (idx != 0)
1424             goto bad_args;
1425         clk = cg->coreclk;
1426         if (IS_ERR(clk))
1427             clk = NULL;
1428         break;
1429     default:
1430         goto bad_args;
1431     }
1432 
1433     if (!clk)
1434         return ERR_PTR(-ENOENT);
1435     return clk;
1436 
1437 bad_args:
1438     pr_err("%s: Bad phandle args %u %u\n", __func__, type, idx);
1439     return ERR_PTR(-EINVAL);
1440 }
1441 
1442 #ifdef CONFIG_PPC
1443 #include <asm/mpc85xx.h>
1444 
1445 static const u32 a4510_svrs[] __initconst = {
1446     (SVR_P2040 << 8) | 0x10,    /* P2040 1.0 */
1447     (SVR_P2040 << 8) | 0x11,    /* P2040 1.1 */
1448     (SVR_P2041 << 8) | 0x10,    /* P2041 1.0 */
1449     (SVR_P2041 << 8) | 0x11,    /* P2041 1.1 */
1450     (SVR_P3041 << 8) | 0x10,    /* P3041 1.0 */
1451     (SVR_P3041 << 8) | 0x11,    /* P3041 1.1 */
1452     (SVR_P4040 << 8) | 0x20,    /* P4040 2.0 */
1453     (SVR_P4080 << 8) | 0x20,    /* P4080 2.0 */
1454     (SVR_P5010 << 8) | 0x10,    /* P5010 1.0 */
1455     (SVR_P5010 << 8) | 0x20,    /* P5010 2.0 */
1456     (SVR_P5020 << 8) | 0x10,    /* P5020 1.0 */
1457     (SVR_P5021 << 8) | 0x10,    /* P5021 1.0 */
1458     (SVR_P5040 << 8) | 0x10,    /* P5040 1.0 */
1459 };
1460 
1461 #define SVR_SECURITY    0x80000 /* The Security (E) bit */
1462 
1463 static bool __init has_erratum_a4510(void)
1464 {
1465     u32 svr = mfspr(SPRN_SVR);
1466     int i;
1467 
1468     svr &= ~SVR_SECURITY;
1469 
1470     for (i = 0; i < ARRAY_SIZE(a4510_svrs); i++) {
1471         if (svr == a4510_svrs[i])
1472             return true;
1473     }
1474 
1475     return false;
1476 }
1477 #else
1478 static bool __init has_erratum_a4510(void)
1479 {
1480     return false;
1481 }
1482 #endif
1483 
1484 static void __init _clockgen_init(struct device_node *np, bool legacy)
1485 {
1486     int i, ret;
1487     bool is_old_ls1021a = false;
1488 
1489     /* May have already been called by a legacy probe */
1490     if (clockgen.node)
1491         return;
1492 
1493     clockgen.node = np;
1494     clockgen.regs = of_iomap(np, 0);
1495     if (!clockgen.regs &&
1496         of_device_is_compatible(of_root, "fsl,ls1021a")) {
1497         /* Compatibility hack for old, broken device trees */
1498         clockgen.regs = ioremap(0x1ee1000, 0x1000);
1499         is_old_ls1021a = true;
1500     }
1501     if (!clockgen.regs) {
1502         pr_err("%s(): %pOFn: of_iomap() failed\n", __func__, np);
1503         return;
1504     }
1505 
1506     for (i = 0; i < ARRAY_SIZE(chipinfo); i++) {
1507         if (of_device_is_compatible(np, chipinfo[i].compat))
1508             break;
1509         if (is_old_ls1021a &&
1510             !strcmp(chipinfo[i].compat, "fsl,ls1021a-clockgen"))
1511             break;
1512     }
1513 
1514     if (i == ARRAY_SIZE(chipinfo)) {
1515         pr_err("%s: unknown clockgen node %pOF\n", __func__, np);
1516         goto err;
1517     }
1518     clockgen.info = chipinfo[i];
1519 
1520     if (clockgen.info.guts_compat) {
1521         struct device_node *guts;
1522 
1523         guts = of_find_compatible_node(NULL, NULL,
1524                            clockgen.info.guts_compat);
1525         if (guts) {
1526             clockgen.guts = of_iomap(guts, 0);
1527             if (!clockgen.guts) {
1528                 pr_err("%s: Couldn't map %pOF regs\n", __func__,
1529                        guts);
1530             }
1531             of_node_put(guts);
1532         }
1533 
1534     }
1535 
1536     if (has_erratum_a4510())
1537         clockgen.info.flags |= CG_CMUX_GE_PLAT;
1538 
1539     clockgen.sysclk = create_sysclk("cg-sysclk");
1540     clockgen.coreclk = create_coreclk("cg-coreclk");
1541     create_plls(&clockgen);
1542     create_muxes(&clockgen);
1543 
1544     if (clockgen.info.init_periph)
1545         clockgen.info.init_periph(&clockgen);
1546 
1547     ret = of_clk_add_provider(np, clockgen_clk_get, &clockgen);
1548     if (ret) {
1549         pr_err("%s: Couldn't register clk provider for node %pOFn: %d\n",
1550                __func__, np, ret);
1551     }
1552 
1553     /* Don't create cpufreq device for legacy clockgen blocks */
1554     add_cpufreq_dev = !legacy;
1555 
1556     return;
1557 err:
1558     iounmap(clockgen.regs);
1559     clockgen.regs = NULL;
1560 }
1561 
1562 static void __init clockgen_init(struct device_node *np)
1563 {
1564     _clockgen_init(np, false);
1565 }
1566 
1567 static int __init clockgen_cpufreq_init(void)
1568 {
1569     struct platform_device *pdev;
1570 
1571     if (add_cpufreq_dev) {
1572         pdev = platform_device_register_simple("qoriq-cpufreq", -1,
1573                 NULL, 0);
1574         if (IS_ERR(pdev))
1575             pr_err("Couldn't register qoriq-cpufreq err=%ld\n",
1576                 PTR_ERR(pdev));
1577     }
1578     return 0;
1579 }
1580 device_initcall(clockgen_cpufreq_init);
1581 
1582 CLK_OF_DECLARE(qoriq_clockgen_1, "fsl,qoriq-clockgen-1.0", clockgen_init);
1583 CLK_OF_DECLARE(qoriq_clockgen_2, "fsl,qoriq-clockgen-2.0", clockgen_init);
1584 CLK_OF_DECLARE(qoriq_clockgen_b4420, "fsl,b4420-clockgen", clockgen_init);
1585 CLK_OF_DECLARE(qoriq_clockgen_b4860, "fsl,b4860-clockgen", clockgen_init);
1586 CLK_OF_DECLARE(qoriq_clockgen_ls1012a, "fsl,ls1012a-clockgen", clockgen_init);
1587 CLK_OF_DECLARE(qoriq_clockgen_ls1021a, "fsl,ls1021a-clockgen", clockgen_init);
1588 CLK_OF_DECLARE(qoriq_clockgen_ls1028a, "fsl,ls1028a-clockgen", clockgen_init);
1589 CLK_OF_DECLARE(qoriq_clockgen_ls1043a, "fsl,ls1043a-clockgen", clockgen_init);
1590 CLK_OF_DECLARE(qoriq_clockgen_ls1046a, "fsl,ls1046a-clockgen", clockgen_init);
1591 CLK_OF_DECLARE(qoriq_clockgen_ls1088a, "fsl,ls1088a-clockgen", clockgen_init);
1592 CLK_OF_DECLARE(qoriq_clockgen_ls2080a, "fsl,ls2080a-clockgen", clockgen_init);
1593 CLK_OF_DECLARE(qoriq_clockgen_lx2160a, "fsl,lx2160a-clockgen", clockgen_init);
1594 CLK_OF_DECLARE(qoriq_clockgen_p2041, "fsl,p2041-clockgen", clockgen_init);
1595 CLK_OF_DECLARE(qoriq_clockgen_p3041, "fsl,p3041-clockgen", clockgen_init);
1596 CLK_OF_DECLARE(qoriq_clockgen_p4080, "fsl,p4080-clockgen", clockgen_init);
1597 CLK_OF_DECLARE(qoriq_clockgen_p5020, "fsl,p5020-clockgen", clockgen_init);
1598 CLK_OF_DECLARE(qoriq_clockgen_p5040, "fsl,p5040-clockgen", clockgen_init);
1599 CLK_OF_DECLARE(qoriq_clockgen_t1023, "fsl,t1023-clockgen", clockgen_init);
1600 CLK_OF_DECLARE(qoriq_clockgen_t1040, "fsl,t1040-clockgen", clockgen_init);
1601 CLK_OF_DECLARE(qoriq_clockgen_t2080, "fsl,t2080-clockgen", clockgen_init);
1602 CLK_OF_DECLARE(qoriq_clockgen_t4240, "fsl,t4240-clockgen", clockgen_init);
1603 
1604 /* Legacy nodes */
1605 CLK_OF_DECLARE(qoriq_sysclk_1, "fsl,qoriq-sysclk-1.0", sysclk_init);
1606 CLK_OF_DECLARE(qoriq_sysclk_2, "fsl,qoriq-sysclk-2.0", sysclk_init);
1607 CLK_OF_DECLARE(qoriq_core_pll_1, "fsl,qoriq-core-pll-1.0", core_pll_init);
1608 CLK_OF_DECLARE(qoriq_core_pll_2, "fsl,qoriq-core-pll-2.0", core_pll_init);
1609 CLK_OF_DECLARE(qoriq_core_mux_1, "fsl,qoriq-core-mux-1.0", core_mux_init);
1610 CLK_OF_DECLARE(qoriq_core_mux_2, "fsl,qoriq-core-mux-2.0", core_mux_init);
1611 CLK_OF_DECLARE(qoriq_pltfrm_pll_1, "fsl,qoriq-platform-pll-1.0", pltfrm_pll_init);
1612 CLK_OF_DECLARE(qoriq_pltfrm_pll_2, "fsl,qoriq-platform-pll-2.0", pltfrm_pll_init);