spd_sdram.c 34 KB

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