blob: 02b9df93c29a07d39991cbeb2473d7199141f099 [file] [log] [blame]
Ghennadi Procopciucf648e5d2024-06-12 09:07:16 +03001/*
2 * Copyright 2024 NXP
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6#include <drivers/clk.h>
Ghennadi Procopciuc0609bcd2024-08-06 13:25:51 +03007#include <platform_def.h>
Ghennadi Procopciucf648e5d2024-06-12 09:07:16 +03008#include <s32cc-clk-drv.h>
9#include <s32cc-clk-ids.h>
10#include <s32cc-clk-utils.h>
11
Ghennadi Procopciuc22f94742024-08-06 11:48:11 +030012#define S32CC_FXOSC_FREQ (40U * MHZ)
13#define S32CC_ARM_PLL_VCO_FREQ (2U * GHZ)
14#define S32CC_ARM_PLL_PHI0_FREQ (1U * GHZ)
15#define S32CC_A53_FREQ (1U * GHZ)
16#define S32CC_XBAR_2X_FREQ (800U * MHZ)
17#define S32CC_PERIPH_PLL_VCO_FREQ (2U * GHZ)
Ghennadi Procopciuc0609bcd2024-08-06 13:25:51 +030018#define S32CC_PERIPH_PLL_PHI3_FREQ UART_CLOCK_HZ
Ghennadi Procopciuc74dde092024-09-09 10:24:35 +030019#define S32CC_DDR_PLL_VCO_FREQ (1600U * MHZ)
20#define S32CC_DDR_PLL_PHI0_FREQ (800U * MHZ)
Ghennadi Procopciucf648e5d2024-06-12 09:07:16 +030021
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +030022static int setup_fxosc(void)
Ghennadi Procopciucf648e5d2024-06-12 09:07:16 +030023{
24 int ret;
25
Ghennadi Procopciuc97a30902024-07-23 12:14:02 +030026 ret = clk_set_rate(S32CC_CLK_FXOSC, S32CC_FXOSC_FREQ, NULL);
Ghennadi Procopciuc4e4786d2024-06-12 11:17:37 +030027 if (ret != 0) {
28 return ret;
29 }
30
Ghennadi Procopciuc97a30902024-07-23 12:14:02 +030031 return ret;
32}
33
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +030034static int setup_arm_pll(void)
Ghennadi Procopciuc97a30902024-07-23 12:14:02 +030035{
36 int ret;
37
38 ret = clk_set_parent(S32CC_CLK_ARM_PLL_MUX, S32CC_CLK_FXOSC);
Ghennadi Procopciucf648e5d2024-06-12 09:07:16 +030039 if (ret != 0) {
40 return ret;
41 }
42
Ghennadi Procopciuce18cf332024-06-12 11:55:32 +030043 ret = clk_set_rate(S32CC_CLK_ARM_PLL_VCO, S32CC_ARM_PLL_VCO_FREQ, NULL);
44 if (ret != 0) {
45 return ret;
46 }
47
Ghennadi Procopciuc907f6542024-06-12 12:00:15 +030048 ret = clk_set_rate(S32CC_CLK_ARM_PLL_PHI0, S32CC_ARM_PLL_PHI0_FREQ, NULL);
49 if (ret != 0) {
50 return ret;
51 }
52
Ghennadi Procopciuc97a30902024-07-23 12:14:02 +030053 return ret;
54}
55
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +030056static int setup_periph_pll(void)
Ghennadi Procopciuc22f94742024-08-06 11:48:11 +030057{
58 int ret;
59
60 ret = clk_set_parent(S32CC_CLK_PERIPH_PLL_MUX, S32CC_CLK_FXOSC);
61 if (ret != 0) {
62 return ret;
63 }
64
65 ret = clk_set_rate(S32CC_CLK_PERIPH_PLL_VCO, S32CC_PERIPH_PLL_VCO_FREQ, NULL);
66 if (ret != 0) {
67 return ret;
68 }
69
70 ret = clk_set_rate(S32CC_CLK_PERIPH_PLL_PHI3, S32CC_PERIPH_PLL_PHI3_FREQ, NULL);
71 if (ret != 0) {
72 return ret;
73 }
74
Ghennadi Procopciuc22f94742024-08-06 11:48:11 +030075 return ret;
76}
77
Ghennadi Procopciuc97a30902024-07-23 12:14:02 +030078static int enable_a53_clk(void)
79{
80 int ret;
81
82 ret = clk_set_parent(S32CC_CLK_MC_CGM1_MUX0, S32CC_CLK_ARM_PLL_PHI0);
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +030083 if (ret != 0) {
84 return ret;
85 }
86
Ghennadi Procopciuc97a30902024-07-23 12:14:02 +030087 ret = clk_set_rate(S32CC_CLK_A53_CORE, S32CC_A53_FREQ, NULL);
Ghennadi Procopciuc9efc7502024-06-12 14:30:30 +030088 if (ret != 0) {
89 return ret;
90 }
91
Ghennadi Procopciuca080f782024-06-12 14:44:47 +030092 ret = clk_enable(S32CC_CLK_A53_CORE);
93 if (ret != 0) {
94 return ret;
95 }
96
Ghennadi Procopciucf648e5d2024-06-12 09:07:16 +030097 return ret;
98}
Ghennadi Procopciuc97a30902024-07-23 12:14:02 +030099
Ghennadi Procopciucb3950cf2024-08-05 16:51:03 +0300100static int enable_xbar_clk(void)
101{
102 int ret;
103
104 ret = clk_set_parent(S32CC_CLK_MC_CGM0_MUX0, S32CC_CLK_ARM_PLL_DFS1);
105 if (ret != 0) {
106 return ret;
107 }
108
109 ret = clk_set_rate(S32CC_CLK_XBAR_2X, S32CC_XBAR_2X_FREQ, NULL);
110 if (ret != 0) {
111 return ret;
112 }
113
114 ret = clk_enable(S32CC_CLK_ARM_PLL_DFS1);
115 if (ret != 0) {
116 return ret;
117 }
118
119 ret = clk_enable(S32CC_CLK_XBAR_2X);
120 if (ret != 0) {
121 return ret;
122 }
123
124 return ret;
125}
126
Ghennadi Procopciuc0609bcd2024-08-06 13:25:51 +0300127static int enable_uart_clk(void)
128{
129 int ret;
130
131 ret = clk_set_parent(S32CC_CLK_MC_CGM0_MUX8, S32CC_CLK_PERIPH_PLL_PHI3);
132 if (ret != 0) {
133 return ret;
134 }
135
136 ret = clk_enable(S32CC_CLK_LINFLEX_BAUD);
137 if (ret != 0) {
138 return ret;
139 }
140
141 return ret;
142}
143
Ghennadi Procopciuc74dde092024-09-09 10:24:35 +0300144static int setup_ddr_pll(void)
145{
146 int ret;
147
148 ret = clk_set_parent(S32CC_CLK_DDR_PLL_MUX, S32CC_CLK_FXOSC);
149 if (ret != 0) {
150 return ret;
151 }
152
153 ret = clk_set_rate(S32CC_CLK_DDR_PLL_VCO, S32CC_DDR_PLL_VCO_FREQ, NULL);
154 if (ret != 0) {
155 return ret;
156 }
157
158 ret = clk_set_rate(S32CC_CLK_DDR_PLL_PHI0, S32CC_DDR_PLL_PHI0_FREQ, NULL);
159 if (ret != 0) {
160 return ret;
161 }
162
163 return ret;
164}
165
Ghennadi Procopciucd89e32f2024-09-17 11:22:30 +0300166static int enable_ddr_clk(void)
167{
168 int ret;
169
170 ret = clk_set_parent(S32CC_CLK_MC_CGM5_MUX0, S32CC_CLK_DDR_PLL_PHI0);
171 if (ret != 0) {
172 return ret;
173 }
174
175 ret = clk_enable(S32CC_CLK_DDR);
176 if (ret != 0) {
177 return ret;
178 }
179
180 return ret;
181}
182
Ghennadi Procopciuc97a30902024-07-23 12:14:02 +0300183int s32cc_init_early_clks(void)
184{
185 int ret;
186
187 s32cc_clk_register_drv();
188
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300189 ret = setup_fxosc();
Ghennadi Procopciuc97a30902024-07-23 12:14:02 +0300190 if (ret != 0) {
191 return ret;
192 }
193
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300194 ret = setup_arm_pll();
Ghennadi Procopciuc97a30902024-07-23 12:14:02 +0300195 if (ret != 0) {
196 return ret;
197 }
198
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300199 ret = enable_a53_clk();
Ghennadi Procopciuc22f94742024-08-06 11:48:11 +0300200 if (ret != 0) {
201 return ret;
202 }
203
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300204 ret = enable_xbar_clk();
Ghennadi Procopciuc97a30902024-07-23 12:14:02 +0300205 if (ret != 0) {
206 return ret;
207 }
208
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300209 ret = setup_periph_pll();
Ghennadi Procopciucb3950cf2024-08-05 16:51:03 +0300210 if (ret != 0) {
211 return ret;
212 }
213
Ghennadi Procopciuc0609bcd2024-08-06 13:25:51 +0300214 ret = enable_uart_clk();
215 if (ret != 0) {
216 return ret;
217 }
218
Ghennadi Procopciuc74dde092024-09-09 10:24:35 +0300219 ret = setup_ddr_pll();
220 if (ret != 0) {
221 return ret;
222 }
223
Ghennadi Procopciucd89e32f2024-09-17 11:22:30 +0300224 ret = enable_ddr_clk();
225 if (ret != 0) {
226 return ret;
227 }
228
Ghennadi Procopciuc97a30902024-07-23 12:14:02 +0300229 return ret;
230}