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