void write_cmd(int base,char cmd)
{ 
 outportb(base+FLOPPY_FIFO, cmd);
}

unsigned char read_data(int base)
{
 inportb(base + FLOPPY_FIFO);
}

void check_interrupt(int base,int *st0,int *cyl)
{
 write_cmd(base,CMD_SENSE_INTERRUPT);
 *st0=read_data(base);   //get the fdc status
 *cyl=read_data(base);
}

void motor(int base,int onoff)
{
 int i;
 if(onoff==1)	
  {
   outportb(base + FLOPPY_DOR, 0x1c); //turn motor on 
   delay(300); //sleep for 300 millie s
   motor_on=1;
  }  
 else if(onoff==0)
  {
   outportb(base + FLOPPY_DOR,0x0c);
   motor_on=0;
  }
}

int calibrate(int base)
{
 int i,st0,cyl = -1;
 motor(base,1);		//turn motor on
 for(i=0;i<10;i++)
  {
   write_cmd(base,CMD_RECALIBRATE);
   write_cmd(base,0);
   irq_wait(floppy_irq);
   check_interrupt(base,&st0,&cyl);
   if(st0 & 0xC0)
     {
      printf("error\n");
      continue;
     }
   if(cyl==0)
    {
     motor(base,0);	//turn motor off
     return 0;
    }
  }
 motor(base,0);
 return -1;
}

void fdc_handler(struct regs *r)
{
 outportb(0x20,0x26);
}

int floppy_reset(int base)
{
 printf("floppy init...");

 irq_install_handler(0x26, fdc_handler);

 outportb(base + FLOPPY_DOR, 0x00);//disable controller
 outportb(base + FLOPPY_DOR, 0x0C);//enable controller
 irq_wait(floppy_irq);
 {
  int st0,cyl;
  check_interrupt(base,&st0,&cyl);
 }
 //set transfer speed 500kb/s
 outportb(base + FLOPPY_CCR, 0x00);

 write_cmd(base, CMD_SPECIFY);
 write_cmd(base, 0xDF);
 write_cmd(base,0x02);
 
 if(calibrate(base)==0)
  printf("[Done]\n");
}

int seek(int base,unsigned cyli,int head)
{
 int i;
 unsigned st0,cyl = -1;
 motor(base,1); 	//turn motor on
 for(i=0;i<10;i++)
  {
    write_cmd(base, CMD_SEEK);
    write_cmd(base,head << 2);
    write_cmd(base,cyli);
    
    irq_wait(floppy_irq);
    check_interrupt(base,&st0,&cyl);
    if(st0 & 0xC0)
     printf("error\n");
    if(cyl==cyli)	//does it match the right cylinder
      {
       motor(base,0);
       return 0;
      }
  }
 motor(base,0);
 return -1;
}

void dma_init(floppy_dir dir)
{
 union
  {
   unsigned char b[4];
   unsigned long l;
  }a, c;  //address and length
 a.l=(unsigned) &floppy_dmabuf;
 c.l=(unsigned) floppy_dmalen - 1;
 if((a.l >> 24) || (c.l >> 16) || (((a.l&0xffff)+c.l)>>16)) 
  { 
    printf("floppy_dma_init: static buffer problem\n"); 
  } 
 
 unsigned char mode;
 switch(dir)
  {
    case floppy_dir_read: mode = 0x46;break;//read
    case floppy_dir_write: mode= 0x4A;break;//write
  }

 outportb(0x0A,0x06);   //mask channel 2
 outportb(0x0C,0xFF);   //reset flip-flop
 outportb(0x04,a.b[0]); //address low byte
 outportb(0x04,a.b[1]);	//        high
 outportb(0x81,a.b[2]); //externel page register
 
 outportb(0x0C,0xFF);	//reset flip-flop
 outportb(0x05,c.b[0]);	//count low byte
 outportb(0x05,c.b[1]); //      high
 outportb(0x0B,mode);	//set mode
 outportb(0x0A,0x02);	//unmask channel 2
}

