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