/************************************************************************** * int fdc_setup * setup the fdc according the datasheet * assuming in PS2 Mode */ int fdc_setup(int drive, FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG) { int i; #ifdef CONFIG_AMIGAONEG3SE irq_install_handler(6, (interrupt_handler_t *)fdc_interrupt, NULL); i8259_unmask_irq(6); #endif #ifdef CFG_FDC_HW_INIT fdc_hw_init (); #endif /* first, we reset the FDC via the DOR */ write_fdc_reg(FDC_DOR,0x00); for(i=0; i<255; i++) /* then we wait some time */ udelay(500); /* then, we clear the reset in the DOR */ pCMD->drive=drive; select_fdc_drive(pCMD); /* initialize the CCR */ write_fdc_reg(FDC_CCR,pFG->rate); /* then initialize the DSR */ write_fdc_reg(FDC_DSR,pFG->rate); if(wait_for_fdc_int()==FALSE) { PRINTF("Time Out after writing CCR\n"); return FALSE; } /* now issue sense Interrupt and status command * assuming only one drive present (drive 0) */ pCMD->dma=0; /* we don't use any dma at all */ for(i=0;i<4;i++) { /* issue sense interrupt for all 4 possible drives */ pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT; if(fdc_issue_cmd(pCMD,pFG)==FALSE) { PRINTF("Sense Interrupt for drive %d failed\n",i); } } /* issue the configure command */ pCMD->drive=drive; select_fdc_drive(pCMD); pCMD->cmd[COMMAND]=FDC_CMD_CONFIGURE; if(fdc_issue_cmd(pCMD,pFG)==FALSE) { PRINTF(" configure timeout\n"); stop_fdc_drive(pCMD); return FALSE; } /* issue specify command */ pCMD->cmd[COMMAND]=FDC_CMD_SPECIFY; if(fdc_issue_cmd(pCMD,pFG)==FALSE) { PRINTF(" specify timeout\n"); stop_fdc_drive(pCMD); return FALSE; } /* then, we clear the reset in the DOR */ /* fdc_check_drive(pCMD,pFG); */ /* write_fdc_reg(FDC_DOR,0x04); */ return TRUE; }
/* issues a recalibrate command, waits for interrupt and * issues a sense_interrupt */ int fdc_seek(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG) { pCMD->cmd[COMMAND]=FDC_CMD_SEEK; if(fdc_issue_cmd(pCMD,pFG)==FALSE) return FALSE; while(wait_for_fdc_int()!=TRUE); pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT; return(fdc_issue_cmd(pCMD,pFG)); }
/* issues a recalibrate command, waits for interrupt and * issues a sense_interrupt */ int fdc_recalibrate(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG) { pCMD->cmd[COMMAND]=FDC_CMD_RECALIBRATE; if(fdc_issue_cmd(pCMD,pFG)==FALSE) return FALSE; while(wait_for_fdc_int()!=TRUE); pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT; return(fdc_issue_cmd(pCMD,pFG)); }
/* issues a recalibrate command, waits for interrupt and * issues a sense_interrupt */ int fdc_seek(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG) { pCMD->cmd[COMMAND]=FDC_CMD_SEEK; if (fdc_issue_cmd(pCMD, pFG) == false) return false; while (wait_for_fdc_int() != true); pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT; return(fdc_issue_cmd(pCMD,pFG)); }
/* Scan all drives and check if drive is present and disk is inserted */ int fdc_check_drive(FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG) { int i,drives,state; /* OK procedure of data book is satisfied. * trying to get some information over the drives */ state=0; /* no drives, no disks */ for(drives=0;drives<4;drives++) { pCMD->drive=drives; select_fdc_drive(pCMD); pCMD->blnr=0; /* set to the 1st block */ if(fdc_recalibrate(pCMD,pFG)==FALSE) continue; if((pCMD->result[STATUS_0]&0x10)==0x10) continue; /* ok drive connected check for disk */ state|=(1<<drives); pCMD->blnr=pFG->size; /* set to the last block */ if(fdc_seek(pCMD,pFG)==FALSE) continue; pCMD->blnr=0; /* set to the 1st block */ if(fdc_recalibrate(pCMD,pFG)==FALSE) continue; pCMD->cmd[COMMAND]=FDC_CMD_READ_ID; if(fdc_issue_cmd(pCMD,pFG)==FALSE) continue; state|=(0x10<<drives); } stop_fdc_drive(pCMD); for(i=0;i<4;i++) { PRINTF("Floppy Drive %d %sconnected %sDisk inserted %s\n",i, ((state&(1<<i))==(1<<i)) ? "":"not ", ((state&(0x10<<i))==(0x10<<i)) ? "":"no ", ((state&(0x10<<i))==(0x10<<i)) ? pFG->name : ""); } pCMD->flags=state; return TRUE; }
/* reads data from FDC, seek commands are issued automatic */ int fdc_read_data(unsigned char *buffer, unsigned long blocks,FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG) { /* first seek to start address */ unsigned long len,lastblk,readblk,i,timeout,ii,offset; unsigned char pcn,c,retriesrw,retriescal; unsigned char *bufferw; /* working buffer */ int sect_size; int flags; flags=disable_interrupts(); /* switch off all Interrupts */ select_fdc_drive(pCMD); /* switch on drive */ sect_size=0x080<<pFG->sect_code; retriesrw=0; retriescal=0; offset=0; if(fdc_seek(pCMD,pFG)==FALSE) { stop_fdc_drive(pCMD); enable_interrupts(); return FALSE; } if((pCMD->result[STATUS_0]&0x20)!=0x20) { printf("Seek error Status: %02X\n",pCMD->result[STATUS_0]); stop_fdc_drive(pCMD); enable_interrupts(); return FALSE; } pcn=pCMD->result[STATUS_PCN]; /* current track */ /* now determine the next seek point */ lastblk=pCMD->blnr + blocks; /* readblk=(pFG->head*pFG->sect)-(pCMD->blnr%(pFG->head*pFG->sect)); */ readblk=pFG->sect-(pCMD->blnr%pFG->sect); PRINTF("1st nr of block possible read %ld start %ld\n",readblk,pCMD->blnr); if(readblk>blocks) /* is end within 1st track */ readblk=blocks; /* yes, correct it */ PRINTF("we read %ld blocks start %ld\n",readblk,pCMD->blnr); bufferw=&buffer[0]; /* setup working buffer */ do { retryrw: len=sect_size * readblk; pCMD->cmd[COMMAND]=FDC_CMD_READ; if(fdc_issue_cmd(pCMD,pFG)==FALSE) { stop_fdc_drive(pCMD); enable_interrupts(); return FALSE; } for (i=0;i<len;i++) { timeout=FDC_TIME_OUT; do { c=read_fdc_reg(FDC_MSR); if((c&0xC0)==0xC0) { bufferw[i]=read_fdc_reg(FDC_FIFO); break; } if((c&0xC0)==0x80) { /* output */ PRINTF("Transfer error transfered: at %ld, MSR=%02X\n",i,c); if(i>6) { for(ii=0;ii<7;ii++) { pCMD->result[ii]=bufferw[(i-7+ii)]; } /* for */ } if(retriesrw++>FDC_RW_RETRIES) { if (retriescal++>FDC_CAL_RETRIES) { stop_fdc_drive(pCMD); enable_interrupts(); return FALSE; } else { PRINTF(" trying to recalibrate Try %d\n",retriescal); if(fdc_recalibrate(pCMD,pFG)==FALSE) { stop_fdc_drive(pCMD); enable_interrupts(); return FALSE; } retriesrw=0; goto retrycal; } /* else >FDC_CAL_RETRIES */ } else { PRINTF("Read retry %d\n",retriesrw); goto retryrw; } /* else >FDC_RW_RETRIES */ }/* if output */ timeout--; }while(TRUE); } /* for len */ /* the last sector of a track or all data has been read, * we need to get the results */ fdc_terminate(pCMD); offset+=(sect_size*readblk); /* set up buffer pointer */ bufferw=&buffer[offset]; pCMD->blnr+=readblk; /* update current block nr */ blocks-=readblk; /* update blocks */ if(blocks==0) break; /* we are finish */ /* setup new read blocks */ /* readblk=pFG->head*pFG->sect; */ readblk=pFG->sect; if(readblk>blocks) readblk=blocks; retrycal: /* a seek is necessary */ if(fdc_seek(pCMD,pFG)==FALSE) { stop_fdc_drive(pCMD); enable_interrupts(); return FALSE; } if((pCMD->result[STATUS_0]&0x20)!=0x20) { PRINTF("Seek error Status: %02X\n",pCMD->result[STATUS_0]); stop_fdc_drive(pCMD); return FALSE; } pcn=pCMD->result[STATUS_PCN]; /* current track */ }while(TRUE); /* start over */ stop_fdc_drive(pCMD); /* switch off drive */ enable_interrupts(); return TRUE; }