/*****************************************************************************
/*  Name: HDD.C
/*  Author: Kieran Foot
/*  Date: 19/12/07 10:58
/*  Description: Hard Disk Driver
/****************************************************************************/
#include "HDD.H"
/*****************************************************************************
*****************************************************************************/
static void nsleep(unsigned milliseconds) {
    //timer_wait(milliseconds);
}
/*****************************************************************************
*****************************************************************************/
static void msleep(unsigned nanoseconds) {
    timer_wait(nanoseconds);
}
/*****************************************************************************
*****************************************************************************/
static void ata_detect_drives(unsigned io_adr) {
	static char *msg[] =
	{
		"Unknown error", "OK", "Formatter device error",
		"Sector buffer error", "ECC circuitry error",
		"Controller microprocessor errror"
	};
	unsigned timeout, byte0, byte1;

	kprintf("Testing ATA Interface 0x%03X:\n", io_adr);
	for(timeout = 20; timeout != 0; timeout--) {
		if(inportb(io_adr + 7) != 0xFF)
			break;
		msleep(1);
	}
	if(timeout == 0) {
	    kprintf("floating bus; probing cylinder registers...\n");
		outportb(io_adr + 4, 0x55);
		outportb(io_adr + 5, 0xAA);
		byte0 = inportb(io_adr + 4);
		outportb(0x80, 0x55);
		byte1 = inportb(io_adr + 5);
		if(byte0 != 0x55 || byte1 != 0xAA)
		{
		kprintf("floating bus; no drive(s) present\n");
			return;
		}
	}
	for(timeout = 5000; timeout != 0; timeout--) {
		if((inportb(io_adr + 7) & 0x80) == 0)
			break;
		msleep(1);
	}
	if(timeout == 0) {
        kprintf("BSY != 0; no drive(s) present\n");
		return;
	}
	outportb(io_adr + 7, 0x90);
	nsleep(400);
	for(timeout = 5000; timeout != 0; timeout--) {
		if((inportb(io_adr + 7) & 0x80) == 0)
			break;
		msleep(1);
	}
	if(timeout == 0) {
        kprintf("Execute Drive Diagnostics did not complete; no drive(s) present\n");
		return;
	}
	byte0 = inportb(io_adr + 1);	/* b0-b6=master diagnostic code */
	byte1 = byte0 & 0x7F;		/* b7=1 if slave error */
	if(byte1 >= sizeof(msg) / sizeof(msg[0]))
		byte1 = 0;
	kprintf("  master drive: %s\n", msg[byte1]);
    kprintf("  slave  drive: ");
	if(byte0 < 0x80)
	kprintf("NONE\n");
	else
	{
		outportb(io_adr + 6, 0xB0); /* select slave */
		nsleep(400);
		byte0 = inportb(io_adr + 1); /* read slave diagnostic */
		byte1 = byte0 & 0x7F;
		if(byte1 >= sizeof(msg) / sizeof(msg[0]))
			byte1 = 0;
		kprintf("%s\n", msg[byte1]);
	}
}
/*****************************************************************************
*****************************************************************************/
void drive_detect(void) {
    ata_detect_drives(0x1f0);
    ata_detect_drives(0x170);
    ata_detect_drives(0x1e8);
    ata_detect_drives(0x168);
}    
    
