blob: 5e08b8341903537bc71ae553bf5af7e6c32d8352 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2019 MediaTek Inc.
4 * Author: Yong Wu <yong.wu@mediatek.com>
5 */
6
7#include <linux/clk.h>
8#include <linux/delay.h>
9#include <linux/mfd/syscon.h>
10#include <linux/of.h>
11#include <linux/of_address.h>
12#include <linux/of_device.h>
13#include <linux/platform_device.h>
14#include <linux/slab.h>
15
16#include "clk-mtk.h"
17#include "clk-mux.h"
18#include "clk-gate.h"
19
20#include <dt-bindings/clock/mt2731-clk.h>
21
22#define CLK_CFG_UPDATE 0x004
23
24static DEFINE_SPINLOCK(mt2731_clk_lock);
25
26static const struct mtk_fixed_clk top_fixed_clks[] = {
27 /* FIXED_CLK(CLK_TOP_CLK26M, "clk26m", NULL, 26000000), */
28 FIXED_CLK(CLK_TOP_USB20_192M, "usb20_192m_ck", "univpll", 192000000),
29};
30
31static const struct mtk_fixed_factor top_divs[] = {
32 FACTOR(CLK_TOP_SYSPLL, "syspll_ck", "mainpll", 1, 1),
33 FACTOR(CLK_TOP_SYSPLL_D2, "syspll_d2", "mainpll", 1, 2),
34 FACTOR(CLK_TOP_SYSPLL1_D2, "syspll1_d2", "syspll_d2", 1, 2),
35 FACTOR(CLK_TOP_SYSPLL1_D4, "syspll1_d4", "syspll_d2", 1, 4),
36 FACTOR(CLK_TOP_SYSPLL1_D8, "syspll1_d8", "syspll_d2", 1, 8),
37 FACTOR(CLK_TOP_SYSPLL1_D16, "syspll1_d16", "syspll_d2", 1, 16),
38 FACTOR(CLK_TOP_SYSPLL_D3, "syspll_d3", "mainpll", 1, 3),
39 FACTOR(CLK_TOP_SYSPLL2_D2, "syspll2_d2", "syspll_d3", 1, 2),
40 FACTOR(CLK_TOP_SYSPLL2_D4, "syspll2_d4", "syspll_d3", 1, 4),
41 FACTOR(CLK_TOP_SYSPLL2_D8, "syspll2_d8", "syspll_d3", 1, 8),
42 FACTOR(CLK_TOP_SYSPLL_D5, "syspll_d5", "mainpll", 1, 5),
43 FACTOR(CLK_TOP_SYSPLL3_D2, "syspll3_d2", "syspll_d5", 1, 2),
44 FACTOR(CLK_TOP_SYSPLL3_D4, "syspll3_d4", "syspll_d5", 1, 4),
45 FACTOR(CLK_TOP_SYSPLL_D7, "syspll_d7", "mainpll", 1, 7),
46 FACTOR(CLK_TOP_SYSPLL4_D2, "syspll4_d2", "syspll_d7", 1, 2),
47 FACTOR(CLK_TOP_SYSPLL4_D4, "syspll4_d4", "syspll_d7", 1, 4),
48 /* FACTOR(CLK_TOP_USB20_192M, "usb20_192m_ck", "None", 1, 1), */
49 FACTOR(CLK_TOP_USB20_192M_D2, "usb20_192m_d2", "usb20_192m_ck", 1, 2),
50 FACTOR(CLK_TOP_USB20_192M_D4, "usb20_192m_d4", "usb20_192m_ck", 1, 4),
51 FACTOR(CLK_TOP_UNIVPLL_D2, "univpll_d2", "univpll", 1, 2),
52 FACTOR(CLK_TOP_UNIVPLL1_D2, "univpll1_d2", "univpll_d2", 1, 2),
53 FACTOR(CLK_TOP_UNIVPLL1_D4, "univpll1_d4", "univpll_d2", 1, 4),
54 FACTOR(CLK_TOP_UNIVPLL1_D8, "univpll1_d8", "univpll_d2", 1, 8),
55 FACTOR(CLK_TOP_UNIVPLL_D3, "univpll_d3", "univpll", 1, 3),
56 FACTOR(CLK_TOP_UNIVPLL2_D2, "univpll2_d2", "univpll_d3", 1, 2),
57 FACTOR(CLK_TOP_UNIVPLL2_D4, "univpll2_d4", "univpll_d3", 1, 4),
58 FACTOR(CLK_TOP_UNIVPLL2_D8, "univpll2_d8", "univpll_d3", 1, 8),
59 FACTOR(CLK_TOP_UNIVPLL2_D16, "univpll2_d16", "univpll_d3", 1, 16),
60 FACTOR(CLK_TOP_UNIVPLL_D5, "univpll_d5", "univpll", 1, 5),
61 FACTOR(CLK_TOP_UNIVPLL3_D2, "univpll3_d2", "univpll_d5", 1, 2),
62 FACTOR(CLK_TOP_UNIVPLL3_D4, "univpll3_d4", "univpll_d5", 1, 4),
63 FACTOR(CLK_TOP_MSDCPLL, "msdcpll_ck", "msdcpll", 1, 1),
64 FACTOR(CLK_TOP_MSDCPLL_D2, "msdcpll_d2", "msdcpll_ck", 1, 2),
65 FACTOR(CLK_TOP_APLL1, "apll1_ck", "apll1", 1, 1),
66 FACTOR(CLK_TOP_APLL1_D2, "apll1_d2", "apll1_ck", 1, 2),
67 FACTOR(CLK_TOP_APLL1_D4, "apll1_d4", "apll1_ck", 1, 4),
68 FACTOR(CLK_TOP_APLL1_D8, "apll1_d8", "apll1_ck", 1, 8),
69 FACTOR(CLK_TOP_APLL2, "apll2_ck", "apll2", 1, 1),
70 FACTOR(CLK_TOP_APLL2_D2, "apll2_d2", "apll2_ck", 1, 2),
71 FACTOR(CLK_TOP_APLL2_D4, "apll2_d4", "apll2_ck", 1, 4),
72 FACTOR(CLK_TOP_APLL2_D8, "apll2_d8", "apll2_ck", 1, 8),
73 FACTOR(CLK_TOP_ETHERPLL, "etherpll_ck", "etherpll", 1, 1),
74 FACTOR(CLK_TOP_ETHERPLL_D4, "etherpll_d4", "etherpll_ck", 1, 4),
75 FACTOR(CLK_TOP_ETHERPLL_D10, "etherpll_d10", "etherpll", 1, 10),
76 FACTOR(CLK_TOP_HD_FAXI, "hd_faxi_ck", "axi_sel", 1, 1),
77 FACTOR(CLK_TOP_F_FUART, "f_fuart_ck", "uart_sel", 1, 1),
78 FACTOR(CLK_TOP_SPI, "spi_ck", "spi_sel", 1, 1),
79 FACTOR(CLK_TOP_MSDC50_0, "msdc50_0_ck", "msdc50_0_sel", 1, 1),
80 FACTOR(CLK_TOP_MSDC30_1, "msdc30_1_ck", "msdc30_1_sel", 1, 1),
81 FACTOR(CLK_TOP_AUDIO, "audio_ck", "audio_sel", 1, 1),
82 FACTOR(CLK_TOP_AUD_1, "aud_1_ck", "aud_1_sel", 1, 1),
83 FACTOR(CLK_TOP_AUD_ENGEN1, "aud_engen1_ck", "aud_engen1_sel", 1, 1),
84 FACTOR(CLK_TOP_AUD_ENGEN2, "aud_engen2_ck", "aud_engen2_sel", 1, 1),
85 FACTOR(CLK_TOP_HSM_CRYPTO, "hsm_crypto_ck", "hsm_crypto_sel", 1, 1),
86 FACTOR(CLK_TOP_I2C, "i2c_ck", "i2c_sel", 1, 1),
87 FACTOR(CLK_TOP_MSDC50_2_HCLK,
88 "msdc5_2hclk_ck", "msdc5_2hclk_sel", 1, 1),
89 FACTOR(CLK_TOP_MSDC30_2, "msdc30_2_ck", "msdc30_2_sel", 1, 1),
90 FACTOR(CLK_TOP_NFI1X_BCLK, "nfi1x_bclk_ck", "nfi1x_bclk_sel", 1, 1),
91 FACTOR(CLK_TOP_PCIE_MAC, "pcie_mac_ck", "pcie_mac_sel", 1, 1),
92 FACTOR(CLK_TOP_F_FSSUSB_TOP, "f_fssusb_top_ck", "ssusb_top_sel", 1, 1),
93 FACTOR(CLK_TOP_SPISLV, "spislv_ck", "spislv_sel", 1, 1),
94 FACTOR(CLK_TOP_ETHER_125M, "ether_125m_ck", "ether_125m_sel", 1, 1),
95 FACTOR(CLK_TOP_PWM, "pwm_ck", "pwm_sel", 1, 1),
96 FACTOR(CLK_TOP_ARMPLL_DIVIDER_PLL1, "arm_div_pll1", "syspll_ck", 1, 1),
97 FACTOR(CLK_TOP_ARMPLL_DIVIDER_PLL2, "arm_div_pll2", "univpll_d2", 1, 1),
98};
99
100static const char * const axi_parents[] = {
101 "clk26m",
102 "syspll2_d2",
103 "syspll_d7",
104 "syspll1_d4",
105 "syspll3_d2"
106};
107
108static const char * const uart_parents[] = {
109 "clk26m",
110 "univpll2_d8"
111};
112
113static const char * const spi_parents[] = {
114 "clk26m",
115 "syspll3_d2",
116 "syspll4_d2",
117 "syspll2_d4"
118};
119
120static const char * const msdc5_0hclk_parents[] = {
121 "clk26m",
122 "syspll1_d2",
123 "univpll1_d4",
124 "syspll2_d2"
125};
126
127static const char * const msdc50_0_parents[] = {
128 "clk26m",
129 "msdcpll_ck",
130 "syspll2_d2",
131 "syspll4_d2",
132 "univpll1_d2",
133 "syspll1_d2",
134 "univpll_d5",
135 "univpll1_d4"
136};
137
138static const char * const msdc30_1_parents[] = {
139 "clk26m",
140 "msdcpll_d2",
141 "univpll2_d2",
142 "syspll2_d2",
143 "syspll1_d4",
144 "univpll1_d4",
145 "usb20_192m_d4",
146 "syspll2_d4"
147};
148
149static const char * const audio_parents[] = {
150 "clk26m",
151 "syspll3_d4",
152 "syspll4_d4",
153 "syspll1_d16"
154};
155
156static const char * const aud_intbus_parents[] = {
157 "clk26m",
158 "syspll1_d4",
159 "syspll4_d2"
160};
161
162static const char * const aud_1_parents[] = {
163 "clk26m",
164 "apll1_ck"
165};
166
167static const char * const aud_2_parents[] = {
168 "clk26m",
169 "apll2_ck"
170};
171
172static const char * const aud_engen1_parents[] = {
173 "clk26m",
174 "apll1_d2",
175 "apll1_d4",
176 "apll1_d8"
177};
178
179static const char * const aud_engen2_parents[] = {
180 "clk26m",
181 "apll2_d2",
182 "apll2_d4",
183 "apll2_d8"
184};
185
186static const char * const dxcc_parents[] = {
187 "clk26m",
188 "syspll1_d2",
189 "syspll1_d4",
190 "syspll1_d8"
191};
192
193static const char * const hsm_crypto_parents[] = {
194 "clk26m",
195 "syspll2_d2",
196 "syspll1_d2",
197 "univpll1_d2"
198};
199
200static const char * const hsm_arc_parents[] = {
201 "clk26m",
202 "syspll1_d8",
203 "syspll1_d4",
204 "syspll2_d2"
205};
206
207static const char * const gcpu_parents[] = {
208 "clk26m",
209 "syspll1_d4",
210 "syspll1_d2",
211 "syspll_d3"
212};
213
214static const char * const ecc_parents[] = {
215 "clk26m",
216 "univpll2_d2",
217 "univpll1_d2",
218 "univpll_d3"
219};
220
221static const char * const usb_top_parents[] = {
222 "clk26m",
223 "univpll3_d4",
224 "usb20_192m_d2"
225};
226
227static const char * const spm_parents[] = {
228 "clk26m",
229 "syspll1_d8"
230};
231
232static const char * const i2c_parents[] = {
233 "clk26m",
234 "univpll3_d4",
235 "univpll3_d2",
236 "syspll1_d8",
237 "syspll2_d8"
238};
239
240static const char * const ulposc_parents[] = {
241 "clk26m",
242 "univpll2_d16"
243};
244
245static const char * const nfi1x_bclk_parents[] = {
246 "clk26m",
247 "univpll2_d8",
248 "univpll1_d8",
249 "syspll2_d4",
250 "univpll2_d4",
251 "syspll1_d4",
252 "univpll1_d4",
253 "syspll2_d2"
254};
255
256static const char * const spinfi_bclk_parents[] = {
257 "clk26m",
258 "univpll2_d8",
259 "syspll1_d8",
260 "univpll1_d8",
261 "syspll2_d4",
262 "univpll2_d4",
263 "univpll3_d2",
264 "syspll1_d4"
265};
266
267static const char * const pcie_mac_parents[] = {
268 "clk26m",
269 "syspll1_d8",
270 "syspll3_d2",
271 "syspll1_d4"
272};
273
274static const char * const ssusb_top_parents[] = {
275 "clk26m",
276 "univpll3_d4",
277 "univpll3_d2"
278};
279
280static const char * const spislv_parents[] = {
281 "clk26m",
282 "univpll2_d4",
283 "univpll3_d2",
284 "univpll1_d4",
285 "univpll2_d2",
286 "univpll_d5",
287 "univpll1_d2",
288 "univpll_d3"
289};
290
291static const char * const ether_125m_parents[] = {
292 "clk26m",
293 "etherpll_d4"
294};
295
296static const char * const ether_50_parents[] = {
297 "clk26m",
298 "etherpll_d10"
299};
300
301static const char * const ether_62p4m_parents[] = {
302 "clk26m",
303 "univpll3_d4"
304};
305
306static const struct mtk_mux top_muxes[] = {
307 /* CLK_CFG_0 */
308 MUX_CLR_SET_UPD_FLAGS(CLK_TOP_AXI_SEL, "axi_sel", axi_parents,
309 0x40, 0x44, 0x48, 0, 3, 7, CLK_CFG_UPDATE, 0,
310 CLK_IS_CRITICAL),
311 MUX_CLR_SET_UPD(CLK_TOP_UART_SEL, "uart_sel", uart_parents,
312 0x40, 0x44, 0x48, 8, 1, 15, CLK_CFG_UPDATE, 1),
313 MUX_CLR_SET_UPD(CLK_TOP_SPI_SEL, "spi_sel", spi_parents,
314 0x40, 0x44, 0x48, 16, 2, 23, CLK_CFG_UPDATE, 2),
315 MUX_CLR_SET_UPD(CLK_TOP_MSDC50_0_HCLK_SEL,
316 "msdc5_0hclk", msdc5_0hclk_parents,
317 0x40, 0x44, 0x48, 24, 2, 31, CLK_CFG_UPDATE, 3),
318 /* CLK_CFG_1 */
319 MUX_CLR_SET_UPD(CLK_TOP_MSDC50_0_SEL,
320 "msdc50_0_sel", msdc50_0_parents,
321 0x050, 0x054, 0x58, 0, 3, 7, CLK_CFG_UPDATE, 4),
322 MUX_CLR_SET_UPD(CLK_TOP_MSDC30_1_SEL,
323 "msdc30_1_sel", msdc30_1_parents,
324 0x050, 0x054, 0x58, 8, 3, 15, CLK_CFG_UPDATE, 5),
325 MUX_CLR_SET_UPD(CLK_TOP_AUDIO_SEL, "audio_sel", audio_parents,
326 0x050, 0x054, 0x58, 16, 2, 23, CLK_CFG_UPDATE, 6),
327 MUX_CLR_SET_UPD(CLK_TOP_AUD_INTBUS_SEL,
328 "aud_intbus_sel", aud_intbus_parents,
329 0x050, 0x054, 0x58, 24, 2, 31, CLK_CFG_UPDATE, 7),
330 /* CLK_CFG_2 */
331 MUX_CLR_SET_UPD(CLK_TOP_AUD_1_SEL, "aud_1_sel", aud_1_parents,
332 0x060, 0x064, 0x068, 0, 1, 7, CLK_CFG_UPDATE, 8),
333 MUX_CLR_SET_UPD(CLK_TOP_AUD_2_SEL, "aud_2_sel", aud_2_parents,
334 0x060, 0x064, 0x068, 8, 1, 15, CLK_CFG_UPDATE, 9),
335 MUX_CLR_SET_UPD(CLK_TOP_AUD_ENGEN1_SEL,
336 "aud_engen1_sel", aud_engen1_parents,
337 0x060, 0x064, 0x068, 16, 2, 23, CLK_CFG_UPDATE, 10),
338 MUX_CLR_SET_UPD(CLK_TOP_AUD_ENGEN2_SEL,
339 "aud_engen2_sel", aud_engen2_parents,
340 0x060, 0x064, 0x068, 24, 2, 31, CLK_CFG_UPDATE, 11),
341 /* CLK_CFG_3 */
342 MUX_CLR_SET_UPD(CLK_TOP_DXCC_SEL, "dxcc_sel", dxcc_parents,
343 0x070, 0x074, 0x078, 0, 2, 7, CLK_CFG_UPDATE, 12),
344 /*
345 MUX_CLR_SET_UPD(CLK_TOP_HSM_CRYPTO_SEL,
346 "hsm_crypto_sel", hsm_crypto_parents,
347 0x070, 0x074, 0x078, 8, 2, 15, CLK_CFG_UPDATE, 13),
348 MUX_CLR_SET_UPD(CLK_TOP_HSM_ARC_SEL,
349 "hsm_arc_sel", hsm_arc_parents,
350 0x070, 0x074, 0x078, 16, 2, 23, CLK_CFG_UPDATE, 14),
351 */
352 MUX_CLR_SET_UPD_FLAGS(CLK_TOP_GCPU_SEL, "gcpu_sel", gcpu_parents,
353 0x070, 0x074, 0x078, 24, 2, 31, CLK_CFG_UPDATE, 15,
354 CLK_IS_CRITICAL),
355 /* CLK_CFG_4 */
356 MUX_CLR_SET_UPD(CLK_TOP_ECC_SEL, "ecc_sel", ecc_parents,
357 0x080, 0x084, 0x088, 0, 2, 7, CLK_CFG_UPDATE, 16),
358 MUX_CLR_SET_UPD(CLK_TOP_USB_TOP_SEL,
359 "usb_top_sel", usb_top_parents,
360 0x080, 0x084, 0x088, 8, 2, 15, CLK_CFG_UPDATE, 17),
361 MUX_CLR_SET_UPD_FLAGS(CLK_TOP_SPM_SEL, "spm_sel", spm_parents,
362 0x080, 0x084, 0x088, 16, 1, 23, CLK_CFG_UPDATE, 18,
363 CLK_IS_CRITICAL),
364 MUX_CLR_SET_UPD(CLK_TOP_I2C_SEL, "i2c_sel", i2c_parents,
365 0x080, 0x084, 0x088, 24, 3, 31, CLK_CFG_UPDATE, 19),
366 /* CLK_CFG_5 */
367 MUX_CLR_SET_UPD(CLK_TOP_PWRAP_ULPOSC_SEL,
368 "ulposc_sel", ulposc_parents,
369 0x090, 0x094, 0x098, 0, 1, 7, CLK_CFG_UPDATE, 20),
370 MUX_CLR_SET_UPD(CLK_TOP_MSDC50_2_HCLK_SEL,
371 "msdc5_2hclk_sel", msdc5_0hclk_parents,
372 0x090, 0x094, 0x098, 8, 2, 15, CLK_CFG_UPDATE, 21),
373 MUX_CLR_SET_UPD(CLK_TOP_MSDC30_2_SEL,
374 "msdc30_2_sel", msdc50_0_parents,
375 0x090, 0x094, 0x098, 16, 3, 23, CLK_CFG_UPDATE, 22),
376 MUX_CLR_SET_UPD(CLK_TOP_NFI1X_BCLK_SEL,
377 "nfi1x_bclk_sel", nfi1x_bclk_parents,
378 0x090, 0x094, 0x098, 24, 3, 31, CLK_CFG_UPDATE, 23),
379 /* CLK_CFG_6 */
380 MUX_CLR_SET_UPD(CLK_TOP_SPINFI_BCLK_SEL,
381 "spinfi_bclk_sel", spinfi_bclk_parents,
382 0x0a0, 0x0a4, 0x0a8, 0, 3, 7, CLK_CFG_UPDATE, 24),
383 MUX_CLR_SET_UPD(CLK_TOP_PCIE_MAC_SEL,
384 "pcie_mac_sel", pcie_mac_parents,
385 0x0a0, 0x0a4, 0x0a8, 8, 2, 15, CLK_CFG_UPDATE, 25),
386 MUX_CLR_SET_UPD(CLK_TOP_SSUSB_TOP_SEL,
387 "ssusb_top_sel", ssusb_top_parents,
388 0x0a0, 0x0a4, 0x0a8, 16, 2, 23, CLK_CFG_UPDATE, 26),
389 MUX_CLR_SET_UPD(CLK_TOP_SPISLV_SEL,
390 "spislv_sel", spislv_parents,
391 0x0a0, 0x0a4, 0x0a8, 24, 3, 31, CLK_CFG_UPDATE, 27),
392 /* CLK_CFG_7 */
393 MUX_CLR_SET_UPD(CLK_TOP_ETHER_125M_SEL,
394 "ether_125m_sel", ether_125m_parents,
395 0x0b0, 0x0b4, 0x0b8, 0, 1, 7, CLK_CFG_UPDATE, 28),
396 MUX_CLR_SET_UPD(CLK_TOP_ETHER_50M_RMII_SEL,
397 "ether_50_sel", ether_50_parents,
398 0x0b0, 0x0b4, 0x0b8, 8, 1, 15, CLK_CFG_UPDATE, 29),
399 MUX_CLR_SET_UPD(CLK_TOP_ETHER_62P4M_SEL,
400 "ether_62p4m_sel", ether_62p4m_parents,
401 0x0b0, 0x0b4, 0x0b8, 16, 1, 23, CLK_CFG_UPDATE, 30),
402 MUX_CLR_SET_UPD(CLK_TOP_PWM_SEL,
403 "pwm_sel", ether_62p4m_parents,
404 0x0b0, 0x0b4, 0x0b8, 24, 1, 31, CLK_CFG_UPDATE, 31),
405};
406
407static const char * const i2s0_m_ck_parents[] __initconst = {
408 "aud_1_sel",
409 "aud_2_sel"
410};
411
412static const char * const i2s1_m_ck_parents[] __initconst = {
413 "aud_1_sel",
414 "aud_2_sel"
415};
416
417static const char * const i2s2_m_ck_parents[] __initconst = {
418 "aud_1_sel",
419 "aud_2_sel"
420};
421
422static const char * const i2s3_m_ck_parents[] __initconst = {
423 "aud_1_sel",
424 "aud_2_sel"
425};
426
427static const char * const i2s4_m_ck_parents[] __initconst = {
428 "aud_1_sel",
429 "aud_2_sel"
430};
431
432static const struct mtk_composite top_audmuxes[] = {
433 MUX(CLK_TOP_I2S0_M_SEL, "i2s0_m_ck_sel", i2s0_m_ck_parents,
434 0x320, 8, 1),
435 MUX(CLK_TOP_I2S1_M_SEL, "i2s1_m_ck_sel", i2s1_m_ck_parents,
436 0x320, 9, 1),
437 MUX(CLK_TOP_I2S2_M_SEL, "i2s2_m_ck_sel", i2s2_m_ck_parents,
438 0x320, 10, 1),
439 MUX(CLK_TOP_I2S3_M_SEL, "i2s3_m_ck_sel", i2s3_m_ck_parents,
440 0x320, 11, 1),
441 MUX(CLK_TOP_I2S4_M_SEL, "i2s4_m_ck_sel", i2s4_m_ck_parents,
442 0x320, 12, 1),
443
444 DIV_GATE(CLK_TOP_APLL12_DIV0, "apll12_div0", "i2s0_m_ck_sel",
445 0x320, 2, 0x324, 8, 0),
446 DIV_GATE(CLK_TOP_APLL12_DIV1, "apll12_div1", "i2s1_m_ck_sel",
447 0x320, 3, 0x324, 8, 8),
448 DIV_GATE(CLK_TOP_APLL12_DIV2, "apll12_div2", "i2s2_m_ck_sel",
449 0x320, 4, 0x324, 8, 16),
450 DIV_GATE(CLK_TOP_APLL12_DIV3, "apll12_div3", "i2s3_m_ck_sel",
451 0x320, 5, 0x324, 8, 24),
452 DIV_GATE(CLK_TOP_APLL12_DIV4, "apll12_div4", "i2s4_m_ck_sel",
453 0x320, 6, 0x328, 8, 0),
454 DIV_GATE(CLK_TOP_APLL12_DIVB, "apll12_divb", "apll12_div4",
455 0x320, 7, 0x328, 8, 8),
456};
457
458static const struct mtk_gate_regs top0_cg_regs = {
459 .set_ofs = 0x104,
460 .clr_ofs = 0x104,
461 .sta_ofs = 0x104,
462};
463
464#define GATE_TOP0(_id, _name, _parent, _shift) { \
465 .id = _id, \
466 .name = _name, \
467 .parent_name = _parent, \
468 .regs = &top0_cg_regs, \
469 .shift = _shift, \
470 .ops = &mtk_clk_gate_ops_no_setclr_inv, \
471 }
472
473static const struct mtk_gate top_clks[] = {
474 /* TOP0 */
475 GATE_TOP0(CLK_TOP_ARMPLL_DIVIDER_PLL1_EN,
476 "arm_div_pll1_en", "arm_div_pll1", 4),
477 GATE_TOP0(CLK_TOP_ARMPLL_DIVIDER_PLL2_EN,
478 "arm_div_pll2_en", "arm_div_pll2", 5),
479 /* TOP1 */
480 /*
481 *GATE_TOP1(CLK_TOP_APLL12_DIV0, "apll12_div0", "aud_1_ck", 2),
482 *GATE_TOP1(CLK_TOP_APLL12_DIV1, "apll12_div1", "aud_1_ck", 3),
483 *GATE_TOP1(CLK_TOP_APLL12_DIV2, "apll12_div2", "aud_1_ck", 4),
484 *GATE_TOP1(CLK_TOP_APLL12_DIV3, "apll12_div3", "aud_1_ck", 5),
485 *GATE_TOP1(CLK_TOP_APLL12_DIV4, "apll12_div4", "aud_1_ck", 6),
486 *GATE_TOP1(CLK_TOP_APLL12_DIVB, "apll12_divb", "aud_1_ck", 7),
487 */
488};
489
490static const struct mtk_gate_regs ifr0_cg_regs = {
491 .set_ofs = 0x80,
492 .clr_ofs = 0x84,
493 .sta_ofs = 0x90,
494};
495
496static const struct mtk_gate_regs ifr1_cg_regs = {
497 .set_ofs = 0x88,
498 .clr_ofs = 0x8c,
499 .sta_ofs = 0x94,
500};
501
502static const struct mtk_gate_regs ifr2_cg_regs = {
503 .set_ofs = 0xa4,
504 .clr_ofs = 0xa8,
505 .sta_ofs = 0xac,
506};
507
508static const struct mtk_gate_regs ifr3_cg_regs = {
509 .set_ofs = 0xc0,
510 .clr_ofs = 0xc4,
511 .sta_ofs = 0xc8,
512};
513
514#define GATE_IFR0(_id, _name, _parent, _shift) { \
515 .id = _id, \
516 .name = _name, \
517 .parent_name = _parent, \
518 .regs = &ifr0_cg_regs, \
519 .shift = _shift, \
520 .ops = &mtk_clk_gate_ops_setclr, \
521 }
522
523#define GATE_IFR1(_id, _name, _parent, _shift) { \
524 .id = _id, \
525 .name = _name, \
526 .parent_name = _parent, \
527 .regs = &ifr1_cg_regs, \
528 .shift = _shift, \
529 .ops = &mtk_clk_gate_ops_setclr, \
530 }
531
532#define GATE_IFR1_I(_id, _name, _parent, _shift) { \
533 .id = _id, \
534 .name = _name, \
535 .parent_name = _parent, \
536 .regs = &ifr1_cg_regs, \
537 .shift = _shift, \
538 .ops = &mtk_clk_gate_ops_setclr_inv, \
539 }
540
541#define GATE_IFR2(_id, _name, _parent, _shift) { \
542 .id = _id, \
543 .name = _name, \
544 .parent_name = _parent, \
545 .regs = &ifr2_cg_regs, \
546 .shift = _shift, \
547 .ops = &mtk_clk_gate_ops_setclr, \
548 }
549
550#define GATE_IFR3(_id, _name, _parent, _shift) { \
551 .id = _id, \
552 .name = _name, \
553 .parent_name = _parent, \
554 .regs = &ifr3_cg_regs, \
555 .shift = _shift, \
556 .ops = &mtk_clk_gate_ops_setclr, \
557 }
558
559static const struct mtk_gate ifr_clks[] = {
560 /* IFR0 */
561 GATE_IFR0(CLK_IFR_APXGPT, "ifr_apxgpt", "hd_faxi_ck", 6),
562 GATE_IFR0(CLK_IFR_ICUSB, "ifr_icusb", "hd_faxi_ck", 8),
563 GATE_IFR0(CLK_IFR_GCE, "ifr_gce", "hd_faxi_ck", 9),
564 GATE_IFR0(CLK_IFR_THERM, "ifr_therm", "hd_faxi_ck", 10),
565 GATE_IFR0(CLK_IFR_I2C_AP, "ifr_i2c_ap", "i2c_ck", 11),
566 GATE_IFR0(CLK_IFR_I2C_CCU, "ifr_i2c_ccu", "i2c_ck", 12),
567 GATE_IFR0(CLK_IFR_I2C_SSPM, "ifr_i2c_sspm", "i2c_ck", 13),
568 GATE_IFR0(CLK_IFR_I2C_RSV, "ifr_i2c_rsv", "i2c_ck", 14),
569 GATE_IFR0(CLK_IFR_PWM_HCLK, "ifr_pwm_hclk", "hd_faxi_ck", 15),
570 GATE_IFR0(CLK_IFR_PWM1, "ifr_pwm1", "pwm_ck", 16),
571 GATE_IFR0(CLK_IFR_PWM2, "ifr_pwm2", "pwm_ck", 17),
572 GATE_IFR0(CLK_IFR_PWM3, "ifr_pwm3", "pwm_ck", 18),
573 GATE_IFR0(CLK_IFR_PWM4, "ifr_pwm4", "pwm_ck", 19),
574 GATE_IFR0(CLK_IFR_PWM5, "ifr_pwm5", "pwm_ck", 20),
575 GATE_IFR0(CLK_IFR_PWM, "ifr_pwm", "pwm_ck", 21),
576 GATE_IFR0(CLK_IFR_UART0, "ifr_uart0", "f_fuart_ck", 22),
577 GATE_IFR0(CLK_IFR_UART1, "ifr_uart1", "f_fuart_ck", 23),
578 GATE_IFR0(CLK_IFR_UART2, "ifr_uart2", "f_fuart_ck", 24),
579 GATE_IFR0(CLK_IFR_UART3, "ifr_uart3", "f_fuart_ck", 25),
580 GATE_IFR0(CLK_IFR_GCE_26M, "ifr_gce_26m", "clk26m", 27),
581 GATE_IFR0(CLK_IFR_CQ_DMA_FPC, "ifr_dma", "hd_faxi_ck", 28),
582 /* IFR1 */
583 GATE_IFR1(CLK_IFR_SPI0, "ifr_spi0", "spi_ck", 1),
584 GATE_IFR1(CLK_IFR_MSDC0, "ifr_msdc0", "msdc5_0hclk", 2),
585 GATE_IFR1(CLK_IFR_MSDC1, "ifr_msdc1", "hd_faxi_ck", 4),
586 GATE_IFR1(CLK_IFR_MSDC2, "ifr_msdc2_clk", "msdc5_2hclk_ck", 5),
587 GATE_IFR1_I(CLK_IFR_TRNG, "ifr_trng", "hd_faxi_ck", 9),
588 GATE_IFR1(CLK_IFR_CCIF1_AP, "ifr_ccif1_ap", "hd_faxi_ck", 12),
589 GATE_IFR1(CLK_IFR_CCIF1_MD, "ifr_ccif1_md", "hd_faxi_ck", 13),
590 GATE_IFR1(CLK_IFR_PCIE, "ifr_pcie", "pcie_mac_ck", 16),
591 GATE_IFR1(CLK_IFR_NFI, "ifr_nfi", "nfi1x_bclk_ck", 17),
592 GATE_IFR1(CLK_IFR_AP_DMA, "ifr_ap_dma", "hd_faxi_ck", 18),
593 GATE_IFR1(CLK_IFR_DEVICE_APC, "ifr_dapc", "hd_faxi_ck", 20),
594 GATE_IFR1(CLK_IFR_CCIF_AP, "ifr_ccif_ap", "hd_faxi_ck", 23),
595 /* GATE_IFR1(CLK_IFR_DEBUGSYS, "ifr_debugsys", "hd_faxi_ck", 24), */
596 GATE_IFR1(CLK_IFR_CCIF_MD, "ifr_ccif_md", "hd_faxi_ck", 26),
597 /* GATE_IFR1(CLK_IFR_HSM, "ifr_hsm", "hsm_crypto_ck", 27),
598 * GATE_IFR1(CLK_IFR_HSM_AO, "ifr_hsm_ao", "hsm_crypto_ck", 28),
599 */
600 GATE_IFR1(CLK_IFR_ETHER, "ifr_ether", "ether_125m_ck", 29),
601 GATE_IFR1(CLK_IFR_SPI_SLAVE, "ifr_spi_slave", "spislv_ck", 30),
602 /* IFR2 */
603 GATE_IFR2(CLK_IFR_RG_PWM_FBCLK6, "ifr_pwmfb", "clk26m", 0),
604 GATE_IFR2(CLK_IFR_SSUSB, "ifr_ssusb", "f_fssusb_top_ck", 1),
605 GATE_IFR2(CLK_IFR_CLDMA_BCLK, "ifr_cldmabclk", "hd_faxi_ck", 3),
606 GATE_IFR2(CLK_IFR_AUDIO_26M_BCLK, "ifr_audio26m", "clk26m", 4),
607 GATE_IFR2(CLK_IFR_SPI1, "ifr_spi1", "spi_ck", 6),
608 GATE_IFR2(CLK_IFR_SPI2, "ifr_spi2", "spi_ck", 9),
609 GATE_IFR2(CLK_IFR_SPI3, "ifr_spi3", "spi_ck", 10),
610 GATE_IFR2(CLK_IFR_SPI4, "ifr_spi4", "spi_ck", 25),
611 GATE_IFR2(CLK_IFR_SPI5, "ifr_spi5", "spi_ck", 26),
612 GATE_IFR2(CLK_IFR_CQ_DMA, "ifr_cq_dma", "hd_faxi_ck", 27),
613 /* IFR3 */
614 GATE_IFR3(CLK_IFR_I2C6, "ifr_i2c6", "i2c_ck", 6),
615 GATE_IFR3(CLK_IFR_MSDC0_SRC, "ifr_msdc0_clk", "msdc50_0_ck", 9),
616 GATE_IFR3(CLK_IFR_MSDC1_SRC, "ifr_msdc1_clk", "msdc30_1_ck", 10),
617 GATE_IFR3(CLK_IFR_MSDC2_SRC, "ifr_msdc2", "msdc30_2_ck", 11),
618 GATE_IFR3(CLK_IFR_MCU_PM_BCLK, "ifr_mcu_pm_bclk", "hd_faxi_ck", 17),
619 GATE_IFR3(CLK_IFR_CCIF2_AP, "ifr_ccif2_ap", "hd_faxi_ck", 18),
620 GATE_IFR3(CLK_IFR_CCIF2_MD, "ifr_ccif2_md", "hd_faxi_ck", 19),
621 GATE_IFR3(CLK_IFR_UART4, "ifr_uart4", "hd_faxi_ck", 22),
622 GATE_IFR3(CLK_IFR_UART5, "ifr_uart5", "hd_faxi_ck", 23),
623 GATE_IFR3(CLK_IFR_UART6, "ifr_uart6", "hd_faxi_ck", 24),
624};
625
626/* FIXME: modify FMAX/FMIN/RSTBAR */
627#define MT2731_PLL_FMAX (3000UL * MHZ)
628#define MT2731_PLL_FMIN (1500UL * MHZ)
629#define CON0_MT2731_RST_BAR BIT(23)
630
631#define PLL_B(_id, _name, _reg, _pwr_reg, _en_mask, _flags, _pcwbits, \
632 _pcwibits, _pd_reg, _pd_shift, _tuner_reg, _tuner_en_reg,\
633 _tuner_en_bit, _pcw_reg, _pcw_shift, \
634 _pcw_chg_reg, _div_table) {\
635 .id = _id, \
636 .name = _name, \
637 .reg = _reg, \
638 .pwr_reg = _pwr_reg, \
639 .en_mask = _en_mask, \
640 .flags = _flags, \
641 .rst_bar_mask = CON0_MT2731_RST_BAR, \
642 .fmax = MT2731_PLL_FMAX, \
643 .fmin = MT2731_PLL_FMIN, \
644 .pcwbits = _pcwbits, \
645 .pcwibits = _pcwibits, \
646 .pd_reg = _pd_reg, \
647 .pd_shift = _pd_shift, \
648 .tuner_reg = _tuner_reg, \
649 .tuner_en_reg = _tuner_en_reg, \
650 .tuner_en_bit = _tuner_en_bit, \
651 .pcw_reg = _pcw_reg, \
652 .pcw_shift = _pcw_shift, \
653 .pcw_chg_reg = _pcw_chg_reg, \
654 .div_table = _div_table, \
655 }
656
657#define PLL(_id, _name, _reg, _pwr_reg, _en_mask, _flags, _pcwbits, \
658 _pcwibits, _pd_reg, _pd_shift, _tuner_reg, \
659 _tuner_en_reg, _tuner_en_bit, _pcw_reg, \
660 _pcw_shift, _pcw_chg_reg) \
661 PLL_B(_id, _name, _reg, _pwr_reg, _en_mask, _flags, \
662 _pcwbits, _pcwibits, _pd_reg, _pd_shift, \
663 _tuner_reg, _tuner_en_reg, _tuner_en_bit, \
664 _pcw_reg, _pcw_shift, _pcw_chg_reg, NULL) \
665
666static const struct mtk_pll_div_table armpll_div_table[] = {
667 { .div = 0, .freq = MT2731_PLL_FMAX },
668 { .div = 1, .freq = 1500 * MHZ },
669 { .div = 2, .freq = 750 * MHZ },
670 { .div = 3, .freq = 375 * MHZ },
671 { .div = 4, .freq = 187500000 },
672 { } /* sentinel */
673};
674
675/* The frequency of mainpll and univpll are NOT recommended to adjust. */
676static const struct mtk_pll_data plls[] = {
677 /* _pd_reg: postdiv */
678 PLL_B(CLK_APMIXED_ARMPLL, "armpll", 0x0204, 0x0210, BIT(0),
679 PLL_AO, 22, 8, 0x0208, 24, 0, 0, 0, 0x0208, 0, 0,
680 armpll_div_table),
681 PLL(CLK_APMIXED_MAINPLL, "mainpll", 0x0214, 0x0220, BIT(0),
682 HAVE_RST_BAR | PLL_AO, 22, 8, 0x0218, 24, 0, 0, 0, 0x0218, 0,
683 0),
684 /*
685 * In order to generate u192M, univpll HW always need freq*2(hw will/2).
686 * But some user still need get_rate, we have to return the accurate
687 * rate, thus use 7 for pcwibits. It's ok since the reg keep as-is.
688 */
689 PLL(CLK_APMIXED_UNIVPLL, "univpll", 0x0224, 0x0230, BIT(0),
690 HAVE_RST_BAR, 22, 7, 0x0228, 24, 0, 0, 0, 0x0228, 0, 0),
691 PLL(CLK_APMIXED_MSDCPLL, "msdcpll", 0x0234, 0x0240, BIT(0),
692 0, 22, 8, 0x0238, 24, 0, 0, 0, 0x0238, 0, 0),
693 PLL(CLK_APMIXED_APLL1, "apll1", 0x0244, 0x0254, BIT(0),
694 0, 32, 8, 0x0248, 24, 0x40, 0x0C, 0, 0x024C, 0, 0x248),
695 /* No tuner for apll2. */
696 PLL(CLK_APMIXED_APLL2, "apll2", 0x0258, 0x0268, BIT(0),
697 0, 32, 8, 0x025C, 24, 0, 0, 0, 0x0260, 0, 0x25C),
698 PLL(CLK_APMIXED_MPLL, "mpll", 0x026C, 0x0278, BIT(0),
699 PLL_AO, 22, 8, 0x0270, 24, 0, 0, 0, 0x0270, 0, 0),
700 PLL(CLK_APMIXED_ETHERPLL, "etherpll", 0x027C, 0x0288, BIT(0),
701 0, 22, 8, 0x0280, 24, 0, 0, 0, 0x0280, 0, 0),
702};
703
704static int clk_mt2731_apmixed_probe(struct platform_device *pdev)
705{
706 struct clk_onecell_data *clk_data;
707 int r;
708 struct device_node *node = pdev->dev.of_node;
709 void __iomem *base;
710 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
711 u32 reg;
712
713 clk_data = mtk_alloc_clk_data(CLK_APMIXED_NR_CLK);
714
715 mtk_clk_register_plls(node, plls, ARRAY_SIZE(plls), clk_data);
716
717 r = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
718
719 if (r)
720 pr_notice("%s(): could not register clock provider: %d\n",
721 __func__, r);
722
723 /* switch PLL/BUS mux to spm control.*/
724 #define PLLON_CON0 0x44
725 #define AP_PLL_CON3 0xC
726 base = devm_ioremap_resource(&pdev->dev, res);
727 if (IS_ERR(base)) {
728 pr_notice("%s(): ioremap failed\n", __func__);
729 return PTR_ERR(base);
730 }
731 reg = readl_relaxed(base + PLLON_CON0);
732
733 /* reg &= ~(BIT(16) | BIT(20) | BIT(24)); univpll */
734 reg &= ~(BIT(17) | BIT(21) | BIT(25)); /* mpll */
735 reg &= ~(BIT(18) | BIT(22) | BIT(26)); /* mainpll */
736 reg &= ~(BIT(19) | BIT(23) | BIT(27)); /* armpll */
737 writel(reg, base + PLLON_CON0);
738
739 reg = readl_relaxed(base + AP_PLL_CON3);
740 reg &= ~BIT(1); /* CLKSQ sleep control. */
741 reg &= ~BIT(2); /* CLKSQ LPF sleep control. */
742 writel(reg, base + AP_PLL_CON3);
743
744 devm_iounmap(&pdev->dev, base);
745
746 return r;
747}
748
749static int clk_mt2731_top_probe(struct platform_device *pdev)
750{
751 int r;
752 struct device_node *node = pdev->dev.of_node;
753 void __iomem *base;
754 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
755 struct clk_onecell_data *mt2731_top_clk_data;
756
757 base = devm_ioremap_resource(&pdev->dev, res);
758 if (IS_ERR(base)) {
759 pr_notice("%s(): ioremap failed\n", __func__);
760 return PTR_ERR(base);
761 }
762
763 mt2731_top_clk_data = mtk_alloc_clk_data(CLK_TOP_NR_CLK);
764
765 mtk_clk_register_fixed_clks(top_fixed_clks, ARRAY_SIZE(top_fixed_clks),
766 mt2731_top_clk_data);
767 mtk_clk_register_factors(top_divs, ARRAY_SIZE(top_divs),
768 mt2731_top_clk_data);
769 /* mtk_clk_register_composites(top_muxes, ARRAY_SIZE(top_muxes),
770 * base, &mt2731_clk_lock, mt2731_top_clk_data);
771 */
772 mtk_clk_register_muxes(top_muxes, ARRAY_SIZE(top_muxes),
773 node, &mt2731_clk_lock, mt2731_top_clk_data);
774 mtk_clk_register_composites(top_audmuxes, ARRAY_SIZE(top_audmuxes),
775 base, &mt2731_clk_lock, mt2731_top_clk_data);
776 mtk_clk_register_gates(node, top_clks, ARRAY_SIZE(top_clks),
777 mt2731_top_clk_data);
778
779 r = of_clk_add_provider(node, of_clk_src_onecell_get,
780 mt2731_top_clk_data);
781
782 if (r)
783 pr_notice("%s(): could not register clock provider: %d\n",
784 __func__, r);
785 //mtk_clk_enable_critical();
786 { /* switch PLL/BUS mux to spm control.*/
787 #define CLK_SCP_CFG_1 0x204
788 u32 reg = readl_relaxed(base + CLK_SCP_CFG_1);
789
790 reg |= 0x1;
791 writel(reg, base + CLK_SCP_CFG_1);
792 }
793
794 return r;
795}
796
797static int clk_mt2731_infra_probe(struct platform_device *pdev)
798{
799 struct clk_onecell_data *clk_data;
800 struct device_node *node = pdev->dev.of_node;
801 int r;
802
803 clk_data = mtk_alloc_clk_data(CLK_IFR_NR_CLK);
804
805 mtk_clk_register_gates(node, ifr_clks, ARRAY_SIZE(ifr_clks),
806 clk_data);
807
808 r = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
809
810 if (r)
811 pr_err("%s(): could not register clock provider: %d\n",
812 __func__, r);
813
814 return r;
815}
816
817static const struct of_device_id of_match_clk_mt2731[] = {
818 {
819 .compatible = "mediatek,mt2731-apmixedsys",
820 .data = clk_mt2731_apmixed_probe,
821 }, {
822 .compatible = "mediatek,mt2731-topckgen",
823 .data = clk_mt2731_top_probe,
824 }, {
825 .compatible = "mediatek,mt2731-infracfg",
826 .data = clk_mt2731_infra_probe,
827 }, {
828 /* sentinel */
829 }
830};
831
832static int clk_mt2731_probe(struct platform_device *pdev)
833{
834 int (*clk_probe)(struct platform_device *);
835 int r;
836
837 clk_probe = of_device_get_match_data(&pdev->dev);
838 if (!clk_probe)
839 return -EINVAL;
840
841 r = clk_probe(pdev);
842 if (r)
843 dev_notice(&pdev->dev,
844 "could not register clock provider: %s: %d\n",
845 pdev->name, r);
846
847 return r;
848}
849
850static struct platform_driver clk_mt2731_drv = {
851 .probe = clk_mt2731_probe,
852 .driver = {
853 .name = "clk-mt2731",
854 .owner = THIS_MODULE,
855 .of_match_table = of_match_clk_mt2731,
856 },
857};
858
859static int __init clk_mt2731_init(void)
860{
861 return platform_driver_register(&clk_mt2731_drv);
862}
863
864arch_initcall(clk_mt2731_init);