summaryrefslogtreecommitdiff
path: root/src/northbridge/via/vx800/timing_setting.c
blob: 6dbe570b3ef4dce6c894a535f9076e4100242cb3 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * 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.
 */

 /*
    Set Dram Timing functions
  */

void SetCL(DRAM_SYS_ATTR * DramAttr);

void SetTrp(DRAM_SYS_ATTR * DramAttr);

void SetTrcd(DRAM_SYS_ATTR * DramAttr);

void SetTras(DRAM_SYS_ATTR * DramAttr);

void SetTrfc(DRAM_SYS_ATTR * DramAttr);

void SetTrrd(DRAM_SYS_ATTR * DramAttr);

void SetTwr(DRAM_SYS_ATTR * DramAttr);

void SetTwtr(DRAM_SYS_ATTR * DramAttr);

void SetTrtp(DRAM_SYS_ATTR * DramAttr);

/* Set DRAM	Timing*/
void DRAMTimingSetting(DRAM_SYS_ATTR * DramAttr)
{
	PRINT_DEBUG_MEM("Set CAS latency value!");
	SetCL(DramAttr);

	PRINT_DEBUG_MEM("Set tRP value!");
	SetTrp(DramAttr);

	PRINT_DEBUG_MEM("Set tRCD value!");
	SetTrcd(DramAttr);

	PRINT_DEBUG_MEM("Set tRAS value!");
	SetTras(DramAttr);

	PRINT_DEBUG_MEM("Set tRFC value!");
	SetTrfc(DramAttr);

	PRINT_DEBUG_MEM("Set tRRD value!");
	SetTrrd(DramAttr);

	PRINT_DEBUG_MEM("Set tWR value!");
	SetTwr(DramAttr);

	PRINT_DEBUG_MEM("Set tWTR value!");
	SetTwtr(DramAttr);

	PRINT_DEBUG_MEM("Set tRTP value!");
	SetTrtp(DramAttr);
}

/*
Set DRAM Timing: CAS Latency for DDR1
D0F3RX62 bit[0:2] for CAS Latency;
*/
void SetCL(DRAM_SYS_ATTR * DramAttr)
{
	u8 Data;
	u8 CL;

	/*DDR2 CL Value: 20, 30, 40, 50 -> 2, 3, 4, 5 */
	CL = (u8) ((DramAttr->CL - 20) / 10);	//000,001,010,011

	PRINT_DEBUG_MEM("CAS = ");
	PRINT_DEBUG_MEM_HEX8(CL);
	PRINT_DEBUG_MEM("\n");
	Data = pci_read_config8(MEMCTRL, 0x62);
	Data = (u8) ((Data & 0xf8) | CL);
	pci_write_config8(MEMCTRL, 0x62, Data);
}

/*
 Minimum row precharge time, Trp for DDR1/DDR2
 D0F3Rx64[3:2] for Trp 2T~5T
*/
#define MAX_TRP 6
#define MIN_TRP 2

void SetTrp(DRAM_SYS_ATTR * DramAttr)
{
	u8 Data;
	u16 Max, Tmp;
	u8 Socket;

	/*get the max Trp value from SPD data
	   SPD Byte27, Bit7:2->1ns~63ns, Bit1:0->0ns, 0.25ns, 0.50ns, 0.75ns */
	Max = 0;
	for (Socket = 0; Socket < MAX_SOCKETS; Socket++) {
		if (DramAttr->DimmInfo[Socket].bPresence) {
			Tmp =
			    (u16) (DramAttr->
				   DimmInfo[Socket].SPDDataBuf[SPD_SDRAM_TRP]);
			if (Tmp > Max)
				Max = Tmp;
		}
		/*Calculate clock,this value should be 2T,3T,4T,5T */
	}
	Tmp =
	    (u16) CEIL_DIV(Max * 100, (DramAttr->DramCyc) << 2);
	PRINT_DEBUG_MEM("Trp = ");
	PRINT_DEBUG_MEM_HEX16(Tmp);
	PRINT_DEBUG_MEM("\r");

	if (Tmp > MAX_TRP)
		Tmp = MAX_TRP;
	else if (Tmp < MIN_TRP)
		Tmp = MIN_TRP;

	Tmp -= 2;		//00->2T, 01->3T, 10->4T, 11->5T
	Tmp <<= 1;		//bit1,2,3

	Data = pci_read_config8(MEMCTRL, 0x64);
	Data = (u8) ((Data & 0xf1) | (u8) Tmp);
	pci_write_config8(MEMCTRL, 0x64, Data);

	//enable DDR2 8-Bank Device Timing Constraint
	Data = pci_read_config8(MEMCTRL, 0x62);
	Data = (u8) ((Data & 0xf7) | 0x08);
	pci_write_config8(MEMCTRL, 0x62, Data);
}

