Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (C) 2013 DENX Software Engineering
0004  *
0005  * Gerhard Sittig, <gsi@denx.de>
0006  *
0007  * common clock driver support for the MPC512x platform
0008  */
0009 
0010 #include <linux/bitops.h>
0011 #include <linux/clk.h>
0012 #include <linux/clk-provider.h>
0013 #include <linux/clkdev.h>
0014 #include <linux/device.h>
0015 #include <linux/errno.h>
0016 #include <linux/io.h>
0017 #include <linux/of.h>
0018 #include <linux/of_address.h>
0019 
0020 #include <asm/mpc5121.h>
0021 #include <dt-bindings/clock/mpc512x-clock.h>
0022 
0023 #include "mpc512x.h"        /* our public mpc5121_clk_init() API */
0024 
0025 /* helpers to keep the MCLK intermediates "somewhere" in our table */
0026 enum {
0027     MCLK_IDX_MUX0,
0028     MCLK_IDX_EN0,
0029     MCLK_IDX_DIV0,
0030     MCLK_MAX_IDX,
0031 };
0032 
0033 #define NR_PSCS         12
0034 #define NR_MSCANS       4
0035 #define NR_SPDIFS       1
0036 #define NR_OUTCLK       4
0037 #define NR_MCLKS        (NR_PSCS + NR_MSCANS + NR_SPDIFS + NR_OUTCLK)
0038 
0039 /* extend the public set of clocks by adding internal slots for management */
0040 enum {
0041     /* arrange for adjacent numbers after the public set */
0042     MPC512x_CLK_START_PRIVATE = MPC512x_CLK_LAST_PUBLIC,
0043     /* clocks which aren't announced to the public */
0044     MPC512x_CLK_DDR,
0045     MPC512x_CLK_MEM,
0046     MPC512x_CLK_IIM,
0047     /* intermediates in div+gate combos or fractional dividers */
0048     MPC512x_CLK_DDR_UG,
0049     MPC512x_CLK_SDHC_x4,
0050     MPC512x_CLK_SDHC_UG,
0051     MPC512x_CLK_SDHC2_UG,
0052     MPC512x_CLK_DIU_x4,
0053     MPC512x_CLK_DIU_UG,
0054     MPC512x_CLK_MBX_BUS_UG,
0055     MPC512x_CLK_MBX_UG,
0056     MPC512x_CLK_MBX_3D_UG,
0057     MPC512x_CLK_PCI_UG,
0058     MPC512x_CLK_NFC_UG,
0059     MPC512x_CLK_LPC_UG,
0060     MPC512x_CLK_SPDIF_TX_IN,
0061     /* intermediates for the mux+gate+div+mux MCLK generation */
0062     MPC512x_CLK_MCLKS_FIRST,
0063     MPC512x_CLK_MCLKS_LAST = MPC512x_CLK_MCLKS_FIRST
0064                 + NR_MCLKS * MCLK_MAX_IDX,
0065     /* internal, symbolic spec for the number of slots */
0066     MPC512x_CLK_LAST_PRIVATE,
0067 };
0068 
0069 /* data required for the OF clock provider registration */
0070 static struct clk *clks[MPC512x_CLK_LAST_PRIVATE];
0071 static struct clk_onecell_data clk_data;
0072 
0073 /* CCM register access */
0074 static struct mpc512x_ccm __iomem *clkregs;
0075 static DEFINE_SPINLOCK(clklock);
0076 
0077 /* SoC variants {{{ */
0078 
0079 /*
0080  * tell SoC variants apart as they are rather similar yet not identical,
0081  * cache the result in an enum to not repeatedly run the expensive OF test
0082  *
0083  * MPC5123 is an MPC5121 without the MBX graphics accelerator
0084  *
0085  * MPC5125 has many more differences: no MBX, no AXE, no VIU, no SPDIF,
0086  * no PATA, no SATA, no PCI, two FECs (of different compatibility name),
0087  * only 10 PSCs (of different compatibility name), two SDHCs, different
0088  * NFC IP block, output clocks, system PLL status query, different CPMF
0089  * interpretation, no CFM, different fourth PSC/CAN mux0 input -- yet
0090  * those differences can get folded into this clock provider support
0091  * code and don't warrant a separate highly redundant implementation
0092  */
0093 
0094 static enum soc_type {
0095     MPC512x_SOC_MPC5121,
0096     MPC512x_SOC_MPC5123,
0097     MPC512x_SOC_MPC5125,
0098 } soc;
0099 
0100 static void __init mpc512x_clk_determine_soc(void)
0101 {
0102     if (of_machine_is_compatible("fsl,mpc5121")) {
0103         soc = MPC512x_SOC_MPC5121;
0104         return;
0105     }
0106     if (of_machine_is_compatible("fsl,mpc5123")) {
0107         soc = MPC512x_SOC_MPC5123;
0108         return;
0109     }
0110     if (of_machine_is_compatible("fsl,mpc5125")) {
0111         soc = MPC512x_SOC_MPC5125;
0112         return;
0113     }
0114 }
0115 
0116 static bool __init soc_has_mbx(void)
0117 {
0118     if (soc == MPC512x_SOC_MPC5121)
0119         return true;
0120     return false;
0121 }
0122 
0123 static bool __init soc_has_axe(void)
0124 {
0125     if (soc == MPC512x_SOC_MPC5125)
0126         return false;
0127     return true;
0128 }
0129 
0130 static bool __init soc_has_viu(void)
0131 {
0132     if (soc == MPC512x_SOC_MPC5125)
0133         return false;
0134     return true;
0135 }
0136 
0137 static bool __init soc_has_spdif(void)
0138 {
0139     if (soc == MPC512x_SOC_MPC5125)
0140         return false;
0141     return true;
0142 }
0143 
0144 static bool __init soc_has_pata(void)
0145 {
0146     if (soc == MPC512x_SOC_MPC5125)
0147         return false;
0148     return true;
0149 }
0150 
0151 static bool __init soc_has_sata(void)
0152 {
0153     if (soc == MPC512x_SOC_MPC5125)
0154         return false;
0155     return true;
0156 }
0157 
0158 static bool __init soc_has_pci(void)
0159 {
0160     if (soc == MPC512x_SOC_MPC5125)
0161         return false;
0162     return true;
0163 }
0164 
0165 static bool __init soc_has_fec2(void)
0166 {
0167     if (soc == MPC512x_SOC_MPC5125)
0168         return true;
0169     return false;
0170 }
0171 
0172 static int __init soc_max_pscnum(void)
0173 {
0174     if (soc == MPC512x_SOC_MPC5125)
0175         return 10;
0176     return 12;
0177 }
0178 
0179 static bool __init soc_has_sdhc2(void)
0180 {
0181     if (soc == MPC512x_SOC_MPC5125)
0182         return true;
0183     return false;
0184 }
0185 
0186 static bool __init soc_has_nfc_5125(void)
0187 {
0188     if (soc == MPC512x_SOC_MPC5125)
0189         return true;
0190     return false;
0191 }
0192 
0193 static bool __init soc_has_outclk(void)
0194 {
0195     if (soc == MPC512x_SOC_MPC5125)
0196         return true;
0197     return false;
0198 }
0199 
0200 static bool __init soc_has_cpmf_0_bypass(void)
0201 {
0202     if (soc == MPC512x_SOC_MPC5125)
0203         return true;
0204     return false;
0205 }
0206 
0207 static bool __init soc_has_mclk_mux0_canin(void)
0208 {
0209     if (soc == MPC512x_SOC_MPC5125)
0210         return true;
0211     return false;
0212 }
0213 
0214 /* }}} SoC variants */
0215 /* common clk API wrappers {{{ */
0216 
0217 /* convenience wrappers around the common clk API */
0218 static inline struct clk *mpc512x_clk_fixed(const char *name, int rate)
0219 {
0220     return clk_register_fixed_rate(NULL, name, NULL, 0, rate);
0221 }
0222 
0223 static inline struct clk *mpc512x_clk_factor(
0224     const char *name, const char *parent_name,
0225     int mul, int div)
0226 {
0227     int clkflags;
0228 
0229     clkflags = CLK_SET_RATE_PARENT;
0230     return clk_register_fixed_factor(NULL, name, parent_name, clkflags,
0231                      mul, div);
0232 }
0233 
0234 static inline struct clk *mpc512x_clk_divider(
0235     const char *name, const char *parent_name, u8 clkflags,
0236     u32 __iomem *reg, u8 pos, u8 len, int divflags)
0237 {
0238     divflags |= CLK_DIVIDER_BIG_ENDIAN;
0239     return clk_register_divider(NULL, name, parent_name, clkflags,
0240                     reg, pos, len, divflags, &clklock);
0241 }
0242 
0243 static inline struct clk *mpc512x_clk_divtable(
0244     const char *name, const char *parent_name,
0245     u32 __iomem *reg, u8 pos, u8 len,
0246     const struct clk_div_table *divtab)
0247 {
0248     u8 divflags;
0249 
0250     divflags = CLK_DIVIDER_BIG_ENDIAN;
0251     return clk_register_divider_table(NULL, name, parent_name, 0,
0252                       reg, pos, len, divflags,
0253                       divtab, &clklock);
0254 }
0255 
0256 static inline struct clk *mpc512x_clk_gated(
0257     const char *name, const char *parent_name,
0258     u32 __iomem *reg, u8 pos)
0259 {
0260     int clkflags;
0261     u8 gateflags;
0262 
0263     clkflags = CLK_SET_RATE_PARENT;
0264     gateflags = CLK_GATE_BIG_ENDIAN;
0265     return clk_register_gate(NULL, name, parent_name, clkflags,
0266                  reg, pos, gateflags, &clklock);
0267 }
0268 
0269 static inline struct clk *mpc512x_clk_muxed(const char *name,
0270     const char **parent_names, int parent_count,
0271     u32 __iomem *reg, u8 pos, u8 len)
0272 {
0273     int clkflags;
0274     u8 muxflags;
0275 
0276     clkflags = CLK_SET_RATE_PARENT;
0277     muxflags = CLK_MUX_BIG_ENDIAN;
0278     return clk_register_mux(NULL, name,
0279                 parent_names, parent_count, clkflags,
0280                 reg, pos, len, muxflags, &clklock);
0281 }
0282 
0283 /* }}} common clk API wrappers */
0284 
0285 /* helper to isolate a bit field from a register */
0286 static inline int get_bit_field(uint32_t __iomem *reg, uint8_t pos, uint8_t len)
0287 {
0288     uint32_t val;
0289 
0290     val = in_be32(reg);
0291     val >>= pos;
0292     val &= (1 << len) - 1;
0293     return val;
0294 }
0295 
0296 /* get the SPMF and translate it into the "sys pll" multiplier */
0297 static int __init get_spmf_mult(void)
0298 {
0299     static int spmf_to_mult[] = {
0300         68, 1, 12, 16, 20, 24, 28, 32,
0301         36, 40, 44, 48, 52, 56, 60, 64,
0302     };
0303     int spmf;
0304 
0305     spmf = get_bit_field(&clkregs->spmr, 24, 4);
0306     return spmf_to_mult[spmf];
0307 }
0308 
0309 /*
0310  * get the SYS_DIV value and translate it into a divide factor
0311  *
0312  * values returned from here are a multiple of the real factor since the
0313  * divide ratio is fractional
0314  */
0315 static int __init get_sys_div_x2(void)
0316 {
0317     static int sysdiv_code_to_x2[] = {
0318         4, 5, 6, 7, 8, 9, 10, 14,
0319         12, 16, 18, 22, 20, 24, 26, 30,
0320         28, 32, 34, 38, 36, 40, 42, 46,
0321         44, 48, 50, 54, 52, 56, 58, 62,
0322         60, 64, 66,
0323     };
0324     int divcode;
0325 
0326     divcode = get_bit_field(&clkregs->scfr2, 26, 6);
0327     return sysdiv_code_to_x2[divcode];
0328 }
0329 
0330 /*
0331  * get the CPMF value and translate it into a multiplier factor
0332  *
0333  * values returned from here are a multiple of the real factor since the
0334  * multiplier ratio is fractional
0335  */
0336 static int __init get_cpmf_mult_x2(void)
0337 {
0338     static int cpmf_to_mult_x36[] = {
0339         /* 0b000 is "times 36" */
0340         72, 2, 2, 3, 4, 5, 6, 7,
0341     };
0342     static int cpmf_to_mult_0by[] = {
0343         /* 0b000 is "bypass" */
0344         2, 2, 2, 3, 4, 5, 6, 7,
0345     };
0346 
0347     int *cpmf_to_mult;
0348     int cpmf;
0349 
0350     cpmf = get_bit_field(&clkregs->spmr, 16, 4);
0351     if (soc_has_cpmf_0_bypass())
0352         cpmf_to_mult = cpmf_to_mult_0by;
0353     else
0354         cpmf_to_mult = cpmf_to_mult_x36;
0355     return cpmf_to_mult[cpmf];
0356 }
0357 
0358 /*
0359  * some of the clock dividers do scale in a linear way, yet not all of
0360  * their bit combinations are legal; use a divider table to get a
0361  * resulting set of applicable divider values
0362  */
0363 
0364 /* applies to the IPS_DIV, and PCI_DIV values */
0365 static const struct clk_div_table divtab_2346[] = {
0366     { .val = 2, .div = 2, },
0367     { .val = 3, .div = 3, },
0368     { .val = 4, .div = 4, },
0369     { .val = 6, .div = 6, },
0370     { .div = 0, },
0371 };
0372 
0373 /* applies to the MBX_DIV, LPC_DIV, and NFC_DIV values */
0374 static const struct clk_div_table divtab_1234[] = {
0375     { .val = 1, .div = 1, },
0376     { .val = 2, .div = 2, },
0377     { .val = 3, .div = 3, },
0378     { .val = 4, .div = 4, },
0379     { .div = 0, },
0380 };
0381 
0382 static int __init get_freq_from_dt(char *propname)
0383 {
0384     struct device_node *np;
0385     const unsigned int *prop;
0386     int val;
0387 
0388     val = 0;
0389     np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-immr");
0390     if (np) {
0391         prop = of_get_property(np, propname, NULL);
0392         if (prop)
0393             val = *prop;
0394         of_node_put(np);
0395     }
0396     return val;
0397 }
0398 
0399 static void __init mpc512x_clk_preset_data(void)
0400 {
0401     size_t i;
0402 
0403     for (i = 0; i < ARRAY_SIZE(clks); i++)
0404         clks[i] = ERR_PTR(-ENODEV);
0405 }
0406 
0407 /*
0408  * - receives the "bus frequency" from the caller (that's the IPS clock
0409  *   rate, the historical source of clock information)
0410  * - fetches the system PLL multiplier and divider values as well as the
0411  *   IPS divider value from hardware
0412  * - determines the REF clock rate either from the XTAL/OSC spec (if
0413  *   there is a device tree node describing the oscillator) or from the
0414  *   IPS bus clock (supported for backwards compatibility, such that
0415  *   setups without XTAL/OSC specs keep working)
0416  * - creates the "ref" clock item in the clock tree, such that
0417  *   subsequent code can create the remainder of the hierarchy (REF ->
0418  *   SYS -> CSB -> IPS) from the REF clock rate and the returned mul/div
0419  *   values
0420  */
0421 static void __init mpc512x_clk_setup_ref_clock(struct device_node *np, int bus_freq,
0422                     int *sys_mul, int *sys_div,
0423                     int *ips_div)
0424 {
0425     struct clk *osc_clk;
0426     int calc_freq;
0427 
0428     /* fetch mul/div factors from the hardware */
0429     *sys_mul = get_spmf_mult();
0430     *sys_mul *= 2;      /* compensate for the fractional divider */
0431     *sys_div = get_sys_div_x2();
0432     *ips_div = get_bit_field(&clkregs->scfr1, 23, 3);
0433 
0434     /* lookup the oscillator clock for its rate */
0435     osc_clk = of_clk_get_by_name(np, "osc");
0436 
0437     /*
0438      * either descend from OSC to REF (and in bypassing verify the
0439      * IPS rate), or backtrack from IPS and multiplier values that
0440      * were fetched from hardware to REF and thus to the OSC value
0441      *
0442      * in either case the REF clock gets created here and the
0443      * remainder of the clock tree can get spanned from there
0444      */
0445     if (!IS_ERR(osc_clk)) {
0446         clks[MPC512x_CLK_REF] = mpc512x_clk_factor("ref", "osc", 1, 1);
0447         calc_freq = clk_get_rate(clks[MPC512x_CLK_REF]);
0448         calc_freq *= *sys_mul;
0449         calc_freq /= *sys_div;
0450         calc_freq /= 2;
0451         calc_freq /= *ips_div;
0452         if (bus_freq && calc_freq != bus_freq)
0453             pr_warn("calc rate %d != OF spec %d\n",
0454                 calc_freq, bus_freq);
0455     } else {
0456         calc_freq = bus_freq;   /* start with IPS */
0457         calc_freq *= *ips_div;  /* IPS -> CSB */
0458         calc_freq *= 2;     /* CSB -> SYS */
0459         calc_freq *= *sys_div;  /* SYS -> PLL out */
0460         calc_freq /= *sys_mul;  /* PLL out -> REF == OSC */
0461         clks[MPC512x_CLK_REF] = mpc512x_clk_fixed("ref", calc_freq);
0462     }
0463 }
0464 
0465 /* MCLK helpers {{{ */
0466 
0467 /*
0468  * helper code for the MCLK subtree setup
0469  *
0470  * the overview in section 5.2.4 of the MPC5121e Reference Manual rev4
0471  * suggests that all instances of the "PSC clock generation" are equal,
0472  * and that one might re-use the PSC setup for MSCAN clock generation
0473  * (section 5.2.5) as well, at least the logic if not the data for
0474  * description
0475  *
0476  * the details (starting at page 5-20) show differences in the specific
0477  * inputs of the first mux stage ("can clk in", "spdif tx"), and the
0478  * factual non-availability of the second mux stage (it's present yet
0479  * only one input is valid)
0480  *
0481  * the MSCAN clock related registers (starting at page 5-35) all
0482  * reference "spdif clk" at the first mux stage and don't mention any
0483  * "can clk" at all, which somehow is unexpected
0484  *
0485  * TODO re-check the document, and clarify whether the RM is correct in
0486  * the overview or in the details, and whether the difference is a
0487  * clipboard induced error or results from chip revisions
0488  *
0489  * it turns out that the RM rev4 as of 2012-06 talks about "can" for the
0490  * PSCs while RM rev3 as of 2008-10 talks about "spdif", so I guess that
0491  * first a doc update is required which better reflects reality in the
0492  * SoC before the implementation should follow while no questions remain
0493  */
0494 
0495 /*
0496  * note that this declaration raises a checkpatch warning, but
0497  * it's the very data type dictated by <linux/clk-provider.h>,
0498  * "fixing" this warning will break compilation
0499  */
0500 static const char *parent_names_mux0_spdif[] = {
0501     "sys", "ref", "psc-mclk-in", "spdif-tx",
0502 };
0503 
0504 static const char *parent_names_mux0_canin[] = {
0505     "sys", "ref", "psc-mclk-in", "can-clk-in",
0506 };
0507 
0508 enum mclk_type {
0509     MCLK_TYPE_PSC,
0510     MCLK_TYPE_MSCAN,
0511     MCLK_TYPE_SPDIF,
0512     MCLK_TYPE_OUTCLK,
0513 };
0514 
0515 struct mclk_setup_data {
0516     enum mclk_type type;
0517     bool has_mclk1;
0518     const char *name_mux0;
0519     const char *name_en0;
0520     const char *name_div0;
0521     const char *parent_names_mux1[2];
0522     const char *name_mclk;
0523 };
0524 
0525 #define MCLK_SETUP_DATA_PSC(id) { \
0526     MCLK_TYPE_PSC, 0, \
0527     "psc" #id "-mux0", \
0528     "psc" #id "-en0", \
0529     "psc" #id "_mclk_div", \
0530     { "psc" #id "_mclk_div", "dummy", }, \
0531     "psc" #id "_mclk", \
0532 }
0533 
0534 #define MCLK_SETUP_DATA_MSCAN(id) { \
0535     MCLK_TYPE_MSCAN, 0, \
0536     "mscan" #id "-mux0", \
0537     "mscan" #id "-en0", \
0538     "mscan" #id "_mclk_div", \
0539     { "mscan" #id "_mclk_div", "dummy", }, \
0540     "mscan" #id "_mclk", \
0541 }
0542 
0543 #define MCLK_SETUP_DATA_SPDIF { \
0544     MCLK_TYPE_SPDIF, 1, \
0545     "spdif-mux0", \
0546     "spdif-en0", \
0547     "spdif_mclk_div", \
0548     { "spdif_mclk_div", "spdif-rx", }, \
0549     "spdif_mclk", \
0550 }
0551 
0552 #define MCLK_SETUP_DATA_OUTCLK(id) { \
0553     MCLK_TYPE_OUTCLK, 0, \
0554     "out" #id "-mux0", \
0555     "out" #id "-en0", \
0556     "out" #id "_mclk_div", \
0557     { "out" #id "_mclk_div", "dummy", }, \
0558     "out" #id "_clk", \
0559 }
0560 
0561 static struct mclk_setup_data mclk_psc_data[] = {
0562     MCLK_SETUP_DATA_PSC(0),
0563     MCLK_SETUP_DATA_PSC(1),
0564     MCLK_SETUP_DATA_PSC(2),
0565     MCLK_SETUP_DATA_PSC(3),
0566     MCLK_SETUP_DATA_PSC(4),
0567     MCLK_SETUP_DATA_PSC(5),
0568     MCLK_SETUP_DATA_PSC(6),
0569     MCLK_SETUP_DATA_PSC(7),
0570     MCLK_SETUP_DATA_PSC(8),
0571     MCLK_SETUP_DATA_PSC(9),
0572     MCLK_SETUP_DATA_PSC(10),
0573     MCLK_SETUP_DATA_PSC(11),
0574 };
0575 
0576 static struct mclk_setup_data mclk_mscan_data[] = {
0577     MCLK_SETUP_DATA_MSCAN(0),
0578     MCLK_SETUP_DATA_MSCAN(1),
0579     MCLK_SETUP_DATA_MSCAN(2),
0580     MCLK_SETUP_DATA_MSCAN(3),
0581 };
0582 
0583 static struct mclk_setup_data mclk_spdif_data[] = {
0584     MCLK_SETUP_DATA_SPDIF,
0585 };
0586 
0587 static struct mclk_setup_data mclk_outclk_data[] = {
0588     MCLK_SETUP_DATA_OUTCLK(0),
0589     MCLK_SETUP_DATA_OUTCLK(1),
0590     MCLK_SETUP_DATA_OUTCLK(2),
0591     MCLK_SETUP_DATA_OUTCLK(3),
0592 };
0593 
0594 /* setup the MCLK clock subtree of an individual PSC/MSCAN/SPDIF */
0595 static void __init mpc512x_clk_setup_mclk(struct mclk_setup_data *entry, size_t idx)
0596 {
0597     size_t clks_idx_pub, clks_idx_int;
0598     u32 __iomem *mccr_reg;  /* MCLK control register (mux, en, div) */
0599     int div;
0600 
0601     /* derive a few parameters from the component type and index */
0602     switch (entry->type) {
0603     case MCLK_TYPE_PSC:
0604         clks_idx_pub = MPC512x_CLK_PSC0_MCLK + idx;
0605         clks_idx_int = MPC512x_CLK_MCLKS_FIRST
0606                  + (idx) * MCLK_MAX_IDX;
0607         mccr_reg = &clkregs->psc_ccr[idx];
0608         break;
0609     case MCLK_TYPE_MSCAN:
0610         clks_idx_pub = MPC512x_CLK_MSCAN0_MCLK + idx;
0611         clks_idx_int = MPC512x_CLK_MCLKS_FIRST
0612                  + (NR_PSCS + idx) * MCLK_MAX_IDX;
0613         mccr_reg = &clkregs->mscan_ccr[idx];
0614         break;
0615     case MCLK_TYPE_SPDIF:
0616         clks_idx_pub = MPC512x_CLK_SPDIF_MCLK;
0617         clks_idx_int = MPC512x_CLK_MCLKS_FIRST
0618                  + (NR_PSCS + NR_MSCANS) * MCLK_MAX_IDX;
0619         mccr_reg = &clkregs->spccr;
0620         break;
0621     case MCLK_TYPE_OUTCLK:
0622         clks_idx_pub = MPC512x_CLK_OUT0_CLK + idx;
0623         clks_idx_int = MPC512x_CLK_MCLKS_FIRST
0624                  + (NR_PSCS + NR_MSCANS + NR_SPDIFS + idx)
0625                  * MCLK_MAX_IDX;
0626         mccr_reg = &clkregs->out_ccr[idx];
0627         break;
0628     default:
0629         return;
0630     }
0631 
0632     /*
0633      * this was grabbed from the PPC_CLOCK implementation, which
0634      * enforced a specific MCLK divider while the clock was gated
0635      * during setup (that's a documented hardware requirement)
0636      *
0637      * the PPC_CLOCK implementation might even have violated the
0638      * "MCLK <= IPS" constraint, the fixed divider value of 1
0639      * results in a divider of 2 and thus MCLK = SYS/2 which equals
0640      * CSB which is greater than IPS; the serial port setup may have
0641      * adjusted the divider which the clock setup might have left in
0642      * an undesirable state
0643      *
0644      * initial setup is:
0645      * - MCLK 0 from SYS
0646      * - MCLK DIV such to not exceed the IPS clock
0647      * - MCLK 0 enabled
0648      * - MCLK 1 from MCLK DIV
0649      */
0650     div = clk_get_rate(clks[MPC512x_CLK_SYS]);
0651     div /= clk_get_rate(clks[MPC512x_CLK_IPS]);
0652     out_be32(mccr_reg, (0 << 16));
0653     out_be32(mccr_reg, (0 << 16) | ((div - 1) << 17));
0654     out_be32(mccr_reg, (1 << 16) | ((div - 1) << 17));
0655 
0656     /*
0657      * create the 'struct clk' items of the MCLK's clock subtree
0658      *
0659      * note that by design we always create all nodes and won't take
0660      * shortcuts here, because
0661      * - the "internal" MCLK_DIV and MCLK_OUT signal in turn are
0662      *   selectable inputs to the CFM while those who "actually use"
0663      *   the PSC/MSCAN/SPDIF (serial drivers et al) need the MCLK
0664      *   for their bitrate
0665      * - in the absence of "aliases" for clocks we need to create
0666      *   individual 'struct clk' items for whatever might get
0667      *   referenced or looked up, even if several of those items are
0668      *   identical from the logical POV (their rate value)
0669      * - for easier future maintenance and for better reflection of
0670      *   the SoC's documentation, it appears appropriate to generate
0671      *   clock items even for those muxers which actually are NOPs
0672      *   (those with two inputs of which one is reserved)
0673      */
0674     clks[clks_idx_int + MCLK_IDX_MUX0] = mpc512x_clk_muxed(
0675             entry->name_mux0,
0676             soc_has_mclk_mux0_canin()
0677                 ? &parent_names_mux0_canin[0]
0678                 : &parent_names_mux0_spdif[0],
0679             ARRAY_SIZE(parent_names_mux0_spdif),
0680             mccr_reg, 14, 2);
0681     clks[clks_idx_int + MCLK_IDX_EN0] = mpc512x_clk_gated(
0682             entry->name_en0, entry->name_mux0,
0683             mccr_reg, 16);
0684     clks[clks_idx_int + MCLK_IDX_DIV0] = mpc512x_clk_divider(
0685             entry->name_div0,
0686             entry->name_en0, CLK_SET_RATE_GATE,
0687             mccr_reg, 17, 15, 0);
0688     if (entry->has_mclk1) {
0689         clks[clks_idx_pub] = mpc512x_clk_muxed(
0690                 entry->name_mclk,
0691                 &entry->parent_names_mux1[0],
0692                 ARRAY_SIZE(entry->parent_names_mux1),
0693                 mccr_reg, 7, 1);
0694     } else {
0695         clks[clks_idx_pub] = mpc512x_clk_factor(
0696                 entry->name_mclk,
0697                 entry->parent_names_mux1[0],
0698                 1, 1);
0699     }
0700 }
0701 
0702 /* }}} MCLK helpers */
0703 
0704 static void __init mpc512x_clk_setup_clock_tree(struct device_node *np, int busfreq)
0705 {
0706     int sys_mul, sys_div, ips_div;
0707     int mul, div;
0708     size_t mclk_idx;
0709     int freq;
0710 
0711     /*
0712      * developer's notes:
0713      * - consider whether to handle clocks which have both gates and
0714      *   dividers via intermediates or by means of composites
0715      * - fractional dividers appear to not map well to composites
0716      *   since they can be seen as a fixed multiplier and an
0717      *   adjustable divider, while composites can only combine at
0718      *   most one of a mux, div, and gate each into one 'struct clk'
0719      *   item
0720      * - PSC/MSCAN/SPDIF clock generation OTOH already is very
0721      *   specific and cannot get mapped to composites (at least not
0722      *   a single one, maybe two of them, but then some of these
0723      *   intermediate clock signals get referenced elsewhere (e.g.
0724      *   in the clock frequency measurement, CFM) and thus need
0725      *   publicly available names
0726      * - the current source layout appropriately reflects the
0727      *   hardware setup, and it works, so it's questionable whether
0728      *   further changes will result in big enough a benefit
0729      */
0730 
0731     /* regardless of whether XTAL/OSC exists, have REF created */
0732     mpc512x_clk_setup_ref_clock(np, busfreq, &sys_mul, &sys_div, &ips_div);
0733 
0734     /* now setup the REF -> SYS -> CSB -> IPS hierarchy */
0735     clks[MPC512x_CLK_SYS] = mpc512x_clk_factor("sys", "ref",
0736                            sys_mul, sys_div);
0737     clks[MPC512x_CLK_CSB] = mpc512x_clk_factor("csb", "sys", 1, 2);
0738     clks[MPC512x_CLK_IPS] = mpc512x_clk_divtable("ips", "csb",
0739                              &clkregs->scfr1, 23, 3,
0740                              divtab_2346);
0741     /* now setup anything below SYS and CSB and IPS */
0742 
0743     clks[MPC512x_CLK_DDR_UG] = mpc512x_clk_factor("ddr-ug", "sys", 1, 2);
0744 
0745     /*
0746      * the Reference Manual discusses that for SDHC only even divide
0747      * ratios are supported because clock domain synchronization
0748      * between 'per' and 'ipg' is broken;
0749      * keep the divider's bit 0 cleared (per reset value), and only
0750      * allow to setup the divider's bits 7:1, which results in that
0751      * only even divide ratios can get configured upon rate changes;
0752      * keep the "x4" name because this bit shift hack is an internal
0753      * implementation detail, the "fractional divider with quarters"
0754      * semantics remains
0755      */
0756     clks[MPC512x_CLK_SDHC_x4] = mpc512x_clk_factor("sdhc-x4", "csb", 2, 1);
0757     clks[MPC512x_CLK_SDHC_UG] = mpc512x_clk_divider("sdhc-ug", "sdhc-x4", 0,
0758                             &clkregs->scfr2, 1, 7,
0759                             CLK_DIVIDER_ONE_BASED);
0760     if (soc_has_sdhc2()) {
0761         clks[MPC512x_CLK_SDHC2_UG] = mpc512x_clk_divider(
0762                 "sdhc2-ug", "sdhc-x4", 0, &clkregs->scfr2,
0763                 9, 7, CLK_DIVIDER_ONE_BASED);
0764     }
0765 
0766     clks[MPC512x_CLK_DIU_x4] = mpc512x_clk_factor("diu-x4", "csb", 4, 1);
0767     clks[MPC512x_CLK_DIU_UG] = mpc512x_clk_divider("diu-ug", "diu-x4", 0,
0768                                &clkregs->scfr1, 0, 8,
0769                                CLK_DIVIDER_ONE_BASED);
0770 
0771     /*
0772      * the "power architecture PLL" was setup from data which was
0773      * sampled from the reset config word, at this point in time the
0774      * configuration can be considered fixed and read only (i.e. no
0775      * longer adjustable, or no longer in need of adjustment), which
0776      * is why we don't register a PLL here but assume fixed factors
0777      */
0778     mul = get_cpmf_mult_x2();
0779     div = 2;    /* compensate for the fractional factor */
0780     clks[MPC512x_CLK_E300] = mpc512x_clk_factor("e300", "csb", mul, div);
0781 
0782     if (soc_has_mbx()) {
0783         clks[MPC512x_CLK_MBX_BUS_UG] = mpc512x_clk_factor(
0784                 "mbx-bus-ug", "csb", 1, 2);
0785         clks[MPC512x_CLK_MBX_UG] = mpc512x_clk_divtable(
0786                 "mbx-ug", "mbx-bus-ug", &clkregs->scfr1,
0787                 14, 3, divtab_1234);
0788         clks[MPC512x_CLK_MBX_3D_UG] = mpc512x_clk_factor(
0789                 "mbx-3d-ug", "mbx-ug", 1, 1);
0790     }
0791     if (soc_has_pci()) {
0792         clks[MPC512x_CLK_PCI_UG] = mpc512x_clk_divtable(
0793                 "pci-ug", "csb", &clkregs->scfr1,
0794                 20, 3, divtab_2346);
0795     }
0796     if (soc_has_nfc_5125()) {
0797         /*
0798          * XXX TODO implement 5125 NFC clock setup logic,
0799          * with high/low period counters in clkregs->scfr3,
0800          * currently there are no users so it's ENOIMPL
0801          */
0802         clks[MPC512x_CLK_NFC_UG] = ERR_PTR(-ENOTSUPP);
0803     } else {
0804         clks[MPC512x_CLK_NFC_UG] = mpc512x_clk_divtable(
0805                 "nfc-ug", "ips", &clkregs->scfr1,
0806                 8, 3, divtab_1234);
0807     }
0808     clks[MPC512x_CLK_LPC_UG] = mpc512x_clk_divtable("lpc-ug", "ips",
0809                             &clkregs->scfr1, 11, 3,
0810                             divtab_1234);
0811 
0812     clks[MPC512x_CLK_LPC] = mpc512x_clk_gated("lpc", "lpc-ug",
0813                           &clkregs->sccr1, 30);
0814     clks[MPC512x_CLK_NFC] = mpc512x_clk_gated("nfc", "nfc-ug",
0815                           &clkregs->sccr1, 29);
0816     if (soc_has_pata()) {
0817         clks[MPC512x_CLK_PATA] = mpc512x_clk_gated(
0818                 "pata", "ips", &clkregs->sccr1, 28);
0819     }
0820     /* for PSCs there is a "registers" gate and a bitrate MCLK subtree */
0821     for (mclk_idx = 0; mclk_idx < soc_max_pscnum(); mclk_idx++) {
0822         char name[12];
0823         snprintf(name, sizeof(name), "psc%d", mclk_idx);
0824         clks[MPC512x_CLK_PSC0 + mclk_idx] = mpc512x_clk_gated(
0825                 name, "ips", &clkregs->sccr1, 27 - mclk_idx);
0826         mpc512x_clk_setup_mclk(&mclk_psc_data[mclk_idx], mclk_idx);
0827     }
0828     clks[MPC512x_CLK_PSC_FIFO] = mpc512x_clk_gated("psc-fifo", "ips",
0829                                &clkregs->sccr1, 15);
0830     if (soc_has_sata()) {
0831         clks[MPC512x_CLK_SATA] = mpc512x_clk_gated(
0832                 "sata", "ips", &clkregs->sccr1, 14);
0833     }
0834     clks[MPC512x_CLK_FEC] = mpc512x_clk_gated("fec", "ips",
0835                           &clkregs->sccr1, 13);
0836     if (soc_has_pci()) {
0837         clks[MPC512x_CLK_PCI] = mpc512x_clk_gated(
0838                 "pci", "pci-ug", &clkregs->sccr1, 11);
0839     }
0840     clks[MPC512x_CLK_DDR] = mpc512x_clk_gated("ddr", "ddr-ug",
0841                           &clkregs->sccr1, 10);
0842     if (soc_has_fec2()) {
0843         clks[MPC512x_CLK_FEC2] = mpc512x_clk_gated(
0844                 "fec2", "ips", &clkregs->sccr1, 9);
0845     }
0846 
0847     clks[MPC512x_CLK_DIU] = mpc512x_clk_gated("diu", "diu-ug",
0848                           &clkregs->sccr2, 31);
0849     if (soc_has_axe()) {
0850         clks[MPC512x_CLK_AXE] = mpc512x_clk_gated(
0851                 "axe", "csb", &clkregs->sccr2, 30);
0852     }
0853     clks[MPC512x_CLK_MEM] = mpc512x_clk_gated("mem", "ips",
0854                           &clkregs->sccr2, 29);
0855     clks[MPC512x_CLK_USB1] = mpc512x_clk_gated("usb1", "csb",
0856                            &clkregs->sccr2, 28);
0857     clks[MPC512x_CLK_USB2] = mpc512x_clk_gated("usb2", "csb",
0858                            &clkregs->sccr2, 27);
0859     clks[MPC512x_CLK_I2C] = mpc512x_clk_gated("i2c", "ips",
0860                           &clkregs->sccr2, 26);
0861     /* MSCAN differs from PSC with just one gate for multiple components */
0862     clks[MPC512x_CLK_BDLC] = mpc512x_clk_gated("bdlc", "ips",
0863                            &clkregs->sccr2, 25);
0864     for (mclk_idx = 0; mclk_idx < ARRAY_SIZE(mclk_mscan_data); mclk_idx++)
0865         mpc512x_clk_setup_mclk(&mclk_mscan_data[mclk_idx], mclk_idx);
0866     clks[MPC512x_CLK_SDHC] = mpc512x_clk_gated("sdhc", "sdhc-ug",
0867                            &clkregs->sccr2, 24);
0868     /* there is only one SPDIF component, which shares MCLK support code */
0869     if (soc_has_spdif()) {
0870         clks[MPC512x_CLK_SPDIF] = mpc512x_clk_gated(
0871                 "spdif", "ips", &clkregs->sccr2, 23);
0872         mpc512x_clk_setup_mclk(&mclk_spdif_data[0], 0);
0873     }
0874     if (soc_has_mbx()) {
0875         clks[MPC512x_CLK_MBX_BUS] = mpc512x_clk_gated(
0876                 "mbx-bus", "mbx-bus-ug", &clkregs->sccr2, 22);
0877         clks[MPC512x_CLK_MBX] = mpc512x_clk_gated(
0878                 "mbx", "mbx-ug", &clkregs->sccr2, 21);
0879         clks[MPC512x_CLK_MBX_3D] = mpc512x_clk_gated(
0880                 "mbx-3d", "mbx-3d-ug", &clkregs->sccr2, 20);
0881     }
0882     clks[MPC512x_CLK_IIM] = mpc512x_clk_gated("iim", "csb",
0883                           &clkregs->sccr2, 19);
0884     if (soc_has_viu()) {
0885         clks[MPC512x_CLK_VIU] = mpc512x_clk_gated(
0886                 "viu", "csb", &clkregs->sccr2, 18);
0887     }
0888     if (soc_has_sdhc2()) {
0889         clks[MPC512x_CLK_SDHC2] = mpc512x_clk_gated(
0890                 "sdhc-2", "sdhc2-ug", &clkregs->sccr2, 17);
0891     }
0892 
0893     if (soc_has_outclk()) {
0894         size_t idx; /* used as mclk_idx, just to trim line length */
0895         for (idx = 0; idx < ARRAY_SIZE(mclk_outclk_data); idx++)
0896             mpc512x_clk_setup_mclk(&mclk_outclk_data[idx], idx);
0897     }
0898 
0899     /*
0900      * externally provided clocks (when implemented in hardware,
0901      * device tree may specify values which otherwise were unknown)
0902      */
0903     freq = get_freq_from_dt("psc_mclk_in");
0904     if (!freq)
0905         freq = 25000000;
0906     clks[MPC512x_CLK_PSC_MCLK_IN] = mpc512x_clk_fixed("psc_mclk_in", freq);
0907     if (soc_has_mclk_mux0_canin()) {
0908         freq = get_freq_from_dt("can_clk_in");
0909         clks[MPC512x_CLK_CAN_CLK_IN] = mpc512x_clk_fixed(
0910                 "can_clk_in", freq);
0911     } else {
0912         freq = get_freq_from_dt("spdif_tx_in");
0913         clks[MPC512x_CLK_SPDIF_TX_IN] = mpc512x_clk_fixed(
0914                 "spdif_tx_in", freq);
0915         freq = get_freq_from_dt("spdif_rx_in");
0916         clks[MPC512x_CLK_SPDIF_TX_IN] = mpc512x_clk_fixed(
0917                 "spdif_rx_in", freq);
0918     }
0919 
0920     /* fixed frequency for AC97, always 24.567MHz */
0921     clks[MPC512x_CLK_AC97] = mpc512x_clk_fixed("ac97", 24567000);
0922 
0923     /*
0924      * pre-enable those "internal" clock items which never get
0925      * claimed by any peripheral driver, to not have the clock
0926      * subsystem disable them late at startup
0927      */
0928     clk_prepare_enable(clks[MPC512x_CLK_DUMMY]);
0929     clk_prepare_enable(clks[MPC512x_CLK_E300]); /* PowerPC CPU */
0930     clk_prepare_enable(clks[MPC512x_CLK_DDR]);  /* DRAM */
0931     clk_prepare_enable(clks[MPC512x_CLK_MEM]);  /* SRAM */
0932     clk_prepare_enable(clks[MPC512x_CLK_IPS]);  /* SoC periph */
0933     clk_prepare_enable(clks[MPC512x_CLK_LPC]);  /* boot media */
0934 }
0935 
0936 /*
0937  * registers the set of public clocks (those listed in the dt-bindings/
0938  * header file) for OF lookups, keeps the intermediates private to us
0939  */
0940 static void __init mpc5121_clk_register_of_provider(struct device_node *np)
0941 {
0942     clk_data.clks = clks;
0943     clk_data.clk_num = MPC512x_CLK_LAST_PUBLIC + 1; /* _not_ ARRAY_SIZE() */
0944     of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
0945 }
0946 
0947 /*
0948  * temporary support for the period of time between introduction of CCF
0949  * support and the adjustment of peripheral drivers to OF based lookups
0950  */
0951 static void __init mpc5121_clk_provide_migration_support(void)
0952 {
0953 
0954     /*
0955      * pre-enable those clock items which are not yet appropriately
0956      * acquired by their peripheral driver
0957      *
0958      * the PCI clock cannot get acquired by its peripheral driver,
0959      * because for this platform the driver won't probe(), instead
0960      * initialization is done from within the .setup_arch() routine
0961      * at a point in time where the clock provider has not been
0962      * setup yet and thus isn't available yet
0963      *
0964      * so we "pre-enable" the clock here, to not have the clock
0965      * subsystem automatically disable this item in a late init call
0966      *
0967      * this PCI clock pre-enable workaround only applies when there
0968      * are device tree nodes for PCI and thus the peripheral driver
0969      * has attached to bridges, otherwise the PCI clock remains
0970      * unused and so it gets disabled
0971      */
0972     clk_prepare_enable(clks[MPC512x_CLK_PSC3_MCLK]);/* serial console */
0973     if (of_find_compatible_node(NULL, "pci", "fsl,mpc5121-pci"))
0974         clk_prepare_enable(clks[MPC512x_CLK_PCI]);
0975 }
0976 
0977 /*
0978  * those macros are not exactly pretty, but they encapsulate a lot
0979  * of copy'n'paste heavy code which is even more ugly, and reduce
0980  * the potential for inconsistencies in those many code copies
0981  */
0982 #define FOR_NODES(compatname) \
0983     for_each_compatible_node(np, NULL, compatname)
0984 
0985 #define NODE_PREP do { \
0986     of_address_to_resource(np, 0, &res); \
0987     snprintf(devname, sizeof(devname), "%08x.%s", res.start, np->name); \
0988 } while (0)
0989 
0990 #define NODE_CHK(clkname, clkitem, regnode, regflag) do { \
0991     struct clk *clk; \
0992     clk = of_clk_get_by_name(np, clkname); \
0993     if (IS_ERR(clk)) { \
0994         clk = clkitem; \
0995         clk_register_clkdev(clk, clkname, devname); \
0996         if (regnode) \
0997             clk_register_clkdev(clk, clkname, np->name); \
0998         did_register |= DID_REG_ ## regflag; \
0999         pr_debug("clock alias name '%s' for dev '%s' pointer %p\n", \
1000              clkname, devname, clk); \
1001     } else { \
1002         clk_put(clk); \
1003     } \
1004 } while (0)
1005 
1006 /*
1007  * register source code provided fallback results for clock lookups,
1008  * these get consulted when OF based clock lookup fails (that is in the
1009  * case of not yet adjusted device tree data, where clock related specs
1010  * are missing)
1011  */
1012 static void __init mpc5121_clk_provide_backwards_compat(void)
1013 {
1014     enum did_reg_flags {
1015         DID_REG_PSC = BIT(0),
1016         DID_REG_PSCFIFO = BIT(1),
1017         DID_REG_NFC = BIT(2),
1018         DID_REG_CAN = BIT(3),
1019         DID_REG_I2C = BIT(4),
1020         DID_REG_DIU = BIT(5),
1021         DID_REG_VIU = BIT(6),
1022         DID_REG_FEC = BIT(7),
1023         DID_REG_USB = BIT(8),
1024         DID_REG_PATA    = BIT(9),
1025     };
1026 
1027     int did_register;
1028     struct device_node *np;
1029     struct resource res;
1030     int idx;
1031     char devname[32];
1032 
1033     did_register = 0;
1034 
1035     FOR_NODES(mpc512x_select_psc_compat()) {
1036         NODE_PREP;
1037         idx = (res.start >> 8) & 0xf;
1038         NODE_CHK("ipg", clks[MPC512x_CLK_PSC0 + idx], 0, PSC);
1039         NODE_CHK("mclk", clks[MPC512x_CLK_PSC0_MCLK + idx], 0, PSC);
1040     }
1041 
1042     FOR_NODES("fsl,mpc5121-psc-fifo") {
1043         NODE_PREP;
1044         NODE_CHK("ipg", clks[MPC512x_CLK_PSC_FIFO], 1, PSCFIFO);
1045     }
1046 
1047     FOR_NODES("fsl,mpc5121-nfc") {
1048         NODE_PREP;
1049         NODE_CHK("ipg", clks[MPC512x_CLK_NFC], 0, NFC);
1050     }
1051 
1052     FOR_NODES("fsl,mpc5121-mscan") {
1053         NODE_PREP;
1054         idx = 0;
1055         idx += (res.start & 0x2000) ? 2 : 0;
1056         idx += (res.start & 0x0080) ? 1 : 0;
1057         NODE_CHK("ipg", clks[MPC512x_CLK_BDLC], 0, CAN);
1058         NODE_CHK("mclk", clks[MPC512x_CLK_MSCAN0_MCLK + idx], 0, CAN);
1059     }
1060 
1061     /*
1062      * do register the 'ips', 'sys', and 'ref' names globally
1063      * instead of inside each individual CAN node, as there is no
1064      * potential for a name conflict (in contrast to 'ipg' and 'mclk')
1065      */
1066     if (did_register & DID_REG_CAN) {
1067         clk_register_clkdev(clks[MPC512x_CLK_IPS], "ips", NULL);
1068         clk_register_clkdev(clks[MPC512x_CLK_SYS], "sys", NULL);
1069         clk_register_clkdev(clks[MPC512x_CLK_REF], "ref", NULL);
1070     }
1071 
1072     FOR_NODES("fsl,mpc5121-i2c") {
1073         NODE_PREP;
1074         NODE_CHK("ipg", clks[MPC512x_CLK_I2C], 0, I2C);
1075     }
1076 
1077     /*
1078      * workaround for the fact that the I2C driver does an "anonymous"
1079      * lookup (NULL name spec, which yields the first clock spec) for
1080      * which we cannot register an alias -- a _global_ 'ipg' alias that
1081      * is not bound to any device name and returns the I2C clock item
1082      * is not a good idea
1083      *
1084      * so we have the lookup in the peripheral driver fail, which is
1085      * silent and non-fatal, and pre-enable the clock item here such
1086      * that register access is possible
1087      *
1088      * see commit b3bfce2b "i2c: mpc: cleanup clock API use" for
1089      * details, adjusting s/NULL/"ipg"/ in i2c-mpc.c would make this
1090      * workaround obsolete
1091      */
1092     if (did_register & DID_REG_I2C)
1093         clk_prepare_enable(clks[MPC512x_CLK_I2C]);
1094 
1095     FOR_NODES("fsl,mpc5121-diu") {
1096         NODE_PREP;
1097         NODE_CHK("ipg", clks[MPC512x_CLK_DIU], 1, DIU);
1098     }
1099 
1100     FOR_NODES("fsl,mpc5121-viu") {
1101         NODE_PREP;
1102         NODE_CHK("ipg", clks[MPC512x_CLK_VIU], 0, VIU);
1103     }
1104 
1105     /*
1106      * note that 2771399a "fs_enet: cleanup clock API use" did use the
1107      * "per" string for the clock lookup in contrast to the "ipg" name
1108      * which most other nodes are using -- this is not a fatal thing
1109      * but just something to keep in mind when doing compatibility
1110      * registration, it's a non-issue with up-to-date device tree data
1111      */
1112     FOR_NODES("fsl,mpc5121-fec") {
1113         NODE_PREP;
1114         NODE_CHK("per", clks[MPC512x_CLK_FEC], 0, FEC);
1115     }
1116     FOR_NODES("fsl,mpc5121-fec-mdio") {
1117         NODE_PREP;
1118         NODE_CHK("per", clks[MPC512x_CLK_FEC], 0, FEC);
1119     }
1120     /*
1121      * MPC5125 has two FECs: FEC1 at 0x2800, FEC2 at 0x4800;
1122      * the clock items don't "form an array" since FEC2 was
1123      * added only later and was not allowed to shift all other
1124      * clock item indices, so the numbers aren't adjacent
1125      */
1126     FOR_NODES("fsl,mpc5125-fec") {
1127         NODE_PREP;
1128         if (res.start & 0x4000)
1129             idx = MPC512x_CLK_FEC2;
1130         else
1131             idx = MPC512x_CLK_FEC;
1132         NODE_CHK("per", clks[idx], 0, FEC);
1133     }
1134 
1135     FOR_NODES("fsl,mpc5121-usb2-dr") {
1136         NODE_PREP;
1137         idx = (res.start & 0x4000) ? 1 : 0;
1138         NODE_CHK("ipg", clks[MPC512x_CLK_USB1 + idx], 0, USB);
1139     }
1140 
1141     FOR_NODES("fsl,mpc5121-pata") {
1142         NODE_PREP;
1143         NODE_CHK("ipg", clks[MPC512x_CLK_PATA], 0, PATA);
1144     }
1145 
1146     /*
1147      * try to collapse diagnostics into a single line of output yet
1148      * provide a full list of what is missing, to avoid noise in the
1149      * absence of up-to-date device tree data -- backwards
1150      * compatibility to old DTBs is a requirement, updates may be
1151      * desirable or preferrable but are not at all mandatory
1152      */
1153     if (did_register) {
1154         pr_notice("device tree lacks clock specs, adding fallbacks (0x%x,%s%s%s%s%s%s%s%s%s%s)\n",
1155               did_register,
1156               (did_register & DID_REG_PSC) ? " PSC" : "",
1157               (did_register & DID_REG_PSCFIFO) ? " PSCFIFO" : "",
1158               (did_register & DID_REG_NFC) ? " NFC" : "",
1159               (did_register & DID_REG_CAN) ? " CAN" : "",
1160               (did_register & DID_REG_I2C) ? " I2C" : "",
1161               (did_register & DID_REG_DIU) ? " DIU" : "",
1162               (did_register & DID_REG_VIU) ? " VIU" : "",
1163               (did_register & DID_REG_FEC) ? " FEC" : "",
1164               (did_register & DID_REG_USB) ? " USB" : "",
1165               (did_register & DID_REG_PATA) ? " PATA" : "");
1166     } else {
1167         pr_debug("device tree has clock specs, no fallbacks added\n");
1168     }
1169 }
1170 
1171 /*
1172  * The "fixed-clock" nodes (which includes the oscillator node if the board's
1173  * DT provides one) has already been scanned by the of_clk_init() in
1174  * time_init().
1175  */
1176 int __init mpc5121_clk_init(void)
1177 {
1178     struct device_node *clk_np;
1179     int busfreq;
1180 
1181     /* map the clock control registers */
1182     clk_np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-clock");
1183     if (!clk_np)
1184         return -ENODEV;
1185     clkregs = of_iomap(clk_np, 0);
1186     WARN_ON(!clkregs);
1187 
1188     /* determine the SoC variant we run on */
1189     mpc512x_clk_determine_soc();
1190 
1191     /* invalidate all not yet registered clock slots */
1192     mpc512x_clk_preset_data();
1193 
1194     /*
1195      * add a dummy clock for those situations where a clock spec is
1196      * required yet no real clock is involved
1197      */
1198     clks[MPC512x_CLK_DUMMY] = mpc512x_clk_fixed("dummy", 0);
1199 
1200     /*
1201      * have all the real nodes in the clock tree populated from REF
1202      * down to all leaves, either starting from the OSC node or from
1203      * a REF root that was created from the IPS bus clock input
1204      */
1205     busfreq = get_freq_from_dt("bus-frequency");
1206     mpc512x_clk_setup_clock_tree(clk_np, busfreq);
1207 
1208     /* register as an OF clock provider */
1209     mpc5121_clk_register_of_provider(clk_np);
1210 
1211     /*
1212      * unbreak not yet adjusted peripheral drivers during migration
1213      * towards fully operational common clock support, and allow
1214      * operation in the absence of clock related device tree specs
1215      */
1216     mpc5121_clk_provide_migration_support();
1217     mpc5121_clk_provide_backwards_compat();
1218 
1219     return 0;
1220 }