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 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 * 3. All advertising materials mentioning features or use of this software
30 * must display the following acknowledgement:
31 * This product includes software developed by the University of
32 * California, Berkeley and its contributors.
33 * 4. Neither the name of the University nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91
50 * $Id: fd.c,v 1.145 1999/05/30 16:52:12 phk Exp $
55 #include "opt_devfs.h"
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
66 #include <sys/disklabel.h>
67 #include <sys/devicestat.h>
68 #include <sys/fcntl.h>
69 #include <sys/malloc.h>
70 #include <sys/module.h>
72 #include <sys/syslog.h>
75 #include <machine/bus.h>
78 #include <machine/clock.h>
79 #include <machine/ioctl_fd.h>
80 #include <machine/resource.h>
81 #include <machine/stdarg.h>
84 #include <sys/devfsext.h>
87 #include <isa/isavar.h>
88 #include <isa/isareg.h>
89 #include <isa/fdreg.h>
95 #warning "fix FDC_YE! - newbus casualty"
98 /* misuse a flag to identify format operation */
99 #define B_FORMAT B_XXX
101 /* configuration flags */
102 #define FDC_PRETEND_D0 (1 << 0) /* pretend drive 0 to be there */
104 #define FDC_IS_PCMCIA (1 << 1) /* if successful probe, then it's
108 /* internally used only, not really from CMOS: */
109 #define RTCFDT_144M_PRETENDED 0x1000
111 /* error returns for fd_cmd() */
113 #define FD_NOT_VALID -2
114 #define FDC_ERRMAX 100 /* do not log more */
117 #define NUMDENS (NUMTYPES - 6)
119 /* These defines (-1) must match index for fd_types */
120 #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */
121 #define NO_TYPE 0 /* must match NO_TYPE in ft.c */
131 #define FD_1480in5_25 9
132 #define FD_1440in5_25 10
133 #define FD_820in5_25 11
134 #define FD_800in5_25 12
135 #define FD_720in5_25 13
136 #define FD_360in5_25 14
139 static struct fd_type fd_types[NUMTYPES] =
141 { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
142 { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
143 { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
144 { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */
145 { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */
146 { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */
147 { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */
148 { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */
150 { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
151 { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
152 { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */
153 { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */
154 { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */
155 { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */
158 #define DRVS_PER_CTLR 2 /* 2 floppies */
160 /***********************************************************************\
161 * Per controller structure. *
162 \***********************************************************************/
163 static devclass_t fdc_devclass;
165 /***********************************************************************\
166 * Per drive structure. *
167 * N per controller (DRVS_PER_CTLR) *
168 \***********************************************************************/
170 struct fdc_data *fdc; /* pointer to controller structure */
171 int fdsu; /* this units number on this controller */
172 int type; /* Drive type (FD_1440...) */
173 struct fd_type *ft; /* pointer to the type descriptor */
175 #define FD_OPEN 0x01 /* it's open */
176 #define FD_ACTIVE 0x02 /* it's active */
177 #define FD_MOTOR 0x04 /* motor should be on */
178 #define FD_MOTOR_WAIT 0x08 /* motor coming up */
181 #define FD_NO_TRACK -2
182 int track; /* where we think the head is */
183 int options; /* user configurable options, see ioctl_fd.h */
184 struct callout_handle toffhandle;
185 struct callout_handle tohandle;
186 struct devstat device_stats;
188 void *bdevs[1 + NUMDENS + MAXPARTITIONS];
189 void *cdevs[1 + NUMDENS + MAXPARTITIONS];
194 static devclass_t fd_devclass;
196 /***********************************************************************\
197 * Throughout this file the following conventions will be used: *
198 * fd is a pointer to the fd_data struct for the drive in question *
199 * fdc is a pointer to the fdc_data struct for the controller *
200 * fdu is the floppy drive unit number *
201 * fdcu is the floppy controller unit number *
202 * fdsu is the floppy drive unit number on that controller. (sub-unit) *
203 \***********************************************************************/
207 static int yeattach(struct isa_device *);
210 /* needed for ft driver, thus exported */
211 int in_fdc(struct fdc_data *);
212 int out_fdc(struct fdc_data *, int);
214 /* internal functions */
215 static void fdc_add_device(device_t, const char *, int);
216 static void fdc_intr(void *);
217 static void set_motor(struct fdc_data *, int, int);
220 static timeout_t fd_turnoff;
221 static timeout_t fd_motor_on;
222 static void fd_turnon(struct fd_data *);
223 static void fdc_reset(fdc_p);
224 static int fd_in(struct fdc_data *, int *);
225 static void fdstart(struct fdc_data *);
226 static timeout_t fd_iotimeout;
227 static timeout_t fd_pseudointr;
228 static int fdstate(struct fdc_data *);
229 static int retrier(struct fdc_data *);
230 static int fdformat(dev_t, struct fd_formb *, struct proc *);
232 static int enable_fifo(fdc_p fdc);
234 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */
240 #define SEEKCOMPLETE 3
242 #define RECALCOMPLETE 5
248 #define IOTIMEDOUT 11
249 #define RESETCOMPLETE 12
255 static char const * const fdstates[] =
275 /* CAUTION: fd_debug causes huge amounts of logging output */
276 static int volatile fd_debug = 0;
277 #define TRACE0(arg) if(fd_debug) printf(arg)
278 #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
279 #else /* FDC_DEBUG */
281 #define TRACE1(arg1, arg2)
282 #endif /* FDC_DEBUG */
286 #include <sys/select.h>
287 #include <sys/module.h>
288 #include <pccard/cardinfo.h>
289 #include <pccard/driver.h>
290 #include <pccard/slot.h>
293 * PC-Card (PCMCIA) specific code.
295 static int yeinit(struct pccard_devinfo *); /* init device */
296 static void yeunload(struct pccard_devinfo *); /* Disable driver */
297 static int yeintr(struct pccard_devinfo *); /* Interrupt handler */
299 PCCARD_MODULE(fdc, yeinit, yeunload, yeintr, 0, bio_imask);
302 * this is the secret PIO data port (offset from base)
304 #define FDC_YE_DATAPORT 6
307 * Initialize the device - called from Slot manager.
309 static int yeinit(struct pccard_devinfo *devi)
311 fdc_p fdc = &fdc_data[devi->isahd.id_unit];
313 /* validate unit number. */
314 if (devi->isahd.id_unit >= NFDC)
316 fdc->baseport = devi->isahd.id_iobase;
320 outb(fdc->baseport+FDOUT, 0);
322 outb(fdc->baseport+FDOUT, FDO_FRST);
327 if (yeattach(&devi->isahd) == 0)
334 * yeunload - unload the driver and clear the table.
336 * This is usually called when the card is ejected, but
337 * can be caused by a modunload of a controller driver.
338 * The idea is to reset the driver's view of the device
339 * and ensure that any driver entry points such as
340 * read and write do not hang.
342 static void yeunload(struct pccard_devinfo *devi)
344 if (fd_data[devi->isahd.id_unit].type == NO_TYPE)
348 * this prevents Fdopen() and fdstrategy() from attempting
349 * to access unloaded controller
351 fd_data[devi->isahd.id_unit].type = NO_TYPE;
353 printf("fdc%d: unload\n", devi->isahd.id_unit);
357 * yeintr - Shared interrupt called from
358 * front end of PC-Card handler.
360 static int yeintr(struct pccard_devinfo *devi)
362 fdintr((fdcu_t)devi->isahd.id_unit);
365 #endif /* NCARD > 0 */
368 static d_open_t Fdopen; /* NOTE, not fdopen */
369 static d_close_t fdclose;
370 static d_ioctl_t fdioctl;
371 static d_strategy_t fdstrategy;
376 static struct cdevsw fd_cdevsw = {
380 /* write */ physwrite,
384 /* devtotty */ nodevtotty,
387 /* strategy */ fdstrategy,
390 /* maj */ CDEV_MAJOR,
395 /* bmaj */ BDEV_MAJOR
399 fdc_err(struct fdc_data *fdc, const char *s)
403 if (fdc->fdc_errs < FDC_ERRMAX) {
404 device_print_prettyname(fdc->fdc_dev);
406 } else if (fdc->fdc_errs == FDC_ERRMAX) {
407 device_print_prettyname(fdc->fdc_dev);
408 printf("too many errors, not logging any more\n");
416 * fd_cmd: Send a command to the chip. Takes a varargs with this structure:
418 * # of output bytes, output bytes as ints ...,
419 * # of input bytes, input bytes as ints ...
422 fd_cmd(struct fdc_data *fdc, int n_out, ...)
430 cmd = (u_char)(va_arg(ap, int));
433 for (n = 0; n < n_out; n++)
435 if (out_fdc(fdc, va_arg(ap, int)) < 0)
438 snprintf(msg, sizeof(msg),
439 "cmd %x failed at out byte %d of %d\n",
441 return fdc_err(fdc, msg);
444 n_in = va_arg(ap, int);
445 for (n = 0; n < n_in; n++)
447 int *ptr = va_arg(ap, int *);
448 if (fd_in(fdc, ptr) < 0)
451 snprintf(msg, sizeof(msg),
452 "cmd %02x failed at in byte %d of %d\n",
454 return fdc_err(fdc, msg);
462 enable_fifo(fdc_p fdc)
466 if ((fdc->flags & FDC_HAS_FIFO) == 0) {
470 * Cannot use fd_cmd the normal way here, since
471 * this might be an invalid command. Thus we send the
472 * first byte, and check for an early turn of data directon.
475 if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
476 return fdc_err(fdc, "Enable FIFO failed\n");
478 /* If command is invalid, return */
480 while ((i = inb(fdc->baseport + FDSTS) & (NE7_DIO | NE7_RQM))
481 != NE7_RQM && j-- > 0)
482 if (i == (NE7_DIO | NE7_RQM)) {
488 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
490 return fdc_err(fdc, "Enable FIFO failed\n");
492 fdc->flags |= FDC_HAS_FIFO;
496 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
497 return fdc_err(fdc, "Re-enable FIFO failed\n");
502 fd_sense_drive_status(fdc_p fdc, int *st3p)
506 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
508 return fdc_err(fdc, "Sense Drive Status failed\n");
517 fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
521 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
524 "sense intr err reading stat reg 0\n");
531 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
533 * There doesn't seem to have been an interrupt.
538 if (fd_in(fdc, &cyl) < 0) {
539 return fdc_err(fdc, "can't get cyl num\n");
550 fd_read_status(fdc_p fdc, int fdsu)
554 for (i = 0; i < 7; i++) {
556 * XXX types are poorly chosen. Only bytes can by read
557 * from the hardware, but fdc->status[] wants u_ints and
558 * fd_in() gives ints.
562 ret = fd_in(fdc, &status);
563 fdc->status[i] = status;
569 fdc->flags |= FDC_STAT_VALID;
571 fdc->flags &= ~FDC_STAT_VALID;
576 /****************************************************************************/
577 /* autoconfiguration stuff */
578 /****************************************************************************/
581 fdc_probe(device_t dev)
583 int error, i, ic_type;
584 struct fdc_data *fdc;
585 char myname[8]; /* better be long enough */
588 if (isa_get_vendorid(dev))
591 fdc = device_get_softc(dev);
592 bzero(fdc, sizeof *fdc);
594 fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
595 fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
597 fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
598 &fdc->rid_ioport, 0ul, ~0ul,
599 IO_FDCSIZE, RF_ACTIVE);
600 if (fdc->res_ioport == 0) {
601 device_print_prettyname(dev);
602 printf("cannot reserve I/O port range\n");
606 fdc->baseport = fdc->res_ioport->r_start;
608 fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
609 &fdc->rid_irq, 0ul, ~0ul, 1,
611 if (fdc->res_irq == 0) {
612 device_print_prettyname(dev);
613 printf("cannot reserve interrupt line\n");
617 fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
618 &fdc->rid_drq, 0ul, ~0ul, 1,
620 if (fdc->res_drq == 0) {
621 device_print_prettyname(dev);
622 printf("cannot reserve DMA request line\n");
626 fdc->dmachan = fdc->res_drq->r_start;
627 error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
628 INTR_TYPE_BIO, fdc_intr, fdc, &fdc->fdc_intr);
630 /* First - lets reset the floppy controller */
631 outb(fdc->baseport + FDOUT, 0);
633 outb(fdc->baseport + FDOUT, FDO_FRST);
635 /* see if it can handle a command */
636 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
637 NE7_SPEC_2(2, 0), 0)) {
642 if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
643 ic_type = (u_char)ic_type;
646 device_set_desc(dev, "NEC 765 or clone");
647 fdc->fdct = FDC_NE765;
650 device_set_desc(dev, "Intel 82077 or clone");
651 fdc->fdct = FDC_I82077;
654 device_set_desc(dev, "NEC 72065B or clone");
655 fdc->fdct = FDC_NE72065;
658 device_set_desc(dev, "generic floppy controller");
659 fdc->fdct = FDC_UNKNOWN;
664 snprintf(myname, sizeof(myname), "%s%d", device_get_name(dev),
665 device_get_unit(dev));
666 for (i = resource_query_string(-1, "at", myname); i != -1;
667 i = resource_query_string(i, "at", myname))
668 fdc_add_device(dev, resource_query_name(i),
669 resource_query_unit(i));
672 * don't succeed on probe; wait
673 * for PCCARD subsystem to do it
675 if (dev->id_flags & FDC_IS_PCMCIA)
682 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
684 if (fdc->res_irq != 0) {
685 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
687 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
690 if (fdc->res_ioport != 0) {
691 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
693 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
696 if (fdc->res_drq != 0) {
697 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
699 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
706 * Aped dfr@freebsd.org's isa_add_device().
709 fdc_add_device(device_t dev, const char *name, int unit)
714 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT);
717 if (resource_int_value(name, unit, "drive", ivar) != 0)
719 child = device_add_child(dev, name, unit, ivar);
722 if (resource_int_value(name, unit, "disabled", &disabled) == 0
724 device_disable(child);
728 fdc_attach(device_t dev)
730 struct fdc_data *fdc = device_get_softc(dev);
731 fdcu_t fdcu = device_get_unit(dev);
734 fdc->flags |= FDC_ATTACHED;
736 /* Acquire the DMA channel forever, The driver will do the rest */
737 /* XXX should integrate with rman */
738 isa_dma_acquire(fdc->dmachan);
739 isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
740 fdc->state = DEVIDLE;
742 /* reset controller, turn motor off, clear fdout mirror reg */
743 outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
744 bufq_init(&fdc->head);
746 #ifdef FIFO_BEFORE_MOTORON
747 /* Hmm, this doesn't work here - is set_motor() magic? -Peter */
748 if (fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN
749 && enable_fifo(fdc) == 0) {
750 device_print_prettyname(dev);
751 printf("FIFO enabled, %d bytes threshold\n", fifo_threshold);
755 * Probe and attach any children as were configured above.
757 return (bus_generic_attach(dev));
761 fdc_print_child(device_t me, device_t child)
763 printf(" at %s%d drive %d", device_get_name(me), device_get_unit(me),
764 *(int *)device_get_ivars(child));
768 fd_probe(device_t dev)
773 struct fdc_data *fdc;
775 #ifndef FIFO_BEFORE_MOTORON
776 static int fd_fifo = 0;
779 fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
780 fd = device_get_softc(dev);
781 fdc = device_get_softc(device_get_parent(dev));
783 bzero(fd, sizeof *fd);
787 fd->fdu = device_get_unit(dev);
790 /* look up what bios thinks we have */
793 if (isa_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0)
794 fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
796 fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
799 fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
806 fdt = RTCFDT_144M; /* XXX probably */
809 /* is there a unit? */
810 if (fdt == RTCFDT_NONE)
814 set_motor(fdc, fdsu, TURNON);
815 DELAY(1000000); /* 1 sec */
817 #ifndef FIFO_BEFORE_MOTORON
818 if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN
819 && enable_fifo(fdc) == 0) {
820 device_print_prettyname(device_get_parent(dev));
821 printf("FIFO enabled, %d bytes threshold\n", fifo_threshold);
826 if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0)
827 && (st3 & NE7_ST3_T0)) {
828 /* if at track 0, first seek inwards */
829 /* seek some steps: */
830 fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0);
831 DELAY(300000); /* ...wait a moment... */
832 fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
835 /* If we're at track 0 first seek inwards. */
836 if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
837 /* Seek some steps... */
838 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
839 /* ...wait a moment... */
841 /* make ctrlr happy: */
842 fd_sense_int(fdc, 0, 0);
846 for (i = 0; i < 2; i++) {
848 * we must recalibrate twice, just in case the
849 * heads have been beyond cylinder 76, since most
850 * FDCs still barf when attempting to recalibrate
854 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
855 /* a second being enough for full stroke seek*/
856 DELAY(i == 0 ? 1000000 : 300000);
858 /* anything responding? */
859 if (fd_sense_int(fdc, &st0, 0) == 0 &&
860 (st0 & NE7_ST0_EC) == 0)
861 break; /* already probed succesfully */
865 set_motor(fdc, fdsu, TURNOFF);
867 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
870 fd->track = FD_NO_TRACK;
874 callout_handle_init(&fd->toffhandle);
875 callout_handle_init(&fd->tohandle);
879 device_set_desc(dev, "1200-KB 5.25\" drive");
882 case RTCFDT_144M | RTCFDT_144M_PRETENDED:
883 device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive");
887 device_set_desc(dev, "1440-KB 3.5\" drive");
892 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
896 device_set_desc(dev, "360-KB 5.25\" drive");
900 printf("720-KB 3.5\" drive");
910 fd_attach(device_t dev)
920 fd = device_get_softc(dev);
922 #ifdef DEVFS /* XXX bitrot */
923 mynor = fd->fdu << 6;
924 fd->bdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_BLK,
925 UID_ROOT, GID_OPERATOR, 0640,
927 fd->cdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_CHR,
928 UID_ROOT, GID_OPERATOR, 0640,
930 for (i = 1; i < 1 + NUMDENS; i++) {
932 * XXX this and the lookup in Fdopen() should be
941 if (i != FD_720 && i != FD_800 && i != FD_820)
945 if (i != FD_360 && i != FD_720 && i != FD_800
946 && i != FD_820 && i != FD_1200
947 && i != FD_1440 && i != FD_1480)
951 if (i != FD_720 && i != FD_800 && i != FD_820
952 && i != FD_1200 && i != FD_1440
953 && i != FD_1480 && i != FD_1720)
957 typesize = fd_types[i - 1].size / 2;
959 * XXX all these conversions give bloated code and
962 if (typesize == 1476)
964 if (typesize == 1722)
966 typemynor = mynor | i;
968 devfs_add_devswf(&fd_cdevsw, typemynor, DV_BLK,
969 UID_ROOT, GID_OPERATOR, 0640,
970 "fd%d.%d", fd->fdu, typesize);
972 devfs_add_devswf(&fd_cdevsw, typemynor, DV_CHR,
973 UID_ROOT, GID_OPERATOR, 0640,
974 "rfd%d.%d", fd->fdu, typesize);
977 for (i = 0; i < MAXPARTITIONS; i++) {
978 fd->bdevs[1 + NUMDENS + i] = devfs_makelink(fd->bdevs[0],
979 "fd%d%c", fd->fdu, 'a' + i);
980 fd->cdevs[1 + NUMDENS + i] =
981 devfs_makelink(fd->cdevs[0],
982 "rfd%d%c", fd->fdu, 'a' + i);
986 * Export the drive to the devstat interface.
988 devstat_add_entry(&fd->device_stats, device_get_name(dev),
989 device_get_unit(dev), 512, DEVSTAT_NO_ORDERED_TAGS,
990 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
991 DEVSTAT_PRIORITY_FD);
997 * this is a subset of fdattach() optimized for the Y-E Data
998 * PCMCIA floppy drive.
1000 static int yeattach(struct isa_device *dev)
1002 fdcu_t fdcu = dev->id_unit;
1003 fdc_p fdc = fdc_data + fdcu;
1004 fdsu_t fdsu = 0; /* assume 1 drive per YE controller */
1015 * the FDC_PCMCIA flag is used to to indicate special PIO is used
1018 fdc->flags = FDC_ATTACHED|FDC_PCMCIA;
1019 fdc->state = DEVIDLE;
1020 /* reset controller, turn motor off, clear fdout mirror reg */
1021 outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
1022 bufq_init(&fdc->head);
1024 * assume 2 drives/ "normal" controller
1028 printf("fdu %d >= NFD\n",fdu);
1033 set_motor(fdcu, fdsu, TURNON);
1034 DELAY(1000000); /* 1 sec */
1035 fdc->fdct = FDC_NE765;
1037 if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) &&
1038 (st3 & NE7_ST3_T0)) {
1039 /* if at track 0, first seek inwards */
1040 /* seek some steps: */
1041 (void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0);
1042 DELAY(300000); /* ...wait a moment... */
1043 (void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
1046 /* If we're at track 0 first seek inwards. */
1047 if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
1048 /* Seek some steps... */
1049 if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1050 /* ...wait a moment... */
1052 /* make ctrlr happy: */
1053 (void)fd_sense_int(fdc, 0, 0);
1057 for(i = 0; i < 2; i++) {
1059 * we must recalibrate twice, just in case the
1060 * heads have been beyond cylinder 76, since most
1061 * FDCs still barf when attempting to recalibrate
1062 * more than 77 steps
1065 if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1066 /* a second being enough for full stroke seek*/
1067 DELAY(i == 0? 1000000: 300000);
1069 /* anything responding? */
1070 if (fd_sense_int(fdc, &st0, 0) == 0 &&
1071 (st0 & NE7_ST0_EC) == 0)
1072 break; /* already probed succesfully */
1076 set_motor(fdcu, fdsu, TURNOFF);
1078 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
1081 fd->track = FD_NO_TRACK;
1085 printf("fdc%d: 1.44MB 3.5in PCMCIA\n", fdcu);
1090 fd->bdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_BLK,
1091 UID_ROOT, GID_OPERATOR, 0640,
1093 fd->cdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_CHR,
1094 UID_ROOT, GID_OPERATOR, 0640,
1097 * XXX this and the lookup in Fdopen() should be
1100 typemynor = mynor | FD_1440;
1101 typesize = fd_types[FD_1440 - 1].size / 2;
1103 * XXX all these conversions give bloated code and
1106 if (typesize == 1476)
1108 if (typesize == 1722)
1110 fd->bdevs[FD_1440] = devfs_add_devswf(&fd_cdevsw, typemynor,
1111 DV_BLK, UID_ROOT, GID_OPERATOR,
1112 0640, "fd%d.%d", fdu, typesize);
1113 fd->cdevs[FD_1440] = devfs_add_devswf(&fd_cdevsw, typemynor,
1114 DV_CHR, UID_ROOT, GID_OPERATOR,
1115 0640,"rfd%d.%d", fdu, typesize);
1116 for (i = 0; i < MAXPARTITIONS; i++) {
1117 fd->bdevs[1 + NUMDENS + i] = devfs_makelink(fd->bdevs[0],
1118 "fd%d%c", fdu, 'a' + i);
1119 fd->cdevs[1 + NUMDENS + i] = devfs_makelink(fd->cdevs[0],
1120 "rfd%d%c", fdu, 'a' + i);
1127 /****************************************************************************/
1128 /* motor control stuff */
1129 /* remember to not deselect the drive we're working on */
1130 /****************************************************************************/
1132 set_motor(struct fdc_data *fdc, int fdsu, int turnon)
1134 int fdout = fdc->fdout;
1135 int needspecify = 0;
1138 fdout &= ~FDO_FDSEL;
1139 fdout |= (FDO_MOEN0 << fdsu) + fdsu;
1141 fdout &= ~(FDO_MOEN0 << fdsu);
1144 && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
1145 /* gonna turn off the last drive, put FDC to bed */
1146 fdout &= ~ (FDO_FRST|FDO_FDMAEN);
1148 /* make sure controller is selected and specified */
1149 if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
1151 fdout |= (FDO_FRST|FDO_FDMAEN);
1154 outb(fdc->baseport+FDOUT, fdout);
1156 TRACE1("[0x%x->FDOUT]", fdout);
1161 * special case: since we have just woken up the FDC
1162 * from its sleep, we silently assume the command will
1163 * be accepted, and do not test for a timeout
1165 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1166 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1168 if (fdc->flags & FDC_HAS_FIFO)
1169 (void) enable_fifo(fdc);
1174 fd_turnoff(void *xfd)
1179 TRACE1("[fd%d: turnoff]", fd->fdu);
1182 * Don't turn off the motor yet if the drive is active.
1183 * XXX shouldn't even schedule turnoff until drive is inactive
1184 * and nothing is queued on it.
1186 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
1187 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
1192 fd->flags &= ~FD_MOTOR;
1193 set_motor(fd->fdc, fd->fdsu, TURNOFF);
1198 fd_motor_on(void *xfd)
1204 fd->flags &= ~FD_MOTOR_WAIT;
1205 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
1215 if(!(fd->flags & FD_MOTOR))
1217 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
1218 set_motor(fd->fdc, fd->fdsu, TURNON);
1219 timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
1224 fdc_reset(fdc_p fdc)
1226 /* Try a reset, keep motor on */
1227 outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1228 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1230 /* enable FDC, but defer interrupts a moment */
1231 outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
1232 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
1234 outb(fdc->baseport + FDOUT, fdc->fdout);
1235 TRACE1("[0x%x->FDOUT]", fdc->fdout);
1237 /* XXX after a reset, silently believe the FDC will accept commands */
1238 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1239 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1241 if (fdc->flags & FDC_HAS_FIFO)
1242 (void) enable_fifo(fdc);
1245 /****************************************************************************/
1247 /****************************************************************************/
1249 in_fdc(struct fdc_data *fdc)
1251 int baseport = fdc->baseport;
1253 while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
1254 != (NE7_DIO|NE7_RQM) && j-- > 0)
1256 return fdc_err(fdc, "ready for output in input\n");
1258 return fdc_err(fdc, bootverbose? "input ready timeout\n": 0);
1260 i = inb(baseport+FDDATA);
1261 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1263 #else /* !FDC_DEBUG */
1264 return inb(baseport+FDDATA);
1265 #endif /* FDC_DEBUG */
1269 * fd_in: Like in_fdc, but allows you to see if it worked.
1272 fd_in(struct fdc_data *fdc, int *ptr)
1274 int baseport = fdc->baseport;
1276 while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
1277 != (NE7_DIO|NE7_RQM) && j-- > 0)
1279 return fdc_err(fdc, "ready for output in input\n");
1281 return fdc_err(fdc, bootverbose? "input ready timeout\n": 0);
1283 i = inb(baseport+FDDATA);
1284 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1287 #else /* !FDC_DEBUG */
1288 i = inb(baseport+FDDATA);
1292 #endif /* FDC_DEBUG */
1296 out_fdc(struct fdc_data *fdc, int x)
1298 int baseport = fdc->baseport;
1301 /* Check that the direction bit is set */
1303 while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
1304 if (i <= 0) return fdc_err(fdc, "direction bit not set\n");
1306 /* Check that the floppy controller is ready for a command */
1308 while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
1310 return fdc_err(fdc, bootverbose? "output ready timeout\n": 0);
1312 /* Send the command and return */
1313 outb(baseport+FDDATA, x);
1314 TRACE1("[0x%x->FDDATA]", x);
1318 /****************************************************************************/
1319 /* fdopen/fdclose */
1320 /****************************************************************************/
1322 Fdopen(dev_t dev, int flags, int mode, struct proc *p)
1324 fdu_t fdu = FDUNIT(minor(dev));
1325 int type = FDTYPE(minor(dev));
1330 if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1333 if ((fdc == NULL) || (fd->type == NO_TYPE))
1341 * For each type of basic drive, make sure we are trying
1342 * to open a type it can do,
1344 if (type != fd->type) {
1357 type = FD_1480in5_25;
1360 type = FD_1440in5_25;
1363 type = FD_820in5_25;
1366 type = FD_800in5_25;
1369 type = FD_720in5_25;
1372 type = FD_360in5_25;
1379 if ( type != FD_1720
1391 fd->ft = fd_types + type - 1;
1392 fd->flags |= FD_OPEN;
1393 device_busy(fd->dev);
1394 device_busy(fd->fdc->fdc_dev);
1399 fdclose(dev_t dev, int flags, int mode, struct proc *p)
1401 fdu_t fdu = FDUNIT(minor(dev));
1404 fd = devclass_get_softc(fd_devclass, fdu);
1405 fd->flags &= ~FD_OPEN;
1406 fd->options &= ~FDOPT_NORETRY;
1411 /****************************************************************************/
1413 /****************************************************************************/
1415 fdstrategy(struct buf *bp)
1417 unsigned nblocks, blknum, cando;
1424 fdu = FDUNIT(minor(bp->b_dev));
1425 fd = devclass_get_softc(fd_devclass, fdu);
1427 panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
1428 (u_long)major(bp->b_dev), (u_long)minor(bp->b_dev));
1431 if (fd->type == NO_TYPE) {
1432 bp->b_error = ENXIO;
1433 bp->b_flags |= B_ERROR;
1435 * I _refuse_ to use a goto
1442 fdblk = 128 << (fd->ft->secsize);
1443 if (!(bp->b_flags & B_FORMAT)) {
1444 if (bp->b_blkno < 0) {
1446 "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1447 fdu, (u_long)bp->b_blkno, bp->b_bcount);
1448 bp->b_error = EINVAL;
1449 bp->b_flags |= B_ERROR;
1452 if ((bp->b_bcount % fdblk) != 0) {
1453 bp->b_error = EINVAL;
1454 bp->b_flags |= B_ERROR;
1460 * Set up block calculations.
1462 if (bp->b_blkno > 20000000) {
1464 * Reject unreasonably high block number, prevent the
1465 * multiplication below from overflowing.
1467 bp->b_error = EINVAL;
1468 bp->b_flags |= B_ERROR;
1471 blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk;
1472 nblocks = fd->ft->size;
1474 if (blknum + (bp->b_bcount / fdblk) > nblocks) {
1475 if (blknum <= nblocks) {
1476 cando = (nblocks - blknum) * fdblk;
1477 bp->b_resid = bp->b_bcount - cando;
1479 goto bad; /* not actually bad but EOF */
1481 bp->b_error = EINVAL;
1482 bp->b_flags |= B_ERROR;
1486 bp->b_pblkno = bp->b_blkno;
1488 bufqdisksort(&fdc->head, bp);
1489 untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1491 /* Tell devstat we are starting on the transaction */
1492 devstat_start_transaction(&fd->device_stats);
1502 /***************************************************************\
1504 * We have just queued something.. if the controller is not busy *
1505 * then simulate the case where it has just finished a command *
1506 * So that it (the interrupt routine) looks on the queue for more*
1507 * work to do and picks up what we just added. *
1508 * If the controller is already busy, we need do nothing, as it *
1509 * will pick up our work when the present work completes *
1510 \***************************************************************/
1512 fdstart(struct fdc_data *fdc)
1517 if(fdc->state == DEVIDLE)
1525 fd_iotimeout(void *xfdc)
1531 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
1534 * Due to IBM's brain-dead design, the FDC has a faked ready
1535 * signal, hardwired to ready == true. Thus, any command
1536 * issued if there's no diskette in the drive will _never_
1537 * complete, and must be aborted by resetting the FDC.
1538 * Many thanks, Big Blue!
1539 * The FDC must not be reset directly, since that would
1540 * interfere with the state machine. Instead, pretend that
1541 * the command completed but was invalid. The state machine
1542 * will reset the FDC and retry once.
1545 fdc->status[0] = NE7_ST0_IC_IV;
1546 fdc->flags &= ~FDC_STAT_VALID;
1547 fdc->state = IOTIMEDOUT;
1552 /* just ensure it has the right spl */
1554 fd_pseudointr(void *xfdc)
1563 /***********************************************************************\
1565 * keep calling the state machine until it returns a 0 *
1566 * ALWAYS called at SPLBIO *
1567 \***********************************************************************/
1569 fdc_intr(void *xfdc)
1578 * magic pseudo-DMA initialization for YE FDC. Sets count and
1581 #define SET_BCDR(wr,cnt,port) outb(port,(((cnt)-1) & 0xff)); \
1582 outb(port+1,((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)))
1585 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy
1587 static int fdcpio(fdcu_t fdcu, long flags, caddr_t addr, u_int count)
1589 u_char *cptr = (u_char *)addr;
1590 fdc_p fdc = &fdc_data[fdcu];
1591 int io = fdc->baseport;
1593 if (flags & B_READ) {
1594 if (fdc->state != PIOREAD) {
1595 fdc->state = PIOREAD;
1598 SET_BCDR(0,count,io);
1599 insb(io+FDC_YE_DATAPORT,cptr,count);
1601 outsb(io+FDC_YE_DATAPORT,cptr,count);
1602 SET_BCDR(0,count,io);
1608 /***********************************************************************\
1609 * The controller state machine. *
1610 * if it returns a non zero value, it should be called again immediatly *
1611 \***********************************************************************/
1615 int read, format, head, i, sec = 0, sectrac, st0, cyl, st3;
1616 unsigned blknum = 0, b_cylinder = 0;
1617 fdu_t fdu = fdc->fdu;
1619 register struct buf *bp;
1620 struct fd_formb *finfo = NULL;
1625 bp = bufq_first(&fdc->head);
1627 bufq_remove(&fdc->head, bp);
1632 /***********************************************\
1633 * nothing left for this controller to do *
1634 * Force into the IDLE state, *
1635 \***********************************************/
1636 fdc->state = DEVIDLE;
1638 device_print_prettyname(fdc->fdc_dev);
1639 printf("unexpected valid fd pointer\n");
1643 TRACE1("[fdc%d IDLE]", fdc->fdcu);
1646 fdu = FDUNIT(minor(bp->b_dev));
1647 fd = devclass_get_softc(fd_devclass, fdu);
1648 fdblk = 128 << fd->ft->secsize;
1649 if (fdc->fd && (fd != fdc->fd)) {
1650 device_print_prettyname(fd->dev);
1651 printf("confused fd pointers\n");
1653 read = bp->b_flags & B_READ;
1654 format = bp->b_flags & B_FORMAT;
1656 finfo = (struct fd_formb *)bp->b_data;
1657 fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1660 if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
1661 blknum = (unsigned) bp->b_pblkno * DEV_BSIZE/fdblk +
1663 b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1665 TRACE1("fd%d", fdu);
1666 TRACE1("[%s]", fdstates[fdc->state]);
1667 TRACE1("(0x%x)", fd->flags);
1668 untimeout(fd_turnoff, fd, fd->toffhandle);
1669 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
1673 case FINDWORK: /* we have found new work */
1678 outb(fdc->baseport+FDCTL, fd->ft->trans);
1679 TRACE1("[0x%x->FDCTL]", fd->ft->trans);
1680 /*******************************************************\
1681 * If the next drive has a motor startup pending, then *
1682 * it will start up in its own good time *
1683 \*******************************************************/
1684 if(fd->flags & FD_MOTOR_WAIT) {
1685 fdc->state = MOTORWAIT;
1686 return (0); /* come back later */
1688 /*******************************************************\
1689 * Maybe if it's not starting, it SHOULD be starting *
1690 \*******************************************************/
1691 if (!(fd->flags & FD_MOTOR))
1693 fdc->state = MOTORWAIT;
1697 else /* at least make sure we are selected */
1699 set_motor(fdc, fd->fdsu, TURNON);
1701 if (fdc->flags & FDC_NEEDS_RESET) {
1702 fdc->state = RESETCTLR;
1703 fdc->flags &= ~FDC_NEEDS_RESET;
1705 fdc->state = DOSEEK;
1708 if (b_cylinder == (unsigned)fd->track)
1710 fdc->state = SEEKCOMPLETE;
1713 if (fd_cmd(fdc, 3, NE7CMD_SEEK,
1714 fd->fdsu, b_cylinder * fd->ft->steptrac,
1718 * seek command not accepted, looks like
1719 * the FDC went off to the Saints...
1721 fdc->retry = 6; /* try a reset */
1722 return(retrier(fdc));
1724 fd->track = FD_NO_TRACK;
1725 fdc->state = SEEKWAIT;
1726 return(0); /* will return later */
1728 /* allow heads to settle */
1729 timeout(fd_pseudointr, fdc, hz / 16);
1730 fdc->state = SEEKCOMPLETE;
1731 return(0); /* will return later */
1732 case SEEKCOMPLETE : /* SEEK DONE, START DMA */
1733 /* Make sure seek really happened*/
1734 if(fd->track == FD_NO_TRACK) {
1735 int descyl = b_cylinder * fd->ft->steptrac;
1738 * This might be a "ready changed" interrupt,
1739 * which cannot really happen since the
1740 * RDY pin is hardwired to + 5 volts. This
1741 * generally indicates a "bouncing" intr
1742 * line, so do one of the following:
1744 * When running on an enhanced FDC that is
1745 * known to not go stuck after responding
1746 * with INVALID, fetch all interrupt states
1747 * until seeing either an INVALID or a
1748 * real interrupt condition.
1750 * When running on a dumb old NE765, give
1751 * up immediately. The controller will
1752 * provide up to four dummy RC interrupt
1753 * conditions right after reset (for the
1754 * corresponding four drives), so this is
1755 * our only chance to get notice that it
1756 * was not the FDC that caused the interrupt.
1758 if (fd_sense_int(fdc, &st0, &cyl)
1761 if(fdc->fdct == FDC_NE765
1762 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1763 return 0; /* hope for a real intr */
1764 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1769 * seek to cyl 0 requested; make sure we are
1772 if (fd_sense_drive_status(fdc, &st3))
1774 if ((st3 & NE7_ST3_T0) == 0) {
1776 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
1777 fdu, st3, NE7_ST3BITS);
1784 return (retrier(fdc));
1788 if (cyl != descyl) {
1790 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
1791 fdu, descyl, cyl, st0);
1794 return (retrier(fdc));
1798 fd->track = b_cylinder;
1800 if (!(fdc->flags & FDC_PCMCIA))
1802 isa_dmastart(bp->b_flags, bp->b_data+fd->skip,
1803 format ? bp->b_bcount : fdblk, fdc->dmachan);
1804 sectrac = fd->ft->sectrac;
1805 sec = blknum % (sectrac * fd->ft->heads);
1806 head = sec / sectrac;
1807 sec = sec % sectrac + 1;
1808 fd->hddrv = ((head&1)<<2)+fdu;
1812 /* make sure the drive is writable */
1813 if(fd_sense_drive_status(fdc, &st3) != 0)
1815 /* stuck controller? */
1816 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1817 format ? bp->b_bcount : fdblk,
1819 fdc->retry = 6; /* reset the beast */
1820 return (retrier(fdc));
1822 if(st3 & NE7_ST3_WP)
1825 * XXX YES! this is ugly.
1826 * in order to force the current operation
1827 * to fail, we will have to fake an FDC
1828 * error - all error handling is done
1831 fdc->status[0] = NE7_ST0_IC_AT;
1832 fdc->status[1] = NE7_ST1_NW;
1834 fdc->status[3] = fd->track;
1835 fdc->status[4] = head;
1836 fdc->status[5] = sec;
1837 fdc->retry = 8; /* break out immediately */
1838 fdc->state = IOTIMEDOUT; /* not really... */
1845 if (fdc->flags & FDC_PCMCIA)
1846 (void)fdcpio(fdcu,bp->b_flags,
1847 bp->b_data+fd->skip,
1851 if(fd_cmd(fdc, 6, NE7CMD_FORMAT, head << 2 | fdu,
1852 finfo->fd_formb_secshift,
1853 finfo->fd_formb_nsecs,
1854 finfo->fd_formb_gaplen,
1855 finfo->fd_formb_fillbyte, 0)) {
1856 /* controller fell over */
1857 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1858 format ? bp->b_bcount : fdblk,
1861 return (retrier(fdc));
1865 if (fdc->flags & FDC_PCMCIA) {
1867 * this seems to be necessary even when
1870 SET_BCDR(1,fdblk,fdc->baseport);
1873 * perform the write pseudo-DMA before
1874 * the WRITE command is sent
1877 (void)fdcpio(fdcu,bp->b_flags,
1878 bp->b_data+fd->skip,
1883 (read ? NE7CMD_READ : NE7CMD_WRITE),
1884 head << 2 | fdu, /* head & unit */
1885 fd->track, /* track */
1887 sec, /* sector + 1 */
1888 fd->ft->secsize, /* sector size */
1889 sectrac, /* sectors/track */
1890 fd->ft->gap, /* gap size */
1891 fd->ft->datalen, /* data length */
1893 /* the beast is sleeping again */
1894 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1895 format ? bp->b_bcount : fdblk,
1898 return (retrier(fdc));
1902 if (fdc->flags & FDC_PCMCIA)
1904 * if this is a read, then simply await interrupt
1905 * before performing PIO
1907 if (read && !fdcpio(fdcu,bp->b_flags,
1908 bp->b_data+fd->skip,fdblk)) {
1909 fd->tohandle = timeout(fd_iotimeout,
1911 return(0); /* will return later */
1915 * write (or format) operation will fall through and
1916 * await completion interrupt
1919 fdc->state = IOCOMPLETE;
1920 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
1921 return (0); /* will return later */
1925 * actually perform the PIO read. The IOCOMPLETE case
1926 * removes the timeout for us.
1928 (void)fdcpio(fdcu,bp->b_flags,bp->b_data+fd->skip,fdblk);
1929 fdc->state = IOCOMPLETE;
1932 case IOCOMPLETE: /* IO DONE, post-analyze */
1933 untimeout(fd_iotimeout, fdc, fd->tohandle);
1935 if (fd_read_status(fdc, fd->fdsu)) {
1936 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1937 format ? bp->b_bcount : fdblk,
1940 fdc->retry = 6; /* force a reset */
1941 return (retrier(fdc));
1944 fdc->state = IOTIMEDOUT;
1950 if (!(fdc->flags & FDC_PCMCIA))
1952 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1953 format ? bp->b_bcount : fdblk, fdc->dmachan);
1954 if (fdc->status[0] & NE7_ST0_IC) {
1955 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
1956 && fdc->status[1] & NE7_ST1_OR) {
1958 * DMA overrun. Someone hogged the bus
1959 * and didn't release it in time for the
1960 * next FDC transfer.
1961 * Just restart it, don't increment retry
1964 fdc->state = SEEKCOMPLETE;
1967 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
1969 fdc->retry = 6; /* force a reset */
1970 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
1971 && fdc->status[2] & NE7_ST2_WC
1973 fdc->retry = 3; /* force recalibrate */
1974 return (retrier(fdc));
1978 if (!format && fd->skip < bp->b_bcount - bp->b_resid) {
1979 /* set up next transfer */
1980 fdc->state = DOSEEK;
1985 /* Tell devstat we have finished with the transaction */
1986 devstat_end_transaction(&fd->device_stats,
1987 bp->b_bcount - bp->b_resid,
1989 (bp->b_flags & B_READ) ?
1990 DEVSTAT_READ : DEVSTAT_WRITE);
1994 fdc->state = FINDWORK;
2000 fdc->state = RESETCOMPLETE;
2004 * Discard all the results from the reset so that they
2005 * can't cause an unexpected interrupt later.
2007 for (i = 0; i < 4; i++)
2008 (void)fd_sense_int(fdc, &st0, &cyl);
2009 fdc->state = STARTRECAL;
2012 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2015 return (retrier(fdc));
2017 fdc->state = RECALWAIT;
2018 return (0); /* will return later */
2020 /* allow heads to settle */
2021 timeout(fd_pseudointr, fdc, hz / 8);
2022 fdc->state = RECALCOMPLETE;
2023 return (0); /* will return later */
2027 * See SEEKCOMPLETE for a comment on this:
2029 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2031 if(fdc->fdct == FDC_NE765
2032 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2033 return 0; /* hope for a real intr */
2034 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2035 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
2039 * a recalibrate from beyond cylinder 77
2040 * will "fail" due to the FDC limitations;
2041 * since people used to complain much about
2042 * the failure message, try not logging
2043 * this one if it seems to be the first
2046 printf("fd%d: recal failed ST0 %b cyl %d\n",
2047 fdu, st0, NE7_ST0BITS, cyl);
2048 if(fdc->retry < 3) fdc->retry = 3;
2049 return (retrier(fdc));
2052 /* Seek (probably) necessary */
2053 fdc->state = DOSEEK;
2054 return (1); /* will return immediatly */
2056 if(fd->flags & FD_MOTOR_WAIT)
2058 return (0); /* time's not up yet */
2060 if (fdc->flags & FDC_NEEDS_RESET) {
2061 fdc->state = RESETCTLR;
2062 fdc->flags &= ~FDC_NEEDS_RESET;
2065 * If all motors were off, then the controller was
2066 * reset, so it has lost track of the current
2067 * cylinder. Recalibrate to handle this case.
2068 * But first, discard the results of the reset.
2070 fdc->state = RESETCOMPLETE;
2072 return (1); /* will return immediatly */
2074 device_print_prettyname(fdc->fdc_dev);
2075 printf("unexpected FD int->");
2076 if (fd_read_status(fdc, fd->fdsu) == 0)
2077 printf("FDC status :%x %x %x %x %x %x %x ",
2086 printf("No status available ");
2087 if (fd_sense_int(fdc, &st0, &cyl) != 0)
2089 printf("[controller is dead now]\n");
2092 printf("ST0 = %x, PCN = %x\n", st0, cyl);
2095 /*XXX confusing: some branches return immediately, others end up here*/
2096 return (1); /* Come back immediatly to new state */
2100 retrier(struct fdc_data *fdc)
2102 register struct buf *bp;
2108 /* XXX shouldn't this be cached somewhere? */
2109 fdu = FDUNIT(minor(bp->b_dev));
2110 fd = devclass_get_softc(fd_devclass, fdu);
2111 if (fd->options & FDOPT_NORETRY)
2114 switch (fdc->retry) {
2115 case 0: case 1: case 2:
2116 fdc->state = SEEKCOMPLETE;
2118 case 3: case 4: case 5:
2119 fdc->state = STARTRECAL;
2122 fdc->state = RESETCTLR;
2129 dev_t sav_b_dev = bp->b_dev;
2131 bp->b_dev = makedev(major(bp->b_dev),
2132 (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
2133 diskerr(bp, "fd", "hard error", LOG_PRINTF,
2134 fdc->fd->skip / DEV_BSIZE,
2135 (struct disklabel *)NULL);
2136 bp->b_dev = sav_b_dev;
2137 if (fdc->flags & FDC_STAT_VALID)
2140 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2141 fdc->status[0], NE7_ST0BITS,
2142 fdc->status[1], NE7_ST1BITS,
2143 fdc->status[2], NE7_ST2BITS,
2144 fdc->status[3], fdc->status[4],
2148 printf(" (No status)\n");
2150 bp->b_flags |= B_ERROR;
2152 bp->b_resid += bp->b_bcount - fdc->fd->skip;
2155 /* Tell devstat we have finished with the transaction */
2156 devstat_end_transaction(&fdc->fd->device_stats,
2157 bp->b_bcount - bp->b_resid,
2159 (bp->b_flags & B_READ) ? DEVSTAT_READ :
2163 fdc->state = FINDWORK;
2164 fdc->flags |= FDC_NEEDS_RESET;
2174 fdformat(dev, finfo, p)
2176 struct fd_formb *finfo;
2186 fdu = FDUNIT(minor(dev));
2187 fd = devclass_get_softc(fd_devclass, fdu);
2188 fdblk = 128 << fd->ft->secsize;
2190 /* set up a buffer header for fdstrategy() */
2191 bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
2195 * keep the process from being swapped
2198 bzero((void *)bp, sizeof(struct buf));
2199 bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
2202 * calculate a fake blkno, so fdstrategy() would initiate a
2203 * seek to the requested cylinder
2205 bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
2206 + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
2208 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2209 bp->b_data = (caddr_t)finfo;
2211 /* now do the format */
2215 /* ...and wait for it to complete */
2217 while(!(bp->b_flags & B_DONE)) {
2218 rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
2219 if (rv == EWOULDBLOCK)
2224 if (rv == EWOULDBLOCK) {
2229 if (bp->b_flags & B_ERROR)
2232 * allow the process to be swapped
2240 * TODO: don't allocate buffer on stack.
2244 fdioctl(dev, cmd, addr, flag, p)
2251 fdu_t fdu = FDUNIT(minor(dev));
2252 fd_p fd = devclass_get_softc(fd_devclass, fdu);
2255 struct fd_type *fdt;
2256 struct disklabel *dl;
2257 char buffer[DEV_BSIZE];
2260 fdblk = 128 << fd->ft->secsize;
2264 bzero(buffer, sizeof (buffer));
2265 dl = (struct disklabel *)buffer;
2266 dl->d_secsize = fdblk;
2268 dl->d_secpercyl = fdt->size / fdt->tracks;
2269 dl->d_type = DTYPE_FLOPPY;
2271 if (readdisklabel(dkmodpart(dev, RAW_PART), fdstrategy, dl)
2277 *(struct disklabel *)addr = *dl;
2281 if ((flag & FWRITE) == 0)
2286 if ((flag & FWRITE) == 0)
2291 if ((flag & FWRITE) == 0) {
2296 dl = (struct disklabel *)addr;
2298 if ((error = setdisklabel((struct disklabel *)buffer, dl,
2302 error = writedisklabel(dev, fdstrategy,
2303 (struct disklabel *)buffer);
2306 if ((flag & FWRITE) == 0)
2307 error = EBADF; /* must be opened for writing */
2308 else if (((struct fd_formb *)addr)->format_version !=
2310 error = EINVAL; /* wrong version of formatting prog */
2312 error = fdformat(dev, (struct fd_formb *)addr, p);
2315 case FD_GTYPE: /* get drive type */
2316 *(struct fd_type *)addr = *fd->ft;
2319 case FD_STYPE: /* set drive type */
2320 /* this is considered harmful; only allow for superuser */
2323 *fd->ft = *(struct fd_type *)addr;
2326 case FD_GOPTS: /* get drive options */
2327 *(int *)addr = fd->options;
2330 case FD_SOPTS: /* set drive options */
2331 fd->options = *(int *)addr;
2341 static device_method_t fdc_methods[] = {
2342 /* Device interface */
2343 DEVMETHOD(device_probe, fdc_probe),
2344 DEVMETHOD(device_attach, fdc_attach),
2345 DEVMETHOD(device_detach, bus_generic_detach),
2346 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2347 DEVMETHOD(device_suspend, bus_generic_suspend),
2348 DEVMETHOD(device_resume, bus_generic_resume),
2351 DEVMETHOD(bus_print_child, fdc_print_child),
2352 /* Our children never use any other bus interface methods. */
2357 static driver_t fdc_driver = {
2360 sizeof(struct fdc_data)
2363 DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
2365 static device_method_t fd_methods[] = {
2366 /* Device interface */
2367 DEVMETHOD(device_probe, fd_probe),
2368 DEVMETHOD(device_attach, fd_attach),
2369 DEVMETHOD(device_detach, bus_generic_detach),
2370 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2371 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
2372 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
2377 static driver_t fd_driver = {
2380 sizeof(struct fd_data)
2383 DEV_DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, CDEV_MAJOR, BDEV_MAJOR,
2386 #endif /* NFDC > 0 */
2389 * Hello emacs, these are the
2392 * c-continued-statement-offset: 8
2393 * c-continued-brace-offset: 0
2394 * c-brace-offset: -8
2395 * c-brace-imaginary-offset: 0
2396 * c-argdecl-indent: 8
2397 * c-label-offset: -8
2398 * c++-hanging-braces: 1
2399 * c++-access-specifier-offset: -8
2400 * c++-empty-arglist-indent: 8
2401 * c++-friend-offset: 0