denali_spd_ddr2.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /*
  2. * cpu/ppc4xx/denali_spd_ddr2.c
  3. * This SPD SDRAM detection code supports AMCC PPC44x CPUs with a Denali-core
  4. * DDR2 controller, specifically the 440EPx/GRx.
  5. *
  6. * (C) Copyright 2007-2008
  7. * Larry Johnson, lrj@acm.org.
  8. *
  9. * Based primarily on cpu/ppc4xx/4xx_spd_ddr2.c, which is...
  10. *
  11. * (C) Copyright 2007
  12. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  13. *
  14. * COPYRIGHT AMCC CORPORATION 2004
  15. *
  16. * See file CREDITS for list of people who contributed to this
  17. * project.
  18. *
  19. * This program is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU General Public License as
  21. * published by the Free Software Foundation; either version 2 of
  22. * the License, or (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  32. * MA 02111-1307 USA
  33. *
  34. */
  35. /* define DEBUG for debugging output (obviously ;-)) */
  36. #if 0
  37. #define DEBUG
  38. #endif
  39. #include <common.h>
  40. #include <command.h>
  41. #include <ppc4xx.h>
  42. #include <i2c.h>
  43. #include <asm/io.h>
  44. #include <asm/processor.h>
  45. #include <asm/mmu.h>
  46. #if defined(CONFIG_SPD_EEPROM) && \
  47. (defined(CONFIG_440EPX) || defined(CONFIG_440GRX))
  48. /*-----------------------------------------------------------------------------+
  49. * Defines
  50. *-----------------------------------------------------------------------------*/
  51. #ifndef TRUE
  52. #define TRUE 1
  53. #endif
  54. #ifndef FALSE
  55. #define FALSE 0
  56. #endif
  57. #define MAXDIMMS 2
  58. #define MAXRANKS 2
  59. #define ONE_BILLION 1000000000
  60. #define MULDIV64(m1, m2, d) (u32)(((u64)(m1) * (u64)(m2)) / (u64)(d))
  61. #define DLL_DQS_DELAY 0x19
  62. #define DLL_DQS_BYPASS 0x0B
  63. #define DQS_OUT_SHIFT 0x7F
  64. /*
  65. * This DDR2 setup code can dynamically setup the TLB entries for the DDR2 memory
  66. * region. Right now the cache should still be disabled in U-Boot because of the
  67. * EMAC driver, that need it's buffer descriptor to be located in non cached
  68. * memory.
  69. *
  70. * If at some time this restriction doesn't apply anymore, just define
  71. * CONFIG_4xx_DCACHE in the board config file and this code should setup
  72. * everything correctly.
  73. */
  74. #if defined(CONFIG_4xx_DCACHE)
  75. #define MY_TLB_WORD2_I_ENABLE 0 /* enable caching on SDRAM */
  76. #else
  77. #define MY_TLB_WORD2_I_ENABLE TLB_WORD2_I_ENABLE /* disable caching on SDRAM */
  78. #endif
  79. /*-----------------------------------------------------------------------------+
  80. * Prototypes
  81. *-----------------------------------------------------------------------------*/
  82. extern int denali_wait_for_dlllock(void);
  83. extern void denali_core_search_data_eye(void);
  84. extern void dcbz_area(u32 start_address, u32 num_bytes);
  85. extern void dflush(void);
  86. /*
  87. * Board-specific Platform code can reimplement spd_ddr_init_hang () if needed
  88. */
  89. void __spd_ddr_init_hang(void)
  90. {
  91. hang();
  92. }
  93. void spd_ddr_init_hang(void)
  94. __attribute__ ((weak, alias("__spd_ddr_init_hang")));
  95. #if defined(DEBUG)
  96. static void print_mcsr(void)
  97. {
  98. printf("MCSR = 0x%08X\n", mfspr(SPRN_MCSR));
  99. }
  100. static void denali_sdram_register_dump(void)
  101. {
  102. unsigned int sdram_data;
  103. printf("\n Register Dump:\n");
  104. mfsdram(DDR0_00, sdram_data);
  105. printf(" DDR0_00 = 0x%08X", sdram_data);
  106. mfsdram(DDR0_01, sdram_data);
  107. printf(" DDR0_01 = 0x%08X\n", sdram_data);
  108. mfsdram(DDR0_02, sdram_data);
  109. printf(" DDR0_02 = 0x%08X", sdram_data);
  110. mfsdram(DDR0_03, sdram_data);
  111. printf(" DDR0_03 = 0x%08X\n", sdram_data);
  112. mfsdram(DDR0_04, sdram_data);
  113. printf(" DDR0_04 = 0x%08X", sdram_data);
  114. mfsdram(DDR0_05, sdram_data);
  115. printf(" DDR0_05 = 0x%08X\n", sdram_data);
  116. mfsdram(DDR0_06, sdram_data);
  117. printf(" DDR0_06 = 0x%08X", sdram_data);
  118. mfsdram(DDR0_07, sdram_data);
  119. printf(" DDR0_07 = 0x%08X\n", sdram_data);
  120. mfsdram(DDR0_08, sdram_data);
  121. printf(" DDR0_08 = 0x%08X", sdram_data);
  122. mfsdram(DDR0_09, sdram_data);
  123. printf(" DDR0_09 = 0x%08X\n", sdram_data);
  124. mfsdram(DDR0_10, sdram_data);
  125. printf(" DDR0_10 = 0x%08X", sdram_data);
  126. mfsdram(DDR0_11, sdram_data);
  127. printf(" DDR0_11 = 0x%08X\n", sdram_data);
  128. mfsdram(DDR0_12, sdram_data);
  129. printf(" DDR0_12 = 0x%08X", sdram_data);
  130. mfsdram(DDR0_14, sdram_data);
  131. printf(" DDR0_14 = 0x%08X\n", sdram_data);
  132. mfsdram(DDR0_17, sdram_data);
  133. printf(" DDR0_17 = 0x%08X", sdram_data);
  134. mfsdram(DDR0_18, sdram_data);
  135. printf(" DDR0_18 = 0x%08X\n", sdram_data);
  136. mfsdram(DDR0_19, sdram_data);
  137. printf(" DDR0_19 = 0x%08X", sdram_data);
  138. mfsdram(DDR0_20, sdram_data);
  139. printf(" DDR0_20 = 0x%08X\n", sdram_data);
  140. mfsdram(DDR0_21, sdram_data);
  141. printf(" DDR0_21 = 0x%08X", sdram_data);
  142. mfsdram(DDR0_22, sdram_data);
  143. printf(" DDR0_22 = 0x%08X\n", sdram_data);
  144. mfsdram(DDR0_23, sdram_data);
  145. printf(" DDR0_23 = 0x%08X", sdram_data);
  146. mfsdram(DDR0_24, sdram_data);
  147. printf(" DDR0_24 = 0x%08X\n", sdram_data);
  148. mfsdram(DDR0_25, sdram_data);
  149. printf(" DDR0_25 = 0x%08X", sdram_data);
  150. mfsdram(DDR0_26, sdram_data);
  151. printf(" DDR0_26 = 0x%08X\n", sdram_data);
  152. mfsdram(DDR0_27, sdram_data);
  153. printf(" DDR0_27 = 0x%08X", sdram_data);
  154. mfsdram(DDR0_28, sdram_data);
  155. printf(" DDR0_28 = 0x%08X\n", sdram_data);
  156. mfsdram(DDR0_31, sdram_data);
  157. printf(" DDR0_31 = 0x%08X", sdram_data);
  158. mfsdram(DDR0_32, sdram_data);
  159. printf(" DDR0_32 = 0x%08X\n", sdram_data);
  160. mfsdram(DDR0_33, sdram_data);
  161. printf(" DDR0_33 = 0x%08X", sdram_data);
  162. mfsdram(DDR0_34, sdram_data);
  163. printf(" DDR0_34 = 0x%08X\n", sdram_data);
  164. mfsdram(DDR0_35, sdram_data);
  165. printf(" DDR0_35 = 0x%08X", sdram_data);
  166. mfsdram(DDR0_36, sdram_data);
  167. printf(" DDR0_36 = 0x%08X\n", sdram_data);
  168. mfsdram(DDR0_37, sdram_data);
  169. printf(" DDR0_37 = 0x%08X", sdram_data);
  170. mfsdram(DDR0_38, sdram_data);
  171. printf(" DDR0_38 = 0x%08X\n", sdram_data);
  172. mfsdram(DDR0_39, sdram_data);
  173. printf(" DDR0_39 = 0x%08X", sdram_data);
  174. mfsdram(DDR0_40, sdram_data);
  175. printf(" DDR0_40 = 0x%08X\n", sdram_data);
  176. mfsdram(DDR0_41, sdram_data);
  177. printf(" DDR0_41 = 0x%08X", sdram_data);
  178. mfsdram(DDR0_42, sdram_data);
  179. printf(" DDR0_42 = 0x%08X\n", sdram_data);
  180. mfsdram(DDR0_43, sdram_data);
  181. printf(" DDR0_43 = 0x%08X", sdram_data);
  182. mfsdram(DDR0_44, sdram_data);
  183. printf(" DDR0_44 = 0x%08X\n", sdram_data);
  184. }
  185. #else
  186. static inline void denali_sdram_register_dump(void)
  187. {
  188. }
  189. inline static void print_mcsr(void)
  190. {
  191. }
  192. #endif /* defined(DEBUG) */
  193. static int is_ecc_enabled(void)
  194. {
  195. u32 val;
  196. mfsdram(DDR0_22, val);
  197. return 0x3 == DDR0_22_CTRL_RAW_DECODE(val);
  198. }
  199. static unsigned char spd_read(u8 chip, unsigned int addr)
  200. {
  201. u8 data[2];
  202. if (0 != i2c_probe(chip) || 0 != i2c_read(chip, addr, 1, data, 1)) {
  203. debug("spd_read(0x%02X, 0x%02X) failed\n", chip, addr);
  204. return 0;
  205. }
  206. debug("spd_read(0x%02X, 0x%02X) returned 0x%02X\n",
  207. chip, addr, data[0]);
  208. return data[0];
  209. }
  210. static unsigned long get_tcyc(unsigned char reg)
  211. {
  212. /*
  213. * Byte 9, et al: Cycle time for CAS Latency=X, is split into two
  214. * nibbles: the higher order nibble (bits 4-7) designates the cycle time
  215. * to a granularity of 1ns; the value presented by the lower order
  216. * nibble (bits 0-3) has a granularity of .1ns and is added to the value
  217. * designated by the higher nibble. In addition, four lines of the lower
  218. * order nibble are assigned to support +.25, +.33, +.66, and +.75.
  219. */
  220. unsigned char subfield_b = reg & 0x0F;
  221. switch (subfield_b & 0x0F) {
  222. case 0x0:
  223. case 0x1:
  224. case 0x2:
  225. case 0x3:
  226. case 0x4:
  227. case 0x5:
  228. case 0x6:
  229. case 0x7:
  230. case 0x8:
  231. case 0x9:
  232. return 1000 * (reg >> 4) + 100 * subfield_b;
  233. case 0xA:
  234. return 1000 * (reg >> 4) + 250;
  235. case 0xB:
  236. return 1000 * (reg >> 4) + 333;
  237. case 0xC:
  238. return 1000 * (reg >> 4) + 667;
  239. case 0xD:
  240. return 1000 * (reg >> 4) + 750;
  241. }
  242. return 0;
  243. }
  244. /*------------------------------------------------------------------
  245. * Find the installed DIMMs, make sure that the are DDR2, and fill
  246. * in the dimm_ranks array. Then dimm_ranks[dimm_num] > 0 iff the
  247. * DIMM and dimm_num is present.
  248. * Note: Because there are only two chip-select lines, it is assumed
  249. * that a board with a single socket can support two ranks on that
  250. * socket, while a board with two sockets can support only one rank
  251. * on each socket.
  252. *-----------------------------------------------------------------*/
  253. static void get_spd_info(unsigned long dimm_ranks[],
  254. unsigned long *ranks,
  255. unsigned char const iic0_dimm_addr[],
  256. unsigned long num_dimm_banks)
  257. {
  258. unsigned long dimm_num;
  259. unsigned long dimm_found = FALSE;
  260. unsigned long const max_ranks_per_dimm = (1 == num_dimm_banks) ? 2 : 1;
  261. unsigned char num_of_bytes;
  262. unsigned char total_size;
  263. *ranks = 0;
  264. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  265. num_of_bytes = 0;
  266. total_size = 0;
  267. num_of_bytes = spd_read(iic0_dimm_addr[dimm_num], 0);
  268. total_size = spd_read(iic0_dimm_addr[dimm_num], 1);
  269. if ((num_of_bytes != 0) && (total_size != 0)) {
  270. unsigned char const dimm_type =
  271. spd_read(iic0_dimm_addr[dimm_num], 2);
  272. unsigned long ranks_on_dimm =
  273. (spd_read(iic0_dimm_addr[dimm_num], 5) & 0x07) + 1;
  274. if (8 != dimm_type) {
  275. switch (dimm_type) {
  276. case 1:
  277. printf("ERROR: Standard Fast Page Mode "
  278. "DRAM DIMM");
  279. break;
  280. case 2:
  281. printf("ERROR: EDO DIMM");
  282. break;
  283. case 3:
  284. printf("ERROR: Pipelined Nibble DIMM");
  285. break;
  286. case 4:
  287. printf("ERROR: SDRAM DIMM");
  288. break;
  289. case 5:
  290. printf("ERROR: Multiplexed ROM DIMM");
  291. break;
  292. case 6:
  293. printf("ERROR: SGRAM DIMM");
  294. break;
  295. case 7:
  296. printf("ERROR: DDR1 DIMM");
  297. break;
  298. default:
  299. printf("ERROR: Unknown DIMM (type %d)",
  300. (unsigned int)dimm_type);
  301. break;
  302. }
  303. printf(" detected in slot %lu.\n", dimm_num);
  304. printf("Only DDR2 SDRAM DIMMs are supported."
  305. "\n");
  306. printf("Replace the module with a DDR2 DIMM."
  307. "\n\n");
  308. spd_ddr_init_hang();
  309. }
  310. dimm_found = TRUE;
  311. debug("DIMM slot %lu: populated with %lu-rank DDR2 DIMM"
  312. "\n", dimm_num, ranks_on_dimm);
  313. if (ranks_on_dimm > max_ranks_per_dimm) {
  314. printf("WARNING: DRAM DIMM in slot %lu has %lu "
  315. "ranks.\n");
  316. if (1 == max_ranks_per_dimm) {
  317. printf("Only one rank will be used.\n");
  318. } else {
  319. printf
  320. ("Only two ranks will be used.\n");
  321. }
  322. ranks_on_dimm = max_ranks_per_dimm;
  323. }
  324. dimm_ranks[dimm_num] = ranks_on_dimm;
  325. *ranks += ranks_on_dimm;
  326. } else {
  327. dimm_ranks[dimm_num] = 0;
  328. debug("DIMM slot %lu: Not populated\n", dimm_num);
  329. }
  330. }
  331. if (dimm_found == FALSE) {
  332. printf("ERROR: No memory installed.\n");
  333. printf("Install at least one DDR2 DIMM.\n\n");
  334. spd_ddr_init_hang();
  335. }
  336. debug("Total number of ranks = %d\n", *ranks);
  337. }
  338. /*------------------------------------------------------------------
  339. * For the memory DIMMs installed, this routine verifies that
  340. * frequency previously calculated is supported.
  341. *-----------------------------------------------------------------*/
  342. static void check_frequency(unsigned long *dimm_ranks,
  343. unsigned char const iic0_dimm_addr[],
  344. unsigned long num_dimm_banks,
  345. unsigned long sdram_freq)
  346. {
  347. unsigned long dimm_num;
  348. unsigned long cycle_time;
  349. unsigned long calc_cycle_time;
  350. /*
  351. * calc_cycle_time is calculated from DDR frequency set by board/chip
  352. * and is expressed in picoseconds to match the way DIMM cycle time is
  353. * calculated below.
  354. */
  355. calc_cycle_time = MULDIV64(ONE_BILLION, 1000, sdram_freq);
  356. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  357. if (dimm_ranks[dimm_num]) {
  358. cycle_time =
  359. get_tcyc(spd_read(iic0_dimm_addr[dimm_num], 9));
  360. debug("cycle_time=%d ps\n", cycle_time);
  361. if (cycle_time > (calc_cycle_time + 10)) {
  362. /*
  363. * the provided sdram cycle_time is too small
  364. * for the available DIMM cycle_time. The
  365. * additionnal 10ps is here to accept a small
  366. * incertainty.
  367. */
  368. printf
  369. ("ERROR: DRAM DIMM detected with cycle_time %d ps in "
  370. "slot %d \n while calculated cycle time is %d ps.\n",
  371. (unsigned int)cycle_time,
  372. (unsigned int)dimm_num,
  373. (unsigned int)calc_cycle_time);
  374. printf
  375. ("Replace the DIMM, or change DDR frequency via "
  376. "strapping bits.\n\n");
  377. spd_ddr_init_hang();
  378. }
  379. }
  380. }
  381. }
  382. /*------------------------------------------------------------------
  383. * This routine gets size information for the installed memory
  384. * DIMMs.
  385. *-----------------------------------------------------------------*/
  386. static void get_dimm_size(unsigned long dimm_ranks[],
  387. unsigned char const iic0_dimm_addr[],
  388. unsigned long num_dimm_banks,
  389. unsigned long *const rows,
  390. unsigned long *const banks,
  391. unsigned long *const cols, unsigned long *const width)
  392. {
  393. unsigned long dimm_num;
  394. *rows = 0;
  395. *banks = 0;
  396. *cols = 0;
  397. *width = 0;
  398. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  399. if (dimm_ranks[dimm_num]) {
  400. unsigned long t;
  401. /* Rows */
  402. t = spd_read(iic0_dimm_addr[dimm_num], 3);
  403. if (0 == *rows) {
  404. *rows = t;
  405. } else if (t != *rows) {
  406. printf("ERROR: DRAM DIMM modules do not all "
  407. "have the same number of rows.\n\n");
  408. spd_ddr_init_hang();
  409. }
  410. /* Banks */
  411. t = spd_read(iic0_dimm_addr[dimm_num], 17);
  412. if (0 == *banks) {
  413. *banks = t;
  414. } else if (t != *banks) {
  415. printf("ERROR: DRAM DIMM modules do not all "
  416. "have the same number of banks.\n\n");
  417. spd_ddr_init_hang();
  418. }
  419. /* Columns */
  420. t = spd_read(iic0_dimm_addr[dimm_num], 4);
  421. if (0 == *cols) {
  422. *cols = t;
  423. } else if (t != *cols) {
  424. printf("ERROR: DRAM DIMM modules do not all "
  425. "have the same number of columns.\n\n");
  426. spd_ddr_init_hang();
  427. }
  428. /* Data width */
  429. t = spd_read(iic0_dimm_addr[dimm_num], 6);
  430. if (0 == *width) {
  431. *width = t;
  432. } else if (t != *width) {
  433. printf("ERROR: DRAM DIMM modules do not all "
  434. "have the same data width.\n\n");
  435. spd_ddr_init_hang();
  436. }
  437. }
  438. }
  439. debug("Number of rows = %d\n", *rows);
  440. debug("Number of columns = %d\n", *cols);
  441. debug("Number of banks = %d\n", *banks);
  442. debug("Data width = %d\n", *width);
  443. if (*rows > 14) {
  444. printf("ERROR: DRAM DIMM modules have %lu address rows.\n",
  445. *rows);
  446. printf("Only modules with 14 or fewer rows are supported.\n\n");
  447. spd_ddr_init_hang();
  448. }
  449. if (4 != *banks && 8 != *banks) {
  450. printf("ERROR: DRAM DIMM modules have %lu banks.\n", *banks);
  451. printf("Only modules with 4 or 8 banks are supported.\n\n");
  452. spd_ddr_init_hang();
  453. }
  454. if (*cols > 12) {
  455. printf("ERROR: DRAM DIMM modules have %lu address columns.\n",
  456. *cols);
  457. printf("Only modules with 12 or fewer columns are "
  458. "supported.\n\n");
  459. spd_ddr_init_hang();
  460. }
  461. if (32 != *width && 40 != *width && 64 != *width && 72 != *width) {
  462. printf("ERROR: DRAM DIMM modules have a width of %lu bit.\n",
  463. *width);
  464. printf("Only modules with widths of 32, 40, 64, and 72 bits "
  465. "are supported.\n\n");
  466. spd_ddr_init_hang();
  467. }
  468. }
  469. /*------------------------------------------------------------------
  470. * Only 1.8V modules are supported. This routine verifies this.
  471. *-----------------------------------------------------------------*/
  472. static void check_voltage_type(unsigned long dimm_ranks[],
  473. unsigned char const iic0_dimm_addr[],
  474. unsigned long num_dimm_banks)
  475. {
  476. unsigned long dimm_num;
  477. unsigned long voltage_type;
  478. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  479. if (dimm_ranks[dimm_num]) {
  480. voltage_type = spd_read(iic0_dimm_addr[dimm_num], 8);
  481. if (0x05 != voltage_type) { /* 1.8V for DDR2 */
  482. printf("ERROR: Slot %lu provides 1.8V for DDR2 "
  483. "DIMMs.\n", dimm_num);
  484. switch (voltage_type) {
  485. case 0x00:
  486. printf("This DIMM is 5.0 Volt/TTL.\n");
  487. break;
  488. case 0x01:
  489. printf("This DIMM is LVTTL.\n");
  490. break;
  491. case 0x02:
  492. printf("This DIMM is 1.5 Volt.\n");
  493. break;
  494. case 0x03:
  495. printf("This DIMM is 3.3 Volt/TTL.\n");
  496. break;
  497. case 0x04:
  498. printf("This DIMM is 2.5 Volt.\n");
  499. break;
  500. default:
  501. printf("This DIMM is an unknown "
  502. "voltage.\n");
  503. break;
  504. }
  505. printf("Replace it with a 1.8V DDR2 DIMM.\n\n");
  506. spd_ddr_init_hang();
  507. }
  508. }
  509. }
  510. }
  511. static void program_ddr0_03(unsigned long dimm_ranks[],
  512. unsigned char const iic0_dimm_addr[],
  513. unsigned long num_dimm_banks,
  514. unsigned long sdram_freq,
  515. unsigned long rows, unsigned long *cas_latency)
  516. {
  517. unsigned long dimm_num;
  518. unsigned long cas_index;
  519. unsigned long cycle_2_0_clk;
  520. unsigned long cycle_3_0_clk;
  521. unsigned long cycle_4_0_clk;
  522. unsigned long cycle_5_0_clk;
  523. unsigned long max_2_0_tcyc_ps = 100;
  524. unsigned long max_3_0_tcyc_ps = 100;
  525. unsigned long max_4_0_tcyc_ps = 100;
  526. unsigned long max_5_0_tcyc_ps = 100;
  527. unsigned char cas_available = 0x3C; /* value for DDR2 */
  528. u32 ddr0_03 = DDR0_03_BSTLEN_ENCODE(0x2) | DDR0_03_INITAREF_ENCODE(0x2);
  529. unsigned int const tcyc_addr[3] = { 9, 23, 25 };
  530. /*------------------------------------------------------------------
  531. * Get the board configuration info.
  532. *-----------------------------------------------------------------*/
  533. debug("sdram_freq = %d\n", sdram_freq);
  534. /*------------------------------------------------------------------
  535. * Handle the timing. We need to find the worst case timing of all
  536. * the dimm modules installed.
  537. *-----------------------------------------------------------------*/
  538. /* loop through all the DIMM slots on the board */
  539. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  540. /* If a dimm is installed in a particular slot ... */
  541. if (dimm_ranks[dimm_num]) {
  542. unsigned char const cas_bit =
  543. spd_read(iic0_dimm_addr[dimm_num], 18);
  544. unsigned char cas_mask;
  545. cas_available &= cas_bit;
  546. for (cas_mask = 0x80; cas_mask; cas_mask >>= 1) {
  547. if (cas_bit & cas_mask)
  548. break;
  549. }
  550. debug("cas_bit (SPD byte 18) = %02X, cas_mask = %02X\n",
  551. cas_bit, cas_mask);
  552. for (cas_index = 0; cas_index < 3;
  553. cas_mask >>= 1, cas_index++) {
  554. unsigned long cycle_time_ps;
  555. if (!(cas_available & cas_mask)) {
  556. continue;
  557. }
  558. cycle_time_ps =
  559. get_tcyc(spd_read(iic0_dimm_addr[dimm_num],
  560. tcyc_addr[cas_index]));
  561. debug("cas_index = %d: cycle_time_ps = %d\n",
  562. cas_index, cycle_time_ps);
  563. /*
  564. * DDR2 devices use the following bitmask for CAS latency:
  565. * Bit 7 6 5 4 3 2 1 0
  566. * TBD 6.0 5.0 4.0 3.0 2.0 TBD TBD
  567. */
  568. switch (cas_mask) {
  569. case 0x20:
  570. max_5_0_tcyc_ps =
  571. max(max_5_0_tcyc_ps, cycle_time_ps);
  572. break;
  573. case 0x10:
  574. max_4_0_tcyc_ps =
  575. max(max_4_0_tcyc_ps, cycle_time_ps);
  576. break;
  577. case 0x08:
  578. max_3_0_tcyc_ps =
  579. max(max_3_0_tcyc_ps, cycle_time_ps);
  580. break;
  581. case 0x04:
  582. max_2_0_tcyc_ps =
  583. max(max_2_0_tcyc_ps, cycle_time_ps);
  584. break;
  585. }
  586. }
  587. }
  588. }
  589. debug("cas_available (bit map) = 0x%02X\n", cas_available);
  590. /*------------------------------------------------------------------
  591. * Set the SDRAM mode, SDRAM_MMODE
  592. *-----------------------------------------------------------------*/
  593. /* add 10 here because of rounding problems */
  594. cycle_2_0_clk = MULDIV64(ONE_BILLION, 1000, max_2_0_tcyc_ps) + 10;
  595. cycle_3_0_clk = MULDIV64(ONE_BILLION, 1000, max_3_0_tcyc_ps) + 10;
  596. cycle_4_0_clk = MULDIV64(ONE_BILLION, 1000, max_4_0_tcyc_ps) + 10;
  597. cycle_5_0_clk = MULDIV64(ONE_BILLION, 1000, max_5_0_tcyc_ps) + 10;
  598. debug("cycle_2_0_clk = %d\n", cycle_2_0_clk);
  599. debug("cycle_3_0_clk = %d\n", cycle_3_0_clk);
  600. debug("cycle_4_0_clk = %d\n", cycle_4_0_clk);
  601. debug("cycle_5_0_clk = %d\n", cycle_5_0_clk);
  602. if ((cas_available & 0x04) && (sdram_freq <= cycle_2_0_clk)) {
  603. *cas_latency = 2;
  604. ddr0_03 |= DDR0_03_CASLAT_ENCODE(0x2) |
  605. DDR0_03_CASLAT_LIN_ENCODE(0x4);
  606. } else if ((cas_available & 0x08) && (sdram_freq <= cycle_3_0_clk)) {
  607. *cas_latency = 3;
  608. ddr0_03 |= DDR0_03_CASLAT_ENCODE(0x3) |
  609. DDR0_03_CASLAT_LIN_ENCODE(0x6);
  610. } else if ((cas_available & 0x10) && (sdram_freq <= cycle_4_0_clk)) {
  611. *cas_latency = 4;
  612. ddr0_03 |= DDR0_03_CASLAT_ENCODE(0x4) |
  613. DDR0_03_CASLAT_LIN_ENCODE(0x8);
  614. } else if ((cas_available & 0x20) && (sdram_freq <= cycle_5_0_clk)) {
  615. *cas_latency = 5;
  616. ddr0_03 |= DDR0_03_CASLAT_ENCODE(0x5) |
  617. DDR0_03_CASLAT_LIN_ENCODE(0xA);
  618. } else {
  619. printf("ERROR: Cannot find a supported CAS latency with the "
  620. "installed DIMMs.\n");
  621. printf("Only DDR2 DIMMs with CAS latencies of 2.0, 3.0, 4.0, "
  622. "and 5.0 are supported.\n");
  623. printf("Make sure the PLB speed is within the supported range "
  624. "of the DIMMs.\n");
  625. printf("sdram_freq=%d cycle2=%d cycle3=%d cycle4=%d "
  626. "cycle5=%d\n\n", sdram_freq, cycle_2_0_clk,
  627. cycle_3_0_clk, cycle_4_0_clk, cycle_5_0_clk);
  628. spd_ddr_init_hang();
  629. }
  630. debug("CAS latency = %d\n", *cas_latency);
  631. mtsdram(DDR0_03, ddr0_03);
  632. }
  633. static void program_ddr0_04(unsigned long dimm_ranks[],
  634. unsigned char const iic0_dimm_addr[],
  635. unsigned long num_dimm_banks,
  636. unsigned long sdram_freq)
  637. {
  638. unsigned long dimm_num;
  639. unsigned long t_rc_ps = 0;
  640. unsigned long t_rrd_ps = 0;
  641. unsigned long t_rtp_ps = 0;
  642. unsigned long t_rc_clk;
  643. unsigned long t_rrd_clk;
  644. unsigned long t_rtp_clk;
  645. /*------------------------------------------------------------------
  646. * Handle the timing. We need to find the worst case timing of all
  647. * the dimm modules installed.
  648. *-----------------------------------------------------------------*/
  649. /* loop through all the DIMM slots on the board */
  650. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  651. /* If a dimm is installed in a particular slot ... */
  652. if (dimm_ranks[dimm_num]) {
  653. unsigned long ps;
  654. /* tRC */
  655. ps = 1000 * spd_read(iic0_dimm_addr[dimm_num], 41);
  656. switch (spd_read(iic0_dimm_addr[dimm_num], 40) >> 4) {
  657. case 0x1:
  658. ps += 250;
  659. break;
  660. case 0x2:
  661. ps += 333;
  662. break;
  663. case 0x3:
  664. ps += 500;
  665. break;
  666. case 0x4:
  667. ps += 667;
  668. break;
  669. case 0x5:
  670. ps += 750;
  671. break;
  672. }
  673. t_rc_ps = max(t_rc_ps, ps);
  674. /* tRRD */
  675. ps = 250 * spd_read(iic0_dimm_addr[dimm_num], 28);
  676. t_rrd_ps = max(t_rrd_ps, ps);
  677. /* tRTP */
  678. ps = 250 * spd_read(iic0_dimm_addr[dimm_num], 38);
  679. t_rtp_ps = max(t_rtp_ps, ps);
  680. }
  681. }
  682. debug("t_rc_ps = %d\n", t_rc_ps);
  683. t_rc_clk = (MULDIV64(sdram_freq, t_rc_ps, ONE_BILLION) + 999) / 1000;
  684. debug("t_rrd_ps = %d\n", t_rrd_ps);
  685. t_rrd_clk = (MULDIV64(sdram_freq, t_rrd_ps, ONE_BILLION) + 999) / 1000;
  686. debug("t_rtp_ps = %d\n", t_rtp_ps);
  687. t_rtp_clk = (MULDIV64(sdram_freq, t_rtp_ps, ONE_BILLION) + 999) / 1000;
  688. mtsdram(DDR0_04, DDR0_04_TRC_ENCODE(t_rc_clk) |
  689. DDR0_04_TRRD_ENCODE(t_rrd_clk) |
  690. DDR0_04_TRTP_ENCODE(t_rtp_clk));
  691. }
  692. static void program_ddr0_05(unsigned long dimm_ranks[],
  693. unsigned char const iic0_dimm_addr[],
  694. unsigned long num_dimm_banks,
  695. unsigned long sdram_freq)
  696. {
  697. unsigned long dimm_num;
  698. unsigned long t_rp_ps = 0;
  699. unsigned long t_ras_ps = 0;
  700. unsigned long t_rp_clk;
  701. unsigned long t_ras_clk;
  702. u32 ddr0_05 = DDR0_05_TMRD_ENCODE(0x2) | DDR0_05_TEMRS_ENCODE(0x2);
  703. /*------------------------------------------------------------------
  704. * Handle the timing. We need to find the worst case timing of all
  705. * the dimm modules installed.
  706. *-----------------------------------------------------------------*/
  707. /* loop through all the DIMM slots on the board */
  708. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  709. /* If a dimm is installed in a particular slot ... */
  710. if (dimm_ranks[dimm_num]) {
  711. unsigned long ps;
  712. /* tRP */
  713. ps = 250 * spd_read(iic0_dimm_addr[dimm_num], 27);
  714. t_rp_ps = max(t_rp_ps, ps);
  715. /* tRAS */
  716. ps = 1000 * spd_read(iic0_dimm_addr[dimm_num], 30);
  717. t_ras_ps = max(t_ras_ps, ps);
  718. }
  719. }
  720. debug("t_rp_ps = %d\n", t_rp_ps);
  721. t_rp_clk = (MULDIV64(sdram_freq, t_rp_ps, ONE_BILLION) + 999) / 1000;
  722. debug("t_ras_ps = %d\n", t_ras_ps);
  723. t_ras_clk = (MULDIV64(sdram_freq, t_ras_ps, ONE_BILLION) + 999) / 1000;
  724. mtsdram(DDR0_05, ddr0_05 | DDR0_05_TRP_ENCODE(t_rp_clk) |
  725. DDR0_05_TRAS_MIN_ENCODE(t_ras_clk));
  726. }
  727. static void program_ddr0_06(unsigned long dimm_ranks[],
  728. unsigned char const iic0_dimm_addr[],
  729. unsigned long num_dimm_banks,
  730. unsigned long sdram_freq)
  731. {
  732. unsigned long dimm_num;
  733. unsigned char spd_40;
  734. unsigned long t_wtr_ps = 0;
  735. unsigned long t_rfc_ps = 0;
  736. unsigned long t_wtr_clk;
  737. unsigned long t_rfc_clk;
  738. u32 ddr0_06 =
  739. DDR0_06_WRITEINTERP_ENCODE(0x1) | DDR0_06_TDLL_ENCODE(200);
  740. /*------------------------------------------------------------------
  741. * Handle the timing. We need to find the worst case timing of all
  742. * the dimm modules installed.
  743. *-----------------------------------------------------------------*/
  744. /* loop through all the DIMM slots on the board */
  745. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  746. /* If a dimm is installed in a particular slot ... */
  747. if (dimm_ranks[dimm_num]) {
  748. unsigned long ps;
  749. /* tWTR */
  750. ps = 250 * spd_read(iic0_dimm_addr[dimm_num], 37);
  751. t_wtr_ps = max(t_wtr_ps, ps);
  752. /* tRFC */
  753. ps = 1000 * spd_read(iic0_dimm_addr[dimm_num], 42);
  754. spd_40 = spd_read(iic0_dimm_addr[dimm_num], 40);
  755. ps += 256000 * (spd_40 & 0x01);
  756. switch ((spd_40 & 0x0E) >> 1) {
  757. case 0x1:
  758. ps += 250;
  759. break;
  760. case 0x2:
  761. ps += 333;
  762. break;
  763. case 0x3:
  764. ps += 500;
  765. break;
  766. case 0x4:
  767. ps += 667;
  768. break;
  769. case 0x5:
  770. ps += 750;
  771. break;
  772. }
  773. t_rfc_ps = max(t_rfc_ps, ps);
  774. }
  775. }
  776. debug("t_wtr_ps = %d\n", t_wtr_ps);
  777. t_wtr_clk = (MULDIV64(sdram_freq, t_wtr_ps, ONE_BILLION) + 999) / 1000;
  778. debug("t_rfc_ps = %d\n", t_rfc_ps);
  779. t_rfc_clk = (MULDIV64(sdram_freq, t_rfc_ps, ONE_BILLION) + 999) / 1000;
  780. mtsdram(DDR0_06, ddr0_06 | DDR0_06_TWTR_ENCODE(t_wtr_clk) |
  781. DDR0_06_TRFC_ENCODE(t_rfc_clk));
  782. }
  783. static void program_ddr0_10(unsigned long dimm_ranks[], unsigned long ranks)
  784. {
  785. unsigned long csmap;
  786. if (2 == ranks) {
  787. /* Both chip selects in use */
  788. csmap = 0x03;
  789. } else {
  790. /* One chip select in use */
  791. csmap = (1 == dimm_ranks[0]) ? 0x1 : 0x2;
  792. }
  793. mtsdram(DDR0_10, DDR0_10_WRITE_MODEREG_ENCODE(0x0) |
  794. DDR0_10_CS_MAP_ENCODE(csmap) |
  795. DDR0_10_OCD_ADJUST_PUP_CS_0_ENCODE(0));
  796. }
  797. static void program_ddr0_11(unsigned long sdram_freq)
  798. {
  799. unsigned long const t_xsnr_ps = 200000; /* 200 ns */
  800. unsigned long t_xsnr_clk;
  801. debug("t_xsnr_ps = %d\n", t_xsnr_ps);
  802. t_xsnr_clk =
  803. (MULDIV64(sdram_freq, t_xsnr_ps, ONE_BILLION) + 999) / 1000;
  804. mtsdram(DDR0_11, DDR0_11_SREFRESH_ENCODE(0) |
  805. DDR0_11_TXSNR_ENCODE(t_xsnr_clk) | DDR0_11_TXSR_ENCODE(200));
  806. }
  807. static void program_ddr0_22(unsigned long dimm_ranks[],
  808. unsigned char const iic0_dimm_addr[],
  809. unsigned long num_dimm_banks, unsigned long width)
  810. {
  811. #if defined(CONFIG_DDR_ECC)
  812. unsigned long dimm_num;
  813. unsigned long ecc_available = width >= 64;
  814. u32 ddr0_22 = DDR0_22_DQS_OUT_SHIFT_BYPASS_ENCODE(0x26) |
  815. DDR0_22_DQS_OUT_SHIFT_ENCODE(DQS_OUT_SHIFT) |
  816. DDR0_22_DLL_DQS_BYPASS_8_ENCODE(DLL_DQS_BYPASS);
  817. /* loop through all the DIMM slots on the board */
  818. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  819. /* If a dimm is installed in a particular slot ... */
  820. if (dimm_ranks[dimm_num]) {
  821. /* Check for ECC */
  822. if (0 == (spd_read(iic0_dimm_addr[dimm_num], 11) &
  823. 0x02)) {
  824. ecc_available = FALSE;
  825. }
  826. }
  827. }
  828. if (ecc_available) {
  829. debug("ECC found on all DIMMs present\n");
  830. mtsdram(DDR0_22, ddr0_22 | DDR0_22_CTRL_RAW_ENCODE(0x3));
  831. } else {
  832. debug("ECC not found on some or all DIMMs present\n");
  833. mtsdram(DDR0_22, ddr0_22 | DDR0_22_CTRL_RAW_ENCODE(0x0));
  834. }
  835. #else
  836. mtsdram(DDR0_22, DDR0_22_CTRL_RAW_ENCODE(0x0) |
  837. DDR0_22_DQS_OUT_SHIFT_BYPASS_ENCODE(0x26) |
  838. DDR0_22_DQS_OUT_SHIFT_ENCODE(DQS_OUT_SHIFT) |
  839. DDR0_22_DLL_DQS_BYPASS_8_ENCODE(DLL_DQS_BYPASS));
  840. #endif /* defined(CONFIG_DDR_ECC) */
  841. }
  842. static void program_ddr0_24(unsigned long ranks)
  843. {
  844. u32 ddr0_24 = DDR0_24_RTT_PAD_TERMINATION_ENCODE(0x1) | /* 75 ohm */
  845. DDR0_24_ODT_RD_MAP_CS1_ENCODE(0x0);
  846. if (2 == ranks) {
  847. /* Both chip selects in use */
  848. ddr0_24 |= DDR0_24_ODT_WR_MAP_CS1_ENCODE(0x1) |
  849. DDR0_24_ODT_WR_MAP_CS0_ENCODE(0x2);
  850. } else {
  851. /* One chip select in use */
  852. /* One of the two fields added to ddr0_24 is a "don't care" */
  853. ddr0_24 |= DDR0_24_ODT_WR_MAP_CS1_ENCODE(0x2) |
  854. DDR0_24_ODT_WR_MAP_CS0_ENCODE(0x1);
  855. }
  856. mtsdram(DDR0_24, ddr0_24);
  857. }
  858. static void program_ddr0_26(unsigned long sdram_freq)
  859. {
  860. unsigned long const t_ref_ps = 7800000; /* 7.8 us. refresh */
  861. /* TODO: check definition of tRAS_MAX */
  862. unsigned long const t_ras_max_ps = 9 * t_ref_ps;
  863. unsigned long t_ras_max_clk;
  864. unsigned long t_ref_clk;
  865. /* Round down t_ras_max_clk and t_ref_clk */
  866. debug("t_ras_max_ps = %d\n", t_ras_max_ps);
  867. t_ras_max_clk = MULDIV64(sdram_freq, t_ras_max_ps, ONE_BILLION) / 1000;
  868. debug("t_ref_ps = %d\n", t_ref_ps);
  869. t_ref_clk = MULDIV64(sdram_freq, t_ref_ps, ONE_BILLION) / 1000;
  870. mtsdram(DDR0_26, DDR0_26_TRAS_MAX_ENCODE(t_ras_max_clk) |
  871. DDR0_26_TREF_ENCODE(t_ref_clk));
  872. }
  873. static void program_ddr0_27(unsigned long sdram_freq)
  874. {
  875. unsigned long const t_init_ps = 200000000; /* 200 us. init */
  876. unsigned long t_init_clk;
  877. debug("t_init_ps = %d\n", t_init_ps);
  878. t_init_clk =
  879. (MULDIV64(sdram_freq, t_init_ps, ONE_BILLION) + 999) / 1000;
  880. mtsdram(DDR0_27, DDR0_27_EMRS_DATA_ENCODE(0x0000) |
  881. DDR0_27_TINIT_ENCODE(t_init_clk));
  882. }
  883. static void program_ddr0_43(unsigned long dimm_ranks[],
  884. unsigned char const iic0_dimm_addr[],
  885. unsigned long num_dimm_banks,
  886. unsigned long sdram_freq,
  887. unsigned long cols, unsigned long banks)
  888. {
  889. unsigned long dimm_num;
  890. unsigned long t_wr_ps = 0;
  891. unsigned long t_wr_clk;
  892. u32 ddr0_43 = DDR0_43_APREBIT_ENCODE(10) |
  893. DDR0_43_COLUMN_SIZE_ENCODE(12 - cols) |
  894. DDR0_43_EIGHT_BANK_MODE_ENCODE(8 == banks ? 1 : 0);
  895. /*------------------------------------------------------------------
  896. * Handle the timing. We need to find the worst case timing of all
  897. * the dimm modules installed.
  898. *-----------------------------------------------------------------*/
  899. /* loop through all the DIMM slots on the board */
  900. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  901. /* If a dimm is installed in a particular slot ... */
  902. if (dimm_ranks[dimm_num]) {
  903. unsigned long ps;
  904. ps = 250 * spd_read(iic0_dimm_addr[dimm_num], 36);
  905. t_wr_ps = max(t_wr_ps, ps);
  906. }
  907. }
  908. debug("t_wr_ps = %d\n", t_wr_ps);
  909. t_wr_clk = (MULDIV64(sdram_freq, t_wr_ps, ONE_BILLION) + 999) / 1000;
  910. mtsdram(DDR0_43, ddr0_43 | DDR0_43_TWR_ENCODE(t_wr_clk));
  911. }
  912. static void program_ddr0_44(unsigned long dimm_ranks[],
  913. unsigned char const iic0_dimm_addr[],
  914. unsigned long num_dimm_banks,
  915. unsigned long sdram_freq)
  916. {
  917. unsigned long dimm_num;
  918. unsigned long t_rcd_ps = 0;
  919. unsigned long t_rcd_clk;
  920. /*------------------------------------------------------------------
  921. * Handle the timing. We need to find the worst case timing of all
  922. * the dimm modules installed.
  923. *-----------------------------------------------------------------*/
  924. /* loop through all the DIMM slots on the board */
  925. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  926. /* If a dimm is installed in a particular slot ... */
  927. if (dimm_ranks[dimm_num]) {
  928. unsigned long ps;
  929. ps = 250 * spd_read(iic0_dimm_addr[dimm_num], 29);
  930. t_rcd_ps = max(t_rcd_ps, ps);
  931. }
  932. }
  933. debug("t_rcd_ps = %d\n", t_rcd_ps);
  934. t_rcd_clk = (MULDIV64(sdram_freq, t_rcd_ps, ONE_BILLION) + 999) / 1000;
  935. mtsdram(DDR0_44, DDR0_44_TRCD_ENCODE(t_rcd_clk));
  936. }
  937. /*-----------------------------------------------------------------------------+
  938. * initdram. Initializes the 440EPx/GPx DDR SDRAM controller.
  939. * Note: This routine runs from flash with a stack set up in the chip's
  940. * sram space. It is important that the routine does not require .sbss, .bss or
  941. * .data sections. It also cannot call routines that require these sections.
  942. *-----------------------------------------------------------------------------*/
  943. /*-----------------------------------------------------------------------------
  944. * Function: initdram
  945. * Description: Configures SDRAM memory banks for DDR operation.
  946. * Auto Memory Configuration option reads the DDR SDRAM EEPROMs
  947. * via the IIC bus and then configures the DDR SDRAM memory
  948. * banks appropriately. If Auto Memory Configuration is
  949. * not used, it is assumed that no DIMM is plugged
  950. *-----------------------------------------------------------------------------*/
  951. long int initdram(int board_type)
  952. {
  953. unsigned char const iic0_dimm_addr[] = SPD_EEPROM_ADDRESS;
  954. unsigned long dimm_ranks[MAXDIMMS];
  955. unsigned long ranks;
  956. unsigned long rows;
  957. unsigned long banks;
  958. unsigned long cols;
  959. unsigned long width;
  960. unsigned long const sdram_freq = get_bus_freq(0);
  961. unsigned long const num_dimm_banks = sizeof(iic0_dimm_addr); /* on board dimm banks */
  962. unsigned long cas_latency = 0; /* to quiet initialization warning */
  963. unsigned long dram_size;
  964. debug("\nEntering initdram()\n");
  965. /*------------------------------------------------------------------
  966. * Stop the DDR-SDRAM controller.
  967. *-----------------------------------------------------------------*/
  968. mtsdram(DDR0_02, DDR0_02_START_ENCODE(0));
  969. /*
  970. * Make sure I2C controller is initialized
  971. * before continuing.
  972. */
  973. /* switch to correct I2C bus */
  974. I2C_SET_BUS(CFG_SPD_BUS_NUM);
  975. i2c_init(CFG_I2C_SPEED, CFG_I2C_SLAVE);
  976. /*------------------------------------------------------------------
  977. * Clear out the serial presence detect buffers.
  978. * Perform IIC reads from the dimm. Fill in the spds.
  979. * Check to see if the dimm slots are populated
  980. *-----------------------------------------------------------------*/
  981. get_spd_info(dimm_ranks, &ranks, iic0_dimm_addr, num_dimm_banks);
  982. /*------------------------------------------------------------------
  983. * Check the frequency supported for the dimms plugged.
  984. *-----------------------------------------------------------------*/
  985. check_frequency(dimm_ranks, iic0_dimm_addr, num_dimm_banks, sdram_freq);
  986. /*------------------------------------------------------------------
  987. * Check and get size information.
  988. *-----------------------------------------------------------------*/
  989. get_dimm_size(dimm_ranks, iic0_dimm_addr, num_dimm_banks, &rows, &banks,
  990. &cols, &width);
  991. /*------------------------------------------------------------------
  992. * Check the voltage type for the dimms plugged.
  993. *-----------------------------------------------------------------*/
  994. check_voltage_type(dimm_ranks, iic0_dimm_addr, num_dimm_banks);
  995. /*------------------------------------------------------------------
  996. * Program registers for SDRAM controller.
  997. *-----------------------------------------------------------------*/
  998. mtsdram(DDR0_00, DDR0_00_DLL_INCREMENT_ENCODE(0x19) |
  999. DDR0_00_DLL_START_POINT_DECODE(0x0A));
  1000. mtsdram(DDR0_01, DDR0_01_PLB0_DB_CS_LOWER_ENCODE(0x01) |
  1001. DDR0_01_PLB0_DB_CS_UPPER_ENCODE(0x00) |
  1002. DDR0_01_INT_MASK_ENCODE(0xFF));
  1003. program_ddr0_03(dimm_ranks, iic0_dimm_addr, num_dimm_banks, sdram_freq,
  1004. rows, &cas_latency);
  1005. program_ddr0_04(dimm_ranks, iic0_dimm_addr, num_dimm_banks, sdram_freq);
  1006. program_ddr0_05(dimm_ranks, iic0_dimm_addr, num_dimm_banks, sdram_freq);
  1007. program_ddr0_06(dimm_ranks, iic0_dimm_addr, num_dimm_banks, sdram_freq);
  1008. /*------------------------------------------------------------------
  1009. * TODO: tFAW not found in SPD. Value of 13 taken from Sequoia
  1010. * board SDRAM, but may be overly concervate.
  1011. *-----------------------------------------------------------------*/
  1012. mtsdram(DDR0_07, DDR0_07_NO_CMD_INIT_ENCODE(0) |
  1013. DDR0_07_TFAW_ENCODE(13) |
  1014. DDR0_07_AUTO_REFRESH_MODE_ENCODE(1) |
  1015. DDR0_07_AREFRESH_ENCODE(0));
  1016. mtsdram(DDR0_08, DDR0_08_WRLAT_ENCODE(cas_latency - 1) |
  1017. DDR0_08_TCPD_ENCODE(200) | DDR0_08_DQS_N_EN_ENCODE(0) |
  1018. DDR0_08_DDRII_ENCODE(1));
  1019. mtsdram(DDR0_09, DDR0_09_OCD_ADJUST_PDN_CS_0_ENCODE(0x00) |
  1020. DDR0_09_RTT_0_ENCODE(0x1) |
  1021. DDR0_09_WR_DQS_SHIFT_BYPASS_ENCODE(0x1D) |
  1022. DDR0_09_WR_DQS_SHIFT_ENCODE(DQS_OUT_SHIFT - 0x20));
  1023. program_ddr0_10(dimm_ranks, ranks);
  1024. program_ddr0_11(sdram_freq);
  1025. mtsdram(DDR0_12, DDR0_12_TCKE_ENCODE(3));
  1026. mtsdram(DDR0_14, DDR0_14_DLL_BYPASS_MODE_ENCODE(0) |
  1027. DDR0_14_REDUC_ENCODE(width <= 40 ? 1 : 0) |
  1028. DDR0_14_REG_DIMM_ENABLE_ENCODE(0));
  1029. mtsdram(DDR0_17, DDR0_17_DLL_DQS_DELAY_0_ENCODE(DLL_DQS_DELAY));
  1030. mtsdram(DDR0_18, DDR0_18_DLL_DQS_DELAY_4_ENCODE(DLL_DQS_DELAY) |
  1031. DDR0_18_DLL_DQS_DELAY_3_ENCODE(DLL_DQS_DELAY) |
  1032. DDR0_18_DLL_DQS_DELAY_2_ENCODE(DLL_DQS_DELAY) |
  1033. DDR0_18_DLL_DQS_DELAY_1_ENCODE(DLL_DQS_DELAY));
  1034. mtsdram(DDR0_19, DDR0_19_DLL_DQS_DELAY_8_ENCODE(DLL_DQS_DELAY) |
  1035. DDR0_19_DLL_DQS_DELAY_7_ENCODE(DLL_DQS_DELAY) |
  1036. DDR0_19_DLL_DQS_DELAY_6_ENCODE(DLL_DQS_DELAY) |
  1037. DDR0_19_DLL_DQS_DELAY_5_ENCODE(DLL_DQS_DELAY));
  1038. mtsdram(DDR0_20, DDR0_20_DLL_DQS_BYPASS_3_ENCODE(DLL_DQS_BYPASS) |
  1039. DDR0_20_DLL_DQS_BYPASS_2_ENCODE(DLL_DQS_BYPASS) |
  1040. DDR0_20_DLL_DQS_BYPASS_1_ENCODE(DLL_DQS_BYPASS) |
  1041. DDR0_20_DLL_DQS_BYPASS_0_ENCODE(DLL_DQS_BYPASS));
  1042. mtsdram(DDR0_21, DDR0_21_DLL_DQS_BYPASS_7_ENCODE(DLL_DQS_BYPASS) |
  1043. DDR0_21_DLL_DQS_BYPASS_6_ENCODE(DLL_DQS_BYPASS) |
  1044. DDR0_21_DLL_DQS_BYPASS_5_ENCODE(DLL_DQS_BYPASS) |
  1045. DDR0_21_DLL_DQS_BYPASS_4_ENCODE(DLL_DQS_BYPASS));
  1046. program_ddr0_22(dimm_ranks, iic0_dimm_addr, num_dimm_banks, width);
  1047. mtsdram(DDR0_23, DDR0_23_ODT_RD_MAP_CS0_ENCODE(0x0) |
  1048. DDR0_23_FWC_ENCODE(0));
  1049. program_ddr0_24(ranks);
  1050. program_ddr0_26(sdram_freq);
  1051. program_ddr0_27(sdram_freq);
  1052. mtsdram(DDR0_28, DDR0_28_EMRS3_DATA_ENCODE(0x0000) |
  1053. DDR0_28_EMRS2_DATA_ENCODE(0x0000));
  1054. mtsdram(DDR0_31, DDR0_31_XOR_CHECK_BITS_ENCODE(0x0000));
  1055. mtsdram(DDR0_42, DDR0_42_ADDR_PINS_DECODE(14 - rows) |
  1056. DDR0_42_CASLAT_LIN_GATE_ENCODE(2 * cas_latency));
  1057. program_ddr0_43(dimm_ranks, iic0_dimm_addr, num_dimm_banks, sdram_freq,
  1058. cols, banks);
  1059. program_ddr0_44(dimm_ranks, iic0_dimm_addr, num_dimm_banks, sdram_freq);
  1060. denali_sdram_register_dump();
  1061. dram_size = (width >= 64) ? 8 : 4;
  1062. dram_size *= 1 << cols;
  1063. dram_size *= banks;
  1064. dram_size *= 1 << rows;
  1065. dram_size *= ranks;
  1066. debug("dram_size = %lu\n", dram_size);
  1067. /* Start the SDRAM controler */
  1068. mtsdram(DDR0_02, DDR0_02_START_ENCODE(1));
  1069. denali_wait_for_dlllock();
  1070. #if defined(CONFIG_DDR_DATA_EYE)
  1071. /* -----------------------------------------------------------+
  1072. * Perform data eye search if requested.
  1073. * ----------------------------------------------------------*/
  1074. program_tlb(0, CFG_SDRAM_BASE, dram_size, TLB_WORD2_I_ENABLE);
  1075. denali_core_search_data_eye();
  1076. denali_sdram_register_dump();
  1077. remove_tlb(CFG_SDRAM_BASE, dram_size);
  1078. #endif
  1079. #if defined(CONFIG_ZERO_SDRAM) || defined(CONFIG_DDR_ECC)
  1080. program_tlb(0, CFG_SDRAM_BASE, dram_size, 0);
  1081. sync();
  1082. eieio();
  1083. /* Zero the memory */
  1084. debug("Zeroing SDRAM...");
  1085. dcbz_area(CFG_SDRAM_BASE, dram_size);
  1086. dflush();
  1087. debug("Completed\n");
  1088. sync();
  1089. eieio();
  1090. remove_tlb(CFG_SDRAM_BASE, dram_size);
  1091. #if defined(CONFIG_DDR_ECC)
  1092. /*
  1093. * If ECC is enabled, clear and enable interrupts
  1094. */
  1095. if (is_ecc_enabled()) {
  1096. u32 val;
  1097. sync();
  1098. eieio();
  1099. /* Clear error status */
  1100. mfsdram(DDR0_00, val);
  1101. mtsdram(DDR0_00, val | DDR0_00_INT_ACK_ALL);
  1102. /* Set 'int_mask' parameter to functionnal value */
  1103. mfsdram(DDR0_01, val);
  1104. mtsdram(DDR0_01, (val & ~DDR0_01_INT_MASK_MASK) |
  1105. DDR0_01_INT_MASK_ALL_OFF);
  1106. #if defined(CONFIG_DDR_DATA_EYE)
  1107. /*
  1108. * Running denali_core_search_data_eye() when ECC is enabled
  1109. * causes non-ECC machine checks. This clears them.
  1110. */
  1111. print_mcsr();
  1112. mtspr(SPRN_MCSR, mfspr(SPRN_MCSR));
  1113. print_mcsr();
  1114. #endif
  1115. sync();
  1116. eieio();
  1117. }
  1118. #endif /* defined(CONFIG_DDR_ECC) */
  1119. #endif /* defined(CONFIG_ZERO_SDRAM) || defined(CONFIG_DDR_ECC) */
  1120. program_tlb(0, CFG_SDRAM_BASE, dram_size, MY_TLB_WORD2_I_ENABLE);
  1121. return dram_size;
  1122. }
  1123. void board_add_ram_info(int use_default)
  1124. {
  1125. u32 val;
  1126. printf(" (ECC");
  1127. if (!is_ecc_enabled()) {
  1128. printf(" not");
  1129. }
  1130. printf(" enabled, %d MHz", (2 * get_bus_freq(0)) / 1000000);
  1131. mfsdram(DDR0_03, val);
  1132. printf(", CL%d)", DDR0_03_CASLAT_LIN_DECODE(val) >> 1);
  1133. }
  1134. #endif /* CONFIG_SPD_EEPROM */