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

📄 ide.h

📁 IDE 驱动程序对开发硬盘驱动很有用,对学习LINUX驱动开发有帮助
💻 H
字号:
/************************************************************************
//    ide.h
//
//    Modification History:
//          $Id: ide.h,v 1.15 2005/10/05 09:38:06 raphael Exp $
//          $Log: ide.h,v $
//          Revision 1.15  2005/10/05 09:38:06  raphael
//          Merge SONATA2_7_8 into Sonata2Customer head.
//
//          Revision 1.29.4.2  2005/08/17 08:16:14  hwxiao
//          modify HDD sleep mode. add idle Immediate command.
//
//          Revision 1.29.4.1  2005/06/13 02:59:33  hwxiao
//          modify file System for sonata 2.7, add version control and edit function
//
//          Revision 1.29  2005/06/06 19:18:36  rinllow
//          37512     Card Reader : Upgrade OnSpec firmware form card-reader.
//
//          Revision 1.28  2005/04/19 21:56:27  rinllow
//          reorganize card reader state machine
//
//          Revision 1.27  2005/02/13 22:51:55  mahashin
//          Port of fixes from 2.4.8 branch.
//
//          Revision 1.26  2005/02/06 05:30:34  rinllow
//          [32711] fix memory card reader issues
//
//          Revision 1.25  2005/02/02 07:46:45  hwxiao
//          fix bug 31418,31479, modify for flash card hot plug
//
//          Revision 1.24  2005/01/14 18:24:00  mahashin
//          Added log header
//
//
*************************************************************************/

#ifndef _IDE_H
#define _IDE_H

#include "ata.h"
#include "apitypes.h"		// Uint32
#include "nucleus.h"

#define FAT12_16                    1

#define CURRENT_MAGNUM_VERSION      1

#define SWAP_ALIGN                  0    /* Used for Little Endian */
#define BIG_ENDIAN                  1    /* Used for Big Endian    */

/*  Set this to total number of logical drives to support. */
#define NDRIVES                     4
#define NDISKS                      4


// return code
#define IDE_IO_ERROR                0x01
#define IDE_MALLOC_ERROR            0x02
#define IDE_INTERNAL                0x03
#define IDE_BADDRIVE                0x04
#define IDE_BADPARM                 0x05
#define IDE_BADMBR                  0x06
#define IDE_BADDRIVENO              0x07

#define IDE_MBR_RESET               0x08

// ata command code
#define IDE_CMD_REST                        0x08        /* device reset                     */
#define IDE_CMD_READS                       0x20        /* Read sector(s)                   */
#define IDE_CMD_WRITES                      0x30        /* Write sector(s)                  */

#define IDE_CMD_READV                       0x40        /* Read verify sectors(s)           */
#define IDE_CMD_FORMAT                      0x50        /* Mark sectors bad on a track      */
#define IDE_CMD_SEEK                        0x70        /* Seek to a track                  */
#define IDE_CMD_STATUS                      0x82
#define IDE_CMD_DIAG                        0x90        /* Initiate drive diagnostics       */
#define IDE_CMD_STANDBY_IMMEDIATE           0x94
#define IDE_CMD_PASSTHRU_FLASH              0x9f
#define IDE_CMD_CLEAN                       0xB2
#define IDE_CMD_MEDIA_TYPE                  0xB8
#define IDE_CMD_READM                       0xC4        /* Read multiple sectors/interrupt  */
#define IDE_CMD_WRITEM                      0xC5        /* Write multiple sectors/interrupt */
#define IDE_CMD_WRITEDMA                    0xCB
#define IDE_CMD_SETM                        0xC6        /* Set multiple mode                */
#define IDE_CMD_SCAN                        0xd5
#define IDE_CMD_MEDIA                       0xda
#define IDE_CMD_IDLE_IMMEDIATE              0xE1
#define IDE_CMD_READB                       0xE4        /* Read first drive buffer          */
#define IDE_CMD_WRITEB                      0xE8        /* Write drive buffer               */
#define IDE_CMD_IDENT                       0xEC        /* Return drive parameters          */
#define IDE_CMD_CHECK_POWER_MODE            0xE5
#define IDE_CMD_SLEEP                       0xE6        /* Place drive in low power mode    */
#define IDE_CMD_MSNS                        0xEF

