blob: f09c167cecb6b57022395190bfb5ffd2ed602a1f [file] [log] [blame]
Li-Ta Lo81521262004-07-08 17:18:27 +00001#include <stdio.h>
2#include <pci/pci.h>
3#include "pci.h"
4
5#define PCITAG struct pci_filter *
6
7#define DEBUG_PCI 1
8
9struct pci_access *pacc;
10struct pci_dev *dev;
11
12struct pci_filter ltag;
13
14
15int pciNumBuses = 0;
16
17int pciInit(void)
18{
19 pacc = pci_alloc();
20
21 pci_init(pacc);
22 pci_scan_bus(pacc);
23 for (dev = pacc->devices; dev; dev = dev->next) {
24 pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES);
25 }
26 return 0;
27}
28
29int pciExit(void)
30{
31 pci_cleanup(pacc);
32 return 0;
33}
34
35PCITAG findPci(unsigned short bx)
36{
37 PCITAG tag = &ltag;
38
39 int bus = (bx >> 8) & 0xFF;
40 int slot = (bx >> 3) & 0x1F;
41 int func = bx & 0x7;
42
43 tag->bus = bus;
44 tag->slot = slot;
45 tag->func = func;
46
Li-Ta Lo8b0356c2005-01-11 03:18:39 +000047 if (pci_get_dev(pacc, 0, bus, slot, func))
Li-Ta Lo81521262004-07-08 17:18:27 +000048 return tag;
49
50 return NULL;
51}
52
53u32 pciSlotBX(PCITAG tag)
54{
55 return (tag->bus << 8) | (tag->slot << 3) | (tag->func);
56}
57
58u8 pciReadByte(PCITAG tag, u32 idx)
59{
60 struct pci_dev *d;
Li-Ta Lo8b0356c2005-01-11 03:18:39 +000061 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Li-Ta Lo81521262004-07-08 17:18:27 +000062 return pci_read_byte(d, idx);
63#ifdef DEBUG_PCI
64 printf("PCI: device not found while read byte (%x:%x.%x)\n",
65 tag->bus, tag->slot, tag->func);
66#endif
67 return 0;
68}
69
70u16 pciReadWord(PCITAG tag, u32 idx)
71{
72 struct pci_dev *d;
Li-Ta Lo8b0356c2005-01-11 03:18:39 +000073 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Li-Ta Lo81521262004-07-08 17:18:27 +000074 return pci_read_word(d, idx);
75#ifdef DEBUG_PCI
76 printf("PCI: device not found while read word (%x:%x.%x)\n",
77 tag->bus, tag->slot, tag->func);
78#endif
79 return 0;
80}
81
82u32 pciReadLong(PCITAG tag, u32 idx)
83{
84 struct pci_dev *d;
Li-Ta Lo8b0356c2005-01-11 03:18:39 +000085 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Li-Ta Lo81521262004-07-08 17:18:27 +000086 return pci_read_long(d, idx);
87#ifdef DEBUG_PCI
88 printf("PCI: device not found while read long (%x:%x.%x)\n",
89 tag->bus, tag->slot, tag->func);
90#endif
91 return 0;
92}
93
94
95void pciWriteLong(PCITAG tag, u32 idx, u32 data)
96{
97 struct pci_dev *d;
Li-Ta Lo8b0356c2005-01-11 03:18:39 +000098 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Li-Ta Lo81521262004-07-08 17:18:27 +000099 pci_write_long(d, idx, data);
100#ifdef DEBUG_PCI
101 else
102 printf("PCI: device not found while write long (%x:%x.%x)\n",
103 tag->bus, tag->slot, tag->func);
104#endif
105}
106
107void pciWriteWord(PCITAG tag, u32 idx, u16 data)
108{
109 struct pci_dev *d;
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000110 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Li-Ta Lo81521262004-07-08 17:18:27 +0000111 pci_write_word(d, idx, data);
112#ifdef DEBUG_PCI
113 else
114 printf("PCI: device not found while write word (%x:%x.%x)\n",
115 tag->bus, tag->slot, tag->func);
116#endif
117
118}
119
120void pciWriteByte(PCITAG tag, u32 idx, u8 data)
121{
122 struct pci_dev *d;
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000123 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Li-Ta Lo81521262004-07-08 17:18:27 +0000124 pci_write_long(d, idx, data);
125#ifdef DEBUG_PCI
126 else
127 printf("PCI: device not found while write long (%x:%x.%x)\n",
128 tag->bus, tag->slot, tag->func);
129#endif
130}