spd_sdram.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /*
  2. * Copyright 2004 Freescale Semiconductor.
  3. * (C) Copyright 2003 Motorola Inc.
  4. * Xianghua Xiao (X.Xiao@motorola.com)
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <asm/processor.h>
  26. #include <i2c.h>
  27. #include <spd.h>
  28. #include <asm/mmu.h>
  29. #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  30. extern void dma_init(void);
  31. extern uint dma_check(void);
  32. extern int dma_xfer(void *dest, uint count, void *src);
  33. #endif
  34. #ifdef CONFIG_SPD_EEPROM
  35. #ifndef CFG_READ_SPD
  36. #define CFG_READ_SPD i2c_read
  37. #endif
  38. /*
  39. * Convert picoseconds into clock cycles (rounding up if needed).
  40. */
  41. int
  42. picos_to_clk(int picos)
  43. {
  44. int clks;
  45. clks = picos / (2000000000 / (get_bus_freq(0) / 1000));
  46. if (picos % (2000000000 / (get_bus_freq(0) / 1000)) != 0) {
  47. clks++;
  48. }
  49. return clks;
  50. }
  51. /*
  52. * Calculate the Density of each Physical Rank.
  53. * Returned size is in bytes.
  54. *
  55. * Study these table from Byte 31 of JEDEC SPD Spec.
  56. *
  57. * DDR I DDR II
  58. * Bit Size Size
  59. * --- ----- ------
  60. * 7 high 512MB 512MB
  61. * 6 256MB 256MB
  62. * 5 128MB 128MB
  63. * 4 64MB 16GB
  64. * 3 32MB 8GB
  65. * 2 16MB 4GB
  66. * 1 2GB 2GB
  67. * 0 low 1GB 1GB
  68. *
  69. * Reorder Table to be linear by stripping the bottom
  70. * 2 or 5 bits off and shifting them up to the top.
  71. */
  72. unsigned int
  73. compute_banksize(unsigned int mem_type, unsigned char row_dens)
  74. {
  75. unsigned int bsize;
  76. if (mem_type == SPD_MEMTYPE_DDR) {
  77. /* Bottom 2 bits up to the top. */
  78. bsize = ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
  79. debug("DDR: DDR I rank density = 0x%08x\n", bsize);
  80. } else {
  81. /* Bottom 5 bits up to the top. */
  82. bsize = ((row_dens >> 5) | ((row_dens & 31) << 3)) << 27;
  83. debug("DDR: DDR II rank density = 0x%08x\n", bsize);
  84. }
  85. return bsize;
  86. }
  87. /*
  88. * Convert a two-nibble BCD value into a cycle time.
  89. * While the spec calls for nano-seconds, picos are returned.
  90. *
  91. * This implements the tables for bytes 9, 23 and 25 for both
  92. * DDR I and II. No allowance for distinguishing the invalid
  93. * fields absent for DDR I yet present in DDR II is made.
  94. * (That is, cycle times of .25, .33, .66 and .75 ns are
  95. * allowed for both DDR II and I.)
  96. */
  97. unsigned int
  98. convert_bcd_tenths_to_cycle_time_ps(unsigned int spd_val)
  99. {
  100. /*
  101. * Table look up the lower nibble, allow DDR I & II.
  102. */
  103. unsigned int tenths_ps[16] = {
  104. 0,
  105. 100,
  106. 200,
  107. 300,
  108. 400,
  109. 500,
  110. 600,
  111. 700,
  112. 800,
  113. 900,
  114. 250,
  115. 330, /* FIXME: Is 333 better/valid? */
  116. 660, /* FIXME: Is 667 better/valid? */
  117. 750,
  118. 0, /* undefined */
  119. 0 /* undefined */
  120. };
  121. unsigned int whole_ns = (spd_val & 0xF0) >> 4;
  122. unsigned int tenth_ns = spd_val & 0x0F;
  123. unsigned int ps = whole_ns * 1000 + tenths_ps[tenth_ns];
  124. return ps;
  125. }
  126. long int
  127. spd_sdram(void)
  128. {
  129. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  130. volatile ccsr_ddr_t *ddr1 = &immap->im_ddr1;
  131. volatile ccsr_gur_t *gur = &immap->im_gur;
  132. spd_eeprom_t spd;
  133. unsigned int n_ranks;
  134. unsigned int rank_density;
  135. unsigned int odt_rd_cfg, odt_wr_cfg;
  136. unsigned int odt_cfg, mode_odt_enable;
  137. unsigned int dqs_cfg;
  138. unsigned char twr_clk, twtr_clk, twr_auto_clk;
  139. unsigned int tCKmin_ps, tCKmax_ps;
  140. unsigned int max_data_rate, effective_data_rate;
  141. unsigned int busfreq;
  142. unsigned sdram_cfg_1;
  143. unsigned int memsize;
  144. unsigned char caslat, caslat_ctrl;
  145. unsigned int trfc, trfc_clk, trfc_low, trfc_high;
  146. unsigned int trcd_clk;
  147. unsigned int trtp_clk;
  148. unsigned char cke_min_clk;
  149. unsigned char add_lat;
  150. unsigned char wr_lat;
  151. unsigned char wr_data_delay;
  152. unsigned char four_act;
  153. unsigned char cpo;
  154. unsigned char burst_len;
  155. unsigned int mode_caslat;
  156. unsigned char sdram_type;
  157. unsigned char d_init;
  158. unsigned int law_size;
  159. volatile ccsr_local_mcm_t *mcm = &immap->im_local_mcm;
  160. /*
  161. * Read SPD information.
  162. */
  163. CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) &spd, sizeof(spd));
  164. /*
  165. * Check for supported memory module types.
  166. */
  167. if (spd.mem_type != SPD_MEMTYPE_DDR &&
  168. spd.mem_type != SPD_MEMTYPE_DDR2) {
  169. printf("Unable to locate DDR I or DDR II module.\n"
  170. " Fundamental memory type is 0x%0x\n",
  171. spd.mem_type);
  172. return 0;
  173. }
  174. /*
  175. * These test gloss over DDR I and II differences in interpretation
  176. * of bytes 3 and 4, but irrelevantly. Multiple asymmetric banks
  177. * are not supported on DDR I; and not encoded on DDR II.
  178. *
  179. * Also note that the 8548 controller can support:
  180. * 12 <= nrow <= 16
  181. * and
  182. * 8 <= ncol <= 11 (still, for DDR)
  183. * 6 <= ncol <= 9 (for FCRAM)
  184. */
  185. if (spd.nrow_addr < 12 || spd.nrow_addr > 14) {
  186. printf("DDR: Unsupported number of Row Addr lines: %d.\n",
  187. spd.nrow_addr);
  188. return 0;
  189. }
  190. if (spd.ncol_addr < 8 || spd.ncol_addr > 11) {
  191. printf("DDR: Unsupported number of Column Addr lines: %d.\n",
  192. spd.ncol_addr);
  193. return 0;
  194. }
  195. /*
  196. * Determine the number of physical banks controlled by
  197. * different Chip Select signals. This is not quite the
  198. * same as the number of DIMM modules on the board. Feh.
  199. */
  200. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  201. n_ranks = spd.nrows;
  202. } else {
  203. n_ranks = (spd.nrows & 0x7) + 1;
  204. }
  205. debug("DDR: number of ranks = %d\n", n_ranks);
  206. if (n_ranks > 2) {
  207. printf("DDR: Only 2 chip selects are supported: %d\n",
  208. n_ranks);
  209. return 0;
  210. }
  211. /*
  212. * Adjust DDR II IO voltage biasing. It just makes it work.
  213. */
  214. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  215. gur->ddrioovcr = (0
  216. | 0x80000000 /* Enable */
  217. | 0x10000000 /* VSEL to 1.8V */
  218. );
  219. }
  220. /*
  221. * Determine the size of each Rank in bytes.
  222. */
  223. rank_density = compute_banksize(spd.mem_type, spd.row_dens);
  224. /*
  225. * Eg: Bounds: 0x0000_0000 to 0x0f000_0000 first 256 Meg
  226. */
  227. ddr1->cs0_bnds = (rank_density >> 24) - 1;
  228. /*
  229. * ODT configuration recommendation from DDR Controller Chapter.
  230. */
  231. odt_rd_cfg = 0; /* Never assert ODT */
  232. odt_wr_cfg = 0; /* Never assert ODT */
  233. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  234. odt_wr_cfg = 1; /* Assert ODT on writes to CS0 */
  235. }
  236. ddr1->cs0_config = ( 1 << 31
  237. | (odt_rd_cfg << 20)
  238. | (odt_wr_cfg << 16)
  239. | (spd.nrow_addr - 12) << 8
  240. | (spd.ncol_addr - 8) );
  241. debug("\n");
  242. debug("DDR: cs0_bnds = 0x%08x\n", ddr1->cs0_bnds);
  243. debug("DDR: cs0_config = 0x%08x\n", ddr1->cs0_config);
  244. if (n_ranks == 2) {
  245. /*
  246. * Eg: Bounds: 0x0f00_0000 to 0x1e0000_0000, second 256 Meg
  247. */
  248. ddr1->cs1_bnds = ( (rank_density >> 8)
  249. | ((rank_density >> (24 - 1)) - 1) );
  250. ddr1->cs1_config = ( 1<<31
  251. | (odt_rd_cfg << 20)
  252. | (odt_wr_cfg << 16)
  253. | (spd.nrow_addr - 12) << 8
  254. | (spd.ncol_addr - 8) );
  255. debug("DDR: cs1_bnds = 0x%08x\n", ddr1->cs1_bnds);
  256. debug("DDR: cs1_config = 0x%08x\n", ddr1->cs1_config);
  257. }
  258. /*
  259. * Find the largest CAS by locating the highest 1 bit
  260. * in the spd.cas_lat field. Translate it to a DDR
  261. * controller field value:
  262. *
  263. * CAS Lat DDR I DDR II Ctrl
  264. * Clocks SPD Bit SPD Bit Value
  265. * ------- ------- ------- -----
  266. * 1.0 0 0001
  267. * 1.5 1 0010
  268. * 2.0 2 2 0011
  269. * 2.5 3 0100
  270. * 3.0 4 3 0101
  271. * 3.5 5 0110
  272. * 4.0 4 0111
  273. * 4.5 1000
  274. * 5.0 5 1001
  275. */
  276. caslat = __ilog2(spd.cas_lat);
  277. if ((spd.mem_type == SPD_MEMTYPE_DDR)
  278. && (caslat > 5)) {
  279. printf("DDR I: Invalid SPD CAS Latency: 0x%x.\n", spd.cas_lat);
  280. return 0;
  281. } else if (spd.mem_type == SPD_MEMTYPE_DDR2
  282. && (caslat < 2 || caslat > 5)) {
  283. printf("DDR II: Invalid SPD CAS Latency: 0x%x.\n",
  284. spd.cas_lat);
  285. return 0;
  286. }
  287. debug("DDR: caslat SPD bit is %d\n", caslat);
  288. /*
  289. * Calculate the Maximum Data Rate based on the Minimum Cycle time.
  290. * The SPD clk_cycle field (tCKmin) is measured in tenths of
  291. * nanoseconds and represented as BCD.
  292. */
  293. tCKmin_ps = convert_bcd_tenths_to_cycle_time_ps(spd.clk_cycle);
  294. debug("DDR: tCKmin = %d ps\n", tCKmin_ps);
  295. /*
  296. * Double-data rate, scaled 1000 to picoseconds, and back down to MHz.
  297. */
  298. max_data_rate = 2 * 1000 * 1000 / tCKmin_ps;
  299. debug("DDR: Module max data rate = %d Mhz\n", max_data_rate);
  300. /*
  301. * Adjust the CAS Latency to allow for bus speeds that
  302. * are slower than the DDR module.
  303. */
  304. busfreq = get_bus_freq(0) / 1000000; /* MHz */
  305. effective_data_rate = max_data_rate;
  306. if (busfreq < 90) {
  307. /* DDR rate out-of-range */
  308. puts("DDR: platform frequency is not fit for DDR rate\n");
  309. return 0;
  310. } else if (90 <= busfreq && busfreq < 230 && max_data_rate >= 230) {
  311. /*
  312. * busfreq 90~230 range, treated as DDR 200.
  313. */
  314. effective_data_rate = 200;
  315. if (spd.clk_cycle3 == 0xa0) /* 10 ns */
  316. caslat -= 2;
  317. else if (spd.clk_cycle2 == 0xa0)
  318. caslat--;
  319. } else if (230 <= busfreq && busfreq < 280 && max_data_rate >= 280) {
  320. /*
  321. * busfreq 230~280 range, treated as DDR 266.
  322. */
  323. effective_data_rate = 266;
  324. if (spd.clk_cycle3 == 0x75) /* 7.5 ns */
  325. caslat -= 2;
  326. else if (spd.clk_cycle2 == 0x75)
  327. caslat--;
  328. } else if (280 <= busfreq && busfreq < 350 && max_data_rate >= 350) {
  329. /*
  330. * busfreq 280~350 range, treated as DDR 333.
  331. */
  332. effective_data_rate = 333;
  333. if (spd.clk_cycle3 == 0x60) /* 6.0 ns */
  334. caslat -= 2;
  335. else if (spd.clk_cycle2 == 0x60)
  336. caslat--;
  337. } else if (350 <= busfreq && busfreq < 460 && max_data_rate >= 460) {
  338. /*
  339. * busfreq 350~460 range, treated as DDR 400.
  340. */
  341. effective_data_rate = 400;
  342. if (spd.clk_cycle3 == 0x50) /* 5.0 ns */
  343. caslat -= 2;
  344. else if (spd.clk_cycle2 == 0x50)
  345. caslat--;
  346. } else if (460 <= busfreq && busfreq < 560 && max_data_rate >= 560) {
  347. /*
  348. * busfreq 460~560 range, treated as DDR 533.
  349. */
  350. effective_data_rate = 533;
  351. if (spd.clk_cycle3 == 0x3D) /* 3.75 ns */
  352. caslat -= 2;
  353. else if (spd.clk_cycle2 == 0x3D)
  354. caslat--;
  355. } else if (560 <= busfreq && busfreq < 700 && max_data_rate >= 700) {
  356. /*
  357. * busfreq 560~700 range, treated as DDR 667.
  358. */
  359. effective_data_rate = 667;
  360. if (spd.clk_cycle3 == 0x30) /* 3.0 ns */
  361. caslat -= 2;
  362. else if (spd.clk_cycle2 == 0x30)
  363. caslat--;
  364. } else if (700 <= busfreq) {
  365. /*
  366. * DDR rate out-of-range
  367. */
  368. printf("DDR: Bus freq %d MHz is not fit for DDR rate %d MHz\n",
  369. busfreq, max_data_rate);
  370. return 0;
  371. }
  372. /*
  373. * Convert caslat clocks to DDR controller value.
  374. * Force caslat_ctrl to be DDR Controller field-sized.
  375. */
  376. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  377. caslat_ctrl = (caslat + 1) & 0x07;
  378. } else {
  379. caslat_ctrl = (2 * caslat - 1) & 0x0f;
  380. }
  381. debug("DDR: effective data rate is %d MHz\n", effective_data_rate);
  382. debug("DDR: caslat SPD bit is %d, controller field is 0x%x\n",
  383. caslat, caslat_ctrl);
  384. /*
  385. * Timing Config 0.
  386. * Avoid writing for DDR I. The new PQ38 DDR controller
  387. * dreams up non-zero default values to be backwards compatible.
  388. */
  389. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  390. unsigned char taxpd_clk = 8; /* By the book. */
  391. unsigned char tmrd_clk = 2; /* By the book. */
  392. unsigned char act_pd_exit = 2; /* Empirical? */
  393. unsigned char pre_pd_exit = 6; /* Empirical? */
  394. ddr1->timing_cfg_0 = (0
  395. | ((act_pd_exit & 0x7) << 20) /* ACT_PD_EXIT */
  396. | ((pre_pd_exit & 0x7) << 16) /* PRE_PD_EXIT */
  397. | ((taxpd_clk & 0xf) << 8) /* ODT_PD_EXIT */
  398. | ((tmrd_clk & 0xf) << 0) /* MRS_CYC */
  399. );
  400. debug("DDR: timing_cfg_0 = 0x%08x\n", ddr1->timing_cfg_0);
  401. } else {
  402. }
  403. /*
  404. * Some Timing Config 1 values now.
  405. * Sneak Extended Refresh Recovery in here too.
  406. */
  407. /*
  408. * For DDR I, WRREC(Twr) and WRTORD(Twtr) are not in SPD,
  409. * use conservative value.
  410. * For DDR II, they are bytes 36 and 37, in quarter nanos.
  411. */
  412. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  413. twr_clk = 3; /* Clocks */
  414. twtr_clk = 1; /* Clocks */
  415. } else {
  416. twr_clk = picos_to_clk(spd.twr * 250);
  417. twtr_clk = picos_to_clk(spd.twtr * 250);
  418. }
  419. /*
  420. * Calculate Trfc, in picos.
  421. * DDR I: Byte 42 straight up in ns.
  422. * DDR II: Byte 40 and 42 swizzled some, in ns.
  423. */
  424. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  425. trfc = spd.trfc * 1000; /* up to ps */
  426. } else {
  427. unsigned int byte40_table_ps[8] = {
  428. 0,
  429. 250,
  430. 330,
  431. 500,
  432. 660,
  433. 750,
  434. 0,
  435. 0
  436. };
  437. trfc = (((spd.trctrfc_ext & 0x1) * 256) + spd.trfc) * 1000
  438. + byte40_table_ps[(spd.trctrfc_ext >> 1) & 0x7];
  439. }
  440. trfc_clk = picos_to_clk(trfc);
  441. /*
  442. * Trcd, Byte 29, from quarter nanos to ps and clocks.
  443. */
  444. trcd_clk = picos_to_clk(spd.trcd * 250) & 0x7;
  445. /*
  446. * Convert trfc_clk to DDR controller fields. DDR I should
  447. * fit in the REFREC field (16-19) of TIMING_CFG_1, but the
  448. * 8548 controller has an extended REFREC field of three bits.
  449. * The controller automatically adds 8 clocks to this value,
  450. * so preadjust it down 8 first before splitting it up.
  451. */
  452. trfc_low = (trfc_clk - 8) & 0xf;
  453. trfc_high = ((trfc_clk - 8) >> 4) & 0x3;
  454. /*
  455. * Sneak in some Extended Refresh Recovery.
  456. */
  457. ddr1->ext_refrec = (trfc_high << 16);
  458. debug("DDR: ext_refrec = 0x%08x\n", ddr1->ext_refrec);
  459. ddr1->timing_cfg_1 =
  460. (0
  461. | ((picos_to_clk(spd.trp * 250) & 0x07) << 28) /* PRETOACT */
  462. | ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24) /* ACTTOPRE */
  463. | (trcd_clk << 20) /* ACTTORW */
  464. | (caslat_ctrl << 16) /* CASLAT */
  465. | (trfc_low << 12) /* REFEC */
  466. | ((twr_clk & 0x07) << 8) /* WRRREC */
  467. | ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) /* ACTTOACT */
  468. | ((twtr_clk & 0x07) << 0) /* WRTORD */
  469. );
  470. debug("DDR: timing_cfg_1 = 0x%08x\n", ddr1->timing_cfg_1);
  471. /*
  472. * Timing_Config_2
  473. * Was: 0x00000800;
  474. */
  475. /*
  476. * Additive Latency
  477. * For DDR I, 0.
  478. * For DDR II, with ODT enabled, use "a value" less than ACTTORW,
  479. * which comes from Trcd, and also note that:
  480. * add_lat + caslat must be >= 4
  481. */
  482. add_lat = 0;
  483. if (spd.mem_type == SPD_MEMTYPE_DDR2
  484. && (odt_wr_cfg || odt_rd_cfg)
  485. && (caslat < 4)) {
  486. add_lat = 4 - caslat;
  487. if (add_lat > trcd_clk) {
  488. add_lat = trcd_clk - 1;
  489. }
  490. }
  491. /*
  492. * Write Data Delay
  493. * Historically 0x2 == 4/8 clock delay.
  494. * Empirically, 0x3 == 6/8 clock delay is suggested for DDR I 266.
  495. */
  496. wr_data_delay = 3;
  497. /*
  498. * Write Latency
  499. * Read to Precharge
  500. * Minimum CKE Pulse Width.
  501. * Four Activate Window
  502. */
  503. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  504. /*
  505. * This is a lie. It should really be 1, but if it is
  506. * set to 1, bits overlap into the old controller's
  507. * otherwise unused ACSM field. If we leave it 0, then
  508. * the HW will magically treat it as 1 for DDR 1. Oh Yea.
  509. */
  510. wr_lat = 0;
  511. trtp_clk = 2; /* By the book. */
  512. cke_min_clk = 1; /* By the book. */
  513. four_act = 1; /* By the book. */
  514. } else {
  515. wr_lat = caslat - 1;
  516. /* Convert SPD value from quarter nanos to picos. */
  517. trtp_clk = picos_to_clk(spd.trtp * 250);
  518. cke_min_clk = 3; /* By the book. */
  519. four_act = picos_to_clk(37500); /* By the book. 1k pages? */
  520. }
  521. /*
  522. * Empirically set ~MCAS-to-preamble override for DDR 2.
  523. * Your milage will vary.
  524. */
  525. cpo = 0;
  526. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  527. if (effective_data_rate == 266 || effective_data_rate == 333) {
  528. cpo = 0x7; /* READ_LAT + 5/4 */
  529. } else if (effective_data_rate == 400) {
  530. cpo = 0x9; /* READ_LAT + 7/4 */
  531. } else {
  532. /* Pure speculation */
  533. cpo = 0xb;
  534. }
  535. }
  536. ddr1->timing_cfg_2 = (0
  537. | ((add_lat & 0x7) << 28) /* ADD_LAT */
  538. | ((cpo & 0x1f) << 23) /* CPO */
  539. | ((wr_lat & 0x7) << 19) /* WR_LAT */
  540. | ((trtp_clk & 0x7) << 13) /* RD_TO_PRE */
  541. | ((wr_data_delay & 0x7) << 10) /* WR_DATA_DELAY */
  542. | ((cke_min_clk & 0x7) << 6) /* CKE_PLS */
  543. | ((four_act & 0x1f) << 0) /* FOUR_ACT */
  544. );
  545. debug("DDR: timing_cfg_2 = 0x%08x\n", ddr1->timing_cfg_2);
  546. /*
  547. * Determine the Mode Register Set.
  548. *
  549. * This is nominally part specific, but it appears to be
  550. * consistent for all DDR I devices, and for all DDR II devices.
  551. *
  552. * caslat must be programmed
  553. * burst length is always 4
  554. * burst type is sequential
  555. *
  556. * For DDR I:
  557. * operating mode is "normal"
  558. *
  559. * For DDR II:
  560. * other stuff
  561. */
  562. mode_caslat = 0;
  563. /*
  564. * Table lookup from DDR I or II Device Operation Specs.
  565. */
  566. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  567. if (1 <= caslat && caslat <= 4) {
  568. unsigned char mode_caslat_table[4] = {
  569. 0x5, /* 1.5 clocks */
  570. 0x2, /* 2.0 clocks */
  571. 0x6, /* 2.5 clocks */
  572. 0x3 /* 3.0 clocks */
  573. };
  574. mode_caslat = mode_caslat_table[caslat - 1];
  575. } else {
  576. puts("DDR I: Only CAS Latencies of 1.5, 2.0, "
  577. "2.5 and 3.0 clocks are supported.\n");
  578. return 0;
  579. }
  580. } else {
  581. if (2 <= caslat && caslat <= 5) {
  582. mode_caslat = caslat;
  583. } else {
  584. puts("DDR II: Only CAS Latencies of 2.0, 3.0, "
  585. "4.0 and 5.0 clocks are supported.\n");
  586. return 0;
  587. }
  588. }
  589. /*
  590. * Encoded Burst Lenght of 4.
  591. */
  592. burst_len = 2; /* Fiat. */
  593. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  594. twr_auto_clk = 0; /* Historical */
  595. } else {
  596. /*
  597. * Determine tCK max in picos. Grab tWR and convert to picos.
  598. * Auto-precharge write recovery is:
  599. * WR = roundup(tWR_ns/tCKmax_ns).
  600. *
  601. * Ponder: Is twr_auto_clk different than twr_clk?
  602. */
  603. tCKmax_ps = convert_bcd_tenths_to_cycle_time_ps(spd.tckmax);
  604. twr_auto_clk = (spd.twr * 250 + tCKmax_ps - 1) / tCKmax_ps;
  605. }
  606. /*
  607. * Mode Reg in bits 16 ~ 31,
  608. * Extended Mode Reg 1 in bits 0 ~ 15.
  609. */
  610. mode_odt_enable = 0x0; /* Default disabled */
  611. if (odt_wr_cfg || odt_rd_cfg) {
  612. /*
  613. * Bits 6 and 2 in Extended MRS(1)
  614. * Bit 2 == 0x04 == 75 Ohm, with 2 DIMM modules.
  615. * Bit 6 == 0x40 == 150 Ohm, with 1 DIMM module.
  616. */
  617. mode_odt_enable = 0x40; /* 150 Ohm */
  618. }
  619. ddr1->sdram_mode_1 =
  620. (0
  621. | (add_lat << (16 + 3)) /* Additive Latency in EMRS1 */
  622. | (mode_odt_enable << 16) /* ODT Enable in EMRS1 */
  623. | (twr_auto_clk << 9) /* Write Recovery Autopre */
  624. | (mode_caslat << 4) /* caslat */
  625. | (burst_len << 0) /* Burst length */
  626. );
  627. debug("DDR: sdram_mode = 0x%08x\n", ddr1->sdram_mode_1);
  628. /*
  629. * Clear EMRS2 and EMRS3.
  630. */
  631. ddr1->sdram_mode_2 = 0;
  632. debug("DDR: sdram_mode_2 = 0x%08x\n", ddr1->sdram_mode_2);
  633. /*
  634. * Determine Refresh Rate. Ignore self refresh bit on DDR I.
  635. * Table from SPD Spec, Byte 12, converted to picoseconds and
  636. * filled in with "default" normal values.
  637. */
  638. {
  639. unsigned int refresh_clk;
  640. unsigned int refresh_time_ns[8] = {
  641. 15625000, /* 0 Normal 1.00x */
  642. 3900000, /* 1 Reduced .25x */
  643. 7800000, /* 2 Extended .50x */
  644. 31300000, /* 3 Extended 2.00x */
  645. 62500000, /* 4 Extended 4.00x */
  646. 125000000, /* 5 Extended 8.00x */
  647. 15625000, /* 6 Normal 1.00x filler */
  648. 15625000, /* 7 Normal 1.00x filler */
  649. };
  650. refresh_clk = picos_to_clk(refresh_time_ns[spd.refresh & 0x7]);
  651. /*
  652. * Set BSTOPRE to 0x100 for page mode
  653. * If auto-charge is used, set BSTOPRE = 0
  654. */
  655. ddr1->sdram_interval =
  656. (0
  657. | (refresh_clk & 0x3fff) << 16
  658. | 0x100
  659. );
  660. debug("DDR: sdram_interval = 0x%08x\n", ddr1->sdram_interval);
  661. }
  662. /*
  663. * Is this an ECC DDR chip?
  664. * But don't mess with it if the DDR controller will init mem.
  665. */
  666. #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  667. if (spd.config == 0x02) {
  668. ddr1->err_disable = 0x0000000d;
  669. ddr1->err_sbe = 0x00ff0000;
  670. }
  671. debug("DDR: err_disable = 0x%08x\n", ddr1->err_disable);
  672. debug("DDR: err_sbe = 0x%08x\n", ddr1->err_sbe);
  673. #endif
  674. asm("sync;isync");
  675. udelay(500);
  676. /*
  677. * SDRAM Cfg 2
  678. */
  679. /*
  680. * When ODT is enabled, Chap 9 suggests asserting ODT to
  681. * internal IOs only during reads.
  682. */
  683. odt_cfg = 0;
  684. if (odt_rd_cfg | odt_wr_cfg) {
  685. odt_cfg = 0x2; /* ODT to IOs during reads */
  686. }
  687. /*
  688. * Try to use differential DQS with DDR II.
  689. */
  690. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  691. dqs_cfg = 0; /* No Differential DQS for DDR I */
  692. } else {
  693. dqs_cfg = 0x1; /* Differential DQS for DDR II */
  694. }
  695. #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  696. /*
  697. * Use the DDR controller to auto initialize memory.
  698. */
  699. d_init = 1;
  700. ddr1->sdram_data_init = CONFIG_MEM_INIT_VALUE;
  701. debug("DDR: ddr_data_init = 0x%08x\n", ddr1->sdram_data_init);
  702. #else
  703. /*
  704. * Memory will be initialized via DMA, or not at all.
  705. */
  706. d_init = 0;
  707. #endif
  708. ddr1->sdram_cfg_2 = (0
  709. | (dqs_cfg << 26) /* Differential DQS */
  710. | (odt_cfg << 21) /* ODT */
  711. | (d_init << 4) /* D_INIT auto init DDR */
  712. );
  713. debug("DDR: sdram_cfg_2 = 0x%08x\n", ddr1->sdram_cfg_2);
  714. #ifdef MPC86xx_DDR_SDRAM_CLK_CNTL
  715. {
  716. unsigned char clk_adjust;
  717. /*
  718. * Setup the clock control.
  719. * SDRAM_CLK_CNTL[0] = Source synchronous enable == 1
  720. * SDRAM_CLK_CNTL[5-7] = Clock Adjust
  721. * 0110 3/4 cycle late
  722. * 0111 7/8 cycle late
  723. */
  724. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  725. clk_adjust = 0x6;
  726. } else {
  727. clk_adjust = 0x7;
  728. }
  729. ddr1->sdram_clk_cntl = (0
  730. | 0x80000000
  731. | (clk_adjust << 23)
  732. );
  733. debug("DDR: sdram_clk_cntl = 0x%08x\n", ddr1->sdram_clk_cntl);
  734. }
  735. #endif
  736. /*
  737. * Figure out the settings for the sdram_cfg register.
  738. * Build up the entire register in 'sdram_cfg' before writing
  739. * since the write into the register will actually enable the
  740. * memory controller; all settings must be done before enabling.
  741. *
  742. * sdram_cfg[0] = 1 (ddr sdram logic enable)
  743. * sdram_cfg[1] = 1 (self-refresh-enable)
  744. * sdram_cfg[5:7] = (SDRAM type = DDR SDRAM)
  745. * 010 DDR 1 SDRAM
  746. * 011 DDR 2 SDRAM
  747. */
  748. sdram_type = (spd.mem_type == SPD_MEMTYPE_DDR) ? 2 : 3;
  749. sdram_cfg_1 = (0
  750. | (1 << 31) /* Enable */
  751. | (1 << 30) /* Self refresh */
  752. | (sdram_type << 24) /* SDRAM type */
  753. );
  754. /*
  755. * sdram_cfg[3] = RD_EN - registered DIMM enable
  756. * A value of 0x26 indicates micron registered DIMMS (micron.com)
  757. */
  758. if (spd.mem_type == SPD_MEMTYPE_DDR && spd.mod_attr == 0x26) {
  759. sdram_cfg_1 |= 0x10000000; /* RD_EN */
  760. }
  761. #if defined(CONFIG_DDR_ECC)
  762. /*
  763. * If the user wanted ECC (enabled via sdram_cfg[2])
  764. */
  765. if (spd.config == 0x02) {
  766. sdram_cfg_1 |= 0x20000000; /* ECC_EN */
  767. }
  768. #endif
  769. /*
  770. * REV1 uses 1T timing.
  771. * REV2 may use 1T or 2T as configured by the user.
  772. */
  773. {
  774. uint pvr = get_pvr();
  775. if (pvr != PVR_85xx_REV1) {
  776. #if defined(CONFIG_DDR_2T_TIMING)
  777. /*
  778. * Enable 2T timing by setting sdram_cfg[16].
  779. */
  780. sdram_cfg_1 |= 0x8000; /* 2T_EN */
  781. #endif
  782. }
  783. }
  784. /*
  785. * 200 painful micro-seconds must elapse between
  786. * the DDR clock setup and the DDR config enable.
  787. */
  788. udelay(200);
  789. /*
  790. * Go!
  791. */
  792. ddr1->sdram_cfg_1 = sdram_cfg_1;
  793. asm("sync;isync");
  794. udelay(500);
  795. debug("DDR: sdram_cfg = 0x%08x\n", ddr1->sdram_cfg_1);
  796. #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  797. debug("DDR: memory initializing\n");
  798. /*
  799. * Poll until memory is initialized.
  800. * 512 Meg at 400 might hit this 200 times or so.
  801. */
  802. while ((ddr1->sdram_cfg_2 & (d_init << 4)) != 0) {
  803. udelay(1000);
  804. }
  805. debug("DDR: memory initialized\n");
  806. #endif
  807. /*
  808. * Figure out memory size in Megabytes.
  809. */
  810. memsize = n_ranks * rank_density / 0x100000;
  811. /*
  812. * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23. Fnord.
  813. */
  814. law_size = 19 + __ilog2(memsize);
  815. /*
  816. * Set up LAWBAR for all of DDR.
  817. */
  818. mcm->lawbar1 = ((CFG_DDR_SDRAM_BASE >> 12) & 0xfffff);
  819. mcm->lawar1 = (LAWAR_EN
  820. | LAWAR_TRGT_IF_DDR
  821. | (LAWAR_SIZE & law_size));
  822. debug("DDR: LAWBAR1=0x%08x\n", mcm->lawbar1);
  823. debug("DDR: LARAR1=0x%08x\n", mcm->lawar1);
  824. return memsize * 1024 * 1024;
  825. }
  826. #endif /* CONFIG_SPD_EEPROM */
  827. #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  828. /*
  829. * Initialize all of memory for ECC, then enable errors.
  830. */
  831. void
  832. ddr_enable_ecc(unsigned int dram_size)
  833. {
  834. uint *p = 0;
  835. uint i = 0;
  836. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  837. volatile ccsr_ddr_t *ddr1= &immap->im_ddr1;
  838. dma_init();
  839. for (*p = 0; p < (uint *)(8 * 1024); p++) {
  840. if (((unsigned int)p & 0x1f) == 0) {
  841. ppcDcbz((unsigned long) p);
  842. }
  843. *p = (unsigned int)CONFIG_MEM_INIT_VALUE;
  844. if (((unsigned int)p & 0x1c) == 0x1c) {
  845. ppcDcbf((unsigned long) p);
  846. }
  847. }
  848. /* 8K */
  849. dma_xfer((uint *)0x2000, 0x2000, (uint *)0);
  850. /* 16K */
  851. dma_xfer((uint *)0x4000, 0x4000, (uint *)0);
  852. /* 32K */
  853. dma_xfer((uint *)0x8000, 0x8000, (uint *)0);
  854. /* 64K */
  855. dma_xfer((uint *)0x10000, 0x10000, (uint *)0);
  856. /* 128k */
  857. dma_xfer((uint *)0x20000, 0x20000, (uint *)0);
  858. /* 256k */
  859. dma_xfer((uint *)0x40000, 0x40000, (uint *)0);
  860. /* 512k */
  861. dma_xfer((uint *)0x80000, 0x80000, (uint *)0);
  862. /* 1M */
  863. dma_xfer((uint *)0x100000, 0x100000, (uint *)0);
  864. /* 2M */
  865. dma_xfer((uint *)0x200000, 0x200000, (uint *)0);
  866. /* 4M */
  867. dma_xfer((uint *)0x400000, 0x400000, (uint *)0);
  868. for (i = 1; i < dram_size / 0x800000; i++) {
  869. dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
  870. }
  871. /*
  872. * Enable errors for ECC.
  873. */
  874. debug("DMA DDR: err_disable = 0x%08x\n", ddr1->err_disable);
  875. ddr1->err_disable = 0x00000000;
  876. asm("sync;isync;msync");
  877. debug("DMA DDR: err_disable = 0x%08x\n", ddr1->err_disable);
  878. }
  879. #endif /* CONFIG_DDR_ECC && ! CONFIG_ECC_INIT_VIA_DDRCONTROLLER */