1 | /* pcifinddevice.c |
---|
2 | * |
---|
3 | * Copyright 2001, Till Straumann <strauman@slac.stanford.edu> |
---|
4 | * |
---|
5 | * find a particular PCI device |
---|
6 | * (we assume, the firmware configured the PCI bus[es] for us) |
---|
7 | * |
---|
8 | * |
---|
9 | * Kate Feng <feng1@bnl.gov>, modified it to support |
---|
10 | * the mvme5500 board and provided glues to Till's vmeUniverse.c. |
---|
11 | * |
---|
12 | */ |
---|
13 | |
---|
14 | #define PCI_INVALID_VENDORDEVICEID 0xffffffff |
---|
15 | #define PCI_MULTI_FUNCTION 0x80 |
---|
16 | |
---|
17 | /*#define PCI_DEBUG*/ |
---|
18 | |
---|
19 | #include <bsp/pci.h> |
---|
20 | #include <rtems/bspIo.h> |
---|
21 | |
---|
22 | int BSP_PCIxFindDevice(unsigned short vendorid, unsigned short deviceid, |
---|
23 | int instance, int pciNum, int *pbus, int *pdev, int *pfun ) |
---|
24 | { |
---|
25 | unsigned int d; |
---|
26 | unsigned short s; |
---|
27 | unsigned char bus,dev,fun,hd; |
---|
28 | |
---|
29 | for (bus=0; bus<2; bus++) { |
---|
30 | for (dev=0; dev<PCI_MAX_DEVICES; dev++) { |
---|
31 | PCIx_read_config_byte(pciNum, bus, dev, 0, PCI0_HEADER_TYPE, &hd); |
---|
32 | hd = (hd & PCI_MULTI_FUNCTION ? PCI_MAX_FUNCTIONS : 1); |
---|
33 | for (fun=0; fun<hd; fun++) { |
---|
34 | /* |
---|
35 | * The last devfn id/slot is special; must skip it |
---|
36 | */ |
---|
37 | if (PCI_MAX_DEVICES-1==dev && PCI_MAX_FUNCTIONS-1 == fun) |
---|
38 | break; |
---|
39 | (void)PCIx_read_config_dword(pciNum, bus,dev,fun,PCI0_VENDOR_ID,&d); |
---|
40 | if (PCI_INVALID_VENDORDEVICEID == d) |
---|
41 | continue; |
---|
42 | #ifdef PCI_DEBUG |
---|
43 | printk("pci_find_device: found 0x%08x at %d/%d/%d\n",d,bus,dev,fun); |
---|
44 | #endif |
---|
45 | (void)PCIx_read_config_word(pciNum, bus,dev,fun,PCI0_VENDOR_ID,&s); |
---|
46 | if (vendorid != s) |
---|
47 | continue; |
---|
48 | (void)PCIx_read_config_word(pciNum, bus,dev,fun,PCI0_DEVICE_ID,&s); |
---|
49 | if (deviceid == s) { |
---|
50 | if (instance--) continue; |
---|
51 | *pbus=bus; *pdev=dev; *pfun=fun; |
---|
52 | return 0; |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | } /* end for bus */ |
---|
57 | return -1; |
---|
58 | } |
---|
59 | |
---|
60 | int pci_find_device( unsigned short vendorid, unsigned short deviceid, |
---|
61 | int instance, int *pbus, int *pdev, int *pfun ) |
---|
62 | { |
---|
63 | return(BSP_PCIxFindDevice(vendorid,deviceid,instance,0,pbus,pdev,pfun)); |
---|
64 | } |
---|
65 | |
---|
66 | /* eof */ |
---|