Path: blob/master/arch/powerpc/platforms/512x/clock-commonclk.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Copyright (C) 2013 DENX Software Engineering3*4* Gerhard Sittig, <[email protected]>5*6* common clock driver support for the MPC512x platform7*/89#include <linux/bitops.h>10#include <linux/clk.h>11#include <linux/clk-provider.h>12#include <linux/clkdev.h>13#include <linux/device.h>14#include <linux/errno.h>15#include <linux/io.h>16#include <linux/of.h>17#include <linux/of_address.h>1819#include <asm/mpc5121.h>20#include <dt-bindings/clock/mpc512x-clock.h>2122#include "mpc512x.h" /* our public mpc5121_clk_init() API */2324/* helpers to keep the MCLK intermediates "somewhere" in our table */25enum {26MCLK_IDX_MUX0,27MCLK_IDX_EN0,28MCLK_IDX_DIV0,29MCLK_MAX_IDX,30};3132#define NR_PSCS 1233#define NR_MSCANS 434#define NR_SPDIFS 135#define NR_OUTCLK 436#define NR_MCLKS (NR_PSCS + NR_MSCANS + NR_SPDIFS + NR_OUTCLK)3738/* extend the public set of clocks by adding internal slots for management */39enum {40/* arrange for adjacent numbers after the public set */41MPC512x_CLK_START_PRIVATE = MPC512x_CLK_LAST_PUBLIC,42/* clocks which aren't announced to the public */43MPC512x_CLK_DDR,44MPC512x_CLK_MEM,45MPC512x_CLK_IIM,46/* intermediates in div+gate combos or fractional dividers */47MPC512x_CLK_DDR_UG,48MPC512x_CLK_SDHC_x4,49MPC512x_CLK_SDHC_UG,50MPC512x_CLK_SDHC2_UG,51MPC512x_CLK_DIU_x4,52MPC512x_CLK_DIU_UG,53MPC512x_CLK_MBX_BUS_UG,54MPC512x_CLK_MBX_UG,55MPC512x_CLK_MBX_3D_UG,56MPC512x_CLK_PCI_UG,57MPC512x_CLK_NFC_UG,58MPC512x_CLK_LPC_UG,59MPC512x_CLK_SPDIF_TX_IN,60/* intermediates for the mux+gate+div+mux MCLK generation */61MPC512x_CLK_MCLKS_FIRST,62MPC512x_CLK_MCLKS_LAST = MPC512x_CLK_MCLKS_FIRST63+ NR_MCLKS * MCLK_MAX_IDX,64/* internal, symbolic spec for the number of slots */65MPC512x_CLK_LAST_PRIVATE,66};6768/* data required for the OF clock provider registration */69static struct clk *clks[MPC512x_CLK_LAST_PRIVATE];70static struct clk_onecell_data clk_data;7172/* CCM register access */73static struct mpc512x_ccm __iomem *clkregs;74static DEFINE_SPINLOCK(clklock);7576/* SoC variants {{{ */7778/*79* tell SoC variants apart as they are rather similar yet not identical,80* cache the result in an enum to not repeatedly run the expensive OF test81*82* MPC5123 is an MPC5121 without the MBX graphics accelerator83*84* MPC5125 has many more differences: no MBX, no AXE, no VIU, no SPDIF,85* no PATA, no SATA, no PCI, two FECs (of different compatibility name),86* only 10 PSCs (of different compatibility name), two SDHCs, different87* NFC IP block, output clocks, system PLL status query, different CPMF88* interpretation, no CFM, different fourth PSC/CAN mux0 input -- yet89* those differences can get folded into this clock provider support90* code and don't warrant a separate highly redundant implementation91*/9293static enum soc_type {94MPC512x_SOC_MPC5121,95MPC512x_SOC_MPC5123,96MPC512x_SOC_MPC5125,97} soc;9899static void __init mpc512x_clk_determine_soc(void)100{101if (of_machine_is_compatible("fsl,mpc5121")) {102soc = MPC512x_SOC_MPC5121;103return;104}105if (of_machine_is_compatible("fsl,mpc5123")) {106soc = MPC512x_SOC_MPC5123;107return;108}109if (of_machine_is_compatible("fsl,mpc5125")) {110soc = MPC512x_SOC_MPC5125;111return;112}113}114115static bool __init soc_has_mbx(void)116{117if (soc == MPC512x_SOC_MPC5121)118return true;119return false;120}121122static bool __init soc_has_axe(void)123{124if (soc == MPC512x_SOC_MPC5125)125return false;126return true;127}128129static bool __init soc_has_viu(void)130{131if (soc == MPC512x_SOC_MPC5125)132return false;133return true;134}135136static bool __init soc_has_spdif(void)137{138if (soc == MPC512x_SOC_MPC5125)139return false;140return true;141}142143static bool __init soc_has_pata(void)144{145if (soc == MPC512x_SOC_MPC5125)146return false;147return true;148}149150static bool __init soc_has_sata(void)151{152if (soc == MPC512x_SOC_MPC5125)153return false;154return true;155}156157static bool __init soc_has_pci(void)158{159if (soc == MPC512x_SOC_MPC5125)160return false;161return true;162}163164static bool __init soc_has_fec2(void)165{166if (soc == MPC512x_SOC_MPC5125)167return true;168return false;169}170171static int __init soc_max_pscnum(void)172{173if (soc == MPC512x_SOC_MPC5125)174return 10;175return 12;176}177178static bool __init soc_has_sdhc2(void)179{180if (soc == MPC512x_SOC_MPC5125)181return true;182return false;183}184185static bool __init soc_has_nfc_5125(void)186{187if (soc == MPC512x_SOC_MPC5125)188return true;189return false;190}191192static bool __init soc_has_outclk(void)193{194if (soc == MPC512x_SOC_MPC5125)195return true;196return false;197}198199static bool __init soc_has_cpmf_0_bypass(void)200{201if (soc == MPC512x_SOC_MPC5125)202return true;203return false;204}205206static bool __init soc_has_mclk_mux0_canin(void)207{208if (soc == MPC512x_SOC_MPC5125)209return true;210return false;211}212213/* }}} SoC variants */214/* common clk API wrappers {{{ */215216/* convenience wrappers around the common clk API */217static inline struct clk *mpc512x_clk_fixed(const char *name, int rate)218{219return clk_register_fixed_rate(NULL, name, NULL, 0, rate);220}221222static inline struct clk *mpc512x_clk_factor(223const char *name, const char *parent_name,224int mul, int div)225{226int clkflags;227228clkflags = CLK_SET_RATE_PARENT;229return clk_register_fixed_factor(NULL, name, parent_name, clkflags,230mul, div);231}232233static inline struct clk *mpc512x_clk_divider(234const char *name, const char *parent_name, u8 clkflags,235u32 __iomem *reg, u8 pos, u8 len, int divflags)236{237divflags |= CLK_DIVIDER_BIG_ENDIAN;238return clk_register_divider(NULL, name, parent_name, clkflags,239reg, pos, len, divflags, &clklock);240}241242static inline struct clk *mpc512x_clk_divtable(243const char *name, const char *parent_name,244u32 __iomem *reg, u8 pos, u8 len,245const struct clk_div_table *divtab)246{247u8 divflags;248249divflags = CLK_DIVIDER_BIG_ENDIAN;250return clk_register_divider_table(NULL, name, parent_name, 0,251reg, pos, len, divflags,252divtab, &clklock);253}254255static inline struct clk *mpc512x_clk_gated(256const char *name, const char *parent_name,257u32 __iomem *reg, u8 pos)258{259int clkflags;260u8 gateflags;261262clkflags = CLK_SET_RATE_PARENT;263gateflags = CLK_GATE_BIG_ENDIAN;264return clk_register_gate(NULL, name, parent_name, clkflags,265reg, pos, gateflags, &clklock);266}267268static inline struct clk *mpc512x_clk_muxed(const char *name,269const char **parent_names, int parent_count,270u32 __iomem *reg, u8 pos, u8 len)271{272int clkflags;273u8 muxflags;274275clkflags = CLK_SET_RATE_PARENT;276muxflags = CLK_MUX_BIG_ENDIAN;277return clk_register_mux(NULL, name,278parent_names, parent_count, clkflags,279reg, pos, len, muxflags, &clklock);280}281282/* }}} common clk API wrappers */283284/* helper to isolate a bit field from a register */285static inline int get_bit_field(uint32_t __iomem *reg, uint8_t pos, uint8_t len)286{287uint32_t val;288289val = in_be32(reg);290val >>= pos;291val &= (1 << len) - 1;292return val;293}294295/* get the SPMF and translate it into the "sys pll" multiplier */296static int __init get_spmf_mult(void)297{298static int spmf_to_mult[] = {29968, 1, 12, 16, 20, 24, 28, 32,30036, 40, 44, 48, 52, 56, 60, 64,301};302int spmf;303304spmf = get_bit_field(&clkregs->spmr, 24, 4);305return spmf_to_mult[spmf];306}307308/*309* get the SYS_DIV value and translate it into a divide factor310*311* values returned from here are a multiple of the real factor since the312* divide ratio is fractional313*/314static int __init get_sys_div_x2(void)315{316static int sysdiv_code_to_x2[] = {3174, 5, 6, 7, 8, 9, 10, 14,31812, 16, 18, 22, 20, 24, 26, 30,31928, 32, 34, 38, 36, 40, 42, 46,32044, 48, 50, 54, 52, 56, 58, 62,32160, 64, 66,322};323int divcode;324325divcode = get_bit_field(&clkregs->scfr2, 26, 6);326return sysdiv_code_to_x2[divcode];327}328329/*330* get the CPMF value and translate it into a multiplier factor331*332* values returned from here are a multiple of the real factor since the333* multiplier ratio is fractional334*/335static int __init get_cpmf_mult_x2(void)336{337static int cpmf_to_mult_x36[] = {338/* 0b000 is "times 36" */33972, 2, 2, 3, 4, 5, 6, 7,340};341static int cpmf_to_mult_0by[] = {342/* 0b000 is "bypass" */3432, 2, 2, 3, 4, 5, 6, 7,344};345346int *cpmf_to_mult;347int cpmf;348349cpmf = get_bit_field(&clkregs->spmr, 16, 4);350if (soc_has_cpmf_0_bypass())351cpmf_to_mult = cpmf_to_mult_0by;352else353cpmf_to_mult = cpmf_to_mult_x36;354return cpmf_to_mult[cpmf];355}356357/*358* some of the clock dividers do scale in a linear way, yet not all of359* their bit combinations are legal; use a divider table to get a360* resulting set of applicable divider values361*/362363/* applies to the IPS_DIV, and PCI_DIV values */364static const struct clk_div_table divtab_2346[] = {365{ .val = 2, .div = 2, },366{ .val = 3, .div = 3, },367{ .val = 4, .div = 4, },368{ .val = 6, .div = 6, },369{ .div = 0, },370};371372/* applies to the MBX_DIV, LPC_DIV, and NFC_DIV values */373static const struct clk_div_table divtab_1234[] = {374{ .val = 1, .div = 1, },375{ .val = 2, .div = 2, },376{ .val = 3, .div = 3, },377{ .val = 4, .div = 4, },378{ .div = 0, },379};380381static int __init get_freq_from_dt(char *propname)382{383struct device_node *np;384const unsigned int *prop;385int val;386387val = 0;388np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-immr");389if (np) {390prop = of_get_property(np, propname, NULL);391if (prop)392val = *prop;393of_node_put(np);394}395return val;396}397398static void __init mpc512x_clk_preset_data(void)399{400size_t i;401402for (i = 0; i < ARRAY_SIZE(clks); i++)403clks[i] = ERR_PTR(-ENODEV);404}405406/*407* - receives the "bus frequency" from the caller (that's the IPS clock408* rate, the historical source of clock information)409* - fetches the system PLL multiplier and divider values as well as the410* IPS divider value from hardware411* - determines the REF clock rate either from the XTAL/OSC spec (if412* there is a device tree node describing the oscillator) or from the413* IPS bus clock (supported for backwards compatibility, such that414* setups without XTAL/OSC specs keep working)415* - creates the "ref" clock item in the clock tree, such that416* subsequent code can create the remainder of the hierarchy (REF ->417* SYS -> CSB -> IPS) from the REF clock rate and the returned mul/div418* values419*/420static void __init mpc512x_clk_setup_ref_clock(struct device_node *np, int bus_freq,421int *sys_mul, int *sys_div,422int *ips_div)423{424struct clk *osc_clk;425int calc_freq;426427/* fetch mul/div factors from the hardware */428*sys_mul = get_spmf_mult();429*sys_mul *= 2; /* compensate for the fractional divider */430*sys_div = get_sys_div_x2();431*ips_div = get_bit_field(&clkregs->scfr1, 23, 3);432433/* lookup the oscillator clock for its rate */434osc_clk = of_clk_get_by_name(np, "osc");435436/*437* either descend from OSC to REF (and in bypassing verify the438* IPS rate), or backtrack from IPS and multiplier values that439* were fetched from hardware to REF and thus to the OSC value440*441* in either case the REF clock gets created here and the442* remainder of the clock tree can get spanned from there443*/444if (!IS_ERR(osc_clk)) {445clks[MPC512x_CLK_REF] = mpc512x_clk_factor("ref", "osc", 1, 1);446calc_freq = clk_get_rate(clks[MPC512x_CLK_REF]);447calc_freq *= *sys_mul;448calc_freq /= *sys_div;449calc_freq /= 2;450calc_freq /= *ips_div;451if (bus_freq && calc_freq != bus_freq)452pr_warn("calc rate %d != OF spec %d\n",453calc_freq, bus_freq);454} else {455calc_freq = bus_freq; /* start with IPS */456calc_freq *= *ips_div; /* IPS -> CSB */457calc_freq *= 2; /* CSB -> SYS */458calc_freq *= *sys_div; /* SYS -> PLL out */459calc_freq /= *sys_mul; /* PLL out -> REF == OSC */460clks[MPC512x_CLK_REF] = mpc512x_clk_fixed("ref", calc_freq);461}462}463464/* MCLK helpers {{{ */465466/*467* helper code for the MCLK subtree setup468*469* the overview in section 5.2.4 of the MPC5121e Reference Manual rev4470* suggests that all instances of the "PSC clock generation" are equal,471* and that one might re-use the PSC setup for MSCAN clock generation472* (section 5.2.5) as well, at least the logic if not the data for473* description474*475* the details (starting at page 5-20) show differences in the specific476* inputs of the first mux stage ("can clk in", "spdif tx"), and the477* factual non-availability of the second mux stage (it's present yet478* only one input is valid)479*480* the MSCAN clock related registers (starting at page 5-35) all481* reference "spdif clk" at the first mux stage and don't mention any482* "can clk" at all, which somehow is unexpected483*484* TODO re-check the document, and clarify whether the RM is correct in485* the overview or in the details, and whether the difference is a486* clipboard induced error or results from chip revisions487*488* it turns out that the RM rev4 as of 2012-06 talks about "can" for the489* PSCs while RM rev3 as of 2008-10 talks about "spdif", so I guess that490* first a doc update is required which better reflects reality in the491* SoC before the implementation should follow while no questions remain492*/493494/*495* note that this declaration raises a checkpatch warning, but496* it's the very data type dictated by <linux/clk-provider.h>,497* "fixing" this warning will break compilation498*/499static const char *parent_names_mux0_spdif[] = {500"sys", "ref", "psc-mclk-in", "spdif-tx",501};502503static const char *parent_names_mux0_canin[] = {504"sys", "ref", "psc-mclk-in", "can-clk-in",505};506507enum mclk_type {508MCLK_TYPE_PSC,509MCLK_TYPE_MSCAN,510MCLK_TYPE_SPDIF,511MCLK_TYPE_OUTCLK,512};513514struct mclk_setup_data {515enum mclk_type type;516bool has_mclk1;517const char *name_mux0;518const char *name_en0;519const char *name_div0;520const char *parent_names_mux1[2];521const char *name_mclk;522};523524#define MCLK_SETUP_DATA_PSC(id) { \525MCLK_TYPE_PSC, 0, \526"psc" #id "-mux0", \527"psc" #id "-en0", \528"psc" #id "_mclk_div", \529{ "psc" #id "_mclk_div", "dummy", }, \530"psc" #id "_mclk", \531}532533#define MCLK_SETUP_DATA_MSCAN(id) { \534MCLK_TYPE_MSCAN, 0, \535"mscan" #id "-mux0", \536"mscan" #id "-en0", \537"mscan" #id "_mclk_div", \538{ "mscan" #id "_mclk_div", "dummy", }, \539"mscan" #id "_mclk", \540}541542#define MCLK_SETUP_DATA_SPDIF { \543MCLK_TYPE_SPDIF, 1, \544"spdif-mux0", \545"spdif-en0", \546"spdif_mclk_div", \547{ "spdif_mclk_div", "spdif-rx", }, \548"spdif_mclk", \549}550551#define MCLK_SETUP_DATA_OUTCLK(id) { \552MCLK_TYPE_OUTCLK, 0, \553"out" #id "-mux0", \554"out" #id "-en0", \555"out" #id "_mclk_div", \556{ "out" #id "_mclk_div", "dummy", }, \557"out" #id "_clk", \558}559560static struct mclk_setup_data mclk_psc_data[] = {561MCLK_SETUP_DATA_PSC(0),562MCLK_SETUP_DATA_PSC(1),563MCLK_SETUP_DATA_PSC(2),564MCLK_SETUP_DATA_PSC(3),565MCLK_SETUP_DATA_PSC(4),566MCLK_SETUP_DATA_PSC(5),567MCLK_SETUP_DATA_PSC(6),568MCLK_SETUP_DATA_PSC(7),569MCLK_SETUP_DATA_PSC(8),570MCLK_SETUP_DATA_PSC(9),571MCLK_SETUP_DATA_PSC(10),572MCLK_SETUP_DATA_PSC(11),573};574575static struct mclk_setup_data mclk_mscan_data[] = {576MCLK_SETUP_DATA_MSCAN(0),577MCLK_SETUP_DATA_MSCAN(1),578MCLK_SETUP_DATA_MSCAN(2),579MCLK_SETUP_DATA_MSCAN(3),580};581582static struct mclk_setup_data mclk_spdif_data[] = {583MCLK_SETUP_DATA_SPDIF,584};585586static struct mclk_setup_data mclk_outclk_data[] = {587MCLK_SETUP_DATA_OUTCLK(0),588MCLK_SETUP_DATA_OUTCLK(1),589MCLK_SETUP_DATA_OUTCLK(2),590MCLK_SETUP_DATA_OUTCLK(3),591};592593/* setup the MCLK clock subtree of an individual PSC/MSCAN/SPDIF */594static void __init mpc512x_clk_setup_mclk(struct mclk_setup_data *entry, size_t idx)595{596size_t clks_idx_pub, clks_idx_int;597u32 __iomem *mccr_reg; /* MCLK control register (mux, en, div) */598int div;599600/* derive a few parameters from the component type and index */601switch (entry->type) {602case MCLK_TYPE_PSC:603clks_idx_pub = MPC512x_CLK_PSC0_MCLK + idx;604clks_idx_int = MPC512x_CLK_MCLKS_FIRST605+ (idx) * MCLK_MAX_IDX;606mccr_reg = &clkregs->psc_ccr[idx];607break;608case MCLK_TYPE_MSCAN:609clks_idx_pub = MPC512x_CLK_MSCAN0_MCLK + idx;610clks_idx_int = MPC512x_CLK_MCLKS_FIRST611+ (NR_PSCS + idx) * MCLK_MAX_IDX;612mccr_reg = &clkregs->mscan_ccr[idx];613break;614case MCLK_TYPE_SPDIF:615clks_idx_pub = MPC512x_CLK_SPDIF_MCLK;616clks_idx_int = MPC512x_CLK_MCLKS_FIRST617+ (NR_PSCS + NR_MSCANS) * MCLK_MAX_IDX;618mccr_reg = &clkregs->spccr;619break;620case MCLK_TYPE_OUTCLK:621clks_idx_pub = MPC512x_CLK_OUT0_CLK + idx;622clks_idx_int = MPC512x_CLK_MCLKS_FIRST623+ (NR_PSCS + NR_MSCANS + NR_SPDIFS + idx)624* MCLK_MAX_IDX;625mccr_reg = &clkregs->out_ccr[idx];626break;627default:628return;629}630631/*632* this was grabbed from the PPC_CLOCK implementation, which633* enforced a specific MCLK divider while the clock was gated634* during setup (that's a documented hardware requirement)635*636* the PPC_CLOCK implementation might even have violated the637* "MCLK <= IPS" constraint, the fixed divider value of 1638* results in a divider of 2 and thus MCLK = SYS/2 which equals639* CSB which is greater than IPS; the serial port setup may have640* adjusted the divider which the clock setup might have left in641* an undesirable state642*643* initial setup is:644* - MCLK 0 from SYS645* - MCLK DIV such to not exceed the IPS clock646* - MCLK 0 enabled647* - MCLK 1 from MCLK DIV648*/649div = clk_get_rate(clks[MPC512x_CLK_SYS]);650div /= clk_get_rate(clks[MPC512x_CLK_IPS]);651out_be32(mccr_reg, (0 << 16));652out_be32(mccr_reg, (0 << 16) | ((div - 1) << 17));653out_be32(mccr_reg, (1 << 16) | ((div - 1) << 17));654655/*656* create the 'struct clk' items of the MCLK's clock subtree657*658* note that by design we always create all nodes and won't take659* shortcuts here, because660* - the "internal" MCLK_DIV and MCLK_OUT signal in turn are661* selectable inputs to the CFM while those who "actually use"662* the PSC/MSCAN/SPDIF (serial drivers et al) need the MCLK663* for their bitrate664* - in the absence of "aliases" for clocks we need to create665* individual 'struct clk' items for whatever might get666* referenced or looked up, even if several of those items are667* identical from the logical POV (their rate value)668* - for easier future maintenance and for better reflection of669* the SoC's documentation, it appears appropriate to generate670* clock items even for those muxers which actually are NOPs671* (those with two inputs of which one is reserved)672*/673clks[clks_idx_int + MCLK_IDX_MUX0] = mpc512x_clk_muxed(674entry->name_mux0,675soc_has_mclk_mux0_canin()676? &parent_names_mux0_canin[0]677: &parent_names_mux0_spdif[0],678ARRAY_SIZE(parent_names_mux0_spdif),679mccr_reg, 14, 2);680clks[clks_idx_int + MCLK_IDX_EN0] = mpc512x_clk_gated(681entry->name_en0, entry->name_mux0,682mccr_reg, 16);683clks[clks_idx_int + MCLK_IDX_DIV0] = mpc512x_clk_divider(684entry->name_div0,685entry->name_en0, CLK_SET_RATE_GATE,686mccr_reg, 17, 15, 0);687if (entry->has_mclk1) {688clks[clks_idx_pub] = mpc512x_clk_muxed(689entry->name_mclk,690&entry->parent_names_mux1[0],691ARRAY_SIZE(entry->parent_names_mux1),692mccr_reg, 7, 1);693} else {694clks[clks_idx_pub] = mpc512x_clk_factor(695entry->name_mclk,696entry->parent_names_mux1[0],6971, 1);698}699}700701/* }}} MCLK helpers */702703static void __init mpc512x_clk_setup_clock_tree(struct device_node *np, int busfreq)704{705int sys_mul, sys_div, ips_div;706int mul, div;707size_t mclk_idx;708int freq;709710/*711* developer's notes:712* - consider whether to handle clocks which have both gates and713* dividers via intermediates or by means of composites714* - fractional dividers appear to not map well to composites715* since they can be seen as a fixed multiplier and an716* adjustable divider, while composites can only combine at717* most one of a mux, div, and gate each into one 'struct clk'718* item719* - PSC/MSCAN/SPDIF clock generation OTOH already is very720* specific and cannot get mapped to composites (at least not721* a single one, maybe two of them, but then some of these722* intermediate clock signals get referenced elsewhere (e.g.723* in the clock frequency measurement, CFM) and thus need724* publicly available names725* - the current source layout appropriately reflects the726* hardware setup, and it works, so it's questionable whether727* further changes will result in big enough a benefit728*/729730/* regardless of whether XTAL/OSC exists, have REF created */731mpc512x_clk_setup_ref_clock(np, busfreq, &sys_mul, &sys_div, &ips_div);732733/* now setup the REF -> SYS -> CSB -> IPS hierarchy */734clks[MPC512x_CLK_SYS] = mpc512x_clk_factor("sys", "ref",735sys_mul, sys_div);736clks[MPC512x_CLK_CSB] = mpc512x_clk_factor("csb", "sys", 1, 2);737clks[MPC512x_CLK_IPS] = mpc512x_clk_divtable("ips", "csb",738&clkregs->scfr1, 23, 3,739divtab_2346);740/* now setup anything below SYS and CSB and IPS */741742clks[MPC512x_CLK_DDR_UG] = mpc512x_clk_factor("ddr-ug", "sys", 1, 2);743744/*745* the Reference Manual discusses that for SDHC only even divide746* ratios are supported because clock domain synchronization747* between 'per' and 'ipg' is broken;748* keep the divider's bit 0 cleared (per reset value), and only749* allow to setup the divider's bits 7:1, which results in that750* only even divide ratios can get configured upon rate changes;751* keep the "x4" name because this bit shift hack is an internal752* implementation detail, the "fractional divider with quarters"753* semantics remains754*/755clks[MPC512x_CLK_SDHC_x4] = mpc512x_clk_factor("sdhc-x4", "csb", 2, 1);756clks[MPC512x_CLK_SDHC_UG] = mpc512x_clk_divider("sdhc-ug", "sdhc-x4", 0,757&clkregs->scfr2, 1, 7,758CLK_DIVIDER_ONE_BASED);759if (soc_has_sdhc2()) {760clks[MPC512x_CLK_SDHC2_UG] = mpc512x_clk_divider(761"sdhc2-ug", "sdhc-x4", 0, &clkregs->scfr2,7629, 7, CLK_DIVIDER_ONE_BASED);763}764765clks[MPC512x_CLK_DIU_x4] = mpc512x_clk_factor("diu-x4", "csb", 4, 1);766clks[MPC512x_CLK_DIU_UG] = mpc512x_clk_divider("diu-ug", "diu-x4", 0,767&clkregs->scfr1, 0, 8,768CLK_DIVIDER_ONE_BASED);769770/*771* the "power architecture PLL" was setup from data which was772* sampled from the reset config word, at this point in time the773* configuration can be considered fixed and read only (i.e. no774* longer adjustable, or no longer in need of adjustment), which775* is why we don't register a PLL here but assume fixed factors776*/777mul = get_cpmf_mult_x2();778div = 2; /* compensate for the fractional factor */779clks[MPC512x_CLK_E300] = mpc512x_clk_factor("e300", "csb", mul, div);780781if (soc_has_mbx()) {782clks[MPC512x_CLK_MBX_BUS_UG] = mpc512x_clk_factor(783"mbx-bus-ug", "csb", 1, 2);784clks[MPC512x_CLK_MBX_UG] = mpc512x_clk_divtable(785"mbx-ug", "mbx-bus-ug", &clkregs->scfr1,78614, 3, divtab_1234);787clks[MPC512x_CLK_MBX_3D_UG] = mpc512x_clk_factor(788"mbx-3d-ug", "mbx-ug", 1, 1);789}790if (soc_has_pci()) {791clks[MPC512x_CLK_PCI_UG] = mpc512x_clk_divtable(792"pci-ug", "csb", &clkregs->scfr1,79320, 3, divtab_2346);794}795if (soc_has_nfc_5125()) {796/*797* XXX TODO implement 5125 NFC clock setup logic,798* with high/low period counters in clkregs->scfr3,799* currently there are no users so it's ENOIMPL800*/801clks[MPC512x_CLK_NFC_UG] = ERR_PTR(-ENOTSUPP);802} else {803clks[MPC512x_CLK_NFC_UG] = mpc512x_clk_divtable(804"nfc-ug", "ips", &clkregs->scfr1,8058, 3, divtab_1234);806}807clks[MPC512x_CLK_LPC_UG] = mpc512x_clk_divtable("lpc-ug", "ips",808&clkregs->scfr1, 11, 3,809divtab_1234);810811clks[MPC512x_CLK_LPC] = mpc512x_clk_gated("lpc", "lpc-ug",812&clkregs->sccr1, 30);813clks[MPC512x_CLK_NFC] = mpc512x_clk_gated("nfc", "nfc-ug",814&clkregs->sccr1, 29);815if (soc_has_pata()) {816clks[MPC512x_CLK_PATA] = mpc512x_clk_gated(817"pata", "ips", &clkregs->sccr1, 28);818}819/* for PSCs there is a "registers" gate and a bitrate MCLK subtree */820for (mclk_idx = 0; mclk_idx < soc_max_pscnum(); mclk_idx++) {821char name[12];822snprintf(name, sizeof(name), "psc%d", mclk_idx);823clks[MPC512x_CLK_PSC0 + mclk_idx] = mpc512x_clk_gated(824name, "ips", &clkregs->sccr1, 27 - mclk_idx);825mpc512x_clk_setup_mclk(&mclk_psc_data[mclk_idx], mclk_idx);826}827clks[MPC512x_CLK_PSC_FIFO] = mpc512x_clk_gated("psc-fifo", "ips",828&clkregs->sccr1, 15);829if (soc_has_sata()) {830clks[MPC512x_CLK_SATA] = mpc512x_clk_gated(831"sata", "ips", &clkregs->sccr1, 14);832}833clks[MPC512x_CLK_FEC] = mpc512x_clk_gated("fec", "ips",834&clkregs->sccr1, 13);835if (soc_has_pci()) {836clks[MPC512x_CLK_PCI] = mpc512x_clk_gated(837"pci", "pci-ug", &clkregs->sccr1, 11);838}839clks[MPC512x_CLK_DDR] = mpc512x_clk_gated("ddr", "ddr-ug",840&clkregs->sccr1, 10);841if (soc_has_fec2()) {842clks[MPC512x_CLK_FEC2] = mpc512x_clk_gated(843"fec2", "ips", &clkregs->sccr1, 9);844}845846clks[MPC512x_CLK_DIU] = mpc512x_clk_gated("diu", "diu-ug",847&clkregs->sccr2, 31);848if (soc_has_axe()) {849clks[MPC512x_CLK_AXE] = mpc512x_clk_gated(850"axe", "csb", &clkregs->sccr2, 30);851}852clks[MPC512x_CLK_MEM] = mpc512x_clk_gated("mem", "ips",853&clkregs->sccr2, 29);854clks[MPC512x_CLK_USB1] = mpc512x_clk_gated("usb1", "csb",855&clkregs->sccr2, 28);856clks[MPC512x_CLK_USB2] = mpc512x_clk_gated("usb2", "csb",857&clkregs->sccr2, 27);858clks[MPC512x_CLK_I2C] = mpc512x_clk_gated("i2c", "ips",859&clkregs->sccr2, 26);860/* MSCAN differs from PSC with just one gate for multiple components */861clks[MPC512x_CLK_BDLC] = mpc512x_clk_gated("bdlc", "ips",862&clkregs->sccr2, 25);863for (mclk_idx = 0; mclk_idx < ARRAY_SIZE(mclk_mscan_data); mclk_idx++)864mpc512x_clk_setup_mclk(&mclk_mscan_data[mclk_idx], mclk_idx);865clks[MPC512x_CLK_SDHC] = mpc512x_clk_gated("sdhc", "sdhc-ug",866&clkregs->sccr2, 24);867/* there is only one SPDIF component, which shares MCLK support code */868if (soc_has_spdif()) {869clks[MPC512x_CLK_SPDIF] = mpc512x_clk_gated(870"spdif", "ips", &clkregs->sccr2, 23);871mpc512x_clk_setup_mclk(&mclk_spdif_data[0], 0);872}873if (soc_has_mbx()) {874clks[MPC512x_CLK_MBX_BUS] = mpc512x_clk_gated(875"mbx-bus", "mbx-bus-ug", &clkregs->sccr2, 22);876clks[MPC512x_CLK_MBX] = mpc512x_clk_gated(877"mbx", "mbx-ug", &clkregs->sccr2, 21);878clks[MPC512x_CLK_MBX_3D] = mpc512x_clk_gated(879"mbx-3d", "mbx-3d-ug", &clkregs->sccr2, 20);880}881clks[MPC512x_CLK_IIM] = mpc512x_clk_gated("iim", "csb",882&clkregs->sccr2, 19);883if (soc_has_viu()) {884clks[MPC512x_CLK_VIU] = mpc512x_clk_gated(885"viu", "csb", &clkregs->sccr2, 18);886}887if (soc_has_sdhc2()) {888clks[MPC512x_CLK_SDHC2] = mpc512x_clk_gated(889"sdhc-2", "sdhc2-ug", &clkregs->sccr2, 17);890}891892if (soc_has_outclk()) {893size_t idx; /* used as mclk_idx, just to trim line length */894for (idx = 0; idx < ARRAY_SIZE(mclk_outclk_data); idx++)895mpc512x_clk_setup_mclk(&mclk_outclk_data[idx], idx);896}897898/*899* externally provided clocks (when implemented in hardware,900* device tree may specify values which otherwise were unknown)901*/902freq = get_freq_from_dt("psc_mclk_in");903if (!freq)904freq = 25000000;905clks[MPC512x_CLK_PSC_MCLK_IN] = mpc512x_clk_fixed("psc_mclk_in", freq);906if (soc_has_mclk_mux0_canin()) {907freq = get_freq_from_dt("can_clk_in");908clks[MPC512x_CLK_CAN_CLK_IN] = mpc512x_clk_fixed(909"can_clk_in", freq);910} else {911freq = get_freq_from_dt("spdif_tx_in");912clks[MPC512x_CLK_SPDIF_TX_IN] = mpc512x_clk_fixed(913"spdif_tx_in", freq);914freq = get_freq_from_dt("spdif_rx_in");915clks[MPC512x_CLK_SPDIF_TX_IN] = mpc512x_clk_fixed(916"spdif_rx_in", freq);917}918919/* fixed frequency for AC97, always 24.567MHz */920clks[MPC512x_CLK_AC97] = mpc512x_clk_fixed("ac97", 24567000);921922/*923* pre-enable those "internal" clock items which never get924* claimed by any peripheral driver, to not have the clock925* subsystem disable them late at startup926*/927clk_prepare_enable(clks[MPC512x_CLK_DUMMY]);928clk_prepare_enable(clks[MPC512x_CLK_E300]); /* PowerPC CPU */929clk_prepare_enable(clks[MPC512x_CLK_DDR]); /* DRAM */930clk_prepare_enable(clks[MPC512x_CLK_MEM]); /* SRAM */931clk_prepare_enable(clks[MPC512x_CLK_IPS]); /* SoC periph */932clk_prepare_enable(clks[MPC512x_CLK_LPC]); /* boot media */933}934935/*936* registers the set of public clocks (those listed in the dt-bindings/937* header file) for OF lookups, keeps the intermediates private to us938*/939static void __init mpc5121_clk_register_of_provider(struct device_node *np)940{941clk_data.clks = clks;942clk_data.clk_num = MPC512x_CLK_LAST_PUBLIC + 1; /* _not_ ARRAY_SIZE() */943of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);944}945946/*947* temporary support for the period of time between introduction of CCF948* support and the adjustment of peripheral drivers to OF based lookups949*/950static void __init mpc5121_clk_provide_migration_support(void)951{952struct device_node *np;953/*954* pre-enable those clock items which are not yet appropriately955* acquired by their peripheral driver956*957* the PCI clock cannot get acquired by its peripheral driver,958* because for this platform the driver won't probe(), instead959* initialization is done from within the .setup_arch() routine960* at a point in time where the clock provider has not been961* setup yet and thus isn't available yet962*963* so we "pre-enable" the clock here, to not have the clock964* subsystem automatically disable this item in a late init call965*966* this PCI clock pre-enable workaround only applies when there967* are device tree nodes for PCI and thus the peripheral driver968* has attached to bridges, otherwise the PCI clock remains969* unused and so it gets disabled970*/971clk_prepare_enable(clks[MPC512x_CLK_PSC3_MCLK]);/* serial console */972np = of_find_compatible_node(NULL, "pci", "fsl,mpc5121-pci");973of_node_put(np);974if (np)975clk_prepare_enable(clks[MPC512x_CLK_PCI]);976}977978/*979* those macros are not exactly pretty, but they encapsulate a lot980* of copy'n'paste heavy code which is even more ugly, and reduce981* the potential for inconsistencies in those many code copies982*/983#define FOR_NODES(compatname) \984for_each_compatible_node(np, NULL, compatname)985986#define NODE_PREP do { \987of_address_to_resource(np, 0, &res); \988snprintf(devname, sizeof(devname), "%pa.%s", &res.start, np->name); \989} while (0)990991#define NODE_CHK(clkname, clkitem, regnode, regflag) do { \992struct clk *clk; \993clk = of_clk_get_by_name(np, clkname); \994if (IS_ERR(clk)) { \995clk = clkitem; \996clk_register_clkdev(clk, clkname, devname); \997if (regnode) \998clk_register_clkdev(clk, clkname, np->name); \999did_register |= DID_REG_ ## regflag; \1000pr_debug("clock alias name '%s' for dev '%s' pointer %p\n", \1001clkname, devname, clk); \1002} else { \1003clk_put(clk); \1004} \1005} while (0)10061007/*1008* register source code provided fallback results for clock lookups,1009* these get consulted when OF based clock lookup fails (that is in the1010* case of not yet adjusted device tree data, where clock related specs1011* are missing)1012*/1013static void __init mpc5121_clk_provide_backwards_compat(void)1014{1015enum did_reg_flags {1016DID_REG_PSC = BIT(0),1017DID_REG_PSCFIFO = BIT(1),1018DID_REG_NFC = BIT(2),1019DID_REG_CAN = BIT(3),1020DID_REG_I2C = BIT(4),1021DID_REG_DIU = BIT(5),1022DID_REG_VIU = BIT(6),1023DID_REG_FEC = BIT(7),1024DID_REG_USB = BIT(8),1025DID_REG_PATA = BIT(9),1026};10271028int did_register;1029struct device_node *np;1030struct resource res;1031int idx;1032char devname[32];10331034did_register = 0;10351036FOR_NODES(mpc512x_select_psc_compat()) {1037NODE_PREP;1038idx = (res.start >> 8) & 0xf;1039NODE_CHK("ipg", clks[MPC512x_CLK_PSC0 + idx], 0, PSC);1040NODE_CHK("mclk", clks[MPC512x_CLK_PSC0_MCLK + idx], 0, PSC);1041}10421043FOR_NODES("fsl,mpc5121-psc-fifo") {1044NODE_PREP;1045NODE_CHK("ipg", clks[MPC512x_CLK_PSC_FIFO], 1, PSCFIFO);1046}10471048FOR_NODES("fsl,mpc5121-nfc") {1049NODE_PREP;1050NODE_CHK("ipg", clks[MPC512x_CLK_NFC], 0, NFC);1051}10521053FOR_NODES("fsl,mpc5121-mscan") {1054NODE_PREP;1055idx = 0;1056idx += (res.start & 0x2000) ? 2 : 0;1057idx += (res.start & 0x0080) ? 1 : 0;1058NODE_CHK("ipg", clks[MPC512x_CLK_BDLC], 0, CAN);1059NODE_CHK("mclk", clks[MPC512x_CLK_MSCAN0_MCLK + idx], 0, CAN);1060}10611062/*1063* do register the 'ips', 'sys', and 'ref' names globally1064* instead of inside each individual CAN node, as there is no1065* potential for a name conflict (in contrast to 'ipg' and 'mclk')1066*/1067if (did_register & DID_REG_CAN) {1068clk_register_clkdev(clks[MPC512x_CLK_IPS], "ips", NULL);1069clk_register_clkdev(clks[MPC512x_CLK_SYS], "sys", NULL);1070clk_register_clkdev(clks[MPC512x_CLK_REF], "ref", NULL);1071}10721073FOR_NODES("fsl,mpc5121-i2c") {1074NODE_PREP;1075NODE_CHK("ipg", clks[MPC512x_CLK_I2C], 0, I2C);1076}10771078/*1079* workaround for the fact that the I2C driver does an "anonymous"1080* lookup (NULL name spec, which yields the first clock spec) for1081* which we cannot register an alias -- a _global_ 'ipg' alias that1082* is not bound to any device name and returns the I2C clock item1083* is not a good idea1084*1085* so we have the lookup in the peripheral driver fail, which is1086* silent and non-fatal, and pre-enable the clock item here such1087* that register access is possible1088*1089* see commit b3bfce2b "i2c: mpc: cleanup clock API use" for1090* details, adjusting s/NULL/"ipg"/ in i2c-mpc.c would make this1091* workaround obsolete1092*/1093if (did_register & DID_REG_I2C)1094clk_prepare_enable(clks[MPC512x_CLK_I2C]);10951096FOR_NODES("fsl,mpc5121-diu") {1097NODE_PREP;1098NODE_CHK("ipg", clks[MPC512x_CLK_DIU], 1, DIU);1099}11001101FOR_NODES("fsl,mpc5121-viu") {1102NODE_PREP;1103NODE_CHK("ipg", clks[MPC512x_CLK_VIU], 0, VIU);1104}11051106/*1107* note that 2771399a "fs_enet: cleanup clock API use" did use the1108* "per" string for the clock lookup in contrast to the "ipg" name1109* which most other nodes are using -- this is not a fatal thing1110* but just something to keep in mind when doing compatibility1111* registration, it's a non-issue with up-to-date device tree data1112*/1113FOR_NODES("fsl,mpc5121-fec") {1114NODE_PREP;1115NODE_CHK("per", clks[MPC512x_CLK_FEC], 0, FEC);1116}1117FOR_NODES("fsl,mpc5121-fec-mdio") {1118NODE_PREP;1119NODE_CHK("per", clks[MPC512x_CLK_FEC], 0, FEC);1120}1121/*1122* MPC5125 has two FECs: FEC1 at 0x2800, FEC2 at 0x4800;1123* the clock items don't "form an array" since FEC2 was1124* added only later and was not allowed to shift all other1125* clock item indices, so the numbers aren't adjacent1126*/1127FOR_NODES("fsl,mpc5125-fec") {1128NODE_PREP;1129if (res.start & 0x4000)1130idx = MPC512x_CLK_FEC2;1131else1132idx = MPC512x_CLK_FEC;1133NODE_CHK("per", clks[idx], 0, FEC);1134}11351136FOR_NODES("fsl,mpc5121-usb2-dr") {1137NODE_PREP;1138idx = (res.start & 0x4000) ? 1 : 0;1139NODE_CHK("ipg", clks[MPC512x_CLK_USB1 + idx], 0, USB);1140}11411142FOR_NODES("fsl,mpc5121-pata") {1143NODE_PREP;1144NODE_CHK("ipg", clks[MPC512x_CLK_PATA], 0, PATA);1145}11461147/*1148* try to collapse diagnostics into a single line of output yet1149* provide a full list of what is missing, to avoid noise in the1150* absence of up-to-date device tree data -- backwards1151* compatibility to old DTBs is a requirement, updates may be1152* desirable or preferrable but are not at all mandatory1153*/1154if (did_register) {1155pr_notice("device tree lacks clock specs, adding fallbacks (0x%x,%s%s%s%s%s%s%s%s%s%s)\n",1156did_register,1157(did_register & DID_REG_PSC) ? " PSC" : "",1158(did_register & DID_REG_PSCFIFO) ? " PSCFIFO" : "",1159(did_register & DID_REG_NFC) ? " NFC" : "",1160(did_register & DID_REG_CAN) ? " CAN" : "",1161(did_register & DID_REG_I2C) ? " I2C" : "",1162(did_register & DID_REG_DIU) ? " DIU" : "",1163(did_register & DID_REG_VIU) ? " VIU" : "",1164(did_register & DID_REG_FEC) ? " FEC" : "",1165(did_register & DID_REG_USB) ? " USB" : "",1166(did_register & DID_REG_PATA) ? " PATA" : "");1167} else {1168pr_debug("device tree has clock specs, no fallbacks added\n");1169}1170}11711172/*1173* The "fixed-clock" nodes (which includes the oscillator node if the board's1174* DT provides one) has already been scanned by the of_clk_init() in1175* time_init().1176*/1177int __init mpc5121_clk_init(void)1178{1179struct device_node *clk_np;1180int busfreq;11811182/* map the clock control registers */1183clk_np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-clock");1184if (!clk_np)1185return -ENODEV;1186clkregs = of_iomap(clk_np, 0);1187WARN_ON(!clkregs);11881189/* determine the SoC variant we run on */1190mpc512x_clk_determine_soc();11911192/* invalidate all not yet registered clock slots */1193mpc512x_clk_preset_data();11941195/*1196* add a dummy clock for those situations where a clock spec is1197* required yet no real clock is involved1198*/1199clks[MPC512x_CLK_DUMMY] = mpc512x_clk_fixed("dummy", 0);12001201/*1202* have all the real nodes in the clock tree populated from REF1203* down to all leaves, either starting from the OSC node or from1204* a REF root that was created from the IPS bus clock input1205*/1206busfreq = get_freq_from_dt("bus-frequency");1207mpc512x_clk_setup_clock_tree(clk_np, busfreq);12081209/* register as an OF clock provider */1210mpc5121_clk_register_of_provider(clk_np);12111212of_node_put(clk_np);12131214/*1215* unbreak not yet adjusted peripheral drivers during migration1216* towards fully operational common clock support, and allow1217* operation in the absence of clock related device tree specs1218*/1219mpc5121_clk_provide_migration_support();1220mpc5121_clk_provide_backwards_compat();12211222return 0;1223}122412251226