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 $
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/bootmaj.h>
62 #include <sys/kernel.h>
66 #include <sys/diskslice.h>
68 #include <sys/devicestat.h>
69 #include <sys/fcntl.h>
70 #include <sys/malloc.h>
71 #include <sys/module.h>
74 #include <sys/syslog.h>
75 #include <sys/device.h>
79 #include <sys/thread2.h>
81 #include <machine/clock.h>
82 #include <machine/inttypes.h>
83 #include <machine/ioctl_fd.h>
84 #include <machine/stdarg.h>
86 #include <bus/isa/isavar.h>
87 #include <bus/isa/isareg.h>
90 #include <bus/isa/rtc.h>
92 /* configuration flags */
93 #define FDC_PRETEND_D0 (1 << 0) /* pretend drive 0 to be there */
94 #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */
96 /* internally used only, not really from CMOS: */
97 #define RTCFDT_144M_PRETENDED 0x1000
99 /* error returns for fd_cmd() */
101 #define FD_NOT_VALID -2
102 #define FDC_ERRMAX 100 /* do not log more */
104 * Stop retrying after this many DMA overruns. Since each retry takes
105 * one revolution, with 300 rpm., 25 retries take approximately 10
106 * seconds which the read attempt will block in case the DMA overrun
109 #define FDC_DMAOV_MAX 25
112 * Timeout value for the PIO loops to wait until the FDC main status
113 * register matches our expectations (request for master, direction
114 * bit). This is supposed to be a number of microseconds, although
115 * timing might actually not be very accurate.
117 * Timeouts of 100 msec are believed to be required for some broken
120 #define FDSTS_TIMEOUT 100000
123 #define NUMDENS (NUMTYPES - 7)
125 /* These defines (-1) must match index for fd_types */
126 #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */
127 #define NO_TYPE 0 /* must match NO_TYPE in ft.c */
139 #define FD_1480in5_25 11
140 #define FD_1440in5_25 12
141 #define FD_820in5_25 13
142 #define FD_800in5_25 14
143 #define FD_720in5_25 15
144 #define FD_360in5_25 16
145 #define FD_640in5_25 17
148 static struct fd_type fd_types[NUMTYPES] =
150 { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
151 { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
152 { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
153 { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */
154 { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */
155 { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */
156 { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */
157 { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */
158 { 8,2,0xFF,0x2A,80,1280,1,FDC_250KBPS,2,0x50,1 }, /* 640K in DD 5.25in */
159 { 8,3,0xFF,0x35,77,1232,1,FDC_500KBPS,2,0x74,1 }, /* 1.23M in HD 5.25in */
161 { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
162 { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
163 { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */
164 { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */
165 { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */
166 { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */
167 { 8,2,0xFF,0x2A,80,1280,1,FDC_300KBPS,2,0x50,1 }, /* 640K in HD 5.25in */
170 #define DRVS_PER_CTLR 2 /* 2 floppies */
172 /***********************************************************************\
173 * Per controller structure. *
174 \***********************************************************************/
175 devclass_t fdc_devclass;
177 /***********************************************************************\
178 * Per drive structure. *
179 * N per controller (DRVS_PER_CTLR) *
180 \***********************************************************************/
182 struct fdc_data *fdc; /* pointer to controller structure */
183 int fdsu; /* this units number on this controller */
184 int type; /* Drive type (FD_1440...) */
185 struct fd_type ft; /* the type descriptor */
187 #define FD_OPEN 0x01 /* it's open */
188 #define FD_ACTIVE 0x02 /* it's active */
189 #define FD_MOTOR 0x04 /* motor should be on */
190 #define FD_MOTOR_WAIT 0x08 /* motor coming up */
193 #define FD_NO_TRACK -2
194 int track; /* where we think the head is */
195 int options; /* user configurable options, see ioctl_fd.h */
196 struct callout toffhandle;
197 struct callout tohandle;
198 struct callout motor;
200 struct devstat device_stats;
208 static devclass_t fd_devclass;
210 /***********************************************************************\
211 * Throughout this file the following conventions will be used: *
212 * fd is a pointer to the fd_data struct for the drive in question *
213 * fdc is a pointer to the fdc_data struct for the controller *
214 * fdu is the floppy drive unit number *
215 * fdcu is the floppy controller unit number *
216 * fdsu is the floppy drive unit number on that controller. (sub-unit) *
217 \***********************************************************************/
219 /* internal functions */
220 static void fdc_intr(void *);
221 static void set_motor(struct fdc_data *, int, int);
224 static timeout_t fd_turnoff;
225 static timeout_t fd_motor_on;
226 static void fd_turnon(struct fd_data *);
227 static void fdc_reset(fdc_p);
228 static int fd_in(struct fdc_data *, int *);
229 static int out_fdc(struct fdc_data *, int);
230 static void fdstart(struct fdc_data *);
231 static timeout_t fd_iotimeout;
232 static timeout_t fd_pseudointr;
233 static int fdstate(struct fdc_data *);
234 static int retrier(struct fdc_data *);
235 static int fdformat(cdev_t, struct fd_formb *, struct ucred *);
237 static int enable_fifo(fdc_p fdc);
239 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */
245 #define SEEKCOMPLETE 3
247 #define RECALCOMPLETE 5
253 #define IOTIMEDOUT 11
254 #define RESETCOMPLETE 12
258 static char const * const fdstates[] =
276 /* CAUTION: fd_debug causes huge amounts of logging output */
277 static int volatile fd_debug = 0;
278 #define TRACE0(arg) if(fd_debug) kprintf(arg)
279 #define TRACE1(arg1, arg2) if(fd_debug) kprintf(arg1, arg2)
280 #else /* FDC_DEBUG */
282 #define TRACE1(arg1, arg2)
283 #endif /* FDC_DEBUG */
286 fdout_wr(fdc_p fdc, u_int8_t v)
288 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
294 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
298 fddata_wr(fdc_p fdc, u_int8_t v)
300 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
306 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
310 fdctl_wr_isa(fdc_p fdc, u_int8_t v)
312 bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
320 return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
325 static d_open_t Fdopen; /* NOTE, not fdopen */
326 static d_close_t fdclose;
327 static d_ioctl_t fdioctl;
328 static d_strategy_t fdstrategy;
330 static struct dev_ops fd_ops = {
331 { "fd", FD_CDEV_MAJOR, D_DISK },
335 .d_write = physwrite,
337 .d_strategy = fdstrategy,
341 fdc_err(struct fdc_data *fdc, const char *s)
345 if (fdc->fdc_errs < FDC_ERRMAX)
346 device_printf(fdc->fdc_dev, "%s", s);
347 else if (fdc->fdc_errs == FDC_ERRMAX)
348 device_printf(fdc->fdc_dev, "too many errors, not "
349 "logging any more\n");
356 * fd_cmd: Send a command to the chip. Takes a varargs with this structure:
358 * # of output bytes, output bytes as ints ...,
359 * # of input bytes, input bytes as ints ...
362 fd_cmd(struct fdc_data *fdc, int n_out, ...)
369 __va_start(ap, n_out);
370 cmd = (u_char)(__va_arg(ap, int));
372 __va_start(ap, n_out);
373 for (n = 0; n < n_out; n++)
375 if (out_fdc(fdc, __va_arg(ap, int)) < 0)
378 ksnprintf(msg, sizeof(msg),
379 "cmd %x failed at out byte %d of %d\n",
381 return fdc_err(fdc, msg);
384 n_in = __va_arg(ap, int);
385 for (n = 0; n < n_in; n++)
387 int *ptr = __va_arg(ap, int *);
388 if (fd_in(fdc, ptr) < 0)
391 ksnprintf(msg, sizeof(msg),
392 "cmd %02x failed at in byte %d of %d\n",
394 return fdc_err(fdc, msg);
402 enable_fifo(fdc_p fdc)
406 if ((fdc->flags & FDC_HAS_FIFO) == 0) {
410 * Cannot use fd_cmd the normal way here, since
411 * this might be an invalid command. Thus we send the
412 * first byte, and check for an early turn of data directon.
415 if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
416 return fdc_err(fdc, "Enable FIFO failed\n");
418 /* If command is invalid, return */
420 while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
421 != NE7_RQM && j-- > 0) {
422 if (i == (NE7_DIO | NE7_RQM)) {
430 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
432 return fdc_err(fdc, "Enable FIFO failed\n");
434 fdc->flags |= FDC_HAS_FIFO;
438 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
439 return fdc_err(fdc, "Re-enable FIFO failed\n");
444 fd_sense_drive_status(fdc_p fdc, int *st3p)
448 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
450 return fdc_err(fdc, "Sense Drive Status failed\n");
459 fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
463 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
466 "sense intr err reading stat reg 0\n");
473 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
475 * There doesn't seem to have been an interrupt.
480 if (fd_in(fdc, &cyl) < 0) {
481 return fdc_err(fdc, "can't get cyl num\n");
492 fd_read_status(fdc_p fdc, int fdsu)
496 for (i = 0; i < 7; i++) {
498 * XXX types are poorly chosen. Only bytes can by read
499 * from the hardware, but fdc->status[] wants u_ints and
500 * fd_in() gives ints.
504 ret = fd_in(fdc, &status);
505 fdc->status[i] = status;
511 fdc->flags |= FDC_STAT_VALID;
513 fdc->flags &= ~FDC_STAT_VALID;
518 /****************************************************************************/
519 /* autoconfiguration stuff */
520 /****************************************************************************/
523 fdc_alloc_resources(struct fdc_data *fdc)
529 ispnp = (fdc->flags & FDC_ISPNP) != 0;
530 ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
531 fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
532 fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
535 * On standard ISA, we don't just use an 8 port range
536 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
539 * Isn't PC hardware wonderful.
541 * The Y-E Data PCMCIA FDC doesn't have this problem, it
542 * uses the register with offset 6 for pseudo-DMA, and the
543 * one with offset 7 as control register.
545 fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
546 &fdc->rid_ioport, 0ul, ~0ul,
547 ispcmcia ? 8 : (ispnp ? 1 : 6),
549 if (fdc->res_ioport == 0) {
550 device_printf(dev, "cannot reserve I/O port range\n");
553 fdc->portt = rman_get_bustag(fdc->res_ioport);
554 fdc->porth = rman_get_bushandle(fdc->res_ioport);
558 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
559 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
560 * by checking the size and adjust the port address
563 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
567 * Register the control port range as rid 1 if it
568 * isn't there already. Most PnP BIOSen will have
569 * already done this but non-PnP configurations don't.
571 * And some (!!) report 0x3f2-0x3f5 and completely
572 * leave out the control register! It seems that some
573 * non-antique controller chips have a different
574 * method of programming the transfer speed which
575 * doesn't require the control register, but it's
576 * mighty bogus as the chip still responds to the
577 * address for the control register.
579 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
582 /* Find the control port, usually 0x3f7 */
583 ctlstart = rman_get_start(fdc->res_ioport) +
586 bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
590 * Now (finally!) allocate the control port.
593 fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
595 0ul, ~0ul, 1, RF_ACTIVE);
596 if (fdc->res_ctl == 0) {
598 "cannot reserve control I/O port range\n");
601 fdc->ctlt = rman_get_bustag(fdc->res_ctl);
602 fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
605 fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
606 &fdc->rid_irq, 0ul, ~0ul, 1,
608 if (fdc->res_irq == 0) {
609 device_printf(dev, "cannot reserve interrupt line\n");
613 if ((fdc->flags & FDC_NODMA) == 0) {
614 fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
615 &fdc->rid_drq, 0ul, ~0ul, 1,
617 if (fdc->res_drq == 0) {
618 device_printf(dev, "cannot reserve DMA request line\n");
621 fdc->dmachan = fdc->res_drq->r_start;
628 fdc_release_resources(struct fdc_data *fdc)
633 if (fdc->res_irq != 0) {
634 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
636 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
639 if (fdc->res_ctl != 0) {
640 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
642 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
645 if (fdc->res_ioport != 0) {
646 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
648 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
651 if (fdc->res_drq != 0) {
652 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
654 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
659 /****************************************************************************/
660 /* autoconfiguration stuff */
661 /****************************************************************************/
663 static struct isa_pnp_id fdc_ids[] = {
664 {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
665 {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
670 fdc_read_ivar(device_t dev, device_t child, int which, u_long *result)
672 struct fdc_ivars *ivars = device_get_ivars(child);
675 case FDC_IVAR_FDUNIT:
676 *result = ivars->fdunit;
685 * fdc controller section.
688 fdc_probe(device_t dev)
691 struct fdc_data *fdc;
693 fdc = device_get_softc(dev);
694 bzero(fdc, sizeof *fdc);
696 fdc->fdctl_wr = fdctl_wr_isa;
699 error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
703 fdc->flags |= FDC_ISPNP;
705 /* Attempt to allocate our resources for the duration of the probe */
706 error = fdc_alloc_resources(fdc);
710 /* First - lets reset the floppy controller */
713 fdout_wr(fdc, FDO_FRST);
715 /* see if it can handle a command */
716 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
717 NE7_SPEC_2(2, 0), 0)) {
722 if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
723 ic_type = (u_char)ic_type;
726 device_set_desc(dev, "NEC 765 or clone");
727 fdc->fdct = FDC_NE765;
730 device_set_desc(dev, "Intel 82077 or clone");
731 fdc->fdct = FDC_I82077;
734 device_set_desc(dev, "NEC 72065B or clone");
735 fdc->fdct = FDC_NE72065;
738 device_set_desc(dev, "generic floppy controller");
739 fdc->fdct = FDC_UNKNOWN;
745 fdc_release_resources(fdc);
750 * Add a child device to the fdc controller. It will then be probed etc.
753 fdc_add_child(device_t dev, const char *name, int unit)
756 struct fdc_ivars *ivar;
759 ivar = kmalloc(sizeof *ivar, M_DEVBUF /* XXX */, M_WAITOK | M_ZERO);
760 if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
762 child = device_add_child(dev, name, unit);
765 device_set_ivars(child, ivar);
766 if (resource_int_value(name, unit, "disabled", &disabled) == 0
768 device_disable(child);
772 fdc_attach(device_t dev)
774 struct fdc_data *fdc;
777 fdc = device_get_softc(dev);
779 callout_init(&fdc->pseudointr_ch);
781 error = fdc_alloc_resources(fdc);
783 device_printf(dev, "cannot reacquire resources\n");
786 error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
788 &fdc->fdc_intr, NULL);
790 device_printf(dev, "cannot setup interrupt\n");
793 fdc->fdcu = device_get_unit(dev);
794 fdc->flags |= FDC_ATTACHED;
796 if ((fdc->flags & FDC_NODMA) == 0) {
797 /* Acquire the DMA channel forever, The driver will do the rest */
798 /* XXX should integrate with rman */
799 isa_dma_acquire(fdc->dmachan);
800 isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
802 fdc->state = DEVIDLE;
804 /* reset controller, turn motor off, clear fdout mirror reg */
805 fdout_wr(fdc, ((fdc->fdout = 0)));
806 bioq_init(&fdc->bio_queue);
809 * Probe and attach any children. We should probably detect
810 * devices from the BIOS unless overridden.
812 for (i = resource_query_string(-1, "at", device_get_nameunit(dev));
814 i = resource_query_string(i, "at", device_get_nameunit(dev)))
815 fdc_add_child(dev, resource_query_name(i),
816 resource_query_unit(i));
818 return (bus_generic_attach(dev));
822 fdc_print_child(device_t me, device_t child)
826 retval += bus_print_child_header(me, child);
827 retval += kprintf(" on %s drive %d\n", device_get_nameunit(me),
828 fdc_get_fdunit(child));
833 static device_method_t fdc_methods[] = {
834 /* Device interface */
835 DEVMETHOD(device_probe, fdc_probe),
836 DEVMETHOD(device_attach, fdc_attach),
837 DEVMETHOD(device_detach, bus_generic_detach),
838 DEVMETHOD(device_shutdown, bus_generic_shutdown),
839 DEVMETHOD(device_suspend, bus_generic_suspend),
840 DEVMETHOD(device_resume, bus_generic_resume),
843 DEVMETHOD(bus_print_child, fdc_print_child),
844 DEVMETHOD(bus_read_ivar, fdc_read_ivar),
845 /* Our children never use any other bus interface methods. */
850 static driver_t fdc_driver = {
853 sizeof(struct fdc_data)
856 DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
857 DRIVER_MODULE(fdc, acpi, fdc_driver, fdc_devclass, 0, 0);
859 /******************************************************************/
861 * devices attached to the controller section.
864 fd_probe(device_t dev)
869 struct fdc_data *fdc;
871 static int fd_fifo = 0;
873 fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
874 fd = device_get_softc(dev);
875 fdc = device_get_softc(device_get_parent(dev));
877 bzero(fd, sizeof *fd);
881 fd->fdu = device_get_unit(dev);
884 /* look up what bios thinks we have */
887 if ((fdc->flags & FDC_ISPCMCIA))
889 else if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0)
890 fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
892 fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
895 fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
902 fdt = RTCFDT_144M; /* XXX probably */
905 /* is there a unit? */
906 if (fdt == RTCFDT_NONE)
910 set_motor(fdc, fdsu, TURNON);
911 DELAY(1000000); /* 1 sec */
913 /* XXX This doesn't work before the first set_motor() */
914 if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN
915 && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0
916 && enable_fifo(fdc) == 0) {
917 device_printf(device_get_parent(dev),
918 "FIFO enabled, %d bytes threshold\n", fifo_threshold);
922 if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0)
923 && (st3 & NE7_ST3_T0)) {
924 /* if at track 0, first seek inwards */
925 /* seek some steps: */
926 fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0);
927 DELAY(300000); /* ...wait a moment... */
928 fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
931 /* If we're at track 0 first seek inwards. */
932 if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
933 /* Seek some steps... */
934 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
935 /* ...wait a moment... */
937 /* make ctrlr happy: */
938 fd_sense_int(fdc, 0, 0);
942 for (i = 0; i < 2; i++) {
944 * we must recalibrate twice, just in case the
945 * heads have been beyond cylinder 76, since most
946 * FDCs still barf when attempting to recalibrate
950 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
951 /* a second being enough for full stroke seek*/
952 DELAY(i == 0 ? 1000000 : 300000);
954 /* anything responding? */
955 if (fd_sense_int(fdc, &st0, 0) == 0 &&
956 (st0 & NE7_ST0_EC) == 0)
957 break; /* already probed succesfully */
961 set_motor(fdc, fdsu, TURNOFF);
963 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
966 fd->track = FD_NO_TRACK;
970 callout_init(&fd->toffhandle);
971 callout_init(&fd->tohandle);
972 callout_init(&fd->motor);
976 device_set_desc(dev, "1200-KB 5.25\" drive");
979 case RTCFDT_144M | RTCFDT_144M_PRETENDED:
980 device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive");
984 device_set_desc(dev, "1440-KB 3.5\" drive");
989 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
993 device_set_desc(dev, "360-KB 5.25\" drive");
997 kprintf("720-KB 3.5\" drive");
1003 fd->ft = fd_types[fd->type - 1];
1008 fd_attach(device_t dev)
1010 struct disk_info info;
1014 fd = device_get_softc(dev);
1016 disk_create(fd->fdu, &fd->disk, &fd_ops);
1017 disk_setdisktype(&fd->disk, "floppy");
1020 * Make special raw floppy devices with preset types to
1021 * make formatting easier. These override the disk management
1022 * layer for the whole-slice-disk for partitions 128-191. Note
1023 * that we do not override partition 255, which is the
1024 * whole-slice-part. If we did we would have to provide our
1025 * own DIOCGPART ioctl.
1027 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 1),
1028 UID_ROOT, GID_WHEEL, 0600, "fd%d.1720", fd->fdu);
1029 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 2),
1030 UID_ROOT, GID_WHEEL, 0600, "fd%d.1480", fd->fdu);
1031 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 3),
1032 UID_ROOT, GID_WHEEL, 0600, "fd%d.1440", fd->fdu);
1033 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 4),
1034 UID_ROOT, GID_WHEEL, 0600, "fd%d.1200", fd->fdu);
1035 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 5),
1036 UID_ROOT, GID_WHEEL, 0600, "fd%d.820", fd->fdu);
1037 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 6),
1038 UID_ROOT, GID_WHEEL, 0600, "fd%d.800", fd->fdu);
1039 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 7),
1040 UID_ROOT, GID_WHEEL, 0600, "fd%d.720", fd->fdu);
1041 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 8),
1042 UID_ROOT, GID_WHEEL, 0600, "fd%d.360", fd->fdu);
1043 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 9),
1044 UID_ROOT, GID_WHEEL, 0600, "fd%d.640", fd->fdu);
1045 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 10),
1046 UID_ROOT, GID_WHEEL, 0600, "fd%d.1232", fd->fdu);
1048 devstat_add_entry(&fd->device_stats, device_get_name(dev),
1049 device_get_unit(dev), 512, DEVSTAT_NO_ORDERED_TAGS,
1050 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
1051 DEVSTAT_PRIORITY_FD);
1053 if (fd->type != NO_TYPE) {
1054 bzero(&info, sizeof(info));
1055 ft = &fd_types[fd->type - 1];
1056 info.d_media_blksize = 128 << ft->secsize;
1057 info.d_media_blocks = ft->size;
1058 info.d_dsflags = DSO_COMPATPARTA | DSO_COMPATMBR;
1059 info.d_nheads = ft->heads;
1060 info.d_secpertrack = ft->sectrac;
1061 info.d_secpercyl = ft->sectrac * ft->heads;
1062 info.d_ncylinders = ft->size / info.d_secpercyl;
1063 disk_setdiskinfo(&fd->disk, &info);
1069 fd_detach(device_t dev)
1073 fd = device_get_softc(dev);
1074 kprintf("devfs: Please make sure that only the right fd device was removed!!!\n");
1075 dev_ops_remove_minor(&fd_ops,
1076 /*dkunitmask() | dkmakeslice(-1) | dkmakepart(128|64),*/
1077 dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128));
1078 disk_invalidate(&fd->disk);
1079 disk_destroy(&fd->disk);
1080 devstat_remove_entry(&fd->device_stats);
1081 callout_stop(&fd->toffhandle);
1082 callout_stop(&fd->motor);
1087 static device_method_t fd_methods[] = {
1088 /* Device interface */
1089 DEVMETHOD(device_probe, fd_probe),
1090 DEVMETHOD(device_attach, fd_attach),
1091 DEVMETHOD(device_detach, fd_detach),
1092 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1093 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
1094 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
1099 static driver_t fd_driver = {
1102 sizeof(struct fd_data)
1105 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
1107 /****************************************************************************/
1108 /* motor control stuff */
1109 /* remember to not deselect the drive we're working on */
1110 /****************************************************************************/
1112 set_motor(struct fdc_data *fdc, int fdsu, int turnon)
1114 int fdout = fdc->fdout;
1115 int needspecify = 0;
1118 fdout &= ~FDO_FDSEL;
1119 fdout |= (FDO_MOEN0 << fdsu) + fdsu;
1121 fdout &= ~(FDO_MOEN0 << fdsu);
1124 && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
1125 /* gonna turn off the last drive, put FDC to bed */
1126 fdout &= ~ (FDO_FRST|FDO_FDMAEN);
1128 /* make sure controller is selected and specified */
1129 if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
1131 fdout |= (FDO_FRST|FDO_FDMAEN);
1134 fdout_wr(fdc, fdout);
1136 TRACE1("[0x%x->FDOUT]", fdout);
1141 * special case: since we have just woken up the FDC
1142 * from its sleep, we silently assume the command will
1143 * be accepted, and do not test for a timeout
1145 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1146 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1148 if (fdc->flags & FDC_HAS_FIFO)
1149 (void) enable_fifo(fdc);
1154 fd_turnoff(void *xfd)
1158 TRACE1("[fd%d: turnoff]", fd->fdu);
1162 * Don't turn off the motor yet if the drive is active.
1164 * If we got here, this could only mean we missed an interrupt.
1165 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
1166 * after a controller reset. Just schedule a pseudo-interrupt
1167 * so the state machine gets re-entered.
1169 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
1175 fd->flags &= ~FD_MOTOR;
1176 set_motor(fd->fdc, fd->fdsu, TURNOFF);
1181 fd_motor_on(void *xfd)
1186 fd->flags &= ~FD_MOTOR_WAIT;
1187 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
1197 if(!(fd->flags & FD_MOTOR))
1199 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
1200 set_motor(fd->fdc, fd->fdsu, TURNON);
1201 callout_reset(&fd->motor, hz, fd_motor_on, fd);
1206 fdc_reset(fdc_p fdc)
1208 /* Try a reset, keep motor on */
1209 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1210 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1212 /* enable FDC, but defer interrupts a moment */
1213 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
1214 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
1216 fdout_wr(fdc, fdc->fdout);
1217 TRACE1("[0x%x->FDOUT]", fdc->fdout);
1219 /* XXX after a reset, silently believe the FDC will accept commands */
1220 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1221 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1223 if (fdc->flags & FDC_HAS_FIFO)
1224 (void) enable_fifo(fdc);
1227 /****************************************************************************/
1229 /****************************************************************************/
1231 * FDC IO functions, take care of the main status register, timeout
1232 * in case the desired status bits are never set.
1234 * These PIO loops initially start out with short delays between
1235 * each iteration in the expectation that the required condition
1236 * is usually met quickly, so it can be handled immediately. After
1237 * about 1 ms, stepping is increased to achieve a better timing
1238 * accuracy in the calls to DELAY().
1241 fd_in(struct fdc_data *fdc, int *ptr)
1245 for (j = 0, step = 1;
1246 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) &&
1250 return (fdc_err(fdc, "ready for output in input\n"));
1255 if (j >= FDSTS_TIMEOUT)
1256 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
1259 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1262 #else /* !FDC_DEBUG */
1267 #endif /* FDC_DEBUG */
1271 out_fdc(struct fdc_data *fdc, int x)
1275 for (j = 0, step = 1;
1276 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != NE7_RQM &&
1279 if (i == (NE7_DIO|NE7_RQM))
1280 return (fdc_err(fdc, "ready for input in output\n"));
1285 if (j >= FDSTS_TIMEOUT)
1286 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
1288 /* Send the command and return */
1290 TRACE1("[0x%x->FDDATA]", x);
1294 /****************************************************************************/
1295 /* fdopen/fdclose */
1296 /****************************************************************************/
1298 Fdopen(struct dev_open_args *ap)
1300 cdev_t dev = ap->a_head.a_dev;
1301 fdu_t fdu = dkunit(dev);
1302 struct disk_info info;
1310 if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1313 if ((fdc == NULL) || (fd->type == NO_TYPE))
1317 * Figure out the type of floppy. There are special whole-disk-device
1318 * overrides that will override the current type.
1321 if (type == WHOLE_SLICE_PART) {
1322 type = fd->type; /* do not change selected type data */
1324 } else if (type > 128) {
1325 type -= 128; /* set to specific format */
1328 type = fd->type; /* reset to default */
1333 if (type != fd->type) {
1335 * For each type of basic drive, make sure we are trying
1336 * to open a type it can do,
1351 type = FD_1480in5_25;
1354 type = FD_1440in5_25;
1359 type = FD_820in5_25;
1362 type = FD_800in5_25;
1365 type = FD_720in5_25;
1368 type = FD_640in5_25;
1371 type = FD_360in5_25;
1378 if ( type != FD_1720
1392 * fd->type is the basic drive type, not the current format
1393 * we are reading. We only change the type when opening the
1394 * whole-slice-partition
1397 fd->ft = fd_types[type - 1];
1398 fd->flags |= FD_OPEN;
1401 * Clearing the DMA overrun counter at open time is a bit messy.
1402 * Since we're only managing one counter per controller, opening
1403 * the second drive could mess it up. Anyway, if the DMA overrun
1404 * condition is really persistent, it will eventually time out
1405 * still. OTOH, clearing it here will ensure we'll at least start
1406 * trying again after a previous (maybe even long ago) failure.
1407 * Also, this is merely a stop-gap measure only that should not
1408 * happen during normal operation, so we can tolerate it to be a
1409 * bit sloppy about this.
1411 fdc->dma_overruns = 0;
1414 * Set disk parameters for the disk management layer.
1416 * Note that we do not set RAWEXTENSIONS here. We override
1417 * the minor numbers in the raw-extension range and handle them
1420 bzero(&info, sizeof(info));
1422 info.d_media_blksize = 128 << ft->secsize;
1423 info.d_media_blocks = ft->size;
1424 info.d_dsflags = DSO_COMPATPARTA | DSO_COMPATMBR;
1425 info.d_nheads = ft->heads;
1426 info.d_secpertrack = ft->sectrac;
1427 info.d_secpercyl = ft->sectrac * ft->heads;
1428 info.d_ncylinders = ft->size / info.d_secpercyl;
1429 disk_setdiskinfo(&fd->disk, &info);
1435 fdclose(struct dev_close_args *ap)
1437 cdev_t dev = ap->a_head.a_dev;
1438 fdu_t fdu = dkunit(dev);
1441 fd = devclass_get_softc(fd_devclass, fdu);
1442 fd->flags &= ~FD_OPEN;
1443 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG);
1448 /****************************************************************************/
1450 /****************************************************************************/
1452 fdstrategy(struct dev_strategy_args *ap)
1454 cdev_t dev = ap->a_head.a_dev;
1455 struct bio *bio = ap->a_bio;
1456 struct buf *bp = bio->bio_buf;
1457 unsigned nblocks, blknum, cando;
1464 fd = devclass_get_softc(fd_devclass, fdu);
1466 panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
1467 (u_long)major(dev), (u_long)minor(dev));
1469 if (fd->type == NO_TYPE) {
1470 bp->b_error = ENXIO;
1471 bp->b_flags |= B_ERROR;
1475 fdblk = 128 << (fd->ft.secsize);
1476 if (bp->b_cmd != BUF_CMD_FORMAT) {
1477 if (bio->bio_offset < 0) {
1479 "fd%d: fdstrat: bad request offset = %"PRId64", bcount = %d\n",
1480 fdu, bio->bio_offset, bp->b_bcount);
1481 bp->b_error = EINVAL;
1482 bp->b_flags |= B_ERROR;
1485 if ((bp->b_bcount % fdblk) != 0) {
1486 bp->b_error = EINVAL;
1487 bp->b_flags |= B_ERROR;
1493 * Set up block calculations.
1495 if (bio->bio_offset > 20000000LL * fdblk) {
1497 * Reject unreasonably high block number, prevent the
1498 * multiplication below from overflowing.
1500 bp->b_error = EINVAL;
1501 bp->b_flags |= B_ERROR;
1504 blknum = (unsigned)(bio->bio_offset / fdblk);
1505 nblocks = fd->ft.size;
1507 if (blknum + (bp->b_bcount / fdblk) > nblocks) {
1508 if (blknum <= nblocks) {
1509 cando = (nblocks - blknum) * fdblk;
1510 bp->b_resid = bp->b_bcount - cando;
1512 goto bad; /* not actually bad but EOF */
1514 bp->b_error = EINVAL;
1515 bp->b_flags |= B_ERROR;
1520 bio->bio_driver_info = dev;
1521 bioqdisksort(&fdc->bio_queue, bio);
1522 callout_stop(&fd->toffhandle);
1524 /* Tell devstat we are starting on the transaction */
1525 devstat_start_transaction(&fd->device_stats);
1527 device_busy(fd->dev);
1538 /***************************************************************\
1540 * We have just queued something.. if the controller is not busy *
1541 * then simulate the case where it has just finished a command *
1542 * So that it (the interrupt routine) looks on the queue for more*
1543 * work to do and picks up what we just added. *
1544 * If the controller is already busy, we need do nothing, as it *
1545 * will pick up our work when the present work completes *
1546 \***************************************************************/
1548 fdstart(struct fdc_data *fdc)
1551 if(fdc->state == DEVIDLE)
1559 fd_iotimeout(void *xfdc)
1564 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
1567 * Due to IBM's brain-dead design, the FDC has a faked ready
1568 * signal, hardwired to ready == true. Thus, any command
1569 * issued if there's no diskette in the drive will _never_
1570 * complete, and must be aborted by resetting the FDC.
1571 * Many thanks, Big Blue!
1572 * The FDC must not be reset directly, since that would
1573 * interfere with the state machine. Instead, pretend that
1574 * the command completed but was invalid. The state machine
1575 * will reset the FDC and retry once.
1578 fdc->status[0] = NE7_ST0_IC_IV;
1579 fdc->flags &= ~FDC_STAT_VALID;
1580 fdc->state = IOTIMEDOUT;
1585 /* just ensure it is running in a critical section */
1587 fd_pseudointr(void *xfdc)
1594 /***********************************************************************\
1596 * keep calling the state machine until it returns a 0 *
1597 * ALWAYS called at SPLBIO *
1598 \***********************************************************************/
1600 fdc_intr(void *xfdc)
1608 * magic pseudo-DMA initialization for YE FDC. Sets count and
1611 #define SET_BCDR(fdc,wr,cnt,port) \
1612 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \
1613 ((cnt)-1) & 0xff); \
1614 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
1615 ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
1618 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy
1620 static int fdcpio(fdc_p fdc, buf_cmd_t cmd, caddr_t addr, u_int count)
1622 u_char *cptr = (u_char *)addr;
1624 if (cmd == BUF_CMD_READ) {
1625 if (fdc->state != PIOREAD) {
1626 fdc->state = PIOREAD;
1629 SET_BCDR(fdc, 0, count, 0);
1630 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1631 FDC_YE_DATAPORT, cptr, count);
1633 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1634 FDC_YE_DATAPORT, cptr, count);
1635 SET_BCDR(fdc, 0, count, 0);
1640 /***********************************************************************\
1641 * The controller state machine. *
1642 * if it returns a non zero value, it should be called again immediatly *
1643 \***********************************************************************/
1647 int read, format, head, i, sec = 0, sectrac, st0, cyl, st3;
1648 unsigned blknum = 0, b_cylinder = 0;
1653 struct fd_formb *finfo = NULL;
1659 bio = bioq_first(&fdc->bio_queue);
1661 bioq_remove(&fdc->bio_queue, bio);
1666 /***********************************************\
1667 * nothing left for this controller to do *
1668 * Force into the IDLE state, *
1669 \***********************************************/
1670 fdc->state = DEVIDLE;
1672 device_printf(fdc->fdc_dev,
1673 "unexpected valid fd pointer\n");
1677 TRACE1("[fdc%d IDLE]", fdc->fdcu);
1681 dev = bio->bio_driver_info;
1684 fd = devclass_get_softc(fd_devclass, fdu);
1685 fdblk = 128 << fd->ft.secsize;
1686 if (fdc->fd && (fd != fdc->fd))
1687 device_printf(fd->dev, "confused fd pointers\n");
1688 read = (bp->b_cmd == BUF_CMD_READ);
1689 format = (bp->b_cmd == BUF_CMD_FORMAT);
1691 finfo = (struct fd_formb *)bp->b_data;
1692 fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1695 if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
1696 blknum = (unsigned)(bio->bio_offset / fdblk) +
1698 b_cylinder = blknum / (fd->ft.sectrac * fd->ft.heads);
1700 TRACE1("fd%d", fdu);
1701 TRACE1("[%s]", fdstates[fdc->state]);
1702 TRACE1("(0x%x)", fd->flags);
1703 callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
1707 case FINDWORK: /* we have found new work */
1712 fdc->fdctl_wr(fdc, fd->ft.trans);
1713 TRACE1("[0x%x->FDCTL]", fd->ft.trans);
1714 /*******************************************************\
1715 * If the next drive has a motor startup pending, then *
1716 * it will start up in its own good time *
1717 \*******************************************************/
1718 if(fd->flags & FD_MOTOR_WAIT) {
1719 fdc->state = MOTORWAIT;
1720 return (0); /* come back later */
1722 /*******************************************************\
1723 * Maybe if it's not starting, it SHOULD be starting *
1724 \*******************************************************/
1725 if (!(fd->flags & FD_MOTOR))
1727 fdc->state = MOTORWAIT;
1731 else /* at least make sure we are selected */
1733 set_motor(fdc, fd->fdsu, TURNON);
1735 if (fdc->flags & FDC_NEEDS_RESET) {
1736 fdc->state = RESETCTLR;
1737 fdc->flags &= ~FDC_NEEDS_RESET;
1739 fdc->state = DOSEEK;
1742 if (b_cylinder == (unsigned)fd->track)
1744 fdc->state = SEEKCOMPLETE;
1747 if (fd_cmd(fdc, 3, NE7CMD_SEEK,
1748 fd->fdsu, b_cylinder * fd->ft.steptrac,
1752 * seek command not accepted, looks like
1753 * the FDC went off to the Saints...
1755 fdc->retry = 6; /* try a reset */
1756 return(retrier(fdc));
1758 fd->track = FD_NO_TRACK;
1759 fdc->state = SEEKWAIT;
1760 return(0); /* will return later */
1762 /* allow heads to settle */
1763 callout_reset(&fdc->pseudointr_ch, hz / 16,
1764 fd_pseudointr, fdc);
1765 fdc->state = SEEKCOMPLETE;
1766 return(0); /* will return later */
1767 case SEEKCOMPLETE : /* SEEK DONE, START DMA */
1768 /* Make sure seek really happened*/
1769 if(fd->track == FD_NO_TRACK) {
1770 int descyl = b_cylinder * fd->ft.steptrac;
1773 * This might be a "ready changed" interrupt,
1774 * which cannot really happen since the
1775 * RDY pin is hardwired to + 5 volts. This
1776 * generally indicates a "bouncing" intr
1777 * line, so do one of the following:
1779 * When running on an enhanced FDC that is
1780 * known to not go stuck after responding
1781 * with INVALID, fetch all interrupt states
1782 * until seeing either an INVALID or a
1783 * real interrupt condition.
1785 * When running on a dumb old NE765, give
1786 * up immediately. The controller will
1787 * provide up to four dummy RC interrupt
1788 * conditions right after reset (for the
1789 * corresponding four drives), so this is
1790 * our only chance to get notice that it
1791 * was not the FDC that caused the interrupt.
1793 if (fd_sense_int(fdc, &st0, &cyl)
1796 if(fdc->fdct == FDC_NE765
1797 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1798 return 0; /* hope for a real intr */
1799 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1804 * seek to cyl 0 requested; make sure we are
1807 if (fd_sense_drive_status(fdc, &st3))
1809 if ((st3 & NE7_ST3_T0) == 0) {
1811 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
1812 fdu, st3, NE7_ST3BITS);
1819 return (retrier(fdc));
1823 if (cyl != descyl) {
1825 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
1826 fdu, descyl, cyl, st0);
1829 return (retrier(fdc));
1833 fd->track = b_cylinder;
1834 if (!(fdc->flags & FDC_NODMA)) {
1835 isa_dmastart(isa_dmabp(bp),
1836 bp->b_data+fd->skip,
1837 format ? bp->b_bcount : fdblk, fdc->dmachan);
1839 sectrac = fd->ft.sectrac;
1840 sec = blknum % (sectrac * fd->ft.heads);
1841 head = sec / sectrac;
1842 sec = sec % sectrac + 1;
1843 fd->hddrv = ((head&1)<<2)+fdu;
1847 /* make sure the drive is writable */
1848 if(fd_sense_drive_status(fdc, &st3) != 0)
1850 /* stuck controller? */
1851 if (!(fdc->flags & FDC_NODMA))
1852 isa_dmadone(isa_dmabp(bp),
1853 bp->b_data + fd->skip,
1854 format ? bp->b_bcount : fdblk,
1856 fdc->retry = 6; /* reset the beast */
1857 return (retrier(fdc));
1859 if(st3 & NE7_ST3_WP)
1862 * XXX YES! this is ugly.
1863 * in order to force the current operation
1864 * to fail, we will have to fake an FDC
1865 * error - all error handling is done
1868 fdc->status[0] = NE7_ST0_IC_AT;
1869 fdc->status[1] = NE7_ST1_NW;
1871 fdc->status[3] = fd->track;
1872 fdc->status[4] = head;
1873 fdc->status[5] = sec;
1874 fdc->retry = 8; /* break out immediately */
1875 fdc->state = IOTIMEDOUT; /* not really... */
1881 if (fdc->flags & FDC_NODMA) {
1883 * This seems to be necessary for
1884 * whatever obscure reason; if we omit
1885 * it, we end up filling the sector ID
1886 * fields of the newly formatted track
1887 * entirely with garbage, causing
1888 * `wrong cylinder' errors all over
1889 * the place when trying to read them
1894 SET_BCDR(fdc, 1, bp->b_bcount, 0);
1896 (void)fdcpio(fdc,bp->b_cmd,
1897 bp->b_data+fd->skip,
1902 if(fd_cmd(fdc, 6, NE7CMD_FORMAT, head << 2 | fdu,
1903 finfo->fd_formb_secshift,
1904 finfo->fd_formb_nsecs,
1905 finfo->fd_formb_gaplen,
1906 finfo->fd_formb_fillbyte, 0)) {
1907 /* controller fell over */
1908 if (!(fdc->flags & FDC_NODMA))
1909 isa_dmadone(isa_dmabp(bp),
1910 bp->b_data + fd->skip,
1911 format ? bp->b_bcount : fdblk,
1914 return (retrier(fdc));
1917 if (fdc->flags & FDC_NODMA) {
1919 * this seems to be necessary even when
1922 SET_BCDR(fdc, 1, fdblk, 0);
1925 * perform the write pseudo-DMA before
1926 * the WRITE command is sent
1929 (void)fdcpio(fdc,bp->b_cmd,
1930 bp->b_data+fd->skip,
1934 (read ? NE7CMD_READ : NE7CMD_WRITE),
1935 head << 2 | fdu, /* head & unit */
1936 fd->track, /* track */
1938 sec, /* sector + 1 */
1939 fd->ft.secsize, /* sector size */
1940 sectrac, /* sectors/track */
1941 fd->ft.gap, /* gap size */
1942 fd->ft.datalen, /* data length */
1944 /* the beast is sleeping again */
1945 if (!(fdc->flags & FDC_NODMA))
1946 isa_dmadone(isa_dmabp(bp),
1947 bp->b_data + fd->skip,
1948 format ? bp->b_bcount : fdblk,
1951 return (retrier(fdc));
1954 if (fdc->flags & FDC_NODMA)
1956 * if this is a read, then simply await interrupt
1957 * before performing PIO
1959 if (read && !fdcpio(fdc,bp->b_cmd,
1960 bp->b_data+fd->skip,fdblk)) {
1961 callout_reset(&fd->tohandle, hz,
1963 return(0); /* will return later */
1967 * write (or format) operation will fall through and
1968 * await completion interrupt
1970 fdc->state = IOCOMPLETE;
1971 callout_reset(&fd->tohandle, hz, fd_iotimeout, fdc);
1972 return (0); /* will return later */
1975 * actually perform the PIO read. The IOCOMPLETE case
1976 * removes the timeout for us.
1978 (void)fdcpio(fdc,bp->b_cmd,bp->b_data+fd->skip,fdblk);
1979 fdc->state = IOCOMPLETE;
1981 case IOCOMPLETE: /* IO DONE, post-analyze */
1982 callout_stop(&fd->tohandle);
1984 if (fd_read_status(fdc, fd->fdsu)) {
1985 if (!(fdc->flags & FDC_NODMA)) {
1986 isa_dmadone(isa_dmabp(bp),
1987 bp->b_data + fd->skip,
1988 format ? bp->b_bcount : fdblk,
1992 fdc->retry = 6; /* force a reset */
1993 return (retrier(fdc));
1996 fdc->state = IOTIMEDOUT;
2001 if (!(fdc->flags & FDC_NODMA)) {
2002 isa_dmadone(isa_dmabp(bp),
2003 bp->b_data + fd->skip,
2004 format ? bp->b_bcount : fdblk, fdc->dmachan);
2006 if (fdc->status[0] & NE7_ST0_IC) {
2007 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2008 && fdc->status[1] & NE7_ST1_OR) {
2010 * DMA overrun. Someone hogged the bus and
2011 * didn't release it in time for the next
2014 * We normally restart this without bumping
2015 * the retry counter. However, in case
2016 * something is seriously messed up (like
2017 * broken hardware), we rather limit the
2018 * number of retries so the IO operation
2019 * doesn't block indefinately.
2021 if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
2022 fdc->state = SEEKCOMPLETE;
2024 } /* else fall through */
2026 if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
2028 fdc->retry = 6; /* force a reset */
2029 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2030 && fdc->status[2] & NE7_ST2_WC
2032 fdc->retry = 3; /* force recalibrate */
2033 return (retrier(fdc));
2036 /* Operation successful, retry DMA overruns again next time. */
2037 fdc->dma_overruns = 0;
2039 if (!format && fd->skip < bp->b_bcount - bp->b_resid) {
2040 /* set up next transfer */
2041 fdc->state = DOSEEK;
2047 device_unbusy(fd->dev);
2049 devstat_end_transaction_buf(&fd->device_stats, bp);
2053 fdc->state = FINDWORK;
2059 fdc->state = RESETCOMPLETE;
2063 * Discard all the results from the reset so that they
2064 * can't cause an unexpected interrupt later.
2066 for (i = 0; i < 4; i++)
2067 (void)fd_sense_int(fdc, &st0, &cyl);
2068 fdc->state = STARTRECAL;
2071 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2074 return (retrier(fdc));
2076 fdc->state = RECALWAIT;
2077 return (0); /* will return later */
2079 /* allow heads to settle */
2080 callout_reset(&fdc->pseudointr_ch, hz / 8, fd_pseudointr, fdc);
2081 fdc->state = RECALCOMPLETE;
2082 return (0); /* will return later */
2086 * See SEEKCOMPLETE for a comment on this:
2088 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2090 if(fdc->fdct == FDC_NE765
2091 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2092 return 0; /* hope for a real intr */
2093 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2094 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
2098 * a recalibrate from beyond cylinder 77
2099 * will "fail" due to the FDC limitations;
2100 * since people used to complain much about
2101 * the failure message, try not logging
2102 * this one if it seems to be the first
2105 kprintf("fd%d: recal failed ST0 %b cyl %d\n",
2106 fdu, st0, NE7_ST0BITS, cyl);
2107 if(fdc->retry < 3) fdc->retry = 3;
2108 return (retrier(fdc));
2111 /* Seek (probably) necessary */
2112 fdc->state = DOSEEK;
2113 return (1); /* will return immediatly */
2115 if(fd->flags & FD_MOTOR_WAIT)
2117 return (0); /* time's not up yet */
2119 if (fdc->flags & FDC_NEEDS_RESET) {
2120 fdc->state = RESETCTLR;
2121 fdc->flags &= ~FDC_NEEDS_RESET;
2124 * If all motors were off, then the controller was
2125 * reset, so it has lost track of the current
2126 * cylinder. Recalibrate to handle this case.
2127 * But first, discard the results of the reset.
2129 fdc->state = RESETCOMPLETE;
2131 return (1); /* will return immediatly */
2133 device_printf(fdc->fdc_dev, "unexpected FD int->");
2134 if (fd_read_status(fdc, fd->fdsu) == 0)
2135 kprintf("FDC status :%x %x %x %x %x %x %x ",
2144 kprintf("No status available ");
2145 if (fd_sense_int(fdc, &st0, &cyl) != 0)
2147 kprintf("[controller is dead now]\n");
2150 kprintf("ST0 = %x, PCN = %x\n", st0, cyl);
2153 /*XXX confusing: some branches return immediately, others end up here*/
2154 return (1); /* Come back immediatly to new state */
2158 retrier(struct fdc_data *fdc)
2168 dev = bio->bio_driver_info;
2170 /* XXX shouldn't this be cached somewhere? */
2172 fd = devclass_get_softc(fd_devclass, fdu);
2173 if (fd->options & FDOPT_NORETRY)
2176 switch (fdc->retry) {
2177 case 0: case 1: case 2:
2178 fdc->state = SEEKCOMPLETE;
2180 case 3: case 4: case 5:
2181 fdc->state = STARTRECAL;
2184 fdc->state = RESETCTLR;
2191 int printerror = (fd->options & FDOPT_NOERRLOG) == 0;
2195 * note: use the correct device for more
2196 * verbose error reporting.
2199 "hard error", LOG_PRINTF,
2203 if (fdc->flags & FDC_STAT_VALID)
2205 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2206 fdc->status[0], NE7_ST0BITS,
2207 fdc->status[1], NE7_ST1BITS,
2208 fdc->status[2], NE7_ST2BITS,
2209 fdc->status[3], fdc->status[4],
2212 kprintf(" (No status)\n");
2215 bp->b_flags |= B_ERROR;
2217 bp->b_resid += bp->b_bcount - fdc->fd->skip;
2221 device_unbusy(fd->dev);
2223 devstat_end_transaction_buf(&fdc->fd->device_stats, bp);
2225 fdc->state = FINDWORK;
2226 fdc->flags |= FDC_NEEDS_RESET;
2236 fdformat(cdev_t dev, struct fd_formb *finfo, struct ucred *cred)
2245 fd = devclass_get_softc(fd_devclass, fdu);
2246 fdblk = 128 << fd->ft.secsize;
2248 /* set up a buffer header for fdstrategy() */
2250 bp->b_cmd = BUF_CMD_FORMAT;
2253 * calculate a fake blkno, so fdstrategy() would initiate a
2254 * seek to the requested cylinder
2256 bp->b_bio1.bio_offset = (off_t)(finfo->cyl *
2257 (fd->ft.sectrac * fd->ft.heads)
2258 + finfo->head * fd->ft.sectrac) * fdblk;
2259 bp->b_bio1.bio_driver_info = dev;
2260 bp->b_bio1.bio_flags |= BIO_SYNC;
2261 bp->b_bio1.bio_done = biodone_sync;
2263 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2264 bp->b_data = (caddr_t)finfo;
2266 /* now do the format */
2267 dev_dstrategy(dev, &bp->b_bio1);
2269 /* ...and wait for it to complete */
2270 rv = biowait_timeout(&bp->b_bio1, "fdform", 20 * hz);
2271 if (rv == EWOULDBLOCK) {
2275 device_unbusy(fd->dev);
2277 biodone(&bp->b_bio1);
2279 if (bp->b_flags & B_ERROR)
2282 * allow the process to be swapped
2289 * TODO: don't allocate buffer on stack.
2293 fdioctl(struct dev_ioctl_args *ap)
2295 cdev_t dev = ap->a_head.a_dev;
2296 fdu_t fdu = dkunit(dev);
2297 fd_p fd = devclass_get_softc(fd_devclass, fdu);
2298 struct fdc_status *fsp;
2301 switch (ap->a_cmd) {
2303 if ((ap->a_fflag & FWRITE) == 0)
2304 error = EBADF; /* must be opened for writing */
2305 else if (((struct fd_formb *)ap->a_data)->format_version !=
2307 error = EINVAL; /* wrong version of formatting prog */
2309 error = fdformat(dev, (struct fd_formb *)ap->a_data, ap->a_cred);
2312 case FD_GTYPE: /* get drive type */
2313 *(struct fd_type *)ap->a_data = fd->ft;
2316 case FD_STYPE: /* set drive type */
2317 /* this is considered harmful; only allow for superuser */
2318 if (priv_check_cred(ap->a_cred, PRIV_ROOT, 0) != 0)
2320 fd->ft = *(struct fd_type *)ap->a_data;
2323 case FD_GOPTS: /* get drive options */
2324 *(int *)ap->a_data = fd->options;
2327 case FD_SOPTS: /* set drive options */
2328 fd->options = *(int *)ap->a_data;
2332 fsp = (struct fdc_status *)ap->a_data;
2333 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2335 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
2346 * Hello emacs, these are the
2349 * c-continued-statement-offset: 8
2350 * c-continued-brace-offset: 0
2351 * c-brace-offset: -8
2352 * c-brace-imaginary-offset: 0
2353 * c-argdecl-indent: 8
2354 * c-label-offset: -8
2355 * c++-hanging-braces: 1
2356 * c++-access-specifier-offset: -8
2357 * c++-empty-arglist-indent: 8
2358 * c++-friend-offset: 0