int do_track(int base,unsigned cyl,floppy_dir dir)
{
 unsigned char cmd;
 static const int flags = 0xC0;
 switch(dir)
  {
    case  floppy_dir_read:
      cmd = CMD_READ_DATA | flags ;
      break;
    case floppy_dir_write:
      cmd = CMD_WRITE_DATA | flags ;
      break;
  }
 if(seek(base,cyl,0)) return -1;
 if(seek(base,cyl,1)) return -1;
 int i;
 for(i=0;i<20;i++)
  {
   motor(base,1);
   dma_init(dir);
   delay(100); 		//sleep 100ms
   write_cmd(base, cmd);
   write_cmd(base, 0);
   write_cmd(base, cyl); //head and drive
   write_cmd(base, 0);   //first head 
   write_cmd(base, 1);   //first sector
   write_cmd(base, 2);	 //128*2^x=512
   write_cmd(base, 18);  //sector per track
   write_cmd(base, 0x1B);//length of GAP 3
   write_cmd(base, 0xFF);
   
   irq_wait(floppy_irq); 

   unsigned char st0, st1, st2, rcy, rhe, rse, bps; 
        st0 = read_data(base); 
        st1 = read_data(base); 
        st2 = read_data(base); 
        /* 
         * These are cylinder/head/sector values, updated with some 
         * rather bizarre logic, that I would like to understand. 
         * 
         */ 
        rcy = read_data(base); 
        rhe = read_data(base); 
        rse = read_data(base); 
        // bytes per sector, should be what we programmed in 
        bps = read_data(base); 

        int error = 0; 

        if(st0 & 0xC0) { 
            static const char * status[] = 
            { 0, "error", "invalid command", "drive not ready" }; 
            printf("floppy_do_sector: status = %s\n", status[st0 >> 6]); 
            error = 1; 
        } 
        if(st1 & 0x80) { 
            printf("floppy_do_sector: end of cylinder\n"); 
            error = 1; 
        } 
        if(st0 & 0x08) { 
            printf("floppy_do_sector: drive not ready\n"); 
            error = 1; 
        } 
        if(st1 & 0x20) { 
            printf("floppy_do_sector: CRC error\n"); 
            error = 1; 
        } 
        if(st1 & 0x10) { 
            printf("floppy_do_sector: controller timeout\n"); 
            error = 1; 
        } 
        if(st1 & 0x04) { 
            printf("floppy_do_sector: no data found\n"); 
            error = 1; 
        } 
        if((st1|st2) & 0x01) { 
            printf("floppy_do_sector: no address mark found\n"); 
            error = 1; 
        } 
        if(st2 & 0x40) { 
            printf("floppy_do_sector: deleted address mark\n"); 
            error = 1; 
        } 
        if(st2 & 0x20) { 
            printf("floppy_do_sector: CRC error in data\n"); 
            error = 1; 
        } 
        if(st2 & 0x10) { 
            printf("floppy_do_sector: wrong cylinder\n"); 
            error = 1; 
        } 
        if(st2 & 0x04) { 
            printf("floppy_do_sector: uPD765 sector not found\n"); 
            error = 1; 
        } 
        if(st2 & 0x02) { 
            printf("floppy_do_sector: bad cylinder\n"); 
            error = 1; 
        } 
        if(bps != 0x2) { 
            printf("floppy_do_sector: wanted 512B/sector, got %d\n", (1<<(bps+7))); 
            error = 1; 
        } 
        if(st1 & 0x02) { 
            printf("floppy_do_sector: not writable\n"); 
            error = 2; 
        } 

        if(!error) { 
            motor(base, 0); 
            return 0; 
        } 
        if(error > 1) { 
            printf("floppy_do_sector: not retrying..\n"); 
            motor(base, 0); 
            return -2; 
        } 

    printf("floppy_do_sector: 20 retries exhausted\n"); 
    motor(base, 0); 
    return -1; 

 
  }
 motor(base,0);
 return -1;
}