📄 otgdev.c
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
#include <windows.h>
#include <usb200.h>
#include <usbtypes.h>
#include <oal.h>
#include <bsp.h>
#include <s3c6410_syscon.h>
#include <usbdbgddsi.h>
#include <usbdbgutils.h>
#include "Otgdev.h"
//=====================================================================
// global varibles used in several functions
OTGDEV oOtgDev;
ENDPT_INFO EndPointsInfo[MAX_ENDPTS];
DWORD g_NumEPsUsed = 0;
DWORD EP_FIFO[MAX_ENDPTS];
DWORD DIEPCTL[MAX_ENDPTS];
DWORD DIEPINT[MAX_ENDPTS];
DWORD DIEPTSIZ[MAX_ENDPTS];
DWORD DIEPDMA[MAX_ENDPTS];
DWORD DOEPCTL[MAX_ENDPTS];
DWORD DOEPINT[MAX_ENDPTS];
DWORD DOEPTSIZ[MAX_ENDPTS];
DWORD DOEPDMA[MAX_ENDPTS];
//////////
// Function Name : OTGDevice_StallHelper
// Function Desctiption : This function stalls for a specified period of time
// Input : NONE
// Output : NONE
// Version :
void OTGDevice_StallHelper(int count)
{
volatile int j,i;
for(j = 0; j < count; j++)
for(i=0;i<1000;i++);
}
//////////
// Function Name : OTGDevice_InitOtg
// Function Desctiption : This function initializes OTG PHY and LINK.
// Input : NONE
// Output : NONE
// Version :
void OTGDevice_Init()
{
UINT8 ucMode;
volatile S3C6410_SYSCON_REG *g_pSysConReg;
USBDBGMSG(USBDBG_ZONE_FUNC, (L"usbpdd: +OTGDevice_Init\r\n"));
g_pSysConReg = (S3C6410_SYSCON_REG *)OALPAtoVA(S3C6410_BASE_REG_PA_SYSCON, FALSE);
// Enable Clock Source
g_pSysConReg->HCLK_GATE |= (1<<20); // HCLK_USB
#ifdef OTGDEVICE_HIGH_SPEED
USBDBGMSG(USBDBG_ZONE_VERBOSE, (L"usbpdd: Init: Running as High Speed device by default\r\n"));
oOtgDev.m_eSpeed = USB_HIGH;
oOtgDev.m_uControlEPMaxPktSize = HIGH_SPEED_CONTROL_PKT_SIZE;
oOtgDev.m_uBulkInEPMaxPktSize = HIGH_SPEED_BULK_PKT_SIZE;
oOtgDev.m_uBulkOutEPMaxPktSize = HIGH_SPEED_BULK_PKT_SIZE;
#else
USBDBGMSG(USBDBG_ZONE_VERBOSE, (L"usbpdd: Init: Running as Full Speed device by default\r\n"));
oOtgDev.m_eSpeed = USB_FULL;
oOtgDev.m_uControlEPMaxPktSize = FULL_SPEED_CONTROL_PKT_SIZE;
oOtgDev.m_uBulkInEPMaxPktSize = FULL_SPEED_BULK_PKT_SIZE;
oOtgDev.m_uBulkOutEPMaxPktSize = FULL_SPEED_BULK_PKT_SIZE;
#endif
oOtgDev.m_uIsUsbOtgSetConfiguration = 0;
OTGDevice_SetSoftDisconnect();
OTGDevice_SoftResetCore();
OTGDevice_InitCore();
OTGDevice_CheckCurrentMode(&ucMode);
if (ucMode == INT_DEV_MODE)
{
OTGDevice_StallHelper(2000);
OTGDevice_InitDevice();
}
else
{
USBDBGMSG(USBDBG_ZONE_FUNC, (L"usbpdd: Error! OTGDevice_Init: Current Mode is Host\r\n"));
return;
}
}
//////////
// Function Name : OTGDevice_InitPhyCon
// Function Desctiption : This function initializes OTG Phy.
// Input : NONE
// Output : NONE
// Version :
void OTGDevice_InitPhyCon(void)
{
OUTREG32(PHYPWR, 0x0);
OUTREG32(PHYCTRL, 0x20);
OUTREG32(RSTCON, 0x1);
OTGDevice_StallHelper(100);
OUTREG32(RSTCON, 0x0);
OTGDevice_StallHelper(100);
}
//////////
// Function Name : OTGDevice_SoftResetCore
// Function Desctiption : This function soft-resets OTG Core and then unresets again.
// Input : NONE
// Output : NONE
// Version :
void OTGDevice_SoftResetCore(void)
{
volatile UINT32 uTemp;
OUTREG32(GRSTCTL, CORE_SOFT_RESET);
OTGDevice_StallHelper(100);
do
{
uTemp = INREG32(GRSTCTL);
} while(!(uTemp & AHB_MASTER_IDLE));
}
//////////
// Function Name : OTGDevice_InitCore
// Function Desctiption : This function initializes OTG Link Core.
// Input : NONE
// Output : NONE
// Version :
void OTGDevice_InitCore(void)
{
OUTREG32(GAHBCFG, MODE_SLAVE|BURST_SINGLE|GBL_INT_UNMASK);
OUTREG32(GUSBCFG, 0<<15 // PHY Low Power Clock sel
|1<<14 // Non-Periodic TxFIFO Rewind Enable
|0x5<<10 // Turnaround time
|0<<9|0<<8 // [0:HNP disable, 1:HNP enable][ 0:SRP disable, 1:SRP enable] H1= 1,1
|0<<7 // Ulpi DDR sel
|0<<6 // 0: high speed utmi+, 1: full speed serial
|0<<4 // 0: utmi+, 1:ulpi
|1<<3 // phy i/f 0:8bit, 1:16bit
|0x7<<0 // HS/FS Timeout*
);
}
//////////
// Function Name : OTGDevice_CheckCurrentMode
// Function Desctiption : This function checks the current mode.
// Input : pucMode, current mode(device or host)
// Output : NONE
// Version :
void OTGDevice_CheckCurrentMode(UINT8 *pucMode)
{
*pucMode = (UINT8)(INREG32(GINTSTS)) & 0x1;
}
//////////
// Function Name : OTGDevice_SetSoftDisconnect
// Function Desctiption : This function puts the OTG device core in the disconnected state.
// Input : NONE
// Output : NONE
// Version :
void OTGDevice_SetSoftDisconnect(void)
{
SETREG32(DCTL, SOFT_DISCONNECT);
}
//////////
// Function Name : OTGDevice_ClearSoftDisconnect
// Function Desctiption : This function makes the OTG device core to exit from the disconnected state.
// Input : NONE
// Output : NONE
// Version :
void OTGDevice_ClearSoftDisconnect(void)
{
CLRREG32(DCTL, SOFT_DISCONNECT);
}
//////////
// Function Name : OTGDevice_InitDevice
// Function Desctiption : This function configures OTG Core to initial settings of device mode.
// Input : NONE
// Output : NONE
// Version :
void OTGDevice_InitDevice(void)
{
OUTREG32(DCFG, 1<<18|oOtgDev.m_eSpeed<<0); // [][1: full speed(30Mhz) 0:high speed]
OUTREG32(GINTMSK, OTGINTMASK); //gint unmask
}
//////////
// Function Name : OTGDevice_SetAddress
// Function Desctiption : This function sets the USB address of the device
// Input : address - sent by the USB host through the Set_Address command
// Output : NONE
// Version :
void OTGDevice_SetAddress(USHORT address)
{
USBDBGMSG(USBDBG_ZONE_VERBOSE, (L"UsbDbg: Setting device address to %d.\r\n", address));
OUTREG32(DCFG, 1<<18 | ((UINT8)address)<<4 | oOtgDev.m_eSpeed<<0);
}
//////////
// Function Name : OTGDevice_InitEndPts
// Function Desctiption : This function configures the endpoints based on the Device Descriptor.
// Input : USBDBG_DEVICE_DESCRIPTOR* pDeviceDesc
// Output : NONE
//
BOOL OTGDevice_InitEndPts(USBDBG_DEVICE_DESCRIPTOR* pDeviceDesc)
{
UINT32 i,j,k;
DWORD EpIndex = 0;
DWORD epNum;
USBDBG_CONFIG_DESCRIPTOR* pConfigDesc;
USBDBG_INTERFACE_DESCRIPTOR* pUsbDbg_IntrfcDesc;
USB_ENDPOINT_DESCRIPTOR* pEndPtDesc;
// Configure Endpoint register addresses
for (i=0; i<MAX_ENDPTS; i++)
{
EP_FIFO[i] = (EP0_FIFO + (0x1000*i));
DIEPCTL[i] = (DIEPCTL0 + (0x20*i));
DIEPINT[i] = (DIEPINT0 + (0x20*i));
DIEPTSIZ[i] = (DIEPTSIZ0 + (0x20*i));
DIEPDMA[i] = (DIEPDMA0 + (0x20*i));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -