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