1 | /* find a particular PCI device |
---|
2 | * (we assume, the firmware configured the PCI bus[es] for us) |
---|
3 | */ |
---|
4 | |
---|
5 | /* |
---|
6 | * Authorship |
---|
7 | * ---------- |
---|
8 | * This software was created by |
---|
9 | * Till Straumann <strauman@slac.stanford.edu>, 2001, |
---|
10 | * Stanford Linear Accelerator Center, Stanford University. |
---|
11 | * |
---|
12 | * Acknowledgement of sponsorship |
---|
13 | * ------------------------------ |
---|
14 | * This software was produced by |
---|
15 | * the Stanford Linear Accelerator Center, Stanford University, |
---|
16 | * under Contract DE-AC03-76SFO0515 with the Department of Energy. |
---|
17 | * |
---|
18 | * Government disclaimer of liability |
---|
19 | * ---------------------------------- |
---|
20 | * Neither the United States nor the United States Department of Energy, |
---|
21 | * nor any of their employees, makes any warranty, express or implied, or |
---|
22 | * assumes any legal liability or responsibility for the accuracy, |
---|
23 | * completeness, or usefulness of any data, apparatus, product, or process |
---|
24 | * disclosed, or represents that its use would not infringe privately owned |
---|
25 | * rights. |
---|
26 | * |
---|
27 | * Stanford disclaimer of liability |
---|
28 | * -------------------------------- |
---|
29 | * Stanford University makes no representations or warranties, express or |
---|
30 | * implied, nor assumes any liability for the use of this software. |
---|
31 | * |
---|
32 | * Stanford disclaimer of copyright |
---|
33 | * -------------------------------- |
---|
34 | * Stanford University, owner of the copyright, hereby disclaims its |
---|
35 | * copyright and all other rights in this software. Hence, anyone may |
---|
36 | * freely use it for any purpose without restriction. |
---|
37 | * |
---|
38 | * Maintenance of notices |
---|
39 | * ---------------------- |
---|
40 | * In the interest of clarity regarding the origin and status of this |
---|
41 | * SLAC software, this and all the preceding Stanford University notices |
---|
42 | * are to remain affixed to any copy or derivative of this software made |
---|
43 | * or distributed by the recipient and are to be affixed to any copy of |
---|
44 | * software made or distributed by the recipient that contains a copy or |
---|
45 | * derivative of this software. |
---|
46 | * |
---|
47 | * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03 |
---|
48 | */ |
---|
49 | |
---|
50 | #include <inttypes.h> |
---|
51 | #include <bsp/pci.h> |
---|
52 | #include <rtems/bspIo.h> |
---|
53 | #include <stdio.h> |
---|
54 | |
---|
55 | /* Stolen from i386... */ |
---|
56 | |
---|
57 | /* |
---|
58 | * Make device signature from bus number, device number and function |
---|
59 | * number |
---|
60 | */ |
---|
61 | #define PCIB_DEVSIG_MAKE(b,d,f) ((b<<8)|(d<<3)|(f)) |
---|
62 | |
---|
63 | /* |
---|
64 | * Extract various parts from device signature |
---|
65 | */ |
---|
66 | #define PCIB_DEVSIG_BUS(x) (((x)>>8) &0xff) |
---|
67 | #define PCIB_DEVSIG_DEV(x) (((x)>>3) & 0x1f) |
---|
68 | #define PCIB_DEVSIG_FUNC(x) ((x) & 0x7) |
---|
69 | |
---|
70 | typedef struct { |
---|
71 | unsigned short vid,did; |
---|
72 | int inst; |
---|
73 | } fd_arg; |
---|
74 | |
---|
75 | static int |
---|
76 | find_dev_cb( |
---|
77 | int bus, |
---|
78 | int dev, |
---|
79 | int fun, |
---|
80 | void *uarg |
---|
81 | ) { |
---|
82 | fd_arg *a = uarg; |
---|
83 | unsigned short s; |
---|
84 | |
---|
85 | pci_read_config_word(bus,dev,fun,PCI_VENDOR_ID,&s); |
---|
86 | if (a->vid == s) { |
---|
87 | pci_read_config_word(bus,dev,fun,PCI_DEVICE_ID,&s); |
---|
88 | if (a->did == s && 0 == a->inst-- ) { |
---|
89 | a->inst = PCIB_DEVSIG_MAKE( bus, dev, fun ); |
---|
90 | return 1; |
---|
91 | } |
---|
92 | } |
---|
93 | return 0; |
---|
94 | } |
---|
95 | |
---|
96 | int |
---|
97 | pci_find_device( |
---|
98 | unsigned short vendorid, |
---|
99 | unsigned short deviceid, |
---|
100 | int instance, |
---|
101 | int *pbus, |
---|
102 | int *pdev, |
---|
103 | int *pfun |
---|
104 | ) { |
---|
105 | fd_arg a; |
---|
106 | void *h; |
---|
107 | a.vid = vendorid; |
---|
108 | a.did = deviceid; |
---|
109 | a.inst = instance; |
---|
110 | |
---|
111 | if ( (h = BSP_pciScan(0, find_dev_cb, (void*)&a)) ) { |
---|
112 | *pbus = PCIB_DEVSIG_BUS( a.inst ); |
---|
113 | *pdev = PCIB_DEVSIG_DEV( a.inst ); |
---|
114 | *pfun = PCIB_DEVSIG_FUNC( a.inst ); |
---|
115 | return 0; |
---|
116 | } |
---|
117 | return -1; |
---|
118 | } |
---|
119 | |
---|
120 | static int |
---|
121 | dump_dev_cb( |
---|
122 | int bus, |
---|
123 | int dev, |
---|
124 | int fun, |
---|
125 | void *uarg |
---|
126 | ) |
---|
127 | { |
---|
128 | uint16_t vi,di; |
---|
129 | uint16_t cd,st; |
---|
130 | uint32_t b1,b2; |
---|
131 | uint8_t il,ip; |
---|
132 | FILE *f = uarg; |
---|
133 | |
---|
134 | pci_read_config_word (bus, dev, fun, PCI_VENDOR_ID, &vi); |
---|
135 | pci_read_config_word (bus, dev, fun, PCI_DEVICE_ID, &di); |
---|
136 | pci_read_config_word (bus, dev, fun, PCI_COMMAND, &cd); |
---|
137 | pci_read_config_word (bus, dev, fun, PCI_STATUS, &st); |
---|
138 | pci_read_config_dword(bus, dev, fun, PCI_BASE_ADDRESS_0, &b1); |
---|
139 | pci_read_config_dword(bus, dev, fun, PCI_BASE_ADDRESS_1, &b2); |
---|
140 | pci_read_config_byte (bus, dev, fun, PCI_INTERRUPT_LINE, &il); |
---|
141 | pci_read_config_byte (bus, dev, fun, PCI_INTERRUPT_PIN, &ip); |
---|
142 | |
---|
143 | /* fprintf(f,"%3d:0x%02x:%d 0x%04x-0x%04x: 0x%04x 0x%04x 0x%08x 0x%08x %d -> %3d (=0x%02x)\n", */ |
---|
144 | fprintf(f,"%3d:0x%02x:%d 0x%04x-0x%04x: 0x%04x 0x%04x 0x%08" PRIx32 " 0x%08" PRIx32 " %d -> %3d (=0x%02x)\n", |
---|
145 | bus, dev, fun, vi, di, cd, st, b1, b2, ip, il, il); |
---|
146 | return 0; |
---|
147 | } |
---|
148 | |
---|
149 | void |
---|
150 | BSP_pciConfigDump(FILE *f) |
---|
151 | { |
---|
152 | if ( !f ) |
---|
153 | f = stdout; |
---|
154 | fprintf(f,"BUS:SLOT:FUN VENDOR-DEV_ID: COMMAND STATUS BASE_ADDR0 BASE_ADDR1 IRQ_PIN -> IRQ_LINE\n"); |
---|
155 | BSP_pciScan(0, dump_dev_cb, f); |
---|
156 | } |
---|
157 | |
---|
158 | BSP_PciScanHandle |
---|
159 | BSP_pciScan( |
---|
160 | BSP_PciScanHandle handle, |
---|
161 | BSP_PciScannerCb cb, |
---|
162 | void *uarg |
---|
163 | ) { |
---|
164 | |
---|
165 | uint32_t d; |
---|
166 | unsigned char bus,dev,fun,hd; |
---|
167 | |
---|
168 | bus = PCIB_DEVSIG_BUS( (unsigned long)handle ); |
---|
169 | dev = PCIB_DEVSIG_DEV( (unsigned long)handle ); |
---|
170 | fun = PCIB_DEVSIG_FUNC( (unsigned long)handle ); |
---|
171 | |
---|
172 | hd = fun > 0 ? PCI_MAX_FUNCTIONS : 1; |
---|
173 | |
---|
174 | for (; bus<pci_bus_count(); bus++, dev=0) { |
---|
175 | for (; dev<PCI_MAX_DEVICES; dev++, fun=0) { |
---|
176 | for (; fun<hd; fun++) { |
---|
177 | /* |
---|
178 | * The last devfn id/slot is special; must skip it |
---|
179 | */ |
---|
180 | if (PCI_MAX_DEVICES-1==dev && PCI_MAX_FUNCTIONS-1 == fun) |
---|
181 | break; |
---|
182 | |
---|
183 | (void)pci_read_config_dword(bus,dev,0,PCI_VENDOR_ID,&d); |
---|
184 | if (PCI_INVALID_VENDORDEVICEID == d) |
---|
185 | continue; |
---|
186 | |
---|
187 | if ( 0 == fun ) { |
---|
188 | pci_read_config_byte(bus,dev,0, PCI_HEADER_TYPE, &hd); |
---|
189 | hd = (hd & PCI_HEADER_TYPE_MULTI_FUNCTION ? PCI_MAX_FUNCTIONS : 1); |
---|
190 | } |
---|
191 | |
---|
192 | (void)pci_read_config_dword(bus,dev,fun,PCI_VENDOR_ID,&d); |
---|
193 | if (PCI_INVALID_VENDORDEVICEID == d) |
---|
194 | continue; |
---|
195 | #ifdef PCI_DEBUG |
---|
196 | printk("BSP_pciScan: found 0x%08x at %d/x%02x/%d\n",d,bus,dev,fun); |
---|
197 | #endif |
---|
198 | if ( cb(bus,dev,fun,uarg) > 0 ) { |
---|
199 | if ( ++fun >= hd ) { |
---|
200 | fun = 0; |
---|
201 | if ( ++dev >= PCI_MAX_DEVICES ) { |
---|
202 | dev = 0; |
---|
203 | bus++; |
---|
204 | } |
---|
205 | } |
---|
206 | return (void*) PCIB_DEVSIG_MAKE(bus,dev,fun); |
---|
207 | } |
---|
208 | } |
---|
209 | } |
---|
210 | } |
---|
211 | return 0; |
---|
212 | } |
---|