vaseboot/include/VasEBoot/ata.h

226 lines
6.2 KiB
C

/* ata.h - ATA disk access. */
/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
*
* VAS_EBOOT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VAS_EBOOT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef VAS_EBOOT_ATA_HEADER
#define VAS_EBOOT_ATA_HEADER 1
#include <VasEBoot/misc.h>
#include <VasEBoot/symbol.h>
#include <VasEBoot/disk.h>
/* XXX: For now this only works on i386. */
#include <VasEBoot/cpu/io.h>
typedef enum
{
VAS_EBOOT_ATA_CHS,
VAS_EBOOT_ATA_LBA,
VAS_EBOOT_ATA_LBA48
} VasEBoot_ata_addressing_t;
#define VAS_EBOOT_ATA_CH0_PORT1 0x1f0
#define VAS_EBOOT_ATA_CH1_PORT1 0x170
#define VAS_EBOOT_ATA_CH0_PORT2 0x3f6
#define VAS_EBOOT_ATA_CH1_PORT2 0x376
#define VAS_EBOOT_ATA_REG_DATA 0
#define VAS_EBOOT_ATA_REG_ERROR 1
#define VAS_EBOOT_ATA_REG_FEATURES 1
#define VAS_EBOOT_ATA_REG_SECTORS 2
#define VAS_EBOOT_ATAPI_REG_IREASON 2
#define VAS_EBOOT_ATA_REG_SECTNUM 3
#define VAS_EBOOT_ATA_REG_CYLLSB 4
#define VAS_EBOOT_ATA_REG_CYLMSB 5
#define VAS_EBOOT_ATA_REG_LBALOW 3
#define VAS_EBOOT_ATA_REG_LBAMID 4
#define VAS_EBOOT_ATAPI_REG_CNTLOW 4
#define VAS_EBOOT_ATA_REG_LBAHIGH 5
#define VAS_EBOOT_ATAPI_REG_CNTHIGH 5
#define VAS_EBOOT_ATA_REG_DISK 6
#define VAS_EBOOT_ATA_REG_CMD 7
#define VAS_EBOOT_ATA_REG_STATUS 7
#define VAS_EBOOT_ATA_REG2_CONTROL 0
#define VAS_EBOOT_ATA_STATUS_ERR 0x01
#define VAS_EBOOT_ATA_STATUS_INDEX 0x02
#define VAS_EBOOT_ATA_STATUS_ECC 0x04
#define VAS_EBOOT_ATA_STATUS_DRQ 0x08
#define VAS_EBOOT_ATA_STATUS_SEEK 0x10
#define VAS_EBOOT_ATA_STATUS_WRERR 0x20
#define VAS_EBOOT_ATA_STATUS_READY 0x40
#define VAS_EBOOT_ATA_STATUS_BUSY 0x80
/* ATAPI interrupt reason values (I/O, D/C bits). */
#define VAS_EBOOT_ATAPI_IREASON_MASK 0x3
#define VAS_EBOOT_ATAPI_IREASON_DATA_OUT 0x0
#define VAS_EBOOT_ATAPI_IREASON_CMD_OUT 0x1
#define VAS_EBOOT_ATAPI_IREASON_DATA_IN 0x2
#define VAS_EBOOT_ATAPI_IREASON_ERROR 0x3
enum VasEBoot_ata_commands
{
VAS_EBOOT_ATA_CMD_CHECK_POWER_MODE = 0xe5,
VAS_EBOOT_ATA_CMD_IDENTIFY_DEVICE = 0xec,
VAS_EBOOT_ATA_CMD_IDENTIFY_PACKET_DEVICE = 0xa1,
VAS_EBOOT_ATA_CMD_IDLE = 0xe3,
VAS_EBOOT_ATA_CMD_PACKET = 0xa0,
VAS_EBOOT_ATA_CMD_READ_SECTORS = 0x20,
VAS_EBOOT_ATA_CMD_READ_SECTORS_EXT = 0x24,
VAS_EBOOT_ATA_CMD_READ_SECTORS_DMA = 0xc8,
VAS_EBOOT_ATA_CMD_READ_SECTORS_DMA_EXT = 0x25,
VAS_EBOOT_ATA_CMD_SECURITY_FREEZE_LOCK = 0xf5,
VAS_EBOOT_ATA_CMD_SET_FEATURES = 0xef,
VAS_EBOOT_ATA_CMD_SLEEP = 0xe6,
VAS_EBOOT_ATA_CMD_SMART = 0xb0,
VAS_EBOOT_ATA_CMD_STANDBY_IMMEDIATE = 0xe0,
VAS_EBOOT_ATA_CMD_WRITE_SECTORS = 0x30,
VAS_EBOOT_ATA_CMD_WRITE_SECTORS_EXT = 0x34,
VAS_EBOOT_ATA_CMD_WRITE_SECTORS_DMA_EXT = 0x35,
VAS_EBOOT_ATA_CMD_WRITE_SECTORS_DMA = 0xca,
};
enum VasEBoot_ata_timeout_milliseconds
{
VAS_EBOOT_ATA_TOUT_STD = 1000, /* 1s standard timeout. */
VAS_EBOOT_ATA_TOUT_DATA = 10000, /* 10s DATA I/O timeout. */
VAS_EBOOT_ATA_TOUT_SPINUP = 10000, /* Give the device 10s on first try to spinon. */
};
typedef union
{
VasEBoot_uint8_t raw[11];
struct
{
union
{
VasEBoot_uint8_t features;
VasEBoot_uint8_t error;
};
union
{
VasEBoot_uint8_t sectors;
VasEBoot_uint8_t atapi_ireason;
};
union
{
VasEBoot_uint8_t lba_low;
VasEBoot_uint8_t sectnum;
};
union
{
VasEBoot_uint8_t lba_mid;
VasEBoot_uint8_t cyllsb;
VasEBoot_uint8_t atapi_cntlow;
};
union
{
VasEBoot_uint8_t lba_high;
VasEBoot_uint8_t cylmsb;
VasEBoot_uint8_t atapi_cnthigh;
};
VasEBoot_uint8_t disk;
union
{
VasEBoot_uint8_t cmd;
VasEBoot_uint8_t status;
};
VasEBoot_uint8_t sectors48;
VasEBoot_uint8_t lba48_low;
VasEBoot_uint8_t lba48_mid;
VasEBoot_uint8_t lba48_high;
};
} VasEBoot_ata_regs_t;
/* ATA pass through parameters and function. */
struct VasEBoot_disk_ata_pass_through_parms
{
VasEBoot_ata_regs_t taskfile;
void * buffer;
VasEBoot_size_t size;
int write;
void *cmd;
int cmdsize;
int dma;
};
struct VasEBoot_ata
{
/* Addressing methods available for accessing this device. If CHS
is only available, use that. Otherwise use LBA, except for the
high sectors. In that case use LBA48. */
VasEBoot_ata_addressing_t addr;
/* Sector count. */
VasEBoot_uint64_t size;
VasEBoot_uint32_t log_sector_size;
/* CHS maximums. */
VasEBoot_uint16_t cylinders;
VasEBoot_uint16_t heads;
VasEBoot_uint16_t sectors_per_track;
/* Set to 0 for ATA, set to 1 for ATAPI. */
int atapi;
int dma;
VasEBoot_size_t maxbuffer;
int *present;
void *data;
struct VasEBoot_ata_dev *dev;
};
typedef struct VasEBoot_ata *VasEBoot_ata_t;
typedef int (*VasEBoot_ata_dev_iterate_hook_t) (int id, int bus, void *data);
struct VasEBoot_ata_dev
{
/* Call HOOK with each device name, until HOOK returns non-zero. */
int (*iterate) (VasEBoot_ata_dev_iterate_hook_t hook, void *hook_data,
VasEBoot_disk_pull_t pull);
/* Open the device named NAME, and set up SCSI. */
VasEBoot_err_t (*open) (int id, int bus, struct VasEBoot_ata *scsi);
/* Close the scsi device SCSI. */
void (*close) (struct VasEBoot_ata *ata);
/* Read SIZE bytes from the device SCSI into BUF after sending the
command CMD of size CMDSIZE. */
VasEBoot_err_t (*readwrite) (struct VasEBoot_ata *ata,
struct VasEBoot_disk_ata_pass_through_parms *parms,
int spinup);
/* The next scsi device. */
struct VasEBoot_ata_dev *next;
};
typedef struct VasEBoot_ata_dev *VasEBoot_ata_dev_t;
void VasEBoot_ata_dev_register (VasEBoot_ata_dev_t dev);
void VasEBoot_ata_dev_unregister (VasEBoot_ata_dev_t dev);
#endif /* ! VAS_EBOOT_ATA_HEADER */