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