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 | CTLFLAG_MPSAFE, 0,
276 static int fifo_threshold = 8;
277 SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0,
278 "FIFO threshold setting");
280 static int debugflags = 0;
281 SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0,
284 static int retries = 10;
285 SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0,
286 "Number of retries to attempt");
288 static int spec1 = NE7_SPEC_1(6, 240);
289 SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0,
290 "Specification byte one (step-rate + head unload)");
292 static int spec2 = NE7_SPEC_2(16, 0);
293 SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0,
294 "Specification byte two (head load time + no-dma)");
297 SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0,
298 "Head settling time in sec/hz");
301 fdprinttype(struct fd_type *ft)
304 printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)",
305 ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks,
306 ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter,
307 ft->offset_side2, ft->flags);
311 fdsettype(struct fd_data *fd, struct fd_type *ft)
314 ft->size = ft->sectrac * ft->heads * ft->tracks;
315 fd->sectorsize = 128 << fd->ft->secsize;
319 * Bus space handling (access to low-level IO).
322 fdregwr(struct fdc_data *fdc, int reg, uint8_t v)
325 bus_space_write_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg], v);
328 static inline uint8_t
329 fdregrd(struct fdc_data *fdc, int reg)
332 return bus_space_read_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg]);
336 fdctl_wr(struct fdc_data *fdc, u_int8_t v)
339 fdregwr(fdc, FDCTL, v);
343 fdout_wr(struct fdc_data *fdc, u_int8_t v)
346 fdregwr(fdc, FDOUT, v);
350 fdsts_rd(struct fdc_data *fdc)
353 return fdregrd(fdc, FDSTS);
357 fddsr_wr(struct fdc_data *fdc, u_int8_t v)
360 fdregwr(fdc, FDDSR, v);
364 fddata_wr(struct fdc_data *fdc, u_int8_t v)
367 fdregwr(fdc, FDDATA, v);
371 fddata_rd(struct fdc_data *fdc)
374 return fdregrd(fdc, FDDATA);
378 fdin_rd(struct fdc_data *fdc)
381 return fdregrd(fdc, FDCTL);
385 * Magic pseudo-DMA initialization for YE FDC. Sets count and
389 fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count)
391 fdregwr(fdc, FDBCDR, (count - 1) & 0xff);
392 fdregwr(fdc, FDBCDR + 1,
393 (iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f));
397 fdc_err(struct fdc_data *fdc, const char *s)
401 if (fdc->fdc_errs < FDC_ERRMAX)
402 device_printf(fdc->fdc_dev, "%s", s);
403 else if (fdc->fdc_errs == FDC_ERRMAX)
404 device_printf(fdc->fdc_dev, "too many errors, not "
405 "logging any more\n");
412 * FDC IO functions, take care of the main status register, timeout
413 * in case the desired status bits are never set.
415 * These PIO loops initially start out with short delays between
416 * each iteration in the expectation that the required condition
417 * is usually met quickly, so it can be handled immediately.
420 fdc_in(struct fdc_data *fdc, int *ptr)
425 for (j = 0; j < FDSTS_TIMEOUT; j += step) {
426 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
427 if (i == (NE7_DIO|NE7_RQM)) {
434 return (fdc_err(fdc, "ready for output in input\n"));
438 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
442 fdc_out(struct fdc_data *fdc, int x)
447 for (j = 0; j < FDSTS_TIMEOUT; j += step) {
448 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
453 if (i == (NE7_DIO|NE7_RQM))
454 return (fdc_err(fdc, "ready for input in output\n"));
458 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
462 * fdc_cmd: Send a command to the chip.
463 * Takes a varargs with this structure:
465 * output bytes as int [...]
467 * input bytes as int* [...]
470 fdc_cmd(struct fdc_data *fdc, int n_out, ...)
478 for (n = 0; n < n_out; n++) {
482 if (fdc_out(fdc, i) < 0) {
484 snprintf(msg, sizeof(msg),
485 "cmd %x failed at out byte %d of %d\n",
487 fdc->flags |= FDC_NEEDS_RESET;
489 return fdc_err(fdc, msg);
492 n_in = va_arg(ap, int);
493 for (n = 0; n < n_in; n++) {
494 int *ptr = va_arg(ap, int *);
495 if (fdc_in(fdc, ptr) != 0) {
497 snprintf(msg, sizeof(msg),
498 "cmd %02x failed at in byte %d of %d\n",
500 fdc->flags |= FDC_NEEDS_RESET;
502 return fdc_err(fdc, msg);
510 fdc_reset(struct fdc_data *fdc)
514 if (fdc->fdct == FDC_ENHANCED) {
515 /* Try a software reset, default precomp, and 500 kb/s */
516 fddsr_wr(fdc, I8207X_DSR_SR);
518 /* Try a hardware reset, keep motor on */
519 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
521 /* enable FDC, but defer interrupts a moment */
522 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
525 fdout_wr(fdc, fdc->fdout);
527 /* XXX after a reset, silently believe the FDC will accept commands */
528 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
529 device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
531 if (fdc->fdct == FDC_ENHANCED) {
535 /* 0x40 | */ /* Enable Implied Seek -
537 0x10 | /* Polling disabled */
538 (fifo_threshold - 1), /* Fifo threshold */
539 0x00, /* Precomp track */
541 device_printf(fdc->fdc_dev,
542 " CONFIGURE failed in reset\n");
543 if (debugflags & 1) {
546 10, &r[0], &r[1], &r[2], &r[3], &r[4],
547 &r[5], &r[6], &r[7], &r[8], &r[9]))
548 device_printf(fdc->fdc_dev,
549 " DUMPREG failed in reset\n");
550 for (i = 0; i < 10; i++)
551 printf(" %02x", r[i]);
558 fdc_sense_drive(struct fdc_data *fdc, int *st3p)
562 if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
563 return (fdc_err(fdc, "Sense Drive Status failed\n"));
570 fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
574 ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
576 (void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
583 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
585 * There doesn't seem to have been an interrupt.
587 return (FD_NOT_VALID);
590 if (fdc_in(fdc, &cyl) != 0)
591 return fdc_err(fdc, "can't get cyl num\n");
600 fdc_read_status(struct fdc_data *fdc)
604 for (i = ret = 0; i < 7; i++) {
605 ret = fdc_in(fdc, &status);
606 fdc->status[i] = status;
612 fdc->flags |= FDC_STAT_VALID;
614 fdc->flags &= ~FDC_STAT_VALID;
623 fd_select(struct fd_data *fd)
625 struct fdc_data *fdc;
627 /* XXX: lock controller */
629 fdc->fdout &= ~FDO_FDSEL;
630 fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
631 fdout_wr(fdc, fdc->fdout);
642 mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
643 fd->flags &= ~FD_MOTORWAIT;
644 fd->flags |= FD_MOTOR;
647 bp = bioq_takefirst(&fd->fd_bq);
650 bioq_disksort(&fd->fdc->head, bp);
654 wakeup(&fd->fdc->head);
658 fd_motor(struct fd_data *fd, int turnon)
660 struct fdc_data *fdc;
664 mtx_assert(&fdc->fdc_mtx, MA_OWNED);
667 fd->flags |= FD_MOTORWAIT;
668 fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
669 callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
671 callout_stop(&fd->toffhandle);
672 fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
673 fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
675 fdout_wr(fdc, fdc->fdout);
679 fd_turnoff(void *xfd)
681 struct fd_data *fd = xfd;
683 mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
688 * fdc_intr - wake up the worker thread.
699 fdc_intr_fast(void *arg)
703 return(FILTER_HANDLED);
707 * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
710 fdc_pio(struct fdc_data *fdc)
717 cptr = fdc->fd->fd_ioptr;
718 count = fdc->fd->fd_iosize;
720 if (bp->bio_cmd == BIO_READ) {
721 fdbcdr_wr(fdc, 0, count);
722 bus_space_read_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
723 fdc->ioff[FD_YE_DATAPORT], cptr, count);
725 bus_space_write_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
726 fdc->ioff[FD_YE_DATAPORT], cptr, count);
727 fdbcdr_wr(fdc, 0, count); /* needed? */
732 fdc_biodone(struct fdc_data *fdc, int error)
740 mtx_lock(&fdc->fdc_mtx);
741 if (--fd->fd_iocount == 0)
742 callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
745 mtx_unlock(&fdc->fdc_mtx);
746 if (bp->bio_to != NULL) {
747 if ((debugflags & 2) && fd->fdc->retry > 0)
748 printf("retries: %d\n", fd->fdc->retry);
749 g_io_deliver(bp, error);
752 bp->bio_error = error;
753 bp->bio_flags |= BIO_DONE;
758 static int retry_line;
761 fdc_worker(struct fdc_data *fdc)
766 int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
769 static int need_recal;
770 struct fdc_readid *idp;
771 struct fd_formb *finfo;
775 /* Have we exhausted our retries ? */
779 (fdc->retry >= retries || (fd->options & FDOPT_NORETRY))) {
780 if ((debugflags & 4))
781 printf("Too many retries (EIO)\n");
782 if (fdc->flags & FDC_NEEDS_RESET) {
783 mtx_lock(&fdc->fdc_mtx);
784 fd->flags |= FD_EMPTY;
785 mtx_unlock(&fdc->fdc_mtx);
787 return (fdc_biodone(fdc, EIO));
790 /* Disable ISADMA if we bailed while it was active */
791 if (fd != NULL && (fd->flags & FD_ISADMA)) {
793 bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
794 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
795 mtx_lock(&fdc->fdc_mtx);
796 fd->flags &= ~FD_ISADMA;
797 mtx_unlock(&fdc->fdc_mtx);
800 /* Unwedge the controller ? */
801 if (fdc->flags & FDC_NEEDS_RESET) {
802 fdc->flags &= ~FDC_NEEDS_RESET;
807 tsleep(fdc, PRIBIO, "fdcrst", hz);
808 /* Discard results */
809 for (i = 0; i < 4; i++)
810 fdc_sense_int(fdc, &st0, &cyl);
811 /* All drives must recal */
815 /* Pick up a request, if need be wait for it */
816 if (fdc->bp == NULL) {
817 mtx_lock(&fdc->fdc_mtx);
819 fdc->bp = bioq_takefirst(&fdc->head);
821 msleep(&fdc->head, &fdc->fdc_mtx,
823 } while (fdc->bp == NULL &&
824 (fdc->flags & FDC_KTHREAD_EXIT) == 0);
825 mtx_unlock(&fdc->fdc_mtx);
829 * Nothing to do, worker thread has been
835 fd = fdc->fd = bp->bio_driver1;
837 fd->fd_ioptr = bp->bio_data;
838 if (bp->bio_cmd == BIO_FMT) {
839 i = offsetof(struct fd_formb, fd_formb_cylno(0));
841 fd->fd_iosize = bp->bio_length - i;
845 /* Select drive, setup params */
847 if (fdc->fdct == FDC_ENHANCED)
848 fddsr_wr(fdc, fd->ft->trans);
850 fdctl_wr(fdc, fd->ft->trans);
852 if (bp->bio_cmd == BIO_PROBE) {
853 if ((!(device_get_flags(fd->dev) & FD_NO_CHLINE) &&
854 !(fdin_rd(fdc) & FDI_DCHG) &&
855 !(fd->flags & FD_EMPTY)) ||
856 fd_probe_disk(fd, &need_recal) == 0)
857 return (fdc_biodone(fdc, 0));
862 * If we are dead just flush the requests
864 if (fd->flags & FD_EMPTY)
865 return (fdc_biodone(fdc, ENXIO));
867 /* Check if we lost our media */
868 if (fdin_rd(fdc) & FDI_DCHG) {
869 if (debugflags & 0x40)
870 printf("Lost disk\n");
871 mtx_lock(&fdc->fdc_mtx);
872 fd->flags |= FD_EMPTY;
873 fd->flags |= FD_NEWDISK;
874 mtx_unlock(&fdc->fdc_mtx);
876 g_orphan_provider(fd->fd_provider, ENXIO);
877 fd->fd_provider->flags |= G_PF_WITHER;
879 g_new_providerf(fd->fd_geom, "%s", fd->fd_geom->name);
880 g_error_provider(fd->fd_provider, 0);
882 return (fdc_biodone(fdc, ENXIO));
885 /* Check if the floppy is write-protected */
886 if (bp->bio_cmd == BIO_FMT || bp->bio_cmd == BIO_WRITE) {
887 retry_line = __LINE__;
888 if(fdc_sense_drive(fdc, &st3) != 0)
891 return (fdc_biodone(fdc, EROFS));
894 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
895 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
896 i = fd->ft->sectrac * fd->ft->heads;
897 cylinder = bp->bio_pblkno / i;
898 descyl = cylinder * steptrac;
899 sec = bp->bio_pblkno % i;
901 head = sec / fd->ft->sectrac;
902 sec = sec % fd->ft->sectrac + 1;
904 /* If everything is going swimmingly, use multisector xfer */
905 if (fdc->retry == 0 &&
906 (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
907 fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
908 nsect = fd->fd_iosize / fd->sectorsize;
909 } else if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
910 fd->fd_iosize = fd->sectorsize;
914 /* Do RECAL if we need to or are going to track zero anyway */
915 if ((need_recal & (1 << fd->fdsu)) ||
916 (cylinder == 0 && fd->track != 0) ||
918 retry_line = __LINE__;
919 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
921 tsleep(fdc, PRIBIO, "fdrecal", hz);
922 retry_line = __LINE__;
923 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
924 return (1); /* XXX */
925 retry_line = __LINE__;
926 if ((st0 & 0xc0) || cyl != 0)
928 need_recal &= ~(1 << fd->fdsu);
930 /* let the heads settle */
932 tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
936 * SEEK to where we want to be
938 if (cylinder != fd->track) {
939 retry_line = __LINE__;
940 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
942 tsleep(fdc, PRIBIO, "fdseek", hz);
943 retry_line = __LINE__;
944 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
945 return (1); /* XXX */
946 retry_line = __LINE__;
947 if ((st0 & 0xc0) || cyl != descyl) {
948 need_recal |= (1 << fd->fdsu);
951 /* let the heads settle */
953 tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
955 fd->track = cylinder;
958 printf("op %x bn %ju siz %u ptr %p retry %d\n",
959 bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
960 fd->fd_ioptr, fdc->retry);
962 /* Setup ISADMA if we need it and have it */
963 if ((bp->bio_cmd == BIO_READ ||
964 bp->bio_cmd == BIO_WRITE ||
965 bp->bio_cmd == BIO_FMT)
966 && !(fdc->flags & FDC_NODMA)) {
968 bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
969 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
970 mtx_lock(&fdc->fdc_mtx);
971 fd->flags |= FD_ISADMA;
972 mtx_unlock(&fdc->fdc_mtx);
975 /* Do PIO if we have to */
976 if (fdc->flags & FDC_NODMA) {
977 if (bp->bio_cmd == BIO_READ ||
978 bp->bio_cmd == BIO_WRITE ||
979 bp->bio_cmd == BIO_FMT)
980 fdbcdr_wr(fdc, 1, fd->fd_iosize);
981 if (bp->bio_cmd == BIO_WRITE ||
982 bp->bio_cmd == BIO_FMT)
986 switch(bp->bio_cmd) {
989 finfo = (struct fd_formb *)bp->bio_data;
990 retry_line = __LINE__;
993 head << 2 | fd->fdsu,
994 finfo->fd_formb_secshift,
995 finfo->fd_formb_nsecs,
996 finfo->fd_formb_gaplen,
997 finfo->fd_formb_fillbyte, 0))
1001 retry_line = __LINE__;
1003 NE7CMD_READID | mfm,
1004 head << 2 | fd->fdsu, 0))
1008 retry_line = __LINE__;
1010 NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT,
1011 head << 2 | fd->fdsu, /* head & unit */
1012 fd->track, /* track */
1014 sec, /* sector + 1 */
1015 fd->ft->secsize, /* sector size */
1016 fd->ft->sectrac, /* sectors/track */
1017 fd->ft->gap, /* gap size */
1018 fd->ft->datalen, /* data length */
1023 retry_line = __LINE__;
1025 NE7CMD_WRITE | mfm | NE7CMD_MT,
1026 head << 2 | fd->fdsu, /* head & unit */
1027 fd->track, /* track */
1029 sec, /* sector + 1 */
1030 fd->ft->secsize, /* sector size */
1031 fd->ft->sectrac, /* sectors/track */
1032 fd->ft->gap, /* gap size */
1033 fd->ft->datalen, /* data length */
1038 KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd));
1041 /* Wait for interrupt */
1042 i = tsleep(fdc, PRIBIO, "fddata", hz);
1044 /* PIO if the read looks good */
1045 if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd == BIO_READ))
1049 if (fd->flags & FD_ISADMA) {
1051 bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
1052 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
1053 mtx_lock(&fdc->fdc_mtx);
1054 fd->flags &= ~FD_ISADMA;
1055 mtx_unlock(&fdc->fdc_mtx);
1062 * Due to IBM's brain-dead design, the FDC has a faked ready
1063 * signal, hardwired to ready == true. Thus, any command
1064 * issued if there's no diskette in the drive will _never_
1065 * complete, and must be aborted by resetting the FDC.
1066 * Many thanks, Big Blue!
1068 retry_line = __LINE__;
1069 fdc->flags |= FDC_NEEDS_RESET;
1073 retry_line = __LINE__;
1074 if (fdc_read_status(fdc))
1077 if (debugflags & 0x10)
1078 printf(" -> %x %x %x %x\n",
1079 fdc->status[0], fdc->status[1],
1080 fdc->status[2], fdc->status[3]);
1082 st0 = fdc->status[0] & NE7_ST0_IC;
1084 retry_line = __LINE__;
1085 if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) {
1087 * DMA overrun. Someone hogged the bus and
1088 * didn't release it in time for the next
1093 retry_line = __LINE__;
1094 if(st0 == NE7_ST0_IC_IV) {
1095 fdc->flags |= FDC_NEEDS_RESET;
1098 retry_line = __LINE__;
1099 if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) {
1100 need_recal |= (1 << fd->fdsu);
1103 if (debugflags & 0x20) {
1104 printf("status %02x %02x %02x %02x %02x %02x\n",
1105 fdc->status[0], fdc->status[1], fdc->status[2],
1106 fdc->status[3], fdc->status[4], fdc->status[5]);
1108 retry_line = __LINE__;
1109 if (fd->options & FDOPT_NOERROR)
1115 switch(bp->bio_cmd) {
1117 /* copy out ID field contents */
1118 idp = (struct fdc_readid *)bp->bio_data;
1119 idp->cyl = fdc->status[3];
1120 idp->head = fdc->status[4];
1121 idp->sec = fdc->status[5];
1122 idp->secshift = fdc->status[6];
1123 if (debugflags & 0x40)
1124 printf("c %d h %d s %d z %d\n",
1125 idp->cyl, idp->head, idp->sec, idp->secshift);
1129 bp->bio_pblkno += nsect;
1130 bp->bio_resid -= fd->fd_iosize;
1131 bp->bio_completed += fd->fd_iosize;
1132 fd->fd_ioptr += fd->fd_iosize;
1133 if (override_error) {
1134 if ((debugflags & 4))
1135 printf("FDOPT_NOERROR: returning bad data\n");
1137 /* Since we managed to get something done,
1138 * reset the retry */
1140 if (bp->bio_resid > 0)
1147 return (fdc_biodone(fdc, 0));
1151 fdc_thread(void *arg)
1153 struct fdc_data *fdc;
1158 mtx_lock(&fdc->fdc_mtx);
1159 fdc->flags |= FDC_KTHREAD_ALIVE;
1160 while ((fdc->flags & FDC_KTHREAD_EXIT) == 0) {
1161 mtx_unlock(&fdc->fdc_mtx);
1162 i = fdc_worker(fdc);
1163 if (i && debugflags & 0x20) {
1164 if (fdc->bp != NULL)
1165 g_print_bio("", fdc->bp, "");
1166 printf("Retry line %d\n", retry_line);
1169 mtx_lock(&fdc->fdc_mtx);
1171 fdc->flags &= ~(FDC_KTHREAD_EXIT | FDC_KTHREAD_ALIVE);
1172 mtx_unlock(&fdc->fdc_mtx);
1178 * Enqueue a request.
1181 fd_enqueue(struct fd_data *fd, struct bio *bp)
1183 struct fdc_data *fdc;
1188 mtx_lock(&fdc->fdc_mtx);
1189 /* If we go from idle, cancel motor turnoff */
1190 if (fd->fd_iocount++ == 0)
1191 callout_stop(&fd->toffhandle);
1192 if (fd->flags & FD_MOTOR) {
1193 /* The motor is on, send it directly to the controller */
1194 bioq_disksort(&fdc->head, bp);
1197 /* Queue it on the drive until the motor has started */
1198 bioq_insert_tail(&fd->fd_bq, bp);
1199 if (!(fd->flags & FD_MOTORWAIT))
1202 mtx_unlock(&fdc->fdc_mtx);
1206 * Try to find out if we have a disk in the drive.
1209 fd_probe_disk(struct fd_data *fd, int *recal)
1211 struct fdc_data *fdc;
1216 oopts = fd->options;
1217 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1221 * First recal, then seek to cyl#1, this clears the old condition on
1222 * the disk change line so we can examine it for current status.
1224 if (debugflags & 0x40)
1225 printf("New disk in probe\n");
1226 mtx_lock(&fdc->fdc_mtx);
1227 fd->flags |= FD_NEWDISK;
1228 mtx_unlock(&fdc->fdc_mtx);
1229 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
1231 tsleep(fdc, PRIBIO, "fdrecal", hz);
1232 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1233 goto done; /* XXX */
1234 if ((st0 & 0xc0) || cyl != 0)
1237 /* Seek to track 1 */
1238 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0))
1240 tsleep(fdc, PRIBIO, "fdseek", hz);
1241 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1242 goto done; /* XXX */
1243 *recal |= (1 << fd->fdsu);
1244 if (fdin_rd(fdc) & FDI_DCHG) {
1245 if (debugflags & 0x40)
1246 printf("Empty in probe\n");
1247 mtx_lock(&fdc->fdc_mtx);
1248 fd->flags |= FD_EMPTY;
1249 mtx_unlock(&fdc->fdc_mtx);
1251 if (fdc_sense_drive(fdc, &st3) != 0)
1253 if (debugflags & 0x40)
1254 printf("Got disk in probe\n");
1255 mtx_lock(&fdc->fdc_mtx);
1256 fd->flags &= ~FD_EMPTY;
1257 if (st3 & NE7_ST3_WP)
1260 fd->flags &= ~FD_WP;
1261 mtx_unlock(&fdc->fdc_mtx);
1266 fd->options = oopts;
1271 fdmisccmd(struct fd_data *fd, u_int cmd, void *data)
1274 struct fd_formb *finfo;
1275 struct fdc_readid *idfield;
1278 bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
1281 * Set up a bio request for fdstrategy(). bio_offset is faked
1282 * so that fdstrategy() will seek to the requested
1283 * cylinder, and use the desired head.
1286 if (cmd == BIO_FMT) {
1287 finfo = (struct fd_formb *)data;
1289 (finfo->cyl * fd->ft->heads + finfo->head) *
1291 bp->bio_length = sizeof *finfo;
1292 } else if (cmd == BIO_RDID) {
1293 idfield = (struct fdc_readid *)data;
1295 (idfield->cyl * fd->ft->heads + idfield->head) *
1297 bp->bio_length = sizeof(struct fdc_readid);
1298 } else if (cmd == BIO_PROBE) {
1301 panic("wrong cmd in fdmisccmd()");
1302 bp->bio_offset = bp->bio_pblkno * fd->sectorsize;
1303 bp->bio_data = data;
1304 bp->bio_driver1 = fd;
1310 tsleep(bp, PRIBIO, "fdwait", hz);
1311 } while (!(bp->bio_flags & BIO_DONE));
1312 error = bp->bio_error;
1319 * Try figuring out the density of the media present in our device.
1322 fdautoselect(struct fd_data *fd)
1324 struct fd_type *fdtp;
1325 struct fdc_readid id;
1328 if (!(fd->ft->flags & FL_AUTO))
1331 fdtp = fd_native_types[fd->type];
1332 fdsettype(fd, fdtp);
1333 if (!(fd->ft->flags & FL_AUTO))
1337 * Try reading sector ID fields, first at cylinder 0, head 0,
1338 * then at cylinder 2, head N. We don't probe cylinder 1,
1339 * since for 5.25in DD media in a HD drive, there are no data
1340 * to read (2 step pulses per media cylinder required). For
1341 * two-sided media, the second probe always goes to head 1, so
1342 * we can tell them apart from single-sided media. As a
1343 * side-effect this means that single-sided media should be
1344 * mentioned in the search list after two-sided media of an
1345 * otherwise identical density. Media with a different number
1346 * of sectors per track but otherwise identical parameters
1347 * cannot be distinguished at all.
1349 * If we successfully read an ID field on both cylinders where
1350 * the recorded values match our expectation, we are done.
1351 * Otherwise, we try the next density entry from the table.
1353 * Stepping to cylinder 2 has the side-effect of clearing the
1354 * unit attention bit.
1356 oopts = fd->options;
1357 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1358 for (; fdtp->heads; fdtp++) {
1359 fdsettype(fd, fdtp);
1361 id.cyl = id.head = 0;
1362 rv = fdmisccmd(fd, BIO_RDID, &id);
1365 if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize)
1368 id.head = fd->ft->heads - 1;
1369 rv = fdmisccmd(fd, BIO_RDID, &id);
1370 if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
1371 id.secshift != fdtp->secsize)
1377 fd->options = oopts;
1378 if (fdtp->heads == 0) {
1379 if (debugflags & 0x40)
1380 device_printf(fd->dev, "autoselection failed\n");
1381 fdsettype(fd, fd_native_types[fd->type]);
1384 if (debugflags & 0x40) {
1385 device_printf(fd->dev,
1386 "autoselected %d KB medium\n",
1388 fdprinttype(fd->ft);
1395 * GEOM class implementation
1398 static g_access_t fd_access;
1399 static g_start_t fd_start;
1400 static g_ioctl_t fd_ioctl;
1402 struct g_class g_fd_class = {
1404 .version = G_VERSION,
1406 .access = fd_access,
1411 fd_access(struct g_provider *pp, int r, int w, int e)
1414 struct fdc_data *fdc;
1418 fd = pp->geom->softc;
1422 * If our provider is withering, we can only get negative requests
1423 * and we don't want to even see them
1425 if (pp->flags & G_PF_WITHER)
1432 if (ar == 0 && aw == 0 && ae == 0) {
1433 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
1434 device_unbusy(fd->dev);
1439 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
1440 if (fdmisccmd(fd, BIO_PROBE, NULL))
1442 if (fd->flags & FD_EMPTY)
1444 if (fd->flags & FD_NEWDISK) {
1445 if (fdautoselect(fd) != 0 &&
1446 (device_get_flags(fd->dev) & FD_NO_CHLINE)) {
1447 mtx_lock(&fdc->fdc_mtx);
1448 fd->flags |= FD_EMPTY;
1449 mtx_unlock(&fdc->fdc_mtx);
1452 mtx_lock(&fdc->fdc_mtx);
1453 fd->flags &= ~FD_NEWDISK;
1454 mtx_unlock(&fdc->fdc_mtx);
1456 device_busy(fd->dev);
1460 if (w > 0 && (fd->flags & FD_WP)) {
1462 device_unbusy(fd->dev);
1466 pp->sectorsize = fd->sectorsize;
1467 pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
1468 pp->mediasize = pp->stripesize * fd->ft->tracks;
1473 fd_start(struct bio *bp)
1475 struct fdc_data * fdc;
1476 struct fd_data * fd;
1478 fd = bp->bio_to->geom->softc;
1480 bp->bio_driver1 = fd;
1481 if (bp->bio_cmd == BIO_GETATTR) {
1482 if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
1484 if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
1486 g_io_deliver(bp, ENOIOCTL);
1489 if (!(bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
1490 g_io_deliver(bp, EOPNOTSUPP);
1493 bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
1494 bp->bio_resid = bp->bio_length;
1500 fd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
1503 struct fdc_status *fsp;
1504 struct fdc_readid *rid;
1507 fd = pp->geom->softc;
1510 case FD_GTYPE: /* get drive type */
1511 *(struct fd_type *)data = *fd->ft;
1514 case FD_STYPE: /* set drive type */
1516 * Allow setting drive type temporarily iff
1517 * currently unset. Used for fdformat so any
1518 * user can set it, and then start formatting.
1520 fd->fts = *(struct fd_type *)data;
1521 if (fd->fts.sectrac) {
1522 /* XXX: check for rubbish */
1523 fdsettype(fd, &fd->fts);
1525 fdsettype(fd, fd_native_types[fd->type]);
1527 if (debugflags & 0x40)
1528 fdprinttype(fd->ft);
1531 case FD_GOPTS: /* get drive options */
1532 *(int *)data = fd->options;
1535 case FD_SOPTS: /* set drive options */
1536 fd->options = *(int *)data;
1540 error = priv_check(td, PRIV_DRIVER);
1543 fd->fdc->fdc_errs = 0;
1547 fsp = (struct fdc_status *)data;
1548 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
1550 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
1554 *(enum fd_drivetype *)data = fd->type;
1558 if (!(fflag & FWRITE))
1560 if (((struct fd_formb *)data)->format_version !=
1562 return (EINVAL); /* wrong version of formatting prog */
1563 error = fdmisccmd(fd, BIO_FMT, data);
1564 mtx_lock(&fd->fdc->fdc_mtx);
1565 fd->flags |= FD_NEWDISK;
1566 mtx_unlock(&fd->fdc->fdc_mtx);
1570 rid = (struct fdc_readid *)data;
1571 if (rid->cyl > 85 || rid->head > 1)
1573 error = fdmisccmd(fd, BIO_RDID, data);
1578 /* For backwards compat with old fd*(8) tools */
1583 if (debugflags & 0x80)
1584 printf("Unknown ioctl %lx\n", cmd);
1594 * Configuration/initialization stuff, per controller.
1597 devclass_t fdc_devclass;
1598 static devclass_t fd_devclass;
1606 fdc_release_resources(struct fdc_data *fdc)
1609 struct resource *last;
1614 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1615 fdc->fdc_intr = NULL;
1616 if (fdc->res_irq != NULL)
1617 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
1619 fdc->res_irq = NULL;
1621 for (i = 0; i < FDC_MAXREG; i++) {
1622 if (fdc->resio[i] != NULL && fdc->resio[i] != last) {
1623 bus_release_resource(dev, SYS_RES_IOPORT,
1624 fdc->ridio[i], fdc->resio[i]);
1625 last = fdc->resio[i];
1626 fdc->resio[i] = NULL;
1629 if (fdc->res_drq != NULL)
1630 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
1632 fdc->res_drq = NULL;
1636 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1638 struct fdc_ivars *ivars = device_get_ivars(child);
1641 case FDC_IVAR_FDUNIT:
1642 *result = ivars->fdunit;
1644 case FDC_IVAR_FDTYPE:
1645 *result = ivars->fdtype;
1654 fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1656 struct fdc_ivars *ivars = device_get_ivars(child);
1659 case FDC_IVAR_FDUNIT:
1660 ivars->fdunit = value;
1662 case FDC_IVAR_FDTYPE:
1663 ivars->fdtype = value;
1672 fdc_initial_reset(device_t dev, struct fdc_data *fdc)
1674 int ic_type, part_id;
1677 * A status value of 0xff is very unlikely, but not theoretically
1678 * impossible, but it is far more likely to indicate an empty bus.
1680 if (fdsts_rd(fdc) == 0xff)
1684 * Assert a reset to the floppy controller and check that the status
1685 * register goes to zero.
1689 if (fdsts_rd(fdc) != 0)
1693 * Clear the reset and see it come ready.
1695 fdout_wr(fdc, FDO_FRST);
1697 if (fdsts_rd(fdc) != 0x80)
1700 /* Then, see if it can handle a command. */
1701 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(6, 240),
1702 NE7_SPEC_2(31, 0), 0))
1706 * Try to identify the chip.
1708 * The i8272 datasheet documents that unknown commands
1709 * will return ST0 as 0x80. The i8272 is supposedly identical
1711 * The i82077SL datasheet says 0x90 for the VERSION command,
1712 * and several "superio" chips emulate this.
1714 if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
1716 if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
1720 "ic_type %02x part_id %02x\n", ic_type, part_id);
1721 switch (ic_type & 0xff) {
1723 device_set_desc(dev, "NEC 765 or clone");
1724 fdc->fdct = FDC_NE765;
1728 device_set_desc(dev,
1729 "Enhanced floppy controller");
1730 fdc->fdct = FDC_ENHANCED;
1733 device_set_desc(dev, "Generic floppy controller");
1734 fdc->fdct = FDC_UNKNOWN;
1741 fdc_detach(device_t dev)
1743 struct fdc_data *fdc;
1746 fdc = device_get_softc(dev);
1748 /* have our children detached first */
1749 if ((error = bus_generic_detach(dev)))
1753 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1754 fdc->fdc_intr = NULL;
1756 /* kill worker thread */
1757 mtx_lock(&fdc->fdc_mtx);
1758 fdc->flags |= FDC_KTHREAD_EXIT;
1760 while ((fdc->flags & FDC_KTHREAD_ALIVE) != 0)
1761 msleep(fdc->fdc_thread, &fdc->fdc_mtx, PRIBIO, "fdcdet", 0);
1762 mtx_unlock(&fdc->fdc_mtx);
1764 /* reset controller, turn motor off */
1767 if (!(fdc->flags & FDC_NODMA))
1768 isa_dma_release(fdc->dmachan);
1769 fdc_release_resources(fdc);
1770 mtx_destroy(&fdc->fdc_mtx);
1775 * Add a child device to the fdc controller. It will then be probed etc.
1778 fdc_add_child(device_t dev, const char *name, int unit)
1780 struct fdc_ivars *ivar;
1783 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
1786 child = device_add_child(dev, name, unit);
1787 if (child == NULL) {
1788 free(ivar, M_DEVBUF);
1791 device_set_ivars(child, ivar);
1792 ivar->fdunit = unit;
1793 ivar->fdtype = FDT_NONE;
1794 if (resource_disabled(name, unit))
1795 device_disable(child);
1800 fdc_attach(device_t dev)
1802 struct fdc_data *fdc;
1805 fdc = device_get_softc(dev);
1807 error = fdc_initial_reset(dev, fdc);
1809 device_printf(dev, "does not respond\n");
1812 error = bus_setup_intr(dev, fdc->res_irq,
1813 INTR_TYPE_BIO | INTR_ENTROPY |
1814 ((fdc->flags & FDC_NOFAST) ? INTR_MPSAFE : 0),
1815 ((fdc->flags & FDC_NOFAST) ? NULL : fdc_intr_fast),
1816 ((fdc->flags & FDC_NOFAST) ? fdc_intr : NULL),
1817 fdc, &fdc->fdc_intr);
1819 device_printf(dev, "cannot setup interrupt\n");
1822 if (!(fdc->flags & FDC_NODMA)) {
1823 error = isa_dma_acquire(fdc->dmachan);
1825 error = isa_dma_init(fdc->dmachan,
1826 MAX_BYTES_PER_CYL, M_WAITOK);
1828 isa_dma_release(fdc->dmachan);
1833 fdc->fdcu = device_get_unit(dev);
1834 fdc->flags |= FDC_NEEDS_RESET;
1836 mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
1838 /* reset controller, turn motor off, clear fdout mirror reg */
1839 fdout_wr(fdc, fdc->fdout = 0);
1840 bioq_init(&fdc->head);
1848 fdc_start_worker(device_t dev)
1850 struct fdc_data *fdc;
1852 fdc = device_get_softc(dev);
1853 kproc_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
1854 "fdc%d", device_get_unit(dev));
1858 fdc_hints_probe(device_t dev)
1860 const char *name, *dname;
1861 int i, error, dunit;
1864 * Probe and attach any children. We should probably detect
1865 * devices from the BIOS unless overridden.
1867 name = device_get_nameunit(dev);
1869 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
1870 resource_int_value(dname, dunit, "drive", &dunit);
1871 fdc_add_child(dev, dname, dunit);
1874 if ((error = bus_generic_attach(dev)) != 0)
1880 fdc_print_child(device_t me, device_t child)
1882 int retval = 0, flags;
1884 retval += bus_print_child_header(me, child);
1885 retval += printf(" on %s drive %d", device_get_nameunit(me),
1886 fdc_get_fdunit(child));
1887 if ((flags = device_get_flags(me)) != 0)
1888 retval += printf(" flags %#x", flags);
1889 retval += printf("\n");
1895 * Configuration/initialization, per drive.
1898 fd_probe(device_t dev)
1904 struct fdc_data *fdc;
1908 fdsu = fdc_get_fdunit(dev);
1909 fd = device_get_softc(dev);
1910 fdc = device_get_softc(device_get_parent(dev));
1911 flags = device_get_flags(dev);
1916 unit = device_get_unit(dev);
1918 /* Auto-probe if fdinfo is present, but always allow override. */
1919 type = flags & FD_TYPEMASK;
1920 if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
1924 /* make sure fdautoselect() will be called */
1925 fd->flags = FD_EMPTY;
1929 #if defined(__i386__) || defined(__amd64__)
1930 if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
1931 /* Look up what the BIOS thinks we have. */
1933 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
1935 fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
1936 if (fd->type == FDT_288M_1)
1937 fd->type = FDT_288M;
1939 #endif /* __i386__ || __amd64__ */
1940 /* is there a unit? */
1941 if (fd->type == FDT_NONE)
1944 mtx_lock(&fdc->fdc_mtx);
1950 fdc_reset(fdc); /* XXX reset, then unreset, etc. */
1951 DELAY(1000000); /* 1 sec */
1953 if ((flags & FD_NO_PROBE) == 0) {
1954 /* If we're at track 0 first seek inwards. */
1955 if ((fdc_sense_drive(fdc, &st3) == 0) &&
1956 (st3 & NE7_ST3_T0)) {
1957 /* Seek some steps... */
1958 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1959 /* ...wait a moment... */
1961 /* make ctrlr happy: */
1962 fdc_sense_int(fdc, NULL, NULL);
1966 for (i = 0; i < 2; i++) {
1968 * we must recalibrate twice, just in case the
1969 * heads have been beyond cylinder 76, since
1970 * most FDCs still barf when attempting to
1971 * recalibrate more than 77 steps
1974 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1975 /* a second being enough for full stroke seek*/
1976 DELAY(i == 0 ? 1000000 : 300000);
1978 /* anything responding? */
1979 if (fdc_sense_int(fdc, &st0, NULL) == 0 &&
1980 (st0 & NE7_ST0_EC) == 0)
1981 break; /* already probed successfully */
1988 mtx_unlock(&fdc->fdc_mtx);
1990 if ((flags & FD_NO_PROBE) == 0 &&
1991 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
1998 device_set_desc(dev, "1200-KB 5.25\" drive");
2001 device_set_desc(dev, "1440-KB 3.5\" drive");
2004 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
2007 device_set_desc(dev, "360-KB 5.25\" drive");
2010 device_set_desc(dev, "720-KB 3.5\" drive");
2015 fd->track = FD_NO_TRACK;
2019 callout_init_mtx(&fd->toffhandle, &fd->fdc->fdc_mtx, 0);
2021 /* initialize densities for subdevices */
2022 fdsettype(fd, fd_native_types[fd->type]);
2027 * We have to do this in a geom event because GEOM is not running
2028 * when fd_attach() is.
2029 * XXX: move fd_attach after geom like ata/scsi disks
2032 fd_attach2(void *arg, int flag)
2038 fd->fd_geom = g_new_geomf(&g_fd_class,
2039 "fd%d", device_get_unit(fd->dev));
2040 fd->fd_provider = g_new_providerf(fd->fd_geom, "%s", fd->fd_geom->name);
2041 fd->fd_geom->softc = fd;
2042 g_error_provider(fd->fd_provider, 0);
2046 fd_attach(device_t dev)
2050 fd = device_get_softc(dev);
2051 g_post_event(fd_attach2, fd, M_WAITOK, NULL);
2052 fd->flags |= FD_EMPTY;
2053 bioq_init(&fd->fd_bq);
2059 fd_detach_geom(void *arg, int flag)
2061 struct fd_data *fd = arg;
2063 g_topology_assert();
2064 g_wither_geom(fd->fd_geom, ENXIO);
2068 fd_detach(device_t dev)
2072 fd = device_get_softc(dev);
2073 g_waitfor_event(fd_detach_geom, fd, M_WAITOK, NULL);
2074 while (device_get_state(dev) == DS_BUSY)
2075 tsleep(fd, PZERO, "fdd", hz/10);
2076 callout_drain(&fd->toffhandle);
2081 static device_method_t fd_methods[] = {
2082 /* Device interface */
2083 DEVMETHOD(device_probe, fd_probe),
2084 DEVMETHOD(device_attach, fd_attach),
2085 DEVMETHOD(device_detach, fd_detach),
2086 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2087 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
2088 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
2092 static driver_t fd_driver = {
2095 sizeof(struct fd_data)
2099 fdc_modevent(module_t mod, int type, void *data)
2102 return (g_modevent(NULL, type, &g_fd_class));
2105 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, fdc_modevent, 0);