AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / Mem / NB / HY / mndcthy.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * mndctHy.c
6  *
7  * Northbridge DCT support for Hydra
8  *
9  * @xrefitem bom "File Content Label" "Release Content"
10  * @e project: AGESA
11  * @e sub-project: (Mem/NB/HY)
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 "mu.h"
63 #include "OptionMemory.h"       // need def for MEM_FEAT_BLOCK_NB
64 #include "mnhy.h"
65 #include "merrhdl.h"
66 #include "cpuFamRegisters.h"
67 #include "Filecode.h"
68 CODE_GROUP (G1_PEICC)
69 RDATA_GROUP (G2_PEI)
70
71 #define FILECODE PROC_MEM_NB_HY_MNDCTHY_FILECODE
72
73 /*----------------------------------------------------------------------------
74  *                          DEFINITIONS AND MACROS
75  *
76  *----------------------------------------------------------------------------
77  */
78 #define UNUSED_CLK 4
79 /*----------------------------------------------------------------------------
80  *                           TYPEDEFS AND STRUCTURES
81  *
82  *----------------------------------------------------------------------------
83  */
84
85 /*----------------------------------------------------------------------------
86  *                        PROTOTYPES OF LOCAL FUNCTIONS
87  *
88  *----------------------------------------------------------------------------
89  */
90
91 /*----------------------------------------------------------------------------
92  *                            EXPORTED FUNCTIONS
93  *
94  *----------------------------------------------------------------------------
95  */
96
97 extern BUILD_OPT_CFG UserOptions;
98
99 /* -----------------------------------------------------------------------------*/
100 /**
101  *
102  *
103  *      This function programs the memory controller with configuration parameters
104  *
105  *
106  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
107  *
108  *     @return          TRUE - An Error value lower than AGESA_FATAL may have occurred
109  *     @return          FALSE - An Error value greater than or equal to AGESA_FATAL may have occurred
110  *     @return          NBPtr->MCTPtr->ErrCode - Contains detailed AGESA_STATUS value
111  */
112
113 BOOLEAN
114 MemNAutoConfigHy (
115   IN OUT   MEM_NB_BLOCK *NBPtr
116   )
117 {
118   UINT8 PowerDownMode;
119   UINT32 Value32;
120   DIE_STRUCT *MCTPtr;
121   DCT_STRUCT *DCTPtr;
122   MEM_PARAMETER_STRUCT *RefPtr;
123
124   RefPtr = NBPtr->RefPtr;
125   MCTPtr = NBPtr->MCTPtr;
126   DCTPtr = NBPtr->DCTPtr;
127   //======================================================================
128   // Build Dram Control Register Value (F2x78)
129   //======================================================================
130   //
131   //It is recommended that these bits remain in the default state.
132   //MemNSetBitFieldNb (NBPtr, BFRdPtrInit, 5);
133
134   MemNSetBitFieldNb (NBPtr, BFEarlyArbEn, 1);
135
136   //======================================================================
137   // Build Dram Config Lo Register Value
138   //======================================================================
139   //
140
141   if (MCTPtr->Status[SbParDimms]) {
142     //
143     // SbParDimms should be set for all DDR3 RDIMMS
144     // Cannot turn off ParEn for DDR3
145     //
146     //@attention - add debug option for parity control
147     MemNSetBitFieldNb (NBPtr, BFParEn, 1);
148   }
149
150
151   if (MCTPtr->GangedMode) {
152     MemNSetBitFieldNb (NBPtr, BFWidth128, 1);
153   }
154
155   MemNSetBitFieldNb (NBPtr, BFX4Dimm, DCTPtr->Timings.Dimmx4Present & 0xF);
156
157   if (!MCTPtr->Status[SbRegistered]) {
158     MemNSetBitFieldNb (NBPtr, BFUnBuffDimm, 1);
159   }
160
161   if (MCTPtr->Status[SbEccDimms]) {
162     MemNSetBitFieldNb (NBPtr, BFDimmEccEn, 1);
163   }
164
165   //======================================================================
166   // Build Dram Config Hi Register Value
167   //======================================================================
168   //
169
170   MemNSetBitFieldNb (NBPtr, BFMemClkFreq, MemNGetMemClkFreqIdNb (NBPtr, DCTPtr->Timings.Speed));
171
172   if (MCTPtr->Status[SbRegistered]) {
173     if ((DCTPtr->Timings.Dimmx4Present != 0) && (DCTPtr->Timings.Dimmx8Present != 0)) {
174       MemNSetBitFieldNb (NBPtr, BFRDqsEn, 1);
175     }
176   }
177
178   if (RefPtr->EnableBankSwizzle) {
179     MemNSetBitFieldNb (NBPtr, BFBankSwizzleMode, 1);
180   }
181
182   if (DCTPtr->Timings.DimmQrPresent) {
183     if (UserOptions.CfgMemoryQuadrankType == QUADRANK_UNBUFFERED) {
184       MemNSetBitFieldNb (NBPtr, BFFourRankSoDimm, 1);
185     } else if (UserOptions.CfgMemoryQuadrankType == QUADRANK_REGISTERED) {
186       MemNSetBitFieldNb (NBPtr, BFFourRankRDimm, 1);
187     }
188   }
189
190   MemNSetBitFieldNb (NBPtr, BFDcqBypassMax, 0xF);
191   MemNSetBitFieldNb (NBPtr, BFDcqArbBypassEn, 1);
192   //======================================================================
193   // Build Dram Config Misc Register Value
194   //======================================================================
195   //
196   if (MCTPtr->Status[SbRegistered]) {
197     if (MemNGetBitFieldNb (NBPtr, BFDdr3Mode)!= 0) {
198       MemNSetBitFieldNb (NBPtr, BFSubMemclkRegDly, 1);
199     }
200   }
201   //======================================================================
202   // Build Dram Config Misc 2 Register Value
203   //======================================================================
204   //
205   //
206   // Ddr3FourSocketCh - Must be the same for both DCTs if either of them have > 2 Dimms
207   //
208   if ((GetMaxDimmsPerChannel (RefPtr->PlatformMemoryConfiguration, MCTPtr->SocketId, NBPtr->ChannelPtr->ChannelID)) > 2) {
209     MemNBrdcstSetNb (NBPtr, BFDdr3FourSocketCh, 1);
210   }
211   //
212   // DTaxTxFifpWrDly
213   //
214   Value32 = MemNGetBitFieldNb (NBPtr, BFRdPtrInit);
215   if ((Value32 >= 2) && (Value32 <= 5)) {
216     MemNSetBitFieldNb (NBPtr, BFDataTxFifoWrDly, (6 - Value32));
217   }
218
219   //
220   // ProgOdtEn
221   //
222   if (MemNGetBitFieldNb (NBPtr, BFDdr3Mode) == 1) {
223     MemNSetBitFieldNb (NBPtr, BFProgOdtEn, 1);
224   } else {
225     MemNSetBitFieldNb (NBPtr, BFProgOdtEn, 0);
226   }
227   //
228   // OdtSwizzle
229   //
230   if ((MemNGetBitFieldNb (NBPtr, BFDdr3Mode) == 0) && (MemNGetBitFieldNb (NBPtr, BFFourRankRDimm) == 0) && (RefPtr->EnablePowerDown)) {
231     PowerDownMode = (UINT8) ((UserOptions.CfgPowerDownMode == POWER_DOWN_MODE_AUTO) ? POWER_DOWN_BY_CHANNEL : UserOptions.CfgPowerDownMode);
232     IDS_OPTION_HOOK (IDS_POWERDOWN_MODE, &PowerDownMode, &(NBPtr->MemPtr->StdHeader));
233     if (PowerDownMode == 1) {
234       MemNSetBitFieldNb (NBPtr, BFOdtSwizzle, 1);
235     }
236   }
237
238
239   return (BOOLEAN) (MCTPtr->ErrCode < AGESA_FATAL);
240 }
241
242 /* -----------------------------------------------------------------------------*/
243 /**
244  *
245  *
246  *   This function sends an MRS command
247  *
248  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
249  *
250  */
251
252 VOID
253 MemNSendMrsCmdHy (
254   IN OUT   MEM_NB_BLOCK *NBPtr
255   )
256 {
257   MemNSwapBitsNb (NBPtr);
258
259   IDS_HDT_CONSOLE (MEM_FLOW, "\t\t\tCS%d MR%d %04x\n",
260               (MemNGetBitFieldNb (NBPtr, BFDramInitRegReg) >> 20) & 0xF,
261               (MemNGetBitFieldNb (NBPtr, BFDramInitRegReg) >> 16) & 0xF,
262               (MemNGetBitFieldNb (NBPtr, BFDramInitRegReg) & 0xFFFF));
263
264   // 1.Set SendMrsCmd=1
265   MemNSetBitFieldNb (NBPtr, BFSendMrsCmd, 1);
266
267   // 2.Wait for SendMrsCmd=0
268   MemNPollBitFieldNb (NBPtr, BFSendMrsCmd, 0, PCI_ACCESS_TIMEOUT, FALSE);
269 }
270
271 /* -----------------------------------------------------------------------------*/
272 /**
273  *
274  *
275  *   This function sends an MRS command to all CS of all channels
276  *
277  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
278  *     @param[in,out]   *DummyPtr - Unused pointer
279  *
280  *     @return    TRUE
281  */
282
283 BOOLEAN
284 MemNSendMrsCmdPerCsHy (
285   IN OUT   MEM_NB_BLOCK *NBPtr,
286   IN OUT   VOID *DummyPtr
287   )
288 {
289   UINT8 Dct;
290   UINT8 ChipSel;
291   UINT32 Dummy;
292
293   if (!NBPtr->MCTPtr->Status[SbRegistered]) {
294     for (Dct = 0; Dct < NBPtr->DctCount; Dct++) {
295       NBPtr->SwitchDCT (NBPtr, Dct);
296       if (NBPtr->DCTPtr->Timings.DctMemSize != 0) {
297         IDS_HDT_CONSOLE (MEM_STATUS, "\tDct %d\n", Dct);
298         for (ChipSel = 0; ChipSel < MAX_CS_PER_CHANNEL; ChipSel++) {
299           if (NBPtr->GetSysAddr (NBPtr, ChipSel, &Dummy)) {
300             IDS_HDT_CONSOLE (MEM_STATUS, "\t\tCS %d\n", ChipSel);
301             // if chip select present
302             NBPtr->TechPtr->SendAllMRCmds (NBPtr->TechPtr, ChipSel);
303             // NOTE: wait 512 clocks for DLL-relock
304             MemUWait10ns (50000, NBPtr->MemPtr);  // wait 500us
305           }
306         }
307       }
308     }
309   }
310   return TRUE;
311 }
312
313 /* -----------------------------------------------------------------------------*/
314 /**
315  *
316  *
317  *   This is a general purpose function that executes before DRAM init
318  *
319  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
320  *
321  */
322
323 VOID
324 MemNBeforeDramInitHy (
325   IN OUT   MEM_NB_BLOCK *NBPtr
326   )
327 {
328   UINT8 Dct;
329   for (Dct = 0; Dct < NBPtr->DctCount; Dct ++) {
330     MemNSwitchDCTNb (NBPtr, Dct);
331     if (NBPtr->DCTPtr->Timings.DctMemSize != 0) {
332       //Set PllLockTime and DllLockTime to default.
333       MemNSetBitFieldNb (NBPtr, BFPhyPLLLockTime, 0x000007D0);
334       MemNSetBitFieldNb (NBPtr, BFPhyDLLLockTime, 0x00000190);
335       MemNSetBitFieldNb (NBPtr, BFDisDllShutdownSR, 1);
336     }
337   }
338 }
339
340 /* -----------------------------------------------------------------------------*/
341 /**
342  *
343  *
344  *   Enable DLL Shut down
345  *
346  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
347  *
348  */
349 VOID
350 MemNEnDLLShutDownHy (
351   IN OUT   MEM_NB_BLOCK *NBPtr
352   )
353 {
354   UINT8 Dct;
355   BOOLEAN DllShutDownEn;
356
357   DllShutDownEn = TRUE;
358   IDS_OPTION_HOOK (IDS_DLL_SHUT_DOWN, &DllShutDownEn, &(NBPtr->MemPtr->StdHeader));
359
360   if (DllShutDownEn && NBPtr->IsSupported[SetDllShutDown]) {
361     if ((NBPtr->ChannelPtr->MCTPtr->LogicalCpuid.Revision & AMD_F10_D1) != 0) {
362       for (Dct = 0; Dct < NBPtr->DctCount; Dct++) {
363         MemNSwitchDCTNb (NBPtr, Dct);
364         if (NBPtr->DCTPtr->Timings.DctMemSize != 0) {
365           MemNSetBitFieldNb (NBPtr, BFPhyPLLLockTime, 0x0000001C);
366           MemNSetBitFieldNb (NBPtr, BFPhyDLLLockTime, 0x0000013D);
367           MemNSetBitFieldNb (NBPtr, BFDisDllShutdownSR, 0);
368         }
369       }
370     }
371   }
372 }
373
374 /* -----------------------------------------------------------------------------*/
375 /**
376  *
377  *
378  *   Workaround for erratum 322 and 263
379  *
380  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
381  *
382  */
383 VOID
384 MemNBeforePlatformSpecHy (
385   IN OUT   MEM_NB_BLOCK *NBPtr
386   )
387 {
388   // Errata 263
389   if ((NBPtr->DCTPtr->Timings.Speed == DDR533_FREQUENCY) || (NBPtr->DCTPtr->Timings.Speed == DDR667_FREQUENCY)) {
390     MemNSetBitFieldNb (NBPtr, BFErr263, 0x0800);
391   } else {
392     MemNSetBitFieldNb (NBPtr, BFErr263, 0);
393   }
394
395   // Errata 322
396   // 1.Write 00000000h to F2x[1,0]9C_xD08E000
397   MemNSetBitFieldNb (NBPtr, BFErr322I, 0);
398   // 2.If DRAM Configuration Register[MemClkFreq] (F2x[1,0]94[2:0]) is
399   //    greater than or equal to 011b (DDR-800 and higher),
400   //    then write 00000080h to F2x[1,0]9C_xD02E001,
401   //    else write 00000090h to F2x[1,0]9C_xD02E001.
402   MemNSetBitFieldNb (NBPtr, BFErr322II, (NBPtr->DCTPtr->Timings.Speed >= DDR800_FREQUENCY) ? 0x80 : 0x90);
403 }
404
405
406 /* -----------------------------------------------------------------------------*/
407 /**
408  *
409  *    Initializes extended MMIO address space
410  *
411  *     @param[in,out]   *NBPtr   - Pointer to the MEM_NB_BLOCK
412  *     @param[in,out]   OptParam - Optional parameter
413  *
414  *     @return    TRUE
415  */
416 BOOLEAN
417 MemNInitExtMMIOAddrHy  (
418   IN OUT   MEM_NB_BLOCK *NBPtr,
419   IN OUT   VOID *OptParam
420   )
421 {
422   UINT8  Index;
423   UINT32 Value;
424   PCI_ADDR PciAddr;
425
426   if (NBPtr->RefPtr->SysLimit >= _1TB_RJ16) {
427     // Initialize all indices of F1x114_x2 and F1x114_x3.
428     for (Index = 0; Index < 32; Index++) {
429       PciAddr = NBPtr->PciAddr;
430       PciAddr.Address.Function = 1;
431
432       PciAddr.Address.Register = 0x110;
433       Value = 0x20000000 | Index;
434       LibAmdPciWrite (AccessWidth32, PciAddr, &Value, &NBPtr->MemPtr->StdHeader);
435
436       PciAddr.Address.Register = 0x114;
437       Value = 0;
438       LibAmdPciWrite (AccessWidth32, PciAddr, &Value, &NBPtr->MemPtr->StdHeader);
439
440       PciAddr.Address.Register = 0x110;
441       Value = 0x30000000 | Index;
442       LibAmdPciWrite (AccessWidth32, PciAddr, &Value, &NBPtr->MemPtr->StdHeader);
443
444       PciAddr.Address.Register = 0x114;
445       Value = 0;
446       LibAmdPciWrite (AccessWidth32, PciAddr, &Value, &NBPtr->MemPtr->StdHeader);
447     }
448   }
449   return TRUE;
450 }
451
452 /*----------------------------------------------------------------------------
453  *                              LOCAL FUNCTIONS
454  *
455  *----------------------------------------------------------------------------
456  */