ddr1_dimm_params.c 9.1 KB

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