⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ide.c

📁 CIRRUS 公司EP93XX系列CPU的WINCE下的BSP
💻 C
📖 第 1 页 / 共 3 页
字号:
//    SelectDevice();
//    WriteAltDriveController(ATA_CTRL_ENABLE_INTR);
//    
}
int ide_reset(void)
{
    DWORD dwAttempts;
    BYTE   bStatus;


//    if( m_BootDeviceType == BOOT_FROM_PCMCIA)
//    {
//        DEBUGMSG(ZONE_INFO, (TEXT("ide_reset: entered.\r\n")));
//        WAIT_IDE_BUSY;
//        WRITE_IDE_UCHAR(IDE_ALT_CTRL_REG,IDE_ALT_CTRL_SRST | IDE_ALT_CTRL_DSBL_INTR);
//        DelayInMsec(4);
//        WRITE_IDE_UCHAR(IDE_ALT_CTRL_REG,IDE_ALT_CTRL_DSBL_INTR);
//        WAIT_IDE_NOT_DRDY;
//        WAIT_IDE_BUSY;
//        if(IS_IDE_ERROR)
//        {
//            DEBUGMSG(1, (TEXT("WARNING: Drive reset failed.\r\n")));
//        }
//
//        DEBUGMSG(ZONE_INFO, (TEXT("ide_reset: done.\r\n")));
//        return 0;
//    }
//
//    WriteAltDriveController( 0x8);
    // Read the status first
    bStatus = GetBaseStatus();
    WriteAltDriveController(ATA_CTRL_RESET | ATA_CTRL_ENABLE_INTR);
    
    WaitForDisc( WAIT_TYPE_BUSY, 5000);
    
    WriteAltDriveController(ATA_CTRL_ENABLE_INTR);

    WaitForDisc( WAIT_TYPE_READY, 4000);
//    if (bSoftReset) 
//    {
//    SERPRINT("ATAPI:ResetController ...performing soft reset !\r\n");
//        AtapiSoftReset();
//    } 
//    else 
    {
        DelayInuSec(50000);
    }    
#define MAX_RESET_ATTEMPTS          10

    for (dwAttempts = 0; dwAttempts < MAX_RESET_ATTEMPTS; dwAttempts++)
    {
        bStatus = GetBaseStatus();
        
        if ((bStatus != ATA_STATUS_IDLE) && (bStatus != 0x0))
        {
            
            if (bStatus == 0xFF)
            {
                SetDriveHead( ATA_HEAD_DRIVE_2);
                
                bStatus = GetBaseStatus();
                
                if ((bStatus != ATA_STATUS_IDLE) && (bStatus != 0x0))
                {
                    SERPRINT("ATAPI::ResetController Drive 2 status = 0x%X\r\n", bStatus);
                    
                    if (bStatus == 0xFF)
                    {
                        SERPRINT("ATAPI::ResetController no second device\r\n");
                        return -1;
                    }
                }
                else
                {
                    break;
                }
            }
            DelayInuSec(50000);
        } 
        else 
        {
            break;
        }
    }
    if (dwAttempts == MAX_RESET_ATTEMPTS)
    {
        SERPRINT("ATAPIPCI:AtaResetController no response after %d reset attempts\r\n", MAX_RESET_ATTEMPTS);
        return -1;
    }
    return 0;
}
#define DEFAULT_WAIT_CHECK_ITER     2000
#define DEFAULT_WAIT_SAMPLE_TIMES   100
#define DEFAULT_WAIT_STALL_TIME     4000  

BYTE WaitOnBusy(BOOL fBase)
{ 
    DWORD i,j;
    BYTE bStatus;
    for (i=0; i< DEFAULT_WAIT_CHECK_ITER; i++)   
    {
        for (j=0; j<DEFAULT_WAIT_SAMPLE_TIMES; j++)  
        {
            bStatus = fBase ? GetBaseStatus() : GetAltStatus();
            if (!(bStatus & ATA_STATUS_BUSY)) 
            {
                return bStatus & (ATA_STATUS_ERROR |ATA_STATUS_BUSY);
            }    
        }
       DelayInuSec(DEFAULT_WAIT_STALL_TIME);            
    }
    return bStatus & (ATA_STATUS_ERROR |ATA_STATUS_BUSY);
}

BOOL IsAtapiDevice(void) 
{
//        return (m_Id.GeneralConfiguration & IDE_IDDATA_ATAPI_DEVICE);
    return (m_BootDeviceType==BOOT_FROM_IDE_CDROM);
}    

