📄 ata.c
字号:
//****************************************************************************
//
// ATA.C - Routines read and write data to an ATAPI device.
//
// Copyright (c) 2001 Cirrus Logic, Inc.
//
//****************************************************************************
#include "ep7312.h"
#include "lib7312.h"
#include "ata.h"
//
// see ATASimpleRegPacket()
//
unsigned char reg_atapi_reg_fr;
unsigned char reg_atapi_reg_sc;
unsigned char reg_atapi_reg_sn;
unsigned char reg_atapi_reg_dh;
//
// Global defines
//
REGCMDINFO reg_cmd_info;
//****************************************************************************
//
// Function Name: ATAInitializeInterface
//
// Function Description: Initializes the EP7312 to perform accesses to the
// ATA interface.
//
//****************************************************************************
void ATAInitializeInterface(void)
{
unsigned long * volatile ulRegister = (unsigned long *)(HwBaseAddress +
HwMemConfig2);
//
// Clear out the value of chip select 5 (bits 8-15) in the MemConfigReg2.
//
*ulRegister &= 0xFFFF00FF;
//
// Configure the EP7312's chip select for 8-bit bus width, 1 wait state for
// random accesses, and 0 wait states for sequential accesses.
//
*ulRegister |= 0x0000BE00;
}
//****************************************************************************
//
// Function Name: ATAReadCommandRegister
//
// Function Description: Reads an 8-bit value from an ATA command register.
//
//***************************************************************************
unsigned char ATAReadCommandRegister(unsigned char ucRegister)
{
unsigned char * volatile pucRegister = (unsigned char *)(ATACommandBaseAddress +
ucRegister);
unsigned char ucData;
//
// Read and the return the contents of the register.
//
ucData = *pucRegister;
//
// SerialSendString("\rRead command register ");
//
return(ucData);
}
//***************************************************************************
//
// Function Name: ATAWriteCommandRegister
//
// Function Description: Writes an 8-bit value to an ATA command register.
//
//***************************************************************************
void ATAWriteCommandRegister(unsigned char ucRegister, unsigned char ucData)
{
unsigned char * volatile pucRegister = (unsigned char *)(ATACommandBaseAddress +
ucRegister);
//
// Write to the register.
//
*pucRegister = ucData;
return;
}
//***************************************************************************
//
// Function Name: ATAReadControlRegister
//
// Function Description: Reads an 8-bit value from an ATA control register.
//
//***************************************************************************
unsigned char ATAReadControlRegister(unsigned char ucRegister)
{
unsigned char * volatile pucRegister = (unsigned char *)(ATAControlBaseAddress +
ucRegister);
unsigned char ucData;
//
// Read and the return the contents of the register.
//
ucData = *pucRegister;
return(ucData);
}
//***************************************************************************
//
// Function Name: ATAWriteControlRegister
//
// Function Description: Writes an 8-bit value to an ATA control register.
//
//***************************************************************************
void ATAWriteControlRegister(unsigned char ucRegister, unsigned char ucData)
{
unsigned char * volatile pucRegister = (unsigned char *)(ATAControlBaseAddress +
ucRegister);
//
// Write to the register.
//
*pucRegister = ucData;
return;
}
//***************************************************************************
//
// Function Name: ATAReadDataRegister
//
// Function Description: Reads 16-bit values from the ATA data register.
//
//***************************************************************************
unsigned long ATAReadDataRegister(unsigned short *pusBuffer, unsigned long ulCount)
{
unsigned short * volatile pusRegister = (unsigned short *)(ATACommandBaseAddress +
DATAREGISTER);
unsigned long * volatile pulRegister = (unsigned long *)(HwBaseAddress +
HwMemConfig2);
unsigned long ulTempCount = 0;
unsigned short winzips_temp = 0;
unsigned short status;
unsigned long ulTemp;
//
// Save the configuration for the ATA interface's chip select.
//
ulTemp = *pulRegister;
//
// Read in the number of words specified by the ulCount parameter.
//
//
// Wait for the DRQ bit in the ATA Status register to be set.
//
while(!(ATAReadCommandRegister(STATUSREGISTER) & ATADRQ));
//
// Configure the ATA interface's chip select to perform 16-bit accesses.
//
*pulRegister &= 0xfffffcff;
*pulRegister |= 0x00000100;
while((ulTempCount<ulCount) && (ATAReadCommandRegister(STATUSREGISTER) & ATADRQ))
{
//
// Read in the data and increment the pointer.
//
*pusBuffer = *pusRegister;
winzips_temp = *pusBuffer;
pusBuffer++;
//
// Increment the count.
//
ulTempCount++;
status = ATAReadCommandRegister(STATUSREGISTER);
}
//
// Restore the chip select configuration.
//
*pulRegister = ulTemp;
status = ATAReadCommandRegister(STATUSREGISTER);
return(ulTempCount);
}
//***************************************************************************
//
// Function Name: ATAWriteDataRegister
//
// Function Description: Writes 16-bit values to the ATA data register.
//
//***************************************************************************
unsigned long ATAWriteDataRegister(unsigned short *pusBuffer, unsigned long ulCount)
{
unsigned short * volatile pusRegister = (unsigned short *)(ATACommandBaseAddress +
DATAREGISTER);
unsigned long * volatile pulRegister = (unsigned long *)(HwBaseAddress +
HwMemConfig2);
unsigned long ulTempCount = 0;
unsigned long ulTemp;
unsigned short temp = 0;
//
//Save the configuration for the ATA interface's chip select.
//
ulTemp = *pulRegister;
//
// Read in the number of words specified by the ulCount parameter.
//
//
// Wait for the DRQ bit in the ATA Status register to be set.
//
while(!(ATAReadCommandRegister(STATUSREGISTER) & ATADRQ));
//
// Configure the ATA interface's chip select to perform 16-bit accesses.
//
*pulRegister &= 0xfffffcff;
*pulRegister |= 0x00000100;
//
// (ATAReadCommandRegister(STATUSREGISTER) & ATADRQ))
//
while (ulTempCount<ulCount)
{
//
// Read in the data and increment the pointer.
//
temp = *pusBuffer;
*pusRegister = *pusBuffer;
pusBuffer++;
//
// Increment the count.
//
ulTempCount++;
}
//
// Restore the chip select configuration.
//
*pulRegister = ulTemp;
return(ulTempCount);
}
//***************************************************************************
//
// Function Name: ATASelectDevice
//
// Function Description: Must be called before any command is issued.
//
//***************************************************************************
void ATASelectDevice (int DeviceNumber)
{
//
// Wait until BSY=0 and DRQ=0
//
while(ATAReadCommandRegister(STATUSREGISTER) & (ATABSY | ATADRQ));
if(DeviceNumber == 0)
ATAWriteCommandRegister(DEVICEHEADREGISTER, DEVICEHEAD_DEV0);
else
ATAWriteCommandRegister(DEVICEHEADREGISTER, DEVICEHEAD_DEV1);
//
// Wait until BSY=0 and DRQ=0
//
while(ATAReadCommandRegister(STATUSREGISTER) & (ATABSY | ATADRQ));
return;
}
//*********************************************************************
//
// Function Name: ATASoftReset
//
// Function Description: Function perform an ATA soft reset sequence.
//
//*********************************************************************
void ATASoftReset(void)
{
unsigned long ulIndex;
//
// Check for a clear BSY bit in the status register.
//
for(ulIndex=0;ulIndex<74000;ulIndex++);
while(ATAReadCommandRegister(STATUSREGISTER) & ATABSY);
//
// Perform the reset.
//
ATAWriteControlRegister(DEVICECONTROLREGISTER, 0x06);
for(ulIndex=0;ulIndex<500;ulIndex++);
ATAWriteControlRegister(DEVICECONTROLREGISTER, 0x02);
//
// Wait for the soft reset to complete.
//
for(ulIndex=0;ulIndex<74000;ulIndex++);
while(ATAReadCommandRegister(STATUSREGISTER) & (ATABSY | ATADRDY));
}
//************************************************************************
//
// Function Name: ATADeviceReset
//
// Function Description: Function performs an ATA device reset command.
//
//************************************************************************
void ATADeviceReset(unsigned char ucDevice)
{
while(ATAReadCommandRegister(STATUSREGISTER) & (ATABSY | ATADRQ));
//
// Select the device that is to be reset.
//
if(ucDevice == DEVICE0)
ATAWriteCommandRegister(DEVICEHEADREGISTER, DEVICE0);
else
ATAWriteCommandRegister(DEVICEHEADREGISTER, DEVICE1);
//
// Write the ATA Device Reset to the Command Register.
//
ATAWriteCommandRegister(COMMANDREGISTER, 0x08);
//
// Wait until the command is complete.
//
while(ATAReadCommandRegister(STATUSREGISTER) & ATABSY);
}
//*******************************************************************
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -