clock.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * (C) Copyright 2016
  3. * Vikas Manocha, <vikas.manocha@st.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <asm/io.h>
  9. #include <asm/arch/rcc.h>
  10. #include <asm/arch/stm32.h>
  11. #include <asm/arch/stm32_periph.h>
  12. #define RCC_CR_HSION (1 << 0)
  13. #define RCC_CR_HSEON (1 << 16)
  14. #define RCC_CR_HSERDY (1 << 17)
  15. #define RCC_CR_HSEBYP (1 << 18)
  16. #define RCC_CR_CSSON (1 << 19)
  17. #define RCC_CR_PLLON (1 << 24)
  18. #define RCC_CR_PLLRDY (1 << 25)
  19. #define RCC_PLLCFGR_PLLM_MASK 0x3F
  20. #define RCC_PLLCFGR_PLLN_MASK 0x7FC0
  21. #define RCC_PLLCFGR_PLLP_MASK 0x30000
  22. #define RCC_PLLCFGR_PLLQ_MASK 0xF000000
  23. #define RCC_PLLCFGR_PLLSRC (1 << 22)
  24. #define RCC_PLLCFGR_PLLM_SHIFT 0
  25. #define RCC_PLLCFGR_PLLN_SHIFT 6
  26. #define RCC_PLLCFGR_PLLP_SHIFT 16
  27. #define RCC_PLLCFGR_PLLQ_SHIFT 24
  28. #define RCC_CFGR_AHB_PSC_MASK 0xF0
  29. #define RCC_CFGR_APB1_PSC_MASK 0x1C00
  30. #define RCC_CFGR_APB2_PSC_MASK 0xE000
  31. #define RCC_CFGR_SW0 (1 << 0)
  32. #define RCC_CFGR_SW1 (1 << 1)
  33. #define RCC_CFGR_SW_MASK 0x3
  34. #define RCC_CFGR_SW_HSI 0
  35. #define RCC_CFGR_SW_HSE RCC_CFGR_SW0
  36. #define RCC_CFGR_SW_PLL RCC_CFGR_SW1
  37. #define RCC_CFGR_SWS0 (1 << 2)
  38. #define RCC_CFGR_SWS1 (1 << 3)
  39. #define RCC_CFGR_SWS_MASK 0xC
  40. #define RCC_CFGR_SWS_HSI 0
  41. #define RCC_CFGR_SWS_HSE RCC_CFGR_SWS0
  42. #define RCC_CFGR_SWS_PLL RCC_CFGR_SWS1
  43. #define RCC_CFGR_HPRE_SHIFT 4
  44. #define RCC_CFGR_PPRE1_SHIFT 10
  45. #define RCC_CFGR_PPRE2_SHIFT 13
  46. #define RCC_APB1ENR_PWREN (1 << 28)
  47. /*
  48. * RCC USART specific definitions
  49. */
  50. #define RCC_ENR_USART1EN (1 << 4)
  51. #define RCC_ENR_USART2EN (1 << 17)
  52. #define RCC_ENR_USART3EN (1 << 18)
  53. #define RCC_ENR_USART6EN (1 << 5)
  54. /*
  55. * Offsets of some PWR registers
  56. */
  57. #define PWR_CR1_ODEN (1 << 16)
  58. #define PWR_CR1_ODSWEN (1 << 17)
  59. #define PWR_CSR1_ODRDY (1 << 16)
  60. #define PWR_CSR1_ODSWRDY (1 << 17)
  61. /*
  62. * RCC GPIO specific definitions
  63. */
  64. #define RCC_ENR_GPIO_A_EN (1 << 0)
  65. #define RCC_ENR_GPIO_B_EN (1 << 1)
  66. #define RCC_ENR_GPIO_C_EN (1 << 2)
  67. #define RCC_ENR_GPIO_D_EN (1 << 3)
  68. #define RCC_ENR_GPIO_E_EN (1 << 4)
  69. #define RCC_ENR_GPIO_F_EN (1 << 5)
  70. #define RCC_ENR_GPIO_G_EN (1 << 6)
  71. #define RCC_ENR_GPIO_H_EN (1 << 7)
  72. #define RCC_ENR_GPIO_I_EN (1 << 8)
  73. #define RCC_ENR_GPIO_J_EN (1 << 9)
  74. #define RCC_ENR_GPIO_K_EN (1 << 10)
  75. struct pll_psc {
  76. u8 pll_m;
  77. u16 pll_n;
  78. u8 pll_p;
  79. u8 pll_q;
  80. u8 ahb_psc;
  81. u8 apb1_psc;
  82. u8 apb2_psc;
  83. };
  84. #define AHB_PSC_1 0
  85. #define AHB_PSC_2 0x8
  86. #define AHB_PSC_4 0x9
  87. #define AHB_PSC_8 0xA
  88. #define AHB_PSC_16 0xB
  89. #define AHB_PSC_64 0xC
  90. #define AHB_PSC_128 0xD
  91. #define AHB_PSC_256 0xE
  92. #define AHB_PSC_512 0xF
  93. #define APB_PSC_1 0
  94. #define APB_PSC_2 0x4
  95. #define APB_PSC_4 0x5
  96. #define APB_PSC_8 0x6
  97. #define APB_PSC_16 0x7
  98. #if !defined(CONFIG_STM32_HSE_HZ)
  99. #error "CONFIG_STM32_HSE_HZ not defined!"
  100. #else
  101. #if (CONFIG_STM32_HSE_HZ == 25000000)
  102. #if (CONFIG_SYS_CLK_FREQ == 200000000)
  103. /* 200 MHz */
  104. struct pll_psc sys_pll_psc = {
  105. .pll_m = 25,
  106. .pll_n = 400,
  107. .pll_p = 2,
  108. .pll_q = 8,
  109. .ahb_psc = AHB_PSC_1,
  110. .apb1_psc = APB_PSC_4,
  111. .apb2_psc = APB_PSC_2
  112. };
  113. #endif
  114. #else
  115. #error "No PLL/Prescaler configuration for given CONFIG_STM32_HSE_HZ exists"
  116. #endif
  117. #endif
  118. int configure_clocks(void)
  119. {
  120. /* Reset RCC configuration */
  121. setbits_le32(&STM32_RCC->cr, RCC_CR_HSION);
  122. writel(0, &STM32_RCC->cfgr); /* Reset CFGR */
  123. clrbits_le32(&STM32_RCC->cr, (RCC_CR_HSEON | RCC_CR_CSSON
  124. | RCC_CR_PLLON));
  125. writel(0x24003010, &STM32_RCC->pllcfgr); /* Reset value from RM */
  126. clrbits_le32(&STM32_RCC->cr, RCC_CR_HSEBYP);
  127. writel(0, &STM32_RCC->cir); /* Disable all interrupts */
  128. /* Configure for HSE+PLL operation */
  129. setbits_le32(&STM32_RCC->cr, RCC_CR_HSEON);
  130. while (!(readl(&STM32_RCC->cr) & RCC_CR_HSERDY))
  131. ;
  132. setbits_le32(&STM32_RCC->cfgr, ((
  133. sys_pll_psc.ahb_psc << RCC_CFGR_HPRE_SHIFT)
  134. | (sys_pll_psc.apb1_psc << RCC_CFGR_PPRE1_SHIFT)
  135. | (sys_pll_psc.apb2_psc << RCC_CFGR_PPRE2_SHIFT)));
  136. /* Configure the main PLL */
  137. uint32_t pllcfgr = 0;
  138. pllcfgr = RCC_PLLCFGR_PLLSRC; /* pll source HSE */
  139. pllcfgr |= sys_pll_psc.pll_m << RCC_PLLCFGR_PLLM_SHIFT;
  140. pllcfgr |= sys_pll_psc.pll_n << RCC_PLLCFGR_PLLN_SHIFT;
  141. pllcfgr |= ((sys_pll_psc.pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT;
  142. pllcfgr |= sys_pll_psc.pll_q << RCC_PLLCFGR_PLLQ_SHIFT;
  143. writel(pllcfgr, &STM32_RCC->pllcfgr);
  144. /* Enable the main PLL */
  145. setbits_le32(&STM32_RCC->cr, RCC_CR_PLLON);
  146. while (!(readl(&STM32_RCC->cr) & RCC_CR_PLLRDY))
  147. ;
  148. /* Enable high performance mode, System frequency up to 200 MHz */
  149. setbits_le32(&STM32_RCC->apb1enr, RCC_APB1ENR_PWREN);
  150. setbits_le32(&STM32_PWR->cr1, PWR_CR1_ODEN);
  151. /* Infinite wait! */
  152. while (!(readl(&STM32_PWR->csr1) & PWR_CSR1_ODRDY))
  153. ;
  154. /* Enable the Over-drive switch */
  155. setbits_le32(&STM32_PWR->cr1, PWR_CR1_ODSWEN);
  156. /* Infinite wait! */
  157. while (!(readl(&STM32_PWR->csr1) & PWR_CSR1_ODSWRDY))
  158. ;
  159. stm32_flash_latency_cfg(5);
  160. clrbits_le32(&STM32_RCC->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1));
  161. setbits_le32(&STM32_RCC->cfgr, RCC_CFGR_SW_PLL);
  162. while ((readl(&STM32_RCC->cfgr) & RCC_CFGR_SWS_MASK) !=
  163. RCC_CFGR_SWS_PLL)
  164. ;
  165. return 0;
  166. }
  167. unsigned long clock_get(enum clock clck)
  168. {
  169. u32 sysclk = 0;
  170. u32 shift = 0;
  171. /* Prescaler table lookups for clock computation */
  172. u8 ahb_psc_table[16] = {
  173. 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9
  174. };
  175. u8 apb_psc_table[8] = {
  176. 0, 0, 0, 0, 1, 2, 3, 4
  177. };
  178. if ((readl(&STM32_RCC->cfgr) & RCC_CFGR_SWS_MASK) ==
  179. RCC_CFGR_SWS_PLL) {
  180. u16 pllm, plln, pllp;
  181. pllm = (readl(&STM32_RCC->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
  182. plln = ((readl(&STM32_RCC->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
  183. >> RCC_PLLCFGR_PLLN_SHIFT);
  184. pllp = ((((readl(&STM32_RCC->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
  185. >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
  186. sysclk = ((CONFIG_STM32_HSE_HZ / pllm) * plln) / pllp;
  187. }
  188. switch (clck) {
  189. case CLOCK_CORE:
  190. return sysclk;
  191. break;
  192. case CLOCK_AHB:
  193. shift = ahb_psc_table[(
  194. (readl(&STM32_RCC->cfgr) & RCC_CFGR_AHB_PSC_MASK)
  195. >> RCC_CFGR_HPRE_SHIFT)];
  196. return sysclk >>= shift;
  197. break;
  198. case CLOCK_APB1:
  199. shift = apb_psc_table[(
  200. (readl(&STM32_RCC->cfgr) & RCC_CFGR_APB1_PSC_MASK)
  201. >> RCC_CFGR_PPRE1_SHIFT)];
  202. return sysclk >>= shift;
  203. break;
  204. case CLOCK_APB2:
  205. shift = apb_psc_table[(
  206. (readl(&STM32_RCC->cfgr) & RCC_CFGR_APB2_PSC_MASK)
  207. >> RCC_CFGR_PPRE2_SHIFT)];
  208. return sysclk >>= shift;
  209. break;
  210. default:
  211. return 0;
  212. break;
  213. }
  214. }
  215. void clock_setup(int peripheral)
  216. {
  217. switch (peripheral) {
  218. case USART1_CLOCK_CFG:
  219. setbits_le32(RCC_BASE + RCC_APB2ENR, RCC_ENR_USART1EN);
  220. break;
  221. case GPIO_A_CLOCK_CFG:
  222. setbits_le32(RCC_BASE + RCC_AHB1ENR, RCC_ENR_GPIO_A_EN);
  223. break;
  224. case GPIO_B_CLOCK_CFG:
  225. setbits_le32(RCC_BASE + RCC_AHB1ENR, RCC_ENR_GPIO_B_EN);
  226. break;
  227. case GPIO_C_CLOCK_CFG:
  228. setbits_le32(RCC_BASE + RCC_AHB1ENR, RCC_ENR_GPIO_C_EN);
  229. break;
  230. case GPIO_D_CLOCK_CFG:
  231. setbits_le32(RCC_BASE + RCC_AHB1ENR, RCC_ENR_GPIO_D_EN);
  232. break;
  233. case GPIO_E_CLOCK_CFG:
  234. setbits_le32(RCC_BASE + RCC_AHB1ENR, RCC_ENR_GPIO_E_EN);
  235. break;
  236. case GPIO_F_CLOCK_CFG:
  237. setbits_le32(RCC_BASE + RCC_AHB1ENR, RCC_ENR_GPIO_F_EN);
  238. break;
  239. case GPIO_G_CLOCK_CFG:
  240. setbits_le32(RCC_BASE + RCC_AHB1ENR, RCC_ENR_GPIO_G_EN);
  241. break;
  242. case GPIO_H_CLOCK_CFG:
  243. setbits_le32(RCC_BASE + RCC_AHB1ENR, RCC_ENR_GPIO_H_EN);
  244. break;
  245. case GPIO_I_CLOCK_CFG:
  246. setbits_le32(RCC_BASE + RCC_AHB1ENR, RCC_ENR_GPIO_I_EN);
  247. break;
  248. case GPIO_J_CLOCK_CFG:
  249. setbits_le32(RCC_BASE + RCC_AHB1ENR, RCC_ENR_GPIO_J_EN);
  250. break;
  251. case GPIO_K_CLOCK_CFG:
  252. setbits_le32(RCC_BASE + RCC_AHB1ENR, RCC_ENR_GPIO_K_EN);
  253. break;
  254. default:
  255. break;
  256. }
  257. }