/***************************************************************************************** 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 <isa/fdc.h> #include <sys/spinlock.h> #include <sys/device.h> #include <sys/idt.h> #include <sys/io.h> #include <sys/dma.h> #include <lib/kmalloc.h> #include <lib/kprintf.h> static spinLock_t fdcSpinLock = SPIN_LOCK_INITIALIZER; static volatile bool done = FALSE; static bool motor = FALSE; static bool diskChange = FALSE; static volatile int timeOut = 0x0; static Int8 statSize = 0x0; static Int8 status[7] = { 0x0 }; static Int8 sr0 = 0x0; static volatile Int8 fdcTrack = 0xFF; static unsigned long tbaddr = 0x80000L; static drvGeom geometry = { dg144Heads,dg144Tracks,dg144Spt }; static void fdc_write(void *,void *,u_int32_t,u_int32_t); static void fdc_read(void *,void *,u_int32_t,u_int32_t); static void fdc_isr(); static void fdc_reset(); static bool fdc_readBlock(int,u_int8_t *,unsigned long); static bool fdc_writeBlock(int,u_int8_t *,unsigned long); static bool fdc_wait(bool); static void fdc_sendByte(int); static int fdc_getByte(); static bool fdc_seek(int); static void fdc_recalibrate(void); static bool fdc_rw(int,Int8 *,bool,unsigned long); static void fdc_motorOn(void); static void fdc_motorOff(void); static void fdc_block2Hts(int,int *,int *,int *); void fdc_isr(); asm( ".globl fdc_isr \n" "fdc_isr: \n" " pusha \n" " push %ss \n" " push %ds \n" " push %es \n" " push %fs \n" " push %gs \n" " call fdc_isrHndlr \n" " pop %gs \n" " pop %fs \n" " pop %es \n" " pop %ds \n" " pop %ss \n" " popa \n" " iret \n" ); static int fdc_init2(struct device_node *dev) { dev->devInfo->size = (1024 * 1450); return(0x0); } int fdc_init() { struct device_interface *devInfo = (struct device_interface *)kmalloc(sizeof(struct device_interface)); setVector(fdc_isr, mVec+6, (dInt+dPresent)); irqEnable(6); fdc_reset(); devInfo->major = 0x0; devInfo->init = (void *)&fdc_init2; devInfo->read = fdc_read; devInfo->write = fdc_write; devInfo->reset = (void *)fdc_reset; device_add(0,'c',devInfo); //devfs_makeNode("fd0",'b',0x0,0x0); return(0x0); } static void fdc_read(void *info,void *baseAddr,u_int32_t startSector,u_int32_t sectorCount) { spinLock(&fdcSpinLock); fdc_readBlock(startSector,baseAddr,sectorCount); spinUnlock(&fdcSpinLock); return; } static void fdc_write(void *info,void *baseAddr,u_int32_t startSector,u_int32_t sectorCount){ fdc_writeBlock(startSector,baseAddr,sectorCount); return; } static void fdc_isrHndlr() { done = TRUE; outportByte(0x20,0x20); } static void fdc_reset(void) { outportByte(fdcDor,0); motor = FALSE; outportByte(fdcDor,0x0c); done = TRUE; fdc_wait(TRUE); fdc_sendByte(cmdSpecify); fdc_sendByte(0xdf); fdc_sendByte(0x02); fdc_seek(1); fdc_recalibrate(); diskChange = FALSE; return; } static bool fdc_readBlock(int block,u_int8_t *blockBuffer,unsigned long numSectors) { int result = 0x0,loop = 0x0; if (numSectors > 1) { for (loop=0; loop<numSectors; loop++) { result = fdc_rw(block+loop, blockBuffer+(loop*512), TRUE, 1); } return result; } return fdc_rw(block,blockBuffer,TRUE,numSectors); } static bool fdc_writeBlock(int block,u_int8_t *blockBuffer, unsigned long numSectors) { return fdc_rw(block,blockBuffer,FALSE, numSectors); } static bool fdc_wait(bool sensei) { timeOut = 50000; while (!done && timeOut); statSize = 0; while ((statSize < 7) && (inportByte(fdcMsr) & (1<<4))) { status[(int)statSize++] = fdc_getByte(); } if (sensei) { fdc_sendByte(cmdSensei); sr0 = fdc_getByte(); fdcTrack = fdc_getByte(); } done = FALSE; if (!timeOut) { if (inportByte(fdcDir) & 0x80) { diskChange = TRUE; } return(FALSE); } else { return(TRUE); } } static void fdc_sendByte(int Int8) { volatile int msr; int tmo; for (tmo=0;tmo<128;tmo++) { msr = inportByte(fdcMsr); if ((msr & 0xc0) == 0x80) { outportByte(fdcData,Int8); return; } inportByte(0x80); } } static int fdc_getByte() { volatile int msr; int tmo; for (tmo=0;tmo<128;tmo++) { msr = inportByte(fdcMsr); if ((msr & 0xd0) == 0xd0) { return inportByte(fdcData); } inportByte(0x80); } return(-1); } static bool fdc_seek(int track) { if (fdcTrack == track) { return(TRUE); } fdc_sendByte(cmdSeek); fdc_sendByte(0); fdc_sendByte(track); if (!fdc_wait(TRUE)) { kprintf("wait fdc failed\n"); return(FALSE); } if ((sr0 != 0x20) || (fdcTrack != track)) { return(FALSE); } else { return(TRUE); } } static void fdc_recalibrate(void) { fdc_motorOn(); fdc_sendByte(cmdRecal); fdc_sendByte(0); fdc_wait(TRUE); fdc_motorOff(); } static bool fdc_rw(int block,Int8 *blockBuffer,bool read,unsigned long numSectors) { int head = 0x0,track = 0x0,sector = 0x0,tries= 0x0, copyCount = 0x0; unsigned char *p_tbaddr = (char *)0x80000; unsigned char *p_blockbuff = blockBuffer; //kprintf("Block: [%i]\n",block); fdc_block2Hts(block,&head,&track,§or); fdc_motorOn(); if (!read && blockBuffer) { /* copy data from data buffer into track buffer */ for (copyCount=0; copyCount<(numSectors*512); copyCount++) { *p_tbaddr = *p_blockbuff; p_blockbuff++; p_tbaddr++; } } for (tries = 0;tries < 3;tries++) { if (inportByte(fdcDir) & 0x80) { diskChange = TRUE; fdc_seek(1); /* clear "disk change" status */ fdc_recalibrate(); fdc_motorOff(); kprintf("FDC: Disk change detected. Trying again.\n"); return fdc_rw(block, blockBuffer, read, numSectors); } if (!fdc_seek(track)) { fdc_motorOff(); kprintf("FDC: Error seeking to track [%i]\n",block); return FALSE; } outportByte(fdcCcr,0); if (read) { dmaXfer(2,tbaddr,numSectors*512,FALSE); fdc_sendByte(cmdRead); } else { dmaXfer(2,tbaddr,numSectors*512,TRUE); fdc_sendByte(cmdWrite); } fdc_sendByte(head << 2); fdc_sendByte(track); fdc_sendByte(head); fdc_sendByte(sector); fdc_sendByte(2); /* 512 Int8s/sector */ fdc_sendByte(geometry.spt); if (geometry.spt == dg144Spt) { fdc_sendByte(dg144Gap3rw); /* gap 3 size for 1.44M read/write */ } else { fdc_sendByte(dg168Gap3rw); /* gap 3 size for 1.68M read/write */ } fdc_sendByte(0xff); /* DTL = unused */ if (!fdc_wait(TRUE)) { kprintf("Timed out, trying operation again after reset()\n"); fdc_reset(); return fdc_rw(block, blockBuffer, read, numSectors); } if ((status[0] & 0xc0) == 0) break; /* worked! outta here! */ fdc_recalibrate(); /* oops, try again... */ } fdc_motorOff(); if (read && blockBuffer) { p_blockbuff = blockBuffer; p_tbaddr = (char *) 0x80000; for (copyCount=0x0; copyCount<(numSectors*512); copyCount++) { *p_blockbuff = *p_tbaddr; p_blockbuff++; p_tbaddr++; } } return (tries != 3); } static void fdc_motorOn(void) { if (motor == FALSE) { outportByte(fdcDor,0x1c); motor = TRUE; } } static void fdc_motorOff(void) { if (motor == TRUE) { //outportByte(fdcDor,0x0); //outportByte(fdcDor,0x0C); motor = FALSE; } } static void fdc_block2Hts(int block,int *head,int *track,int *sector) { *head = (block % (geometry.spt * geometry.heads)) / (geometry.spt); *track = block / (geometry.spt * geometry.heads); *sector = block % geometry.spt + 1; } /*** END ***/