stm32mp1_ram.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 2018-2023, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
  5. */
  6. #include <errno.h>
  7. #include <arch_helpers.h>
  8. #include <common/debug.h>
  9. #include <common/fdt_wrappers.h>
  10. #include <drivers/clk.h>
  11. #include <drivers/st/stm32mp1_ddr.h>
  12. #include <drivers/st/stm32mp1_ddr_helpers.h>
  13. #include <drivers/st/stm32mp1_ram.h>
  14. #include <drivers/st/stm32mp_ddr.h>
  15. #include <drivers/st/stm32mp_ddr_test.h>
  16. #include <drivers/st/stm32mp_ram.h>
  17. #include <lib/mmio.h>
  18. #include <libfdt.h>
  19. #include <platform_def.h>
  20. static struct stm32mp_ddr_priv ddr_priv_data;
  21. int stm32mp1_ddr_clk_enable(struct stm32mp_ddr_priv *priv, uint32_t mem_speed)
  22. {
  23. unsigned long ddrphy_clk, ddr_clk, mem_speed_hz;
  24. ddr_enable_clock();
  25. ddrphy_clk = clk_get_rate(DDRPHYC);
  26. VERBOSE("DDR: mem_speed (%u kHz), RCC %lu kHz\n",
  27. mem_speed, ddrphy_clk / 1000U);
  28. mem_speed_hz = mem_speed * 1000U;
  29. /* Max 10% frequency delta */
  30. if (ddrphy_clk > mem_speed_hz) {
  31. ddr_clk = ddrphy_clk - mem_speed_hz;
  32. } else {
  33. ddr_clk = mem_speed_hz - ddrphy_clk;
  34. }
  35. if (ddr_clk > (mem_speed_hz / 10)) {
  36. ERROR("DDR expected freq %u kHz, current is %lu kHz\n",
  37. mem_speed, ddrphy_clk / 1000U);
  38. return -1;
  39. }
  40. return 0;
  41. }
  42. static int stm32mp1_ddr_setup(void)
  43. {
  44. struct stm32mp_ddr_priv *priv = &ddr_priv_data;
  45. int ret;
  46. struct stm32mp_ddr_config config;
  47. int node;
  48. uintptr_t uret;
  49. size_t retsize;
  50. void *fdt;
  51. const struct stm32mp_ddr_param param[] = {
  52. CTL_PARAM(reg),
  53. CTL_PARAM(timing),
  54. CTL_PARAM(map),
  55. CTL_PARAM(perf),
  56. PHY_PARAM(reg),
  57. PHY_PARAM(timing),
  58. };
  59. if (fdt_get_address(&fdt) == 0) {
  60. return -ENOENT;
  61. }
  62. node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT);
  63. if (node < 0) {
  64. ERROR("%s: Cannot read DDR node in DT\n", __func__);
  65. return -EINVAL;
  66. }
  67. ret = stm32mp_ddr_dt_get_info(fdt, node, &config.info);
  68. if (ret < 0) {
  69. return ret;
  70. }
  71. ret = stm32mp_ddr_dt_get_param(fdt, node, param, ARRAY_SIZE(param), (uintptr_t)&config);
  72. if (ret < 0) {
  73. return ret;
  74. }
  75. /* Disable axidcg clock gating during init */
  76. mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_AXIDCGEN);
  77. stm32mp1_ddr_init(priv, &config);
  78. /* Enable axidcg clock gating */
  79. mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_AXIDCGEN);
  80. priv->info.size = config.info.size;
  81. VERBOSE("%s : ram size(%x, %x)\n", __func__,
  82. (uint32_t)priv->info.base, (uint32_t)priv->info.size);
  83. if (stm32mp_map_ddr_non_cacheable() != 0) {
  84. panic();
  85. }
  86. uret = stm32mp_ddr_test_data_bus();
  87. if (uret != 0UL) {
  88. ERROR("DDR data bus test: can't access memory @ 0x%lx\n",
  89. uret);
  90. panic();
  91. }
  92. uret = stm32mp_ddr_test_addr_bus(config.info.size);
  93. if (uret != 0UL) {
  94. ERROR("DDR addr bus test: can't access memory @ 0x%lx\n",
  95. uret);
  96. panic();
  97. }
  98. retsize = stm32mp_ddr_check_size();
  99. if (retsize < config.info.size) {
  100. ERROR("DDR size: 0x%zx does not match DT config: 0x%zx\n",
  101. retsize, config.info.size);
  102. panic();
  103. }
  104. INFO("Memory size = 0x%zx (%zu MB)\n", retsize, retsize / (1024U * 1024U));
  105. if (stm32mp_unmap_ddr() != 0) {
  106. panic();
  107. }
  108. return 0;
  109. }
  110. int stm32mp1_ddr_probe(void)
  111. {
  112. struct stm32mp_ddr_priv *priv = &ddr_priv_data;
  113. VERBOSE("STM32MP DDR probe\n");
  114. priv->ctl = (struct stm32mp_ddrctl *)stm32mp_ddrctrl_base();
  115. priv->phy = (struct stm32mp_ddrphy *)stm32mp_ddrphyc_base();
  116. priv->pwr = stm32mp_pwr_base();
  117. priv->rcc = stm32mp_rcc_base();
  118. priv->info.base = STM32MP_DDR_BASE;
  119. priv->info.size = 0;
  120. return stm32mp1_ddr_setup();
  121. }