blob: 7909d770afeebc2e467dc066f32da668c476b7b1 [file] [log] [blame]
Peng Fana84dab42021-08-07 16:00:45 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2021 NXP
4 */
5
6#include <common.h>
7#include <div64.h>
8#include <asm/io.h>
9#include <errno.h>
10#include <asm/arch/imx-regs.h>
11#include <asm/arch/pcc.h>
12#include <asm/arch/cgc.h>
13#include <asm/arch/sys_proto.h>
14
Ye Li5ff18da2021-10-29 09:46:18 +080015#define cgc_clk_TYPES 2
16#define cgc_clk_NUM 8
Peng Fana84dab42021-08-07 16:00:45 +080017
Alice Guo0f9b10a2021-10-29 09:46:29 +080018static enum cgc_clk pcc1_clksrc[][8] = {
19 {
20 },
21 {
22 DUMMY0_CLK,
23 LPOSC,
24 SOSC_DIV2,
25 FRO_DIV2,
26 CM33_BUSCLK,
27 PLL1_VCO_DIV,
28 PLL0_PFD2_DIV,
29 PLL0_PFD1_DIV,
30 }
31};
32
Ye Li5ff18da2021-10-29 09:46:18 +080033static enum cgc_clk pcc3_clksrc[][8] = {
Peng Fana84dab42021-08-07 16:00:45 +080034 {
35 },
36 { DUMMY0_CLK,
37 LPOSC,
38 SOSC_DIV2,
39 FRO_DIV2,
40 XBAR_BUSCLK,
41 PLL3_PFD1_DIV1,
42 PLL3_PFD0_DIV2,
43 PLL3_PFD0_DIV1
44 }
45};
46
Ye Li5ff18da2021-10-29 09:46:18 +080047static enum cgc_clk pcc4_clksrc[][8] = {
Peng Fana84dab42021-08-07 16:00:45 +080048 {
49 DUMMY0_CLK,
50 SOSC_DIV1,
51 FRO_DIV1,
52 PLL3_PFD3_DIV2,
53 PLL3_PFD3_DIV1,
54 PLL3_PFD2_DIV2,
55 PLL3_PFD2_DIV1,
56 PLL3_PFD1_DIV2
57 },
58 {
59 DUMMY0_CLK,
60 DUMMY1_CLK,
61 LPOSC,
62 SOSC_DIV2,
63 FRO_DIV2,
64 XBAR_BUSCLK,
65 PLL3_VCODIV,
66 PLL3_PFD0_DIV1
67 }
68};
69
Ye Li5ff18da2021-10-29 09:46:18 +080070static enum cgc_clk pcc5_clksrc[][8] = {
71 {
72 DUMMY0_CLK,
73 PLL4_PFD3_DIV2,
74 PLL4_PFD2_DIV2,
75 PLL4_PFD2_DIV1,
76 PLL4_PFD1_DIV2,
77 PLL4_PFD1_DIV1,
78 PLL4_PFD0_DIV2,
79 PLL4_PFD0_DIV1
80 },
81 {
82 DUMMY0_CLK,
83 DUMMY1_CLK,
84 LPOSC,
85 SOSC_DIV2,
86 FRO_DIV2,
87 LPAV_BUSCLK,
88 PLL4_VCODIV,
89 PLL4_PFD3_DIV1
90 }
91};
92
Alice Guo0f9b10a2021-10-29 09:46:29 +080093static struct pcc_entry pcc1_arrays[] = {
94 {PCC1_RBASE, ADC1_PCC1_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV, PCC_HAS_RST_B},
95 {}
96};
97
Peng Fana84dab42021-08-07 16:00:45 +080098static struct pcc_entry pcc3_arrays[] = {
99 {PCC3_RBASE, DMA1_MP_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
100 {PCC3_RBASE, DMA1_CH0_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
101 {PCC3_RBASE, DMA1_CH1_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
102 {PCC3_RBASE, DMA1_CH2_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
103 {PCC3_RBASE, DMA1_CH3_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
104 {PCC3_RBASE, DMA1_CH4_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
105 {PCC3_RBASE, DMA1_CH5_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
106 {PCC3_RBASE, DMA1_CH6_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
107 {PCC3_RBASE, DMA1_CH7_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
108 {PCC3_RBASE, DMA1_CH8_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
109 {PCC3_RBASE, DMA1_CH9_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
110 {PCC3_RBASE, DMA1_CH10_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
111 {PCC3_RBASE, DMA1_CH11_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
112 {PCC3_RBASE, DMA1_CH12_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
113 {PCC3_RBASE, DMA1_CH13_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
114 {PCC3_RBASE, DMA1_CH14_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
115 {PCC3_RBASE, DMA1_CH15_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
116 {PCC3_RBASE, DMA1_CH16_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
117 {PCC3_RBASE, DMA1_CH17_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
118 {PCC3_RBASE, DMA1_CH18_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
119 {PCC3_RBASE, DMA1_CH19_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
120 {PCC3_RBASE, DMA1_CH20_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
121 {PCC3_RBASE, DMA1_CH21_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
122 {PCC3_RBASE, DMA1_CH22_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
123 {PCC3_RBASE, DMA1_CH23_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
124 {PCC3_RBASE, DMA1_CH24_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
125 {PCC3_RBASE, DMA1_CH25_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
126 {PCC3_RBASE, DMA1_CH26_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
127 {PCC3_RBASE, DMA1_CH27_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
128 {PCC3_RBASE, DMA1_CH28_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
129 {PCC3_RBASE, DMA1_CH29_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
130 {PCC3_RBASE, DMA1_CH30_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
131 {PCC3_RBASE, DMA1_CH31_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
132 {PCC3_RBASE, MU0_B_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
133 {PCC3_RBASE, MU3_A_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
134 {PCC3_RBASE, LLWU1_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
135 {PCC3_RBASE, UPOWER_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
136 {PCC3_RBASE, WDOG3_PCC3_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B},
137 {PCC3_RBASE, WDOG4_PCC3_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B},
138 {PCC3_RBASE, XRDC_MGR_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
139 {PCC3_RBASE, SEMA42_1_PCC3_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV, PCC_NO_RST_B},
140 {PCC3_RBASE, ROMCP1_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B},
141 {PCC3_RBASE, LPIT1_PCC3_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B},
142 {PCC3_RBASE, TPM4_PCC3_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B},
143 {PCC3_RBASE, TPM5_PCC3_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B},
144 {PCC3_RBASE, FLEXIO1_PCC3_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B},
145 {PCC3_RBASE, I3C2_PCC3_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B},
146 {PCC3_RBASE, LPI2C4_PCC3_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B},
147 {PCC3_RBASE, LPI2C5_PCC3_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B},
148 {PCC3_RBASE, LPUART4_PCC3_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B},
149 {PCC3_RBASE, LPUART5_PCC3_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B},
150 {PCC3_RBASE, LPSPI4_PCC3_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B},
151 {PCC3_RBASE, LPSPI5_PCC3_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B},
152 {}
153};
154
155static struct pcc_entry pcc4_arrays[] = {
156 {PCC4_RBASE, FLEXSPI2_PCC4_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV, PCC_HAS_RST_B },
157 {PCC4_RBASE, TPM6_PCC4_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B },
158 {PCC4_RBASE, TPM7_PCC4_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B },
159 {PCC4_RBASE, LPI2C6_PCC4_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B },
160 {PCC4_RBASE, LPI2C7_PCC4_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B },
161 {PCC4_RBASE, LPUART6_PCC4_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B },
162 {PCC4_RBASE, LPUART7_PCC4_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B },
163 {PCC4_RBASE, SAI4_PCC4_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_HAS_RST_B },
164 {PCC4_RBASE, SAI5_PCC4_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_HAS_RST_B },
165 {PCC4_RBASE, PCTLE_PCC4_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
166 {PCC4_RBASE, PCTLF_PCC4_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
167 {PCC4_RBASE, SDHC0_PCC4_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV, PCC_HAS_RST_B },
168 {PCC4_RBASE, SDHC1_PCC4_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV, PCC_HAS_RST_B },
169 {PCC4_RBASE, SDHC2_PCC4_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV, PCC_HAS_RST_B },
170 {PCC4_RBASE, USB0_PCC4_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_HAS_RST_B },
171 {PCC4_RBASE, USBPHY_PCC4_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_HAS_RST_B },
172 {PCC4_RBASE, USB1_PCC4_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_HAS_RST_B },
173 {PCC4_RBASE, USB1PHY_PCC4_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_HAS_RST_B },
174 {PCC4_RBASE, USB_XBAR_PCC4_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
175 {PCC4_RBASE, ENET_PCC4_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_HAS_RST_B },
176 {PCC4_RBASE, SFA1_PCC4_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
177 {PCC4_RBASE, RGPIOE_PCC4_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
178 {PCC4_RBASE, RGPIOF_PCC4_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
179 {}
180};
181
Ye Li5ff18da2021-10-29 09:46:18 +0800182static struct pcc_entry pcc5_arrays[] = {
183 {PCC5_RBASE, DMA2_MP_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
184 {PCC5_RBASE, DMA2_CH0_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
185 {PCC5_RBASE, DMA2_CH1_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
186 {PCC5_RBASE, DMA2_CH2_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
187 {PCC5_RBASE, DMA2_CH3_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
188 {PCC5_RBASE, DMA2_CH4_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
189 {PCC5_RBASE, DMA2_CH5_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
190 {PCC5_RBASE, DMA2_CH6_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
191 {PCC5_RBASE, DMA2_CH7_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
192 {PCC5_RBASE, DMA2_CH8_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
193 {PCC5_RBASE, DMA2_CH9_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
194 {PCC5_RBASE, DMA2_CH10_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
195 {PCC5_RBASE, DMA2_CH11_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
196 {PCC5_RBASE, DMA2_CH12_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
197 {PCC5_RBASE, DMA2_CH13_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
198 {PCC5_RBASE, DMA2_CH14_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
199 {PCC5_RBASE, DMA2_CH15_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
200 {PCC5_RBASE, DMA2_CH16_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
201 {PCC5_RBASE, DMA2_CH17_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
202 {PCC5_RBASE, DMA2_CH18_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
203 {PCC5_RBASE, DMA2_CH19_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
204 {PCC5_RBASE, DMA2_CH20_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
205 {PCC5_RBASE, DMA2_CH21_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
206 {PCC5_RBASE, DMA2_CH22_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
207 {PCC5_RBASE, DMA2_CH23_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
208 {PCC5_RBASE, DMA2_CH24_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
209 {PCC5_RBASE, DMA2_CH25_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
210 {PCC5_RBASE, DMA2_CH26_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
211 {PCC5_RBASE, DMA2_CH27_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
212 {PCC5_RBASE, DMA2_CH28_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
213 {PCC5_RBASE, DMA2_CH29_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
214 {PCC5_RBASE, DMA2_CH30_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
215 {PCC5_RBASE, DMA2_CH31_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
216 {PCC5_RBASE, MU2_B_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
217 {PCC5_RBASE, MU3_B_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
218 {PCC5_RBASE, SEMA42_2_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
219 {PCC5_RBASE, CMC2_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
220 {PCC5_RBASE, AVD_SIM_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
221 {PCC5_RBASE, LPAV_CGC_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
222 {PCC5_RBASE, PCC5_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
223 {PCC5_RBASE, TPM8_PCC5_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B },
224 {PCC5_RBASE, SAI6_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_HAS_RST_B },
225 {PCC5_RBASE, SAI7_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_HAS_RST_B },
226 {PCC5_RBASE, SPDIF_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_HAS_RST_B },
227 {PCC5_RBASE, ISI_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_HAS_RST_B },
228 {PCC5_RBASE, CSI_REGS_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_HAS_RST_B },
229 {PCC5_RBASE, CSI_PCC5_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV, PCC_HAS_RST_B },
230 {PCC5_RBASE, DSI_PCC5_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV, PCC_HAS_RST_B },
231 {PCC5_RBASE, WDOG5_PCC5_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV, PCC_HAS_RST_B },
232 {PCC5_RBASE, EPDC_PCC5_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV, PCC_HAS_RST_B },
233 {PCC5_RBASE, PXP_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_HAS_RST_B },
234 {PCC5_RBASE, SFA2_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
235 {PCC5_RBASE, GPU2D_PCC5_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV, PCC_HAS_RST_B },
236 {PCC5_RBASE, GPU3D_PCC5_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV, PCC_HAS_RST_B },
237 {PCC5_RBASE, DCNANO_PCC5_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV, PCC_HAS_RST_B },
238 {PCC5_RBASE, LPDDR4_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_HAS_RST_B },
239 {PCC5_RBASE, CSI_CLK_UI_PCC5_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV, PCC_NO_RST_B },
240 {PCC5_RBASE, CSI_CLK_ESC_PCC5_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV, PCC_NO_RST_B },
241 {PCC5_RBASE, RGPIOD_PCC5_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV, PCC_NO_RST_B },
242 {}
243};
244
Peng Fana84dab42021-08-07 16:00:45 +0800245static int find_pcc_entry(int pcc_controller, int pcc_clk_slot, struct pcc_entry **out)
246{
247 struct pcc_entry *pcc_array;
248 int index = 0;
249
250 switch (pcc_controller) {
Alice Guo0f9b10a2021-10-29 09:46:29 +0800251 case 1:
252 pcc_array = pcc1_arrays;
253 *out = &pcc1_arrays[0];
254 break;
Peng Fana84dab42021-08-07 16:00:45 +0800255 case 3:
256 pcc_array = pcc3_arrays;
257 *out = &pcc3_arrays[0];
258 break;
259 case 4:
260 pcc_array = pcc4_arrays;
261 *out = &pcc4_arrays[0];
262 break;
Ye Li5ff18da2021-10-29 09:46:18 +0800263 case 5:
264 pcc_array = pcc5_arrays;
265 *out = &pcc5_arrays[0];
266 break;
Peng Fana84dab42021-08-07 16:00:45 +0800267 default:
268 printf("Not supported pcc_controller: %d\n", pcc_controller);
269 return -EINVAL;
270 }
271
272 while (pcc_array->pcc_base) {
273 if (pcc_array->pcc_slot == pcc_clk_slot)
274 return index;
275
276 pcc_array++;
277 index++;
278 }
279
280 return -ENOENT;
281}
282
283int pcc_clock_enable(int pcc_controller, int pcc_clk_slot, bool enable)
284{
285 u32 val;
286 void __iomem *reg;
287 int clk;
288 struct pcc_entry *pcc_array;
289
290 clk = find_pcc_entry(pcc_controller, pcc_clk_slot, &pcc_array);
291 if (clk < 0)
292 return -EINVAL;
293
294 reg = (void __iomem *)(uintptr_t)(pcc_array[clk].pcc_base + pcc_array[clk].pcc_slot * 4);
295
296 val = readl(reg);
297
298 debug("%s: clk %d, reg 0x%p, val 0x%x, enable %d\n", __func__, clk, reg, val, enable);
299
300 if (!(val & PCC_PR_MASK) || (val & PCC_INUSE_MASK))
301 return -EPERM;
302
303 if (enable)
304 val |= PCC_CGC_MASK;
305 else
306 val &= ~PCC_CGC_MASK;
307
308 writel(val, reg);
309
310 debug("%s: val 0x%x\n", __func__, val);
311
312 return 0;
313}
314
315/* The clock source select needs clock is disabled */
Ye Li5ff18da2021-10-29 09:46:18 +0800316int pcc_clock_sel(int pcc_controller, int pcc_clk_slot, enum cgc_clk src)
Peng Fana84dab42021-08-07 16:00:45 +0800317{
318 u32 val, i, clksrc_type;
319 void __iomem *reg;
320 struct pcc_entry *pcc_array;
Ye Li5ff18da2021-10-29 09:46:18 +0800321 enum cgc_clk *cgc_clk_array;
Peng Fana84dab42021-08-07 16:00:45 +0800322 int clk;
323
324 clk = find_pcc_entry(pcc_controller, pcc_clk_slot, &pcc_array);
325 if (clk < 0)
326 return -EINVAL;
327
328 reg = (void __iomem *)(uintptr_t)(pcc_array[clk].pcc_base + pcc_array[clk].pcc_slot * 4);
329
330 clksrc_type = pcc_array[clk].clksrc;
331 if (clksrc_type >= CLKSRC_NO_PCS) {
332 printf("No PCS field for the PCC %d, clksrc type %d\n",
333 clk, clksrc_type);
334 return -EPERM;
335 }
336
Alice Guo0f9b10a2021-10-29 09:46:29 +0800337 if (pcc_controller == 1)
338 cgc_clk_array = pcc1_clksrc[clksrc_type];
339 else if (pcc_controller == 3)
Ye Li5ff18da2021-10-29 09:46:18 +0800340 cgc_clk_array = pcc3_clksrc[clksrc_type];
341 else if (pcc_controller == 4)
342 cgc_clk_array = pcc4_clksrc[clksrc_type];
Peng Fana84dab42021-08-07 16:00:45 +0800343 else
Ye Li5ff18da2021-10-29 09:46:18 +0800344 cgc_clk_array = pcc5_clksrc[clksrc_type];
Peng Fana84dab42021-08-07 16:00:45 +0800345
Ye Li5ff18da2021-10-29 09:46:18 +0800346 for (i = 0; i < cgc_clk_NUM; i++) {
347 if (cgc_clk_array[i] == src) {
Peng Fana84dab42021-08-07 16:00:45 +0800348 /* Find the clock src, then set it to PCS */
349 break;
350 }
351 }
352
Ye Li5ff18da2021-10-29 09:46:18 +0800353 if (i == cgc_clk_NUM) {
Peng Fana84dab42021-08-07 16:00:45 +0800354 printf("No parent in PCS of PCC %d, invalid scg_clk %d\n", clk, src);
355 return -EINVAL;
356 }
357
358 val = readl(reg);
359
360 debug("%s: clk %d, reg 0x%p, val 0x%x, clksrc_type %d\n",
361 __func__, clk, reg, val, clksrc_type);
362
363 if (!(val & PCC_PR_MASK) || (val & PCC_INUSE_MASK) ||
364 (val & PCC_CGC_MASK)) {
365 printf("Not permit to select clock source val = 0x%x\n", val);
366 return -EPERM;
367 }
368
369 val &= ~PCC_PCS_MASK;
370 val |= i << PCC_PCS_OFFSET;
371
372 writel(val, reg);
373
374 debug("%s: val 0x%x\n", __func__, val);
375
376 return 0;
377}
378
379int pcc_clock_div_config(int pcc_controller, int pcc_clk_slot, bool frac, u8 div)
380{
381 u32 val;
382 void __iomem *reg;
383 struct pcc_entry *pcc_array;
384 int clk;
385
386 clk = find_pcc_entry(pcc_controller, pcc_clk_slot, &pcc_array);
387 if (clk < 0)
388 return -EINVAL;
389
390 reg = (void __iomem *)(uintptr_t)(pcc_array[clk].pcc_base + pcc_array[clk].pcc_slot * 4);
391
392 if (div > 8 || (div == 1 && frac != 0))
393 return -EINVAL;
394
395 if (pcc_array[clk].div >= PCC_NO_DIV) {
396 printf("No DIV/FRAC field for the PCC %d\n", clk);
397 return -EPERM;
398 }
399
400 val = readl(reg);
401
402 if (!(val & PCC_PR_MASK) || (val & PCC_INUSE_MASK) ||
403 (val & PCC_CGC_MASK)) {
404 printf("Not permit to set div/frac val = 0x%x\n", val);
405 return -EPERM;
406 }
407
408 if (frac)
409 val |= PCC_FRAC_MASK;
410 else
411 val &= ~PCC_FRAC_MASK;
412
413 val &= ~PCC_PCD_MASK;
414 val |= (div - 1) & PCC_PCD_MASK;
415
416 writel(val, reg);
417
418 return 0;
419}
420
421bool pcc_clock_is_enable(int pcc_controller, int pcc_clk_slot)
422{
423 u32 val;
424 void __iomem *reg;
425 struct pcc_entry *pcc_array;
426 int clk;
427
428 clk = find_pcc_entry(pcc_controller, pcc_clk_slot, &pcc_array);
429 if (clk < 0)
430 return -EINVAL;
431
432 reg = (void __iomem *)(uintptr_t)(pcc_array[clk].pcc_base + pcc_array[clk].pcc_slot * 4);
433 val = readl(reg);
434
435 if ((val & PCC_INUSE_MASK) || (val & PCC_CGC_MASK))
436 return true;
437
438 return false;
439}
440
Ye Li5ff18da2021-10-29 09:46:18 +0800441int pcc_clock_get_clksrc(int pcc_controller, int pcc_clk_slot, enum cgc_clk *src)
Peng Fana84dab42021-08-07 16:00:45 +0800442{
443 u32 val, clksrc_type;
444 void __iomem *reg;
445 struct pcc_entry *pcc_array;
446 int clk;
Ye Li5ff18da2021-10-29 09:46:18 +0800447 enum cgc_clk *cgc_clk_array;
Peng Fana84dab42021-08-07 16:00:45 +0800448
449 clk = find_pcc_entry(pcc_controller, pcc_clk_slot, &pcc_array);
450 if (clk < 0)
451 return -EINVAL;
452
453 clksrc_type = pcc_array[clk].clksrc;
454 if (clksrc_type >= CLKSRC_NO_PCS) {
455 printf("No PCS field for the PCC %d, clksrc type %d\n",
456 pcc_clk_slot, clksrc_type);
457 return -EPERM;
458 }
459
460 reg = (void __iomem *)(uintptr_t)(pcc_array[clk].pcc_base + pcc_array[clk].pcc_slot * 4);
461
462 val = readl(reg);
463
464 debug("%s: clk %d, reg 0x%p, val 0x%x, type %d\n",
465 __func__, pcc_clk_slot, reg, val, clksrc_type);
466
467 if (!(val & PCC_PR_MASK)) {
468 printf("This pcc slot is not present = 0x%x\n", val);
469 return -EPERM;
470 }
471
472 val &= PCC_PCS_MASK;
473 val = (val >> PCC_PCS_OFFSET);
474
475 if (!val) {
476 printf("Clock source is off\n");
477 return -EIO;
478 }
479
480 if (pcc_controller == 3)
Ye Li5ff18da2021-10-29 09:46:18 +0800481 cgc_clk_array = pcc3_clksrc[clksrc_type];
482 else if (pcc_controller == 4)
483 cgc_clk_array = pcc4_clksrc[clksrc_type];
Peng Fana84dab42021-08-07 16:00:45 +0800484 else
Ye Li5ff18da2021-10-29 09:46:18 +0800485 cgc_clk_array = pcc5_clksrc[clksrc_type];
Peng Fana84dab42021-08-07 16:00:45 +0800486
Ye Li5ff18da2021-10-29 09:46:18 +0800487 *src = cgc_clk_array[val];
Peng Fana84dab42021-08-07 16:00:45 +0800488
489 debug("%s: parent cgc1 clk %d\n", __func__, *src);
490
491 return 0;
492}
493
494int pcc_reset_peripheral(int pcc_controller, int pcc_clk_slot, bool reset)
495{
496 u32 val;
497 void __iomem *reg;
498 struct pcc_entry *pcc_array;
499 int clk;
500
501 clk = find_pcc_entry(pcc_controller, pcc_clk_slot, &pcc_array);
502 if (clk < 0)
503 return -EINVAL;
504
505 if (pcc_array[clk].rst_b == PCC_NO_RST_B)
506 return 0;
507
508 reg = (void __iomem *)(uintptr_t)(pcc_array[clk].pcc_base + pcc_array[clk].pcc_slot * 4);
509
510 val = readl(reg);
511
512 debug("%s: clk %d, reg 0x%p, val 0x%x\n", __func__, pcc_clk_slot, reg, val);
513
514 if (!(val & PCC_PR_MASK)) {
515 printf("This pcc slot is not present = 0x%x\n", val);
516 return -EPERM;
517 }
518
519 if (reset)
520 val &= ~BIT(28);
521 else
522 val |= BIT(28);
523
524 writel(val, reg);
525
526 debug("%s: clk %d, reg 0x%p, val 0x%x\n", __func__, pcc_clk_slot, reg, val);
527
528 return 0;
529}
530
531u32 pcc_clock_get_rate(int pcc_controller, int pcc_clk_slot)
532{
533 u32 val, rate, frac, div;
534 void __iomem *reg;
Ye Li5ff18da2021-10-29 09:46:18 +0800535 enum cgc_clk parent;
Peng Fana84dab42021-08-07 16:00:45 +0800536 int ret;
537 int clk;
538 struct pcc_entry *pcc_array;
539
540 clk = find_pcc_entry(pcc_controller, pcc_clk_slot, &pcc_array);
541 if (clk < 0)
542 return -EINVAL;
543
544 ret = pcc_clock_get_clksrc(pcc_controller, pcc_clk_slot, &parent);
545 if (ret)
546 return 0;
547
Ye Li5ff18da2021-10-29 09:46:18 +0800548 rate = cgc_clk_get_rate(parent);
Peng Fana84dab42021-08-07 16:00:45 +0800549
550 debug("%s: parent rate %u\n", __func__, rate);
551
552 if (pcc_array[clk].div == PCC_HAS_DIV) {
553 reg = (void __iomem *)(uintptr_t)(pcc_array[clk].pcc_base +
554 pcc_array[clk].pcc_slot * 4);
555 val = readl(reg);
556
557 frac = (val & PCC_FRAC_MASK) >> PCC_FRAC_OFFSET;
558 div = (val & PCC_PCD_MASK) >> PCC_PCD_OFFSET;
559
560 /*
561 * Theoretically don't have overflow in the calc,
562 * the rate won't exceed 2G
563 */
564 rate = rate * (frac + 1) / (div + 1);
565 }
566
567 debug("%s: rate %u\n", __func__, rate);
568 return rate;
569}