dram.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
  4. */
  5. #include <common.h>
  6. #include <errno.h>
  7. #include <fdtdec.h>
  8. #include <malloc.h>
  9. #include <asm/mrccache.h>
  10. #include <asm/mtrr.h>
  11. #include <asm/post.h>
  12. #include <asm/arch/mrc.h>
  13. #include <asm/arch/msg_port.h>
  14. #include <asm/arch/quark.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. static __maybe_unused int prepare_mrc_cache(struct mrc_params *mrc_params)
  17. {
  18. struct mrc_data_container *cache;
  19. struct mrc_region entry;
  20. int ret;
  21. ret = mrccache_get_region(NULL, &entry);
  22. if (ret)
  23. return ret;
  24. cache = mrccache_find_current(&entry);
  25. if (!cache)
  26. return -ENOENT;
  27. debug("%s: mrc cache at %p, size %x checksum %04x\n", __func__,
  28. cache->data, cache->data_size, cache->checksum);
  29. /* copy mrc cache to the mrc_params */
  30. memcpy(&mrc_params->timings, cache->data, cache->data_size);
  31. return 0;
  32. }
  33. static int mrc_configure_params(struct mrc_params *mrc_params)
  34. {
  35. const void *blob = gd->fdt_blob;
  36. int node;
  37. int mrc_flags;
  38. node = fdtdec_next_compatible(blob, 0, COMPAT_INTEL_QRK_MRC);
  39. if (node < 0) {
  40. debug("%s: Cannot find MRC node\n", __func__);
  41. return -EINVAL;
  42. }
  43. #ifdef CONFIG_ENABLE_MRC_CACHE
  44. mrc_params->boot_mode = prepare_mrc_cache(mrc_params);
  45. if (mrc_params->boot_mode)
  46. mrc_params->boot_mode = BM_COLD;
  47. else
  48. mrc_params->boot_mode = BM_FAST;
  49. #else
  50. mrc_params->boot_mode = BM_COLD;
  51. #endif
  52. /*
  53. * TODO:
  54. *
  55. * We need determine ECC by pin strap state
  56. *
  57. * Disable ECC by default for now
  58. */
  59. mrc_params->ecc_enables = 0;
  60. mrc_flags = fdtdec_get_int(blob, node, "flags", 0);
  61. if (mrc_flags & MRC_FLAG_SCRAMBLE_EN)
  62. mrc_params->scrambling_enables = 1;
  63. else
  64. mrc_params->scrambling_enables = 0;
  65. mrc_params->dram_width = fdtdec_get_int(blob, node, "dram-width", 0);
  66. mrc_params->ddr_speed = fdtdec_get_int(blob, node, "dram-speed", 0);
  67. mrc_params->ddr_type = fdtdec_get_int(blob, node, "dram-type", 0);
  68. mrc_params->rank_enables = fdtdec_get_int(blob, node, "rank-mask", 0);
  69. mrc_params->channel_enables = fdtdec_get_int(blob, node,
  70. "chan-mask", 0);
  71. mrc_params->channel_width = fdtdec_get_int(blob, node,
  72. "chan-width", 0);
  73. mrc_params->address_mode = fdtdec_get_int(blob, node, "addr-mode", 0);
  74. mrc_params->refresh_rate = fdtdec_get_int(blob, node,
  75. "refresh-rate", 0);
  76. mrc_params->sr_temp_range = fdtdec_get_int(blob, node,
  77. "sr-temp-range", 0);
  78. mrc_params->ron_value = fdtdec_get_int(blob, node,
  79. "ron-value", 0);
  80. mrc_params->rtt_nom_value = fdtdec_get_int(blob, node,
  81. "rtt-nom-value", 0);
  82. mrc_params->rd_odt_value = fdtdec_get_int(blob, node,
  83. "rd-odt-value", 0);
  84. mrc_params->params.density = fdtdec_get_int(blob, node,
  85. "dram-density", 0);
  86. mrc_params->params.cl = fdtdec_get_int(blob, node, "dram-cl", 0);
  87. mrc_params->params.ras = fdtdec_get_int(blob, node, "dram-ras", 0);
  88. mrc_params->params.wtr = fdtdec_get_int(blob, node, "dram-wtr", 0);
  89. mrc_params->params.rrd = fdtdec_get_int(blob, node, "dram-rrd", 0);
  90. mrc_params->params.faw = fdtdec_get_int(blob, node, "dram-faw", 0);
  91. debug("MRC dram_width %d\n", mrc_params->dram_width);
  92. debug("MRC rank_enables %d\n", mrc_params->rank_enables);
  93. debug("MRC ddr_speed %d\n", mrc_params->ddr_speed);
  94. debug("MRC flags: %s\n",
  95. (mrc_params->scrambling_enables) ? "SCRAMBLE_EN" : "");
  96. debug("MRC density=%d tCL=%d tRAS=%d tWTR=%d tRRD=%d tFAW=%d\n",
  97. mrc_params->params.density, mrc_params->params.cl,
  98. mrc_params->params.ras, mrc_params->params.wtr,
  99. mrc_params->params.rrd, mrc_params->params.faw);
  100. return 0;
  101. }
  102. int dram_init(void)
  103. {
  104. struct mrc_params mrc_params;
  105. #ifdef CONFIG_ENABLE_MRC_CACHE
  106. char *cache;
  107. #endif
  108. int ret;
  109. memset(&mrc_params, 0, sizeof(struct mrc_params));
  110. ret = mrc_configure_params(&mrc_params);
  111. if (ret)
  112. return ret;
  113. /* Set up the DRAM by calling the memory reference code */
  114. mrc_init(&mrc_params);
  115. if (mrc_params.status)
  116. return -EIO;
  117. gd->ram_size = mrc_params.mem_size;
  118. post_code(POST_DRAM);
  119. /* variable range MTRR#2: RAM area */
  120. disable_caches();
  121. msg_port_write(MSG_PORT_HOST_BRIDGE, MTRR_VAR_PHYBASE(MTRR_VAR_RAM),
  122. 0 | MTRR_TYPE_WRBACK);
  123. msg_port_write(MSG_PORT_HOST_BRIDGE, MTRR_VAR_PHYMASK(MTRR_VAR_RAM),
  124. (~(gd->ram_size - 1)) | MTRR_PHYS_MASK_VALID);
  125. enable_caches();
  126. #ifdef CONFIG_ENABLE_MRC_CACHE
  127. cache = malloc(sizeof(struct mrc_timings));
  128. if (cache) {
  129. memcpy(cache, &mrc_params.timings, sizeof(struct mrc_timings));
  130. gd->arch.mrc_output = cache;
  131. gd->arch.mrc_output_len = sizeof(struct mrc_timings);
  132. }
  133. #endif
  134. return 0;
  135. }
  136. int dram_init_banksize(void)
  137. {
  138. gd->bd->bi_dram[0].start = 0;
  139. gd->bd->bi_dram[0].size = gd->ram_size;
  140. return 0;
  141. }
  142. /*
  143. * This function looks for the highest region of memory lower than 4GB which
  144. * has enough space for U-Boot where U-Boot is aligned on a page boundary.
  145. * It overrides the default implementation found elsewhere which simply
  146. * picks the end of ram, wherever that may be. The location of the stack,
  147. * the relocation address, and how far U-Boot is moved by relocation are
  148. * set in the global data structure.
  149. */
  150. ulong board_get_usable_ram_top(ulong total_size)
  151. {
  152. return gd->ram_size;
  153. }