2 * This file is part of the coreboot project.
4 * Copyright (C) 2009 One Laptop per Child, Association, Inc.
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.
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.
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
20 void DRAMClearEndingAddress(DRAM_SYS_ATTR * DramAttr);
22 void DRAMSizingEachRank(DRAM_SYS_ATTR * DramAttr);
24 BOOLEAN DoDynamicSizing1XM(DRAM_SYS_ATTR * DramAttr,
25 u8 * nRA, u8 * nCA, u8 * nBS, u8 PhyRank);
27 void DRAMSetRankMAType(DRAM_SYS_ATTR * DramAttr);
29 void DRAMSetEndingAddress(DRAM_SYS_ATTR * DramAttr);
31 void DRAMPRToVRMapping(DRAM_SYS_ATTR * DramAttr);
33 /*===================================================================
34 Function : DRAMBankInterleave()
37 DramAttr: pointer point to DRAMSYSATTR which consist the DDR and Dimm information in MotherBoard
39 Purpose : STEP 13 Set Bank Interleave VIANB3DRAMREG69[7:6] 00:No Interleave 01:2 Bank 10:4 Bank 11:8 Bank
40 Scan all DIMMs on board to find out the lowest Bank Interleave among these DIMMs and set register.
41 ===================================================================*/
42 void DRAMBankInterleave(DRAM_SYS_ATTR * DramAttr)
45 DIMM_INFO *CurrentDimminfo;
46 u8 Bank = 3, Shift, RankNO, Count;
48 for (RankNO = 0; RankNO < 4; RankNO += 2) //all_even 0 RankNO 4 6
50 if ((DramAttr->RankPresentMap & Shift) != 0) {
51 CurrentDimminfo = &(DramAttr->DimmInfo[RankNO >> 1]); //this Rank in a dimm
53 (u8) (CurrentDimminfo->SPDDataBuf
54 [SPD_SDRAM_NO_OF_BANKS]);
57 else if (SpdBAData == 8)
67 Data = pci_read_config8(MEMCTRL, 0x69);
70 pci_write_config8(MEMCTRL, 0x69, Data);
72 if (DramAttr->DimmNumChB > 0) {
73 CurrentDimminfo = &(DramAttr->DimmInfo[3]); //this Rank in a dimm
75 (u8) (CurrentDimminfo->SPDDataBuf[SPD_SDRAM_NO_OF_BANKS]);
78 else if (SpdBAData == 2)
82 pci_write_config8(MEMCTRL, 0x87, Bank);
86 /*===================================================================
87 Function : DRAMSizingMATypeM()
90 DramAttr: pointer point to DRAMSYSATTR which consist the DDR and Dimm information in MotherBoard
92 Purpose : STEP 14 1 DRAM Sizing 2 Fill MA type 3 Prank to vrankMapping
93 ===================================================================*/
94 void DRAMSizingMATypeM(DRAM_SYS_ATTR * DramAttr)
96 DRAMClearEndingAddress(DramAttr);
97 DRAMSizingEachRank(DramAttr);
98 //DRAMReInitDIMMBL (DramAttr);
99 DRAMSetRankMAType(DramAttr);
100 DRAMSetEndingAddress(DramAttr);
101 DRAMPRToVRMapping(DramAttr);
104 /*===================================================================
105 Function : DRAMClearEndingAddress()
108 DramAttr: pointer point to DRAMSYSATTR which consist the DDR and Dimm information in MotherBoard
110 Purpose : clear Ending and Start adress from 0x40-4f to zero
111 ===================================================================*/
112 void DRAMClearEndingAddress(DRAM_SYS_ATTR * DramAttr)
116 for (Reg = 0x40; Reg <= 0x4f; Reg++) {
117 pci_write_config8(MEMCTRL, Reg, Data);
121 /*===================================================================
122 Function : DRAMSizingEachRank()
125 DramAttr: pointer point to DRAMSYSATTR which consist the DDR and Dimm information in MotherBoard
127 Purpose : Sizing each Rank invidually, by number of rows column banks pins, be care about 128bit
128 ===================================================================*/
129 void DRAMSizingEachRank(DRAM_SYS_ATTR * DramAttr)
131 u8 Slot, RankIndex, Rows, Columns, Banks;
133 BOOLEAN HasThreeBitBA;
136 HasThreeBitBA = FALSE;
137 for (Slot = 0; Slot < 2; Slot++) {
138 if (!DramAttr->DimmInfo[Slot].bPresence)
140 Rows = DramAttr->DimmInfo[Slot].SPDDataBuf[SPD_SDRAM_ROW_ADDR];
142 DramAttr->DimmInfo[Slot].SPDDataBuf[SPD_SDRAM_COL_ADDR];
143 Banks = DramAttr->DimmInfo[Slot].SPDDataBuf[SPD_SDRAM_NO_OF_BANKS]; //this is Bank number not Bank address bit
150 Size = (u32) (1 << (Rows + Columns + Banks + 3));
151 RankIndex = 2 * Slot;
152 DramAttr->RankSize[RankIndex] = Size;
153 //if this module have two ranks
155 DimmInfo[Slot].SPDDataBuf[SPD_SDRAM_DIMM_RANKS] & 0x07) ==
158 DramAttr->RankSize[RankIndex] = Size;
161 PRINT_DEBUG_MEM("rows: ");
162 PRINT_DEBUG_MEM_HEX8(Rows);
163 PRINT_DEBUG_MEM(", columns:");
164 PRINT_DEBUG_MEM_HEX8(Columns);
165 PRINT_DEBUG_MEM(", banks:");
166 PRINT_DEBUG_MEM_HEX8(Banks);
167 PRINT_DEBUG_MEM("\r");
170 HasThreeBitBA = TRUE;
173 //must set BA2 enable if any 8-bank device exists
175 Data = pci_read_config8(MEMCTRL, 0x53);
177 pci_write_config8(MEMCTRL, 0x53, Data);
180 for (RankIndex = 0; DramAttr->RankSize[RankIndex] != 0; RankIndex++) {
181 PRINT_DEBUG_MEM("Rank:");
182 PRINT_DEBUG_MEM_HEX8(RankIndex);
183 PRINT_DEBUG_MEM(", Size:");
184 PRINT_DEBUG_MEM_HEX32(DramAttr->RankSize[RankIndex] >> 20);
185 PRINT_DEBUG_MEM("\r");
190 /*===================================================================
191 Function : DRAMSetRankMAType()
194 DramAttr: pointer point to DRAMSYSATTR which consist the DDR and Dimm information in MotherBoard
196 Purpose : set the matype Reg by MAMapTypeTbl, which the rule can be found in memoryinit
197 ===================================================================*/
198 void DRAMSetRankMAType(DRAM_SYS_ATTR * DramAttr)
200 u8 SlotNum, Data, j, Reg, or, and;
201 u8 ShiftBits[] = { 5, 1, 5, 1 }; /* Rank 0/1 MA Map Type is 7:5, Rank 2/3 MA Map Type is 3:1. See Fun3Rx50. */
202 u8 MAMapTypeTbl[] = { /* Table 12 of P4M800 Pro DataSheet. */
203 2, 9, 0, /* Bank Address Bits, Column Address Bits, Rank MA Map Type */
212 Data = pci_read_config8(MEMCTRL, 0x50);
214 pci_write_config8(MEMCTRL, 0x50, Data);
215 // disable MA32/16 MA33/17 swap in memory init it has this Reg fill
216 Data = pci_read_config8(MEMCTRL, 0x6b);
218 pci_write_config8(MEMCTRL, 0x6b, Data);
221 for (SlotNum = 0; SlotNum < MAX_DIMMS; SlotNum++) {
222 if (DramAttr->DimmInfo[SlotNum].bPresence) {
223 for (j = 0; MAMapTypeTbl[j] != 0; j += 3) {
224 if ((1 << MAMapTypeTbl[j]) ==
226 DimmInfo[SlotNum].SPDDataBuf
227 [SPD_SDRAM_NO_OF_BANKS]
228 && MAMapTypeTbl[j + 1] ==
230 DimmInfo[SlotNum].SPDDataBuf
231 [SPD_SDRAM_COL_ADDR]) {
235 if (0 == MAMapTypeTbl[j]) {
237 ("UNSUPPORTED Bank, Row and Column Addr Bits!\r");
240 or = MAMapTypeTbl[j + 2] << ShiftBits[SlotNum];
241 if (DramAttr->CmdRate == 1)
242 or |= 0x01 << (ShiftBits[SlotNum] - 1);
245 if ((SlotNum & 0x01) == 0x01) {
246 and = 0xf1; // BUGBUG: it should be 0xf0
248 and = 0x1f; // BUGBUG: it should be 0x0f
250 Data = pci_read_config8(MEMCTRL, 0x50 + Reg);
253 pci_write_config8(MEMCTRL, 0x50 + Reg, Data);
256 //may have some Reg filling at add 3-52 11 and 3-53 in his function
259 /*===================================================================
260 Function : DRAMSetEndingAddress()
263 DramAttr: pointer point to DRAMSYSATTR which consist the DDR and Dimm information in MotherBoard
265 Purpose : realize the Vrank 40...Reg (Start and Ending Regs). Vrank have same order with phy Rank, Size is actual Size
266 ===================================================================*/
267 void DRAMSetEndingAddress(DRAM_SYS_ATTR * DramAttr)
269 u8 Shift = 1, Data, RankNO, Size, Start = 0, End = 0, Vrank;
270 for (RankNO = 0; RankNO < 4; RankNO++) {
271 if ((DramAttr->RankPresentMap & Shift) != 0) {
272 Size = (u8) (DramAttr->RankSize[RankNO] >> 26); // current Size in the unit of 64M
275 End = End + Size; // calculate current ending address, add the current Size to ending
276 Vrank = RankNO; // get virtual Rank
277 Data = End; // set begin/End address register to correspondig virtual Rank #
278 pci_write_config8(MEMCTRL, 0x40 + Vrank, Data);
280 pci_write_config8(MEMCTRL, 0x48 + Vrank, Data);
281 PRINT_DEBUG_MEM("Rank: ");
282 PRINT_DEBUG_MEM_HEX8(Vrank);
283 PRINT_DEBUG_MEM(", Start:");
284 PRINT_DEBUG_MEM_HEX8(Start);
285 PRINT_DEBUG_MEM(", End:");
286 PRINT_DEBUG_MEM_HEX8(End);
287 PRINT_DEBUG_MEM("\r");
295 if (DramAttr->RankNumChB > 0) {
296 //this is a bug,fixed is to 2,so the max LL size is 128M
298 pci_write_config8(MEMCTRL, 0x44, Data);
301 pci_write_config8(PCI_DEV(0, 17, 7), 0x60, Data);
302 // We should directly write to south Bridge, not in north bridge
303 // program LOW TOP Address
304 Data = pci_read_config8(MEMCTRL, 0x88);
305 pci_write_config8(MEMCTRL, 0x85, Data);
307 // also program vlink mirror
308 // We should directly write to south Bridge, not in north bridge
309 pci_write_config8(PCI_DEV(0, 17, 7), 0xe5, Data);
312 /*===================================================================
313 Function : DRAMPRToVRMapping()
316 DramAttr: pointer point to DRAMSYSATTR which consist the DDR and Dimm information in MotherBoard
318 Purpose : set the Vrank-prank map with the same order
319 ===================================================================*/
320 void DRAMPRToVRMapping(DRAM_SYS_ATTR * DramAttr)
322 u8 Shift, Data, and, or, DimmNO = 0, PhyRankNO, Reg;
324 for (Reg = 0x54; Reg <= 0x57; Reg++) //clear the map-reg
327 pci_write_config8(MEMCTRL, Reg, Data);
331 for (PhyRankNO = 0; PhyRankNO < MAX_RANKS; PhyRankNO++) {
332 if ((DramAttr->RankPresentMap & Shift) != 0) {
333 or = PhyRankNO; // get virtual Rank ,same with PhyRank
336 if ((PhyRankNO & 0x01) == 0x01) // get mask for register
342 DimmNO = (PhyRankNO >> 1);
343 Data = pci_read_config8(MEMCTRL, 0x54 + DimmNO);
346 pci_write_config8(MEMCTRL, 0x54 + DimmNO, Data);