AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / Mem / NB / DA / mndctda.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * mndctda.c
6  *
7  * Northbridge DA DCT supporting functions
8  *
9  * @xrefitem bom "File Content Label" "Release Content"
10  * @e project: AGESA
11  * @e sub-project: (Mem/NB/DA)
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  *----------------------------------------------------------------------------
49  *                                MODULES USED
50  *
51  *----------------------------------------------------------------------------
52  */
53
54
55
56 #include "AGESA.h"
57 #include "amdlib.h"
58 #include "Ids.h"
59 #include "mm.h"
60 #include "mn.h"
61 #include "mt.h"
62 #include "OptionMemory.h"       // need def for MEM_FEAT_BLOCK_NB
63 #include "mnda.h"
64 #include "merrhdl.h"
65 #include "cpuRegisters.h"
66 #include "Filecode.h"
67 #include "GeneralServices.h"
68 #include "cpuFamilyTranslation.h"
69 #include "cpuRegisters.h"
70 #include "mport.h"
71 #include "F10PackageType.h"
72 CODE_GROUP (G1_PEICC)
73 RDATA_GROUP (G2_PEI)
74
75 #define FILECODE PROC_MEM_NB_DA_MNDCTDA_FILECODE
76 /*----------------------------------------------------------------------------
77  *                          DEFINITIONS AND MACROS
78  *
79  *----------------------------------------------------------------------------
80  */
81 #define UNUSED_CLK 4
82
83 /*----------------------------------------------------------------------------
84  *                           TYPEDEFS AND STRUCTURES
85  *
86  *----------------------------------------------------------------------------
87  */
88
89 /*----------------------------------------------------------------------------
90  *                        PROTOTYPES OF LOCAL FUNCTIONS
91  *
92  *----------------------------------------------------------------------------
93  */
94
95
96
97 /*----------------------------------------------------------------------------
98  *                            EXPORTED FUNCTIONS
99  *
100  *----------------------------------------------------------------------------
101  */
102
103 extern BUILD_OPT_CFG UserOptions;
104
105 /* -----------------------------------------------------------------------------*/
106 /**
107  *
108  *
109  *   This is a general purpose function that executes before DRAM init
110  *
111  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
112  *
113  */
114
115 VOID
116 MemNBeforeDramInitDA (
117   IN OUT   MEM_NB_BLOCK *NBPtr
118   )
119 {
120   UINT8 Dct;
121
122   //Setting the reset value of Phy DLL standby and shutdown registers.
123   for (Dct = 0; Dct < MAX_DCTS_PER_NODE_DA; Dct++) {
124     MemNSwitchDCTNb (NBPtr, Dct);
125     if (NBPtr->DCTPtr->Timings.DctMemSize != 0) {
126       //Set PllLockTime and DllLockTime to default.
127       MemNSetBitFieldNb (NBPtr, BFPhyPLLLockTime, 0x000007D0);
128       MemNSetBitFieldNb (NBPtr, BFPhyDLLLockTime, 0x00000190);
129       MemNSetBitFieldNb (NBPtr, BFDisDllShutdownSR, 1);
130
131       //Clear PHY PLL Control Register before doing fence training and reset DLL
132       MemNSetBitFieldNb (NBPtr, BFPhy0x0D080F0C, 0x00002000);
133       MemNSetBitFieldNb (NBPtr, BFPhyDLLControl, 0);
134       MemNSetBitFieldNb (NBPtr, BFPhy0x0D080F0C, 0);
135     }
136   }
137 }
138
139 /* -----------------------------------------------------------------------------*/
140 /**
141  *
142  *
143  *      This function programs the memory controller with configuration parameters
144  *
145  *
146  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
147  *
148  *     @return          TRUE - An Error value lower than AGESA_FATAL may have occurred
149  *     @return          FALSE - An Error value greater than or equal to AGESA_FATAL may have occurred
150  *     @return          NBPtr->MCTPtr->ErrCode - Contains detailed AGESA_STATUS value
151  */
152
153 BOOLEAN
154 memNAutoConfigDA (
155   IN OUT   MEM_NB_BLOCK *NBPtr
156   )
157 {
158   DIE_STRUCT *MCTPtr;
159   DCT_STRUCT *DCTPtr;
160   MEM_PARAMETER_STRUCT *RefPtr;
161
162   RefPtr = NBPtr->RefPtr;
163   MCTPtr = NBPtr->MCTPtr;
164   DCTPtr = NBPtr->DCTPtr;
165   //======================================================================
166   // Build Dram Control Register Value (F2x78)
167   //======================================================================
168   //
169
170   MemNSetBitFieldNb (NBPtr, BFEarlyArbEn, 1);
171
172   //======================================================================
173   // Build Dram Config Lo Register Value
174   //======================================================================
175   //
176
177   if (MCTPtr->Status[SbParDimms]) {
178     //
179     // SbParDimms should be set for all DDR3 RDIMMS
180     // Cannot turn off ParEn for DDR3
181     //
182     //@attention - add debug option for parity control
183     MemNSetBitFieldNb (NBPtr, BFParEn, 1);
184   }
185
186
187   if (MCTPtr->GangedMode) {
188     MemNSetBitFieldNb (NBPtr, BFWidth128, 1);
189   }
190
191   MemNSetBitFieldNb (NBPtr, BFX4Dimm, DCTPtr->Timings.Dimmx4Present & 0xF);
192
193   if (!MCTPtr->Status[SbRegistered]) {
194     MemNSetBitFieldNb (NBPtr, BFUnBuffDimm, 1);
195   }
196
197   if (MCTPtr->Status[SbEccDimms]) {
198     MemNSetBitFieldNb (NBPtr, BFDimmEccEn, 1);
199   }
200
201  //======================================================================
202   // Build Dram Config Hi Register Value
203   //======================================================================
204   //
205
206   MemNSetBitFieldNb (NBPtr, BFMemClkFreq, MemNGetMemClkFreqIdNb (NBPtr, DCTPtr->Timings.Speed));
207
208   if (MCTPtr->Status[SbRegistered]) {
209     if (DCTPtr->Timings.Dimmx4Present && DCTPtr->Timings.Dimmx8Present) {
210       MemNSetBitFieldNb (NBPtr, BFRDqsEn, 1);
211     }
212   }
213
214   if (RefPtr->EnableBankSwizzle) {
215     MemNSetBitFieldNb (NBPtr, BFBankSwizzleMode, 1);
216   }
217
218   if (DCTPtr->Timings.DimmQrPresent) {
219     if (UserOptions.CfgMemoryQuadrankType == QUADRANK_UNBUFFERED) {
220       MemNSetBitFieldNb (NBPtr, BFFourRankSoDimm, 1);
221     } else if (UserOptions.CfgMemoryQuadrankType == QUADRANK_REGISTERED) {
222       MemNSetBitFieldNb (NBPtr, BFFourRankRDimm, 1);
223     }
224   }
225
226   MemNSetBitFieldNb (NBPtr, BFDcqBypassMax, 0xF);
227
228   MemNSetBitFieldNb (NBPtr, BFDcqArbBypassEn, 1);
229
230   //======================================================================
231   // Build Dram Config Misc Register Value
232   //======================================================================
233   //
234   MemNSetBitFieldNb (NBPtr, BFOdtSwizzle, 1);
235   // For DDR3 Registered Dimms
236   if (MCTPtr->Status[SbRegistered]) {
237     if (MemNGetBitFieldNb (NBPtr, BFDdr3Mode)!= 0) {
238       MemNSetBitFieldNb (NBPtr, BFSubMemclkRegDly, 1);
239     }
240   }
241   return (BOOLEAN) (MCTPtr->ErrCode < AGESA_FATAL);
242 }
243
244 /* -----------------------------------------------------------------------------*/
245 /**
246  *
247  *
248  *   This function sends an MRS command
249  *
250  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
251  *
252  */
253
254 VOID
255 MemNSendMrsCmdDA (
256   IN OUT   MEM_NB_BLOCK *NBPtr
257   )
258 {
259   MemNSwapBitsNb (NBPtr);
260
261   IDS_HDT_CONSOLE (MEM_FLOW, "\t\t\tCS%d MR%d %04x\n",
262               (MemNGetBitFieldNb (NBPtr, BFDramInitRegReg) >> 20) & 0xF,
263               (MemNGetBitFieldNb (NBPtr, BFDramInitRegReg) >> 16) & 0xF,
264               (MemNGetBitFieldNb (NBPtr, BFDramInitRegReg) & 0xFFFF));
265
266   // 1.Set SendMrsCmd=1
267   MemNSetBitFieldNb (NBPtr, BFSendMrsCmd, 1);
268
269   // 2.Wait for SendMrsCmd=0
270   MemNPollBitFieldNb (NBPtr, BFSendMrsCmd, 0, PCI_ACCESS_TIMEOUT, FALSE);
271 }
272
273 /* -----------------------------------------------------------------------------*/
274 /**
275  *
276  *
277  *   Workaround for erratum 322 and 263
278  *
279  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
280  *
281  */
282 VOID
283 MemNBeforePlatformSpecDA (
284   IN OUT   MEM_NB_BLOCK *NBPtr
285   )
286 {
287   // Errata 263
288   if ((NBPtr->DCTPtr->Timings.Speed == DDR533_FREQUENCY) || (NBPtr->DCTPtr->Timings.Speed == DDR667_FREQUENCY)) {
289     MemNSetBitFieldNb (NBPtr, BFErr263, 0x0800);
290   } else {
291     MemNSetBitFieldNb (NBPtr, BFErr263, 0);
292   }
293
294   // Errata 322
295   // 1.Write 00000000h to F2x[1,0]9C_xD08E000
296   MemNSetBitFieldNb (NBPtr, BFErr322I, 0);
297   // 2.If DRAM Configuration Register[MemClkFreq] (F2x[1,0]94[2:0]) is
298   //    greater than or equal to 011b (DDR-800 and higher),
299   //    then write 00000080h to F2x[1,0]9C_xD02E001,
300   //    else write 00000090h to F2x[1,0]9C_xD02E001.
301   MemNSetBitFieldNb (NBPtr, BFErr322II, (NBPtr->DCTPtr->Timings.Speed >= DDR800_FREQUENCY) ? 0x80 : 0x90);
302 }
303
304 /* -----------------------------------------------------------------------------*/
305 /**
306  *
307  *
308  *   Change Average Value of 3
309  *
310  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
311  *
312  */
313 BOOLEAN
314 MemNChangeAvgValue3DA (
315   IN OUT   MEM_NB_BLOCK *NBPtr
316   )
317 {
318   if ((NBPtr->MCTPtr->LogicalCpuid.Revision & AMD_F10_C0) != 0) {
319     return TRUE;
320   } else {
321     return FALSE;
322   }
323 }
324
325 /**
326  *
327  *
328  *   Change Average Value of 8
329  *
330  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
331  *
332  */
333 BOOLEAN
334 MemNChangeAvgValue8DA (
335   IN OUT   MEM_NB_BLOCK *NBPtr
336   )
337 {
338   if ((NBPtr->MCTPtr->LogicalCpuid.Revision & AMD_F10_C1) != 0) {
339     return TRUE;
340   } else {
341     return FALSE;
342   }
343 }
344 /* -----------------------------------------------------------------------------*/
345 /**
346  *
347  *
348  *   Enable DLL Shut down
349  *
350  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
351  *
352  */
353 VOID
354 MemNEnDLLShutDownDA (
355   IN OUT   MEM_NB_BLOCK *NBPtr
356   )
357 {
358   UINT8 Dct;
359   BOOLEAN DllShutDownEn;
360
361   DllShutDownEn = TRUE;
362   IDS_OPTION_HOOK (IDS_DLL_SHUT_DOWN, &DllShutDownEn, &(NBPtr->MemPtr->StdHeader));
363
364   if (DllShutDownEn && NBPtr->IsSupported[SetDllShutDown]) {
365     for (Dct = 0; Dct < NBPtr->DctCount; Dct++) {
366       MemNSwitchDCTNb (NBPtr, Dct);
367       if (NBPtr->DCTPtr->Timings.DctMemSize != 0) {
368         MemNSetBitFieldNb (NBPtr, BFPhyPLLLockTime, 0x0000001C);
369         MemNSetBitFieldNb (NBPtr, BFPhyDLLLockTime, 0x0000013D);
370         MemNSetBitFieldNb (NBPtr, BFDisDllShutdownSR, 0);
371       }
372     }
373   }
374 }
375
376 /* -----------------------------------------------------------------------------*/
377 /**
378  *
379  *
380  *   This function caps speed based on batter life check.
381  *
382  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
383  */
384 VOID
385 MemNCapSpeedBatteryLifeDA (
386   IN OUT   MEM_NB_BLOCK *NBPtr
387   )
388 {
389   CONST UINT16 SupportedFreq[] = {
390     DDR1600_FREQUENCY,
391     DDR1333_FREQUENCY,
392     DDR1066_FREQUENCY,
393     DDR800_FREQUENCY,
394     DDR667_FREQUENCY,
395     DDR533_FREQUENCY,
396     DDR400_FREQUENCY
397   };
398   UINT32 NBFreq;
399   UINT8 j;
400   UINT16 DdrFreq;
401   CPU_SPECIFIC_SERVICES   *FamilySpecificServices;
402   UINT32                  ProcessorPackageType;
403
404   FamilySpecificServices = NULL;
405   DdrFreq = DDR800_FREQUENCY; // Set Default to be 400Mhz
406   ProcessorPackageType = LibAmdGetPackageType (&(NBPtr->MemPtr->StdHeader));
407   GetCpuServicesOfSocket (NBPtr->MCTPtr->SocketId, (const CPU_SPECIFIC_SERVICES **)&FamilySpecificServices, &(NBPtr->MemPtr->StdHeader));
408   if (FamilySpecificServices->IsNbPstateEnabled (FamilySpecificServices, NBPtr->MemPtr->PlatFormConfig, &(NBPtr->MemPtr->StdHeader))) {
409     NBFreq = (MemNGetBitFieldNb (NBPtr, BFNbFid) + 4) * 100;  // Calculate the Nb P1 frequency (NbFreq / 2)
410     for (j = 0; j < GET_SIZE_OF (SupportedFreq); j++) {
411       if (NBFreq >= ((UINT32) 2 * SupportedFreq[j])) {
412         // Pick Max MEMCLK that is less than or equal to (NCLK_P1 / 2)
413         DdrFreq = SupportedFreq[j];
414         break;
415       }
416     }
417     if (NBPtr->MemPtr->PlatFormConfig->PlatformProfile.PlatformPowerPolicy == BatteryLife) {
418       if (NBPtr->DCTPtr->Timings.TargetSpeed > DdrFreq) {
419         NBPtr->DCTPtr->Timings.TargetSpeed = DdrFreq;
420       }
421     } else {
422       PutEventLog (AGESA_WARNING, MEM_WARNING_PERFORMANCE_ENABLED_BATTERY_LIFE_PREFERRED, NBPtr->Node, NBPtr->Dct, NBPtr->Channel, 0, &NBPtr->MemPtr->StdHeader);
423       DdrFreq = DDR800_FREQUENCY; // Set Default to be 400Mhz
424       NBFreq = (MemNGetBitFieldNb (NBPtr, BFNbFid) + 4) * 200;  // Calculate the Nb P0 frequency
425       for (j = 0; j < GET_SIZE_OF (SupportedFreq); j++) {
426         if (NBFreq >= ((UINT32) 2 * SupportedFreq[j])) {
427           // Pick Max MEMCLK that is less than or equal to (NCLK_P0 / 2)
428           DdrFreq = SupportedFreq[j];
429           break;
430         }
431       }
432       if (NBPtr->DCTPtr->Timings.TargetSpeed > DdrFreq) {
433         NBPtr->DCTPtr->Timings.TargetSpeed = DdrFreq;
434       }
435     }
436     if (((NBPtr->MCTPtr->LogicalCpuid.Revision & AMD_F10_C3) != 0) && (ProcessorPackageType == PACKAGE_TYPE_S1G3_S1G4 || ProcessorPackageType == PACKAGE_TYPE_ASB2)) {
437       MemNSetBitFieldNb (NBPtr, BFRdPtrInit, 4);
438       MemNSetBitFieldNb (NBPtr, BFDataTxFifoWrDly, 1);
439     } else {
440       MemNSetBitFieldNb (NBPtr, BFRdPtrInit, 6);
441       MemNSetBitFieldNb (NBPtr, BFDataTxFifoWrDly, 0);
442     }
443   } else {
444     NBFreq = (MemNGetBitFieldNb (NBPtr, BFNbFid) + 4) * 200;  // Calculate the Nb P0 frequency
445     for (j = 0; j < GET_SIZE_OF (SupportedFreq); j++) {
446       if (NBFreq >= ((UINT32) 2 * SupportedFreq[j])) {
447         // Pick Max MEMCLK that is less than or equal to (NCLK_P0 / 2)
448         DdrFreq = SupportedFreq[j];
449         break;
450       }
451     }
452     if (NBPtr->DCTPtr->Timings.TargetSpeed > DdrFreq) {
453       NBPtr->DCTPtr->Timings.TargetSpeed = DdrFreq;
454     }
455     MemNSetBitFieldNb (NBPtr, BFRdPtrInit, 6);
456     MemNSetBitFieldNb (NBPtr, BFDataTxFifoWrDly, 0);
457   }
458 }
459
460 /* -----------------------------------------------------------------------------*/
461 /**
462  *
463  *
464 /*----------------------------------------------------------------------------
465  *                              LOCAL FUNCTIONS
466  *
467  *----------------------------------------------------------------------------
468  */