hdd_if.c
资源名称:8202s.rar [点击查看]
上传用户:poi891205
上传日期:2013-07-15
资源大小:9745k
文件大小:11k
源码类别:
DVD
开发平台:
C/C++
- /*=================================================================
- hdd_if.c: Some ATA Control for HDD
- 2002-04-04 10:00AM Created by cyue
- 2002-04-28 10:00AM modified by Verdure
- 2002-06-12 08:42AM Modified by cyue
- Copyright(c)2000-2002 by Worldplus Technology (ShenZhen) Co., Ltd.
- ALL RIGHTS RESERVED
- =================================================================*/
- #include "misc.h"
- #include "set.h"
- #include "global.h"
- #include "ata.h"
- #include "atapi_if.h"
- #include "image.h"
- #include "fs.h"
- #include "func.h"
- #include "cderr.h"
- #include "avd.h"
- #include "hdd_if.h"
- #include "hdd_if.h"
- #ifdef USE_HDD // cyue: Skip code if not use hdd!!
- #define DBG_HDD
- #ifndef DBG_HDD
- #undef printf
- #undef print_block
- #define printf(f, a...) {}
- #define print_block(x,y) {}
- #endif
- //extern UINT8 CSSEnable;
- #define POLLING_MODE
- #ifdef POLLING_MODE //for new D version chip
- #define DVDDSP_CONFIG_DEF ATAPI_ADV_POLLING
- #else
- #define DVDDSP_CONFIG_DEF ATAPI_INTR_MODE
- #endif
- #define SHORT_TIME_OUT (1*90)//1 sec //4 sec
- #define NORMAL_TIME_OUT (7*90)//7 sec
- #define LONG_TIME_OUT (8*90)//8 sec
- #define MAX_TIME_OUT (10*90)//10 sec
- /* Check whether HD present */
- UINT8 HDD_DEV_Existence(void)
- {
- /* read Status register until BSY=0 & DRQ=0 */
- if(atapi_chk_time_out(WAIT_DRQ_BSY_ZERO,NORMAL_TIME_OUT)) //wait 8sec
- {
- printf("ATA BUS NOT READY!n");
- return ATAPI_ERROR;
- }
- atapi_reg_write(IDE_DEVICE_HEAD, HDD_DEV);
- /* read Status register until BSY=0 & DRQ=0 */
- if(atapi_chk_time_out(WAIT_DRQ_BSY_ZERO,NORMAL_TIME_OUT))
- {
- printf("HD NOT READY!!n");
- return ATAPI_ERROR;
- }
- return 0;
- }
- /* Identify the device before issuing the command to a device */
- void HDD_DEV_Identify(void)
- {
- while(atapi_reg_read(IDE_STATUS) & (STATUS_BSY | STATUS_DRQ)) ;
- atapi_reg_write(IDE_DEVICE_HEAD,HDD_DEV);
- atapi_reg_read(IDE_ALT_STATUS); //delay 400ns
- while(atapi_reg_read(IDE_STATUS) & (STATUS_BSY | STATUS_DRQ)) ;
- return;
- }
- //int ATA_Read(UINT32 lba,UINT32 seccnt)
- //UINT8 atapi_do_packet(UINT8 *pck)
- UINT8 hdd_do_packet( UINT32 wait_time, UINT8 *pck)
- {
- //UINT32 wait_time=0;
- //printf(__FUNCTION__":");
- //printf("--sec=%d,lba=%lxnn",*(pck+9),
- // (*(pck+2)<<24) | (*(pck+3)<<16) | (*(pck+4)<<8) | *(pck+5) );
- if (dev_status_flag&READ_PRESENT_DATA)
- wait_time=SHORT_TIME_OUT;
- else
- wait_time=NORMAL_TIME_OUT;
- //HDD_DEV_Identify();
- if (atapi_chk_time_out(WAIT_ALT_NO_BUSY,wait_time))
- {
- //printf(__FUNCTION__" ==> Timout 1n");
- cderr_handler(CDERR_ATA_BUSY);
- return ATAPI_ERROR;
- }
- atapi_reg_write(IDE_DEVICE_HEAD,HDD_DEV|(*(pck+2)&0x0f));
- atapi_reg_read(IDE_ALT_STATUS); //delay 400ns
- atapi_reg_write(IDE_SECTOR_CNT,*(pck+9));
- atapi_reg_write(IDE_SECTOR_NUM,*(pck+5));
- atapi_reg_write(IDE_CYLINDER_LO,*(pck+4));
- atapi_reg_write(IDE_CYLINDER_HI,*(pck+3));
- atapi_reg_write(IDE_COMMAND,0x20);
- atapi_reg_read(IDE_ALT_STATUS); //delay 400ns
- if (atapi_chk_time_out(WAIT_IRQ,MAX_TIME_OUT))
- {
- //printf(__FUNCTION__" ==>< IRQ Timout >n");
- cderr_handler(CDERR_ATA_BUSY);
- return ATAPI_ERROR;
- }
- if (atapi_chk_time_out(WAIT_STATUS_NO_BSY,wait_time))
- {
- //printf(__FUNCTION__" ==> Timout 4n");
- cderr_handler(CDERR_ATA_BUSY);
- return ATAPI_ERROR;
- }
- atapi_reg_read(IDE_ALT_STATUS);
- if (atapi_is_error() != 0)
- {
- //printf("send_data in do_packet errorn");
- return ATAPI_ERROR;
- }
- return ATAPI_OK;
- }
- int HDD_PIO_ReadData(UINT8 *buff)
- {
- int i;
- while(1)
- {
- atapi_reg_read(IDE_ALT_STATUS);
- switch(atapi_reg_read(IDE_STATUS)&(STATUS_BSY|STATUS_DRQ))
- {
- case STATUS_DRQ: //Need Data Transfer
- for(i=0;i<256;i++) // Sector Block Transfer
- {
- register UINT16 data=atapi_reg_read(IDE_DATA);
- *buff++=data&0xFF;
- *buff++=data>>8;
- }
- break;
- case 0: // Done
- atapi_reg_read(IDE_ALT_STATUS);
- atapi_reg_read(IDE_STATUS);
- return 0;
- case STATUS_BSY: // ATA Device Busy
- break;
- default:
- break;
- }
- }
- return 0;
- }
- int HDD_PIO_WriteData(UINT8 *buff)
- {
- int i;
- while(1)
- {
- switch(atapi_reg_read(IDE_STATUS)&(STATUS_BSY|STATUS_DRQ))
- {
- case STATUS_DRQ: //Need Data Transfer
- for(i=0;i<256;i++) // Sector Block Transfer
- {
- register UINT16 data= *buff+(*(buff+1))*256;
- atapi_reg_write(IDE_DATA,data);
- buff+=2;
- }
- break;
- case 0: // Done
- atapi_reg_read(IDE_ALT_STATUS);
- atapi_reg_read(IDE_STATUS);
- return 0;
- case STATUS_BSY: // ATA Device Busy
- break;
- default:
- break;
- }
- }
- return 0;
- }
- int HDD_ReadSec(UINT8 *buff,UINT32 lba,int seccnt)
- {
- //printf(__FUNCTION__":nn");
- HDD_DEV_Identify();
- atapi_reg_write(IDE_DEVICE_HEAD,HDD_DEV|(lba>>24));
- atapi_reg_read(IDE_ALT_STATUS); //delay 400ns
- atapi_reg_write(IDE_SECTOR_CNT,seccnt);
- atapi_reg_write(IDE_SECTOR_NUM,lba&0xFF);
- atapi_reg_write(IDE_CYLINDER_LO,(lba>>8)&0xFF);
- atapi_reg_write(IDE_CYLINDER_HI,(lba>>16)&0xFF);
- atapi_reg_write(IDE_COMMAND,0x20);
- atapi_reg_read(IDE_ALT_STATUS); //delay 400ns
- return HDD_PIO_ReadData(buff);
- }
- int HDD_WriteSec(UINT8 *buff,UINT32 lba,int seccnt)
- {
- //printf(__FUNCTION__":nn");
- HDD_DEV_Identify();
- atapi_reg_write(IDE_DEVICE_HEAD,HDD_DEV|(lba>>24));
- atapi_reg_read(IDE_ALT_STATUS); //delay 400ns
- atapi_reg_write(IDE_SECTOR_CNT,seccnt);
- atapi_reg_write(IDE_SECTOR_NUM,lba&0xFF);
- atapi_reg_write(IDE_CYLINDER_LO,(lba>>8)&0xFF);
- atapi_reg_write(IDE_CYLINDER_HI,(lba>>16)&0xFF);
- atapi_reg_write(IDE_COMMAND,0x30);
- atapi_reg_read(IDE_ALT_STATUS); //delay 400ns
- return HDD_PIO_WriteData(buff);
- }
- /* HD go to Sleep,and must wake it up by HD Reset */
- void HDD_Sleep(void)
- {
- //printf(__FUNCTION__":nn");
- HDD_DEV_Identify();
- atapi_reg_write(IDE_DEVICE_HEAD,HDD_DEV);
- atapi_reg_read(IDE_ALT_STATUS); //delay 400ns
- atapi_reg_write(IDE_COMMAND,0x99);
- atapi_reg_read(IDE_ALT_STATUS); //delay 400ns
- while(atapi_reg_read(IDE_STATUS)&STATUS_BSY) ;
- printf("NOW HD SLEEP!!!");
- }
- UINT32 HDD_GetMaxLBA()
- {
- //printf(__FUNCTION__":nn");
- HDD_DEV_Identify();
- atapi_reg_write(IDE_DEVICE_HEAD,HDD_DEV);
- atapi_reg_read(IDE_ALT_STATUS); //delay 400ns
- atapi_reg_write(IDE_COMMAND,0xF8);
- atapi_reg_read(IDE_ALT_STATUS); //delay 400ns
- while(atapi_reg_read(IDE_STATUS)&STATUS_BSY);
- printf("n--GetLBA: %x--n", atapi_reg_read(IDE_STATUS));
- return ( (UINT32)atapi_reg_read(IDE_SECTOR_NUM)
- +((UINT32)atapi_reg_read(IDE_CYLINDER_LO)<<8)
- +((UINT32)atapi_reg_read(IDE_CYLINDER_HI)<<16)
- +(((UINT32)atapi_reg_read(IDE_DEVICE_HEAD)&0x0F)<<24) );
- }
- int HDD_GetCHS(UINT8* buff)
- {
- //printf(__FUNCTION__":nn");
- HDD_DEV_Identify();
- atapi_reg_write(IDE_DEVICE_HEAD,HDD_DEV);
- atapi_reg_read(IDE_ALT_STATUS); //delay 400ns
- atapi_reg_write(IDE_COMMAND,0xEC);
- atapi_reg_read(IDE_ALT_STATUS); //delay 400ns
- return HDD_PIO_ReadData(buff);
- }
- void HDD_Reset(void)
- {
- //printf(__FUNCTION__":nn");
- #ifdef DVD728
- regs0->dvddsp_public = PIO_MODE4|ATAPI_ADV_POLLING;
- #else
- regs0->dvddsp_config = PIO_MODE4|ATAPI_ADV_POLLING;
- #endif
- atapi_rst(DSP_HOST_RESET|ATA_HW_RESET);
- #ifdef DVD728
- regs0->dvddsp_public = PIO_MODE4|ATAPI_ADV_POLLING;
- #else
- regs0->dvddsp_config = PIO_MODE4|ATAPI_ADV_POLLING;
- #endif
- while(atapi_reg_read(IDE_ALT_STATUS) & (STATUS_BSY|STATUS_ERR)) ;
- printf("n--Reset: %x--n", atapi_reg_read(IDE_STATUS));
- delay(6000);
- }
- void print_sector(UINT8 *buff)
- {
- int i,j;
- for(i=0;i<0x20;i++)
- {
- printf("n %02x0: ",i);
- for(j=0;j<0x10;j++)
- {
- printf(" %02x",*buff++);
- }
- }
- }
- void ata_init(void)
- {
- #ifdef DVD728
- regs0->dvddsp_public = PIO_MODE4|DVDDSP_CONFIG_DEF;//DVDDSP_CONFIG_DEF;
- #else
- regs0->dvddsp_config = PIO_MODE4|DVDDSP_CONFIG_DEF;//DVDDSP_CONFIG_DEF;
- #endif
- atapi_rst(DSP_HOST_RESET|ATA_HW_RESET);
- #ifdef DVD728
- regs0->dvddsp_public = PIO_MODE4|DVDDSP_CONFIG_DEF;//DVDDSP_CONFIG_DEF;
- #else
- regs0->dvddsp_config = PIO_MODE4|DVDDSP_CONFIG_DEF;//DVDDSP_CONFIG_DEF;
- #endif
- delay(1);
- printf("ATA Test Rdyn");
- }
- //UINT8 atapi_block_read(UINT32 lba, UINT32 len)
- UINT8 hdd_block_read(UINT32 lba, UINT32 len)
- {
- #if 0
- if (atapi_is_error() == ATAPI_ERROR)
- {
- printf("enter ata_block_read errorn");
- cderr_handler(CDERR_DAT_DISCONTINUE);
- return ATAPI_ERROR;
- }
- if (is_user_opened())
- {
- printf("USER OPEN!!!n");
- return ATAPI_ERROR;
- }
- #endif
- //printf("HDD_block_read:%lx---%lxn",lba,len);
- //regs0->dvddsp_config = ATAPI_DEVICE ;// | ATAPI_FIFO_FLUSH | ATAPI_CSS_ENABLE;
- dev_status_flag|=READ_PRESENT_DATA;
- //ATA_Read(lba, len*4);
- atapi_p_read_12(lba, len);
- #ifdef DVD728
- regs0->dvddsp_public = PIO_MODE4|DVDDSP_CONFIG_DEF;
- #else
- regs0->dvddsp_config = PIO_MODE4|DVDDSP_CONFIG_DEF;
- #endif
- regs0->dvddsp_blocksize = 256;
- regs0->dvddsp_blocklength = len;//*8;
- //regs0->dvddsp_function = (IDE_DATA<<8) | ATAPI_READ | DATA_MODE | HOST_START ;
- if (atapi_reg_read(IDE_STATUS) == 0x50)
- { //DRQ=0,BSY=0,DRDY=1,DSC=1
- printf("skip directly!n");
- return ATAPI_ERROR;
- }
- else
- {
- regs0->dvddsp_function = (IDE_DATA<<8) | ATAPI_READ | DATA_MODE | HOST_START ;
- }
- while ((regs0->dvddsp_function&HOST_BUSY) != 0) ;
- return ATAPI_OK;
- }
- #if 0
- UINT8 atapi_receive_data(UINT8 *p, int n)
- {
- UINT16 USER_DATA;
- int i,j;
- /*****
- * Host: read Alternate status register and ignore results. This
- * prevent polling host from reading status before it is valid
- ******/
- atapi_reg_read(IDE_ALT_STATUS);
- j = 0;
- do
- {
- /*****
- * Host: read Status register until BSY=0 & DRQ=1. Status register
- * is read to clear pending interrput
- *****/
- #ifdef POLLING_MODE
- if (atapi_chk_time_out(WAIT_DRQ_OK,LONG_TIME_OUT))
- {
- //printf(__FUNCTION__" ==> Timout 1n");
- cderr_handler(CDERR_ATA_BUSY);
- return ATAPI_ERROR;
- }
- #else
- while ( (atapi_reg_read(IDE_STATUS) & STATUS_BSY) ||!(atapi_reg_read(IDE_STATUS) & STATUS_DRQ) )
- {
- WaitIRQ();
- }
- #endif
- printf("data length:n=%dn",n);
- i = 512;
- if (i > n)
- i = n;
- /*****
- * DRQ block transfer
- *****/
- for (; i > 0; i-=2)
- {
- USER_DATA = atapi_reg_read(IDE_DATA);
- *(p+j+1) = USER_DATA >> 8;
- *(p+j) = USER_DATA & 0xff;
- j += 2;
- n -= 2;
- }
- } while (n != 0);
- /*****
- * Host: wait device to clear BSY=0, it indicates command is complete.
- *****/
- if (atapi_chk_time_out(WAIT_STATUS_NO_BSY,LONG_TIME_OUT))
- {
- //printf(__FUNCTION__" ==> Timout 2n");
- //cderr_handler(CDERR_ATA_BUSY);
- if (atapi_is_error()==0)
- {//printf(__FUNCTION__" ==> !!!!ATAPI ERROR n");}
- return ATAPI_ERROR;
- }
- /*****
- * Host: read Alternate status register and ignore results. This
- * polling host from reading status before it is valid.
- * Note - Device is command complete, it should perform its error routine if
- * an error is reported
- *****/
- atapi_reg_read(IDE_ALT_STATUS);
- return atapi_is_error();
- }
- #endif
- #endif //USE_HDD