spd_sdram.c 34 KB

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