denali_spd_ddr2.c 40 KB

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