spd_sdram.c 35 KB

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