Vikas Manocha | e66c49f | 2016-02-11 15:47:20 -0800 | [diff] [blame] | 1 | /* |
Patrice Chotard | 3bc599c | 2017-10-23 09:53:58 +0200 | [diff] [blame] | 2 | * Copyright (C) 2017, STMicroelectronics - All Rights Reserved |
| 3 | * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics. |
Vikas Manocha | e66c49f | 2016-02-11 15:47:20 -0800 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
Patrice Chotard | 3bc599c | 2017-10-23 09:53:58 +0200 | [diff] [blame] | 7 | |
Vikas Manocha | e66c49f | 2016-02-11 15:47:20 -0800 | [diff] [blame] | 8 | #include <common.h> |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 9 | #include <clk-uclass.h> |
| 10 | #include <dm.h> |
Patrice Chotard | 928954f | 2017-11-15 13:14:51 +0100 | [diff] [blame] | 11 | #include <stm32_rcc.h> |
Patrice Chotard | d0a768b | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 12 | |
Vikas Manocha | e66c49f | 2016-02-11 15:47:20 -0800 | [diff] [blame] | 13 | #include <asm/io.h> |
Vikas Manocha | e66c49f | 2016-02-11 15:47:20 -0800 | [diff] [blame] | 14 | #include <asm/arch/stm32.h> |
Patrice Chotard | d0a768b | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 15 | #include <asm/arch/stm32_pwr.h> |
Vikas Manocha | e66c49f | 2016-02-11 15:47:20 -0800 | [diff] [blame] | 16 | |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 17 | #include <dt-bindings/mfd/stm32f7-rcc.h> |
| 18 | |
Michael Kurz | bad5188 | 2017-01-22 16:04:24 +0100 | [diff] [blame] | 19 | #define RCC_CR_HSION BIT(0) |
| 20 | #define RCC_CR_HSEON BIT(16) |
| 21 | #define RCC_CR_HSERDY BIT(17) |
| 22 | #define RCC_CR_HSEBYP BIT(18) |
| 23 | #define RCC_CR_CSSON BIT(19) |
| 24 | #define RCC_CR_PLLON BIT(24) |
| 25 | #define RCC_CR_PLLRDY BIT(25) |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 26 | #define RCC_CR_PLLSAION BIT(28) |
| 27 | #define RCC_CR_PLLSAIRDY BIT(29) |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 28 | |
Michael Kurz | bad5188 | 2017-01-22 16:04:24 +0100 | [diff] [blame] | 29 | #define RCC_PLLCFGR_PLLM_MASK GENMASK(5, 0) |
| 30 | #define RCC_PLLCFGR_PLLN_MASK GENMASK(14, 6) |
| 31 | #define RCC_PLLCFGR_PLLP_MASK GENMASK(17, 16) |
| 32 | #define RCC_PLLCFGR_PLLQ_MASK GENMASK(27, 24) |
| 33 | #define RCC_PLLCFGR_PLLSRC BIT(22) |
| 34 | #define RCC_PLLCFGR_PLLM_SHIFT 0 |
| 35 | #define RCC_PLLCFGR_PLLN_SHIFT 6 |
| 36 | #define RCC_PLLCFGR_PLLP_SHIFT 16 |
| 37 | #define RCC_PLLCFGR_PLLQ_SHIFT 24 |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 38 | |
Michael Kurz | bad5188 | 2017-01-22 16:04:24 +0100 | [diff] [blame] | 39 | #define RCC_CFGR_AHB_PSC_MASK GENMASK(7, 4) |
| 40 | #define RCC_CFGR_APB1_PSC_MASK GENMASK(12, 10) |
| 41 | #define RCC_CFGR_APB2_PSC_MASK GENMASK(15, 13) |
| 42 | #define RCC_CFGR_SW0 BIT(0) |
| 43 | #define RCC_CFGR_SW1 BIT(1) |
| 44 | #define RCC_CFGR_SW_MASK GENMASK(1, 0) |
| 45 | #define RCC_CFGR_SW_HSI 0 |
| 46 | #define RCC_CFGR_SW_HSE RCC_CFGR_SW0 |
| 47 | #define RCC_CFGR_SW_PLL RCC_CFGR_SW1 |
| 48 | #define RCC_CFGR_SWS0 BIT(2) |
| 49 | #define RCC_CFGR_SWS1 BIT(3) |
| 50 | #define RCC_CFGR_SWS_MASK GENMASK(3, 2) |
| 51 | #define RCC_CFGR_SWS_HSI 0 |
| 52 | #define RCC_CFGR_SWS_HSE RCC_CFGR_SWS0 |
| 53 | #define RCC_CFGR_SWS_PLL RCC_CFGR_SWS1 |
| 54 | #define RCC_CFGR_HPRE_SHIFT 4 |
| 55 | #define RCC_CFGR_PPRE1_SHIFT 10 |
| 56 | #define RCC_CFGR_PPRE2_SHIFT 13 |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 57 | |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 58 | #define RCC_PLLCFGR_PLLSAIN_MASK GENMASK(14, 6) |
| 59 | #define RCC_PLLCFGR_PLLSAIP_MASK GENMASK(17, 16) |
| 60 | #define RCC_PLLSAICFGR_PLLSAIN_SHIFT 6 |
| 61 | #define RCC_PLLSAICFGR_PLLSAIP_SHIFT 16 |
| 62 | #define RCC_PLLSAICFGR_PLLSAIP_4 BIT(17) |
| 63 | #define RCC_PLLSAICFGR_PLLSAIQ_4 BIT(26) |
| 64 | #define RCC_PLLSAICFGR_PLLSAIR_2 BIT(29) |
| 65 | |
| 66 | #define RCC_DCKCFGRX_CK48MSEL BIT(27) |
| 67 | #define RCC_DCKCFGRX_SDMMC1SEL BIT(28) |
| 68 | #define RCC_DCKCFGR2_SDMMC2SEL BIT(29) |
| 69 | |
| 70 | #define RCC_APB2ENR_SAI1EN BIT(22) |
| 71 | |
Patrice Chotard | c88c6a9 | 2017-11-15 13:14:49 +0100 | [diff] [blame] | 72 | /* |
| 73 | * RCC AHB1ENR specific definitions |
| 74 | */ |
| 75 | #define RCC_AHB1ENR_ETHMAC_EN BIT(25) |
| 76 | #define RCC_AHB1ENR_ETHMAC_TX_EN BIT(26) |
| 77 | #define RCC_AHB1ENR_ETHMAC_RX_EN BIT(27) |
| 78 | |
| 79 | /* |
| 80 | * RCC APB1ENR specific definitions |
| 81 | */ |
| 82 | #define RCC_APB1ENR_TIM2EN BIT(0) |
| 83 | #define RCC_APB1ENR_PWREN BIT(28) |
| 84 | |
| 85 | /* |
| 86 | * RCC APB2ENR specific definitions |
| 87 | */ |
| 88 | #define RCC_APB2ENR_SYSCFGEN BIT(14) |
| 89 | |
Patrice Chotard | 4a56fd4 | 2017-12-12 09:49:39 +0100 | [diff] [blame] | 90 | enum periph_clock { |
| 91 | SYSCFG_CLOCK_CFG, |
| 92 | TIMER2_CLOCK_CFG, |
| 93 | STMMAC_CLOCK_CFG, |
| 94 | }; |
| 95 | |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 96 | struct stm32_clk_info stm32f4_clk_info = { |
| 97 | /* 180 MHz */ |
| 98 | .sys_pll_psc = { |
| 99 | .pll_m = 8, |
| 100 | .pll_n = 360, |
| 101 | .pll_p = 2, |
| 102 | .pll_q = 8, |
| 103 | .ahb_psc = AHB_PSC_1, |
| 104 | .apb1_psc = APB_PSC_4, |
| 105 | .apb2_psc = APB_PSC_2, |
| 106 | }, |
| 107 | .has_overdrive = false, |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 108 | .v2 = false, |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | struct stm32_clk_info stm32f7_clk_info = { |
| 112 | /* 200 MHz */ |
| 113 | .sys_pll_psc = { |
| 114 | .pll_m = 25, |
| 115 | .pll_n = 400, |
| 116 | .pll_p = 2, |
| 117 | .pll_q = 8, |
| 118 | .ahb_psc = AHB_PSC_1, |
| 119 | .apb1_psc = APB_PSC_4, |
| 120 | .apb2_psc = APB_PSC_2, |
| 121 | }, |
| 122 | .has_overdrive = true, |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 123 | .v2 = true, |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 124 | }; |
| 125 | |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 126 | struct stm32_clk { |
| 127 | struct stm32_rcc_regs *base; |
Patrice Chotard | d0a768b | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 128 | struct stm32_pwr_regs *pwr_regs; |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 129 | struct stm32_clk_info *info; |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 130 | }; |
| 131 | |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 132 | static int configure_clocks(struct udevice *dev) |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 133 | { |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 134 | struct stm32_clk *priv = dev_get_priv(dev); |
| 135 | struct stm32_rcc_regs *regs = priv->base; |
Patrice Chotard | d0a768b | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 136 | struct stm32_pwr_regs *pwr = priv->pwr_regs; |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 137 | struct pll_psc sys_pll_psc = priv->info->sys_pll_psc; |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 138 | u32 pllsaicfgr = 0; |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 139 | |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 140 | /* Reset RCC configuration */ |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 141 | setbits_le32(®s->cr, RCC_CR_HSION); |
| 142 | writel(0, ®s->cfgr); /* Reset CFGR */ |
| 143 | clrbits_le32(®s->cr, (RCC_CR_HSEON | RCC_CR_CSSON |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 144 | | RCC_CR_PLLON | RCC_CR_PLLSAION)); |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 145 | writel(0x24003010, ®s->pllcfgr); /* Reset value from RM */ |
| 146 | clrbits_le32(®s->cr, RCC_CR_HSEBYP); |
| 147 | writel(0, ®s->cir); /* Disable all interrupts */ |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 148 | |
| 149 | /* Configure for HSE+PLL operation */ |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 150 | setbits_le32(®s->cr, RCC_CR_HSEON); |
| 151 | while (!(readl(®s->cr) & RCC_CR_HSERDY)) |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 152 | ; |
| 153 | |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 154 | setbits_le32(®s->cfgr, (( |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 155 | sys_pll_psc.ahb_psc << RCC_CFGR_HPRE_SHIFT) |
| 156 | | (sys_pll_psc.apb1_psc << RCC_CFGR_PPRE1_SHIFT) |
| 157 | | (sys_pll_psc.apb2_psc << RCC_CFGR_PPRE2_SHIFT))); |
| 158 | |
| 159 | /* Configure the main PLL */ |
Patrice Chotard | 1543bf7 | 2017-10-26 13:23:19 +0200 | [diff] [blame] | 160 | setbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLSRC); /* pll source HSE */ |
| 161 | clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLM_MASK, |
| 162 | sys_pll_psc.pll_m << RCC_PLLCFGR_PLLM_SHIFT); |
| 163 | clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLN_MASK, |
| 164 | sys_pll_psc.pll_n << RCC_PLLCFGR_PLLN_SHIFT); |
| 165 | clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLP_MASK, |
| 166 | ((sys_pll_psc.pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT); |
| 167 | clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLQ_MASK, |
| 168 | sys_pll_psc.pll_q << RCC_PLLCFGR_PLLQ_SHIFT); |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 169 | |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 170 | /* Configure the SAI PLL to get a 48 MHz source */ |
| 171 | pllsaicfgr = RCC_PLLSAICFGR_PLLSAIR_2 | RCC_PLLSAICFGR_PLLSAIQ_4 | |
| 172 | RCC_PLLSAICFGR_PLLSAIP_4; |
| 173 | pllsaicfgr |= 192 << RCC_PLLSAICFGR_PLLSAIN_SHIFT; |
| 174 | writel(pllsaicfgr, ®s->pllsaicfgr); |
| 175 | |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 176 | /* Enable the main PLL */ |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 177 | setbits_le32(®s->cr, RCC_CR_PLLON); |
| 178 | while (!(readl(®s->cr) & RCC_CR_PLLRDY)) |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 179 | ; |
| 180 | |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 181 | if (priv->info->v2) { /*stm32f7 case */ |
| 182 | /* select PLLSAI as 48MHz clock source */ |
| 183 | setbits_le32(®s->dckcfgr2, RCC_DCKCFGRX_CK48MSEL); |
| 184 | |
| 185 | /* select 48MHz as SDMMC1 clock source */ |
| 186 | clrbits_le32(®s->dckcfgr2, RCC_DCKCFGRX_SDMMC1SEL); |
| 187 | |
| 188 | /* select 48MHz as SDMMC2 clock source */ |
| 189 | clrbits_le32(®s->dckcfgr2, RCC_DCKCFGR2_SDMMC2SEL); |
| 190 | } else { /* stm32f4 case */ |
| 191 | /* select PLLSAI as 48MHz clock source */ |
| 192 | setbits_le32(®s->dckcfgr, RCC_DCKCFGRX_CK48MSEL); |
| 193 | |
| 194 | /* select 48MHz as SDMMC1 clock source */ |
| 195 | clrbits_le32(®s->dckcfgr, RCC_DCKCFGRX_SDMMC1SEL); |
| 196 | } |
| 197 | |
| 198 | /* Enable the SAI PLL */ |
| 199 | setbits_le32(®s->cr, RCC_CR_PLLSAION); |
| 200 | while (!(readl(®s->cr) & RCC_CR_PLLSAIRDY)) |
| 201 | ; |
| 202 | |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 203 | setbits_le32(®s->apb1enr, RCC_APB1ENR_PWREN); |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 204 | |
| 205 | if (priv->info->has_overdrive) { |
| 206 | /* |
| 207 | * Enable high performance mode |
| 208 | * System frequency up to 200 MHz |
| 209 | */ |
| 210 | setbits_le32(&pwr->cr1, PWR_CR1_ODEN); |
| 211 | /* Infinite wait! */ |
| 212 | while (!(readl(&pwr->csr1) & PWR_CSR1_ODRDY)) |
| 213 | ; |
| 214 | /* Enable the Over-drive switch */ |
| 215 | setbits_le32(&pwr->cr1, PWR_CR1_ODSWEN); |
| 216 | /* Infinite wait! */ |
| 217 | while (!(readl(&pwr->csr1) & PWR_CSR1_ODSWRDY)) |
| 218 | ; |
| 219 | } |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 220 | |
| 221 | stm32_flash_latency_cfg(5); |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 222 | clrbits_le32(®s->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1)); |
| 223 | setbits_le32(®s->cfgr, RCC_CFGR_SW_PLL); |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 224 | |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 225 | while ((readl(®s->cfgr) & RCC_CFGR_SWS_MASK) != |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 226 | RCC_CFGR_SWS_PLL) |
| 227 | ; |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 228 | /* gate the SAI clock, needed for MMC 1&2 clocks */ |
| 229 | setbits_le32(®s->apb2enr, RCC_APB2ENR_SAI1EN); |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 234 | static unsigned long stm32_clk_pll48clk_rate(struct stm32_clk *priv, |
| 235 | u32 sysclk) |
| 236 | { |
| 237 | struct stm32_rcc_regs *regs = priv->base; |
| 238 | u16 pllq, pllm, pllsain, pllsaip; |
| 239 | bool pllsai; |
| 240 | |
| 241 | pllq = (readl(®s->pllcfgr) & RCC_PLLCFGR_PLLQ_MASK) |
| 242 | >> RCC_PLLCFGR_PLLQ_SHIFT; |
| 243 | |
| 244 | if (priv->info->v2) /*stm32f7 case */ |
| 245 | pllsai = readl(®s->dckcfgr2) & RCC_DCKCFGRX_CK48MSEL; |
| 246 | else |
| 247 | pllsai = readl(®s->dckcfgr) & RCC_DCKCFGRX_CK48MSEL; |
| 248 | |
| 249 | if (pllsai) { |
| 250 | /* PLL48CLK is selected from PLLSAI, get PLLSAI value */ |
| 251 | pllm = (readl(®s->pllcfgr) & RCC_PLLCFGR_PLLM_MASK); |
| 252 | pllsain = ((readl(®s->pllsaicfgr) & RCC_PLLCFGR_PLLSAIN_MASK) |
| 253 | >> RCC_PLLSAICFGR_PLLSAIN_SHIFT); |
| 254 | pllsaip = ((((readl(®s->pllsaicfgr) & RCC_PLLCFGR_PLLSAIP_MASK) |
| 255 | >> RCC_PLLSAICFGR_PLLSAIP_SHIFT) + 1) << 1); |
| 256 | return ((CONFIG_STM32_HSE_HZ / pllm) * pllsain) / pllsaip; |
| 257 | } |
| 258 | /* PLL48CLK is selected from PLLQ */ |
| 259 | return sysclk / pllq; |
| 260 | } |
| 261 | |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 262 | static unsigned long stm32_clk_get_rate(struct clk *clk) |
| 263 | { |
| 264 | struct stm32_clk *priv = dev_get_priv(clk->dev); |
| 265 | struct stm32_rcc_regs *regs = priv->base; |
| 266 | u32 sysclk = 0; |
| 267 | u32 shift = 0; |
Patrice Chotard | f264e23 | 2017-11-15 13:14:48 +0100 | [diff] [blame] | 268 | u16 pllm, plln, pllp; |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 269 | /* Prescaler table lookups for clock computation */ |
| 270 | u8 ahb_psc_table[16] = { |
| 271 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9 |
| 272 | }; |
| 273 | u8 apb_psc_table[8] = { |
| 274 | 0, 0, 0, 0, 1, 2, 3, 4 |
| 275 | }; |
| 276 | |
| 277 | if ((readl(®s->cfgr) & RCC_CFGR_SWS_MASK) == |
| 278 | RCC_CFGR_SWS_PLL) { |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 279 | pllm = (readl(®s->pllcfgr) & RCC_PLLCFGR_PLLM_MASK); |
| 280 | plln = ((readl(®s->pllcfgr) & RCC_PLLCFGR_PLLN_MASK) |
| 281 | >> RCC_PLLCFGR_PLLN_SHIFT); |
| 282 | pllp = ((((readl(®s->pllcfgr) & RCC_PLLCFGR_PLLP_MASK) |
| 283 | >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1); |
| 284 | sysclk = ((CONFIG_STM32_HSE_HZ / pllm) * plln) / pllp; |
| 285 | } else { |
| 286 | return -EINVAL; |
| 287 | } |
| 288 | |
| 289 | switch (clk->id) { |
| 290 | /* |
| 291 | * AHB CLOCK: 3 x 32 bits consecutive registers are used : |
| 292 | * AHB1, AHB2 and AHB3 |
| 293 | */ |
| 294 | case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI): |
| 295 | shift = ahb_psc_table[( |
| 296 | (readl(®s->cfgr) & RCC_CFGR_AHB_PSC_MASK) |
| 297 | >> RCC_CFGR_HPRE_SHIFT)]; |
| 298 | return sysclk >>= shift; |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 299 | /* APB1 CLOCK */ |
| 300 | case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8): |
| 301 | shift = apb_psc_table[( |
| 302 | (readl(®s->cfgr) & RCC_CFGR_APB1_PSC_MASK) |
| 303 | >> RCC_CFGR_PPRE1_SHIFT)]; |
| 304 | return sysclk >>= shift; |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 305 | /* APB2 CLOCK */ |
| 306 | case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(LTDC): |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 307 | /* |
| 308 | * particular case for SDMMC1 and SDMMC2 : |
| 309 | * 48Mhz source clock can be from main PLL or from |
| 310 | * SAI PLL |
| 311 | */ |
| 312 | switch (clk->id) { |
| 313 | case STM32F7_APB2_CLOCK(SDMMC1): |
| 314 | if (readl(®s->dckcfgr2) & RCC_DCKCFGRX_SDMMC1SEL) |
| 315 | /* System clock is selected as SDMMC1 clock */ |
| 316 | return sysclk; |
| 317 | else |
| 318 | return stm32_clk_pll48clk_rate(priv, sysclk); |
| 319 | break; |
| 320 | case STM32F7_APB2_CLOCK(SDMMC2): |
| 321 | if (readl(®s->dckcfgr2) & RCC_DCKCFGR2_SDMMC2SEL) |
| 322 | /* System clock is selected as SDMMC2 clock */ |
| 323 | return sysclk; |
| 324 | else |
| 325 | return stm32_clk_pll48clk_rate(priv, sysclk); |
| 326 | break; |
| 327 | } |
| 328 | |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 329 | shift = apb_psc_table[( |
| 330 | (readl(®s->cfgr) & RCC_CFGR_APB2_PSC_MASK) |
| 331 | >> RCC_CFGR_PPRE2_SHIFT)]; |
| 332 | return sysclk >>= shift; |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 333 | default: |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 334 | pr_err("clock index %ld out of range\n", clk->id); |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 335 | return -EINVAL; |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 339 | static int stm32_clk_enable(struct clk *clk) |
| 340 | { |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 341 | struct stm32_clk *priv = dev_get_priv(clk->dev); |
| 342 | struct stm32_rcc_regs *regs = priv->base; |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 343 | u32 offset = clk->id / 32; |
| 344 | u32 bit_index = clk->id % 32; |
| 345 | |
| 346 | debug("%s: clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n", |
| 347 | __func__, clk->id, offset, bit_index); |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 348 | setbits_le32(®s->ahb1enr + offset, BIT(bit_index)); |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 349 | |
| 350 | return 0; |
| 351 | } |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 352 | |
Vikas Manocha | e66c49f | 2016-02-11 15:47:20 -0800 | [diff] [blame] | 353 | void clock_setup(int peripheral) |
| 354 | { |
| 355 | switch (peripheral) { |
Michael Kurz | 081de09 | 2017-01-22 16:04:26 +0100 | [diff] [blame] | 356 | case SYSCFG_CLOCK_CFG: |
| 357 | setbits_le32(&STM32_RCC->apb2enr, RCC_APB2ENR_SYSCFGEN); |
| 358 | break; |
| 359 | case TIMER2_CLOCK_CFG: |
| 360 | setbits_le32(&STM32_RCC->apb1enr, RCC_APB1ENR_TIM2EN); |
| 361 | break; |
Michael Kurz | b20b70f | 2017-01-22 16:04:27 +0100 | [diff] [blame] | 362 | case STMMAC_CLOCK_CFG: |
| 363 | setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_EN); |
| 364 | setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_RX_EN); |
| 365 | setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_TX_EN); |
| 366 | break; |
Vikas Manocha | e66c49f | 2016-02-11 15:47:20 -0800 | [diff] [blame] | 367 | default: |
| 368 | break; |
| 369 | } |
| 370 | } |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 371 | |
| 372 | static int stm32_clk_probe(struct udevice *dev) |
| 373 | { |
Patrice Chotard | d0a768b | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 374 | struct ofnode_phandle_args args; |
| 375 | int err; |
| 376 | |
Patrice Chotard | f264e23 | 2017-11-15 13:14:48 +0100 | [diff] [blame] | 377 | debug("%s\n", __func__); |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 378 | |
| 379 | struct stm32_clk *priv = dev_get_priv(dev); |
| 380 | fdt_addr_t addr; |
| 381 | |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 382 | addr = dev_read_addr(dev); |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 383 | if (addr == FDT_ADDR_T_NONE) |
| 384 | return -EINVAL; |
| 385 | |
| 386 | priv->base = (struct stm32_rcc_regs *)addr; |
Patrice Chotard | 928954f | 2017-11-15 13:14:51 +0100 | [diff] [blame] | 387 | |
| 388 | switch (dev_get_driver_data(dev)) { |
| 389 | case STM32F4: |
| 390 | priv->info = &stm32f4_clk_info; |
| 391 | break; |
| 392 | case STM32F7: |
| 393 | priv->info = &stm32f7_clk_info; |
| 394 | break; |
| 395 | default: |
| 396 | return -EINVAL; |
| 397 | } |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 398 | |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 399 | if (priv->info->has_overdrive) { |
| 400 | err = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0, |
| 401 | &args); |
| 402 | if (err) { |
| 403 | debug("%s: can't find syscon device (%d)\n", __func__, |
| 404 | err); |
| 405 | return err; |
| 406 | } |
| 407 | |
| 408 | priv->pwr_regs = (struct stm32_pwr_regs *)ofnode_get_addr(args.node); |
Patrice Chotard | d0a768b | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 411 | configure_clocks(dev); |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
Simon Glass | a4e0ef5 | 2017-05-18 20:09:40 -0600 | [diff] [blame] | 416 | static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args) |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 417 | { |
| 418 | debug("%s(clk=%p)\n", __func__, clk); |
| 419 | |
| 420 | if (args->args_count != 2) { |
| 421 | debug("Invaild args_count: %d\n", args->args_count); |
| 422 | return -EINVAL; |
| 423 | } |
| 424 | |
| 425 | if (args->args_count) |
| 426 | clk->id = args->args[1]; |
| 427 | else |
| 428 | clk->id = 0; |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static struct clk_ops stm32_clk_ops = { |
| 434 | .of_xlate = stm32_clk_of_xlate, |
| 435 | .enable = stm32_clk_enable, |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 436 | .get_rate = stm32_clk_get_rate, |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 437 | }; |
| 438 | |
Patrice Chotard | f264e23 | 2017-11-15 13:14:48 +0100 | [diff] [blame] | 439 | U_BOOT_DRIVER(stm32fx_clk) = { |
Patrice Chotard | 928954f | 2017-11-15 13:14:51 +0100 | [diff] [blame] | 440 | .name = "stm32fx_rcc_clock", |
Patrice Chotard | 0cc40df | 2017-09-21 10:08:09 +0200 | [diff] [blame] | 441 | .id = UCLASS_CLK, |
Patrice Chotard | 0cc40df | 2017-09-21 10:08:09 +0200 | [diff] [blame] | 442 | .ops = &stm32_clk_ops, |
| 443 | .probe = stm32_clk_probe, |
| 444 | .priv_auto_alloc_size = sizeof(struct stm32_clk), |
| 445 | .flags = DM_FLAG_PRE_RELOC, |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 446 | }; |