Merge branch 'master' of git://localhost/acess2
[tpg/acess2.git] / KernelLand / Modules / USB / UHCI / uhci.h
1 /*
2  * AcessOS Version 1
3  * USB Stack
4  * - Universal Host Controller Interface
5  */
6 #ifndef _UHCI_H_
7 #define _UHCI_H_
8
9 // === TYPES ===
10 typedef struct sUHCI_Controller tUHCI_Controller;
11 typedef struct sUHCI_EndpointInfo       tUHCI_EndpointInfo;
12 typedef struct sUHCI_ExtraTDInfo        tUHCI_ExtraTDInfo;
13
14 typedef struct sUHCI_TD tUHCI_TD;
15 typedef struct sUHCI_QH tUHCI_QH;
16
17 // === STRUCTURES ===
18 struct sUHCI_ExtraTDInfo
19 {
20          int    Offset;
21         tPAddr  FirstPage;
22         tPAddr  SecondPage;
23         
24         tUSBHostCb      Callback;
25         void    *CallbackPtr;
26 };
27
28 struct sUHCI_EndpointInfo
29 {
30         unsigned MaxPacketSize : 12;
31         unsigned Type : 3;
32         unsigned Tgl : 1;
33 };
34
35 #define TD_CTL_IOC      (1 << 24)
36 #define TD_CTL_ACTIVE   (1 << 23)
37 #define TD_CTL_STALLED  (1 << 22)
38 #define TD_CTL_DATABUFERR       (1 << 21)
39 #define TD_CTL_BABBLE   (1 << 20)
40 #define TD_CTL_NAK      (1 << 19)
41 #define TD_CTL_CRCERR   (1 << 18)
42 #define TD_CTL_BITSTUFF (1 << 17)
43 #define TD_CTL_RESERVED (1 << 16)
44
45 struct sUHCI_TD
46 {
47         /**
48          * \brief Next Entry in list
49          * 
50          * 31:4 - Address
51          * 3 - Reserved
52          * 2 - Depth/Breadth Select
53          * 1 - QH/TD Select
54          * 0 - Terminate (Last in List)
55          */
56         Uint32  Link;
57         
58         /**
59          * \brief Control and Status Field
60          * 
61          * 31:30 - Reserved
62          * 29 - Short Packet Detect (Input Only)
63          * 28:27 - Number of Errors Allowed
64          * 26 - Low Speed Device (Communicating with a low speed device)
65          * 25 - Isynchonious Select
66          * 24 - Interrupt on Completion (IOC)
67          * 23:16 - Status
68          *     23 - Active
69          *     22 - Stalled
70          *     21 - Data Buffer Error
71          *     20 - Babble Detected
72          *     19 - NAK Detected
73          *     18 - CRC/Timout Error
74          *     17 - Bitstuff Error
75          *     16 - Reserved
76          * 15:11 - Reserved
77          * 10:0 - Actual Length (Number of bytes transfered)
78          */
79         Uint32  Control;
80         
81         /**
82          * \brief Packet Header
83          * 
84          * 31:21 - Maximum Length (0=1, Max 0x4FF, 0x7FF=0)
85          * 20 - Reserved
86          * 19 - Data Toggle
87          * 18:15 - Endpoint
88          * 14:8 - Device Address
89          * 7:0 - PID (Packet Identifcation) - Only 96, E1, 2D allowed
90          *
91          * 0x96 = Data IN
92          * 0xE1 = Data Out
93          * 0x2D = Setup
94          */
95         Uint32  Token;
96         
97         /**
98          * \brief Pointer to the data to send
99          */
100         Uint32  BufferPointer;
101
102         struct
103         {
104                 tUHCI_ExtraTDInfo       *ExtraInfo;
105                 char    bActive;        // Allocated
106                 Uint8   QueueIndex;     // QH, 0-127 are interrupt, 128 undef, 129 Control, 130 Bulk
107                 char    bFreePointer;   // Free \a BufferPointer once done
108         } _info;
109 } __attribute__((aligned(16)));
110
111 struct sUHCI_QH
112 {
113         /**
114          * \brief Next Entry in list
115          * 
116          * 31:4 - Address
117          * 3:2 - Reserved
118          * 1 - QH/TD Select
119          * 0 - Terminate (Last in List)
120          */
121         Uint32  Next;
122
123         /**
124          * \brief Next Entry in list
125          * 
126          * 31:4 - Address
127          * 3:2 - Reserved
128          * 1 - QH/TD Select
129          * 0 - Terminate (Last in List)
130          */
131         Uint32  Child;
132         
133         /*
134          * \note Area for software use
135          * \brief Last TD in this list, used to add things to the end
136          */
137         tUHCI_TD        *_LastItem;
138 } __attribute__((aligned(16)));
139
140 struct sUHCI_Controller
141 {
142         /**
143          * \brief PCI Device ID
144          */
145         Uint16  PciId;
146         
147         /**
148          * \brief IO Base Address
149          */
150         Uint16  IOBase;
151         
152         /**
153          * \brief Memory Mapped-IO base address
154          */
155         Uint16  *MemIOMap;
156
157         /**
158          * \brief IRQ Number assigned to the device
159          */
160          int    IRQNum;
161
162         /**
163          * \brief Number of the last frame to be cleaned
164          */
165          int    LastCleanedFrame;
166         
167         /**
168          * \brief Frame list
169          * 
170          * 31:4 - Frame Pointer
171          * 3:2 - Reserved
172          * 1 - QH/TD Selector
173          * 0 - Terminate (Empty Pointer)
174          */
175         Uint32  *FrameList;
176         
177         /**
178          * \brief Physical Address of the Frame List
179          */
180         tPAddr  PhysFrameList;
181
182         tUSBHub *RootHub;
183
184         /**
185          * \brief Load in bytes on each interrupt queue
186          */
187          int    InterruptLoad[128];
188
189         tPAddr          PhysTDQHPage;
190         struct
191         {
192                 // 127 Interrupt Queue Heads
193                 // - 4ms -> 256ms range of periods
194                 tUHCI_QH        InterruptQHs[0];
195                 tUHCI_QH        InterruptQHs_256ms[64];
196                 tUHCI_QH        InterruptQHs_128ms[32];
197                 tUHCI_QH        InterruptQHs_64ms [16];
198                 tUHCI_QH        InterruptQHs_32ms [ 8];
199                 tUHCI_QH        InterruptQHs_16ms [ 4];
200                 tUHCI_QH        InterruptQHs_8ms  [ 2];
201                 tUHCI_QH        InterruptQHs_4ms  [ 1];
202                 tUHCI_QH        _padding;
203         
204                 tUHCI_QH        ControlQH;
205                 tUHCI_QH        BulkQH;
206                 
207                 tUHCI_TD        LocalTDPool[ (4096-(128+2)*sizeof(tUHCI_QH)) / sizeof(tUHCI_TD) ];
208         }       *TDQHPage;
209         
210         struct {
211                 tUHCI_EndpointInfo EndpointInfo[16];
212         } *DevInfo[256];
213 };
214
215 // === ENUMERATIONS ===
216 enum eUHCI_IOPorts {
217         /**
218          * \brief USB Command Register
219          * 
220          * 15:8 - Reserved
221          * 7 - Maximum Packet Size selector (1: 64 bytes, 0: 32 bytes)
222          * 6 - Configure Flag (No Hardware Effect)
223          * 5 - Software Debug (Don't think it will be needed)
224          * 4 - Force Global Resume
225          * 3 - Enter Global Suspend Mode
226          * 2 - Global Reset (Resets all devices on the bus)
227          * 1 - Host Controller Reset (Reset just the controller)
228          * 0 - Run/Stop
229          */
230         USBCMD  = 0x00,
231         /**
232          * \brief USB Status Register
233          * 
234          * 15:6 - Reserved
235          * 5 - HC Halted, set to 1 when USBCMD:RS is set to 0
236          * 4 - Host Controller Process Error (Errors related to the bus)
237          * 3 - Host System Error (Errors related to the OS/PCI Bus)
238          * 2 - Resume Detect (Set if a RESUME command is sent to the Controller)
239          * 1 - USB Error Interrupt
240          * 0 - USB Interrupts (Set if a transaction with the IOC bit set is completed)
241          */
242         USBSTS  = 0x02,
243         /**
244          * \brief USB Interrupt Enable Register
245          * 
246          * 15:4 - Reserved
247          * 3 - Short Packet Interrupt Enable
248          * 2 - Interrupt on Complete (IOC) Enable
249          * 1 - Resume Interrupt Enable
250          * 0 - Timout / CRC Error Interrupt Enable
251          */
252         USBINTR = 0x04,
253         /**
254          * \brief Frame Number (Index into the Frame List)
255          * 
256          * 15:11 - Reserved
257          * 10:0 - Index (Incremented each approx 1ms)
258          */
259         FRNUM   = 0x06,
260         /**
261          * \brief Frame List Base Address
262          * 
263          * 31:12 - Pysical Address >> 12
264          * 11:0 - Reserved (Set to Zero)
265          */
266         FLBASEADD = 0x08,       // 32-bit
267         /**
268          * \brief Start-of-frame Modify Register
269          * \note 8-bits only
270          * 
271          * Sets the size of a frame
272          * Frequency = (11936+n)/12000 kHz
273          * 
274          * 7 - Reserved
275          * 6:0 -
276          */
277         SOFMOD = 0x0C,  // 8bit
278         /**
279          * \brief Port Status and Controll Register (Port 1)
280          * 
281          * 15:13 - Reserved
282          * 12 - Suspend
283          * 11:10 - Reserved
284          * 9 - Port Reset
285          * 8 - Low Speed Device Attached
286          * 5:4 - Line Status
287          * 3 - Port Enable/Disable Change - Used for detecting device removal
288          * 2 - Port Enable/Disable
289          * 1 - Connect Status Change
290          * 0 - Current Connect Status
291          */
292         PORTSC1 = 0x10,
293         /**
294          * \brief Port Status and Controll Register (Port 2)
295          * 
296          * See ::PORTSC1
297          */
298         PORTSC2 = 0x12
299 };
300
301 #endif

UCC git Repository :: git.ucc.asn.au