[a31845f7] | 1 | /* PCI Help Function, iterate over all PCI devices. Find devices by cfg access. |
---|
| 2 | * |
---|
[71e8a5c] | 3 | * COPYRIGHT (c) 2010 Cobham Gaisler AB. |
---|
[a31845f7] | 4 | * |
---|
| 5 | * The license and distribution terms for this file may be |
---|
| 6 | * found in the file LICENSE in this distribution or at |
---|
| 7 | * http://www.rtems.com/license/LICENSE. |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include <pci.h> |
---|
| 11 | #include <pci/access.h> |
---|
| 12 | |
---|
| 13 | /*#define DEBUG*/ |
---|
| 14 | |
---|
| 15 | #ifdef DEBUG |
---|
| 16 | #include <rtems/bspIo.h> |
---|
| 17 | #define DBG(args...) printk(args) |
---|
| 18 | #else |
---|
| 19 | #define DBG(args...) |
---|
| 20 | #endif |
---|
| 21 | |
---|
| 22 | int pci_for_each(int (*func)(pci_dev_t, void*), void *arg) |
---|
| 23 | { |
---|
| 24 | uint32_t id; |
---|
| 25 | uint8_t hd; |
---|
| 26 | int bus, dev, fun, result, fail; |
---|
| 27 | int maxbus = pci_bus_count(); |
---|
| 28 | pci_dev_t pcidev; |
---|
| 29 | |
---|
| 30 | for (bus = 0; bus < maxbus ; bus++) { |
---|
| 31 | for (dev = 0; dev < PCI_MAX_DEVICES; dev++) { |
---|
| 32 | pcidev = PCI_DEV(bus, dev, 0); |
---|
| 33 | |
---|
| 34 | for (fun = 0; fun < PCI_MAX_FUNCTIONS; fun++, pcidev++) { |
---|
| 35 | fail = pci_cfg_r32(pcidev, PCI_VENDOR_ID, &id); |
---|
| 36 | if (fail || (0xffffffff == id) || (0 == id)) { |
---|
| 37 | if (fun == 0) |
---|
| 38 | break; |
---|
| 39 | else |
---|
| 40 | continue; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | DBG("pcibus_for_each: found 0x%08lx at" |
---|
| 44 | " %d/%d/%d\n", id, bus, dev, fun); |
---|
| 45 | result = func(pcidev, arg); |
---|
| 46 | if (result != 0) |
---|
| 47 | return result; /* Stopped */ |
---|
| 48 | |
---|
| 49 | /* Stop if not a multi-function device */ |
---|
| 50 | if (fun == 0) { |
---|
| 51 | pci_cfg_r8(pcidev, PCI_HEADER_TYPE, |
---|
| 52 | &hd); |
---|
| 53 | if ((hd & PCI_MULTI_FUNCTION) == 0) |
---|
| 54 | break; |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | return 0; /* scanned all */ |
---|
| 61 | } |
---|