ddr2_dimm_params.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright 2008 Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * Version 2 as published by the Free Software Foundation.
  7. */
  8. #include <common.h>
  9. #include <asm/fsl_ddr_sdram.h>
  10. #include "ddr.h"
  11. /*
  12. * Calculate the Density of each Physical Rank.
  13. * Returned size is in bytes.
  14. *
  15. * Study these table from Byte 31 of JEDEC SPD Spec.
  16. *
  17. * DDR I DDR II
  18. * Bit Size Size
  19. * --- ----- ------
  20. * 7 high 512MB 512MB
  21. * 6 256MB 256MB
  22. * 5 128MB 128MB
  23. * 4 64MB 16GB
  24. * 3 32MB 8GB
  25. * 2 16MB 4GB
  26. * 1 2GB 2GB
  27. * 0 low 1GB 1GB
  28. *
  29. * Reorder Table to be linear by stripping the bottom
  30. * 2 or 5 bits off and shifting them up to the top.
  31. *
  32. */
  33. static phys_size_t
  34. compute_ranksize(unsigned int mem_type, unsigned char row_dens)
  35. {
  36. phys_size_t bsize;
  37. /* Bottom 5 bits up to the top. */
  38. bsize = ((row_dens >> 5) | ((row_dens & 31) << 3));
  39. bsize <<= 27ULL;
  40. debug("DDR: DDR II rank density = 0x%08x\n", bsize);
  41. return bsize;
  42. }
  43. /*
  44. * Convert a two-nibble BCD value into a cycle time.
  45. * While the spec calls for nano-seconds, picos are returned.
  46. *
  47. * This implements the tables for bytes 9, 23 and 25 for both
  48. * DDR I and II. No allowance for distinguishing the invalid
  49. * fields absent for DDR I yet present in DDR II is made.
  50. * (That is, cycle times of .25, .33, .66 and .75 ns are
  51. * allowed for both DDR II and I.)
  52. */
  53. static unsigned int
  54. convert_bcd_tenths_to_cycle_time_ps(unsigned int spd_val)
  55. {
  56. /* Table look up the lower nibble, allow DDR I & II. */
  57. unsigned int tenths_ps[16] = {
  58. 0,
  59. 100,
  60. 200,
  61. 300,
  62. 400,
  63. 500,
  64. 600,
  65. 700,
  66. 800,
  67. 900,
  68. 250, /* This and the next 3 entries valid ... */
  69. 330, /* ... only for tCK calculations. */
  70. 660,
  71. 750,
  72. 0, /* undefined */
  73. 0 /* undefined */
  74. };
  75. unsigned int whole_ns = (spd_val & 0xF0) >> 4;
  76. unsigned int tenth_ns = spd_val & 0x0F;
  77. unsigned int ps = whole_ns * 1000 + tenths_ps[tenth_ns];
  78. return ps;
  79. }
  80. static unsigned int
  81. convert_bcd_hundredths_to_cycle_time_ps(unsigned int spd_val)
  82. {
  83. unsigned int tenth_ns = (spd_val & 0xF0) >> 4;
  84. unsigned int hundredth_ns = spd_val & 0x0F;
  85. unsigned int ps = tenth_ns * 100 + hundredth_ns * 10;
  86. return ps;
  87. }
  88. static unsigned int byte40_table_ps[8] = {
  89. 0,
  90. 250,
  91. 330,
  92. 500,
  93. 660,
  94. 750,
  95. 0, /* supposed to be RFC, but not sure what that means */
  96. 0 /* Undefined */
  97. };
  98. static unsigned int
  99. compute_trfc_ps_from_spd(unsigned char trctrfc_ext, unsigned char trfc)
  100. {
  101. unsigned int trfc_ps;
  102. trfc_ps = (((trctrfc_ext & 0x1) * 256) + trfc) * 1000
  103. + byte40_table_ps[(trctrfc_ext >> 1) & 0x7];
  104. return trfc_ps;
  105. }
  106. static unsigned int
  107. compute_trc_ps_from_spd(unsigned char trctrfc_ext, unsigned char trc)
  108. {
  109. unsigned int trc_ps;
  110. trc_ps = trc * 1000 + byte40_table_ps[(trctrfc_ext >> 4) & 0x7];
  111. return trc_ps;
  112. }
  113. /*
  114. * Determine Refresh Rate. Ignore self refresh bit on DDR I.
  115. * Table from SPD Spec, Byte 12, converted to picoseconds and
  116. * filled in with "default" normal values.
  117. */
  118. static unsigned int
  119. determine_refresh_rate_ps(const unsigned int spd_refresh)
  120. {
  121. unsigned int refresh_time_ps[8] = {
  122. 15625000, /* 0 Normal 1.00x */
  123. 3900000, /* 1 Reduced .25x */
  124. 7800000, /* 2 Extended .50x */
  125. 31300000, /* 3 Extended 2.00x */
  126. 62500000, /* 4 Extended 4.00x */
  127. 125000000, /* 5 Extended 8.00x */
  128. 15625000, /* 6 Normal 1.00x filler */
  129. 15625000, /* 7 Normal 1.00x filler */
  130. };
  131. return refresh_time_ps[spd_refresh & 0x7];
  132. }
  133. /*
  134. * The purpose of this function is to compute a suitable
  135. * CAS latency given the DRAM clock period. The SPD only
  136. * defines at most 3 CAS latencies. Typically the slower in
  137. * frequency the DIMM runs at, the shorter its CAS latency can.
  138. * be. If the DIMM is operating at a sufficiently low frequency,
  139. * it may be able to run at a CAS latency shorter than the
  140. * shortest SPD-defined CAS latency.
  141. *
  142. * If a CAS latency is not found, 0 is returned.
  143. *
  144. * Do this by finding in the standard speed bin table the longest
  145. * tCKmin that doesn't exceed the value of mclk_ps (tCK).
  146. *
  147. * An assumption made is that the SDRAM device allows the
  148. * CL to be programmed for a value that is lower than those
  149. * advertised by the SPD. This is not always the case,
  150. * as those modes not defined in the SPD are optional.
  151. *
  152. * CAS latency de-rating based upon values JEDEC Standard No. 79-2C
  153. * Table 40, "DDR2 SDRAM stanadard speed bins and tCK, tRCD, tRP, tRAS,
  154. * and tRC for corresponding bin"
  155. *
  156. * ordinal 2, ddr2_speed_bins[1] contains tCK for CL=3
  157. * Not certain if any good value exists for CL=2
  158. */
  159. /* CL2 CL3 CL4 CL5 CL6 */
  160. unsigned short ddr2_speed_bins[] = { 0, 5000, 3750, 3000, 2500 };
  161. unsigned int
  162. compute_derated_DDR2_CAS_latency(unsigned int mclk_ps)
  163. {
  164. const unsigned int num_speed_bins = ARRAY_SIZE(ddr2_speed_bins);
  165. unsigned int lowest_tCKmin_found = 0;
  166. unsigned int lowest_tCKmin_CL = 0;
  167. unsigned int i;
  168. debug("mclk_ps = %u\n", mclk_ps);
  169. for (i = 0; i < num_speed_bins; i++) {
  170. unsigned int x = ddr2_speed_bins[i];
  171. debug("i=%u, x = %u, lowest_tCKmin_found = %u\n",
  172. i, x, lowest_tCKmin_found);
  173. if (x && x <= mclk_ps && x >= lowest_tCKmin_found ) {
  174. lowest_tCKmin_found = x;
  175. lowest_tCKmin_CL = i + 2;
  176. }
  177. }
  178. debug("lowest_tCKmin_CL = %u\n", lowest_tCKmin_CL);
  179. return lowest_tCKmin_CL;
  180. }
  181. /*
  182. * ddr_compute_dimm_parameters for DDR2 SPD
  183. *
  184. * Compute DIMM parameters based upon the SPD information in spd.
  185. * Writes the results to the dimm_params_t structure pointed by pdimm.
  186. *
  187. * FIXME: use #define for the retvals
  188. */
  189. unsigned int
  190. ddr_compute_dimm_parameters(const ddr2_spd_eeprom_t *spd,
  191. dimm_params_t *pdimm,
  192. unsigned int dimm_number)
  193. {
  194. unsigned int retval;
  195. if (spd->mem_type) {
  196. if (spd->mem_type != SPD_MEMTYPE_DDR2) {
  197. printf("DIMM %u: is not a DDR2 SPD.\n", dimm_number);
  198. return 1;
  199. }
  200. } else {
  201. memset(pdimm, 0, sizeof(dimm_params_t));
  202. return 1;
  203. }
  204. retval = ddr2_spd_check(spd);
  205. if (retval) {
  206. printf("DIMM %u: failed checksum\n", dimm_number);
  207. return 2;
  208. }
  209. /*
  210. * The part name in ASCII in the SPD EEPROM is not null terminated.
  211. * Guarantee null termination here by presetting all bytes to 0
  212. * and copying the part name in ASCII from the SPD onto it
  213. */
  214. memset(pdimm->mpart, 0, sizeof(pdimm->mpart));
  215. memcpy(pdimm->mpart, spd->mpart, sizeof(pdimm->mpart) - 1);
  216. /* DIMM organization parameters */
  217. pdimm->n_ranks = (spd->mod_ranks & 0x7) + 1;
  218. pdimm->rank_density = compute_ranksize(spd->mem_type, spd->rank_dens);
  219. pdimm->capacity = pdimm->n_ranks * pdimm->rank_density;
  220. pdimm->data_width = spd->dataw;
  221. pdimm->primary_sdram_width = spd->primw;
  222. pdimm->ec_sdram_width = spd->ecw;
  223. /* FIXME: what about registered SO-DIMM? */
  224. switch (spd->dimm_type) {
  225. case 0x01: /* RDIMM */
  226. case 0x10: /* Mini-RDIMM */
  227. pdimm->registered_dimm = 1; /* register buffered */
  228. break;
  229. case 0x02: /* UDIMM */
  230. case 0x04: /* SO-DIMM */
  231. case 0x08: /* Micro-DIMM */
  232. case 0x20: /* Mini-UDIMM */
  233. pdimm->registered_dimm = 0; /* unbuffered */
  234. break;
  235. default:
  236. printf("unknown dimm_type 0x%02X\n", spd->dimm_type);
  237. return 1;
  238. break;
  239. }
  240. /* SDRAM device parameters */
  241. pdimm->n_row_addr = spd->nrow_addr;
  242. pdimm->n_col_addr = spd->ncol_addr;
  243. pdimm->n_banks_per_sdram_device = spd->nbanks;
  244. pdimm->edc_config = spd->config;
  245. pdimm->burst_lengths_bitmask = spd->burstl;
  246. pdimm->row_density = spd->rank_dens;
  247. /*
  248. * Calculate the Maximum Data Rate based on the Minimum Cycle time.
  249. * The SPD clk_cycle field (tCKmin) is measured in tenths of
  250. * nanoseconds and represented as BCD.
  251. */
  252. pdimm->tCKmin_X_ps
  253. = convert_bcd_tenths_to_cycle_time_ps(spd->clk_cycle);
  254. pdimm->tCKmin_X_minus_1_ps
  255. = convert_bcd_tenths_to_cycle_time_ps(spd->clk_cycle2);
  256. pdimm->tCKmin_X_minus_2_ps
  257. = convert_bcd_tenths_to_cycle_time_ps(spd->clk_cycle3);
  258. pdimm->tCKmax_ps = convert_bcd_tenths_to_cycle_time_ps(spd->tckmax);
  259. /*
  260. * Compute CAS latencies defined by SPD
  261. * The SPD caslat_X should have at least 1 and at most 3 bits set.
  262. *
  263. * If cas_lat after masking is 0, the __ilog2 function returns
  264. * 255 into the variable. This behavior is abused once.
  265. */
  266. pdimm->caslat_X = __ilog2(spd->cas_lat);
  267. pdimm->caslat_X_minus_1 = __ilog2(spd->cas_lat
  268. & ~(1 << pdimm->caslat_X));
  269. pdimm->caslat_X_minus_2 = __ilog2(spd->cas_lat
  270. & ~(1 << pdimm->caslat_X)
  271. & ~(1 << pdimm->caslat_X_minus_1));
  272. /* Compute CAS latencies below that defined by SPD */
  273. pdimm->caslat_lowest_derated
  274. = compute_derated_DDR2_CAS_latency(get_memory_clk_period_ps());
  275. /* Compute timing parameters */
  276. pdimm->tRCD_ps = spd->trcd * 250;
  277. pdimm->tRP_ps = spd->trp * 250;
  278. pdimm->tRAS_ps = spd->tras * 1000;
  279. pdimm->tWR_ps = spd->twr * 250;
  280. pdimm->tWTR_ps = spd->twtr * 250;
  281. pdimm->tRFC_ps = compute_trfc_ps_from_spd(spd->trctrfc_ext, spd->trfc);
  282. pdimm->tRRD_ps = spd->trrd * 250;
  283. pdimm->tRC_ps = compute_trc_ps_from_spd(spd->trctrfc_ext, spd->trc);
  284. pdimm->refresh_rate_ps = determine_refresh_rate_ps(spd->refresh);
  285. pdimm->tIS_ps = convert_bcd_hundredths_to_cycle_time_ps(spd->ca_setup);
  286. pdimm->tIH_ps = convert_bcd_hundredths_to_cycle_time_ps(spd->ca_hold);
  287. pdimm->tDS_ps
  288. = convert_bcd_hundredths_to_cycle_time_ps(spd->data_setup);
  289. pdimm->tDH_ps
  290. = convert_bcd_hundredths_to_cycle_time_ps(spd->data_hold);
  291. pdimm->tRTP_ps = spd->trtp * 250;
  292. pdimm->tDQSQ_max_ps = spd->tdqsq * 10;
  293. pdimm->tQHS_ps = spd->tqhs * 10;
  294. return 0;
  295. }