AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / Mem / Tech / mttml.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * mttml.c
6  *
7  * Technology Max Latency Training support
8  *
9  * @xrefitem bom "File Content Label" "Release Content"
10  * @e project: AGESA
11  * @e sub-project: (Mem/Tech)
12  * @e \$Revision: 56279 $ @e \$Date: 2011-07-11 13:11:28 -0600 (Mon, 11 Jul 2011) $
13  *
14  **/
15 /*****************************************************************************
16 *
17 * Copyright (C) 2012 Advanced Micro Devices, Inc.
18 * All rights reserved.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions are met:
22 *     * Redistributions of source code must retain the above copyright
23 *       notice, this list of conditions and the following disclaimer.
24 *     * Redistributions in binary form must reproduce the above copyright
25 *       notice, this list of conditions and the following disclaimer in the
26 *       documentation and/or other materials provided with the distribution.
27 *     * Neither the name of Advanced Micro Devices, Inc. nor the names of
28 *       its contributors may be used to endorse or promote products derived
29 *       from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY
35 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * ***************************************************************************
43 *
44 */
45
46 /*
47  *----------------------------------------------------------------------------
48  *                                MODULES USED
49  *
50  *----------------------------------------------------------------------------
51  */
52
53
54
55 #include "AGESA.h"
56 #include "Ids.h"
57 #include "mm.h"
58 #include "mn.h"
59 #include "mt.h"
60 #include "merrhdl.h"
61 #include "GeneralServices.h"
62 #include "Filecode.h"
63 CODE_GROUP (G1_PEICC)
64 RDATA_GROUP (G2_PEI)
65
66 #define FILECODE PROC_MEM_TECH_MTTML_FILECODE
67 /*----------------------------------------------------------------------------
68  *                          DEFINITIONS AND MACROS
69  *
70  *----------------------------------------------------------------------------
71  */
72
73 /*----------------------------------------------------------------------------
74  *                           TYPEDEFS AND STRUCTURES
75  *
76  *----------------------------------------------------------------------------
77  */
78
79 /*----------------------------------------------------------------------------
80  *                        PROTOTYPES OF LOCAL FUNCTIONS
81  *
82  *----------------------------------------------------------------------------
83  */
84
85 /*----------------------------------------------------------------------------
86  *                            EXPORTED FUNCTIONS
87  *
88  *----------------------------------------------------------------------------
89  */
90 /* -----------------------------------------------------------------------------*/
91 /**
92  *
93  *      This function trains Max latency for all dies
94  *
95  *     @param[in,out]   *TechPtr   - Pointer to the MEM_TECH_BLOCK
96  *
97  *     @return          TRUE -  No fatal error occurs.
98  *     @return          FALSE - Fatal error occurs.
99  */
100
101 BOOLEAN
102 MemTTrainMaxLatency (
103   IN OUT   MEM_TECH_BLOCK *TechPtr
104   )
105 {
106   UINT32 TestAddrRJ16;
107   UINT8 Dct;
108   UINT8 ChipSel;
109   UINT8  *PatternBufPtr;
110   UINT8  *TestBufferPtr;
111   UINT8  CurrentNbPstate;
112   UINT16 CalcMaxLatDly;
113   UINT16 MaxLatDly;
114   UINT16 MaxLatLimit;
115   UINT16 Margin;
116   UINT16 CurTest;
117   UINT16 _CL_;
118   UINT8 TimesFail;
119   UINT8 TimesRetrain;
120   UINT16 i;
121
122   MEM_DATA_STRUCT *MemPtr;
123   DIE_STRUCT *MCTPtr;
124   MEM_NB_BLOCK  *NBPtr;
125
126   NBPtr = TechPtr->NBPtr;
127   MCTPtr = NBPtr->MCTPtr;
128   MemPtr = NBPtr->MemPtr;
129   TechPtr->TrainingType = TRN_MAX_READ_LATENCY;
130   TimesRetrain = DEFAULT_TRAINING_TIMES;
131   IDS_OPTION_HOOK (IDS_MEM_RETRAIN_TIMES, &TimesRetrain, &MemPtr->StdHeader);
132
133   IDS_HDT_CONSOLE (MEM_STATUS, "\nStart MaxRdLat training\n");
134   // Set environment settings before training
135   AGESA_TESTPOINT (TpProcMemMaxRdLatencyTraining, &(MemPtr->StdHeader));
136   MemTBeginTraining (TechPtr);
137   //
138   // Initialize the Training Pattern
139   //
140   if (AGESA_SUCCESS != NBPtr->TrainingPatternInit (NBPtr)) {
141     return (BOOLEAN) (MCTPtr->ErrCode < AGESA_FATAL);
142   }
143   TechPtr->PatternLength = (MCTPtr->Status[Sb128bitmode]) ? 6 : 3;
144   //
145   // Setup hardware training engine (if applicable)
146   //
147   NBPtr->FamilySpecificHook[SetupHwTrainingEngine] (NBPtr, &TechPtr->TrainingType);
148
149   MaxLatDly = 0;
150   _CL_ = TechPtr->PatternLength;
151   PatternBufPtr = TechPtr->PatternBufPtr;
152   TestBufferPtr = TechPtr->TestBufPtr;
153   //
154   // Begin max latency training
155   //
156   for (Dct = 0; Dct < NBPtr->DctCount; Dct++) {
157     if (MCTPtr->Status[Sb128bitmode] && (Dct != 0)) {
158       break;
159     }
160
161     IDS_HDT_CONSOLE (MEM_STATUS, "\tDct %d\n", Dct);
162     NBPtr->SwitchDCT (NBPtr, Dct);
163
164     if (NBPtr->DCTPtr->Timings.DctMemSize != 0) {
165       if (TechPtr->FindMaxDlyForMaxRdLat (TechPtr, &ChipSel)) {
166         TechPtr->ChipSel = ChipSel;
167         if (NBPtr->GetSysAddr (NBPtr, ChipSel, &TestAddrRJ16)) {
168           IDS_HDT_CONSOLE (MEM_STATUS, "\t\tCS %d\n", ChipSel);
169           IDS_HDT_CONSOLE (MEM_FLOW, "\t\t\tWrite to address: %04x0000\n", TestAddrRJ16);
170
171           // Write the test patterns
172           AGESA_TESTPOINT (TpProcMemMaxRdLatWritePattern, &(MemPtr->StdHeader));
173           NBPtr->WritePattern (NBPtr, TestAddrRJ16, PatternBufPtr, _CL_);
174
175           // Sweep max latency delays
176           NBPtr->getMaxLatParams (NBPtr, TechPtr->MaxDlyForMaxRdLat, &CalcMaxLatDly, &MaxLatLimit, &Margin);
177           AGESA_TESTPOINT (TpProcMemMaxRdLatStartSweep, &(MemPtr->StdHeader));
178
179           TimesFail = 0;
180           ERROR_HANDLE_RETRAIN_BEGIN (TimesFail, TimesRetrain)
181           {
182             MaxLatDly = CalcMaxLatDly;
183             for (i = 0; i < (MaxLatLimit - CalcMaxLatDly); i++) {
184               NBPtr->SetBitField (NBPtr, BFMaxLatency, MaxLatDly);
185               IDS_HDT_CONSOLE (MEM_FLOW, "\t\t\tDly %3x", MaxLatDly);
186               TechPtr->ResetDCTWrPtr (TechPtr, 6);
187
188               AGESA_TESTPOINT (TpProcMemMaxRdLatReadPattern, &(MemPtr->StdHeader));
189               NBPtr->ReadPattern (NBPtr, TestBufferPtr, TestAddrRJ16, _CL_);
190               AGESA_TESTPOINT (TpProcMemMaxRdLatTestPattern, &(MemPtr->StdHeader));
191               CurTest = NBPtr->CompareTestPattern (NBPtr, TestBufferPtr, PatternBufPtr, _CL_ * 64);
192               NBPtr->FlushPattern (NBPtr, TestAddrRJ16, _CL_);
193
194               if (NBPtr->IsSupported[ReverseMaxRdLatTrain]) {
195                 // Reverse training decrements MaxLatDly whenever the test passes
196                 // and uses the last passing MaxLatDly as left edge
197                 if (CurTest == 0xFFFF) {
198                   IDS_HDT_CONSOLE (MEM_FLOW, "  P");
199                   if (MaxLatDly == 0) {
200                     break;
201                   } else {
202                     MaxLatDly--;
203                   }
204                 }
205               } else {
206                 // Traditional training increments MaxLatDly until the test passes
207                 // and uses it as left edge
208                 if (CurTest == 0xFFFF) {
209                   IDS_HDT_CONSOLE (MEM_FLOW, "  P");
210                   break;
211                 } else {
212                   MaxLatDly++;
213                 }
214               }
215               IDS_HDT_CONSOLE (MEM_FLOW, "\n");
216             }// End of delay sweep
217             ERROR_HANDLE_RETRAIN_END ((MaxLatDly >= MaxLatLimit), TimesFail)
218           }
219
220           AGESA_TESTPOINT (TpProcMemMaxRdLatSetDelay, &(MemPtr->StdHeader));
221
222           if (MaxLatDly >= MaxLatLimit) {
223             PutEventLog (AGESA_ERROR, MEM_ERROR_MAX_LAT_NO_WINDOW, NBPtr->Node, NBPtr->Dct, NBPtr->Channel, 0, &NBPtr->MemPtr->StdHeader);
224             SetMemError (AGESA_ERROR, MCTPtr);
225             NBPtr->DCTPtr->Timings.CsTrainFail |= NBPtr->DCTPtr->Timings.CsPresent;
226             MCTPtr->ChannelTrainFail |= (UINT32)1 << Dct;
227             if (!NBPtr->MemPtr->ErrorHandling (MCTPtr, NBPtr->Dct, EXCLUDE_ALL_CHIPSEL, &NBPtr->MemPtr->StdHeader)) {
228               ASSERT (FALSE);
229               return FALSE;
230             }
231           } else {
232             NBPtr->FamilySpecificHook[AddlMaxRdLatTrain] (NBPtr, &TestAddrRJ16);
233
234             MaxLatDly = MaxLatDly + Margin;
235             if (NBPtr->IsSupported[ReverseMaxRdLatTrain]) {
236               MaxLatDly++;  // Add 1 to get back to the last passing value
237             }
238             // Set final delays
239             CurrentNbPstate = (UINT8) MemNGetBitFieldNb (NBPtr, BFCurNbPstate);
240             ASSERT (CurrentNbPstate <= 3);
241             NBPtr->ChannelPtr->DctMaxRdLat [CurrentNbPstate] = MaxLatDly;
242             NBPtr->SetBitField (NBPtr, BFMaxLatency, MaxLatDly);
243             IDS_HDT_CONSOLE (MEM_FLOW, "\t\tFinal MaxRdLat: %03x\n", MaxLatDly);
244
245           }
246         }
247       }
248     }
249   }
250
251   // Restore environment settings after training
252   MemTEndTraining (TechPtr);
253   IDS_HDT_CONSOLE (MEM_FLOW, "End MaxRdLat training\n\n");
254   //
255   // Finalize the Pattern
256   //
257   NBPtr->TrainingPatternFinalize (NBPtr);
258   return (BOOLEAN) (MCTPtr->ErrCode < AGESA_FATAL);
259 }