spd_sdram.c 33 KB

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