Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / northbridge / via / vx800 / dev_init.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2009 One Laptop per Child, Association, Inc.
5  *
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.
9  *
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.
14  *
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
18  */
19
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);
28
29 CB_STATUS VerifyChc(void);
30
31 /*===================================================================
32 Function   : DRAMRegInitValue()
33 Precondition :
34 Input      :
35                    DramAttr:  pointer point to  DRAM_SYS_ATTR  which consist the DDR and Dimm information
36                                     in MotherBoard
37 Output     : Void
38 Purpose   : Set necessary register before DRAM initialize
39 ===================================================================*/
40
41 static const u8 DramRegTbl[][3] = {
42         /* Reg AND   OR */
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
47
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
52
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
57         {0x68, 0x00, 0x0C},
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
61                                 // will fail
62         /*
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
65          * R/W DRAM.
66          */
67
68         // {0x79, 0x00, 0x8F },
69         {0x85, 0x00, 0x00},
70         // {0x90, 0x87, 0x78 },
71         // {0x91, 0x00, 0x46 },
72         {0x40, 0x00, 0x00},
73
74         {0, 0, 0}
75 };
76
77 void DRAMRegInitValue(DRAM_SYS_ATTR *DramAttr)
78 {
79         u8 Idx, CL, Data;
80
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);
86         }
87
88         Data = 0x80;
89         pci_write_config8(PCI_DEV(0, 0, 4), 0xa3, Data);
90
91         // Set DRAM controller mode. */
92         Data = pci_read_config8(MEMCTRL, 0x6c);
93         Data &= 0xFB;
94         if (ENABLE_CHC == 0) {
95                 Data |= 0x4;    /* Only CHA 64 bit mode */
96                 pci_write_config8(MEMCTRL, 0x6c, Data);
97         } else {
98                 Data |= 0x0;    /* CHA + CHC */
99                 pci_write_config8(MEMCTRL, 0x6c, Data);
100
101                 // Data = 0xAA;
102                 // pci_write_config8(MEMCTRL, 0xb1, Data);
103
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);
109
110                 // enable CHC  RXDB[7]
111                 // Data = pci_read_config8(MEMCTRL, 0xdb);
112                 // Data = (Data & 0x7F) | 0x80;
113                 // pci_write_config8(MEMCTRL, 0xdb, Data);
114
115                 // rx62[2:0], CHA and CHB CL
116                 Data = pci_read_config8(MEMCTRL, 0x62);
117                 CL = Data & 0x07;
118
119                 // If CL = 6, so I set CHB CL = 5 default.
120                 if (CL >= 4)
121                         CL = 3;
122
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);
127
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);
132
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);
137
138                 /* Set column type RXDD[6] and enable ODT PAD RXDD[7]. */
139                 Data = pci_read_config8(MEMCTRL, 0xdd);
140                 Data |= 0x80;
141                 Idx = DramAttr->DimmInfo[2].SPDDataBuf[SPD_SDRAM_COL_ADDR];
142                 if ((Idx & 0x0F) == 10)
143                         Data |= 0x40;   /* MA9~MA0 */
144                 else
145                         Data &= 0xBF;   /* MA8~MA0 */
146                 pci_write_config8(MEMCTRL, 0xdd, Data);
147         }
148
149         // Disable Read DRAM fast ready ;Rx51[7]
150         // Disable Read Around Write    ;Rx51[6]
151
152         // Disable Consecutive Read     ;RX52[1:0]
153         // Disable Speculative Read
154 }
155
156 /*===================================================================
157 Function   : DRAMInitializeProc()
158 Precondition :
159 Input      :
160                    DramAttr:  pointer point to  DRAM_SYS_ATTR  which consist the DDR and Dimm information
161                                     in MotherBoard
162 Output     : Void
163 Purpose   : DRAM initialize according to the bios porting guid
164 ===================================================================*/
165
166 #define EXIST_TEST_PATTERN              0x55555555
167 #define NOT_EXIST_TEST_PATTERN          0xAAAAAAAA
168
169 static BOOLEAN ChkForExistLowBank(void)
170 {
171         u32 *Address, data32;
172
173         /* Check pattern */
174         Address = (u32 *) 8;
175         *Address = EXIST_TEST_PATTERN;
176         Address = (u32 *) 4;
177         *Address = EXIST_TEST_PATTERN;
178
179         // _asm {WBINVD}
180         WaitMicroSec(100);
181         Address = (u32 *) 8;
182         data32 = *Address;
183         if (data32 != EXIST_TEST_PATTERN)
184                 return FALSE;
185         Address = (u32 *) 4;
186         data32 = *Address;
187         if (data32 != EXIST_TEST_PATTERN)
188                 return FALSE;
189
190         /* Check not Pattern */
191         Address = (u32 *) 8;
192         *Address = NOT_EXIST_TEST_PATTERN;
193         Address = (u32 *) 4;
194         *Address = NOT_EXIST_TEST_PATTERN;
195         // _asm {WBINVD}
196         WaitMicroSec(100);
197
198         Address = (u32 *) 8;
199         data32 = *Address;
200         if (data32 != (u32) (NOT_EXIST_TEST_PATTERN))
201                 return FALSE;
202         Address = (u32 *) 4;
203         data32 = *Address;
204         if (data32 != (u32) (NOT_EXIST_TEST_PATTERN))
205                 return FALSE;
206
207         return TRUE;
208 }
209
210 void DRAMInitializeProc(DRAM_SYS_ATTR *DramAttr)
211 {
212         u8 shift, idx;
213         BOOLEAN Status;
214
215         shift = 1;
216         for (idx = 0; idx < MAX_RANKS; idx++) {
217                 if ((DramAttr->RankPresentMap & shift) != 0) {
218                         /*
219                          * Set VR# to physical rank indicated = PR + physical
220                          * rank enable bit.
221                          */
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");
230                         } else {
231                                 PRINT_DEBUG_MEM(" F\r");
232                         }
233
234                         /*
235                          * Set VR# to physical rank indicated = 00h + physical
236                          * rank enable bit.
237                          */
238                         DRAMSetVRNum(DramAttr, idx, 0, FALSE);
239                         SetEndingAddr(DramAttr, idx, -16);
240                 }
241                 shift <<= 1;
242         }
243         if (ENABLE_CHC)
244                 InitDDR2CHC(DramAttr);
245
246 }
247
248 /*===================================================================
249 Function   : DRAMSetVRNUM()
250 Precondition :
251 Input      :
252                    DramAttr:  pointer point to  DRAM_SYS_ATTR  which consist the DDR and Dimm information
253                                     in MotherBoard
254                   PhyRank:   Physical Rank number
255                   VirRank:    Virtual Rank number
256                   Enable:      Enable/Disable Physical Rank
257 Output     : Void
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 ===================================================================*/
262
263 void DRAMSetVRNum(DRAM_SYS_ATTR *DramAttr, u8 PhyRank /* physical rank */,
264                   u8 VirRank /* virtual rank */, BOOLEAN Enable)
265 {
266         u8 Data, AndData, OrData;
267
268         Data = pci_read_config8(MEMCTRL, (0x54 + (PhyRank >> 1)));
269
270         OrData = 0;
271         if (Enable)
272                 OrData |= 0x08;
273         OrData |= VirRank;
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
277         } else {
278                 AndData = 0xF0; // keep the value of even rank on PR # is odd(keep 0,2,4,6)
279         }
280
281         Data &= AndData;
282         Data |= OrData;
283         pci_write_config8(MEMCTRL, (0x54 + (PhyRank >> 1)), Data);
284 }
285
286 /*===================================================================
287 Function   : SetEndingAddr()
288 Precondition :
289 Input      :
290                    DramAttr:  pointer point to  DRAM_SYS_ATTR  which consist the DDR and Dimm information
291                                     in MotherBoard
292                   VirRank:    Virtual Rank number
293                   Value:       (value) add or subtract value to this and after banks
294 Output     : Void
295 Purpose   : Set ending address of virtual rank specified by VirRank
296 ===================================================================*/
297
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 */) {
301         u8 Data;
302
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);
307
308         /* Program the virank's begining address to zero. */
309         Data = 0x00;
310         pci_write_config8(MEMCTRL, 0x48 + VirRank, Data);
311 }
312
313 /*===================================================================
314 Function   : InitDDR2()
315 Precondition :
316 Input      :
317                    DramAttr:  pointer point to  DRAM_SYS_ATTR  which consist the DDR and Dimm information
318                                     in MotherBoard
319 Output     : Void
320 Purpose   : Initialize DDR2 by standard sequence
321 ===================================================================*/
322
323 //                               DLL:         Enable                              Reset
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)
326
327 //               CPU(DRAM)
328 // { DLL: Enable. A17(BA0)=1 and A3(MA0)=0 }
329 // { DLL: reset.  A11(MA8)=1 }
330 //
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
333
334 //                                                       MA11        MA10(AP)      MA9
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
340
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
345 };
346
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)
355
356 void InitDDR2CHA(DRAM_SYS_ATTR *DramAttr)
357 {
358         u8 Data, Reg6BVal, Idx, CL, BL, Twr, DimmNum;
359         u32 AccessAddr;
360
361         /* Step 2 */
362         /* Disable bank paging and multi page. */
363         Data = pci_read_config8(MEMCTRL, 0x69);
364         Data &= ~0x03;
365         pci_write_config8(MEMCTRL, 0x69, Data);
366
367         Reg6BVal = pci_read_config8(MEMCTRL, 0x6b);
368         Reg6BVal &= ~0x07;
369
370         /* Step 3 */
371         /* At least one NOP cycle will be issued after the 1m sec device
372          * deselect.
373          */
374         Data = Reg6BVal | 0x01;
375         pci_write_config8(MEMCTRL, 0x6b, Data);
376
377         /* Step 4 */
378         /* Read a double word from any address of the DIMM. */
379         DimmRead(0x0);
380
381         /* Step 5 */
382         /*
383          * A minimum pause of 200u sec will be provided after the NOP.
384          * - <<<    reduce BOOT UP time >>> -
385          * Loop 200us
386          */
387         for (Idx = 0; Idx < 0x10; Idx++)
388                 WaitMicroSec(100);
389
390         // Step 6.
391         // Precharge all (PALL) will be issued to the DDR.
392         Data = Reg6BVal | 0x02;
393         pci_write_config8(MEMCTRL, 0x6b, Data);
394
395         // Step7.
396         // Read a double word from any address of the DIMM
397         DimmRead(0x0);
398
399         // Step 8.
400         // MSR Eable will be issued to the DDR
401         Data = Reg6BVal | 0x03;
402         pci_write_config8(MEMCTRL, 0x6b, Data);
403
404         /* Step 9, 10.
405          *
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.
410          */
411         DimmNum = DramAttr->DimmNumChA;
412
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");
419
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. */
430         } else {
431                 PRINT_DEBUG_MEM("Dimm NUM ERROR:");
432                 PRINT_DEBUG_MEM_HEX8(DimmNum);
433                 PRINT_DEBUG_MEM("\r");
434         }
435
436         /* Step 11. Precharge all (PALL) will be issued to the DDR. */
437         Data = Reg6BVal | 0x02;
438         pci_write_config8(MEMCTRL, 0x6b, Data);
439
440         /* Step 12. Read a double word from any address of the DIMM. */
441         DimmRead(0x0);
442
443         /* Step 13. Execute 8 CBR refresh. */
444         Data = Reg6BVal | 0x04;
445         pci_write_config8(MEMCTRL, 0x6b, Data);
446
447         // issue 14,15 , 16
448         //reads and wait 100us between each read
449         for (Idx = 0; Idx < 8; Idx++) {
450                 DimmRead(0x0);
451                 WaitMicroSec(100);
452         }
453
454         /* Step 17. Enable MRS for MAA. */
455         Data = Reg6BVal | 0x03;
456         pci_write_config8(MEMCTRL, 0x6b, Data);
457
458         /*
459          * Step 18. The SDRAM parameters (Burst Length, CAS# Latency,
460          * Write recovery etc.)
461          */
462
463         /* Burst Length: really offset Rx6c[3] */
464         Data = pci_read_config8(MEMCTRL, 0x6c);
465         BL = (Data & 0x08) >> 3;
466
467         /* CL: really offset RX62[2:0] */
468         Data = pci_read_config8(MEMCTRL, 0x62);
469         CL = Data & 0x03;
470
471         AccessAddr = (u32) (CHA_DDR2_MRS_table[CL]);
472         if (BL)
473                 AccessAddr += 8;
474
475         /* Write recovery: really offset Rx63[7-5] */
476         Data = pci_read_config8(MEMCTRL, 0x63);
477         Twr = (Data & 0xE0) >> 5;
478
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");
485
486         /* Step 19, 20 */
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");
493
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. */
504         } else {
505                 PRINT_DEBUG_MEM("Dimm NUM ERROR: ");
506                 PRINT_DEBUG_MEM_HEX8(DimmNum);
507                 PRINT_DEBUG_MEM("\r");
508         }
509
510         /*
511          * Step 21. After MRS the device should be ready for full
512          * functionality within 3 clocks after Tmrd is met.
513          */
514         Data = Reg6BVal;
515         pci_write_config8(MEMCTRL, 0x6b, Data);
516
517         /* Enable bank paging and multi page. */
518         Data = pci_read_config8(MEMCTRL, 0x69);
519         Data |= 0x03;
520         pci_write_config8(MEMCTRL, 0x69, Data);
521 }
522
523 /*===================================================================
524 Function   : InitDDR2_CHB()
525 Precondition :
526 Input      :
527                    DramAttr:  pointer point to  DRAM_SYS_ATTR  which consist the DDR and Dimm information
528                                     in MotherBoard
529 Output     : Void
530 Purpose   : Initialize DDR2 of CHB by standard sequence
531 Reference  :
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)
536 //               CPU(DRAM)
537 // { DLL: Enable. A17(BA0)=1 and A3(MA0)=0 }
538 // { DLL: reset.  A11(MA8)=1 }
539 //
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
542
543 //                                                       MA11        MA10(AP)      MA9
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
549
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 };
552
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)
561 void InitDDR2CHB(
562                DRAM_SYS_ATTR          *DramAttr
563              )
564
565 {
566     u8     Data;
567     u8      Idx, CL, BL, Twr;
568     u32   AccessAddr;
569
570     Data = 0x80;
571     pci_write_config8(MEMCTRL, 0x54, Data);
572
573     // step3.
574     //disable bank paging and multi page
575     Data=pci_read_config8(MEMCTRL, 0x69);
576     Data &= ~0x03;
577     pci_write_config8(MEMCTRL, 0x69, Data);
578
579     Data=pci_read_config8(MEMCTRL, 0xd3);
580     Data |= 0x80;
581     pci_write_config8(MEMCTRL, 0xd3, Data);
582
583     //step 4. Initialize CHB begin
584     Data=pci_read_config8(MEMCTRL, 0xd3);
585     Data |= 0x40;
586     pci_write_config8(MEMCTRL, 0xd3, Data);
587
588     //Step 5. NOP command enable
589     Data=pci_read_config8(MEMCTRL, 0xd7);
590     Data &= 0xC7;
591     Data  |= 0x08;
592     pci_write_config8(MEMCTRL, 0xd7, Data);
593
594     //Step 6.  issue a nop cycle,RegD3[7]  0 -> 1
595     Data=pci_read_config8(MEMCTRL, 0xd3);
596     Data &= 0x7F;
597     pci_write_config8(MEMCTRL, 0xd3, Data);
598     Data |=  0x80;
599     pci_write_config8(MEMCTRL, 0xd3, Data);
600
601     // Step 7.
602     // A minimum pause of 200u sec will be provided after the NOP.
603     // - <<<    reduce BOOT UP time >>> -
604     // Loop 200us
605     for (Idx = 0; Idx < 0x10; Idx++)
606         WaitMicroSec(10);
607
608     // Step 8.
609     // all banks precharge command enable
610     Data=pci_read_config8(MEMCTRL, 0xd7);
611     Data &= 0xC7;
612     Data |= 0x10;
613     pci_write_config8(MEMCTRL, 0xd7, Data);
614
615    //step 9. issue a precharge all cycle,RegD3[7]  0 -> 1
616     Data=pci_read_config8(MEMCTRL, 0xd3);
617     Data &= 0x7F;
618     pci_write_config8(MEMCTRL, 0xd3, Data);
619     Data |=  0x80;
620     pci_write_config8(MEMCTRL, 0xd3, Data);
621
622    //step10. EMRS enable
623     Data=pci_read_config8(MEMCTRL, 0xd7);
624     Data &= 0xC7;
625     Data |= 0x18;
626     pci_write_config8(MEMCTRL, 0xd7, Data);
627
628     Data=pci_read_config8(MEMCTRL, 0xd3);
629     Data &= 0xC7;
630     Data |= 0x08;
631     pci_write_config8(MEMCTRL, 0xd3, Data);
632
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);
637
638     Data = (u8)((AccessAddr & 0xff00) >> 8);
639     pci_write_config8(MEMCTRL, 0xda, Data);
640
641     Data=pci_read_config8(MEMCTRL, 0xd7);
642     Data &= 0xF9;
643     Data |= (u8)((AccessAddr & 0x30000) >> 15);
644     pci_write_config8(MEMCTRL, 0xd7, Data);
645
646     //step12.  issue EMRS cycle
647     Data=pci_read_config8(MEMCTRL, 0xd3);
648     Data &= 0x7F;
649     pci_write_config8(MEMCTRL, 0xd3, Data);
650     Data |=  0x80;
651     pci_write_config8(MEMCTRL, 0xd3, Data);
652
653     //step13. MSR enable
654     Data=pci_read_config8(MEMCTRL, 0xd7);
655     Data &= 0xC7;
656     Data |= 0x18;
657     pci_write_config8(MEMCTRL, 0xd7, Data);
658
659     Data=pci_read_config8(MEMCTRL, 0xd3);
660     Data &= 0xC7;
661     Data |= 0x00;
662     pci_write_config8(MEMCTRL, 0xd3, Data);
663
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);
668
669     Data = (u8)((AccessAddr & 0xff00) >> 8);
670     pci_write_config8(MEMCTRL, 0xda, Data);
671
672     Data=pci_read_config8(MEMCTRL, 0xd7);
673     Data &= 0xF9;
674     Data |= (u8)((AccessAddr & 0x30000) >> 15);
675     pci_write_config8(MEMCTRL, 0xd7, Data);
676
677     //step15.  issue MRS cycle
678     Data=pci_read_config8(MEMCTRL, 0xd3);
679     Data &= 0x7F;
680     pci_write_config8(MEMCTRL, 0xd3, Data);
681     Data |=  0x80;
682     pci_write_config8(MEMCTRL, 0xd3, Data);
683
684     //clear the address
685     Data = 0x00;
686     pci_write_config8(MEMCTRL, 0xda, Data);
687
688      //step16.  all banks precharge command enable
689     Data=pci_read_config8(MEMCTRL, 0xd7);
690     Data &= 0xC7;
691     Data |= 0x10;
692     pci_write_config8(MEMCTRL, 0xd7, Data);
693
694
695    // step17. issue precharge all cycle
696     Data=pci_read_config8(MEMCTRL, 0xd3);
697     Data &= 0x7F;
698     pci_write_config8(MEMCTRL, 0xd3, Data);
699     Data |=  0x80;
700     pci_write_config8(MEMCTRL, 0xd3, Data);
701
702     //step18.  CBR cycle enable
703     Data=pci_read_config8(MEMCTRL, 0xd7);
704     Data &= 0xC7;
705     Data |= 0x20;
706     pci_write_config8(MEMCTRL, 0xd7, Data);
707
708     //step 19.20.21
709     //repeat issue 8 CBR cycle, between each cycle stop 100us
710     for (Idx = 0; Idx < 8; Idx++)
711     {
712          // issue CBR cycle
713     Data=pci_read_config8(MEMCTRL, 0xd3);
714     Data &= 0x7F;
715     pci_write_config8(MEMCTRL, 0xd3, Data);
716     Data |=  0x80;
717     pci_write_config8(MEMCTRL, 0xd3, Data);
718
719     WaitMicroSec(200);
720     }
721
722     //step22. MSR enable
723     Data=pci_read_config8(MEMCTRL, 0xd7);
724     Data &= 0xC7;
725     Data |= 0x18;
726     pci_write_config8(MEMCTRL, 0xd7, Data);
727
728     Data=pci_read_config8(MEMCTRL, 0xd3);
729     Data &= 0xC7;
730     Data |= 0x00;
731     pci_write_config8(MEMCTRL, 0xd3, Data);
732
733
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;
739
740     // CL = really offset RX62[2:0]
741     Data=pci_read_config8(MEMCTRL, 0x62);
742     CL = Data & 0x03;
743
744     AccessAddr  = (u32)(CHB_DDR2_MRS_table[CL]);
745     if (BL)
746     {
747         AccessAddr += 8;
748     }
749
750     //Write recovery  : really offset Rx63[7:5]
751     Data=pci_read_config8(MEMCTRL, 0x63);
752     Twr = (Data & 0xE0) >> 5;
753
754     AccessAddr += CHB_DDR2_Twr_table[Twr];
755     //MSR Address use addr[20:3]
756     AccessAddr >>= 3;
757
758    //step 23. MSR command
759     Data = (u8)(AccessAddr & 0xFF);
760     pci_write_config8(MEMCTRL, 0xD9, Data);
761
762     Data = (u8)((AccessAddr & 0xFF00) >> 8);
763     pci_write_config8(MEMCTRL, 0xda, Data);
764
765     Data=pci_read_config8(MEMCTRL, 0xd7);
766     Data &= 0xF9;
767     Data |= (u8)(((AccessAddr & 0x30000)>>16) << 1);
768     pci_write_config8(MEMCTRL, 0xd7, Data);
769
770      //step 24.  issue MRS cycle
771     Data=pci_read_config8(MEMCTRL, 0xd3);
772     Data &= 0x7F;
773     pci_write_config8(MEMCTRL, 0xd3, Data);
774     Data |=  0x80;
775     pci_write_config8(MEMCTRL, 0xd3, Data);
776
777     //step 25. EMRS enable
778     Data=pci_read_config8(MEMCTRL, 0xd7);
779     Data &= 0xC7;
780     Data |= 0x18;
781     pci_write_config8(MEMCTRL, 0xd7, Data);
782
783     Data=pci_read_config8(MEMCTRL, 0xd3);
784     Data &= 0xC7;
785     Data |= 0x08;
786     pci_write_config8(MEMCTRL, 0xd3, Data);
787
788
789     //step 26. OCD default
790      AccessAddr = (CHB_OCD_Default_150ohm) >> 3;
791     Data =(u8) (AccessAddr & 0xff);
792     pci_write_config8(MEMCTRL, 0xd9, Data);
793
794     Data = (u8)((AccessAddr & 0xff00) >> 8);
795     pci_write_config8(MEMCTRL, 0xda, Data);
796
797     Data=pci_read_config8(MEMCTRL, 0xd7);
798     Data &= 0xF9;
799     Data |= (u8)((AccessAddr & 0x30000) >> 15);
800     pci_write_config8(MEMCTRL, 0xd7, Data);
801
802     //step 27.  issue EMRS cycle
803     Data=pci_read_config8(MEMCTRL, 0xd3);
804     Data &= 0x7F;
805     pci_write_config8(MEMCTRL, 0xd3, Data);
806     Data |=  0x80;
807     pci_write_config8(MEMCTRL, 0xd3, Data);
808
809      //step 25. EMRS enable
810     Data=pci_read_config8(MEMCTRL, 0xd7);
811     Data &= 0xC7;
812     Data |= 0x18;
813     pci_write_config8(MEMCTRL, 0xd7, Data);
814
815     Data=pci_read_config8(MEMCTRL, 0xd3);
816     Data &= 0xC7;
817     Data |= 0x08;
818     pci_write_config8(MEMCTRL, 0xd3, Data);
819
820     //step 28. OCD Exit
821      AccessAddr = (CHB_OCD_Exit_150ohm) >> 3;
822      Data =(u8) (AccessAddr & 0xff);
823     pci_write_config8(MEMCTRL, 0xd9, Data);
824
825     Data = (u8)((AccessAddr & 0xff00) >> 8);
826     pci_write_config8(MEMCTRL, 0xda, Data);
827
828     Data=pci_read_config8(MEMCTRL, 0xd7);
829     Data &= 0xF9;
830     Data |= (u8)((AccessAddr & 0x30000) >> 15);
831     pci_write_config8(MEMCTRL, 0xd7, Data);
832
833      //step 29. issue EMRS cycle
834     Data=pci_read_config8(MEMCTRL, 0xd3);
835     Data &= 0x7F;
836     pci_write_config8(MEMCTRL, 0xd3, Data);
837     Data |=  0x80;
838     pci_write_config8(MEMCTRL, 0xd3, Data);
839
840     //clear  all the address
841     Data = 0x00;
842     pci_write_config8(MEMCTRL, 0xd9, Data);
843
844     Data = 0x00;
845     pci_write_config8(MEMCTRL, 0xda, Data);
846
847     Data=pci_read_config8(MEMCTRL, 0xd7);
848     Data &= 0xF9;
849     pci_write_config8(MEMCTRL, 0xd7, Data);
850
851     //step 30. normal SDRAM Mode
852     Data=pci_read_config8(MEMCTRL, 0xd7);
853     Data &= 0xC7;
854     Data |= 0x00;
855     pci_write_config8(MEMCTRL, 0xd7, Data);
856
857     Data=pci_read_config8(MEMCTRL, 0xd3);
858     Data &= 0xC7;
859     Data |= 0x00;
860     pci_write_config8(MEMCTRL, 0xd3, Data);
861
862     //step 31.  exit the initialization mode
863     Data=pci_read_config8(MEMCTRL, 0xd3);
864     Data &= 0xBF;
865     pci_write_config8(MEMCTRL, 0xd3, Data);
866
867
868     //step 32. Enable bank paging and multi page
869     Data=pci_read_config8(MEMCTRL, 0x69);
870     Data |= 0x03;
871     pci_write_config8(MEMCTRL, 0x69, Data);
872 }
873 */
874
875 /*===================================================================
876 Function   : InitDDR2CHC()
877 Precondition :
878 Input      :
879                    DramAttr:  pointer point to  DRAM_SYS_ATTR  which consist the DDR and Dimm information
880                                     in MotherBoard
881 Output     : Void
882 Purpose   : Initialize DDR2 of CHC by standard sequence
883 Reference  :
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
887
888 void InitDDR2CHC(DRAM_SYS_ATTR *DramAttr)
889 {
890         u8 Data, Idx, CL, Twr;
891         u32 AccessAddr;
892         CB_STATUS Status;
893
894         /* Step 3. Clear RxDF[2] to disable Tri-state output. */
895         Data = pci_read_config8(MEMCTRL, 0xdf);
896         Data &= 0xFB;
897         pci_write_config8(MEMCTRL, 0xdf, Data);
898
899         /*
900          * Step 4. Enable the initialization mode of DRAM Controller C with
901          * NB's PLL clock.
902          */
903         Data = pci_read_config8(MEMCTRL, 0xdb);
904         Data |= 0x60;
905         pci_write_config8(MEMCTRL, 0xdb, Data);
906
907         /* Step 5. NOP command enable. */
908         Data = pci_read_config8(MEMCTRL, 0xdb);
909         Data &= 0xE3;
910         Data |= 0x00;
911         pci_write_config8(MEMCTRL, 0xdb, Data);
912
913         /* Step 6. Issue a nop cycle, RegDB[1] 0 -> 1. */
914         Data = pci_read_config8(MEMCTRL, 0xdb);
915         Data |= 0x2;
916         pci_write_config8(MEMCTRL, 0xdb, Data);
917         Data &= 0xFD;
918         pci_write_config8(MEMCTRL, 0xdb, Data);
919
920         /*
921          * Step 7.
922          * A minimum pause of 200u sec will be provided after the NOP.
923          * - <<<    reduce BOOT UP time >>> -
924          * Loop 200us
925          */
926         for (Idx = 0; Idx < 0x10; Idx++)
927                 WaitMicroSec(100);
928
929         /* Step 8. Signal bank precharge command enable. */
930         Data = pci_read_config8(MEMCTRL, 0xdb);
931         Data &= 0xE3;
932         Data |= 0x14;
933         pci_write_config8(MEMCTRL, 0xdb, Data);
934
935         /* Set MA10 = 1, precharge all bank. */
936         Data = 0x00;
937         pci_write_config8(MEMCTRL, 0xf8, Data);
938
939         Data = 0x04;
940         pci_write_config8(MEMCTRL, 0xf9, Data);
941
942         /* step 9. Issue a precharge all cycle, RegD3[7] 0 -> 1. */
943         Data = pci_read_config8(MEMCTRL, 0xdb);
944         Data |= 0x2;
945         pci_write_config8(MEMCTRL, 0xdb, Data);
946         Data &= 0xFD;
947         pci_write_config8(MEMCTRL, 0xdb, Data);
948
949         /* Step 10. MRS enable. */
950         Data = pci_read_config8(MEMCTRL, 0xdb);
951         Data &= 0xE3;
952         Data |= 0x1C;
953         pci_write_config8(MEMCTRL, 0xdb, Data);
954
955         /* Step 11. EMRS DLL enable and Disable DQS. */
956         Data = 0x40;
957         pci_write_config8(MEMCTRL, 0xf8, Data);
958
959         Data = 0x24;
960         pci_write_config8(MEMCTRL, 0xf9, Data);
961
962         /* Step 12. Issue EMRS cycle. */
963         Data = pci_read_config8(MEMCTRL, 0xdb);
964         Data |= 0x2;
965         pci_write_config8(MEMCTRL, 0xdb, Data);
966         Data &= 0xFD;
967         pci_write_config8(MEMCTRL, 0xdb, Data);
968
969         /* Step 13. MSR enable. */
970         Data = pci_read_config8(MEMCTRL, 0xdb);
971         Data &= 0xE3;
972         Data |= 0x1C;
973         pci_write_config8(MEMCTRL, 0xdb, Data);
974
975         /* Step 14. MSR DLL Reset. */
976         Data = 0x00;
977         pci_write_config8(MEMCTRL, 0xf8, Data);
978
979         Data = 0x01;
980         pci_write_config8(MEMCTRL, 0xf9, Data);
981
982         /* Step 15. Issue MRS cycle. */
983         Data = pci_read_config8(MEMCTRL, 0xdb);
984         Data |= 0x2;
985         pci_write_config8(MEMCTRL, 0xdb, Data);
986         Data &= 0xFD;
987         pci_write_config8(MEMCTRL, 0xdb, Data);
988
989         /* Step 16. Signal banks precharge command enable. */
990         Data = pci_read_config8(MEMCTRL, 0xdb);
991         Data &= 0xE3;
992         Data |= 0x14;
993         pci_write_config8(MEMCTRL, 0xdb, Data);
994
995         /* Set MA10 = 1, precharge all bank. */
996         Data = 0x00;
997         pci_write_config8(MEMCTRL, 0xf8, Data);
998
999         Data = 0x04;
1000         pci_write_config8(MEMCTRL, 0xf9, Data);
1001
1002         /* Step 17. Issue precharge all cycle. */
1003         Data = pci_read_config8(MEMCTRL, 0xdb);
1004         Data |= 0x2;
1005         pci_write_config8(MEMCTRL, 0xdb, Data);
1006         Data &= 0xFD;
1007         pci_write_config8(MEMCTRL, 0xdb, Data);
1008
1009         /* Step 18. CBR cycle enable. */
1010         Data = pci_read_config8(MEMCTRL, 0xdb);
1011         Data &= 0xE3;
1012         Data |= 0x18;
1013         pci_write_config8(MEMCTRL, 0xdb, Data);
1014
1015         Data = 0x00;
1016         pci_write_config8(MEMCTRL, 0xf8, Data);
1017
1018         Data = 0x00;
1019         pci_write_config8(MEMCTRL, 0xf9, Data);
1020
1021         //step 19.20.21
1022         //repeat issue 8 CBR cycle, between each cycle stop 100us
1023         for (Idx = 0; Idx < 8; Idx++) {
1024                 // issue CBR cycle
1025                 Data = pci_read_config8(MEMCTRL, 0xdb);
1026                 Data |= 0x2;
1027                 pci_write_config8(MEMCTRL, 0xdb, Data);
1028                 Data &= 0xFD;
1029                 pci_write_config8(MEMCTRL, 0xdb, Data);
1030                 WaitMicroSec(100);
1031         }
1032
1033         //the SDRAM parameters.(, CAS# Latency , Write recovery etc.)
1034         //------------------------------------------------------------
1035
1036         // CL = really offset RXDC[7:6]
1037         Data = pci_read_config8(MEMCTRL, 0xdc);
1038         CL = (Data & 0xC0) >> 6;
1039
1040         AccessAddr = (u32) (CHC_MRS_table[CL]);
1041
1042         //Write recovery  : really offset Rx63[7:5]
1043         Data = pci_read_config8(MEMCTRL, 0x63);
1044         Twr = (Data & 0xE0) >> 5;
1045
1046         AccessAddr += Twr * 0x200;
1047
1048         //step22. MSR enable
1049         Data = pci_read_config8(MEMCTRL, 0xdb);
1050         Data &= 0xE3;
1051         Data |= 0x1C;
1052         pci_write_config8(MEMCTRL, 0xdb, Data);
1053
1054         //step 23. MSR command
1055         Data = (u8) (AccessAddr & 0xFF);
1056         pci_write_config8(MEMCTRL, 0xf8, Data);
1057
1058         Data = (u8) ((AccessAddr & 0xFF00) >> 8);
1059         pci_write_config8(MEMCTRL, 0xf9, Data);
1060
1061         //step 24.  issue MRS cycle
1062         Data = pci_read_config8(MEMCTRL, 0xdb);
1063         Data |= 0x2;
1064         pci_write_config8(MEMCTRL, 0xdb, Data);
1065         Data &= 0xFD;
1066         pci_write_config8(MEMCTRL, 0xdb, Data);
1067
1068         //step 25. EMRS enable
1069         Data = pci_read_config8(MEMCTRL, 0xdb);
1070         Data &= 0xE3;
1071         Data |= 0x1C;
1072         pci_write_config8(MEMCTRL, 0xdb, Data);
1073
1074         //step 26. OCD default
1075         Data = 0xC0;
1076         pci_write_config8(MEMCTRL, 0xf8, Data);
1077
1078         Data = 0x27;
1079         pci_write_config8(MEMCTRL, 0xf9, Data);
1080
1081         //step 27.  issue EMRS cycle
1082         Data = pci_read_config8(MEMCTRL, 0xdb);
1083         Data |= 0x2;
1084         pci_write_config8(MEMCTRL, 0xdb, Data);
1085         Data &= 0xFD;
1086         pci_write_config8(MEMCTRL, 0xdb, Data);
1087
1088         //step 28. OCD Exit
1089         Data = 0x40;
1090         pci_write_config8(MEMCTRL, 0xf8, Data);
1091
1092         Data = 0x24;
1093         pci_write_config8(MEMCTRL, 0xf9, Data);
1094
1095         //step 29. issue EMRS cycle
1096         Data = pci_read_config8(MEMCTRL, 0xdb);
1097         Data |= 0x2;
1098         pci_write_config8(MEMCTRL, 0xdb, Data);
1099         Data &= 0xFD;
1100         pci_write_config8(MEMCTRL, 0xdb, Data);
1101
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);
1107         Data &= 0x9F;
1108         pci_write_config8(MEMCTRL, 0xdb, Data);
1109 }
1110
1111 CB_STATUS VerifyChc(void)
1112 {
1113         u8 Data, ByteVal, Index, pad;
1114         u16 row;
1115
1116         //first write the pad to all the address
1117
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]
1120         pad = 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++) {
1124                         Data <<= 1;
1125                         if (Data == 0)
1126                                 Data = 1;
1127                         pci_write_config8(PCI_DEV(0, 0, 7), 0xC0 + Index, Data);
1128                 }
1129
1130                 /* Issue the bank active command. */
1131                 // bank active command enable
1132                 Data = pci_read_config8(MEMCTRL, 0xdb);
1133                 Data &= 0xE3;
1134                 Data |= 0x10;
1135                 pci_write_config8(MEMCTRL, 0xdb, Data);
1136
1137                 Data = (u8) (row && 0xFF);
1138                 pci_write_config8(MEMCTRL, 0xf8, Data);
1139
1140                 Data = (u8) ((row && 0xFF) >> 8);
1141                 pci_write_config8(MEMCTRL, 0xf9, Data);
1142
1143                 /* Issue active cycle. */
1144                 Data = pci_read_config8(MEMCTRL, 0xdb);
1145                 Data |= 0x2;
1146                 pci_write_config8(MEMCTRL, 0xdb, Data);
1147                 Data &= 0xFD;
1148                 pci_write_config8(MEMCTRL, 0xdb, Data);
1149
1150                 /* Issue ready/completion for read/write. */
1151                 // read/completion command enable
1152                 Data = pci_read_config8(MEMCTRL, 0xdb);
1153                 Data &= 0xE3;
1154                 Data |= 0x04;
1155                 pci_write_config8(MEMCTRL, 0xdb, Data);
1156
1157                 Data = 0x00;
1158                 pci_write_config8(MEMCTRL, 0xf8, Data);
1159
1160                 Data = 0x00;
1161                 pci_write_config8(MEMCTRL, 0xf9, Data);
1162
1163                 /* Issue read/completion cycle. */
1164                 Data = pci_read_config8(MEMCTRL, 0xdb);
1165                 Data |= 0x2;
1166                 pci_write_config8(MEMCTRL, 0xdb, Data);
1167                 Data &= 0xFD;
1168                 pci_write_config8(MEMCTRL, 0xdb, Data);
1169
1170                 /* Issue write command. */
1171                 // write command enable
1172                 Data = pci_read_config8(MEMCTRL, 0xdb);
1173                 Data &= 0xE3;
1174                 Data |= 0x0C;
1175                 pci_write_config8(MEMCTRL, 0xdb, Data);
1176
1177                 Data = 0x00;
1178                 pci_write_config8(MEMCTRL, 0xf8, Data);
1179
1180                 Data = (u8) ((row & 0x60) << 5);
1181                 pci_write_config8(MEMCTRL, 0xf9, Data);
1182
1183                 /* Issue write cycle. */
1184                 Data = pci_read_config8(MEMCTRL, 0xdb);
1185                 Data |= 0x2;
1186                 pci_write_config8(MEMCTRL, 0xdb, Data);
1187                 Data &= 0xFD;
1188                 pci_write_config8(MEMCTRL, 0xdb, Data);
1189
1190                 ////issue ready/completion for read/write
1191                 // read/completion command enable
1192                 Data = pci_read_config8(MEMCTRL, 0xdb);
1193                 Data &= 0xE3;
1194                 Data |= 0x04;
1195                 pci_write_config8(MEMCTRL, 0xdb, Data);
1196
1197                 Data = 0x00;
1198                 pci_write_config8(MEMCTRL, 0xf8, Data);
1199
1200                 Data = 0x00;
1201                 pci_write_config8(MEMCTRL, 0xf9, Data);
1202
1203                 /* Issue read/completion cycle. */
1204                 Data = pci_read_config8(MEMCTRL, 0xdb);
1205                 Data |= 0x2;
1206                 pci_write_config8(MEMCTRL, 0xdb, Data);
1207                 Data &= 0xFD;
1208                 pci_write_config8(MEMCTRL, 0xdb, Data);
1209
1210                 /* Issue the bank active command. */
1211                 // bank active command enable
1212                 Data = pci_read_config8(MEMCTRL, 0xdb);
1213                 Data &= 0xE3;
1214                 Data |= 0x10;
1215                 pci_write_config8(MEMCTRL, 0xdb, Data);
1216
1217                 Data = (u8) (row && 0xFF);
1218                 pci_write_config8(MEMCTRL, 0xf8, Data);
1219
1220                 Data = (u8) ((row && 0xFF) >> 8);
1221                 pci_write_config8(MEMCTRL, 0xf9, Data);
1222
1223                 //  issue active cycle
1224                 Data = pci_read_config8(MEMCTRL, 0xdb);
1225                 Data |= 0x2;
1226                 pci_write_config8(MEMCTRL, 0xdb, Data);
1227                 Data &= 0xFD;
1228                 pci_write_config8(MEMCTRL, 0xdb, Data);
1229
1230                 ////issue ready/completion for read/write
1231                 // read/completion command enable
1232                 Data = pci_read_config8(MEMCTRL, 0xdb);
1233                 Data &= 0xE3;
1234                 Data |= 0x04;
1235                 pci_write_config8(MEMCTRL, 0xdb, Data);
1236
1237                 Data = 0x00;
1238                 pci_write_config8(MEMCTRL, 0xf8, Data);
1239
1240                 Data = 0x00;
1241                 pci_write_config8(MEMCTRL, 0xf9, Data);
1242
1243                 //  issue read/completion cycle
1244                 Data = pci_read_config8(MEMCTRL, 0xdb);
1245                 Data |= 0x2;
1246                 pci_write_config8(MEMCTRL, 0xdb, Data);
1247                 Data &= 0xFD;
1248                 pci_write_config8(MEMCTRL, 0xdb, Data);
1249
1250                 ////issue read command
1251                 // read/completion command enable
1252                 Data = pci_read_config8(MEMCTRL, 0xdb);
1253                 Data &= 0xE3;
1254                 Data |= 0x08;
1255                 pci_write_config8(MEMCTRL, 0xdb, Data);
1256
1257                 Data = 0x00;
1258                 pci_write_config8(MEMCTRL, 0xf8, Data);
1259
1260                 Data = (u8) ((row & 0x60) << 5);
1261                 pci_write_config8(MEMCTRL, 0xf9, Data);
1262
1263                 //  issue read cycle
1264                 Data = pci_read_config8(MEMCTRL, 0xdb);
1265                 Data |= 0x2;
1266                 pci_write_config8(MEMCTRL, 0xdb, Data);
1267                 Data &= 0xFD;
1268                 pci_write_config8(MEMCTRL, 0xdb, Data);
1269
1270                 ////issue ready/completion for read/write
1271                 // read/completion command enable
1272                 Data = pci_read_config8(MEMCTRL, 0xdb);
1273                 Data &= 0xE3;
1274                 Data |= 0x04;
1275                 pci_write_config8(MEMCTRL, 0xdb, Data);
1276
1277                 Data = 0x00;
1278                 pci_write_config8(MEMCTRL, 0xf8, Data);
1279
1280                 Data = 0x00;
1281                 pci_write_config8(MEMCTRL, 0xf9, Data);
1282
1283                 /* Issue read/completion cycle. */
1284                 Data = pci_read_config8(MEMCTRL, 0xdb);
1285                 Data |= 0x2;
1286                 pci_write_config8(MEMCTRL, 0xdb, Data);
1287                 Data &= 0xFD;
1288                 pci_write_config8(MEMCTRL, 0xdb, Data);
1289
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");
1296                         }
1297                         ByteVal <<= 1;
1298                         if (ByteVal == 0)
1299                                 ByteVal = 1;
1300                 }
1301                 pad <<= 1;
1302                 if (pad == 0)
1303                         pad = 1;
1304         }
1305
1306         return CB_SUCCESS;
1307 }