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