blob: 7578e6f4a89385ba0a347baf1fa269aa1c59ad01 [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
Stefan Reinauer18588442005-01-17 11:08:08 +00005#define LIBPCI_CHECK_VERSION(major,minor,micro) \
6 ( (LIBPCI_MAJOR_VERSION > (major)) || \
7 (LIBPCI_MAJOR_VERSION == (major) && LIBPCI_MINOR_VERSION > (minor)) || \
8 (LIBPCI_MAJOR_VERSION == (major) && LIBPCI_MINOR_VERSION == (minor)) && \
9 LIBPCI_MICRO_VERSION >= (micro) )
10
Li-Ta Lo81521262004-07-08 17:18:27 +000011#define PCITAG struct pci_filter *
12
13#define DEBUG_PCI 1
14
15struct pci_access *pacc;
16struct pci_dev *dev;
17
18struct pci_filter ltag;
19
20
21int pciNumBuses = 0;
22
23int pciInit(void)
24{
25 pacc = pci_alloc();
26
27 pci_init(pacc);
28 pci_scan_bus(pacc);
29 for (dev = pacc->devices; dev; dev = dev->next) {
30 pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES);
31 }
32 return 0;
33}
34
35int pciExit(void)
36{
37 pci_cleanup(pacc);
38 return 0;
39}
40
41PCITAG findPci(unsigned short bx)
42{
43 PCITAG tag = &ltag;
44
45 int bus = (bx >> 8) & 0xFF;
46 int slot = (bx >> 3) & 0x1F;
47 int func = bx & 0x7;
48
49 tag->bus = bus;
50 tag->slot = slot;
51 tag->func = func;
52
Stefan Reinauer18588442005-01-17 11:08:08 +000053#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +000054 if (pci_get_dev(pacc, 0, bus, slot, func))
Stefan Reinauer18588442005-01-17 11:08:08 +000055#else
56 if (pci_get_dev(pacc, bus, slot, func))
57#endif
Li-Ta Lo81521262004-07-08 17:18:27 +000058 return tag;
59
60 return NULL;
61}
62
63u32 pciSlotBX(PCITAG tag)
64{
65 return (tag->bus << 8) | (tag->slot << 3) | (tag->func);
66}
67
68u8 pciReadByte(PCITAG tag, u32 idx)
69{
70 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +000071#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +000072 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +000073#else
74 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
75#endif
Li-Ta Lo81521262004-07-08 17:18:27 +000076 return pci_read_byte(d, idx);
77#ifdef DEBUG_PCI
78 printf("PCI: device not found while read byte (%x:%x.%x)\n",
79 tag->bus, tag->slot, tag->func);
80#endif
81 return 0;
82}
83
84u16 pciReadWord(PCITAG tag, u32 idx)
85{
86 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +000087#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +000088 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +000089#else
90 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
91#endif
Li-Ta Lo81521262004-07-08 17:18:27 +000092 return pci_read_word(d, idx);
93#ifdef DEBUG_PCI
94 printf("PCI: device not found while read word (%x:%x.%x)\n",
95 tag->bus, tag->slot, tag->func);
96#endif
97 return 0;
98}
99
100u32 pciReadLong(PCITAG tag, u32 idx)
101{
102 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +0000103#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000104 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +0000105#else
106 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
107#endif
Li-Ta Lo81521262004-07-08 17:18:27 +0000108 return pci_read_long(d, idx);
109#ifdef DEBUG_PCI
110 printf("PCI: device not found while read long (%x:%x.%x)\n",
111 tag->bus, tag->slot, tag->func);
112#endif
113 return 0;
114}
115
116
117void pciWriteLong(PCITAG tag, u32 idx, u32 data)
118{
119 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +0000120#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000121 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +0000122#else
123 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
124#endif
Li-Ta Lo81521262004-07-08 17:18:27 +0000125 pci_write_long(d, idx, data);
126#ifdef DEBUG_PCI
127 else
128 printf("PCI: device not found while write long (%x:%x.%x)\n",
129 tag->bus, tag->slot, tag->func);
130#endif
131}
132
133void pciWriteWord(PCITAG tag, u32 idx, u16 data)
134{
135 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +0000136#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000137 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +0000138#else
139 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
140#endif
Li-Ta Lo81521262004-07-08 17:18:27 +0000141 pci_write_word(d, idx, data);
142#ifdef DEBUG_PCI
143 else
144 printf("PCI: device not found while write word (%x:%x.%x)\n",
145 tag->bus, tag->slot, tag->func);
146#endif
147
148}
149
150void pciWriteByte(PCITAG tag, u32 idx, u8 data)
151{
152 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +0000153#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000154 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +0000155#else
156 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
157#endif
Li-Ta Lo81521262004-07-08 17:18:27 +0000158 pci_write_long(d, idx, data);
159#ifdef DEBUG_PCI
160 else
161 printf("PCI: device not found while write long (%x:%x.%x)\n",
162 tag->bus, tag->slot, tag->func);
163#endif
164}