void AtapiSoftReset()
{
    WriteCommand( ATAPI_CMD_SOFT_RESET);
    DelayInuSec(400);
    WaitForDisc_P(WAIT_TYPE_NOT_BUSY,4000, 100);
    WaitForDisc_P(WAIT_TYPE_READY,5000, 100);
}

BOOL IsCDRomDevice(void) 
{
    return (((m_Id.GeneralConfiguration >> 8) & 0x1f) == ATA_IDDEVICE_CDROM);
}    

BOOL IsDVDROMDevice(void)
{
     return TRUE;
}

BOOL IsDiskDevice(void) 
{
    return (((m_Id.GeneralConfiguration >> 8) & 0x1f) == ATA_IDDEVICE_DISK);
}    

BOOL IsRemoveableDevice(void) 
{
    return (m_Id.GeneralConfiguration & IDE_IDDATA_REMOVABLE);
}    

BOOL IsDRQTypeIRQ(void)
{
    return ((m_Id.GeneralConfiguration >> 5) & 0x0003) == ATA_DRQTYPE_INTRQ;
}    
WORD GetPacketSize(void)
{
    return m_Id.GeneralConfiguration & 0x0003 ? 16 : 12;
}

#define IDE_COMMAND_SET_FEATURE                 0xEF

BOOL SetTransferMode(BYTE bMode)
{
    BYTE bError, bStatus;

    //* * * CAMSDB040504 - Try forceing the IDE/ATA mode to
    //* * * a UDMA mode. (START)
    //bMode = 0x41;
    //DEBUGMSG( 1, (TEXT("ATAPI:SetTransferMode(...) FORCING mode to Mode=%02X\r\n"), bMode));
    //* * * CAMSDB040504 - Try forceing the IDE/ATA mode to
    //* * * a UDMA mode. (START)
 
    SelectDevice();

    WaitForDisc(WAIT_TYPE_NOT_BUSY, 1000);
    WaitForDisc(WAIT_TYPE_READY, 10000);
    WaitOnBusy(TRUE);
    
    SelectDevice();

    WriteFeature(ATA_SET_TRANSFER_MODE);
    WriteSectorCount( bMode);
    WriteSectorNumber(0);
    WriteLowCount(0);
    WriteHighCount(0);
    WriteCommand( IDE_COMMAND_SET_FEATURE);

    SelectDevice();
    WaitOnBusy(TRUE);
    //
    //  Set Default PIO mode 
    //
    SelectDevice();
    WaitForDisc(WAIT_TYPE_NOT_BUSY, 2000);

    bStatus = GetBaseStatus();
    bError = GetError();
    // DEBUGMSG( 1, (TEXT("ATAPI:SetTransferMode Mode=%X Status=%X Error=%X\r\n"), bMode, bStatus, bError));
    if ((bStatus & 0x1) && (bError & 0x4)) {
        ide_reset();
        return FALSE;
    }

    //* * * CAMSDB04/05/04 - Re-print capabilities after forced mode above (START)
    //DEBUGMSG( 1, (TEXT("* * * ATAPI:Doing AnalyzeDeviceCapabilities() again START.\r\n")));
    //AnalyzeDeviceCapabilities();
    //DEBUGMSG( 1, (TEXT("* * * ATAPI:Doing AnalyzeDeviceCapabilities() again END.\r\n")));
    //* * * CAMSDB04/05/04 - Re-print capabilities after forced mode above (END)

    return TRUE;
}

int id_drive(void)
{
    DWORD   dwBlockSize;
    WORD    wDevType;
    PUSHORT tempS;
    BYTE    tempByte;
    int     k;

    WaitOnBusy(FALSE);
    
    SelectDevice();

    WriteCommand(ATA_CMD_IDENTIFY);
    if (WaitOnBusy(FALSE) & ATA_STATUS_ERROR)
    {
        // Not ATA Device. Try ATAPI !!!.
        SERPRINT("Not ATA Device. Try ATAPI !!!\r\n");
        if (GetError() & ATA_ERROR_ABORTED)
        {
            //
            // Try ATAPI!!
            //
            SelectDevice();
            WriteCommand(ATAPI_CMD_IDENTIFY);

            if (WaitOnBusy(FALSE) & ATA_STATUS_ERROR)
            {
                SERPRINT("ATAPI: Device not found.\r\n");
                return -1;
            }
        }
        m_BootDeviceType = BOOT_FROM_IDE_CDROM;
    }
    else
    {
        if(m_BootDeviceType == 0)
            m_BootDeviceType = BOOT_FROM_IDE_ATADISK;
    }
    dwBlockSize = sizeof(IDENTIFY_DATA);

    ASSERT( dwBlockSize <= BYTES_PER_SECTOR);

    if (m_f16Bit) 
    {
        dwBlockSize /= 2;
        ReadWordBuffer( (PWORD)&m_Id, dwBlockSize);
    } 
    else 
    {
        ReadByteBuffer( (PBYTE)&m_Id, dwBlockSize);
    }

    //
    //  Read the rest of data if available !!! And throw it away !!!
    //
    while (GetAltStatus() & ATA_STATUS_DATA_REQ ) 
    {
        if (m_f16Bit) 
            ReadWord();
        else
            ReadByte();
    }        

    //
    // Byte flip model number, revision, and serial number string.
    //
    tempS = m_Id.ModelNumber;
    for (k=0; k<20; k++) {
        tempByte = (UCHAR)(tempS[k] & 0x00FF);
        tempS[k] = tempS[k] >> 8;
        tempS[k] |= tempByte << 8;
    }
    
    tempS = m_Id.FirmwareRevision;
    for (k=0; k<4; k++) {
        tempByte = (UCHAR)(tempS[k] & 0x00FF);
        tempS[k] = tempS[k] >> 8;
        tempS[k] |= tempByte << 8;
    }
    
    tempS = m_Id.SerialNumber;
    for (k=0; k<10; k++) {
        tempByte = (UCHAR)(tempS[k] & 0x00FF);
        tempS[k] = tempS[k] >> 8;
        tempS[k] |= tempByte << 8;
    }

//    EdbgDumpHexBuf((PUCHAR)&m_Id, sizeof(IDENTIFY_DATA));

    if ((m_Id.GeneralConfiguration == 0) || 
       (m_Id.GeneralConfiguration == 0xffff)||
       (m_Id.GeneralConfiguration == 0xff7f)||
       (m_Id.GeneralConfiguration == 0x7fff)||
       ((m_Id.GeneralConfiguration == m_Id.IntegrityWord) && (m_Id.NumberOfCurrentCylinders == m_Id.IntegrityWord)))
    {   
        //DEBUGMSG( ZONE_INIT | ZONE_ERROR, (TEXT("ATAPI:Identify General Configuration %X is not valid !!!\r\n"), m_Id.GeneralConfiguration));
        return -1;
    }

    

    // 
    //  Convert ATA Configuration to the ATAPI Configuration.
    //  Clear  ATAPI Device Bit (8).

    if (m_BootDeviceType!=BOOT_FROM_IDE_CDROM)
    {
        m_Id.GeneralConfiguration &= ~IDE_IDDATA_REMOVABLE; 
    }

    wDevType = (m_Id.GeneralConfiguration>>8) & 0x1F;

    //DEBUGMSG(ZONE_INIT, (TEXT("ATAPI:Identify DevId %x wDevType (%x) \r\n"), wDevType));

    if (wDevType == ATA_IDDEVICE_UNKNOWN)   // Unknown Type or no device type
        return -1;      // Or Device not present!!!


    if (!((wDevType == ATA_IDDEVICE_DISK)   ||      // Direct Access Device
          (wDevType == ATA_IDDEVICE_CDROM) ||       // CD-ROM
          (wDevType == ATA_IDDEVICE_OPTICAL_MEM)))  // Optical Memory
    {         
        //DEBUGMSG(ZONE_INIT, (TEXT("ATAPI:Identify Not a supported device ???\r\n")));
#if 0
        return -1;
#else
        //DEBUGMSG(ZONE_INIT, (TEXT("ATAPI:Identify Assuming hard disk device\r\n")));
#endif  
        
    }
    
    if (wDevType == ATA_IDDEVICE_CDROM)     // CD-ROM
    {
        //DEBUGMSG( ZONE_INIT, (TEXT("ATAPI:Identify CD-ROM Device\r\n")));
//        m_dwDeviceFlags |=DFLAGS_DEVICE_CDROM;
    }

    //DEBUGMSG( ZONE_INIT, (TEXT("ATAPI:Identify Completed Stage 1 Status=%02X\r\n"),GetAltStatus()));

//    m_dwDeviceFlags |= DFLAGS_DEVICE_PRESENT;
//    m_dwDeviceFlags |= (IsAtapiDevice()) ? DFLAGS_ATAPI_DEVICE : 0;
//    m_dwDeviceFlags |= (IsRemoveableDevice()) ? DFLAGS_REMOVABLE_DRIVE : 0;

    SetTransferMode( (BYTE)4 | ATA_PIO_FCT_MODE);

    if (IsAtapiDevice()) 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -