aboutsummaryrefslogtreecommitdiff
path: root/src/northbridge/amd/amdmct/mct_ddr3/mutilc_d.c
blob: f17e4d6758e69e9559afd9b2400200abdf5dde31 (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
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2010 Advanced Micro Devices, Inc.
 * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, Raptor Engineering
 *
 * 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.
 */

/* This file contains functions for common utility functions */
#include <inttypes.h>
#include <console/console.h>
#include <string.h>
#include "mct_d.h"
#include "mct_d_gcc.h"
#include "mwlc_d.h"

static uint8_t is_fam15h(void)
{
	uint8_t fam15h = 0;
	uint32_t family;

	family = cpuid_eax(0x80000001);
	family = ((family & 0xf00000) >> 16) | ((family & 0xf00) >> 8);

	if (family >= 0x6f)
		/* Family 15h or later */
		fam15h = 1;

	return fam15h;
}

void AmdMemPCIReadBits(SBDFO loc, u8 highbit, u8 lowbit, u32 *pValue)
{
	/* ASSERT(highbit < 32 && lowbit < 32 && highbit >= lowbit && (loc & 3) == 0); */

	AmdMemPCIRead(loc, pValue);
	*pValue = *pValue >> lowbit;  /* Shift */

	/* A 1<<32 == 1<<0 due to x86 SHL instruction, so skip if that is the case */
	if ((highbit-lowbit) != 31)
		*pValue &= (((u32)1 << (highbit-lowbit+1))-1);
}

void AmdMemPCIWriteBits(SBDFO loc, u8 highbit, u8 lowbit, u32 *pValue)
{
	u32 temp, mask;

	/* ASSERT(highbit < 32 && lowbit < 32 && highbit >= lowbit && (loc & 3) == 0); */

	/* A 1<<32 == 1<<0 due to x86 SHL instruction, so skip if that is the case */
	if ((highbit-lowbit) != 31)
		mask = (((u32)1 << (highbit-lowbit+1))-1);
	else
		mask = (u32)0xFFFFFFFF;

	AmdMemPCIRead(loc, &temp);
	temp &= ~(mask << lowbit);
	temp |= (*pValue & mask) << lowbit;
	AmdMemPCIWrite(loc, &temp);
}

/*-----------------------------------------------------------------------------
 * u32 bitTestSet(u32 csMask,u32 tempD)
 *
 * Description:
 *     This routine sets a bit in a u32
 *
 * Parameters:
 *     IN        csMask = Target value in which the bit will be set
 *     IN        tempD     =  Bit that will be set
 *     OUT    value     =  Target value with the bit set
 *-----------------------------------------------------------------------------
 */
u32 bitTestSet(u32 csMask,u32 tempD)
{
	u32 localTemp;
	/* ASSERT(tempD < 32); */
	localTemp = 1;
	csMask |= localTemp << tempD;
	return csMask;
}

/*-----------------------------------------------------------------------------
 * u32 bitTestReset(u32 csMask,u32 tempD)
 *
 * Description:
 *     This routine re-sets a bit in a u32
 *
 * Parameters:
 *     IN        csMask = Target value in which the bit will be re-set
 *     IN        tempD     =  Bit that will be re-set
 *     OUT    value     =  Target value with the bit re-set
 *-----------------------------------------------------------------------------
 */
u32 bitTestReset(u32 csMask,u32 tempD)
{
	u32 temp, localTemp;
	/* ASSERT(tempD < 32); */
	localTemp = 1;
	temp = localTemp << tempD;
	temp = ~temp;
	csMask &= temp;
	return csMask;
}

/*-----------------------------------------------------------------------------
 *  u32 get_Bits(DCTStruct *DCTData, u8 DCT, u8 Node, u8 func, u16 offset,
 *                 u8 low, u8 high)
 *
 * Description:
 *     This routine Gets the PCT bits from the specified Node, DCT and PCI address
 *
 * Parameters:
 *   IN  OUT *DCTData - Pointer to buffer with information about each DCT
 *     IN        DCT - DCT number
 *              - 1 indicates DCT 1
 *              - 0 indicates DCT 0
 *              - 2 both DCTs
 *          Node - Node number
 *          Func - PCI Function number
 *          Offset - PCI register number
 *          Low - Low bit of the bit field
 *          High - High bit of the bit field
 *
 *     OUT    value     =  Value read from PCI space
 *-----------------------------------------------------------------------------
 */
u32 get_Bits(sDCTStruct *pDCTData,
		u8 dct, u8 node, u8 func,
		u16 offset, u8 low, u8 high)
{
	u32 temp;
	uint32_t dword;

	/* ASSERT(node < MAX_NODES); */
	if (dct == BOTH_DCTS)
	{
		/* Registers exist on DCT0 only */
		if (is_fam15h())
		{
			/* Select DCT 0 */
			AmdMemPCIRead(MAKE_SBDFO(0,0,24+node,1,0x10c), &dword);
			dword &= ~0x1;
			AmdMemPCIWrite(MAKE_SBDFO(0,0,24+node,1,0x10c), &dword);
		}

		AmdMemPCIReadBits(MAKE_SBDFO(0,0,24+node,func,offset), high, low, &temp);
	}
	else
	{
		if (is_fam15h())
		{
			/* Select DCT */
			AmdMemPCIRead(MAKE_SBDFO(0,0,24+node,1,0x10c), &dword);
			dword &= ~0x1;
			dword |= (dct & 0x1);
			AmdMemPCIWrite(MAKE_SBDFO(0,0,24+node,1,0x10c), &dword);

			/* Read from the selected DCT */
			AmdMemPCIReadBits(MAKE_SBDFO(0,0,24+node,func,offset), high, low, &temp);
		}
		else
		{
			if (dct == 1)
			{
				/* Read from dct 1 */
				offset += 0x100;
				AmdMemPCIReadBits(MAKE_SBDFO(0,0,24+node,func,offset), high, low, &temp);
			}
			else
			{
				/* Read from dct 0 */
				AmdMemPCIReadBits(MAKE_SBDFO(0,0,24+node,func,offset), high, low, &temp);
			}
		}
	}
	return temp;
}

/*-----------------------------------------------------------------------------
 *  void set_Bits(DCTStruct *DCTData,u8 DCT,u8 Node,u8 func, u16 offset,
 *                u8 low, u8 high, u32 value)
 *
 * Description:
 *     This routine Sets the PCT bits from the specified Node, DCT and PCI address
 *
 * Parameters:
 *   IN  OUT *DCTData - Pointer to buffer with information about each DCT
 *     IN        DCT - DCT number
 *              - 1 indicates DCT 1
 *              - 0 indicates DCT 0
 *              - 2 both DCTs
 *          Node - Node number
 *          Func - PCI Function number
 *          Offset - PCI register number
 *          Low - Low bit of the bit field
 *          High - High bit of the bit field
 *
 *     OUT
 *-----------------------------------------------------------------------------
 */
void set_Bits(sDCTStruct *pDCTData,
		u8 dct, u8 node, u8 func,
		u16 offset, u8 low, u8 high, u32 value)
{
	u32 temp;
	uint32_t dword;

	temp = value;

	if (dct == BOTH_DCTS)
	{
		/* Registers exist on DCT0 only */
		if (is_fam15h())
		{
			/* Select DCT 0 */
			AmdMemPCIRead(MAKE_SBDFO(0,0,24+node,1,0x10c), &dword);
			dword &= ~0x1;
			AmdMemPCIWrite(MAKE_SBDFO(0,0,24+node,1,0x10c), &dword);
		}

		AmdMemPCIWriteBits(MAKE_SBDFO(0,0,24+node,func,offset), high, low, &temp);
	}
	else
	{
		if (is_fam15h())
		{
			/* Select DCT */
			AmdMemPCIRead(MAKE_SBDFO(0,0,24+node,1,0x10c), &dword);
			dword &= ~0x1;
			dword |= (dct & 0x1);
			AmdMemPCIWrite(MAKE_SBDFO(0,0,24+node,1,0x10c), &dword);

			/* Write to the selected DCT */
			AmdMemPCIWriteBits(MAKE_SBDFO(0,0,24+node,func,offset), high, low, &temp);
		}
		else
		{
			if (dct == 1)
			{
				/* Write to dct 1 */
				offset += 0x100;
				AmdMemPCIWriteBits(MAKE_SBDFO(0,0,24+node,func,offset), high, low, &temp);
			}
			else
			{
				/* Write to dct 0 */
				AmdMemPCIWriteBits(MAKE_SBDFO(0,0,24+node,func,offset), high, low, &temp);
			}
		}
	}
}

/*-------------------------------------------------
 *  u32 get_ADD_DCT_Bits(DCTStruct *DCTData,u8 DCT,u8 Node,u8 func,
 *                         u16 offset,u8 low, u8 high)
 *
 * Description:
 *     This routine gets the Additional PCT register from Function 2 by specified
 *   Node, DCT and PCI address
 *
 * Parameters:
 *   IN  OUT *DCTData - Pointer to buffer with information about each DCT
 *     IN        DCT - DCT number
 *              - 1 indicates DCT 1
 *              - 0 indicates DCT 0
 *              - 2 both DCTs
 *          Node - Node number
 *          Func - PCI Function number
 *          Offset - Additional PCI register number
 *          Low - Low bit of the bit field
 *          High - High bit of the bit field
 *
 *     OUT
 *-------------------------------------------------
 */
u32 get_ADD_DCT_Bits(sDCTStruct *pDCTData,
		u8 dct, u8 node, u8 func,
		u16 offset, u8 low, u8 high)
{
	u32 tempD;
	tempD = offset;
	tempD = bitTestReset(tempD,DctAccessWrite);
	set_Bits(pDCTData, dct, node, FUN_DCT, DRAM_CONTROLLER_ADD_DATA_OFFSET_REG,
		PCI_MIN_LOW, PCI_MAX_HIGH, offset);
	while ((get_Bits(pDCTData,dct, node, FUN_DCT, DRAM_CONTROLLER_ADD_DATA_OFFSET_REG,
			DctAccessDone, DctAccessDone)) == 0);
	return (get_Bits(pDCTData, dct, node, FUN_DCT, DRAM_CONTROLLER_ADD_DATA_PORT_REG,
			low, high));
}

/*-------------------------------------------------
 *  void set_DCT_ADDR_Bits(DCTStruct *DCTData, u8 DCT,u8 Node,u8 func,
 *                         u16 offset,u8 low, u8 high, u32 value)
 *
 * Description:
 *     This routine sets the Additional PCT register from Function 2 by specified
 *   Node, DCT and PCI address
 *
 * Parameters:
 *   IN  OUT *DCTData - Pointer to buffer with information about each DCT
 *     IN        DCT - DCT number
 *              - 1 indicates DCT 1
 *              - 0 indicates DCT 0
 *              - 2 both DCTs
 *          Node - Node number
 *          Func - PCI Function number
 *          Offset - Additional PCI register number
 *          Low - Low bit of the bit field
 *          High - High bit of the bit field
 *
 *     OUT
 *-------------------------------------------------
 */
void set_DCT_ADDR_Bits(sDCTStruct *pDCTData,
		u8 dct, u8 node, u8 func,
		u16 offset, u8 low, u8 high, u32 value)
{
	u32 tempD;

	set_Bits(pDCTData, dct, node, FUN_DCT, DRAM_CONTROLLER_ADD_DATA_OFFSET_REG,
		PCI_MIN_LOW, PCI_MAX_HIGH, offset);
	while ((get_Bits(pDCTData,dct, node, FUN_DCT, DRAM_CONTROLLER_ADD_DATA_OFFSET_REG,
			DctAccessDone, DctAccessDone)) == 0);

	set_Bits(pDCTData, dct, node, FUN_DCT, DRAM_CONTROLLER_ADD_DATA_PORT_REG,
		low, high, value);
	tempD = offset;
	tempD = bitTestSet(tempD,DctAccessWrite);
	set_Bits(pDCTData, dct, node, FUN_DCT,DRAM_CONTROLLER_ADD_DATA_OFFSET_REG,
		PCI_MIN_LOW, PCI_MAX_HIGH, tempD);
	while ((get_Bits(pDCTData,dct, pDCTData->NodeId, FUN_DCT,
			DRAM_CONTROLLER_ADD_DATA_OFFSET_REG, DctAccessDone,
			DctAccessDone)) == 0);
}

/*-------------------------------------------------
 * BOOL bitTest(u32 value, u8 bitLoc)
 *
 * Description:
 *     This routine tests the value to determine if the bitLoc is set
 *
 * Parameters:
 *     IN        Value - value to be tested
 *          bitLoc - bit location to be tested
 *     OUT    TRUE - bit is set
 *          FALSE - bit is clear
 *-------------------------------------------------
 */
BOOL bitTest(u32 value, u8 bitLoc)
{
	u32 tempD, compD;
	tempD = value;
	compD = 0;
	compD = bitTestSet(compD,bitLoc);
	tempD &= compD;
	if (compD == tempD)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}