Home | History | Annotate | Line # | Download | only in dev
fd.c revision 1.11
      1 /*	$NetBSD: fd.c,v 1.11 1995/10/14 20:17:46 leo Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Leo Weppelman.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Leo Weppelman.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * This file contains a driver for the Floppy Disk Controller (FDC)
     35  * on the Atari TT. It uses the WD 1772 chip, modified for steprates.
     36  *
     37  * The ST floppy disk controller shares the access to the DMA circuitry
     38  * with other devices. For this reason the floppy disk controller makes
     39  * use of some special DMA accessing code.
     40  *
     41  * Interrupts from the FDC are in fact DMA interrupts which get their
     42  * first level handling in 'dma.c' . If the floppy driver is currently
     43  * using DMA the interrupt is signalled to 'fdcint'.
     44  *
     45  * TODO:
     46  *   - Test it with 2 drives (I don't have them)
     47  *   - Test it with an HD-drive (Don't have that either)
     48  *   - Finish ioctl's
     49  */
     50 
     51 #include	<sys/param.h>
     52 #include	<sys/systm.h>
     53 #include	<sys/kernel.h>
     54 #include	<sys/malloc.h>
     55 #include	<sys/buf.h>
     56 #include	<sys/device.h>
     57 #include	<sys/ioctl.h>
     58 #include	<sys/fcntl.h>
     59 #include	<sys/conf.h>
     60 #include	<sys/disklabel.h>
     61 #include	<sys/disk.h>
     62 #include	<sys/dkbad.h>
     63 #include	<atari/atari/device.h>
     64 #include	<machine/disklabel.h>
     65 #include	<machine/iomap.h>
     66 #include	<machine/mfp.h>
     67 #include	<machine/dma.h>
     68 #include	<machine/video.h>
     69 #include	<atari/dev/fdreg.h>
     70 
     71 /*
     72  * Be verbose for debugging
     73  */
     74 /*#define FLP_DEBUG	1 */
     75 
     76 #define	FDC_MAX_DMA_AD	0x1000000	/* No DMA possible beyond	*/
     77 
     78 /* Parameters for the disk drive. */
     79 #define SECTOR_SIZE	512	/* physical sector size in bytes	*/
     80 #define NR_DRIVES	2	/* maximum number of drives		*/
     81 #define NR_TYPES	3	/* number of diskette/drive combinations*/
     82 #define MAX_ERRORS	10	/* how often to try rd/wt before quitting*/
     83 #define STEP_DELAY	6000	/* 6ms (6000us) delay after stepping	*/
     84 
     85 
     86 #define	INV_TRK		32000	/* Should fit in unsigned short		*/
     87 #define	INV_PART	NR_TYPES
     88 
     89 /*
     90  * Driver states
     91  */
     92 #define	FLP_IDLE	0x00	/* floppy is idle			*/
     93 #define	FLP_MON		0x01	/* idle with motor on			*/
     94 #define	FLP_STAT	0x02	/* determine floppy status		*/
     95 #define	FLP_XFER	0x04	/* read/write data from floppy		*/
     96 
     97 /*
     98  * Timer delay's
     99  */
    100 #define	FLP_MONDELAY	(3 * hz)	/* motor-on delay		*/
    101 #define	FLP_XFERDELAY	(2 * hz)	/* timeout on transfer		*/
    102 
    103 /*
    104  * The density codes
    105  */
    106 #define	FLP_DD		0		/* Double density		*/
    107 #define	FLP_HD		1		/* High density			*/
    108 
    109 
    110 #define	b_block		b_resid		/* FIXME: this is not the place	*/
    111 
    112 /*
    113  * Global data for all physical floppy devices
    114  */
    115 static short	selected = 0;		/* drive/head currently selected*/
    116 static short	motoron  = 0;		/* motor is spinning		*/
    117 static short	nopens   = 0;		/* Number of opens executed	*/
    118 
    119 static short	fd_state = FLP_IDLE;	/* Current driver state		*/
    120 static int	lock_stat= 0;		/* dma locking status		*/
    121 static short	fd_cmd   = 0;		/* command being executed	*/
    122 static char	*fd_error= NULL;	/* error from fd_xfer_ok()	*/
    123 
    124 /*
    125  * Private per device data
    126  */
    127 struct fd_softc {
    128 	struct dkdevice dkdev;
    129 	struct buf	bufq;		/* queue of buf's		*/
    130 	int		unit;		/* unit for atari controlling hw*/
    131 	int		nheads;		/* number of heads in use	*/
    132 	int		nsectors;	/* number of sectors/track	*/
    133 	int		density;	/* density code			*/
    134 	int		nblocks;	/* number of blocks on disk	*/
    135 	int		curtrk;		/* track head positioned on	*/
    136 	short		flags;		/* misc flags			*/
    137 	short		part;		/* Current open partition	*/
    138 	int		sector;		/* logical sector for I/O	*/
    139 	caddr_t		io_data;	/* KVA for data transfer	*/
    140 	int		io_bytes;	/* bytes left for I/O		*/
    141 	int		io_dir;		/* B_READ/B_WRITE		*/
    142 	int		errcnt;		/* current error count		*/
    143 	u_char		*bounceb;	/* Bounce buffer		*/
    144 
    145 };
    146 
    147 /*
    148  * Flags in fd_softc:
    149  */
    150 #define FLPF_NOTRESP	0x001		/* Unit not responding		*/
    151 #define FLPF_ISOPEN	0x002		/* Unit is open			*/
    152 #define FLPF_SPARE	0x004		/* Not used			*/
    153 #define FLPF_HAVELAB	0x008		/* We have a valid label	*/
    154 #define FLPF_BOUNCE	0x010		/* Now using the bounce buffer	*/
    155 #define FLPF_WRTPROT	0x020		/* Unit is write-protected	*/
    156 #define FLPF_EMPTY	0x040		/* Unit is empty		*/
    157 #define FLPF_INOPEN	0x080		/* Currently being opened	*/
    158 #define FLPF_GETSTAT	0x100		/* Getting unit status		*/
    159 
    160 struct fd_types {
    161 	int		nheads;		/* Heads in use			*/
    162 	int		nsectors;	/* sectors per track		*/
    163 	int		nblocks;	/* number of blocks		*/
    164 	int		density;	/* density code			*/
    165 } fdtypes[NR_TYPES] = {
    166 		{ 1,  9,  720 , FLP_DD },	/* 360  Kb	*/
    167 		{ 2,  9, 1440 , FLP_DD },	/* 720  Kb	*/
    168 		{ 2, 18, 2880 , FLP_HD },	/* 1.44 Mb	*/
    169 };
    170 
    171 typedef void	(*FPV)();
    172 
    173 /*
    174  * Private drive functions....
    175  */
    176 static void	fdstart __P((struct fd_softc *));
    177 static void	fddone __P((struct fd_softc *));
    178 static void	fdstatus __P((struct fd_softc *));
    179 static void	fd_xfer __P((struct fd_softc *));
    180 static void	fdcint __P((struct fd_softc *));
    181 static int	fd_xfer_ok __P((struct fd_softc *));
    182 static void	fdmotoroff __P((struct fd_softc *));
    183 static void	fdminphys __P((struct buf *));
    184 static void	fdtestdrv __P((struct fd_softc *));
    185 static int	fdgetdisklabel __P((struct fd_softc *, dev_t));
    186 static int	fdselect __P((int, int, int));
    187 static void	fddeselect __P((void));
    188 
    189 extern __inline__ u_char read_fdreg(u_short regno)
    190 {
    191 	DMA->dma_mode = regno;
    192 	return(DMA->dma_data);
    193 }
    194 
    195 extern __inline__ void write_fdreg(u_short regno, u_short val)
    196 {
    197 	DMA->dma_mode = regno;
    198 	DMA->dma_data = val;
    199 }
    200 
    201 extern __inline__ u_char read_dmastat(void)
    202 {
    203 	DMA->dma_mode = FDC_CS | DMA_SCREG;
    204 	return(DMA->dma_stat);
    205 }
    206 
    207 /*
    208  * Autoconfig stuff....
    209  */
    210 static int	fdcmatch __P((struct device *, struct cfdata *, void *));
    211 static int	fdcprint __P((void *, char *));
    212 static void	fdcattach __P((struct device *, struct device *, void *));
    213 
    214 struct cfdriver fdccd = {
    215 	NULL, "fdc", (cfmatch_t)fdcmatch, fdcattach, DV_DULL,
    216 	sizeof(struct device), NULL, 0 };
    217 
    218 static int
    219 fdcmatch(pdp, cfp, auxp)
    220 struct device	*pdp;
    221 struct cfdata	*cfp;
    222 void		*auxp;
    223 {
    224 	if(strcmp("fdc", auxp) || cfp->cf_unit != 0)
    225 		return(0);
    226 	return(1);
    227 }
    228 
    229 static void
    230 fdcattach(pdp, dp, auxp)
    231 struct device	*pdp, *dp;
    232 void		*auxp;
    233 {
    234 	struct fd_softc	fdsoftc;
    235 	int		i, nfound = 0;
    236 
    237 	printf("\n");
    238 	fddeselect();
    239 	for(i = 0; i < NR_DRIVES; i++) {
    240 
    241 		/*
    242 		 * Test if unit is present
    243 		 */
    244 		fdsoftc.unit  = i;
    245 		fdsoftc.flags = 0;
    246 		st_dmagrab(fdcint, fdtestdrv, &fdsoftc, &lock_stat, 0);
    247 		st_dmafree(&fdsoftc, &lock_stat);
    248 
    249 		if(!(fdsoftc.flags & FLPF_NOTRESP)) {
    250 			nfound++;
    251 			config_found(dp, (void*)i, fdcprint);
    252 		}
    253 	}
    254 
    255 	if(nfound) {
    256 		/*
    257 		 * enable disk related interrupts
    258 		 */
    259 		MFP->mf_ierb  |= IB_DINT;
    260 		MFP->mf_iprb  &= ~IB_DINT;
    261 		MFP->mf_imrb  |= IB_DINT;
    262 	}
    263 }
    264 
    265 static int
    266 fdcprint(auxp, pnp)
    267 void	*auxp;
    268 char	*pnp;
    269 {
    270 	return(UNCONF);
    271 }
    272 
    273 static int	fdmatch __P((struct device *, struct cfdata *, void *));
    274 static void	fdattach __P((struct device *, struct device *, void *));
    275 	   void fdstrategy __P((struct buf *));
    276 struct dkdriver fddkdriver = { fdstrategy };
    277 
    278 struct cfdriver fdcd = {
    279 	NULL, "fd", (cfmatch_t)fdmatch, fdattach, DV_DISK,
    280 	sizeof(struct fd_softc), NULL, 0 };
    281 
    282 static int
    283 fdmatch(pdp, cfp, auxp)
    284 struct device	*pdp;
    285 struct cfdata	*cfp;
    286 void		*auxp;
    287 {
    288 	int	unit = (int)auxp;
    289 	return(1);
    290 }
    291 
    292 static void
    293 fdattach(pdp, dp, auxp)
    294 struct device	*pdp, *dp;
    295 void		*auxp;
    296 {
    297 	struct fd_softc	*sc;
    298 
    299 	sc = (struct fd_softc *)dp;
    300 
    301 	printf("\n");
    302 
    303 	sc->dkdev.dk_driver = &fddkdriver;
    304 }
    305 
    306 fdioctl(dev, cmd, addr, flag, p)
    307 dev_t		dev;
    308 u_long		cmd;
    309 int		flag;
    310 caddr_t		addr;
    311 struct proc	*p;
    312 {
    313 	struct fd_softc *sc;
    314 	void		*data;
    315 
    316 	sc = getsoftc(fdcd, DISKUNIT(dev));
    317 
    318 	if((sc->flags & FLPF_HAVELAB) == 0)
    319 		return(EBADF);
    320 
    321 	switch(cmd) {
    322 		case DIOCSBAD:
    323 			return(EINVAL);
    324 		case DIOCGDINFO:
    325 			*(struct disklabel *)addr = sc->dkdev.dk_label;
    326 			return(0);
    327 		case DIOCGPART:
    328 			((struct partinfo *)addr)->disklab =
    329 				&sc->dkdev.dk_label;
    330 			((struct partinfo *)addr)->part =
    331 				&sc->dkdev.dk_label.d_partitions[DISKPART(dev)];
    332 			return(0);
    333 #ifdef notyet /* XXX LWP */
    334 		case DIOCSRETRIES:
    335 		case DIOCSSTEP:
    336 		case DIOCSDINFO:
    337 		case DIOCWDINFO:
    338 		case DIOCWLABEL:
    339 #endif /* notyet */
    340 		default:
    341 			return(ENOTTY);
    342 	}
    343 }
    344 
    345 /*
    346  * Open the device. If this is the first open on both the floppy devices,
    347  * intialize the controller.
    348  * Note that partition info on the floppy device is used to distinguise
    349  * between 780Kb and 360Kb floppy's.
    350  *	partition 0: 360Kb
    351  *	partition 1: 780Kb
    352  */
    353 Fdopen(dev, flags, devtype, proc)
    354 dev_t		dev;
    355 int		flags, devtype;
    356 struct proc	*proc;
    357 {
    358 	struct fd_softc	*sc;
    359 	int		sps;
    360 
    361 #ifdef FLP_DEBUG
    362 	printf("Fdopen dev=0x%x\n", dev);
    363 #endif
    364 
    365 	if(DISKPART(dev) >= NR_TYPES)
    366 		return(ENXIO);
    367 
    368 	if((sc = getsoftc(fdcd, DISKUNIT(dev))) == NULL)
    369 		return(ENXIO);
    370 
    371 	/*
    372 	 * If no floppy currently open, reset the controller and select
    373 	 * floppy type.
    374 	 */
    375 	if(!nopens) {
    376 
    377 #ifdef FLP_DEBUG
    378 		printf("Fdopen device not yet open\n");
    379 #endif
    380 		nopens++;
    381 		write_fdreg(FDC_CS, IRUPT);
    382 		delay(40);
    383 	}
    384 
    385 	/*
    386 	 * Sleep while other process is opening the device
    387 	 */
    388 	sps = splbio();
    389 	while(sc->flags & FLPF_INOPEN)
    390 		tsleep((caddr_t)sc, PRIBIO, "Fdopen", 0);
    391 	splx(sps);
    392 
    393 	if(!(sc->flags & FLPF_ISOPEN)) {
    394 		/*
    395 		 * Initialise some driver values.
    396 		 */
    397 		int	part = DISKPART(dev);
    398 		void	*addr;
    399 
    400 		sc->bufq.b_actf = NULL;
    401 		sc->unit        = DISKUNIT(dev);
    402 		sc->part        = part;
    403 		sc->nheads	= fdtypes[part].nheads;
    404 		sc->nsectors	= fdtypes[part].nsectors;
    405 		sc->nblocks     = fdtypes[part].nblocks;
    406 		sc->density	= fdtypes[part].density;
    407 		sc->curtrk	= INV_TRK;
    408 		sc->sector	= 0;
    409 		sc->errcnt	= 0;
    410 		sc->bounceb	= (u_char*)alloc_stmem(SECTOR_SIZE, &addr);
    411 		if(sc->bounceb == NULL)
    412 			return(ENOMEM); /* XXX */
    413 
    414 		/*
    415 		 * Go get write protect + loaded status
    416 		 */
    417 		sc->flags |= FLPF_INOPEN|FLPF_GETSTAT;
    418 		sps = splbio();
    419 		st_dmagrab(fdcint, fdstatus, sc, &lock_stat, 0);
    420 		while(sc->flags & FLPF_GETSTAT)
    421 			tsleep((caddr_t)sc, PRIBIO, "Fdopen", 0);
    422 		splx(sps);
    423 		wakeup((caddr_t)sc);
    424 
    425 		if((sc->flags & FLPF_WRTPROT) && (flags & FWRITE)) {
    426 			sc->flags = 0;
    427 			return(EPERM);
    428 		}
    429 		if(sc->flags & FLPF_EMPTY) {
    430 			sc->flags = 0;
    431 			return(ENXIO);
    432 		}
    433 		sc->flags &= ~(FLPF_INOPEN|FLPF_GETSTAT);
    434 		sc->flags |= FLPF_ISOPEN;
    435 	}
    436 	else {
    437 		/*
    438 		 * Multiply opens are granted when accessing the same type of
    439 		 * floppy (eq. the same partition).
    440 		 */
    441 		if(sc->part != DISKPART(dev))
    442 			return(ENXIO);	/* XXX temporarely out of business */
    443 	}
    444 	fdgetdisklabel(sc, dev);
    445 #ifdef FLP_DEBUG
    446 	printf("Fdopen open succeeded on type %d\n", sc->part);
    447 #endif
    448 }
    449 
    450 fdclose(dev, flags, devtype, proc)
    451 dev_t		dev;
    452 int		flags, devtype;
    453 struct proc	*proc;
    454 {
    455 	struct fd_softc	*sc;
    456 
    457 	sc = getsoftc(fdcd, DISKUNIT(dev));
    458 	free_stmem(sc->bounceb);
    459 	sc->flags = 0;
    460 	nopens--;
    461 
    462 #ifdef FLP_DEBUG
    463 	printf("Closed floppy device -- nopens: %d\n", nopens);
    464 #endif
    465 	return(0);
    466 }
    467 
    468 void
    469 fdstrategy(bp)
    470 struct buf	*bp;
    471 {
    472 	struct fd_softc	 *sc;
    473 	struct disklabel *lp;
    474 	int		 sps, nblocks;
    475 
    476 	sc = getsoftc(fdcd, DISKUNIT(bp->b_dev));
    477 
    478 #ifdef FLP_DEBUG
    479 	printf("fdstrategy: 0x%x\n", bp);
    480 #endif
    481 
    482 	/*
    483 	 * check for valid partition and bounds
    484 	 */
    485 	lp = &sc->dkdev.dk_label;
    486 	if ((sc->flags & FLPF_HAVELAB) == 0) {
    487 		bp->b_error = EIO;
    488 		goto bad;
    489 	}
    490 	if (bounds_check_with_label(bp, lp, 0) <= 0)
    491 		goto done;
    492 
    493 	if (bp->b_bcount == 0)
    494 		goto done;
    495 
    496 	/*
    497 	 * queue the buf and kick the low level code
    498 	 */
    499 	sps = splbio();
    500 	disksort(&sc->bufq, bp);
    501 	if (!lock_stat) {
    502 		if (fd_state & FLP_MON)
    503 			untimeout((FPV)fdmotoroff, (void*)sc);
    504 		fd_state = FLP_IDLE;
    505 		st_dmagrab(fdcint, fdstart, sc, &lock_stat, 0);
    506 	}
    507 	splx(sps);
    508 
    509 	return;
    510 bad:
    511 	bp->b_flags |= B_ERROR;
    512 done:
    513 	bp->b_resid = bp->b_bcount;
    514 	biodone(bp);
    515 }
    516 
    517 /*
    518  * no dumps to floppy disks thank you.
    519  */
    520 int
    521 fddump(dev_t dev)
    522 {
    523 	return(ENXIO);
    524 }
    525 
    526 /*
    527  * no dumps to floppy disks thank you.
    528  */
    529 int
    530 fdsize(dev)
    531 dev_t dev;
    532 {
    533 	return(-1);
    534 }
    535 
    536 int
    537 fdread(dev, uio)
    538 dev_t		dev;
    539 struct uio	*uio;
    540 {
    541 	return(physio(fdstrategy, NULL, dev, B_READ, fdminphys, uio));
    542 }
    543 
    544 int
    545 fdwrite(dev, uio)
    546 dev_t		dev;
    547 struct uio	*uio;
    548 {
    549 	return(physio(fdstrategy, NULL, dev, B_WRITE, fdminphys, uio));
    550 }
    551 
    552 /*
    553  * Called through DMA-dispatcher, get status.
    554  */
    555 static void
    556 fdstatus(sc)
    557 struct fd_softc	*sc;
    558 {
    559 #ifdef FLP_DEBUG
    560 	printf("fdstatus\n");
    561 #endif
    562 	sc->errcnt = 0;
    563 	fd_state   = FLP_STAT;
    564 	fd_xfer(sc);
    565 }
    566 
    567 /*
    568  * Called through the dma-dispatcher. So we know we are the only ones
    569  * messing with the floppy-controler.
    570  * Initialize some fields in the fdsoftc for the state-machine and get
    571  * it going.
    572  */
    573 static void
    574 fdstart(sc)
    575 struct fd_softc	*sc;
    576 {
    577 	struct buf	*bp;
    578 
    579 	bp           = sc->bufq.b_actf;
    580 	sc->sector   = bp->b_blkno;	/* Start sector for I/O		*/
    581 	sc->io_data  = bp->b_data;	/* KVA base for I/O		*/
    582 	sc->io_bytes = bp->b_bcount;	/* Transfer size in bytes	*/
    583 	sc->io_dir   = bp->b_flags & B_READ;/* Direction of transfer	*/
    584 	sc->errcnt   = 0;		/* No errors yet		*/
    585 	fd_state     = FLP_XFER;	/* Yes, we're going to transfer	*/
    586 
    587 	fd_xfer(sc);
    588 }
    589 
    590 /*
    591  * The current transaction is finished (for good or bad). Let go of
    592  * the the dma-resources. Call biodone() to finish the transaction.
    593  * Find a new transaction to work on.
    594  */
    595 static void
    596 fddone(sc)
    597 register struct fd_softc	*sc;
    598 {
    599 	struct buf	*bp, *dp;
    600 	struct fd_softc	*sc1;
    601 	int		i, sps;
    602 
    603 	/*
    604 	 * Give others a chance to use the dma.
    605 	 */
    606 	st_dmafree(sc, &lock_stat);
    607 
    608 
    609 	if(fd_state != FLP_STAT) {
    610 		/*
    611 		 * Finish current transaction.
    612 		 */
    613 		sps = splbio();
    614 		dp = &sc->bufq;
    615 		bp = dp->b_actf;
    616 		if(bp == NULL)
    617 			panic("fddone");
    618 		dp->b_actf = bp->b_actf;
    619 		splx(sps);
    620 
    621 #ifdef FLP_DEBUG
    622 		printf("fddone: unit: %d, buf: %x, resid: %d\n",sc->unit,bp,
    623 								sc->io_bytes);
    624 #endif
    625 		bp->b_resid = sc->io_bytes;
    626 		biodone(bp);
    627 	}
    628 	fd_state = FLP_MON;
    629 
    630 	if(lock_stat)
    631 		return;		/* XXX Is this possible?	*/
    632 
    633 	/*
    634 	 * Find a new transaction on round-robin basis.
    635 	 */
    636 	for(i = sc->unit + 1; ;i++) {
    637 		if(i >= fdcd.cd_ndevs)
    638 			i = 0;
    639 		if((sc1 = fdcd.cd_devs[i]) == NULL)
    640 			continue;
    641 		if(sc1->bufq.b_actf)
    642 			break;
    643 		if(i == sc->unit) {
    644 			timeout((FPV)fdmotoroff, (void*)sc, FLP_MONDELAY);
    645 #ifdef FLP_DEBUG
    646 			printf("fddone: Nothing to do\n");
    647 #endif
    648 			return;	/* No work */
    649 		}
    650 	}
    651 	fd_state = FLP_IDLE;
    652 #ifdef FLP_DEBUG
    653 	printf("fddone: Staring job on unit %d\n", sc1->unit);
    654 #endif
    655 	st_dmagrab(fdcint, fdstart, sc1, &lock_stat, 0);
    656 }
    657 
    658 static int
    659 fdselect(drive, head, dense)
    660 int	drive, head, dense;
    661 {
    662 	int	i, sps, spinning;
    663 #ifdef FLP_DEBUG
    664 	printf("fdselect: drive=%d, head=%d, dense=%d\n", drive, head, dense);
    665 #endif
    666 	i = ((drive == 1) ? PA_FLOP1 : PA_FLOP0) | head;
    667 	spinning = motoron;
    668 	motoron  = 1;
    669 
    670 	switch(dense) {
    671 		case FLP_DD:
    672 			DMA->dma_drvmode = 0;
    673 			break;
    674 		case FLP_HD:
    675 			DMA->dma_drvmode = (FDC_HDSET|FDC_HDSIG);
    676 			break;
    677 		default:
    678 			panic("fdselect: unknown density code\n");
    679 	}
    680 	if(i != selected) {
    681 		sps = splhigh();
    682 
    683 		selected = i;
    684 		SOUND->sd_selr = YM_IOA;
    685 		SOUND->sd_wdat = (SOUND->sd_rdat & 0x78) | (i ^ 0x07);
    686 		splx(sps);
    687 	}
    688 	return(spinning);
    689 }
    690 
    691 static void
    692 fddeselect()
    693 {
    694 	int	sps;
    695 
    696 	sps = splhigh();
    697 	SOUND->sd_selr = YM_IOA;
    698 	SOUND->sd_wdat = SOUND->sd_rdat | 0x07;
    699 	splx(sps);
    700 
    701 	motoron = selected = 0;
    702 	DMA->dma_drvmode   = 0;
    703 }
    704 
    705 /****************************************************************************
    706  * The following functions assume to be running as a result of a            *
    707  * disk-interrupt (e.q. spl = splbio).				            *
    708  * They form the finit-state machine, the actual driver.                    *
    709  *                                                                          *
    710  *	fdstart()/ --> fd_xfer() -> activate hardware                       *
    711  *  fdopen()          ^                                                     *
    712  *                    |                                                     *
    713  *                    +-- not ready -<------------+                         *
    714  *                                                |                         *
    715  *  fdmotoroff()/ --> fdcint() -> fd_xfer_ok() ---+                         *
    716  *  h/w interrupt                 |                                         *
    717  *                               \|/                                        *
    718  *                            finished ---> fdone()                         *
    719  *                                                                          *
    720  ****************************************************************************/
    721 static void
    722 fd_xfer(sc)
    723 struct fd_softc	*sc;
    724 {
    725 	register int	head = 0;
    726 	register int	track, sector, hbit;
    727 		 int	i;
    728 		 u_long	phys_addr;
    729 
    730 	switch(fd_state) {
    731 	    case FLP_XFER:
    732 		/*
    733 		 * Calculate head/track values
    734 		 */
    735 		track  = sc->sector / sc->nsectors;
    736 		head   = track % sc->nheads;
    737 		track  = track / sc->nheads;
    738 #ifdef FLP_DEBUG
    739 		printf("fd_xfer: sector:%d,head:%d,track:%d\n", sc->sector,head,
    740 								track);
    741 #endif
    742 		break;
    743 
    744 	    case FLP_STAT:
    745 		/*
    746 		 * FLP_STAT only wants to recalibrate
    747 		 */
    748 		sc->curtrk = INV_TRK;
    749 		break;
    750 	    default:
    751 		panic("fd_xfer: wrong state (0x%x)", fd_state);
    752 	}
    753 
    754 	/*
    755 	 * Select the drive.
    756 	 */
    757 	hbit = fdselect(sc->unit, head, sc->density) ? HBIT : 0;
    758 
    759 	if(sc->curtrk == INV_TRK) {
    760 		/*
    761 		 * Recalibrate, since we lost track of head positioning.
    762 		 * The floppy disk controller has no way of determining its
    763 		 * absolute arm position (track).  Instead, it steps the
    764 		 * arm a track at a time and keeps track of where it
    765 		 * thinks it is (in software).  However, after a SEEK, the
    766 		 * hardware reads information from the diskette telling
    767 		 * where the arm actually is.  If the arm is in the wrong place,
    768 		 * a recalibration is done, which forces the arm to track 0.
    769 		 * This way the controller can get back into sync with reality.
    770 		 */
    771 		fd_cmd = RESTORE;
    772 		write_fdreg(FDC_CS, RESTORE|VBIT|hbit);
    773 		timeout((FPV)fdmotoroff, (void*)sc, FLP_XFERDELAY);
    774 
    775 #ifdef FLP_DEBUG
    776 		printf("fd_xfer:Recalibrating drive %d\n", sc->unit);
    777 #endif
    778 		return;
    779 	}
    780 
    781 	write_fdreg(FDC_TR, sc->curtrk);
    782 
    783 	/*
    784 	 * Issue a SEEK command on the indicated drive unless the arm is
    785 	 * already positioned on the correct track.
    786 	 */
    787 	if(track != sc->curtrk) {
    788 		sc->curtrk = track;	/* be optimistic */
    789 		write_fdreg(FDC_DR, track);
    790 		write_fdreg(FDC_CS, SEEK|RATE6|VBIT|hbit);
    791 		timeout((FPV)fdmotoroff, (void*)sc, FLP_XFERDELAY);
    792 		fd_cmd = SEEK;
    793 #ifdef FLP_DEBUG
    794 		printf("fd_xfer:Seek to track %d on drive %d\n",track,sc->unit);
    795 #endif
    796 		return;
    797 	}
    798 
    799 	/*
    800 	 * The drive is now on the proper track. Read or write 1 block.
    801 	 */
    802 	sector = sc->sector % sc->nsectors;
    803 	sector++;	/* start numbering at 1 */
    804 
    805 	write_fdreg(FDC_SR, sector);
    806 
    807 	phys_addr = (u_long)kvtop(sc->io_data);
    808 	if(phys_addr >= FDC_MAX_DMA_AD) {
    809 		/*
    810 		 * We _must_ bounce this address
    811 		 */
    812 		phys_addr = (u_long)kvtop(sc->bounceb);
    813 		if(sc->io_dir == B_WRITE)
    814 			bcopy(sc->io_data, sc->bounceb, SECTOR_SIZE);
    815 		sc->flags |= FLPF_BOUNCE;
    816 	}
    817 	st_dmaaddr_set((caddr_t)phys_addr);	/* DMA address setup */
    818 
    819 #ifdef FLP_DEBUG
    820 	printf("fd_xfer:Start io (io_addr:%x)\n", kvtop(sc->io_data));
    821 #endif
    822 
    823 	if(sc->io_dir == B_READ) {
    824 		/* Issue the command */
    825 		st_dmacomm(DMA_FDC | DMA_SCREG, 1);
    826 		write_fdreg(FDC_CS, F_READ|hbit);
    827 		fd_cmd = F_READ;
    828 	}
    829 	else {
    830 		/* Issue the command */
    831 		st_dmacomm(DMA_WRBIT | DMA_FDC | DMA_SCREG, 1);
    832 		write_fdreg(DMA_WRBIT | FDC_CS, F_WRITE|hbit|EBIT|PBIT);
    833 		fd_cmd = F_WRITE;
    834 	}
    835 	timeout((FPV)fdmotoroff, (void*)sc, FLP_XFERDELAY);
    836 }
    837 
    838 /* return values of fd_xfer_ok(): */
    839 #define X_OK			0
    840 #define X_AGAIN			1
    841 #define X_ERROR			2
    842 #define X_FAIL			3
    843 
    844 /*
    845  * Hardware interrupt function.
    846  */
    847 static void
    848 fdcint(sc)
    849 struct fd_softc	*sc;
    850 {
    851 	struct	buf	*bp;
    852 
    853 #ifdef FLP_DEBUG
    854 	printf("fdcint: unit = %d\n", sc->unit);
    855 #endif
    856 
    857 	/*
    858 	 * Cancel timeout (we made it, didn't we)
    859 	 */
    860 	untimeout((FPV)fdmotoroff, (void*)sc);
    861 
    862 	switch(fd_xfer_ok(sc)) {
    863 		case X_ERROR :
    864 			if(++(sc->errcnt) < MAX_ERRORS) {
    865 				/*
    866 				 * Command failed but still retries left.
    867 				 */
    868 				break;
    869 			}
    870 			/* FALL THROUGH */
    871 		case X_FAIL  :
    872 			/*
    873 			 * Non recoverable error. Fall back to motor-on
    874 			 * idle-state.
    875 			 */
    876 			if(fd_error != NULL) {
    877 				printf("Floppy error: %s\n", fd_error);
    878 				fd_error = NULL;
    879 			}
    880 
    881 			if(fd_state == FLP_STAT) {
    882 				sc->flags |= FLPF_EMPTY;
    883 				sc->flags &= ~FLPF_GETSTAT;
    884 				wakeup((caddr_t)sc);
    885 				fddone(sc);
    886 				return;
    887 			}
    888 
    889 			bp = sc->bufq.b_actf;
    890 
    891 			bp->b_error  = EIO;
    892 			bp->b_flags |= B_ERROR;
    893 			fd_state     = FLP_MON;
    894 
    895 			break;
    896 		case X_AGAIN:
    897 			/*
    898 			 * Start next part of state machine.
    899 			 */
    900 			break;
    901 		case X_OK:
    902 			/*
    903 			 * Command ok and finished. Reset error-counter.
    904 			 * If there are no more bytes to transfer fall back
    905 			 * to motor-on idle state.
    906 			 */
    907 			sc->errcnt = 0;
    908 
    909 			if(fd_state == FLP_STAT) {
    910 				sc->flags &= ~FLPF_GETSTAT;
    911 				wakeup((caddr_t)sc);
    912 				fddone(sc);
    913 				return;
    914 			}
    915 
    916 			if((sc->flags & FLPF_BOUNCE) && (sc->io_dir == B_READ))
    917 				bcopy(sc->bounceb, sc->io_data, SECTOR_SIZE);
    918 			sc->flags &= ~FLPF_BOUNCE;
    919 
    920 			sc->sector++;
    921 			sc->io_data  += SECTOR_SIZE;
    922 			sc->io_bytes -= SECTOR_SIZE;
    923 			if(sc->io_bytes <= 0)
    924 				fd_state = FLP_MON;
    925 	}
    926 	if(fd_state == FLP_MON)
    927 		fddone(sc);
    928 	else fd_xfer(sc);
    929 }
    930 
    931 /*
    932  * Determine status of last command. Should only be called through
    933  * 'fdcint()'.
    934  * Returns:
    935  *	X_ERROR : Error on command; might succeed next time.
    936  *	X_FAIL  : Error on command; will never succeed.
    937  *	X_AGAIN : Part of a command succeeded, call 'fd_xfer()' to complete.
    938  *	X_OK	: Command succeeded and is complete.
    939  *
    940  * This function only affects sc->curtrk.
    941  */
    942 static int
    943 fd_xfer_ok(sc)
    944 register struct fd_softc	*sc;
    945 {
    946 	register int	status;
    947 
    948 #ifdef FLP_DEBUG
    949 	printf("fd_xfer_ok: cmd: 0x%x, state: 0x%x\n", fd_cmd, fd_state);
    950 #endif
    951 	switch(fd_cmd) {
    952 		case IRUPT:
    953 			/*
    954 			 * Timeout. Force a recalibrate before we try again.
    955 			 */
    956 			status = read_fdreg(FDC_CS);
    957 
    958 			fd_error = "Timeout";
    959 			sc->curtrk = INV_TRK;
    960 			return(X_ERROR);
    961 		case F_READ:
    962 			/*
    963 			 * Test for DMA error
    964 			 */
    965 			status = read_dmastat();
    966 			if(!(status & DMAOK)) {
    967 				fd_error = "Dma error";
    968 				return(X_ERROR);
    969 			}
    970 			/*
    971 			 * Get controller status and check for errors.
    972 			 */
    973 			status = read_fdreg(FDC_CS);
    974 			if(status & (RNF | CRCERR | LD_T00)) {
    975 				fd_error = "Read error";
    976 				if(status & RNF)
    977 					sc->curtrk = INV_TRK;
    978 				return(X_ERROR);
    979 			}
    980 			break;
    981 		case F_WRITE:
    982 			/*
    983 			 * Test for DMA error
    984 			 */
    985 			status = read_dmastat();
    986 			if(!(status & DMAOK)) {
    987 				fd_error = "Dma error";
    988 				return(X_ERROR);
    989 			}
    990 			/*
    991 			 * Get controller status and check for errors.
    992 			 */
    993 			status = read_fdreg(FDC_CS);
    994 			if(status & WRI_PRO) {
    995 				fd_error = "Write protected";
    996 				return(X_FAIL);
    997 			}
    998 			if(status & (RNF | CRCERR | LD_T00)) {
    999 				fd_error = "Write error";
   1000 				sc->curtrk = INV_TRK;
   1001 				return(X_ERROR);
   1002 			}
   1003 			break;
   1004 		case SEEK:
   1005 			status = read_fdreg(FDC_CS);
   1006 			if(status & (RNF | CRCERR)) {
   1007 				fd_error = "Seek error";
   1008 				sc->curtrk = INV_TRK;
   1009 				return(X_ERROR);
   1010 			}
   1011 			return(X_AGAIN);
   1012 		case RESTORE:
   1013 			/*
   1014 			 * Determine if the recalibration succeeded.
   1015 			 */
   1016 			status = read_fdreg(FDC_CS);
   1017 			if(status & RNF) {
   1018 				fd_error = "Recalibrate error";
   1019 				/* reset controller */
   1020 				write_fdreg(FDC_CS, IRUPT);
   1021 				sc->curtrk = INV_TRK;
   1022 				return(X_ERROR);
   1023 			}
   1024 			sc->curtrk = 0;
   1025 			if(fd_state == FLP_STAT) {
   1026 				if(status & WRI_PRO)
   1027 					sc->flags |= FLPF_WRTPROT;
   1028 				break;
   1029 			}
   1030 			return(X_AGAIN);
   1031 		default:
   1032 			fd_error = "Driver error: fd_xfer_ok : Unknown state";
   1033 			return(X_FAIL);
   1034 	}
   1035 	return(X_OK);
   1036 }
   1037 
   1038 /*
   1039  * All timeouts will call this function.
   1040  */
   1041 static void
   1042 fdmotoroff(sc)
   1043 struct fd_softc	*sc;
   1044 {
   1045 	int	sps;
   1046 
   1047 	/*
   1048 	 * Get at harware interrupt level
   1049 	 */
   1050 	sps = splbio();
   1051 
   1052 #if FLP_DEBUG
   1053 	printf("fdmotoroff, state = 0x%x\n", fd_state);
   1054 #endif
   1055 
   1056 	switch(fd_state) {
   1057 		case FLP_STAT :
   1058 		case FLP_XFER :
   1059 			/*
   1060 			 * Timeout during a transfer; cancel transaction
   1061 			 * set command to 'IRUPT'.
   1062 			 * A drive-interrupt is simulated to trigger the state
   1063 			 * machine.
   1064 			 */
   1065 			/*
   1066 			 * Cancel current transaction
   1067 			 */
   1068 			fd_cmd = IRUPT;
   1069 			write_fdreg(FDC_CS, IRUPT);
   1070 			delay(20);
   1071 			(void)read_fdreg(FDC_CS);
   1072 			write_fdreg(FDC_CS, RESTORE);
   1073 			break;
   1074 
   1075 		case FLP_MON  :
   1076 			/*
   1077 			 * Turn motor off.
   1078 			 */
   1079 			if(selected)
   1080 				fddeselect();
   1081 			fd_state = FLP_IDLE;
   1082 			break;
   1083 	}
   1084 	splx(sps);
   1085 }
   1086 
   1087 /*
   1088  * min byte count to whats left of the track in question
   1089  */
   1090 static void
   1091 fdminphys(bp)
   1092 struct buf	*bp;
   1093 {
   1094 	struct fd_softc	*sc;
   1095 	int		sec, toff, tsz;
   1096 
   1097 	if((sc = getsoftc(fdcd, DISKUNIT(bp->b_dev))) == NULL)
   1098 		panic("fdminphys: couldn't get softc");
   1099 
   1100 	sec  = bp->b_blkno % (sc->nsectors * sc->nheads);
   1101 	toff = sec * SECTOR_SIZE;
   1102 	tsz  = sc->nsectors * sc->nheads * SECTOR_SIZE;
   1103 
   1104 #ifdef FLP_DEBUG
   1105 	printf("fdminphys: before %d", bp->b_bcount);
   1106 #endif
   1107 
   1108 	bp->b_bcount = min(bp->b_bcount, tsz - toff);
   1109 
   1110 #ifdef FLP_DEBUG
   1111 	printf(" after %d\n", bp->b_bcount);
   1112 #endif
   1113 
   1114 	minphys(bp);
   1115 }
   1116 
   1117 /*
   1118  * Used to find out wich drives are actually connected. We do this by issueing
   1119  * is 'RESTORE' command and check if the 'track-0' bit is set. This also works
   1120  * if the drive is present but no floppy is inserted.
   1121  */
   1122 static void
   1123 fdtestdrv(fdsoftc)
   1124 struct fd_softc	*fdsoftc;
   1125 {
   1126 	int	i, status;
   1127 
   1128 	/*
   1129 	 * Select the right unit and head.
   1130 	 */
   1131 	fdselect(fdsoftc->unit, 0, FLP_DD);
   1132 
   1133 	write_fdreg(FDC_CS, RESTORE|HBIT);
   1134 
   1135 	/*
   1136 	 * Wait for about 2 seconds.
   1137 	 */
   1138 	delay(2000000);
   1139 
   1140 	status = read_fdreg(FDC_CS);
   1141 	if(status & (RNF|BUSY)) {
   1142 		write_fdreg(FDC_CS, IRUPT);	/* reset controller */
   1143 		delay(40);
   1144 	}
   1145 
   1146 	if(!(status & LD_T00))
   1147 		fdsoftc->flags |= FLPF_NOTRESP;
   1148 
   1149 	fddeselect();
   1150 }
   1151 
   1152 /*
   1153  * Build disk label. For now we only create a label from what we know
   1154  * from 'sc'.
   1155  */
   1156 static int
   1157 fdgetdisklabel(sc, dev)
   1158 struct fd_softc *sc;
   1159 dev_t			dev;
   1160 {
   1161 	struct disklabel	*lp, *dlp;
   1162 	int			part;
   1163 
   1164 	/*
   1165 	 * If we already got one, get out.
   1166 	 */
   1167 	if(sc->flags & FLPF_HAVELAB)
   1168 		return(0);
   1169 
   1170 #ifdef FLP_DEBUG
   1171 	printf("fdgetdisklabel()\n");
   1172 #endif
   1173 
   1174 	part = DISKPART(dev);
   1175 	lp   = &sc->dkdev.dk_label;
   1176 	bzero(lp, sizeof(struct disklabel));
   1177 
   1178 	lp->d_secsize     = SECTOR_SIZE;
   1179 	lp->d_ntracks     = sc->nheads;
   1180 	lp->d_nsectors    = sc->nsectors;
   1181 	lp->d_secpercyl   = lp->d_ntracks * lp->d_nsectors;
   1182 	lp->d_ncylinders  = sc->nblocks / lp->d_secpercyl;
   1183 	lp->d_secperunit  = sc->nblocks;
   1184 
   1185 	lp->d_type        = DTYPE_FLOPPY;
   1186 	lp->d_rpm         = 300; 	/* good guess I suppose.	*/
   1187 	lp->d_interleave  = 1;		/* FIXME: is this OK?		*/
   1188 	lp->d_bbsize      = 0;
   1189 	lp->d_sbsize      = 0;
   1190 	lp->d_npartitions = part + 1;
   1191 	lp->d_trkseek     = STEP_DELAY;
   1192 	lp->d_magic       = DISKMAGIC;
   1193 	lp->d_magic2      = DISKMAGIC;
   1194 	lp->d_checksum    = dkcksum(lp);
   1195 	lp->d_partitions[part].p_size   = lp->d_secperunit;
   1196 	lp->d_partitions[part].p_fstype = FS_UNUSED;
   1197 	lp->d_partitions[part].p_fsize  = 1024;
   1198 	lp->d_partitions[part].p_frag   = 8;
   1199 	sc->flags        |= FLPF_HAVELAB;
   1200 
   1201 	return(0);
   1202 }
   1203