/*
Minimum RAS to CAS dely,Trcd for DDR1/DDR2
D0F3Rx64[7:6] for Trcd
*/
#define MAX_TRCD 6
#define MIN_TRCD 2

void SetTrcd(DRAM_SYS_ATTR * DramAttr)
{
	u8 Data;
	u16 Max, Tmp;
	u8 Socket;

	/*get the max Trcd value from SPD data
	   SPD Byte29, Bit7:2->1ns~63ns, Bit1:0->0ns, 0.25ns, 0.50ns, 0.75ns */
	Max = 0;
	for (Socket = 0; Socket < MAX_SOCKETS; Socket++) {
		if (DramAttr->DimmInfo[Socket].bPresence) {
			Tmp =
			    (u16) (DramAttr->
				   DimmInfo[Socket].SPDDataBuf[SPD_SDRAM_TRCD]);
			if (Tmp > Max)
				Max = Tmp;
		}
	}
	/*Calculate clock,this value should be 2T,3T,4T,5T */
	Tmp =
	    (u16) CEIL_DIV(Max * 100, (DramAttr->DramCyc) << 2);
	PRINT_DEBUG_MEM("Trcd =");
	PRINT_DEBUG_MEM_HEX16(Tmp);
	PRINT_DEBUG_MEM("\r");

	if (Tmp > MAX_TRCD)
		Tmp = MAX_TRCD;
	else if (Tmp < MIN_TRCD)
		Tmp = MIN_TRCD;
	Tmp -= 2;		//00->2T, 01->3T, 10->4T, 11->5T
	Tmp <<= 5;		//bit5,6,7

	Data = pci_read_config8(MEMCTRL, 0x64);
	Data = (u8) ((Data & 0x1f) | (u8) Tmp);
	pci_write_config8(MEMCTRL, 0x64, Data);

}

/*
 minimum active to precharge time,Tras for DDR1/DDR2
 D0F3Rx62[7:4] Tras
*/
#define MAX_TRAS 20		//20T
#define MIN_TRAS 5		//5T
void SetTras(DRAM_SYS_ATTR * DramAttr)
{
	u8 Data;
	u16 Max, Tmp;
	u8 Socket;

	/*get the max Tras value from SPD data
	   SPD byte30: bit0:7 1ns~255ns */
	Max = 0;
	for (Socket = 0; Socket < MAX_SOCKETS; Socket++) {
		if (DramAttr->DimmInfo[Socket].bPresence) {
			Tmp =
			    (u16) (DramAttr->
				   DimmInfo[Socket].SPDDataBuf[SPD_SDRAM_TRAS]);
			if (Tmp > Max)
				Max = Tmp;
		}
	}

	/*Calculate clock,value range 5T-20T */
	Tmp = (u16) CEIL_DIV((Max * 100), DramAttr->DramCyc);
	PRINT_DEBUG_MEM("Tras =");
	PRINT_DEBUG_MEM_HEX16(Tmp);
	PRINT_DEBUG_MEM("\r");

	if (Tmp > MAX_TRAS)
		Tmp = MAX_TRAS;
	else if (Tmp < MIN_TRAS)
		Tmp = MIN_TRAS;
	Tmp -= 5;		//0->5T  ... 1111->20T
	Tmp <<= 4;		//bit4:7

	Data = pci_read_config8(MEMCTRL, 0x62);
	Data = (u8) ((Data & 0x0f) | (u8) Tmp);
	pci_write_config8(MEMCTRL, 0x62, Data);
}

/*
Minimum refresh to activate/refresh command period Trfc for DDR1/DDR2
D0F3Rx61[5:0] for Trfc
*/

#define MAX_TRFC 71		// Max supported,71T
#define MIN_TRFC 8		// Min supported,8T