#define IDE_CMD_READS_EXT                   0x24
#define IDE_CMD_WRITES_EXT                  0x34
#define IDE_CMD_READM_EXT                   0x29
#define IDE_CMD_WRITEM_EXT                  0x39
#define IDE_CMD_WRITEDMA_EXT                0x35
#define IDE_CMD_SET_MAX_ADD_EXT             0x37
#define IDE_CMD_READ_VERIFY_EXT             0x42
#define IDE_CMD_READ_NATIVE_MAX_ADDR_EXT    0x27
#define IDE_CMD_READDMA_EXT                 0x25
#define IDE_CMD_FLUSH_CACHE_EXT             0xEA

#define IDE_CMD_READ            0x1
#define IDE_CMD_WRITE           0x2

#define MAX_READ_DMA_SIZE       0x1000			// max size for the CS98k DMA read is 4 kB
#define MAX_WRITE_DMA_SIZE      0x800			// the size of the 98k SRAM is only 2k

#define LBA_MODE                0x40
#define HD_SECTOR_SIZE          512

//mbr layout offset,see PTABLE_ENTRY for reference
#define MBR_OFFSET_BOOT     (0)
#define MBR_OFFSET_S_HEAD   (MBR_OFFSET_BOOT   + 1)
#define MBR_OFFSET_S_SEC    (MBR_OFFSET_S_HEAD + 1)
#define MBR_OFFSET_S_CYL    (MBR_OFFSET_S_HEAD + 2)
#define MBR_OFFSET_P_TYP    (MBR_OFFSET_S_HEAD + 3)
#define MBR_OFFSET_E_HEAD   (MBR_OFFSET_S_HEAD + 4)
#define MBR_OFFSET_E_SEC    (MBR_OFFSET_S_HEAD + 5)
#define MBR_OFFSET_E_CYL    (MBR_OFFSET_S_HEAD + 6)
#define MBR_OFFSET_R_SEC    (MBR_OFFSET_E_CYL  + 1)
#define MBR_OFFSET_P_SIZE   (MBR_OFFSET_R_SEC  + 4)

#ifdef BIG_ENDIAN
#define SWAP16(X,Y) UtilSwap16(X,Y)
#define SWAP32(X,Y) UtilSwap32(X,Y)
#else 
#if (SWAP_ALIGN)   /* Little endian alignment care */
    #define SWAP16(X,Y)   UtilThrough16(X,Y)
    #define SWAP32(X,Y)   UtilThrough32(X,Y)
#else  
    #define SWAP16(X,Y)   *(Uint16 *)(X) = *(Uint16 *)(Y)
    #define SWAP32(X,Y)   *(Uint32 *)(X) = *(Uint32 *)(Y)
#endif
#endif /* ifdef BIG_ENDIAN */

enum 
{
    IDE_ERRORCODE_NOERROR                  = 0,
    IDE_ERRORCODE_INVALID_DRIVE            = 0x00000100,
    IDE_ERRORCODE_TIMEOUT                  = 0x00000200,
    IDE_ERRORCODE_INSUFFICIENT_BUF_SIZE    = 0x00000400,
    IDE_ERRORCODE_SIZE_MISMATCH            = 0x00000800,
    IDE_ERRORCODE_CHECK_SENSE	           = 0x00001000,
    IDE_WARNINGCODE_REASON_MISMATCH        = 0x00010000
};

typedef enum
{
	MEDIADEVICE_BRAND_UNKNOWN,
	MEDIADEVICE_BRAND_SAIN,
	MEDIADEVICE_BRAND_ONSPEC,
	MEDIADEVICE_BRAND_NOEXIST,
}MEDIADEVICE_BRAND;

typedef enum
{
	IDE_STATE_IDLE,
	IDE_STATE_UPGRADE_FIRMWARE,
	IDE_STATE_RUNNING,
}IDE_STATE;

typedef struct {
	ATA_Device_t    driveID;
	Uint32          cmdCode;
	Uint32          *pktCmd;
	Uint32          pktCmdSize;
	Uint8           *data;
	Uint32          lba;
	Uint32          dataSize;
	Uint32          maxDataSize;
	Uint32          featureReg;
	Uint32          sectorCntReg;
	Uint32          deviceReg;
	Uint32          readWrite;      /* bit 0 for read bit 1 for write */
	Boolean         ext_cmd;
} IDE_Cmd;

