/*
* Copyright (c) 1994 Christian E. Hopps
* Copyright (c) 1996 Ezra Story
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Christian E. Hopps.
* This product includes software developed by Ezra Story.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*/
/* that's nice, but we don't want to always use this as an amiga drive
bunghole :-) */
#define FDNHEADS (2) /* amiga drives always have 2 heads */
#define FDSECSIZE (512) /* amiga drives always have 512 byte sectors */
#define FDSECLWORDS (128)
#define FDSETTLEDELAY (18000) /* usec delay after seeking after switch dir */
#define FDSTEPDELAY (3500) /* usec delay after stepping */
#define FDPRESIDEDELAY (1000) /* usec delay before writing can occur */
#define FDWRITEDELAY (1300) /* usec delay after write */
/*
* floppy device type
*/
struct fdtype {
u_int driveid; /* drive identification (from drive) */
u_int ncylinders; /* number of cylinders on drive */
u_int amiga_nsectors; /* number of sectors per amiga track */
u_int msdos_nsectors; /* number of sectors per msdos track */
u_int nreadw; /* number of words (short) read per track */
u_int nwritew; /* number of words (short) written per track */
u_int gap; /* track gap size in long words */
const u_int precomp[2]; /* 1st and 2nd precomp values */
const char *desc; /* description of drive type (useq) */
};
/*
* floppy disk device data
*/
struct fd_softc {
device_t sc_dev; /* generic device info; must come first */
struct disk dkdev; /* generic disk info */
struct bufq_state *bufq;/* queue pending I/O operations */
struct buf curbuf; /* state of current I/O operation */
struct callout calibrate_ch;
struct callout motor_ch;
struct fdtype *type;
void *cachep; /* cached track data (write through) */
int cachetrk; /* cached track -1 for none */
int hwunit; /* unit for amiga controlling hw */
int unitmask; /* mask for cia select deselect */
int pstepdir; /* previous step direction */
int curcyl; /* current curcyl head positioned on */
int flags; /* misc flags */
int wlabel;
int stepdelay; /* useq to delay after seek user settable */
int nsectors; /* number of sectors per track */
int openpart; /* which partition [ab] == [12] is open */
short retries; /* number of times to retry failed io */
short retried; /* number of times current io retried */
int bytespersec; /* number of bytes per sector */
};
/* fd_softc->flags */
#define FDF_MOTORON (0x01) /* motor is running */
#define FDF_MOTOROFF (0x02) /* motor is waiting to be turned off */
#define FDF_WMOTOROFF (0x04) /* unit wants a wakeup after off */
#define FDF_DIRTY (0x08) /* track cache needs write */
#define FDF_WRITEWAIT (0x10) /* need to head select delay on next setpos */
#define FDF_HAVELABEL (0x20) /* label is valid */
#define FDF_JUSTFLUSH (0x40) /* don't bother caching track. */
#define FDF_NOTRACK0 (0x80) /* was not able to recalibrate drive */
int fdc_wantwakeup;
int fdc_side;
void *fdc_dmap;
struct fd_softc *fdc_indma;
int fdc_dmalen;
int fdc_dmawrite;
struct fdcargs {
struct fdtype *type;
int unit;
};
int fdcmatch(device_t, cfdata_t, void *);
void fdcattach(device_t, device_t, void *);
int fdcprint(void *, const char *);
int fdmatch(device_t, cfdata_t, void *);
void fdattach(device_t, device_t, void *);
/*
* read size is (nsectors + 1) * mfm secsize + gap bytes + 2 shorts
* write size is nsectors * mfm secsize + gap bytes + 3 shorts
* the extra shorts are to deal with a DMA hw bug in the controller
* they are probably too much (I believe the bug is 1 short on write and
* 3 bits on read) but there is no need to be cheap here.
*/
#define MAXTRKSZ (22 * FDSECSIZE)
struct fdtype fdtype[] = {
{ 0x00000000, 80, 11, 9, 7358, 6815, 414, { 80, 161 }, "3.5dd" },
{ 0x55555555, 40, 11, 9, 7358, 6815, 414, { 80, 161 }, "5.25dd" },
{ 0xAAAAAAAA, 80, 22, 18, 14716, 13630, 828, { 80, 161 }, "3.5hd" }
};
int nfdtype = __arraycount(fdtype);
/*
* set motor for select units, true motor on else off
*/
#define FDSETMOTOR(on) do { \
if (on) ciab.prb &= ~CIAB_PRB_MTR; else ciab.prb |= CIAB_PRB_MTR; \
} while (0)
/*
* set head for select units
*/
#define FDSETHEAD(head) do { \
if (head) ciab.prb &= ~CIAB_PRB_SIDE; else ciab.prb |= CIAB_PRB_SIDE; \
delay(1); } while (0)
/*
* select direction, true towards spindle else outwards
*/
#define FDSETDIR(in) do { \
if (in) ciab.prb &= ~CIAB_PRB_DIR; else ciab.prb |= CIAB_PRB_DIR; \
delay(1); } while (0)
/*
* step the selected units
*/
#define FDSTEP do { \
ciab.prb &= ~CIAB_PRB_STEP; ciab.prb |= CIAB_PRB_STEP; \
} while (0)
#define FDDMASTART(len, towrite) do { \
int dmasz = (len) | ((towrite) ? DISKLEN_WRITE : 0) | DISKLEN_DMAEN; \
custom.dsklen = dmasz; custom.dsklen = dmasz; } while (0)
#define FDDMASTOP do { custom.dsklen = 0; } while (0)
int
fdcmatch(device_t parent, cfdata_t cf, void *aux)
{
static int fdc_matched = 0;
/* Allow only once instance. */
if (matchname("fdc", aux) == 0 || fdc_matched)
return(0);
if ((fdc_dmap = alloc_chipmem(DMABUFSZ)) == NULL) {
printf("fdc: unable to allocate DMA buffer\n");
return(0);
}
/*
* precalc msdos MFM and CRC
*/
for (i = 0; i < 128; i++)
msdecode[i] = 0xff;
for (i = 0; i < 16; i++)
msdecode[msencode[i]] = i;
for (i = 0; i < 256; i++) {
mscrctab[i] = (0x1021 * (i & 0xf0)) ^ (0x1021 * (i & 0x0f)) ^
(0x1021 * (i >> 4));
}
/*ARGSUSED*/
int
fdopen(dev_t dev, int flags, int devtype, struct lwp *l)
{
struct fd_softc *sc;
int wasopen, fwork, error, s;
error = 0;
if (FDPART(dev) >= FDMAXPARTS)
return(ENXIO);
if ((sc = getsoftc(fd_cd, FDUNIT(dev))) == NULL)
return(ENXIO);
if (sc->flags & FDF_NOTRACK0)
return(ENXIO);
if (sc->cachep == NULL)
sc->cachep = malloc(MAXTRKSZ, M_DEVBUF, M_WAITOK);
s = splbio();
/*
* if we are sleeping in fdclose(); waiting for a chance to
* shut the motor off, do a sleep here also.
*/
while (sc->flags & FDF_WMOTOROFF)
tsleep(fdmotoroff, PRIBIO, "fdopen", 0);
fwork = 0;
/*
* if not open let user open request type, otherwise
* ensure they are trying to open same type.
*/
if (sc->openpart == FDPART(dev))
wasopen = 1;
else if (sc->openpart == -1) {
sc->openpart = FDPART(dev);
wasopen = 0;
} else {
wasopen = 1;
error = EPERM;
goto done;
}
/*
* wait for current io to complete if any
*/
if (fdc_indma) {
fwork = 1;
fdc_wantwakeup++;
tsleep(fdopen, PRIBIO, "fdopen", 0);
}
if ((error = fdloaddisk(sc)) != 0)
goto done;
if ((error = fdgetdisklabel(sc, dev)) != 0)
goto done;
#ifdef FDDEBUG
printf(" open successful\n");
#endif
done:
/*
* if we requested that fddone()->fdfindwork() wake us, allow it to
* complete its job now
*/
if (fwork)
fdfindwork(FDUNIT(dev));
splx(s);
/*
* if we were not open and we marked us so reverse that.
*/
if (error && wasopen == 0)
sc->openpart = -1;
return(error);
}
/*ARGSUSED*/
int
fdclose(dev_t dev, int flags, int devtype, struct lwp *l)
{
struct fd_softc *sc;
int s;
switch (cmd) {
case DIOCSBAD:
return(EINVAL);
case DIOCSRETRIES:
if (*(int *)addr < 0)
return(EINVAL);
sc->retries = *(int *)addr;
return(0);
case DIOCSSTEP:
if (*(int *)addr < FDSTEPDELAY)
return(EINVAL);
sc->dkdev.dk_label->d_trkseek = sc->stepdelay = *(int *)addr;
return(0);
case DIOCSDINFO:
if ((flag & FWRITE) == 0)
return(EBADF);
return(fdsetdisklabel(sc, (struct disklabel *)addr));
case DIOCWDINFO:
if ((flag & FWRITE) == 0)
return(EBADF);
if ((error = fdsetdisklabel(sc, (struct disklabel *)addr)) != 0)
return(error);
wlab = sc->wlabel;
sc->wlabel = 1;
error = fdputdisklabel(sc, dev);
sc->wlabel = wlab;
return(error);
case DIOCWLABEL:
if ((flag & FWRITE) == 0)
return(EBADF);
sc->wlabel = *(int *)addr;
return(0);
case DIOCGDEFLABEL:
fdgetdefaultlabel(sc, (struct disklabel *)addr, FDPART(dev));
return(0);
default:
return(ENOTTY);
}
}
int
fdread(dev_t dev, struct uio *uio, int flags)
{
return (physio(fdstrategy, NULL, dev, B_READ, fdminphys, uio));
}
int
fdwrite(dev_t dev, struct uio *uio, int flags)
{
return (physio(fdstrategy, NULL, dev, B_WRITE, fdminphys, uio));
}
void
fdintr(int flag)
{
int s;
s = splbio();
if (fdc_indma)
fddmadone(fdc_indma, 0);
splx(s);
}
void
fdidxintr(void)
{
if (fdc_indma && fdc_dmalen) {
/*
* turn off intr and start actual dma
*/
ciab.icr = CIA_ICR_FLG;
FDDMASTART(fdc_dmalen, fdc_dmawrite);
fdc_dmalen = 0;
}
}
void
fdstrategy(struct buf *bp)
{
struct fd_softc *sc;
int unit, s;
unit = FDUNIT(bp->b_dev);
sc = getsoftc(fd_cd, unit);
#ifdef FDDEBUG
printf("fdstrategy: %p\n", bp);
#endif
/*
* check for valid partition and bounds
*/
if ((sc->flags & FDF_HAVELABEL) == 0) {
bp->b_error = EIO;
goto done;
}
if (bounds_check_with_label(&sc->dkdev, bp, sc->wlabel) <= 0)
goto done;
/*
* trans count of zero or bounds check indicates io is done
* we are done.
*/
if (bp->b_bcount == 0)
goto done;
bp->b_rawblkno = bp->b_blkno;
/*
* queue the buf and kick the low level code
*/
s = splbio();
bufq_put(sc->bufq, bp);
fdstart(sc);
splx(s);
return;
done:
bp->b_resid = bp->b_bcount;
biodone(bp);
}
/*
* make sure disk is loaded and label is up-to-date.
*/
int
fdloaddisk(struct fd_softc *sc)
{
/*
* if diskchange is low step drive to 0 then up one then to zero.
*/
fdselunit(sc); /* make sure the unit is selected */
if (FDTESTC(FDB_CHANGED)) {
fdsetpos(sc, 0, 0);
sc->cachetrk = -1; /* invalidate the cache */
sc->flags &= ~FDF_HAVELABEL;
fdsetpos(sc, FDNHEADS, 0);
fdsetpos(sc, 0, 0);
if (FDTESTC(FDB_CHANGED)) {
fdmotoroff(sc);
FDDESELECT(sc->unitmask);
return(ENXIO);
}
}
FDDESELECT(sc->unitmask);
fdmotoroff(sc);
sc->type = fdcgetfdtype(sc->hwunit);
if (sc->type == NULL)
return(ENXIO);
if (sc->openpart == FDMSDOSPART)
sc->nsectors = sc->type->msdos_nsectors;
else
sc->nsectors = sc->type->amiga_nsectors;
return(0);
}
/*
* read disk label, if present otherwise create one
* return a new label if raw part and none found, otherwise err.
*/
int
fdgetdisklabel(struct fd_softc *sc, dev_t dev)
{
struct disklabel *lp, *dlp;
struct cpu_disklabel *clp;
struct buf *bp;
int error, part;
/*
* set the incore copy of this units disklabel
*/
int
fdsetdisklabel(struct fd_softc *sc, struct disklabel *lp)
{
struct disklabel *clp;
struct partition *pp;
/*
* must have at least opened raw unit to fetch the
* raw_part stuff.
*/
if ((sc->flags & FDF_HAVELABEL) == 0)
return(EINVAL);
clp = sc->dkdev.dk_label;
/*
* make sure things check out and we only have one valid
* partition
*/
#ifdef FDDEBUG
printf("fdsetdisklabel\n");
#endif
if (lp->d_secsize != FDSECSIZE ||
lp->d_nsectors != clp->d_nsectors ||
lp->d_ntracks != FDNHEADS ||
lp->d_ncylinders != clp->d_ncylinders ||
lp->d_secpercyl != clp->d_secpercyl ||
lp->d_secperunit != clp->d_secperunit ||
lp->d_magic != DISKMAGIC ||
lp->d_magic2 != DISKMAGIC ||
lp->d_npartitions == 0 ||
lp->d_npartitions > FDMAXPARTS ||
(lp->d_partitions[0].p_offset && lp->d_partitions[1].p_offset) ||
dkcksum(lp))
return(EINVAL);
/*
* if any partitions are present make sure they
* represent the currently open type
*/
if ((pp = &lp->d_partitions[0])->p_size) {
if ((pp = &lp->d_partitions[1])->p_size == 0)
goto done;
else if (sc->openpart != 1)
return(EINVAL);
} else if (sc->openpart != 0)
return(EINVAL);
/*
* make sure selected partition is within bounds
* XXX on the second check, its to handle a bug in
* XXX the cluster routines as they require multiples
* XXX of PAGE_SIZE currently
*/
if ((pp->p_offset + pp->p_size >= lp->d_secperunit) ||
(pp->p_frag * pp->p_fsize % PAGE_SIZE))
return(EINVAL);
done:
memcpy(clp, lp, sizeof(struct disklabel));
return(0);
}
/*
* write out the incore copy of this units disklabel
*/
int
fdputdisklabel(struct fd_softc *sc, dev_t dev)
{
struct disklabel *lp, *dlp;
struct buf *bp;
int error;
if ((sc->flags & FDF_HAVELABEL) == 0)
return(EBADF);
#ifdef FDDEBUG
printf("fdputdisklabel\n");
#endif
/*
* get buf and read in sector 0
*/
lp = sc->dkdev.dk_label;
bp = geteblk((int)lp->d_secsize);
bp->b_dev = FDMAKEDEV(major(dev), FDUNIT(dev), RAW_PART);
bp->b_blkno = 0;
bp->b_cylinder = 0;
bp->b_bcount = FDSECSIZE;
bp->b_flags |= B_READ;
fdstrategy(bp);
if ((error = biowait(bp)) != 0)
goto done;
/*
* copy disklabel to buf and write it out synchronous
*/
dlp = (struct disklabel *)((char*)bp->b_data + LABELOFFSET);
memcpy(dlp, lp, sizeof(struct disklabel));
bp->b_blkno = 0;
bp->b_cylinder = 0;
bp->b_flags &= ~(B_READ);
bp->b_oflags &= ~(BO_DONE);
bp->b_flags |= B_WRITE;
fdstrategy(bp);
error = biowait(bp);
done:
brelse(bp, 0);
return(error);
}
/*
* figure out drive type or NULL if none.
*/
struct fdtype *
fdcgetfdtype(int unit)
{
struct fdtype *ftp;
u_long id, idb;
int cnt, umask;
for (idb = 0x80000000; idb; idb >>= 1) {
FDSELECT(umask);
delay(1);
if (FDTESTC(FDB_READY) == 0)
id |= idb;
FDDESELECT(umask);
delay(1);
}
#ifdef FDDEBUG
printf("fdcgettype unit %d id 0x%lx\n", unit, id);
#endif
for (cnt = 0, ftp = fdtype; cnt < nfdtype; ftp++, cnt++)
if (ftp->driveid == id)
return(ftp);
/*
* 3.5dd's at unit 0 do not always return id.
*/
if (unit == 0)
return(fdtype);
return(NULL);
}
/*
* turn motor off if possible otherwise mark as needed and will be done
* later.
*/
void
fdmotoroff(void *arg)
{
struct fd_softc *sc;
int s;
sc = arg;
s = splbio();
#ifdef FDDEBUG
printf("fdmotoroff: unit %d\n", sc->hwunit);
#endif
if ((sc->flags & FDF_MOTORON) == 0)
goto done;
/*
* if we have a timeout on a DMA operation let fddmadone()
* deal with it.
*/
if (fdc_indma == sc) {
fddmadone(sc, 1);
goto done;
}
#ifdef FDDEBUG
printf(" motor was on, turning off\n");
#endif
/*
* flush cache if needed
*/
if (sc->flags & FDF_DIRTY) {
sc->flags |= FDF_JUSTFLUSH | FDF_MOTOROFF;
#ifdef FDDEBUG
printf(" flushing dirty buffer first\n");
#endif
/*
* if DMA'ing done for now, fddone() will call us again
*/
if (fdc_indma)
goto done;
fddmastart(sc, sc->cachetrk);
goto done;
}
/*
* if controller is busy just schedule us to be called back
*/
if (fdc_indma) {
/*
* someone else has the controller now
* just set flag and let fddone() call us again.
*/
sc->flags |= FDF_MOTOROFF;
goto done;
}
#ifdef FDDEBUG
printf(" hw turning unit off\n");
#endif
/*
* select drive seek to track exit with motor on.
* fdsetpos(x, 0, 0) does calibrates the drive.
*/
void
fdsetpos(struct fd_softc *sc, int trk, int towrite)
{
int nstep, sdir, ondly, ncyl, nside;
if (sc->curcyl == ncyl && fdc_side == nside)
return;
if (towrite)
sc->flags |= FDF_WRITEWAIT;
#ifdef FDDEBUG
printf("fdsetpos: cyl %d head %d towrite %d\n", trk / FDNHEADS,
trk % FDNHEADS, towrite);
#endif
nstep = ncyl - sc->curcyl;
if (nstep) {
/*
* figure direction
*/
if (nstep > 0 && ncyl != 0) {
sdir = FDSTEPIN;
FDSETDIR(1);
} else {
nstep = -nstep;
sdir = FDSTEPOUT;
FDSETDIR(0);
}
if (ncyl == 0) {
/*
* either just want cylinder 0 or doing
* a calibrate.
*/
nstep = 256;
while (FDTESTC(FDB_CYLZERO) == 0 && nstep--) {
FDSTEP;
delay(sc->stepdelay);
}
if (nstep < 0)
sc->flags |= FDF_NOTRACK0;
} else {
/*
* step the needed amount amount.
*/
while (nstep--) {
FDSTEP;
delay(sc->stepdelay);
}
}
/*
* if switched directions
* allow drive to settle.
*/
if (sc->pstepdir != sdir)
delay(FDSETTLEDELAY);
sc->pstepdir = sdir;
sc->curcyl = ncyl;
}
if (nside == fdc_side)
return;
/*
* select side
*/
fdc_side = nside;
FDSETHEAD(nside);
delay(FDPRESIDEDELAY);
}
void
fdselunit(struct fd_softc *sc)
{
FDDESELECT(FDCUNITMASK); /* deselect all */
FDSETMOTOR(sc->flags & FDF_MOTORON); /* set motor to unit's state */
delay(1);
FDSELECT(sc->unitmask); /* select unit */
delay(1);
}
/*
* process next buf on device queue.
* normal sequence of events:
* fdstart() -> fddmastart();
* fdidxintr();
* fdintr() -> fddmadone() -> fddone();
* if the track is in the cache then fdstart() will short-circuit
* to fddone() else if the track cache is dirty it will flush. If
* the buf is not an entire track it will cache the requested track.
*/
void
fdstart(struct fd_softc *sc)
{
int trk, error, write;
struct buf *bp, *dp;
int changed;
#ifdef FDDEBUG
printf("fdstart: unit %d\n", sc->hwunit);
#endif
/*
* if DMA'ing just return. we must have been called from fdstrategy.
*/
if (fdc_indma)
return;
/*
* get next buf if there.
*/
dp = &sc->curbuf;
if ((bp = bufq_peek(sc->bufq)) == NULL) {
#ifdef FDDEBUG
printf(" nothing to do\n");
#endif
return;
}
/*
* Mark us as busy now, in case fddone() gets called in one
* of the cases below.
*/
disk_busy(&sc->dkdev);
/*
* make sure same disk is loaded
*/
fdselunit(sc);
changed = FDTESTC(FDB_CHANGED);
FDDESELECT(sc->unitmask);
if (changed) {
/*
* disk missing, invalidate all future io on
* this unit until re-open()'ed also invalidate
* all current io
*/
printf("fdstart: disk changed\n");
#ifdef FDDEBUG
printf(" disk was removed invalidating all io\n");
#endif
sc->flags &= ~FDF_HAVELABEL;
for (;;) {
bp = bufq_get(sc->bufq);
bp->b_error = EIO;
if (bufq_peek(sc->bufq) == NULL)
break;
biodone(bp);
}
/*
* do fddone() on last buf to allow other units to start.
*/
bufq_put(sc->bufq, bp);
fddone(sc);
return;
}
/*
* we have a valid buf, setup our local version
* we use this count to allow reading over multiple tracks.
* into a single buffer
*/
dp->b_bcount = bp->b_bcount;
dp->b_blkno = bp->b_blkno;
dp->b_data = bp->b_data;
dp->b_flags = bp->b_flags;
dp->b_resid = 0;
/*
* continue a started operation on next track. always begin at
* sector 0 on the next track.
*/
void
fdcont(struct fd_softc *sc)
{
struct buf *dp, *bp;
int trk, write;
/*
* guarantee the drive has been at current head and cyl
* for at least FDWRITEDELAY after a write.
*/
if (sc->flags & FDF_WRITEWAIT) {
delay(FDWRITEDELAY);
sc->flags &= ~FDF_WRITEWAIT;
}
if ((sc->flags & FDF_MOTOROFF) == 0) {
/*
* motor runs for 1.5 seconds after last DMA
*/
callout_reset(&sc->motor_ch, 3 * hz / 2, fdmotoroff, sc);
}
if (sc->flags & FDF_DIRTY) {
/*
* if buffer dirty, the last DMA cleaned it
*/
sc->flags &= ~FDF_DIRTY;
if (timeo)
aprint_error_dev(sc->sc_dev,
"write of track cache timed out.\n");
if (sc->flags & FDF_JUSTFLUSH) {
sc->flags &= ~FDF_JUSTFLUSH;
/*
* we are done DMA'ing
*/
fddone(sc);
return;
}
/*
* load the cache
*/
fddmastart(sc, sc->cachetrk);
return;
}
#ifdef FDDEBUG
else if (sc->flags & FDF_MOTOROFF)
panic("fddmadone: FDF_MOTOROFF with no FDF_DIRTY");
#endif
/*
* cache loaded decode it into cache buffer
*/
if (timeo == 0 && fdrawtocache(sc) == 0)
sc->retried = 0;
else {
#ifdef FDDEBUG
if (timeo)
aprint_debug_dev(sc->sc_dev,
"fddmadone: cache load timed out.\n");
#endif
if (sc->retried >= sc->retries) {
sc->retried = 0;
sc->cachetrk = -1;
} else {
sc->retried++;
/*
* this will be restarted at end of calibrate loop.
*/
callout_stop(&sc->motor_ch);
fdcalibrate(sc);
return;
}
}
fddone(sc);
}
#ifdef FDDEBUG
printf("fddone: unit %d\n", sc->hwunit);
#endif
/*
* check to see if unit is just flushing the cache,
* that is we have no io queued.
*/
if (sc->flags & FDF_MOTOROFF)
goto nobuf;
dp = &sc->curbuf;
if ((bp = bufq_peek(sc->bufq)) == NULL)
panic ("fddone");
/*
* check for an error that may have occurred
* while getting the track.
*/
if (sc->cachetrk == -1) {
sc->retried = 0;
bp->b_error = EIO;
} else if (bp->b_error == 0) {
data = sc->cachep;
/*
* get offset of data in track cache and limit
* the copy size to not exceed the cache's end.
*/
data += (dp->b_blkno % sc->nsectors) * FDSECSIZE;
sz = sc->nsectors - dp->b_blkno % sc->nsectors;
sz *= FDSECSIZE;
sz = uimin(dp->b_bcount, sz);
if (bp->b_flags & B_READ)
memcpy(dp->b_data, data, sz);
else {
memcpy(data, dp->b_data, sz);
sc->flags |= FDF_DIRTY;
}
bp->b_resid = dp->b_bcount - sz;
if (bp->b_resid == 0) {
bp->b_error = 0;
} else {
/*
* not done yet need to read next track
*/
fdcont(sc);
return;
}
}
/*
* remove from queue.
*/
(void)bufq_get(sc->bufq);
void
fdfindwork(int unit)
{
struct fd_softc *ssc, *sc;
int i, last;
/*
* first see if we have any fdopen()'s waiting
*/
if (fdc_wantwakeup) {
wakeup(fdopen);
fdc_wantwakeup--;
return;
}
/*
* start next available unit, linear search from the next unit
* wrapping and finally this unit.
*/
last = 0;
ssc = NULL;
for (i = unit + 1; last == 0; i++) {
if (i == unit)
last = 1;
if (i >= fd_cd.cd_ndevs) {
i = -1;
continue;
}
if ((sc = device_lookup_private(&fd_cd, i)) == NULL)
continue;
/*
* if unit has requested to be turned off
* and it has no buf's queued do it now
*/
if (sc->flags & FDF_MOTOROFF) {
if (bufq_peek(sc->bufq) == NULL)
fdmotoroff(sc);
else {
/*
* we gained a buf request while
* we waited, forget the motoroff
*/
sc->flags &= ~FDF_MOTOROFF;
}
/*
* if we now have DMA unit must have needed
* flushing, quit
*/
if (fdc_indma)
return;
}
/*
* if we have no start unit and the current unit has
* io waiting choose this unit to start.
*/
if (ssc == NULL && bufq_peek(sc->bufq) != NULL)
ssc = sc;
}
if (ssc)
fdstart(ssc);
}
/*
* min byte count to whats left of the track in question
*/
void
fdminphys(struct buf *bp)
{
struct fd_softc *sc;
int sec, toff, tsz;
if ((sc = getsoftc(fd_cd, FDUNIT(bp->b_dev))) == NULL)
panic("fdminphys: couldn't get softc");
/*
* encode the track cache into raw MFM ready for DMA
* when we go to multiple disk formats, this will call type dependent
* functions
*/
void fdcachetoraw(struct fd_softc *sc)
{
if (sc->openpart == FDMSDOSPART)
mscachetoraw(sc);
else
amcachetoraw(sc);
}
/*
* decode raw MFM from DMA into units track cache.
* when we go to multiple disk formats, this will call type dependent
* functions
*/
int
fdrawtocache(struct fd_softc *sc)
{
if (sc->openpart == FDMSDOSPART)
return(msrawtocache(sc));
else
return(amrawtocache(sc));
}
/*
* if we are at gap then we can no longer be sure
* of correct sync marks
*/
if ((info & 0xff) == 1)
doagain = 1;
else
doagain = 0;
srp = rp = fdfindsync(crp, erp);
}
return(0);
}
/*
* skip gap and read in data
*/
if ((rp = (u_short *)fdfindsync((u_long *)rp, (u_long *)erp)) == NULL)
return(-1);
if (*rp++ != FDMFMDATA)
continue;
rp = msblkdecode(rp, cp + ((sec-1) * sc->bytespersec),
sc->bytespersec);
rp += 2; /* skip CRC-16 */
retry = 0;
} while (retry);
}
return(0);
}
/*
* encode len longwords of `dp' data in amiga mfm block format (`rp')
* this format specified that the odd bits are at current pos and even
* bits at len + current pos
*/
u_long *
mfmblkencode(u_long *dp, u_long *rp, u_long *cp, int len)
{
u_long *sdp, *edp, d, dtmp, correct;
sdp = dp;
edp = dp + len;
if (*(rp - 1) & 0x1)
correct = 1;
else
correct = 0;
/*
* do odd bits
*/
while (dp < edp) {
d = (*dp >> 1) & 0x55555555; /* remove clock bits */
dtmp = d ^ 0x55555555;
d |= ((dtmp >> 1) | 0x80000000) & (dtmp << 1);
/*
* correct upper clock bit if needed
*/
if (correct)
d &= 0x7fffffff;
if (d & 0x1)
correct = 1;
else
correct = 0;
/*
* do checksums and store in raw buffer
*/
if (cp)
*cp ^= d;
*rp++ = d;
dp++;
}
/*
* do even bits
*/
dp = sdp;
while (dp < edp) {
d = *dp & 0x55555555; /* remove clock bits */
dtmp = d ^ 0x55555555;
d |= ((dtmp >> 1) | 0x80000000) & (dtmp << 1);
/*
* correct upper clock bit if needed
*/
if (correct)
d &= 0x7fffffff;
if (d & 0x1)
correct = 1;
else
correct = 0;
/*
* do checksums and store in raw buffer
*/
if (cp)
*cp ^= d;
*rp++ = d;
dp++;
}
if (cp)
*cp &= 0x55555555;
return(rp);
}
/*
* decode len longwords of `dp' data in amiga mfm block format (`rp')
* this format specified that the odd bits are at current pos and even
* bits at len + current pos
*/
u_long *
mfmblkdecode(u_long *rp, u_long *dp, u_long *cp, int len)
{
u_long o, e;
int cnt;
cnt = len;
while (cnt--) {
o = *rp;
e = *(rp + len);
if (cp) {
*cp ^= o;
*cp ^= e;
}
o &= 0x55555555;
e &= 0x55555555;
*dp++ = (o << 1) | e;
rp++;
}
if (cp)
*cp &= 0x55555555;
return(rp + len);
}
/*
* decode len words in standard MFM format to len bytes
* of data.
*/
u_short *
msblkdecode(u_short *rp, u_char *cp, int len)
{
while (len--) {
*cp++ = msdecode[*rp & 0x7f] |
(msdecode[(*rp >> 8) & 0x7f] << 4);
rp++;
}
return(rp);
}
/*
* encode len bytes of data into len words in standard MFM format.
* If a pointer is supplied for crc, calculate the CRC-16 of the data
* as well.
*/
u_short *
msblkencode(u_short *rp, u_char *cp, int len, u_short *crc)
{
u_short td;
u_short mycrc;
/* preload crc for header (4 bytes)
* or data (anything else)
*/
mycrc = (len == 4) ? 0xb230 : 0xe295;
/* Check for zeros in top bit of encode and bottom
* bit of previous encode. if so, slap a one in between
* them.
*/
if ((td & 0x140) == 0)
td |= 0x80;
if ((td & 0x4000) == 0 && (rp[-1] & 1) == 0)
td |= 0x8000;
*rp++ = td;
/*
* calc crc if requested
*/
if (crc)
mycrc = (mycrc << 8) ^ mscrctab[*cp ^ (mycrc >> 8)];