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.heep.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
58 #include <sys/param.h>
61 #include <sys/devicestat.h>
63 #include <sys/fcntl.h>
64 #include <sys/fdcio.h>
65 #include <sys/filio.h>
66 #include <sys/kernel.h>
68 #include <sys/malloc.h>
69 #include <sys/module.h>
70 #include <sys/mutex.h>
74 #include <sys/systm.h>
76 #include <machine/bus.h>
77 #include <machine/stdarg.h>
80 #include <isa/isavar.h>
81 #include <pc98/cbus/fdcreg.h>
82 #include <pc98/cbus/fdcvar.h>
83 #include <pc98/pc98/pc98_machdep.h>
85 #include <isa/isavar.h>
86 #include <isa/isareg.h>
87 #include <dev/fdc/fdcreg.h>
88 #include <dev/fdc/fdcvar.h>
92 #define FDBIO_FORMAT BIO_CMD2
94 /* configuration flags for fdc */
95 #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */
98 * Stop retrying after this many DMA overruns. Since each retry takes
99 * one revolution, with 300 rpm., 25 retries take approximately 5
100 * seconds which the read attempt will block in case the DMA overrun
103 #define FDC_DMAOV_MAX 25
106 * Timeout value for the PIO loops to wait until the FDC main status
107 * register matches our expectations (request for master, direction
108 * bit). This is supposed to be a number of microseconds, although
109 * timing might actually not be very accurate.
111 * Timeouts of 100 msec are believed to be required for some broken
114 #define FDSTS_TIMEOUT 100000
117 * Number of subdevices that can be used for different density types.
125 #define FDBIO_RDSECTID BIO_CMD1
128 * List of native drive densities. Order must match enum fd_drivetype
129 * in <sys/fdcio.h>. Upon attaching the drive, each of the
130 * programmable subdevices is initialized with the native density
134 static struct fd_type fd_native_types[] =
136 { 0 }, /* FDT_NONE */
137 { 0 }, /* FDT_360K */
138 { 15,2,0xFF,0x1B,80,2400,0,2,0x54,1,0,FL_MFM }, /* FDT_12M */
139 { 0 }, /* FDT_720K */
140 { 18,2,0xFF,0x1B,80,2880,2,2,0x54,1,0,FL_MFM }, /* FDT_144M */
141 { 0 }, /* FDT_288M */
144 static struct fd_type fd_searchlist_12m[] = {
145 { 15,2,0xFF,0x1B,80,2400,0,2,0x54,1,0,FL_MFM }, /* 1.2M */
147 { 10,2,0xFF,0x10,82,1640,1,2,0x30,1,0,FL_MFM }, /* 820K */
148 { 10,2,0xFF,0x10,80,1600,1,2,0x30,1,0,FL_MFM }, /* 800K */
150 { 9,2,0xFF,0x20,80,1440,1,2,0x50,1,0,FL_MFM }, /* 720K */
151 { 9,2,0xFF,0x20,40, 720,1,2,0x50,1,0,FL_MFM|FL_2STEP },/* 360K */
152 { 8,2,0xFF,0x2A,80,1280,1,2,0x50,1,0,FL_MFM }, /* 640K */
153 { 8,3,0xFF,0x35,77,1232,0,2,0x74,1,0,FL_MFM }, /* 1.23M 1024/sec */
155 { 8,3,0xFF,0x35,80,1280,0,2,0x74,1,0,FL_MFM }, /* 1.28M 1024/sec */
158 static struct fd_type fd_searchlist_144m[] = {
160 { 21,2,0xFF,0x04,82,3444,2,2,0x0C,2,0,FL_MFM }, /* 1.72M in 3mode */
161 { 18,2,0xFF,0x1B,82,2952,2,2,0x54,1,0,FL_MFM }, /* 1.48M in 3mode */
163 { 18,2,0xFF,0x1B,80,2880,2,2,0x54,1,0,FL_MFM }, /* 1.44M in 3mode */
164 { 15,2,0xFF,0x1B,80,2400,0,2,0x54,1,0,FL_MFM }, /* 1.2M */
166 { 10,2,0xFF,0x10,82,1640,1,2,0x30,1,0,FL_MFM }, /* 820K */
167 { 10,2,0xFF,0x10,80,1600,1,2,0x30,1,0,FL_MFM }, /* 800K */
169 { 9,2,0xFF,0x20,80,1440,1,2,0x50,1,0,FL_MFM }, /* 720K */
170 { 9,2,0xFF,0x20,40, 720,1,2,0x50,1,0,FL_MFM|FL_2STEP },/* 360K */
171 { 8,2,0xFF,0x2A,80,1280,1,2,0x50,1,0,FL_MFM }, /* 640K */
172 { 8,3,0xFF,0x35,77,1232,0,2,0x74,1,0,FL_MFM }, /* 1.23M 1024/sec */
174 { 8,3,0xFF,0x35,80,1280,0,2,0x74,1,0,FL_MFM }, /* 1.28M 1024/sec */
175 { 9,3,0xFF,0x35,82,1476,0,2,0x47,1,0,FL_MFM }, /* 1.48M 1024/sec 9sec */
176 { 10,3,0xFF,0x1B,82,1640,2,2,0x54,1,0,FL_MFM }, /* 1.64M in 3mode - Reserve */
180 static struct fd_type fd_native_types[] =
182 { 0 }, /* FDT_NONE */
183 { 9,2,0xFF,0x2A,40, 720,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_360K */
184 { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* FDT_12M */
185 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_720K */
186 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
187 #if 0 /* we currently don't handle 2.88 MB */
188 { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /*FDT_288M*/
190 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
195 * 360 KB 5.25" and 720 KB 3.5" drives don't have automatic density
196 * selection, they just start out with their native density (or lose).
197 * So 1.2 MB 5.25", 1.44 MB 3.5", and 2.88 MB 3.5" drives have their
198 * respective lists of densities to search for.
200 static struct fd_type fd_searchlist_12m[] = {
201 { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* 1.2M */
202 { 9,2,0xFF,0x23,40, 720,FDC_300KBPS,2,0x50,1,0,FL_MFM|FL_2STEP }, /* 360K */
203 { 9,2,0xFF,0x20,80,1440,FDC_300KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
206 static struct fd_type fd_searchlist_144m[] = {
207 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
208 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
211 /* We search for 1.44M first since this is the most common case. */
212 static struct fd_type fd_searchlist_288m[] = {
213 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
215 { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /* 2.88M */
217 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
221 #define MAX_SEC_SIZE (128 << 3)
222 #define MAX_CYLINDER 85 /* some people really stress their drives
226 devclass_t fdc_devclass;
229 * Per drive structure (softc).
232 struct fdc_data *fdc; /* pointer to controller structure */
233 int fdsu; /* this units number on this controller */
234 enum fd_drivetype type; /* drive type */
235 struct fd_type *ft; /* pointer to current type descriptor */
236 struct fd_type fts[NUMDENS]; /* type descriptors */
238 #define FD_OPEN 0x01 /* it's open */
239 #define FD_NONBLOCK 0x02 /* O_NONBLOCK set */
240 #define FD_ACTIVE 0x04 /* it's active */
241 #define FD_MOTOR 0x08 /* motor should be on */
242 #define FD_MOTOR_WAIT 0x10 /* motor coming up */
243 #define FD_UA 0x20 /* force unit attention */
246 #define FD_NO_TRACK -2
247 int track; /* where we think the head is */
248 int options; /* user configurable options, see fdcio.h */
249 struct callout_handle toffhandle;
250 struct callout_handle tohandle;
251 struct devstat *device_stats;
252 struct cdev *masterdev;
265 static devclass_t fd_devclass;
267 /* configuration flags for fd */
268 #define FD_TYPEMASK 0x0f /* drive type, matches enum
269 * fd_drivetype; on i386 machines, if
270 * given as 0, use RTC type for fd0
272 #define FD_DTYPE(flags) ((flags) & FD_TYPEMASK)
273 #define FD_NO_CHLINE 0x10 /* drive does not support changeline
274 * aka. unit attention */
275 #define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just
276 * assume it is there */
279 * Throughout this file the following conventions will be used:
281 * fd is a pointer to the fd_data struct for the drive in question
282 * fdc is a pointer to the fdc_data struct for the controller
283 * fdu is the floppy drive unit number
284 * fdcu is the floppy controller unit number
285 * fdsu is the floppy drive unit number on that controller. (sub-unit)
289 * Function declarations, same (chaotic) order as they appear in the
290 * file. Re-ordering is too late now, it would only obfuscate the
291 * diffs against old and offspring versions (like the PC98 one).
293 * Anyone adding functions here, please keep this sequence the same
294 * as below -- makes locating a particular function in the body much
297 static u_int8_t fdsts_rd(fdc_p);
298 static void fddata_wr(fdc_p, u_int8_t);
299 static u_int8_t fddata_rd(fdc_p);
300 static int fdc_err(struct fdc_data *, const char *);
301 static int enable_fifo(fdc_p fdc);
302 static int fd_sense_drive_status(fdc_p, int *);
303 static int fd_sense_int(fdc_p, int *, int *);
304 static int fd_read_status(fdc_p);
305 static int fd_probe(device_t);
306 static int fd_attach(device_t);
307 static int fd_detach(device_t);
308 static void set_motor(struct fdc_data *, int, int);
311 static timeout_t fd_turnoff;
312 static timeout_t fd_motor_on;
313 static void fd_turnon(struct fd_data *);
314 static void fdc_reset(fdc_p);
315 static int fd_in(struct fdc_data *, int *);
316 static int out_fdc(struct fdc_data *, int);
317 static d_open_t fdopen;
318 static d_close_t fdclose;
319 static d_strategy_t fdstrategy;
320 static void fdstart(struct fdc_data *);
321 static timeout_t fd_iotimeout;
322 static timeout_t fd_pseudointr;
323 static driver_intr_t fdc_intr;
324 static int fdcpio(fdc_p, long, caddr_t, u_int);
325 static int fdautoselect(struct cdev *);
326 static int fdstate(struct fdc_data *);
327 static int retrier(struct fdc_data *);
328 static void fdbiodone(struct bio *);
329 static int fdmisccmd(struct cdev *, u_int, void *);
330 static d_ioctl_t fdioctl;
332 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */
335 /* CAUTION: fd_debug causes huge amounts of logging output */
336 static int volatile fd_debug = 0;
337 #define TRACE0(arg) do { if (fd_debug) printf(arg); } while (0)
338 #define TRACE1(arg1, arg2) do { if (fd_debug) printf(arg1, arg2); } while (0)
339 #else /* FDC_DEBUG */
340 #define TRACE0(arg) do { } while (0)
341 #define TRACE1(arg1, arg2) do { } while (0)
342 #endif /* FDC_DEBUG */
345 * Bus space handling (access to low-level IO).
349 fdout_wr(fdc_p fdc, u_int8_t v)
351 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
358 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
362 fddata_wr(fdc_p fdc, u_int8_t v)
364 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
370 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
375 fdctl_wr(fdc_p fdc, u_int8_t v)
377 bus_space_write_1(fdc->portt, fdc->porth, FDCTL, v);
385 return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
389 static struct cdevsw fd_cdevsw = {
390 .d_version = D_VERSION,
394 .d_write = physwrite,
396 .d_strategy = fdstrategy,
398 .d_flags = D_DISK | D_NEEDGIANT,
402 * Auxiliary functions. Well, some only. Others are scattered
403 * throughout the entire file.
406 fdc_err(struct fdc_data *fdc, const char *s)
410 if (fdc->fdc_errs < FDC_ERRMAX)
411 device_printf(fdc->fdc_dev, "%s", s);
412 else if (fdc->fdc_errs == FDC_ERRMAX)
413 device_printf(fdc->fdc_dev, "too many errors, not "
414 "logging any more\n");
421 * fd_cmd: Send a command to the chip. Takes a varargs with this structure:
423 * # of output bytes, output bytes as ints ...,
424 * # of input bytes, input bytes as ints ...
427 fd_cmd(struct fdc_data *fdc, int n_out, ...)
435 cmd = (u_char)(va_arg(ap, int));
438 for (n = 0; n < n_out; n++)
440 if (out_fdc(fdc, va_arg(ap, int)) < 0)
443 snprintf(msg, sizeof(msg),
444 "cmd %x failed at out byte %d of %d\n",
446 return fdc_err(fdc, msg);
449 n_in = va_arg(ap, int);
450 for (n = 0; n < n_in; n++)
452 int *ptr = va_arg(ap, int *);
453 if (fd_in(fdc, ptr) < 0)
456 snprintf(msg, sizeof(msg),
457 "cmd %02x failed at in byte %d of %d\n",
459 return fdc_err(fdc, msg);
467 enable_fifo(fdc_p fdc)
471 if ((fdc->flags & FDC_HAS_FIFO) == 0) {
474 * Cannot use fd_cmd the normal way here, since
475 * this might be an invalid command. Thus we send the
476 * first byte, and check for an early turn of data directon.
479 if (out_fdc(fdc, I8207X_CONFIG) < 0)
480 return fdc_err(fdc, "Enable FIFO failed\n");
482 /* If command is invalid, return */
484 while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
485 != NE7_RQM && j-- > 0) {
486 if (i == (NE7_DIO | NE7_RQM)) {
494 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
496 return fdc_err(fdc, "Enable FIFO failed\n");
498 fdc->flags |= FDC_HAS_FIFO;
502 I8207X_CONFIG, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
503 return fdc_err(fdc, "Re-enable FIFO failed\n");
508 fd_sense_drive_status(fdc_p fdc, int *st3p)
512 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
514 return fdc_err(fdc, "Sense Drive Status failed\n");
523 fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
527 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
530 "sense intr err reading stat reg 0\n");
537 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
539 * There doesn't seem to have been an interrupt.
544 if (fd_in(fdc, &cyl) < 0) {
545 return fdc_err(fdc, "can't get cyl num\n");
556 fd_read_status(fdc_p fdc)
560 for (i = ret = 0; i < 7; i++) {
562 * XXX types are poorly chosen. Only bytes can be read
563 * from the hardware, but fdc->status[] wants u_ints and
564 * fd_in() gives ints.
568 ret = fd_in(fdc, &status);
569 fdc->status[i] = status;
575 fdc->flags |= FDC_STAT_VALID;
577 fdc->flags &= ~FDC_STAT_VALID;
583 static int pc98_trans = 0; /* 0 : HD , 1 : DD , 2 : 1.44 */
584 static int pc98_trans_prev = -1;
586 static void set_density(fdc_p fdc)
588 /* always motor on */
589 bus_space_write_1(fdc->sc_fdsiot, fdc->sc_fdsioh, 0,
590 (pc98_trans != 1 ? FDP_FDDEXC : 0) | FDP_PORTEXC);
592 fdctl_wr(fdc, FDC_RST | FDC_DMAE);
593 /* in the case of note W, always inhibit 100ms timer */
596 static int pc98_fd_check_ready(fdu_t fdu)
598 fd_p fd = devclass_get_softc(fd_devclass, fdu);
599 struct fdc_data *fdc = fd->fdc;
600 int retry = 0, status;
602 while (retry++ < 30000) {
603 set_motor(fdc, fd->fdsu, TURNON);
604 out_fdc(fdc, NE7CMD_SENSED); /* Sense Drive Status */
606 out_fdc(fdc, fdu); /* Drive number */
608 if ((fd_in(fdc, &status) == 0) && (status & NE7_ST3_RD)) {
609 fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
617 static void pc98_fd_check_type(struct fd_data *fd)
619 struct fdc_data *fdc;
621 if (fd->type != FDT_NONE || fd->fdu < 0 || fd->fdu > 3)
626 /* Look up what the BIOS thinks we have. */
627 if (!((PC98_SYSTEM_PARAMETER(0x55c) >> fd->fdu) & 0x01)) {
631 if ((PC98_SYSTEM_PARAMETER(0x5ae) >> fd->fdu) & 0x01) {
632 /* Check 3mode I/F */
634 bus_space_write_1(fdc->sc_fdemsiot, fdc->sc_fdemsioh, 0,
635 (fd->fdu << 5) | 0x10);
636 if (!(bus_space_read_1(fdc->sc_fdemsiot, fdc->sc_fdemsioh, 0) &
641 device_printf(fd->dev,
642 "Warning: can't control 3mode I/F, fallback to 2mode.\n");
650 fdc_release_resources(struct fdc_data *fdc)
656 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
657 fdc->fdc_intr = NULL;
659 if (fdc->res_irq != 0) {
660 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
665 if (fdc->res_ctl != 0) {
666 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
672 if (fdc->res_fdsio != 0) {
673 bus_release_resource(dev, SYS_RES_IOPORT, 3, fdc->res_fdsio);
674 fdc->res_fdsio = NULL;
676 if (fdc->res_fdemsio != 0) {
677 bus_release_resource(dev, SYS_RES_IOPORT, 4, fdc->res_fdemsio);
678 fdc->res_fdemsio = NULL;
681 if (fdc->res_ioport != 0) {
682 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
684 fdc->res_ioport = NULL;
686 if (fdc->res_drq != 0) {
687 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
694 * Configuration/initialization stuff, per controller.
698 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
700 struct fdc_ivars *ivars = device_get_ivars(child);
703 case FDC_IVAR_FDUNIT:
704 *result = ivars->fdunit;
706 case FDC_IVAR_FDTYPE:
707 *result = ivars->fdtype;
716 fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
718 struct fdc_ivars *ivars = device_get_ivars(child);
721 case FDC_IVAR_FDUNIT:
722 ivars->fdunit = value;
724 case FDC_IVAR_FDTYPE:
725 ivars->fdtype = value;
734 fdc_initial_reset(struct fdc_data *fdc)
737 /* see if it can handle a command */
738 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(4, 240),
739 NE7_SPEC_2(2, 0), 0))
742 /* First, reset the floppy controller. */
745 fdout_wr(fdc, FDO_FRST);
747 /* Then, see if it can handle a command. */
748 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
749 NE7_SPEC_2(2, 0), 0))
756 fdc_detach(device_t dev)
758 struct fdc_data *fdc;
761 fdc = device_get_softc(dev);
763 /* have our children detached first */
764 if ((error = bus_generic_detach(dev)))
768 /* reset controller, turn motor off */
771 /* reset controller, turn motor off */
775 fdc_release_resources(fdc);
780 * Add a child device to the fdc controller. It will then be probed etc.
783 fdc_add_child(device_t dev, const char *name, int unit)
785 struct fdc_ivars *ivar;
788 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
791 child = device_add_child(dev, name, unit);
793 free(ivar, M_DEVBUF);
796 device_set_ivars(child, ivar);
798 ivar->fdtype = FDT_NONE;
799 if (resource_disabled(name, unit))
800 device_disable(child);
805 fdc_attach(device_t dev)
807 struct fdc_data *fdc;
810 fdc = device_get_softc(dev);
812 error = bus_setup_intr(dev, fdc->res_irq,
813 INTR_TYPE_BIO | INTR_ENTROPY, NULL, fdc_intr, fdc,
816 device_printf(dev, "cannot setup interrupt\n");
819 fdc->fdcu = device_get_unit(dev);
820 fdc->flags |= FDC_NEEDS_RESET;
822 fdc->state = DEVIDLE;
825 /* reset controller, turn motor off, clear fdout mirror reg */
828 /* reset controller, turn motor off, clear fdout mirror reg */
829 fdout_wr(fdc, fdc->fdout = 0);
831 bioq_init(&fdc->head);
837 fdc_hints_probe(device_t dev)
839 const char *name, *dname;
843 * Probe and attach any children. We should probably detect
844 * devices from the BIOS unless overridden.
846 name = device_get_nameunit(dev);
848 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
849 resource_int_value(dname, dunit, "drive", &dunit);
850 fdc_add_child(dev, dname, dunit);
853 if ((error = bus_generic_attach(dev)) != 0)
859 fdc_print_child(device_t me, device_t child)
861 int retval = 0, flags;
863 retval += bus_print_child_header(me, child);
864 retval += printf(" on %s drive %d", device_get_nameunit(me),
865 fdc_get_fdunit(child));
866 if ((flags = device_get_flags(me)) != 0)
867 retval += printf(" flags %#x", flags);
868 retval += printf("\n");
874 * Configuration/initialization, per drive.
877 fd_probe(device_t dev)
884 struct fdc_data *fdc;
888 fdsu = fdc_get_fdunit(dev);
889 fd = device_get_softc(dev);
890 fdc = device_get_softc(device_get_parent(dev));
891 flags = device_get_flags(dev);
896 fd->fdu = device_get_unit(dev);
898 /* Auto-probe if fdinfo is present, but always allow override. */
899 type = FD_DTYPE(flags);
900 if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
904 /* make sure fdautoselect() will be called */
910 pc98_fd_check_type(fd);
913 * XXX I think using __i386__ is wrong here since we actually want to probe
914 * for the machine type, not the CPU type (so non-PC arch's like the PC98 will
918 if (fd->type == FDT_NONE && (fd->fdu == 0 || fd->fdu == 1)) {
919 /* Look up what the BIOS thinks we have. */
921 if ((fdc->flags & FDC_ISPCMCIA))
923 * Somewhat special. No need to force the
924 * user to set device flags, since the Y-E
925 * Data PCMCIA floppy is always a 1.44 MB
930 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
932 fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
934 if (fd->type == FDT_288M_1)
937 #endif /* __i386__ */
940 /* is there a unit? */
941 if (fd->type == FDT_NONE)
946 set_motor(fdc, fdsu, TURNON);
947 fdc_reset(fdc); /* XXX reset, then unreset, etc. */
948 DELAY(1000000); /* 1 sec */
950 if ((flags & FD_NO_PROBE) == 0) {
951 /* If we're at track 0 first seek inwards. */
952 if ((fd_sense_drive_status(fdc, &st3) == 0) &&
953 (st3 & NE7_ST3_T0)) {
954 /* Seek some steps... */
955 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
956 /* ...wait a moment... */
958 /* make ctrlr happy: */
959 fd_sense_int(fdc, 0, 0);
963 for (i = 0; i < 2; i++) {
965 * we must recalibrate twice, just in case the
966 * heads have been beyond cylinder 76, since
967 * most FDCs still barf when attempting to
968 * recalibrate more than 77 steps
971 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
972 /* a second being enough for full stroke seek*/
973 DELAY(i == 0 ? 1000000 : 300000);
975 /* anything responding? */
976 if (fd_sense_int(fdc, &st0, 0) == 0 &&
977 (st0 & NE7_ST0_EC) == 0)
978 break; /* already probed succesfully */
983 set_motor(fdc, fdsu, TURNOFF);
985 if ((flags & FD_NO_PROBE) == 0 &&
986 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
992 /* This doesn't work before the first reset. */
993 if ((fdc->flags & FDC_HAS_FIFO) == 0 &&
994 fdc->fdct == FDC_ENHANCED &&
995 (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 &&
996 enable_fifo(fdc) == 0) {
997 device_printf(device_get_parent(dev),
998 "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1005 device_set_desc(dev, "1.44M FDD");
1008 device_set_desc(dev, "1M/640K FDD");
1016 device_set_desc(dev, "1200-KB 5.25\" drive");
1019 device_set_desc(dev, "1440-KB 3.5\" drive");
1022 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1025 device_set_desc(dev, "360-KB 5.25\" drive");
1028 device_set_desc(dev, "720-KB 3.5\" drive");
1034 fd->track = FD_NO_TRACK;
1041 callout_handle_init(&fd->toffhandle);
1042 callout_handle_init(&fd->tohandle);
1044 /* initialize densities for subdevices */
1046 for (i = 0; i < NUMDENS; i++)
1047 memcpy(fd->fts + i, fd_searchlist_144m + i,
1048 sizeof(struct fd_type));
1050 for (i = 0; i < NUMDENS; i++)
1051 memcpy(fd->fts + i, fd_native_types + fd->type,
1052 sizeof(struct fd_type));
1058 fd_attach(device_t dev)
1062 fd = device_get_softc(dev);
1063 fd->masterdev = make_dev(&fd_cdevsw, fd->fdu,
1064 UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu);
1065 fd->masterdev->si_drv1 = fd;
1066 fd->device_stats = devstat_new_entry(device_get_name(dev),
1067 device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS,
1068 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
1069 DEVSTAT_PRIORITY_FD);
1074 fd_detach(device_t dev)
1078 fd = device_get_softc(dev);
1079 untimeout(fd_turnoff, fd, fd->toffhandle);
1080 devstat_remove_entry(fd->device_stats);
1081 destroy_dev(fd->masterdev);
1086 static device_method_t fd_methods[] = {
1087 /* Device interface */
1088 DEVMETHOD(device_probe, fd_probe),
1089 DEVMETHOD(device_attach, fd_attach),
1090 DEVMETHOD(device_detach, fd_detach),
1091 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1092 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
1093 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
1098 static driver_t fd_driver = {
1101 sizeof(struct fd_data)
1104 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
1107 * More auxiliary functions.
1110 * Motor control stuff.
1111 * Remember to not deselect the drive we're working on.
1114 set_motor(struct fdc_data *fdc, int fdsu, int turnon)
1117 bus_space_write_1(fdc->sc_fdsiot, fdc->sc_fdsioh, 0,
1118 (pc98_trans != 1 ? FDP_FDDEXC : 0) | FDP_PORTEXC);
1120 fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
1126 fdout &= ~FDO_FDSEL;
1127 fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu;
1129 fdout &= ~(FDO_MOEN0 << fdsu);
1131 fdout_wr(fdc, fdout);
1132 TRACE1("[0x%x->FDOUT]", fdout);
1137 fd_turnoff(void *xfd)
1142 TRACE1("[fd%d: turnoff]", fd->fdu);
1146 * Don't turn off the motor yet if the drive is active.
1148 * If we got here, this could only mean we missed an interrupt.
1149 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
1150 * after a controller reset. Just schedule a pseudo-interrupt
1151 * so the state machine gets re-entered.
1153 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
1159 fd->flags &= ~FD_MOTOR;
1160 set_motor(fd->fdc, fd->fdsu, TURNOFF);
1165 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 timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
1191 fdc_reset(fdc_p fdc)
1193 /* Try a reset, keep motor on */
1196 if (pc98_machine_type & M_EPSON_PC98)
1197 fdctl_wr(fdc, FDC_RST | FDC_RDY | FDC_DD | FDC_MTON);
1199 fdctl_wr(fdc, FDC_RST | FDC_RDY | FDC_DMAE | FDC_MTON);
1201 fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
1204 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1205 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1207 /* enable FDC, but defer interrupts a moment */
1208 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
1209 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
1211 fdout_wr(fdc, fdc->fdout);
1212 TRACE1("[0x%x->FDOUT]", fdc->fdout);
1215 /* XXX after a reset, silently believe the FDC will accept commands */
1217 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1218 NE7_SPEC_1(4, 240), NE7_SPEC_2(2, 0),
1221 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1222 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1225 if (fdc->flags & FDC_HAS_FIFO)
1226 (void) enable_fifo(fdc);
1230 * FDC IO functions, take care of the main status register, timeout
1231 * in case the desired status bits are never set.
1233 * These PIO loops initially start out with short delays between
1234 * each iteration in the expectation that the required condition
1235 * is usually met quickly, so it can be handled immediately. After
1236 * about 1 ms, stepping is increased to achieve a better timing
1237 * accuracy in the calls to DELAY().
1240 fd_in(struct fdc_data *fdc, int *ptr)
1244 for (j = 0, step = 1;
1245 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) &&
1249 return (fdc_err(fdc, "ready for output in input\n"));
1254 if (j >= FDSTS_TIMEOUT)
1255 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
1258 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1261 #else /* !FDC_DEBUG */
1266 #endif /* FDC_DEBUG */
1270 out_fdc(struct fdc_data *fdc, int x)
1274 for (j = 0, step = 1;
1275 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != NE7_RQM &&
1278 if (i == (NE7_DIO|NE7_RQM))
1279 return (fdc_err(fdc, "ready for input in output\n"));
1284 if (j >= FDSTS_TIMEOUT)
1285 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
1287 /* Send the command and return */
1289 TRACE1("[0x%x->FDDATA]", x);
1294 * Block device driver interface functions (interspersed with even more
1295 * auxiliary functions).
1298 fdopen(struct cdev *dev, int flags, int mode, struct thread *td)
1305 int rv, unitattn, dflags;
1311 if ((fdc == NULL) || (fd->type == FDT_NONE))
1316 dflags = device_get_flags(fd->dev);
1318 * This is a bit bogus. It's still possible that e. g. a
1319 * descriptor gets inherited to a child, but then it's at
1320 * least for the same subdevice. By checking FD_OPEN here, we
1321 * can ensure that a device isn't attempted to be opened with
1322 * different densities at the same time where the second open
1323 * could clobber the settings from the first one.
1325 if (fd->flags & FD_OPEN)
1329 if (pc98_fd_check_ready(fdu) == -1)
1333 if (flags & FNONBLOCK) {
1335 * Unfortunately, physio(9) discards its ioflag
1336 * argument, thus preventing us from seeing the
1337 * O_NONBLOCK bit. So we need to keep track
1340 fd->flags |= FD_NONBLOCK;
1344 * Figure out a unit attention condition.
1346 * If UA has been forced, proceed.
1348 * If the drive has no changeline support,
1349 * or if the drive parameters have been lost
1350 * due to previous non-blocking access,
1351 * assume a forced UA condition.
1353 * If motor is off, turn it on for a moment
1354 * and select our drive, in order to read the
1355 * UA hardware signal.
1357 * If motor is on, and our drive is currently
1358 * selected, just read the hardware bit.
1360 * If motor is on, but active for another
1361 * drive on that controller, we are lost. We
1362 * cannot risk to deselect the other drive, so
1363 * we just assume a forced UA condition to be
1367 if ((dflags & FD_NO_CHLINE) != 0 ||
1368 (fd->flags & FD_UA) != 0 ||
1371 fd->flags &= ~FD_UA;
1373 } else if (fdc->fdout & (FDO_MOEN0 | FDO_MOEN1 |
1374 FDO_MOEN2 | FDO_MOEN3)) {
1375 if ((fdc->fdout & FDO_FDSEL) == fd->fdsu)
1376 unitattn = fdin_rd(fdc) & FDI_DCHG;
1380 set_motor(fdc, fd->fdsu, TURNON);
1381 unitattn = fdin_rd(fdc) & FDI_DCHG;
1382 set_motor(fdc, fd->fdsu, TURNOFF);
1385 if (unitattn && (rv = fdautoselect(dev)) != 0)
1388 fd->flags |= FD_OPEN;
1390 if ((fdc->flags & FDC_NODMA) == 0) {
1391 if (fdc->dmacnt++ == 0) {
1392 isa_dma_acquire(fdc->dmachan);
1393 isa_dmainit(fdc->dmachan, MAX_SEC_SIZE);
1398 * Clearing the DMA overrun counter at open time is a bit messy.
1399 * Since we're only managing one counter per controller, opening
1400 * the second drive could mess it up. Anyway, if the DMA overrun
1401 * condition is really persistent, it will eventually time out
1402 * still. OTOH, clearing it here will ensure we'll at least start
1403 * trying again after a previous (maybe even long ago) failure.
1404 * Also, this is merely a stop-gap measure only that should not
1405 * happen during normal operation, so we can tolerate it to be a
1406 * bit sloppy about this.
1408 fdc->dma_overruns = 0;
1414 fdclose(struct cdev *dev, int flags, int mode, struct thread *td)
1421 fd->flags &= ~(FD_OPEN | FD_NONBLOCK);
1422 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
1424 if ((fdc->flags & FDC_NODMA) == 0)
1425 if (--fdc->dmacnt == 0)
1426 isa_dma_release(fdc->dmachan);
1432 fdstrategy(struct bio *bp)
1434 long blknum, nblocks;
1441 fd = bp->bio_dev->si_drv1;
1444 bp->bio_resid = bp->bio_bcount;
1445 if (fd->type == FDT_NONE || fd->ft == 0) {
1446 if (fd->type != FDT_NONE && (fd->flags & FD_NONBLOCK))
1447 bp->bio_error = EAGAIN;
1449 bp->bio_error = ENXIO;
1450 bp->bio_flags |= BIO_ERROR;
1453 fdblk = 128 << (fd->ft->secsize);
1454 if (bp->bio_cmd != FDBIO_FORMAT && bp->bio_cmd != FDBIO_RDSECTID) {
1455 if (fd->flags & FD_NONBLOCK) {
1456 bp->bio_error = EAGAIN;
1457 bp->bio_flags |= BIO_ERROR;
1460 if (bp->bio_offset < 0) {
1462 "fd%d: fdstrat: bad request offset = %ju, bcount = %ld\n",
1463 fdu, (intmax_t)bp->bio_offset, bp->bio_bcount);
1464 bp->bio_error = EINVAL;
1465 bp->bio_flags |= BIO_ERROR;
1468 if ((bp->bio_bcount % fdblk) != 0) {
1469 bp->bio_error = EINVAL;
1470 bp->bio_flags |= BIO_ERROR;
1476 * Set up block calculations.
1479 if (bp->bio_offset >= ((off_t)128 << fd->ft->secsize) * fd->ft->size) {
1480 bp->bio_error = EINVAL;
1481 bp->bio_flags |= BIO_ERROR;
1485 blknum = bp->bio_offset / fdblk;
1486 nblocks = fd->ft->size;
1487 if (blknum + bp->bio_bcount / fdblk > nblocks) {
1488 if (blknum >= nblocks) {
1489 if (bp->bio_cmd != BIO_READ) {
1490 bp->bio_error = ENOSPC;
1491 bp->bio_flags |= BIO_ERROR;
1493 goto bad; /* not always bad, but EOF */
1495 bp->bio_bcount = (nblocks - blknum) * fdblk;
1497 bp->bio_pblkno = blknum;
1499 bioq_disksort(&fdc->head, bp);
1500 untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1501 devstat_start_transaction_bio(fd->device_stats, bp);
1502 device_busy(fd->dev);
1514 * We have just queued something. If the controller is not busy
1515 * then simulate the case where it has just finished a command
1516 * So that it (the interrupt routine) looks on the queue for more
1517 * work to do and picks up what we just added.
1519 * If the controller is already busy, we need do nothing, as it
1520 * will pick up our work when the present work completes.
1523 fdstart(struct fdc_data *fdc)
1528 if(fdc->state == DEVIDLE)
1536 fd_iotimeout(void *xfdc)
1542 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
1545 * Due to IBM's brain-dead design, the FDC has a faked ready
1546 * signal, hardwired to ready == true. Thus, any command
1547 * issued if there's no diskette in the drive will _never_
1548 * complete, and must be aborted by resetting the FDC.
1549 * Many thanks, Big Blue!
1550 * The FDC must not be reset directly, since that would
1551 * interfere with the state machine. Instead, pretend that
1552 * the command completed but was invalid. The state machine
1553 * will reset the FDC and retry once.
1556 fdc->status[0] = NE7_ST0_IC_IV;
1557 fdc->flags &= ~FDC_STAT_VALID;
1558 fdc->state = IOTIMEDOUT;
1563 /* Just ensure it has the right spl. */
1565 fd_pseudointr(void *xfdc)
1577 * Keep calling the state machine until it returns a 0.
1578 * Always called at splbio.
1581 fdc_intr(void *xfdc)
1589 * Magic pseudo-DMA initialization for YE FDC. Sets count and
1592 #define SET_BCDR(fdc,wr,cnt,port) \
1593 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \
1594 ((cnt)-1) & 0xff); \
1595 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
1596 ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
1599 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy.
1602 fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
1604 u_char *cptr = (u_char *)addr;
1606 if (flags == BIO_READ) {
1607 if (fdc->state != PIOREAD) {
1608 fdc->state = PIOREAD;
1611 SET_BCDR(fdc, 0, count, 0);
1612 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1613 FDC_YE_DATAPORT, cptr, count);
1615 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1616 FDC_YE_DATAPORT, cptr, count);
1617 SET_BCDR(fdc, 0, count, 0);
1623 * Try figuring out the density of the media present in our device.
1626 fdautoselect(struct cdev *dev)
1629 struct fd_type *fdtp;
1630 struct fdc_readid id;
1631 int i, n, oopts, rv;
1642 /* no autoselection on those drives */
1643 fd->ft = fd_native_types + fd->type;
1648 fdtp = fd_searchlist_12m;
1649 n = sizeof fd_searchlist_12m / sizeof(struct fd_type);
1653 fdtp = fd_searchlist_144m;
1654 n = sizeof fd_searchlist_144m / sizeof(struct fd_type);
1659 fdtp = fd_searchlist_288m;
1660 n = sizeof fd_searchlist_288m / sizeof(struct fd_type);
1666 * Try reading sector ID fields, first at cylinder 0, head 0,
1667 * then at cylinder 2, head N. We don't probe cylinder 1,
1668 * since for 5.25in DD media in a HD drive, there are no data
1669 * to read (2 step pulses per media cylinder required). For
1670 * two-sided media, the second probe always goes to head 1, so
1671 * we can tell them apart from single-sided media. As a
1672 * side-effect this means that single-sided media should be
1673 * mentioned in the search list after two-sided media of an
1674 * otherwise identical density. Media with a different number
1675 * of sectors per track but otherwise identical parameters
1676 * cannot be distinguished at all.
1678 * If we successfully read an ID field on both cylinders where
1679 * the recorded values match our expectation, we are done.
1680 * Otherwise, we try the next density entry from the table.
1682 * Stepping to cylinder 2 has the side-effect of clearing the
1683 * unit attention bit.
1685 oopts = fd->options;
1686 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1687 for (i = 0; i < n; i++, fdtp++) {
1690 id.cyl = id.head = 0;
1691 rv = fdmisccmd(dev, FDBIO_RDSECTID, &id);
1694 if (id.cyl != 0 || id.head != 0 ||
1695 id.secshift != fdtp->secsize)
1698 id.head = fd->ft->heads - 1;
1699 rv = fdmisccmd(dev, FDBIO_RDSECTID, &id);
1700 if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
1701 id.secshift != fdtp->secsize)
1707 fd->options = oopts;
1710 device_printf(fd->dev, "autoselection failed\n");
1715 device_printf(fd->dev, "autoselected %d KB medium\n",
1717 (128 << (fd->ft->secsize)) *
1718 fd->ft->size / 1024);
1728 * The controller state machine.
1730 * If it returns a non zero value, it should be called again immediately.
1735 struct fdc_readid *idp;
1736 int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac;
1737 int st0, cyl, st3, idf, ne7cmd, mfm, steptrac;
1738 unsigned long blknum;
1739 fdu_t fdu = fdc->fdu;
1741 register struct bio *bp;
1742 struct fd_formb *finfo = NULL;
1747 bp = bioq_takefirst(&fdc->head);
1753 * Nothing left for this controller to do,
1754 * force into the IDLE state.
1756 fdc->state = DEVIDLE;
1758 device_printf(fdc->fdc_dev,
1759 "unexpected valid fd pointer\n");
1763 TRACE1("[fdc%d IDLE]", fdc->fdcu);
1766 fd = bp->bio_dev->si_drv1;
1768 fdblk = 128 << fd->ft->secsize;
1769 if (fdc->fd && (fd != fdc->fd))
1770 device_printf(fd->dev, "confused fd pointers\n");
1771 read = bp->bio_cmd == BIO_READ;
1772 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
1773 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
1778 format = bp->bio_cmd == FDBIO_FORMAT;
1779 rdsectid = bp->bio_cmd == FDBIO_RDSECTID;
1781 finfo = (struct fd_formb *)bp->bio_data;
1782 TRACE1("fd%d", fdu);
1783 TRACE1("[%s]", fdstates[fdc->state]);
1784 TRACE1("(0x%x)", fd->flags);
1785 untimeout(fd_turnoff, fd, fd->toffhandle);
1786 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
1790 case FINDWORK: /* we have found new work */
1796 pc98_trans = fd->ft->trans;
1797 if (pc98_trans_prev != pc98_trans) {
1800 for (i = 0; i < 10; i++) {
1804 pc98_trans_prev = pc98_trans;
1806 if (pc98_trans != fd->pc98_trans) {
1807 if (fd->type == FDT_144M) {
1808 bus_space_write_1(fdc->sc_fdemsiot,
1816 fd->pc98_trans = pc98_trans;
1819 fdc->fdctl_wr(fdc, fd->ft->trans);
1821 TRACE1("[0x%x->FDCTL]", fd->ft->trans);
1823 * If the next drive has a motor startup pending, then
1824 * it will start up in its own good time.
1826 if(fd->flags & FD_MOTOR_WAIT) {
1827 fdc->state = MOTORWAIT;
1828 return (0); /* will return later */
1831 * Maybe if it's not starting, it SHOULD be starting.
1833 if (!(fd->flags & FD_MOTOR))
1835 fdc->state = MOTORWAIT;
1837 return (0); /* will return later */
1839 else /* at least make sure we are selected */
1841 set_motor(fdc, fd->fdsu, TURNON);
1843 if (fdc->flags & FDC_NEEDS_RESET) {
1844 fdc->state = RESETCTLR;
1845 fdc->flags &= ~FDC_NEEDS_RESET;
1847 fdc->state = DOSEEK;
1848 return (1); /* will return immediately */
1851 blknum = bp->bio_pblkno + fd->skip / fdblk;
1852 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1853 if (cylinder == fd->track)
1855 fdc->state = SEEKCOMPLETE;
1856 return (1); /* will return immediately */
1859 pc98_fd_check_ready(fdu);
1861 if (fd_cmd(fdc, 3, NE7CMD_SEEK,
1862 fd->fdsu, cylinder * steptrac, 0))
1865 * Seek command not accepted, looks like
1866 * the FDC went off to the Saints...
1868 fdc->retry = 6; /* try a reset */
1869 return(retrier(fdc));
1871 fd->track = FD_NO_TRACK;
1872 fdc->state = SEEKWAIT;
1873 return(0); /* will return later */
1876 /* allow heads to settle */
1877 timeout(fd_pseudointr, fdc, hz / 16);
1878 fdc->state = SEEKCOMPLETE;
1879 return(0); /* will return later */
1881 case SEEKCOMPLETE : /* seek done, start DMA */
1882 blknum = bp->bio_pblkno + fd->skip / fdblk;
1883 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1885 /* Make sure seek really happened. */
1886 if(fd->track == FD_NO_TRACK) {
1887 int descyl = cylinder * steptrac;
1890 * This might be a "ready changed" interrupt,
1891 * which cannot really happen since the
1892 * RDY pin is hardwired to + 5 volts. This
1893 * generally indicates a "bouncing" intr
1894 * line, so do one of the following:
1896 * When running on an enhanced FDC that is
1897 * known to not go stuck after responding
1898 * with INVALID, fetch all interrupt states
1899 * until seeing either an INVALID or a
1900 * real interrupt condition.
1902 * When running on a dumb old NE765, give
1903 * up immediately. The controller will
1904 * provide up to four dummy RC interrupt
1905 * conditions right after reset (for the
1906 * corresponding four drives), so this is
1907 * our only chance to get notice that it
1908 * was not the FDC that caused the interrupt.
1910 if (fd_sense_int(fdc, &st0, &cyl)
1912 return (0); /* will return later */
1913 if(fdc->fdct == FDC_NE765
1914 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1915 return (0); /* hope for a real intr */
1916 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1921 * seek to cyl 0 requested; make sure we are
1924 if (fd_sense_drive_status(fdc, &st3))
1926 if ((st3 & NE7_ST3_T0) == 0) {
1928 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
1929 fdu, st3, NE7_ST3BITS);
1936 return (retrier(fdc));
1940 if (cyl != descyl) {
1942 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
1943 fdu, descyl, cyl, st0);
1946 return (retrier(fdc));
1950 fd->track = cylinder;
1952 fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1954 if (!rdsectid && !(fdc->flags & FDC_NODMA))
1955 isa_dmastart(idf, bp->bio_data+fd->skip,
1956 format ? bp->bio_bcount : fdblk, fdc->dmachan);
1957 blknum = bp->bio_pblkno + fd->skip / fdblk;
1958 sectrac = fd->ft->sectrac;
1959 sec = blknum % (sectrac * fd->ft->heads);
1960 head = sec / sectrac;
1961 sec = sec % sectrac + 1;
1962 if (head != 0 && fd->ft->offset_side2 != 0)
1963 sec += fd->ft->offset_side2;
1964 fd->hddrv = ((head&1)<<2)+fdu;
1966 if(format || !(read || rdsectid))
1968 /* make sure the drive is writable */
1969 if(fd_sense_drive_status(fdc, &st3) != 0)
1971 /* stuck controller? */
1972 if (!(fdc->flags & FDC_NODMA))
1974 bp->bio_data + fd->skip,
1975 format ? bp->bio_bcount : fdblk,
1977 fdc->retry = 6; /* reset the beast */
1978 return (retrier(fdc));
1980 if(st3 & NE7_ST3_WP)
1983 * XXX YES! this is ugly.
1984 * in order to force the current operation
1985 * to fail, we will have to fake an FDC
1986 * error - all error handling is done
1989 fdc->status[0] = NE7_ST0_IC_AT;
1990 fdc->status[1] = NE7_ST1_NW;
1992 fdc->status[3] = fd->track;
1993 fdc->status[4] = head;
1994 fdc->status[5] = sec;
1995 fdc->retry = 8; /* break out immediately */
1996 fdc->state = IOTIMEDOUT; /* not really... */
1997 return (1); /* will return immediately */
2002 ne7cmd = NE7CMD_FORMAT | mfm;
2003 if (fdc->flags & FDC_NODMA) {
2005 * This seems to be necessary for
2006 * whatever obscure reason; if we omit
2007 * it, we end up filling the sector ID
2008 * fields of the newly formatted track
2009 * entirely with garbage, causing
2010 * `wrong cylinder' errors all over
2011 * the place when trying to read them
2016 SET_BCDR(fdc, 1, bp->bio_bcount, 0);
2018 (void)fdcpio(fdc,bp->bio_cmd,
2019 bp->bio_data+fd->skip,
2024 if(fd_cmd(fdc, 6, ne7cmd, head << 2 | fdu,
2025 finfo->fd_formb_secshift,
2026 finfo->fd_formb_nsecs,
2027 finfo->fd_formb_gaplen,
2028 finfo->fd_formb_fillbyte, 0)) {
2029 /* controller fell over */
2030 if (!(fdc->flags & FDC_NODMA))
2032 bp->bio_data + fd->skip,
2033 format ? bp->bio_bcount : fdblk,
2036 return (retrier(fdc));
2038 } else if (rdsectid) {
2039 ne7cmd = NE7CMD_READID | mfm;
2040 if (fd_cmd(fdc, 2, ne7cmd, head << 2 | fdu, 0)) {
2041 /* controller jamming */
2043 return (retrier(fdc));
2046 /* read or write operation */
2047 ne7cmd = (read ? NE7CMD_READ | NE7CMD_SK : NE7CMD_WRITE) | mfm;
2048 if (fdc->flags & FDC_NODMA) {
2050 * This seems to be necessary even when
2053 SET_BCDR(fdc, 1, fdblk, 0);
2056 * Perform the write pseudo-DMA before
2057 * the WRITE command is sent.
2060 (void)fdcpio(fdc,bp->bio_cmd,
2061 bp->bio_data+fd->skip,
2066 head << 2 | fdu, /* head & unit */
2067 fd->track, /* track */
2069 sec, /* sector + 1 */
2070 fd->ft->secsize, /* sector size */
2071 sectrac, /* sectors/track */
2072 fd->ft->gap, /* gap size */
2073 fd->ft->datalen, /* data length */
2075 /* the beast is sleeping again */
2076 if (!(fdc->flags & FDC_NODMA))
2078 bp->bio_data + fd->skip,
2079 format ? bp->bio_bcount : fdblk,
2082 return (retrier(fdc));
2085 if (!rdsectid && (fdc->flags & FDC_NODMA))
2087 * If this is a read, then simply await interrupt
2088 * before performing PIO.
2090 if (read && !fdcpio(fdc,bp->bio_cmd,
2091 bp->bio_data+fd->skip,fdblk)) {
2092 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
2093 return(0); /* will return later */
2097 * Write (or format) operation will fall through and
2098 * await completion interrupt.
2100 fdc->state = IOCOMPLETE;
2101 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
2102 return (0); /* will return later */
2106 * Actually perform the PIO read. The IOCOMPLETE case
2107 * removes the timeout for us.
2109 (void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk);
2110 fdc->state = IOCOMPLETE;
2112 case IOCOMPLETE: /* IO done, post-analyze */
2113 untimeout(fd_iotimeout, fdc, fd->tohandle);
2115 if (fd_read_status(fdc)) {
2116 if (!rdsectid && !(fdc->flags & FDC_NODMA))
2117 isa_dmadone(idf, bp->bio_data + fd->skip,
2118 format ? bp->bio_bcount : fdblk,
2121 fdc->retry = 6; /* force a reset */
2122 return (retrier(fdc));
2125 fdc->state = IOTIMEDOUT;
2129 if (!rdsectid && !(fdc->flags & FDC_NODMA))
2130 isa_dmadone(idf, bp->bio_data + fd->skip,
2131 format ? bp->bio_bcount : fdblk, fdc->dmachan);
2132 if (fdc->status[0] & NE7_ST0_IC) {
2133 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2134 && fdc->status[1] & NE7_ST1_OR) {
2136 * DMA overrun. Someone hogged the bus and
2137 * didn't release it in time for the next
2140 * We normally restart this without bumping
2141 * the retry counter. However, in case
2142 * something is seriously messed up (like
2143 * broken hardware), we rather limit the
2144 * number of retries so the IO operation
2145 * doesn't block indefinately.
2147 if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
2148 fdc->state = SEEKCOMPLETE;
2149 return (1);/* will return immediately */
2150 } /* else fall through */
2152 if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
2154 fdc->retry = 6; /* force a reset */
2155 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2156 && fdc->status[2] & NE7_ST2_WC
2158 fdc->retry = 3; /* force recalibrate */
2159 return (retrier(fdc));
2163 /* copy out ID field contents */
2164 idp = (struct fdc_readid *)bp->bio_data;
2165 idp->cyl = fdc->status[3];
2166 idp->head = fdc->status[4];
2167 idp->sec = fdc->status[5];
2168 idp->secshift = fdc->status[6];
2170 /* Operation successful, retry DMA overruns again next time. */
2171 fdc->dma_overruns = 0;
2173 if (!rdsectid && !format && fd->skip < bp->bio_bcount) {
2174 /* set up next transfer */
2175 fdc->state = DOSEEK;
2181 device_unbusy(fd->dev);
2182 biofinish(bp, fd->device_stats, 0);
2185 fdc->state = FINDWORK;
2187 return (1); /* will return immediately */
2192 fdc->state = RESETCOMPLETE;
2193 return (0); /* will return later */
2197 * Discard all the results from the reset so that they
2198 * can't cause an unexpected interrupt later.
2200 for (i = 0; i < 4; i++)
2201 (void)fd_sense_int(fdc, &st0, &cyl);
2202 fdc->state = STARTRECAL;
2206 pc98_fd_check_ready(fdu);
2208 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2211 return (retrier(fdc));
2213 fdc->state = RECALWAIT;
2214 return (0); /* will return later */
2217 /* allow heads to settle */
2218 timeout(fd_pseudointr, fdc, hz / 8);
2219 fdc->state = RECALCOMPLETE;
2220 return (0); /* will return later */
2225 * See SEEKCOMPLETE for a comment on this:
2227 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2228 return (0); /* will return later */
2229 if(fdc->fdct == FDC_NE765
2230 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2231 return (0); /* hope for a real intr */
2232 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2233 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
2237 * A recalibrate from beyond cylinder 77
2238 * will "fail" due to the FDC limitations;
2239 * since people used to complain much about
2240 * the failure message, try not logging
2241 * this one if it seems to be the first
2244 printf("fd%d: recal failed ST0 %b cyl %d\n",
2245 fdu, st0, NE7_ST0BITS, cyl);
2246 if(fdc->retry < 3) fdc->retry = 3;
2247 return (retrier(fdc));
2250 /* Seek (probably) necessary */
2251 fdc->state = DOSEEK;
2252 return (1); /* will return immediately */
2255 if(fd->flags & FD_MOTOR_WAIT)
2257 return (0); /* time's not up yet */
2259 if (fdc->flags & FDC_NEEDS_RESET) {
2260 fdc->state = RESETCTLR;
2261 fdc->flags &= ~FDC_NEEDS_RESET;
2263 fdc->state = DOSEEK;
2264 return (1); /* will return immediately */
2267 device_printf(fdc->fdc_dev, "unexpected FD int->");
2268 if (fd_read_status(fdc) == 0)
2269 printf("FDC status :%x %x %x %x %x %x %x ",
2278 printf("No status available ");
2279 if (fd_sense_int(fdc, &st0, &cyl) != 0)
2281 printf("[controller is dead now]\n");
2282 return (0); /* will return later */
2284 printf("ST0 = %x, PCN = %x\n", st0, cyl);
2285 return (0); /* will return later */
2287 /* noone should ever get here */
2291 retrier(struct fdc_data *fdc)
2299 /* XXX shouldn't this be cached somewhere? */
2300 fd = bp->bio_dev->si_drv1;
2302 if (fd->options & FDOPT_NORETRY)
2305 switch (fdc->retry) {
2306 case 0: case 1: case 2:
2307 fdc->state = SEEKCOMPLETE;
2309 case 3: case 4: case 5:
2310 fdc->state = STARTRECAL;
2313 fdc->state = RESETCTLR;
2319 if ((fd->options & FDOPT_NOERRLOG) == 0) {
2320 disk_err(bp, "hard error",
2321 fdc->fd->skip / DEV_BSIZE, 0);
2322 if (fdc->flags & FDC_STAT_VALID) {
2324 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2325 fdc->status[0], NE7_ST0BITS,
2326 fdc->status[1], NE7_ST1BITS,
2327 fdc->status[2], NE7_ST2BITS,
2328 fdc->status[3], fdc->status[4],
2332 printf(" (No status)\n");
2334 if ((fd->options & FDOPT_NOERROR) == 0) {
2335 bp->bio_flags |= BIO_ERROR;
2336 bp->bio_error = EIO;
2337 bp->bio_resid = bp->bio_bcount - fdc->fd->skip;
2342 device_unbusy(fd->dev);
2343 biofinish(bp, fdc->fd->device_stats, 0);
2344 fdc->state = FINDWORK;
2345 fdc->flags |= FDC_NEEDS_RESET;
2355 fdbiodone(struct bio *bp)
2361 fdmisccmd(struct cdev *dev, u_int cmd, void *data)
2366 struct fd_formb *finfo;
2367 struct fdc_readid *idfield;
2373 fdblk = 128 << fd->ft->secsize;
2374 finfo = (struct fd_formb *)data;
2375 idfield = (struct fdc_readid *)data;
2377 bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
2380 * Set up a bio request for fdstrategy(). bio_offset is faked
2381 * so that fdstrategy() will seek to the requested
2382 * cylinder, and use the desired head.
2385 if (cmd == FDBIO_FORMAT) {
2387 (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) +
2388 finfo->head * fd->ft->sectrac) * fdblk;
2389 bp->bio_bcount = sizeof(struct fd_idfield_data) *
2390 finfo->fd_formb_nsecs;
2391 } else if (cmd == FDBIO_RDSECTID) {
2393 (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) +
2394 idfield->head * fd->ft->sectrac) * fdblk;
2395 bp->bio_bcount = sizeof(struct fdc_readid);
2397 panic("wrong cmd in fdmisccmd()");
2398 bp->bio_data = data;
2400 bp->bio_done = fdbiodone;
2403 /* Now run the command. */
2405 error = biowait(bp, "fdcmd");
2412 fdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
2416 struct fdc_status *fsp;
2417 struct fdc_readid *rid;
2424 pc98_fd_check_ready(fdu);
2428 * First, handle everything that could be done with
2429 * FD_NONBLOCK still being set.
2433 case DIOCGMEDIASIZE:
2435 return ((fd->flags & FD_NONBLOCK) ? EAGAIN : ENXIO);
2436 *(off_t *)addr = (128 << (fd->ft->secsize)) * fd->ft->size;
2439 case DIOCGSECTORSIZE:
2441 return ((fd->flags & FD_NONBLOCK) ? EAGAIN : ENXIO);
2442 *(u_int *)addr = 128 << (fd->ft->secsize);
2446 if (*(int *)addr != 0)
2447 fd->flags |= FD_NONBLOCK;
2451 * No drive type has been selected yet,
2452 * cannot turn FNONBLOCK off.
2456 fd->flags &= ~FD_NONBLOCK;
2461 /* keep the generic fcntl() code happy */
2464 case FD_GTYPE: /* get drive type */
2466 /* no type known yet, return the native type */
2467 *(struct fd_type *)addr = fd_native_types[fd->type];
2469 *(struct fd_type *)addr = *fd->ft;
2472 case FD_STYPE: /* set drive type */
2474 * Allow setting drive type temporarily iff
2475 * currently unset. Used for fdformat so any
2476 * user can set it, and then start formatting.
2479 return (EINVAL); /* already set */
2480 fd->fts[0] = *(struct fd_type *)addr;
2481 fd->ft = &fd->fts[0];
2485 case FD_GOPTS: /* get drive options */
2486 *(int *)addr = fd->options + FDOPT_AUTOSEL;
2489 case FD_SOPTS: /* set drive options */
2490 fd->options = *(int *)addr & ~FDOPT_AUTOSEL;
2495 if ((fd_debug != 0) != (*(int *)addr != 0)) {
2496 fd_debug = (*(int *)addr != 0);
2497 printf("fd%d: debugging turned %s\n",
2498 fd->fdu, fd_debug ? "on" : "off");
2504 if (priv_check(td, PRIV_DRIVER) != 0)
2506 fd->fdc->fdc_errs = 0;
2510 fsp = (struct fdc_status *)addr;
2511 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2513 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
2517 *(enum fd_drivetype *)addr = fd->type;
2522 * Now handle everything else. Make sure we have a valid
2525 if (fd->flags & FD_NONBLOCK)
2534 if ((flag & FWRITE) == 0)
2535 return (EBADF); /* must be opened for writing */
2536 if (((struct fd_formb *)addr)->format_version !=
2538 return (EINVAL); /* wrong version of formatting prog */
2539 error = fdmisccmd(dev, FDBIO_FORMAT, addr);
2542 case FD_GTYPE: /* get drive type */
2543 *(struct fd_type *)addr = *fd->ft;
2546 case FD_STYPE: /* set drive type */
2547 /* this is considered harmful; only allow for superuser */
2548 if (priv_check(td, PRIV_DRIVER) != 0)
2550 *fd->ft = *(struct fd_type *)addr;
2553 case FD_GOPTS: /* get drive options */
2554 *(int *)addr = fd->options;
2557 case FD_SOPTS: /* set drive options */
2558 fd->options = *(int *)addr;
2563 if ((fd_debug != 0) != (*(int *)addr != 0)) {
2564 fd_debug = (*(int *)addr != 0);
2565 printf("fd%d: debugging turned %s\n",
2566 fd->fdu, fd_debug ? "on" : "off");
2572 if (priv_check(td, PRIV_DRIVER) != 0)
2574 fd->fdc->fdc_errs = 0;
2578 fsp = (struct fdc_status *)addr;
2579 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2581 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
2585 rid = (struct fdc_readid *)addr;
2586 if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD)
2588 error = fdmisccmd(dev, FDBIO_RDSECTID, addr);