/*****************************************************************************************
Copyright (c) 2002-2004 The UbixOS Project
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of
conditions, the following disclaimer and the list of authors. Redistributions in binary
form must reproduce the above copyright notice, this list of conditions, the following
disclaimer and the list of authors in the documentation and/or other materials provided
with the distribution. Neither the name of the UbixOS Project nor the names of its
contributors may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
$Id$
*****************************************************************************************/
#include <pci/hd.h>
#include <sys/video.h>
#include <sys/device.h>
#include <sys/io.h>
#include <lib/kmalloc.h>
#include <lib/kprintf.h>
#include <devfs/devfs.h>
struct driveInfo *hdd0;
struct driveInfo *hdd0s1;
struct driveInfo *hdd1;
struct driveInfo *hdd2;
struct driveInfo *hdd3;
void initHardDisk() {
struct deviceInterface *devInfo = 0x0;
hdd0 = (struct driveInfo *)kmalloc(sizeof(struct driveInfo));
hdd0s1 = (struct driveInfo *)kmalloc(sizeof(struct driveInfo));
hdd1 = (struct driveInfo *)kmalloc(sizeof(struct driveInfo));
hdd2 = (struct driveInfo *)kmalloc(sizeof(struct driveInfo));
hdd3 = (struct driveInfo *)kmalloc(sizeof(struct driveInfo));
hdd0->hdPort = 0x1F0;
hdd0->hdDev = 0x40;
hdd0->parOffset = 0x0;
hdd0s1->hdPort = 0x1F0;
hdd0s1->hdDev = 0x40;
hdd0s1->parOffset = 63;
hdd1->hdPort = 0x1F0;
hdd1->hdDev = 0x50;
hdd2->hdPort = 0x177;
hdd2->hdDev = 0x40;
hdd3->hdPort = 0x177;
hdd3->hdDev = 0x50;
/* Alloc memory for device structure and set it up correctly */
devInfo = (struct deviceInterface *)kmalloc(sizeof(struct deviceInterface));
devInfo->read = (void *)&hdRead;
devInfo->write = (void *)&hdWrite;
devInfo->reset = (void *)&hdReset;
devInfo->init = (void *)&hdInit;
devInfo->ioctl = (void *)&hdIoctl;
devInfo->stop = (void *)&hdStop;
devInfo->start = (void *)&hdStart;
devInfo->standby = (void *)&hdStandby;
devInfo->info = hdd0;
devInfo->major = 1;
deviceAdd(0,'c',devInfo);
//deviceAdd(1,0,'c',hdRead,hdWrite,hdReset,hdInit,hdIoctl,hdStop,hdStart,hdStandby,hdd0);
//deviceAdd(1,1,'c',hdRead,hdWrite,hdReset,hdInit,hdIoctl,hdStop,hdStart,hdStandby,hdd0s1);
devFsMkNod("ad0",'c',0x1,0x0);
devFsMkNod("ad0s1",'c',0x1,0x1);
/*
if (!initDrive(hdd1)) {
addDrive(2,1,hdd1,hdRead,hdWrite,0x0);
}
if (!initDrive(hdd2)) {
addDrive(3,1,hdd2,hdRead,hdWrite,0x0);
}
if (!initDrive(hdd3)) {
addDrive(4,1,hdd3,hdRead,hdWrite,0x0);
}
*/
return;
}
int hdStandby() {
return(0x0);
}
int hdStart() {
return(0x0);
}
int hdStop() {
return(0x0);
}
int hdIoctl() {
return(0x0);
}
int hdReset() {
return(0x0);
}
int hdInit(struct deviceNode *dev) {
char retVal = 0x0;
long counter = 0x0;
short *tmp = 0x0;
struct driveInfo *hdd = dev->devInfo->info;
for (counter = 1000000;counter >= 0;counter--) {
retVal = inportByte(hdd->hdPort + hdStat) & 0x80;
if (!retVal) goto ready;
}
kprintf("Error Initializing Drive\n");
return(1);
ready:
outportByte(hdd->hdPort + hdHead,hdd->hdDev);
outportByte(hdd->hdPort + hdCmd,0xEC);
for (counter = 1000000;counter >= 0;counter--) {
retVal = inportByte(hdd->hdPort + hdStat);
if ((retVal & 1) != 0x0) {
kprintf("Error Drive Not Available\n");
return(1);
}
if ((retVal & 8) != 0x0) {
goto go;
}
}
kprintf("Time Out Waiting On Drive\n");
return(1);
go:
tmp = (short *)hdd->hdSector;
for (counter = 0;counter < (512/2);counter++) {
tmp[counter] = inportWord(hdd->hdPort + hdData);
}
retVal = tmp[0x2F] & 0xFF;
switch (retVal) {
case 0:
hdd->hdShift = 0;
hdd->hdMulti = 1;
break;
case 2:
hdd->hdShift = 1;
hdd->hdMulti = retVal;
break;
case 4:
hdd->hdShift = 2;
hdd->hdMulti = retVal;
break;
case 8:
hdd->hdShift = 3;
hdd->hdMulti = retVal;
break;
case 16:
hdd->hdShift = 4;
hdd->hdMulti = retVal;
break;
case 32:
hdd->hdShift = 5;
hdd->hdMulti = retVal;
break;
case 64:
hdd->hdShift = 6;
hdd->hdMulti = retVal;
break;
default:
kprintf("Error BLOCK Mode Unavailable: [%i]\n",retVal);
return(1);
}
outportByte(hdd->hdPort + hdSecCount,retVal);
outportByte(hdd->hdPort + hdHead,hdd->hdDev);
outportByte(hdd->hdPort + hdCmd,0xC6);
hdd->hdMask = retVal;
hdd->hdSize = (hdd->hdSector[0x7B] * 256 * 256 * 256) + (hdd->hdSector[0x7A] * 256 * 256) + (hdd->hdSector[0x79] * 256) + hdd->hdSector[0x78];
hdd->hdEnable = 1;
kprintf("Drive: [0x%X/0x%X], Size: [%iSectors/%iKBytes]\n",hdd->hdPort,hdd->hdDev,hdd->hdSize,((hdd->hdSize*512)/1024));
dev->size = hdd->hdSize*512;
return(0x0);
}
void hdWrite(struct driveInfo *hdd,void *baseAddr,uInt32 startSector,uInt32 sectorCount) {
long counter = 0x0;
long retVal = 0x0;
short transactionCount = 0x0;
short *tmp = (short *)baseAddr;
startSector += hdd->parOffset;
if (hdd->hdEnable == 0x0) {
kprintf("Invalid Drive\n");
return;
}
if ((sectorCount >> hdd->hdShift) == 0x0) {
hdd->hdCalc = sectorCount; /* hdd->hdMask; */
transactionCount = 1;
}
else {
hdd->hdCalc = hdd->hdMulti;
transactionCount = sectorCount >> hdd->hdShift;
}
for (;transactionCount > 0;transactionCount--) {
for (counter = 1000000;counter >= 0;counter--) {
retVal = inportByte(hdd->hdPort + hdStat) & 0x80;
if (!retVal) goto ready;
}
kprintf("Time Out Waiting On Drive\n");
return;
ready:
outportByte(hdd->hdPort + hdSecCount,hdd->hdCalc);
outportByte(hdd->hdPort + hdSecNum,(startSector & 0xFF));
retVal = startSector >> 8;
outportByte(hdd->hdPort + hdCylLow,(retVal & 0xFF));
retVal >>= 8;
outportByte(hdd->hdPort + hdCylHi,(retVal & 0xFF));
retVal >>= 8;
retVal &= 0x0F;
retVal |= hdd->hdDev;
outportByte(hdd->hdPort + hdHead,(retVal & 0xFF));
if (hdd->hdShift > 0)
outportByte(hdd->hdPort + hdCmd,0xC5);
else
outportByte(hdd->hdPort + hdCmd,0x30);
for (counter = 1000000;counter >= 0;counter--) {
retVal = inportByte(hdd->hdPort + hdStat);
if ((retVal & 1) != 0x0) {
kprintf("HD Write Error\n");
return;
}
if ((retVal & 8) != 0x0) {
goto go;
}
}
kprintf("Time Out Waiting On Drive\n");
return;
go:
for (counter = 0;counter < (hdd->hdCalc << 8);counter++) {
outportWord(hdd->hdPort + hdData,(short)tmp[counter]);
}
tmp += (counter + 0);
startSector += hdd->hdCalc;
}
return;
}
void hdRead(struct driveInfo *hdd,void *baseAddr,uInt32 startSector,uInt32 sectorCount) {
long counter = 0x0;
long retVal = 0x0;
short transactionCount = 0x0;
short *tmp = (short *)baseAddr;
startSector += hdd->parOffset;
if (hdd->hdEnable == 0x0) {
kprintf("Invalid Drive\n");
return;
}
if ((sectorCount >> hdd->hdShift) == 0x0) {
hdd->hdCalc = sectorCount; /* hdd->hdMask); */
transactionCount = 1;
}
else {
hdd->hdCalc = hdd->hdMulti;
transactionCount = sectorCount >> hdd->hdShift;
}
for (;transactionCount > 0;transactionCount--) {
for (counter = 1000000;counter >= 0;counter--) {
retVal = inportByte(hdd->hdPort + hdStat) & 0x80;
if (!retVal) goto ready;
}
kprintf("Time Out Waiting On Drive\n");
return;
ready:
outportByte(hdd->hdPort + hdSecCount,hdd->hdCalc);
outportByte(hdd->hdPort + hdSecNum,(startSector & 0xFF));
retVal = startSector >> 8;
outportByte(hdd->hdPort + hdCylLow,(retVal & 0xFF));
retVal >>= 8;
outportByte(hdd->hdPort + hdCylHi,(retVal & 0xFF));
retVal >>= 8;
retVal &= 0x0F;
retVal |= hdd->hdDev;
outportByte(hdd->hdPort + hdHead,(retVal & 0xFF));
if (hdd->hdShift > 0)
outportByte(hdd->hdPort + hdCmd,0xC4);
else
outportByte(hdd->hdPort + hdCmd,0x20);
for (counter = 1000000;counter >= 0;counter--) {
retVal = inportByte(hdd->hdPort + hdStat);
if ((retVal & 1) != 0x0) {
kprintf("HD Read Error: [%i:0x%X:%i]\n",counter,(uInt32)baseAddr,startSector);
return;
}
if ((retVal & 8) != 0x0) {
goto go;
}
}
kprintf("Error: Time Out Waiting On Drive\n");
return;
go:
for (counter = 0;counter < (hdd->hdCalc << 8);counter++) {
tmp[counter] = inportWord(hdd->hdPort + hdData);
}
tmp += (counter + 0);
startSector += hdd->hdCalc;
}
return;
}
/***
$Log$
Revision 1.10 2004/05/19 15:20:06 reddawg
Fixed reference problems due to changes in drive subsystem
Revision 1.9 2004/05/19 15:07:59 reddawg
Typo defInfo should of been devInfo
Revision 1.7 2004/05/19 04:07:43 reddawg
kmalloc(size,pid) no more it is no kmalloc(size); the way it should of been
Revision 1.6 2004/04/28 21:10:40 reddawg
Lots Of changes to make it work with existing os
Revision 1.5 2004/04/28 02:37:34 reddawg
More updates for using the new driver subsystem
Revision 1.4 2004/04/28 02:22:54 reddawg
This is a fiarly large commit but we are starting to use new driver model
all around
Revision 1.3 2004/04/27 21:05:19 reddawg
Updating drivers to use new model
Revision 1.2 2004/04/26 22:22:33 reddawg
DevFS now uses correct size of device
Revision 1.1.1.1 2004/04/15 12:07:16 reddawg
UbixOS v1.0
Revision 1.12 2004/04/13 16:36:33 reddawg
Changed our copyright, it is all now under a BSD-Style license
END
***/