📄 ata_processor.c
字号:
/******************************************************************************
*
* (c) copyright Freescale Semiconductor Hong Kong Ltd 2004
* ALL RIGHTS RESERVED
*
*******************************************************************************
** THIS CODE IS ONLY INTENDED AS AN EXAMPLE FOR DEMONSTRATING THE FREESCALE **
** MICROCONTROLLERS. IT HAS ONLY BEEN GIVEN A MIMIMUM LEVEL OF TEST. IT IS **
** PROVIDED 'AS SEEN' WITH NO GUARANTEES AND NO PROMISE OF SUPPORT. **
*******************************************************************************
*
* FILE: ata_processor.c REVISION 0.1
*
* DESCRIPTION: ata drivers.
*
* NOTES: All modules remain at their reset addresses
*
* UPDATED HISTORY:
*
* REV YYYY.MM.DD AUTHOR DESCRIPTION OF CHANGE
* --- ---------- ------ ---------------------
* 0.0 2003.07.27 Derek Lau Initial version
* 0.1 2004.04.08 Derek Lau Demo version
*
******************************************************************************/
/* Freescale is not obligated to provide any support, upgrades or new */
/* releases of the Software. Freescale may make changes to the Software at */
/* any time, without any obligation to notify or provide updated versions of */
/* the Software to you. Freescale expressly disclaims any warranty for the */
/* Software. The Software is provided as is, without warranty of any kind, */
/* either express or implied, including, without limitation, the implied */
/* warranties of merchantability, fitness for a particular purpose, or */
/* non-infringement. You assume the entire risk arising out of the use or */
/* performance of the Software, or any systems you design using the software */
/* (if any). Nothing may be construed as a warranty or representation by */
/* Freescale that the Software or any derivative work developed with or */
/* incorporating the Software will be free from infringement of the */
/* intellectual property rights of third parties. In no event will Freescale */
/* be liable, whether in contract, tort, or otherwise, for any incidental, */
/* special, indirect, consequential or punitive damages, including, but not */
/* limited to, damages for any loss of use, loss of time, inconvenience, */
/* commercial loss, or lost profits, savings, or revenues to the full extent */
/* such may be disclaimed by law. The Software is not fault tolerant and is */
/* not designed, manufactured or intended by Freescale for incorporation */
/* into products intended for use or resale in on-line control equipment in */
/* hazardous, dangerous to life or potentially life-threatening environments */
/* requiring fail-safe performance, such as in the operation of nuclear */
/* facilities, aircraft navigation or communication systems, air traffic */
/* control, direct life support machines or weapons systems, in which the */
/* failure of products could lead directly to death, personal injury or */
/* severe physical or environmental damage (High Risk Activities). You */
/* specifically represent and warrant that you will not use the Software or */
/* any derivative work of the Software for High Risk Activities. */
/* Freescale and the Freescale logos are registered trademarks of Freescale */
/* Semiconductor Inc. */
/*****************************************************************************/
#include "FreescaleDef.h" // Get my definitions (Constants & Macros)
#include "uf32reg.h" // Get the uf32 registers.
#include "mk_extern.h" // Get Mini-Kernel global prototypes
#include "ata_includes.h" // Get ATA module configuration
#include "ata_extern.h" // Get ATA exported global prototypes
#include "usbms_extern.h"
//#include "scsi_includes.h" // Get SCSI configuration
//#include "scsi_extern.h" // Get SCSI exported global prototypes
#pragma CODE_SEG DEFAULT
// code is placed in the main code area.
// ==================================================================
// ATA_ATAPIRead() -
//
// PIO Read block data from ATA device
//
// Input - = Buffer pointer, LBA, Block of transfer
//
// Output - = Data wrote to buffer
//
// Function returns ATA error code
//
// ==================================================================
ATAErrorCode ATA_ATAPIRead(muint16 *pBuffer, muint8 BlockSize)
{
ATAErrorCode error;
sATACommand sATACmd;
sATACmd.features = 0 ;
sATACmd.count = BlockSize;
sATACmd.lba_low =(muint8) *(gpbCBWPacket+kSCSI10LBAByte0);
sATACmd.lba_mid = (muint8) *(gpbCBWPacket+kSCSI10LBAByte0);
sATACmd.lba_high = (muint8) *(gpbCBWPacket+kSCSI10LBAByte0);
sATACmd.device = (muint8) *(gpbCBWPacket+kSCSI10LBAByte0) | 0x40;
sATACmd.command = kATACmdRead;
sATACmd.dmamode = 0;
error = ATA_Command(&sATACmd);
if (error)
return (error);
error = ATA_PIORead(pBuffer, (muint16) BlockSize << kOneByte);
if (error)
return (error);
return (kATAPass);
}
// ==================================================================
// ATA_SCSIInqury() -
//
// SCSI Inqury Command
//
// Input - = Buffer pointer, XferLen
//
// Output - = Inquiry out Data wrote to buffer
// = Temp data used for Identify Device
//
// Function returns ATA error code
//
// ==================================================================
ATAErrorCode ATA_SCSIInquiry(muint16 *pBuffer)
{
ATAErrorCode error;
muint8 i;
error = ATA_IdentifyDevice( (muint16 *) pBuffer, kATADevInfoModelEnd);
if (error)
return (error);
for (i=0; i<14; i++)
{
QC3DR = (*(pBuffer+kATADevInfoModelBeg+i) << 8) | (*(pBuffer+kATADevInfoModelBeg+i)>>8);
}
return (kATAPass);
}
// ==================================================================
// ATA_SCSIReadCapacity() -
//
// SCSI Read Capacity / ATA Read Native Max Address
//
// Input - = Buffer pointer
//
// Output - = Data wrote to buffer
//
// Function returns ATA error code
//
// ==================================================================
ATAErrorCode ATA_SCSIReadCapacity(muint8 *pBuffer)
{
ATAErrorCode error, dummy;
ATA_DDHR_B = 0x40; // LBA mode
error = ATA_WaitRegBusy();
if (error)
return(error);
ATA_DCR = (kATACmdReadMaxAdd << 8);
error = ATA_WaitRegBusy();
if (error)
return(error);
error = ATA_WaitATABusy();
if (!error)
{
dummy = ATA_DDHR_B; // bit27 - bit24
error = ATA_WaitRegBusy();
if (error)
return(error);
*(pBuffer) = ATA_DDHR_B & 0x0f; // (mask the upper 4 bits)
dummy = ATA_LBAH_B;
error = ATA_WaitRegBusy();
if (error)
return(error);
*(pBuffer+1) = ATA_LBAH_B; // bit23 - bit16
dummy = ATA_LBAM_B;
error = ATA_WaitRegBusy();
if (error)
return(error);
*(pBuffer+2) = ATA_LBAM_B; // bit15 - bit8
dummy = ATA_LBAL_B;
error = ATA_WaitRegBusy();
if (error)
return(error);
*(pBuffer+3) = ATA_LBAL_B; // bit7 - bit0
if ((*pBuffer == 0x0f) && ( *(pBuffer+1) == 0xff))
{
ATA_DCR = (kATACmdReadMaxAddExt << 8);
error = ATA_WaitATABusy();
if (!error)
{
ATA_DCTR_B = (1<<ATAHOB); // 48 bit access
(void) ATA_WaitRegBusy();
dummy = ATA_LBAL_B;
(void) ATA_WaitRegBusy();
*(pBuffer) = ATA_LBAL_B; // bit 31 - bit24
ATA_DCTR_B = 0; //
(void) ATA_WaitRegBusy();
}
}
}
else
{
*(pBuffer) = (muint8) (gATAMaxCapacity >> kThreeByte);
*(pBuffer+1) = (muint8) (gATAMaxCapacity >> kTwoByte);
*(pBuffer+2) = (muint8) (gATAMaxCapacity >> kOneByte);
*(pBuffer+3) = (muint8) gATAMaxCapacity;
}
*(pBuffer+4) = 0;
*(pBuffer+5) = 0;
*(pBuffer+6) = 0x02; // 512 bytes per block
*(pBuffer+7) = 0x00;
return (kATAPass);
}
// ==================================================================
// ATA_IdentifyDevice() -
//
// ATA Identify Device command / Identify Packet Device Command
//
// Input - = Buffer pointer, Transfer Length
//
// Output - = Device information at buffer
//
// Function returns ATA error code
//
// ==================================================================
ATAErrorCode ATA_IdentifyDevice(muint16 *pBuffer, muint16 XferLen)
{
ATAErrorCode error;
error = ATA_WaitRegBusy();
if (error)
return(error);
ATA_DDHR_B =0;
error = ATA_WaitRegBusy();
if (error)
return(error);
if (mCheckBit(bitATAATAPI,gATADevInfo))
ATA_DCR = (kATACmdIdentifyP << 8);
else
ATA_DCR = (kATACmdIdentify << 8);
error = ATA_WaitRegBusy();
if (error)
return(error);
error = ATA_WaitATABusy();
if (error)
return(error);
error = ATA_PIORead(pBuffer, XferLen);
if (error)
return(error);
return (kATAPass);
}
//
// The end of file ata_processor.c
// *********************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -