Home | History | Annotate | Line # | Download | only in jazz
fd.c revision 1.30
      1 /*	$NetBSD: fd.c,v 1.30 2007/07/09 20:52:04 ad Exp $	*/
      2 /*	$OpenBSD: fd.c,v 1.6 1998/10/03 21:18:57 millert Exp $	*/
      3 /*	NetBSD: fd.c,v 1.78 1995/07/04 07:23:09 mycroft Exp 	*/
      4 
      5 /*-
      6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to The NetBSD Foundation
     10  * by Charles M. Hannum.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*-
     42  * Copyright (c) 1990 The Regents of the University of California.
     43  * All rights reserved.
     44  *
     45  * This code is derived from software contributed to Berkeley by
     46  * Don Ahn.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     73  */
     74 
     75 #include <sys/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.30 2007/07/09 20:52:04 ad Exp $");
     77 
     78 #include <sys/param.h>
     79 #include <sys/systm.h>
     80 #include <sys/callout.h>
     81 #include <sys/kernel.h>
     82 #include <sys/conf.h>
     83 #include <sys/file.h>
     84 #include <sys/ioctl.h>
     85 #include <sys/device.h>
     86 #include <sys/disklabel.h>
     87 #include <sys/disk.h>
     88 #include <sys/buf.h>
     89 #include <sys/bufq.h>
     90 #include <sys/uio.h>
     91 #include <sys/syslog.h>
     92 #include <sys/queue.h>
     93 
     94 #include <uvm/uvm_extern.h>
     95 
     96 #include <dev/cons.h>
     97 
     98 #include <machine/bus.h>
     99 #include <machine/cpu.h>
    100 
    101 #include <arc/jazz/fdreg.h>
    102 #include <arc/jazz/fdcvar.h>
    103 
    104 #include "ioconf.h"
    105 #include "locators.h"
    106 
    107 #define FDUNIT(dev)	DISKUNIT(dev)
    108 #define FDTYPE(dev)	DISKPART(dev)
    109 
    110 /* controller driver configuration */
    111 int fdprint(void *, const char *);
    112 
    113 /*
    114  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
    115  * we tell them apart.
    116  */
    117 struct fd_type {
    118 	int	sectrac;	/* sectors per track */
    119 	int	heads;		/* number of heads */
    120 	int	seccyl;		/* sectors per cylinder */
    121 	int	secsize;	/* size code for sectors */
    122 	int	datalen;	/* data len when secsize = 0 */
    123 	int	steprate;	/* step rate and head unload time */
    124 	int	gap1;		/* gap len between sectors */
    125 	int	gap2;		/* formatting gap */
    126 	int	cyls;		/* total num of cylinders */
    127 	int	size;		/* size of disk in sectors */
    128 	int	step;		/* steps per cylinder */
    129 	int	rate;		/* transfer speed code */
    130 	const char *name;
    131 };
    132 
    133 /* The order of entries in the following table is important -- BEWARE! */
    134 struct fd_type fd_types[] = {
    135 	/* 1.44MB diskette */
    136 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB"    },
    137 	/* 1.2 MB AT-diskettes */
    138 	{ 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB"    },
    139 	/* 360kB in 1.2MB drive */
    140 	{  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" },
    141 	/* 360kB PC diskettes */
    142 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" },
    143 	/* 3.5" 720kB diskette */
    144 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB"    },
    145 	/* 720kB in 1.2MB drive */
    146 	{  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x"  },
    147 	/* 360kB in 720kB drive */
    148 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x"  },
    149 };
    150 
    151 /* software state, per disk (with up to 4 disks per ctlr) */
    152 struct fd_softc {
    153 	struct device sc_dev;
    154 	struct disk sc_dk;
    155 
    156 	const struct fd_type *sc_deftype; /* default type descriptor */
    157 	struct fd_type *sc_type;	/* current type descriptor */
    158 	struct fd_type sc_type_copy;	/* copy for fiddling when formatting */
    159 
    160 	struct callout sc_motoron_ch;
    161 	struct callout sc_motoroff_ch;
    162 
    163 	daddr_t	sc_blkno;	/* starting block number */
    164 	int sc_bcount;		/* byte count left */
    165 	int sc_opts;		/* user-set options */
    166 	int sc_skip;		/* bytes already transferred */
    167 	int sc_nblks;		/* number of blocks currently transferring */
    168 	int sc_nbytes;		/* number of bytes currently transferring */
    169 
    170 	int sc_drive;		/* physical unit number */
    171 	int sc_flags;
    172 #define	FD_OPEN		0x01		/* it's open */
    173 #define	FD_MOTOR	0x02		/* motor should be on */
    174 #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
    175 	int sc_cylin;		/* where we think the head is */
    176 
    177 	void *sc_sdhook;	/* saved shutdown hook for drive. */
    178 
    179 	TAILQ_ENTRY(fd_softc) sc_drivechain;
    180 	int sc_ops;		/* I/O ops since last switch */
    181 	struct bufq_state *sc_q;/* pending I/O requests */
    182 	int sc_active;		/* number of active I/O operations */
    183 };
    184 
    185 /* floppy driver configuration */
    186 int fdprobe(struct device *, struct cfdata *, void *);
    187 void fdattach(struct device *, struct device *, void *);
    188 
    189 CFATTACH_DECL(fd, sizeof(struct fd_softc), fdprobe, fdattach, NULL, NULL);
    190 
    191 dev_type_open(fdopen);
    192 dev_type_close(fdclose);
    193 dev_type_read(fdread);
    194 dev_type_write(fdwrite);
    195 dev_type_ioctl(fdioctl);
    196 dev_type_strategy(fdstrategy);
    197 
    198 const struct bdevsw fd_bdevsw = {
    199 	fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
    200 };
    201 
    202 const struct cdevsw fd_cdevsw = {
    203 	fdopen, fdclose, fdread, fdwrite, fdioctl,
    204 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    205 };
    206 
    207 void fdgetdisklabel(struct fd_softc *);
    208 int fd_get_parms(struct fd_softc *);
    209 void fdstrategy(struct buf *);
    210 void fdstart(struct fd_softc *);
    211 
    212 struct dkdriver fddkdriver = { fdstrategy };
    213 
    214 #if 0
    215 const struct fd_type *fd_nvtotype(char *, int, int);
    216 #endif
    217 void fd_set_motor(struct fdc_softc *, int);
    218 void fd_motor_off(void *);
    219 void fd_motor_on(void *);
    220 int fdcresult(struct fdc_softc *);
    221 void fdcstart(struct fdc_softc *);
    222 void fdcstatus(struct device *, int, const char *);
    223 void fdctimeout(void *);
    224 void fdcpseudointr(void *);
    225 void fdcretry(struct fdc_softc *);
    226 void fdfinish(struct fd_softc *, struct buf *);
    227 inline const struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
    228 void fd_mountroot_hook(struct device *);
    229 
    230 /*
    231  * Arguments passed between fdcattach and fdprobe.
    232  */
    233 struct fdc_attach_args {
    234 	int fa_drive;
    235 	const struct fd_type *fa_deftype;
    236 };
    237 
    238 /*
    239  * Print the location of a disk drive (called just before attaching the
    240  * the drive).  If `fdc' is not NULL, the drive was found but was not
    241  * in the system config file; print the drive name as well.
    242  * Return QUIET (config_find ignores this if the device was configured) to
    243  * avoid printing `fdN not configured' messages.
    244  */
    245 int
    246 fdprint(void *aux, const char *fdc)
    247 {
    248 	struct fdc_attach_args *fa = aux;
    249 
    250 	if (!fdc)
    251 		aprint_normal(" drive %d", fa->fa_drive);
    252 	return QUIET;
    253 }
    254 
    255 void
    256 fdcattach(struct fdc_softc *fdc)
    257 {
    258 	struct fdc_attach_args fa;
    259 	bus_space_tag_t iot;
    260 	bus_space_handle_t ioh;
    261 	int type;
    262 
    263 	iot = fdc->sc_iot;
    264 	ioh = fdc->sc_ioh;
    265 	callout_init(&fdc->sc_timo_ch, 0);
    266 	callout_init(&fdc->sc_intr_ch, 0);
    267 
    268 	fdc->sc_state = DEVIDLE;
    269 	TAILQ_INIT(&fdc->sc_drives);
    270 
    271 	/*
    272 	 * No way yet to determine default disk types.
    273 	 * we assume 1.44 3.5" type for the moment.
    274 	 */
    275 	type = 0;
    276 
    277 	/* physical limit: two drives per controller. */
    278 	for (fa.fa_drive = 0; fa.fa_drive < 2; fa.fa_drive++) {
    279 		fa.fa_deftype = &fd_types[type];
    280 		(void)config_found(&fdc->sc_dev, (void *)&fa, fdprint);
    281 	}
    282 }
    283 
    284 int
    285 fdprobe(struct device *parent, struct cfdata *match, void *aux)
    286 {
    287 	struct fdc_softc *fdc = (void *)parent;
    288 	struct cfdata *cf = match;
    289 	struct fdc_attach_args *fa = aux;
    290 	int drive = fa->fa_drive;
    291 	bus_space_tag_t iot = fdc->sc_iot;
    292 	bus_space_handle_t ioh = fdc->sc_ioh;
    293 	int n;
    294 
    295 	if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
    296 	    cf->cf_loc[FDCCF_DRIVE] != drive)
    297 		return 0;
    298 
    299 	/* select drive and turn on motor */
    300 	bus_space_write_1(iot, ioh, FDOUT, drive | FDO_FRST | FDO_MOEN(drive));
    301 	/* wait for motor to spin up */
    302 	delay(250000);
    303 	out_fdc(iot, ioh, NE7CMD_RECAL);
    304 	out_fdc(iot, ioh, drive);
    305 	/* wait for recalibrate */
    306 	delay(2000000);
    307 	out_fdc(iot, ioh, NE7CMD_SENSEI);
    308 	n = fdcresult(fdc);
    309 #ifdef FD_DEBUG
    310 	{
    311 		int i;
    312 		printf("fdprobe: status");
    313 		for (i = 0; i < n; i++)
    314 			printf(" %x", fdc->sc_status[i]);
    315 		printf("\n");
    316 	}
    317 #endif
    318 	if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
    319 		return 0;
    320 	/* turn off motor */
    321 	bus_space_write_1(iot, ioh, FDOUT, FDO_FRST);
    322 
    323 	return 1;
    324 }
    325 
    326 /*
    327  * Controller is working, and drive responded.  Attach it.
    328  */
    329 void
    330 fdattach(struct device *parent, struct device *self, void *aux)
    331 {
    332 	struct fdc_softc *fdc = (void *)parent;
    333 	struct fd_softc *fd = (void *)self;
    334 	struct fdc_attach_args *fa = aux;
    335 	const struct fd_type *type = fa->fa_deftype;
    336 	int drive = fa->fa_drive;
    337 
    338 	callout_init(&fd->sc_motoron_ch, 0);
    339 	callout_init(&fd->sc_motoroff_ch, 0);
    340 
    341 	/* XXX Allow `flags' to override device type? */
    342 
    343 	if (type)
    344 		printf(": %s, %d cyl, %d head, %d sec\n", type->name,
    345 		    type->cyls, type->heads, type->sectrac);
    346 	else
    347 		printf(": density unknown\n");
    348 
    349 	bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
    350 	fd->sc_cylin = -1;
    351 	fd->sc_drive = drive;
    352 	fd->sc_deftype = type;
    353 	fdc->sc_fd[drive] = fd;
    354 
    355 	/*
    356 	 * Initialize and attach the disk structure.
    357 	 */
    358 	fd->sc_dk.dk_name = fd->sc_dev.dv_xname;
    359 	fd->sc_dk.dk_driver = &fddkdriver;
    360 	disk_attach(&fd->sc_dk);
    361 
    362 	/* Establish a mountroot hook. */
    363 	mountroothook_establish(fd_mountroot_hook, &fd->sc_dev);
    364 
    365 	/* Needed to power off if the motor is on when we halt. */
    366 	fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
    367 }
    368 
    369 #if 0
    370 /*
    371  * Translate nvram type into internal data structure.  Return NULL for
    372  * none/unknown/unusable.
    373  */
    374 const struct fd_type *
    375 fd_nvtotype(char *fdc, int nvraminfo, int drive)
    376 {
    377 	int type;
    378 
    379 	type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
    380 #if 0
    381 	switch (type) {
    382 	case NVRAM_DISKETTE_NONE:
    383 		return NULL;
    384 	case NVRAM_DISKETTE_12M:
    385 		return &fd_types[1];
    386 	case NVRAM_DISKETTE_TYPE5:
    387 	case NVRAM_DISKETTE_TYPE6:
    388 		/* XXX We really ought to handle 2.88MB format. */
    389 	case NVRAM_DISKETTE_144M:
    390 		return &fd_types[0];
    391 	case NVRAM_DISKETTE_360K:
    392 		return &fd_types[3];
    393 	case NVRAM_DISKETTE_720K:
    394 		return &fd_types[4];
    395 	default:
    396 		printf("%s: drive %d: unknown device type 0x%x\n",
    397 		    fdc, drive, type);
    398 		return NULL;
    399 	}
    400 #else
    401 	return &fd_types[0]; /* Use only 1.44 for now */
    402 #endif
    403 }
    404 #endif
    405 
    406 inline const struct fd_type *
    407 fd_dev_to_type(struct fd_softc *fd, dev_t dev)
    408 {
    409 	int type = FDTYPE(dev);
    410 
    411 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
    412 		return NULL;
    413 	return type ? &fd_types[type - 1] : fd->sc_deftype;
    414 }
    415 
    416 void
    417 fdstrategy(struct buf *bp)
    418 {
    419 	struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(bp->b_dev));
    420 	int sz;
    421 	int s;
    422 
    423 	/* Valid unit, controller, and request? */
    424 	if (bp->b_blkno < 0 ||
    425 	    (bp->b_bcount % FDC_BSIZE) != 0) {
    426 		bp->b_error = EINVAL;
    427 		goto bad;
    428 	}
    429 
    430 	/* If it's a null transfer, return immediately. */
    431 	if (bp->b_bcount == 0)
    432 		goto done;
    433 
    434 	sz = howmany(bp->b_bcount, FDC_BSIZE);
    435 
    436 	if (bp->b_blkno + sz > fd->sc_type->size) {
    437 		sz = fd->sc_type->size - bp->b_blkno;
    438 		if (sz == 0) {
    439 			/* If exactly at end of disk, return EOF. */
    440 			goto done;
    441 		}
    442 		if (sz < 0) {
    443 			/* If past end of disk, return EINVAL. */
    444 			bp->b_error = EINVAL;
    445 			goto bad;
    446 		}
    447 		/* Otherwise, truncate request. */
    448 		bp->b_bcount = sz << DEV_BSHIFT;
    449 	}
    450 
    451 	bp->b_rawblkno = bp->b_blkno;
    452 	bp->b_cylinder =
    453 	    bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
    454 
    455 #ifdef FD_DEBUG
    456 	printf("fdstrategy: b_blkno %" PRId64 " b_bcount %ld blkno %" PRId64
    457 	    " cylin %ld sz %d\n",
    458 	    bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylinder, sz);
    459 #endif
    460 
    461 	/* Queue transfer on drive, activate drive and controller if idle. */
    462 	s = splbio();
    463 	BUFQ_PUT(fd->sc_q, bp);
    464 	callout_stop(&fd->sc_motoroff_ch);		/* a good idea */
    465 	if (fd->sc_active == 0)
    466 		fdstart(fd);
    467 #ifdef DIAGNOSTIC
    468 	else {
    469 		struct fdc_softc *fdc = (void *) device_parent(&fd->sc_dev);
    470 		if (fdc->sc_state == DEVIDLE) {
    471 			printf("fdstrategy: controller inactive\n");
    472 			fdcstart(fdc);
    473 		}
    474 	}
    475 #endif
    476 	splx(s);
    477 	return;
    478 
    479 bad:
    480 	bp->b_flags |= B_ERROR;
    481 done:
    482 	/* Toss transfer; we're done early. */
    483 	bp->b_resid = bp->b_bcount;
    484 	biodone(bp);
    485 }
    486 
    487 void
    488 fdstart(struct fd_softc *fd)
    489 {
    490 	struct fdc_softc *fdc = (void *) device_parent(&fd->sc_dev);
    491 	int active = TAILQ_FIRST(&fdc->sc_drives) != 0;
    492 
    493 	/* Link into controller queue. */
    494 	fd->sc_active = 1;
    495 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    496 
    497 	/* If controller not already active, start it. */
    498 	if (!active)
    499 		fdcstart(fdc);
    500 }
    501 
    502 void
    503 fdfinish(struct fd_softc *fd, struct buf *bp)
    504 {
    505 	struct fdc_softc *fdc = (void *) device_parent(&fd->sc_dev);
    506 
    507 	/*
    508 	 * Move this drive to the end of the queue to give others a `fair'
    509 	 * chance.  We only force a switch if N operations are completed while
    510 	 * another drive is waiting to be serviced, since there is a long motor
    511 	 * startup delay whenever we switch.
    512 	 */
    513 	(void)BUFQ_GET(fd->sc_q);
    514 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
    515 		fd->sc_ops = 0;
    516 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    517 		if (BUFQ_PEEK(fd->sc_q) != NULL)
    518 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    519 		else
    520 			fd->sc_active = 0;
    521 	}
    522 	bp->b_resid = fd->sc_bcount;
    523 	fd->sc_skip = 0;
    524 	biodone(bp);
    525 	/* turn off motor 5s from now */
    526 	callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
    527 	fdc->sc_state = DEVIDLE;
    528 }
    529 
    530 int
    531 fdread(dev_t dev, struct uio *uio, int flags)
    532 {
    533 
    534 	return physio(fdstrategy, NULL, dev, B_READ, minphys, uio);
    535 }
    536 
    537 int
    538 fdwrite(dev_t dev, struct uio *uio, int flags)
    539 {
    540 
    541 	return physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio);
    542 }
    543 
    544 void
    545 fd_set_motor(struct fdc_softc *fdc, int reset)
    546 {
    547 	struct fd_softc *fd;
    548 	u_char status;
    549 	int n;
    550 
    551 	if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
    552 		status = fd->sc_drive;
    553 	else
    554 		status = 0;
    555 	if (!reset)
    556 		status |= FDO_FRST | FDO_FDMAEN;
    557 	for (n = 0; n < 4; n++)
    558 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    559 			status |= FDO_MOEN(n);
    560 	bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, FDOUT, status);
    561 }
    562 
    563 void
    564 fd_motor_off(void *arg)
    565 {
    566 	struct fd_softc *fd = arg;
    567 	int s;
    568 
    569 	s = splbio();
    570 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    571 	fd_set_motor((struct fdc_softc *) device_parent(&fd->sc_dev), 0);
    572 	splx(s);
    573 }
    574 
    575 void
    576 fd_motor_on(void *arg)
    577 {
    578 	struct fd_softc *fd = arg;
    579 	struct fdc_softc *fdc = (void *) device_parent(&fd->sc_dev);
    580 	int s;
    581 
    582 	s = splbio();
    583 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    584 	if ((TAILQ_FIRST(&fdc->sc_drives) == fd) &&
    585 	    (fdc->sc_state == MOTORWAIT))
    586 		(void) fdcintr(fdc);
    587 	splx(s);
    588 }
    589 
    590 int
    591 fdcresult(struct fdc_softc *fdc)
    592 {
    593 	bus_space_tag_t iot = fdc->sc_iot;
    594 	bus_space_handle_t ioh = fdc->sc_ioh;
    595 	u_char i;
    596 	int j = 100000,
    597 	    n = 0;
    598 
    599 	for (; j; j--) {
    600 		i = bus_space_read_1(iot, ioh, FDSTS) &
    601 		    (NE7_DIO | NE7_RQM | NE7_CB);
    602 		if (i == NE7_RQM)
    603 			return n;
    604 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    605 			if (n >= sizeof(fdc->sc_status)) {
    606 				log(LOG_ERR, "fdcresult: overrun\n");
    607 				return -1;
    608 			}
    609 			fdc->sc_status[n++] =
    610 			    bus_space_read_1(iot, ioh, FDDATA);
    611 		}
    612 		delay(10);
    613 	}
    614 	log(LOG_ERR, "fdcresult: timeout\n");
    615 	return -1;
    616 }
    617 
    618 int
    619 out_fdc(bus_space_tag_t iot, bus_space_handle_t ioh, u_char x)
    620 {
    621 	int i = 100000;
    622 
    623 	while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_DIO) && i-- > 0);
    624 	if (i <= 0)
    625 		return -1;
    626 	while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_RQM) == 0 && i-- > 0);
    627 	if (i <= 0)
    628 		return -1;
    629 	bus_space_write_1(iot, ioh, FDDATA, x);
    630 	return 0;
    631 }
    632 
    633 int
    634 fdopen(dev_t dev, int flags, int mode, struct lwp *l)
    635 {
    636 	struct fd_softc *fd;
    637 	const struct fd_type *type;
    638 
    639 	fd = device_lookup(&fd_cd, FDUNIT(dev));
    640 	if (fd == NULL)
    641 		return ENXIO;
    642 
    643 	type = fd_dev_to_type(fd, dev);
    644 	if (type == NULL)
    645 		return ENXIO;
    646 
    647 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    648 	    memcmp(fd->sc_type, type, sizeof(*type)))
    649 		return EBUSY;
    650 
    651 	fd->sc_type_copy = *type;
    652 	fd->sc_type = &fd->sc_type_copy;
    653 	fd->sc_cylin = -1;
    654 	fd->sc_flags |= FD_OPEN;
    655 
    656 	return 0;
    657 }
    658 
    659 int
    660 fdclose(dev_t dev, int flags, int mode, struct lwp *l)
    661 {
    662 	struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
    663 
    664 	fd->sc_flags &= ~FD_OPEN;
    665 	return 0;
    666 }
    667 
    668 void
    669 fdcstart(struct fdc_softc *fdc)
    670 {
    671 
    672 #ifdef DIAGNOSTIC
    673 	/* only got here if controller's drive queue was inactive; should
    674 	   be in idle state */
    675 	if (fdc->sc_state != DEVIDLE) {
    676 		printf("fdcstart: not idle\n");
    677 		return;
    678 	}
    679 #endif
    680 	(void) fdcintr(fdc);
    681 }
    682 
    683 void
    684 fdcstatus(struct device *dv, int n, const char *s)
    685 {
    686 	struct fdc_softc *fdc = (void *) device_parent(dv);
    687 	char bits[64];
    688 
    689 	if (n == 0) {
    690 		out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
    691 		(void) fdcresult(fdc);
    692 		n = 2;
    693 	}
    694 
    695 	printf("%s: %s", dv->dv_xname, s);
    696 
    697 	switch (n) {
    698 	case 0:
    699 		printf("\n");
    700 		break;
    701 	case 2:
    702 		printf(" (st0 %s cyl %d)\n",
    703 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
    704 		    bits, sizeof(bits)), fdc->sc_status[1]);
    705 		break;
    706 	case 7:
    707 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
    708 		    NE7_ST0BITS, bits, sizeof(bits)));
    709 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
    710 		    NE7_ST1BITS, bits, sizeof(bits)));
    711 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
    712 		    NE7_ST2BITS, bits, sizeof(bits)));
    713 		printf(" cyl %d head %d sec %d)\n",
    714 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
    715 		break;
    716 #ifdef DIAGNOSTIC
    717 	default:
    718 		printf("\nfdcstatus: weird size");
    719 		break;
    720 #endif
    721 	}
    722 }
    723 
    724 void
    725 fdctimeout(void *arg)
    726 {
    727 	struct fdc_softc *fdc = arg;
    728 	struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
    729 	int s;
    730 
    731 	s = splbio();
    732 #ifdef DEBUG
    733 	log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state);
    734 #endif
    735 	fdcstatus(&fd->sc_dev, 0, "timeout");
    736 
    737 	if (BUFQ_PEEK(fd->sc_q) != NULL)
    738 		fdc->sc_state++;
    739 	else
    740 		fdc->sc_state = DEVIDLE;
    741 
    742 	(void) fdcintr(fdc);
    743 	splx(s);
    744 }
    745 
    746 void
    747 fdcpseudointr(void *arg)
    748 {
    749 	int s;
    750 
    751 	/* Just ensure it has the right spl. */
    752 	s = splbio();
    753 	(void) fdcintr(arg);
    754 	splx(s);
    755 }
    756 
    757 int
    758 fdcintr(void *arg)
    759 {
    760 	struct fdc_softc *fdc = arg;
    761 #define	st0	fdc->sc_status[0]
    762 #define	cyl	fdc->sc_status[1]
    763 	struct fd_softc *fd;
    764 	struct buf *bp;
    765 	bus_space_tag_t iot = fdc->sc_iot;
    766 	bus_space_handle_t ioh = fdc->sc_ioh;
    767 	int read, head, sec, i, nblks;
    768 	struct fd_type *type;
    769 
    770 loop:
    771 	/* Is there a drive for the controller to do a transfer with? */
    772 	fd = TAILQ_FIRST(&fdc->sc_drives);
    773 	if (fd == NULL) {
    774 		fdc->sc_state = DEVIDLE;
    775 		return 1;
    776 	}
    777 
    778 	/* Is there a transfer to this drive?  If not, deactivate drive. */
    779 	bp = BUFQ_PEEK(fd->sc_q);
    780 	if (bp == NULL) {
    781 		fd->sc_ops = 0;
    782 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    783 		fd->sc_active = 0;
    784 		goto loop;
    785 	}
    786 
    787 	switch (fdc->sc_state) {
    788 	case DEVIDLE:
    789 		fdc->sc_errors = 0;
    790 		fd->sc_skip = 0;
    791 		fd->sc_bcount = bp->b_bcount;
    792 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
    793 		callout_stop(&fd->sc_motoroff_ch);
    794 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
    795 			fdc->sc_state = MOTORWAIT;
    796 			return 1;
    797 		}
    798 		if ((fd->sc_flags & FD_MOTOR) == 0) {
    799 			/* Turn on the motor, being careful about pairing. */
    800 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
    801 			if (ofd && ofd->sc_flags & FD_MOTOR) {
    802 				callout_stop(&ofd->sc_motoroff_ch);
    803 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    804 			}
    805 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
    806 			fd_set_motor(fdc, 0);
    807 			fdc->sc_state = MOTORWAIT;
    808 			/* Allow .25s for motor to stabilize. */
    809 			callout_reset(&fd->sc_motoron_ch, hz / 4,
    810 			    fd_motor_on, fd);
    811 			return 1;
    812 		}
    813 		/* Make sure the right drive is selected. */
    814 		fd_set_motor(fdc, 0);
    815 
    816 		/* fall through */
    817 	case DOSEEK:
    818 	doseek:
    819 		if (fd->sc_cylin == bp->b_cylinder)
    820 			goto doio;
    821 
    822 		out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
    823 		out_fdc(iot, ioh, fd->sc_type->steprate);
    824 		out_fdc(iot, ioh, 6);		/* XXX head load time == 6ms */
    825 
    826 		out_fdc(iot, ioh, NE7CMD_SEEK);	/* seek function */
    827 		out_fdc(iot, ioh, fd->sc_drive); /* drive number */
    828 		out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
    829 
    830 		fd->sc_cylin = -1;
    831 		fdc->sc_state = SEEKWAIT;
    832 
    833 		iostat_seek(fd->sc_dk.dk_stats);
    834 		disk_busy(&fd->sc_dk);
    835 
    836 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
    837 		return 1;
    838 
    839 	case DOIO:
    840 	doio:
    841 		type = fd->sc_type;
    842 		sec = fd->sc_blkno % type->seccyl;
    843 		nblks = type->seccyl - sec;
    844 		nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
    845 		nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE);
    846 		fd->sc_nblks = nblks;
    847 		fd->sc_nbytes = nblks * FDC_BSIZE;
    848 		head = sec / type->sectrac;
    849 		sec -= head * type->sectrac;
    850 #ifdef DIAGNOSTIC
    851 		{
    852 			int block;
    853 			block = (fd->sc_cylin * type->heads + head) *
    854 			    type->sectrac + sec;
    855 			if (block != fd->sc_blkno) {
    856 				printf("fdcintr: block %d != blkno %" PRId64
    857 				    "\n", block, fd->sc_blkno);
    858 #ifdef DDB
    859 				 Debugger();
    860 #endif
    861 			}
    862 		}
    863 #endif
    864 		read = (bp->b_flags & B_READ) != 0;
    865 		FDCDMA_START(fdc, (char *)bp->b_data + fd->sc_skip,
    866 		    fd->sc_nbytes, read);
    867 		bus_space_write_1(iot, ioh, FDCTL, type->rate);
    868 #ifdef FD_DEBUG
    869 		printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
    870 		    read ? "read" : "write", fd->sc_drive, fd->sc_cylin, head,
    871 		    sec, nblks);
    872 #endif
    873 		if (read)
    874 			out_fdc(iot, ioh, NE7CMD_READ);	/* READ */
    875 		else
    876 			out_fdc(iot, ioh, NE7CMD_WRITE);/* WRITE */
    877 		out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
    878 		out_fdc(iot, ioh, fd->sc_cylin);	/* track */
    879 		out_fdc(iot, ioh, head);
    880 		out_fdc(iot, ioh, sec + 1);		/* sector + 1 */
    881 		out_fdc(iot, ioh, type->secsize);	/* sector size */
    882 		out_fdc(iot, ioh, type->sectrac);	/* sectors/track */
    883 		out_fdc(iot, ioh, type->gap1);		/* gap1 size */
    884 		out_fdc(iot, ioh, type->datalen);	/* data length */
    885 		fdc->sc_state = IOCOMPLETE;
    886 
    887 		disk_busy(&fd->sc_dk);
    888 
    889 		/* allow 2 seconds for operation */
    890 		callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
    891 		return 1;				/* will return later */
    892 
    893 	case SEEKWAIT:
    894 		callout_stop(&fdc->sc_timo_ch);
    895 		fdc->sc_state = SEEKCOMPLETE;
    896 		/* allow 1/50 second for heads to settle */
    897 		callout_reset(&fdc->sc_intr_ch, hz / 50, fdcpseudointr, fdc);
    898 		return 1;
    899 
    900 	case SEEKCOMPLETE:
    901 		disk_unbusy(&fd->sc_dk, 0, 0);
    902 
    903 		/* Make sure seek really happened. */
    904 		out_fdc(iot, ioh, NE7CMD_SENSEI);
    905 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
    906 		    cyl != bp->b_cylinder * fd->sc_type->step) {
    907 #ifdef FD_DEBUG
    908 			fdcstatus(&fd->sc_dev, 2, "seek failed");
    909 #endif
    910 			fdcretry(fdc);
    911 			goto loop;
    912 		}
    913 		fd->sc_cylin = bp->b_cylinder;
    914 		goto doio;
    915 
    916 	case IOTIMEDOUT:
    917 		FDCDMA_ABORT(fdc);
    918 
    919 	case SEEKTIMEDOUT:
    920 	case RECALTIMEDOUT:
    921 	case RESETTIMEDOUT:
    922 		fdcretry(fdc);
    923 		goto loop;
    924 
    925 	case IOCOMPLETE: /* IO DONE, post-analyze */
    926 		callout_stop(&fdc->sc_timo_ch);
    927 
    928 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
    929 		    (bp->b_flags & B_READ));
    930 
    931 		i = fdcresult(fdc);
    932 		if (i != 7 || (st0 & 0xf8) != 0) {
    933 			FDCDMA_ABORT(fdc);
    934 #ifdef FD_DEBUG
    935 			fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
    936 			    "read failed" : "write failed");
    937 			printf("blkno %" PRId64 " nblks %d\n",
    938 			    fd->sc_blkno, fd->sc_nblks);
    939 #endif
    940 			fdcretry(fdc);
    941 			goto loop;
    942 		}
    943 		FDCDMA_DONE(fdc);
    944 		if (fdc->sc_errors) {
    945 			diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
    946 			    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
    947 			printf("\n");
    948 			fdc->sc_errors = 0;
    949 		}
    950 		fd->sc_blkno += fd->sc_nblks;
    951 		fd->sc_skip += fd->sc_nbytes;
    952 		fd->sc_bcount -= fd->sc_nbytes;
    953 		if (fd->sc_bcount > 0) {
    954 			bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
    955 			goto doseek;
    956 		}
    957 		fdfinish(fd, bp);
    958 		goto loop;
    959 
    960 	case DORESET:
    961 		/* try a reset, keep motor on */
    962 		fd_set_motor(fdc, 1);
    963 		delay(100);
    964 		fd_set_motor(fdc, 0);
    965 		fdc->sc_state = RESETCOMPLETE;
    966 		callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
    967 		return 1;			/* will return later */
    968 
    969 	case RESETCOMPLETE:
    970 		callout_stop(&fdc->sc_timo_ch);
    971 		/* clear the controller output buffer */
    972 		for (i = 0; i < 4; i++) {
    973 			out_fdc(iot, ioh, NE7CMD_SENSEI);
    974 			(void) fdcresult(fdc);
    975 		}
    976 
    977 		/* fall through */
    978 	case DORECAL:
    979 		out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
    980 		out_fdc(iot, ioh, fd->sc_drive);
    981 		fdc->sc_state = RECALWAIT;
    982 		callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
    983 		return 1;			/* will return later */
    984 
    985 	case RECALWAIT:
    986 		callout_stop(&fdc->sc_timo_ch);
    987 		fdc->sc_state = RECALCOMPLETE;
    988 		/* allow 1/30 second for heads to settle */
    989 		callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc);
    990 		return 1;			/* will return later */
    991 
    992 	case RECALCOMPLETE:
    993 		out_fdc(iot, ioh, NE7CMD_SENSEI);
    994 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
    995 #ifdef FD_DEBUG
    996 			fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
    997 #endif
    998 			fdcretry(fdc);
    999 			goto loop;
   1000 		}
   1001 		fd->sc_cylin = 0;
   1002 		goto doseek;
   1003 
   1004 	case MOTORWAIT:
   1005 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1006 			return 1;		/* time's not up yet */
   1007 		goto doseek;
   1008 
   1009 	default:
   1010 		fdcstatus(&fd->sc_dev, 0, "stray interrupt");
   1011 		return 1;
   1012 	}
   1013 #ifdef DIAGNOSTIC
   1014 	panic("fdcintr: impossible");
   1015 #endif
   1016 #undef	st0
   1017 #undef	cyl
   1018 }
   1019 
   1020 void
   1021 fdcretry(struct fdc_softc *fdc)
   1022 {
   1023 	struct fd_softc *fd;
   1024 	struct buf *bp;
   1025 	char bits[64];
   1026 
   1027 	fd = TAILQ_FIRST(&fdc->sc_drives);
   1028 	bp = BUFQ_PEEK(fd->sc_q);
   1029 
   1030 	switch (fdc->sc_errors) {
   1031 	case 0:
   1032 		/* try again */
   1033 		fdc->sc_state = DOSEEK;
   1034 		break;
   1035 
   1036 	case 1: case 2: case 3:
   1037 		/* didn't work; try recalibrating */
   1038 		fdc->sc_state = DORECAL;
   1039 		break;
   1040 
   1041 	case 4:
   1042 		/* still no go; reset the bastard */
   1043 		fdc->sc_state = DORESET;
   1044 		break;
   1045 
   1046 	default:
   1047 		diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1048 		    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1049 
   1050 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
   1051 		    NE7_ST0BITS, bits, sizeof(bits)));
   1052 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
   1053 		    NE7_ST1BITS, bits, sizeof(bits)));
   1054 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
   1055 		    NE7_ST2BITS, bits, sizeof(bits)));
   1056 		printf(" cyl %d head %d sec %d)\n",
   1057 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
   1058 
   1059 		bp->b_flags |= B_ERROR;
   1060 		bp->b_error = EIO;
   1061 		fdfinish(fd, bp);
   1062 	}
   1063 	fdc->sc_errors++;
   1064 }
   1065 
   1066 int
   1067 fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1068 {
   1069 	struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
   1070 	struct disklabel buffer;
   1071 	int error;
   1072 
   1073 	switch (cmd) {
   1074 	case DIOCGDINFO:
   1075 		memset(&buffer, 0, sizeof(buffer));
   1076 
   1077 		buffer.d_secpercyl = fd->sc_type->seccyl;
   1078 		buffer.d_type = DTYPE_FLOPPY;
   1079 		buffer.d_secsize = FDC_BSIZE;
   1080 
   1081 		if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
   1082 			return EINVAL;
   1083 
   1084 		*(struct disklabel *)addr = buffer;
   1085 		return 0;
   1086 
   1087 	case DIOCWLABEL:
   1088 		if ((flag & FWRITE) == 0)
   1089 			return EBADF;
   1090 		/* XXX do something */
   1091 		return 0;
   1092 
   1093 	case DIOCWDINFO:
   1094 		if ((flag & FWRITE) == 0)
   1095 			return EBADF;
   1096 
   1097 		error = setdisklabel(&buffer, (struct disklabel *)addr,
   1098 		    0, NULL);
   1099 		if (error)
   1100 			return error;
   1101 
   1102 		error = writedisklabel(dev, fdstrategy, &buffer, NULL);
   1103 		return error;
   1104 
   1105 	default:
   1106 		return ENOTTY;
   1107 	}
   1108 
   1109 #ifdef DIAGNOSTIC
   1110 	panic("fdioctl: impossible");
   1111 #endif
   1112 }
   1113 
   1114 /*
   1115  * Mountroot hook: prompt the user to enter the root file system floppy.
   1116  */
   1117 void
   1118 fd_mountroot_hook(struct device *dev)
   1119 {
   1120 	int c;
   1121 
   1122 	printf("Insert filesystem floppy and press return.");
   1123 	cnpollc(1);
   1124 	for (;;) {
   1125 		c = cngetc();
   1126 		if ((c == '\r') || (c == '\n')) {
   1127 			printf("\n");
   1128 			break;
   1129 		}
   1130 	}
   1131 	cnpollc(0);
   1132 }
   1133