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