* Copyright (c) 1990 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* @(#)fd.c 5.1 (Berkeley) %G%
/****************************************************************************/
/****************************************************************************/
#include "machine/device.h"
#define FDUNIT(s) ((s)&1)
#define FDTYPE(s) (((s)>>1)&7)
int sectrac
; /* sectors per track */
int secsize
; /* size code for sectors */
int datalen
; /* data len when secsize = 0 */
int gap
; /* gap len between sectors */
int tracks
; /* total num of tracks */
int size
; /* size of disk in sectors */
int steptrac
; /* steps per cylinder */
int trans
; /* transfer speed code */
struct fd_type fd_types
[NUMTYPES
] = {
{ 18,2,0xFF,0x1B,80,2880,1,0 }, /* 1.44 meg HD 3.5in floppy */
{ 15,2,0xFF,0x1B,80,2400,1,0 }, /* 1.2 meg HD floppy */
{ 9,2,0xFF,0x23,40,720,2,1 }, /* 360k floppy in 1.2meg drive */
{ 9,2,0xFF,0x2A,40,720,1,1 }, /* 360k floppy in DD drive */
int type
; /* Drive type (HD, DD */
int active
; /* Drive activity boolean */
int motor
; /* Motor on flag */
int opencnt
; /* Num times open */
struct buf head
; /* Head of buf chain */
struct buf rhead
; /* Raw head of buf chain */
/* state needed for current transfer */
static char fdrawbuf
[FDBLK
];
/* stuff needed for virtual to physical calculations */
static unsigned long sbase
= (unsigned long) &Sysbase
;
/****************************************************************************/
/* autoconfiguration stuff */
/****************************************************************************/
int fdprobe(), fdattach(), fd_turnoff();
struct driver fddriver
= {
/****************************************************************************/
/****************************************************************************/
register struct buf
*bp
; /* IO operation to perform */
register struct buf
*dp
,*dp0
,*dp1
;
unit
= FDUNIT(minor(bp
->b_dev
));
type
= FDTYPE(minor(bp
->b_dev
));
printf("fdstrat: unit = %d, blkno = %d, bcount = %d\n",
unit
, bp
->b_blkno
, bp
->b_bcount
);
if ((unit
>= NFD
) || (bp
->b_blkno
< 0)) {
printf("fdstrat: unit = %d, blkno = %d, bcount = %d\n",
unit
, bp
->b_blkno
, bp
->b_bcount
);
pg("fd:error in fdstrategy");
* Set up block calculations.
blknum
= (unsigned long) bp
->b_blkno
* DEV_BSIZE
/FDBLK
;
nblocks
= fd_types
[type
].size
;
if (blknum
+ (bp
->b_bcount
/ FDBLK
) > nblocks
) {
if (blknum
== nblocks
) bp
->b_resid
= bp
->b_bcount
;
else bp
->b_error
= ENOSPC
;
bp
->b_cylin
= blknum
/ (fd_types
[type
].sectrac
* 2);
bp
->b_cylin
*= (fd_types
[type
].steptrac
);
dp
= &fd_unit
[unit
].head
;
if ((dp0
->b_active
== 0)&&(dp1
->b_active
== 0)) {
untimeout(fd_turnoff
,unit
);
fdstart(unit
); /* start drive if idle */
/****************************************************************************/
/* motor control stuff */
/****************************************************************************/
outb(0x3f2,unit
| (reset
? 0 : 0xC) | (m0
? 16 : 0) | (m1
? 32 : 0));
if (unit
) set_motor(0,0);
/****************************************************************************/
/****************************************************************************/
while ((i
= inb(0x3f4) & 192) != 192) if (i
== 128) return -1;
if (fd_status
[i
] < 0) break;
printf("FD bad status :%X %X %X %X %X %X %X\n",
fd_status
[0], fd_status
[1], fd_status
[2], fd_status
[3],
fd_status
[4], fd_status
[5], fd_status
[6] );
dump_stat(); /* error: direction. eat up output */
/* printf("Error r = %d:",r); */
printf("New MAX = %d\n",maxcnt
);
/* see if fdc responding */
if (inb(0x3f4)&128) return 0;
/****************************************************************************/
/****************************************************************************/
int unit
= FDUNIT(minor(dev
));
int type
= FDTYPE(minor(dev
));
if (unit
>= NFD
) return(ENXIO
);
if (type
>= NUMTYPES
) return(ENXIO
);
if (check_fdc()) return(EBUSY
);
/* Set proper disk type, only allow one type */
/****************************************************************************/
/****************************************************************************/
* Routines to do raw IO for a unit.
fdread(dev
, uio
) /* character read routine */
int unit
= FDUNIT(minor(dev
)) ;
if (unit
>= NFD
) return(ENXIO
);
return(physio(fdstrategy
,&fd_unit
[unit
].rhead
,dev
,B_READ
,minphys
,uio
));
fdwrite(dev
, uio
) /* character write routine */
int unit
= FDUNIT(minor(dev
));
if (unit
>= NFD
) return(ENXIO
);
return(physio(fdstrategy
,&fd_unit
[unit
].rhead
,dev
,B_WRITE
,minphys
,uio
));
/****************************************************************************/
/****************************************************************************/
register struct buf
*dp
,*bp
;
if (!fd_unit
[unit
].motor
) {
timeout(fdstart
,unit
,hz
);
dp
= &fd_unit
[unit
].head
;
/* Seek necessary, never quite sure where head is at! */
out_fdc(15); /* Seek function */
out_fdc(unit
); /* Drive number */
if ((x
>= sbase
) & (x
< sbase
+ 0x800000)) return 1;
/****************************************************************************/
/* set up DMA read/write operation and virtual address addr for nbytes */
/****************************************************************************/
if (kernel_space(addr
)) raw
= 0;
/* copy bounce buffer on write */
if (raw
&& !read
) bcopy(addr
,fdrawbuf
,FDBLK
);
/* Set read/write bytes */
outb(0xC,0x46); outb(0xB,0x46);
outb(0xC,0x4A); outb(0xB,0x4A);
if (raw
) phys
= (unsigned long) &fdrawbuf
[0];
/* translate to physical */
outb(0x4,(phys
>>8) & 0xFF);
outb(0x81,(phys
>>16) & 0xFF);
outb(0x5,(nbytes
>>8) & 0xFF);
dp
= &fd_unit
[fd_drive
].head
;
printf("Timeout drive status %X\n",i
);
printf("ST0 = %X, PCN = %X\n",i
,j
);
/****************************************************************************/
/****************************************************************************/
register struct buf
*dp
,*bp
;
int read
,head
,trac
,sec
,i
,s
,sectrac
;
dp
= &fd_unit
[fd_drive
].head
;
read
= bp
->b_flags
& B_READ
;
ft
= &fd_types
[FDTYPE(bp
->b_dev
)];
case 1 : /* SEEK DONE, START DMA */
printf("ST0 = %X, PCN = %X:",i
,sec
);
fd_dma(read
,bp
->b_un
.b_addr
+fd_skip
,FDBLK
);
blknum
= (unsigned long)bp
->b_blkno
*DEV_BSIZE
/FDBLK
sec
= blknum
% (sectrac
* 2);
if (read
) out_fdc(0xE6); /* READ */
else out_fdc(0xC5); /* WRITE */
out_fdc(head
<< 2 | fd_drive
); /* head & unit */
out_fdc(fd_track
); /* track */
out_fdc(sec
); /* sector XXX +1? */
out_fdc(ft
->secsize
); /* sector size */
out_fdc(sectrac
); /* sectors/track */
out_fdc(ft
->gap
); /* gap size */
out_fdc(ft
->datalen
); /* data length */
timeout(fd_timeout
,2,hz
);
case 2 : /* IO DONE, post-analyze */
printf("status0 err %d:",fd_status
[0]);
printf("status1 err %d:",fd_status
[0]);
printf("status2 err %d:",fd_status
[0]);
if (!kernel_space(bp
->b_un
.b_addr
+fd_skip
)) {
if (read
) bcopy(fdrawbuf
,bp
->b_un
.b_addr
+fd_skip
,
if (fd_skip
>= bp
->b_bcount
) {
dp
->b_actf
= bp
->av_forw
;
/* set up next transfer */
blknum
= (unsigned long)bp
->b_blkno
*DEV_BSIZE
/FDBLK
bp
->b_cylin
= (blknum
/ (ft
->sectrac
* 2));
bp
->b_cylin
*= ft
->steptrac
;
if (bp
->b_cylin
!= fd_track
) {
out_fdc(15); /* Seek function */
out_fdc(fd_drive
);/* Drive number */
out_fdc(15); /* Seek function */
out_fdc(fd_drive
);/* Drive number */
out_fdc(3); /* specify command */
out_fdc(7); /* Recalibrate Function */
printf("Unexpected FD int->");
printf("ST0 = %X, PCN = %X\n",i
,sec
);
printf("intr status :%X %X %X %X %X %X %X ",
fd_status
[0], fd_status
[1], fd_status
[2], fd_status
[3],
fd_status
[4], fd_status
[5], fd_status
[6] );
/* Try a reset, keep motor on */
printf("FD err %X %X %X %X %X %X %X\n",
fd_status
[0], fd_status
[1], fd_status
[2], fd_status
[3],
fd_status
[4], fd_status
[5], fd_status
[6] );
bp
->b_resid
= bp
->b_bcount
- fd_skip
;
dp
->b_actf
= bp
->av_forw
;
nextstate : After a transfer is done, continue processing
requests on the current drive queue. If empty, go to
the other drives queue. If that is empty too, timeout
to turn off the current drive in 5 seconds, and go
to state 0 (not expecting any interrupts).
dpother
= &fd_unit
[fd_drive
? 0 : 1].head
;
if (dp
->b_actf
) fdstart(fd_drive
);
else if (dpother
->b_actf
) {
fdstart(fd_drive
? 0 : 1);
untimeout(fd_turnoff
,fd_drive
);
timeout(fd_turnoff
,fd_drive
,5*hz
);