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