2 * Acess2 USB Stack - OHCI Driver
3 * - By John Hodge (thePowersGang)
6 * - Open Host Controller Interface driver
9 #define VERSION VER2(0,1)
17 #define MAX_CONTROLLERS 4
18 #define MAX_TD_PAGES 2
19 #define MAX_ENDPT_PAGES 2
20 #define MAX_PACKET_SIZE 0x400 // TODO: Check what should be used
23 int OHCI_Initialise(char **Arguments);
24 void OHCI_Cleanup(void);
26 void OHCI_InitialiseController(tOHCI_Controller *Controller);
28 void *OHCI_int_AddTD(tOHCI_Controller *Controller, int Dest, int DataTgl, int Type, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length);
29 void *OHCI_DataIN(void *Ptr, int Dest, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length);
30 void *OHCI_DataOUT(void *Ptr, int Dest, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length);
31 void *OHCI_SendSETUP(void *Ptr, int Dest, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length);
32 int OHCI_IsTransferComplete(void *Ptr, void *Handle);
34 void *OHCI_StartPoll(void *Ptr, int Dest, int MaxPeriod, tUSBHostCb Cb, void *CbData, void *DataBuf, size_t Length);
35 void OHCI_StopPoll(void *Ptr, void *Hdl);
36 void OHCI_CheckPortUpdate(void *Ptr);
37 void OHCI_InterruptHandler(int IRQ, void *Ptr);
39 tOHCI_Endpoint *OHCI_int_AllocateEndpt(tOHCI_Controller *Controller, int Dest);
40 tOHCI_Endpoint *OHCI_int_GetEndptFromPhys(tPAddr PhysAddr);
41 tOHCI_GeneralTD *OHCI_int_AllocateGTD(tOHCI_Controller *Controller);
42 tOHCI_GeneralTD *OHCI_int_GetGTDFromPhys(tPAddr PhysAddr);
45 MODULE_DEFINE(0, VERSION, USB_OHCI, OHCI_Initialise, OHCI_Cleanup, "USB_Core", NULL);
46 tUSBHostDef gOHCI_HostDef = {
47 .InterruptIN = OHCI_StartPoll,
48 .StopInterrupt = OHCI_StopPoll,
50 .SendIN = OHCI_DataIN,
51 .SendOUT = OHCI_DataOUT,
52 .SendSETUP = OHCI_SendSETUP,
53 .IsOpComplete = OHCI_IsTransferComplete,
56 .CheckPorts = OHCI_CheckPortUpdate
58 tOHCI_Controller gOHCI_Controllers[MAX_CONTROLLERS];
59 tOHCI_GeneralTD *gapOHCI_TDPool[MAX_TD_PAGES]; //!< Virtual pointers to TDs, each has an index in avail bits
60 const int ciTDs_per_page = PAGE_SIZE/sizeof(tOHCI_GeneralTD);
61 tOHCI_Endpoint *gapOHCI_EndpointPool[MAX_ENDPT_PAGES];
62 const int ciEndpoints_per_page = PAGE_SIZE/sizeof(tOHCI_Endpoint);
65 int OHCI_Initialise(char **Arguments)
70 while( (id = PCI_GetDeviceByClass(0x0C0310, 0xFFFFFF, id)) != -1 && card_num < MAX_CONTROLLERS )
72 tOHCI_Controller *ctrlr = &gOHCI_Controllers[card_num];
76 ctrlr->IRQNum = PCI_GetIRQ(id);
77 ctrlr->ControlSpacePhys = PCI_GetBAR(id, 0); // Offset 0x10
78 PCI_ConfigWrite( id, 4, 2, 0x0006 ); // Enable memory and bus master
80 if( ctrlr->ControlSpacePhys == 0 || ctrlr->ControlSpacePhys & 1 ) {
81 ctrlr->ControlSpacePhys = 0;
85 OHCI_InitialiseController( ctrlr );
91 Log_Warning("OHCI", "Too many controllers (> %i) were detected, ignoring the rest",
98 void OHCI_Cleanup(void)
101 // TODO: Cleanup for unload
103 for( i = 0; i < MAX_CONTROLLERS && gOHCI_Controllers[i].ControlSpacePhys; i ++ )
105 // TODO: Clean up resources used
109 void OHCI_InitialiseController(tOHCI_Controller *Controller)
111 tOHCI_Controller *cnt = Controller;
112 volatile struct sRegisters *iospace;
114 Log_Debug("OHCI", "Card #%i at 0x%X, IRQ %i",
115 cnt->ID, cnt->ControlSpacePhys, cnt->IRQNum);
117 // - Prepare mappings
118 cnt->ControlSpace = (void*)MM_MapHWPages(cnt->ControlSpacePhys, 1);
119 IRQ_AddHandler( cnt->IRQNum, OHCI_InterruptHandler, cnt);
120 iospace = cnt->ControlSpace;
122 Log_Debug("OHCI", "Card #%i version is 0x%x", cnt->ID, iospace->HcRevision);
124 // Check who had control
125 if( iospace->HcControl & (1 << 8) ) // InterruptRouting
127 LOG("USB was in the hands of SMM, asking for it back");
128 // SMM has control, ask for it back
129 // - Write '1' to OwnershipChangeRequest
130 // - Wait for InterruptRouting to clear
132 while( iospace->HcControl & (1 << 8) ) ;
135 else if( (iospace->HcControl & 0xC0) != 0x00 ) // UsbReset
137 LOG("USB was in the hands of the BIOS");
138 // BIOS had control, check for Operational
139 if( (iospace->HcControl & 0xC0) != 0x80 ) // UsbOperational
141 // - If not, set to resume
142 iospace->HcControl &= ~0xC0; // UsbResume
143 iospace->HcControl |= 0x40; // UsbResume
149 // Cold boot, wait a bit
150 // TODO: Wait for reset time
153 // Allocate HCCA area
154 cnt->HCCA = (void*)MM_AllocDMA(1, 32, &cnt->HCCAPhys);
156 Log_Error("OHCI", "Unable to allocate HCCA (1 page, 32-bit)");
159 // TODO: Check return value
160 // - HCCA is 256 bytes in length, but I like alignment
161 cnt->IntLists = (void*)( (tVAddr)cnt->HCCA + 512 );
162 LOG("HCCAPhys = %P, HCCA = %p", cnt->HCCAPhys, cnt->HCCA);
164 // --- Restart the controller ---
165 Uint32 fm_interval = iospace->HcFmInterval;
166 iospace->HcCommandStatus |= (1 << 0);
167 // - Wait 10 micro-seconds
169 iospace->HcFmInterval = fm_interval;
170 // (Now in UsbSuspend state)
173 // - Check that things are good?
175 // --- Initialise Virtual Queues ---
176 memset(cnt->IntLists, 0, sizeof(*cnt->IntLists));
177 // Set next pointers into a binary tree
179 tPAddr next_lvl = cnt->HCCAPhys + 512;
180 next_lvl += 16 * sizeof(tOHCI_Endpoint);
181 for( int i = 0; i < 16; i ++ )
182 cnt->IntLists->Period16[i].NextED = next_lvl + i/2 * sizeof(tOHCI_Endpoint);
183 next_lvl += 8 * sizeof(tOHCI_Endpoint);
184 for( int i = 0; i < 8; i ++ )
185 cnt->IntLists->Period8[i].NextED = next_lvl + i/2 * sizeof(tOHCI_Endpoint);
186 next_lvl += 4 * sizeof(tOHCI_Endpoint);
187 for( int i = 0; i < 4; i ++ )
188 cnt->IntLists->Period4[i].NextED = next_lvl + i/2 * sizeof(tOHCI_Endpoint);
189 next_lvl += 2 * sizeof(tOHCI_Endpoint);
190 for( int i = 0; i < 2; i ++ )
191 cnt->IntLists->Period2[i].NextED = next_lvl + sizeof(tOHCI_Endpoint);
192 next_lvl += 1 * sizeof(tOHCI_Endpoint);
193 cnt->IntLists->Period1[0].NextED = next_lvl;
195 // Set all endpoints to be skipped
196 for( int i = 0; i < 32; i ++ )
198 tOHCI_Endpoint *ep = &cnt->IntLists->Period16[i];
199 ep->Flags |= (1 << 14); // Skip
202 // --- Initialise HCCA ---
203 // - Interrupt table is set up to point to each
204 for( int i = 0; i < 32; i ++ )
206 static const int _balance[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
207 cnt->HCCA->HccaInterruptTable[i] = cnt->HCCAPhys + 512 + _balance[i&15] * sizeof(tOHCI_Endpoint);
209 cnt->HCCA->HccaFrameNumber = 0;
210 cnt->HCCA->HccaDoneHead = 0;
212 // --- Initialise Registers
213 iospace->HcControlHeadED = MM_GetPhysAddr( (tVAddr)&cnt->IntLists->StopED );
214 iospace->HcBulkHeadED = MM_GetPhysAddr( (tVAddr)&cnt->IntLists->StopED );
215 iospace->HcHCCA = cnt->HCCAPhys;
216 iospace->HcInterruptEnable = 0x7B; // 0111 1011 (RHSC, FNO, UE, RD, WDH, SO)
217 iospace->HcInterruptDisable = 0x4; // Disable SOF interrupts
218 iospace->HcControl = 0x3C; // All queues on
219 iospace->HcPeriodicStart = fm_interval / 10 * 9; // 90% of fm_interval
222 iospace->HcControl |= (2 << 6); // UsbOperational
224 LOG("HcRhDescriptorA = 0x%x", iospace->HcRhDescriptorA);
225 LOG("HcRhDescriptorB = 0x%x", iospace->HcRhDescriptorB);
227 // --- Tell USB core that this controller is avaliable
228 cnt->nPorts = iospace->HcRhDescriptorA & 0x7F;
229 if( cnt->nPorts > 15 ) {
231 Log_Warning("OHCI", "Controller reports %i ports, but spec only allows 15, capping", cnt->nPorts);
234 cnt->RootHub = USB_RegisterHost(&gOHCI_HostDef, cnt, cnt->nPorts);
237 // --- USB IO Functions ---
239 tOHCI_Controller *Controller, int Dest,
240 int DataTgl, int Type,
241 tUSBHostCb Cb, void *CbData,
242 void *Buf, size_t Length
250 if( Length > MAX_PACKET_SIZE )
253 ENTER("pController xDest iDataTgl iType pCb pCbData pBuf iLength",
254 Controller, Dest, DataTgl, Type, Cb, CbData, Buf, Length);
256 // ---- Check that the packet resides within memory the controller can access
257 if( MM_GetPhysAddr((tVAddr)Buf) >= (1ULL << 32) || MM_GetPhysAddr((tVAddr)Buf + Length -1) >= (1ULL << 32) )
259 // TODO: Handle destination outside of 32-bit address range
260 Log_Warning("OHCI", "_int_DoTD - Buffer outside of 32-bit range, TODO: Handle this");
265 // --- Find/Allocate Endpoint descriptor
266 ep = OHCI_int_AllocateEndpt(Controller, Dest);
268 Log_Warning("OHCI", "_int_DoTD - Unable to find/allocate Endpoint 0x%x... oops", Dest);
274 td = OHCI_int_AllocateGTD(Controller);
276 Log_Warning("OHCI", "_int_DoTD - Unable to allocate TD... oops");
280 td_phys = MM_GetPhysAddr( (tVAddr)td );
283 td->Flags |= (1 << 18); // Buffer rounding
284 td->Flags |= (Type & 3) << 19; // Type/Direction
285 td->Flags |= (2 | DataTgl) << 24; // Data Toggle
286 td->CBP = MM_GetPhysAddr( (tVAddr) Buf );
287 td->BE = MM_GetPhysAddr( (tVAddr)Buf + Length - 1 );
288 // - Save callback etc
289 td->CbPointer = (tVAddr)Cb;
290 td->CbArg = (tVAddr)CbData;
292 // --- Add to the end of the Endpoint's list
294 OHCI_int_GetGTDFromPhys( ep->TailP )->NextTD = td_phys;
302 void *OHCI_DataIN(void *Ptr, int Dest, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length)
304 return OHCI_int_DoTD(Ptr, Dest, DataTgl, 2, Cb, CbData, Buf, Length);
306 void *OHCI_DataOUT(void *Ptr, int Dest, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length)
308 return OHCI_int_DoTD(Ptr, Dest, DataTgl, 1, Cb, CbData, Buf, Length);
310 void *OHCI_SendSETUP(void *Ptr, int Dest, int DataTgl, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length)
312 return OHCI_int_DoTD(Ptr, Dest, DataTgl, 0, Cb, CbData, Buf, Length);
314 int OHCI_IsTransferComplete(void *Ptr, void *Handle)
316 tOHCI_GeneralTD *td = Handle;
318 ENTER("pPtr pHandle", Ptr, Handle);
327 Log_Warning("OHCI", "TODO: Implement cleanup in OHCI_IsTransferComplete");
333 // --- Interrupt polling ---
334 void *OHCI_StartPoll(void *Ptr, int Dest, int MaxPeriod, tUSBHostCb Cb, void *CbData, void *DataBuf, size_t Length)
338 if( MaxPeriod <= 0 ) return NULL;
340 // Allocate? (or obtain) an ED
343 for( slot = 32; slot > MaxPeriod && slot; slot >>= 1 );
351 // Add to period 1 list
354 // Determine best list by current load
365 Log_Error("OHCI", "OHCI_StartPoll - `slot` is invalid (%i)", slot);
369 Log_Warning("OHCI", "TODO: Implement OHCI_StartPoll");
373 void OHCI_StopPoll(void *Ptr, void *Hdl)
379 void OHCI_CheckPortUpdate(void *Ptr)
381 tOHCI_Controller *cnt = Ptr;
382 for( int i = 0; i < cnt->nPorts; i ++ )
384 volatile Uint32 *status_ptr = &cnt->ControlSpace->HcRhPortStatus[i];
385 Uint32 status = *status_ptr;
387 if( status & (1 << 16) )
389 LOG("Connect status change port %i, *status = 0x%x", i, status);
390 // Connect or disconnect?
391 if( status & (1 << 0) )
393 // Connect, set things up :)
398 *status_ptr = 1 << 4; // PRS
401 *status_ptr = 1 << 1; // SetPortEnable
402 LOG("Device connected on port %i", i);
405 USB_DeviceConnected(cnt->RootHub, i);
410 USB_DeviceDisconnected(cnt->RootHub, i);
414 // TODO: Handle other bits?
418 // --- Interrupt handler
419 void OHCI_InterruptHandler(int IRQ, void *Ptr)
421 tOHCI_Controller *cnt = Ptr;
422 // TODO: Interrupt handler
423 LOG("Interrupt on controller %i - Status = 0x%x",
424 cnt->ID, cnt->ControlSpace->HcInterruptStatus);
426 cnt->ControlSpace->HcInterruptStatus = cnt->ControlSpace->HcInterruptStatus;
429 // --- Internal Functions ---
430 tOHCI_Endpoint *OHCI_int_AllocateEndpt(tOHCI_Controller *Controller, int Dest)
432 tOHCI_Endpoint *first_free_ep = NULL;
433 tOHCI_Endpoint *first_freeable_ep = NULL;
437 // Convert 8.4 dev:endpt destination into 4.7 endpt:dev
438 _dest = (Dest >> 4) | ((Dest & 0xF) << 7);
441 for( pg = 0; pg < MAX_ENDPT_PAGES; pg ++ )
443 if( !gapOHCI_EndpointPool[pg] ) {
444 tPAddr paddr; // Not used
445 gapOHCI_EndpointPool[pg] = (void*)MM_AllocDMA(1, 32, &paddr);
446 memset(gapOHCI_EndpointPool[pg], 0, PAGE_SIZE);
449 for( i = 0; i < ciEndpoints_per_page; i ++ )
451 tOHCI_Endpoint *ep = &gapOHCI_EndpointPool[pg][i];
453 // Check if it's allocated
454 if( ep->Flags & (1 << 31) )
457 first_freeable_ep = ep;
459 if( ((ep->Flags >> 27) & 0xF) != Controller->ID )
461 if( (ep->Flags & 0x7FF) != _dest )
473 if( first_free_ep ) {
474 first_free_ep->Flags = (1 << 31);
477 first_free_ep->Flags |= Controller->ID << 27;
478 first_free_ep->Flags |= _dest;
479 first_free_ep->Flags |= 1023 << 16; // Max Packet Size
481 return first_free_ep;
484 if( first_freeable_ep ) {
486 first_free_ep->Flags = (1 << 31);
489 first_free_ep->Flags |= Controller->ID << 27;
490 first_free_ep->Flags |= _dest;
492 return first_free_ep;
494 Log_Warning("OHCI", "TODO: Implement freeing EPs when no avalable slots");
500 tOHCI_Endpoint *OHCI_int_GetEndptFromPhys(tPAddr PhysAddr)
503 for( i = 0; i < MAX_ENDPT_PAGES; i ++ )
506 addr = MM_GetPhysAddr( (tVAddr)gapOHCI_EndpointPool[i] );
507 if( PhysAddr >= addr && PhysAddr < addr + 0x1000 )
509 return gapOHCI_EndpointPool[i] + (PhysAddr - addr) / sizeof(tOHCI_Endpoint);
515 tOHCI_GeneralTD *OHCI_int_AllocateGTD(tOHCI_Controller *Controller)
519 for( pg = 0; pg < MAX_TD_PAGES; pg ++ )
521 if( !gapOHCI_TDPool[pg] ) {
522 tPAddr paddr; // Not used
523 gapOHCI_TDPool[pg] = (void*)MM_AllocDMA(1, 32, &paddr);
524 memset(gapOHCI_TDPool[pg], 0, PAGE_SIZE);
527 for( i = 0; i < ciTDs_per_page; i ++ )
529 // Check if allocated, and if not take it
530 if( gapOHCI_TDPool[pg][i].Flags & (1 << 31) )
532 gapOHCI_TDPool[pg][i].Flags = (1 << 31);
535 gapOHCI_TDPool[pg][i].Flags |= Controller->ID << 27;
537 return &gapOHCI_TDPool[pg][i];
543 tOHCI_GeneralTD *OHCI_int_GetGTDFromPhys(tPAddr PhysAddr)
546 for( i = 0; i < MAX_TD_PAGES; i ++ )
549 addr = MM_GetPhysAddr( (tVAddr)gapOHCI_TDPool[i] );
550 if( PhysAddr >= addr && PhysAddr < addr + 0x1000 )
552 return gapOHCI_TDPool[i] + (PhysAddr - addr) / sizeof(tOHCI_GeneralTD);