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>
85 #include <isa/isareg.h>
86 #include <dev/fdc/fdcvar.h>
89 #include <dev/ic/nec765.h>
92 * Runtime configuration hints/flags
95 /* configuration flags for fd */
96 #define FD_TYPEMASK 0x0f /* drive type, matches enum
97 * fd_drivetype; on i386 machines, if
98 * given as 0, use RTC type for fd0
100 #define FD_NO_CHLINE 0x10 /* drive does not support changeline
101 * aka. unit attention */
102 #define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just
103 * assume it is there */
106 * Things that could conceiveably considered parameters or tweakables
110 * Maximal number of bytes in a cylinder.
111 * This is used for ISADMA bouncebuffer allocation and sets the max
112 * xfersize we support.
114 * 2.88M format has 2 x 36 x 512, allow for hacked up density.
116 #define MAX_BYTES_PER_CYL (2 * 40 * 512)
119 * Timeout value for the PIO loops to wait until the FDC main status
120 * register matches our expectations (request for master, direction
121 * bit). This is supposed to be a number of microseconds, although
122 * timing might actually not be very accurate.
124 * Timeouts of 100 msec are believed to be required for some broken
127 #define FDSTS_TIMEOUT 100000
130 * After this many errors, stop whining. Close will reset this count.
132 #define FDC_ERRMAX 100
135 * AutoDensity search lists for each drive type.
138 static struct fd_type fd_searchlist_360k[] = {
143 static struct fd_type fd_searchlist_12m[] = {
144 { FDF_5_1200 | FL_AUTO },
145 { FDF_5_360 | FL_2STEP | FL_AUTO},
149 static struct fd_type fd_searchlist_720k[] = {
154 static struct fd_type fd_searchlist_144m[] = {
155 { FDF_3_1440 | FL_AUTO},
156 { FDF_3_720 | FL_AUTO},
160 static struct fd_type fd_searchlist_288m[] = {
161 { FDF_3_1440 | FL_AUTO },
163 { FDF_3_2880 | FL_AUTO }, /* XXX: probably doesn't work */
165 { FDF_3_720 | FL_AUTO},
170 * Order must match enum fd_drivetype in <sys/fdcio.h>.
172 static struct fd_type *fd_native_types[] = {
174 fd_searchlist_360k, /* FDT_360K */
175 fd_searchlist_12m, /* FDT_12M */
176 fd_searchlist_720k, /* FDT_720K */
177 fd_searchlist_144m, /* FDT_144M */
178 fd_searchlist_288m, /* FDT_288M_1 (mapped to FDT_288M) */
179 fd_searchlist_288m, /* FDT_288M */
183 * Internals start here
187 #define FDOUT 2 /* Digital Output Register (W) */
188 #define FDO_FDSEL 0x03 /* floppy device select */
189 #define FDO_FRST 0x04 /* floppy controller reset */
190 #define FDO_FDMAEN 0x08 /* enable floppy DMA and Interrupt */
191 #define FDO_MOEN0 0x10 /* motor enable drive 0 */
192 #define FDO_MOEN1 0x20 /* motor enable drive 1 */
193 #define FDO_MOEN2 0x40 /* motor enable drive 2 */
194 #define FDO_MOEN3 0x80 /* motor enable drive 3 */
196 #define FDSTS 4 /* NEC 765 Main Status Register (R) */
197 #define FDDSR 4 /* Data Rate Select Register (W) */
198 #define FDDATA 5 /* NEC 765 Data Register (R/W) */
199 #define FDCTL 7 /* Control Register (W) */
202 * The YE-DATA PC Card floppies use PIO to read in the data rather
203 * than DMA due to the wild variability of DMA for the PC Card
204 * devices. DMA was deleted from the PC Card specification in version
205 * 7.2 of the standard, but that post-dates the YE-DATA devices by many
208 * In addition, if we cannot setup the DMA resources for the ISA
209 * attachment, we'll use this same offset for data transfer. However,
210 * that almost certainly won't work.
212 * For this mode, offset 0 and 1 must be used to setup the transfer
213 * for this floppy. This is OK for PC Card YE Data devices, but for
214 * ISA this is likely wrong. These registers are only available on
215 * those systems that map them to the floppy drive. Newer systems do
216 * not do this, and we should likely prohibit access to them (or
217 * disallow NODMA to be set).
219 #define FDBCDR 0 /* And 1 */
220 #define FD_YE_DATAPORT 6 /* Drive Data port */
222 #define FDI_DCHG 0x80 /* diskette has been changed */
223 /* requires drive and motor being selected */
224 /* is cleared by any step pulse to drive */
227 * We have three private BIO commands.
229 #define BIO_PROBE BIO_CMD0
230 #define BIO_RDID BIO_CMD1
231 #define BIO_FMT BIO_CMD2
234 * Per drive structure (softc).
237 u_char *fd_ioptr; /* IO pointer */
238 u_int fd_iosize; /* Size of IO chunks */
239 u_int fd_iocount; /* Outstanding requests */
240 struct fdc_data *fdc; /* pointer to controller structure */
241 int fdsu; /* this units number on this controller */
242 enum fd_drivetype type; /* drive type */
243 struct fd_type *ft; /* pointer to current type descriptor */
244 struct fd_type fts; /* type descriptors */
247 #define FD_WP (1<<0) /* Write protected */
248 #define FD_MOTOR (1<<1) /* motor should be on */
249 #define FD_MOTORWAIT (1<<2) /* motor should be on */
250 #define FD_EMPTY (1<<3) /* no media */
251 #define FD_NEWDISK (1<<4) /* media changed */
252 #define FD_ISADMA (1<<5) /* isa dma started */
253 int track; /* where we think the head is */
254 #define FD_NO_TRACK -2
255 int options; /* FDOPT_* */
256 struct callout toffhandle;
257 struct g_geom *fd_geom;
258 struct g_provider *fd_provider;
260 struct bio_queue_head fd_bq;
263 #define FD_NOT_VALID -2
265 static driver_intr_t fdc_intr;
266 static driver_filter_t fdc_intr_fast;
267 static void fdc_reset(struct fdc_data *);
268 static int fd_probe_disk(struct fd_data *, int *);
270 static SYSCTL_NODE(_debug, OID_AUTO, fdc, CTLFLAG_RW, 0, "fdc driver");
272 static int fifo_threshold = 8;
273 SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0,
274 "FIFO threshold setting");
276 static int debugflags = 0;
277 SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0,
280 static int retries = 10;
281 SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0,
282 "Number of retries to attempt");
284 static int spec1 = 0xaf;
285 SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0,
286 "Specification byte one (step-rate + head unload)");
288 static int spec2 = 0x10;
289 SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0,
290 "Specification byte two (head load time + no-dma)");
293 SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0,
294 "Head settling time in sec/hz");
297 fdprinttype(struct fd_type *ft)
300 printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)",
301 ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks,
302 ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter,
303 ft->offset_side2, ft->flags);
307 fdsettype(struct fd_data *fd, struct fd_type *ft)
310 ft->size = ft->sectrac * ft->heads * ft->tracks;
311 fd->sectorsize = 128 << fd->ft->secsize;
315 * Bus space handling (access to low-level IO).
318 fdregwr(struct fdc_data *fdc, int reg, uint8_t v)
321 bus_space_write_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg], v);
324 static inline uint8_t
325 fdregrd(struct fdc_data *fdc, int reg)
328 return bus_space_read_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg]);
332 fdctl_wr(struct fdc_data *fdc, u_int8_t v)
335 fdregwr(fdc, FDCTL, v);
339 fdout_wr(struct fdc_data *fdc, u_int8_t v)
342 fdregwr(fdc, FDOUT, v);
346 fdsts_rd(struct fdc_data *fdc)
349 return fdregrd(fdc, FDSTS);
353 fddsr_wr(struct fdc_data *fdc, u_int8_t v)
356 fdregwr(fdc, FDDSR, v);
360 fddata_wr(struct fdc_data *fdc, u_int8_t v)
363 fdregwr(fdc, FDDATA, v);
367 fddata_rd(struct fdc_data *fdc)
370 return fdregrd(fdc, FDDATA);
374 fdin_rd(struct fdc_data *fdc)
377 return fdregrd(fdc, FDCTL);
381 * Magic pseudo-DMA initialization for YE FDC. Sets count and
385 fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count)
387 fdregwr(fdc, FDBCDR, (count - 1) & 0xff);
388 fdregwr(fdc, FDBCDR + 1,
389 (iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f));
393 fdc_err(struct fdc_data *fdc, const char *s)
397 if (fdc->fdc_errs < FDC_ERRMAX)
398 device_printf(fdc->fdc_dev, "%s", s);
399 else if (fdc->fdc_errs == FDC_ERRMAX)
400 device_printf(fdc->fdc_dev, "too many errors, not "
401 "logging any more\n");
408 * FDC IO functions, take care of the main status register, timeout
409 * in case the desired status bits are never set.
411 * These PIO loops initially start out with short delays between
412 * each iteration in the expectation that the required condition
413 * is usually met quickly, so it can be handled immediately.
416 fdc_in(struct fdc_data *fdc, int *ptr)
421 for (j = 0; j < FDSTS_TIMEOUT; j += step) {
422 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
423 if (i == (NE7_DIO|NE7_RQM)) {
430 return (fdc_err(fdc, "ready for output in input\n"));
434 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
438 fdc_out(struct fdc_data *fdc, int x)
443 for (j = 0; j < FDSTS_TIMEOUT; j += step) {
444 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
449 if (i == (NE7_DIO|NE7_RQM))
450 return (fdc_err(fdc, "ready for input in output\n"));
454 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
458 * fdc_cmd: Send a command to the chip.
459 * Takes a varargs with this structure:
461 * output bytes as int [...]
463 * input bytes as int* [...]
466 fdc_cmd(struct fdc_data *fdc, int n_out, ...)
474 for (n = 0; n < n_out; n++) {
478 if (fdc_out(fdc, i) < 0) {
480 snprintf(msg, sizeof(msg),
481 "cmd %x failed at out byte %d of %d\n",
483 fdc->flags |= FDC_NEEDS_RESET;
485 return fdc_err(fdc, msg);
488 n_in = va_arg(ap, int);
489 for (n = 0; n < n_in; n++) {
490 int *ptr = va_arg(ap, int *);
491 if (fdc_in(fdc, ptr) < 0) {
493 snprintf(msg, sizeof(msg),
494 "cmd %02x failed at in byte %d of %d\n",
496 fdc->flags |= FDC_NEEDS_RESET;
498 return fdc_err(fdc, msg);
506 fdc_reset(struct fdc_data *fdc)
510 if (fdc->fdct == FDC_ENHANCED) {
511 /* Try a software reset, default precomp, and 500 kb/s */
512 fddsr_wr(fdc, I8207X_DSR_SR);
514 /* Try a hardware reset, keep motor on */
515 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
517 /* enable FDC, but defer interrupts a moment */
518 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
521 fdout_wr(fdc, fdc->fdout);
523 /* XXX after a reset, silently believe the FDC will accept commands */
524 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
525 device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
527 if (fdc->fdct == FDC_ENHANCED) {
531 /* 0x40 | */ /* Enable Implied Seek -
533 0x10 | /* Polling disabled */
534 (fifo_threshold - 1), /* Fifo threshold */
535 0x00, /* Precomp track */
537 device_printf(fdc->fdc_dev,
538 " CONFIGURE failed in reset\n");
539 if (debugflags & 1) {
542 10, &r[0], &r[1], &r[2], &r[3], &r[4],
543 &r[5], &r[6], &r[7], &r[8], &r[9]))
544 device_printf(fdc->fdc_dev,
545 " DUMPREG failed in reset\n");
546 for (i = 0; i < 10; i++)
547 printf(" %02x", r[i]);
554 fdc_sense_drive(struct fdc_data *fdc, int *st3p)
558 if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
559 return (fdc_err(fdc, "Sense Drive Status failed\n"));
566 fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
570 ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
572 (void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
579 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
581 * There doesn't seem to have been an interrupt.
583 return (FD_NOT_VALID);
586 if (fdc_in(fdc, &cyl) < 0)
587 return fdc_err(fdc, "can't get cyl num\n");
596 fdc_read_status(struct fdc_data *fdc)
600 for (i = ret = 0; i < 7; i++) {
601 ret = fdc_in(fdc, &status);
602 fdc->status[i] = status;
608 fdc->flags |= FDC_STAT_VALID;
610 fdc->flags &= ~FDC_STAT_VALID;
619 fd_select(struct fd_data *fd)
621 struct fdc_data *fdc;
623 /* XXX: lock controller */
625 fdc->fdout &= ~FDO_FDSEL;
626 fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
627 fdout_wr(fdc, fdc->fdout);
638 mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
639 fd->flags &= ~FD_MOTORWAIT;
640 fd->flags |= FD_MOTOR;
643 bp = bioq_takefirst(&fd->fd_bq);
646 bioq_disksort(&fd->fdc->head, bp);
650 wakeup(&fd->fdc->head);
654 fd_motor(struct fd_data *fd, int turnon)
656 struct fdc_data *fdc;
660 mtx_assert(&fdc->fdc_mtx, MA_OWNED);
663 fd->flags |= FD_MOTORWAIT;
664 fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
665 callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
667 callout_stop(&fd->toffhandle);
668 fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
669 fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
671 fdout_wr(fdc, fdc->fdout);
675 fd_turnoff(void *xfd)
677 struct fd_data *fd = xfd;
679 mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
684 * fdc_intr - wake up the worker thread.
695 fdc_intr_fast(void *arg)
699 return(FILTER_HANDLED);
703 * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
706 fdc_pio(struct fdc_data *fdc)
713 cptr = fdc->fd->fd_ioptr;
714 count = fdc->fd->fd_iosize;
716 if (bp->bio_cmd == BIO_READ) {
717 fdbcdr_wr(fdc, 0, count);
718 bus_space_read_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
719 fdc->ioff[FD_YE_DATAPORT], cptr, count);
721 bus_space_write_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
722 fdc->ioff[FD_YE_DATAPORT], cptr, count);
723 fdbcdr_wr(fdc, 0, count); /* needed? */
728 fdc_biodone(struct fdc_data *fdc, int error)
736 mtx_lock(&fdc->fdc_mtx);
737 if (--fd->fd_iocount == 0)
738 callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
741 mtx_unlock(&fdc->fdc_mtx);
742 if (bp->bio_to != NULL) {
743 if ((debugflags & 2) && fd->fdc->retry > 0)
744 printf("retries: %d\n", fd->fdc->retry);
745 g_io_deliver(bp, error);
748 bp->bio_error = error;
749 bp->bio_flags |= BIO_DONE;
754 static int retry_line;
757 fdc_worker(struct fdc_data *fdc)
762 int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
765 static int need_recal;
766 struct fdc_readid *idp;
767 struct fd_formb *finfo;
771 /* Have we exhausted our retries ? */
775 (fdc->retry >= retries || (fd->options & FDOPT_NORETRY))) {
776 if ((debugflags & 4))
777 printf("Too many retries (EIO)\n");
778 if (fdc->flags & FDC_NEEDS_RESET) {
779 mtx_lock(&fdc->fdc_mtx);
780 fd->flags |= FD_EMPTY;
781 mtx_unlock(&fdc->fdc_mtx);
783 return (fdc_biodone(fdc, EIO));
786 /* Disable ISADMA if we bailed while it was active */
787 if (fd != NULL && (fd->flags & FD_ISADMA)) {
789 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
790 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
791 mtx_lock(&fdc->fdc_mtx);
792 fd->flags &= ~FD_ISADMA;
793 mtx_unlock(&fdc->fdc_mtx);
796 /* Unwedge the controller ? */
797 if (fdc->flags & FDC_NEEDS_RESET) {
798 fdc->flags &= ~FDC_NEEDS_RESET;
800 tsleep(fdc, PRIBIO, "fdcrst", hz);
801 /* Discard results */
802 for (i = 0; i < 4; i++)
803 fdc_sense_int(fdc, &st0, &cyl);
804 /* All drives must recal */
808 /* Pick up a request, if need be wait for it */
809 if (fdc->bp == NULL) {
810 mtx_lock(&fdc->fdc_mtx);
812 fdc->bp = bioq_takefirst(&fdc->head);
814 msleep(&fdc->head, &fdc->fdc_mtx,
816 } while (fdc->bp == NULL &&
817 (fdc->flags & FDC_KTHREAD_EXIT) == 0);
818 mtx_unlock(&fdc->fdc_mtx);
822 * Nothing to do, worker thread has been
828 fd = fdc->fd = bp->bio_driver1;
830 fd->fd_ioptr = bp->bio_data;
831 if (bp->bio_cmd & BIO_FMT) {
832 i = offsetof(struct fd_formb, fd_formb_cylno(0));
834 fd->fd_iosize = bp->bio_length - i;
838 /* Select drive, setup params */
840 if (fdc->fdct == FDC_ENHANCED)
841 fddsr_wr(fdc, fd->ft->trans);
843 fdctl_wr(fdc, fd->ft->trans);
845 if (bp->bio_cmd & BIO_PROBE) {
846 if ((!(device_get_flags(fd->dev) & FD_NO_CHLINE) &&
847 !(fdin_rd(fdc) & FDI_DCHG) &&
848 !(fd->flags & FD_EMPTY)) ||
849 fd_probe_disk(fd, &need_recal) == 0)
850 return (fdc_biodone(fdc, 0));
855 * If we are dead just flush the requests
857 if (fd->flags & FD_EMPTY)
858 return (fdc_biodone(fdc, ENXIO));
860 /* Check if we lost our media */
861 if (fdin_rd(fdc) & FDI_DCHG) {
862 if (debugflags & 0x40)
863 printf("Lost disk\n");
864 mtx_lock(&fdc->fdc_mtx);
865 fd->flags |= FD_EMPTY;
866 fd->flags |= FD_NEWDISK;
867 mtx_unlock(&fdc->fdc_mtx);
869 g_orphan_provider(fd->fd_provider, ENXIO);
870 fd->fd_provider->flags |= G_PF_WITHER;
872 g_new_providerf(fd->fd_geom, "%s", fd->fd_geom->name);
873 g_error_provider(fd->fd_provider, 0);
875 return (fdc_biodone(fdc, ENXIO));
878 /* Check if the floppy is write-protected */
879 if(bp->bio_cmd & (BIO_FMT | BIO_WRITE)) {
880 retry_line = __LINE__;
881 if(fdc_sense_drive(fdc, &st3) != 0)
884 return (fdc_biodone(fdc, EROFS));
887 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
888 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
889 i = fd->ft->sectrac * fd->ft->heads;
890 cylinder = bp->bio_pblkno / i;
891 descyl = cylinder * steptrac;
892 sec = bp->bio_pblkno % i;
894 head = sec / fd->ft->sectrac;
895 sec = sec % fd->ft->sectrac + 1;
897 /* If everything is going swimmingly, use multisector xfer */
898 if (fdc->retry == 0 && bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
899 fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
900 nsect = fd->fd_iosize / fd->sectorsize;
901 } else if (bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
902 fd->fd_iosize = fd->sectorsize;
906 /* Do RECAL if we need to or are going to track zero anyway */
907 if ((need_recal & (1 << fd->fdsu)) ||
908 (cylinder == 0 && fd->track != 0) ||
910 retry_line = __LINE__;
911 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
913 tsleep(fdc, PRIBIO, "fdrecal", hz);
914 retry_line = __LINE__;
915 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
916 return (1); /* XXX */
917 retry_line = __LINE__;
918 if ((st0 & 0xc0) || cyl != 0)
920 need_recal &= ~(1 << fd->fdsu);
922 /* let the heads settle */
924 tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
928 * SEEK to where we want to be
930 if (cylinder != fd->track) {
931 retry_line = __LINE__;
932 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
934 tsleep(fdc, PRIBIO, "fdseek", hz);
935 retry_line = __LINE__;
936 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
937 return (1); /* XXX */
938 retry_line = __LINE__;
939 if ((st0 & 0xc0) || cyl != descyl) {
940 need_recal |= (1 << fd->fdsu);
943 /* let the heads settle */
945 tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
947 fd->track = cylinder;
950 printf("op %x bn %ju siz %u ptr %p retry %d\n",
951 bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
952 fd->fd_ioptr, fdc->retry);
954 /* Setup ISADMA if we need it and have it */
955 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
956 && !(fdc->flags & FDC_NODMA)) {
958 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
959 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
960 mtx_lock(&fdc->fdc_mtx);
961 fd->flags |= FD_ISADMA;
962 mtx_unlock(&fdc->fdc_mtx);
965 /* Do PIO if we have to */
966 if (fdc->flags & FDC_NODMA) {
967 if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
968 fdbcdr_wr(fdc, 1, fd->fd_iosize);
969 if (bp->bio_cmd & (BIO_WRITE|BIO_FMT))
973 switch(bp->bio_cmd) {
976 finfo = (struct fd_formb *)bp->bio_data;
977 retry_line = __LINE__;
980 head << 2 | fd->fdsu,
981 finfo->fd_formb_secshift,
982 finfo->fd_formb_nsecs,
983 finfo->fd_formb_gaplen,
984 finfo->fd_formb_fillbyte, 0))
988 retry_line = __LINE__;
991 head << 2 | fd->fdsu, 0))
995 retry_line = __LINE__;
997 NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT,
998 head << 2 | fd->fdsu, /* head & unit */
999 fd->track, /* track */
1001 sec, /* sector + 1 */
1002 fd->ft->secsize, /* sector size */
1003 fd->ft->sectrac, /* sectors/track */
1004 fd->ft->gap, /* gap size */
1005 fd->ft->datalen, /* data length */
1010 retry_line = __LINE__;
1012 NE7CMD_WRITE | mfm | NE7CMD_MT,
1013 head << 2 | fd->fdsu, /* head & unit */
1014 fd->track, /* track */
1016 sec, /* sector + 1 */
1017 fd->ft->secsize, /* sector size */
1018 fd->ft->sectrac, /* sectors/track */
1019 fd->ft->gap, /* gap size */
1020 fd->ft->datalen, /* data length */
1025 KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd));
1028 /* Wait for interrupt */
1029 i = tsleep(fdc, PRIBIO, "fddata", hz);
1031 /* PIO if the read looks good */
1032 if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd & BIO_READ))
1036 if (fd->flags & FD_ISADMA) {
1038 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
1039 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
1040 mtx_lock(&fdc->fdc_mtx);
1041 fd->flags &= ~FD_ISADMA;
1042 mtx_unlock(&fdc->fdc_mtx);
1049 * Due to IBM's brain-dead design, the FDC has a faked ready
1050 * signal, hardwired to ready == true. Thus, any command
1051 * issued if there's no diskette in the drive will _never_
1052 * complete, and must be aborted by resetting the FDC.
1053 * Many thanks, Big Blue!
1055 retry_line = __LINE__;
1056 fdc->flags |= FDC_NEEDS_RESET;
1060 retry_line = __LINE__;
1061 if (fdc_read_status(fdc))
1064 if (debugflags & 0x10)
1065 printf(" -> %x %x %x %x\n",
1066 fdc->status[0], fdc->status[1],
1067 fdc->status[2], fdc->status[3]);
1069 st0 = fdc->status[0] & NE7_ST0_IC;
1071 retry_line = __LINE__;
1072 if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) {
1074 * DMA overrun. Someone hogged the bus and
1075 * didn't release it in time for the next
1080 retry_line = __LINE__;
1081 if(st0 == NE7_ST0_IC_IV) {
1082 fdc->flags |= FDC_NEEDS_RESET;
1085 retry_line = __LINE__;
1086 if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) {
1087 need_recal |= (1 << fd->fdsu);
1090 if (debugflags & 0x20) {
1091 printf("status %02x %02x %02x %02x %02x %02x\n",
1092 fdc->status[0], fdc->status[1], fdc->status[2],
1093 fdc->status[3], fdc->status[4], fdc->status[5]);
1095 retry_line = __LINE__;
1096 if (fd->options & FDOPT_NOERROR)
1102 switch(bp->bio_cmd) {
1104 /* copy out ID field contents */
1105 idp = (struct fdc_readid *)bp->bio_data;
1106 idp->cyl = fdc->status[3];
1107 idp->head = fdc->status[4];
1108 idp->sec = fdc->status[5];
1109 idp->secshift = fdc->status[6];
1110 if (debugflags & 0x40)
1111 printf("c %d h %d s %d z %d\n",
1112 idp->cyl, idp->head, idp->sec, idp->secshift);
1116 bp->bio_pblkno += nsect;
1117 bp->bio_resid -= fd->fd_iosize;
1118 bp->bio_completed += fd->fd_iosize;
1119 fd->fd_ioptr += fd->fd_iosize;
1120 if (override_error) {
1121 if ((debugflags & 4))
1122 printf("FDOPT_NOERROR: returning bad data\n");
1124 /* Since we managed to get something done,
1125 * reset the retry */
1127 if (bp->bio_resid > 0)
1134 return (fdc_biodone(fdc, 0));
1138 fdc_thread(void *arg)
1140 struct fdc_data *fdc;
1145 mtx_lock(&fdc->fdc_mtx);
1146 fdc->flags |= FDC_KTHREAD_ALIVE;
1147 while ((fdc->flags & FDC_KTHREAD_EXIT) == 0) {
1148 mtx_unlock(&fdc->fdc_mtx);
1149 i = fdc_worker(fdc);
1150 if (i && debugflags & 0x20) {
1151 if (fdc->bp != NULL) {
1152 g_print_bio(fdc->bp);
1155 printf("Retry line %d\n", retry_line);
1158 mtx_lock(&fdc->fdc_mtx);
1160 fdc->flags &= ~(FDC_KTHREAD_EXIT | FDC_KTHREAD_ALIVE);
1161 mtx_unlock(&fdc->fdc_mtx);
1167 * Enqueue a request.
1170 fd_enqueue(struct fd_data *fd, struct bio *bp)
1172 struct fdc_data *fdc;
1177 mtx_lock(&fdc->fdc_mtx);
1178 /* If we go from idle, cancel motor turnoff */
1179 if (fd->fd_iocount++ == 0)
1180 callout_stop(&fd->toffhandle);
1181 if (fd->flags & FD_MOTOR) {
1182 /* The motor is on, send it directly to the controller */
1183 bioq_disksort(&fdc->head, bp);
1186 /* Queue it on the drive until the motor has started */
1187 bioq_insert_tail(&fd->fd_bq, bp);
1188 if (!(fd->flags & FD_MOTORWAIT))
1191 mtx_unlock(&fdc->fdc_mtx);
1195 * Try to find out if we have a disk in the drive.
1198 fd_probe_disk(struct fd_data *fd, int *recal)
1200 struct fdc_data *fdc;
1205 oopts = fd->options;
1206 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1210 * First recal, then seek to cyl#1, this clears the old condition on
1211 * the disk change line so we can examine it for current status.
1213 if (debugflags & 0x40)
1214 printf("New disk in probe\n");
1215 mtx_lock(&fdc->fdc_mtx);
1216 fd->flags |= FD_NEWDISK;
1217 mtx_unlock(&fdc->fdc_mtx);
1218 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
1220 tsleep(fdc, PRIBIO, "fdrecal", hz);
1221 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1222 goto done; /* XXX */
1223 if ((st0 & 0xc0) || cyl != 0)
1226 /* Seek to track 1 */
1227 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0))
1229 tsleep(fdc, PRIBIO, "fdseek", hz);
1230 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1231 goto done; /* XXX */
1232 *recal |= (1 << fd->fdsu);
1233 if (fdin_rd(fdc) & FDI_DCHG) {
1234 if (debugflags & 0x40)
1235 printf("Empty in probe\n");
1236 mtx_lock(&fdc->fdc_mtx);
1237 fd->flags |= FD_EMPTY;
1238 mtx_unlock(&fdc->fdc_mtx);
1240 if (fdc_sense_drive(fdc, &st3) != 0)
1242 if (debugflags & 0x40)
1243 printf("Got disk in probe\n");
1244 mtx_lock(&fdc->fdc_mtx);
1245 fd->flags &= ~FD_EMPTY;
1246 if (st3 & NE7_ST3_WP)
1249 fd->flags &= ~FD_WP;
1250 mtx_unlock(&fdc->fdc_mtx);
1255 fd->options = oopts;
1260 fdmisccmd(struct fd_data *fd, u_int cmd, void *data)
1263 struct fd_formb *finfo;
1264 struct fdc_readid *idfield;
1267 bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
1270 * Set up a bio request for fdstrategy(). bio_offset is faked
1271 * so that fdstrategy() will seek to the requested
1272 * cylinder, and use the desired head.
1275 if (cmd == BIO_FMT) {
1276 finfo = (struct fd_formb *)data;
1278 (finfo->cyl * fd->ft->heads + finfo->head) *
1280 bp->bio_length = sizeof *finfo;
1281 } else if (cmd == BIO_RDID) {
1282 idfield = (struct fdc_readid *)data;
1284 (idfield->cyl * fd->ft->heads + idfield->head) *
1286 bp->bio_length = sizeof(struct fdc_readid);
1287 } else if (cmd == BIO_PROBE) {
1290 panic("wrong cmd in fdmisccmd()");
1291 bp->bio_offset = bp->bio_pblkno * fd->sectorsize;
1292 bp->bio_data = data;
1293 bp->bio_driver1 = fd;
1299 tsleep(bp, PRIBIO, "fdwait", hz);
1300 } while (!(bp->bio_flags & BIO_DONE));
1301 error = bp->bio_error;
1308 * Try figuring out the density of the media present in our device.
1311 fdautoselect(struct fd_data *fd)
1313 struct fd_type *fdtp;
1314 struct fdc_readid id;
1317 if (!(fd->ft->flags & FL_AUTO))
1320 fdtp = fd_native_types[fd->type];
1321 fdsettype(fd, fdtp);
1322 if (!(fd->ft->flags & FL_AUTO))
1326 * Try reading sector ID fields, first at cylinder 0, head 0,
1327 * then at cylinder 2, head N. We don't probe cylinder 1,
1328 * since for 5.25in DD media in a HD drive, there are no data
1329 * to read (2 step pulses per media cylinder required). For
1330 * two-sided media, the second probe always goes to head 1, so
1331 * we can tell them apart from single-sided media. As a
1332 * side-effect this means that single-sided media should be
1333 * mentioned in the search list after two-sided media of an
1334 * otherwise identical density. Media with a different number
1335 * of sectors per track but otherwise identical parameters
1336 * cannot be distinguished at all.
1338 * If we successfully read an ID field on both cylinders where
1339 * the recorded values match our expectation, we are done.
1340 * Otherwise, we try the next density entry from the table.
1342 * Stepping to cylinder 2 has the side-effect of clearing the
1343 * unit attention bit.
1345 oopts = fd->options;
1346 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1347 for (; fdtp->heads; fdtp++) {
1348 fdsettype(fd, fdtp);
1350 id.cyl = id.head = 0;
1351 rv = fdmisccmd(fd, BIO_RDID, &id);
1354 if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize)
1357 id.head = fd->ft->heads - 1;
1358 rv = fdmisccmd(fd, BIO_RDID, &id);
1359 if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
1360 id.secshift != fdtp->secsize)
1366 fd->options = oopts;
1367 if (fdtp->heads == 0) {
1368 if (debugflags & 0x40)
1369 device_printf(fd->dev, "autoselection failed\n");
1370 fdsettype(fd, fd_native_types[fd->type]);
1373 if (debugflags & 0x40) {
1374 device_printf(fd->dev,
1375 "autoselected %d KB medium\n", fd->ft->size / 2);
1376 fdprinttype(fd->ft);
1383 * GEOM class implementation
1386 static g_access_t fd_access;
1387 static g_start_t fd_start;
1388 static g_ioctl_t fd_ioctl;
1390 struct g_class g_fd_class = {
1392 .version = G_VERSION,
1394 .access = fd_access,
1399 fd_access(struct g_provider *pp, int r, int w, int e)
1402 struct fdc_data *fdc;
1406 fd = pp->geom->softc;
1410 * If our provider is withering, we can only get negative requests
1411 * and we don't want to even see them
1413 if (pp->flags & G_PF_WITHER)
1420 if (ar == 0 && aw == 0 && ae == 0) {
1421 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
1422 device_unbusy(fd->dev);
1427 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
1428 if (fdmisccmd(fd, BIO_PROBE, NULL))
1430 if (fd->flags & FD_EMPTY)
1432 if (fd->flags & FD_NEWDISK) {
1433 if (fdautoselect(fd) != 0 &&
1434 (device_get_flags(fd->dev) & FD_NO_CHLINE)) {
1435 mtx_lock(&fdc->fdc_mtx);
1436 fd->flags |= FD_EMPTY;
1437 mtx_unlock(&fdc->fdc_mtx);
1440 mtx_lock(&fdc->fdc_mtx);
1441 fd->flags &= ~FD_NEWDISK;
1442 mtx_unlock(&fdc->fdc_mtx);
1444 device_busy(fd->dev);
1448 if (w > 0 && (fd->flags & FD_WP)) {
1450 device_unbusy(fd->dev);
1454 pp->sectorsize = fd->sectorsize;
1455 pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
1456 pp->mediasize = pp->stripesize * fd->ft->tracks;
1461 fd_start(struct bio *bp)
1463 struct fdc_data * fdc;
1464 struct fd_data * fd;
1466 fd = bp->bio_to->geom->softc;
1468 bp->bio_driver1 = fd;
1469 if (bp->bio_cmd & BIO_GETATTR) {
1470 if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
1472 if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
1474 g_io_deliver(bp, ENOIOCTL);
1477 if (!(bp->bio_cmd & (BIO_READ|BIO_WRITE))) {
1478 g_io_deliver(bp, EOPNOTSUPP);
1481 bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
1482 bp->bio_resid = bp->bio_length;
1488 fd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
1491 struct fdc_status *fsp;
1492 struct fdc_readid *rid;
1495 fd = pp->geom->softc;
1498 case FD_GTYPE: /* get drive type */
1499 *(struct fd_type *)data = *fd->ft;
1502 case FD_STYPE: /* set drive type */
1504 * Allow setting drive type temporarily iff
1505 * currently unset. Used for fdformat so any
1506 * user can set it, and then start formatting.
1508 fd->fts = *(struct fd_type *)data;
1509 if (fd->fts.sectrac) {
1510 /* XXX: check for rubbish */
1511 fdsettype(fd, &fd->fts);
1513 fdsettype(fd, fd_native_types[fd->type]);
1515 if (debugflags & 0x40)
1516 fdprinttype(fd->ft);
1519 case FD_GOPTS: /* get drive options */
1520 *(int *)data = fd->options;
1523 case FD_SOPTS: /* set drive options */
1524 fd->options = *(int *)data;
1528 error = priv_check(td, PRIV_DRIVER);
1531 fd->fdc->fdc_errs = 0;
1535 fsp = (struct fdc_status *)data;
1536 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
1538 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
1542 *(enum fd_drivetype *)data = fd->type;
1546 if (!(fflag & FWRITE))
1548 if (((struct fd_formb *)data)->format_version !=
1550 return (EINVAL); /* wrong version of formatting prog */
1551 error = fdmisccmd(fd, BIO_FMT, data);
1552 mtx_lock(&fd->fdc->fdc_mtx);
1553 fd->flags |= FD_NEWDISK;
1554 mtx_unlock(&fd->fdc->fdc_mtx);
1558 rid = (struct fdc_readid *)data;
1559 if (rid->cyl > 85 || rid->head > 1)
1561 error = fdmisccmd(fd, BIO_RDID, data);
1566 /* For backwards compat with old fd*(8) tools */
1571 if (debugflags & 0x80)
1572 printf("Unknown ioctl %lx\n", cmd);
1582 * Configuration/initialization stuff, per controller.
1585 devclass_t fdc_devclass;
1586 static devclass_t fd_devclass;
1594 fdc_release_resources(struct fdc_data *fdc)
1597 struct resource *last;
1602 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1603 fdc->fdc_intr = NULL;
1604 if (fdc->res_irq != NULL)
1605 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
1607 fdc->res_irq = NULL;
1609 for (i = 0; i < FDC_MAXREG; i++) {
1610 if (fdc->resio[i] != NULL && fdc->resio[i] != last) {
1611 bus_release_resource(dev, SYS_RES_IOPORT,
1612 fdc->ridio[i], fdc->resio[i]);
1613 last = fdc->resio[i];
1614 fdc->resio[i] = NULL;
1617 if (fdc->res_drq != NULL)
1618 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
1620 fdc->res_drq = NULL;
1624 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1626 struct fdc_ivars *ivars = device_get_ivars(child);
1629 case FDC_IVAR_FDUNIT:
1630 *result = ivars->fdunit;
1632 case FDC_IVAR_FDTYPE:
1633 *result = ivars->fdtype;
1642 fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1644 struct fdc_ivars *ivars = device_get_ivars(child);
1647 case FDC_IVAR_FDUNIT:
1648 ivars->fdunit = value;
1650 case FDC_IVAR_FDTYPE:
1651 ivars->fdtype = value;
1660 fdc_initial_reset(device_t dev, struct fdc_data *fdc)
1662 int ic_type, part_id;
1665 * A status value of 0xff is very unlikely, but not theoretically
1666 * impossible, but it is far more likely to indicate an empty bus.
1668 if (fdsts_rd(fdc) == 0xff)
1672 * Assert a reset to the floppy controller and check that the status
1673 * register goes to zero.
1677 if (fdsts_rd(fdc) != 0)
1681 * Clear the reset and see it come ready.
1683 fdout_wr(fdc, FDO_FRST);
1685 if (fdsts_rd(fdc) != 0x80)
1688 /* Then, see if it can handle a command. */
1689 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, 0xaf, 0x1e, 0))
1693 * Try to identify the chip.
1695 * The i8272 datasheet documents that unknown commands
1696 * will return ST0 as 0x80. The i8272 is supposedly identical
1698 * The i82077SL datasheet says 0x90 for the VERSION command,
1699 * and several "superio" chips emulate this.
1701 if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
1703 if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
1707 "ic_type %02x part_id %02x\n", ic_type, part_id);
1708 switch (ic_type & 0xff) {
1710 device_set_desc(dev, "NEC 765 or clone");
1711 fdc->fdct = FDC_NE765;
1715 device_set_desc(dev,
1716 "Enhanced floppy controller");
1717 fdc->fdct = FDC_ENHANCED;
1720 device_set_desc(dev, "Generic floppy controller");
1721 fdc->fdct = FDC_UNKNOWN;
1728 fdc_detach(device_t dev)
1730 struct fdc_data *fdc;
1733 fdc = device_get_softc(dev);
1735 /* have our children detached first */
1736 if ((error = bus_generic_detach(dev)))
1740 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1741 fdc->fdc_intr = NULL;
1743 /* kill worker thread */
1744 mtx_lock(&fdc->fdc_mtx);
1745 fdc->flags |= FDC_KTHREAD_EXIT;
1747 while ((fdc->flags & FDC_KTHREAD_ALIVE) != 0)
1748 msleep(fdc->fdc_thread, &fdc->fdc_mtx, PRIBIO, "fdcdet", 0);
1749 mtx_unlock(&fdc->fdc_mtx);
1751 /* reset controller, turn motor off */
1754 if (!(fdc->flags & FDC_NODMA))
1755 isa_dma_release(fdc->dmachan);
1756 fdc_release_resources(fdc);
1757 mtx_destroy(&fdc->fdc_mtx);
1762 * Add a child device to the fdc controller. It will then be probed etc.
1765 fdc_add_child(device_t dev, const char *name, int unit)
1767 struct fdc_ivars *ivar;
1770 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
1773 child = device_add_child(dev, name, unit);
1774 if (child == NULL) {
1775 free(ivar, M_DEVBUF);
1778 device_set_ivars(child, ivar);
1779 ivar->fdunit = unit;
1780 ivar->fdtype = FDT_NONE;
1781 if (resource_disabled(name, unit))
1782 device_disable(child);
1787 fdc_attach(device_t dev)
1789 struct fdc_data *fdc;
1792 fdc = device_get_softc(dev);
1794 error = fdc_initial_reset(dev, fdc);
1796 device_printf(dev, "does not respond\n");
1799 error = bus_setup_intr(dev, fdc->res_irq,
1800 INTR_TYPE_BIO | INTR_ENTROPY |
1801 ((fdc->flags & FDC_NOFAST) ? INTR_MPSAFE : 0),
1802 ((fdc->flags & FDC_NOFAST) ? NULL : fdc_intr_fast),
1803 ((fdc->flags & FDC_NOFAST) ? fdc_intr : NULL),
1804 fdc, &fdc->fdc_intr);
1806 device_printf(dev, "cannot setup interrupt\n");
1809 if (!(fdc->flags & FDC_NODMA)) {
1810 error = isa_dma_acquire(fdc->dmachan);
1812 error = isa_dma_init(fdc->dmachan,
1813 MAX_BYTES_PER_CYL, M_WAITOK);
1815 isa_dma_release(fdc->dmachan);
1820 fdc->fdcu = device_get_unit(dev);
1821 fdc->flags |= FDC_NEEDS_RESET;
1823 mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
1825 /* reset controller, turn motor off, clear fdout mirror reg */
1826 fdout_wr(fdc, fdc->fdout = 0);
1827 bioq_init(&fdc->head);
1829 kproc_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
1830 "fdc%d", device_get_unit(dev));
1838 fdc_hints_probe(device_t dev)
1840 const char *name, *dname;
1841 int i, error, dunit;
1844 * Probe and attach any children. We should probably detect
1845 * devices from the BIOS unless overridden.
1847 name = device_get_nameunit(dev);
1849 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
1850 resource_int_value(dname, dunit, "drive", &dunit);
1851 fdc_add_child(dev, dname, dunit);
1854 if ((error = bus_generic_attach(dev)) != 0)
1860 fdc_print_child(device_t me, device_t child)
1862 int retval = 0, flags;
1864 retval += bus_print_child_header(me, child);
1865 retval += printf(" on %s drive %d", device_get_nameunit(me),
1866 fdc_get_fdunit(child));
1867 if ((flags = device_get_flags(me)) != 0)
1868 retval += printf(" flags %#x", flags);
1869 retval += printf("\n");
1875 * Configuration/initialization, per drive.
1878 fd_probe(device_t dev)
1883 struct fdc_data *fdc;
1887 fdsu = fdc_get_fdunit(dev);
1888 fd = device_get_softc(dev);
1889 fdc = device_get_softc(device_get_parent(dev));
1890 flags = device_get_flags(dev);
1895 unit = device_get_unit(dev);
1897 /* Auto-probe if fdinfo is present, but always allow override. */
1898 type = flags & FD_TYPEMASK;
1899 if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
1903 /* make sure fdautoselect() will be called */
1904 fd->flags = FD_EMPTY;
1908 #if (defined(__i386__) && !defined(PC98)) || defined(__amd64__)
1909 if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
1910 /* Look up what the BIOS thinks we have. */
1912 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
1914 fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
1915 if (fd->type == FDT_288M_1)
1916 fd->type = FDT_288M;
1918 #endif /* __i386__ || __amd64__ */
1919 /* is there a unit? */
1920 if (fd->type == FDT_NONE)
1924 mtx_lock(&fdc->fdc_mtx);
1930 fdc_reset(fdc); /* XXX reset, then unreset, etc. */
1931 DELAY(1000000); /* 1 sec */
1933 if ((flags & FD_NO_PROBE) == 0) {
1934 /* If we're at track 0 first seek inwards. */
1935 if ((fdc_sense_drive(fdc, &st3) == 0) &&
1936 (st3 & NE7_ST3_T0)) {
1937 /* Seek some steps... */
1938 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1939 /* ...wait a moment... */
1941 /* make ctrlr happy: */
1942 fdc_sense_int(fdc, NULL, NULL);
1946 for (i = 0; i < 2; i++) {
1948 * we must recalibrate twice, just in case the
1949 * heads have been beyond cylinder 76, since
1950 * most FDCs still barf when attempting to
1951 * recalibrate more than 77 steps
1954 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1955 /* a second being enough for full stroke seek*/
1956 DELAY(i == 0 ? 1000000 : 300000);
1958 /* anything responding? */
1959 if (fdc_sense_int(fdc, &st0, NULL) == 0 &&
1960 (st0 & NE7_ST0_EC) == 0)
1961 break; /* already probed succesfully */
1969 mtx_unlock(&fdc->fdc_mtx);
1972 if ((flags & FD_NO_PROBE) == 0 &&
1973 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
1980 device_set_desc(dev, "1200-KB 5.25\" drive");
1983 device_set_desc(dev, "1440-KB 3.5\" drive");
1986 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1989 device_set_desc(dev, "360-KB 5.25\" drive");
1992 device_set_desc(dev, "720-KB 3.5\" drive");
1997 fd->track = FD_NO_TRACK;
2001 callout_init_mtx(&fd->toffhandle, &fd->fdc->fdc_mtx, 0);
2003 /* initialize densities for subdevices */
2004 fdsettype(fd, fd_native_types[fd->type]);
2009 * We have to do this in a geom event because GEOM is not running
2010 * when fd_attach() is.
2011 * XXX: move fd_attach after geom like ata/scsi disks
2014 fd_attach2(void *arg, int flag)
2020 fd->fd_geom = g_new_geomf(&g_fd_class,
2021 "fd%d", device_get_unit(fd->dev));
2022 fd->fd_provider = g_new_providerf(fd->fd_geom, "%s", fd->fd_geom->name);
2023 fd->fd_geom->softc = fd;
2024 g_error_provider(fd->fd_provider, 0);
2028 fd_attach(device_t dev)
2032 fd = device_get_softc(dev);
2033 g_post_event(fd_attach2, fd, M_WAITOK, NULL);
2034 fd->flags |= FD_EMPTY;
2035 bioq_init(&fd->fd_bq);
2041 fd_detach_geom(void *arg, int flag)
2043 struct fd_data *fd = arg;
2045 g_topology_assert();
2046 g_wither_geom(fd->fd_geom, ENXIO);
2050 fd_detach(device_t dev)
2054 fd = device_get_softc(dev);
2055 g_waitfor_event(fd_detach_geom, fd, M_WAITOK, NULL);
2056 while (device_get_state(dev) == DS_BUSY)
2057 tsleep(fd, PZERO, "fdd", hz/10);
2058 callout_drain(&fd->toffhandle);
2063 static device_method_t fd_methods[] = {
2064 /* Device interface */
2065 DEVMETHOD(device_probe, fd_probe),
2066 DEVMETHOD(device_attach, fd_attach),
2067 DEVMETHOD(device_detach, fd_detach),
2068 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2069 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
2070 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
2074 static driver_t fd_driver = {
2077 sizeof(struct fd_data)
2081 fdc_modevent(module_t mod, int type, void *data)
2084 return (g_modevent(NULL, type, &g_fd_class));
2087 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, fdc_modevent, 0);