blob: d7bd3a7cdc2c91902521ffc1b0f6ea809e31f062 [file] [log] [blame]
Zheng Baoeb75f652010-04-23 17:32:48 +00001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2010 Advanced Micro Devices, Inc.
Timothy Pearson730a0432015-10-16 13:51:51 -05005 * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, Raptor Engineering
Zheng Baoeb75f652010-04-23 17:32:48 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Zheng Baoeb75f652010-04-23 17:32:48 +000015 */
16
17static u32 mct_ControlRC(struct MCTStatStruc *pMCTstat,
18 struct DCTStatStruc *pDCTstat, u32 MrsChipSel, u32 CtrlWordNum)
19{
20 u8 Dimms, DimmNum, MaxDimm, Speed;
21 u32 val;
Kerry She99cfa1e2010-08-30 07:31:31 +000022 u32 dct = 0;
Zheng Baoeb75f652010-04-23 17:32:48 +000023
Zheng Bao69436e12011-01-06 02:18:12 +000024 DimmNum = (MrsChipSel >> 20) & 0xFE;
Zheng Baoeb75f652010-04-23 17:32:48 +000025
26 /* assume dct=0; */
27 /* if (dct == 1) */
28 /* DimmNum ++; */
29 /* cl +=8; */
30
31 MaxDimm = mctGet_NVbits(NV_MAX_DIMMS);
32 Speed = pDCTstat->DIMMAutoSpeed;
Kerry She99cfa1e2010-08-30 07:31:31 +000033
34 if (pDCTstat->CSPresent_DCT[0] > 0) {
35 dct = 0;
36 } else if (pDCTstat->CSPresent_DCT[1] > 0 ){
37 dct = 1;
Zheng Bao69436e12011-01-06 02:18:12 +000038 DimmNum ++;
Kerry She99cfa1e2010-08-30 07:31:31 +000039 }
Kerry She99cfa1e2010-08-30 07:31:31 +000040 Dimms = pDCTstat->MAdimms[dct];
Zheng Baoeb75f652010-04-23 17:32:48 +000041
42 val = 0;
43 if (CtrlWordNum == 0)
44 val |= 1 << 1;
45 else if (CtrlWordNum == 1) {
46 if (!((pDCTstat->DimmDRPresent | pDCTstat->DimmQRPresent) & (1 << DimmNum)))
47 val |= 0xC; /* if single rank, set DBA1 and DBA0 */
Zheng Bao69436e12011-01-06 02:18:12 +000048 } else if (CtrlWordNum == 2) {
Zheng Baoeb75f652010-04-23 17:32:48 +000049 if (MaxDimm == 4) {
50 if (Speed == 4) {
Zheng Bao69436e12011-01-06 02:18:12 +000051 if (((pDCTstat->DimmQRPresent & (1 << DimmNum)) && (Dimms == 1)) || (Dimms == 2))
Zheng Baoeb75f652010-04-23 17:32:48 +000052 if (!(pDCTstat->MirrPresU_NumRegR & (1 << DimmNum)))
53 val |= 1 << 2;
54 } else {
55 if (pDCTstat->MirrPresU_NumRegR & (1 << DimmNum))
Zheng Bao69436e12011-01-06 02:18:12 +000056 val |= 1 << 2;
Zheng Baoeb75f652010-04-23 17:32:48 +000057 }
58 } else {
59 if (Dimms > 1)
Zheng Bao69436e12011-01-06 02:18:12 +000060 val |= 1 << 2;
Zheng Baoeb75f652010-04-23 17:32:48 +000061 }
62 } else if (CtrlWordNum == 3) {
Zheng Bao69436e12011-01-06 02:18:12 +000063 val |= (pDCTstat->CtrlWrd3 >> (DimmNum << 2)) & 0xFF;
Zheng Baoeb75f652010-04-23 17:32:48 +000064 } else if (CtrlWordNum == 4) {
Zheng Bao69436e12011-01-06 02:18:12 +000065 val |= (pDCTstat->CtrlWrd4 >> (DimmNum << 2)) & 0xFF;
Zheng Baoeb75f652010-04-23 17:32:48 +000066 } else if (CtrlWordNum == 5) {
Zheng Bao69436e12011-01-06 02:18:12 +000067 val |= (pDCTstat->CtrlWrd5 >> (DimmNum << 2)) & 0xFF;
Zheng Baoeb75f652010-04-23 17:32:48 +000068 } else if (CtrlWordNum == 8) {
69 if (MaxDimm == 4)
70 if (Speed == 4)
71 if (pDCTstat->MirrPresU_NumRegR & (1 << DimmNum))
72 val |= 1 << 2;
73 } else if (CtrlWordNum == 9) {
74 val |= 0xD; /* DBA1, DBA0, DA3 = 0 */
75 }
Zheng Bao69436e12011-01-06 02:18:12 +000076 val &= 0xffffff0f;
Zheng Baoeb75f652010-04-23 17:32:48 +000077
Zheng Bao69436e12011-01-06 02:18:12 +000078 val = MrsChipSel | ((val >> 2) & 3) << 16 | ((val & 3) << 3);
Zheng Baoeb75f652010-04-23 17:32:48 +000079
80 /* transfer Control word number to address [BA2,A2,A1,A0] */
81 if (CtrlWordNum > 7) {
82 val |= 1 << 18;
83 CtrlWordNum &= 7;
84 }
85 val |= CtrlWordNum;
86
87 return val;
88}
89
90static void mct_SendCtrlWrd(struct MCTStatStruc *pMCTstat,
91 struct DCTStatStruc *pDCTstat, u32 val)
92{
Timothy Pearson730a0432015-10-16 13:51:51 -050093 uint8_t dct = 0;
Zheng Baoeb75f652010-04-23 17:32:48 +000094 u32 dev = pDCTstat->dev_dct;
95
Kerry She99cfa1e2010-08-30 07:31:31 +000096 if (pDCTstat->CSPresent_DCT[0] > 0) {
Timothy Pearson730a0432015-10-16 13:51:51 -050097 dct = 0;
Kerry She99cfa1e2010-08-30 07:31:31 +000098 } else if (pDCTstat->CSPresent_DCT[1] > 0 ){
Timothy Pearson730a0432015-10-16 13:51:51 -050099 dct = 1;
Kerry She99cfa1e2010-08-30 07:31:31 +0000100 }
101
Timothy Pearson730a0432015-10-16 13:51:51 -0500102 val |= Get_NB32_DCT(dev, dct, 0x7C) & ~0xFFFFFF;
Zheng Baoeb75f652010-04-23 17:32:48 +0000103 val |= 1 << SendControlWord;
Timothy Pearson730a0432015-10-16 13:51:51 -0500104 Set_NB32_DCT(dev, dct, 0x7C, val);
Zheng Baoeb75f652010-04-23 17:32:48 +0000105
106 do {
Timothy Pearson730a0432015-10-16 13:51:51 -0500107 val = Get_NB32_DCT(dev, dct, 0x7C);
Zheng Baoeb75f652010-04-23 17:32:48 +0000108 } while (val & (1 << SendControlWord));
109}
110
111void mct_DramControlReg_Init_D(struct MCTStatStruc *pMCTstat,
112 struct DCTStatStruc *pDCTstat, u8 dct)
113{
114 u8 MrsChipSel;
115 u32 dev = pDCTstat->dev_dct;
116 u32 val, cw;
117
118 mct_Wait(1600);
119
120 mct_Wait(1200);
121
122 for (MrsChipSel = 0; MrsChipSel < 8; MrsChipSel ++, MrsChipSel ++) {
123 if (pDCTstat->CSPresent & (1 << MrsChipSel)) {
Timothy Pearson730a0432015-10-16 13:51:51 -0500124 val = Get_NB32_DCT(dev, dct, 0xa8);
Zheng Baoeb75f652010-04-23 17:32:48 +0000125 val &= ~(0xF << 8);
126
127 switch (MrsChipSel) {
128 case 0:
129 case 1:
130 val |= 3 << 8;
131 case 2:
132 case 3:
133 val |= (3 << 2) << 8;
134 case 4:
135 case 5:
136 val |= (3 << 4) << 8;
137 case 6:
138 case 7:
139 val |= (3 << 6) << 8;
140 }
Timothy Pearson730a0432015-10-16 13:51:51 -0500141 Set_NB32_DCT(dev, dct, 0xa8, val);
Zheng Baoeb75f652010-04-23 17:32:48 +0000142
143 for (cw=0; cw <=15; cw ++) {
144 mct_Wait(1600);
145 if (!(cw==6 || cw==7)) {
146 val = mct_ControlRC(pMCTstat, pDCTstat, MrsChipSel << 20, cw);
147 mct_SendCtrlWrd(pMCTstat, pDCTstat, val);
148 }
149 }
150 }
151 }
152
153 mct_Wait(1200);
154}
155
156void FreqChgCtrlWrd(struct MCTStatStruc *pMCTstat,
157 struct DCTStatStruc *pDCTstat)
158{
159 u32 SaveSpeed = pDCTstat->DIMMAutoSpeed;
160 u32 MrsChipSel;
161 u32 dev = pDCTstat->dev_dct;
162 u32 val;
163
164 pDCTstat->DIMMAutoSpeed = pDCTstat->TargetFreq;
165 for (MrsChipSel=0; MrsChipSel < 8; MrsChipSel++, MrsChipSel++) {
166 if (pDCTstat->CSPresent & (1 << MrsChipSel)) {
Zheng Bao69436e12011-01-06 02:18:12 +0000167 /* 2. Program F2x[1, 0]A8[CtrlWordCS]=bit mask for target chip selects. */
Timothy Pearson730a0432015-10-16 13:51:51 -0500168 val = Get_NB32_DCT(dev, 0, 0xA8); /* TODO: dct 0 / 1 select */
Zheng Baoeb75f652010-04-23 17:32:48 +0000169 val &= ~(0xFF << 8);
Zheng Bao69436e12011-01-06 02:18:12 +0000170 val |= (0x3 << (MrsChipSel & 0xFE)) << 8;
Timothy Pearson730a0432015-10-16 13:51:51 -0500171 Set_NB32_DCT(dev, 0, 0xA8, val); /* TODO: dct 0 / 1 select */
Zheng Baoeb75f652010-04-23 17:32:48 +0000172
Zheng Bao69436e12011-01-06 02:18:12 +0000173 /* Resend control word 10 */
Zheng Baoeb75f652010-04-23 17:32:48 +0000174 mct_Wait(1600);
175 switch (pDCTstat->TargetFreq) {
Zheng Bao69436e12011-01-06 02:18:12 +0000176 case 5:
Zheng Baoeb75f652010-04-23 17:32:48 +0000177 mct_SendCtrlWrd(pMCTstat, pDCTstat, MrsChipSel << 20 | 0x4000A);
178 break;
Zheng Bao69436e12011-01-06 02:18:12 +0000179 case 6:
Zheng Baoeb75f652010-04-23 17:32:48 +0000180 mct_SendCtrlWrd(pMCTstat, pDCTstat, MrsChipSel << 20 | 0x40012);
181 break;
182 case 7:
183 mct_SendCtrlWrd(pMCTstat, pDCTstat, MrsChipSel << 20 | 0x4001A);
184 break;
185 }
186
187 mct_Wait(1600);
188
Zheng Bao69436e12011-01-06 02:18:12 +0000189 /* Resend control word 2 */
Zheng Baoeb75f652010-04-23 17:32:48 +0000190 val = mct_ControlRC(pMCTstat, pDCTstat, MrsChipSel << 20, 2);
191 mct_SendCtrlWrd(pMCTstat, pDCTstat, val);
192
193 mct_Wait(1600);
194
195 /* Resend control word 8 */
196 val = mct_ControlRC(pMCTstat, pDCTstat, MrsChipSel << 20, 8);
197 mct_SendCtrlWrd(pMCTstat, pDCTstat, val);
198
199 mct_Wait(1600);
200 }
201 }
202 pDCTstat->DIMMAutoSpeed = SaveSpeed;
203}