2 * Copyright (c) 2004 Poul-Henning Kamp
3 * Copyright (c) 1990 The Regents of the University of California.
6 * This code is derived from software contributed to Berkeley by
9 * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
10 * aided by the Linux floppy driver modifications from David Bateman
11 * (dbateman@eng.uts.edu.au).
13 * Copyright (c) 1993, 1994 by
14 * jc@irbs.UUCP (John Capo)
15 * vak@zebub.msk.su (Serge Vakulenko)
16 * ache@astral.msk.su (Andrew A. Chernov)
18 * Copyright (c) 1993, 1994, 1995 by
19 * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
20 * dufault@hda.com (Peter Dufault)
22 * Copyright (c) 2001 Joerg Wunsch,
23 * joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 * 4. Neither the name of the University nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
58 #include <sys/param.h>
61 #include <sys/devicestat.h>
63 #include <sys/fcntl.h>
64 #include <sys/fdcio.h>
65 #include <sys/filio.h>
66 #include <sys/kernel.h>
67 #include <sys/kthread.h>
69 #include <sys/malloc.h>
70 #include <sys/module.h>
71 #include <sys/mutex.h>
75 #include <sys/sysctl.h>
76 #include <sys/systm.h>
78 #include <geom/geom.h>
80 #include <machine/bus.h>
81 #include <machine/clock.h>
82 #include <machine/stdarg.h>
84 #include <isa/isavar.h>
86 #include <pc98/pc98/pc98_machdep.h>
88 #include <isa/isareg.h>
91 #include <dev/fdc/fdcvar.h>
93 #include <dev/ic/nec765.h>
96 * Runtime configuration hints/flags
99 /* configuration flags for fd */
100 #define FD_TYPEMASK 0x0f /* drive type, matches enum
101 * fd_drivetype; on i386 machines, if
102 * given as 0, use RTC type for fd0
104 #define FD_NO_CHLINE 0x10 /* drive does not support changeline
105 * aka. unit attention */
106 #define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just
107 * assume it is there */
110 * Things that could conceiveably considered parameters or tweakables
114 * Maximal number of bytes in a cylinder.
115 * This is used for ISADMA bouncebuffer allocation and sets the max
116 * xfersize we support.
118 * 2.88M format has 2 x 36 x 512, allow for hacked up density.
120 #define MAX_BYTES_PER_CYL (2 * 40 * 512)
123 * Timeout value for the PIO loops to wait until the FDC main status
124 * register matches our expectations (request for master, direction
125 * bit). This is supposed to be a number of microseconds, although
126 * timing might actually not be very accurate.
128 * Timeouts of 100 msec are believed to be required for some broken
131 #define FDSTS_TIMEOUT 100000
134 * After this many errors, stop whining. Close will reset this count.
136 #define FDC_ERRMAX 100
139 * AutoDensity search lists for each drive type.
142 static struct fd_type fd_searchlist_360k[] = {
149 static struct fd_type fd_searchlist_12m[] = {
151 { FDF_5_1200 | FL_AUTO },
152 { FDF_5_720 | FL_AUTO },
153 { FDF_5_360 | FL_AUTO },
154 { FDF_5_640 | FL_AUTO },
155 { FDF_5_1230 | FL_AUTO },
157 { FDF_5_1200 | FL_AUTO },
158 { FDF_5_360 | FL_2STEP | FL_AUTO},
163 static struct fd_type fd_searchlist_720k[] = {
170 static struct fd_type fd_searchlist_144m[] = {
172 { FDF_3_1440 | FL_AUTO},
173 { FDF_3_1200 | FL_AUTO},
174 { FDF_3_720 | FL_AUTO},
175 { FDF_3_360 | FL_AUTO},
176 { FDF_3_640 | FL_AUTO},
177 { FDF_3_1230 | FL_AUTO},
179 { FDF_3_1440 | FL_AUTO},
180 { FDF_3_720 | FL_AUTO},
185 static struct fd_type fd_searchlist_288m[] = {
187 { FDF_3_1440 | FL_AUTO },
189 { FDF_3_2880 | FL_AUTO }, /* XXX: probably doesn't work */
191 { FDF_3_720 | FL_AUTO},
197 * Order must match enum fd_drivetype in <sys/fdcio.h>.
199 static struct fd_type *fd_native_types[] = {
201 fd_searchlist_360k, /* FDT_360K */
202 fd_searchlist_12m, /* FDT_12M */
203 fd_searchlist_720k, /* FDT_720K */
204 fd_searchlist_144m, /* FDT_144M */
205 fd_searchlist_288m, /* FDT_288M_1 (mapped to FDT_288M) */
206 fd_searchlist_288m, /* FDT_288M */
210 * Internals start here
215 #define FDSTS 0 /* NEC 765 Main Status Register (R) */
216 #define FDDATA 1 /* NEC 765 Data Register (R/W) */
217 #define FDCTL 2 /* FD Control Register */
218 #define FDC_RST 0x80 /* FDC RESET */
219 #define FDC_RDY 0x40 /* force READY */
220 #define FDC_DD 0x20 /* FDD Mode Exchange 0:1M 1:640K */
221 #define FDC_DMAE 0x10 /* enable floppy DMA */
222 #define FDC_MTON 0x08 /* MOTOR ON (when EMTON=1)*/
223 #define FDC_TMSK 0x04 /* TIMER MASK */
224 #define FDC_TTRG 0x01 /* TIMER TRIGER */
227 #define FDP_EMTON 0x04 /* enable MTON */
228 #define FDP_FDDEXC 0x02 /* FDD Mode Exchange 1:1M 0:640K */
229 #define FDP_PORTEXC 0x01 /* PORT Exchane 1:1M 0:640K */
234 #define FDOUT 2 /* Digital Output Register (W) */
235 #define FDO_FDSEL 0x03 /* floppy device select */
236 #define FDO_FRST 0x04 /* floppy controller reset */
237 #define FDO_FDMAEN 0x08 /* enable floppy DMA and Interrupt */
238 #define FDO_MOEN0 0x10 /* motor enable drive 0 */
239 #define FDO_MOEN1 0x20 /* motor enable drive 1 */
240 #define FDO_MOEN2 0x40 /* motor enable drive 2 */
241 #define FDO_MOEN3 0x80 /* motor enable drive 3 */
243 #define FDSTS 4 /* NEC 765 Main Status Register (R) */
244 #define FDDSR 4 /* Data Rate Select Register (W) */
245 #define FDDATA 5 /* NEC 765 Data Register (R/W) */
246 #define FDCTL 7 /* Control Register (W) */
250 * The YE-DATA PC Card floppies use PIO to read in the data rather
251 * than DMA due to the wild variability of DMA for the PC Card
252 * devices. DMA was deleted from the PC Card specification in version
253 * 7.2 of the standard, but that post-dates the YE-DATA devices by many
256 * In addition, if we cannot setup the DMA resources for the ISA
257 * attachment, we'll use this same offset for data transfer. However,
258 * that almost certainly won't work.
260 * For this mode, offset 0 and 1 must be used to setup the transfer
261 * for this floppy. This is OK for PC Card YE Data devices, but for
262 * ISA this is likely wrong. These registers are only available on
263 * those systems that map them to the floppy drive. Newer systems do
264 * not do this, and we should likely prohibit access to them (or
265 * disallow NODMA to be set).
267 #define FDBCDR 0 /* And 1 */
268 #define FD_YE_DATAPORT 6 /* Drive Data port */
271 #define FDI_DCHG 0x80 /* diskette has been changed */
272 /* requires drive and motor being selected */
273 /* is cleared by any step pulse to drive */
277 * We have three private BIO commands.
279 #define BIO_PROBE BIO_CMD0
280 #define BIO_RDID BIO_CMD1
281 #define BIO_FMT BIO_CMD2
284 * Per drive structure (softc).
287 u_char *fd_ioptr; /* IO pointer */
288 u_int fd_iosize; /* Size of IO chunks */
289 u_int fd_iocount; /* Outstanding requests */
290 struct fdc_data *fdc; /* pointer to controller structure */
291 int fdsu; /* this units number on this controller */
292 enum fd_drivetype type; /* drive type */
293 struct fd_type *ft; /* pointer to current type descriptor */
294 struct fd_type fts; /* type descriptors */
297 #define FD_WP (1<<0) /* Write protected */
298 #define FD_MOTOR (1<<1) /* motor should be on */
299 #define FD_MOTORWAIT (1<<2) /* motor should be on */
300 #define FD_EMPTY (1<<3) /* no media */
301 #define FD_NEWDISK (1<<4) /* media changed */
302 #define FD_ISADMA (1<<5) /* isa dma started */
303 int track; /* where we think the head is */
304 #define FD_NO_TRACK -2
305 int options; /* FDOPT_* */
306 struct callout toffhandle;
307 struct g_geom *fd_geom;
308 struct g_provider *fd_provider;
310 struct bio_queue_head fd_bq;
316 #define FD_NOT_VALID -2
318 static driver_intr_t fdc_intr;
319 static driver_filter_t fdc_intr_fast;
320 static void fdc_reset(struct fdc_data *);
321 static int fd_probe_disk(struct fd_data *, int *);
323 static SYSCTL_NODE(_debug, OID_AUTO, fdc, CTLFLAG_RW, 0, "fdc driver");
325 static int fifo_threshold = 8;
326 SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0,
327 "FIFO threshold setting");
329 static int debugflags = 0;
330 SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0,
333 static int retries = 10;
334 SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0,
335 "Number of retries to attempt");
338 static int spec1 = NE7_SPEC_1(4, 240);
340 static int spec1 = NE7_SPEC_1(6, 240);
342 SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0,
343 "Specification byte one (step-rate + head unload)");
346 static int spec2 = NE7_SPEC_2(2, 0);
348 static int spec2 = NE7_SPEC_2(16, 0);
350 SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0,
351 "Specification byte two (head load time + no-dma)");
354 SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0,
355 "Head settling time in sec/hz");
358 fdprinttype(struct fd_type *ft)
361 printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)",
362 ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks,
363 ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter,
364 ft->offset_side2, ft->flags);
368 fdsettype(struct fd_data *fd, struct fd_type *ft)
371 ft->size = ft->sectrac * ft->heads * ft->tracks;
372 fd->sectorsize = 128 << fd->ft->secsize;
376 * Bus space handling (access to low-level IO).
379 fdregwr(struct fdc_data *fdc, int reg, uint8_t v)
382 bus_space_write_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg], v);
385 static inline uint8_t
386 fdregrd(struct fdc_data *fdc, int reg)
389 return bus_space_read_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg]);
393 fdctl_wr(struct fdc_data *fdc, u_int8_t v)
396 fdregwr(fdc, FDCTL, v);
401 fdout_wr(struct fdc_data *fdc, u_int8_t v)
404 fdregwr(fdc, FDOUT, v);
409 fdsts_rd(struct fdc_data *fdc)
412 return fdregrd(fdc, FDSTS);
417 fddsr_wr(struct fdc_data *fdc, u_int8_t v)
420 fdregwr(fdc, FDDSR, v);
425 fddata_wr(struct fdc_data *fdc, u_int8_t v)
428 fdregwr(fdc, FDDATA, v);
432 fddata_rd(struct fdc_data *fdc)
435 return fdregrd(fdc, FDDATA);
440 fdin_rd(struct fdc_data *fdc)
443 return fdregrd(fdc, FDCTL);
448 * Magic pseudo-DMA initialization for YE FDC. Sets count and
452 fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count)
454 fdregwr(fdc, FDBCDR, (count - 1) & 0xff);
455 fdregwr(fdc, FDBCDR + 1,
456 (iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f));
460 fdc_err(struct fdc_data *fdc, const char *s)
464 if (fdc->fdc_errs < FDC_ERRMAX)
465 device_printf(fdc->fdc_dev, "%s", s);
466 else if (fdc->fdc_errs == FDC_ERRMAX)
467 device_printf(fdc->fdc_dev, "too many errors, not "
468 "logging any more\n");
475 * FDC IO functions, take care of the main status register, timeout
476 * in case the desired status bits are never set.
478 * These PIO loops initially start out with short delays between
479 * each iteration in the expectation that the required condition
480 * is usually met quickly, so it can be handled immediately.
483 fdc_in(struct fdc_data *fdc, int *ptr)
488 for (j = 0; j < FDSTS_TIMEOUT; j += step) {
489 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
490 if (i == (NE7_DIO|NE7_RQM)) {
497 return (fdc_err(fdc, "ready for output in input\n"));
501 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
505 fdc_out(struct fdc_data *fdc, int x)
510 for (j = 0; j < FDSTS_TIMEOUT; j += step) {
511 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
516 if (i == (NE7_DIO|NE7_RQM))
517 return (fdc_err(fdc, "ready for input in output\n"));
521 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
525 * fdc_cmd: Send a command to the chip.
526 * Takes a varargs with this structure:
528 * output bytes as int [...]
530 * input bytes as int* [...]
533 fdc_cmd(struct fdc_data *fdc, int n_out, ...)
541 for (n = 0; n < n_out; n++) {
545 if (fdc_out(fdc, i) < 0) {
547 snprintf(msg, sizeof(msg),
548 "cmd %x failed at out byte %d of %d\n",
550 fdc->flags |= FDC_NEEDS_RESET;
552 return fdc_err(fdc, msg);
555 n_in = va_arg(ap, int);
556 for (n = 0; n < n_in; n++) {
557 int *ptr = va_arg(ap, int *);
558 if (fdc_in(fdc, ptr) < 0) {
560 snprintf(msg, sizeof(msg),
561 "cmd %02x failed at in byte %d of %d\n",
563 fdc->flags |= FDC_NEEDS_RESET;
565 return fdc_err(fdc, msg);
573 static void fd_motor(struct fd_data *fd, int turnon);
575 static int pc98_trans = 0; /* 0 : HD , 1 : DD , 2 : 1.44 */
576 static int pc98_trans_prev = -1;
579 set_density(struct fdc_data *fdc)
581 /* always motor on */
582 fdregwr(fdc, FDP, (pc98_trans != 1 ? FDP_FDDEXC : 0) | FDP_PORTEXC);
584 fdctl_wr(fdc, FDC_RST | FDC_DMAE);
585 /* in the case of note W, always inhibit 100ms timer */
589 pc98_fd_check_ready(struct fd_data *fd)
591 struct fdc_data *fdc = fd->fdc;
592 int retry = 0, status;
593 int fdu = device_get_unit(fd->dev);
595 while (retry++ < 30000) {
597 fdc_out(fdc, NE7CMD_SENSED); /* Sense Drive Status */
599 fdc_out(fdc, fdu); /* Drive number */
601 if ((fdc_in(fdc, &status) == 0) && (status & NE7_ST3_RD)) {
602 fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
611 pc98_fd_check_type(struct fd_data *fd, int unit)
613 struct fdc_data *fdc;
615 if (fd->type != FDT_NONE || unit < 0 || unit > 3)
620 /* Look up what the BIOS thinks we have. */
621 if (!((PC98_SYSTEM_PARAMETER(0x55c) >> unit) & 0x01)) {
625 if ((PC98_SYSTEM_PARAMETER(0x5ae) >> unit) & 0x01) {
626 /* Check 3mode I/F */
628 fdregwr(fdc, FDEM, (unit << 5) | 0x10);
629 if (!(fdregrd(fdc, FDEM) & 0x01)) {
633 device_printf(fd->dev,
634 "Warning: can't control 3mode I/F, fallback to 2mode.\n");
642 fdc_reset(struct fdc_data *fdc)
648 if (pc98_machine_type & M_EPSON_PC98)
649 fdctl_wr(fdc, FDC_RST | FDC_RDY | FDC_DD | FDC_MTON);
651 fdctl_wr(fdc, FDC_RST | FDC_RDY | FDC_DMAE | FDC_MTON);
653 fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
656 if (fdc->fdct == FDC_ENHANCED) {
657 /* Try a software reset, default precomp, and 500 kb/s */
658 fddsr_wr(fdc, I8207X_DSR_SR);
660 /* Try a hardware reset, keep motor on */
661 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
663 /* enable FDC, but defer interrupts a moment */
664 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
667 fdout_wr(fdc, fdc->fdout);
670 /* XXX after a reset, silently believe the FDC will accept commands */
671 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
672 device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
674 if (fdc->fdct == FDC_ENHANCED) {
678 /* 0x40 | */ /* Enable Implied Seek -
680 0x10 | /* Polling disabled */
681 (fifo_threshold - 1), /* Fifo threshold */
682 0x00, /* Precomp track */
684 device_printf(fdc->fdc_dev,
685 " CONFIGURE failed in reset\n");
686 if (debugflags & 1) {
689 10, &r[0], &r[1], &r[2], &r[3], &r[4],
690 &r[5], &r[6], &r[7], &r[8], &r[9]))
691 device_printf(fdc->fdc_dev,
692 " DUMPREG failed in reset\n");
693 for (i = 0; i < 10; i++)
694 printf(" %02x", r[i]);
701 fdc_sense_drive(struct fdc_data *fdc, int *st3p)
705 if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
706 return (fdc_err(fdc, "Sense Drive Status failed\n"));
713 fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
717 ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
719 (void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
726 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
728 * There doesn't seem to have been an interrupt.
730 return (FD_NOT_VALID);
733 if (fdc_in(fdc, &cyl) < 0)
734 return fdc_err(fdc, "can't get cyl num\n");
743 fdc_read_status(struct fdc_data *fdc)
747 for (i = ret = 0; i < 7; i++) {
748 ret = fdc_in(fdc, &status);
749 fdc->status[i] = status;
755 fdc->flags |= FDC_STAT_VALID;
757 fdc->flags &= ~FDC_STAT_VALID;
767 fd_select(struct fd_data *fd)
769 struct fdc_data *fdc;
771 /* XXX: lock controller */
773 fdc->fdout &= ~FDO_FDSEL;
774 fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
775 fdout_wr(fdc, fdc->fdout);
786 mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
787 fd->flags &= ~FD_MOTORWAIT;
788 fd->flags |= FD_MOTOR;
791 bp = bioq_takefirst(&fd->fd_bq);
794 bioq_disksort(&fd->fdc->head, bp);
798 wakeup(&fd->fdc->head);
803 fd_motor(struct fd_data *fd, int turnon)
805 struct fdc_data *fdc;
809 mtx_assert(&fdc->fdc_mtx, MA_OWNED);
812 fdregwr(fdc, FDP, (pc98_trans != 1 ? FDP_FDDEXC : 0) | FDP_PORTEXC);
814 fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
817 fd->flags |= FD_MOTORWAIT;
818 fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
819 callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
821 callout_stop(&fd->toffhandle);
822 fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
823 fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
825 fdout_wr(fdc, fdc->fdout);
830 fd_turnoff(void *xfd)
832 struct fd_data *fd = xfd;
834 mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
839 * fdc_intr - wake up the worker thread.
850 fdc_intr_fast(void *arg)
854 return(FILTER_HANDLED);
858 * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
861 fdc_pio(struct fdc_data *fdc)
868 cptr = fdc->fd->fd_ioptr;
869 count = fdc->fd->fd_iosize;
871 if (bp->bio_cmd == BIO_READ) {
872 fdbcdr_wr(fdc, 0, count);
873 bus_space_read_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
874 fdc->ioff[FD_YE_DATAPORT], cptr, count);
876 bus_space_write_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
877 fdc->ioff[FD_YE_DATAPORT], cptr, count);
878 fdbcdr_wr(fdc, 0, count); /* needed? */
883 fdc_biodone(struct fdc_data *fdc, int error)
891 mtx_lock(&fdc->fdc_mtx);
892 if (--fd->fd_iocount == 0)
893 callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
896 mtx_unlock(&fdc->fdc_mtx);
897 if (bp->bio_to != NULL) {
898 if ((debugflags & 2) && fd->fdc->retry > 0)
899 printf("retries: %d\n", fd->fdc->retry);
900 g_io_deliver(bp, error);
903 bp->bio_error = error;
904 bp->bio_flags |= BIO_DONE;
909 static int retry_line;
912 fdc_worker(struct fdc_data *fdc)
917 int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
920 static int need_recal;
921 struct fdc_readid *idp;
922 struct fd_formb *finfo;
926 /* Have we exhausted our retries ? */
930 (fdc->retry >= retries || (fd->options & FDOPT_NORETRY))) {
931 if ((debugflags & 4))
932 printf("Too many retries (EIO)\n");
933 if (fdc->flags & FDC_NEEDS_RESET) {
934 mtx_lock(&fdc->fdc_mtx);
935 fd->flags |= FD_EMPTY;
936 mtx_unlock(&fdc->fdc_mtx);
938 return (fdc_biodone(fdc, EIO));
941 /* Disable ISADMA if we bailed while it was active */
942 if (fd != NULL && (fd->flags & FD_ISADMA)) {
944 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
945 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
946 mtx_lock(&fdc->fdc_mtx);
947 fd->flags &= ~FD_ISADMA;
948 mtx_unlock(&fdc->fdc_mtx);
951 /* Unwedge the controller ? */
952 if (fdc->flags & FDC_NEEDS_RESET) {
953 fdc->flags &= ~FDC_NEEDS_RESET;
955 tsleep(fdc, PRIBIO, "fdcrst", hz);
956 /* Discard results */
957 for (i = 0; i < 4; i++)
958 fdc_sense_int(fdc, &st0, &cyl);
959 /* All drives must recal */
963 /* Pick up a request, if need be wait for it */
964 if (fdc->bp == NULL) {
965 mtx_lock(&fdc->fdc_mtx);
967 fdc->bp = bioq_takefirst(&fdc->head);
969 msleep(&fdc->head, &fdc->fdc_mtx,
971 } while (fdc->bp == NULL &&
972 (fdc->flags & FDC_KTHREAD_EXIT) == 0);
973 mtx_unlock(&fdc->fdc_mtx);
977 * Nothing to do, worker thread has been
983 fd = fdc->fd = bp->bio_driver1;
985 fd->fd_ioptr = bp->bio_data;
986 if (bp->bio_cmd & BIO_FMT) {
987 i = offsetof(struct fd_formb, fd_formb_cylno(0));
989 fd->fd_iosize = bp->bio_length - i;
993 /* Select drive, setup params */
995 pc98_trans = fd->ft->trans;
996 if (pc98_trans_prev != pc98_trans) {
1000 for (i = 0; i < 10; i++) {
1004 pc98_trans_prev = pc98_trans;
1006 if (pc98_trans != fd->pc98_trans) {
1007 if (fd->type == FDT_144M) {
1009 (device_get_unit(fd->dev) << 5) | 0x10 |
1014 fd->pc98_trans = pc98_trans;
1018 if (fdc->fdct == FDC_ENHANCED)
1019 fddsr_wr(fdc, fd->ft->trans);
1021 fdctl_wr(fdc, fd->ft->trans);
1024 if (bp->bio_cmd & BIO_PROBE) {
1025 if ((!(device_get_flags(fd->dev) & FD_NO_CHLINE) &&
1027 !(fdin_rd(fdc) & FDI_DCHG) &&
1029 !(fd->flags & FD_EMPTY)) ||
1030 fd_probe_disk(fd, &need_recal) == 0)
1031 return (fdc_biodone(fdc, 0));
1036 * If we are dead just flush the requests
1038 if (fd->flags & FD_EMPTY)
1039 return (fdc_biodone(fdc, ENXIO));
1042 /* Check if we lost our media */
1043 if (fdin_rd(fdc) & FDI_DCHG) {
1044 if (debugflags & 0x40)
1045 printf("Lost disk\n");
1046 mtx_lock(&fdc->fdc_mtx);
1047 fd->flags |= FD_EMPTY;
1048 fd->flags |= FD_NEWDISK;
1049 mtx_unlock(&fdc->fdc_mtx);
1051 g_orphan_provider(fd->fd_provider, ENXIO);
1052 fd->fd_provider->flags |= G_PF_WITHER;
1054 g_new_providerf(fd->fd_geom, "%s", fd->fd_geom->name);
1055 g_error_provider(fd->fd_provider, 0);
1056 g_topology_unlock();
1057 return (fdc_biodone(fdc, ENXIO));
1061 /* Check if the floppy is write-protected */
1062 if (bp->bio_cmd & (BIO_FMT | BIO_WRITE)) {
1063 retry_line = __LINE__;
1064 if(fdc_sense_drive(fdc, &st3) != 0)
1066 if(st3 & NE7_ST3_WP)
1067 return (fdc_biodone(fdc, EROFS));
1070 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
1071 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
1072 i = fd->ft->sectrac * fd->ft->heads;
1073 cylinder = bp->bio_pblkno / i;
1074 descyl = cylinder * steptrac;
1075 sec = bp->bio_pblkno % i;
1077 head = sec / fd->ft->sectrac;
1078 sec = sec % fd->ft->sectrac + 1;
1080 /* If everything is going swimmingly, use multisector xfer */
1081 if (fdc->retry == 0 && bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
1082 fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
1083 nsect = fd->fd_iosize / fd->sectorsize;
1084 } else if (bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
1085 fd->fd_iosize = fd->sectorsize;
1089 /* Do RECAL if we need to or are going to track zero anyway */
1090 if ((need_recal & (1 << fd->fdsu)) ||
1091 (cylinder == 0 && fd->track != 0) ||
1094 pc98_fd_check_ready(fd);
1096 retry_line = __LINE__;
1097 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
1099 tsleep(fdc, PRIBIO, "fdrecal", hz);
1100 retry_line = __LINE__;
1101 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1102 return (1); /* XXX */
1103 retry_line = __LINE__;
1104 if ((st0 & 0xc0) || cyl != 0)
1106 need_recal &= ~(1 << fd->fdsu);
1108 /* let the heads settle */
1110 tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
1114 * SEEK to where we want to be
1116 if (cylinder != fd->track) {
1118 pc98_fd_check_ready(fd);
1120 retry_line = __LINE__;
1121 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
1123 tsleep(fdc, PRIBIO, "fdseek", hz);
1124 retry_line = __LINE__;
1125 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1126 return (1); /* XXX */
1127 retry_line = __LINE__;
1128 if ((st0 & 0xc0) || cyl != descyl) {
1129 need_recal |= (1 << fd->fdsu);
1132 /* let the heads settle */
1134 tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
1136 fd->track = cylinder;
1139 printf("op %x bn %ju siz %u ptr %p retry %d\n",
1140 bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
1141 fd->fd_ioptr, fdc->retry);
1143 /* Setup ISADMA if we need it and have it */
1144 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
1145 && !(fdc->flags & FDC_NODMA)) {
1147 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
1148 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
1149 mtx_lock(&fdc->fdc_mtx);
1150 fd->flags |= FD_ISADMA;
1151 mtx_unlock(&fdc->fdc_mtx);
1154 /* Do PIO if we have to */
1155 if (fdc->flags & FDC_NODMA) {
1156 if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
1157 fdbcdr_wr(fdc, 1, fd->fd_iosize);
1158 if (bp->bio_cmd & (BIO_WRITE|BIO_FMT))
1162 switch(bp->bio_cmd) {
1165 finfo = (struct fd_formb *)bp->bio_data;
1166 retry_line = __LINE__;
1168 NE7CMD_FORMAT | mfm,
1169 head << 2 | fd->fdsu,
1170 finfo->fd_formb_secshift,
1171 finfo->fd_formb_nsecs,
1172 finfo->fd_formb_gaplen,
1173 finfo->fd_formb_fillbyte, 0))
1177 retry_line = __LINE__;
1179 NE7CMD_READID | mfm,
1180 head << 2 | fd->fdsu, 0))
1184 retry_line = __LINE__;
1186 NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT,
1187 head << 2 | fd->fdsu, /* head & unit */
1188 fd->track, /* track */
1190 sec, /* sector + 1 */
1191 fd->ft->secsize, /* sector size */
1192 fd->ft->sectrac, /* sectors/track */
1193 fd->ft->gap, /* gap size */
1194 fd->ft->datalen, /* data length */
1199 retry_line = __LINE__;
1201 NE7CMD_WRITE | mfm | NE7CMD_MT,
1202 head << 2 | fd->fdsu, /* head & unit */
1203 fd->track, /* track */
1205 sec, /* sector + 1 */
1206 fd->ft->secsize, /* sector size */
1207 fd->ft->sectrac, /* sectors/track */
1208 fd->ft->gap, /* gap size */
1209 fd->ft->datalen, /* data length */
1214 KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd));
1217 /* Wait for interrupt */
1218 i = tsleep(fdc, PRIBIO, "fddata", hz);
1220 /* PIO if the read looks good */
1221 if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd & BIO_READ))
1225 if (fd->flags & FD_ISADMA) {
1227 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
1228 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
1229 mtx_lock(&fdc->fdc_mtx);
1230 fd->flags &= ~FD_ISADMA;
1231 mtx_unlock(&fdc->fdc_mtx);
1238 * Due to IBM's brain-dead design, the FDC has a faked ready
1239 * signal, hardwired to ready == true. Thus, any command
1240 * issued if there's no diskette in the drive will _never_
1241 * complete, and must be aborted by resetting the FDC.
1242 * Many thanks, Big Blue!
1244 retry_line = __LINE__;
1245 fdc->flags |= FDC_NEEDS_RESET;
1249 retry_line = __LINE__;
1250 if (fdc_read_status(fdc))
1253 if (debugflags & 0x10)
1254 printf(" -> %x %x %x %x\n",
1255 fdc->status[0], fdc->status[1],
1256 fdc->status[2], fdc->status[3]);
1258 st0 = fdc->status[0] & NE7_ST0_IC;
1260 retry_line = __LINE__;
1261 if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) {
1263 * DMA overrun. Someone hogged the bus and
1264 * didn't release it in time for the next
1269 retry_line = __LINE__;
1270 if(st0 == NE7_ST0_IC_IV) {
1271 fdc->flags |= FDC_NEEDS_RESET;
1274 retry_line = __LINE__;
1275 if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) {
1276 need_recal |= (1 << fd->fdsu);
1279 if (debugflags & 0x20) {
1280 printf("status %02x %02x %02x %02x %02x %02x\n",
1281 fdc->status[0], fdc->status[1], fdc->status[2],
1282 fdc->status[3], fdc->status[4], fdc->status[5]);
1284 retry_line = __LINE__;
1285 if (fd->options & FDOPT_NOERROR)
1291 switch(bp->bio_cmd) {
1293 /* copy out ID field contents */
1294 idp = (struct fdc_readid *)bp->bio_data;
1295 idp->cyl = fdc->status[3];
1296 idp->head = fdc->status[4];
1297 idp->sec = fdc->status[5];
1298 idp->secshift = fdc->status[6];
1299 if (debugflags & 0x40)
1300 printf("c %d h %d s %d z %d\n",
1301 idp->cyl, idp->head, idp->sec, idp->secshift);
1305 bp->bio_pblkno += nsect;
1306 bp->bio_resid -= fd->fd_iosize;
1307 bp->bio_completed += fd->fd_iosize;
1308 fd->fd_ioptr += fd->fd_iosize;
1309 if (override_error) {
1310 if ((debugflags & 4))
1311 printf("FDOPT_NOERROR: returning bad data\n");
1313 /* Since we managed to get something done,
1314 * reset the retry */
1316 if (bp->bio_resid > 0)
1323 return (fdc_biodone(fdc, 0));
1327 fdc_thread(void *arg)
1329 struct fdc_data *fdc;
1334 mtx_lock(&fdc->fdc_mtx);
1335 fdc->flags |= FDC_KTHREAD_ALIVE;
1336 while ((fdc->flags & FDC_KTHREAD_EXIT) == 0) {
1337 mtx_unlock(&fdc->fdc_mtx);
1338 i = fdc_worker(fdc);
1339 if (i && debugflags & 0x20) {
1340 if (fdc->bp != NULL) {
1341 g_print_bio(fdc->bp);
1344 printf("Retry line %d\n", retry_line);
1347 mtx_lock(&fdc->fdc_mtx);
1349 fdc->flags &= ~(FDC_KTHREAD_EXIT | FDC_KTHREAD_ALIVE);
1350 mtx_unlock(&fdc->fdc_mtx);
1356 * Enqueue a request.
1359 fd_enqueue(struct fd_data *fd, struct bio *bp)
1361 struct fdc_data *fdc;
1366 mtx_lock(&fdc->fdc_mtx);
1367 /* If we go from idle, cancel motor turnoff */
1368 if (fd->fd_iocount++ == 0)
1369 callout_stop(&fd->toffhandle);
1370 if (fd->flags & FD_MOTOR) {
1371 /* The motor is on, send it directly to the controller */
1372 bioq_disksort(&fdc->head, bp);
1375 /* Queue it on the drive until the motor has started */
1376 bioq_insert_tail(&fd->fd_bq, bp);
1377 if (!(fd->flags & FD_MOTORWAIT))
1380 mtx_unlock(&fdc->fdc_mtx);
1384 * Try to find out if we have a disk in the drive.
1387 fd_probe_disk(struct fd_data *fd, int *recal)
1389 struct fdc_data *fdc;
1394 oopts = fd->options;
1395 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1399 * First recal, then seek to cyl#1, this clears the old condition on
1400 * the disk change line so we can examine it for current status.
1402 if (debugflags & 0x40)
1403 printf("New disk in probe\n");
1404 mtx_lock(&fdc->fdc_mtx);
1405 fd->flags |= FD_NEWDISK;
1406 mtx_unlock(&fdc->fdc_mtx);
1407 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
1409 tsleep(fdc, PRIBIO, "fdrecal", hz);
1410 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1411 goto done; /* XXX */
1412 if ((st0 & 0xc0) || cyl != 0)
1415 /* Seek to track 1 */
1416 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0))
1418 tsleep(fdc, PRIBIO, "fdseek", hz);
1419 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1420 goto done; /* XXX */
1421 *recal |= (1 << fd->fdsu);
1423 if (fdin_rd(fdc) & FDI_DCHG) {
1424 if (debugflags & 0x40)
1425 printf("Empty in probe\n");
1426 mtx_lock(&fdc->fdc_mtx);
1427 fd->flags |= FD_EMPTY;
1428 mtx_unlock(&fdc->fdc_mtx);
1433 if (fdc_sense_drive(fdc, &st3) != 0)
1435 if (debugflags & 0x40)
1436 printf("Got disk in probe\n");
1437 mtx_lock(&fdc->fdc_mtx);
1438 fd->flags &= ~FD_EMPTY;
1439 if (st3 & NE7_ST3_WP)
1442 fd->flags &= ~FD_WP;
1443 mtx_unlock(&fdc->fdc_mtx);
1448 fd->options = oopts;
1453 fdmisccmd(struct fd_data *fd, u_int cmd, void *data)
1456 struct fd_formb *finfo;
1457 struct fdc_readid *idfield;
1460 bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
1463 * Set up a bio request for fdstrategy(). bio_offset is faked
1464 * so that fdstrategy() will seek to the requested
1465 * cylinder, and use the desired head.
1468 if (cmd == BIO_FMT) {
1469 finfo = (struct fd_formb *)data;
1471 (finfo->cyl * fd->ft->heads + finfo->head) *
1473 bp->bio_length = sizeof *finfo;
1474 } else if (cmd == BIO_RDID) {
1475 idfield = (struct fdc_readid *)data;
1477 (idfield->cyl * fd->ft->heads + idfield->head) *
1479 bp->bio_length = sizeof(struct fdc_readid);
1480 } else if (cmd == BIO_PROBE) {
1483 panic("wrong cmd in fdmisccmd()");
1484 bp->bio_offset = bp->bio_pblkno * fd->sectorsize;
1485 bp->bio_data = data;
1486 bp->bio_driver1 = fd;
1492 tsleep(bp, PRIBIO, "fdwait", hz);
1493 } while (!(bp->bio_flags & BIO_DONE));
1494 error = bp->bio_error;
1501 * Try figuring out the density of the media present in our device.
1504 fdautoselect(struct fd_data *fd)
1506 struct fd_type *fdtp;
1507 struct fdc_readid id;
1510 if (!(fd->ft->flags & FL_AUTO))
1513 fdtp = fd_native_types[fd->type];
1514 fdsettype(fd, fdtp);
1515 if (!(fd->ft->flags & FL_AUTO))
1519 * Try reading sector ID fields, first at cylinder 0, head 0,
1520 * then at cylinder 2, head N. We don't probe cylinder 1,
1521 * since for 5.25in DD media in a HD drive, there are no data
1522 * to read (2 step pulses per media cylinder required). For
1523 * two-sided media, the second probe always goes to head 1, so
1524 * we can tell them apart from single-sided media. As a
1525 * side-effect this means that single-sided media should be
1526 * mentioned in the search list after two-sided media of an
1527 * otherwise identical density. Media with a different number
1528 * of sectors per track but otherwise identical parameters
1529 * cannot be distinguished at all.
1531 * If we successfully read an ID field on both cylinders where
1532 * the recorded values match our expectation, we are done.
1533 * Otherwise, we try the next density entry from the table.
1535 * Stepping to cylinder 2 has the side-effect of clearing the
1536 * unit attention bit.
1538 oopts = fd->options;
1539 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1540 for (; fdtp->heads; fdtp++) {
1541 fdsettype(fd, fdtp);
1543 id.cyl = id.head = 0;
1544 rv = fdmisccmd(fd, BIO_RDID, &id);
1547 if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize)
1550 id.head = fd->ft->heads - 1;
1551 rv = fdmisccmd(fd, BIO_RDID, &id);
1552 if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
1553 id.secshift != fdtp->secsize)
1559 fd->options = oopts;
1560 if (fdtp->heads == 0) {
1561 if (debugflags & 0x40)
1562 device_printf(fd->dev, "autoselection failed\n");
1563 fdsettype(fd, fd_native_types[fd->type]);
1566 if (debugflags & 0x40) {
1567 device_printf(fd->dev,
1568 "autoselected %d KB medium\n",
1570 (128 << (fd->ft->secsize)) * fd->ft->size / 1024);
1574 fdprinttype(fd->ft);
1581 * GEOM class implementation
1584 static g_access_t fd_access;
1585 static g_start_t fd_start;
1586 static g_ioctl_t fd_ioctl;
1588 struct g_class g_fd_class = {
1590 .version = G_VERSION,
1592 .access = fd_access,
1597 fd_access(struct g_provider *pp, int r, int w, int e)
1600 struct fdc_data *fdc;
1604 fd = pp->geom->softc;
1608 * If our provider is withering, we can only get negative requests
1609 * and we don't want to even see them
1611 if (pp->flags & G_PF_WITHER)
1618 if (ar == 0 && aw == 0 && ae == 0) {
1619 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
1620 device_unbusy(fd->dev);
1625 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
1627 if (pc98_fd_check_ready(fd) == -1)
1630 if (fdmisccmd(fd, BIO_PROBE, NULL))
1632 if (fd->flags & FD_EMPTY)
1634 if (fd->flags & FD_NEWDISK) {
1635 if (fdautoselect(fd) != 0 &&
1636 (device_get_flags(fd->dev) & FD_NO_CHLINE)) {
1637 mtx_lock(&fdc->fdc_mtx);
1638 fd->flags |= FD_EMPTY;
1639 mtx_unlock(&fdc->fdc_mtx);
1642 mtx_lock(&fdc->fdc_mtx);
1643 fd->flags &= ~FD_NEWDISK;
1644 mtx_unlock(&fdc->fdc_mtx);
1646 device_busy(fd->dev);
1650 if (w > 0 && (fd->flags & FD_WP)) {
1652 device_unbusy(fd->dev);
1656 pp->sectorsize = fd->sectorsize;
1657 pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
1658 pp->mediasize = pp->stripesize * fd->ft->tracks;
1663 fd_start(struct bio *bp)
1665 struct fdc_data * fdc;
1666 struct fd_data * fd;
1668 fd = bp->bio_to->geom->softc;
1670 bp->bio_driver1 = fd;
1671 if (bp->bio_cmd & BIO_GETATTR) {
1672 if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
1674 if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
1676 g_io_deliver(bp, ENOIOCTL);
1679 if (!(bp->bio_cmd & (BIO_READ|BIO_WRITE))) {
1680 g_io_deliver(bp, EOPNOTSUPP);
1683 bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
1684 bp->bio_resid = bp->bio_length;
1690 fd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
1693 struct fdc_status *fsp;
1694 struct fdc_readid *rid;
1697 fd = pp->geom->softc;
1700 pc98_fd_check_ready(fd);
1704 case FD_GTYPE: /* get drive type */
1705 *(struct fd_type *)data = *fd->ft;
1708 case FD_STYPE: /* set drive type */
1710 * Allow setting drive type temporarily iff
1711 * currently unset. Used for fdformat so any
1712 * user can set it, and then start formatting.
1714 fd->fts = *(struct fd_type *)data;
1715 if (fd->fts.sectrac) {
1716 /* XXX: check for rubbish */
1717 fdsettype(fd, &fd->fts);
1719 fdsettype(fd, fd_native_types[fd->type]);
1721 if (debugflags & 0x40)
1722 fdprinttype(fd->ft);
1725 case FD_GOPTS: /* get drive options */
1726 *(int *)data = fd->options;
1729 case FD_SOPTS: /* set drive options */
1730 fd->options = *(int *)data;
1734 error = priv_check(td, PRIV_DRIVER);
1737 fd->fdc->fdc_errs = 0;
1741 fsp = (struct fdc_status *)data;
1742 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
1744 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
1748 *(enum fd_drivetype *)data = fd->type;
1752 if (!(fflag & FWRITE))
1754 if (((struct fd_formb *)data)->format_version !=
1756 return (EINVAL); /* wrong version of formatting prog */
1757 error = fdmisccmd(fd, BIO_FMT, data);
1758 mtx_lock(&fd->fdc->fdc_mtx);
1759 fd->flags |= FD_NEWDISK;
1760 mtx_unlock(&fd->fdc->fdc_mtx);
1764 rid = (struct fdc_readid *)data;
1765 if (rid->cyl > 85 || rid->head > 1)
1767 error = fdmisccmd(fd, BIO_RDID, data);
1772 /* For backwards compat with old fd*(8) tools */
1777 if (debugflags & 0x80)
1778 printf("Unknown ioctl %lx\n", cmd);
1788 * Configuration/initialization stuff, per controller.
1791 devclass_t fdc_devclass;
1792 static devclass_t fd_devclass;
1800 fdc_release_resources(struct fdc_data *fdc)
1803 struct resource *last;
1808 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1809 fdc->fdc_intr = NULL;
1810 if (fdc->res_irq != NULL)
1811 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
1813 fdc->res_irq = NULL;
1815 for (i = 0; i < FDC_MAXREG; i++) {
1816 if (fdc->resio[i] != NULL && fdc->resio[i] != last) {
1817 bus_release_resource(dev, SYS_RES_IOPORT,
1818 fdc->ridio[i], fdc->resio[i]);
1819 last = fdc->resio[i];
1820 fdc->resio[i] = NULL;
1823 if (fdc->res_drq != NULL)
1824 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
1826 fdc->res_drq = NULL;
1830 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1832 struct fdc_ivars *ivars = device_get_ivars(child);
1835 case FDC_IVAR_FDUNIT:
1836 *result = ivars->fdunit;
1838 case FDC_IVAR_FDTYPE:
1839 *result = ivars->fdtype;
1848 fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1850 struct fdc_ivars *ivars = device_get_ivars(child);
1853 case FDC_IVAR_FDUNIT:
1854 ivars->fdunit = value;
1856 case FDC_IVAR_FDTYPE:
1857 ivars->fdtype = value;
1866 fdc_initial_reset(device_t dev, struct fdc_data *fdc)
1868 int ic_type, part_id;
1871 /* See if it can handle a command. */
1872 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(4, 240),
1873 NE7_SPEC_2(2, 0), 0))
1877 * A status value of 0xff is very unlikely, but not theoretically
1878 * impossible, but it is far more likely to indicate an empty bus.
1880 if (fdsts_rd(fdc) == 0xff)
1884 * Assert a reset to the floppy controller and check that the status
1885 * register goes to zero.
1889 if (fdsts_rd(fdc) != 0)
1893 * Clear the reset and see it come ready.
1895 fdout_wr(fdc, FDO_FRST);
1897 if (fdsts_rd(fdc) != 0x80)
1900 /* Then, see if it can handle a command. */
1901 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(6, 240),
1902 NE7_SPEC_2(31, 0), 0))
1907 * Try to identify the chip.
1909 * The i8272 datasheet documents that unknown commands
1910 * will return ST0 as 0x80. The i8272 is supposedly identical
1912 * The i82077SL datasheet says 0x90 for the VERSION command,
1913 * and several "superio" chips emulate this.
1915 if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
1917 if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
1921 "ic_type %02x part_id %02x\n", ic_type, part_id);
1922 switch (ic_type & 0xff) {
1924 device_set_desc(dev, "NEC 765 or clone");
1925 fdc->fdct = FDC_NE765;
1929 device_set_desc(dev,
1930 "Enhanced floppy controller");
1931 fdc->fdct = FDC_ENHANCED;
1934 device_set_desc(dev, "Generic floppy controller");
1935 fdc->fdct = FDC_UNKNOWN;
1942 fdc_detach(device_t dev)
1944 struct fdc_data *fdc;
1947 fdc = device_get_softc(dev);
1949 /* have our children detached first */
1950 if ((error = bus_generic_detach(dev)))
1954 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1955 fdc->fdc_intr = NULL;
1957 /* kill worker thread */
1958 mtx_lock(&fdc->fdc_mtx);
1959 fdc->flags |= FDC_KTHREAD_EXIT;
1961 while ((fdc->flags & FDC_KTHREAD_ALIVE) != 0)
1962 msleep(fdc->fdc_thread, &fdc->fdc_mtx, PRIBIO, "fdcdet", 0);
1963 mtx_unlock(&fdc->fdc_mtx);
1965 /* reset controller, turn motor off */
1972 if (!(fdc->flags & FDC_NODMA))
1973 isa_dma_release(fdc->dmachan);
1974 fdc_release_resources(fdc);
1975 mtx_destroy(&fdc->fdc_mtx);
1980 * Add a child device to the fdc controller. It will then be probed etc.
1983 fdc_add_child(device_t dev, const char *name, int unit)
1985 struct fdc_ivars *ivar;
1988 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
1991 child = device_add_child(dev, name, unit);
1992 if (child == NULL) {
1993 free(ivar, M_DEVBUF);
1996 device_set_ivars(child, ivar);
1997 ivar->fdunit = unit;
1998 ivar->fdtype = FDT_NONE;
1999 if (resource_disabled(name, unit))
2000 device_disable(child);
2005 fdc_attach(device_t dev)
2007 struct fdc_data *fdc;
2010 fdc = device_get_softc(dev);
2012 error = fdc_initial_reset(dev, fdc);
2014 device_printf(dev, "does not respond\n");
2017 error = bus_setup_intr(dev, fdc->res_irq,
2018 INTR_TYPE_BIO | INTR_ENTROPY |
2019 ((fdc->flags & FDC_NOFAST) ? INTR_MPSAFE : 0),
2020 ((fdc->flags & FDC_NOFAST) ? NULL : fdc_intr_fast),
2021 ((fdc->flags & FDC_NOFAST) ? fdc_intr : NULL),
2022 fdc, &fdc->fdc_intr);
2024 device_printf(dev, "cannot setup interrupt\n");
2027 if (!(fdc->flags & FDC_NODMA)) {
2028 error = isa_dma_acquire(fdc->dmachan);
2030 error = isa_dma_init(fdc->dmachan,
2031 MAX_BYTES_PER_CYL, M_WAITOK);
2033 isa_dma_release(fdc->dmachan);
2038 fdc->fdcu = device_get_unit(dev);
2039 fdc->flags |= FDC_NEEDS_RESET;
2041 mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
2043 /* reset controller, turn motor off, clear fdout mirror reg */
2047 fdout_wr(fdc, fdc->fdout = 0);
2049 bioq_init(&fdc->head);
2051 kproc_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
2052 "fdc%d", device_get_unit(dev));
2060 fdc_hints_probe(device_t dev)
2062 const char *name, *dname;
2063 int i, error, dunit;
2066 * Probe and attach any children. We should probably detect
2067 * devices from the BIOS unless overridden.
2069 name = device_get_nameunit(dev);
2071 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
2072 resource_int_value(dname, dunit, "drive", &dunit);
2073 fdc_add_child(dev, dname, dunit);
2076 if ((error = bus_generic_attach(dev)) != 0)
2082 fdc_print_child(device_t me, device_t child)
2084 int retval = 0, flags;
2086 retval += bus_print_child_header(me, child);
2087 retval += printf(" on %s drive %d", device_get_nameunit(me),
2088 fdc_get_fdunit(child));
2089 if ((flags = device_get_flags(me)) != 0)
2090 retval += printf(" flags %#x", flags);
2091 retval += printf("\n");
2097 * Configuration/initialization, per drive.
2100 fd_probe(device_t dev)
2108 struct fdc_data *fdc;
2112 fdsu = fdc_get_fdunit(dev);
2113 fd = device_get_softc(dev);
2114 fdc = device_get_softc(device_get_parent(dev));
2115 flags = device_get_flags(dev);
2120 unit = device_get_unit(dev);
2122 /* Auto-probe if fdinfo is present, but always allow override. */
2123 type = flags & FD_TYPEMASK;
2124 if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
2128 /* make sure fdautoselect() will be called */
2129 fd->flags = FD_EMPTY;
2134 pc98_fd_check_type(fd, unit);
2135 #elif defined(__i386__) || defined(__amd64__)
2136 if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
2137 /* Look up what the BIOS thinks we have. */
2139 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
2141 fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
2142 if (fd->type == FDT_288M_1)
2143 fd->type = FDT_288M;
2145 #endif /* __i386__ || __amd64__ */
2146 /* is there a unit? */
2147 if (fd->type == FDT_NONE)
2152 mtx_lock(&fdc->fdc_mtx);
2158 fdc_reset(fdc); /* XXX reset, then unreset, etc. */
2159 DELAY(1000000); /* 1 sec */
2161 if ((flags & FD_NO_PROBE) == 0) {
2162 /* If we're at track 0 first seek inwards. */
2163 if ((fdc_sense_drive(fdc, &st3) == 0) &&
2164 (st3 & NE7_ST3_T0)) {
2165 /* Seek some steps... */
2166 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
2167 /* ...wait a moment... */
2169 /* make ctrlr happy: */
2170 fdc_sense_int(fdc, NULL, NULL);
2174 for (i = 0; i < 2; i++) {
2176 * we must recalibrate twice, just in case the
2177 * heads have been beyond cylinder 76, since
2178 * most FDCs still barf when attempting to
2179 * recalibrate more than 77 steps
2182 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
2183 /* a second being enough for full stroke seek*/
2184 DELAY(i == 0 ? 1000000 : 300000);
2186 /* anything responding? */
2187 if (fdc_sense_int(fdc, &st0, NULL) == 0 &&
2188 (st0 & NE7_ST0_EC) == 0)
2189 break; /* already probed succesfully */
2197 mtx_unlock(&fdc->fdc_mtx);
2200 if ((flags & FD_NO_PROBE) == 0 &&
2201 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
2210 device_set_desc(dev, "1.44M FDD");
2213 device_set_desc(dev, "1M/640K FDD");
2217 device_set_desc(dev, "1200-KB 5.25\" drive");
2220 device_set_desc(dev, "1440-KB 3.5\" drive");
2223 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
2226 device_set_desc(dev, "360-KB 5.25\" drive");
2229 device_set_desc(dev, "720-KB 3.5\" drive");
2235 fd->track = FD_NO_TRACK;
2242 callout_init_mtx(&fd->toffhandle, &fd->fdc->fdc_mtx, 0);
2244 /* initialize densities for subdevices */
2245 fdsettype(fd, fd_native_types[fd->type]);
2250 * We have to do this in a geom event because GEOM is not running
2251 * when fd_attach() is.
2252 * XXX: move fd_attach after geom like ata/scsi disks
2255 fd_attach2(void *arg, int flag)
2261 fd->fd_geom = g_new_geomf(&g_fd_class,
2262 "fd%d", device_get_unit(fd->dev));
2263 fd->fd_provider = g_new_providerf(fd->fd_geom, "%s", fd->fd_geom->name);
2264 fd->fd_geom->softc = fd;
2265 g_error_provider(fd->fd_provider, 0);
2269 fd_attach(device_t dev)
2273 fd = device_get_softc(dev);
2274 g_post_event(fd_attach2, fd, M_WAITOK, NULL);
2275 fd->flags |= FD_EMPTY;
2276 bioq_init(&fd->fd_bq);
2282 fd_detach_geom(void *arg, int flag)
2284 struct fd_data *fd = arg;
2286 g_topology_assert();
2287 g_wither_geom(fd->fd_geom, ENXIO);
2291 fd_detach(device_t dev)
2295 fd = device_get_softc(dev);
2296 g_waitfor_event(fd_detach_geom, fd, M_WAITOK, NULL);
2297 while (device_get_state(dev) == DS_BUSY)
2298 tsleep(fd, PZERO, "fdd", hz/10);
2299 callout_drain(&fd->toffhandle);
2304 static device_method_t fd_methods[] = {
2305 /* Device interface */
2306 DEVMETHOD(device_probe, fd_probe),
2307 DEVMETHOD(device_attach, fd_attach),
2308 DEVMETHOD(device_detach, fd_detach),
2309 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2310 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
2311 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
2315 static driver_t fd_driver = {
2318 sizeof(struct fd_data)
2322 fdc_modevent(module_t mod, int type, void *data)
2325 return (g_modevent(NULL, type, &g_fd_class));
2328 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, fdc_modevent, 0);