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.43 2008/08/13 08:18:09 swildner 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/diskslice.h>
70 #include <sys/devicestat.h>
71 #include <sys/fcntl.h>
72 #include <sys/malloc.h>
73 #include <sys/module.h>
76 #include <sys/syslog.h>
77 #include <sys/device.h>
81 #include <sys/thread2.h>
83 #include <machine/clock.h>
84 #include <machine/ioctl_fd.h>
85 #include <machine/stdarg.h>
87 #include <bus/isa/isavar.h>
88 #include <bus/isa/isareg.h>
91 #include <bus/isa/rtc.h>
93 /* configuration flags */
94 #define FDC_PRETEND_D0 (1 << 0) /* pretend drive 0 to be there */
95 #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */
97 /* internally used only, not really from CMOS: */
98 #define RTCFDT_144M_PRETENDED 0x1000
100 /* error returns for fd_cmd() */
102 #define FD_NOT_VALID -2
103 #define FDC_ERRMAX 100 /* do not log more */
105 * Stop retrying after this many DMA overruns. Since each retry takes
106 * one revolution, with 300 rpm., 25 retries take approximately 10
107 * seconds which the read attempt will block in case the DMA overrun
110 #define FDC_DMAOV_MAX 25
113 * Timeout value for the PIO loops to wait until the FDC main status
114 * register matches our expectations (request for master, direction
115 * bit). This is supposed to be a number of microseconds, although
116 * timing might actually not be very accurate.
118 * Timeouts of 100 msec are believed to be required for some broken
121 #define FDSTS_TIMEOUT 100000
124 #define NUMDENS (NUMTYPES - 7)
126 /* These defines (-1) must match index for fd_types */
127 #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */
128 #define NO_TYPE 0 /* must match NO_TYPE in ft.c */
140 #define FD_1480in5_25 11
141 #define FD_1440in5_25 12
142 #define FD_820in5_25 13
143 #define FD_800in5_25 14
144 #define FD_720in5_25 15
145 #define FD_360in5_25 16
146 #define FD_640in5_25 17
149 static struct fd_type fd_types[NUMTYPES] =
151 { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
152 { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
153 { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
154 { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */
155 { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */
156 { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */
157 { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */
158 { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */
159 { 8,2,0xFF,0x2A,80,1280,1,FDC_250KBPS,2,0x50,1 }, /* 640K in DD 5.25in */
160 { 8,3,0xFF,0x35,77,1232,1,FDC_500KBPS,2,0x74,1 }, /* 1.23M in HD 5.25in */
162 { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
163 { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
164 { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */
165 { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */
166 { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */
167 { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */
168 { 8,2,0xFF,0x2A,80,1280,1,FDC_300KBPS,2,0x50,1 }, /* 640K in HD 5.25in */
171 #define DRVS_PER_CTLR 2 /* 2 floppies */
173 /***********************************************************************\
174 * Per controller structure. *
175 \***********************************************************************/
176 devclass_t fdc_devclass;
178 /***********************************************************************\
179 * Per drive structure. *
180 * N per controller (DRVS_PER_CTLR) *
181 \***********************************************************************/
183 struct fdc_data *fdc; /* pointer to controller structure */
184 int fdsu; /* this units number on this controller */
185 int type; /* Drive type (FD_1440...) */
186 struct fd_type ft; /* the type descriptor */
188 #define FD_OPEN 0x01 /* it's open */
189 #define FD_ACTIVE 0x02 /* it's active */
190 #define FD_MOTOR 0x04 /* motor should be on */
191 #define FD_MOTOR_WAIT 0x08 /* motor coming up */
194 #define FD_NO_TRACK -2
195 int track; /* where we think the head is */
196 int options; /* user configurable options, see ioctl_fd.h */
197 struct callout toffhandle;
198 struct callout tohandle;
199 struct callout motor;
201 struct devstat device_stats;
209 static devclass_t fd_devclass;
211 /***********************************************************************\
212 * Throughout this file the following conventions will be used: *
213 * fd is a pointer to the fd_data struct for the drive in question *
214 * fdc is a pointer to the fdc_data struct for the controller *
215 * fdu is the floppy drive unit number *
216 * fdcu is the floppy controller unit number *
217 * fdsu is the floppy drive unit number on that controller. (sub-unit) *
218 \***********************************************************************/
220 /* internal functions */
221 static void fdc_intr(void *);
222 static void set_motor(struct fdc_data *, int, int);
225 static timeout_t fd_turnoff;
226 static timeout_t fd_motor_on;
227 static void fd_turnon(struct fd_data *);
228 static void fdc_reset(fdc_p);
229 static int fd_in(struct fdc_data *, int *);
230 static int out_fdc(struct fdc_data *, int);
231 static void fdstart(struct fdc_data *);
232 static timeout_t fd_iotimeout;
233 static timeout_t fd_pseudointr;
234 static int fdstate(struct fdc_data *);
235 static int retrier(struct fdc_data *);
236 static int fdformat(cdev_t, struct fd_formb *, struct ucred *);
238 static int enable_fifo(fdc_p fdc);
240 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */
246 #define SEEKCOMPLETE 3
248 #define RECALCOMPLETE 5
254 #define IOTIMEDOUT 11
255 #define RESETCOMPLETE 12
259 static char const * const fdstates[] =
277 /* CAUTION: fd_debug causes huge amounts of logging output */
278 static int volatile fd_debug = 0;
279 #define TRACE0(arg) if(fd_debug) kprintf(arg)
280 #define TRACE1(arg1, arg2) if(fd_debug) kprintf(arg1, arg2)
281 #else /* FDC_DEBUG */
283 #define TRACE1(arg1, arg2)
284 #endif /* FDC_DEBUG */
287 fdout_wr(fdc_p fdc, u_int8_t v)
289 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
295 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
299 fddata_wr(fdc_p fdc, u_int8_t v)
301 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
307 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
311 fdctl_wr_isa(fdc_p fdc, u_int8_t v)
313 bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
321 return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
326 static d_open_t Fdopen; /* NOTE, not fdopen */
327 static d_close_t fdclose;
328 static d_ioctl_t fdioctl;
329 static d_strategy_t fdstrategy;
331 static struct dev_ops fd_ops = {
332 { "fd", FD_CDEV_MAJOR, D_DISK },
336 .d_write = physwrite,
338 .d_strategy = fdstrategy,
342 fdc_err(struct fdc_data *fdc, const char *s)
346 if (fdc->fdc_errs < FDC_ERRMAX)
347 device_printf(fdc->fdc_dev, "%s", s);
348 else if (fdc->fdc_errs == FDC_ERRMAX)
349 device_printf(fdc->fdc_dev, "too many errors, not "
350 "logging any more\n");
357 * fd_cmd: Send a command to the chip. Takes a varargs with this structure:
359 * # of output bytes, output bytes as ints ...,
360 * # of input bytes, input bytes as ints ...
363 fd_cmd(struct fdc_data *fdc, int n_out, ...)
370 __va_start(ap, n_out);
371 cmd = (u_char)(__va_arg(ap, int));
373 __va_start(ap, n_out);
374 for (n = 0; n < n_out; n++)
376 if (out_fdc(fdc, __va_arg(ap, int)) < 0)
379 ksnprintf(msg, sizeof(msg),
380 "cmd %x failed at out byte %d of %d\n",
382 return fdc_err(fdc, msg);
385 n_in = __va_arg(ap, int);
386 for (n = 0; n < n_in; n++)
388 int *ptr = __va_arg(ap, int *);
389 if (fd_in(fdc, ptr) < 0)
392 ksnprintf(msg, sizeof(msg),
393 "cmd %02x failed at in byte %d of %d\n",
395 return fdc_err(fdc, msg);
403 enable_fifo(fdc_p fdc)
407 if ((fdc->flags & FDC_HAS_FIFO) == 0) {
411 * Cannot use fd_cmd the normal way here, since
412 * this might be an invalid command. Thus we send the
413 * first byte, and check for an early turn of data directon.
416 if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
417 return fdc_err(fdc, "Enable FIFO failed\n");
419 /* If command is invalid, return */
421 while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
422 != NE7_RQM && j-- > 0) {
423 if (i == (NE7_DIO | NE7_RQM)) {
431 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
433 return fdc_err(fdc, "Enable FIFO failed\n");
435 fdc->flags |= FDC_HAS_FIFO;
439 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
440 return fdc_err(fdc, "Re-enable FIFO failed\n");
445 fd_sense_drive_status(fdc_p fdc, int *st3p)
449 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
451 return fdc_err(fdc, "Sense Drive Status failed\n");
460 fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
464 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
467 "sense intr err reading stat reg 0\n");
474 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
476 * There doesn't seem to have been an interrupt.
481 if (fd_in(fdc, &cyl) < 0) {
482 return fdc_err(fdc, "can't get cyl num\n");
493 fd_read_status(fdc_p fdc, int fdsu)
497 for (i = 0; i < 7; i++) {
499 * XXX types are poorly chosen. Only bytes can by read
500 * from the hardware, but fdc->status[] wants u_ints and
501 * fd_in() gives ints.
505 ret = fd_in(fdc, &status);
506 fdc->status[i] = status;
512 fdc->flags |= FDC_STAT_VALID;
514 fdc->flags &= ~FDC_STAT_VALID;
519 /****************************************************************************/
520 /* autoconfiguration stuff */
521 /****************************************************************************/
524 fdc_alloc_resources(struct fdc_data *fdc)
530 ispnp = (fdc->flags & FDC_ISPNP) != 0;
531 ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
532 fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
533 fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
536 * On standard ISA, we don't just use an 8 port range
537 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
540 * Isn't PC hardware wonderful.
542 * The Y-E Data PCMCIA FDC doesn't have this problem, it
543 * uses the register with offset 6 for pseudo-DMA, and the
544 * one with offset 7 as control register.
546 fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
547 &fdc->rid_ioport, 0ul, ~0ul,
548 ispcmcia ? 8 : (ispnp ? 1 : 6),
550 if (fdc->res_ioport == 0) {
551 device_printf(dev, "cannot reserve I/O port range\n");
554 fdc->portt = rman_get_bustag(fdc->res_ioport);
555 fdc->porth = rman_get_bushandle(fdc->res_ioport);
559 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
560 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
561 * by checking the size and adjust the port address
564 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
568 * Register the control port range as rid 1 if it
569 * isn't there already. Most PnP BIOSen will have
570 * already done this but non-PnP configurations don't.
572 * And some (!!) report 0x3f2-0x3f5 and completely
573 * leave out the control register! It seems that some
574 * non-antique controller chips have a different
575 * method of programming the transfer speed which
576 * doesn't require the control register, but it's
577 * mighty bogus as the chip still responds to the
578 * address for the control register.
580 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
583 /* Find the control port, usually 0x3f7 */
584 ctlstart = rman_get_start(fdc->res_ioport) +
587 bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
591 * Now (finally!) allocate the control port.
594 fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
596 0ul, ~0ul, 1, RF_ACTIVE);
597 if (fdc->res_ctl == 0) {
599 "cannot reserve control I/O port range\n");
602 fdc->ctlt = rman_get_bustag(fdc->res_ctl);
603 fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
606 fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
607 &fdc->rid_irq, 0ul, ~0ul, 1,
609 if (fdc->res_irq == 0) {
610 device_printf(dev, "cannot reserve interrupt line\n");
614 if ((fdc->flags & FDC_NODMA) == 0) {
615 fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
616 &fdc->rid_drq, 0ul, ~0ul, 1,
618 if (fdc->res_drq == 0) {
619 device_printf(dev, "cannot reserve DMA request line\n");
622 fdc->dmachan = fdc->res_drq->r_start;
629 fdc_release_resources(struct fdc_data *fdc)
634 if (fdc->res_irq != 0) {
635 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
637 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
640 if (fdc->res_ctl != 0) {
641 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
643 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
646 if (fdc->res_ioport != 0) {
647 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
649 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
652 if (fdc->res_drq != 0) {
653 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
655 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
660 /****************************************************************************/
661 /* autoconfiguration stuff */
662 /****************************************************************************/
664 static struct isa_pnp_id fdc_ids[] = {
665 {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
666 {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
671 fdc_read_ivar(device_t dev, device_t child, int which, u_long *result)
673 struct fdc_ivars *ivars = device_get_ivars(child);
676 case FDC_IVAR_FDUNIT:
677 *result = ivars->fdunit;
686 * fdc controller section.
689 fdc_probe(device_t dev)
692 struct fdc_data *fdc;
694 fdc = device_get_softc(dev);
695 bzero(fdc, sizeof *fdc);
697 fdc->fdctl_wr = fdctl_wr_isa;
700 error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
704 fdc->flags |= FDC_ISPNP;
706 /* Attempt to allocate our resources for the duration of the probe */
707 error = fdc_alloc_resources(fdc);
711 /* First - lets reset the floppy controller */
714 fdout_wr(fdc, FDO_FRST);
716 /* see if it can handle a command */
717 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
718 NE7_SPEC_2(2, 0), 0)) {
723 if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
724 ic_type = (u_char)ic_type;
727 device_set_desc(dev, "NEC 765 or clone");
728 fdc->fdct = FDC_NE765;
731 device_set_desc(dev, "Intel 82077 or clone");
732 fdc->fdct = FDC_I82077;
735 device_set_desc(dev, "NEC 72065B or clone");
736 fdc->fdct = FDC_NE72065;
739 device_set_desc(dev, "generic floppy controller");
740 fdc->fdct = FDC_UNKNOWN;
746 fdc_release_resources(fdc);
751 * Add a child device to the fdc controller. It will then be probed etc.
754 fdc_add_child(device_t dev, const char *name, int unit)
757 struct fdc_ivars *ivar;
760 ivar = kmalloc(sizeof *ivar, M_DEVBUF /* XXX */, M_WAITOK | M_ZERO);
761 if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
763 child = device_add_child(dev, name, unit);
766 device_set_ivars(child, ivar);
767 if (resource_int_value(name, unit, "disabled", &disabled) == 0
769 device_disable(child);
773 fdc_attach(device_t dev)
775 struct fdc_data *fdc;
778 fdc = device_get_softc(dev);
780 callout_init(&fdc->pseudointr_ch);
782 error = fdc_alloc_resources(fdc);
784 device_printf(dev, "cannot reacquire resources\n");
787 error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
789 &fdc->fdc_intr, NULL);
791 device_printf(dev, "cannot setup interrupt\n");
794 fdc->fdcu = device_get_unit(dev);
795 fdc->flags |= FDC_ATTACHED;
797 if ((fdc->flags & FDC_NODMA) == 0) {
798 /* Acquire the DMA channel forever, The driver will do the rest */
799 /* XXX should integrate with rman */
800 isa_dma_acquire(fdc->dmachan);
801 isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
803 fdc->state = DEVIDLE;
805 /* reset controller, turn motor off, clear fdout mirror reg */
806 fdout_wr(fdc, ((fdc->fdout = 0)));
807 bioq_init(&fdc->bio_queue);
810 * Probe and attach any children. We should probably detect
811 * devices from the BIOS unless overridden.
813 for (i = resource_query_string(-1, "at", device_get_nameunit(dev));
815 i = resource_query_string(i, "at", device_get_nameunit(dev)))
816 fdc_add_child(dev, resource_query_name(i),
817 resource_query_unit(i));
819 return (bus_generic_attach(dev));
823 fdc_print_child(device_t me, device_t child)
827 retval += bus_print_child_header(me, child);
828 retval += kprintf(" on %s drive %d\n", device_get_nameunit(me),
829 fdc_get_fdunit(child));
834 static device_method_t fdc_methods[] = {
835 /* Device interface */
836 DEVMETHOD(device_probe, fdc_probe),
837 DEVMETHOD(device_attach, fdc_attach),
838 DEVMETHOD(device_detach, bus_generic_detach),
839 DEVMETHOD(device_shutdown, bus_generic_shutdown),
840 DEVMETHOD(device_suspend, bus_generic_suspend),
841 DEVMETHOD(device_resume, bus_generic_resume),
844 DEVMETHOD(bus_print_child, fdc_print_child),
845 DEVMETHOD(bus_read_ivar, fdc_read_ivar),
846 /* Our children never use any other bus interface methods. */
851 static driver_t fdc_driver = {
854 sizeof(struct fdc_data)
857 DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
858 DRIVER_MODULE(fdc, acpi, fdc_driver, fdc_devclass, 0, 0);
860 /******************************************************************/
862 * devices attached to the controller section.
865 fd_probe(device_t dev)
870 struct fdc_data *fdc;
872 static int fd_fifo = 0;
874 fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
875 fd = device_get_softc(dev);
876 fdc = device_get_softc(device_get_parent(dev));
878 bzero(fd, sizeof *fd);
882 fd->fdu = device_get_unit(dev);
885 /* look up what bios thinks we have */
888 if ((fdc->flags & FDC_ISPCMCIA))
890 else if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0)
891 fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
893 fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
896 fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
903 fdt = RTCFDT_144M; /* XXX probably */
906 /* is there a unit? */
907 if (fdt == RTCFDT_NONE)
911 set_motor(fdc, fdsu, TURNON);
912 DELAY(1000000); /* 1 sec */
914 /* XXX This doesn't work before the first set_motor() */
915 if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN
916 && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0
917 && enable_fifo(fdc) == 0) {
918 device_printf(device_get_parent(dev),
919 "FIFO enabled, %d bytes threshold\n", fifo_threshold);
923 if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0)
924 && (st3 & NE7_ST3_T0)) {
925 /* if at track 0, first seek inwards */
926 /* seek some steps: */
927 fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0);
928 DELAY(300000); /* ...wait a moment... */
929 fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
932 /* If we're at track 0 first seek inwards. */
933 if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
934 /* Seek some steps... */
935 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
936 /* ...wait a moment... */
938 /* make ctrlr happy: */
939 fd_sense_int(fdc, 0, 0);
943 for (i = 0; i < 2; i++) {
945 * we must recalibrate twice, just in case the
946 * heads have been beyond cylinder 76, since most
947 * FDCs still barf when attempting to recalibrate
951 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
952 /* a second being enough for full stroke seek*/
953 DELAY(i == 0 ? 1000000 : 300000);
955 /* anything responding? */
956 if (fd_sense_int(fdc, &st0, 0) == 0 &&
957 (st0 & NE7_ST0_EC) == 0)
958 break; /* already probed succesfully */
962 set_motor(fdc, fdsu, TURNOFF);
964 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
967 fd->track = FD_NO_TRACK;
971 callout_init(&fd->toffhandle);
972 callout_init(&fd->tohandle);
973 callout_init(&fd->motor);
977 device_set_desc(dev, "1200-KB 5.25\" drive");
980 case RTCFDT_144M | RTCFDT_144M_PRETENDED:
981 device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive");
985 device_set_desc(dev, "1440-KB 3.5\" drive");
990 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
994 device_set_desc(dev, "360-KB 5.25\" drive");
998 kprintf("720-KB 3.5\" drive");
1004 fd->ft = fd_types[fd->type - 1];
1009 fd_attach(device_t dev)
1013 fd = device_get_softc(dev);
1015 disk_create(fd->fdu, &fd->disk, &fd_ops);
1018 * Make special raw floppy devices with preset types to
1019 * make formatting easier. These override the disk management
1020 * layer for the whole-slice-disk for partitions 128-191. Note
1021 * that we do not override partition 255, which is the
1022 * whole-slice-part. If we did we would have to provide our
1023 * own DIOCGPART ioctl.
1025 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 1),
1026 UID_ROOT, GID_WHEEL, 0600, "fd%d.1720", fd->fdu);
1027 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 2),
1028 UID_ROOT, GID_WHEEL, 0600, "fd%d.1480", fd->fdu);
1029 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 3),
1030 UID_ROOT, GID_WHEEL, 0600, "fd%d.1440", fd->fdu);
1031 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 4),
1032 UID_ROOT, GID_WHEEL, 0600, "fd%d.1200", fd->fdu);
1033 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 5),
1034 UID_ROOT, GID_WHEEL, 0600, "fd%d.820", fd->fdu);
1035 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 6),
1036 UID_ROOT, GID_WHEEL, 0600, "fd%d.800", fd->fdu);
1037 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 7),
1038 UID_ROOT, GID_WHEEL, 0600, "fd%d.720", fd->fdu);
1039 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 8),
1040 UID_ROOT, GID_WHEEL, 0600, "fd%d.360", fd->fdu);
1041 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 9),
1042 UID_ROOT, GID_WHEEL, 0600, "fd%d.640", fd->fdu);
1043 make_dev(&fd_ops, dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128 + 10),
1044 UID_ROOT, GID_WHEEL, 0600, "fd%d.1232", fd->fdu);
1046 devstat_add_entry(&fd->device_stats, device_get_name(dev),
1047 device_get_unit(dev), 512, DEVSTAT_NO_ORDERED_TAGS,
1048 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
1049 DEVSTAT_PRIORITY_FD);
1054 fd_detach(device_t dev)
1058 fd = device_get_softc(dev);
1059 kprintf("devfs: Please make sure that only the right fd device was removed!!!\n");
1060 dev_ops_remove_minor(&fd_ops,
1061 /*dkunitmask() | dkmakeslice(-1) | dkmakepart(128|64),*/
1062 dkmakeminor(fd->fdu, WHOLE_DISK_SLICE, 128));
1063 disk_invalidate(&fd->disk);
1064 disk_destroy(&fd->disk);
1065 devstat_remove_entry(&fd->device_stats);
1066 callout_stop(&fd->toffhandle);
1067 callout_stop(&fd->motor);
1072 static device_method_t fd_methods[] = {
1073 /* Device interface */
1074 DEVMETHOD(device_probe, fd_probe),
1075 DEVMETHOD(device_attach, fd_attach),
1076 DEVMETHOD(device_detach, fd_detach),
1077 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1078 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
1079 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
1084 static driver_t fd_driver = {
1087 sizeof(struct fd_data)
1090 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
1092 /****************************************************************************/
1093 /* motor control stuff */
1094 /* remember to not deselect the drive we're working on */
1095 /****************************************************************************/
1097 set_motor(struct fdc_data *fdc, int fdsu, int turnon)
1099 int fdout = fdc->fdout;
1100 int needspecify = 0;
1103 fdout &= ~FDO_FDSEL;
1104 fdout |= (FDO_MOEN0 << fdsu) + fdsu;
1106 fdout &= ~(FDO_MOEN0 << fdsu);
1109 && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
1110 /* gonna turn off the last drive, put FDC to bed */
1111 fdout &= ~ (FDO_FRST|FDO_FDMAEN);
1113 /* make sure controller is selected and specified */
1114 if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
1116 fdout |= (FDO_FRST|FDO_FDMAEN);
1119 fdout_wr(fdc, fdout);
1121 TRACE1("[0x%x->FDOUT]", fdout);
1126 * special case: since we have just woken up the FDC
1127 * from its sleep, we silently assume the command will
1128 * be accepted, and do not test for a timeout
1130 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1131 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1133 if (fdc->flags & FDC_HAS_FIFO)
1134 (void) enable_fifo(fdc);
1139 fd_turnoff(void *xfd)
1143 TRACE1("[fd%d: turnoff]", fd->fdu);
1147 * Don't turn off the motor yet if the drive is active.
1149 * If we got here, this could only mean we missed an interrupt.
1150 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
1151 * after a controller reset. Just schedule a pseudo-interrupt
1152 * so the state machine gets re-entered.
1154 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
1160 fd->flags &= ~FD_MOTOR;
1161 set_motor(fd->fdc, fd->fdsu, TURNOFF);
1166 fd_motor_on(void *xfd)
1171 fd->flags &= ~FD_MOTOR_WAIT;
1172 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
1182 if(!(fd->flags & FD_MOTOR))
1184 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
1185 set_motor(fd->fdc, fd->fdsu, TURNON);
1186 callout_reset(&fd->motor, hz, fd_motor_on, fd);
1191 fdc_reset(fdc_p fdc)
1193 /* Try a reset, keep motor on */
1194 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1195 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1197 /* enable FDC, but defer interrupts a moment */
1198 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
1199 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
1201 fdout_wr(fdc, fdc->fdout);
1202 TRACE1("[0x%x->FDOUT]", fdc->fdout);
1204 /* XXX after a reset, silently believe the FDC will accept commands */
1205 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1206 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1208 if (fdc->flags & FDC_HAS_FIFO)
1209 (void) enable_fifo(fdc);
1212 /****************************************************************************/
1214 /****************************************************************************/
1216 * FDC IO functions, take care of the main status register, timeout
1217 * in case the desired status bits are never set.
1219 * These PIO loops initially start out with short delays between
1220 * each iteration in the expectation that the required condition
1221 * is usually met quickly, so it can be handled immediately. After
1222 * about 1 ms, stepping is increased to achieve a better timing
1223 * accuracy in the calls to DELAY().
1226 fd_in(struct fdc_data *fdc, int *ptr)
1230 for (j = 0, step = 1;
1231 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) &&
1235 return (fdc_err(fdc, "ready for output in input\n"));
1240 if (j >= FDSTS_TIMEOUT)
1241 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
1244 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1247 #else /* !FDC_DEBUG */
1252 #endif /* FDC_DEBUG */
1256 out_fdc(struct fdc_data *fdc, int x)
1260 for (j = 0, step = 1;
1261 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != NE7_RQM &&
1264 if (i == (NE7_DIO|NE7_RQM))
1265 return (fdc_err(fdc, "ready for input in output\n"));
1270 if (j >= FDSTS_TIMEOUT)
1271 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
1273 /* Send the command and return */
1275 TRACE1("[0x%x->FDDATA]", x);
1279 /****************************************************************************/
1280 /* fdopen/fdclose */
1281 /****************************************************************************/
1283 Fdopen(struct dev_open_args *ap)
1285 cdev_t dev = ap->a_head.a_dev;
1286 fdu_t fdu = dkunit(dev);
1287 struct disk_info info;
1295 if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1298 if ((fdc == NULL) || (fd->type == NO_TYPE))
1302 * Figure out the type of floppy. There are special whole-disk-device
1303 * overrides that will override the current type.
1306 if (type == WHOLE_SLICE_PART) {
1307 type = fd->type; /* do not change selected type data */
1309 } else if (type > 128) {
1310 type -= 128; /* set to specific format */
1313 type = fd->type; /* reset to default */
1318 if (type != fd->type) {
1320 * For each type of basic drive, make sure we are trying
1321 * to open a type it can do,
1336 type = FD_1480in5_25;
1339 type = FD_1440in5_25;
1344 type = FD_820in5_25;
1347 type = FD_800in5_25;
1350 type = FD_720in5_25;
1353 type = FD_640in5_25;
1356 type = FD_360in5_25;
1363 if ( type != FD_1720
1377 * fd->type is the basic drive type, not the current format
1378 * we are reading. We only change the type when opening the
1379 * whole-slice-partition
1382 fd->ft = fd_types[type - 1];
1383 fd->flags |= FD_OPEN;
1386 * Clearing the DMA overrun counter at open time is a bit messy.
1387 * Since we're only managing one counter per controller, opening
1388 * the second drive could mess it up. Anyway, if the DMA overrun
1389 * condition is really persistent, it will eventually time out
1390 * still. OTOH, clearing it here will ensure we'll at least start
1391 * trying again after a previous (maybe even long ago) failure.
1392 * Also, this is merely a stop-gap measure only that should not
1393 * happen during normal operation, so we can tolerate it to be a
1394 * bit sloppy about this.
1396 fdc->dma_overruns = 0;
1399 * Set disk parameters for the disk management layer.
1401 * Note that we do not set RAWEXTENSIONS here. We override
1402 * the minor numbers in the raw-extension range and handle them
1405 bzero(&info, sizeof(info));
1407 info.d_media_blksize = 128 << ft->secsize;
1408 info.d_media_blocks = ft->size;
1409 info.d_dsflags = DSO_COMPATPARTA | DSO_COMPATMBR;
1410 info.d_nheads = ft->heads;
1411 info.d_secpertrack = ft->sectrac;
1412 info.d_secpercyl = ft->sectrac * ft->heads;
1413 info.d_ncylinders = ft->size / info.d_secpercyl;
1414 disk_setdiskinfo(&fd->disk, &info);
1420 fdclose(struct dev_close_args *ap)
1422 cdev_t dev = ap->a_head.a_dev;
1423 fdu_t fdu = dkunit(dev);
1426 fd = devclass_get_softc(fd_devclass, fdu);
1427 fd->flags &= ~FD_OPEN;
1428 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG);
1433 /****************************************************************************/
1435 /****************************************************************************/
1437 fdstrategy(struct dev_strategy_args *ap)
1439 cdev_t dev = ap->a_head.a_dev;
1440 struct bio *bio = ap->a_bio;
1441 struct buf *bp = bio->bio_buf;
1442 unsigned nblocks, blknum, cando;
1449 fd = devclass_get_softc(fd_devclass, fdu);
1451 panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
1452 (u_long)major(dev), (u_long)minor(dev));
1454 if (fd->type == NO_TYPE) {
1455 bp->b_error = ENXIO;
1456 bp->b_flags |= B_ERROR;
1460 fdblk = 128 << (fd->ft.secsize);
1461 if (bp->b_cmd != BUF_CMD_FORMAT) {
1462 if (bio->bio_offset < 0) {
1464 "fd%d: fdstrat: bad request offset = %lld, bcount = %d\n",
1465 fdu, bio->bio_offset, bp->b_bcount);
1466 bp->b_error = EINVAL;
1467 bp->b_flags |= B_ERROR;
1470 if ((bp->b_bcount % fdblk) != 0) {
1471 bp->b_error = EINVAL;
1472 bp->b_flags |= B_ERROR;
1478 * Set up block calculations.
1480 if (bio->bio_offset > 20000000LL * fdblk) {
1482 * Reject unreasonably high block number, prevent the
1483 * multiplication below from overflowing.
1485 bp->b_error = EINVAL;
1486 bp->b_flags |= B_ERROR;
1489 blknum = (unsigned)(bio->bio_offset / fdblk);
1490 nblocks = fd->ft.size;
1492 if (blknum + (bp->b_bcount / fdblk) > nblocks) {
1493 if (blknum <= nblocks) {
1494 cando = (nblocks - blknum) * fdblk;
1495 bp->b_resid = bp->b_bcount - cando;
1497 goto bad; /* not actually bad but EOF */
1499 bp->b_error = EINVAL;
1500 bp->b_flags |= B_ERROR;
1505 bio->bio_driver_info = dev;
1506 bioqdisksort(&fdc->bio_queue, bio);
1507 callout_stop(&fd->toffhandle);
1509 /* Tell devstat we are starting on the transaction */
1510 devstat_start_transaction(&fd->device_stats);
1511 device_busy(fd->dev);
1522 /***************************************************************\
1524 * We have just queued something.. if the controller is not busy *
1525 * then simulate the case where it has just finished a command *
1526 * So that it (the interrupt routine) looks on the queue for more*
1527 * work to do and picks up what we just added. *
1528 * If the controller is already busy, we need do nothing, as it *
1529 * will pick up our work when the present work completes *
1530 \***************************************************************/
1532 fdstart(struct fdc_data *fdc)
1535 if(fdc->state == DEVIDLE)
1543 fd_iotimeout(void *xfdc)
1548 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
1551 * Due to IBM's brain-dead design, the FDC has a faked ready
1552 * signal, hardwired to ready == true. Thus, any command
1553 * issued if there's no diskette in the drive will _never_
1554 * complete, and must be aborted by resetting the FDC.
1555 * Many thanks, Big Blue!
1556 * The FDC must not be reset directly, since that would
1557 * interfere with the state machine. Instead, pretend that
1558 * the command completed but was invalid. The state machine
1559 * will reset the FDC and retry once.
1562 fdc->status[0] = NE7_ST0_IC_IV;
1563 fdc->flags &= ~FDC_STAT_VALID;
1564 fdc->state = IOTIMEDOUT;
1569 /* just ensure it is running in a critical section */
1571 fd_pseudointr(void *xfdc)
1578 /***********************************************************************\
1580 * keep calling the state machine until it returns a 0 *
1581 * ALWAYS called at SPLBIO *
1582 \***********************************************************************/
1584 fdc_intr(void *xfdc)
1592 * magic pseudo-DMA initialization for YE FDC. Sets count and
1595 #define SET_BCDR(fdc,wr,cnt,port) \
1596 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \
1597 ((cnt)-1) & 0xff); \
1598 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
1599 ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
1602 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy
1604 static int fdcpio(fdc_p fdc, buf_cmd_t cmd, caddr_t addr, u_int count)
1606 u_char *cptr = (u_char *)addr;
1608 if (cmd == BUF_CMD_READ) {
1609 if (fdc->state != PIOREAD) {
1610 fdc->state = PIOREAD;
1613 SET_BCDR(fdc, 0, count, 0);
1614 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1615 FDC_YE_DATAPORT, cptr, count);
1617 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1618 FDC_YE_DATAPORT, cptr, count);
1619 SET_BCDR(fdc, 0, count, 0);
1624 /***********************************************************************\
1625 * The controller state machine. *
1626 * if it returns a non zero value, it should be called again immediatly *
1627 \***********************************************************************/
1631 int read, format, head, i, sec = 0, sectrac, st0, cyl, st3;
1632 unsigned blknum = 0, b_cylinder = 0;
1637 struct fd_formb *finfo = NULL;
1643 bio = bioq_first(&fdc->bio_queue);
1645 bioq_remove(&fdc->bio_queue, bio);
1650 /***********************************************\
1651 * nothing left for this controller to do *
1652 * Force into the IDLE state, *
1653 \***********************************************/
1654 fdc->state = DEVIDLE;
1656 device_printf(fdc->fdc_dev,
1657 "unexpected valid fd pointer\n");
1661 TRACE1("[fdc%d IDLE]", fdc->fdcu);
1665 dev = bio->bio_driver_info;
1668 fd = devclass_get_softc(fd_devclass, fdu);
1669 fdblk = 128 << fd->ft.secsize;
1670 if (fdc->fd && (fd != fdc->fd))
1671 device_printf(fd->dev, "confused fd pointers\n");
1672 read = (bp->b_cmd == BUF_CMD_READ);
1673 format = (bp->b_cmd == BUF_CMD_FORMAT);
1675 finfo = (struct fd_formb *)bp->b_data;
1676 fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1679 if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
1680 blknum = (unsigned)(bio->bio_offset / fdblk) +
1682 b_cylinder = blknum / (fd->ft.sectrac * fd->ft.heads);
1684 TRACE1("fd%d", fdu);
1685 TRACE1("[%s]", fdstates[fdc->state]);
1686 TRACE1("(0x%x)", fd->flags);
1687 callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
1691 case FINDWORK: /* we have found new work */
1696 fdc->fdctl_wr(fdc, fd->ft.trans);
1697 TRACE1("[0x%x->FDCTL]", fd->ft.trans);
1698 /*******************************************************\
1699 * If the next drive has a motor startup pending, then *
1700 * it will start up in its own good time *
1701 \*******************************************************/
1702 if(fd->flags & FD_MOTOR_WAIT) {
1703 fdc->state = MOTORWAIT;
1704 return (0); /* come back later */
1706 /*******************************************************\
1707 * Maybe if it's not starting, it SHOULD be starting *
1708 \*******************************************************/
1709 if (!(fd->flags & FD_MOTOR))
1711 fdc->state = MOTORWAIT;
1715 else /* at least make sure we are selected */
1717 set_motor(fdc, fd->fdsu, TURNON);
1719 if (fdc->flags & FDC_NEEDS_RESET) {
1720 fdc->state = RESETCTLR;
1721 fdc->flags &= ~FDC_NEEDS_RESET;
1723 fdc->state = DOSEEK;
1726 if (b_cylinder == (unsigned)fd->track)
1728 fdc->state = SEEKCOMPLETE;
1731 if (fd_cmd(fdc, 3, NE7CMD_SEEK,
1732 fd->fdsu, b_cylinder * fd->ft.steptrac,
1736 * seek command not accepted, looks like
1737 * the FDC went off to the Saints...
1739 fdc->retry = 6; /* try a reset */
1740 return(retrier(fdc));
1742 fd->track = FD_NO_TRACK;
1743 fdc->state = SEEKWAIT;
1744 return(0); /* will return later */
1746 /* allow heads to settle */
1747 callout_reset(&fdc->pseudointr_ch, hz / 16,
1748 fd_pseudointr, fdc);
1749 fdc->state = SEEKCOMPLETE;
1750 return(0); /* will return later */
1751 case SEEKCOMPLETE : /* SEEK DONE, START DMA */
1752 /* Make sure seek really happened*/
1753 if(fd->track == FD_NO_TRACK) {
1754 int descyl = b_cylinder * fd->ft.steptrac;
1757 * This might be a "ready changed" interrupt,
1758 * which cannot really happen since the
1759 * RDY pin is hardwired to + 5 volts. This
1760 * generally indicates a "bouncing" intr
1761 * line, so do one of the following:
1763 * When running on an enhanced FDC that is
1764 * known to not go stuck after responding
1765 * with INVALID, fetch all interrupt states
1766 * until seeing either an INVALID or a
1767 * real interrupt condition.
1769 * When running on a dumb old NE765, give
1770 * up immediately. The controller will
1771 * provide up to four dummy RC interrupt
1772 * conditions right after reset (for the
1773 * corresponding four drives), so this is
1774 * our only chance to get notice that it
1775 * was not the FDC that caused the interrupt.
1777 if (fd_sense_int(fdc, &st0, &cyl)
1780 if(fdc->fdct == FDC_NE765
1781 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1782 return 0; /* hope for a real intr */
1783 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1788 * seek to cyl 0 requested; make sure we are
1791 if (fd_sense_drive_status(fdc, &st3))
1793 if ((st3 & NE7_ST3_T0) == 0) {
1795 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
1796 fdu, st3, NE7_ST3BITS);
1803 return (retrier(fdc));
1807 if (cyl != descyl) {
1809 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
1810 fdu, descyl, cyl, st0);
1813 return (retrier(fdc));
1817 fd->track = b_cylinder;
1818 if (!(fdc->flags & FDC_NODMA)) {
1819 isa_dmastart(isa_dmabp(bp),
1820 bp->b_data+fd->skip,
1821 format ? bp->b_bcount : fdblk, fdc->dmachan);
1823 sectrac = fd->ft.sectrac;
1824 sec = blknum % (sectrac * fd->ft.heads);
1825 head = sec / sectrac;
1826 sec = sec % sectrac + 1;
1827 fd->hddrv = ((head&1)<<2)+fdu;
1831 /* make sure the drive is writable */
1832 if(fd_sense_drive_status(fdc, &st3) != 0)
1834 /* stuck controller? */
1835 if (!(fdc->flags & FDC_NODMA))
1836 isa_dmadone(isa_dmabp(bp),
1837 bp->b_data + fd->skip,
1838 format ? bp->b_bcount : fdblk,
1840 fdc->retry = 6; /* reset the beast */
1841 return (retrier(fdc));
1843 if(st3 & NE7_ST3_WP)
1846 * XXX YES! this is ugly.
1847 * in order to force the current operation
1848 * to fail, we will have to fake an FDC
1849 * error - all error handling is done
1852 fdc->status[0] = NE7_ST0_IC_AT;
1853 fdc->status[1] = NE7_ST1_NW;
1855 fdc->status[3] = fd->track;
1856 fdc->status[4] = head;
1857 fdc->status[5] = sec;
1858 fdc->retry = 8; /* break out immediately */
1859 fdc->state = IOTIMEDOUT; /* not really... */
1865 if (fdc->flags & FDC_NODMA) {
1867 * This seems to be necessary for
1868 * whatever obscure reason; if we omit
1869 * it, we end up filling the sector ID
1870 * fields of the newly formatted track
1871 * entirely with garbage, causing
1872 * `wrong cylinder' errors all over
1873 * the place when trying to read them
1878 SET_BCDR(fdc, 1, bp->b_bcount, 0);
1880 (void)fdcpio(fdc,bp->b_cmd,
1881 bp->b_data+fd->skip,
1886 if(fd_cmd(fdc, 6, NE7CMD_FORMAT, head << 2 | fdu,
1887 finfo->fd_formb_secshift,
1888 finfo->fd_formb_nsecs,
1889 finfo->fd_formb_gaplen,
1890 finfo->fd_formb_fillbyte, 0)) {
1891 /* controller fell over */
1892 if (!(fdc->flags & FDC_NODMA))
1893 isa_dmadone(isa_dmabp(bp),
1894 bp->b_data + fd->skip,
1895 format ? bp->b_bcount : fdblk,
1898 return (retrier(fdc));
1901 if (fdc->flags & FDC_NODMA) {
1903 * this seems to be necessary even when
1906 SET_BCDR(fdc, 1, fdblk, 0);
1909 * perform the write pseudo-DMA before
1910 * the WRITE command is sent
1913 (void)fdcpio(fdc,bp->b_cmd,
1914 bp->b_data+fd->skip,
1918 (read ? NE7CMD_READ : NE7CMD_WRITE),
1919 head << 2 | fdu, /* head & unit */
1920 fd->track, /* track */
1922 sec, /* sector + 1 */
1923 fd->ft.secsize, /* sector size */
1924 sectrac, /* sectors/track */
1925 fd->ft.gap, /* gap size */
1926 fd->ft.datalen, /* data length */
1928 /* the beast is sleeping again */
1929 if (!(fdc->flags & FDC_NODMA))
1930 isa_dmadone(isa_dmabp(bp),
1931 bp->b_data + fd->skip,
1932 format ? bp->b_bcount : fdblk,
1935 return (retrier(fdc));
1938 if (fdc->flags & FDC_NODMA)
1940 * if this is a read, then simply await interrupt
1941 * before performing PIO
1943 if (read && !fdcpio(fdc,bp->b_cmd,
1944 bp->b_data+fd->skip,fdblk)) {
1945 callout_reset(&fd->tohandle, hz,
1947 return(0); /* will return later */
1951 * write (or format) operation will fall through and
1952 * await completion interrupt
1954 fdc->state = IOCOMPLETE;
1955 callout_reset(&fd->tohandle, hz, fd_iotimeout, fdc);
1956 return (0); /* will return later */
1959 * actually perform the PIO read. The IOCOMPLETE case
1960 * removes the timeout for us.
1962 (void)fdcpio(fdc,bp->b_cmd,bp->b_data+fd->skip,fdblk);
1963 fdc->state = IOCOMPLETE;
1965 case IOCOMPLETE: /* IO DONE, post-analyze */
1966 callout_stop(&fd->tohandle);
1968 if (fd_read_status(fdc, fd->fdsu)) {
1969 if (!(fdc->flags & FDC_NODMA)) {
1970 isa_dmadone(isa_dmabp(bp),
1971 bp->b_data + fd->skip,
1972 format ? bp->b_bcount : fdblk,
1976 fdc->retry = 6; /* force a reset */
1977 return (retrier(fdc));
1980 fdc->state = IOTIMEDOUT;
1985 if (!(fdc->flags & FDC_NODMA)) {
1986 isa_dmadone(isa_dmabp(bp),
1987 bp->b_data + fd->skip,
1988 format ? bp->b_bcount : fdblk, fdc->dmachan);
1990 if (fdc->status[0] & NE7_ST0_IC) {
1991 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
1992 && fdc->status[1] & NE7_ST1_OR) {
1994 * DMA overrun. Someone hogged the bus and
1995 * didn't release it in time for the next
1998 * We normally restart this without bumping
1999 * the retry counter. However, in case
2000 * something is seriously messed up (like
2001 * broken hardware), we rather limit the
2002 * number of retries so the IO operation
2003 * doesn't block indefinately.
2005 if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
2006 fdc->state = SEEKCOMPLETE;
2008 } /* else fall through */
2010 if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
2012 fdc->retry = 6; /* force a reset */
2013 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2014 && fdc->status[2] & NE7_ST2_WC
2016 fdc->retry = 3; /* force recalibrate */
2017 return (retrier(fdc));
2020 /* Operation successful, retry DMA overruns again next time. */
2021 fdc->dma_overruns = 0;
2023 if (!format && fd->skip < bp->b_bcount - bp->b_resid) {
2024 /* set up next transfer */
2025 fdc->state = DOSEEK;
2030 device_unbusy(fd->dev);
2031 devstat_end_transaction_buf(&fd->device_stats, bp);
2035 fdc->state = FINDWORK;
2041 fdc->state = RESETCOMPLETE;
2045 * Discard all the results from the reset so that they
2046 * can't cause an unexpected interrupt later.
2048 for (i = 0; i < 4; i++)
2049 (void)fd_sense_int(fdc, &st0, &cyl);
2050 fdc->state = STARTRECAL;
2053 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2056 return (retrier(fdc));
2058 fdc->state = RECALWAIT;
2059 return (0); /* will return later */
2061 /* allow heads to settle */
2062 callout_reset(&fdc->pseudointr_ch, hz / 8, fd_pseudointr, fdc);
2063 fdc->state = RECALCOMPLETE;
2064 return (0); /* will return later */
2068 * See SEEKCOMPLETE for a comment on this:
2070 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2072 if(fdc->fdct == FDC_NE765
2073 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2074 return 0; /* hope for a real intr */
2075 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2076 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
2080 * a recalibrate from beyond cylinder 77
2081 * will "fail" due to the FDC limitations;
2082 * since people used to complain much about
2083 * the failure message, try not logging
2084 * this one if it seems to be the first
2087 kprintf("fd%d: recal failed ST0 %b cyl %d\n",
2088 fdu, st0, NE7_ST0BITS, cyl);
2089 if(fdc->retry < 3) fdc->retry = 3;
2090 return (retrier(fdc));
2093 /* Seek (probably) necessary */
2094 fdc->state = DOSEEK;
2095 return (1); /* will return immediatly */
2097 if(fd->flags & FD_MOTOR_WAIT)
2099 return (0); /* time's not up yet */
2101 if (fdc->flags & FDC_NEEDS_RESET) {
2102 fdc->state = RESETCTLR;
2103 fdc->flags &= ~FDC_NEEDS_RESET;
2106 * If all motors were off, then the controller was
2107 * reset, so it has lost track of the current
2108 * cylinder. Recalibrate to handle this case.
2109 * But first, discard the results of the reset.
2111 fdc->state = RESETCOMPLETE;
2113 return (1); /* will return immediatly */
2115 device_printf(fdc->fdc_dev, "unexpected FD int->");
2116 if (fd_read_status(fdc, fd->fdsu) == 0)
2117 kprintf("FDC status :%x %x %x %x %x %x %x ",
2126 kprintf("No status available ");
2127 if (fd_sense_int(fdc, &st0, &cyl) != 0)
2129 kprintf("[controller is dead now]\n");
2132 kprintf("ST0 = %x, PCN = %x\n", st0, cyl);
2135 /*XXX confusing: some branches return immediately, others end up here*/
2136 return (1); /* Come back immediatly to new state */
2140 retrier(struct fdc_data *fdc)
2150 dev = bio->bio_driver_info;
2152 /* XXX shouldn't this be cached somewhere? */
2154 fd = devclass_get_softc(fd_devclass, fdu);
2155 if (fd->options & FDOPT_NORETRY)
2158 switch (fdc->retry) {
2159 case 0: case 1: case 2:
2160 fdc->state = SEEKCOMPLETE;
2162 case 3: case 4: case 5:
2163 fdc->state = STARTRECAL;
2166 fdc->state = RESETCTLR;
2173 int printerror = (fd->options & FDOPT_NOERRLOG) == 0;
2177 * note: use the correct device for more
2178 * verbose error reporting.
2181 "hard error", LOG_PRINTF,
2185 if (fdc->flags & FDC_STAT_VALID)
2187 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2188 fdc->status[0], NE7_ST0BITS,
2189 fdc->status[1], NE7_ST1BITS,
2190 fdc->status[2], NE7_ST2BITS,
2191 fdc->status[3], fdc->status[4],
2194 kprintf(" (No status)\n");
2197 bp->b_flags |= B_ERROR;
2199 bp->b_resid += bp->b_bcount - fdc->fd->skip;
2202 device_unbusy(fd->dev);
2203 devstat_end_transaction_buf(&fdc->fd->device_stats, bp);
2205 fdc->state = FINDWORK;
2206 fdc->flags |= FDC_NEEDS_RESET;
2216 fdformat(cdev_t dev, struct fd_formb *finfo, struct ucred *cred)
2225 fd = devclass_get_softc(fd_devclass, fdu);
2226 fdblk = 128 << fd->ft.secsize;
2228 /* set up a buffer header for fdstrategy() */
2230 bp->b_cmd = BUF_CMD_FORMAT;
2233 * calculate a fake blkno, so fdstrategy() would initiate a
2234 * seek to the requested cylinder
2236 bp->b_bio1.bio_offset = (off_t)(finfo->cyl *
2237 (fd->ft.sectrac * fd->ft.heads)
2238 + finfo->head * fd->ft.sectrac) * fdblk;
2239 bp->b_bio1.bio_driver_info = dev;
2240 bp->b_bio1.bio_flags |= BIO_SYNC;
2241 bp->b_bio1.bio_done = biodone_sync;
2243 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2244 bp->b_data = (caddr_t)finfo;
2246 /* now do the format */
2247 dev_dstrategy(dev, &bp->b_bio1);
2249 /* ...and wait for it to complete */
2250 rv = biowait_timeout(&bp->b_bio1, "fdform", 20 * hz);
2251 if (rv == EWOULDBLOCK) {
2254 device_unbusy(fd->dev);
2255 biodone(&bp->b_bio1);
2257 if (bp->b_flags & B_ERROR)
2260 * allow the process to be swapped
2267 * TODO: don't allocate buffer on stack.
2271 fdioctl(struct dev_ioctl_args *ap)
2273 cdev_t dev = ap->a_head.a_dev;
2274 fdu_t fdu = dkunit(dev);
2275 fd_p fd = devclass_get_softc(fd_devclass, fdu);
2276 struct fdc_status *fsp;
2279 switch (ap->a_cmd) {
2281 if ((ap->a_fflag & FWRITE) == 0)
2282 error = EBADF; /* must be opened for writing */
2283 else if (((struct fd_formb *)ap->a_data)->format_version !=
2285 error = EINVAL; /* wrong version of formatting prog */
2287 error = fdformat(dev, (struct fd_formb *)ap->a_data, ap->a_cred);
2290 case FD_GTYPE: /* get drive type */
2291 *(struct fd_type *)ap->a_data = fd->ft;
2294 case FD_STYPE: /* set drive type */
2295 /* this is considered harmful; only allow for superuser */
2296 if (priv_check_cred(ap->a_cred, PRIV_ROOT, 0) != 0)
2298 fd->ft = *(struct fd_type *)ap->a_data;
2301 case FD_GOPTS: /* get drive options */
2302 *(int *)ap->a_data = fd->options;
2305 case FD_SOPTS: /* set drive options */
2306 fd->options = *(int *)ap->a_data;
2310 fsp = (struct fdc_status *)ap->a_data;
2311 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2313 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
2324 * Hello emacs, these are the
2327 * c-continued-statement-offset: 8
2328 * c-continued-brace-offset: 0
2329 * c-brace-offset: -8
2330 * c-brace-imaginary-offset: 0
2331 * c-argdecl-indent: 8
2332 * c-label-offset: -8
2333 * c++-hanging-braces: 1
2334 * c++-access-specifier-offset: -8
2335 * c++-empty-arglist-indent: 8
2336 * c++-friend-offset: 0