AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / Mem / Tech / mttecc.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * mttecc.c
6  *
7  * Technology ECC byte 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 #include "AGESA.h"
55 #include "Ids.h"
56 #include "mm.h"
57 #include "mn.h"
58 #include "mt.h"
59 #include "Filecode.h"
60 CODE_GROUP (G1_PEICC)
61 RDATA_GROUP (G2_PEI)
62
63 #define FILECODE PROC_MEM_TECH_MTTECC_FILECODE
64 /*----------------------------------------------------------------------------
65  *                          DEFINITIONS AND MACROS
66  *
67  *----------------------------------------------------------------------------
68  */
69
70 /*----------------------------------------------------------------------------
71  *                           TYPEDEFS AND STRUCTURES
72  *
73  *----------------------------------------------------------------------------
74  */
75
76 /*----------------------------------------------------------------------------
77  *                        PROTOTYPES OF LOCAL FUNCTIONS
78  *
79  *----------------------------------------------------------------------------
80  */
81
82 VOID
83 STATIC
84 MemTCalcDQSEccTmg (
85   IN OUT   MEM_TECH_BLOCK *TechPtr,
86   IN       UINT8 Dimm,
87   IN       UINT8 Type,
88   IN OUT   VOID  *DlyArray
89   );
90
91 /*----------------------------------------------------------------------------
92  *                            EXPORTED FUNCTIONS
93  *
94  *----------------------------------------------------------------------------
95  */
96
97 /* -----------------------------------------------------------------------------*/
98 /**
99  *
100  *   This function sets the DQS ECC timings
101  *
102  *     @param[in,out]   *TechPtr   - Pointer to the MEM_TECH_BLOCK
103  *
104  *     @return          TRUE -  No fatal error occurs.
105  *     @return          FALSE - Fatal error occurs.
106  */
107
108 BOOLEAN
109 MemTSetDQSEccTmgs (
110   IN OUT   MEM_TECH_BLOCK *TechPtr
111   )
112 {
113   UINT8 Dct;
114   UINT8 Dimm;
115   UINT8 i;
116
117   MEM_NB_BLOCK *NBPtr;
118   CH_DEF_STRUCT *ChannelPtr;
119
120   NBPtr = TechPtr->NBPtr;
121   if (NBPtr->MCTPtr->NodeMemSize) {
122     for (Dct = 0; Dct < NBPtr->DctCount; Dct++) {
123       NBPtr->SwitchDCT (NBPtr, Dct);
124       if (NBPtr->DCTPtr->Timings.DctMemSize != 0) {
125         ChannelPtr = NBPtr->ChannelPtr;
126         for (Dimm = 0; Dimm < MAX_DIMMS_PER_CHANNEL; Dimm++) {
127           if (NBPtr->DCTPtr->Timings.CsEnabled & ((UINT16)1 << (Dimm * 2))) {
128             i = Dimm * TechPtr->DlyTableWidth ();
129             MemTCalcDQSEccTmg (TechPtr, Dimm, AccessRcvEnDly, &ChannelPtr->RcvEnDlys[i]);
130             MemTCalcDQSEccTmg (TechPtr, Dimm, AccessRdDqsDly, &ChannelPtr->RdDqsDlys[i]);
131             MemTCalcDQSEccTmg (TechPtr, Dimm, AccessWrDatDly, &ChannelPtr->WrDatDlys[i]);
132           }
133         }
134       }
135     }
136   }
137   return (BOOLEAN) (NBPtr->MCTPtr->ErrCode < AGESA_FATAL);
138 }
139
140 /*----------------------------------------------------------------------------
141  *                              LOCAL FUNCTIONS
142  *
143  *----------------------------------------------------------------------------
144  */
145
146 /* -----------------------------------------------------------------------------*/
147 /**
148  *
149  *   This function calculates the DQS ECC timings
150  *
151  *     @param[in,out]   *TechPtr - Pointer to the MEM_TECH_BLOCK
152  *     @param[in]       Dimm - Dimm number
153  *     @param[in]       Type - Type of DQS timing
154  *     @param[in,out]   *DlyArray - Pointer to the array of delays per this Dimm
155  *
156  */
157
158 VOID
159 STATIC
160 MemTCalcDQSEccTmg (
161   IN OUT   MEM_TECH_BLOCK *TechPtr,
162   IN       UINT8 Dimm,
163   IN       UINT8 Type,
164   IN OUT   VOID  *DlyArray
165   )
166 {
167   UINT8 i;
168   UINT8 j;
169   UINT8 Scale;
170   UINT8 EccByte;
171   UINT16 ByteiDly;
172   UINT16 BytejDly;
173   UINT16 EccDly;
174   UINT8 *WrDqsDly;
175   MEM_NB_BLOCK *NBPtr;
176   CH_DEF_STRUCT *ChannelPtr;
177
178   NBPtr = TechPtr->NBPtr;
179   ChannelPtr = NBPtr->ChannelPtr;
180
181   EccByte = TechPtr->MaxByteLanes ();
182   i = (UINT8) (ChannelPtr->DctEccDqsLike & 0xFF);
183   j = (UINT8) (ChannelPtr->DctEccDqsLike >> 8);
184   Scale = ChannelPtr->DctEccDqsScale;
185   WrDqsDly = &ChannelPtr->WrDqsDlys[Dimm * TechPtr->DlyTableWidth ()];
186
187   if (Type == AccessRcvEnDly) {
188     ByteiDly = ((UINT16 *) DlyArray)[i];
189     BytejDly = ((UINT16 *) DlyArray)[j];
190   } else {
191     ByteiDly = ((UINT8 *) DlyArray)[i];
192     BytejDly = ((UINT8 *) DlyArray)[j];
193   }
194
195   //
196   // For WrDatDly, Subtract TxDqs Delay to get
197   //  TxDq-TxDqs Delta, which is what should be averaged.
198   //
199   if (Type == AccessWrDatDly) {
200     ByteiDly = ByteiDly - WrDqsDly[i];
201     BytejDly = BytejDly - WrDqsDly[j];
202   }
203
204   if (BytejDly > ByteiDly) {
205     EccDly = ByteiDly + (UINT8) (((UINT16) (BytejDly - ByteiDly) * Scale + 0x77) / 0xFF);
206     //                                                            Round up --^
207   } else {
208     EccDly = BytejDly + (UINT8) (((UINT16) (ByteiDly - BytejDly) * (0xFF - Scale) + 0x77) / 0xFF);
209     //                                                                     Round up --^
210   }
211
212   if (Type == AccessRcvEnDly) {
213     ((UINT16 *) DlyArray)[EccByte] = EccDly;
214   } else {
215     ((UINT8 *) DlyArray)[EccByte] = (UINT8) EccDly;
216   }
217
218   //
219   // For WrDatDly, Add back the TxDqs value for ECC bytelane
220   //
221   if (Type == AccessWrDatDly) {
222     EccDly = EccDly + WrDqsDly[EccByte];
223   }
224
225   NBPtr->SetTrainDly (NBPtr, Type, DIMM_BYTE_ACCESS (Dimm, EccByte), EccDly);
226 }