nvramtool: 64bit safe CBFS handling
[coreboot.git] / util / vgabios / pci-userspace.c
1 #include <stdio.h>
2 #include <pci/pci.h>
3 #include "pci.h"
4
5 #ifdef PCI_LIB_VERSION
6 #define LIBPCI_CHECK_VERSION(major,minor,micro) \
7     ((((major) << 16) | ((minor) << 8) | (micro)) <= PCI_LIB_VERSION)
8 #else
9 #define LIBPCI_CHECK_VERSION(major,minor,micro) \
10  ( (LIBPCI_MAJOR_VERSION > (major)) || \
11    (LIBPCI_MAJOR_VERSION == (major) && LIBPCI_MINOR_VERSION > (minor)) || \
12  (LIBPCI_MAJOR_VERSION == (major) && LIBPCI_MINOR_VERSION == (minor)) && \
13  LIBPCI_MICRO_VERSION >= (micro) )
14 #endif
15
16 #define PCITAG struct pci_filter *
17
18 #define DEBUG_PCI 1
19
20 struct pci_access *pacc;
21 struct pci_dev *dev;
22
23 struct pci_filter ltag;
24
25
26 int pciNumBuses = 0;
27
28 int pciInit(void)
29 {
30         pacc = pci_alloc();
31
32         pci_init(pacc);
33         pci_scan_bus(pacc);
34         for (dev = pacc->devices; dev; dev = dev->next) {
35                 pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES);
36         }
37         return 0;
38 }
39
40 int pciExit(void)
41 {
42         pci_cleanup(pacc);
43         return 0;
44 }
45
46 PCITAG findPci(unsigned short bx)
47 {
48         PCITAG tag = &ltag;
49
50         int bus = (bx >> 8) & 0xFF;
51         int slot = (bx >> 3) & 0x1F;
52         int func = bx & 0x7;
53
54         tag->bus = bus;
55         tag->slot = slot;
56         tag->func = func;
57
58 #if LIBPCI_CHECK_VERSION(2,1,99)
59         if (pci_get_dev(pacc, 0, bus, slot, func))
60 #else
61         if (pci_get_dev(pacc, bus, slot, func))
62 #endif
63                 return tag;
64
65         return NULL;
66 }
67
68 u32 pciSlotBX(PCITAG tag)
69 {
70         return (tag->bus << 8) | (tag->slot << 3) | (tag->func);
71 }
72
73 u8 pciReadByte(PCITAG tag, u32 idx)
74 {
75         struct pci_dev *d;
76 #if LIBPCI_CHECK_VERSION(2,1,99)
77         if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
78 #else
79         if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
80 #endif
81                 return pci_read_byte(d, idx);
82 #ifdef DEBUG_PCI
83         printf("PCI: device not found while read byte (%x:%x.%x)\n",
84                tag->bus, tag->slot, tag->func);
85 #endif
86         return 0;
87 }
88
89 u16 pciReadWord(PCITAG tag, u32 idx)
90 {
91         struct pci_dev *d;
92 #if LIBPCI_CHECK_VERSION(2,1,99)
93         if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
94 #else
95         if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
96 #endif
97                 return pci_read_word(d, idx);
98 #ifdef DEBUG_PCI
99         printf("PCI: device not found while read word (%x:%x.%x)\n",
100                tag->bus, tag->slot, tag->func);
101 #endif
102         return 0;
103 }
104
105 u32 pciReadLong(PCITAG tag, u32 idx)
106 {
107         struct pci_dev *d;
108 #if LIBPCI_CHECK_VERSION(2,1,99)
109         if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
110 #else
111         if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
112 #endif
113                 return pci_read_long(d, idx);
114 #ifdef DEBUG_PCI
115         printf("PCI: device not found while read long (%x:%x.%x)\n",
116                tag->bus, tag->slot, tag->func);
117 #endif
118         return 0;
119 }
120
121
122 void pciWriteLong(PCITAG tag, u32 idx, u32 data)
123 {
124         struct pci_dev *d;
125 #if LIBPCI_CHECK_VERSION(2,1,99)
126         if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
127 #else
128         if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
129 #endif
130                 pci_write_long(d, idx, data);
131 #ifdef DEBUG_PCI
132         else
133                 printf("PCI: device not found while write long (%x:%x.%x)\n",
134                        tag->bus, tag->slot, tag->func);
135 #endif
136 }
137
138 void pciWriteWord(PCITAG tag, u32 idx, u16 data)
139 {
140         struct pci_dev *d;
141 #if LIBPCI_CHECK_VERSION(2,1,99)
142         if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
143 #else
144         if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
145 #endif
146                 pci_write_word(d, idx, data);
147 #ifdef DEBUG_PCI
148         else
149                 printf("PCI: device not found while write word (%x:%x.%x)\n",
150                        tag->bus, tag->slot, tag->func);
151 #endif
152
153 }
154
155 void pciWriteByte(PCITAG tag, u32 idx, u8 data)
156 {
157         struct pci_dev *d;
158 #if LIBPCI_CHECK_VERSION(2,1,99)
159         if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
160 #else
161         if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
162 #endif
163                 pci_write_long(d, idx, data);
164 #ifdef DEBUG_PCI
165         else
166                 printf("PCI: device not found while write long (%x:%x.%x)\n",
167                        tag->bus, tag->slot, tag->func);
168 #endif
169 }