spd_sdram.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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. #include <asm/fsl_law.h>
  30. #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  31. extern void dma_init(void);
  32. extern uint dma_check(void);
  33. extern int dma_xfer(void *dest, uint count, void *src);
  34. #endif
  35. #ifdef CONFIG_SPD_EEPROM
  36. #ifndef CFG_READ_SPD
  37. #define CFG_READ_SPD i2c_read
  38. #endif
  39. /*
  40. * Only one of the following three should be 1; others should be 0
  41. * By default the cache line interleaving is selected if
  42. * the CONFIG_DDR_INTERLEAVE flag is defined
  43. */
  44. #define CFG_PAGE_INTERLEAVING 0
  45. #define CFG_BANK_INTERLEAVING 0
  46. #define CFG_SUPER_BANK_INTERLEAVING 0
  47. /*
  48. * Convert picoseconds into DRAM clock cycles (rounding up if needed).
  49. */
  50. static unsigned int
  51. picos_to_clk(unsigned int picos)
  52. {
  53. /* use unsigned long long to avoid rounding errors */
  54. const unsigned long long ULL_2e12 = 2000000000000ULL;
  55. unsigned long long clks;
  56. unsigned long long clks_temp;
  57. if (! picos)
  58. return 0;
  59. clks = get_bus_freq(0) * (unsigned long long) picos;
  60. clks_temp = clks;
  61. clks = clks / ULL_2e12;
  62. if (clks_temp % ULL_2e12) {
  63. clks++;
  64. }
  65. if (clks > 0xFFFFFFFFULL) {
  66. clks = 0xFFFFFFFFULL;
  67. }
  68. return (unsigned int) clks;
  69. }
  70. /*
  71. * Calculate the Density of each Physical Rank.
  72. * Returned size is in bytes.
  73. *
  74. * Study these table from Byte 31 of JEDEC SPD Spec.
  75. *
  76. * DDR I DDR II
  77. * Bit Size Size
  78. * --- ----- ------
  79. * 7 high 512MB 512MB
  80. * 6 256MB 256MB
  81. * 5 128MB 128MB
  82. * 4 64MB 16GB
  83. * 3 32MB 8GB
  84. * 2 16MB 4GB
  85. * 1 2GB 2GB
  86. * 0 low 1GB 1GB
  87. *
  88. * Reorder Table to be linear by stripping the bottom
  89. * 2 or 5 bits off and shifting them up to the top.
  90. */
  91. unsigned int
  92. compute_banksize(unsigned int mem_type, unsigned char row_dens)
  93. {
  94. unsigned int bsize;
  95. if (mem_type == SPD_MEMTYPE_DDR) {
  96. /* Bottom 2 bits up to the top. */
  97. bsize = ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
  98. debug("DDR: DDR I rank density = 0x%08x\n", bsize);
  99. } else {
  100. /* Bottom 5 bits up to the top. */
  101. bsize = ((row_dens >> 5) | ((row_dens & 31) << 3)) << 27;
  102. debug("DDR: DDR II rank density = 0x%08x\n", bsize);
  103. }
  104. return bsize;
  105. }
  106. /*
  107. * Convert a two-nibble BCD value into a cycle time.
  108. * While the spec calls for nano-seconds, picos are returned.
  109. *
  110. * This implements the tables for bytes 9, 23 and 25 for both
  111. * DDR I and II. No allowance for distinguishing the invalid
  112. * fields absent for DDR I yet present in DDR II is made.
  113. * (That is, cycle times of .25, .33, .66 and .75 ns are
  114. * allowed for both DDR II and I.)
  115. */
  116. unsigned int
  117. convert_bcd_tenths_to_cycle_time_ps(unsigned int spd_val)
  118. {
  119. /*
  120. * Table look up the lower nibble, allow DDR I & II.
  121. */
  122. unsigned int tenths_ps[16] = {
  123. 0,
  124. 100,
  125. 200,
  126. 300,
  127. 400,
  128. 500,
  129. 600,
  130. 700,
  131. 800,
  132. 900,
  133. 250,
  134. 330,
  135. 660,
  136. 750,
  137. 0, /* undefined */
  138. 0 /* undefined */
  139. };
  140. unsigned int whole_ns = (spd_val & 0xF0) >> 4;
  141. unsigned int tenth_ns = spd_val & 0x0F;
  142. unsigned int ps = whole_ns * 1000 + tenths_ps[tenth_ns];
  143. return ps;
  144. }
  145. /*
  146. * Determine Refresh Rate. Ignore self refresh bit on DDR I.
  147. * Table from SPD Spec, Byte 12, converted to picoseconds and
  148. * filled in with "default" normal values.
  149. */
  150. unsigned int determine_refresh_rate(unsigned int spd_refresh)
  151. {
  152. unsigned int refresh_time_ns[8] = {
  153. 15625000, /* 0 Normal 1.00x */
  154. 3900000, /* 1 Reduced .25x */
  155. 7800000, /* 2 Extended .50x */
  156. 31300000, /* 3 Extended 2.00x */
  157. 62500000, /* 4 Extended 4.00x */
  158. 125000000, /* 5 Extended 8.00x */
  159. 15625000, /* 6 Normal 1.00x filler */
  160. 15625000, /* 7 Normal 1.00x filler */
  161. };
  162. return picos_to_clk(refresh_time_ns[spd_refresh & 0x7]);
  163. }
  164. long int
  165. spd_init(unsigned char i2c_address, unsigned int ddr_num,
  166. unsigned int dimm_num, unsigned int start_addr)
  167. {
  168. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  169. volatile ccsr_ddr_t *ddr;
  170. volatile ccsr_gur_t *gur = &immap->im_gur;
  171. spd_eeprom_t spd;
  172. unsigned int n_ranks;
  173. unsigned int rank_density;
  174. unsigned int odt_rd_cfg, odt_wr_cfg, ba_bits;
  175. unsigned int odt_cfg, mode_odt_enable;
  176. unsigned int refresh_clk;
  177. #ifdef MPC86xx_DDR_SDRAM_CLK_CNTL
  178. unsigned char clk_adjust;
  179. #endif
  180. unsigned int dqs_cfg;
  181. unsigned char twr_clk, twtr_clk, twr_auto_clk;
  182. unsigned int tCKmin_ps, tCKmax_ps;
  183. unsigned int max_data_rate;
  184. unsigned int busfreq;
  185. unsigned int memsize;
  186. unsigned char caslat, caslat_ctrl;
  187. unsigned int trfc, trfc_clk, trfc_low, trfc_high;
  188. unsigned int trcd_clk;
  189. unsigned int trtp_clk;
  190. unsigned char cke_min_clk;
  191. unsigned char add_lat;
  192. unsigned char wr_lat;
  193. unsigned char wr_data_delay;
  194. unsigned char four_act;
  195. unsigned char cpo;
  196. unsigned char burst_len;
  197. unsigned int mode_caslat;
  198. unsigned char d_init;
  199. unsigned int tCycle_ps, modfreq;
  200. if (ddr_num == 1)
  201. ddr = &immap->im_ddr1;
  202. else
  203. ddr = &immap->im_ddr2;
  204. /*
  205. * Read SPD information.
  206. */
  207. debug("Performing SPD read at I2C address 0x%02lx\n",i2c_address);
  208. memset((void *)&spd, 0, sizeof(spd));
  209. CFG_READ_SPD(i2c_address, 0, 1, (uchar *) &spd, sizeof(spd));
  210. /*
  211. * Check for supported memory module types.
  212. */
  213. if (spd.mem_type != SPD_MEMTYPE_DDR &&
  214. spd.mem_type != SPD_MEMTYPE_DDR2) {
  215. debug("Warning: Unable to locate DDR I or DDR II module for DIMM %d of DDR controller %d.\n"
  216. " Fundamental memory type is 0x%0x\n",
  217. dimm_num,
  218. ddr_num,
  219. spd.mem_type);
  220. return 0;
  221. }
  222. debug("\nFound memory of type 0x%02lx ", spd.mem_type);
  223. if (spd.mem_type == SPD_MEMTYPE_DDR)
  224. debug("DDR I\n");
  225. else
  226. debug("DDR II\n");
  227. /*
  228. * These test gloss over DDR I and II differences in interpretation
  229. * of bytes 3 and 4, but irrelevantly. Multiple asymmetric banks
  230. * are not supported on DDR I; and not encoded on DDR II.
  231. *
  232. * Also note that the 8548 controller can support:
  233. * 12 <= nrow <= 16
  234. * and
  235. * 8 <= ncol <= 11 (still, for DDR)
  236. * 6 <= ncol <= 9 (for FCRAM)
  237. */
  238. if (spd.nrow_addr < 12 || spd.nrow_addr > 14) {
  239. printf("DDR: Unsupported number of Row Addr lines: %d.\n",
  240. spd.nrow_addr);
  241. return 0;
  242. }
  243. if (spd.ncol_addr < 8 || spd.ncol_addr > 11) {
  244. printf("DDR: Unsupported number of Column Addr lines: %d.\n",
  245. spd.ncol_addr);
  246. return 0;
  247. }
  248. /*
  249. * Determine the number of physical banks controlled by
  250. * different Chip Select signals. This is not quite the
  251. * same as the number of DIMM modules on the board. Feh.
  252. */
  253. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  254. n_ranks = spd.nrows;
  255. } else {
  256. n_ranks = (spd.nrows & 0x7) + 1;
  257. }
  258. debug("DDR: number of ranks = %d\n", n_ranks);
  259. if (n_ranks > 2) {
  260. printf("DDR: Only 2 chip selects are supported: %d\n",
  261. n_ranks);
  262. return 0;
  263. }
  264. /*
  265. * Adjust DDR II IO voltage biasing. Rev1 only
  266. */
  267. if (((get_svr() & 0xf0) == 0x10) && (spd.mem_type == SPD_MEMTYPE_DDR2)) {
  268. gur->ddrioovcr = (0
  269. | 0x80000000 /* Enable */
  270. | 0x10000000 /* VSEL to 1.8V */
  271. );
  272. }
  273. /*
  274. * Determine the size of each Rank in bytes.
  275. */
  276. rank_density = compute_banksize(spd.mem_type, spd.row_dens);
  277. debug("Start address for this controller is 0x%08lx\n", start_addr);
  278. /*
  279. * ODT configuration recommendation from DDR Controller Chapter.
  280. */
  281. odt_rd_cfg = 0; /* Never assert ODT */
  282. odt_wr_cfg = 0; /* Never assert ODT */
  283. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  284. odt_wr_cfg = 1; /* Assert ODT on writes to CS0 */
  285. }
  286. ba_bits = 0;
  287. if (spd.nbanks == 0x8)
  288. ba_bits = 1;
  289. #ifdef CONFIG_DDR_INTERLEAVE
  290. if (dimm_num != 1) {
  291. printf("For interleaving memory on HPCN, need to use DIMM 1 for DDR Controller %d !\n", ddr_num);
  292. return 0;
  293. } else {
  294. /*
  295. * Since interleaved memory only uses CS0, the
  296. * memory sticks have to be identical in size and quantity
  297. * of ranks. That essentially gives double the size on
  298. * one rank, i.e on CS0 for both controllers put together.
  299. * Confirm this???
  300. */
  301. rank_density *= 2;
  302. /*
  303. * Eg: Bounds: 0x0000_0000 to 0x0f000_0000 first 256 Meg
  304. */
  305. start_addr = 0;
  306. ddr->cs0_bnds = (start_addr >> 8)
  307. | (((start_addr + rank_density - 1) >> 24));
  308. /*
  309. * Default interleaving mode to cache-line interleaving.
  310. */
  311. ddr->cs0_config = ( 1 << 31
  312. #if (CFG_PAGE_INTERLEAVING == 1)
  313. | (PAGE_INTERLEAVING)
  314. #elif (CFG_BANK_INTERLEAVING == 1)
  315. | (BANK_INTERLEAVING)
  316. #elif (CFG_SUPER_BANK_INTERLEAVING == 1)
  317. | (SUPER_BANK_INTERLEAVING)
  318. #else
  319. | (CACHE_LINE_INTERLEAVING)
  320. #endif
  321. | (odt_rd_cfg << 20)
  322. | (odt_wr_cfg << 16)
  323. | (ba_bits << 14)
  324. | (spd.nrow_addr - 12) << 8
  325. | (spd.ncol_addr - 8) );
  326. debug("DDR: cs0_bnds = 0x%08x\n", ddr->cs0_bnds);
  327. debug("DDR: cs0_config = 0x%08x\n", ddr->cs0_config);
  328. /*
  329. * Adjustment for dual rank memory to get correct memory
  330. * size (return value of this function).
  331. */
  332. if (n_ranks == 2) {
  333. n_ranks = 1;
  334. rank_density /= 2;
  335. } else {
  336. rank_density /= 2;
  337. }
  338. }
  339. #else /* CONFIG_DDR_INTERLEAVE */
  340. if (dimm_num == 1) {
  341. /*
  342. * Eg: Bounds: 0x0000_0000 to 0x0f000_0000 first 256 Meg
  343. */
  344. ddr->cs0_bnds = (start_addr >> 8)
  345. | (((start_addr + rank_density - 1) >> 24));
  346. ddr->cs0_config = ( 1 << 31
  347. | (odt_rd_cfg << 20)
  348. | (odt_wr_cfg << 16)
  349. | (ba_bits << 14)
  350. | (spd.nrow_addr - 12) << 8
  351. | (spd.ncol_addr - 8) );
  352. debug("DDR: cs0_bnds = 0x%08x\n", ddr->cs0_bnds);
  353. debug("DDR: cs0_config = 0x%08x\n", ddr->cs0_config);
  354. if (n_ranks == 2) {
  355. /*
  356. * Eg: Bounds: 0x1000_0000 to 0x1f00_0000,
  357. * second 256 Meg
  358. */
  359. ddr->cs1_bnds = (((start_addr + rank_density) >> 8)
  360. | (( start_addr + 2*rank_density - 1)
  361. >> 24));
  362. ddr->cs1_config = ( 1<<31
  363. | (odt_rd_cfg << 20)
  364. | (odt_wr_cfg << 16)
  365. | (ba_bits << 14)
  366. | (spd.nrow_addr - 12) << 8
  367. | (spd.ncol_addr - 8) );
  368. debug("DDR: cs1_bnds = 0x%08x\n", ddr->cs1_bnds);
  369. debug("DDR: cs1_config = 0x%08x\n", ddr->cs1_config);
  370. }
  371. } else {
  372. /*
  373. * This is the 2nd DIMM slot for this controller
  374. */
  375. /*
  376. * Eg: Bounds: 0x0000_0000 to 0x0f000_0000 first 256 Meg
  377. */
  378. ddr->cs2_bnds = (start_addr >> 8)
  379. | (((start_addr + rank_density - 1) >> 24));
  380. ddr->cs2_config = ( 1 << 31
  381. | (odt_rd_cfg << 20)
  382. | (odt_wr_cfg << 16)
  383. | (ba_bits << 14)
  384. | (spd.nrow_addr - 12) << 8
  385. | (spd.ncol_addr - 8) );
  386. debug("DDR: cs2_bnds = 0x%08x\n", ddr->cs2_bnds);
  387. debug("DDR: cs2_config = 0x%08x\n", ddr->cs2_config);
  388. if (n_ranks == 2) {
  389. /*
  390. * Eg: Bounds: 0x1000_0000 to 0x1f00_0000,
  391. * second 256 Meg
  392. */
  393. ddr->cs3_bnds = (((start_addr + rank_density) >> 8)
  394. | (( start_addr + 2*rank_density - 1)
  395. >> 24));
  396. ddr->cs3_config = ( 1<<31
  397. | (odt_rd_cfg << 20)
  398. | (odt_wr_cfg << 16)
  399. | (ba_bits << 14)
  400. | (spd.nrow_addr - 12) << 8
  401. | (spd.ncol_addr - 8) );
  402. debug("DDR: cs3_bnds = 0x%08x\n", ddr->cs3_bnds);
  403. debug("DDR: cs3_config = 0x%08x\n", ddr->cs3_config);
  404. }
  405. }
  406. #endif /* CONFIG_DDR_INTERLEAVE */
  407. /*
  408. * Find the largest CAS by locating the highest 1 bit
  409. * in the spd.cas_lat field. Translate it to a DDR
  410. * controller field value:
  411. *
  412. * CAS Lat DDR I DDR II Ctrl
  413. * Clocks SPD Bit SPD Bit Value
  414. * ------- ------- ------- -----
  415. * 1.0 0 0001
  416. * 1.5 1 0010
  417. * 2.0 2 2 0011
  418. * 2.5 3 0100
  419. * 3.0 4 3 0101
  420. * 3.5 5 0110
  421. * 4.0 4 0111
  422. * 4.5 1000
  423. * 5.0 5 1001
  424. */
  425. caslat = __ilog2(spd.cas_lat);
  426. if ((spd.mem_type == SPD_MEMTYPE_DDR)
  427. && (caslat > 5)) {
  428. printf("DDR I: Invalid SPD CAS Latency: 0x%x.\n", spd.cas_lat);
  429. return 0;
  430. } else if (spd.mem_type == SPD_MEMTYPE_DDR2
  431. && (caslat < 2 || caslat > 5)) {
  432. printf("DDR II: Invalid SPD CAS Latency: 0x%x.\n",
  433. spd.cas_lat);
  434. return 0;
  435. }
  436. debug("DDR: caslat SPD bit is %d\n", caslat);
  437. /*
  438. * Calculate the Maximum Data Rate based on the Minimum Cycle time.
  439. * The SPD clk_cycle field (tCKmin) is measured in tenths of
  440. * nanoseconds and represented as BCD.
  441. */
  442. tCKmin_ps = convert_bcd_tenths_to_cycle_time_ps(spd.clk_cycle);
  443. debug("DDR: tCKmin = %d ps\n", tCKmin_ps);
  444. /*
  445. * Double-data rate, scaled 1000 to picoseconds, and back down to MHz.
  446. */
  447. max_data_rate = 2 * 1000 * 1000 / tCKmin_ps;
  448. debug("DDR: Module max data rate = %d Mhz\n", max_data_rate);
  449. /*
  450. * Adjust the CAS Latency to allow for bus speeds that
  451. * are slower than the DDR module.
  452. */
  453. busfreq = get_bus_freq(0) / 1000000; /* MHz */
  454. tCycle_ps = convert_bcd_tenths_to_cycle_time_ps(spd.clk_cycle3);
  455. modfreq = 2 * 1000 * 1000 / tCycle_ps;
  456. if ((spd.mem_type == SPD_MEMTYPE_DDR2) && (busfreq < 266)) {
  457. printf("DDR: platform frequency too low for correct DDR2 controller operation\n");
  458. return 0;
  459. } else if (busfreq < 90) {
  460. printf("DDR: platform frequency too low for correct DDR1 operation\n");
  461. return 0;
  462. }
  463. if ((busfreq <= modfreq) && (spd.cas_lat & (1 << (caslat - 2)))) {
  464. caslat -= 2;
  465. } else {
  466. tCycle_ps = convert_bcd_tenths_to_cycle_time_ps(spd.clk_cycle2);
  467. modfreq = 2 * 1000 * 1000 / tCycle_ps;
  468. if ((busfreq <= modfreq) && (spd.cas_lat & (1 << (caslat - 1))))
  469. caslat -= 1;
  470. else if (busfreq > max_data_rate) {
  471. printf("DDR: Bus freq %d MHz is not fit for DDR rate %d MHz\n",
  472. busfreq, max_data_rate);
  473. return 0;
  474. }
  475. }
  476. /*
  477. * Empirically set ~MCAS-to-preamble override for DDR 2.
  478. * Your milage will vary.
  479. */
  480. cpo = 0;
  481. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  482. if (busfreq <= 333) {
  483. cpo = 0x7;
  484. } else if (busfreq <= 400) {
  485. cpo = 0x9;
  486. } else {
  487. cpo = 0xa;
  488. }
  489. }
  490. /*
  491. * Convert caslat clocks to DDR controller value.
  492. * Force caslat_ctrl to be DDR Controller field-sized.
  493. */
  494. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  495. caslat_ctrl = (caslat + 1) & 0x07;
  496. } else {
  497. caslat_ctrl = (2 * caslat - 1) & 0x0f;
  498. }
  499. debug("DDR: caslat SPD bit is %d, controller field is 0x%x\n",
  500. caslat, caslat_ctrl);
  501. /*
  502. * Timing Config 0.
  503. * Avoid writing for DDR I. The new PQ38 DDR controller
  504. * dreams up non-zero default values to be backwards compatible.
  505. */
  506. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  507. unsigned char taxpd_clk = 8; /* By the book. */
  508. unsigned char tmrd_clk = 2; /* By the book. */
  509. unsigned char act_pd_exit = 2; /* Empirical? */
  510. unsigned char pre_pd_exit = 6; /* Empirical? */
  511. ddr->timing_cfg_0 = (0
  512. | ((act_pd_exit & 0x7) << 20) /* ACT_PD_EXIT */
  513. | ((pre_pd_exit & 0x7) << 16) /* PRE_PD_EXIT */
  514. | ((taxpd_clk & 0xf) << 8) /* ODT_PD_EXIT */
  515. | ((tmrd_clk & 0xf) << 0) /* MRS_CYC */
  516. );
  517. debug("DDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
  518. }
  519. /*
  520. * Some Timing Config 1 values now.
  521. * Sneak Extended Refresh Recovery in here too.
  522. */
  523. /*
  524. * For DDR I, WRREC(Twr) and WRTORD(Twtr) are not in SPD,
  525. * use conservative value.
  526. * For DDR II, they are bytes 36 and 37, in quarter nanos.
  527. */
  528. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  529. twr_clk = 3; /* Clocks */
  530. twtr_clk = 1; /* Clocks */
  531. } else {
  532. twr_clk = picos_to_clk(spd.twr * 250);
  533. twtr_clk = picos_to_clk(spd.twtr * 250);
  534. }
  535. /*
  536. * Calculate Trfc, in picos.
  537. * DDR I: Byte 42 straight up in ns.
  538. * DDR II: Byte 40 and 42 swizzled some, in ns.
  539. */
  540. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  541. trfc = spd.trfc * 1000; /* up to ps */
  542. } else {
  543. unsigned int byte40_table_ps[8] = {
  544. 0,
  545. 250,
  546. 330,
  547. 500,
  548. 660,
  549. 750,
  550. 0,
  551. 0
  552. };
  553. trfc = (((spd.trctrfc_ext & 0x1) * 256) + spd.trfc) * 1000
  554. + byte40_table_ps[(spd.trctrfc_ext >> 1) & 0x7];
  555. }
  556. trfc_clk = picos_to_clk(trfc);
  557. /*
  558. * Trcd, Byte 29, from quarter nanos to ps and clocks.
  559. */
  560. trcd_clk = picos_to_clk(spd.trcd * 250) & 0x7;
  561. /*
  562. * Convert trfc_clk to DDR controller fields. DDR I should
  563. * fit in the REFREC field (16-19) of TIMING_CFG_1, but the
  564. * 8548 controller has an extended REFREC field of three bits.
  565. * The controller automatically adds 8 clocks to this value,
  566. * so preadjust it down 8 first before splitting it up.
  567. */
  568. trfc_low = (trfc_clk - 8) & 0xf;
  569. trfc_high = ((trfc_clk - 8) >> 4) & 0x3;
  570. /*
  571. * Sneak in some Extended Refresh Recovery.
  572. */
  573. ddr->ext_refrec = (trfc_high << 16);
  574. debug("DDR: ext_refrec = 0x%08x\n", ddr->ext_refrec);
  575. ddr->timing_cfg_1 =
  576. (0
  577. | ((picos_to_clk(spd.trp * 250) & 0x07) << 28) /* PRETOACT */
  578. | ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24) /* ACTTOPRE */
  579. | (trcd_clk << 20) /* ACTTORW */
  580. | (caslat_ctrl << 16) /* CASLAT */
  581. | (trfc_low << 12) /* REFEC */
  582. | ((twr_clk & 0x07) << 8) /* WRRREC */
  583. | ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) /* ACTTOACT */
  584. | ((twtr_clk & 0x07) << 0) /* WRTORD */
  585. );
  586. debug("DDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
  587. /*
  588. * Timing_Config_2
  589. * Was: 0x00000800;
  590. */
  591. /*
  592. * Additive Latency
  593. * For DDR I, 0.
  594. * For DDR II, with ODT enabled, use "a value" less than ACTTORW,
  595. * which comes from Trcd, and also note that:
  596. * add_lat + caslat must be >= 4
  597. */
  598. add_lat = 0;
  599. if (spd.mem_type == SPD_MEMTYPE_DDR2
  600. && (odt_wr_cfg || odt_rd_cfg)
  601. && (caslat < 4)) {
  602. add_lat = 4 - caslat;
  603. if (add_lat >= trcd_clk) {
  604. add_lat = trcd_clk - 1;
  605. }
  606. }
  607. /*
  608. * Write Data Delay
  609. * Historically 0x2 == 4/8 clock delay.
  610. * Empirically, 0x3 == 6/8 clock delay is suggested for DDR I 266.
  611. */
  612. wr_data_delay = 3;
  613. /*
  614. * Write Latency
  615. * Read to Precharge
  616. * Minimum CKE Pulse Width.
  617. * Four Activate Window
  618. */
  619. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  620. /*
  621. * This is a lie. It should really be 1, but if it is
  622. * set to 1, bits overlap into the old controller's
  623. * otherwise unused ACSM field. If we leave it 0, then
  624. * the HW will magically treat it as 1 for DDR 1. Oh Yea.
  625. */
  626. wr_lat = 0;
  627. trtp_clk = 2; /* By the book. */
  628. cke_min_clk = 1; /* By the book. */
  629. four_act = 1; /* By the book. */
  630. } else {
  631. wr_lat = caslat - 1;
  632. /* Convert SPD value from quarter nanos to picos. */
  633. trtp_clk = picos_to_clk(spd.trtp * 250);
  634. cke_min_clk = 3; /* By the book. */
  635. four_act = picos_to_clk(37500); /* By the book. 1k pages? */
  636. }
  637. ddr->timing_cfg_2 = (0
  638. | ((add_lat & 0x7) << 28) /* ADD_LAT */
  639. | ((cpo & 0x1f) << 23) /* CPO */
  640. | ((wr_lat & 0x7) << 19) /* WR_LAT */
  641. | ((trtp_clk & 0x7) << 13) /* RD_TO_PRE */
  642. | ((wr_data_delay & 0x7) << 10) /* WR_DATA_DELAY */
  643. | ((cke_min_clk & 0x7) << 6) /* CKE_PLS */
  644. | ((four_act & 0x1f) << 0) /* FOUR_ACT */
  645. );
  646. debug("DDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
  647. /*
  648. * Determine the Mode Register Set.
  649. *
  650. * This is nominally part specific, but it appears to be
  651. * consistent for all DDR I devices, and for all DDR II devices.
  652. *
  653. * caslat must be programmed
  654. * burst length is always 4
  655. * burst type is sequential
  656. *
  657. * For DDR I:
  658. * operating mode is "normal"
  659. *
  660. * For DDR II:
  661. * other stuff
  662. */
  663. mode_caslat = 0;
  664. /*
  665. * Table lookup from DDR I or II Device Operation Specs.
  666. */
  667. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  668. if (1 <= caslat && caslat <= 4) {
  669. unsigned char mode_caslat_table[4] = {
  670. 0x5, /* 1.5 clocks */
  671. 0x2, /* 2.0 clocks */
  672. 0x6, /* 2.5 clocks */
  673. 0x3 /* 3.0 clocks */
  674. };
  675. mode_caslat = mode_caslat_table[caslat - 1];
  676. } else {
  677. puts("DDR I: Only CAS Latencies of 1.5, 2.0, "
  678. "2.5 and 3.0 clocks are supported.\n");
  679. return 0;
  680. }
  681. } else {
  682. if (2 <= caslat && caslat <= 5) {
  683. mode_caslat = caslat;
  684. } else {
  685. puts("DDR II: Only CAS Latencies of 2.0, 3.0, "
  686. "4.0 and 5.0 clocks are supported.\n");
  687. return 0;
  688. }
  689. }
  690. /*
  691. * Encoded Burst Length of 4.
  692. */
  693. burst_len = 2; /* Fiat. */
  694. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  695. twr_auto_clk = 0; /* Historical */
  696. } else {
  697. /*
  698. * Determine tCK max in picos. Grab tWR and convert to picos.
  699. * Auto-precharge write recovery is:
  700. * WR = roundup(tWR_ns/tCKmax_ns).
  701. *
  702. * Ponder: Is twr_auto_clk different than twr_clk?
  703. */
  704. tCKmax_ps = convert_bcd_tenths_to_cycle_time_ps(spd.tckmax);
  705. twr_auto_clk = (spd.twr * 250 + tCKmax_ps - 1) / tCKmax_ps;
  706. }
  707. /*
  708. * Mode Reg in bits 16 ~ 31,
  709. * Extended Mode Reg 1 in bits 0 ~ 15.
  710. */
  711. mode_odt_enable = 0x0; /* Default disabled */
  712. if (odt_wr_cfg || odt_rd_cfg) {
  713. /*
  714. * Bits 6 and 2 in Extended MRS(1)
  715. * Bit 2 == 0x04 == 75 Ohm, with 2 DIMM modules.
  716. * Bit 6 == 0x40 == 150 Ohm, with 1 DIMM module.
  717. */
  718. mode_odt_enable = 0x40; /* 150 Ohm */
  719. }
  720. ddr->sdram_mode_1 =
  721. (0
  722. | (add_lat << (16 + 3)) /* Additive Latency in EMRS1 */
  723. | (mode_odt_enable << 16) /* ODT Enable in EMRS1 */
  724. | (twr_auto_clk << 9) /* Write Recovery Autopre */
  725. | (mode_caslat << 4) /* caslat */
  726. | (burst_len << 0) /* Burst length */
  727. );
  728. debug("DDR: sdram_mode = 0x%08x\n", ddr->sdram_mode_1);
  729. /*
  730. * Clear EMRS2 and EMRS3.
  731. */
  732. ddr->sdram_mode_2 = 0;
  733. debug("DDR: sdram_mode_2 = 0x%08x\n", ddr->sdram_mode_2);
  734. /*
  735. * Determine Refresh Rate.
  736. */
  737. refresh_clk = determine_refresh_rate(spd.refresh & 0x7);
  738. /*
  739. * Set BSTOPRE to 0x100 for page mode
  740. * If auto-charge is used, set BSTOPRE = 0
  741. */
  742. ddr->sdram_interval =
  743. (0
  744. | (refresh_clk & 0x3fff) << 16
  745. | 0x100
  746. );
  747. debug("DDR: sdram_interval = 0x%08x\n", ddr->sdram_interval);
  748. /*
  749. * Is this an ECC DDR chip?
  750. * But don't mess with it if the DDR controller will init mem.
  751. */
  752. #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  753. if (spd.config == 0x02) {
  754. ddr->err_disable = 0x0000000d;
  755. ddr->err_sbe = 0x00ff0000;
  756. }
  757. debug("DDR: err_disable = 0x%08x\n", ddr->err_disable);
  758. debug("DDR: err_sbe = 0x%08x\n", ddr->err_sbe);
  759. #endif
  760. asm volatile("sync;isync");
  761. udelay(500);
  762. /*
  763. * SDRAM Cfg 2
  764. */
  765. /*
  766. * When ODT is enabled, Chap 9 suggests asserting ODT to
  767. * internal IOs only during reads.
  768. */
  769. odt_cfg = 0;
  770. if (odt_rd_cfg | odt_wr_cfg) {
  771. odt_cfg = 0x2; /* ODT to IOs during reads */
  772. }
  773. /*
  774. * Try to use differential DQS with DDR II.
  775. */
  776. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  777. dqs_cfg = 0; /* No Differential DQS for DDR I */
  778. } else {
  779. dqs_cfg = 0x1; /* Differential DQS for DDR II */
  780. }
  781. #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  782. /*
  783. * Use the DDR controller to auto initialize memory.
  784. */
  785. d_init = 1;
  786. ddr->sdram_data_init = CONFIG_MEM_INIT_VALUE;
  787. debug("DDR: ddr_data_init = 0x%08x\n", ddr->sdram_data_init);
  788. #else
  789. /*
  790. * Memory will be initialized via DMA, or not at all.
  791. */
  792. d_init = 0;
  793. #endif
  794. ddr->sdram_cfg_2 = (0
  795. | (dqs_cfg << 26) /* Differential DQS */
  796. | (odt_cfg << 21) /* ODT */
  797. | (d_init << 4) /* D_INIT auto init DDR */
  798. );
  799. debug("DDR: sdram_cfg_2 = 0x%08x\n", ddr->sdram_cfg_2);
  800. #ifdef MPC86xx_DDR_SDRAM_CLK_CNTL
  801. /*
  802. * Setup the clock control.
  803. * SDRAM_CLK_CNTL[0] = Source synchronous enable == 1
  804. * SDRAM_CLK_CNTL[5-7] = Clock Adjust
  805. * 0110 3/4 cycle late
  806. * 0111 7/8 cycle late
  807. */
  808. if (spd.mem_type == SPD_MEMTYPE_DDR)
  809. clk_adjust = 0x6;
  810. else
  811. clk_adjust = 0x7;
  812. ddr->sdram_clk_cntl = (0
  813. | 0x80000000
  814. | (clk_adjust << 23)
  815. );
  816. debug("DDR: sdram_clk_cntl = 0x%08x\n", ddr->sdram_clk_cntl);
  817. #endif
  818. /*
  819. * Figure out memory size in Megabytes.
  820. */
  821. debug("# ranks = %d, rank_density = 0x%08lx\n", n_ranks, rank_density);
  822. memsize = n_ranks * rank_density / 0x100000;
  823. return memsize;
  824. }
  825. unsigned int enable_ddr(unsigned int ddr_num)
  826. {
  827. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  828. spd_eeprom_t spd1,spd2;
  829. volatile ccsr_ddr_t *ddr;
  830. unsigned sdram_cfg_1;
  831. unsigned char sdram_type, mem_type, mod_attr;
  832. unsigned char d_init;
  833. unsigned int no_dimm1=0, no_dimm2=0;
  834. /* Set up pointer to enable the current ddr controller */
  835. if (ddr_num == 1)
  836. ddr = &immap->im_ddr1;
  837. else
  838. ddr = &immap->im_ddr2;
  839. /*
  840. * Read both dimm slots and decide whether
  841. * or not to enable this controller.
  842. */
  843. memset((void *)&spd1, 0, sizeof(spd1));
  844. memset((void *)&spd2, 0, sizeof(spd2));
  845. if (ddr_num == 1) {
  846. CFG_READ_SPD(SPD_EEPROM_ADDRESS1,
  847. 0, 1, (uchar *) &spd1, sizeof(spd1));
  848. #if defined(SPD_EEPROM_ADDRESS2)
  849. CFG_READ_SPD(SPD_EEPROM_ADDRESS2,
  850. 0, 1, (uchar *) &spd2, sizeof(spd2));
  851. #endif
  852. } else {
  853. #if defined(SPD_EEPROM_ADDRESS3)
  854. CFG_READ_SPD(SPD_EEPROM_ADDRESS3,
  855. 0, 1, (uchar *) &spd1, sizeof(spd1));
  856. #endif
  857. #if defined(SPD_EEPROM_ADDRESS4)
  858. CFG_READ_SPD(SPD_EEPROM_ADDRESS4,
  859. 0, 1, (uchar *) &spd2, sizeof(spd2));
  860. #endif
  861. }
  862. /*
  863. * Check for supported memory module types.
  864. */
  865. if (spd1.mem_type != SPD_MEMTYPE_DDR
  866. && spd1.mem_type != SPD_MEMTYPE_DDR2) {
  867. no_dimm1 = 1;
  868. } else {
  869. debug("\nFound memory of type 0x%02lx ",spd1.mem_type );
  870. if (spd1.mem_type == SPD_MEMTYPE_DDR)
  871. debug("DDR I\n");
  872. else
  873. debug("DDR II\n");
  874. }
  875. if (spd2.mem_type != SPD_MEMTYPE_DDR &&
  876. spd2.mem_type != SPD_MEMTYPE_DDR2) {
  877. no_dimm2 = 1;
  878. } else {
  879. debug("\nFound memory of type 0x%02lx ",spd2.mem_type );
  880. if (spd2.mem_type == SPD_MEMTYPE_DDR)
  881. debug("DDR I\n");
  882. else
  883. debug("DDR II\n");
  884. }
  885. #ifdef CONFIG_DDR_INTERLEAVE
  886. if (no_dimm1) {
  887. printf("For interleaved operation memory modules need to be present in CS0 DIMM slots of both DDR controllers!\n");
  888. return 0;
  889. }
  890. #endif
  891. /*
  892. * Memory is not present in DIMM1 and DIMM2 - so do not enable DDRn
  893. */
  894. if (no_dimm1 && no_dimm2) {
  895. printf("No memory modules found for DDR controller %d!!\n", ddr_num);
  896. return 0;
  897. } else {
  898. #if defined(CONFIG_DDR_ECC)
  899. unsigned char config;
  900. #endif
  901. mem_type = no_dimm2 ? spd1.mem_type : spd2.mem_type;
  902. /*
  903. * Figure out the settings for the sdram_cfg register.
  904. * Build up the entire register in 'sdram_cfg' before
  905. * writing since the write into the register will
  906. * actually enable the memory controller; all settings
  907. * must be done before enabling.
  908. *
  909. * sdram_cfg[0] = 1 (ddr sdram logic enable)
  910. * sdram_cfg[1] = 1 (self-refresh-enable)
  911. * sdram_cfg[5:7] = (SDRAM type = DDR SDRAM)
  912. * 010 DDR 1 SDRAM
  913. * 011 DDR 2 SDRAM
  914. */
  915. sdram_type = (mem_type == SPD_MEMTYPE_DDR) ? 2 : 3;
  916. sdram_cfg_1 = (0
  917. | (1 << 31) /* Enable */
  918. | (1 << 30) /* Self refresh */
  919. | (sdram_type << 24) /* SDRAM type */
  920. );
  921. /*
  922. * sdram_cfg[3] = RD_EN - registered DIMM enable
  923. * A value of 0x26 indicates micron registered
  924. * DIMMS (micron.com)
  925. */
  926. mod_attr = no_dimm2 ? spd1.mod_attr : spd2.mod_attr;
  927. if (mem_type == SPD_MEMTYPE_DDR && mod_attr == 0x26) {
  928. sdram_cfg_1 |= 0x10000000; /* RD_EN */
  929. }
  930. #if defined(CONFIG_DDR_ECC)
  931. config = no_dimm2 ? spd1.config : spd2.config;
  932. /*
  933. * If the user wanted ECC (enabled via sdram_cfg[2])
  934. */
  935. if (config == 0x02) {
  936. ddr->err_disable = 0x00000000;
  937. asm volatile("sync;isync;");
  938. ddr->err_sbe = 0x00ff0000;
  939. ddr->err_int_en = 0x0000000d;
  940. sdram_cfg_1 |= 0x20000000; /* ECC_EN */
  941. }
  942. #endif
  943. /*
  944. * Set 1T or 2T timing based on 1 or 2 modules
  945. */
  946. {
  947. if (!(no_dimm1 || no_dimm2)) {
  948. /*
  949. * 2T timing,because both DIMMS are present.
  950. * Enable 2T timing by setting sdram_cfg[16].
  951. */
  952. sdram_cfg_1 |= 0x8000; /* 2T_EN */
  953. }
  954. }
  955. /*
  956. * 200 painful micro-seconds must elapse between
  957. * the DDR clock setup and the DDR config enable.
  958. */
  959. udelay(200);
  960. /*
  961. * Go!
  962. */
  963. ddr->sdram_cfg_1 = sdram_cfg_1;
  964. asm volatile("sync;isync");
  965. udelay(500);
  966. debug("DDR: sdram_cfg = 0x%08x\n", ddr->sdram_cfg_1);
  967. #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  968. d_init = 1;
  969. debug("DDR: memory initializing\n");
  970. /*
  971. * Poll until memory is initialized.
  972. * 512 Meg at 400 might hit this 200 times or so.
  973. */
  974. while ((ddr->sdram_cfg_2 & (d_init << 4)) != 0) {
  975. udelay(1000);
  976. }
  977. debug("DDR: memory initialized\n\n");
  978. #endif
  979. debug("Enabled DDR Controller %d\n", ddr_num);
  980. return 1;
  981. }
  982. }
  983. long int
  984. spd_sdram(void)
  985. {
  986. int memsize_ddr1_dimm1 = 0;
  987. int memsize_ddr1_dimm2 = 0;
  988. int memsize_ddr1 = 0;
  989. unsigned int law_size_ddr1;
  990. #ifdef CONFIG_DDR_INTERLEAVE
  991. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  992. volatile ccsr_ddr_t *ddr1 = &immap->im_ddr1;
  993. #endif
  994. #if (CONFIG_NUM_DDR_CONTROLLERS > 1)
  995. int memsize_ddr2_dimm1 = 0;
  996. int memsize_ddr2_dimm2 = 0;
  997. int memsize_ddr2 = 0;
  998. unsigned int law_size_ddr2;
  999. #endif
  1000. unsigned int ddr1_enabled = 0;
  1001. unsigned int ddr2_enabled = 0;
  1002. int memsize_total = 0;
  1003. #ifdef CONFIG_DDR_INTERLEAVE
  1004. unsigned int law_size_interleaved;
  1005. volatile ccsr_ddr_t *ddr2 = &immap->im_ddr2;
  1006. memsize_ddr1_dimm1 = spd_init(SPD_EEPROM_ADDRESS1,
  1007. 1, 1,
  1008. (unsigned int)memsize_total * 1024*1024);
  1009. memsize_total += memsize_ddr1_dimm1;
  1010. memsize_ddr2_dimm1 = spd_init(SPD_EEPROM_ADDRESS3,
  1011. 2, 1,
  1012. (unsigned int)memsize_total * 1024*1024);
  1013. memsize_total += memsize_ddr2_dimm1;
  1014. if (memsize_ddr1_dimm1 != memsize_ddr2_dimm1) {
  1015. if (memsize_ddr1_dimm1 < memsize_ddr2_dimm1)
  1016. memsize_total -= memsize_ddr1_dimm1;
  1017. else
  1018. memsize_total -= memsize_ddr2_dimm1;
  1019. debug("Total memory available for interleaving 0x%08lx\n",
  1020. memsize_total * 1024 * 1024);
  1021. debug("Adjusting CS0_BNDS to account for unequal DIMM sizes in interleaved memory\n");
  1022. ddr1->cs0_bnds = ((memsize_total * 1024 * 1024) - 1) >> 24;
  1023. ddr2->cs0_bnds = ((memsize_total * 1024 * 1024) - 1) >> 24;
  1024. debug("DDR1: cs0_bnds = 0x%08x\n", ddr1->cs0_bnds);
  1025. debug("DDR2: cs0_bnds = 0x%08x\n", ddr2->cs0_bnds);
  1026. }
  1027. ddr1_enabled = enable_ddr(1);
  1028. ddr2_enabled = enable_ddr(2);
  1029. /*
  1030. * Both controllers need to be enabled for interleaving.
  1031. */
  1032. if (ddr1_enabled && ddr2_enabled) {
  1033. law_size_interleaved = 19 + __ilog2(memsize_total);
  1034. /*
  1035. * Set up LAWBAR for DDR 1 space.
  1036. */
  1037. #ifdef CONFIG_FSL_LAW
  1038. set_law(1, CFG_DDR_SDRAM_BASE, law_size_interleaved, LAW_TRGT_IF_DDR_INTRLV);
  1039. #endif
  1040. debug("Interleaved memory size is 0x%08lx\n", memsize_total);
  1041. #if (CFG_PAGE_INTERLEAVING == 1)
  1042. printf("Page ");
  1043. #elif (CFG_BANK_INTERLEAVING == 1)
  1044. printf("Bank ");
  1045. #elif (CFG_SUPER_BANK_INTERLEAVING == 1)
  1046. printf("Super-bank ");
  1047. #else
  1048. printf("Cache-line ");
  1049. #endif
  1050. printf("Interleaved");
  1051. return memsize_total * 1024 * 1024;
  1052. } else {
  1053. printf("Interleaved memory not enabled - check CS0 DIMM slots for both controllers.\n");
  1054. return 0;
  1055. }
  1056. #else
  1057. /*
  1058. * Call spd_sdram() routine to init ddr1 - pass I2c address,
  1059. * controller number, dimm number, and starting address.
  1060. */
  1061. memsize_ddr1_dimm1 = spd_init(SPD_EEPROM_ADDRESS1,
  1062. 1, 1,
  1063. (unsigned int)memsize_total * 1024*1024);
  1064. memsize_total += memsize_ddr1_dimm1;
  1065. #if defined(SPD_EEPROM_ADDRESS2)
  1066. memsize_ddr1_dimm2 = spd_init(SPD_EEPROM_ADDRESS2,
  1067. 1, 2,
  1068. (unsigned int)memsize_total * 1024*1024);
  1069. #endif
  1070. memsize_total += memsize_ddr1_dimm2;
  1071. /*
  1072. * Enable the DDR controller - pass ddr controller number.
  1073. */
  1074. ddr1_enabled = enable_ddr(1);
  1075. /* Keep track of memory to be addressed by DDR1 */
  1076. memsize_ddr1 = memsize_ddr1_dimm1 + memsize_ddr1_dimm2;
  1077. /*
  1078. * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23. Fnord.
  1079. */
  1080. if (ddr1_enabled) {
  1081. law_size_ddr1 = 19 + __ilog2(memsize_ddr1);
  1082. /*
  1083. * Set up LAWBAR for DDR 1 space.
  1084. */
  1085. #ifdef CONFIG_FSL_LAW
  1086. set_law(1, CFG_DDR_SDRAM_BASE, law_size_ddr1, LAW_TRGT_IF_DDR_1);
  1087. #endif
  1088. }
  1089. #if (CONFIG_NUM_DDR_CONTROLLERS > 1)
  1090. memsize_ddr2_dimm1 = spd_init(SPD_EEPROM_ADDRESS3,
  1091. 2, 1,
  1092. (unsigned int)memsize_total * 1024*1024);
  1093. memsize_total += memsize_ddr2_dimm1;
  1094. memsize_ddr2_dimm2 = spd_init(SPD_EEPROM_ADDRESS4,
  1095. 2, 2,
  1096. (unsigned int)memsize_total * 1024*1024);
  1097. memsize_total += memsize_ddr2_dimm2;
  1098. ddr2_enabled = enable_ddr(2);
  1099. /* Keep track of memory to be addressed by DDR2 */
  1100. memsize_ddr2 = memsize_ddr2_dimm1 + memsize_ddr2_dimm2;
  1101. if (ddr2_enabled) {
  1102. law_size_ddr2 = 19 + __ilog2(memsize_ddr2);
  1103. /*
  1104. * Set up LAWBAR for DDR 2 space.
  1105. */
  1106. #ifdef CONFIG_FSL_LAW
  1107. set_law(8,
  1108. (ddr1_enabled ? (memsize_ddr1 * 1024 * 1024) : CFG_DDR_SDRAM_BASE),
  1109. law_size_ddr2, LAW_TRGT_IF_DDR_2);
  1110. #endif
  1111. }
  1112. debug("\nMemory size of DDR2 = 0x%08lx\n", memsize_ddr2);
  1113. #endif /* CONFIG_NUM_DDR_CONTROLLERS > 1 */
  1114. debug("\nMemory size of DDR1 = 0x%08lx\n", memsize_ddr1);
  1115. /*
  1116. * If neither DDR controller is enabled return 0.
  1117. */
  1118. if (!ddr1_enabled && !ddr2_enabled)
  1119. return 0;
  1120. printf("Non-interleaved");
  1121. return memsize_total * 1024 * 1024;
  1122. #endif /* CONFIG_DDR_INTERLEAVE */
  1123. }
  1124. #endif /* CONFIG_SPD_EEPROM */
  1125. #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  1126. /*
  1127. * Initialize all of memory for ECC, then enable errors.
  1128. */
  1129. void
  1130. ddr_enable_ecc(unsigned int dram_size)
  1131. {
  1132. uint *p = 0;
  1133. uint i = 0;
  1134. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  1135. volatile ccsr_ddr_t *ddr1= &immap->im_ddr1;
  1136. dma_init();
  1137. for (*p = 0; p < (uint *)(8 * 1024); p++) {
  1138. if (((unsigned int)p & 0x1f) == 0) {
  1139. ppcDcbz((unsigned long) p);
  1140. }
  1141. *p = (unsigned int)CONFIG_MEM_INIT_VALUE;
  1142. if (((unsigned int)p & 0x1c) == 0x1c) {
  1143. ppcDcbf((unsigned long) p);
  1144. }
  1145. }
  1146. dma_xfer((uint *)0x002000, 0x002000, (uint *)0); /* 8K */
  1147. dma_xfer((uint *)0x004000, 0x004000, (uint *)0); /* 16K */
  1148. dma_xfer((uint *)0x008000, 0x008000, (uint *)0); /* 32K */
  1149. dma_xfer((uint *)0x010000, 0x010000, (uint *)0); /* 64K */
  1150. dma_xfer((uint *)0x020000, 0x020000, (uint *)0); /* 128k */
  1151. dma_xfer((uint *)0x040000, 0x040000, (uint *)0); /* 256k */
  1152. dma_xfer((uint *)0x080000, 0x080000, (uint *)0); /* 512k */
  1153. dma_xfer((uint *)0x100000, 0x100000, (uint *)0); /* 1M */
  1154. dma_xfer((uint *)0x200000, 0x200000, (uint *)0); /* 2M */
  1155. dma_xfer((uint *)0x400000, 0x400000, (uint *)0); /* 4M */
  1156. for (i = 1; i < dram_size / 0x800000; i++) {
  1157. dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
  1158. }
  1159. /*
  1160. * Enable errors for ECC.
  1161. */
  1162. debug("DMA DDR: err_disable = 0x%08x\n", ddr1->err_disable);
  1163. ddr1->err_disable = 0x00000000;
  1164. asm volatile("sync;isync");
  1165. debug("DMA DDR: err_disable = 0x%08x\n", ddr1->err_disable);
  1166. }
  1167. #endif /* CONFIG_DDR_ECC && ! CONFIG_ECC_INIT_VIA_DDRCONTROLLER */