| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #ifndef PCI_H |
|---|
| 20 | #define PCI_H |
|---|
| 21 | |
|---|
| 22 | #include <devices/bus.h> |
|---|
| 23 | #include <console.h> |
|---|
| 24 | #include <module.h> |
|---|
| 25 | #include <typedefs.h> |
|---|
| 26 | #include <resource.h> |
|---|
| 27 | |
|---|
| 28 | struct PciBus |
|---|
| 29 | { |
|---|
| 30 | int index; |
|---|
| 31 | struct ListHead next; |
|---|
| 32 | struct ListHead deviceList; |
|---|
| 33 | struct KeBus bus; |
|---|
| 34 | }; |
|---|
| 35 | |
|---|
| 36 | struct PciDriver; |
|---|
| 37 | |
|---|
| 38 | struct PciDevice |
|---|
| 39 | { |
|---|
| 40 | |
|---|
| 41 | BYTE dev, func, headerType; |
|---|
| 42 | DWORD vendorId, deviceId; |
|---|
| 43 | int irq; |
|---|
| 44 | DWORD devClass; |
|---|
| 45 | WORD subVendor, subDevice; |
|---|
| 46 | |
|---|
| 47 | struct Resource spaces[6]; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | struct PciBus* bus; |
|---|
| 51 | struct PciDriver* driver; |
|---|
| 52 | struct KeBusEntry entry; |
|---|
| 53 | |
|---|
| 54 | void* driverData; |
|---|
| 55 | |
|---|
| 56 | struct ListHead next; |
|---|
| 57 | }; |
|---|
| 58 | |
|---|
| 59 | #define PciSetData(driver, data) ((driver)->driverData = (data)) |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | struct PciBios |
|---|
| 63 | { |
|---|
| 64 | DWORD magicNumber; |
|---|
| 65 | DWORD physEntry; |
|---|
| 66 | BYTE version; |
|---|
| 67 | BYTE length; |
|---|
| 68 | BYTE crc; |
|---|
| 69 | BYTE reserved[5]; |
|---|
| 70 | }PACKED; |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | #define PCI_VENDOR_ID 0x00 |
|---|
| 74 | #define PCI_DEVICE_ID 0x02 |
|---|
| 75 | #define PCI_COMMAND 0x04 |
|---|
| 76 | #define PCI_COMMAND_IO 0x01 |
|---|
| 77 | #define PCI_COMMAND_MEM 0x02 |
|---|
| 78 | #define PCI_COMMAND_MASTER 0x04 |
|---|
| 79 | #define PCI_SUBSYS_VENDOR_ID 0x2C |
|---|
| 80 | #define PCI_SUBSYS_ID 0x2E |
|---|
| 81 | #define PCI_INTERRUPT_PIN 0x3D |
|---|
| 82 | |
|---|
| 83 | #define PCI_PRIMARY_BUS 0x18 |
|---|
| 84 | #define PCI_SECONDARY_BUS 0x19 |
|---|
| 85 | |
|---|
| 86 | #define PCI_WRITE_TEST 0xFFFFFFFF |
|---|
| 87 | #define PCI_BAR_BASE 0x10 |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | #define PCI_NUM_RESOURCES 6 |
|---|
| 91 | |
|---|
| 92 | #define PCI_RESOURCE_IO 0x01 |
|---|
| 93 | |
|---|
| 94 | extern int (*PciRead)(int bus, int dev, int func, int reg, int bytes, DWORD* val); |
|---|
| 95 | extern int (*PciWrite)(int bus, int dev, int func, int reg, DWORD v, int bytes); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | int PciDetectBios(); |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | #define PCI_VENDOR_INVALID(vendorId) (((vendorId) == 0xFFFF) || ((vendorId) == 0x0000)) |
|---|
| 102 | #define PCI_RES_BEGIN_MASK(addressReg) ((addressReg) & 0xFFFFFFF0) |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | #define PCI_ID_ANY (DWORD)(~0) |
|---|
| 109 | |
|---|
| 110 | #define PciIdTableEnd() {0, 0, 0, 0, 0, 0, NULL} |
|---|
| 111 | |
|---|
| 112 | #define PCI_ID_DATA(id) ((id)->data) |
|---|
| 113 | |
|---|
| 114 | struct PciDeviceId |
|---|
| 115 | { |
|---|
| 116 | DWORD vendorId, deviceId; |
|---|
| 117 | DWORD subVendor, subDevice; |
|---|
| 118 | DWORD class, classMask; |
|---|
| 119 | void* data; |
|---|
| 120 | }; |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | #define PCI_BASE_CLASS_DISPLAY 0x03 |
|---|
| 124 | |
|---|
| 125 | struct PciDriver |
|---|
| 126 | { |
|---|
| 127 | char* name; |
|---|
| 128 | struct PciDeviceId* idTable; |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | int (*initOne)(struct PciDevice* device, struct PciDeviceId* devId); |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | struct Module* module; |
|---|
| 135 | struct ListHead next; |
|---|
| 136 | void* data; |
|---|
| 137 | }; |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | int PciEnableDevice(struct PciDevice* device); |
|---|
| 141 | int PciDisableDevice(struct PciDevice* device); |
|---|
| 142 | int PciSetMaster(struct PciDevice* device); |
|---|
| 143 | int PciRegisterDriverMod(struct Module* module, struct PciDriver* pciDriver); |
|---|
| 144 | #define PciRegisterDriver(driver) (PciRegisterDriverMod(THIS_MODULE, (driver))) |
|---|
| 145 | |
|---|
| 146 | int PciWriteConfigByte(struct PciDevice* device, int reg, BYTE value); |
|---|
| 147 | int PciReadConfigByte(struct PciDevice* device, int reg, BYTE* value); |
|---|
| 148 | int PciReadConfigWord(struct PciDevice* device, int reg, WORD* value); |
|---|
| 149 | int PciWriteConfigWord(struct PciDevice* device, int reg, WORD value); |
|---|
| 150 | |
|---|
| 151 | int PciReadConfigDword(struct PciDevice* device, int reg, DWORD* value); |
|---|
| 152 | int PciWriteConfigDword(struct PciDevice* device, int reg, DWORD value); |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | static inline unsigned long PciResourceStart(struct PciDevice* device, int index) |
|---|
| 156 | { |
|---|
| 157 | if (index < 0 || index >= 6) |
|---|
| 158 | return 0; |
|---|
| 159 | |
|---|
| 160 | return device->spaces[index].start; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | static inline unsigned long PciResourceLen(struct PciDevice* device, int index) |
|---|
| 164 | { |
|---|
| 165 | if (index < 0 || index >= 6) |
|---|
| 166 | return 0; |
|---|
| 167 | |
|---|
| 168 | return device->spaces[index].len; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | static inline unsigned long PciResourceFlags(struct PciDevice* device, int index) |
|---|
| 172 | { |
|---|
| 173 | if (index < 0 || index >= 6) |
|---|
| 174 | return 0; |
|---|
| 175 | |
|---|
| 176 | return device->spaces[index].flags; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | #endif |
|---|