clock.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2018-2023 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdbool.h>
  7. #include <lib/mmio.h>
  8. #include <platform_def.h>
  9. #define IMX_CCM_IP_BASE (IMX_CCM_BASE + 0xa000)
  10. #define DRAM_SEL_CFG (IMX_CCM_BASE + 0x9800)
  11. #define CCM_IP_CLK_ROOT_GEN_TAGET(i) (IMX_CCM_IP_BASE + 0x80 * (i) + 0x00)
  12. #define CCM_IP_CLK_ROOT_GEN_TAGET_SET(i) (IMX_CCM_IP_BASE + 0x80 * (i) + 0x04)
  13. #define CCM_IP_CLK_ROOT_GEN_TAGET_CLR(i) (IMX_CCM_IP_BASE + 0x80 * (i) + 0x08)
  14. #define PLL_FREQ_800M U(0x00ece580)
  15. #define PLL_FREQ_400M U(0x00ec6984)
  16. #define PLL_FREQ_167M U(0x00f5a406)
  17. void ddr_pll_bypass_100mts(void)
  18. {
  19. /* change the clock source of dram_alt_clk_root to source 2 --100MHz */
  20. mmio_write_32(CCM_IP_CLK_ROOT_GEN_TAGET_CLR(0), (0x7 << 24) | (0x7 << 16));
  21. mmio_write_32(CCM_IP_CLK_ROOT_GEN_TAGET_SET(0), (0x2 << 24));
  22. /* change the clock source of dram_apb_clk_root to source 2 --40MHz/2 */
  23. mmio_write_32(CCM_IP_CLK_ROOT_GEN_TAGET_CLR(1), (0x7 << 24) | (0x7 << 16));
  24. mmio_write_32(CCM_IP_CLK_ROOT_GEN_TAGET_SET(1), (0x2 << 24) | (0x1 << 16));
  25. /* configure pll bypass mode */
  26. mmio_write_32(DRAM_SEL_CFG + 0x4, BIT(24));
  27. }
  28. void ddr_pll_bypass_400mts(void)
  29. {
  30. /* change the clock source of dram_alt_clk_root to source 1 --400MHz */
  31. mmio_write_32(CCM_IP_CLK_ROOT_GEN_TAGET_CLR(0), (0x7 << 24) | (0x7 << 16));
  32. mmio_write_32(CCM_IP_CLK_ROOT_GEN_TAGET_SET(0), (0x1 << 24) | (0x1 << 16));
  33. /* change the clock source of dram_apb_clk_root to source 3 --160MHz/2 */
  34. mmio_write_32(CCM_IP_CLK_ROOT_GEN_TAGET_CLR(1), (0x7 << 24) | (0x7 << 16));
  35. mmio_write_32(CCM_IP_CLK_ROOT_GEN_TAGET_SET(1), (0x3 << 24) | (0x1 << 16));
  36. /* configure pll bypass mode */
  37. mmio_write_32(DRAM_SEL_CFG + 0x4, BIT(24));
  38. }
  39. void ddr_pll_unbypass(void)
  40. {
  41. mmio_write_32(DRAM_SEL_CFG + 0x8, BIT(24));
  42. mmio_write_32(CCM_IP_CLK_ROOT_GEN_TAGET_CLR(1), (0x7 << 24) | (0x7 << 16));
  43. /* to source 4 --800MHz/5 */
  44. mmio_write_32(CCM_IP_CLK_ROOT_GEN_TAGET_SET(1), (0x4 << 24) | (0x4 << 16));
  45. }
  46. #if defined(PLAT_imx8mq)
  47. void dram_pll_init(unsigned int drate)
  48. {
  49. /* bypass the PLL */
  50. mmio_setbits_32(HW_DRAM_PLL_CFG0, 0x30);
  51. switch (drate) {
  52. case 3200:
  53. mmio_write_32(HW_DRAM_PLL_CFG2, PLL_FREQ_800M);
  54. break;
  55. case 1600:
  56. mmio_write_32(HW_DRAM_PLL_CFG2, PLL_FREQ_400M);
  57. break;
  58. case 667:
  59. mmio_write_32(HW_DRAM_PLL_CFG2, PLL_FREQ_167M);
  60. break;
  61. default:
  62. break;
  63. }
  64. /* unbypass the PLL */
  65. mmio_clrbits_32(HW_DRAM_PLL_CFG0, 0x30);
  66. while (!(mmio_read_32(HW_DRAM_PLL_CFG0) & BIT(31))) {
  67. ;
  68. }
  69. }
  70. #else
  71. void dram_pll_init(unsigned int drate)
  72. {
  73. /* bypass the PLL */
  74. mmio_setbits_32(DRAM_PLL_CTRL, (1 << 16));
  75. mmio_clrbits_32(DRAM_PLL_CTRL, (1 << 9));
  76. switch (drate) {
  77. case 4000:
  78. mmio_write_32(DRAM_PLL_CTRL + 0x4, (250 << 12) | (3 << 4) | 1);
  79. break;
  80. case 3734:
  81. case 3733:
  82. case 3732:
  83. mmio_write_32(DRAM_PLL_CTRL + 0x4, (311 << 12) | (4 << 4) | 1);
  84. break;
  85. case 3600:
  86. mmio_write_32(DRAM_PLL_CTRL + 0x4, (300 << 12) | (8 << 4) | 0);
  87. break;
  88. case 3200:
  89. mmio_write_32(DRAM_PLL_CTRL + 0x4, (300 << 12) | (9 << 4) | 0);
  90. break;
  91. case 2400:
  92. mmio_write_32(DRAM_PLL_CTRL + 0x4, (300 << 12) | (3 << 4) | 2);
  93. break;
  94. case 1600:
  95. mmio_write_32(DRAM_PLL_CTRL + 0x4, (400 << 12) | (3 << 4) | 3);
  96. break;
  97. case 1066:
  98. mmio_write_32(DRAM_PLL_CTRL + 0x4, (266 << 12) | (3 << 4) | 3);
  99. break;
  100. case 667:
  101. mmio_write_32(DRAM_PLL_CTRL + 0x4, (334 << 12) | (3 << 4) | 4);
  102. break;
  103. default:
  104. break;
  105. }
  106. mmio_setbits_32(DRAM_PLL_CTRL, BIT(9));
  107. /* wait for PLL locked */
  108. while (!(mmio_read_32(DRAM_PLL_CTRL) & BIT(31))) {
  109. ;
  110. }
  111. /* unbypass the PLL */
  112. mmio_clrbits_32(DRAM_PLL_CTRL, BIT(16));
  113. }
  114. #endif
  115. /* change the dram clock frequency */
  116. void dram_clock_switch(unsigned int target_drate, bool bypass_mode)
  117. {
  118. if (bypass_mode) {
  119. switch (target_drate) {
  120. case 400:
  121. ddr_pll_bypass_400mts();
  122. break;
  123. case 100:
  124. ddr_pll_bypass_100mts();
  125. break;
  126. default:
  127. ddr_pll_unbypass();
  128. break;
  129. }
  130. } else {
  131. dram_pll_init(target_drate);
  132. }
  133. }