spd_sdram.c 34 KB

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