clk_stm32f7.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * (C) Copyright 2017
  3. * Vikas Manocha, <vikas.manocha@st.com>
  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. uint32_t pllcfgr = 0;
  124. pllcfgr = RCC_PLLCFGR_PLLSRC; /* pll source HSE */
  125. pllcfgr |= sys_pll_psc.pll_m << RCC_PLLCFGR_PLLM_SHIFT;
  126. pllcfgr |= sys_pll_psc.pll_n << RCC_PLLCFGR_PLLN_SHIFT;
  127. pllcfgr |= ((sys_pll_psc.pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT;
  128. pllcfgr |= sys_pll_psc.pll_q << RCC_PLLCFGR_PLLQ_SHIFT;
  129. writel(pllcfgr, &regs->pllcfgr);
  130. /* Enable the main PLL */
  131. setbits_le32(&regs->cr, RCC_CR_PLLON);
  132. while (!(readl(&regs->cr) & RCC_CR_PLLRDY))
  133. ;
  134. /* Enable high performance mode, System frequency up to 200 MHz */
  135. setbits_le32(&regs->apb1enr, RCC_APB1ENR_PWREN);
  136. setbits_le32(&STM32_PWR->cr1, PWR_CR1_ODEN);
  137. /* Infinite wait! */
  138. while (!(readl(&STM32_PWR->csr1) & PWR_CSR1_ODRDY))
  139. ;
  140. /* Enable the Over-drive switch */
  141. setbits_le32(&STM32_PWR->cr1, PWR_CR1_ODSWEN);
  142. /* Infinite wait! */
  143. while (!(readl(&STM32_PWR->csr1) & PWR_CSR1_ODSWRDY))
  144. ;
  145. stm32_flash_latency_cfg(5);
  146. clrbits_le32(&regs->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1));
  147. setbits_le32(&regs->cfgr, RCC_CFGR_SW_PLL);
  148. while ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) !=
  149. RCC_CFGR_SWS_PLL)
  150. ;
  151. return 0;
  152. }
  153. static unsigned long stm32_clk_get_rate(struct clk *clk)
  154. {
  155. struct stm32_clk *priv = dev_get_priv(clk->dev);
  156. struct stm32_rcc_regs *regs = priv->base;
  157. u32 sysclk = 0;
  158. u32 shift = 0;
  159. /* Prescaler table lookups for clock computation */
  160. u8 ahb_psc_table[16] = {
  161. 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9
  162. };
  163. u8 apb_psc_table[8] = {
  164. 0, 0, 0, 0, 1, 2, 3, 4
  165. };
  166. if ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) ==
  167. RCC_CFGR_SWS_PLL) {
  168. u16 pllm, plln, pllp;
  169. pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
  170. plln = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
  171. >> RCC_PLLCFGR_PLLN_SHIFT);
  172. pllp = ((((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
  173. >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
  174. sysclk = ((CONFIG_STM32_HSE_HZ / pllm) * plln) / pllp;
  175. } else {
  176. return -EINVAL;
  177. }
  178. switch (clk->id) {
  179. /*
  180. * AHB CLOCK: 3 x 32 bits consecutive registers are used :
  181. * AHB1, AHB2 and AHB3
  182. */
  183. case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI):
  184. shift = ahb_psc_table[(
  185. (readl(&regs->cfgr) & RCC_CFGR_AHB_PSC_MASK)
  186. >> RCC_CFGR_HPRE_SHIFT)];
  187. return sysclk >>= shift;
  188. break;
  189. /* APB1 CLOCK */
  190. case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8):
  191. shift = apb_psc_table[(
  192. (readl(&regs->cfgr) & RCC_CFGR_APB1_PSC_MASK)
  193. >> RCC_CFGR_PPRE1_SHIFT)];
  194. return sysclk >>= shift;
  195. break;
  196. /* APB2 CLOCK */
  197. case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(LTDC):
  198. shift = apb_psc_table[(
  199. (readl(&regs->cfgr) & RCC_CFGR_APB2_PSC_MASK)
  200. >> RCC_CFGR_PPRE2_SHIFT)];
  201. return sysclk >>= shift;
  202. break;
  203. default:
  204. error("clock index %ld out of range\n", clk->id);
  205. return -EINVAL;
  206. break;
  207. }
  208. }
  209. static int stm32_clk_enable(struct clk *clk)
  210. {
  211. struct stm32_clk *priv = dev_get_priv(clk->dev);
  212. struct stm32_rcc_regs *regs = priv->base;
  213. u32 offset = clk->id / 32;
  214. u32 bit_index = clk->id % 32;
  215. debug("%s: clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n",
  216. __func__, clk->id, offset, bit_index);
  217. setbits_le32(&regs->ahb1enr + offset, BIT(bit_index));
  218. return 0;
  219. }
  220. void clock_setup(int peripheral)
  221. {
  222. switch (peripheral) {
  223. case SYSCFG_CLOCK_CFG:
  224. setbits_le32(&STM32_RCC->apb2enr, RCC_APB2ENR_SYSCFGEN);
  225. break;
  226. case TIMER2_CLOCK_CFG:
  227. setbits_le32(&STM32_RCC->apb1enr, RCC_APB1ENR_TIM2EN);
  228. break;
  229. case STMMAC_CLOCK_CFG:
  230. setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_EN);
  231. setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_RX_EN);
  232. setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_TX_EN);
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. static int stm32_clk_probe(struct udevice *dev)
  239. {
  240. debug("%s: stm32_clk_probe\n", __func__);
  241. struct stm32_clk *priv = dev_get_priv(dev);
  242. fdt_addr_t addr;
  243. addr = devfdt_get_addr(dev);
  244. if (addr == FDT_ADDR_T_NONE)
  245. return -EINVAL;
  246. priv->base = (struct stm32_rcc_regs *)addr;
  247. configure_clocks(dev);
  248. return 0;
  249. }
  250. static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
  251. {
  252. debug("%s(clk=%p)\n", __func__, clk);
  253. if (args->args_count != 2) {
  254. debug("Invaild args_count: %d\n", args->args_count);
  255. return -EINVAL;
  256. }
  257. if (args->args_count)
  258. clk->id = args->args[1];
  259. else
  260. clk->id = 0;
  261. return 0;
  262. }
  263. static struct clk_ops stm32_clk_ops = {
  264. .of_xlate = stm32_clk_of_xlate,
  265. .enable = stm32_clk_enable,
  266. .get_rate = stm32_clk_get_rate,
  267. };
  268. static const struct udevice_id stm32_clk_ids[] = {
  269. { .compatible = "st,stm32f42xx-rcc"},
  270. {}
  271. };
  272. U_BOOT_DRIVER(stm32f7_clk) = {
  273. .name = "stm32f7_clk",
  274. .id = UCLASS_CLK,
  275. .of_match = stm32_clk_ids,
  276. .ops = &stm32_clk_ops,
  277. .probe = stm32_clk_probe,
  278. .priv_auto_alloc_size = sizeof(struct stm32_clk),
  279. .flags = DM_FLAG_PRE_RELOC,
  280. };