fd.c revision 1.19 1 /* $NetBSD: fd.c,v 1.19 1998/07/04 22:18:46 jonathan Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994, 1995 Charles Hannum.
5 * Copyright (c) 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Don Ahn.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)fd.c 7.4 (Berkeley) 5/25/91
40 */
41
42 #include "opt_ddb.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/conf.h>
48 #include <sys/file.h>
49 #include <sys/stat.h>
50 #include <sys/ioctl.h>
51 #include <sys/malloc.h>
52 #include <sys/device.h>
53 #include <sys/disklabel.h>
54 #include <sys/dkstat.h>
55 #include <sys/disk.h>
56 #include <sys/buf.h>
57 #include <sys/uio.h>
58 #include <sys/syslog.h>
59 #include <sys/queue.h>
60
61 #include <machine/cpu.h>
62
63 #include <x68k/x68k/iodevice.h>
64 #include <x68k/dev/dmavar.h>
65 #include <x68k/dev/fdreg.h>
66 #include <x68k/dev/opmreg.h>
67
68 #include "locators.h"
69
70 #define infdc (IODEVbase->io_fdc)
71
72 #ifdef DEBUG
73 #define DPRINTF(x) if (fddebug) printf x
74 int fddebug = 0;
75 #else
76 #define DPRINTF(x)
77 #endif
78
79 #define FDUNIT(dev) (minor(dev) / 8)
80 #define FDTYPE(dev) (minor(dev) % 8)
81
82 #define b_cylin b_resid
83
84 enum fdc_state {
85 DEVIDLE = 0,
86 MOTORWAIT,
87 DOSEEK,
88 SEEKWAIT,
89 SEEKTIMEDOUT,
90 SEEKCOMPLETE,
91 DOIO,
92 IOCOMPLETE,
93 IOTIMEDOUT,
94 DORESET,
95 RESETCOMPLETE,
96 RESETTIMEDOUT,
97 DORECAL,
98 RECALWAIT,
99 RECALTIMEDOUT,
100 RECALCOMPLETE,
101 DOCOPY,
102 DOIOHALF,
103 COPYCOMPLETE,
104 };
105
106 /* software state, per controller */
107 struct fdc_softc {
108 struct device sc_dev; /* boilerplate */
109 u_char sc_flags;
110
111 struct fd_softc *sc_fd[4]; /* pointers to children */
112 TAILQ_HEAD(drivehead, fd_softc) sc_drives;
113 enum fdc_state sc_state;
114 int sc_errors; /* number of retries so far */
115 u_char sc_status[7]; /* copy of registers */
116 } fdc_softc;
117
118 bdev_decl(fd);
119 cdev_decl(fd);
120
121 int fdcintr __P((void));
122 void fdcreset __P((void));
123
124 /* controller driver configuration */
125 int fdcprobe __P((struct device *, void *, void *));
126 void fdcattach __P((struct device *, struct device *, void *));
127 int fdprint __P((void *, const char *));
128
129 struct cfattach fdc_ca = {
130 sizeof(struct fdc_softc), fdcprobe, fdcattach
131 };
132
133 extern struct cfdriver fdc_cd;
134
135 /*
136 * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
137 * we tell them apart.
138 */
139 struct fd_type {
140 int sectrac; /* sectors per track */
141 int heads; /* number of heads */
142 int seccyl; /* sectors per cylinder */
143 int secsize; /* size code for sectors */
144 int datalen; /* data len when secsize = 0 */
145 int steprate; /* step rate and head unload time */
146 int gap1; /* gap len between sectors */
147 int gap2; /* formatting gap */
148 int tracks; /* total num of tracks */
149 int size; /* size of disk in sectors */
150 int step; /* steps per cylinder */
151 int rate; /* transfer speed code */
152 char *name;
153 };
154
155 /* The order of entries in the following table is important -- BEWARE! */
156 struct fd_type fd_types[] = {
157 { 8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS, "1.2MB/[1024bytes/sector]" }, /* 1.2 MB japanese format */
158 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB" }, /* 1.44MB diskette */
159 { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB" }, /* 1.2 MB AT-diskettes */
160 { 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" }, /* 360kB in 1.2MB drive */
161 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" }, /* 360kB PC diskettes */
162 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB" }, /* 3.5" 720kB diskette */
163 { 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x" }, /* 720kB in 1.2MB drive */
164 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x" }, /* 360kB in 720kB drive */
165 };
166
167 /* software state, per disk (with up to 4 disks per ctlr) */
168 struct fd_softc {
169 struct device sc_dev;
170 struct disk sc_dk;
171
172 struct fd_type *sc_deftype; /* default type descriptor */
173 struct fd_type *sc_type; /* current type descriptor */
174
175 daddr_t sc_blkno; /* starting block number */
176 int sc_bcount; /* byte count left */
177 int sc_skip; /* bytes already transferred */
178 int sc_nblks; /* number of blocks currently tranferring */
179 int sc_nbytes; /* number of bytes currently tranferring */
180
181 int sc_drive; /* physical unit number */
182 int sc_flags;
183 #define FD_BOPEN 0x01 /* it's open */
184 #define FD_COPEN 0x02 /* it's open */
185 #define FD_OPEN (FD_BOPEN|FD_COPEN) /* it's open */
186 #define FD_MOTOR 0x04 /* motor should be on */
187 #define FD_MOTOR_WAIT 0x08 /* motor coming up */
188 #define FD_ALIVE 0x10 /* alive */
189 int sc_cylin; /* where we think the head is */
190
191 TAILQ_ENTRY(fd_softc) sc_drivechain;
192 int sc_ops; /* I/O ops since last switch */
193 struct buf sc_q; /* head of buf chain */
194 u_char *sc_copybuf; /* for secsize >=3 */
195 u_char sc_part; /* for secsize >=3 */
196 #define SEC_P10 0x02 /* first part */
197 #define SEC_P01 0x01 /* second part */
198 #define SEC_P11 0x03 /* both part */
199 };
200
201 /* floppy driver configuration */
202 int fdprobe __P((struct device *, void *, void *));
203 void fdattach __P((struct device *, struct device *, void *));
204
205 struct cfattach fd_ca = {
206 sizeof(struct fd_softc), fdprobe, fdattach
207 };
208
209 extern struct cfdriver fd_cd;
210
211 void fdstrategy __P((struct buf *));
212 void fdstart __P((struct fd_softc *fd));
213
214 struct dkdriver fddkdriver = { fdstrategy };
215
216 void fd_set_motor __P((struct fdc_softc *fdc, int reset));
217 void fd_motor_off __P((void *arg));
218 void fd_motor_on __P((void *arg));
219 int fdcresult __P((struct fdc_softc *fdc));
220 int out_fdc __P((u_char x));
221 void fdcstart __P((struct fdc_softc *fdc));
222 void fdcstatus __P((struct device *dv, int n, char *s));
223 void fdctimeout __P((void *arg));
224 void fdcpseudointr __P((void *arg));
225 void fdcretry __P((struct fdc_softc *fdc));
226 void fdfinish __P((struct fd_softc *fd, struct buf *bp));
227 __inline struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
228 static int fdcpoll __P((struct fdc_softc *));
229 static int fdgetdisklabel __P((struct fd_softc *, dev_t));
230 static void fd_do_eject __P((int));
231
232 void fd_mountroot_hook __P((struct device *));
233
234 /* dma transfer routines */
235 __inline static void fdc_dmastart __P((int, caddr_t, int));
236 void fdcdmaintr __P((void));
237 void fdcdmaerrintr __P((void));
238
239 #define FDDI_EN 0x02
240 #define FDCI_EN 0x04
241 #define FDD_INT 0x40
242 #define FDC_INT 0x80
243
244 #define DMA_BRD 0x01
245 #define DMA_BWR 0x02
246
247 #define DRQ 0
248
249 static u_char *fdc_dmabuf;
250
251 __inline static void
252 fdc_dmastart(read, addr, count)
253 int read;
254 caddr_t addr;
255 int count;
256 {
257 volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
258
259 DPRINTF(("fdc_dmastart: (%s, addr = %p, count = %d\n",
260 read ? "read" : "write", addr, count));
261 if (dmarangecheck((vm_offset_t)addr, count)) {
262 dma_bouncebytes[DRQ] = count;
263 dma_dataaddr[DRQ] = addr;
264 if (!(read)) {
265 bcopy(addr, dma_bouncebuf[DRQ], count);
266 dma_bounced[DRQ] = DMA_BWR;
267 } else {
268 dma_bounced[DRQ] = DMA_BRD;
269 }
270 addr = dma_bouncebuf[DRQ];
271 } else {
272 dma_bounced[DRQ] = 0;
273 }
274
275 dmac->csr = 0xff;
276 dmac->ocr = read ? 0xb2 : 0x32;
277 dmac->mtc = (unsigned short)count;
278 asm("nop");
279 asm("nop");
280 dmac->mar = (unsigned long)kvtop(addr);
281 #if defined(M68040) || defined(M68060)
282 /*
283 * Push back dirty cache lines
284 */
285 if (mmutype == MMU_68040)
286 DCFP(kvtop(addr));
287 #endif
288 dmac->ccr = 0x88;
289 }
290
291 void
292 fdcdmaintr()
293 {
294 volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
295 dmac->csr = 0xff;
296 PCIA(); /* XXX? by oki */
297 if (dma_bounced[DRQ] == DMA_BRD) {
298 bcopy(dma_bouncebuf[DRQ], dma_dataaddr[DRQ], dma_bouncebytes[DRQ]);
299 }
300 dma_bounced[DRQ] = 0;
301 }
302
303 void
304 fdcdmaerrintr()
305 {
306 volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
307 printf("fdcdmaerrintr: csr=%x, cer=%x\n", dmac->csr, dmac->cer);
308 dmac->csr = 0xff;
309 }
310
311 int
312 fdcprobe(parent, match, aux)
313 struct device *parent;
314 void *match, *aux;
315 {
316 if (strcmp("fdc", aux) != 0)
317 return 0;
318 return 1;
319 }
320
321 /*
322 * Arguments passed between fdcattach and fdprobe.
323 */
324 struct fdc_attach_args {
325 int fa_drive;
326 struct fd_type *fa_deftype;
327 };
328
329 /*
330 * Print the location of a disk drive (called just before attaching the
331 * the drive). If `fdc' is not NULL, the drive was found but was not
332 * in the system config file; print the drive name as well.
333 * Return QUIET (config_find ignores this if the device was configured) to
334 * avoid printing `fdN not configured' messages.
335 */
336 int
337 fdprint(aux, fdc)
338 void *aux;
339 const char *fdc;
340 {
341 register struct fdc_attach_args *fa = aux;
342
343 if (!fdc)
344 printf(" drive %d", fa->fa_drive);
345 return QUIET;
346 }
347
348 void
349 fdcattach(parent, self, aux)
350 struct device *parent, *self;
351 void *aux;
352 {
353 struct fdc_softc *fdc = (void *)self;
354 volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
355 struct fdc_attach_args fa;
356
357 fdc->sc_state = DEVIDLE;
358 TAILQ_INIT(&fdc->sc_drives);
359
360 fdc->sc_flags = 0;
361
362 /* reset */
363 ioctlr.intr &= (~FDDI_EN);
364 ioctlr.intr |= FDCI_EN;
365 fdcresult(fdc);
366 fdcreset();
367
368 /* Initialize DMAC channel */
369 dmac->dcr = 0x80;
370 dmac->scr = 0x04;
371 dmac->csr = 0xff;
372 dmac->cpr = 0x00;
373 dmac->dar = (unsigned long) kvtop((void *)&infdc.data);
374 dmac->mfc = 0x05;
375 dmac->dfc = 0x05;
376 dmac->bfc = 0x05;
377 dmac->niv = 0x64;
378 dmac->eiv = 0x65;
379
380 printf(": uPD72065 FDC\n");
381 out_fdc(NE7CMD_SPECIFY);/* specify command */
382 out_fdc(0xd0);
383 out_fdc(0x10);
384
385 fdc_dmabuf = (u_char *)malloc(NBPG, M_DEVBUF, M_WAITOK);
386 if (fdc_dmabuf == 0)
387 printf("fdcattach: WARNING!! malloc() failed.\n");
388 dma_bouncebuf[DRQ] = fdc_dmabuf;
389
390 /* physical limit: four drives per controller. */
391 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
392 (void)config_found(self, (void *)&fa, fdprint);
393 }
394 }
395
396 void
397 fdcreset()
398 {
399 infdc.stat = FDC_RESET;
400 }
401
402 static int
403 fdcpoll(fdc)
404 struct fdc_softc *fdc;
405 {
406 int i = 25000;
407 while (--i > 0) {
408 if ((ioctlr.intr & 0x80)) {
409 out_fdc(NE7CMD_SENSEI);
410 fdcresult(fdc);
411 break;
412 }
413 DELAY(100);
414 }
415 return i;
416 }
417
418 int
419 fdprobe(parent, match, aux)
420 struct device *parent;
421 void *match, *aux;
422 {
423 struct fdc_softc *fdc = (void *)parent;
424 struct cfdata *cf = match;
425 struct fd_type *type;
426 int drive = cf->cf_unit;
427 int n;
428 int found = 0;
429 int i;
430
431 if (cf->cf_loc[FDCCF_UNIT] != FDCCF_UNIT_DEFAULT &&
432 cf->cf_loc[FDCCF_UNIT] != drive)
433 return 0;
434
435 type = &fd_types[0]; /* XXX 1.2MB */
436
437 ioctlr.intr &= (~FDCI_EN);
438
439 /* select drive and turn on motor */
440 infdc.select = 0x80 | (type->rate << 4)| drive;
441 fdc_force_ready(FDCRDY);
442 fdcpoll(fdc);
443
444 retry:
445 out_fdc(NE7CMD_RECAL);
446 out_fdc(drive);
447
448 i = 25000;
449 while (--i > 0) {
450 if ((ioctlr.intr & 0x80)) {
451 out_fdc(NE7CMD_SENSEI);
452 n = fdcresult(fdc);
453 break;
454 }
455 DELAY(100);
456 }
457
458 #ifdef FDDEBUG
459 {
460 int i;
461 printf("fdprobe: status");
462 for (i = 0; i < n; i++)
463 printf(" %x", fdc->sc_status[i]);
464 printf("\n");
465 }
466 #endif
467
468 if (n == 2) {
469 if ((fdc->sc_status[0] & 0xf0) == 0x20) {
470 found = 1;
471 } else if ((fdc->sc_status[0] & 0xf0) == 0xc0) {
472 goto retry;
473 }
474 }
475
476 /* turn off motor */
477 infdc.select = (type->rate << 4)| drive;
478 fdc_force_ready(FDCSTBY);
479 if (!found) {
480 ioctlr.intr |= FDCI_EN;
481 return 0;
482 }
483
484 return 1;
485 }
486
487 void
488 fdattach(parent, self, aux)
489 struct device *parent;
490 struct device *self;
491 void *aux;
492 {
493 struct fdc_softc *fdc = (void *)parent;
494 register struct fd_softc *fd = (void *)self;
495 struct fdc_attach_args *fa = aux;
496 int drive = fa->fa_drive;
497 struct fd_type *type = &fd_types[0]; /* XXX 1.2MB */
498
499 fd->sc_flags = 0;
500
501 ioctlr.intr |= FDCI_EN;
502
503 if (type)
504 printf(": %s %d cyl, %d head, %d sec\n", type->name,
505 type->tracks, type->heads, type->sectrac);
506 else
507 printf(": density unknown\n");
508
509 fd->sc_cylin = -1;
510 fd->sc_drive = drive;
511 fd->sc_deftype = type;
512 fdc->sc_fd[drive] = fd;
513
514 fd->sc_copybuf = (u_char *)malloc(NBPG, M_DEVBUF, M_WAITOK);
515 if (fd->sc_copybuf == 0)
516 printf("fdprobe: WARNING!! malloc() failed.\n");
517 fd->sc_flags |= FD_ALIVE;
518
519 /*
520 * Initialize and attach the disk structure.
521 */
522 fd->sc_dk.dk_name = fd->sc_dev.dv_xname;
523 fd->sc_dk.dk_driver = &fddkdriver;
524 disk_attach(&fd->sc_dk);
525
526 /*
527 * Establish a mountroot_hook anyway in case we booted
528 * with RB_ASKNAME and get selected as the boot device.
529 */
530 mountroothook_establish(fd_mountroot_hook, &fd->sc_dev);
531 }
532
533 __inline struct fd_type *
534 fd_dev_to_type(fd, dev)
535 struct fd_softc *fd;
536 dev_t dev;
537 {
538 int type = FDTYPE(dev);
539
540 if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
541 return NULL;
542 return &fd_types[type];
543 }
544
545 void
546 fdstrategy(bp)
547 register struct buf *bp; /* IO operation to perform */
548 {
549 struct fd_softc *fd;
550 int unit = FDUNIT(bp->b_dev);
551 int sz;
552 int s;
553
554 if (unit >= fd_cd.cd_ndevs ||
555 (fd = fd_cd.cd_devs[unit]) == 0 ||
556 bp->b_blkno < 0 ||
557 (bp->b_bcount % FDC_BSIZE) != 0) {
558 #ifdef FDDEBUG
559 printf("fdstrategy: unit=%d, blkno=%d, bcount=%d\n", unit,
560 bp->b_blkno, bp->b_bcount);
561 #endif
562 bp->b_error = EINVAL;
563 goto bad;
564 }
565
566 /* If it's a null transfer, return immediately. */
567 if (bp->b_bcount == 0)
568 goto done;
569
570 sz = howmany(bp->b_bcount, FDC_BSIZE);
571
572 if (bp->b_blkno + sz > (fd->sc_type->size << (fd->sc_type->secsize - 2))) {
573 sz = (fd->sc_type->size << (fd->sc_type->secsize - 2)) - bp->b_blkno;
574 if (sz == 0) {
575 /* If exactly at end of disk, return EOF. */
576 bp->b_resid = bp->b_bcount;
577 goto done;
578 }
579 if (sz < 0) {
580 /* If past end of disk, return EINVAL. */
581 bp->b_error = EINVAL;
582 goto bad;
583 }
584 /* Otherwise, truncate request. */
585 bp->b_bcount = sz << DEV_BSHIFT;
586 }
587
588 bp->b_cylin = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE)
589 / (fd->sc_type->seccyl * (1 << (fd->sc_type->secsize - 2)));
590
591 DPRINTF(("fdstrategy: %s b_blkno %d b_bcount %ld cylin %ld\n",
592 bp->b_flags & B_READ ? "read" : "write",
593 bp->b_blkno, bp->b_bcount, bp->b_cylin));
594 /* Queue transfer on drive, activate drive and controller if idle. */
595 s = splbio();
596 disksort(&fd->sc_q, bp);
597 untimeout(fd_motor_off, fd); /* a good idea */
598 if (!fd->sc_q.b_active)
599 fdstart(fd);
600 #ifdef DIAGNOSTIC
601 else {
602 struct fdc_softc *fdc = fdc_cd.cd_devs[0]; /* XXX */
603 if (fdc->sc_state == DEVIDLE) {
604 printf("fdstrategy: controller inactive\n");
605 fdcstart(fdc);
606 }
607 }
608 #endif
609 splx(s);
610 return;
611
612 bad:
613 bp->b_flags |= B_ERROR;
614 done:
615 /* Toss transfer; we're done early. */
616 biodone(bp);
617 }
618
619 void
620 fdstart(fd)
621 struct fd_softc *fd;
622 {
623 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
624 int active = fdc->sc_drives.tqh_first != 0;
625
626 /* Link into controller queue. */
627 fd->sc_q.b_active = 1;
628 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
629
630 /* If controller not already active, start it. */
631 if (!active)
632 fdcstart(fdc);
633 }
634
635 void
636 fdfinish(fd, bp)
637 struct fd_softc *fd;
638 struct buf *bp;
639 {
640 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
641
642 /*
643 * Move this drive to the end of the queue to give others a `fair'
644 * chance. We only force a switch if N operations are completed while
645 * another drive is waiting to be serviced, since there is a long motor
646 * startup delay whenever we switch.
647 */
648 if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
649 fd->sc_ops = 0;
650 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
651 if (bp->b_actf) {
652 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
653 } else
654 fd->sc_q.b_active = 0;
655 }
656 bp->b_resid = fd->sc_bcount;
657 fd->sc_skip = 0;
658 fd->sc_q.b_actf = bp->b_actf;
659 biodone(bp);
660 /* turn off motor 5s from now */
661 timeout(fd_motor_off, fd, 5 * hz);
662 fdc->sc_state = DEVIDLE;
663 }
664
665 int
666 fdread(dev, uio, flags)
667 dev_t dev;
668 struct uio *uio;
669 int flags;
670 {
671
672 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
673 }
674
675 int
676 fdwrite(dev, uio, flags)
677 dev_t dev;
678 struct uio *uio;
679 int flags;
680 {
681
682 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
683 }
684
685 void
686 fd_set_motor(fdc, reset)
687 struct fdc_softc *fdc;
688 int reset;
689 {
690 struct fd_softc *fd;
691 int n;
692
693 DPRINTF(("fd_set_motor:\n"));
694 for (n = 0; n < 4; n++)
695 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR)) {
696 infdc.select = 0x80 | (fd->sc_type->rate << 4)| n;
697 }
698 }
699
700 void
701 fd_motor_off(arg)
702 void *arg;
703 {
704 struct fd_softc *fd = arg;
705 int s;
706
707 DPRINTF(("fd_motor_off:\n"));
708
709 s = splbio();
710 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
711 infdc.select = (fd->sc_type->rate << 4) | fd->sc_drive;
712 #if 0
713 fd_set_motor((struct fdc_softc *)&fdc_softc[0], 0); /* XXX */
714 #endif
715 splx(s);
716 }
717
718 void
719 fd_motor_on(arg)
720 void *arg;
721 {
722 struct fd_softc *fd = arg;
723 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
724 int s;
725
726 DPRINTF(("fd_motor_on:\n"));
727
728 s = splbio();
729 fd->sc_flags &= ~FD_MOTOR_WAIT;
730 if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
731 (void) fdcintr();
732 splx(s);
733 }
734
735 int
736 fdcresult(fdc)
737 struct fdc_softc *fdc;
738 {
739 u_char i;
740 int j = 100000,
741 n = 0;
742
743 for (; j; j--) {
744
745 i = infdc.stat & (NE7_DIO | NE7_RQM | NE7_CB);
746
747
748 if (i == NE7_RQM)
749 return n;
750 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
751 if (n >= sizeof(fdc->sc_status)) {
752 log(LOG_ERR, "fdcresult: overrun\n");
753 return -1;
754 }
755 fdc->sc_status[n++] = infdc.data;
756 }
757 }
758 log(LOG_ERR, "fdcresult: timeout\n");
759 return -1;
760 }
761
762 int
763 out_fdc(x)
764 u_char x;
765 {
766 int i = 100000;
767
768 while ((infdc.stat & NE7_DIO) && i-- > 0);
769 if (i <= 0)
770 return -1;
771 while ((infdc.stat & NE7_RQM) == 0 && i-- > 0);
772 if (i <= 0)
773 return -1;
774
775 infdc.data = x;
776
777 return 0;
778 }
779
780 int
781 fdopen(dev, flags, mode, p)
782 dev_t dev;
783 int flags, mode;
784 struct proc *p;
785 {
786 int unit;
787 struct fd_softc *fd;
788 struct fd_type *type;
789
790 unit = FDUNIT(dev);
791 if (unit >= fd_cd.cd_ndevs)
792 return ENXIO;
793 fd = fd_cd.cd_devs[unit];
794 if (fd == 0)
795 return ENXIO;
796 type = fd_dev_to_type(fd, dev);
797 if (type == NULL)
798 return ENXIO;
799
800 if ((fd->sc_flags & FD_OPEN) != 0 &&
801 fd->sc_type != type)
802 return EBUSY;
803
804 if ((fd->sc_flags & FD_OPEN) == 0) {
805 /* Lock eject button */
806 infdc.drvstat = 0x40 | ( 1 << unit);
807 infdc.drvstat = 0x40;
808 }
809
810 fd->sc_type = type;
811 fd->sc_cylin = -1;
812
813 switch (mode) {
814 case S_IFCHR:
815 fd->sc_flags |= FD_COPEN;
816 break;
817 case S_IFBLK:
818 fd->sc_flags |= FD_BOPEN;
819 break;
820 }
821
822 fdgetdisklabel(fd, dev);
823
824 return 0;
825 }
826
827 int
828 fdclose(dev, flags, mode, p)
829 dev_t dev;
830 int flags, mode;
831 struct proc *p;
832 {
833 int unit = FDUNIT(dev);
834 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
835
836 DPRINTF(("fdclose %d\n", unit));
837
838 switch (mode) {
839 case S_IFCHR:
840 fd->sc_flags &= ~FD_COPEN;
841 break;
842 case S_IFBLK:
843 fd->sc_flags &= ~FD_BOPEN;
844 break;
845 }
846
847 if ((fd->sc_flags & FD_OPEN) == 0) {
848 infdc.drvstat = ( 1 << unit);
849 infdc.drvstat = 0x00;
850 }
851 return 0;
852 }
853
854 void
855 fdcstart(fdc)
856 struct fdc_softc *fdc;
857 {
858
859 #ifdef DIAGNOSTIC
860 /* only got here if controller's drive queue was inactive; should
861 be in idle state */
862 if (fdc->sc_state != DEVIDLE) {
863 printf("fdcstart: not idle\n");
864 return;
865 }
866 #endif
867 (void) fdcintr();
868 }
869
870 void
871 fdcstatus(dv, n, s)
872 struct device *dv;
873 int n;
874 char *s;
875 {
876 struct fdc_softc *fdc = (void *)dv->dv_parent;
877 char bits[64];
878
879 if (n == 0) {
880 out_fdc(NE7CMD_SENSEI);
881 (void) fdcresult(fdc);
882 n = 2;
883 }
884
885 printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
886
887 switch (n) {
888 case 0:
889 printf("\n");
890 break;
891 case 2:
892 printf(" (st0 %s cyl %d)\n",
893 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
894 bits, sizeof(bits)), fdc->sc_status[1]);
895 break;
896 case 7:
897 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
898 NE7_ST0BITS, bits, sizeof(bits)));
899 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
900 NE7_ST1BITS, bits, sizeof(bits)));
901 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
902 NE7_ST2BITS, bits, sizeof(bits)));
903 printf(" cyl %d head %d sec %d)\n",
904 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
905 break;
906 #ifdef DIAGNOSTIC
907 default:
908 printf(" fdcstatus: weird size: %d\n", n);
909 break;
910 #endif
911 }
912 }
913
914 void
915 fdctimeout(arg)
916 void *arg;
917 {
918 struct fdc_softc *fdc = arg;
919 struct fd_softc *fd = fdc->sc_drives.tqh_first;
920 int s;
921
922 s = splbio();
923 fdcstatus(&fd->sc_dev, 0, "timeout");
924
925 if (fd->sc_q.b_actf)
926 fdc->sc_state++;
927 else
928 fdc->sc_state = DEVIDLE;
929
930 (void) fdcintr();
931 splx(s);
932 }
933
934 void
935 fdcpseudointr(arg)
936 void *arg;
937 {
938 int s;
939
940 /* just ensure it has the right spl */
941 s = splbio();
942 (void) fdcintr();
943 splx(s);
944 }
945
946 int
947 fdcintr()
948 {
949 struct fdc_softc *fdc = fdc_cd.cd_devs[0]; /* XXX */
950 #define st0 fdc->sc_status[0]
951 #define cyl fdc->sc_status[1]
952 struct fd_softc *fd;
953 struct buf *bp;
954 int read, head, sec, pos, i, sectrac, nblks;
955 int tmp;
956 struct fd_type *type;
957
958 loop:
959 fd = fdc->sc_drives.tqh_first;
960 if (fd == NULL) {
961 DPRINTF(("fdcintr: set DEVIDLE\n"));
962 if (fdc->sc_state == DEVIDLE) {
963 if ((ioctlr.intr & 0x80)) {
964 out_fdc(NE7CMD_SENSEI);
965 if ((tmp = fdcresult(fdc)) != 2 || (st0 & 0xf8) != 0x20) {
966 goto loop;
967 }
968 }
969 }
970 /* no drives waiting; end */
971 fdc->sc_state = DEVIDLE;
972 return 1;
973 }
974
975 /* Is there a transfer to this drive? If not, deactivate drive. */
976 bp = fd->sc_q.b_actf;
977 if (bp == NULL) {
978 fd->sc_ops = 0;
979 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
980 fd->sc_q.b_active = 0;
981 goto loop;
982 }
983
984 switch (fdc->sc_state) {
985 case DEVIDLE:
986 DPRINTF(("fdcintr: in DEVIDLE\n"));
987 fdc->sc_errors = 0;
988 fd->sc_skip = 0;
989 fd->sc_bcount = bp->b_bcount;
990 fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
991 untimeout(fd_motor_off, fd);
992 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
993 fdc->sc_state = MOTORWAIT;
994 return 1;
995 }
996 if ((fd->sc_flags & FD_MOTOR) == 0) {
997 /* Turn on the motor */
998 /* being careful about other drives. */
999 for (i = 0; i < 4; i++) {
1000 struct fd_softc *ofd = fdc->sc_fd[i];
1001 if (ofd && ofd->sc_flags & FD_MOTOR) {
1002 untimeout(fd_motor_off, ofd);
1003 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1004 break;
1005 }
1006 }
1007 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1008 fd_set_motor(fdc, 0);
1009 fdc->sc_state = MOTORWAIT;
1010 /* allow .5s for motor to stabilize */
1011 timeout(fd_motor_on, fd, hz / 2);
1012 return 1;
1013 }
1014 /* Make sure the right drive is selected. */
1015 fd_set_motor(fdc, 0);
1016
1017 /* fall through */
1018 case DOSEEK:
1019 doseek:
1020 DPRINTF(("fdcintr: in DOSEEK\n"));
1021 if (fd->sc_cylin == bp->b_cylin)
1022 goto doio;
1023
1024 out_fdc(NE7CMD_SPECIFY);/* specify command */
1025 out_fdc(0xd0); /* XXX const */
1026 out_fdc(0x10);
1027
1028 out_fdc(NE7CMD_SEEK); /* seek function */
1029 out_fdc(fd->sc_drive); /* drive number */
1030 out_fdc(bp->b_cylin * fd->sc_type->step);
1031
1032 fd->sc_cylin = -1;
1033 fdc->sc_state = SEEKWAIT;
1034
1035 fd->sc_dk.dk_seek++;
1036 disk_busy(&fd->sc_dk);
1037
1038 timeout(fdctimeout, fdc, 4 * hz);
1039 return 1;
1040
1041 case DOIO:
1042 doio:
1043 DPRINTF(("fdcintr: DOIO: "));
1044 type = fd->sc_type;
1045 sectrac = type->sectrac;
1046 pos = fd->sc_blkno % (sectrac * (1 << (type->secsize - 2)));
1047 sec = pos / (1 << (type->secsize - 2));
1048 if (type->secsize == 2) {
1049 fd->sc_part = SEC_P11;
1050 nblks = (sectrac - sec) << (type->secsize - 2);
1051 nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
1052 DPRINTF(("nblks(0)"));
1053 } else if ((fd->sc_blkno % 2) == 0) {
1054 if (fd->sc_bcount & 0x00000200) {
1055 if (fd->sc_bcount == FDC_BSIZE) {
1056 fd->sc_part = SEC_P10;
1057 nblks = 1;
1058 DPRINTF(("nblks(1)"));
1059 } else {
1060 fd->sc_part = SEC_P11;
1061 nblks = (sectrac - sec) * 2;
1062 nblks = min(nblks, fd->sc_bcount
1063 / FDC_BSIZE - 1);
1064 DPRINTF(("nblks(2)"));
1065 }
1066 } else {
1067 fd->sc_part = SEC_P11;
1068 nblks = (sectrac - sec)
1069 << (type->secsize - 2);
1070 nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
1071 DPRINTF(("nblks(3)"));
1072 }
1073 } else {
1074 fd->sc_part = SEC_P01;
1075 nblks = 1;
1076 DPRINTF(("nblks(4)"));
1077 }
1078 nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
1079 DPRINTF((" %d\n", nblks));
1080 fd->sc_nblks = nblks;
1081 fd->sc_nbytes = nblks * FDC_BSIZE;
1082 head = (fd->sc_blkno
1083 % (type->seccyl * (1 << (type->secsize - 2))))
1084 / (type->sectrac * (1 << (type->secsize - 2)));
1085
1086 #ifdef DIAGNOSTIC
1087 {int block;
1088 block = ((fd->sc_cylin * type->heads + head) * type->sectrac
1089 + sec) * (1 << (type->secsize - 2));
1090 block += (fd->sc_part == SEC_P01) ? 1 : 0;
1091 if (block != fd->sc_blkno) {
1092 printf("C H R N: %d %d %d %d\n", fd->sc_cylin, head, sec, type->secsize);
1093 printf("fdcintr: doio: block %d != blkno %d\n", block, fd->sc_blkno);
1094 #ifdef DDB
1095 Debugger();
1096 #endif
1097 }}
1098 #endif
1099 read = bp->b_flags & B_READ;
1100 DPRINTF(("fdcintr: %s drive %d track %d head %d sec %d nblks %d, skip %d\n",
1101 read ? "read" : "write", fd->sc_drive, fd->sc_cylin,
1102 head, sec, nblks, fd->sc_skip));
1103 DPRINTF(("C H R N: %d %d %d %d\n", fd->sc_cylin, head, sec,
1104 type->secsize));
1105
1106 if (fd->sc_part != SEC_P11)
1107 goto docopy;
1108
1109 fdc_dmastart(read, bp->b_data + fd->sc_skip, fd->sc_nbytes);
1110 if (read)
1111 out_fdc(NE7CMD_READ); /* READ */
1112 else
1113 out_fdc(NE7CMD_WRITE); /* WRITE */
1114 out_fdc((head << 2) | fd->sc_drive);
1115 out_fdc(bp->b_cylin); /* cylinder */
1116 out_fdc(head);
1117 out_fdc(sec + 1); /* sector +1 */
1118 out_fdc(type->secsize); /* sector size */
1119 out_fdc(type->sectrac); /* sectors/track */
1120 out_fdc(type->gap1); /* gap1 size */
1121 out_fdc(type->datalen); /* data length */
1122 fdc->sc_state = IOCOMPLETE;
1123
1124 disk_busy(&fd->sc_dk);
1125
1126 /* allow 2 seconds for operation */
1127 timeout(fdctimeout, fdc, 2 * hz);
1128 return 1; /* will return later */
1129
1130 case DOCOPY:
1131 docopy:
1132 DPRINTF(("fdcintr: DOCOPY:\n"));
1133 fdc_dmastart(B_READ, fd->sc_copybuf, 1024);
1134 out_fdc(NE7CMD_READ); /* READ */
1135 out_fdc((head << 2) | fd->sc_drive);
1136 out_fdc(bp->b_cylin); /* cylinder */
1137 out_fdc(head);
1138 out_fdc(sec + 1); /* sector +1 */
1139 out_fdc(type->secsize); /* sector size */
1140 out_fdc(type->sectrac); /* sectors/track */
1141 out_fdc(type->gap1); /* gap1 size */
1142 out_fdc(type->datalen); /* data length */
1143 fdc->sc_state = COPYCOMPLETE;
1144 /* allow 2 seconds for operation */
1145 timeout(fdctimeout, fdc, 2 * hz);
1146 return 1; /* will return later */
1147
1148 case DOIOHALF:
1149 doiohalf:
1150 DPRINTF((" DOIOHALF:\n"));
1151
1152 #ifdef DIAGNOSTIC
1153 type = fd->sc_type;
1154 sectrac = type->sectrac;
1155 pos = fd->sc_blkno % (sectrac * (1 << (type->secsize - 2)));
1156 sec = pos / (1 << (type->secsize - 2));
1157 head = (fd->sc_blkno
1158 % (type->seccyl * (1 << (type->secsize - 2))))
1159 / (type->sectrac * (1 << (type->secsize - 2)));
1160 {int block;
1161 block = ((fd->sc_cylin * type->heads + head) * type->sectrac + sec)
1162 * (1 << (type->secsize - 2));
1163 block += (fd->sc_part == SEC_P01) ? 1 : 0;
1164 if (block != fd->sc_blkno) {
1165 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
1166 #ifdef DDB
1167 Debugger();
1168 #endif
1169 }}
1170 #endif
1171 if (read = bp->b_flags & B_READ) {
1172 bcopy(fd->sc_copybuf
1173 + (fd->sc_part & SEC_P01 ? FDC_BSIZE : 0),
1174 bp->b_data + fd->sc_skip,
1175 FDC_BSIZE);
1176 fdc->sc_state = IOCOMPLETE;
1177 goto iocomplete2;
1178 } else {
1179 bcopy(bp->b_data + fd->sc_skip,
1180 fd->sc_copybuf
1181 + (fd->sc_part & SEC_P01 ? FDC_BSIZE : 0),
1182 FDC_BSIZE);
1183 fdc_dmastart(read, fd->sc_copybuf, 1024);
1184 }
1185 out_fdc(NE7CMD_WRITE); /* WRITE */
1186 out_fdc((head << 2) | fd->sc_drive);
1187 out_fdc(bp->b_cylin); /* cylinder */
1188 out_fdc(head);
1189 out_fdc(sec + 1); /* sector +1 */
1190 out_fdc(fd->sc_type->secsize); /* sector size */
1191 out_fdc(sectrac); /* sectors/track */
1192 out_fdc(fd->sc_type->gap1); /* gap1 size */
1193 out_fdc(fd->sc_type->datalen); /* data length */
1194 fdc->sc_state = IOCOMPLETE;
1195 /* allow 2 seconds for operation */
1196 timeout(fdctimeout, fdc, 2 * hz);
1197 return 1; /* will return later */
1198
1199 case SEEKWAIT:
1200 untimeout(fdctimeout, fdc);
1201 fdc->sc_state = SEEKCOMPLETE;
1202 /* allow 1/50 second for heads to settle */
1203 /* timeout(fdcpseudointr, fdc, hz / 50);*/
1204 return 1;
1205
1206 case SEEKCOMPLETE:
1207 /* Make sure seek really happened */
1208 DPRINTF(("fdcintr: SEEKCOMPLETE: FDC status = %x\n",
1209 infdc.stat));
1210 out_fdc(NE7CMD_SENSEI);
1211 tmp = fdcresult(fdc);
1212 if ((st0 & 0xf8) == 0xc0) {
1213 DPRINTF(("fdcintr: first seek!\n"));
1214 fdc->sc_state = DORECAL;
1215 goto loop;
1216 } else if (tmp != 2 || (st0 & 0xf8) != 0x20 || cyl != bp->b_cylin) {
1217 #ifdef FDDEBUG
1218 fdcstatus(&fd->sc_dev, 2, "seek failed");
1219 #endif
1220 fdcretry(fdc);
1221 goto loop;
1222 }
1223 fd->sc_cylin = bp->b_cylin;
1224 goto doio;
1225
1226 case IOTIMEDOUT:
1227 #if 0
1228 isa_dmaabort(fdc->sc_drq);
1229 #endif
1230 case SEEKTIMEDOUT:
1231 case RECALTIMEDOUT:
1232 case RESETTIMEDOUT:
1233 fdcretry(fdc);
1234 goto loop;
1235
1236 case IOCOMPLETE: /* IO DONE, post-analyze */
1237 untimeout(fdctimeout, fdc);
1238 DPRINTF(("fdcintr: in IOCOMPLETE\n"));
1239 if ((tmp = fdcresult(fdc)) != 7 || (st0 & 0xf8) != 0) {
1240 printf("fdcintr: resnum=%d, st0=%x\n", tmp, st0);
1241 #if 0
1242 isa_dmaabort(fdc->sc_drq);
1243 #endif
1244 fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
1245 "read failed" : "write failed");
1246 printf("blkno %d nblks %d\n",
1247 fd->sc_blkno, fd->sc_nblks);
1248 fdcretry(fdc);
1249 goto loop;
1250 }
1251 #if 0
1252 isa_dmadone(bp->b_flags & B_READ, bp->b_data + fd->sc_skip,
1253 nblks * FDC_BSIZE, fdc->sc_drq);
1254 #endif
1255 iocomplete2:
1256 if (fdc->sc_errors) {
1257 diskerr(bp, "fd", "soft error", LOG_PRINTF,
1258 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1259 printf("\n");
1260 fdc->sc_errors = 0;
1261 }
1262 fd->sc_blkno += fd->sc_nblks;
1263 fd->sc_skip += fd->sc_nbytes;
1264 fd->sc_bcount -= fd->sc_nbytes;
1265 DPRINTF(("fd->sc_bcount = %d\n", fd->sc_bcount));
1266 if (fd->sc_bcount > 0) {
1267 bp->b_cylin = fd->sc_blkno
1268 / (fd->sc_type->seccyl
1269 * (1 << (fd->sc_type->secsize - 2)));
1270 goto doseek;
1271 }
1272 fdfinish(fd, bp);
1273 goto loop;
1274
1275 case COPYCOMPLETE: /* IO DONE, post-analyze */
1276 DPRINTF(("fdcintr: COPYCOMPLETE:"));
1277 untimeout(fdctimeout, fdc);
1278 if ((tmp = fdcresult(fdc)) != 7 || (st0 & 0xf8) != 0) {
1279 printf("fdcintr: resnum=%d, st0=%x\n", tmp, st0);
1280 #if 0
1281 isa_dmaabort(fdc->sc_drq);
1282 #endif
1283 fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
1284 "read failed" : "write failed");
1285 printf("blkno %d nblks %d\n",
1286 fd->sc_blkno, fd->sc_nblks);
1287 fdcretry(fdc);
1288 goto loop;
1289 }
1290 goto doiohalf;
1291
1292 case DORESET:
1293 DPRINTF(("fdcintr: in DORESET\n"));
1294 /* try a reset, keep motor on */
1295 fd_set_motor(fdc, 1);
1296 DELAY(100);
1297 fd_set_motor(fdc, 0);
1298 fdc->sc_state = RESETCOMPLETE;
1299 timeout(fdctimeout, fdc, hz / 2);
1300 return 1; /* will return later */
1301
1302 case RESETCOMPLETE:
1303 DPRINTF(("fdcintr: in RESETCOMPLETE\n"));
1304 untimeout(fdctimeout, fdc);
1305 /* clear the controller output buffer */
1306 for (i = 0; i < 4; i++) {
1307 out_fdc(NE7CMD_SENSEI);
1308 (void) fdcresult(fdc);
1309 }
1310
1311 /* fall through */
1312 case DORECAL:
1313 DPRINTF(("fdcintr: in DORECAL\n"));
1314 out_fdc(NE7CMD_RECAL); /* recalibrate function */
1315 out_fdc(fd->sc_drive);
1316 fdc->sc_state = RECALWAIT;
1317 timeout(fdctimeout, fdc, 5 * hz);
1318 return 1; /* will return later */
1319
1320 case RECALWAIT:
1321 DPRINTF(("fdcintr: in RECALWAIT\n"));
1322 untimeout(fdctimeout, fdc);
1323 fdc->sc_state = RECALCOMPLETE;
1324 /* allow 1/30 second for heads to settle */
1325 /* timeout(fdcpseudointr, fdc, hz / 30);*/
1326 return 1; /* will return later */
1327
1328 case RECALCOMPLETE:
1329 DPRINTF(("fdcintr: in RECALCOMPLETE\n"));
1330 out_fdc(NE7CMD_SENSEI);
1331 tmp = fdcresult(fdc);
1332 if ((st0 & 0xf8) == 0xc0) {
1333 DPRINTF(("fdcintr: first seek!\n"));
1334 fdc->sc_state = DORECAL;
1335 goto loop;
1336 } else if (tmp != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1337 #ifdef FDDEBUG
1338 fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
1339 #endif
1340 fdcretry(fdc);
1341 goto loop;
1342 }
1343 fd->sc_cylin = 0;
1344 goto doseek;
1345
1346 case MOTORWAIT:
1347 if (fd->sc_flags & FD_MOTOR_WAIT)
1348 return 1; /* time's not up yet */
1349 goto doseek;
1350
1351 default:
1352 fdcstatus(&fd->sc_dev, 0, "stray interrupt");
1353 return 1;
1354 }
1355 #ifdef DIAGNOSTIC
1356 panic("fdcintr: impossible");
1357 #endif
1358 #undef st0
1359 #undef cyl
1360 }
1361
1362 void
1363 fdcretry(fdc)
1364 struct fdc_softc *fdc;
1365 {
1366 struct fd_softc *fd;
1367 struct buf *bp;
1368 char bits[64];
1369
1370 DPRINTF(("fdcretry:\n"));
1371 fd = fdc->sc_drives.tqh_first;
1372 bp = fd->sc_q.b_actf;
1373
1374 switch (fdc->sc_errors) {
1375 case 0:
1376 /* try again */
1377 fdc->sc_state = SEEKCOMPLETE;
1378 break;
1379
1380 case 1: case 2: case 3:
1381 /* didn't work; try recalibrating */
1382 fdc->sc_state = DORECAL;
1383 break;
1384
1385 case 4:
1386 /* still no go; reset the bastard */
1387 fdc->sc_state = DORESET;
1388 break;
1389
1390 default:
1391 diskerr(bp, "fd", "hard error", LOG_PRINTF,
1392 fd->sc_skip, (struct disklabel *)NULL);
1393 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
1394 NE7_ST0BITS, bits,
1395 sizeof(bits)));
1396 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
1397 NE7_ST1BITS, bits,
1398 sizeof(bits)));
1399 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
1400 NE7_ST2BITS, bits,
1401 sizeof(bits)));
1402 printf(" cyl %d head %d sec %d)\n",
1403 fdc->sc_status[3],
1404 fdc->sc_status[4],
1405 fdc->sc_status[5]);
1406
1407 bp->b_flags |= B_ERROR;
1408 bp->b_error = EIO;
1409 fdfinish(fd, bp);
1410 }
1411 fdc->sc_errors++;
1412 }
1413
1414 int
1415 fdsize(dev)
1416 dev_t dev;
1417 {
1418
1419 /* Swapping to floppies would not make sense. */
1420 return -1;
1421 }
1422
1423 int
1424 fddump(dev, blkno, va, size)
1425 dev_t dev;
1426 daddr_t blkno;
1427 caddr_t va;
1428 size_t size;
1429 {
1430
1431 /* Not implemented. */
1432 return ENXIO;
1433 }
1434
1435 int
1436 fdioctl(dev, cmd, addr, flag, p)
1437 dev_t dev;
1438 u_long cmd;
1439 caddr_t addr;
1440 int flag;
1441 struct proc *p;
1442 {
1443 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1444 int unit = FDUNIT(dev);
1445 struct disklabel buffer;
1446 int error;
1447
1448 DPRINTF(("fdioctl:\n"));
1449 switch (cmd) {
1450 case DIOCGDINFO:
1451 #if 1
1452 *(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1453 return(0);
1454 #else
1455 bzero(&buffer, sizeof(buffer));
1456
1457 buffer.d_secpercyl = fd->sc_type->seccyl;
1458 buffer.d_type = DTYPE_FLOPPY;
1459 buffer.d_secsize = 128 << fd->sc_type->secsize;
1460
1461 if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
1462 return EINVAL;
1463
1464 *(struct disklabel *)addr = buffer;
1465 return 0;
1466 #endif
1467
1468 case DIOCGPART:
1469 ((struct partinfo *)addr)->disklab = fd->sc_dk.dk_label;
1470 ((struct partinfo *)addr)->part =
1471 &fd->sc_dk.dk_label->d_partitions[DISKPART(dev)];
1472 return(0);
1473
1474 case DIOCWLABEL:
1475 if ((flag & FWRITE) == 0)
1476 return EBADF;
1477 /* XXX do something */
1478 return 0;
1479
1480 case DIOCWDINFO:
1481 if ((flag & FWRITE) == 0)
1482 return EBADF;
1483
1484 error = setdisklabel(&buffer, (struct disklabel *)addr, 0, NULL);
1485 if (error)
1486 return error;
1487
1488 error = writedisklabel(dev, fdstrategy, &buffer, NULL);
1489 return error;
1490
1491 case DIOCLOCK:
1492 /*
1493 * Nothing to do here, really.
1494 */
1495 return 0; /* XXX */
1496
1497 case DIOCEJECT:
1498 fd_do_eject(unit);
1499 return 0;
1500
1501 default:
1502 return ENOTTY;
1503 }
1504
1505 #ifdef DIAGNOSTIC
1506 panic("fdioctl: impossible");
1507 #endif
1508 }
1509
1510 void
1511 fd_do_eject(unit)
1512 int unit;
1513 {
1514 infdc.drvstat = 0x20 | ( 1 << unit);
1515 DELAY(1); /* XXX */
1516 infdc.drvstat = 0x20;
1517 }
1518
1519 /*
1520 * Build disk label. For now we only create a label from what we know
1521 * from 'sc'.
1522 */
1523 static int
1524 fdgetdisklabel(sc, dev)
1525 struct fd_softc *sc;
1526 dev_t dev;
1527 {
1528 struct disklabel *lp;
1529 int part;
1530
1531 #ifdef FDDEBUG
1532 printf("fdgetdisklabel()\n");
1533 #endif
1534
1535 part = DISKPART(dev);
1536 lp = sc->sc_dk.dk_label;
1537 bzero(lp, sizeof(struct disklabel));
1538
1539 lp->d_secsize = 128 << sc->sc_type->secsize;
1540 lp->d_ntracks = sc->sc_type->heads;
1541 lp->d_nsectors = sc->sc_type->sectrac;
1542 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1543 lp->d_ncylinders = sc->sc_type->size / lp->d_secpercyl;
1544 lp->d_secperunit = sc->sc_type->size;
1545
1546 lp->d_type = DTYPE_FLOPPY;
1547 lp->d_rpm = 300; /* XXX */
1548 lp->d_interleave = 1; /* FIXME: is this OK? */
1549 lp->d_bbsize = 0;
1550 lp->d_sbsize = 0;
1551 lp->d_npartitions = part + 1;
1552 #define STEP_DELAY 6000 /* 6ms (6000us) delay after stepping */
1553 lp->d_trkseek = STEP_DELAY; /* XXX */
1554 lp->d_magic = DISKMAGIC;
1555 lp->d_magic2 = DISKMAGIC;
1556 lp->d_checksum = dkcksum(lp);
1557 lp->d_partitions[part].p_size = lp->d_secperunit;
1558 lp->d_partitions[part].p_fstype = FS_UNUSED;
1559 lp->d_partitions[part].p_fsize = 1024;
1560 lp->d_partitions[part].p_frag = 8;
1561
1562 return(0);
1563 }
1564
1565 /*
1566 * Mountroot hook: prompt the user to enter the root file system
1567 * floppy.
1568 */
1569 void
1570 fd_mountroot_hook(dev)
1571 struct device *dev;
1572 {
1573 int c;
1574
1575 fd_do_eject(dev->dv_unit);
1576 printf("Insert filesystem floppy and press return.");
1577 for (;;) {
1578 c = cngetc();
1579 if ((c == '\r') || (c == '\n')) {
1580 printf("\n");
1581 break;
1582 }
1583 }
1584 }
1585