0012-haswell-NRI-Post-process-selected-timings.patch 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. From faabed9ca8974b2e7192c55b59a9d28d75e72df6 Mon Sep 17 00:00:00 2001
  2. From: Angel Pons <th3fanbus@gmail.com>
  3. Date: Sat, 7 May 2022 16:29:55 +0200
  4. Subject: [PATCH 12/26] haswell NRI: Post-process selected timings
  5. Once the MPLL has been initialised, convert the timings from the SPD to
  6. be in DCLKs, which is what the hardware expects. In addition, calculate
  7. the values for tREFI and tXP.
  8. Change-Id: Id02caf858f75b9e08016762b3aefda282b274386
  9. Signed-off-by: Angel Pons <th3fanbus@gmail.com>
  10. ---
  11. .../intel/haswell/native_raminit/Makefile.inc | 1 +
  12. .../haswell/native_raminit/lookup_timings.c | 62 +++++++++++
  13. .../haswell/native_raminit/raminit_main.c | 1 +
  14. .../haswell/native_raminit/raminit_native.h | 8 ++
  15. .../haswell/native_raminit/spd_bitmunching.c | 100 ++++++++++++++++++
  16. 5 files changed, 172 insertions(+)
  17. create mode 100644 src/northbridge/intel/haswell/native_raminit/lookup_timings.c
  18. diff --git a/src/northbridge/intel/haswell/native_raminit/Makefile.inc b/src/northbridge/intel/haswell/native_raminit/Makefile.inc
  19. index c125d84f0b..2769e0bbb4 100644
  20. --- a/src/northbridge/intel/haswell/native_raminit/Makefile.inc
  21. +++ b/src/northbridge/intel/haswell/native_raminit/Makefile.inc
  22. @@ -1,5 +1,6 @@
  23. ## SPDX-License-Identifier: GPL-2.0-or-later
  24. +romstage-y += lookup_timings.c
  25. romstage-y += init_mpll.c
  26. romstage-y += io_comp_control.c
  27. romstage-y += raminit_main.c
  28. diff --git a/src/northbridge/intel/haswell/native_raminit/lookup_timings.c b/src/northbridge/intel/haswell/native_raminit/lookup_timings.c
  29. new file mode 100644
  30. index 0000000000..038686c844
  31. --- /dev/null
  32. +++ b/src/northbridge/intel/haswell/native_raminit/lookup_timings.c
  33. @@ -0,0 +1,62 @@
  34. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  35. +
  36. +#include <commonlib/clamp.h>
  37. +#include <types.h>
  38. +
  39. +#include "raminit_native.h"
  40. +
  41. +struct timing_lookup {
  42. + uint32_t clock;
  43. + uint32_t value;
  44. +};
  45. +
  46. +static uint32_t lookup_timing(
  47. + const uint32_t mem_clock_mhz,
  48. + const struct timing_lookup *const lookup,
  49. + const size_t length)
  50. +{
  51. + /* Fall back to the last index */
  52. + size_t i;
  53. + for (i = 0; i < length - 1; i++) {
  54. + /* Account for imprecise frequency values */
  55. + if ((mem_clock_mhz - 5) <= lookup[i].clock)
  56. + break;
  57. + }
  58. + return lookup[i].value;
  59. +}
  60. +
  61. +static const uint32_t fmax = UINT32_MAX;
  62. +
  63. +uint8_t get_tCWL(const uint32_t mem_clock_mhz)
  64. +{
  65. + const struct timing_lookup lut[] = {
  66. + { 400, 5 },
  67. + { 533, 6 },
  68. + { 666, 7 },
  69. + { 800, 8 },
  70. + { 933, 9 },
  71. + { 1066, 10 },
  72. + { 1200, 11 },
  73. + { fmax, 12 },
  74. + };
  75. + return lookup_timing(mem_clock_mhz, lut, ARRAY_SIZE(lut));
  76. +}
  77. +
  78. +/* tREFI = 7800 ns * DDR MHz */
  79. +uint32_t get_tREFI(const uint32_t mem_clock_mhz)
  80. +{
  81. + return (mem_clock_mhz * 7800) / 1000;
  82. +}
  83. +
  84. +uint32_t get_tXP(const uint32_t mem_clock_mhz)
  85. +{
  86. + const struct timing_lookup lut[] = {
  87. + { 400, 3 },
  88. + { 666, 4 },
  89. + { 800, 5 },
  90. + { 933, 6 },
  91. + { 1066, 7 },
  92. + { fmax, 8 },
  93. + };
  94. + return lookup_timing(mem_clock_mhz, lut, ARRAY_SIZE(lut));
  95. +}
  96. diff --git a/src/northbridge/intel/haswell/native_raminit/raminit_main.c b/src/northbridge/intel/haswell/native_raminit/raminit_main.c
  97. index 09545422c0..5f2be980d4 100644
  98. --- a/src/northbridge/intel/haswell/native_raminit/raminit_main.c
  99. +++ b/src/northbridge/intel/haswell/native_raminit/raminit_main.c
  100. @@ -22,6 +22,7 @@ struct task_entry {
  101. static const struct task_entry cold_boot[] = {
  102. { collect_spd_info, true, "PROCSPD", },
  103. { initialise_mpll, true, "INITMPLL", },
  104. + { convert_timings, true, "CONVTIM", },
  105. };
  106. /* Return a generic stepping value to make stepping checks simpler */
  107. diff --git a/src/northbridge/intel/haswell/native_raminit/raminit_native.h b/src/northbridge/intel/haswell/native_raminit/raminit_native.h
  108. index a54581abc7..01e5ed1bd6 100644
  109. --- a/src/northbridge/intel/haswell/native_raminit/raminit_native.h
  110. +++ b/src/northbridge/intel/haswell/native_raminit/raminit_native.h
  111. @@ -78,6 +78,9 @@ struct sysinfo {
  112. uint32_t tCWL;
  113. uint32_t tCMD;
  114. + uint32_t tREFI;
  115. + uint32_t tXP;
  116. +
  117. uint8_t lanes; /* 8 or 9 */
  118. uint8_t chanmap;
  119. uint8_t dpc[NUM_CHANNELS]; /* DIMMs per channel */
  120. @@ -96,7 +99,12 @@ void raminit_main(enum raminit_boot_mode bootmode);
  121. enum raminit_status collect_spd_info(struct sysinfo *ctrl);
  122. enum raminit_status initialise_mpll(struct sysinfo *ctrl);
  123. +enum raminit_status convert_timings(struct sysinfo *ctrl);
  124. enum raminit_status wait_for_first_rcomp(void);
  125. +uint8_t get_tCWL(uint32_t mem_clock_mhz);
  126. +uint32_t get_tREFI(uint32_t mem_clock_mhz);
  127. +uint32_t get_tXP(uint32_t mem_clock_mhz);
  128. +
  129. #endif
  130. diff --git a/src/northbridge/intel/haswell/native_raminit/spd_bitmunching.c b/src/northbridge/intel/haswell/native_raminit/spd_bitmunching.c
  131. index dbe02c72d0..becbea0725 100644
  132. --- a/src/northbridge/intel/haswell/native_raminit/spd_bitmunching.c
  133. +++ b/src/northbridge/intel/haswell/native_raminit/spd_bitmunching.c
  134. @@ -204,3 +204,103 @@ enum raminit_status collect_spd_info(struct sysinfo *ctrl)
  135. get_spd_data(ctrl);
  136. return find_common_spd_parameters(ctrl);
  137. }
  138. +
  139. +#define MIN_CWL 5
  140. +#define MAX_CWL 12
  141. +
  142. +/* Except for tCK, hardware expects all timing values in DCLKs, not nanoseconds */
  143. +enum raminit_status convert_timings(struct sysinfo *ctrl)
  144. +{
  145. + /*
  146. + * Obtain all required timing values, in DCLKs.
  147. + */
  148. +
  149. + /* Convert primary timings from nanoseconds to DCLKs */
  150. + ctrl->tAA = DIV_ROUND_UP(ctrl->tAA, ctrl->tCK);
  151. + ctrl->tWR = DIV_ROUND_UP(ctrl->tWR, ctrl->tCK);
  152. + ctrl->tRCD = DIV_ROUND_UP(ctrl->tRCD, ctrl->tCK);
  153. + ctrl->tRRD = DIV_ROUND_UP(ctrl->tRRD, ctrl->tCK);
  154. + ctrl->tRP = DIV_ROUND_UP(ctrl->tRP, ctrl->tCK);
  155. + ctrl->tRAS = DIV_ROUND_UP(ctrl->tRAS, ctrl->tCK);
  156. + ctrl->tRC = DIV_ROUND_UP(ctrl->tRC, ctrl->tCK);
  157. + ctrl->tRFC = DIV_ROUND_UP(ctrl->tRFC, ctrl->tCK);
  158. + ctrl->tWTR = DIV_ROUND_UP(ctrl->tWTR, ctrl->tCK);
  159. + ctrl->tRTP = DIV_ROUND_UP(ctrl->tRTP, ctrl->tCK);
  160. + ctrl->tFAW = DIV_ROUND_UP(ctrl->tFAW, ctrl->tCK);
  161. + ctrl->tCWL = DIV_ROUND_UP(ctrl->tCWL, ctrl->tCK);
  162. + ctrl->tCMD = DIV_ROUND_UP(ctrl->tCMD, ctrl->tCK);
  163. +
  164. + /* Constrain primary timings to hardware limits */
  165. + /** TODO: complain when clamping? **/
  166. + ctrl->tAA = clamp_u32(4, ctrl->tAA, 24);
  167. + ctrl->tWR = clamp_u32(5, ctrl->tWR, 16);
  168. + ctrl->tRCD = clamp_u32(4, ctrl->tRCD, 20);
  169. + ctrl->tRRD = clamp_u32(4, ctrl->tRRD, 65535);
  170. + ctrl->tRP = clamp_u32(4, ctrl->tRP, 15);
  171. + ctrl->tRAS = clamp_u32(10, ctrl->tRAS, 40);
  172. + ctrl->tRC = clamp_u32(1, ctrl->tRC, 4095);
  173. + ctrl->tRFC = clamp_u32(1, ctrl->tRFC, 511);
  174. + ctrl->tWTR = clamp_u32(4, ctrl->tWTR, 10);
  175. + ctrl->tRTP = clamp_u32(4, ctrl->tRTP, 15);
  176. + ctrl->tFAW = clamp_u32(10, ctrl->tFAW, 54);
  177. +
  178. + /** TODO: Honor tREFI from XMP **/
  179. + ctrl->tREFI = get_tREFI(ctrl->mem_clock_mhz);
  180. + ctrl->tXP = get_tXP(ctrl->mem_clock_mhz);
  181. +
  182. + /*
  183. + * Check some values, and adjust them if necessary.
  184. + */
  185. +
  186. + /* If tWR cannot be written into DDR3 MR0, adjust it */
  187. + switch (ctrl->tWR) {
  188. + case 9:
  189. + case 11:
  190. + case 13:
  191. + case 15:
  192. + ctrl->tWR++;
  193. + }
  194. +
  195. + /* If tCWL is not supported or unspecified, look up a reasonable default */
  196. + if (ctrl->tCWL < MIN_CWL || ctrl->tCWL > MAX_CWL)
  197. + ctrl->tCWL = get_tCWL(ctrl->mem_clock_mhz);
  198. +
  199. + /* This is needed to support ODT properly on 2DPC */
  200. + if (ctrl->tAA - ctrl->tCWL > 4)
  201. + ctrl->tCWL = ctrl->tAA - 4;
  202. +
  203. + /* If tCMD is invalid, use a guesstimate default */
  204. + if (!ctrl->tCMD) {
  205. + ctrl->tCMD = MAX(ctrl->dpc[0], ctrl->dpc[1]);
  206. + printk(RAM_DEBUG, "tCMD was zero, picking a guesstimate value\n");
  207. + }
  208. + ctrl->tCMD = clamp_u32(1, ctrl->tCMD, 3);
  209. +
  210. + /*
  211. + * Print final timings.
  212. + */
  213. +
  214. + /* tCK is special */
  215. + printk(BIOS_DEBUG, "Selected tCK : %u ns\n", ctrl->tCK / 256);
  216. +
  217. + /* Primary timings */
  218. + printk(BIOS_DEBUG, "Selected tAA : %uT\n", ctrl->tAA);
  219. + printk(BIOS_DEBUG, "Selected tWR : %uT\n", ctrl->tWR);
  220. + printk(BIOS_DEBUG, "Selected tRCD : %uT\n", ctrl->tRCD);
  221. + printk(BIOS_DEBUG, "Selected tRRD : %uT\n", ctrl->tRRD);
  222. + printk(BIOS_DEBUG, "Selected tRP : %uT\n", ctrl->tRP);
  223. + printk(BIOS_DEBUG, "Selected tRAS : %uT\n", ctrl->tRAS);
  224. + printk(BIOS_DEBUG, "Selected tRC : %uT\n", ctrl->tRC);
  225. + printk(BIOS_DEBUG, "Selected tRFC : %uT\n", ctrl->tRFC);
  226. + printk(BIOS_DEBUG, "Selected tWTR : %uT\n", ctrl->tWTR);
  227. + printk(BIOS_DEBUG, "Selected tRTP : %uT\n", ctrl->tRTP);
  228. + printk(BIOS_DEBUG, "Selected tFAW : %uT\n", ctrl->tFAW);
  229. + printk(BIOS_DEBUG, "Selected tCWL : %uT\n", ctrl->tCWL);
  230. + printk(BIOS_DEBUG, "Selected tCMD : %uT\n", ctrl->tCMD);
  231. +
  232. + /* Derived timings */
  233. + printk(BIOS_DEBUG, "Selected tREFI : %uT\n", ctrl->tREFI);
  234. + printk(BIOS_DEBUG, "Selected tXP : %uT\n", ctrl->tXP);
  235. +
  236. + return RAMINIT_STATUS_SUCCESS;
  237. +}
  238. --
  239. 2.39.2