/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Charles M. Hannum.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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.
*/
/*-
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Don Ahn.
*
* 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. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
*
* @(#)fd.c 7.4 (Berkeley) 5/25/91
*/
/*
* Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
* we tell them apart.
*/
struct fd_type {
int sectrac; /* sectors per track */
int heads; /* number of heads */
int seccyl; /* sectors per cylinder */
int secsize; /* size code for sectors */
int datalen; /* data len when secsize = 0 */
int steprate; /* step rate and head unload time */
int gap1; /* gap len between sectors */
int gap2; /* formatting gap */
int cyls; /* total num of cylinders */
int size; /* size of disk in sectors */
int step; /* steps per cylinder */
int rate; /* transfer speed code */
const char *name;
};
/* The order of entries in the following table is important -- BEWARE! */
const static struct fd_type fd_types[] = {
/* 1.44MB diskette */
{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB" },
/* 1.2 MB AT-diskettes */
{ 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB" },
/* 360kB in 1.2MB drive */
{ 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" },
/* 360kB PC diskettes */
{ 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" },
/* 3.5" 720kB diskette */
{ 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB" },
/* 720kB in 1.2MB drive */
{ 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x" },
/* 360kB in 720kB drive */
{ 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x" },
};
/* software state, per disk (with up to 4 disks per ctlr) */
struct fd_softc {
device_t sc_dev;
struct disk sc_dk;
const struct fd_type *sc_deftype; /* default type descriptor */
struct fd_type *sc_type; /* current type descriptor */
struct fd_type sc_type_copy; /* copy for fiddling when formatting */
daddr_t sc_blkno; /* starting block number */
int sc_bcount; /* byte count left */
int sc_opts; /* user-set options */
int sc_skip; /* bytes already transferred */
int sc_nblks; /* number of blocks currently transferring */
int sc_nbytes; /* number of bytes currently transferring */
int sc_drive; /* physical unit number */
int sc_flags;
#define FD_OPEN 0x01 /* it's open */
#define FD_MOTOR 0x02 /* motor should be on */
#define FD_MOTOR_WAIT 0x04 /* motor coming up */
int sc_cylin; /* where we think the head is */
TAILQ_ENTRY(fd_softc) sc_drivechain;
int sc_ops; /* I/O ops since last switch */
struct bufq_state *sc_q;/* pending I/O requests */
int sc_active; /* number of active I/O operations */
};
/*
* Arguments passed between fdcattach and fdprobe.
*/
struct fdc_attach_args {
int fa_drive;
const struct fd_type *fa_deftype;
};
/*
* Print the location of a disk drive (called just before attaching the
* the drive). If `fdc' is not NULL, the drive was found but was not
* in the system config file; print the drive name as well.
* Return QUIET (config_find ignores this if the device was configured) to
* avoid printing `fdN not configured' messages.
*/
static int
fdprint(void *aux, const char *fdc)
{
struct fdc_attach_args *fa = aux;
/*
* Initialize and attach the disk structure.
*/
disk_init(&fd->sc_dk, device_xname(fd->sc_dev), &fddkdriver);
disk_attach(&fd->sc_dk);
/* Establish a mountroot hook. */
mountroothook_establish(fd_mountroot_hook, fd->sc_dev);
/* Needed to power off if the motor is on when we halt. */
if (!pmf_device_register1(self, NULL, NULL, fd_shutdown))
aprint_error_dev(self, "couldn't establish power handler\n");
}
bool
fd_shutdown(device_t self, int howto)
{
struct fd_softc *fd;
fd = device_private(self);
fd_motor_off(fd);
return true;
}
#if 0
/*
* Translate nvram type into internal data structure. Return NULL for
* none/unknown/unusable.
*/
static const struct fd_type *
fd_nvtotype(char *fdc, int nvraminfo, int drive)
{
int type;
type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
#if 0
switch (type) {
case NVRAM_DISKETTE_NONE:
return NULL;
case NVRAM_DISKETTE_12M:
return &fd_types[1];
case NVRAM_DISKETTE_TYPE5:
case NVRAM_DISKETTE_TYPE6:
/* XXX We really ought to handle 2.88MB format. */
case NVRAM_DISKETTE_144M:
return &fd_types[0];
case NVRAM_DISKETTE_360K:
return &fd_types[3];
case NVRAM_DISKETTE_720K:
return &fd_types[4];
default:
printf("%s: drive %d: unknown device type 0x%x\n",
fdc, drive, type);
return NULL;
}
#else
return &fd_types[0]; /* Use only 1.44 for now */
#endif
}
#endif
static const struct fd_type *
fd_dev_to_type(struct fd_softc *fd, dev_t dev)
{
int type = FDTYPE(dev);
if (type > __arraycount(fd_types))
return NULL;
return type ? &fd_types[type - 1] : fd->sc_deftype;
}
void
fdstrategy(struct buf *bp)
{
struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(bp->b_dev));
int sz;
int s;
/* Valid unit, controller, and request? */
if (bp->b_blkno < 0 ||
(bp->b_bcount % FDC_BSIZE) != 0) {
bp->b_error = EINVAL;
goto done;
}
/* If it's a null transfer, return immediately. */
if (bp->b_bcount == 0)
goto done;
sz = howmany(bp->b_bcount, FDC_BSIZE);
if (bp->b_blkno + sz > fd->sc_type->size) {
sz = fd->sc_type->size - bp->b_blkno;
if (sz == 0) {
/* If exactly at end of disk, return EOF. */
goto done;
}
if (sz < 0) {
/* If past end of disk, return EINVAL. */
bp->b_error = EINVAL;
goto done;
}
/* Otherwise, truncate request. */
bp->b_bcount = sz << DEV_BSHIFT;
}
/*
* Move this drive to the end of the queue to give others a `fair'
* chance. We only force a switch if N operations are completed while
* another drive is waiting to be serviced, since there is a long motor
* startup delay whenever we switch.
*/
(void)bufq_get(fd->sc_q);
if (TAILQ_NEXT(fd, sc_drivechain) && ++fd->sc_ops >= 8) {
fd->sc_ops = 0;
TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
if (bufq_peek(fd->sc_q) != NULL)
TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
else
fd->sc_active = 0;
}
bp->b_resid = fd->sc_bcount;
fd->sc_skip = 0;
biodone(bp);
/* turn off motor 5s from now */
callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
fdc->sc_state = DEVIDLE;
}
int
fdread(dev_t dev, struct uio *uio, int flags)
{
void
fd_set_motor(struct fdc_softc *fdc, int reset)
{
struct fd_softc *fd;
u_char status;
int n;
if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
status = fd->sc_drive;
else
status = 0;
if (!reset)
status |= FDO_FRST | FDO_FDMAEN;
for (n = 0; n < 4; n++)
if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
status |= FDO_MOEN(n);
bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, FDOUT, status);
}
int
fdclose(dev_t dev, int flags, int mode, struct lwp *l)
{
struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(dev));
fd->sc_flags &= ~FD_OPEN;
return 0;
}
void
fdcstart(struct fdc_softc *fdc)
{
#ifdef DIAGNOSTIC
/* only got here if controller's drive queue was inactive; should
be in idle state */
if (fdc->sc_state != DEVIDLE) {
printf("%s: not idle\n", __func__);
return;
}
#endif
(void)fdcintr(fdc);
}
static void
fdcpstatus(int n, struct fdc_softc *fdc)
{
char bits[64];
loop:
/* Is there a drive for the controller to do a transfer with? */
fd = TAILQ_FIRST(&fdc->sc_drives);
if (fd == NULL) {
fdc->sc_state = DEVIDLE;
return 1;
}
/* Is there a transfer to this drive? If not, deactivate drive. */
bp = bufq_peek(fd->sc_q);
if (bp == NULL) {
fd->sc_ops = 0;
TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
fd->sc_active = 0;
goto loop;
}
switch (fdc->sc_state) {
case DEVIDLE:
fdc->sc_errors = 0;
fd->sc_skip = 0;
fd->sc_bcount = bp->b_bcount;
fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
callout_stop(&fd->sc_motoroff_ch);
if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
fdc->sc_state = MOTORWAIT;
return 1;
}
if ((fd->sc_flags & FD_MOTOR) == 0) {
/* Turn on the motor, being careful about pairing. */
struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
if (ofd && ofd->sc_flags & FD_MOTOR) {
callout_stop(&ofd->sc_motoroff_ch);
ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
}
fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
fd_set_motor(fdc, 0);
fdc->sc_state = MOTORWAIT;
/* Allow .25s for motor to stabilize. */
callout_reset(&fd->sc_motoron_ch, hz / 4,
fd_motor_on, fd);
return 1;
}
/* Make sure the right drive is selected. */
fd_set_motor(fdc, 0);
/* fall through */
case DOSEEK:
doseek:
if (fd->sc_cylin == bp->b_cylinder)
goto doio;
out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
out_fdc(iot, ioh, fd->sc_type->steprate);
out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */
out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */
out_fdc(iot, ioh, fd->sc_drive); /* drive number */
out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
case DORESET:
/* try a reset, keep motor on */
fd_set_motor(fdc, 1);
delay(100);
fd_set_motor(fdc, 0);
fdc->sc_state = RESETCOMPLETE;
callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
return 1; /* will return later */
case RESETCOMPLETE:
callout_stop(&fdc->sc_timo_ch);
/* clear the controller output buffer */
for (i = 0; i < 4; i++) {
out_fdc(iot, ioh, NE7CMD_SENSEI);
(void)fdcresult(fdc);
}
/* fall through */
case DORECAL:
out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
out_fdc(iot, ioh, fd->sc_drive);
fdc->sc_state = RECALWAIT;
callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
return 1; /* will return later */
case RECALWAIT:
callout_stop(&fdc->sc_timo_ch);
fdc->sc_state = RECALCOMPLETE;
/* allow 1/30 second for heads to settle */
callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc);
return 1; /* will return later */