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