summaryrefslogtreecommitdiff
path: root/src/northbridge/via/vx800/clk_ctrl.c
blob: 0f7207e9f46c0075798fffd07f0b4ae2351b994e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2009 One Laptop per Child, Association, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc.
 */

void DutyCycleCtrl(DRAM_SYS_ATTR *DramAttr)
{
	u8 Data, FreqId, i;

	if (DIMMFREQ_800 == DramAttr->DramFreq)
		FreqId = 2;
	else if (DIMMFREQ_667 == DramAttr->DramFreq)
		FreqId = 3;
	else if (DIMMFREQ_533 == DramAttr->DramFreq)
		FreqId = 4;
	else if (DIMMFREQ_400 == DramAttr->DramFreq)
		FreqId = 5;
	else
		FreqId = 5;

	if (DramAttr->RankNumChA > 0) { /* 1 rank */
		for (i = 0; i < DUTY_CYCLE_REG_NUM; i++) {
			Data = pci_read_config8(MEMCTRL,
						ChA_Duty_Control_DDR2[i][0]);
			Data &= ChA_Duty_Control_DDR2[i][1]; /* mask */
			Data |= ChA_Duty_Control_DDR2[i][FreqId]; /* set val */
			pci_write_config8(MEMCTRL,
					  ChA_Duty_Control_DDR2[i][0], Data);
		}
	}

	if (1 == ENABLE_CHC) { /* 1 rank */
		for (i = 0; i < DUTY_CYCLE_REG_NUM; i++) {
			Data = pci_read_config8(MEMCTRL,
						ChB_Duty_Control_DDR2[i][0]);
			Data &= ChB_Duty_Control_DDR2[i][1]; /* mask */
			Data |= ChB_Duty_Control_DDR2[i][FreqId]; /* set val */
			pci_write_config8(MEMCTRL,
					  ChB_Duty_Control_DDR2[i][0], Data);
		}
	}
}

/*
 * DRAM clock phase and delay control
 */

/* Subroutine list */
void ClkPhsCtrlFBMDDR2(DRAM_SYS_ATTR *DramAttr);
void WrtDataPhsCtrl(DRAM_SYS_ATTR *DramAttr);
void DQDQSOutputDlyCtrl(DRAM_SYS_ATTR *DramAttr);
void DQSInputCaptureCtrl(DRAM_SYS_ATTR *DramAttr);
void DCLKPhsCtrl(DRAM_SYS_ATTR *DramAttr);

void DRAMClkCtrl(DRAM_SYS_ATTR *DramAttr)
{
	/* Write data clock phase control. */
	WrtDataPhsCtrl(DramAttr);
	/* Clock phase control */
	ClkPhsCtrlFBMDDR2(DramAttr);
	 /**/ DQDQSOutputDlyCtrl(DramAttr);
	 /**/ DQSInputCaptureCtrl(DramAttr);
	DCLKPhsCtrl(DramAttr);
}

