mindrvr.c
来自「ata和atapi的驱动程序」· C语言 代码 · 共 2,161 行 · 第 1/5 页
C
2,161 行
// these commands transfer only 1 sector
if ( ( cmd == CMD_IDENTIFY_DEVICE )
|| ( cmd == CMD_IDENTIFY_DEVICE_PACKET )
)
numSect = 1;
// adjust multiple count
if ( multiCnt & 0x0800 )
{
// assume caller knows what they are doing
multiCnt &= 0x00ff;
}
else
{
// only Read Multiple uses multiCnt
if ( cmd != CMD_READ_MULTIPLE )
multiCnt = 1;
}
reg_cmd_info.ns = numSect;
reg_cmd_info.mc = multiCnt;
return exec_pio_data_in_cmd( dev, bufAddr, numSect, multiCnt );
}
//*************************************************************
//
// reg_pio_data_in_lba48() - Easy way to execute a PIO Data In command
// using an LBA sector address.
//
//*************************************************************
int reg_pio_data_in_lba48( unsigned char dev, unsigned char cmd,
unsigned int fr, unsigned int sc,
unsigned long lbahi, unsigned long lbalo,
unsigned char * bufAddr,
long numSect, int multiCnt )
{
reg_cmd_info.cmd = cmd;
reg_cmd_info.fr = fr;
reg_cmd_info.sc = sc;
reg_cmd_info.dh = (unsigned char) ( CB_DH_LBA | ( dev ? CB_DH_DEV1 : CB_DH_DEV0 ) );
reg_cmd_info.dc = (unsigned char) ( int_use_intr_flag ? 0 : CB_DC_NIEN );
reg_cmd_info.lbaSize = LBA48;
reg_cmd_info.lbaHigh = lbahi;
reg_cmd_info.lbaLow = lbalo;
// adjust multiple count
if ( multiCnt & 0x0800 )
{
// assume caller knows what they are doing
multiCnt &= 0x00ff;
}
else
{
// only Read Multiple Ext uses multiCnt
if ( cmd != CMD_READ_MULTIPLE_EXT )
multiCnt = 1;
}
reg_cmd_info.ns = numSect;
reg_cmd_info.mc = multiCnt;
return exec_pio_data_in_cmd( dev, bufAddr, numSect, multiCnt );
}
//*************************************************************
//
// exec_pio_data_out_cmd() - Execute a PIO Data Out command.
//
// See ATA-2 Section 9.4, ATA-3 Section 9.4,
// ATA-4 Section 8.7 Figure 11.
//
//*************************************************************
static int exec_pio_data_out_cmd( unsigned char dev,
unsigned char * bufAddr,
long numSect, int multiCnt );
static int exec_pio_data_out_cmd( unsigned char dev,
unsigned char * bufAddr,
long numSect, int multiCnt )
{
unsigned char status;
int loopFlag = 1;
long wordCnt;
// reset Bus Master Error bit
pio_writeBusMstrStatus( BM_SR_MASK_ERR );
// Set command time out.
tmr_set_timeout();
// Select the drive - call the sub_select function.
// Quit now if this fails.
if ( sub_select( dev ) )
{
return 1;
}
// Set up all the registers except the command register.
sub_setup_command();
// Start the command by setting the Command register. The drive
// should immediately set BUSY status.
pio_outbyte( CB_CMD, reg_cmd_info.cmd );
// Waste some time by reading the alternate status a few times.
// This gives the drive time to set BUSY in the status register on
// really fast systems. If we don't do this, a slow drive on a fast
// system may not set BUSY fast enough and we would think it had
// completed the command when it really had not even started the
// command yet.
DELAY400NS;
// Wait for not BUSY or time out.
// Note: No interrupt is generated for the
// first sector of a write command.
while ( 1 )
{
status = pio_inbyte( CB_ASTAT );
if ( ( status & CB_STAT_BSY ) == 0 )
break;
if ( tmr_chk_timeout() )
{
reg_cmd_info.to = 1;
reg_cmd_info.ec = 47;
loopFlag = 0;
break;
}
}
// This loop writes each sector.
while ( loopFlag )
{
// WRITE_LOOP:
//
// NOTE NOTE NOTE ... The primary status register (1f7) MUST NOT be
// read more than ONCE for each sector transferred! When the
// primary status register is read, the drive resets IRQ. The
// alternate status register (3f6) can be read any number of times.
// For correct results, transfer the 256 words (REP OUTSW), wait for
// interrupt and then read the primary status register. AS
// SOON as BOTH the primary status register has been read AND the
// last of the 256 words has been written, the drive is allowed to
// generate the next IRQ (newer and faster drives could generate
// the next IRQ in 50 microseconds or less). If the primary
// status register is read more than once, there is the possibility
// of a race between the drive and the software and the next IRQ
// could be reset before the system interrupt controller sees it.
// If BSY=0 and DRQ=1, transfer the data,
// even if we find out there is an error later.
if ( ( status & ( CB_STAT_BSY | CB_STAT_DRQ ) ) == CB_STAT_DRQ )
{
// increment number of DRQ packets
reg_cmd_info.drqPackets ++ ;
// determine the number of sectors to transfer
wordCnt = multiCnt ? multiCnt : 1;
if ( wordCnt > numSect )
wordCnt = numSect;
wordCnt = wordCnt * 256;
// Do the REP OUTSW to write the data for one DRQ block.
reg_cmd_info.totalBytesXfer += ( wordCnt << 1 );
pio_drq_block_out( CB_DATA, bufAddr, wordCnt );
DELAY400NS; // delay so device can get the status updated
// Note: The drive should have dropped DATA REQUEST and
// raised BUSY by now.
// Decrement the count of sectors to be transferred
// and increment buffer address.
numSect = numSect - ( multiCnt ? multiCnt : 1 );
bufAddr = bufAddr + ( 512 * ( multiCnt ? multiCnt : 1 ) );
}
// So was there any error condition?
if ( status & ( CB_STAT_BSY | CB_STAT_DF | CB_STAT_ERR ) )
{
reg_cmd_info.ec = 41;
break; // go to WRITE_DONE
}
// DRQ should have been set -- was it?
if ( ( status & CB_STAT_DRQ ) == 0 )
{
reg_cmd_info.ec = 42;
break; // go to WRITE_DONE
}
// Wait for interrupt -or- wait for not BUSY -or- wait for time out.
sub_wait_poll( 44, 45 );
// If polling or error read the status, otherwise
// get the status that was read by the interrupt handler.
if ( ( ! int_use_intr_flag ) || ( reg_cmd_info.ec ) )
status = pio_inbyte( CB_STAT );
else
status = int_ata_status;
// If there was a time out error, go to WRITE_DONE.
if ( reg_cmd_info.ec )
break; // go to WRITE_DONE
// If all of the requested sectors have been transferred, make a
// few more checks before we exit.
if ( numSect < 1 )
{
// Since the drive has transferred all of the sectors without
// error, the drive MUST not have BUSY, DEVICE FAULT, DATA REQUEST
// or ERROR status at this time.
if ( status & ( CB_STAT_BSY | CB_STAT_DF | CB_STAT_DRQ | CB_STAT_ERR ) )
{
reg_cmd_info.ec = 43;
break; // go to WRITE_DONE
}
// All sectors have been written without error, go to WRITE_DONE.
break; // go to WRITE_DONE
}
//
// This is the end of the write loop. If we get here, the loop
// is repeated to write the next sector. Go back to WRITE_LOOP.
}
// BMIDE Error=1?
if ( pio_readBusMstrStatus() & BM_SR_MASK_ERR )
{
reg_cmd_info.ec = 78; // yes
}
// WRITE_DONE:
// All done. The return values of this function are described in
// MINDRVR.H.
if ( reg_cmd_info.ec )
return 1;
return 0;
}
//*************************************************************
//
// reg_pio_data_out_lba28() - Easy way to execute a PIO Data In command
// using an LBA sector address.
//
//*************************************************************
int reg_pio_data_out_lba28( unsigned char dev, unsigned char cmd,
unsigned int fr, unsigned int sc,
unsigned long lba,
unsigned char * bufAddr,
long numSect, int multiCnt )
{
reg_cmd_info.cmd = cmd;
reg_cmd_info.fr = fr;
reg_cmd_info.sc = sc;
reg_cmd_info.dh = (unsigned char) ( CB_DH_LBA | ( dev ? CB_DH_DEV1 : CB_DH_DEV0 ) );
reg_cmd_info.dc = (unsigned char) ( int_use_intr_flag ? 0 : CB_DC_NIEN );
reg_cmd_info.lbaSize = LBA28;
reg_cmd_info.lbaHigh = 0;
reg_cmd_info.lbaLow = lba;
// adjust multiple count
if ( multiCnt & 0x0800 )
{
// assume caller knows what they are doing
multiCnt &= 0x00ff;
}
else
{
// only Write Multiple and CFA Write Multiple W/O Erase uses multiCnt
if ( ( cmd != CMD_WRITE_MULTIPLE )
&& ( cmd != CMD_CFA_WRITE_MULTIPLE_WO_ERASE )
)
multiCnt = 1;
}
reg_cmd_info.ns = numSect;
reg_cmd_info.mc = multiCnt;
return exec_pio_data_out_cmd( dev, bufAddr, numSect, multiCnt );
}
//*************************************************************
//
// reg_pio_data_out_lba48() - Easy way to execute a PIO Data In command
// using an LBA sector address.
//
//*************************************************************
int reg_pio_data_out_lba48( unsigned char dev, unsigned char cmd,
unsigned int fr, unsigned int sc,
unsigned long lbahi, unsigned long lbalo,
unsigned char * bufAddr,
long numSect, int multiCnt )
{
reg_cmd_info.cmd = cmd;
reg_cmd_info.fr = fr;
reg_cmd_info.sc = sc;
reg_cmd_info.dh = (unsigned char) ( CB_DH_LBA | ( dev ? CB_DH_DEV1 : CB_DH_DEV0 ) );
reg_cmd_info.dc = (unsigned char) ( int_use_intr_flag ? 0 : CB_DC_NIEN );
reg_cmd_info.lbaSize = LBA48;
reg_cmd_info.lbaHigh = lbahi;
reg_cmd_info.lbaLow = lbalo;
// adjust multiple count
if ( multiCnt & 0x0800 )
{
// assume caller knows what they are doing
multiCnt &= 0x00ff;
}
else
{
// only Write Multiple Ext uses multiCnt
if ( cmd != CMD_WRITE_MULTIPLE_EXT )
multiCnt = 1;
}
reg_cmd_info.ns = numSect;
reg_cmd_info.mc = multiCnt;
return exec_pio_data_out_cmd( dev, bufAddr, numSect, multiCnt );
}
#if INCLUDE_ATAPI_PIO
//*************************************************************
//
// reg_packet() - Execute an ATAPI Packet (A0H) command.
//
// See ATA-4 Section 9.10, Figure 14.
//
//*************************************************************
int reg_packet( unsigned char dev,
unsigned int cpbc,
unsigned char * cdbBufAddr,
int dir,
long dpbc,
unsigned char * dataBufAddr )
{
unsigned char status;
unsigned int byteCnt;
long wordCnt;
// reset Bus Master Error bit
pio_writeBusMstrStatus( BM_SR_MASK_ERR );
// Make sure the command packet size is either 12 or 16
// and save the command packet size and data.
cpbc = cpbc < 12 ? 12 : cpbc;
cpbc = cpbc > 12 ? 16 : cpbc;
// Setup current command information.
reg_cmd_info.cmd = CMD_PACKET;
reg_cmd_info.fr = 0;
reg_cmd_info.sc = 0;
reg_cmd_info.sn = 0;
reg_cmd_info.cl = (unsigned char) ( dpbc & 0x00ff );
reg_cmd_info.ch = ( unsigned char) ( ( dpbc & 0xff00 ) >> 8 );
reg_cmd_info.dh = (unsigned char) ( dev ? CB_DH_DEV1 : CB_DH_DEV0 );
reg_cmd_info.dc = (unsigned char) ( int_use_intr_flag ? 0 : CB_DC_NIEN );
// Set command time out.
tmr_set_timeout();
// Select the drive - call the sub_select function.
// Quit now if this fails.
if ( sub_select( dev ) )
{
return 1;
}
// Set up all the registers except the command register.
sub_setup_command();
// Start the command by setting the Command register. The drive
// should immediately set BUSY status.
pio_outbyte( CB_CMD, CMD_PACKET );
// Waste some time by reading the alternate status a few times.
// This gives the drive time to set BUSY in the status register on
// really fast systems. If we don't do this, a slow drive on a fast
// system may not set BUSY fast enough and we would think it had
// completed the command when it really had not even started the
// command yet.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?