mv_ddr_topology.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) Marvell International Ltd. and its affiliates
  4. */
  5. #include "mv_ddr_topology.h"
  6. #include "mv_ddr_common.h"
  7. #include "mv_ddr_spd.h"
  8. #include "ddr3_init.h"
  9. #include "ddr_topology_def.h"
  10. #include "ddr3_training_ip_db.h"
  11. #include "ddr3_training_ip.h"
  12. unsigned int mv_ddr_cl_calc(unsigned int taa_min, unsigned int tclk)
  13. {
  14. unsigned int cl = ceil_div(taa_min, tclk);
  15. return mv_ddr_spd_supported_cl_get(cl);
  16. }
  17. unsigned int mv_ddr_cwl_calc(unsigned int tclk)
  18. {
  19. unsigned int cwl;
  20. if (tclk >= 1250)
  21. cwl = 9;
  22. else if (tclk >= 1071)
  23. cwl = 10;
  24. else if (tclk >= 938)
  25. cwl = 11;
  26. else if (tclk >= 833)
  27. cwl = 12;
  28. else
  29. cwl = 0;
  30. return cwl;
  31. }
  32. struct mv_ddr_topology_map *mv_ddr_topology_map_update(void)
  33. {
  34. struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
  35. unsigned int octets_per_if_num = ddr3_tip_dev_attr_get(0, MV_ATTR_OCTET_PER_INTERFACE);
  36. enum hws_speed_bin speed_bin_index;
  37. enum hws_ddr_freq freq = DDR_FREQ_LAST;
  38. unsigned int tclk;
  39. unsigned char val = 0;
  40. int i;
  41. if (tm->interface_params[0].memory_freq == DDR_FREQ_SAR)
  42. tm->interface_params[0].memory_freq = mv_ddr_init_freq_get();
  43. if (tm->cfg_src == MV_DDR_CFG_SPD) {
  44. /* check dram device type */
  45. val = mv_ddr_spd_dev_type_get(&tm->spd_data);
  46. if (val != MV_DDR_SPD_DEV_TYPE_DDR4) {
  47. printf("mv_ddr: unsupported dram device type found\n");
  48. return NULL;
  49. }
  50. /* update topology map with timing data */
  51. if (mv_ddr_spd_timing_calc(&tm->spd_data, tm->timing_data) > 0) {
  52. printf("mv_ddr: negative timing data found\n");
  53. return NULL;
  54. }
  55. /* update device width in topology map */
  56. tm->interface_params[0].bus_width = mv_ddr_spd_dev_width_get(&tm->spd_data);
  57. /* update die capacity in topology map */
  58. tm->interface_params[0].memory_size = mv_ddr_spd_die_capacity_get(&tm->spd_data);
  59. /* update bus bit mask in topology map */
  60. tm->bus_act_mask = mv_ddr_bus_bit_mask_get();
  61. /* update cs bit mask in topology map */
  62. val = mv_ddr_spd_cs_bit_mask_get(&tm->spd_data);
  63. for (i = 0; i < octets_per_if_num; i++) {
  64. tm->interface_params[0].as_bus_params[i].cs_bitmask = val;
  65. }
  66. /* check dram module type */
  67. val = mv_ddr_spd_module_type_get(&tm->spd_data);
  68. switch (val) {
  69. case MV_DDR_SPD_MODULE_TYPE_UDIMM:
  70. case MV_DDR_SPD_MODULE_TYPE_SO_DIMM:
  71. case MV_DDR_SPD_MODULE_TYPE_MINI_UDIMM:
  72. case MV_DDR_SPD_MODULE_TYPE_72BIT_SO_UDIMM:
  73. case MV_DDR_SPD_MODULE_TYPE_16BIT_SO_DIMM:
  74. case MV_DDR_SPD_MODULE_TYPE_32BIT_SO_DIMM:
  75. break;
  76. default:
  77. printf("mv_ddr: unsupported dram module type found\n");
  78. return NULL;
  79. }
  80. /* update mirror bit mask in topology map */
  81. val = mv_ddr_spd_mem_mirror_get(&tm->spd_data);
  82. for (i = 0; i < octets_per_if_num; i++) {
  83. tm->interface_params[0].as_bus_params[i].mirror_enable_bitmask = val << 1;
  84. }
  85. tclk = 1000000 / freq_val[tm->interface_params[0].memory_freq];
  86. /* update cas write latency (cwl) */
  87. val = mv_ddr_cwl_calc(tclk);
  88. if (val == 0) {
  89. printf("mv_ddr: unsupported cas write latency value found\n");
  90. return NULL;
  91. }
  92. tm->interface_params[0].cas_wl = val;
  93. /* update cas latency (cl) */
  94. mv_ddr_spd_supported_cls_calc(&tm->spd_data);
  95. val = mv_ddr_cl_calc(tm->timing_data[MV_DDR_TAA_MIN], tclk);
  96. if (val == 0) {
  97. printf("mv_ddr: unsupported cas latency value found\n");
  98. return NULL;
  99. }
  100. tm->interface_params[0].cas_l = val;
  101. } else if (tm->cfg_src == MV_DDR_CFG_DEFAULT) {
  102. /* set cas and cas-write latencies per speed bin, if they unset */
  103. speed_bin_index = tm->interface_params[0].speed_bin_index;
  104. freq = tm->interface_params[0].memory_freq;
  105. if (tm->interface_params[0].cas_l == 0)
  106. tm->interface_params[0].cas_l =
  107. cas_latency_table[speed_bin_index].cl_val[freq];
  108. if (tm->interface_params[0].cas_wl == 0)
  109. tm->interface_params[0].cas_wl =
  110. cas_write_latency_table[speed_bin_index].cl_val[freq];
  111. }
  112. return tm;
  113. }
  114. unsigned short mv_ddr_bus_bit_mask_get(void)
  115. {
  116. unsigned short pri_and_ext_bus_width = 0x0;
  117. struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
  118. unsigned int octets_per_if_num = ddr3_tip_dev_attr_get(0, MV_ATTR_OCTET_PER_INTERFACE);
  119. if (tm->cfg_src == MV_DDR_CFG_SPD) {
  120. enum mv_ddr_pri_bus_width pri_bus_width = mv_ddr_spd_pri_bus_width_get(&tm->spd_data);
  121. enum mv_ddr_bus_width_ext bus_width_ext = mv_ddr_spd_bus_width_ext_get(&tm->spd_data);
  122. switch (pri_bus_width) {
  123. case MV_DDR_PRI_BUS_WIDTH_16:
  124. pri_and_ext_bus_width = BUS_MASK_16BIT;
  125. break;
  126. case MV_DDR_PRI_BUS_WIDTH_32:
  127. pri_and_ext_bus_width = BUS_MASK_32BIT;
  128. break;
  129. case MV_DDR_PRI_BUS_WIDTH_64:
  130. pri_and_ext_bus_width = MV_DDR_64BIT_BUS_MASK;
  131. break;
  132. default:
  133. pri_and_ext_bus_width = 0x0;
  134. }
  135. if (bus_width_ext == MV_DDR_BUS_WIDTH_EXT_8)
  136. pri_and_ext_bus_width |= 1 << (octets_per_if_num - 1);
  137. }
  138. return pri_and_ext_bus_width;
  139. }
  140. unsigned int mv_ddr_if_bus_width_get(void)
  141. {
  142. struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
  143. unsigned int bus_width;
  144. switch (tm->bus_act_mask) {
  145. case BUS_MASK_16BIT:
  146. case BUS_MASK_16BIT_ECC:
  147. case BUS_MASK_16BIT_ECC_PUP3:
  148. bus_width = 16;
  149. break;
  150. case BUS_MASK_32BIT:
  151. case BUS_MASK_32BIT_ECC:
  152. case MV_DDR_32BIT_ECC_PUP8_BUS_MASK:
  153. bus_width = 32;
  154. break;
  155. case MV_DDR_64BIT_BUS_MASK:
  156. case MV_DDR_64BIT_ECC_PUP8_BUS_MASK:
  157. bus_width = 64;
  158. break;
  159. default:
  160. printf("mv_ddr: unsupported bus active mask parameter found\n");
  161. bus_width = 0;
  162. }
  163. return bus_width;
  164. }