void SetTrfc(DRAM_SYS_ATTR * DramAttr)
{

	u8 Data;
	u32 Max, Tmp;
	u8 Byte40;
	u8 Socket;

	/*get the max Trfc value from SPD data */
	Max = 0;
	for (Socket = 0; Socket < MAX_SOCKETS; Socket++) {
		if (DramAttr->DimmInfo[Socket].bPresence) {
			Tmp =
			    (u32) (DramAttr->
				   DimmInfo[Socket].SPDDataBuf[SPD_SDRAM_TRFC])
			    * 100;
			/*only DDR2 need to add byte 40 bit[7:4] */
			Byte40 =
			    (DramAttr->
			     DimmInfo[Socket].SPDDataBuf[SPD_SDRAM_TRFC2]);
			/*if bit0 = 1, byte42(RFC)+256ns, SPD spec JEDEC standard No.21.c */
			if (Byte40 & 0x01)
				Tmp += (256 * 100);
			/*bit1,2,3 000->0ns+byte42; 001->0.25ns+byte42; 010->0.33ns+byte42; 011->0.5ns+byte42;100-> 0.75ns+byte42 */
			switch ((Byte40 >> 1) & 0x07) {	/*bit1,2,3 */
			case 1:
				Tmp += 25;
				break;
			case 2:
				Tmp += 33;
				break;
			case 3:
				Tmp += 50;
				break;
			case 4:
				Tmp += 66;
				break;
			case 5:
				Tmp += 75;
				break;
			case 6:	//what is FRU???
			default:
				break;
			}
			if (Tmp > Max)
				Max = Tmp;
		}
	}

	/*Calculate clock,value range 8T-71T */
	Tmp = (u16) CEIL_DIV(Max, DramAttr->DramCyc);
	PRINT_DEBUG_MEM("Trfc = ");
	PRINT_DEBUG_MEM_HEX16(Tmp);
	PRINT_DEBUG_MEM("\r");
	if (Tmp > MAX_TRFC)
		Tmp = MAX_TRFC;
	else if (Tmp < MIN_TRFC) {
		// return;
		Tmp = 0x40;
	}
	/*D0F3Rx61 bit[0:5] 0->8T ... 63->71T */
	Tmp -= 8;

	Data = pci_read_config8(MEMCTRL, 0x61);
	Data = (u8) ((Data & 0xc0) | ((u8) Tmp & 0x3f));
	pci_write_config8(MEMCTRL, 0x61, Data);
}

/*
Minimum row active to row active delay: Trrd for DDR1/DDR2
D0F3Rx61[7:6]:Trrd  00->2T, 01->3T, 10->4T, 11->5T
*/
#define MAX_TRRD 5
#define MIN_TRRD 2

void SetTrrd(DRAM_SYS_ATTR * DramAttr)
{
	u8 Data;
	u16 Max, Tmp;
	u8 Socket;

	/*get the max Trrd value from SPD data
	   SPD Byte28, Bit7:2->1ns~63ns, Bit1:0->0ns, 0.25ns, 0.50ns, 0.75ns */
	Max = 0;
	for (Socket = 0; Socket < MAX_SOCKETS; Socket++) {
		if (DramAttr->DimmInfo[Socket].bPresence) {
			Tmp =
			    (u16) (DramAttr->
				   DimmInfo[Socket].SPDDataBuf[SPD_SDRAM_TRRD]);
			if (Tmp > Max)
				Max = Tmp;
		}
	}

	/*Calculate clock,this value should be 2T,3T,4T,5T */
	Tmp =
	    (u16) CEIL_DIV(Max * 100, (DramAttr->DramCyc) << 2);
	PRINT_DEBUG_MEM("Trrd =");
	PRINT_DEBUG_MEM_HEX16(Tmp);
	PRINT_DEBUG_MEM("\r");

	if (Tmp > MAX_TRRD)
		Tmp = MAX_TRRD;
	else if (Tmp < MIN_TRRD)
		Tmp = MIN_TRRD;
	Tmp -= 2;		//00->2T, 01->3T, 10->4T, 11->5T
	Tmp <<= 6;

	Data = pci_read_config8(MEMCTRL, 0x61);
	Data = (u8) ((Data & 0x3f) | (u8) Tmp);
	pci_write_config8(MEMCTRL, 0x61, Data);
}

/*
Write recovery time: Twr for DDR1/DDR2
Device 0 Function 3:REG63[7:5]:Twr 00->2T 01->3T 10->4T 11->5T
*/
#define MAX_TWR 6
#define MIN_TWR 2

