Ghennadi Procopciuc | 3a580e9 | 2024-06-11 18:39:58 +0300 | [diff] [blame] | 1 | /* |
Ghennadi Procopciuc | bd69113 | 2025-01-10 16:26:21 +0200 | [diff] [blame] | 2 | * Copyright 2024-2025 NXP |
Ghennadi Procopciuc | 3a580e9 | 2024-06-11 18:39:58 +0300 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | #include <errno.h> |
Ghennadi Procopciuc | d937351 | 2024-06-12 08:09:19 +0300 | [diff] [blame] | 7 | #include <common/debug.h> |
Ghennadi Procopciuc | 3a580e9 | 2024-06-11 18:39:58 +0300 | [diff] [blame] | 8 | #include <drivers/clk.h> |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 9 | #include <lib/mmio.h> |
Ghennadi Procopciuc | 514c738 | 2024-11-26 16:39:41 +0200 | [diff] [blame] | 10 | #include <lib/xlat_tables/xlat_tables_v2.h> |
Ghennadi Procopciuc | b5101c4 | 2024-06-12 14:21:39 +0300 | [diff] [blame] | 11 | #include <s32cc-clk-ids.h> |
Ghennadi Procopciuc | d937351 | 2024-06-12 08:09:19 +0300 | [diff] [blame] | 12 | #include <s32cc-clk-modules.h> |
Ghennadi Procopciuc | 8a4f840 | 2024-09-17 11:22:30 +0300 | [diff] [blame] | 13 | #include <s32cc-clk-regs.h> |
Ghennadi Procopciuc | d937351 | 2024-06-12 08:09:19 +0300 | [diff] [blame] | 14 | #include <s32cc-clk-utils.h> |
Ghennadi Procopciuc | 8a4f840 | 2024-09-17 11:22:30 +0300 | [diff] [blame] | 15 | #include <s32cc-mc-me.h> |
Ghennadi Procopciuc | d937351 | 2024-06-12 08:09:19 +0300 | [diff] [blame] | 16 | |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 17 | #define MAX_STACK_DEPTH (40U) |
Ghennadi Procopciuc | d937351 | 2024-06-12 08:09:19 +0300 | [diff] [blame] | 18 | |
Ghennadi Procopciuc | b5101c4 | 2024-06-12 14:21:39 +0300 | [diff] [blame] | 19 | /* This is used for floating-point precision calculations. */ |
| 20 | #define FP_PRECISION (100000000UL) |
| 21 | |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 22 | struct s32cc_clk_drv { |
| 23 | uintptr_t fxosc_base; |
Ghennadi Procopciuc | b5101c4 | 2024-06-12 14:21:39 +0300 | [diff] [blame] | 24 | uintptr_t armpll_base; |
Ghennadi Procopciuc | 8653352 | 2024-08-06 11:48:11 +0300 | [diff] [blame] | 25 | uintptr_t periphpll_base; |
Ghennadi Procopciuc | 4cd04c5 | 2024-08-05 16:48:49 +0300 | [diff] [blame] | 26 | uintptr_t armdfs_base; |
Ghennadi Procopciuc | 9dbca85 | 2024-08-05 16:50:52 +0300 | [diff] [blame] | 27 | uintptr_t cgm0_base; |
Ghennadi Procopciuc | 7004f67 | 2024-06-12 14:44:47 +0300 | [diff] [blame] | 28 | uintptr_t cgm1_base; |
Ghennadi Procopciuc | 8a4f840 | 2024-09-17 11:22:30 +0300 | [diff] [blame] | 29 | uintptr_t cgm5_base; |
Ghennadi Procopciuc | 18c2b13 | 2024-09-09 10:24:35 +0300 | [diff] [blame] | 30 | uintptr_t ddrpll_base; |
Ghennadi Procopciuc | 8a4f840 | 2024-09-17 11:22:30 +0300 | [diff] [blame] | 31 | uintptr_t mc_me; |
| 32 | uintptr_t mc_rgm; |
| 33 | uintptr_t rdc; |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 34 | }; |
| 35 | |
Ghennadi Procopciuc | 2fb2550 | 2025-01-13 09:50:51 +0200 | [diff] [blame] | 36 | static int set_module_rate(const struct s32cc_clk_obj *module, |
| 37 | unsigned long rate, unsigned long *orate, |
| 38 | unsigned int *depth); |
| 39 | static int get_module_rate(const struct s32cc_clk_obj *module, |
| 40 | const struct s32cc_clk_drv *drv, |
| 41 | unsigned long *rate, |
| 42 | unsigned int depth); |
| 43 | |
Ghennadi Procopciuc | d937351 | 2024-06-12 08:09:19 +0300 | [diff] [blame] | 44 | static int update_stack_depth(unsigned int *depth) |
| 45 | { |
| 46 | if (*depth == 0U) { |
| 47 | return -ENOMEM; |
| 48 | } |
| 49 | |
| 50 | (*depth)--; |
| 51 | return 0; |
| 52 | } |
Ghennadi Procopciuc | 3a580e9 | 2024-06-11 18:39:58 +0300 | [diff] [blame] | 53 | |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 54 | static struct s32cc_clk_drv *get_drv(void) |
| 55 | { |
| 56 | static struct s32cc_clk_drv driver = { |
| 57 | .fxosc_base = FXOSC_BASE_ADDR, |
Ghennadi Procopciuc | b5101c4 | 2024-06-12 14:21:39 +0300 | [diff] [blame] | 58 | .armpll_base = ARMPLL_BASE_ADDR, |
Ghennadi Procopciuc | 8653352 | 2024-08-06 11:48:11 +0300 | [diff] [blame] | 59 | .periphpll_base = PERIPHPLL_BASE_ADDR, |
Ghennadi Procopciuc | 4cd04c5 | 2024-08-05 16:48:49 +0300 | [diff] [blame] | 60 | .armdfs_base = ARM_DFS_BASE_ADDR, |
Ghennadi Procopciuc | 9dbca85 | 2024-08-05 16:50:52 +0300 | [diff] [blame] | 61 | .cgm0_base = CGM0_BASE_ADDR, |
Ghennadi Procopciuc | 7004f67 | 2024-06-12 14:44:47 +0300 | [diff] [blame] | 62 | .cgm1_base = CGM1_BASE_ADDR, |
Ghennadi Procopciuc | 8a4f840 | 2024-09-17 11:22:30 +0300 | [diff] [blame] | 63 | .cgm5_base = MC_CGM5_BASE_ADDR, |
Ghennadi Procopciuc | 18c2b13 | 2024-09-09 10:24:35 +0300 | [diff] [blame] | 64 | .ddrpll_base = DDRPLL_BASE_ADDR, |
Ghennadi Procopciuc | 8a4f840 | 2024-09-17 11:22:30 +0300 | [diff] [blame] | 65 | .mc_me = MC_ME_BASE_ADDR, |
| 66 | .mc_rgm = MC_RGM_BASE_ADDR, |
| 67 | .rdc = RDC_BASE_ADDR, |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | return &driver; |
| 71 | } |
| 72 | |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 73 | static int enable_module(struct s32cc_clk_obj *module, |
| 74 | const struct s32cc_clk_drv *drv, |
| 75 | unsigned int depth); |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 76 | |
Ghennadi Procopciuc | 96e069c | 2024-09-11 09:29:50 +0300 | [diff] [blame] | 77 | static struct s32cc_clk_obj *get_clk_parent(const struct s32cc_clk_obj *module) |
| 78 | { |
| 79 | const struct s32cc_clk *clk = s32cc_obj2clk(module); |
| 80 | |
| 81 | if (clk->module != NULL) { |
| 82 | return clk->module; |
| 83 | } |
| 84 | |
| 85 | if (clk->pclock != NULL) { |
| 86 | return &clk->pclock->desc; |
| 87 | } |
| 88 | |
| 89 | return NULL; |
| 90 | } |
| 91 | |
Ghennadi Procopciuc | b5101c4 | 2024-06-12 14:21:39 +0300 | [diff] [blame] | 92 | static int get_base_addr(enum s32cc_clk_source id, const struct s32cc_clk_drv *drv, |
| 93 | uintptr_t *base) |
| 94 | { |
| 95 | int ret = 0; |
| 96 | |
| 97 | switch (id) { |
| 98 | case S32CC_FXOSC: |
| 99 | *base = drv->fxosc_base; |
| 100 | break; |
| 101 | case S32CC_ARM_PLL: |
| 102 | *base = drv->armpll_base; |
| 103 | break; |
Ghennadi Procopciuc | 8653352 | 2024-08-06 11:48:11 +0300 | [diff] [blame] | 104 | case S32CC_PERIPH_PLL: |
| 105 | *base = drv->periphpll_base; |
| 106 | break; |
Ghennadi Procopciuc | 18c2b13 | 2024-09-09 10:24:35 +0300 | [diff] [blame] | 107 | case S32CC_DDR_PLL: |
| 108 | *base = drv->ddrpll_base; |
| 109 | break; |
Ghennadi Procopciuc | 4cd04c5 | 2024-08-05 16:48:49 +0300 | [diff] [blame] | 110 | case S32CC_ARM_DFS: |
| 111 | *base = drv->armdfs_base; |
| 112 | break; |
Ghennadi Procopciuc | 9dbca85 | 2024-08-05 16:50:52 +0300 | [diff] [blame] | 113 | case S32CC_CGM0: |
| 114 | *base = drv->cgm0_base; |
| 115 | break; |
Ghennadi Procopciuc | b5101c4 | 2024-06-12 14:21:39 +0300 | [diff] [blame] | 116 | case S32CC_CGM1: |
Ghennadi Procopciuc | 7004f67 | 2024-06-12 14:44:47 +0300 | [diff] [blame] | 117 | *base = drv->cgm1_base; |
Ghennadi Procopciuc | b5101c4 | 2024-06-12 14:21:39 +0300 | [diff] [blame] | 118 | break; |
Ghennadi Procopciuc | 8a4f840 | 2024-09-17 11:22:30 +0300 | [diff] [blame] | 119 | case S32CC_CGM5: |
| 120 | *base = drv->cgm5_base; |
| 121 | break; |
Ghennadi Procopciuc | b5101c4 | 2024-06-12 14:21:39 +0300 | [diff] [blame] | 122 | case S32CC_FIRC: |
| 123 | break; |
| 124 | case S32CC_SIRC: |
| 125 | break; |
| 126 | default: |
| 127 | ret = -EINVAL; |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | if (ret != 0) { |
| 132 | ERROR("Unknown clock source id: %u\n", id); |
| 133 | } |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 138 | static void enable_fxosc(const struct s32cc_clk_drv *drv) |
| 139 | { |
| 140 | uintptr_t fxosc_base = drv->fxosc_base; |
| 141 | uint32_t ctrl; |
| 142 | |
| 143 | ctrl = mmio_read_32(FXOSC_CTRL(fxosc_base)); |
| 144 | if ((ctrl & FXOSC_CTRL_OSCON) != U(0)) { |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | ctrl = FXOSC_CTRL_COMP_EN; |
| 149 | ctrl &= ~FXOSC_CTRL_OSC_BYP; |
| 150 | ctrl |= FXOSC_CTRL_EOCV(0x1); |
| 151 | ctrl |= FXOSC_CTRL_GM_SEL(0x7); |
| 152 | mmio_write_32(FXOSC_CTRL(fxosc_base), ctrl); |
| 153 | |
| 154 | /* Switch ON the crystal oscillator. */ |
| 155 | mmio_setbits_32(FXOSC_CTRL(fxosc_base), FXOSC_CTRL_OSCON); |
| 156 | |
| 157 | /* Wait until the clock is stable. */ |
| 158 | while ((mmio_read_32(FXOSC_STAT(fxosc_base)) & FXOSC_STAT_OSC_STAT) == U(0)) { |
| 159 | } |
| 160 | } |
| 161 | |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 162 | static int enable_osc(struct s32cc_clk_obj *module, |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 163 | const struct s32cc_clk_drv *drv, |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 164 | unsigned int depth) |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 165 | { |
| 166 | const struct s32cc_osc *osc = s32cc_obj2osc(module); |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 167 | unsigned int ldepth = depth; |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 168 | int ret = 0; |
| 169 | |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 170 | ret = update_stack_depth(&ldepth); |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 171 | if (ret != 0) { |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | switch (osc->source) { |
| 176 | case S32CC_FXOSC: |
| 177 | enable_fxosc(drv); |
| 178 | break; |
| 179 | /* FIRC and SIRC oscillators are enabled by default */ |
| 180 | case S32CC_FIRC: |
| 181 | break; |
| 182 | case S32CC_SIRC: |
| 183 | break; |
| 184 | default: |
| 185 | ERROR("Invalid oscillator %d\n", osc->source); |
| 186 | ret = -EINVAL; |
| 187 | break; |
| 188 | }; |
| 189 | |
| 190 | return ret; |
| 191 | } |
| 192 | |
Ghennadi Procopciuc | 96e069c | 2024-09-11 09:29:50 +0300 | [diff] [blame] | 193 | static struct s32cc_clk_obj *get_pll_parent(const struct s32cc_clk_obj *module) |
| 194 | { |
| 195 | const struct s32cc_pll *pll = s32cc_obj2pll(module); |
| 196 | |
| 197 | if (pll->source == NULL) { |
| 198 | ERROR("Failed to identify PLL's parent\n"); |
| 199 | } |
| 200 | |
| 201 | return pll->source; |
| 202 | } |
| 203 | |
Ghennadi Procopciuc | b5101c4 | 2024-06-12 14:21:39 +0300 | [diff] [blame] | 204 | static int get_pll_mfi_mfn(unsigned long pll_vco, unsigned long ref_freq, |
| 205 | uint32_t *mfi, uint32_t *mfn) |
| 206 | |
| 207 | { |
| 208 | unsigned long vco; |
| 209 | unsigned long mfn64; |
| 210 | |
| 211 | /* FRAC-N mode */ |
| 212 | *mfi = (uint32_t)(pll_vco / ref_freq); |
| 213 | |
| 214 | /* MFN formula : (double)(pll_vco % ref_freq) / ref_freq * 18432.0 */ |
| 215 | mfn64 = pll_vco % ref_freq; |
| 216 | mfn64 *= FP_PRECISION; |
| 217 | mfn64 /= ref_freq; |
| 218 | mfn64 *= 18432UL; |
| 219 | mfn64 /= FP_PRECISION; |
| 220 | |
| 221 | if (mfn64 > UINT32_MAX) { |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | |
| 225 | *mfn = (uint32_t)mfn64; |
| 226 | |
| 227 | vco = ((unsigned long)*mfn * FP_PRECISION) / 18432UL; |
| 228 | vco += (unsigned long)*mfi * FP_PRECISION; |
| 229 | vco *= ref_freq; |
| 230 | vco /= FP_PRECISION; |
| 231 | |
| 232 | if (vco != pll_vco) { |
| 233 | ERROR("Failed to find MFI and MFN settings for PLL freq %lu. Nearest freq = %lu\n", |
| 234 | pll_vco, vco); |
| 235 | return -EINVAL; |
| 236 | } |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static struct s32cc_clkmux *get_pll_mux(const struct s32cc_pll *pll) |
| 242 | { |
| 243 | const struct s32cc_clk_obj *source = pll->source; |
| 244 | const struct s32cc_clk *clk; |
| 245 | |
| 246 | if (source == NULL) { |
| 247 | ERROR("Failed to identify PLL's parent\n"); |
| 248 | return NULL; |
| 249 | } |
| 250 | |
| 251 | if (source->type != s32cc_clk_t) { |
| 252 | ERROR("The parent of the PLL isn't a clock\n"); |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | clk = s32cc_obj2clk(source); |
| 257 | |
| 258 | if (clk->module == NULL) { |
| 259 | ERROR("The clock isn't connected to a module\n"); |
| 260 | return NULL; |
| 261 | } |
| 262 | |
| 263 | source = clk->module; |
| 264 | |
| 265 | if ((source->type != s32cc_clkmux_t) && |
| 266 | (source->type != s32cc_shared_clkmux_t)) { |
| 267 | ERROR("The parent of the PLL isn't a MUX\n"); |
| 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | return s32cc_obj2clkmux(source); |
| 272 | } |
| 273 | |
| 274 | static void disable_odiv(uintptr_t pll_addr, uint32_t div_index) |
| 275 | { |
| 276 | mmio_clrbits_32(PLLDIG_PLLODIV(pll_addr, div_index), PLLDIG_PLLODIV_DE); |
| 277 | } |
| 278 | |
Ghennadi Procopciuc | 84e8208 | 2024-06-12 14:30:30 +0300 | [diff] [blame] | 279 | static void enable_odiv(uintptr_t pll_addr, uint32_t div_index) |
| 280 | { |
| 281 | mmio_setbits_32(PLLDIG_PLLODIV(pll_addr, div_index), PLLDIG_PLLODIV_DE); |
| 282 | } |
| 283 | |
Ghennadi Procopciuc | b5101c4 | 2024-06-12 14:21:39 +0300 | [diff] [blame] | 284 | static void disable_odivs(uintptr_t pll_addr, uint32_t ndivs) |
| 285 | { |
| 286 | uint32_t i; |
| 287 | |
| 288 | for (i = 0; i < ndivs; i++) { |
| 289 | disable_odiv(pll_addr, i); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | static void enable_pll_hw(uintptr_t pll_addr) |
| 294 | { |
| 295 | /* Enable the PLL. */ |
| 296 | mmio_write_32(PLLDIG_PLLCR(pll_addr), 0x0); |
| 297 | |
| 298 | /* Poll until PLL acquires lock. */ |
| 299 | while ((mmio_read_32(PLLDIG_PLLSR(pll_addr)) & PLLDIG_PLLSR_LOCK) == 0U) { |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | static void disable_pll_hw(uintptr_t pll_addr) |
| 304 | { |
| 305 | mmio_write_32(PLLDIG_PLLCR(pll_addr), PLLDIG_PLLCR_PLLPD); |
| 306 | } |
| 307 | |
| 308 | static int program_pll(const struct s32cc_pll *pll, uintptr_t pll_addr, |
| 309 | const struct s32cc_clk_drv *drv, uint32_t sclk_id, |
| 310 | unsigned long sclk_freq) |
| 311 | { |
| 312 | uint32_t rdiv = 1, mfi, mfn; |
| 313 | int ret; |
| 314 | |
| 315 | ret = get_pll_mfi_mfn(pll->vco_freq, sclk_freq, &mfi, &mfn); |
| 316 | if (ret != 0) { |
| 317 | return -EINVAL; |
| 318 | } |
| 319 | |
| 320 | /* Disable ODIVs*/ |
| 321 | disable_odivs(pll_addr, pll->ndividers); |
| 322 | |
| 323 | /* Disable PLL */ |
| 324 | disable_pll_hw(pll_addr); |
| 325 | |
| 326 | /* Program PLLCLKMUX */ |
| 327 | mmio_write_32(PLLDIG_PLLCLKMUX(pll_addr), sclk_id); |
| 328 | |
| 329 | /* Program VCO */ |
| 330 | mmio_clrsetbits_32(PLLDIG_PLLDV(pll_addr), |
| 331 | PLLDIG_PLLDV_RDIV_MASK | PLLDIG_PLLDV_MFI_MASK, |
| 332 | PLLDIG_PLLDV_RDIV_SET(rdiv) | PLLDIG_PLLDV_MFI(mfi)); |
| 333 | |
| 334 | mmio_write_32(PLLDIG_PLLFD(pll_addr), |
| 335 | PLLDIG_PLLFD_MFN_SET(mfn) | PLLDIG_PLLFD_SMDEN); |
| 336 | |
| 337 | enable_pll_hw(pll_addr); |
| 338 | |
| 339 | return ret; |
| 340 | } |
| 341 | |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 342 | static int enable_pll(struct s32cc_clk_obj *module, |
Ghennadi Procopciuc | b5101c4 | 2024-06-12 14:21:39 +0300 | [diff] [blame] | 343 | const struct s32cc_clk_drv *drv, |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 344 | unsigned int depth) |
Ghennadi Procopciuc | b5101c4 | 2024-06-12 14:21:39 +0300 | [diff] [blame] | 345 | { |
| 346 | const struct s32cc_pll *pll = s32cc_obj2pll(module); |
| 347 | const struct s32cc_clkmux *mux; |
| 348 | uintptr_t pll_addr = UL(0x0); |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 349 | unsigned int ldepth = depth; |
Ghennadi Procopciuc | b5101c4 | 2024-06-12 14:21:39 +0300 | [diff] [blame] | 350 | unsigned long sclk_freq; |
| 351 | uint32_t sclk_id; |
| 352 | int ret; |
| 353 | |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 354 | ret = update_stack_depth(&ldepth); |
Ghennadi Procopciuc | b5101c4 | 2024-06-12 14:21:39 +0300 | [diff] [blame] | 355 | if (ret != 0) { |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | mux = get_pll_mux(pll); |
| 360 | if (mux == NULL) { |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | |
| 364 | if (pll->instance != mux->module) { |
| 365 | ERROR("MUX type is not in sync with PLL ID\n"); |
| 366 | return -EINVAL; |
| 367 | } |
| 368 | |
| 369 | ret = get_base_addr(pll->instance, drv, &pll_addr); |
| 370 | if (ret != 0) { |
| 371 | ERROR("Failed to detect PLL instance\n"); |
| 372 | return ret; |
| 373 | } |
| 374 | |
| 375 | switch (mux->source_id) { |
| 376 | case S32CC_CLK_FIRC: |
| 377 | sclk_freq = 48U * MHZ; |
| 378 | sclk_id = 0; |
| 379 | break; |
| 380 | case S32CC_CLK_FXOSC: |
| 381 | sclk_freq = 40U * MHZ; |
| 382 | sclk_id = 1; |
| 383 | break; |
| 384 | default: |
| 385 | ERROR("Invalid source selection for PLL 0x%lx\n", |
| 386 | pll_addr); |
| 387 | return -EINVAL; |
| 388 | }; |
| 389 | |
| 390 | return program_pll(pll, pll_addr, drv, sclk_id, sclk_freq); |
| 391 | } |
| 392 | |
Ghennadi Procopciuc | 84e8208 | 2024-06-12 14:30:30 +0300 | [diff] [blame] | 393 | static inline struct s32cc_pll *get_div_pll(const struct s32cc_pll_out_div *pdiv) |
| 394 | { |
| 395 | const struct s32cc_clk_obj *parent; |
| 396 | |
| 397 | parent = pdiv->parent; |
| 398 | if (parent == NULL) { |
| 399 | ERROR("Failed to identify PLL divider's parent\n"); |
| 400 | return NULL; |
| 401 | } |
| 402 | |
| 403 | if (parent->type != s32cc_pll_t) { |
| 404 | ERROR("The parent of the divider is not a PLL instance\n"); |
| 405 | return NULL; |
| 406 | } |
| 407 | |
| 408 | return s32cc_obj2pll(parent); |
| 409 | } |
| 410 | |
| 411 | static void config_pll_out_div(uintptr_t pll_addr, uint32_t div_index, uint32_t dc) |
| 412 | { |
| 413 | uint32_t pllodiv; |
| 414 | uint32_t pdiv; |
| 415 | |
| 416 | pllodiv = mmio_read_32(PLLDIG_PLLODIV(pll_addr, div_index)); |
| 417 | pdiv = PLLDIG_PLLODIV_DIV(pllodiv); |
| 418 | |
| 419 | if (((pdiv + 1U) == dc) && ((pllodiv & PLLDIG_PLLODIV_DE) != 0U)) { |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | if ((pllodiv & PLLDIG_PLLODIV_DE) != 0U) { |
| 424 | disable_odiv(pll_addr, div_index); |
| 425 | } |
| 426 | |
| 427 | pllodiv = PLLDIG_PLLODIV_DIV_SET(dc - 1U); |
| 428 | mmio_write_32(PLLDIG_PLLODIV(pll_addr, div_index), pllodiv); |
| 429 | |
| 430 | enable_odiv(pll_addr, div_index); |
| 431 | } |
| 432 | |
Ghennadi Procopciuc | 96e069c | 2024-09-11 09:29:50 +0300 | [diff] [blame] | 433 | static struct s32cc_clk_obj *get_pll_div_parent(const struct s32cc_clk_obj *module) |
| 434 | { |
| 435 | const struct s32cc_pll_out_div *pdiv = s32cc_obj2plldiv(module); |
| 436 | |
| 437 | if (pdiv->parent == NULL) { |
| 438 | ERROR("Failed to identify PLL DIV's parent\n"); |
| 439 | } |
| 440 | |
| 441 | return pdiv->parent; |
| 442 | } |
| 443 | |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 444 | static int enable_pll_div(struct s32cc_clk_obj *module, |
Ghennadi Procopciuc | 84e8208 | 2024-06-12 14:30:30 +0300 | [diff] [blame] | 445 | const struct s32cc_clk_drv *drv, |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 446 | unsigned int depth) |
Ghennadi Procopciuc | 84e8208 | 2024-06-12 14:30:30 +0300 | [diff] [blame] | 447 | { |
| 448 | const struct s32cc_pll_out_div *pdiv = s32cc_obj2plldiv(module); |
| 449 | uintptr_t pll_addr = 0x0ULL; |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 450 | unsigned int ldepth = depth; |
Ghennadi Procopciuc | 84e8208 | 2024-06-12 14:30:30 +0300 | [diff] [blame] | 451 | const struct s32cc_pll *pll; |
| 452 | uint32_t dc; |
| 453 | int ret; |
| 454 | |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 455 | ret = update_stack_depth(&ldepth); |
Ghennadi Procopciuc | 84e8208 | 2024-06-12 14:30:30 +0300 | [diff] [blame] | 456 | if (ret != 0) { |
| 457 | return ret; |
| 458 | } |
| 459 | |
| 460 | pll = get_div_pll(pdiv); |
| 461 | if (pll == NULL) { |
| 462 | ERROR("The parent of the PLL DIV is invalid\n"); |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | ret = get_base_addr(pll->instance, drv, &pll_addr); |
| 467 | if (ret != 0) { |
| 468 | ERROR("Failed to detect PLL instance\n"); |
| 469 | return -EINVAL; |
| 470 | } |
| 471 | |
| 472 | dc = (uint32_t)(pll->vco_freq / pdiv->freq); |
| 473 | |
| 474 | config_pll_out_div(pll_addr, pdiv->index, dc); |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
Ghennadi Procopciuc | 7004f67 | 2024-06-12 14:44:47 +0300 | [diff] [blame] | 479 | static int cgm_mux_clk_config(uintptr_t cgm_addr, uint32_t mux, uint32_t source, |
| 480 | bool safe_clk) |
| 481 | { |
| 482 | uint32_t css, csc; |
| 483 | |
| 484 | css = mmio_read_32(CGM_MUXn_CSS(cgm_addr, mux)); |
| 485 | |
| 486 | /* Already configured */ |
| 487 | if ((MC_CGM_MUXn_CSS_SELSTAT(css) == source) && |
| 488 | (MC_CGM_MUXn_CSS_SWTRG(css) == MC_CGM_MUXn_CSS_SWTRG_SUCCESS) && |
| 489 | ((css & MC_CGM_MUXn_CSS_SWIP) == 0U) && !safe_clk) { |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | /* Ongoing clock switch? */ |
| 494 | while ((mmio_read_32(CGM_MUXn_CSS(cgm_addr, mux)) & |
| 495 | MC_CGM_MUXn_CSS_SWIP) != 0U) { |
| 496 | } |
| 497 | |
| 498 | csc = mmio_read_32(CGM_MUXn_CSC(cgm_addr, mux)); |
| 499 | |
| 500 | /* Clear previous source. */ |
| 501 | csc &= ~(MC_CGM_MUXn_CSC_SELCTL_MASK); |
| 502 | |
| 503 | if (!safe_clk) { |
| 504 | /* Select the clock source and trigger the clock switch. */ |
| 505 | csc |= MC_CGM_MUXn_CSC_SELCTL(source) | MC_CGM_MUXn_CSC_CLK_SW; |
| 506 | } else { |
| 507 | /* Switch to safe clock */ |
| 508 | csc |= MC_CGM_MUXn_CSC_SAFE_SW; |
| 509 | } |
| 510 | |
| 511 | mmio_write_32(CGM_MUXn_CSC(cgm_addr, mux), csc); |
| 512 | |
| 513 | /* Wait for configuration bit to auto-clear. */ |
| 514 | while ((mmio_read_32(CGM_MUXn_CSC(cgm_addr, mux)) & |
| 515 | MC_CGM_MUXn_CSC_CLK_SW) != 0U) { |
| 516 | } |
| 517 | |
| 518 | /* Is the clock switch completed? */ |
| 519 | while ((mmio_read_32(CGM_MUXn_CSS(cgm_addr, mux)) & |
| 520 | MC_CGM_MUXn_CSS_SWIP) != 0U) { |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | * Check if the switch succeeded. |
| 525 | * Check switch trigger cause and the source. |
| 526 | */ |
| 527 | css = mmio_read_32(CGM_MUXn_CSS(cgm_addr, mux)); |
| 528 | if (!safe_clk) { |
| 529 | if ((MC_CGM_MUXn_CSS_SWTRG(css) == MC_CGM_MUXn_CSS_SWTRG_SUCCESS) && |
| 530 | (MC_CGM_MUXn_CSS_SELSTAT(css) == source)) { |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | ERROR("Failed to change the source of mux %" PRIu32 " to %" PRIu32 " (CGM=%lu)\n", |
| 535 | mux, source, cgm_addr); |
| 536 | } else { |
| 537 | if (((MC_CGM_MUXn_CSS_SWTRG(css) == MC_CGM_MUXn_CSS_SWTRG_SAFE_CLK) || |
| 538 | (MC_CGM_MUXn_CSS_SWTRG(css) == MC_CGM_MUXn_CSS_SWTRG_SAFE_CLK_INACTIVE)) && |
| 539 | ((MC_CGM_MUXn_CSS_SAFE_SW & css) != 0U)) { |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | ERROR("The switch of mux %" PRIu32 " (CGM=%lu) to safe clock failed\n", |
| 544 | mux, cgm_addr); |
| 545 | } |
| 546 | |
| 547 | return -EINVAL; |
| 548 | } |
| 549 | |
| 550 | static int enable_cgm_mux(const struct s32cc_clkmux *mux, |
| 551 | const struct s32cc_clk_drv *drv) |
| 552 | { |
| 553 | uintptr_t cgm_addr = UL(0x0); |
| 554 | uint32_t mux_hw_clk; |
| 555 | int ret; |
| 556 | |
| 557 | ret = get_base_addr(mux->module, drv, &cgm_addr); |
| 558 | if (ret != 0) { |
| 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | mux_hw_clk = (uint32_t)S32CC_CLK_ID(mux->source_id); |
| 563 | |
| 564 | return cgm_mux_clk_config(cgm_addr, mux->index, |
| 565 | mux_hw_clk, false); |
| 566 | } |
| 567 | |
Ghennadi Procopciuc | 96e069c | 2024-09-11 09:29:50 +0300 | [diff] [blame] | 568 | static struct s32cc_clk_obj *get_mux_parent(const struct s32cc_clk_obj *module) |
| 569 | { |
| 570 | const struct s32cc_clkmux *mux = s32cc_obj2clkmux(module); |
| 571 | struct s32cc_clk *clk; |
| 572 | |
| 573 | if (mux == NULL) { |
| 574 | return NULL; |
| 575 | } |
| 576 | |
| 577 | clk = s32cc_get_arch_clk(mux->source_id); |
| 578 | if (clk == NULL) { |
| 579 | ERROR("Invalid parent (%lu) for mux %" PRIu8 "\n", |
| 580 | mux->source_id, mux->index); |
| 581 | return NULL; |
| 582 | } |
| 583 | |
| 584 | return &clk->desc; |
| 585 | } |
| 586 | |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 587 | static int enable_mux(struct s32cc_clk_obj *module, |
Ghennadi Procopciuc | 7004f67 | 2024-06-12 14:44:47 +0300 | [diff] [blame] | 588 | const struct s32cc_clk_drv *drv, |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 589 | unsigned int depth) |
Ghennadi Procopciuc | 7004f67 | 2024-06-12 14:44:47 +0300 | [diff] [blame] | 590 | { |
| 591 | const struct s32cc_clkmux *mux = s32cc_obj2clkmux(module); |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 592 | unsigned int ldepth = depth; |
Ghennadi Procopciuc | 7004f67 | 2024-06-12 14:44:47 +0300 | [diff] [blame] | 593 | const struct s32cc_clk *clk; |
| 594 | int ret = 0; |
| 595 | |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 596 | ret = update_stack_depth(&ldepth); |
Ghennadi Procopciuc | 7004f67 | 2024-06-12 14:44:47 +0300 | [diff] [blame] | 597 | if (ret != 0) { |
| 598 | return ret; |
| 599 | } |
| 600 | |
| 601 | if (mux == NULL) { |
| 602 | return -EINVAL; |
| 603 | } |
| 604 | |
| 605 | clk = s32cc_get_arch_clk(mux->source_id); |
| 606 | if (clk == NULL) { |
| 607 | ERROR("Invalid parent (%lu) for mux %" PRIu8 "\n", |
| 608 | mux->source_id, mux->index); |
| 609 | return -EINVAL; |
| 610 | } |
| 611 | |
| 612 | switch (mux->module) { |
| 613 | /* PLL mux will be enabled by PLL setup */ |
| 614 | case S32CC_ARM_PLL: |
Ghennadi Procopciuc | f8490b8 | 2024-09-11 14:54:57 +0300 | [diff] [blame] | 615 | case S32CC_PERIPH_PLL: |
Ghennadi Procopciuc | 18c2b13 | 2024-09-09 10:24:35 +0300 | [diff] [blame] | 616 | case S32CC_DDR_PLL: |
Ghennadi Procopciuc | 7004f67 | 2024-06-12 14:44:47 +0300 | [diff] [blame] | 617 | break; |
| 618 | case S32CC_CGM1: |
| 619 | ret = enable_cgm_mux(mux, drv); |
| 620 | break; |
Ghennadi Procopciuc | 9dbca85 | 2024-08-05 16:50:52 +0300 | [diff] [blame] | 621 | case S32CC_CGM0: |
| 622 | ret = enable_cgm_mux(mux, drv); |
| 623 | break; |
Ghennadi Procopciuc | 8a4f840 | 2024-09-17 11:22:30 +0300 | [diff] [blame] | 624 | case S32CC_CGM5: |
| 625 | ret = enable_cgm_mux(mux, drv); |
| 626 | break; |
Ghennadi Procopciuc | 7004f67 | 2024-06-12 14:44:47 +0300 | [diff] [blame] | 627 | default: |
| 628 | ERROR("Unknown mux parent type: %d\n", mux->module); |
| 629 | ret = -EINVAL; |
| 630 | break; |
| 631 | }; |
| 632 | |
| 633 | return ret; |
| 634 | } |
| 635 | |
Ghennadi Procopciuc | 96e069c | 2024-09-11 09:29:50 +0300 | [diff] [blame] | 636 | static struct s32cc_clk_obj *get_dfs_parent(const struct s32cc_clk_obj *module) |
| 637 | { |
| 638 | const struct s32cc_dfs *dfs = s32cc_obj2dfs(module); |
| 639 | |
| 640 | if (dfs->parent == NULL) { |
| 641 | ERROR("Failed to identify DFS's parent\n"); |
| 642 | } |
| 643 | |
| 644 | return dfs->parent; |
| 645 | } |
| 646 | |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 647 | static int enable_dfs(struct s32cc_clk_obj *module, |
Ghennadi Procopciuc | 4cd04c5 | 2024-08-05 16:48:49 +0300 | [diff] [blame] | 648 | const struct s32cc_clk_drv *drv, |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 649 | unsigned int depth) |
Ghennadi Procopciuc | 4cd04c5 | 2024-08-05 16:48:49 +0300 | [diff] [blame] | 650 | { |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 651 | unsigned int ldepth = depth; |
Ghennadi Procopciuc | 4cd04c5 | 2024-08-05 16:48:49 +0300 | [diff] [blame] | 652 | int ret = 0; |
| 653 | |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 654 | ret = update_stack_depth(&ldepth); |
Ghennadi Procopciuc | 4cd04c5 | 2024-08-05 16:48:49 +0300 | [diff] [blame] | 655 | if (ret != 0) { |
| 656 | return ret; |
| 657 | } |
| 658 | |
| 659 | return 0; |
| 660 | } |
| 661 | |
Ghennadi Procopciuc | 2fb2550 | 2025-01-13 09:50:51 +0200 | [diff] [blame] | 662 | static int get_dfs_freq(const struct s32cc_clk_obj *module, |
| 663 | const struct s32cc_clk_drv *drv, |
| 664 | unsigned long *rate, unsigned int depth) |
| 665 | { |
| 666 | const struct s32cc_dfs *dfs = s32cc_obj2dfs(module); |
| 667 | unsigned int ldepth = depth; |
| 668 | uintptr_t dfs_addr; |
| 669 | int ret; |
| 670 | |
| 671 | ret = update_stack_depth(&ldepth); |
| 672 | if (ret != 0) { |
| 673 | return ret; |
| 674 | } |
| 675 | |
| 676 | ret = get_base_addr(dfs->instance, drv, &dfs_addr); |
| 677 | if (ret != 0) { |
| 678 | ERROR("Failed to detect the DFS instance\n"); |
| 679 | return ret; |
| 680 | } |
| 681 | |
| 682 | return get_module_rate(dfs->parent, drv, rate, ldepth); |
| 683 | } |
| 684 | |
Ghennadi Procopciuc | 4cd04c5 | 2024-08-05 16:48:49 +0300 | [diff] [blame] | 685 | static struct s32cc_dfs *get_div_dfs(const struct s32cc_dfs_div *dfs_div) |
| 686 | { |
| 687 | const struct s32cc_clk_obj *parent = dfs_div->parent; |
| 688 | |
| 689 | if (parent->type != s32cc_dfs_t) { |
| 690 | ERROR("DFS DIV doesn't have a DFS as parent\n"); |
| 691 | return NULL; |
| 692 | } |
| 693 | |
| 694 | return s32cc_obj2dfs(parent); |
| 695 | } |
| 696 | |
| 697 | static struct s32cc_pll *dfsdiv2pll(const struct s32cc_dfs_div *dfs_div) |
| 698 | { |
| 699 | const struct s32cc_clk_obj *parent; |
| 700 | const struct s32cc_dfs *dfs; |
| 701 | |
| 702 | dfs = get_div_dfs(dfs_div); |
| 703 | if (dfs == NULL) { |
| 704 | return NULL; |
| 705 | } |
| 706 | |
| 707 | parent = dfs->parent; |
| 708 | if (parent->type != s32cc_pll_t) { |
| 709 | return NULL; |
| 710 | } |
| 711 | |
| 712 | return s32cc_obj2pll(parent); |
| 713 | } |
| 714 | |
| 715 | static int get_dfs_mfi_mfn(unsigned long dfs_freq, const struct s32cc_dfs_div *dfs_div, |
| 716 | uint32_t *mfi, uint32_t *mfn) |
| 717 | { |
| 718 | uint64_t factor64, tmp64, ofreq; |
| 719 | uint32_t factor32; |
| 720 | |
| 721 | unsigned long in = dfs_freq; |
| 722 | unsigned long out = dfs_div->freq; |
| 723 | |
| 724 | /** |
| 725 | * factor = (IN / OUT) / 2 |
| 726 | * MFI = integer(factor) |
| 727 | * MFN = (factor - MFI) * 36 |
| 728 | */ |
| 729 | factor64 = ((((uint64_t)in) * FP_PRECISION) / ((uint64_t)out)) / 2ULL; |
| 730 | tmp64 = factor64 / FP_PRECISION; |
| 731 | if (tmp64 > UINT32_MAX) { |
| 732 | return -EINVAL; |
| 733 | } |
| 734 | |
| 735 | factor32 = (uint32_t)tmp64; |
| 736 | *mfi = factor32; |
| 737 | |
| 738 | tmp64 = ((factor64 - ((uint64_t)*mfi * FP_PRECISION)) * 36UL) / FP_PRECISION; |
| 739 | if (tmp64 > UINT32_MAX) { |
| 740 | return -EINVAL; |
| 741 | } |
| 742 | |
| 743 | *mfn = (uint32_t)tmp64; |
| 744 | |
| 745 | /* div_freq = in / (2 * (*mfi + *mfn / 36.0)) */ |
| 746 | factor64 = (((uint64_t)*mfn) * FP_PRECISION) / 36ULL; |
| 747 | factor64 += ((uint64_t)*mfi) * FP_PRECISION; |
| 748 | factor64 *= 2ULL; |
| 749 | ofreq = (((uint64_t)in) * FP_PRECISION) / factor64; |
| 750 | |
| 751 | if (ofreq != dfs_div->freq) { |
| 752 | ERROR("Failed to find MFI and MFN settings for DFS DIV freq %lu\n", |
| 753 | dfs_div->freq); |
| 754 | ERROR("Nearest freq = %" PRIx64 "\n", ofreq); |
| 755 | return -EINVAL; |
| 756 | } |
| 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | static int init_dfs_port(uintptr_t dfs_addr, uint32_t port, |
| 762 | uint32_t mfi, uint32_t mfn) |
| 763 | { |
| 764 | uint32_t portsr, portolsr; |
| 765 | uint32_t mask, old_mfi, old_mfn; |
| 766 | uint32_t dvport; |
| 767 | bool init_dfs; |
| 768 | |
| 769 | dvport = mmio_read_32(DFS_DVPORTn(dfs_addr, port)); |
| 770 | |
| 771 | old_mfi = DFS_DVPORTn_MFI(dvport); |
| 772 | old_mfn = DFS_DVPORTn_MFN(dvport); |
| 773 | |
| 774 | portsr = mmio_read_32(DFS_PORTSR(dfs_addr)); |
| 775 | portolsr = mmio_read_32(DFS_PORTOLSR(dfs_addr)); |
| 776 | |
| 777 | /* Skip configuration if it's not needed */ |
| 778 | if (((portsr & BIT_32(port)) != 0U) && |
| 779 | ((portolsr & BIT_32(port)) == 0U) && |
| 780 | (mfi == old_mfi) && (mfn == old_mfn)) { |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | init_dfs = (portsr == 0U); |
| 785 | |
| 786 | if (init_dfs) { |
| 787 | mask = DFS_PORTRESET_MASK; |
| 788 | } else { |
| 789 | mask = DFS_PORTRESET_SET(BIT_32(port)); |
| 790 | } |
| 791 | |
| 792 | mmio_write_32(DFS_PORTOLSR(dfs_addr), mask); |
| 793 | mmio_write_32(DFS_PORTRESET(dfs_addr), mask); |
| 794 | |
| 795 | while ((mmio_read_32(DFS_PORTSR(dfs_addr)) & mask) != 0U) { |
| 796 | } |
| 797 | |
| 798 | if (init_dfs) { |
| 799 | mmio_write_32(DFS_CTL(dfs_addr), DFS_CTL_RESET); |
| 800 | } |
| 801 | |
| 802 | mmio_write_32(DFS_DVPORTn(dfs_addr, port), |
| 803 | DFS_DVPORTn_MFI_SET(mfi) | DFS_DVPORTn_MFN_SET(mfn)); |
| 804 | |
| 805 | if (init_dfs) { |
| 806 | /* DFS clk enable programming */ |
| 807 | mmio_clrbits_32(DFS_CTL(dfs_addr), DFS_CTL_RESET); |
| 808 | } |
| 809 | |
| 810 | mmio_clrbits_32(DFS_PORTRESET(dfs_addr), BIT_32(port)); |
| 811 | |
| 812 | while ((mmio_read_32(DFS_PORTSR(dfs_addr)) & BIT_32(port)) != BIT_32(port)) { |
| 813 | } |
| 814 | |
| 815 | portolsr = mmio_read_32(DFS_PORTOLSR(dfs_addr)); |
| 816 | if ((portolsr & DFS_PORTOLSR_LOL(port)) != 0U) { |
| 817 | ERROR("Failed to lock DFS divider\n"); |
| 818 | return -EINVAL; |
| 819 | } |
| 820 | |
| 821 | return 0; |
| 822 | } |
| 823 | |
Ghennadi Procopciuc | 96e069c | 2024-09-11 09:29:50 +0300 | [diff] [blame] | 824 | static struct s32cc_clk_obj * |
| 825 | get_dfs_div_parent(const struct s32cc_clk_obj *module) |
| 826 | { |
| 827 | const struct s32cc_dfs_div *dfs_div = s32cc_obj2dfsdiv(module); |
| 828 | |
| 829 | if (dfs_div->parent == NULL) { |
| 830 | ERROR("Failed to identify DFS divider's parent\n"); |
| 831 | } |
| 832 | |
| 833 | return dfs_div->parent; |
| 834 | } |
| 835 | |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 836 | static int enable_dfs_div(struct s32cc_clk_obj *module, |
Ghennadi Procopciuc | 4cd04c5 | 2024-08-05 16:48:49 +0300 | [diff] [blame] | 837 | const struct s32cc_clk_drv *drv, |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 838 | unsigned int depth) |
Ghennadi Procopciuc | 4cd04c5 | 2024-08-05 16:48:49 +0300 | [diff] [blame] | 839 | { |
| 840 | const struct s32cc_dfs_div *dfs_div = s32cc_obj2dfsdiv(module); |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 841 | unsigned int ldepth = depth; |
Ghennadi Procopciuc | 4cd04c5 | 2024-08-05 16:48:49 +0300 | [diff] [blame] | 842 | const struct s32cc_pll *pll; |
| 843 | const struct s32cc_dfs *dfs; |
| 844 | uintptr_t dfs_addr = 0UL; |
| 845 | uint32_t mfi, mfn; |
| 846 | int ret = 0; |
| 847 | |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 848 | ret = update_stack_depth(&ldepth); |
Ghennadi Procopciuc | 4cd04c5 | 2024-08-05 16:48:49 +0300 | [diff] [blame] | 849 | if (ret != 0) { |
| 850 | return ret; |
| 851 | } |
| 852 | |
| 853 | dfs = get_div_dfs(dfs_div); |
| 854 | if (dfs == NULL) { |
| 855 | return -EINVAL; |
| 856 | } |
| 857 | |
| 858 | pll = dfsdiv2pll(dfs_div); |
| 859 | if (pll == NULL) { |
| 860 | ERROR("Failed to identify DFS divider's parent\n"); |
| 861 | return -EINVAL; |
| 862 | } |
| 863 | |
| 864 | ret = get_base_addr(dfs->instance, drv, &dfs_addr); |
| 865 | if ((ret != 0) || (dfs_addr == 0UL)) { |
| 866 | return -EINVAL; |
| 867 | } |
| 868 | |
| 869 | ret = get_dfs_mfi_mfn(pll->vco_freq, dfs_div, &mfi, &mfn); |
| 870 | if (ret != 0) { |
| 871 | return -EINVAL; |
| 872 | } |
| 873 | |
| 874 | return init_dfs_port(dfs_addr, dfs_div->index, mfi, mfn); |
| 875 | } |
| 876 | |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 877 | typedef int (*enable_clk_t)(struct s32cc_clk_obj *module, |
| 878 | const struct s32cc_clk_drv *drv, |
| 879 | unsigned int depth); |
| 880 | |
Ghennadi Procopciuc | 8a4f840 | 2024-09-17 11:22:30 +0300 | [diff] [blame] | 881 | static int enable_part(struct s32cc_clk_obj *module, |
| 882 | const struct s32cc_clk_drv *drv, |
| 883 | unsigned int depth) |
| 884 | { |
| 885 | const struct s32cc_part *part = s32cc_obj2part(module); |
| 886 | uint32_t part_no = part->partition_id; |
| 887 | |
| 888 | if ((drv->mc_me == 0UL) || (drv->mc_rgm == 0UL) || (drv->rdc == 0UL)) { |
| 889 | return -EINVAL; |
| 890 | } |
| 891 | |
| 892 | return mc_me_enable_partition(drv->mc_me, drv->mc_rgm, drv->rdc, part_no); |
| 893 | } |
| 894 | |
| 895 | static int enable_part_block(struct s32cc_clk_obj *module, |
| 896 | const struct s32cc_clk_drv *drv, |
| 897 | unsigned int depth) |
| 898 | { |
| 899 | const struct s32cc_part_block *block = s32cc_obj2partblock(module); |
| 900 | const struct s32cc_part *part = block->part; |
| 901 | uint32_t part_no = part->partition_id; |
| 902 | unsigned int ldepth = depth; |
| 903 | uint32_t cofb; |
| 904 | int ret; |
| 905 | |
| 906 | ret = update_stack_depth(&ldepth); |
| 907 | if (ret != 0) { |
| 908 | return ret; |
| 909 | } |
| 910 | |
| 911 | if ((block->block >= s32cc_part_block0) && |
| 912 | (block->block <= s32cc_part_block15)) { |
| 913 | cofb = (uint32_t)block->block - (uint32_t)s32cc_part_block0; |
| 914 | mc_me_enable_part_cofb(drv->mc_me, part_no, cofb, block->status); |
| 915 | } else { |
| 916 | ERROR("Unknown partition block type: %d\n", block->block); |
| 917 | return -EINVAL; |
| 918 | } |
| 919 | |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | static struct s32cc_clk_obj * |
| 924 | get_part_block_parent(const struct s32cc_clk_obj *module) |
| 925 | { |
| 926 | const struct s32cc_part_block *block = s32cc_obj2partblock(module); |
| 927 | |
| 928 | return &block->part->desc; |
| 929 | } |
| 930 | |
| 931 | static int enable_module_with_refcount(struct s32cc_clk_obj *module, |
| 932 | const struct s32cc_clk_drv *drv, |
| 933 | unsigned int depth); |
| 934 | |
| 935 | static int enable_part_block_link(struct s32cc_clk_obj *module, |
| 936 | const struct s32cc_clk_drv *drv, |
| 937 | unsigned int depth) |
| 938 | { |
| 939 | const struct s32cc_part_block_link *link = s32cc_obj2partblocklink(module); |
| 940 | struct s32cc_part_block *block = link->block; |
| 941 | unsigned int ldepth = depth; |
| 942 | int ret; |
| 943 | |
| 944 | ret = update_stack_depth(&ldepth); |
| 945 | if (ret != 0) { |
| 946 | return ret; |
| 947 | } |
| 948 | |
| 949 | /* Move the enablement algorithm to partition tree */ |
| 950 | return enable_module_with_refcount(&block->desc, drv, ldepth); |
| 951 | } |
| 952 | |
| 953 | static struct s32cc_clk_obj * |
| 954 | get_part_block_link_parent(const struct s32cc_clk_obj *module) |
| 955 | { |
| 956 | const struct s32cc_part_block_link *link = s32cc_obj2partblocklink(module); |
| 957 | |
| 958 | return link->parent; |
| 959 | } |
| 960 | |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 961 | static int no_enable(struct s32cc_clk_obj *module, |
| 962 | const struct s32cc_clk_drv *drv, |
| 963 | unsigned int depth) |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 964 | { |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 965 | return 0; |
| 966 | } |
| 967 | |
| 968 | static int exec_cb_with_refcount(enable_clk_t en_cb, struct s32cc_clk_obj *mod, |
| 969 | const struct s32cc_clk_drv *drv, bool leaf_node, |
| 970 | unsigned int depth) |
| 971 | { |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 972 | unsigned int ldepth = depth; |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 973 | int ret = 0; |
| 974 | |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 975 | if (mod == NULL) { |
| 976 | return 0; |
| 977 | } |
| 978 | |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 979 | ret = update_stack_depth(&ldepth); |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 980 | if (ret != 0) { |
| 981 | return ret; |
| 982 | } |
| 983 | |
| 984 | /* Refcount will be updated as part of the recursivity */ |
| 985 | if (leaf_node) { |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 986 | return en_cb(mod, drv, ldepth); |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | if (mod->refcount == 0U) { |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 990 | ret = en_cb(mod, drv, ldepth); |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | if (ret == 0) { |
| 994 | mod->refcount++; |
| 995 | } |
| 996 | |
| 997 | return ret; |
| 998 | } |
| 999 | |
| 1000 | static struct s32cc_clk_obj *get_module_parent(const struct s32cc_clk_obj *module); |
| 1001 | |
| 1002 | static int enable_module(struct s32cc_clk_obj *module, |
| 1003 | const struct s32cc_clk_drv *drv, |
| 1004 | unsigned int depth) |
| 1005 | { |
| 1006 | struct s32cc_clk_obj *parent = get_module_parent(module); |
Ghennadi Procopciuc | 8a4f840 | 2024-09-17 11:22:30 +0300 | [diff] [blame] | 1007 | static const enable_clk_t enable_clbs[12] = { |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 1008 | [s32cc_clk_t] = no_enable, |
| 1009 | [s32cc_osc_t] = enable_osc, |
| 1010 | [s32cc_pll_t] = enable_pll, |
| 1011 | [s32cc_pll_out_div_t] = enable_pll_div, |
| 1012 | [s32cc_clkmux_t] = enable_mux, |
| 1013 | [s32cc_shared_clkmux_t] = enable_mux, |
| 1014 | [s32cc_dfs_t] = enable_dfs, |
| 1015 | [s32cc_dfs_div_t] = enable_dfs_div, |
Ghennadi Procopciuc | 8a4f840 | 2024-09-17 11:22:30 +0300 | [diff] [blame] | 1016 | [s32cc_part_t] = enable_part, |
| 1017 | [s32cc_part_block_t] = enable_part_block, |
| 1018 | [s32cc_part_block_link_t] = enable_part_block_link, |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 1019 | }; |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 1020 | unsigned int ldepth = depth; |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 1021 | uint32_t index; |
| 1022 | int ret = 0; |
| 1023 | |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 1024 | ret = update_stack_depth(&ldepth); |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 1025 | if (ret != 0) { |
| 1026 | return ret; |
| 1027 | } |
| 1028 | |
| 1029 | if (drv == NULL) { |
| 1030 | return -EINVAL; |
| 1031 | } |
| 1032 | |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 1033 | index = (uint32_t)module->type; |
| 1034 | |
| 1035 | if (index >= ARRAY_SIZE(enable_clbs)) { |
| 1036 | ERROR("Undefined module type: %d\n", module->type); |
| 1037 | return -EINVAL; |
| 1038 | } |
| 1039 | |
| 1040 | if (enable_clbs[index] == NULL) { |
| 1041 | ERROR("Undefined callback for the clock type: %d\n", |
| 1042 | module->type); |
| 1043 | return -EINVAL; |
| 1044 | } |
| 1045 | |
| 1046 | parent = get_module_parent(module); |
| 1047 | |
| 1048 | ret = exec_cb_with_refcount(enable_module, parent, drv, |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 1049 | false, ldepth); |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 1050 | if (ret != 0) { |
| 1051 | return ret; |
| 1052 | } |
| 1053 | |
| 1054 | ret = exec_cb_with_refcount(enable_clbs[index], module, drv, |
Ghennadi Procopciuc | 8ee0fc3 | 2024-09-30 09:39:15 +0300 | [diff] [blame] | 1055 | true, ldepth); |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 1056 | if (ret != 0) { |
| 1057 | return ret; |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | return ret; |
| 1061 | } |
| 1062 | |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 1063 | static int enable_module_with_refcount(struct s32cc_clk_obj *module, |
| 1064 | const struct s32cc_clk_drv *drv, |
| 1065 | unsigned int depth) |
| 1066 | { |
| 1067 | return exec_cb_with_refcount(enable_module, module, drv, false, depth); |
| 1068 | } |
| 1069 | |
Ghennadi Procopciuc | 3a580e9 | 2024-06-11 18:39:58 +0300 | [diff] [blame] | 1070 | static int s32cc_clk_enable(unsigned long id) |
| 1071 | { |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 1072 | const struct s32cc_clk_drv *drv = get_drv(); |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 1073 | unsigned int depth = MAX_STACK_DEPTH; |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 1074 | struct s32cc_clk *clk; |
Ghennadi Procopciuc | 8ab3435 | 2024-06-12 09:25:17 +0300 | [diff] [blame] | 1075 | |
| 1076 | clk = s32cc_get_arch_clk(id); |
| 1077 | if (clk == NULL) { |
| 1078 | return -EINVAL; |
| 1079 | } |
| 1080 | |
Ghennadi Procopciuc | 5300040 | 2024-09-09 13:00:26 +0300 | [diff] [blame] | 1081 | return enable_module_with_refcount(&clk->desc, drv, depth); |
Ghennadi Procopciuc | 3a580e9 | 2024-06-11 18:39:58 +0300 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | static void s32cc_clk_disable(unsigned long id) |
| 1085 | { |
| 1086 | } |
| 1087 | |
| 1088 | static bool s32cc_clk_is_enabled(unsigned long id) |
| 1089 | { |
| 1090 | return false; |
| 1091 | } |
| 1092 | |
Ghennadi Procopciuc | d937351 | 2024-06-12 08:09:19 +0300 | [diff] [blame] | 1093 | static int set_osc_freq(const struct s32cc_clk_obj *module, unsigned long rate, |
| 1094 | unsigned long *orate, unsigned int *depth) |
| 1095 | { |
| 1096 | struct s32cc_osc *osc = s32cc_obj2osc(module); |
| 1097 | int ret; |
| 1098 | |
| 1099 | ret = update_stack_depth(depth); |
| 1100 | if (ret != 0) { |
| 1101 | return ret; |
| 1102 | } |
| 1103 | |
| 1104 | if ((osc->freq != 0UL) && (rate != osc->freq)) { |
| 1105 | ERROR("Already initialized oscillator. freq = %lu\n", |
| 1106 | osc->freq); |
| 1107 | return -EINVAL; |
| 1108 | } |
| 1109 | |
| 1110 | osc->freq = rate; |
| 1111 | *orate = osc->freq; |
| 1112 | |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
Ghennadi Procopciuc | bd69113 | 2025-01-10 16:26:21 +0200 | [diff] [blame] | 1116 | static int get_osc_freq(const struct s32cc_clk_obj *module, |
| 1117 | const struct s32cc_clk_drv *drv, |
| 1118 | unsigned long *rate, unsigned int depth) |
| 1119 | { |
| 1120 | const struct s32cc_osc *osc = s32cc_obj2osc(module); |
| 1121 | unsigned int ldepth = depth; |
| 1122 | int ret; |
| 1123 | |
| 1124 | ret = update_stack_depth(&ldepth); |
| 1125 | if (ret != 0) { |
| 1126 | return ret; |
| 1127 | } |
| 1128 | |
| 1129 | if (osc->freq == 0UL) { |
| 1130 | ERROR("Uninitialized oscillator\n"); |
| 1131 | return -EINVAL; |
| 1132 | } |
| 1133 | |
| 1134 | *rate = osc->freq; |
| 1135 | |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
Ghennadi Procopciuc | d937351 | 2024-06-12 08:09:19 +0300 | [diff] [blame] | 1139 | static int set_clk_freq(const struct s32cc_clk_obj *module, unsigned long rate, |
| 1140 | unsigned long *orate, unsigned int *depth) |
| 1141 | { |
| 1142 | const struct s32cc_clk *clk = s32cc_obj2clk(module); |
| 1143 | int ret; |
| 1144 | |
| 1145 | ret = update_stack_depth(depth); |
| 1146 | if (ret != 0) { |
| 1147 | return ret; |
| 1148 | } |
| 1149 | |
| 1150 | if ((clk->min_freq != 0UL) && (clk->max_freq != 0UL) && |
| 1151 | ((rate < clk->min_freq) || (rate > clk->max_freq))) { |
| 1152 | ERROR("%lu frequency is out of the allowed range: [%lu:%lu]\n", |
| 1153 | rate, clk->min_freq, clk->max_freq); |
| 1154 | return -EINVAL; |
| 1155 | } |
| 1156 | |
| 1157 | if (clk->module != NULL) { |
| 1158 | return set_module_rate(clk->module, rate, orate, depth); |
| 1159 | } |
| 1160 | |
| 1161 | if (clk->pclock != NULL) { |
| 1162 | return set_clk_freq(&clk->pclock->desc, rate, orate, depth); |
| 1163 | } |
| 1164 | |
| 1165 | return -EINVAL; |
| 1166 | } |
| 1167 | |
Ghennadi Procopciuc | 46de0b9 | 2025-01-10 16:33:36 +0200 | [diff] [blame] | 1168 | static int get_clk_freq(const struct s32cc_clk_obj *module, |
| 1169 | const struct s32cc_clk_drv *drv, unsigned long *rate, |
| 1170 | unsigned int depth) |
| 1171 | { |
| 1172 | const struct s32cc_clk *clk = s32cc_obj2clk(module); |
| 1173 | unsigned int ldepth = depth; |
| 1174 | int ret; |
| 1175 | |
| 1176 | ret = update_stack_depth(&ldepth); |
| 1177 | if (ret != 0) { |
| 1178 | return ret; |
| 1179 | } |
| 1180 | |
| 1181 | if (clk == NULL) { |
| 1182 | ERROR("Invalid clock\n"); |
| 1183 | return -EINVAL; |
| 1184 | } |
| 1185 | |
| 1186 | if (clk->module != NULL) { |
| 1187 | return get_module_rate(clk->module, drv, rate, ldepth); |
| 1188 | } |
| 1189 | |
| 1190 | if (clk->pclock == NULL) { |
| 1191 | ERROR("Invalid clock parent\n"); |
| 1192 | return -EINVAL; |
| 1193 | } |
| 1194 | |
| 1195 | return get_clk_freq(&clk->pclock->desc, drv, rate, ldepth); |
| 1196 | } |
| 1197 | |
Ghennadi Procopciuc | 7ad4e23 | 2024-06-12 11:55:32 +0300 | [diff] [blame] | 1198 | static int set_pll_freq(const struct s32cc_clk_obj *module, unsigned long rate, |
| 1199 | unsigned long *orate, unsigned int *depth) |
| 1200 | { |
| 1201 | struct s32cc_pll *pll = s32cc_obj2pll(module); |
| 1202 | int ret; |
| 1203 | |
| 1204 | ret = update_stack_depth(depth); |
| 1205 | if (ret != 0) { |
| 1206 | return ret; |
| 1207 | } |
| 1208 | |
| 1209 | if ((pll->vco_freq != 0UL) && (pll->vco_freq != rate)) { |
| 1210 | ERROR("PLL frequency was already set\n"); |
| 1211 | return -EINVAL; |
| 1212 | } |
| 1213 | |
| 1214 | pll->vco_freq = rate; |
| 1215 | *orate = pll->vco_freq; |
| 1216 | |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
Ghennadi Procopciuc | fbebafa | 2025-01-13 09:33:42 +0200 | [diff] [blame] | 1220 | static int get_pll_freq(const struct s32cc_clk_obj *module, |
| 1221 | const struct s32cc_clk_drv *drv, |
| 1222 | unsigned long *rate, unsigned int depth) |
| 1223 | { |
| 1224 | const struct s32cc_pll *pll = s32cc_obj2pll(module); |
| 1225 | const struct s32cc_clk *source; |
| 1226 | uint32_t mfi, mfn, rdiv, plldv; |
| 1227 | unsigned long prate, clk_src; |
| 1228 | unsigned int ldepth = depth; |
| 1229 | uintptr_t pll_addr = 0UL; |
| 1230 | uint64_t t1, t2; |
| 1231 | uint32_t pllpd; |
| 1232 | int ret; |
| 1233 | |
| 1234 | ret = update_stack_depth(&ldepth); |
| 1235 | if (ret != 0) { |
| 1236 | return ret; |
| 1237 | } |
| 1238 | |
| 1239 | ret = get_base_addr(pll->instance, drv, &pll_addr); |
| 1240 | if (ret != 0) { |
| 1241 | ERROR("Failed to detect PLL instance\n"); |
| 1242 | return ret; |
| 1243 | } |
| 1244 | |
| 1245 | /* Disabled PLL */ |
| 1246 | pllpd = mmio_read_32(PLLDIG_PLLCR(pll_addr)) & PLLDIG_PLLCR_PLLPD; |
| 1247 | if (pllpd != 0U) { |
| 1248 | *rate = pll->vco_freq; |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | clk_src = mmio_read_32(PLLDIG_PLLCLKMUX(pll_addr)); |
| 1253 | switch (clk_src) { |
| 1254 | case 0: |
| 1255 | clk_src = S32CC_CLK_FIRC; |
| 1256 | break; |
| 1257 | case 1: |
| 1258 | clk_src = S32CC_CLK_FXOSC; |
| 1259 | break; |
| 1260 | default: |
| 1261 | ERROR("Failed to identify PLL source id %" PRIu64 "\n", clk_src); |
| 1262 | return -EINVAL; |
| 1263 | }; |
| 1264 | |
| 1265 | source = s32cc_get_arch_clk(clk_src); |
| 1266 | if (source == NULL) { |
| 1267 | ERROR("Failed to get PLL source clock\n"); |
| 1268 | return -EINVAL; |
| 1269 | } |
| 1270 | |
| 1271 | ret = get_module_rate(&source->desc, drv, &prate, ldepth); |
| 1272 | if (ret != 0) { |
| 1273 | ERROR("Failed to get PLL's parent frequency\n"); |
| 1274 | return ret; |
| 1275 | } |
| 1276 | |
| 1277 | plldv = mmio_read_32(PLLDIG_PLLDV(pll_addr)); |
| 1278 | mfi = PLLDIG_PLLDV_MFI(plldv); |
| 1279 | rdiv = PLLDIG_PLLDV_RDIV(plldv); |
| 1280 | if (rdiv == 0U) { |
| 1281 | rdiv = 1; |
| 1282 | } |
| 1283 | |
| 1284 | /* Frac-N mode */ |
| 1285 | mfn = PLLDIG_PLLFD_MFN_SET(mmio_read_32(PLLDIG_PLLFD(pll_addr))); |
| 1286 | |
| 1287 | /* PLL VCO frequency in Fractional mode when PLLDV[RDIV] is not 0 */ |
| 1288 | t1 = prate / rdiv; |
| 1289 | t2 = (mfi * FP_PRECISION) + (mfn * FP_PRECISION / 18432U); |
| 1290 | |
| 1291 | *rate = t1 * t2 / FP_PRECISION; |
| 1292 | |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
Ghennadi Procopciuc | de950ef | 2024-06-12 12:00:15 +0300 | [diff] [blame] | 1296 | static int set_pll_div_freq(const struct s32cc_clk_obj *module, unsigned long rate, |
| 1297 | unsigned long *orate, unsigned int *depth) |
| 1298 | { |
| 1299 | struct s32cc_pll_out_div *pdiv = s32cc_obj2plldiv(module); |
| 1300 | const struct s32cc_pll *pll; |
| 1301 | unsigned long prate, dc; |
| 1302 | int ret; |
| 1303 | |
| 1304 | ret = update_stack_depth(depth); |
| 1305 | if (ret != 0) { |
| 1306 | return ret; |
| 1307 | } |
| 1308 | |
| 1309 | if (pdiv->parent == NULL) { |
| 1310 | ERROR("Failed to identify PLL divider's parent\n"); |
| 1311 | return -EINVAL; |
| 1312 | } |
| 1313 | |
| 1314 | pll = s32cc_obj2pll(pdiv->parent); |
| 1315 | if (pll == NULL) { |
| 1316 | ERROR("The parent of the PLL DIV is invalid\n"); |
| 1317 | return -EINVAL; |
| 1318 | } |
| 1319 | |
| 1320 | prate = pll->vco_freq; |
| 1321 | |
| 1322 | /** |
| 1323 | * The PLL is not initialized yet, so let's take a risk |
| 1324 | * and accept the proposed rate. |
| 1325 | */ |
| 1326 | if (prate == 0UL) { |
| 1327 | pdiv->freq = rate; |
| 1328 | *orate = rate; |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | /* Decline in case the rate cannot fit PLL's requirements. */ |
| 1333 | dc = prate / rate; |
| 1334 | if ((prate / dc) != rate) { |
| 1335 | return -EINVAL; |
| 1336 | } |
| 1337 | |
| 1338 | pdiv->freq = rate; |
| 1339 | *orate = pdiv->freq; |
| 1340 | |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
Ghennadi Procopciuc | 65739db | 2024-06-12 12:29:54 +0300 | [diff] [blame] | 1344 | static int set_fixed_div_freq(const struct s32cc_clk_obj *module, unsigned long rate, |
| 1345 | unsigned long *orate, unsigned int *depth) |
| 1346 | { |
| 1347 | const struct s32cc_fixed_div *fdiv = s32cc_obj2fixeddiv(module); |
| 1348 | int ret; |
| 1349 | |
| 1350 | ret = update_stack_depth(depth); |
| 1351 | if (ret != 0) { |
| 1352 | return ret; |
| 1353 | } |
| 1354 | |
| 1355 | if (fdiv->parent == NULL) { |
| 1356 | ERROR("The divider doesn't have a valid parent\b"); |
| 1357 | return -EINVAL; |
| 1358 | } |
| 1359 | |
| 1360 | ret = set_module_rate(fdiv->parent, rate * fdiv->rate_div, orate, depth); |
| 1361 | |
| 1362 | /* Update the output rate based on the parent's rate */ |
| 1363 | *orate /= fdiv->rate_div; |
| 1364 | |
| 1365 | return ret; |
| 1366 | } |
| 1367 | |
Ghennadi Procopciuc | 64e0c22 | 2024-06-12 13:05:05 +0300 | [diff] [blame] | 1368 | static int set_mux_freq(const struct s32cc_clk_obj *module, unsigned long rate, |
| 1369 | unsigned long *orate, unsigned int *depth) |
| 1370 | { |
| 1371 | const struct s32cc_clkmux *mux = s32cc_obj2clkmux(module); |
| 1372 | const struct s32cc_clk *clk = s32cc_get_arch_clk(mux->source_id); |
| 1373 | int ret; |
| 1374 | |
| 1375 | ret = update_stack_depth(depth); |
| 1376 | if (ret != 0) { |
| 1377 | return ret; |
| 1378 | } |
| 1379 | |
| 1380 | if (clk == NULL) { |
| 1381 | ERROR("Mux (id:%" PRIu8 ") without a valid source (%lu)\n", |
| 1382 | mux->index, mux->source_id); |
| 1383 | return -EINVAL; |
| 1384 | } |
| 1385 | |
| 1386 | return set_module_rate(&clk->desc, rate, orate, depth); |
| 1387 | } |
| 1388 | |
Ghennadi Procopciuc | 4cd04c5 | 2024-08-05 16:48:49 +0300 | [diff] [blame] | 1389 | static int set_dfs_div_freq(const struct s32cc_clk_obj *module, unsigned long rate, |
| 1390 | unsigned long *orate, unsigned int *depth) |
| 1391 | { |
| 1392 | struct s32cc_dfs_div *dfs_div = s32cc_obj2dfsdiv(module); |
| 1393 | const struct s32cc_dfs *dfs; |
| 1394 | int ret; |
| 1395 | |
| 1396 | ret = update_stack_depth(depth); |
| 1397 | if (ret != 0) { |
| 1398 | return ret; |
| 1399 | } |
| 1400 | |
| 1401 | if (dfs_div->parent == NULL) { |
| 1402 | ERROR("Failed to identify DFS divider's parent\n"); |
| 1403 | return -EINVAL; |
| 1404 | } |
| 1405 | |
| 1406 | /* Sanity check */ |
| 1407 | dfs = s32cc_obj2dfs(dfs_div->parent); |
| 1408 | if (dfs->parent == NULL) { |
| 1409 | ERROR("Failed to identify DFS's parent\n"); |
| 1410 | return -EINVAL; |
| 1411 | } |
| 1412 | |
| 1413 | if ((dfs_div->freq != 0U) && (dfs_div->freq != rate)) { |
| 1414 | ERROR("DFS DIV frequency was already set to %lu\n", |
| 1415 | dfs_div->freq); |
| 1416 | return -EINVAL; |
| 1417 | } |
| 1418 | |
| 1419 | dfs_div->freq = rate; |
| 1420 | *orate = rate; |
| 1421 | |
| 1422 | return ret; |
| 1423 | } |
| 1424 | |
Ghennadi Procopciuc | 8f23e76 | 2025-01-13 10:47:26 +0200 | [diff] [blame^] | 1425 | static unsigned long compute_dfs_div_freq(unsigned long pfreq, uint32_t mfi, uint32_t mfn) |
| 1426 | { |
| 1427 | unsigned long freq; |
| 1428 | |
| 1429 | /** |
| 1430 | * Formula for input and output clocks of each port divider. |
| 1431 | * See 'Digital Frequency Synthesizer' chapter from Reference Manual. |
| 1432 | * |
| 1433 | * freq = pfreq / (2 * (mfi + mfn / 36.0)); |
| 1434 | */ |
| 1435 | freq = (mfi * FP_PRECISION) + (mfn * FP_PRECISION / 36UL); |
| 1436 | freq *= 2UL; |
| 1437 | freq = pfreq * FP_PRECISION / freq; |
| 1438 | |
| 1439 | return freq; |
| 1440 | } |
| 1441 | |
| 1442 | static int get_dfs_div_freq(const struct s32cc_clk_obj *module, |
| 1443 | const struct s32cc_clk_drv *drv, |
| 1444 | unsigned long *rate, unsigned int depth) |
| 1445 | { |
| 1446 | const struct s32cc_dfs_div *dfs_div = s32cc_obj2dfsdiv(module); |
| 1447 | unsigned int ldepth = depth; |
| 1448 | const struct s32cc_dfs *dfs; |
| 1449 | uint32_t dvport, mfi, mfn; |
| 1450 | uintptr_t dfs_addr = 0UL; |
| 1451 | unsigned long pfreq; |
| 1452 | int ret; |
| 1453 | |
| 1454 | ret = update_stack_depth(&ldepth); |
| 1455 | if (ret != 0) { |
| 1456 | return ret; |
| 1457 | } |
| 1458 | |
| 1459 | dfs = get_div_dfs(dfs_div); |
| 1460 | if (dfs == NULL) { |
| 1461 | return -EINVAL; |
| 1462 | } |
| 1463 | |
| 1464 | ret = get_module_rate(dfs_div->parent, drv, &pfreq, ldepth); |
| 1465 | if (ret != 0) { |
| 1466 | return ret; |
| 1467 | } |
| 1468 | |
| 1469 | ret = get_base_addr(dfs->instance, drv, &dfs_addr); |
| 1470 | if (ret != 0) { |
| 1471 | ERROR("Failed to detect the DFS instance\n"); |
| 1472 | return ret; |
| 1473 | } |
| 1474 | |
| 1475 | dvport = mmio_read_32(DFS_DVPORTn(dfs_addr, dfs_div->index)); |
| 1476 | |
| 1477 | mfi = DFS_DVPORTn_MFI(dvport); |
| 1478 | mfn = DFS_DVPORTn_MFN(dvport); |
| 1479 | |
| 1480 | /* Disabled port */ |
| 1481 | if ((mfi == 0U) && (mfn == 0U)) { |
| 1482 | *rate = dfs_div->freq; |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | *rate = compute_dfs_div_freq(pfreq, mfi, mfn); |
| 1487 | return 0; |
| 1488 | } |
| 1489 | |
Ghennadi Procopciuc | d937351 | 2024-06-12 08:09:19 +0300 | [diff] [blame] | 1490 | static int set_module_rate(const struct s32cc_clk_obj *module, |
| 1491 | unsigned long rate, unsigned long *orate, |
| 1492 | unsigned int *depth) |
| 1493 | { |
| 1494 | int ret = 0; |
| 1495 | |
| 1496 | ret = update_stack_depth(depth); |
| 1497 | if (ret != 0) { |
| 1498 | return ret; |
| 1499 | } |
| 1500 | |
Ghennadi Procopciuc | 4cd04c5 | 2024-08-05 16:48:49 +0300 | [diff] [blame] | 1501 | ret = -EINVAL; |
| 1502 | |
Ghennadi Procopciuc | d937351 | 2024-06-12 08:09:19 +0300 | [diff] [blame] | 1503 | switch (module->type) { |
| 1504 | case s32cc_clk_t: |
| 1505 | ret = set_clk_freq(module, rate, orate, depth); |
| 1506 | break; |
| 1507 | case s32cc_osc_t: |
| 1508 | ret = set_osc_freq(module, rate, orate, depth); |
| 1509 | break; |
Ghennadi Procopciuc | 7ad4e23 | 2024-06-12 11:55:32 +0300 | [diff] [blame] | 1510 | case s32cc_pll_t: |
| 1511 | ret = set_pll_freq(module, rate, orate, depth); |
| 1512 | break; |
Ghennadi Procopciuc | de950ef | 2024-06-12 12:00:15 +0300 | [diff] [blame] | 1513 | case s32cc_pll_out_div_t: |
| 1514 | ret = set_pll_div_freq(module, rate, orate, depth); |
| 1515 | break; |
Ghennadi Procopciuc | 65739db | 2024-06-12 12:29:54 +0300 | [diff] [blame] | 1516 | case s32cc_fixed_div_t: |
| 1517 | ret = set_fixed_div_freq(module, rate, orate, depth); |
| 1518 | break; |
Ghennadi Procopciuc | a8be748 | 2024-06-12 09:53:18 +0300 | [diff] [blame] | 1519 | case s32cc_clkmux_t: |
Ghennadi Procopciuc | 64e0c22 | 2024-06-12 13:05:05 +0300 | [diff] [blame] | 1520 | ret = set_mux_freq(module, rate, orate, depth); |
| 1521 | break; |
Ghennadi Procopciuc | 3fa91a9 | 2024-06-12 10:53:06 +0300 | [diff] [blame] | 1522 | case s32cc_shared_clkmux_t: |
Ghennadi Procopciuc | 64e0c22 | 2024-06-12 13:05:05 +0300 | [diff] [blame] | 1523 | ret = set_mux_freq(module, rate, orate, depth); |
Ghennadi Procopciuc | a8be748 | 2024-06-12 09:53:18 +0300 | [diff] [blame] | 1524 | break; |
Ghennadi Procopciuc | 4cd04c5 | 2024-08-05 16:48:49 +0300 | [diff] [blame] | 1525 | case s32cc_dfs_t: |
| 1526 | ERROR("Setting the frequency of a DFS is not allowed!"); |
| 1527 | break; |
| 1528 | case s32cc_dfs_div_t: |
| 1529 | ret = set_dfs_div_freq(module, rate, orate, depth); |
| 1530 | break; |
Ghennadi Procopciuc | d937351 | 2024-06-12 08:09:19 +0300 | [diff] [blame] | 1531 | default: |
Ghennadi Procopciuc | d937351 | 2024-06-12 08:09:19 +0300 | [diff] [blame] | 1532 | break; |
| 1533 | } |
| 1534 | |
| 1535 | return ret; |
| 1536 | } |
| 1537 | |
Ghennadi Procopciuc | bd69113 | 2025-01-10 16:26:21 +0200 | [diff] [blame] | 1538 | static int get_module_rate(const struct s32cc_clk_obj *module, |
| 1539 | const struct s32cc_clk_drv *drv, |
| 1540 | unsigned long *rate, |
| 1541 | unsigned int depth) |
| 1542 | { |
| 1543 | unsigned int ldepth = depth; |
| 1544 | int ret = 0; |
| 1545 | |
| 1546 | ret = update_stack_depth(&ldepth); |
| 1547 | if (ret != 0) { |
| 1548 | return ret; |
| 1549 | } |
| 1550 | |
| 1551 | switch (module->type) { |
| 1552 | case s32cc_osc_t: |
| 1553 | ret = get_osc_freq(module, drv, rate, ldepth); |
| 1554 | break; |
Ghennadi Procopciuc | 46de0b9 | 2025-01-10 16:33:36 +0200 | [diff] [blame] | 1555 | case s32cc_clk_t: |
| 1556 | ret = get_clk_freq(module, drv, rate, ldepth); |
| 1557 | break; |
Ghennadi Procopciuc | fbebafa | 2025-01-13 09:33:42 +0200 | [diff] [blame] | 1558 | case s32cc_pll_t: |
| 1559 | ret = get_pll_freq(module, drv, rate, ldepth); |
| 1560 | break; |
Ghennadi Procopciuc | 2fb2550 | 2025-01-13 09:50:51 +0200 | [diff] [blame] | 1561 | case s32cc_dfs_t: |
| 1562 | ret = get_dfs_freq(module, drv, rate, ldepth); |
| 1563 | break; |
Ghennadi Procopciuc | 8f23e76 | 2025-01-13 10:47:26 +0200 | [diff] [blame^] | 1564 | case s32cc_dfs_div_t: |
| 1565 | ret = get_dfs_div_freq(module, drv, rate, ldepth); |
| 1566 | break; |
Ghennadi Procopciuc | bd69113 | 2025-01-10 16:26:21 +0200 | [diff] [blame] | 1567 | default: |
| 1568 | ret = -EINVAL; |
| 1569 | break; |
| 1570 | } |
| 1571 | |
| 1572 | return ret; |
| 1573 | } |
| 1574 | |
Ghennadi Procopciuc | 3a580e9 | 2024-06-11 18:39:58 +0300 | [diff] [blame] | 1575 | static int s32cc_clk_set_rate(unsigned long id, unsigned long rate, |
| 1576 | unsigned long *orate) |
| 1577 | { |
Ghennadi Procopciuc | d937351 | 2024-06-12 08:09:19 +0300 | [diff] [blame] | 1578 | unsigned int depth = MAX_STACK_DEPTH; |
| 1579 | const struct s32cc_clk *clk; |
| 1580 | int ret; |
| 1581 | |
| 1582 | clk = s32cc_get_arch_clk(id); |
| 1583 | if (clk == NULL) { |
| 1584 | return -EINVAL; |
| 1585 | } |
| 1586 | |
| 1587 | ret = set_module_rate(&clk->desc, rate, orate, &depth); |
| 1588 | if (ret != 0) { |
| 1589 | ERROR("Failed to set frequency (%lu MHz) for clock %lu\n", |
| 1590 | rate, id); |
| 1591 | } |
| 1592 | |
| 1593 | return ret; |
Ghennadi Procopciuc | 3a580e9 | 2024-06-11 18:39:58 +0300 | [diff] [blame] | 1594 | } |
| 1595 | |
Ghennadi Procopciuc | bd69113 | 2025-01-10 16:26:21 +0200 | [diff] [blame] | 1596 | static unsigned long s32cc_clk_get_rate(unsigned long id) |
| 1597 | { |
| 1598 | const struct s32cc_clk_drv *drv = get_drv(); |
| 1599 | unsigned int depth = MAX_STACK_DEPTH; |
| 1600 | const struct s32cc_clk *clk; |
| 1601 | unsigned long rate = 0UL; |
| 1602 | int ret; |
| 1603 | |
| 1604 | clk = s32cc_get_arch_clk(id); |
| 1605 | if (clk == NULL) { |
| 1606 | return 0; |
| 1607 | } |
| 1608 | |
| 1609 | ret = get_module_rate(&clk->desc, drv, &rate, depth); |
| 1610 | if (ret != 0) { |
| 1611 | ERROR("Failed to get frequency (%lu MHz) for clock %lu\n", |
| 1612 | rate, id); |
| 1613 | return 0; |
| 1614 | } |
| 1615 | |
| 1616 | return rate; |
| 1617 | } |
| 1618 | |
Ghennadi Procopciuc | 96e069c | 2024-09-11 09:29:50 +0300 | [diff] [blame] | 1619 | static struct s32cc_clk_obj *get_no_parent(const struct s32cc_clk_obj *module) |
| 1620 | { |
| 1621 | return NULL; |
| 1622 | } |
| 1623 | |
| 1624 | typedef struct s32cc_clk_obj *(*get_parent_clb_t)(const struct s32cc_clk_obj *clk_obj); |
| 1625 | |
| 1626 | static struct s32cc_clk_obj *get_module_parent(const struct s32cc_clk_obj *module) |
| 1627 | { |
Ghennadi Procopciuc | 8a4f840 | 2024-09-17 11:22:30 +0300 | [diff] [blame] | 1628 | static const get_parent_clb_t parents_clbs[12] = { |
Ghennadi Procopciuc | 96e069c | 2024-09-11 09:29:50 +0300 | [diff] [blame] | 1629 | [s32cc_clk_t] = get_clk_parent, |
| 1630 | [s32cc_osc_t] = get_no_parent, |
| 1631 | [s32cc_pll_t] = get_pll_parent, |
| 1632 | [s32cc_pll_out_div_t] = get_pll_div_parent, |
| 1633 | [s32cc_clkmux_t] = get_mux_parent, |
| 1634 | [s32cc_shared_clkmux_t] = get_mux_parent, |
| 1635 | [s32cc_dfs_t] = get_dfs_parent, |
| 1636 | [s32cc_dfs_div_t] = get_dfs_div_parent, |
Ghennadi Procopciuc | 8a4f840 | 2024-09-17 11:22:30 +0300 | [diff] [blame] | 1637 | [s32cc_part_t] = get_no_parent, |
| 1638 | [s32cc_part_block_t] = get_part_block_parent, |
| 1639 | [s32cc_part_block_link_t] = get_part_block_link_parent, |
Ghennadi Procopciuc | 96e069c | 2024-09-11 09:29:50 +0300 | [diff] [blame] | 1640 | }; |
| 1641 | uint32_t index; |
| 1642 | |
| 1643 | if (module == NULL) { |
| 1644 | return NULL; |
| 1645 | } |
| 1646 | |
| 1647 | index = (uint32_t)module->type; |
| 1648 | |
| 1649 | if (index >= ARRAY_SIZE(parents_clbs)) { |
| 1650 | ERROR("Undefined module type: %d\n", module->type); |
| 1651 | return NULL; |
| 1652 | } |
| 1653 | |
| 1654 | if (parents_clbs[index] == NULL) { |
| 1655 | ERROR("Undefined parent getter for type: %d\n", module->type); |
| 1656 | return NULL; |
| 1657 | } |
| 1658 | |
| 1659 | return parents_clbs[index](module); |
| 1660 | } |
| 1661 | |
Ghennadi Procopciuc | 3a580e9 | 2024-06-11 18:39:58 +0300 | [diff] [blame] | 1662 | static int s32cc_clk_get_parent(unsigned long id) |
| 1663 | { |
Ghennadi Procopciuc | 96e069c | 2024-09-11 09:29:50 +0300 | [diff] [blame] | 1664 | struct s32cc_clk *parent_clk; |
| 1665 | const struct s32cc_clk_obj *parent; |
| 1666 | const struct s32cc_clk *clk; |
| 1667 | unsigned long parent_id; |
| 1668 | int ret; |
| 1669 | |
| 1670 | clk = s32cc_get_arch_clk(id); |
| 1671 | if (clk == NULL) { |
| 1672 | return -EINVAL; |
| 1673 | } |
| 1674 | |
| 1675 | parent = get_module_parent(clk->module); |
| 1676 | if (parent == NULL) { |
| 1677 | return -EINVAL; |
| 1678 | } |
| 1679 | |
| 1680 | parent_clk = s32cc_obj2clk(parent); |
| 1681 | if (parent_clk == NULL) { |
| 1682 | return -EINVAL; |
| 1683 | } |
| 1684 | |
| 1685 | ret = s32cc_get_clk_id(parent_clk, &parent_id); |
| 1686 | if (ret != 0) { |
| 1687 | return ret; |
| 1688 | } |
| 1689 | |
| 1690 | if (parent_id > (unsigned long)INT_MAX) { |
| 1691 | return -E2BIG; |
| 1692 | } |
| 1693 | |
| 1694 | return (int)parent_id; |
Ghennadi Procopciuc | 3a580e9 | 2024-06-11 18:39:58 +0300 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | static int s32cc_clk_set_parent(unsigned long id, unsigned long parent_id) |
| 1698 | { |
Ghennadi Procopciuc | 12e7a2c | 2024-06-12 10:02:07 +0300 | [diff] [blame] | 1699 | const struct s32cc_clk *parent; |
| 1700 | const struct s32cc_clk *clk; |
| 1701 | bool valid_source = false; |
| 1702 | struct s32cc_clkmux *mux; |
| 1703 | uint8_t i; |
| 1704 | |
| 1705 | clk = s32cc_get_arch_clk(id); |
| 1706 | if (clk == NULL) { |
| 1707 | return -EINVAL; |
| 1708 | } |
| 1709 | |
| 1710 | parent = s32cc_get_arch_clk(parent_id); |
| 1711 | if (parent == NULL) { |
| 1712 | return -EINVAL; |
| 1713 | } |
| 1714 | |
| 1715 | if (!is_s32cc_clk_mux(clk)) { |
| 1716 | ERROR("Clock %lu is not a mux\n", id); |
| 1717 | return -EINVAL; |
| 1718 | } |
| 1719 | |
| 1720 | mux = s32cc_clk2mux(clk); |
| 1721 | if (mux == NULL) { |
| 1722 | ERROR("Failed to cast clock %lu to clock mux\n", id); |
| 1723 | return -EINVAL; |
| 1724 | } |
| 1725 | |
| 1726 | for (i = 0; i < mux->nclks; i++) { |
| 1727 | if (mux->clkids[i] == parent_id) { |
| 1728 | valid_source = true; |
| 1729 | break; |
| 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | if (!valid_source) { |
| 1734 | ERROR("Clock %lu is not a valid clock for mux %lu\n", |
| 1735 | parent_id, id); |
| 1736 | return -EINVAL; |
| 1737 | } |
| 1738 | |
| 1739 | mux->source_id = parent_id; |
| 1740 | |
| 1741 | return 0; |
Ghennadi Procopciuc | 3a580e9 | 2024-06-11 18:39:58 +0300 | [diff] [blame] | 1742 | } |
| 1743 | |
Ghennadi Procopciuc | 514c738 | 2024-11-26 16:39:41 +0200 | [diff] [blame] | 1744 | static int s32cc_clk_mmap_regs(const struct s32cc_clk_drv *drv) |
| 1745 | { |
| 1746 | const uintptr_t base_addrs[11] = { |
| 1747 | drv->fxosc_base, |
| 1748 | drv->armpll_base, |
| 1749 | drv->periphpll_base, |
| 1750 | drv->armdfs_base, |
| 1751 | drv->cgm0_base, |
| 1752 | drv->cgm1_base, |
| 1753 | drv->cgm5_base, |
| 1754 | drv->ddrpll_base, |
| 1755 | drv->mc_me, |
| 1756 | drv->mc_rgm, |
| 1757 | drv->rdc, |
| 1758 | }; |
| 1759 | size_t i; |
| 1760 | int ret; |
| 1761 | |
| 1762 | for (i = 0U; i < ARRAY_SIZE(base_addrs); i++) { |
| 1763 | ret = mmap_add_dynamic_region(base_addrs[i], base_addrs[i], |
| 1764 | PAGE_SIZE, |
| 1765 | MT_DEVICE | MT_RW | MT_SECURE); |
| 1766 | if (ret != 0) { |
| 1767 | ERROR("Failed to map clock module 0x%" PRIuPTR "\n", |
| 1768 | base_addrs[i]); |
| 1769 | return ret; |
| 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
Ghennadi Procopciuc | 61b5ef2 | 2024-11-27 12:33:26 +0200 | [diff] [blame] | 1776 | int s32cc_clk_register_drv(bool mmap_regs) |
Ghennadi Procopciuc | 3a580e9 | 2024-06-11 18:39:58 +0300 | [diff] [blame] | 1777 | { |
| 1778 | static const struct clk_ops s32cc_clk_ops = { |
| 1779 | .enable = s32cc_clk_enable, |
| 1780 | .disable = s32cc_clk_disable, |
| 1781 | .is_enabled = s32cc_clk_is_enabled, |
| 1782 | .get_rate = s32cc_clk_get_rate, |
| 1783 | .set_rate = s32cc_clk_set_rate, |
| 1784 | .get_parent = s32cc_clk_get_parent, |
| 1785 | .set_parent = s32cc_clk_set_parent, |
| 1786 | }; |
Ghennadi Procopciuc | 514c738 | 2024-11-26 16:39:41 +0200 | [diff] [blame] | 1787 | const struct s32cc_clk_drv *drv; |
Ghennadi Procopciuc | 3a580e9 | 2024-06-11 18:39:58 +0300 | [diff] [blame] | 1788 | |
| 1789 | clk_register(&s32cc_clk_ops); |
Ghennadi Procopciuc | 514c738 | 2024-11-26 16:39:41 +0200 | [diff] [blame] | 1790 | |
| 1791 | drv = get_drv(); |
| 1792 | if (drv == NULL) { |
| 1793 | return -EINVAL; |
| 1794 | } |
| 1795 | |
Ghennadi Procopciuc | 61b5ef2 | 2024-11-27 12:33:26 +0200 | [diff] [blame] | 1796 | if (mmap_regs) { |
| 1797 | return s32cc_clk_mmap_regs(drv); |
| 1798 | } |
| 1799 | |
| 1800 | return 0; |
Ghennadi Procopciuc | 3a580e9 | 2024-06-11 18:39:58 +0300 | [diff] [blame] | 1801 | } |
| 1802 | |