19be88e75SGabriel Fernandez /* 29be88e75SGabriel Fernandez * Copyright (C) 2022, STMicroelectronics - All Rights Reserved 39be88e75SGabriel Fernandez * 49be88e75SGabriel Fernandez * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause 59be88e75SGabriel Fernandez */ 69be88e75SGabriel Fernandez 79be88e75SGabriel Fernandez #include <assert.h> 89be88e75SGabriel Fernandez #include <errno.h> 99be88e75SGabriel Fernandez #include <limits.h> 109be88e75SGabriel Fernandez #include <stdint.h> 119be88e75SGabriel Fernandez #include <stdio.h> 129be88e75SGabriel Fernandez 139be88e75SGabriel Fernandez #include <arch.h> 149be88e75SGabriel Fernandez #include <arch_helpers.h> 159be88e75SGabriel Fernandez #include "clk-stm32-core.h" 169be88e75SGabriel Fernandez #include <common/debug.h> 179be88e75SGabriel Fernandez #include <common/fdt_wrappers.h> 189be88e75SGabriel Fernandez #include <drivers/clk.h> 199be88e75SGabriel Fernandez #include <drivers/delay_timer.h> 209be88e75SGabriel Fernandez #include <drivers/st/stm32mp13_rcc.h> 219be88e75SGabriel Fernandez #include <drivers/st/stm32mp1_clk.h> 229be88e75SGabriel Fernandez #include <drivers/st/stm32mp_clkfunc.h> 239be88e75SGabriel Fernandez #include <dt-bindings/clock/stm32mp13-clksrc.h> 249be88e75SGabriel Fernandez #include <lib/mmio.h> 259be88e75SGabriel Fernandez #include <lib/spinlock.h> 269be88e75SGabriel Fernandez #include <lib/utils_def.h> 279be88e75SGabriel Fernandez #include <libfdt.h> 289be88e75SGabriel Fernandez #include <plat/common/platform.h> 299be88e75SGabriel Fernandez 309be88e75SGabriel Fernandez #include <platform_def.h> 319be88e75SGabriel Fernandez 329be88e75SGabriel Fernandez struct stm32_osci_dt_cfg { 339be88e75SGabriel Fernandez unsigned long freq; 349be88e75SGabriel Fernandez bool bypass; 359be88e75SGabriel Fernandez bool digbyp; 369be88e75SGabriel Fernandez bool css; 379be88e75SGabriel Fernandez uint32_t drive; 389be88e75SGabriel Fernandez }; 399be88e75SGabriel Fernandez 409be88e75SGabriel Fernandez enum pll_mn { 419be88e75SGabriel Fernandez PLL_CFG_M, 429be88e75SGabriel Fernandez PLL_CFG_N, 439be88e75SGabriel Fernandez PLL_DIV_MN_NB 449be88e75SGabriel Fernandez }; 459be88e75SGabriel Fernandez 469be88e75SGabriel Fernandez enum pll_pqr { 479be88e75SGabriel Fernandez PLL_CFG_P, 489be88e75SGabriel Fernandez PLL_CFG_Q, 499be88e75SGabriel Fernandez PLL_CFG_R, 509be88e75SGabriel Fernandez PLL_DIV_PQR_NB 519be88e75SGabriel Fernandez }; 529be88e75SGabriel Fernandez 539be88e75SGabriel Fernandez enum pll_csg { 549be88e75SGabriel Fernandez PLL_CSG_MOD_PER, 559be88e75SGabriel Fernandez PLL_CSG_INC_STEP, 569be88e75SGabriel Fernandez PLL_CSG_SSCG_MODE, 579be88e75SGabriel Fernandez PLL_CSG_NB 589be88e75SGabriel Fernandez }; 599be88e75SGabriel Fernandez 609be88e75SGabriel Fernandez struct stm32_pll_vco { 619be88e75SGabriel Fernandez uint32_t status; 629be88e75SGabriel Fernandez uint32_t src; 639be88e75SGabriel Fernandez uint32_t div_mn[PLL_DIV_MN_NB]; 649be88e75SGabriel Fernandez uint32_t frac; 659be88e75SGabriel Fernandez bool csg_enabled; 669be88e75SGabriel Fernandez uint32_t csg[PLL_CSG_NB]; 679be88e75SGabriel Fernandez }; 689be88e75SGabriel Fernandez 699be88e75SGabriel Fernandez struct stm32_pll_output { 709be88e75SGabriel Fernandez uint32_t output[PLL_DIV_PQR_NB]; 719be88e75SGabriel Fernandez }; 729be88e75SGabriel Fernandez 739be88e75SGabriel Fernandez struct stm32_pll_dt_cfg { 749be88e75SGabriel Fernandez struct stm32_pll_vco vco; 759be88e75SGabriel Fernandez struct stm32_pll_output output; 769be88e75SGabriel Fernandez }; 779be88e75SGabriel Fernandez 789be88e75SGabriel Fernandez struct stm32_clk_platdata { 799be88e75SGabriel Fernandez uint32_t nosci; 809be88e75SGabriel Fernandez struct stm32_osci_dt_cfg *osci; 819be88e75SGabriel Fernandez uint32_t npll; 829be88e75SGabriel Fernandez struct stm32_pll_dt_cfg *pll; 839be88e75SGabriel Fernandez uint32_t nclksrc; 849be88e75SGabriel Fernandez uint32_t *clksrc; 859be88e75SGabriel Fernandez uint32_t nclkdiv; 869be88e75SGabriel Fernandez uint32_t *clkdiv; 879be88e75SGabriel Fernandez }; 889be88e75SGabriel Fernandez 899be88e75SGabriel Fernandez enum stm32_clock { 909be88e75SGabriel Fernandez /* ROOT CLOCKS */ 919be88e75SGabriel Fernandez _CK_OFF, 929be88e75SGabriel Fernandez _CK_HSI, 939be88e75SGabriel Fernandez _CK_HSE, 949be88e75SGabriel Fernandez _CK_CSI, 959be88e75SGabriel Fernandez _CK_LSI, 969be88e75SGabriel Fernandez _CK_LSE, 979be88e75SGabriel Fernandez _I2SCKIN, 989be88e75SGabriel Fernandez _CSI_DIV122, 999be88e75SGabriel Fernandez _HSE_DIV, 1009be88e75SGabriel Fernandez _HSE_DIV2, 1019be88e75SGabriel Fernandez _CK_PLL1, 1029be88e75SGabriel Fernandez _CK_PLL2, 1039be88e75SGabriel Fernandez _CK_PLL3, 1049be88e75SGabriel Fernandez _CK_PLL4, 1059be88e75SGabriel Fernandez _PLL1P, 1069be88e75SGabriel Fernandez _PLL1P_DIV, 1079be88e75SGabriel Fernandez _PLL2P, 1089be88e75SGabriel Fernandez _PLL2Q, 1099be88e75SGabriel Fernandez _PLL2R, 1109be88e75SGabriel Fernandez _PLL3P, 1119be88e75SGabriel Fernandez _PLL3Q, 1129be88e75SGabriel Fernandez _PLL3R, 1139be88e75SGabriel Fernandez _PLL4P, 1149be88e75SGabriel Fernandez _PLL4Q, 1159be88e75SGabriel Fernandez _PLL4R, 1169be88e75SGabriel Fernandez _PCLK1, 1179be88e75SGabriel Fernandez _PCLK2, 1189be88e75SGabriel Fernandez _PCLK3, 1199be88e75SGabriel Fernandez _PCLK4, 1209be88e75SGabriel Fernandez _PCLK5, 1219be88e75SGabriel Fernandez _PCLK6, 1229be88e75SGabriel Fernandez _CKMPU, 1239be88e75SGabriel Fernandez _CKAXI, 1249be88e75SGabriel Fernandez _CKMLAHB, 1259be88e75SGabriel Fernandez _CKPER, 1269be88e75SGabriel Fernandez _CKTIMG1, 1279be88e75SGabriel Fernandez _CKTIMG2, 1289be88e75SGabriel Fernandez _CKTIMG3, 1299be88e75SGabriel Fernandez _USB_PHY_48, 1309be88e75SGabriel Fernandez _MCO1_K, 1319be88e75SGabriel Fernandez _MCO2_K, 1329be88e75SGabriel Fernandez _TRACECK, 1339be88e75SGabriel Fernandez /* BUS and KERNEL CLOCKS */ 1349be88e75SGabriel Fernandez _DDRC1, 1359be88e75SGabriel Fernandez _DDRC1LP, 1369be88e75SGabriel Fernandez _DDRPHYC, 1379be88e75SGabriel Fernandez _DDRPHYCLP, 1389be88e75SGabriel Fernandez _DDRCAPB, 1399be88e75SGabriel Fernandez _DDRCAPBLP, 1409be88e75SGabriel Fernandez _AXIDCG, 1419be88e75SGabriel Fernandez _DDRPHYCAPB, 1429be88e75SGabriel Fernandez _DDRPHYCAPBLP, 1439be88e75SGabriel Fernandez _SYSCFG, 1449be88e75SGabriel Fernandez _DDRPERFM, 1459be88e75SGabriel Fernandez _IWDG2APB, 1469be88e75SGabriel Fernandez _USBPHY_K, 1479be88e75SGabriel Fernandez _USBO_K, 1489be88e75SGabriel Fernandez _RTCAPB, 1499be88e75SGabriel Fernandez _TZC, 1509be88e75SGabriel Fernandez _ETZPC, 1519be88e75SGabriel Fernandez _IWDG1APB, 1529be88e75SGabriel Fernandez _BSEC, 1539be88e75SGabriel Fernandez _STGENC, 1549be88e75SGabriel Fernandez _USART1_K, 1559be88e75SGabriel Fernandez _USART2_K, 1569be88e75SGabriel Fernandez _I2C3_K, 1579be88e75SGabriel Fernandez _I2C4_K, 1589be88e75SGabriel Fernandez _I2C5_K, 1599be88e75SGabriel Fernandez _TIM12, 1609be88e75SGabriel Fernandez _TIM15, 1619be88e75SGabriel Fernandez _RTCCK, 1629be88e75SGabriel Fernandez _GPIOA, 1639be88e75SGabriel Fernandez _GPIOB, 1649be88e75SGabriel Fernandez _GPIOC, 1659be88e75SGabriel Fernandez _GPIOD, 1669be88e75SGabriel Fernandez _GPIOE, 1679be88e75SGabriel Fernandez _GPIOF, 1689be88e75SGabriel Fernandez _GPIOG, 1699be88e75SGabriel Fernandez _GPIOH, 1709be88e75SGabriel Fernandez _GPIOI, 1719be88e75SGabriel Fernandez _PKA, 1729be88e75SGabriel Fernandez _SAES_K, 1739be88e75SGabriel Fernandez _CRYP1, 1749be88e75SGabriel Fernandez _HASH1, 1759be88e75SGabriel Fernandez _RNG1_K, 1769be88e75SGabriel Fernandez _BKPSRAM, 1779be88e75SGabriel Fernandez _SDMMC1_K, 1789be88e75SGabriel Fernandez _SDMMC2_K, 1799be88e75SGabriel Fernandez _DBGCK, 1809be88e75SGabriel Fernandez _USART3_K, 1819be88e75SGabriel Fernandez _UART4_K, 1829be88e75SGabriel Fernandez _UART5_K, 1839be88e75SGabriel Fernandez _UART7_K, 1849be88e75SGabriel Fernandez _UART8_K, 1859be88e75SGabriel Fernandez _USART6_K, 1869be88e75SGabriel Fernandez _MCE, 1879be88e75SGabriel Fernandez _FMC_K, 1889be88e75SGabriel Fernandez _QSPI_K, 1899be88e75SGabriel Fernandez #if defined(IMAGE_BL32) 1909be88e75SGabriel Fernandez _LTDC, 1919be88e75SGabriel Fernandez _DMA1, 1929be88e75SGabriel Fernandez _DMA2, 1939be88e75SGabriel Fernandez _MDMA, 1949be88e75SGabriel Fernandez _ETH1MAC, 1959be88e75SGabriel Fernandez _USBH, 1969be88e75SGabriel Fernandez _TIM2, 1979be88e75SGabriel Fernandez _TIM3, 1989be88e75SGabriel Fernandez _TIM4, 1999be88e75SGabriel Fernandez _TIM5, 2009be88e75SGabriel Fernandez _TIM6, 2019be88e75SGabriel Fernandez _TIM7, 2029be88e75SGabriel Fernandez _LPTIM1_K, 2039be88e75SGabriel Fernandez _SPI2_K, 2049be88e75SGabriel Fernandez _SPI3_K, 2059be88e75SGabriel Fernandez _SPDIF_K, 2069be88e75SGabriel Fernandez _TIM1, 2079be88e75SGabriel Fernandez _TIM8, 2089be88e75SGabriel Fernandez _SPI1_K, 2099be88e75SGabriel Fernandez _SAI1_K, 2109be88e75SGabriel Fernandez _SAI2_K, 2119be88e75SGabriel Fernandez _DFSDM, 2129be88e75SGabriel Fernandez _FDCAN_K, 2139be88e75SGabriel Fernandez _TIM13, 2149be88e75SGabriel Fernandez _TIM14, 2159be88e75SGabriel Fernandez _TIM16, 2169be88e75SGabriel Fernandez _TIM17, 2179be88e75SGabriel Fernandez _SPI4_K, 2189be88e75SGabriel Fernandez _SPI5_K, 2199be88e75SGabriel Fernandez _I2C1_K, 2209be88e75SGabriel Fernandez _I2C2_K, 2219be88e75SGabriel Fernandez _ADFSDM, 2229be88e75SGabriel Fernandez _LPTIM2_K, 2239be88e75SGabriel Fernandez _LPTIM3_K, 2249be88e75SGabriel Fernandez _LPTIM4_K, 2259be88e75SGabriel Fernandez _LPTIM5_K, 2269be88e75SGabriel Fernandez _VREF, 2279be88e75SGabriel Fernandez _DTS, 2289be88e75SGabriel Fernandez _PMBCTRL, 2299be88e75SGabriel Fernandez _HDP, 2309be88e75SGabriel Fernandez _STGENRO, 2319be88e75SGabriel Fernandez _DCMIPP_K, 2329be88e75SGabriel Fernandez _DMAMUX1, 2339be88e75SGabriel Fernandez _DMAMUX2, 2349be88e75SGabriel Fernandez _DMA3, 2359be88e75SGabriel Fernandez _ADC1_K, 2369be88e75SGabriel Fernandez _ADC2_K, 2379be88e75SGabriel Fernandez _TSC, 2389be88e75SGabriel Fernandez _AXIMC, 2399be88e75SGabriel Fernandez _ETH1CK, 2409be88e75SGabriel Fernandez _ETH1TX, 2419be88e75SGabriel Fernandez _ETH1RX, 2429be88e75SGabriel Fernandez _CRC1, 2439be88e75SGabriel Fernandez _ETH2CK, 2449be88e75SGabriel Fernandez _ETH2TX, 2459be88e75SGabriel Fernandez _ETH2RX, 2469be88e75SGabriel Fernandez _ETH2MAC, 2479be88e75SGabriel Fernandez #endif 2489be88e75SGabriel Fernandez CK_LAST 2499be88e75SGabriel Fernandez }; 2509be88e75SGabriel Fernandez 2519be88e75SGabriel Fernandez /* PARENT CONFIG */ 2529be88e75SGabriel Fernandez static const uint16_t RTC_src[] = { 2539be88e75SGabriel Fernandez _CK_OFF, _CK_LSE, _CK_LSI, _CK_HSE 2549be88e75SGabriel Fernandez }; 2559be88e75SGabriel Fernandez 2569be88e75SGabriel Fernandez static const uint16_t MCO1_src[] = { 2579be88e75SGabriel Fernandez _CK_HSI, _CK_HSE, _CK_CSI, _CK_LSI, _CK_LSE 2589be88e75SGabriel Fernandez }; 2599be88e75SGabriel Fernandez 2609be88e75SGabriel Fernandez static const uint16_t MCO2_src[] = { 2619be88e75SGabriel Fernandez _CKMPU, _CKAXI, _CKMLAHB, _PLL4P, _CK_HSE, _CK_HSI 2629be88e75SGabriel Fernandez }; 2639be88e75SGabriel Fernandez 2649be88e75SGabriel Fernandez static const uint16_t PLL12_src[] = { 2659be88e75SGabriel Fernandez _CK_HSI, _CK_HSE 2669be88e75SGabriel Fernandez }; 2679be88e75SGabriel Fernandez 2689be88e75SGabriel Fernandez static const uint16_t PLL3_src[] = { 2699be88e75SGabriel Fernandez _CK_HSI, _CK_HSE, _CK_CSI 2709be88e75SGabriel Fernandez }; 2719be88e75SGabriel Fernandez 2729be88e75SGabriel Fernandez static const uint16_t PLL4_src[] = { 2739be88e75SGabriel Fernandez _CK_HSI, _CK_HSE, _CK_CSI, _I2SCKIN 2749be88e75SGabriel Fernandez }; 2759be88e75SGabriel Fernandez 2769be88e75SGabriel Fernandez static const uint16_t MPU_src[] = { 2779be88e75SGabriel Fernandez _CK_HSI, _CK_HSE, _PLL1P, _PLL1P_DIV 2789be88e75SGabriel Fernandez }; 2799be88e75SGabriel Fernandez 2809be88e75SGabriel Fernandez static const uint16_t AXI_src[] = { 2819be88e75SGabriel Fernandez _CK_HSI, _CK_HSE, _PLL2P 2829be88e75SGabriel Fernandez }; 2839be88e75SGabriel Fernandez 2849be88e75SGabriel Fernandez static const uint16_t MLAHBS_src[] = { 2859be88e75SGabriel Fernandez _CK_HSI, _CK_HSE, _CK_CSI, _PLL3P 2869be88e75SGabriel Fernandez }; 2879be88e75SGabriel Fernandez 2889be88e75SGabriel Fernandez static const uint16_t CKPER_src[] = { 2899be88e75SGabriel Fernandez _CK_HSI, _CK_CSI, _CK_HSE, _CK_OFF 2909be88e75SGabriel Fernandez }; 2919be88e75SGabriel Fernandez 2929be88e75SGabriel Fernandez static const uint16_t I2C12_src[] = { 2939be88e75SGabriel Fernandez _PCLK1, _PLL4R, _CK_HSI, _CK_CSI 2949be88e75SGabriel Fernandez }; 2959be88e75SGabriel Fernandez 2969be88e75SGabriel Fernandez static const uint16_t I2C3_src[] = { 2979be88e75SGabriel Fernandez _PCLK6, _PLL4R, _CK_HSI, _CK_CSI 2989be88e75SGabriel Fernandez }; 2999be88e75SGabriel Fernandez 3009be88e75SGabriel Fernandez static const uint16_t I2C4_src[] = { 3019be88e75SGabriel Fernandez _PCLK6, _PLL4R, _CK_HSI, _CK_CSI 3029be88e75SGabriel Fernandez }; 3039be88e75SGabriel Fernandez 3049be88e75SGabriel Fernandez static const uint16_t I2C5_src[] = { 3059be88e75SGabriel Fernandez _PCLK6, _PLL4R, _CK_HSI, _CK_CSI 3069be88e75SGabriel Fernandez }; 3079be88e75SGabriel Fernandez 3089be88e75SGabriel Fernandez static const uint16_t SPI1_src[] = { 3099be88e75SGabriel Fernandez _PLL4P, _PLL3Q, _I2SCKIN, _CKPER, _PLL3R 3109be88e75SGabriel Fernandez }; 3119be88e75SGabriel Fernandez 3129be88e75SGabriel Fernandez static const uint16_t SPI23_src[] = { 3139be88e75SGabriel Fernandez _PLL4P, _PLL3Q, _I2SCKIN, _CKPER, _PLL3R 3149be88e75SGabriel Fernandez }; 3159be88e75SGabriel Fernandez 3169be88e75SGabriel Fernandez static const uint16_t SPI4_src[] = { 3179be88e75SGabriel Fernandez _PCLK6, _PLL4Q, _CK_HSI, _CK_CSI, _CK_HSE, _I2SCKIN 3189be88e75SGabriel Fernandez }; 3199be88e75SGabriel Fernandez 3209be88e75SGabriel Fernandez static const uint16_t SPI5_src[] = { 3219be88e75SGabriel Fernandez _PCLK6, _PLL4Q, _CK_HSI, _CK_CSI, _CK_HSE 3229be88e75SGabriel Fernandez }; 3239be88e75SGabriel Fernandez 3249be88e75SGabriel Fernandez static const uint16_t UART1_src[] = { 3259be88e75SGabriel Fernandez _PCLK6, _PLL3Q, _CK_HSI, _CK_CSI, _PLL4Q, _CK_HSE 3269be88e75SGabriel Fernandez }; 3279be88e75SGabriel Fernandez 3289be88e75SGabriel Fernandez static const uint16_t UART2_src[] = { 3299be88e75SGabriel Fernandez _PCLK6, _PLL3Q, _CK_HSI, _CK_CSI, _PLL4Q, _CK_HSE 3309be88e75SGabriel Fernandez }; 3319be88e75SGabriel Fernandez 3329be88e75SGabriel Fernandez static const uint16_t UART35_src[] = { 3339be88e75SGabriel Fernandez _PCLK1, _PLL4Q, _CK_HSI, _CK_CSI, _CK_HSE 3349be88e75SGabriel Fernandez }; 3359be88e75SGabriel Fernandez 3369be88e75SGabriel Fernandez static const uint16_t UART4_src[] = { 3379be88e75SGabriel Fernandez _PCLK1, _PLL4Q, _CK_HSI, _CK_CSI, _CK_HSE 3389be88e75SGabriel Fernandez }; 3399be88e75SGabriel Fernandez 3409be88e75SGabriel Fernandez static const uint16_t UART6_src[] = { 3419be88e75SGabriel Fernandez _PCLK2, _PLL4Q, _CK_HSI, _CK_CSI, _CK_HSE 3429be88e75SGabriel Fernandez }; 3439be88e75SGabriel Fernandez 3449be88e75SGabriel Fernandez static const uint16_t UART78_src[] = { 3459be88e75SGabriel Fernandez _PCLK1, _PLL4Q, _CK_HSI, _CK_CSI, _CK_HSE 3469be88e75SGabriel Fernandez }; 3479be88e75SGabriel Fernandez 3489be88e75SGabriel Fernandez static const uint16_t LPTIM1_src[] = { 3499be88e75SGabriel Fernandez _PCLK1, _PLL4P, _PLL3Q, _CK_LSE, _CK_LSI, _CKPER 3509be88e75SGabriel Fernandez }; 3519be88e75SGabriel Fernandez 3529be88e75SGabriel Fernandez static const uint16_t LPTIM2_src[] = { 3539be88e75SGabriel Fernandez _PCLK3, _PLL4Q, _CKPER, _CK_LSE, _CK_LSI 3549be88e75SGabriel Fernandez }; 3559be88e75SGabriel Fernandez 3569be88e75SGabriel Fernandez static const uint16_t LPTIM3_src[] = { 3579be88e75SGabriel Fernandez _PCLK3, _PLL4Q, _CKPER, _CK_LSE, _CK_LSI 3589be88e75SGabriel Fernandez }; 3599be88e75SGabriel Fernandez 3609be88e75SGabriel Fernandez static const uint16_t LPTIM45_src[] = { 3619be88e75SGabriel Fernandez _PCLK3, _PLL4P, _PLL3Q, _CK_LSE, _CK_LSI, _CKPER 3629be88e75SGabriel Fernandez }; 3639be88e75SGabriel Fernandez 3649be88e75SGabriel Fernandez static const uint16_t SAI1_src[] = { 3659be88e75SGabriel Fernandez _PLL4Q, _PLL3Q, _I2SCKIN, _CKPER, _PLL3R 3669be88e75SGabriel Fernandez }; 3679be88e75SGabriel Fernandez 3689be88e75SGabriel Fernandez static const uint16_t SAI2_src[] = { 3699be88e75SGabriel Fernandez _PLL4Q, _PLL3Q, _I2SCKIN, _CKPER, _NO_ID, _PLL3R 3709be88e75SGabriel Fernandez }; 3719be88e75SGabriel Fernandez 3729be88e75SGabriel Fernandez static const uint16_t FDCAN_src[] = { 3739be88e75SGabriel Fernandez _CK_HSE, _PLL3Q, _PLL4Q, _PLL4R 3749be88e75SGabriel Fernandez }; 3759be88e75SGabriel Fernandez 3769be88e75SGabriel Fernandez static const uint16_t SPDIF_src[] = { 3779be88e75SGabriel Fernandez _PLL4P, _PLL3Q, _CK_HSI 3789be88e75SGabriel Fernandez }; 3799be88e75SGabriel Fernandez 3809be88e75SGabriel Fernandez static const uint16_t ADC1_src[] = { 3819be88e75SGabriel Fernandez _PLL4R, _CKPER, _PLL3Q 3829be88e75SGabriel Fernandez }; 3839be88e75SGabriel Fernandez 3849be88e75SGabriel Fernandez static const uint16_t ADC2_src[] = { 3859be88e75SGabriel Fernandez _PLL4R, _CKPER, _PLL3Q 3869be88e75SGabriel Fernandez }; 3879be88e75SGabriel Fernandez 3889be88e75SGabriel Fernandez static const uint16_t SDMMC1_src[] = { 3899be88e75SGabriel Fernandez _CKAXI, _PLL3R, _PLL4P, _CK_HSI 3909be88e75SGabriel Fernandez }; 3919be88e75SGabriel Fernandez 3929be88e75SGabriel Fernandez static const uint16_t SDMMC2_src[] = { 3939be88e75SGabriel Fernandez _CKAXI, _PLL3R, _PLL4P, _CK_HSI 3949be88e75SGabriel Fernandez }; 3959be88e75SGabriel Fernandez 3969be88e75SGabriel Fernandez static const uint16_t ETH1_src[] = { 3979be88e75SGabriel Fernandez _PLL4P, _PLL3Q 3989be88e75SGabriel Fernandez }; 3999be88e75SGabriel Fernandez 4009be88e75SGabriel Fernandez static const uint16_t ETH2_src[] = { 4019be88e75SGabriel Fernandez _PLL4P, _PLL3Q 4029be88e75SGabriel Fernandez }; 4039be88e75SGabriel Fernandez 4049be88e75SGabriel Fernandez static const uint16_t USBPHY_src[] = { 4059be88e75SGabriel Fernandez _CK_HSE, _PLL4R, _HSE_DIV2 4069be88e75SGabriel Fernandez }; 4079be88e75SGabriel Fernandez 4089be88e75SGabriel Fernandez static const uint16_t USBO_src[] = { 4099be88e75SGabriel Fernandez _PLL4R, _USB_PHY_48 4109be88e75SGabriel Fernandez }; 4119be88e75SGabriel Fernandez 4129be88e75SGabriel Fernandez static const uint16_t QSPI_src[] = { 4139be88e75SGabriel Fernandez _CKAXI, _PLL3R, _PLL4P, _CKPER 4149be88e75SGabriel Fernandez }; 4159be88e75SGabriel Fernandez 4169be88e75SGabriel Fernandez static const uint16_t FMC_src[] = { 4179be88e75SGabriel Fernandez _CKAXI, _PLL3R, _PLL4P, _CKPER 4189be88e75SGabriel Fernandez }; 4199be88e75SGabriel Fernandez 4209be88e75SGabriel Fernandez /* Position 2 of RNG1 mux is reserved */ 4219be88e75SGabriel Fernandez static const uint16_t RNG1_src[] = { 4229be88e75SGabriel Fernandez _CK_CSI, _PLL4R, _CK_OFF, _CK_LSI 4239be88e75SGabriel Fernandez }; 4249be88e75SGabriel Fernandez 4259be88e75SGabriel Fernandez static const uint16_t STGEN_src[] = { 4269be88e75SGabriel Fernandez _CK_HSI, _CK_HSE 4279be88e75SGabriel Fernandez }; 4289be88e75SGabriel Fernandez 4299be88e75SGabriel Fernandez static const uint16_t DCMIPP_src[] = { 4309be88e75SGabriel Fernandez _CKAXI, _PLL2Q, _PLL4P, _CKPER 4319be88e75SGabriel Fernandez }; 4329be88e75SGabriel Fernandez 4339be88e75SGabriel Fernandez static const uint16_t SAES_src[] = { 4349be88e75SGabriel Fernandez _CKAXI, _CKPER, _PLL4R, _CK_LSI 4359be88e75SGabriel Fernandez }; 4369be88e75SGabriel Fernandez 4379be88e75SGabriel Fernandez #define MUX_CFG(id, src, _offset, _shift, _witdh)[id] = {\ 4389be88e75SGabriel Fernandez .id_parents = src,\ 4399be88e75SGabriel Fernandez .num_parents = ARRAY_SIZE(src),\ 4409be88e75SGabriel Fernandez .mux = &(struct mux_cfg) {\ 4419be88e75SGabriel Fernandez .offset = (_offset),\ 4429be88e75SGabriel Fernandez .shift = (_shift),\ 4439be88e75SGabriel Fernandez .width = (_witdh),\ 4449be88e75SGabriel Fernandez .bitrdy = MUX_NO_BIT_RDY,\ 4459be88e75SGabriel Fernandez },\ 4469be88e75SGabriel Fernandez } 4479be88e75SGabriel Fernandez 4489be88e75SGabriel Fernandez #define MUX_RDY_CFG(id, src, _offset, _shift, _witdh)[id] = {\ 4499be88e75SGabriel Fernandez .id_parents = src,\ 4509be88e75SGabriel Fernandez .num_parents = ARRAY_SIZE(src),\ 4519be88e75SGabriel Fernandez .mux = &(struct mux_cfg) {\ 4529be88e75SGabriel Fernandez .offset = (_offset),\ 4539be88e75SGabriel Fernandez .shift = (_shift),\ 4549be88e75SGabriel Fernandez .width = (_witdh),\ 4559be88e75SGabriel Fernandez .bitrdy = 31,\ 4569be88e75SGabriel Fernandez },\ 4579be88e75SGabriel Fernandez } 4589be88e75SGabriel Fernandez 459*ee21709eSYann Gautier static const struct parent_cfg parent_mp13[MUX_MAX] = { 4609be88e75SGabriel Fernandez MUX_CFG(MUX_ADC1, ADC1_src, RCC_ADC12CKSELR, 0, 2), 4619be88e75SGabriel Fernandez MUX_CFG(MUX_ADC2, ADC2_src, RCC_ADC12CKSELR, 2, 2), 4629be88e75SGabriel Fernandez MUX_RDY_CFG(MUX_AXI, AXI_src, RCC_ASSCKSELR, 0, 3), 4639be88e75SGabriel Fernandez MUX_CFG(MUX_CKPER, CKPER_src, RCC_CPERCKSELR, 0, 2), 4649be88e75SGabriel Fernandez MUX_CFG(MUX_DCMIPP, DCMIPP_src, RCC_DCMIPPCKSELR, 0, 2), 4659be88e75SGabriel Fernandez MUX_CFG(MUX_ETH1, ETH1_src, RCC_ETH12CKSELR, 0, 2), 4669be88e75SGabriel Fernandez MUX_CFG(MUX_ETH2, ETH2_src, RCC_ETH12CKSELR, 8, 2), 4679be88e75SGabriel Fernandez MUX_CFG(MUX_FDCAN, FDCAN_src, RCC_FDCANCKSELR, 0, 2), 4689be88e75SGabriel Fernandez MUX_CFG(MUX_FMC, FMC_src, RCC_FMCCKSELR, 0, 2), 4699be88e75SGabriel Fernandez MUX_CFG(MUX_I2C12, I2C12_src, RCC_I2C12CKSELR, 0, 3), 4709be88e75SGabriel Fernandez MUX_CFG(MUX_I2C3, I2C3_src, RCC_I2C345CKSELR, 0, 3), 4719be88e75SGabriel Fernandez MUX_CFG(MUX_I2C4, I2C4_src, RCC_I2C345CKSELR, 3, 3), 4729be88e75SGabriel Fernandez MUX_CFG(MUX_I2C5, I2C5_src, RCC_I2C345CKSELR, 6, 3), 4739be88e75SGabriel Fernandez MUX_CFG(MUX_LPTIM1, LPTIM1_src, RCC_LPTIM1CKSELR, 0, 3), 4749be88e75SGabriel Fernandez MUX_CFG(MUX_LPTIM2, LPTIM2_src, RCC_LPTIM23CKSELR, 0, 3), 4759be88e75SGabriel Fernandez MUX_CFG(MUX_LPTIM3, LPTIM3_src, RCC_LPTIM23CKSELR, 3, 3), 4769be88e75SGabriel Fernandez MUX_CFG(MUX_LPTIM45, LPTIM45_src, RCC_LPTIM45CKSELR, 0, 3), 4779be88e75SGabriel Fernandez MUX_CFG(MUX_MCO1, MCO1_src, RCC_MCO1CFGR, 0, 3), 4789be88e75SGabriel Fernandez MUX_CFG(MUX_MCO2, MCO2_src, RCC_MCO2CFGR, 0, 3), 4799be88e75SGabriel Fernandez MUX_RDY_CFG(MUX_MLAHB, MLAHBS_src, RCC_MSSCKSELR, 0, 2), 4809be88e75SGabriel Fernandez MUX_RDY_CFG(MUX_MPU, MPU_src, RCC_MPCKSELR, 0, 2), 4819be88e75SGabriel Fernandez MUX_RDY_CFG(MUX_PLL12, PLL12_src, RCC_RCK12SELR, 0, 2), 4829be88e75SGabriel Fernandez MUX_RDY_CFG(MUX_PLL3, PLL3_src, RCC_RCK3SELR, 0, 2), 4839be88e75SGabriel Fernandez MUX_RDY_CFG(MUX_PLL4, PLL4_src, RCC_RCK4SELR, 0, 2), 4849be88e75SGabriel Fernandez MUX_CFG(MUX_QSPI, QSPI_src, RCC_QSPICKSELR, 0, 2), 4859be88e75SGabriel Fernandez MUX_CFG(MUX_RNG1, RNG1_src, RCC_RNG1CKSELR, 0, 2), 4869be88e75SGabriel Fernandez MUX_CFG(MUX_RTC, RTC_src, RCC_BDCR, 16, 2), 4879be88e75SGabriel Fernandez MUX_CFG(MUX_SAES, SAES_src, RCC_SAESCKSELR, 0, 2), 4889be88e75SGabriel Fernandez MUX_CFG(MUX_SAI1, SAI1_src, RCC_SAI1CKSELR, 0, 3), 4899be88e75SGabriel Fernandez MUX_CFG(MUX_SAI2, SAI2_src, RCC_SAI2CKSELR, 0, 3), 4909be88e75SGabriel Fernandez MUX_CFG(MUX_SDMMC1, SDMMC1_src, RCC_SDMMC12CKSELR, 0, 3), 4919be88e75SGabriel Fernandez MUX_CFG(MUX_SDMMC2, SDMMC2_src, RCC_SDMMC12CKSELR, 3, 3), 4929be88e75SGabriel Fernandez MUX_CFG(MUX_SPDIF, SPDIF_src, RCC_SPDIFCKSELR, 0, 2), 4939be88e75SGabriel Fernandez MUX_CFG(MUX_SPI1, SPI1_src, RCC_SPI2S1CKSELR, 0, 3), 4949be88e75SGabriel Fernandez MUX_CFG(MUX_SPI23, SPI23_src, RCC_SPI2S23CKSELR, 0, 3), 4959be88e75SGabriel Fernandez MUX_CFG(MUX_SPI4, SPI4_src, RCC_SPI45CKSELR, 0, 3), 4969be88e75SGabriel Fernandez MUX_CFG(MUX_SPI5, SPI5_src, RCC_SPI45CKSELR, 3, 3), 4979be88e75SGabriel Fernandez MUX_CFG(MUX_STGEN, STGEN_src, RCC_STGENCKSELR, 0, 2), 4989be88e75SGabriel Fernandez MUX_CFG(MUX_UART1, UART1_src, RCC_UART12CKSELR, 0, 3), 4999be88e75SGabriel Fernandez MUX_CFG(MUX_UART2, UART2_src, RCC_UART12CKSELR, 3, 3), 5009be88e75SGabriel Fernandez MUX_CFG(MUX_UART35, UART35_src, RCC_UART35CKSELR, 0, 3), 5019be88e75SGabriel Fernandez MUX_CFG(MUX_UART4, UART4_src, RCC_UART4CKSELR, 0, 3), 5029be88e75SGabriel Fernandez MUX_CFG(MUX_UART6, UART6_src, RCC_UART6CKSELR, 0, 3), 5039be88e75SGabriel Fernandez MUX_CFG(MUX_UART78, UART78_src, RCC_UART78CKSELR, 0, 3), 5049be88e75SGabriel Fernandez MUX_CFG(MUX_USBO, USBO_src, RCC_USBCKSELR, 4, 1), 5059be88e75SGabriel Fernandez MUX_CFG(MUX_USBPHY, USBPHY_src, RCC_USBCKSELR, 0, 2), 5069be88e75SGabriel Fernandez }; 5079be88e75SGabriel Fernandez 5089be88e75SGabriel Fernandez /* 5099be88e75SGabriel Fernandez * GATE CONFIG 5109be88e75SGabriel Fernandez */ 5119be88e75SGabriel Fernandez 5129be88e75SGabriel Fernandez enum enum_gate_cfg { 5139be88e75SGabriel Fernandez GATE_ZERO, /* reserved for no gate */ 5149be88e75SGabriel Fernandez GATE_LSE, 5159be88e75SGabriel Fernandez GATE_RTCCK, 5169be88e75SGabriel Fernandez GATE_LSI, 5179be88e75SGabriel Fernandez GATE_HSI, 5189be88e75SGabriel Fernandez GATE_CSI, 5199be88e75SGabriel Fernandez GATE_HSE, 5209be88e75SGabriel Fernandez GATE_LSI_RDY, 5219be88e75SGabriel Fernandez GATE_CSI_RDY, 5229be88e75SGabriel Fernandez GATE_LSE_RDY, 5239be88e75SGabriel Fernandez GATE_HSE_RDY, 5249be88e75SGabriel Fernandez GATE_HSI_RDY, 5259be88e75SGabriel Fernandez GATE_MCO1, 5269be88e75SGabriel Fernandez GATE_MCO2, 5279be88e75SGabriel Fernandez GATE_DBGCK, 5289be88e75SGabriel Fernandez GATE_TRACECK, 5299be88e75SGabriel Fernandez GATE_PLL1, 5309be88e75SGabriel Fernandez GATE_PLL1_DIVP, 5319be88e75SGabriel Fernandez GATE_PLL1_DIVQ, 5329be88e75SGabriel Fernandez GATE_PLL1_DIVR, 5339be88e75SGabriel Fernandez GATE_PLL2, 5349be88e75SGabriel Fernandez GATE_PLL2_DIVP, 5359be88e75SGabriel Fernandez GATE_PLL2_DIVQ, 5369be88e75SGabriel Fernandez GATE_PLL2_DIVR, 5379be88e75SGabriel Fernandez GATE_PLL3, 5389be88e75SGabriel Fernandez GATE_PLL3_DIVP, 5399be88e75SGabriel Fernandez GATE_PLL3_DIVQ, 5409be88e75SGabriel Fernandez GATE_PLL3_DIVR, 5419be88e75SGabriel Fernandez GATE_PLL4, 5429be88e75SGabriel Fernandez GATE_PLL4_DIVP, 5439be88e75SGabriel Fernandez GATE_PLL4_DIVQ, 5449be88e75SGabriel Fernandez GATE_PLL4_DIVR, 5459be88e75SGabriel Fernandez GATE_DDRC1, 5469be88e75SGabriel Fernandez GATE_DDRC1LP, 5479be88e75SGabriel Fernandez GATE_DDRPHYC, 5489be88e75SGabriel Fernandez GATE_DDRPHYCLP, 5499be88e75SGabriel Fernandez GATE_DDRCAPB, 5509be88e75SGabriel Fernandez GATE_DDRCAPBLP, 5519be88e75SGabriel Fernandez GATE_AXIDCG, 5529be88e75SGabriel Fernandez GATE_DDRPHYCAPB, 5539be88e75SGabriel Fernandez GATE_DDRPHYCAPBLP, 5549be88e75SGabriel Fernandez GATE_TIM2, 5559be88e75SGabriel Fernandez GATE_TIM3, 5569be88e75SGabriel Fernandez GATE_TIM4, 5579be88e75SGabriel Fernandez GATE_TIM5, 5589be88e75SGabriel Fernandez GATE_TIM6, 5599be88e75SGabriel Fernandez GATE_TIM7, 5609be88e75SGabriel Fernandez GATE_LPTIM1, 5619be88e75SGabriel Fernandez GATE_SPI2, 5629be88e75SGabriel Fernandez GATE_SPI3, 5639be88e75SGabriel Fernandez GATE_USART3, 5649be88e75SGabriel Fernandez GATE_UART4, 5659be88e75SGabriel Fernandez GATE_UART5, 5669be88e75SGabriel Fernandez GATE_UART7, 5679be88e75SGabriel Fernandez GATE_UART8, 5689be88e75SGabriel Fernandez GATE_I2C1, 5699be88e75SGabriel Fernandez GATE_I2C2, 5709be88e75SGabriel Fernandez GATE_SPDIF, 5719be88e75SGabriel Fernandez GATE_TIM1, 5729be88e75SGabriel Fernandez GATE_TIM8, 5739be88e75SGabriel Fernandez GATE_SPI1, 5749be88e75SGabriel Fernandez GATE_USART6, 5759be88e75SGabriel Fernandez GATE_SAI1, 5769be88e75SGabriel Fernandez GATE_SAI2, 5779be88e75SGabriel Fernandez GATE_DFSDM, 5789be88e75SGabriel Fernandez GATE_ADFSDM, 5799be88e75SGabriel Fernandez GATE_FDCAN, 5809be88e75SGabriel Fernandez GATE_LPTIM2, 5819be88e75SGabriel Fernandez GATE_LPTIM3, 5829be88e75SGabriel Fernandez GATE_LPTIM4, 5839be88e75SGabriel Fernandez GATE_LPTIM5, 5849be88e75SGabriel Fernandez GATE_VREF, 5859be88e75SGabriel Fernandez GATE_DTS, 5869be88e75SGabriel Fernandez GATE_PMBCTRL, 5879be88e75SGabriel Fernandez GATE_HDP, 5889be88e75SGabriel Fernandez GATE_SYSCFG, 5899be88e75SGabriel Fernandez GATE_DCMIPP, 5909be88e75SGabriel Fernandez GATE_DDRPERFM, 5919be88e75SGabriel Fernandez GATE_IWDG2APB, 5929be88e75SGabriel Fernandez GATE_USBPHY, 5939be88e75SGabriel Fernandez GATE_STGENRO, 5949be88e75SGabriel Fernandez GATE_LTDC, 5959be88e75SGabriel Fernandez GATE_RTCAPB, 5969be88e75SGabriel Fernandez GATE_TZC, 5979be88e75SGabriel Fernandez GATE_ETZPC, 5989be88e75SGabriel Fernandez GATE_IWDG1APB, 5999be88e75SGabriel Fernandez GATE_BSEC, 6009be88e75SGabriel Fernandez GATE_STGENC, 6019be88e75SGabriel Fernandez GATE_USART1, 6029be88e75SGabriel Fernandez GATE_USART2, 6039be88e75SGabriel Fernandez GATE_SPI4, 6049be88e75SGabriel Fernandez GATE_SPI5, 6059be88e75SGabriel Fernandez GATE_I2C3, 6069be88e75SGabriel Fernandez GATE_I2C4, 6079be88e75SGabriel Fernandez GATE_I2C5, 6089be88e75SGabriel Fernandez GATE_TIM12, 6099be88e75SGabriel Fernandez GATE_TIM13, 6109be88e75SGabriel Fernandez GATE_TIM14, 6119be88e75SGabriel Fernandez GATE_TIM15, 6129be88e75SGabriel Fernandez GATE_TIM16, 6139be88e75SGabriel Fernandez GATE_TIM17, 6149be88e75SGabriel Fernandez GATE_DMA1, 6159be88e75SGabriel Fernandez GATE_DMA2, 6169be88e75SGabriel Fernandez GATE_DMAMUX1, 6179be88e75SGabriel Fernandez GATE_DMA3, 6189be88e75SGabriel Fernandez GATE_DMAMUX2, 6199be88e75SGabriel Fernandez GATE_ADC1, 6209be88e75SGabriel Fernandez GATE_ADC2, 6219be88e75SGabriel Fernandez GATE_USBO, 6229be88e75SGabriel Fernandez GATE_TSC, 6239be88e75SGabriel Fernandez GATE_GPIOA, 6249be88e75SGabriel Fernandez GATE_GPIOB, 6259be88e75SGabriel Fernandez GATE_GPIOC, 6269be88e75SGabriel Fernandez GATE_GPIOD, 6279be88e75SGabriel Fernandez GATE_GPIOE, 6289be88e75SGabriel Fernandez GATE_GPIOF, 6299be88e75SGabriel Fernandez GATE_GPIOG, 6309be88e75SGabriel Fernandez GATE_GPIOH, 6319be88e75SGabriel Fernandez GATE_GPIOI, 6329be88e75SGabriel Fernandez GATE_PKA, 6339be88e75SGabriel Fernandez GATE_SAES, 6349be88e75SGabriel Fernandez GATE_CRYP1, 6359be88e75SGabriel Fernandez GATE_HASH1, 6369be88e75SGabriel Fernandez GATE_RNG1, 6379be88e75SGabriel Fernandez GATE_BKPSRAM, 6389be88e75SGabriel Fernandez GATE_AXIMC, 6399be88e75SGabriel Fernandez GATE_MCE, 6409be88e75SGabriel Fernandez GATE_ETH1CK, 6419be88e75SGabriel Fernandez GATE_ETH1TX, 6429be88e75SGabriel Fernandez GATE_ETH1RX, 6439be88e75SGabriel Fernandez GATE_ETH1MAC, 6449be88e75SGabriel Fernandez GATE_FMC, 6459be88e75SGabriel Fernandez GATE_QSPI, 6469be88e75SGabriel Fernandez GATE_SDMMC1, 6479be88e75SGabriel Fernandez GATE_SDMMC2, 6489be88e75SGabriel Fernandez GATE_CRC1, 6499be88e75SGabriel Fernandez GATE_USBH, 6509be88e75SGabriel Fernandez GATE_ETH2CK, 6519be88e75SGabriel Fernandez GATE_ETH2TX, 6529be88e75SGabriel Fernandez GATE_ETH2RX, 6539be88e75SGabriel Fernandez GATE_ETH2MAC, 6549be88e75SGabriel Fernandez GATE_MDMA, 6559be88e75SGabriel Fernandez 6569be88e75SGabriel Fernandez LAST_GATE 6579be88e75SGabriel Fernandez }; 6589be88e75SGabriel Fernandez 6599be88e75SGabriel Fernandez #define GATE_CFG(id, _offset, _bit_idx, _offset_clr)[id] = {\ 6609be88e75SGabriel Fernandez .offset = (_offset),\ 6619be88e75SGabriel Fernandez .bit_idx = (_bit_idx),\ 6629be88e75SGabriel Fernandez .set_clr = (_offset_clr),\ 6639be88e75SGabriel Fernandez } 6649be88e75SGabriel Fernandez 6659be88e75SGabriel Fernandez static const struct gate_cfg gates_mp13[LAST_GATE] = { 6669be88e75SGabriel Fernandez GATE_CFG(GATE_LSE, RCC_BDCR, 0, 0), 6679be88e75SGabriel Fernandez GATE_CFG(GATE_RTCCK, RCC_BDCR, 20, 0), 6689be88e75SGabriel Fernandez GATE_CFG(GATE_LSI, RCC_RDLSICR, 0, 0), 6699be88e75SGabriel Fernandez GATE_CFG(GATE_HSI, RCC_OCENSETR, 0, 1), 6709be88e75SGabriel Fernandez GATE_CFG(GATE_CSI, RCC_OCENSETR, 4, 1), 6719be88e75SGabriel Fernandez GATE_CFG(GATE_HSE, RCC_OCENSETR, 8, 1), 6729be88e75SGabriel Fernandez GATE_CFG(GATE_LSI_RDY, RCC_RDLSICR, 1, 0), 6739be88e75SGabriel Fernandez GATE_CFG(GATE_CSI_RDY, RCC_OCRDYR, 4, 0), 6749be88e75SGabriel Fernandez GATE_CFG(GATE_LSE_RDY, RCC_BDCR, 2, 0), 6759be88e75SGabriel Fernandez GATE_CFG(GATE_HSE_RDY, RCC_OCRDYR, 8, 0), 6769be88e75SGabriel Fernandez GATE_CFG(GATE_HSI_RDY, RCC_OCRDYR, 0, 0), 6779be88e75SGabriel Fernandez GATE_CFG(GATE_MCO1, RCC_MCO1CFGR, 12, 0), 6789be88e75SGabriel Fernandez GATE_CFG(GATE_MCO2, RCC_MCO2CFGR, 12, 0), 6799be88e75SGabriel Fernandez GATE_CFG(GATE_DBGCK, RCC_DBGCFGR, 8, 0), 6809be88e75SGabriel Fernandez GATE_CFG(GATE_TRACECK, RCC_DBGCFGR, 9, 0), 6819be88e75SGabriel Fernandez GATE_CFG(GATE_PLL1, RCC_PLL1CR, 0, 0), 6829be88e75SGabriel Fernandez GATE_CFG(GATE_PLL1_DIVP, RCC_PLL1CR, 4, 0), 6839be88e75SGabriel Fernandez GATE_CFG(GATE_PLL1_DIVQ, RCC_PLL1CR, 5, 0), 6849be88e75SGabriel Fernandez GATE_CFG(GATE_PLL1_DIVR, RCC_PLL1CR, 6, 0), 6859be88e75SGabriel Fernandez GATE_CFG(GATE_PLL2, RCC_PLL2CR, 0, 0), 6869be88e75SGabriel Fernandez GATE_CFG(GATE_PLL2_DIVP, RCC_PLL2CR, 4, 0), 6879be88e75SGabriel Fernandez GATE_CFG(GATE_PLL2_DIVQ, RCC_PLL2CR, 5, 0), 6889be88e75SGabriel Fernandez GATE_CFG(GATE_PLL2_DIVR, RCC_PLL2CR, 6, 0), 6899be88e75SGabriel Fernandez GATE_CFG(GATE_PLL3, RCC_PLL3CR, 0, 0), 6909be88e75SGabriel Fernandez GATE_CFG(GATE_PLL3_DIVP, RCC_PLL3CR, 4, 0), 6919be88e75SGabriel Fernandez GATE_CFG(GATE_PLL3_DIVQ, RCC_PLL3CR, 5, 0), 6929be88e75SGabriel Fernandez GATE_CFG(GATE_PLL3_DIVR, RCC_PLL3CR, 6, 0), 6939be88e75SGabriel Fernandez GATE_CFG(GATE_PLL4, RCC_PLL4CR, 0, 0), 6949be88e75SGabriel Fernandez GATE_CFG(GATE_PLL4_DIVP, RCC_PLL4CR, 4, 0), 6959be88e75SGabriel Fernandez GATE_CFG(GATE_PLL4_DIVQ, RCC_PLL4CR, 5, 0), 6969be88e75SGabriel Fernandez GATE_CFG(GATE_PLL4_DIVR, RCC_PLL4CR, 6, 0), 6979be88e75SGabriel Fernandez GATE_CFG(GATE_DDRC1, RCC_DDRITFCR, 0, 0), 6989be88e75SGabriel Fernandez GATE_CFG(GATE_DDRC1LP, RCC_DDRITFCR, 1, 0), 6999be88e75SGabriel Fernandez GATE_CFG(GATE_DDRPHYC, RCC_DDRITFCR, 4, 0), 7009be88e75SGabriel Fernandez GATE_CFG(GATE_DDRPHYCLP, RCC_DDRITFCR, 5, 0), 7019be88e75SGabriel Fernandez GATE_CFG(GATE_DDRCAPB, RCC_DDRITFCR, 6, 0), 7029be88e75SGabriel Fernandez GATE_CFG(GATE_DDRCAPBLP, RCC_DDRITFCR, 7, 0), 7039be88e75SGabriel Fernandez GATE_CFG(GATE_AXIDCG, RCC_DDRITFCR, 8, 0), 7049be88e75SGabriel Fernandez GATE_CFG(GATE_DDRPHYCAPB, RCC_DDRITFCR, 9, 0), 7059be88e75SGabriel Fernandez GATE_CFG(GATE_DDRPHYCAPBLP, RCC_DDRITFCR, 10, 0), 7069be88e75SGabriel Fernandez GATE_CFG(GATE_TIM2, RCC_MP_APB1ENSETR, 0, 1), 7079be88e75SGabriel Fernandez GATE_CFG(GATE_TIM3, RCC_MP_APB1ENSETR, 1, 1), 7089be88e75SGabriel Fernandez GATE_CFG(GATE_TIM4, RCC_MP_APB1ENSETR, 2, 1), 7099be88e75SGabriel Fernandez GATE_CFG(GATE_TIM5, RCC_MP_APB1ENSETR, 3, 1), 7109be88e75SGabriel Fernandez GATE_CFG(GATE_TIM6, RCC_MP_APB1ENSETR, 4, 1), 7119be88e75SGabriel Fernandez GATE_CFG(GATE_TIM7, RCC_MP_APB1ENSETR, 5, 1), 7129be88e75SGabriel Fernandez GATE_CFG(GATE_LPTIM1, RCC_MP_APB1ENSETR, 9, 1), 7139be88e75SGabriel Fernandez GATE_CFG(GATE_SPI2, RCC_MP_APB1ENSETR, 11, 1), 7149be88e75SGabriel Fernandez GATE_CFG(GATE_SPI3, RCC_MP_APB1ENSETR, 12, 1), 7159be88e75SGabriel Fernandez GATE_CFG(GATE_USART3, RCC_MP_APB1ENSETR, 15, 1), 7169be88e75SGabriel Fernandez GATE_CFG(GATE_UART4, RCC_MP_APB1ENSETR, 16, 1), 7179be88e75SGabriel Fernandez GATE_CFG(GATE_UART5, RCC_MP_APB1ENSETR, 17, 1), 7189be88e75SGabriel Fernandez GATE_CFG(GATE_UART7, RCC_MP_APB1ENSETR, 18, 1), 7199be88e75SGabriel Fernandez GATE_CFG(GATE_UART8, RCC_MP_APB1ENSETR, 19, 1), 7209be88e75SGabriel Fernandez GATE_CFG(GATE_I2C1, RCC_MP_APB1ENSETR, 21, 1), 7219be88e75SGabriel Fernandez GATE_CFG(GATE_I2C2, RCC_MP_APB1ENSETR, 22, 1), 7229be88e75SGabriel Fernandez GATE_CFG(GATE_SPDIF, RCC_MP_APB1ENSETR, 26, 1), 7239be88e75SGabriel Fernandez GATE_CFG(GATE_TIM1, RCC_MP_APB2ENSETR, 0, 1), 7249be88e75SGabriel Fernandez GATE_CFG(GATE_TIM8, RCC_MP_APB2ENSETR, 1, 1), 7259be88e75SGabriel Fernandez GATE_CFG(GATE_SPI1, RCC_MP_APB2ENSETR, 8, 1), 7269be88e75SGabriel Fernandez GATE_CFG(GATE_USART6, RCC_MP_APB2ENSETR, 13, 1), 7279be88e75SGabriel Fernandez GATE_CFG(GATE_SAI1, RCC_MP_APB2ENSETR, 16, 1), 7289be88e75SGabriel Fernandez GATE_CFG(GATE_SAI2, RCC_MP_APB2ENSETR, 17, 1), 7299be88e75SGabriel Fernandez GATE_CFG(GATE_DFSDM, RCC_MP_APB2ENSETR, 20, 1), 7309be88e75SGabriel Fernandez GATE_CFG(GATE_ADFSDM, RCC_MP_APB2ENSETR, 21, 1), 7319be88e75SGabriel Fernandez GATE_CFG(GATE_FDCAN, RCC_MP_APB2ENSETR, 24, 1), 7329be88e75SGabriel Fernandez GATE_CFG(GATE_LPTIM2, RCC_MP_APB3ENSETR, 0, 1), 7339be88e75SGabriel Fernandez GATE_CFG(GATE_LPTIM3, RCC_MP_APB3ENSETR, 1, 1), 7349be88e75SGabriel Fernandez GATE_CFG(GATE_LPTIM4, RCC_MP_APB3ENSETR, 2, 1), 7359be88e75SGabriel Fernandez GATE_CFG(GATE_LPTIM5, RCC_MP_APB3ENSETR, 3, 1), 7369be88e75SGabriel Fernandez GATE_CFG(GATE_VREF, RCC_MP_APB3ENSETR, 13, 1), 7379be88e75SGabriel Fernandez GATE_CFG(GATE_DTS, RCC_MP_APB3ENSETR, 16, 1), 7389be88e75SGabriel Fernandez GATE_CFG(GATE_PMBCTRL, RCC_MP_APB3ENSETR, 17, 1), 7399be88e75SGabriel Fernandez GATE_CFG(GATE_HDP, RCC_MP_APB3ENSETR, 20, 1), 7409be88e75SGabriel Fernandez GATE_CFG(GATE_SYSCFG, RCC_MP_S_APB3ENSETR, 0, 1), 7419be88e75SGabriel Fernandez GATE_CFG(GATE_DCMIPP, RCC_MP_APB4ENSETR, 1, 1), 7429be88e75SGabriel Fernandez GATE_CFG(GATE_DDRPERFM, RCC_MP_APB4ENSETR, 8, 1), 7439be88e75SGabriel Fernandez GATE_CFG(GATE_IWDG2APB, RCC_MP_APB4ENSETR, 15, 1), 7449be88e75SGabriel Fernandez GATE_CFG(GATE_USBPHY, RCC_MP_APB4ENSETR, 16, 1), 7459be88e75SGabriel Fernandez GATE_CFG(GATE_STGENRO, RCC_MP_APB4ENSETR, 20, 1), 7469be88e75SGabriel Fernandez GATE_CFG(GATE_LTDC, RCC_MP_S_APB4ENSETR, 0, 1), 7479be88e75SGabriel Fernandez GATE_CFG(GATE_RTCAPB, RCC_MP_APB5ENSETR, 8, 1), 7489be88e75SGabriel Fernandez GATE_CFG(GATE_TZC, RCC_MP_APB5ENSETR, 11, 1), 7499be88e75SGabriel Fernandez GATE_CFG(GATE_ETZPC, RCC_MP_APB5ENSETR, 13, 1), 7509be88e75SGabriel Fernandez GATE_CFG(GATE_IWDG1APB, RCC_MP_APB5ENSETR, 15, 1), 7519be88e75SGabriel Fernandez GATE_CFG(GATE_BSEC, RCC_MP_APB5ENSETR, 16, 1), 7529be88e75SGabriel Fernandez GATE_CFG(GATE_STGENC, RCC_MP_APB5ENSETR, 20, 1), 7539be88e75SGabriel Fernandez GATE_CFG(GATE_USART1, RCC_MP_APB6ENSETR, 0, 1), 7549be88e75SGabriel Fernandez GATE_CFG(GATE_USART2, RCC_MP_APB6ENSETR, 1, 1), 7559be88e75SGabriel Fernandez GATE_CFG(GATE_SPI4, RCC_MP_APB6ENSETR, 2, 1), 7569be88e75SGabriel Fernandez GATE_CFG(GATE_SPI5, RCC_MP_APB6ENSETR, 3, 1), 7579be88e75SGabriel Fernandez GATE_CFG(GATE_I2C3, RCC_MP_APB6ENSETR, 4, 1), 7589be88e75SGabriel Fernandez GATE_CFG(GATE_I2C4, RCC_MP_APB6ENSETR, 5, 1), 7599be88e75SGabriel Fernandez GATE_CFG(GATE_I2C5, RCC_MP_APB6ENSETR, 6, 1), 7609be88e75SGabriel Fernandez GATE_CFG(GATE_TIM12, RCC_MP_APB6ENSETR, 7, 1), 7619be88e75SGabriel Fernandez GATE_CFG(GATE_TIM13, RCC_MP_APB6ENSETR, 8, 1), 7629be88e75SGabriel Fernandez GATE_CFG(GATE_TIM14, RCC_MP_APB6ENSETR, 9, 1), 7639be88e75SGabriel Fernandez GATE_CFG(GATE_TIM15, RCC_MP_APB6ENSETR, 10, 1), 7649be88e75SGabriel Fernandez GATE_CFG(GATE_TIM16, RCC_MP_APB6ENSETR, 11, 1), 7659be88e75SGabriel Fernandez GATE_CFG(GATE_TIM17, RCC_MP_APB6ENSETR, 12, 1), 7669be88e75SGabriel Fernandez GATE_CFG(GATE_DMA1, RCC_MP_AHB2ENSETR, 0, 1), 7679be88e75SGabriel Fernandez GATE_CFG(GATE_DMA2, RCC_MP_AHB2ENSETR, 1, 1), 7689be88e75SGabriel Fernandez GATE_CFG(GATE_DMAMUX1, RCC_MP_AHB2ENSETR, 2, 1), 7699be88e75SGabriel Fernandez GATE_CFG(GATE_DMA3, RCC_MP_AHB2ENSETR, 3, 1), 7709be88e75SGabriel Fernandez GATE_CFG(GATE_DMAMUX2, RCC_MP_AHB2ENSETR, 4, 1), 7719be88e75SGabriel Fernandez GATE_CFG(GATE_ADC1, RCC_MP_AHB2ENSETR, 5, 1), 7729be88e75SGabriel Fernandez GATE_CFG(GATE_ADC2, RCC_MP_AHB2ENSETR, 6, 1), 7739be88e75SGabriel Fernandez GATE_CFG(GATE_USBO, RCC_MP_AHB2ENSETR, 8, 1), 7749be88e75SGabriel Fernandez GATE_CFG(GATE_TSC, RCC_MP_AHB4ENSETR, 15, 1), 7759be88e75SGabriel Fernandez 7769be88e75SGabriel Fernandez GATE_CFG(GATE_GPIOA, RCC_MP_S_AHB4ENSETR, 0, 1), 7779be88e75SGabriel Fernandez GATE_CFG(GATE_GPIOB, RCC_MP_S_AHB4ENSETR, 1, 1), 7789be88e75SGabriel Fernandez GATE_CFG(GATE_GPIOC, RCC_MP_S_AHB4ENSETR, 2, 1), 7799be88e75SGabriel Fernandez GATE_CFG(GATE_GPIOD, RCC_MP_S_AHB4ENSETR, 3, 1), 7809be88e75SGabriel Fernandez GATE_CFG(GATE_GPIOE, RCC_MP_S_AHB4ENSETR, 4, 1), 7819be88e75SGabriel Fernandez GATE_CFG(GATE_GPIOF, RCC_MP_S_AHB4ENSETR, 5, 1), 7829be88e75SGabriel Fernandez GATE_CFG(GATE_GPIOG, RCC_MP_S_AHB4ENSETR, 6, 1), 7839be88e75SGabriel Fernandez GATE_CFG(GATE_GPIOH, RCC_MP_S_AHB4ENSETR, 7, 1), 7849be88e75SGabriel Fernandez GATE_CFG(GATE_GPIOI, RCC_MP_S_AHB4ENSETR, 8, 1), 7859be88e75SGabriel Fernandez 7869be88e75SGabriel Fernandez GATE_CFG(GATE_PKA, RCC_MP_AHB5ENSETR, 2, 1), 7879be88e75SGabriel Fernandez GATE_CFG(GATE_SAES, RCC_MP_AHB5ENSETR, 3, 1), 7889be88e75SGabriel Fernandez GATE_CFG(GATE_CRYP1, RCC_MP_AHB5ENSETR, 4, 1), 7899be88e75SGabriel Fernandez GATE_CFG(GATE_HASH1, RCC_MP_AHB5ENSETR, 5, 1), 7909be88e75SGabriel Fernandez GATE_CFG(GATE_RNG1, RCC_MP_AHB5ENSETR, 6, 1), 7919be88e75SGabriel Fernandez GATE_CFG(GATE_BKPSRAM, RCC_MP_AHB5ENSETR, 8, 1), 7929be88e75SGabriel Fernandez GATE_CFG(GATE_AXIMC, RCC_MP_AHB5ENSETR, 16, 1), 7939be88e75SGabriel Fernandez GATE_CFG(GATE_MCE, RCC_MP_AHB6ENSETR, 1, 1), 7949be88e75SGabriel Fernandez GATE_CFG(GATE_ETH1CK, RCC_MP_AHB6ENSETR, 7, 1), 7959be88e75SGabriel Fernandez GATE_CFG(GATE_ETH1TX, RCC_MP_AHB6ENSETR, 8, 1), 7969be88e75SGabriel Fernandez GATE_CFG(GATE_ETH1RX, RCC_MP_AHB6ENSETR, 9, 1), 7979be88e75SGabriel Fernandez GATE_CFG(GATE_ETH1MAC, RCC_MP_AHB6ENSETR, 10, 1), 7989be88e75SGabriel Fernandez GATE_CFG(GATE_FMC, RCC_MP_AHB6ENSETR, 12, 1), 7999be88e75SGabriel Fernandez GATE_CFG(GATE_QSPI, RCC_MP_AHB6ENSETR, 14, 1), 8009be88e75SGabriel Fernandez GATE_CFG(GATE_SDMMC1, RCC_MP_AHB6ENSETR, 16, 1), 8019be88e75SGabriel Fernandez GATE_CFG(GATE_SDMMC2, RCC_MP_AHB6ENSETR, 17, 1), 8029be88e75SGabriel Fernandez GATE_CFG(GATE_CRC1, RCC_MP_AHB6ENSETR, 20, 1), 8039be88e75SGabriel Fernandez GATE_CFG(GATE_USBH, RCC_MP_AHB6ENSETR, 24, 1), 8049be88e75SGabriel Fernandez GATE_CFG(GATE_ETH2CK, RCC_MP_AHB6ENSETR, 27, 1), 8059be88e75SGabriel Fernandez GATE_CFG(GATE_ETH2TX, RCC_MP_AHB6ENSETR, 28, 1), 8069be88e75SGabriel Fernandez GATE_CFG(GATE_ETH2RX, RCC_MP_AHB6ENSETR, 29, 1), 8079be88e75SGabriel Fernandez GATE_CFG(GATE_ETH2MAC, RCC_MP_AHB6ENSETR, 30, 1), 8089be88e75SGabriel Fernandez GATE_CFG(GATE_MDMA, RCC_MP_S_AHB6ENSETR, 0, 1), 8099be88e75SGabriel Fernandez }; 8109be88e75SGabriel Fernandez 8119be88e75SGabriel Fernandez /* 8129be88e75SGabriel Fernandez * DIV CONFIG 8139be88e75SGabriel Fernandez */ 8149be88e75SGabriel Fernandez 8159be88e75SGabriel Fernandez static const struct clk_div_table axi_div_table[] = { 8169be88e75SGabriel Fernandez { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 4 }, 8179be88e75SGabriel Fernandez { 4, 4 }, { 5, 4 }, { 6, 4 }, { 7, 4 }, 8189be88e75SGabriel Fernandez { 0 }, 8199be88e75SGabriel Fernandez }; 8209be88e75SGabriel Fernandez 8219be88e75SGabriel Fernandez static const struct clk_div_table mlahb_div_table[] = { 8229be88e75SGabriel Fernandez { 0, 1 }, { 1, 2 }, { 2, 4 }, { 3, 8 }, 8239be88e75SGabriel Fernandez { 4, 16 }, { 5, 32 }, { 6, 64 }, { 7, 128 }, 8249be88e75SGabriel Fernandez { 8, 256 }, { 9, 512 }, { 10, 512}, { 11, 512 }, 8259be88e75SGabriel Fernandez { 12, 512 }, { 13, 512 }, { 14, 512}, { 15, 512 }, 8269be88e75SGabriel Fernandez { 0 }, 8279be88e75SGabriel Fernandez }; 8289be88e75SGabriel Fernandez 8299be88e75SGabriel Fernandez static const struct clk_div_table apb_div_table[] = { 8309be88e75SGabriel Fernandez { 0, 1 }, { 1, 2 }, { 2, 4 }, { 3, 8 }, 8319be88e75SGabriel Fernandez { 4, 16 }, { 5, 16 }, { 6, 16 }, { 7, 16 }, 8329be88e75SGabriel Fernandez { 0 }, 8339be88e75SGabriel Fernandez }; 8349be88e75SGabriel Fernandez 8359be88e75SGabriel Fernandez #define DIV_CFG(id, _offset, _shift, _width, _flags, _table, _bitrdy)[id] = {\ 8369be88e75SGabriel Fernandez .offset = _offset,\ 8379be88e75SGabriel Fernandez .shift = _shift,\ 8389be88e75SGabriel Fernandez .width = _width,\ 8399be88e75SGabriel Fernandez .flags = _flags,\ 8409be88e75SGabriel Fernandez .table = _table,\ 8419be88e75SGabriel Fernandez .bitrdy = _bitrdy,\ 8429be88e75SGabriel Fernandez } 8439be88e75SGabriel Fernandez 844*ee21709eSYann Gautier static const struct div_cfg dividers_mp13[DIV_MAX] = { 8459be88e75SGabriel Fernandez DIV_CFG(DIV_PLL1DIVP, RCC_PLL1CFGR2, 0, 7, 0, NULL, DIV_NO_BIT_RDY), 8469be88e75SGabriel Fernandez DIV_CFG(DIV_PLL2DIVP, RCC_PLL2CFGR2, 0, 7, 0, NULL, DIV_NO_BIT_RDY), 8479be88e75SGabriel Fernandez DIV_CFG(DIV_PLL2DIVQ, RCC_PLL2CFGR2, 8, 7, 0, NULL, DIV_NO_BIT_RDY), 8489be88e75SGabriel Fernandez DIV_CFG(DIV_PLL2DIVR, RCC_PLL2CFGR2, 16, 7, 0, NULL, DIV_NO_BIT_RDY), 8499be88e75SGabriel Fernandez DIV_CFG(DIV_PLL3DIVP, RCC_PLL3CFGR2, 0, 7, 0, NULL, DIV_NO_BIT_RDY), 8509be88e75SGabriel Fernandez DIV_CFG(DIV_PLL3DIVQ, RCC_PLL3CFGR2, 8, 7, 0, NULL, DIV_NO_BIT_RDY), 8519be88e75SGabriel Fernandez DIV_CFG(DIV_PLL3DIVR, RCC_PLL3CFGR2, 16, 7, 0, NULL, DIV_NO_BIT_RDY), 8529be88e75SGabriel Fernandez DIV_CFG(DIV_PLL4DIVP, RCC_PLL4CFGR2, 0, 7, 0, NULL, DIV_NO_BIT_RDY), 8539be88e75SGabriel Fernandez DIV_CFG(DIV_PLL4DIVQ, RCC_PLL4CFGR2, 8, 7, 0, NULL, DIV_NO_BIT_RDY), 8549be88e75SGabriel Fernandez DIV_CFG(DIV_PLL4DIVR, RCC_PLL4CFGR2, 16, 7, 0, NULL, DIV_NO_BIT_RDY), 8559be88e75SGabriel Fernandez DIV_CFG(DIV_MPU, RCC_MPCKDIVR, 0, 4, 0, NULL, DIV_NO_BIT_RDY), 8569be88e75SGabriel Fernandez DIV_CFG(DIV_AXI, RCC_AXIDIVR, 0, 3, 0, axi_div_table, 31), 8579be88e75SGabriel Fernandez DIV_CFG(DIV_MLAHB, RCC_MLAHBDIVR, 0, 4, 0, mlahb_div_table, 31), 8589be88e75SGabriel Fernandez DIV_CFG(DIV_APB1, RCC_APB1DIVR, 0, 3, 0, apb_div_table, 31), 8599be88e75SGabriel Fernandez DIV_CFG(DIV_APB2, RCC_APB2DIVR, 0, 3, 0, apb_div_table, 31), 8609be88e75SGabriel Fernandez DIV_CFG(DIV_APB3, RCC_APB3DIVR, 0, 3, 0, apb_div_table, 31), 8619be88e75SGabriel Fernandez DIV_CFG(DIV_APB4, RCC_APB4DIVR, 0, 3, 0, apb_div_table, 31), 8629be88e75SGabriel Fernandez DIV_CFG(DIV_APB5, RCC_APB5DIVR, 0, 3, 0, apb_div_table, 31), 8639be88e75SGabriel Fernandez DIV_CFG(DIV_APB6, RCC_APB6DIVR, 0, 3, 0, apb_div_table, 31), 8649be88e75SGabriel Fernandez DIV_CFG(DIV_RTC, RCC_RTCDIVR, 0, 6, 0, NULL, DIV_NO_BIT_RDY), 8659be88e75SGabriel Fernandez DIV_CFG(DIV_MCO1, RCC_MCO1CFGR, 4, 4, 0, NULL, DIV_NO_BIT_RDY), 8669be88e75SGabriel Fernandez DIV_CFG(DIV_MCO2, RCC_MCO2CFGR, 4, 4, 0, NULL, DIV_NO_BIT_RDY), 8679be88e75SGabriel Fernandez 8689be88e75SGabriel Fernandez DIV_CFG(DIV_HSI, RCC_HSICFGR, 0, 2, CLK_DIVIDER_POWER_OF_TWO, NULL, DIV_NO_BIT_RDY), 8699be88e75SGabriel Fernandez DIV_CFG(DIV_TRACE, RCC_DBGCFGR, 0, 3, CLK_DIVIDER_POWER_OF_TWO, NULL, DIV_NO_BIT_RDY), 8709be88e75SGabriel Fernandez 8719be88e75SGabriel Fernandez DIV_CFG(DIV_ETH1PTP, RCC_ETH12CKSELR, 4, 4, 0, NULL, DIV_NO_BIT_RDY), 8729be88e75SGabriel Fernandez DIV_CFG(DIV_ETH2PTP, RCC_ETH12CKSELR, 12, 4, 0, NULL, DIV_NO_BIT_RDY), 8739be88e75SGabriel Fernandez }; 8749be88e75SGabriel Fernandez 8759be88e75SGabriel Fernandez #define MAX_HSI_HZ 64000000 8769be88e75SGabriel Fernandez #define USB_PHY_48_MHZ 48000000 8779be88e75SGabriel Fernandez 8789be88e75SGabriel Fernandez #define TIMEOUT_US_200MS U(200000) 8799be88e75SGabriel Fernandez #define TIMEOUT_US_1S U(1000000) 8809be88e75SGabriel Fernandez 8819be88e75SGabriel Fernandez #define PLLRDY_TIMEOUT TIMEOUT_US_200MS 8829be88e75SGabriel Fernandez #define CLKSRC_TIMEOUT TIMEOUT_US_200MS 8839be88e75SGabriel Fernandez #define CLKDIV_TIMEOUT TIMEOUT_US_200MS 8849be88e75SGabriel Fernandez #define HSIDIV_TIMEOUT TIMEOUT_US_200MS 8859be88e75SGabriel Fernandez #define OSCRDY_TIMEOUT TIMEOUT_US_1S 8869be88e75SGabriel Fernandez 8879be88e75SGabriel Fernandez enum stm32_osc { 8889be88e75SGabriel Fernandez OSC_HSI, 8899be88e75SGabriel Fernandez OSC_HSE, 8909be88e75SGabriel Fernandez OSC_CSI, 8919be88e75SGabriel Fernandez OSC_LSI, 8929be88e75SGabriel Fernandez OSC_LSE, 8939be88e75SGabriel Fernandez OSC_I2SCKIN, 8949be88e75SGabriel Fernandez NB_OSCILLATOR 8959be88e75SGabriel Fernandez }; 8969be88e75SGabriel Fernandez 8979be88e75SGabriel Fernandez enum stm32mp1_pll_id { 8989be88e75SGabriel Fernandez _PLL1, 8999be88e75SGabriel Fernandez _PLL2, 9009be88e75SGabriel Fernandez _PLL3, 9019be88e75SGabriel Fernandez _PLL4, 9029be88e75SGabriel Fernandez _PLL_NB 9039be88e75SGabriel Fernandez }; 9049be88e75SGabriel Fernandez 9059be88e75SGabriel Fernandez enum stm32mp1_plltype { 9069be88e75SGabriel Fernandez PLL_800, 9079be88e75SGabriel Fernandez PLL_1600, 9089be88e75SGabriel Fernandez PLL_2000, 9099be88e75SGabriel Fernandez PLL_TYPE_NB 9109be88e75SGabriel Fernandez }; 9119be88e75SGabriel Fernandez 9129be88e75SGabriel Fernandez #define RCC_OFFSET_PLLXCR 0 9139be88e75SGabriel Fernandez #define RCC_OFFSET_PLLXCFGR1 4 9149be88e75SGabriel Fernandez #define RCC_OFFSET_PLLXCFGR2 8 9159be88e75SGabriel Fernandez #define RCC_OFFSET_PLLXFRACR 12 9169be88e75SGabriel Fernandez #define RCC_OFFSET_PLLXCSGR 16 9179be88e75SGabriel Fernandez 9189be88e75SGabriel Fernandez struct stm32_clk_pll { 9199be88e75SGabriel Fernandez enum stm32mp1_plltype plltype; 9209be88e75SGabriel Fernandez uint16_t clk_id; 9219be88e75SGabriel Fernandez uint16_t reg_pllxcr; 9229be88e75SGabriel Fernandez }; 9239be88e75SGabriel Fernandez 9249be88e75SGabriel Fernandez struct stm32mp1_pll { 9259be88e75SGabriel Fernandez uint8_t refclk_min; 9269be88e75SGabriel Fernandez uint8_t refclk_max; 9279be88e75SGabriel Fernandez }; 9289be88e75SGabriel Fernandez 9299be88e75SGabriel Fernandez /* Define characteristic of PLL according type */ 9309be88e75SGabriel Fernandez static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = { 9319be88e75SGabriel Fernandez [PLL_800] = { 9329be88e75SGabriel Fernandez .refclk_min = 4, 9339be88e75SGabriel Fernandez .refclk_max = 16, 9349be88e75SGabriel Fernandez }, 9359be88e75SGabriel Fernandez [PLL_1600] = { 9369be88e75SGabriel Fernandez .refclk_min = 8, 9379be88e75SGabriel Fernandez .refclk_max = 16, 9389be88e75SGabriel Fernandez }, 9399be88e75SGabriel Fernandez [PLL_2000] = { 9409be88e75SGabriel Fernandez .refclk_min = 8, 9419be88e75SGabriel Fernandez .refclk_max = 16, 9429be88e75SGabriel Fernandez }, 9439be88e75SGabriel Fernandez }; 9449be88e75SGabriel Fernandez 9459be88e75SGabriel Fernandez #if STM32MP_USB_PROGRAMMER 9469be88e75SGabriel Fernandez static bool pll4_bootrom; 9479be88e75SGabriel Fernandez #endif 9489be88e75SGabriel Fernandez 9499be88e75SGabriel Fernandez /* RCC clock device driver private */ 9509be88e75SGabriel Fernandez static unsigned int refcounts_mp13[CK_LAST]; 9519be88e75SGabriel Fernandez 9529be88e75SGabriel Fernandez static const struct stm32_clk_pll *clk_st32_pll_data(unsigned int idx); 9539be88e75SGabriel Fernandez 9549be88e75SGabriel Fernandez #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER 9559be88e75SGabriel Fernandez static void clk_oscillator_check_bypass(struct stm32_clk_priv *priv, int idx, 9569be88e75SGabriel Fernandez bool digbyp, bool bypass) 9579be88e75SGabriel Fernandez { 9589be88e75SGabriel Fernandez struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, idx); 9599be88e75SGabriel Fernandez struct stm32_clk_bypass *bypass_data = osc_data->bypass; 9609be88e75SGabriel Fernandez uintptr_t address; 9619be88e75SGabriel Fernandez 9629be88e75SGabriel Fernandez if (bypass_data == NULL) { 9639be88e75SGabriel Fernandez return; 9649be88e75SGabriel Fernandez } 9659be88e75SGabriel Fernandez 9669be88e75SGabriel Fernandez address = priv->base + bypass_data->offset; 9679be88e75SGabriel Fernandez if ((mmio_read_32(address) & RCC_OCENR_HSEBYP) && 9689be88e75SGabriel Fernandez (!(digbyp || bypass))) { 9699be88e75SGabriel Fernandez panic(); 9709be88e75SGabriel Fernandez } 9719be88e75SGabriel Fernandez } 9729be88e75SGabriel Fernandez #endif 9739be88e75SGabriel Fernandez 9749be88e75SGabriel Fernandez static void stm32_enable_oscillator_hse(struct stm32_clk_priv *priv) 9759be88e75SGabriel Fernandez { 9769be88e75SGabriel Fernandez struct stm32_clk_platdata *pdata = priv->pdata; 9779be88e75SGabriel Fernandez struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_HSE]; 9789be88e75SGabriel Fernandez bool digbyp = osci->digbyp; 9799be88e75SGabriel Fernandez bool bypass = osci->bypass; 9809be88e75SGabriel Fernandez bool css = osci->css; 9819be88e75SGabriel Fernandez 9829be88e75SGabriel Fernandez if (_clk_stm32_get_rate(priv, _CK_HSE) == 0U) { 9839be88e75SGabriel Fernandez return; 9849be88e75SGabriel Fernandez } 9859be88e75SGabriel Fernandez 9869be88e75SGabriel Fernandez clk_oscillator_set_bypass(priv, _CK_HSE, digbyp, bypass); 9879be88e75SGabriel Fernandez 9889be88e75SGabriel Fernandez _clk_stm32_enable(priv, _CK_HSE); 9899be88e75SGabriel Fernandez 9909be88e75SGabriel Fernandez #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER 9919be88e75SGabriel Fernandez clk_oscillator_check_bypass(priv, _CK_HSE, digbyp, bypass); 9929be88e75SGabriel Fernandez #endif 9939be88e75SGabriel Fernandez 9949be88e75SGabriel Fernandez clk_oscillator_set_css(priv, _CK_HSE, css); 9959be88e75SGabriel Fernandez } 9969be88e75SGabriel Fernandez 9979be88e75SGabriel Fernandez static void stm32_enable_oscillator_lse(struct stm32_clk_priv *priv) 9989be88e75SGabriel Fernandez { 9999be88e75SGabriel Fernandez struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, _CK_LSE); 10009be88e75SGabriel Fernandez struct stm32_clk_platdata *pdata = priv->pdata; 10019be88e75SGabriel Fernandez struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_LSE]; 10029be88e75SGabriel Fernandez bool digbyp = osci->digbyp; 10039be88e75SGabriel Fernandez bool bypass = osci->bypass; 10049be88e75SGabriel Fernandez uint8_t drive = osci->drive; 10059be88e75SGabriel Fernandez 10069be88e75SGabriel Fernandez if (_clk_stm32_get_rate(priv, _CK_LSE) == 0U) { 10079be88e75SGabriel Fernandez return; 10089be88e75SGabriel Fernandez } 10099be88e75SGabriel Fernandez 10109be88e75SGabriel Fernandez clk_oscillator_set_bypass(priv, _CK_LSE, digbyp, bypass); 10119be88e75SGabriel Fernandez 10129be88e75SGabriel Fernandez clk_oscillator_set_drive(priv, _CK_LSE, drive); 10139be88e75SGabriel Fernandez 10149be88e75SGabriel Fernandez _clk_stm32_gate_enable(priv, osc_data->gate_id); 10159be88e75SGabriel Fernandez } 10169be88e75SGabriel Fernandez 10179be88e75SGabriel Fernandez static int stm32mp1_set_hsidiv(uint8_t hsidiv) 10189be88e75SGabriel Fernandez { 10199be88e75SGabriel Fernandez uint64_t timeout; 10209be88e75SGabriel Fernandez uintptr_t rcc_base = stm32mp_rcc_base(); 10219be88e75SGabriel Fernandez uintptr_t address = rcc_base + RCC_OCRDYR; 10229be88e75SGabriel Fernandez 10239be88e75SGabriel Fernandez mmio_clrsetbits_32(rcc_base + RCC_HSICFGR, 10249be88e75SGabriel Fernandez RCC_HSICFGR_HSIDIV_MASK, 10259be88e75SGabriel Fernandez RCC_HSICFGR_HSIDIV_MASK & (uint32_t)hsidiv); 10269be88e75SGabriel Fernandez 10279be88e75SGabriel Fernandez timeout = timeout_init_us(HSIDIV_TIMEOUT); 10289be88e75SGabriel Fernandez while ((mmio_read_32(address) & RCC_OCRDYR_HSIDIVRDY) == 0U) { 10299be88e75SGabriel Fernandez if (timeout_elapsed(timeout)) { 10309be88e75SGabriel Fernandez ERROR("HSIDIV failed @ 0x%lx: 0x%x\n", 10319be88e75SGabriel Fernandez address, mmio_read_32(address)); 10329be88e75SGabriel Fernandez return -ETIMEDOUT; 10339be88e75SGabriel Fernandez } 10349be88e75SGabriel Fernandez } 10359be88e75SGabriel Fernandez 10369be88e75SGabriel Fernandez return 0; 10379be88e75SGabriel Fernandez } 10389be88e75SGabriel Fernandez 10399be88e75SGabriel Fernandez static int stm32mp1_hsidiv(unsigned long hsifreq) 10409be88e75SGabriel Fernandez { 10419be88e75SGabriel Fernandez uint8_t hsidiv; 10429be88e75SGabriel Fernandez uint32_t hsidivfreq = MAX_HSI_HZ; 10439be88e75SGabriel Fernandez 10449be88e75SGabriel Fernandez for (hsidiv = 0; hsidiv < 4U; hsidiv++) { 10459be88e75SGabriel Fernandez if (hsidivfreq == hsifreq) { 10469be88e75SGabriel Fernandez break; 10479be88e75SGabriel Fernandez } 10489be88e75SGabriel Fernandez 10499be88e75SGabriel Fernandez hsidivfreq /= 2U; 10509be88e75SGabriel Fernandez } 10519be88e75SGabriel Fernandez 10529be88e75SGabriel Fernandez if (hsidiv == 4U) { 10539be88e75SGabriel Fernandez ERROR("Invalid clk-hsi frequency\n"); 10549be88e75SGabriel Fernandez return -EINVAL; 10559be88e75SGabriel Fernandez } 10569be88e75SGabriel Fernandez 10579be88e75SGabriel Fernandez if (hsidiv != 0U) { 10589be88e75SGabriel Fernandez return stm32mp1_set_hsidiv(hsidiv); 10599be88e75SGabriel Fernandez } 10609be88e75SGabriel Fernandez 10619be88e75SGabriel Fernandez return 0; 10629be88e75SGabriel Fernandez } 10639be88e75SGabriel Fernandez 10649be88e75SGabriel Fernandez static int stm32_clk_oscillators_lse_set_css(struct stm32_clk_priv *priv) 10659be88e75SGabriel Fernandez { 10669be88e75SGabriel Fernandez struct stm32_clk_platdata *pdata = priv->pdata; 10679be88e75SGabriel Fernandez struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_LSE]; 10689be88e75SGabriel Fernandez 10699be88e75SGabriel Fernandez clk_oscillator_set_css(priv, _CK_LSE, osci->css); 10709be88e75SGabriel Fernandez 10719be88e75SGabriel Fernandez return 0; 10729be88e75SGabriel Fernandez } 10739be88e75SGabriel Fernandez 10749be88e75SGabriel Fernandez static int stm32mp1_come_back_to_hsi(void) 10759be88e75SGabriel Fernandez { 10769be88e75SGabriel Fernandez int ret; 10779be88e75SGabriel Fernandez struct stm32_clk_priv *priv = clk_stm32_get_priv(); 10789be88e75SGabriel Fernandez 10799be88e75SGabriel Fernandez /* Come back to HSI */ 10809be88e75SGabriel Fernandez ret = _clk_stm32_set_parent(priv, _CKMPU, _CK_HSI); 10819be88e75SGabriel Fernandez if (ret != 0) { 10829be88e75SGabriel Fernandez return ret; 10839be88e75SGabriel Fernandez } 10849be88e75SGabriel Fernandez 10859be88e75SGabriel Fernandez ret = _clk_stm32_set_parent(priv, _CKAXI, _CK_HSI); 10869be88e75SGabriel Fernandez if (ret != 0) { 10879be88e75SGabriel Fernandez return ret; 10889be88e75SGabriel Fernandez } 10899be88e75SGabriel Fernandez 10909be88e75SGabriel Fernandez ret = _clk_stm32_set_parent(priv, _CKMLAHB, _CK_HSI); 10919be88e75SGabriel Fernandez if (ret != 0) { 10929be88e75SGabriel Fernandez return ret; 10939be88e75SGabriel Fernandez } 10949be88e75SGabriel Fernandez 10959be88e75SGabriel Fernandez return 0; 10969be88e75SGabriel Fernandez } 10979be88e75SGabriel Fernandez 10989be88e75SGabriel Fernandez static int stm32_clk_configure_clk_get_binding_id(struct stm32_clk_priv *priv, uint32_t data) 10999be88e75SGabriel Fernandez { 11009be88e75SGabriel Fernandez unsigned long binding_id = ((unsigned long)data & CLK_ID_MASK) >> CLK_ID_SHIFT; 11019be88e75SGabriel Fernandez 11029be88e75SGabriel Fernandez return clk_get_index(priv, binding_id); 11039be88e75SGabriel Fernandez } 11049be88e75SGabriel Fernandez 11059be88e75SGabriel Fernandez static int stm32_clk_configure_clk(struct stm32_clk_priv *priv, uint32_t data) 11069be88e75SGabriel Fernandez { 11079be88e75SGabriel Fernandez int sel = (data & CLK_SEL_MASK) >> CLK_SEL_SHIFT; 11089be88e75SGabriel Fernandez int enable = (data & CLK_ON_MASK) >> CLK_ON_SHIFT; 11099be88e75SGabriel Fernandez int clk_id; 11109be88e75SGabriel Fernandez int ret; 11119be88e75SGabriel Fernandez 11129be88e75SGabriel Fernandez clk_id = stm32_clk_configure_clk_get_binding_id(priv, data); 11139be88e75SGabriel Fernandez if (clk_id < 0) { 11149be88e75SGabriel Fernandez return clk_id; 11159be88e75SGabriel Fernandez } 11169be88e75SGabriel Fernandez 11179be88e75SGabriel Fernandez ret = _clk_stm32_set_parent_by_index(priv, clk_id, sel); 11189be88e75SGabriel Fernandez if (ret != 0) { 11199be88e75SGabriel Fernandez return ret; 11209be88e75SGabriel Fernandez } 11219be88e75SGabriel Fernandez 1122c3ae7da0SYann Gautier if (enable != 0) { 11239be88e75SGabriel Fernandez clk_stm32_enable_call_ops(priv, clk_id); 11249be88e75SGabriel Fernandez } else { 11259be88e75SGabriel Fernandez clk_stm32_disable_call_ops(priv, clk_id); 11269be88e75SGabriel Fernandez } 11279be88e75SGabriel Fernandez 11289be88e75SGabriel Fernandez return 0; 11299be88e75SGabriel Fernandez } 11309be88e75SGabriel Fernandez 11319be88e75SGabriel Fernandez static int stm32_clk_configure_mux(struct stm32_clk_priv *priv, uint32_t data) 11329be88e75SGabriel Fernandez { 11339be88e75SGabriel Fernandez int mux = (data & MUX_ID_MASK) >> MUX_ID_SHIFT; 11349be88e75SGabriel Fernandez int sel = (data & MUX_SEL_MASK) >> MUX_SEL_SHIFT; 11359be88e75SGabriel Fernandez 11369be88e75SGabriel Fernandez return clk_mux_set_parent(priv, mux, sel); 11379be88e75SGabriel Fernandez } 11389be88e75SGabriel Fernandez 11399be88e75SGabriel Fernandez static int stm32_clk_dividers_configure(struct stm32_clk_priv *priv) 11409be88e75SGabriel Fernandez { 11419be88e75SGabriel Fernandez struct stm32_clk_platdata *pdata = priv->pdata; 11429be88e75SGabriel Fernandez uint32_t i; 11439be88e75SGabriel Fernandez 11449be88e75SGabriel Fernandez for (i = 0; i < pdata->nclkdiv; i++) { 11459be88e75SGabriel Fernandez int div_id, div_n; 11469be88e75SGabriel Fernandez int val; 11479be88e75SGabriel Fernandez int ret; 11489be88e75SGabriel Fernandez 11499be88e75SGabriel Fernandez val = pdata->clkdiv[i] & CMD_DATA_MASK; 11509be88e75SGabriel Fernandez div_id = (val & DIV_ID_MASK) >> DIV_ID_SHIFT; 11519be88e75SGabriel Fernandez div_n = (val & DIV_DIVN_MASK) >> DIV_DIVN_SHIFT; 11529be88e75SGabriel Fernandez 11539be88e75SGabriel Fernandez ret = clk_stm32_set_div(priv, div_id, div_n); 11549be88e75SGabriel Fernandez if (ret != 0) { 11559be88e75SGabriel Fernandez return ret; 11569be88e75SGabriel Fernandez } 11579be88e75SGabriel Fernandez } 11589be88e75SGabriel Fernandez 11599be88e75SGabriel Fernandez return 0; 11609be88e75SGabriel Fernandez } 11619be88e75SGabriel Fernandez 11629be88e75SGabriel Fernandez static int stm32_clk_source_configure(struct stm32_clk_priv *priv) 11639be88e75SGabriel Fernandez { 11649be88e75SGabriel Fernandez struct stm32_clk_platdata *pdata = priv->pdata; 11659be88e75SGabriel Fernandez bool ckper_disabled = false; 11669be88e75SGabriel Fernandez int clk_id; 11679be88e75SGabriel Fernandez int ret; 11689be88e75SGabriel Fernandez uint32_t i; 11699be88e75SGabriel Fernandez 11709be88e75SGabriel Fernandez for (i = 0; i < pdata->nclksrc; i++) { 11719be88e75SGabriel Fernandez uint32_t val = pdata->clksrc[i]; 11729be88e75SGabriel Fernandez uint32_t cmd, cmd_data; 11739be88e75SGabriel Fernandez 11749be88e75SGabriel Fernandez if (val == (uint32_t)CLK_CKPER_DISABLED) { 11759be88e75SGabriel Fernandez ckper_disabled = true; 11769be88e75SGabriel Fernandez continue; 11779be88e75SGabriel Fernandez } 11789be88e75SGabriel Fernandez 11799be88e75SGabriel Fernandez if (val == (uint32_t)CLK_RTC_DISABLED) { 11809be88e75SGabriel Fernandez continue; 11819be88e75SGabriel Fernandez } 11829be88e75SGabriel Fernandez 11839be88e75SGabriel Fernandez cmd = (val & CMD_MASK) >> CMD_SHIFT; 11849be88e75SGabriel Fernandez cmd_data = val & ~CMD_MASK; 11859be88e75SGabriel Fernandez 11869be88e75SGabriel Fernandez switch (cmd) { 11879be88e75SGabriel Fernandez case CMD_MUX: 11889be88e75SGabriel Fernandez ret = stm32_clk_configure_mux(priv, cmd_data); 11899be88e75SGabriel Fernandez break; 11909be88e75SGabriel Fernandez 11919be88e75SGabriel Fernandez case CMD_CLK: 11929be88e75SGabriel Fernandez clk_id = stm32_clk_configure_clk_get_binding_id(priv, cmd_data); 11939be88e75SGabriel Fernandez 11949be88e75SGabriel Fernandez if (clk_id == _RTCCK) { 11959be88e75SGabriel Fernandez if ((_clk_stm32_is_enabled(priv, _RTCCK) == true)) { 11969be88e75SGabriel Fernandez continue; 11979be88e75SGabriel Fernandez } 11989be88e75SGabriel Fernandez } 11999be88e75SGabriel Fernandez 12009be88e75SGabriel Fernandez ret = stm32_clk_configure_clk(priv, cmd_data); 12019be88e75SGabriel Fernandez break; 12029be88e75SGabriel Fernandez default: 12039be88e75SGabriel Fernandez ret = -EINVAL; 12049be88e75SGabriel Fernandez break; 12059be88e75SGabriel Fernandez } 12069be88e75SGabriel Fernandez 12079be88e75SGabriel Fernandez if (ret != 0) { 12089be88e75SGabriel Fernandez return ret; 12099be88e75SGabriel Fernandez } 12109be88e75SGabriel Fernandez } 12119be88e75SGabriel Fernandez 12129be88e75SGabriel Fernandez /* 12139be88e75SGabriel Fernandez * CKPER is source for some peripheral clocks 12149be88e75SGabriel Fernandez * (FMC-NAND / QPSI-NOR) and switching source is allowed 12159be88e75SGabriel Fernandez * only if previous clock is still ON 12169be88e75SGabriel Fernandez * => deactivate CKPER only after switching clock 12179be88e75SGabriel Fernandez */ 12189be88e75SGabriel Fernandez if (ckper_disabled) { 12199be88e75SGabriel Fernandez ret = stm32_clk_configure_mux(priv, CLK_CKPER_DISABLED & CMD_MASK); 12209be88e75SGabriel Fernandez if (ret != 0) { 12219be88e75SGabriel Fernandez return ret; 12229be88e75SGabriel Fernandez } 12239be88e75SGabriel Fernandez } 12249be88e75SGabriel Fernandez 12259be88e75SGabriel Fernandez return 0; 12269be88e75SGabriel Fernandez } 12279be88e75SGabriel Fernandez 12289be88e75SGabriel Fernandez static int stm32_clk_stgen_configure(struct stm32_clk_priv *priv, int id) 12299be88e75SGabriel Fernandez { 12309be88e75SGabriel Fernandez unsigned long stgen_freq; 12319be88e75SGabriel Fernandez 12329be88e75SGabriel Fernandez stgen_freq = _clk_stm32_get_rate(priv, id); 12339be88e75SGabriel Fernandez 12349be88e75SGabriel Fernandez stm32mp_stgen_config(stgen_freq); 12359be88e75SGabriel Fernandez 12369be88e75SGabriel Fernandez return 0; 12379be88e75SGabriel Fernandez } 12389be88e75SGabriel Fernandez 12399be88e75SGabriel Fernandez #define CLK_PLL_CFG(_idx, _clk_id, _type, _reg)\ 12409be88e75SGabriel Fernandez [(_idx)] = {\ 12419be88e75SGabriel Fernandez .clk_id = (_clk_id),\ 12429be88e75SGabriel Fernandez .plltype = (_type),\ 12439be88e75SGabriel Fernandez .reg_pllxcr = (_reg),\ 12449be88e75SGabriel Fernandez } 12459be88e75SGabriel Fernandez 12469be88e75SGabriel Fernandez static int clk_stm32_pll_compute_cfgr1(struct stm32_clk_priv *priv, 12479be88e75SGabriel Fernandez const struct stm32_clk_pll *pll, 12489be88e75SGabriel Fernandez struct stm32_pll_vco *vco, 12499be88e75SGabriel Fernandez uint32_t *value) 12509be88e75SGabriel Fernandez { 12519be88e75SGabriel Fernandez uint32_t divm = vco->div_mn[PLL_CFG_M]; 12529be88e75SGabriel Fernandez uint32_t divn = vco->div_mn[PLL_CFG_N]; 12539be88e75SGabriel Fernandez unsigned long prate = 0UL; 12549be88e75SGabriel Fernandez unsigned long refclk = 0UL; 12559be88e75SGabriel Fernandez 12569be88e75SGabriel Fernandez prate = _clk_stm32_get_parent_rate(priv, pll->clk_id); 12579be88e75SGabriel Fernandez refclk = prate / (divm + 1U); 12589be88e75SGabriel Fernandez 12599be88e75SGabriel Fernandez if ((refclk < (stm32mp1_pll[pll->plltype].refclk_min * 1000000U)) || 12609be88e75SGabriel Fernandez (refclk > (stm32mp1_pll[pll->plltype].refclk_max * 1000000U))) { 12619be88e75SGabriel Fernandez return -EINVAL; 12629be88e75SGabriel Fernandez } 12639be88e75SGabriel Fernandez 12649be88e75SGabriel Fernandez *value = 0; 12659be88e75SGabriel Fernandez 12669be88e75SGabriel Fernandez if ((pll->plltype == PLL_800) && (refclk >= 8000000U)) { 12679be88e75SGabriel Fernandez *value = 1U << RCC_PLLNCFGR1_IFRGE_SHIFT; 12689be88e75SGabriel Fernandez } 12699be88e75SGabriel Fernandez 12709be88e75SGabriel Fernandez *value |= (divn << RCC_PLLNCFGR1_DIVN_SHIFT) & RCC_PLLNCFGR1_DIVN_MASK; 12719be88e75SGabriel Fernandez *value |= (divm << RCC_PLLNCFGR1_DIVM_SHIFT) & RCC_PLLNCFGR1_DIVM_MASK; 12729be88e75SGabriel Fernandez 12739be88e75SGabriel Fernandez return 0; 12749be88e75SGabriel Fernandez } 12759be88e75SGabriel Fernandez 12769be88e75SGabriel Fernandez static uint32_t clk_stm32_pll_compute_cfgr2(struct stm32_pll_output *out) 12779be88e75SGabriel Fernandez { 12789be88e75SGabriel Fernandez uint32_t value = 0; 12799be88e75SGabriel Fernandez 12809be88e75SGabriel Fernandez value |= (out->output[PLL_CFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) & RCC_PLLNCFGR2_DIVP_MASK; 12819be88e75SGabriel Fernandez value |= (out->output[PLL_CFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) & RCC_PLLNCFGR2_DIVQ_MASK; 12829be88e75SGabriel Fernandez value |= (out->output[PLL_CFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) & RCC_PLLNCFGR2_DIVR_MASK; 12839be88e75SGabriel Fernandez 12849be88e75SGabriel Fernandez return value; 12859be88e75SGabriel Fernandez } 12869be88e75SGabriel Fernandez 12879be88e75SGabriel Fernandez static void clk_stm32_pll_config_vco(struct stm32_clk_priv *priv, 12889be88e75SGabriel Fernandez const struct stm32_clk_pll *pll, 12899be88e75SGabriel Fernandez struct stm32_pll_vco *vco) 12909be88e75SGabriel Fernandez { 12919be88e75SGabriel Fernandez uintptr_t pll_base = priv->base + pll->reg_pllxcr; 12929be88e75SGabriel Fernandez uint32_t value = 0; 12939be88e75SGabriel Fernandez 12949be88e75SGabriel Fernandez if (clk_stm32_pll_compute_cfgr1(priv, pll, vco, &value) != 0) { 12959be88e75SGabriel Fernandez ERROR("Invalid Vref clock !\n"); 12969be88e75SGabriel Fernandez panic(); 12979be88e75SGabriel Fernandez } 12989be88e75SGabriel Fernandez 12999be88e75SGabriel Fernandez /* Write N / M / IFREGE fields */ 13009be88e75SGabriel Fernandez mmio_write_32(pll_base + RCC_OFFSET_PLLXCFGR1, value); 13019be88e75SGabriel Fernandez 13029be88e75SGabriel Fernandez /* Fractional configuration */ 13039be88e75SGabriel Fernandez mmio_write_32(pll_base + RCC_OFFSET_PLLXFRACR, 0); 13049be88e75SGabriel Fernandez 13059be88e75SGabriel Fernandez /* Frac must be enabled only once its configuration is loaded */ 13069be88e75SGabriel Fernandez mmio_write_32(pll_base + RCC_OFFSET_PLLXFRACR, vco->frac << RCC_PLLNFRACR_FRACV_SHIFT); 13079be88e75SGabriel Fernandez mmio_setbits_32(pll_base + RCC_OFFSET_PLLXFRACR, RCC_PLLNFRACR_FRACLE); 13089be88e75SGabriel Fernandez } 13099be88e75SGabriel Fernandez 13109be88e75SGabriel Fernandez static void clk_stm32_pll_config_csg(struct stm32_clk_priv *priv, 13119be88e75SGabriel Fernandez const struct stm32_clk_pll *pll, 13129be88e75SGabriel Fernandez struct stm32_pll_vco *vco) 13139be88e75SGabriel Fernandez { 13149be88e75SGabriel Fernandez uintptr_t pll_base = priv->base + pll->reg_pllxcr; 13159be88e75SGabriel Fernandez uint32_t mod_per = 0; 13169be88e75SGabriel Fernandez uint32_t inc_step = 0; 13179be88e75SGabriel Fernandez uint32_t sscg_mode = 0; 13189be88e75SGabriel Fernandez uint32_t value = 0; 13199be88e75SGabriel Fernandez 13209be88e75SGabriel Fernandez if (!vco->csg_enabled) { 13219be88e75SGabriel Fernandez return; 13229be88e75SGabriel Fernandez } 13239be88e75SGabriel Fernandez 13249be88e75SGabriel Fernandez mod_per = vco->csg[PLL_CSG_MOD_PER]; 13259be88e75SGabriel Fernandez inc_step = vco->csg[PLL_CSG_INC_STEP]; 13269be88e75SGabriel Fernandez sscg_mode = vco->csg[PLL_CSG_SSCG_MODE]; 13279be88e75SGabriel Fernandez 13289be88e75SGabriel Fernandez value |= (mod_per << RCC_PLLNCSGR_MOD_PER_SHIFT) & RCC_PLLNCSGR_MOD_PER_MASK; 13299be88e75SGabriel Fernandez value |= (inc_step << RCC_PLLNCSGR_INC_STEP_SHIFT) & RCC_PLLNCSGR_INC_STEP_MASK; 13309be88e75SGabriel Fernandez value |= (sscg_mode << RCC_PLLNCSGR_SSCG_MODE_SHIFT) & RCC_PLLNCSGR_SSCG_MODE_MASK; 13319be88e75SGabriel Fernandez 13329be88e75SGabriel Fernandez mmio_write_32(pll_base + RCC_OFFSET_PLLXCSGR, value); 13339be88e75SGabriel Fernandez mmio_setbits_32(pll_base + RCC_OFFSET_PLLXCR, RCC_PLLNCR_SSCG_CTRL); 13349be88e75SGabriel Fernandez } 13359be88e75SGabriel Fernandez 13369be88e75SGabriel Fernandez static void clk_stm32_pll_config_out(struct stm32_clk_priv *priv, const struct stm32_clk_pll *pll, 13379be88e75SGabriel Fernandez struct stm32_pll_output *out) 13389be88e75SGabriel Fernandez { 13399be88e75SGabriel Fernandez uintptr_t pll_base = priv->base + pll->reg_pllxcr; 13409be88e75SGabriel Fernandez uint32_t value = 0; 13419be88e75SGabriel Fernandez 13429be88e75SGabriel Fernandez value = clk_stm32_pll_compute_cfgr2(out); 13439be88e75SGabriel Fernandez 13449be88e75SGabriel Fernandez mmio_write_32(pll_base + RCC_OFFSET_PLLXCFGR2, value); 13459be88e75SGabriel Fernandez } 13469be88e75SGabriel Fernandez 13479be88e75SGabriel Fernandez static inline struct stm32_pll_dt_cfg *clk_stm32_pll_get_pdata(int pll_idx) 13489be88e75SGabriel Fernandez { 13499be88e75SGabriel Fernandez struct stm32_clk_priv *priv = clk_stm32_get_priv(); 13509be88e75SGabriel Fernandez struct stm32_clk_platdata *pdata = priv->pdata; 13519be88e75SGabriel Fernandez 13529be88e75SGabriel Fernandez return &pdata->pll[pll_idx]; 13539be88e75SGabriel Fernandez } 13549be88e75SGabriel Fernandez 13559be88e75SGabriel Fernandez static bool _clk_stm32_pll_is_enabled(struct stm32_clk_priv *priv, const struct stm32_clk_pll *pll) 13569be88e75SGabriel Fernandez { 13579be88e75SGabriel Fernandez uintptr_t pll_base = priv->base + pll->reg_pllxcr; 13589be88e75SGabriel Fernandez 13599be88e75SGabriel Fernandez return ((mmio_read_32(pll_base) & RCC_PLLNCR_PLLON) != 0U); 13609be88e75SGabriel Fernandez } 13619be88e75SGabriel Fernandez 13629be88e75SGabriel Fernandez static void _clk_stm32_pll_set_on(struct stm32_clk_priv *priv, const struct stm32_clk_pll *pll) 13639be88e75SGabriel Fernandez { 13649be88e75SGabriel Fernandez uintptr_t pll_base = priv->base + pll->reg_pllxcr; 13659be88e75SGabriel Fernandez 13669be88e75SGabriel Fernandez /* Preserve RCC_PLLNCR_SSCG_CTRL value */ 13679be88e75SGabriel Fernandez mmio_clrsetbits_32(pll_base, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN, 13689be88e75SGabriel Fernandez RCC_PLLNCR_PLLON); 13699be88e75SGabriel Fernandez } 13709be88e75SGabriel Fernandez 13719be88e75SGabriel Fernandez static void _clk_stm32_pll_set_off(struct stm32_clk_priv *priv, const struct stm32_clk_pll *pll) 13729be88e75SGabriel Fernandez { 13739be88e75SGabriel Fernandez uintptr_t pll_base = priv->base + pll->reg_pllxcr; 13749be88e75SGabriel Fernandez 13759be88e75SGabriel Fernandez /* Stop all output */ 13769be88e75SGabriel Fernandez mmio_clrbits_32(pll_base, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN); 13779be88e75SGabriel Fernandez 13789be88e75SGabriel Fernandez /* Stop PLL */ 13799be88e75SGabriel Fernandez mmio_clrbits_32(pll_base, RCC_PLLNCR_PLLON); 13809be88e75SGabriel Fernandez } 13819be88e75SGabriel Fernandez 13829be88e75SGabriel Fernandez static int _clk_stm32_pll_wait_ready_on(struct stm32_clk_priv *priv, 13839be88e75SGabriel Fernandez const struct stm32_clk_pll *pll) 13849be88e75SGabriel Fernandez { 13859be88e75SGabriel Fernandez uintptr_t pll_base = priv->base + pll->reg_pllxcr; 13869be88e75SGabriel Fernandez uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT); 13879be88e75SGabriel Fernandez 13889be88e75SGabriel Fernandez /* Wait PLL lock */ 13899be88e75SGabriel Fernandez while ((mmio_read_32(pll_base) & RCC_PLLNCR_PLLRDY) == 0U) { 13909be88e75SGabriel Fernandez if (timeout_elapsed(timeout)) { 13919be88e75SGabriel Fernandez ERROR("%d clock start failed @ 0x%x: 0x%x\n", 13929be88e75SGabriel Fernandez pll->clk_id, pll->reg_pllxcr, mmio_read_32(pll_base)); 13939be88e75SGabriel Fernandez return -EINVAL; 13949be88e75SGabriel Fernandez } 13959be88e75SGabriel Fernandez } 13969be88e75SGabriel Fernandez 13979be88e75SGabriel Fernandez return 0; 13989be88e75SGabriel Fernandez } 13999be88e75SGabriel Fernandez 14009be88e75SGabriel Fernandez static int _clk_stm32_pll_wait_ready_off(struct stm32_clk_priv *priv, 14019be88e75SGabriel Fernandez const struct stm32_clk_pll *pll) 14029be88e75SGabriel Fernandez { 14039be88e75SGabriel Fernandez uintptr_t pll_base = priv->base + pll->reg_pllxcr; 14049be88e75SGabriel Fernandez uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT); 14059be88e75SGabriel Fernandez 14069be88e75SGabriel Fernandez /* Wait PLL lock */ 14079be88e75SGabriel Fernandez while ((mmio_read_32(pll_base) & RCC_PLLNCR_PLLRDY) != 0U) { 14089be88e75SGabriel Fernandez if (timeout_elapsed(timeout)) { 14099be88e75SGabriel Fernandez ERROR("%d clock stop failed @ 0x%x: 0x%x\n", 14109be88e75SGabriel Fernandez pll->clk_id, pll->reg_pllxcr, mmio_read_32(pll_base)); 14119be88e75SGabriel Fernandez return -EINVAL; 14129be88e75SGabriel Fernandez } 14139be88e75SGabriel Fernandez } 14149be88e75SGabriel Fernandez 14159be88e75SGabriel Fernandez return 0; 14169be88e75SGabriel Fernandez } 14179be88e75SGabriel Fernandez 14189be88e75SGabriel Fernandez static int _clk_stm32_pll_enable(struct stm32_clk_priv *priv, const struct stm32_clk_pll *pll) 14199be88e75SGabriel Fernandez { 14209be88e75SGabriel Fernandez if (_clk_stm32_pll_is_enabled(priv, pll)) { 14219be88e75SGabriel Fernandez return 0; 14229be88e75SGabriel Fernandez } 14239be88e75SGabriel Fernandez 14249be88e75SGabriel Fernandez /* Preserve RCC_PLLNCR_SSCG_CTRL value */ 14259be88e75SGabriel Fernandez _clk_stm32_pll_set_on(priv, pll); 14269be88e75SGabriel Fernandez 14279be88e75SGabriel Fernandez /* Wait PLL lock */ 14289be88e75SGabriel Fernandez return _clk_stm32_pll_wait_ready_on(priv, pll); 14299be88e75SGabriel Fernandez } 14309be88e75SGabriel Fernandez 14319be88e75SGabriel Fernandez static void _clk_stm32_pll_disable(struct stm32_clk_priv *priv, const struct stm32_clk_pll *pll) 14329be88e75SGabriel Fernandez { 14339be88e75SGabriel Fernandez if (!_clk_stm32_pll_is_enabled(priv, pll)) { 14349be88e75SGabriel Fernandez return; 14359be88e75SGabriel Fernandez } 14369be88e75SGabriel Fernandez 14379be88e75SGabriel Fernandez /* Stop all outputs and the PLL */ 14389be88e75SGabriel Fernandez _clk_stm32_pll_set_off(priv, pll); 14399be88e75SGabriel Fernandez 14409be88e75SGabriel Fernandez /* Wait PLL stopped */ 14419be88e75SGabriel Fernandez _clk_stm32_pll_wait_ready_off(priv, pll); 14429be88e75SGabriel Fernandez } 14439be88e75SGabriel Fernandez 14449be88e75SGabriel Fernandez static int _clk_stm32_pll_init(struct stm32_clk_priv *priv, int pll_idx, 14459be88e75SGabriel Fernandez struct stm32_pll_dt_cfg *pll_conf) 14469be88e75SGabriel Fernandez { 14479be88e75SGabriel Fernandez const struct stm32_clk_pll *pll = clk_st32_pll_data(pll_idx); 14489be88e75SGabriel Fernandez uintptr_t pll_base = priv->base + pll->reg_pllxcr; 14499be88e75SGabriel Fernandez int ret = 0; 14509be88e75SGabriel Fernandez 14519be88e75SGabriel Fernandez /* Configure PLLs source */ 14529be88e75SGabriel Fernandez ret = stm32_clk_configure_mux(priv, pll_conf->vco.src); 1453c3ae7da0SYann Gautier if (ret != 0) { 14549be88e75SGabriel Fernandez return ret; 14559be88e75SGabriel Fernandez } 14569be88e75SGabriel Fernandez 14579be88e75SGabriel Fernandez #if STM32MP_USB_PROGRAMMER 14589be88e75SGabriel Fernandez if ((pll_idx == _PLL4) && pll4_bootrom) { 14599be88e75SGabriel Fernandez clk_stm32_pll_config_out(priv, pll, &pll_conf->output); 14609be88e75SGabriel Fernandez 14619be88e75SGabriel Fernandez mmio_setbits_32(pll_base, 14629be88e75SGabriel Fernandez RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN); 14639be88e75SGabriel Fernandez 14649be88e75SGabriel Fernandez return 0; 14659be88e75SGabriel Fernandez } 14669be88e75SGabriel Fernandez #endif 14679be88e75SGabriel Fernandez /* Stop the PLL before */ 14689be88e75SGabriel Fernandez _clk_stm32_pll_disable(priv, pll); 14699be88e75SGabriel Fernandez 14709be88e75SGabriel Fernandez clk_stm32_pll_config_vco(priv, pll, &pll_conf->vco); 14719be88e75SGabriel Fernandez clk_stm32_pll_config_out(priv, pll, &pll_conf->output); 14729be88e75SGabriel Fernandez clk_stm32_pll_config_csg(priv, pll, &pll_conf->vco); 14739be88e75SGabriel Fernandez 14749be88e75SGabriel Fernandez ret = _clk_stm32_pll_enable(priv, pll); 14759be88e75SGabriel Fernandez if (ret != 0) { 14769be88e75SGabriel Fernandez return ret; 14779be88e75SGabriel Fernandez } 14789be88e75SGabriel Fernandez 14799be88e75SGabriel Fernandez mmio_setbits_32(pll_base, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN); 14809be88e75SGabriel Fernandez 14819be88e75SGabriel Fernandez return 0; 14829be88e75SGabriel Fernandez } 14839be88e75SGabriel Fernandez 14849be88e75SGabriel Fernandez static int clk_stm32_pll_init(struct stm32_clk_priv *priv, int pll_idx) 14859be88e75SGabriel Fernandez { 14869be88e75SGabriel Fernandez struct stm32_pll_dt_cfg *pll_conf = clk_stm32_pll_get_pdata(pll_idx); 14879be88e75SGabriel Fernandez 1488c3ae7da0SYann Gautier if (pll_conf->vco.status != 0U) { 14899be88e75SGabriel Fernandez return _clk_stm32_pll_init(priv, pll_idx, pll_conf); 14909be88e75SGabriel Fernandez } 14919be88e75SGabriel Fernandez 14929be88e75SGabriel Fernandez return 0; 14939be88e75SGabriel Fernandez } 14949be88e75SGabriel Fernandez 14959be88e75SGabriel Fernandez static int stm32_clk_pll_configure(struct stm32_clk_priv *priv) 14969be88e75SGabriel Fernandez { 14979be88e75SGabriel Fernandez int err = 0; 14989be88e75SGabriel Fernandez 14999be88e75SGabriel Fernandez err = clk_stm32_pll_init(priv, _PLL1); 1500c3ae7da0SYann Gautier if (err != 0) { 15019be88e75SGabriel Fernandez return err; 15029be88e75SGabriel Fernandez } 15039be88e75SGabriel Fernandez 15049be88e75SGabriel Fernandez err = clk_stm32_pll_init(priv, _PLL2); 1505c3ae7da0SYann Gautier if (err != 0) { 15069be88e75SGabriel Fernandez return err; 15079be88e75SGabriel Fernandez } 15089be88e75SGabriel Fernandez 15099be88e75SGabriel Fernandez err = clk_stm32_pll_init(priv, _PLL3); 1510c3ae7da0SYann Gautier if (err != 0) { 15119be88e75SGabriel Fernandez return err; 15129be88e75SGabriel Fernandez } 15139be88e75SGabriel Fernandez 15149be88e75SGabriel Fernandez err = clk_stm32_pll_init(priv, _PLL4); 1515c3ae7da0SYann Gautier if (err != 0) { 15169be88e75SGabriel Fernandez return err; 15179be88e75SGabriel Fernandez } 15189be88e75SGabriel Fernandez 15199be88e75SGabriel Fernandez return 0; 15209be88e75SGabriel Fernandez } 15219be88e75SGabriel Fernandez 15229be88e75SGabriel Fernandez static int stm32_clk_oscillators_wait_lse_ready(struct stm32_clk_priv *priv) 15239be88e75SGabriel Fernandez { 15249be88e75SGabriel Fernandez int ret = 0; 15259be88e75SGabriel Fernandez 15269be88e75SGabriel Fernandez if (_clk_stm32_get_rate(priv, _CK_LSE) != 0U) { 15279be88e75SGabriel Fernandez ret = clk_oscillator_wait_ready_on(priv, _CK_LSE); 15289be88e75SGabriel Fernandez } 15299be88e75SGabriel Fernandez 15309be88e75SGabriel Fernandez return ret; 15319be88e75SGabriel Fernandez } 15329be88e75SGabriel Fernandez 15339be88e75SGabriel Fernandez static void stm32_clk_oscillators_enable(struct stm32_clk_priv *priv) 15349be88e75SGabriel Fernandez { 15359be88e75SGabriel Fernandez stm32_enable_oscillator_hse(priv); 15369be88e75SGabriel Fernandez stm32_enable_oscillator_lse(priv); 15379be88e75SGabriel Fernandez _clk_stm32_enable(priv, _CK_LSI); 15389be88e75SGabriel Fernandez _clk_stm32_enable(priv, _CK_CSI); 15399be88e75SGabriel Fernandez } 15409be88e75SGabriel Fernandez 15419be88e75SGabriel Fernandez static int stm32_clk_hsidiv_configure(struct stm32_clk_priv *priv) 15429be88e75SGabriel Fernandez { 15439be88e75SGabriel Fernandez return stm32mp1_hsidiv(_clk_stm32_get_rate(priv, _CK_HSI)); 15449be88e75SGabriel Fernandez } 15459be88e75SGabriel Fernandez 15469be88e75SGabriel Fernandez #if STM32MP_USB_PROGRAMMER 15479be88e75SGabriel Fernandez static bool stm32mp1_clk_is_pll4_used_by_bootrom(struct stm32_clk_priv *priv, int usbphy_p) 15489be88e75SGabriel Fernandez { 15499be88e75SGabriel Fernandez /* Don't initialize PLL4, when used by BOOTROM */ 15509be88e75SGabriel Fernandez if ((stm32mp_get_boot_itf_selected() == 15519be88e75SGabriel Fernandez BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB) && 15529be88e75SGabriel Fernandez (usbphy_p == _PLL4R)) { 15539be88e75SGabriel Fernandez return true; 15549be88e75SGabriel Fernandez } 15559be88e75SGabriel Fernandez 15569be88e75SGabriel Fernandez return false; 15579be88e75SGabriel Fernandez } 15589be88e75SGabriel Fernandez 15599be88e75SGabriel Fernandez static int stm32mp1_clk_check_usb_conflict(struct stm32_clk_priv *priv, int usbphy_p, int usbo_p) 15609be88e75SGabriel Fernandez { 15619be88e75SGabriel Fernandez int _usbo_p; 15629be88e75SGabriel Fernandez int _usbphy_p; 15639be88e75SGabriel Fernandez 15649be88e75SGabriel Fernandez if (!pll4_bootrom) { 15659be88e75SGabriel Fernandez return 0; 15669be88e75SGabriel Fernandez } 15679be88e75SGabriel Fernandez 15689be88e75SGabriel Fernandez _usbo_p = _clk_stm32_get_parent(priv, _USBO_K); 15699be88e75SGabriel Fernandez _usbphy_p = _clk_stm32_get_parent(priv, _USBPHY_K); 15709be88e75SGabriel Fernandez 15719be88e75SGabriel Fernandez if ((_usbo_p != usbo_p) || (_usbphy_p != usbphy_p)) { 15729be88e75SGabriel Fernandez return -FDT_ERR_BADVALUE; 15739be88e75SGabriel Fernandez } 15749be88e75SGabriel Fernandez 15759be88e75SGabriel Fernandez return 0; 15769be88e75SGabriel Fernandez } 15779be88e75SGabriel Fernandez #endif 15789be88e75SGabriel Fernandez 15799be88e75SGabriel Fernandez static struct clk_oscillator_data stm32mp13_osc_data[NB_OSCILLATOR] = { 15809be88e75SGabriel Fernandez OSCILLATOR(OSC_HSI, _CK_HSI, "clk-hsi", GATE_HSI, GATE_HSI_RDY, 15819be88e75SGabriel Fernandez NULL, NULL, NULL), 15829be88e75SGabriel Fernandez 15839be88e75SGabriel Fernandez OSCILLATOR(OSC_LSI, _CK_LSI, "clk-lsi", GATE_LSI, GATE_LSI_RDY, 15849be88e75SGabriel Fernandez NULL, NULL, NULL), 15859be88e75SGabriel Fernandez 15869be88e75SGabriel Fernandez OSCILLATOR(OSC_CSI, _CK_CSI, "clk-csi", GATE_CSI, GATE_CSI_RDY, 15879be88e75SGabriel Fernandez NULL, NULL, NULL), 15889be88e75SGabriel Fernandez 15899be88e75SGabriel Fernandez OSCILLATOR(OSC_LSE, _CK_LSE, "clk-lse", GATE_LSE, GATE_LSE_RDY, 15909be88e75SGabriel Fernandez BYPASS(RCC_BDCR, 1, 3), 15919be88e75SGabriel Fernandez CSS(RCC_BDCR, 8), 15929be88e75SGabriel Fernandez DRIVE(RCC_BDCR, 4, 2, 2)), 15939be88e75SGabriel Fernandez 15949be88e75SGabriel Fernandez OSCILLATOR(OSC_HSE, _CK_HSE, "clk-hse", GATE_HSE, GATE_HSE_RDY, 15959be88e75SGabriel Fernandez BYPASS(RCC_OCENSETR, 10, 7), 15969be88e75SGabriel Fernandez CSS(RCC_OCENSETR, 11), 15979be88e75SGabriel Fernandez NULL), 15989be88e75SGabriel Fernandez 15999be88e75SGabriel Fernandez OSCILLATOR(OSC_I2SCKIN, _I2SCKIN, "i2s_ckin", NO_GATE, NO_GATE, 16009be88e75SGabriel Fernandez NULL, NULL, NULL), 16019be88e75SGabriel Fernandez }; 16029be88e75SGabriel Fernandez 16039be88e75SGabriel Fernandez static const char *clk_stm32_get_oscillator_name(enum stm32_osc id) 16049be88e75SGabriel Fernandez { 16059be88e75SGabriel Fernandez if (id < NB_OSCILLATOR) { 16069be88e75SGabriel Fernandez return stm32mp13_osc_data[id].name; 16079be88e75SGabriel Fernandez } 16089be88e75SGabriel Fernandez 16099be88e75SGabriel Fernandez return NULL; 16109be88e75SGabriel Fernandez } 16119be88e75SGabriel Fernandez 16129be88e75SGabriel Fernandez #define CLK_PLL_CFG(_idx, _clk_id, _type, _reg)\ 16139be88e75SGabriel Fernandez [(_idx)] = {\ 16149be88e75SGabriel Fernandez .clk_id = (_clk_id),\ 16159be88e75SGabriel Fernandez .plltype = (_type),\ 16169be88e75SGabriel Fernandez .reg_pllxcr = (_reg),\ 16179be88e75SGabriel Fernandez } 16189be88e75SGabriel Fernandez 16199be88e75SGabriel Fernandez static const struct stm32_clk_pll stm32_mp13_clk_pll[_PLL_NB] = { 16209be88e75SGabriel Fernandez CLK_PLL_CFG(_PLL1, _CK_PLL1, PLL_2000, RCC_PLL1CR), 16219be88e75SGabriel Fernandez CLK_PLL_CFG(_PLL2, _CK_PLL2, PLL_1600, RCC_PLL2CR), 16229be88e75SGabriel Fernandez CLK_PLL_CFG(_PLL3, _CK_PLL3, PLL_800, RCC_PLL3CR), 16239be88e75SGabriel Fernandez CLK_PLL_CFG(_PLL4, _CK_PLL4, PLL_800, RCC_PLL4CR), 16249be88e75SGabriel Fernandez }; 16259be88e75SGabriel Fernandez 16269be88e75SGabriel Fernandez static const struct stm32_clk_pll *clk_st32_pll_data(unsigned int idx) 16279be88e75SGabriel Fernandez { 16289be88e75SGabriel Fernandez return &stm32_mp13_clk_pll[idx]; 16299be88e75SGabriel Fernandez } 16309be88e75SGabriel Fernandez 16319be88e75SGabriel Fernandez struct stm32_pll_cfg { 16329be88e75SGabriel Fernandez int pll_id; 16339be88e75SGabriel Fernandez }; 16349be88e75SGabriel Fernandez 16359be88e75SGabriel Fernandez static unsigned long clk_stm32_pll_recalc_rate(struct stm32_clk_priv *priv, int id, 16369be88e75SGabriel Fernandez unsigned long prate) 16379be88e75SGabriel Fernandez { 16389be88e75SGabriel Fernandez const struct clk_stm32 *clk = _clk_get(priv, id); 16399be88e75SGabriel Fernandez struct stm32_pll_cfg *pll_cfg = clk->clock_cfg; 16409be88e75SGabriel Fernandez const struct stm32_clk_pll *pll = clk_st32_pll_data(pll_cfg->pll_id); 16419be88e75SGabriel Fernandez uintptr_t pll_base = priv->base + pll->reg_pllxcr; 16429be88e75SGabriel Fernandez uint32_t cfgr1, fracr, divm, divn; 16439be88e75SGabriel Fernandez unsigned long fvco; 16449be88e75SGabriel Fernandez 16459be88e75SGabriel Fernandez cfgr1 = mmio_read_32(pll_base + RCC_OFFSET_PLLXCFGR1); 16469be88e75SGabriel Fernandez fracr = mmio_read_32(pll_base + RCC_OFFSET_PLLXFRACR); 16479be88e75SGabriel Fernandez 16489be88e75SGabriel Fernandez divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT; 16499be88e75SGabriel Fernandez divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK; 16509be88e75SGabriel Fernandez 16519be88e75SGabriel Fernandez /* 16529be88e75SGabriel Fernandez * With FRACV : 16539be88e75SGabriel Fernandez * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1) 16549be88e75SGabriel Fernandez * Without FRACV 16559be88e75SGabriel Fernandez * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1) 16569be88e75SGabriel Fernandez */ 16579be88e75SGabriel Fernandez if ((fracr & RCC_PLLNFRACR_FRACLE) != 0U) { 16589be88e75SGabriel Fernandez uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >> 16599be88e75SGabriel Fernandez RCC_PLLNFRACR_FRACV_SHIFT; 16609be88e75SGabriel Fernandez unsigned long long numerator, denominator; 16619be88e75SGabriel Fernandez 16629be88e75SGabriel Fernandez numerator = (((unsigned long long)divn + 1U) << 13) + fracv; 16639be88e75SGabriel Fernandez numerator = prate * numerator; 16649be88e75SGabriel Fernandez denominator = ((unsigned long long)divm + 1U) << 13; 16659be88e75SGabriel Fernandez fvco = (unsigned long)(numerator / denominator); 16669be88e75SGabriel Fernandez } else { 16679be88e75SGabriel Fernandez fvco = (unsigned long)(prate * (divn + 1U) / (divm + 1U)); 16689be88e75SGabriel Fernandez } 16699be88e75SGabriel Fernandez 16709be88e75SGabriel Fernandez return fvco; 16719be88e75SGabriel Fernandez }; 16729be88e75SGabriel Fernandez 16739be88e75SGabriel Fernandez static bool clk_stm32_pll_is_enabled(struct stm32_clk_priv *priv, int id) 16749be88e75SGabriel Fernandez { 16759be88e75SGabriel Fernandez const struct clk_stm32 *clk = _clk_get(priv, id); 16769be88e75SGabriel Fernandez struct stm32_pll_cfg *pll_cfg = clk->clock_cfg; 16779be88e75SGabriel Fernandez const struct stm32_clk_pll *pll = clk_st32_pll_data(pll_cfg->pll_id); 16789be88e75SGabriel Fernandez 16799be88e75SGabriel Fernandez return _clk_stm32_pll_is_enabled(priv, pll); 16809be88e75SGabriel Fernandez } 16819be88e75SGabriel Fernandez 16829be88e75SGabriel Fernandez static int clk_stm32_pll_enable(struct stm32_clk_priv *priv, int id) 16839be88e75SGabriel Fernandez { 16849be88e75SGabriel Fernandez const struct clk_stm32 *clk = _clk_get(priv, id); 16859be88e75SGabriel Fernandez struct stm32_pll_cfg *pll_cfg = clk->clock_cfg; 16869be88e75SGabriel Fernandez const struct stm32_clk_pll *pll = clk_st32_pll_data(pll_cfg->pll_id); 16879be88e75SGabriel Fernandez 16889be88e75SGabriel Fernandez return _clk_stm32_pll_enable(priv, pll); 16899be88e75SGabriel Fernandez } 16909be88e75SGabriel Fernandez 16919be88e75SGabriel Fernandez static void clk_stm32_pll_disable(struct stm32_clk_priv *priv, int id) 16929be88e75SGabriel Fernandez { 16939be88e75SGabriel Fernandez const struct clk_stm32 *clk = _clk_get(priv, id); 16949be88e75SGabriel Fernandez struct stm32_pll_cfg *pll_cfg = clk->clock_cfg; 16959be88e75SGabriel Fernandez const struct stm32_clk_pll *pll = clk_st32_pll_data(pll_cfg->pll_id); 16969be88e75SGabriel Fernandez 16979be88e75SGabriel Fernandez _clk_stm32_pll_disable(priv, pll); 16989be88e75SGabriel Fernandez } 16999be88e75SGabriel Fernandez 17009be88e75SGabriel Fernandez static const struct stm32_clk_ops clk_stm32_pll_ops = { 17019be88e75SGabriel Fernandez .recalc_rate = clk_stm32_pll_recalc_rate, 17029be88e75SGabriel Fernandez .enable = clk_stm32_pll_enable, 17039be88e75SGabriel Fernandez .disable = clk_stm32_pll_disable, 17049be88e75SGabriel Fernandez .is_enabled = clk_stm32_pll_is_enabled, 17059be88e75SGabriel Fernandez }; 17069be88e75SGabriel Fernandez 17079be88e75SGabriel Fernandez #define CLK_PLL(idx, _idx, _parent, _gate, _pll_id, _flags)[idx] = {\ 17089be88e75SGabriel Fernandez .binding = _idx,\ 17099be88e75SGabriel Fernandez .parent = _parent,\ 17109be88e75SGabriel Fernandez .flags = (_flags),\ 17119be88e75SGabriel Fernandez .clock_cfg = &(struct stm32_pll_cfg) {\ 17129be88e75SGabriel Fernandez .pll_id = _pll_id,\ 17139be88e75SGabriel Fernandez },\ 17149be88e75SGabriel Fernandez .ops = &clk_stm32_pll_ops,\ 17159be88e75SGabriel Fernandez } 17169be88e75SGabriel Fernandez 17179be88e75SGabriel Fernandez struct clk_stm32_composite_cfg { 17189be88e75SGabriel Fernandez int gate_id; 17199be88e75SGabriel Fernandez int div_id; 17209be88e75SGabriel Fernandez }; 17219be88e75SGabriel Fernandez 17229be88e75SGabriel Fernandez static unsigned long clk_stm32_composite_recalc_rate(struct stm32_clk_priv *priv, 17239be88e75SGabriel Fernandez int idx, unsigned long prate) 17249be88e75SGabriel Fernandez { 17259be88e75SGabriel Fernandez const struct clk_stm32 *clk = _clk_get(priv, idx); 17269be88e75SGabriel Fernandez struct clk_stm32_composite_cfg *composite_cfg = clk->clock_cfg; 17279be88e75SGabriel Fernandez 17289be88e75SGabriel Fernandez return _clk_stm32_divider_recalc(priv, composite_cfg->div_id, prate); 17299be88e75SGabriel Fernandez }; 17309be88e75SGabriel Fernandez 17319be88e75SGabriel Fernandez static bool clk_stm32_composite_gate_is_enabled(struct stm32_clk_priv *priv, int idx) 17329be88e75SGabriel Fernandez { 17339be88e75SGabriel Fernandez const struct clk_stm32 *clk = _clk_get(priv, idx); 17349be88e75SGabriel Fernandez struct clk_stm32_composite_cfg *composite_cfg = clk->clock_cfg; 17359be88e75SGabriel Fernandez 17369be88e75SGabriel Fernandez return _clk_stm32_gate_is_enabled(priv, composite_cfg->gate_id); 17379be88e75SGabriel Fernandez } 17389be88e75SGabriel Fernandez 17399be88e75SGabriel Fernandez static int clk_stm32_composite_gate_enable(struct stm32_clk_priv *priv, int idx) 17409be88e75SGabriel Fernandez { 17419be88e75SGabriel Fernandez const struct clk_stm32 *clk = _clk_get(priv, idx); 17429be88e75SGabriel Fernandez struct clk_stm32_composite_cfg *composite_cfg = clk->clock_cfg; 17439be88e75SGabriel Fernandez 17449be88e75SGabriel Fernandez return _clk_stm32_gate_enable(priv, composite_cfg->gate_id); 17459be88e75SGabriel Fernandez } 17469be88e75SGabriel Fernandez 17479be88e75SGabriel Fernandez static void clk_stm32_composite_gate_disable(struct stm32_clk_priv *priv, int idx) 17489be88e75SGabriel Fernandez { 17499be88e75SGabriel Fernandez const struct clk_stm32 *clk = _clk_get(priv, idx); 17509be88e75SGabriel Fernandez struct clk_stm32_composite_cfg *composite_cfg = clk->clock_cfg; 17519be88e75SGabriel Fernandez 17529be88e75SGabriel Fernandez _clk_stm32_gate_disable(priv, composite_cfg->gate_id); 17539be88e75SGabriel Fernandez } 17549be88e75SGabriel Fernandez 17559be88e75SGabriel Fernandez static const struct stm32_clk_ops clk_stm32_composite_ops = { 17569be88e75SGabriel Fernandez .recalc_rate = clk_stm32_composite_recalc_rate, 17579be88e75SGabriel Fernandez .is_enabled = clk_stm32_composite_gate_is_enabled, 17589be88e75SGabriel Fernandez .enable = clk_stm32_composite_gate_enable, 17599be88e75SGabriel Fernandez .disable = clk_stm32_composite_gate_disable, 17609be88e75SGabriel Fernandez }; 17619be88e75SGabriel Fernandez 17629be88e75SGabriel Fernandez #define STM32_COMPOSITE(idx, _binding, _parent, _flags, _gate_id,\ 17639be88e75SGabriel Fernandez _div_id)[idx] = {\ 17649be88e75SGabriel Fernandez .binding = (_binding),\ 17659be88e75SGabriel Fernandez .parent = (_parent),\ 17669be88e75SGabriel Fernandez .flags = (_flags),\ 17679be88e75SGabriel Fernandez .clock_cfg = &(struct clk_stm32_composite_cfg) {\ 17689be88e75SGabriel Fernandez .gate_id = (_gate_id),\ 17699be88e75SGabriel Fernandez .div_id = (_div_id),\ 17709be88e75SGabriel Fernandez },\ 17719be88e75SGabriel Fernandez .ops = &clk_stm32_composite_ops,\ 17729be88e75SGabriel Fernandez } 17739be88e75SGabriel Fernandez 17749be88e75SGabriel Fernandez static const struct clk_stm32 stm32mp13_clk[CK_LAST] = { 17759be88e75SGabriel Fernandez /* ROOT CLOCKS */ 17769be88e75SGabriel Fernandez CLK_FIXED_RATE(_CK_OFF, _NO_ID, 0), 17779be88e75SGabriel Fernandez CLK_OSC(_CK_HSE, CK_HSE, CLK_IS_ROOT, OSC_HSE), 17789be88e75SGabriel Fernandez CLK_OSC(_CK_HSI, CK_HSI, CLK_IS_ROOT, OSC_HSI), 17799be88e75SGabriel Fernandez CLK_OSC(_CK_CSI, CK_CSI, CLK_IS_ROOT, OSC_CSI), 17809be88e75SGabriel Fernandez CLK_OSC(_CK_LSI, CK_LSI, CLK_IS_ROOT, OSC_LSI), 17819be88e75SGabriel Fernandez CLK_OSC(_CK_LSE, CK_LSE, CLK_IS_ROOT, OSC_LSE), 17829be88e75SGabriel Fernandez 17839be88e75SGabriel Fernandez CLK_OSC_FIXED(_I2SCKIN, _NO_ID, CLK_IS_ROOT, OSC_I2SCKIN), 17849be88e75SGabriel Fernandez 17859be88e75SGabriel Fernandez CLK_FIXED_RATE(_USB_PHY_48, _NO_ID, USB_PHY_48_MHZ), 17869be88e75SGabriel Fernandez 17879be88e75SGabriel Fernandez STM32_DIV(_HSE_DIV, _NO_ID, _CK_HSE, 0, DIV_RTC), 17889be88e75SGabriel Fernandez 17899be88e75SGabriel Fernandez FIXED_FACTOR(_HSE_DIV2, CK_HSE_DIV2, _CK_HSE, 1, 2), 17909be88e75SGabriel Fernandez FIXED_FACTOR(_CSI_DIV122, _NO_ID, _CK_CSI, 1, 122), 17919be88e75SGabriel Fernandez 17929be88e75SGabriel Fernandez CLK_PLL(_CK_PLL1, PLL1, MUX(MUX_PLL12), GATE_PLL1, _PLL1, 0), 17939be88e75SGabriel Fernandez CLK_PLL(_CK_PLL2, PLL2, MUX(MUX_PLL12), GATE_PLL2, _PLL2, 0), 17949be88e75SGabriel Fernandez CLK_PLL(_CK_PLL3, PLL3, MUX(MUX_PLL3), GATE_PLL3, _PLL3, 0), 17959be88e75SGabriel Fernandez CLK_PLL(_CK_PLL4, PLL4, MUX(MUX_PLL4), GATE_PLL4, _PLL4, 0), 17969be88e75SGabriel Fernandez 17979be88e75SGabriel Fernandez STM32_COMPOSITE(_PLL1P, PLL1_P, _CK_PLL1, CLK_IS_CRITICAL, GATE_PLL1_DIVP, DIV_PLL1DIVP), 17989be88e75SGabriel Fernandez STM32_DIV(_PLL1P_DIV, _NO_ID, _CK_PLL1, 0, DIV_MPU), 17999be88e75SGabriel Fernandez 18009be88e75SGabriel Fernandez STM32_COMPOSITE(_PLL2P, PLL2_P, _CK_PLL2, CLK_IS_CRITICAL, GATE_PLL2_DIVP, DIV_PLL2DIVP), 18019be88e75SGabriel Fernandez STM32_COMPOSITE(_PLL2Q, PLL2_Q, _CK_PLL2, 0, GATE_PLL2_DIVQ, DIV_PLL2DIVQ), 18029be88e75SGabriel Fernandez STM32_COMPOSITE(_PLL2R, PLL2_R, _CK_PLL2, CLK_IS_CRITICAL, GATE_PLL2_DIVR, DIV_PLL2DIVR), 18039be88e75SGabriel Fernandez 18049be88e75SGabriel Fernandez STM32_COMPOSITE(_PLL3P, PLL3_P, _CK_PLL3, 0, GATE_PLL3_DIVP, DIV_PLL3DIVP), 18059be88e75SGabriel Fernandez STM32_COMPOSITE(_PLL3Q, PLL3_Q, _CK_PLL3, 0, GATE_PLL3_DIVQ, DIV_PLL3DIVQ), 18069be88e75SGabriel Fernandez STM32_COMPOSITE(_PLL3R, PLL3_R, _CK_PLL3, 0, GATE_PLL3_DIVR, DIV_PLL3DIVR), 18079be88e75SGabriel Fernandez 18089be88e75SGabriel Fernandez STM32_COMPOSITE(_PLL4P, PLL4_P, _CK_PLL4, 0, GATE_PLL4_DIVP, DIV_PLL4DIVP), 18099be88e75SGabriel Fernandez STM32_COMPOSITE(_PLL4Q, PLL4_Q, _CK_PLL4, 0, GATE_PLL4_DIVQ, DIV_PLL4DIVQ), 18109be88e75SGabriel Fernandez STM32_COMPOSITE(_PLL4R, PLL4_R, _CK_PLL4, 0, GATE_PLL4_DIVR, DIV_PLL4DIVR), 18119be88e75SGabriel Fernandez 18129be88e75SGabriel Fernandez STM32_MUX(_CKMPU, CK_MPU, MUX_MPU, 0), 18139be88e75SGabriel Fernandez STM32_DIV(_CKAXI, CK_AXI, MUX(MUX_AXI), 0, DIV_AXI), 18149be88e75SGabriel Fernandez STM32_DIV(_CKMLAHB, CK_MLAHB, MUX(MUX_MLAHB), CLK_IS_CRITICAL, DIV_MLAHB), 18159be88e75SGabriel Fernandez STM32_MUX(_CKPER, CK_PER, MUX(MUX_CKPER), 0), 18169be88e75SGabriel Fernandez 18179be88e75SGabriel Fernandez STM32_DIV(_PCLK1, PCLK1, _CKMLAHB, 0, DIV_APB1), 18189be88e75SGabriel Fernandez STM32_DIV(_PCLK2, PCLK2, _CKMLAHB, 0, DIV_APB2), 18199be88e75SGabriel Fernandez STM32_DIV(_PCLK3, PCLK3, _CKMLAHB, 0, DIV_APB3), 18209be88e75SGabriel Fernandez STM32_DIV(_PCLK4, PCLK4, _CKAXI, 0, DIV_APB4), 18219be88e75SGabriel Fernandez STM32_DIV(_PCLK5, PCLK5, _CKAXI, 0, DIV_APB5), 18229be88e75SGabriel Fernandez STM32_DIV(_PCLK6, PCLK6, _CKMLAHB, 0, DIV_APB6), 18239be88e75SGabriel Fernandez 18249be88e75SGabriel Fernandez CK_TIMER(_CKTIMG1, CK_TIMG1, _PCLK1, 0, RCC_APB1DIVR, RCC_TIMG1PRER), 18259be88e75SGabriel Fernandez CK_TIMER(_CKTIMG2, CK_TIMG2, _PCLK2, 0, RCC_APB2DIVR, RCC_TIMG2PRER), 18269be88e75SGabriel Fernandez CK_TIMER(_CKTIMG3, CK_TIMG3, _PCLK6, 0, RCC_APB6DIVR, RCC_TIMG3PRER), 18279be88e75SGabriel Fernandez 18289be88e75SGabriel Fernandez /* END ROOT CLOCKS */ 18299be88e75SGabriel Fernandez 18309be88e75SGabriel Fernandez STM32_GATE(_DDRC1, DDRC1, _CKAXI, CLK_IS_CRITICAL, GATE_DDRC1), 18319be88e75SGabriel Fernandez STM32_GATE(_DDRC1LP, DDRC1LP, _CKAXI, CLK_IS_CRITICAL, GATE_DDRC1LP), 18329be88e75SGabriel Fernandez STM32_GATE(_DDRPHYC, DDRPHYC, _PLL2R, CLK_IS_CRITICAL, GATE_DDRPHYC), 18339be88e75SGabriel Fernandez STM32_GATE(_DDRPHYCLP, DDRPHYCLP, _PLL2R, CLK_IS_CRITICAL, GATE_DDRPHYCLP), 18349be88e75SGabriel Fernandez STM32_GATE(_DDRCAPB, DDRCAPB, _PCLK4, CLK_IS_CRITICAL, GATE_DDRCAPB), 18359be88e75SGabriel Fernandez STM32_GATE(_DDRCAPBLP, DDRCAPBLP, _PCLK4, CLK_IS_CRITICAL, GATE_DDRCAPBLP), 18369be88e75SGabriel Fernandez STM32_GATE(_AXIDCG, AXIDCG, _CKAXI, CLK_IS_CRITICAL, GATE_AXIDCG), 18379be88e75SGabriel Fernandez STM32_GATE(_DDRPHYCAPB, DDRPHYCAPB, _PCLK4, CLK_IS_CRITICAL, GATE_DDRPHYCAPB), 18389be88e75SGabriel Fernandez STM32_GATE(_DDRPHYCAPBLP, DDRPHYCAPBLP, _PCLK4, CLK_IS_CRITICAL, GATE_DDRPHYCAPBLP), 18399be88e75SGabriel Fernandez 18409be88e75SGabriel Fernandez STM32_GATE(_SYSCFG, SYSCFG, _PCLK3, 0, GATE_SYSCFG), 18419be88e75SGabriel Fernandez STM32_GATE(_DDRPERFM, DDRPERFM, _PCLK4, 0, GATE_DDRPERFM), 18429be88e75SGabriel Fernandez STM32_GATE(_IWDG2APB, IWDG2, _PCLK4, 0, GATE_IWDG2APB), 18439be88e75SGabriel Fernandez STM32_GATE(_USBPHY_K, USBPHY_K, MUX(MUX_USBPHY), 0, GATE_USBPHY), 18449be88e75SGabriel Fernandez STM32_GATE(_USBO_K, USBO_K, MUX(MUX_USBO), 0, GATE_USBO), 18459be88e75SGabriel Fernandez 18469be88e75SGabriel Fernandez STM32_GATE(_RTCAPB, RTCAPB, _PCLK5, CLK_IS_CRITICAL, GATE_RTCAPB), 18479be88e75SGabriel Fernandez STM32_GATE(_TZC, TZC, _PCLK5, CLK_IS_CRITICAL, GATE_TZC), 18489be88e75SGabriel Fernandez STM32_GATE(_ETZPC, TZPC, _PCLK5, CLK_IS_CRITICAL, GATE_ETZPC), 18499be88e75SGabriel Fernandez STM32_GATE(_IWDG1APB, IWDG1, _PCLK5, 0, GATE_IWDG1APB), 18509be88e75SGabriel Fernandez STM32_GATE(_BSEC, BSEC, _PCLK5, CLK_IS_CRITICAL, GATE_BSEC), 18519be88e75SGabriel Fernandez STM32_GATE(_STGENC, STGEN_K, MUX(MUX_STGEN), CLK_IS_CRITICAL, GATE_STGENC), 18529be88e75SGabriel Fernandez 18539be88e75SGabriel Fernandez STM32_GATE(_USART1_K, USART1_K, MUX(MUX_UART1), 0, GATE_USART1), 18549be88e75SGabriel Fernandez STM32_GATE(_USART2_K, USART2_K, MUX(MUX_UART2), 0, GATE_USART2), 18559be88e75SGabriel Fernandez STM32_GATE(_I2C3_K, I2C3_K, MUX(MUX_I2C3), 0, GATE_I2C3), 18569be88e75SGabriel Fernandez STM32_GATE(_I2C4_K, I2C4_K, MUX(MUX_I2C4), 0, GATE_I2C4), 18579be88e75SGabriel Fernandez STM32_GATE(_I2C5_K, I2C5_K, MUX(MUX_I2C5), 0, GATE_I2C5), 18589be88e75SGabriel Fernandez STM32_GATE(_TIM12, TIM12_K, _CKTIMG3, 0, GATE_TIM12), 18599be88e75SGabriel Fernandez STM32_GATE(_TIM15, TIM15_K, _CKTIMG3, 0, GATE_TIM15), 18609be88e75SGabriel Fernandez 18619be88e75SGabriel Fernandez STM32_GATE(_RTCCK, RTC, MUX(MUX_RTC), 0, GATE_RTCCK), 18629be88e75SGabriel Fernandez 18639be88e75SGabriel Fernandez STM32_GATE(_GPIOA, GPIOA, _CKMLAHB, 0, GATE_GPIOA), 18649be88e75SGabriel Fernandez STM32_GATE(_GPIOB, GPIOB, _CKMLAHB, 0, GATE_GPIOB), 18659be88e75SGabriel Fernandez STM32_GATE(_GPIOC, GPIOC, _CKMLAHB, 0, GATE_GPIOC), 18669be88e75SGabriel Fernandez STM32_GATE(_GPIOD, GPIOD, _CKMLAHB, 0, GATE_GPIOD), 18679be88e75SGabriel Fernandez STM32_GATE(_GPIOE, GPIOE, _CKMLAHB, 0, GATE_GPIOE), 18689be88e75SGabriel Fernandez STM32_GATE(_GPIOF, GPIOF, _CKMLAHB, 0, GATE_GPIOF), 18699be88e75SGabriel Fernandez STM32_GATE(_GPIOG, GPIOG, _CKMLAHB, 0, GATE_GPIOG), 18709be88e75SGabriel Fernandez STM32_GATE(_GPIOH, GPIOH, _CKMLAHB, 0, GATE_GPIOH), 18719be88e75SGabriel Fernandez STM32_GATE(_GPIOI, GPIOI, _CKMLAHB, 0, GATE_GPIOI), 18729be88e75SGabriel Fernandez 18739be88e75SGabriel Fernandez STM32_GATE(_PKA, PKA, _CKAXI, 0, GATE_PKA), 18749be88e75SGabriel Fernandez STM32_GATE(_SAES_K, SAES_K, MUX(MUX_SAES), 0, GATE_SAES), 18759be88e75SGabriel Fernandez STM32_GATE(_CRYP1, CRYP1, _PCLK5, 0, GATE_CRYP1), 18769be88e75SGabriel Fernandez STM32_GATE(_HASH1, HASH1, _PCLK5, 0, GATE_HASH1), 18779be88e75SGabriel Fernandez 18789be88e75SGabriel Fernandez STM32_GATE(_RNG1_K, RNG1_K, MUX(MUX_RNG1), 0, GATE_RNG1), 18799be88e75SGabriel Fernandez STM32_GATE(_BKPSRAM, BKPSRAM, _PCLK5, CLK_IS_CRITICAL, GATE_BKPSRAM), 18809be88e75SGabriel Fernandez 18819be88e75SGabriel Fernandez STM32_GATE(_SDMMC1_K, SDMMC1_K, MUX(MUX_SDMMC1), 0, GATE_SDMMC1), 18829be88e75SGabriel Fernandez STM32_GATE(_SDMMC2_K, SDMMC2_K, MUX(MUX_SDMMC2), 0, GATE_SDMMC2), 18839be88e75SGabriel Fernandez STM32_GATE(_DBGCK, CK_DBG, _CKAXI, 0, GATE_DBGCK), 18849be88e75SGabriel Fernandez 18859be88e75SGabriel Fernandez /* TODO: CHECK CLOCK FOR BL2/BL32 AND IF ONLY FOR TEST OR NOT */ 18869be88e75SGabriel Fernandez STM32_GATE(_USART3_K, USART3_K, MUX(MUX_UART35), 0, GATE_USART3), 18879be88e75SGabriel Fernandez STM32_GATE(_UART4_K, UART4_K, MUX(MUX_UART4), 0, GATE_UART4), 18889be88e75SGabriel Fernandez STM32_GATE(_UART5_K, UART5_K, MUX(MUX_UART35), 0, GATE_UART5), 18899be88e75SGabriel Fernandez STM32_GATE(_UART7_K, UART7_K, MUX(MUX_UART78), 0, GATE_UART7), 18909be88e75SGabriel Fernandez STM32_GATE(_UART8_K, UART8_K, MUX(MUX_UART78), 0, GATE_UART8), 18919be88e75SGabriel Fernandez STM32_GATE(_USART6_K, USART6_K, MUX(MUX_UART6), 0, GATE_USART6), 18929be88e75SGabriel Fernandez STM32_GATE(_MCE, MCE, _CKAXI, CLK_IS_CRITICAL, GATE_MCE), 18939be88e75SGabriel Fernandez STM32_GATE(_FMC_K, FMC_K, MUX(MUX_FMC), 0, GATE_FMC), 18949be88e75SGabriel Fernandez STM32_GATE(_QSPI_K, QSPI_K, MUX(MUX_QSPI), 0, GATE_QSPI), 18959be88e75SGabriel Fernandez 18969be88e75SGabriel Fernandez STM32_COMPOSITE(_MCO1_K, CK_MCO1, MUX(MUX_MCO1), 0, GATE_MCO1, DIV_MCO1), 18979be88e75SGabriel Fernandez STM32_COMPOSITE(_MCO2_K, CK_MCO2, MUX(MUX_MCO2), 0, GATE_MCO2, DIV_MCO2), 18989be88e75SGabriel Fernandez STM32_COMPOSITE(_TRACECK, CK_TRACE, _CKAXI, 0, GATE_TRACECK, DIV_TRACE), 18999be88e75SGabriel Fernandez 19009be88e75SGabriel Fernandez #if defined(IMAGE_BL32) 19019be88e75SGabriel Fernandez STM32_GATE(_TIM2, TIM2_K, _CKTIMG1, 0, GATE_TIM2), 19029be88e75SGabriel Fernandez STM32_GATE(_TIM3, TIM3_K, _CKTIMG1, 0, GATE_TIM3), 19039be88e75SGabriel Fernandez STM32_GATE(_TIM4, TIM4_K, _CKTIMG1, 0, GATE_TIM4), 19049be88e75SGabriel Fernandez STM32_GATE(_TIM5, TIM5_K, _CKTIMG1, 0, GATE_TIM5), 19059be88e75SGabriel Fernandez STM32_GATE(_TIM6, TIM6_K, _CKTIMG1, 0, GATE_TIM6), 19069be88e75SGabriel Fernandez STM32_GATE(_TIM7, TIM7_K, _CKTIMG1, 0, GATE_TIM7), 19079be88e75SGabriel Fernandez STM32_GATE(_TIM13, TIM13_K, _CKTIMG3, 0, GATE_TIM13), 19089be88e75SGabriel Fernandez STM32_GATE(_TIM14, TIM14_K, _CKTIMG3, 0, GATE_TIM14), 19099be88e75SGabriel Fernandez STM32_GATE(_LPTIM1_K, LPTIM1_K, MUX(MUX_LPTIM1), 0, GATE_LPTIM1), 19109be88e75SGabriel Fernandez STM32_GATE(_SPI2_K, SPI2_K, MUX(MUX_SPI23), 0, GATE_SPI2), 19119be88e75SGabriel Fernandez STM32_GATE(_SPI3_K, SPI3_K, MUX(MUX_SPI23), 0, GATE_SPI3), 19129be88e75SGabriel Fernandez STM32_GATE(_SPDIF_K, SPDIF_K, MUX(MUX_SPDIF), 0, GATE_SPDIF), 19139be88e75SGabriel Fernandez STM32_GATE(_TIM1, TIM1_K, _CKTIMG2, 0, GATE_TIM1), 19149be88e75SGabriel Fernandez STM32_GATE(_TIM8, TIM8_K, _CKTIMG2, 0, GATE_TIM8), 19159be88e75SGabriel Fernandez STM32_GATE(_TIM16, TIM16_K, _CKTIMG3, 0, GATE_TIM16), 19169be88e75SGabriel Fernandez STM32_GATE(_TIM17, TIM17_K, _CKTIMG3, 0, GATE_TIM17), 19179be88e75SGabriel Fernandez STM32_GATE(_SPI1_K, SPI1_K, MUX(MUX_SPI1), 0, GATE_SPI1), 19189be88e75SGabriel Fernandez STM32_GATE(_SPI4_K, SPI4_K, MUX(MUX_SPI4), 0, GATE_SPI4), 19199be88e75SGabriel Fernandez STM32_GATE(_SPI5_K, SPI5_K, MUX(MUX_SPI5), 0, GATE_SPI5), 19209be88e75SGabriel Fernandez STM32_GATE(_SAI1_K, SAI1_K, MUX(MUX_SAI1), 0, GATE_SAI1), 19219be88e75SGabriel Fernandez STM32_GATE(_SAI2_K, SAI2_K, MUX(MUX_SAI2), 0, GATE_SAI2), 19229be88e75SGabriel Fernandez STM32_GATE(_DFSDM, DFSDM_K, MUX(MUX_SAI1), 0, GATE_DFSDM), 19239be88e75SGabriel Fernandez STM32_GATE(_FDCAN_K, FDCAN_K, MUX(MUX_FDCAN), 0, GATE_FDCAN), 19249be88e75SGabriel Fernandez STM32_GATE(_USBH, USBH, _CKAXI, 0, GATE_USBH), 19259be88e75SGabriel Fernandez STM32_GATE(_I2C1_K, I2C1_K, MUX(MUX_I2C12), 0, GATE_I2C1), 19269be88e75SGabriel Fernandez STM32_GATE(_I2C2_K, I2C2_K, MUX(MUX_I2C12), 0, GATE_I2C2), 19279be88e75SGabriel Fernandez STM32_GATE(_ADFSDM, ADFSDM_K, MUX(MUX_SAI1), 0, GATE_ADFSDM), 19289be88e75SGabriel Fernandez STM32_GATE(_LPTIM2_K, LPTIM2_K, MUX(MUX_LPTIM2), 0, GATE_LPTIM2), 19299be88e75SGabriel Fernandez STM32_GATE(_LPTIM3_K, LPTIM3_K, MUX(MUX_LPTIM3), 0, GATE_LPTIM3), 19309be88e75SGabriel Fernandez STM32_GATE(_LPTIM4_K, LPTIM4_K, MUX(MUX_LPTIM45), 0, GATE_LPTIM4), 19319be88e75SGabriel Fernandez STM32_GATE(_LPTIM5_K, LPTIM5_K, MUX(MUX_LPTIM45), 0, GATE_LPTIM5), 19329be88e75SGabriel Fernandez STM32_GATE(_VREF, VREF, _PCLK3, 0, GATE_VREF), 19339be88e75SGabriel Fernandez STM32_GATE(_DTS, TMPSENS, _PCLK3, 0, GATE_DTS), 19349be88e75SGabriel Fernandez STM32_GATE(_PMBCTRL, PMBCTRL, _PCLK3, 0, GATE_HDP), 19359be88e75SGabriel Fernandez STM32_GATE(_HDP, HDP, _PCLK3, 0, GATE_PMBCTRL), 19369be88e75SGabriel Fernandez STM32_GATE(_STGENRO, STGENRO, _PCLK4, 0, GATE_DCMIPP), 19379be88e75SGabriel Fernandez STM32_GATE(_DCMIPP_K, DCMIPP_K, MUX(MUX_DCMIPP), 0, GATE_DCMIPP), 19389be88e75SGabriel Fernandez STM32_GATE(_DMAMUX1, DMAMUX1, _CKAXI, 0, GATE_DMAMUX1), 19399be88e75SGabriel Fernandez STM32_GATE(_DMAMUX2, DMAMUX2, _CKAXI, 0, GATE_DMAMUX2), 19409be88e75SGabriel Fernandez STM32_GATE(_DMA3, DMA3, _CKAXI, 0, GATE_DMAMUX2), 19419be88e75SGabriel Fernandez STM32_GATE(_ADC1_K, ADC1_K, MUX(MUX_ADC1), 0, GATE_ADC1), 19429be88e75SGabriel Fernandez STM32_GATE(_ADC2_K, ADC2_K, MUX(MUX_ADC2), 0, GATE_ADC2), 19439be88e75SGabriel Fernandez STM32_GATE(_TSC, TSC, _CKAXI, 0, GATE_TSC), 19449be88e75SGabriel Fernandez STM32_GATE(_AXIMC, AXIMC, _CKAXI, 0, GATE_AXIMC), 19459be88e75SGabriel Fernandez STM32_GATE(_CRC1, CRC1, _CKAXI, 0, GATE_ETH1TX), 19469be88e75SGabriel Fernandez STM32_GATE(_ETH1CK, ETH1CK_K, MUX(MUX_ETH1), 0, GATE_ETH1CK), 19479be88e75SGabriel Fernandez STM32_GATE(_ETH1TX, ETH1TX, _CKAXI, 0, GATE_ETH1TX), 19489be88e75SGabriel Fernandez STM32_GATE(_ETH1RX, ETH1RX, _CKAXI, 0, GATE_ETH1RX), 19499be88e75SGabriel Fernandez STM32_GATE(_ETH2CK, ETH2CK_K, MUX(MUX_ETH2), 0, GATE_ETH2CK), 19509be88e75SGabriel Fernandez STM32_GATE(_ETH2TX, ETH2TX, _CKAXI, 0, GATE_ETH2TX), 19519be88e75SGabriel Fernandez STM32_GATE(_ETH2RX, ETH2RX, _CKAXI, 0, GATE_ETH2RX), 19529be88e75SGabriel Fernandez STM32_GATE(_ETH2MAC, ETH2MAC, _CKAXI, 0, GATE_ETH2MAC), 19539be88e75SGabriel Fernandez #endif 19549be88e75SGabriel Fernandez }; 19559be88e75SGabriel Fernandez 19569be88e75SGabriel Fernandez static struct stm32_pll_dt_cfg mp13_pll[_PLL_NB]; 19579be88e75SGabriel Fernandez 19589be88e75SGabriel Fernandez static struct stm32_osci_dt_cfg mp13_osci[NB_OSCILLATOR]; 19599be88e75SGabriel Fernandez 19609be88e75SGabriel Fernandez static uint32_t mp13_clksrc[MUX_MAX]; 19619be88e75SGabriel Fernandez 19629be88e75SGabriel Fernandez static uint32_t mp13_clkdiv[DIV_MAX]; 19639be88e75SGabriel Fernandez 19649be88e75SGabriel Fernandez static struct stm32_clk_platdata stm32mp13_clock_pdata = { 19659be88e75SGabriel Fernandez .osci = mp13_osci, 19669be88e75SGabriel Fernandez .nosci = NB_OSCILLATOR, 19679be88e75SGabriel Fernandez .pll = mp13_pll, 19689be88e75SGabriel Fernandez .npll = _PLL_NB, 19699be88e75SGabriel Fernandez .clksrc = mp13_clksrc, 19709be88e75SGabriel Fernandez .nclksrc = MUX_MAX, 19719be88e75SGabriel Fernandez .clkdiv = mp13_clkdiv, 19729be88e75SGabriel Fernandez .nclkdiv = DIV_MAX, 19739be88e75SGabriel Fernandez }; 19749be88e75SGabriel Fernandez 19759be88e75SGabriel Fernandez static struct stm32_clk_priv stm32mp13_clock_data = { 19769be88e75SGabriel Fernandez .base = RCC_BASE, 19779be88e75SGabriel Fernandez .num = ARRAY_SIZE(stm32mp13_clk), 19789be88e75SGabriel Fernandez .clks = stm32mp13_clk, 19799be88e75SGabriel Fernandez .parents = parent_mp13, 19809be88e75SGabriel Fernandez .nb_parents = ARRAY_SIZE(parent_mp13), 19819be88e75SGabriel Fernandez .gates = gates_mp13, 19829be88e75SGabriel Fernandez .nb_gates = ARRAY_SIZE(gates_mp13), 19839be88e75SGabriel Fernandez .div = dividers_mp13, 19849be88e75SGabriel Fernandez .nb_div = ARRAY_SIZE(dividers_mp13), 19859be88e75SGabriel Fernandez .osci_data = stm32mp13_osc_data, 19869be88e75SGabriel Fernandez .nb_osci_data = ARRAY_SIZE(stm32mp13_osc_data), 19879be88e75SGabriel Fernandez .gate_refcounts = refcounts_mp13, 19889be88e75SGabriel Fernandez .pdata = &stm32mp13_clock_pdata, 19899be88e75SGabriel Fernandez }; 19909be88e75SGabriel Fernandez 19919be88e75SGabriel Fernandez static int stm32mp1_init_clock_tree(void) 19929be88e75SGabriel Fernandez { 19939be88e75SGabriel Fernandez struct stm32_clk_priv *priv = clk_stm32_get_priv(); 19949be88e75SGabriel Fernandez int ret; 19959be88e75SGabriel Fernandez 19969be88e75SGabriel Fernandez #if STM32MP_USB_PROGRAMMER 19979be88e75SGabriel Fernandez int usbphy_p = _clk_stm32_get_parent(priv, _USBPHY_K); 19989be88e75SGabriel Fernandez int usbo_p = _clk_stm32_get_parent(priv, _USBO_K); 19999be88e75SGabriel Fernandez 20009be88e75SGabriel Fernandez /* Don't initialize PLL4, when used by BOOTROM */ 20019be88e75SGabriel Fernandez pll4_bootrom = stm32mp1_clk_is_pll4_used_by_bootrom(priv, usbphy_p); 20029be88e75SGabriel Fernandez #endif 20039be88e75SGabriel Fernandez 20049be88e75SGabriel Fernandez /* 20059be88e75SGabriel Fernandez * Switch ON oscillators found in device-tree. 20069be88e75SGabriel Fernandez * Note: HSI already ON after BootROM stage. 20079be88e75SGabriel Fernandez */ 20089be88e75SGabriel Fernandez stm32_clk_oscillators_enable(priv); 20099be88e75SGabriel Fernandez 20109be88e75SGabriel Fernandez /* Come back to HSI */ 20119be88e75SGabriel Fernandez ret = stm32mp1_come_back_to_hsi(); 20129be88e75SGabriel Fernandez if (ret != 0) { 20139be88e75SGabriel Fernandez return ret; 20149be88e75SGabriel Fernandez } 20159be88e75SGabriel Fernandez 20169be88e75SGabriel Fernandez ret = stm32_clk_hsidiv_configure(priv); 20179be88e75SGabriel Fernandez if (ret != 0) { 20189be88e75SGabriel Fernandez return ret; 20199be88e75SGabriel Fernandez } 20209be88e75SGabriel Fernandez 20219be88e75SGabriel Fernandez ret = stm32_clk_stgen_configure(priv, _STGENC); 20229be88e75SGabriel Fernandez if (ret != 0) { 20239be88e75SGabriel Fernandez panic(); 20249be88e75SGabriel Fernandez } 20259be88e75SGabriel Fernandez 20269be88e75SGabriel Fernandez ret = stm32_clk_dividers_configure(priv); 20279be88e75SGabriel Fernandez if (ret != 0) { 20289be88e75SGabriel Fernandez panic(); 20299be88e75SGabriel Fernandez } 20309be88e75SGabriel Fernandez 20319be88e75SGabriel Fernandez ret = stm32_clk_pll_configure(priv); 20329be88e75SGabriel Fernandez if (ret != 0) { 20339be88e75SGabriel Fernandez panic(); 20349be88e75SGabriel Fernandez } 20359be88e75SGabriel Fernandez 20369be88e75SGabriel Fernandez /* Wait LSE ready before to use it */ 20379be88e75SGabriel Fernandez ret = stm32_clk_oscillators_wait_lse_ready(priv); 20389be88e75SGabriel Fernandez if (ret != 0) { 20399be88e75SGabriel Fernandez panic(); 20409be88e75SGabriel Fernandez } 20419be88e75SGabriel Fernandez 20429be88e75SGabriel Fernandez /* Configure with expected clock source */ 20439be88e75SGabriel Fernandez ret = stm32_clk_source_configure(priv); 20449be88e75SGabriel Fernandez if (ret != 0) { 20459be88e75SGabriel Fernandez panic(); 20469be88e75SGabriel Fernandez } 20479be88e75SGabriel Fernandez 20489be88e75SGabriel Fernandez /* Configure LSE css after RTC source configuration */ 20499be88e75SGabriel Fernandez ret = stm32_clk_oscillators_lse_set_css(priv); 20509be88e75SGabriel Fernandez if (ret != 0) { 20519be88e75SGabriel Fernandez panic(); 20529be88e75SGabriel Fernandez } 20539be88e75SGabriel Fernandez 20549be88e75SGabriel Fernandez #if STM32MP_USB_PROGRAMMER 20559be88e75SGabriel Fernandez ret = stm32mp1_clk_check_usb_conflict(priv, usbphy_p, usbo_p); 20569be88e75SGabriel Fernandez if (ret != 0) { 20579be88e75SGabriel Fernandez return ret; 20589be88e75SGabriel Fernandez } 20599be88e75SGabriel Fernandez #endif 20609be88e75SGabriel Fernandez /* reconfigure STGEN with DT config */ 20619be88e75SGabriel Fernandez ret = stm32_clk_stgen_configure(priv, _STGENC); 20629be88e75SGabriel Fernandez if (ret != 0) { 20639be88e75SGabriel Fernandez panic(); 20649be88e75SGabriel Fernandez } 20659be88e75SGabriel Fernandez 20669be88e75SGabriel Fernandez /* Software Self-Refresh mode (SSR) during DDR initilialization */ 20679be88e75SGabriel Fernandez mmio_clrsetbits_32(priv->base + RCC_DDRITFCR, 20689be88e75SGabriel Fernandez RCC_DDRITFCR_DDRCKMOD_MASK, 20699be88e75SGabriel Fernandez RCC_DDRITFCR_DDRCKMOD_SSR << 20709be88e75SGabriel Fernandez RCC_DDRITFCR_DDRCKMOD_SHIFT); 20719be88e75SGabriel Fernandez 20729be88e75SGabriel Fernandez return 0; 20739be88e75SGabriel Fernandez } 20749be88e75SGabriel Fernandez 20759be88e75SGabriel Fernandez #define LSEDRV_MEDIUM_HIGH 2 20769be88e75SGabriel Fernandez 20779be88e75SGabriel Fernandez static int clk_stm32_parse_oscillator_fdt(void *fdt, int node, const char *name, 20789be88e75SGabriel Fernandez struct stm32_osci_dt_cfg *osci) 20799be88e75SGabriel Fernandez { 20809be88e75SGabriel Fernandez int subnode = 0; 20819be88e75SGabriel Fernandez 20829be88e75SGabriel Fernandez /* default value oscillator not found, freq=0 */ 20839be88e75SGabriel Fernandez osci->freq = 0; 20849be88e75SGabriel Fernandez 20859be88e75SGabriel Fernandez fdt_for_each_subnode(subnode, fdt, node) { 20869be88e75SGabriel Fernandez const char *cchar = NULL; 20879be88e75SGabriel Fernandez const fdt32_t *cuint = NULL; 20889be88e75SGabriel Fernandez int ret = 0; 20899be88e75SGabriel Fernandez 20909be88e75SGabriel Fernandez cchar = fdt_get_name(fdt, subnode, &ret); 20919be88e75SGabriel Fernandez if (cchar == NULL) { 20929be88e75SGabriel Fernandez return ret; 20939be88e75SGabriel Fernandez } 20949be88e75SGabriel Fernandez 20959be88e75SGabriel Fernandez if (strncmp(cchar, name, (size_t)ret) || 20969be88e75SGabriel Fernandez fdt_get_status(subnode) == DT_DISABLED) { 20979be88e75SGabriel Fernandez continue; 20989be88e75SGabriel Fernandez } 20999be88e75SGabriel Fernandez 21009be88e75SGabriel Fernandez cuint = fdt_getprop(fdt, subnode, "clock-frequency", &ret); 21019be88e75SGabriel Fernandez if (cuint == NULL) { 21029be88e75SGabriel Fernandez return ret; 21039be88e75SGabriel Fernandez } 21049be88e75SGabriel Fernandez 21059be88e75SGabriel Fernandez osci->freq = fdt32_to_cpu(*cuint); 21069be88e75SGabriel Fernandez 21079be88e75SGabriel Fernandez if (fdt_getprop(fdt, subnode, "st,bypass", NULL) != NULL) { 21089be88e75SGabriel Fernandez osci->bypass = true; 21099be88e75SGabriel Fernandez } 21109be88e75SGabriel Fernandez 21119be88e75SGabriel Fernandez if (fdt_getprop(fdt, subnode, "st,digbypass", NULL) != NULL) { 21129be88e75SGabriel Fernandez osci->digbyp = true; 21139be88e75SGabriel Fernandez } 21149be88e75SGabriel Fernandez 21159be88e75SGabriel Fernandez if (fdt_getprop(fdt, subnode, "st,css", NULL) != NULL) { 21169be88e75SGabriel Fernandez osci->css = true; 21179be88e75SGabriel Fernandez } 21189be88e75SGabriel Fernandez 21199be88e75SGabriel Fernandez osci->drive = fdt_read_uint32_default(fdt, subnode, "st,drive", LSEDRV_MEDIUM_HIGH); 21209be88e75SGabriel Fernandez 21219be88e75SGabriel Fernandez return 0; 21229be88e75SGabriel Fernandez } 21239be88e75SGabriel Fernandez 21249be88e75SGabriel Fernandez return 0; 21259be88e75SGabriel Fernandez } 21269be88e75SGabriel Fernandez 21279be88e75SGabriel Fernandez static int stm32_clk_parse_fdt_all_oscillator(void *fdt, struct stm32_clk_platdata *pdata) 21289be88e75SGabriel Fernandez { 21299be88e75SGabriel Fernandez int fdt_err = 0; 21309be88e75SGabriel Fernandez uint32_t i = 0; 21319be88e75SGabriel Fernandez int node = 0; 21329be88e75SGabriel Fernandez 21339be88e75SGabriel Fernandez node = fdt_path_offset(fdt, "/clocks"); 21349be88e75SGabriel Fernandez if (node < 0) { 21359be88e75SGabriel Fernandez return -FDT_ERR_NOTFOUND; 21369be88e75SGabriel Fernandez } 21379be88e75SGabriel Fernandez 21389be88e75SGabriel Fernandez for (i = 0; i < pdata->nosci; i++) { 21399be88e75SGabriel Fernandez const char *name = NULL; 21409be88e75SGabriel Fernandez 21419be88e75SGabriel Fernandez name = clk_stm32_get_oscillator_name((enum stm32_osc)i); 21429be88e75SGabriel Fernandez if (name == NULL) { 21439be88e75SGabriel Fernandez continue; 21449be88e75SGabriel Fernandez } 21459be88e75SGabriel Fernandez 21469be88e75SGabriel Fernandez fdt_err = clk_stm32_parse_oscillator_fdt(fdt, node, name, &pdata->osci[i]); 21479be88e75SGabriel Fernandez if (fdt_err < 0) { 21489be88e75SGabriel Fernandez panic(); 21499be88e75SGabriel Fernandez } 21509be88e75SGabriel Fernandez } 21519be88e75SGabriel Fernandez 21529be88e75SGabriel Fernandez return 0; 21539be88e75SGabriel Fernandez } 21549be88e75SGabriel Fernandez 21559be88e75SGabriel Fernandez #define RCC_PLL_NAME_SIZE 12 21569be88e75SGabriel Fernandez 21579be88e75SGabriel Fernandez static int clk_stm32_load_vco_config(void *fdt, int subnode, struct stm32_pll_vco *vco) 21589be88e75SGabriel Fernandez { 21599be88e75SGabriel Fernandez int err = 0; 21609be88e75SGabriel Fernandez 21619be88e75SGabriel Fernandez err = fdt_read_uint32_array(fdt, subnode, "divmn", (int)PLL_DIV_MN_NB, vco->div_mn); 21629be88e75SGabriel Fernandez if (err != 0) { 21639be88e75SGabriel Fernandez return err; 21649be88e75SGabriel Fernandez } 21659be88e75SGabriel Fernandez 21669be88e75SGabriel Fernandez err = fdt_read_uint32_array(fdt, subnode, "csg", (int)PLL_CSG_NB, vco->csg); 21679be88e75SGabriel Fernandez 21689be88e75SGabriel Fernandez vco->csg_enabled = (err == 0); 21699be88e75SGabriel Fernandez 21709be88e75SGabriel Fernandez if (err == -FDT_ERR_NOTFOUND) { 21719be88e75SGabriel Fernandez err = 0; 21729be88e75SGabriel Fernandez } 21739be88e75SGabriel Fernandez 21749be88e75SGabriel Fernandez if (err != 0) { 21759be88e75SGabriel Fernandez return err; 21769be88e75SGabriel Fernandez } 21779be88e75SGabriel Fernandez 21789be88e75SGabriel Fernandez vco->status = RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN | RCC_PLLNCR_PLLON; 21799be88e75SGabriel Fernandez 21809be88e75SGabriel Fernandez vco->frac = fdt_read_uint32_default(fdt, subnode, "frac", 0); 21819be88e75SGabriel Fernandez 21829be88e75SGabriel Fernandez vco->src = fdt_read_uint32_default(fdt, subnode, "src", UINT32_MAX); 21839be88e75SGabriel Fernandez 21849be88e75SGabriel Fernandez return 0; 21859be88e75SGabriel Fernandez } 21869be88e75SGabriel Fernandez 21879be88e75SGabriel Fernandez static int clk_stm32_load_output_config(void *fdt, int subnode, struct stm32_pll_output *output) 21889be88e75SGabriel Fernandez { 21899be88e75SGabriel Fernandez int err = 0; 21909be88e75SGabriel Fernandez 21919be88e75SGabriel Fernandez err = fdt_read_uint32_array(fdt, subnode, "st,pll_div_pqr", (int)PLL_DIV_PQR_NB, 21929be88e75SGabriel Fernandez output->output); 21939be88e75SGabriel Fernandez if (err != 0) { 21949be88e75SGabriel Fernandez return err; 21959be88e75SGabriel Fernandez } 21969be88e75SGabriel Fernandez 21979be88e75SGabriel Fernandez return 0; 21989be88e75SGabriel Fernandez } 21999be88e75SGabriel Fernandez 22009be88e75SGabriel Fernandez static int clk_stm32_parse_pll_fdt(void *fdt, int subnode, struct stm32_pll_dt_cfg *pll) 22019be88e75SGabriel Fernandez { 22029be88e75SGabriel Fernandez const fdt32_t *cuint = NULL; 22039be88e75SGabriel Fernandez int subnode_pll = 0; 22049be88e75SGabriel Fernandez int subnode_vco = 0; 22059be88e75SGabriel Fernandez int err = 0; 22069be88e75SGabriel Fernandez 22079be88e75SGabriel Fernandez cuint = fdt_getprop(fdt, subnode, "st,pll", NULL); 22089be88e75SGabriel Fernandez if (!cuint) { 22099be88e75SGabriel Fernandez return -FDT_ERR_NOTFOUND; 22109be88e75SGabriel Fernandez } 22119be88e75SGabriel Fernandez 22129be88e75SGabriel Fernandez subnode_pll = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint)); 22139be88e75SGabriel Fernandez if (subnode_pll < 0) { 22149be88e75SGabriel Fernandez return -FDT_ERR_NOTFOUND; 22159be88e75SGabriel Fernandez } 22169be88e75SGabriel Fernandez 22179be88e75SGabriel Fernandez cuint = fdt_getprop(fdt, subnode_pll, "st,pll_vco", NULL); 22189be88e75SGabriel Fernandez if (!cuint) { 22199be88e75SGabriel Fernandez return -FDT_ERR_NOTFOUND; 22209be88e75SGabriel Fernandez } 22219be88e75SGabriel Fernandez 22229be88e75SGabriel Fernandez subnode_vco = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint)); 22239be88e75SGabriel Fernandez if (subnode_vco < 0) { 22249be88e75SGabriel Fernandez return -FDT_ERR_NOTFOUND; 22259be88e75SGabriel Fernandez } 22269be88e75SGabriel Fernandez 22279be88e75SGabriel Fernandez err = clk_stm32_load_vco_config(fdt, subnode_vco, &pll->vco); 22289be88e75SGabriel Fernandez if (err != 0) { 22299be88e75SGabriel Fernandez return err; 22309be88e75SGabriel Fernandez } 22319be88e75SGabriel Fernandez 22329be88e75SGabriel Fernandez err = clk_stm32_load_output_config(fdt, subnode_pll, &pll->output); 22339be88e75SGabriel Fernandez if (err != 0) { 22349be88e75SGabriel Fernandez return err; 22359be88e75SGabriel Fernandez } 22369be88e75SGabriel Fernandez 22379be88e75SGabriel Fernandez return 0; 22389be88e75SGabriel Fernandez } 22399be88e75SGabriel Fernandez 22409be88e75SGabriel Fernandez static int stm32_clk_parse_fdt_all_pll(void *fdt, int node, struct stm32_clk_platdata *pdata) 22419be88e75SGabriel Fernandez { 22429be88e75SGabriel Fernandez size_t i = 0U; 22439be88e75SGabriel Fernandez 22449be88e75SGabriel Fernandez for (i = _PLL1; i < pdata->npll; i++) { 22459be88e75SGabriel Fernandez struct stm32_pll_dt_cfg *pll = pdata->pll + i; 22469be88e75SGabriel Fernandez char name[RCC_PLL_NAME_SIZE]; 22479be88e75SGabriel Fernandez int subnode = 0; 22489be88e75SGabriel Fernandez int err = 0; 22499be88e75SGabriel Fernandez 22509be88e75SGabriel Fernandez snprintf(name, sizeof(name), "st,pll@%u", i); 22519be88e75SGabriel Fernandez 22529be88e75SGabriel Fernandez subnode = fdt_subnode_offset(fdt, node, name); 22539be88e75SGabriel Fernandez if (!fdt_check_node(subnode)) { 22549be88e75SGabriel Fernandez continue; 22559be88e75SGabriel Fernandez } 22569be88e75SGabriel Fernandez 22579be88e75SGabriel Fernandez err = clk_stm32_parse_pll_fdt(fdt, subnode, pll); 22589be88e75SGabriel Fernandez if (err != 0) { 22599be88e75SGabriel Fernandez panic(); 22609be88e75SGabriel Fernandez } 22619be88e75SGabriel Fernandez } 22629be88e75SGabriel Fernandez 22639be88e75SGabriel Fernandez return 0; 22649be88e75SGabriel Fernandez } 22659be88e75SGabriel Fernandez 22669be88e75SGabriel Fernandez static int stm32_clk_parse_fdt(struct stm32_clk_platdata *pdata) 22679be88e75SGabriel Fernandez { 22689be88e75SGabriel Fernandez void *fdt = NULL; 22699be88e75SGabriel Fernandez int node; 22709be88e75SGabriel Fernandez uint32_t err; 22719be88e75SGabriel Fernandez 22729be88e75SGabriel Fernandez if (fdt_get_address(&fdt) == 0) { 22739be88e75SGabriel Fernandez return -ENOENT; 22749be88e75SGabriel Fernandez } 22759be88e75SGabriel Fernandez 22769be88e75SGabriel Fernandez node = fdt_node_offset_by_compatible(fdt, -1, DT_RCC_CLK_COMPAT); 22779be88e75SGabriel Fernandez if (node < 0) { 22789be88e75SGabriel Fernandez panic(); 22799be88e75SGabriel Fernandez } 22809be88e75SGabriel Fernandez 22819be88e75SGabriel Fernandez err = stm32_clk_parse_fdt_all_oscillator(fdt, pdata); 22829be88e75SGabriel Fernandez if (err != 0) { 22839be88e75SGabriel Fernandez return err; 22849be88e75SGabriel Fernandez } 22859be88e75SGabriel Fernandez 22869be88e75SGabriel Fernandez err = stm32_clk_parse_fdt_all_pll(fdt, node, pdata); 22879be88e75SGabriel Fernandez if (err != 0) { 22889be88e75SGabriel Fernandez return err; 22899be88e75SGabriel Fernandez } 22909be88e75SGabriel Fernandez 22919be88e75SGabriel Fernandez err = stm32_clk_parse_fdt_by_name(fdt, node, "st,clkdiv", pdata->clkdiv, &pdata->nclkdiv); 22929be88e75SGabriel Fernandez if (err != 0) { 22939be88e75SGabriel Fernandez return err; 22949be88e75SGabriel Fernandez } 22959be88e75SGabriel Fernandez 22969be88e75SGabriel Fernandez err = stm32_clk_parse_fdt_by_name(fdt, node, "st,clksrc", pdata->clksrc, &pdata->nclksrc); 22979be88e75SGabriel Fernandez if (err != 0) { 22989be88e75SGabriel Fernandez return err; 22999be88e75SGabriel Fernandez } 23009be88e75SGabriel Fernandez 23019be88e75SGabriel Fernandez return 0; 23029be88e75SGabriel Fernandez } 23039be88e75SGabriel Fernandez 23049be88e75SGabriel Fernandez int stm32mp1_clk_init(void) 23059be88e75SGabriel Fernandez { 23069be88e75SGabriel Fernandez return 0; 23079be88e75SGabriel Fernandez } 23089be88e75SGabriel Fernandez 23099be88e75SGabriel Fernandez int stm32mp1_clk_probe(void) 23109be88e75SGabriel Fernandez { 23119be88e75SGabriel Fernandez uintptr_t base = RCC_BASE; 23129be88e75SGabriel Fernandez int ret; 23139be88e75SGabriel Fernandez 23149be88e75SGabriel Fernandez ret = stm32_clk_parse_fdt(&stm32mp13_clock_pdata); 23159be88e75SGabriel Fernandez if (ret != 0) { 23169be88e75SGabriel Fernandez return ret; 23179be88e75SGabriel Fernandez } 23189be88e75SGabriel Fernandez 23199be88e75SGabriel Fernandez ret = clk_stm32_init(&stm32mp13_clock_data, base); 23209be88e75SGabriel Fernandez if (ret != 0) { 23219be88e75SGabriel Fernandez return ret; 23229be88e75SGabriel Fernandez } 23239be88e75SGabriel Fernandez 23249be88e75SGabriel Fernandez ret = stm32mp1_init_clock_tree(); 23259be88e75SGabriel Fernandez if (ret != 0) { 23269be88e75SGabriel Fernandez return ret; 23279be88e75SGabriel Fernandez } 23289be88e75SGabriel Fernandez 23299be88e75SGabriel Fernandez clk_stm32_enable_critical_clocks(); 23309be88e75SGabriel Fernandez 23319be88e75SGabriel Fernandez return 0; 23329be88e75SGabriel Fernandez } 2333