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