summaryrefslogtreecommitdiff
path: root/drivers/pci/pci_interface.c
blob: 0b2ee1f638dd53762d93a61d70f2fa32d1268ea5 (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
/* pci_interface.c - PCI bus support */

/*
 * Copyright (c) 2009-2011, 2013-2014 Wind River Systems, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1) Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2) Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3) Neither the name of Wind River Systems nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
DESCRIPTION

This module implements the PCI H/W access functions.

*/

#include <nanokernel.h>
#include <arch/cpu.h>

#include <pci/pci_mgr.h>
#include <string.h>
#include <board.h>

#if (PCI_CTRL_ADDR_REG == 0)
#error "PCI_CTRL_ADDR_REG cannot be zero"
#endif

#if (PCI_CTRL_DATA_REG == 0)
#error "PCI_CTRL_DATA_REG cannot be zero"
#endif

/******************************************************************************
*
* pci_ctrl_read - read a PCI controller register
*
* This routine reads the specified register from the PCI controller and
* places the data into the provided buffer.
*
* RETURNS: N/A
*
*/

static void pci_ctrl_read(uint32_t reg,   /* PCI register to read */
		    uint32_t *data, /* where to put the data */
		    uint32_t size /* size of the data to read (8/16/32 bits) */
		    )
{
	/* read based on the size requested */

	switch (size) {
		/* long (32 bits) */
	case SYS_PCI_ACCESS_32BIT:
		*data = PLB_LONG_REG_READ(reg);
		break;
		/* word (16 bits) */
	case SYS_PCI_ACCESS_16BIT:
		*data = PLB_WORD_REG_READ(reg);
		break;
		/* byte (8 bits) */
	case SYS_PCI_ACCESS_8BIT:
		*data = PLB_BYTE_REG_READ(reg);
		break;
	}
}

/******************************************************************************
*
* pci_ctrl_write - write a PCI controller register
*
* This routine writes the provided data to the specified register in the PCI
* controller.
*
* RETURNS: N/A
*
*/

static void pci_ctrl_write(uint32_t reg,  /* PCI register to write */
		     uint32_t data, /* data to write */
		     uint32_t size  /* size of the data to write (8/16/32 bits)
				      */
		     )
{
	/* write based on the size requested */

	switch (size) {
		/* long (32 bits) */
	case SYS_PCI_ACCESS_32BIT:
		PLB_LONG_REG_WRITE(data, reg);
		break;
		/* word (16 bits) */
	case SYS_PCI_ACCESS_16BIT:
		PLB_WORD_REG_WRITE(data, reg);
		break;
		/* byte (8 bits) */
	case SYS_PCI_ACCESS_8BIT:
		PLB_BYTE_REG_WRITE(data, reg);
		break;
	}
}

/******************************************************************************
*
* pci_ctrl_data_read - read the PCI controller data register
*
* This routine reads the data register of the specified PCI controller.
*
* RETURNS: 0 or -1
*
*/

static int pci_ctrl_data_read(uint32_t controller, /* controller number */
		       uint32_t offset,     /* offset within data region */
		       uint32_t *data,      /* returned data */
		       uint32_t size	/* size of data to read */
		       )
{
	/* we only support one controller */

	if (controller != DEFAULT_PCI_CONTROLLER)
		return (-1);

	pci_ctrl_read(PCI_CTRL_DATA_REG + offset, data, size);

	return 0;
}

/******************************************************************************
*
* pci_ctrl_data_write - write the PCI controller data register
*
* This routine writes the provided data to the data register of the
* specified PCI controller.
*
* RETURNS: 0 or -1
*
*/

static int pci_ctrl_data_write(uint32_t controller, /* controller number */
			uint32_t offset, /* offset within address register */
			uint32_t data,   /* data to write */
			uint32_t size    /* size of data */
			)
{
	/* we only support one controller */

	if (controller != DEFAULT_PCI_CONTROLLER)
		return (-1);

	pci_ctrl_write(PCI_CTRL_DATA_REG + offset, data, size);

	return 0;
}

/******************************************************************************
*
* pci_ctrl_addr_write - write the PCI controller address register
*
* This routine writes the provided data to the address register of the
* specified PCI controller.
*
* RETURNS: 0 or -1
*
*/

static int pci_ctrl_addr_write(uint32_t controller, /* controller number */
			uint32_t offset, /* offset within address register */
			uint32_t data,   /* data to write */
			uint32_t size    /* size of data */
			)
{
	/* we only support one controller */

	if (controller != DEFAULT_PCI_CONTROLLER)
		return (-1);

	pci_ctrl_write(PCI_CTRL_ADDR_REG + offset, data, size);
	return 0;
}

/*******************************************************************************
*
* pci_read - read a PCI register from a device
*
* This routine reads data from a PCI device's configuration space.  The
* device and register to read is specified by the address parameter ("addr")
* and must be set appropriately by the caller.  The address is defined by
* the structure type pci_addr_t and contains the following members:
*
*     bus:     PCI bus number (0-255)
*     device:  PCI device number (0-31)
*     func:    device function number (0-7)
*     reg:     device 32-bit register number to read (0-63)
*     offset:  offset within 32-bit register to read (0-3)
*
* The size parameter specifies the number of bytes to read from the PCI
* configuration space, valid values are 1, 2, and 4 bytes.  A 32-bit value
* is always returned but it will contain only the number of bytes specified
* by the size parameter.
*
* If multiple PCI controllers are present in the system, the controller id
* can be specified in the "controller" parameter.  If only one controller
* is present, the id DEFAULT_PCI_CONTROLLER can be used to denote this
* controller.
*
* Example:
*
*  union pci_addr_reg addr;
*  uint32_t status;
*
*  addr.field.bus    = 0;    /@ PCI bus zero        @/
*  addr.field.device = 1;    /@ PCI device one      @/
*  addr.field.func   = 0;    /@ PCI function zero   @/
*  addr.field.reg    = 4;    /@ PCI register 4      @/
*  addr.field.offset = 0;    /@ PCI register offset @/
*
*  pci_read (DEFAULT_PCI_CONTROLLER, addr, sizeof(uint16_t), &status);
*
*
* NOTE:
*   Reading of PCI data must be performed as an atomic operation. It is up to
*   the caller to enforce this.
*
* RETURNS: N/A
*
*/

void pci_read(uint32_t controller, /* PCI controller to use */
	     union pci_addr_reg addr,       /* PCI address to read   */
	     uint32_t size,       /* size of data in bytes */
	     uint32_t *data       /* data read from device */
	     )
{
	uint32_t access_size;
	uint32_t access_offset;

	/* validate the access size */

	switch (size) {
	case 1:
		access_size = SYS_PCI_ACCESS_8BIT;
		access_offset = addr.field.offset;
		break;
	case 2:
		access_size = SYS_PCI_ACCESS_16BIT;
		access_offset = addr.field.offset;
		break;
	case 4:
	default:
		access_size = SYS_PCI_ACCESS_32BIT;
		access_offset = 0;
		break;
	}

	/* ensure enable has been set */

	addr.field.enable = 1;

	/* clear the offset for the address register */

	addr.field.offset = 0;

	/* read the data from the PCI controller */

	pci_ctrl_addr_write(
		controller, PCI_NO_OFFSET, addr.value, SYS_PCI_ACCESS_32BIT);

	pci_ctrl_data_read(controller, access_offset, data, access_size);
}

/*******************************************************************************
*
* pci_write - write a to a PCI register
*
* This routine writes data to a PCI device's configuration space.  The
* device and register to write is specified by the address parameter ("addr")
* and must be set appropriately by the caller.  The address is defined by
* the structure type pci_addr_t and contains the following members:
*
*     bus:     PCI bus number (0-255)
*     device:  PCI device number (0-31)
*     func:    device function number (0-7)
*     reg:     device register number to read (0-63)
*     offset:  offset within 32-bit register to write (0-3)
*
* The size parameter specifies the number of bytes to write to the PCI
* configuration space, valid values are 1, 2, and 4 bytes.  A 32-bit value
* is always provided but only the number of bytes specified by the size
* parameter will be written to the device.
*
* If multiple PCI controllers are present in the system, the controller id
* can be specified in the "controller" parameter.  If only one controller
* is present, the id DEFAULT_PCI_CONTROLLER can be used to denote this
* controller.
*
* Example:
*
*  pci_addr_t addr;
*  uint32_t bar0 = 0xE0000000;
*
*  addr.field.bus    = 0;    /@ PCI bus zero        @/
*  addr.field.device = 1;    /@ PCI device one      @/
*  addr.field.func   = 0;    /@ PCI function zero   @/
*  addr.field.reg    = 16;   /@ PCI register 16     @/
*  addr.field.offset = 0;    /@ PCI register offset @/
*
*  pci_write (DEFAULT_PCI_CONTROLLER, addr, sizeof(uint32_t), bar0);
*
* NOTE:
*   Writing of PCI data must be performed as an atomic operation. It is up to
*   the caller to enforce this.
*
*
* RETURNS: N/A
*
*/

void pci_write(uint32_t controller, /* controller to use   */
	      union pci_addr_reg addr,       /* PCI address to read */
	      uint32_t size,       /* size in bytes       */
	      uint32_t data	/* data to write       */
	      )
{
	uint32_t access_size;
	uint32_t access_offset;

	/* validate the access size */

	switch (size) {
	case 1:
		access_size = SYS_PCI_ACCESS_8BIT;
		access_offset = addr.field.offset;
		break;
	case 2:
		access_size = SYS_PCI_ACCESS_16BIT;
		access_offset = addr.field.offset;
		break;
	case 4:
	default:
		access_size = SYS_PCI_ACCESS_32BIT;
		access_offset = 0;
		break;
	}

	/* ensure enable has been set */

	addr.field.enable = 1;

	/* clear the offset for the address register */

	addr.field.offset = 0;

	/* write the data to the PCI controller */

	pci_ctrl_addr_write(
		controller, PCI_NO_OFFSET, addr.value, SYS_PCI_ACCESS_32BIT);
	pci_ctrl_data_write(controller, access_offset, data, access_size);
}

/*******************************************************************************
*
* pci_header_get - get the PCI header for a device
*
* This routine reads the PCI header for the specified device and puts the
* result in the supplied header structure.
*
* RETURNS: N/A
*/

void pci_header_get(uint32_t controller,
			union pci_addr_reg pci_ctrl_addr,
			union pci_dev *pci_dev_header)
{
	uint32_t i;

	/* clear out the header */

	memset((char *)pci_dev_header, sizeof(union pci_dev), 0);

	/* fill in the PCI header from the device */

	for (i = 0; i < PCI_HEADER_WORDS; i++) {
		pci_ctrl_addr.field.reg = i;
		pci_read(controller,
			pci_ctrl_addr,
			sizeof(uint32_t),
			&pci_dev_header->words.word[i]);
	}
}