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