spd_sdram.c 27 KB

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