denali_spd_ddr2.c 40 KB

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