2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2004 Poul-Henning Kamp
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
11 * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
12 * aided by the Linux floppy driver modifications from David Bateman
13 * (dbateman@eng.uts.edu.au).
15 * Copyright (c) 1993, 1994 by
16 * jc@irbs.UUCP (John Capo)
17 * vak@zebub.msk.su (Serge Vakulenko)
18 * ache@astral.msk.su (Andrew A. Chernov)
20 * Copyright (c) 1993, 1994, 1995 by
21 * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
22 * dufault@hda.com (Peter Dufault)
24 * Copyright (c) 2001 Joerg Wunsch,
25 * joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. Neither the name of the University nor the names of its contributors
36 * may be used to endorse or promote products derived from this software
37 * without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
60 #include <sys/param.h>
63 #include <sys/devicestat.h>
65 #include <sys/fcntl.h>
66 #include <sys/fdcio.h>
67 #include <sys/filio.h>
68 #include <sys/kernel.h>
69 #include <sys/kthread.h>
71 #include <sys/malloc.h>
72 #include <sys/module.h>
73 #include <sys/mutex.h>
77 #include <sys/sysctl.h>
78 #include <sys/systm.h>
80 #include <geom/geom.h>
82 #include <machine/bus.h>
83 #include <machine/clock.h>
84 #include <machine/stdarg.h>
86 #include <isa/isavar.h>
87 #include <isa/isareg.h>
89 #include <dev/fdc/fdcvar.h>
91 #include <dev/ic/nec765.h>
94 * Runtime configuration hints/flags
97 /* configuration flags for fd */
98 #define FD_TYPEMASK 0x0f /* drive type, matches enum
99 * fd_drivetype; on i386 machines, if
100 * given as 0, use RTC type for fd0
102 #define FD_NO_CHLINE 0x10 /* drive does not support changeline
103 * aka. unit attention */
104 #define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just
105 * assume it is there */
108 * Things that could conceiveably considered parameters or tweakables
112 * Maximal number of bytes in a cylinder.
113 * This is used for ISADMA bouncebuffer allocation and sets the max
114 * xfersize we support.
116 * 2.88M format has 2 x 36 x 512, allow for hacked up density.
118 #define MAX_BYTES_PER_CYL (2 * 40 * 512)
121 * Timeout value for the PIO loops to wait until the FDC main status
122 * register matches our expectations (request for master, direction
123 * bit). This is supposed to be a number of microseconds, although
124 * timing might actually not be very accurate.
126 * Timeouts of 100 msec are believed to be required for some broken
129 #define FDSTS_TIMEOUT 100000
132 * After this many errors, stop whining. Close will reset this count.
134 #define FDC_ERRMAX 100
137 * AutoDensity search lists for each drive type.
140 static struct fd_type fd_searchlist_360k[] = {
145 static struct fd_type fd_searchlist_12m[] = {
146 { FDF_5_1200 | FL_AUTO },
147 { FDF_5_400 | FL_AUTO },
148 { FDF_5_360 | FL_2STEP | FL_AUTO},
152 static struct fd_type fd_searchlist_720k[] = {
157 static struct fd_type fd_searchlist_144m[] = {
158 { FDF_3_1440 | FL_AUTO},
159 { FDF_3_720 | FL_AUTO},
163 static struct fd_type fd_searchlist_288m[] = {
164 { FDF_3_1440 | FL_AUTO },
166 { FDF_3_2880 | FL_AUTO }, /* XXX: probably doesn't work */
168 { FDF_3_720 | FL_AUTO},
173 * Order must match enum fd_drivetype in <sys/fdcio.h>.
175 static struct fd_type *fd_native_types[] = {
177 fd_searchlist_360k, /* FDT_360K */
178 fd_searchlist_12m, /* FDT_12M */
179 fd_searchlist_720k, /* FDT_720K */
180 fd_searchlist_144m, /* FDT_144M */
181 fd_searchlist_288m, /* FDT_288M_1 (mapped to FDT_288M) */
182 fd_searchlist_288m, /* FDT_288M */
186 * Internals start here
190 #define FDOUT 2 /* Digital Output Register (W) */
191 #define FDO_FDSEL 0x03 /* floppy device select */
192 #define FDO_FRST 0x04 /* floppy controller reset */
193 #define FDO_FDMAEN 0x08 /* enable floppy DMA and Interrupt */
194 #define FDO_MOEN0 0x10 /* motor enable drive 0 */
195 #define FDO_MOEN1 0x20 /* motor enable drive 1 */
196 #define FDO_MOEN2 0x40 /* motor enable drive 2 */
197 #define FDO_MOEN3 0x80 /* motor enable drive 3 */
199 #define FDSTS 4 /* NEC 765 Main Status Register (R) */
200 #define FDDSR 4 /* Data Rate Select Register (W) */
201 #define FDDATA 5 /* NEC 765 Data Register (R/W) */
202 #define FDCTL 7 /* Control Register (W) */
205 * The YE-DATA PC Card floppies use PIO to read in the data rather
206 * than DMA due to the wild variability of DMA for the PC Card
207 * devices. DMA was deleted from the PC Card specification in version
208 * 7.2 of the standard, but that post-dates the YE-DATA devices by many
211 * In addition, if we cannot setup the DMA resources for the ISA
212 * attachment, we'll use this same offset for data transfer. However,
213 * that almost certainly won't work.
215 * For this mode, offset 0 and 1 must be used to setup the transfer
216 * for this floppy. This is OK for PC Card YE Data devices, but for
217 * ISA this is likely wrong. These registers are only available on
218 * those systems that map them to the floppy drive. Newer systems do
219 * not do this, and we should likely prohibit access to them (or
220 * disallow NODMA to be set).
222 #define FDBCDR 0 /* And 1 */
223 #define FD_YE_DATAPORT 6 /* Drive Data port */
225 #define FDI_DCHG 0x80 /* diskette has been changed */
226 /* requires drive and motor being selected */
227 /* is cleared by any step pulse to drive */
230 * We have three private BIO commands.
232 #define BIO_PROBE BIO_CMD0
233 #define BIO_RDID BIO_CMD1
234 #define BIO_FMT BIO_CMD2
237 * Per drive structure (softc).
240 u_char *fd_ioptr; /* IO pointer */
241 u_int fd_iosize; /* Size of IO chunks */
242 u_int fd_iocount; /* Outstanding requests */
243 struct fdc_data *fdc; /* pointer to controller structure */
244 int fdsu; /* this units number on this controller */
245 enum fd_drivetype type; /* drive type */
246 struct fd_type *ft; /* pointer to current type descriptor */
247 struct fd_type fts; /* type descriptors */
250 #define FD_WP (1<<0) /* Write protected */
251 #define FD_MOTOR (1<<1) /* motor should be on */
252 #define FD_MOTORWAIT (1<<2) /* motor should be on */
253 #define FD_EMPTY (1<<3) /* no media */
254 #define FD_NEWDISK (1<<4) /* media changed */
255 #define FD_ISADMA (1<<5) /* isa dma started */
256 int track; /* where we think the head is */
257 #define FD_NO_TRACK -2
258 int options; /* FDOPT_* */
259 struct callout toffhandle;
260 struct g_geom *fd_geom;
261 struct g_provider *fd_provider;
263 struct bio_queue_head fd_bq;
266 #define FD_NOT_VALID -2
268 static driver_intr_t fdc_intr;
269 static driver_filter_t fdc_intr_fast;
270 static void fdc_reset(struct fdc_data *);
271 static int fd_probe_disk(struct fd_data *, int *);
273 static SYSCTL_NODE(_debug, OID_AUTO, fdc, CTLFLAG_RW, 0, "fdc driver");
275 static int fifo_threshold = 8;
276 SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0,
277 "FIFO threshold setting");
279 static int debugflags = 0;
280 SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0,
283 static int retries = 10;
284 SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0,
285 "Number of retries to attempt");
287 static int spec1 = NE7_SPEC_1(6, 240);
288 SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0,
289 "Specification byte one (step-rate + head unload)");
291 static int spec2 = NE7_SPEC_2(16, 0);
292 SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0,
293 "Specification byte two (head load time + no-dma)");
296 SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0,
297 "Head settling time in sec/hz");
300 fdprinttype(struct fd_type *ft)
303 printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)",
304 ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks,
305 ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter,
306 ft->offset_side2, ft->flags);
310 fdsettype(struct fd_data *fd, struct fd_type *ft)
313 ft->size = ft->sectrac * ft->heads * ft->tracks;
314 fd->sectorsize = 128 << fd->ft->secsize;
318 * Bus space handling (access to low-level IO).
321 fdregwr(struct fdc_data *fdc, int reg, uint8_t v)
324 bus_space_write_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg], v);
327 static inline uint8_t
328 fdregrd(struct fdc_data *fdc, int reg)
331 return bus_space_read_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg]);
335 fdctl_wr(struct fdc_data *fdc, u_int8_t v)
338 fdregwr(fdc, FDCTL, v);
342 fdout_wr(struct fdc_data *fdc, u_int8_t v)
345 fdregwr(fdc, FDOUT, v);
349 fdsts_rd(struct fdc_data *fdc)
352 return fdregrd(fdc, FDSTS);
356 fddsr_wr(struct fdc_data *fdc, u_int8_t v)
359 fdregwr(fdc, FDDSR, v);
363 fddata_wr(struct fdc_data *fdc, u_int8_t v)
366 fdregwr(fdc, FDDATA, v);
370 fddata_rd(struct fdc_data *fdc)
373 return fdregrd(fdc, FDDATA);
377 fdin_rd(struct fdc_data *fdc)
380 return fdregrd(fdc, FDCTL);
384 * Magic pseudo-DMA initialization for YE FDC. Sets count and
388 fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count)
390 fdregwr(fdc, FDBCDR, (count - 1) & 0xff);
391 fdregwr(fdc, FDBCDR + 1,
392 (iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f));
396 fdc_err(struct fdc_data *fdc, const char *s)
400 if (fdc->fdc_errs < FDC_ERRMAX)
401 device_printf(fdc->fdc_dev, "%s", s);
402 else if (fdc->fdc_errs == FDC_ERRMAX)
403 device_printf(fdc->fdc_dev, "too many errors, not "
404 "logging any more\n");
411 * FDC IO functions, take care of the main status register, timeout
412 * in case the desired status bits are never set.
414 * These PIO loops initially start out with short delays between
415 * each iteration in the expectation that the required condition
416 * is usually met quickly, so it can be handled immediately.
419 fdc_in(struct fdc_data *fdc, int *ptr)
424 for (j = 0; j < FDSTS_TIMEOUT; j += step) {
425 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
426 if (i == (NE7_DIO|NE7_RQM)) {
433 return (fdc_err(fdc, "ready for output in input\n"));
437 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
441 fdc_out(struct fdc_data *fdc, int x)
446 for (j = 0; j < FDSTS_TIMEOUT; j += step) {
447 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
452 if (i == (NE7_DIO|NE7_RQM))
453 return (fdc_err(fdc, "ready for input in output\n"));
457 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
461 * fdc_cmd: Send a command to the chip.
462 * Takes a varargs with this structure:
464 * output bytes as int [...]
466 * input bytes as int* [...]
469 fdc_cmd(struct fdc_data *fdc, int n_out, ...)
477 for (n = 0; n < n_out; n++) {
481 if (fdc_out(fdc, i) < 0) {
483 snprintf(msg, sizeof(msg),
484 "cmd %x failed at out byte %d of %d\n",
486 fdc->flags |= FDC_NEEDS_RESET;
488 return fdc_err(fdc, msg);
491 n_in = va_arg(ap, int);
492 for (n = 0; n < n_in; n++) {
493 int *ptr = va_arg(ap, int *);
494 if (fdc_in(fdc, ptr) < 0) {
496 snprintf(msg, sizeof(msg),
497 "cmd %02x failed at in byte %d of %d\n",
499 fdc->flags |= FDC_NEEDS_RESET;
501 return fdc_err(fdc, msg);
509 fdc_reset(struct fdc_data *fdc)
513 if (fdc->fdct == FDC_ENHANCED) {
514 /* Try a software reset, default precomp, and 500 kb/s */
515 fddsr_wr(fdc, I8207X_DSR_SR);
517 /* Try a hardware reset, keep motor on */
518 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
520 /* enable FDC, but defer interrupts a moment */
521 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
524 fdout_wr(fdc, fdc->fdout);
526 /* XXX after a reset, silently believe the FDC will accept commands */
527 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
528 device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
530 if (fdc->fdct == FDC_ENHANCED) {
534 /* 0x40 | */ /* Enable Implied Seek -
536 0x10 | /* Polling disabled */
537 (fifo_threshold - 1), /* Fifo threshold */
538 0x00, /* Precomp track */
540 device_printf(fdc->fdc_dev,
541 " CONFIGURE failed in reset\n");
542 if (debugflags & 1) {
545 10, &r[0], &r[1], &r[2], &r[3], &r[4],
546 &r[5], &r[6], &r[7], &r[8], &r[9]))
547 device_printf(fdc->fdc_dev,
548 " DUMPREG failed in reset\n");
549 for (i = 0; i < 10; i++)
550 printf(" %02x", r[i]);
557 fdc_sense_drive(struct fdc_data *fdc, int *st3p)
561 if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
562 return (fdc_err(fdc, "Sense Drive Status failed\n"));
569 fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
573 ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
575 (void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
582 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
584 * There doesn't seem to have been an interrupt.
586 return (FD_NOT_VALID);
589 if (fdc_in(fdc, &cyl) < 0)
590 return fdc_err(fdc, "can't get cyl num\n");
599 fdc_read_status(struct fdc_data *fdc)
603 for (i = ret = 0; i < 7; i++) {
604 ret = fdc_in(fdc, &status);
605 fdc->status[i] = status;
611 fdc->flags |= FDC_STAT_VALID;
613 fdc->flags &= ~FDC_STAT_VALID;
622 fd_select(struct fd_data *fd)
624 struct fdc_data *fdc;
626 /* XXX: lock controller */
628 fdc->fdout &= ~FDO_FDSEL;
629 fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
630 fdout_wr(fdc, fdc->fdout);
641 mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
642 fd->flags &= ~FD_MOTORWAIT;
643 fd->flags |= FD_MOTOR;
646 bp = bioq_takefirst(&fd->fd_bq);
649 bioq_disksort(&fd->fdc->head, bp);
653 wakeup(&fd->fdc->head);
657 fd_motor(struct fd_data *fd, int turnon)
659 struct fdc_data *fdc;
663 mtx_assert(&fdc->fdc_mtx, MA_OWNED);
666 fd->flags |= FD_MOTORWAIT;
667 fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
668 callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
670 callout_stop(&fd->toffhandle);
671 fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
672 fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
674 fdout_wr(fdc, fdc->fdout);
678 fd_turnoff(void *xfd)
680 struct fd_data *fd = xfd;
682 mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
687 * fdc_intr - wake up the worker thread.
698 fdc_intr_fast(void *arg)
702 return(FILTER_HANDLED);
706 * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
709 fdc_pio(struct fdc_data *fdc)
716 cptr = fdc->fd->fd_ioptr;
717 count = fdc->fd->fd_iosize;
719 if (bp->bio_cmd == BIO_READ) {
720 fdbcdr_wr(fdc, 0, count);
721 bus_space_read_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
722 fdc->ioff[FD_YE_DATAPORT], cptr, count);
724 bus_space_write_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
725 fdc->ioff[FD_YE_DATAPORT], cptr, count);
726 fdbcdr_wr(fdc, 0, count); /* needed? */
731 fdc_biodone(struct fdc_data *fdc, int error)
739 mtx_lock(&fdc->fdc_mtx);
740 if (--fd->fd_iocount == 0)
741 callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
744 mtx_unlock(&fdc->fdc_mtx);
745 if (bp->bio_to != NULL) {
746 if ((debugflags & 2) && fd->fdc->retry > 0)
747 printf("retries: %d\n", fd->fdc->retry);
748 g_io_deliver(bp, error);
751 bp->bio_error = error;
752 bp->bio_flags |= BIO_DONE;
757 static int retry_line;
760 fdc_worker(struct fdc_data *fdc)
765 int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
768 static int need_recal;
769 struct fdc_readid *idp;
770 struct fd_formb *finfo;
774 /* Have we exhausted our retries ? */
778 (fdc->retry >= retries || (fd->options & FDOPT_NORETRY))) {
779 if ((debugflags & 4))
780 printf("Too many retries (EIO)\n");
781 if (fdc->flags & FDC_NEEDS_RESET) {
782 mtx_lock(&fdc->fdc_mtx);
783 fd->flags |= FD_EMPTY;
784 mtx_unlock(&fdc->fdc_mtx);
786 return (fdc_biodone(fdc, EIO));
789 /* Disable ISADMA if we bailed while it was active */
790 if (fd != NULL && (fd->flags & FD_ISADMA)) {
792 bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
793 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
794 mtx_lock(&fdc->fdc_mtx);
795 fd->flags &= ~FD_ISADMA;
796 mtx_unlock(&fdc->fdc_mtx);
799 /* Unwedge the controller ? */
800 if (fdc->flags & FDC_NEEDS_RESET) {
801 fdc->flags &= ~FDC_NEEDS_RESET;
806 tsleep(fdc, PRIBIO, "fdcrst", hz);
807 /* Discard results */
808 for (i = 0; i < 4; i++)
809 fdc_sense_int(fdc, &st0, &cyl);
810 /* All drives must recal */
814 /* Pick up a request, if need be wait for it */
815 if (fdc->bp == NULL) {
816 mtx_lock(&fdc->fdc_mtx);
818 fdc->bp = bioq_takefirst(&fdc->head);
820 msleep(&fdc->head, &fdc->fdc_mtx,
822 } while (fdc->bp == NULL &&
823 (fdc->flags & FDC_KTHREAD_EXIT) == 0);
824 mtx_unlock(&fdc->fdc_mtx);
828 * Nothing to do, worker thread has been
834 fd = fdc->fd = bp->bio_driver1;
836 fd->fd_ioptr = bp->bio_data;
837 if (bp->bio_cmd == BIO_FMT) {
838 i = offsetof(struct fd_formb, fd_formb_cylno(0));
840 fd->fd_iosize = bp->bio_length - i;
844 /* Select drive, setup params */
846 if (fdc->fdct == FDC_ENHANCED)
847 fddsr_wr(fdc, fd->ft->trans);
849 fdctl_wr(fdc, fd->ft->trans);
851 if (bp->bio_cmd == BIO_PROBE) {
852 if ((!(device_get_flags(fd->dev) & FD_NO_CHLINE) &&
853 !(fdin_rd(fdc) & FDI_DCHG) &&
854 !(fd->flags & FD_EMPTY)) ||
855 fd_probe_disk(fd, &need_recal) == 0)
856 return (fdc_biodone(fdc, 0));
861 * If we are dead just flush the requests
863 if (fd->flags & FD_EMPTY)
864 return (fdc_biodone(fdc, ENXIO));
866 /* Check if we lost our media */
867 if (fdin_rd(fdc) & FDI_DCHG) {
868 if (debugflags & 0x40)
869 printf("Lost disk\n");
870 mtx_lock(&fdc->fdc_mtx);
871 fd->flags |= FD_EMPTY;
872 fd->flags |= FD_NEWDISK;
873 mtx_unlock(&fdc->fdc_mtx);
875 g_orphan_provider(fd->fd_provider, ENXIO);
876 fd->fd_provider->flags |= G_PF_WITHER;
878 g_new_providerf(fd->fd_geom, "%s", fd->fd_geom->name);
879 g_error_provider(fd->fd_provider, 0);
881 return (fdc_biodone(fdc, ENXIO));
884 /* Check if the floppy is write-protected */
885 if (bp->bio_cmd == BIO_FMT || bp->bio_cmd == BIO_WRITE) {
886 retry_line = __LINE__;
887 if(fdc_sense_drive(fdc, &st3) != 0)
890 return (fdc_biodone(fdc, EROFS));
893 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
894 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
895 i = fd->ft->sectrac * fd->ft->heads;
896 cylinder = bp->bio_pblkno / i;
897 descyl = cylinder * steptrac;
898 sec = bp->bio_pblkno % i;
900 head = sec / fd->ft->sectrac;
901 sec = sec % fd->ft->sectrac + 1;
903 /* If everything is going swimmingly, use multisector xfer */
904 if (fdc->retry == 0 &&
905 (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
906 fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
907 nsect = fd->fd_iosize / fd->sectorsize;
908 } else if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
909 fd->fd_iosize = fd->sectorsize;
913 /* Do RECAL if we need to or are going to track zero anyway */
914 if ((need_recal & (1 << fd->fdsu)) ||
915 (cylinder == 0 && fd->track != 0) ||
917 retry_line = __LINE__;
918 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
920 tsleep(fdc, PRIBIO, "fdrecal", hz);
921 retry_line = __LINE__;
922 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
923 return (1); /* XXX */
924 retry_line = __LINE__;
925 if ((st0 & 0xc0) || cyl != 0)
927 need_recal &= ~(1 << fd->fdsu);
929 /* let the heads settle */
931 tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
935 * SEEK to where we want to be
937 if (cylinder != fd->track) {
938 retry_line = __LINE__;
939 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
941 tsleep(fdc, PRIBIO, "fdseek", hz);
942 retry_line = __LINE__;
943 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
944 return (1); /* XXX */
945 retry_line = __LINE__;
946 if ((st0 & 0xc0) || cyl != descyl) {
947 need_recal |= (1 << fd->fdsu);
950 /* let the heads settle */
952 tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
954 fd->track = cylinder;
957 printf("op %x bn %ju siz %u ptr %p retry %d\n",
958 bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
959 fd->fd_ioptr, fdc->retry);
961 /* Setup ISADMA if we need it and have it */
962 if ((bp->bio_cmd == BIO_READ ||
963 bp->bio_cmd == BIO_WRITE ||
964 bp->bio_cmd == BIO_FMT)
965 && !(fdc->flags & FDC_NODMA)) {
967 bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
968 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
969 mtx_lock(&fdc->fdc_mtx);
970 fd->flags |= FD_ISADMA;
971 mtx_unlock(&fdc->fdc_mtx);
974 /* Do PIO if we have to */
975 if (fdc->flags & FDC_NODMA) {
976 if (bp->bio_cmd == BIO_READ ||
977 bp->bio_cmd == BIO_WRITE ||
978 bp->bio_cmd == BIO_FMT)
979 fdbcdr_wr(fdc, 1, fd->fd_iosize);
980 if (bp->bio_cmd == BIO_WRITE ||
981 bp->bio_cmd == BIO_FMT)
985 switch(bp->bio_cmd) {
988 finfo = (struct fd_formb *)bp->bio_data;
989 retry_line = __LINE__;
992 head << 2 | fd->fdsu,
993 finfo->fd_formb_secshift,
994 finfo->fd_formb_nsecs,
995 finfo->fd_formb_gaplen,
996 finfo->fd_formb_fillbyte, 0))
1000 retry_line = __LINE__;
1002 NE7CMD_READID | mfm,
1003 head << 2 | fd->fdsu, 0))
1007 retry_line = __LINE__;
1009 NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT,
1010 head << 2 | fd->fdsu, /* head & unit */
1011 fd->track, /* track */
1013 sec, /* sector + 1 */
1014 fd->ft->secsize, /* sector size */
1015 fd->ft->sectrac, /* sectors/track */
1016 fd->ft->gap, /* gap size */
1017 fd->ft->datalen, /* data length */
1022 retry_line = __LINE__;
1024 NE7CMD_WRITE | mfm | NE7CMD_MT,
1025 head << 2 | fd->fdsu, /* head & unit */
1026 fd->track, /* track */
1028 sec, /* sector + 1 */
1029 fd->ft->secsize, /* sector size */
1030 fd->ft->sectrac, /* sectors/track */
1031 fd->ft->gap, /* gap size */
1032 fd->ft->datalen, /* data length */
1037 KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd));
1040 /* Wait for interrupt */
1041 i = tsleep(fdc, PRIBIO, "fddata", hz);
1043 /* PIO if the read looks good */
1044 if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd == BIO_READ))
1048 if (fd->flags & FD_ISADMA) {
1050 bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
1051 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
1052 mtx_lock(&fdc->fdc_mtx);
1053 fd->flags &= ~FD_ISADMA;
1054 mtx_unlock(&fdc->fdc_mtx);
1061 * Due to IBM's brain-dead design, the FDC has a faked ready
1062 * signal, hardwired to ready == true. Thus, any command
1063 * issued if there's no diskette in the drive will _never_
1064 * complete, and must be aborted by resetting the FDC.
1065 * Many thanks, Big Blue!
1067 retry_line = __LINE__;
1068 fdc->flags |= FDC_NEEDS_RESET;
1072 retry_line = __LINE__;
1073 if (fdc_read_status(fdc))
1076 if (debugflags & 0x10)
1077 printf(" -> %x %x %x %x\n",
1078 fdc->status[0], fdc->status[1],
1079 fdc->status[2], fdc->status[3]);
1081 st0 = fdc->status[0] & NE7_ST0_IC;
1083 retry_line = __LINE__;
1084 if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) {
1086 * DMA overrun. Someone hogged the bus and
1087 * didn't release it in time for the next
1092 retry_line = __LINE__;
1093 if(st0 == NE7_ST0_IC_IV) {
1094 fdc->flags |= FDC_NEEDS_RESET;
1097 retry_line = __LINE__;
1098 if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) {
1099 need_recal |= (1 << fd->fdsu);
1102 if (debugflags & 0x20) {
1103 printf("status %02x %02x %02x %02x %02x %02x\n",
1104 fdc->status[0], fdc->status[1], fdc->status[2],
1105 fdc->status[3], fdc->status[4], fdc->status[5]);
1107 retry_line = __LINE__;
1108 if (fd->options & FDOPT_NOERROR)
1114 switch(bp->bio_cmd) {
1116 /* copy out ID field contents */
1117 idp = (struct fdc_readid *)bp->bio_data;
1118 idp->cyl = fdc->status[3];
1119 idp->head = fdc->status[4];
1120 idp->sec = fdc->status[5];
1121 idp->secshift = fdc->status[6];
1122 if (debugflags & 0x40)
1123 printf("c %d h %d s %d z %d\n",
1124 idp->cyl, idp->head, idp->sec, idp->secshift);
1128 bp->bio_pblkno += nsect;
1129 bp->bio_resid -= fd->fd_iosize;
1130 bp->bio_completed += fd->fd_iosize;
1131 fd->fd_ioptr += fd->fd_iosize;
1132 if (override_error) {
1133 if ((debugflags & 4))
1134 printf("FDOPT_NOERROR: returning bad data\n");
1136 /* Since we managed to get something done,
1137 * reset the retry */
1139 if (bp->bio_resid > 0)
1146 return (fdc_biodone(fdc, 0));
1150 fdc_thread(void *arg)
1152 struct fdc_data *fdc;
1157 mtx_lock(&fdc->fdc_mtx);
1158 fdc->flags |= FDC_KTHREAD_ALIVE;
1159 while ((fdc->flags & FDC_KTHREAD_EXIT) == 0) {
1160 mtx_unlock(&fdc->fdc_mtx);
1161 i = fdc_worker(fdc);
1162 if (i && debugflags & 0x20) {
1163 if (fdc->bp != NULL)
1164 g_print_bio("", fdc->bp, "");
1165 printf("Retry line %d\n", retry_line);
1168 mtx_lock(&fdc->fdc_mtx);
1170 fdc->flags &= ~(FDC_KTHREAD_EXIT | FDC_KTHREAD_ALIVE);
1171 mtx_unlock(&fdc->fdc_mtx);
1177 * Enqueue a request.
1180 fd_enqueue(struct fd_data *fd, struct bio *bp)
1182 struct fdc_data *fdc;
1187 mtx_lock(&fdc->fdc_mtx);
1188 /* If we go from idle, cancel motor turnoff */
1189 if (fd->fd_iocount++ == 0)
1190 callout_stop(&fd->toffhandle);
1191 if (fd->flags & FD_MOTOR) {
1192 /* The motor is on, send it directly to the controller */
1193 bioq_disksort(&fdc->head, bp);
1196 /* Queue it on the drive until the motor has started */
1197 bioq_insert_tail(&fd->fd_bq, bp);
1198 if (!(fd->flags & FD_MOTORWAIT))
1201 mtx_unlock(&fdc->fdc_mtx);
1205 * Try to find out if we have a disk in the drive.
1208 fd_probe_disk(struct fd_data *fd, int *recal)
1210 struct fdc_data *fdc;
1215 oopts = fd->options;
1216 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1220 * First recal, then seek to cyl#1, this clears the old condition on
1221 * the disk change line so we can examine it for current status.
1223 if (debugflags & 0x40)
1224 printf("New disk in probe\n");
1225 mtx_lock(&fdc->fdc_mtx);
1226 fd->flags |= FD_NEWDISK;
1227 mtx_unlock(&fdc->fdc_mtx);
1228 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
1230 tsleep(fdc, PRIBIO, "fdrecal", hz);
1231 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1232 goto done; /* XXX */
1233 if ((st0 & 0xc0) || cyl != 0)
1236 /* Seek to track 1 */
1237 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0))
1239 tsleep(fdc, PRIBIO, "fdseek", hz);
1240 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1241 goto done; /* XXX */
1242 *recal |= (1 << fd->fdsu);
1243 if (fdin_rd(fdc) & FDI_DCHG) {
1244 if (debugflags & 0x40)
1245 printf("Empty in probe\n");
1246 mtx_lock(&fdc->fdc_mtx);
1247 fd->flags |= FD_EMPTY;
1248 mtx_unlock(&fdc->fdc_mtx);
1250 if (fdc_sense_drive(fdc, &st3) != 0)
1252 if (debugflags & 0x40)
1253 printf("Got disk in probe\n");
1254 mtx_lock(&fdc->fdc_mtx);
1255 fd->flags &= ~FD_EMPTY;
1256 if (st3 & NE7_ST3_WP)
1259 fd->flags &= ~FD_WP;
1260 mtx_unlock(&fdc->fdc_mtx);
1265 fd->options = oopts;
1270 fdmisccmd(struct fd_data *fd, u_int cmd, void *data)
1273 struct fd_formb *finfo;
1274 struct fdc_readid *idfield;
1277 bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
1280 * Set up a bio request for fdstrategy(). bio_offset is faked
1281 * so that fdstrategy() will seek to the requested
1282 * cylinder, and use the desired head.
1285 if (cmd == BIO_FMT) {
1286 finfo = (struct fd_formb *)data;
1288 (finfo->cyl * fd->ft->heads + finfo->head) *
1290 bp->bio_length = sizeof *finfo;
1291 } else if (cmd == BIO_RDID) {
1292 idfield = (struct fdc_readid *)data;
1294 (idfield->cyl * fd->ft->heads + idfield->head) *
1296 bp->bio_length = sizeof(struct fdc_readid);
1297 } else if (cmd == BIO_PROBE) {
1300 panic("wrong cmd in fdmisccmd()");
1301 bp->bio_offset = bp->bio_pblkno * fd->sectorsize;
1302 bp->bio_data = data;
1303 bp->bio_driver1 = fd;
1309 tsleep(bp, PRIBIO, "fdwait", hz);
1310 } while (!(bp->bio_flags & BIO_DONE));
1311 error = bp->bio_error;
1318 * Try figuring out the density of the media present in our device.
1321 fdautoselect(struct fd_data *fd)
1323 struct fd_type *fdtp;
1324 struct fdc_readid id;
1327 if (!(fd->ft->flags & FL_AUTO))
1330 fdtp = fd_native_types[fd->type];
1331 fdsettype(fd, fdtp);
1332 if (!(fd->ft->flags & FL_AUTO))
1336 * Try reading sector ID fields, first at cylinder 0, head 0,
1337 * then at cylinder 2, head N. We don't probe cylinder 1,
1338 * since for 5.25in DD media in a HD drive, there are no data
1339 * to read (2 step pulses per media cylinder required). For
1340 * two-sided media, the second probe always goes to head 1, so
1341 * we can tell them apart from single-sided media. As a
1342 * side-effect this means that single-sided media should be
1343 * mentioned in the search list after two-sided media of an
1344 * otherwise identical density. Media with a different number
1345 * of sectors per track but otherwise identical parameters
1346 * cannot be distinguished at all.
1348 * If we successfully read an ID field on both cylinders where
1349 * the recorded values match our expectation, we are done.
1350 * Otherwise, we try the next density entry from the table.
1352 * Stepping to cylinder 2 has the side-effect of clearing the
1353 * unit attention bit.
1355 oopts = fd->options;
1356 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1357 for (; fdtp->heads; fdtp++) {
1358 fdsettype(fd, fdtp);
1360 id.cyl = id.head = 0;
1361 rv = fdmisccmd(fd, BIO_RDID, &id);
1364 if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize)
1367 id.head = fd->ft->heads - 1;
1368 rv = fdmisccmd(fd, BIO_RDID, &id);
1369 if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
1370 id.secshift != fdtp->secsize)
1376 fd->options = oopts;
1377 if (fdtp->heads == 0) {
1378 if (debugflags & 0x40)
1379 device_printf(fd->dev, "autoselection failed\n");
1380 fdsettype(fd, fd_native_types[fd->type]);
1383 if (debugflags & 0x40) {
1384 device_printf(fd->dev,
1385 "autoselected %d KB medium\n",
1387 fdprinttype(fd->ft);
1394 * GEOM class implementation
1397 static g_access_t fd_access;
1398 static g_start_t fd_start;
1399 static g_ioctl_t fd_ioctl;
1401 struct g_class g_fd_class = {
1403 .version = G_VERSION,
1405 .access = fd_access,
1410 fd_access(struct g_provider *pp, int r, int w, int e)
1413 struct fdc_data *fdc;
1417 fd = pp->geom->softc;
1421 * If our provider is withering, we can only get negative requests
1422 * and we don't want to even see them
1424 if (pp->flags & G_PF_WITHER)
1431 if (ar == 0 && aw == 0 && ae == 0) {
1432 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
1433 device_unbusy(fd->dev);
1438 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
1439 if (fdmisccmd(fd, BIO_PROBE, NULL))
1441 if (fd->flags & FD_EMPTY)
1443 if (fd->flags & FD_NEWDISK) {
1444 if (fdautoselect(fd) != 0 &&
1445 (device_get_flags(fd->dev) & FD_NO_CHLINE)) {
1446 mtx_lock(&fdc->fdc_mtx);
1447 fd->flags |= FD_EMPTY;
1448 mtx_unlock(&fdc->fdc_mtx);
1451 mtx_lock(&fdc->fdc_mtx);
1452 fd->flags &= ~FD_NEWDISK;
1453 mtx_unlock(&fdc->fdc_mtx);
1455 device_busy(fd->dev);
1459 if (w > 0 && (fd->flags & FD_WP)) {
1461 device_unbusy(fd->dev);
1465 pp->sectorsize = fd->sectorsize;
1466 pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
1467 pp->mediasize = pp->stripesize * fd->ft->tracks;
1472 fd_start(struct bio *bp)
1474 struct fdc_data * fdc;
1475 struct fd_data * fd;
1477 fd = bp->bio_to->geom->softc;
1479 bp->bio_driver1 = fd;
1480 if (bp->bio_cmd == BIO_GETATTR) {
1481 if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
1483 if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
1485 g_io_deliver(bp, ENOIOCTL);
1488 if (!(bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
1489 g_io_deliver(bp, EOPNOTSUPP);
1492 bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
1493 bp->bio_resid = bp->bio_length;
1499 fd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
1502 struct fdc_status *fsp;
1503 struct fdc_readid *rid;
1506 fd = pp->geom->softc;
1509 case FD_GTYPE: /* get drive type */
1510 *(struct fd_type *)data = *fd->ft;
1513 case FD_STYPE: /* set drive type */
1515 * Allow setting drive type temporarily iff
1516 * currently unset. Used for fdformat so any
1517 * user can set it, and then start formatting.
1519 fd->fts = *(struct fd_type *)data;
1520 if (fd->fts.sectrac) {
1521 /* XXX: check for rubbish */
1522 fdsettype(fd, &fd->fts);
1524 fdsettype(fd, fd_native_types[fd->type]);
1526 if (debugflags & 0x40)
1527 fdprinttype(fd->ft);
1530 case FD_GOPTS: /* get drive options */
1531 *(int *)data = fd->options;
1534 case FD_SOPTS: /* set drive options */
1535 fd->options = *(int *)data;
1539 error = priv_check(td, PRIV_DRIVER);
1542 fd->fdc->fdc_errs = 0;
1546 fsp = (struct fdc_status *)data;
1547 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
1549 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
1553 *(enum fd_drivetype *)data = fd->type;
1557 if (!(fflag & FWRITE))
1559 if (((struct fd_formb *)data)->format_version !=
1561 return (EINVAL); /* wrong version of formatting prog */
1562 error = fdmisccmd(fd, BIO_FMT, data);
1563 mtx_lock(&fd->fdc->fdc_mtx);
1564 fd->flags |= FD_NEWDISK;
1565 mtx_unlock(&fd->fdc->fdc_mtx);
1569 rid = (struct fdc_readid *)data;
1570 if (rid->cyl > 85 || rid->head > 1)
1572 error = fdmisccmd(fd, BIO_RDID, data);
1577 /* For backwards compat with old fd*(8) tools */
1582 if (debugflags & 0x80)
1583 printf("Unknown ioctl %lx\n", cmd);
1593 * Configuration/initialization stuff, per controller.
1596 devclass_t fdc_devclass;
1597 static devclass_t fd_devclass;
1605 fdc_release_resources(struct fdc_data *fdc)
1608 struct resource *last;
1613 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1614 fdc->fdc_intr = NULL;
1615 if (fdc->res_irq != NULL)
1616 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
1618 fdc->res_irq = NULL;
1620 for (i = 0; i < FDC_MAXREG; i++) {
1621 if (fdc->resio[i] != NULL && fdc->resio[i] != last) {
1622 bus_release_resource(dev, SYS_RES_IOPORT,
1623 fdc->ridio[i], fdc->resio[i]);
1624 last = fdc->resio[i];
1625 fdc->resio[i] = NULL;
1628 if (fdc->res_drq != NULL)
1629 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
1631 fdc->res_drq = NULL;
1635 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1637 struct fdc_ivars *ivars = device_get_ivars(child);
1640 case FDC_IVAR_FDUNIT:
1641 *result = ivars->fdunit;
1643 case FDC_IVAR_FDTYPE:
1644 *result = ivars->fdtype;
1653 fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1655 struct fdc_ivars *ivars = device_get_ivars(child);
1658 case FDC_IVAR_FDUNIT:
1659 ivars->fdunit = value;
1661 case FDC_IVAR_FDTYPE:
1662 ivars->fdtype = value;
1671 fdc_initial_reset(device_t dev, struct fdc_data *fdc)
1673 int ic_type, part_id;
1676 * A status value of 0xff is very unlikely, but not theoretically
1677 * impossible, but it is far more likely to indicate an empty bus.
1679 if (fdsts_rd(fdc) == 0xff)
1683 * Assert a reset to the floppy controller and check that the status
1684 * register goes to zero.
1688 if (fdsts_rd(fdc) != 0)
1692 * Clear the reset and see it come ready.
1694 fdout_wr(fdc, FDO_FRST);
1696 if (fdsts_rd(fdc) != 0x80)
1699 /* Then, see if it can handle a command. */
1700 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(6, 240),
1701 NE7_SPEC_2(31, 0), 0))
1705 * Try to identify the chip.
1707 * The i8272 datasheet documents that unknown commands
1708 * will return ST0 as 0x80. The i8272 is supposedly identical
1710 * The i82077SL datasheet says 0x90 for the VERSION command,
1711 * and several "superio" chips emulate this.
1713 if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
1715 if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
1719 "ic_type %02x part_id %02x\n", ic_type, part_id);
1720 switch (ic_type & 0xff) {
1722 device_set_desc(dev, "NEC 765 or clone");
1723 fdc->fdct = FDC_NE765;
1727 device_set_desc(dev,
1728 "Enhanced floppy controller");
1729 fdc->fdct = FDC_ENHANCED;
1732 device_set_desc(dev, "Generic floppy controller");
1733 fdc->fdct = FDC_UNKNOWN;
1740 fdc_detach(device_t dev)
1742 struct fdc_data *fdc;
1745 fdc = device_get_softc(dev);
1747 /* have our children detached first */
1748 if ((error = bus_generic_detach(dev)))
1752 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1753 fdc->fdc_intr = NULL;
1755 /* kill worker thread */
1756 mtx_lock(&fdc->fdc_mtx);
1757 fdc->flags |= FDC_KTHREAD_EXIT;
1759 while ((fdc->flags & FDC_KTHREAD_ALIVE) != 0)
1760 msleep(fdc->fdc_thread, &fdc->fdc_mtx, PRIBIO, "fdcdet", 0);
1761 mtx_unlock(&fdc->fdc_mtx);
1763 /* reset controller, turn motor off */
1766 if (!(fdc->flags & FDC_NODMA))
1767 isa_dma_release(fdc->dmachan);
1768 fdc_release_resources(fdc);
1769 mtx_destroy(&fdc->fdc_mtx);
1774 * Add a child device to the fdc controller. It will then be probed etc.
1777 fdc_add_child(device_t dev, const char *name, int unit)
1779 struct fdc_ivars *ivar;
1782 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
1785 child = device_add_child(dev, name, unit);
1786 if (child == NULL) {
1787 free(ivar, M_DEVBUF);
1790 device_set_ivars(child, ivar);
1791 ivar->fdunit = unit;
1792 ivar->fdtype = FDT_NONE;
1793 if (resource_disabled(name, unit))
1794 device_disable(child);
1799 fdc_attach(device_t dev)
1801 struct fdc_data *fdc;
1804 fdc = device_get_softc(dev);
1806 error = fdc_initial_reset(dev, fdc);
1808 device_printf(dev, "does not respond\n");
1811 error = bus_setup_intr(dev, fdc->res_irq,
1812 INTR_TYPE_BIO | INTR_ENTROPY |
1813 ((fdc->flags & FDC_NOFAST) ? INTR_MPSAFE : 0),
1814 ((fdc->flags & FDC_NOFAST) ? NULL : fdc_intr_fast),
1815 ((fdc->flags & FDC_NOFAST) ? fdc_intr : NULL),
1816 fdc, &fdc->fdc_intr);
1818 device_printf(dev, "cannot setup interrupt\n");
1821 if (!(fdc->flags & FDC_NODMA)) {
1822 error = isa_dma_acquire(fdc->dmachan);
1824 error = isa_dma_init(fdc->dmachan,
1825 MAX_BYTES_PER_CYL, M_WAITOK);
1827 isa_dma_release(fdc->dmachan);
1832 fdc->fdcu = device_get_unit(dev);
1833 fdc->flags |= FDC_NEEDS_RESET;
1835 mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
1837 /* reset controller, turn motor off, clear fdout mirror reg */
1838 fdout_wr(fdc, fdc->fdout = 0);
1839 bioq_init(&fdc->head);
1847 fdc_start_worker(device_t dev)
1849 struct fdc_data *fdc;
1851 fdc = device_get_softc(dev);
1852 kproc_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
1853 "fdc%d", device_get_unit(dev));
1857 fdc_hints_probe(device_t dev)
1859 const char *name, *dname;
1860 int i, error, dunit;
1863 * Probe and attach any children. We should probably detect
1864 * devices from the BIOS unless overridden.
1866 name = device_get_nameunit(dev);
1868 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
1869 resource_int_value(dname, dunit, "drive", &dunit);
1870 fdc_add_child(dev, dname, dunit);
1873 if ((error = bus_generic_attach(dev)) != 0)
1879 fdc_print_child(device_t me, device_t child)
1881 int retval = 0, flags;
1883 retval += bus_print_child_header(me, child);
1884 retval += printf(" on %s drive %d", device_get_nameunit(me),
1885 fdc_get_fdunit(child));
1886 if ((flags = device_get_flags(me)) != 0)
1887 retval += printf(" flags %#x", flags);
1888 retval += printf("\n");
1894 * Configuration/initialization, per drive.
1897 fd_probe(device_t dev)
1903 struct fdc_data *fdc;
1907 fdsu = fdc_get_fdunit(dev);
1908 fd = device_get_softc(dev);
1909 fdc = device_get_softc(device_get_parent(dev));
1910 flags = device_get_flags(dev);
1915 unit = device_get_unit(dev);
1917 /* Auto-probe if fdinfo is present, but always allow override. */
1918 type = flags & FD_TYPEMASK;
1919 if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
1923 /* make sure fdautoselect() will be called */
1924 fd->flags = FD_EMPTY;
1928 #if defined(__i386__) || defined(__amd64__)
1929 if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
1930 /* Look up what the BIOS thinks we have. */
1932 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
1934 fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
1935 if (fd->type == FDT_288M_1)
1936 fd->type = FDT_288M;
1938 #endif /* __i386__ || __amd64__ */
1939 /* is there a unit? */
1940 if (fd->type == FDT_NONE)
1943 mtx_lock(&fdc->fdc_mtx);
1949 fdc_reset(fdc); /* XXX reset, then unreset, etc. */
1950 DELAY(1000000); /* 1 sec */
1952 if ((flags & FD_NO_PROBE) == 0) {
1953 /* If we're at track 0 first seek inwards. */
1954 if ((fdc_sense_drive(fdc, &st3) == 0) &&
1955 (st3 & NE7_ST3_T0)) {
1956 /* Seek some steps... */
1957 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1958 /* ...wait a moment... */
1960 /* make ctrlr happy: */
1961 fdc_sense_int(fdc, NULL, NULL);
1965 for (i = 0; i < 2; i++) {
1967 * we must recalibrate twice, just in case the
1968 * heads have been beyond cylinder 76, since
1969 * most FDCs still barf when attempting to
1970 * recalibrate more than 77 steps
1973 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1974 /* a second being enough for full stroke seek*/
1975 DELAY(i == 0 ? 1000000 : 300000);
1977 /* anything responding? */
1978 if (fdc_sense_int(fdc, &st0, NULL) == 0 &&
1979 (st0 & NE7_ST0_EC) == 0)
1980 break; /* already probed successfully */
1987 mtx_unlock(&fdc->fdc_mtx);
1989 if ((flags & FD_NO_PROBE) == 0 &&
1990 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
1997 device_set_desc(dev, "1200-KB 5.25\" drive");
2000 device_set_desc(dev, "1440-KB 3.5\" drive");
2003 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
2006 device_set_desc(dev, "360-KB 5.25\" drive");
2009 device_set_desc(dev, "720-KB 3.5\" drive");
2014 fd->track = FD_NO_TRACK;
2018 callout_init_mtx(&fd->toffhandle, &fd->fdc->fdc_mtx, 0);
2020 /* initialize densities for subdevices */
2021 fdsettype(fd, fd_native_types[fd->type]);
2026 * We have to do this in a geom event because GEOM is not running
2027 * when fd_attach() is.
2028 * XXX: move fd_attach after geom like ata/scsi disks
2031 fd_attach2(void *arg, int flag)
2037 fd->fd_geom = g_new_geomf(&g_fd_class,
2038 "fd%d", device_get_unit(fd->dev));
2039 fd->fd_provider = g_new_providerf(fd->fd_geom, "%s", fd->fd_geom->name);
2040 fd->fd_geom->softc = fd;
2041 g_error_provider(fd->fd_provider, 0);
2045 fd_attach(device_t dev)
2049 fd = device_get_softc(dev);
2050 g_post_event(fd_attach2, fd, M_WAITOK, NULL);
2051 fd->flags |= FD_EMPTY;
2052 bioq_init(&fd->fd_bq);
2058 fd_detach_geom(void *arg, int flag)
2060 struct fd_data *fd = arg;
2062 g_topology_assert();
2063 g_wither_geom(fd->fd_geom, ENXIO);
2067 fd_detach(device_t dev)
2071 fd = device_get_softc(dev);
2072 g_waitfor_event(fd_detach_geom, fd, M_WAITOK, NULL);
2073 while (device_get_state(dev) == DS_BUSY)
2074 tsleep(fd, PZERO, "fdd", hz/10);
2075 callout_drain(&fd->toffhandle);
2080 static device_method_t fd_methods[] = {
2081 /* Device interface */
2082 DEVMETHOD(device_probe, fd_probe),
2083 DEVMETHOD(device_attach, fd_attach),
2084 DEVMETHOD(device_detach, fd_detach),
2085 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2086 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
2087 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
2091 static driver_t fd_driver = {
2094 sizeof(struct fd_data)
2098 fdc_modevent(module_t mod, int type, void *data)
2101 return (g_modevent(NULL, type, &g_fd_class));
2104 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, fdc_modevent, 0);