Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vikas Manocha | 1b51c93 | 2016-02-11 15:47:20 -0800 | [diff] [blame] | 2 | /* |
Patrice Chotard | 789ee0e | 2017-10-23 09:53:58 +0200 | [diff] [blame] | 3 | * Copyright (C) 2017, STMicroelectronics - All Rights Reserved |
| 4 | * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics. |
Vikas Manocha | 1b51c93 | 2016-02-11 15:47:20 -0800 | [diff] [blame] | 5 | */ |
Patrice Chotard | 789ee0e | 2017-10-23 09:53:58 +0200 | [diff] [blame] | 6 | |
Patrick Delaunay | a57b8cd | 2020-11-06 19:01:46 +0100 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_CLK |
| 8 | |
Vikas Manocha | daaeaab | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 9 | #include <clk-uclass.h> |
| 10 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Patrice Chotard | 03f10a1 | 2017-11-15 13:14:51 +0100 | [diff] [blame] | 12 | #include <stm32_rcc.h> |
Vikas Manocha | 1b51c93 | 2016-02-11 15:47:20 -0800 | [diff] [blame] | 13 | #include <asm/io.h> |
Vikas Manocha | 1b51c93 | 2016-02-11 15:47:20 -0800 | [diff] [blame] | 14 | #include <asm/arch/stm32.h> |
Patrice Chotard | 22768d5 | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 15 | #include <asm/arch/stm32_pwr.h> |
Patrick Delaunay | a57b8cd | 2020-11-06 19:01:46 +0100 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Patrice Chotard | 7bdf971 | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 17 | #include <dt-bindings/mfd/stm32f7-rcc.h> |
Patrick Delaunay | a57b8cd | 2020-11-06 19:01:46 +0100 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Patrice Chotard | 7bdf971 | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 19 | |
Michael Kurz | c204fb7 | 2017-01-22 16:04:24 +0100 | [diff] [blame] | 20 | #define RCC_CR_HSION BIT(0) |
| 21 | #define RCC_CR_HSEON BIT(16) |
| 22 | #define RCC_CR_HSERDY BIT(17) |
| 23 | #define RCC_CR_HSEBYP BIT(18) |
| 24 | #define RCC_CR_CSSON BIT(19) |
| 25 | #define RCC_CR_PLLON BIT(24) |
| 26 | #define RCC_CR_PLLRDY BIT(25) |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 27 | #define RCC_CR_PLLSAION BIT(28) |
| 28 | #define RCC_CR_PLLSAIRDY BIT(29) |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 29 | |
Michael Kurz | c204fb7 | 2017-01-22 16:04:24 +0100 | [diff] [blame] | 30 | #define RCC_PLLCFGR_PLLM_MASK GENMASK(5, 0) |
| 31 | #define RCC_PLLCFGR_PLLN_MASK GENMASK(14, 6) |
| 32 | #define RCC_PLLCFGR_PLLP_MASK GENMASK(17, 16) |
| 33 | #define RCC_PLLCFGR_PLLQ_MASK GENMASK(27, 24) |
| 34 | #define RCC_PLLCFGR_PLLSRC BIT(22) |
| 35 | #define RCC_PLLCFGR_PLLM_SHIFT 0 |
| 36 | #define RCC_PLLCFGR_PLLN_SHIFT 6 |
| 37 | #define RCC_PLLCFGR_PLLP_SHIFT 16 |
| 38 | #define RCC_PLLCFGR_PLLQ_SHIFT 24 |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 39 | |
Michael Kurz | c204fb7 | 2017-01-22 16:04:24 +0100 | [diff] [blame] | 40 | #define RCC_CFGR_AHB_PSC_MASK GENMASK(7, 4) |
| 41 | #define RCC_CFGR_APB1_PSC_MASK GENMASK(12, 10) |
| 42 | #define RCC_CFGR_APB2_PSC_MASK GENMASK(15, 13) |
| 43 | #define RCC_CFGR_SW0 BIT(0) |
| 44 | #define RCC_CFGR_SW1 BIT(1) |
| 45 | #define RCC_CFGR_SW_MASK GENMASK(1, 0) |
| 46 | #define RCC_CFGR_SW_HSI 0 |
| 47 | #define RCC_CFGR_SW_HSE RCC_CFGR_SW0 |
| 48 | #define RCC_CFGR_SW_PLL RCC_CFGR_SW1 |
| 49 | #define RCC_CFGR_SWS0 BIT(2) |
| 50 | #define RCC_CFGR_SWS1 BIT(3) |
| 51 | #define RCC_CFGR_SWS_MASK GENMASK(3, 2) |
| 52 | #define RCC_CFGR_SWS_HSI 0 |
| 53 | #define RCC_CFGR_SWS_HSE RCC_CFGR_SWS0 |
| 54 | #define RCC_CFGR_SWS_PLL RCC_CFGR_SWS1 |
| 55 | #define RCC_CFGR_HPRE_SHIFT 4 |
| 56 | #define RCC_CFGR_PPRE1_SHIFT 10 |
| 57 | #define RCC_CFGR_PPRE2_SHIFT 13 |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 58 | |
Patrice Chotard | 9490aca | 2018-02-08 17:20:46 +0100 | [diff] [blame] | 59 | #define RCC_PLLSAICFGR_PLLSAIN_MASK GENMASK(14, 6) |
| 60 | #define RCC_PLLSAICFGR_PLLSAIP_MASK GENMASK(17, 16) |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 61 | #define RCC_PLLSAICFGR_PLLSAIQ_MASK GENMASK(27, 24) |
| 62 | #define RCC_PLLSAICFGR_PLLSAIR_MASK GENMASK(30, 28) |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 63 | #define RCC_PLLSAICFGR_PLLSAIN_SHIFT 6 |
| 64 | #define RCC_PLLSAICFGR_PLLSAIP_SHIFT 16 |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 65 | #define RCC_PLLSAICFGR_PLLSAIQ_SHIFT 24 |
| 66 | #define RCC_PLLSAICFGR_PLLSAIR_SHIFT 28 |
Patrice Chotard | d5d3655 | 2018-01-19 18:02:40 +0100 | [diff] [blame] | 67 | #define RCC_PLLSAICFGR_PLLSAIP_4 BIT(16) |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 68 | #define RCC_PLLSAICFGR_PLLSAIQ_4 BIT(26) |
Patrice Chotard | ff7b11e | 2018-02-08 17:20:49 +0100 | [diff] [blame] | 69 | #define RCC_PLLSAICFGR_PLLSAIR_3 BIT(29) | BIT(28) |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 70 | |
Patrice Chotard | ef77287 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 71 | #define RCC_DCKCFGRX_TIMPRE BIT(24) |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 72 | #define RCC_DCKCFGRX_CK48MSEL BIT(27) |
| 73 | #define RCC_DCKCFGRX_SDMMC1SEL BIT(28) |
| 74 | #define RCC_DCKCFGR2_SDMMC2SEL BIT(29) |
| 75 | |
Patrice Chotard | ff7b11e | 2018-02-08 17:20:49 +0100 | [diff] [blame] | 76 | #define RCC_DCKCFGR_PLLSAIDIVR_SHIFT 16 |
| 77 | #define RCC_DCKCFGR_PLLSAIDIVR_MASK GENMASK(17, 16) |
| 78 | #define RCC_DCKCFGR_PLLSAIDIVR_2 0 |
| 79 | |
Patrice Chotard | 06fc648 | 2017-11-15 13:14:49 +0100 | [diff] [blame] | 80 | /* |
| 81 | * RCC AHB1ENR specific definitions |
| 82 | */ |
| 83 | #define RCC_AHB1ENR_ETHMAC_EN BIT(25) |
| 84 | #define RCC_AHB1ENR_ETHMAC_TX_EN BIT(26) |
| 85 | #define RCC_AHB1ENR_ETHMAC_RX_EN BIT(27) |
| 86 | |
| 87 | /* |
| 88 | * RCC APB1ENR specific definitions |
| 89 | */ |
| 90 | #define RCC_APB1ENR_TIM2EN BIT(0) |
| 91 | #define RCC_APB1ENR_PWREN BIT(28) |
| 92 | |
| 93 | /* |
| 94 | * RCC APB2ENR specific definitions |
| 95 | */ |
| 96 | #define RCC_APB2ENR_SYSCFGEN BIT(14) |
Patrice Chotard | e2d564e | 2018-01-18 14:10:05 +0100 | [diff] [blame] | 97 | #define RCC_APB2ENR_SAI1EN BIT(22) |
Patrice Chotard | 06fc648 | 2017-11-15 13:14:49 +0100 | [diff] [blame] | 98 | |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 99 | enum pllsai_div { |
| 100 | PLLSAIP, |
| 101 | PLLSAIQ, |
| 102 | PLLSAIR, |
| 103 | }; |
| 104 | |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 105 | static const struct stm32_clk_info stm32f4_clk_info = { |
Patrice Chotard | 1509d66 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 106 | /* 180 MHz */ |
| 107 | .sys_pll_psc = { |
Patrice Chotard | 1509d66 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 108 | .pll_n = 360, |
| 109 | .pll_p = 2, |
| 110 | .pll_q = 8, |
| 111 | .ahb_psc = AHB_PSC_1, |
| 112 | .apb1_psc = APB_PSC_4, |
| 113 | .apb2_psc = APB_PSC_2, |
| 114 | }, |
| 115 | .has_overdrive = false, |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 116 | .v2 = false, |
Patrice Chotard | 1509d66 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 117 | }; |
| 118 | |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 119 | static const struct stm32_clk_info stm32f7_clk_info = { |
Patrice Chotard | 1509d66 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 120 | /* 200 MHz */ |
| 121 | .sys_pll_psc = { |
Patrice Chotard | 1509d66 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 122 | .pll_n = 400, |
| 123 | .pll_p = 2, |
| 124 | .pll_q = 8, |
| 125 | .ahb_psc = AHB_PSC_1, |
| 126 | .apb1_psc = APB_PSC_4, |
| 127 | .apb2_psc = APB_PSC_2, |
| 128 | }, |
| 129 | .has_overdrive = true, |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 130 | .v2 = true, |
Patrice Chotard | 1509d66 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 131 | }; |
| 132 | |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 133 | struct stm32_clk { |
| 134 | struct stm32_rcc_regs *base; |
Patrice Chotard | 22768d5 | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 135 | struct stm32_pwr_regs *pwr_regs; |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 136 | struct stm32_clk_info info; |
| 137 | unsigned long hse_rate; |
Patrice Chotard | 7264aae | 2018-04-11 17:07:45 +0200 | [diff] [blame] | 138 | bool pllsaip; |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 139 | }; |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 140 | |
Patrice Chotard | 2cd1ed1 | 2018-02-08 17:20:50 +0100 | [diff] [blame] | 141 | #ifdef CONFIG_VIDEO_STM32 |
| 142 | static const u8 plldivr_table[] = { 0, 0, 2, 3, 4, 5, 6, 7 }; |
| 143 | #endif |
Patrice Chotard | ff7b11e | 2018-02-08 17:20:49 +0100 | [diff] [blame] | 144 | static const u8 pllsaidivr_table[] = { 2, 4, 8, 16 }; |
| 145 | |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 146 | static int configure_clocks(struct udevice *dev) |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 147 | { |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 148 | struct stm32_clk *priv = dev_get_priv(dev); |
| 149 | struct stm32_rcc_regs *regs = priv->base; |
Patrice Chotard | 22768d5 | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 150 | struct stm32_pwr_regs *pwr = priv->pwr_regs; |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 151 | struct pll_psc *sys_pll_psc = &priv->info.sys_pll_psc; |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 152 | |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 153 | /* Reset RCC configuration */ |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 154 | setbits_le32(®s->cr, RCC_CR_HSION); |
| 155 | writel(0, ®s->cfgr); /* Reset CFGR */ |
| 156 | clrbits_le32(®s->cr, (RCC_CR_HSEON | RCC_CR_CSSON |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 157 | | RCC_CR_PLLON | RCC_CR_PLLSAION)); |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 158 | writel(0x24003010, ®s->pllcfgr); /* Reset value from RM */ |
| 159 | clrbits_le32(®s->cr, RCC_CR_HSEBYP); |
| 160 | writel(0, ®s->cir); /* Disable all interrupts */ |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 161 | |
| 162 | /* Configure for HSE+PLL operation */ |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 163 | setbits_le32(®s->cr, RCC_CR_HSEON); |
| 164 | while (!(readl(®s->cr) & RCC_CR_HSERDY)) |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 165 | ; |
| 166 | |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 167 | setbits_le32(®s->cfgr, (( |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 168 | sys_pll_psc->ahb_psc << RCC_CFGR_HPRE_SHIFT) |
| 169 | | (sys_pll_psc->apb1_psc << RCC_CFGR_PPRE1_SHIFT) |
| 170 | | (sys_pll_psc->apb2_psc << RCC_CFGR_PPRE2_SHIFT))); |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 171 | |
| 172 | /* Configure the main PLL */ |
Patrice Chotard | b6653f6 | 2017-10-26 13:23:19 +0200 | [diff] [blame] | 173 | setbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLSRC); /* pll source HSE */ |
| 174 | clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLM_MASK, |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 175 | sys_pll_psc->pll_m << RCC_PLLCFGR_PLLM_SHIFT); |
Patrice Chotard | b6653f6 | 2017-10-26 13:23:19 +0200 | [diff] [blame] | 176 | clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLN_MASK, |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 177 | sys_pll_psc->pll_n << RCC_PLLCFGR_PLLN_SHIFT); |
Patrice Chotard | b6653f6 | 2017-10-26 13:23:19 +0200 | [diff] [blame] | 178 | clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLP_MASK, |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 179 | ((sys_pll_psc->pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT); |
Patrice Chotard | b6653f6 | 2017-10-26 13:23:19 +0200 | [diff] [blame] | 180 | clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLQ_MASK, |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 181 | sys_pll_psc->pll_q << RCC_PLLCFGR_PLLQ_SHIFT); |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 182 | |
Patrice Chotard | cb15d28 | 2018-02-08 17:20:47 +0100 | [diff] [blame] | 183 | /* configure SDMMC clock */ |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 184 | if (priv->info.v2) { /*stm32f7 case */ |
Patrice Chotard | 7264aae | 2018-04-11 17:07:45 +0200 | [diff] [blame] | 185 | if (priv->pllsaip) |
| 186 | /* select PLLSAIP as 48MHz clock source */ |
| 187 | setbits_le32(®s->dckcfgr2, RCC_DCKCFGRX_CK48MSEL); |
| 188 | else |
| 189 | /* select PLLQ as 48MHz clock source */ |
| 190 | clrbits_le32(®s->dckcfgr2, RCC_DCKCFGRX_CK48MSEL); |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 191 | |
| 192 | /* select 48MHz as SDMMC1 clock source */ |
| 193 | clrbits_le32(®s->dckcfgr2, RCC_DCKCFGRX_SDMMC1SEL); |
| 194 | |
| 195 | /* select 48MHz as SDMMC2 clock source */ |
| 196 | clrbits_le32(®s->dckcfgr2, RCC_DCKCFGR2_SDMMC2SEL); |
| 197 | } else { /* stm32f4 case */ |
Patrice Chotard | 7264aae | 2018-04-11 17:07:45 +0200 | [diff] [blame] | 198 | if (priv->pllsaip) |
| 199 | /* select PLLSAIP as 48MHz clock source */ |
| 200 | setbits_le32(®s->dckcfgr, RCC_DCKCFGRX_CK48MSEL); |
| 201 | else |
| 202 | /* select PLLQ as 48MHz clock source */ |
| 203 | clrbits_le32(®s->dckcfgr, RCC_DCKCFGRX_CK48MSEL); |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 204 | |
| 205 | /* select 48MHz as SDMMC1 clock source */ |
| 206 | clrbits_le32(®s->dckcfgr, RCC_DCKCFGRX_SDMMC1SEL); |
| 207 | } |
| 208 | |
Patrice Chotard | ff7b11e | 2018-02-08 17:20:49 +0100 | [diff] [blame] | 209 | /* |
Patrice Chotard | 7264aae | 2018-04-11 17:07:45 +0200 | [diff] [blame] | 210 | * Configure the SAI PLL to generate LTDC pixel clock and |
| 211 | * 48 Mhz for SDMMC and USB |
Patrice Chotard | ff7b11e | 2018-02-08 17:20:49 +0100 | [diff] [blame] | 212 | */ |
Patrice Chotard | 7264aae | 2018-04-11 17:07:45 +0200 | [diff] [blame] | 213 | clrsetbits_le32(®s->pllsaicfgr, RCC_PLLSAICFGR_PLLSAIP_MASK, |
| 214 | RCC_PLLSAICFGR_PLLSAIP_4); |
Patrice Chotard | ff7b11e | 2018-02-08 17:20:49 +0100 | [diff] [blame] | 215 | clrsetbits_le32(®s->pllsaicfgr, RCC_PLLSAICFGR_PLLSAIR_MASK, |
| 216 | RCC_PLLSAICFGR_PLLSAIR_3); |
| 217 | clrsetbits_le32(®s->pllsaicfgr, RCC_PLLSAICFGR_PLLSAIN_MASK, |
| 218 | 195 << RCC_PLLSAICFGR_PLLSAIN_SHIFT); |
| 219 | |
| 220 | clrsetbits_le32(®s->dckcfgr, RCC_DCKCFGR_PLLSAIDIVR_MASK, |
| 221 | RCC_DCKCFGR_PLLSAIDIVR_2 << RCC_DCKCFGR_PLLSAIDIVR_SHIFT); |
Patrice Chotard | 7264aae | 2018-04-11 17:07:45 +0200 | [diff] [blame] | 222 | |
Patrice Chotard | cb15d28 | 2018-02-08 17:20:47 +0100 | [diff] [blame] | 223 | /* Enable the main PLL */ |
| 224 | setbits_le32(®s->cr, RCC_CR_PLLON); |
| 225 | while (!(readl(®s->cr) & RCC_CR_PLLRDY)) |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 226 | ; |
| 227 | |
Patrice Chotard | 7264aae | 2018-04-11 17:07:45 +0200 | [diff] [blame] | 228 | /* Enable the SAI PLL */ |
Patrice Chotard | ff7b11e | 2018-02-08 17:20:49 +0100 | [diff] [blame] | 229 | setbits_le32(®s->cr, RCC_CR_PLLSAION); |
| 230 | while (!(readl(®s->cr) & RCC_CR_PLLSAIRDY)) |
| 231 | ; |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 232 | setbits_le32(®s->apb1enr, RCC_APB1ENR_PWREN); |
Patrice Chotard | 1509d66 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 233 | |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 234 | if (priv->info.has_overdrive) { |
Patrice Chotard | 1509d66 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 235 | /* |
| 236 | * Enable high performance mode |
| 237 | * System frequency up to 200 MHz |
| 238 | */ |
| 239 | setbits_le32(&pwr->cr1, PWR_CR1_ODEN); |
| 240 | /* Infinite wait! */ |
| 241 | while (!(readl(&pwr->csr1) & PWR_CSR1_ODRDY)) |
| 242 | ; |
| 243 | /* Enable the Over-drive switch */ |
| 244 | setbits_le32(&pwr->cr1, PWR_CR1_ODSWEN); |
| 245 | /* Infinite wait! */ |
| 246 | while (!(readl(&pwr->csr1) & PWR_CSR1_ODSWRDY)) |
| 247 | ; |
| 248 | } |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 249 | |
| 250 | stm32_flash_latency_cfg(5); |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 251 | clrbits_le32(®s->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1)); |
| 252 | setbits_le32(®s->cfgr, RCC_CFGR_SW_PLL); |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 253 | |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 254 | while ((readl(®s->cfgr) & RCC_CFGR_SWS_MASK) != |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 255 | RCC_CFGR_SWS_PLL) |
| 256 | ; |
| 257 | |
Patrice Chotard | e2d564e | 2018-01-18 14:10:05 +0100 | [diff] [blame] | 258 | #ifdef CONFIG_ETH_DESIGNWARE |
| 259 | /* gate the SYSCFG clock, needed to set RMII ethernet interface */ |
| 260 | setbits_le32(®s->apb2enr, RCC_APB2ENR_SYSCFGEN); |
| 261 | #endif |
| 262 | |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 266 | static bool stm32_clk_get_ck48msel(struct stm32_clk *priv) |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 267 | { |
| 268 | struct stm32_rcc_regs *regs = priv->base; |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 269 | |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 270 | if (priv->info.v2) /*stm32f7 case */ |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 271 | return readl(®s->dckcfgr2) & RCC_DCKCFGRX_CK48MSEL; |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 272 | else |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 273 | |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 274 | return readl(®s->dckcfgr) & RCC_DCKCFGRX_CK48MSEL; |
| 275 | } |
| 276 | |
| 277 | static unsigned long stm32_clk_get_pllsai_vco_rate(struct stm32_clk *priv) |
| 278 | { |
| 279 | struct stm32_rcc_regs *regs = priv->base; |
| 280 | u16 pllm, pllsain; |
| 281 | |
| 282 | pllm = (readl(®s->pllcfgr) & RCC_PLLCFGR_PLLM_MASK); |
| 283 | pllsain = ((readl(®s->pllsaicfgr) & RCC_PLLSAICFGR_PLLSAIN_MASK) |
| 284 | >> RCC_PLLSAICFGR_PLLSAIN_SHIFT); |
| 285 | |
| 286 | return ((priv->hse_rate / pllm) * pllsain); |
| 287 | } |
| 288 | |
| 289 | static unsigned long stm32_clk_get_pllsai_rate(struct stm32_clk *priv, |
| 290 | enum pllsai_div output) |
| 291 | { |
| 292 | struct stm32_rcc_regs *regs = priv->base; |
| 293 | u16 pll_div_output; |
| 294 | |
| 295 | switch (output) { |
| 296 | case PLLSAIP: |
| 297 | pll_div_output = ((((readl(®s->pllsaicfgr) |
| 298 | & RCC_PLLSAICFGR_PLLSAIP_MASK) |
| 299 | >> RCC_PLLSAICFGR_PLLSAIP_SHIFT) + 1) << 1); |
| 300 | break; |
| 301 | case PLLSAIQ: |
| 302 | pll_div_output = (readl(®s->pllsaicfgr) |
| 303 | & RCC_PLLSAICFGR_PLLSAIQ_MASK) |
| 304 | >> RCC_PLLSAICFGR_PLLSAIQ_SHIFT; |
| 305 | break; |
| 306 | case PLLSAIR: |
| 307 | pll_div_output = (readl(®s->pllsaicfgr) |
| 308 | & RCC_PLLSAICFGR_PLLSAIR_MASK) |
| 309 | >> RCC_PLLSAICFGR_PLLSAIR_SHIFT; |
| 310 | break; |
| 311 | default: |
Patrick Delaunay | a57b8cd | 2020-11-06 19:01:46 +0100 | [diff] [blame] | 312 | log_err("incorrect PLLSAI output %d\n", output); |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 313 | return -EINVAL; |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 314 | } |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 315 | |
| 316 | return (stm32_clk_get_pllsai_vco_rate(priv) / pll_div_output); |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 317 | } |
| 318 | |
Patrice Chotard | ef77287 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 319 | static bool stm32_get_timpre(struct stm32_clk *priv) |
Patrice Chotard | 7bdf971 | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 320 | { |
Patrice Chotard | 7bdf971 | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 321 | struct stm32_rcc_regs *regs = priv->base; |
Patrice Chotard | ef77287 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 322 | u32 val; |
| 323 | |
| 324 | if (priv->info.v2) /*stm32f7 case */ |
| 325 | val = readl(®s->dckcfgr2); |
| 326 | else |
| 327 | val = readl(®s->dckcfgr); |
| 328 | /* get timer prescaler */ |
| 329 | return !!(val & RCC_DCKCFGRX_TIMPRE); |
| 330 | } |
| 331 | |
| 332 | static u32 stm32_get_hclk_rate(struct stm32_rcc_regs *regs, u32 sysclk) |
| 333 | { |
| 334 | u8 shift; |
Patrice Chotard | 7bdf971 | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 335 | /* Prescaler table lookups for clock computation */ |
| 336 | u8 ahb_psc_table[16] = { |
| 337 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9 |
| 338 | }; |
Patrice Chotard | ef77287 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 339 | |
| 340 | shift = ahb_psc_table[( |
| 341 | (readl(®s->cfgr) & RCC_CFGR_AHB_PSC_MASK) |
| 342 | >> RCC_CFGR_HPRE_SHIFT)]; |
| 343 | |
| 344 | return sysclk >> shift; |
| 345 | }; |
| 346 | |
| 347 | static u8 stm32_get_apb_shift(struct stm32_rcc_regs *regs, enum apb apb) |
| 348 | { |
| 349 | /* Prescaler table lookups for clock computation */ |
Patrice Chotard | 7bdf971 | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 350 | u8 apb_psc_table[8] = { |
| 351 | 0, 0, 0, 0, 1, 2, 3, 4 |
| 352 | }; |
| 353 | |
Patrice Chotard | ef77287 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 354 | if (apb == APB1) |
| 355 | return apb_psc_table[( |
| 356 | (readl(®s->cfgr) & RCC_CFGR_APB1_PSC_MASK) |
| 357 | >> RCC_CFGR_PPRE1_SHIFT)]; |
| 358 | else /* APB2 */ |
| 359 | return apb_psc_table[( |
| 360 | (readl(®s->cfgr) & RCC_CFGR_APB2_PSC_MASK) |
| 361 | >> RCC_CFGR_PPRE2_SHIFT)]; |
| 362 | }; |
| 363 | |
| 364 | static u32 stm32_get_timer_rate(struct stm32_clk *priv, u32 sysclk, |
| 365 | enum apb apb) |
| 366 | { |
| 367 | struct stm32_rcc_regs *regs = priv->base; |
| 368 | u8 shift = stm32_get_apb_shift(regs, apb); |
| 369 | |
| 370 | if (stm32_get_timpre(priv)) |
| 371 | /* |
| 372 | * if APB prescaler is configured to a |
| 373 | * division factor of 1, 2 or 4 |
| 374 | */ |
| 375 | switch (shift) { |
| 376 | case 0: |
| 377 | case 1: |
| 378 | case 2: |
| 379 | return stm32_get_hclk_rate(regs, sysclk); |
| 380 | default: |
| 381 | return (sysclk >> shift) * 4; |
| 382 | } |
| 383 | else |
| 384 | /* |
| 385 | * if APB prescaler is configured to a |
| 386 | * division factor of 1 |
| 387 | */ |
| 388 | if (shift == 0) |
| 389 | return sysclk; |
| 390 | else |
| 391 | return (sysclk >> shift) * 2; |
| 392 | }; |
| 393 | |
| 394 | static ulong stm32_clk_get_rate(struct clk *clk) |
| 395 | { |
| 396 | struct stm32_clk *priv = dev_get_priv(clk->dev); |
| 397 | struct stm32_rcc_regs *regs = priv->base; |
| 398 | u32 sysclk = 0; |
Patrice Chotard | 81d7765 | 2018-02-08 17:20:45 +0100 | [diff] [blame] | 399 | u32 vco; |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 400 | u32 sdmmcxsel_bit; |
Patrice Chotard | ff7b11e | 2018-02-08 17:20:49 +0100 | [diff] [blame] | 401 | u32 saidivr; |
| 402 | u32 pllsai_rate; |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 403 | u16 pllm, plln, pllp, pllq; |
Patrice Chotard | ef77287 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 404 | |
Patrice Chotard | 7bdf971 | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 405 | if ((readl(®s->cfgr) & RCC_CFGR_SWS_MASK) == |
| 406 | RCC_CFGR_SWS_PLL) { |
Patrice Chotard | 7bdf971 | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 407 | pllm = (readl(®s->pllcfgr) & RCC_PLLCFGR_PLLM_MASK); |
| 408 | plln = ((readl(®s->pllcfgr) & RCC_PLLCFGR_PLLN_MASK) |
| 409 | >> RCC_PLLCFGR_PLLN_SHIFT); |
| 410 | pllp = ((((readl(®s->pllcfgr) & RCC_PLLCFGR_PLLP_MASK) |
| 411 | >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1); |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 412 | pllq = ((readl(®s->pllcfgr) & RCC_PLLCFGR_PLLQ_MASK) |
| 413 | >> RCC_PLLCFGR_PLLQ_SHIFT); |
Patrice Chotard | 81d7765 | 2018-02-08 17:20:45 +0100 | [diff] [blame] | 414 | vco = (priv->hse_rate / pllm) * plln; |
| 415 | sysclk = vco / pllp; |
Patrice Chotard | 7bdf971 | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 416 | } else { |
| 417 | return -EINVAL; |
| 418 | } |
| 419 | |
| 420 | switch (clk->id) { |
| 421 | /* |
| 422 | * AHB CLOCK: 3 x 32 bits consecutive registers are used : |
| 423 | * AHB1, AHB2 and AHB3 |
| 424 | */ |
| 425 | case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI): |
Patrice Chotard | ef77287 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 426 | return stm32_get_hclk_rate(regs, sysclk); |
Patrice Chotard | 7bdf971 | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 427 | /* APB1 CLOCK */ |
| 428 | case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8): |
Patrice Chotard | ef77287 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 429 | /* For timer clock, an additionnal prescaler is used*/ |
| 430 | switch (clk->id) { |
| 431 | case STM32F7_APB1_CLOCK(TIM2): |
| 432 | case STM32F7_APB1_CLOCK(TIM3): |
| 433 | case STM32F7_APB1_CLOCK(TIM4): |
| 434 | case STM32F7_APB1_CLOCK(TIM5): |
| 435 | case STM32F7_APB1_CLOCK(TIM6): |
| 436 | case STM32F7_APB1_CLOCK(TIM7): |
| 437 | case STM32F7_APB1_CLOCK(TIM12): |
| 438 | case STM32F7_APB1_CLOCK(TIM13): |
| 439 | case STM32F7_APB1_CLOCK(TIM14): |
| 440 | return stm32_get_timer_rate(priv, sysclk, APB1); |
| 441 | } |
| 442 | return (sysclk >> stm32_get_apb_shift(regs, APB1)); |
| 443 | |
Patrice Chotard | 7bdf971 | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 444 | /* APB2 CLOCK */ |
Patrice Chotard | e199c3a | 2018-02-08 17:20:51 +0100 | [diff] [blame] | 445 | case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(DSI): |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 446 | switch (clk->id) { |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 447 | /* |
| 448 | * particular case for SDMMC1 and SDMMC2 : |
| 449 | * 48Mhz source clock can be from main PLL or from |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 450 | * PLLSAIP |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 451 | */ |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 452 | case STM32F7_APB2_CLOCK(SDMMC1): |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 453 | case STM32F7_APB2_CLOCK(SDMMC2): |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 454 | if (clk->id == STM32F7_APB2_CLOCK(SDMMC1)) |
| 455 | sdmmcxsel_bit = RCC_DCKCFGRX_SDMMC1SEL; |
| 456 | else |
| 457 | sdmmcxsel_bit = RCC_DCKCFGR2_SDMMC2SEL; |
| 458 | |
| 459 | if (readl(®s->dckcfgr2) & sdmmcxsel_bit) |
| 460 | /* System clock is selected as SDMMC1 clock */ |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 461 | return sysclk; |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 462 | /* |
| 463 | * 48 MHz can be generated by either PLLSAIP |
| 464 | * or by PLLQ depending of CK48MSEL bit of RCC_DCKCFGR |
| 465 | */ |
| 466 | if (stm32_clk_get_ck48msel(priv)) |
| 467 | return stm32_clk_get_pllsai_rate(priv, PLLSAIP); |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 468 | else |
Patrice Chotard | 8aca2d1 | 2018-02-08 17:20:48 +0100 | [diff] [blame] | 469 | return (vco / pllq); |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 470 | break; |
Patrice Chotard | ef77287 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 471 | |
| 472 | /* For timer clock, an additionnal prescaler is used*/ |
| 473 | case STM32F7_APB2_CLOCK(TIM1): |
| 474 | case STM32F7_APB2_CLOCK(TIM8): |
| 475 | case STM32F7_APB2_CLOCK(TIM9): |
| 476 | case STM32F7_APB2_CLOCK(TIM10): |
| 477 | case STM32F7_APB2_CLOCK(TIM11): |
| 478 | return stm32_get_timer_rate(priv, sysclk, APB2); |
| 479 | break; |
Patrice Chotard | ff7b11e | 2018-02-08 17:20:49 +0100 | [diff] [blame] | 480 | |
| 481 | /* particular case for LTDC clock */ |
| 482 | case STM32F7_APB2_CLOCK(LTDC): |
| 483 | saidivr = readl(®s->dckcfgr); |
| 484 | saidivr = (saidivr & RCC_DCKCFGR_PLLSAIDIVR_MASK) |
| 485 | >> RCC_DCKCFGR_PLLSAIDIVR_SHIFT; |
| 486 | pllsai_rate = stm32_clk_get_pllsai_rate(priv, PLLSAIR); |
| 487 | |
| 488 | return pllsai_rate / pllsaidivr_table[saidivr]; |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 489 | } |
Patrice Chotard | ef77287 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 490 | return (sysclk >> stm32_get_apb_shift(regs, APB2)); |
Patrice Chotard | 369d483 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 491 | |
Patrice Chotard | 7bdf971 | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 492 | default: |
Patrick Delaunay | a57b8cd | 2020-11-06 19:01:46 +0100 | [diff] [blame] | 493 | dev_err(clk->dev, "clock index %ld out of range\n", clk->id); |
Patrice Chotard | 7bdf971 | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 494 | return -EINVAL; |
Patrice Chotard | 7bdf971 | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
Patrice Chotard | 24e8578 | 2018-01-29 18:14:14 +0100 | [diff] [blame] | 498 | static ulong stm32_set_rate(struct clk *clk, ulong rate) |
| 499 | { |
Patrice Chotard | 2cd1ed1 | 2018-02-08 17:20:50 +0100 | [diff] [blame] | 500 | #ifdef CONFIG_VIDEO_STM32 |
| 501 | struct stm32_clk *priv = dev_get_priv(clk->dev); |
| 502 | struct stm32_rcc_regs *regs = priv->base; |
| 503 | u32 pllsair_rate, pllsai_vco_rate, current_rate; |
| 504 | u32 best_div, best_diff, diff; |
| 505 | u16 div; |
| 506 | u8 best_plldivr, best_pllsaidivr; |
| 507 | u8 i, j; |
| 508 | bool found = false; |
| 509 | |
| 510 | /* Only set_rate for LTDC clock is implemented */ |
| 511 | if (clk->id != STM32F7_APB2_CLOCK(LTDC)) { |
Patrick Delaunay | a57b8cd | 2020-11-06 19:01:46 +0100 | [diff] [blame] | 512 | dev_err(clk->dev, |
| 513 | "set_rate not implemented for clock index %ld\n", |
| 514 | clk->id); |
Patrice Chotard | 2cd1ed1 | 2018-02-08 17:20:50 +0100 | [diff] [blame] | 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | if (rate == stm32_clk_get_rate(clk)) |
| 519 | /* already set to requested rate */ |
| 520 | return rate; |
| 521 | |
| 522 | /* get the current PLLSAIR output freq */ |
| 523 | pllsair_rate = stm32_clk_get_pllsai_rate(priv, PLLSAIR); |
Dario Binacchi | 8dcbf81 | 2023-11-11 11:46:19 +0100 | [diff] [blame] | 524 | if ((pllsair_rate % rate) == 0) { |
| 525 | best_div = pllsair_rate / rate; |
Patrice Chotard | 2cd1ed1 | 2018-02-08 17:20:50 +0100 | [diff] [blame] | 526 | |
Dario Binacchi | 8dcbf81 | 2023-11-11 11:46:19 +0100 | [diff] [blame] | 527 | /* look into pllsaidivr_table if this divider is available */ |
| 528 | for (i = 0 ; i < sizeof(pllsaidivr_table); i++) |
| 529 | if (best_div == pllsaidivr_table[i]) { |
| 530 | /* set pll_saidivr with found value */ |
| 531 | clrsetbits_le32(®s->dckcfgr, |
| 532 | RCC_DCKCFGR_PLLSAIDIVR_MASK, |
| 533 | pllsaidivr_table[i] << |
| 534 | RCC_DCKCFGR_PLLSAIDIVR_SHIFT); |
| 535 | return rate; |
| 536 | } |
| 537 | } |
Patrice Chotard | 2cd1ed1 | 2018-02-08 17:20:50 +0100 | [diff] [blame] | 538 | |
| 539 | /* |
| 540 | * As no pllsaidivr value is suitable to obtain requested freq, |
| 541 | * test all combination of pllsaidivr * pllsair and find the one |
| 542 | * which give freq closest to requested rate. |
| 543 | */ |
| 544 | |
| 545 | pllsai_vco_rate = stm32_clk_get_pllsai_vco_rate(priv); |
| 546 | best_diff = ULONG_MAX; |
| 547 | best_pllsaidivr = 0; |
| 548 | best_plldivr = 0; |
| 549 | /* |
| 550 | * start at index 2 of plldivr_table as divider value at index 0 |
| 551 | * and 1 are 0) |
| 552 | */ |
| 553 | for (i = 2; i < sizeof(plldivr_table); i++) { |
| 554 | for (j = 0; j < sizeof(pllsaidivr_table); j++) { |
| 555 | div = plldivr_table[i] * pllsaidivr_table[j]; |
| 556 | current_rate = pllsai_vco_rate / div; |
| 557 | /* perfect combination is found ? */ |
| 558 | if (current_rate == rate) { |
| 559 | best_pllsaidivr = j; |
| 560 | best_plldivr = i; |
| 561 | found = true; |
| 562 | break; |
| 563 | } |
| 564 | |
| 565 | diff = (current_rate > rate) ? |
| 566 | current_rate - rate : rate - current_rate; |
| 567 | |
| 568 | /* found a better combination ? */ |
| 569 | if (diff < best_diff) { |
| 570 | best_diff = diff; |
| 571 | best_pllsaidivr = j; |
| 572 | best_plldivr = i; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | if (found) |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | /* Disable the SAI PLL */ |
| 581 | clrbits_le32(®s->cr, RCC_CR_PLLSAION); |
| 582 | |
| 583 | /* set pll_saidivr with found value */ |
| 584 | clrsetbits_le32(®s->dckcfgr, RCC_DCKCFGR_PLLSAIDIVR_MASK, |
| 585 | best_pllsaidivr << RCC_DCKCFGR_PLLSAIDIVR_SHIFT); |
| 586 | |
| 587 | /* set pllsair with found value */ |
| 588 | clrsetbits_le32(®s->pllsaicfgr, RCC_PLLSAICFGR_PLLSAIR_MASK, |
| 589 | plldivr_table[best_plldivr] |
| 590 | << RCC_PLLSAICFGR_PLLSAIR_SHIFT); |
| 591 | |
| 592 | /* Enable the SAI PLL */ |
| 593 | setbits_le32(®s->cr, RCC_CR_PLLSAION); |
| 594 | while (!(readl(®s->cr) & RCC_CR_PLLSAIRDY)) |
| 595 | ; |
| 596 | |
| 597 | div = plldivr_table[best_plldivr] * pllsaidivr_table[best_pllsaidivr]; |
| 598 | return pllsai_vco_rate / div; |
| 599 | #else |
Patrice Chotard | 24e8578 | 2018-01-29 18:14:14 +0100 | [diff] [blame] | 600 | return 0; |
Patrice Chotard | 2cd1ed1 | 2018-02-08 17:20:50 +0100 | [diff] [blame] | 601 | #endif |
Patrice Chotard | 24e8578 | 2018-01-29 18:14:14 +0100 | [diff] [blame] | 602 | } |
| 603 | |
Vikas Manocha | daaeaab | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 604 | static int stm32_clk_enable(struct clk *clk) |
| 605 | { |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 606 | struct stm32_clk *priv = dev_get_priv(clk->dev); |
| 607 | struct stm32_rcc_regs *regs = priv->base; |
Vikas Manocha | daaeaab | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 608 | u32 offset = clk->id / 32; |
| 609 | u32 bit_index = clk->id % 32; |
| 610 | |
Patrick Delaunay | a57b8cd | 2020-11-06 19:01:46 +0100 | [diff] [blame] | 611 | dev_dbg(clk->dev, "clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n", |
| 612 | clk->id, offset, bit_index); |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 613 | setbits_le32(®s->ahb1enr + offset, BIT(bit_index)); |
Vikas Manocha | daaeaab | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 614 | |
| 615 | return 0; |
| 616 | } |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 617 | |
Vikas Manocha | daaeaab | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 618 | static int stm32_clk_probe(struct udevice *dev) |
| 619 | { |
Patrice Chotard | 22768d5 | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 620 | struct ofnode_phandle_args args; |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 621 | struct udevice *fixed_clock_dev = NULL; |
| 622 | struct clk clk; |
Patrice Chotard | 22768d5 | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 623 | int err; |
| 624 | |
Patrick Delaunay | a57b8cd | 2020-11-06 19:01:46 +0100 | [diff] [blame] | 625 | dev_dbg(dev, "%s\n", __func__); |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 626 | |
| 627 | struct stm32_clk *priv = dev_get_priv(dev); |
| 628 | fdt_addr_t addr; |
| 629 | |
Patrice Chotard | 1509d66 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 630 | addr = dev_read_addr(dev); |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 631 | if (addr == FDT_ADDR_T_NONE) |
| 632 | return -EINVAL; |
| 633 | |
| 634 | priv->base = (struct stm32_rcc_regs *)addr; |
Patrice Chotard | 7264aae | 2018-04-11 17:07:45 +0200 | [diff] [blame] | 635 | priv->pllsaip = true; |
Patrice Chotard | 03f10a1 | 2017-11-15 13:14:51 +0100 | [diff] [blame] | 636 | |
| 637 | switch (dev_get_driver_data(dev)) { |
Patrice Chotard | 7264aae | 2018-04-11 17:07:45 +0200 | [diff] [blame] | 638 | case STM32F42X: |
| 639 | priv->pllsaip = false; |
| 640 | /* fallback into STM32F469 case */ |
| 641 | case STM32F469: |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 642 | memcpy(&priv->info, &stm32f4_clk_info, |
| 643 | sizeof(struct stm32_clk_info)); |
Patrice Chotard | 03f10a1 | 2017-11-15 13:14:51 +0100 | [diff] [blame] | 644 | break; |
Patrice Chotard | 7264aae | 2018-04-11 17:07:45 +0200 | [diff] [blame] | 645 | |
Patrice Chotard | 03f10a1 | 2017-11-15 13:14:51 +0100 | [diff] [blame] | 646 | case STM32F7: |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 647 | memcpy(&priv->info, &stm32f7_clk_info, |
| 648 | sizeof(struct stm32_clk_info)); |
Patrice Chotard | 03f10a1 | 2017-11-15 13:14:51 +0100 | [diff] [blame] | 649 | break; |
| 650 | default: |
| 651 | return -EINVAL; |
| 652 | } |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 653 | |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 654 | /* retrieve HSE frequency (external oscillator) */ |
| 655 | err = uclass_get_device_by_name(UCLASS_CLK, "clk-hse", |
| 656 | &fixed_clock_dev); |
| 657 | |
| 658 | if (err) { |
Patrick Delaunay | a57b8cd | 2020-11-06 19:01:46 +0100 | [diff] [blame] | 659 | dev_err(dev, "Can't find fixed clock (%d)", err); |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 660 | return err; |
| 661 | } |
| 662 | |
| 663 | err = clk_request(fixed_clock_dev, &clk); |
| 664 | if (err) { |
Patrick Delaunay | a57b8cd | 2020-11-06 19:01:46 +0100 | [diff] [blame] | 665 | dev_err(dev, "Can't request %s clk (%d)", |
| 666 | fixed_clock_dev->name, err); |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 667 | return err; |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | * set pllm factor accordingly to the external oscillator |
| 672 | * frequency (HSE). For STM32F4 and STM32F7, we want VCO |
| 673 | * freq at 1MHz |
| 674 | * if input PLL frequency is 25Mhz, divide it by 25 |
| 675 | */ |
| 676 | clk.id = 0; |
| 677 | priv->hse_rate = clk_get_rate(&clk); |
| 678 | |
| 679 | if (priv->hse_rate < 1000000) { |
Patrick Delaunay | a57b8cd | 2020-11-06 19:01:46 +0100 | [diff] [blame] | 680 | dev_err(dev, "unexpected HSE clock rate = %ld \"n", |
| 681 | priv->hse_rate); |
Patrice Chotard | acd97ca | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 682 | return -EINVAL; |
| 683 | } |
| 684 | |
| 685 | priv->info.sys_pll_psc.pll_m = priv->hse_rate / 1000000; |
| 686 | |
| 687 | if (priv->info.has_overdrive) { |
Patrice Chotard | 1509d66 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 688 | err = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0, |
| 689 | &args); |
| 690 | if (err) { |
Patrick Delaunay | a57b8cd | 2020-11-06 19:01:46 +0100 | [diff] [blame] | 691 | dev_err(dev, "can't find syscon device (%d)\n", err); |
Patrice Chotard | 1509d66 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 692 | return err; |
| 693 | } |
Patrice Chotard | 22768d5 | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 694 | |
Patrice Chotard | 1509d66 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 695 | priv->pwr_regs = (struct stm32_pwr_regs *)ofnode_get_addr(args.node); |
| 696 | } |
Patrice Chotard | 22768d5 | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 697 | |
Patrice Chotard | d93fc2c | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 698 | configure_clocks(dev); |
Vikas Manocha | daaeaab | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
Simon Glass | b7ae277 | 2017-05-18 20:09:40 -0600 | [diff] [blame] | 703 | static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args) |
Vikas Manocha | daaeaab | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 704 | { |
Patrick Delaunay | a57b8cd | 2020-11-06 19:01:46 +0100 | [diff] [blame] | 705 | dev_dbg(clk->dev, "clk=%p\n", clk); |
Vikas Manocha | daaeaab | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 706 | |
| 707 | if (args->args_count != 2) { |
Sean Anderson | a1b654b | 2021-12-01 14:26:53 -0500 | [diff] [blame] | 708 | dev_dbg(clk->dev, "Invalid args_count: %d\n", args->args_count); |
Vikas Manocha | daaeaab | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 709 | return -EINVAL; |
| 710 | } |
| 711 | |
| 712 | if (args->args_count) |
| 713 | clk->id = args->args[1]; |
| 714 | else |
| 715 | clk->id = 0; |
| 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | static struct clk_ops stm32_clk_ops = { |
| 721 | .of_xlate = stm32_clk_of_xlate, |
| 722 | .enable = stm32_clk_enable, |
Patrice Chotard | 7bdf971 | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 723 | .get_rate = stm32_clk_get_rate, |
Patrice Chotard | 24e8578 | 2018-01-29 18:14:14 +0100 | [diff] [blame] | 724 | .set_rate = stm32_set_rate, |
Vikas Manocha | daaeaab | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 725 | }; |
| 726 | |
Patrice Chotard | d4f2d20 | 2017-11-15 13:14:48 +0100 | [diff] [blame] | 727 | U_BOOT_DRIVER(stm32fx_clk) = { |
Patrice Chotard | 03f10a1 | 2017-11-15 13:14:51 +0100 | [diff] [blame] | 728 | .name = "stm32fx_rcc_clock", |
Patrice Chotard | b323de5 | 2017-09-21 10:08:09 +0200 | [diff] [blame] | 729 | .id = UCLASS_CLK, |
Patrice Chotard | b323de5 | 2017-09-21 10:08:09 +0200 | [diff] [blame] | 730 | .ops = &stm32_clk_ops, |
| 731 | .probe = stm32_clk_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 732 | .priv_auto = sizeof(struct stm32_clk), |
Patrice Chotard | b323de5 | 2017-09-21 10:08:09 +0200 | [diff] [blame] | 733 | .flags = DM_FLAG_PRE_RELOC, |
Vikas Manocha | daaeaab | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 734 | }; |