summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/powerpc/ppcn_60x/pci/pci.c
blob: 8978d88fe4dd9888a90bb231bcb21fdb798fc85e (plain) (blame)
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
/*
 *  COPYRIGHT (c) 1998 by Radstone Technology
 *
 *
 * THIS FILE IS PROVIDED TO YOU, THE USER, "AS IS", WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK
 * AS TO THE QUALITY AND PERFORMANCE OF ALL CODE IN THIS FILE IS WITH YOU.
 *
 * You are hereby granted permission to use, copy, modify, and distribute
 * this file, provided that this notice, plus the above copyright notice
 * and disclaimer, appears in all copies. Radstone Technology will provide
 * no support for this code.
 *
 */
#include <bsp.h>
#include <pci.h>

/* SCE 97/4/9
 *
 * Use PCI configuration space access mechanism 1
 *
 * This is the preferred access mechanism and must be used when accessing
 * bridged PCI busses.
 *
 * The address to be written to the PCI_CONFIG_ADDRESS port is constructed
 * thus (the representation below is little endian):
 *
 *  31  30   24 23        16 15        11 10        8 7          2 1   0
 *  ----------------------------------------------------------------------
 * | 1 | Resvd | Bus Number | Dev Number | Fn Number | Reg Number | 0 | 0 |
 *  ----------------------------------------------------------------------
 *
 * On bus 0, the first 'real' device is at Device number 11, the Eagle being
 * device 0. On all other busses, device numbering starts at 0.
 */
/*
 * Normal PCI device numbering on busses other than 0 is such that
 * that the first device (0) is attached to AD16, second (1) to AD17 etc.
 */
#define CONFIG_ADDRESS(Bus, Device, Function, Offset) \
    (0x80000000 | (Bus<<16) | \
    ((Device+(((Bus==0)&&(Device>0)) ? 10 : 0))<<11) | \
    (Function<<8) | \
    (Offset&~0x03))
#define BYTE_LANE_OFFSET(Offset) ((Offset)&0x3)

/*
 * Private data
 */
static uint8_t   ucMaxPCIBus;

/*
 * Public routines
 */
rtems_status_code PCIConfigWrite8(
	uint8_t   ucBusNumber,
	uint8_t   ucSlotNumber,
	uint8_t   ucFunctionNumber,
	uint8_t   ucOffset,
	uint8_t   ucValue
)
{
	ISR_Level Irql;

	/*
	 * Ensure that accesses to the addr/data ports are indivisible
	 */
	_ISR_Disable(Irql);

	/*
	 * Write to the configuration space address register
	 */
	outport_32(PCI_CONFIG_ADDR,
		   CONFIG_ADDRESS(ucBusNumber, ucSlotNumber,
				  ucFunctionNumber, ucOffset));
	/*
	 * Write to the configuration space data register with the appropriate
	 * offset
	 */
	outport_byte(PCI_CONFIG_DATA+BYTE_LANE_OFFSET(ucOffset), ucValue);

	 _ISR_Enable(Irql);

	return(RTEMS_SUCCESSFUL);
}

rtems_status_code PCIConfigWrite16(
	uint8_t   ucBusNumber,
	uint8_t   ucSlotNumber,
	uint8_t   ucFunctionNumber,
	uint8_t   ucOffset,
	uint16_t   usValue
)
{
	ISR_Level Irql;

	/*
	 * Ensure that accesses to the addr/data ports are indivisible
	 */
	_ISR_Disable(Irql);

	/*
	 * Write to the configuration space address register
	 */
	outport_32(PCI_CONFIG_ADDR,
		   CONFIG_ADDRESS(ucBusNumber, ucSlotNumber,
				  ucFunctionNumber, ucOffset));
	/*
	 * Write to the configuration space data register with the appropriate
	 * offset
	 */
	outport_16(PCI_CONFIG_DATA+BYTE_LANE_OFFSET(ucOffset), usValue);

	_ISR_Enable(Irql);

	return(RTEMS_SUCCESSFUL);
}

rtems_status_code PCIConfigWrite32(
	uint8_t   ucBusNumber,
	uint8_t   ucSlotNumber,
	uint8_t   ucFunctionNumber,
	uint8_t   ucOffset,
	uint32_t   ulValue
)
{
	ISR_Level Irql;

	/*
	 * Ensure that accesses to the addr/data ports are indivisible
	 */
	_ISR_Disable(Irql);

	/*
	 * Write to the configuration space address register
	 */
	outport_32(PCI_CONFIG_ADDR,
		   CONFIG_ADDRESS(ucBusNumber, ucSlotNumber,
				  ucFunctionNumber, ucOffset));
	/*
	 * Write to the configuration space data register with the appropriate
	 * offset
	 */
	outport_32(PCI_CONFIG_DATA, ulValue);

	_ISR_Enable(Irql);

	return(RTEMS_SUCCESSFUL);
}

