spd_sdram.c 27 KB

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