clk_stm32f7.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  3. * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <clk-uclass.h>
  9. #include <dm.h>
  10. #include <asm/io.h>
  11. #include <asm/arch/rcc.h>
  12. #include <asm/arch/stm32.h>
  13. #include <asm/arch/stm32_periph.h>
  14. #include <dt-bindings/mfd/stm32f7-rcc.h>
  15. #define RCC_CR_HSION BIT(0)
  16. #define RCC_CR_HSEON BIT(16)
  17. #define RCC_CR_HSERDY BIT(17)
  18. #define RCC_CR_HSEBYP BIT(18)
  19. #define RCC_CR_CSSON BIT(19)
  20. #define RCC_CR_PLLON BIT(24)
  21. #define RCC_CR_PLLRDY BIT(25)
  22. #define RCC_PLLCFGR_PLLM_MASK GENMASK(5, 0)
  23. #define RCC_PLLCFGR_PLLN_MASK GENMASK(14, 6)
  24. #define RCC_PLLCFGR_PLLP_MASK GENMASK(17, 16)
  25. #define RCC_PLLCFGR_PLLQ_MASK GENMASK(27, 24)
  26. #define RCC_PLLCFGR_PLLSRC BIT(22)
  27. #define RCC_PLLCFGR_PLLM_SHIFT 0
  28. #define RCC_PLLCFGR_PLLN_SHIFT 6
  29. #define RCC_PLLCFGR_PLLP_SHIFT 16
  30. #define RCC_PLLCFGR_PLLQ_SHIFT 24
  31. #define RCC_CFGR_AHB_PSC_MASK GENMASK(7, 4)
  32. #define RCC_CFGR_APB1_PSC_MASK GENMASK(12, 10)
  33. #define RCC_CFGR_APB2_PSC_MASK GENMASK(15, 13)
  34. #define RCC_CFGR_SW0 BIT(0)
  35. #define RCC_CFGR_SW1 BIT(1)
  36. #define RCC_CFGR_SW_MASK GENMASK(1, 0)
  37. #define RCC_CFGR_SW_HSI 0
  38. #define RCC_CFGR_SW_HSE RCC_CFGR_SW0
  39. #define RCC_CFGR_SW_PLL RCC_CFGR_SW1
  40. #define RCC_CFGR_SWS0 BIT(2)
  41. #define RCC_CFGR_SWS1 BIT(3)
  42. #define RCC_CFGR_SWS_MASK GENMASK(3, 2)
  43. #define RCC_CFGR_SWS_HSI 0
  44. #define RCC_CFGR_SWS_HSE RCC_CFGR_SWS0
  45. #define RCC_CFGR_SWS_PLL RCC_CFGR_SWS1
  46. #define RCC_CFGR_HPRE_SHIFT 4
  47. #define RCC_CFGR_PPRE1_SHIFT 10
  48. #define RCC_CFGR_PPRE2_SHIFT 13
  49. /*
  50. * Offsets of some PWR registers
  51. */
  52. #define PWR_CR1_ODEN BIT(16)
  53. #define PWR_CR1_ODSWEN BIT(17)
  54. #define PWR_CSR1_ODRDY BIT(16)
  55. #define PWR_CSR1_ODSWRDY BIT(17)
  56. struct pll_psc {
  57. u8 pll_m;
  58. u16 pll_n;
  59. u8 pll_p;
  60. u8 pll_q;
  61. u8 ahb_psc;
  62. u8 apb1_psc;
  63. u8 apb2_psc;
  64. };
  65. #define AHB_PSC_1 0
  66. #define AHB_PSC_2 0x8
  67. #define AHB_PSC_4 0x9
  68. #define AHB_PSC_8 0xA
  69. #define AHB_PSC_16 0xB
  70. #define AHB_PSC_64 0xC
  71. #define AHB_PSC_128 0xD
  72. #define AHB_PSC_256 0xE
  73. #define AHB_PSC_512 0xF
  74. #define APB_PSC_1 0
  75. #define APB_PSC_2 0x4
  76. #define APB_PSC_4 0x5
  77. #define APB_PSC_8 0x6
  78. #define APB_PSC_16 0x7
  79. struct stm32_clk {
  80. struct stm32_rcc_regs *base;
  81. };
  82. #if !defined(CONFIG_STM32_HSE_HZ)
  83. #error "CONFIG_STM32_HSE_HZ not defined!"
  84. #else
  85. #if (CONFIG_STM32_HSE_HZ == 25000000)
  86. #if (CONFIG_SYS_CLK_FREQ == 200000000)
  87. /* 200 MHz */
  88. struct pll_psc sys_pll_psc = {
  89. .pll_m = 25,
  90. .pll_n = 400,
  91. .pll_p = 2,
  92. .pll_q = 8,
  93. .ahb_psc = AHB_PSC_1,
  94. .apb1_psc = APB_PSC_4,
  95. .apb2_psc = APB_PSC_2
  96. };
  97. #endif
  98. #else
  99. #error "No PLL/Prescaler configuration for given CONFIG_STM32_HSE_HZ exists"
  100. #endif
  101. #endif
  102. static int configure_clocks(struct udevice *dev)
  103. {
  104. struct stm32_clk *priv = dev_get_priv(dev);
  105. struct stm32_rcc_regs *regs = priv->base;
  106. /* Reset RCC configuration */
  107. setbits_le32(&regs->cr, RCC_CR_HSION);
  108. writel(0, &regs->cfgr); /* Reset CFGR */
  109. clrbits_le32(&regs->cr, (RCC_CR_HSEON | RCC_CR_CSSON
  110. | RCC_CR_PLLON));
  111. writel(0x24003010, &regs->pllcfgr); /* Reset value from RM */
  112. clrbits_le32(&regs->cr, RCC_CR_HSEBYP);
  113. writel(0, &regs->cir); /* Disable all interrupts */
  114. /* Configure for HSE+PLL operation */
  115. setbits_le32(&regs->cr, RCC_CR_HSEON);
  116. while (!(readl(&regs->cr) & RCC_CR_HSERDY))
  117. ;
  118. setbits_le32(&regs->cfgr, ((
  119. sys_pll_psc.ahb_psc << RCC_CFGR_HPRE_SHIFT)
  120. | (sys_pll_psc.apb1_psc << RCC_CFGR_PPRE1_SHIFT)
  121. | (sys_pll_psc.apb2_psc << RCC_CFGR_PPRE2_SHIFT)));
  122. /* Configure the main PLL */
  123. setbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLSRC); /* pll source HSE */
  124. clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLM_MASK,
  125. sys_pll_psc.pll_m << RCC_PLLCFGR_PLLM_SHIFT);
  126. clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLN_MASK,
  127. sys_pll_psc.pll_n << RCC_PLLCFGR_PLLN_SHIFT);
  128. clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLP_MASK,
  129. ((sys_pll_psc.pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT);
  130. clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLQ_MASK,
  131. sys_pll_psc.pll_q << RCC_PLLCFGR_PLLQ_SHIFT);
  132. /* Enable the main PLL */
  133. setbits_le32(&regs->cr, RCC_CR_PLLON);
  134. while (!(readl(&regs->cr) & RCC_CR_PLLRDY))
  135. ;
  136. /* Enable high performance mode, System frequency up to 200 MHz */
  137. setbits_le32(&regs->apb1enr, RCC_APB1ENR_PWREN);
  138. setbits_le32(&STM32_PWR->cr1, PWR_CR1_ODEN);
  139. /* Infinite wait! */
  140. while (!(readl(&STM32_PWR->csr1) & PWR_CSR1_ODRDY))
  141. ;
  142. /* Enable the Over-drive switch */
  143. setbits_le32(&STM32_PWR->cr1, PWR_CR1_ODSWEN);
  144. /* Infinite wait! */
  145. while (!(readl(&STM32_PWR->csr1) & PWR_CSR1_ODSWRDY))
  146. ;
  147. stm32_flash_latency_cfg(5);
  148. clrbits_le32(&regs->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1));
  149. setbits_le32(&regs->cfgr, RCC_CFGR_SW_PLL);
  150. while ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) !=
  151. RCC_CFGR_SWS_PLL)
  152. ;
  153. return 0;
  154. }
  155. static unsigned long stm32_clk_get_rate(struct clk *clk)
  156. {
  157. struct stm32_clk *priv = dev_get_priv(clk->dev);
  158. struct stm32_rcc_regs *regs = priv->base;
  159. u32 sysclk = 0;
  160. u32 shift = 0;
  161. /* Prescaler table lookups for clock computation */
  162. u8 ahb_psc_table[16] = {
  163. 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9
  164. };
  165. u8 apb_psc_table[8] = {
  166. 0, 0, 0, 0, 1, 2, 3, 4
  167. };
  168. if ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) ==
  169. RCC_CFGR_SWS_PLL) {
  170. u16 pllm, plln, pllp;
  171. pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
  172. plln = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
  173. >> RCC_PLLCFGR_PLLN_SHIFT);
  174. pllp = ((((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
  175. >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
  176. sysclk = ((CONFIG_STM32_HSE_HZ / pllm) * plln) / pllp;
  177. } else {
  178. return -EINVAL;
  179. }
  180. switch (clk->id) {
  181. /*
  182. * AHB CLOCK: 3 x 32 bits consecutive registers are used :
  183. * AHB1, AHB2 and AHB3
  184. */
  185. case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI):
  186. shift = ahb_psc_table[(
  187. (readl(&regs->cfgr) & RCC_CFGR_AHB_PSC_MASK)
  188. >> RCC_CFGR_HPRE_SHIFT)];
  189. return sysclk >>= shift;
  190. break;
  191. /* APB1 CLOCK */
  192. case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8):
  193. shift = apb_psc_table[(
  194. (readl(&regs->cfgr) & RCC_CFGR_APB1_PSC_MASK)
  195. >> RCC_CFGR_PPRE1_SHIFT)];
  196. return sysclk >>= shift;
  197. break;
  198. /* APB2 CLOCK */
  199. case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(LTDC):
  200. shift = apb_psc_table[(
  201. (readl(&regs->cfgr) & RCC_CFGR_APB2_PSC_MASK)
  202. >> RCC_CFGR_PPRE2_SHIFT)];
  203. return sysclk >>= shift;
  204. break;
  205. default:
  206. pr_err("clock index %ld out of range\n", clk->id);
  207. return -EINVAL;
  208. break;
  209. }
  210. }
  211. static int stm32_clk_enable(struct clk *clk)
  212. {
  213. struct stm32_clk *priv = dev_get_priv(clk->dev);
  214. struct stm32_rcc_regs *regs = priv->base;
  215. u32 offset = clk->id / 32;
  216. u32 bit_index = clk->id % 32;
  217. debug("%s: clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n",
  218. __func__, clk->id, offset, bit_index);
  219. setbits_le32(&regs->ahb1enr + offset, BIT(bit_index));
  220. return 0;
  221. }
  222. void clock_setup(int peripheral)
  223. {
  224. switch (peripheral) {
  225. case SYSCFG_CLOCK_CFG:
  226. setbits_le32(&STM32_RCC->apb2enr, RCC_APB2ENR_SYSCFGEN);
  227. break;
  228. case TIMER2_CLOCK_CFG:
  229. setbits_le32(&STM32_RCC->apb1enr, RCC_APB1ENR_TIM2EN);
  230. break;
  231. case STMMAC_CLOCK_CFG:
  232. setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_EN);
  233. setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_RX_EN);
  234. setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_TX_EN);
  235. break;
  236. default:
  237. break;
  238. }
  239. }
  240. static int stm32_clk_probe(struct udevice *dev)
  241. {
  242. debug("%s: stm32_clk_probe\n", __func__);
  243. struct stm32_clk *priv = dev_get_priv(dev);
  244. fdt_addr_t addr;
  245. addr = devfdt_get_addr(dev);
  246. if (addr == FDT_ADDR_T_NONE)
  247. return -EINVAL;
  248. priv->base = (struct stm32_rcc_regs *)addr;
  249. configure_clocks(dev);
  250. return 0;
  251. }
  252. static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
  253. {
  254. debug("%s(clk=%p)\n", __func__, clk);
  255. if (args->args_count != 2) {
  256. debug("Invaild args_count: %d\n", args->args_count);
  257. return -EINVAL;
  258. }
  259. if (args->args_count)
  260. clk->id = args->args[1];
  261. else
  262. clk->id = 0;
  263. return 0;
  264. }
  265. static struct clk_ops stm32_clk_ops = {
  266. .of_xlate = stm32_clk_of_xlate,
  267. .enable = stm32_clk_enable,
  268. .get_rate = stm32_clk_get_rate,
  269. };
  270. static const struct udevice_id stm32_clk_ids[] = {
  271. { .compatible = "st,stm32f42xx-rcc"},
  272. {}
  273. };
  274. U_BOOT_DRIVER(stm32f7_clk) = {
  275. .name = "stm32f7_clk",
  276. .id = UCLASS_CLK,
  277. .of_match = stm32_clk_ids,
  278. .ops = &stm32_clk_ops,
  279. .probe = stm32_clk_probe,
  280. .priv_auto_alloc_size = sizeof(struct stm32_clk),
  281. .flags = DM_FLAG_PRE_RELOC,
  282. };