void SetTwr(DRAM_SYS_ATTR * DramAttr)
{
	u8 Data;
	u16 Max, Tmp;
	u8 Socket;

	/*get the max Trtp value from SPD data
	   SPD Byte36, Bit7:2->1ns~63ns, Bit1:0->0ns, 0.25ns, 0.50ns, 0.75ns */
	Max = 0;
	for (Socket = 0; Socket < MAX_SOCKETS; Socket++) {
		if (DramAttr->DimmInfo[Socket].bPresence) {
			Tmp =
			    (u16) (DramAttr->
				   DimmInfo[Socket].SPDDataBuf[SPD_SDRAM_TWR]);
			if (Tmp > Max)
				Max = Tmp;
		}
	}
	/*Calculate clock */
	Tmp = (u16) CEIL_DIV((Max * 100), ((DramAttr->DramCyc) << 2));	//this value should be 2T,3T,4T,5T
	PRINT_DEBUG_MEM("Twr = ");
	PRINT_DEBUG_MEM_HEX16(Tmp);
	PRINT_DEBUG_MEM("\r");

	if (Tmp > MAX_TWR)
		Tmp = MAX_TWR;
	else if (Tmp < MIN_TWR)
		Tmp = MIN_TWR;
	Tmp -= 2;		//00->2T, 01->3T, 10->4T, 11->5T
	Tmp <<= 5;

	Data = pci_read_config8(MEMCTRL, 0x63);
	Data = (u8) ((Data & 0x1f) | (u8) Tmp);
	pci_write_config8(MEMCTRL, 0x63, Data);
}

/*
Internal write to read command delay: Twtr for DDR1/DDR2
Device 0 Function 3:REG63[1,0]:Twtr   DDR: 1T or 2T; DDR2 2T or 3T
*/
#define MAX_TWTR 5		//5T
#define MIN_TWTR 2		//2T

void SetTwtr(DRAM_SYS_ATTR * DramAttr)
{
	u8 Data;
	u16 Max, Tmp;
	u8 Socket;

	/*get the max Trtp value from SPD data
	   SPD Byte37, Bit7:2->1ns~63ns, Bit1:0->0ns, 0.25ns, 0.50ns, 0.75ns */
	Max = 0;
	for (Socket = 0; Socket < MAX_SOCKETS; Socket++) {
		if (DramAttr->DimmInfo[Socket].bPresence) {
			Tmp =
			    (u16) (DramAttr->
				   DimmInfo[Socket].SPDDataBuf[SPD_SDRAM_TWTR]);
			if (Tmp > Max)
				Max = Tmp;
		}
	}
	/*Calculate clock */
	Tmp = (u16) CEIL_DIV((Max * 100), ((DramAttr->DramCyc) << 2));	//this value should be 2T or 3T

	PRINT_DEBUG_MEM("Twtr =");
	PRINT_DEBUG_MEM_HEX16(Tmp);
	PRINT_DEBUG_MEM("\r");

	if (Tmp > MAX_TWR)
		Tmp = MAX_TWTR;
	else if (Tmp < MIN_TWR)
		Tmp = MIN_TWTR;
	Tmp -= 2;		//00->2T, 01->3T, 10->4T, 11->5T
	Data = pci_read_config8(MEMCTRL, 0x63);
	Data = (u8) ((Data & 0xFC) | Tmp);
	pci_write_config8(MEMCTRL, 0x63, Data);
}

/*
Internal read to precharge command delay, Trtp for DDR1/DDR2
Device 0 Function 3:REG63[3]:Trtp  2T or 3T
*/
#define MAX_TRTP 3		//3T
#define MIN_TRTP 2		//2T

void SetTrtp(DRAM_SYS_ATTR * DramAttr)
{
	u8 Data;
	u16 Max, Tmp;
	u8 Socket;

	/*get the max Trtp value from SPD data
	   SPD Byte38, Bit7:2->1ns~63ns, Bit1:0->0ns, 0.25ns, 0.50ns, 0.75ns */
	Max = 0;
	for (Socket = 0; Socket < MAX_SOCKETS; Socket++) {
		if (DramAttr->DimmInfo[Socket].bPresence) {
			Tmp =
			    (u16) (DramAttr->
				   DimmInfo[Socket].SPDDataBuf[SPD_SDRAM_TRTP]);
			if (Tmp > Max)
				Max = Tmp;
		}
	}
	/*Calculate clock */
	Tmp = (u16) CEIL_DIV((Max * 100), ((DramAttr->DramCyc) << 2));	//this value should be 2T or 3T

	PRINT_DEBUG_MEM("Trtp =");
	PRINT_DEBUG_MEM_HEX16(Tmp);
	PRINT_DEBUG_MEM("\r");

	Data = pci_read_config8(MEMCTRL, 0x63);
	if (Tmp > MIN_TRTP)
		Data = (u8) (Data | 0x08);	/*set bit3, set 3T */
	else
		Data = (u8) (Data & 0xf7);	/*clear bit3, set 2T */

	pci_write_config8(MEMCTRL, 0x63, Data);
}