stm32mp2_ram.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2021-2024, 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/stm32mp2_ddr.h>
  12. #include <drivers/st/stm32mp2_ddr_helpers.h>
  13. #include <drivers/st/stm32mp2_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. static bool ddr_self_refresh;
  22. static int ddr_dt_get_ui_param(void *fdt, int node, struct stm32mp_ddr_config *config)
  23. {
  24. int ret;
  25. uint32_t size;
  26. size = sizeof(struct user_input_basic) / sizeof(int);
  27. ret = fdt_read_uint32_array(fdt, node, "st,phy-basic", size, (uint32_t *)&config->uib);
  28. VERBOSE("%s: %s[0x%x] = %d\n", __func__, "st,phy-basic", size, ret);
  29. if (ret != 0) {
  30. ERROR("%s: can't read %s, error=%d\n", __func__, "st,phy-basic", ret);
  31. return -EINVAL;
  32. }
  33. size = sizeof(struct user_input_advanced) / sizeof(int);
  34. ret = fdt_read_uint32_array(fdt, node, "st,phy-advanced", size, (uint32_t *)&config->uia);
  35. VERBOSE("%s: %s[0x%x] = %d\n", __func__, "st,phy-advanced", size, ret);
  36. if (ret != 0) {
  37. ERROR("%s: can't read %s, error=%d\n", __func__, "st,phy-advanced", ret);
  38. return -EINVAL;
  39. }
  40. size = sizeof(struct user_input_mode_register) / sizeof(int);
  41. ret = fdt_read_uint32_array(fdt, node, "st,phy-mr", size, (uint32_t *)&config->uim);
  42. VERBOSE("%s: %s[0x%x] = %d\n", __func__, "st,phy-mr", size, ret);
  43. if (ret != 0) {
  44. ERROR("%s: can't read %s, error=%d\n", __func__, "st,phy-mr", ret);
  45. return -EINVAL;
  46. }
  47. size = sizeof(struct user_input_swizzle) / sizeof(int);
  48. ret = fdt_read_uint32_array(fdt, node, "st,phy-swizzle", size, (uint32_t *)&config->uis);
  49. VERBOSE("%s: %s[0x%x] = %d\n", __func__, "st,phy-swizzle", size, ret);
  50. if (ret != 0) {
  51. ERROR("%s: can't read %s, error=%d\n", __func__, "st,phy-swizzle", ret);
  52. return -EINVAL;
  53. }
  54. return 0;
  55. }
  56. static int stm32mp2_ddr_setup(void)
  57. {
  58. struct stm32mp_ddr_priv *priv = &ddr_priv_data;
  59. int ret;
  60. struct stm32mp_ddr_config config;
  61. int node;
  62. uintptr_t uret;
  63. void *fdt;
  64. const struct stm32mp_ddr_param param[] = {
  65. CTL_PARAM(reg),
  66. CTL_PARAM(timing),
  67. CTL_PARAM(map),
  68. CTL_PARAM(perf)
  69. };
  70. if (fdt_get_address(&fdt) == 0) {
  71. return -ENOENT;
  72. }
  73. node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT);
  74. if (node < 0) {
  75. ERROR("%s: can't read DDR node in DT\n", __func__);
  76. return -EINVAL;
  77. }
  78. ret = stm32mp_ddr_dt_get_info(fdt, node, &config.info);
  79. if (ret < 0) {
  80. return ret;
  81. }
  82. ret = stm32mp_ddr_dt_get_param(fdt, node, param, ARRAY_SIZE(param), (uintptr_t)&config);
  83. if (ret < 0) {
  84. return ret;
  85. }
  86. ret = ddr_dt_get_ui_param(fdt, node, &config);
  87. if (ret < 0) {
  88. return ret;
  89. }
  90. config.self_refresh = false;
  91. if (stm32mp_is_wakeup_from_standby()) {
  92. config.self_refresh = true;
  93. }
  94. /* Map dynamically RETRAM area to save or restore PHY retention registers */
  95. if (stm32mp_map_retram() != 0) {
  96. panic();
  97. }
  98. stm32mp2_ddr_init(priv, &config);
  99. /* Unmap RETRAM, no more used until next DDR initialization call */
  100. if (stm32mp_unmap_retram() != 0) {
  101. panic();
  102. }
  103. priv->info.size = config.info.size;
  104. VERBOSE("%s : ram size(%lx, %lx)\n", __func__, priv->info.base, priv->info.size);
  105. if (stm32mp_map_ddr_non_cacheable() != 0) {
  106. panic();
  107. }
  108. if (config.self_refresh) {
  109. uret = stm32mp_ddr_test_rw_access();
  110. if (uret != 0UL) {
  111. ERROR("DDR rw test: can't access memory @ 0x%lx\n", uret);
  112. panic();
  113. }
  114. /* TODO Restore area overwritten by training */
  115. //stm32_restore_ddr_training_area();
  116. } else {
  117. size_t retsize;
  118. uret = stm32mp_ddr_test_data_bus();
  119. if (uret != 0UL) {
  120. ERROR("DDR data bus test: can't access memory @ 0x%lx\n", uret);
  121. panic();
  122. }
  123. uret = stm32mp_ddr_test_addr_bus(config.info.size);
  124. if (uret != 0UL) {
  125. ERROR("DDR addr bus test: can't access memory @ 0x%lx\n", uret);
  126. panic();
  127. }
  128. retsize = stm32mp_ddr_check_size();
  129. if (retsize < config.info.size) {
  130. ERROR("DDR size: 0x%zx does not match DT config: 0x%zx\n",
  131. retsize, config.info.size);
  132. panic();
  133. }
  134. INFO("Memory size = 0x%zx (%zu MB)\n", retsize, retsize / (1024U * 1024U));
  135. }
  136. /*
  137. * Initialization sequence has configured DDR registers with settings.
  138. * The Self Refresh (SR) mode corresponding to these settings has now
  139. * to be set.
  140. */
  141. ddr_set_sr_mode(ddr_read_sr_mode());
  142. if (stm32mp_unmap_ddr() != 0) {
  143. panic();
  144. }
  145. /* Save DDR self_refresh state */
  146. ddr_self_refresh = config.self_refresh;
  147. return 0;
  148. }
  149. bool stm32mp2_ddr_is_restored(void)
  150. {
  151. return ddr_self_refresh;
  152. }
  153. int stm32mp2_ddr_probe(void)
  154. {
  155. struct stm32mp_ddr_priv *priv = &ddr_priv_data;
  156. VERBOSE("STM32MP DDR probe\n");
  157. priv->ctl = (struct stm32mp_ddrctl *)stm32mp_ddrctrl_base();
  158. priv->phy = (struct stm32mp_ddrphy *)stm32mp_ddrphyc_base();
  159. priv->pwr = stm32mp_pwr_base();
  160. priv->rcc = stm32mp_rcc_base();
  161. priv->info.base = STM32MP_DDR_BASE;
  162. priv->info.size = 0;
  163. return stm32mp2_ddr_setup();
  164. }