typedef struct IDE_DRIVE_DESC
{
	Uint8               driveID;
	Uint8	            serial[20];
	Uint8	            firmware[8];
	Uint8	            model[40];
	Uint32              maxLBA;
	Uint32              maxMultipleTransfer;
	Uint32              currentMultipleTransfer;
	Boolean             initialized;
	Boolean	            IsRemovableMediaDevice;
	Boolean	            supports48bitAddFeatureSet;
	MEDIADEVICE_BRAND   MediaDeviceBrand;
	Boolean             mediaChanged;
	IDE_STATE           state;
} ide_drive_desc;

typedef struct LOGICAL_DRIVE_DESC
{
	Uint32      phys_disk;
	Uint8       open_count;
	Uint32      drv_start;
	Uint32      drv_end;
	int         fat_type;
	Uint8       part_no;
	Boolean     superClusterFormated;

	Uint32      rootblock;                  /* First block of root dir */
	Uint32      maxfindex;                  /* Last element in the fat */
	int         fat_is_dirty;               /* FAT update flag */
	Uint16      valid_fsinfo;               /* fsinfo valid  flag */
	Uint32      free_clusters_count;        /* If non-zero pc_free may use this value */
	Uint32      free_contig_pointer;        /* Efficiently stored */
	Uint32      start_free_clusters_count;  /* start value */
	Uint32      start_free_contig_pointer;  /* start value */

          /******** BPB  ***********/
	Uint16      bytspsector;        /* 0x0b Must be 512 for this implementation */
	Uint8       secpcluster;        /* 0x0d Sectors per cluster */
	Uint16      fatblock;           /* 0x0e Reserved sectors before the FAT */
#ifdef FAT12_16
	Uint16      fat_type_based_on_csize;
	Uint16	    root_ent_cnt;       /* Root Entry Count */
	Uint32	    root_dir_sect;      /* No. of sectors occupied by Root Dir */
	Uint32	    cluster_count;      /* Cluster count */
	Uint32	    max_cluster;        /* Max Cluster number */
	Uint32	    first_datasector;
	Uint32	    rootdirblock;       /* for FAT12 */
	Uint16	    numsecs16;
	Uint16	    secpfat16;
#endif
	Uint8       numfats;            /* 0x10 Number of FATS on the disk */
	Uint32      numsecs;            /* 0x13 Total # sectors on the disk */
	Uint16      secptrk;            /* 0x18 sectors per track */
	Uint32      secpfat;            /* 0x24 Size of each FAT on the FAT32 */
	Uint16      fat_flag;           /* 0x28 FAT flag */
	Uint32      rootdirstartcl;     /* 0x2c root cluster */
	Uint16      fsinfo;             /* 0x30 fsinfo block */
	Uint16	    scfatblock;	        /* 0x34 CIRRUS Super Cluster FAT sector */
	Uint16      magnum_version;     /* 0x36 MAGNUM file system version */
} logical_drive_desc;

/* Partition table descriptions. */
typedef struct ptable_entry
{
    Uint8       boot;               /* BootSignature */
    Uint8       s_head;             /* StartHead */
    Uint8       s_sec;              /* StartSector(Bit0-5) Bit6 and 7 are cylinder number */
    Uint8       s_cyl;              /* StartCylinder Upper two bit of starting clyinder number are in StartSector field. */
    Uint8       p_typ;              /* Partition Type */
    Uint8       e_head;             /* EndHead */
    Uint8       e_sec;              /* EndSector(Bit0-5) Bit6 and 7 are cylinder number*/
    Uint8       e_cyl;              /* EndCylinder Upper two bit of ending clyinder number are in StartSector field. */
    Uint32      r_sec;              /* Relativity sector */
    Uint32      p_size;             /* Size of partition */

} PTABLE_ENTRY;

/* (Master) Boot Record structure */
typedef struct ptable
{
    PTABLE_ENTRY    ents[4];        /* Entry table */
    Uint16          signature;      /* should be 0xAA55 */
} PTABLE;

typedef struct 
{
	Uint32      maxMultipleTransfer;        // Multiple transfer size
	Uint32      maxLBA;                     // Total sectors in HDD
	char        serial[20];                 // serial numberf
	char        firmware[8];                // firmware version
	char        model[40];                  // Model number
	Boolean	    supports48bitAddFeatureSet;
} IDE_DIAG_IDENTIFY_RESULT;

