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