2 * Copyright (c) 1990 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
9 * aided by the Linux floppy driver modifications from David Bateman
10 * (dbateman@eng.uts.edu.au).
12 * Copyright (c) 1993, 1994 by
13 * jc@irbs.UUCP (John Capo)
14 * vak@zebub.msk.su (Serge Vakulenko)
15 * ache@astral.msk.su (Andrew A. Chernov)
17 * Copyright (c) 1993, 1994, 1995 by
18 * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
19 * dufault@hda.com (Peter Dufault)
21 * Copyright (c) 2001 Joerg Wunsch,
22 * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the University of
35 * California, Berkeley and its contributors.
36 * 4. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91
53 * $FreeBSD: src/sys/isa/fd.c,v 1.176.2.8 2002/05/15 21:56:14 joerg Exp $
54 * $DragonFly: src/sys/dev/disk/fd/fd.c,v 1.8 2003/07/22 17:03:32 dillon Exp $
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/bootmaj.h>
64 #include <sys/kernel.h>
68 #include <sys/disklabel.h>
69 #include <sys/devicestat.h>
70 #include <sys/fcntl.h>
71 #include <sys/malloc.h>
72 #include <sys/module.h>
74 #include <sys/syslog.h>
75 #include <sys/device.h>
78 #include <machine/bus.h>
83 #include <machine/clock.h>
84 #include <machine/ioctl_fd.h>
85 #include <machine/resource.h>
86 #include <machine/stdarg.h>
88 #include <isa/isavar.h>
89 #include <isa/isareg.h>
90 #include <isa/fdreg.h>
94 /* misuse a flag to identify format operation */
95 #define B_FORMAT B_XXX
97 /* configuration flags */
98 #define FDC_PRETEND_D0 (1 << 0) /* pretend drive 0 to be there */
99 #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */
101 /* internally used only, not really from CMOS: */
102 #define RTCFDT_144M_PRETENDED 0x1000
104 /* error returns for fd_cmd() */
106 #define FD_NOT_VALID -2
107 #define FDC_ERRMAX 100 /* do not log more */
109 * Stop retrying after this many DMA overruns. Since each retry takes
110 * one revolution, with 300 rpm., 25 retries take approximately 10
111 * seconds which the read attempt will block in case the DMA overrun
114 #define FDC_DMAOV_MAX 25
117 * Timeout value for the PIO loops to wait until the FDC main status
118 * register matches our expectations (request for master, direction
119 * bit). This is supposed to be a number of microseconds, although
120 * timing might actually not be very accurate.
122 * Timeouts of 100 msec are believed to be required for some broken
125 #define FDSTS_TIMEOUT 100000
128 #define NUMDENS (NUMTYPES - 7)
130 /* These defines (-1) must match index for fd_types */
131 #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */
132 #define NO_TYPE 0 /* must match NO_TYPE in ft.c */
144 #define FD_1480in5_25 11
145 #define FD_1440in5_25 12
146 #define FD_820in5_25 13
147 #define FD_800in5_25 14
148 #define FD_720in5_25 15
149 #define FD_360in5_25 16
150 #define FD_640in5_25 17
153 static struct fd_type fd_types[NUMTYPES] =
155 { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
156 { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
157 { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
158 { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */
159 { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */
160 { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */
161 { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */
162 { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */
163 { 8,2,0xFF,0x2A,80,1280,1,FDC_250KBPS,2,0x50,1 }, /* 640K in DD 5.25in */
164 { 8,3,0xFF,0x35,77,1232,1,FDC_500KBPS,2,0x74,1 }, /* 1.23M in HD 5.25in */
166 { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
167 { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
168 { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */
169 { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */
170 { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */
171 { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */
172 { 8,2,0xFF,0x2A,80,1280,1,FDC_300KBPS,2,0x50,1 }, /* 640K in HD 5.25in */
175 #define DRVS_PER_CTLR 2 /* 2 floppies */
177 /***********************************************************************\
178 * Per controller structure. *
179 \***********************************************************************/
180 static devclass_t fdc_devclass;
182 /***********************************************************************\
183 * Per drive structure. *
184 * N per controller (DRVS_PER_CTLR) *
185 \***********************************************************************/
187 struct fdc_data *fdc; /* pointer to controller structure */
188 int fdsu; /* this units number on this controller */
189 int type; /* Drive type (FD_1440...) */
190 struct fd_type *ft; /* pointer to the type descriptor */
192 #define FD_OPEN 0x01 /* it's open */
193 #define FD_ACTIVE 0x02 /* it's active */
194 #define FD_MOTOR 0x04 /* motor should be on */
195 #define FD_MOTOR_WAIT 0x08 /* motor coming up */
198 #define FD_NO_TRACK -2
199 int track; /* where we think the head is */
200 int options; /* user configurable options, see ioctl_fd.h */
201 struct callout_handle toffhandle;
202 struct callout_handle tohandle;
203 struct devstat device_stats;
211 static devclass_t fd_devclass;
213 /***********************************************************************\
214 * Throughout this file the following conventions will be used: *
215 * fd is a pointer to the fd_data struct for the drive in question *
216 * fdc is a pointer to the fdc_data struct for the controller *
217 * fdu is the floppy drive unit number *
218 * fdcu is the floppy controller unit number *
219 * fdsu is the floppy drive unit number on that controller. (sub-unit) *
220 \***********************************************************************/
222 /* internal functions */
223 static void fdc_intr(void *);
224 static void set_motor(struct fdc_data *, int, int);
227 static timeout_t fd_turnoff;
228 static timeout_t fd_motor_on;
229 static void fd_turnon(struct fd_data *);
230 static void fdc_reset(fdc_p);
231 static int fd_in(struct fdc_data *, int *);
232 static int out_fdc(struct fdc_data *, int);
233 static void fdstart(struct fdc_data *);
234 static timeout_t fd_iotimeout;
235 static timeout_t fd_pseudointr;
236 static int fdstate(struct fdc_data *);
237 static int retrier(struct fdc_data *);
238 static int fdformat(dev_t, struct fd_formb *, struct thread *);
240 static int enable_fifo(fdc_p fdc);
242 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */
248 #define SEEKCOMPLETE 3
250 #define RECALCOMPLETE 5
256 #define IOTIMEDOUT 11
257 #define RESETCOMPLETE 12
261 static char const * const fdstates[] =
279 /* CAUTION: fd_debug causes huge amounts of logging output */
280 static int volatile fd_debug = 0;
281 #define TRACE0(arg) if(fd_debug) printf(arg)
282 #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
283 #else /* FDC_DEBUG */
285 #define TRACE1(arg1, arg2)
286 #endif /* FDC_DEBUG */
289 fdout_wr(fdc_p fdc, u_int8_t v)
291 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
297 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
301 fddata_wr(fdc_p fdc, u_int8_t v)
303 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
309 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
313 fdctl_wr_isa(fdc_p fdc, u_int8_t v)
315 bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
320 fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v)
322 bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v);
331 return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
336 static d_open_t Fdopen; /* NOTE, not fdopen */
337 static d_close_t fdclose;
338 static d_ioctl_t fdioctl;
339 static d_strategy_t fdstrategy;
341 static struct cdevsw fd_cdevsw = {
343 /* maj */ FD_CDEV_MAJOR,
351 /* write */ physwrite,
355 /* strategy */ fdstrategy,
361 fdc_err(struct fdc_data *fdc, const char *s)
365 if (fdc->fdc_errs < FDC_ERRMAX)
366 device_printf(fdc->fdc_dev, "%s", s);
367 else if (fdc->fdc_errs == FDC_ERRMAX)
368 device_printf(fdc->fdc_dev, "too many errors, not "
369 "logging any more\n");
376 * fd_cmd: Send a command to the chip. Takes a varargs with this structure:
378 * # of output bytes, output bytes as ints ...,
379 * # of input bytes, input bytes as ints ...
382 fd_cmd(struct fdc_data *fdc, int n_out, ...)
390 cmd = (u_char)(va_arg(ap, int));
393 for (n = 0; n < n_out; n++)
395 if (out_fdc(fdc, va_arg(ap, int)) < 0)
398 snprintf(msg, sizeof(msg),
399 "cmd %x failed at out byte %d of %d\n",
401 return fdc_err(fdc, msg);
404 n_in = va_arg(ap, int);
405 for (n = 0; n < n_in; n++)
407 int *ptr = va_arg(ap, int *);
408 if (fd_in(fdc, ptr) < 0)
411 snprintf(msg, sizeof(msg),
412 "cmd %02x failed at in byte %d of %d\n",
414 return fdc_err(fdc, msg);
422 enable_fifo(fdc_p fdc)
426 if ((fdc->flags & FDC_HAS_FIFO) == 0) {
430 * Cannot use fd_cmd the normal way here, since
431 * this might be an invalid command. Thus we send the
432 * first byte, and check for an early turn of data directon.
435 if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
436 return fdc_err(fdc, "Enable FIFO failed\n");
438 /* If command is invalid, return */
440 while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
441 != NE7_RQM && j-- > 0) {
442 if (i == (NE7_DIO | NE7_RQM)) {
450 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
452 return fdc_err(fdc, "Enable FIFO failed\n");
454 fdc->flags |= FDC_HAS_FIFO;
458 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
459 return fdc_err(fdc, "Re-enable FIFO failed\n");
464 fd_sense_drive_status(fdc_p fdc, int *st3p)
468 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
470 return fdc_err(fdc, "Sense Drive Status failed\n");
479 fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
483 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
486 "sense intr err reading stat reg 0\n");
493 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
495 * There doesn't seem to have been an interrupt.
500 if (fd_in(fdc, &cyl) < 0) {
501 return fdc_err(fdc, "can't get cyl num\n");
512 fd_read_status(fdc_p fdc, int fdsu)
516 for (i = 0; i < 7; i++) {
518 * XXX types are poorly chosen. Only bytes can by read
519 * from the hardware, but fdc->status[] wants u_ints and
520 * fd_in() gives ints.
524 ret = fd_in(fdc, &status);
525 fdc->status[i] = status;
531 fdc->flags |= FDC_STAT_VALID;
533 fdc->flags &= ~FDC_STAT_VALID;
538 /****************************************************************************/
539 /* autoconfiguration stuff */
540 /****************************************************************************/
543 fdc_alloc_resources(struct fdc_data *fdc)
549 ispnp = (fdc->flags & FDC_ISPNP) != 0;
550 ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
551 fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
552 fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
555 * On standard ISA, we don't just use an 8 port range
556 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
559 * Isn't PC hardware wonderful.
561 * The Y-E Data PCMCIA FDC doesn't have this problem, it
562 * uses the register with offset 6 for pseudo-DMA, and the
563 * one with offset 7 as control register.
565 fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
566 &fdc->rid_ioport, 0ul, ~0ul,
567 ispcmcia ? 8 : (ispnp ? 1 : 6),
569 if (fdc->res_ioport == 0) {
570 device_printf(dev, "cannot reserve I/O port range\n");
573 fdc->portt = rman_get_bustag(fdc->res_ioport);
574 fdc->porth = rman_get_bushandle(fdc->res_ioport);
578 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
579 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
580 * by checking the size and adjust the port address
583 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
587 * Register the control port range as rid 1 if it
588 * isn't there already. Most PnP BIOSen will have
589 * already done this but non-PnP configurations don't.
591 * And some (!!) report 0x3f2-0x3f5 and completely
592 * leave out the control register! It seems that some
593 * non-antique controller chips have a different
594 * method of programming the transfer speed which
595 * doesn't require the control register, but it's
596 * mighty bogus as the chip still responds to the
597 * address for the control register.
599 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
602 /* Find the control port, usually 0x3f7 */
603 ctlstart = rman_get_start(fdc->res_ioport) +
606 bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
610 * Now (finally!) allocate the control port.
613 fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
615 0ul, ~0ul, 1, RF_ACTIVE);
616 if (fdc->res_ctl == 0) {
618 "cannot reserve control I/O port range\n");
621 fdc->ctlt = rman_get_bustag(fdc->res_ctl);
622 fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
625 fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
626 &fdc->rid_irq, 0ul, ~0ul, 1,
628 if (fdc->res_irq == 0) {
629 device_printf(dev, "cannot reserve interrupt line\n");
633 if ((fdc->flags & FDC_NODMA) == 0) {
634 fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
635 &fdc->rid_drq, 0ul, ~0ul, 1,
637 if (fdc->res_drq == 0) {
638 device_printf(dev, "cannot reserve DMA request line\n");
641 fdc->dmachan = fdc->res_drq->r_start;
648 fdc_release_resources(struct fdc_data *fdc)
653 if (fdc->res_irq != 0) {
654 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
656 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
659 if (fdc->res_ctl != 0) {
660 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
662 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
665 if (fdc->res_ioport != 0) {
666 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
668 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
671 if (fdc->res_drq != 0) {
672 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
674 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
679 /****************************************************************************/
680 /* autoconfiguration stuff */
681 /****************************************************************************/
683 static struct isa_pnp_id fdc_ids[] = {
684 {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
685 {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
690 fdc_read_ivar(device_t dev, device_t child, int which, u_long *result)
692 struct fdc_ivars *ivars = device_get_ivars(child);
695 case FDC_IVAR_FDUNIT:
696 *result = ivars->fdunit;
705 * fdc controller section.
708 fdc_probe(device_t dev)
711 struct fdc_data *fdc;
713 fdc = device_get_softc(dev);
714 bzero(fdc, sizeof *fdc);
716 fdc->fdctl_wr = fdctl_wr_isa;
719 error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
723 fdc->flags |= FDC_ISPNP;
725 /* Attempt to allocate our resources for the duration of the probe */
726 error = fdc_alloc_resources(fdc);
730 /* First - lets reset the floppy controller */
733 fdout_wr(fdc, FDO_FRST);
735 /* see if it can handle a command */
736 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
737 NE7_SPEC_2(2, 0), 0)) {
742 if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
743 ic_type = (u_char)ic_type;
746 device_set_desc(dev, "NEC 765 or clone");
747 fdc->fdct = FDC_NE765;
750 device_set_desc(dev, "Intel 82077 or clone");
751 fdc->fdct = FDC_I82077;
754 device_set_desc(dev, "NEC 72065B or clone");
755 fdc->fdct = FDC_NE72065;
758 device_set_desc(dev, "generic floppy controller");
759 fdc->fdct = FDC_UNKNOWN;
765 fdc_release_resources(fdc);
772 fdc_pccard_probe(device_t dev)
775 struct fdc_data *fdc;
777 fdc = device_get_softc(dev);
778 bzero(fdc, sizeof *fdc);
780 fdc->fdctl_wr = fdctl_wr_pcmcia;
782 fdc->flags |= FDC_ISPCMCIA | FDC_NODMA;
784 /* Attempt to allocate our resources for the duration of the probe */
785 error = fdc_alloc_resources(fdc);
789 /* First - lets reset the floppy controller */
792 fdout_wr(fdc, FDO_FRST);
794 /* see if it can handle a command */
795 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
796 NE7_SPEC_2(2, 0), 0)) {
801 device_set_desc(dev, "Y-E Data PCMCIA floppy");
802 fdc->fdct = FDC_NE765;
805 fdc_release_resources(fdc);
810 fdc_pccard_detach(device_t dev)
812 struct fdc_data *fdc;
815 fdc = device_get_softc(dev);
817 /* have our children detached first */
818 if ((error = bus_generic_detach(dev)))
821 if ((fdc->flags & FDC_ATTACHED) == 0) {
822 device_printf(dev, "already unloaded\n");
825 fdc->flags &= ~FDC_ATTACHED;
827 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
829 fdc_release_resources(fdc);
830 device_printf(dev, "unload\n");
834 #endif /* NCARD > 0 */
837 * Add a child device to the fdc controller. It will then be probed etc.
840 fdc_add_child(device_t dev, const char *name, int unit)
843 struct fdc_ivars *ivar;
846 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT);
849 bzero(ivar, sizeof *ivar);
850 if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
852 child = device_add_child(dev, name, unit);
855 device_set_ivars(child, ivar);
856 if (resource_int_value(name, unit, "disabled", &disabled) == 0
858 device_disable(child);
862 fdc_attach(device_t dev)
864 struct fdc_data *fdc;
867 fdc = device_get_softc(dev);
868 error = fdc_alloc_resources(fdc);
870 device_printf(dev, "cannot re-aquire resources\n");
873 error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
874 INTR_TYPE_BIO, fdc_intr, fdc, &fdc->fdc_intr);
876 device_printf(dev, "cannot setup interrupt\n");
879 fdc->fdcu = device_get_unit(dev);
880 fdc->flags |= FDC_ATTACHED;
882 if ((fdc->flags & FDC_NODMA) == 0) {
883 /* Acquire the DMA channel forever, The driver will do the rest */
884 /* XXX should integrate with rman */
885 isa_dma_acquire(fdc->dmachan);
886 isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
888 fdc->state = DEVIDLE;
890 /* reset controller, turn motor off, clear fdout mirror reg */
891 fdout_wr(fdc, ((fdc->fdout = 0)));
892 bufq_init(&fdc->head);
895 * Probe and attach any children. We should probably detect
896 * devices from the BIOS unless overridden.
898 for (i = resource_query_string(-1, "at", device_get_nameunit(dev));
900 i = resource_query_string(i, "at", device_get_nameunit(dev)))
901 fdc_add_child(dev, resource_query_name(i),
902 resource_query_unit(i));
904 return (bus_generic_attach(dev));
908 fdc_print_child(device_t me, device_t child)
912 retval += bus_print_child_header(me, child);
913 retval += printf(" on %s drive %d\n", device_get_nameunit(me),
914 fdc_get_fdunit(child));
919 static device_method_t fdc_methods[] = {
920 /* Device interface */
921 DEVMETHOD(device_probe, fdc_probe),
922 DEVMETHOD(device_attach, fdc_attach),
923 DEVMETHOD(device_detach, bus_generic_detach),
924 DEVMETHOD(device_shutdown, bus_generic_shutdown),
925 DEVMETHOD(device_suspend, bus_generic_suspend),
926 DEVMETHOD(device_resume, bus_generic_resume),
929 DEVMETHOD(bus_print_child, fdc_print_child),
930 DEVMETHOD(bus_read_ivar, fdc_read_ivar),
931 /* Our children never use any other bus interface methods. */
936 static driver_t fdc_driver = {
939 sizeof(struct fdc_data)
942 DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
946 static device_method_t fdc_pccard_methods[] = {
947 /* Device interface */
948 DEVMETHOD(device_probe, fdc_pccard_probe),
949 DEVMETHOD(device_attach, fdc_attach),
950 DEVMETHOD(device_detach, fdc_pccard_detach),
951 DEVMETHOD(device_shutdown, bus_generic_shutdown),
952 DEVMETHOD(device_suspend, bus_generic_suspend),
953 DEVMETHOD(device_resume, bus_generic_resume),
956 DEVMETHOD(bus_print_child, fdc_print_child),
957 DEVMETHOD(bus_read_ivar, fdc_read_ivar),
958 /* Our children never use any other bus interface methods. */
963 static driver_t fdc_pccard_driver = {
966 sizeof(struct fdc_data)
969 DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
971 #endif /* NCARD > 0 */
973 /******************************************************************/
975 * devices attached to the controller section.
978 fd_probe(device_t dev)
983 struct fdc_data *fdc;
985 static int fd_fifo = 0;
987 fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
988 fd = device_get_softc(dev);
989 fdc = device_get_softc(device_get_parent(dev));
991 bzero(fd, sizeof *fd);
995 fd->fdu = device_get_unit(dev);
998 /* look up what bios thinks we have */
1001 if ((fdc->flags & FDC_ISPCMCIA))
1003 else if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0)
1004 fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
1006 fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
1009 fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
1016 fdt = RTCFDT_144M; /* XXX probably */
1019 /* is there a unit? */
1020 if (fdt == RTCFDT_NONE)
1024 set_motor(fdc, fdsu, TURNON);
1025 DELAY(1000000); /* 1 sec */
1027 /* XXX This doesn't work before the first set_motor() */
1028 if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN
1029 && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0
1030 && enable_fifo(fdc) == 0) {
1031 device_printf(device_get_parent(dev),
1032 "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1036 if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0)
1037 && (st3 & NE7_ST3_T0)) {
1038 /* if at track 0, first seek inwards */
1039 /* seek some steps: */
1040 fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0);
1041 DELAY(300000); /* ...wait a moment... */
1042 fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
1045 /* If we're at track 0 first seek inwards. */
1046 if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
1047 /* Seek some steps... */
1048 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1049 /* ...wait a moment... */
1051 /* make ctrlr happy: */
1052 fd_sense_int(fdc, 0, 0);
1056 for (i = 0; i < 2; i++) {
1058 * we must recalibrate twice, just in case the
1059 * heads have been beyond cylinder 76, since most
1060 * FDCs still barf when attempting to recalibrate
1061 * more than 77 steps
1064 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1065 /* a second being enough for full stroke seek*/
1066 DELAY(i == 0 ? 1000000 : 300000);
1068 /* anything responding? */
1069 if (fd_sense_int(fdc, &st0, 0) == 0 &&
1070 (st0 & NE7_ST0_EC) == 0)
1071 break; /* already probed succesfully */
1075 set_motor(fdc, fdsu, TURNOFF);
1077 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
1080 fd->track = FD_NO_TRACK;
1084 callout_handle_init(&fd->toffhandle);
1085 callout_handle_init(&fd->tohandle);
1089 device_set_desc(dev, "1200-KB 5.25\" drive");
1092 case RTCFDT_144M | RTCFDT_144M_PRETENDED:
1093 device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive");
1097 device_set_desc(dev, "1440-KB 3.5\" drive");
1102 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1106 device_set_desc(dev, "360-KB 5.25\" drive");
1110 printf("720-KB 3.5\" drive");
1120 fd_attach(device_t dev)
1129 static int cdevsw_add_done = 0;
1131 fd = device_get_softc(dev);
1133 if (!cdevsw_add_done) {
1134 cdevsw_add(&fd_cdevsw); /* XXX */
1137 make_dev(&fd_cdevsw, (fd->fdu << 6),
1138 UID_ROOT, GID_OPERATOR, 0640, "rfd%d", fd->fdu);
1141 /* Other make_dev() go here. */
1145 * Export the drive to the devstat interface.
1147 devstat_add_entry(&fd->device_stats, device_get_name(dev),
1148 device_get_unit(dev), 512, DEVSTAT_NO_ORDERED_TAGS,
1149 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
1150 DEVSTAT_PRIORITY_FD);
1155 fd_detach(device_t dev)
1159 fd = device_get_softc(dev);
1160 untimeout(fd_turnoff, fd, fd->toffhandle);
1165 static device_method_t fd_methods[] = {
1166 /* Device interface */
1167 DEVMETHOD(device_probe, fd_probe),
1168 DEVMETHOD(device_attach, fd_attach),
1169 DEVMETHOD(device_detach, fd_detach),
1170 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1171 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
1172 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
1177 static driver_t fd_driver = {
1180 sizeof(struct fd_data)
1183 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
1185 /****************************************************************************/
1186 /* motor control stuff */
1187 /* remember to not deselect the drive we're working on */
1188 /****************************************************************************/
1190 set_motor(struct fdc_data *fdc, int fdsu, int turnon)
1192 int fdout = fdc->fdout;
1193 int needspecify = 0;
1196 fdout &= ~FDO_FDSEL;
1197 fdout |= (FDO_MOEN0 << fdsu) + fdsu;
1199 fdout &= ~(FDO_MOEN0 << fdsu);
1202 && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
1203 /* gonna turn off the last drive, put FDC to bed */
1204 fdout &= ~ (FDO_FRST|FDO_FDMAEN);
1206 /* make sure controller is selected and specified */
1207 if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
1209 fdout |= (FDO_FRST|FDO_FDMAEN);
1212 fdout_wr(fdc, fdout);
1214 TRACE1("[0x%x->FDOUT]", fdout);
1219 * special case: since we have just woken up the FDC
1220 * from its sleep, we silently assume the command will
1221 * be accepted, and do not test for a timeout
1223 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1224 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1226 if (fdc->flags & FDC_HAS_FIFO)
1227 (void) enable_fifo(fdc);
1232 fd_turnoff(void *xfd)
1237 TRACE1("[fd%d: turnoff]", fd->fdu);
1241 * Don't turn off the motor yet if the drive is active.
1243 * If we got here, this could only mean we missed an interrupt.
1244 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
1245 * after a controller reset. Just schedule a pseudo-interrupt
1246 * so the state machine gets re-entered.
1248 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
1254 fd->flags &= ~FD_MOTOR;
1255 set_motor(fd->fdc, fd->fdsu, TURNOFF);
1260 fd_motor_on(void *xfd)
1266 fd->flags &= ~FD_MOTOR_WAIT;
1267 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
1277 if(!(fd->flags & FD_MOTOR))
1279 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
1280 set_motor(fd->fdc, fd->fdsu, TURNON);
1281 timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
1286 fdc_reset(fdc_p fdc)
1288 /* Try a reset, keep motor on */
1289 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1290 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1292 /* enable FDC, but defer interrupts a moment */
1293 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
1294 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
1296 fdout_wr(fdc, fdc->fdout);
1297 TRACE1("[0x%x->FDOUT]", fdc->fdout);
1299 /* XXX after a reset, silently believe the FDC will accept commands */
1300 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1301 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1303 if (fdc->flags & FDC_HAS_FIFO)
1304 (void) enable_fifo(fdc);
1307 /****************************************************************************/
1309 /****************************************************************************/
1311 * FDC IO functions, take care of the main status register, timeout
1312 * in case the desired status bits are never set.
1314 * These PIO loops initially start out with short delays between
1315 * each iteration in the expectation that the required condition
1316 * is usually met quickly, so it can be handled immediately. After
1317 * about 1 ms, stepping is increased to achieve a better timing
1318 * accuracy in the calls to DELAY().
1321 fd_in(struct fdc_data *fdc, int *ptr)
1325 for (j = 0, step = 1;
1326 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) &&
1330 return (fdc_err(fdc, "ready for output in input\n"));
1335 if (j >= FDSTS_TIMEOUT)
1336 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
1339 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1342 #else /* !FDC_DEBUG */
1347 #endif /* FDC_DEBUG */
1351 out_fdc(struct fdc_data *fdc, int x)
1355 for (j = 0, step = 1;
1356 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != NE7_RQM &&
1359 if (i == (NE7_DIO|NE7_RQM))
1360 return (fdc_err(fdc, "ready for input in output\n"));
1365 if (j >= FDSTS_TIMEOUT)
1366 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
1368 /* Send the command and return */
1370 TRACE1("[0x%x->FDDATA]", x);
1374 /****************************************************************************/
1375 /* fdopen/fdclose */
1376 /****************************************************************************/
1378 Fdopen(dev_t dev, int flags, int mode, struct thread *td)
1380 fdu_t fdu = FDUNIT(minor(dev));
1381 int type = FDTYPE(minor(dev));
1386 if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1389 if ((fdc == NULL) || (fd->type == NO_TYPE))
1397 * For each type of basic drive, make sure we are trying
1398 * to open a type it can do,
1400 if (type != fd->type) {
1414 type = FD_1480in5_25;
1417 type = FD_1440in5_25;
1422 type = FD_820in5_25;
1425 type = FD_800in5_25;
1428 type = FD_720in5_25;
1431 type = FD_640in5_25;
1434 type = FD_360in5_25;
1441 if ( type != FD_1720
1454 fd->ft = fd_types + type - 1;
1455 fd->flags |= FD_OPEN;
1457 * Clearing the DMA overrun counter at open time is a bit messy.
1458 * Since we're only managing one counter per controller, opening
1459 * the second drive could mess it up. Anyway, if the DMA overrun
1460 * condition is really persistent, it will eventually time out
1461 * still. OTOH, clearing it here will ensure we'll at least start
1462 * trying again after a previous (maybe even long ago) failure.
1463 * Also, this is merely a stop-gap measure only that should not
1464 * happen during normal operation, so we can tolerate it to be a
1465 * bit sloppy about this.
1467 fdc->dma_overruns = 0;
1473 fdclose(dev_t dev, int flags, int mode, struct thread *td)
1475 fdu_t fdu = FDUNIT(minor(dev));
1478 fd = devclass_get_softc(fd_devclass, fdu);
1479 fd->flags &= ~FD_OPEN;
1480 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG);
1485 /****************************************************************************/
1487 /****************************************************************************/
1489 fdstrategy(struct buf *bp)
1491 unsigned nblocks, blknum, cando;
1498 fdu = FDUNIT(minor(bp->b_dev));
1499 fd = devclass_get_softc(fd_devclass, fdu);
1501 panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
1502 (u_long)major(bp->b_dev), (u_long)minor(bp->b_dev));
1504 if (fd->type == NO_TYPE) {
1505 bp->b_error = ENXIO;
1506 bp->b_flags |= B_ERROR;
1510 fdblk = 128 << (fd->ft->secsize);
1511 if (!(bp->b_flags & B_FORMAT)) {
1512 if (bp->b_blkno < 0) {
1514 "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1515 fdu, (u_long)bp->b_blkno, bp->b_bcount);
1516 bp->b_error = EINVAL;
1517 bp->b_flags |= B_ERROR;
1520 if ((bp->b_bcount % fdblk) != 0) {
1521 bp->b_error = EINVAL;
1522 bp->b_flags |= B_ERROR;
1528 * Set up block calculations.
1530 if (bp->b_blkno > 20000000) {
1532 * Reject unreasonably high block number, prevent the
1533 * multiplication below from overflowing.
1535 bp->b_error = EINVAL;
1536 bp->b_flags |= B_ERROR;
1539 blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk;
1540 nblocks = fd->ft->size;
1542 if (blknum + (bp->b_bcount / fdblk) > nblocks) {
1543 if (blknum <= nblocks) {
1544 cando = (nblocks - blknum) * fdblk;
1545 bp->b_resid = bp->b_bcount - cando;
1547 goto bad; /* not actually bad but EOF */
1549 bp->b_error = EINVAL;
1550 bp->b_flags |= B_ERROR;
1554 bp->b_pblkno = bp->b_blkno;
1556 bufqdisksort(&fdc->head, bp);
1557 untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1559 /* Tell devstat we are starting on the transaction */
1560 devstat_start_transaction(&fd->device_stats);
1561 device_busy(fd->dev);
1571 /***************************************************************\
1573 * We have just queued something.. if the controller is not busy *
1574 * then simulate the case where it has just finished a command *
1575 * So that it (the interrupt routine) looks on the queue for more*
1576 * work to do and picks up what we just added. *
1577 * If the controller is already busy, we need do nothing, as it *
1578 * will pick up our work when the present work completes *
1579 \***************************************************************/
1581 fdstart(struct fdc_data *fdc)
1586 if(fdc->state == DEVIDLE)
1594 fd_iotimeout(void *xfdc)
1600 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
1603 * Due to IBM's brain-dead design, the FDC has a faked ready
1604 * signal, hardwired to ready == true. Thus, any command
1605 * issued if there's no diskette in the drive will _never_
1606 * complete, and must be aborted by resetting the FDC.
1607 * Many thanks, Big Blue!
1608 * The FDC must not be reset directly, since that would
1609 * interfere with the state machine. Instead, pretend that
1610 * the command completed but was invalid. The state machine
1611 * will reset the FDC and retry once.
1614 fdc->status[0] = NE7_ST0_IC_IV;
1615 fdc->flags &= ~FDC_STAT_VALID;
1616 fdc->state = IOTIMEDOUT;
1621 /* just ensure it has the right spl */
1623 fd_pseudointr(void *xfdc)
1632 /***********************************************************************\
1634 * keep calling the state machine until it returns a 0 *
1635 * ALWAYS called at SPLBIO *
1636 \***********************************************************************/
1638 fdc_intr(void *xfdc)
1646 * magic pseudo-DMA initialization for YE FDC. Sets count and
1649 #define SET_BCDR(fdc,wr,cnt,port) \
1650 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \
1651 ((cnt)-1) & 0xff); \
1652 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
1653 ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
1656 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy
1658 static int fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
1660 u_char *cptr = (u_char *)addr;
1662 if (flags & B_READ) {
1663 if (fdc->state != PIOREAD) {
1664 fdc->state = PIOREAD;
1667 SET_BCDR(fdc, 0, count, 0);
1668 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1669 FDC_YE_DATAPORT, cptr, count);
1671 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1672 FDC_YE_DATAPORT, cptr, count);
1673 SET_BCDR(fdc, 0, count, 0);
1678 /***********************************************************************\
1679 * The controller state machine. *
1680 * if it returns a non zero value, it should be called again immediatly *
1681 \***********************************************************************/
1685 int read, format, head, i, sec = 0, sectrac, st0, cyl, st3;
1686 unsigned blknum = 0, b_cylinder = 0;
1687 fdu_t fdu = fdc->fdu;
1689 register struct buf *bp;
1690 struct fd_formb *finfo = NULL;
1695 bp = bufq_first(&fdc->head);
1697 bufq_remove(&fdc->head, bp);
1702 /***********************************************\
1703 * nothing left for this controller to do *
1704 * Force into the IDLE state, *
1705 \***********************************************/
1706 fdc->state = DEVIDLE;
1708 device_printf(fdc->fdc_dev,
1709 "unexpected valid fd pointer\n");
1713 TRACE1("[fdc%d IDLE]", fdc->fdcu);
1716 fdu = FDUNIT(minor(bp->b_dev));
1717 fd = devclass_get_softc(fd_devclass, fdu);
1718 fdblk = 128 << fd->ft->secsize;
1719 if (fdc->fd && (fd != fdc->fd))
1720 device_printf(fd->dev, "confused fd pointers\n");
1721 read = bp->b_flags & B_READ;
1722 format = bp->b_flags & B_FORMAT;
1724 finfo = (struct fd_formb *)bp->b_data;
1725 fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1728 if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
1729 blknum = (unsigned) bp->b_pblkno * DEV_BSIZE/fdblk +
1731 b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1733 TRACE1("fd%d", fdu);
1734 TRACE1("[%s]", fdstates[fdc->state]);
1735 TRACE1("(0x%x)", fd->flags);
1736 untimeout(fd_turnoff, fd, fd->toffhandle);
1737 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
1741 case FINDWORK: /* we have found new work */
1746 fdc->fdctl_wr(fdc, fd->ft->trans);
1747 TRACE1("[0x%x->FDCTL]", fd->ft->trans);
1748 /*******************************************************\
1749 * If the next drive has a motor startup pending, then *
1750 * it will start up in its own good time *
1751 \*******************************************************/
1752 if(fd->flags & FD_MOTOR_WAIT) {
1753 fdc->state = MOTORWAIT;
1754 return (0); /* come back later */
1756 /*******************************************************\
1757 * Maybe if it's not starting, it SHOULD be starting *
1758 \*******************************************************/
1759 if (!(fd->flags & FD_MOTOR))
1761 fdc->state = MOTORWAIT;
1765 else /* at least make sure we are selected */
1767 set_motor(fdc, fd->fdsu, TURNON);
1769 if (fdc->flags & FDC_NEEDS_RESET) {
1770 fdc->state = RESETCTLR;
1771 fdc->flags &= ~FDC_NEEDS_RESET;
1773 fdc->state = DOSEEK;
1776 if (b_cylinder == (unsigned)fd->track)
1778 fdc->state = SEEKCOMPLETE;
1781 if (fd_cmd(fdc, 3, NE7CMD_SEEK,
1782 fd->fdsu, b_cylinder * fd->ft->steptrac,
1786 * seek command not accepted, looks like
1787 * the FDC went off to the Saints...
1789 fdc->retry = 6; /* try a reset */
1790 return(retrier(fdc));
1792 fd->track = FD_NO_TRACK;
1793 fdc->state = SEEKWAIT;
1794 return(0); /* will return later */
1796 /* allow heads to settle */
1797 timeout(fd_pseudointr, fdc, hz / 16);
1798 fdc->state = SEEKCOMPLETE;
1799 return(0); /* will return later */
1800 case SEEKCOMPLETE : /* SEEK DONE, START DMA */
1801 /* Make sure seek really happened*/
1802 if(fd->track == FD_NO_TRACK) {
1803 int descyl = b_cylinder * fd->ft->steptrac;
1806 * This might be a "ready changed" interrupt,
1807 * which cannot really happen since the
1808 * RDY pin is hardwired to + 5 volts. This
1809 * generally indicates a "bouncing" intr
1810 * line, so do one of the following:
1812 * When running on an enhanced FDC that is
1813 * known to not go stuck after responding
1814 * with INVALID, fetch all interrupt states
1815 * until seeing either an INVALID or a
1816 * real interrupt condition.
1818 * When running on a dumb old NE765, give
1819 * up immediately. The controller will
1820 * provide up to four dummy RC interrupt
1821 * conditions right after reset (for the
1822 * corresponding four drives), so this is
1823 * our only chance to get notice that it
1824 * was not the FDC that caused the interrupt.
1826 if (fd_sense_int(fdc, &st0, &cyl)
1829 if(fdc->fdct == FDC_NE765
1830 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1831 return 0; /* hope for a real intr */
1832 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1837 * seek to cyl 0 requested; make sure we are
1840 if (fd_sense_drive_status(fdc, &st3))
1842 if ((st3 & NE7_ST3_T0) == 0) {
1844 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
1845 fdu, st3, NE7_ST3BITS);
1852 return (retrier(fdc));
1856 if (cyl != descyl) {
1858 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
1859 fdu, descyl, cyl, st0);
1862 return (retrier(fdc));
1866 fd->track = b_cylinder;
1867 if (!(fdc->flags & FDC_NODMA))
1868 isa_dmastart(bp->b_flags, bp->b_data+fd->skip,
1869 format ? bp->b_bcount : fdblk, fdc->dmachan);
1870 sectrac = fd->ft->sectrac;
1871 sec = blknum % (sectrac * fd->ft->heads);
1872 head = sec / sectrac;
1873 sec = sec % sectrac + 1;
1874 fd->hddrv = ((head&1)<<2)+fdu;
1878 /* make sure the drive is writable */
1879 if(fd_sense_drive_status(fdc, &st3) != 0)
1881 /* stuck controller? */
1882 if (!(fdc->flags & FDC_NODMA))
1883 isa_dmadone(bp->b_flags,
1884 bp->b_data + fd->skip,
1885 format ? bp->b_bcount : fdblk,
1887 fdc->retry = 6; /* reset the beast */
1888 return (retrier(fdc));
1890 if(st3 & NE7_ST3_WP)
1893 * XXX YES! this is ugly.
1894 * in order to force the current operation
1895 * to fail, we will have to fake an FDC
1896 * error - all error handling is done
1899 fdc->status[0] = NE7_ST0_IC_AT;
1900 fdc->status[1] = NE7_ST1_NW;
1902 fdc->status[3] = fd->track;
1903 fdc->status[4] = head;
1904 fdc->status[5] = sec;
1905 fdc->retry = 8; /* break out immediately */
1906 fdc->state = IOTIMEDOUT; /* not really... */
1912 if (fdc->flags & FDC_NODMA) {
1914 * This seems to be necessary for
1915 * whatever obscure reason; if we omit
1916 * it, we end up filling the sector ID
1917 * fields of the newly formatted track
1918 * entirely with garbage, causing
1919 * `wrong cylinder' errors all over
1920 * the place when trying to read them
1925 SET_BCDR(fdc, 1, bp->b_bcount, 0);
1927 (void)fdcpio(fdc,bp->b_flags,
1928 bp->b_data+fd->skip,
1933 if(fd_cmd(fdc, 6, NE7CMD_FORMAT, head << 2 | fdu,
1934 finfo->fd_formb_secshift,
1935 finfo->fd_formb_nsecs,
1936 finfo->fd_formb_gaplen,
1937 finfo->fd_formb_fillbyte, 0)) {
1938 /* controller fell over */
1939 if (!(fdc->flags & FDC_NODMA))
1940 isa_dmadone(bp->b_flags,
1941 bp->b_data + fd->skip,
1942 format ? bp->b_bcount : fdblk,
1945 return (retrier(fdc));
1948 if (fdc->flags & FDC_NODMA) {
1950 * this seems to be necessary even when
1953 SET_BCDR(fdc, 1, fdblk, 0);
1956 * perform the write pseudo-DMA before
1957 * the WRITE command is sent
1960 (void)fdcpio(fdc,bp->b_flags,
1961 bp->b_data+fd->skip,
1965 (read ? NE7CMD_READ : NE7CMD_WRITE),
1966 head << 2 | fdu, /* head & unit */
1967 fd->track, /* track */
1969 sec, /* sector + 1 */
1970 fd->ft->secsize, /* sector size */
1971 sectrac, /* sectors/track */
1972 fd->ft->gap, /* gap size */
1973 fd->ft->datalen, /* data length */
1975 /* the beast is sleeping again */
1976 if (!(fdc->flags & FDC_NODMA))
1977 isa_dmadone(bp->b_flags,
1978 bp->b_data + fd->skip,
1979 format ? bp->b_bcount : fdblk,
1982 return (retrier(fdc));
1985 if (fdc->flags & FDC_NODMA)
1987 * if this is a read, then simply await interrupt
1988 * before performing PIO
1990 if (read && !fdcpio(fdc,bp->b_flags,
1991 bp->b_data+fd->skip,fdblk)) {
1992 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
1993 return(0); /* will return later */
1997 * write (or format) operation will fall through and
1998 * await completion interrupt
2000 fdc->state = IOCOMPLETE;
2001 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
2002 return (0); /* will return later */
2005 * actually perform the PIO read. The IOCOMPLETE case
2006 * removes the timeout for us.
2008 (void)fdcpio(fdc,bp->b_flags,bp->b_data+fd->skip,fdblk);
2009 fdc->state = IOCOMPLETE;
2011 case IOCOMPLETE: /* IO DONE, post-analyze */
2012 untimeout(fd_iotimeout, fdc, fd->tohandle);
2014 if (fd_read_status(fdc, fd->fdsu)) {
2015 if (!(fdc->flags & FDC_NODMA))
2016 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
2017 format ? bp->b_bcount : fdblk,
2020 fdc->retry = 6; /* force a reset */
2021 return (retrier(fdc));
2024 fdc->state = IOTIMEDOUT;
2029 if (!(fdc->flags & FDC_NODMA))
2030 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
2031 format ? bp->b_bcount : fdblk, fdc->dmachan);
2032 if (fdc->status[0] & NE7_ST0_IC) {
2033 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2034 && fdc->status[1] & NE7_ST1_OR) {
2036 * DMA overrun. Someone hogged the bus and
2037 * didn't release it in time for the next
2040 * We normally restart this without bumping
2041 * the retry counter. However, in case
2042 * something is seriously messed up (like
2043 * broken hardware), we rather limit the
2044 * number of retries so the IO operation
2045 * doesn't block indefinately.
2047 if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
2048 fdc->state = SEEKCOMPLETE;
2050 } /* else fall through */
2052 if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
2054 fdc->retry = 6; /* force a reset */
2055 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2056 && fdc->status[2] & NE7_ST2_WC
2058 fdc->retry = 3; /* force recalibrate */
2059 return (retrier(fdc));
2062 /* Operation successful, retry DMA overruns again next time. */
2063 fdc->dma_overruns = 0;
2065 if (!format && fd->skip < bp->b_bcount - bp->b_resid) {
2066 /* set up next transfer */
2067 fdc->state = DOSEEK;
2072 device_unbusy(fd->dev);
2073 devstat_end_transaction_buf(&fd->device_stats, bp);
2077 fdc->state = FINDWORK;
2083 fdc->state = RESETCOMPLETE;
2087 * Discard all the results from the reset so that they
2088 * can't cause an unexpected interrupt later.
2090 for (i = 0; i < 4; i++)
2091 (void)fd_sense_int(fdc, &st0, &cyl);
2092 fdc->state = STARTRECAL;
2095 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2098 return (retrier(fdc));
2100 fdc->state = RECALWAIT;
2101 return (0); /* will return later */
2103 /* allow heads to settle */
2104 timeout(fd_pseudointr, fdc, hz / 8);
2105 fdc->state = RECALCOMPLETE;
2106 return (0); /* will return later */
2110 * See SEEKCOMPLETE for a comment on this:
2112 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2114 if(fdc->fdct == FDC_NE765
2115 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2116 return 0; /* hope for a real intr */
2117 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2118 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
2122 * a recalibrate from beyond cylinder 77
2123 * will "fail" due to the FDC limitations;
2124 * since people used to complain much about
2125 * the failure message, try not logging
2126 * this one if it seems to be the first
2129 printf("fd%d: recal failed ST0 %b cyl %d\n",
2130 fdu, st0, NE7_ST0BITS, cyl);
2131 if(fdc->retry < 3) fdc->retry = 3;
2132 return (retrier(fdc));
2135 /* Seek (probably) necessary */
2136 fdc->state = DOSEEK;
2137 return (1); /* will return immediatly */
2139 if(fd->flags & FD_MOTOR_WAIT)
2141 return (0); /* time's not up yet */
2143 if (fdc->flags & FDC_NEEDS_RESET) {
2144 fdc->state = RESETCTLR;
2145 fdc->flags &= ~FDC_NEEDS_RESET;
2148 * If all motors were off, then the controller was
2149 * reset, so it has lost track of the current
2150 * cylinder. Recalibrate to handle this case.
2151 * But first, discard the results of the reset.
2153 fdc->state = RESETCOMPLETE;
2155 return (1); /* will return immediatly */
2157 device_printf(fdc->fdc_dev, "unexpected FD int->");
2158 if (fd_read_status(fdc, fd->fdsu) == 0)
2159 printf("FDC status :%x %x %x %x %x %x %x ",
2168 printf("No status available ");
2169 if (fd_sense_int(fdc, &st0, &cyl) != 0)
2171 printf("[controller is dead now]\n");
2174 printf("ST0 = %x, PCN = %x\n", st0, cyl);
2177 /*XXX confusing: some branches return immediately, others end up here*/
2178 return (1); /* Come back immediatly to new state */
2182 retrier(struct fdc_data *fdc)
2184 register struct buf *bp;
2190 /* XXX shouldn't this be cached somewhere? */
2191 fdu = FDUNIT(minor(bp->b_dev));
2192 fd = devclass_get_softc(fd_devclass, fdu);
2193 if (fd->options & FDOPT_NORETRY)
2196 switch (fdc->retry) {
2197 case 0: case 1: case 2:
2198 fdc->state = SEEKCOMPLETE;
2200 case 3: case 4: case 5:
2201 fdc->state = STARTRECAL;
2204 fdc->state = RESETCTLR;
2211 int printerror = (fd->options & FDOPT_NOERRLOG) == 0;
2212 dev_t sav_b_dev = bp->b_dev;
2215 bp->b_dev = makedev(major(bp->b_dev),
2216 (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
2218 diskerr(bp, "hard error", LOG_PRINTF,
2219 fdc->fd->skip / DEV_BSIZE,
2220 (struct disklabel *)NULL);
2221 bp->b_dev = sav_b_dev;
2223 if (fdc->flags & FDC_STAT_VALID)
2225 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2226 fdc->status[0], NE7_ST0BITS,
2227 fdc->status[1], NE7_ST1BITS,
2228 fdc->status[2], NE7_ST2BITS,
2229 fdc->status[3], fdc->status[4],
2232 printf(" (No status)\n");
2235 bp->b_flags |= B_ERROR;
2237 bp->b_resid += bp->b_bcount - fdc->fd->skip;
2240 device_unbusy(fd->dev);
2241 devstat_end_transaction_buf(&fdc->fd->device_stats, bp);
2243 fdc->state = FINDWORK;
2244 fdc->flags |= FDC_NEEDS_RESET;
2254 fdformat(dev_t dev, struct fd_formb *finfo, struct thread *td)
2256 struct proc *p = td->td_proc;
2264 fdu = FDUNIT(minor(dev));
2265 fd = devclass_get_softc(fd_devclass, fdu);
2266 fdblk = 128 << fd->ft->secsize;
2268 /* set up a buffer header for fdstrategy() */
2269 bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
2273 * keep the process from being swapped
2276 bzero((void *)bp, sizeof(struct buf));
2278 BUF_LOCK(bp, LK_EXCLUSIVE);
2279 bp->b_flags = B_PHYS | B_FORMAT;
2282 * calculate a fake blkno, so fdstrategy() would initiate a
2283 * seek to the requested cylinder
2285 bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
2286 + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
2288 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2289 bp->b_data = (caddr_t)finfo;
2291 /* now do the format */
2293 BUF_STRATEGY(bp, 0);
2295 /* ...and wait for it to complete */
2297 while(!(bp->b_flags & B_DONE)) {
2298 rv = tsleep((caddr_t)bp, 0, "fdform", 20 * hz);
2299 if (rv == EWOULDBLOCK)
2304 if (rv == EWOULDBLOCK) {
2307 device_unbusy(fd->dev);
2310 if (bp->b_flags & B_ERROR)
2313 * allow the process to be swapped
2323 * TODO: don't allocate buffer on stack.
2327 fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
2329 fdu_t fdu = FDUNIT(minor(dev));
2330 fd_p fd = devclass_get_softc(fd_devclass, fdu);
2333 struct fd_type *fdt;
2334 struct disklabel *dl;
2335 struct fdc_status *fsp;
2336 char buffer[DEV_BSIZE];
2339 fdblk = 128 << fd->ft->secsize;
2343 bzero(buffer, sizeof (buffer));
2344 dl = (struct disklabel *)buffer;
2345 dl->d_secsize = fdblk;
2347 dl->d_secpercyl = fdt->size / fdt->tracks;
2348 dl->d_type = DTYPE_FLOPPY;
2350 if (readdisklabel(dev, dl)
2356 *(struct disklabel *)addr = *dl;
2360 if ((flag & FWRITE) == 0)
2365 if ((flag & FWRITE) == 0)
2370 if ((flag & FWRITE) == 0) {
2375 dl = (struct disklabel *)addr;
2377 if ((error = setdisklabel((struct disklabel *)buffer, dl,
2381 error = writedisklabel(dev, (struct disklabel *)buffer);
2384 if ((flag & FWRITE) == 0)
2385 error = EBADF; /* must be opened for writing */
2386 else if (((struct fd_formb *)addr)->format_version !=
2388 error = EINVAL; /* wrong version of formatting prog */
2390 error = fdformat(dev, (struct fd_formb *)addr, td);
2393 case FD_GTYPE: /* get drive type */
2394 *(struct fd_type *)addr = *fd->ft;
2397 case FD_STYPE: /* set drive type */
2398 /* this is considered harmful; only allow for superuser */
2401 *fd->ft = *(struct fd_type *)addr;
2404 case FD_GOPTS: /* get drive options */
2405 *(int *)addr = fd->options;
2408 case FD_SOPTS: /* set drive options */
2409 fd->options = *(int *)addr;
2413 fsp = (struct fdc_status *)addr;
2414 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2416 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
2427 * Hello emacs, these are the
2430 * c-continued-statement-offset: 8
2431 * c-continued-brace-offset: 0
2432 * c-brace-offset: -8
2433 * c-brace-imaginary-offset: 0
2434 * c-argdecl-indent: 8
2435 * c-label-offset: -8
2436 * c++-hanging-braces: 1
2437 * c++-access-specifier-offset: -8
2438 * c++-empty-arglist-indent: 8
2439 * c++-friend-offset: 0