denali_spd_ddr2.c 40 KB

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