void ClkPhsCtrlFBMDDR2(DRAM_SYS_ATTR *DramAttr)
{
	u8 Data, FreqId, i;

	if (DramAttr->DramFreq == DIMMFREQ_800)
		FreqId = 2;
	else if (DramAttr->DramFreq == DIMMFREQ_667)
		FreqId = 3;
	else if (DramAttr->DramFreq == DIMMFREQ_533)
		FreqId = 4;
	else if (DramAttr->DramFreq == DIMMFREQ_400)
		FreqId = 5;
	else
		FreqId = 5;

	/* Channel A */
	// 2~4 Rank
	if (DramAttr->RankNumChA == 1) { /* 1 rank */
		for (i = 0; i < 3; i++) {
			Data = pci_read_config8(MEMCTRL,
				DDR2_ChA_Clk_Phase_Table_1R[i][0]);
			Data &= DDR2_ChA_Clk_Phase_Table_1R[i][1]; /* mask */
			Data |= DDR2_ChA_Clk_Phase_Table_1R[i][FreqId];	/* set val */
			pci_write_config8(MEMCTRL,
				DDR2_ChA_Clk_Phase_Table_1R[i][0], Data);
		}
	} else if (DramAttr->RankNumChA > 1) {	/* 2~4 Rank */
		for (i = 0; i < 3; i++) {
			Data = pci_read_config8(MEMCTRL,
				     DDR2_ChA_Clk_Phase_Table_2R[i][0]);
			Data &= DDR2_ChA_Clk_Phase_Table_2R[i][1]; /* mask */
			Data |= DDR2_ChA_Clk_Phase_Table_2R[i][FreqId];	/* set val */
			pci_write_config8(MEMCTRL,
				DDR2_ChA_Clk_Phase_Table_2R[i][0], Data);
		}
	}
#if ENABLE_CHB
	if (DramAttr->RankNumChB > 0) { /* 1 rank */
		for (i = 0; i < 3; i++) {
			Data = pci_read_config8(MEMCTRL,
				DDR2_ChB_Clk_Phase_Table_1R[i][0]);
			Data &= DDR2_ChB_Clk_Phase_Table_1R[i][1]; /* mask */
			Data |= DDR2_ChB_Clk_Phase_Table_1R[i][FreqId];	/* set val */
			pci_write_config8(MEMCTRL,
				DDR2_ChB_Clk_Phase_Table_1R[i][0], Data);
		}
	}
#endif
}

void WrtDataPhsCtrl(DRAM_SYS_ATTR *DramAttr)
{
	u8 Data, FreqId, i;

	if (DIMMFREQ_800 == DramAttr->DramFreq)
		FreqId = 2;
	else if (DIMMFREQ_667 == DramAttr->DramFreq)
		FreqId = 3;
	else if (DIMMFREQ_533 == DramAttr->DramFreq)
		FreqId = 4;
	else if (DIMMFREQ_400 == DramAttr->DramFreq)
		FreqId = 5;
	else
		FreqId = 5;

	if (DramAttr->RankNumChA > 0) { /* 1 rank */
		for (i = 0; i < WrtData_REG_NUM; i++) {
			Data = pci_read_config8(MEMCTRL,
				DDR2_ChA_WrtData_Phase_Table[i][0]);
			Data &= DDR2_ChA_WrtData_Phase_Table[i][1]; /* mask */
			Data |= DDR2_ChA_WrtData_Phase_Table[i][FreqId]; /* set val */
			pci_write_config8(MEMCTRL,
				DDR2_ChA_WrtData_Phase_Table[i][0], Data);
		}
	}
#if ENABLE_CHB
	if (DramAttr->RankNumChB > 0) {	/* 1 rank */
		for (i = 0; i < WrtData_REG_NUM; i++) {
			Data = pci_read_config8(MEMCTRL,
				DDR2_ChB_WrtData_Phase_Table[i][0]);
			Data &= DDR2_ChB_WrtData_Phase_Table[i][1]; /* mask */
			Data |= DDR2_ChB_WrtData_Phase_Table[i][FreqId]; /* set val */
			pci_write_config8(MEMCTRL,
				DDR2_ChB_WrtData_Phase_Table[i][0], Data);
		}
	}
#endif
	Data = pci_read_config8(MEMCTRL, 0x8C);
	Data &= 0xFC;
	Data |= 0x03;
	pci_write_config8(MEMCTRL, 0x8C, Data);
}

