xref: /OK3568_Linux_fs/kernel/arch/powerpc/platforms/512x/clock-commonclk.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2013 DENX Software Engineering
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Gerhard Sittig, <gsi@denx.de>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * common clock driver support for the MPC512x platform
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/bitops.h>
11*4882a593Smuzhiyun #include <linux/clk.h>
12*4882a593Smuzhiyun #include <linux/clk-provider.h>
13*4882a593Smuzhiyun #include <linux/clkdev.h>
14*4882a593Smuzhiyun #include <linux/device.h>
15*4882a593Smuzhiyun #include <linux/errno.h>
16*4882a593Smuzhiyun #include <linux/io.h>
17*4882a593Smuzhiyun #include <linux/of.h>
18*4882a593Smuzhiyun #include <linux/of_address.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <asm/mpc5121.h>
21*4882a593Smuzhiyun #include <dt-bindings/clock/mpc512x-clock.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include "mpc512x.h"		/* our public mpc5121_clk_init() API */
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /* helpers to keep the MCLK intermediates "somewhere" in our table */
26*4882a593Smuzhiyun enum {
27*4882a593Smuzhiyun 	MCLK_IDX_MUX0,
28*4882a593Smuzhiyun 	MCLK_IDX_EN0,
29*4882a593Smuzhiyun 	MCLK_IDX_DIV0,
30*4882a593Smuzhiyun 	MCLK_MAX_IDX,
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #define NR_PSCS			12
34*4882a593Smuzhiyun #define NR_MSCANS		4
35*4882a593Smuzhiyun #define NR_SPDIFS		1
36*4882a593Smuzhiyun #define NR_OUTCLK		4
37*4882a593Smuzhiyun #define NR_MCLKS		(NR_PSCS + NR_MSCANS + NR_SPDIFS + NR_OUTCLK)
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /* extend the public set of clocks by adding internal slots for management */
40*4882a593Smuzhiyun enum {
41*4882a593Smuzhiyun 	/* arrange for adjacent numbers after the public set */
42*4882a593Smuzhiyun 	MPC512x_CLK_START_PRIVATE = MPC512x_CLK_LAST_PUBLIC,
43*4882a593Smuzhiyun 	/* clocks which aren't announced to the public */
44*4882a593Smuzhiyun 	MPC512x_CLK_DDR,
45*4882a593Smuzhiyun 	MPC512x_CLK_MEM,
46*4882a593Smuzhiyun 	MPC512x_CLK_IIM,
47*4882a593Smuzhiyun 	/* intermediates in div+gate combos or fractional dividers */
48*4882a593Smuzhiyun 	MPC512x_CLK_DDR_UG,
49*4882a593Smuzhiyun 	MPC512x_CLK_SDHC_x4,
50*4882a593Smuzhiyun 	MPC512x_CLK_SDHC_UG,
51*4882a593Smuzhiyun 	MPC512x_CLK_SDHC2_UG,
52*4882a593Smuzhiyun 	MPC512x_CLK_DIU_x4,
53*4882a593Smuzhiyun 	MPC512x_CLK_DIU_UG,
54*4882a593Smuzhiyun 	MPC512x_CLK_MBX_BUS_UG,
55*4882a593Smuzhiyun 	MPC512x_CLK_MBX_UG,
56*4882a593Smuzhiyun 	MPC512x_CLK_MBX_3D_UG,
57*4882a593Smuzhiyun 	MPC512x_CLK_PCI_UG,
58*4882a593Smuzhiyun 	MPC512x_CLK_NFC_UG,
59*4882a593Smuzhiyun 	MPC512x_CLK_LPC_UG,
60*4882a593Smuzhiyun 	MPC512x_CLK_SPDIF_TX_IN,
61*4882a593Smuzhiyun 	/* intermediates for the mux+gate+div+mux MCLK generation */
62*4882a593Smuzhiyun 	MPC512x_CLK_MCLKS_FIRST,
63*4882a593Smuzhiyun 	MPC512x_CLK_MCLKS_LAST = MPC512x_CLK_MCLKS_FIRST
64*4882a593Smuzhiyun 				+ NR_MCLKS * MCLK_MAX_IDX,
65*4882a593Smuzhiyun 	/* internal, symbolic spec for the number of slots */
66*4882a593Smuzhiyun 	MPC512x_CLK_LAST_PRIVATE,
67*4882a593Smuzhiyun };
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /* data required for the OF clock provider registration */
70*4882a593Smuzhiyun static struct clk *clks[MPC512x_CLK_LAST_PRIVATE];
71*4882a593Smuzhiyun static struct clk_onecell_data clk_data;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /* CCM register access */
74*4882a593Smuzhiyun static struct mpc512x_ccm __iomem *clkregs;
75*4882a593Smuzhiyun static DEFINE_SPINLOCK(clklock);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun /* SoC variants {{{ */
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /*
80*4882a593Smuzhiyun  * tell SoC variants apart as they are rather similar yet not identical,
81*4882a593Smuzhiyun  * cache the result in an enum to not repeatedly run the expensive OF test
82*4882a593Smuzhiyun  *
83*4882a593Smuzhiyun  * MPC5123 is an MPC5121 without the MBX graphics accelerator
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  * MPC5125 has many more differences: no MBX, no AXE, no VIU, no SPDIF,
86*4882a593Smuzhiyun  * no PATA, no SATA, no PCI, two FECs (of different compatibility name),
87*4882a593Smuzhiyun  * only 10 PSCs (of different compatibility name), two SDHCs, different
88*4882a593Smuzhiyun  * NFC IP block, output clocks, system PLL status query, different CPMF
89*4882a593Smuzhiyun  * interpretation, no CFM, different fourth PSC/CAN mux0 input -- yet
90*4882a593Smuzhiyun  * those differences can get folded into this clock provider support
91*4882a593Smuzhiyun  * code and don't warrant a separate highly redundant implementation
92*4882a593Smuzhiyun  */
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun static enum soc_type {
95*4882a593Smuzhiyun 	MPC512x_SOC_MPC5121,
96*4882a593Smuzhiyun 	MPC512x_SOC_MPC5123,
97*4882a593Smuzhiyun 	MPC512x_SOC_MPC5125,
98*4882a593Smuzhiyun } soc;
99*4882a593Smuzhiyun 
mpc512x_clk_determine_soc(void)100*4882a593Smuzhiyun static void mpc512x_clk_determine_soc(void)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun 	if (of_machine_is_compatible("fsl,mpc5121")) {
103*4882a593Smuzhiyun 		soc = MPC512x_SOC_MPC5121;
104*4882a593Smuzhiyun 		return;
105*4882a593Smuzhiyun 	}
106*4882a593Smuzhiyun 	if (of_machine_is_compatible("fsl,mpc5123")) {
107*4882a593Smuzhiyun 		soc = MPC512x_SOC_MPC5123;
108*4882a593Smuzhiyun 		return;
109*4882a593Smuzhiyun 	}
110*4882a593Smuzhiyun 	if (of_machine_is_compatible("fsl,mpc5125")) {
111*4882a593Smuzhiyun 		soc = MPC512x_SOC_MPC5125;
112*4882a593Smuzhiyun 		return;
113*4882a593Smuzhiyun 	}
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun 
soc_has_mbx(void)116*4882a593Smuzhiyun static bool soc_has_mbx(void)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	if (soc == MPC512x_SOC_MPC5121)
119*4882a593Smuzhiyun 		return true;
120*4882a593Smuzhiyun 	return false;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
soc_has_axe(void)123*4882a593Smuzhiyun static bool soc_has_axe(void)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	if (soc == MPC512x_SOC_MPC5125)
126*4882a593Smuzhiyun 		return false;
127*4882a593Smuzhiyun 	return true;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
soc_has_viu(void)130*4882a593Smuzhiyun static bool soc_has_viu(void)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun 	if (soc == MPC512x_SOC_MPC5125)
133*4882a593Smuzhiyun 		return false;
134*4882a593Smuzhiyun 	return true;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun 
soc_has_spdif(void)137*4882a593Smuzhiyun static bool soc_has_spdif(void)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	if (soc == MPC512x_SOC_MPC5125)
140*4882a593Smuzhiyun 		return false;
141*4882a593Smuzhiyun 	return true;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
soc_has_pata(void)144*4882a593Smuzhiyun static bool soc_has_pata(void)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	if (soc == MPC512x_SOC_MPC5125)
147*4882a593Smuzhiyun 		return false;
148*4882a593Smuzhiyun 	return true;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
soc_has_sata(void)151*4882a593Smuzhiyun static bool soc_has_sata(void)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	if (soc == MPC512x_SOC_MPC5125)
154*4882a593Smuzhiyun 		return false;
155*4882a593Smuzhiyun 	return true;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
soc_has_pci(void)158*4882a593Smuzhiyun static bool soc_has_pci(void)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun 	if (soc == MPC512x_SOC_MPC5125)
161*4882a593Smuzhiyun 		return false;
162*4882a593Smuzhiyun 	return true;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun 
soc_has_fec2(void)165*4882a593Smuzhiyun static bool soc_has_fec2(void)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	if (soc == MPC512x_SOC_MPC5125)
168*4882a593Smuzhiyun 		return true;
169*4882a593Smuzhiyun 	return false;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
soc_max_pscnum(void)172*4882a593Smuzhiyun static int soc_max_pscnum(void)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun 	if (soc == MPC512x_SOC_MPC5125)
175*4882a593Smuzhiyun 		return 10;
176*4882a593Smuzhiyun 	return 12;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
soc_has_sdhc2(void)179*4882a593Smuzhiyun static bool soc_has_sdhc2(void)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun 	if (soc == MPC512x_SOC_MPC5125)
182*4882a593Smuzhiyun 		return true;
183*4882a593Smuzhiyun 	return false;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun 
soc_has_nfc_5125(void)186*4882a593Smuzhiyun static bool soc_has_nfc_5125(void)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun 	if (soc == MPC512x_SOC_MPC5125)
189*4882a593Smuzhiyun 		return true;
190*4882a593Smuzhiyun 	return false;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun 
soc_has_outclk(void)193*4882a593Smuzhiyun static bool soc_has_outclk(void)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	if (soc == MPC512x_SOC_MPC5125)
196*4882a593Smuzhiyun 		return true;
197*4882a593Smuzhiyun 	return false;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
soc_has_cpmf_0_bypass(void)200*4882a593Smuzhiyun static bool soc_has_cpmf_0_bypass(void)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	if (soc == MPC512x_SOC_MPC5125)
203*4882a593Smuzhiyun 		return true;
204*4882a593Smuzhiyun 	return false;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun 
soc_has_mclk_mux0_canin(void)207*4882a593Smuzhiyun static bool soc_has_mclk_mux0_canin(void)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun 	if (soc == MPC512x_SOC_MPC5125)
210*4882a593Smuzhiyun 		return true;
211*4882a593Smuzhiyun 	return false;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun /* }}} SoC variants */
215*4882a593Smuzhiyun /* common clk API wrappers {{{ */
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun /* convenience wrappers around the common clk API */
mpc512x_clk_fixed(const char * name,int rate)218*4882a593Smuzhiyun static inline struct clk *mpc512x_clk_fixed(const char *name, int rate)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun 	return clk_register_fixed_rate(NULL, name, NULL, 0, rate);
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun 
mpc512x_clk_factor(const char * name,const char * parent_name,int mul,int div)223*4882a593Smuzhiyun static inline struct clk *mpc512x_clk_factor(
224*4882a593Smuzhiyun 	const char *name, const char *parent_name,
225*4882a593Smuzhiyun 	int mul, int div)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun 	int clkflags;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	clkflags = CLK_SET_RATE_PARENT;
230*4882a593Smuzhiyun 	return clk_register_fixed_factor(NULL, name, parent_name, clkflags,
231*4882a593Smuzhiyun 					 mul, div);
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
mpc512x_clk_divider(const char * name,const char * parent_name,u8 clkflags,u32 __iomem * reg,u8 pos,u8 len,int divflags)234*4882a593Smuzhiyun static inline struct clk *mpc512x_clk_divider(
235*4882a593Smuzhiyun 	const char *name, const char *parent_name, u8 clkflags,
236*4882a593Smuzhiyun 	u32 __iomem *reg, u8 pos, u8 len, int divflags)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun 	divflags |= CLK_DIVIDER_BIG_ENDIAN;
239*4882a593Smuzhiyun 	return clk_register_divider(NULL, name, parent_name, clkflags,
240*4882a593Smuzhiyun 				    reg, pos, len, divflags, &clklock);
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun 
mpc512x_clk_divtable(const char * name,const char * parent_name,u32 __iomem * reg,u8 pos,u8 len,const struct clk_div_table * divtab)243*4882a593Smuzhiyun static inline struct clk *mpc512x_clk_divtable(
244*4882a593Smuzhiyun 	const char *name, const char *parent_name,
245*4882a593Smuzhiyun 	u32 __iomem *reg, u8 pos, u8 len,
246*4882a593Smuzhiyun 	const struct clk_div_table *divtab)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	u8 divflags;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	divflags = CLK_DIVIDER_BIG_ENDIAN;
251*4882a593Smuzhiyun 	return clk_register_divider_table(NULL, name, parent_name, 0,
252*4882a593Smuzhiyun 					  reg, pos, len, divflags,
253*4882a593Smuzhiyun 					  divtab, &clklock);
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun 
mpc512x_clk_gated(const char * name,const char * parent_name,u32 __iomem * reg,u8 pos)256*4882a593Smuzhiyun static inline struct clk *mpc512x_clk_gated(
257*4882a593Smuzhiyun 	const char *name, const char *parent_name,
258*4882a593Smuzhiyun 	u32 __iomem *reg, u8 pos)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun 	int clkflags;
261*4882a593Smuzhiyun 	u8 gateflags;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	clkflags = CLK_SET_RATE_PARENT;
264*4882a593Smuzhiyun 	gateflags = CLK_GATE_BIG_ENDIAN;
265*4882a593Smuzhiyun 	return clk_register_gate(NULL, name, parent_name, clkflags,
266*4882a593Smuzhiyun 				 reg, pos, gateflags, &clklock);
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun 
mpc512x_clk_muxed(const char * name,const char ** parent_names,int parent_count,u32 __iomem * reg,u8 pos,u8 len)269*4882a593Smuzhiyun static inline struct clk *mpc512x_clk_muxed(const char *name,
270*4882a593Smuzhiyun 	const char **parent_names, int parent_count,
271*4882a593Smuzhiyun 	u32 __iomem *reg, u8 pos, u8 len)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun 	int clkflags;
274*4882a593Smuzhiyun 	u8 muxflags;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	clkflags = CLK_SET_RATE_PARENT;
277*4882a593Smuzhiyun 	muxflags = CLK_MUX_BIG_ENDIAN;
278*4882a593Smuzhiyun 	return clk_register_mux(NULL, name,
279*4882a593Smuzhiyun 				parent_names, parent_count, clkflags,
280*4882a593Smuzhiyun 				reg, pos, len, muxflags, &clklock);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun /* }}} common clk API wrappers */
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun /* helper to isolate a bit field from a register */
get_bit_field(uint32_t __iomem * reg,uint8_t pos,uint8_t len)286*4882a593Smuzhiyun static inline int get_bit_field(uint32_t __iomem *reg, uint8_t pos, uint8_t len)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun 	uint32_t val;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	val = in_be32(reg);
291*4882a593Smuzhiyun 	val >>= pos;
292*4882a593Smuzhiyun 	val &= (1 << len) - 1;
293*4882a593Smuzhiyun 	return val;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun /* get the SPMF and translate it into the "sys pll" multiplier */
get_spmf_mult(void)297*4882a593Smuzhiyun static int get_spmf_mult(void)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun 	static int spmf_to_mult[] = {
300*4882a593Smuzhiyun 		68, 1, 12, 16, 20, 24, 28, 32,
301*4882a593Smuzhiyun 		36, 40, 44, 48, 52, 56, 60, 64,
302*4882a593Smuzhiyun 	};
303*4882a593Smuzhiyun 	int spmf;
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	spmf = get_bit_field(&clkregs->spmr, 24, 4);
306*4882a593Smuzhiyun 	return spmf_to_mult[spmf];
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun /*
310*4882a593Smuzhiyun  * get the SYS_DIV value and translate it into a divide factor
311*4882a593Smuzhiyun  *
312*4882a593Smuzhiyun  * values returned from here are a multiple of the real factor since the
313*4882a593Smuzhiyun  * divide ratio is fractional
314*4882a593Smuzhiyun  */
get_sys_div_x2(void)315*4882a593Smuzhiyun static int get_sys_div_x2(void)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun 	static int sysdiv_code_to_x2[] = {
318*4882a593Smuzhiyun 		4, 5, 6, 7, 8, 9, 10, 14,
319*4882a593Smuzhiyun 		12, 16, 18, 22, 20, 24, 26, 30,
320*4882a593Smuzhiyun 		28, 32, 34, 38, 36, 40, 42, 46,
321*4882a593Smuzhiyun 		44, 48, 50, 54, 52, 56, 58, 62,
322*4882a593Smuzhiyun 		60, 64, 66,
323*4882a593Smuzhiyun 	};
324*4882a593Smuzhiyun 	int divcode;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	divcode = get_bit_field(&clkregs->scfr2, 26, 6);
327*4882a593Smuzhiyun 	return sysdiv_code_to_x2[divcode];
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun /*
331*4882a593Smuzhiyun  * get the CPMF value and translate it into a multiplier factor
332*4882a593Smuzhiyun  *
333*4882a593Smuzhiyun  * values returned from here are a multiple of the real factor since the
334*4882a593Smuzhiyun  * multiplier ratio is fractional
335*4882a593Smuzhiyun  */
get_cpmf_mult_x2(void)336*4882a593Smuzhiyun static int get_cpmf_mult_x2(void)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun 	static int cpmf_to_mult_x36[] = {
339*4882a593Smuzhiyun 		/* 0b000 is "times 36" */
340*4882a593Smuzhiyun 		72, 2, 2, 3, 4, 5, 6, 7,
341*4882a593Smuzhiyun 	};
342*4882a593Smuzhiyun 	static int cpmf_to_mult_0by[] = {
343*4882a593Smuzhiyun 		/* 0b000 is "bypass" */
344*4882a593Smuzhiyun 		2, 2, 2, 3, 4, 5, 6, 7,
345*4882a593Smuzhiyun 	};
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	int *cpmf_to_mult;
348*4882a593Smuzhiyun 	int cpmf;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	cpmf = get_bit_field(&clkregs->spmr, 16, 4);
351*4882a593Smuzhiyun 	if (soc_has_cpmf_0_bypass())
352*4882a593Smuzhiyun 		cpmf_to_mult = cpmf_to_mult_0by;
353*4882a593Smuzhiyun 	else
354*4882a593Smuzhiyun 		cpmf_to_mult = cpmf_to_mult_x36;
355*4882a593Smuzhiyun 	return cpmf_to_mult[cpmf];
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun /*
359*4882a593Smuzhiyun  * some of the clock dividers do scale in a linear way, yet not all of
360*4882a593Smuzhiyun  * their bit combinations are legal; use a divider table to get a
361*4882a593Smuzhiyun  * resulting set of applicable divider values
362*4882a593Smuzhiyun  */
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun /* applies to the IPS_DIV, and PCI_DIV values */
365*4882a593Smuzhiyun static const struct clk_div_table divtab_2346[] = {
366*4882a593Smuzhiyun 	{ .val = 2, .div = 2, },
367*4882a593Smuzhiyun 	{ .val = 3, .div = 3, },
368*4882a593Smuzhiyun 	{ .val = 4, .div = 4, },
369*4882a593Smuzhiyun 	{ .val = 6, .div = 6, },
370*4882a593Smuzhiyun 	{ .div = 0, },
371*4882a593Smuzhiyun };
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun /* applies to the MBX_DIV, LPC_DIV, and NFC_DIV values */
374*4882a593Smuzhiyun static const struct clk_div_table divtab_1234[] = {
375*4882a593Smuzhiyun 	{ .val = 1, .div = 1, },
376*4882a593Smuzhiyun 	{ .val = 2, .div = 2, },
377*4882a593Smuzhiyun 	{ .val = 3, .div = 3, },
378*4882a593Smuzhiyun 	{ .val = 4, .div = 4, },
379*4882a593Smuzhiyun 	{ .div = 0, },
380*4882a593Smuzhiyun };
381*4882a593Smuzhiyun 
get_freq_from_dt(char * propname)382*4882a593Smuzhiyun static int get_freq_from_dt(char *propname)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun 	struct device_node *np;
385*4882a593Smuzhiyun 	const unsigned int *prop;
386*4882a593Smuzhiyun 	int val;
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	val = 0;
389*4882a593Smuzhiyun 	np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-immr");
390*4882a593Smuzhiyun 	if (np) {
391*4882a593Smuzhiyun 		prop = of_get_property(np, propname, NULL);
392*4882a593Smuzhiyun 		if (prop)
393*4882a593Smuzhiyun 			val = *prop;
394*4882a593Smuzhiyun 	    of_node_put(np);
395*4882a593Smuzhiyun 	}
396*4882a593Smuzhiyun 	return val;
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun 
mpc512x_clk_preset_data(void)399*4882a593Smuzhiyun static void mpc512x_clk_preset_data(void)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun 	size_t i;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(clks); i++)
404*4882a593Smuzhiyun 		clks[i] = ERR_PTR(-ENODEV);
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun /*
408*4882a593Smuzhiyun  * - receives the "bus frequency" from the caller (that's the IPS clock
409*4882a593Smuzhiyun  *   rate, the historical source of clock information)
410*4882a593Smuzhiyun  * - fetches the system PLL multiplier and divider values as well as the
411*4882a593Smuzhiyun  *   IPS divider value from hardware
412*4882a593Smuzhiyun  * - determines the REF clock rate either from the XTAL/OSC spec (if
413*4882a593Smuzhiyun  *   there is a device tree node describing the oscillator) or from the
414*4882a593Smuzhiyun  *   IPS bus clock (supported for backwards compatibility, such that
415*4882a593Smuzhiyun  *   setups without XTAL/OSC specs keep working)
416*4882a593Smuzhiyun  * - creates the "ref" clock item in the clock tree, such that
417*4882a593Smuzhiyun  *   subsequent code can create the remainder of the hierarchy (REF ->
418*4882a593Smuzhiyun  *   SYS -> CSB -> IPS) from the REF clock rate and the returned mul/div
419*4882a593Smuzhiyun  *   values
420*4882a593Smuzhiyun  */
mpc512x_clk_setup_ref_clock(struct device_node * np,int bus_freq,int * sys_mul,int * sys_div,int * ips_div)421*4882a593Smuzhiyun static void mpc512x_clk_setup_ref_clock(struct device_node *np, int bus_freq,
422*4882a593Smuzhiyun 					int *sys_mul, int *sys_div,
423*4882a593Smuzhiyun 					int *ips_div)
424*4882a593Smuzhiyun {
425*4882a593Smuzhiyun 	struct clk *osc_clk;
426*4882a593Smuzhiyun 	int calc_freq;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	/* fetch mul/div factors from the hardware */
429*4882a593Smuzhiyun 	*sys_mul = get_spmf_mult();
430*4882a593Smuzhiyun 	*sys_mul *= 2;		/* compensate for the fractional divider */
431*4882a593Smuzhiyun 	*sys_div = get_sys_div_x2();
432*4882a593Smuzhiyun 	*ips_div = get_bit_field(&clkregs->scfr1, 23, 3);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	/* lookup the oscillator clock for its rate */
435*4882a593Smuzhiyun 	osc_clk = of_clk_get_by_name(np, "osc");
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	/*
438*4882a593Smuzhiyun 	 * either descend from OSC to REF (and in bypassing verify the
439*4882a593Smuzhiyun 	 * IPS rate), or backtrack from IPS and multiplier values that
440*4882a593Smuzhiyun 	 * were fetched from hardware to REF and thus to the OSC value
441*4882a593Smuzhiyun 	 *
442*4882a593Smuzhiyun 	 * in either case the REF clock gets created here and the
443*4882a593Smuzhiyun 	 * remainder of the clock tree can get spanned from there
444*4882a593Smuzhiyun 	 */
445*4882a593Smuzhiyun 	if (!IS_ERR(osc_clk)) {
446*4882a593Smuzhiyun 		clks[MPC512x_CLK_REF] = mpc512x_clk_factor("ref", "osc", 1, 1);
447*4882a593Smuzhiyun 		calc_freq = clk_get_rate(clks[MPC512x_CLK_REF]);
448*4882a593Smuzhiyun 		calc_freq *= *sys_mul;
449*4882a593Smuzhiyun 		calc_freq /= *sys_div;
450*4882a593Smuzhiyun 		calc_freq /= 2;
451*4882a593Smuzhiyun 		calc_freq /= *ips_div;
452*4882a593Smuzhiyun 		if (bus_freq && calc_freq != bus_freq)
453*4882a593Smuzhiyun 			pr_warn("calc rate %d != OF spec %d\n",
454*4882a593Smuzhiyun 				calc_freq, bus_freq);
455*4882a593Smuzhiyun 	} else {
456*4882a593Smuzhiyun 		calc_freq = bus_freq;	/* start with IPS */
457*4882a593Smuzhiyun 		calc_freq *= *ips_div;	/* IPS -> CSB */
458*4882a593Smuzhiyun 		calc_freq *= 2;		/* CSB -> SYS */
459*4882a593Smuzhiyun 		calc_freq *= *sys_div;	/* SYS -> PLL out */
460*4882a593Smuzhiyun 		calc_freq /= *sys_mul;	/* PLL out -> REF == OSC */
461*4882a593Smuzhiyun 		clks[MPC512x_CLK_REF] = mpc512x_clk_fixed("ref", calc_freq);
462*4882a593Smuzhiyun 	}
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun /* MCLK helpers {{{ */
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun /*
468*4882a593Smuzhiyun  * helper code for the MCLK subtree setup
469*4882a593Smuzhiyun  *
470*4882a593Smuzhiyun  * the overview in section 5.2.4 of the MPC5121e Reference Manual rev4
471*4882a593Smuzhiyun  * suggests that all instances of the "PSC clock generation" are equal,
472*4882a593Smuzhiyun  * and that one might re-use the PSC setup for MSCAN clock generation
473*4882a593Smuzhiyun  * (section 5.2.5) as well, at least the logic if not the data for
474*4882a593Smuzhiyun  * description
475*4882a593Smuzhiyun  *
476*4882a593Smuzhiyun  * the details (starting at page 5-20) show differences in the specific
477*4882a593Smuzhiyun  * inputs of the first mux stage ("can clk in", "spdif tx"), and the
478*4882a593Smuzhiyun  * factual non-availability of the second mux stage (it's present yet
479*4882a593Smuzhiyun  * only one input is valid)
480*4882a593Smuzhiyun  *
481*4882a593Smuzhiyun  * the MSCAN clock related registers (starting at page 5-35) all
482*4882a593Smuzhiyun  * reference "spdif clk" at the first mux stage and don't mention any
483*4882a593Smuzhiyun  * "can clk" at all, which somehow is unexpected
484*4882a593Smuzhiyun  *
485*4882a593Smuzhiyun  * TODO re-check the document, and clarify whether the RM is correct in
486*4882a593Smuzhiyun  * the overview or in the details, and whether the difference is a
487*4882a593Smuzhiyun  * clipboard induced error or results from chip revisions
488*4882a593Smuzhiyun  *
489*4882a593Smuzhiyun  * it turns out that the RM rev4 as of 2012-06 talks about "can" for the
490*4882a593Smuzhiyun  * PSCs while RM rev3 as of 2008-10 talks about "spdif", so I guess that
491*4882a593Smuzhiyun  * first a doc update is required which better reflects reality in the
492*4882a593Smuzhiyun  * SoC before the implementation should follow while no questions remain
493*4882a593Smuzhiyun  */
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun /*
496*4882a593Smuzhiyun  * note that this declaration raises a checkpatch warning, but
497*4882a593Smuzhiyun  * it's the very data type dictated by <linux/clk-provider.h>,
498*4882a593Smuzhiyun  * "fixing" this warning will break compilation
499*4882a593Smuzhiyun  */
500*4882a593Smuzhiyun static const char *parent_names_mux0_spdif[] = {
501*4882a593Smuzhiyun 	"sys", "ref", "psc-mclk-in", "spdif-tx",
502*4882a593Smuzhiyun };
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun static const char *parent_names_mux0_canin[] = {
505*4882a593Smuzhiyun 	"sys", "ref", "psc-mclk-in", "can-clk-in",
506*4882a593Smuzhiyun };
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun enum mclk_type {
509*4882a593Smuzhiyun 	MCLK_TYPE_PSC,
510*4882a593Smuzhiyun 	MCLK_TYPE_MSCAN,
511*4882a593Smuzhiyun 	MCLK_TYPE_SPDIF,
512*4882a593Smuzhiyun 	MCLK_TYPE_OUTCLK,
513*4882a593Smuzhiyun };
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun struct mclk_setup_data {
516*4882a593Smuzhiyun 	enum mclk_type type;
517*4882a593Smuzhiyun 	bool has_mclk1;
518*4882a593Smuzhiyun 	const char *name_mux0;
519*4882a593Smuzhiyun 	const char *name_en0;
520*4882a593Smuzhiyun 	const char *name_div0;
521*4882a593Smuzhiyun 	const char *parent_names_mux1[2];
522*4882a593Smuzhiyun 	const char *name_mclk;
523*4882a593Smuzhiyun };
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun #define MCLK_SETUP_DATA_PSC(id) { \
526*4882a593Smuzhiyun 	MCLK_TYPE_PSC, 0, \
527*4882a593Smuzhiyun 	"psc" #id "-mux0", \
528*4882a593Smuzhiyun 	"psc" #id "-en0", \
529*4882a593Smuzhiyun 	"psc" #id "_mclk_div", \
530*4882a593Smuzhiyun 	{ "psc" #id "_mclk_div", "dummy", }, \
531*4882a593Smuzhiyun 	"psc" #id "_mclk", \
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun #define MCLK_SETUP_DATA_MSCAN(id) { \
535*4882a593Smuzhiyun 	MCLK_TYPE_MSCAN, 0, \
536*4882a593Smuzhiyun 	"mscan" #id "-mux0", \
537*4882a593Smuzhiyun 	"mscan" #id "-en0", \
538*4882a593Smuzhiyun 	"mscan" #id "_mclk_div", \
539*4882a593Smuzhiyun 	{ "mscan" #id "_mclk_div", "dummy", }, \
540*4882a593Smuzhiyun 	"mscan" #id "_mclk", \
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun #define MCLK_SETUP_DATA_SPDIF { \
544*4882a593Smuzhiyun 	MCLK_TYPE_SPDIF, 1, \
545*4882a593Smuzhiyun 	"spdif-mux0", \
546*4882a593Smuzhiyun 	"spdif-en0", \
547*4882a593Smuzhiyun 	"spdif_mclk_div", \
548*4882a593Smuzhiyun 	{ "spdif_mclk_div", "spdif-rx", }, \
549*4882a593Smuzhiyun 	"spdif_mclk", \
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun #define MCLK_SETUP_DATA_OUTCLK(id) { \
553*4882a593Smuzhiyun 	MCLK_TYPE_OUTCLK, 0, \
554*4882a593Smuzhiyun 	"out" #id "-mux0", \
555*4882a593Smuzhiyun 	"out" #id "-en0", \
556*4882a593Smuzhiyun 	"out" #id "_mclk_div", \
557*4882a593Smuzhiyun 	{ "out" #id "_mclk_div", "dummy", }, \
558*4882a593Smuzhiyun 	"out" #id "_clk", \
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun static struct mclk_setup_data mclk_psc_data[] = {
562*4882a593Smuzhiyun 	MCLK_SETUP_DATA_PSC(0),
563*4882a593Smuzhiyun 	MCLK_SETUP_DATA_PSC(1),
564*4882a593Smuzhiyun 	MCLK_SETUP_DATA_PSC(2),
565*4882a593Smuzhiyun 	MCLK_SETUP_DATA_PSC(3),
566*4882a593Smuzhiyun 	MCLK_SETUP_DATA_PSC(4),
567*4882a593Smuzhiyun 	MCLK_SETUP_DATA_PSC(5),
568*4882a593Smuzhiyun 	MCLK_SETUP_DATA_PSC(6),
569*4882a593Smuzhiyun 	MCLK_SETUP_DATA_PSC(7),
570*4882a593Smuzhiyun 	MCLK_SETUP_DATA_PSC(8),
571*4882a593Smuzhiyun 	MCLK_SETUP_DATA_PSC(9),
572*4882a593Smuzhiyun 	MCLK_SETUP_DATA_PSC(10),
573*4882a593Smuzhiyun 	MCLK_SETUP_DATA_PSC(11),
574*4882a593Smuzhiyun };
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun static struct mclk_setup_data mclk_mscan_data[] = {
577*4882a593Smuzhiyun 	MCLK_SETUP_DATA_MSCAN(0),
578*4882a593Smuzhiyun 	MCLK_SETUP_DATA_MSCAN(1),
579*4882a593Smuzhiyun 	MCLK_SETUP_DATA_MSCAN(2),
580*4882a593Smuzhiyun 	MCLK_SETUP_DATA_MSCAN(3),
581*4882a593Smuzhiyun };
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun static struct mclk_setup_data mclk_spdif_data[] = {
584*4882a593Smuzhiyun 	MCLK_SETUP_DATA_SPDIF,
585*4882a593Smuzhiyun };
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun static struct mclk_setup_data mclk_outclk_data[] = {
588*4882a593Smuzhiyun 	MCLK_SETUP_DATA_OUTCLK(0),
589*4882a593Smuzhiyun 	MCLK_SETUP_DATA_OUTCLK(1),
590*4882a593Smuzhiyun 	MCLK_SETUP_DATA_OUTCLK(2),
591*4882a593Smuzhiyun 	MCLK_SETUP_DATA_OUTCLK(3),
592*4882a593Smuzhiyun };
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun /* setup the MCLK clock subtree of an individual PSC/MSCAN/SPDIF */
mpc512x_clk_setup_mclk(struct mclk_setup_data * entry,size_t idx)595*4882a593Smuzhiyun static void mpc512x_clk_setup_mclk(struct mclk_setup_data *entry, size_t idx)
596*4882a593Smuzhiyun {
597*4882a593Smuzhiyun 	size_t clks_idx_pub, clks_idx_int;
598*4882a593Smuzhiyun 	u32 __iomem *mccr_reg;	/* MCLK control register (mux, en, div) */
599*4882a593Smuzhiyun 	int div;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	/* derive a few parameters from the component type and index */
602*4882a593Smuzhiyun 	switch (entry->type) {
603*4882a593Smuzhiyun 	case MCLK_TYPE_PSC:
604*4882a593Smuzhiyun 		clks_idx_pub = MPC512x_CLK_PSC0_MCLK + idx;
605*4882a593Smuzhiyun 		clks_idx_int = MPC512x_CLK_MCLKS_FIRST
606*4882a593Smuzhiyun 			     + (idx) * MCLK_MAX_IDX;
607*4882a593Smuzhiyun 		mccr_reg = &clkregs->psc_ccr[idx];
608*4882a593Smuzhiyun 		break;
609*4882a593Smuzhiyun 	case MCLK_TYPE_MSCAN:
610*4882a593Smuzhiyun 		clks_idx_pub = MPC512x_CLK_MSCAN0_MCLK + idx;
611*4882a593Smuzhiyun 		clks_idx_int = MPC512x_CLK_MCLKS_FIRST
612*4882a593Smuzhiyun 			     + (NR_PSCS + idx) * MCLK_MAX_IDX;
613*4882a593Smuzhiyun 		mccr_reg = &clkregs->mscan_ccr[idx];
614*4882a593Smuzhiyun 		break;
615*4882a593Smuzhiyun 	case MCLK_TYPE_SPDIF:
616*4882a593Smuzhiyun 		clks_idx_pub = MPC512x_CLK_SPDIF_MCLK;
617*4882a593Smuzhiyun 		clks_idx_int = MPC512x_CLK_MCLKS_FIRST
618*4882a593Smuzhiyun 			     + (NR_PSCS + NR_MSCANS) * MCLK_MAX_IDX;
619*4882a593Smuzhiyun 		mccr_reg = &clkregs->spccr;
620*4882a593Smuzhiyun 		break;
621*4882a593Smuzhiyun 	case MCLK_TYPE_OUTCLK:
622*4882a593Smuzhiyun 		clks_idx_pub = MPC512x_CLK_OUT0_CLK + idx;
623*4882a593Smuzhiyun 		clks_idx_int = MPC512x_CLK_MCLKS_FIRST
624*4882a593Smuzhiyun 			     + (NR_PSCS + NR_MSCANS + NR_SPDIFS + idx)
625*4882a593Smuzhiyun 			     * MCLK_MAX_IDX;
626*4882a593Smuzhiyun 		mccr_reg = &clkregs->out_ccr[idx];
627*4882a593Smuzhiyun 		break;
628*4882a593Smuzhiyun 	default:
629*4882a593Smuzhiyun 		return;
630*4882a593Smuzhiyun 	}
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	/*
633*4882a593Smuzhiyun 	 * this was grabbed from the PPC_CLOCK implementation, which
634*4882a593Smuzhiyun 	 * enforced a specific MCLK divider while the clock was gated
635*4882a593Smuzhiyun 	 * during setup (that's a documented hardware requirement)
636*4882a593Smuzhiyun 	 *
637*4882a593Smuzhiyun 	 * the PPC_CLOCK implementation might even have violated the
638*4882a593Smuzhiyun 	 * "MCLK <= IPS" constraint, the fixed divider value of 1
639*4882a593Smuzhiyun 	 * results in a divider of 2 and thus MCLK = SYS/2 which equals
640*4882a593Smuzhiyun 	 * CSB which is greater than IPS; the serial port setup may have
641*4882a593Smuzhiyun 	 * adjusted the divider which the clock setup might have left in
642*4882a593Smuzhiyun 	 * an undesirable state
643*4882a593Smuzhiyun 	 *
644*4882a593Smuzhiyun 	 * initial setup is:
645*4882a593Smuzhiyun 	 * - MCLK 0 from SYS
646*4882a593Smuzhiyun 	 * - MCLK DIV such to not exceed the IPS clock
647*4882a593Smuzhiyun 	 * - MCLK 0 enabled
648*4882a593Smuzhiyun 	 * - MCLK 1 from MCLK DIV
649*4882a593Smuzhiyun 	 */
650*4882a593Smuzhiyun 	div = clk_get_rate(clks[MPC512x_CLK_SYS]);
651*4882a593Smuzhiyun 	div /= clk_get_rate(clks[MPC512x_CLK_IPS]);
652*4882a593Smuzhiyun 	out_be32(mccr_reg, (0 << 16));
653*4882a593Smuzhiyun 	out_be32(mccr_reg, (0 << 16) | ((div - 1) << 17));
654*4882a593Smuzhiyun 	out_be32(mccr_reg, (1 << 16) | ((div - 1) << 17));
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	/*
657*4882a593Smuzhiyun 	 * create the 'struct clk' items of the MCLK's clock subtree
658*4882a593Smuzhiyun 	 *
659*4882a593Smuzhiyun 	 * note that by design we always create all nodes and won't take
660*4882a593Smuzhiyun 	 * shortcuts here, because
661*4882a593Smuzhiyun 	 * - the "internal" MCLK_DIV and MCLK_OUT signal in turn are
662*4882a593Smuzhiyun 	 *   selectable inputs to the CFM while those who "actually use"
663*4882a593Smuzhiyun 	 *   the PSC/MSCAN/SPDIF (serial drivers et al) need the MCLK
664*4882a593Smuzhiyun 	 *   for their bitrate
665*4882a593Smuzhiyun 	 * - in the absence of "aliases" for clocks we need to create
666*4882a593Smuzhiyun 	 *   individial 'struct clk' items for whatever might get
667*4882a593Smuzhiyun 	 *   referenced or looked up, even if several of those items are
668*4882a593Smuzhiyun 	 *   identical from the logical POV (their rate value)
669*4882a593Smuzhiyun 	 * - for easier future maintenance and for better reflection of
670*4882a593Smuzhiyun 	 *   the SoC's documentation, it appears appropriate to generate
671*4882a593Smuzhiyun 	 *   clock items even for those muxers which actually are NOPs
672*4882a593Smuzhiyun 	 *   (those with two inputs of which one is reserved)
673*4882a593Smuzhiyun 	 */
674*4882a593Smuzhiyun 	clks[clks_idx_int + MCLK_IDX_MUX0] = mpc512x_clk_muxed(
675*4882a593Smuzhiyun 			entry->name_mux0,
676*4882a593Smuzhiyun 			soc_has_mclk_mux0_canin()
677*4882a593Smuzhiyun 				? &parent_names_mux0_canin[0]
678*4882a593Smuzhiyun 				: &parent_names_mux0_spdif[0],
679*4882a593Smuzhiyun 			ARRAY_SIZE(parent_names_mux0_spdif),
680*4882a593Smuzhiyun 			mccr_reg, 14, 2);
681*4882a593Smuzhiyun 	clks[clks_idx_int + MCLK_IDX_EN0] = mpc512x_clk_gated(
682*4882a593Smuzhiyun 			entry->name_en0, entry->name_mux0,
683*4882a593Smuzhiyun 			mccr_reg, 16);
684*4882a593Smuzhiyun 	clks[clks_idx_int + MCLK_IDX_DIV0] = mpc512x_clk_divider(
685*4882a593Smuzhiyun 			entry->name_div0,
686*4882a593Smuzhiyun 			entry->name_en0, CLK_SET_RATE_GATE,
687*4882a593Smuzhiyun 			mccr_reg, 17, 15, 0);
688*4882a593Smuzhiyun 	if (entry->has_mclk1) {
689*4882a593Smuzhiyun 		clks[clks_idx_pub] = mpc512x_clk_muxed(
690*4882a593Smuzhiyun 				entry->name_mclk,
691*4882a593Smuzhiyun 				&entry->parent_names_mux1[0],
692*4882a593Smuzhiyun 				ARRAY_SIZE(entry->parent_names_mux1),
693*4882a593Smuzhiyun 				mccr_reg, 7, 1);
694*4882a593Smuzhiyun 	} else {
695*4882a593Smuzhiyun 		clks[clks_idx_pub] = mpc512x_clk_factor(
696*4882a593Smuzhiyun 				entry->name_mclk,
697*4882a593Smuzhiyun 				entry->parent_names_mux1[0],
698*4882a593Smuzhiyun 				1, 1);
699*4882a593Smuzhiyun 	}
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun /* }}} MCLK helpers */
703*4882a593Smuzhiyun 
mpc512x_clk_setup_clock_tree(struct device_node * np,int busfreq)704*4882a593Smuzhiyun static void mpc512x_clk_setup_clock_tree(struct device_node *np, int busfreq)
705*4882a593Smuzhiyun {
706*4882a593Smuzhiyun 	int sys_mul, sys_div, ips_div;
707*4882a593Smuzhiyun 	int mul, div;
708*4882a593Smuzhiyun 	size_t mclk_idx;
709*4882a593Smuzhiyun 	int freq;
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	/*
712*4882a593Smuzhiyun 	 * developer's notes:
713*4882a593Smuzhiyun 	 * - consider whether to handle clocks which have both gates and
714*4882a593Smuzhiyun 	 *   dividers via intermediates or by means of composites
715*4882a593Smuzhiyun 	 * - fractional dividers appear to not map well to composites
716*4882a593Smuzhiyun 	 *   since they can be seen as a fixed multiplier and an
717*4882a593Smuzhiyun 	 *   adjustable divider, while composites can only combine at
718*4882a593Smuzhiyun 	 *   most one of a mux, div, and gate each into one 'struct clk'
719*4882a593Smuzhiyun 	 *   item
720*4882a593Smuzhiyun 	 * - PSC/MSCAN/SPDIF clock generation OTOH already is very
721*4882a593Smuzhiyun 	 *   specific and cannot get mapped to composites (at least not
722*4882a593Smuzhiyun 	 *   a single one, maybe two of them, but then some of these
723*4882a593Smuzhiyun 	 *   intermediate clock signals get referenced elsewhere (e.g.
724*4882a593Smuzhiyun 	 *   in the clock frequency measurement, CFM) and thus need
725*4882a593Smuzhiyun 	 *   publicly available names
726*4882a593Smuzhiyun 	 * - the current source layout appropriately reflects the
727*4882a593Smuzhiyun 	 *   hardware setup, and it works, so it's questionable whether
728*4882a593Smuzhiyun 	 *   further changes will result in big enough a benefit
729*4882a593Smuzhiyun 	 */
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	/* regardless of whether XTAL/OSC exists, have REF created */
732*4882a593Smuzhiyun 	mpc512x_clk_setup_ref_clock(np, busfreq, &sys_mul, &sys_div, &ips_div);
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	/* now setup the REF -> SYS -> CSB -> IPS hierarchy */
735*4882a593Smuzhiyun 	clks[MPC512x_CLK_SYS] = mpc512x_clk_factor("sys", "ref",
736*4882a593Smuzhiyun 						   sys_mul, sys_div);
737*4882a593Smuzhiyun 	clks[MPC512x_CLK_CSB] = mpc512x_clk_factor("csb", "sys", 1, 2);
738*4882a593Smuzhiyun 	clks[MPC512x_CLK_IPS] = mpc512x_clk_divtable("ips", "csb",
739*4882a593Smuzhiyun 						     &clkregs->scfr1, 23, 3,
740*4882a593Smuzhiyun 						     divtab_2346);
741*4882a593Smuzhiyun 	/* now setup anything below SYS and CSB and IPS */
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	clks[MPC512x_CLK_DDR_UG] = mpc512x_clk_factor("ddr-ug", "sys", 1, 2);
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	/*
746*4882a593Smuzhiyun 	 * the Reference Manual discusses that for SDHC only even divide
747*4882a593Smuzhiyun 	 * ratios are supported because clock domain synchronization
748*4882a593Smuzhiyun 	 * between 'per' and 'ipg' is broken;
749*4882a593Smuzhiyun 	 * keep the divider's bit 0 cleared (per reset value), and only
750*4882a593Smuzhiyun 	 * allow to setup the divider's bits 7:1, which results in that
751*4882a593Smuzhiyun 	 * only even divide ratios can get configured upon rate changes;
752*4882a593Smuzhiyun 	 * keep the "x4" name because this bit shift hack is an internal
753*4882a593Smuzhiyun 	 * implementation detail, the "fractional divider with quarters"
754*4882a593Smuzhiyun 	 * semantics remains
755*4882a593Smuzhiyun 	 */
756*4882a593Smuzhiyun 	clks[MPC512x_CLK_SDHC_x4] = mpc512x_clk_factor("sdhc-x4", "csb", 2, 1);
757*4882a593Smuzhiyun 	clks[MPC512x_CLK_SDHC_UG] = mpc512x_clk_divider("sdhc-ug", "sdhc-x4", 0,
758*4882a593Smuzhiyun 							&clkregs->scfr2, 1, 7,
759*4882a593Smuzhiyun 							CLK_DIVIDER_ONE_BASED);
760*4882a593Smuzhiyun 	if (soc_has_sdhc2()) {
761*4882a593Smuzhiyun 		clks[MPC512x_CLK_SDHC2_UG] = mpc512x_clk_divider(
762*4882a593Smuzhiyun 				"sdhc2-ug", "sdhc-x4", 0, &clkregs->scfr2,
763*4882a593Smuzhiyun 				9, 7, CLK_DIVIDER_ONE_BASED);
764*4882a593Smuzhiyun 	}
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun 	clks[MPC512x_CLK_DIU_x4] = mpc512x_clk_factor("diu-x4", "csb", 4, 1);
767*4882a593Smuzhiyun 	clks[MPC512x_CLK_DIU_UG] = mpc512x_clk_divider("diu-ug", "diu-x4", 0,
768*4882a593Smuzhiyun 						       &clkregs->scfr1, 0, 8,
769*4882a593Smuzhiyun 						       CLK_DIVIDER_ONE_BASED);
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	/*
772*4882a593Smuzhiyun 	 * the "power architecture PLL" was setup from data which was
773*4882a593Smuzhiyun 	 * sampled from the reset config word, at this point in time the
774*4882a593Smuzhiyun 	 * configuration can be considered fixed and read only (i.e. no
775*4882a593Smuzhiyun 	 * longer adjustable, or no longer in need of adjustment), which
776*4882a593Smuzhiyun 	 * is why we don't register a PLL here but assume fixed factors
777*4882a593Smuzhiyun 	 */
778*4882a593Smuzhiyun 	mul = get_cpmf_mult_x2();
779*4882a593Smuzhiyun 	div = 2;	/* compensate for the fractional factor */
780*4882a593Smuzhiyun 	clks[MPC512x_CLK_E300] = mpc512x_clk_factor("e300", "csb", mul, div);
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	if (soc_has_mbx()) {
783*4882a593Smuzhiyun 		clks[MPC512x_CLK_MBX_BUS_UG] = mpc512x_clk_factor(
784*4882a593Smuzhiyun 				"mbx-bus-ug", "csb", 1, 2);
785*4882a593Smuzhiyun 		clks[MPC512x_CLK_MBX_UG] = mpc512x_clk_divtable(
786*4882a593Smuzhiyun 				"mbx-ug", "mbx-bus-ug", &clkregs->scfr1,
787*4882a593Smuzhiyun 				14, 3, divtab_1234);
788*4882a593Smuzhiyun 		clks[MPC512x_CLK_MBX_3D_UG] = mpc512x_clk_factor(
789*4882a593Smuzhiyun 				"mbx-3d-ug", "mbx-ug", 1, 1);
790*4882a593Smuzhiyun 	}
791*4882a593Smuzhiyun 	if (soc_has_pci()) {
792*4882a593Smuzhiyun 		clks[MPC512x_CLK_PCI_UG] = mpc512x_clk_divtable(
793*4882a593Smuzhiyun 				"pci-ug", "csb", &clkregs->scfr1,
794*4882a593Smuzhiyun 				20, 3, divtab_2346);
795*4882a593Smuzhiyun 	}
796*4882a593Smuzhiyun 	if (soc_has_nfc_5125()) {
797*4882a593Smuzhiyun 		/*
798*4882a593Smuzhiyun 		 * XXX TODO implement 5125 NFC clock setup logic,
799*4882a593Smuzhiyun 		 * with high/low period counters in clkregs->scfr3,
800*4882a593Smuzhiyun 		 * currently there are no users so it's ENOIMPL
801*4882a593Smuzhiyun 		 */
802*4882a593Smuzhiyun 		clks[MPC512x_CLK_NFC_UG] = ERR_PTR(-ENOTSUPP);
803*4882a593Smuzhiyun 	} else {
804*4882a593Smuzhiyun 		clks[MPC512x_CLK_NFC_UG] = mpc512x_clk_divtable(
805*4882a593Smuzhiyun 				"nfc-ug", "ips", &clkregs->scfr1,
806*4882a593Smuzhiyun 				8, 3, divtab_1234);
807*4882a593Smuzhiyun 	}
808*4882a593Smuzhiyun 	clks[MPC512x_CLK_LPC_UG] = mpc512x_clk_divtable("lpc-ug", "ips",
809*4882a593Smuzhiyun 							&clkregs->scfr1, 11, 3,
810*4882a593Smuzhiyun 							divtab_1234);
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	clks[MPC512x_CLK_LPC] = mpc512x_clk_gated("lpc", "lpc-ug",
813*4882a593Smuzhiyun 						  &clkregs->sccr1, 30);
814*4882a593Smuzhiyun 	clks[MPC512x_CLK_NFC] = mpc512x_clk_gated("nfc", "nfc-ug",
815*4882a593Smuzhiyun 						  &clkregs->sccr1, 29);
816*4882a593Smuzhiyun 	if (soc_has_pata()) {
817*4882a593Smuzhiyun 		clks[MPC512x_CLK_PATA] = mpc512x_clk_gated(
818*4882a593Smuzhiyun 				"pata", "ips", &clkregs->sccr1, 28);
819*4882a593Smuzhiyun 	}
820*4882a593Smuzhiyun 	/* for PSCs there is a "registers" gate and a bitrate MCLK subtree */
821*4882a593Smuzhiyun 	for (mclk_idx = 0; mclk_idx < soc_max_pscnum(); mclk_idx++) {
822*4882a593Smuzhiyun 		char name[12];
823*4882a593Smuzhiyun 		snprintf(name, sizeof(name), "psc%d", mclk_idx);
824*4882a593Smuzhiyun 		clks[MPC512x_CLK_PSC0 + mclk_idx] = mpc512x_clk_gated(
825*4882a593Smuzhiyun 				name, "ips", &clkregs->sccr1, 27 - mclk_idx);
826*4882a593Smuzhiyun 		mpc512x_clk_setup_mclk(&mclk_psc_data[mclk_idx], mclk_idx);
827*4882a593Smuzhiyun 	}
828*4882a593Smuzhiyun 	clks[MPC512x_CLK_PSC_FIFO] = mpc512x_clk_gated("psc-fifo", "ips",
829*4882a593Smuzhiyun 						       &clkregs->sccr1, 15);
830*4882a593Smuzhiyun 	if (soc_has_sata()) {
831*4882a593Smuzhiyun 		clks[MPC512x_CLK_SATA] = mpc512x_clk_gated(
832*4882a593Smuzhiyun 				"sata", "ips", &clkregs->sccr1, 14);
833*4882a593Smuzhiyun 	}
834*4882a593Smuzhiyun 	clks[MPC512x_CLK_FEC] = mpc512x_clk_gated("fec", "ips",
835*4882a593Smuzhiyun 						  &clkregs->sccr1, 13);
836*4882a593Smuzhiyun 	if (soc_has_pci()) {
837*4882a593Smuzhiyun 		clks[MPC512x_CLK_PCI] = mpc512x_clk_gated(
838*4882a593Smuzhiyun 				"pci", "pci-ug", &clkregs->sccr1, 11);
839*4882a593Smuzhiyun 	}
840*4882a593Smuzhiyun 	clks[MPC512x_CLK_DDR] = mpc512x_clk_gated("ddr", "ddr-ug",
841*4882a593Smuzhiyun 						  &clkregs->sccr1, 10);
842*4882a593Smuzhiyun 	if (soc_has_fec2()) {
843*4882a593Smuzhiyun 		clks[MPC512x_CLK_FEC2] = mpc512x_clk_gated(
844*4882a593Smuzhiyun 				"fec2", "ips", &clkregs->sccr1, 9);
845*4882a593Smuzhiyun 	}
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	clks[MPC512x_CLK_DIU] = mpc512x_clk_gated("diu", "diu-ug",
848*4882a593Smuzhiyun 						  &clkregs->sccr2, 31);
849*4882a593Smuzhiyun 	if (soc_has_axe()) {
850*4882a593Smuzhiyun 		clks[MPC512x_CLK_AXE] = mpc512x_clk_gated(
851*4882a593Smuzhiyun 				"axe", "csb", &clkregs->sccr2, 30);
852*4882a593Smuzhiyun 	}
853*4882a593Smuzhiyun 	clks[MPC512x_CLK_MEM] = mpc512x_clk_gated("mem", "ips",
854*4882a593Smuzhiyun 						  &clkregs->sccr2, 29);
855*4882a593Smuzhiyun 	clks[MPC512x_CLK_USB1] = mpc512x_clk_gated("usb1", "csb",
856*4882a593Smuzhiyun 						   &clkregs->sccr2, 28);
857*4882a593Smuzhiyun 	clks[MPC512x_CLK_USB2] = mpc512x_clk_gated("usb2", "csb",
858*4882a593Smuzhiyun 						   &clkregs->sccr2, 27);
859*4882a593Smuzhiyun 	clks[MPC512x_CLK_I2C] = mpc512x_clk_gated("i2c", "ips",
860*4882a593Smuzhiyun 						  &clkregs->sccr2, 26);
861*4882a593Smuzhiyun 	/* MSCAN differs from PSC with just one gate for multiple components */
862*4882a593Smuzhiyun 	clks[MPC512x_CLK_BDLC] = mpc512x_clk_gated("bdlc", "ips",
863*4882a593Smuzhiyun 						   &clkregs->sccr2, 25);
864*4882a593Smuzhiyun 	for (mclk_idx = 0; mclk_idx < ARRAY_SIZE(mclk_mscan_data); mclk_idx++)
865*4882a593Smuzhiyun 		mpc512x_clk_setup_mclk(&mclk_mscan_data[mclk_idx], mclk_idx);
866*4882a593Smuzhiyun 	clks[MPC512x_CLK_SDHC] = mpc512x_clk_gated("sdhc", "sdhc-ug",
867*4882a593Smuzhiyun 						   &clkregs->sccr2, 24);
868*4882a593Smuzhiyun 	/* there is only one SPDIF component, which shares MCLK support code */
869*4882a593Smuzhiyun 	if (soc_has_spdif()) {
870*4882a593Smuzhiyun 		clks[MPC512x_CLK_SPDIF] = mpc512x_clk_gated(
871*4882a593Smuzhiyun 				"spdif", "ips", &clkregs->sccr2, 23);
872*4882a593Smuzhiyun 		mpc512x_clk_setup_mclk(&mclk_spdif_data[0], 0);
873*4882a593Smuzhiyun 	}
874*4882a593Smuzhiyun 	if (soc_has_mbx()) {
875*4882a593Smuzhiyun 		clks[MPC512x_CLK_MBX_BUS] = mpc512x_clk_gated(
876*4882a593Smuzhiyun 				"mbx-bus", "mbx-bus-ug", &clkregs->sccr2, 22);
877*4882a593Smuzhiyun 		clks[MPC512x_CLK_MBX] = mpc512x_clk_gated(
878*4882a593Smuzhiyun 				"mbx", "mbx-ug", &clkregs->sccr2, 21);
879*4882a593Smuzhiyun 		clks[MPC512x_CLK_MBX_3D] = mpc512x_clk_gated(
880*4882a593Smuzhiyun 				"mbx-3d", "mbx-3d-ug", &clkregs->sccr2, 20);
881*4882a593Smuzhiyun 	}
882*4882a593Smuzhiyun 	clks[MPC512x_CLK_IIM] = mpc512x_clk_gated("iim", "csb",
883*4882a593Smuzhiyun 						  &clkregs->sccr2, 19);
884*4882a593Smuzhiyun 	if (soc_has_viu()) {
885*4882a593Smuzhiyun 		clks[MPC512x_CLK_VIU] = mpc512x_clk_gated(
886*4882a593Smuzhiyun 				"viu", "csb", &clkregs->sccr2, 18);
887*4882a593Smuzhiyun 	}
888*4882a593Smuzhiyun 	if (soc_has_sdhc2()) {
889*4882a593Smuzhiyun 		clks[MPC512x_CLK_SDHC2] = mpc512x_clk_gated(
890*4882a593Smuzhiyun 				"sdhc-2", "sdhc2-ug", &clkregs->sccr2, 17);
891*4882a593Smuzhiyun 	}
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	if (soc_has_outclk()) {
894*4882a593Smuzhiyun 		size_t idx;	/* used as mclk_idx, just to trim line length */
895*4882a593Smuzhiyun 		for (idx = 0; idx < ARRAY_SIZE(mclk_outclk_data); idx++)
896*4882a593Smuzhiyun 			mpc512x_clk_setup_mclk(&mclk_outclk_data[idx], idx);
897*4882a593Smuzhiyun 	}
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 	/*
900*4882a593Smuzhiyun 	 * externally provided clocks (when implemented in hardware,
901*4882a593Smuzhiyun 	 * device tree may specify values which otherwise were unknown)
902*4882a593Smuzhiyun 	 */
903*4882a593Smuzhiyun 	freq = get_freq_from_dt("psc_mclk_in");
904*4882a593Smuzhiyun 	if (!freq)
905*4882a593Smuzhiyun 		freq = 25000000;
906*4882a593Smuzhiyun 	clks[MPC512x_CLK_PSC_MCLK_IN] = mpc512x_clk_fixed("psc_mclk_in", freq);
907*4882a593Smuzhiyun 	if (soc_has_mclk_mux0_canin()) {
908*4882a593Smuzhiyun 		freq = get_freq_from_dt("can_clk_in");
909*4882a593Smuzhiyun 		clks[MPC512x_CLK_CAN_CLK_IN] = mpc512x_clk_fixed(
910*4882a593Smuzhiyun 				"can_clk_in", freq);
911*4882a593Smuzhiyun 	} else {
912*4882a593Smuzhiyun 		freq = get_freq_from_dt("spdif_tx_in");
913*4882a593Smuzhiyun 		clks[MPC512x_CLK_SPDIF_TX_IN] = mpc512x_clk_fixed(
914*4882a593Smuzhiyun 				"spdif_tx_in", freq);
915*4882a593Smuzhiyun 		freq = get_freq_from_dt("spdif_rx_in");
916*4882a593Smuzhiyun 		clks[MPC512x_CLK_SPDIF_TX_IN] = mpc512x_clk_fixed(
917*4882a593Smuzhiyun 				"spdif_rx_in", freq);
918*4882a593Smuzhiyun 	}
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	/* fixed frequency for AC97, always 24.567MHz */
921*4882a593Smuzhiyun 	clks[MPC512x_CLK_AC97] = mpc512x_clk_fixed("ac97", 24567000);
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	/*
924*4882a593Smuzhiyun 	 * pre-enable those "internal" clock items which never get
925*4882a593Smuzhiyun 	 * claimed by any peripheral driver, to not have the clock
926*4882a593Smuzhiyun 	 * subsystem disable them late at startup
927*4882a593Smuzhiyun 	 */
928*4882a593Smuzhiyun 	clk_prepare_enable(clks[MPC512x_CLK_DUMMY]);
929*4882a593Smuzhiyun 	clk_prepare_enable(clks[MPC512x_CLK_E300]);	/* PowerPC CPU */
930*4882a593Smuzhiyun 	clk_prepare_enable(clks[MPC512x_CLK_DDR]);	/* DRAM */
931*4882a593Smuzhiyun 	clk_prepare_enable(clks[MPC512x_CLK_MEM]);	/* SRAM */
932*4882a593Smuzhiyun 	clk_prepare_enable(clks[MPC512x_CLK_IPS]);	/* SoC periph */
933*4882a593Smuzhiyun 	clk_prepare_enable(clks[MPC512x_CLK_LPC]);	/* boot media */
934*4882a593Smuzhiyun }
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun /*
937*4882a593Smuzhiyun  * registers the set of public clocks (those listed in the dt-bindings/
938*4882a593Smuzhiyun  * header file) for OF lookups, keeps the intermediates private to us
939*4882a593Smuzhiyun  */
mpc5121_clk_register_of_provider(struct device_node * np)940*4882a593Smuzhiyun static void mpc5121_clk_register_of_provider(struct device_node *np)
941*4882a593Smuzhiyun {
942*4882a593Smuzhiyun 	clk_data.clks = clks;
943*4882a593Smuzhiyun 	clk_data.clk_num = MPC512x_CLK_LAST_PUBLIC + 1;	/* _not_ ARRAY_SIZE() */
944*4882a593Smuzhiyun 	of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
945*4882a593Smuzhiyun }
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun /*
948*4882a593Smuzhiyun  * temporary support for the period of time between introduction of CCF
949*4882a593Smuzhiyun  * support and the adjustment of peripheral drivers to OF based lookups
950*4882a593Smuzhiyun  */
mpc5121_clk_provide_migration_support(void)951*4882a593Smuzhiyun static void mpc5121_clk_provide_migration_support(void)
952*4882a593Smuzhiyun {
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 	/*
955*4882a593Smuzhiyun 	 * pre-enable those clock items which are not yet appropriately
956*4882a593Smuzhiyun 	 * acquired by their peripheral driver
957*4882a593Smuzhiyun 	 *
958*4882a593Smuzhiyun 	 * the PCI clock cannot get acquired by its peripheral driver,
959*4882a593Smuzhiyun 	 * because for this platform the driver won't probe(), instead
960*4882a593Smuzhiyun 	 * initialization is done from within the .setup_arch() routine
961*4882a593Smuzhiyun 	 * at a point in time where the clock provider has not been
962*4882a593Smuzhiyun 	 * setup yet and thus isn't available yet
963*4882a593Smuzhiyun 	 *
964*4882a593Smuzhiyun 	 * so we "pre-enable" the clock here, to not have the clock
965*4882a593Smuzhiyun 	 * subsystem automatically disable this item in a late init call
966*4882a593Smuzhiyun 	 *
967*4882a593Smuzhiyun 	 * this PCI clock pre-enable workaround only applies when there
968*4882a593Smuzhiyun 	 * are device tree nodes for PCI and thus the peripheral driver
969*4882a593Smuzhiyun 	 * has attached to bridges, otherwise the PCI clock remains
970*4882a593Smuzhiyun 	 * unused and so it gets disabled
971*4882a593Smuzhiyun 	 */
972*4882a593Smuzhiyun 	clk_prepare_enable(clks[MPC512x_CLK_PSC3_MCLK]);/* serial console */
973*4882a593Smuzhiyun 	if (of_find_compatible_node(NULL, "pci", "fsl,mpc5121-pci"))
974*4882a593Smuzhiyun 		clk_prepare_enable(clks[MPC512x_CLK_PCI]);
975*4882a593Smuzhiyun }
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun /*
978*4882a593Smuzhiyun  * those macros are not exactly pretty, but they encapsulate a lot
979*4882a593Smuzhiyun  * of copy'n'paste heavy code which is even more ugly, and reduce
980*4882a593Smuzhiyun  * the potential for inconsistencies in those many code copies
981*4882a593Smuzhiyun  */
982*4882a593Smuzhiyun #define FOR_NODES(compatname) \
983*4882a593Smuzhiyun 	for_each_compatible_node(np, NULL, compatname)
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun #define NODE_PREP do { \
986*4882a593Smuzhiyun 	of_address_to_resource(np, 0, &res); \
987*4882a593Smuzhiyun 	snprintf(devname, sizeof(devname), "%08x.%s", res.start, np->name); \
988*4882a593Smuzhiyun } while (0)
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun #define NODE_CHK(clkname, clkitem, regnode, regflag) do { \
991*4882a593Smuzhiyun 	struct clk *clk; \
992*4882a593Smuzhiyun 	clk = of_clk_get_by_name(np, clkname); \
993*4882a593Smuzhiyun 	if (IS_ERR(clk)) { \
994*4882a593Smuzhiyun 		clk = clkitem; \
995*4882a593Smuzhiyun 		clk_register_clkdev(clk, clkname, devname); \
996*4882a593Smuzhiyun 		if (regnode) \
997*4882a593Smuzhiyun 			clk_register_clkdev(clk, clkname, np->name); \
998*4882a593Smuzhiyun 		did_register |= DID_REG_ ## regflag; \
999*4882a593Smuzhiyun 		pr_debug("clock alias name '%s' for dev '%s' pointer %p\n", \
1000*4882a593Smuzhiyun 			 clkname, devname, clk); \
1001*4882a593Smuzhiyun 	} else { \
1002*4882a593Smuzhiyun 		clk_put(clk); \
1003*4882a593Smuzhiyun 	} \
1004*4882a593Smuzhiyun } while (0)
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun /*
1007*4882a593Smuzhiyun  * register source code provided fallback results for clock lookups,
1008*4882a593Smuzhiyun  * these get consulted when OF based clock lookup fails (that is in the
1009*4882a593Smuzhiyun  * case of not yet adjusted device tree data, where clock related specs
1010*4882a593Smuzhiyun  * are missing)
1011*4882a593Smuzhiyun  */
mpc5121_clk_provide_backwards_compat(void)1012*4882a593Smuzhiyun static void mpc5121_clk_provide_backwards_compat(void)
1013*4882a593Smuzhiyun {
1014*4882a593Smuzhiyun 	enum did_reg_flags {
1015*4882a593Smuzhiyun 		DID_REG_PSC	= BIT(0),
1016*4882a593Smuzhiyun 		DID_REG_PSCFIFO	= BIT(1),
1017*4882a593Smuzhiyun 		DID_REG_NFC	= BIT(2),
1018*4882a593Smuzhiyun 		DID_REG_CAN	= BIT(3),
1019*4882a593Smuzhiyun 		DID_REG_I2C	= BIT(4),
1020*4882a593Smuzhiyun 		DID_REG_DIU	= BIT(5),
1021*4882a593Smuzhiyun 		DID_REG_VIU	= BIT(6),
1022*4882a593Smuzhiyun 		DID_REG_FEC	= BIT(7),
1023*4882a593Smuzhiyun 		DID_REG_USB	= BIT(8),
1024*4882a593Smuzhiyun 		DID_REG_PATA	= BIT(9),
1025*4882a593Smuzhiyun 	};
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 	int did_register;
1028*4882a593Smuzhiyun 	struct device_node *np;
1029*4882a593Smuzhiyun 	struct resource res;
1030*4882a593Smuzhiyun 	int idx;
1031*4882a593Smuzhiyun 	char devname[32];
1032*4882a593Smuzhiyun 
1033*4882a593Smuzhiyun 	did_register = 0;
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun 	FOR_NODES(mpc512x_select_psc_compat()) {
1036*4882a593Smuzhiyun 		NODE_PREP;
1037*4882a593Smuzhiyun 		idx = (res.start >> 8) & 0xf;
1038*4882a593Smuzhiyun 		NODE_CHK("ipg", clks[MPC512x_CLK_PSC0 + idx], 0, PSC);
1039*4882a593Smuzhiyun 		NODE_CHK("mclk", clks[MPC512x_CLK_PSC0_MCLK + idx], 0, PSC);
1040*4882a593Smuzhiyun 	}
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 	FOR_NODES("fsl,mpc5121-psc-fifo") {
1043*4882a593Smuzhiyun 		NODE_PREP;
1044*4882a593Smuzhiyun 		NODE_CHK("ipg", clks[MPC512x_CLK_PSC_FIFO], 1, PSCFIFO);
1045*4882a593Smuzhiyun 	}
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun 	FOR_NODES("fsl,mpc5121-nfc") {
1048*4882a593Smuzhiyun 		NODE_PREP;
1049*4882a593Smuzhiyun 		NODE_CHK("ipg", clks[MPC512x_CLK_NFC], 0, NFC);
1050*4882a593Smuzhiyun 	}
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	FOR_NODES("fsl,mpc5121-mscan") {
1053*4882a593Smuzhiyun 		NODE_PREP;
1054*4882a593Smuzhiyun 		idx = 0;
1055*4882a593Smuzhiyun 		idx += (res.start & 0x2000) ? 2 : 0;
1056*4882a593Smuzhiyun 		idx += (res.start & 0x0080) ? 1 : 0;
1057*4882a593Smuzhiyun 		NODE_CHK("ipg", clks[MPC512x_CLK_BDLC], 0, CAN);
1058*4882a593Smuzhiyun 		NODE_CHK("mclk", clks[MPC512x_CLK_MSCAN0_MCLK + idx], 0, CAN);
1059*4882a593Smuzhiyun 	}
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 	/*
1062*4882a593Smuzhiyun 	 * do register the 'ips', 'sys', and 'ref' names globally
1063*4882a593Smuzhiyun 	 * instead of inside each individual CAN node, as there is no
1064*4882a593Smuzhiyun 	 * potential for a name conflict (in contrast to 'ipg' and 'mclk')
1065*4882a593Smuzhiyun 	 */
1066*4882a593Smuzhiyun 	if (did_register & DID_REG_CAN) {
1067*4882a593Smuzhiyun 		clk_register_clkdev(clks[MPC512x_CLK_IPS], "ips", NULL);
1068*4882a593Smuzhiyun 		clk_register_clkdev(clks[MPC512x_CLK_SYS], "sys", NULL);
1069*4882a593Smuzhiyun 		clk_register_clkdev(clks[MPC512x_CLK_REF], "ref", NULL);
1070*4882a593Smuzhiyun 	}
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	FOR_NODES("fsl,mpc5121-i2c") {
1073*4882a593Smuzhiyun 		NODE_PREP;
1074*4882a593Smuzhiyun 		NODE_CHK("ipg", clks[MPC512x_CLK_I2C], 0, I2C);
1075*4882a593Smuzhiyun 	}
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun 	/*
1078*4882a593Smuzhiyun 	 * workaround for the fact that the I2C driver does an "anonymous"
1079*4882a593Smuzhiyun 	 * lookup (NULL name spec, which yields the first clock spec) for
1080*4882a593Smuzhiyun 	 * which we cannot register an alias -- a _global_ 'ipg' alias that
1081*4882a593Smuzhiyun 	 * is not bound to any device name and returns the I2C clock item
1082*4882a593Smuzhiyun 	 * is not a good idea
1083*4882a593Smuzhiyun 	 *
1084*4882a593Smuzhiyun 	 * so we have the lookup in the peripheral driver fail, which is
1085*4882a593Smuzhiyun 	 * silent and non-fatal, and pre-enable the clock item here such
1086*4882a593Smuzhiyun 	 * that register access is possible
1087*4882a593Smuzhiyun 	 *
1088*4882a593Smuzhiyun 	 * see commit b3bfce2b "i2c: mpc: cleanup clock API use" for
1089*4882a593Smuzhiyun 	 * details, adjusting s/NULL/"ipg"/ in i2c-mpc.c would make this
1090*4882a593Smuzhiyun 	 * workaround obsolete
1091*4882a593Smuzhiyun 	 */
1092*4882a593Smuzhiyun 	if (did_register & DID_REG_I2C)
1093*4882a593Smuzhiyun 		clk_prepare_enable(clks[MPC512x_CLK_I2C]);
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 	FOR_NODES("fsl,mpc5121-diu") {
1096*4882a593Smuzhiyun 		NODE_PREP;
1097*4882a593Smuzhiyun 		NODE_CHK("ipg", clks[MPC512x_CLK_DIU], 1, DIU);
1098*4882a593Smuzhiyun 	}
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	FOR_NODES("fsl,mpc5121-viu") {
1101*4882a593Smuzhiyun 		NODE_PREP;
1102*4882a593Smuzhiyun 		NODE_CHK("ipg", clks[MPC512x_CLK_VIU], 0, VIU);
1103*4882a593Smuzhiyun 	}
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun 	/*
1106*4882a593Smuzhiyun 	 * note that 2771399a "fs_enet: cleanup clock API use" did use the
1107*4882a593Smuzhiyun 	 * "per" string for the clock lookup in contrast to the "ipg" name
1108*4882a593Smuzhiyun 	 * which most other nodes are using -- this is not a fatal thing
1109*4882a593Smuzhiyun 	 * but just something to keep in mind when doing compatibility
1110*4882a593Smuzhiyun 	 * registration, it's a non-issue with up-to-date device tree data
1111*4882a593Smuzhiyun 	 */
1112*4882a593Smuzhiyun 	FOR_NODES("fsl,mpc5121-fec") {
1113*4882a593Smuzhiyun 		NODE_PREP;
1114*4882a593Smuzhiyun 		NODE_CHK("per", clks[MPC512x_CLK_FEC], 0, FEC);
1115*4882a593Smuzhiyun 	}
1116*4882a593Smuzhiyun 	FOR_NODES("fsl,mpc5121-fec-mdio") {
1117*4882a593Smuzhiyun 		NODE_PREP;
1118*4882a593Smuzhiyun 		NODE_CHK("per", clks[MPC512x_CLK_FEC], 0, FEC);
1119*4882a593Smuzhiyun 	}
1120*4882a593Smuzhiyun 	/*
1121*4882a593Smuzhiyun 	 * MPC5125 has two FECs: FEC1 at 0x2800, FEC2 at 0x4800;
1122*4882a593Smuzhiyun 	 * the clock items don't "form an array" since FEC2 was
1123*4882a593Smuzhiyun 	 * added only later and was not allowed to shift all other
1124*4882a593Smuzhiyun 	 * clock item indices, so the numbers aren't adjacent
1125*4882a593Smuzhiyun 	 */
1126*4882a593Smuzhiyun 	FOR_NODES("fsl,mpc5125-fec") {
1127*4882a593Smuzhiyun 		NODE_PREP;
1128*4882a593Smuzhiyun 		if (res.start & 0x4000)
1129*4882a593Smuzhiyun 			idx = MPC512x_CLK_FEC2;
1130*4882a593Smuzhiyun 		else
1131*4882a593Smuzhiyun 			idx = MPC512x_CLK_FEC;
1132*4882a593Smuzhiyun 		NODE_CHK("per", clks[idx], 0, FEC);
1133*4882a593Smuzhiyun 	}
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun 	FOR_NODES("fsl,mpc5121-usb2-dr") {
1136*4882a593Smuzhiyun 		NODE_PREP;
1137*4882a593Smuzhiyun 		idx = (res.start & 0x4000) ? 1 : 0;
1138*4882a593Smuzhiyun 		NODE_CHK("ipg", clks[MPC512x_CLK_USB1 + idx], 0, USB);
1139*4882a593Smuzhiyun 	}
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun 	FOR_NODES("fsl,mpc5121-pata") {
1142*4882a593Smuzhiyun 		NODE_PREP;
1143*4882a593Smuzhiyun 		NODE_CHK("ipg", clks[MPC512x_CLK_PATA], 0, PATA);
1144*4882a593Smuzhiyun 	}
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	/*
1147*4882a593Smuzhiyun 	 * try to collapse diagnostics into a single line of output yet
1148*4882a593Smuzhiyun 	 * provide a full list of what is missing, to avoid noise in the
1149*4882a593Smuzhiyun 	 * absence of up-to-date device tree data -- backwards
1150*4882a593Smuzhiyun 	 * compatibility to old DTBs is a requirement, updates may be
1151*4882a593Smuzhiyun 	 * desirable or preferrable but are not at all mandatory
1152*4882a593Smuzhiyun 	 */
1153*4882a593Smuzhiyun 	if (did_register) {
1154*4882a593Smuzhiyun 		pr_notice("device tree lacks clock specs, adding fallbacks (0x%x,%s%s%s%s%s%s%s%s%s%s)\n",
1155*4882a593Smuzhiyun 			  did_register,
1156*4882a593Smuzhiyun 			  (did_register & DID_REG_PSC) ? " PSC" : "",
1157*4882a593Smuzhiyun 			  (did_register & DID_REG_PSCFIFO) ? " PSCFIFO" : "",
1158*4882a593Smuzhiyun 			  (did_register & DID_REG_NFC) ? " NFC" : "",
1159*4882a593Smuzhiyun 			  (did_register & DID_REG_CAN) ? " CAN" : "",
1160*4882a593Smuzhiyun 			  (did_register & DID_REG_I2C) ? " I2C" : "",
1161*4882a593Smuzhiyun 			  (did_register & DID_REG_DIU) ? " DIU" : "",
1162*4882a593Smuzhiyun 			  (did_register & DID_REG_VIU) ? " VIU" : "",
1163*4882a593Smuzhiyun 			  (did_register & DID_REG_FEC) ? " FEC" : "",
1164*4882a593Smuzhiyun 			  (did_register & DID_REG_USB) ? " USB" : "",
1165*4882a593Smuzhiyun 			  (did_register & DID_REG_PATA) ? " PATA" : "");
1166*4882a593Smuzhiyun 	} else {
1167*4882a593Smuzhiyun 		pr_debug("device tree has clock specs, no fallbacks added\n");
1168*4882a593Smuzhiyun 	}
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun /*
1172*4882a593Smuzhiyun  * The "fixed-clock" nodes (which includes the oscillator node if the board's
1173*4882a593Smuzhiyun  * DT provides one) has already been scanned by the of_clk_init() in
1174*4882a593Smuzhiyun  * time_init().
1175*4882a593Smuzhiyun  */
mpc5121_clk_init(void)1176*4882a593Smuzhiyun int __init mpc5121_clk_init(void)
1177*4882a593Smuzhiyun {
1178*4882a593Smuzhiyun 	struct device_node *clk_np;
1179*4882a593Smuzhiyun 	int busfreq;
1180*4882a593Smuzhiyun 
1181*4882a593Smuzhiyun 	/* map the clock control registers */
1182*4882a593Smuzhiyun 	clk_np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-clock");
1183*4882a593Smuzhiyun 	if (!clk_np)
1184*4882a593Smuzhiyun 		return -ENODEV;
1185*4882a593Smuzhiyun 	clkregs = of_iomap(clk_np, 0);
1186*4882a593Smuzhiyun 	WARN_ON(!clkregs);
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	/* determine the SoC variant we run on */
1189*4882a593Smuzhiyun 	mpc512x_clk_determine_soc();
1190*4882a593Smuzhiyun 
1191*4882a593Smuzhiyun 	/* invalidate all not yet registered clock slots */
1192*4882a593Smuzhiyun 	mpc512x_clk_preset_data();
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun 	/*
1195*4882a593Smuzhiyun 	 * add a dummy clock for those situations where a clock spec is
1196*4882a593Smuzhiyun 	 * required yet no real clock is involved
1197*4882a593Smuzhiyun 	 */
1198*4882a593Smuzhiyun 	clks[MPC512x_CLK_DUMMY] = mpc512x_clk_fixed("dummy", 0);
1199*4882a593Smuzhiyun 
1200*4882a593Smuzhiyun 	/*
1201*4882a593Smuzhiyun 	 * have all the real nodes in the clock tree populated from REF
1202*4882a593Smuzhiyun 	 * down to all leaves, either starting from the OSC node or from
1203*4882a593Smuzhiyun 	 * a REF root that was created from the IPS bus clock input
1204*4882a593Smuzhiyun 	 */
1205*4882a593Smuzhiyun 	busfreq = get_freq_from_dt("bus-frequency");
1206*4882a593Smuzhiyun 	mpc512x_clk_setup_clock_tree(clk_np, busfreq);
1207*4882a593Smuzhiyun 
1208*4882a593Smuzhiyun 	/* register as an OF clock provider */
1209*4882a593Smuzhiyun 	mpc5121_clk_register_of_provider(clk_np);
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun 	/*
1212*4882a593Smuzhiyun 	 * unbreak not yet adjusted peripheral drivers during migration
1213*4882a593Smuzhiyun 	 * towards fully operational common clock support, and allow
1214*4882a593Smuzhiyun 	 * operation in the absence of clock related device tree specs
1215*4882a593Smuzhiyun 	 */
1216*4882a593Smuzhiyun 	mpc5121_clk_provide_migration_support();
1217*4882a593Smuzhiyun 	mpc5121_clk_provide_backwards_compat();
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 	return 0;
1220*4882a593Smuzhiyun }
1221