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