rtems_status_code PCIConfigRead8(
	uint8_t   ucBusNumber,
	uint8_t   ucSlotNumber,
	uint8_t   ucFunctionNumber,
	uint8_t   ucOffset,
	uint8_t   *pucValue
)
{
	ISR_Level Irql;

	/*
	 * Ensure that accesses to the addr/data ports are indivisible
	 */
	_ISR_Disable(Irql);

	/*
	 * Write to the configuration space address register
	 */
	outport_32(PCI_CONFIG_ADDR,
		   CONFIG_ADDRESS(ucBusNumber, ucSlotNumber,
				  ucFunctionNumber, ucOffset));
	/*
	 * Read from the configuration space data register with the appropriate
	 * offset
	 */
	inport_byte(PCI_CONFIG_DATA+BYTE_LANE_OFFSET(ucOffset), *pucValue);

	_ISR_Enable(Irql);

	return(RTEMS_SUCCESSFUL);
}

rtems_status_code PCIConfigRead16(
	uint8_t   ucBusNumber,
	uint8_t   ucSlotNumber,
	uint8_t   ucFunctionNumber,
	uint8_t   ucOffset,
	uint16_t   *pusValue
)
{
	ISR_Level Irql;

	/*
	 * Ensure that accesses to the addr/data ports are indivisible
	 */
	_ISR_Disable(Irql);

	/*
	 * Write to the configuration space address register
	 */
	outport_32(PCI_CONFIG_ADDR,
		   CONFIG_ADDRESS(ucBusNumber, ucSlotNumber,
				  ucFunctionNumber, ucOffset));
	/*
	 * Read from the configuration space data register with the appropriate
	 * offset
	 */
	inport_16(PCI_CONFIG_DATA+BYTE_LANE_OFFSET(ucOffset), *pusValue);

	_ISR_Enable(Irql);

	return(RTEMS_SUCCESSFUL);
}

rtems_status_code PCIConfigRead32(
	uint8_t   ucBusNumber,
	uint8_t   ucSlotNumber,
	uint8_t   ucFunctionNumber,
	uint8_t   ucOffset,
	uint32_t   *pulValue
)
{
	ISR_Level Irql;

	/*
	 * Ensure that accesses to the addr/data ports are indivisible
	 */
	_ISR_Disable(Irql);

	/*
	 * Write to the configuration space address register
	 */
	outport_32(PCI_CONFIG_ADDR,
		   CONFIG_ADDRESS(ucBusNumber, ucSlotNumber,
				  ucFunctionNumber, ucOffset));
	/*
	 * Read from the configuration space data register with the appropriate
	 * offset
	 */
	inport_32(PCI_CONFIG_DATA, *pulValue);

	_ISR_Enable(Irql);

	return(RTEMS_SUCCESSFUL);
}

/*
 * This routine determines the maximum bus number in the system
 */
void pci_initialize()
{
	uint8_t   ucSlotNumber, ucFnNumber, ucNumFuncs;
	uint8_t   ucHeader;
	uint8_t   ucBaseClass, ucSubClass, ucMaxSubordinate;
	uint32_t   ulDeviceID;

	/*
	 * Scan PCI bus 0 looking for PCI-PCI bridges
	 */
	for(ucSlotNumber=0;ucSlotNumber<PCI_MAX_DEVICES;ucSlotNumber++)
	{
		(void)PCIConfigRead32(0,
				      ucSlotNumber,
				      0,
				      PCI_CONFIG_VENDOR_LOW,
				      &ulDeviceID);
		if(ulDeviceID==PCI_INVALID_VENDORDEVICEID)
		{
			/*
			 * This slot is empty
			 */
			continue;
		}
		(void)PCIConfigRead8(0,
				     ucSlotNumber,
				     0,
				     PCI_CONFIG_HEADER_TYPE,
				     &ucHeader);
		if(ucHeader&PCI_MULTI_FUNCTION)
		{
			ucNumFuncs=PCI_MAX_FUNCTIONS;
		}
		else
		{
			ucNumFuncs=1;
		}
		for(ucFnNumber=0;ucFnNumber<ucNumFuncs;ucFnNumber++)
		{
			(void)PCIConfigRead32(0,
					      ucSlotNumber,
					      ucFnNumber,
					      PCI_CONFIG_VENDOR_LOW,
					      &ulDeviceID);
			if(ulDeviceID==PCI_INVALID_VENDORDEVICEID)
			{
				/*
				 * This slot/function is empty
				 */
				continue;
			}

			/*
			 * This slot/function has a device fitted.
			 */
			(void)PCIConfigRead8(0,
					     ucSlotNumber,
					     ucFnNumber,
					     PCI_CONFIG_CLASS_CODE_U,
					     &ucBaseClass);
			(void)PCIConfigRead8(0,
					     ucSlotNumber,
					     ucFnNumber,
					     PCI_CONFIG_CLASS_CODE_M,
					     &ucSubClass);
			if((ucBaseClass==PCI_BASE_CLASS_BRIDGE) &&
			   (ucSubClass==PCI_SUB_CLASS_BRIDGE_PCI))
			{
				/*
				 * We have found a PCI-PCI bridge
				 */
				(void)PCIConfigRead8(0,
						     ucSlotNumber,
						     ucFnNumber,
						     PCI_BRIDGE_SUBORDINATE_BUS,
						     &ucMaxSubordinate);
				if(ucMaxSubordinate>ucMaxPCIBus)
				{
					ucMaxPCIBus=ucMaxSubordinate;
				}
			}
		}
	}
}

/*
 * Return the number of PCI busses in the system
 */
uint8_t   BusCountPCI()
{
	return(ucMaxPCIBus+1);
}