void DQDQSOutputDlyCtrl(DRAM_SYS_ATTR *DramAttr)
{
	u8 Data, FreqId;

	if (DIMMFREQ_400 == DramAttr->DramFreq)
		FreqId = 0;
	else if (DIMMFREQ_533 == DramAttr->DramFreq)
		FreqId = 1;
	else if (DIMMFREQ_667 == DramAttr->DramFreq)
		FreqId = 2;
	else if (DIMMFREQ_800 == DramAttr->DramFreq)
		FreqId = 2;
	else
		FreqId = 0;

	if (DramAttr->RankNumChA > 0) {
		Data = DDR2_CHA_DQ_DQS_Delay_Table[FreqId][0];
		pci_write_config8(MEMCTRL, 0xf0, Data);

		Data = DDR2_CHA_DQ_DQS_Delay_Table[FreqId][1];
		pci_write_config8(MEMCTRL, 0xf1, Data);

		Data = DDR2_CHA_DQ_DQS_Delay_Table[FreqId][2];
		pci_write_config8(MEMCTRL, 0xf2, Data);

		Data = DDR2_CHA_DQ_DQS_Delay_Table[FreqId][3];
		pci_write_config8(MEMCTRL, 0xf3, Data);
	}
#if ENABLE_CHB
	if (DramAttr->RankNumChB > 0) {
		Data = DDR2_CHB_DQ_DQS_Delay_Table[FreqId][0];
		pci_write_config8(MEMCTRL, 0xf4, Data);

		Data = DDR2_CHB_DQ_DQS_Delay_Table[FreqId][1];
		pci_write_config8(MEMCTRL, 0xf5, Data);

		Data = DDR2_CHB_DQ_DQS_Delay_Table[FreqId][2];
		pci_write_config8(MEMCTRL, 0xf6, Data);

		Data = DDR2_CHB_DQ_DQS_Delay_Table[FreqId][3];
		pci_write_config8(MEMCTRL, 0xf7, Data);
	}
#endif
}

void DQSInputCaptureCtrl(DRAM_SYS_ATTR *DramAttr)
{
	u8 Data, FreqId, i;

	if (DIMMFREQ_800 == DramAttr->DramFreq)
		FreqId = 2;
	else if (DIMMFREQ_667 == DramAttr->DramFreq)
		FreqId = 3;
	else if (DIMMFREQ_533 == DramAttr->DramFreq)
		FreqId = 4;
	else if (DIMMFREQ_400 == DramAttr->DramFreq)
		FreqId = 5;
	else
		FreqId = 2;

	Data = 0x8A;
	pci_write_config8(MEMCTRL, 0x77, Data);

	if (DramAttr->RankNumChA > 0) { /* 1 rank */
		for (i = 0; i < DQS_INPUT_CAPTURE_REG_NUM; i++) {
			Data = pci_read_config8(MEMCTRL,
				DDR2_ChA_DQS_Input_Capture_Tbl[i][0]);
			Data &= DDR2_ChA_DQS_Input_Capture_Tbl[i][1]; /* mask */
			Data |= DDR2_ChA_DQS_Input_Capture_Tbl[i][FreqId]; /* set val */
			pci_write_config8(MEMCTRL,
				DDR2_ChA_DQS_Input_Capture_Tbl[i][0], Data);
		}
	}
#if ENABLE_CHB
	if (DramAttr->RankNumChB > 0) { /* 1 rank */
		for (i = 0; i < DQS_INPUT_CAPTURE_REG_NUM; i++) {
			Data = pci_read_config8(MEMCTRL,
				DDR2_ChB_DQS_Input_Capture_Tbl[i][0]);
			Data &= DDR2_ChB_DQS_Input_Capture_Tbl[i][1]; /* mask */
			Data |= DDR2_ChB_DQS_Input_Capture_Tbl[i][FreqId]; /* set val */
			pci_write_config8(MEMCTRL,
			        DDR2_ChB_DQS_Input_Capture_Tbl[i][0], Data);
		}
	}
#endif
}

/*
 * This is very important, if you don't set it correctly, DRAM will be
 * unreliable,
 *
 * Set DCLK Phase control(Reg99H[6:1]) according the DDRII in the DIMM.
 */
void DCLKPhsCtrl(DRAM_SYS_ATTR *DramAttr)
{
	u8 Data;

	Data = 0;       /* TODO: Can be dropped? */
	Data = pci_read_config8(MEMCTRL, 0x99);
	Data &= 0xE1;
	/* DDR in Dimm1, MCLKOA[4,3,0] will output MCLK */
	if (DramAttr->RankPresentMap & 0x03)
		Data |= 0x09 << 1;
	/* DDR in Dimm2, MCLKOA[5,2,1] will output MCLK */
	if (DramAttr->RankPresentMap & 0x0C)
		Data |= 0x06 << 1;

	pci_write_config8(MEMCTRL, 0x99, Data);
}