spd_sdram.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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, ba_bits;
  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. ba_bits = 0;
  286. if (spd.nbanks == 0x8)
  287. ba_bits = 1;
  288. #ifdef CONFIG_DDR_INTERLEAVE
  289. if (dimm_num != 1) {
  290. printf("For interleaving memory on HPCN, need to use DIMM 1 for DDR Controller %d !\n", ddr_num);
  291. return 0;
  292. } else {
  293. /*
  294. * Since interleaved memory only uses CS0, the
  295. * memory sticks have to be identical in size and quantity
  296. * of ranks. That essentially gives double the size on
  297. * one rank, i.e on CS0 for both controllers put together.
  298. * Confirm this???
  299. */
  300. rank_density *= 2;
  301. /*
  302. * Eg: Bounds: 0x0000_0000 to 0x0f000_0000 first 256 Meg
  303. */
  304. start_addr = 0;
  305. ddr->cs0_bnds = (start_addr >> 8)
  306. | (((start_addr + rank_density - 1) >> 24));
  307. /*
  308. * Default interleaving mode to cache-line interleaving.
  309. */
  310. ddr->cs0_config = ( 1 << 31
  311. #if (CFG_PAGE_INTERLEAVING == 1)
  312. | (PAGE_INTERLEAVING)
  313. #elif (CFG_BANK_INTERLEAVING == 1)
  314. | (BANK_INTERLEAVING)
  315. #elif (CFG_SUPER_BANK_INTERLEAVING == 1)
  316. | (SUPER_BANK_INTERLEAVING)
  317. #else
  318. | (CACHE_LINE_INTERLEAVING)
  319. #endif
  320. | (odt_rd_cfg << 20)
  321. | (odt_wr_cfg << 16)
  322. | (ba_bits << 14)
  323. | (spd.nrow_addr - 12) << 8
  324. | (spd.ncol_addr - 8) );
  325. debug("DDR: cs0_bnds = 0x%08x\n", ddr->cs0_bnds);
  326. debug("DDR: cs0_config = 0x%08x\n", ddr->cs0_config);
  327. /*
  328. * Adjustment for dual rank memory to get correct memory
  329. * size (return value of this function).
  330. */
  331. if (n_ranks == 2) {
  332. n_ranks = 1;
  333. rank_density /= 2;
  334. } else {
  335. rank_density /= 2;
  336. }
  337. }
  338. #else /* CONFIG_DDR_INTERLEAVE */
  339. if (dimm_num == 1) {
  340. /*
  341. * Eg: Bounds: 0x0000_0000 to 0x0f000_0000 first 256 Meg
  342. */
  343. ddr->cs0_bnds = (start_addr >> 8)
  344. | (((start_addr + rank_density - 1) >> 24));
  345. ddr->cs0_config = ( 1 << 31
  346. | (odt_rd_cfg << 20)
  347. | (odt_wr_cfg << 16)
  348. | (ba_bits << 14)
  349. | (spd.nrow_addr - 12) << 8
  350. | (spd.ncol_addr - 8) );
  351. debug("DDR: cs0_bnds = 0x%08x\n", ddr->cs0_bnds);
  352. debug("DDR: cs0_config = 0x%08x\n", ddr->cs0_config);
  353. if (n_ranks == 2) {
  354. /*
  355. * Eg: Bounds: 0x1000_0000 to 0x1f00_0000,
  356. * second 256 Meg
  357. */
  358. ddr->cs1_bnds = (((start_addr + rank_density) >> 8)
  359. | (( start_addr + 2*rank_density - 1)
  360. >> 24));
  361. ddr->cs1_config = ( 1<<31
  362. | (odt_rd_cfg << 20)
  363. | (odt_wr_cfg << 16)
  364. | (ba_bits << 14)
  365. | (spd.nrow_addr - 12) << 8
  366. | (spd.ncol_addr - 8) );
  367. debug("DDR: cs1_bnds = 0x%08x\n", ddr->cs1_bnds);
  368. debug("DDR: cs1_config = 0x%08x\n", ddr->cs1_config);
  369. }
  370. } else {
  371. /*
  372. * This is the 2nd DIMM slot for this controller
  373. */
  374. /*
  375. * Eg: Bounds: 0x0000_0000 to 0x0f000_0000 first 256 Meg
  376. */
  377. ddr->cs2_bnds = (start_addr >> 8)
  378. | (((start_addr + rank_density - 1) >> 24));
  379. ddr->cs2_config = ( 1 << 31
  380. | (odt_rd_cfg << 20)
  381. | (odt_wr_cfg << 16)
  382. | (ba_bits << 14)
  383. | (spd.nrow_addr - 12) << 8
  384. | (spd.ncol_addr - 8) );
  385. debug("DDR: cs2_bnds = 0x%08x\n", ddr->cs2_bnds);
  386. debug("DDR: cs2_config = 0x%08x\n", ddr->cs2_config);
  387. if (n_ranks == 2) {
  388. /*
  389. * Eg: Bounds: 0x1000_0000 to 0x1f00_0000,
  390. * second 256 Meg
  391. */
  392. ddr->cs3_bnds = (((start_addr + rank_density) >> 8)
  393. | (( start_addr + 2*rank_density - 1)
  394. >> 24));
  395. ddr->cs3_config = ( 1<<31
  396. | (odt_rd_cfg << 20)
  397. | (odt_wr_cfg << 16)
  398. | (ba_bits << 14)
  399. | (spd.nrow_addr - 12) << 8
  400. | (spd.ncol_addr - 8) );
  401. debug("DDR: cs3_bnds = 0x%08x\n", ddr->cs3_bnds);
  402. debug("DDR: cs3_config = 0x%08x\n", ddr->cs3_config);
  403. }
  404. }
  405. #endif /* CONFIG_DDR_INTERLEAVE */
  406. /*
  407. * Find the largest CAS by locating the highest 1 bit
  408. * in the spd.cas_lat field. Translate it to a DDR
  409. * controller field value:
  410. *
  411. * CAS Lat DDR I DDR II Ctrl
  412. * Clocks SPD Bit SPD Bit Value
  413. * ------- ------- ------- -----
  414. * 1.0 0 0001
  415. * 1.5 1 0010
  416. * 2.0 2 2 0011
  417. * 2.5 3 0100
  418. * 3.0 4 3 0101
  419. * 3.5 5 0110
  420. * 4.0 4 0111
  421. * 4.5 1000
  422. * 5.0 5 1001
  423. */
  424. caslat = __ilog2(spd.cas_lat);
  425. if ((spd.mem_type == SPD_MEMTYPE_DDR)
  426. && (caslat > 5)) {
  427. printf("DDR I: Invalid SPD CAS Latency: 0x%x.\n", spd.cas_lat);
  428. return 0;
  429. } else if (spd.mem_type == SPD_MEMTYPE_DDR2
  430. && (caslat < 2 || caslat > 5)) {
  431. printf("DDR II: Invalid SPD CAS Latency: 0x%x.\n",
  432. spd.cas_lat);
  433. return 0;
  434. }
  435. debug("DDR: caslat SPD bit is %d\n", caslat);
  436. /*
  437. * Calculate the Maximum Data Rate based on the Minimum Cycle time.
  438. * The SPD clk_cycle field (tCKmin) is measured in tenths of
  439. * nanoseconds and represented as BCD.
  440. */
  441. tCKmin_ps = convert_bcd_tenths_to_cycle_time_ps(spd.clk_cycle);
  442. debug("DDR: tCKmin = %d ps\n", tCKmin_ps);
  443. /*
  444. * Double-data rate, scaled 1000 to picoseconds, and back down to MHz.
  445. */
  446. max_data_rate = 2 * 1000 * 1000 / tCKmin_ps;
  447. debug("DDR: Module max data rate = %d Mhz\n", max_data_rate);
  448. /*
  449. * Adjust the CAS Latency to allow for bus speeds that
  450. * are slower than the DDR module.
  451. */
  452. busfreq = get_bus_freq(0) / 1000000; /* MHz */
  453. tCycle_ps = convert_bcd_tenths_to_cycle_time_ps(spd.clk_cycle3);
  454. modfreq = 2 * 1000 * 1000 / tCycle_ps;
  455. if ((spd.mem_type == SPD_MEMTYPE_DDR2) && (busfreq < 266)) {
  456. printf("DDR: platform frequency too low for correct DDR2 controller operation\n");
  457. return 0;
  458. } else if (busfreq < 90) {
  459. printf("DDR: platform frequency too low for correct DDR1 operation\n");
  460. return 0;
  461. }
  462. if ((busfreq <= modfreq) && (spd.cas_lat & (1 << (caslat - 2)))) {
  463. caslat -= 2;
  464. } else {
  465. tCycle_ps = convert_bcd_tenths_to_cycle_time_ps(spd.clk_cycle2);
  466. modfreq = 2 * 1000 * 1000 / tCycle_ps;
  467. if ((busfreq <= modfreq) && (spd.cas_lat & (1 << (caslat - 1))))
  468. caslat -= 1;
  469. else if (busfreq > max_data_rate) {
  470. printf("DDR: Bus freq %d MHz is not fit for DDR rate %d MHz\n",
  471. busfreq, max_data_rate);
  472. return 0;
  473. }
  474. }
  475. /*
  476. * Empirically set ~MCAS-to-preamble override for DDR 2.
  477. * Your milage will vary.
  478. */
  479. cpo = 0;
  480. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  481. if (busfreq <= 333) {
  482. cpo = 0x7;
  483. } else if (busfreq <= 400) {
  484. cpo = 0x9;
  485. } else {
  486. cpo = 0xa;
  487. }
  488. }
  489. /*
  490. * Convert caslat clocks to DDR controller value.
  491. * Force caslat_ctrl to be DDR Controller field-sized.
  492. */
  493. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  494. caslat_ctrl = (caslat + 1) & 0x07;
  495. } else {
  496. caslat_ctrl = (2 * caslat - 1) & 0x0f;
  497. }
  498. debug("DDR: caslat SPD bit is %d, controller field is 0x%x\n",
  499. caslat, caslat_ctrl);
  500. /*
  501. * Timing Config 0.
  502. * Avoid writing for DDR I. The new PQ38 DDR controller
  503. * dreams up non-zero default values to be backwards compatible.
  504. */
  505. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  506. unsigned char taxpd_clk = 8; /* By the book. */
  507. unsigned char tmrd_clk = 2; /* By the book. */
  508. unsigned char act_pd_exit = 2; /* Empirical? */
  509. unsigned char pre_pd_exit = 6; /* Empirical? */
  510. ddr->timing_cfg_0 = (0
  511. | ((act_pd_exit & 0x7) << 20) /* ACT_PD_EXIT */
  512. | ((pre_pd_exit & 0x7) << 16) /* PRE_PD_EXIT */
  513. | ((taxpd_clk & 0xf) << 8) /* ODT_PD_EXIT */
  514. | ((tmrd_clk & 0xf) << 0) /* MRS_CYC */
  515. );
  516. debug("DDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
  517. }
  518. /*
  519. * Some Timing Config 1 values now.
  520. * Sneak Extended Refresh Recovery in here too.
  521. */
  522. /*
  523. * For DDR I, WRREC(Twr) and WRTORD(Twtr) are not in SPD,
  524. * use conservative value.
  525. * For DDR II, they are bytes 36 and 37, in quarter nanos.
  526. */
  527. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  528. twr_clk = 3; /* Clocks */
  529. twtr_clk = 1; /* Clocks */
  530. } else {
  531. twr_clk = picos_to_clk(spd.twr * 250);
  532. twtr_clk = picos_to_clk(spd.twtr * 250);
  533. }
  534. /*
  535. * Calculate Trfc, in picos.
  536. * DDR I: Byte 42 straight up in ns.
  537. * DDR II: Byte 40 and 42 swizzled some, in ns.
  538. */
  539. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  540. trfc = spd.trfc * 1000; /* up to ps */
  541. } else {
  542. unsigned int byte40_table_ps[8] = {
  543. 0,
  544. 250,
  545. 330,
  546. 500,
  547. 660,
  548. 750,
  549. 0,
  550. 0
  551. };
  552. trfc = (((spd.trctrfc_ext & 0x1) * 256) + spd.trfc) * 1000
  553. + byte40_table_ps[(spd.trctrfc_ext >> 1) & 0x7];
  554. }
  555. trfc_clk = picos_to_clk(trfc);
  556. /*
  557. * Trcd, Byte 29, from quarter nanos to ps and clocks.
  558. */
  559. trcd_clk = picos_to_clk(spd.trcd * 250) & 0x7;
  560. /*
  561. * Convert trfc_clk to DDR controller fields. DDR I should
  562. * fit in the REFREC field (16-19) of TIMING_CFG_1, but the
  563. * 8548 controller has an extended REFREC field of three bits.
  564. * The controller automatically adds 8 clocks to this value,
  565. * so preadjust it down 8 first before splitting it up.
  566. */
  567. trfc_low = (trfc_clk - 8) & 0xf;
  568. trfc_high = ((trfc_clk - 8) >> 4) & 0x3;
  569. /*
  570. * Sneak in some Extended Refresh Recovery.
  571. */
  572. ddr->ext_refrec = (trfc_high << 16);
  573. debug("DDR: ext_refrec = 0x%08x\n", ddr->ext_refrec);
  574. ddr->timing_cfg_1 =
  575. (0
  576. | ((picos_to_clk(spd.trp * 250) & 0x07) << 28) /* PRETOACT */
  577. | ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24) /* ACTTOPRE */
  578. | (trcd_clk << 20) /* ACTTORW */
  579. | (caslat_ctrl << 16) /* CASLAT */
  580. | (trfc_low << 12) /* REFEC */
  581. | ((twr_clk & 0x07) << 8) /* WRRREC */
  582. | ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) /* ACTTOACT */
  583. | ((twtr_clk & 0x07) << 0) /* WRTORD */
  584. );
  585. debug("DDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
  586. /*
  587. * Timing_Config_2
  588. * Was: 0x00000800;
  589. */
  590. /*
  591. * Additive Latency
  592. * For DDR I, 0.
  593. * For DDR II, with ODT enabled, use "a value" less than ACTTORW,
  594. * which comes from Trcd, and also note that:
  595. * add_lat + caslat must be >= 4
  596. */
  597. add_lat = 0;
  598. if (spd.mem_type == SPD_MEMTYPE_DDR2
  599. && (odt_wr_cfg || odt_rd_cfg)
  600. && (caslat < 4)) {
  601. add_lat = 4 - caslat;
  602. if (add_lat >= trcd_clk) {
  603. add_lat = trcd_clk - 1;
  604. }
  605. }
  606. /*
  607. * Write Data Delay
  608. * Historically 0x2 == 4/8 clock delay.
  609. * Empirically, 0x3 == 6/8 clock delay is suggested for DDR I 266.
  610. */
  611. wr_data_delay = 3;
  612. /*
  613. * Write Latency
  614. * Read to Precharge
  615. * Minimum CKE Pulse Width.
  616. * Four Activate Window
  617. */
  618. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  619. /*
  620. * This is a lie. It should really be 1, but if it is
  621. * set to 1, bits overlap into the old controller's
  622. * otherwise unused ACSM field. If we leave it 0, then
  623. * the HW will magically treat it as 1 for DDR 1. Oh Yea.
  624. */
  625. wr_lat = 0;
  626. trtp_clk = 2; /* By the book. */
  627. cke_min_clk = 1; /* By the book. */
  628. four_act = 1; /* By the book. */
  629. } else {
  630. wr_lat = caslat - 1;
  631. /* Convert SPD value from quarter nanos to picos. */
  632. trtp_clk = picos_to_clk(spd.trtp * 250);
  633. cke_min_clk = 3; /* By the book. */
  634. four_act = picos_to_clk(37500); /* By the book. 1k pages? */
  635. }
  636. ddr->timing_cfg_2 = (0
  637. | ((add_lat & 0x7) << 28) /* ADD_LAT */
  638. | ((cpo & 0x1f) << 23) /* CPO */
  639. | ((wr_lat & 0x7) << 19) /* WR_LAT */
  640. | ((trtp_clk & 0x7) << 13) /* RD_TO_PRE */
  641. | ((wr_data_delay & 0x7) << 10) /* WR_DATA_DELAY */
  642. | ((cke_min_clk & 0x7) << 6) /* CKE_PLS */
  643. | ((four_act & 0x1f) << 0) /* FOUR_ACT */
  644. );
  645. debug("DDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
  646. /*
  647. * Determine the Mode Register Set.
  648. *
  649. * This is nominally part specific, but it appears to be
  650. * consistent for all DDR I devices, and for all DDR II devices.
  651. *
  652. * caslat must be programmed
  653. * burst length is always 4
  654. * burst type is sequential
  655. *
  656. * For DDR I:
  657. * operating mode is "normal"
  658. *
  659. * For DDR II:
  660. * other stuff
  661. */
  662. mode_caslat = 0;
  663. /*
  664. * Table lookup from DDR I or II Device Operation Specs.
  665. */
  666. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  667. if (1 <= caslat && caslat <= 4) {
  668. unsigned char mode_caslat_table[4] = {
  669. 0x5, /* 1.5 clocks */
  670. 0x2, /* 2.0 clocks */
  671. 0x6, /* 2.5 clocks */
  672. 0x3 /* 3.0 clocks */
  673. };
  674. mode_caslat = mode_caslat_table[caslat - 1];
  675. } else {
  676. puts("DDR I: Only CAS Latencies of 1.5, 2.0, "
  677. "2.5 and 3.0 clocks are supported.\n");
  678. return 0;
  679. }
  680. } else {
  681. if (2 <= caslat && caslat <= 5) {
  682. mode_caslat = caslat;
  683. } else {
  684. puts("DDR II: Only CAS Latencies of 2.0, 3.0, "
  685. "4.0 and 5.0 clocks are supported.\n");
  686. return 0;
  687. }
  688. }
  689. /*
  690. * Encoded Burst Length of 4.
  691. */
  692. burst_len = 2; /* Fiat. */
  693. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  694. twr_auto_clk = 0; /* Historical */
  695. } else {
  696. /*
  697. * Determine tCK max in picos. Grab tWR and convert to picos.
  698. * Auto-precharge write recovery is:
  699. * WR = roundup(tWR_ns/tCKmax_ns).
  700. *
  701. * Ponder: Is twr_auto_clk different than twr_clk?
  702. */
  703. tCKmax_ps = convert_bcd_tenths_to_cycle_time_ps(spd.tckmax);
  704. twr_auto_clk = (spd.twr * 250 + tCKmax_ps - 1) / tCKmax_ps;
  705. }
  706. /*
  707. * Mode Reg in bits 16 ~ 31,
  708. * Extended Mode Reg 1 in bits 0 ~ 15.
  709. */
  710. mode_odt_enable = 0x0; /* Default disabled */
  711. if (odt_wr_cfg || odt_rd_cfg) {
  712. /*
  713. * Bits 6 and 2 in Extended MRS(1)
  714. * Bit 2 == 0x04 == 75 Ohm, with 2 DIMM modules.
  715. * Bit 6 == 0x40 == 150 Ohm, with 1 DIMM module.
  716. */
  717. mode_odt_enable = 0x40; /* 150 Ohm */
  718. }
  719. ddr->sdram_mode_1 =
  720. (0
  721. | (add_lat << (16 + 3)) /* Additive Latency in EMRS1 */
  722. | (mode_odt_enable << 16) /* ODT Enable in EMRS1 */
  723. | (twr_auto_clk << 9) /* Write Recovery Autopre */
  724. | (mode_caslat << 4) /* caslat */
  725. | (burst_len << 0) /* Burst length */
  726. );
  727. debug("DDR: sdram_mode = 0x%08x\n", ddr->sdram_mode_1);
  728. /*
  729. * Clear EMRS2 and EMRS3.
  730. */
  731. ddr->sdram_mode_2 = 0;
  732. debug("DDR: sdram_mode_2 = 0x%08x\n", ddr->sdram_mode_2);
  733. /*
  734. * Determine Refresh Rate.
  735. */
  736. refresh_clk = determine_refresh_rate(spd.refresh & 0x7);
  737. /*
  738. * Set BSTOPRE to 0x100 for page mode
  739. * If auto-charge is used, set BSTOPRE = 0
  740. */
  741. ddr->sdram_interval =
  742. (0
  743. | (refresh_clk & 0x3fff) << 16
  744. | 0x100
  745. );
  746. debug("DDR: sdram_interval = 0x%08x\n", ddr->sdram_interval);
  747. /*
  748. * Is this an ECC DDR chip?
  749. * But don't mess with it if the DDR controller will init mem.
  750. */
  751. #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  752. if (spd.config == 0x02) {
  753. ddr->err_disable = 0x0000000d;
  754. ddr->err_sbe = 0x00ff0000;
  755. }
  756. debug("DDR: err_disable = 0x%08x\n", ddr->err_disable);
  757. debug("DDR: err_sbe = 0x%08x\n", ddr->err_sbe);
  758. #endif
  759. asm volatile("sync;isync");
  760. udelay(500);
  761. /*
  762. * SDRAM Cfg 2
  763. */
  764. /*
  765. * When ODT is enabled, Chap 9 suggests asserting ODT to
  766. * internal IOs only during reads.
  767. */
  768. odt_cfg = 0;
  769. if (odt_rd_cfg | odt_wr_cfg) {
  770. odt_cfg = 0x2; /* ODT to IOs during reads */
  771. }
  772. /*
  773. * Try to use differential DQS with DDR II.
  774. */
  775. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  776. dqs_cfg = 0; /* No Differential DQS for DDR I */
  777. } else {
  778. dqs_cfg = 0x1; /* Differential DQS for DDR II */
  779. }
  780. #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  781. /*
  782. * Use the DDR controller to auto initialize memory.
  783. */
  784. d_init = 1;
  785. ddr->sdram_data_init = CONFIG_MEM_INIT_VALUE;
  786. debug("DDR: ddr_data_init = 0x%08x\n", ddr->sdram_data_init);
  787. #else
  788. /*
  789. * Memory will be initialized via DMA, or not at all.
  790. */
  791. d_init = 0;
  792. #endif
  793. ddr->sdram_cfg_2 = (0
  794. | (dqs_cfg << 26) /* Differential DQS */
  795. | (odt_cfg << 21) /* ODT */
  796. | (d_init << 4) /* D_INIT auto init DDR */
  797. );
  798. debug("DDR: sdram_cfg_2 = 0x%08x\n", ddr->sdram_cfg_2);
  799. #ifdef MPC86xx_DDR_SDRAM_CLK_CNTL
  800. /*
  801. * Setup the clock control.
  802. * SDRAM_CLK_CNTL[0] = Source synchronous enable == 1
  803. * SDRAM_CLK_CNTL[5-7] = Clock Adjust
  804. * 0110 3/4 cycle late
  805. * 0111 7/8 cycle late
  806. */
  807. if (spd.mem_type == SPD_MEMTYPE_DDR)
  808. clk_adjust = 0x6;
  809. else
  810. clk_adjust = 0x7;
  811. ddr->sdram_clk_cntl = (0
  812. | 0x80000000
  813. | (clk_adjust << 23)
  814. );
  815. debug("DDR: sdram_clk_cntl = 0x%08x\n", ddr->sdram_clk_cntl);
  816. #endif
  817. /*
  818. * Figure out memory size in Megabytes.
  819. */
  820. debug("# ranks = %d, rank_density = 0x%08lx\n", n_ranks, rank_density);
  821. memsize = n_ranks * rank_density / 0x100000;
  822. return memsize;
  823. }
  824. unsigned int enable_ddr(unsigned int ddr_num)
  825. {
  826. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  827. spd_eeprom_t spd1,spd2;
  828. volatile ccsr_ddr_t *ddr;
  829. unsigned sdram_cfg_1;
  830. unsigned char sdram_type, mem_type, config, mod_attr;
  831. unsigned char d_init;
  832. unsigned int no_dimm1=0, no_dimm2=0;
  833. /* Set up pointer to enable the current ddr controller */
  834. if (ddr_num == 1)
  835. ddr = &immap->im_ddr1;
  836. else
  837. ddr = &immap->im_ddr2;
  838. /*
  839. * Read both dimm slots and decide whether
  840. * or not to enable this controller.
  841. */
  842. memset((void *)&spd1, 0, sizeof(spd1));
  843. memset((void *)&spd2, 0, sizeof(spd2));
  844. if (ddr_num == 1) {
  845. CFG_READ_SPD(SPD_EEPROM_ADDRESS1,
  846. 0, 1, (uchar *) &spd1, sizeof(spd1));
  847. #if defined(SPD_EEPROM_ADDRESS2)
  848. CFG_READ_SPD(SPD_EEPROM_ADDRESS2,
  849. 0, 1, (uchar *) &spd2, sizeof(spd2));
  850. #endif
  851. } else {
  852. #if defined(SPD_EEPROM_ADDRESS3)
  853. CFG_READ_SPD(SPD_EEPROM_ADDRESS3,
  854. 0, 1, (uchar *) &spd1, sizeof(spd1));
  855. #endif
  856. #if defined(SPD_EEPROM_ADDRESS4)
  857. CFG_READ_SPD(SPD_EEPROM_ADDRESS4,
  858. 0, 1, (uchar *) &spd2, sizeof(spd2));
  859. #endif
  860. }
  861. /*
  862. * Check for supported memory module types.
  863. */
  864. if (spd1.mem_type != SPD_MEMTYPE_DDR
  865. && spd1.mem_type != SPD_MEMTYPE_DDR2) {
  866. no_dimm1 = 1;
  867. } else {
  868. debug("\nFound memory of type 0x%02lx ",spd1.mem_type );
  869. if (spd1.mem_type == SPD_MEMTYPE_DDR)
  870. debug("DDR I\n");
  871. else
  872. debug("DDR II\n");
  873. }
  874. if (spd2.mem_type != SPD_MEMTYPE_DDR &&
  875. spd2.mem_type != SPD_MEMTYPE_DDR2) {
  876. no_dimm2 = 1;
  877. } else {
  878. debug("\nFound memory of type 0x%02lx ",spd2.mem_type );
  879. if (spd2.mem_type == SPD_MEMTYPE_DDR)
  880. debug("DDR I\n");
  881. else
  882. debug("DDR II\n");
  883. }
  884. #ifdef CONFIG_DDR_INTERLEAVE
  885. if (no_dimm1) {
  886. printf("For interleaved operation memory modules need to be present in CS0 DIMM slots of both DDR controllers!\n");
  887. return 0;
  888. }
  889. #endif
  890. /*
  891. * Memory is not present in DIMM1 and DIMM2 - so do not enable DDRn
  892. */
  893. if (no_dimm1 && no_dimm2) {
  894. printf("No memory modules found for DDR controller %d!!\n", ddr_num);
  895. return 0;
  896. } else {
  897. mem_type = no_dimm2 ? spd1.mem_type : spd2.mem_type;
  898. /*
  899. * Figure out the settings for the sdram_cfg register.
  900. * Build up the entire register in 'sdram_cfg' before
  901. * writing since the write into the register will
  902. * actually enable the memory controller; all settings
  903. * must be done before enabling.
  904. *
  905. * sdram_cfg[0] = 1 (ddr sdram logic enable)
  906. * sdram_cfg[1] = 1 (self-refresh-enable)
  907. * sdram_cfg[5:7] = (SDRAM type = DDR SDRAM)
  908. * 010 DDR 1 SDRAM
  909. * 011 DDR 2 SDRAM
  910. */
  911. sdram_type = (mem_type == SPD_MEMTYPE_DDR) ? 2 : 3;
  912. sdram_cfg_1 = (0
  913. | (1 << 31) /* Enable */
  914. | (1 << 30) /* Self refresh */
  915. | (sdram_type << 24) /* SDRAM type */
  916. );
  917. /*
  918. * sdram_cfg[3] = RD_EN - registered DIMM enable
  919. * A value of 0x26 indicates micron registered
  920. * DIMMS (micron.com)
  921. */
  922. mod_attr = no_dimm2 ? spd1.mod_attr : spd2.mod_attr;
  923. if (mem_type == SPD_MEMTYPE_DDR && mod_attr == 0x26) {
  924. sdram_cfg_1 |= 0x10000000; /* RD_EN */
  925. }
  926. #if defined(CONFIG_DDR_ECC)
  927. config = no_dimm2 ? spd1.config : spd2.config;
  928. /*
  929. * If the user wanted ECC (enabled via sdram_cfg[2])
  930. */
  931. if (config == 0x02) {
  932. ddr->err_disable = 0x00000000;
  933. asm volatile("sync;isync;");
  934. ddr->err_sbe = 0x00ff0000;
  935. ddr->err_int_en = 0x0000000d;
  936. sdram_cfg_1 |= 0x20000000; /* ECC_EN */
  937. }
  938. #endif
  939. /*
  940. * Set 1T or 2T timing based on 1 or 2 modules
  941. */
  942. {
  943. if (!(no_dimm1 || no_dimm2)) {
  944. /*
  945. * 2T timing,because both DIMMS are present.
  946. * Enable 2T timing by setting sdram_cfg[16].
  947. */
  948. sdram_cfg_1 |= 0x8000; /* 2T_EN */
  949. }
  950. }
  951. /*
  952. * 200 painful micro-seconds must elapse between
  953. * the DDR clock setup and the DDR config enable.
  954. */
  955. udelay(200);
  956. /*
  957. * Go!
  958. */
  959. ddr->sdram_cfg_1 = sdram_cfg_1;
  960. asm volatile("sync;isync");
  961. udelay(500);
  962. debug("DDR: sdram_cfg = 0x%08x\n", ddr->sdram_cfg_1);
  963. #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  964. d_init = 1;
  965. debug("DDR: memory initializing\n");
  966. /*
  967. * Poll until memory is initialized.
  968. * 512 Meg at 400 might hit this 200 times or so.
  969. */
  970. while ((ddr->sdram_cfg_2 & (d_init << 4)) != 0) {
  971. udelay(1000);
  972. }
  973. debug("DDR: memory initialized\n\n");
  974. #endif
  975. debug("Enabled DDR Controller %d\n", ddr_num);
  976. return 1;
  977. }
  978. }
  979. long int
  980. spd_sdram(void)
  981. {
  982. int memsize_ddr1_dimm1 = 0;
  983. int memsize_ddr1_dimm2 = 0;
  984. int memsize_ddr1 = 0;
  985. unsigned int law_size_ddr1;
  986. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  987. volatile ccsr_local_mcm_t *mcm = &immap->im_local_mcm;
  988. #ifdef CONFIG_DDR_INTERLEAVE
  989. volatile ccsr_ddr_t *ddr1 = &immap->im_ddr1;
  990. #endif
  991. #if (CONFIG_NUM_DDR_CONTROLLERS > 1)
  992. int memsize_ddr2_dimm1 = 0;
  993. int memsize_ddr2_dimm2 = 0;
  994. int memsize_ddr2 = 0;
  995. unsigned int law_size_ddr2;
  996. #endif
  997. unsigned int ddr1_enabled = 0;
  998. unsigned int ddr2_enabled = 0;
  999. int memsize_total = 0;
  1000. #ifdef CONFIG_DDR_INTERLEAVE
  1001. unsigned int law_size_interleaved;
  1002. volatile ccsr_ddr_t *ddr2 = &immap->im_ddr2;
  1003. memsize_ddr1_dimm1 = spd_init(SPD_EEPROM_ADDRESS1,
  1004. 1, 1,
  1005. (unsigned int)memsize_total * 1024*1024);
  1006. memsize_total += memsize_ddr1_dimm1;
  1007. memsize_ddr2_dimm1 = spd_init(SPD_EEPROM_ADDRESS3,
  1008. 2, 1,
  1009. (unsigned int)memsize_total * 1024*1024);
  1010. memsize_total += memsize_ddr2_dimm1;
  1011. if (memsize_ddr1_dimm1 != memsize_ddr2_dimm1) {
  1012. if (memsize_ddr1_dimm1 < memsize_ddr2_dimm1)
  1013. memsize_total -= memsize_ddr1_dimm1;
  1014. else
  1015. memsize_total -= memsize_ddr2_dimm1;
  1016. debug("Total memory available for interleaving 0x%08lx\n",
  1017. memsize_total * 1024 * 1024);
  1018. debug("Adjusting CS0_BNDS to account for unequal DIMM sizes in interleaved memory\n");
  1019. ddr1->cs0_bnds = ((memsize_total * 1024 * 1024) - 1) >> 24;
  1020. ddr2->cs0_bnds = ((memsize_total * 1024 * 1024) - 1) >> 24;
  1021. debug("DDR1: cs0_bnds = 0x%08x\n", ddr1->cs0_bnds);
  1022. debug("DDR2: cs0_bnds = 0x%08x\n", ddr2->cs0_bnds);
  1023. }
  1024. ddr1_enabled = enable_ddr(1);
  1025. ddr2_enabled = enable_ddr(2);
  1026. /*
  1027. * Both controllers need to be enabled for interleaving.
  1028. */
  1029. if (ddr1_enabled && ddr2_enabled) {
  1030. law_size_interleaved = 19 + __ilog2(memsize_total);
  1031. /*
  1032. * Set up LAWBAR for DDR 1 space.
  1033. */
  1034. mcm->lawbar1 = ((CFG_DDR_SDRAM_BASE >> 12) & 0xfffff);
  1035. mcm->lawar1 = (LAWAR_EN
  1036. | LAWAR_TRGT_IF_DDR_INTERLEAVED
  1037. | (LAWAR_SIZE & law_size_interleaved));
  1038. debug("DDR: LAWBAR1=0x%08x\n", mcm->lawbar1);
  1039. debug("DDR: LAWAR1=0x%08x\n", mcm->lawar1);
  1040. debug("Interleaved memory size is 0x%08lx\n", memsize_total);
  1041. #ifdef CONFIG_DDR_INTERLEAVE
  1042. #if (CFG_PAGE_INTERLEAVING == 1)
  1043. printf("Page ");
  1044. #elif (CFG_BANK_INTERLEAVING == 1)
  1045. printf("Bank ");
  1046. #elif (CFG_SUPER_BANK_INTERLEAVING == 1)
  1047. printf("Super-bank ");
  1048. #else
  1049. printf("Cache-line ");
  1050. #endif
  1051. #endif
  1052. printf("Interleaved");
  1053. return memsize_total * 1024 * 1024;
  1054. } else {
  1055. printf("Interleaved memory not enabled - check CS0 DIMM slots for both controllers.\n");
  1056. return 0;
  1057. }
  1058. #else
  1059. /*
  1060. * Call spd_sdram() routine to init ddr1 - pass I2c address,
  1061. * controller number, dimm number, and starting address.
  1062. */
  1063. memsize_ddr1_dimm1 = spd_init(SPD_EEPROM_ADDRESS1,
  1064. 1, 1,
  1065. (unsigned int)memsize_total * 1024*1024);
  1066. memsize_total += memsize_ddr1_dimm1;
  1067. #if defined(SPD_EEPROM_ADDRESS2)
  1068. memsize_ddr1_dimm2 = spd_init(SPD_EEPROM_ADDRESS2,
  1069. 1, 2,
  1070. (unsigned int)memsize_total * 1024*1024);
  1071. #endif
  1072. memsize_total += memsize_ddr1_dimm2;
  1073. /*
  1074. * Enable the DDR controller - pass ddr controller number.
  1075. */
  1076. ddr1_enabled = enable_ddr(1);
  1077. /* Keep track of memory to be addressed by DDR1 */
  1078. memsize_ddr1 = memsize_ddr1_dimm1 + memsize_ddr1_dimm2;
  1079. /*
  1080. * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23. Fnord.
  1081. */
  1082. if (ddr1_enabled) {
  1083. law_size_ddr1 = 19 + __ilog2(memsize_ddr1);
  1084. /*
  1085. * Set up LAWBAR for DDR 1 space.
  1086. */
  1087. mcm->lawbar1 = ((CFG_DDR_SDRAM_BASE >> 12) & 0xfffff);
  1088. mcm->lawar1 = (LAWAR_EN
  1089. | LAWAR_TRGT_IF_DDR1
  1090. | (LAWAR_SIZE & law_size_ddr1));
  1091. debug("DDR: LAWBAR1=0x%08x\n", mcm->lawbar1);
  1092. debug("DDR: LAWAR1=0x%08x\n", mcm->lawar1);
  1093. }
  1094. #if (CONFIG_NUM_DDR_CONTROLLERS > 1)
  1095. memsize_ddr2_dimm1 = spd_init(SPD_EEPROM_ADDRESS3,
  1096. 2, 1,
  1097. (unsigned int)memsize_total * 1024*1024);
  1098. memsize_total += memsize_ddr2_dimm1;
  1099. memsize_ddr2_dimm2 = spd_init(SPD_EEPROM_ADDRESS4,
  1100. 2, 2,
  1101. (unsigned int)memsize_total * 1024*1024);
  1102. memsize_total += memsize_ddr2_dimm2;
  1103. ddr2_enabled = enable_ddr(2);
  1104. /* Keep track of memory to be addressed by DDR2 */
  1105. memsize_ddr2 = memsize_ddr2_dimm1 + memsize_ddr2_dimm2;
  1106. if (ddr2_enabled) {
  1107. law_size_ddr2 = 19 + __ilog2(memsize_ddr2);
  1108. /*
  1109. * Set up LAWBAR for DDR 2 space.
  1110. */
  1111. if (ddr1_enabled)
  1112. mcm->lawbar8 = (((memsize_ddr1 * 1024 * 1024) >> 12)
  1113. & 0xfffff);
  1114. else
  1115. mcm->lawbar8 = ((CFG_DDR_SDRAM_BASE >> 12) & 0xfffff);
  1116. mcm->lawar8 = (LAWAR_EN
  1117. | LAWAR_TRGT_IF_DDR2
  1118. | (LAWAR_SIZE & law_size_ddr2));
  1119. debug("\nDDR: LAWBAR8=0x%08x\n", mcm->lawbar8);
  1120. debug("DDR: LAWAR8=0x%08x\n", mcm->lawar8);
  1121. }
  1122. debug("\nMemory size of DDR2 = 0x%08lx\n", memsize_ddr2);
  1123. #endif /* CONFIG_NUM_DDR_CONTROLLERS > 1 */
  1124. debug("\nMemory size of DDR1 = 0x%08lx\n", memsize_ddr1);
  1125. /*
  1126. * If neither DDR controller is enabled return 0.
  1127. */
  1128. if (!ddr1_enabled && !ddr2_enabled)
  1129. return 0;
  1130. printf("Non-interleaved");
  1131. return memsize_total * 1024 * 1024;
  1132. #endif /* CONFIG_DDR_INTERLEAVE */
  1133. }
  1134. #endif /* CONFIG_SPD_EEPROM */
  1135. #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  1136. /*
  1137. * Initialize all of memory for ECC, then enable errors.
  1138. */
  1139. void
  1140. ddr_enable_ecc(unsigned int dram_size)
  1141. {
  1142. uint *p = 0;
  1143. uint i = 0;
  1144. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  1145. volatile ccsr_ddr_t *ddr1= &immap->im_ddr1;
  1146. dma_init();
  1147. for (*p = 0; p < (uint *)(8 * 1024); p++) {
  1148. if (((unsigned int)p & 0x1f) == 0) {
  1149. ppcDcbz((unsigned long) p);
  1150. }
  1151. *p = (unsigned int)CONFIG_MEM_INIT_VALUE;
  1152. if (((unsigned int)p & 0x1c) == 0x1c) {
  1153. ppcDcbf((unsigned long) p);
  1154. }
  1155. }
  1156. dma_xfer((uint *)0x002000, 0x002000, (uint *)0); /* 8K */
  1157. dma_xfer((uint *)0x004000, 0x004000, (uint *)0); /* 16K */
  1158. dma_xfer((uint *)0x008000, 0x008000, (uint *)0); /* 32K */
  1159. dma_xfer((uint *)0x010000, 0x010000, (uint *)0); /* 64K */
  1160. dma_xfer((uint *)0x020000, 0x020000, (uint *)0); /* 128k */
  1161. dma_xfer((uint *)0x040000, 0x040000, (uint *)0); /* 256k */
  1162. dma_xfer((uint *)0x080000, 0x080000, (uint *)0); /* 512k */
  1163. dma_xfer((uint *)0x100000, 0x100000, (uint *)0); /* 1M */
  1164. dma_xfer((uint *)0x200000, 0x200000, (uint *)0); /* 2M */
  1165. dma_xfer((uint *)0x400000, 0x400000, (uint *)0); /* 4M */
  1166. for (i = 1; i < dram_size / 0x800000; i++) {
  1167. dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
  1168. }
  1169. /*
  1170. * Enable errors for ECC.
  1171. */
  1172. debug("DMA DDR: err_disable = 0x%08x\n", ddr1->err_disable);
  1173. ddr1->err_disable = 0x00000000;
  1174. asm volatile("sync;isync");
  1175. debug("DMA DDR: err_disable = 0x%08x\n", ddr1->err_disable);
  1176. }
  1177. #endif /* CONFIG_DDR_ECC && ! CONFIG_ECC_INIT_VIA_DDRCONTROLLER */