Backport from Etherboot-5.1.8. Has problems when probing for second disk.

This commit is contained in:
Greg Watson 2003-06-06 20:11:11 +00:00
parent adea9f37a8
commit 1c8cce6646

View file

@ -1,6 +1,13 @@
#define BSY_SET_DURING_SPINUP 1
/*
* UBL, The Universal Talkware Boot Loader
* Copyright (C) 2000 Universal Talkware Inc.
* Copyright (C) 2002 Eric Biederman
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -11,521 +18,497 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Note: Parts of this code grew out of the Openbios effort that was
* abandoned. Without the info that we were able to learn from
* OpenBios this program would have never worked. We are forever
* grateful for those who came before us.
*
* This code can be retrieved in a machine/human readable form at:
*
* http://www.talkware.net/GPL/UBL
*
* Anyone making changes to this code please send them to us so we
* can include them in the standard release.
*
*
* $Id$
*/
#include <arch/io.h>
#include <printk.h>
#include <string.h>
#include <timer.h>
#include <pci.h>
#include <pc80/ide.h>
#include <arch/io.h>
static __inline__ int wait_for_notbusy(unsigned base)
struct controller controller;
struct harddisk_info harddisk_info[NUM_HD];
static int await_ide(int (*done)(struct controller *ctrl),
struct controller *ctrl, unsigned long timeout)
{
unsigned i = 0;
do {
if (((inb_p(IDE_REG_ERROR(base)) & 0x80) != 0x80) &&
((inb_p(IDE_REG_STATUS(base)) & 0x80) != 0x80))
{
int result;
for(;;) {
result = done(ctrl);
if (result) {
return 0;
}
i++;
} while (i != 0);
return 1;
}
static __inline__ int wait_for_dataready(unsigned base)
{
unsigned i = 0;
do {
if (((inb_p(IDE_REG_ERROR(base)) & 0x80) != 0x80) &&
((inb_p(IDE_REG_STATUS(base)) & 0x88) != 0x88))
{
return 0;
if ((timeout == 0) || (ticks_since_boot() > timeout)) {
break;
}
i++;
} while (i != 0);
return 1;
udelay(1000); /* Added to avoid spinning GRW */
}
return -1;
}
static __inline__ int write_command(
unsigned base,
ide_command_t command,
ide_cmd_param_t * params)
/* The maximum time any IDE command can last 31 seconds,
* So if any IDE commands takes this long we know we have problems.
*/
#define IDE_TIMEOUT (32*get_hz())
static int not_bsy(struct controller *ctrl)
{
return !(inb(IDE_REG_STATUS(ctrl)) & IDE_STATUS_BSY);
}
#if !BSY_SET_DURING_SPINUP
static int timeout(struct controller *ctrl)
{
if (wait_for_notbusy(base) != 0)
return 1;
outb_p(params->precomp, IDE_REG_PRECOMP(base));
outb_p(params->sector_count, IDE_REG_SECTOR_COUNT(base));
outb_p(params->sector_number, IDE_REG_SECTOR_NUMBER(base));
outb_p(params->cylinder & 0xFF, IDE_REG_CYLINDER_LSB(base));
outb_p((params->cylinder >> 8) & 0x03, IDE_REG_CYLINDER_MSB(base));
outb_p(params->drivehead, IDE_REG_DRIVEHEAD(base));
if (wait_for_notbusy(base) != 0)
return 1;
outb_p(command, IDE_REG_COMMAND(base));
return 0;
}
#endif
int ide_shutdown(void)
static int ide_software_reset(struct controller *ctrl)
{
outb_p(IDE_CMD_STANDBY_IMMEDIATE, IDE_REG_COMMAND(IDE_BASE1));
outb_p(IDE_CMD_STANDBY_IMMEDIATE2, IDE_REG_COMMAND(IDE_BASE1));
return 0;
}
int ide_read_data(unsigned base, void * buf, size_t size)
{
register unsigned short * ptr = (unsigned short *) buf;
if (wait_for_dataready(base)) {
printk_info("data not ready...\n");
return 1;
}
while (size > 1) {
*ptr++ = inw_p(IDE_REG_DATA(base));
size -= sizeof(unsigned short);
}
return 0;
}
int ide_write_data(unsigned base, void * buf, size_t size)
{
register unsigned short * ptr = (unsigned short *) buf;
if (wait_for_dataready(base))
return 1;
while (size > 1) {
outw_p(*ptr, IDE_REG_DATA(base));
ptr++;
size -= sizeof(unsigned short);
}
return 0;
}
harddisk_info_t harddisk_info[NUM_HD];
static char buffer[512];
static int init_drive(unsigned base, int driveno)
{
volatile int delay;
ide_cmd_param_t cmd = IDE_DEFAULT_COMMAND;
unsigned char command_val;
unsigned short* drive_info;
harddisk_info[driveno].controller_port = base;
harddisk_info[driveno].num_heads = 0u;
harddisk_info[driveno].num_cylinders = 0u;
harddisk_info[driveno].num_sectors_per_track = 0u;
harddisk_info[driveno].num_sectors = 0ul;
harddisk_info[driveno].address_mode = IDE_DH_CHS;
harddisk_info[driveno].drive_exists = 0;
cmd.drivehead = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS |
IDE_DH_DRIVE(driveno);
write_command(base, IDE_CMD_GET_INFO, &cmd);
if ((inb_p(IDE_REG_STATUS(base)) & 1) != 0) {
/* Well, if that command didn't work, we probably don't have drive. */
printk_info("Drive %d: detect FAILED\n", driveno);
return 1;
}
ide_read_data(base, buffer, IDE_SECTOR_SIZE);
/* Now suck the data out */
drive_info = (unsigned short*)buffer;
harddisk_info[driveno].num_heads = drive_info[3];
harddisk_info[driveno].num_cylinders = drive_info[1];
harddisk_info[driveno].num_sectors_per_track = drive_info[6];
harddisk_info[driveno].num_sectors = *((unsigned int*)&(drive_info[60]));
memcpy(harddisk_info[driveno].model_number, ((unsigned short*)&(drive_info[27])), 40);
harddisk_info[driveno].drive_exists = 1;
printk_info("%s \n", harddisk_info[driveno].model_number);
printk_info("%s: sectors_per_track=[%d], num_heads=[%d], num_cylinders=[%d]\n",
__FUNCTION__,
harddisk_info[driveno].num_sectors_per_track,
harddisk_info[driveno].num_heads,
harddisk_info[driveno].num_cylinders);
#define HD harddisk_info[driveno]
if(drive_info[49] != 0) {
printk_info("IDE%d %d/%d/%d cap: %04x\n",
(int)driveno,
(int)HD.num_heads, (int)HD.num_cylinders,
(int)HD.num_sectors_per_track,
(int) drive_info[49]);
}
if (drive_info[49] & 0x200) { /* bit 9 of capability word is lba supported bit */
harddisk_info[driveno].address_mode = IDE_DH_LBA;
} else {
harddisk_info[driveno].address_mode = IDE_DH_CHS;
}
// harddisk_info[driveno].address_mode = IDE_DH_CHS;
/* Set up the Extended control register */
if (harddisk_info[driveno].num_heads > 8) {
command_val = 0x0A;
} else {
command_val = 0x02;
}
outb_p(command_val, IDE_REG_CONTROL(base));
/* Execute the drive diagnostics command */
write_command(base, IDE_CMD_DRIVE_DIAG, &cmd);
if ((inb_p(IDE_REG_STATUS(base)) & 1) != 0) {
return 1;
}
/* Reset the bus (again) */
outb_p(cmd.drivehead, IDE_REG_DRIVEHEAD(base));
outb_p(0x04, IDE_REG_CONTROL(base));
for (delay = 0x100; delay > 0; --delay);
outb_p(command_val, IDE_REG_CONTROL(base));
/* Now do a drive recalibrate */
write_command(base, IDE_CMD_RECALIBRATE, &cmd);
if ((inb_p(IDE_REG_STATUS(base)) & 1) != 0) {
return 1;
}
(void)wait_for_notbusy(base);
/* Set device parameters */
cmd.sector_count = harddisk_info[driveno].num_sectors_per_track;
cmd.drivehead = IDE_DH_DEFAULT |
IDE_DH_HEAD(harddisk_info[driveno].num_heads) |
IDE_DH_DRIVE(driveno) |
harddisk_info[driveno].address_mode;
write_command(base, IDE_CMD_SET_PARAMS, &cmd);
/* Set multiple mode */
cmd.sector_count = 0x10; /* Single-word DMA, mode 0 */
cmd.drivehead = IDE_DH_DEFAULT |
IDE_DH_HEAD(0) |
IDE_DH_DRIVE(driveno) |
harddisk_info[driveno].address_mode;
write_command(base, IDE_CMD_SET_MULTIMODE, &cmd);
/* Make sure command is still OK */
outb_p(command_val, IDE_REG_CONTROL(base));
/* Set parameters _again_ */
cmd.sector_count = harddisk_info[driveno].num_sectors_per_track;
cmd.drivehead = IDE_DH_DEFAULT |
IDE_DH_HEAD(harddisk_info[driveno].num_heads) |
IDE_DH_DRIVE(driveno) |
harddisk_info[driveno].address_mode;
write_command(base, IDE_CMD_SET_PARAMS, &cmd);
/* Make sure command is still OK */
outb_p(command_val, IDE_REG_CONTROL(base));
#if 0
/* Exercise the drive to see if it works OK */
printk_info("Exercising HardDisk- buffer=0x%08lX\n", (unsigned long) buffer);
outb_p(0x42, 0xeb);
while (1) {
for (idx = 0; idx < harddisk_info[driveno].num_sectors; ++idx) {
outb_p(idx & 0xFF, 0x80);
retval = ide_read_sector(driveno, buffer, idx);
if (retval != 0) {
printk_info("readsector(driveno=%d, sector=%lu) returned %d!\n",
driveno, (unsigned long) idx, retval);
}
}
printk_info("Exercise complete!\n");
outb(0x42, 0xeb);
}
#endif /* 0 */
return 0;
}
static int init_controller(unsigned base, int basedrive) {
volatile int delay;
/* First, check to see if the controller even exists */
outb_p(0x5, IDE_REG_SECTOR_COUNT(base));
if (inb_p(IDE_REG_SECTOR_COUNT(base)) != 0x5) {
printk_info("Controller %d: detect FAILED (1)\n", basedrive / 2);
/* Wait a little bit in case this is immediately after
* hardware reset.
*/
udelay(2000);
/* A software reset should not be delivered while the bsy bit
* is set. If the bsy bit does not clear in a reasonable
* amount of time give up.
*/
if (await_ide(not_bsy, ctrl, ticks_since_boot() + IDE_TIMEOUT) < 0) {
return -1;
}
outb_p(0xA, IDE_REG_SECTOR_COUNT(base));
if (inb_p(IDE_REG_SECTOR_COUNT(base)) != 0xA) {
printk_info("Controller %d: detect FAILED (2)\n", basedrive / 2);
return -2;
/* Disable Interrupts and reset the ide bus */
outb(IDE_CTRL_HD15 | IDE_CTRL_SRST | IDE_CTRL_NIEN,
IDE_REG_DEVICE_CONTROL(ctrl));
udelay(5);
outb(IDE_CTRL_HD15 | IDE_CTRL_NIEN, IDE_REG_DEVICE_CONTROL(ctrl));
udelay(2000);
if (await_ide(not_bsy, ctrl, ticks_since_boot() + IDE_TIMEOUT) < 0) {
return -1;
}
/* Reset the system */
outb_p(0x4, IDE_REG_CONTROL(base));
for (delay = 0x100; delay > 0; --delay);
outb_p(0x0, IDE_REG_CONTROL(base));
/* Now initialize the individual drives */
init_drive(base, basedrive);
init_drive(base, basedrive+1);
return 0;
}
static void pio_set_registers(
struct controller *ctrl, const struct ide_pio_command *cmd)
{
uint8_t device;
/* Disable Interrupts */
outb(IDE_CTRL_HD15 | IDE_CTRL_NIEN, IDE_REG_DEVICE_CONTROL(ctrl));
/* Possibly switch selected device */
device = inb(IDE_REG_DEVICE(ctrl));
outb(cmd->device, IDE_REG_DEVICE(ctrl));
if ((device & (1UL << 4)) != (cmd->device & (1UL << 4))) {
/* Allow time for the selected drive to switch,
* The linux ide code suggests 50ms is the right
* amount of time to use here.
*/
udelay(50000);
}
outb(cmd->feature, IDE_REG_FEATURE(ctrl));
outb(cmd->sector_count2, IDE_REG_SECTOR_COUNT(ctrl));
outb(cmd->sector_count, IDE_REG_SECTOR_COUNT(ctrl));
outb(cmd->lba_low2, IDE_REG_LBA_LOW(ctrl));
outb(cmd->lba_low, IDE_REG_LBA_LOW(ctrl));
outb(cmd->lba_mid2, IDE_REG_LBA_MID(ctrl));
outb(cmd->lba_mid, IDE_REG_LBA_MID(ctrl));
outb(cmd->lba_high2, IDE_REG_LBA_HIGH(ctrl));
outb(cmd->lba_high, IDE_REG_LBA_HIGH(ctrl));
outb(cmd->command, IDE_REG_COMMAND(ctrl));
}
static int pio_non_data(struct controller *ctrl, const struct ide_pio_command *cmd)
{
/* Wait until the busy bit is clear */
if (await_ide(not_bsy, ctrl, ticks_since_boot() + IDE_TIMEOUT) < 0) {
return -1;
}
pio_set_registers(ctrl, cmd);
if (await_ide(not_bsy, ctrl, ticks_since_boot() + IDE_TIMEOUT) < 0) {
return -1;
}
/* FIXME is there more error checking I could do here? */
return 0;
}
static int pio_data_in(struct controller *ctrl, const struct ide_pio_command *cmd,
void *buffer, size_t bytes)
{
unsigned int status;
/* FIXME handle commands with multiple blocks */
/* Wait until the busy bit is clear */
if (await_ide(not_bsy, ctrl, ticks_since_boot() + IDE_TIMEOUT) < 0) {
return -1;
}
/* How do I tell if INTRQ is asserted? */
pio_set_registers(ctrl, cmd);
if (await_ide(not_bsy, ctrl, ticks_since_boot() + IDE_TIMEOUT) < 0) {
return -1;
}
status = inb(IDE_REG_STATUS(ctrl));
if (!(status & IDE_STATUS_DRQ)) {
return -1;
}
insw(IDE_REG_DATA(ctrl), buffer, bytes/2);
status = inb(IDE_REG_STATUS(ctrl));
if (status & IDE_STATUS_ERR) {
return -1;
}
return 0;
}
static inline int ide_read_sector_chs(
struct harddisk_info *info, void *buffer, unsigned long sector)
{
struct ide_pio_command cmd;
unsigned int track;
unsigned int offset;
unsigned int cylinder;
memset(&cmd, 0, sizeof(cmd));
cmd.sector_count = 1;
track = sector / info->sectors_per_track;
/* Sector number */
offset = 1 + (sector % info->sectors_per_track);
cylinder = track / info->heads;
cmd.lba_low = offset;
cmd.lba_mid = cylinder & 0xff;
cmd.lba_high = (cylinder >> 8) & 0xff;
cmd.device = IDE_DH_DEFAULT |
IDE_DH_HEAD(track % info->heads) |
info->slave |
IDE_DH_CHS;
cmd.command = IDE_CMD_READ_SECTORS;
return pio_data_in(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
}
static inline int ide_read_sector_lba(
struct harddisk_info *info, void *buffer, unsigned long sector)
{
struct ide_pio_command cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.sector_count = 1;
cmd.lba_low = sector & 0xff;
cmd.lba_mid = (sector >> 8) & 0xff;
cmd.lba_high = (sector >> 16) & 0xff;
cmd.device = IDE_DH_DEFAULT |
((sector >> 24) & 0x0f) |
info->slave |
IDE_DH_LBA;
cmd.command = IDE_CMD_READ_SECTORS;
return pio_data_in(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
}
static inline int ide_read_sector_lba48(
struct harddisk_info *info, void *buffer, sector_t sector)
{
struct ide_pio_command cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.sector_count = 1;
cmd.lba_low = sector & 0xff;
cmd.lba_mid = (sector >> 8) & 0xff;
cmd.lba_high = (sector >> 16) & 0xff;
cmd.lba_low2 = (sector >> 24) & 0xff;
cmd.lba_mid2 = (sector >> 32) & 0xff;
cmd.lba_high2 = (sector >> 40) & 0xff;
cmd.device = info->slave | IDE_DH_LBA;
cmd.command = IDE_CMD_READ_SECTORS_EXT;
return pio_data_in(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
}
int ide_read_sector(int driveno, void * buf, unsigned int sector,
int byte_offset, int n_bytes)
{
struct harddisk_info *info = &harddisk_info[driveno];
int result;
/* Report the buffer is empty */
if (sector > info->sectors) {
return -1;
}
if (info->address_mode == ADDRESS_MODE_CHS) {
result = ide_read_sector_chs(info, buf, sector);
}
else if (info->address_mode == ADDRESS_MODE_LBA) {
result = ide_read_sector_lba(info, buf, sector);
}
else if (info->address_mode == ADDRESS_MODE_LBA48) {
result = ide_read_sector_lba48(info, buf, sector);
}
else {
result = -1;
}
return result;
}
static int init_drive(struct harddisk_info *info, struct controller *ctrl, int slave, int basedrive)
{
uint16_t* drive_info;
struct ide_pio_command cmd;
unsigned char disk_buffer[DISK_BUFFER_SIZE];
int i;
info->ctrl = ctrl;
info->heads = 0u;
info->cylinders = 0u;
info->sectors_per_track = 0u;
info->address_mode = IDE_DH_CHS;
info->sectors = 0ul;
info->drive_exists = 0;
info->slave_absent = 0;
info->slave = slave?IDE_DH_SLAVE: IDE_DH_MASTER;
info->basedrive = basedrive;
printk_info("Testing for disk %d\n", info->basedrive);
/* Select the drive that we are testing */
outb(IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | info->slave,
IDE_REG_DEVICE(ctrl));
udelay(50000);
/* Test to see if the drive registers exist,
* In many cases this quickly rules out a missing drive.
*/
for(i = 0; i < 4; i++) {
outb(0xaa + i, (ctrl->cmd_base) + 2 + i);
}
for(i = 0; i < 4; i++) {
if (inb((ctrl->cmd_base) + 2 + i) != 0xaa + i) {
return 1;
}
}
for(i = 0; i < 4; i++) {
outb(0x55 + i, (ctrl->cmd_base) + 2 + i);
}
for(i = 0; i < 4; i++) {
if (inb((ctrl->cmd_base) + 2 + i) != 0x55 + i) {
return 1;
}
}
printk_info("Probing for disk %d\n", info->basedrive);
memset(&cmd, 0, sizeof(cmd));
cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | info->slave;
cmd.command = IDE_CMD_IDENTIFY_DEVICE;
if (pio_data_in(ctrl, &cmd, disk_buffer, IDE_SECTOR_SIZE) < 0) {
/* Well, if that command didn't work, we probably don't have drive. */
return 1;
}
/* Now suck the data out */
drive_info = (uint16_t *)disk_buffer;
if (drive_info[2] == 0x37C8) {
/* If the response is incomplete spin up the drive... */
memset(&cmd, 0, sizeof(cmd));
cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS |
info->slave;
cmd.feature = IDE_FEATURE_STANDBY_SPINUP_DRIVE;
if (pio_non_data(ctrl, &cmd) < 0) {
/* If the command doesn't work give up on the drive */
return 1;
}
}
if ((drive_info[2] == 0x37C8) || (drive_info[2] == 0x8C73)) {
/* The response is incomplete retry the drive info command */
memset(&cmd, 0, sizeof(cmd));
cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS |
info->slave;
cmd.command = IDE_CMD_IDENTIFY_DEVICE;
if(pio_data_in(ctrl, &cmd, disk_buffer, IDE_SECTOR_SIZE) < 0) {
/* If the command didn't work give up on the drive. */
return 1;
}
}
if ((drive_info[2] != 0x37C8) &&
(drive_info[2] != 0x738C) &&
(drive_info[2] != 0x8C73) &&
(drive_info[2] != 0xC837) &&
(drive_info[2] != 0x0000)) {
printk_info("Invalid IDE Configuration: %hx\n", drive_info[2]);
return 1;
}
for(i = 27; i < 47; i++) {
info->model_number[((i-27)<< 1)] = (drive_info[i] >> 8) & 0xff;
info->model_number[((i-27)<< 1)+1] = drive_info[i] & 0xff;
}
info->model_number[40] = '\0';
info->drive_exists = 1;
/* See if LBA is supported */
if (drive_info[49] & (1 << 9)) {
info->address_mode = ADDRESS_MODE_LBA;
info->sectors = (drive_info[61] << 16) | (drive_info[60]);
/* Enable LBA48 mode if it is present */
if (drive_info[83] & (1 <<10)) {
/* Should LBA48 depend on LBA? */
printk_info("LBA48 mode\n");
info->address_mode = ADDRESS_MODE_LBA48;
info->sectors =
(((sector_t)drive_info[103]) << 48) |
(((sector_t)drive_info[102]) << 32) |
(((sector_t)drive_info[101]) << 16) |
(((sector_t)drive_info[100]) << 0);
}
} else {
info->address_mode = ADDRESS_MODE_CHS;
info->heads = drive_info[3];
info->cylinders = drive_info[1];
info->sectors_per_track = drive_info[6];
info->sectors =
info->sectors_per_track *
info->heads *
info->cylinders;
printk_info("%s sectors_per_track=[%d], heads=[%d], cylinders=[%d]\n",
__FUNCTION__,
info->sectors_per_track,
info->heads,
info->cylinders);
}
/* See if we have a slave */
if (!info->slave && (((drive_info[93] >> 14) & 3) == 1)) {
info->slave_absent = !(drive_info[93] & (1 << 5));
}
/* See if we need to put the device in CFA power mode 1 */
if ((drive_info[160] & ((1 << 15) | (1 << 13)| (1 << 12))) ==
((1 << 15) | (1 << 13)| (1 << 12))) {
memset(&cmd, 0, sizeof(cmd));
cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | info->slave;
cmd.feature = IDE_FEATURE_CFA_ENABLE_POWER_MODE1;
if (pio_non_data(ctrl, &cmd) < 0) {
/* If I need to power up the drive, and I can't
* give up.
*/
printk_info("Cannot power up CFA device\n");
return 1;
}
}
printk_info("disk%d %dk cap: %hx\n",
info->basedrive,
(unsigned long)(info->sectors >> 1),
drive_info[49]);
return 0;
}
static int init_controller(struct controller *ctrl, int basedrive)
{
struct harddisk_info *info;
/* Put the drives ide channel in a know state and wait
* for the drives to spinup.
*
* In practice IDE disks tend not to respond to commands until
* they have spun up. This makes IDE hard to deal with
* immediately after power up, as the delays can be quite
* long, so we must be very careful here.
*
* There are two pathological cases that must be dealt with:
*
* - The BSY bit not being set while the IDE drives spin up.
* In this cases only a hard coded delay will work. As
* I have not reproduced it, and this is out of spec for
* IDE drives the work around can be enabled by setting
* BSY_SET_DURING_SPINUP to 0.
*
* - The BSY bit floats high when no drives are plugged in.
* This case will not be detected except by timing out but
* we avoid the problems by only probing devices we are
* supposed to boot from. If we don't do the probe we
* will not experience the problem.
*
* So speed wise I am only slow if the BSY bit is not set
* or not reported by the IDE controller during spinup, which
* is quite rare.
*
*/
#if !BSY_SET_DURING_SPINUP
if (await_ide(timeout, ctrl, IDE_TIMEOUT) < 0) {
return -1;
}
#endif
if (ide_software_reset(ctrl) < 0) {
return -1;
}
/* Note: I have just done a software reset. It may be
* reasonable to just read the boot time signatures
* off of the drives to see if they are present.
*
* For now I will go with just sending commands to the drives
* and assuming filtering out missing drives by detecting registers
* that won't set and commands that fail to execute properly.
*/
/* Now initialize the individual drives */
info = &harddisk_info[basedrive];
init_drive(info, ctrl, 0, basedrive & 1);
/* at the moment this only works for the first drive */
#if 0
if (info->drive_exists && !info->slave_absent) {
basedrive++;
info++;
init_drive(info, ctrl, 1, basedrive & 1);
}
#endif
return 0;
}
int ide_init(void)
{
outb_p(0x42, 0xEB);
printk_info ("I am now initializing the ide system\n");
struct harddisk_info *info;
int index;
int drives = 0;
if (init_controller(IDE_BASE1, 0) < 0) {
printk_info ("Initializing the main controller failed!\n");
/* error return error */
return -1;
};
#if (NUM_HD > 3)
init_controller(IDE_BASE2, 2);
#endif
#if (NUM_HD > 5)
init_controller(IDE_BASE3, 4);
#endif
#if (NUM_HD > 7)
init_controller(IDE_BASE4, 6);
#endif
return 0;
}
/* read a sector or a partial sector */
int ide_read_sector(int drive, void * buffer, unsigned int block, int byte_offset,
int n_bytes) {
ide_cmd_param_t cmd = IDE_DEFAULT_COMMAND;
unsigned base;
unsigned char sect_buffer[IDE_SECTOR_SIZE];
unsigned int track;
int status;
int address_mode = harddisk_info[drive].address_mode;
//int i;
printk_info("%s: drive[%d], buffer[%08x], block[%08x], offset[%d], n_bytes[%d]\n",
__FUNCTION__, drive, buffer, block, byte_offset, n_bytes);
printk_info("%s: block(%08x) to addr(%08x)\r", __FUNCTION__, block, (int)buffer);
if ((drive < 0) || (drive >= NUM_HD) ||
(harddisk_info[drive].drive_exists == 0))
{
printk_info("unknown drive\n");
return 1;
}
base = harddisk_info[drive].controller_port;
if (harddisk_info[drive].num_heads > 8) {
outb_p(0xA, IDE_REG_CONTROL(base));
} else {
outb_p(0x2, IDE_REG_CONTROL(base));
}
cmd.sector_count = 1;
if (address_mode == IDE_DH_CHS) {
track = block / harddisk_info[drive].num_sectors_per_track;
cmd.sector_number = 1+(block % harddisk_info[drive].num_sectors_per_track);
cmd.cylinder = track / harddisk_info[drive].num_heads;
cmd.drivehead = IDE_DH_DEFAULT |
IDE_DH_HEAD(track % harddisk_info[drive].num_heads) |
IDE_DH_DRIVE(drive) |
IDE_DH_CHS;
printk_info("%s: CHS: track=[%d], sector_number=[%d], cylinder=[%d]\n", __FUNCTION__, track, cmd.sector_number, cmd.cylinder);
/*
*/
} else {
#if 1
cmd.sector_number = block & 0xff; /* lower byte of block (lba) */
cmd.cylinder = (block >> 8) & 0xffff; /* middle 2 bytes of block (lba) */
cmd.drivehead = IDE_DH_DEFAULT | /* set bits that must be on */
((block >> 24) & 0x0f) | /* lower nibble of byte 3 of block */
IDE_DH_DRIVE(drive) |
IDE_DH_LBA;
#else
cmd.sector_number = (block >> 24) & 0xff; /* byte 0 of block (lba) */
cmd.cylinder = (block >> 8) & 0xffff; /* bytes 1 & 2 of block (lba) */
cmd.drivehead = IDE_DH_DEFAULT | /* set bits that must be on */
((block >> 4) & 0x0f) | /* upper nibble of byte 3 of block */
IDE_DH_DRIVE(drive) |
IDE_DH_LBA;
#endif
printk_info("%s: LBA: drivehead[%0x], cylinder[%04x], sector[%0x], block[%8x]\n",
__FUNCTION__, cmd.drivehead, cmd.cylinder, cmd.sector_number, block & 0x0fffffff);
/*
*/
}
write_command(base, IDE_CMD_READ_MULTI_RETRY, &cmd);
if ((inb_p(IDE_REG_STATUS(base)) & 1) != 0) {
printk_info("ide not ready...\n");
return 1;
}
if (n_bytes != IDE_SECTOR_SIZE) {
status = ide_read_data(base, sect_buffer, IDE_SECTOR_SIZE);
if (status == 0) {
memcpy(buffer, sect_buffer+byte_offset, n_bytes);
}
} else {
status = ide_read_data(base, buffer, IDE_SECTOR_SIZE);
}
return status;
}
#ifdef DANGER_IDE_WRITE
/* write a sector or a partial sector */
int ide_write_sector(int drive, void * buffer, unsigned int block ) {
ide_cmd_param_t cmd = IDE_DEFAULT_COMMAND;
unsigned base;
unsigned int track;
int status;
int address_mode = harddisk_info[drive].address_mode;
//int i;
printk_info("%s: drive[%d], buffer[%08x], block[%08x]\n",
__FUNCTION__, drive, buffer, block);
printk_info("%s: block(%08x) from addr(%08x)\r", __FUNCTION__, block, (int)buffer);
if ((drive < 0) || (drive >= NUM_HD) ||
(harddisk_info[drive].drive_exists == 0))
{
printk_info("unknown drive\n");
return 1;
}
base = harddisk_info[drive].controller_port;
if (harddisk_info[drive].num_heads > 8) {
outb_p(0xA, IDE_REG_CONTROL(base));
} else {
outb_p(0x2, IDE_REG_CONTROL(base));
}
cmd.sector_count = 1;
if (address_mode == IDE_DH_CHS) {
track = block / harddisk_info[drive].num_sectors_per_track;
cmd.sector_number = 1+(block % harddisk_info[drive].num_sectors_per_track);
cmd.cylinder = track / harddisk_info[drive].num_heads;
cmd.drivehead = IDE_DH_DEFAULT |
IDE_DH_HEAD(track % harddisk_info[drive].num_heads) |
IDE_DH_DRIVE(drive) |
IDE_DH_CHS;
printk_info("%s: CHS: track=[%d], sector_number=[%d], cylinder=[%d]\n", __FUNCTION__, track, cmd.sector_number, cmd.cylinder);
/*
*/
} else {
#if 1
cmd.sector_number = block & 0xff; /* lower byte of block (lba) */
cmd.cylinder = (block >> 8) & 0xffff; /* middle 2 bytes of block (lba) */
cmd.drivehead = IDE_DH_DEFAULT | /* set bits that must be on */
((block >> 24) & 0x0f) | /* lower nibble of byte 3 of block */
IDE_DH_DRIVE(drive) |
IDE_DH_LBA;
#else
cmd.sector_number = (block >> 24) & 0xff; /* byte 0 of block (lba) */
cmd.cylinder = (block >> 8) & 0xffff; /* bytes 1 & 2 of block (lba) */
cmd.drivehead = IDE_DH_DEFAULT | /* set bits that must be on */
((block >> 4) & 0x0f) | /* upper nibble of byte 3 of block */
IDE_DH_DRIVE(drive) |
IDE_DH_LBA;
#endif
printk_info("%s: LBA: drivehead[%0x], cylinder[%04x], sector[%0x], block[%8x]\n",
__FUNCTION__, cmd.drivehead, cmd.cylinder, cmd.sector_number, block & 0x0fffffff);
/*
*/
}
write_command(base, IDE_CMD_WRITE_MULTI_RETRY, &cmd);
if ((inb_p(IDE_REG_STATUS(base)) & 1) != 0) {
printk_info("ide not ready...\n");
return 1;
}
status = ide_write_data(base, buffer, IDE_SECTOR_SIZE);
return status;
}
#endif
/* Intialize the harddisk_info structures */
memset(harddisk_info, 0, sizeof(harddisk_info));
for(index = 0; index < 1; index++) {
#if 0
/* read a sector or a partial sector */
int ide_read_sector(int drive, void * buffer, unsigned int block, int byte_offset,
int n_bytes) {
ide_cmd_param_t cmd = IDE_DEFAULT_COMMAND;
unsigned base;
unsigned char sect_buffer[IDE_SECTOR_SIZE];
unsigned int track;
int status;
if ((drive < 0) || (drive >= NUM_HD) ||
(harddisk_info[drive].drive_exists == 0))
{
return 1;
}
base = harddisk_info[drive].controller_port;
if (harddisk_info[drive].num_heads > 8) {
outb_p(0xA, IDE_REG_CONTROL(base));
/* IDE normal pci mode */
unsigned cmd_reg, ctrl_reg;
uint32_t cmd_base, ctrl_base;
if (index < 2) {
cmd_reg = PCI_BASE_ADDRESS_0;
ctrl_reg = PCI_BASE_ADDRESS_1;
} else {
outb_p(0x2, IDE_REG_CONTROL(base));
cmd_reg = PCI_BASE_ADDRESS_2;
ctrl_reg = PCI_BASE_ADDRESS_3;
}
pcibios_read_config_dword(0, 0, cmd_reg, &cmd_base);
pcibios_read_config_dword(0, 0, ctrl_reg, &ctrl_base);
controller.cmd_base = cmd_base & ~3;
controller.ctrl_base = ctrl_base & ~3;
track = block / harddisk_info[drive].num_sectors_per_track;
cmd.sector_count = 1;
cmd.sector_number = 1+(block % harddisk_info[drive].num_sectors_per_track);
cmd.cylinder = track % harddisk_info[drive].num_heads;
cmd.drivehead = IDE_DH_DEFAULT |
IDE_DH_HEAD(track / harddisk_info[drive].num_heads) |
IDE_DH_DRIVE(drive) |
IDE_DH_CHS;
printk_info("%s: track=[%d], sector_number=[%d], cylinder=[%d]\n",
__FUNCTION__, track, cmd.sector_number, cmd.cylinder);
write_command(base, IDE_CMD_READ_MULTI_RETRY, &cmd);
if ((inb_p(IDE_REG_STATUS(base)) & 1) != 0) {
return 1;
}
if (n_bytes != IDE_SECTOR_SIZE) {
status = ide_read_data(base, sect_buffer, IDE_SECTOR_SIZE);
if (status == 0) {
memcpy(buffer, sect_buffer+byte_offset, n_bytes);
}
} else {
status = ide_read_data(base, buffer, IDE_SECTOR_SIZE);
}
printk_info("%s: status = [%d]\n", __FUNCTION__, status);
return status;
}
#endif
uint16_t base;
base = (index < 1)?IDE_BASE0:IDE_BASE1;
controller.cmd_base = base;
controller.ctrl_base = base + IDE_REG_EXTENDED_OFFSET;
printk_info("init_controller %d at (%x, %x)\n", index, controller.cmd_base, controller.ctrl_base);
if (init_controller(&controller, index << 1) < 0) {
/* nothing behind the controller */
continue;
}
drives++;
}
return drives > 0 ? 0 : -1;
}