Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <pci/pci.h> |
| 3 | #include "pci.h" |
| 4 | |
Luc Verhaegen | e6e899d | 2009-05-27 11:39:16 +0000 | [diff] [blame^] | 5 | #ifdef PCI_LIB_VERSION |
| 6 | #define LIBPCI_CHECK_VERSION(major,minor,micro) \ |
| 7 | ((((major) << 16) | ((minor) << 8) | (micro)) <= PCI_LIB_VERSION) |
| 8 | #else |
Stefan Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 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) ) |
Luc Verhaegen | e6e899d | 2009-05-27 11:39:16 +0000 | [diff] [blame^] | 14 | #endif |
Stefan Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 15 | |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 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 = <ag; |
| 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 Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 58 | #if LIBPCI_CHECK_VERSION(2,1,99) |
Li-Ta Lo | 8b0356c | 2005-01-11 03:18:39 +0000 | [diff] [blame] | 59 | if (pci_get_dev(pacc, 0, bus, slot, func)) |
Stefan Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 60 | #else |
| 61 | if (pci_get_dev(pacc, bus, slot, func)) |
| 62 | #endif |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 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; |
Stefan Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 76 | #if LIBPCI_CHECK_VERSION(2,1,99) |
Li-Ta Lo | 8b0356c | 2005-01-11 03:18:39 +0000 | [diff] [blame] | 77 | if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func))) |
Stefan Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 78 | #else |
| 79 | if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func))) |
| 80 | #endif |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 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; |
Stefan Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 92 | #if LIBPCI_CHECK_VERSION(2,1,99) |
Li-Ta Lo | 8b0356c | 2005-01-11 03:18:39 +0000 | [diff] [blame] | 93 | if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func))) |
Stefan Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 94 | #else |
| 95 | if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func))) |
| 96 | #endif |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 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; |
Stefan Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 108 | #if LIBPCI_CHECK_VERSION(2,1,99) |
Li-Ta Lo | 8b0356c | 2005-01-11 03:18:39 +0000 | [diff] [blame] | 109 | if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func))) |
Stefan Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 110 | #else |
| 111 | if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func))) |
| 112 | #endif |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 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; |
Stefan Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 125 | #if LIBPCI_CHECK_VERSION(2,1,99) |
Li-Ta Lo | 8b0356c | 2005-01-11 03:18:39 +0000 | [diff] [blame] | 126 | if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func))) |
Stefan Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 127 | #else |
| 128 | if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func))) |
| 129 | #endif |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 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; |
Stefan Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 141 | #if LIBPCI_CHECK_VERSION(2,1,99) |
Li-Ta Lo | 8b0356c | 2005-01-11 03:18:39 +0000 | [diff] [blame] | 142 | if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func))) |
Stefan Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 143 | #else |
| 144 | if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func))) |
| 145 | #endif |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 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; |
Stefan Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 158 | #if LIBPCI_CHECK_VERSION(2,1,99) |
Li-Ta Lo | 8b0356c | 2005-01-11 03:18:39 +0000 | [diff] [blame] | 159 | if ((d = pci_get_dev(pacc, 0, tag->bus, tag->slot, tag->func))) |
Stefan Reinauer | 1858844 | 2005-01-17 11:08:08 +0000 | [diff] [blame] | 160 | #else |
| 161 | if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func))) |
| 162 | #endif |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 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 | } |