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 | |
| 5 | #define PCITAG struct pci_filter * |
| 6 | |
| 7 | #define DEBUG_PCI 1 |
| 8 | |
| 9 | struct pci_access *pacc; |
| 10 | struct pci_dev *dev; |
| 11 | |
| 12 | struct pci_filter ltag; |
| 13 | |
| 14 | |
| 15 | int pciNumBuses = 0; |
| 16 | |
| 17 | int pciInit(void) |
| 18 | { |
| 19 | pacc = pci_alloc(); |
| 20 | |
| 21 | pci_init(pacc); |
| 22 | pci_scan_bus(pacc); |
| 23 | for (dev = pacc->devices; dev; dev = dev->next) { |
| 24 | pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES); |
| 25 | } |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | int pciExit(void) |
| 30 | { |
| 31 | pci_cleanup(pacc); |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | PCITAG findPci(unsigned short bx) |
| 36 | { |
| 37 | PCITAG tag = <ag; |
| 38 | |
| 39 | int bus = (bx >> 8) & 0xFF; |
| 40 | int slot = (bx >> 3) & 0x1F; |
| 41 | int func = bx & 0x7; |
| 42 | |
| 43 | tag->bus = bus; |
| 44 | tag->slot = slot; |
| 45 | tag->func = func; |
| 46 | |
| 47 | if (pci_get_dev(pacc, bus, slot, func)) |
| 48 | return tag; |
| 49 | |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | u32 pciSlotBX(PCITAG tag) |
| 54 | { |
| 55 | return (tag->bus << 8) | (tag->slot << 3) | (tag->func); |
| 56 | } |
| 57 | |
| 58 | u8 pciReadByte(PCITAG tag, u32 idx) |
| 59 | { |
| 60 | struct pci_dev *d; |
| 61 | if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func))) |
| 62 | return pci_read_byte(d, idx); |
| 63 | #ifdef DEBUG_PCI |
| 64 | printf("PCI: device not found while read byte (%x:%x.%x)\n", |
| 65 | tag->bus, tag->slot, tag->func); |
| 66 | #endif |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | u16 pciReadWord(PCITAG tag, u32 idx) |
| 71 | { |
| 72 | struct pci_dev *d; |
| 73 | if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func))) |
| 74 | return pci_read_word(d, idx); |
| 75 | #ifdef DEBUG_PCI |
| 76 | printf("PCI: device not found while read word (%x:%x.%x)\n", |
| 77 | tag->bus, tag->slot, tag->func); |
| 78 | #endif |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | u32 pciReadLong(PCITAG tag, u32 idx) |
| 83 | { |
| 84 | struct pci_dev *d; |
| 85 | if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func))) |
| 86 | return pci_read_long(d, idx); |
| 87 | #ifdef DEBUG_PCI |
| 88 | printf("PCI: device not found while read long (%x:%x.%x)\n", |
| 89 | tag->bus, tag->slot, tag->func); |
| 90 | #endif |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void pciWriteLong(PCITAG tag, u32 idx, u32 data) |
| 96 | { |
| 97 | struct pci_dev *d; |
| 98 | if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func))) |
| 99 | pci_write_long(d, idx, data); |
| 100 | #ifdef DEBUG_PCI |
| 101 | else |
| 102 | printf("PCI: device not found while write long (%x:%x.%x)\n", |
| 103 | tag->bus, tag->slot, tag->func); |
| 104 | #endif |
| 105 | } |
| 106 | |
| 107 | void pciWriteWord(PCITAG tag, u32 idx, u16 data) |
| 108 | { |
| 109 | struct pci_dev *d; |
| 110 | if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func))) |
| 111 | pci_write_word(d, idx, data); |
| 112 | #ifdef DEBUG_PCI |
| 113 | else |
| 114 | printf("PCI: device not found while write word (%x:%x.%x)\n", |
| 115 | tag->bus, tag->slot, tag->func); |
| 116 | #endif |
| 117 | |
| 118 | } |
| 119 | |
| 120 | void pciWriteByte(PCITAG tag, u32 idx, u8 data) |
| 121 | { |
| 122 | struct pci_dev *d; |
| 123 | if ((d = pci_get_dev(pacc, tag->bus, tag->slot, tag->func))) |
| 124 | pci_write_long(d, idx, data); |
| 125 | #ifdef DEBUG_PCI |
| 126 | else |
| 127 | printf("PCI: device not found while write long (%x:%x.%x)\n", |
| 128 | tag->bus, tag->slot, tag->func); |
| 129 | #endif |
| 130 | } |