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.3 2003/06/19 01:55:05 dillon Exp $
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
67 #include <sys/disklabel.h>
68 #include <sys/devicestat.h>
69 #include <sys/fcntl.h>
70 #include <sys/malloc.h>
71 #include <sys/module.h>
73 #include <sys/syslog.h>
76 #include <machine/bus.h>
81 #include <machine/clock.h>
82 #include <machine/ioctl_fd.h>
83 #include <machine/resource.h>
84 #include <machine/stdarg.h>
86 #include <isa/isavar.h>
87 #include <isa/isareg.h>
88 #include <isa/fdreg.h>
92 /* misuse a flag to identify format operation */
93 #define B_FORMAT B_XXX
95 /* configuration flags */
96 #define FDC_PRETEND_D0 (1 << 0) /* pretend drive 0 to be there */
97 #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */
99 /* internally used only, not really from CMOS: */
100 #define RTCFDT_144M_PRETENDED 0x1000
102 /* error returns for fd_cmd() */
104 #define FD_NOT_VALID -2
105 #define FDC_ERRMAX 100 /* do not log more */
107 * Stop retrying after this many DMA overruns. Since each retry takes
108 * one revolution, with 300 rpm., 25 retries take approximately 10
109 * seconds which the read attempt will block in case the DMA overrun
112 #define FDC_DMAOV_MAX 25
115 * Timeout value for the PIO loops to wait until the FDC main status
116 * register matches our expectations (request for master, direction
117 * bit). This is supposed to be a number of microseconds, although
118 * timing might actually not be very accurate.
120 * Timeouts of 100 msec are believed to be required for some broken
123 #define FDSTS_TIMEOUT 100000
126 #define NUMDENS (NUMTYPES - 7)
128 /* These defines (-1) must match index for fd_types */
129 #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */
130 #define NO_TYPE 0 /* must match NO_TYPE in ft.c */
142 #define FD_1480in5_25 11
143 #define FD_1440in5_25 12
144 #define FD_820in5_25 13
145 #define FD_800in5_25 14
146 #define FD_720in5_25 15
147 #define FD_360in5_25 16
148 #define FD_640in5_25 17
151 static struct fd_type fd_types[NUMTYPES] =
153 { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
154 { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
155 { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
156 { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */
157 { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */
158 { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */
159 { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */
160 { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */
161 { 8,2,0xFF,0x2A,80,1280,1,FDC_250KBPS,2,0x50,1 }, /* 640K in DD 5.25in */
162 { 8,3,0xFF,0x35,77,1232,1,FDC_500KBPS,2,0x74,1 }, /* 1.23M in HD 5.25in */
164 { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
165 { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
166 { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */
167 { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */
168 { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */
169 { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */
170 { 8,2,0xFF,0x2A,80,1280,1,FDC_300KBPS,2,0x50,1 }, /* 640K in HD 5.25in */
173 #define DRVS_PER_CTLR 2 /* 2 floppies */
175 /***********************************************************************\
176 * Per controller structure. *
177 \***********************************************************************/
178 static devclass_t fdc_devclass;
180 /***********************************************************************\
181 * Per drive structure. *
182 * N per controller (DRVS_PER_CTLR) *
183 \***********************************************************************/
185 struct fdc_data *fdc; /* pointer to controller structure */
186 int fdsu; /* this units number on this controller */
187 int type; /* Drive type (FD_1440...) */
188 struct fd_type *ft; /* pointer to the type descriptor */
190 #define FD_OPEN 0x01 /* it's open */
191 #define FD_ACTIVE 0x02 /* it's active */
192 #define FD_MOTOR 0x04 /* motor should be on */
193 #define FD_MOTOR_WAIT 0x08 /* motor coming up */
196 #define FD_NO_TRACK -2
197 int track; /* where we think the head is */
198 int options; /* user configurable options, see ioctl_fd.h */
199 struct callout_handle toffhandle;
200 struct callout_handle tohandle;
201 struct devstat device_stats;
209 static devclass_t fd_devclass;
211 /***********************************************************************\
212 * Throughout this file the following conventions will be used: *
213 * fd is a pointer to the fd_data struct for the drive in question *
214 * fdc is a pointer to the fdc_data struct for the controller *
215 * fdu is the floppy drive unit number *
216 * fdcu is the floppy controller unit number *
217 * fdsu is the floppy drive unit number on that controller. (sub-unit) *
218 \***********************************************************************/
220 /* internal functions */
221 static void fdc_intr(void *);
222 static void set_motor(struct fdc_data *, int, int);
225 static timeout_t fd_turnoff;
226 static timeout_t fd_motor_on;
227 static void fd_turnon(struct fd_data *);
228 static void fdc_reset(fdc_p);
229 static int fd_in(struct fdc_data *, int *);
230 static int out_fdc(struct fdc_data *, int);
231 static void fdstart(struct fdc_data *);
232 static timeout_t fd_iotimeout;
233 static timeout_t fd_pseudointr;
234 static int fdstate(struct fdc_data *);
235 static int retrier(struct fdc_data *);
236 static int fdformat(dev_t, struct fd_formb *, struct proc *);
238 static int enable_fifo(fdc_p fdc);
240 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */
246 #define SEEKCOMPLETE 3
248 #define RECALCOMPLETE 5
254 #define IOTIMEDOUT 11
255 #define RESETCOMPLETE 12
259 static char const * const fdstates[] =
277 /* CAUTION: fd_debug causes huge amounts of logging output */
278 static int volatile fd_debug = 0;
279 #define TRACE0(arg) if(fd_debug) printf(arg)
280 #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
281 #else /* FDC_DEBUG */
283 #define TRACE1(arg1, arg2)
284 #endif /* FDC_DEBUG */
287 fdout_wr(fdc_p fdc, u_int8_t v)
289 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
295 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
299 fddata_wr(fdc_p fdc, u_int8_t v)
301 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
307 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
311 fdctl_wr_isa(fdc_p fdc, u_int8_t v)
313 bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
318 fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v)
320 bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v);
329 return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
334 static d_open_t Fdopen; /* NOTE, not fdopen */
335 static d_close_t fdclose;
336 static d_ioctl_t fdioctl;
337 static d_strategy_t fdstrategy;
342 static struct cdevsw fd_cdevsw = {
346 /* write */ physwrite,
350 /* strategy */ fdstrategy,
352 /* maj */ CDEV_MAJOR,
356 /* bmaj */ BDEV_MAJOR
360 fdc_err(struct fdc_data *fdc, const char *s)
364 if (fdc->fdc_errs < FDC_ERRMAX)
365 device_printf(fdc->fdc_dev, "%s", s);
366 else if (fdc->fdc_errs == FDC_ERRMAX)
367 device_printf(fdc->fdc_dev, "too many errors, not "
368 "logging any more\n");
375 * fd_cmd: Send a command to the chip. Takes a varargs with this structure:
377 * # of output bytes, output bytes as ints ...,
378 * # of input bytes, input bytes as ints ...
381 fd_cmd(struct fdc_data *fdc, int n_out, ...)
389 cmd = (u_char)(va_arg(ap, int));
392 for (n = 0; n < n_out; n++)
394 if (out_fdc(fdc, va_arg(ap, int)) < 0)
397 snprintf(msg, sizeof(msg),
398 "cmd %x failed at out byte %d of %d\n",
400 return fdc_err(fdc, msg);
403 n_in = va_arg(ap, int);
404 for (n = 0; n < n_in; n++)
406 int *ptr = va_arg(ap, int *);
407 if (fd_in(fdc, ptr) < 0)
410 snprintf(msg, sizeof(msg),
411 "cmd %02x failed at in byte %d of %d\n",
413 return fdc_err(fdc, msg);
421 enable_fifo(fdc_p fdc)
425 if ((fdc->flags & FDC_HAS_FIFO) == 0) {
429 * Cannot use fd_cmd the normal way here, since
430 * this might be an invalid command. Thus we send the
431 * first byte, and check for an early turn of data directon.
434 if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
435 return fdc_err(fdc, "Enable FIFO failed\n");
437 /* If command is invalid, return */
439 while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
440 != NE7_RQM && j-- > 0) {
441 if (i == (NE7_DIO | NE7_RQM)) {
449 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
451 return fdc_err(fdc, "Enable FIFO failed\n");
453 fdc->flags |= FDC_HAS_FIFO;
457 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
458 return fdc_err(fdc, "Re-enable FIFO failed\n");
463 fd_sense_drive_status(fdc_p fdc, int *st3p)
467 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
469 return fdc_err(fdc, "Sense Drive Status failed\n");
478 fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
482 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
485 "sense intr err reading stat reg 0\n");
492 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
494 * There doesn't seem to have been an interrupt.
499 if (fd_in(fdc, &cyl) < 0) {
500 return fdc_err(fdc, "can't get cyl num\n");
511 fd_read_status(fdc_p fdc, int fdsu)
515 for (i = 0; i < 7; i++) {
517 * XXX types are poorly chosen. Only bytes can by read
518 * from the hardware, but fdc->status[] wants u_ints and
519 * fd_in() gives ints.
523 ret = fd_in(fdc, &status);
524 fdc->status[i] = status;
530 fdc->flags |= FDC_STAT_VALID;
532 fdc->flags &= ~FDC_STAT_VALID;
537 /****************************************************************************/
538 /* autoconfiguration stuff */
539 /****************************************************************************/
542 fdc_alloc_resources(struct fdc_data *fdc)
548 ispnp = (fdc->flags & FDC_ISPNP) != 0;
549 ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
550 fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
551 fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
554 * On standard ISA, we don't just use an 8 port range
555 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
558 * Isn't PC hardware wonderful.
560 * The Y-E Data PCMCIA FDC doesn't have this problem, it
561 * uses the register with offset 6 for pseudo-DMA, and the
562 * one with offset 7 as control register.
564 fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
565 &fdc->rid_ioport, 0ul, ~0ul,
566 ispcmcia ? 8 : (ispnp ? 1 : 6),
568 if (fdc->res_ioport == 0) {
569 device_printf(dev, "cannot reserve I/O port range\n");
572 fdc->portt = rman_get_bustag(fdc->res_ioport);
573 fdc->porth = rman_get_bushandle(fdc->res_ioport);
577 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
578 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
579 * by checking the size and adjust the port address
582 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
586 * Register the control port range as rid 1 if it
587 * isn't there already. Most PnP BIOSen will have
588 * already done this but non-PnP configurations don't.
590 * And some (!!) report 0x3f2-0x3f5 and completely
591 * leave out the control register! It seems that some
592 * non-antique controller chips have a different
593 * method of programming the transfer speed which
594 * doesn't require the control register, but it's
595 * mighty bogus as the chip still responds to the
596 * address for the control register.
598 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
601 /* Find the control port, usually 0x3f7 */
602 ctlstart = rman_get_start(fdc->res_ioport) +
605 bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
609 * Now (finally!) allocate the control port.
612 fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
614 0ul, ~0ul, 1, RF_ACTIVE);
615 if (fdc->res_ctl == 0) {
617 "cannot reserve control I/O port range\n");
620 fdc->ctlt = rman_get_bustag(fdc->res_ctl);
621 fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
624 fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
625 &fdc->rid_irq, 0ul, ~0ul, 1,
627 if (fdc->res_irq == 0) {
628 device_printf(dev, "cannot reserve interrupt line\n");
632 if ((fdc->flags & FDC_NODMA) == 0) {
633 fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
634 &fdc->rid_drq, 0ul, ~0ul, 1,
636 if (fdc->res_drq == 0) {
637 device_printf(dev, "cannot reserve DMA request line\n");
640 fdc->dmachan = fdc->res_drq->r_start;
647 fdc_release_resources(struct fdc_data *fdc)
652 if (fdc->res_irq != 0) {
653 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
655 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
658 if (fdc->res_ctl != 0) {
659 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
661 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
664 if (fdc->res_ioport != 0) {
665 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
667 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
670 if (fdc->res_drq != 0) {
671 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
673 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
678 /****************************************************************************/
679 /* autoconfiguration stuff */
680 /****************************************************************************/
682 static struct isa_pnp_id fdc_ids[] = {
683 {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
684 {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
689 fdc_read_ivar(device_t dev, device_t child, int which, u_long *result)
691 struct fdc_ivars *ivars = device_get_ivars(child);
694 case FDC_IVAR_FDUNIT:
695 *result = ivars->fdunit;
704 * fdc controller section.
707 fdc_probe(device_t dev)
710 struct fdc_data *fdc;
712 fdc = device_get_softc(dev);
713 bzero(fdc, sizeof *fdc);
715 fdc->fdctl_wr = fdctl_wr_isa;
718 error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
722 fdc->flags |= FDC_ISPNP;
724 /* Attempt to allocate our resources for the duration of the probe */
725 error = fdc_alloc_resources(fdc);
729 /* First - lets reset the floppy controller */
732 fdout_wr(fdc, FDO_FRST);
734 /* see if it can handle a command */
735 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
736 NE7_SPEC_2(2, 0), 0)) {
741 if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
742 ic_type = (u_char)ic_type;
745 device_set_desc(dev, "NEC 765 or clone");
746 fdc->fdct = FDC_NE765;
749 device_set_desc(dev, "Intel 82077 or clone");
750 fdc->fdct = FDC_I82077;
753 device_set_desc(dev, "NEC 72065B or clone");
754 fdc->fdct = FDC_NE72065;
757 device_set_desc(dev, "generic floppy controller");
758 fdc->fdct = FDC_UNKNOWN;
764 fdc_release_resources(fdc);
771 fdc_pccard_probe(device_t dev)
774 struct fdc_data *fdc;
776 fdc = device_get_softc(dev);
777 bzero(fdc, sizeof *fdc);
779 fdc->fdctl_wr = fdctl_wr_pcmcia;
781 fdc->flags |= FDC_ISPCMCIA | FDC_NODMA;
783 /* Attempt to allocate our resources for the duration of the probe */
784 error = fdc_alloc_resources(fdc);
788 /* First - lets reset the floppy controller */
791 fdout_wr(fdc, FDO_FRST);
793 /* see if it can handle a command */
794 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
795 NE7_SPEC_2(2, 0), 0)) {
800 device_set_desc(dev, "Y-E Data PCMCIA floppy");
801 fdc->fdct = FDC_NE765;
804 fdc_release_resources(fdc);
809 fdc_pccard_detach(device_t dev)
811 struct fdc_data *fdc;
814 fdc = device_get_softc(dev);
816 /* have our children detached first */
817 if ((error = bus_generic_detach(dev)))
820 if ((fdc->flags & FDC_ATTACHED) == 0) {
821 device_printf(dev, "already unloaded\n");
824 fdc->flags &= ~FDC_ATTACHED;
826 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
828 fdc_release_resources(fdc);
829 device_printf(dev, "unload\n");
833 #endif /* NCARD > 0 */
836 * Add a child device to the fdc controller. It will then be probed etc.
839 fdc_add_child(device_t dev, const char *name, int unit)
842 struct fdc_ivars *ivar;
845 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT);
848 bzero(ivar, sizeof *ivar);
849 if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
851 child = device_add_child(dev, name, unit);
854 device_set_ivars(child, ivar);
855 if (resource_int_value(name, unit, "disabled", &disabled) == 0
857 device_disable(child);
861 fdc_attach(device_t dev)
863 struct fdc_data *fdc;
866 fdc = device_get_softc(dev);
867 error = fdc_alloc_resources(fdc);
869 device_printf(dev, "cannot re-aquire resources\n");
872 error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
873 INTR_TYPE_BIO, fdc_intr, fdc, &fdc->fdc_intr);
875 device_printf(dev, "cannot setup interrupt\n");
878 fdc->fdcu = device_get_unit(dev);
879 fdc->flags |= FDC_ATTACHED;
881 if ((fdc->flags & FDC_NODMA) == 0) {
882 /* Acquire the DMA channel forever, The driver will do the rest */
883 /* XXX should integrate with rman */
884 isa_dma_acquire(fdc->dmachan);
885 isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
887 fdc->state = DEVIDLE;
889 /* reset controller, turn motor off, clear fdout mirror reg */
890 fdout_wr(fdc, ((fdc->fdout = 0)));
891 bufq_init(&fdc->head);
894 * Probe and attach any children. We should probably detect
895 * devices from the BIOS unless overridden.
897 for (i = resource_query_string(-1, "at", device_get_nameunit(dev));
899 i = resource_query_string(i, "at", device_get_nameunit(dev)))
900 fdc_add_child(dev, resource_query_name(i),
901 resource_query_unit(i));
903 return (bus_generic_attach(dev));
907 fdc_print_child(device_t me, device_t child)
911 retval += bus_print_child_header(me, child);
912 retval += printf(" on %s drive %d\n", device_get_nameunit(me),
913 fdc_get_fdunit(child));
918 static device_method_t fdc_methods[] = {
919 /* Device interface */
920 DEVMETHOD(device_probe, fdc_probe),
921 DEVMETHOD(device_attach, fdc_attach),
922 DEVMETHOD(device_detach, bus_generic_detach),
923 DEVMETHOD(device_shutdown, bus_generic_shutdown),
924 DEVMETHOD(device_suspend, bus_generic_suspend),
925 DEVMETHOD(device_resume, bus_generic_resume),
928 DEVMETHOD(bus_print_child, fdc_print_child),
929 DEVMETHOD(bus_read_ivar, fdc_read_ivar),
930 /* Our children never use any other bus interface methods. */
935 static driver_t fdc_driver = {
938 sizeof(struct fdc_data)
941 DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
945 static device_method_t fdc_pccard_methods[] = {
946 /* Device interface */
947 DEVMETHOD(device_probe, fdc_pccard_probe),
948 DEVMETHOD(device_attach, fdc_attach),
949 DEVMETHOD(device_detach, fdc_pccard_detach),
950 DEVMETHOD(device_shutdown, bus_generic_shutdown),
951 DEVMETHOD(device_suspend, bus_generic_suspend),
952 DEVMETHOD(device_resume, bus_generic_resume),
955 DEVMETHOD(bus_print_child, fdc_print_child),
956 DEVMETHOD(bus_read_ivar, fdc_read_ivar),
957 /* Our children never use any other bus interface methods. */
962 static driver_t fdc_pccard_driver = {
965 sizeof(struct fdc_data)
968 DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
970 #endif /* NCARD > 0 */
972 /******************************************************************/
974 * devices attached to the controller section.
977 fd_probe(device_t dev)
982 struct fdc_data *fdc;
984 static int fd_fifo = 0;
986 fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
987 fd = device_get_softc(dev);
988 fdc = device_get_softc(device_get_parent(dev));
990 bzero(fd, sizeof *fd);
994 fd->fdu = device_get_unit(dev);
997 /* look up what bios thinks we have */
1000 if ((fdc->flags & FDC_ISPCMCIA))
1002 else if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0)
1003 fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
1005 fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
1008 fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
1015 fdt = RTCFDT_144M; /* XXX probably */
1018 /* is there a unit? */
1019 if (fdt == RTCFDT_NONE)
1023 set_motor(fdc, fdsu, TURNON);
1024 DELAY(1000000); /* 1 sec */
1026 /* XXX This doesn't work before the first set_motor() */
1027 if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN
1028 && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0
1029 && enable_fifo(fdc) == 0) {
1030 device_printf(device_get_parent(dev),
1031 "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1035 if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0)
1036 && (st3 & NE7_ST3_T0)) {
1037 /* if at track 0, first seek inwards */
1038 /* seek some steps: */
1039 fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0);
1040 DELAY(300000); /* ...wait a moment... */
1041 fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
1044 /* If we're at track 0 first seek inwards. */
1045 if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
1046 /* Seek some steps... */
1047 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1048 /* ...wait a moment... */
1050 /* make ctrlr happy: */
1051 fd_sense_int(fdc, 0, 0);
1055 for (i = 0; i < 2; i++) {
1057 * we must recalibrate twice, just in case the
1058 * heads have been beyond cylinder 76, since most
1059 * FDCs still barf when attempting to recalibrate
1060 * more than 77 steps
1063 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1064 /* a second being enough for full stroke seek*/
1065 DELAY(i == 0 ? 1000000 : 300000);
1067 /* anything responding? */
1068 if (fd_sense_int(fdc, &st0, 0) == 0 &&
1069 (st0 & NE7_ST0_EC) == 0)
1070 break; /* already probed succesfully */
1074 set_motor(fdc, fdsu, TURNOFF);
1076 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
1079 fd->track = FD_NO_TRACK;
1083 callout_handle_init(&fd->toffhandle);
1084 callout_handle_init(&fd->tohandle);
1088 device_set_desc(dev, "1200-KB 5.25\" drive");
1091 case RTCFDT_144M | RTCFDT_144M_PRETENDED:
1092 device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive");
1096 device_set_desc(dev, "1440-KB 3.5\" drive");
1101 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1105 device_set_desc(dev, "360-KB 5.25\" drive");
1109 printf("720-KB 3.5\" drive");
1119 fd_attach(device_t dev)
1128 static int cdevsw_add_done = 0;
1130 fd = device_get_softc(dev);
1132 if (!cdevsw_add_done) {
1133 cdevsw_add(&fd_cdevsw); /* XXX */
1136 make_dev(&fd_cdevsw, (fd->fdu << 6),
1137 UID_ROOT, GID_OPERATOR, 0640, "rfd%d", fd->fdu);
1140 /* Other make_dev() go here. */
1144 * Export the drive to the devstat interface.
1146 devstat_add_entry(&fd->device_stats, device_get_name(dev),
1147 device_get_unit(dev), 512, DEVSTAT_NO_ORDERED_TAGS,
1148 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
1149 DEVSTAT_PRIORITY_FD);
1154 fd_detach(device_t dev)
1158 fd = device_get_softc(dev);
1159 untimeout(fd_turnoff, fd, fd->toffhandle);
1164 static device_method_t fd_methods[] = {
1165 /* Device interface */
1166 DEVMETHOD(device_probe, fd_probe),
1167 DEVMETHOD(device_attach, fd_attach),
1168 DEVMETHOD(device_detach, fd_detach),
1169 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1170 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
1171 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
1176 static driver_t fd_driver = {
1179 sizeof(struct fd_data)
1182 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
1184 /****************************************************************************/
1185 /* motor control stuff */
1186 /* remember to not deselect the drive we're working on */
1187 /****************************************************************************/
1189 set_motor(struct fdc_data *fdc, int fdsu, int turnon)
1191 int fdout = fdc->fdout;
1192 int needspecify = 0;
1195 fdout &= ~FDO_FDSEL;
1196 fdout |= (FDO_MOEN0 << fdsu) + fdsu;
1198 fdout &= ~(FDO_MOEN0 << fdsu);
1201 && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
1202 /* gonna turn off the last drive, put FDC to bed */
1203 fdout &= ~ (FDO_FRST|FDO_FDMAEN);
1205 /* make sure controller is selected and specified */
1206 if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
1208 fdout |= (FDO_FRST|FDO_FDMAEN);
1211 fdout_wr(fdc, fdout);
1213 TRACE1("[0x%x->FDOUT]", fdout);
1218 * special case: since we have just woken up the FDC
1219 * from its sleep, we silently assume the command will
1220 * be accepted, and do not test for a timeout
1222 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1223 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1225 if (fdc->flags & FDC_HAS_FIFO)
1226 (void) enable_fifo(fdc);
1231 fd_turnoff(void *xfd)
1236 TRACE1("[fd%d: turnoff]", fd->fdu);
1240 * Don't turn off the motor yet if the drive is active.
1242 * If we got here, this could only mean we missed an interrupt.
1243 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
1244 * after a controller reset. Just schedule a pseudo-interrupt
1245 * so the state machine gets re-entered.
1247 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
1253 fd->flags &= ~FD_MOTOR;
1254 set_motor(fd->fdc, fd->fdsu, TURNOFF);
1259 fd_motor_on(void *xfd)
1265 fd->flags &= ~FD_MOTOR_WAIT;
1266 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
1276 if(!(fd->flags & FD_MOTOR))
1278 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
1279 set_motor(fd->fdc, fd->fdsu, TURNON);
1280 timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
1285 fdc_reset(fdc_p fdc)
1287 /* Try a reset, keep motor on */
1288 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1289 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1291 /* enable FDC, but defer interrupts a moment */
1292 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
1293 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
1295 fdout_wr(fdc, fdc->fdout);
1296 TRACE1("[0x%x->FDOUT]", fdc->fdout);
1298 /* XXX after a reset, silently believe the FDC will accept commands */
1299 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1300 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1302 if (fdc->flags & FDC_HAS_FIFO)
1303 (void) enable_fifo(fdc);
1306 /****************************************************************************/
1308 /****************************************************************************/
1310 * FDC IO functions, take care of the main status register, timeout
1311 * in case the desired status bits are never set.
1313 * These PIO loops initially start out with short delays between
1314 * each iteration in the expectation that the required condition
1315 * is usually met quickly, so it can be handled immediately. After
1316 * about 1 ms, stepping is increased to achieve a better timing
1317 * accuracy in the calls to DELAY().
1320 fd_in(struct fdc_data *fdc, int *ptr)
1324 for (j = 0, step = 1;
1325 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) &&
1329 return (fdc_err(fdc, "ready for output in input\n"));
1334 if (j >= FDSTS_TIMEOUT)
1335 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
1338 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1341 #else /* !FDC_DEBUG */
1346 #endif /* FDC_DEBUG */
1350 out_fdc(struct fdc_data *fdc, int x)
1354 for (j = 0, step = 1;
1355 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != NE7_RQM &&
1358 if (i == (NE7_DIO|NE7_RQM))
1359 return (fdc_err(fdc, "ready for input in output\n"));
1364 if (j >= FDSTS_TIMEOUT)
1365 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
1367 /* Send the command and return */
1369 TRACE1("[0x%x->FDDATA]", x);
1373 /****************************************************************************/
1374 /* fdopen/fdclose */
1375 /****************************************************************************/
1377 Fdopen(dev_t dev, int flags, int mode, struct proc *p)
1379 fdu_t fdu = FDUNIT(minor(dev));
1380 int type = FDTYPE(minor(dev));
1385 if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1388 if ((fdc == NULL) || (fd->type == NO_TYPE))
1396 * For each type of basic drive, make sure we are trying
1397 * to open a type it can do,
1399 if (type != fd->type) {
1413 type = FD_1480in5_25;
1416 type = FD_1440in5_25;
1421 type = FD_820in5_25;
1424 type = FD_800in5_25;
1427 type = FD_720in5_25;
1430 type = FD_640in5_25;
1433 type = FD_360in5_25;
1440 if ( type != FD_1720
1453 fd->ft = fd_types + type - 1;
1454 fd->flags |= FD_OPEN;
1456 * Clearing the DMA overrun counter at open time is a bit messy.
1457 * Since we're only managing one counter per controller, opening
1458 * the second drive could mess it up. Anyway, if the DMA overrun
1459 * condition is really persistent, it will eventually time out
1460 * still. OTOH, clearing it here will ensure we'll at least start
1461 * trying again after a previous (maybe even long ago) failure.
1462 * Also, this is merely a stop-gap measure only that should not
1463 * happen during normal operation, so we can tolerate it to be a
1464 * bit sloppy about this.
1466 fdc->dma_overruns = 0;
1472 fdclose(dev_t dev, int flags, int mode, struct proc *p)
1474 fdu_t fdu = FDUNIT(minor(dev));
1477 fd = devclass_get_softc(fd_devclass, fdu);
1478 fd->flags &= ~FD_OPEN;
1479 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG);
1484 /****************************************************************************/
1486 /****************************************************************************/
1488 fdstrategy(struct buf *bp)
1490 unsigned nblocks, blknum, cando;
1497 fdu = FDUNIT(minor(bp->b_dev));
1498 fd = devclass_get_softc(fd_devclass, fdu);
1500 panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
1501 (u_long)major(bp->b_dev), (u_long)minor(bp->b_dev));
1503 if (fd->type == NO_TYPE) {
1504 bp->b_error = ENXIO;
1505 bp->b_flags |= B_ERROR;
1509 fdblk = 128 << (fd->ft->secsize);
1510 if (!(bp->b_flags & B_FORMAT)) {
1511 if (bp->b_blkno < 0) {
1513 "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1514 fdu, (u_long)bp->b_blkno, bp->b_bcount);
1515 bp->b_error = EINVAL;
1516 bp->b_flags |= B_ERROR;
1519 if ((bp->b_bcount % fdblk) != 0) {
1520 bp->b_error = EINVAL;
1521 bp->b_flags |= B_ERROR;
1527 * Set up block calculations.
1529 if (bp->b_blkno > 20000000) {
1531 * Reject unreasonably high block number, prevent the
1532 * multiplication below from overflowing.
1534 bp->b_error = EINVAL;
1535 bp->b_flags |= B_ERROR;
1538 blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk;
1539 nblocks = fd->ft->size;
1541 if (blknum + (bp->b_bcount / fdblk) > nblocks) {
1542 if (blknum <= nblocks) {
1543 cando = (nblocks - blknum) * fdblk;
1544 bp->b_resid = bp->b_bcount - cando;
1546 goto bad; /* not actually bad but EOF */
1548 bp->b_error = EINVAL;
1549 bp->b_flags |= B_ERROR;
1553 bp->b_pblkno = bp->b_blkno;
1555 bufqdisksort(&fdc->head, bp);
1556 untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1558 /* Tell devstat we are starting on the transaction */
1559 devstat_start_transaction(&fd->device_stats);
1560 device_busy(fd->dev);
1570 /***************************************************************\
1572 * We have just queued something.. if the controller is not busy *
1573 * then simulate the case where it has just finished a command *
1574 * So that it (the interrupt routine) looks on the queue for more*
1575 * work to do and picks up what we just added. *
1576 * If the controller is already busy, we need do nothing, as it *
1577 * will pick up our work when the present work completes *
1578 \***************************************************************/
1580 fdstart(struct fdc_data *fdc)
1585 if(fdc->state == DEVIDLE)
1593 fd_iotimeout(void *xfdc)
1599 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
1602 * Due to IBM's brain-dead design, the FDC has a faked ready
1603 * signal, hardwired to ready == true. Thus, any command
1604 * issued if there's no diskette in the drive will _never_
1605 * complete, and must be aborted by resetting the FDC.
1606 * Many thanks, Big Blue!
1607 * The FDC must not be reset directly, since that would
1608 * interfere with the state machine. Instead, pretend that
1609 * the command completed but was invalid. The state machine
1610 * will reset the FDC and retry once.
1613 fdc->status[0] = NE7_ST0_IC_IV;
1614 fdc->flags &= ~FDC_STAT_VALID;
1615 fdc->state = IOTIMEDOUT;
1620 /* just ensure it has the right spl */
1622 fd_pseudointr(void *xfdc)
1631 /***********************************************************************\
1633 * keep calling the state machine until it returns a 0 *
1634 * ALWAYS called at SPLBIO *
1635 \***********************************************************************/
1637 fdc_intr(void *xfdc)
1645 * magic pseudo-DMA initialization for YE FDC. Sets count and
1648 #define SET_BCDR(fdc,wr,cnt,port) \
1649 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \
1650 ((cnt)-1) & 0xff); \
1651 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
1652 ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
1655 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy
1657 static int fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
1659 u_char *cptr = (u_char *)addr;
1661 if (flags & B_READ) {
1662 if (fdc->state != PIOREAD) {
1663 fdc->state = PIOREAD;
1666 SET_BCDR(fdc, 0, count, 0);
1667 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1668 FDC_YE_DATAPORT, cptr, count);
1670 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1671 FDC_YE_DATAPORT, cptr, count);
1672 SET_BCDR(fdc, 0, count, 0);
1677 /***********************************************************************\
1678 * The controller state machine. *
1679 * if it returns a non zero value, it should be called again immediatly *
1680 \***********************************************************************/
1684 int read, format, head, i, sec = 0, sectrac, st0, cyl, st3;
1685 unsigned blknum = 0, b_cylinder = 0;
1686 fdu_t fdu = fdc->fdu;
1688 register struct buf *bp;
1689 struct fd_formb *finfo = NULL;
1694 bp = bufq_first(&fdc->head);
1696 bufq_remove(&fdc->head, bp);
1701 /***********************************************\
1702 * nothing left for this controller to do *
1703 * Force into the IDLE state, *
1704 \***********************************************/
1705 fdc->state = DEVIDLE;
1707 device_printf(fdc->fdc_dev,
1708 "unexpected valid fd pointer\n");
1712 TRACE1("[fdc%d IDLE]", fdc->fdcu);
1715 fdu = FDUNIT(minor(bp->b_dev));
1716 fd = devclass_get_softc(fd_devclass, fdu);
1717 fdblk = 128 << fd->ft->secsize;
1718 if (fdc->fd && (fd != fdc->fd))
1719 device_printf(fd->dev, "confused fd pointers\n");
1720 read = bp->b_flags & B_READ;
1721 format = bp->b_flags & B_FORMAT;
1723 finfo = (struct fd_formb *)bp->b_data;
1724 fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1727 if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
1728 blknum = (unsigned) bp->b_pblkno * DEV_BSIZE/fdblk +
1730 b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1732 TRACE1("fd%d", fdu);
1733 TRACE1("[%s]", fdstates[fdc->state]);
1734 TRACE1("(0x%x)", fd->flags);
1735 untimeout(fd_turnoff, fd, fd->toffhandle);
1736 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
1740 case FINDWORK: /* we have found new work */
1745 fdc->fdctl_wr(fdc, fd->ft->trans);
1746 TRACE1("[0x%x->FDCTL]", fd->ft->trans);
1747 /*******************************************************\
1748 * If the next drive has a motor startup pending, then *
1749 * it will start up in its own good time *
1750 \*******************************************************/
1751 if(fd->flags & FD_MOTOR_WAIT) {
1752 fdc->state = MOTORWAIT;
1753 return (0); /* come back later */
1755 /*******************************************************\
1756 * Maybe if it's not starting, it SHOULD be starting *
1757 \*******************************************************/
1758 if (!(fd->flags & FD_MOTOR))
1760 fdc->state = MOTORWAIT;
1764 else /* at least make sure we are selected */
1766 set_motor(fdc, fd->fdsu, TURNON);
1768 if (fdc->flags & FDC_NEEDS_RESET) {
1769 fdc->state = RESETCTLR;
1770 fdc->flags &= ~FDC_NEEDS_RESET;
1772 fdc->state = DOSEEK;
1775 if (b_cylinder == (unsigned)fd->track)
1777 fdc->state = SEEKCOMPLETE;
1780 if (fd_cmd(fdc, 3, NE7CMD_SEEK,
1781 fd->fdsu, b_cylinder * fd->ft->steptrac,
1785 * seek command not accepted, looks like
1786 * the FDC went off to the Saints...
1788 fdc->retry = 6; /* try a reset */
1789 return(retrier(fdc));
1791 fd->track = FD_NO_TRACK;
1792 fdc->state = SEEKWAIT;
1793 return(0); /* will return later */
1795 /* allow heads to settle */
1796 timeout(fd_pseudointr, fdc, hz / 16);
1797 fdc->state = SEEKCOMPLETE;
1798 return(0); /* will return later */
1799 case SEEKCOMPLETE : /* SEEK DONE, START DMA */
1800 /* Make sure seek really happened*/
1801 if(fd->track == FD_NO_TRACK) {
1802 int descyl = b_cylinder * fd->ft->steptrac;
1805 * This might be a "ready changed" interrupt,
1806 * which cannot really happen since the
1807 * RDY pin is hardwired to + 5 volts. This
1808 * generally indicates a "bouncing" intr
1809 * line, so do one of the following:
1811 * When running on an enhanced FDC that is
1812 * known to not go stuck after responding
1813 * with INVALID, fetch all interrupt states
1814 * until seeing either an INVALID or a
1815 * real interrupt condition.
1817 * When running on a dumb old NE765, give
1818 * up immediately. The controller will
1819 * provide up to four dummy RC interrupt
1820 * conditions right after reset (for the
1821 * corresponding four drives), so this is
1822 * our only chance to get notice that it
1823 * was not the FDC that caused the interrupt.
1825 if (fd_sense_int(fdc, &st0, &cyl)
1828 if(fdc->fdct == FDC_NE765
1829 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1830 return 0; /* hope for a real intr */
1831 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1836 * seek to cyl 0 requested; make sure we are
1839 if (fd_sense_drive_status(fdc, &st3))
1841 if ((st3 & NE7_ST3_T0) == 0) {
1843 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
1844 fdu, st3, NE7_ST3BITS);
1851 return (retrier(fdc));
1855 if (cyl != descyl) {
1857 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
1858 fdu, descyl, cyl, st0);
1861 return (retrier(fdc));
1865 fd->track = b_cylinder;
1866 if (!(fdc->flags & FDC_NODMA))
1867 isa_dmastart(bp->b_flags, bp->b_data+fd->skip,
1868 format ? bp->b_bcount : fdblk, fdc->dmachan);
1869 sectrac = fd->ft->sectrac;
1870 sec = blknum % (sectrac * fd->ft->heads);
1871 head = sec / sectrac;
1872 sec = sec % sectrac + 1;
1873 fd->hddrv = ((head&1)<<2)+fdu;
1877 /* make sure the drive is writable */
1878 if(fd_sense_drive_status(fdc, &st3) != 0)
1880 /* stuck controller? */
1881 if (!(fdc->flags & FDC_NODMA))
1882 isa_dmadone(bp->b_flags,
1883 bp->b_data + fd->skip,
1884 format ? bp->b_bcount : fdblk,
1886 fdc->retry = 6; /* reset the beast */
1887 return (retrier(fdc));
1889 if(st3 & NE7_ST3_WP)
1892 * XXX YES! this is ugly.
1893 * in order to force the current operation
1894 * to fail, we will have to fake an FDC
1895 * error - all error handling is done
1898 fdc->status[0] = NE7_ST0_IC_AT;
1899 fdc->status[1] = NE7_ST1_NW;
1901 fdc->status[3] = fd->track;
1902 fdc->status[4] = head;
1903 fdc->status[5] = sec;
1904 fdc->retry = 8; /* break out immediately */
1905 fdc->state = IOTIMEDOUT; /* not really... */
1911 if (fdc->flags & FDC_NODMA) {
1913 * This seems to be necessary for
1914 * whatever obscure reason; if we omit
1915 * it, we end up filling the sector ID
1916 * fields of the newly formatted track
1917 * entirely with garbage, causing
1918 * `wrong cylinder' errors all over
1919 * the place when trying to read them
1924 SET_BCDR(fdc, 1, bp->b_bcount, 0);
1926 (void)fdcpio(fdc,bp->b_flags,
1927 bp->b_data+fd->skip,
1932 if(fd_cmd(fdc, 6, NE7CMD_FORMAT, head << 2 | fdu,
1933 finfo->fd_formb_secshift,
1934 finfo->fd_formb_nsecs,
1935 finfo->fd_formb_gaplen,
1936 finfo->fd_formb_fillbyte, 0)) {
1937 /* controller fell over */
1938 if (!(fdc->flags & FDC_NODMA))
1939 isa_dmadone(bp->b_flags,
1940 bp->b_data + fd->skip,
1941 format ? bp->b_bcount : fdblk,
1944 return (retrier(fdc));
1947 if (fdc->flags & FDC_NODMA) {
1949 * this seems to be necessary even when
1952 SET_BCDR(fdc, 1, fdblk, 0);
1955 * perform the write pseudo-DMA before
1956 * the WRITE command is sent
1959 (void)fdcpio(fdc,bp->b_flags,
1960 bp->b_data+fd->skip,
1964 (read ? NE7CMD_READ : NE7CMD_WRITE),
1965 head << 2 | fdu, /* head & unit */
1966 fd->track, /* track */
1968 sec, /* sector + 1 */
1969 fd->ft->secsize, /* sector size */
1970 sectrac, /* sectors/track */
1971 fd->ft->gap, /* gap size */
1972 fd->ft->datalen, /* data length */
1974 /* the beast is sleeping again */
1975 if (!(fdc->flags & FDC_NODMA))
1976 isa_dmadone(bp->b_flags,
1977 bp->b_data + fd->skip,
1978 format ? bp->b_bcount : fdblk,
1981 return (retrier(fdc));
1984 if (fdc->flags & FDC_NODMA)
1986 * if this is a read, then simply await interrupt
1987 * before performing PIO
1989 if (read && !fdcpio(fdc,bp->b_flags,
1990 bp->b_data+fd->skip,fdblk)) {
1991 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
1992 return(0); /* will return later */
1996 * write (or format) operation will fall through and
1997 * await completion interrupt
1999 fdc->state = IOCOMPLETE;
2000 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
2001 return (0); /* will return later */
2004 * actually perform the PIO read. The IOCOMPLETE case
2005 * removes the timeout for us.
2007 (void)fdcpio(fdc,bp->b_flags,bp->b_data+fd->skip,fdblk);
2008 fdc->state = IOCOMPLETE;
2010 case IOCOMPLETE: /* IO DONE, post-analyze */
2011 untimeout(fd_iotimeout, fdc, fd->tohandle);
2013 if (fd_read_status(fdc, fd->fdsu)) {
2014 if (!(fdc->flags & FDC_NODMA))
2015 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
2016 format ? bp->b_bcount : fdblk,
2019 fdc->retry = 6; /* force a reset */
2020 return (retrier(fdc));
2023 fdc->state = IOTIMEDOUT;
2028 if (!(fdc->flags & FDC_NODMA))
2029 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
2030 format ? bp->b_bcount : fdblk, fdc->dmachan);
2031 if (fdc->status[0] & NE7_ST0_IC) {
2032 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2033 && fdc->status[1] & NE7_ST1_OR) {
2035 * DMA overrun. Someone hogged the bus and
2036 * didn't release it in time for the next
2039 * We normally restart this without bumping
2040 * the retry counter. However, in case
2041 * something is seriously messed up (like
2042 * broken hardware), we rather limit the
2043 * number of retries so the IO operation
2044 * doesn't block indefinately.
2046 if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
2047 fdc->state = SEEKCOMPLETE;
2049 } /* else fall through */
2051 if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
2053 fdc->retry = 6; /* force a reset */
2054 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2055 && fdc->status[2] & NE7_ST2_WC
2057 fdc->retry = 3; /* force recalibrate */
2058 return (retrier(fdc));
2061 /* Operation successful, retry DMA overruns again next time. */
2062 fdc->dma_overruns = 0;
2064 if (!format && fd->skip < bp->b_bcount - bp->b_resid) {
2065 /* set up next transfer */
2066 fdc->state = DOSEEK;
2071 device_unbusy(fd->dev);
2072 devstat_end_transaction_buf(&fd->device_stats, bp);
2076 fdc->state = FINDWORK;
2082 fdc->state = RESETCOMPLETE;
2086 * Discard all the results from the reset so that they
2087 * can't cause an unexpected interrupt later.
2089 for (i = 0; i < 4; i++)
2090 (void)fd_sense_int(fdc, &st0, &cyl);
2091 fdc->state = STARTRECAL;
2094 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2097 return (retrier(fdc));
2099 fdc->state = RECALWAIT;
2100 return (0); /* will return later */
2102 /* allow heads to settle */
2103 timeout(fd_pseudointr, fdc, hz / 8);
2104 fdc->state = RECALCOMPLETE;
2105 return (0); /* will return later */
2109 * See SEEKCOMPLETE for a comment on this:
2111 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2113 if(fdc->fdct == FDC_NE765
2114 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2115 return 0; /* hope for a real intr */
2116 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2117 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
2121 * a recalibrate from beyond cylinder 77
2122 * will "fail" due to the FDC limitations;
2123 * since people used to complain much about
2124 * the failure message, try not logging
2125 * this one if it seems to be the first
2128 printf("fd%d: recal failed ST0 %b cyl %d\n",
2129 fdu, st0, NE7_ST0BITS, cyl);
2130 if(fdc->retry < 3) fdc->retry = 3;
2131 return (retrier(fdc));
2134 /* Seek (probably) necessary */
2135 fdc->state = DOSEEK;
2136 return (1); /* will return immediatly */
2138 if(fd->flags & FD_MOTOR_WAIT)
2140 return (0); /* time's not up yet */
2142 if (fdc->flags & FDC_NEEDS_RESET) {
2143 fdc->state = RESETCTLR;
2144 fdc->flags &= ~FDC_NEEDS_RESET;
2147 * If all motors were off, then the controller was
2148 * reset, so it has lost track of the current
2149 * cylinder. Recalibrate to handle this case.
2150 * But first, discard the results of the reset.
2152 fdc->state = RESETCOMPLETE;
2154 return (1); /* will return immediatly */
2156 device_printf(fdc->fdc_dev, "unexpected FD int->");
2157 if (fd_read_status(fdc, fd->fdsu) == 0)
2158 printf("FDC status :%x %x %x %x %x %x %x ",
2167 printf("No status available ");
2168 if (fd_sense_int(fdc, &st0, &cyl) != 0)
2170 printf("[controller is dead now]\n");
2173 printf("ST0 = %x, PCN = %x\n", st0, cyl);
2176 /*XXX confusing: some branches return immediately, others end up here*/
2177 return (1); /* Come back immediatly to new state */
2181 retrier(struct fdc_data *fdc)
2183 register struct buf *bp;
2189 /* XXX shouldn't this be cached somewhere? */
2190 fdu = FDUNIT(minor(bp->b_dev));
2191 fd = devclass_get_softc(fd_devclass, fdu);
2192 if (fd->options & FDOPT_NORETRY)
2195 switch (fdc->retry) {
2196 case 0: case 1: case 2:
2197 fdc->state = SEEKCOMPLETE;
2199 case 3: case 4: case 5:
2200 fdc->state = STARTRECAL;
2203 fdc->state = RESETCTLR;
2210 int printerror = (fd->options & FDOPT_NOERRLOG) == 0;
2211 dev_t sav_b_dev = bp->b_dev;
2214 bp->b_dev = makedev(major(bp->b_dev),
2215 (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
2217 diskerr(bp, "hard error", LOG_PRINTF,
2218 fdc->fd->skip / DEV_BSIZE,
2219 (struct disklabel *)NULL);
2220 bp->b_dev = sav_b_dev;
2222 if (fdc->flags & FDC_STAT_VALID)
2224 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2225 fdc->status[0], NE7_ST0BITS,
2226 fdc->status[1], NE7_ST1BITS,
2227 fdc->status[2], NE7_ST2BITS,
2228 fdc->status[3], fdc->status[4],
2231 printf(" (No status)\n");
2234 bp->b_flags |= B_ERROR;
2236 bp->b_resid += bp->b_bcount - fdc->fd->skip;
2239 device_unbusy(fd->dev);
2240 devstat_end_transaction_buf(&fdc->fd->device_stats, bp);
2242 fdc->state = FINDWORK;
2243 fdc->flags |= FDC_NEEDS_RESET;
2253 fdformat(dev, finfo, p)
2255 struct fd_formb *finfo;
2265 fdu = FDUNIT(minor(dev));
2266 fd = devclass_get_softc(fd_devclass, fdu);
2267 fdblk = 128 << fd->ft->secsize;
2269 /* set up a buffer header for fdstrategy() */
2270 bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
2274 * keep the process from being swapped
2277 bzero((void *)bp, sizeof(struct buf));
2279 BUF_LOCK(bp, LK_EXCLUSIVE);
2280 bp->b_flags = B_PHYS | B_FORMAT;
2283 * calculate a fake blkno, so fdstrategy() would initiate a
2284 * seek to the requested cylinder
2286 bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
2287 + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
2289 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2290 bp->b_data = (caddr_t)finfo;
2292 /* now do the format */
2294 BUF_STRATEGY(bp, 0);
2296 /* ...and wait for it to complete */
2298 while(!(bp->b_flags & B_DONE)) {
2299 rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
2300 if (rv == EWOULDBLOCK)
2305 if (rv == EWOULDBLOCK) {
2308 device_unbusy(fd->dev);
2311 if (bp->b_flags & B_ERROR)
2314 * allow the process to be swapped
2324 * TODO: don't allocate buffer on stack.
2328 fdioctl(dev, cmd, addr, flag, p)
2335 fdu_t fdu = FDUNIT(minor(dev));
2336 fd_p fd = devclass_get_softc(fd_devclass, fdu);
2339 struct fd_type *fdt;
2340 struct disklabel *dl;
2341 struct fdc_status *fsp;
2342 char buffer[DEV_BSIZE];
2345 fdblk = 128 << fd->ft->secsize;
2349 bzero(buffer, sizeof (buffer));
2350 dl = (struct disklabel *)buffer;
2351 dl->d_secsize = fdblk;
2353 dl->d_secpercyl = fdt->size / fdt->tracks;
2354 dl->d_type = DTYPE_FLOPPY;
2356 if (readdisklabel(dev, dl)
2362 *(struct disklabel *)addr = *dl;
2366 if ((flag & FWRITE) == 0)
2371 if ((flag & FWRITE) == 0)
2376 if ((flag & FWRITE) == 0) {
2381 dl = (struct disklabel *)addr;
2383 if ((error = setdisklabel((struct disklabel *)buffer, dl,
2387 error = writedisklabel(dev, (struct disklabel *)buffer);
2390 if ((flag & FWRITE) == 0)
2391 error = EBADF; /* must be opened for writing */
2392 else if (((struct fd_formb *)addr)->format_version !=
2394 error = EINVAL; /* wrong version of formatting prog */
2396 error = fdformat(dev, (struct fd_formb *)addr, p);
2399 case FD_GTYPE: /* get drive type */
2400 *(struct fd_type *)addr = *fd->ft;
2403 case FD_STYPE: /* set drive type */
2404 /* this is considered harmful; only allow for superuser */
2407 *fd->ft = *(struct fd_type *)addr;
2410 case FD_GOPTS: /* get drive options */
2411 *(int *)addr = fd->options;
2414 case FD_SOPTS: /* set drive options */
2415 fd->options = *(int *)addr;
2419 fsp = (struct fdc_status *)addr;
2420 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2422 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
2433 * Hello emacs, these are the
2436 * c-continued-statement-offset: 8
2437 * c-continued-brace-offset: 0
2438 * c-brace-offset: -8
2439 * c-brace-imaginary-offset: 0
2440 * c-argdecl-indent: 8
2441 * c-label-offset: -8
2442 * c++-hanging-braces: 1
2443 * c++-access-specifier-offset: -8
2444 * c++-empty-arglist-indent: 8
2445 * c++-friend-offset: 0