blob: bc71a6159459594e6381679cda285254e53665ca [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
Luc Verhaegene6e899d2009-05-27 11:39:16 +00005#ifdef PCI_LIB_VERSION
6#define LIBPCI_CHECK_VERSION(major,minor,micro) \
7 ((((major) << 16) | ((minor) << 8) | (micro)) <= PCI_LIB_VERSION)
8#else
Stefan Reinauer18588442005-01-17 11:08:08 +00009#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) )
Luc Verhaegene6e899d2009-05-27 11:39:16 +000014#endif
Stefan Reinauer18588442005-01-17 11:08:08 +000015
Li-Ta Lo81521262004-07-08 17:18:27 +000016#define PCITAG struct pci_filter *
17
18#define DEBUG_PCI 1
19
20struct pci_access *pacc;
21struct pci_dev *dev;
22
23struct pci_filter ltag;
24
25
26int pciNumBuses = 0;
27
28int 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
40int pciExit(void)
41{
42 pci_cleanup(pacc);
43 return 0;
44}
45
46PCITAG 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
Stefan Reinauer18588442005-01-17 11:08:08 +000058#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +000059 if (pci_get_dev(pacc, 0, bus, slot, func))
Stefan Reinauer18588442005-01-17 11:08:08 +000060#else
61 if (pci_get_dev(pacc, bus, slot, func))
62#endif
Li-Ta Lo81521262004-07-08 17:18:27 +000063 return tag;
64
65 return NULL;
66}
67
68u32 pciSlotBX(PCITAG tag)
69{
70 return (tag->bus << 8) | (tag->slot << 3) | (tag->func);
71}
72
73u8 pciReadByte(PCITAG tag, u32 idx)
74{
75 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +000076#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +000077 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +000078#else
79 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
80#endif
Li-Ta Lo81521262004-07-08 17:18:27 +000081 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
89u16 pciReadWord(PCITAG tag, u32 idx)
90{
91 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +000092#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +000093 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +000094#else
95 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
96#endif
Li-Ta Lo81521262004-07-08 17:18:27 +000097 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
105u32 pciReadLong(PCITAG tag, u32 idx)
106{
107 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +0000108#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000109 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +0000110#else
111 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
112#endif
Li-Ta Lo81521262004-07-08 17:18:27 +0000113 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
122void pciWriteLong(PCITAG tag, u32 idx, u32 data)
123{
124 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +0000125#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000126 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +0000127#else
128 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
129#endif
Li-Ta Lo81521262004-07-08 17:18:27 +0000130 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
138void pciWriteWord(PCITAG tag, u32 idx, u16 data)
139{
140 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +0000141#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000142 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +0000143#else
144 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
145#endif
Li-Ta Lo81521262004-07-08 17:18:27 +0000146 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
155void pciWriteByte(PCITAG tag, u32 idx, u8 data)
156{
157 struct pci_dev *d;
Stefan Reinauer18588442005-01-17 11:08:08 +0000158#if LIBPCI_CHECK_VERSION(2,1,99)
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000159 if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func)))
Stefan Reinauer18588442005-01-17 11:08:08 +0000160#else
161 if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func)))
162#endif
Li-Ta Lo81521262004-07-08 17:18:27 +0000163 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}