xref: /openbmc/u-boot/arch/arm/mach-imx/mx7ulp/pcc.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2552a848eSStefano Babic /*
3552a848eSStefano Babic  * Copyright (C) 2016 Freescale Semiconductor, Inc.
4552a848eSStefano Babic  */
5552a848eSStefano Babic 
6552a848eSStefano Babic #include <common.h>
7552a848eSStefano Babic #include <div64.h>
8552a848eSStefano Babic #include <asm/io.h>
9552a848eSStefano Babic #include <errno.h>
10552a848eSStefano Babic #include <asm/arch/imx-regs.h>
11552a848eSStefano Babic #include <asm/arch/pcc.h>
12552a848eSStefano Babic #include <asm/arch/sys_proto.h>
13552a848eSStefano Babic 
14552a848eSStefano Babic #define PCC_CLKSRC_TYPES 2
15552a848eSStefano Babic #define PCC_CLKSRC_NUM 7
16552a848eSStefano Babic 
17552a848eSStefano Babic static enum scg_clk pcc_clksrc[PCC_CLKSRC_TYPES][PCC_CLKSRC_NUM] = {
18552a848eSStefano Babic 	{	SCG_NIC1_BUS_CLK,
19552a848eSStefano Babic 		SCG_NIC1_CLK,
20552a848eSStefano Babic 		SCG_DDR_CLK,
21552a848eSStefano Babic 		SCG_APLL_PFD2_CLK,
22552a848eSStefano Babic 		SCG_APLL_PFD1_CLK,
23552a848eSStefano Babic 		SCG_APLL_PFD0_CLK,
24552a848eSStefano Babic 		USB_PLL_OUT,
25552a848eSStefano Babic 	},
26552a848eSStefano Babic 	{	SCG_SOSC_DIV2_CLK,  /* SOSC BUS clock */
27552a848eSStefano Babic 		MIPI_PLL_OUT,
28552a848eSStefano Babic 		SCG_FIRC_DIV2_CLK,  /* FIRC BUS clock */
29552a848eSStefano Babic 		SCG_ROSC_CLK,
30552a848eSStefano Babic 		SCG_NIC1_BUS_CLK,
31552a848eSStefano Babic 		SCG_NIC1_CLK,
32552a848eSStefano Babic 		SCG_APLL_PFD3_CLK,
33552a848eSStefano Babic 	},
34552a848eSStefano Babic };
35552a848eSStefano Babic 
36552a848eSStefano Babic static struct pcc_entry pcc_arrays[] = {
37552a848eSStefano Babic 	{PCC2_RBASE, DMA1_PCC2_SLOT,		CLKSRC_NO_PCS, PCC_NO_DIV},
38552a848eSStefano Babic 	{PCC2_RBASE, RGPIO1_PCC2_SLOT,		CLKSRC_NO_PCS, PCC_NO_DIV},
39552a848eSStefano Babic 	{PCC2_RBASE, FLEXBUS0_PCC2_SLOT,	CLKSRC_NO_PCS, PCC_NO_DIV},
40552a848eSStefano Babic 	{PCC2_RBASE, SEMA42_1_PCC2_SLOT,	CLKSRC_NO_PCS, PCC_NO_DIV},
41552a848eSStefano Babic 	{PCC2_RBASE, DMA1_CH_MUX0_PCC2_SLOT,	CLKSRC_NO_PCS, PCC_NO_DIV},
42552a848eSStefano Babic 	{PCC2_RBASE, SNVS_PCC2_SLOT,		CLKSRC_NO_PCS, PCC_NO_DIV},
43552a848eSStefano Babic 	{PCC2_RBASE, CAAM_PCC2_SLOT,		CLKSRC_NO_PCS, PCC_NO_DIV},
44552a848eSStefano Babic 	{PCC2_RBASE, LPTPM4_PCC2_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
45552a848eSStefano Babic 	{PCC2_RBASE, LPTPM5_PCC2_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
46552a848eSStefano Babic 	{PCC2_RBASE, LPIT1_PCC2_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
47552a848eSStefano Babic 	{PCC2_RBASE, LPSPI2_PCC2_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
48552a848eSStefano Babic 	{PCC2_RBASE, LPSPI3_PCC2_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
49552a848eSStefano Babic 	{PCC2_RBASE, LPI2C4_PCC2_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
50552a848eSStefano Babic 	{PCC2_RBASE, LPI2C5_PCC2_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
51552a848eSStefano Babic 	{PCC2_RBASE, LPUART4_PCC2_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
52552a848eSStefano Babic 	{PCC2_RBASE, LPUART5_PCC2_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
53552a848eSStefano Babic 	{PCC2_RBASE, FLEXIO1_PCC2_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
54552a848eSStefano Babic 	{PCC2_RBASE, USBOTG0_PCC2_SLOT,		CLKSRC_PER_PLAT, PCC_HAS_DIV},
55552a848eSStefano Babic 	{PCC2_RBASE, USBOTG1_PCC2_SLOT,		CLKSRC_PER_PLAT, PCC_HAS_DIV},
56552a848eSStefano Babic 	{PCC2_RBASE, USBPHY_PCC2_SLOT,		CLKSRC_NO_PCS, PCC_NO_DIV},
57552a848eSStefano Babic 	{PCC2_RBASE, USB_PL301_PCC2_SLOT,	CLKSRC_NO_PCS, PCC_NO_DIV},
58552a848eSStefano Babic 	{PCC2_RBASE, USDHC0_PCC2_SLOT,		CLKSRC_PER_PLAT, PCC_HAS_DIV},
59552a848eSStefano Babic 	{PCC2_RBASE, USDHC1_PCC2_SLOT,		CLKSRC_PER_PLAT, PCC_HAS_DIV},
60552a848eSStefano Babic 	{PCC2_RBASE, WDG1_PCC2_SLOT,		CLKSRC_PER_BUS,	PCC_HAS_DIV},
61552a848eSStefano Babic 	{PCC2_RBASE, WDG2_PCC2_SLOT,		CLKSRC_PER_BUS, PCC_HAS_DIV},
62552a848eSStefano Babic 
63552a848eSStefano Babic 	{PCC3_RBASE, LPTPM6_PCC3_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
64552a848eSStefano Babic 	{PCC3_RBASE, LPTPM7_PCC3_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
65552a848eSStefano Babic 	{PCC3_RBASE, LPI2C6_PCC3_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
66552a848eSStefano Babic 	{PCC3_RBASE, LPI2C7_PCC3_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
67552a848eSStefano Babic 	{PCC3_RBASE, LPUART6_PCC3_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
68552a848eSStefano Babic 	{PCC3_RBASE, LPUART7_PCC3_SLOT,		CLKSRC_PER_BUS, PCC_NO_DIV},
69552a848eSStefano Babic 	{PCC3_RBASE, VIU0_PCC3_SLOT,		CLKSRC_NO_PCS, PCC_NO_DIV},
70552a848eSStefano Babic 	{PCC3_RBASE, DSI0_PCC3_SLOT,		CLKSRC_PER_BUS, PCC_HAS_DIV},
71552a848eSStefano Babic 	{PCC3_RBASE, LCDIF0_PCC3_SLOT,		CLKSRC_PER_PLAT, PCC_HAS_DIV},
72552a848eSStefano Babic 	{PCC3_RBASE, MMDC0_PCC3_SLOT,		CLKSRC_NO_PCS, PCC_NO_DIV},
73552a848eSStefano Babic 	{PCC3_RBASE, PORTC_PCC3_SLOT,		CLKSRC_NO_PCS, PCC_NO_DIV},
74552a848eSStefano Babic 	{PCC3_RBASE, PORTD_PCC3_SLOT,		CLKSRC_NO_PCS, PCC_NO_DIV},
75552a848eSStefano Babic 	{PCC3_RBASE, PORTE_PCC3_SLOT,		CLKSRC_NO_PCS, PCC_NO_DIV},
76552a848eSStefano Babic 	{PCC3_RBASE, PORTF_PCC3_SLOT,		CLKSRC_NO_PCS, PCC_NO_DIV},
77552a848eSStefano Babic 	{PCC3_RBASE, GPU3D_PCC3_SLOT,		CLKSRC_PER_PLAT, PCC_NO_DIV},
78552a848eSStefano Babic 	{PCC3_RBASE, GPU2D_PCC3_SLOT,		CLKSRC_PER_PLAT, PCC_NO_DIV},
79552a848eSStefano Babic };
80552a848eSStefano Babic 
pcc_clock_enable(enum pcc_clk clk,bool enable)81552a848eSStefano Babic int pcc_clock_enable(enum pcc_clk clk, bool enable)
82552a848eSStefano Babic {
83552a848eSStefano Babic 	u32 reg, val;
84552a848eSStefano Babic 
85552a848eSStefano Babic 	if (clk >= ARRAY_SIZE(pcc_arrays))
86552a848eSStefano Babic 		return -EINVAL;
87552a848eSStefano Babic 
88552a848eSStefano Babic 	reg = pcc_arrays[clk].pcc_base + pcc_arrays[clk].pcc_slot * 4;
89552a848eSStefano Babic 
90552a848eSStefano Babic 	val = readl(reg);
91552a848eSStefano Babic 
92552a848eSStefano Babic 	clk_debug("pcc_clock_enable: clk %d, reg 0x%x, val 0x%x, enable %d\n",
93552a848eSStefano Babic 		  clk, reg, val, enable);
94552a848eSStefano Babic 
95552a848eSStefano Babic 	if (!(val & PCC_PR_MASK) || (val & PCC_INUSE_MASK))
96552a848eSStefano Babic 		return -EPERM;
97552a848eSStefano Babic 
98552a848eSStefano Babic 	if (enable)
99552a848eSStefano Babic 		val |= PCC_CGC_MASK;
100552a848eSStefano Babic 	else
101552a848eSStefano Babic 		val &= ~PCC_CGC_MASK;
102552a848eSStefano Babic 
103552a848eSStefano Babic 	writel(val, reg);
104552a848eSStefano Babic 
105552a848eSStefano Babic 	clk_debug("pcc_clock_enable: val 0x%x\n", val);
106552a848eSStefano Babic 
107552a848eSStefano Babic 	return 0;
108552a848eSStefano Babic }
109552a848eSStefano Babic 
110552a848eSStefano Babic /* The clock source select needs clock is disabled */
pcc_clock_sel(enum pcc_clk clk,enum scg_clk src)111552a848eSStefano Babic int pcc_clock_sel(enum pcc_clk clk, enum scg_clk src)
112552a848eSStefano Babic {
113552a848eSStefano Babic 	u32 reg, val, i, clksrc_type;
114552a848eSStefano Babic 
115552a848eSStefano Babic 	if (clk >= ARRAY_SIZE(pcc_arrays))
116552a848eSStefano Babic 		return -EINVAL;
117552a848eSStefano Babic 
118552a848eSStefano Babic 	clksrc_type = pcc_arrays[clk].clksrc;
119552a848eSStefano Babic 	if (clksrc_type >= CLKSRC_NO_PCS) {
120552a848eSStefano Babic 		printf("No PCS field for the PCC %d, clksrc type %d\n",
121552a848eSStefano Babic 		       clk, clksrc_type);
122552a848eSStefano Babic 		return -EPERM;
123552a848eSStefano Babic 	}
124552a848eSStefano Babic 
125552a848eSStefano Babic 	for (i = 0; i < PCC_CLKSRC_NUM; i++) {
126552a848eSStefano Babic 		if (pcc_clksrc[clksrc_type][i] == src) {
127552a848eSStefano Babic 			/* Find the clock src, then set it to PCS */
128552a848eSStefano Babic 			break;
129552a848eSStefano Babic 		}
130552a848eSStefano Babic 	}
131552a848eSStefano Babic 
132552a848eSStefano Babic 	if (i == PCC_CLKSRC_NUM) {
133552a848eSStefano Babic 		printf("Not find the parent scg_clk in PCS of PCC %d, invalid scg_clk %d\n", clk, src);
134552a848eSStefano Babic 		return -EINVAL;
135552a848eSStefano Babic 	}
136552a848eSStefano Babic 
137552a848eSStefano Babic 	reg = pcc_arrays[clk].pcc_base + pcc_arrays[clk].pcc_slot * 4;
138552a848eSStefano Babic 
139552a848eSStefano Babic 	val = readl(reg);
140552a848eSStefano Babic 
141552a848eSStefano Babic 	clk_debug("pcc_clock_sel: clk %d, reg 0x%x, val 0x%x, clksrc_type %d\n",
142552a848eSStefano Babic 		  clk, reg, val, clksrc_type);
143552a848eSStefano Babic 
144552a848eSStefano Babic 	if (!(val & PCC_PR_MASK) || (val & PCC_INUSE_MASK) ||
145552a848eSStefano Babic 	    (val & PCC_CGC_MASK)) {
146552a848eSStefano Babic 		printf("Not permit to select clock source val = 0x%x\n", val);
147552a848eSStefano Babic 		return -EPERM;
148552a848eSStefano Babic 	}
149552a848eSStefano Babic 
150552a848eSStefano Babic 	val &= ~PCC_PCS_MASK;
151552a848eSStefano Babic 	val |= ((i + 1) << PCC_PCS_OFFSET);
152552a848eSStefano Babic 
153552a848eSStefano Babic 	writel(val, reg);
154552a848eSStefano Babic 
155552a848eSStefano Babic 	clk_debug("pcc_clock_sel: val 0x%x\n", val);
156552a848eSStefano Babic 
157552a848eSStefano Babic 	return 0;
158552a848eSStefano Babic }
159552a848eSStefano Babic 
pcc_clock_div_config(enum pcc_clk clk,bool frac,u8 div)160552a848eSStefano Babic int pcc_clock_div_config(enum pcc_clk clk, bool frac, u8 div)
161552a848eSStefano Babic {
162552a848eSStefano Babic 	u32 reg, val;
163552a848eSStefano Babic 
164552a848eSStefano Babic 	if (clk >= ARRAY_SIZE(pcc_arrays) || div > 8 ||
165552a848eSStefano Babic 	    (div == 1 && frac != 0))
166552a848eSStefano Babic 		return -EINVAL;
167552a848eSStefano Babic 
168552a848eSStefano Babic 	if (pcc_arrays[clk].div >= PCC_NO_DIV) {
169552a848eSStefano Babic 		printf("No DIV/FRAC field for the PCC %d\n", clk);
170552a848eSStefano Babic 		return -EPERM;
171552a848eSStefano Babic 	}
172552a848eSStefano Babic 
173552a848eSStefano Babic 	reg = pcc_arrays[clk].pcc_base + pcc_arrays[clk].pcc_slot * 4;
174552a848eSStefano Babic 
175552a848eSStefano Babic 	val = readl(reg);
176552a848eSStefano Babic 
177552a848eSStefano Babic 	if (!(val & PCC_PR_MASK) || (val & PCC_INUSE_MASK) ||
178552a848eSStefano Babic 	    (val & PCC_CGC_MASK)) {
179552a848eSStefano Babic 		printf("Not permit to set div/frac val = 0x%x\n", val);
180552a848eSStefano Babic 		return -EPERM;
181552a848eSStefano Babic 	}
182552a848eSStefano Babic 
183552a848eSStefano Babic 	if (frac)
184552a848eSStefano Babic 		val |= PCC_FRAC_MASK;
185552a848eSStefano Babic 	else
186552a848eSStefano Babic 		val &= ~PCC_FRAC_MASK;
187552a848eSStefano Babic 
188552a848eSStefano Babic 	val &= ~PCC_PCD_MASK;
189552a848eSStefano Babic 	val |= (div - 1) & PCC_PCD_MASK;
190552a848eSStefano Babic 
191552a848eSStefano Babic 	writel(val, reg);
192552a848eSStefano Babic 
193552a848eSStefano Babic 	return 0;
194552a848eSStefano Babic }
195552a848eSStefano Babic 
pcc_clock_is_enable(enum pcc_clk clk)196552a848eSStefano Babic bool pcc_clock_is_enable(enum pcc_clk clk)
197552a848eSStefano Babic {
198552a848eSStefano Babic 	u32 reg, val;
199552a848eSStefano Babic 
200552a848eSStefano Babic 	if (clk >= ARRAY_SIZE(pcc_arrays))
201552a848eSStefano Babic 		return -EINVAL;
202552a848eSStefano Babic 
203552a848eSStefano Babic 	reg = pcc_arrays[clk].pcc_base + pcc_arrays[clk].pcc_slot * 4;
204552a848eSStefano Babic 	val = readl(reg);
205552a848eSStefano Babic 
206552a848eSStefano Babic 	if ((val & PCC_INUSE_MASK) || (val & PCC_CGC_MASK))
207552a848eSStefano Babic 		return true;
208552a848eSStefano Babic 
209552a848eSStefano Babic 	return false;
210552a848eSStefano Babic }
211552a848eSStefano Babic 
pcc_clock_get_clksrc(enum pcc_clk clk,enum scg_clk * src)212552a848eSStefano Babic int pcc_clock_get_clksrc(enum pcc_clk clk, enum scg_clk *src)
213552a848eSStefano Babic {
214552a848eSStefano Babic 	u32 reg, val, clksrc_type;
215552a848eSStefano Babic 
216552a848eSStefano Babic 	if (clk >= ARRAY_SIZE(pcc_arrays))
217552a848eSStefano Babic 		return -EINVAL;
218552a848eSStefano Babic 
219552a848eSStefano Babic 	clksrc_type = pcc_arrays[clk].clksrc;
220552a848eSStefano Babic 	if (clksrc_type >= CLKSRC_NO_PCS) {
221552a848eSStefano Babic 		printf("No PCS field for the PCC %d, clksrc type %d\n",
222552a848eSStefano Babic 		       clk, clksrc_type);
223552a848eSStefano Babic 		return -EPERM;
224552a848eSStefano Babic 	}
225552a848eSStefano Babic 
226552a848eSStefano Babic 	reg = pcc_arrays[clk].pcc_base + pcc_arrays[clk].pcc_slot * 4;
227552a848eSStefano Babic 
228552a848eSStefano Babic 	val = readl(reg);
229552a848eSStefano Babic 
230552a848eSStefano Babic 	clk_debug("pcc_clock_get_clksrc: clk %d, reg 0x%x, val 0x%x, type %d\n",
231552a848eSStefano Babic 		  clk, reg, val, clksrc_type);
232552a848eSStefano Babic 
233552a848eSStefano Babic 	if (!(val & PCC_PR_MASK)) {
234552a848eSStefano Babic 		printf("This pcc slot is not present = 0x%x\n", val);
235552a848eSStefano Babic 		return -EPERM;
236552a848eSStefano Babic 	}
237552a848eSStefano Babic 
238552a848eSStefano Babic 	val &= PCC_PCS_MASK;
239552a848eSStefano Babic 	val = (val >> PCC_PCS_OFFSET);
240552a848eSStefano Babic 
241552a848eSStefano Babic 	if (!val) {
242552a848eSStefano Babic 		printf("Clock source is off\n");
243552a848eSStefano Babic 		return -EIO;
244552a848eSStefano Babic 	}
245552a848eSStefano Babic 
246552a848eSStefano Babic 	*src = pcc_clksrc[clksrc_type][val - 1];
247552a848eSStefano Babic 
248552a848eSStefano Babic 	clk_debug("pcc_clock_get_clksrc: parent scg clk %d\n", *src);
249552a848eSStefano Babic 
250552a848eSStefano Babic 	return 0;
251552a848eSStefano Babic }
252552a848eSStefano Babic 
pcc_clock_get_rate(enum pcc_clk clk)253552a848eSStefano Babic u32 pcc_clock_get_rate(enum pcc_clk clk)
254552a848eSStefano Babic {
255552a848eSStefano Babic 	u32 reg, val, rate, frac, div;
256552a848eSStefano Babic 	enum scg_clk parent;
257552a848eSStefano Babic 	int ret;
258552a848eSStefano Babic 
259552a848eSStefano Babic 	ret = pcc_clock_get_clksrc(clk, &parent);
260552a848eSStefano Babic 	if (ret)
261552a848eSStefano Babic 		return 0;
262552a848eSStefano Babic 
263552a848eSStefano Babic 	rate = scg_clk_get_rate(parent);
264552a848eSStefano Babic 
265552a848eSStefano Babic 	clk_debug("pcc_clock_get_rate: parent rate %u\n", rate);
266552a848eSStefano Babic 
267552a848eSStefano Babic 	if (pcc_arrays[clk].div == PCC_HAS_DIV) {
268552a848eSStefano Babic 		reg = pcc_arrays[clk].pcc_base + pcc_arrays[clk].pcc_slot * 4;
269552a848eSStefano Babic 		val = readl(reg);
270552a848eSStefano Babic 
271552a848eSStefano Babic 		frac = (val & PCC_FRAC_MASK) >> PCC_FRAC_OFFSET;
272552a848eSStefano Babic 		div = (val & PCC_PCD_MASK) >> PCC_PCD_OFFSET;
273552a848eSStefano Babic 
274552a848eSStefano Babic 		/*
275552a848eSStefano Babic 		 * Theoretically don't have overflow in the calc,
276552a848eSStefano Babic 		 * the rate won't exceed 2G
277552a848eSStefano Babic 		 */
278552a848eSStefano Babic 		rate = rate * (frac + 1) / (div + 1);
279552a848eSStefano Babic 	}
280552a848eSStefano Babic 
281552a848eSStefano Babic 	clk_debug("pcc_clock_get_rate: rate %u\n", rate);
282552a848eSStefano Babic 	return rate;
283552a848eSStefano Babic }
284