source: rtems/c/src/lib/libbsp/powerpc/shared/pci/pcifinddevice.c @ 2b0a037

Last change on this file since 2b0a037 was 2b0a037, checked in by Till Straumann <strauman@…>, on 01/16/07 at 23:09:50

2007-01-16 Till Straumann <strauman@…>

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