dram.c 5.0 KB

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