blob: 44caf9f2896d081b46331c173785627f1ef35dc2 [file] [log] [blame]
Ghennadi Procopciucfc26eb02024-06-11 18:39:58 +03001/*
Ghennadi Procopciuc7d927632025-01-10 16:26:21 +02002 * Copyright 2024-2025 NXP
Ghennadi Procopciucfc26eb02024-06-11 18:39:58 +03003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6#include <errno.h>
Ghennadi Procopciuc0d525772024-06-12 08:09:19 +03007#include <common/debug.h>
Ghennadi Procopciucfc26eb02024-06-11 18:39:58 +03008#include <drivers/clk.h>
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +03009#include <lib/mmio.h>
Ghennadi Procopciucbdd85872024-11-26 16:39:41 +020010#include <lib/xlat_tables/xlat_tables_v2.h>
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +030011#include <s32cc-clk-ids.h>
Ghennadi Procopciuc0d525772024-06-12 08:09:19 +030012#include <s32cc-clk-modules.h>
Ghennadi Procopciucd89e32f2024-09-17 11:22:30 +030013#include <s32cc-clk-regs.h>
Ghennadi Procopciuc0d525772024-06-12 08:09:19 +030014#include <s32cc-clk-utils.h>
Ghennadi Procopciucd89e32f2024-09-17 11:22:30 +030015#include <s32cc-mc-me.h>
Ghennadi Procopciuc0d525772024-06-12 08:09:19 +030016
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +030017#define MAX_STACK_DEPTH (40U)
Ghennadi Procopciuc0d525772024-06-12 08:09:19 +030018
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +030019/* This is used for floating-point precision calculations. */
20#define FP_PRECISION (100000000UL)
21
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +030022struct s32cc_clk_drv {
23 uintptr_t fxosc_base;
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +030024 uintptr_t armpll_base;
Ghennadi Procopciuc22f94742024-08-06 11:48:11 +030025 uintptr_t periphpll_base;
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +030026 uintptr_t armdfs_base;
Ghennadi Procopciuceb26f082025-01-20 16:02:21 +020027 uintptr_t periphdfs_base;
Ghennadi Procopciuc90c90002024-08-05 16:50:52 +030028 uintptr_t cgm0_base;
Ghennadi Procopciuca080f782024-06-12 14:44:47 +030029 uintptr_t cgm1_base;
Ghennadi Procopciucd89e32f2024-09-17 11:22:30 +030030 uintptr_t cgm5_base;
Ghennadi Procopciuc74dde092024-09-09 10:24:35 +030031 uintptr_t ddrpll_base;
Ghennadi Procopciucd89e32f2024-09-17 11:22:30 +030032 uintptr_t mc_me;
33 uintptr_t mc_rgm;
34 uintptr_t rdc;
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +030035};
36
Ghennadi Procopciuc393f79b2025-01-13 09:50:51 +020037static int set_module_rate(const struct s32cc_clk_obj *module,
38 unsigned long rate, unsigned long *orate,
39 unsigned int *depth);
40static int get_module_rate(const struct s32cc_clk_obj *module,
41 const struct s32cc_clk_drv *drv,
42 unsigned long *rate,
43 unsigned int depth);
44
Ghennadi Procopciuc0d525772024-06-12 08:09:19 +030045static int update_stack_depth(unsigned int *depth)
46{
47 if (*depth == 0U) {
48 return -ENOMEM;
49 }
50
51 (*depth)--;
52 return 0;
53}
Ghennadi Procopciucfc26eb02024-06-11 18:39:58 +030054
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +030055static struct s32cc_clk_drv *get_drv(void)
56{
57 static struct s32cc_clk_drv driver = {
58 .fxosc_base = FXOSC_BASE_ADDR,
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +030059 .armpll_base = ARMPLL_BASE_ADDR,
Ghennadi Procopciuc22f94742024-08-06 11:48:11 +030060 .periphpll_base = PERIPHPLL_BASE_ADDR,
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +030061 .armdfs_base = ARM_DFS_BASE_ADDR,
Ghennadi Procopciuceb26f082025-01-20 16:02:21 +020062 .periphdfs_base = PERIPH_DFS_BASE_ADDR,
Ghennadi Procopciuc90c90002024-08-05 16:50:52 +030063 .cgm0_base = CGM0_BASE_ADDR,
Ghennadi Procopciuca080f782024-06-12 14:44:47 +030064 .cgm1_base = CGM1_BASE_ADDR,
Ghennadi Procopciucd89e32f2024-09-17 11:22:30 +030065 .cgm5_base = MC_CGM5_BASE_ADDR,
Ghennadi Procopciuc74dde092024-09-09 10:24:35 +030066 .ddrpll_base = DDRPLL_BASE_ADDR,
Ghennadi Procopciucd89e32f2024-09-17 11:22:30 +030067 .mc_me = MC_ME_BASE_ADDR,
68 .mc_rgm = MC_RGM_BASE_ADDR,
69 .rdc = RDC_BASE_ADDR,
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +030070 };
71
72 return &driver;
73}
74
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +030075static int enable_module(struct s32cc_clk_obj *module,
76 const struct s32cc_clk_drv *drv,
77 unsigned int depth);
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +030078
Ghennadi Procopciuc302831c2024-09-11 09:29:50 +030079static struct s32cc_clk_obj *get_clk_parent(const struct s32cc_clk_obj *module)
80{
81 const struct s32cc_clk *clk = s32cc_obj2clk(module);
82
83 if (clk->module != NULL) {
84 return clk->module;
85 }
86
87 if (clk->pclock != NULL) {
88 return &clk->pclock->desc;
89 }
90
91 return NULL;
92}
93
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +030094static int get_base_addr(enum s32cc_clk_source id, const struct s32cc_clk_drv *drv,
95 uintptr_t *base)
96{
97 int ret = 0;
98
99 switch (id) {
100 case S32CC_FXOSC:
101 *base = drv->fxosc_base;
102 break;
103 case S32CC_ARM_PLL:
104 *base = drv->armpll_base;
105 break;
Ghennadi Procopciuc22f94742024-08-06 11:48:11 +0300106 case S32CC_PERIPH_PLL:
107 *base = drv->periphpll_base;
108 break;
Ghennadi Procopciuc74dde092024-09-09 10:24:35 +0300109 case S32CC_DDR_PLL:
110 *base = drv->ddrpll_base;
111 break;
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +0300112 case S32CC_ARM_DFS:
113 *base = drv->armdfs_base;
114 break;
Ghennadi Procopciuceb26f082025-01-20 16:02:21 +0200115 case S32CC_PERIPH_DFS:
116 *base = drv->periphdfs_base;
117 break;
Ghennadi Procopciuc90c90002024-08-05 16:50:52 +0300118 case S32CC_CGM0:
119 *base = drv->cgm0_base;
120 break;
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300121 case S32CC_CGM1:
Ghennadi Procopciuca080f782024-06-12 14:44:47 +0300122 *base = drv->cgm1_base;
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300123 break;
Ghennadi Procopciucd89e32f2024-09-17 11:22:30 +0300124 case S32CC_CGM5:
125 *base = drv->cgm5_base;
126 break;
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300127 case S32CC_FIRC:
128 break;
129 case S32CC_SIRC:
130 break;
131 default:
132 ret = -EINVAL;
133 break;
134 }
135
136 if (ret != 0) {
137 ERROR("Unknown clock source id: %u\n", id);
138 }
139
140 return ret;
141}
142
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +0300143static void enable_fxosc(const struct s32cc_clk_drv *drv)
144{
145 uintptr_t fxosc_base = drv->fxosc_base;
146 uint32_t ctrl;
147
148 ctrl = mmio_read_32(FXOSC_CTRL(fxosc_base));
149 if ((ctrl & FXOSC_CTRL_OSCON) != U(0)) {
150 return;
151 }
152
153 ctrl = FXOSC_CTRL_COMP_EN;
154 ctrl &= ~FXOSC_CTRL_OSC_BYP;
155 ctrl |= FXOSC_CTRL_EOCV(0x1);
156 ctrl |= FXOSC_CTRL_GM_SEL(0x7);
157 mmio_write_32(FXOSC_CTRL(fxosc_base), ctrl);
158
159 /* Switch ON the crystal oscillator. */
160 mmio_setbits_32(FXOSC_CTRL(fxosc_base), FXOSC_CTRL_OSCON);
161
162 /* Wait until the clock is stable. */
163 while ((mmio_read_32(FXOSC_STAT(fxosc_base)) & FXOSC_STAT_OSC_STAT) == U(0)) {
164 }
165}
166
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300167static int enable_osc(struct s32cc_clk_obj *module,
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +0300168 const struct s32cc_clk_drv *drv,
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300169 unsigned int depth)
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +0300170{
171 const struct s32cc_osc *osc = s32cc_obj2osc(module);
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +0300172 unsigned int ldepth = depth;
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +0300173 int ret = 0;
174
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +0300175 ret = update_stack_depth(&ldepth);
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +0300176 if (ret != 0) {
177 return ret;
178 }
179
180 switch (osc->source) {
181 case S32CC_FXOSC:
182 enable_fxosc(drv);
183 break;
184 /* FIRC and SIRC oscillators are enabled by default */
185 case S32CC_FIRC:
186 break;
187 case S32CC_SIRC:
188 break;
189 default:
190 ERROR("Invalid oscillator %d\n", osc->source);
191 ret = -EINVAL;
192 break;
193 };
194
195 return ret;
196}
197
Ghennadi Procopciuc302831c2024-09-11 09:29:50 +0300198static struct s32cc_clk_obj *get_pll_parent(const struct s32cc_clk_obj *module)
199{
200 const struct s32cc_pll *pll = s32cc_obj2pll(module);
201
202 if (pll->source == NULL) {
203 ERROR("Failed to identify PLL's parent\n");
204 }
205
206 return pll->source;
207}
208
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300209static int get_pll_mfi_mfn(unsigned long pll_vco, unsigned long ref_freq,
210 uint32_t *mfi, uint32_t *mfn)
211
212{
213 unsigned long vco;
214 unsigned long mfn64;
215
216 /* FRAC-N mode */
217 *mfi = (uint32_t)(pll_vco / ref_freq);
218
219 /* MFN formula : (double)(pll_vco % ref_freq) / ref_freq * 18432.0 */
220 mfn64 = pll_vco % ref_freq;
221 mfn64 *= FP_PRECISION;
222 mfn64 /= ref_freq;
223 mfn64 *= 18432UL;
224 mfn64 /= FP_PRECISION;
225
226 if (mfn64 > UINT32_MAX) {
227 return -EINVAL;
228 }
229
230 *mfn = (uint32_t)mfn64;
231
232 vco = ((unsigned long)*mfn * FP_PRECISION) / 18432UL;
233 vco += (unsigned long)*mfi * FP_PRECISION;
234 vco *= ref_freq;
235 vco /= FP_PRECISION;
236
237 if (vco != pll_vco) {
238 ERROR("Failed to find MFI and MFN settings for PLL freq %lu. Nearest freq = %lu\n",
239 pll_vco, vco);
240 return -EINVAL;
241 }
242
243 return 0;
244}
245
246static struct s32cc_clkmux *get_pll_mux(const struct s32cc_pll *pll)
247{
248 const struct s32cc_clk_obj *source = pll->source;
249 const struct s32cc_clk *clk;
250
251 if (source == NULL) {
252 ERROR("Failed to identify PLL's parent\n");
253 return NULL;
254 }
255
256 if (source->type != s32cc_clk_t) {
257 ERROR("The parent of the PLL isn't a clock\n");
258 return NULL;
259 }
260
261 clk = s32cc_obj2clk(source);
262
263 if (clk->module == NULL) {
264 ERROR("The clock isn't connected to a module\n");
265 return NULL;
266 }
267
268 source = clk->module;
269
270 if ((source->type != s32cc_clkmux_t) &&
271 (source->type != s32cc_shared_clkmux_t)) {
272 ERROR("The parent of the PLL isn't a MUX\n");
273 return NULL;
274 }
275
276 return s32cc_obj2clkmux(source);
277}
278
279static void disable_odiv(uintptr_t pll_addr, uint32_t div_index)
280{
281 mmio_clrbits_32(PLLDIG_PLLODIV(pll_addr, div_index), PLLDIG_PLLODIV_DE);
282}
283
Ghennadi Procopciuc9efc7502024-06-12 14:30:30 +0300284static void enable_odiv(uintptr_t pll_addr, uint32_t div_index)
285{
286 mmio_setbits_32(PLLDIG_PLLODIV(pll_addr, div_index), PLLDIG_PLLODIV_DE);
287}
288
Ghennadi Procopciuc1d08c6d2025-01-15 14:59:22 +0200289static void enable_odivs(uintptr_t pll_addr, uint32_t ndivs, uint32_t mask)
290{
291 uint32_t i;
292
293 for (i = 0; i < ndivs; i++) {
294 if ((mask & BIT_32(i)) != 0U) {
295 enable_odiv(pll_addr, i);
296 }
297 }
298}
299
300static int adjust_odiv_settings(const struct s32cc_pll *pll, uintptr_t pll_addr,
301 uint32_t odivs_mask, unsigned long old_vco)
302{
303 uint64_t old_odiv_freq, odiv_freq;
304 uint32_t i, pllodiv, pdiv;
305 int ret = 0;
306
307 if (old_vco == 0UL) {
308 return 0;
309 }
310
311 for (i = 0; i < pll->ndividers; i++) {
312 if ((odivs_mask & BIT_32(i)) == 0U) {
313 continue;
314 }
315
316 pllodiv = mmio_read_32(PLLDIG_PLLODIV(pll_addr, i));
317
318 pdiv = PLLDIG_PLLODIV_DIV(pllodiv);
319
320 old_odiv_freq = ((old_vco * FP_PRECISION) / (pdiv + 1U)) / FP_PRECISION;
321 pdiv = (uint32_t)(pll->vco_freq * FP_PRECISION / old_odiv_freq / FP_PRECISION);
322
323 odiv_freq = pll->vco_freq * FP_PRECISION / pdiv / FP_PRECISION;
324
325 if (old_odiv_freq != odiv_freq) {
326 ERROR("Failed to adjust ODIV %" PRIu32 " to match previous frequency\n",
327 i);
328 }
329
330 pllodiv = PLLDIG_PLLODIV_DIV_SET(pdiv - 1U);
331 mmio_write_32(PLLDIG_PLLODIV(pll_addr, i), pllodiv);
332 }
333
334 return ret;
335}
336
337static uint32_t get_enabled_odivs(uintptr_t pll_addr, uint32_t ndivs)
338{
339 uint32_t mask = 0;
340 uint32_t pllodiv;
341 uint32_t i;
342
343 for (i = 0; i < ndivs; i++) {
344 pllodiv = mmio_read_32(PLLDIG_PLLODIV(pll_addr, i));
345 if ((pllodiv & PLLDIG_PLLODIV_DE) != 0U) {
346 mask |= BIT_32(i);
347 }
348 }
349
350 return mask;
351}
352
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300353static void disable_odivs(uintptr_t pll_addr, uint32_t ndivs)
354{
355 uint32_t i;
356
357 for (i = 0; i < ndivs; i++) {
358 disable_odiv(pll_addr, i);
359 }
360}
361
362static void enable_pll_hw(uintptr_t pll_addr)
363{
364 /* Enable the PLL. */
365 mmio_write_32(PLLDIG_PLLCR(pll_addr), 0x0);
366
367 /* Poll until PLL acquires lock. */
368 while ((mmio_read_32(PLLDIG_PLLSR(pll_addr)) & PLLDIG_PLLSR_LOCK) == 0U) {
369 }
370}
371
372static void disable_pll_hw(uintptr_t pll_addr)
373{
374 mmio_write_32(PLLDIG_PLLCR(pll_addr), PLLDIG_PLLCR_PLLPD);
375}
376
Ghennadi Procopciuc1d08c6d2025-01-15 14:59:22 +0200377static bool is_pll_enabled(uintptr_t pll_base)
378{
379 uint32_t pllcr, pllsr;
380
381 pllcr = mmio_read_32(PLLDIG_PLLCR(pll_base));
382 pllsr = mmio_read_32(PLLDIG_PLLSR(pll_base));
383
384 /* Enabled and locked PLL */
385 if ((pllcr & PLLDIG_PLLCR_PLLPD) != 0U) {
386 return false;
387 }
388
389 if ((pllsr & PLLDIG_PLLSR_LOCK) == 0U) {
390 return false;
391 }
392
393 return true;
394}
395
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300396static int program_pll(const struct s32cc_pll *pll, uintptr_t pll_addr,
397 const struct s32cc_clk_drv *drv, uint32_t sclk_id,
Ghennadi Procopciuc1d08c6d2025-01-15 14:59:22 +0200398 unsigned long sclk_freq, unsigned int depth)
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300399{
400 uint32_t rdiv = 1, mfi, mfn;
Ghennadi Procopciuc1d08c6d2025-01-15 14:59:22 +0200401 unsigned long old_vco = 0UL;
402 unsigned int ldepth = depth;
403 uint32_t odivs_mask;
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300404 int ret;
405
Ghennadi Procopciuc1d08c6d2025-01-15 14:59:22 +0200406 ret = update_stack_depth(&ldepth);
407 if (ret != 0) {
408 return ret;
409 }
410
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300411 ret = get_pll_mfi_mfn(pll->vco_freq, sclk_freq, &mfi, &mfn);
412 if (ret != 0) {
413 return -EINVAL;
414 }
415
Ghennadi Procopciuc1d08c6d2025-01-15 14:59:22 +0200416 odivs_mask = get_enabled_odivs(pll_addr, pll->ndividers);
417
418 if (is_pll_enabled(pll_addr)) {
419 ret = get_module_rate(&pll->desc, drv, &old_vco, ldepth);
420 if (ret != 0) {
421 return ret;
422 }
423 }
424
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300425 /* Disable ODIVs*/
426 disable_odivs(pll_addr, pll->ndividers);
427
428 /* Disable PLL */
429 disable_pll_hw(pll_addr);
430
431 /* Program PLLCLKMUX */
432 mmio_write_32(PLLDIG_PLLCLKMUX(pll_addr), sclk_id);
433
434 /* Program VCO */
435 mmio_clrsetbits_32(PLLDIG_PLLDV(pll_addr),
436 PLLDIG_PLLDV_RDIV_MASK | PLLDIG_PLLDV_MFI_MASK,
437 PLLDIG_PLLDV_RDIV_SET(rdiv) | PLLDIG_PLLDV_MFI(mfi));
438
439 mmio_write_32(PLLDIG_PLLFD(pll_addr),
440 PLLDIG_PLLFD_MFN_SET(mfn) | PLLDIG_PLLFD_SMDEN);
441
Ghennadi Procopciuc1d08c6d2025-01-15 14:59:22 +0200442 ret = adjust_odiv_settings(pll, pll_addr, odivs_mask, old_vco);
443 if (ret != 0) {
444 return ret;
445 }
446
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300447 enable_pll_hw(pll_addr);
448
Ghennadi Procopciuc1d08c6d2025-01-15 14:59:22 +0200449 /* Enable out dividers */
450 enable_odivs(pll_addr, pll->ndividers, odivs_mask);
451
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300452 return ret;
453}
454
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300455static int enable_pll(struct s32cc_clk_obj *module,
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300456 const struct s32cc_clk_drv *drv,
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300457 unsigned int depth)
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300458{
459 const struct s32cc_pll *pll = s32cc_obj2pll(module);
Ghennadi Procopciuc1d08c6d2025-01-15 14:59:22 +0200460 unsigned int clk_src, ldepth = depth;
461 unsigned long sclk_freq, pll_vco;
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300462 const struct s32cc_clkmux *mux;
463 uintptr_t pll_addr = UL(0x0);
Ghennadi Procopciuc1d08c6d2025-01-15 14:59:22 +0200464 bool pll_enabled;
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300465 uint32_t sclk_id;
466 int ret;
467
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +0300468 ret = update_stack_depth(&ldepth);
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300469 if (ret != 0) {
470 return ret;
471 }
472
473 mux = get_pll_mux(pll);
474 if (mux == NULL) {
475 return -EINVAL;
476 }
477
478 if (pll->instance != mux->module) {
479 ERROR("MUX type is not in sync with PLL ID\n");
480 return -EINVAL;
481 }
482
483 ret = get_base_addr(pll->instance, drv, &pll_addr);
484 if (ret != 0) {
485 ERROR("Failed to detect PLL instance\n");
486 return ret;
487 }
488
489 switch (mux->source_id) {
490 case S32CC_CLK_FIRC:
491 sclk_freq = 48U * MHZ;
492 sclk_id = 0;
493 break;
494 case S32CC_CLK_FXOSC:
495 sclk_freq = 40U * MHZ;
496 sclk_id = 1;
497 break;
498 default:
499 ERROR("Invalid source selection for PLL 0x%lx\n",
500 pll_addr);
501 return -EINVAL;
502 };
503
Ghennadi Procopciuc1d08c6d2025-01-15 14:59:22 +0200504 ret = get_module_rate(&pll->desc, drv, &pll_vco, depth);
505 if (ret != 0) {
506 return ret;
507 }
508
509 pll_enabled = is_pll_enabled(pll_addr);
510 clk_src = mmio_read_32(PLLDIG_PLLCLKMUX(pll_addr));
511
512 if ((clk_src == sclk_id) && pll_enabled &&
513 (pll_vco == pll->vco_freq)) {
514 return 0;
515 }
516
517 return program_pll(pll, pll_addr, drv, sclk_id, sclk_freq, ldepth);
Ghennadi Procopciucb390c4d2024-06-12 14:21:39 +0300518}
519
Ghennadi Procopciuc9efc7502024-06-12 14:30:30 +0300520static inline struct s32cc_pll *get_div_pll(const struct s32cc_pll_out_div *pdiv)
521{
522 const struct s32cc_clk_obj *parent;
523
524 parent = pdiv->parent;
525 if (parent == NULL) {
526 ERROR("Failed to identify PLL divider's parent\n");
527 return NULL;
528 }
529
530 if (parent->type != s32cc_pll_t) {
531 ERROR("The parent of the divider is not a PLL instance\n");
532 return NULL;
533 }
534
535 return s32cc_obj2pll(parent);
536}
537
538static void config_pll_out_div(uintptr_t pll_addr, uint32_t div_index, uint32_t dc)
539{
540 uint32_t pllodiv;
541 uint32_t pdiv;
542
543 pllodiv = mmio_read_32(PLLDIG_PLLODIV(pll_addr, div_index));
544 pdiv = PLLDIG_PLLODIV_DIV(pllodiv);
545
546 if (((pdiv + 1U) == dc) && ((pllodiv & PLLDIG_PLLODIV_DE) != 0U)) {
547 return;
548 }
549
550 if ((pllodiv & PLLDIG_PLLODIV_DE) != 0U) {
551 disable_odiv(pll_addr, div_index);
552 }
553
554 pllodiv = PLLDIG_PLLODIV_DIV_SET(dc - 1U);
555 mmio_write_32(PLLDIG_PLLODIV(pll_addr, div_index), pllodiv);
556
557 enable_odiv(pll_addr, div_index);
558}
559
Ghennadi Procopciuc302831c2024-09-11 09:29:50 +0300560static struct s32cc_clk_obj *get_pll_div_parent(const struct s32cc_clk_obj *module)
561{
562 const struct s32cc_pll_out_div *pdiv = s32cc_obj2plldiv(module);
563
564 if (pdiv->parent == NULL) {
565 ERROR("Failed to identify PLL DIV's parent\n");
566 }
567
568 return pdiv->parent;
569}
570
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300571static int enable_pll_div(struct s32cc_clk_obj *module,
Ghennadi Procopciuc9efc7502024-06-12 14:30:30 +0300572 const struct s32cc_clk_drv *drv,
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300573 unsigned int depth)
Ghennadi Procopciuc9efc7502024-06-12 14:30:30 +0300574{
575 const struct s32cc_pll_out_div *pdiv = s32cc_obj2plldiv(module);
576 uintptr_t pll_addr = 0x0ULL;
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +0300577 unsigned int ldepth = depth;
Ghennadi Procopciuc9efc7502024-06-12 14:30:30 +0300578 const struct s32cc_pll *pll;
Ghennadi Procopciuc1d08c6d2025-01-15 14:59:22 +0200579 unsigned long pll_vco;
Ghennadi Procopciuc9efc7502024-06-12 14:30:30 +0300580 uint32_t dc;
581 int ret;
582
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +0300583 ret = update_stack_depth(&ldepth);
Ghennadi Procopciuc9efc7502024-06-12 14:30:30 +0300584 if (ret != 0) {
585 return ret;
586 }
587
588 pll = get_div_pll(pdiv);
589 if (pll == NULL) {
590 ERROR("The parent of the PLL DIV is invalid\n");
591 return 0;
592 }
593
594 ret = get_base_addr(pll->instance, drv, &pll_addr);
595 if (ret != 0) {
596 ERROR("Failed to detect PLL instance\n");
597 return -EINVAL;
598 }
599
Ghennadi Procopciuc1d08c6d2025-01-15 14:59:22 +0200600 ret = get_module_rate(&pll->desc, drv, &pll_vco, ldepth);
601 if (ret != 0) {
602 ERROR("Failed to enable the PLL due to unknown rate for 0x%" PRIxPTR "\n",
603 pll_addr);
604 return ret;
605 }
606
607 dc = (uint32_t)(pll_vco / pdiv->freq);
Ghennadi Procopciuc9efc7502024-06-12 14:30:30 +0300608
609 config_pll_out_div(pll_addr, pdiv->index, dc);
610
611 return 0;
612}
613
Ghennadi Procopciuca080f782024-06-12 14:44:47 +0300614static int cgm_mux_clk_config(uintptr_t cgm_addr, uint32_t mux, uint32_t source,
615 bool safe_clk)
616{
617 uint32_t css, csc;
618
619 css = mmio_read_32(CGM_MUXn_CSS(cgm_addr, mux));
620
621 /* Already configured */
622 if ((MC_CGM_MUXn_CSS_SELSTAT(css) == source) &&
623 (MC_CGM_MUXn_CSS_SWTRG(css) == MC_CGM_MUXn_CSS_SWTRG_SUCCESS) &&
624 ((css & MC_CGM_MUXn_CSS_SWIP) == 0U) && !safe_clk) {
625 return 0;
626 }
627
628 /* Ongoing clock switch? */
629 while ((mmio_read_32(CGM_MUXn_CSS(cgm_addr, mux)) &
630 MC_CGM_MUXn_CSS_SWIP) != 0U) {
631 }
632
633 csc = mmio_read_32(CGM_MUXn_CSC(cgm_addr, mux));
634
635 /* Clear previous source. */
636 csc &= ~(MC_CGM_MUXn_CSC_SELCTL_MASK);
637
638 if (!safe_clk) {
639 /* Select the clock source and trigger the clock switch. */
640 csc |= MC_CGM_MUXn_CSC_SELCTL(source) | MC_CGM_MUXn_CSC_CLK_SW;
641 } else {
642 /* Switch to safe clock */
643 csc |= MC_CGM_MUXn_CSC_SAFE_SW;
644 }
645
646 mmio_write_32(CGM_MUXn_CSC(cgm_addr, mux), csc);
647
648 /* Wait for configuration bit to auto-clear. */
649 while ((mmio_read_32(CGM_MUXn_CSC(cgm_addr, mux)) &
650 MC_CGM_MUXn_CSC_CLK_SW) != 0U) {
651 }
652
653 /* Is the clock switch completed? */
654 while ((mmio_read_32(CGM_MUXn_CSS(cgm_addr, mux)) &
655 MC_CGM_MUXn_CSS_SWIP) != 0U) {
656 }
657
658 /*
659 * Check if the switch succeeded.
660 * Check switch trigger cause and the source.
661 */
662 css = mmio_read_32(CGM_MUXn_CSS(cgm_addr, mux));
663 if (!safe_clk) {
664 if ((MC_CGM_MUXn_CSS_SWTRG(css) == MC_CGM_MUXn_CSS_SWTRG_SUCCESS) &&
665 (MC_CGM_MUXn_CSS_SELSTAT(css) == source)) {
666 return 0;
667 }
668
669 ERROR("Failed to change the source of mux %" PRIu32 " to %" PRIu32 " (CGM=%lu)\n",
670 mux, source, cgm_addr);
671 } else {
672 if (((MC_CGM_MUXn_CSS_SWTRG(css) == MC_CGM_MUXn_CSS_SWTRG_SAFE_CLK) ||
673 (MC_CGM_MUXn_CSS_SWTRG(css) == MC_CGM_MUXn_CSS_SWTRG_SAFE_CLK_INACTIVE)) &&
674 ((MC_CGM_MUXn_CSS_SAFE_SW & css) != 0U)) {
675 return 0;
676 }
677
678 ERROR("The switch of mux %" PRIu32 " (CGM=%lu) to safe clock failed\n",
679 mux, cgm_addr);
680 }
681
682 return -EINVAL;
683}
684
685static int enable_cgm_mux(const struct s32cc_clkmux *mux,
686 const struct s32cc_clk_drv *drv)
687{
688 uintptr_t cgm_addr = UL(0x0);
689 uint32_t mux_hw_clk;
690 int ret;
691
692 ret = get_base_addr(mux->module, drv, &cgm_addr);
693 if (ret != 0) {
694 return ret;
695 }
696
697 mux_hw_clk = (uint32_t)S32CC_CLK_ID(mux->source_id);
698
699 return cgm_mux_clk_config(cgm_addr, mux->index,
700 mux_hw_clk, false);
701}
702
Ghennadi Procopciuc302831c2024-09-11 09:29:50 +0300703static struct s32cc_clk_obj *get_mux_parent(const struct s32cc_clk_obj *module)
704{
705 const struct s32cc_clkmux *mux = s32cc_obj2clkmux(module);
706 struct s32cc_clk *clk;
707
708 if (mux == NULL) {
709 return NULL;
710 }
711
712 clk = s32cc_get_arch_clk(mux->source_id);
713 if (clk == NULL) {
714 ERROR("Invalid parent (%lu) for mux %" PRIu8 "\n",
715 mux->source_id, mux->index);
716 return NULL;
717 }
718
719 return &clk->desc;
720}
721
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300722static int enable_mux(struct s32cc_clk_obj *module,
Ghennadi Procopciuca080f782024-06-12 14:44:47 +0300723 const struct s32cc_clk_drv *drv,
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300724 unsigned int depth)
Ghennadi Procopciuca080f782024-06-12 14:44:47 +0300725{
726 const struct s32cc_clkmux *mux = s32cc_obj2clkmux(module);
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +0300727 unsigned int ldepth = depth;
Ghennadi Procopciuca080f782024-06-12 14:44:47 +0300728 const struct s32cc_clk *clk;
729 int ret = 0;
730
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +0300731 ret = update_stack_depth(&ldepth);
Ghennadi Procopciuca080f782024-06-12 14:44:47 +0300732 if (ret != 0) {
733 return ret;
734 }
735
736 if (mux == NULL) {
737 return -EINVAL;
738 }
739
740 clk = s32cc_get_arch_clk(mux->source_id);
741 if (clk == NULL) {
742 ERROR("Invalid parent (%lu) for mux %" PRIu8 "\n",
743 mux->source_id, mux->index);
744 return -EINVAL;
745 }
746
747 switch (mux->module) {
748 /* PLL mux will be enabled by PLL setup */
749 case S32CC_ARM_PLL:
Ghennadi Procopciuc9de0c112024-09-11 14:54:57 +0300750 case S32CC_PERIPH_PLL:
Ghennadi Procopciuc74dde092024-09-09 10:24:35 +0300751 case S32CC_DDR_PLL:
Ghennadi Procopciuca080f782024-06-12 14:44:47 +0300752 break;
753 case S32CC_CGM1:
754 ret = enable_cgm_mux(mux, drv);
755 break;
Ghennadi Procopciuc90c90002024-08-05 16:50:52 +0300756 case S32CC_CGM0:
757 ret = enable_cgm_mux(mux, drv);
758 break;
Ghennadi Procopciucd89e32f2024-09-17 11:22:30 +0300759 case S32CC_CGM5:
760 ret = enable_cgm_mux(mux, drv);
761 break;
Ghennadi Procopciuca080f782024-06-12 14:44:47 +0300762 default:
763 ERROR("Unknown mux parent type: %d\n", mux->module);
764 ret = -EINVAL;
765 break;
766 };
767
768 return ret;
769}
770
Ghennadi Procopciuc302831c2024-09-11 09:29:50 +0300771static struct s32cc_clk_obj *get_dfs_parent(const struct s32cc_clk_obj *module)
772{
773 const struct s32cc_dfs *dfs = s32cc_obj2dfs(module);
774
775 if (dfs->parent == NULL) {
776 ERROR("Failed to identify DFS's parent\n");
777 }
778
779 return dfs->parent;
780}
781
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300782static int enable_dfs(struct s32cc_clk_obj *module,
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +0300783 const struct s32cc_clk_drv *drv,
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300784 unsigned int depth)
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +0300785{
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +0300786 unsigned int ldepth = depth;
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +0300787 int ret = 0;
788
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +0300789 ret = update_stack_depth(&ldepth);
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +0300790 if (ret != 0) {
791 return ret;
792 }
793
794 return 0;
795}
796
Ghennadi Procopciuc393f79b2025-01-13 09:50:51 +0200797static int get_dfs_freq(const struct s32cc_clk_obj *module,
798 const struct s32cc_clk_drv *drv,
799 unsigned long *rate, unsigned int depth)
800{
801 const struct s32cc_dfs *dfs = s32cc_obj2dfs(module);
802 unsigned int ldepth = depth;
803 uintptr_t dfs_addr;
804 int ret;
805
806 ret = update_stack_depth(&ldepth);
807 if (ret != 0) {
808 return ret;
809 }
810
811 ret = get_base_addr(dfs->instance, drv, &dfs_addr);
812 if (ret != 0) {
813 ERROR("Failed to detect the DFS instance\n");
814 return ret;
815 }
816
817 return get_module_rate(dfs->parent, drv, rate, ldepth);
818}
819
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +0300820static struct s32cc_dfs *get_div_dfs(const struct s32cc_dfs_div *dfs_div)
821{
822 const struct s32cc_clk_obj *parent = dfs_div->parent;
823
824 if (parent->type != s32cc_dfs_t) {
825 ERROR("DFS DIV doesn't have a DFS as parent\n");
826 return NULL;
827 }
828
829 return s32cc_obj2dfs(parent);
830}
831
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +0300832static int get_dfs_mfi_mfn(unsigned long dfs_freq, const struct s32cc_dfs_div *dfs_div,
833 uint32_t *mfi, uint32_t *mfn)
834{
835 uint64_t factor64, tmp64, ofreq;
836 uint32_t factor32;
837
838 unsigned long in = dfs_freq;
839 unsigned long out = dfs_div->freq;
840
841 /**
842 * factor = (IN / OUT) / 2
843 * MFI = integer(factor)
844 * MFN = (factor - MFI) * 36
845 */
846 factor64 = ((((uint64_t)in) * FP_PRECISION) / ((uint64_t)out)) / 2ULL;
847 tmp64 = factor64 / FP_PRECISION;
848 if (tmp64 > UINT32_MAX) {
849 return -EINVAL;
850 }
851
852 factor32 = (uint32_t)tmp64;
853 *mfi = factor32;
854
855 tmp64 = ((factor64 - ((uint64_t)*mfi * FP_PRECISION)) * 36UL) / FP_PRECISION;
856 if (tmp64 > UINT32_MAX) {
857 return -EINVAL;
858 }
859
860 *mfn = (uint32_t)tmp64;
861
862 /* div_freq = in / (2 * (*mfi + *mfn / 36.0)) */
863 factor64 = (((uint64_t)*mfn) * FP_PRECISION) / 36ULL;
864 factor64 += ((uint64_t)*mfi) * FP_PRECISION;
865 factor64 *= 2ULL;
866 ofreq = (((uint64_t)in) * FP_PRECISION) / factor64;
867
868 if (ofreq != dfs_div->freq) {
869 ERROR("Failed to find MFI and MFN settings for DFS DIV freq %lu\n",
870 dfs_div->freq);
871 ERROR("Nearest freq = %" PRIx64 "\n", ofreq);
872 return -EINVAL;
873 }
874
875 return 0;
876}
877
878static int init_dfs_port(uintptr_t dfs_addr, uint32_t port,
879 uint32_t mfi, uint32_t mfn)
880{
881 uint32_t portsr, portolsr;
882 uint32_t mask, old_mfi, old_mfn;
883 uint32_t dvport;
884 bool init_dfs;
885
886 dvport = mmio_read_32(DFS_DVPORTn(dfs_addr, port));
887
888 old_mfi = DFS_DVPORTn_MFI(dvport);
889 old_mfn = DFS_DVPORTn_MFN(dvport);
890
891 portsr = mmio_read_32(DFS_PORTSR(dfs_addr));
892 portolsr = mmio_read_32(DFS_PORTOLSR(dfs_addr));
893
894 /* Skip configuration if it's not needed */
895 if (((portsr & BIT_32(port)) != 0U) &&
896 ((portolsr & BIT_32(port)) == 0U) &&
897 (mfi == old_mfi) && (mfn == old_mfn)) {
898 return 0;
899 }
900
901 init_dfs = (portsr == 0U);
902
903 if (init_dfs) {
904 mask = DFS_PORTRESET_MASK;
905 } else {
906 mask = DFS_PORTRESET_SET(BIT_32(port));
907 }
908
909 mmio_write_32(DFS_PORTOLSR(dfs_addr), mask);
910 mmio_write_32(DFS_PORTRESET(dfs_addr), mask);
911
912 while ((mmio_read_32(DFS_PORTSR(dfs_addr)) & mask) != 0U) {
913 }
914
915 if (init_dfs) {
916 mmio_write_32(DFS_CTL(dfs_addr), DFS_CTL_RESET);
917 }
918
919 mmio_write_32(DFS_DVPORTn(dfs_addr, port),
920 DFS_DVPORTn_MFI_SET(mfi) | DFS_DVPORTn_MFN_SET(mfn));
921
922 if (init_dfs) {
923 /* DFS clk enable programming */
924 mmio_clrbits_32(DFS_CTL(dfs_addr), DFS_CTL_RESET);
925 }
926
927 mmio_clrbits_32(DFS_PORTRESET(dfs_addr), BIT_32(port));
928
929 while ((mmio_read_32(DFS_PORTSR(dfs_addr)) & BIT_32(port)) != BIT_32(port)) {
930 }
931
932 portolsr = mmio_read_32(DFS_PORTOLSR(dfs_addr));
933 if ((portolsr & DFS_PORTOLSR_LOL(port)) != 0U) {
934 ERROR("Failed to lock DFS divider\n");
935 return -EINVAL;
936 }
937
938 return 0;
939}
940
Ghennadi Procopciuc302831c2024-09-11 09:29:50 +0300941static struct s32cc_clk_obj *
942get_dfs_div_parent(const struct s32cc_clk_obj *module)
943{
944 const struct s32cc_dfs_div *dfs_div = s32cc_obj2dfsdiv(module);
945
946 if (dfs_div->parent == NULL) {
947 ERROR("Failed to identify DFS divider's parent\n");
948 }
949
950 return dfs_div->parent;
951}
952
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300953static int enable_dfs_div(struct s32cc_clk_obj *module,
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +0300954 const struct s32cc_clk_drv *drv,
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300955 unsigned int depth)
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +0300956{
957 const struct s32cc_dfs_div *dfs_div = s32cc_obj2dfsdiv(module);
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +0300958 unsigned int ldepth = depth;
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +0300959 const struct s32cc_dfs *dfs;
960 uintptr_t dfs_addr = 0UL;
Ghennadi Procopciuce6f4b322025-01-15 14:57:58 +0200961 unsigned long dfs_freq;
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +0300962 uint32_t mfi, mfn;
963 int ret = 0;
964
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +0300965 ret = update_stack_depth(&ldepth);
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +0300966 if (ret != 0) {
967 return ret;
968 }
969
970 dfs = get_div_dfs(dfs_div);
971 if (dfs == NULL) {
972 return -EINVAL;
973 }
974
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +0300975 ret = get_base_addr(dfs->instance, drv, &dfs_addr);
976 if ((ret != 0) || (dfs_addr == 0UL)) {
977 return -EINVAL;
978 }
979
Ghennadi Procopciuce6f4b322025-01-15 14:57:58 +0200980 ret = get_module_rate(&dfs->desc, drv, &dfs_freq, depth);
981 if (ret != 0) {
982 return ret;
983 }
984
985 ret = get_dfs_mfi_mfn(dfs_freq, dfs_div, &mfi, &mfn);
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +0300986 if (ret != 0) {
987 return -EINVAL;
988 }
989
990 return init_dfs_port(dfs_addr, dfs_div->index, mfi, mfn);
991}
992
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +0300993typedef int (*enable_clk_t)(struct s32cc_clk_obj *module,
994 const struct s32cc_clk_drv *drv,
995 unsigned int depth);
996
Ghennadi Procopciucd89e32f2024-09-17 11:22:30 +0300997static int enable_part(struct s32cc_clk_obj *module,
998 const struct s32cc_clk_drv *drv,
999 unsigned int depth)
1000{
1001 const struct s32cc_part *part = s32cc_obj2part(module);
1002 uint32_t part_no = part->partition_id;
1003
1004 if ((drv->mc_me == 0UL) || (drv->mc_rgm == 0UL) || (drv->rdc == 0UL)) {
1005 return -EINVAL;
1006 }
1007
1008 return mc_me_enable_partition(drv->mc_me, drv->mc_rgm, drv->rdc, part_no);
1009}
1010
1011static int enable_part_block(struct s32cc_clk_obj *module,
1012 const struct s32cc_clk_drv *drv,
1013 unsigned int depth)
1014{
1015 const struct s32cc_part_block *block = s32cc_obj2partblock(module);
1016 const struct s32cc_part *part = block->part;
1017 uint32_t part_no = part->partition_id;
1018 unsigned int ldepth = depth;
1019 uint32_t cofb;
1020 int ret;
1021
1022 ret = update_stack_depth(&ldepth);
1023 if (ret != 0) {
1024 return ret;
1025 }
1026
1027 if ((block->block >= s32cc_part_block0) &&
1028 (block->block <= s32cc_part_block15)) {
1029 cofb = (uint32_t)block->block - (uint32_t)s32cc_part_block0;
1030 mc_me_enable_part_cofb(drv->mc_me, part_no, cofb, block->status);
1031 } else {
1032 ERROR("Unknown partition block type: %d\n", block->block);
1033 return -EINVAL;
1034 }
1035
1036 return 0;
1037}
1038
1039static struct s32cc_clk_obj *
1040get_part_block_parent(const struct s32cc_clk_obj *module)
1041{
1042 const struct s32cc_part_block *block = s32cc_obj2partblock(module);
1043
1044 return &block->part->desc;
1045}
1046
1047static int enable_module_with_refcount(struct s32cc_clk_obj *module,
1048 const struct s32cc_clk_drv *drv,
1049 unsigned int depth);
1050
1051static int enable_part_block_link(struct s32cc_clk_obj *module,
1052 const struct s32cc_clk_drv *drv,
1053 unsigned int depth)
1054{
1055 const struct s32cc_part_block_link *link = s32cc_obj2partblocklink(module);
1056 struct s32cc_part_block *block = link->block;
1057 unsigned int ldepth = depth;
1058 int ret;
1059
1060 ret = update_stack_depth(&ldepth);
1061 if (ret != 0) {
1062 return ret;
1063 }
1064
1065 /* Move the enablement algorithm to partition tree */
1066 return enable_module_with_refcount(&block->desc, drv, ldepth);
1067}
1068
1069static struct s32cc_clk_obj *
1070get_part_block_link_parent(const struct s32cc_clk_obj *module)
1071{
1072 const struct s32cc_part_block_link *link = s32cc_obj2partblocklink(module);
1073
1074 return link->parent;
1075}
1076
Ghennadi Procopciuc244b5442025-01-13 12:00:50 +02001077static int get_part_block_link_freq(const struct s32cc_clk_obj *module,
1078 const struct s32cc_clk_drv *drv,
1079 unsigned long *rate, unsigned int depth)
1080{
1081 const struct s32cc_part_block_link *block = s32cc_obj2partblocklink(module);
1082 unsigned int ldepth = depth;
1083 int ret;
1084
1085 ret = update_stack_depth(&ldepth);
1086 if (ret != 0) {
1087 return ret;
1088 }
1089
1090 return get_module_rate(block->parent, drv, rate, ldepth);
1091}
1092
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001093static int no_enable(struct s32cc_clk_obj *module,
1094 const struct s32cc_clk_drv *drv,
1095 unsigned int depth)
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +03001096{
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001097 return 0;
1098}
1099
1100static int exec_cb_with_refcount(enable_clk_t en_cb, struct s32cc_clk_obj *mod,
1101 const struct s32cc_clk_drv *drv, bool leaf_node,
1102 unsigned int depth)
1103{
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +03001104 unsigned int ldepth = depth;
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001105 int ret = 0;
1106
1107 if (mod == NULL) {
1108 return 0;
1109 }
1110
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +03001111 ret = update_stack_depth(&ldepth);
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001112 if (ret != 0) {
1113 return ret;
1114 }
1115
1116 /* Refcount will be updated as part of the recursivity */
1117 if (leaf_node) {
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +03001118 return en_cb(mod, drv, ldepth);
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001119 }
1120
1121 if (mod->refcount == 0U) {
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +03001122 ret = en_cb(mod, drv, ldepth);
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001123 }
1124
1125 if (ret == 0) {
1126 mod->refcount++;
1127 }
1128
1129 return ret;
1130}
1131
1132static struct s32cc_clk_obj *get_module_parent(const struct s32cc_clk_obj *module);
1133
1134static int enable_module(struct s32cc_clk_obj *module,
1135 const struct s32cc_clk_drv *drv,
1136 unsigned int depth)
1137{
1138 struct s32cc_clk_obj *parent = get_module_parent(module);
Ghennadi Procopciuc224a0762025-01-23 10:34:35 +02001139 static const enable_clk_t enable_clbs[13] = {
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001140 [s32cc_clk_t] = no_enable,
1141 [s32cc_osc_t] = enable_osc,
1142 [s32cc_pll_t] = enable_pll,
1143 [s32cc_pll_out_div_t] = enable_pll_div,
1144 [s32cc_clkmux_t] = enable_mux,
1145 [s32cc_shared_clkmux_t] = enable_mux,
1146 [s32cc_dfs_t] = enable_dfs,
1147 [s32cc_dfs_div_t] = enable_dfs_div,
Ghennadi Procopciucd89e32f2024-09-17 11:22:30 +03001148 [s32cc_part_t] = enable_part,
1149 [s32cc_part_block_t] = enable_part_block,
1150 [s32cc_part_block_link_t] = enable_part_block_link,
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001151 };
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +03001152 unsigned int ldepth = depth;
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001153 uint32_t index;
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +03001154 int ret = 0;
1155
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +03001156 ret = update_stack_depth(&ldepth);
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +03001157 if (ret != 0) {
1158 return ret;
1159 }
1160
1161 if (drv == NULL) {
1162 return -EINVAL;
1163 }
1164
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001165 index = (uint32_t)module->type;
1166
1167 if (index >= ARRAY_SIZE(enable_clbs)) {
1168 ERROR("Undefined module type: %d\n", module->type);
1169 return -EINVAL;
1170 }
1171
1172 if (enable_clbs[index] == NULL) {
1173 ERROR("Undefined callback for the clock type: %d\n",
1174 module->type);
1175 return -EINVAL;
1176 }
1177
1178 parent = get_module_parent(module);
1179
1180 ret = exec_cb_with_refcount(enable_module, parent, drv,
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +03001181 false, ldepth);
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001182 if (ret != 0) {
1183 return ret;
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +03001184 }
1185
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001186 ret = exec_cb_with_refcount(enable_clbs[index], module, drv,
Ghennadi Procopciuc8486f7f2024-09-30 09:39:15 +03001187 true, ldepth);
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001188 if (ret != 0) {
1189 return ret;
1190 }
1191
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +03001192 return ret;
1193}
1194
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001195static int enable_module_with_refcount(struct s32cc_clk_obj *module,
1196 const struct s32cc_clk_drv *drv,
1197 unsigned int depth)
1198{
1199 return exec_cb_with_refcount(enable_module, module, drv, false, depth);
1200}
1201
Ghennadi Procopciucfc26eb02024-06-11 18:39:58 +03001202static int s32cc_clk_enable(unsigned long id)
1203{
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001204 const struct s32cc_clk_drv *drv = get_drv();
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +03001205 unsigned int depth = MAX_STACK_DEPTH;
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001206 struct s32cc_clk *clk;
Ghennadi Procopciuc9dee8e42024-06-12 09:25:17 +03001207
1208 clk = s32cc_get_arch_clk(id);
1209 if (clk == NULL) {
1210 return -EINVAL;
1211 }
1212
Ghennadi Procopciuceaf9cb12024-09-09 13:00:26 +03001213 return enable_module_with_refcount(&clk->desc, drv, depth);
Ghennadi Procopciucfc26eb02024-06-11 18:39:58 +03001214}
1215
1216static void s32cc_clk_disable(unsigned long id)
1217{
1218}
1219
1220static bool s32cc_clk_is_enabled(unsigned long id)
1221{
1222 return false;
1223}
1224
Ghennadi Procopciuc0d525772024-06-12 08:09:19 +03001225static int set_osc_freq(const struct s32cc_clk_obj *module, unsigned long rate,
1226 unsigned long *orate, unsigned int *depth)
1227{
1228 struct s32cc_osc *osc = s32cc_obj2osc(module);
1229 int ret;
1230
1231 ret = update_stack_depth(depth);
1232 if (ret != 0) {
1233 return ret;
1234 }
1235
1236 if ((osc->freq != 0UL) && (rate != osc->freq)) {
1237 ERROR("Already initialized oscillator. freq = %lu\n",
1238 osc->freq);
1239 return -EINVAL;
1240 }
1241
1242 osc->freq = rate;
1243 *orate = osc->freq;
1244
1245 return 0;
1246}
1247
Ghennadi Procopciuc7d927632025-01-10 16:26:21 +02001248static int get_osc_freq(const struct s32cc_clk_obj *module,
1249 const struct s32cc_clk_drv *drv,
1250 unsigned long *rate, unsigned int depth)
1251{
1252 const struct s32cc_osc *osc = s32cc_obj2osc(module);
1253 unsigned int ldepth = depth;
1254 int ret;
1255
1256 ret = update_stack_depth(&ldepth);
1257 if (ret != 0) {
1258 return ret;
1259 }
1260
1261 if (osc->freq == 0UL) {
1262 ERROR("Uninitialized oscillator\n");
1263 return -EINVAL;
1264 }
1265
1266 *rate = osc->freq;
1267
1268 return 0;
1269}
1270
Ghennadi Procopciuc0d525772024-06-12 08:09:19 +03001271static int set_clk_freq(const struct s32cc_clk_obj *module, unsigned long rate,
1272 unsigned long *orate, unsigned int *depth)
1273{
1274 const struct s32cc_clk *clk = s32cc_obj2clk(module);
1275 int ret;
1276
1277 ret = update_stack_depth(depth);
1278 if (ret != 0) {
1279 return ret;
1280 }
1281
1282 if ((clk->min_freq != 0UL) && (clk->max_freq != 0UL) &&
1283 ((rate < clk->min_freq) || (rate > clk->max_freq))) {
1284 ERROR("%lu frequency is out of the allowed range: [%lu:%lu]\n",
1285 rate, clk->min_freq, clk->max_freq);
1286 return -EINVAL;
1287 }
1288
1289 if (clk->module != NULL) {
1290 return set_module_rate(clk->module, rate, orate, depth);
1291 }
1292
1293 if (clk->pclock != NULL) {
1294 return set_clk_freq(&clk->pclock->desc, rate, orate, depth);
1295 }
1296
1297 return -EINVAL;
1298}
1299
Ghennadi Procopciucdba9d3d2025-01-10 16:33:36 +02001300static int get_clk_freq(const struct s32cc_clk_obj *module,
1301 const struct s32cc_clk_drv *drv, unsigned long *rate,
1302 unsigned int depth)
1303{
1304 const struct s32cc_clk *clk = s32cc_obj2clk(module);
1305 unsigned int ldepth = depth;
1306 int ret;
1307
1308 ret = update_stack_depth(&ldepth);
1309 if (ret != 0) {
1310 return ret;
1311 }
1312
1313 if (clk == NULL) {
1314 ERROR("Invalid clock\n");
1315 return -EINVAL;
1316 }
1317
1318 if (clk->module != NULL) {
1319 return get_module_rate(clk->module, drv, rate, ldepth);
1320 }
1321
1322 if (clk->pclock == NULL) {
1323 ERROR("Invalid clock parent\n");
1324 return -EINVAL;
1325 }
1326
1327 return get_clk_freq(&clk->pclock->desc, drv, rate, ldepth);
1328}
1329
Ghennadi Procopciuce18cf332024-06-12 11:55:32 +03001330static int set_pll_freq(const struct s32cc_clk_obj *module, unsigned long rate,
1331 unsigned long *orate, unsigned int *depth)
1332{
1333 struct s32cc_pll *pll = s32cc_obj2pll(module);
1334 int ret;
1335
1336 ret = update_stack_depth(depth);
1337 if (ret != 0) {
1338 return ret;
1339 }
1340
1341 if ((pll->vco_freq != 0UL) && (pll->vco_freq != rate)) {
1342 ERROR("PLL frequency was already set\n");
1343 return -EINVAL;
1344 }
1345
1346 pll->vco_freq = rate;
1347 *orate = pll->vco_freq;
1348
1349 return 0;
1350}
1351
Ghennadi Procopciuc4bec7992025-01-13 09:33:42 +02001352static int get_pll_freq(const struct s32cc_clk_obj *module,
1353 const struct s32cc_clk_drv *drv,
1354 unsigned long *rate, unsigned int depth)
1355{
1356 const struct s32cc_pll *pll = s32cc_obj2pll(module);
1357 const struct s32cc_clk *source;
1358 uint32_t mfi, mfn, rdiv, plldv;
1359 unsigned long prate, clk_src;
1360 unsigned int ldepth = depth;
1361 uintptr_t pll_addr = 0UL;
1362 uint64_t t1, t2;
Ghennadi Procopciuc4bec7992025-01-13 09:33:42 +02001363 int ret;
1364
1365 ret = update_stack_depth(&ldepth);
1366 if (ret != 0) {
1367 return ret;
1368 }
1369
1370 ret = get_base_addr(pll->instance, drv, &pll_addr);
1371 if (ret != 0) {
1372 ERROR("Failed to detect PLL instance\n");
1373 return ret;
1374 }
1375
1376 /* Disabled PLL */
Ghennadi Procopciuc1d08c6d2025-01-15 14:59:22 +02001377 if (!is_pll_enabled(pll_addr)) {
Ghennadi Procopciuc4bec7992025-01-13 09:33:42 +02001378 *rate = pll->vco_freq;
1379 return 0;
1380 }
1381
1382 clk_src = mmio_read_32(PLLDIG_PLLCLKMUX(pll_addr));
1383 switch (clk_src) {
1384 case 0:
1385 clk_src = S32CC_CLK_FIRC;
1386 break;
1387 case 1:
1388 clk_src = S32CC_CLK_FXOSC;
1389 break;
1390 default:
1391 ERROR("Failed to identify PLL source id %" PRIu64 "\n", clk_src);
1392 return -EINVAL;
1393 };
1394
1395 source = s32cc_get_arch_clk(clk_src);
1396 if (source == NULL) {
1397 ERROR("Failed to get PLL source clock\n");
1398 return -EINVAL;
1399 }
1400
1401 ret = get_module_rate(&source->desc, drv, &prate, ldepth);
1402 if (ret != 0) {
1403 ERROR("Failed to get PLL's parent frequency\n");
1404 return ret;
1405 }
1406
1407 plldv = mmio_read_32(PLLDIG_PLLDV(pll_addr));
1408 mfi = PLLDIG_PLLDV_MFI(plldv);
1409 rdiv = PLLDIG_PLLDV_RDIV(plldv);
1410 if (rdiv == 0U) {
1411 rdiv = 1;
1412 }
1413
1414 /* Frac-N mode */
1415 mfn = PLLDIG_PLLFD_MFN_SET(mmio_read_32(PLLDIG_PLLFD(pll_addr)));
1416
1417 /* PLL VCO frequency in Fractional mode when PLLDV[RDIV] is not 0 */
1418 t1 = prate / rdiv;
1419 t2 = (mfi * FP_PRECISION) + (mfn * FP_PRECISION / 18432U);
1420
1421 *rate = t1 * t2 / FP_PRECISION;
1422
1423 return 0;
1424}
1425
Ghennadi Procopciuc907f6542024-06-12 12:00:15 +03001426static int set_pll_div_freq(const struct s32cc_clk_obj *module, unsigned long rate,
1427 unsigned long *orate, unsigned int *depth)
1428{
1429 struct s32cc_pll_out_div *pdiv = s32cc_obj2plldiv(module);
1430 const struct s32cc_pll *pll;
1431 unsigned long prate, dc;
1432 int ret;
1433
1434 ret = update_stack_depth(depth);
1435 if (ret != 0) {
1436 return ret;
1437 }
1438
1439 if (pdiv->parent == NULL) {
1440 ERROR("Failed to identify PLL divider's parent\n");
1441 return -EINVAL;
1442 }
1443
1444 pll = s32cc_obj2pll(pdiv->parent);
1445 if (pll == NULL) {
1446 ERROR("The parent of the PLL DIV is invalid\n");
1447 return -EINVAL;
1448 }
1449
1450 prate = pll->vco_freq;
1451
1452 /**
1453 * The PLL is not initialized yet, so let's take a risk
1454 * and accept the proposed rate.
1455 */
1456 if (prate == 0UL) {
1457 pdiv->freq = rate;
1458 *orate = rate;
1459 return 0;
1460 }
1461
1462 /* Decline in case the rate cannot fit PLL's requirements. */
1463 dc = prate / rate;
1464 if ((prate / dc) != rate) {
1465 return -EINVAL;
1466 }
1467
1468 pdiv->freq = rate;
1469 *orate = pdiv->freq;
1470
1471 return 0;
1472}
1473
Ghennadi Procopciuc5cf422a2025-01-13 11:38:34 +02001474static int get_pll_div_freq(const struct s32cc_clk_obj *module,
1475 const struct s32cc_clk_drv *drv,
1476 unsigned long *rate, unsigned int depth)
1477{
1478 const struct s32cc_pll_out_div *pdiv = s32cc_obj2plldiv(module);
1479 const struct s32cc_pll *pll;
1480 unsigned int ldepth = depth;
1481 uintptr_t pll_addr = 0UL;
1482 unsigned long pfreq;
1483 uint32_t pllodiv;
1484 uint32_t dc;
1485 int ret;
1486
1487 ret = update_stack_depth(&ldepth);
1488 if (ret != 0) {
1489 return ret;
1490 }
1491
1492 pll = get_div_pll(pdiv);
1493 if (pll == NULL) {
1494 ERROR("The parent of the PLL DIV is invalid\n");
1495 return -EINVAL;
1496 }
1497
1498 ret = get_base_addr(pll->instance, drv, &pll_addr);
1499 if (ret != 0) {
1500 ERROR("Failed to detect PLL instance\n");
1501 return -EINVAL;
1502 }
1503
1504 ret = get_module_rate(pdiv->parent, drv, &pfreq, ldepth);
1505 if (ret != 0) {
1506 ERROR("Failed to get the frequency of PLL %" PRIxPTR "\n",
1507 pll_addr);
1508 return ret;
1509 }
1510
1511 pllodiv = mmio_read_32(PLLDIG_PLLODIV(pll_addr, pdiv->index));
1512
1513 /* Disabled module */
1514 if ((pllodiv & PLLDIG_PLLODIV_DE) == 0U) {
1515 *rate = pdiv->freq;
1516 return 0;
1517 }
1518
1519 dc = PLLDIG_PLLODIV_DIV(pllodiv);
1520 *rate = (pfreq * FP_PRECISION) / (dc + 1U) / FP_PRECISION;
1521
1522 return 0;
1523}
1524
Ghennadi Procopciuce6946b62024-06-12 12:29:54 +03001525static int set_fixed_div_freq(const struct s32cc_clk_obj *module, unsigned long rate,
1526 unsigned long *orate, unsigned int *depth)
1527{
1528 const struct s32cc_fixed_div *fdiv = s32cc_obj2fixeddiv(module);
1529 int ret;
1530
1531 ret = update_stack_depth(depth);
1532 if (ret != 0) {
1533 return ret;
1534 }
1535
1536 if (fdiv->parent == NULL) {
1537 ERROR("The divider doesn't have a valid parent\b");
1538 return -EINVAL;
1539 }
1540
1541 ret = set_module_rate(fdiv->parent, rate * fdiv->rate_div, orate, depth);
1542
1543 /* Update the output rate based on the parent's rate */
1544 *orate /= fdiv->rate_div;
1545
1546 return ret;
1547}
1548
Ghennadi Procopciuc0e2aea72025-01-13 10:56:04 +02001549static int get_fixed_div_freq(const struct s32cc_clk_obj *module,
1550 const struct s32cc_clk_drv *drv,
1551 unsigned long *rate, unsigned int depth)
1552{
1553 const struct s32cc_fixed_div *fdiv = s32cc_obj2fixeddiv(module);
1554 unsigned long pfreq;
1555 int ret;
1556
1557 ret = get_module_rate(fdiv->parent, drv, &pfreq, depth);
1558 if (ret != 0) {
1559 return ret;
1560 }
1561
1562 *rate = (pfreq * FP_PRECISION / fdiv->rate_div) / FP_PRECISION;
1563 return 0;
1564}
1565
Ghennadi Procopciucb1bc3442025-01-28 11:57:09 +02001566static inline struct s32cc_clk_obj *get_fixed_div_parent(const struct s32cc_clk_obj *module)
1567{
1568 const struct s32cc_fixed_div *fdiv = s32cc_obj2fixeddiv(module);
1569
1570 return fdiv->parent;
1571}
1572
Ghennadi Procopciuca6a39e82024-06-12 13:05:05 +03001573static int set_mux_freq(const struct s32cc_clk_obj *module, unsigned long rate,
1574 unsigned long *orate, unsigned int *depth)
1575{
1576 const struct s32cc_clkmux *mux = s32cc_obj2clkmux(module);
1577 const struct s32cc_clk *clk = s32cc_get_arch_clk(mux->source_id);
1578 int ret;
1579
1580 ret = update_stack_depth(depth);
1581 if (ret != 0) {
1582 return ret;
1583 }
1584
1585 if (clk == NULL) {
1586 ERROR("Mux (id:%" PRIu8 ") without a valid source (%lu)\n",
1587 mux->index, mux->source_id);
1588 return -EINVAL;
1589 }
1590
1591 return set_module_rate(&clk->desc, rate, orate, depth);
1592}
1593
Ghennadi Procopciucf774c502025-01-13 11:49:55 +02001594static int get_mux_freq(const struct s32cc_clk_obj *module,
1595 const struct s32cc_clk_drv *drv,
1596 unsigned long *rate, unsigned int depth)
1597{
1598 const struct s32cc_clkmux *mux = s32cc_obj2clkmux(module);
1599 const struct s32cc_clk *clk = s32cc_get_arch_clk(mux->source_id);
1600 unsigned int ldepth = depth;
1601 int ret;
1602
1603 ret = update_stack_depth(&ldepth);
1604 if (ret != 0) {
1605 return ret;
1606 }
1607
1608 if (clk == NULL) {
1609 ERROR("Mux (id:%" PRIu8 ") without a valid source (%lu)\n",
1610 mux->index, mux->source_id);
1611 return -EINVAL;
1612 }
1613
1614 return get_clk_freq(&clk->desc, drv, rate, ldepth);
1615}
1616
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +03001617static int set_dfs_div_freq(const struct s32cc_clk_obj *module, unsigned long rate,
1618 unsigned long *orate, unsigned int *depth)
1619{
1620 struct s32cc_dfs_div *dfs_div = s32cc_obj2dfsdiv(module);
1621 const struct s32cc_dfs *dfs;
1622 int ret;
1623
1624 ret = update_stack_depth(depth);
1625 if (ret != 0) {
1626 return ret;
1627 }
1628
1629 if (dfs_div->parent == NULL) {
1630 ERROR("Failed to identify DFS divider's parent\n");
1631 return -EINVAL;
1632 }
1633
1634 /* Sanity check */
1635 dfs = s32cc_obj2dfs(dfs_div->parent);
1636 if (dfs->parent == NULL) {
1637 ERROR("Failed to identify DFS's parent\n");
1638 return -EINVAL;
1639 }
1640
1641 if ((dfs_div->freq != 0U) && (dfs_div->freq != rate)) {
1642 ERROR("DFS DIV frequency was already set to %lu\n",
1643 dfs_div->freq);
1644 return -EINVAL;
1645 }
1646
1647 dfs_div->freq = rate;
1648 *orate = rate;
1649
1650 return ret;
1651}
1652
Ghennadi Procopciucca578b92025-01-13 10:47:26 +02001653static unsigned long compute_dfs_div_freq(unsigned long pfreq, uint32_t mfi, uint32_t mfn)
1654{
1655 unsigned long freq;
1656
1657 /**
1658 * Formula for input and output clocks of each port divider.
1659 * See 'Digital Frequency Synthesizer' chapter from Reference Manual.
1660 *
1661 * freq = pfreq / (2 * (mfi + mfn / 36.0));
1662 */
1663 freq = (mfi * FP_PRECISION) + (mfn * FP_PRECISION / 36UL);
1664 freq *= 2UL;
1665 freq = pfreq * FP_PRECISION / freq;
1666
1667 return freq;
1668}
1669
1670static int get_dfs_div_freq(const struct s32cc_clk_obj *module,
1671 const struct s32cc_clk_drv *drv,
1672 unsigned long *rate, unsigned int depth)
1673{
1674 const struct s32cc_dfs_div *dfs_div = s32cc_obj2dfsdiv(module);
1675 unsigned int ldepth = depth;
1676 const struct s32cc_dfs *dfs;
1677 uint32_t dvport, mfi, mfn;
1678 uintptr_t dfs_addr = 0UL;
1679 unsigned long pfreq;
1680 int ret;
1681
1682 ret = update_stack_depth(&ldepth);
1683 if (ret != 0) {
1684 return ret;
1685 }
1686
1687 dfs = get_div_dfs(dfs_div);
1688 if (dfs == NULL) {
1689 return -EINVAL;
1690 }
1691
1692 ret = get_module_rate(dfs_div->parent, drv, &pfreq, ldepth);
1693 if (ret != 0) {
1694 return ret;
1695 }
1696
1697 ret = get_base_addr(dfs->instance, drv, &dfs_addr);
1698 if (ret != 0) {
1699 ERROR("Failed to detect the DFS instance\n");
1700 return ret;
1701 }
1702
1703 dvport = mmio_read_32(DFS_DVPORTn(dfs_addr, dfs_div->index));
1704
1705 mfi = DFS_DVPORTn_MFI(dvport);
1706 mfn = DFS_DVPORTn_MFN(dvport);
1707
1708 /* Disabled port */
1709 if ((mfi == 0U) && (mfn == 0U)) {
1710 *rate = dfs_div->freq;
1711 return 0;
1712 }
1713
1714 *rate = compute_dfs_div_freq(pfreq, mfi, mfn);
1715 return 0;
1716}
1717
Ghennadi Procopciucb64f11a2025-01-28 11:52:11 +02001718static int set_part_block_link_freq(const struct s32cc_clk_obj *module,
1719 unsigned long rate, unsigned long *orate,
1720 const unsigned int *depth)
1721{
1722 const struct s32cc_part_block_link *link = s32cc_obj2partblocklink(module);
1723 const struct s32cc_clk_obj *parent = link->parent;
1724 unsigned int ldepth = *depth;
1725 int ret;
1726
1727 ret = update_stack_depth(&ldepth);
1728 if (ret != 0) {
1729 return ret;
1730 }
1731
1732 if (parent == NULL) {
1733 ERROR("Partition block link with no parent\n");
1734 return -EINVAL;
1735 }
1736
1737 return set_module_rate(parent, rate, orate, &ldepth);
1738}
1739
Ghennadi Procopciuc0d525772024-06-12 08:09:19 +03001740static int set_module_rate(const struct s32cc_clk_obj *module,
1741 unsigned long rate, unsigned long *orate,
1742 unsigned int *depth)
1743{
1744 int ret = 0;
1745
1746 ret = update_stack_depth(depth);
1747 if (ret != 0) {
1748 return ret;
1749 }
1750
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +03001751 ret = -EINVAL;
1752
Ghennadi Procopciuc0d525772024-06-12 08:09:19 +03001753 switch (module->type) {
1754 case s32cc_clk_t:
1755 ret = set_clk_freq(module, rate, orate, depth);
1756 break;
1757 case s32cc_osc_t:
1758 ret = set_osc_freq(module, rate, orate, depth);
1759 break;
Ghennadi Procopciuce18cf332024-06-12 11:55:32 +03001760 case s32cc_pll_t:
1761 ret = set_pll_freq(module, rate, orate, depth);
1762 break;
Ghennadi Procopciuc907f6542024-06-12 12:00:15 +03001763 case s32cc_pll_out_div_t:
1764 ret = set_pll_div_freq(module, rate, orate, depth);
1765 break;
Ghennadi Procopciuce6946b62024-06-12 12:29:54 +03001766 case s32cc_fixed_div_t:
1767 ret = set_fixed_div_freq(module, rate, orate, depth);
1768 break;
Ghennadi Procopciuc7277b972024-06-12 09:53:18 +03001769 case s32cc_clkmux_t:
Ghennadi Procopciuca6a39e82024-06-12 13:05:05 +03001770 ret = set_mux_freq(module, rate, orate, depth);
1771 break;
Ghennadi Procopciuc8384d182024-06-12 10:53:06 +03001772 case s32cc_shared_clkmux_t:
Ghennadi Procopciuca6a39e82024-06-12 13:05:05 +03001773 ret = set_mux_freq(module, rate, orate, depth);
Ghennadi Procopciuc7277b972024-06-12 09:53:18 +03001774 break;
Ghennadi Procopciucf21d3ae2024-08-05 16:48:49 +03001775 case s32cc_dfs_t:
1776 ERROR("Setting the frequency of a DFS is not allowed!");
1777 break;
1778 case s32cc_dfs_div_t:
1779 ret = set_dfs_div_freq(module, rate, orate, depth);
1780 break;
Ghennadi Procopciucb64f11a2025-01-28 11:52:11 +02001781 case s32cc_part_block_link_t:
1782 ret = set_part_block_link_freq(module, rate, orate, depth);
1783 break;
1784 case s32cc_part_t:
1785 ERROR("It's not allowed to set the frequency of a partition !");
1786 break;
1787 case s32cc_part_block_t:
1788 ERROR("It's not allowed to set the frequency of a partition block !");
1789 break;
Ghennadi Procopciuc0d525772024-06-12 08:09:19 +03001790 default:
Ghennadi Procopciuc0d525772024-06-12 08:09:19 +03001791 break;
1792 }
1793
1794 return ret;
1795}
1796
Ghennadi Procopciuc7d927632025-01-10 16:26:21 +02001797static int get_module_rate(const struct s32cc_clk_obj *module,
1798 const struct s32cc_clk_drv *drv,
1799 unsigned long *rate,
1800 unsigned int depth)
1801{
1802 unsigned int ldepth = depth;
1803 int ret = 0;
1804
1805 ret = update_stack_depth(&ldepth);
1806 if (ret != 0) {
1807 return ret;
1808 }
1809
1810 switch (module->type) {
1811 case s32cc_osc_t:
1812 ret = get_osc_freq(module, drv, rate, ldepth);
1813 break;
Ghennadi Procopciucdba9d3d2025-01-10 16:33:36 +02001814 case s32cc_clk_t:
1815 ret = get_clk_freq(module, drv, rate, ldepth);
1816 break;
Ghennadi Procopciuc4bec7992025-01-13 09:33:42 +02001817 case s32cc_pll_t:
1818 ret = get_pll_freq(module, drv, rate, ldepth);
1819 break;
Ghennadi Procopciuc393f79b2025-01-13 09:50:51 +02001820 case s32cc_dfs_t:
1821 ret = get_dfs_freq(module, drv, rate, ldepth);
1822 break;
Ghennadi Procopciucca578b92025-01-13 10:47:26 +02001823 case s32cc_dfs_div_t:
1824 ret = get_dfs_div_freq(module, drv, rate, ldepth);
1825 break;
Ghennadi Procopciuc0e2aea72025-01-13 10:56:04 +02001826 case s32cc_fixed_div_t:
1827 ret = get_fixed_div_freq(module, drv, rate, ldepth);
1828 break;
Ghennadi Procopciuc5cf422a2025-01-13 11:38:34 +02001829 case s32cc_pll_out_div_t:
1830 ret = get_pll_div_freq(module, drv, rate, ldepth);
1831 break;
Ghennadi Procopciucf774c502025-01-13 11:49:55 +02001832 case s32cc_clkmux_t:
1833 ret = get_mux_freq(module, drv, rate, ldepth);
1834 break;
1835 case s32cc_shared_clkmux_t:
1836 ret = get_mux_freq(module, drv, rate, ldepth);
1837 break;
Ghennadi Procopciuc244b5442025-01-13 12:00:50 +02001838 case s32cc_part_t:
1839 ERROR("s32cc_part_t cannot be used to get rate\n");
1840 break;
1841 case s32cc_part_block_t:
1842 ERROR("s32cc_part_block_t cannot be used to get rate\n");
1843 break;
1844 case s32cc_part_block_link_t:
1845 ret = get_part_block_link_freq(module, drv, rate, ldepth);
1846 break;
Ghennadi Procopciuc7d927632025-01-10 16:26:21 +02001847 default:
1848 ret = -EINVAL;
1849 break;
1850 }
1851
1852 return ret;
1853}
1854
Ghennadi Procopciucfc26eb02024-06-11 18:39:58 +03001855static int s32cc_clk_set_rate(unsigned long id, unsigned long rate,
1856 unsigned long *orate)
1857{
Ghennadi Procopciuc0d525772024-06-12 08:09:19 +03001858 unsigned int depth = MAX_STACK_DEPTH;
1859 const struct s32cc_clk *clk;
1860 int ret;
1861
1862 clk = s32cc_get_arch_clk(id);
1863 if (clk == NULL) {
1864 return -EINVAL;
1865 }
1866
1867 ret = set_module_rate(&clk->desc, rate, orate, &depth);
1868 if (ret != 0) {
1869 ERROR("Failed to set frequency (%lu MHz) for clock %lu\n",
1870 rate, id);
1871 }
1872
1873 return ret;
Ghennadi Procopciucfc26eb02024-06-11 18:39:58 +03001874}
1875
Ghennadi Procopciuc7d927632025-01-10 16:26:21 +02001876static unsigned long s32cc_clk_get_rate(unsigned long id)
1877{
1878 const struct s32cc_clk_drv *drv = get_drv();
1879 unsigned int depth = MAX_STACK_DEPTH;
1880 const struct s32cc_clk *clk;
1881 unsigned long rate = 0UL;
1882 int ret;
1883
1884 clk = s32cc_get_arch_clk(id);
1885 if (clk == NULL) {
1886 return 0;
1887 }
1888
1889 ret = get_module_rate(&clk->desc, drv, &rate, depth);
1890 if (ret != 0) {
1891 ERROR("Failed to get frequency (%lu MHz) for clock %lu\n",
1892 rate, id);
1893 return 0;
1894 }
1895
1896 return rate;
1897}
1898
Ghennadi Procopciuc302831c2024-09-11 09:29:50 +03001899static struct s32cc_clk_obj *get_no_parent(const struct s32cc_clk_obj *module)
1900{
1901 return NULL;
1902}
1903
1904typedef struct s32cc_clk_obj *(*get_parent_clb_t)(const struct s32cc_clk_obj *clk_obj);
1905
1906static struct s32cc_clk_obj *get_module_parent(const struct s32cc_clk_obj *module)
1907{
Ghennadi Procopciuc224a0762025-01-23 10:34:35 +02001908 static const get_parent_clb_t parents_clbs[13] = {
Ghennadi Procopciuc302831c2024-09-11 09:29:50 +03001909 [s32cc_clk_t] = get_clk_parent,
1910 [s32cc_osc_t] = get_no_parent,
1911 [s32cc_pll_t] = get_pll_parent,
1912 [s32cc_pll_out_div_t] = get_pll_div_parent,
1913 [s32cc_clkmux_t] = get_mux_parent,
1914 [s32cc_shared_clkmux_t] = get_mux_parent,
1915 [s32cc_dfs_t] = get_dfs_parent,
1916 [s32cc_dfs_div_t] = get_dfs_div_parent,
Ghennadi Procopciucd89e32f2024-09-17 11:22:30 +03001917 [s32cc_part_t] = get_no_parent,
Ghennadi Procopciucb1bc3442025-01-28 11:57:09 +02001918 [s32cc_fixed_div_t] = get_fixed_div_parent,
Ghennadi Procopciucd89e32f2024-09-17 11:22:30 +03001919 [s32cc_part_block_t] = get_part_block_parent,
1920 [s32cc_part_block_link_t] = get_part_block_link_parent,
Ghennadi Procopciuc302831c2024-09-11 09:29:50 +03001921 };
1922 uint32_t index;
1923
1924 if (module == NULL) {
1925 return NULL;
1926 }
1927
1928 index = (uint32_t)module->type;
1929
1930 if (index >= ARRAY_SIZE(parents_clbs)) {
1931 ERROR("Undefined module type: %d\n", module->type);
1932 return NULL;
1933 }
1934
1935 if (parents_clbs[index] == NULL) {
1936 ERROR("Undefined parent getter for type: %d\n", module->type);
1937 return NULL;
1938 }
1939
1940 return parents_clbs[index](module);
1941}
1942
Ghennadi Procopciucfc26eb02024-06-11 18:39:58 +03001943static int s32cc_clk_get_parent(unsigned long id)
1944{
Ghennadi Procopciuc302831c2024-09-11 09:29:50 +03001945 struct s32cc_clk *parent_clk;
1946 const struct s32cc_clk_obj *parent;
1947 const struct s32cc_clk *clk;
1948 unsigned long parent_id;
1949 int ret;
1950
1951 clk = s32cc_get_arch_clk(id);
1952 if (clk == NULL) {
1953 return -EINVAL;
1954 }
1955
1956 parent = get_module_parent(clk->module);
1957 if (parent == NULL) {
1958 return -EINVAL;
1959 }
1960
1961 parent_clk = s32cc_obj2clk(parent);
1962 if (parent_clk == NULL) {
1963 return -EINVAL;
1964 }
1965
1966 ret = s32cc_get_clk_id(parent_clk, &parent_id);
1967 if (ret != 0) {
1968 return ret;
1969 }
1970
1971 if (parent_id > (unsigned long)INT_MAX) {
1972 return -E2BIG;
1973 }
1974
1975 return (int)parent_id;
Ghennadi Procopciucfc26eb02024-06-11 18:39:58 +03001976}
1977
1978static int s32cc_clk_set_parent(unsigned long id, unsigned long parent_id)
1979{
Ghennadi Procopciucaa45fa92024-06-12 10:02:07 +03001980 const struct s32cc_clk *parent;
1981 const struct s32cc_clk *clk;
1982 bool valid_source = false;
1983 struct s32cc_clkmux *mux;
1984 uint8_t i;
1985
1986 clk = s32cc_get_arch_clk(id);
1987 if (clk == NULL) {
1988 return -EINVAL;
1989 }
1990
1991 parent = s32cc_get_arch_clk(parent_id);
1992 if (parent == NULL) {
1993 return -EINVAL;
1994 }
1995
1996 if (!is_s32cc_clk_mux(clk)) {
1997 ERROR("Clock %lu is not a mux\n", id);
1998 return -EINVAL;
1999 }
2000
2001 mux = s32cc_clk2mux(clk);
2002 if (mux == NULL) {
2003 ERROR("Failed to cast clock %lu to clock mux\n", id);
2004 return -EINVAL;
2005 }
2006
2007 for (i = 0; i < mux->nclks; i++) {
2008 if (mux->clkids[i] == parent_id) {
2009 valid_source = true;
2010 break;
2011 }
2012 }
2013
2014 if (!valid_source) {
2015 ERROR("Clock %lu is not a valid clock for mux %lu\n",
2016 parent_id, id);
2017 return -EINVAL;
2018 }
2019
2020 mux->source_id = parent_id;
2021
2022 return 0;
Ghennadi Procopciucfc26eb02024-06-11 18:39:58 +03002023}
2024
Ghennadi Procopciucbdd85872024-11-26 16:39:41 +02002025static int s32cc_clk_mmap_regs(const struct s32cc_clk_drv *drv)
2026{
Ghennadi Procopciuceb26f082025-01-20 16:02:21 +02002027 const uintptr_t base_addrs[12] = {
Ghennadi Procopciucbdd85872024-11-26 16:39:41 +02002028 drv->fxosc_base,
2029 drv->armpll_base,
2030 drv->periphpll_base,
2031 drv->armdfs_base,
Ghennadi Procopciuceb26f082025-01-20 16:02:21 +02002032 drv->periphdfs_base,
Ghennadi Procopciucbdd85872024-11-26 16:39:41 +02002033 drv->cgm0_base,
2034 drv->cgm1_base,
2035 drv->cgm5_base,
2036 drv->ddrpll_base,
2037 drv->mc_me,
2038 drv->mc_rgm,
2039 drv->rdc,
2040 };
2041 size_t i;
2042 int ret;
2043
2044 for (i = 0U; i < ARRAY_SIZE(base_addrs); i++) {
2045 ret = mmap_add_dynamic_region(base_addrs[i], base_addrs[i],
2046 PAGE_SIZE,
2047 MT_DEVICE | MT_RW | MT_SECURE);
2048 if (ret != 0) {
2049 ERROR("Failed to map clock module 0x%" PRIuPTR "\n",
2050 base_addrs[i]);
2051 return ret;
2052 }
2053 }
2054
2055 return 0;
2056}
2057
Ghennadi Procopciuce83c8c62024-11-27 12:33:26 +02002058int s32cc_clk_register_drv(bool mmap_regs)
Ghennadi Procopciucfc26eb02024-06-11 18:39:58 +03002059{
2060 static const struct clk_ops s32cc_clk_ops = {
2061 .enable = s32cc_clk_enable,
2062 .disable = s32cc_clk_disable,
2063 .is_enabled = s32cc_clk_is_enabled,
2064 .get_rate = s32cc_clk_get_rate,
2065 .set_rate = s32cc_clk_set_rate,
2066 .get_parent = s32cc_clk_get_parent,
2067 .set_parent = s32cc_clk_set_parent,
2068 };
Ghennadi Procopciucbdd85872024-11-26 16:39:41 +02002069 const struct s32cc_clk_drv *drv;
Ghennadi Procopciucfc26eb02024-06-11 18:39:58 +03002070
2071 clk_register(&s32cc_clk_ops);
Ghennadi Procopciucbdd85872024-11-26 16:39:41 +02002072
2073 drv = get_drv();
2074 if (drv == NULL) {
2075 return -EINVAL;
2076 }
2077
Ghennadi Procopciuce83c8c62024-11-27 12:33:26 +02002078 if (mmap_regs) {
2079 return s32cc_clk_mmap_regs(drv);
2080 }
2081
2082 return 0;
Ghennadi Procopciucfc26eb02024-06-11 18:39:58 +03002083}
2084