Fix ECC disable option for AMD Fam10 DDR2 and DDR3.
[coreboot.git] / src / northbridge / amd / amdmct / mct / mctecc_d.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007-2008 Advanced Micro Devices, 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
21 #include "mct_d.h"
22
23 static void setSyncOnUnEccEn_D(struct MCTStatStruc *pMCTstat,
24                                 struct DCTStatStruc *pDCTstatA);
25 #ifdef UNUSED_CODE
26 static u32 GetScrubAddr_D(u32 Node);
27 #endif
28 static u8 isDramECCEn_D(struct DCTStatStruc *pDCTstat);
29
30
31 /* Initialize ECC modes of Integrated Dram+Memory Controllers of a network of
32  * Hammer processors.  Use Dram background scrubber to fast initialize ECC bits
33  * of all dram.
34  *
35  * Notes:
36  *
37  * Order that items are set:
38  *  1. eccen bit in NB
39  *  2. Scrub Base
40  *  3. Temp Node Base
41  *  4. Temp Node Limit
42  *  5. Redir bit in NB
43  *  6. Scrub CTL
44  *
45  * Conditions for setting background scrubber.
46  *  1. node is present
47  *  2. node has dram functioning (WE=RE=1)
48  *  3. all eccdimms (or bit 17 of offset 90,fn 2)
49  *  4. no chip-select gap exists
50  *
51  * The dram background scrubber is used under very controlled circumstances to
52  * initialize all the ECC bits on the DIMMs of the entire dram address map
53  * (including hidden or lost dram and dram above 4GB). We will turn the scrub
54  * rate up to maximum, which should clear 4GB of dram in about 2.7 seconds.
55  * We will activate the scrubbers of all nodes with ecc dram and let them run in
56  * parallel, thereby reducing even further the time required to condition dram.
57  * Finally, we will go through each node and either disable background scrubber,
58  *  or set the scrub rate to the user setup specified rate.
59  *
60  * To allow the NB to scrub, we need to wait a time period long enough to
61  * guarantee that the NB scrubs the entire dram on its node. Do do this, we
62  * simply sample the scrub ADDR once, for an initial value, then we sample and poll until the polled value of scrub ADDR
63  * has wrapped around at least once: Scrub ADDRi+1 < Scrub ADDRi. Since we let all
64  * Nodes run in parallel, we need to guarantee that all nodes have wrapped. To do
65  * this efficiently, we need only to sample one of the nodes, the node with the
66  * largest ammount of dram populated is the one which will take the longest amount
67  * of time (the scrub rate is set to max, the same rate, on all nodes).  So,
68  * during setup of scrub Base, we determine how much memory and which node has
69  * the largest memory installed.
70  *
71  * Scrubbing should not ordinarily be enabled on a Node with a chip-select gap
72  * (aka SW memhole, cs hoisting, etc..).To init ECC memory on this node, the
73  * scrubber is used in two steps.  First, the Dram Limit for the node is adjusted
74  * down to the bottom of the gap, and that ECC dram is initialized.  Second, the
75  * orignal Limit is restored, the Scrub base is set to 4GB, and scrubber is
76  * allowed to run until the Scrub Addr wraps around to zero.
77  */
78 u8 ECCInit_D(struct MCTStatStruc *pMCTstat, struct DCTStatStruc *pDCTstatA)
79 {
80         u8 Node;
81         u8 AllECC;
82         u16 OB_NBECC;
83         u32 curBase;
84         u16 OB_ECCRedir;
85         u32 LDramECC;
86         u32 OF_ScrubCTL;
87         u16 OB_ChipKill;
88         u8 MemClrECC;
89
90         u32 dev;
91         u32 reg;
92         u32 val;
93         u16 nvbits;
94
95         mctHookBeforeECC();
96
97         /* Construct these booleans, based on setup options, for easy handling
98         later in this procedure */
99         OB_NBECC = mctGet_NVbits(NV_NBECC);     /* MCA ECC (MCE) enable bit */
100
101         OB_ECCRedir =  mctGet_NVbits(NV_ECCRedir);      /* ECC Redirection */
102
103         OB_ChipKill = mctGet_NVbits(NV_ChipKill);       /* ECC Chip-kill mode */
104
105         OF_ScrubCTL = 0;                /* Scrub CTL for Dcache, L2, and dram */
106         nvbits = mctGet_NVbits(NV_DCBKScrub);
107         mct_AdjustScrub_D(pDCTstatA, &nvbits);
108         OF_ScrubCTL |= (u32) nvbits << 16;
109
110         nvbits = mctGet_NVbits(NV_L2BKScrub);
111         OF_ScrubCTL |= (u32) nvbits << 8;
112
113         nvbits = mctGet_NVbits(NV_DramBKScrub);
114         OF_ScrubCTL |= nvbits;
115
116         AllECC = 1;
117         MemClrECC = 0;
118         for (Node = 0; Node < MAX_NODES_SUPPORTED; Node++) {
119                 struct DCTStatStruc *pDCTstat;
120                 pDCTstat = pDCTstatA + Node;
121                 LDramECC = 0;
122                 if (NodePresent_D(Node)) {      /*If Node is present */
123                         dev = pDCTstat->dev_map;
124                         reg = 0x40+(Node << 3); /* Dram Base Node 0 + index */
125                         val = Get_NB32(dev, reg);
126
127                         /* WE/RE is checked */
128                         if((val & 3)==3) {      /* Node has dram populated */
129                                 /* Negate 'all nodes/dimms ECC' flag if non ecc
130                                    memory populated */
131                                 if( pDCTstat->Status & (1<<SB_ECCDIMMs)) {
132                                         LDramECC = isDramECCEn_D(pDCTstat);
133                                         if(pDCTstat->ErrCode != SC_RunningOK) {
134                                                 pDCTstat->Status &=  ~(1 << SB_ECCDIMMs);
135                                                 if (!OB_NBECC) {
136                                                         pDCTstat->ErrStatus |= (1 << SB_DramECCDis);
137                                                 }
138                                                 AllECC = 0;
139                                                 LDramECC =0;
140                                         }
141                                 } else {
142                                         AllECC = 0;
143                                 }
144                                 if(LDramECC) {  /* if ECC is enabled on this dram */
145                                         if (OB_NBECC) {
146                                                 mct_EnableDatIntlv_D(pMCTstat, pDCTstat);
147                                                 dev = pDCTstat->dev_nbmisc;
148                                                 reg =0x44;      /* MCA NB Configuration */
149                                                 val = Get_NB32(dev, reg);
150                                                 val |= 1 << 22; /* EccEn */
151                                                 Set_NB32(dev, reg, val);
152                                                 DCTMemClr_Init_D(pMCTstat, pDCTstat);
153                                                 MemClrECC = 1;
154                                                 print_tx("  ECC enabled on node: ", Node);
155                                         }
156                                 }       /* this node has ECC enabled dram */
157                         } else {
158                                 LDramECC = 0;
159                         }       /* Node has Dram */
160
161                         if (MemClrECC) {
162                                 MCTMemClrSync_D(pMCTstat, pDCTstatA);
163                         }
164                 }       /* if Node present */
165         }
166
167         if(AllECC)
168                 pMCTstat->GStatus |= 1<<GSB_ECCDIMMs;
169         else
170                 pMCTstat->GStatus &= ~(1<<GSB_ECCDIMMs);
171
172         /* Program the Dram BKScrub CTL to the proper (user selected) value.*/
173         /* Reset MC4_STS. */
174         for (Node = 0; Node < MAX_NODES_SUPPORTED; Node++) {
175                 struct DCTStatStruc *pDCTstat;
176                 pDCTstat = pDCTstatA + Node;
177                 LDramECC = 0;
178                 if (NodePresent_D(Node)) {      /* If Node is present */
179                         reg = 0x40+(Node<<3);   /* Dram Base Node 0 + index */
180                         val = Get_NB32(pDCTstat->dev_map, reg);
181                         curBase = val & 0xffff0000;
182                         /*WE/RE is checked because memory config may have been */
183                         if((val & 3)==3) {      /* Node has dram populated */
184                                 if (isDramECCEn_D(pDCTstat)) {  /* if ECC is enabled on this dram */
185                                         dev = pDCTstat->dev_nbmisc;
186                                         val = curBase << 8;
187                                         if(OB_ECCRedir) {
188                                                 val |= (1<<0); /* enable redirection */
189                                         }
190                                         Set_NB32(dev, 0x5C, val); /* Dram Scrub Addr Low */
191                                         val = curBase>>24;
192                                         Set_NB32(dev, 0x60, val); /* Dram Scrub Addr High */
193                                         Set_NB32(dev, 0x58, OF_ScrubCTL);       /*Scrub Control */
194
195                                         /* Divisor should not be set deeper than
196                                          * divide by 16 when Dcache scrubber or
197                                          * L2 scrubber is enabled.
198                                          */
199                                         if ((OF_ScrubCTL & (0x1F << 16)) || (OF_ScrubCTL & (0x1F << 8))) {
200                                                 val = Get_NB32(dev, 0x84);
201                                                 if ((val & 0xE0000000) > 0x80000000) {  /* Get F3x84h[31:29]ClkDivisor for C1 */
202                                                         val &= 0x1FFFFFFF;      /* If ClkDivisor is deeper than divide-by-16 */
203                                                         val |= 0x80000000;      /* set it to divide-by-16 */
204                                                         Set_NB32(dev, 0x84, val);
205                                                 }
206                                         }
207                                 }       /* this node has ECC enabled dram */
208                         }       /*Node has Dram */
209                 }       /*if Node present */
210         }
211
212         if(mctGet_NVbits(NV_SyncOnUnEccEn))
213                 setSyncOnUnEccEn_D(pMCTstat, pDCTstatA);
214
215         mctHookAfterECC();
216         for (Node = 0; Node < MAX_NODES_SUPPORTED; Node++) {
217                 struct DCTStatStruc *pDCTstat;
218                 pDCTstat = pDCTstatA + Node;
219                 if (NodePresent_D(Node)) {
220                         print_tx("ECCInit: Node ", Node);
221                         print_tx("ECCInit: Status ", pDCTstat->Status);
222                         print_tx("ECCInit: ErrStatus ", pDCTstat->ErrStatus);
223                         print_tx("ECCInit: ErrCode ", pDCTstat->ErrCode);
224                         print_t("ECCInit: Done\n");
225                 }
226         }
227         return MemClrECC;
228 }
229
230
231 static void setSyncOnUnEccEn_D(struct MCTStatStruc *pMCTstat,
232                                 struct DCTStatStruc *pDCTstatA)
233 {
234         u32 Node;
235         u32 reg;
236         u32 dev;
237         u32 val;
238
239         for (Node = 0; Node < MAX_NODES_SUPPORTED; Node++) {
240                 struct DCTStatStruc *pDCTstat;
241                 pDCTstat = pDCTstatA + Node;
242                 if (NodePresent_D(Node)) {      /* If Node is present*/
243                         reg = 0x40+(Node<<3);   /* Dram Base Node 0 + index*/
244                         val = Get_NB32(pDCTstat->dev_map, reg);
245                         /*WE/RE is checked because memory config may have been*/
246                         if((val & 3)==3) {      /* Node has dram populated*/
247                                 if( isDramECCEn_D(pDCTstat)) {
248                                         /*if ECC is enabled on this dram*/
249                                         dev = pDCTstat->dev_nbmisc;
250                                         reg = 0x44;     /* MCA NB Configuration*/
251                                         val = Get_NB32(dev, reg);
252                                         val |= (1<<SyncOnUcEccEn);
253                                         Set_NB32(dev, reg, val);
254                                 }
255                         }       /* Node has Dram*/
256                 }       /* if Node present*/
257         }
258 }
259
260 #ifdef UNUSED_CODE
261 static u32 GetScrubAddr_D(u32 Node)
262 {
263         /* Get the current 40-bit Scrub ADDR address, scaled to 32-bits,
264          * of the specified Node.
265          */
266
267         u32 reg;
268         u32 regx;
269         u32 lo, hi;
270         u32 val;
271         u32 dev = PA_NBMISC(Node);
272
273
274         reg = 0x60;             /* Scrub Addr High */
275         hi = Get_NB32(dev, reg);
276
277         regx = 0x5C;            /* Scrub Addr Low */
278         lo = Get_NB32(dev, regx);
279                                 /* Scrub Addr High again, detect 32-bit wrap */
280         val = Get_NB32(dev, reg);
281         if(val != hi) {
282                 hi = val;       /* Scrub Addr Low again, if wrap occured */
283                 lo = Get_NB32(dev, regx);
284         }
285
286         val = hi << 24;
287         val |= lo >> 8;
288
289         return val;             /* ScrubAddr[39:8] */
290 }
291 #endif
292
293 static u8 isDramECCEn_D(struct DCTStatStruc *pDCTstat)
294 {
295         u32 reg;
296         u32 val;
297         u8 i;
298         u32 dev = pDCTstat->dev_dct;
299         u8 ch_end;
300         u8 isDimmECCEn = 0;
301
302         if(pDCTstat->GangedMode) {
303                 ch_end = 1;
304         } else {
305                 ch_end = 2;
306         }
307         for(i=0; i<ch_end; i++) {
308                 if(pDCTstat->DIMMValidDCT[i] > 0){
309                         reg = 0x90 + i * 0x100;         /* Dram Config Low */
310                         val = Get_NB32(dev, reg);
311                         if(val & (1<<DimmEcEn)) {
312                                 /* set local flag 'dram ecc capable' */
313                                 isDimmECCEn = 1;
314                                 break;
315                         }
316                 }
317         }
318         return isDimmECCEn;
319 }