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.19 2004/09/19 00:36:37 joerg Exp $
59 #include "use_pccard.h"
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 <bus/isa/isavar.h>
89 #include <bus/isa/isareg.h>
92 #include <bus/isa/rtc.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 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 toffhandle;
202 struct callout tohandle;
203 struct callout motor;
204 struct devstat device_stats;
212 static devclass_t fd_devclass;
214 /***********************************************************************\
215 * Throughout this file the following conventions will be used: *
216 * fd is a pointer to the fd_data struct for the drive in question *
217 * fdc is a pointer to the fdc_data struct for the controller *
218 * fdu is the floppy drive unit number *
219 * fdcu is the floppy controller unit number *
220 * fdsu is the floppy drive unit number on that controller. (sub-unit) *
221 \***********************************************************************/
223 /* internal functions */
224 static void fdc_intr(void *);
225 static void set_motor(struct fdc_data *, int, int);
228 static timeout_t fd_turnoff;
229 static timeout_t fd_motor_on;
230 static void fd_turnon(struct fd_data *);
231 static void fdc_reset(fdc_p);
232 static int fd_in(struct fdc_data *, int *);
233 static int out_fdc(struct fdc_data *, int);
234 static void fdstart(struct fdc_data *);
235 static timeout_t fd_iotimeout;
236 static timeout_t fd_pseudointr;
237 static int fdstate(struct fdc_data *);
238 static int retrier(struct fdc_data *);
239 static int fdformat(dev_t, struct fd_formb *, struct thread *);
241 static int enable_fifo(fdc_p fdc);
243 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */
249 #define SEEKCOMPLETE 3
251 #define RECALCOMPLETE 5
257 #define IOTIMEDOUT 11
258 #define RESETCOMPLETE 12
262 static char const * const fdstates[] =
280 /* CAUTION: fd_debug causes huge amounts of logging output */
281 static int volatile fd_debug = 0;
282 #define TRACE0(arg) if(fd_debug) printf(arg)
283 #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
284 #else /* FDC_DEBUG */
286 #define TRACE1(arg1, arg2)
287 #endif /* FDC_DEBUG */
290 fdout_wr(fdc_p fdc, u_int8_t v)
292 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
298 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
302 fddata_wr(fdc_p fdc, u_int8_t v)
304 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
310 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
314 fdctl_wr_isa(fdc_p fdc, u_int8_t v)
316 bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
324 return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
329 static d_open_t Fdopen; /* NOTE, not fdopen */
330 static d_close_t fdclose;
331 static d_ioctl_t fdioctl;
332 static d_strategy_t fdstrategy;
334 static struct cdevsw fd_cdevsw = {
336 /* maj */ FD_CDEV_MAJOR,
344 /* write */ physwrite,
348 /* strategy */ fdstrategy,
354 fdc_err(struct fdc_data *fdc, const char *s)
358 if (fdc->fdc_errs < FDC_ERRMAX)
359 device_printf(fdc->fdc_dev, "%s", s);
360 else if (fdc->fdc_errs == FDC_ERRMAX)
361 device_printf(fdc->fdc_dev, "too many errors, not "
362 "logging any more\n");
369 * fd_cmd: Send a command to the chip. Takes a varargs with this structure:
371 * # of output bytes, output bytes as ints ...,
372 * # of input bytes, input bytes as ints ...
375 fd_cmd(struct fdc_data *fdc, int n_out, ...)
382 __va_start(ap, n_out);
383 cmd = (u_char)(__va_arg(ap, int));
385 __va_start(ap, n_out);
386 for (n = 0; n < n_out; n++)
388 if (out_fdc(fdc, __va_arg(ap, int)) < 0)
391 snprintf(msg, sizeof(msg),
392 "cmd %x failed at out byte %d of %d\n",
394 return fdc_err(fdc, msg);
397 n_in = __va_arg(ap, int);
398 for (n = 0; n < n_in; n++)
400 int *ptr = __va_arg(ap, int *);
401 if (fd_in(fdc, ptr) < 0)
404 snprintf(msg, sizeof(msg),
405 "cmd %02x failed at in byte %d of %d\n",
407 return fdc_err(fdc, msg);
415 enable_fifo(fdc_p fdc)
419 if ((fdc->flags & FDC_HAS_FIFO) == 0) {
423 * Cannot use fd_cmd the normal way here, since
424 * this might be an invalid command. Thus we send the
425 * first byte, and check for an early turn of data directon.
428 if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
429 return fdc_err(fdc, "Enable FIFO failed\n");
431 /* If command is invalid, return */
433 while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
434 != NE7_RQM && j-- > 0) {
435 if (i == (NE7_DIO | NE7_RQM)) {
443 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
445 return fdc_err(fdc, "Enable FIFO failed\n");
447 fdc->flags |= FDC_HAS_FIFO;
451 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
452 return fdc_err(fdc, "Re-enable FIFO failed\n");
457 fd_sense_drive_status(fdc_p fdc, int *st3p)
461 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
463 return fdc_err(fdc, "Sense Drive Status failed\n");
472 fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
476 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
479 "sense intr err reading stat reg 0\n");
486 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
488 * There doesn't seem to have been an interrupt.
493 if (fd_in(fdc, &cyl) < 0) {
494 return fdc_err(fdc, "can't get cyl num\n");
505 fd_read_status(fdc_p fdc, int fdsu)
509 for (i = 0; i < 7; i++) {
511 * XXX types are poorly chosen. Only bytes can by read
512 * from the hardware, but fdc->status[] wants u_ints and
513 * fd_in() gives ints.
517 ret = fd_in(fdc, &status);
518 fdc->status[i] = status;
524 fdc->flags |= FDC_STAT_VALID;
526 fdc->flags &= ~FDC_STAT_VALID;
531 /****************************************************************************/
532 /* autoconfiguration stuff */
533 /****************************************************************************/
536 fdc_alloc_resources(struct fdc_data *fdc)
542 ispnp = (fdc->flags & FDC_ISPNP) != 0;
543 ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
544 fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
545 fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
548 * On standard ISA, we don't just use an 8 port range
549 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
552 * Isn't PC hardware wonderful.
554 * The Y-E Data PCMCIA FDC doesn't have this problem, it
555 * uses the register with offset 6 for pseudo-DMA, and the
556 * one with offset 7 as control register.
558 fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
559 &fdc->rid_ioport, 0ul, ~0ul,
560 ispcmcia ? 8 : (ispnp ? 1 : 6),
562 if (fdc->res_ioport == 0) {
563 device_printf(dev, "cannot reserve I/O port range\n");
566 fdc->portt = rman_get_bustag(fdc->res_ioport);
567 fdc->porth = rman_get_bushandle(fdc->res_ioport);
571 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
572 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
573 * by checking the size and adjust the port address
576 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
580 * Register the control port range as rid 1 if it
581 * isn't there already. Most PnP BIOSen will have
582 * already done this but non-PnP configurations don't.
584 * And some (!!) report 0x3f2-0x3f5 and completely
585 * leave out the control register! It seems that some
586 * non-antique controller chips have a different
587 * method of programming the transfer speed which
588 * doesn't require the control register, but it's
589 * mighty bogus as the chip still responds to the
590 * address for the control register.
592 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
595 /* Find the control port, usually 0x3f7 */
596 ctlstart = rman_get_start(fdc->res_ioport) +
599 bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
603 * Now (finally!) allocate the control port.
606 fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
608 0ul, ~0ul, 1, RF_ACTIVE);
609 if (fdc->res_ctl == 0) {
611 "cannot reserve control I/O port range\n");
614 fdc->ctlt = rman_get_bustag(fdc->res_ctl);
615 fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
618 fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
619 &fdc->rid_irq, 0ul, ~0ul, 1,
621 if (fdc->res_irq == 0) {
622 device_printf(dev, "cannot reserve interrupt line\n");
626 if ((fdc->flags & FDC_NODMA) == 0) {
627 fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
628 &fdc->rid_drq, 0ul, ~0ul, 1,
630 if (fdc->res_drq == 0) {
631 device_printf(dev, "cannot reserve DMA request line\n");
634 fdc->dmachan = fdc->res_drq->r_start;
641 fdc_release_resources(struct fdc_data *fdc)
646 if (fdc->res_irq != 0) {
647 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
649 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
652 if (fdc->res_ctl != 0) {
653 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
655 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
658 if (fdc->res_ioport != 0) {
659 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
661 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
664 if (fdc->res_drq != 0) {
665 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
667 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
672 /****************************************************************************/
673 /* autoconfiguration stuff */
674 /****************************************************************************/
676 static struct isa_pnp_id fdc_ids[] = {
677 {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
678 {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
683 fdc_read_ivar(device_t dev, device_t child, int which, u_long *result)
685 struct fdc_ivars *ivars = device_get_ivars(child);
688 case FDC_IVAR_FDUNIT:
689 *result = ivars->fdunit;
698 * fdc controller section.
701 fdc_probe(device_t dev)
704 struct fdc_data *fdc;
706 fdc = device_get_softc(dev);
707 bzero(fdc, sizeof *fdc);
709 fdc->fdctl_wr = fdctl_wr_isa;
712 error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
716 fdc->flags |= FDC_ISPNP;
718 /* Attempt to allocate our resources for the duration of the probe */
719 error = fdc_alloc_resources(fdc);
723 /* First - lets reset the floppy controller */
726 fdout_wr(fdc, FDO_FRST);
728 /* see if it can handle a command */
729 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
730 NE7_SPEC_2(2, 0), 0)) {
735 if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
736 ic_type = (u_char)ic_type;
739 device_set_desc(dev, "NEC 765 or clone");
740 fdc->fdct = FDC_NE765;
743 device_set_desc(dev, "Intel 82077 or clone");
744 fdc->fdct = FDC_I82077;
747 device_set_desc(dev, "NEC 72065B or clone");
748 fdc->fdct = FDC_NE72065;
751 device_set_desc(dev, "generic floppy controller");
752 fdc->fdct = FDC_UNKNOWN;
758 fdc_release_resources(fdc);
763 * Add a child device to the fdc controller. It will then be probed etc.
766 fdc_add_child(device_t dev, const char *name, int unit)
769 struct fdc_ivars *ivar;
772 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_WAITOK | M_ZERO);
773 if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
775 child = device_add_child(dev, name, unit);
778 device_set_ivars(child, ivar);
779 if (resource_int_value(name, unit, "disabled", &disabled) == 0
781 device_disable(child);
785 fdc_attach(device_t dev)
787 struct fdc_data *fdc;
790 fdc = device_get_softc(dev);
791 error = fdc_alloc_resources(fdc);
793 device_printf(dev, "cannot re-aquire resources\n");
796 error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
797 INTR_TYPE_BIO, fdc_intr, fdc, &fdc->fdc_intr);
799 device_printf(dev, "cannot setup interrupt\n");
802 fdc->fdcu = device_get_unit(dev);
803 fdc->flags |= FDC_ATTACHED;
805 if ((fdc->flags & FDC_NODMA) == 0) {
806 /* Acquire the DMA channel forever, The driver will do the rest */
807 /* XXX should integrate with rman */
808 isa_dma_acquire(fdc->dmachan);
809 isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
811 fdc->state = DEVIDLE;
813 /* reset controller, turn motor off, clear fdout mirror reg */
814 fdout_wr(fdc, ((fdc->fdout = 0)));
815 bufq_init(&fdc->head);
818 * Probe and attach any children. We should probably detect
819 * devices from the BIOS unless overridden.
821 for (i = resource_query_string(-1, "at", device_get_nameunit(dev));
823 i = resource_query_string(i, "at", device_get_nameunit(dev)))
824 fdc_add_child(dev, resource_query_name(i),
825 resource_query_unit(i));
827 return (bus_generic_attach(dev));
831 fdc_print_child(device_t me, device_t child)
835 retval += bus_print_child_header(me, child);
836 retval += printf(" on %s drive %d\n", device_get_nameunit(me),
837 fdc_get_fdunit(child));
842 static device_method_t fdc_methods[] = {
843 /* Device interface */
844 DEVMETHOD(device_probe, fdc_probe),
845 DEVMETHOD(device_attach, fdc_attach),
846 DEVMETHOD(device_detach, bus_generic_detach),
847 DEVMETHOD(device_shutdown, bus_generic_shutdown),
848 DEVMETHOD(device_suspend, bus_generic_suspend),
849 DEVMETHOD(device_resume, bus_generic_resume),
852 DEVMETHOD(bus_print_child, fdc_print_child),
853 DEVMETHOD(bus_read_ivar, fdc_read_ivar),
854 /* Our children never use any other bus interface methods. */
859 static driver_t fdc_driver = {
862 sizeof(struct fdc_data)
865 DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
867 /******************************************************************/
869 * devices attached to the controller section.
872 fd_probe(device_t dev)
877 struct fdc_data *fdc;
879 static int fd_fifo = 0;
881 fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
882 fd = device_get_softc(dev);
883 fdc = device_get_softc(device_get_parent(dev));
885 bzero(fd, sizeof *fd);
889 fd->fdu = device_get_unit(dev);
892 /* look up what bios thinks we have */
895 if ((fdc->flags & FDC_ISPCMCIA))
897 else if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0)
898 fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
900 fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
903 fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
910 fdt = RTCFDT_144M; /* XXX probably */
913 /* is there a unit? */
914 if (fdt == RTCFDT_NONE)
918 set_motor(fdc, fdsu, TURNON);
919 DELAY(1000000); /* 1 sec */
921 /* XXX This doesn't work before the first set_motor() */
922 if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN
923 && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0
924 && enable_fifo(fdc) == 0) {
925 device_printf(device_get_parent(dev),
926 "FIFO enabled, %d bytes threshold\n", fifo_threshold);
930 if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0)
931 && (st3 & NE7_ST3_T0)) {
932 /* if at track 0, first seek inwards */
933 /* seek some steps: */
934 fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0);
935 DELAY(300000); /* ...wait a moment... */
936 fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
939 /* If we're at track 0 first seek inwards. */
940 if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
941 /* Seek some steps... */
942 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
943 /* ...wait a moment... */
945 /* make ctrlr happy: */
946 fd_sense_int(fdc, 0, 0);
950 for (i = 0; i < 2; i++) {
952 * we must recalibrate twice, just in case the
953 * heads have been beyond cylinder 76, since most
954 * FDCs still barf when attempting to recalibrate
958 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
959 /* a second being enough for full stroke seek*/
960 DELAY(i == 0 ? 1000000 : 300000);
962 /* anything responding? */
963 if (fd_sense_int(fdc, &st0, 0) == 0 &&
964 (st0 & NE7_ST0_EC) == 0)
965 break; /* already probed succesfully */
969 set_motor(fdc, fdsu, TURNOFF);
971 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
974 fd->track = FD_NO_TRACK;
978 callout_init(&fd->toffhandle);
979 callout_init(&fd->tohandle);
980 callout_init(&fd->motor);
984 device_set_desc(dev, "1200-KB 5.25\" drive");
987 case RTCFDT_144M | RTCFDT_144M_PRETENDED:
988 device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive");
992 device_set_desc(dev, "1440-KB 3.5\" drive");
997 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1001 device_set_desc(dev, "360-KB 5.25\" drive");
1005 printf("720-KB 3.5\" drive");
1015 fd_attach(device_t dev)
1025 fd = device_get_softc(dev);
1027 cdevsw_add(&fd_cdevsw, -1 << 6, fd->fdu << 6);
1028 make_dev(&fd_cdevsw, (fd->fdu << 6),
1029 UID_ROOT, GID_OPERATOR, 0640, "rfd%d", fd->fdu);
1032 /* Other make_dev() go here. */
1036 * Export the drive to the devstat interface.
1038 devstat_add_entry(&fd->device_stats, device_get_name(dev),
1039 device_get_unit(dev), 512, DEVSTAT_NO_ORDERED_TAGS,
1040 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
1041 DEVSTAT_PRIORITY_FD);
1046 fd_detach(device_t dev)
1050 fd = device_get_softc(dev);
1051 callout_stop(&fd->toffhandle);
1052 callout_stop(&fd->motor);
1057 static device_method_t fd_methods[] = {
1058 /* Device interface */
1059 DEVMETHOD(device_probe, fd_probe),
1060 DEVMETHOD(device_attach, fd_attach),
1061 DEVMETHOD(device_detach, fd_detach),
1062 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1063 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
1064 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
1069 static driver_t fd_driver = {
1072 sizeof(struct fd_data)
1075 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
1077 /****************************************************************************/
1078 /* motor control stuff */
1079 /* remember to not deselect the drive we're working on */
1080 /****************************************************************************/
1082 set_motor(struct fdc_data *fdc, int fdsu, int turnon)
1084 int fdout = fdc->fdout;
1085 int needspecify = 0;
1088 fdout &= ~FDO_FDSEL;
1089 fdout |= (FDO_MOEN0 << fdsu) + fdsu;
1091 fdout &= ~(FDO_MOEN0 << fdsu);
1094 && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
1095 /* gonna turn off the last drive, put FDC to bed */
1096 fdout &= ~ (FDO_FRST|FDO_FDMAEN);
1098 /* make sure controller is selected and specified */
1099 if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
1101 fdout |= (FDO_FRST|FDO_FDMAEN);
1104 fdout_wr(fdc, fdout);
1106 TRACE1("[0x%x->FDOUT]", fdout);
1111 * special case: since we have just woken up the FDC
1112 * from its sleep, we silently assume the command will
1113 * be accepted, and do not test for a timeout
1115 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1116 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1118 if (fdc->flags & FDC_HAS_FIFO)
1119 (void) enable_fifo(fdc);
1124 fd_turnoff(void *xfd)
1129 TRACE1("[fd%d: turnoff]", fd->fdu);
1133 * Don't turn off the motor yet if the drive is active.
1135 * If we got here, this could only mean we missed an interrupt.
1136 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
1137 * after a controller reset. Just schedule a pseudo-interrupt
1138 * so the state machine gets re-entered.
1140 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
1146 fd->flags &= ~FD_MOTOR;
1147 set_motor(fd->fdc, fd->fdsu, TURNOFF);
1152 fd_motor_on(void *xfd)
1158 fd->flags &= ~FD_MOTOR_WAIT;
1159 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
1169 if(!(fd->flags & FD_MOTOR))
1171 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
1172 set_motor(fd->fdc, fd->fdsu, TURNON);
1173 callout_reset(&fd->motor, hz, fd_motor_on, fd);
1178 fdc_reset(fdc_p fdc)
1180 /* Try a reset, keep motor on */
1181 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1182 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1184 /* enable FDC, but defer interrupts a moment */
1185 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
1186 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
1188 fdout_wr(fdc, fdc->fdout);
1189 TRACE1("[0x%x->FDOUT]", fdc->fdout);
1191 /* XXX after a reset, silently believe the FDC will accept commands */
1192 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1193 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1195 if (fdc->flags & FDC_HAS_FIFO)
1196 (void) enable_fifo(fdc);
1199 /****************************************************************************/
1201 /****************************************************************************/
1203 * FDC IO functions, take care of the main status register, timeout
1204 * in case the desired status bits are never set.
1206 * These PIO loops initially start out with short delays between
1207 * each iteration in the expectation that the required condition
1208 * is usually met quickly, so it can be handled immediately. After
1209 * about 1 ms, stepping is increased to achieve a better timing
1210 * accuracy in the calls to DELAY().
1213 fd_in(struct fdc_data *fdc, int *ptr)
1217 for (j = 0, step = 1;
1218 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) &&
1222 return (fdc_err(fdc, "ready for output in input\n"));
1227 if (j >= FDSTS_TIMEOUT)
1228 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
1231 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1234 #else /* !FDC_DEBUG */
1239 #endif /* FDC_DEBUG */
1243 out_fdc(struct fdc_data *fdc, int x)
1247 for (j = 0, step = 1;
1248 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != NE7_RQM &&
1251 if (i == (NE7_DIO|NE7_RQM))
1252 return (fdc_err(fdc, "ready for input in output\n"));
1257 if (j >= FDSTS_TIMEOUT)
1258 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
1260 /* Send the command and return */
1262 TRACE1("[0x%x->FDDATA]", x);
1266 /****************************************************************************/
1267 /* fdopen/fdclose */
1268 /****************************************************************************/
1270 Fdopen(dev_t dev, int flags, int mode, struct thread *td)
1272 fdu_t fdu = FDUNIT(minor(dev));
1273 int type = FDTYPE(minor(dev));
1278 if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1281 if ((fdc == NULL) || (fd->type == NO_TYPE))
1289 * For each type of basic drive, make sure we are trying
1290 * to open a type it can do,
1292 if (type != fd->type) {
1306 type = FD_1480in5_25;
1309 type = FD_1440in5_25;
1314 type = FD_820in5_25;
1317 type = FD_800in5_25;
1320 type = FD_720in5_25;
1323 type = FD_640in5_25;
1326 type = FD_360in5_25;
1333 if ( type != FD_1720
1346 fd->ft = fd_types + type - 1;
1347 fd->flags |= FD_OPEN;
1349 * Clearing the DMA overrun counter at open time is a bit messy.
1350 * Since we're only managing one counter per controller, opening
1351 * the second drive could mess it up. Anyway, if the DMA overrun
1352 * condition is really persistent, it will eventually time out
1353 * still. OTOH, clearing it here will ensure we'll at least start
1354 * trying again after a previous (maybe even long ago) failure.
1355 * Also, this is merely a stop-gap measure only that should not
1356 * happen during normal operation, so we can tolerate it to be a
1357 * bit sloppy about this.
1359 fdc->dma_overruns = 0;
1365 fdclose(dev_t dev, int flags, int mode, struct thread *td)
1367 fdu_t fdu = FDUNIT(minor(dev));
1370 fd = devclass_get_softc(fd_devclass, fdu);
1371 fd->flags &= ~FD_OPEN;
1372 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG);
1377 /****************************************************************************/
1379 /****************************************************************************/
1381 fdstrategy(struct buf *bp)
1383 unsigned nblocks, blknum, cando;
1390 fdu = FDUNIT(minor(bp->b_dev));
1391 fd = devclass_get_softc(fd_devclass, fdu);
1393 panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
1394 (u_long)major(bp->b_dev), (u_long)minor(bp->b_dev));
1396 if (fd->type == NO_TYPE) {
1397 bp->b_error = ENXIO;
1398 bp->b_flags |= B_ERROR;
1402 fdblk = 128 << (fd->ft->secsize);
1403 if (!(bp->b_flags & B_FORMAT)) {
1404 if (bp->b_blkno < 0) {
1406 "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1407 fdu, (u_long)bp->b_blkno, bp->b_bcount);
1408 bp->b_error = EINVAL;
1409 bp->b_flags |= B_ERROR;
1412 if ((bp->b_bcount % fdblk) != 0) {
1413 bp->b_error = EINVAL;
1414 bp->b_flags |= B_ERROR;
1420 * Set up block calculations.
1422 if (bp->b_blkno > 20000000) {
1424 * Reject unreasonably high block number, prevent the
1425 * multiplication below from overflowing.
1427 bp->b_error = EINVAL;
1428 bp->b_flags |= B_ERROR;
1431 blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk;
1432 nblocks = fd->ft->size;
1434 if (blknum + (bp->b_bcount / fdblk) > nblocks) {
1435 if (blknum <= nblocks) {
1436 cando = (nblocks - blknum) * fdblk;
1437 bp->b_resid = bp->b_bcount - cando;
1439 goto bad; /* not actually bad but EOF */
1441 bp->b_error = EINVAL;
1442 bp->b_flags |= B_ERROR;
1446 bp->b_pblkno = bp->b_blkno;
1448 bufqdisksort(&fdc->head, bp);
1449 callout_stop(&fd->toffhandle);
1451 /* Tell devstat we are starting on the transaction */
1452 devstat_start_transaction(&fd->device_stats);
1453 device_busy(fd->dev);
1463 /***************************************************************\
1465 * We have just queued something.. if the controller is not busy *
1466 * then simulate the case where it has just finished a command *
1467 * So that it (the interrupt routine) looks on the queue for more*
1468 * work to do and picks up what we just added. *
1469 * If the controller is already busy, we need do nothing, as it *
1470 * will pick up our work when the present work completes *
1471 \***************************************************************/
1473 fdstart(struct fdc_data *fdc)
1478 if(fdc->state == DEVIDLE)
1486 fd_iotimeout(void *xfdc)
1492 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
1495 * Due to IBM's brain-dead design, the FDC has a faked ready
1496 * signal, hardwired to ready == true. Thus, any command
1497 * issued if there's no diskette in the drive will _never_
1498 * complete, and must be aborted by resetting the FDC.
1499 * Many thanks, Big Blue!
1500 * The FDC must not be reset directly, since that would
1501 * interfere with the state machine. Instead, pretend that
1502 * the command completed but was invalid. The state machine
1503 * will reset the FDC and retry once.
1506 fdc->status[0] = NE7_ST0_IC_IV;
1507 fdc->flags &= ~FDC_STAT_VALID;
1508 fdc->state = IOTIMEDOUT;
1513 /* just ensure it has the right spl */
1515 fd_pseudointr(void *xfdc)
1524 /***********************************************************************\
1526 * keep calling the state machine until it returns a 0 *
1527 * ALWAYS called at SPLBIO *
1528 \***********************************************************************/
1530 fdc_intr(void *xfdc)
1538 * magic pseudo-DMA initialization for YE FDC. Sets count and
1541 #define SET_BCDR(fdc,wr,cnt,port) \
1542 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \
1543 ((cnt)-1) & 0xff); \
1544 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
1545 ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
1548 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy
1550 static int fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
1552 u_char *cptr = (u_char *)addr;
1554 if (flags & B_READ) {
1555 if (fdc->state != PIOREAD) {
1556 fdc->state = PIOREAD;
1559 SET_BCDR(fdc, 0, count, 0);
1560 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1561 FDC_YE_DATAPORT, cptr, count);
1563 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1564 FDC_YE_DATAPORT, cptr, count);
1565 SET_BCDR(fdc, 0, count, 0);
1570 /***********************************************************************\
1571 * The controller state machine. *
1572 * if it returns a non zero value, it should be called again immediatly *
1573 \***********************************************************************/
1577 int read, format, head, i, sec = 0, sectrac, st0, cyl, st3;
1578 unsigned blknum = 0, b_cylinder = 0;
1579 fdu_t fdu = fdc->fdu;
1582 struct fd_formb *finfo = NULL;
1587 bp = bufq_first(&fdc->head);
1589 bufq_remove(&fdc->head, bp);
1594 /***********************************************\
1595 * nothing left for this controller to do *
1596 * Force into the IDLE state, *
1597 \***********************************************/
1598 fdc->state = DEVIDLE;
1600 device_printf(fdc->fdc_dev,
1601 "unexpected valid fd pointer\n");
1605 TRACE1("[fdc%d IDLE]", fdc->fdcu);
1608 fdu = FDUNIT(minor(bp->b_dev));
1609 fd = devclass_get_softc(fd_devclass, fdu);
1610 fdblk = 128 << fd->ft->secsize;
1611 if (fdc->fd && (fd != fdc->fd))
1612 device_printf(fd->dev, "confused fd pointers\n");
1613 read = bp->b_flags & B_READ;
1614 format = bp->b_flags & B_FORMAT;
1616 finfo = (struct fd_formb *)bp->b_data;
1617 fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1620 if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
1621 blknum = (unsigned) bp->b_pblkno * DEV_BSIZE/fdblk +
1623 b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1625 TRACE1("fd%d", fdu);
1626 TRACE1("[%s]", fdstates[fdc->state]);
1627 TRACE1("(0x%x)", fd->flags);
1628 callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
1632 case FINDWORK: /* we have found new work */
1637 fdc->fdctl_wr(fdc, fd->ft->trans);
1638 TRACE1("[0x%x->FDCTL]", fd->ft->trans);
1639 /*******************************************************\
1640 * If the next drive has a motor startup pending, then *
1641 * it will start up in its own good time *
1642 \*******************************************************/
1643 if(fd->flags & FD_MOTOR_WAIT) {
1644 fdc->state = MOTORWAIT;
1645 return (0); /* come back later */
1647 /*******************************************************\
1648 * Maybe if it's not starting, it SHOULD be starting *
1649 \*******************************************************/
1650 if (!(fd->flags & FD_MOTOR))
1652 fdc->state = MOTORWAIT;
1656 else /* at least make sure we are selected */
1658 set_motor(fdc, fd->fdsu, TURNON);
1660 if (fdc->flags & FDC_NEEDS_RESET) {
1661 fdc->state = RESETCTLR;
1662 fdc->flags &= ~FDC_NEEDS_RESET;
1664 fdc->state = DOSEEK;
1667 if (b_cylinder == (unsigned)fd->track)
1669 fdc->state = SEEKCOMPLETE;
1672 if (fd_cmd(fdc, 3, NE7CMD_SEEK,
1673 fd->fdsu, b_cylinder * fd->ft->steptrac,
1677 * seek command not accepted, looks like
1678 * the FDC went off to the Saints...
1680 fdc->retry = 6; /* try a reset */
1681 return(retrier(fdc));
1683 fd->track = FD_NO_TRACK;
1684 fdc->state = SEEKWAIT;
1685 return(0); /* will return later */
1687 /* allow heads to settle */
1688 callout_reset(&fdc->pseudointr_ch, hz / 16,
1689 fd_pseudointr, fdc);
1690 fdc->state = SEEKCOMPLETE;
1691 return(0); /* will return later */
1692 case SEEKCOMPLETE : /* SEEK DONE, START DMA */
1693 /* Make sure seek really happened*/
1694 if(fd->track == FD_NO_TRACK) {
1695 int descyl = b_cylinder * fd->ft->steptrac;
1698 * This might be a "ready changed" interrupt,
1699 * which cannot really happen since the
1700 * RDY pin is hardwired to + 5 volts. This
1701 * generally indicates a "bouncing" intr
1702 * line, so do one of the following:
1704 * When running on an enhanced FDC that is
1705 * known to not go stuck after responding
1706 * with INVALID, fetch all interrupt states
1707 * until seeing either an INVALID or a
1708 * real interrupt condition.
1710 * When running on a dumb old NE765, give
1711 * up immediately. The controller will
1712 * provide up to four dummy RC interrupt
1713 * conditions right after reset (for the
1714 * corresponding four drives), so this is
1715 * our only chance to get notice that it
1716 * was not the FDC that caused the interrupt.
1718 if (fd_sense_int(fdc, &st0, &cyl)
1721 if(fdc->fdct == FDC_NE765
1722 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1723 return 0; /* hope for a real intr */
1724 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1729 * seek to cyl 0 requested; make sure we are
1732 if (fd_sense_drive_status(fdc, &st3))
1734 if ((st3 & NE7_ST3_T0) == 0) {
1736 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
1737 fdu, st3, NE7_ST3BITS);
1744 return (retrier(fdc));
1748 if (cyl != descyl) {
1750 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
1751 fdu, descyl, cyl, st0);
1754 return (retrier(fdc));
1758 fd->track = b_cylinder;
1759 if (!(fdc->flags & FDC_NODMA))
1760 isa_dmastart(bp->b_flags, bp->b_data+fd->skip,
1761 format ? bp->b_bcount : fdblk, fdc->dmachan);
1762 sectrac = fd->ft->sectrac;
1763 sec = blknum % (sectrac * fd->ft->heads);
1764 head = sec / sectrac;
1765 sec = sec % sectrac + 1;
1766 fd->hddrv = ((head&1)<<2)+fdu;
1770 /* make sure the drive is writable */
1771 if(fd_sense_drive_status(fdc, &st3) != 0)
1773 /* stuck controller? */
1774 if (!(fdc->flags & FDC_NODMA))
1775 isa_dmadone(bp->b_flags,
1776 bp->b_data + fd->skip,
1777 format ? bp->b_bcount : fdblk,
1779 fdc->retry = 6; /* reset the beast */
1780 return (retrier(fdc));
1782 if(st3 & NE7_ST3_WP)
1785 * XXX YES! this is ugly.
1786 * in order to force the current operation
1787 * to fail, we will have to fake an FDC
1788 * error - all error handling is done
1791 fdc->status[0] = NE7_ST0_IC_AT;
1792 fdc->status[1] = NE7_ST1_NW;
1794 fdc->status[3] = fd->track;
1795 fdc->status[4] = head;
1796 fdc->status[5] = sec;
1797 fdc->retry = 8; /* break out immediately */
1798 fdc->state = IOTIMEDOUT; /* not really... */
1804 if (fdc->flags & FDC_NODMA) {
1806 * This seems to be necessary for
1807 * whatever obscure reason; if we omit
1808 * it, we end up filling the sector ID
1809 * fields of the newly formatted track
1810 * entirely with garbage, causing
1811 * `wrong cylinder' errors all over
1812 * the place when trying to read them
1817 SET_BCDR(fdc, 1, bp->b_bcount, 0);
1819 (void)fdcpio(fdc,bp->b_flags,
1820 bp->b_data+fd->skip,
1825 if(fd_cmd(fdc, 6, NE7CMD_FORMAT, head << 2 | fdu,
1826 finfo->fd_formb_secshift,
1827 finfo->fd_formb_nsecs,
1828 finfo->fd_formb_gaplen,
1829 finfo->fd_formb_fillbyte, 0)) {
1830 /* controller fell over */
1831 if (!(fdc->flags & FDC_NODMA))
1832 isa_dmadone(bp->b_flags,
1833 bp->b_data + fd->skip,
1834 format ? bp->b_bcount : fdblk,
1837 return (retrier(fdc));
1840 if (fdc->flags & FDC_NODMA) {
1842 * this seems to be necessary even when
1845 SET_BCDR(fdc, 1, fdblk, 0);
1848 * perform the write pseudo-DMA before
1849 * the WRITE command is sent
1852 (void)fdcpio(fdc,bp->b_flags,
1853 bp->b_data+fd->skip,
1857 (read ? NE7CMD_READ : NE7CMD_WRITE),
1858 head << 2 | fdu, /* head & unit */
1859 fd->track, /* track */
1861 sec, /* sector + 1 */
1862 fd->ft->secsize, /* sector size */
1863 sectrac, /* sectors/track */
1864 fd->ft->gap, /* gap size */
1865 fd->ft->datalen, /* data length */
1867 /* the beast is sleeping again */
1868 if (!(fdc->flags & FDC_NODMA))
1869 isa_dmadone(bp->b_flags,
1870 bp->b_data + fd->skip,
1871 format ? bp->b_bcount : fdblk,
1874 return (retrier(fdc));
1877 if (fdc->flags & FDC_NODMA)
1879 * if this is a read, then simply await interrupt
1880 * before performing PIO
1882 if (read && !fdcpio(fdc,bp->b_flags,
1883 bp->b_data+fd->skip,fdblk)) {
1884 callout_reset(&fd->tohandle, hz,
1886 return(0); /* will return later */
1890 * write (or format) operation will fall through and
1891 * await completion interrupt
1893 fdc->state = IOCOMPLETE;
1894 callout_reset(&fd->tohandle, hz, fd_iotimeout, fdc);
1895 return (0); /* will return later */
1898 * actually perform the PIO read. The IOCOMPLETE case
1899 * removes the timeout for us.
1901 (void)fdcpio(fdc,bp->b_flags,bp->b_data+fd->skip,fdblk);
1902 fdc->state = IOCOMPLETE;
1904 case IOCOMPLETE: /* IO DONE, post-analyze */
1905 callout_stop(&fd->tohandle);
1907 if (fd_read_status(fdc, fd->fdsu)) {
1908 if (!(fdc->flags & FDC_NODMA))
1909 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1910 format ? bp->b_bcount : fdblk,
1913 fdc->retry = 6; /* force a reset */
1914 return (retrier(fdc));
1917 fdc->state = IOTIMEDOUT;
1922 if (!(fdc->flags & FDC_NODMA))
1923 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1924 format ? bp->b_bcount : fdblk, fdc->dmachan);
1925 if (fdc->status[0] & NE7_ST0_IC) {
1926 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
1927 && fdc->status[1] & NE7_ST1_OR) {
1929 * DMA overrun. Someone hogged the bus and
1930 * didn't release it in time for the next
1933 * We normally restart this without bumping
1934 * the retry counter. However, in case
1935 * something is seriously messed up (like
1936 * broken hardware), we rather limit the
1937 * number of retries so the IO operation
1938 * doesn't block indefinately.
1940 if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
1941 fdc->state = SEEKCOMPLETE;
1943 } /* else fall through */
1945 if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
1947 fdc->retry = 6; /* force a reset */
1948 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
1949 && fdc->status[2] & NE7_ST2_WC
1951 fdc->retry = 3; /* force recalibrate */
1952 return (retrier(fdc));
1955 /* Operation successful, retry DMA overruns again next time. */
1956 fdc->dma_overruns = 0;
1958 if (!format && fd->skip < bp->b_bcount - bp->b_resid) {
1959 /* set up next transfer */
1960 fdc->state = DOSEEK;
1965 device_unbusy(fd->dev);
1966 devstat_end_transaction_buf(&fd->device_stats, bp);
1970 fdc->state = FINDWORK;
1976 fdc->state = RESETCOMPLETE;
1980 * Discard all the results from the reset so that they
1981 * can't cause an unexpected interrupt later.
1983 for (i = 0; i < 4; i++)
1984 (void)fd_sense_int(fdc, &st0, &cyl);
1985 fdc->state = STARTRECAL;
1988 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
1991 return (retrier(fdc));
1993 fdc->state = RECALWAIT;
1994 return (0); /* will return later */
1996 /* allow heads to settle */
1997 callout_reset(&fdc->pseudointr_ch, hz / 8, fd_pseudointr, fdc);
1998 fdc->state = RECALCOMPLETE;
1999 return (0); /* will return later */
2003 * See SEEKCOMPLETE for a comment on this:
2005 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2007 if(fdc->fdct == FDC_NE765
2008 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2009 return 0; /* hope for a real intr */
2010 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2011 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
2015 * a recalibrate from beyond cylinder 77
2016 * will "fail" due to the FDC limitations;
2017 * since people used to complain much about
2018 * the failure message, try not logging
2019 * this one if it seems to be the first
2022 printf("fd%d: recal failed ST0 %b cyl %d\n",
2023 fdu, st0, NE7_ST0BITS, cyl);
2024 if(fdc->retry < 3) fdc->retry = 3;
2025 return (retrier(fdc));
2028 /* Seek (probably) necessary */
2029 fdc->state = DOSEEK;
2030 return (1); /* will return immediatly */
2032 if(fd->flags & FD_MOTOR_WAIT)
2034 return (0); /* time's not up yet */
2036 if (fdc->flags & FDC_NEEDS_RESET) {
2037 fdc->state = RESETCTLR;
2038 fdc->flags &= ~FDC_NEEDS_RESET;
2041 * If all motors were off, then the controller was
2042 * reset, so it has lost track of the current
2043 * cylinder. Recalibrate to handle this case.
2044 * But first, discard the results of the reset.
2046 fdc->state = RESETCOMPLETE;
2048 return (1); /* will return immediatly */
2050 device_printf(fdc->fdc_dev, "unexpected FD int->");
2051 if (fd_read_status(fdc, fd->fdsu) == 0)
2052 printf("FDC status :%x %x %x %x %x %x %x ",
2061 printf("No status available ");
2062 if (fd_sense_int(fdc, &st0, &cyl) != 0)
2064 printf("[controller is dead now]\n");
2067 printf("ST0 = %x, PCN = %x\n", st0, cyl);
2070 /*XXX confusing: some branches return immediately, others end up here*/
2071 return (1); /* Come back immediatly to new state */
2075 retrier(struct fdc_data *fdc)
2083 /* XXX shouldn't this be cached somewhere? */
2084 fdu = FDUNIT(minor(bp->b_dev));
2085 fd = devclass_get_softc(fd_devclass, fdu);
2086 if (fd->options & FDOPT_NORETRY)
2089 switch (fdc->retry) {
2090 case 0: case 1: case 2:
2091 fdc->state = SEEKCOMPLETE;
2093 case 3: case 4: case 5:
2094 fdc->state = STARTRECAL;
2097 fdc->state = RESETCTLR;
2104 int printerror = (fd->options & FDOPT_NOERRLOG) == 0;
2108 * note: use the correct device for more
2109 * verbose error reporting.
2113 subdev = make_sub_dev(bp->b_dev,
2114 (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
2116 "hard error", LOG_PRINTF,
2117 fdc->fd->skip / DEV_BSIZE,
2118 (struct disklabel *)NULL);
2121 if (fdc->flags & FDC_STAT_VALID)
2123 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2124 fdc->status[0], NE7_ST0BITS,
2125 fdc->status[1], NE7_ST1BITS,
2126 fdc->status[2], NE7_ST2BITS,
2127 fdc->status[3], fdc->status[4],
2130 printf(" (No status)\n");
2133 bp->b_flags |= B_ERROR;
2135 bp->b_resid += bp->b_bcount - fdc->fd->skip;
2138 device_unbusy(fd->dev);
2139 devstat_end_transaction_buf(&fdc->fd->device_stats, bp);
2141 fdc->state = FINDWORK;
2142 fdc->flags |= FDC_NEEDS_RESET;
2152 fdformat(dev_t dev, struct fd_formb *finfo, struct thread *td)
2154 struct proc *p = td->td_proc;
2162 fdu = FDUNIT(minor(dev));
2163 fd = devclass_get_softc(fd_devclass, fdu);
2164 fdblk = 128 << fd->ft->secsize;
2166 /* set up a buffer header for fdstrategy() */
2167 bp = malloc(sizeof(struct buf), M_TEMP, M_WAITOK | M_ZERO);
2170 * keep the process from being swapped
2174 BUF_LOCK(bp, LK_EXCLUSIVE);
2175 bp->b_flags = B_PHYS | B_FORMAT;
2178 * calculate a fake blkno, so fdstrategy() would initiate a
2179 * seek to the requested cylinder
2181 bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
2182 + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
2184 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2185 bp->b_data = (caddr_t)finfo;
2187 /* now do the format */
2189 BUF_STRATEGY(bp, 0);
2191 /* ...and wait for it to complete */
2193 while(!(bp->b_flags & B_DONE)) {
2194 rv = tsleep((caddr_t)bp, 0, "fdform", 20 * hz);
2195 if (rv == EWOULDBLOCK)
2200 if (rv == EWOULDBLOCK) {
2203 device_unbusy(fd->dev);
2206 if (bp->b_flags & B_ERROR)
2209 * allow the process to be swapped
2219 * TODO: don't allocate buffer on stack.
2223 fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
2225 fdu_t fdu = FDUNIT(minor(dev));
2226 fd_p fd = devclass_get_softc(fd_devclass, fdu);
2229 struct fd_type *fdt;
2230 struct disklabel *dl;
2231 struct fdc_status *fsp;
2232 char buffer[DEV_BSIZE];
2235 fdblk = 128 << fd->ft->secsize;
2239 bzero(buffer, sizeof (buffer));
2240 dl = (struct disklabel *)buffer;
2241 dl->d_secsize = fdblk;
2243 dl->d_secpercyl = fdt->size / fdt->tracks;
2244 dl->d_type = DTYPE_FLOPPY;
2246 if (readdisklabel(dev, dl)
2252 *(struct disklabel *)addr = *dl;
2256 if ((flag & FWRITE) == 0)
2261 if ((flag & FWRITE) == 0)
2266 if ((flag & FWRITE) == 0) {
2271 dl = (struct disklabel *)addr;
2273 if ((error = setdisklabel((struct disklabel *)buffer, dl,
2277 error = writedisklabel(dev, (struct disklabel *)buffer);
2280 if ((flag & FWRITE) == 0)
2281 error = EBADF; /* must be opened for writing */
2282 else if (((struct fd_formb *)addr)->format_version !=
2284 error = EINVAL; /* wrong version of formatting prog */
2286 error = fdformat(dev, (struct fd_formb *)addr, td);
2289 case FD_GTYPE: /* get drive type */
2290 *(struct fd_type *)addr = *fd->ft;
2293 case FD_STYPE: /* set drive type */
2294 /* this is considered harmful; only allow for superuser */
2297 *fd->ft = *(struct fd_type *)addr;
2300 case FD_GOPTS: /* get drive options */
2301 *(int *)addr = fd->options;
2304 case FD_SOPTS: /* set drive options */
2305 fd->options = *(int *)addr;
2309 fsp = (struct fdc_status *)addr;
2310 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2312 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
2323 * Hello emacs, these are the
2326 * c-continued-statement-offset: 8
2327 * c-continued-brace-offset: 0
2328 * c-brace-offset: -8
2329 * c-brace-imaginary-offset: 0
2330 * c-argdecl-indent: 8
2331 * c-label-offset: -8
2332 * c++-hanging-braces: 1
2333 * c++-access-specifier-offset: -8
2334 * c++-empty-arglist-indent: 8
2335 * c++-friend-offset: 0