/*
* Copyright (c) 1995 Leo Weppelman.
* 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.
*
* 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.
*/
/*
* This file contains a driver for the Floppy Disk Controller (FDC)
* on the Atari TT. It uses the WD 1772 chip, modified for steprates.
*
* The ST floppy disk controller shares the access to the DMA circuitry
* with other devices. For this reason the floppy disk controller makes
* use of some special DMA accessing code.
*
* Interrupts from the FDC are in fact DMA interrupts which get their
* first level handling in 'dma.c' . If the floppy driver is currently
* using DMA the interrupt is signalled to 'fdcint'.
*
* TODO:
* - Test it with 2 drives (I don't have them)
* - Test it with an HD-drive (Don't have that either)
* - Finish ioctl's
*/
/*
* Be verbose for debugging
*/
/*#define FLP_DEBUG 1 */
#define FDC_MAX_DMA_AD 0x1000000 /* No DMA possible beyond */
/* Parameters for the disk drive. */
#define SECTOR_SIZE 512 /* physical sector size in bytes */
#define NR_DRIVES 2 /* maximum number of drives */
#define NR_TYPES 3 /* number of diskette/drive combinations*/
#define MAX_ERRORS 10 /* how often to try rd/wt before quitting*/
#define STEP_DELAY 6000 /* 6ms (6000us) delay after stepping */
#define INV_TRK 32000 /* Should fit in unsigned short */
#define INV_PART NR_TYPES
/*
* Driver states
*/
#define FLP_IDLE 0x00 /* floppy is idle */
#define FLP_MON 0x01 /* idle with motor on */
#define FLP_STAT 0x02 /* determine floppy status */
#define FLP_XFER 0x04 /* read/write data from floppy */
/*
* The density codes
*/
#define FLP_DD 0 /* Double density */
#define FLP_HD 1 /* High density */
#define b_block b_resid /* FIXME: this is not the place */
/*
* Global data for all physical floppy devices
*/
static short selected = 0; /* drive/head currently selected*/
static short motoron = 0; /* motor is spinning */
static short nopens = 0; /* Number of opens executed */
static short fd_state = FLP_IDLE; /* Current driver state */
static int lock_stat = 0; /* DMA locking status */
static short fd_cmd = 0; /* command being executed */
static const char *fd_error = NULL; /* error from fd_xfer_ok() */
/*
* Private per device data
*/
struct fd_softc {
device_t sc_dev; /* generic device info */
struct disk dkdev; /* generic disk info */
struct bufq_state *bufq; /* queue of buf's */
struct callout sc_motor_ch;
int unit; /* unit for atari controlling hw*/
int nheads; /* number of heads in use */
int nsectors; /* number of sectors/track */
int density; /* density code */
int nblocks; /* number of blocks on disk */
int curtrk; /* track head positioned on */
short flags; /* misc flags */
short part; /* Current open partition */
int sector; /* logical sector for I/O */
uint8_t *io_data; /* KVA for data transfer */
int io_bytes; /* bytes left for I/O */
int io_dir; /* B_READ/B_WRITE */
int errcnt; /* current error count */
uint8_t *bounceb; /* Bounce buffer */
};
/*
* Flags in fd_softc:
*/
#define FLPF_NOTRESP 0x001 /* Unit not responding */
#define FLPF_ISOPEN 0x002 /* Unit is open */
#define FLPF_SPARE 0x004 /* Not used */
#define FLPF_HAVELAB 0x008 /* We have a valid label */
#define FLPF_BOUNCE 0x010 /* Now using the bounce buffer */
#define FLPF_WRTPROT 0x020 /* Unit is write-protected */
#define FLPF_EMPTY 0x040 /* Unit is empty */
#define FLPF_INOPEN 0x080 /* Currently being opened */
#define FLPF_GETSTAT 0x100 /* Getting unit status */
struct fd_types {
int nheads; /* Heads in use */
int nsectors; /* sectors per track */
int nblocks; /* number of blocks */
int density; /* density code */
const char *descr; /* type description */
} fdtypes[NR_TYPES] = {
{ 1, 9, 720 , FLP_DD , "360KB" }, /* 360 Kb */
{ 2, 9, 1440 , FLP_DD , "720KB" }, /* 720 Kb */
{ 2, 18, 2880 , FLP_HD , "1.44MB" }, /* 1.44 Mb */
};
#define FLP_TYPE_360 0 /* XXX: Please keep these in */
#define FLP_TYPE_720 1 /* sync with the numbering in */
#define FLP_TYPE_144 2 /* 'fdtypes' right above! */
/*
* This is set only once at attach time. The value is determined by reading
* the configuration switches and is one of the FLP_TYPE_*'s.
* This is similar to the way Atari handles the _FLP cookie.
*/
static short def_type = 0; /* Reflects config-switches */
/*
* Config switch stuff. Used only for the floppy type for now. That's
* why it's here...
* XXX: If needed in more places, it should be moved to its own include file.
* Note: This location _must_ be read as an u_short. Failure to do so
* will return garbage!
*/
static u_short
rd_cfg_switch(void)
{
return *(volatile u_short *)AD_CFG_SWITCH;
}
/*
* Switch definitions.
* Note: ON reads as a zero bit!
*/
#define CFG_SWITCH_NOHD 0x4000
/*
* Make sure motor will be turned of when a floppy is
* inserted in the first selected drive.
*/
fdselect(first_found, 0, FLP_DD);
fd_state = FLP_MON;
callout_reset(&fdsc->sc_motor_ch, 0, (FPV)fdmotoroff, fdsc);
/*
* enable disk related interrupts
*/
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);
/*
* Find out if an Ajax chip might be installed. Set the default
* floppy type accordingly.
*/
swtch = rd_cfg_switch();
def_type = (swtch & CFG_SWITCH_NOHD) ? FLP_TYPE_720 : FLP_TYPE_144;
type = &fdtypes[def_type];
switch (cmd) {
case DIOCSBAD:
return EINVAL;
#ifdef notyet /* XXX LWP */
case DIOCSRETRIES:
case DIOCSSTEP:
case DIOCSDINFO:
case DIOCWDINFO:
case DIOCWLABEL:
break;
#endif /* notyet */
case DIOCGDEFLABEL:
fdgetdefaultlabel(sc, (struct disklabel *)addr, RAW_PART);
return 0;
}
return ENOTTY;
}
/*
* Open the device. If this is the first open on both the floppy devices,
* initialize the controller.
* Note that partition info on the floppy device is used to distinguise
* between 780Kb and 360Kb floppy's.
* partition 0: 360Kb
* partition 1: 780Kb
*/
static int
fdopen(dev_t dev, int flags, int devtype, struct lwp *l)
{
struct fd_softc *sc;
int s;
/*
* Sleep while other process is opening the device
*/
s = splbio();
while (sc->flags & FLPF_INOPEN)
tsleep((void *)sc, PRIBIO, "fdopen", 0);
splx(s);
if ((sc->flags & FLPF_ISOPEN) == 0) {
/*
* Initialise some driver values.
*/
int type;
void *addr;
/*
* Called through the DMA-dispatcher. So we know we are the only ones
* messing with the floppy-controller.
* Initialize some fields in the fdsoftc for the state-machine and get
* it going.
*/
static void
fdstart(struct fd_softc *sc)
{
struct buf *bp;
bp = bufq_peek(sc->bufq);
sc->sector = bp->b_blkno; /* Start sector for I/O */
sc->io_data = bp->b_data; /* KVA base for I/O */
sc->io_bytes = bp->b_bcount; /* Transfer size in bytes */
sc->io_dir = bp->b_flags & B_READ;/* Direction of transfer */
sc->errcnt = 0; /* No errors yet */
fd_state = FLP_XFER; /* Yes, we're going to transfer */
/* Instrumentation. */
disk_busy(&sc->dkdev);
fd_xfer(sc);
}
/*
* The current transaction is finished (for good or bad). Let go of
* the DMA-resources. Call biodone() to finish the transaction.
* Find a new transaction to work on.
*/
static void
fddone(register struct fd_softc *sc)
{
struct buf *bp;
struct fd_softc *sc1;
int i, s;
/*
* Give others a chance to use the DMA.
*/
st_dmafree(sc, &lock_stat);
if (fd_state != FLP_STAT) {
/*
* Finish current transaction.
*/
s = splbio();
bp = bufq_get(sc->bufq);
if (bp == NULL)
panic("fddone");
splx(s);
if (lock_stat)
return; /* XXX Is this possible? */
/*
* Find a new transaction on round-robin basis.
*/
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; /* No work */
}
}
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;
if (sc->curtrk == INV_TRK) {
/*
* Recalibrate, since we lost track of head positioning.
* The floppy disk controller has no way of determining its
* absolute arm position (track). Instead, it steps the
* arm a track at a time and keeps track of where it
* thinks it is (in software). However, after a SEEK, the
* hardware reads information from the diskette telling
* where the arm actually is. If the arm is in the wrong place,
* a recalibration is done, which forces the arm to track 0.
* This way the controller can get back into sync with reality.
*/
fd_cmd = RESTORE;
write_fdreg(FDC_CS, RESTORE|VBIT|hbit);
callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY,
(FPV)fdmotoroff, sc);
/*
* Issue a SEEK command on the indicated drive unless the arm is
* already positioned on the correct track.
*/
if (track != sc->curtrk) {
sc->curtrk = track; /* be optimistic */
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;
}
/*
* The drive is now on the proper track. Read or write 1 block.
*/
sector = sc->sector % sc->nsectors;
sector++; /* start numbering at 1 */
write_fdreg(FDC_SR, sector);
phys_addr = (paddr_t)kvtop(sc->io_data);
if (phys_addr >= FDC_MAX_DMA_AD) {
/*
* We _must_ bounce this address
*/
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); /* DMA address setup */
switch (fd_xfer_ok(sc)) {
case X_ERROR:
if (++sc->errcnt < MAX_ERRORS) {
/*
* Command failed but still retries left.
*/
break;
}
/* FALL THROUGH */
case X_FAIL:
/*
* Non recoverable error. Fall back to motor-on
* idle-state.
*/
if (fd_error != NULL) {
printf("Floppy error: %s\n", fd_error);
fd_error = NULL;
}
break;
case X_AGAIN:
/*
* Start next part of state machine.
*/
break;
case X_OK:
/*
* Command ok and finished. Reset error-counter.
* If there are no more bytes to transfer fall back
* to motor-on idle state.
*/
sc->errcnt = 0;
/*
* Determine status of last command. Should only be called through
* 'fdcint()'.
* Returns:
* X_ERROR : Error on command; might succeed next time.
* X_FAIL : Error on command; will never succeed.
* X_AGAIN : Part of a command succeeded, call 'fd_xfer()' to complete.
* X_OK : Command succeeded and is complete.
*
* This function only affects sc->curtrk.
*/
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:
/*
* Timeout. Force a recalibrate before we try again.
*/
status = read_fdreg(FDC_CS);
fd_error = "Timeout";
sc->curtrk = INV_TRK;
return X_ERROR;
case F_READ:
/*
* Test for DMA error
*/
status = read_dmastat();
if ((status & DMAOK) == 0) {
fd_error = "DMA error";
return X_ERROR;
}
/*
* Get controller status and check for errors.
*/
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:
/*
* Test for DMA error
*/
status = read_dmastat();
if ((status & DMAOK) == 0) {
fd_error = "DMA error";
return X_ERROR;
}
/*
* Get controller status and check for errors.
*/
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:
/*
* Determine if the recalibration succeeded.
*/
status = read_fdreg(FDC_CS);
if ((status & RNF) != 0) {
fd_error = "Recalibrate error";
/* reset controller */
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;
}
/*
* All timeouts will call this function.
*/
static void
fdmotoroff(struct fd_softc *sc)
{
int s;
/*
* Get at hardware interrupt level
*/
s = splbio();
#if FLP_DEBUG
printf("fdmotoroff, state = 0x%x\n", fd_state);
#endif
switch (fd_state) {
case FLP_STAT:
case FLP_XFER:
/*
* Timeout during a transfer; cancel transaction
* set command to 'IRUPT'.
* A drive-interrupt is simulated to trigger the state
* machine.
*/
/*
* Cancel current transaction
*/
fd_cmd = IRUPT;
write_fdreg(FDC_CS, IRUPT);
delay(20);
(void)read_fdreg(FDC_CS);
write_fdreg(FDC_CS, RESTORE);
break;
case FLP_MON:
/*
* Turn motor off.
*/
if (selected) {
int tmp;
#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);
}
/*
* Called from fdmotoroff to turn the motor actually off....
* This can't be done in fdmotoroff itself, because exclusive access to the
* DMA controller is needed to read the FDC-status register. The function
* 'fdmoff()' always runs as the result of a 'dmagrab()'.
* We need to test the status-register because we want to be sure that the
* drive motor is really off before deselecting the drive. The FDC only
* turns off the drive motor after having seen 10 index-pulses. You only
* get index-pulses when a drive is selected....This means that if the
* drive is deselected when the motor is still spinning, it will continue
* to spin _even_ when you insert a floppy later on...
*/
static void
fdmoff(struct fd_softc *fdsoftc)
{
int tmp;
/*
* Used to find out which drives are actually connected. We do this by issuing
* is 'RESTORE' command and check if the 'track-0' bit is set. This also works
* if the drive is present but no floppy is inserted.
*/
static void
fdtestdrv(struct fd_softc *fdsoftc)
{
int status;
/*
* Select the right unit and head.
*/
fdselect(fdsoftc->unit, 0, FLP_DD);
write_fdreg(FDC_CS, RESTORE|HBIT);
/*
* Wait for about 2 seconds.
*/
delay(2000000);
status = read_fdreg(FDC_CS);
if ((status & (RNF|BUSY)) != 0) {
write_fdreg(FDC_CS, IRUPT); /* reset controller */
delay(40);
}
if ((status & LD_T00) == 0)
fdsoftc->flags |= FLPF_NOTRESP;
/*
* Build disk label. For now we only create a label from what we know
* from 'sc'.
*/
static int
fdgetdisklabel(struct fd_softc *sc, dev_t dev)
{
struct disklabel *lp;
int part;
/*
* If we already got one, get out.
*/
if ((sc->flags & FLPF_HAVELAB) != 0)
return 0;