root/Whitix/trunk/include/pci.h

Revision 2008, 4.4 KB (checked in by mwhitworth, 3 years ago)

Rework PCI code to use generic resource layer, add defines and more prototypes, add module parameter to driver registration function.

Line 
1/*  This file is part of Whitix.
2 *
3 *  Whitix is free software; you can redistribute it and/or modify
4 *  it under the terms of the GNU General Public License as published by
5 *  the Free Software Foundation; either version 2 of the License, or
6 *  (at your option) any later version.
7 *
8 *  Whitix is distributed in the hope that it will be useful,
9 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *  GNU General Public License for more details.
12 *
13 *  You should have received a copy of the GNU General Public License
14 *  along with Whitix; if not, write to the Free Software
15 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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
28struct PciBus
29{
30        int index;
31        struct ListHead next;
32        struct ListHead deviceList;
33        struct KeBus bus;
34};
35
36struct PciDriver;
37
38struct PciDevice
39{
40        /* PCI configuration information; cached. */
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        /* PCI layer data. */
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/* PCI BIOS structure. */
62struct 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/* PCI configuration space registers */
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/* NUmber of I/O resource slots for each device. */
90#define PCI_NUM_RESOURCES       6
91
92#define PCI_RESOURCE_IO         0x01
93
94extern int (*PciRead)(int bus, int dev, int func, int reg, int bytes, DWORD* val);
95extern int (*PciWrite)(int bus, int dev, int func, int reg, DWORD v, int bytes);
96
97/* Internal functions */
98int PciDetectBios();
99
100/* Internal defines */
101#define PCI_VENDOR_INVALID(vendorId) (((vendorId) == 0xFFFF) || ((vendorId) == 0x0000))
102#define PCI_RES_BEGIN_MASK(addressReg) ((addressReg) & 0xFFFFFFF0)
103
104/*** LIBRARY CODE ***/
105
106/* PCI library structures. Used by drivers. */
107
108#define PCI_ID_ANY      (DWORD)(~0)     /* Used if the driver doesn't care about a certain ID field. */
109
110#define PciIdTableEnd() {0, 0, 0, 0, 0, 0, NULL}
111
112#define PCI_ID_DATA(id) ((id)->data)
113
114struct PciDeviceId
115{
116        DWORD vendorId, deviceId;
117        DWORD subVendor, subDevice;
118        DWORD class, classMask;
119        void* data;
120};
121
122/* PCI device classes. */
123#define PCI_BASE_CLASS_DISPLAY          0x03
124
125struct PciDriver
126{
127        char* name;
128        struct PciDeviceId* idTable;
129
130        /* Function pointers. */
131        int (*initOne)(struct PciDevice* device, struct PciDeviceId* devId);
132
133        /* Internal data */
134        struct Module* module;
135        struct ListHead next;
136        void* data;
137};
138
139/* PCI library functions. Used by drivers. */
140int PciEnableDevice(struct PciDevice* device);
141int PciDisableDevice(struct PciDevice* device);
142int PciSetMaster(struct PciDevice* device);
143int PciRegisterDriverMod(struct Module* module, struct PciDriver* pciDriver);
144#define PciRegisterDriver(driver) (PciRegisterDriverMod(THIS_MODULE, (driver)))
145
146int PciWriteConfigByte(struct PciDevice* device, int reg, BYTE value);
147int PciReadConfigByte(struct PciDevice* device, int reg, BYTE* value);
148int PciReadConfigWord(struct PciDevice* device, int reg, WORD* value);
149int PciWriteConfigWord(struct PciDevice* device, int reg, WORD value);
150
151int PciReadConfigDword(struct PciDevice* device, int reg, DWORD* value);
152int PciWriteConfigDword(struct PciDevice* device, int reg, DWORD value);
153
154/* Inline library functions */
155static 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
163static 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
171static 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
Note: See TracBrowser for help on using the browser.