#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.100 2026/05/30 10:10:11 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/kernel.h>
#include <sys/buf.h>
#include <sys/bufq.h>
#include <sys/proc.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <sys/conf.h>
#include <sys/disklabel.h>
#include <sys/disk.h>
#include <atari/atari/device.h>
#include <atari/atari/stalloc.h>
#include <machine/disklabel.h>
#include <machine/iomap.h>
#include <machine/mfp.h>
#include <machine/dma.h>
#include <machine/video.h>
#include <machine/cpu.h>
#include <atari/dev/ym2149reg.h>
#include <atari/dev/fdreg.h>
#include "ioconf.h"
#define FDC_MAX_DMA_AD 0x1000000
#define SECTOR_SIZE 512
#define NR_DRIVES 2
#define NR_TYPES 3
#define MAX_ERRORS 10
#define STEP_DELAY 6000
#define INV_TRK 32000
#define INV_PART NR_TYPES
#define FLP_IDLE 0x00
#define FLP_MON 0x01
#define FLP_STAT 0x02
#define FLP_XFER 0x04
#define FLP_MONDELAY (3 * hz)
#define FLP_XFERDELAY (2 * hz)
#define FLP_DD 0
#define FLP_HD 1
#define b_block b_resid
static short selected = 0;
static short motoron = 0;
static short nopens = 0;
static short fd_state = FLP_IDLE;
static int lock_stat = 0;
static short fd_cmd = 0;
static const char *fd_error = NULL;
struct fd_softc {
device_t sc_dev;
struct disk dkdev;
struct bufq_state *bufq;
struct callout sc_motor_ch;
int unit;
int nheads;
int nsectors;
int density;
int nblocks;
int curtrk;
short flags;
short part;
int sector;
uint8_t *io_data;
int io_bytes;
int io_dir;
int errcnt;
uint8_t *bounceb;
};
#define FLPF_NOTRESP 0x001
#define FLPF_ISOPEN 0x002
#define FLPF_SPARE 0x004
#define FLPF_HAVELAB 0x008
#define FLPF_BOUNCE 0x010
#define FLPF_WRTPROT 0x020
#define FLPF_EMPTY 0x040
#define FLPF_INOPEN 0x080
#define FLPF_GETSTAT 0x100
struct fd_types {
int nheads;
int nsectors;
int nblocks;
int density;
const char *descr;
} fdtypes[NR_TYPES] = {
{ 1, 9, 720 , FLP_DD , "360KB" },
{ 2, 9, 1440 , FLP_DD , "720KB" },
{ 2, 18, 2880 , FLP_HD , "1.44MB" },
};
#define FLP_TYPE_360 0
#define FLP_TYPE_720 1
#define FLP_TYPE_144 2
static short def_type = 0;
#define FLP_DEFTYPE 1
#define FLP_TYPE(dev) ( DISKPART(dev) == 0 ? def_type : DISKPART(dev) - 1 )
typedef void (*FPV)(void *);
static dev_type_open(fdopen);
static dev_type_close(fdclose);
static dev_type_read(fdread);
static dev_type_write(fdwrite);
static dev_type_ioctl(fdioctl);
static dev_type_strategy(fdstrategy);
static void fdstart(struct fd_softc *);
static void fddone(struct fd_softc *);
static void fdstatus(struct fd_softc *);
static void fd_xfer(struct fd_softc *);
static void fdcint(struct fd_softc *);
static int fd_xfer_ok(struct fd_softc *);
static void fdmotoroff(struct fd_softc *);
static void fdminphys(struct buf *);
static void fdtestdrv(struct fd_softc *);
static void fdgetdefaultlabel(struct fd_softc *, struct disklabel *,
int);
static int fdgetdisklabel(struct fd_softc *, dev_t);
static int fdselect(int, int, int);
static void fddeselect(void);
static void fdmoff(struct fd_softc *);
static u_short rd_cfg_switch(void);
static inline uint8_t read_fdreg(u_short);
static inline void write_fdreg(u_short, u_short);
static inline uint8_t read_dmastat(void);
static inline
uint8_t read_fdreg(u_short regno)
{
DMA->dma_mode = regno;
return DMA->dma_data;
}
static inline
void write_fdreg(u_short regno, u_short val)
{
DMA->dma_mode = regno;
DMA->dma_data = val;
}
static inline
uint8_t read_dmastat(void)
{
DMA->dma_mode = FDC_CS | DMA_SCREG;
return DMA->dma_stat;
}
static u_short
rd_cfg_switch(void)
{
return *(volatile u_short *)AD_CFG_SWITCH;
}
#define CFG_SWITCH_NOHD 0x4000
static int fdcmatch(device_t, cfdata_t, void *);
static int fdcprint(void *, const char *);
static void fdcattach(device_t, device_t, void *);
CFATTACH_DECL_NEW(fdc, 0,
fdcmatch, fdcattach, NULL, NULL);
const struct bdevsw fd_bdevsw = {
.d_open = fdopen,
.d_close = fdclose,
.d_strategy = fdstrategy,
.d_ioctl = fdioctl,
.d_dump = nodump,
.d_psize = nosize,
.d_discard = nodiscard,
.d_flag = D_DISK
};
const struct cdevsw fd_cdevsw = {
.d_open = fdopen,
.d_close = fdclose,
.d_read = fdread,
.d_write = fdwrite,
.d_ioctl = fdioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_DISK
};
static int
fdcmatch(device_t parent, cfdata_t match, void *aux)
{
static int fdc_matched = 0;
if (strcmp("fdc", aux) || fdc_matched)
return 0;
fdc_matched = 1;
return 1;
}
static void
fdcattach(device_t parent, device_t self, void *aux)
{
struct fd_softc fdsoftc;
int i, nfound, first_found;
nfound = first_found = 0;
aprint_normal("\n");
fddeselect();
for (i = 0; i < NR_DRIVES; i++) {
fdsoftc.unit = i;
fdsoftc.flags = 0;
st_dmagrab((dma_farg)fdcint, (dma_farg)fdtestdrv, &fdsoftc,
&lock_stat, 0, NULL);
st_dmafree(&fdsoftc, &lock_stat);
if ((fdsoftc.flags & FLPF_NOTRESP) == 0) {
if (nfound == 0)
first_found = i;
nfound++;
config_found(self, (void *)i, fdcprint, CFARGS_NONE);
}
}
if (nfound != 0) {
struct fd_softc *fdsc =
device_lookup_private(&fd_cd, first_found);
fdselect(first_found, 0, FLP_DD);
fd_state = FLP_MON;
callout_reset(&fdsc->sc_motor_ch, 0, (FPV)fdmotoroff, fdsc);
MFP->mf_ierb |= IB_DINT;
MFP->mf_iprb = (uint8_t)~IB_DINT;
MFP->mf_imrb |= IB_DINT;
}
}
static int
fdcprint(void *aux, const char *pnp)
{
if (pnp != NULL)
aprint_normal("fd%d at %s:", (int)aux, pnp);
return UNCONF;
}
static int fdmatch(device_t, cfdata_t, void *);
static void fdattach(device_t, device_t, void *);
struct dkdriver fddkdriver = {
.d_strategy = fdstrategy
};
CFATTACH_DECL_NEW(fd, sizeof(struct fd_softc),
fdmatch, fdattach, NULL, NULL);
static int
fdmatch(device_t parent, cfdata_t match, void *aux)
{
return 1;
}
static void
fdattach(device_t parent, device_t self, void *aux)
{
struct fd_softc *sc;
struct fd_types *type;
u_short swtch;
sc = device_private(self);
sc->sc_dev = self;
callout_init(&sc->sc_motor_ch, 0);
swtch = rd_cfg_switch();
def_type = (swtch & CFG_SWITCH_NOHD) ? FLP_TYPE_720 : FLP_TYPE_144;
type = &fdtypes[def_type];
aprint_normal(": %s %d cyl, %d head, %d sec\n", type->descr,
type->nblocks / (type->nsectors * type->nheads), type->nheads,
type->nsectors);
disk_init(&sc->dkdev, device_xname(sc->sc_dev), &fddkdriver);
disk_attach(&sc->dkdev);
}
static int
fdioctl(dev_t dev, u_long cmd, void * addr, int flag, struct lwp *l)
{
struct fd_softc *sc;
int error;
sc = device_lookup_private(&fd_cd, DISKUNIT(dev));
if ((sc->flags & FLPF_HAVELAB) == 0)
return EBADF;
error = disk_ioctl(&sc->dkdev, RAW_PART, cmd, addr, flag, l);
if (error != EPASSTHROUGH)
return error;
switch (cmd) {
#ifdef notyet
case DIOCSRETRIES:
case DIOCSSTEP:
case DIOCSDINFO:
case DIOCWDINFO:
case DIOCWLABEL:
break;
#endif
case DIOCGDEFLABEL:
fdgetdefaultlabel(sc, (struct disklabel *)addr, RAW_PART);
return 0;
}
return ENOTTY;
}
static int
fdopen(dev_t dev, int flags, int devtype, struct lwp *l)
{
struct fd_softc *sc;
int s;
#ifdef FLP_DEBUG
printf("fdopen dev=0x%llx\n", dev);
#endif
if (FLP_TYPE(dev) >= NR_TYPES)
return ENXIO;
if ((sc = device_lookup_private(&fd_cd, DISKUNIT(dev))) == NULL)
return ENXIO;
if (nopens == 0) {
#ifdef FLP_DEBUG
printf("fdopen device not yet open\n");
#endif
nopens++;
write_fdreg(FDC_CS, IRUPT);
delay(40);
}
s = splbio();
while (sc->flags & FLPF_INOPEN)
tsleep((void *)sc, PRIBIO, "fdopen", 0);
splx(s);
if ((sc->flags & FLPF_ISOPEN) == 0) {
int type;
void *addr;
type = FLP_TYPE(dev);
bufq_alloc(&sc->bufq, "disksort", BUFQ_SORT_RAWBLOCK);
sc->unit = DISKUNIT(dev);
sc->part = RAW_PART;
sc->nheads = fdtypes[type].nheads;
sc->nsectors = fdtypes[type].nsectors;
sc->nblocks = fdtypes[type].nblocks;
sc->density = fdtypes[type].density;
sc->curtrk = INV_TRK;
sc->sector = 0;
sc->errcnt = 0;
sc->bounceb = alloc_stmem(SECTOR_SIZE, &addr);
if (sc->bounceb == NULL)
return ENOMEM;
sc->flags |= FLPF_INOPEN|FLPF_GETSTAT;
s = splbio();
st_dmagrab((dma_farg)fdcint, (dma_farg)fdstatus, sc,
&lock_stat, 0, NULL);
while ((sc->flags & FLPF_GETSTAT) != 0)
tsleep((void *)sc, PRIBIO, "fdopen", 0);
splx(s);
wakeup((void *)sc);
if ((sc->flags & FLPF_WRTPROT) != 0 &&
(flags & FWRITE) != 0) {
sc->flags = 0;
return EPERM;
}
if ((sc->flags & FLPF_EMPTY) != 0) {
sc->flags = 0;
return ENXIO;
}
sc->flags &= ~(FLPF_INOPEN|FLPF_GETSTAT);
sc->flags |= FLPF_ISOPEN;
} else {
if (sc->density != fdtypes[DISKPART(dev)].density)
return ENXIO;
}
fdgetdisklabel(sc, dev);
#ifdef FLP_DEBUG
printf("fdopen open succeeded on type %d\n", sc->part);
#endif
return 0;
}
static int
fdclose(dev_t dev, int flags, int devtype, struct lwp *l)
{
struct fd_softc *sc;
sc = device_lookup_private(&fd_cd, DISKUNIT(dev));
free_stmem(sc->bounceb);
sc->flags = 0;
nopens--;
#ifdef FLP_DEBUG
printf("Closed floppy device -- nopens: %d\n", nopens);
#endif
return 0;
}
static void
fdstrategy(struct buf *bp)
{
struct fd_softc *sc;
struct disklabel *lp;
int s, sz;
sc = device_lookup_private(&fd_cd, DISKUNIT(bp->b_dev));
#ifdef FLP_DEBUG
printf("fdstrategy: %p, b_bcount: %d\n", bp, bp->b_bcount);
#endif
lp = sc->dkdev.dk_label;
if ((sc->flags & FLPF_HAVELAB) == 0) {
bp->b_error = EIO;
goto done;
}
if (bp->b_blkno < 0 || (bp->b_bcount % SECTOR_SIZE) != 0) {
bp->b_error = EINVAL;
goto done;
}
if (bp->b_bcount == 0)
goto done;
sz = howmany(bp->b_bcount, SECTOR_SIZE);
if (bp->b_blkno + sz > sc->nblocks) {
sz = sc->nblocks - bp->b_blkno;
if (sz == 0)
goto done;
if (sz < 0) {
bp->b_error = EINVAL;
goto done;
}
if (bp->b_flags & B_RAW)
bp->b_bcount = sz << DEV_BSHIFT;
else
bp->b_bcount = sz * lp->d_secsize;
}
bp->b_rawblkno = bp->b_blkno;
s = splbio();
bufq_put(sc->bufq, bp);
if (!lock_stat) {
if (fd_state & FLP_MON)
callout_stop(&sc->sc_motor_ch);
fd_state = FLP_IDLE;
st_dmagrab((dma_farg)fdcint, (dma_farg)fdstart, sc,
&lock_stat, 0, NULL);
}
splx(s);
return;
done:
bp->b_resid = bp->b_bcount;
biodone(bp);
}
static int
fdread(dev_t dev, struct uio *uio, int flags)
{
return physio(fdstrategy, NULL, dev, B_READ, fdminphys, uio);
}
static int
fdwrite(dev_t dev, struct uio *uio, int flags)
{
return physio(fdstrategy, NULL, dev, B_WRITE, fdminphys, uio);
}
static void
fdstatus(struct fd_softc *sc)
{
#ifdef FLP_DEBUG
printf("fdstatus\n");
#endif
sc->errcnt = 0;
fd_state = FLP_STAT;
fd_xfer(sc);
}
static void
fdstart(struct fd_softc *sc)
{
struct buf *bp;
bp = bufq_peek(sc->bufq);
sc->sector = bp->b_blkno;
sc->io_data = bp->b_data;
sc->io_bytes = bp->b_bcount;
sc->io_dir = bp->b_flags & B_READ;
sc->errcnt = 0;
fd_state = FLP_XFER;
disk_busy(&sc->dkdev);
fd_xfer(sc);
}
static void
fddone(register struct fd_softc *sc)
{
struct buf *bp;
struct fd_softc *sc1;
int i, s;
st_dmafree(sc, &lock_stat);
if (fd_state != FLP_STAT) {
s = splbio();
bp = bufq_get(sc->bufq);
if (bp == NULL)
panic("fddone");
splx(s);
#ifdef FLP_DEBUG
printf("fddone: unit: %d, buf: %p, resid: %d\n",sc->unit, bp,
sc->io_bytes);
#endif
bp->b_resid = sc->io_bytes;
disk_unbusy(&sc->dkdev, (bp->b_bcount - bp->b_resid),
(bp->b_flags & B_READ));
biodone(bp);
}
fd_state = FLP_MON;
if (lock_stat)
return;
for (i = sc->unit + 1;; i++) {
if (i >= fd_cd.cd_ndevs)
i = 0;
if ((sc1 = device_lookup_private(&fd_cd, i)) == NULL)
continue;
if (bufq_peek(sc1->bufq) != NULL)
break;
if (i == sc->unit) {
callout_reset(&sc->sc_motor_ch, FLP_MONDELAY,
(FPV)fdmotoroff, sc);
#ifdef FLP_DEBUG
printf("fddone: Nothing to do\n");
#endif
return;
}
}
fd_state = FLP_IDLE;
#ifdef FLP_DEBUG
printf("fddone: Staring job on unit %d\n", sc1->unit);
#endif
st_dmagrab((dma_farg)fdcint, (dma_farg)fdstart, sc1, &lock_stat, 0,
NULL);
}
static int
fdselect(int drive, int head, int dense)
{
int i, spinning;
#ifdef FLP_DEBUG
printf("fdselect: drive=%d, head=%d, dense=%d\n", drive, head, dense);
#endif
i = ((drive == 1) ? PA_FLOP1 : PA_FLOP0) | head;
spinning = motoron;
motoron = 1;
switch (dense) {
case FLP_DD:
DMA->dma_drvmode = 0;
break;
case FLP_HD:
DMA->dma_drvmode = (FDC_HDSET|FDC_HDSIG);
break;
default:
panic("fdselect: unknown density code");
}
if (i != selected) {
selected = i;
ym2149_fd_select((i ^ PA_FDSEL));
}
return spinning;
}
static void
fddeselect(void)
{
ym2149_fd_select(PA_FDSEL);
motoron = selected = 0;
DMA->dma_drvmode = 0;
}
static void
fd_xfer(struct fd_softc *sc)
{
int head;
int track, sector, hbit;
paddr_t phys_addr;
head = track = 0;
switch (fd_state) {
case FLP_XFER:
track = sc->sector / sc->nsectors;
head = track % sc->nheads;
track = track / sc->nheads;
#ifdef FLP_DEBUG
printf("fd_xfer: sector:%d,head:%d,track:%d\n",
sc->sector, head, track);
#endif
break;
case FLP_STAT:
sc->curtrk = INV_TRK;
break;
default:
panic("fd_xfer: wrong state (0x%x)", fd_state);
}
hbit = fdselect(sc->unit, head, sc->density) ? HBIT : 0;
if (sc->curtrk == INV_TRK) {
fd_cmd = RESTORE;
write_fdreg(FDC_CS, RESTORE|VBIT|hbit);
callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY,
(FPV)fdmotoroff, sc);
#ifdef FLP_DEBUG
printf("fd_xfer:Recalibrating drive %d\n", sc->unit);
#endif
return;
}
write_fdreg(FDC_TR, sc->curtrk);
if (track != sc->curtrk) {
sc->curtrk = track;
write_fdreg(FDC_DR, track);
write_fdreg(FDC_CS, SEEK|RATE6|VBIT|hbit);
callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY,
(FPV)fdmotoroff, sc);
fd_cmd = SEEK;
#ifdef FLP_DEBUG
printf("fd_xfer:Seek to track %d on drive %d\n",
track, sc->unit);
#endif
return;
}
sector = sc->sector % sc->nsectors;
sector++;
write_fdreg(FDC_SR, sector);
phys_addr = (paddr_t)kvtop(sc->io_data);
if (phys_addr >= FDC_MAX_DMA_AD) {
phys_addr = (paddr_t)kvtop(sc->bounceb);
if (sc->io_dir == B_WRITE)
memcpy(sc->bounceb, sc->io_data, SECTOR_SIZE);
sc->flags |= FLPF_BOUNCE;
}
st_dmaaddr_set((void *)phys_addr);
#ifdef FLP_DEBUG
printf("fd_xfer:Start io (io_addr:%lx)\n", (u_long)kvtop(sc->io_data));
#endif
if (sc->io_dir == B_READ) {
st_dmacomm(DMA_FDC | DMA_SCREG, 1);
write_fdreg(FDC_CS, F_READ|hbit);
fd_cmd = F_READ;
} else {
st_dmacomm(DMA_WRBIT | DMA_FDC | DMA_SCREG, 1);
write_fdreg(DMA_WRBIT | FDC_CS, F_WRITE|hbit|EBIT|PBIT);
fd_cmd = F_WRITE;
}
callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY, (FPV)fdmotoroff, sc);
}
#define X_OK 0
#define X_AGAIN 1
#define X_ERROR 2
#define X_FAIL 3
static void
fdcint(struct fd_softc *sc)
{
struct buf *bp;
#ifdef FLP_DEBUG
printf("fdcint: unit = %d\n", sc->unit);
#endif
callout_stop(&sc->sc_motor_ch);
switch (fd_xfer_ok(sc)) {
case X_ERROR:
if (++sc->errcnt < MAX_ERRORS) {
break;
}
case X_FAIL:
if (fd_error != NULL) {
printf("Floppy error: %s\n", fd_error);
fd_error = NULL;
}
if (fd_state == FLP_STAT) {
sc->flags |= FLPF_EMPTY;
sc->flags &= ~FLPF_GETSTAT;
wakeup((void *)sc);
fddone(sc);
return;
}
bp = bufq_peek(sc->bufq);
bp->b_error = EIO;
fd_state = FLP_MON;
break;
case X_AGAIN:
break;
case X_OK:
sc->errcnt = 0;
if (fd_state == FLP_STAT) {
sc->flags &= ~FLPF_GETSTAT;
wakeup((void *)sc);
fddone(sc);
return;
}
if ((sc->flags & FLPF_BOUNCE) != 0 &&
sc->io_dir == B_READ)
memcpy(sc->io_data, sc->bounceb, SECTOR_SIZE);
sc->flags &= ~FLPF_BOUNCE;
sc->sector++;
sc->io_data += SECTOR_SIZE;
sc->io_bytes -= SECTOR_SIZE;
if (sc->io_bytes <= 0)
fd_state = FLP_MON;
}
if (fd_state == FLP_MON)
fddone(sc);
else
fd_xfer(sc);
}
static int
fd_xfer_ok(register struct fd_softc *sc)
{
int status;
#ifdef FLP_DEBUG
printf("fd_xfer_ok: cmd: 0x%x, state: 0x%x\n", fd_cmd, fd_state);
#endif
switch (fd_cmd) {
case IRUPT:
status = read_fdreg(FDC_CS);
fd_error = "Timeout";
sc->curtrk = INV_TRK;
return X_ERROR;
case F_READ:
status = read_dmastat();
if ((status & DMAOK) == 0) {
fd_error = "DMA error";
return X_ERROR;
}
status = read_fdreg(FDC_CS);
if ((status & (RNF | CRCERR | LD_T00)) != 0) {
fd_error = "Read error";
if ((status & RNF) != 0)
sc->curtrk = INV_TRK;
return X_ERROR;
}
break;
case F_WRITE:
status = read_dmastat();
if ((status & DMAOK) == 0) {
fd_error = "DMA error";
return X_ERROR;
}
status = read_fdreg(FDC_CS);
if ((status & WRI_PRO) != 0) {
fd_error = "Write protected";
return X_FAIL;
}
if ((status & (RNF | CRCERR | LD_T00)) != 0) {
fd_error = "Write error";
sc->curtrk = INV_TRK;
return X_ERROR;
}
break;
case SEEK:
status = read_fdreg(FDC_CS);
if ((status & (RNF | CRCERR)) != 0) {
fd_error = "Seek error";
sc->curtrk = INV_TRK;
return X_ERROR;
}
return X_AGAIN;
case RESTORE:
status = read_fdreg(FDC_CS);
if ((status & RNF) != 0) {
fd_error = "Recalibrate error";
write_fdreg(FDC_CS, IRUPT);
sc->curtrk = INV_TRK;
return X_ERROR;
}
sc->curtrk = 0;
if (fd_state == FLP_STAT) {
if ((status & WRI_PRO) != 0)
sc->flags |= FLPF_WRTPROT;
break;
}
return X_AGAIN;
default:
fd_error = "Driver error: fd_xfer_ok : Unknown state";
return X_FAIL;
}
return X_OK;
}
static void
fdmotoroff(struct fd_softc *sc)
{
int s;
s = splbio();
#if FLP_DEBUG
printf("fdmotoroff, state = 0x%x\n", fd_state);
#endif
switch (fd_state) {
case FLP_STAT:
case FLP_XFER:
fd_cmd = IRUPT;
write_fdreg(FDC_CS, IRUPT);
delay(20);
(void)read_fdreg(FDC_CS);
write_fdreg(FDC_CS, RESTORE);
break;
case FLP_MON:
if (selected) {
int tmp;
st_dmagrab((dma_farg)fdcint, (dma_farg)fdmoff, sc,
&tmp, 0, NULL);
} else
fd_state = FLP_IDLE;
break;
}
splx(s);
}
static void
fdminphys(struct buf *bp)
{
struct fd_softc *sc;
int sec, toff, tsz;
if ((sc = device_lookup_private(&fd_cd, DISKUNIT(bp->b_dev))) == NULL)
panic("fdminphys: couldn't get softc");
sec = bp->b_blkno % (sc->nsectors * sc->nheads);
toff = sec * SECTOR_SIZE;
tsz = sc->nsectors * sc->nheads * SECTOR_SIZE;
#ifdef FLP_DEBUG
printf("fdminphys: before %d", bp->b_bcount);
#endif
bp->b_bcount = uimin(bp->b_bcount, tsz - toff);
#ifdef FLP_DEBUG
printf(" after %d\n", bp->b_bcount);
#endif
minphys(bp);
}
static void
fdmoff(struct fd_softc *fdsoftc)
{
int tmp;
if ((fd_state == FLP_MON) && selected) {
tmp = read_fdreg(FDC_CS);
if ((tmp & MOTORON) == 0) {
fddeselect();
fd_state = FLP_IDLE;
} else
callout_reset(&fdsoftc->sc_motor_ch, 10 * FLP_MONDELAY,
(FPV)fdmotoroff, fdsoftc);
}
st_dmafree(fdsoftc, &tmp);
}
static void
fdtestdrv(struct fd_softc *fdsoftc)
{
int status;
fdselect(fdsoftc->unit, 0, FLP_DD);
write_fdreg(FDC_CS, RESTORE|HBIT);
delay(2000000);
status = read_fdreg(FDC_CS);
if ((status & (RNF|BUSY)) != 0) {
write_fdreg(FDC_CS, IRUPT);
delay(40);
}
if ((status & LD_T00) == 0)
fdsoftc->flags |= FLPF_NOTRESP;
fddeselect();
}
static void
fdgetdefaultlabel(struct fd_softc *sc, struct disklabel *lp, int part)
{
memset(lp, 0, sizeof(struct disklabel));
lp->d_secsize = SECTOR_SIZE;
lp->d_ntracks = sc->nheads;
lp->d_nsectors = sc->nsectors;
lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
lp->d_ncylinders = sc->nblocks / lp->d_secpercyl;
lp->d_secperunit = sc->nblocks;
lp->d_type = DKTYPE_FLOPPY;
lp->d_rpm = 300;
lp->d_interleave = 1;
lp->d_bbsize = 0;
lp->d_sbsize = 0;
lp->d_npartitions = part + 1;
lp->d_trkseek = STEP_DELAY;
lp->d_magic = DISKMAGIC;
lp->d_magic2 = DISKMAGIC;
lp->d_checksum = dkcksum(lp);
lp->d_partitions[part].p_size = lp->d_secperunit;
lp->d_partitions[part].p_fstype = FS_UNUSED;
lp->d_partitions[part].p_fsize = 1024;
lp->d_partitions[part].p_frag = 8;
}
static int
fdgetdisklabel(struct fd_softc *sc, dev_t dev)
{
struct disklabel *lp;
int part;
if ((sc->flags & FLPF_HAVELAB) != 0)
return 0;
#ifdef FLP_DEBUG
printf("fdgetdisklabel()\n");
#endif
part = RAW_PART;
lp = sc->dkdev.dk_label;
fdgetdefaultlabel(sc, lp, part);
sc->flags |= FLPF_HAVELAB;
return 0;
}