typedef struct
{
	Uint8       num_valid_partition;
	Boolean     valid_partition[4];
	PTABLE      ptable;
} IDE_DIAG_READ_MBR_RESULT;

typedef enum
{
	MEDIA_EXIST             = 0x00,
	MEDIA_NO_MEDIA          = 0x02,
	MEDIA_READING           = 0x04,
	MEDIA_CHANGE_REQUEST    = 0x08,
	MEDIA_CHANGED           = 0x20,
	MEDIA_WRITE_PROTECTED   = 0x40,
	MEDIA_UNREMOVALBE       = 0x100,
	MEDIA_IO_TIMEOUT        = 0x200,
} MediaSatus;

int ide_io (Uint8 driveno,Uint32 sector,void *buffer,Uint16 count,Uint8 reading, void *ctxt);
int ide_exec (Uint8 driveno,Uint32 sector,void *buffer,Uint16 count,Uint8 reading, Boolean use_cache);
int ide_open (Uint8 driveID, Uint8 partno, Boolean force_format, Uint8 *driveno);
int ide_close (Uint8 driveno);
int ide_invalidate(Uint8 driveID);

Uint32 ide_set_multiple(Uint8 driveID, Uint32 multiple);

Uint32 IDE_SendCmd (IDE_Cmd *cmd);
Uint32 IDE_Send(IDE_Cmd *cmd);

void UtilSwap16(Uint16 *to, Uint16 *from);
void UtilSwap32(Uint32 *to, Uint32 *from);
void UtilThrough16(Uint16 *to, Uint16 *from);
void UtilThrough32(Uint32 *to, Uint32 *from);

Int32 ide_init_drive(Uint8 driveID, Uint32 *part_count);
Int32 ide_reset_MBR(Uint8 driveID);
Int32 ide_init_media(Uint8 driveID);
Int32 ide_inquire_media(Uint8 driveID);
Int32 ide_device_version(Uint8 driveID, char **firmware_version);
Int32 ide_command_identify (Uint8 driveID, Uint8 * address);
Int32 ide_command_seek (Uint8 driveID);
Int32 ide_command_media(Uint8 driveID);
Int32 ide_command_enable_msns(Uint8 driveID);
Int32 ide_command_check_power_mode(Uint8 driveID, Uint8* mode);
Int32 ide_command_idle_immediate(Uint8 driveID);
Int32 ide_command_standby_immediate(Uint8 driveID);
Int32 readMBR(logical_drive_desc *desc, Uint8 diskno, Uint32 addr, Uint32 *partition_count);
// In the case where no valid MBR is found this function can be used
// to create a default MBR with 1 primary partition spanning the entire
// HDD
Int32 writeMBR(Uint8 diskno, Uint32 addr, Uint32 disk_lba_size);

// Functions to be called by diagnostics only
Int32 ide_diag_identify(Uint8 driveID, IDE_DIAG_IDENTIFY_RESULT *result); // driveID = 2
Int32 ide_diag_read_mbr(Uint8 diskno, IDE_DIAG_READ_MBR_RESULT *result);  // diskno = 2
Int32 ide_diag_write_mbr(Uint8 diskno, Uint32 partition_size);            // diskno = 2 partition_size is maxLBA from identify
Int32 ide_diag_open(Uint8 driveID, Uint8 partno, Boolean force_format, Uint8 *driveno); // driveID = 2 partno = 0 force_format = TRUE
Int32 ide_diag_close(Uint8 driveno);// driveno is from ide_open
Int32 ide_diag_read_sectors(Uint8 driveno, Uint32 sector, void *buffer, Uint16 count); // driveno is from ide_open
Int32 ide_diag_write_sectors(Uint8 driveno, Uint32 sector, void *buffer, Uint16 count); // driveno is from ide_open

// modify for flash card hot plug
Boolean ide_is_removable(Uint8 driveID);
Int32 ide_command_write_sectors(
								 Uint8 * address, 	// Source address for the data
								 Uint16 disk, 			// Disk number (0 or 1)
								 Uint32 blockno, 		// Block number to read
								 Uint16 nblocks,			// Number of blocks (legal range: 1-256)
								 Boolean use_cache
								 );
Int32 ide_change_status(Uint8 driveID, IDE_STATE status);
#endif

⌨️ 快捷键说明

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