2 * This file is part of the coreboot project.
4 * Copyright (C) 2009 One Laptop per Child, Association, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 void DRAMSetVRNum(DRAM_SYS_ATTR *DramAttr, u8 PhyRank /* physical rank */,
21 u8 VirRank /* virtual rank */, BOOLEAN Enable);
22 void SetEndingAddr(DRAM_SYS_ATTR *DramAttr, u8 VirRank /* Ending address
23 register number indicator (INDEX */, INT8 Value /* (value)
24 add or subtract value to this and after banks. */);
25 void InitDDR2CHA(DRAM_SYS_ATTR *DramAttr);
26 void InitDDR2CHB(DRAM_SYS_ATTR *DramAttr);
27 void InitDDR2CHC(DRAM_SYS_ATTR *DramAttr);
29 CB_STATUS VerifyChc(void);
31 /*===================================================================
32 Function : DRAMRegInitValue()
35 DramAttr: pointer point to DRAM_SYS_ATTR which consist the DDR and Dimm information
38 Purpose : Set necessary register before DRAM initialize
39 ===================================================================*/
41 static const u8 DramRegTbl[][3] = {
43 {0x50, 0x11, 0xEE}, // DDR default MA7 for DRAM init
44 {0x51, 0x11, 0x60}, // DDR default MA3 for CHB init
45 {0x52, 0x00, 0x33}, // DDR use BA0=M17, BA1=M18,
46 {0x53, 0x00, 0x3F}, // DDR BA2=M19
48 {0x54, 0x00, 0x00}, // default PR0=VR0; PR1=VR1
49 {0x55, 0x00, 0x00}, // default PR2=VR2; PR3=VR3
50 {0x56, 0x00, 0x00}, // default PR4=VR4; PR5=VR5
51 {0x57, 0x00, 0x00}, // default PR4=VR4; PR5=VR5
53 {0x60, 0x00, 0x00}, // disable fast turn-around
54 {0x65, 0x00, 0xD9}, // AGP timer = 0XD; Host timer = 8;
55 {0x66, 0x00, 0x88}, // DRAMC Queue Size = 4; park at the last bus
56 // owner,Priority promotion timer = 8
58 {0x69, 0xF0, 0x04}, // set RX69[3:0]=0000b
59 {0x6A, 0x00, 0x00}, // refresh counter
60 {0x6E, 0xF8, 0x80}, // must set 6E[7], or else DDR2 probe test
63 * In here, we not set RX70~RX74, because we just init DRAM but no
64 * need R/W DRAM, when we check DQS input/output delay, then we need
68 // {0x79, 0x00, 0x8F },
70 // {0x90, 0x87, 0x78 },
71 // {0x91, 0x00, 0x46 },
77 void DRAMRegInitValue(DRAM_SYS_ATTR *DramAttr)
81 for (Idx = 0; DramRegTbl[Idx][0] != 0; Idx++) {
82 Data = pci_read_config8(MEMCTRL, DramRegTbl[Idx][0]);
83 Data &= DramRegTbl[Idx][1];
84 Data |= DramRegTbl[Idx][2];
85 pci_write_config8(MEMCTRL, DramRegTbl[Idx][0], Data);
89 pci_write_config8(PCI_DEV(0, 0, 4), 0xa3, Data);
91 // Set DRAM controller mode. */
92 Data = pci_read_config8(MEMCTRL, 0x6c);
94 if (ENABLE_CHC == 0) {
95 Data |= 0x4; /* Only CHA 64 bit mode */
96 pci_write_config8(MEMCTRL, 0x6c, Data);
98 Data |= 0x0; /* CHA + CHC */
99 pci_write_config8(MEMCTRL, 0x6c, Data);
102 // pci_write_config8(MEMCTRL, 0xb1, Data);
104 // set CHB DQSB input delay, or else will meet error which
105 // is some byte is right but another bit is error.
106 Data = pci_read_config8(MEMCTRL, 0xff);
107 Data = (Data & 0x03) | 0x3D;
108 pci_write_config8(MEMCTRL, 0xff, Data);
110 // enable CHC RXDB[7]
111 // Data = pci_read_config8(MEMCTRL, 0xdb);
112 // Data = (Data & 0x7F) | 0x80;
113 // pci_write_config8(MEMCTRL, 0xdb, Data);
115 // rx62[2:0], CHA and CHB CL
116 Data = pci_read_config8(MEMCTRL, 0x62);
119 // If CL = 6, so I set CHB CL = 5 default.
123 /* Set CHC Read CL rxDC[6:7]. */
124 Data = pci_read_config8(MEMCTRL, 0xdc);
125 Data = (Data & 0x3F) | (CL << 6);
126 pci_write_config8(MEMCTRL, 0xdc, Data);
128 /* Set CHC write CL rxDF[6:7]. */
129 Data = pci_read_config8(MEMCTRL, 0xdf);
130 Data = (Data & 0x3F) | (CL << 6);
131 pci_write_config8(MEMCTRL, 0xdf, Data);
133 /* Set CHC ODT RxDC[5:0] */
134 Data = pci_read_config8(MEMCTRL, 0xdc);
135 Data = (Data & 0xC0) | 0x03;
136 pci_write_config8(MEMCTRL, 0xdc, Data);
138 /* Set column type RXDD[6] and enable ODT PAD RXDD[7]. */
139 Data = pci_read_config8(MEMCTRL, 0xdd);
141 Idx = DramAttr->DimmInfo[2].SPDDataBuf[SPD_SDRAM_COL_ADDR];
142 if ((Idx & 0x0F) == 10)
143 Data |= 0x40; /* MA9~MA0 */
145 Data &= 0xBF; /* MA8~MA0 */
146 pci_write_config8(MEMCTRL, 0xdd, Data);
149 // Disable Read DRAM fast ready ;Rx51[7]
150 // Disable Read Around Write ;Rx51[6]
152 // Disable Consecutive Read ;RX52[1:0]
153 // Disable Speculative Read
156 /*===================================================================
157 Function : DRAMInitializeProc()
160 DramAttr: pointer point to DRAM_SYS_ATTR which consist the DDR and Dimm information
163 Purpose : DRAM initialize according to the bios porting guid
164 ===================================================================*/
166 #define EXIST_TEST_PATTERN 0x55555555
167 #define NOT_EXIST_TEST_PATTERN 0xAAAAAAAA
169 static BOOLEAN ChkForExistLowBank(void)
171 u32 *Address, data32;
175 *Address = EXIST_TEST_PATTERN;
177 *Address = EXIST_TEST_PATTERN;
183 if (data32 != EXIST_TEST_PATTERN)
187 if (data32 != EXIST_TEST_PATTERN)
190 /* Check not Pattern */
192 *Address = NOT_EXIST_TEST_PATTERN;
194 *Address = NOT_EXIST_TEST_PATTERN;
200 if (data32 != (u32) (NOT_EXIST_TEST_PATTERN))
204 if (data32 != (u32) (NOT_EXIST_TEST_PATTERN))
210 void DRAMInitializeProc(DRAM_SYS_ATTR *DramAttr)
216 for (idx = 0; idx < MAX_RANKS; idx++) {
217 if ((DramAttr->RankPresentMap & shift) != 0) {
219 * Set VR# to physical rank indicated = PR + physical
222 DRAMSetVRNum(DramAttr, idx, idx, TRUE);
223 SetEndingAddr(DramAttr, idx, 0x10); /* Assume 1G size */
224 if (idx < 4) /* CHA init */
225 InitDDR2CHA(DramAttr); // temp wjb 2007/1 only for compiling
226 // in the function InitDDR2,the parameter is no need
227 Status = ChkForExistLowBank();
228 if (Status == TRUE) {
229 PRINT_DEBUG_MEM(" S\r");
231 PRINT_DEBUG_MEM(" F\r");
235 * Set VR# to physical rank indicated = 00h + physical
238 DRAMSetVRNum(DramAttr, idx, 0, FALSE);
239 SetEndingAddr(DramAttr, idx, -16);
244 InitDDR2CHC(DramAttr);
248 /*===================================================================
249 Function : DRAMSetVRNUM()
252 DramAttr: pointer point to DRAM_SYS_ATTR which consist the DDR and Dimm information
254 PhyRank: Physical Rank number
255 VirRank: Virtual Rank number
256 Enable: Enable/Disable Physical Rank
258 Purpose : Set virtual rank number for physical rank
259 Program the specific physical rank with specific virtual rank number
260 Program when necessary, otherwise don't touch the pr-vr-mapping registers
261 ===================================================================*/
263 void DRAMSetVRNum(DRAM_SYS_ATTR *DramAttr, u8 PhyRank /* physical rank */,
264 u8 VirRank /* virtual rank */, BOOLEAN Enable)
266 u8 Data, AndData, OrData;
268 Data = pci_read_config8(MEMCTRL, (0x54 + (PhyRank >> 1)));
274 if ((PhyRank & 0x01) == 0x00) {
275 AndData = 0x0F; // keep the value of odd rank on PR # is even(keep 1,3,5,7)
276 OrData <<= 4; // VR #, value to be set
278 AndData = 0xF0; // keep the value of even rank on PR # is odd(keep 0,2,4,6)
283 pci_write_config8(MEMCTRL, (0x54 + (PhyRank >> 1)), Data);
286 /*===================================================================
287 Function : SetEndingAddr()
290 DramAttr: pointer point to DRAM_SYS_ATTR which consist the DDR and Dimm information
292 VirRank: Virtual Rank number
293 Value: (value) add or subtract value to this and after banks
295 Purpose : Set ending address of virtual rank specified by VirRank
296 ===================================================================*/
298 void SetEndingAddr(DRAM_SYS_ATTR *DramAttr, u8 VirRank /* ending address
299 register number indicator (INDEX */, INT8 Value /* (value)
300 add or subtract value to this and after banks */) {
303 /* Read register,Rx40-Rx47(0,1,2,3,4,5,6,7) and set ending address. */
304 Data = pci_read_config8(MEMCTRL, 0x40 + VirRank);
305 Data = (u8) (Data + Value);
306 pci_write_config8(MEMCTRL, 0x40 + VirRank, Data);
308 /* Program the virank's begining address to zero. */
310 pci_write_config8(MEMCTRL, 0x48 + VirRank, Data);
313 /*===================================================================
314 Function : InitDDR2()
317 DramAttr: pointer point to DRAM_SYS_ATTR which consist the DDR and Dimm information
320 Purpose : Initialize DDR2 by standard sequence
321 ===================================================================*/
324 static const u32 CHA_MRS_DLL_150[2] = { 0x00020200, 0x00000800 }; // with 150 ohm (A17=1, A9=1), (A11=1)(cpu address)
325 static const u32 CHA_MRS_DLL_75[2] = { 0x00020020, 0x00000800 }; // with 75 ohm (A17=1, A5=1), (A11=1)(cpu address)
328 // { DLL: Enable. A17(BA0)=1 and A3(MA0)=0 }
329 // { DLL: reset. A11(MA8)=1 }
331 // DDR2 CL=2 CL=3 CL=4 CL=5 CL=6(Burst type=interleave)(WR fine tune in code)
332 static const u16 CHA_DDR2_MRS_table[5] = { 0x0150, 0x01D0, 0x0250, 0x02D0, 0x350 }; // BL=4 ;Use 1X-bandwidth MA table to init DRAM
335 #define CHA_MRS_DDR2_TWR2 (0 << 13) + (0 << 20) + (1 << 12) // Value = 001000h
336 #define CHA_MRS_DDR2_TWR3 (0 << 13) + (1 << 20) + (0 << 12) // Value = 100000h
337 #define CHA_MRS_DDR2_TWR4 (0 << 13) + (1 << 20) + (1 << 12) // Value = 101000h
338 #define CHA_MRS_DDR2_TWR5 (1 << 13) + (0 << 20) + (0 << 12) // Value = 002000h
339 #define CHA_MRS_DDR2_TWR6 (1 << 13) + (0 << 20) + (1 << 12) // Value = 003000h
341 // DDR2 Twr=2 Twr=3 Twr=4 Twr=5
342 static const u32 CHA_DDR2_Twr_table[5] = {
343 CHA_MRS_DDR2_TWR2, CHA_MRS_DDR2_TWR3, CHA_MRS_DDR2_TWR4,
344 CHA_MRS_DDR2_TWR5, CHA_MRS_DDR2_TWR6
347 #define CHA_OCD_Exit_150ohm 0x20200 // EMRS(1), BA0=1, MA9=MA8=MA7=0,MA6=1,MA2=0 (DRAM bus address)
348 // A17=1, A12=A11=A10=0,A9=1 ,A5=0 (CPU address)
349 #define CHA_OCD_Default_150ohm 0x21E00 // EMRS(1), BA0=1, MA9=MA8=MA7=1,MA6=1,MA2=0 (DRAM bus address)
350 // A17=1, A12=A11=A10=1,A9=1 ,A5=0 (CPU address)
351 #define CHA_OCD_Exit_75ohm 0x20020 // EMRS(1), BA0=1, MA9=MA8=MA7=0,MA6=0,MA2=1 (DRAM bus address)
352 // A17=1, A12=A11=A10=0,A9=0 ,A5=1 (CPU address)
353 #define CHA_OCD_Default_75ohm 0x21C20 // EMRS(1), BA0=1, MA9=MA8=MA7=1,MA6=0,MA2=1 (DRAM bus address)
354 // A17=1, A12=A11=A10=1,A9=0 ,A5=1 (CPU address)
356 void InitDDR2CHA(DRAM_SYS_ATTR *DramAttr)
358 u8 Data, Reg6BVal, Idx, CL, BL, Twr, DimmNum;
362 /* Disable bank paging and multi page. */
363 Data = pci_read_config8(MEMCTRL, 0x69);
365 pci_write_config8(MEMCTRL, 0x69, Data);
367 Reg6BVal = pci_read_config8(MEMCTRL, 0x6b);
371 /* At least one NOP cycle will be issued after the 1m sec device
374 Data = Reg6BVal | 0x01;
375 pci_write_config8(MEMCTRL, 0x6b, Data);
378 /* Read a double word from any address of the DIMM. */
383 * A minimum pause of 200u sec will be provided after the NOP.
384 * - <<< reduce BOOT UP time >>> -
387 for (Idx = 0; Idx < 0x10; Idx++)
391 // Precharge all (PALL) will be issued to the DDR.
392 Data = Reg6BVal | 0x02;
393 pci_write_config8(MEMCTRL, 0x6b, Data);
396 // Read a double word from any address of the DIMM
400 // MSR Eable will be issued to the DDR
401 Data = Reg6BVal | 0x03;
402 pci_write_config8(MEMCTRL, 0x6b, Data);
406 * Check ODT value for EMRS(1) command according to ODTLookUp_TBL
407 * in driving_setting.c if there is one DIMM in MB's one channel,
408 * the DDR2's ODT is 150ohm if there is two DIMM in MB's one channel,
409 * the DDR2's ODT is 75 ohm.
411 DimmNum = DramAttr->DimmNumChA;
413 if (DimmNum == 1) { /* DDR's ODT is 150ohm */
414 AccessAddr = (u32) CHA_MRS_DLL_150[0];
415 DimmRead(AccessAddr); /* Issue EMRS DLL Enable. */
416 PRINT_DEBUG_MEM("Step 9 Address ");
417 PRINT_DEBUG_MEM_HEX32(AccessAddr);
418 PRINT_DEBUG_MEM("\r");
420 AccessAddr = (u32) CHA_MRS_DLL_150[1];
421 DimmRead(AccessAddr); /* Issue MRS DLL Reset. */
422 PRINT_DEBUG_MEM("Step 10 Address ");
423 PRINT_DEBUG_MEM_HEX32(AccessAddr);
424 PRINT_DEBUG_MEM("\r");
425 } else if (DimmNum == 2) { /* DDR's ODT is 75ohm */
426 AccessAddr = (u32) CHA_MRS_DLL_75[0];
427 DimmRead(AccessAddr); /* Issue EMRS DLL Enable. */
428 AccessAddr = (u32) CHA_MRS_DLL_75[1];
429 DimmRead(AccessAddr); /* Issue MRS DLL Reset. */
431 PRINT_DEBUG_MEM("Dimm NUM ERROR:");
432 PRINT_DEBUG_MEM_HEX8(DimmNum);
433 PRINT_DEBUG_MEM("\r");
436 /* Step 11. Precharge all (PALL) will be issued to the DDR. */
437 Data = Reg6BVal | 0x02;
438 pci_write_config8(MEMCTRL, 0x6b, Data);
440 /* Step 12. Read a double word from any address of the DIMM. */
443 /* Step 13. Execute 8 CBR refresh. */
444 Data = Reg6BVal | 0x04;
445 pci_write_config8(MEMCTRL, 0x6b, Data);
448 //reads and wait 100us between each read
449 for (Idx = 0; Idx < 8; Idx++) {
454 /* Step 17. Enable MRS for MAA. */
455 Data = Reg6BVal | 0x03;
456 pci_write_config8(MEMCTRL, 0x6b, Data);
459 * Step 18. The SDRAM parameters (Burst Length, CAS# Latency,
460 * Write recovery etc.)
463 /* Burst Length: really offset Rx6c[3] */
464 Data = pci_read_config8(MEMCTRL, 0x6c);
465 BL = (Data & 0x08) >> 3;
467 /* CL: really offset RX62[2:0] */
468 Data = pci_read_config8(MEMCTRL, 0x62);
471 AccessAddr = (u32) (CHA_DDR2_MRS_table[CL]);
475 /* Write recovery: really offset Rx63[7-5] */
476 Data = pci_read_config8(MEMCTRL, 0x63);
477 Twr = (Data & 0xE0) >> 5;
479 AccessAddr += CHA_DDR2_Twr_table[Twr];
480 // AccessAddr = 0x1012D8;
481 DimmRead(AccessAddr); /* Set MRS command. */
482 PRINT_DEBUG_MEM("Step 18 Address");
483 PRINT_DEBUG_MEM_HEX32(AccessAddr);
484 PRINT_DEBUG_MEM("\r");
487 if (DimmNum == 1) { /* DDR's ODT is 150ohm */
488 AccessAddr = (u32) CHA_OCD_Default_150ohm;
489 DimmRead(AccessAddr); /* Issue EMRS OCD Default. */
490 PRINT_DEBUG_MEM("Step 19 Address ");
491 PRINT_DEBUG_MEM_HEX32(AccessAddr);
492 PRINT_DEBUG_MEM("\r");
494 AccessAddr = (u32) CHA_OCD_Exit_150ohm;
495 DimmRead(AccessAddr); /* Issue EMRS OCD Calibration Mode Exit. */
496 PRINT_DEBUG_MEM("Step 20 Address ");
497 PRINT_DEBUG_MEM_HEX32(AccessAddr);
498 PRINT_DEBUG_MEM("\r");
499 } else if (DimmNum == 2) { /* DDR's ODT is 75ohm */
500 AccessAddr = (u32) CHA_OCD_Default_75ohm;
501 DimmRead(AccessAddr); /* Issue EMRS OCD Default. */
502 AccessAddr = (u32) CHA_OCD_Exit_75ohm;
503 DimmRead(AccessAddr); /* Issue EMRS OCD Calibration Mode Exit. */
505 PRINT_DEBUG_MEM("Dimm NUM ERROR: ");
506 PRINT_DEBUG_MEM_HEX8(DimmNum);
507 PRINT_DEBUG_MEM("\r");
511 * Step 21. After MRS the device should be ready for full
512 * functionality within 3 clocks after Tmrd is met.
515 pci_write_config8(MEMCTRL, 0x6b, Data);
517 /* Enable bank paging and multi page. */
518 Data = pci_read_config8(MEMCTRL, 0x69);
520 pci_write_config8(MEMCTRL, 0x69, Data);
523 /*===================================================================
524 Function : InitDDR2_CHB()
527 DramAttr: pointer point to DRAM_SYS_ATTR which consist the DDR and Dimm information
530 Purpose : Initialize DDR2 of CHB by standard sequence
532 ===================================================================*/
533 /*// DLL: Enable Reset
534 static const u32 CHB_MRS_DLL_150[2] = { 0x00020200 | (1 << 20), 0x00000800 }; // with 150 ohm (A17=1, A9=1), (A11=1)(cpu address)
535 //u32 CHB_MRS_DLL_75[2] = { 0x00020020 | (1 << 20), 0x00000800 }; // with 75 ohm (A17=1, A5=1), (A11=1)(cpu address)
537 // { DLL: Enable. A17(BA0)=1 and A3(MA0)=0 }
538 // { DLL: reset. A11(MA8)=1 }
540 // DDR2 CL=2 CL=3 CL=4 CL=5 (Burst type=interleave)(WR fine tune in code)
541 static const u16 CHB_DDR2_MRS_table[4] ={ 0x0150, 0x01D0, 0x0250, 0x02D0 }; // BL=4 ;Use 1X-bandwidth MA table to init DRAM
544 #define CHB_MRS_DDR2_TWR2 (0 << 13) + (0 << 20) + (1 << 12) // Value = 001000h
545 #define CHB_MRS_DDR2_TWR3 (0 << 13) + (1 << 20) + (0 << 12) // Value = 100000h
546 #define CHB_MRS_DDR2_TWR4 (0 << 13) + (1 << 20) + (1 << 12) // Value = 101000h
547 #define CHB_MRS_DDR2_TWR5 (1 << 13) + (0 << 20) + (0 << 12) // Value = 002000h
548 #define CHB_MRS_DDR2_TWR6 (1 << 13) + (0 << 20) + (1 << 12) // Value = 003000h
550 // DDR2 Twr=2 Twr=3 Twr=4 Twr=5
551 static const u32 CHB_DDR2_Twr_table[5] = { CHB_MRS_DDR2_TWR2, CHB_MRS_DDR2_TWR3, CHB_MRS_DDR2_TWR4, CHB_MRS_DDR2_TWR5, CHB_MRS_DDR2_TWR6 };
553 #define CHB_OCD_Exit_150ohm 0x20200 | (1 << 20) // EMRS(1), BA0=1, MA9=MA8=MA7=0,MA6=1,MA2=0 (DRAM bus address)
554 // A17=1, A12=A11=A10=0,A9=1 ,A5=0 (CPU address)
555 #define CHB_OCD_Default_150ohm 0x21E00 | (1 << 20) // EMRS(1), BA0=1, MA9=MA8=MA7=1,MA6=1,MA2=0 (DRAM bus address)
556 // A17=1, A12=A11=A10=1,A9=1 ,A5=0 (CPU address)
557 //#define CHB_OCD_Exit_75ohm 0x20020 | (1 << 20) // EMRS(1), BA0=1, MA9=MA8=MA7=0,MA6=0,MA2=1 (DRAM bus address)
558 // A17=1, A12=A11=A10=0,A9=0 ,A5=1 (CPU address)
559 //#define CHB_OCD_Default_75ohm 0x21C20 | (1 << 20) // EMRS(1), BA0=1, MA9=MA8=MA7=1,MA6=0,MA2=1 (DRAM bus address)
560 // A17=1, A12=A11=A10=1,A9=0 ,A5=1 (CPU address)
562 DRAM_SYS_ATTR *DramAttr
571 pci_write_config8(MEMCTRL, 0x54, Data);
574 //disable bank paging and multi page
575 Data=pci_read_config8(MEMCTRL, 0x69);
577 pci_write_config8(MEMCTRL, 0x69, Data);
579 Data=pci_read_config8(MEMCTRL, 0xd3);
581 pci_write_config8(MEMCTRL, 0xd3, Data);
583 //step 4. Initialize CHB begin
584 Data=pci_read_config8(MEMCTRL, 0xd3);
586 pci_write_config8(MEMCTRL, 0xd3, Data);
588 //Step 5. NOP command enable
589 Data=pci_read_config8(MEMCTRL, 0xd7);
592 pci_write_config8(MEMCTRL, 0xd7, Data);
594 //Step 6. issue a nop cycle,RegD3[7] 0 -> 1
595 Data=pci_read_config8(MEMCTRL, 0xd3);
597 pci_write_config8(MEMCTRL, 0xd3, Data);
599 pci_write_config8(MEMCTRL, 0xd3, Data);
602 // A minimum pause of 200u sec will be provided after the NOP.
603 // - <<< reduce BOOT UP time >>> -
605 for (Idx = 0; Idx < 0x10; Idx++)
609 // all banks precharge command enable
610 Data=pci_read_config8(MEMCTRL, 0xd7);
613 pci_write_config8(MEMCTRL, 0xd7, Data);
615 //step 9. issue a precharge all cycle,RegD3[7] 0 -> 1
616 Data=pci_read_config8(MEMCTRL, 0xd3);
618 pci_write_config8(MEMCTRL, 0xd3, Data);
620 pci_write_config8(MEMCTRL, 0xd3, Data);
622 //step10. EMRS enable
623 Data=pci_read_config8(MEMCTRL, 0xd7);
626 pci_write_config8(MEMCTRL, 0xd7, Data);
628 Data=pci_read_config8(MEMCTRL, 0xd3);
631 pci_write_config8(MEMCTRL, 0xd3, Data);
633 //step11. EMRS DLL Enable and Disable DQS
634 AccessAddr = CHB_MRS_DLL_150[0] >> 3;
635 Data =(u8) (AccessAddr & 0xff);
636 pci_write_config8(MEMCTRL, 0xd9, Data);
638 Data = (u8)((AccessAddr & 0xff00) >> 8);
639 pci_write_config8(MEMCTRL, 0xda, Data);
641 Data=pci_read_config8(MEMCTRL, 0xd7);
643 Data |= (u8)((AccessAddr & 0x30000) >> 15);
644 pci_write_config8(MEMCTRL, 0xd7, Data);
646 //step12. issue EMRS cycle
647 Data=pci_read_config8(MEMCTRL, 0xd3);
649 pci_write_config8(MEMCTRL, 0xd3, Data);
651 pci_write_config8(MEMCTRL, 0xd3, Data);
654 Data=pci_read_config8(MEMCTRL, 0xd7);
657 pci_write_config8(MEMCTRL, 0xd7, Data);
659 Data=pci_read_config8(MEMCTRL, 0xd3);
662 pci_write_config8(MEMCTRL, 0xd3, Data);
664 //step 14. MSR DLL Reset
665 AccessAddr = CHB_MRS_DLL_150[1] >> 3;
666 Data =(u8) (AccessAddr & 0xff);
667 pci_write_config8(MEMCTRL, 0xd9, Data);
669 Data = (u8)((AccessAddr & 0xff00) >> 8);
670 pci_write_config8(MEMCTRL, 0xda, Data);
672 Data=pci_read_config8(MEMCTRL, 0xd7);
674 Data |= (u8)((AccessAddr & 0x30000) >> 15);
675 pci_write_config8(MEMCTRL, 0xd7, Data);
677 //step15. issue MRS cycle
678 Data=pci_read_config8(MEMCTRL, 0xd3);
680 pci_write_config8(MEMCTRL, 0xd3, Data);
682 pci_write_config8(MEMCTRL, 0xd3, Data);
686 pci_write_config8(MEMCTRL, 0xda, Data);
688 //step16. all banks precharge command enable
689 Data=pci_read_config8(MEMCTRL, 0xd7);
692 pci_write_config8(MEMCTRL, 0xd7, Data);
695 // step17. issue precharge all cycle
696 Data=pci_read_config8(MEMCTRL, 0xd3);
698 pci_write_config8(MEMCTRL, 0xd3, Data);
700 pci_write_config8(MEMCTRL, 0xd3, Data);
702 //step18. CBR cycle enable
703 Data=pci_read_config8(MEMCTRL, 0xd7);
706 pci_write_config8(MEMCTRL, 0xd7, Data);
709 //repeat issue 8 CBR cycle, between each cycle stop 100us
710 for (Idx = 0; Idx < 8; Idx++)
713 Data=pci_read_config8(MEMCTRL, 0xd3);
715 pci_write_config8(MEMCTRL, 0xd3, Data);
717 pci_write_config8(MEMCTRL, 0xd3, Data);
723 Data=pci_read_config8(MEMCTRL, 0xd7);
726 pci_write_config8(MEMCTRL, 0xd7, Data);
728 Data=pci_read_config8(MEMCTRL, 0xd3);
731 pci_write_config8(MEMCTRL, 0xd3, Data);
734 //the SDRAM parameters.(Burst Length, CAS# Latency , Write recovery etc.)
735 //-------------------------------------------------------------
736 //Burst Length : really offset Rx6c[1]
737 Data=pci_read_config8(MEMCTRL, 0x6C);
738 BL = (Data & 0x02) >> 1;
740 // CL = really offset RX62[2:0]
741 Data=pci_read_config8(MEMCTRL, 0x62);
744 AccessAddr = (u32)(CHB_DDR2_MRS_table[CL]);
750 //Write recovery : really offset Rx63[7:5]
751 Data=pci_read_config8(MEMCTRL, 0x63);
752 Twr = (Data & 0xE0) >> 5;
754 AccessAddr += CHB_DDR2_Twr_table[Twr];
755 //MSR Address use addr[20:3]
758 //step 23. MSR command
759 Data = (u8)(AccessAddr & 0xFF);
760 pci_write_config8(MEMCTRL, 0xD9, Data);
762 Data = (u8)((AccessAddr & 0xFF00) >> 8);
763 pci_write_config8(MEMCTRL, 0xda, Data);
765 Data=pci_read_config8(MEMCTRL, 0xd7);
767 Data |= (u8)(((AccessAddr & 0x30000)>>16) << 1);
768 pci_write_config8(MEMCTRL, 0xd7, Data);
770 //step 24. issue MRS cycle
771 Data=pci_read_config8(MEMCTRL, 0xd3);
773 pci_write_config8(MEMCTRL, 0xd3, Data);
775 pci_write_config8(MEMCTRL, 0xd3, Data);
777 //step 25. EMRS enable
778 Data=pci_read_config8(MEMCTRL, 0xd7);
781 pci_write_config8(MEMCTRL, 0xd7, Data);
783 Data=pci_read_config8(MEMCTRL, 0xd3);
786 pci_write_config8(MEMCTRL, 0xd3, Data);
789 //step 26. OCD default
790 AccessAddr = (CHB_OCD_Default_150ohm) >> 3;
791 Data =(u8) (AccessAddr & 0xff);
792 pci_write_config8(MEMCTRL, 0xd9, Data);
794 Data = (u8)((AccessAddr & 0xff00) >> 8);
795 pci_write_config8(MEMCTRL, 0xda, Data);
797 Data=pci_read_config8(MEMCTRL, 0xd7);
799 Data |= (u8)((AccessAddr & 0x30000) >> 15);
800 pci_write_config8(MEMCTRL, 0xd7, Data);
802 //step 27. issue EMRS cycle
803 Data=pci_read_config8(MEMCTRL, 0xd3);
805 pci_write_config8(MEMCTRL, 0xd3, Data);
807 pci_write_config8(MEMCTRL, 0xd3, Data);
809 //step 25. EMRS enable
810 Data=pci_read_config8(MEMCTRL, 0xd7);
813 pci_write_config8(MEMCTRL, 0xd7, Data);
815 Data=pci_read_config8(MEMCTRL, 0xd3);
818 pci_write_config8(MEMCTRL, 0xd3, Data);
821 AccessAddr = (CHB_OCD_Exit_150ohm) >> 3;
822 Data =(u8) (AccessAddr & 0xff);
823 pci_write_config8(MEMCTRL, 0xd9, Data);
825 Data = (u8)((AccessAddr & 0xff00) >> 8);
826 pci_write_config8(MEMCTRL, 0xda, Data);
828 Data=pci_read_config8(MEMCTRL, 0xd7);
830 Data |= (u8)((AccessAddr & 0x30000) >> 15);
831 pci_write_config8(MEMCTRL, 0xd7, Data);
833 //step 29. issue EMRS cycle
834 Data=pci_read_config8(MEMCTRL, 0xd3);
836 pci_write_config8(MEMCTRL, 0xd3, Data);
838 pci_write_config8(MEMCTRL, 0xd3, Data);
840 //clear all the address
842 pci_write_config8(MEMCTRL, 0xd9, Data);
845 pci_write_config8(MEMCTRL, 0xda, Data);
847 Data=pci_read_config8(MEMCTRL, 0xd7);
849 pci_write_config8(MEMCTRL, 0xd7, Data);
851 //step 30. normal SDRAM Mode
852 Data=pci_read_config8(MEMCTRL, 0xd7);
855 pci_write_config8(MEMCTRL, 0xd7, Data);
857 Data=pci_read_config8(MEMCTRL, 0xd3);
860 pci_write_config8(MEMCTRL, 0xd3, Data);
862 //step 31. exit the initialization mode
863 Data=pci_read_config8(MEMCTRL, 0xd3);
865 pci_write_config8(MEMCTRL, 0xd3, Data);
868 //step 32. Enable bank paging and multi page
869 Data=pci_read_config8(MEMCTRL, 0x69);
871 pci_write_config8(MEMCTRL, 0x69, Data);
875 /*===================================================================
876 Function : InitDDR2CHC()
879 DramAttr: pointer point to DRAM_SYS_ATTR which consist the DDR and Dimm information
882 Purpose : Initialize DDR2 of CHC by standard sequence
884 ===================================================================*/
885 // DDR2 CL=2 CL=3 CL=4 CL=5 (Burst type=interleave)(WR fine tune in code)
886 static const u16 CHC_MRS_table[4] = { 0x22B, 0x23B, 0x24B, 0x25B }; // Use 1X-bandwidth MA table to init DRAM
888 void InitDDR2CHC(DRAM_SYS_ATTR *DramAttr)
890 u8 Data, Idx, CL, Twr;
894 /* Step 3. Clear RxDF[2] to disable Tri-state output. */
895 Data = pci_read_config8(MEMCTRL, 0xdf);
897 pci_write_config8(MEMCTRL, 0xdf, Data);
900 * Step 4. Enable the initialization mode of DRAM Controller C with
903 Data = pci_read_config8(MEMCTRL, 0xdb);
905 pci_write_config8(MEMCTRL, 0xdb, Data);
907 /* Step 5. NOP command enable. */
908 Data = pci_read_config8(MEMCTRL, 0xdb);
911 pci_write_config8(MEMCTRL, 0xdb, Data);
913 /* Step 6. Issue a nop cycle, RegDB[1] 0 -> 1. */
914 Data = pci_read_config8(MEMCTRL, 0xdb);
916 pci_write_config8(MEMCTRL, 0xdb, Data);
918 pci_write_config8(MEMCTRL, 0xdb, Data);
922 * A minimum pause of 200u sec will be provided after the NOP.
923 * - <<< reduce BOOT UP time >>> -
926 for (Idx = 0; Idx < 0x10; Idx++)
929 /* Step 8. Signal bank precharge command enable. */
930 Data = pci_read_config8(MEMCTRL, 0xdb);
933 pci_write_config8(MEMCTRL, 0xdb, Data);
935 /* Set MA10 = 1, precharge all bank. */
937 pci_write_config8(MEMCTRL, 0xf8, Data);
940 pci_write_config8(MEMCTRL, 0xf9, Data);
942 /* step 9. Issue a precharge all cycle, RegD3[7] 0 -> 1. */
943 Data = pci_read_config8(MEMCTRL, 0xdb);
945 pci_write_config8(MEMCTRL, 0xdb, Data);
947 pci_write_config8(MEMCTRL, 0xdb, Data);
949 /* Step 10. MRS enable. */
950 Data = pci_read_config8(MEMCTRL, 0xdb);
953 pci_write_config8(MEMCTRL, 0xdb, Data);
955 /* Step 11. EMRS DLL enable and Disable DQS. */
957 pci_write_config8(MEMCTRL, 0xf8, Data);
960 pci_write_config8(MEMCTRL, 0xf9, Data);
962 /* Step 12. Issue EMRS cycle. */
963 Data = pci_read_config8(MEMCTRL, 0xdb);
965 pci_write_config8(MEMCTRL, 0xdb, Data);
967 pci_write_config8(MEMCTRL, 0xdb, Data);
969 /* Step 13. MSR enable. */
970 Data = pci_read_config8(MEMCTRL, 0xdb);
973 pci_write_config8(MEMCTRL, 0xdb, Data);
975 /* Step 14. MSR DLL Reset. */
977 pci_write_config8(MEMCTRL, 0xf8, Data);
980 pci_write_config8(MEMCTRL, 0xf9, Data);
982 /* Step 15. Issue MRS cycle. */
983 Data = pci_read_config8(MEMCTRL, 0xdb);
985 pci_write_config8(MEMCTRL, 0xdb, Data);
987 pci_write_config8(MEMCTRL, 0xdb, Data);
989 /* Step 16. Signal banks precharge command enable. */
990 Data = pci_read_config8(MEMCTRL, 0xdb);
993 pci_write_config8(MEMCTRL, 0xdb, Data);
995 /* Set MA10 = 1, precharge all bank. */
997 pci_write_config8(MEMCTRL, 0xf8, Data);
1000 pci_write_config8(MEMCTRL, 0xf9, Data);
1002 /* Step 17. Issue precharge all cycle. */
1003 Data = pci_read_config8(MEMCTRL, 0xdb);
1005 pci_write_config8(MEMCTRL, 0xdb, Data);
1007 pci_write_config8(MEMCTRL, 0xdb, Data);
1009 /* Step 18. CBR cycle enable. */
1010 Data = pci_read_config8(MEMCTRL, 0xdb);
1013 pci_write_config8(MEMCTRL, 0xdb, Data);
1016 pci_write_config8(MEMCTRL, 0xf8, Data);
1019 pci_write_config8(MEMCTRL, 0xf9, Data);
1022 //repeat issue 8 CBR cycle, between each cycle stop 100us
1023 for (Idx = 0; Idx < 8; Idx++) {
1025 Data = pci_read_config8(MEMCTRL, 0xdb);
1027 pci_write_config8(MEMCTRL, 0xdb, Data);
1029 pci_write_config8(MEMCTRL, 0xdb, Data);
1033 //the SDRAM parameters.(, CAS# Latency , Write recovery etc.)
1034 //------------------------------------------------------------
1036 // CL = really offset RXDC[7:6]
1037 Data = pci_read_config8(MEMCTRL, 0xdc);
1038 CL = (Data & 0xC0) >> 6;
1040 AccessAddr = (u32) (CHC_MRS_table[CL]);
1042 //Write recovery : really offset Rx63[7:5]
1043 Data = pci_read_config8(MEMCTRL, 0x63);
1044 Twr = (Data & 0xE0) >> 5;
1046 AccessAddr += Twr * 0x200;
1048 //step22. MSR enable
1049 Data = pci_read_config8(MEMCTRL, 0xdb);
1052 pci_write_config8(MEMCTRL, 0xdb, Data);
1054 //step 23. MSR command
1055 Data = (u8) (AccessAddr & 0xFF);
1056 pci_write_config8(MEMCTRL, 0xf8, Data);
1058 Data = (u8) ((AccessAddr & 0xFF00) >> 8);
1059 pci_write_config8(MEMCTRL, 0xf9, Data);
1061 //step 24. issue MRS cycle
1062 Data = pci_read_config8(MEMCTRL, 0xdb);
1064 pci_write_config8(MEMCTRL, 0xdb, Data);
1066 pci_write_config8(MEMCTRL, 0xdb, Data);
1068 //step 25. EMRS enable
1069 Data = pci_read_config8(MEMCTRL, 0xdb);
1072 pci_write_config8(MEMCTRL, 0xdb, Data);
1074 //step 26. OCD default
1076 pci_write_config8(MEMCTRL, 0xf8, Data);
1079 pci_write_config8(MEMCTRL, 0xf9, Data);
1081 //step 27. issue EMRS cycle
1082 Data = pci_read_config8(MEMCTRL, 0xdb);
1084 pci_write_config8(MEMCTRL, 0xdb, Data);
1086 pci_write_config8(MEMCTRL, 0xdb, Data);
1090 pci_write_config8(MEMCTRL, 0xf8, Data);
1093 pci_write_config8(MEMCTRL, 0xf9, Data);
1095 //step 29. issue EMRS cycle
1096 Data = pci_read_config8(MEMCTRL, 0xdb);
1098 pci_write_config8(MEMCTRL, 0xdb, Data);
1100 pci_write_config8(MEMCTRL, 0xdb, Data);
1102 Status = VerifyChc();
1103 if (Status != CB_SUCCESS)
1104 PRINT_DEBUG_MEM("Error!!!!CHC init error!\r");
1105 //step 31. exit the initialization mode
1106 Data = pci_read_config8(MEMCTRL, 0xdb);
1108 pci_write_config8(MEMCTRL, 0xdb, Data);
1111 CB_STATUS VerifyChc(void)
1113 u8 Data, ByteVal, Index, pad;
1116 //first write the pad to all the address
1118 //the row bits is 13 and rank bit is 2, so the address bits is 15 and the value is 0x7fff
1119 //verify each MA[0:12],BA[0:1]
1121 for (row = 0; row < 0x8000; row++) {
1122 /* Set the write value, Verify each MD[15:0]. */
1123 for (Data = pad, Index = 0; Index < 16; Index++) {
1127 pci_write_config8(PCI_DEV(0, 0, 7), 0xC0 + Index, Data);
1130 /* Issue the bank active command. */
1131 // bank active command enable
1132 Data = pci_read_config8(MEMCTRL, 0xdb);
1135 pci_write_config8(MEMCTRL, 0xdb, Data);
1137 Data = (u8) (row && 0xFF);
1138 pci_write_config8(MEMCTRL, 0xf8, Data);
1140 Data = (u8) ((row && 0xFF) >> 8);
1141 pci_write_config8(MEMCTRL, 0xf9, Data);
1143 /* Issue active cycle. */
1144 Data = pci_read_config8(MEMCTRL, 0xdb);
1146 pci_write_config8(MEMCTRL, 0xdb, Data);
1148 pci_write_config8(MEMCTRL, 0xdb, Data);
1150 /* Issue ready/completion for read/write. */
1151 // read/completion command enable
1152 Data = pci_read_config8(MEMCTRL, 0xdb);
1155 pci_write_config8(MEMCTRL, 0xdb, Data);
1158 pci_write_config8(MEMCTRL, 0xf8, Data);
1161 pci_write_config8(MEMCTRL, 0xf9, Data);
1163 /* Issue read/completion cycle. */
1164 Data = pci_read_config8(MEMCTRL, 0xdb);
1166 pci_write_config8(MEMCTRL, 0xdb, Data);
1168 pci_write_config8(MEMCTRL, 0xdb, Data);
1170 /* Issue write command. */
1171 // write command enable
1172 Data = pci_read_config8(MEMCTRL, 0xdb);
1175 pci_write_config8(MEMCTRL, 0xdb, Data);
1178 pci_write_config8(MEMCTRL, 0xf8, Data);
1180 Data = (u8) ((row & 0x60) << 5);
1181 pci_write_config8(MEMCTRL, 0xf9, Data);
1183 /* Issue write cycle. */
1184 Data = pci_read_config8(MEMCTRL, 0xdb);
1186 pci_write_config8(MEMCTRL, 0xdb, Data);
1188 pci_write_config8(MEMCTRL, 0xdb, Data);
1190 ////issue ready/completion for read/write
1191 // read/completion command enable
1192 Data = pci_read_config8(MEMCTRL, 0xdb);
1195 pci_write_config8(MEMCTRL, 0xdb, Data);
1198 pci_write_config8(MEMCTRL, 0xf8, Data);
1201 pci_write_config8(MEMCTRL, 0xf9, Data);
1203 /* Issue read/completion cycle. */
1204 Data = pci_read_config8(MEMCTRL, 0xdb);
1206 pci_write_config8(MEMCTRL, 0xdb, Data);
1208 pci_write_config8(MEMCTRL, 0xdb, Data);
1210 /* Issue the bank active command. */
1211 // bank active command enable
1212 Data = pci_read_config8(MEMCTRL, 0xdb);
1215 pci_write_config8(MEMCTRL, 0xdb, Data);
1217 Data = (u8) (row && 0xFF);
1218 pci_write_config8(MEMCTRL, 0xf8, Data);
1220 Data = (u8) ((row && 0xFF) >> 8);
1221 pci_write_config8(MEMCTRL, 0xf9, Data);
1223 // issue active cycle
1224 Data = pci_read_config8(MEMCTRL, 0xdb);
1226 pci_write_config8(MEMCTRL, 0xdb, Data);
1228 pci_write_config8(MEMCTRL, 0xdb, Data);
1230 ////issue ready/completion for read/write
1231 // read/completion command enable
1232 Data = pci_read_config8(MEMCTRL, 0xdb);
1235 pci_write_config8(MEMCTRL, 0xdb, Data);
1238 pci_write_config8(MEMCTRL, 0xf8, Data);
1241 pci_write_config8(MEMCTRL, 0xf9, Data);
1243 // issue read/completion cycle
1244 Data = pci_read_config8(MEMCTRL, 0xdb);
1246 pci_write_config8(MEMCTRL, 0xdb, Data);
1248 pci_write_config8(MEMCTRL, 0xdb, Data);
1250 ////issue read command
1251 // read/completion command enable
1252 Data = pci_read_config8(MEMCTRL, 0xdb);
1255 pci_write_config8(MEMCTRL, 0xdb, Data);
1258 pci_write_config8(MEMCTRL, 0xf8, Data);
1260 Data = (u8) ((row & 0x60) << 5);
1261 pci_write_config8(MEMCTRL, 0xf9, Data);
1264 Data = pci_read_config8(MEMCTRL, 0xdb);
1266 pci_write_config8(MEMCTRL, 0xdb, Data);
1268 pci_write_config8(MEMCTRL, 0xdb, Data);
1270 ////issue ready/completion for read/write
1271 // read/completion command enable
1272 Data = pci_read_config8(MEMCTRL, 0xdb);
1275 pci_write_config8(MEMCTRL, 0xdb, Data);
1278 pci_write_config8(MEMCTRL, 0xf8, Data);
1281 pci_write_config8(MEMCTRL, 0xf9, Data);
1283 /* Issue read/completion cycle. */
1284 Data = pci_read_config8(MEMCTRL, 0xdb);
1286 pci_write_config8(MEMCTRL, 0xdb, Data);
1288 pci_write_config8(MEMCTRL, 0xdb, Data);
1290 /* Verify the value. */
1291 for (ByteVal = pad, Index = 0; Index < 16; Index++) {
1292 Data = pci_read_config8(PCI_DEV(0, 0, 7), 0xD0 + Index);
1293 if (ByteVal != Data) {
1294 PRINT_DEBUG_MEM("Error! row = %x, index =%x, "
1295 "data = %x, byteval=%x\r");