spd_sdram.c 34 KB

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