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