9 #include <ddekit/pci.h>
10 #include <ddekit/panic.h>
11 #include <minix/syslib.h>
13 #ifdef DDEBUG_LEVEL_PCI
15 #define DDEBUG DDEBUG_LEVEL_PCI
21 #define PCI_MAX_DEVS 32
23 #define PCI_TAKE_ALL (-1)
25 struct ddekit_pci_dev
{
26 int devind
; /* thats how we identify the defice at the pci server */
27 ddekit_uint16_t vid
; /* as we get them for */
28 /* free during iteration store them */
31 int slot
; /* slot should equal index in dev array */
32 int func
; /* don't support multiple functionalities yet -> 0 */
35 struct ddekit_pci_dev pci_devs
[PCI_MAX_DEVS
];
37 static struct ddekit_pci_dev
* ddekit_get_dev_helper(int bus
, int slot
,
40 /****************************************************************************/
41 /* ddekit_pci_init_only_one */
42 /****************************************************************************/
43 void ddekit_pci_init_only_one(int skip
)
46 * If skip is not PCI_TAKE_ALL this function will skip skip PCI DEVICES
47 * and than only take on PCI device.
50 int res
, count
, more
, take_all
= 0;
56 DDEBUG_MSG_INFO("Initializing PCI subsystem...");
66 for (count
= 0 ; count
< PCI_MAX_DEVS
; count
++) {
68 struct ddekit_pci_dev
*d
= &pci_devs
[count
];
72 res
= pci_first_dev(&d
->devind
, &d
->vid
, &d
->did
);
74 d
->devind
= pci_devs
[count
-1].devind
;
75 res
= pci_next_dev(&d
->devind
, &d
->vid
, &d
->did
);
78 if (res
&& d
->devind
!=0 && (take_all
|| skip
== 0)) {
80 DDEBUG_MSG_VERBOSE("Found pci device: "
81 "(ind: %x, vid: %x, did: %x) "
83 d
->devind
, d
->vid
, d
->did
, count
);
87 res
= pci_reserve_ok(d
->devind
);
89 ddekit_panic("ddekit_pci_init_only_one: "
90 "pci_reserve_ok failed (%d)\n",res
);
94 /* no more PCI devices */
95 DDEBUG_MSG_VERBOSE("Found %d PCI devices.", count
);
108 /****************************************************************************/
109 /* ddekit_pci_get_device_id */
110 /****************************************************************************/
111 void ddekit_pci_init(void)
113 ddekit_pci_init_only_one(DDEKIT_PCI_ANY_ID
);
116 /****************************************************************************/
117 /* ddekit_pci_get_device_id */
118 /****************************************************************************/
119 int ddekit_pci_get_device(int nr
, int *bus
, int *slot
, int *func
)
121 if(nr
>= 0 && nr
< PCI_MAX_DEVS
) {
133 /****************************************************************************/
134 /* ddekit_pci_get_device_id */
135 /****************************************************************************/
136 static struct ddekit_pci_dev
*
137 ddekit_get_dev_helper(int bus
, int slot
, int func
)
140 * Used internally to look up devices.
141 * Should make it easier to support multiple buses somewhen
143 struct ddekit_pci_dev
* ret
= 0;
144 if (slot
>= 0 && slot
< PCI_MAX_DEVS
) {
145 ret
= &pci_devs
[slot
];
147 if (ret
->devind
== -1) {
153 /****************************************************************************/
154 /* ddekit_pci_read */
155 /****************************************************************************/
157 (int bus
, int slot
, int func
, int pos
, int len
, ddekit_uint32_t
*val
)
161 return ddekit_pci_readb(bus
, slot
, func
, pos
,
162 (ddekit_uint8_t
*) val
);
164 return ddekit_pci_readw(bus
, slot
, func
, pos
,
165 (ddekit_uint16_t
*) val
);
167 return ddekit_pci_readl(bus
, slot
, func
, pos
, val
);
172 /****************************************************************************/
173 /* ddekit_pci_write */
174 /****************************************************************************/
176 (int bus
, int slot
, int func
, int pos
, int len
, ddekit_uint32_t val
)
180 return ddekit_pci_writeb(bus
, slot
, func
, pos
,
181 (ddekit_uint8_t
) val
);
183 return ddekit_pci_writew(bus
, slot
, func
, pos
,
184 (ddekit_uint16_t
) val
);
186 return ddekit_pci_writel(bus
, slot
, func
, pos
, val
);
191 /****************************************************************************/
192 /* ddekit_pci_readb */
193 /****************************************************************************/
194 int ddekit_pci_readb (int bus
, int slot
, int func
, int pos
, ddekit_uint8_t
*val
) {
195 struct ddekit_pci_dev
* dev
= ddekit_get_dev_helper(bus
, slot
, func
);
201 *val
= pci_attr_r8 (dev
->devind
, pos
);
202 DDEBUG_MSG_VERBOSE("bus: %d, slot: %d, func: %d, pos: %x %x",
203 bus
, slot
, func
, pos
, *val
);
209 /****************************************************************************/
210 /* ddekit_pci_readw */
211 /****************************************************************************/
213 (int bus
, int slot
, int func
, int pos
, ddekit_uint16_t
*val
) {
214 struct ddekit_pci_dev
* dev
= ddekit_get_dev_helper(bus
, slot
, func
);
220 *val
= pci_attr_r16 (dev
->devind
, pos
);
221 DDEBUG_MSG_VERBOSE("bus: %d, slot: %d, func: %d, pos: %x %x",
222 bus
, slot
, func
, pos
, *val
);
228 /****************************************************************************/
229 /* ddekit_pci_readl */
230 /****************************************************************************/
232 (int bus
, int slot
, int func
, int pos
, ddekit_uint32_t
*val
) {
233 struct ddekit_pci_dev
* dev
= ddekit_get_dev_helper(bus
, slot
, func
);
239 *val
= pci_attr_r32 (dev
->devind
, pos
);
240 DDEBUG_MSG_VERBOSE("bus: %d, slot: %d, func: %d, pos: %x %x",
241 bus
, slot
, func
, pos
, *val
);
247 /****************************************************************************/
248 /* ddekit_pci_writeb */
249 /****************************************************************************/
250 int ddekit_pci_writeb
251 (int bus
, int slot
, int func
, int pos
, ddekit_uint8_t val
) {
252 struct ddekit_pci_dev
* dev
= ddekit_get_dev_helper(bus
, slot
, func
);
254 pci_attr_w8 (dev
->devind
, pos
, val
);
255 DDEBUG_MSG_VERBOSE("bus: %d, slot: %d, func: %d, pos: %x %x",
256 bus
, slot
, func
, pos
, val
);
262 /****************************************************************************/
263 /* ddekit_pci_writel */
264 /****************************************************************************/
265 int ddekit_pci_writew
266 (int bus
, int slot
, int func
, int pos
, ddekit_uint16_t val
) {
267 struct ddekit_pci_dev
* dev
= ddekit_get_dev_helper(bus
, slot
, func
);
269 pci_attr_w16 (dev
->devind
, pos
, val
);
270 DDEBUG_MSG_VERBOSE("bus: %d, slot: %d, func: %d, pos: %x %x",
271 bus
,slot
,func
,pos
, val
);
277 /****************************************************************************/
278 /* ddekit_pci_writel */
279 /****************************************************************************/
280 int ddekit_pci_writel
281 (int bus
, int slot
, int func
, int pos
, ddekit_uint32_t val
) {
282 struct ddekit_pci_dev
* dev
= ddekit_get_dev_helper(bus
, slot
, func
);
284 pci_attr_w32 (dev
->devind
, pos
, val
);
285 DDEBUG_MSG_VERBOSE("bus: %d, slot: %d, func: %d, pos: %x %x",bus
,slot
,func
,pos
, val
);
291 /****************************************************************************/
292 /* ddekit_pci_find_device */
293 /****************************************************************************/
294 struct ddekit_pci_dev
*ddekit_pci_find_device
295 (int *bus
, int *slot
, int *func
, struct ddekit_pci_dev
*start
)
302 for(i
=0; i
< PCI_MAX_DEVS
; i
++)
304 /* start searching? */
306 search
= (&pci_devs
[i
]==start
);
309 struct ddekit_pci_dev
* dev
= &pci_devs
[i
];
310 if ((*slot
==dev
->slot
|| *slot
== DDEKIT_PCI_ANY_ID
)
311 && (*func
==dev
->func
|| *func
== DDEKIT_PCI_ANY_ID
))
323 /****************************************************************************/
324 /* ddekit_pci_get_vendor */
325 /****************************************************************************/
326 unsigned short ddekit_pci_get_vendor(struct ddekit_pci_dev
*dev
)
331 /****************************************************************************/
332 /* ddekit_pci_get_device_id */
333 /****************************************************************************/
334 unsigned short ddekit_pci_get_device_id(struct ddekit_pci_dev
*dev
)
340 * XXX: Those are neither used be DDEFBSD or DDELinux implement them
344 /****************************************************************************/
345 /* ddekit_pci_enable_device */
346 /****************************************************************************/
347 int ddekit_pci_enable_device(struct ddekit_pci_dev
*dev
)
353 /****************************************************************************/
354 /* ddekit_pci_disable_device */
355 /****************************************************************************/
356 int ddekit_pci_disable_device(struct ddekit_pci_dev
*dev
)
362 /****************************************************************************/
363 /* ddekit_pci_set_master */
364 /****************************************************************************/
365 void ddekit_pci_set_master(struct ddekit_pci_dev
*dev
)
371 /****************************************************************************/
372 /* ddekit_pci_get_sub_vendor */
373 /****************************************************************************/
374 unsigned short ddekit_pci_get_sub_vendor(struct ddekit_pci_dev
*dev
)
380 /****************************************************************************/
381 /* ddekit_pci_get_sub_device */
382 /****************************************************************************/
383 unsigned short ddekit_pci_get_sub_device(struct ddekit_pci_dev
*dev
)
389 /****************************************************************************/
390 /* ddekit_pci_get_dev_class */
391 /****************************************************************************/
392 unsigned ddekit_pci_get_dev_class(struct ddekit_pci_dev
*dev
)
398 /****************************************************************************/
399 /* ddekit_pci_get_irq */
400 /****************************************************************************/
402 ddekit_pci_get_irq(struct ddekit_pci_dev
*dev
)
408 /****************************************************************************/
409 /* ddekit_pci_get_name */
410 /****************************************************************************/
411 char *ddekit_pci_get_name(struct ddekit_pci_dev
*dev
)
417 /****************************************************************************/
418 /* ddekit_pci_get_slot_name */
419 /****************************************************************************/
420 char *ddekit_pci_get_slot_name(struct ddekit_pci_dev
*dev
)
426 /****************************************************************************/
427 /* ddekit_pci_get_resource */
428 /****************************************************************************/
430 ddekit_pci_get_resource(struct ddekit_pci_dev
*dev
, unsigned int idx
)
436 /****************************************************************************/
437 /* ddekit_pci_irq_enable */
438 /****************************************************************************/
439 int ddekit_pci_irq_enable
440 (int bus
, int slot
, int func
, int pin
, int *irq
)
442 /* call not needed */