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 |
Patrice Chotard | 990dba6 | 2018-01-19 18:02:40 +0100 | [diff] [blame] | 62 | #define RCC_PLLSAICFGR_PLLSAIP_4 BIT(16) |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 63 | #define RCC_PLLSAICFGR_PLLSAIQ_4 BIT(26) |
| 64 | #define RCC_PLLSAICFGR_PLLSAIR_2 BIT(29) |
| 65 | |
Patrice Chotard | 61803a9 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 66 | #define RCC_DCKCFGRX_TIMPRE BIT(24) |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 67 | #define RCC_DCKCFGRX_CK48MSEL BIT(27) |
| 68 | #define RCC_DCKCFGRX_SDMMC1SEL BIT(28) |
| 69 | #define RCC_DCKCFGR2_SDMMC2SEL BIT(29) |
| 70 | |
Patrice Chotard | c88c6a9 | 2017-11-15 13:14:49 +0100 | [diff] [blame] | 71 | /* |
| 72 | * RCC AHB1ENR specific definitions |
| 73 | */ |
| 74 | #define RCC_AHB1ENR_ETHMAC_EN BIT(25) |
| 75 | #define RCC_AHB1ENR_ETHMAC_TX_EN BIT(26) |
| 76 | #define RCC_AHB1ENR_ETHMAC_RX_EN BIT(27) |
| 77 | |
| 78 | /* |
| 79 | * RCC APB1ENR specific definitions |
| 80 | */ |
| 81 | #define RCC_APB1ENR_TIM2EN BIT(0) |
| 82 | #define RCC_APB1ENR_PWREN BIT(28) |
| 83 | |
| 84 | /* |
| 85 | * RCC APB2ENR specific definitions |
| 86 | */ |
| 87 | #define RCC_APB2ENR_SYSCFGEN BIT(14) |
Patrice Chotard | 20fe38e | 2018-01-18 14:10:05 +0100 | [diff] [blame] | 88 | #define RCC_APB2ENR_SAI1EN BIT(22) |
Patrice Chotard | c88c6a9 | 2017-11-15 13:14:49 +0100 | [diff] [blame] | 89 | |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 90 | static const struct stm32_clk_info stm32f4_clk_info = { |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 91 | /* 180 MHz */ |
| 92 | .sys_pll_psc = { |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 93 | .pll_n = 360, |
| 94 | .pll_p = 2, |
| 95 | .pll_q = 8, |
| 96 | .ahb_psc = AHB_PSC_1, |
| 97 | .apb1_psc = APB_PSC_4, |
| 98 | .apb2_psc = APB_PSC_2, |
| 99 | }, |
| 100 | .has_overdrive = false, |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 101 | .v2 = false, |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 104 | static const struct stm32_clk_info stm32f7_clk_info = { |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 105 | /* 200 MHz */ |
| 106 | .sys_pll_psc = { |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 107 | .pll_n = 400, |
| 108 | .pll_p = 2, |
| 109 | .pll_q = 8, |
| 110 | .ahb_psc = AHB_PSC_1, |
| 111 | .apb1_psc = APB_PSC_4, |
| 112 | .apb2_psc = APB_PSC_2, |
| 113 | }, |
| 114 | .has_overdrive = true, |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 115 | .v2 = true, |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 116 | }; |
| 117 | |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 118 | struct stm32_clk { |
| 119 | struct stm32_rcc_regs *base; |
Patrice Chotard | d0a768b | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 120 | struct stm32_pwr_regs *pwr_regs; |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 121 | struct stm32_clk_info info; |
| 122 | unsigned long hse_rate; |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 123 | }; |
| 124 | |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 125 | static int configure_clocks(struct udevice *dev) |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 126 | { |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 127 | struct stm32_clk *priv = dev_get_priv(dev); |
| 128 | struct stm32_rcc_regs *regs = priv->base; |
Patrice Chotard | d0a768b | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 129 | struct stm32_pwr_regs *pwr = priv->pwr_regs; |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 130 | struct pll_psc *sys_pll_psc = &priv->info.sys_pll_psc; |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 131 | u32 pllsaicfgr = 0; |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 132 | |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 133 | /* Reset RCC configuration */ |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 134 | setbits_le32(®s->cr, RCC_CR_HSION); |
| 135 | writel(0, ®s->cfgr); /* Reset CFGR */ |
| 136 | clrbits_le32(®s->cr, (RCC_CR_HSEON | RCC_CR_CSSON |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 137 | | RCC_CR_PLLON | RCC_CR_PLLSAION)); |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 138 | writel(0x24003010, ®s->pllcfgr); /* Reset value from RM */ |
| 139 | clrbits_le32(®s->cr, RCC_CR_HSEBYP); |
| 140 | writel(0, ®s->cir); /* Disable all interrupts */ |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 141 | |
| 142 | /* Configure for HSE+PLL operation */ |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 143 | setbits_le32(®s->cr, RCC_CR_HSEON); |
| 144 | while (!(readl(®s->cr) & RCC_CR_HSERDY)) |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 145 | ; |
| 146 | |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 147 | setbits_le32(®s->cfgr, (( |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 148 | sys_pll_psc->ahb_psc << RCC_CFGR_HPRE_SHIFT) |
| 149 | | (sys_pll_psc->apb1_psc << RCC_CFGR_PPRE1_SHIFT) |
| 150 | | (sys_pll_psc->apb2_psc << RCC_CFGR_PPRE2_SHIFT))); |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 151 | |
| 152 | /* Configure the main PLL */ |
Patrice Chotard | 1543bf7 | 2017-10-26 13:23:19 +0200 | [diff] [blame] | 153 | setbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLSRC); /* pll source HSE */ |
| 154 | clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLM_MASK, |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 155 | sys_pll_psc->pll_m << RCC_PLLCFGR_PLLM_SHIFT); |
Patrice Chotard | 1543bf7 | 2017-10-26 13:23:19 +0200 | [diff] [blame] | 156 | clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLN_MASK, |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 157 | sys_pll_psc->pll_n << RCC_PLLCFGR_PLLN_SHIFT); |
Patrice Chotard | 1543bf7 | 2017-10-26 13:23:19 +0200 | [diff] [blame] | 158 | clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLP_MASK, |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 159 | ((sys_pll_psc->pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT); |
Patrice Chotard | 1543bf7 | 2017-10-26 13:23:19 +0200 | [diff] [blame] | 160 | clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLQ_MASK, |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 161 | sys_pll_psc->pll_q << RCC_PLLCFGR_PLLQ_SHIFT); |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 162 | |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 163 | /* Configure the SAI PLL to get a 48 MHz source */ |
| 164 | pllsaicfgr = RCC_PLLSAICFGR_PLLSAIR_2 | RCC_PLLSAICFGR_PLLSAIQ_4 | |
| 165 | RCC_PLLSAICFGR_PLLSAIP_4; |
| 166 | pllsaicfgr |= 192 << RCC_PLLSAICFGR_PLLSAIN_SHIFT; |
| 167 | writel(pllsaicfgr, ®s->pllsaicfgr); |
| 168 | |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 169 | /* Enable the main PLL */ |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 170 | setbits_le32(®s->cr, RCC_CR_PLLON); |
| 171 | while (!(readl(®s->cr) & RCC_CR_PLLRDY)) |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 172 | ; |
| 173 | |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 174 | if (priv->info.v2) { /*stm32f7 case */ |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 175 | /* select PLLSAI as 48MHz clock source */ |
| 176 | setbits_le32(®s->dckcfgr2, RCC_DCKCFGRX_CK48MSEL); |
| 177 | |
| 178 | /* select 48MHz as SDMMC1 clock source */ |
| 179 | clrbits_le32(®s->dckcfgr2, RCC_DCKCFGRX_SDMMC1SEL); |
| 180 | |
| 181 | /* select 48MHz as SDMMC2 clock source */ |
| 182 | clrbits_le32(®s->dckcfgr2, RCC_DCKCFGR2_SDMMC2SEL); |
| 183 | } else { /* stm32f4 case */ |
| 184 | /* select PLLSAI as 48MHz clock source */ |
| 185 | setbits_le32(®s->dckcfgr, RCC_DCKCFGRX_CK48MSEL); |
| 186 | |
| 187 | /* select 48MHz as SDMMC1 clock source */ |
| 188 | clrbits_le32(®s->dckcfgr, RCC_DCKCFGRX_SDMMC1SEL); |
| 189 | } |
| 190 | |
| 191 | /* Enable the SAI PLL */ |
| 192 | setbits_le32(®s->cr, RCC_CR_PLLSAION); |
| 193 | while (!(readl(®s->cr) & RCC_CR_PLLSAIRDY)) |
| 194 | ; |
| 195 | |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 196 | setbits_le32(®s->apb1enr, RCC_APB1ENR_PWREN); |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 197 | |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 198 | if (priv->info.has_overdrive) { |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 199 | /* |
| 200 | * Enable high performance mode |
| 201 | * System frequency up to 200 MHz |
| 202 | */ |
| 203 | setbits_le32(&pwr->cr1, PWR_CR1_ODEN); |
| 204 | /* Infinite wait! */ |
| 205 | while (!(readl(&pwr->csr1) & PWR_CSR1_ODRDY)) |
| 206 | ; |
| 207 | /* Enable the Over-drive switch */ |
| 208 | setbits_le32(&pwr->cr1, PWR_CR1_ODSWEN); |
| 209 | /* Infinite wait! */ |
| 210 | while (!(readl(&pwr->csr1) & PWR_CSR1_ODSWRDY)) |
| 211 | ; |
| 212 | } |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 213 | |
| 214 | stm32_flash_latency_cfg(5); |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 215 | clrbits_le32(®s->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1)); |
| 216 | setbits_le32(®s->cfgr, RCC_CFGR_SW_PLL); |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 217 | |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 218 | while ((readl(®s->cfgr) & RCC_CFGR_SWS_MASK) != |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 219 | RCC_CFGR_SWS_PLL) |
| 220 | ; |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 221 | /* gate the SAI clock, needed for MMC 1&2 clocks */ |
| 222 | setbits_le32(®s->apb2enr, RCC_APB2ENR_SAI1EN); |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 223 | |
Patrice Chotard | 20fe38e | 2018-01-18 14:10:05 +0100 | [diff] [blame] | 224 | #ifdef CONFIG_ETH_DESIGNWARE |
| 225 | /* gate the SYSCFG clock, needed to set RMII ethernet interface */ |
| 226 | setbits_le32(®s->apb2enr, RCC_APB2ENR_SYSCFGEN); |
| 227 | #endif |
| 228 | |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 229 | return 0; |
| 230 | } |
| 231 | |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 232 | static unsigned long stm32_clk_pll48clk_rate(struct stm32_clk *priv, |
Patrice Chotard | aa230be | 2018-02-08 17:20:45 +0100 | [diff] [blame^] | 233 | u32 vco) |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 234 | { |
| 235 | struct stm32_rcc_regs *regs = priv->base; |
| 236 | u16 pllq, pllm, pllsain, pllsaip; |
| 237 | bool pllsai; |
| 238 | |
| 239 | pllq = (readl(®s->pllcfgr) & RCC_PLLCFGR_PLLQ_MASK) |
| 240 | >> RCC_PLLCFGR_PLLQ_SHIFT; |
| 241 | |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 242 | if (priv->info.v2) /*stm32f7 case */ |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 243 | pllsai = readl(®s->dckcfgr2) & RCC_DCKCFGRX_CK48MSEL; |
| 244 | else |
| 245 | pllsai = readl(®s->dckcfgr) & RCC_DCKCFGRX_CK48MSEL; |
| 246 | |
| 247 | if (pllsai) { |
| 248 | /* PLL48CLK is selected from PLLSAI, get PLLSAI value */ |
| 249 | pllm = (readl(®s->pllcfgr) & RCC_PLLCFGR_PLLM_MASK); |
| 250 | pllsain = ((readl(®s->pllsaicfgr) & RCC_PLLCFGR_PLLSAIN_MASK) |
| 251 | >> RCC_PLLSAICFGR_PLLSAIN_SHIFT); |
| 252 | pllsaip = ((((readl(®s->pllsaicfgr) & RCC_PLLCFGR_PLLSAIP_MASK) |
| 253 | >> RCC_PLLSAICFGR_PLLSAIP_SHIFT) + 1) << 1); |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 254 | return ((priv->hse_rate / pllm) * pllsain) / pllsaip; |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 255 | } |
| 256 | /* PLL48CLK is selected from PLLQ */ |
Patrice Chotard | aa230be | 2018-02-08 17:20:45 +0100 | [diff] [blame^] | 257 | return vco / pllq; |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Patrice Chotard | 61803a9 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 260 | static bool stm32_get_timpre(struct stm32_clk *priv) |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 261 | { |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 262 | struct stm32_rcc_regs *regs = priv->base; |
Patrice Chotard | 61803a9 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 263 | u32 val; |
| 264 | |
| 265 | if (priv->info.v2) /*stm32f7 case */ |
| 266 | val = readl(®s->dckcfgr2); |
| 267 | else |
| 268 | val = readl(®s->dckcfgr); |
| 269 | /* get timer prescaler */ |
| 270 | return !!(val & RCC_DCKCFGRX_TIMPRE); |
| 271 | } |
| 272 | |
| 273 | static u32 stm32_get_hclk_rate(struct stm32_rcc_regs *regs, u32 sysclk) |
| 274 | { |
| 275 | u8 shift; |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 276 | /* Prescaler table lookups for clock computation */ |
| 277 | u8 ahb_psc_table[16] = { |
| 278 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9 |
| 279 | }; |
Patrice Chotard | 61803a9 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 280 | |
| 281 | shift = ahb_psc_table[( |
| 282 | (readl(®s->cfgr) & RCC_CFGR_AHB_PSC_MASK) |
| 283 | >> RCC_CFGR_HPRE_SHIFT)]; |
| 284 | |
| 285 | return sysclk >> shift; |
| 286 | }; |
| 287 | |
| 288 | static u8 stm32_get_apb_shift(struct stm32_rcc_regs *regs, enum apb apb) |
| 289 | { |
| 290 | /* Prescaler table lookups for clock computation */ |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 291 | u8 apb_psc_table[8] = { |
| 292 | 0, 0, 0, 0, 1, 2, 3, 4 |
| 293 | }; |
| 294 | |
Patrice Chotard | 61803a9 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 295 | if (apb == APB1) |
| 296 | return apb_psc_table[( |
| 297 | (readl(®s->cfgr) & RCC_CFGR_APB1_PSC_MASK) |
| 298 | >> RCC_CFGR_PPRE1_SHIFT)]; |
| 299 | else /* APB2 */ |
| 300 | return apb_psc_table[( |
| 301 | (readl(®s->cfgr) & RCC_CFGR_APB2_PSC_MASK) |
| 302 | >> RCC_CFGR_PPRE2_SHIFT)]; |
| 303 | }; |
| 304 | |
| 305 | static u32 stm32_get_timer_rate(struct stm32_clk *priv, u32 sysclk, |
| 306 | enum apb apb) |
| 307 | { |
| 308 | struct stm32_rcc_regs *regs = priv->base; |
| 309 | u8 shift = stm32_get_apb_shift(regs, apb); |
| 310 | |
| 311 | if (stm32_get_timpre(priv)) |
| 312 | /* |
| 313 | * if APB prescaler is configured to a |
| 314 | * division factor of 1, 2 or 4 |
| 315 | */ |
| 316 | switch (shift) { |
| 317 | case 0: |
| 318 | case 1: |
| 319 | case 2: |
| 320 | return stm32_get_hclk_rate(regs, sysclk); |
| 321 | default: |
| 322 | return (sysclk >> shift) * 4; |
| 323 | } |
| 324 | else |
| 325 | /* |
| 326 | * if APB prescaler is configured to a |
| 327 | * division factor of 1 |
| 328 | */ |
| 329 | if (shift == 0) |
| 330 | return sysclk; |
| 331 | else |
| 332 | return (sysclk >> shift) * 2; |
| 333 | }; |
| 334 | |
| 335 | static ulong stm32_clk_get_rate(struct clk *clk) |
| 336 | { |
| 337 | struct stm32_clk *priv = dev_get_priv(clk->dev); |
| 338 | struct stm32_rcc_regs *regs = priv->base; |
| 339 | u32 sysclk = 0; |
Patrice Chotard | aa230be | 2018-02-08 17:20:45 +0100 | [diff] [blame^] | 340 | u32 vco; |
Patrice Chotard | 61803a9 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 341 | u16 pllm, plln, pllp; |
| 342 | |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 343 | if ((readl(®s->cfgr) & RCC_CFGR_SWS_MASK) == |
| 344 | RCC_CFGR_SWS_PLL) { |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 345 | pllm = (readl(®s->pllcfgr) & RCC_PLLCFGR_PLLM_MASK); |
| 346 | plln = ((readl(®s->pllcfgr) & RCC_PLLCFGR_PLLN_MASK) |
| 347 | >> RCC_PLLCFGR_PLLN_SHIFT); |
| 348 | pllp = ((((readl(®s->pllcfgr) & RCC_PLLCFGR_PLLP_MASK) |
| 349 | >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1); |
Patrice Chotard | aa230be | 2018-02-08 17:20:45 +0100 | [diff] [blame^] | 350 | vco = (priv->hse_rate / pllm) * plln; |
| 351 | sysclk = vco / pllp; |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 352 | } else { |
| 353 | return -EINVAL; |
| 354 | } |
| 355 | |
| 356 | switch (clk->id) { |
| 357 | /* |
| 358 | * AHB CLOCK: 3 x 32 bits consecutive registers are used : |
| 359 | * AHB1, AHB2 and AHB3 |
| 360 | */ |
| 361 | case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI): |
Patrice Chotard | 61803a9 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 362 | return stm32_get_hclk_rate(regs, sysclk); |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 363 | /* APB1 CLOCK */ |
| 364 | case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8): |
Patrice Chotard | 61803a9 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 365 | /* For timer clock, an additionnal prescaler is used*/ |
| 366 | switch (clk->id) { |
| 367 | case STM32F7_APB1_CLOCK(TIM2): |
| 368 | case STM32F7_APB1_CLOCK(TIM3): |
| 369 | case STM32F7_APB1_CLOCK(TIM4): |
| 370 | case STM32F7_APB1_CLOCK(TIM5): |
| 371 | case STM32F7_APB1_CLOCK(TIM6): |
| 372 | case STM32F7_APB1_CLOCK(TIM7): |
| 373 | case STM32F7_APB1_CLOCK(TIM12): |
| 374 | case STM32F7_APB1_CLOCK(TIM13): |
| 375 | case STM32F7_APB1_CLOCK(TIM14): |
| 376 | return stm32_get_timer_rate(priv, sysclk, APB1); |
| 377 | } |
| 378 | return (sysclk >> stm32_get_apb_shift(regs, APB1)); |
| 379 | |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 380 | /* APB2 CLOCK */ |
| 381 | case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(LTDC): |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 382 | /* |
| 383 | * particular case for SDMMC1 and SDMMC2 : |
| 384 | * 48Mhz source clock can be from main PLL or from |
| 385 | * SAI PLL |
| 386 | */ |
| 387 | switch (clk->id) { |
| 388 | case STM32F7_APB2_CLOCK(SDMMC1): |
| 389 | if (readl(®s->dckcfgr2) & RCC_DCKCFGRX_SDMMC1SEL) |
| 390 | /* System clock is selected as SDMMC1 clock */ |
| 391 | return sysclk; |
| 392 | else |
Patrice Chotard | aa230be | 2018-02-08 17:20:45 +0100 | [diff] [blame^] | 393 | return stm32_clk_pll48clk_rate(priv, vco); |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 394 | break; |
| 395 | case STM32F7_APB2_CLOCK(SDMMC2): |
| 396 | if (readl(®s->dckcfgr2) & RCC_DCKCFGR2_SDMMC2SEL) |
| 397 | /* System clock is selected as SDMMC2 clock */ |
| 398 | return sysclk; |
| 399 | else |
Patrice Chotard | aa230be | 2018-02-08 17:20:45 +0100 | [diff] [blame^] | 400 | return stm32_clk_pll48clk_rate(priv, vco); |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 401 | break; |
Patrice Chotard | 4e97e25 | 2017-11-15 13:14:52 +0100 | [diff] [blame] | 402 | |
Patrice Chotard | 61803a9 | 2018-02-07 10:44:46 +0100 | [diff] [blame] | 403 | /* For timer clock, an additionnal prescaler is used*/ |
| 404 | case STM32F7_APB2_CLOCK(TIM1): |
| 405 | case STM32F7_APB2_CLOCK(TIM8): |
| 406 | case STM32F7_APB2_CLOCK(TIM9): |
| 407 | case STM32F7_APB2_CLOCK(TIM10): |
| 408 | case STM32F7_APB2_CLOCK(TIM11): |
| 409 | return stm32_get_timer_rate(priv, sysclk, APB2); |
| 410 | break; |
| 411 | } |
| 412 | return (sysclk >> stm32_get_apb_shift(regs, APB2)); |
| 413 | |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 414 | default: |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 415 | pr_err("clock index %ld out of range\n", clk->id); |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 416 | return -EINVAL; |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
Patrice Chotard | 05e23dd | 2018-01-29 18:14:14 +0100 | [diff] [blame] | 420 | static ulong stm32_set_rate(struct clk *clk, ulong rate) |
| 421 | { |
| 422 | return 0; |
| 423 | } |
| 424 | |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 425 | static int stm32_clk_enable(struct clk *clk) |
| 426 | { |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 427 | struct stm32_clk *priv = dev_get_priv(clk->dev); |
| 428 | struct stm32_rcc_regs *regs = priv->base; |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 429 | u32 offset = clk->id / 32; |
| 430 | u32 bit_index = clk->id % 32; |
| 431 | |
| 432 | debug("%s: clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n", |
| 433 | __func__, clk->id, offset, bit_index); |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 434 | setbits_le32(®s->ahb1enr + offset, BIT(bit_index)); |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 435 | |
| 436 | return 0; |
| 437 | } |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 438 | |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 439 | static int stm32_clk_probe(struct udevice *dev) |
| 440 | { |
Patrice Chotard | d0a768b | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 441 | struct ofnode_phandle_args args; |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 442 | struct udevice *fixed_clock_dev = NULL; |
| 443 | struct clk clk; |
Patrice Chotard | d0a768b | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 444 | int err; |
| 445 | |
Patrice Chotard | f264e23 | 2017-11-15 13:14:48 +0100 | [diff] [blame] | 446 | debug("%s\n", __func__); |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 447 | |
| 448 | struct stm32_clk *priv = dev_get_priv(dev); |
| 449 | fdt_addr_t addr; |
| 450 | |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 451 | addr = dev_read_addr(dev); |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 452 | if (addr == FDT_ADDR_T_NONE) |
| 453 | return -EINVAL; |
| 454 | |
| 455 | priv->base = (struct stm32_rcc_regs *)addr; |
Patrice Chotard | 928954f | 2017-11-15 13:14:51 +0100 | [diff] [blame] | 456 | |
| 457 | switch (dev_get_driver_data(dev)) { |
| 458 | case STM32F4: |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 459 | memcpy(&priv->info, &stm32f4_clk_info, |
| 460 | sizeof(struct stm32_clk_info)); |
Patrice Chotard | 928954f | 2017-11-15 13:14:51 +0100 | [diff] [blame] | 461 | break; |
| 462 | case STM32F7: |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 463 | memcpy(&priv->info, &stm32f7_clk_info, |
| 464 | sizeof(struct stm32_clk_info)); |
Patrice Chotard | 928954f | 2017-11-15 13:14:51 +0100 | [diff] [blame] | 465 | break; |
| 466 | default: |
| 467 | return -EINVAL; |
| 468 | } |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 469 | |
Patrice Chotard | cb97ff9 | 2018-01-18 13:39:30 +0100 | [diff] [blame] | 470 | /* retrieve HSE frequency (external oscillator) */ |
| 471 | err = uclass_get_device_by_name(UCLASS_CLK, "clk-hse", |
| 472 | &fixed_clock_dev); |
| 473 | |
| 474 | if (err) { |
| 475 | pr_err("Can't find fixed clock (%d)", err); |
| 476 | return err; |
| 477 | } |
| 478 | |
| 479 | err = clk_request(fixed_clock_dev, &clk); |
| 480 | if (err) { |
| 481 | pr_err("Can't request %s clk (%d)", fixed_clock_dev->name, |
| 482 | err); |
| 483 | return err; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * set pllm factor accordingly to the external oscillator |
| 488 | * frequency (HSE). For STM32F4 and STM32F7, we want VCO |
| 489 | * freq at 1MHz |
| 490 | * if input PLL frequency is 25Mhz, divide it by 25 |
| 491 | */ |
| 492 | clk.id = 0; |
| 493 | priv->hse_rate = clk_get_rate(&clk); |
| 494 | |
| 495 | if (priv->hse_rate < 1000000) { |
| 496 | pr_err("%s: unexpected HSE clock rate = %ld \"n", __func__, |
| 497 | priv->hse_rate); |
| 498 | return -EINVAL; |
| 499 | } |
| 500 | |
| 501 | priv->info.sys_pll_psc.pll_m = priv->hse_rate / 1000000; |
| 502 | |
| 503 | if (priv->info.has_overdrive) { |
Patrice Chotard | f9333c9 | 2017-11-15 13:14:47 +0100 | [diff] [blame] | 504 | err = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0, |
| 505 | &args); |
| 506 | if (err) { |
| 507 | debug("%s: can't find syscon device (%d)\n", __func__, |
| 508 | err); |
| 509 | return err; |
| 510 | } |
| 511 | |
| 512 | priv->pwr_regs = (struct stm32_pwr_regs *)ofnode_get_addr(args.node); |
Patrice Chotard | d0a768b | 2017-11-15 13:14:44 +0100 | [diff] [blame] | 513 | } |
| 514 | |
Patrice Chotard | 199a217 | 2017-07-18 09:29:04 +0200 | [diff] [blame] | 515 | configure_clocks(dev); |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
Simon Glass | a4e0ef5 | 2017-05-18 20:09:40 -0600 | [diff] [blame] | 520 | 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] | 521 | { |
| 522 | debug("%s(clk=%p)\n", __func__, clk); |
| 523 | |
| 524 | if (args->args_count != 2) { |
| 525 | debug("Invaild args_count: %d\n", args->args_count); |
| 526 | return -EINVAL; |
| 527 | } |
| 528 | |
| 529 | if (args->args_count) |
| 530 | clk->id = args->args[1]; |
| 531 | else |
| 532 | clk->id = 0; |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static struct clk_ops stm32_clk_ops = { |
| 538 | .of_xlate = stm32_clk_of_xlate, |
| 539 | .enable = stm32_clk_enable, |
Patrice Chotard | 288f17e | 2017-07-18 09:29:05 +0200 | [diff] [blame] | 540 | .get_rate = stm32_clk_get_rate, |
Patrice Chotard | 05e23dd | 2018-01-29 18:14:14 +0100 | [diff] [blame] | 541 | .set_rate = stm32_set_rate, |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 542 | }; |
| 543 | |
Patrice Chotard | f264e23 | 2017-11-15 13:14:48 +0100 | [diff] [blame] | 544 | U_BOOT_DRIVER(stm32fx_clk) = { |
Patrice Chotard | 928954f | 2017-11-15 13:14:51 +0100 | [diff] [blame] | 545 | .name = "stm32fx_rcc_clock", |
Patrice Chotard | 0cc40df | 2017-09-21 10:08:09 +0200 | [diff] [blame] | 546 | .id = UCLASS_CLK, |
Patrice Chotard | 0cc40df | 2017-09-21 10:08:09 +0200 | [diff] [blame] | 547 | .ops = &stm32_clk_ops, |
| 548 | .probe = stm32_clk_probe, |
| 549 | .priv_auto_alloc_size = sizeof(struct stm32_clk), |
| 550 | .flags = DM_FLAG_PRE_RELOC, |
Vikas Manocha | 712f99a | 2017-02-12 10:25:45 -0800 | [diff] [blame] | 551 | }; |