blob: 796933e5b03d1d2818923595b76fab52b848ffe8 [file] [log] [blame]
Li-Ta Lo81521262004-07-08 17:18:27 +00001#include <stdio.h>
Stefan Reinauer850e7d42015-09-28 13:12:04 -07002#include "pci-userspace.h"
Li-Ta Lo81521262004-07-08 17:18:27 +00003
Luc Verhaegene6e899d2009-05-27 11:39:16 +00004#ifdef PCI_LIB_VERSION
5#define LIBPCI_CHECK_VERSION(major,minor,micro) \
6 ((((major) << 16) | ((minor) << 8) | (micro)) <= PCI_LIB_VERSION)
7#else
Stefan Reinauer18588442005-01-17 11:08:08 +00008#define LIBPCI_CHECK_VERSION(major,minor,micro) \
9 ( (LIBPCI_MAJOR_VERSION > (major)) || \
10 (LIBPCI_MAJOR_VERSION == (major) && LIBPCI_MINOR_VERSION > (minor)) || \
11 (LIBPCI_MAJOR_VERSION == (major) && LIBPCI_MINOR_VERSION == (minor)) && \
12 LIBPCI_MICRO_VERSION >= (micro) )
Luc Verhaegene6e899d2009-05-27 11:39:16 +000013#endif
Stefan Reinauer18588442005-01-17 11:08:08 +000014
Li-Ta Lo81521262004-07-08 17:18:27 +000015#define PCITAG struct pci_filter *
16
17#define DEBUG_PCI 1
18
19struct pci_access *pacc;
20struct pci_dev *dev;
21
22struct pci_filter ltag;
23
24
25int pciNumBuses = 0;
26
27int pciInit(void)
28{
29 pacc = pci_alloc();
30
31 pci_init(pacc);
32 pci_scan_bus(pacc);
33 for (dev = pacc->devices; dev; dev = dev->next) {
34 pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES);
35 }
36 return 0;
37}
38
39int pciExit(void)
40{
41 pci_cleanup(pacc);
42 return 0;
43}
44
45PCITAG findPci(unsigned short bx)
46{
47 PCITAG tag = &ltag;
48
49 int bus = (bx >> 8) & 0xFF;
50 int slot = (bx >> 3) & 0x1F;
51 int func = bx & 0x7;
52
53 tag->bus = bus;
54 tag->slot = slot;
55 tag->func = func;
56
Stefan Reinauer18588442005-01-17 11:08:08 +000057#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +000058 if (pci_get_dev(pacc, 0, bus, slot, func))
Stefan Reinauer18588442005-01-17 11:08:08 +000059#else
60 if (pci_get_dev(pacc, bus, slot, func))
61#endif
Li-Ta Lo81521262004-07-08 17:18:27 +000062 return tag;
63
64 return NULL;
65}
66
67u32 pciSlotBX(PCITAG tag)
68{
69 return (tag->bus << 8) | (tag->slot << 3) | (tag->func);
70}
71
72u8 pciReadByte(PCITAG tag, u32 idx)
73{
74 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +000075#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +000076 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +000077#else
78 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
79#endif
Li-Ta Lo81521262004-07-08 17:18:27 +000080 return pci_read_byte(d, idx);
81#ifdef DEBUG_PCI
82 printf("PCI: device not found while read byte (%x:%x.%x)\n",
83 tag->bus, tag->slot, tag->func);
84#endif
85 return 0;
86}
87
88u16 pciReadWord(PCITAG tag, u32 idx)
89{
90 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +000091#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +000092 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +000093#else
94 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
95#endif
Li-Ta Lo81521262004-07-08 17:18:27 +000096 return pci_read_word(d, idx);
97#ifdef DEBUG_PCI
98 printf("PCI: device not found while read word (%x:%x.%x)\n",
99 tag->bus, tag->slot, tag->func);
100#endif
101 return 0;
102}
103
104u32 pciReadLong(PCITAG tag, u32 idx)
105{
106 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +0000107#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000108 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +0000109#else
110 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
111#endif
Li-Ta Lo81521262004-07-08 17:18:27 +0000112 return pci_read_long(d, idx);
113#ifdef DEBUG_PCI
114 printf("PCI: device not found while read long (%x:%x.%x)\n",
115 tag->bus, tag->slot, tag->func);
116#endif
117 return 0;
118}
119
120
121void pciWriteLong(PCITAG tag, u32 idx, u32 data)
122{
123 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +0000124#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000125 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +0000126#else
127 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
128#endif
Li-Ta Lo81521262004-07-08 17:18:27 +0000129 pci_write_long(d, idx, data);
130#ifdef DEBUG_PCI
131 else
132 printf("PCI: device not found while write long (%x:%x.%x)\n",
133 tag->bus, tag->slot, tag->func);
134#endif
135}
136
137void pciWriteWord(PCITAG tag, u32 idx, u16 data)
138{
139 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +0000140#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000141 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +0000142#else
143 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
144#endif
Li-Ta Lo81521262004-07-08 17:18:27 +0000145 pci_write_word(d, idx, data);
146#ifdef DEBUG_PCI
147 else
148 printf("PCI: device not found while write word (%x:%x.%x)\n",
149 tag->bus, tag->slot, tag->func);
150#endif
151
152}
153
154void pciWriteByte(PCITAG tag, u32 idx, u8 data)
155{
156 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +0000157#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000158 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +0000159#else
160 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
161#endif
Li-Ta Lo81521262004-07-08 17:18:27 +0000162 pci_write_long(d, idx, data);
163#ifdef DEBUG_PCI
164 else
165 printf("PCI: device not found while write long (%x:%x.%x)\n",
166 tag->bus, tag->slot, tag->func);
167#endif
168}